1 /*****************************************************************************
2 * dshow.cpp : DirectShow access and access_demux module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2004, 2006, 2008, 2010 the VideoLAN team
7 * Author: Gildas Bazin <gbazin@videolan.org>
8 * Damien Fouilleul <damienf@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #define __STDC_CONSTANT_MACROS 1
34 #define __STDC_FORMAT_MACROS 1
35 #define CFG_PREFIX "dshow-"
41 #define VLC_MODULE_LICENSE VLC_LICENSE_GPL_2_PLUS
42 #include <vlc_common.h>
43 #include <vlc_plugin.h>
44 #include <vlc_access.h>
45 #include <vlc_demux.h>
47 #include <vlc_dialog.h> /* dialog_Fatal */
48 #include <vlc_charset.h> /* FromWide */
51 #include "vlc_dshow.h"
56 #define INSTANCEDATA_OF_PROPERTY_PTR(x) ((PKSPROPERTY((x))) + 1)
57 #define INSTANCEDATA_OF_PROPERTY_SIZE(x) (sizeof((x)) - sizeof(KSPROPERTY))
59 /*****************************************************************************
60 * Access: local prototypes
61 *****************************************************************************/
62 static block_t *ReadCompressed( access_t * );
63 static int AccessControl ( access_t *, int, va_list );
65 static int Demux ( demux_t * );
66 static int DemuxControl( demux_t *, int, va_list );
68 static int OpenDevice( vlc_object_t *, access_sys_t *, string, bool );
69 static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
70 list<string> *, bool );
71 static size_t EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
72 int, int, int, int, int, int,
73 AM_MEDIA_TYPE *mt, size_t );
74 static bool ConnectFilters( vlc_object_t *, access_sys_t *,
75 IBaseFilter *, CaptureFilter * );
76 static int FindDevices( vlc_object_t *, const char *, char ***, char *** );
77 static int ConfigDevicesCallback( vlc_object_t *, char const *,
78 vlc_value_t, vlc_value_t, void * );
80 static void ShowPropertyPage( IUnknown * );
81 static void ShowDeviceProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
82 IBaseFilter *, bool );
83 static void ShowTunerProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
84 IBaseFilter *, bool );
85 static void ConfigTuner( vlc_object_t *, ICaptureGraphBuilder2 *,
88 /*****************************************************************************
90 *****************************************************************************/
91 static const int pi_tuner_input[] = { 0, 1, 2 };
92 static const char *const ppsz_tuner_input_text[] =
93 {N_("Default"), N_("Cable"), N_("Antenna")};
95 static const int pi_amtuner_mode[] = { AMTUNER_MODE_DEFAULT,
97 AMTUNER_MODE_FM_RADIO,
98 AMTUNER_MODE_AM_RADIO,
100 static const char *const ppsz_amtuner_mode_text[] = { N_("Default"),
106 static const int i_standards_list[] =
109 KS_AnalogVideo_NTSC_M, KS_AnalogVideo_NTSC_M_J, KS_AnalogVideo_NTSC_433,
110 KS_AnalogVideo_PAL_B, KS_AnalogVideo_PAL_D, KS_AnalogVideo_PAL_G,
111 KS_AnalogVideo_PAL_H, KS_AnalogVideo_PAL_I, KS_AnalogVideo_PAL_M,
112 KS_AnalogVideo_PAL_N, KS_AnalogVideo_PAL_60,
113 KS_AnalogVideo_SECAM_B, KS_AnalogVideo_SECAM_D, KS_AnalogVideo_SECAM_G,
114 KS_AnalogVideo_SECAM_H, KS_AnalogVideo_SECAM_K, KS_AnalogVideo_SECAM_K1,
115 KS_AnalogVideo_SECAM_L, KS_AnalogVideo_SECAM_L1,
116 KS_AnalogVideo_PAL_N_COMBO
118 static const char *const ppsz_standards_list_text[] =
121 "NTSC_M", "NTSC_M_J", "NTSC_443",
122 "PAL_B", "PAL_D", "PAL_G",
123 "PAL_H", "PAL_I", "PAL_M",
125 "SECAM_B", "SECAM_D", "SECAM_G",
126 "SECAM_H", "SECAM_K", "SECAM_K1",
127 "SECAM_L", "SECAM_L1",
131 #define VDEV_TEXT N_("Video device name")
132 #define VDEV_LONGTEXT N_( \
133 "Name of the video device that will be used by the " \
134 "DirectShow plugin. If you don't specify anything, the default device " \
136 #define ADEV_TEXT N_("Audio device name")
137 #define ADEV_LONGTEXT N_( \
138 "Name of the audio device that will be used by the " \
139 "DirectShow plugin. If you don't specify anything, the default device " \
141 #define SIZE_TEXT N_("Video size")
142 #define SIZE_LONGTEXT N_( \
143 "Size of the video that will be displayed by the " \
144 "DirectShow plugin. If you don't specify anything the default size for " \
145 "your device will be used. You can specify a standard size (cif, d1, ...) or <width>x<height>.")
146 #define ASPECT_TEXT N_("Picture aspect-ratio n:m")
147 #define ASPECT_LONGTEXT N_("Define input picture aspect-ratio to use. Default is 4:3" )
148 #define CHROMA_TEXT N_("Video input chroma format")
149 #define CHROMA_LONGTEXT N_( \
150 "Force the DirectShow video input to use a specific chroma format " \
151 "(eg. I420 (default), RV24, etc.)")
152 #define FPS_TEXT N_("Video input frame rate")
153 #define FPS_LONGTEXT N_( \
154 "Force the DirectShow video input to use a specific frame rate " \
155 "(eg. 0 means default, 25, 29.97, 50, 59.94, etc.)")
156 #define CONFIG_TEXT N_("Device properties")
157 #define CONFIG_LONGTEXT N_( \
158 "Show the properties dialog of the selected device before starting the " \
160 #define TUNER_TEXT N_("Tuner properties")
161 #define TUNER_LONGTEXT N_( \
162 "Show the tuner properties [channel selection] page." )
163 #define CHANNEL_TEXT N_("Tuner TV Channel")
164 #define CHANNEL_LONGTEXT N_( \
165 "Set the TV channel the tuner will set to " \
166 "(0 means default)." )
167 #define TVFREQ_TEXT N_("Tuner Frequency")
168 #define TVFREQ_LONGTEXT N_( "This overrides the channel. Measured in Hz." )
169 #define STANDARD_TEXT N_( "Video standard" )
170 #define COUNTRY_TEXT N_("Tuner country code")
171 #define COUNTRY_LONGTEXT N_( \
172 "Set the tuner country code that establishes the current " \
173 "channel-to-frequency mapping (0 means default)." )
174 #define TUNER_INPUT_TEXT N_("Tuner input type")
175 #define TUNER_INPUT_LONGTEXT N_( \
176 "Select the tuner input type (Cable/Antenna)." )
177 #define VIDEO_IN_TEXT N_("Video input pin")
178 #define VIDEO_IN_LONGTEXT N_( \
179 "Select the video input source, such as composite, s-video, " \
180 "or tuner. Since these settings are hardware-specific, you should find good " \
181 "settings in the \"Device config\" area, and use those numbers here. -1 " \
182 "means that settings will not be changed.")
183 #define AUDIO_IN_TEXT N_("Audio input pin")
184 #define AUDIO_IN_LONGTEXT N_( \
185 "Select the audio input source. See the \"video input\" option." )
186 #define VIDEO_OUT_TEXT N_("Video output pin")
187 #define VIDEO_OUT_LONGTEXT N_( \
188 "Select the video output type. See the \"video input\" option." )
189 #define AUDIO_OUT_TEXT N_("Audio output pin")
190 #define AUDIO_OUT_LONGTEXT N_( \
191 "Select the audio output type. See the \"video input\" option." )
193 #define AMTUNER_MODE_TEXT N_("AM Tuner mode")
194 #define AMTUNER_MODE_LONGTEXT N_( \
195 "AM Tuner mode. Can be one of Default (0), TV (1)," \
196 "AM Radio (2), FM Radio (3) or DSS (4).")
198 #define AUDIO_CHANNELS_TEXT N_("Number of audio channels")
199 #define AUDIO_CHANNELS_LONGTEXT N_( \
200 "Select audio input format with the given number of audio channels (if non 0)" )
202 #define AUDIO_SAMPLERATE_TEXT N_("Audio sample rate")
203 #define AUDIO_SAMPLERATE_LONGTEXT N_( \
204 "Select audio input format with the given sample rate (if non 0)" )
206 #define AUDIO_BITSPERSAMPLE_TEXT N_("Audio bits per sample")
207 #define AUDIO_BITSPERSAMPLE_LONGTEXT N_( \
208 "Select audio input format with the given bits/sample (if non 0)" )
210 static int CommonOpen ( vlc_object_t *, access_sys_t *, bool );
211 static void CommonClose( vlc_object_t *, access_sys_t * );
213 static int AccessOpen ( vlc_object_t * );
214 static void AccessClose( vlc_object_t * );
216 static int DemuxOpen ( vlc_object_t * );
217 static void DemuxClose ( vlc_object_t * );
220 set_shortname( N_("DirectShow") )
221 set_description( N_("DirectShow input") )
222 set_category( CAT_INPUT )
223 set_subcategory( SUBCAT_INPUT_ACCESS )
225 add_string( CFG_PREFIX "vdev", NULL, VDEV_TEXT, VDEV_LONGTEXT, false)
226 change_string_cb( FindDevices )
227 change_action_add( ConfigDevicesCallback, N_("Configure") )
229 add_string( CFG_PREFIX "adev", NULL, ADEV_TEXT, ADEV_LONGTEXT, false)
230 change_string_cb( FindDevices )
231 change_action_add( ConfigDevicesCallback, N_("Configure") )
233 add_string( CFG_PREFIX "size", NULL, SIZE_TEXT, SIZE_LONGTEXT, false)
236 add_string( CFG_PREFIX "aspect-ratio", "4:3", ASPECT_TEXT, ASPECT_LONGTEXT, false)
239 add_string( CFG_PREFIX "chroma", NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true )
242 add_float( CFG_PREFIX "fps", 0.0f, FPS_TEXT, FPS_LONGTEXT, true )
245 add_bool( CFG_PREFIX "config", false, CONFIG_TEXT, CONFIG_LONGTEXT, true )
247 add_bool( CFG_PREFIX "tuner", false, TUNER_TEXT, TUNER_LONGTEXT, true )
249 add_integer( CFG_PREFIX "tuner-channel", 0, CHANNEL_TEXT, CHANNEL_LONGTEXT,
253 add_integer( CFG_PREFIX "tuner-frequency", 0, TVFREQ_TEXT, TVFREQ_LONGTEXT,
257 add_integer( CFG_PREFIX "tuner-country", 0, COUNTRY_TEXT, COUNTRY_LONGTEXT,
260 add_integer( CFG_PREFIX "tuner-standard", 0, STANDARD_TEXT, STANDARD_TEXT,
262 change_integer_list( i_standards_list, ppsz_standards_list_text )
265 add_integer( CFG_PREFIX "tuner-input", 0, TUNER_INPUT_TEXT,
266 TUNER_INPUT_LONGTEXT, true )
267 change_integer_list( pi_tuner_input, ppsz_tuner_input_text )
270 add_integer( CFG_PREFIX "video-input", -1, VIDEO_IN_TEXT,
271 VIDEO_IN_LONGTEXT, true )
274 add_integer( CFG_PREFIX "video-output", -1, VIDEO_OUT_TEXT,
275 VIDEO_OUT_LONGTEXT, true )
277 add_integer( CFG_PREFIX "audio-input", -1, AUDIO_IN_TEXT,
278 AUDIO_IN_LONGTEXT, true )
281 add_integer( CFG_PREFIX "audio-output", -1, AUDIO_OUT_TEXT,
282 AUDIO_OUT_LONGTEXT, true )
284 add_integer( CFG_PREFIX "amtuner-mode", AMTUNER_MODE_TV,
285 AMTUNER_MODE_TEXT, AMTUNER_MODE_LONGTEXT, false)
286 change_integer_list( pi_amtuner_mode, ppsz_amtuner_mode_text )
289 add_integer( CFG_PREFIX "audio-channels", 0, AUDIO_CHANNELS_TEXT,
290 AUDIO_CHANNELS_LONGTEXT, true )
291 add_integer( CFG_PREFIX "audio-samplerate", 0, AUDIO_SAMPLERATE_TEXT,
292 AUDIO_SAMPLERATE_LONGTEXT, true )
293 add_integer( CFG_PREFIX "audio-bitspersample", 0, AUDIO_BITSPERSAMPLE_TEXT,
294 AUDIO_BITSPERSAMPLE_LONGTEXT, true )
296 add_shortcut( "dshow" )
297 set_capability( "access_demux", 0 )
298 set_callbacks( DemuxOpen, DemuxClose )
301 set_description( N_("DirectShow input") )
302 add_shortcut( "dshow" )
303 set_capability( "access", 0 )
304 set_callbacks( AccessOpen, AccessClose )
309 /*****************************************************************************
310 * DirectShow elementary stream descriptor
311 *****************************************************************************/
312 typedef struct dshow_stream_t
315 IBaseFilter *p_device_filter;
316 CaptureFilter *p_capture_filter;
321 VIDEOINFOHEADER video;
331 deque<VLCMediaSample> samples_queue;
334 /*****************************************************************************
335 * DirectShow utility functions
336 *****************************************************************************/
337 static void CreateDirectShowGraph( access_sys_t *p_sys )
339 p_sys->i_crossbar_route_depth = 0;
341 /* Create directshow filter graph */
342 if( SUCCEEDED( CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
343 (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph) ) )
345 /* Create directshow capture graph builder if available */
346 if( SUCCEEDED( CoCreateInstance( CLSID_CaptureGraphBuilder2, 0,
347 CLSCTX_INPROC, (REFIID)IID_ICaptureGraphBuilder2,
348 (void **)&p_sys->p_capture_graph_builder2 ) ) )
350 p_sys->p_capture_graph_builder2->
351 SetFiltergraph((IGraphBuilder *)p_sys->p_graph);
354 p_sys->p_graph->QueryInterface( IID_IMediaControl,
355 (void **)&p_sys->p_control );
359 static void DeleteDirectShowGraph( vlc_object_t *p_this, access_sys_t *p_sys )
361 DeleteCrossbarRoutes( p_sys );
363 /* Remove filters from graph */
364 msg_Dbg( p_this, "DeleteDirectShowGraph: Removing filters" );
365 for( int i = 0; i < p_sys->i_streams; i++ )
367 /* RemoveFilter does an undocumented Release()
368 * but does not set item to NULL */
369 msg_Dbg( p_this, "DeleteDirectShowGraph: Removing capture filter" );
370 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_capture_filter );
371 p_sys->pp_streams[i]->p_capture_filter = NULL;
373 msg_Dbg( p_this, "DeleteDirectShowGraph: Removing device filter" );
374 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_device_filter );
375 p_sys->pp_streams[i]->p_device_filter = NULL;
378 /* Release directshow objects */
379 if( p_sys->p_control )
381 p_sys->p_control->Release();
382 p_sys->p_control = NULL;
384 if( p_sys->p_capture_graph_builder2 )
386 p_sys->p_capture_graph_builder2->Release();
387 p_sys->p_capture_graph_builder2 = NULL;
392 p_sys->p_graph->Release();
393 p_sys->p_graph = NULL;
397 /*****************************************************************************
398 * CommonOpen: open direct show device
399 *****************************************************************************/
400 static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
401 bool b_access_demux )
405 /* Get/parse options and open device(s) */
406 string vdevname, adevname;
407 int i_width = 0, i_height = 0;
408 vlc_fourcc_t i_chroma = 0;
409 bool b_use_audio = true;
410 bool b_use_video = true;
412 /* Initialize OLE/COM */
413 if( FAILED(CoInitializeEx( NULL, COINIT_APARTMENTTHREADED )) )
414 vlc_assert_unreachable();
416 var_Create( p_this, CFG_PREFIX "config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
417 var_Create( p_this, CFG_PREFIX "tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
418 psz_val = var_CreateGetString( p_this, CFG_PREFIX "vdev" );
421 msg_Dbg( p_this, "dshow-vdev: %s", psz_val ) ;
422 /* skip none device */
423 if ( strncasecmp( psz_val, "none", 4 ) != 0 )
424 vdevname = string( psz_val );
426 b_use_video = false ;
430 psz_val = var_CreateGetString( p_this, CFG_PREFIX "adev" );
433 msg_Dbg( p_this, "dshow-adev: %s", psz_val ) ;
434 /* skip none device */
435 if ( strncasecmp( psz_val, "none", 4 ) != 0 )
436 adevname = string( psz_val );
438 b_use_audio = false ;
444 const char *psz_size;
448 { { "subqcif", 128, 96 },
449 { "qsif", 160, 120 },
450 { "qcif", 176, 144 },
457 psz_val = var_CreateGetString( p_this, CFG_PREFIX "size" );
458 if( !EMPTY_STR(psz_val) )
461 for( i = 0; size_table[i].psz_size; i++ )
463 if( !strcmp( psz_val, size_table[i].psz_size ) )
465 i_width = size_table[i].i_width;
466 i_height = size_table[i].i_height;
470 if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
473 i_width = strtol( psz_val, &psz_parser, 0 );
474 if( *psz_parser == 'x' || *psz_parser == 'X')
476 i_height = strtol( psz_parser + 1, &psz_parser, 0 );
478 msg_Dbg( p_this, "width x height %dx%d", i_width, i_height );
484 psz_val = var_CreateGetString( p_this, CFG_PREFIX "chroma" );
485 i_chroma = vlc_fourcc_GetCodecFromString( UNKNOWN_ES, psz_val );
486 p_sys->b_chroma = i_chroma != 0;
489 var_Create( p_this, CFG_PREFIX "fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
490 var_Create( p_this, CFG_PREFIX "tuner-channel",
491 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
492 var_Create( p_this, CFG_PREFIX "tuner-frequency",
493 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
494 var_Create( p_this, CFG_PREFIX "tuner-standard",
495 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
496 var_Create( p_this, CFG_PREFIX "tuner-country",
497 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
498 var_Create( p_this, CFG_PREFIX "tuner-input",
499 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
501 var_Create( p_this, CFG_PREFIX "amtuner-mode",
502 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
504 var_Create( p_this, CFG_PREFIX "video-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
505 var_Create( p_this, CFG_PREFIX "audio-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
506 var_Create( p_this, CFG_PREFIX "video-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
507 var_Create( p_this, CFG_PREFIX "audio-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
510 /* Initialize some data */
511 p_sys->i_streams = 0;
512 p_sys->pp_streams = NULL;
513 p_sys->i_width = i_width;
514 p_sys->i_height = i_height;
515 p_sys->i_chroma = i_chroma;
517 p_sys->p_graph = NULL;
518 p_sys->p_capture_graph_builder2 = NULL;
519 p_sys->p_control = NULL;
521 /* Build directshow graph */
522 CreateDirectShowGraph( p_sys );
524 vlc_mutex_init( &p_sys->lock );
525 vlc_cond_init( &p_sys->wait );
527 if( !b_use_video && !b_use_audio )
529 dialog_Fatal( p_this, _("Capture failed"),
530 _("No video or audio device selected.") );
531 return VLC_EGENERIC ;
535 msg_Dbg( p_this, "skipping video device" ) ;
536 bool b_err_video = false ;
538 if( b_use_video && OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
540 msg_Err( p_this, "can't open video device");
544 if ( b_use_video && !b_err_video )
546 /* Check if we can handle the demuxing ourselves or need to spawn
547 * a demuxer module */
548 dshow_stream_t *p_stream = p_sys->pp_streams[p_sys->i_streams-1];
550 if( p_stream->mt.majortype == MEDIATYPE_Video )
552 if( /* Raw DV stream */
553 p_stream->i_fourcc == VLC_CODEC_DV ||
554 /* Raw MPEG video stream */
555 p_stream->i_fourcc == VLC_CODEC_MPGV )
561 /* Let the access (only) take care of that */
567 if( p_stream->mt.majortype == MEDIATYPE_Stream )
573 /* Let the access (only) take care of that */
577 if( var_GetBool( p_this, CFG_PREFIX "tuner" ) )
579 /* FIXME: we do MEDIATYPE_Stream here so we don't do
581 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
582 p_stream->p_device_filter, 0 );
588 msg_Dbg( p_this, "skipping audio device") ;
590 bool b_err_audio = false ;
592 if( b_use_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
594 msg_Err( p_this, "can't open audio device");
598 if( ( b_use_video && b_err_video && b_use_audio && b_err_audio ) ||
599 ( !b_use_video && b_use_audio && b_err_audio ) ||
600 ( b_use_video && !b_use_audio && b_err_video ) )
602 msg_Err( p_this, "FATAL: could not open ANY device" ) ;
603 dialog_Fatal( p_this, _("Capture failed"),
604 _("VLC cannot open ANY capture device. "
605 "Check the error log for details.") );
606 return VLC_EGENERIC ;
609 for( int i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
611 int i_val = var_GetInteger( p_this, CFG_PREFIX "video-input" );
613 p_sys->crossbar_routes[i].VideoInputIndex = i_val;
614 i_val = var_GetInteger( p_this, CFG_PREFIX "video-output" );
616 p_sys->crossbar_routes[i].VideoOutputIndex = i_val;
617 i_val = var_GetInteger( p_this, CFG_PREFIX "audio-input" );
619 p_sys->crossbar_routes[i].AudioInputIndex = i_val;
620 i_val = var_GetInteger( p_this, CFG_PREFIX "audio-output" );
622 p_sys->crossbar_routes[i].AudioOutputIndex = i_val;
624 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
625 LONG VideoInputIndex = p_sys->crossbar_routes[i].VideoInputIndex;
626 LONG VideoOutputIndex = p_sys->crossbar_routes[i].VideoOutputIndex;
627 LONG AudioInputIndex = p_sys->crossbar_routes[i].AudioInputIndex;
628 LONG AudioOutputIndex = p_sys->crossbar_routes[i].AudioOutputIndex;
630 if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
632 msg_Dbg( p_this, "crossbar at depth %d, routed video "
633 "output %ld to video input %ld", i, VideoOutputIndex,
636 if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
638 if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
641 msg_Dbg(p_this, "crossbar at depth %d, routed audio "
642 "output %ld to audio input %ld", i,
643 AudioOutputIndex, AudioInputIndex );
648 msg_Err( p_this, "crossbar at depth %d could not route video "
649 "output %ld to input %ld", i, VideoOutputIndex, VideoInputIndex );
653 ** Show properties pages from other filters in graph
655 if( var_GetBool( p_this, CFG_PREFIX "config" ) )
657 for( int i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
659 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
662 if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
665 ShowPropertyPage( p_XF );
671 /* Initialize some data */
672 p_sys->i_current_stream = 0;
674 if( !p_sys->i_streams ) return VLC_EGENERIC;
679 /*****************************************************************************
680 * DemuxOpen: open direct show device as an access_demux module
681 *****************************************************************************/
682 static int DemuxOpen( vlc_object_t *p_this )
684 demux_t *p_demux = (demux_t *)p_this;
687 p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
690 p_demux->p_sys = (demux_sys_t *)p_sys;
692 if( CommonOpen( p_this, p_sys, true ) != VLC_SUCCESS )
694 CommonClose( p_this, p_sys );
698 /* Everything is ready. Let's rock baby */
699 msg_Dbg( p_this, "Playing...");
700 p_sys->p_control->Run();
702 p_demux->pf_demux = Demux;
703 p_demux->pf_control = DemuxControl;
704 p_demux->info.i_title = 0;
705 p_demux->info.i_seekpoint = 0;
707 for( int i = 0; i < p_sys->i_streams; i++ )
709 dshow_stream_t *p_stream = p_sys->pp_streams[i];
712 if( p_stream->mt.majortype == MEDIATYPE_Video )
714 char *psz_aspect = var_CreateGetString( p_this, CFG_PREFIX "aspect-ratio" );
715 char *psz_delim = !EMPTY_STR( psz_aspect ) ? strchr( psz_aspect, ':' ) : NULL;
717 es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
719 fmt.video.i_width = p_stream->header.video.bmiHeader.biWidth;
720 fmt.video.i_height = p_stream->header.video.bmiHeader.biHeight;
724 fmt.video.i_sar_num = atoi( psz_aspect ) * fmt.video.i_height;
725 fmt.video.i_sar_den = atoi( psz_delim + 1 ) * fmt.video.i_width;
729 fmt.video.i_sar_num = 4 * fmt.video.i_height;
730 fmt.video.i_sar_den = 3 * fmt.video.i_width;
734 if( !p_stream->header.video.bmiHeader.biCompression )
736 /* RGB DIB are coded from bottom to top */
737 fmt.video.orientation = ORIENT_BOTTOM_LEFT;
740 /* Setup rgb mask for RGB formats */
741 if( p_stream->i_fourcc == VLC_CODEC_RGB24 )
743 /* This is in RGB format
744 http://msdn.microsoft.com/en-us/library/dd407253%28VS.85%29.aspx?ppud=4
746 fmt.video.i_rmask = 0x00ff0000;
747 fmt.video.i_gmask = 0x0000ff00;
748 fmt.video.i_bmask = 0x000000ff;
751 if( p_stream->header.video.AvgTimePerFrame )
753 fmt.video.i_frame_rate = 10000000;
754 fmt.video.i_frame_rate_base =
755 p_stream->header.video.AvgTimePerFrame;
758 else if( p_stream->mt.majortype == MEDIATYPE_Audio )
760 es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
762 fmt.audio.i_channels = p_stream->header.audio.nChannels;
763 fmt.audio.i_rate = p_stream->header.audio.nSamplesPerSec;
764 fmt.audio.i_bitspersample = p_stream->header.audio.wBitsPerSample;
765 fmt.audio.i_blockalign = fmt.audio.i_channels *
766 fmt.audio.i_bitspersample / 8;
767 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
768 fmt.audio.i_bitspersample;
771 p_stream->p_es = es_out_Add( p_demux->out, &fmt );
777 /*****************************************************************************
778 * AccessOpen: open direct show device as an access module
779 *****************************************************************************/
780 static int AccessOpen( vlc_object_t *p_this )
782 access_t *p_access = (access_t*)p_this;
785 p_access->p_sys = p_sys = (access_sys_t*)calloc( 1, sizeof( access_sys_t ) );
789 if( CommonOpen( p_this, p_sys, false ) != VLC_SUCCESS )
791 CommonClose( p_this, p_sys );
795 dshow_stream_t *p_stream = p_sys->pp_streams[0];
797 /* Check if we need to force demuxers */
798 if( p_stream->i_fourcc == VLC_CODEC_DV )
800 free( p_access->psz_demux );
801 p_access->psz_demux = strdup( "rawdv" );
803 else if( p_stream->i_fourcc == VLC_CODEC_MPGV )
805 free( p_access->psz_demux );
806 p_access->psz_demux = strdup( "mpgv" );
810 p_access->pf_read = NULL;
811 p_access->pf_block = ReadCompressed;
812 p_access->pf_control = AccessControl;
813 p_access->pf_seek = NULL;
814 p_access->info.i_pos = 0;
815 p_access->info.b_eof = false;
816 p_access->p_sys = p_sys;
818 /* Everything is ready. Let's rock baby */
819 msg_Dbg( p_this, "Playing...");
820 p_sys->p_control->Run();
825 /*****************************************************************************
826 * CommonClose: close device
827 *****************************************************************************/
828 static void CommonClose( vlc_object_t *p_this, access_sys_t *p_sys )
830 msg_Dbg( p_this, "releasing DirectShow");
832 DeleteDirectShowGraph( p_this, p_sys );
834 /* Uninitialize OLE/COM */
837 for( int i = 0; i < p_sys->i_streams; i++ ) delete p_sys->pp_streams[i];
838 if( p_sys->i_streams ) free( p_sys->pp_streams );
840 vlc_mutex_destroy( &p_sys->lock );
841 vlc_cond_destroy( &p_sys->wait );
846 /*****************************************************************************
847 * AccessClose: close device
848 *****************************************************************************/
849 static void AccessClose( vlc_object_t *p_this )
851 access_t *p_access = (access_t *)p_this;
852 access_sys_t *p_sys = p_access->p_sys;
854 /* Stop capturing stuff */
855 p_sys->p_control->Stop();
857 CommonClose( p_this, p_sys );
860 /*****************************************************************************
861 * DemuxClose: close device
862 *****************************************************************************/
863 static void DemuxClose( vlc_object_t *p_this )
865 demux_t *p_demux = (demux_t *)p_this;
866 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
868 /* Stop capturing stuff */
869 p_sys->p_control->Stop();
871 CommonClose( p_this, p_sys );
874 /****************************************************************************
876 ****************************************************************************/
877 static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
878 IBaseFilter *p_filter,
879 CaptureFilter *p_capture_filter )
881 CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
883 AM_MEDIA_TYPE mediaType = p_input_pin->CustomGetMediaType();
885 if( p_sys->p_capture_graph_builder2 )
887 if( FAILED(p_sys->p_capture_graph_builder2->
888 RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
889 p_filter, 0, (IBaseFilter *)p_capture_filter )) )
894 // Sort out all the possible video inputs
895 // The class needs to be given the capture filters ANALOGVIDEO input pin
896 IEnumPins *pins = NULL;
897 if( ( mediaType.majortype == MEDIATYPE_Video ||
898 mediaType.majortype == MEDIATYPE_Stream ) &&
899 SUCCEEDED(p_filter->EnumPins(&pins)) )
905 IKsPropertySet *pKs = NULL;
909 while( !Found && ( S_OK == pins->Next(1, &pP, &n) ) )
911 if( S_OK == pP->QueryPinInfo(&pinInfo) )
913 // is this pin an ANALOGVIDEOIN input pin?
914 if( pinInfo.dir == PINDIR_INPUT &&
915 pP->QueryInterface( IID_IKsPropertySet,
916 (void **)&pKs ) == S_OK )
918 if( pKs->Get( AMPROPSETID_Pin,
919 AMPROPERTY_PIN_CATEGORY, NULL, 0,
920 &guid, sizeof(GUID), &dw ) == S_OK )
922 if( guid == PIN_CATEGORY_ANALOGVIDEOIN )
924 // recursively search crossbar routes
925 FindCrossbarRoutes( p_this, p_sys, pP, 0 );
932 pinInfo.pFilter->Release();
937 msg_Dbg( p_this, "ConnectFilters: graph_builder2 available.") ;
939 msg_Warn( p_this, "ConnectFilters: No crossBar routes found (incompatible pin types)" ) ;
945 IEnumPins *p_enumpins;
948 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;
950 while( S_OK == p_enumpins->Next( 1, &p_pin, NULL ) )
952 PIN_DIRECTION pin_dir;
953 p_pin->QueryDirection( &pin_dir );
955 if( pin_dir == PINDIR_OUTPUT &&
956 p_sys->p_graph->ConnectDirect( p_pin, (IPin *)p_input_pin,
960 p_enumpins->Release();
966 p_enumpins->Release();
972 * get fourcc priority from arbritary preference, the higher the better
974 static int GetFourCCPriority( int i_fourcc )
982 case VLC_FOURCC('a','r','a','w'):
984 case VLC_CODEC_RGB24:
987 case VLC_CODEC_RGB32:
995 #define MAX_MEDIA_TYPES 32
997 static int OpenDevice( vlc_object_t *p_this, access_sys_t *p_sys,
998 string devicename, bool b_audio )
1000 /* See if device is already opened */
1001 for( int i = 0; i < p_sys->i_streams; i++ )
1003 if( devicename.size() &&
1004 p_sys->pp_streams[i]->devicename == devicename )
1006 /* Already opened */
1011 list<string> list_devices;
1013 /* Enumerate devices and display their names */
1014 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1015 if( list_devices.empty() )
1016 return VLC_EGENERIC;
1018 list<string>::iterator iter;
1019 for( iter = list_devices.begin(); iter != list_devices.end(); ++iter )
1020 msg_Dbg( p_this, "found device: %s", iter->c_str() );
1022 /* If no device name was specified, pick the 1st one */
1023 if( devicename.size() == 0 )
1025 /* When none selected */
1026 devicename = *list_devices.begin();
1027 msg_Dbg( p_this, "asking for default device: %s", devicename.c_str() ) ;
1030 msg_Dbg( p_this, "asking for device: %s", devicename.c_str() ) ;
1031 // Use the system device enumerator and class enumerator to find
1032 // a capture/preview device, such as a desktop USB video camera.
1033 IBaseFilter *p_device_filter =
1034 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
1036 if( p_device_filter )
1037 msg_Dbg( p_this, "using device: %s", devicename.c_str() );
1040 msg_Err( p_this, "can't use device: %s, unsupported device type",
1041 devicename.c_str() );
1042 dialog_Fatal( p_this, _("Capture failed"),
1043 _("The device you selected cannot be used, because its "
1044 "type is not supported.") );
1045 return VLC_EGENERIC;
1048 // Retreive acceptable media types supported by device
1049 AM_MEDIA_TYPE media_types[MAX_MEDIA_TYPES];
1050 size_t media_count =
1051 EnumDeviceCaps( p_this, p_device_filter, b_audio ? 0 : p_sys->i_chroma,
1052 p_sys->i_width, p_sys->i_height,
1053 b_audio ? var_CreateGetInteger( p_this, CFG_PREFIX "audio-channels" ) : 0,
1054 b_audio ? var_CreateGetInteger( p_this, CFG_PREFIX "audio-samplerate" ) : 0,
1055 b_audio ? var_CreateGetInteger( p_this, CFG_PREFIX "audio-bitspersample" ) : 0,
1056 media_types, MAX_MEDIA_TYPES );
1058 AM_MEDIA_TYPE *mt = NULL;
1060 if( media_count > 0 )
1062 mt = (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * media_count);
1064 // Order and copy returned media types according to arbitrary
1066 for( size_t c = 0; c < media_count; c++ )
1069 GetFourCCPriority(GetFourCCFromMediaType(media_types[c]));
1070 size_t slot_copy = c;
1071 for( size_t d = c+1; d < media_count; d++ )
1074 GetFourCCPriority(GetFourCCFromMediaType(media_types[d]));
1075 if( priority > slot_priority )
1077 slot_priority = priority;
1081 if( slot_copy != c )
1083 mt[c] = media_types[slot_copy];
1084 media_types[slot_copy] = media_types[c];
1088 mt[c] = media_types[c];
1093 /* capture device */
1094 msg_Err( p_this, "capture device '%s' does not support required parameters !", devicename.c_str() );
1095 dialog_Fatal( p_this, _("Capture failed"),
1096 _("The capture device \"%s\" does not support the "
1097 "required parameters."), devicename.c_str() );
1098 p_device_filter->Release();
1099 return VLC_EGENERIC;
1102 /* Create and add our capture filter */
1103 CaptureFilter *p_capture_filter =
1104 new CaptureFilter( p_this, p_sys, mt, media_count );
1105 p_sys->p_graph->AddFilter( p_capture_filter, 0 );
1107 /* Add the device filter to the graph (seems necessary with VfW before
1108 * accessing pin attributes). */
1109 p_sys->p_graph->AddFilter( p_device_filter, 0 );
1111 /* Attempt to connect one of this device's capture output pins */
1112 msg_Dbg( p_this, "connecting filters" );
1113 if( ConnectFilters( p_this, p_sys, p_device_filter, p_capture_filter ) )
1116 msg_Dbg( p_this, "filters connected successfully !" );
1118 dshow_stream_t dshow_stream;
1119 dshow_stream.b_pts = false;
1120 dshow_stream.p_es = 0;
1122 p_capture_filter->CustomGetPin()->CustomGetMediaType();
1124 /* Show Device properties. Done here so the VLC stream is setup with
1125 * the proper parameters. */
1126 if( var_GetBool( p_this, CFG_PREFIX "config" ) )
1128 ShowDeviceProperties( p_this, p_sys->p_capture_graph_builder2,
1129 p_device_filter, b_audio );
1132 ConfigTuner( p_this, p_sys->p_capture_graph_builder2,
1135 if( var_GetBool( p_this, CFG_PREFIX "tuner" ) &&
1136 dshow_stream.mt.majortype != MEDIATYPE_Stream )
1138 /* FIXME: we do MEDIATYPE_Stream later so we don't do it twice. */
1139 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
1140 p_device_filter, b_audio );
1144 p_capture_filter->CustomGetPin()->CustomGetMediaType();
1146 dshow_stream.i_fourcc = GetFourCCFromMediaType( dshow_stream.mt );
1147 if( dshow_stream.i_fourcc )
1149 if( dshow_stream.mt.majortype == MEDIATYPE_Video )
1151 dshow_stream.header.video =
1152 *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
1153 msg_Dbg( p_this, "MEDIATYPE_Video" );
1154 msg_Dbg( p_this, "selected video pin accepts format: %4.4s",
1155 (char *)&dshow_stream.i_fourcc);
1157 else if( dshow_stream.mt.majortype == MEDIATYPE_Audio )
1159 dshow_stream.header.audio =
1160 *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
1161 msg_Dbg( p_this, "MEDIATYPE_Audio" );
1162 msg_Dbg( p_this, "selected audio pin accepts format: %4.4s",
1163 (char *)&dshow_stream.i_fourcc);
1165 else if( dshow_stream.mt.majortype == MEDIATYPE_Stream )
1167 msg_Dbg( p_this, "MEDIATYPE_Stream" );
1168 msg_Dbg( p_this, "selected stream pin accepts format: %4.4s",
1169 (char *)&dshow_stream.i_fourcc);
1173 msg_Dbg( p_this, "unknown stream majortype" );
1177 /* Add directshow elementary stream to our list */
1178 dshow_stream.p_device_filter = p_device_filter;
1179 dshow_stream.p_capture_filter = p_capture_filter;
1181 p_sys->pp_streams = (dshow_stream_t **)xrealloc( p_sys->pp_streams,
1182 sizeof(dshow_stream_t *) * (p_sys->i_streams + 1) );
1183 p_sys->pp_streams[p_sys->i_streams] = new dshow_stream_t;
1184 *p_sys->pp_streams[p_sys->i_streams++] = dshow_stream;
1191 /* Remove filters from graph */
1192 msg_Dbg( p_this, "OpenDevice: Removing filters" ) ;
1193 p_sys->p_graph->RemoveFilter( p_device_filter );
1194 p_sys->p_graph->RemoveFilter( p_capture_filter );
1196 /* Release objects */
1197 /* RemoveFilter does an undocumented Release()
1198 * but does not set item to NULL*/
1200 p_device_filter = NULL;
1201 p_capture_filter = NULL;
1203 return VLC_EGENERIC;
1206 /* FindCaptureDevices:: This Function had two purposes :
1207 Returns the list of capture devices when p_listdevices != NULL
1208 Creates an IBaseFilter when p_devicename corresponds to an existing devname
1209 These actions *may* be requested whith a single call.
1211 static IBaseFilter *
1212 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
1213 list<string> *p_listdevices, bool b_audio )
1215 IBaseFilter *p_base_filter = NULL;
1216 IMoniker *p_moniker = NULL;
1219 list<string> devicelist;
1221 /* Create the system device enumerator */
1222 ICreateDevEnum *p_dev_enum = NULL;
1224 hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
1225 IID_ICreateDevEnum, (void **)&p_dev_enum );
1228 msg_Err( p_this, "failed to create the device enumerator (0x%lx)", hr);
1232 /* Create an enumerator for the video capture devices */
1233 IEnumMoniker *p_class_enum = NULL;
1235 hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
1238 hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
1240 p_dev_enum->Release();
1243 msg_Err( p_this, "failed to create the class enumerator (0x%lx)", hr );
1247 /* If there are no enumerators for the requested type, then
1248 * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
1249 if( p_class_enum == NULL )
1251 msg_Err( p_this, "no %s capture device was detected", ( b_audio ? "audio" : "video" ) );
1255 /* Enumerate the devices */
1257 /* Note that if the Next() call succeeds but there are no monikers,
1258 * it will return S_FALSE (which is not a failure). Therefore, we check
1259 * that the return code is S_OK instead of using SUCCEEDED() macro. */
1261 while( p_class_enum->Next( 1, &p_moniker, &i_fetched ) == S_OK )
1263 /* Getting the property page to get the device name */
1264 IPropertyBag *p_bag;
1265 hr = p_moniker->BindToStorage( 0, 0, IID_IPropertyBag,
1271 hr = p_bag->Read( L"FriendlyName", &var, NULL );
1275 char *p_buf = FromWide( var.bstrVal );
1276 string devname = string(p_buf);
1280 /* find out if this name is already used by a previously found device */
1281 list<string>::const_iterator iter = devicelist.begin();
1282 list<string>::const_iterator end = devicelist.end();
1283 string ordevname = devname ;
1284 while ( iter != end )
1286 if( 0 == (*iter).compare( devname ) )
1287 { /* devname is on the list. Try another name with sequence
1288 number apended and then rescan until a unique entry is found*/
1290 snprintf(seq, 16, " #%d", ++dup);
1291 devname = ordevname + seq;
1292 iter = devicelist.begin();
1297 devicelist.push_back( devname );
1299 if( p_devicename && *p_devicename == devname )
1301 msg_Dbg( p_this, "asked for %s, binding to %s", p_devicename->c_str() , devname.c_str() ) ;
1302 /* NULL possibly means we don't need BindMoniker BindCtx ?? */
1303 hr = p_moniker->BindToObject( NULL, 0, IID_IBaseFilter,
1304 (void **)&p_base_filter );
1307 msg_Err( p_this, "couldn't bind moniker to filter "
1308 "object (0x%lx)", hr );
1309 p_moniker->Release();
1310 p_class_enum->Release();
1313 p_moniker->Release();
1314 p_class_enum->Release();
1315 return p_base_filter;
1320 p_moniker->Release();
1323 p_class_enum->Release();
1325 if( p_listdevices ) {
1327 *p_listdevices = devicelist;
1332 static size_t EnumDeviceCaps( vlc_object_t *p_this, IBaseFilter *p_filter,
1333 int i_fourcc, int i_width, int i_height,
1334 int i_channels, int i_samplespersec,
1335 int i_bitspersample, AM_MEDIA_TYPE *mt,
1338 IEnumPins *p_enumpins;
1340 IEnumMediaTypes *p_enummt;
1341 size_t mt_count = 0;
1343 LONGLONG i_AvgTimePerFrame = 0;
1344 float r_fps = var_GetFloat( p_this, CFG_PREFIX "fps" );
1346 i_AvgTimePerFrame = 10000000000LL/(LONGLONG)(r_fps*1000.0f);
1348 if( FAILED(p_filter->EnumPins( &p_enumpins )) )
1350 msg_Dbg( p_this, "EnumDeviceCaps failed: no pin enumeration !");
1354 while( S_OK == p_enumpins->Next( 1, &p_output_pin, NULL ) )
1358 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1360 msg_Dbg( p_this, "EnumDeviceCaps: %s pin: %S",
1361 info.dir == PINDIR_INPUT ? "input" : "output",
1363 if( info.pFilter ) info.pFilter->Release();
1366 p_output_pin->Release();
1369 p_enumpins->Reset();
1371 while( !mt_count && p_enumpins->Next( 1, &p_output_pin, NULL ) == S_OK )
1375 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1377 if( info.pFilter ) info.pFilter->Release();
1378 if( info.dir == PINDIR_INPUT )
1380 p_output_pin->Release();
1383 msg_Dbg( p_this, "EnumDeviceCaps: trying pin %S", info.achName );
1386 AM_MEDIA_TYPE *p_mt;
1389 ** Configure pin with a default compatible media if possible
1392 IAMStreamConfig *pSC;
1393 if( SUCCEEDED(p_output_pin->QueryInterface( IID_IAMStreamConfig,
1396 int piCount, piSize;
1397 if( SUCCEEDED(pSC->GetNumberOfCapabilities(&piCount, &piSize)) )
1399 BYTE *pSCC= (BYTE *)CoTaskMemAlloc(piSize);
1402 int i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
1403 for( int i=0; i<piCount; ++i )
1405 if( SUCCEEDED(pSC->GetStreamCaps(i, &p_mt, pSCC)) )
1407 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1408 int i_current_priority = GetFourCCPriority(i_current_fourcc);
1410 if( (i_fourcc && (i_current_fourcc != i_fourcc))
1411 || (i_priority > i_current_priority) )
1413 // unwanted chroma, try next media type
1414 FreeMediaType( *p_mt );
1415 CoTaskMemFree( (PVOID)p_mt );
1419 if( MEDIATYPE_Video == p_mt->majortype
1420 && FORMAT_VideoInfo == p_mt->formattype )
1422 VIDEO_STREAM_CONFIG_CAPS *pVSCC = reinterpret_cast<VIDEO_STREAM_CONFIG_CAPS*>(pSCC);
1423 VIDEOINFOHEADER *pVih = reinterpret_cast<VIDEOINFOHEADER*>(p_mt->pbFormat);
1425 if( i_AvgTimePerFrame )
1427 if( pVSCC->MinFrameInterval > i_AvgTimePerFrame
1428 || i_AvgTimePerFrame > pVSCC->MaxFrameInterval )
1430 // required frame rate not compatible, try next media type
1431 FreeMediaType( *p_mt );
1432 CoTaskMemFree( (PVOID)p_mt );
1435 pVih->AvgTimePerFrame = i_AvgTimePerFrame;
1440 if(( !pVSCC->OutputGranularityX
1441 && i_width != pVSCC->MinOutputSize.cx
1442 && i_width != pVSCC->MaxOutputSize.cx)
1444 ( pVSCC->OutputGranularityX
1445 && ((i_width % pVSCC->OutputGranularityX)
1446 || pVSCC->MinOutputSize.cx > i_width
1447 || i_width > pVSCC->MaxOutputSize.cx )))
1449 // required width not compatible, try next media type
1450 FreeMediaType( *p_mt );
1451 CoTaskMemFree( (PVOID)p_mt );
1454 pVih->bmiHeader.biWidth = i_width;
1459 if(( !pVSCC->OutputGranularityY
1460 && i_height != pVSCC->MinOutputSize.cy
1461 && i_height != pVSCC->MaxOutputSize.cy)
1463 ( pVSCC->OutputGranularityY
1464 && ((i_height % pVSCC->OutputGranularityY)
1465 || pVSCC->MinOutputSize.cy > i_height
1466 || i_height > pVSCC->MaxOutputSize.cy )))
1468 // required height not compatible, try next media type
1469 FreeMediaType( *p_mt );
1470 CoTaskMemFree( (PVOID)p_mt );
1473 pVih->bmiHeader.biHeight = i_height;
1476 // Set the sample size and image size.
1477 // (Round the image width up to a DWORD boundary.)
1478 p_mt->lSampleSize = pVih->bmiHeader.biSizeImage =
1479 ((pVih->bmiHeader.biWidth + 3) & ~3) *
1480 pVih->bmiHeader.biHeight * (pVih->bmiHeader.biBitCount>>3);
1482 // no cropping, use full video input buffer
1483 memset(&(pVih->rcSource), 0, sizeof(RECT));
1484 memset(&(pVih->rcTarget), 0, sizeof(RECT));
1486 // select this format as default
1487 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1489 i_priority = i_current_priority;
1491 // no need to check any more media types
1495 else if( p_mt->majortype == MEDIATYPE_Audio
1496 && p_mt->formattype == FORMAT_WaveFormatEx )
1498 AUDIO_STREAM_CONFIG_CAPS *pASCC = reinterpret_cast<AUDIO_STREAM_CONFIG_CAPS*>(pSCC);
1499 WAVEFORMATEX *pWfx = reinterpret_cast<WAVEFORMATEX*>(p_mt->pbFormat);
1501 if( i_current_fourcc && (WAVE_FORMAT_PCM == pWfx->wFormatTag) )
1503 int val = i_channels;
1507 if( ( !pASCC->ChannelsGranularity
1508 && (unsigned int)val != pASCC->MinimumChannels
1509 && (unsigned int)val != pASCC->MaximumChannels)
1511 ( pASCC->ChannelsGranularity
1512 && ((val % pASCC->ChannelsGranularity)
1513 || (unsigned int)val < pASCC->MinimumChannels
1514 || (unsigned int)val > pASCC->MaximumChannels)))
1516 // required number channels not available, try next media type
1517 FreeMediaType( *p_mt );
1518 CoTaskMemFree( (PVOID)p_mt );
1521 pWfx->nChannels = val;
1523 val = i_samplespersec;
1527 if( ( !pASCC->SampleFrequencyGranularity
1528 && (unsigned int)val != pASCC->MinimumSampleFrequency
1529 && (unsigned int)val != pASCC->MaximumSampleFrequency)
1531 ( pASCC->SampleFrequencyGranularity
1532 && ((val % pASCC->SampleFrequencyGranularity)
1533 || (unsigned int)val < pASCC->MinimumSampleFrequency
1534 || (unsigned int)val > pASCC->MaximumSampleFrequency )))
1536 // required sampling rate not available, try next media type
1537 FreeMediaType( *p_mt );
1538 CoTaskMemFree( (PVOID)p_mt );
1541 pWfx->nSamplesPerSec = val;
1543 val = i_bitspersample;
1546 if( VLC_CODEC_FL32 == i_current_fourcc )
1552 if( ( !pASCC->BitsPerSampleGranularity
1553 && (unsigned int)val != pASCC->MinimumBitsPerSample
1554 && (unsigned int)val != pASCC->MaximumBitsPerSample )
1556 ( pASCC->BitsPerSampleGranularity
1557 && ((val % pASCC->BitsPerSampleGranularity)
1558 || (unsigned int)val < pASCC->MinimumBitsPerSample
1559 || (unsigned int)val > pASCC->MaximumBitsPerSample )))
1561 // required sample size not available, try next media type
1562 FreeMediaType( *p_mt );
1563 CoTaskMemFree( (PVOID)p_mt );
1567 pWfx->wBitsPerSample = val;
1568 pWfx->nBlockAlign = (pWfx->wBitsPerSample * pWfx->nChannels)/8;
1569 pWfx->nAvgBytesPerSec = pWfx->nSamplesPerSec * pWfx->nBlockAlign;
1571 // select this format as default
1572 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1574 i_priority = i_current_priority;
1578 FreeMediaType( *p_mt );
1579 CoTaskMemFree( (PVOID)p_mt );
1582 CoTaskMemFree( (LPVOID)pSCC );
1583 if( i_priority >= ES_PRIORITY_SELECTABLE_MIN )
1584 msg_Dbg( p_this, "EnumDeviceCaps: input pin default format configured");
1591 ** Probe pin for available medias (may be a previously configured one)
1594 if( FAILED( p_output_pin->EnumMediaTypes( &p_enummt ) ) )
1596 p_output_pin->Release();
1600 while( p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1602 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1603 if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Video
1604 && p_mt->formattype == FORMAT_VideoInfo )
1606 int i_current_width = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1607 int i_current_height = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1608 LONGLONG i_current_atpf = ((VIDEOINFOHEADER *)p_mt->pbFormat)->AvgTimePerFrame;
1610 if( i_current_height < 0 )
1611 i_current_height = -i_current_height;
1613 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1614 "accepts chroma: %4.4s, width:%i, height:%i, fps:%f",
1615 (char *)&i_current_fourcc, i_current_width,
1616 i_current_height, (10000000.0f/((float)i_current_atpf)) );
1618 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1619 ( !i_width || i_width == i_current_width ) &&
1620 ( !i_height || i_height == i_current_height ) &&
1621 ( !i_AvgTimePerFrame || i_AvgTimePerFrame == i_current_atpf ) &&
1625 mt[mt_count++] = *p_mt;
1627 else FreeMediaType( *p_mt );
1629 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Audio
1630 && p_mt->formattype == FORMAT_WaveFormatEx)
1632 int i_current_channels =
1633 ((WAVEFORMATEX *)p_mt->pbFormat)->nChannels;
1634 int i_current_samplespersec =
1635 ((WAVEFORMATEX *)p_mt->pbFormat)->nSamplesPerSec;
1636 int i_current_bitspersample =
1637 ((WAVEFORMATEX *)p_mt->pbFormat)->wBitsPerSample;
1639 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1640 "accepts format: %4.4s, channels:%i, "
1641 "samples/sec:%i bits/sample:%i",
1642 (char *)&i_current_fourcc, i_current_channels,
1643 i_current_samplespersec, i_current_bitspersample);
1645 if( (!i_channels || i_channels == i_current_channels) &&
1646 (!i_samplespersec ||
1647 i_samplespersec == i_current_samplespersec) &&
1648 (!i_bitspersample ||
1649 i_bitspersample == i_current_bitspersample) &&
1653 mt[mt_count++] = *p_mt;
1655 /* Setup a few properties like the audio latency */
1656 IAMBufferNegotiation *p_ambuf;
1657 if( SUCCEEDED( p_output_pin->QueryInterface(
1658 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
1660 ALLOCATOR_PROPERTIES AllocProp;
1661 AllocProp.cbAlign = -1;
1663 /* 100 ms of latency */
1664 AllocProp.cbBuffer = i_current_channels *
1665 i_current_samplespersec *
1666 i_current_bitspersample / 8 / 10;
1668 AllocProp.cbPrefix = -1;
1669 AllocProp.cBuffers = -1;
1670 p_ambuf->SuggestAllocatorProperties( &AllocProp );
1674 else FreeMediaType( *p_mt );
1676 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Stream )
1678 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1679 "accepts stream format: %4.4s",
1680 (char *)&i_current_fourcc );
1682 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1686 mt[mt_count++] = *p_mt;
1687 i_fourcc = i_current_fourcc;
1689 else FreeMediaType( *p_mt );
1693 const char * psz_type = "unknown";
1694 if( p_mt->majortype == MEDIATYPE_Video ) psz_type = "video";
1695 if( p_mt->majortype == MEDIATYPE_Audio ) psz_type = "audio";
1696 if( p_mt->majortype == MEDIATYPE_Stream ) psz_type = "stream";
1697 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: unsupported format "
1698 "(%s %4.4s)", psz_type, (char *)&p_mt->subtype );
1700 FreeMediaType( *p_mt );
1702 CoTaskMemFree( (PVOID)p_mt );
1705 if( !mt_count && p_enummt->Reset() == S_OK )
1707 // VLC did not find any supported MEDIATYPE for this output pin.
1708 // However the graph builder might insert converter filters in
1709 // the graph if we use a different codec in VLC filter input pin.
1710 // however, in order to avoid nasty surprises, make use of this
1711 // facility only for known unsupported codecs.
1713 while( !mt_count && p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1715 // the first four bytes of subtype GUID contains the codec FOURCC
1716 const char *pfcc = (char *)&p_mt->subtype;
1717 int i_current_fourcc = VLC_FOURCC(pfcc[0], pfcc[1], pfcc[2], pfcc[3]);
1718 if( VLC_FOURCC('H','C','W','2') == i_current_fourcc
1719 && p_mt->majortype == MEDIATYPE_Video )
1721 // output format for 'Hauppauge WinTV PVR PCI II Capture'
1722 // try I420 as an input format
1723 i_current_fourcc = VLC_CODEC_I420;
1724 if( !i_fourcc || i_fourcc == i_current_fourcc )
1726 // return alternative media type
1730 mtr.majortype = MEDIATYPE_Video;
1731 mtr.subtype = MEDIASUBTYPE_I420;
1732 mtr.bFixedSizeSamples = TRUE;
1733 mtr.bTemporalCompression = FALSE;
1735 mtr.formattype = FORMAT_VideoInfo;
1736 mtr.cbFormat = sizeof(vh);
1737 mtr.pbFormat = (BYTE *)&vh;
1739 memset(&vh, 0, sizeof(vh));
1741 vh.bmiHeader.biSize = sizeof(vh.bmiHeader);
1742 vh.bmiHeader.biWidth = i_width > 0 ? i_width :
1743 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1744 vh.bmiHeader.biHeight = i_height > 0 ? i_height :
1745 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1746 vh.bmiHeader.biPlanes = 3;
1747 vh.bmiHeader.biBitCount = 12;
1748 vh.bmiHeader.biCompression = VLC_CODEC_I420;
1749 vh.bmiHeader.biSizeImage = vh.bmiHeader.biWidth * 12 *
1750 vh.bmiHeader.biHeight / 8;
1751 mtr.lSampleSize = vh.bmiHeader.biSizeImage;
1753 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: using 'I420' in place of unsupported format 'HCW2'");
1755 if( SUCCEEDED(CopyMediaType(mt+mt_count, &mtr)) )
1759 FreeMediaType( *p_mt );
1763 p_enummt->Release();
1764 p_output_pin->Release();
1767 p_enumpins->Release();
1771 /*****************************************************************************
1772 * ReadCompressed: reads compressed (MPEG/DV) data from the device.
1773 *****************************************************************************
1774 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1776 *****************************************************************************/
1777 static block_t *ReadCompressed( access_t *p_access )
1779 access_sys_t *p_sys = p_access->p_sys;
1780 dshow_stream_t *p_stream = NULL;
1781 VLCMediaSample sample;
1783 /* Read 1 DV/MPEG frame (they contain the video and audio data) */
1785 /* There must be only 1 elementary stream to produce a valid stream
1786 * of MPEG or DV data */
1787 p_stream = p_sys->pp_streams[0];
1791 if( !vlc_object_alive (p_access) ) return NULL;
1793 /* Get new sample/frame from the elementary stream (blocking). */
1794 vlc_mutex_lock( &p_sys->lock );
1796 if( p_stream->p_capture_filter->CustomGetPin()
1797 ->CustomGetSample( &sample ) != S_OK )
1799 /* No data available. Wait until some data has arrived */
1800 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1801 vlc_mutex_unlock( &p_sys->lock );
1805 vlc_mutex_unlock( &p_sys->lock );
1812 int i_data_size = sample.p_sample->GetActualDataLength();
1814 if( !i_data_size || !(p_block = block_Alloc( i_data_size )) )
1816 sample.p_sample->Release();
1820 sample.p_sample->GetPointer( &p_data );
1821 memcpy( p_block->p_buffer, p_data, i_data_size );
1822 sample.p_sample->Release();
1824 /* The caller got what he wanted */
1828 return NULL; /* never reached */
1831 /****************************************************************************
1833 ****************************************************************************/
1834 static int Demux( demux_t *p_demux )
1836 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
1837 int i_found_samples;
1839 i_found_samples = 0;
1840 vlc_mutex_lock( &p_sys->lock );
1842 while ( !i_found_samples )
1844 /* Try to grab samples from all streams */
1845 for( int i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1847 dshow_stream_t *p_stream = p_sys->pp_streams[i_stream];
1848 if( p_stream->p_capture_filter &&
1849 p_stream->p_capture_filter->CustomGetPin()
1850 ->CustomGetSamples( p_stream->samples_queue ) == S_OK )
1852 i_found_samples = 1;
1856 if ( !i_found_samples)
1858 /* Didn't find any audio nor video sample, just wait till the
1859 * dshow thread pushes some samples */
1860 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1861 /* Some DShow thread pushed data, or the OS broke the wait all
1862 * by itself. In all cases, it's *strongly* advised to test the
1863 * condition again, so let the loop do the test again */
1867 vlc_mutex_unlock( &p_sys->lock );
1869 for ( int i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1872 dshow_stream_t *p_stream = p_sys->pp_streams[i_stream];
1874 i_samples = p_stream->samples_queue.size();
1875 while ( i_samples > 0 )
1880 VLCMediaSample sample;
1882 sample = p_stream->samples_queue.front();
1883 p_stream->samples_queue.pop_front();
1885 i_data_size = sample.p_sample->GetActualDataLength();
1886 sample.p_sample->GetPointer( &p_data );
1888 REFERENCE_TIME i_pts, i_end_date;
1889 HRESULT hr = sample.p_sample->GetTime( &i_pts, &i_end_date );
1890 if( hr == S_OK || hr == VFW_S_NO_STOP_TIME )
1892 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
1894 /* Use our data timestamp */
1895 i_pts = sample.i_timestamp;
1896 p_stream->b_pts = true;
1898 i_pts += (i_pts >= 0) ? +5 : -4;
1899 i_pts /= 10; /* 100-ns to µs conversion */
1903 i_pts = VLC_TS_INVALID;
1905 msg_Dbg( p_demux, "Read() stream: %i, size: %i, PTS: %"PRId64,
1906 i_stream, i_data_size, i_pts );
1909 p_block = block_Alloc( i_data_size );
1910 memcpy( p_block->p_buffer, p_data, i_data_size );
1911 p_block->i_pts = p_block->i_dts = i_pts;
1912 sample.p_sample->Release();
1914 if( i_pts > VLC_TS_INVALID )
1915 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pts );
1916 es_out_Send( p_demux->out, p_stream->p_es, p_block );
1925 /*****************************************************************************
1927 *****************************************************************************/
1928 static int AccessControl( access_t *p_access, int i_query, va_list args )
1935 case ACCESS_CAN_SEEK:
1936 case ACCESS_CAN_FASTSEEK:
1937 case ACCESS_CAN_PAUSE:
1938 case ACCESS_CAN_CONTROL_PACE:
1939 pb_bool = (bool*)va_arg( args, bool* );
1943 case ACCESS_GET_PTS_DELAY:
1944 pi_64 = (int64_t*)va_arg( args, int64_t * );
1946 INT64_C(1000) * var_InheritInteger( p_access, "live-caching" );
1950 return VLC_EGENERIC;
1956 /****************************************************************************
1958 ****************************************************************************/
1959 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1966 /* Special for access_demux */
1967 case DEMUX_CAN_PAUSE:
1968 case DEMUX_CAN_SEEK:
1969 case DEMUX_SET_PAUSE_STATE:
1970 case DEMUX_CAN_CONTROL_PACE:
1971 pb = (bool*)va_arg( args, bool * );
1975 case DEMUX_GET_PTS_DELAY:
1976 pi64 = (int64_t*)va_arg( args, int64_t * );
1978 INT64_C(1000) * var_InheritInteger( p_demux, "live-caching" );
1981 case DEMUX_GET_TIME:
1982 pi64 = (int64_t*)va_arg( args, int64_t * );
1986 /* TODO implement others */
1988 return VLC_EGENERIC;
1991 return VLC_EGENERIC;
1994 /*****************************************************************************
1995 * config variable callback
1996 *****************************************************************************/
1997 static int FindDevices( vlc_object_t *p_this, const char *psz_name,
1998 char ***vp, char ***tp )
2000 /* Find list of devices */
2001 list<string> list_devices;
2002 if( SUCCEEDED(CoInitializeEx( NULL, COINIT_MULTITHREADED ))
2003 || SUCCEEDED(CoInitializeEx( NULL, COINIT_APARTMENTTHREADED )) )
2005 bool b_audio = !strcmp( psz_name, CFG_PREFIX "adev" );
2007 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
2011 unsigned count = 2 + list_devices.size(), i = 2;
2012 char **values = (char **)xmalloc( count * sizeof(*values) );
2013 char **texts = (char **)xmalloc( count * sizeof(*texts) );
2015 values[0] = strdup( "" );
2016 texts[0] = strdup( N_("Default") );
2017 values[1] = strdup( "none" );
2018 texts[1] = strdup( N_("None") );
2020 for( list<string>::iterator iter = list_devices.begin();
2021 iter != list_devices.end();
2024 assert( i < count );
2025 values[i] = strdup( iter->c_str() );
2026 texts[i] = strdup( iter->c_str() );
2035 static int ConfigDevicesCallback( vlc_object_t *p_this, char const *psz_name,
2036 vlc_value_t newval, vlc_value_t, void * )
2038 module_config_t *p_item;
2039 bool b_audio = false;
2040 char *psz_device = NULL;
2041 int i_ret = VLC_SUCCESS;
2043 if( FAILED(CoInitializeEx( NULL, COINIT_MULTITHREADED ))
2044 && FAILED(CoInitializeEx( NULL, COINIT_APARTMENTTHREADED )) )
2045 return VLC_EGENERIC;
2047 if( !EMPTY_STR( newval.psz_string ) )
2048 psz_device = strdup( newval.psz_string );
2051 p_item = config_FindConfig( p_this, psz_name );
2060 if( !strcmp( psz_name, CFG_PREFIX "adev" ) ) b_audio = true;
2066 devicename = psz_device ;
2070 /* If no device name was specified, pick the 1st one */
2071 list<string> list_devices;
2073 /* Enumerate devices */
2074 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
2075 if( list_devices.empty() )
2078 return VLC_EGENERIC;
2080 devicename = *list_devices.begin();
2083 IBaseFilter *p_device_filter =
2084 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
2085 if( p_device_filter )
2087 ShowPropertyPage( p_device_filter );
2088 p_device_filter->Release();
2092 msg_Err( p_this, "didn't find device: %s", devicename.c_str() );
2093 i_ret = VLC_EGENERIC;
2101 /*****************************************************************************
2103 *****************************************************************************/
2105 static void ShowPropertyPage( IUnknown *obj )
2107 ISpecifyPropertyPages *p_spec;
2110 HRESULT hr = obj->QueryInterface( IID_ISpecifyPropertyPages,
2112 if( FAILED(hr) ) return;
2114 if( SUCCEEDED(p_spec->GetPages( &cauuid )) )
2116 if( cauuid.cElems > 0 )
2118 HWND hwnd_desktop = ::GetDesktopWindow();
2120 OleCreatePropertyFrame( hwnd_desktop, 30, 30, NULL, 1, &obj,
2121 cauuid.cElems, cauuid.pElems, 0, 0, NULL );
2123 CoTaskMemFree( cauuid.pElems );
2129 static void ShowDeviceProperties( vlc_object_t *p_this,
2130 ICaptureGraphBuilder2 *p_graph,
2131 IBaseFilter *p_device_filter,
2135 msg_Dbg( p_this, "configuring Device Properties" );
2138 * Video or audio capture filter page
2140 ShowPropertyPage( p_device_filter );
2145 if( p_graph && b_audio )
2147 IAMStreamConfig *p_SC;
2149 msg_Dbg( p_this, "showing WDM Audio Configuration Pages" );
2151 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2152 &MEDIATYPE_Audio, p_device_filter,
2153 IID_IAMStreamConfig, (void **)&p_SC );
2156 ShowPropertyPage(p_SC);
2164 HRESULT hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2165 &MEDIATYPE_Audio, p_device_filter,
2166 IID_IAMTVAudio, (void **)&p_TVA );
2169 ShowPropertyPage(p_TVA);
2177 if( p_graph && !b_audio )
2179 IAMStreamConfig *p_SC;
2181 msg_Dbg( p_this, "showing WDM Video Configuration Pages" );
2183 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2184 &MEDIATYPE_Interleaved, p_device_filter,
2185 IID_IAMStreamConfig, (void **)&p_SC );
2188 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2189 &MEDIATYPE_Video, p_device_filter,
2190 IID_IAMStreamConfig, (void **)&p_SC );
2195 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2196 &MEDIATYPE_Stream, p_device_filter,
2197 IID_IAMStreamConfig, (void **)&p_SC );
2202 ShowPropertyPage(p_SC);
2208 static void ShowTunerProperties( vlc_object_t *p_this,
2209 ICaptureGraphBuilder2 *p_graph,
2210 IBaseFilter *p_device_filter,
2214 msg_Dbg( p_this, "configuring Tuner Properties" );
2216 if( !p_graph || b_audio ) return;
2219 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2220 &MEDIATYPE_Interleaved, p_device_filter,
2221 IID_IAMTVTuner, (void **)&p_TV );
2224 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2225 &MEDIATYPE_Video, p_device_filter,
2226 IID_IAMTVTuner, (void **)&p_TV );
2231 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2232 &MEDIATYPE_Stream, p_device_filter,
2233 IID_IAMTVTuner, (void **)&p_TV );
2238 ShowPropertyPage(p_TV);
2243 static void ConfigTuner( vlc_object_t *p_this, ICaptureGraphBuilder2 *p_graph,
2244 IBaseFilter *p_device_filter )
2246 int i_channel, i_country, i_input, i_amtuner_mode, i_standard;
2248 unsigned i_frequency;
2252 if( !p_graph ) return;
2254 i_channel = var_GetInteger( p_this, CFG_PREFIX "tuner-channel" );
2255 i_country = var_GetInteger( p_this, CFG_PREFIX "tuner-country" );
2256 i_input = var_GetInteger( p_this, CFG_PREFIX "tuner-input" );
2257 i_amtuner_mode = var_GetInteger( p_this, CFG_PREFIX "amtuner-mode" );
2258 i_frequency = var_GetInteger( p_this, CFG_PREFIX "tuner-frequency" );
2259 i_standard = var_GetInteger( p_this, CFG_PREFIX "tuner-standard" );
2261 if( !i_channel && !i_frequency && !i_country && !i_input ) return; /* Nothing to do */
2263 msg_Dbg( p_this, "tuner config: channel %i, frequency %i, country %i, input type %i, standard %s",
2264 i_channel, i_frequency, i_country, i_input, ppsz_standards_list_text[i_standard] );
2267 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Interleaved,
2268 p_device_filter, IID_IAMTVTuner,
2272 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
2273 p_device_filter, IID_IAMTVTuner,
2279 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Stream,
2280 p_device_filter, IID_IAMTVTuner,
2286 msg_Dbg( p_this, "couldn't find tuner interface" );
2290 hr = p_TV->GetAvailableModes( &l_modes );
2291 if( SUCCEEDED(hr) && (l_modes & i_amtuner_mode) )
2293 hr = p_TV->put_Mode( (AMTunerModeType)i_amtuner_mode );
2296 if( i_input == 1 ) p_TV->put_InputType( 0, TunerInputCable );
2297 else if( i_input == 2 ) p_TV->put_InputType( 0, TunerInputAntenna );
2299 p_TV->put_CountryCode( i_country );
2301 if( i_frequency <= 0 ) p_TV->put_Channel( i_channel, AMTUNER_SUBCHAN_NO_TUNE,
2302 AMTUNER_SUBCHAN_NO_TUNE );
2304 if( i_frequency > 0 || i_standard > 0) {
2305 IKsPropertySet *pKs = NULL;
2306 DWORD dw_supported = 0;
2307 KSPROPERTY_TUNER_MODE_CAPS_S ModeCaps;
2308 KSPROPERTY_TUNER_FREQUENCY_S Frequency;
2309 KSPROPERTY_TUNER_STANDARD_S Standard;
2311 hr = p_TV->QueryInterface(IID_IKsPropertySet,(void **)&pKs);
2314 msg_Dbg( p_this, "Couldn't QI for IKsPropertySet" );
2319 memset(&ModeCaps,0,sizeof(KSPROPERTY_TUNER_MODE_CAPS_S));
2320 memset(&Frequency,0,sizeof(KSPROPERTY_TUNER_FREQUENCY_S));
2321 memset(&Standard,0,sizeof(KSPROPERTY_TUNER_STANDARD_S));
2322 ModeCaps.Mode = AMTUNER_MODE_TV;
2324 hr = pKs->QuerySupported(PROPSETID_TUNER,
2325 KSPROPERTY_TUNER_MODE_CAPS,&dw_supported);
2326 if(SUCCEEDED(hr) && dw_supported&KSPROPERTY_SUPPORT_GET)
2329 hr = pKs->Get(PROPSETID_TUNER,KSPROPERTY_TUNER_MODE_CAPS,
2330 INSTANCEDATA_OF_PROPERTY_PTR(&ModeCaps),
2331 INSTANCEDATA_OF_PROPERTY_SIZE(ModeCaps),
2338 msg_Dbg( p_this, "KSPROPERTY_TUNER_MODE_CAPS not supported!" );
2342 msg_Dbg( p_this, "Frequency range supported from %ld to %ld.",
2343 ModeCaps.MinFrequency, ModeCaps.MaxFrequency);
2344 msg_Dbg( p_this, "Video standards supported by the tuner: ");
2345 for(size_t i = 0 ; i < ARRAY_SIZE(ppsz_standards_list_text); i++) {
2346 if(ModeCaps.StandardsSupported & i_standards_list[i])
2347 msg_Dbg( p_this, "%s, ", ppsz_standards_list_text[i]);
2350 if(i_frequency > 0) {
2351 Frequency.Frequency=i_frequency;
2352 if(ModeCaps.Strategy==KS_TUNER_STRATEGY_DRIVER_TUNES)
2353 Frequency.TuningFlags=KS_TUNER_TUNING_FINE;
2355 Frequency.TuningFlags=KS_TUNER_TUNING_EXACT;
2357 if(i_frequency>=ModeCaps.MinFrequency && i_frequency<=ModeCaps.MaxFrequency)
2360 hr = pKs->Set(PROPSETID_TUNER,
2361 KSPROPERTY_TUNER_FREQUENCY,
2362 INSTANCEDATA_OF_PROPERTY_PTR(&Frequency),
2363 INSTANCEDATA_OF_PROPERTY_SIZE(Frequency),
2368 msg_Dbg( p_this, "Couldn't set KSPROPERTY_TUNER_FREQUENCY!" );
2374 msg_Dbg( p_this, "Requested frequency exceeds the supported range!" );
2379 if(i_standard > 0) {
2380 if(i_standard & ModeCaps.StandardsSupported )
2382 Standard.Standard = i_standard;
2383 hr = pKs->Set(PROPSETID_TUNER,
2384 KSPROPERTY_TUNER_STANDARD,
2385 INSTANCEDATA_OF_PROPERTY_PTR(&Standard),
2386 INSTANCEDATA_OF_PROPERTY_SIZE(Standard),
2391 msg_Dbg( p_this, "Couldn't set KSPROPERTY_TUNER_STANDARD!" );
2397 msg_Dbg( p_this, "Requested video standard is not supported by the tuner!" );