1 /*****************************************************************************
2 * dshow.cpp : DirectShow access and access_demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2004, 2006, 2008, 2010 the VideoLAN team
7 * Author: Gildas Bazin <gbazin@videolan.org>
8 * Damien Fouilleul <damienf@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #define __STDC_CONSTANT_MACROS 1
34 #define __STDC_FORMAT_MACROS 1
35 #define CFG_PREFIX "dshow-"
41 #include <vlc_common.h>
42 #include <vlc_plugin.h>
43 #include <vlc_access.h>
44 #include <vlc_demux.h>
46 #include <vlc_dialog.h> /* dialog_Fatal */
47 #include <vlc_charset.h> /* FromWide */
50 #include "vlc_dshow.h"
55 #define INSTANCEDATA_OF_PROPERTY_PTR(x) ((PKSPROPERTY((x))) + 1)
56 #define INSTANCEDATA_OF_PROPERTY_SIZE(x) (sizeof((x)) - sizeof(KSPROPERTY))
58 /*****************************************************************************
59 * Access: local prototypes
60 *****************************************************************************/
61 static block_t *ReadCompressed( access_t * );
62 static int AccessControl ( access_t *, int, va_list );
64 static int Demux ( demux_t * );
65 static int DemuxControl( demux_t *, int, va_list );
67 static int OpenDevice( vlc_object_t *, access_sys_t *, string, bool );
68 static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
69 list<string> *, bool );
70 static size_t EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
71 int, int, int, int, int, int,
72 AM_MEDIA_TYPE *mt, size_t );
73 static bool ConnectFilters( vlc_object_t *, access_sys_t *,
74 IBaseFilter *, CaptureFilter * );
75 static int FindDevices( vlc_object_t *, const char *, char ***, char *** );
76 static int ConfigDevicesCallback( vlc_object_t *, char const *,
77 vlc_value_t, vlc_value_t, void * );
79 static void ShowPropertyPage( IUnknown * );
80 static void ShowDeviceProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
81 IBaseFilter *, bool );
82 static void ShowTunerProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
83 IBaseFilter *, bool );
84 static void ConfigTuner( vlc_object_t *, ICaptureGraphBuilder2 *,
87 /*****************************************************************************
89 *****************************************************************************/
90 static const int pi_tuner_input[] = { 0, 1, 2 };
91 static const char *const ppsz_tuner_input_text[] =
92 {N_("Default"), N_("Cable"), N_("Antenna")};
94 static const int pi_amtuner_mode[] = { AMTUNER_MODE_DEFAULT,
96 AMTUNER_MODE_FM_RADIO,
97 AMTUNER_MODE_AM_RADIO,
99 static const char *const ppsz_amtuner_mode_text[] = { N_("Default"),
105 static const int i_standards_list[] =
108 KS_AnalogVideo_NTSC_M, KS_AnalogVideo_NTSC_M_J, KS_AnalogVideo_NTSC_433,
109 KS_AnalogVideo_PAL_B, KS_AnalogVideo_PAL_D, KS_AnalogVideo_PAL_G,
110 KS_AnalogVideo_PAL_H, KS_AnalogVideo_PAL_I, KS_AnalogVideo_PAL_M,
111 KS_AnalogVideo_PAL_N, KS_AnalogVideo_PAL_60,
112 KS_AnalogVideo_SECAM_B, KS_AnalogVideo_SECAM_D, KS_AnalogVideo_SECAM_G,
113 KS_AnalogVideo_SECAM_H, KS_AnalogVideo_SECAM_K, KS_AnalogVideo_SECAM_K1,
114 KS_AnalogVideo_SECAM_L, KS_AnalogVideo_SECAM_L1,
115 KS_AnalogVideo_PAL_N_COMBO
117 static const char *const ppsz_standards_list_text[] =
120 "NTSC_M", "NTSC_M_J", "NTSC_443",
121 "PAL_B", "PAL_D", "PAL_G",
122 "PAL_H", "PAL_I", "PAL_M",
124 "SECAM_B", "SECAM_D", "SECAM_G",
125 "SECAM_H", "SECAM_K", "SECAM_K1",
126 "SECAM_L", "SECAM_L1",
130 #define VDEV_TEXT N_("Video device name")
131 #define VDEV_LONGTEXT N_( \
132 "Name of the video device that will be used by the " \
133 "DirectShow plugin. If you don't specify anything, the default device " \
135 #define ADEV_TEXT N_("Audio device name")
136 #define ADEV_LONGTEXT N_( \
137 "Name of the audio device that will be used by the " \
138 "DirectShow plugin. If you don't specify anything, the default device " \
140 #define SIZE_TEXT N_("Video size")
141 #define SIZE_LONGTEXT N_( \
142 "Size of the video that will be displayed by the " \
143 "DirectShow plugin. If you don't specify anything the default size for " \
144 "your device will be used. You can specify a standard size (cif, d1, ...) or <width>x<height>.")
145 #define ASPECT_TEXT N_("Picture aspect-ratio n:m")
146 #define ASPECT_LONGTEXT N_("Define input picture aspect-ratio to use. Default is 4:3" )
147 #define CHROMA_TEXT N_("Video input chroma format")
148 #define CHROMA_LONGTEXT N_( \
149 "Force the DirectShow video input to use a specific chroma format " \
150 "(eg. I420 (default), RV24, etc.)")
151 #define FPS_TEXT N_("Video input frame rate")
152 #define FPS_LONGTEXT N_( \
153 "Force the DirectShow video input to use a specific frame rate " \
154 "(eg. 0 means default, 25, 29.97, 50, 59.94, etc.)")
155 #define CONFIG_TEXT N_("Device properties")
156 #define CONFIG_LONGTEXT N_( \
157 "Show the properties dialog of the selected device before starting the " \
159 #define TUNER_TEXT N_("Tuner properties")
160 #define TUNER_LONGTEXT N_( \
161 "Show the tuner properties [channel selection] page." )
162 #define CHANNEL_TEXT N_("Tuner TV Channel")
163 #define CHANNEL_LONGTEXT N_( \
164 "Set the TV channel the tuner will set to " \
165 "(0 means default)." )
166 #define TVFREQ_TEXT N_("Tuner Frequency")
167 #define TVFREQ_LONGTEXT N_( "This overrides the channel. Measured in Hz." )
168 #define STANDARD_TEXT N_( "Video standard" )
169 #define COUNTRY_TEXT N_("Tuner country code")
170 #define COUNTRY_LONGTEXT N_( \
171 "Set the tuner country code that establishes the current " \
172 "channel-to-frequency mapping (0 means default)." )
173 #define TUNER_INPUT_TEXT N_("Tuner input type")
174 #define TUNER_INPUT_LONGTEXT N_( \
175 "Select the tuner input type (Cable/Antenna)." )
176 #define VIDEO_IN_TEXT N_("Video input pin")
177 #define VIDEO_IN_LONGTEXT N_( \
178 "Select the video input source, such as composite, s-video, " \
179 "or tuner. Since these settings are hardware-specific, you should find good " \
180 "settings in the \"Device config\" area, and use those numbers here. -1 " \
181 "means that settings will not be changed.")
182 #define AUDIO_IN_TEXT N_("Audio input pin")
183 #define AUDIO_IN_LONGTEXT N_( \
184 "Select the audio input source. See the \"video input\" option." )
185 #define VIDEO_OUT_TEXT N_("Video output pin")
186 #define VIDEO_OUT_LONGTEXT N_( \
187 "Select the video output type. See the \"video input\" option." )
188 #define AUDIO_OUT_TEXT N_("Audio output pin")
189 #define AUDIO_OUT_LONGTEXT N_( \
190 "Select the audio output type. See the \"video input\" option." )
192 #define AMTUNER_MODE_TEXT N_("AM Tuner mode")
193 #define AMTUNER_MODE_LONGTEXT N_( \
194 "AM Tuner mode. Can be one of Default (0), TV (1)," \
195 "AM Radio (2), FM Radio (3) or DSS (4).")
197 #define AUDIO_CHANNELS_TEXT N_("Number of audio channels")
198 #define AUDIO_CHANNELS_LONGTEXT N_( \
199 "Select audio input format with the given number of audio channels (if non 0)" )
201 #define AUDIO_SAMPLERATE_TEXT N_("Audio sample rate")
202 #define AUDIO_SAMPLERATE_LONGTEXT N_( \
203 "Select audio input format with the given sample rate (if non 0)" )
205 #define AUDIO_BITSPERSAMPLE_TEXT N_("Audio bits per sample")
206 #define AUDIO_BITSPERSAMPLE_LONGTEXT N_( \
207 "Select audio input format with the given bits/sample (if non 0)" )
209 static int CommonOpen ( vlc_object_t *, access_sys_t *, bool );
210 static void CommonClose( vlc_object_t *, access_sys_t * );
212 static int AccessOpen ( vlc_object_t * );
213 static void AccessClose( vlc_object_t * );
215 static int DemuxOpen ( vlc_object_t * );
216 static void DemuxClose ( vlc_object_t * );
219 set_shortname( N_("DirectShow") )
220 set_description( N_("DirectShow input") )
221 set_category( CAT_INPUT )
222 set_subcategory( SUBCAT_INPUT_ACCESS )
224 add_string( CFG_PREFIX "vdev", NULL, VDEV_TEXT, VDEV_LONGTEXT, false)
225 change_string_cb( FindDevices )
226 change_action_add( ConfigDevicesCallback, N_("Configure") )
228 add_string( CFG_PREFIX "adev", NULL, ADEV_TEXT, ADEV_LONGTEXT, false)
229 change_string_cb( FindDevices )
230 change_action_add( ConfigDevicesCallback, N_("Configure") )
232 add_string( CFG_PREFIX "size", NULL, SIZE_TEXT, SIZE_LONGTEXT, false)
235 add_string( CFG_PREFIX "aspect-ratio", "4:3", ASPECT_TEXT, ASPECT_LONGTEXT, false)
238 add_string( CFG_PREFIX "chroma", NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true )
241 add_float( CFG_PREFIX "fps", 0.0f, FPS_TEXT, FPS_LONGTEXT, true )
244 add_bool( CFG_PREFIX "config", false, CONFIG_TEXT, CONFIG_LONGTEXT, true )
246 add_bool( CFG_PREFIX "tuner", false, TUNER_TEXT, TUNER_LONGTEXT, true )
248 add_integer( CFG_PREFIX "tuner-channel", 0, CHANNEL_TEXT, CHANNEL_LONGTEXT,
252 add_integer( CFG_PREFIX "tuner-frequency", 0, TVFREQ_TEXT, TVFREQ_LONGTEXT,
256 add_integer( CFG_PREFIX "tuner-country", 0, COUNTRY_TEXT, COUNTRY_LONGTEXT,
259 add_integer( CFG_PREFIX "tuner-standard", 0, STANDARD_TEXT, STANDARD_TEXT,
261 change_integer_list( i_standards_list, ppsz_standards_list_text )
264 add_integer( CFG_PREFIX "tuner-input", 0, TUNER_INPUT_TEXT,
265 TUNER_INPUT_LONGTEXT, true )
266 change_integer_list( pi_tuner_input, ppsz_tuner_input_text )
269 add_integer( CFG_PREFIX "video-input", -1, VIDEO_IN_TEXT,
270 VIDEO_IN_LONGTEXT, true )
273 add_integer( CFG_PREFIX "video-output", -1, VIDEO_OUT_TEXT,
274 VIDEO_OUT_LONGTEXT, true )
276 add_integer( CFG_PREFIX "audio-input", -1, AUDIO_IN_TEXT,
277 AUDIO_IN_LONGTEXT, true )
280 add_integer( CFG_PREFIX "audio-output", -1, AUDIO_OUT_TEXT,
281 AUDIO_OUT_LONGTEXT, true )
283 add_integer( CFG_PREFIX "amtuner-mode", AMTUNER_MODE_TV,
284 AMTUNER_MODE_TEXT, AMTUNER_MODE_LONGTEXT, false)
285 change_integer_list( pi_amtuner_mode, ppsz_amtuner_mode_text )
288 add_integer( CFG_PREFIX "audio-channels", 0, AUDIO_CHANNELS_TEXT,
289 AUDIO_CHANNELS_LONGTEXT, true )
290 add_integer( CFG_PREFIX "audio-samplerate", 0, AUDIO_SAMPLERATE_TEXT,
291 AUDIO_SAMPLERATE_LONGTEXT, true )
292 add_integer( CFG_PREFIX "audio-bitspersample", 0, AUDIO_BITSPERSAMPLE_TEXT,
293 AUDIO_BITSPERSAMPLE_LONGTEXT, true )
295 add_shortcut( "dshow" )
296 set_capability( "access_demux", 0 )
297 set_callbacks( DemuxOpen, DemuxClose )
300 set_description( N_("DirectShow input") )
301 add_shortcut( "dshow" )
302 set_capability( "access", 0 )
303 set_callbacks( AccessOpen, AccessClose )
308 /*****************************************************************************
309 * DirectShow elementary stream descriptor
310 *****************************************************************************/
311 typedef struct dshow_stream_t
314 IBaseFilter *p_device_filter;
315 CaptureFilter *p_capture_filter;
320 VIDEOINFOHEADER video;
330 deque<VLCMediaSample> samples_queue;
333 /*****************************************************************************
334 * DirectShow utility functions
335 *****************************************************************************/
336 static void CreateDirectShowGraph( access_sys_t *p_sys )
338 p_sys->i_crossbar_route_depth = 0;
340 /* Create directshow filter graph */
341 if( SUCCEEDED( CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
342 (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph) ) )
344 /* Create directshow capture graph builder if available */
345 if( SUCCEEDED( CoCreateInstance( CLSID_CaptureGraphBuilder2, 0,
346 CLSCTX_INPROC, (REFIID)IID_ICaptureGraphBuilder2,
347 (void **)&p_sys->p_capture_graph_builder2 ) ) )
349 p_sys->p_capture_graph_builder2->
350 SetFiltergraph((IGraphBuilder *)p_sys->p_graph);
353 p_sys->p_graph->QueryInterface( IID_IMediaControl,
354 (void **)&p_sys->p_control );
358 static void DeleteDirectShowGraph( vlc_object_t *p_this, access_sys_t *p_sys )
360 DeleteCrossbarRoutes( p_sys );
362 /* Remove filters from graph */
363 msg_Dbg( p_this, "DeleteDirectShowGraph: Removing filters" );
364 for( int i = 0; i < p_sys->i_streams; i++ )
366 /* RemoveFilter does an undocumented Release()
367 * but does not set item to NULL */
368 msg_Dbg( p_this, "DeleteDirectShowGraph: Removing capture filter" );
369 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_capture_filter );
370 p_sys->pp_streams[i]->p_capture_filter = NULL;
372 msg_Dbg( p_this, "DeleteDirectShowGraph: Removing device filter" );
373 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_device_filter );
374 p_sys->pp_streams[i]->p_device_filter = NULL;
377 /* Release directshow objects */
378 if( p_sys->p_control )
380 p_sys->p_control->Release();
381 p_sys->p_control = NULL;
383 if( p_sys->p_capture_graph_builder2 )
385 p_sys->p_capture_graph_builder2->Release();
386 p_sys->p_capture_graph_builder2 = NULL;
391 p_sys->p_graph->Release();
392 p_sys->p_graph = NULL;
396 /*****************************************************************************
397 * CommonOpen: open direct show device
398 *****************************************************************************/
399 static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
400 bool b_access_demux )
404 /* Get/parse options and open device(s) */
405 string vdevname, adevname;
406 int i_width = 0, i_height = 0;
407 vlc_fourcc_t i_chroma = 0;
408 bool b_use_audio = true;
409 bool b_use_video = true;
411 /* Initialize OLE/COM */
412 CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
414 var_Create( p_this, CFG_PREFIX "config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
415 var_Create( p_this, CFG_PREFIX "tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
416 psz_val = var_CreateGetString( p_this, CFG_PREFIX "vdev" );
419 msg_Dbg( p_this, "dshow-vdev: %s", psz_val ) ;
420 /* skip none device */
421 if ( strncasecmp( psz_val, "none", 4 ) != 0 )
422 vdevname = string( psz_val );
424 b_use_video = false ;
428 psz_val = var_CreateGetString( p_this, CFG_PREFIX "adev" );
431 msg_Dbg( p_this, "dshow-adev: %s", psz_val ) ;
432 /* skip none device */
433 if ( strncasecmp( psz_val, "none", 4 ) != 0 )
434 adevname = string( psz_val );
436 b_use_audio = false ;
442 const char *psz_size;
446 { { "subqcif", 128, 96 },
447 { "qsif", 160, 120 },
448 { "qcif", 176, 144 },
455 psz_val = var_CreateGetString( p_this, CFG_PREFIX "size" );
456 if( !EMPTY_STR(psz_val) )
459 for( i = 0; size_table[i].psz_size; i++ )
461 if( !strcmp( psz_val, size_table[i].psz_size ) )
463 i_width = size_table[i].i_width;
464 i_height = size_table[i].i_height;
468 if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
471 i_width = strtol( psz_val, &psz_parser, 0 );
472 if( *psz_parser == 'x' || *psz_parser == 'X')
474 i_height = strtol( psz_parser + 1, &psz_parser, 0 );
476 msg_Dbg( p_this, "width x height %dx%d", i_width, i_height );
482 psz_val = var_CreateGetString( p_this, CFG_PREFIX "chroma" );
483 i_chroma = vlc_fourcc_GetCodecFromString( UNKNOWN_ES, psz_val );
484 p_sys->b_chroma = i_chroma != 0;
487 var_Create( p_this, CFG_PREFIX "fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
488 var_Create( p_this, CFG_PREFIX "tuner-channel",
489 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
490 var_Create( p_this, CFG_PREFIX "tuner-frequency",
491 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
492 var_Create( p_this, CFG_PREFIX "tuner-standard",
493 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
494 var_Create( p_this, CFG_PREFIX "tuner-country",
495 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
496 var_Create( p_this, CFG_PREFIX "tuner-input",
497 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
499 var_Create( p_this, CFG_PREFIX "amtuner-mode",
500 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
502 var_Create( p_this, CFG_PREFIX "video-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
503 var_Create( p_this, CFG_PREFIX "audio-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
504 var_Create( p_this, CFG_PREFIX "video-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
505 var_Create( p_this, CFG_PREFIX "audio-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
508 /* Initialize some data */
509 p_sys->i_streams = 0;
510 p_sys->pp_streams = NULL;
511 p_sys->i_width = i_width;
512 p_sys->i_height = i_height;
513 p_sys->i_chroma = i_chroma;
515 p_sys->p_graph = NULL;
516 p_sys->p_capture_graph_builder2 = NULL;
517 p_sys->p_control = NULL;
519 /* Build directshow graph */
520 CreateDirectShowGraph( p_sys );
522 vlc_mutex_init( &p_sys->lock );
523 vlc_cond_init( &p_sys->wait );
525 if( !b_use_video && !b_use_audio )
527 dialog_Fatal( p_this, _("Capture failed"),
528 _("No video or audio device selected.") );
529 return VLC_EGENERIC ;
533 msg_Dbg( p_this, "skipping video device" ) ;
534 bool b_err_video = false ;
536 if( b_use_video && OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
538 msg_Err( p_this, "can't open video device");
542 if ( b_use_video && !b_err_video )
544 /* Check if we can handle the demuxing ourselves or need to spawn
545 * a demuxer module */
546 dshow_stream_t *p_stream = p_sys->pp_streams[p_sys->i_streams-1];
548 if( p_stream->mt.majortype == MEDIATYPE_Video )
550 if( /* Raw DV stream */
551 p_stream->i_fourcc == VLC_CODEC_DV ||
552 /* Raw MPEG video stream */
553 p_stream->i_fourcc == VLC_CODEC_MPGV )
559 /* Let the access (only) take care of that */
565 if( p_stream->mt.majortype == MEDIATYPE_Stream )
571 /* Let the access (only) take care of that */
575 if( var_GetBool( p_this, CFG_PREFIX "tuner" ) )
577 /* FIXME: we do MEDIATYPE_Stream here so we don't do
579 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
580 p_stream->p_device_filter, 0 );
586 msg_Dbg( p_this, "skipping audio device") ;
588 bool b_err_audio = false ;
590 if( b_use_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
592 msg_Err( p_this, "can't open audio device");
596 if( ( b_use_video && b_err_video && b_use_audio && b_err_audio ) ||
597 ( !b_use_video && b_use_audio && b_err_audio ) ||
598 ( b_use_video && !b_use_audio && b_err_video ) )
600 msg_Err( p_this, "FATAL: could not open ANY device" ) ;
601 dialog_Fatal( p_this, _("Capture failed"),
602 _("VLC cannot open ANY capture device. "
603 "Check the error log for details.") );
604 return VLC_EGENERIC ;
607 for( int i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
609 int i_val = var_GetInteger( p_this, CFG_PREFIX "video-input" );
611 p_sys->crossbar_routes[i].VideoInputIndex = i_val;
612 i_val = var_GetInteger( p_this, CFG_PREFIX "video-output" );
614 p_sys->crossbar_routes[i].VideoOutputIndex = i_val;
615 i_val = var_GetInteger( p_this, CFG_PREFIX "audio-input" );
617 p_sys->crossbar_routes[i].AudioInputIndex = i_val;
618 i_val = var_GetInteger( p_this, CFG_PREFIX "audio-output" );
620 p_sys->crossbar_routes[i].AudioOutputIndex = i_val;
622 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
623 LONG VideoInputIndex = p_sys->crossbar_routes[i].VideoInputIndex;
624 LONG VideoOutputIndex = p_sys->crossbar_routes[i].VideoOutputIndex;
625 LONG AudioInputIndex = p_sys->crossbar_routes[i].AudioInputIndex;
626 LONG AudioOutputIndex = p_sys->crossbar_routes[i].AudioOutputIndex;
628 if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
630 msg_Dbg( p_this, "crossbar at depth %d, routed video "
631 "output %ld to video input %ld", i, VideoOutputIndex,
634 if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
636 if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
639 msg_Dbg(p_this, "crossbar at depth %d, routed audio "
640 "output %ld to audio input %ld", i,
641 AudioOutputIndex, AudioInputIndex );
646 msg_Err( p_this, "crossbar at depth %d could not route video "
647 "output %ld to input %ld", i, VideoOutputIndex, VideoInputIndex );
651 ** Show properties pages from other filters in graph
653 if( var_GetBool( p_this, CFG_PREFIX "config" ) )
655 for( int i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
657 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
660 if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
663 ShowPropertyPage( p_XF );
669 /* Initialize some data */
670 p_sys->i_current_stream = 0;
672 if( !p_sys->i_streams ) return VLC_EGENERIC;
677 /*****************************************************************************
678 * DemuxOpen: open direct show device as an access_demux module
679 *****************************************************************************/
680 static int DemuxOpen( vlc_object_t *p_this )
682 demux_t *p_demux = (demux_t *)p_this;
685 p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
688 p_demux->p_sys = (demux_sys_t *)p_sys;
690 if( CommonOpen( p_this, p_sys, true ) != VLC_SUCCESS )
692 CommonClose( p_this, p_sys );
696 /* Everything is ready. Let's rock baby */
697 msg_Dbg( p_this, "Playing...");
698 p_sys->p_control->Run();
700 p_demux->pf_demux = Demux;
701 p_demux->pf_control = DemuxControl;
702 p_demux->info.i_update = 0;
703 p_demux->info.i_title = 0;
704 p_demux->info.i_seekpoint = 0;
706 for( int i = 0; i < p_sys->i_streams; i++ )
708 dshow_stream_t *p_stream = p_sys->pp_streams[i];
711 if( p_stream->mt.majortype == MEDIATYPE_Video )
713 char *psz_aspect = var_CreateGetString( p_this, CFG_PREFIX "aspect-ratio" );
714 char *psz_delim = !EMPTY_STR( psz_aspect ) ? strchr( psz_aspect, ':' ) : NULL;
716 es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
718 fmt.video.i_width = p_stream->header.video.bmiHeader.biWidth;
719 fmt.video.i_height = p_stream->header.video.bmiHeader.biHeight;
723 fmt.video.i_sar_num = atoi( psz_aspect ) * fmt.video.i_height;
724 fmt.video.i_sar_den = atoi( psz_delim + 1 ) * fmt.video.i_width;
728 fmt.video.i_sar_num = 4 * fmt.video.i_height;
729 fmt.video.i_sar_den = 3 * fmt.video.i_width;
733 if( !p_stream->header.video.bmiHeader.biCompression )
735 /* RGB DIB are coded from bottom to top */
736 fmt.video.i_height = (unsigned int)(-(int)fmt.video.i_height);
739 /* Setup rgb mask for RGB formats */
740 if( p_stream->i_fourcc == VLC_CODEC_RGB24 )
742 /* This is in RGB format
743 http://msdn.microsoft.com/en-us/library/dd407253%28VS.85%29.aspx?ppud=4
745 fmt.video.i_rmask = 0x00ff0000;
746 fmt.video.i_gmask = 0x0000ff00;
747 fmt.video.i_bmask = 0x000000ff;
750 if( p_stream->header.video.AvgTimePerFrame )
752 fmt.video.i_frame_rate = 10000000;
753 fmt.video.i_frame_rate_base =
754 p_stream->header.video.AvgTimePerFrame;
757 else if( p_stream->mt.majortype == MEDIATYPE_Audio )
759 es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
761 fmt.audio.i_channels = p_stream->header.audio.nChannels;
762 fmt.audio.i_rate = p_stream->header.audio.nSamplesPerSec;
763 fmt.audio.i_bitspersample = p_stream->header.audio.wBitsPerSample;
764 fmt.audio.i_blockalign = fmt.audio.i_channels *
765 fmt.audio.i_bitspersample / 8;
766 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
767 fmt.audio.i_bitspersample;
770 p_stream->p_es = es_out_Add( p_demux->out, &fmt );
776 /*****************************************************************************
777 * AccessOpen: open direct show device as an access module
778 *****************************************************************************/
779 static int AccessOpen( vlc_object_t *p_this )
781 access_t *p_access = (access_t*)p_this;
784 p_access->p_sys = p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
788 if( CommonOpen( p_this, p_sys, false ) != VLC_SUCCESS )
790 CommonClose( p_this, p_sys );
794 dshow_stream_t *p_stream = p_sys->pp_streams[0];
796 /* Check if we need to force demuxers */
797 if( !p_access->psz_demux || !*p_access->psz_demux )
799 if( p_stream->i_fourcc == VLC_CODEC_DV )
801 free( p_access->psz_demux );
802 p_access->psz_demux = strdup( "rawdv" );
804 else if( p_stream->i_fourcc == VLC_CODEC_MPGV )
806 free( p_access->psz_demux );
807 p_access->psz_demux = strdup( "mpgv" );
812 p_access->pf_read = NULL;
813 p_access->pf_block = ReadCompressed;
814 p_access->pf_control = AccessControl;
815 p_access->pf_seek = NULL;
816 p_access->info.i_update = 0;
817 p_access->info.i_size = 0;
818 p_access->info.i_pos = 0;
819 p_access->info.b_eof = false;
820 p_access->info.i_title = 0;
821 p_access->info.i_seekpoint = 0;
822 p_access->p_sys = p_sys;
824 /* Everything is ready. Let's rock baby */
825 msg_Dbg( p_this, "Playing...");
826 p_sys->p_control->Run();
831 /*****************************************************************************
832 * CommonClose: close device
833 *****************************************************************************/
834 static void CommonClose( vlc_object_t *p_this, access_sys_t *p_sys )
836 msg_Dbg( p_this, "releasing DirectShow");
838 DeleteDirectShowGraph( p_this, p_sys );
840 /* Uninitialize OLE/COM */
843 for( int i = 0; i < p_sys->i_streams; i++ ) delete p_sys->pp_streams[i];
844 if( p_sys->i_streams ) free( p_sys->pp_streams );
846 vlc_mutex_destroy( &p_sys->lock );
847 vlc_cond_destroy( &p_sys->wait );
852 /*****************************************************************************
853 * AccessClose: close device
854 *****************************************************************************/
855 static void AccessClose( vlc_object_t *p_this )
857 access_t *p_access = (access_t *)p_this;
858 access_sys_t *p_sys = p_access->p_sys;
860 /* Stop capturing stuff */
861 p_sys->p_control->Stop();
863 CommonClose( p_this, p_sys );
866 /*****************************************************************************
867 * DemuxClose: close device
868 *****************************************************************************/
869 static void DemuxClose( vlc_object_t *p_this )
871 demux_t *p_demux = (demux_t *)p_this;
872 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
874 /* Stop capturing stuff */
875 p_sys->p_control->Stop();
877 CommonClose( p_this, p_sys );
880 /****************************************************************************
882 ****************************************************************************/
883 static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
884 IBaseFilter *p_filter,
885 CaptureFilter *p_capture_filter )
887 CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
889 AM_MEDIA_TYPE mediaType = p_input_pin->CustomGetMediaType();
891 if( p_sys->p_capture_graph_builder2 )
893 if( FAILED(p_sys->p_capture_graph_builder2->
894 RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
895 p_filter, 0, (IBaseFilter *)p_capture_filter )) )
900 // Sort out all the possible video inputs
901 // The class needs to be given the capture filters ANALOGVIDEO input pin
902 IEnumPins *pins = NULL;
903 if( ( mediaType.majortype == MEDIATYPE_Video ||
904 mediaType.majortype == MEDIATYPE_Stream ) &&
905 SUCCEEDED(p_filter->EnumPins(&pins)) )
911 IKsPropertySet *pKs = NULL;
915 while( !Found && ( S_OK == pins->Next(1, &pP, &n) ) )
917 if( S_OK == pP->QueryPinInfo(&pinInfo) )
919 // is this pin an ANALOGVIDEOIN input pin?
920 if( pinInfo.dir == PINDIR_INPUT &&
921 pP->QueryInterface( IID_IKsPropertySet,
922 (void **)&pKs ) == S_OK )
924 if( pKs->Get( AMPROPSETID_Pin,
925 AMPROPERTY_PIN_CATEGORY, NULL, 0,
926 &guid, sizeof(GUID), &dw ) == S_OK )
928 if( guid == PIN_CATEGORY_ANALOGVIDEOIN )
930 // recursively search crossbar routes
931 FindCrossbarRoutes( p_this, p_sys, pP, 0 );
938 pinInfo.pFilter->Release();
943 msg_Dbg( p_this, "ConnectFilters: graph_builder2 available.") ;
945 msg_Warn( p_this, "ConnectFilters: No crossBar routes found (incompatible pin types)" ) ;
951 IEnumPins *p_enumpins;
954 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;
956 while( S_OK == p_enumpins->Next( 1, &p_pin, NULL ) )
958 PIN_DIRECTION pin_dir;
959 p_pin->QueryDirection( &pin_dir );
961 if( pin_dir == PINDIR_OUTPUT &&
962 p_sys->p_graph->ConnectDirect( p_pin, (IPin *)p_input_pin,
966 p_enumpins->Release();
972 p_enumpins->Release();
978 * get fourcc priority from arbritary preference, the higher the better
980 static int GetFourCCPriority( int i_fourcc )
988 case VLC_FOURCC('a','r','a','w'):
990 case VLC_CODEC_RGB24:
993 case VLC_CODEC_RGB32:
1001 #define MAX_MEDIA_TYPES 32
1003 static int OpenDevice( vlc_object_t *p_this, access_sys_t *p_sys,
1004 string devicename, bool b_audio )
1006 /* See if device is already opened */
1007 for( int i = 0; i < p_sys->i_streams; i++ )
1009 if( devicename.size() &&
1010 p_sys->pp_streams[i]->devicename == devicename )
1012 /* Already opened */
1017 list<string> list_devices;
1019 /* Enumerate devices and display their names */
1020 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1021 if( list_devices.empty() )
1022 return VLC_EGENERIC;
1024 list<string>::iterator iter;
1025 for( iter = list_devices.begin(); iter != list_devices.end(); ++iter )
1026 msg_Dbg( p_this, "found device: %s", iter->c_str() );
1028 /* If no device name was specified, pick the 1st one */
1029 if( devicename.size() == 0 )
1031 /* When none selected */
1032 devicename = *list_devices.begin();
1033 msg_Dbg( p_this, "asking for default device: %s", devicename.c_str() ) ;
1036 msg_Dbg( p_this, "asking for device: %s", devicename.c_str() ) ;
1037 // Use the system device enumerator and class enumerator to find
1038 // a capture/preview device, such as a desktop USB video camera.
1039 IBaseFilter *p_device_filter =
1040 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
1042 if( p_device_filter )
1043 msg_Dbg( p_this, "using device: %s", devicename.c_str() );
1046 msg_Err( p_this, "can't use device: %s, unsupported device type",
1047 devicename.c_str() );
1048 dialog_Fatal( p_this, _("Capture failed"),
1049 _("VLC cannot use the device \"%s\", because its "
1050 "type is not supported."), devicename.c_str() );
1051 return VLC_EGENERIC;
1054 // Retreive acceptable media types supported by device
1055 AM_MEDIA_TYPE media_types[MAX_MEDIA_TYPES];
1056 size_t media_count =
1057 EnumDeviceCaps( p_this, p_device_filter, b_audio ? 0 : p_sys->i_chroma,
1058 p_sys->i_width, p_sys->i_height,
1059 b_audio ? var_CreateGetInteger( p_this, CFG_PREFIX "audio-channels" ) : 0,
1060 b_audio ? var_CreateGetInteger( p_this, CFG_PREFIX "audio-samplerate" ) : 0,
1061 b_audio ? var_CreateGetInteger( p_this, CFG_PREFIX "audio-bitspersample" ) : 0,
1062 media_types, MAX_MEDIA_TYPES );
1064 AM_MEDIA_TYPE *mt = NULL;
1066 if( media_count > 0 )
1068 mt = (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * media_count);
1070 // Order and copy returned media types according to arbitrary
1072 for( size_t c = 0; c < media_count; c++ )
1075 GetFourCCPriority(GetFourCCFromMediaType(media_types[c]));
1076 size_t slot_copy = c;
1077 for( size_t d = c+1; d < media_count; d++ )
1080 GetFourCCPriority(GetFourCCFromMediaType(media_types[d]));
1081 if( priority > slot_priority )
1083 slot_priority = priority;
1087 if( slot_copy != c )
1089 mt[c] = media_types[slot_copy];
1090 media_types[slot_copy] = media_types[c];
1094 mt[c] = media_types[c];
1099 /* capture device */
1100 msg_Err( p_this, "capture device '%s' does not support required parameters !", devicename.c_str() );
1101 dialog_Fatal( p_this, _("Capture failed"),
1102 _("The capture device \"%s\" does not support the "
1103 "required parameters."), devicename.c_str() );
1104 p_device_filter->Release();
1105 return VLC_EGENERIC;
1108 /* Create and add our capture filter */
1109 CaptureFilter *p_capture_filter =
1110 new CaptureFilter( p_this, p_sys, mt, media_count );
1111 p_sys->p_graph->AddFilter( p_capture_filter, 0 );
1113 /* Add the device filter to the graph (seems necessary with VfW before
1114 * accessing pin attributes). */
1115 p_sys->p_graph->AddFilter( p_device_filter, 0 );
1117 /* Attempt to connect one of this device's capture output pins */
1118 msg_Dbg( p_this, "connecting filters" );
1119 if( ConnectFilters( p_this, p_sys, p_device_filter, p_capture_filter ) )
1122 msg_Dbg( p_this, "filters connected successfully !" );
1124 dshow_stream_t dshow_stream;
1125 dshow_stream.b_pts = false;
1126 dshow_stream.p_es = 0;
1128 p_capture_filter->CustomGetPin()->CustomGetMediaType();
1130 /* Show Device properties. Done here so the VLC stream is setup with
1131 * the proper parameters. */
1132 if( var_GetBool( p_this, CFG_PREFIX "config" ) )
1134 ShowDeviceProperties( p_this, p_sys->p_capture_graph_builder2,
1135 p_device_filter, b_audio );
1138 ConfigTuner( p_this, p_sys->p_capture_graph_builder2,
1141 if( var_GetBool( p_this, CFG_PREFIX "tuner" ) &&
1142 dshow_stream.mt.majortype != MEDIATYPE_Stream )
1144 /* FIXME: we do MEDIATYPE_Stream later so we don't do it twice. */
1145 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
1146 p_device_filter, b_audio );
1150 p_capture_filter->CustomGetPin()->CustomGetMediaType();
1152 dshow_stream.i_fourcc = GetFourCCFromMediaType( dshow_stream.mt );
1153 if( dshow_stream.i_fourcc )
1155 if( dshow_stream.mt.majortype == MEDIATYPE_Video )
1157 dshow_stream.header.video =
1158 *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
1159 msg_Dbg( p_this, "MEDIATYPE_Video" );
1160 msg_Dbg( p_this, "selected video pin accepts format: %4.4s",
1161 (char *)&dshow_stream.i_fourcc);
1163 else if( dshow_stream.mt.majortype == MEDIATYPE_Audio )
1165 dshow_stream.header.audio =
1166 *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
1167 msg_Dbg( p_this, "MEDIATYPE_Audio" );
1168 msg_Dbg( p_this, "selected audio pin accepts format: %4.4s",
1169 (char *)&dshow_stream.i_fourcc);
1171 else if( dshow_stream.mt.majortype == MEDIATYPE_Stream )
1173 msg_Dbg( p_this, "MEDIATYPE_Stream" );
1174 msg_Dbg( p_this, "selected stream pin accepts format: %4.4s",
1175 (char *)&dshow_stream.i_fourcc);
1179 msg_Dbg( p_this, "unknown stream majortype" );
1183 /* Add directshow elementary stream to our list */
1184 dshow_stream.p_device_filter = p_device_filter;
1185 dshow_stream.p_capture_filter = p_capture_filter;
1187 p_sys->pp_streams = (dshow_stream_t **)xrealloc( p_sys->pp_streams,
1188 sizeof(dshow_stream_t *) * (p_sys->i_streams + 1) );
1189 p_sys->pp_streams[p_sys->i_streams] = new dshow_stream_t;
1190 *p_sys->pp_streams[p_sys->i_streams++] = dshow_stream;
1197 /* Remove filters from graph */
1198 msg_Dbg( p_this, "OpenDevice: Removing filters" ) ;
1199 p_sys->p_graph->RemoveFilter( p_device_filter );
1200 p_sys->p_graph->RemoveFilter( p_capture_filter );
1202 /* Release objects */
1203 /* RemoveFilter does an undocumented Release()
1204 * but does not set item to NULL*/
1206 p_device_filter = NULL;
1207 p_capture_filter = NULL;
1209 return VLC_EGENERIC;
1212 /* FindCaptureDevices:: This Function had two purposes :
1213 Returns the list of capture devices when p_listdevices != NULL
1214 Creates an IBaseFilter when p_devicename corresponds to an existing devname
1215 These actions *may* be requested whith a single call.
1217 static IBaseFilter *
1218 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
1219 list<string> *p_listdevices, bool b_audio )
1221 IBaseFilter *p_base_filter = NULL;
1222 IMoniker *p_moniker = NULL;
1225 list<string> devicelist;
1227 /* Create the system device enumerator */
1228 ICreateDevEnum *p_dev_enum = NULL;
1230 hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
1231 IID_ICreateDevEnum, (void **)&p_dev_enum );
1234 msg_Err( p_this, "failed to create the device enumerator (0x%lx)", hr);
1238 /* Create an enumerator for the video capture devices */
1239 IEnumMoniker *p_class_enum = NULL;
1241 hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
1244 hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
1246 p_dev_enum->Release();
1249 msg_Err( p_this, "failed to create the class enumerator (0x%lx)", hr );
1253 /* If there are no enumerators for the requested type, then
1254 * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
1255 if( p_class_enum == NULL )
1257 msg_Err( p_this, "no %s capture device was detected", ( b_audio ? "audio" : "video" ) );
1261 /* Enumerate the devices */
1263 /* Note that if the Next() call succeeds but there are no monikers,
1264 * it will return S_FALSE (which is not a failure). Therefore, we check
1265 * that the return code is S_OK instead of using SUCCEEDED() macro. */
1267 while( p_class_enum->Next( 1, &p_moniker, &i_fetched ) == S_OK )
1269 /* Getting the property page to get the device name */
1270 IPropertyBag *p_bag;
1271 hr = p_moniker->BindToStorage( 0, 0, IID_IPropertyBag,
1277 hr = p_bag->Read( L"FriendlyName", &var, NULL );
1281 char *p_buf = FromWide( var.bstrVal );
1282 string devname = string(p_buf);
1286 /* find out if this name is already used by a previously found device */
1287 list<string>::const_iterator iter = devicelist.begin();
1288 list<string>::const_iterator end = devicelist.end();
1289 string ordevname = devname ;
1290 while ( iter != end )
1292 if( 0 == (*iter).compare( devname ) )
1293 { /* devname is on the list. Try another name with sequence
1294 number apended and then rescan until a unique entry is found*/
1296 snprintf(seq, 16, " #%d", ++dup);
1297 devname = ordevname + seq;
1298 iter = devicelist.begin();
1303 devicelist.push_back( devname );
1305 if( p_devicename && *p_devicename == devname )
1307 msg_Dbg( p_this, "asked for %s, binding to %s", p_devicename->c_str() , devname.c_str() ) ;
1308 /* NULL possibly means we don't need BindMoniker BindCtx ?? */
1309 hr = p_moniker->BindToObject( NULL, 0, IID_IBaseFilter,
1310 (void **)&p_base_filter );
1313 msg_Err( p_this, "couldn't bind moniker to filter "
1314 "object (0x%lx)", hr );
1315 p_moniker->Release();
1316 p_class_enum->Release();
1319 p_moniker->Release();
1320 p_class_enum->Release();
1321 return p_base_filter;
1326 p_moniker->Release();
1329 p_class_enum->Release();
1331 if( p_listdevices ) {
1333 *p_listdevices = devicelist;
1338 static size_t EnumDeviceCaps( vlc_object_t *p_this, IBaseFilter *p_filter,
1339 int i_fourcc, int i_width, int i_height,
1340 int i_channels, int i_samplespersec,
1341 int i_bitspersample, AM_MEDIA_TYPE *mt,
1344 IEnumPins *p_enumpins;
1346 IEnumMediaTypes *p_enummt;
1347 size_t mt_count = 0;
1349 LONGLONG i_AvgTimePerFrame = 0;
1350 float r_fps = var_GetFloat( p_this, CFG_PREFIX "fps" );
1352 i_AvgTimePerFrame = 10000000000LL/(LONGLONG)(r_fps*1000.0f);
1354 if( FAILED(p_filter->EnumPins( &p_enumpins )) )
1356 msg_Dbg( p_this, "EnumDeviceCaps failed: no pin enumeration !");
1360 while( S_OK == p_enumpins->Next( 1, &p_output_pin, NULL ) )
1364 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1366 msg_Dbg( p_this, "EnumDeviceCaps: %s pin: %S",
1367 info.dir == PINDIR_INPUT ? "input" : "output",
1369 if( info.pFilter ) info.pFilter->Release();
1372 p_output_pin->Release();
1375 p_enumpins->Reset();
1377 while( !mt_count && p_enumpins->Next( 1, &p_output_pin, NULL ) == S_OK )
1381 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1383 if( info.pFilter ) info.pFilter->Release();
1384 if( info.dir == PINDIR_INPUT )
1386 p_output_pin->Release();
1389 msg_Dbg( p_this, "EnumDeviceCaps: trying pin %S", info.achName );
1392 AM_MEDIA_TYPE *p_mt;
1395 ** Configure pin with a default compatible media if possible
1398 IAMStreamConfig *pSC;
1399 if( SUCCEEDED(p_output_pin->QueryInterface( IID_IAMStreamConfig,
1402 int piCount, piSize;
1403 if( SUCCEEDED(pSC->GetNumberOfCapabilities(&piCount, &piSize)) )
1405 BYTE *pSCC= (BYTE *)CoTaskMemAlloc(piSize);
1408 int i_priority = -1;
1409 for( int i=0; i<piCount; ++i )
1411 if( SUCCEEDED(pSC->GetStreamCaps(i, &p_mt, pSCC)) )
1413 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1414 int i_current_priority = GetFourCCPriority(i_current_fourcc);
1416 if( (i_fourcc && (i_current_fourcc != i_fourcc))
1417 || (i_priority > i_current_priority) )
1419 // unwanted chroma, try next media type
1420 FreeMediaType( *p_mt );
1421 CoTaskMemFree( (PVOID)p_mt );
1425 if( MEDIATYPE_Video == p_mt->majortype
1426 && FORMAT_VideoInfo == p_mt->formattype )
1428 VIDEO_STREAM_CONFIG_CAPS *pVSCC = reinterpret_cast<VIDEO_STREAM_CONFIG_CAPS*>(pSCC);
1429 VIDEOINFOHEADER *pVih = reinterpret_cast<VIDEOINFOHEADER*>(p_mt->pbFormat);
1431 if( i_AvgTimePerFrame )
1433 if( pVSCC->MinFrameInterval > i_AvgTimePerFrame
1434 || i_AvgTimePerFrame > pVSCC->MaxFrameInterval )
1436 // required frame rate not compatible, try next media type
1437 FreeMediaType( *p_mt );
1438 CoTaskMemFree( (PVOID)p_mt );
1441 pVih->AvgTimePerFrame = i_AvgTimePerFrame;
1446 if(( !pVSCC->OutputGranularityX
1447 && i_width != pVSCC->MinOutputSize.cx
1448 && i_width != pVSCC->MaxOutputSize.cx)
1450 ( pVSCC->OutputGranularityX
1451 && ((i_width % pVSCC->OutputGranularityX)
1452 || pVSCC->MinOutputSize.cx > i_width
1453 || i_width > pVSCC->MaxOutputSize.cx )))
1455 // required width not compatible, try next media type
1456 FreeMediaType( *p_mt );
1457 CoTaskMemFree( (PVOID)p_mt );
1460 pVih->bmiHeader.biWidth = i_width;
1465 if(( !pVSCC->OutputGranularityY
1466 && i_height != pVSCC->MinOutputSize.cy
1467 && i_height != pVSCC->MaxOutputSize.cy)
1469 ( pVSCC->OutputGranularityY
1470 && ((i_height % pVSCC->OutputGranularityY)
1471 || pVSCC->MinOutputSize.cy > i_height
1472 || i_height > pVSCC->MaxOutputSize.cy )))
1474 // required height not compatible, try next media type
1475 FreeMediaType( *p_mt );
1476 CoTaskMemFree( (PVOID)p_mt );
1479 pVih->bmiHeader.biHeight = i_height;
1482 // Set the sample size and image size.
1483 // (Round the image width up to a DWORD boundary.)
1484 p_mt->lSampleSize = pVih->bmiHeader.biSizeImage =
1485 ((pVih->bmiHeader.biWidth + 3) & ~3) *
1486 pVih->bmiHeader.biHeight * (pVih->bmiHeader.biBitCount>>3);
1488 // no cropping, use full video input buffer
1489 memset(&(pVih->rcSource), 0, sizeof(RECT));
1490 memset(&(pVih->rcTarget), 0, sizeof(RECT));
1492 // select this format as default
1493 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1495 i_priority = i_current_priority;
1497 // no need to check any more media types
1501 else if( p_mt->majortype == MEDIATYPE_Audio
1502 && p_mt->formattype == FORMAT_WaveFormatEx )
1504 AUDIO_STREAM_CONFIG_CAPS *pASCC = reinterpret_cast<AUDIO_STREAM_CONFIG_CAPS*>(pSCC);
1505 WAVEFORMATEX *pWfx = reinterpret_cast<WAVEFORMATEX*>(p_mt->pbFormat);
1507 if( i_current_fourcc && (WAVE_FORMAT_PCM == pWfx->wFormatTag) )
1509 int val = i_channels;
1513 if( ( !pASCC->ChannelsGranularity
1514 && (unsigned int)val != pASCC->MinimumChannels
1515 && (unsigned int)val != pASCC->MaximumChannels)
1517 ( pASCC->ChannelsGranularity
1518 && ((val % pASCC->ChannelsGranularity)
1519 || (unsigned int)val < pASCC->MinimumChannels
1520 || (unsigned int)val > pASCC->MaximumChannels)))
1522 // required number channels not available, try next media type
1523 FreeMediaType( *p_mt );
1524 CoTaskMemFree( (PVOID)p_mt );
1527 pWfx->nChannels = val;
1529 val = i_samplespersec;
1533 if( ( !pASCC->SampleFrequencyGranularity
1534 && (unsigned int)val != pASCC->MinimumSampleFrequency
1535 && (unsigned int)val != pASCC->MaximumSampleFrequency)
1537 ( pASCC->SampleFrequencyGranularity
1538 && ((val % pASCC->SampleFrequencyGranularity)
1539 || (unsigned int)val < pASCC->MinimumSampleFrequency
1540 || (unsigned int)val > pASCC->MaximumSampleFrequency )))
1542 // required sampling rate not available, try next media type
1543 FreeMediaType( *p_mt );
1544 CoTaskMemFree( (PVOID)p_mt );
1547 pWfx->nSamplesPerSec = val;
1549 val = i_bitspersample;
1552 if( VLC_CODEC_FL32 == i_current_fourcc )
1558 if( ( !pASCC->BitsPerSampleGranularity
1559 && (unsigned int)val != pASCC->MinimumBitsPerSample
1560 && (unsigned int)val != pASCC->MaximumBitsPerSample )
1562 ( pASCC->BitsPerSampleGranularity
1563 && ((val % pASCC->BitsPerSampleGranularity)
1564 || (unsigned int)val < pASCC->MinimumBitsPerSample
1565 || (unsigned int)val > pASCC->MaximumBitsPerSample )))
1567 // required sample size not available, try next media type
1568 FreeMediaType( *p_mt );
1569 CoTaskMemFree( (PVOID)p_mt );
1573 pWfx->wBitsPerSample = val;
1574 pWfx->nBlockAlign = (pWfx->wBitsPerSample * pWfx->nChannels)/8;
1575 pWfx->nAvgBytesPerSec = pWfx->nSamplesPerSec * pWfx->nBlockAlign;
1577 // select this format as default
1578 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1580 i_priority = i_current_priority;
1584 FreeMediaType( *p_mt );
1585 CoTaskMemFree( (PVOID)p_mt );
1588 CoTaskMemFree( (LPVOID)pSCC );
1589 if( i_priority >= 0 )
1590 msg_Dbg( p_this, "EnumDeviceCaps: input pin default format configured");
1597 ** Probe pin for available medias (may be a previously configured one)
1600 if( FAILED( p_output_pin->EnumMediaTypes( &p_enummt ) ) )
1602 p_output_pin->Release();
1606 while( p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1608 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1609 if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Video
1610 && p_mt->formattype == FORMAT_VideoInfo )
1612 int i_current_width = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1613 int i_current_height = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1614 LONGLONG i_current_atpf = ((VIDEOINFOHEADER *)p_mt->pbFormat)->AvgTimePerFrame;
1616 if( i_current_height < 0 )
1617 i_current_height = -i_current_height;
1619 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1620 "accepts chroma: %4.4s, width:%i, height:%i, fps:%f",
1621 (char *)&i_current_fourcc, i_current_width,
1622 i_current_height, (10000000.0f/((float)i_current_atpf)) );
1624 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1625 ( !i_width || i_width == i_current_width ) &&
1626 ( !i_height || i_height == i_current_height ) &&
1627 ( !i_AvgTimePerFrame || i_AvgTimePerFrame == i_current_atpf ) &&
1631 mt[mt_count++] = *p_mt;
1633 else FreeMediaType( *p_mt );
1635 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Audio
1636 && p_mt->formattype == FORMAT_WaveFormatEx)
1638 int i_current_channels =
1639 ((WAVEFORMATEX *)p_mt->pbFormat)->nChannels;
1640 int i_current_samplespersec =
1641 ((WAVEFORMATEX *)p_mt->pbFormat)->nSamplesPerSec;
1642 int i_current_bitspersample =
1643 ((WAVEFORMATEX *)p_mt->pbFormat)->wBitsPerSample;
1645 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1646 "accepts format: %4.4s, channels:%i, "
1647 "samples/sec:%i bits/sample:%i",
1648 (char *)&i_current_fourcc, i_current_channels,
1649 i_current_samplespersec, i_current_bitspersample);
1651 if( (!i_channels || i_channels == i_current_channels) &&
1652 (!i_samplespersec ||
1653 i_samplespersec == i_current_samplespersec) &&
1654 (!i_bitspersample ||
1655 i_bitspersample == i_current_bitspersample) &&
1659 mt[mt_count++] = *p_mt;
1661 /* Setup a few properties like the audio latency */
1662 IAMBufferNegotiation *p_ambuf;
1663 if( SUCCEEDED( p_output_pin->QueryInterface(
1664 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
1666 ALLOCATOR_PROPERTIES AllocProp;
1667 AllocProp.cbAlign = -1;
1669 /* 100 ms of latency */
1670 AllocProp.cbBuffer = i_current_channels *
1671 i_current_samplespersec *
1672 i_current_bitspersample / 8 / 10;
1674 AllocProp.cbPrefix = -1;
1675 AllocProp.cBuffers = -1;
1676 p_ambuf->SuggestAllocatorProperties( &AllocProp );
1680 else FreeMediaType( *p_mt );
1682 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Stream )
1684 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1685 "accepts stream format: %4.4s",
1686 (char *)&i_current_fourcc );
1688 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1692 mt[mt_count++] = *p_mt;
1693 i_fourcc = i_current_fourcc;
1695 else FreeMediaType( *p_mt );
1699 const char * psz_type = "unknown";
1700 if( p_mt->majortype == MEDIATYPE_Video ) psz_type = "video";
1701 if( p_mt->majortype == MEDIATYPE_Audio ) psz_type = "audio";
1702 if( p_mt->majortype == MEDIATYPE_Stream ) psz_type = "stream";
1703 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: unsupported format "
1704 "(%s %4.4s)", psz_type, (char *)&p_mt->subtype );
1706 FreeMediaType( *p_mt );
1708 CoTaskMemFree( (PVOID)p_mt );
1711 if( !mt_count && p_enummt->Reset() == S_OK )
1713 // VLC did not find any supported MEDIATYPE for this output pin.
1714 // However the graph builder might insert converter filters in
1715 // the graph if we use a different codec in VLC filter input pin.
1716 // however, in order to avoid nasty surprises, make use of this
1717 // facility only for known unsupported codecs.
1719 while( !mt_count && p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1721 // the first four bytes of subtype GUID contains the codec FOURCC
1722 const char *pfcc = (char *)&p_mt->subtype;
1723 int i_current_fourcc = VLC_FOURCC(pfcc[0], pfcc[1], pfcc[2], pfcc[3]);
1724 if( VLC_FOURCC('H','C','W','2') == i_current_fourcc
1725 && p_mt->majortype == MEDIATYPE_Video )
1727 // output format for 'Hauppauge WinTV PVR PCI II Capture'
1728 // try I420 as an input format
1729 i_current_fourcc = VLC_CODEC_I420;
1730 if( !i_fourcc || i_fourcc == i_current_fourcc )
1732 // return alternative media type
1736 mtr.majortype = MEDIATYPE_Video;
1737 mtr.subtype = MEDIASUBTYPE_I420;
1738 mtr.bFixedSizeSamples = TRUE;
1739 mtr.bTemporalCompression = FALSE;
1741 mtr.formattype = FORMAT_VideoInfo;
1742 mtr.cbFormat = sizeof(vh);
1743 mtr.pbFormat = (BYTE *)&vh;
1745 memset(&vh, 0, sizeof(vh));
1747 vh.bmiHeader.biSize = sizeof(vh.bmiHeader);
1748 vh.bmiHeader.biWidth = i_width > 0 ? i_width :
1749 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1750 vh.bmiHeader.biHeight = i_height > 0 ? i_height :
1751 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1752 vh.bmiHeader.biPlanes = 3;
1753 vh.bmiHeader.biBitCount = 12;
1754 vh.bmiHeader.biCompression = VLC_CODEC_I420;
1755 vh.bmiHeader.biSizeImage = vh.bmiHeader.biWidth * 12 *
1756 vh.bmiHeader.biHeight / 8;
1757 mtr.lSampleSize = vh.bmiHeader.biSizeImage;
1759 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: using 'I420' in place of unsupported format 'HCW2'");
1761 if( SUCCEEDED(CopyMediaType(mt+mt_count, &mtr)) )
1765 FreeMediaType( *p_mt );
1769 p_enummt->Release();
1770 p_output_pin->Release();
1773 p_enumpins->Release();
1777 /*****************************************************************************
1778 * ReadCompressed: reads compressed (MPEG/DV) data from the device.
1779 *****************************************************************************
1780 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1782 *****************************************************************************/
1783 static block_t *ReadCompressed( access_t *p_access )
1785 access_sys_t *p_sys = p_access->p_sys;
1786 dshow_stream_t *p_stream = NULL;
1787 VLCMediaSample sample;
1789 /* Read 1 DV/MPEG frame (they contain the video and audio data) */
1791 /* There must be only 1 elementary stream to produce a valid stream
1792 * of MPEG or DV data */
1793 p_stream = p_sys->pp_streams[0];
1797 if( !vlc_object_alive (p_access) ) return NULL;
1799 /* Get new sample/frame from the elementary stream (blocking). */
1800 vlc_mutex_lock( &p_sys->lock );
1802 if( p_stream->p_capture_filter->CustomGetPin()
1803 ->CustomGetSample( &sample ) != S_OK )
1805 /* No data available. Wait until some data has arrived */
1806 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1807 vlc_mutex_unlock( &p_sys->lock );
1811 vlc_mutex_unlock( &p_sys->lock );
1818 int i_data_size = sample.p_sample->GetActualDataLength();
1820 if( !i_data_size || !(p_block = block_New( p_access, i_data_size )) )
1822 sample.p_sample->Release();
1826 sample.p_sample->GetPointer( &p_data );
1827 memcpy( p_block->p_buffer, p_data, i_data_size );
1828 sample.p_sample->Release();
1830 /* The caller got what he wanted */
1834 return NULL; /* never reached */
1837 /****************************************************************************
1839 ****************************************************************************/
1840 static int Demux( demux_t *p_demux )
1842 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
1843 int i_found_samples;
1845 i_found_samples = 0;
1846 vlc_mutex_lock( &p_sys->lock );
1848 while ( !i_found_samples )
1850 /* Try to grab samples from all streams */
1851 for( int i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1853 dshow_stream_t *p_stream = p_sys->pp_streams[i_stream];
1854 if( p_stream->p_capture_filter &&
1855 p_stream->p_capture_filter->CustomGetPin()
1856 ->CustomGetSamples( p_stream->samples_queue ) == S_OK )
1858 i_found_samples = 1;
1862 if ( !i_found_samples)
1864 /* Didn't find any audio nor video sample, just wait till the
1865 * dshow thread pushes some samples */
1866 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1867 /* Some DShow thread pushed data, or the OS broke the wait all
1868 * by itself. In all cases, it's *strongly* advised to test the
1869 * condition again, so let the loop do the test again */
1873 vlc_mutex_unlock( &p_sys->lock );
1875 for ( int i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1878 dshow_stream_t *p_stream = p_sys->pp_streams[i_stream];
1880 i_samples = p_stream->samples_queue.size();
1881 while ( i_samples > 0 )
1886 VLCMediaSample sample;
1888 sample = p_stream->samples_queue.front();
1889 p_stream->samples_queue.pop_front();
1891 i_data_size = sample.p_sample->GetActualDataLength();
1892 sample.p_sample->GetPointer( &p_data );
1894 REFERENCE_TIME i_pts, i_end_date;
1895 HRESULT hr = sample.p_sample->GetTime( &i_pts, &i_end_date );
1896 if( hr != VFW_S_NO_STOP_TIME && hr != S_OK ) i_pts = 0;
1900 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
1902 /* Use our data timestamp */
1903 i_pts = sample.i_timestamp;
1904 p_stream->b_pts = true;
1908 i_pts /= 10; /* Dshow works with 100 nano-seconds resolution */
1911 msg_Dbg( p_demux, "Read() stream: %i, size: %i, PTS: %"PRId64,
1912 i_stream, i_data_size, i_pts );
1915 p_block = block_New( p_demux, i_data_size );
1916 memcpy( p_block->p_buffer, p_data, i_data_size );
1917 p_block->i_pts = p_block->i_dts = i_pts;
1918 sample.p_sample->Release();
1920 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pts > 0 ? i_pts : 0 );
1921 es_out_Send( p_demux->out, p_stream->p_es, p_block );
1930 /*****************************************************************************
1932 *****************************************************************************/
1933 static int AccessControl( access_t *p_access, int i_query, va_list args )
1941 case ACCESS_CAN_SEEK:
1942 case ACCESS_CAN_FASTSEEK:
1943 case ACCESS_CAN_PAUSE:
1944 case ACCESS_CAN_CONTROL_PACE:
1945 pb_bool = (bool*)va_arg( args, bool* );
1950 case ACCESS_GET_PTS_DELAY:
1951 pi_64 = (int64_t*)va_arg( args, int64_t * );
1953 INT64_C(1000) * var_InheritInteger( p_access, "live-caching" );
1957 case ACCESS_SET_PAUSE_STATE:
1958 case ACCESS_GET_TITLE_INFO:
1959 case ACCESS_SET_TITLE:
1960 case ACCESS_SET_SEEKPOINT:
1961 case ACCESS_SET_PRIVATE_ID_STATE:
1962 return VLC_EGENERIC;
1965 msg_Warn( p_access, "unimplemented query in control" );
1966 return VLC_EGENERIC;
1972 /****************************************************************************
1974 ****************************************************************************/
1975 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1982 /* Special for access_demux */
1983 case DEMUX_CAN_PAUSE:
1984 case DEMUX_CAN_SEEK:
1985 case DEMUX_SET_PAUSE_STATE:
1986 case DEMUX_CAN_CONTROL_PACE:
1987 pb = (bool*)va_arg( args, bool * );
1991 case DEMUX_GET_PTS_DELAY:
1992 pi64 = (int64_t*)va_arg( args, int64_t * );
1994 INT64_C(1000) * var_InheritInteger( p_demux, "live-caching" );
1997 case DEMUX_GET_TIME:
1998 pi64 = (int64_t*)va_arg( args, int64_t * );
2002 /* TODO implement others */
2004 return VLC_EGENERIC;
2007 return VLC_EGENERIC;
2010 /*****************************************************************************
2011 * config variable callback
2012 *****************************************************************************/
2013 static int FindDevices( vlc_object_t *p_this, const char *psz_name,
2014 char ***vp, char ***tp )
2016 bool b_audio = !strcmp( psz_name, CFG_PREFIX "adev" );
2018 /* Find list of devices */
2019 list<string> list_devices;
2021 /* Initialize OLE/COM */
2022 CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
2024 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
2026 /* Uninitialize OLE/COM */
2029 unsigned count = 2 + list_devices.size(), i = 2;
2030 char **values = (char **)xmalloc( count * sizeof(*values) );
2031 char **texts = (char **)xmalloc( count * sizeof(*texts) );
2033 values[0] = strdup( "" );
2034 texts[0] = strdup( N_("Default") );
2035 values[1] = strdup( "none" );
2036 texts[1] = strdup( N_("None") );
2038 for( list<string>::iterator iter = list_devices.begin();
2039 iter != list_devices.end();
2042 assert( i < count );
2043 values[i] = strdup( iter->c_str() );
2044 texts[i] = strdup( iter->c_str() );
2053 static int ConfigDevicesCallback( vlc_object_t *p_this, char const *psz_name,
2054 vlc_value_t newval, vlc_value_t, void * )
2056 module_config_t *p_item;
2057 bool b_audio = false;
2058 char *psz_device = NULL;
2059 int i_ret = VLC_SUCCESS;
2061 if( !EMPTY_STR( newval.psz_string ) )
2062 psz_device = strdup( newval.psz_string );
2064 /* Initialize OLE/COM */
2065 CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
2067 p_item = config_FindConfig( p_this, psz_name );
2076 if( !strcmp( psz_name, CFG_PREFIX "adev" ) ) b_audio = true;
2082 devicename = psz_device ;
2086 /* If no device name was specified, pick the 1st one */
2087 list<string> list_devices;
2089 /* Enumerate devices */
2090 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
2091 if( list_devices.empty() )
2094 return VLC_EGENERIC;
2096 devicename = *list_devices.begin();
2099 IBaseFilter *p_device_filter =
2100 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
2101 if( p_device_filter )
2103 ShowPropertyPage( p_device_filter );
2104 p_device_filter->Release();
2108 msg_Err( p_this, "didn't find device: %s", devicename.c_str() );
2109 i_ret = VLC_EGENERIC;
2112 /* Uninitialize OLE/COM */
2119 /*****************************************************************************
2121 *****************************************************************************/
2123 static void ShowPropertyPage( IUnknown *obj )
2125 ISpecifyPropertyPages *p_spec;
2128 HRESULT hr = obj->QueryInterface( IID_ISpecifyPropertyPages,
2130 if( FAILED(hr) ) return;
2132 if( SUCCEEDED(p_spec->GetPages( &cauuid )) )
2134 if( cauuid.cElems > 0 )
2136 HWND hwnd_desktop = ::GetDesktopWindow();
2138 OleCreatePropertyFrame( hwnd_desktop, 30, 30, NULL, 1, &obj,
2139 cauuid.cElems, cauuid.pElems, 0, 0, NULL );
2141 CoTaskMemFree( cauuid.pElems );
2147 static void ShowDeviceProperties( vlc_object_t *p_this,
2148 ICaptureGraphBuilder2 *p_graph,
2149 IBaseFilter *p_device_filter,
2153 msg_Dbg( p_this, "configuring Device Properties" );
2156 * Video or audio capture filter page
2158 ShowPropertyPage( p_device_filter );
2163 if( p_graph && b_audio )
2165 IAMStreamConfig *p_SC;
2167 msg_Dbg( p_this, "showing WDM Audio Configuration Pages" );
2169 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2170 &MEDIATYPE_Audio, p_device_filter,
2171 IID_IAMStreamConfig, (void **)&p_SC );
2174 ShowPropertyPage(p_SC);
2182 HRESULT hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2183 &MEDIATYPE_Audio, p_device_filter,
2184 IID_IAMTVAudio, (void **)&p_TVA );
2187 ShowPropertyPage(p_TVA);
2195 if( p_graph && !b_audio )
2197 IAMStreamConfig *p_SC;
2199 msg_Dbg( p_this, "showing WDM Video Configuration Pages" );
2201 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2202 &MEDIATYPE_Interleaved, p_device_filter,
2203 IID_IAMStreamConfig, (void **)&p_SC );
2206 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2207 &MEDIATYPE_Video, p_device_filter,
2208 IID_IAMStreamConfig, (void **)&p_SC );
2213 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2214 &MEDIATYPE_Stream, p_device_filter,
2215 IID_IAMStreamConfig, (void **)&p_SC );
2220 ShowPropertyPage(p_SC);
2226 static void ShowTunerProperties( vlc_object_t *p_this,
2227 ICaptureGraphBuilder2 *p_graph,
2228 IBaseFilter *p_device_filter,
2232 msg_Dbg( p_this, "configuring Tuner Properties" );
2234 if( !p_graph || b_audio ) return;
2237 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2238 &MEDIATYPE_Interleaved, p_device_filter,
2239 IID_IAMTVTuner, (void **)&p_TV );
2242 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2243 &MEDIATYPE_Video, p_device_filter,
2244 IID_IAMTVTuner, (void **)&p_TV );
2249 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2250 &MEDIATYPE_Stream, p_device_filter,
2251 IID_IAMTVTuner, (void **)&p_TV );
2256 ShowPropertyPage(p_TV);
2261 static void ConfigTuner( vlc_object_t *p_this, ICaptureGraphBuilder2 *p_graph,
2262 IBaseFilter *p_device_filter )
2264 int i_channel, i_country, i_input, i_amtuner_mode, i_standard;
2266 unsigned i_frequency;
2270 if( !p_graph ) return;
2272 i_channel = var_GetInteger( p_this, CFG_PREFIX "tuner-channel" );
2273 i_country = var_GetInteger( p_this, CFG_PREFIX "tuner-country" );
2274 i_input = var_GetInteger( p_this, CFG_PREFIX "tuner-input" );
2275 i_amtuner_mode = var_GetInteger( p_this, CFG_PREFIX "amtuner-mode" );
2276 i_frequency = var_GetInteger( p_this, CFG_PREFIX "tuner-frequency" );
2277 i_standard = var_GetInteger( p_this, CFG_PREFIX "tuner-standard" );
2279 if( !i_channel && !i_frequency && !i_country && !i_input ) return; /* Nothing to do */
2281 msg_Dbg( p_this, "tuner config: channel %i, frequency %i, country %i, input type %i, standard %s",
2282 i_channel, i_frequency, i_country, i_input, ppsz_standards_list_text[i_standard] );
2285 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Interleaved,
2286 p_device_filter, IID_IAMTVTuner,
2290 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
2291 p_device_filter, IID_IAMTVTuner,
2297 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Stream,
2298 p_device_filter, IID_IAMTVTuner,
2304 msg_Dbg( p_this, "couldn't find tuner interface" );
2308 hr = p_TV->GetAvailableModes( &l_modes );
2309 if( SUCCEEDED(hr) && (l_modes & i_amtuner_mode) )
2311 hr = p_TV->put_Mode( (AMTunerModeType)i_amtuner_mode );
2314 if( i_input == 1 ) p_TV->put_InputType( 0, TunerInputCable );
2315 else if( i_input == 2 ) p_TV->put_InputType( 0, TunerInputAntenna );
2317 p_TV->put_CountryCode( i_country );
2319 if( i_frequency <= 0 ) p_TV->put_Channel( i_channel, AMTUNER_SUBCHAN_NO_TUNE,
2320 AMTUNER_SUBCHAN_NO_TUNE );
2322 if( i_frequency > 0 || i_standard > 0) {
2323 IKsPropertySet *pKs = NULL;
2324 DWORD dw_supported = 0;
2325 KSPROPERTY_TUNER_MODE_CAPS_S ModeCaps;
2326 KSPROPERTY_TUNER_FREQUENCY_S Frequency;
2327 KSPROPERTY_TUNER_STANDARD_S Standard;
2329 hr = p_TV->QueryInterface(IID_IKsPropertySet,(void **)&pKs);
2332 msg_Dbg( p_this, "Couldn't QI for IKsPropertySet" );
2337 memset(&ModeCaps,0,sizeof(KSPROPERTY_TUNER_MODE_CAPS_S));
2338 memset(&Frequency,0,sizeof(KSPROPERTY_TUNER_FREQUENCY_S));
2339 memset(&Standard,0,sizeof(KSPROPERTY_TUNER_STANDARD_S));
2340 ModeCaps.Mode = AMTUNER_MODE_TV;
2342 hr = pKs->QuerySupported(PROPSETID_TUNER,
2343 KSPROPERTY_TUNER_MODE_CAPS,&dw_supported);
2344 if(SUCCEEDED(hr) && dw_supported&KSPROPERTY_SUPPORT_GET)
2347 hr = pKs->Get(PROPSETID_TUNER,KSPROPERTY_TUNER_MODE_CAPS,
2348 INSTANCEDATA_OF_PROPERTY_PTR(&ModeCaps),
2349 INSTANCEDATA_OF_PROPERTY_SIZE(ModeCaps),
2356 msg_Dbg( p_this, "KSPROPERTY_TUNER_MODE_CAPS not supported!" );
2360 msg_Dbg( p_this, "Frequency range supported from %ld to %ld.",
2361 ModeCaps.MinFrequency, ModeCaps.MaxFrequency);
2362 msg_Dbg( p_this, "Video standards supported by the tuner: ");
2363 for(size_t i = 0 ; i < ARRAY_SIZE(ppsz_standards_list_text); i++) {
2364 if(ModeCaps.StandardsSupported & i_standards_list[i])
2365 msg_Dbg( p_this, "%s, ", ppsz_standards_list_text[i]);
2368 if(i_frequency > 0) {
2369 Frequency.Frequency=i_frequency;
2370 if(ModeCaps.Strategy==KS_TUNER_STRATEGY_DRIVER_TUNES)
2371 Frequency.TuningFlags=KS_TUNER_TUNING_FINE;
2373 Frequency.TuningFlags=KS_TUNER_TUNING_EXACT;
2375 if(i_frequency>=ModeCaps.MinFrequency && i_frequency<=ModeCaps.MaxFrequency)
2378 hr = pKs->Set(PROPSETID_TUNER,
2379 KSPROPERTY_TUNER_FREQUENCY,
2380 INSTANCEDATA_OF_PROPERTY_PTR(&Frequency),
2381 INSTANCEDATA_OF_PROPERTY_SIZE(Frequency),
2386 msg_Dbg( p_this, "Couldn't set KSPROPERTY_TUNER_FREQUENCY!" );
2392 msg_Dbg( p_this, "Requested frequency exceeds the supported range!" );
2397 if(i_standard > 0) {
2398 if(i_standard & ModeCaps.StandardsSupported )
2400 Standard.Standard = i_standard;
2401 hr = pKs->Set(PROPSETID_TUNER,
2402 KSPROPERTY_TUNER_STANDARD,
2403 INSTANCEDATA_OF_PROPERTY_PTR(&Standard),
2404 INSTANCEDATA_OF_PROPERTY_SIZE(Standard),
2409 msg_Dbg( p_this, "Couldn't set KSPROPERTY_TUNER_STANDARD!" );
2415 msg_Dbg( p_this, "Requested video standard is not supported by the tuner!" );