在Windows Mobile 5中使用DirectShow控制摄像头

By Amit Ranjan
July 21, 2006

A number of Windows Mobile 5.0 APIs (for example, SHCameraCapture) make it trivial for a mobile application developer to access a camera, but their ease of use comes at a price—flexibility. Most of the time, using the API directly would offer a solution, but sometimes you need more control and flexibility. That's where Microsoft's DirectShow framework comes in. This article shows how to use DirectShow to access a camera. It demonstrates how to build a filter graph manually and how to handle graph events in the application message handler. Having some prior knowledge of DirectShow and COM will be helpful, but it's not necessary.

Figure 1 depicts the components in the filter graph you will use to capture video.

 

Figure 1: Filter Graph for Video Capture

The camera is the hardware component. For an application to interact with the camera, it would need to talk to its drivers. Next, the video capture filter enables an application to capture video. After capture, you encode the data using WMV9EncMediaObject, a DirectX Media Object (DMO). You can use a DMO inside a filter graph with the help of a DMO Wrapper filter. Next, the encoded video data needs to be multiplexed. You use a Windows Media ASF writer filter for this task. The ASF writer multiplexes the video data and writes it to an .asf file. With that, your filter graph is ready. Now, it's just a matter of running it. As you will see, building the graph is pretty easy too.

Set the Build Environment

First, you need to set the build environment. Add the following libraries in the linker setting of a Visual Studio 2005 Smart Device project:

  • dmoguids.lib
  • strmiids.lib
  • strmbase.lib
  • uuid.lib

Also include the following header files in your project:

  • atlbase.h
  • dmodshow.h
  • dmoreg.h
  • wmcodecids.h

 

Note: For the sake of clarity, this example doesn't show error handling. However, a real world application would require error handling.

Building the Graph

A filter graph that performs audio or video capture is known as a Capture graph. DirectShow provides a Capture Graph Builder object that exposes an interface called ICaptureGraphBuilder2; it exposes methods to help build and control a capture graph.

First, create instances of IGraphBuilder and ICaptureGraphBuilder2 by using the COM function CoCreateInstance:

HRESULT hResult = S_OK;

IGraphBuilder *pFilterGraph;

ICaptureGraphBuilder2 *pCaptureGraphBuilder;

hResult=CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC,

                         IID_IGraphBuilder,(void**)&pFilterGraph);

hResult=CoCreateInstance(CLSID_CaptureGraphBuilder, NULL,

                         CLSCTX_INPROC, IID_ICaptureGraphBuilder2,

                         (void**)& pCaptureGraphBuilder);

CoCreateInstance takes five parameters:

  1. The first is a class ID.
  2. The second decides whether the object created is part of an aggregator.
  3. The third specifies the context in which the newly created object would run.
  4. The fourth parameter is a reference to the identifier of the interface you will use to communicate with the object.
  5. The last parameter is the address of the variable that receives the interface pointer requested.

Once you have created the IGraphBuilder and ICaptureGraphBulder2 instances, you need to call the SetFilterGraph method of the ICaptureGraphBuilder2 interface:

hResult = m_pCaptureGraphBuilder->SetFiltergraph( pFilterGraph );

The SetFilterGraph method takes a pointer to the IGraphBuilder interface. This specifies which filter graph the capture graph builder will use. If you don't call the SetFilterGraph method, the Capture graph builder automatically creates a graph when it needs it.

Now, you're ready to create an instance of the video capture filter. The following code initializes a Video capture filter, the pointer of which is returned by the CoCreateInstance:

IBaseFilter *pVideoCaptureFilter;

hResult=CoCreateInstance(CLSID_VideoCapture, NULL, CLSCTX_INPROC,

                         IID_IBaseFilter, (void**)&pVideoCaptureFilter);

You then need to get a pointer to IPersistPropertyBag from the video capture filter. You use this pointer to set the capture device (in other words, the camera) that the capture filter will use, as follows:

IPersistPropertyBag *pPropertyBag;

hResult=pVideoCaptureFilter->QueryInterface( &pPropertyBag );

Now, you need to get a handle on the camera you will use to capture video. You can enumerate the available camera devices by using the FindFirstDevice and FindNextDevice functions. You can have multiple cameras present on a device. (HTC Universal is one example.) To keep the code simple for this example, use FindFirstDevice to get the first available camera on the device as follows:

DEVMGR_DEVICE_INFORMATION devInfo;

CComVariant  CamName;

CPropertyBag PropBag;

GUID guidCamera = { 0xCB998A05, 0x122C, 0x4166, 0x84, 0x6A, 0x93,

                    0x3E, 0x4D, 0x7E, 0x3C, 0x86 };

devInfo.dwSize = sizeof(devInfo);

