1 /*****************************************************************************
2 * bdagraph.cpp : DirectShow BDA graph for vlc
3 *****************************************************************************
4 * Copyright( C ) 2007 the VideoLAN team
6 * Author: Ken Self <kens@campoz.fslife.co.uk>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 *( at your option ) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
28 /****************************************************************************
29 * Interfaces for calls from C
30 ****************************************************************************/
33 void dvb_newBDAGraph( access_t* p_access )
35 p_access->p_sys->p_bda_module = new BDAGraph( p_access );
38 void dvb_deleteBDAGraph( access_t* p_access )
40 if( p_access->p_sys->p_bda_module )
41 delete p_access->p_sys->p_bda_module;
44 int dvb_SubmitATSCTuneRequest( access_t* p_access )
46 if( p_access->p_sys->p_bda_module )
47 return p_access->p_sys->p_bda_module->SubmitATSCTuneRequest();
51 int dvb_SubmitDVBTTuneRequest( access_t* p_access )
53 if( p_access->p_sys->p_bda_module )
54 return p_access->p_sys->p_bda_module->SubmitDVBTTuneRequest();
58 int dvb_SubmitDVBCTuneRequest( access_t* p_access )
60 if( p_access->p_sys->p_bda_module )
61 return p_access->p_sys->p_bda_module->SubmitDVBCTuneRequest();
65 int dvb_SubmitDVBSTuneRequest( access_t* p_access )
67 if( p_access->p_sys->p_bda_module )
68 return p_access->p_sys->p_bda_module->SubmitDVBSTuneRequest();
72 long dvb_GetBufferSize( access_t* p_access )
74 if( p_access->p_sys->p_bda_module )
75 return p_access->p_sys->p_bda_module->GetBufferSize();
79 long dvb_ReadBuffer( access_t* p_access, long* l_buffer_len, BYTE* p_buff )
81 if( p_access->p_sys->p_bda_module )
82 return p_access->p_sys->p_bda_module->ReadBuffer( l_buffer_len,
88 /*****************************************************************************
90 *****************************************************************************/
91 BDAGraph::BDAGraph( access_t* p_this ):
93 guid_network_type(GUID_NULL),
98 p_tuning_space = NULL;
99 p_tune_request = NULL;
100 p_media_control = NULL;
101 p_filter_graph = NULL;
102 p_system_dev_enum = NULL;
103 p_network_provider = p_tuner_device = p_capture_device = NULL;
104 p_sample_grabber = p_mpeg_demux = p_transport_info = NULL;
105 p_scanning_tuner = NULL;
108 /* Initialize COM - MS says to use CoInitializeEx in preference to
110 CoInitializeEx( 0, COINIT_APARTMENTTHREADED );
113 /*****************************************************************************
115 *****************************************************************************/
116 BDAGraph::~BDAGraph()
122 /*****************************************************************************
123 * Submit an ATSC Tune Request
124 *****************************************************************************/
125 int BDAGraph::SubmitATSCTuneRequest()
131 IATSCChannelTuneRequest* p_atsc_tune_request;
132 IATSCLocator* p_atsc_locator;
133 localComPtr(): p_atsc_tune_request(NULL), p_atsc_locator(NULL) {};
136 if( p_atsc_tune_request )
137 p_atsc_tune_request->Release();
139 p_atsc_locator->Release();
142 long l_major_channel, l_minor_channel, l_physical_channel;
144 l_major_channel = l_minor_channel = l_physical_channel = -1;
146 l_major_channel = var_GetInteger( p_access, "dvb-major-channel" );
147 l_minor_channel = var_GetInteger( p_access, "dvb-minor-channel" );
148 l_physical_channel = var_GetInteger( p_access, "dvb-physical-channel" );
151 guid_network_type = CLSID_ATSCNetworkProvider;
152 hr = CreateTuneRequest();
155 msg_Warn( p_access, "SubmitATSCTuneRequest: "\
156 "Cannot create Tuning Space: hr=0x%8lx", hr );
160 hr = p_tune_request->QueryInterface( IID_IATSCChannelTuneRequest,
161 (void**)&l.p_atsc_tune_request );
164 msg_Warn( p_access, "SubmitATSCTuneRequest: "\
165 "Cannot QI for IATSCChannelTuneRequest: hr=0x%8lx", hr );
168 hr = ::CoCreateInstance( CLSID_ATSCLocator, 0, CLSCTX_INPROC,
169 IID_IATSCLocator, (void**)&l.p_atsc_locator );
172 msg_Warn( p_access, "SubmitATSCTuneRequest: "\
173 "Cannot create the ATSC locator: hr=0x%8lx", hr );
178 if( l_major_channel > 0 )
179 hr = l.p_atsc_tune_request->put_Channel( l_major_channel );
180 if( SUCCEEDED( hr ) && l_minor_channel > 0 )
181 hr = l.p_atsc_tune_request->put_MinorChannel( l_minor_channel );
182 if( SUCCEEDED( hr ) && l_physical_channel > 0 )
183 hr = l.p_atsc_locator->put_PhysicalChannel( l_physical_channel );
186 msg_Warn( p_access, "SubmitATSCTuneRequest: "\
187 "Cannot set tuning parameters: hr=0x%8lx", hr );
191 hr = p_tune_request->put_Locator( l.p_atsc_locator );
194 msg_Warn( p_access, "SubmitATSCTuneRequest: "\
195 "Cannot put the locator: hr=0x%8lx", hr );
199 /* Build and Run the Graph. If a Tuner device is in use the graph will
200 * fail to run. Repeated calls to build will check successive tuner
207 msg_Warn( p_access, "SubmitATSCTuneRequest: "\
208 "Cannot Build the Graph: hr=0x%8lx", hr );
218 /*****************************************************************************
219 * Submit a DVB-T Tune Request
220 ******************************************************************************/
221 int BDAGraph::SubmitDVBTTuneRequest()
227 IDVBTuneRequest* p_dvbt_tune_request;
228 IDVBTLocator* p_dvbt_locator;
229 localComPtr(): p_dvbt_tune_request(NULL), p_dvbt_locator(NULL) {};
232 if( p_dvbt_tune_request )
233 p_dvbt_tune_request->Release();
235 p_dvbt_locator->Release();
238 long l_frequency, l_bandwidth;
240 l_frequency = l_bandwidth = -1;
241 l_frequency = var_GetInteger( p_access, "dvb-frequency" );
242 l_bandwidth = var_GetInteger( p_access, "dvb-bandwidth" );
244 guid_network_type = CLSID_DVBTNetworkProvider;
245 hr = CreateTuneRequest();
248 msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
249 "Cannot create Tune Request: hr=0x%8lx", hr );
253 hr = p_tune_request->QueryInterface( IID_IDVBTuneRequest,
254 (void**)&l.p_dvbt_tune_request );
257 msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
258 "Cannot QI for IDVBTuneRequest: hr=0x%8lx", hr );
261 l.p_dvbt_tune_request->put_ONID( -1 );
262 l.p_dvbt_tune_request->put_SID( -1 );
263 l.p_dvbt_tune_request->put_TSID( -1 );
265 hr = ::CoCreateInstance( CLSID_DVBTLocator, 0, CLSCTX_INPROC,
266 IID_IDVBTLocator, (void**)&l.p_dvbt_locator );
269 msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
270 "Cannot create the DVBT Locator: hr=0x%8lx", hr );
275 if( l_frequency > 0 )
276 hr = l.p_dvbt_locator->put_CarrierFrequency( l_frequency );
277 if( SUCCEEDED( hr ) && l_bandwidth > 0 )
278 hr = l.p_dvbt_locator->put_Bandwidth( l_bandwidth );
281 msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
282 "Cannot set tuning parameters on Locator: hr=0x%8lx", hr );
286 hr = p_tune_request->put_Locator( l.p_dvbt_locator );
289 msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
290 "Cannot put the locator: hr=0x%8lx", hr );
294 /* Build and Run the Graph. If a Tuner device is in use the graph will
295 * fail to run. Repeated calls to build will check successive tuner
302 msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
303 "Cannot Build the Graph: hr=0x%8lx", hr );
313 /*****************************************************************************
314 * Submit a DVB-C Tune Request
315 ******************************************************************************/
316 int BDAGraph::SubmitDVBCTuneRequest()
323 IDVBTuneRequest* p_dvbc_tune_request;
324 IDVBCLocator* p_dvbc_locator;
325 localComPtr(): p_dvbc_tune_request(NULL), p_dvbc_locator(NULL) {};
328 if( p_dvbc_tune_request )
329 p_dvbc_tune_request->Release();
331 p_dvbc_locator->Release();
335 long l_frequency, l_symbolrate;
337 ModulationType i_qam_mod;
339 l_frequency = l_symbolrate = i_qam = -1;
340 l_frequency = var_GetInteger( p_access, "dvb-frequency" );
341 l_symbolrate = var_GetInteger( p_access, "dvb-srate" );
342 i_qam = var_GetInteger( p_access, "dvb-modulation" );
343 i_qam_mod = BDA_MOD_NOT_SET;
345 i_qam_mod = BDA_MOD_16QAM;
347 i_qam_mod = BDA_MOD_32QAM;
349 i_qam_mod = BDA_MOD_64QAM;
351 i_qam_mod = BDA_MOD_128QAM;
353 i_qam_mod = BDA_MOD_256QAM;
355 guid_network_type = CLSID_DVBCNetworkProvider;
356 hr = CreateTuneRequest();
359 msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
360 "Cannot create Tune Request: hr=0x%8lx", hr );
364 hr = p_tune_request->QueryInterface( IID_IDVBTuneRequest,
365 (void**)&l.p_dvbc_tune_request );
368 msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
369 "Cannot QI for IDVBTuneRequest: hr=0x%8lx", hr );
372 l.p_dvbc_tune_request->put_ONID( -1 );
373 l.p_dvbc_tune_request->put_SID( -1 );
374 l.p_dvbc_tune_request->put_TSID( -1 );
376 hr = ::CoCreateInstance( CLSID_DVBCLocator, 0, CLSCTX_INPROC,
377 IID_IDVBCLocator, (void**)&l.p_dvbc_locator );
380 msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
381 "Cannot create the DVB-C Locator: hr=0x%8lx", hr );
386 if( l_frequency > 0 )
387 hr = l.p_dvbc_locator->put_CarrierFrequency( l_frequency );
388 if( SUCCEEDED( hr ) && l_symbolrate > 0 )
389 hr = l.p_dvbc_locator->put_SymbolRate( l_symbolrate );
390 if( SUCCEEDED( hr ) && i_qam_mod != BDA_MOD_NOT_SET )
391 hr = l.p_dvbc_locator->put_Modulation( i_qam_mod );
394 msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
395 "Cannot set tuning parameters on Locator: hr=0x%8lx", hr );
399 hr = p_tune_request->put_Locator( l.p_dvbc_locator );
402 msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
403 "Cannot put the locator: hr=0x%8lx", hr );
407 /* Build and Run the Graph. If a Tuner device is in use the graph will
408 * fail to run. Repeated calls to build will check successive tuner
415 msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
416 "Cannot Build the Graph: hr=0x%8lx", hr );
426 /*****************************************************************************
427 * Submit a DVB-S Tune Request
428 ******************************************************************************/
429 int BDAGraph::SubmitDVBSTuneRequest()
436 IDVBTuneRequest* p_dvbs_tune_request;
437 IDVBSLocator* p_dvbs_locator;
438 IDVBSTuningSpace* p_dvbs_tuning_space;
439 localComPtr(): p_dvbs_tune_request(NULL), p_dvbs_locator(NULL),
440 p_dvbs_tuning_space(NULL) {};
443 if( p_dvbs_tuning_space )
444 p_dvbs_tuning_space->Release();
445 if( p_dvbs_tune_request )
446 p_dvbs_tune_request->Release();
448 p_dvbs_locator->Release();
451 long l_frequency, l_symbolrate, l_azimuth, l_elevation, l_longitude;
452 long l_lnb_lof1, l_lnb_lof2, l_lnb_slof;
453 char* psz_polarisation;
454 Polarisation i_polar;
457 l_frequency = l_symbolrate = l_azimuth = l_elevation = l_longitude = -1;
458 l_lnb_lof1 = l_lnb_lof2 = l_lnb_slof = -1;
459 l_frequency = var_GetInteger( p_access, "dvb-frequency" );
460 l_symbolrate = var_GetInteger( p_access, "dvb-srate" );
461 l_azimuth = var_GetInteger( p_access, "dvb-azimuth" );
462 l_elevation = var_GetInteger( p_access, "dvb-elevation" );
463 l_longitude = var_GetInteger( p_access, "dvb-longitude" );
464 l_lnb_lof1 = var_GetInteger( p_access, "dvb-lnb-lof1" );
465 l_lnb_lof2 = var_GetInteger( p_access, "dvb-lnb-lof2" );
466 l_lnb_slof = var_GetInteger( p_access, "dvb-lnb-slof" );
467 psz_polarisation = var_GetString( p_access, "dvb-polarisation" );
469 b_west = ( l_longitude < 0 ) ? TRUE : FALSE;
471 i_polar = BDA_POLARISATION_NOT_SET;
472 if( *psz_polarisation == 'H' || *psz_polarisation == 'h' )
473 i_polar = BDA_POLARISATION_LINEAR_H;
474 if( *psz_polarisation == 'V' || *psz_polarisation == 'v' )
475 i_polar = BDA_POLARISATION_LINEAR_V;
476 if( *psz_polarisation == 'L' || *psz_polarisation == 'l' )
477 i_polar = BDA_POLARISATION_CIRCULAR_L;
478 if( *psz_polarisation == 'R' || *psz_polarisation == 'r' )
479 i_polar = BDA_POLARISATION_CIRCULAR_R;
481 guid_network_type = CLSID_DVBSNetworkProvider;
482 hr = CreateTuneRequest();
485 msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
486 "Cannot create Tune Request: hr=0x%8lx", hr );
490 hr = p_tune_request->QueryInterface( IID_IDVBTuneRequest,
491 (void**)&l.p_dvbs_tune_request );
494 msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
495 "Cannot QI for IDVBTuneRequest: hr=0x%8lx", hr );
498 l.p_dvbs_tune_request->put_ONID( -1 );
499 l.p_dvbs_tune_request->put_SID( -1 );
500 l.p_dvbs_tune_request->put_TSID( -1 );
502 hr = ::CoCreateInstance( CLSID_DVBSLocator, 0, CLSCTX_INPROC,
503 IID_IDVBSLocator, (void**)&l.p_dvbs_locator );
506 msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
507 "Cannot create the DVBS Locator: hr=0x%8lx", hr );
511 hr = p_tuning_space->QueryInterface( IID_IDVBSTuningSpace,
512 (void**)&l.p_dvbs_tuning_space );
515 msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
516 "Cannot QI for IDVBSTuningSpace: hr=0x%8lx", hr );
521 if( l_frequency > 0 )
522 hr = l.p_dvbs_locator->put_CarrierFrequency( l_frequency );
523 if( SUCCEEDED( hr ) && l_symbolrate > 0 )
524 hr = l.p_dvbs_locator->put_SymbolRate( l_symbolrate );
525 if( SUCCEEDED( hr ) && l_azimuth > 0 )
526 hr = l.p_dvbs_locator->put_Azimuth( l_azimuth );
527 if( SUCCEEDED( hr ) && l_elevation > 0 )
528 hr = l.p_dvbs_locator->put_Elevation( l_elevation );
529 if( SUCCEEDED( hr ) )
530 hr = l.p_dvbs_locator->put_OrbitalPosition( labs( l_longitude ) );
531 if( SUCCEEDED( hr ) )
532 hr = l.p_dvbs_locator->put_WestPosition( b_west );
533 if( SUCCEEDED( hr ) && i_polar != BDA_POLARISATION_NOT_SET )
534 hr = l.p_dvbs_locator->put_SignalPolarisation( i_polar );
535 if( SUCCEEDED( hr ) && l_lnb_lof1 > 0 )
536 hr = l.p_dvbs_tuning_space->put_LowOscillator( l_lnb_lof1 );
537 if( SUCCEEDED( hr ) && l_lnb_lof2 > 0 )
538 hr = l.p_dvbs_tuning_space->put_HighOscillator( l_lnb_lof2 );
539 if( SUCCEEDED( hr ) && l_lnb_slof > 0 )
540 hr = l.p_dvbs_tuning_space->put_HighOscillator( l_lnb_slof );
543 msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
544 "Cannot set tuning parameters on Locator: hr=0x%8lx", hr );
548 hr = p_tune_request->put_Locator( l.p_dvbs_locator );
551 msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
552 "Cannot put the locator: hr=0x%8lx", hr );
556 /* Build and Run the Graph. If a Tuner device is in use the graph will
557 * fail to run. Repeated calls to build will check successive tuner
564 msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
565 "Cannot Build the Graph: hr=0x%8lx", hr );
575 /*****************************************************************************
576 * Load the Tuning Space from System Tuning Spaces according to the
577 * Network Type requested
578 ******************************************************************************/
579 HRESULT BDAGraph::CreateTuneRequest()
582 GUID guid_this_network_type;
586 ITuningSpaceContainer* p_tuning_space_container;
587 IEnumTuningSpaces* p_tuning_space_enum;
588 ITuningSpace* p_this_tuning_space;
589 localComPtr(): p_tuning_space_container(NULL),
590 p_tuning_space_enum(NULL), p_this_tuning_space(NULL) {};
593 if( p_tuning_space_container )
594 p_tuning_space_container->Release();
595 if( p_tuning_space_enum )
596 p_tuning_space_enum->Release();
597 if( p_this_tuning_space )
598 p_this_tuning_space->Release();
602 /* A Tuning Space may already have been set up. If it is for the same
603 * network type then all is well. Otherwise, reset the Tuning Space and get
607 hr = p_tuning_space->get__NetworkType( &guid_this_network_type );
608 if( FAILED( hr ) ) guid_this_network_type = GUID_NULL;
609 if( guid_this_network_type == guid_network_type )
615 p_tuning_space->Release();
616 p_tuning_space = NULL;
620 /* Force use of the first available Tuner Device during Build */
623 /* Get the SystemTuningSpaces container to enumerate through all the
624 * defined tuning spaces */
625 hr = ::CoCreateInstance( CLSID_SystemTuningSpaces, 0, CLSCTX_INPROC,
626 IID_ITuningSpaceContainer, (void**)&l.p_tuning_space_container );
629 msg_Warn( p_access, "CreateTuneRequest: "\
630 "Cannot CoCreate SystemTuningSpaces: hr=0x%8lx", hr );
634 hr = l.p_tuning_space_container->get_EnumTuningSpaces(
635 &l.p_tuning_space_enum );
638 msg_Warn( p_access, "CreateTuneRequest: "\
639 "Cannot create SystemTuningSpaces Enumerator: hr=0x%8lx", hr );
643 while( l.p_tuning_space_enum->Next( 1, &l.p_this_tuning_space, NULL ) ==
646 hr = l.p_this_tuning_space->get__NetworkType( &guid_this_network_type );
648 /* GUID_NULL means a non-BDA network was found e.g analog
649 * Ignore failures and non-BDA networks and keep looking */
650 if( FAILED( hr ) ) guid_this_network_type == GUID_NULL;
652 if( guid_this_network_type == guid_network_type )
654 hr = l.p_this_tuning_space->QueryInterface( IID_ITuningSpace,
655 (void**)&p_tuning_space );
658 msg_Warn( p_access, "CreateTuneRequest: "\
659 "Cannot QI Tuning Space: hr=0x%8lx", hr );
662 hr = p_tuning_space->CreateTuneRequest( &p_tune_request );
665 msg_Warn( p_access, "CreateTuneRequest: "\
666 "Cannot Create Tune Request: hr=0x%8lx", hr );
675 msg_Warn( p_access, "CreateTuneRequest: "\
676 "Cannot find a suitable System Tuning Space: hr=0x%8lx", hr );
682 /******************************************************************************
684 * Step 4: Build the Filter Graph
685 * Build sets up devices, adds and connects filters
686 ******************************************************************************/
687 HRESULT BDAGraph::Build()
690 long l_capture_used, l_tif_used;
691 AM_MEDIA_TYPE grabber_media_type;
693 /* If we have already have a filter graph, rebuild it*/
696 hr = ::CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC,
697 IID_IGraphBuilder, (void**)&p_filter_graph );
700 msg_Warn( p_access, "Build: "\
701 "Cannot CoCreate IFilterGraph: hr=0x%8lx", hr );
705 /* First filter in the graph is the Network Provider and
706 * its Scanning Tuner which takes the Tune Request*/
707 hr = ::CoCreateInstance( guid_network_type, NULL, CLSCTX_INPROC_SERVER,
708 IID_IBaseFilter, (void**)&p_network_provider);
711 msg_Warn( p_access, "Build: "\
712 "Cannot CoCreate Network Provider: hr=0x%8lx", hr );
715 hr = p_filter_graph->AddFilter( p_network_provider, L"Network Provider" );
718 msg_Warn( p_access, "Build: "\
719 "Cannot load network provider: hr=0x%8lx", hr );
723 hr = p_network_provider->QueryInterface( IID_IScanningTuner,
724 (void**)&p_scanning_tuner );
727 msg_Warn( p_access, "Build: "\
728 "Cannot QI Network Provider for Scanning Tuner: hr=0x%8lx", hr );
732 hr = p_scanning_tuner->Validate( p_tune_request );
735 msg_Warn( p_access, "Build: "\
736 "Tune Request is invalid: hr=0x%8lx", hr );
739 hr = p_scanning_tuner->put_TuneRequest( p_tune_request );
742 msg_Warn( p_access, "Build: "\
743 "Cannot submit the tune request: hr=0x%8lx", hr );
747 /* Add the Network Tuner to the Network Provider. On subsequent calls,
748 * l_tuner_used will cause a different tuner to be selected */
749 hr = FindFilter( KSCATEGORY_BDA_NETWORK_TUNER, &l_tuner_used,
750 p_network_provider, &p_tuner_device );
753 msg_Warn( p_access, "Build: "\
754 "Cannot load tuner device and connect network provider: "\
759 /* Always look for all capture devices to match the Network Tuner */
761 hr = FindFilter( KSCATEGORY_BDA_RECEIVER_COMPONENT, &l_capture_used,
762 p_tuner_device, &p_capture_device );
765 /* Some BDA drivers do not provide a Capture Device Filter so force
766 * the Sample Grabber to connect directly to the Tuner Device */
767 p_capture_device = p_tuner_device;
768 p_tuner_device = NULL;
769 msg_Warn( p_access, "Build: "\
770 "Cannot find Capture device. Connecting to tuner: hr=0x%8lx", hr );
773 /* Insert the Sample Grabber to tap into the Transport Stream. */
774 hr = ::CoCreateInstance( CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
775 IID_IBaseFilter, (void**)&p_sample_grabber );
778 msg_Warn( p_access, "Build: "\
779 "Cannot load Sample Grabber Filter: hr=0x%8lx", hr );
782 hr = p_filter_graph->AddFilter( p_sample_grabber, L"Sample Grabber" );
785 msg_Warn( p_access, "Build: "\
786 "Cannot add Sample Grabber Filter to graph: hr=0x%8lx", hr );
790 hr = p_sample_grabber->QueryInterface( IID_ISampleGrabber,
791 (void**)&p_grabber );
794 msg_Warn( p_access, "Build: "\
795 "Cannot QI Sample Grabber Filter: hr=0x%8lx", hr );
799 ZeroMemory( &grabber_media_type, sizeof( AM_MEDIA_TYPE ) );
800 grabber_media_type.majortype == MEDIATYPE_Stream;
801 grabber_media_type.subtype == MEDIASUBTYPE_MPEG2_TRANSPORT;
802 hr = p_grabber->SetMediaType( &grabber_media_type );
805 msg_Warn( p_access, "Build: "\
806 "Cannot set media type on grabber filter: hr=0x%8lx", hr );
809 hr = Connect( p_capture_device, p_sample_grabber );
812 msg_Warn( p_access, "Build: "\
813 "Cannot connect Sample Grabber to Capture device: hr=0x%8lx", hr );
817 /* We need the MPEG2 Demultiplexer even though we are going to use the VLC
818 * TS demuxer. The TIF filter connects to the MPEG2 demux and works with
819 * the Network Provider filter to set up the stream */
820 hr = ::CoCreateInstance( CLSID_MPEG2Demultiplexer, NULL,
821 CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)&p_mpeg_demux );
824 msg_Warn( p_access, "Build: "\
825 "Cannot CoCreateInstance MPEG2 Demultiplexer: hr=0x%8lx", hr );
828 hr = p_filter_graph->AddFilter( p_mpeg_demux, L"Demux" );
831 msg_Warn( p_access, "Build: "\
832 "Cannot add demux filter to graph: hr=0x%8lx", hr );
836 hr = Connect( p_sample_grabber, p_mpeg_demux );
839 msg_Warn( p_access, "Build: "\
840 "Cannot connect demux to grabber: hr=0x%8lx", hr );
844 /* Always look for the Transform Information Filter from the start
845 * of the collection*/
847 hr = FindFilter( KSCATEGORY_BDA_TRANSPORT_INFORMATION, &l_tif_used,
848 p_mpeg_demux, &p_transport_info );
851 msg_Warn( p_access, "Build: "\
852 "Cannot load TIF onto demux: hr=0x%8lx", hr );
856 /* Configure the Sample Grabber to buffer the samples continuously */
857 hr = p_grabber->SetBufferSamples( TRUE );
860 msg_Warn( p_access, "Build: "\
861 "Cannot set Sample Grabber to buffering: hr=0x%8lx", hr );
864 hr = p_grabber->SetOneShot( FALSE );
867 msg_Warn( p_access, "Build: "\
868 "Cannot set Sample Grabber to multi shot: hr=0x%8lx", hr );
871 hr = p_grabber->SetCallback( this, 0 );
874 msg_Warn( p_access, "Build: "\
875 "Cannot set SampleGrabber Callback: hr=0x%8lx", hr );
882 d_graph_register = 0;
885 /* The Media Control is used to Run and Stop the Graph */
886 hr = p_filter_graph->QueryInterface( IID_IMediaControl,
887 (void**)&p_media_control );
890 msg_Warn( p_access, "Build: "\
891 "Cannot QI Media Control: hr=0x%8lx", hr );
897 /******************************************************************************
899 * Looks up all filters in a category and connects to the upstream filter until
900 * a successful match is found. The index of the connected filter is returned.
901 * On subsequent calls, this can be used to start from that point to find
903 * This is used when the graph does not run because a tuner device is in use so
904 * another one needs to be slected.
905 ******************************************************************************/
906 HRESULT BDAGraph::FindFilter( REFCLSID clsid, long* i_moniker_used,
907 IBaseFilter* p_upstream, IBaseFilter** p_p_downstream )
910 int i_moniker_index = -1;
915 IEnumMoniker* p_moniker_enum;
916 IBaseFilter* p_filter;
917 IPropertyBag* p_property_bag;
921 p_moniker_enum(NULL),
924 { ::VariantInit(&var_bstr); };
928 p_moniker->Release();
930 p_moniker_enum->Release();
934 p_property_bag->Release();
935 ::VariantClear(&var_bstr);
939 if( !p_system_dev_enum )
941 hr = ::CoCreateInstance( CLSID_SystemDeviceEnum, 0, CLSCTX_INPROC,
942 IID_ICreateDevEnum, (void**)&p_system_dev_enum );
945 msg_Warn( p_access, "FindFilter: "\
946 "Cannot CoCreate SystemDeviceEnum: hr=0x%8lx", hr );
951 hr = p_system_dev_enum->CreateClassEnumerator( clsid,
952 &l.p_moniker_enum, 0 );
955 msg_Warn( p_access, "FindFilter: "\
956 "Cannot CreateClassEnumerator: hr=0x%8lx", hr );
959 while( l.p_moniker_enum->Next( 1, &l.p_moniker, 0 ) == S_OK )
963 /* Skip over devices already found on previous calls */
964 if( i_moniker_index <= *i_moniker_used ) continue;
965 *i_moniker_used = i_moniker_index;
967 hr = l.p_moniker->BindToObject( NULL, NULL, IID_IBaseFilter,
968 (void**)&l.p_filter );
972 l.p_moniker->Release();
975 l.p_filter->Release();
980 hr = l.p_moniker->BindToStorage( NULL, NULL, IID_IPropertyBag,
981 (void**)&l.p_property_bag );
984 msg_Warn( p_access, "FindFilter: "\
985 "Cannot Bind to Property Bag: hr=0x%8lx", hr );
989 hr = l.p_property_bag->Read( L"FriendlyName", &l.var_bstr, NULL );
992 msg_Warn( p_access, "FindFilter: "\
993 "Cannot read filter friendly name: hr=0x%8lx", hr );
997 hr = p_filter_graph->AddFilter( l.p_filter, l.var_bstr.bstrVal );
1000 msg_Warn( p_access, "FindFilter: "\
1001 "Cannot add filter: hr=0x%8lx", hr );
1005 hr = Connect( p_upstream, l.p_filter );
1006 if( SUCCEEDED( hr ) )
1008 msg_Dbg( p_access, "FindFilter: Connected %S", l.var_bstr.bstrVal );
1009 l.p_filter->QueryInterface( IID_IBaseFilter,
1010 (void**)p_p_downstream );
1013 /* Not the filter we want so unload and try the next one */
1014 hr = p_filter_graph->RemoveFilter( l.p_filter );
1017 msg_Warn( p_access, "FindFilter: "\
1018 "Failed unloading Filter: hr=0x%8lx", hr );
1023 l.p_moniker->Release();
1026 l.p_filter->Release();
1031 msg_Warn( p_access, "FindFilter: No filter connected: hr=0x%8lx", hr );
1035 /*****************************************************************************
1036 * Connect is called from Build to enumerate and connect pins
1037 *****************************************************************************/
1038 HRESULT BDAGraph::Connect( IBaseFilter* p_upstream, IBaseFilter* p_downstream )
1040 HRESULT hr = E_FAIL;
1044 IPin* p_pin_upstream;
1045 IPin* p_pin_downstream;
1046 IEnumPins* p_pin_upstream_enum;
1047 IEnumPins* p_pin_downstream_enum;
1049 localComPtr(): p_pin_upstream(NULL), p_pin_downstream(NULL),
1050 p_pin_upstream_enum(NULL), p_pin_downstream_enum(NULL),
1051 p_pin_temp(NULL) { };
1054 if( p_pin_upstream )
1055 p_pin_upstream->Release();
1056 if( p_pin_downstream )
1057 p_pin_downstream->Release();
1058 if( p_pin_upstream_enum )
1059 p_pin_upstream_enum->Release();
1060 if( p_pin_downstream_enum )
1061 p_pin_downstream_enum->Release();
1063 p_pin_temp->Release();
1067 PIN_INFO pin_info_upstream;
1068 PIN_INFO pin_info_downstream;
1070 hr = p_upstream->EnumPins( &l.p_pin_upstream_enum );
1073 msg_Warn( p_access, "Connect: "\
1074 "Cannot get upstream filter enumerator: hr=0x%8lx", hr );
1077 while( l.p_pin_upstream_enum->Next( 1, &l.p_pin_upstream, 0 ) == S_OK )
1079 hr = l.p_pin_upstream->QueryPinInfo( &pin_info_upstream );
1082 msg_Warn( p_access, "Connect: "\
1083 "Cannot get upstream filter pin information: hr=0x%8lx", hr );
1086 hr = l.p_pin_upstream->ConnectedTo( &l.p_pin_downstream );
1088 l.p_pin_downstream->Release();
1089 if(FAILED( hr ) && hr != VFW_E_NOT_CONNECTED )
1091 msg_Warn( p_access, "Connect: "\
1092 "Cannot check upstream filter connection: hr=0x%8lx", hr );
1095 if(( pin_info_upstream.dir == PINDIR_OUTPUT ) &&
1096 ( hr == VFW_E_NOT_CONNECTED ) )
1098 /* The upstream pin is not yet connected so check each pin on the
1099 * downstream filter */
1100 hr = p_downstream->EnumPins( &l.p_pin_downstream_enum );
1103 msg_Warn( p_access, "Connect: Cannot get "\
1104 "downstream filter enumerator: hr=0x%8lx", hr );
1107 while( l.p_pin_downstream_enum->Next( 1, &l.p_pin_downstream, 0 )
1110 hr = l.p_pin_downstream->QueryPinInfo( &pin_info_downstream );
1113 msg_Warn( p_access, "Connect: Cannot get "\
1114 "downstream filter pin information: hr=0x%8lx", hr );
1118 hr = l.p_pin_downstream->ConnectedTo( &l.p_pin_temp );
1119 if( hr == S_OK ) l.p_pin_temp->Release();
1120 if( hr != VFW_E_NOT_CONNECTED )
1124 msg_Warn( p_access, "Connect: Cannot check "\
1125 "downstream filter connection: hr=0x%8lx", hr );
1129 if(( pin_info_downstream.dir == PINDIR_INPUT ) &&
1130 ( hr == VFW_E_NOT_CONNECTED ) )
1132 hr = p_filter_graph->ConnectDirect( l.p_pin_upstream,
1133 l.p_pin_downstream, NULL );
1134 if( SUCCEEDED( hr ) )
1136 pin_info_downstream.pFilter->Release();
1137 pin_info_upstream.pFilter->Release();
1141 /* If we fall out here it means this downstream pin was not
1142 * suitable so try the next downstream pin */
1143 l.p_pin_downstream = NULL;
1144 pin_info_downstream.pFilter->Release();
1148 /* If we fall out here it means we did not find any suitable downstream
1149 * pin so try the next upstream pin */
1150 l.p_pin_upstream = NULL;
1151 pin_info_upstream.pFilter->Release();
1154 /* If we fall out here it means we did not find any pair of suitable pins */
1158 /*****************************************************************************
1159 * Start uses MediaControl to start the graph
1160 *****************************************************************************/
1161 HRESULT BDAGraph::Start()
1164 OAFilterState i_state; /* State_Stopped, State_Paused, State_Running */
1166 if( !p_media_control )
1168 msg_Dbg( p_access, "Start: Media Control has not been created" );
1171 hr = p_media_control->Run();
1175 /* Query the state of the graph - timeout after 100 milliseconds */
1176 while( hr = p_media_control->GetState( 100, &i_state ) != S_OK )
1181 "Start: Cannot get Graph state: hr=0x%8lx", hr );
1185 if( i_state == State_Running )
1188 /* The Graph is not running so stop it and return an error */
1189 hr = p_media_control->Stop();
1193 "Start: Cannot stop Graph after Run failed: hr=0x%8lx", hr );
1199 /*****************************************************************************
1200 * Read the stream of data - query the buffer size required
1201 *****************************************************************************/
1202 long BDAGraph::GetBufferSize()
1204 long l_buffer_size = 0;
1209 for( int i_timer = 0; queue_sample.empty() && i_timer < 200; i_timer++ )
1212 l_queue_size = queue_sample.size();
1213 if( l_queue_size <= 0 )
1215 msg_Warn( p_access, "BDA GetBufferSize: Timed Out waiting for sample" );
1219 /* Establish the length of the queue as it grows quickly. If the queue
1220 * size is checked dynamically there is a risk of not exiting the loop */
1221 for( long l_queue_count=0; l_queue_count < l_queue_size; l_queue_count++ )
1223 l_buffer_size += queue_sample.front()->GetActualDataLength();
1224 queue_buffer.push( queue_sample.front() );
1227 return l_buffer_size;
1230 /*****************************************************************************
1231 * Read the stream of data - Retrieve from the buffer queue
1232 ******************************************************************************/
1233 long BDAGraph::ReadBuffer( long* pl_buffer_len, BYTE* p_buffer )
1240 while( !queue_buffer.empty() )
1242 queue_buffer.front()->GetPointer( &p_buff_temp );
1243 hr = queue_buffer.front()->IsDiscontinuity();
1246 "BDA ReadBuffer: Sample Discontinuity. 0x%8lx", hr );
1247 memcpy( p_buffer + *pl_buffer_len, p_buff_temp,
1248 queue_buffer.front()->GetActualDataLength() );
1249 *pl_buffer_len += queue_buffer.front()->GetActualDataLength();
1250 queue_buffer.front()->Release();
1254 return *pl_buffer_len;
1257 /******************************************************************************
1258 * SampleCB - Callback when the Sample Grabber has a sample
1259 ******************************************************************************/
1260 STDMETHODIMP BDAGraph::SampleCB( double d_time, IMediaSample *p_sample )
1265 queue_sample.push( p_sample );
1269 msg_Warn( p_access, "BDA SampleCB: Not ready - dropped sample" );
1274 STDMETHODIMP BDAGraph::BufferCB( double d_time, BYTE* p_buffer,
1280 /******************************************************************************
1281 * removes each filter from the graph
1282 ******************************************************************************/
1283 HRESULT BDAGraph::Destroy()
1287 if( p_media_control )
1288 hr = p_media_control->Stop();
1290 if( p_transport_info )
1292 p_filter_graph->RemoveFilter( p_transport_info );
1293 p_transport_info->Release();
1294 p_transport_info = NULL;
1298 p_filter_graph->RemoveFilter( p_mpeg_demux );
1299 p_mpeg_demux->Release();
1300 p_mpeg_demux = NULL;
1302 if( p_sample_grabber )
1304 p_filter_graph->RemoveFilter( p_sample_grabber );
1305 p_sample_grabber->Release();
1306 p_sample_grabber = NULL;
1308 if( p_capture_device )
1310 p_filter_graph->RemoveFilter( p_capture_device );
1311 p_capture_device->Release();
1312 p_capture_device = NULL;
1314 if( p_tuner_device )
1316 p_filter_graph->RemoveFilter( p_tuner_device );
1317 p_tuner_device->Release();
1318 p_tuner_device = NULL;
1320 if( p_network_provider )
1322 p_filter_graph->RemoveFilter( p_network_provider );
1323 p_network_provider->Release();
1324 p_network_provider = NULL;
1327 if( p_scanning_tuner )
1329 p_scanning_tuner->Release();
1330 p_scanning_tuner = NULL;
1332 if( p_media_control )
1334 p_media_control->Release();
1335 p_media_control = NULL;
1337 if( p_scanning_tuner )
1339 p_filter_graph->Release();
1340 p_filter_graph = NULL;
1343 if( d_graph_register )
1351 /*****************************************************************************
1352 * Add/Remove a DirectShow filter graph to/from the Running Object Table.
1353 * Allows GraphEdit to "spy" on a remote filter graph.
1354 ******************************************************************************/
1355 HRESULT BDAGraph::Register()
1360 IMoniker* p_moniker;
1361 IRunningObjectTable* p_ro_table;
1362 localComPtr(): p_moniker(NULL), p_ro_table(NULL) {};
1366 p_moniker->Release();
1368 p_ro_table->Release();
1371 WCHAR psz_w_graph_name[128];
1374 hr = ::GetRunningObjectTable( 0, &l.p_ro_table );
1377 msg_Warn( p_access, "Register: Cannot get ROT: hr=0x%8lx", hr );
1381 wsprintfW( psz_w_graph_name, L"VLC BDA Graph %08x Pid %08x",
1382 (DWORD_PTR) p_filter_graph, ::GetCurrentProcessId() );
1383 hr = CreateItemMoniker( L"!", psz_w_graph_name, &l.p_moniker );
1386 msg_Warn( p_access, "Register: Cannot Create Moniker: hr=0x%8lx", hr );
1389 hr = l.p_ro_table->Register( ROTFLAGS_REGISTRATIONKEEPSALIVE,
1390 p_filter_graph, l.p_moniker, &d_graph_register );
1393 msg_Warn( p_access, "Register: Cannot Register Graph: hr=0x%8lx", hr );
1396 msg_Dbg( p_access, "Register: registered Graph: %S", psz_w_graph_name );
1400 void BDAGraph::Deregister()
1403 IRunningObjectTable* p_ro_table;
1404 hr = ::GetRunningObjectTable( 0, &p_ro_table );
1405 if( SUCCEEDED( hr ) )
1406 p_ro_table->Revoke( d_graph_register );
1407 d_graph_register = 0;
1408 p_ro_table->Release();