Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (17 posts)

Webcam Component for XNA

Last post 9/28/2018 12:01 PM by claudio85. 16 replies.
  • 4/27/2007 1:16 AM

    Webcam Component for XNA

    Keywords: XNA, Webcam Component, Video Component, Video Texture

    I have created a basic Webcam component for XNA that streams video onto a Texture2D, which can be used to draw on a sprite or geometry. This component uses the DirectShow.NET library, which can be found at  http://sourceforge.net/projects/directshownet/.  The component registers itself as a service in the game application, so downstream components should be able to use it as a normal service.  The zip of the project is available at:


    Download link http://upload2.net/page/download/DKNxt0oSK9ud1qL/xnaGame.zip.html

    Information about uploaded file:
    Link Id: DKNxt0oSK9ud1qL
    File Name: xnaGame.zip
    Size: 510 Kb (523239 bytes)
    Uploaded: 2007-04-27 00:06:15


     

  • 4/27/2007 7:35 AM In reply to

    Re: Webcam Component for XNA

    This is awesome. Now to see what I can do with it :).
  • 5/5/2007 8:18 PM In reply to

    Re: Webcam Component for XNA

    The link is not working. Could you re-upload it again or post at another place?

    Thanks.

     

  • 5/8/2007 11:55 PM In reply to

    Re: Webcam Component for XNA

    I just checked the link and it is working for me.  Unfortunately, the free hosting site requires you to wait for a 1 minute timer to expire before it presents the file for download.  Presumably that is enough time to read the ads on the site.

    I am sorry for the inconvenience.  I will be glad to upload the file to another site if you can suggest one that is preferable.

     

  • 5/9/2007 2:40 PM In reply to

    Re: Webcam Component for XNA

    Mirrored here.
  • 5/14/2007 7:52 PM In reply to

    Re: Webcam Component for XNA

    I'm running the demo file, but I'm not seeing anything but cornflower blue. I tried with two different webcams that I have around here. I wouldn't put too much faith in either of them, but they have worked with other apps and they don't throw exceptions when your app initializes. Any suggestions?
  • 5/16/2007 4:21 PM In reply to

    Re: Webcam Component for XNA

    I downloaded and ran a "straight out of the zip" release version on a clean machine.  At first I also saw only a cornflower screen and thought I might be duplicating your problem.  However, as I sat their looking at it - wondering what was amiss - it suddenly began working, just like it was expected too.  It appears that it is taking some time - about 15 seconds on my clean machine - for everything to JIT compile (or perhaps activate a sleeping camera) and start working for the first time. The second and subsequent times it started instantly, however.  Perhaps your situation is similiar, and you might need to wait a bit for it to start completely.

    Also, if you watch the Output window of Visual Studio express as you run the code, you should see console messages indicating the progress in enumerating, selecting, and streaming from the video device.  I have not coded a mechanism to choose from a selection of video devices; it simply uses the first video device that it finds. If there are errors, they should also show up in the output window.  If it is still not clear what the problem is, it may be that the problem is unique to your setup, and you may need to step thru the code on your specific machine.

  • 7/23/2007 6:45 PM In reply to

    Re: Webcam Component for XNA

    I have refactored this work and posted the following contribution to CodeProject:

    VideoTexture Class and Webcam Application for XNA

    http://www.codeproject.com/csharp/VidTextureClassWebcamApp.asp

    Cheers!

    Steve.

  • 11/13/2008 1:02 AM In reply to

    Re: Webcam Component for XNA

    Has anyone gotten this to run in C# 2008 with XNA 3? Trying to use VS2008's project conversion gives the error "The project type is not supported by this installation." for both of the vcproj files... Also, does DirectShowLib2005 need VS2005 to work for some reason?
  • 1/2/2009 4:07 PM In reply to

    Re: Webcam Component for XNA

    I just downloaded it and really quickly made a vs2008 solution with the necessary changes (only two actually)

    first when creating the 2d videotexture (some parameter that was absolete in 3.0) and
    second changing the types for eventParam1, eventParam2 in the HandleGraphEvents method in VideoTexture.cs from int to IntPtr

    I can only grab one frame then I'll get a bunch of exceptions for the rest of the frames for some reason.

    You can get my version at http://ajden.towfeek.se/xnaVideoTexture.rar

    Feel free to email me at ajden.towfeek [at sign] gmail.com if anybode solves the mistery, I'm not going to put too much effort into it.

    Happy new year! ;-)

     

  • 11/21/2009 9:07 AM In reply to

    Re: Webcam Component for XNA

    Hi Ajden,

    When I run the code you posted, I see a  black screen, and the output panel is writting this text continiously:

    A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.dll


    Do you have any idea to solve this problem?

    Thanks
  • 11/26/2009 1:39 AM In reply to

    Re: Webcam Component for XNA

    Maybe someone who knows more about the SampleGrabber can help me.  I'm capturing and displaying video into another window fine, and am very close to getting a Texture2D out of it.  I have a class that extends ISampleGrabberCB, and I'm setting the callback to my ISampleGrabber to my class, but the callback functions are never fired.  Here's my class:

    // Imports 
    using DirectShowLib; 
    using Microsoft.Xna.Framework; 
    using Microsoft.Xna.Framework.Graphics; 
    using System; 
    using System.Runtime.InteropServices; 
    using System.Runtime.InteropServices.ComTypes; 
    using System.Threading; 
     
    namespace MultiNet 
     
        /// <summary> 
        /// Manages web cam 
        /// </summary> 
        public class WebCam : ISampleGrabberCB, IDisposable 
        { 
     
            // Public Properties: 
            public Texture2D Frame; 
     
            // Private Properties: 
            private ICaptureGraphBuilder2 CaptureGraphBuilder; 
            private byte[] FrameBGR; 
            private byte[] FrameBytes; 
            private bool FrameReady; 
            private IGraphBuilder GraphBuilder; 
            private GraphicsDevice GraphicsDevice; 
            private int Height; 
            private IMediaControl MediaControl; 
            private ISampleGrabber SampleGrabber; 
            private Thread UpdateThread; 
            private IVideoWindow VideoWindow; 
            private int Width; 
     
            /// <summary> 
            /// Manages cursors 
            /// </summary> 
            /// <param name="MultiNetClient">Main application</param> 
            public WebCam(GraphicsDevice GraphicsDevice) 
            { 
     
                // Define properties 
                this.GraphicsDevice = GraphicsDevice; 
                FrameReady = false
                Height = 480; 
                Width = 640; 
     
                // Set  
                Frame = new Texture2D(GraphicsDevice, Width, Height, 1, TextureUsage.None, SurfaceFormat.Color); 
                FrameBGR = new byte[(Width * Height) * 3]; 
                FrameBytes = new byte[(Width * Height) * 4]; 
     
                // Get DirectShow interfaces 
                GraphBuilder = (IGraphBuilder)new FilterGraph(); 
                CaptureGraphBuilder = (ICaptureGraphBuilder2)new CaptureGraphBuilder2(); 
                MediaControl = (IMediaControl)GraphBuilder; 
                VideoWindow = (IVideoWindow)GraphBuilder; 
     
                // Attach the filter graph to the capture graph 
                CaptureGraphBuilder.SetFiltergraph(GraphBuilder); 
     
                // Create the input device 
                object VideoInputObject = null
                IBaseFilter VideoInput = null
     
                // Get the list of input devices 
                IEnumMoniker classEnum; 
                ICreateDevEnum devEnum = (ICreateDevEnum)new CreateDevEnum(); 
                devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice, out classEnum, 0); 
                Marshal.ReleaseComObject(devEnum); 
     
                // We've got devices 
                if (classEnum != null
                { 
     
                    // Find first input device 
                    IMoniker[] moniker = new IMoniker[1]; 
                    if (classEnum.Next(moniker.Length, moniker, IntPtr.Zero) == 0) 
                    { 
     
                        // Bind moniker to a filter object 
                        Guid iid = typeof(IBaseFilter).GUID; 
                        moniker[0].BindToObject(nullnullref iid, out VideoInputObject); 
     
                    } 
     
                    // Release COM objects 
                    Marshal.ReleaseComObject(moniker[0]); 
                    Marshal.ReleaseComObject(classEnum); 
     
                    // Case inout object to filter 
                    VideoInput = (IBaseFilter)VideoInputObject; 
     
                } 
     
                // We've got a device 
                if (VideoInput != null
                { 
     
                    // Create a SampleGrabber Filter and add it to the Filter Graph 
                    SampleGrabber grabber = new SampleGrabber(); 
                    SampleGrabber = (ISampleGrabber)grabber; 
                    GraphBuilder.AddFilter((IBaseFilter)grabber, "Grabber"); 
     
                    // Setup Media type info for the SampleGrabber 
                    AMMediaType Type = new AMMediaType(); 
                    Type.majorType = MediaType.Video; 
                    Type.subType = MediaSubType.RGB24; 
                    Type.formatType = FormatType.VideoInfo; 
                    SampleGrabber.SetMediaType(Type); 
     
                    // Add Capture filter to our graph 
                    GraphBuilder.AddFilter(VideoInput, "Video Capture"); 
     
                    // Set SampleGrabber Properties 
                    SampleGrabber.SetBufferSamples(false); 
                    SampleGrabber.SetOneShot(false); 

                    SampleGrabber.SetCallback((ISampleGrabberCB)this, 1);  <-- Setting callback here

                    SampleGrabber.GetConnectedMediaType(new AMMediaType()); 
     
                    // Render the preview pin on the video capture filter 
                    CaptureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, VideoInput, nullnull); 
     
                    // Hide video window 
                    VideoWindow.put_AutoShow(OABool.False); 
                     
                    // Run update thread 
                    UpdateThread = new Thread(new ThreadStart(UpdateBuffer)); 
                    UpdateThread.Start(); 
     
                    // Start previewing video data 
                    MediaControl.Run(); 
     
                    // Release reference to the filter 
                    Marshal.ReleaseComObject(VideoInput); 
     
                } 
     
            } 
     
            /// <summary> 
            /// Runs update logic 
            /// </summary> 
            public void Update() 
            { 
     
                // Set video data into the Output Frame 
                if (Frame.GraphicsDevice.Textures[0] == Frame) Frame.GraphicsDevice.Textures[0] = null
                Frame.SetData<byte>(FrameBytes); 
     
            } 
     
            /// <summary> 
            /// Clean up 
            /// </summary> 
            public void Dispose() 
            { 
     
                // Stop previewing data 
                if (MediaControl != null) MediaControl.StopWhenReady(); 
     
                // Release DirectShow interfaces 
                Marshal.ReleaseComObject(MediaControl); 
                Marshal.ReleaseComObject(GraphBuilder); 
                Marshal.ReleaseComObject(CaptureGraphBuilder); 
                Marshal.ReleaseComObject(VideoWindow); 
                CaptureGraphBuilder = null
                GraphBuilder = null
                MediaControl = null
                VideoWindow = null
     
                // Kill frame 
                Frame.Dispose(); 
                Frame = null
     
            } 
     

    vvv   These aren't getting called, but they should be

            /// <summary> 
            /// Required public callback from DirectShow SampleGrabber 
            /// </summary> 
            public int BufferCB(double SampleTime, IntPtr pBuffer, int BufferLen) 
            { 
     
                // Copy raw data into BGR byte array 
                Marshal.Copy(pBuffer, FrameBGR, 0, BufferLen); 
     
                // Flag the new frame as available 
                FrameReady = true
     
                // Return S_OK 
                return 0; 
     
            } 
     
            /// <summary> 
            /// Required public callback from DirectShow SampleGrabber 
            /// </summary> 
            public int SampleCB(double SampleTime, IMediaSample pSample) 
            { 
     
                // Return S_OK 
                return 0; 
     
            } 
     
            /// <summary> 
            /// Worker to copy the BGR data from the video stream into the RGBA byte array for the Output Frame. 
            /// </summary> 
            protected void UpdateBuffer() 
            { 
     
                // Initialize indexes 
                int samplePosRGBA = 0; 
                int samplePosRGB24 = 0; 
     
                // Loop 
                while (true
                { 
     
                    // Iterate through rows 
                    for (int y = 0, y2 = Height - 1; y < Height; y++, y2--) 
                    { 
     
                        // Iterate through columns 
                        for (int x = 0; x < Width; x++) 
                        { 
     
                            // Update index 
                            samplePosRGBA = (((y2 * Width) + x) * 4); 
                            samplePosRGB24 = ((y * Width) + x) * 3; 
     
                            // Populate array 
                            FrameBytes[samplePosRGBA + 0] = FrameBGR[samplePosRGB24 + 0]; 
                            FrameBytes[samplePosRGBA + 1] = FrameBGR[samplePosRGB24 + 1]; 
                            FrameBytes[samplePosRGBA + 2] = FrameBGR[samplePosRGB24 + 2]; 
                            FrameBytes[samplePosRGBA + 3] = (byte)255; 
     
                        } 
     
                    } 
     
                    // Make frame unavailable 
                    FrameReady = false
                    while (!FrameReady) Thread.Sleep(20); 
     
                } 
     
            } 
         
        } 
     




  • 1/2/2010 8:59 PM In reply to

    Re: Webcam Component for XNA

    Unable to run the demo (Visual Studio wouldn't convert the old files).

    Has anybody had any success with webcam support for XNA 3.0?
  • 1/14/2010 12:19 PM In reply to

    Re: Webcam Component for XNA

    This exception is being thrown from the VideoComponent constructor:

    throw new Exception("Unable to Load Webcam Component. ",ex); 

    So it can't load my webcam. Is there a way round this or do I have to get another webcam?
  • 1/14/2010 1:11 PM In reply to

    Re: Webcam Component for XNA

    Ok solved that but now I'm getting this:

    An unhandled exception of type 'System.AccessViolationException' occurred in Microsoft.Xna.Framework.dll

    Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

    Happening here:

            public override void Draw(GameTime gameTime)  
            {  
                this.videoSprite.Begin(SpriteBlendMode.None, SpriteSortMode.Deferred, SaveStateMode.SaveState);  
                this.videoSprite.Draw(this.videoTexture.VideoTexture2D, this.destinationRectangle, this.sourceRectangle, this.modulationColor, this.rotation, this.origin, this.spriteFlip, this.layerDepth);  
    -->         this.videoSprite.End();  
            } 

    On the last statement. videoSprite is private but changing that has no effect.
  • 9/4/2010 4:39 PM In reply to

    Re: Webcam Component for XNA

    If i would say want to tint only one particular region of the texture obtained from VideoTexture.cs, how would I go about doing that?

    I tried Getting the color data from the image and storing it in an Array:


    obtainedTexture = webCam.VideoTexture.VideoTexture2D; 
     
    Color[] colors1D = new Color[obtainedTexture.Width * obtainedTexture.Height]; 
     
    obtainedTexture.GetData(colors1D); 

    But in this case it runs for 1 frame and then throws an Unhandled Exception:


    An unhandled exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.dll

    Additional information: An unexpected error has occurred.
  • 9/28/2018 12:01 PM In reply to

    Re: Webcam Component for XNA

    Hi. I'm interested in your component for my webcam. I need it for my video transmissions. I'm trying to download the Zip but it gives me a bug. What chance do I have that you send me to my personal email or in some other way?
Page 1 of 1 (17 posts) Previous Discussion Next Discussion