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-"
40 #include <vlc_common.h>
41 #include <vlc_plugin.h>
42 #include <vlc_access.h>
43 #include <vlc_demux.h>
45 #include <vlc_dialog.h> /* dialog_Fatal */
46 #include <vlc_charset.h> /* FromWide */
49 #include "vlc_dshow.h"
54 #define INSTANCEDATA_OF_PROPERTY_PTR(x) ((PKSPROPERTY((x))) + 1)
55 #define INSTANCEDATA_OF_PROPERTY_SIZE(x) (sizeof((x)) - sizeof(KSPROPERTY))
57 /*****************************************************************************
58 * Access: local prototypes
59 *****************************************************************************/
60 static block_t *ReadCompressed( access_t * );
61 static int AccessControl ( access_t *, int, va_list );
63 static int Demux ( demux_t * );
64 static int DemuxControl( demux_t *, int, va_list );
66 static int OpenDevice( vlc_object_t *, access_sys_t *, string, bool );
67 static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
68 list<string> *, bool );
69 static size_t EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
70 int, int, int, int, int, int,
71 AM_MEDIA_TYPE *mt, size_t );
72 static bool ConnectFilters( vlc_object_t *, access_sys_t *,
73 IBaseFilter *, CaptureFilter * );
74 static int FindDevicesCallback( vlc_object_t *, char const *,
75 vlc_value_t, vlc_value_t, void * );
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 char *const ppsz_vdev[] = { "", "none" };
91 static const char *const ppsz_vdev_text[] = { N_("Default"), N_("None") };
93 static const char *const ppsz_adev[] = { "", "none" };
94 static const char *const ppsz_adev_text[] = { N_("Default"), N_("None") };
96 static const int pi_tuner_input[] = { 0, 1, 2 };
97 static const char *const ppsz_tuner_input_text[] =
98 {N_("Default"), N_("Cable"), N_("Antenna")};
100 static const int pi_amtuner_mode[] = { AMTUNER_MODE_DEFAULT,
102 AMTUNER_MODE_FM_RADIO,
103 AMTUNER_MODE_AM_RADIO,
105 static const char *const ppsz_amtuner_mode_text[] = { N_("Default"),
111 static const int i_standards_list[] =
114 KS_AnalogVideo_NTSC_M, KS_AnalogVideo_NTSC_M_J, KS_AnalogVideo_NTSC_433,
115 KS_AnalogVideo_PAL_B, KS_AnalogVideo_PAL_D, KS_AnalogVideo_PAL_G,
116 KS_AnalogVideo_PAL_H, KS_AnalogVideo_PAL_I, KS_AnalogVideo_PAL_M,
117 KS_AnalogVideo_PAL_N, KS_AnalogVideo_PAL_60,
118 KS_AnalogVideo_SECAM_B, KS_AnalogVideo_SECAM_D, KS_AnalogVideo_SECAM_G,
119 KS_AnalogVideo_SECAM_H, KS_AnalogVideo_SECAM_K, KS_AnalogVideo_SECAM_K1,
120 KS_AnalogVideo_SECAM_L, KS_AnalogVideo_SECAM_L1,
121 KS_AnalogVideo_PAL_N_COMBO
123 static const char *const ppsz_standards_list_text[] =
126 "NTSC_M", "NTSC_M_J", "NTSC_443",
127 "PAL_B", "PAL_D", "PAL_G",
128 "PAL_H", "PAL_I", "PAL_M",
130 "SECAM_B", "SECAM_D", "SECAM_G",
131 "SECAM_H", "SECAM_K", "SECAM_K1",
132 "SECAM_L", "SECAM_L1",
136 #define VDEV_TEXT N_("Video device name")
137 #define VDEV_LONGTEXT N_( \
138 "Name of the video device that will be used by the " \
139 "DirectShow plugin. If you don't specify anything, the default device " \
141 #define ADEV_TEXT N_("Audio device name")
142 #define ADEV_LONGTEXT N_( \
143 "Name of the audio device that will be used by the " \
144 "DirectShow plugin. If you don't specify anything, the default device " \
146 #define SIZE_TEXT N_("Video size")
147 #define SIZE_LONGTEXT N_( \
148 "Size of the video that will be displayed by the " \
149 "DirectShow plugin. If you don't specify anything the default size for " \
150 "your device will be used. You can specify a standard size (cif, d1, ...) or <width>x<height>.")
151 #define ASPECT_TEXT N_("Picture aspect-ratio n:m")
152 #define ASPECT_LONGTEXT N_("Define input picture aspect-ratio to use. Default is 4:3" )
153 #define CHROMA_TEXT N_("Video input chroma format")
154 #define CHROMA_LONGTEXT N_( \
155 "Force the DirectShow video input to use a specific chroma format " \
156 "(eg. I420 (default), RV24, etc.)")
157 #define FPS_TEXT N_("Video input frame rate")
158 #define FPS_LONGTEXT N_( \
159 "Force the DirectShow video input to use a specific frame rate" \
160 "(eg. 0 means default, 25, 29.97, 50, 59.94, etc.)")
161 #define CONFIG_TEXT N_("Device properties")
162 #define CONFIG_LONGTEXT N_( \
163 "Show the properties dialog of the selected device before starting the " \
165 #define TUNER_TEXT N_("Tuner properties")
166 #define TUNER_LONGTEXT N_( \
167 "Show the tuner properties [channel selection] page." )
168 #define CHANNEL_TEXT N_("Tuner TV Channel")
169 #define CHANNEL_LONGTEXT N_( \
170 "Set the TV channel the tuner will set to " \
171 "(0 means default)." )
172 #define TVFREQ_TEXT N_("Tuner Frequency")
173 #define TVFREQ_LONGTEXT N_( "This overrides the channel. Measured in Hz." )
174 #define STANDARD_TEXT N_( "Video standard" )
175 #define COUNTRY_TEXT N_("Tuner country code")
176 #define COUNTRY_LONGTEXT N_( \
177 "Set the tuner country code that establishes the current " \
178 "channel-to-frequency mapping (0 means default)." )
179 #define TUNER_INPUT_TEXT N_("Tuner input type")
180 #define TUNER_INPUT_LONGTEXT N_( \
181 "Select the tuner input type (Cable/Antenna)." )
182 #define VIDEO_IN_TEXT N_("Video input pin")
183 #define VIDEO_IN_LONGTEXT N_( \
184 "Select the video input source, such as composite, s-video, " \
185 "or tuner. Since these settings are hardware-specific, you should find good " \
186 "settings in the \"Device config\" area, and use those numbers here. -1 " \
187 "means that settings will not be changed.")
188 #define AUDIO_IN_TEXT N_("Audio input pin")
189 #define AUDIO_IN_LONGTEXT N_( \
190 "Select the audio input source. See the \"video input\" option." )
191 #define VIDEO_OUT_TEXT N_("Video output pin")
192 #define VIDEO_OUT_LONGTEXT N_( \
193 "Select the video output type. See the \"video input\" option." )
194 #define AUDIO_OUT_TEXT N_("Audio output pin")
195 #define AUDIO_OUT_LONGTEXT N_( \
196 "Select the audio output type. See the \"video input\" option." )
198 #define AMTUNER_MODE_TEXT N_("AM Tuner mode")
199 #define AMTUNER_MODE_LONGTEXT N_( \
200 "AM Tuner mode. Can be one of Default (0), TV (1)," \
201 "AM Radio (2), FM Radio (3) or DSS (4).")
203 #define AUDIO_CHANNELS_TEXT N_("Number of audio channels")
204 #define AUDIO_CHANNELS_LONGTEXT N_( \
205 "Select audio input format with the given number of audio channels (if non 0)" )
207 #define AUDIO_SAMPLERATE_TEXT N_("Audio sample rate")
208 #define AUDIO_SAMPLERATE_LONGTEXT N_( \
209 "Select audio input format with the given sample rate (if non 0)" )
211 #define AUDIO_BITSPERSAMPLE_TEXT N_("Audio bits per sample")
212 #define AUDIO_BITSPERSAMPLE_LONGTEXT N_( \
213 "Select audio input format with the given bits/sample (if non 0)" )
215 static int CommonOpen ( vlc_object_t *, access_sys_t *, bool );
216 static void CommonClose( vlc_object_t *, access_sys_t * );
218 static int AccessOpen ( vlc_object_t * );
219 static void AccessClose( vlc_object_t * );
221 static int DemuxOpen ( vlc_object_t * );
222 static void DemuxClose ( vlc_object_t * );
225 set_shortname( N_("DirectShow") )
226 set_description( N_("DirectShow input") )
227 set_category( CAT_INPUT )
228 set_subcategory( SUBCAT_INPUT_ACCESS )
230 add_string( CFG_PREFIX "vdev", NULL, VDEV_TEXT, VDEV_LONGTEXT, false)
231 change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback )
232 change_action_add( FindDevicesCallback, N_("Refresh list") )
233 change_action_add( ConfigDevicesCallback, N_("Configure") )
235 add_string( CFG_PREFIX "adev", NULL, ADEV_TEXT, ADEV_LONGTEXT, false)
236 change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback )
237 change_action_add( FindDevicesCallback, N_("Refresh list") )
238 change_action_add( ConfigDevicesCallback, N_("Configure") )
240 add_string( CFG_PREFIX "size", NULL, SIZE_TEXT, SIZE_LONGTEXT, false)
243 add_string( CFG_PREFIX "aspect-ratio", "4:3", ASPECT_TEXT, ASPECT_LONGTEXT, false)
246 add_string( CFG_PREFIX "chroma", NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true )
249 add_float( CFG_PREFIX "fps", 0.0f, FPS_TEXT, FPS_LONGTEXT, true )
252 add_bool( CFG_PREFIX "config", false, CONFIG_TEXT, CONFIG_LONGTEXT, true )
254 add_bool( CFG_PREFIX "tuner", false, TUNER_TEXT, TUNER_LONGTEXT, true )
256 add_integer( CFG_PREFIX "tuner-channel", 0, CHANNEL_TEXT, CHANNEL_LONGTEXT,
260 add_integer( CFG_PREFIX "tuner-frequency", 0, TVFREQ_TEXT, TVFREQ_LONGTEXT,
264 add_integer( CFG_PREFIX "tuner-country", 0, COUNTRY_TEXT, COUNTRY_LONGTEXT,
267 add_integer( CFG_PREFIX "tuner-standard", 0, STANDARD_TEXT, STANDARD_TEXT,
269 change_integer_list( i_standards_list, ppsz_standards_list_text )
272 add_integer( CFG_PREFIX "tuner-input", 0, TUNER_INPUT_TEXT,
273 TUNER_INPUT_LONGTEXT, true )
274 change_integer_list( pi_tuner_input, ppsz_tuner_input_text )
277 add_integer( CFG_PREFIX "video-input", -1, VIDEO_IN_TEXT,
278 VIDEO_IN_LONGTEXT, true )
281 add_integer( CFG_PREFIX "video-output", -1, VIDEO_OUT_TEXT,
282 VIDEO_OUT_LONGTEXT, true )
284 add_integer( CFG_PREFIX "audio-input", -1, AUDIO_IN_TEXT,
285 AUDIO_IN_LONGTEXT, true )
288 add_integer( CFG_PREFIX "audio-output", -1, AUDIO_OUT_TEXT,
289 AUDIO_OUT_LONGTEXT, true )
291 add_integer( CFG_PREFIX "amtuner-mode", AMTUNER_MODE_TV,
292 AMTUNER_MODE_TEXT, AMTUNER_MODE_LONGTEXT, false)
293 change_integer_list( pi_amtuner_mode, ppsz_amtuner_mode_text )
296 add_integer( CFG_PREFIX "audio-channels", 0, AUDIO_CHANNELS_TEXT,
297 AUDIO_CHANNELS_LONGTEXT, true )
298 add_integer( CFG_PREFIX "audio-samplerate", 0, AUDIO_SAMPLERATE_TEXT,
299 AUDIO_SAMPLERATE_LONGTEXT, true )
300 add_integer( CFG_PREFIX "audio-bitspersample", 0, AUDIO_BITSPERSAMPLE_TEXT,
301 AUDIO_BITSPERSAMPLE_LONGTEXT, true )
303 add_shortcut( "dshow" )
304 set_capability( "access_demux", 0 )
305 set_callbacks( DemuxOpen, DemuxClose )
308 set_description( N_("DirectShow input") )
309 add_shortcut( "dshow" )
310 set_capability( "access", 0 )
311 set_callbacks( AccessOpen, AccessClose )
316 /*****************************************************************************
317 * DirectShow elementary stream descriptor
318 *****************************************************************************/
319 typedef struct dshow_stream_t
322 IBaseFilter *p_device_filter;
323 CaptureFilter *p_capture_filter;
328 VIDEOINFOHEADER video;
338 deque<VLCMediaSample> samples_queue;
341 /*****************************************************************************
342 * DirectShow utility functions
343 *****************************************************************************/
344 static void CreateDirectShowGraph( access_sys_t *p_sys )
346 p_sys->i_crossbar_route_depth = 0;
348 /* Create directshow filter graph */
349 if( SUCCEEDED( CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
350 (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph) ) )
352 /* Create directshow capture graph builder if available */
353 if( SUCCEEDED( CoCreateInstance( CLSID_CaptureGraphBuilder2, 0,
354 CLSCTX_INPROC, (REFIID)IID_ICaptureGraphBuilder2,
355 (void **)&p_sys->p_capture_graph_builder2 ) ) )
357 p_sys->p_capture_graph_builder2->
358 SetFiltergraph((IGraphBuilder *)p_sys->p_graph);
361 p_sys->p_graph->QueryInterface( IID_IMediaControl,
362 (void **)&p_sys->p_control );
366 static void DeleteDirectShowGraph( vlc_object_t *p_this, access_sys_t *p_sys )
368 DeleteCrossbarRoutes( p_sys );
370 /* Remove filters from graph */
371 msg_Dbg( p_this, "DeleteDirectShowGraph: Removing filters" );
372 for( int i = 0; i < p_sys->i_streams; i++ )
374 /* RemoveFilter does an undocumented Release()
375 * but does not set item to NULL */
376 msg_Dbg( p_this, "DeleteDirectShowGraph: Removing capture filter" );
377 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_capture_filter );
378 p_sys->pp_streams[i]->p_capture_filter = NULL;
380 msg_Dbg( p_this, "DeleteDirectShowGraph: Removing device filter" );
381 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_device_filter );
382 p_sys->pp_streams[i]->p_device_filter = NULL;
385 /* Release directshow objects */
386 if( p_sys->p_control )
388 p_sys->p_control->Release();
389 p_sys->p_control = NULL;
391 if( p_sys->p_capture_graph_builder2 )
393 p_sys->p_capture_graph_builder2->Release();
394 p_sys->p_capture_graph_builder2 = NULL;
399 p_sys->p_graph->Release();
400 p_sys->p_graph = NULL;
404 /*****************************************************************************
405 * CommonOpen: open direct show device
406 *****************************************************************************/
407 static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
408 bool b_access_demux )
412 /* Get/parse options and open device(s) */
413 string vdevname, adevname;
414 int i_width = 0, i_height = 0;
415 vlc_fourcc_t i_chroma = 0;
416 bool b_use_audio = true;
417 bool b_use_video = true;
419 /* Initialize OLE/COM */
422 var_Create( p_this, CFG_PREFIX "config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
423 var_Create( p_this, CFG_PREFIX "tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
424 psz_val = var_CreateGetString( p_this, CFG_PREFIX "vdev" );
427 msg_Dbg( p_this, "dshow-vdev: %s", psz_val ) ;
428 /* skip none device */
429 if ( strncasecmp( psz_val, "none", 4 ) != 0 )
430 vdevname = string( psz_val );
432 b_use_video = false ;
436 psz_val = var_CreateGetString( p_this, CFG_PREFIX "adev" );
439 msg_Dbg( p_this, "dshow-adev: %s", psz_val ) ;
440 /* skip none device */
441 if ( strncasecmp( psz_val, "none", 4 ) != 0 )
442 adevname = string( psz_val );
444 b_use_audio = false ;
450 const char *psz_size;
454 { { "subqcif", 128, 96 },
455 { "qsif", 160, 120 },
456 { "qcif", 176, 144 },
463 psz_val = var_CreateGetString( p_this, CFG_PREFIX "size" );
464 if( !EMPTY_STR(psz_val) )
467 for( i = 0; size_table[i].psz_size; i++ )
469 if( !strcmp( psz_val, size_table[i].psz_size ) )
471 i_width = size_table[i].i_width;
472 i_height = size_table[i].i_height;
476 if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
479 i_width = strtol( psz_val, &psz_parser, 0 );
480 if( *psz_parser == 'x' || *psz_parser == 'X')
482 i_height = strtol( psz_parser + 1, &psz_parser, 0 );
484 msg_Dbg( p_this, "width x height %dx%d", i_width, i_height );
490 psz_val = var_CreateGetString( p_this, CFG_PREFIX "chroma" );
491 i_chroma = vlc_fourcc_GetCodecFromString( UNKNOWN_ES, psz_val );
492 p_sys->b_chroma = i_chroma != 0;
495 var_Create( p_this, CFG_PREFIX "fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
496 var_Create( p_this, CFG_PREFIX "tuner-channel",
497 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
498 var_Create( p_this, CFG_PREFIX "tuner-frequency",
499 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
500 var_Create( p_this, CFG_PREFIX "tuner-standard",
501 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
502 var_Create( p_this, CFG_PREFIX "tuner-country",
503 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
504 var_Create( p_this, CFG_PREFIX "tuner-input",
505 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
507 var_Create( p_this, CFG_PREFIX "amtuner-mode",
508 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
510 var_Create( p_this, CFG_PREFIX "video-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
511 var_Create( p_this, CFG_PREFIX "audio-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
512 var_Create( p_this, CFG_PREFIX "video-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
513 var_Create( p_this, CFG_PREFIX "audio-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
516 /* Initialize some data */
517 p_sys->i_streams = 0;
518 p_sys->pp_streams = NULL;
519 p_sys->i_width = i_width;
520 p_sys->i_height = i_height;
521 p_sys->i_chroma = i_chroma;
523 p_sys->p_graph = NULL;
524 p_sys->p_capture_graph_builder2 = NULL;
525 p_sys->p_control = NULL;
527 /* Build directshow graph */
528 CreateDirectShowGraph( p_sys );
530 vlc_mutex_init( &p_sys->lock );
531 vlc_cond_init( &p_sys->wait );
533 if( !b_use_video && !b_use_audio )
535 dialog_Fatal( p_this, _("Capture failed"),
536 _("No video or audio device selected.") );
537 return VLC_EGENERIC ;
541 msg_Dbg( p_this, "skipping video device" ) ;
542 bool b_err_video = false ;
544 if( b_use_video && OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
546 msg_Err( p_this, "can't open video device");
550 if ( b_use_video && !b_err_video )
552 /* Check if we can handle the demuxing ourselves or need to spawn
553 * a demuxer module */
554 dshow_stream_t *p_stream = p_sys->pp_streams[p_sys->i_streams-1];
556 if( p_stream->mt.majortype == MEDIATYPE_Video )
558 if( /* Raw DV stream */
559 p_stream->i_fourcc == VLC_CODEC_DV ||
560 /* Raw MPEG video stream */
561 p_stream->i_fourcc == VLC_CODEC_MPGV )
567 /* Let the access (only) take care of that */
573 if( p_stream->mt.majortype == MEDIATYPE_Stream )
579 /* Let the access (only) take care of that */
583 if( var_GetBool( p_this, CFG_PREFIX "tuner" ) )
585 /* FIXME: we do MEDIATYPE_Stream here so we don't do
587 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
588 p_stream->p_device_filter, 0 );
594 msg_Dbg( p_this, "skipping audio device") ;
596 bool b_err_audio = false ;
598 if( b_use_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
600 msg_Err( p_this, "can't open audio device");
604 if( ( b_use_video && b_err_video && b_use_audio && b_err_audio ) ||
605 ( !b_use_video && b_use_audio && b_err_audio ) ||
606 ( b_use_video && !b_use_audio && b_err_video ) )
608 msg_Err( p_this, "FATAL: could not open ANY device" ) ;
609 dialog_Fatal( p_this, _("Capture failed"),
610 _("VLC cannot open ANY capture device. "
611 "Check the error log for details.") );
612 return VLC_EGENERIC ;
615 for( int i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
617 int i_val = var_GetInteger( p_this, CFG_PREFIX "video-input" );
619 p_sys->crossbar_routes[i].VideoInputIndex = i_val;
620 i_val = var_GetInteger( p_this, CFG_PREFIX "video-output" );
622 p_sys->crossbar_routes[i].VideoOutputIndex = i_val;
623 i_val = var_GetInteger( p_this, CFG_PREFIX "audio-input" );
625 p_sys->crossbar_routes[i].AudioInputIndex = i_val;
626 i_val = var_GetInteger( p_this, CFG_PREFIX "audio-output" );
628 p_sys->crossbar_routes[i].AudioOutputIndex = i_val;
630 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
631 LONG VideoInputIndex = p_sys->crossbar_routes[i].VideoInputIndex;
632 LONG VideoOutputIndex = p_sys->crossbar_routes[i].VideoOutputIndex;
633 LONG AudioInputIndex = p_sys->crossbar_routes[i].AudioInputIndex;
634 LONG AudioOutputIndex = p_sys->crossbar_routes[i].AudioOutputIndex;
636 if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
638 msg_Dbg( p_this, "crossbar at depth %d, routed video "
639 "output %ld to video input %ld", i, VideoOutputIndex,
642 if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
644 if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
647 msg_Dbg(p_this, "crossbar at depth %d, routed audio "
648 "output %ld to audio input %ld", i,
649 AudioOutputIndex, AudioInputIndex );
654 msg_Err( p_this, "crossbar at depth %d could not route video "
655 "output %ld to input %ld", i, VideoOutputIndex, VideoInputIndex );
659 ** Show properties pages from other filters in graph
661 if( var_GetBool( p_this, CFG_PREFIX "config" ) )
663 for( int i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
665 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
668 if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
671 ShowPropertyPage( p_XF );
677 /* Initialize some data */
678 p_sys->i_current_stream = 0;
680 if( !p_sys->i_streams ) return VLC_EGENERIC;
685 /*****************************************************************************
686 * DemuxOpen: open direct show device as an access_demux module
687 *****************************************************************************/
688 static int DemuxOpen( vlc_object_t *p_this )
690 demux_t *p_demux = (demux_t *)p_this;
693 p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
696 p_demux->p_sys = (demux_sys_t *)p_sys;
698 if( CommonOpen( p_this, p_sys, true ) != VLC_SUCCESS )
700 CommonClose( p_this, p_sys );
704 /* Everything is ready. Let's rock baby */
705 msg_Dbg( p_this, "Playing...");
706 p_sys->p_control->Run();
708 p_demux->pf_demux = Demux;
709 p_demux->pf_control = DemuxControl;
710 p_demux->info.i_update = 0;
711 p_demux->info.i_title = 0;
712 p_demux->info.i_seekpoint = 0;
714 for( int i = 0; i < p_sys->i_streams; i++ )
716 dshow_stream_t *p_stream = p_sys->pp_streams[i];
719 if( p_stream->mt.majortype == MEDIATYPE_Video )
721 char *psz_aspect = var_CreateGetString( p_this, CFG_PREFIX "aspect-ratio" );
722 char *psz_delim = !EMPTY_STR( psz_aspect ) ? strchr( psz_aspect, ':' ) : NULL;
724 es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
726 fmt.video.i_width = p_stream->header.video.bmiHeader.biWidth;
727 fmt.video.i_height = p_stream->header.video.bmiHeader.biHeight;
731 fmt.video.i_sar_num = atoi( psz_aspect ) * fmt.video.i_height;
732 fmt.video.i_sar_den = atoi( psz_delim + 1 ) * fmt.video.i_width;
736 fmt.video.i_sar_num = 4 * fmt.video.i_height;
737 fmt.video.i_sar_den = 3 * fmt.video.i_width;
741 if( !p_stream->header.video.bmiHeader.biCompression )
743 /* RGB DIB are coded from bottom to top */
744 fmt.video.i_height = (unsigned int)(-(int)fmt.video.i_height);
747 /* Setup rgb mask for RGB formats */
748 if( p_stream->i_fourcc == VLC_CODEC_RGB24 )
750 /* This is in RGB format
751 http://msdn.microsoft.com/en-us/library/dd407253%28VS.85%29.aspx?ppud=4
753 fmt.video.i_rmask = 0x00ff0000;
754 fmt.video.i_gmask = 0x0000ff00;
755 fmt.video.i_bmask = 0x000000ff;
758 if( p_stream->header.video.AvgTimePerFrame )
760 fmt.video.i_frame_rate = 10000000;
761 fmt.video.i_frame_rate_base =
762 p_stream->header.video.AvgTimePerFrame;
765 else if( p_stream->mt.majortype == MEDIATYPE_Audio )
767 es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
769 fmt.audio.i_channels = p_stream->header.audio.nChannels;
770 fmt.audio.i_rate = p_stream->header.audio.nSamplesPerSec;
771 fmt.audio.i_bitspersample = p_stream->header.audio.wBitsPerSample;
772 fmt.audio.i_blockalign = fmt.audio.i_channels *
773 fmt.audio.i_bitspersample / 8;
774 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
775 fmt.audio.i_bitspersample;
778 p_stream->p_es = es_out_Add( p_demux->out, &fmt );
784 /*****************************************************************************
785 * AccessOpen: open direct show device as an access module
786 *****************************************************************************/
787 static int AccessOpen( vlc_object_t *p_this )
789 access_t *p_access = (access_t*)p_this;
792 p_access->p_sys = p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
796 if( CommonOpen( p_this, p_sys, false ) != VLC_SUCCESS )
798 CommonClose( p_this, p_sys );
802 dshow_stream_t *p_stream = p_sys->pp_streams[0];
804 /* Check if we need to force demuxers */
805 if( !p_access->psz_demux || !*p_access->psz_demux )
807 if( p_stream->i_fourcc == VLC_CODEC_DV )
809 free( p_access->psz_demux );
810 p_access->psz_demux = strdup( "rawdv" );
812 else if( p_stream->i_fourcc == VLC_CODEC_MPGV )
814 free( p_access->psz_demux );
815 p_access->psz_demux = strdup( "mpgv" );
820 p_access->pf_read = NULL;
821 p_access->pf_block = ReadCompressed;
822 p_access->pf_control = AccessControl;
823 p_access->pf_seek = NULL;
824 p_access->info.i_update = 0;
825 p_access->info.i_size = 0;
826 p_access->info.i_pos = 0;
827 p_access->info.b_eof = false;
828 p_access->info.i_title = 0;
829 p_access->info.i_seekpoint = 0;
830 p_access->p_sys = p_sys;
832 /* Everything is ready. Let's rock baby */
833 msg_Dbg( p_this, "Playing...");
834 p_sys->p_control->Run();
839 /*****************************************************************************
840 * CommonClose: close device
841 *****************************************************************************/
842 static void CommonClose( vlc_object_t *p_this, access_sys_t *p_sys )
844 msg_Dbg( p_this, "releasing DirectShow");
846 DeleteDirectShowGraph( p_this, p_sys );
848 /* Uninitialize OLE/COM */
851 for( int i = 0; i < p_sys->i_streams; i++ ) delete p_sys->pp_streams[i];
852 if( p_sys->i_streams ) free( p_sys->pp_streams );
854 vlc_mutex_destroy( &p_sys->lock );
855 vlc_cond_destroy( &p_sys->wait );
860 /*****************************************************************************
861 * AccessClose: close device
862 *****************************************************************************/
863 static void AccessClose( vlc_object_t *p_this )
865 access_t *p_access = (access_t *)p_this;
866 access_sys_t *p_sys = p_access->p_sys;
868 /* Stop capturing stuff */
869 p_sys->p_control->Stop();
871 CommonClose( p_this, p_sys );
874 /*****************************************************************************
875 * DemuxClose: close device
876 *****************************************************************************/
877 static void DemuxClose( vlc_object_t *p_this )
879 demux_t *p_demux = (demux_t *)p_this;
880 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
882 /* Stop capturing stuff */
883 p_sys->p_control->Stop();
885 CommonClose( p_this, p_sys );
888 /****************************************************************************
890 ****************************************************************************/
891 static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
892 IBaseFilter *p_filter,
893 CaptureFilter *p_capture_filter )
895 CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
897 AM_MEDIA_TYPE mediaType = p_input_pin->CustomGetMediaType();
899 if( p_sys->p_capture_graph_builder2 )
901 if( FAILED(p_sys->p_capture_graph_builder2->
902 RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
903 p_filter, 0, (IBaseFilter *)p_capture_filter )) )
908 // Sort out all the possible video inputs
909 // The class needs to be given the capture filters ANALOGVIDEO input pin
910 IEnumPins *pins = NULL;
911 if( ( mediaType.majortype == MEDIATYPE_Video ||
912 mediaType.majortype == MEDIATYPE_Stream ) &&
913 SUCCEEDED(p_filter->EnumPins(&pins)) )
919 IKsPropertySet *pKs = NULL;
923 while( !Found && ( S_OK == pins->Next(1, &pP, &n) ) )
925 if( S_OK == pP->QueryPinInfo(&pinInfo) )
927 // is this pin an ANALOGVIDEOIN input pin?
928 if( pinInfo.dir == PINDIR_INPUT &&
929 pP->QueryInterface( IID_IKsPropertySet,
930 (void **)&pKs ) == S_OK )
932 if( pKs->Get( AMPROPSETID_Pin,
933 AMPROPERTY_PIN_CATEGORY, NULL, 0,
934 &guid, sizeof(GUID), &dw ) == S_OK )
936 if( guid == PIN_CATEGORY_ANALOGVIDEOIN )
938 // recursively search crossbar routes
939 FindCrossbarRoutes( p_this, p_sys, pP, 0 );
946 pinInfo.pFilter->Release();
951 msg_Dbg( p_this, "ConnectFilters: graph_builder2 available.") ;
953 msg_Warn( p_this, "ConnectFilters: No crossBar routes found (incompatible pin types)" ) ;
959 IEnumPins *p_enumpins;
962 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;
964 while( S_OK == p_enumpins->Next( 1, &p_pin, NULL ) )
966 PIN_DIRECTION pin_dir;
967 p_pin->QueryDirection( &pin_dir );
969 if( pin_dir == PINDIR_OUTPUT &&
970 p_sys->p_graph->ConnectDirect( p_pin, (IPin *)p_input_pin,
974 p_enumpins->Release();
980 p_enumpins->Release();
986 * get fourcc priority from arbritary preference, the higher the better
988 static int GetFourCCPriority( int i_fourcc )
996 case VLC_FOURCC('a','r','a','w'):
998 case VLC_CODEC_RGB24:
1000 case VLC_CODEC_YUYV:
1001 case VLC_CODEC_RGB32:
1002 case VLC_CODEC_RGBA:
1009 #define MAX_MEDIA_TYPES 32
1011 static int OpenDevice( vlc_object_t *p_this, access_sys_t *p_sys,
1012 string devicename, bool b_audio )
1014 /* See if device is already opened */
1015 for( int i = 0; i < p_sys->i_streams; i++ )
1017 if( devicename.size() &&
1018 p_sys->pp_streams[i]->devicename == devicename )
1020 /* Already opened */
1025 list<string> list_devices;
1027 /* Enumerate devices and display their names */
1028 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1029 if( list_devices.empty() )
1030 return VLC_EGENERIC;
1032 list<string>::iterator iter;
1033 for( iter = list_devices.begin(); iter != list_devices.end(); ++iter )
1034 msg_Dbg( p_this, "found device: %s", iter->c_str() );
1036 /* If no device name was specified, pick the 1st one */
1037 if( devicename.size() == 0 )
1039 /* When none selected */
1040 devicename = *list_devices.begin();
1041 msg_Dbg( p_this, "asking for default device: %s", devicename.c_str() ) ;
1044 msg_Dbg( p_this, "asking for device: %s", devicename.c_str() ) ;
1045 // Use the system device enumerator and class enumerator to find
1046 // a capture/preview device, such as a desktop USB video camera.
1047 IBaseFilter *p_device_filter =
1048 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
1050 if( p_device_filter )
1051 msg_Dbg( p_this, "using device: %s", devicename.c_str() );
1054 msg_Err( p_this, "can't use device: %s, unsupported device type",
1055 devicename.c_str() );
1056 dialog_Fatal( p_this, _("Capture failed"),
1057 _("VLC cannot use the device \"%s\", because its "
1058 "type is not supported."), devicename.c_str() );
1059 return VLC_EGENERIC;
1062 // Retreive acceptable media types supported by device
1063 AM_MEDIA_TYPE media_types[MAX_MEDIA_TYPES];
1064 size_t media_count =
1065 EnumDeviceCaps( p_this, p_device_filter, b_audio ? 0 : p_sys->i_chroma,
1066 p_sys->i_width, p_sys->i_height,
1067 b_audio ? var_CreateGetInteger( p_this, CFG_PREFIX "audio-channels" ) : 0,
1068 b_audio ? var_CreateGetInteger( p_this, CFG_PREFIX "audio-samplerate" ) : 0,
1069 b_audio ? var_CreateGetInteger( p_this, CFG_PREFIX "audio-bitspersample" ) : 0,
1070 media_types, MAX_MEDIA_TYPES );
1072 AM_MEDIA_TYPE *mt = NULL;
1074 if( media_count > 0 )
1076 mt = (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * media_count);
1078 // Order and copy returned media types according to arbitrary
1080 for( size_t c = 0; c < media_count; c++ )
1083 GetFourCCPriority(GetFourCCFromMediaType(media_types[c]));
1084 size_t slot_copy = c;
1085 for( size_t d = c+1; d < media_count; d++ )
1088 GetFourCCPriority(GetFourCCFromMediaType(media_types[d]));
1089 if( priority > slot_priority )
1091 slot_priority = priority;
1095 if( slot_copy != c )
1097 mt[c] = media_types[slot_copy];
1098 media_types[slot_copy] = media_types[c];
1102 mt[c] = media_types[c];
1107 /* capture device */
1108 msg_Err( p_this, "capture device '%s' does not support required parameters !", devicename.c_str() );
1109 dialog_Fatal( p_this, _("Capture failed"),
1110 _("The capture device \"%s\" does not support the "
1111 "required parameters."), devicename.c_str() );
1112 p_device_filter->Release();
1113 return VLC_EGENERIC;
1116 /* Create and add our capture filter */
1117 CaptureFilter *p_capture_filter =
1118 new CaptureFilter( p_this, p_sys, mt, media_count );
1119 p_sys->p_graph->AddFilter( p_capture_filter, 0 );
1121 /* Add the device filter to the graph (seems necessary with VfW before
1122 * accessing pin attributes). */
1123 p_sys->p_graph->AddFilter( p_device_filter, 0 );
1125 /* Attempt to connect one of this device's capture output pins */
1126 msg_Dbg( p_this, "connecting filters" );
1127 if( ConnectFilters( p_this, p_sys, p_device_filter, p_capture_filter ) )
1130 msg_Dbg( p_this, "filters connected successfully !" );
1132 dshow_stream_t dshow_stream;
1133 dshow_stream.b_pts = false;
1134 dshow_stream.p_es = 0;
1136 p_capture_filter->CustomGetPin()->CustomGetMediaType();
1138 /* Show Device properties. Done here so the VLC stream is setup with
1139 * the proper parameters. */
1140 if( var_GetBool( p_this, CFG_PREFIX "config" ) )
1142 ShowDeviceProperties( p_this, p_sys->p_capture_graph_builder2,
1143 p_device_filter, b_audio );
1146 ConfigTuner( p_this, p_sys->p_capture_graph_builder2,
1149 if( var_GetBool( p_this, CFG_PREFIX "tuner" ) &&
1150 dshow_stream.mt.majortype != MEDIATYPE_Stream )
1152 /* FIXME: we do MEDIATYPE_Stream later so we don't do it twice. */
1153 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
1154 p_device_filter, b_audio );
1158 p_capture_filter->CustomGetPin()->CustomGetMediaType();
1160 dshow_stream.i_fourcc = GetFourCCFromMediaType( dshow_stream.mt );
1161 if( dshow_stream.i_fourcc )
1163 if( dshow_stream.mt.majortype == MEDIATYPE_Video )
1165 dshow_stream.header.video =
1166 *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
1167 msg_Dbg( p_this, "MEDIATYPE_Video" );
1168 msg_Dbg( p_this, "selected video pin accepts format: %4.4s",
1169 (char *)&dshow_stream.i_fourcc);
1171 else if( dshow_stream.mt.majortype == MEDIATYPE_Audio )
1173 dshow_stream.header.audio =
1174 *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
1175 msg_Dbg( p_this, "MEDIATYPE_Audio" );
1176 msg_Dbg( p_this, "selected audio pin accepts format: %4.4s",
1177 (char *)&dshow_stream.i_fourcc);
1179 else if( dshow_stream.mt.majortype == MEDIATYPE_Stream )
1181 msg_Dbg( p_this, "MEDIATYPE_Stream" );
1182 msg_Dbg( p_this, "selected stream pin accepts format: %4.4s",
1183 (char *)&dshow_stream.i_fourcc);
1187 msg_Dbg( p_this, "unknown stream majortype" );
1191 /* Add directshow elementary stream to our list */
1192 dshow_stream.p_device_filter = p_device_filter;
1193 dshow_stream.p_capture_filter = p_capture_filter;
1195 p_sys->pp_streams = (dshow_stream_t **)xrealloc( p_sys->pp_streams,
1196 sizeof(dshow_stream_t *) * (p_sys->i_streams + 1) );
1197 p_sys->pp_streams[p_sys->i_streams] = new dshow_stream_t;
1198 *p_sys->pp_streams[p_sys->i_streams++] = dshow_stream;
1205 /* Remove filters from graph */
1206 msg_Dbg( p_this, "OpenDevice: Removing filters" ) ;
1207 p_sys->p_graph->RemoveFilter( p_device_filter );
1208 p_sys->p_graph->RemoveFilter( p_capture_filter );
1210 /* Release objects */
1211 /* RemoveFilter does an undocumented Release()
1212 * but does not set item to NULL*/
1214 p_device_filter = NULL;
1215 p_capture_filter = NULL;
1217 return VLC_EGENERIC;
1220 /* FindCaptureDevices:: This Function had two purposes :
1221 Returns the list of capture devices when p_listdevices != NULL
1222 Creates an IBaseFilter when p_devicename corresponds to an existing devname
1223 These actions *may* be requested whith a single call.
1225 static IBaseFilter *
1226 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
1227 list<string> *p_listdevices, bool b_audio )
1229 IBaseFilter *p_base_filter = NULL;
1230 IMoniker *p_moniker = NULL;
1233 list<string> devicelist;
1235 /* Create the system device enumerator */
1236 ICreateDevEnum *p_dev_enum = NULL;
1238 hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
1239 IID_ICreateDevEnum, (void **)&p_dev_enum );
1242 msg_Err( p_this, "failed to create the device enumerator (0x%lx)", hr);
1246 /* Create an enumerator for the video capture devices */
1247 IEnumMoniker *p_class_enum = NULL;
1249 hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
1252 hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
1254 p_dev_enum->Release();
1257 msg_Err( p_this, "failed to create the class enumerator (0x%lx)", hr );
1261 /* If there are no enumerators for the requested type, then
1262 * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
1263 if( p_class_enum == NULL )
1265 msg_Err( p_this, "no %s capture device was detected", ( b_audio ? "audio" : "video" ) );
1269 /* Enumerate the devices */
1271 /* Note that if the Next() call succeeds but there are no monikers,
1272 * it will return S_FALSE (which is not a failure). Therefore, we check
1273 * that the return code is S_OK instead of using SUCCEEDED() macro. */
1275 while( p_class_enum->Next( 1, &p_moniker, &i_fetched ) == S_OK )
1277 /* Getting the property page to get the device name */
1278 IPropertyBag *p_bag;
1279 hr = p_moniker->BindToStorage( 0, 0, IID_IPropertyBag,
1285 hr = p_bag->Read( L"FriendlyName", &var, NULL );
1289 char *p_buf = FromWide( var.bstrVal );
1290 string devname = string(p_buf);
1294 /* find out if this name is already used by a previously found device */
1295 list<string>::const_iterator iter = devicelist.begin();
1296 list<string>::const_iterator end = devicelist.end();
1297 string ordevname = devname ;
1298 while ( iter != end )
1300 if( 0 == (*iter).compare( devname ) )
1301 { /* devname is on the list. Try another name with sequence
1302 number apended and then rescan until a unique entry is found*/
1304 snprintf(seq, 16, " #%d", ++dup);
1305 devname = ordevname + seq;
1306 iter = devicelist.begin();
1311 devicelist.push_back( devname );
1313 if( p_devicename && *p_devicename == devname )
1315 msg_Dbg( p_this, "asked for %s, binding to %s", p_devicename->c_str() , devname.c_str() ) ;
1316 /* NULL possibly means we don't need BindMoniker BindCtx ?? */
1317 hr = p_moniker->BindToObject( NULL, 0, IID_IBaseFilter,
1318 (void **)&p_base_filter );
1321 msg_Err( p_this, "couldn't bind moniker to filter "
1322 "object (0x%lx)", hr );
1323 p_moniker->Release();
1324 p_class_enum->Release();
1327 p_moniker->Release();
1328 p_class_enum->Release();
1329 return p_base_filter;
1334 p_moniker->Release();
1337 p_class_enum->Release();
1339 if( p_listdevices ) {
1341 *p_listdevices = devicelist;
1346 static size_t EnumDeviceCaps( vlc_object_t *p_this, IBaseFilter *p_filter,
1347 int i_fourcc, int i_width, int i_height,
1348 int i_channels, int i_samplespersec,
1349 int i_bitspersample, AM_MEDIA_TYPE *mt,
1352 IEnumPins *p_enumpins;
1354 IEnumMediaTypes *p_enummt;
1355 size_t mt_count = 0;
1357 LONGLONG i_AvgTimePerFrame = 0;
1358 float r_fps = var_GetFloat( p_this, CFG_PREFIX "fps" );
1360 i_AvgTimePerFrame = 10000000000LL/(LONGLONG)(r_fps*1000.0f);
1362 if( FAILED(p_filter->EnumPins( &p_enumpins )) )
1364 msg_Dbg( p_this, "EnumDeviceCaps failed: no pin enumeration !");
1368 while( S_OK == p_enumpins->Next( 1, &p_output_pin, NULL ) )
1372 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1374 msg_Dbg( p_this, "EnumDeviceCaps: %s pin: %S",
1375 info.dir == PINDIR_INPUT ? "input" : "output",
1377 if( info.pFilter ) info.pFilter->Release();
1380 p_output_pin->Release();
1383 p_enumpins->Reset();
1385 while( !mt_count && p_enumpins->Next( 1, &p_output_pin, NULL ) == S_OK )
1389 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1391 if( info.pFilter ) info.pFilter->Release();
1392 if( info.dir == PINDIR_INPUT )
1394 p_output_pin->Release();
1397 msg_Dbg( p_this, "EnumDeviceCaps: trying pin %S", info.achName );
1400 AM_MEDIA_TYPE *p_mt;
1403 ** Configure pin with a default compatible media if possible
1406 IAMStreamConfig *pSC;
1407 if( SUCCEEDED(p_output_pin->QueryInterface( IID_IAMStreamConfig,
1410 int piCount, piSize;
1411 if( SUCCEEDED(pSC->GetNumberOfCapabilities(&piCount, &piSize)) )
1413 BYTE *pSCC= (BYTE *)CoTaskMemAlloc(piSize);
1416 int i_priority = -1;
1417 for( int i=0; i<piCount; ++i )
1419 if( SUCCEEDED(pSC->GetStreamCaps(i, &p_mt, pSCC)) )
1421 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1422 int i_current_priority = GetFourCCPriority(i_current_fourcc);
1424 if( (i_fourcc && (i_current_fourcc != i_fourcc))
1425 || (i_priority > i_current_priority) )
1427 // unwanted chroma, try next media type
1428 FreeMediaType( *p_mt );
1429 CoTaskMemFree( (PVOID)p_mt );
1433 if( MEDIATYPE_Video == p_mt->majortype
1434 && FORMAT_VideoInfo == p_mt->formattype )
1436 VIDEO_STREAM_CONFIG_CAPS *pVSCC = reinterpret_cast<VIDEO_STREAM_CONFIG_CAPS*>(pSCC);
1437 VIDEOINFOHEADER *pVih = reinterpret_cast<VIDEOINFOHEADER*>(p_mt->pbFormat);
1439 if( i_AvgTimePerFrame )
1441 if( pVSCC->MinFrameInterval > i_AvgTimePerFrame
1442 || i_AvgTimePerFrame > pVSCC->MaxFrameInterval )
1444 // required frame rate not compatible, try next media type
1445 FreeMediaType( *p_mt );
1446 CoTaskMemFree( (PVOID)p_mt );
1449 pVih->AvgTimePerFrame = i_AvgTimePerFrame;
1454 if(( !pVSCC->OutputGranularityX
1455 && i_width != pVSCC->MinOutputSize.cx
1456 && i_width != pVSCC->MaxOutputSize.cx)
1458 ( pVSCC->OutputGranularityX
1459 && ((i_width % pVSCC->OutputGranularityX)
1460 || pVSCC->MinOutputSize.cx > i_width
1461 || i_width > pVSCC->MaxOutputSize.cx )))
1463 // required width not compatible, try next media type
1464 FreeMediaType( *p_mt );
1465 CoTaskMemFree( (PVOID)p_mt );
1468 pVih->bmiHeader.biWidth = i_width;
1473 if(( !pVSCC->OutputGranularityY
1474 && i_height != pVSCC->MinOutputSize.cy
1475 && i_height != pVSCC->MaxOutputSize.cy)
1477 ( pVSCC->OutputGranularityY
1478 && ((i_height % pVSCC->OutputGranularityY)
1479 || pVSCC->MinOutputSize.cy > i_height
1480 || i_height > pVSCC->MaxOutputSize.cy )))
1482 // required height not compatible, try next media type
1483 FreeMediaType( *p_mt );
1484 CoTaskMemFree( (PVOID)p_mt );
1487 pVih->bmiHeader.biHeight = i_height;
1490 // Set the sample size and image size.
1491 // (Round the image width up to a DWORD boundary.)
1492 p_mt->lSampleSize = pVih->bmiHeader.biSizeImage =
1493 ((pVih->bmiHeader.biWidth + 3) & ~3) *
1494 pVih->bmiHeader.biHeight * (pVih->bmiHeader.biBitCount>>3);
1496 // no cropping, use full video input buffer
1497 memset(&(pVih->rcSource), 0, sizeof(RECT));
1498 memset(&(pVih->rcTarget), 0, sizeof(RECT));
1500 // select this format as default
1501 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1503 i_priority = i_current_priority;
1505 // no need to check any more media types
1509 else if( p_mt->majortype == MEDIATYPE_Audio
1510 && p_mt->formattype == FORMAT_WaveFormatEx )
1512 AUDIO_STREAM_CONFIG_CAPS *pASCC = reinterpret_cast<AUDIO_STREAM_CONFIG_CAPS*>(pSCC);
1513 WAVEFORMATEX *pWfx = reinterpret_cast<WAVEFORMATEX*>(p_mt->pbFormat);
1515 if( i_current_fourcc && (WAVE_FORMAT_PCM == pWfx->wFormatTag) )
1517 int val = i_channels;
1521 if( ( !pASCC->ChannelsGranularity
1522 && (unsigned int)val != pASCC->MinimumChannels
1523 && (unsigned int)val != pASCC->MaximumChannels)
1525 ( pASCC->ChannelsGranularity
1526 && ((val % pASCC->ChannelsGranularity)
1527 || (unsigned int)val < pASCC->MinimumChannels
1528 || (unsigned int)val > pASCC->MaximumChannels)))
1530 // required number channels not available, try next media type
1531 FreeMediaType( *p_mt );
1532 CoTaskMemFree( (PVOID)p_mt );
1535 pWfx->nChannels = val;
1537 val = i_samplespersec;
1541 if( ( !pASCC->SampleFrequencyGranularity
1542 && (unsigned int)val != pASCC->MinimumSampleFrequency
1543 && (unsigned int)val != pASCC->MaximumSampleFrequency)
1545 ( pASCC->SampleFrequencyGranularity
1546 && ((val % pASCC->SampleFrequencyGranularity)
1547 || (unsigned int)val < pASCC->MinimumSampleFrequency
1548 || (unsigned int)val > pASCC->MaximumSampleFrequency )))
1550 // required sampling rate not available, try next media type
1551 FreeMediaType( *p_mt );
1552 CoTaskMemFree( (PVOID)p_mt );
1555 pWfx->nSamplesPerSec = val;
1557 val = i_bitspersample;
1560 if( VLC_CODEC_FL32 == i_current_fourcc )
1566 if( ( !pASCC->BitsPerSampleGranularity
1567 && (unsigned int)val != pASCC->MinimumBitsPerSample
1568 && (unsigned int)val != pASCC->MaximumBitsPerSample )
1570 ( pASCC->BitsPerSampleGranularity
1571 && ((val % pASCC->BitsPerSampleGranularity)
1572 || (unsigned int)val < pASCC->MinimumBitsPerSample
1573 || (unsigned int)val > pASCC->MaximumBitsPerSample )))
1575 // required sample size not available, try next media type
1576 FreeMediaType( *p_mt );
1577 CoTaskMemFree( (PVOID)p_mt );
1581 pWfx->wBitsPerSample = val;
1582 pWfx->nBlockAlign = (pWfx->wBitsPerSample * pWfx->nChannels)/8;
1583 pWfx->nAvgBytesPerSec = pWfx->nSamplesPerSec * pWfx->nBlockAlign;
1585 // select this format as default
1586 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1588 i_priority = i_current_priority;
1592 FreeMediaType( *p_mt );
1593 CoTaskMemFree( (PVOID)p_mt );
1596 CoTaskMemFree( (LPVOID)pSCC );
1597 if( i_priority >= 0 )
1598 msg_Dbg( p_this, "EnumDeviceCaps: input pin default format configured");
1605 ** Probe pin for available medias (may be a previously configured one)
1608 if( FAILED( p_output_pin->EnumMediaTypes( &p_enummt ) ) )
1610 p_output_pin->Release();
1614 while( p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1616 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1617 if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Video
1618 && p_mt->formattype == FORMAT_VideoInfo )
1620 int i_current_width = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1621 int i_current_height = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1622 LONGLONG i_current_atpf = ((VIDEOINFOHEADER *)p_mt->pbFormat)->AvgTimePerFrame;
1624 if( i_current_height < 0 )
1625 i_current_height = -i_current_height;
1627 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1628 "accepts chroma: %4.4s, width:%i, height:%i, fps:%f",
1629 (char *)&i_current_fourcc, i_current_width,
1630 i_current_height, (10000000.0f/((float)i_current_atpf)) );
1632 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1633 ( !i_width || i_width == i_current_width ) &&
1634 ( !i_height || i_height == i_current_height ) &&
1635 ( !i_AvgTimePerFrame || i_AvgTimePerFrame == i_current_atpf ) &&
1639 mt[mt_count++] = *p_mt;
1641 else FreeMediaType( *p_mt );
1643 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Audio
1644 && p_mt->formattype == FORMAT_WaveFormatEx)
1646 int i_current_channels =
1647 ((WAVEFORMATEX *)p_mt->pbFormat)->nChannels;
1648 int i_current_samplespersec =
1649 ((WAVEFORMATEX *)p_mt->pbFormat)->nSamplesPerSec;
1650 int i_current_bitspersample =
1651 ((WAVEFORMATEX *)p_mt->pbFormat)->wBitsPerSample;
1653 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1654 "accepts format: %4.4s, channels:%i, "
1655 "samples/sec:%i bits/sample:%i",
1656 (char *)&i_current_fourcc, i_current_channels,
1657 i_current_samplespersec, i_current_bitspersample);
1659 if( (!i_channels || i_channels == i_current_channels) &&
1660 (!i_samplespersec ||
1661 i_samplespersec == i_current_samplespersec) &&
1662 (!i_bitspersample ||
1663 i_bitspersample == i_current_bitspersample) &&
1667 mt[mt_count++] = *p_mt;
1669 /* Setup a few properties like the audio latency */
1670 IAMBufferNegotiation *p_ambuf;
1671 if( SUCCEEDED( p_output_pin->QueryInterface(
1672 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
1674 ALLOCATOR_PROPERTIES AllocProp;
1675 AllocProp.cbAlign = -1;
1677 /* 100 ms of latency */
1678 AllocProp.cbBuffer = i_current_channels *
1679 i_current_samplespersec *
1680 i_current_bitspersample / 8 / 10;
1682 AllocProp.cbPrefix = -1;
1683 AllocProp.cBuffers = -1;
1684 p_ambuf->SuggestAllocatorProperties( &AllocProp );
1688 else FreeMediaType( *p_mt );
1690 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Stream )
1692 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1693 "accepts stream format: %4.4s",
1694 (char *)&i_current_fourcc );
1696 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1700 mt[mt_count++] = *p_mt;
1701 i_fourcc = i_current_fourcc;
1703 else FreeMediaType( *p_mt );
1707 const char * psz_type = "unknown";
1708 if( p_mt->majortype == MEDIATYPE_Video ) psz_type = "video";
1709 if( p_mt->majortype == MEDIATYPE_Audio ) psz_type = "audio";
1710 if( p_mt->majortype == MEDIATYPE_Stream ) psz_type = "stream";
1711 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: unsupported format "
1712 "(%s %4.4s)", psz_type, (char *)&p_mt->subtype );
1714 FreeMediaType( *p_mt );
1716 CoTaskMemFree( (PVOID)p_mt );
1719 if( !mt_count && p_enummt->Reset() == S_OK )
1721 // VLC did not find any supported MEDIATYPE for this output pin.
1722 // However the graph builder might insert converter filters in
1723 // the graph if we use a different codec in VLC filter input pin.
1724 // however, in order to avoid nasty surprises, make use of this
1725 // facility only for known unsupported codecs.
1727 while( !mt_count && p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1729 // the first four bytes of subtype GUID contains the codec FOURCC
1730 const char *pfcc = (char *)&p_mt->subtype;
1731 int i_current_fourcc = VLC_FOURCC(pfcc[0], pfcc[1], pfcc[2], pfcc[3]);
1732 if( VLC_FOURCC('H','C','W','2') == i_current_fourcc
1733 && p_mt->majortype == MEDIATYPE_Video )
1735 // output format for 'Hauppauge WinTV PVR PCI II Capture'
1736 // try I420 as an input format
1737 i_current_fourcc = VLC_CODEC_I420;
1738 if( !i_fourcc || i_fourcc == i_current_fourcc )
1740 // return alternative media type
1744 mtr.majortype = MEDIATYPE_Video;
1745 mtr.subtype = MEDIASUBTYPE_I420;
1746 mtr.bFixedSizeSamples = TRUE;
1747 mtr.bTemporalCompression = FALSE;
1749 mtr.formattype = FORMAT_VideoInfo;
1750 mtr.cbFormat = sizeof(vh);
1751 mtr.pbFormat = (BYTE *)&vh;
1753 memset(&vh, 0, sizeof(vh));
1755 vh.bmiHeader.biSize = sizeof(vh.bmiHeader);
1756 vh.bmiHeader.biWidth = i_width > 0 ? i_width :
1757 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1758 vh.bmiHeader.biHeight = i_height > 0 ? i_height :
1759 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1760 vh.bmiHeader.biPlanes = 3;
1761 vh.bmiHeader.biBitCount = 12;
1762 vh.bmiHeader.biCompression = VLC_CODEC_I420;
1763 vh.bmiHeader.biSizeImage = vh.bmiHeader.biWidth * 12 *
1764 vh.bmiHeader.biHeight / 8;
1765 mtr.lSampleSize = vh.bmiHeader.biSizeImage;
1767 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: using 'I420' in place of unsupported format 'HCW2'");
1769 if( SUCCEEDED(CopyMediaType(mt+mt_count, &mtr)) )
1773 FreeMediaType( *p_mt );
1777 p_enummt->Release();
1778 p_output_pin->Release();
1781 p_enumpins->Release();
1785 /*****************************************************************************
1786 * ReadCompressed: reads compressed (MPEG/DV) data from the device.
1787 *****************************************************************************
1788 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1790 *****************************************************************************/
1791 static block_t *ReadCompressed( access_t *p_access )
1793 access_sys_t *p_sys = p_access->p_sys;
1794 dshow_stream_t *p_stream = NULL;
1795 VLCMediaSample sample;
1797 /* Read 1 DV/MPEG frame (they contain the video and audio data) */
1799 /* There must be only 1 elementary stream to produce a valid stream
1800 * of MPEG or DV data */
1801 p_stream = p_sys->pp_streams[0];
1805 if( !vlc_object_alive (p_access) ) return NULL;
1807 /* Get new sample/frame from the elementary stream (blocking). */
1808 vlc_mutex_lock( &p_sys->lock );
1810 if( p_stream->p_capture_filter->CustomGetPin()
1811 ->CustomGetSample( &sample ) != S_OK )
1813 /* No data available. Wait until some data has arrived */
1814 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1815 vlc_mutex_unlock( &p_sys->lock );
1819 vlc_mutex_unlock( &p_sys->lock );
1826 int i_data_size = sample.p_sample->GetActualDataLength();
1828 if( !i_data_size || !(p_block = block_New( p_access, i_data_size )) )
1830 sample.p_sample->Release();
1834 sample.p_sample->GetPointer( &p_data );
1835 vlc_memcpy( p_block->p_buffer, p_data, i_data_size );
1836 sample.p_sample->Release();
1838 /* The caller got what he wanted */
1842 return NULL; /* never reached */
1845 /****************************************************************************
1847 ****************************************************************************/
1848 static int Demux( demux_t *p_demux )
1850 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
1851 int i_found_samples;
1853 i_found_samples = 0;
1854 vlc_mutex_lock( &p_sys->lock );
1856 while ( !i_found_samples )
1858 /* Try to grab samples from all streams */
1859 for( int i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1861 dshow_stream_t *p_stream = p_sys->pp_streams[i_stream];
1862 if( p_stream->p_capture_filter &&
1863 p_stream->p_capture_filter->CustomGetPin()
1864 ->CustomGetSamples( p_stream->samples_queue ) == S_OK )
1866 i_found_samples = 1;
1870 if ( !i_found_samples)
1872 /* Didn't find any audio nor video sample, just wait till the
1873 * dshow thread pushes some samples */
1874 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1875 /* Some DShow thread pushed data, or the OS broke the wait all
1876 * by itself. In all cases, it's *strongly* advised to test the
1877 * condition again, so let the loop do the test again */
1881 vlc_mutex_unlock( &p_sys->lock );
1883 for ( int i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1886 dshow_stream_t *p_stream = p_sys->pp_streams[i_stream];
1888 i_samples = p_stream->samples_queue.size();
1889 while ( i_samples > 0 )
1894 VLCMediaSample sample;
1896 sample = p_stream->samples_queue.front();
1897 p_stream->samples_queue.pop_front();
1899 i_data_size = sample.p_sample->GetActualDataLength();
1900 sample.p_sample->GetPointer( &p_data );
1902 REFERENCE_TIME i_pts, i_end_date;
1903 HRESULT hr = sample.p_sample->GetTime( &i_pts, &i_end_date );
1904 if( hr != VFW_S_NO_STOP_TIME && hr != S_OK ) i_pts = 0;
1908 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
1910 /* Use our data timestamp */
1911 i_pts = sample.i_timestamp;
1912 p_stream->b_pts = true;
1916 i_pts /= 10; /* Dshow works with 100 nano-seconds resolution */
1919 msg_Dbg( p_demux, "Read() stream: %i, size: %i, PTS: %"PRId64,
1920 i_stream, i_data_size, i_pts );
1923 p_block = block_New( p_demux, i_data_size );
1924 vlc_memcpy( p_block->p_buffer, p_data, i_data_size );
1925 p_block->i_pts = p_block->i_dts = i_pts;
1926 sample.p_sample->Release();
1928 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pts > 0 ? i_pts : 0 );
1929 es_out_Send( p_demux->out, p_stream->p_es, p_block );
1938 /*****************************************************************************
1940 *****************************************************************************/
1941 static int AccessControl( access_t *p_access, int i_query, va_list args )
1949 case ACCESS_CAN_SEEK:
1950 case ACCESS_CAN_FASTSEEK:
1951 case ACCESS_CAN_PAUSE:
1952 case ACCESS_CAN_CONTROL_PACE:
1953 pb_bool = (bool*)va_arg( args, bool* );
1958 case ACCESS_GET_PTS_DELAY:
1959 pi_64 = (int64_t*)va_arg( args, int64_t * );
1961 INT64_C(1000) * var_InheritInteger( p_access, "live-caching" );
1965 case ACCESS_SET_PAUSE_STATE:
1966 case ACCESS_GET_TITLE_INFO:
1967 case ACCESS_SET_TITLE:
1968 case ACCESS_SET_SEEKPOINT:
1969 case ACCESS_SET_PRIVATE_ID_STATE:
1970 return VLC_EGENERIC;
1973 msg_Warn( p_access, "unimplemented query in control" );
1974 return VLC_EGENERIC;
1980 /****************************************************************************
1982 ****************************************************************************/
1983 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1990 /* Special for access_demux */
1991 case DEMUX_CAN_PAUSE:
1992 case DEMUX_CAN_SEEK:
1993 case DEMUX_SET_PAUSE_STATE:
1994 case DEMUX_CAN_CONTROL_PACE:
1995 pb = (bool*)va_arg( args, bool * );
1999 case DEMUX_GET_PTS_DELAY:
2000 pi64 = (int64_t*)va_arg( args, int64_t * );
2002 INT64_C(1000) * var_InheritInteger( p_demux, "live-caching" );
2005 case DEMUX_GET_TIME:
2006 pi64 = (int64_t*)va_arg( args, int64_t * );
2010 /* TODO implement others */
2012 return VLC_EGENERIC;
2015 return VLC_EGENERIC;
2018 /*****************************************************************************
2019 * config variable callback
2020 *****************************************************************************/
2021 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
2022 vlc_value_t, vlc_value_t, void * )
2024 module_config_t *p_item;
2025 bool b_audio = false;
2028 p_item = config_FindConfig( p_this, psz_name );
2029 if( !p_item ) return VLC_SUCCESS;
2031 if( !strcmp( psz_name, CFG_PREFIX "adev" ) ) b_audio = true;
2033 /* Clear-up the current list */
2034 if( p_item->i_list )
2036 /* Keep the 2 first entries */
2037 for( i = 2; i < p_item->i_list; i++ )
2039 free( p_item->ppsz_list[i] );
2040 free( p_item->ppsz_list_text[i] );
2042 /* TODO: Remove when no more needed */
2043 p_item->ppsz_list[i] = NULL;
2044 p_item->ppsz_list_text[i] = NULL;
2048 /* Find list of devices */
2049 list<string> list_devices;
2051 /* Initialize OLE/COM */
2054 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
2056 /* Uninitialize OLE/COM */
2059 if( list_devices.empty() ) return VLC_SUCCESS;
2061 p_item->ppsz_list = (char**)xrealloc( p_item->ppsz_list,
2062 (list_devices.size()+3) * sizeof(char *) );
2063 p_item->ppsz_list_text = (char**)xrealloc( p_item->ppsz_list_text,
2064 (list_devices.size()+3) * sizeof(char *) );
2066 list<string>::iterator iter;
2067 for( iter = list_devices.begin(), i = 2; iter != list_devices.end();
2070 p_item->ppsz_list[i] = strdup( iter->c_str() );
2071 p_item->ppsz_list_text[i] = NULL;
2074 p_item->ppsz_list[i] = NULL;
2075 p_item->ppsz_list_text[i] = NULL;
2080 static int ConfigDevicesCallback( vlc_object_t *p_this, char const *psz_name,
2081 vlc_value_t newval, vlc_value_t, void * )
2083 module_config_t *p_item;
2084 bool b_audio = false;
2085 char *psz_device = NULL;
2086 int i_ret = VLC_SUCCESS;
2088 if( !EMPTY_STR( newval.psz_string ) )
2089 psz_device = strdup( newval.psz_string );
2091 /* Initialize OLE/COM */
2094 p_item = config_FindConfig( p_this, psz_name );
2103 if( !strcmp( psz_name, CFG_PREFIX "adev" ) ) b_audio = true;
2109 devicename = psz_device ;
2113 /* If no device name was specified, pick the 1st one */
2114 list<string> list_devices;
2116 /* Enumerate devices */
2117 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
2118 if( list_devices.empty() )
2121 return VLC_EGENERIC;
2123 devicename = *list_devices.begin();
2126 IBaseFilter *p_device_filter =
2127 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
2128 if( p_device_filter )
2130 ShowPropertyPage( p_device_filter );
2131 p_device_filter->Release();
2135 msg_Err( p_this, "didn't find device: %s", devicename.c_str() );
2136 i_ret = VLC_EGENERIC;
2139 /* Uninitialize OLE/COM */
2146 /*****************************************************************************
2148 *****************************************************************************/
2150 static void ShowPropertyPage( IUnknown *obj )
2152 ISpecifyPropertyPages *p_spec;
2155 HRESULT hr = obj->QueryInterface( IID_ISpecifyPropertyPages,
2157 if( FAILED(hr) ) return;
2159 if( SUCCEEDED(p_spec->GetPages( &cauuid )) )
2161 if( cauuid.cElems > 0 )
2163 HWND hwnd_desktop = ::GetDesktopWindow();
2165 OleCreatePropertyFrame( hwnd_desktop, 30, 30, NULL, 1, &obj,
2166 cauuid.cElems, cauuid.pElems, 0, 0, NULL );
2168 CoTaskMemFree( cauuid.pElems );
2174 static void ShowDeviceProperties( vlc_object_t *p_this,
2175 ICaptureGraphBuilder2 *p_graph,
2176 IBaseFilter *p_device_filter,
2180 msg_Dbg( p_this, "configuring Device Properties" );
2183 * Video or audio capture filter page
2185 ShowPropertyPage( p_device_filter );
2190 if( p_graph && b_audio )
2192 IAMStreamConfig *p_SC;
2194 msg_Dbg( p_this, "showing WDM Audio Configuration Pages" );
2196 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2197 &MEDIATYPE_Audio, p_device_filter,
2198 IID_IAMStreamConfig, (void **)&p_SC );
2201 ShowPropertyPage(p_SC);
2209 HRESULT hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2210 &MEDIATYPE_Audio, p_device_filter,
2211 IID_IAMTVAudio, (void **)&p_TVA );
2214 ShowPropertyPage(p_TVA);
2222 if( p_graph && !b_audio )
2224 IAMStreamConfig *p_SC;
2226 msg_Dbg( p_this, "showing WDM Video Configuration Pages" );
2228 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2229 &MEDIATYPE_Interleaved, p_device_filter,
2230 IID_IAMStreamConfig, (void **)&p_SC );
2233 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2234 &MEDIATYPE_Video, p_device_filter,
2235 IID_IAMStreamConfig, (void **)&p_SC );
2240 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2241 &MEDIATYPE_Stream, p_device_filter,
2242 IID_IAMStreamConfig, (void **)&p_SC );
2247 ShowPropertyPage(p_SC);
2253 static void ShowTunerProperties( vlc_object_t *p_this,
2254 ICaptureGraphBuilder2 *p_graph,
2255 IBaseFilter *p_device_filter,
2259 msg_Dbg( p_this, "configuring Tuner Properties" );
2261 if( !p_graph || b_audio ) return;
2264 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2265 &MEDIATYPE_Interleaved, p_device_filter,
2266 IID_IAMTVTuner, (void **)&p_TV );
2269 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2270 &MEDIATYPE_Video, p_device_filter,
2271 IID_IAMTVTuner, (void **)&p_TV );
2276 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2277 &MEDIATYPE_Stream, p_device_filter,
2278 IID_IAMTVTuner, (void **)&p_TV );
2283 ShowPropertyPage(p_TV);
2288 static void ConfigTuner( vlc_object_t *p_this, ICaptureGraphBuilder2 *p_graph,
2289 IBaseFilter *p_device_filter )
2291 int i_channel, i_country, i_input, i_amtuner_mode, i_standard;
2293 unsigned i_frequency;
2297 if( !p_graph ) return;
2299 i_channel = var_GetInteger( p_this, CFG_PREFIX "tuner-channel" );
2300 i_country = var_GetInteger( p_this, CFG_PREFIX "tuner-country" );
2301 i_input = var_GetInteger( p_this, CFG_PREFIX "tuner-input" );
2302 i_amtuner_mode = var_GetInteger( p_this, CFG_PREFIX "amtuner-mode" );
2303 i_frequency = var_GetInteger( p_this, CFG_PREFIX "tuner-frequency" );
2304 i_standard = var_GetInteger( p_this, CFG_PREFIX "tuner-standard" );
2306 if( !i_channel && !i_frequency && !i_country && !i_input ) return; /* Nothing to do */
2308 msg_Dbg( p_this, "tuner config: channel %i, frequency %i, country %i, input type %i, standard %s",
2309 i_channel, i_frequency, i_country, i_input, ppsz_standards_list_text[i_standard] );
2312 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Interleaved,
2313 p_device_filter, IID_IAMTVTuner,
2317 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
2318 p_device_filter, IID_IAMTVTuner,
2324 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Stream,
2325 p_device_filter, IID_IAMTVTuner,
2331 msg_Dbg( p_this, "couldn't find tuner interface" );
2335 hr = p_TV->GetAvailableModes( &l_modes );
2336 if( SUCCEEDED(hr) && (l_modes & i_amtuner_mode) )
2338 hr = p_TV->put_Mode( (AMTunerModeType)i_amtuner_mode );
2341 if( i_input == 1 ) p_TV->put_InputType( 0, TunerInputCable );
2342 else if( i_input == 2 ) p_TV->put_InputType( 0, TunerInputAntenna );
2344 p_TV->put_CountryCode( i_country );
2346 if( i_frequency <= 0 ) p_TV->put_Channel( i_channel, AMTUNER_SUBCHAN_NO_TUNE,
2347 AMTUNER_SUBCHAN_NO_TUNE );
2349 if( i_frequency > 0 || i_standard > 0) {
2350 IKsPropertySet *pKs = NULL;
2351 DWORD dw_supported = 0;
2352 KSPROPERTY_TUNER_MODE_CAPS_S ModeCaps;
2353 KSPROPERTY_TUNER_FREQUENCY_S Frequency;
2354 KSPROPERTY_TUNER_STANDARD_S Standard;
2356 hr = p_TV->QueryInterface(IID_IKsPropertySet,(void **)&pKs);
2359 msg_Dbg( p_this, "Couldn't QI for IKsPropertySet" );
2364 memset(&ModeCaps,0,sizeof(KSPROPERTY_TUNER_MODE_CAPS_S));
2365 memset(&Frequency,0,sizeof(KSPROPERTY_TUNER_FREQUENCY_S));
2366 memset(&Standard,0,sizeof(KSPROPERTY_TUNER_STANDARD_S));
2367 ModeCaps.Mode = AMTUNER_MODE_TV;
2369 hr = pKs->QuerySupported(PROPSETID_TUNER,
2370 KSPROPERTY_TUNER_MODE_CAPS,&dw_supported);
2371 if(SUCCEEDED(hr) && dw_supported&KSPROPERTY_SUPPORT_GET)
2374 hr = pKs->Get(PROPSETID_TUNER,KSPROPERTY_TUNER_MODE_CAPS,
2375 INSTANCEDATA_OF_PROPERTY_PTR(&ModeCaps),
2376 INSTANCEDATA_OF_PROPERTY_SIZE(ModeCaps),
2383 msg_Dbg( p_this, "KSPROPERTY_TUNER_MODE_CAPS not supported!" );
2387 msg_Dbg( p_this, "Frequency range supported from %ld to %ld.",
2388 ModeCaps.MinFrequency, ModeCaps.MaxFrequency);
2389 msg_Dbg( p_this, "Video standards supported by the tuner: ");
2390 for(size_t i = 0 ; i < ARRAY_SIZE(ppsz_standards_list_text); i++) {
2391 if(ModeCaps.StandardsSupported & i_standards_list[i])
2392 msg_Dbg( p_this, "%s, ", ppsz_standards_list_text[i]);
2395 if(i_frequency > 0) {
2396 Frequency.Frequency=i_frequency;
2397 if(ModeCaps.Strategy==KS_TUNER_STRATEGY_DRIVER_TUNES)
2398 Frequency.TuningFlags=KS_TUNER_TUNING_FINE;
2400 Frequency.TuningFlags=KS_TUNER_TUNING_EXACT;
2402 if(i_frequency>=ModeCaps.MinFrequency && i_frequency<=ModeCaps.MaxFrequency)
2405 hr = pKs->Set(PROPSETID_TUNER,
2406 KSPROPERTY_TUNER_FREQUENCY,
2407 INSTANCEDATA_OF_PROPERTY_PTR(&Frequency),
2408 INSTANCEDATA_OF_PROPERTY_SIZE(Frequency),
2413 msg_Dbg( p_this, "Couldn't set KSPROPERTY_TUNER_FREQUENCY!" );
2419 msg_Dbg( p_this, "Requested frequency exceeds the supported range!" );
2424 if(i_standard > 0) {
2425 if(i_standard & ModeCaps.StandardsSupported )
2427 Standard.Standard = i_standard;
2428 hr = pKs->Set(PROPSETID_TUNER,
2429 KSPROPERTY_TUNER_STANDARD,
2430 INSTANCEDATA_OF_PROPERTY_PTR(&Standard),
2431 INSTANCEDATA_OF_PROPERTY_SIZE(Standard),
2436 msg_Dbg( p_this, "Couldn't set KSPROPERTY_TUNER_STANDARD!" );
2442 msg_Dbg( p_this, "Requested video standard is not supported by the tuner!" );