Click here to Skip to main content
11,641,392 members (59,924 online)
Click here to Skip to main content

VideoTexture Class and Webcam Application for XNA

, 31 Jul 2007 CPOL 151.4K 7.4K 50
Rate this:
Please Sign up or sign in to vote.
A managed C# video texture class, demonstrated in an XNA WebCamComponent and used in a webcam application

Screenshot - XnaWebcamScreenshot.jpg

Figure 1. Screenshot of the xnaVideoViewer application in action

Introduction

"Video textures" are an important class of multimedia objects with interesting uses in video conferencing, visualization, gaming, robotics and image analysis. The most obvious use -- and therefore the usage that I have focused on in this project -- is to map streaming video from a webcam onto a Texture2D object. This is then applied to a sprite and presented as a live video window within an XNA application. In addition to consuming live video from any of your DirectX video devices, the VideoTexture class is also able to consume AVI, MPEG and WMV format video clips.

The VideoTexture class exposes a property called VideoTexture2D, which is intended to be usable wherever a normal Texture2D object might be used within XNA. This means that it can be used to apply a dynamic video texture onto both sprites and geometric meshes/models. It can also be manipulated by pixel, vertex and geometry shaders, just like any other texture.

Using the code

To use the VideoTexture class, you should:

  1. Copy the VideoTexture.cs file into your own project.
  2. Change the namespace in the VideoTexture.cs file to the name of your project namespace.
  3. Add a reference to DirectShowLib-2005.dll from the DirectShow.NET library.
  4. Compile it. The VideoTexture class should now be available in your project.

The XnaVideoViewer application demonstrates the usage of VideoComponent, which in turn demonstrates the usage of the VideoTexture class.

Points of interest

  • The VideoTexture class depends upon the managed DirectShow.NET library.
  • The VideoTexture.VideoTexture2D property can be polled by other classes to get the most current frame of video. This is what happens, for example, in the VideoComponent.Draw() method. Alternatively, classes that want to use VideoTexture can subscribe to VideoTexture.NewVideoTextureEvent and have the current frame of video pushed to them in NewVideoTextureEventArgs each time a new frame is grabbed from the video stream.
  • VideoComponent automatically registers itself as a service within the game application that uses it. It can be referenced through the Services collection via any other components used by the application.
  • Due to framework limitations within XNA -- specifically, the absence of the System.Windows namespace -- the desired source of video input (i.e. cameras or files) needs to be coded manually in the xnaVideoViewer application. This is done in the LoadComponents() method.
  • The xnaVideoViewer application maps the following commands to the XBox 360 controller: Run = A (Green); Pause = Y (Yellow); Stop = B (Red); Exit = Back.
  • The VideoTexture class has been used to provide the video stream for an experimental HUD application. It has recently been used to stream video textures through vertex shaders to correct lens distortions in real-time as a prelude to stitching the streams into a video mosaic.

History

  • 30 July 2007 - Version 1.1.0.0 uploaded to CodeProject. Changes include renaming and significant refactoring of the xnaVideoViewer, VideoComponent and VideoTexture classes. Support for AVI, MPEG and WMV video files was added. Support for Run, Pause and Stop was added. Support for Looping video clips was added. Two separate methods for capturing and handling DirectShow Filter Graph events were added. Additional error handling code was added.
  • 21 July 2007 - Version 1.0.0.0 uploaded to CodeProject.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Stephen Bogner
Engineer Defence R&D Canada
Canada Canada
Stephen Bogner is a Senior Research Engineer with Defence R&D Canada. As the Head Autonomous Applications Group, Autonomous Intelligent Systems Section, he only programs when it can't be avoided, and then only in C#.

You may also be interested in...

