网络视音频系统主要功能就在于视音频的采集,网络传输两个方面,通过Video Capture系列API函数,你就可以轻松的搞定视频捕捉,但是对于视频的网络传输,则要费一番功夫了。对于视音频数据的传输,只简单地使用数据报套接字传输音视频数据是不可行的,还必须在UDP层上采用RTP(实时传输协议)和RTCP(实时传输控制协议)来改善服务质量。实时传输协议提供具有实时特征的、端到端的数据传输服务。我们在音视频数据前插入包含有载荷标识、序号、时间戳和同步源标识符的 RTP包头,然后利用数据报套接字在IP网络上传输RTP包,以此改善连续重放效果和音视频同步。实时传输控制协议RTCP用于RTP的控制,它最基本的功能是利用发送者报告和接收者报告来推断网络的服务质量,若拥塞状况严重,则改用低速率编码标准或降低数据传输比特率,以减少网络负荷,提供较好的Q.S 保证。
Directshow对于音视频的采集提供了很好的接口,利用ICaptureGraphBuilder2接口可以很轻松的建立起视频捕捉的graph图,通过枚举音频设备Filter,也可以很轻松的实现音频的捕捉,有点麻烦的是音视频数据的传输,我们可以自己封装RTP和 RTCP的协议,来自己实现一个filter,用来发送和接收音视频数据,当然了Directshow也提供了一组支持使用RTP协议的网络传输多媒体流的Filters。你也完全可以用Directshow提供的RTP系列的filter实现数据的传输。
下面分析一下这些RTP Filters。
新定义的Filter包括 RTP Source Filter ,RTP Render Filter,RTP Demux Filter,RTP Receive Playload Handler (RPH) filter,RTP Send Payload (SPH) filter,使用这5个filter构建一个通过RTP协议传输音视频数据的Graph是没有问题的。
RTP Source filter被用来从一个单独的RTP会话中接收RTP和RTCP包。这个filter提供一个指定发送给其它主机RTCP接收器报告和指定网络地址和端口接口来接收RTP会话的接口。
RTP Rend filter是用来将数据发到网络上的一个filter,这个filter也提供了和RTP source Filter 类似的接口。
RTP Demux filter用来多路分离来自 RTP Source filter的RTP 包,这个filter有一个或者多个输出的pin。这个Filter提供了如何控制多路分离和如何分配到特定输出pin的接口。
RTP RPH Filter 是用来网络过来的RTP包还原成原来的数据格式,主要支持H.261,H.263,Indeo,G.711,G.723和G.729和常见的多种音视频负载类型。
RTP SPH filter则和RPH filter的功能相对,它的任务是将音视频 压缩filter输出的 数据分解为RTP包,它提供的接口有指定最大生成包大小和pt值。
下面我们看看如何用这些filter来搭建我们采集和传输的graph图。
|
图3 网络视频和音频交互的Graph图 |
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 ULONG cFetched; if (!ppSrcFilter) return E_POINTER; // Create the system device enumerator CComPtr 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 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 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; } |