FindFirstDevice( DeviceSearchByGuid, &guidCamera, & devInfo);

CamName=devInfo.szLegacyName

PropBag.Write( _T("VCapName"), &CamName );

pPropertyBag->Load( &PropBag, NULL );

hResult =pFilterGraph->AddFilter( pVideoCaptureFilter,

                                  _T("Video Capture Filter") );

pPropertyBag.Release();

Note the first parameter in the FindFirstDevice, DeviceSearchByGuid. It specifies the search type. Other options are DeviceSearchByLegacyName, DeviceSearchByDeviceName, and so forth. DeviceSearchByGuid is the most reliable way to find a capture device. The information regarding the device is returned in the DEVMGR_DEVICE_INFORMATION structure. You store the szLegacyName value in the CComVariant variable, and you need an object that has implemented IPropertyBag interface.

In the code sample, CPropertyBag is a custom class that has implemented IPropertyBag. This object is needed to pass the capture device name to the filter. The string VCapName identifies the filter property for the name of the video capture device. Once you have set the capture device, you can add the Video capture filter to the filter graph. You use the AddFilter method of the graph manager for this. This method takes two parameters: the first is the pointer to the filter that is to be added, and the second is the name of the filter. The second parameter can be NULL; in this case, the filter graph manager generates a unique name for the filter. If you have provided a name that conflicts with some other filter, the manager will modify the name to make it unique.

You then need to instantiate the WMV9 encoder:

IBaseFilter *pVideoEncoder;

IDMOWrapperFilter *pWrapperFilter;

hResult=CoCreateInstance(CLSID_DMOWrapperFilter, NULL,CLSCTX_INPROC,

                         IID_IBaseFilter, (void**)&pVideoEncoder);

hResult =pVideoEncoder->QueryInterface( &pWrapperFilter );

hResult =pWrapperFilter->Init( CLSID_CWMV9EncMediaObject,

                               DMOCATEGORY_VIDEO_ENCODER );

hResult=pFilterGraph->AddFilter( pVideoEncoder, L"WMV9DMO Encoder");

Because the WMV9 encoder is a DMO, you can't add/use it like other filters. But DirectShow provides a wrapper filter that enables you to use a DMO like any other filter. You first create an instance of the DMO wrapper filter and then initialize the WMV9 encoder DMO with it. After initializing the DMO, you add it into the filter graph as follows:

IBaseFilter *pASFMultiplexer;

IFileSinkFilter *pFileSinkFilter;

hResult = pCaptureGraphBuilder->SetOutputFileName(&MEDIASUBTYPE_Asf, T("//test.asf"), &pASFMultiplexer, &pFileSinkFilter );

You have added the source and the transform filter in the filter graph, so the last thing remaining is adding a sink filter in the graph. For this, you call the SetOutputFileName method of ICaptureGraphBuilder2. The first parameter is a media subtype; the second parameter is the name of the file in which you want to save the video; the third parameter is the address of a pointer that receives the multiplexer's interface; and the fourth parameter receives the file writers' interface.

With that, your filter graph is ready. All you need to do is connect the source filter, encoder, and multiplexer. You can achieve this by using the RenderStream method of the graph builder, as follows:

hResult = pCaptureGraphBuilder->RenderStream( &PIN_CATEGORY_CAPTURE,

                                         &MEDIATYPE_Video,

                                         m_pVideoCaptureFilter,

                                         pVideoEncoder,

                                         pASFMultiplexer );

 

The first parameter is the pin category, which can be NULL to match any category.The second parameter specifies the media type. The third, fourth, and fifth parameters specify a starting filter, an intermediate filter, and a sink filter, respectively. The method connects the source filter to the transform filter and then the transform filter to the sink filter.

Now your graph is ready, and you can start capturing the video.

Controlling the Graph

Before capturing video, you need two more things: the ImediaEventEx and IMediaControl pointers. IMediaEventEx derives from IMediaEvent, which supports event notification from the filter graph and individual filters to the application.ImediaEventEx provides a method to the register window that receives a message when any event occurs.

IMediaControl is an interface exposed by the filter graph that allows an application to control the streaming media through the graph. The application can use this to start, stop, or pause the running graph.The following code sample first queries the filter graph for its IMediaEventEx interface. Once it gets the pointer to the IMediaEventEx interface, it then calls its method SetNotifyWindow, passing it the handle to the window that handles the message. The second parameter is the message that will be passed as notification to the Windows message handler. The third parameter is the instance data (this can be 0):

IMediaEventEx *pMediaEvent;

IMediaControl *pMediaControl;

#define WM_GRAPHNOTIFY WM_APP+1

hResult =pFilterGraph->QueryInterface( IID_IMediaEventEx, (void**)&pMediaEvent );

