1 /*****************************************************************************
2 * dshow.cpp : DirectShow access 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
37 #include <vlc_common.h>
38 #include <vlc_plugin.h>
39 #include <vlc_input.h>
40 #include <vlc_access.h>
41 #include <vlc_demux.h>
42 #include <vlc_dialog.h>
43 #include <vlc_charset.h>
48 /*****************************************************************************
49 * Access: local prototypes
50 *****************************************************************************/
51 static block_t *ReadCompressed( access_t * );
52 static int AccessControl ( access_t *, int, va_list );
54 static int Demux ( demux_t * );
55 static int DemuxControl( demux_t *, int, va_list );
57 static int OpenDevice( vlc_object_t *, access_sys_t *, string, bool );
58 static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
59 list<string> *, bool );
60 static size_t EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
61 int, int, int, int, int, int,
62 AM_MEDIA_TYPE *mt, size_t );
63 static bool ConnectFilters( vlc_object_t *, access_sys_t *,
64 IBaseFilter *, CaptureFilter * );
65 static int FindDevicesCallback( vlc_object_t *, char const *,
66 vlc_value_t, vlc_value_t, void * );
67 static int ConfigDevicesCallback( vlc_object_t *, char const *,
68 vlc_value_t, vlc_value_t, void * );
70 static void ShowPropertyPage( IUnknown * );
71 static void ShowDeviceProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
72 IBaseFilter *, bool );
73 static void ShowTunerProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
74 IBaseFilter *, bool );
75 static void ConfigTuner( vlc_object_t *, ICaptureGraphBuilder2 *,
78 /*****************************************************************************
80 *****************************************************************************/
81 static const char *const ppsz_vdev[] = { "", "none" };
82 static const char *const ppsz_vdev_text[] = { N_("Default"), N_("None") };
83 static const char *const ppsz_adev[] = { "", "none" };
84 static const char *const ppsz_adev_text[] = { N_("Default"), N_("None") };
85 static const int pi_tuner_input[] = { 0, 1, 2 };
86 static const char *const ppsz_tuner_input_text[] =
87 {N_("Default"), N_("Cable"), N_("Antenna")};
88 static const int pi_amtuner_mode[] = { AMTUNER_MODE_DEFAULT,
90 AMTUNER_MODE_FM_RADIO,
91 AMTUNER_MODE_AM_RADIO,
93 static const char *const ppsz_amtuner_mode_text[] = { N_("Default"),
99 #define CACHING_TEXT N_("Caching value in ms")
100 #define CACHING_LONGTEXT N_( \
101 "Caching value for DirectShow streams. " \
102 "This value should be set in milliseconds." )
103 #define VDEV_TEXT N_("Video device name")
104 #define VDEV_LONGTEXT N_( \
105 "Name of the video device that will be used by the " \
106 "DirectShow plugin. If you don't specify anything, the default device " \
108 #define ADEV_TEXT N_("Audio device name")
109 #define ADEV_LONGTEXT N_( \
110 "Name of the audio device that will be used by the " \
111 "DirectShow plugin. If you don't specify anything, the default device " \
113 #define SIZE_TEXT N_("Video size")
114 #define SIZE_LONGTEXT N_( \
115 "Size of the video that will be displayed by the " \
116 "DirectShow plugin. If you don't specify anything the default size for " \
117 "your device will be used. You can specify a standard size (cif, d1, ...) or <width>x<height>.")
118 #define ASPECT_TEXT N_("Picture aspect-ratio n:m")
119 #define ASPECT_LONGTEXT N_("Define input picture aspect-ratio to use. Default is 4:3" )
120 #define CHROMA_TEXT N_("Video input chroma format")
121 #define CHROMA_LONGTEXT N_( \
122 "Force the DirectShow video input to use a specific chroma format " \
123 "(eg. I420 (default), RV24, etc.)")
124 #define FPS_TEXT N_("Video input frame rate")
125 #define FPS_LONGTEXT N_( \
126 "Force the DirectShow video input to use a specific frame rate" \
127 "(eg. 0 means default, 25, 29.97, 50, 59.94, etc.)")
128 #define CONFIG_TEXT N_("Device properties")
129 #define CONFIG_LONGTEXT N_( \
130 "Show the properties dialog of the selected device before starting the " \
132 #define TUNER_TEXT N_("Tuner properties")
133 #define TUNER_LONGTEXT N_( \
134 "Show the tuner properties [channel selection] page." )
135 #define CHANNEL_TEXT N_("Tuner TV Channel")
136 #define CHANNEL_LONGTEXT N_( \
137 "Set the TV channel the tuner will set to " \
138 "(0 means default)." )
139 #define TVFREQ_TEXT N_("Tuner Frequency")
140 #define TVFREQ_LONGTEXT N_( "This overrides the channel. Measured in Hz." )
141 #define STANDARD_TEXT N_( "Standard" )
142 #define STANDARD_LONGTEXT N_( "Video standard (Default, SECAM_D, PAL_B, NTSC_M, etc...)." )
143 #define COUNTRY_TEXT N_("Tuner country code")
144 #define COUNTRY_LONGTEXT N_( \
145 "Set the tuner country code that establishes the current " \
146 "channel-to-frequency mapping (0 means default)." )
147 #define TUNER_INPUT_TEXT N_("Tuner input type")
148 #define TUNER_INPUT_LONGTEXT N_( \
149 "Select the tuner input type (Cable/Antenna)." )
150 #define VIDEO_IN_TEXT N_("Video input pin")
151 #define VIDEO_IN_LONGTEXT N_( \
152 "Select the video input source, such as composite, s-video, " \
153 "or tuner. Since these settings are hardware-specific, you should find good " \
154 "settings in the \"Device config\" area, and use those numbers here. -1 " \
155 "means that settings will not be changed.")
156 #define AUDIO_IN_TEXT N_("Audio input pin")
157 #define AUDIO_IN_LONGTEXT N_( \
158 "Select the audio input source. See the \"video input\" option." )
159 #define VIDEO_OUT_TEXT N_("Video output pin")
160 #define VIDEO_OUT_LONGTEXT N_( \
161 "Select the video output type. See the \"video input\" option." )
162 #define AUDIO_OUT_TEXT N_("Audio output pin")
163 #define AUDIO_OUT_LONGTEXT N_( \
164 "Select the audio output type. See the \"video input\" option." )
166 #define AMTUNER_MODE_TEXT N_("AM Tuner mode")
167 #define AMTUNER_MODE_LONGTEXT N_( \
168 "AM Tuner mode. Can be one of Default (0), TV (1)," \
169 "AM Radio (2), FM Radio (3) or DSS (4).")
171 #define AUDIO_CHANNELS_TEXT N_("Number of audio channels")
172 #define AUDIO_CHANNELS_LONGTEXT N_( \
173 "Select audio input format with the given number of audio channels (if non 0)" )
175 #define AUDIO_SAMPLERATE_TEXT N_("Audio sample rate")
176 #define AUDIO_SAMPLERATE_LONGTEXT N_( \
177 "Select audio input format with the given sample rate (if non 0)" )
179 #define AUDIO_BITSPERSAMPLE_TEXT N_("Audio bits per sample")
180 #define AUDIO_BITSPERSAMPLE_LONGTEXT N_( \
181 "Select audio input format with the given bits/sample (if non 0)" )
183 static const int i_standards_list[] =
186 KS_AnalogVideo_NTSC_M, KS_AnalogVideo_NTSC_M_J, KS_AnalogVideo_NTSC_433,
187 KS_AnalogVideo_PAL_B, KS_AnalogVideo_PAL_D, KS_AnalogVideo_PAL_G,
188 KS_AnalogVideo_PAL_H, KS_AnalogVideo_PAL_I, KS_AnalogVideo_PAL_M,
189 KS_AnalogVideo_PAL_N, KS_AnalogVideo_PAL_60,
190 KS_AnalogVideo_SECAM_B, KS_AnalogVideo_SECAM_D, KS_AnalogVideo_SECAM_G,
191 KS_AnalogVideo_SECAM_H, KS_AnalogVideo_SECAM_K, KS_AnalogVideo_SECAM_K1,
192 KS_AnalogVideo_SECAM_L, KS_AnalogVideo_SECAM_L1,
193 KS_AnalogVideo_PAL_N_COMBO
195 static const char *const ppsz_standards_list_text[] =
198 "NTSC_M", "NTSC_M_J", "NTSC_443",
199 "PAL_B", "PAL_D", "PAL_G",
200 "PAL_H", "PAL_I", "PAL_M",
202 "SECAM_B", "SECAM_D", "SECAM_G",
203 "SECAM_H", "SECAM_K", "SECAM_K1",
204 "SECAM_L", "SECAM_L1",
208 static int CommonOpen ( vlc_object_t *, access_sys_t *, bool );
209 static void CommonClose( vlc_object_t *, access_sys_t * );
211 static int AccessOpen ( vlc_object_t * );
212 static void AccessClose( vlc_object_t * );
214 static int DemuxOpen ( vlc_object_t * );
215 static void DemuxClose ( vlc_object_t * );
218 set_shortname( N_("DirectShow") )
219 set_description( N_("DirectShow input") )
220 set_category( CAT_INPUT )
221 set_subcategory( SUBCAT_INPUT_ACCESS )
222 add_integer( "dshow-caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
223 CACHING_TEXT, CACHING_LONGTEXT, true )
225 add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, false)
226 change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback )
227 change_action_add( FindDevicesCallback, N_("Refresh list") )
228 change_action_add( ConfigDevicesCallback, N_("Configure") )
230 add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, false)
231 change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback )
232 change_action_add( FindDevicesCallback, N_("Refresh list") )
233 change_action_add( ConfigDevicesCallback, N_("Configure") )
235 add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, false)
237 add_string( "dshow-aspect-ratio", "4:3", NULL, ASPECT_TEXT, ASPECT_LONGTEXT, false)
239 add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true )
241 add_float( "dshow-fps", 0.0f, NULL, FPS_TEXT, FPS_LONGTEXT, true )
243 add_bool( "dshow-config", false, NULL, CONFIG_TEXT, CONFIG_LONGTEXT, true )
245 add_bool( "dshow-tuner", false, NULL, TUNER_TEXT, TUNER_LONGTEXT, true )
247 add_integer( "dshow-tuner-channel", 0, NULL, CHANNEL_TEXT, CHANNEL_LONGTEXT,
250 add_integer( "dshow-tuner-frequency", 0, NULL, TVFREQ_TEXT, TVFREQ_LONGTEXT,
253 add_integer( "dshow-tuner-country", 0, NULL, COUNTRY_TEXT, COUNTRY_LONGTEXT,
256 add_integer( "dshow-tuner-standard", 0, NULL, STANDARD_TEXT, STANDARD_LONGTEXT,
258 change_integer_list( i_standards_list, ppsz_standards_list_text, NULL )
260 add_integer( "dshow-tuner-input", 0, NULL, TUNER_INPUT_TEXT,
261 TUNER_INPUT_LONGTEXT, true )
262 change_integer_list( pi_tuner_input, ppsz_tuner_input_text, NULL )
264 add_integer( "dshow-video-input", -1, NULL, VIDEO_IN_TEXT,
265 VIDEO_IN_LONGTEXT, true )
267 add_integer( "dshow-video-output", -1, NULL, VIDEO_OUT_TEXT,
268 VIDEO_OUT_LONGTEXT, true )
270 add_integer( "dshow-audio-input", -1, NULL, AUDIO_IN_TEXT,
271 AUDIO_IN_LONGTEXT, true )
273 add_integer( "dshow-audio-output", -1, NULL, AUDIO_OUT_TEXT,
274 AUDIO_OUT_LONGTEXT, true )
276 add_integer( "dshow-amtuner-mode", AMTUNER_MODE_TV, NULL,
277 AMTUNER_MODE_TEXT, AMTUNER_MODE_LONGTEXT, false)
278 change_integer_list( pi_amtuner_mode, ppsz_amtuner_mode_text, NULL )
280 add_integer( "dshow-audio-channels", 0, NULL, AUDIO_CHANNELS_TEXT,
281 AUDIO_CHANNELS_LONGTEXT, true )
282 add_integer( "dshow-audio-samplerate", 0, NULL, AUDIO_SAMPLERATE_TEXT,
283 AUDIO_SAMPLERATE_LONGTEXT, true )
284 add_integer( "dshow-audio-bitspersample", 0, NULL, AUDIO_BITSPERSAMPLE_TEXT,
285 AUDIO_BITSPERSAMPLE_LONGTEXT, true )
287 add_shortcut( "dshow" )
288 set_capability( "access_demux", 0 )
289 set_callbacks( DemuxOpen, DemuxClose )
292 set_description( N_("DirectShow input") )
293 add_shortcut( "dshow" )
294 set_capability( "access", 0 )
295 set_callbacks( AccessOpen, AccessClose )
300 /*****************************************************************************
301 * DirectShow elementary stream descriptor
302 *****************************************************************************/
303 typedef struct dshow_stream_t
306 IBaseFilter *p_device_filter;
307 CaptureFilter *p_capture_filter;
312 VIDEOINFOHEADER video;
322 deque<VLCMediaSample> samples_queue;
325 /*****************************************************************************
326 * DirectShow utility functions
327 *****************************************************************************/
328 static void CreateDirectShowGraph( access_sys_t *p_sys )
330 p_sys->i_crossbar_route_depth = 0;
332 /* Create directshow filter graph */
333 if( SUCCEEDED( CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
334 (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph) ) )
336 /* Create directshow capture graph builder if available */
337 if( SUCCEEDED( CoCreateInstance( CLSID_CaptureGraphBuilder2, 0,
338 CLSCTX_INPROC, (REFIID)IID_ICaptureGraphBuilder2,
339 (void **)&p_sys->p_capture_graph_builder2 ) ) )
341 p_sys->p_capture_graph_builder2->
342 SetFiltergraph((IGraphBuilder *)p_sys->p_graph);
345 p_sys->p_graph->QueryInterface( IID_IMediaControl,
346 (void **)&p_sys->p_control );
350 static void DeleteDirectShowGraph( access_sys_t *p_sys )
352 DeleteCrossbarRoutes( p_sys );
354 /* Remove filters from graph */
355 for( int i = 0; i < p_sys->i_streams; i++ )
357 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_capture_filter );
358 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_device_filter );
359 p_sys->pp_streams[i]->p_capture_filter->Release();
360 p_sys->pp_streams[i]->p_device_filter->Release();
363 /* Release directshow objects */
364 if( p_sys->p_control )
366 p_sys->p_control->Release();
367 p_sys->p_control = NULL;
369 if( p_sys->p_capture_graph_builder2 )
371 p_sys->p_capture_graph_builder2->Release();
372 p_sys->p_capture_graph_builder2 = NULL;
377 p_sys->p_graph->Release();
378 p_sys->p_graph = NULL;
382 /*****************************************************************************
383 * CommonOpen: open direct show device
384 *****************************************************************************/
385 static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
386 bool b_access_demux )
391 /* Get/parse options and open device(s) */
392 string vdevname, adevname;
393 int i_width = 0, i_height = 0;
394 vlc_fourcc_t i_chroma = 0;
395 bool b_use_audio = true;
396 bool b_use_video = true;
398 /* Initialize OLE/COM */
401 var_Create( p_this, "dshow-config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
402 var_Create( p_this, "dshow-tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
403 psz_val = var_CreateGetString( p_this, "dshow-vdev" );
406 msg_Dbg( p_this, "dshow-vdev: %s", psz_val ) ;
407 /* skip none device */
408 if ( strncasecmp( psz_val, "none", 4 ) != 0 )
409 vdevname = string( psz_val );
411 b_use_video = false ;
415 psz_val = var_CreateGetString( p_this, "dshow-adev" );
418 msg_Dbg( p_this, "dshow-adev: %s", psz_val ) ;
419 /* skip none device */
420 if ( strncasecmp( psz_val, "none", 4 ) != 0 )
421 adevname = string( psz_val );
423 b_use_audio = false ;
427 static struct {const char *psz_size; int i_width; int i_height;} size_table[] =
428 { { "subqcif", 128, 96 }, { "qsif", 160, 120 }, { "qcif", 176, 144 },
429 { "sif", 320, 240 }, { "cif", 352, 288 }, { "d1", 640, 480 },
433 psz_val = var_CreateGetString( p_this, "dshow-size" );
434 if( !EMPTY_STR(psz_val) )
436 for( i = 0; size_table[i].psz_size; i++ )
438 if( !strcmp( psz_val, size_table[i].psz_size ) )
440 i_width = size_table[i].i_width;
441 i_height = size_table[i].i_height;
445 if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
448 i_width = strtol( psz_val, &psz_parser, 0 );
449 if( *psz_parser == 'x' || *psz_parser == 'X')
451 i_height = strtol( psz_parser + 1, &psz_parser, 0 );
453 msg_Dbg( p_this, "width x height %dx%d", i_width, i_height );
458 psz_val = var_CreateGetString( p_this, "dshow-chroma" );
459 i_chroma = vlc_fourcc_GetCodecFromString( UNKNOWN_ES, psz_val );
460 p_sys->b_chroma = i_chroma != 0;
463 var_Create( p_this, "dshow-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
464 var_Create( p_this, "dshow-tuner-channel",
465 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
466 var_Create( p_this, "dshow-tuner-frequency",
467 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
468 var_Create( p_this, "dshow-tuner-standard",
469 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
470 var_Create( p_this, "dshow-tuner-country",
471 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
472 var_Create( p_this, "dshow-tuner-input",
473 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
475 var_Create( p_this, "dshow-amtuner-mode",
476 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
478 var_Create( p_this, "dshow-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
480 var_Create( p_this, "dshow-video-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
481 var_Create( p_this, "dshow-audio-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
482 var_Create( p_this, "dshow-video-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
483 var_Create( p_this, "dshow-audio-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
486 /* Initialize some data */
487 p_sys->i_streams = 0;
488 p_sys->pp_streams = NULL;
489 p_sys->i_width = i_width;
490 p_sys->i_height = i_height;
491 p_sys->i_chroma = i_chroma;
493 p_sys->p_graph = NULL;
494 p_sys->p_capture_graph_builder2 = NULL;
495 p_sys->p_control = NULL;
497 /* Build directshow graph */
498 CreateDirectShowGraph( p_sys );
500 vlc_mutex_init( &p_sys->lock );
501 vlc_cond_init( &p_sys->wait );
503 if( !b_use_video && !b_use_audio )
505 dialog_Fatal( p_this, _("Capture failed"),
506 _("No video or audio device selected.") );
507 return VLC_EGENERIC ;
511 msg_Dbg( p_this, "skipping video device" ) ;
512 bool b_err_video = false ;
514 if( b_use_video && OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
516 msg_Err( p_this, "can't open video device");
520 if ( b_use_video && !b_err_video )
522 /* Check if we can handle the demuxing ourselves or need to spawn
523 * a demuxer module */
524 dshow_stream_t *p_stream = p_sys->pp_streams[p_sys->i_streams-1];
526 if( p_stream->mt.majortype == MEDIATYPE_Video )
528 if( /* Raw DV stream */
529 p_stream->i_fourcc == VLC_CODEC_DV ||
530 /* Raw MPEG video stream */
531 p_stream->i_fourcc == VLC_CODEC_MPGV )
537 /* Let the access (only) take care of that */
543 if( p_stream->mt.majortype == MEDIATYPE_Stream )
549 /* Let the access (only) take care of that */
553 if( var_GetBool( p_this, "dshow-tuner" ) )
555 /* FIXME: we do MEDIATYPE_Stream here so we don't do
557 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
558 p_stream->p_device_filter, 0 );
564 msg_Dbg( p_this, "skipping audio device") ;
566 bool b_err_audio = false ;
568 if( b_use_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
570 msg_Err( p_this, "can't open audio device");
574 if( ( b_use_video && b_err_video && b_use_audio && b_err_audio ) ||
575 ( !b_use_video && b_use_audio && b_err_audio ) ||
576 ( b_use_video && !b_use_audio && b_err_video ) )
578 msg_Err( p_this, "FATAL: could not open ANY device" ) ;
579 dialog_Fatal( p_this, _("Capture failed"),
580 _("VLC cannot open ANY capture device."
581 "Check the error log for details.") );
582 return VLC_EGENERIC ;
585 for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
587 int i_val = var_GetInteger( p_this, "dshow-video-input" );
589 p_sys->crossbar_routes[i].VideoInputIndex = i_val;
590 i_val = var_GetInteger( p_this, "dshow-video-output" );
592 p_sys->crossbar_routes[i].VideoOutputIndex = i_val;
593 i_val = var_GetInteger( p_this, "dshow-audio-input" );
595 p_sys->crossbar_routes[i].AudioInputIndex = i_val;
596 i_val = var_GetInteger( p_this, "dshow-audio-output" );
598 p_sys->crossbar_routes[i].AudioOutputIndex = i_val;
600 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
601 LONG VideoInputIndex = p_sys->crossbar_routes[i].VideoInputIndex;
602 LONG VideoOutputIndex = p_sys->crossbar_routes[i].VideoOutputIndex;
603 LONG AudioInputIndex = p_sys->crossbar_routes[i].AudioInputIndex;
604 LONG AudioOutputIndex = p_sys->crossbar_routes[i].AudioOutputIndex;
606 if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
608 msg_Dbg( p_this, "crossbar at depth %d, routed video "
609 "output %ld to video input %ld", i, VideoOutputIndex,
612 if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
614 if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
617 msg_Dbg(p_this, "crossbar at depth %d, routed audio "
618 "output %ld to audio input %ld", i,
619 AudioOutputIndex, AudioInputIndex );
624 msg_Err( p_this, "crossbar at depth %d could not route video "
625 "output %ld to input %ld", i, VideoOutputIndex, VideoInputIndex );
629 ** Show properties pages from other filters in graph
631 if( var_GetBool( p_this, "dshow-config" ) )
633 for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
635 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
638 if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
641 ShowPropertyPage( p_XF );
647 /* Initialize some data */
648 p_sys->i_current_stream = 0;
650 if( !p_sys->i_streams ) return VLC_EGENERIC;
655 /*****************************************************************************
656 * DemuxOpen: open direct show device as an access_demux module
657 *****************************************************************************/
658 static int DemuxOpen( vlc_object_t *p_this )
660 demux_t *p_demux = (demux_t *)p_this;
664 p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
667 p_demux->p_sys = (demux_sys_t *)p_sys;
669 if( CommonOpen( p_this, p_sys, true ) != VLC_SUCCESS )
671 CommonClose( p_this, p_sys );
675 /* Everything is ready. Let's rock baby */
676 msg_Dbg( p_this, "Playing...");
677 p_sys->p_control->Run();
679 p_demux->pf_demux = Demux;
680 p_demux->pf_control = DemuxControl;
681 p_demux->info.i_update = 0;
682 p_demux->info.i_title = 0;
683 p_demux->info.i_seekpoint = 0;
685 for( i = 0; i < p_sys->i_streams; i++ )
687 dshow_stream_t *p_stream = p_sys->pp_streams[i];
690 if( p_stream->mt.majortype == MEDIATYPE_Video )
692 char *psz_aspect = var_CreateGetString( p_this, "dshow-aspect-ratio" );
693 char *psz_delim = !EMPTY_STR( psz_aspect ) ? strchr( psz_aspect, ':' ) : NULL;
695 es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
697 fmt.video.i_width = p_stream->header.video.bmiHeader.biWidth;
698 fmt.video.i_height = p_stream->header.video.bmiHeader.biHeight;
702 fmt.video.i_sar_num = atoi( psz_aspect ) * fmt.video.i_height;
703 fmt.video.i_sar_den = atoi( psz_delim + 1 ) * fmt.video.i_width;
707 fmt.video.i_sar_num = 4 * fmt.video.i_height;
708 fmt.video.i_sar_den = 3 * fmt.video.i_width;
712 if( !p_stream->header.video.bmiHeader.biCompression )
714 /* RGB DIB are coded from bottom to top */
715 fmt.video.i_height = (unsigned int)(-(int)fmt.video.i_height);
718 /* Setup rgb mask for RGB formats */
719 if( p_stream->i_fourcc == VLC_CODEC_RGB24 )
721 /* This is in RGB format
722 http://msdn.microsoft.com/en-us/library/dd407253%28VS.85%29.aspx?ppud=4
724 fmt.video.i_rmask = 0x00ff0000;
725 fmt.video.i_gmask = 0x0000ff00;
726 fmt.video.i_bmask = 0x000000ff;
729 if( p_stream->header.video.AvgTimePerFrame )
731 fmt.video.i_frame_rate = 10000000;
732 fmt.video.i_frame_rate_base =
733 p_stream->header.video.AvgTimePerFrame;
736 else if( p_stream->mt.majortype == MEDIATYPE_Audio )
738 es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
740 fmt.audio.i_channels = p_stream->header.audio.nChannels;
741 fmt.audio.i_rate = p_stream->header.audio.nSamplesPerSec;
742 fmt.audio.i_bitspersample = p_stream->header.audio.wBitsPerSample;
743 fmt.audio.i_blockalign = fmt.audio.i_channels *
744 fmt.audio.i_bitspersample / 8;
745 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
746 fmt.audio.i_bitspersample;
749 p_stream->p_es = es_out_Add( p_demux->out, &fmt );
755 /*****************************************************************************
756 * AccessOpen: open direct show device as an access module
757 *****************************************************************************/
758 static int AccessOpen( vlc_object_t *p_this )
760 access_t *p_access = (access_t*)p_this;
763 p_access->p_sys = p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
767 if( CommonOpen( p_this, p_sys, false ) != VLC_SUCCESS )
769 CommonClose( p_this, p_sys );
773 dshow_stream_t *p_stream = p_sys->pp_streams[0];
775 /* Check if we need to force demuxers */
776 if( !p_access->psz_demux || !*p_access->psz_demux )
778 if( p_stream->i_fourcc == VLC_CODEC_DV )
780 free( p_access->psz_demux );
781 p_access->psz_demux = strdup( "rawdv" );
783 else if( p_stream->i_fourcc == VLC_CODEC_MPGV )
785 free( p_access->psz_demux );
786 p_access->psz_demux = strdup( "mpgv" );
791 p_access->pf_read = NULL;
792 p_access->pf_block = ReadCompressed;
793 p_access->pf_control = AccessControl;
794 p_access->pf_seek = NULL;
795 p_access->info.i_update = 0;
796 p_access->info.i_size = 0;
797 p_access->info.i_pos = 0;
798 p_access->info.b_eof = false;
799 p_access->info.i_title = 0;
800 p_access->info.i_seekpoint = 0;
801 p_access->p_sys = p_sys;
803 /* Everything is ready. Let's rock baby */
804 msg_Dbg( p_this, "Playing...");
805 p_sys->p_control->Run();
810 /*****************************************************************************
811 * CommonClose: close device
812 *****************************************************************************/
813 static void CommonClose( vlc_object_t *p_this, access_sys_t *p_sys )
815 msg_Dbg( p_this, "releasing DirectShow");
817 DeleteDirectShowGraph( p_sys );
819 /* Uninitialize OLE/COM */
822 for( int i = 0; i < p_sys->i_streams; i++ ) delete p_sys->pp_streams[i];
823 if( p_sys->i_streams ) free( p_sys->pp_streams );
825 vlc_mutex_destroy( &p_sys->lock );
826 vlc_cond_destroy( &p_sys->wait );
831 /*****************************************************************************
832 * AccessClose: close device
833 *****************************************************************************/
834 static void AccessClose( vlc_object_t *p_this )
836 access_t *p_access = (access_t *)p_this;
837 access_sys_t *p_sys = p_access->p_sys;
839 /* Stop capturing stuff */
840 p_sys->p_control->Stop();
842 CommonClose( p_this, p_sys );
845 /*****************************************************************************
846 * DemuxClose: close device
847 *****************************************************************************/
848 static void DemuxClose( vlc_object_t *p_this )
850 demux_t *p_demux = (demux_t *)p_this;
851 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
853 /* Stop capturing stuff */
854 p_sys->p_control->Stop();
856 CommonClose( p_this, p_sys );
859 /****************************************************************************
861 ****************************************************************************/
862 static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
863 IBaseFilter *p_filter,
864 CaptureFilter *p_capture_filter )
866 CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
868 AM_MEDIA_TYPE mediaType = p_input_pin->CustomGetMediaType();
870 if( p_sys->p_capture_graph_builder2 )
872 if( FAILED(p_sys->p_capture_graph_builder2->
873 RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
874 p_filter, 0, (IBaseFilter *)p_capture_filter )) )
879 // Sort out all the possible video inputs
880 // The class needs to be given the capture filters ANALOGVIDEO input pin
881 IEnumPins *pins = NULL;
882 if( ( mediaType.majortype == MEDIATYPE_Video ||
883 mediaType.majortype == MEDIATYPE_Stream ) &&
884 SUCCEEDED(p_filter->EnumPins(&pins)) )
890 IKsPropertySet *pKs = NULL;
894 while( !Found && ( S_OK == pins->Next(1, &pP, &n) ) )
896 if( S_OK == pP->QueryPinInfo(&pinInfo) )
898 // is this pin an ANALOGVIDEOIN input pin?
899 if( pinInfo.dir == PINDIR_INPUT &&
900 pP->QueryInterface( IID_IKsPropertySet,
901 (void **)&pKs ) == S_OK )
903 if( pKs->Get( AMPROPSETID_Pin,
904 AMPROPERTY_PIN_CATEGORY, NULL, 0,
905 &guid, sizeof(GUID), &dw ) == S_OK )
907 if( guid == PIN_CATEGORY_ANALOGVIDEOIN )
909 // recursively search crossbar routes
910 FindCrossbarRoutes( p_this, p_sys, pP, 0 );
917 pinInfo.pFilter->Release();
922 msg_Dbg( p_this, "ConnectFilters: graph_builder2 available.") ;
924 msg_Warn( p_this, "ConnectFilters: No crossBar routes found (incobatible pin types)" ) ;
930 IEnumPins *p_enumpins;
933 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;
935 while( S_OK == p_enumpins->Next( 1, &p_pin, NULL ) )
937 PIN_DIRECTION pin_dir;
938 p_pin->QueryDirection( &pin_dir );
940 if( pin_dir == PINDIR_OUTPUT &&
941 p_sys->p_graph->ConnectDirect( p_pin, (IPin *)p_input_pin,
945 p_enumpins->Release();
951 p_enumpins->Release();
957 * get fourcc priority from arbritary preference, the higher the better
959 static int GetFourCCPriority( int i_fourcc )
967 case VLC_FOURCC('a','r','a','w'):
969 case VLC_CODEC_RGB24:
972 case VLC_CODEC_RGB32:
980 #define MAX_MEDIA_TYPES 32
982 static int OpenDevice( vlc_object_t *p_this, access_sys_t *p_sys,
983 string devicename, bool b_audio )
985 /* See if device is already opened */
986 for( int i = 0; i < p_sys->i_streams; i++ )
988 if( devicename.size() &&
989 p_sys->pp_streams[i]->devicename == devicename )
996 list<string> list_devices;
998 /* Enumerate devices and display their names */
999 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1000 if( !list_devices.size() )
1001 return VLC_EGENERIC;
1003 list<string>::iterator iter;
1004 for( iter = list_devices.begin(); iter != list_devices.end(); iter++ )
1005 msg_Dbg( p_this, "found device: %s", iter->c_str() );
1007 /* If no device name was specified, pick the 1st one */
1008 if( devicename.size() == 0 )
1010 /* When none selected */
1011 devicename = *list_devices.begin();
1012 msg_Dbg( p_this, "asking for default device: %s", devicename.c_str() ) ;
1015 msg_Dbg( p_this, "asking for device: %s", devicename.c_str() ) ;
1016 // Use the system device enumerator and class enumerator to find
1017 // a capture/preview device, such as a desktop USB video camera.
1018 IBaseFilter *p_device_filter =
1019 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
1021 if( p_device_filter )
1022 msg_Dbg( p_this, "using device: %s", devicename.c_str() );
1025 msg_Err( p_this, "can't use device: %s, unsupported device type",
1026 devicename.c_str() );
1027 dialog_Fatal( p_this, _("Capture failed"),
1028 _("VLC cannot use the device \"%s\", because its "
1029 "type is not supported."), devicename.c_str() );
1030 return VLC_EGENERIC;
1033 // Retreive acceptable media types supported by device
1034 AM_MEDIA_TYPE media_types[MAX_MEDIA_TYPES];
1035 size_t media_count =
1036 EnumDeviceCaps( p_this, p_device_filter, b_audio ? 0 : p_sys->i_chroma,
1037 p_sys->i_width, p_sys->i_height,
1038 b_audio ? var_CreateGetInteger( p_this, "dshow-audio-channels" ) : 0,
1039 b_audio ? var_CreateGetInteger( p_this, "dshow-audio-samplerate" ) : 0,
1040 b_audio ? var_CreateGetInteger( p_this, "dshow-audio-bitspersample" ) : 0,
1041 media_types, MAX_MEDIA_TYPES );
1043 AM_MEDIA_TYPE *mt = NULL;
1045 if( media_count > 0 )
1047 mt = (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * media_count);
1049 // Order and copy returned media types according to arbitrary
1051 for( size_t c = 0; c < media_count; c++ )
1054 GetFourCCPriority(GetFourCCFromMediaType(media_types[c]));
1055 size_t slot_copy = c;
1056 for( size_t d = c+1; d < media_count; d++ )
1059 GetFourCCPriority(GetFourCCFromMediaType(media_types[d]));
1060 if( priority > slot_priority )
1062 slot_priority = priority;
1066 if( slot_copy != c )
1068 mt[c] = media_types[slot_copy];
1069 media_types[slot_copy] = media_types[c];
1073 mt[c] = media_types[c];
1078 /* capture device */
1079 msg_Err( p_this, "capture device '%s' does not support required parameters !", devicename.c_str() );
1080 dialog_Fatal( p_this, _("Capture failed"),
1081 _("The capture device \"%s\" does not support the "
1082 "required parameters."), devicename.c_str() );
1083 p_device_filter->Release();
1084 return VLC_EGENERIC;
1087 /* Create and add our capture filter */
1088 CaptureFilter *p_capture_filter =
1089 new CaptureFilter( p_this, p_sys, mt, media_count );
1090 p_sys->p_graph->AddFilter( p_capture_filter, 0 );
1092 /* Add the device filter to the graph (seems necessary with VfW before
1093 * accessing pin attributes). */
1094 p_sys->p_graph->AddFilter( p_device_filter, 0 );
1096 /* Attempt to connect one of this device's capture output pins */
1097 msg_Dbg( p_this, "connecting filters" );
1098 if( ConnectFilters( p_this, p_sys, p_device_filter, p_capture_filter ) )
1101 msg_Dbg( p_this, "filters connected successfully !" );
1103 dshow_stream_t dshow_stream;
1104 dshow_stream.b_pts = false;
1105 dshow_stream.p_es = 0;
1107 p_capture_filter->CustomGetPin()->CustomGetMediaType();
1109 /* Show Device properties. Done here so the VLC stream is setup with
1110 * the proper parameters. */
1111 if( var_GetBool( p_this, "dshow-config" ) )
1113 ShowDeviceProperties( p_this, p_sys->p_capture_graph_builder2,
1114 p_device_filter, b_audio );
1117 ConfigTuner( p_this, p_sys->p_capture_graph_builder2,
1120 if( var_GetBool( p_this, "dshow-tuner" ) &&
1121 dshow_stream.mt.majortype != MEDIATYPE_Stream )
1123 /* FIXME: we do MEDIATYPE_Stream later so we don't do it twice. */
1124 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
1125 p_device_filter, b_audio );
1129 p_capture_filter->CustomGetPin()->CustomGetMediaType();
1131 dshow_stream.i_fourcc = GetFourCCFromMediaType( dshow_stream.mt );
1132 if( dshow_stream.i_fourcc )
1134 if( dshow_stream.mt.majortype == MEDIATYPE_Video )
1136 dshow_stream.header.video =
1137 *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
1138 msg_Dbg( p_this, "MEDIATYPE_Video" );
1139 msg_Dbg( p_this, "selected video pin accepts format: %4.4s",
1140 (char *)&dshow_stream.i_fourcc);
1142 else if( dshow_stream.mt.majortype == MEDIATYPE_Audio )
1144 dshow_stream.header.audio =
1145 *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
1146 msg_Dbg( p_this, "MEDIATYPE_Audio" );
1147 msg_Dbg( p_this, "selected audio pin accepts format: %4.4s",
1148 (char *)&dshow_stream.i_fourcc);
1150 else if( dshow_stream.mt.majortype == MEDIATYPE_Stream )
1152 msg_Dbg( p_this, "MEDIATYPE_Stream" );
1153 msg_Dbg( p_this, "selected stream pin accepts format: %4.4s",
1154 (char *)&dshow_stream.i_fourcc);
1158 msg_Dbg( p_this, "unknown stream majortype" );
1162 /* Add directshow elementary stream to our list */
1163 dshow_stream.p_device_filter = p_device_filter;
1164 dshow_stream.p_capture_filter = p_capture_filter;
1166 p_sys->pp_streams = (dshow_stream_t **)xrealloc( p_sys->pp_streams,
1167 sizeof(dshow_stream_t *) * (p_sys->i_streams + 1) );
1168 p_sys->pp_streams[p_sys->i_streams] = new dshow_stream_t;
1169 *p_sys->pp_streams[p_sys->i_streams++] = dshow_stream;
1176 /* Remove filters from graph */
1177 p_sys->p_graph->RemoveFilter( p_device_filter );
1178 p_sys->p_graph->RemoveFilter( p_capture_filter );
1180 /* Release objects */
1181 p_device_filter->Release();
1182 p_capture_filter->Release();
1184 return VLC_EGENERIC;
1187 /* FindCaptureDevices:: This Function had two purposes :
1188 Returns the list of capture devices when p_listdevices != NULL
1189 Creates an IBaseFilter when p_devicename corresponds to an existing devname
1190 These actions *may* be requested whith a single call.
1192 static IBaseFilter *
1193 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
1194 list<string> *p_listdevices, bool b_audio )
1196 IBaseFilter *p_base_filter = NULL;
1197 IMoniker *p_moniker = NULL;
1200 list<string> devicelist;
1202 /* Create the system device enumerator */
1203 ICreateDevEnum *p_dev_enum = NULL;
1205 hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
1206 IID_ICreateDevEnum, (void **)&p_dev_enum );
1209 msg_Err( p_this, "failed to create the device enumerator (0x%lx)", hr);
1213 /* Create an enumerator for the video capture devices */
1214 IEnumMoniker *p_class_enum = NULL;
1216 hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
1219 hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
1221 p_dev_enum->Release();
1224 msg_Err( p_this, "failed to create the class enumerator (0x%lx)", hr );
1228 /* If there are no enumerators for the requested type, then
1229 * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
1230 if( p_class_enum == NULL )
1232 msg_Err( p_this, "no %s capture device was detected", ( b_audio ? "audio" : "video" ) );
1236 /* Enumerate the devices */
1238 /* Note that if the Next() call succeeds but there are no monikers,
1239 * it will return S_FALSE (which is not a failure). Therefore, we check
1240 * that the return code is S_OK instead of using SUCCEEDED() macro. */
1242 while( p_class_enum->Next( 1, &p_moniker, &i_fetched ) == S_OK )
1244 /* Getting the property page to get the device name */
1245 IPropertyBag *p_bag;
1246 hr = p_moniker->BindToStorage( 0, 0, IID_IPropertyBag,
1252 hr = p_bag->Read( L"FriendlyName", &var, NULL );
1256 char *p_buf = FromWide( var.bstrVal );
1257 string devname = string(p_buf);
1261 /* find out if this name is already used by a previously found device */
1262 list<string>::const_iterator iter = devicelist.begin();
1263 list<string>::const_iterator end = devicelist.end();
1264 string ordevname = devname ;
1265 while ( iter != end )
1267 if( 0 == (*iter).compare( devname ) )
1268 { /* devname is on the list. Try another name with sequence
1269 number apended and then rescan until a unique entry is found*/
1271 snprintf(seq, 16, " #%d", ++dup);
1272 devname = ordevname + seq;
1273 iter = devicelist.begin();
1278 devicelist.push_back( devname );
1280 if( p_devicename && *p_devicename == devname )
1282 msg_Dbg( p_this, "asked for %s, binding to %s", p_devicename->c_str() , devname.c_str() ) ;
1283 /* NULL possibly means we don't need BindMoniker BindCtx ?? */
1284 hr = p_moniker->BindToObject( NULL, 0, IID_IBaseFilter,
1285 (void **)&p_base_filter );
1288 msg_Err( p_this, "couldn't bind moniker to filter "
1289 "object (0x%lx)", hr );
1290 p_moniker->Release();
1291 p_class_enum->Release();
1294 p_moniker->Release();
1295 p_class_enum->Release();
1296 return p_base_filter;
1301 p_moniker->Release();
1304 p_class_enum->Release();
1306 if( p_listdevices ) {
1308 *p_listdevices = devicelist;
1313 static size_t EnumDeviceCaps( vlc_object_t *p_this, IBaseFilter *p_filter,
1314 int i_fourcc, int i_width, int i_height,
1315 int i_channels, int i_samplespersec,
1316 int i_bitspersample, AM_MEDIA_TYPE *mt,
1319 IEnumPins *p_enumpins;
1321 IEnumMediaTypes *p_enummt;
1322 size_t mt_count = 0;
1324 LONGLONG i_AvgTimePerFrame = 0;
1325 float r_fps = var_GetFloat( p_this, "dshow-fps" );
1327 i_AvgTimePerFrame = 10000000000LL/(LONGLONG)(r_fps*1000.0f);
1329 if( FAILED(p_filter->EnumPins( &p_enumpins )) )
1331 msg_Dbg( p_this, "EnumDeviceCaps failed: no pin enumeration !");
1335 while( S_OK == p_enumpins->Next( 1, &p_output_pin, NULL ) )
1339 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1341 msg_Dbg( p_this, "EnumDeviceCaps: %s pin: %S",
1342 info.dir == PINDIR_INPUT ? "input" : "output",
1344 if( info.pFilter ) info.pFilter->Release();
1347 p_output_pin->Release();
1350 p_enumpins->Reset();
1352 while( !mt_count && p_enumpins->Next( 1, &p_output_pin, NULL ) == S_OK )
1356 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1358 if( info.pFilter ) info.pFilter->Release();
1359 if( info.dir == PINDIR_INPUT )
1361 p_output_pin->Release();
1364 msg_Dbg( p_this, "EnumDeviceCaps: trying pin %S", info.achName );
1367 AM_MEDIA_TYPE *p_mt;
1370 ** Configure pin with a default compatible media if possible
1373 IAMStreamConfig *pSC;
1374 if( SUCCEEDED(p_output_pin->QueryInterface( IID_IAMStreamConfig,
1377 int piCount, piSize;
1378 if( SUCCEEDED(pSC->GetNumberOfCapabilities(&piCount, &piSize)) )
1380 BYTE *pSCC= (BYTE *)CoTaskMemAlloc(piSize);
1383 int i_priority = -1;
1384 for( int i=0; i<piCount; ++i )
1386 if( SUCCEEDED(pSC->GetStreamCaps(i, &p_mt, pSCC)) )
1388 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1389 int i_current_priority = GetFourCCPriority(i_current_fourcc);
1391 if( (i_fourcc && (i_current_fourcc != i_fourcc))
1392 || (i_priority > i_current_priority) )
1394 // unwanted chroma, try next media type
1395 FreeMediaType( *p_mt );
1396 CoTaskMemFree( (PVOID)p_mt );
1400 if( MEDIATYPE_Video == p_mt->majortype
1401 && FORMAT_VideoInfo == p_mt->formattype )
1403 VIDEO_STREAM_CONFIG_CAPS *pVSCC = reinterpret_cast<VIDEO_STREAM_CONFIG_CAPS*>(pSCC);
1404 VIDEOINFOHEADER *pVih = reinterpret_cast<VIDEOINFOHEADER*>(p_mt->pbFormat);
1406 if( i_AvgTimePerFrame )
1408 if( pVSCC->MinFrameInterval > i_AvgTimePerFrame
1409 || i_AvgTimePerFrame > pVSCC->MaxFrameInterval )
1411 // required frame rate not compatible, try next media type
1412 FreeMediaType( *p_mt );
1413 CoTaskMemFree( (PVOID)p_mt );
1416 pVih->AvgTimePerFrame = i_AvgTimePerFrame;
1421 if(( !pVSCC->OutputGranularityX
1422 && i_width != pVSCC->MinOutputSize.cx
1423 && i_width != pVSCC->MaxOutputSize.cx)
1425 ( pVSCC->OutputGranularityX
1426 && ((i_width % pVSCC->OutputGranularityX)
1427 || pVSCC->MinOutputSize.cx > i_width
1428 || i_width > pVSCC->MaxOutputSize.cx )))
1430 // required width not compatible, try next media type
1431 FreeMediaType( *p_mt );
1432 CoTaskMemFree( (PVOID)p_mt );
1435 pVih->bmiHeader.biWidth = i_width;
1440 if(( !pVSCC->OutputGranularityY
1441 && i_height != pVSCC->MinOutputSize.cy
1442 && i_height != pVSCC->MaxOutputSize.cy)
1444 ( pVSCC->OutputGranularityY
1445 && ((i_height % pVSCC->OutputGranularityY)
1446 || pVSCC->MinOutputSize.cy > i_height
1447 || i_height > pVSCC->MaxOutputSize.cy )))
1449 // required height not compatible, try next media type
1450 FreeMediaType( *p_mt );
1451 CoTaskMemFree( (PVOID)p_mt );
1454 pVih->bmiHeader.biHeight = i_height;
1457 // Set the sample size and image size.
1458 // (Round the image width up to a DWORD boundary.)
1459 p_mt->lSampleSize = pVih->bmiHeader.biSizeImage =
1460 ((pVih->bmiHeader.biWidth + 3) & ~3) *
1461 pVih->bmiHeader.biHeight * (pVih->bmiHeader.biBitCount>>3);
1463 // no cropping, use full video input buffer
1464 memset(&(pVih->rcSource), 0, sizeof(RECT));
1465 memset(&(pVih->rcTarget), 0, sizeof(RECT));
1467 // select this format as default
1468 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1470 i_priority = i_current_priority;
1472 // no need to check any more media types
1476 else if( p_mt->majortype == MEDIATYPE_Audio
1477 && p_mt->formattype == FORMAT_WaveFormatEx )
1479 AUDIO_STREAM_CONFIG_CAPS *pASCC = reinterpret_cast<AUDIO_STREAM_CONFIG_CAPS*>(pSCC);
1480 WAVEFORMATEX *pWfx = reinterpret_cast<WAVEFORMATEX*>(p_mt->pbFormat);
1482 if( i_current_fourcc && (WAVE_FORMAT_PCM == pWfx->wFormatTag) )
1484 int val = i_channels;
1488 if( ( !pASCC->ChannelsGranularity
1489 && (unsigned int)val != pASCC->MinimumChannels
1490 && (unsigned int)val != pASCC->MaximumChannels)
1492 ( pASCC->ChannelsGranularity
1493 && ((val % pASCC->ChannelsGranularity)
1494 || (unsigned int)val < pASCC->MinimumChannels
1495 || (unsigned int)val > pASCC->MaximumChannels)))
1497 // required number channels not available, try next media type
1498 FreeMediaType( *p_mt );
1499 CoTaskMemFree( (PVOID)p_mt );
1502 pWfx->nChannels = val;
1504 val = i_samplespersec;
1508 if( ( !pASCC->SampleFrequencyGranularity
1509 && (unsigned int)val != pASCC->MinimumSampleFrequency
1510 && (unsigned int)val != pASCC->MaximumSampleFrequency)
1512 ( pASCC->SampleFrequencyGranularity
1513 && ((val % pASCC->SampleFrequencyGranularity)
1514 || (unsigned int)val < pASCC->MinimumSampleFrequency
1515 || (unsigned int)val > pASCC->MaximumSampleFrequency )))
1517 // required sampling rate not available, try next media type
1518 FreeMediaType( *p_mt );
1519 CoTaskMemFree( (PVOID)p_mt );
1522 pWfx->nSamplesPerSec = val;
1524 val = i_bitspersample;
1527 if( VLC_CODEC_FL32 == i_current_fourcc )
1533 if( ( !pASCC->BitsPerSampleGranularity
1534 && (unsigned int)val != pASCC->MinimumBitsPerSample
1535 && (unsigned int)val != pASCC->MaximumBitsPerSample )
1537 ( pASCC->BitsPerSampleGranularity
1538 && ((val % pASCC->BitsPerSampleGranularity)
1539 || (unsigned int)val < pASCC->MinimumBitsPerSample
1540 || (unsigned int)val > pASCC->MaximumBitsPerSample )))
1542 // required sample size not available, try next media type
1543 FreeMediaType( *p_mt );
1544 CoTaskMemFree( (PVOID)p_mt );
1548 pWfx->wBitsPerSample = val;
1549 pWfx->nBlockAlign = (pWfx->wBitsPerSample * pWfx->nChannels)/8;
1550 pWfx->nAvgBytesPerSec = pWfx->nSamplesPerSec * pWfx->nBlockAlign;
1552 // select this format as default
1553 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1555 i_priority = i_current_priority;
1559 FreeMediaType( *p_mt );
1560 CoTaskMemFree( (PVOID)p_mt );
1563 CoTaskMemFree( (LPVOID)pSCC );
1564 if( i_priority >= 0 )
1565 msg_Dbg( p_this, "EnumDeviceCaps: input pin default format configured");
1572 ** Probe pin for available medias (may be a previously configured one)
1575 if( FAILED( p_output_pin->EnumMediaTypes( &p_enummt ) ) )
1577 p_output_pin->Release();
1581 while( p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1583 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1584 if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Video
1585 && p_mt->formattype == FORMAT_VideoInfo )
1587 int i_current_width = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1588 int i_current_height = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1589 LONGLONG i_current_atpf = ((VIDEOINFOHEADER *)p_mt->pbFormat)->AvgTimePerFrame;
1591 if( i_current_height < 0 )
1592 i_current_height = -i_current_height;
1594 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1595 "accepts chroma: %4.4s, width:%i, height:%i, fps:%f",
1596 (char *)&i_current_fourcc, i_current_width,
1597 i_current_height, (10000000.0f/((float)i_current_atpf)) );
1599 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1600 ( !i_width || i_width == i_current_width ) &&
1601 ( !i_height || i_height == i_current_height ) &&
1602 ( !i_AvgTimePerFrame || i_AvgTimePerFrame == i_current_atpf ) &&
1606 mt[mt_count++] = *p_mt;
1608 else FreeMediaType( *p_mt );
1610 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Audio
1611 && p_mt->formattype == FORMAT_WaveFormatEx)
1613 int i_current_channels =
1614 ((WAVEFORMATEX *)p_mt->pbFormat)->nChannels;
1615 int i_current_samplespersec =
1616 ((WAVEFORMATEX *)p_mt->pbFormat)->nSamplesPerSec;
1617 int i_current_bitspersample =
1618 ((WAVEFORMATEX *)p_mt->pbFormat)->wBitsPerSample;
1620 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1621 "accepts format: %4.4s, channels:%i, "
1622 "samples/sec:%i bits/sample:%i",
1623 (char *)&i_current_fourcc, i_current_channels,
1624 i_current_samplespersec, i_current_bitspersample);
1626 if( (!i_channels || i_channels == i_current_channels) &&
1627 (!i_samplespersec ||
1628 i_samplespersec == i_current_samplespersec) &&
1629 (!i_bitspersample ||
1630 i_bitspersample == i_current_bitspersample) &&
1634 mt[mt_count++] = *p_mt;
1636 /* Setup a few properties like the audio latency */
1637 IAMBufferNegotiation *p_ambuf;
1638 if( SUCCEEDED( p_output_pin->QueryInterface(
1639 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
1641 ALLOCATOR_PROPERTIES AllocProp;
1642 AllocProp.cbAlign = -1;
1644 /* 100 ms of latency */
1645 AllocProp.cbBuffer = i_current_channels *
1646 i_current_samplespersec *
1647 i_current_bitspersample / 8 / 10;
1649 AllocProp.cbPrefix = -1;
1650 AllocProp.cBuffers = -1;
1651 p_ambuf->SuggestAllocatorProperties( &AllocProp );
1655 else FreeMediaType( *p_mt );
1657 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Stream )
1659 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1660 "accepts stream format: %4.4s",
1661 (char *)&i_current_fourcc );
1663 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1667 mt[mt_count++] = *p_mt;
1668 i_fourcc = i_current_fourcc;
1670 else FreeMediaType( *p_mt );
1674 const char * psz_type = "unknown";
1675 if( p_mt->majortype == MEDIATYPE_Video ) psz_type = "video";
1676 if( p_mt->majortype == MEDIATYPE_Audio ) psz_type = "audio";
1677 if( p_mt->majortype == MEDIATYPE_Stream ) psz_type = "stream";
1678 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: unsupported format "
1679 "(%s %4.4s)", psz_type, (char *)&p_mt->subtype );
1681 FreeMediaType( *p_mt );
1683 CoTaskMemFree( (PVOID)p_mt );
1686 if( !mt_count && p_enummt->Reset() == S_OK )
1688 // VLC did not find any supported MEDIATYPE for this output pin.
1689 // However the graph builder might insert converter filters in
1690 // the graph if we use a different codec in VLC filter input pin.
1691 // however, in order to avoid nasty surprises, make use of this
1692 // facility only for known unsupported codecs.
1694 while( !mt_count && p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1696 // the first four bytes of subtype GUID contains the codec FOURCC
1697 const char *pfcc = (char *)&p_mt->subtype;
1698 int i_current_fourcc = VLC_FOURCC(pfcc[0], pfcc[1], pfcc[2], pfcc[3]);
1699 if( VLC_FOURCC('H','C','W','2') == i_current_fourcc
1700 && p_mt->majortype == MEDIATYPE_Video )
1702 // output format for 'Hauppauge WinTV PVR PCI II Capture'
1703 // try I420 as an input format
1704 i_current_fourcc = VLC_CODEC_I420;
1705 if( !i_fourcc || i_fourcc == i_current_fourcc )
1707 // return alternative media type
1711 mtr.majortype = MEDIATYPE_Video;
1712 mtr.subtype = MEDIASUBTYPE_I420;
1713 mtr.bFixedSizeSamples = TRUE;
1714 mtr.bTemporalCompression = FALSE;
1716 mtr.formattype = FORMAT_VideoInfo;
1717 mtr.cbFormat = sizeof(vh);
1718 mtr.pbFormat = (BYTE *)&vh;
1720 memset(&vh, 0, sizeof(vh));
1722 vh.bmiHeader.biSize = sizeof(vh.bmiHeader);
1723 vh.bmiHeader.biWidth = i_width > 0 ? i_width :
1724 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1725 vh.bmiHeader.biHeight = i_height > 0 ? i_height :
1726 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1727 vh.bmiHeader.biPlanes = 3;
1728 vh.bmiHeader.biBitCount = 12;
1729 vh.bmiHeader.biCompression = VLC_CODEC_I420;
1730 vh.bmiHeader.biSizeImage = vh.bmiHeader.biWidth * 12 *
1731 vh.bmiHeader.biHeight / 8;
1732 mtr.lSampleSize = vh.bmiHeader.biSizeImage;
1734 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: using 'I420' in place of unsupported format 'HCW2'");
1736 if( SUCCEEDED(CopyMediaType(mt+mt_count, &mtr)) )
1740 FreeMediaType( *p_mt );
1744 p_enummt->Release();
1745 p_output_pin->Release();
1748 p_enumpins->Release();
1752 /*****************************************************************************
1753 * ReadCompressed: reads compressed (MPEG/DV) data from the device.
1754 *****************************************************************************
1755 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1757 *****************************************************************************/
1758 static block_t *ReadCompressed( access_t *p_access )
1760 access_sys_t *p_sys = p_access->p_sys;
1761 dshow_stream_t *p_stream = NULL;
1762 VLCMediaSample sample;
1764 /* Read 1 DV/MPEG frame (they contain the video and audio data) */
1766 /* There must be only 1 elementary stream to produce a valid stream
1767 * of MPEG or DV data */
1768 p_stream = p_sys->pp_streams[0];
1772 if( !vlc_object_alive (p_access) ) return NULL;
1774 /* Get new sample/frame from the elementary stream (blocking). */
1775 vlc_mutex_lock( &p_sys->lock );
1777 if( p_stream->p_capture_filter->CustomGetPin()
1778 ->CustomGetSample( &sample ) != S_OK )
1780 /* No data available. Wait until some data has arrived */
1781 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1782 vlc_mutex_unlock( &p_sys->lock );
1786 vlc_mutex_unlock( &p_sys->lock );
1793 int i_data_size = sample.p_sample->GetActualDataLength();
1795 if( !i_data_size || !(p_block = block_New( p_access, i_data_size )) )
1797 sample.p_sample->Release();
1801 sample.p_sample->GetPointer( &p_data );
1802 vlc_memcpy( p_block->p_buffer, p_data, i_data_size );
1803 sample.p_sample->Release();
1805 /* The caller got what he wanted */
1809 return NULL; /* never reached */
1812 /****************************************************************************
1814 ****************************************************************************/
1815 static int Demux( demux_t *p_demux )
1817 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
1819 int i_found_samples;
1821 i_found_samples = 0;
1822 vlc_mutex_lock( &p_sys->lock );
1824 while ( !i_found_samples )
1826 /* Try to grab samples from all streams */
1827 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1829 dshow_stream_t *p_stream = p_sys->pp_streams[i_stream];
1830 if( p_stream->p_capture_filter &&
1831 p_stream->p_capture_filter->CustomGetPin()
1832 ->CustomGetSamples( p_stream->samples_queue ) == S_OK )
1834 i_found_samples = 1;
1838 if ( !i_found_samples)
1840 /* Didn't find any audio nor video sample, just wait till the
1841 * dshow thread pushes some samples */
1842 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1843 /* Some DShow thread pushed data, or the OS broke the wait all
1844 * by itself. In all cases, it's *strongly* advised to test the
1845 * condition again, so let the loop do the test again */
1849 vlc_mutex_unlock( &p_sys->lock );
1851 for ( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1854 dshow_stream_t *p_stream = p_sys->pp_streams[i_stream];
1856 i_samples = p_stream->samples_queue.size();
1857 while ( i_samples > 0 )
1862 VLCMediaSample sample;
1864 sample = p_stream->samples_queue.front();
1865 p_stream->samples_queue.pop_front();
1867 i_data_size = sample.p_sample->GetActualDataLength();
1868 sample.p_sample->GetPointer( &p_data );
1870 REFERENCE_TIME i_pts, i_end_date;
1871 HRESULT hr = sample.p_sample->GetTime( &i_pts, &i_end_date );
1872 if( hr != VFW_S_NO_STOP_TIME && hr != S_OK ) i_pts = 0;
1876 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
1878 /* Use our data timestamp */
1879 i_pts = sample.i_timestamp;
1880 p_stream->b_pts = true;
1884 i_pts /= 10; /* Dshow works with 100 nano-seconds resolution */
1887 msg_Dbg( p_demux, "Read() stream: %i, size: %i, PTS: %"PRId64,
1888 i_stream, i_data_size, i_pts );
1891 p_block = block_New( p_demux, i_data_size );
1892 vlc_memcpy( p_block->p_buffer, p_data, i_data_size );
1893 p_block->i_pts = p_block->i_dts = i_pts;
1894 sample.p_sample->Release();
1896 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pts > 0 ? i_pts : 0 );
1897 es_out_Send( p_demux->out, p_stream->p_es, p_block );
1906 /*****************************************************************************
1908 *****************************************************************************/
1909 static int AccessControl( access_t *p_access, int i_query, va_list args )
1917 case ACCESS_CAN_SEEK:
1918 case ACCESS_CAN_FASTSEEK:
1919 case ACCESS_CAN_PAUSE:
1920 case ACCESS_CAN_CONTROL_PACE:
1921 pb_bool = (bool*)va_arg( args, bool* );
1926 case ACCESS_GET_PTS_DELAY:
1927 pi_64 = (int64_t*)va_arg( args, int64_t * );
1928 *pi_64 = var_GetInteger( p_access, "dshow-caching" ) * 1000;
1932 case ACCESS_SET_PAUSE_STATE:
1933 case ACCESS_GET_TITLE_INFO:
1934 case ACCESS_SET_TITLE:
1935 case ACCESS_SET_SEEKPOINT:
1936 case ACCESS_SET_PRIVATE_ID_STATE:
1937 return VLC_EGENERIC;
1940 msg_Warn( p_access, "unimplemented query in control" );
1941 return VLC_EGENERIC;
1947 /****************************************************************************
1949 ****************************************************************************/
1950 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1957 /* Special for access_demux */
1958 case DEMUX_CAN_PAUSE:
1959 case DEMUX_CAN_SEEK:
1960 case DEMUX_SET_PAUSE_STATE:
1961 case DEMUX_CAN_CONTROL_PACE:
1962 pb = (bool*)va_arg( args, bool * );
1966 case DEMUX_GET_PTS_DELAY:
1967 pi64 = (int64_t*)va_arg( args, int64_t * );
1968 *pi64 = var_GetInteger( p_demux, "dshow-caching" ) * 1000;
1971 case DEMUX_GET_TIME:
1972 pi64 = (int64_t*)va_arg( args, int64_t * );
1976 /* TODO implement others */
1978 return VLC_EGENERIC;
1981 return VLC_EGENERIC;
1984 /*****************************************************************************
1985 * config variable callback
1986 *****************************************************************************/
1987 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1988 vlc_value_t newval, vlc_value_t oldval, void * )
1990 module_config_t *p_item;
1991 bool b_audio = false;
1994 p_item = config_FindConfig( p_this, psz_name );
1995 if( !p_item ) return VLC_SUCCESS;
1997 if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = true;
1999 /* Clear-up the current list */
2000 if( p_item->i_list )
2002 /* Keep the 2 first entries */
2003 for( i = 2; i < p_item->i_list; i++ )
2005 free( const_cast<char *>(p_item->ppsz_list[i]) );
2006 free( const_cast<char *>(p_item->ppsz_list_text[i]) );
2008 /* TODO: Remove when no more needed */
2009 p_item->ppsz_list[i] = NULL;
2010 p_item->ppsz_list_text[i] = NULL;
2014 /* Find list of devices */
2015 list<string> list_devices;
2017 /* Initialize OLE/COM */
2020 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
2022 /* Uninitialize OLE/COM */
2025 if( list_devices.empty() ) return VLC_SUCCESS;
2027 p_item->ppsz_list = (char**)xrealloc( p_item->ppsz_list,
2028 (list_devices.size()+3) * sizeof(char *) );
2029 p_item->ppsz_list_text = (char**)xrealloc( p_item->ppsz_list_text,
2030 (list_devices.size()+3) * sizeof(char *) );
2032 list<string>::iterator iter;
2033 for( iter = list_devices.begin(), i = 2; iter != list_devices.end();
2036 p_item->ppsz_list[i] = strdup( iter->c_str() );
2037 p_item->ppsz_list_text[i] = NULL;
2040 p_item->ppsz_list[i] = NULL;
2041 p_item->ppsz_list_text[i] = NULL;
2043 /* Signal change to the interface */
2044 p_item->b_dirty = true;
2049 static int ConfigDevicesCallback( vlc_object_t *p_this, char const *psz_name,
2050 vlc_value_t newval, vlc_value_t oldval, void * )
2052 module_config_t *p_item;
2053 bool b_audio = false;
2054 char *psz_device = NULL;
2055 int i_ret = VLC_SUCCESS;
2057 if( !EMPTY_STR( newval.psz_string ) )
2058 psz_device = strdup( newval.psz_string );
2060 /* Initialize OLE/COM */
2063 p_item = config_FindConfig( p_this, psz_name );
2072 if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = true;
2078 devicename = psz_device ;
2082 /* If no device name was specified, pick the 1st one */
2083 list<string> list_devices;
2085 /* Enumerate devices */
2086 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
2087 if( list_devices.empty() )
2090 return VLC_EGENERIC;
2092 devicename = *list_devices.begin();
2095 IBaseFilter *p_device_filter =
2096 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
2097 if( p_device_filter )
2099 ShowPropertyPage( p_device_filter );
2100 p_device_filter->Release();
2104 msg_Err( p_this, "didn't find device: %s", devicename.c_str() );
2105 i_ret = VLC_EGENERIC;
2108 /* Uninitialize OLE/COM */
2115 /*****************************************************************************
2117 *****************************************************************************/
2119 static void ShowPropertyPage( IUnknown *obj )
2121 ISpecifyPropertyPages *p_spec;
2124 HRESULT hr = obj->QueryInterface( IID_ISpecifyPropertyPages,
2126 if( FAILED(hr) ) return;
2128 if( SUCCEEDED(p_spec->GetPages( &cauuid )) )
2130 if( cauuid.cElems > 0 )
2132 HWND hwnd_desktop = ::GetDesktopWindow();
2134 OleCreatePropertyFrame( hwnd_desktop, 30, 30, NULL, 1, &obj,
2135 cauuid.cElems, cauuid.pElems, 0, 0, NULL );
2137 CoTaskMemFree( cauuid.pElems );
2143 static void ShowDeviceProperties( vlc_object_t *p_this,
2144 ICaptureGraphBuilder2 *p_graph,
2145 IBaseFilter *p_device_filter,
2149 msg_Dbg( p_this, "configuring Device Properties" );
2152 * Video or audio capture filter page
2154 ShowPropertyPage( p_device_filter );
2159 if( p_graph && b_audio )
2161 IAMStreamConfig *p_SC;
2163 msg_Dbg( p_this, "showing WDM Audio Configuration Pages" );
2165 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2166 &MEDIATYPE_Audio, p_device_filter,
2167 IID_IAMStreamConfig, (void **)&p_SC );
2170 ShowPropertyPage(p_SC);
2178 HRESULT hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2179 &MEDIATYPE_Audio, p_device_filter,
2180 IID_IAMTVAudio, (void **)&p_TVA );
2183 ShowPropertyPage(p_TVA);
2191 if( p_graph && !b_audio )
2193 IAMStreamConfig *p_SC;
2195 msg_Dbg( p_this, "showing WDM Video Configuration Pages" );
2197 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2198 &MEDIATYPE_Interleaved, p_device_filter,
2199 IID_IAMStreamConfig, (void **)&p_SC );
2202 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2203 &MEDIATYPE_Video, p_device_filter,
2204 IID_IAMStreamConfig, (void **)&p_SC );
2209 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2210 &MEDIATYPE_Stream, p_device_filter,
2211 IID_IAMStreamConfig, (void **)&p_SC );
2216 ShowPropertyPage(p_SC);
2222 static void ShowTunerProperties( vlc_object_t *p_this,
2223 ICaptureGraphBuilder2 *p_graph,
2224 IBaseFilter *p_device_filter,
2228 msg_Dbg( p_this, "configuring Tuner Properties" );
2230 if( !p_graph || b_audio ) return;
2233 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2234 &MEDIATYPE_Interleaved, p_device_filter,
2235 IID_IAMTVTuner, (void **)&p_TV );
2238 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2239 &MEDIATYPE_Video, p_device_filter,
2240 IID_IAMTVTuner, (void **)&p_TV );
2245 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2246 &MEDIATYPE_Stream, p_device_filter,
2247 IID_IAMTVTuner, (void **)&p_TV );
2252 ShowPropertyPage(p_TV);
2257 static void ConfigTuner( vlc_object_t *p_this, ICaptureGraphBuilder2 *p_graph,
2258 IBaseFilter *p_device_filter )
2260 int i_channel, i_country, i_input, i_amtuner_mode, i_standard, i_frequency;
2265 if( !p_graph ) return;
2267 i_channel = var_GetInteger( p_this, "dshow-tuner-channel" );
2268 i_country = var_GetInteger( p_this, "dshow-tuner-country" );
2269 i_input = var_GetInteger( p_this, "dshow-tuner-input" );
2270 i_amtuner_mode = var_GetInteger( p_this, "dshow-amtuner-mode" );
2271 i_frequency = var_GetInteger( p_this, "dshow-tuner-frequency" );
2273 i_standards_list[var_CreateGetInteger( p_this, "dshow-tuner-standard" )];
2275 if( !i_channel && !i_frequency && !i_country && !i_input ) return; /* Nothing to do */
2277 msg_Dbg( p_this, "tuner config: channel %i, frequency %i, country %i, input type %i, standard %s",
2278 i_channel, i_frequency, i_country, i_input, ppsz_standards_list_text[i_standard] );
2281 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Interleaved,
2282 p_device_filter, IID_IAMTVTuner,
2286 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
2287 p_device_filter, IID_IAMTVTuner,
2293 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Stream,
2294 p_device_filter, IID_IAMTVTuner,
2300 msg_Dbg( p_this, "couldn't find tuner interface" );
2304 hr = p_TV->GetAvailableModes( &l_modes );
2305 if( SUCCEEDED(hr) && (l_modes & i_amtuner_mode) )
2307 hr = p_TV->put_Mode( (AMTunerModeType)i_amtuner_mode );
2310 if( i_input == 1 ) p_TV->put_InputType( 0, TunerInputCable );
2311 else if( i_input == 2 ) p_TV->put_InputType( 0, TunerInputAntenna );
2313 p_TV->put_CountryCode( i_country );
2315 if( i_frequency <= 0 ) p_TV->put_Channel( i_channel, AMTUNER_SUBCHAN_NO_TUNE,
2316 AMTUNER_SUBCHAN_NO_TUNE );
2318 if( i_frequency > 0 || i_standard > 0) {
2319 IKsPropertySet *pKs = NULL;
2320 DWORD dw_supported = 0;
2321 KSPROPERTY_TUNER_MODE_CAPS_S ModeCaps;
2322 KSPROPERTY_TUNER_FREQUENCY_S Frequency;
2323 KSPROPERTY_TUNER_STANDARD_S Standard;
2325 hr = p_TV->QueryInterface(IID_IKsPropertySet,(void **)&pKs);
2328 msg_Dbg( p_this, "Couldn't QI for IKsPropertySet" );
2332 memset(&ModeCaps,0,sizeof(KSPROPERTY_TUNER_MODE_CAPS_S));
2333 memset(&Frequency,0,sizeof(KSPROPERTY_TUNER_FREQUENCY_S));
2334 memset(&Standard,0,sizeof(KSPROPERTY_TUNER_STANDARD_S));
2335 ModeCaps.Mode = AMTUNER_MODE_TV;
2337 hr = pKs->QuerySupported(PROPSETID_TUNER,
2338 KSPROPERTY_TUNER_MODE_CAPS,&dw_supported);
2339 if(SUCCEEDED(hr) && dw_supported&KSPROPERTY_SUPPORT_GET)
2342 hr = pKs->Get(PROPSETID_TUNER,KSPROPERTY_TUNER_MODE_CAPS,
2343 INSTANCEDATA_OF_PROPERTY_PTR(&ModeCaps),
2344 INSTANCEDATA_OF_PROPERTY_SIZE(ModeCaps),
2351 msg_Dbg( p_this, "KSPROPERTY_TUNER_MODE_CAPS not supported!" );
2355 msg_Dbg( p_this, "Frequency range supproted from %d to %d.", ModeCaps.MinFrequency, ModeCaps.MaxFrequency);
2356 msg_Dbg( p_this, "Video standards supproted by the tuner: ");
2357 for(int i = 0 ; i < ARRAY_SIZE(ppsz_standards_list_text); i++) {
2358 if(ModeCaps.StandardsSupported & i_standards_list[i])
2359 msg_Dbg( p_this, "%s, ", ppsz_standards_list_text[i]);
2362 if(i_frequency > 0) {
2363 Frequency.Frequency=i_frequency;
2364 if(ModeCaps.Strategy==KS_TUNER_STRATEGY_DRIVER_TUNES)
2365 Frequency.TuningFlags=KS_TUNER_TUNING_FINE;
2367 Frequency.TuningFlags=KS_TUNER_TUNING_EXACT;
2369 if(i_frequency>=ModeCaps.MinFrequency && i_frequency<=ModeCaps.MaxFrequency)
2372 hr = pKs->Set(PROPSETID_TUNER,
2373 KSPROPERTY_TUNER_FREQUENCY,
2374 INSTANCEDATA_OF_PROPERTY_PTR(&Frequency),
2375 INSTANCEDATA_OF_PROPERTY_SIZE(Frequency),
2380 msg_Dbg( p_this, "Couldn't set KSPROPERTY_TUNER_FREQUENCY!" );
2386 msg_Dbg( p_this, "Requested frequency exceeds the supported range!" );
2391 if(i_standard > 0) {
2392 if(i_standard & ModeCaps.StandardsSupported )
2394 Standard.Standard = i_standard;
2395 hr = pKs->Set(PROPSETID_TUNER,
2396 KSPROPERTY_TUNER_STANDARD,
2397 INSTANCEDATA_OF_PROPERTY_PTR(&Standard),
2398 INSTANCEDATA_OF_PROPERTY_SIZE(Standard),
2403 msg_Dbg( p_this, "Couldn't set KSPROPERTY_TUNER_STANDARD!" );
2409 msg_Dbg( p_this, "Requested video standard is not supported by the tuner!" );