HRESULT CSampleCGB::FindEncoder( IEnumMoniker *pEncoders, REGPINMEDIUM pinMedium, IBaseFilter **ppEncoder ) { if( ! pEncoders ) { return E_INVALIDARG; } if(IsEqualGUID(pinMedium.clsMedium,GUID_NULL) || IsEqualGUID(pinMedium.clsMedium,KSMEDIUMSETID_Standard)) { return E_INVALIDARG; } HRESULT hr; SmartPtr<IBaseFilter> pFilter; SmartPtr<IMoniker> pMoniker; ULONG cFetched; SmartPtr<IPin> pPin; while(pFilter.Release(),pMoniker.Release(),S_OK == pEncoders->Next(1,&pMoniker,&cFetched)) { hr = pMoniker->BindToObject(0,0,IID_IBaseFilter,(void**)&pFilter); if(FAILED(hr))continue; hr = FindPin(pFilter,pinMedium,PINDIR_INPUT,TRUE,&pPin); if(SUCCEEDED(hr)) { *ppEncoder = pFilter.Detach(); return hr; } } return E_FAIL; }
HRESULT ISampleCaptureGraphBuilder::ConnectMultiplexerToMPEG2Demux( IBaseFilter *pEncoder, IEnumMoniker *pMultiplexers ) { if( !pEncoder || !pMultiplexers ) { return E_INVALIDARG; } SmartPtr<IBaseFilter> pFilter; SmartPtr<IMoniker> pMoniker; ULONG fetched; HRESULT hr; while( pFilter.Release(), pMoniker.Release(), S_OK == pMultiplexers->Next( 1, &pMoniker, &fetched ) ) { hr = pMoniker->BindToObject( 0, 0, IID_IBaseFilter, reinterpret_cast<void **>( &pFilter ) ); if( FAILED( hr ) ) { continue; } hr = graph_->AddFilter( pFilter, NULL ); if( FAILED( hr ) ) { continue; } // // connect the encoder to the multiplexer // hr = ConnectFilters( pEncoder, pFilter ); if( FAILED( hr ) ) { graph_->RemoveFilter( pFilter ); continue; } // // connect the multiplexer to the encoder // hr = ConnectFilters( pFilter, pMPEG2Demux_ ); if( SUCCEEDED( hr ) ) { pMultiplexer_ = pFilter; return S_OK; } } return E_FAIL; }
HRESULT ISampleCaptureGraphBuilder::RenderToMPEG2Demux( IPin *pPin, IEnumMoniker *pEncoders) { if( !pPin || !pEncoders ) { return E_INVALIDARG; } REGPINMEDIUM pinMedium ; pinMedium.clsMedium = GUID_NULL; pinMedium.dw1 = 0; pinMedium.dw2 = 0; SmartPtr<IBaseFilter> pFilter; SmartPtr<IMoniker> pMoniker; ULONG fetched; HRESULT hr; while( pFilter.Release(), pMoniker.Release(), S_OK == pEncoders->Next( 1, &pMoniker, &fetched ) ) { hr = pMoniker->BindToObject( 0, 0, IID_IBaseFilter, reinterpret_cast<void **>( &pFilter ) ); if( FAILED( hr ) ) { continue; } hr = graph_->AddFilter( pFilter, L"Encoder" ); if( FAILED( hr ) ) { continue; } hr = ConnectPin( pPin, pFilter ); if( FAILED( hr ) ) { graph_->RemoveFilter( pFilter ); continue; } hr = ConnectEncoderToMPEG2Demux( pFilter, pinMedium ); if( SUCCEEDED( hr ) ) { pEncoder_ = pFilter; return S_OK; } graph_->RemoveFilter( pFilter ); } return E_FAIL; }
HRESULT ISampleCaptureGraphBuilder::ConnectAudioPinToMultiplexer( IPin *pPin, IBaseFilter *pMultiplexer) { if( !pPin || !pMultiplexer ) { return E_INVALIDARG; } REGPINMEDIUM pinMedium; HRESULT hr = GetMedium( pPin, pinMedium ); if( FAILED( hr ) ) { return hr; } SmartPtr<IBaseFilter> pEncoder; SmartPtr<IEnumMoniker> pEncoders; if( FALSE == ::IsEqualGUID( pinMedium.clsMedium, GUID_NULL ) ) { // // search through encoders category; identify // the encoder using the medium // hr = GetEncodersByCategory( &pEncoders ); if( FAILED( hr ) ) { return hr; } hr = FindEncoder( pEncoders, pinMedium, &pEncoder ); if( SUCCEEDED( hr ) ) { hr = graph_->AddFilter( pEncoder, L"Audio Encoder" ); if( SUCCEEDED( hr ) && SUCCEEDED( ConnectPin( pPin, pEncoder ) ) && SUCCEEDED( ConnectFilters( pEncoder, pMultiplexer ) ) ) { return S_OK; } } pEncoders = NULL; hr = GetEncodersByEnumerating( pPin, pinMedium, &pEncoders ); if( FAILED( hr ) ) { return hr; } hr = FindEncoder( pEncoders, pinMedium, &pEncoder ); if( SUCCEEDED( hr ) ) { hr = graph_->AddFilter( pEncoder, L"Audio Encoder" ); if( SUCCEEDED( hr ) && SUCCEEDED( ConnectPin( pPin, pEncoder ) ) && SUCCEEDED( ConnectFilters( pEncoder, pMultiplexer ) ) ) { return S_OK; } } return E_FAIL; } // // Search throgh the codec category // hr = GetEncodersByCategory( &pEncoders ); if( FAILED( hr ) ) { return hr; } SmartPtr<IBaseFilter> pFilter; SmartPtr<IMoniker> pMoniker; ULONG fetched; while( pFilter.Release(), pMoniker.Release(), S_OK == pEncoders->Next( 1, &pMoniker, &fetched ) ) { hr = pMoniker->BindToObject( 0, 0, IID_IBaseFilter, reinterpret_cast<void **>( &pFilter ) ); if( FAILED( hr ) ) { continue; } hr = graph_->AddFilter( pFilter, L"Audio Encoder" ); if( FAILED( hr ) ) { continue; } hr = ConnectPin( pPin, pFilter ); if( FAILED( hr ) ) { graph_->RemoveFilter( pFilter ); continue; } hr = ConnectFilters( pFilter, pMultiplexer ); if( SUCCEEDED( hr ) ) { return S_OK; } graph_->RemoveFilter( pFilter ); } return E_FAIL; }