hResult =pMediaEvent->SetNotifyWindow((OAHWND)hWnd, WM_GRAPHNOTIFY,0);

hResult=pFilterGraph->QueryInterface(&pMediaControl);

hResult =pMediaControl->Run();

When an event occurs, DirectShow will send WM_GRAPHNOTIFY to the specified windows.

Note: WM_GRAPHNOTIFY is used here as an example. This can be any application-defined message.

Next, you get the pointer to the IMediaControl interface. You'll use this interface to control the graph. Call its Run method to put the entire graph into a running state. The following code shows how to start and stop capture by throwing the

ControlStream method of CaptureGraphBuilder:

LONGLONG dwStart  = 0, dwEnd = 0;

WORD wStartCookie = 1, wEndCookie = 2;

dwEnd=MAXLONGLONG;

 

//start capturing

hResult=pCaptureGraphBuilder->ControlStream(&PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,pVideoCaptureFilter, &dwStart, &dwEnd,wStartCookie, wEndCookie);

 

//Stop capturing

dwStart=0;

hResult=pFilterGraph->QueryInterface(&pMediaSeeking );

hResult=pMediaSeeking->GetCurrentPosition( &dwEnd );

hResult= pCaptureGraphBuilder->ControlStream(

   &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video, pVideoCaptureFilter,

   &dwStart, &dwEnd, wStartCookie, wEndCookie );

The code uses the search criteria supplied in the method call to locate an output pin on the capture filter. ControlStream enables an application to control streams without it needing to enumerate filters and pins in the graph.Start and End specify the start and stop times (MAX_LONGLONG is the largest possible reference time value). When you start, the End is set to MAXLONLONG. When you want to stop, you first get the current position of the stream by using the GetCurrentPosition method of the IMediaSeeking interface. You then call the ControlStream method with Start set at 0 and End set at the current position.You now have the graph ready and running. You can start using it to capture and save in an .asf file.

Handling the Graph Events

Because an application will control the graph, you need to write the code to facilitate that. You already have registered the window and message with the filter graph, so the only thing remaining is to handle the message in the window's message handler as follows:

BOOL CALLBACK VidCapDlgProc(HWND hDlg,UINT Msg,WPARAM wParam, LPARAM lParam)

{

   ... ... ... ...

   case WM_GRAPHNOTIFY:

      {

         ProcessGraphMessage();

      }

   ... ... ... ...

}

ProcessGraphMessage()

{

   HRESULT hResult=S_OK;

   long leventCode, param1, param2;

   while(hResult=pEvent->GetEvent(&leventCode, &param1, &param2, 0),

   SUCCEEDED(hResult))

   {

      hResult = pEvent->FreeEventParams(leventCode, param1, param2);

      if (EC_STREAM_CONTROL_STOPPED == leventCode)

      {

         pMediaControl->Stop();

         break;

      }

      else if(EC_CAP_FILE_COMPLETED== leventCode)

      {

         //Handle the file capture completed event

      }

      else if(EC_CAP_FILE_WRITE_ERROR== leventCode)

      {

         //Handle the file write error event

      }

   }

}

You handle the WM_GRAPHNOTIFY message in the windows handler. DirectShow sends this message to the application when any event arises. The application calls a user-defined method to process the events. The GetEvent method of the IMediaEvent interface retrieves the event code and two event parameters from the queue.Because the message loop and event notification are asynchronous, the queue might hold more then one event. Hence, the GetEvent code is called in a loop until it returns a failure code. Also, whenever you call GetEvent, it's important to call FreeEvent to free the resource associated with the event parameter. And, being the good programmer that you are, you won't forget to release the resources afterwards, will you? Call Release on every object that you have created, as follows:

PVideoCaptureFilter->Release ();

pVideoEncoder->Release ();

pMediaEvent ->Release();

pMediaSeeking ->Release();

pASFMultiplexer->Release();

pFileSinkFilter->Release();

pWrapperFilter ->Release();

pFilterGraph->Release();

pCaptureGraphBuilder->Release();

 

What Have You Learned?

You now understand how to create, run, and control a filter graph manually. By using the DirectShow framework to capture from a camera, you gain good control with ease.

时间: 2024-11-05 23:09:29

在Windows Mobile 5中使用DirectShow控制摄像头的相关文章

Windows Media Center 中的“家长控制”

如果您的计算机没有电视调谐器,则要在 Windows Media Center 中播放和录制电视节目需要可选的模拟或数字电视调谐器. http://www.aliyun.com/zixun/aggregation/18283.html">数字媒体的出现提供了对广泛内容的前所未有的访问,其中某些访问可能不适合每个观看者.家长可以通过配置 Windows Media Center 中的"家长控制"来指定和强制适合他们家庭成员的内容. 当启用"家长控制"时

