Windows  Mobile  6.0  SDK环境下,如何用DirectShow

(2009-04-15 16:00:17)

http://topic.csdn.net/u/20090303/13/20118bcc-d703-490d-b7e2-1ebe2e4f9446.html

setformat 失败的原因:

问题得到解决,非常感谢野蛮人的参与,让我有信心做下去。
问题解决总结如下:
为什么不能够设置另外的分辨率呢。后面我在资料上看到了一句话:
VIDEO_STREAM_CONFIG_CAPS中,setFormat如果PIN没有连接,当连接的时候就试图用新的格式;如果pin已经连接了,他就会用新的媒体格式重新连接,但是在这任何的一种情况下,下游的filter都有可能拒绝新的媒体格式。
因为我在设置分辨率之前先将pin连接到输出的,因此,重新设置出错了。
最后,我在pin连接之前设置分辨率,就成功了。
总之,谢谢大家,尤其谢谢野蛮人,结贴。

http://topic.csdn.net/u/20080123/14/2AC749B0-EE67-4A56-96F1-FD5C772BDC3F.html

大概发下代码吧
1.初始化
HRESULT hr = S_OK;
hr = CoInitializeEx(NULL,COINIT_MULTITHREADED );
if(FAILED(hr))
{
OutputDebugString(L"CoInitializeEx Failed!\r\n");
CoUninitialize();
return FALSE;
}
// 创建ICaptureGraphBuilder2接口
hr = CoCreateInstance(CLSID_CaptureGraphBuilder, NULL, CLSCTX_INPROC, IID_ICaptureGraphBuilder2,(void**)&pCaptureGraphBuild);
if(FAILED(hr))
{
OutputDebugString(L"CoCreateInstance CaptureGraphBuilder Failed!\r\n");
return FALSE;
}
// 创建IGraphBuilder接口
hr = CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC,IID_IGraphBuilder,(void**)&pGraphBuilder);
if(FAILED(hr))
{
OutputDebugString(L"CoCreateInstance GraphBuilder Failed!\r\n");
return FALSE;
}
hr = pCaptureGraphBuild->SetFiltergraph(pGraphBuilder);
if(FAILED(hr))
{
OutputDebugString(L"pCaptureGraphBuild SetFiltergraph GraphBuilder Failed!\r\n");
return FALSE;
}
2.查找Camera
GUID guidCamera = { 0xCB998A05, 0x122C, 0x4166, 0x84, 0x6A, 0x93, 0x3E, 0x4D, 0x7E, 0x3C, 0x86 };
// Note about the above: The driver material doesn't ship as part of the SDK. This GUID is hardcoded
// here to be able to enumerate the camera drivers and pass the name of the driver to the video capture filter
di.dwSize = sizeof(di);
handle = FindFirstDevice( DeviceSearchByGuid, &guidCamera, &di );
if(( handle == NULL ) || ( di.hDevice == NULL ))
{
OutputDebugString(L"FindCameraDevice Failed!\r\n");
return FALSE;
}
FindClose( handle );
varCamName = di.szLegacyName;
PropBag.Write( _T("VCapName"), &varCamName );
hr = CoCreateInstance(CLSID_VideoCapture, NULL, CLSCTX_INPROC,IID_IBaseFilter, (void**)&pBaseFilter);
if(FAILED(hr))
{
OutputDebugString(L"CoCreateInstance pBaseFilter Failed!\r\n");
return FALSE;
}
hr = pBaseFilter->QueryInterface(IID_IPersistPropertyBag,(void**)&pPropertyBag );
if(FAILED(hr))
{
OutputDebugString(L"pBaseFilter QueryInterface pPropertyBag Failed!\r\n");
return FALSE;
}
pPropertyBag->Load( &PropBag, NULL );
hr = pGraphBuilder->AddFilter(pBaseFilter,_T("Video Capture Filter"));
if(FAILED(hr))
{
OutputDebugString(L"pGraphBuilder AddFilter pBaseFilter Failed!\r\n");
return FALSE;
}
hr = pCaptureGraphBuild->RenderStream (&PIN_CATEGORY_PREVIEW, &MEDIATYPE_Video,pBaseFilter, NULL, NULL);
if (FAILED(hr))
{
OutputDebugString(TEXT("Couldn't render the video capture stream.\r\n"));
pBaseFilter->Release();
return hr;
}
pBaseFilter->Release();
3.连接
HRESULT hr = S_OK;
IMediaControl* pMediaControl = NULL;
IVideoWindow* pVideoWindow = NULL;
//查询IMediaControl接口
hr = pGraphBuilder->QueryInterface(IID_IMediaControl,(void**)&pMediaControl);
if(FAILED(hr))
{
OutputDebugString(L"pGraphBuilder QueryInterface pMediaControl Failed!\r\n");
return FALSE;
}
//查询IVideoWindow
hr = pGraphBuilder->QueryInterface(IID_IVideoWindow,(void**)&pVideoWindow);
if(FAILED(hr))
{
OutputDebugString(L"pGraphBuilder QueryInterface pVideoWindow Failed!\r\n");
pMediaControl->Release();
return FALSE;
}
hr = pGraphBuilder->QueryInterface(IID_IMediaEventEx, (void **)&pMediaEvent);
if(FAILED(hr))
{
OutputDebugString(L"pGraphBuilder QueryInterface pMediaEvent Failed!\r\n");
return FALSE;
}
hr = pMediaEvent->SetNotifyWindow((OAHWND)g_hWnd,WM_GRAPHNOTIFY,0);//自定义消息WM_GRAPHNOTIFY,到第三步里Wndproc里得到该消息
hr = pVideoWindow->put_Owner(OAHWND(g_hWnd));
if(FAILED(hr))
{
OutputDebugString(L"pVideoWindow put_Owner Failed!\r\n");
return FALSE;
}
hr = pVideoWindow->put_WindowStyle(WS_CHILD | WS_CLIPCHILDREN);
if(FAILED(hr))
{
OutputDebugString(L"pVideoWindow put_WindowStyle Failed!\r\n");
return FALSE;
}
hr = pVideoWindow->put_Visible(OATRUE);
if(FAILED(hr))
{
OutputDebugString(L"pVideoWindow put_Visible Failed!\r\n");
return FALSE;
}
hr = pVideoWindow->SetWindowPosition(0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
if(FAILED(hr))
{
OutputDebugString(L"pVideoWindow SetWindowPosition Failed!\r\n");
return FALSE;
}
hr = pVideoWindow->put_FullScreenMode(OATRUE);
hr = pMediaControl->Run();//开始视频捕捉
if(FAILED(hr))
{
OutputDebugString(L"pMediaControl Run Failed!\r\n");
return FALSE;
}
4.播放
case WM_GRAPHNOTIFY:
HandleGraphEvent();
HRESULT HandleGraphEvent(void)
{
LONG evCode, evParam1, evParam2;
HRESULT hr=S_OK;
// Make sure that we don't access the media event interface
// after it has already been released.
if (!pMediaEvent)
return S_OK;
// Process all queued events
while(SUCCEEDED(pMediaEvent->GetEvent(&evCode, (LONG_PTR *) &evParam1,
(LONG_PTR *) &evParam2, 0)))
{
// Free memory associated with callback, since we're not using it
hr = pMediaEvent->FreeEventParams(evCode, evParam1, evParam2);
// If this is the end of the clip, reset to beginning
if(EC_COMPLETE == evCode)
{
LONGLONG pos=0;
// Reset to first frame of movie
hr = pMediaSeeking->SetPositions(&pos, AM_SEEKING_AbsolutePositioning ,NULL, AM_SEEKING_NoPositioning);
if (FAILED(hr))
{
// Some custom filters (like the Windows CE MIDI filter)
// may not implement seeking interfaces (IMediaSeeking)
// to allow seeking to the start.  In that case, just stop
// and restart for the same effect.  This should not be
// necessary in most cases.
if (FAILED(hr = pMediaControl->Stop()))
{
//Msg(TEXT("Failed(0x%08lx) to stop media clip!\r\n"), hr);
break;
}
if (FAILED(hr = pMediaControl->Run()))
{
//Msg(TEXT("Failed(0x%08lx) to reset media clip!\r\n"), hr);
break;
}
}
}
}
return hr;
}

posted on 2011-01-07 00:06  xilentz  阅读(467)  评论(0编辑  收藏  举报