Comments and Discussions

 
GeneralTinting a region... Pin
b0iNg4-Sep-10 3:41
memberb0iNg4-Sep-10 3:41 
GeneralWebcam problem: A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.dll Pin
jrbosch20-Nov-09 22:48
memberjrbosch20-Nov-09 22:48 
GeneralRe: Webcam problem: A first chance exception of type 'System.InvalidOperationException' occurred in Microsoft.Xna.Framework.dll Pin
barcsik18-Dec-09 12:52
memberbarcsik18-Dec-09 12:52 
GeneralFramerate issues on a Macbook Pro running XP (boot-camp) Pin
Yossi Landesman26-Aug-09 7:25
memberYossi Landesman26-Aug-09 7:25 
QuestionHow to make a webcam texture? Pin
Kim Seonghyune29-Jul-09 19:27
memberKim Seonghyune29-Jul-09 19:27 
GeneralVideo in 3D Pin
oleksii.mdr10-Mar-09 2:35
memberoleksii.mdr10-Mar-09 2:35 
GeneralVS2008, XNA 3.0, DirectShow.NET 2.0 ... almost working Pin
ajdto2342-Jan-09 5:24
memberajdto2342-Jan-09 5:24 
GeneralRe: VS2008, XNA 3.0, DirectShow.NET 2.0 ... almost working Pin
MrDytmire31-Jan-09 2:55
memberMrDytmire31-Jan-09 2:55 
QuestionHow to dump a Frame? Pin
yipling14-Oct-08 4:09
memberyipling14-Oct-08 4:09 
GeneralDirectShowLib-2005.dll Heeeeelp Pin
sashdude7-Jul-08 16:30
membersashdude7-Jul-08 16:30 
GeneralRe: DirectShowLib-2005.dll Heeeeelp Pin
Stephen Bogner8-Jul-08 3:25
memberStephen Bogner8-Jul-08 3:25 
GeneralRe: DirectShowLib-2005.dll Heeeeelp Pin
sashdude9-Jul-08 4:14
membersashdude9-Jul-08 4:14 
GeneralRe: DirectShowLib-2005.dll Heeeeelp Pin
Stephen Bogner9-Jul-08 4:45
memberStephen Bogner9-Jul-08 4:45 
GeneralToggleFullScreen problem. Pin
amar219-Jun-08 11:37
memberamar219-Jun-08 11:37 
Generalproblem capturing the screen Pin
cheskochesko22-May-08 23:56
membercheskochesko22-May-08 23:56 
GeneralProblem with the new xna release 2.0 Pin
Mert Ozdag16-Mar-08 17:46
memberMert Ozdag16-Mar-08 17:46 
GeneralRe: Problem with the new xna release 2.0 Pin
Stephen Bogner17-Mar-08 3:03
memberStephen Bogner17-Mar-08 3:03 
GeneralAudio Pin
Alexandre Ribeiro de Sá21-Feb-08 15:58
memberAlexandre Ribeiro de Sá21-Feb-08 15:58 
GeneralRe: Audio Pin
eclere8-Sep-09 10:30
membereclere8-Sep-09 10:30 
GeneralComposite video input Pin
cheskochesko5-Feb-08 20:57
membercheskochesko5-Feb-08 20:57 
GeneralChanges Necessary for XNA 2.0 Pin
Stephen Bogner14-Jan-08 9:28
memberStephen Bogner14-Jan-08 9:28 
Generalcompile error Pin
odinlai10-Jan-08 9:24
memberodinlai10-Jan-08 9:24 
GeneralRe: compile error Pin
Stephen Bogner14-Jan-08 4:14
memberStephen Bogner14-Jan-08 4:14 
GeneralRe: compile error Pin
Stephen Bogner14-Jan-08 7:04
memberStephen Bogner14-Jan-08 7:04 
QuestionHow to play two avi files simultaneously? Pin
Omid.Q.Rose24-Dec-07 6:30
memberOmid.Q.Rose24-Dec-07 6:30 
GeneralHigher Resolution [modified] Pin
Stephen Bogner11-Oct-07 6:29
memberStephen Bogner11-Oct-07 6:29 
Generalhigher resolution for webcam Pin
sccorpius10-Oct-07 12:45
membersccorpius10-Oct-07 12:45 
GeneralIntegration problem Pin
cheskochesko4-Oct-07 23:08
membercheskochesko4-Oct-07 23:08 
QuestionParameters problem Pin
cheskochesko2-Oct-07 21:06
membercheskochesko2-Oct-07 21:06 
AnswerRe: Parameters problem (solved) Pin
cheskochesko2-Oct-07 22:10
membercheskochesko2-Oct-07 22:10 
GeneralRe: Parameters problem (solved) Pin
cheskochesko3-Oct-07 0:07
membercheskochesko3-Oct-07 0:07 
GeneralNetwork Webcam Pin
freshonlineMax5-Sep-07 19:28
memberfreshonlineMax5-Sep-07 19:28 
GeneralRe: Network Webcam Pin
Stephen Bogner6-Sep-07 11:12
memberStephen Bogner6-Sep-07 11:12 
GeneralRe: Network Webcam Pin
alhambra-eidos5-May-08 23:43
memberalhambra-eidos5-May-08 23:43 
GeneralJodo, que trasto Pin
chemita_cpu31-Aug-07 2:06
memberchemita_cpu31-Aug-07 2:06 
GeneralRe: Jodo, que trasto Pin
Stephen Bogner4-Sep-07 3:32
memberStephen Bogner4-Sep-07 3:32 
GeneralVoice Pin
Omid.Q.Rose19-Aug-07 11:19
memberOmid.Q.Rose19-Aug-07 11:19 
GeneralRe: Voice Pin
Stephen Bogner20-Aug-07 9:16
memberStephen Bogner20-Aug-07 9:16 
GeneralRe: Voice Pin
Omid.Q.Rose20-Aug-07 11:17
memberOmid.Q.Rose20-Aug-07 11:17 
GeneralVideoTexture2D Pin
Omid.Q.Rose19-Aug-07 8:21
memberOmid.Q.Rose19-Aug-07 8:21 
GeneralRe: VideoTexture2D Pin
Stephen Bogner20-Aug-07 9:11
memberStephen Bogner20-Aug-07 9:11 
AnswerRe: VideoTexture2D Pin
Omid.Q.Rose20-Aug-07 10:56
memberOmid.Q.Rose20-Aug-07 10:56 
Questionhelp please Pin
k_hammami20058-Aug-07 23:03
memberk_hammami20058-Aug-07 23:03 
AnswerRe: help please Pin
Stephen Bogner9-Aug-07 3:35
memberStephen Bogner9-Aug-07 3:35 
GeneralCrush!!! Debugger error Pin
jan10241881-Aug-07 9:47
memberjan10241881-Aug-07 9:47 
GeneralRe: Crush!!! Debugger error Pin
jan10241881-Aug-07 10:24
memberjan10241881-Aug-07 10:24 
GeneralRe: Crush!!! Debugger error Pin
Stephen Bogner1-Aug-07 10:54
memberStephen Bogner1-Aug-07 10:54 
GeneralRe: Crush!!! Debugger error Pin
Stephen Bogner1-Aug-07 10:53
memberStephen Bogner1-Aug-07 10:53 
GeneralRe: Crush!!! Debugger error Pin
jan10241881-Aug-07 11:04
memberjan10241881-Aug-07 11:04 
GeneralRe: Crush!!! Debugger error Pin
Stephen Bogner1-Aug-07 11:20
memberStephen Bogner1-Aug-07 11:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150731.1 | Last Updated 31 Jul 2007
Article Copyright 2007 by Stephen Bogner
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid