94b12d07632357a235d0dcb180dd523631270b2c
[vlc.git] / modules / access / bda / bdagraph.cpp
1 /*****************************************************************************
2  * bdagraph.cpp : DirectShow BDA graph for vlc
3  *****************************************************************************
4  * Copyright( C ) 2007 the VideoLAN team
5  *
6  * Author: Ken Self <kens@campoz.fslife.co.uk>
7  *
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.
12  *
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.
17  *
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  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26 #include "bdagraph.h"
27
28 /****************************************************************************
29  * Interfaces for calls from C
30  ****************************************************************************/
31 extern "C" {
32
33     void dvb_newBDAGraph( access_t* p_access )
34     {
35         p_access->p_sys->p_bda_module = new BDAGraph( p_access );
36     };
37
38     void dvb_deleteBDAGraph( access_t* p_access )
39     {
40         if( p_access->p_sys->p_bda_module )
41             delete p_access->p_sys->p_bda_module;
42     };
43
44     int dvb_SubmitATSCTuneRequest( access_t* p_access )
45     {
46         if( p_access->p_sys->p_bda_module )
47             return p_access->p_sys->p_bda_module->SubmitATSCTuneRequest();
48         return VLC_EGENERIC;
49     };
50
51     int dvb_SubmitDVBTTuneRequest( access_t* p_access )
52     {
53         if( p_access->p_sys->p_bda_module )
54             return p_access->p_sys->p_bda_module->SubmitDVBTTuneRequest();
55         return VLC_EGENERIC;
56     };
57
58     int dvb_SubmitDVBCTuneRequest( access_t* p_access )
59     {
60         if( p_access->p_sys->p_bda_module )
61             return p_access->p_sys->p_bda_module->SubmitDVBCTuneRequest();
62         return VLC_EGENERIC;
63     };    
64
65     int dvb_SubmitDVBSTuneRequest( access_t* p_access )
66     {
67         if( p_access->p_sys->p_bda_module )
68             return p_access->p_sys->p_bda_module->SubmitDVBSTuneRequest();
69         return VLC_EGENERIC;
70     };
71
72     long dvb_GetBufferSize( access_t* p_access )
73     {
74         if( p_access->p_sys->p_bda_module )
75             return p_access->p_sys->p_bda_module->GetBufferSize();
76         return -1;
77     };
78
79     long dvb_ReadBuffer( access_t* p_access, long* l_buffer_len, BYTE* p_buff )
80     {
81         if( p_access->p_sys->p_bda_module )
82             return p_access->p_sys->p_bda_module->ReadBuffer( l_buffer_len, 
83                 p_buff );
84         return -1;
85     };
86 };
87
88 /*****************************************************************************
89 * Constructor
90 *****************************************************************************/
91 BDAGraph::BDAGraph( access_t* p_this ):
92     p_access( p_this ),
93     guid_network_type(GUID_NULL),
94     l_tuner_used(-1),
95     d_graph_register( 0 )
96 {
97     b_ready = FALSE;
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;
106     p_grabber = NULL;
107
108     /* Initialize COM - MS says to use CoInitializeEx in preference to
109      * CoInitialize */
110     CoInitializeEx( 0, COINIT_APARTMENTTHREADED );
111 }
112
113 /*****************************************************************************
114 * Destructor
115 *****************************************************************************/
116 BDAGraph::~BDAGraph()
117 {
118     Destroy();
119     CoUninitialize();
120 }
121
122 /*****************************************************************************
123 * Submit an ATSC Tune Request
124 *****************************************************************************/
125 int BDAGraph::SubmitATSCTuneRequest()
126 {
127     HRESULT hr = S_OK;
128     class localComPtr
129     {
130         public:
131         IATSCChannelTuneRequest* p_atsc_tune_request;
132         IATSCLocator* p_atsc_locator;
133         localComPtr(): p_atsc_tune_request(NULL), p_atsc_locator(NULL) {};
134         ~localComPtr()
135         {
136             if( p_atsc_tune_request )
137                 p_atsc_tune_request->Release();
138             if( p_atsc_locator )
139                 p_atsc_locator->Release();
140         }
141     } l;
142     long l_major_channel, l_minor_channel, l_physical_channel;
143
144     l_major_channel = l_minor_channel = l_physical_channel = -1;
145 /*
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" );
149 */
150
151     guid_network_type = CLSID_ATSCNetworkProvider;
152     hr = CreateTuneRequest();
153     if( FAILED( hr ) )
154     {
155         msg_Warn( p_access, "SubmitATSCTuneRequest: "\
156             "Cannot create Tuning Space: hr=0x%8lx", hr );
157         return VLC_EGENERIC;
158     }
159
160     hr = p_tune_request->QueryInterface( IID_IATSCChannelTuneRequest,
161         (void**)&l.p_atsc_tune_request );
162     if( FAILED( hr ) )
163     {
164         msg_Warn( p_access, "SubmitATSCTuneRequest: "\
165             "Cannot QI for IATSCChannelTuneRequest: hr=0x%8lx", hr );
166         return VLC_EGENERIC;
167     }
168     hr = ::CoCreateInstance( CLSID_ATSCLocator, 0, CLSCTX_INPROC,
169                              IID_IATSCLocator, (void**)&l.p_atsc_locator );
170     if( FAILED( hr ) )
171     {
172         msg_Warn( p_access, "SubmitATSCTuneRequest: "\
173             "Cannot create the ATSC locator: hr=0x%8lx", hr );
174         return VLC_EGENERIC;
175     }
176
177     hr = S_OK;
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 );
184     if( FAILED( hr ) )
185     {
186         msg_Warn( p_access, "SubmitATSCTuneRequest: "\
187             "Cannot set tuning parameters: hr=0x%8lx", hr );
188         return VLC_EGENERIC;
189     }
190
191     hr = p_tune_request->put_Locator( l.p_atsc_locator );
192     if( FAILED( hr ) )
193     {
194         msg_Warn( p_access, "SubmitATSCTuneRequest: "\
195             "Cannot put the locator: hr=0x%8lx", hr );
196         return VLC_EGENERIC;
197     }
198
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
201      * devices */
202     do
203     {
204         hr = Build();
205         if( FAILED( hr ) )
206         {
207             msg_Warn( p_access, "SubmitATSCTuneRequest: "\
208                 "Cannot Build the Graph: hr=0x%8lx", hr );
209             return VLC_EGENERIC;
210         }
211         hr = Start();
212     }
213     while( hr != S_OK );
214
215     return VLC_SUCCESS;
216 }
217
218 /*****************************************************************************
219 * Submit a DVB-T Tune Request
220 ******************************************************************************/
221 int BDAGraph::SubmitDVBTTuneRequest()
222 {
223     HRESULT hr = S_OK;
224     class localComPtr
225     {
226         public:
227         IDVBTuneRequest* p_dvbt_tune_request;
228         IDVBTLocator* p_dvbt_locator;
229         localComPtr(): p_dvbt_tune_request(NULL), p_dvbt_locator(NULL) {};
230         ~localComPtr()
231         {
232             if( p_dvbt_tune_request )
233                 p_dvbt_tune_request->Release();
234             if( p_dvbt_locator )
235                 p_dvbt_locator->Release();
236         }
237     } l;
238     long l_frequency, l_bandwidth;
239
240     l_frequency = l_bandwidth = -1;
241     l_frequency = var_GetInteger( p_access, "dvb-frequency" );
242     l_bandwidth = var_GetInteger( p_access, "dvb-bandwidth" );
243
244     guid_network_type = CLSID_DVBTNetworkProvider;
245     hr = CreateTuneRequest();
246     if( FAILED( hr ) )
247     {
248         msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
249             "Cannot create Tune Request: hr=0x%8lx", hr );
250         return VLC_EGENERIC;
251     }
252
253     hr = p_tune_request->QueryInterface( IID_IDVBTuneRequest,
254         (void**)&l.p_dvbt_tune_request );
255     if( FAILED( hr ) )
256     {
257         msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
258             "Cannot QI for IDVBTuneRequest: hr=0x%8lx", hr );
259         return VLC_EGENERIC;
260     }
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 );
264
265     hr = ::CoCreateInstance( CLSID_DVBTLocator, 0, CLSCTX_INPROC,
266         IID_IDVBTLocator, (void**)&l.p_dvbt_locator );
267     if( FAILED( hr ) )
268     {
269         msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
270             "Cannot create the DVBT Locator: hr=0x%8lx", hr );
271         return VLC_EGENERIC;
272     }
273
274     hr = S_OK;
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 );
279     if( FAILED( hr ) )
280     {
281         msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
282             "Cannot set tuning parameters on Locator: hr=0x%8lx", hr );
283         return VLC_EGENERIC;
284     }
285
286     hr = p_tune_request->put_Locator( l.p_dvbt_locator );
287     if( FAILED( hr ) )
288     {
289         msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
290             "Cannot put the locator: hr=0x%8lx", hr );
291         return VLC_EGENERIC;
292     }
293
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
296      * devices */
297     do
298     {
299         hr = Build();
300         if( FAILED( hr ) )
301         {
302             msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
303                 "Cannot Build the Graph: hr=0x%8lx", hr );
304             return VLC_EGENERIC;
305         }
306         hr = Start();
307     }
308     while( hr != S_OK );
309
310     return VLC_SUCCESS;
311 }
312
313 /*****************************************************************************
314 * Submit a DVB-C Tune Request
315 ******************************************************************************/
316 int BDAGraph::SubmitDVBCTuneRequest()
317 {
318     HRESULT hr = S_OK;
319
320     class localComPtr
321     {
322         public:
323         IDVBTuneRequest* p_dvbc_tune_request;
324         IDVBCLocator* p_dvbc_locator;
325         localComPtr(): p_dvbc_tune_request(NULL), p_dvbc_locator(NULL) {};
326         ~localComPtr()
327         {
328             if( p_dvbc_tune_request )
329                 p_dvbc_tune_request->Release();
330             if( p_dvbc_locator )
331                 p_dvbc_locator->Release();
332         }
333     } l;
334
335     long l_frequency, l_symbolrate;
336     int  i_qam;
337     ModulationType i_qam_mod;
338
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;
344     if( i_qam == 16 )
345         i_qam_mod = BDA_MOD_16QAM;
346     if( i_qam == 32 )
347         i_qam_mod = BDA_MOD_32QAM;
348     if( i_qam == 64 )
349         i_qam_mod = BDA_MOD_64QAM;
350     if( i_qam == 128 )
351         i_qam_mod = BDA_MOD_128QAM;
352     if( i_qam == 256 )
353         i_qam_mod = BDA_MOD_256QAM;
354
355     guid_network_type = CLSID_DVBCNetworkProvider;
356     hr = CreateTuneRequest();
357     if( FAILED( hr ) )
358     {
359         msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
360             "Cannot create Tune Request: hr=0x%8lx", hr );
361         return VLC_EGENERIC;
362     }
363
364     hr = p_tune_request->QueryInterface( IID_IDVBTuneRequest,
365         (void**)&l.p_dvbc_tune_request );
366     if( FAILED( hr ) )
367     {
368         msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
369             "Cannot QI for IDVBTuneRequest: hr=0x%8lx", hr );
370         return VLC_EGENERIC;
371     }
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 );
375
376     hr = ::CoCreateInstance( CLSID_DVBCLocator, 0, CLSCTX_INPROC,
377         IID_IDVBCLocator, (void**)&l.p_dvbc_locator );
378     if( FAILED( hr ) )
379     {
380         msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
381             "Cannot create the DVB-C Locator: hr=0x%8lx", hr );
382         return VLC_EGENERIC;
383     }
384
385     hr = S_OK;
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 );
392     if( FAILED( hr ) )
393     {
394         msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
395             "Cannot set tuning parameters on Locator: hr=0x%8lx", hr );
396         return VLC_EGENERIC;
397     }
398
399     hr = p_tune_request->put_Locator( l.p_dvbc_locator );
400     if( FAILED( hr ) )
401     {
402         msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
403             "Cannot put the locator: hr=0x%8lx", hr );
404         return VLC_EGENERIC;
405     }
406
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
409      * devices */
410     do
411     {
412         hr = Build();
413         if( FAILED( hr ) )
414         {
415             msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
416                 "Cannot Build the Graph: hr=0x%8lx", hr );
417             return VLC_EGENERIC;
418         }
419         hr = Start();
420     }
421     while( hr != S_OK );
422
423     return VLC_SUCCESS;
424 }
425
426 /*****************************************************************************
427 * Submit a DVB-S Tune Request
428 ******************************************************************************/
429 int BDAGraph::SubmitDVBSTuneRequest()
430 {
431     HRESULT hr = S_OK;
432
433     class localComPtr
434     {
435         public:
436         IDVBTuneRequest* p_dvbs_tune_request;
437         IDVBSLocator* p_dvbs_locator;
438         localComPtr(): p_dvbs_tune_request(NULL), p_dvbs_locator(NULL) {};
439         ~localComPtr()
440         {
441             if( p_dvbs_tune_request )
442                 p_dvbs_tune_request->Release();
443             if( p_dvbs_locator )
444                 p_dvbs_locator->Release();
445         }
446     } l;
447     long l_frequency, l_symbolrate, l_azimuth, l_elevation, l_longitude;
448     char* psz_polarisation;
449     Polarisation i_polar;
450     VARIANT_BOOL b_west;
451
452     l_frequency = l_symbolrate = l_azimuth = l_elevation = l_longitude = -1;
453     l_frequency = var_GetInteger( p_access, "dvb-frequency" );
454     l_symbolrate = var_GetInteger( p_access, "dvb-srate" );
455     l_azimuth = var_GetInteger( p_access, "dvb-azimuth" );
456     l_elevation = var_GetInteger( p_access, "dvb-elevation" );
457     l_longitude = var_GetInteger( p_access, "dvb-longitude" );
458     psz_polarisation = var_GetString( p_access, "dvb-polarisation" );
459
460     b_west = ( l_longitude < 0 ) ? TRUE : FALSE;
461
462     i_polar = BDA_POLARISATION_NOT_SET;
463     if( *psz_polarisation == 'H' || *psz_polarisation == 'h' )
464         i_polar = BDA_POLARISATION_LINEAR_H;
465     if( *psz_polarisation == 'V' || *psz_polarisation == 'v' )
466         i_polar = BDA_POLARISATION_LINEAR_V;
467     if( *psz_polarisation == 'L' || *psz_polarisation == 'l' )
468         i_polar = BDA_POLARISATION_CIRCULAR_L;
469     if( *psz_polarisation == 'R' || *psz_polarisation == 'r' )
470         i_polar = BDA_POLARISATION_CIRCULAR_R;
471
472     guid_network_type = CLSID_DVBSNetworkProvider;
473     hr = CreateTuneRequest();
474     if( FAILED( hr ) )
475     {
476         msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
477             "Cannot create Tune Request: hr=0x%8lx", hr );
478         return VLC_EGENERIC;
479     }
480
481     hr = p_tune_request->QueryInterface( IID_IDVBTuneRequest,
482         (void**)&l.p_dvbs_tune_request );
483     if( FAILED( hr ) )
484     {
485         msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
486             "Cannot QI for IDVBTuneRequest: hr=0x%8lx", hr );
487         return VLC_EGENERIC;
488     }
489     l.p_dvbs_tune_request->put_ONID( -1 );
490     l.p_dvbs_tune_request->put_SID( -1 );
491     l.p_dvbs_tune_request->put_TSID( -1 );
492
493     hr = ::CoCreateInstance( CLSID_DVBSLocator, 0, CLSCTX_INPROC,
494         IID_IDVBSLocator, (void**)&l.p_dvbs_locator );
495     if( FAILED( hr ) )
496     {
497         msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
498             "Cannot create the DVBS Locator: hr=0x%8lx", hr );
499         return VLC_EGENERIC;
500     }
501
502     hr = S_OK;
503     if( l_frequency > 0 )
504         hr = l.p_dvbs_locator->put_CarrierFrequency( l_frequency );
505     if( SUCCEEDED( hr ) && l_symbolrate > 0 )
506         hr = l.p_dvbs_locator->put_SymbolRate( l_symbolrate );
507     if( SUCCEEDED( hr ) && l_azimuth > 0 )
508         hr = l.p_dvbs_locator->put_Azimuth( l_azimuth );
509     if( SUCCEEDED( hr ) && l_elevation > 0 )
510         hr = l.p_dvbs_locator->put_Elevation( l_elevation );
511     if( SUCCEEDED( hr ) )
512         hr = l.p_dvbs_locator->put_OrbitalPosition( labs( l_longitude ) );
513     if( SUCCEEDED( hr ) )
514         hr = l.p_dvbs_locator->put_WestPosition( b_west );
515     if( SUCCEEDED( hr ) && i_polar != BDA_POLARISATION_NOT_SET )
516         hr = l.p_dvbs_locator->put_SignalPolarisation( i_polar );
517     if( FAILED( hr ) )
518     {
519         msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
520             "Cannot set tuning parameters on Locator: hr=0x%8lx", hr );
521         return VLC_EGENERIC;
522     }
523
524     hr = p_tune_request->put_Locator( l.p_dvbs_locator );
525     if( FAILED( hr ) )
526     {
527         msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
528             "Cannot put the locator: hr=0x%8lx", hr );
529         return VLC_EGENERIC;
530     }
531
532     /* Build and Run the Graph. If a Tuner device is in use the graph will
533      * fail to run. Repeated calls to build will check successive tuner
534      * devices */
535     do
536     {
537         hr = Build();
538         if( FAILED( hr ) )
539         {
540             msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
541                 "Cannot Build the Graph: hr=0x%8lx", hr );
542             return VLC_EGENERIC;
543         }
544         hr = Start();
545     }
546     while( hr != S_OK );
547
548     return VLC_SUCCESS;
549 }
550
551 /*****************************************************************************
552 * Load the Tuning Space from System Tuning Spaces according to the
553 * Network Type requested
554 ******************************************************************************/
555 HRESULT BDAGraph::CreateTuneRequest()
556 {
557     HRESULT hr = S_OK;
558     GUID guid_this_network_type;
559     class localComPtr
560     {
561         public:
562         ITuningSpaceContainer*  p_tuning_space_container;
563         IEnumTuningSpaces*      p_tuning_space_enum;
564         ITuningSpace*           p_this_tuning_space;
565         localComPtr(): p_tuning_space_container(NULL),
566             p_tuning_space_enum(NULL), p_this_tuning_space(NULL) {};
567         ~localComPtr()
568         {
569             if( p_tuning_space_container )
570                 p_tuning_space_container->Release();
571             if( p_tuning_space_enum )
572                 p_tuning_space_enum->Release();
573             if( p_this_tuning_space )
574                 p_this_tuning_space->Release();
575         }
576     } l;
577
578     /* A Tuning Space may already have been set up. If it is for the same
579      * network type then all is well. Otherwise, reset the Tuning Space and get
580      * a new one */
581     if( p_tuning_space )
582     {
583         hr = p_tuning_space->get__NetworkType( &guid_this_network_type );
584         if( FAILED( hr ) ) guid_this_network_type = GUID_NULL;
585         if( guid_this_network_type == guid_network_type )
586         {
587             return S_OK;
588         }
589         else
590         {
591             p_tuning_space->Release();
592             p_tuning_space = NULL;
593         }
594     }
595
596     /* Force use of the first available Tuner Device during Build */
597     l_tuner_used = -1;
598
599     /* Get the SystemTuningSpaces container to enumerate through all the
600      * defined tuning spaces */
601     hr = ::CoCreateInstance( CLSID_SystemTuningSpaces, 0, CLSCTX_INPROC,
602         IID_ITuningSpaceContainer, (void**)&l.p_tuning_space_container );
603     if( FAILED( hr ) )
604     {
605         msg_Warn( p_access, "CreateTuneRequest: "\
606             "Cannot CoCreate SystemTuningSpaces: hr=0x%8lx", hr );
607         return hr;
608     }
609
610     hr = l.p_tuning_space_container->get_EnumTuningSpaces(
611          &l.p_tuning_space_enum );
612     if( FAILED( hr ) )
613     {
614         msg_Warn( p_access, "CreateTuneRequest: "\
615             "Cannot create SystemTuningSpaces Enumerator: hr=0x%8lx", hr );
616         return hr;
617     }
618
619     while( l.p_tuning_space_enum->Next( 1, &l.p_this_tuning_space, NULL ) ==
620         S_OK )
621     {
622        hr = l.p_this_tuning_space->get__NetworkType( &guid_this_network_type );
623
624         /* GUID_NULL means a non-BDA network was found e.g analog
625          * Ignore failures and non-BDA networks and keep looking */
626         if( FAILED( hr ) ) guid_this_network_type == GUID_NULL;
627
628         if( guid_this_network_type == guid_network_type )
629         {
630             hr = l.p_this_tuning_space->QueryInterface( IID_ITuningSpace,
631                 (void**)&p_tuning_space );
632             if( FAILED( hr ) )
633             {
634                 msg_Warn( p_access, "CreateTuneRequest: "\
635                     "Cannot QI Tuning Space: hr=0x%8lx", hr );
636                 return hr;
637             }
638             hr = p_tuning_space->CreateTuneRequest( &p_tune_request );
639             if( FAILED( hr ) )
640             {
641                 msg_Warn( p_access, "CreateTuneRequest: "\
642                     "Cannot Create Tune Request: hr=0x%8lx", hr );
643                 return hr;
644             }
645             return hr;
646         }
647     }
648     hr = E_FAIL;
649     if( FAILED( hr ) )
650     {
651         msg_Warn( p_access, "CreateTuneRequest: "\
652             "Cannot find a suitable System Tuning Space: hr=0x%8lx", hr );
653         return hr;
654     }
655     return hr;
656 }
657
658 /******************************************************************************
659 * Build
660 * Step 4: Build the Filter Graph
661 * Build sets up devices, adds and connects filters
662 ******************************************************************************/
663 HRESULT BDAGraph::Build()
664 {
665     HRESULT hr = S_OK;
666     long l_capture_used, l_tif_used;
667     AM_MEDIA_TYPE grabber_media_type;
668
669     /* If we have already have a filter graph, rebuild it*/
670     Destroy();
671
672     hr = ::CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC,
673         IID_IGraphBuilder, (void**)&p_filter_graph );
674     if( FAILED( hr ) )
675     {
676         msg_Warn( p_access, "Build: "\
677             "Cannot CoCreate IFilterGraph: hr=0x%8lx", hr );
678         return hr;
679     }
680
681     /* First filter in the graph is the Network Provider and
682      * its Scanning Tuner which takes the Tune Request*/
683     hr = ::CoCreateInstance( guid_network_type, NULL, CLSCTX_INPROC_SERVER,
684         IID_IBaseFilter, (void**)&p_network_provider);
685     if( FAILED( hr ) )
686     {
687         msg_Warn( p_access, "Build: "\
688             "Cannot CoCreate Network Provider: hr=0x%8lx", hr );
689         return hr;
690     }
691     hr = p_filter_graph->AddFilter( p_network_provider, L"Network Provider" );
692     if( FAILED( hr ) )
693     {
694         msg_Warn( p_access, "Build: "\
695             "Cannot load network provider: hr=0x%8lx", hr );
696         return hr;
697     }
698
699     hr = p_network_provider->QueryInterface( IID_IScanningTuner,
700         (void**)&p_scanning_tuner );
701     if( FAILED( hr ) )
702     {
703         msg_Warn( p_access, "Build: "\
704             "Cannot QI Network Provider for Scanning Tuner: hr=0x%8lx", hr );
705         return hr;
706     }
707
708     hr = p_scanning_tuner->Validate( p_tune_request );
709     if( FAILED( hr ) )
710     {
711         msg_Warn( p_access, "Build: "\
712             "Tune Request is invalid: hr=0x%8lx", hr );
713         return hr;
714     }
715     hr = p_scanning_tuner->put_TuneRequest( p_tune_request );
716     if( FAILED( hr ) )
717     {
718         msg_Warn( p_access, "Build: "\
719             "Cannot submit the tune request: hr=0x%8lx", hr );
720         return hr;
721     }
722
723     /* Add the Network Tuner to the Network Provider. On subsequent calls,
724      * l_tuner_used will cause a different tuner to be selected */
725     hr = FindFilter( KSCATEGORY_BDA_NETWORK_TUNER, &l_tuner_used,
726         p_network_provider, &p_tuner_device );
727     if( FAILED( hr ) )
728     {
729         msg_Warn( p_access, "Build: "\
730             "Cannot load tuner device and connect network provider: "\
731             "hr=0x%8lx", hr );
732         return hr;
733     }
734
735     /* Always look for all capture devices to match the Network Tuner */
736     l_capture_used = -1;
737     hr = FindFilter( KSCATEGORY_BDA_RECEIVER_COMPONENT, &l_capture_used,
738         p_tuner_device, &p_capture_device );
739     if( FAILED( hr ) )
740     {
741         /* Some BDA drivers do not provide a Capture Device Filter so force
742          * the Sample Grabber to connect directly to the Tuner Device */
743         p_capture_device = p_tuner_device;
744         p_tuner_device = NULL;
745         msg_Warn( p_access, "Build: "\
746             "Cannot find Capture device. Connecting to tuner: hr=0x%8lx", hr );
747     }
748
749     /* Insert the Sample Grabber to tap into the Transport Stream. */
750     hr = ::CoCreateInstance( CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
751         IID_IBaseFilter, (void**)&p_sample_grabber );
752     if( FAILED( hr ) )
753     {
754         msg_Warn( p_access, "Build: "\
755             "Cannot load Sample Grabber Filter: hr=0x%8lx", hr );
756         return hr;
757     }
758     hr = p_filter_graph->AddFilter( p_sample_grabber, L"Sample Grabber" );
759     if( FAILED( hr ) )
760     {
761         msg_Warn( p_access, "Build: "\
762             "Cannot add Sample Grabber Filter to graph: hr=0x%8lx", hr );
763         return hr;
764     }
765
766     hr = p_sample_grabber->QueryInterface( IID_ISampleGrabber,
767         (void**)&p_grabber );
768     if( FAILED( hr ) )
769     {
770         msg_Warn( p_access, "Build: "\
771             "Cannot QI Sample Grabber Filter: hr=0x%8lx", hr );
772         return hr;
773     }
774
775     ZeroMemory( &grabber_media_type, sizeof( AM_MEDIA_TYPE ) );
776     grabber_media_type.majortype == MEDIATYPE_Stream;
777     grabber_media_type.subtype == MEDIASUBTYPE_MPEG2_TRANSPORT;
778     hr = p_grabber->SetMediaType( &grabber_media_type );
779     if( FAILED( hr ) )
780     {
781         msg_Warn( p_access, "Build: "\
782             "Cannot set media type on grabber filter: hr=0x%8lx", hr );
783         return hr;
784     }
785     hr = Connect( p_capture_device, p_sample_grabber );
786     if( FAILED( hr ) )
787     {
788         msg_Warn( p_access, "Build: "\
789             "Cannot connect Sample Grabber to Capture device: hr=0x%8lx", hr );
790         return hr;
791     }
792
793     /* We need the MPEG2 Demultiplexer even though we are going to use the VLC
794      * TS demuxer. The TIF filter connects to the MPEG2 demux and works with
795      * the Network Provider filter to set up the stream */
796     hr = ::CoCreateInstance( CLSID_MPEG2Demultiplexer, NULL,
797         CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)&p_mpeg_demux );
798     if( FAILED( hr ) )
799     {
800         msg_Warn( p_access, "Build: "\
801             "Cannot CoCreateInstance MPEG2 Demultiplexer: hr=0x%8lx", hr );
802         return hr;
803     }
804     hr = p_filter_graph->AddFilter( p_mpeg_demux, L"Demux" );
805     if( FAILED( hr ) )
806     {
807         msg_Warn( p_access, "Build: "\
808             "Cannot add demux filter to graph: hr=0x%8lx", hr );
809         return hr;
810     }
811
812     hr = Connect( p_sample_grabber, p_mpeg_demux );
813     if( FAILED( hr ) )
814     {
815         msg_Warn( p_access, "Build: "\
816             "Cannot connect demux to grabber: hr=0x%8lx", hr );
817         return hr;
818     }
819
820     /* Always look for the Transform Information Filter from the start
821      * of the collection*/
822     l_tif_used = -1;
823     hr = FindFilter( KSCATEGORY_BDA_TRANSPORT_INFORMATION, &l_tif_used,
824         p_mpeg_demux, &p_transport_info );
825     if( FAILED( hr ) )
826     {
827         msg_Warn( p_access, "Build: "\
828             "Cannot load TIF onto demux: hr=0x%8lx", hr );
829         return hr;
830     }
831
832     /* Configure the Sample Grabber to buffer the samples continuously */
833     hr = p_grabber->SetBufferSamples( TRUE );
834     if( FAILED( hr ) )
835     {
836         msg_Warn( p_access, "Build: "\
837             "Cannot set Sample Grabber to buffering: hr=0x%8lx", hr );
838         return hr;
839     }
840     hr = p_grabber->SetOneShot( FALSE );
841     if( FAILED( hr ) )
842     {
843         msg_Warn( p_access, "Build: "\
844             "Cannot set Sample Grabber to multi shot: hr=0x%8lx", hr );
845         return hr;
846     }
847     hr = p_grabber->SetCallback( this, 0 );
848     if( FAILED( hr ) )
849     {
850         msg_Warn( p_access, "Build: "\
851             "Cannot set SampleGrabber Callback: hr=0x%8lx", hr );
852         return hr;
853     }
854
855     hr = Register();
856     if( FAILED( hr ) )
857     {
858         d_graph_register = 0;
859     }
860
861     /* The Media Control is used to Run and Stop the Graph */
862     hr = p_filter_graph->QueryInterface( IID_IMediaControl,
863         (void**)&p_media_control );
864     if( FAILED( hr ) )
865     {
866         msg_Warn( p_access, "Build: "\
867             "Cannot QI Media Control: hr=0x%8lx", hr );
868         return hr;
869     }
870     return hr;
871 }
872
873 /******************************************************************************
874 * FindFilter
875 * Looks up all filters in a category and connects to the upstream filter until
876 * a successful match is found. The index of the connected filter is returned.
877 * On subsequent calls, this can be used to start from that point to find
878 * another match.
879 * This is used when the graph does not run because a tuner device is in use so
880 * another one needs to be slected.
881 ******************************************************************************/
882 HRESULT BDAGraph::FindFilter( REFCLSID clsid, long* i_moniker_used,
883     IBaseFilter* p_upstream, IBaseFilter** p_p_downstream )
884 {
885     HRESULT                 hr = S_OK;
886     int                     i_moniker_index = -1;
887     class localComPtr
888     {
889         public:
890         IMoniker*      p_moniker;
891         IEnumMoniker*  p_moniker_enum;
892         IBaseFilter*   p_filter;
893         IPropertyBag*  p_property_bag;
894         VARIANT        var_bstr;
895         localComPtr():
896             p_moniker(NULL),
897             p_moniker_enum(NULL),
898             p_filter(NULL),
899             p_property_bag(NULL)
900             { ::VariantInit(&var_bstr); };
901         ~localComPtr()
902         {
903             if( p_moniker )
904                 p_moniker->Release();
905             if( p_moniker_enum )
906                 p_moniker_enum->Release();
907             if( p_filter )
908                 p_filter->Release();
909             if( p_property_bag )
910                 p_property_bag->Release();
911             ::VariantClear(&var_bstr);
912         }
913     } l;
914
915     if( !p_system_dev_enum )
916     {
917         hr = ::CoCreateInstance( CLSID_SystemDeviceEnum, 0, CLSCTX_INPROC,
918             IID_ICreateDevEnum, (void**)&p_system_dev_enum );
919         if( FAILED( hr ) )
920         {
921             msg_Warn( p_access, "FindFilter: "\
922                 "Cannot CoCreate SystemDeviceEnum: hr=0x%8lx", hr );
923             return hr;
924         }
925     }
926
927     hr = p_system_dev_enum->CreateClassEnumerator( clsid,
928         &l.p_moniker_enum, 0 );
929     if( hr != S_OK )
930     {
931         msg_Warn( p_access, "FindFilter: "\
932             "Cannot CreateClassEnumerator: hr=0x%8lx", hr );
933         return E_FAIL;
934     }
935     while( l.p_moniker_enum->Next( 1, &l.p_moniker, 0 ) == S_OK )
936     {
937         i_moniker_index++;
938
939         /* Skip over devices already found on previous calls */
940         if( i_moniker_index <= *i_moniker_used ) continue;
941         *i_moniker_used = i_moniker_index;
942
943         hr = l.p_moniker->BindToObject( NULL, NULL, IID_IBaseFilter,
944             (void**)&l.p_filter );
945         if( FAILED( hr ) )
946         {
947             if( l.p_moniker )
948                 l.p_moniker->Release();
949             l.p_moniker = NULL;
950             if( l.p_filter)
951                  l.p_filter->Release();
952             l.p_filter = NULL;
953             continue;
954         }
955
956         hr = l.p_moniker->BindToStorage( NULL, NULL, IID_IPropertyBag,
957             (void**)&l.p_property_bag );
958         if( FAILED( hr ) )
959         {
960             msg_Warn( p_access, "FindFilter: "\
961                 "Cannot Bind to Property Bag: hr=0x%8lx", hr );
962             return hr;
963         }
964
965         hr = l.p_property_bag->Read( L"FriendlyName", &l.var_bstr, NULL );
966         if( FAILED( hr ) )
967         {
968             msg_Warn( p_access, "FindFilter: "\
969                 "Cannot read filter friendly name: hr=0x%8lx", hr );
970             return hr;
971         }
972
973         hr = p_filter_graph->AddFilter( l.p_filter, l.var_bstr.bstrVal );
974         if( FAILED( hr ) )
975         {
976             msg_Warn( p_access, "FindFilter: "\
977                 "Cannot add filter: hr=0x%8lx", hr );
978             return hr;
979         }
980
981         hr = Connect( p_upstream, l.p_filter );
982         if( SUCCEEDED( hr ) )
983         {
984             msg_Dbg( p_access, "FindFilter: Connected %S", l.var_bstr.bstrVal );
985             l.p_filter->QueryInterface( IID_IBaseFilter,
986                 (void**)p_p_downstream );
987             return S_OK;
988         }
989         /* Not the filter we want so unload and try the next one */
990         hr = p_filter_graph->RemoveFilter( l.p_filter );
991         if( FAILED( hr ) )
992         {
993             msg_Warn( p_access, "FindFilter: "\
994                 "Failed unloading Filter: hr=0x%8lx", hr );
995             return hr;
996         }
997
998         if( l.p_moniker )
999             l.p_moniker->Release();
1000         l.p_moniker = NULL;
1001         if( l.p_filter)
1002             l.p_filter->Release();
1003         l.p_filter = NULL;
1004     }
1005
1006     hr = E_FAIL;
1007     msg_Warn( p_access, "FindFilter: No filter connected: hr=0x%8lx", hr );
1008     return hr;
1009 }
1010
1011 /*****************************************************************************
1012 * Connect is called from Build to enumerate and connect pins
1013 *****************************************************************************/
1014 HRESULT BDAGraph::Connect( IBaseFilter* p_upstream, IBaseFilter* p_downstream )
1015 {
1016     HRESULT             hr = E_FAIL;
1017     class localComPtr
1018     {
1019         public:
1020         IPin*      p_pin_upstream;
1021         IPin*      p_pin_downstream;
1022         IEnumPins* p_pin_upstream_enum;
1023         IEnumPins* p_pin_downstream_enum;
1024         IPin*      p_pin_temp;
1025         localComPtr(): p_pin_upstream(NULL), p_pin_downstream(NULL),
1026             p_pin_upstream_enum(NULL), p_pin_downstream_enum(NULL),
1027             p_pin_temp(NULL) { };
1028         ~localComPtr()
1029         {
1030             if( p_pin_upstream )
1031                 p_pin_upstream->Release();
1032             if( p_pin_downstream )
1033                 p_pin_downstream->Release();
1034             if( p_pin_upstream_enum )
1035                 p_pin_upstream_enum->Release();
1036             if( p_pin_downstream_enum )
1037                 p_pin_downstream_enum->Release();
1038             if( p_pin_temp )
1039                 p_pin_temp->Release();
1040         }
1041     } l;
1042
1043     PIN_INFO            pin_info_upstream;
1044     PIN_INFO            pin_info_downstream;
1045
1046     hr = p_upstream->EnumPins( &l.p_pin_upstream_enum );
1047     if( FAILED( hr ) )
1048     {
1049         msg_Warn( p_access, "Connect: "\
1050             "Cannot get upstream filter enumerator: hr=0x%8lx", hr );
1051         return hr;
1052     }
1053     while( l.p_pin_upstream_enum->Next( 1, &l.p_pin_upstream, 0 ) == S_OK )
1054     {
1055         hr = l.p_pin_upstream->QueryPinInfo( &pin_info_upstream );
1056         if( FAILED( hr ) )
1057         {
1058             msg_Warn( p_access, "Connect: "\
1059                 "Cannot get upstream filter pin information: hr=0x%8lx", hr );
1060             return hr;
1061         }
1062         hr = l.p_pin_upstream->ConnectedTo( &l.p_pin_downstream );
1063         if( hr == S_OK )
1064             l.p_pin_downstream->Release();
1065         if(FAILED( hr ) && hr != VFW_E_NOT_CONNECTED )
1066         {
1067             msg_Warn( p_access, "Connect: "\
1068                 "Cannot check upstream filter connection: hr=0x%8lx", hr );
1069             return hr;
1070         }
1071         if(( pin_info_upstream.dir == PINDIR_OUTPUT ) &&
1072            ( hr == VFW_E_NOT_CONNECTED ) )
1073         {
1074             /* The upstream pin is not yet connected so check each pin on the
1075              * downstream filter */
1076             hr = p_downstream->EnumPins( &l.p_pin_downstream_enum );
1077             if( FAILED( hr ) )
1078             {
1079                 msg_Warn( p_access, "Connect: Cannot get "\
1080                     "downstream filter enumerator: hr=0x%8lx", hr );
1081                 return hr;
1082             }
1083             while( l.p_pin_downstream_enum->Next( 1, &l.p_pin_downstream, 0 )
1084                 == S_OK )
1085             {
1086                 hr = l.p_pin_downstream->QueryPinInfo( &pin_info_downstream );
1087                 if( FAILED( hr ) )
1088                 {
1089                     msg_Warn( p_access, "Connect: Cannot get "\
1090                         "downstream filter pin information: hr=0x%8lx", hr );
1091                     return hr;
1092                 }
1093
1094                 hr = l.p_pin_downstream->ConnectedTo( &l.p_pin_temp );
1095                 if( hr == S_OK ) l.p_pin_temp->Release();
1096                 if( hr != VFW_E_NOT_CONNECTED )
1097                 {
1098                     if( FAILED( hr ) )
1099                     {
1100                         msg_Warn( p_access, "Connect: Cannot check "\
1101                             "downstream filter connection: hr=0x%8lx", hr );
1102                         return hr;
1103                     }
1104                 }
1105                 if(( pin_info_downstream.dir == PINDIR_INPUT ) &&
1106                    ( hr == VFW_E_NOT_CONNECTED ) )
1107                 {
1108                     hr = p_filter_graph->ConnectDirect( l.p_pin_upstream,
1109                         l.p_pin_downstream, NULL );
1110                     if( SUCCEEDED( hr ) )
1111                     {
1112                         pin_info_downstream.pFilter->Release();
1113                         pin_info_upstream.pFilter->Release();
1114                         return S_OK;
1115                     }
1116                 }
1117                 /* If we fall out here it means this downstream pin was not
1118                  * suitable so try the next downstream pin */
1119                 l.p_pin_downstream = NULL;
1120                 pin_info_downstream.pFilter->Release();
1121             }
1122         }
1123
1124         /* If we fall out here it means we did not find any suitable downstream
1125          * pin so try the next upstream pin */
1126         l.p_pin_upstream = NULL;
1127         pin_info_upstream.pFilter->Release();
1128     }
1129
1130     /* If we fall out here it means we did not find any pair of suitable pins */
1131     return E_FAIL;
1132 }
1133
1134 /*****************************************************************************
1135 * Start uses MediaControl to start the graph
1136 *****************************************************************************/
1137 HRESULT BDAGraph::Start()
1138 {
1139     HRESULT hr = S_OK;
1140     OAFilterState i_state; /* State_Stopped, State_Paused, State_Running */
1141
1142     if( !p_media_control )
1143     {
1144         msg_Dbg( p_access, "Start: Media Control has not been created" );
1145         return E_FAIL;
1146     }
1147     hr = p_media_control->Run();
1148     if( hr == S_OK )
1149         return hr;
1150
1151     /* Query the state of the graph - timeout after 100 milliseconds */
1152     while( hr = p_media_control->GetState( 100, &i_state ) != S_OK )
1153     {
1154         if( FAILED( hr ) )
1155         {
1156             msg_Warn( p_access,
1157                 "Start: Cannot get Graph state: hr=0x%8lx", hr );
1158             return hr;
1159         }
1160     }
1161     if( i_state == State_Running )
1162         return hr;
1163
1164     /* The Graph is not running so stop it and return an error */
1165     hr = p_media_control->Stop();
1166     if( FAILED( hr ) )
1167     {
1168         msg_Warn( p_access,
1169             "Start: Cannot stop Graph after Run failed: hr=0x%8lx", hr );
1170         return hr;
1171     }
1172     return E_FAIL;
1173 }
1174
1175 /*****************************************************************************
1176 * Read the stream of data - query the buffer size required
1177 *****************************************************************************/
1178 long BDAGraph::GetBufferSize()
1179 {
1180     long l_buffer_size = 0;
1181     long l_queue_size;
1182
1183     b_ready = true;
1184
1185     for( int i_timer = 0; queue_sample.empty() && i_timer < 200; i_timer++ )
1186         Sleep( 10 );
1187
1188     l_queue_size = queue_sample.size();
1189     if( l_queue_size <= 0 )
1190     {
1191         msg_Warn( p_access, "BDA GetBufferSize: Timed Out waiting for sample" );
1192         return -1;
1193     }
1194
1195     /* Establish the length of the queue as it grows quickly. If the queue
1196      * size is checked dynamically there is a risk of not exiting the loop */
1197     for( long l_queue_count=0; l_queue_count < l_queue_size; l_queue_count++ )
1198     {
1199         l_buffer_size += queue_sample.front()->GetActualDataLength();
1200         queue_buffer.push( queue_sample.front() );
1201         queue_sample.pop();
1202     }
1203     return l_buffer_size;
1204 }
1205
1206 /*****************************************************************************
1207 * Read the stream of data - Retrieve from the buffer queue
1208 ******************************************************************************/
1209 long BDAGraph::ReadBuffer( long* pl_buffer_len, BYTE* p_buffer )
1210 {
1211     HRESULT hr = S_OK;
1212
1213     *pl_buffer_len = 0;
1214     BYTE *p_buff_temp;
1215
1216     while( !queue_buffer.empty() )
1217     {
1218         queue_buffer.front()->GetPointer( &p_buff_temp );
1219         hr = queue_buffer.front()->IsDiscontinuity();
1220         if( hr == S_OK )
1221             msg_Warn( p_access,
1222                 "BDA ReadBuffer: Sample Discontinuity. 0x%8lx", hr );
1223         memcpy( p_buffer + *pl_buffer_len, p_buff_temp,
1224             queue_buffer.front()->GetActualDataLength() );
1225         *pl_buffer_len += queue_buffer.front()->GetActualDataLength();
1226         queue_buffer.front()->Release();
1227         queue_buffer.pop();
1228     }
1229
1230     return *pl_buffer_len;
1231 }
1232
1233 /******************************************************************************
1234 * SampleCB - Callback when the Sample Grabber has a sample
1235 ******************************************************************************/
1236 STDMETHODIMP BDAGraph::SampleCB( double d_time, IMediaSample *p_sample )
1237 {
1238     if( b_ready )
1239     {
1240         p_sample->AddRef();
1241         queue_sample.push( p_sample );
1242     }
1243     else
1244     {
1245         msg_Warn( p_access, "BDA SampleCB: Not ready - dropped sample" );
1246     }
1247     return S_OK;
1248 }
1249
1250 STDMETHODIMP BDAGraph::BufferCB( double d_time, BYTE* p_buffer,
1251     long l_buffer_len )
1252 {
1253     return E_FAIL;
1254 }
1255
1256 /******************************************************************************
1257 * removes each filter from the graph
1258 ******************************************************************************/
1259 HRESULT BDAGraph::Destroy()
1260 {
1261     HRESULT hr = S_OK;
1262
1263     if( p_media_control )
1264         hr = p_media_control->Stop();
1265
1266     if( p_transport_info )
1267     {
1268         p_filter_graph->RemoveFilter( p_transport_info );
1269         p_transport_info->Release();
1270         p_transport_info = NULL;
1271     }
1272     if( p_mpeg_demux )
1273     {
1274         p_filter_graph->RemoveFilter( p_mpeg_demux );
1275         p_mpeg_demux->Release();
1276         p_mpeg_demux = NULL;
1277     }
1278     if( p_sample_grabber )
1279     {
1280         p_filter_graph->RemoveFilter( p_sample_grabber );
1281         p_sample_grabber->Release();
1282         p_sample_grabber = NULL;
1283     }
1284     if( p_capture_device )
1285     {
1286         p_filter_graph->RemoveFilter( p_capture_device );
1287         p_capture_device->Release();
1288         p_capture_device = NULL;
1289     }
1290     if( p_tuner_device )
1291     {
1292         p_filter_graph->RemoveFilter( p_tuner_device );
1293         p_tuner_device->Release();
1294         p_tuner_device = NULL;
1295     }
1296     if( p_network_provider )
1297     {
1298         p_filter_graph->RemoveFilter( p_network_provider );
1299         p_network_provider->Release();
1300         p_network_provider = NULL;
1301     }
1302
1303     if( p_scanning_tuner )
1304     {
1305         p_scanning_tuner->Release();
1306         p_scanning_tuner = NULL;
1307     }
1308     if( p_media_control )
1309     {
1310         p_media_control->Release();
1311         p_media_control = NULL;
1312     }
1313     if( p_scanning_tuner )
1314     {
1315         p_filter_graph->Release();
1316         p_filter_graph = NULL;
1317     }
1318
1319     if( d_graph_register )
1320     {
1321         Deregister();
1322     }
1323
1324     return S_OK;
1325 }
1326
1327 /*****************************************************************************
1328 * Add/Remove a DirectShow filter graph to/from the Running Object Table.
1329 * Allows GraphEdit to "spy" on a remote filter graph.
1330 ******************************************************************************/
1331 HRESULT BDAGraph::Register()
1332 {
1333     class localComPtr
1334     {
1335         public:
1336         IMoniker*             p_moniker;
1337         IRunningObjectTable*  p_ro_table;
1338         localComPtr(): p_moniker(NULL), p_ro_table(NULL) {};
1339         ~localComPtr()
1340         {
1341             if( p_moniker )
1342                 p_moniker->Release();
1343             if( p_ro_table )
1344                 p_ro_table->Release();
1345         }
1346     } l;
1347     WCHAR     psz_w_graph_name[128];
1348     HRESULT   hr;
1349
1350     hr = ::GetRunningObjectTable( 0, &l.p_ro_table );
1351     if( FAILED( hr ) )
1352     {
1353         msg_Warn( p_access, "Register: Cannot get ROT: hr=0x%8lx", hr );
1354         return hr;
1355     }
1356
1357     wsprintfW( psz_w_graph_name, L"VLC BDA Graph %08x Pid %08x",
1358         (DWORD_PTR) p_filter_graph, ::GetCurrentProcessId() );
1359     hr = CreateItemMoniker( L"!", psz_w_graph_name, &l.p_moniker );
1360     if( FAILED( hr ) )
1361     {
1362         msg_Warn( p_access, "Register: Cannot Create Moniker: hr=0x%8lx", hr );
1363         return hr;
1364     }
1365     hr = l.p_ro_table->Register( ROTFLAGS_REGISTRATIONKEEPSALIVE,
1366         p_filter_graph, l.p_moniker, &d_graph_register );
1367     if( FAILED( hr ) )
1368     {
1369         msg_Warn( p_access, "Register: Cannot Register Graph: hr=0x%8lx", hr );
1370         return hr;
1371     }
1372     msg_Dbg( p_access, "Register: registered Graph: %S", psz_w_graph_name );
1373     return hr;
1374 }
1375
1376 void BDAGraph::Deregister()
1377 {
1378     HRESULT   hr;
1379     IRunningObjectTable* p_ro_table;
1380     hr = ::GetRunningObjectTable( 0, &p_ro_table );
1381     if( SUCCEEDED( hr ) )
1382         p_ro_table->Revoke( d_graph_register );
1383     d_graph_register = 0;
1384     p_ro_table->Release();
1385 }