Webcam preview in an Avalon app

Hi,

I am building a small webcam API which is part of a larger final year project at my university. In the samples I've seen the camera preview attached to a window, given its HWND handle. The lines of code looked roughly like this (taken from the PlayCap sample in the DirectShow SDK):

...
HWND ghApp = CreateWindow(...);
...
IID_IVideoWindow g_pVW;
...
g_pVW->put_Owner((OAHWND)ghApp);
...

So, what I want to do is actually have the preview go into an Avalon control. I would appreciate any insights on how this may be accomplished.

Thanks in advance,
Cosmin


Answer this question

Webcam preview in an Avalon app

  • StoneWasHere

    Thanks for the options. I've been searching these days on the internet around the subject. I would like to know if currently there are lower level programming options for Avalon. For example I saw that MediaElement uses MediaPlayer, which in turn resorts to using some API from milcore.dll (which I think it means 'Media Integration Layer'). Is this API public Will it be, and when Where can I get this kind of info

    Cheers,
    Cosmin.

  • nolla

    Here are a few options:

    1) Use the HwndHost class. There should be an article in the SDK about the interoperability architecture that covers this. This allows you to easily insert an Hwnd "black box" into a WPF app, but you will not be able to get any kind of advanced compositing (i.e., this window will always be on top with full opacity). Another option with the same limitation is to use an HwndSource.

    2) You should be able to implement a custom protocol handler using Windows Media Player. A uri in this form can then be passed on to the MediaElement or MediaPlayer classes. More info on the method that needs to be implemented at http://msdn.microsoft.com/library/default.asp url=/library/en-us/wmform95/htm/wmcreatestreamforurl.asp. The only drawback of this is that the protocol is registered locally for all apps.

    3) Set up a streaming server that reads from the webcam, and pass its uri to MediaElement or MediaPlayer.

    Thanks,
    Ed


  • RoshanShah

    The lack of webcam support in WPF is supprising to me. Having applications that interact with an attached camera seems like a very typical "next-gen" case. Must be many developers who would want to have easy interfacing with the camera. I know I will need it in later versions of the software project I'm currently working with.

    So please tell us that it is coming in the first version of WPF!

    Thanks!
    Fredrik

  • Charles Washika

    I would go for the option in which I register a new URI for my webcams, and have Media Player do its thing, but I would like to know the following:

    1. is it possible to access any webcam on my system, even if it's a new one for example if I send the app to a friend, which has a different webcam, or if I add a new one, or have multiple ones, and I want to select which one from the URI, etc.

    2. would I be able to take pictures with lossless quality at the current resolution while the camera is running in WPF also I want to be able to change resolution on the fly, make adjustments like brightness, saturation, contrast, etc (on the fly).

    One more question :) Are you planning to add any support for webcams and the like in WinFX/WPF

    Thanks,
    Cosmin.

  • wang ya zheng

    "3) Set up a streaming server that reads from the webcam, and pass its uri to MediaElement or MediaPlayer."

    This is a good theory but it doesn't work in practicality. I've created a "semi-working" (DirectShow.Net) capture solution which goes something like;

    Live WinTV USB2 Capture -> Demux -> MPEG-2 Audio/Video Decoders -> WM ASF Writer (configured with an IWMWriterNetworkSink.)

    You can configure WM ASF Writer with a stock WMV7/8/9 profile and this does work in a MediaElement by setting the source to "http://localhost:<your configured port>" -- but you end up with a piddly video resolution, your CPU is taxed to death and there is significant delay.

    So then I tried setting a custom profile, sending uncompressed YUY2 samples 720x480 @ 30 fps. The feed works, but only in WMP6 -- WMP10 and MediaElement will not render the uncompressed YUY2 stream. It won't throw an error, it just won't render any video, audio stream renders fine however.

    jmorrill on channel9 suggests:

    "Create a source filter that reads from your tuner source. Implement the IFileSourceFilter. In filter's register code, make it add a custom protocol in there "mycapturesource://". Code your filter so when it asks to open file "mycapturesource://video1", you open up the tuner on that device. Now in your wpf mediaelement just set the media source to "mycapturesource://video1". Voila!"

    I think this might be the only way to go -- if you can figure out how to write your own filter. To a .NET developer this is a pretty daunting task. I assume we would be trying to forward a compressed program stream directly from the capture device to the requester, and let WMP10 figure out which splitter/decoder to use (undesirable, I personally would like to let the user pick their favorite MPEG2 decoder.)


  • gzou1

    I have another idea, which may be a better approach: having the DirectShow deliver the preview to an offscreen DirectDraw surface, and then this could be applied to a mesh or rectangle in 3D in Avalon. What do you think

  • Dirk Strikwerda

    Has anyone actually managed to use WMCreateStreamForURL to create a stream for MediaElement I can't seem to find any complete examples.

    I have been playing with GraphEdit to see if I could get a DirectShow filter to work as suggested on another similar post, but can't work out how to view the graph in windows media player or MediaElement.

    There must be an easier and more efficient way of getting live video into WPF than having to write to a series of bitmaps and read them back in.


  • omegarazor

    Unfortunately, we don't have plans on exposing lower level programming options for WPF Media at this time. I'm afraid the options above are the only possibilities.

    Thanks for your post,
    Ed


  • Webcam preview in an Avalon app