static const GUID CLSID_FG729Render = { 0x3556f7d8, 0x5b5, 0x4015, { 0xb9, 0x40, 0x65, 0xb8, 0x8, 0x94, 0xc8, 0xf9 } }; //音频发送 static const GUID CLSID_FG729Source = { 0x290bf11a, 0x93b4, 0x4662, { 0xb1, 0xa3, 0xa, 0x53, 0x51, 0xeb, 0xe5, 0x8e } };//音频接收 static const GUID CLSID_FH263Source = { 0xa0431ccf, 0x75db, 0x463e, { 0xb1, 0xcd, 0xe, 0x9d, 0xb6, 0x67, 0xba, 0x72 } };//视频接收 static const GUID CLSID_FH263Render = { 0x787969cf, 0xc1b6, 0x41c5, { 0xba, 0xa8, 0x4e, 0xff, 0xa3, 0xdb, 0xe4, 0x1f } };//视频发送 //发送和接收音视频数据的filter CComPtr< IBaseFilter > m_pAudioRtpRender ; CComPtr< IBaseFilter > m_pAudioRtpSource ; CComPtr< IBaseFilter > m_pVideoRtpRender ; CComPtr< IBaseFilter > m_pVideoRtpSource ;
char szClientA[100]; int iVideoPort = 9937; int iAudioPort = 9938;
//构建视频的graph图,并发送数据 CComPtr< IGraphBuilder > m_pVideoGraphBuilder; //视频图形管理器 CComPtr< ICaptureGraphBuilder2 > m_pVideoCapGraphBuilder; CComPtr< IBaseFilter > m_pFilterVideoCap; CComPtr< IVideoWindow > m_pVideoWindow; CComPtr< IMediaControl > m_pVideoMediaCtrl ; CComPtr< IBaseFilter > m_pVideoRenderFilter;
HRESULT CMyDialog::VideoGraphInitAndSend() { HRESULT hr; hr =m_pVideoGraphBuilder.CoCreateInstance( CLSID_FilterGraph ); if(FAILED(hr)) return hr; hr =m_pVideoCapGraphBuilder.CoCreateInstance( CLSID_CaptureGraphBuilder2); if(FAILED (hr)) return hr; m_pVideoCapGraphBuilder->SetFiltergraph(m_pVideoGraphBuilder); m_pVideoGraphBuilder->QueryInterface(IID_IMediaControl, (void **)&m_pVideoMediaCtrl); m_pVideoGraphBuilder->QueryInterface(IID_IVideoWindow,(void**)&m_pVideoWindow)
FindDeviceFilter(&m_pFilterVideoCap,CLSID_VideoInputDeviceCategory); if(m_pFilterVideoCap) m_pVideoGraphBuilder->AddFilter( m_pFilterVideoCap,T2W("VideoCap") ) ; //创建预览的filter hr = m_pRenderFilterVideo.CoCreateInstance(CLSID_VideoRenderer); if(FAILED(hr)) return hr; m_pVideoGraphBuilder->AddFilter( m_pRenderFilterVideo, L"VideoRenderFilter" ); Connect(m_pFilterVideoCap ,m_pRenderFilterVideo) ; //设置预览的窗口
CRect rc ; GetClientRect(m_hOwnerWnd, &rc ); int iWidth = rc.right - rc.left ; int iHeight = rc.bottom - rc.top ; int iLeft, iTop; if((iHeight*1.0)/(iWidth*1.0) >= 0.75) { //按宽度算 int tmpiHeight = iWidth*3/4; iTop = (iHeight - tmpiHeight)/2; iHeight = tmpiHeight; iLeft = 0; } else { //按高度算 int tmpiWidth = iHeight*4/3; iLeft = (iWidth - tmpiWidth)/2; iWidth = tmpiWidth; iTop = 0; } m_pVideoWindow->put_Owner( (OAHWND) m_hPreviewWnd ) ; m_pVideoWindow->put_Visible( OATRUE ); m_pVideoWindow->put_WindowStyle( WS_CHILD | WS_CLIPSIBLINGS ) ;
//连接到网络并发送 CComPtr< IRtpOption > pRenderOption; CComPtr< IVideoOption > pVideoOption;
tagVideoInfo vif(160,120,24); int t=((int)(m_iFrameRate/5)*5)+5; vif.nBitCount=24; vif.nWidth=160; vif.nHeight=120;
hr = ::CoCreateInstance(CLSID_FH263Render, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void **)&m_pVideoRtpRender); if(FAILED(hr)) return hr; m_pVideoRtpRender->QueryInterface(IID_IJRTPOption, (void**)&pRenderOption); m_pVideoRtpRender->QueryInterface(IID_IVideoOption,(void**)&pVideoOption); pVideoOption->SetProperty(&vif); pVideoOption->SetSendFrameRate(m_iFrameRate,1);//1 不发送数据,0 实际发送数据 Connect(m_pFilterVideoCap ,m_pVideoRtpRender) ; //连接对方 hr= pRenderOption->Connect(szClientA,iVideoPort,1024); if(FAILED(hr)) return hr; m_pVideoMediaCtrl->Run(); } //视频的接收 CComPtr< IGraphBuilder > m_pVideoGraphBuilder; //视频图形管理器 CComPtr< IBaseFilter > m_pFilterVideoCap; CComPtr< IVideoWindow > m_pVideoWindow; CComPtr< IMediaControl > m_pVideoMediaCtrl ; CComPtr< IBaseFilter > m_pVideoRenderFilter; HWND m_hRenderWnd ; HRESULT VideoRecive() { HRESULT hr; hr=CoCreateInstance(CLSID_FilterGraph,NULL,CLSCTX_INPROC, IID_IFilterGraph,(void**)&m_pVideoGraphBuilder);
m_pVideoGraphBuilder->QueryInterface(IID_IMediaControl, (void **)&m_pVideoMediaCtrl); m_pVideoGraphBuilder->QueryInterface(IID_IVideoWindow,(void**)&m_pVideoWindow) hr = ::CoCreateInstance(CLSID_FH263Source, NULL, CLSCTX_INPROC, IID_IBaseFilter, (void **)&m_pVideoRtpSource); if(FAILED(hr)) return hr; m_pVideoGraphBuilder->AddFilter(m_pVideoRtpSource, L"My Custom Source");
CComPtr< IRtpOption > m_pRtpOption; CComPtr< IVideoOption > m_pVideoOption; m_pVideoRtpSource->QueryInterface(IID_IJRTPOption, (void **)&m_pRtpOption); m_pVideoRtpSource->QueryInterface(IID_IVideoOption, (void **)&m_pVideoOption);
tagVideoInfo vif(160, 120 ,24); m_pVideoOption->SetProperty(&vif); hr= pRenderOption->Connect(szClientA,iVideoPort +1,1024); if(FAILED(hr)) return hr;
//创建预览的filter hr = m_pRenderFilterVideo.CoCreateInstance(CLSID_VideoRenderer); if(FAILED(hr)) return hr; m_pVideoGraphBuilder->AddFilter( m_pRenderFilterVideo, L"VideoRenderFilter" ); Connect(m_pVideoRtpSource ,m_pRenderFilterVideo) ;
CRect rc ; GetClientRect(m_hOwnerWnd, &rc ); int iWidth = rc.right - rc.left ; int iHeight = rc.bottom - rc.top ; int iLeft, iTop; if((iHeight*1.0)/(iWidth*1.0) >= 0.75) { //按宽度算 int tmpiHeight = iWidth*3/4; iTop = (iHeight - tmpiHeight)/2; iHeight = tmpiHeight; iLeft = 0; } else { //按高度算 int tmpiWidth = iHeight*4/3; iLeft = (iWidth - tmpiWidth)/2; iWidth = tmpiWidth; iTop = 0; } m_pVideoWindow->put_Owner( (OAHWND) m_hRenderWnd ) ; m_pVideoWindow->put_Visible( OATRUE ); m_pVideoWindow->put_WindowStyle( WS_CHILD | WS_CLIPSIBLINGS ) ; m_pVideoMediaCtrl->Run();
return S_OK; } // HRESULT FindDeviceFilter(IBaseFilter ** ppSrcFilter,GUID deviceGUID) { HRESULT hr; IBaseFilter * pSrc = NULL; CComPtr <IMoniker> pMoniker =NULL; ULONG cFetched;
if (!ppSrcFilter) return E_POINTER;
// Create the system device enumerator CComPtr <ICreateDevEnum> pDevEnum =NULL;
hr = CoCreateInstance (CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC, IID_ICreateDevEnum, (void **) &pDevEnum); if (FAILED(hr)) return hr;
// Create an enumerator for the video capture devices CComPtr <IEnumMoniker> pClassEnum = NULL;
hr = pDevEnum->CreateClassEnumerator (deviceGUID, &pClassEnum, 0); if (FAILED(hr)) return hr;
if (pClassEnum == NULL) return E_FAIL;
if (S_OK == (pClassEnum->Next (1, &pMoniker, &cFetched))) { hr = pMoniker->BindToObject(0,0,IID_IBaseFilter, (void**)&pSrc); if (FAILED(hr)) return hr; } else return E_FAIL;
*ppSrcFilter = pSrc;
return S_OK; }
//构建音频Graph图,并发送 CComPtr< IGraphBuilder > m_pAudioGraphBuilder; //音频图形管理器 CComPtr< ICaptureGraphBuilder2 > m_pCapAudioGraphBuilder; CComPtr< IBaseFilter > m_pFilterAudioCap; CComPtr< IMediaControl > m_pAudioMediaCtrl ;
HRESULT AudioGraphInit() { HRESULT hr; hr =m_pAudioGraphBuilder.CoCreateInstance( CLSID_FilterGraph ); if(FAILED(hr)) return hr; hr =m_pCapAudioGraphBuilder.CoCreateInstance( CLSID_CaptureGraphBuilder2); if(FAILED (hr)) return hr; m_pAudioGraphBuilder->SetFiltergraph(m_pCapAudioGraphBuilder); m_pAudioGraphBuilder->QueryInterface(IID_IMediaControl, (void **)&m_pAudioMediaCtrl);
FindDeviceFilter(&m_pFilterVideoCap,CLSID_AudioInputDeviceCategory); if(m_pFilterAudioCap) m_pAudioGraphBuilder->AddFilter( m_pFilterAudioCap,T2W("AudioCap") ) ;
//发送到网络 hr =::CoCreateInstance(CLSID_FG729Render,NULL,CLSCTX_INPROC, IID_IBaseFilter,(void**)&m_pFilterRtpSendAudio) if(FAILED(hr)) return hr; m_pAudioGraphBuilder->AddFilter(m_pAudioRtpRender, L"FilterRtpSendAudio"); Connect(m_pFilterAudioCap,m_pAudioRtpRender);
CComPtr< IRtpOption > pOption ; m_pAudioRtpRender->QueryInterface(IID_IJRTPOption,(void**)&pOption) hr =pOption->Connect(szClientA,iAudioPort,1024); if(FAILED(hr)) return hr;
m_pAudioMediaCtrl->Run(); return S_OK; } //音频的接收 CComPtr< IGraphBuilder > m_pAudioGraphBuilder; //音频图形管理器 CComPtr< ICaptureGraphBuilder2 > m_pCapAudioGraphBuilder; CComPtr< IBaseFilter > m_pFilterAudioCap; CComPtr< IMediaControl > m_pAudioMediaCtrl ; CComPtr<IBaseFilter> m_pAudioRender; HRESULT AudioRecive() { HRESULT hr; hr =m_pAudioGraphBuilder.CoCreateInstance( CLSID_FilterGraph ); if(FAILED(hr)) return hr; m_pAudioGraphBuilder->QueryInterface(IID_IMediaControl, (void **)&m_pAudioMediaCtrl);
hr = m_pAudioRtpSource->CoCreateInstance(CLSID_FG729Source) ; if(FAILED(hr)) return hr; m_pAudioGraphBuilder->AddFilter(m_pAudioRtpSource,L"AudioRtp"); //创建声卡Renderfilter FindDeviceFilter(&m_pAudioRender,CLSID_AudioRendererCategory); m_pAudioGraphBuilder->AddFilter(m_pAudioRender,L"AudioRender"); CComPtr< IRtpOption > pRtpOption ; m_pAudioRtpSource->QueryInterface(IID_IJRTPOption,(void**)&pRtpOption) hr= pRtpOption->Connect(szClientA,iAudioPort+2,1024); if(FAILED (hr)) return hr;
Connect(m_pAudioRtpSource,m_pAudioRender); m_pAudioMediaCtrl->Run(); return S_OK; } |