在Windows mobile的控制面板中添加应用

在Windows mobile系统中,用户可以通过设置来访问控制面板的应用程序,软件开发人员也可以通过Windows mobile提供的API函数来访问控制面板的一些信息,例如可以向其中增加一个控制面板的应用. 控制面板应用程序实现为一个Dll中,但必须以cpl为后缀,它导出一个回调函数: LONG CPlApplet(HWND hwndCPl, UINT msg, LPARAM lParam1, LPARAM lParam2); 在用户点击设置时,ctlpnl.exe进程会通过调用CPlApp

Windows Mobile 消息钩子(1)

    在Windows中,设置键盘钩子很多人都做过,但是在windows Mobile系统中并没有直接的函数支持.但是我们可以通过使用undocument api来实现. 一.定义参数 #define WH_KEYBOARD_LL           20  #define HC_ACTION        0  typedef LRESULT(CALLBACK* HOOKPROC)(int code, WPARAM wParam, LPARAM lParam);  typedef HHOOK

Windows Mobile 6.5中使用手势

这个语言参考部分包含了对触摸事件.手势.以及手势动画等编程元素的描述. 触摸API分为两个部分,管理触摸输入的手势API,和控制显示区域如何对用户触摸作出反应的手势物理引擎API. 触摸函数.消息.以及结构体是与鼠标共享的,因为应用程序像处理鼠标左键单击一样处理手写笔事件.想了解其他触摸参考信息,请转到鼠标参考. 索引 触摸手势 介绍窗口触摸,并讨论如何在你的应用程序中实现触摸接口. 手势参考(DTK) 这个API允许你的程序监视触摸输入并对触摸输入进行编程. 物理引擎概览 介绍了物理引擎,以及

Windows Mobile中如何建立GPRS连接以便Socket能正常通信

最近编写一个医疗项目的程序,需要用 Windows Mobile 来做通信处理,需要将手机端的数据通过GPRS传送至公网上的一个服务器上.数据传输我采用的是socket,用数据线+ActiveSync调试通过,数据传输正常,在准备将软件提交给质检部门的时候,用真正的GPRS来做通信测试时,问题出来了,连接始终建立不了,但用手机的IE浏览器却能正常打开网页,而且奇怪的是只要用IE浏览器成功访问过一次网页,我的 socket 就能正常进行数据通信,看来传说中的GPRS常连接被我误解了. 手机开通GP

Windows Server 2008中运用精准粒度密码策略控制

&http://www.aliyun.com/zixun/aggregation/37954.html">nbsp;   在Windows Server 2003或者Windows Server 2000的时候,在一个域中的密码与帐户锁定策略都由默认域安全策略进行控制,域中所有域用户都只能用同一个密码策略,因此,为了企业网络安全起见,在企业中许多IT管理人员都得花心思去说服公司员工遵循密码安全设置的方法,给他们进行培训,为他们介绍密码安全的好处等等,在这上面做了不少工作,但是在许多

Windows Mobile中使用WinCE驱动调试助手的小技巧

      驱动调试助手是针对Windows CE做的,在Windows Mobile中使用会有一些问题,最主要的就是其菜单栏被Windows Mobile系统的任务栏给遮住了,导致相应的功能无法正常使用,如下图所示.             按理来说只要将系统任务栏隐藏就可以,今天在模拟器上实验了一下,看起来是可行的.只是任务栏隐藏后,相应的区域出现画屏,如下图所示.             不知道画屏是不是模拟器的缘故,暂且不管先.驱动调试助手的菜单隐约可见,简单测试了下其中的注册表搜索的功

Windows mobile多国语言实现

介绍一种多国语言的实现办法,这也是微软推荐的方式,打开windows mobile下的windows目录可以看到有很多以MUI为后缀名的文件,例如shellres.dll.0804.mui. shell.dll.0804.mui......我们可以用eXeScope.exe或者resources hacker这样的文件查看器查看一下这些文件究竟是怎么一回事,不难发现文件里面都是一些资源ID和相对应的字符串.也许你就疑惑这是为什么呢?这些文件有什么作用呢?下面分解. MUI是Multilingua

在WPF中使用AForge.net控制摄像头拍照

原文:在WPF中使用AForge.net控制摄像头拍照 利用AForge.net控制摄像头拍照最方便的方法就是利用PictureBox显示摄像头画面,但在WPF中不能直接使用PictureBox.必须通过<WindowsFormsHost></WindowsFormsHost>来提供交换功能.其解决方法如下: 1.按照常规方法新建一个WPF应用程序: 2.添加引用 WindowsFormsIntegration  (与WinForm交互的支持) System.Windows.For