root/modules/access/bda/bdagraph.cpp

Revision f09b30c782d9a5f76f403038ed703c3c381b2468, 49.0 kB (checked in by Rémi Denis-Courmont <rem@videolan.org>, 1 year ago)

Memory error handling

  • Property mode set to 100644
Line 
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 #include <ctype.h>
28
29 /****************************************************************************
30  * Interfaces for calls from C
31  ****************************************************************************/
32 extern "C" {
33
34     void dvb_newBDAGraph( access_t* p_access )
35     {
36         p_access->p_sys->p_bda_module = new BDAGraph( p_access );
37     };
38
39     void dvb_deleteBDAGraph( access_t* p_access )
40     {
41         if( p_access->p_sys->p_bda_module )
42             delete p_access->p_sys->p_bda_module;
43     };
44
45     int dvb_SubmitATSCTuneRequest( access_t* p_access )
46     {
47         if( p_access->p_sys->p_bda_module )
48             return p_access->p_sys->p_bda_module->SubmitATSCTuneRequest();
49         return VLC_EGENERIC;
50     };
51
52     int dvb_SubmitDVBTTuneRequest( access_t* p_access )
53     {
54         if( p_access->p_sys->p_bda_module )
55             return p_access->p_sys->p_bda_module->SubmitDVBTTuneRequest();
56         return VLC_EGENERIC;
57     };
58
59     int dvb_SubmitDVBCTuneRequest( access_t* p_access )
60     {
61         if( p_access->p_sys->p_bda_module )
62             return p_access->p_sys->p_bda_module->SubmitDVBCTuneRequest();
63         return VLC_EGENERIC;
64     };
65
66     int dvb_SubmitDVBSTuneRequest( access_t* p_access )
67     {
68         if( p_access->p_sys->p_bda_module )
69             return p_access->p_sys->p_bda_module->SubmitDVBSTuneRequest();
70         return VLC_EGENERIC;
71     };
72
73     long dvb_GetBufferSize( access_t* p_access )
74     {
75         if( p_access->p_sys->p_bda_module )
76             return p_access->p_sys->p_bda_module->GetBufferSize();
77         return -1;
78     };
79
80     long dvb_ReadBuffer( access_t* p_access, long* l_buffer_len, BYTE* p_buff )
81     {
82         if( p_access->p_sys->p_bda_module )
83             return p_access->p_sys->p_bda_module->ReadBuffer( l_buffer_len,
84                 p_buff );
85         return -1;
86     };
87 };
88
89 /*****************************************************************************
90 * Constructor
91 *****************************************************************************/
92 BDAGraph::BDAGraph( access_t* p_this ):
93     p_access( p_this ),
94     guid_network_type(GUID_NULL),
95     l_tuner_used(-1),
96     d_graph_register( 0 )
97 {
98     b_ready = FALSE;
99     p_tuning_space = NULL;
100     p_tune_request = NULL;
101     p_media_control = NULL;
102     p_filter_graph = NULL;
103     p_system_dev_enum = NULL;
104     p_network_provider = p_tuner_device = p_capture_device = NULL;
105     p_sample_grabber = p_mpeg_demux = p_transport_info = NULL;
106     p_scanning_tuner = NULL;
107     p_grabber = NULL;
108
109     /* Initialize COM - MS says to use CoInitializeEx in preference to
110      * CoInitialize */
111     CoInitializeEx( 0, COINIT_APARTMENTTHREADED );
112 }
113
114 /*****************************************************************************
115 * Destructor
116 *****************************************************************************/
117 BDAGraph::~BDAGraph()
118 {
119     Destroy();
120     CoUninitialize();
121 }
122
123 /*****************************************************************************
124 * Submit an ATSC Tune Request
125 *****************************************************************************/
126 int BDAGraph::SubmitATSCTuneRequest()
127 {
128     HRESULT hr = S_OK;
129     class localComPtr
130     {
131         public:
132         IATSCChannelTuneRequest* p_atsc_tune_request;
133         IATSCLocator* p_atsc_locator;
134         localComPtr(): p_atsc_tune_request(NULL), p_atsc_locator(NULL) {};
135         ~localComPtr()
136         {
137             if( p_atsc_tune_request )
138                 p_atsc_tune_request->Release();
139             if( p_atsc_locator )
140                 p_atsc_locator->Release();
141         }
142     } l;
143     long l_major_channel, l_minor_channel, l_physical_channel;
144
145     l_major_channel = l_minor_channel = l_physical_channel = -1;
146 /*
147     l_major_channel = var_GetInteger( p_access, "dvb-major-channel" );
148     l_minor_channel = var_GetInteger( p_access, "dvb-minor-channel" );
149     l_physical_channel = var_GetInteger( p_access, "dvb-physical-channel" );
150 */
151
152     guid_network_type = CLSID_ATSCNetworkProvider;
153     hr = CreateTuneRequest();
154     if( FAILED( hr ) )
155     {
156         msg_Warn( p_access, "SubmitATSCTuneRequest: "\
157             "Cannot create Tuning Space: hr=0x%8lx", hr );
158         return VLC_EGENERIC;
159     }
160
161     hr = p_tune_request->QueryInterface( IID_IATSCChannelTuneRequest,
162         (void**)&l.p_atsc_tune_request );
163     if( FAILED( hr ) )
164     {
165         msg_Warn( p_access, "SubmitATSCTuneRequest: "\
166             "Cannot QI for IATSCChannelTuneRequest: hr=0x%8lx", hr );
167         return VLC_EGENERIC;
168     }
169     hr = ::CoCreateInstance( CLSID_ATSCLocator, 0, CLSCTX_INPROC,
170                              IID_IATSCLocator, (void**)&l.p_atsc_locator );
171     if( FAILED( hr ) )
172     {
173         msg_Warn( p_access, "SubmitATSCTuneRequest: "\
174             "Cannot create the ATSC locator: hr=0x%8lx", hr );
175         return VLC_EGENERIC;
176     }
177
178     hr = S_OK;
179     if( l_major_channel > 0 )
180         hr = l.p_atsc_tune_request->put_Channel( l_major_channel );
181     if( SUCCEEDED( hr ) && l_minor_channel > 0 )
182         hr = l.p_atsc_tune_request->put_MinorChannel( l_minor_channel );
183     if( SUCCEEDED( hr ) && l_physical_channel > 0 )
184         hr = l.p_atsc_locator->put_PhysicalChannel( l_physical_channel );
185     if( FAILED( hr ) )
186     {
187         msg_Warn( p_access, "SubmitATSCTuneRequest: "\
188             "Cannot set tuning parameters: hr=0x%8lx", hr );
189         return VLC_EGENERIC;
190     }
191
192     hr = p_tune_request->put_Locator( l.p_atsc_locator );
193     if( FAILED( hr ) )
194     {
195         msg_Warn( p_access, "SubmitATSCTuneRequest: "\
196             "Cannot put the locator: hr=0x%8lx", hr );
197         return VLC_EGENERIC;
198     }
199
200     /* Build and Run the Graph. If a Tuner device is in use the graph will
201      * fail to run. Repeated calls to build will check successive tuner
202      * devices */
203     do
204     {
205         hr = Build();
206         if( FAILED( hr ) )
207         {
208             msg_Warn( p_access, "SubmitATSCTuneRequest: "\
209                 "Cannot Build the Graph: hr=0x%8lx", hr );
210             return VLC_EGENERIC;
211         }
212         hr = Start();
213     }
214     while( hr != S_OK );
215
216     return VLC_SUCCESS;
217 }
218
219 /*****************************************************************************
220 * Submit a DVB-T Tune Request
221 ******************************************************************************/
222 int BDAGraph::SubmitDVBTTuneRequest()
223 {
224     HRESULT hr = S_OK;
225     class localComPtr
226     {
227         public:
228         IDVBTuneRequest* p_dvbt_tune_request;
229         IDVBTLocator* p_dvbt_locator;
230         localComPtr(): p_dvbt_tune_request(NULL), p_dvbt_locator(NULL) {};
231         ~localComPtr()
232         {
233             if( p_dvbt_tune_request )
234                 p_dvbt_tune_request->Release();
235             if( p_dvbt_locator )
236                 p_dvbt_locator->Release();
237         }
238     } l;
239     long l_frequency, l_bandwidth, l_hp_fec, l_lp_fec, l_guard;
240     long l_transmission, l_hierarchy;
241     BinaryConvolutionCodeRate i_hp_fec, i_lp_fec;
242     GuardInterval             i_guard;
243     TransmissionMode          i_transmission;
244     HierarchyAlpha            i_hierarchy;
245
246     l_frequency = l_bandwidth = l_hp_fec = l_lp_fec = l_guard = -1;
247     l_transmission = l_hierarchy = -1;
248     l_frequency = var_GetInteger( p_access, "dvb-frequency" );
249     l_bandwidth = var_GetInteger( p_access, "dvb-bandwidth" );
250     l_hp_fec = var_GetInteger( p_access, "dvb-code-rate-hp" );
251     l_lp_fec = var_GetInteger( p_access, "dvb-code-rate-lp" );
252     l_guard = var_GetInteger( p_access, "dvb-guard" );
253     l_transmission = var_GetInteger( p_access, "dvb-transmission" );
254     l_hierarchy = var_GetInteger( p_access, "dvb-hierarchy" );
255
256     i_hp_fec = BDA_BCC_RATE_NOT_SET;
257     if( l_hp_fec == 1 )
258         i_hp_fec = BDA_BCC_RATE_1_2;
259     if( l_hp_fec == 2 )
260         i_hp_fec = BDA_BCC_RATE_2_3;
261     if( l_hp_fec == 3 )
262         i_hp_fec = BDA_BCC_RATE_3_4;
263     if( l_hp_fec == 4 )
264         i_hp_fec = BDA_BCC_RATE_5_6;
265     if( l_hp_fec == 5 )
266         i_hp_fec = BDA_BCC_RATE_7_8;
267
268     i_lp_fec = BDA_BCC_RATE_NOT_SET;
269     if( l_lp_fec == 1 )
270         i_lp_fec = BDA_BCC_RATE_1_2;
271     if( l_lp_fec == 2 )
272         i_lp_fec = BDA_BCC_RATE_2_3;
273     if( l_lp_fec == 3 )
274         i_lp_fec = BDA_BCC_RATE_3_4;
275     if( l_lp_fec == 4 )
276         i_lp_fec = BDA_BCC_RATE_5_6;
277     if( l_lp_fec == 5 )
278         i_lp_fec = BDA_BCC_RATE_7_8;
279
280     i_guard = BDA_GUARD_NOT_SET;
281     if( l_guard == 32 )
282         i_guard = BDA_GUARD_1_32;
283     if( l_guard == 16 )
284         i_guard = BDA_GUARD_1_16;
285     if( l_guard == 8 )
286         i_guard = BDA_GUARD_1_8;
287     if( l_guard == 4 )
288         i_guard = BDA_GUARD_1_4;
289
290     i_transmission = BDA_XMIT_MODE_NOT_SET;
291     if( l_transmission == 2 )
292         i_transmission = BDA_XMIT_MODE_2K;
293     if( l_transmission == 8 )
294         i_transmission = BDA_XMIT_MODE_8K;
295
296     i_hierarchy = BDA_HALPHA_NOT_SET;
297     if( l_hierarchy == 1 )
298         i_hierarchy = BDA_HALPHA_1;
299     if( l_hierarchy == 2 )
300         i_hierarchy = BDA_HALPHA_2;
301     if( l_hierarchy == 4 )
302         i_hierarchy = BDA_HALPHA_4;
303
304     guid_network_type = CLSID_DVBTNetworkProvider;
305     hr = CreateTuneRequest();
306     if( FAILED( hr ) )
307     {
308         msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
309             "Cannot create Tune Request: hr=0x%8lx", hr );
310         return VLC_EGENERIC;
311     }
312
313     hr = p_tune_request->QueryInterface( IID_IDVBTuneRequest,
314         (void**)&l.p_dvbt_tune_request );
315     if( FAILED( hr ) )
316     {
317         msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
318             "Cannot QI for IDVBTuneRequest: hr=0x%8lx", hr );
319         return VLC_EGENERIC;
320     }
321     l.p_dvbt_tune_request->put_ONID( -1 );
322     l.p_dvbt_tune_request->put_SID( -1 );
323     l.p_dvbt_tune_request->put_TSID( -1 );
324
325     hr = ::CoCreateInstance( CLSID_DVBTLocator, 0, CLSCTX_INPROC,
326         IID_IDVBTLocator, (void**)&l.p_dvbt_locator );
327     if( FAILED( hr ) )
328     {
329         msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
330             "Cannot create the DVBT Locator: hr=0x%8lx", hr );
331         return VLC_EGENERIC;
332     }
333
334     hr = S_OK;
335     if( l_frequency > 0 )
336         hr = l.p_dvbt_locator->put_CarrierFrequency( l_frequency );
337     if( SUCCEEDED( hr ) && l_bandwidth > 0 )
338         hr = l.p_dvbt_locator->put_Bandwidth( l_bandwidth );
339     if( SUCCEEDED( hr ) && i_hp_fec != BDA_BCC_RATE_NOT_SET )
340         hr = l.p_dvbt_locator->put_InnerFECRate( i_hp_fec );
341     if( SUCCEEDED( hr ) && i_lp_fec != BDA_BCC_RATE_NOT_SET )
342         hr = l.p_dvbt_locator->put_LPInnerFECRate( i_lp_fec );
343     if( SUCCEEDED( hr ) && i_guard != BDA_GUARD_NOT_SET )
344         hr = l.p_dvbt_locator->put_Guard( i_guard );
345     if( SUCCEEDED( hr ) && i_transmission != BDA_XMIT_MODE_NOT_SET )
346         hr = l.p_dvbt_locator->put_Mode( i_transmission );
347     if( SUCCEEDED( hr ) && i_hierarchy != BDA_HALPHA_NOT_SET )
348         hr = l.p_dvbt_locator->put_HAlpha( i_hierarchy );
349     if( FAILED( hr ) )
350     {
351         msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
352             "Cannot set tuning parameters on Locator: hr=0x%8lx", hr );
353         return VLC_EGENERIC;
354     }
355
356     hr = p_tune_request->put_Locator( l.p_dvbt_locator );
357     if( FAILED( hr ) )
358     {
359         msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
360             "Cannot put the locator: hr=0x%8lx", hr );
361         return VLC_EGENERIC;
362     }
363
364     /* Build and Run the Graph. If a Tuner device is in use the graph will
365      * fail to run. Repeated calls to build will check successive tuner
366      * devices */
367     do
368     {
369         hr = Build();
370         if( FAILED( hr ) )
371         {
372             msg_Warn( p_access, "SubmitDVBTTuneRequest: "\
373                 "Cannot Build the Graph: hr=0x%8lx", hr );
374             return VLC_EGENERIC;
375         }
376         hr = Start();
377     }
378     while( hr != S_OK );
379
380     return VLC_SUCCESS;
381 }
382
383 /*****************************************************************************
384 * Submit a DVB-C Tune Request
385 ******************************************************************************/
386 int BDAGraph::SubmitDVBCTuneRequest()
387 {
388     HRESULT hr = S_OK;
389
390     class localComPtr
391     {
392         public:
393         IDVBTuneRequest* p_dvbc_tune_request;
394         IDVBCLocator* p_dvbc_locator;
395         localComPtr(): p_dvbc_tune_request(NULL), p_dvbc_locator(NULL) {};
396         ~localComPtr()
397         {
398             if( p_dvbc_tune_request )
399                 p_dvbc_tune_request->Release();
400             if( p_dvbc_locator )
401                 p_dvbc_locator->Release();
402         }
403     } l;
404
405     long l_frequency, l_symbolrate;
406     int  i_qam;
407     ModulationType i_qam_mod;
408
409     l_frequency = l_symbolrate = i_qam = -1;
410     l_frequency = var_GetInteger( p_access, "dvb-frequency" );
411     l_symbolrate = var_GetInteger( p_access, "dvb-srate" );
412     i_qam = var_GetInteger( p_access, "dvb-modulation" );
413     i_qam_mod = BDA_MOD_NOT_SET;
414     if( i_qam == 16 )
415         i_qam_mod = BDA_MOD_16QAM;
416     if( i_qam == 32 )
417         i_qam_mod = BDA_MOD_32QAM;
418     if( i_qam == 64 )
419         i_qam_mod = BDA_MOD_64QAM;
420     if( i_qam == 128 )
421         i_qam_mod = BDA_MOD_128QAM;
422     if( i_qam == 256 )
423         i_qam_mod = BDA_MOD_256QAM;
424
425     guid_network_type = CLSID_DVBCNetworkProvider;
426     hr = CreateTuneRequest();
427     if( FAILED( hr ) )
428     {
429         msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
430             "Cannot create Tune Request: hr=0x%8lx", hr );
431         return VLC_EGENERIC;
432     }
433
434     hr = p_tune_request->QueryInterface( IID_IDVBTuneRequest,
435         (void**)&l.p_dvbc_tune_request );
436     if( FAILED( hr ) )
437     {
438         msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
439             "Cannot QI for IDVBTuneRequest: hr=0x%8lx", hr );
440         return VLC_EGENERIC;
441     }
442     l.p_dvbc_tune_request->put_ONID( -1 );
443     l.p_dvbc_tune_request->put_SID( -1 );
444     l.p_dvbc_tune_request->put_TSID( -1 );
445
446     hr = ::CoCreateInstance( CLSID_DVBCLocator, 0, CLSCTX_INPROC,
447         IID_IDVBCLocator, (void**)&l.p_dvbc_locator );
448     if( FAILED( hr ) )
449     {
450         msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
451             "Cannot create the DVB-C Locator: hr=0x%8lx", hr );
452         return VLC_EGENERIC;
453     }
454
455     hr = S_OK;
456     if( l_frequency > 0 )
457         hr = l.p_dvbc_locator->put_CarrierFrequency( l_frequency );
458     if( SUCCEEDED( hr ) && l_symbolrate > 0 )
459         hr = l.p_dvbc_locator->put_SymbolRate( l_symbolrate );
460     if( SUCCEEDED( hr ) && i_qam_mod != BDA_MOD_NOT_SET )
461         hr = l.p_dvbc_locator->put_Modulation( i_qam_mod );
462     if( FAILED( hr ) )
463     {
464         msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
465             "Cannot set tuning parameters on Locator: hr=0x%8lx", hr );
466         return VLC_EGENERIC;
467     }
468
469     hr = p_tune_request->put_Locator( l.p_dvbc_locator );
470     if( FAILED( hr ) )
471     {
472         msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
473             "Cannot put the locator: hr=0x%8lx", hr );
474         return VLC_EGENERIC;
475     }
476
477     /* Build and Run the Graph. If a Tuner device is in use the graph will
478      * fail to run. Repeated calls to build will check successive tuner
479      * devices */
480     do
481     {
482         hr = Build();
483         if( FAILED( hr ) )
484         {
485             msg_Warn( p_access, "SubmitDVBCTuneRequest: "\
486                 "Cannot Build the Graph: hr=0x%8lx", hr );
487             return VLC_EGENERIC;
488         }
489         hr = Start();
490     }
491     while( hr != S_OK );
492
493     return VLC_SUCCESS;
494 }
495
496 /*****************************************************************************
497 * Submit a DVB-S Tune Request
498 ******************************************************************************/
499 int BDAGraph::SubmitDVBSTuneRequest()
500 {
501     HRESULT hr = S_OK;
502
503     class localComPtr
504     {
505         public:
506         IDVBTuneRequest* p_dvbs_tune_request;
507         IDVBSLocator* p_dvbs_locator;
508         IDVBSTuningSpace* p_dvbs_tuning_space;
509         localComPtr(): p_dvbs_tune_request(NULL), p_dvbs_locator(NULL),
510             p_dvbs_tuning_space(NULL) {};
511         ~localComPtr()
512         {
513             if( p_dvbs_tuning_space )
514                 p_dvbs_tuning_space->Release();
515             if( p_dvbs_tune_request )
516                 p_dvbs_tune_request->Release();
517             if( p_dvbs_locator )
518                 p_dvbs_locator->Release();
519         }
520     } l;
521     long l_frequency, l_symbolrate, l_azimuth, l_elevation, l_longitude;
522     long l_lnb_lof1, l_lnb_lof2, l_lnb_slof, l_inversion, l_network_id;
523     char* psz_polarisation;
524     Polarisation i_polar;
525     SpectralInversion i_inversion;
526     VARIANT_BOOL b_west;
527
528     l_frequency = l_symbolrate = l_azimuth = l_elevation = l_longitude = -1;
529     l_lnb_lof1 = l_lnb_lof2 = l_lnb_slof = l_inversion = l_network_id = -1;
530     l_frequency = var_GetInteger( p_access, "dvb-frequency" );
531     l_symbolrate = var_GetInteger( p_access, "dvb-srate" );
532     l_azimuth = var_GetInteger( p_access, "dvb-azimuth" );
533     l_elevation = var_GetInteger( p_access, "dvb-elevation" );
534     l_longitude = var_GetInteger( p_access, "dvb-longitude" );
535     l_lnb_lof1 = var_GetInteger( p_access, "dvb-lnb-lof1" );
536     l_lnb_lof2 = var_GetInteger( p_access, "dvb-lnb-lof2" );
537     l_lnb_slof = var_GetInteger( p_access, "dvb-lnb-slof" );
538     psz_polarisation = var_GetNonEmptyString( p_access, "dvb-polarisation" );
539     l_inversion = var_GetInteger( p_access, "dvb-inversion" );
540     l_network_id = var_GetInteger( p_access, "dvb-network_id" );
541
542     b_west = ( l_longitude < 0 ) ? TRUE : FALSE;
543
544     i_polar = BDA_POLARISATION_NOT_SET;
545     if( psz_polarisation != NULL )
546         switch( toupper( psz_polarisation[0] ) )
547         {
548             case 'H':
549                 i_polar = BDA_POLARISATION_LINEAR_H;
550                 break;
551             case 'V':
552                 i_polar = BDA_POLARISATION_LINEAR_V;
553                 break;
554             case 'L':
555                 i_polar = BDA_POLARISATION_CIRCULAR_L;
556                 break;
557             case 'R':
558                 i_polar = BDA_POLARISATION_CIRCULAR_R;
559                 break;
560         }
561
562     i_inversion = BDA_SPECTRAL_INVERSION_NOT_SET;
563     if( l_inversion == 0 )
564         i_inversion = BDA_SPECTRAL_INVERSION_NORMAL;
565     if( l_inversion == 1 )
566         i_inversion = BDA_SPECTRAL_INVERSION_INVERTED;
567     if( l_inversion == 2 )
568         i_inversion = BDA_SPECTRAL_INVERSION_AUTOMATIC;
569
570     guid_network_type = CLSID_DVBSNetworkProvider;
571     hr = CreateTuneRequest();
572     if( FAILED( hr ) )
573     {
574         msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
575             "Cannot create Tune Request: hr=0x%8lx", hr );
576         return VLC_EGENERIC;
577     }
578
579     hr = p_tune_request->QueryInterface( IID_IDVBTuneRequest,
580         (void**)&l.p_dvbs_tune_request );
581     if( FAILED( hr ) )
582     {
583         msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
584             "Cannot QI for IDVBTuneRequest: hr=0x%8lx", hr );
585         return VLC_EGENERIC;
586     }
587     l.p_dvbs_tune_request->put_ONID( -1 );
588     l.p_dvbs_tune_request->put_SID( -1 );
589     l.p_dvbs_tune_request->put_TSID( -1 );
590
591     hr = ::CoCreateInstance( CLSID_DVBSLocator, 0, CLSCTX_INPROC,
592         IID_IDVBSLocator, (void**)&l.p_dvbs_locator );
593     if( FAILED( hr ) )
594     {
595         msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
596             "Cannot create the DVBS Locator: hr=0x%8lx", hr );
597         return VLC_EGENERIC;
598     }
599
600     hr = p_tuning_space->QueryInterface( IID_IDVBSTuningSpace,
601         (void**)&l.p_dvbs_tuning_space );
602     if( FAILED( hr ) )
603     {
604         msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
605             "Cannot QI for IDVBSTuningSpace: hr=0x%8lx", hr );
606         return VLC_EGENERIC;
607     }
608
609     hr = S_OK;
610     if( l_frequency > 0 )
611         hr = l.p_dvbs_locator->put_CarrierFrequency( l_frequency );
612     if( SUCCEEDED( hr ) && l_symbolrate > 0 )
613         hr = l.p_dvbs_locator->put_SymbolRate( l_symbolrate );
614     if( SUCCEEDED( hr ) && l_azimuth > 0 )
615         hr = l.p_dvbs_locator->put_Azimuth( l_azimuth );
616     if( SUCCEEDED( hr ) && l_elevation > 0 )
617         hr = l.p_dvbs_locator->put_Elevation( l_elevation );
618     if( SUCCEEDED( hr ) )
619         hr = l.p_dvbs_locator->put_OrbitalPosition( labs( l_longitude ) );
620     if( SUCCEEDED( hr ) )
621         hr = l.p_dvbs_locator->put_WestPosition( b_west );
622     if( SUCCEEDED( hr ) && i_polar != BDA_POLARISATION_NOT_SET )
623         hr = l.p_dvbs_locator->put_SignalPolarisation( i_polar );
624     if( SUCCEEDED( hr ) && l_lnb_lof1 > 0 )
625         hr = l.p_dvbs_tuning_space->put_LowOscillator( l_lnb_lof1 );
626     if( SUCCEEDED( hr ) && l_lnb_lof2 > 0 )
627         hr = l.p_dvbs_tuning_space->put_HighOscillator( l_lnb_lof2 );
628     if( SUCCEEDED( hr ) && l_lnb_slof > 0 )
629         hr = l.p_dvbs_tuning_space->put_LNBSwitch( l_lnb_slof );
630     if( SUCCEEDED( hr ) && i_inversion != BDA_SPECTRAL_INVERSION_NOT_SET )
631         hr = l.p_dvbs_tuning_space->put_SpectralInversion( i_inversion );
632     if( SUCCEEDED( hr ) && l_network_id > 0 )
633         hr = l.p_dvbs_tuning_space->put_NetworkID( l_network_id );
634     if( FAILED( hr ) )
635     {
636         msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
637             "Cannot set tuning parameters on Locator: hr=0x%8lx", hr );
638         return VLC_EGENERIC;
639     }
640
641     hr = p_tune_request->put_Locator( l.p_dvbs_locator );
642     if( FAILED( hr ) )
643     {
644         msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
645             "Cannot put the locator: hr=0x%8lx", hr );
646         return VLC_EGENERIC;
647     }
648
649     /* Build and Run the Graph. If a Tuner device is in use the graph will
650      * fail to run. Repeated calls to build will check successive tuner
651      * devices */
652     do
653     {
654         hr = Build();
655         if( FAILED( hr ) )
656         {
657             msg_Warn( p_access, "SubmitDVBSTuneRequest: "\
658                 "Cannot Build the Graph: hr=0x%8lx", hr );
659             return VLC_EGENERIC;
660         }
661         hr = Start();
662     }
663     while( hr != S_OK );
664
665     return VLC_SUCCESS;
666 }
667
668 /*****************************************************************************
669 * Load the Tuning Space from System Tuning Spaces according to the
670 * Network Type requested
671 ******************************************************************************/
672 HRESULT BDAGraph::CreateTuneRequest()
673 {
674     HRESULT hr = S_OK;
675     GUID guid_this_network_type;
676     class localComPtr
677     {
678         public:
679         ITuningSpaceContainer*  p_tuning_space_container;
680         IEnumTuningSpaces*      p_tuning_space_enum;
681         ITuningSpace*           p_this_tuning_space;
682         localComPtr(): p_tuning_space_container(NULL),
683             p_tuning_space_enum(NULL), p_this_tuning_space(NULL) {};
684         ~localComPtr()
685         {
686             if( p_tuning_space_container )
687                 p_tuning_space_container->Release();
688             if( p_tuning_space_enum )
689                 p_tuning_space_enum->Release();
690             if( p_this_tuning_space )
691                 p_this_tuning_space->Release();
692         }
693     } l;
694
695     /* A Tuning Space may already have been set up. If it is for the same
696      * network type then all is well. Otherwise, reset the Tuning Space and get
697      * a new one */
698     if( p_tuning_space )
699     {
700         hr = p_tuning_space->get__NetworkType( &guid_this_network_type );
701         if( FAILED( hr ) ) guid_this_network_type = GUID_NULL;
702         if( guid_this_network_type == guid_network_type )
703         {
704             return S_OK;
705         }
706         else
707         {
708             p_tuning_space->Release();
709             p_tuning_space = NULL;
710         }
711     }
712
713     /* Force use of the first available Tuner Device during Build */
714     l_tuner_used = -1;
715
716     /* Get the SystemTuningSpaces container to enumerate through all the
717      * defined tuning spaces */
718     hr = ::CoCreateInstance( CLSID_SystemTuningSpaces, 0, CLSCTX_INPROC,
719         IID_ITuningSpaceContainer, (void**)&l.p_tuning_space_container );
720     if( FAILED( hr ) )
721     {
722         msg_Warn( p_access, "CreateTuneRequest: "\
723             "Cannot CoCreate SystemTuningSpaces: hr=0x%8lx", hr );
724         return hr;
725     }
726
727     hr = l.p_tuning_space_container->get_EnumTuningSpaces(
728          &l.p_tuning_space_enum );
729     if( FAILED( hr ) )
730     {
731         msg_Warn( p_access, "CreateTuneRequest: "\
732             "Cannot create SystemTuningSpaces Enumerator: hr=0x%8lx", hr );
733         return hr;
734     }
735
736     while( l.p_tuning_space_enum->Next( 1, &l.p_this_tuning_space, NULL ) ==
737         S_OK )
738     {
739        hr = l.p_this_tuning_space->get__NetworkType( &guid_this_network_type );
740
741         /* GUID_NULL means a non-BDA network was found e.g analog
742          * Ignore failures and non-BDA networks and keep looking */
743         if( FAILED( hr ) ) guid_this_network_type == GUID_NULL;
744
745         if( guid_this_network_type == guid_network_type )
746         {
747             hr = l.p_this_tuning_space->QueryInterface( IID_ITuningSpace,
748                 (void**)&p_tuning_space );
749             if( FAILED( hr ) )
750             {
751                 msg_Warn( p_access, "CreateTuneRequest: "\
752                     "Cannot QI Tuning Space: hr=0x%8lx", hr );
753                 return hr;
754             }
755             hr = p_tuning_space->CreateTuneRequest( &p_tune_request );
756             if( FAILED( hr ) )
757             {
758                 msg_Warn( p_access, "CreateTuneRequest: "\
759                     "Cannot Create Tune Request: hr=0x%8lx", hr );
760                 return hr;
761             }
762             return hr;
763         }
764     }
765     hr = E_FAIL;
766     if( FAILED( hr ) )
767     {
768         msg_Warn( p_access, "CreateTuneRequest: "\
769             "Cannot find a suitable System Tuning Space: hr=0x%8lx", hr );
770         return hr;
771     }
772     return hr;
773 }
774
775 /******************************************************************************
776 * Build
777 * Step 4: Build the Filter Graph
778 * Build sets up devices, adds and connects filters
779 ******************************************************************************/
780 HRESULT BDAGraph::Build()
781 {
782     HRESULT hr = S_OK;
783     long l_capture_used, l_tif_used;
784     AM_MEDIA_TYPE grabber_media_type;
785
786     /* If we have already have a filter graph, rebuild it*/
787     Destroy();
788
789     hr = ::CoCreateInstance( CLSID_FilterGraph, NULL, CLSCTX_INPROC,
790         IID_IGraphBuilder, (void**)&p_filter_graph );
791     if( FAILED( hr ) )
792     {
793         msg_Warn( p_access, "Build: "\
794             "Cannot CoCreate IFilterGraph: hr=0x%8lx", hr );
795         return hr;
796     }
797
798     /* First filter in the graph is the Network Provider and
799      * its Scanning Tuner which takes the Tune Request*/
800     hr = ::CoCreateInstance( guid_network_type, NULL, CLSCTX_INPROC_SERVER,
801         IID_IBaseFilter, (void**)&p_network_provider);
802     if( FAILED( hr ) )
803     {
804         msg_Warn( p_access, "Build: "\
805             "Cannot CoCreate Network Provider: hr=0x%8lx", hr );
806         return hr;
807     }
808     hr = p_filter_graph->AddFilter( p_network_provider, L"Network Provider" );
809     if( FAILED( hr ) )
810     {
811         msg_Warn( p_access, "Build: "\
812             "Cannot load network provider: hr=0x%8lx", hr );
813         return hr;
814     }
815
816     hr = p_network_provider->QueryInterface( IID_IScanningTuner,
817         (void**)&p_scanning_tuner );
818     if( FAILED( hr ) )
819     {
820         msg_Warn( p_access, "Build: "\
821             "Cannot QI Network Provider for Scanning Tuner: hr=0x%8lx", hr );
822         return hr;
823     }
824
825     hr = p_scanning_tuner->Validate( p_tune_request );
826     if( FAILED( hr ) )
827     {
828         msg_Warn( p_access, "Build: "\
829             "Tune Request is invalid: hr=0x%8lx", hr );
830         return hr;
831     }
832     hr = p_scanning_tuner->put_TuneRequest( p_tune_request );
833     if( FAILED( hr ) )
834     {
835         msg_Warn( p_access, "Build: "\
836             "Cannot submit the tune request: hr=0x%8lx", hr );
837         return hr;
838     }
839
840     /* Add the Network Tuner to the Network Provider. On subsequent calls,
841      * l_tuner_used will cause a different tuner to be selected */
842     hr = FindFilter( KSCATEGORY_BDA_NETWORK_TUNER, &l_tuner_used,
843         p_network_provider, &p_tuner_device );
844     if( FAILED( hr ) )
845     {
846         msg_Warn( p_access, "Build: "\
847             "Cannot load tuner device and connect network provider: "\
848             "hr=0x%8lx", hr );
849         return hr;
850     }
851
852     /* Always look for all capture devices to match the Network Tuner */
853     l_capture_used = -1;
854     hr = FindFilter( KSCATEGORY_BDA_RECEIVER_COMPONENT, &l_capture_used,
855         p_tuner_device, &p_capture_device );
856     if( FAILED( hr ) )
857     {
858         /* Some BDA drivers do not provide a Capture Device Filter so force
859          * the Sample Grabber to connect directly to the Tuner Device */
860         p_capture_device = p_tuner_device;
861         p_tuner_device = NULL;
862         msg_Warn( p_access, "Build: "\
863             "Cannot find Capture device. Connecting to tuner: hr=0x%8lx", hr );
864     }
865
866     /* Insert the Sample Grabber to tap into the Transport Stream. */
867     hr = ::CoCreateInstance( CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
868         IID_IBaseFilter, (void**)&p_sample_grabber );
869     if( FAILED( hr ) )
870     {
871         msg_Warn( p_access, "Build: "\
872             "Cannot load Sample Grabber Filter: hr=0x%8lx", hr );
873         return hr;
874     }
875     hr = p_filter_graph->AddFilter( p_sample_grabber, L"Sample Grabber" );
876     if( FAILED( hr ) )
877     {
878         msg_Warn( p_access, "Build: "\
879             "Cannot add Sample Grabber Filter to graph: hr=0x%8lx", hr );
880         return hr;
881     }
882
883     hr = p_sample_grabber->QueryInterface( IID_ISampleGrabber,
884         (void**)&p_grabber );
885     if( FAILED( hr ) )
886     {
887         msg_Warn( p_access, "Build: "\
888             "Cannot QI Sample Grabber Filter: hr=0x%8lx", hr );
889         return hr;
890     }
891
892     ZeroMemory( &grabber_media_type, sizeof( AM_MEDIA_TYPE ) );
893     grabber_media_type.majortype == MEDIATYPE_Stream;
894     grabber_media_type.subtype == MEDIASUBTYPE_MPEG2_TRANSPORT;
895     hr = p_grabber->SetMediaType( &grabber_media_type );
896     if( FAILED( hr ) )
897     {
898         msg_Warn( p_access, "Build: "\
899             "Cannot set media type on grabber filter: hr=0x%8lx", hr );
900         return hr;
901     }
902     hr = Connect( p_capture_device, p_sample_grabber );
903     if( FAILED( hr ) )
904     {
905         msg_Warn( p_access, "Build: "\
906             "Cannot connect Sample Grabber to Capture device: hr=0x%8lx", hr );
907         return hr;
908     }
909
910     /* We need the MPEG2 Demultiplexer even though we are going to use the VLC
911      * TS demuxer. The TIF filter connects to the MPEG2 demux and works with
912      * the Network Provider filter to set up the stream */
913     hr = ::CoCreateInstance( CLSID_MPEG2Demultiplexer, NULL,
914         CLSCTX_INPROC_SERVER, IID_IBaseFilter, (void**)&p_mpeg_demux );
915     if( FAILED( hr ) )
916     {
917         msg_Warn( p_access, "Build: "\
918             "Cannot CoCreateInstance MPEG2 Demultiplexer: hr=0x%8lx", hr );
919         return hr;
920     }
921     hr = p_filter_graph->AddFilter( p_mpeg_demux, L"Demux" );
922     if( FAILED( hr ) )
923     {
924         msg_Warn( p_access, "Build: "\
925             "Cannot add demux filter to graph: hr=0x%8lx", hr );
926         return hr;
927     }
928
929     hr = Connect( p_sample_grabber, p_mpeg_demux );
930     if( FAILED( hr ) )
931     {
932         msg_Warn( p_access, "Build: "\
933             "Cannot connect demux to grabber: hr=0x%8lx", hr );
934         return hr;
935     }
936
937     /* Always look for the Transform Information Filter from the start
938      * of the collection*/
939     l_tif_used = -1;
940     hr = FindFilter( KSCATEGORY_BDA_TRANSPORT_INFORMATION, &l_tif_used,
941         p_mpeg_demux, &p_transport_info );
942     if( FAILED( hr ) )
943     {
944         msg_Warn( p_access, "Build: "\
945             "Cannot