1 /*****************************************************************************
2 * dshow.cpp : DirectShow access module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 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_FORMAT_MACROS 1
37 #include <vlc_plugin.h>
38 #include <vlc_input.h>
39 #include <vlc_access.h>
40 #include <vlc_demux.h>
42 #include <vlc_interface.h>
47 /*****************************************************************************
48 * Access: local prototypes
49 *****************************************************************************/
50 static block_t *ReadCompressed( access_t * );
51 static int AccessControl ( access_t *, int, va_list );
53 static int Demux ( demux_t * );
54 static int DemuxControl( demux_t *, int, va_list );
56 static int OpenDevice( vlc_object_t *, access_sys_t *, string, bool );
57 static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
58 list<string> *, bool );
59 static size_t EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
60 int, int, int, int, int, int,
61 AM_MEDIA_TYPE *mt, size_t );
62 static bool ConnectFilters( vlc_object_t *, access_sys_t *,
63 IBaseFilter *, CaptureFilter * );
64 static int FindDevicesCallback( vlc_object_t *, char const *,
65 vlc_value_t, vlc_value_t, void * );
66 static int ConfigDevicesCallback( vlc_object_t *, char const *,
67 vlc_value_t, vlc_value_t, void * );
69 static void ShowPropertyPage( IUnknown * );
70 static void ShowDeviceProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
71 IBaseFilter *, bool );
72 static void ShowTunerProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
73 IBaseFilter *, bool );
74 static void ConfigTuner( vlc_object_t *, ICaptureGraphBuilder2 *,
77 /*****************************************************************************
79 *****************************************************************************/
80 static const char *ppsz_vdev[] = { "", "none" };
81 static const char *ppsz_vdev_text[] = { N_("Default"), N_("None") };
82 static const char *ppsz_adev[] = { "", "none" };
83 static const char *ppsz_adev_text[] = { N_("Default"), N_("None") };
84 static int pi_tuner_input[] = { 0, 1, 2 };
85 static const char *ppsz_tuner_input_text[] =
86 {N_("Default"), N_("Cable"), N_("Antenna")};
87 static const int pi_amtuner_mode[] = { AMTUNER_MODE_DEFAULT,
89 AMTUNER_MODE_FM_RADIO,
90 AMTUNER_MODE_AM_RADIO,
92 static const char *ppsz_amtuner_mode_text[] = { N_("Default"),
98 #define CACHING_TEXT N_("Caching value in ms")
99 #define CACHING_LONGTEXT N_( \
100 "Caching value for DirectShow streams. " \
101 "This value should be set in millisecondss." )
102 #define VDEV_TEXT N_("Video device name")
103 #define VDEV_LONGTEXT N_( \
104 "Name of the video device that will be used by the " \
105 "DirectShow plugin. If you don't specify anything, the default device " \
107 #define ADEV_TEXT N_("Audio device name")
108 #define ADEV_LONGTEXT N_( \
109 "Name of the audio device that will be used by the " \
110 "DirectShow plugin. If you don't specify anything, the default device " \
112 #define SIZE_TEXT N_("Video size")
113 #define SIZE_LONGTEXT N_( \
114 "Size of the video that will be displayed by the " \
115 "DirectShow plugin. If you don't specify anything the default size for " \
116 "your device will be used. You can specify a standard size (cif, d1, ...) or <width>x<height>.")
117 #define CHROMA_TEXT N_("Video input chroma format")
118 #define CHROMA_LONGTEXT N_( \
119 "Force the DirectShow video input to use a specific chroma format " \
120 "(eg. I420 (default), RV24, etc.)")
121 #define FPS_TEXT N_("Video input frame rate")
122 #define FPS_LONGTEXT N_( \
123 "Force the DirectShow video input to use a specific frame rate" \
124 "(eg. 0 means default, 25, 29.97, 50, 59.94, etc.)")
125 #define CONFIG_TEXT N_("Device properties")
126 #define CONFIG_LONGTEXT N_( \
127 "Show the properties dialog of the selected device before starting the " \
129 #define TUNER_TEXT N_("Tuner properties")
130 #define TUNER_LONGTEXT N_( \
131 "Show the tuner properties [channel selection] page." )
132 #define CHANNEL_TEXT N_("Tuner TV Channel")
133 #define CHANNEL_LONGTEXT N_( \
134 "Set the TV channel the tuner will set to " \
135 "(0 means default)." )
136 #define COUNTRY_TEXT N_("Tuner country code")
137 #define COUNTRY_LONGTEXT N_( \
138 "Set the tuner country code that establishes the current " \
139 "channel-to-frequency mapping (0 means default)." )
140 #define TUNER_INPUT_TEXT N_("Tuner input type")
141 #define TUNER_INPUT_LONGTEXT N_( \
142 "Select the tuner input type (Cable/Antenna)." )
143 #define VIDEO_IN_TEXT N_("Video input pin")
144 #define VIDEO_IN_LONGTEXT N_( \
145 "Select the video input source, such as composite, s-video, " \
146 "or tuner. Since these settings are hardware-specific, you should find good " \
147 "settings in the \"Device config\" area, and use those numbers here. -1 " \
148 "means that settings will not be changed.")
149 #define AUDIO_IN_TEXT N_("Audio input pin")
150 #define AUDIO_IN_LONGTEXT N_( \
151 "Select the audio input source. See the \"video input\" option." )
152 #define VIDEO_OUT_TEXT N_("Video output pin")
153 #define VIDEO_OUT_LONGTEXT N_( \
154 "Select the video output type. See the \"video input\" option." )
155 #define AUDIO_OUT_TEXT N_("Audio output pin")
156 #define AUDIO_OUT_LONGTEXT N_( \
157 "Select the audio output type. See the \"video input\" option." )
159 #define AMTUNER_MODE_TEXT N_("AM Tuner mode")
160 #define AMTUNER_MODE_LONGTEXT N_( \
161 "AM Tuner mode. Can be one of DEFAULT, TV, AM_RADIO, FM_RADIO or DSS.")
163 static int CommonOpen ( vlc_object_t *, access_sys_t *, bool );
164 static void CommonClose( vlc_object_t *, access_sys_t * );
166 static int AccessOpen ( vlc_object_t * );
167 static void AccessClose( vlc_object_t * );
169 static int DemuxOpen ( vlc_object_t * );
170 static void DemuxClose ( vlc_object_t * );
173 set_shortname( _("DirectShow") );
174 set_description( _("DirectShow input") );
175 set_category( CAT_INPUT );
176 set_subcategory( SUBCAT_INPUT_ACCESS );
177 add_integer( "dshow-caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
178 CACHING_TEXT, CACHING_LONGTEXT, true );
180 add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, false);
181 change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback );
182 change_action_add( FindDevicesCallback, N_("Refresh list") );
183 change_action_add( ConfigDevicesCallback, N_("Configure") );
185 add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, false);
186 change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback );
187 change_action_add( FindDevicesCallback, N_("Refresh list") );
188 change_action_add( ConfigDevicesCallback, N_("Configure") );
190 add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, false);
192 add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
195 add_float( "dshow-fps", 0.0f, NULL, FPS_TEXT, FPS_LONGTEXT,
198 add_bool( "dshow-config", false, NULL, CONFIG_TEXT, CONFIG_LONGTEXT,
201 add_bool( "dshow-tuner", false, NULL, TUNER_TEXT, TUNER_LONGTEXT,
204 add_integer( "dshow-tuner-channel", 0, NULL, CHANNEL_TEXT,
205 CHANNEL_LONGTEXT, true );
207 add_integer( "dshow-tuner-country", 0, NULL, COUNTRY_TEXT,
208 COUNTRY_LONGTEXT, true );
210 add_integer( "dshow-tuner-input", 0, NULL, TUNER_INPUT_TEXT,
211 TUNER_INPUT_LONGTEXT, true );
212 change_integer_list( pi_tuner_input, ppsz_tuner_input_text, 0 );
214 add_integer( "dshow-video-input", -1, NULL, VIDEO_IN_TEXT,
215 VIDEO_IN_LONGTEXT, true );
217 add_integer( "dshow-audio-input", -1, NULL, AUDIO_IN_TEXT,
218 AUDIO_IN_LONGTEXT, true );
220 add_integer( "dshow-video-output", -1, NULL, VIDEO_OUT_TEXT,
221 VIDEO_OUT_LONGTEXT, true );
223 add_integer( "dshow-audio-output", -1, NULL, AUDIO_OUT_TEXT,
224 AUDIO_OUT_LONGTEXT, true );
226 add_integer( "dshow-amtuner-mode", AMTUNER_MODE_TV, NULL,
227 AMTUNER_MODE_TEXT, AMTUNER_MODE_LONGTEXT, false);
228 change_integer_list( pi_amtuner_mode, ppsz_amtuner_mode_text, 0 );
230 add_shortcut( "dshow" );
231 set_capability( "access_demux", 0 );
232 set_callbacks( DemuxOpen, DemuxClose );
235 set_description( _("DirectShow input") );
236 set_capability( "access", 0 );
237 set_callbacks( AccessOpen, AccessClose );
241 /*****************************************************************************
242 * DirectShow elementary stream descriptor
243 *****************************************************************************/
244 typedef struct dshow_stream_t
247 IBaseFilter *p_device_filter;
248 CaptureFilter *p_capture_filter;
253 VIDEOINFOHEADER video;
264 /*****************************************************************************
265 * DirectShow utility functions
266 *****************************************************************************/
267 static void CreateDirectShowGraph( access_sys_t *p_sys )
269 p_sys->i_crossbar_route_depth = 0;
271 /* Create directshow filter graph */
272 if( SUCCEEDED( CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
273 (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph) ) )
275 /* Create directshow capture graph builder if available */
276 if( SUCCEEDED( CoCreateInstance( CLSID_CaptureGraphBuilder2, 0,
277 CLSCTX_INPROC, (REFIID)IID_ICaptureGraphBuilder2,
278 (void **)&p_sys->p_capture_graph_builder2 ) ) )
280 p_sys->p_capture_graph_builder2->
281 SetFiltergraph((IGraphBuilder *)p_sys->p_graph);
284 p_sys->p_graph->QueryInterface( IID_IMediaControl,
285 (void **)&p_sys->p_control );
289 static void DeleteDirectShowGraph( access_sys_t *p_sys )
291 DeleteCrossbarRoutes( p_sys );
293 /* Remove filters from graph */
294 for( int i = 0; i < p_sys->i_streams; i++ )
296 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_capture_filter );
297 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_device_filter );
298 p_sys->pp_streams[i]->p_capture_filter->Release();
299 p_sys->pp_streams[i]->p_device_filter->Release();
302 /* Release directshow objects */
303 if( p_sys->p_control )
305 p_sys->p_control->Release();
306 p_sys->p_control = NULL;
308 if( p_sys->p_capture_graph_builder2 )
310 p_sys->p_capture_graph_builder2->Release();
311 p_sys->p_capture_graph_builder2 = NULL;
316 p_sys->p_graph->Release();
317 p_sys->p_graph = NULL;
321 /*****************************************************************************
322 * CommonOpen: open direct show device
323 *****************************************************************************/
324 static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
325 bool b_access_demux )
330 /* Get/parse options and open device(s) */
331 string vdevname, adevname;
332 int i_width = 0, i_height = 0, i_chroma = 0;
335 var_Create( p_this, "dshow-config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
336 var_Create( p_this, "dshow-tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
337 var_Create( p_this, "dshow-vdev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
338 var_Get( p_this, "dshow-vdev", &val );
339 if( val.psz_string ) vdevname = string( val.psz_string );
340 free( val.psz_string );
342 var_Create( p_this, "dshow-adev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
343 var_Get( p_this, "dshow-adev", &val );
344 if( val.psz_string ) adevname = string( val.psz_string );
345 free( val.psz_string );
347 static struct {char *psz_size; int i_width; int i_height;} size_table[] =
348 { { "subqcif", 128, 96 }, { "qsif", 160, 120 }, { "qcif", 176, 144 },
349 { "sif", 320, 240 }, { "cif", 352, 288 }, { "d1", 640, 480 },
353 var_Create( p_this, "dshow-size", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
354 var_Get( p_this, "dshow-size", &val );
355 if( val.psz_string && *val.psz_string )
357 for( i = 0; size_table[i].psz_size; i++ )
359 if( !strcmp( val.psz_string, size_table[i].psz_size ) )
361 i_width = size_table[i].i_width;
362 i_height = size_table[i].i_height;
366 if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
369 i_width = strtol( val.psz_string, &psz_parser, 0 );
370 if( *psz_parser == 'x' || *psz_parser == 'X')
372 i_height = strtol( psz_parser + 1, &psz_parser, 0 );
374 msg_Dbg( p_this, "width x height %dx%d", i_width, i_height );
377 free( val.psz_string );
379 p_sys->b_chroma = false;
380 var_Create( p_this, "dshow-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
381 var_Get( p_this, "dshow-chroma", &val );
382 if( val.psz_string && strlen( val.psz_string ) >= 4 )
384 i_chroma = VLC_FOURCC( val.psz_string[0], val.psz_string[1],
385 val.psz_string[2], val.psz_string[3] );
386 p_sys->b_chroma = true;
388 free( val.psz_string );
390 var_Create( p_this, "dshow-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
391 var_Create( p_this, "dshow-tuner-channel",
392 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
393 var_Create( p_this, "dshow-tuner-country",
394 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
395 var_Create( p_this, "dshow-tuner-input",
396 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
398 var_Create( p_this, "dshow-amtuner-mode",
399 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
401 var_Create( p_this, "dshow-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
403 var_Create( p_this, "dshow-video-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
404 var_Create( p_this, "dshow-audio-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
405 var_Create( p_this, "dshow-video-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
406 var_Create( p_this, "dshow-audio-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
408 /* Initialize OLE/COM */
411 /* Initialize some data */
412 p_sys->i_streams = 0;
413 p_sys->pp_streams = 0;
414 p_sys->i_width = i_width;
415 p_sys->i_height = i_height;
416 p_sys->i_chroma = i_chroma;
418 p_sys->p_graph = NULL;
419 p_sys->p_capture_graph_builder2 = NULL;
420 p_sys->p_control = NULL;
422 vlc_mutex_init( &p_sys->lock );
423 vlc_cond_init( p_this, &p_sys->wait );
425 /* Build directshow graph */
426 CreateDirectShowGraph( p_sys );
428 if( OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
430 msg_Err( p_this, "can't open video");
434 /* Check if we can handle the demuxing ourselves or need to spawn
435 * a demuxer module */
436 dshow_stream_t *p_stream = p_sys->pp_streams[p_sys->i_streams-1];
438 if( p_stream->mt.majortype == MEDIATYPE_Video )
440 if( /* Raw DV stream */
441 p_stream->i_fourcc == VLC_FOURCC('d','v','s','l') ||
442 p_stream->i_fourcc == VLC_FOURCC('d','v','s','d') ||
443 p_stream->i_fourcc == VLC_FOURCC('d','v','h','d') ||
444 /* Raw MPEG video stream */
445 p_stream->i_fourcc == VLC_FOURCC('m','p','2','v') )
451 /* Let the access (only) take care of that */
457 if( p_stream->mt.majortype == MEDIATYPE_Stream )
463 /* Let the access (only) take care of that */
467 var_Get( p_this, "dshow-tuner", &val );
470 /* FIXME: we do MEDIATYPE_Stream here so we don't do
472 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
473 p_stream->p_device_filter, 0 );
478 if( b_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
480 msg_Err( p_this, "can't open audio");
483 for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
485 var_Get( p_this, "dshow-video-input", &val );
487 p_sys->crossbar_routes[i].VideoInputIndex=val.i_int;
488 var_Get( p_this, "dshow-video-output", &val );
490 p_sys->crossbar_routes[i].VideoOutputIndex=val.i_int;
491 var_Get( p_this, "dshow-audio-input", &val );
493 p_sys->crossbar_routes[i].AudioInputIndex=val.i_int;
494 var_Get( p_this, "dshow-audio-output", &val );
496 p_sys->crossbar_routes[i].AudioOutputIndex=val.i_int;
498 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
499 LONG VideoInputIndex = p_sys->crossbar_routes[i].VideoInputIndex;
500 LONG VideoOutputIndex = p_sys->crossbar_routes[i].VideoOutputIndex;
501 LONG AudioInputIndex = p_sys->crossbar_routes[i].AudioInputIndex;
502 LONG AudioOutputIndex = p_sys->crossbar_routes[i].AudioOutputIndex;
504 if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
506 msg_Dbg( p_this, "crossbar at depth %d, routed video "
507 "output %ld to video input %ld", i, VideoOutputIndex,
510 if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
512 if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
515 msg_Dbg(p_this, "crossbar at depth %d, routed audio "
516 "output %ld to audio input %ld", i,
517 AudioOutputIndex, AudioInputIndex );
524 ** Show properties pages from other filters in graph
526 var_Get( p_this, "dshow-config", &val );
529 for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
531 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
534 if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
537 ShowPropertyPage( p_XF );
543 /* Initialize some data */
544 p_sys->i_current_stream = 0;
546 if( !p_sys->i_streams ) return VLC_EGENERIC;
551 /*****************************************************************************
552 * DemuxOpen: open direct show device as an access_demux module
553 *****************************************************************************/
554 static int DemuxOpen( vlc_object_t *p_this )
556 demux_t *p_demux = (demux_t *)p_this;
560 p_sys = (access_sys_t *)malloc( sizeof( access_sys_t ) );
561 memset( p_sys, 0, sizeof( access_sys_t ) );
562 p_demux->p_sys = (demux_sys_t *)p_sys;
564 if( CommonOpen( p_this, p_sys, true ) != VLC_SUCCESS )
566 CommonClose( p_this, p_sys );
570 /* Everything is ready. Let's rock baby */
571 msg_Dbg( p_this, "Playing...");
572 p_sys->p_control->Run();
574 p_demux->pf_demux = Demux;
575 p_demux->pf_control = DemuxControl;
576 p_demux->info.i_update = 0;
577 p_demux->info.i_title = 0;
578 p_demux->info.i_seekpoint = 0;
580 for( i = 0; i < p_sys->i_streams; i++ )
582 dshow_stream_t *p_stream = p_sys->pp_streams[i];
585 if( p_stream->mt.majortype == MEDIATYPE_Video )
587 es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
589 fmt.video.i_width = p_stream->header.video.bmiHeader.biWidth;
590 fmt.video.i_height = p_stream->header.video.bmiHeader.biHeight;
591 fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
593 if( !p_stream->header.video.bmiHeader.biCompression )
595 /* RGB DIB are coded from bottom to top */
596 fmt.video.i_height = (unsigned int)(-(int)fmt.video.i_height);
599 /* Setup rgb mask for RGB formats */
600 if( p_stream->i_fourcc == VLC_FOURCC('R','V','2','4') )
602 /* This is in BGR format */
603 fmt.video.i_bmask = 0x00ff0000;
604 fmt.video.i_gmask = 0x0000ff00;
605 fmt.video.i_rmask = 0x000000ff;
608 if( p_stream->header.video.AvgTimePerFrame )
610 fmt.video.i_frame_rate = 10000000;
611 fmt.video.i_frame_rate_base =
612 p_stream->header.video.AvgTimePerFrame;
615 else if( p_stream->mt.majortype == MEDIATYPE_Audio )
617 es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
619 fmt.audio.i_channels = p_stream->header.audio.nChannels;
620 fmt.audio.i_rate = p_stream->header.audio.nSamplesPerSec;
621 fmt.audio.i_bitspersample = p_stream->header.audio.wBitsPerSample;
622 fmt.audio.i_blockalign = fmt.audio.i_channels *
623 fmt.audio.i_bitspersample / 8;
624 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
625 fmt.audio.i_bitspersample;
628 p_stream->p_es = es_out_Add( p_demux->out, &fmt );
634 /*****************************************************************************
635 * AccessOpen: open direct show device as an access module
636 *****************************************************************************/
637 static int AccessOpen( vlc_object_t *p_this )
639 access_t *p_access = (access_t*)p_this;
642 p_access->p_sys = p_sys = (access_sys_t *)malloc( sizeof( access_sys_t ) );
643 memset( p_sys, 0, sizeof( access_sys_t ) );
645 if( CommonOpen( p_this, p_sys, false ) != VLC_SUCCESS )
647 CommonClose( p_this, p_sys );
651 dshow_stream_t *p_stream = p_sys->pp_streams[0];
653 /* Check if we need to force demuxers */
654 if( !p_access->psz_demux || !*p_access->psz_demux )
656 if( p_stream->i_fourcc == VLC_FOURCC('d','v','s','l') ||
657 p_stream->i_fourcc == VLC_FOURCC('d','v','s','d') ||
658 p_stream->i_fourcc == VLC_FOURCC('d','v','h','d') )
660 free( p_access->psz_demux );
661 p_access->psz_demux = strdup( "rawdv" );
663 else if( p_stream->i_fourcc == VLC_FOURCC('m','p','2','v') )
665 free( p_access->psz_demux );
666 p_access->psz_demux = strdup( "mpgv" );
671 p_access->pf_read = NULL;
672 p_access->pf_block = ReadCompressed;
673 p_access->pf_control = AccessControl;
674 p_access->pf_seek = NULL;
675 p_access->info.i_update = 0;
676 p_access->info.i_size = 0;
677 p_access->info.i_pos = 0;
678 p_access->info.b_eof = false;
679 p_access->info.i_title = 0;
680 p_access->info.i_seekpoint = 0;
681 p_access->p_sys = p_sys;
683 /* Everything is ready. Let's rock baby */
684 msg_Dbg( p_this, "Playing...");
685 p_sys->p_control->Run();
690 /*****************************************************************************
691 * CommonClose: close device
692 *****************************************************************************/
693 static void CommonClose( vlc_object_t *p_this, access_sys_t *p_sys )
695 msg_Dbg( p_this, "releasing DirectShow");
697 DeleteDirectShowGraph( p_sys );
699 /* Uninitialize OLE/COM */
702 for( int i = 0; i < p_sys->i_streams; i++ ) delete p_sys->pp_streams[i];
703 if( p_sys->i_streams ) free( p_sys->pp_streams );
705 vlc_mutex_destroy( &p_sys->lock );
706 vlc_cond_destroy( &p_sys->wait );
711 /*****************************************************************************
712 * AccessClose: close device
713 *****************************************************************************/
714 static void AccessClose( vlc_object_t *p_this )
716 access_t *p_access = (access_t *)p_this;
717 access_sys_t *p_sys = p_access->p_sys;
719 /* Stop capturing stuff */
720 p_sys->p_control->Stop();
722 CommonClose( p_this, p_sys );
725 /*****************************************************************************
726 * DemuxClose: close device
727 *****************************************************************************/
728 static void DemuxClose( vlc_object_t *p_this )
730 demux_t *p_demux = (demux_t *)p_this;
731 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
733 /* Stop capturing stuff */
734 p_sys->p_control->Stop();
736 CommonClose( p_this, p_sys );
739 /****************************************************************************
741 ****************************************************************************/
742 static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
743 IBaseFilter *p_filter,
744 CaptureFilter *p_capture_filter )
746 CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
748 AM_MEDIA_TYPE mediaType = p_input_pin->CustomGetMediaType();
750 if( p_sys->p_capture_graph_builder2 )
752 if( FAILED(p_sys->p_capture_graph_builder2->
753 RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
754 p_filter, 0, (IBaseFilter *)p_capture_filter )) )
759 // Sort out all the possible video inputs
760 // The class needs to be given the capture filters ANALOGVIDEO input pin
762 if( ( mediaType.majortype == MEDIATYPE_Video ||
763 mediaType.majortype == MEDIATYPE_Stream ) &&
764 SUCCEEDED(p_filter->EnumPins(&pins)) )
770 IKsPropertySet *pKs=0;
774 while( !Found && ( S_OK == pins->Next(1, &pP, &n) ) )
776 if( S_OK == pP->QueryPinInfo(&pinInfo) )
778 // is this pin an ANALOGVIDEOIN input pin?
779 if( pinInfo.dir == PINDIR_INPUT &&
780 pP->QueryInterface( IID_IKsPropertySet,
781 (void **)&pKs ) == S_OK )
783 if( pKs->Get( AMPROPSETID_Pin,
784 AMPROPERTY_PIN_CATEGORY, NULL, 0,
785 &guid, sizeof(GUID), &dw ) == S_OK )
787 if( guid == PIN_CATEGORY_ANALOGVIDEOIN )
789 // recursively search crossbar routes
790 FindCrossbarRoutes( p_this, p_sys, pP, 0 );
797 pinInfo.pFilter->Release();
807 IEnumPins *p_enumpins;
810 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;
812 while( S_OK == p_enumpins->Next( 1, &p_pin, NULL ) )
814 PIN_DIRECTION pin_dir;
815 p_pin->QueryDirection( &pin_dir );
817 if( pin_dir == PINDIR_OUTPUT &&
818 p_sys->p_graph->ConnectDirect( p_pin, (IPin *)p_input_pin,
822 p_enumpins->Release();
828 p_enumpins->Release();
834 * get fourcc priority from arbritary preference, the higher the better
836 static int GetFourCCPriority( int i_fourcc )
840 case VLC_FOURCC('I','4','2','0'):
841 case VLC_FOURCC('f','l','3','2'):
843 case VLC_FOURCC('Y','V','1','2'):
844 case VLC_FOURCC('a','r','a','w'):
846 case VLC_FOURCC('R','V','2','4'):
848 case VLC_FOURCC('Y','U','Y','2'):
849 case VLC_FOURCC('R','V','3','2'):
850 case VLC_FOURCC('R','G','B','A'):
857 #define MAX_MEDIA_TYPES 32
859 static int OpenDevice( vlc_object_t *p_this, access_sys_t *p_sys,
860 string devicename, bool b_audio )
862 /* See if device is already opened */
863 for( int i = 0; i < p_sys->i_streams; i++ )
865 if( devicename.size() &&
866 p_sys->pp_streams[i]->devicename == devicename )
873 list<string> list_devices;
875 /* Enumerate devices and display their names */
876 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
878 if( !list_devices.size() )
881 list<string>::iterator iter;
882 for( iter = list_devices.begin(); iter != list_devices.end(); iter++ )
883 msg_Dbg( p_this, "found device: %s", iter->c_str() );
885 /* If no device name was specified, pick the 1st one */
886 if( devicename.size() == 0 )
888 devicename = *list_devices.begin();
891 // Use the system device enumerator and class enumerator to find
892 // a capture/preview device, such as a desktop USB video camera.
893 IBaseFilter *p_device_filter =
894 FindCaptureDevice( p_this, &devicename, 0, b_audio );
895 if( p_device_filter )
896 msg_Dbg( p_this, "using device: %s", devicename.c_str() );
899 msg_Err( p_this, "can't use device: %s, unsupported device type",
900 devicename.c_str() );
901 intf_UserFatal( p_this, false, _("Capturing failed"),
902 _("VLC cannot use the device \"%s\", because its "
903 "type is not supported.") );
907 // Retreive acceptable media types supported by device
908 AM_MEDIA_TYPE media_types[MAX_MEDIA_TYPES];
910 EnumDeviceCaps( p_this, p_device_filter, b_audio ? 0 : p_sys->i_chroma,
911 p_sys->i_width, p_sys->i_height,
912 0, 0, 0, media_types, MAX_MEDIA_TYPES );
914 AM_MEDIA_TYPE *mt = NULL;
916 if( media_count > 0 )
918 mt = (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * media_count);
920 // Order and copy returned media types according to arbitrary
922 for( size_t c = 0; c < media_count; c++ )
925 GetFourCCPriority(GetFourCCFromMediaType(media_types[c]));
926 size_t slot_copy = c;
927 for( size_t d = c+1; d < media_count; d++ )
930 GetFourCCPriority(GetFourCCFromMediaType(media_types[d]));
931 if( priority > slot_priority )
933 slot_priority = priority;
939 mt[c] = media_types[slot_copy];
940 media_types[slot_copy] = media_types[c];
944 mt[c] = media_types[c];
950 msg_Err( p_this, "capture device '%s' does not support required parameters !", devicename.c_str() );
951 intf_UserFatal( p_this, false, _("Capturing failed"),
952 _("The capture device \"%s\" does not support the "
953 "required parameters."), devicename.c_str() );
954 p_device_filter->Release();
958 /* Create and add our capture filter */
959 CaptureFilter *p_capture_filter =
960 new CaptureFilter( p_this, p_sys, mt, media_count );
961 p_sys->p_graph->AddFilter( p_capture_filter, 0 );
963 /* Add the device filter to the graph (seems necessary with VfW before
964 * accessing pin attributes). */
965 p_sys->p_graph->AddFilter( p_device_filter, 0 );
967 /* Attempt to connect one of this device's capture output pins */
968 msg_Dbg( p_this, "connecting filters" );
969 if( ConnectFilters( p_this, p_sys, p_device_filter, p_capture_filter ) )
972 msg_Dbg( p_this, "filters connected successfully !" );
974 dshow_stream_t dshow_stream;
975 dshow_stream.b_pts = false;
976 dshow_stream.p_es = 0;
978 p_capture_filter->CustomGetPin()->CustomGetMediaType();
980 /* Show Device properties. Done here so the VLC stream is setup with
981 * the proper parameters. */
983 var_Get( p_this, "dshow-config", &val );
986 ShowDeviceProperties( p_this, p_sys->p_capture_graph_builder2,
987 p_device_filter, b_audio );
990 ConfigTuner( p_this, p_sys->p_capture_graph_builder2,
993 var_Get( p_this, "dshow-tuner", &val );
994 if( val.b_bool && dshow_stream.mt.majortype != MEDIATYPE_Stream )
996 /* FIXME: we do MEDIATYPE_Stream later so we don't do it twice. */
997 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
998 p_device_filter, b_audio );
1002 p_capture_filter->CustomGetPin()->CustomGetMediaType();
1004 dshow_stream.i_fourcc = GetFourCCFromMediaType( dshow_stream.mt );
1005 if( dshow_stream.i_fourcc )
1007 if( dshow_stream.mt.majortype == MEDIATYPE_Video )
1009 dshow_stream.header.video =
1010 *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
1011 msg_Dbg( p_this, "MEDIATYPE_Video" );
1012 msg_Dbg( p_this, "selected video pin accepts format: %4.4s",
1013 (char *)&dshow_stream.i_fourcc);
1015 else if( dshow_stream.mt.majortype == MEDIATYPE_Audio )
1017 dshow_stream.header.audio =
1018 *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
1019 msg_Dbg( p_this, "MEDIATYPE_Audio" );
1020 msg_Dbg( p_this, "selected audio pin accepts format: %4.4s",
1021 (char *)&dshow_stream.i_fourcc);
1023 else if( dshow_stream.mt.majortype == MEDIATYPE_Stream )
1025 msg_Dbg( p_this, "MEDIATYPE_Stream" );
1026 msg_Dbg( p_this, "selected stream pin accepts format: %4.4s",
1027 (char *)&dshow_stream.i_fourcc);
1031 msg_Dbg( p_this, "unknown stream majortype" );
1035 /* Add directshow elementary stream to our list */
1036 dshow_stream.p_device_filter = p_device_filter;
1037 dshow_stream.p_capture_filter = p_capture_filter;
1039 p_sys->pp_streams = (dshow_stream_t **)realloc( p_sys->pp_streams,
1040 sizeof(dshow_stream_t *) * (p_sys->i_streams + 1) );
1041 p_sys->pp_streams[p_sys->i_streams] = new dshow_stream_t;
1042 *p_sys->pp_streams[p_sys->i_streams++] = dshow_stream;
1049 /* Remove filters from graph */
1050 p_sys->p_graph->RemoveFilter( p_device_filter );
1051 p_sys->p_graph->RemoveFilter( p_capture_filter );
1053 /* Release objects */
1054 p_device_filter->Release();
1055 p_capture_filter->Release();
1057 return VLC_EGENERIC;
1060 static IBaseFilter *
1061 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
1062 list<string> *p_listdevices, bool b_audio )
1064 IBaseFilter *p_base_filter = NULL;
1065 IMoniker *p_moniker = NULL;
1068 list<string> devicelist;
1070 /* Create the system device enumerator */
1071 ICreateDevEnum *p_dev_enum = NULL;
1073 hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
1074 IID_ICreateDevEnum, (void **)&p_dev_enum );
1077 msg_Err( p_this, "failed to create the device enumerator (0x%lx)", hr);
1081 /* Create an enumerator for the video capture devices */
1082 IEnumMoniker *p_class_enum = NULL;
1084 hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
1087 hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
1089 p_dev_enum->Release();
1092 msg_Err( p_this, "failed to create the class enumerator (0x%lx)", hr );
1096 /* If there are no enumerators for the requested type, then
1097 * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
1098 if( p_class_enum == NULL )
1100 msg_Err( p_this, "no capture device was detected" );
1104 /* Enumerate the devices */
1106 /* Note that if the Next() call succeeds but there are no monikers,
1107 * it will return S_FALSE (which is not a failure). Therefore, we check
1108 * that the return code is S_OK instead of using SUCCEEDED() macro. */
1110 while( p_class_enum->Next( 1, &p_moniker, &i_fetched ) == S_OK )
1112 /* Getting the property page to get the device name */
1113 IPropertyBag *p_bag;
1114 hr = p_moniker->BindToStorage( 0, 0, IID_IPropertyBag,
1120 hr = p_bag->Read( L"FriendlyName", &var, NULL );
1124 int i_convert = WideCharToMultiByte(CP_ACP, 0, var.bstrVal,
1125 SysStringLen(var.bstrVal), NULL, 0, NULL, NULL);
1126 char *p_buf = (char *)alloca( i_convert+1 ); p_buf[0] = 0;
1127 WideCharToMultiByte( CP_ACP, 0, var.bstrVal,
1128 SysStringLen(var.bstrVal), p_buf, i_convert, NULL, NULL );
1129 SysFreeString(var.bstrVal);
1130 p_buf[i_convert] = '\0';
1132 string devname = string(p_buf);
1135 /* find out if this name is already used by a previously found device */
1136 list<string>::const_iterator iter = devicelist.begin();
1137 list<string>::const_iterator end = devicelist.end();
1138 while ( iter != end )
1140 if( 0 == (*iter).compare(0, devname.size(), devname) )
1146 /* we have a duplicate device name, append a sequence number to name
1147 to provive a unique list back to the user */
1149 sprintf(seq, " #%d", dup);
1150 devname.append(seq);
1152 devicelist.push_back( devname );
1154 if( p_devicename && *p_devicename == devname )
1156 /* Bind Moniker to a filter object */
1157 hr = p_moniker->BindToObject( 0, 0, IID_IBaseFilter,
1158 (void **)&p_base_filter );
1161 msg_Err( p_this, "couldn't bind moniker to filter "
1162 "object (0x%lx)", hr );
1163 p_moniker->Release();
1164 p_class_enum->Release();
1167 p_moniker->Release();
1168 p_class_enum->Release();
1169 return p_base_filter;
1174 p_moniker->Release();
1177 p_class_enum->Release();
1179 if( p_listdevices ) {
1181 *p_listdevices = devicelist;
1186 static size_t EnumDeviceCaps( vlc_object_t *p_this, IBaseFilter *p_filter,
1187 int i_fourcc, int i_width, int i_height,
1188 int i_channels, int i_samplespersec,
1189 int i_bitspersample, AM_MEDIA_TYPE *mt,
1192 IEnumPins *p_enumpins;
1194 IEnumMediaTypes *p_enummt;
1195 size_t mt_count = 0;
1197 LONGLONG i_AvgTimePerFrame = 0;
1198 float r_fps = var_GetFloat( p_this, "dshow-fps" );
1200 i_AvgTimePerFrame = 10000000000LL/(LONGLONG)(r_fps*1000.0f);
1202 if( FAILED(p_filter->EnumPins( &p_enumpins )) )
1204 msg_Dbg( p_this, "EnumDeviceCaps failed: no pin enumeration !");
1208 while( S_OK == p_enumpins->Next( 1, &p_output_pin, NULL ) )
1212 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1214 msg_Dbg( p_this, "EnumDeviceCaps: %s pin: %S",
1215 info.dir == PINDIR_INPUT ? "input" : "output",
1217 if( info.pFilter ) info.pFilter->Release();
1220 p_output_pin->Release();
1223 p_enumpins->Reset();
1225 while( !mt_count && p_enumpins->Next( 1, &p_output_pin, NULL ) == S_OK )
1229 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1231 if( info.pFilter ) info.pFilter->Release();
1232 if( info.dir == PINDIR_INPUT )
1234 p_output_pin->Release();
1237 msg_Dbg( p_this, "EnumDeviceCaps: trying pin %S", info.achName );
1240 AM_MEDIA_TYPE *p_mt;
1243 ** Configure pin with a default compatible media if possible
1246 IAMStreamConfig *pSC;
1247 if( SUCCEEDED(p_output_pin->QueryInterface( IID_IAMStreamConfig,
1250 int piCount, piSize;
1251 if( SUCCEEDED(pSC->GetNumberOfCapabilities(&piCount, &piSize)) )
1253 BYTE *pSCC= (BYTE *)CoTaskMemAlloc(piSize);
1256 int i_priority = -1;
1257 for( int i=0; i<piCount; ++i )
1259 if( SUCCEEDED(pSC->GetStreamCaps(i, &p_mt, pSCC)) )
1261 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1262 int i_current_priority = GetFourCCPriority(i_current_fourcc);
1264 if( (i_fourcc && (i_current_fourcc != i_fourcc))
1265 || (i_priority > i_current_priority) )
1267 // unwanted chroma, try next media type
1268 FreeMediaType( *p_mt );
1269 CoTaskMemFree( (PVOID)p_mt );
1273 if( MEDIATYPE_Video == p_mt->majortype
1274 && FORMAT_VideoInfo == p_mt->formattype )
1276 VIDEO_STREAM_CONFIG_CAPS *pVSCC = reinterpret_cast<VIDEO_STREAM_CONFIG_CAPS*>(pSCC);
1277 VIDEOINFOHEADER *pVih = reinterpret_cast<VIDEOINFOHEADER*>(p_mt->pbFormat);
1279 if( i_AvgTimePerFrame )
1281 if( pVSCC->MinFrameInterval > i_AvgTimePerFrame
1282 || i_AvgTimePerFrame > pVSCC->MaxFrameInterval )
1284 // required frame rate not compatible, try next media type
1285 FreeMediaType( *p_mt );
1286 CoTaskMemFree( (PVOID)p_mt );
1289 pVih->AvgTimePerFrame = i_AvgTimePerFrame;
1294 if( i_width % pVSCC->OutputGranularityX
1295 || pVSCC->MinOutputSize.cx > i_width
1296 || i_width > pVSCC->MaxOutputSize.cx )
1298 // required width not compatible, try next media type
1299 FreeMediaType( *p_mt );
1300 CoTaskMemFree( (PVOID)p_mt );
1303 pVih->bmiHeader.biWidth = i_width;
1308 if( i_height % pVSCC->OutputGranularityY
1309 || pVSCC->MinOutputSize.cy > i_height
1310 || i_height > pVSCC->MaxOutputSize.cy )
1312 // required height not compatible, try next media type
1313 FreeMediaType( *p_mt );
1314 CoTaskMemFree( (PVOID)p_mt );
1317 pVih->bmiHeader.biHeight = i_height;
1320 // Set the sample size and image size.
1321 // (Round the image width up to a DWORD boundary.)
1322 p_mt->lSampleSize = pVih->bmiHeader.biSizeImage =
1323 ((pVih->bmiHeader.biWidth + 3) & ~3) *
1324 pVih->bmiHeader.biHeight * (pVih->bmiHeader.biBitCount>>3);
1326 // no cropping, use full video input buffer
1327 memset(&(pVih->rcSource), 0, sizeof(RECT));
1328 memset(&(pVih->rcTarget), 0, sizeof(RECT));
1330 // select this format as default
1331 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1333 i_priority = i_current_priority;
1335 // no need to check any more media types
1339 else if( p_mt->majortype == MEDIATYPE_Audio
1340 && p_mt->formattype == FORMAT_WaveFormatEx )
1342 AUDIO_STREAM_CONFIG_CAPS *pASCC = reinterpret_cast<AUDIO_STREAM_CONFIG_CAPS*>(pSCC);
1343 WAVEFORMATEX *pWfx = reinterpret_cast<WAVEFORMATEX*>(p_mt->pbFormat);
1345 if( i_current_fourcc && (WAVE_FORMAT_PCM == pWfx->wFormatTag) )
1347 int val = i_channels;
1351 if( val % pASCC->ChannelsGranularity
1352 || (unsigned int)val < pASCC->MinimumChannels
1353 || (unsigned int)val > pASCC->MaximumChannels )
1355 // required number channels not available, try next media type
1356 FreeMediaType( *p_mt );
1357 CoTaskMemFree( (PVOID)p_mt );
1360 pWfx->nChannels = val;
1362 val = i_samplespersec;
1366 if( val % pASCC->SampleFrequencyGranularity
1367 || (unsigned int)val < pASCC->MinimumSampleFrequency
1368 || (unsigned int)val > pASCC->MaximumSampleFrequency )
1370 // required sampling rate not available, try next media type
1371 FreeMediaType( *p_mt );
1372 CoTaskMemFree( (PVOID)p_mt );
1375 pWfx->nSamplesPerSec = val;
1377 val = i_bitspersample;
1380 if( VLC_FOURCC('f', 'l', '3', '2') == i_current_fourcc )
1386 if( val % pASCC->BitsPerSampleGranularity
1387 || (unsigned int)val < pASCC->MinimumBitsPerSample
1388 || (unsigned int)val > pASCC->MaximumBitsPerSample )
1390 // required sample size not available, try next media type
1391 FreeMediaType( *p_mt );
1392 CoTaskMemFree( (PVOID)p_mt );
1396 pWfx->wBitsPerSample = val;
1397 pWfx->nBlockAlign = (pWfx->wBitsPerSample * pWfx->nChannels)/8;
1398 pWfx->nAvgBytesPerSec = pWfx->nSamplesPerSec * pWfx->nBlockAlign;
1400 // select this format as default
1401 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1403 i_priority = i_current_priority;
1407 FreeMediaType( *p_mt );
1408 CoTaskMemFree( (PVOID)p_mt );
1411 CoTaskMemFree( (LPVOID)pSCC );
1412 if( i_priority >= 0 )
1413 msg_Dbg( p_this, "EnumDeviceCaps: input pin default format configured");
1420 ** Probe pin for available medias (may be a previously configured one)
1423 if( FAILED( p_output_pin->EnumMediaTypes( &p_enummt ) ) )
1425 p_output_pin->Release();
1429 while( p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1431 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1432 if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Video
1433 && p_mt->formattype == FORMAT_VideoInfo )
1435 int i_current_width = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1436 int i_current_height = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1437 LONGLONG i_current_atpf = ((VIDEOINFOHEADER *)p_mt->pbFormat)->AvgTimePerFrame;
1439 if( i_current_height < 0 )
1440 i_current_height = -i_current_height;
1442 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1443 "accepts chroma: %4.4s, width:%i, height:%i, fps:%f",
1444 (char *)&i_current_fourcc, i_current_width,
1445 i_current_height, (10000000.0f/((float)i_current_atpf)) );
1447 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1448 ( !i_width || i_width == i_current_width ) &&
1449 ( !i_height || i_height == i_current_height ) &&
1450 ( !i_AvgTimePerFrame || i_AvgTimePerFrame == i_current_atpf ) &&
1454 mt[mt_count++] = *p_mt;
1456 else FreeMediaType( *p_mt );
1458 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Audio
1459 && p_mt->formattype == FORMAT_WaveFormatEx)
1461 int i_current_channels =
1462 ((WAVEFORMATEX *)p_mt->pbFormat)->nChannels;
1463 int i_current_samplespersec =
1464 ((WAVEFORMATEX *)p_mt->pbFormat)->nSamplesPerSec;
1465 int i_current_bitspersample =
1466 ((WAVEFORMATEX *)p_mt->pbFormat)->wBitsPerSample;
1468 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1469 "accepts format: %4.4s, channels:%i, "
1470 "samples/sec:%i bits/sample:%i",
1471 (char *)&i_current_fourcc, i_current_channels,
1472 i_current_samplespersec, i_current_bitspersample);
1474 if( (!i_channels || i_channels == i_current_channels) &&
1475 (!i_samplespersec ||
1476 i_samplespersec == i_current_samplespersec) &&
1477 (!i_bitspersample ||
1478 i_bitspersample == i_current_bitspersample) &&
1482 mt[mt_count++] = *p_mt;
1484 /* Setup a few properties like the audio latency */
1485 IAMBufferNegotiation *p_ambuf;
1486 if( SUCCEEDED( p_output_pin->QueryInterface(
1487 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
1489 ALLOCATOR_PROPERTIES AllocProp;
1490 AllocProp.cbAlign = -1;
1492 /* 100 ms of latency */
1493 AllocProp.cbBuffer = i_current_channels *
1494 i_current_samplespersec *
1495 i_current_bitspersample / 8 / 10;
1497 AllocProp.cbPrefix = -1;
1498 AllocProp.cBuffers = -1;
1499 p_ambuf->SuggestAllocatorProperties( &AllocProp );
1503 else FreeMediaType( *p_mt );
1505 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Stream )
1507 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1508 "accepts stream format: %4.4s",
1509 (char *)&i_current_fourcc );
1511 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1515 mt[mt_count++] = *p_mt;
1516 i_fourcc = i_current_fourcc;
1518 else FreeMediaType( *p_mt );
1522 char *psz_type = "unknown";
1523 if( p_mt->majortype == MEDIATYPE_Video ) psz_type = "video";
1524 if( p_mt->majortype == MEDIATYPE_Audio ) psz_type = "audio";
1525 if( p_mt->majortype == MEDIATYPE_Stream ) psz_type = "stream";
1526 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: unsupported format "
1527 "(%s %4.4s)", psz_type, (char *)&p_mt->subtype );
1529 FreeMediaType( *p_mt );
1531 CoTaskMemFree( (PVOID)p_mt );
1534 if( !mt_count && p_enummt->Reset() == S_OK )
1536 // VLC did not find any supported MEDIATYPE for this output pin.
1537 // However the graph builder might insert converter filters in
1538 // the graph if we use a different codec in VLC filter input pin.
1539 // however, in order to avoid nasty surprises, make use of this
1540 // facility only for known unsupported codecs.
1542 while( !mt_count && p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1544 // the first four bytes of subtype GUID contains the codec FOURCC
1545 const char *pfcc = (char *)&p_mt->subtype;
1546 int i_current_fourcc = VLC_FOURCC(pfcc[0], pfcc[1], pfcc[2], pfcc[3]);
1547 if( VLC_FOURCC('H','C','W','2') == i_current_fourcc
1548 && p_mt->majortype == MEDIATYPE_Video )
1550 // output format for 'Hauppauge WinTV PVR PCI II Capture'
1551 // try I420 as an input format
1552 i_current_fourcc = VLC_FOURCC('I','4','2','0');
1553 if( !i_fourcc || i_fourcc == i_current_fourcc )
1555 // return alternative media type
1559 mtr.majortype = MEDIATYPE_Video;
1560 mtr.subtype = MEDIASUBTYPE_I420;
1561 mtr.bFixedSizeSamples = TRUE;
1562 mtr.bTemporalCompression = FALSE;
1564 mtr.formattype = FORMAT_VideoInfo;
1565 mtr.cbFormat = sizeof(vh);
1566 mtr.pbFormat = (BYTE *)&vh;
1568 memset(&vh, 0, sizeof(vh));
1570 vh.bmiHeader.biSize = sizeof(vh.bmiHeader);
1571 vh.bmiHeader.biWidth = i_width > 0 ? i_width :
1572 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1573 vh.bmiHeader.biHeight = i_height > 0 ? i_height :
1574 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1575 vh.bmiHeader.biPlanes = 3;
1576 vh.bmiHeader.biBitCount = 12;
1577 vh.bmiHeader.biCompression = VLC_FOURCC('I','4','2','0');
1578 vh.bmiHeader.biSizeImage = vh.bmiHeader.biWidth * 12 *
1579 vh.bmiHeader.biHeight / 8;
1580 mtr.lSampleSize = vh.bmiHeader.biSizeImage;
1582 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: using 'I420' in place of unsupported format 'HCW2'");
1584 if( SUCCEEDED(CopyMediaType(mt+mt_count, &mtr)) )
1588 FreeMediaType( *p_mt );
1592 p_enummt->Release();
1593 p_output_pin->Release();
1596 p_enumpins->Release();
1600 /*****************************************************************************
1601 * ReadCompressed: reads compressed (MPEG/DV) data from the device.
1602 *****************************************************************************
1603 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1605 *****************************************************************************/
1606 static block_t *ReadCompressed( access_t *p_access )
1608 access_sys_t *p_sys = p_access->p_sys;
1609 dshow_stream_t *p_stream = NULL;
1610 VLCMediaSample sample;
1612 /* Read 1 DV/MPEG frame (they contain the video and audio data) */
1614 /* There must be only 1 elementary stream to produce a valid stream
1615 * of MPEG or DV data */
1616 p_stream = p_sys->pp_streams[0];
1620 if( p_access->b_die || p_access->b_error ) return 0;
1622 /* Get new sample/frame from the elementary stream (blocking). */
1623 vlc_mutex_lock( &p_sys->lock );
1625 if( p_stream->p_capture_filter->CustomGetPin()
1626 ->CustomGetSample( &sample ) != S_OK )
1628 /* No data available. Wait until some data has arrived */
1629 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1630 vlc_mutex_unlock( &p_sys->lock );
1634 vlc_mutex_unlock( &p_sys->lock );
1641 int i_data_size = sample.p_sample->GetActualDataLength();
1643 if( !i_data_size || !(p_block = block_New( p_access, i_data_size )) )
1645 sample.p_sample->Release();
1649 sample.p_sample->GetPointer( &p_data );
1650 vlc_memcpy( p_block->p_buffer, p_data, i_data_size );
1651 sample.p_sample->Release();
1653 /* The caller got what he wanted */
1657 return 0; /* never reached */
1660 /****************************************************************************
1662 ****************************************************************************/
1663 static int Demux( demux_t *p_demux )
1665 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
1666 dshow_stream_t *p_stream = NULL;
1667 VLCMediaSample sample;
1668 int i_data_size, i_stream;
1672 vlc_mutex_lock( &p_sys->lock );
1674 /* Try to grab an audio sample (audio has a higher priority) */
1675 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1677 p_stream = p_sys->pp_streams[i_stream];
1678 if( p_stream->mt.majortype == MEDIATYPE_Audio &&
1679 p_stream->p_capture_filter &&
1680 p_stream->p_capture_filter->CustomGetPin()
1681 ->CustomGetSample( &sample ) == S_OK )
1686 /* Try to grab a video sample */
1687 if( i_stream == p_sys->i_streams )
1689 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1691 p_stream = p_sys->pp_streams[i_stream];
1692 if( p_stream->p_capture_filter &&
1693 p_stream->p_capture_filter->CustomGetPin()
1694 ->CustomGetSample( &sample ) == S_OK )
1701 vlc_mutex_unlock( &p_sys->lock );
1703 if( i_stream == p_sys->i_streams )
1705 /* Sleep so we do not consume all the cpu, 10ms seems
1706 * like a good value (100fps) */
1714 i_data_size = sample.p_sample->GetActualDataLength();
1715 sample.p_sample->GetPointer( &p_data );
1717 REFERENCE_TIME i_pts, i_end_date;
1718 HRESULT hr = sample.p_sample->GetTime( &i_pts, &i_end_date );
1719 if( hr != VFW_S_NO_STOP_TIME && hr != S_OK ) i_pts = 0;
1723 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
1725 /* Use our data timestamp */
1726 i_pts = sample.i_timestamp;
1727 p_stream->b_pts = true;
1731 i_pts /= 10; /* Dshow works with 100 nano-seconds resolution */
1734 msg_Dbg( p_demux, "Read() stream: %i, size: %i, PTS: %"PRId64,
1735 i_stream, i_data_size, i_pts );
1738 p_block = block_New( p_demux, i_data_size );
1739 vlc_memcpy( p_block->p_buffer, p_data, i_data_size );
1740 p_block->i_pts = p_block->i_dts = i_pts;
1741 sample.p_sample->Release();
1743 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pts > 0 ? i_pts : 0 );
1744 es_out_Send( p_demux->out, p_stream->p_es, p_block );
1749 /*****************************************************************************
1751 *****************************************************************************/
1752 static int AccessControl( access_t *p_access, int i_query, va_list args )
1761 case ACCESS_CAN_SEEK:
1762 case ACCESS_CAN_FASTSEEK:
1763 case ACCESS_CAN_PAUSE:
1764 case ACCESS_CAN_CONTROL_PACE:
1765 pb_bool = (bool*)va_arg( args, bool* );
1770 case ACCESS_GET_MTU:
1771 pi_int = (int*)va_arg( args, int * );
1775 case ACCESS_GET_PTS_DELAY:
1776 pi_64 = (int64_t*)va_arg( args, int64_t * );
1777 *pi_64 = (int64_t)var_GetInteger( p_access, "dshow-caching" ) * 1000;
1781 case ACCESS_SET_PAUSE_STATE:
1782 case ACCESS_GET_TITLE_INFO:
1783 case ACCESS_SET_TITLE:
1784 case ACCESS_SET_SEEKPOINT:
1785 case ACCESS_SET_PRIVATE_ID_STATE:
1786 return VLC_EGENERIC;
1789 msg_Warn( p_access, "unimplemented query in control" );
1790 return VLC_EGENERIC;
1796 /****************************************************************************
1798 ****************************************************************************/
1799 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1806 /* Special for access_demux */
1807 case DEMUX_CAN_PAUSE:
1808 case DEMUX_CAN_SEEK:
1809 case DEMUX_SET_PAUSE_STATE:
1810 case DEMUX_CAN_CONTROL_PACE:
1811 pb = (bool*)va_arg( args, bool * );
1815 case DEMUX_GET_PTS_DELAY:
1816 pi64 = (int64_t*)va_arg( args, int64_t * );
1817 *pi64 = (int64_t)var_GetInteger( p_demux, "dshow-caching" ) * 1000;
1820 case DEMUX_GET_TIME:
1821 pi64 = (int64_t*)va_arg( args, int64_t * );
1825 /* TODO implement others */
1827 return VLC_EGENERIC;
1830 return VLC_EGENERIC;
1833 /*****************************************************************************
1834 * config variable callback
1835 *****************************************************************************/
1836 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1837 vlc_value_t newval, vlc_value_t oldval, void * )
1839 module_config_t *p_item;
1840 bool b_audio = false;
1843 p_item = config_FindConfig( p_this, psz_name );
1844 if( !p_item ) return VLC_SUCCESS;
1846 if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = true;
1848 /* Clear-up the current list */
1849 if( p_item->i_list )
1851 /* Keep the 2 first entries */
1852 for( i = 2; i < p_item->i_list; i++ )
1854 free( const_cast<char *>(p_item->ppsz_list[i]) );
1855 free( const_cast<char *>(p_item->ppsz_list_text[i]) );
1857 /* TODO: Remove when no more needed */
1858 p_item->ppsz_list[i] = NULL;
1859 p_item->ppsz_list_text[i] = NULL;
1863 /* Find list of devices */
1864 list<string> list_devices;
1866 /* Initialize OLE/COM */
1869 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1871 /* Uninitialize OLE/COM */
1874 if( !list_devices.size() ) return VLC_SUCCESS;
1877 (char **)realloc( p_item->ppsz_list,
1878 (list_devices.size()+3) * sizeof(char *) );
1879 p_item->ppsz_list_text =
1880 (char **)realloc( p_item->ppsz_list_text,
1881 (list_devices.size()+3) * sizeof(char *) );
1883 list<string>::iterator iter;
1884 for( iter = list_devices.begin(), i = 2; iter != list_devices.end();
1887 p_item->ppsz_list[i] = strdup( iter->c_str() );
1888 p_item->ppsz_list_text[i] = NULL;
1891 p_item->ppsz_list[i] = NULL;
1892 p_item->ppsz_list_text[i] = NULL;
1894 /* Signal change to the interface */
1895 p_item->b_dirty = true;
1900 static int ConfigDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1901 vlc_value_t newval, vlc_value_t oldval, void * )
1903 module_config_t *p_item;
1904 bool b_audio = false;
1906 /* Initialize OLE/COM */
1909 p_item = config_FindConfig( p_this, psz_name );
1910 if( !p_item ) return VLC_SUCCESS;
1912 if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = true;
1916 if( newval.psz_string && *newval.psz_string )
1918 devicename = newval.psz_string;
1922 /* If no device name was specified, pick the 1st one */
1923 list<string> list_devices;
1925 /* Enumerate devices */
1926 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1927 if( !list_devices.size() ) return VLC_EGENERIC;
1928 devicename = *list_devices.begin();
1931 IBaseFilter *p_device_filter =
1932 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
1933 if( p_device_filter )
1935 ShowPropertyPage( p_device_filter );
1936 p_device_filter->Release();
1940 /* Uninitialize OLE/COM */
1943 msg_Err( p_this, "didn't find device: %s", devicename.c_str() );
1944 return VLC_EGENERIC;
1947 /* Uninitialize OLE/COM */
1953 /*****************************************************************************
1955 *****************************************************************************/
1956 static void ShowPropertyPage( IUnknown *obj )
1958 ISpecifyPropertyPages *p_spec;
1961 HRESULT hr = obj->QueryInterface( IID_ISpecifyPropertyPages,
1963 if( FAILED(hr) ) return;
1965 if( SUCCEEDED(p_spec->GetPages( &cauuid )) )
1967 if( cauuid.cElems > 0 )
1969 HWND hwnd_desktop = ::GetDesktopWindow();
1971 OleCreatePropertyFrame( hwnd_desktop, 30, 30, NULL, 1, &obj,
1972 cauuid.cElems, cauuid.pElems, 0, 0, NULL );
1974 CoTaskMemFree( cauuid.pElems );
1980 static void ShowDeviceProperties( vlc_object_t *p_this,
1981 ICaptureGraphBuilder2 *p_graph,
1982 IBaseFilter *p_device_filter,
1986 msg_Dbg( p_this, "configuring Device Properties" );
1989 * Video or audio capture filter page
1991 ShowPropertyPage( p_device_filter );
1996 if( p_graph && b_audio )
1998 IAMStreamConfig *p_SC;
2000 msg_Dbg( p_this, "showing WDM Audio Configuration Pages" );
2002 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2003 &MEDIATYPE_Audio, p_device_filter,
2004 IID_IAMStreamConfig, (void **)&p_SC );
2007 ShowPropertyPage(p_SC);
2015 HRESULT hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2016 &MEDIATYPE_Audio, p_device_filter,
2017 IID_IAMTVAudio, (void **)&p_TVA );
2020 ShowPropertyPage(p_TVA);
2028 if( p_graph && !b_audio )
2030 IAMStreamConfig *p_SC;
2032 msg_Dbg( p_this, "showing WDM Video Configuration Pages" );
2034 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2035 &MEDIATYPE_Interleaved, p_device_filter,
2036 IID_IAMStreamConfig, (void **)&p_SC );
2039 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2040 &MEDIATYPE_Video, p_device_filter,
2041 IID_IAMStreamConfig, (void **)&p_SC );
2046 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2047 &MEDIATYPE_Stream, p_device_filter,
2048 IID_IAMStreamConfig, (void **)&p_SC );
2053 ShowPropertyPage(p_SC);
2059 static void ShowTunerProperties( vlc_object_t *p_this,
2060 ICaptureGraphBuilder2 *p_graph,
2061 IBaseFilter *p_device_filter,
2065 msg_Dbg( p_this, "configuring Tuner Properties" );
2067 if( !p_graph || b_audio ) return;
2070 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2071 &MEDIATYPE_Interleaved, p_device_filter,
2072 IID_IAMTVTuner, (void **)&p_TV );
2075 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2076 &MEDIATYPE_Video, p_device_filter,
2077 IID_IAMTVTuner, (void **)&p_TV );
2082 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2083 &MEDIATYPE_Stream, p_device_filter,
2084 IID_IAMTVTuner, (void **)&p_TV );
2089 ShowPropertyPage(p_TV);
2094 static void ConfigTuner( vlc_object_t *p_this, ICaptureGraphBuilder2 *p_graph,
2095 IBaseFilter *p_device_filter )
2097 int i_channel, i_country, i_input, i_amtuner_mode;
2102 if( !p_graph ) return;
2104 i_channel = var_GetInteger( p_this, "dshow-tuner-channel" );
2105 i_country = var_GetInteger( p_this, "dshow-tuner-country" );
2106 i_input = var_GetInteger( p_this, "dshow-tuner-input" );
2107 i_amtuner_mode = var_GetInteger( p_this, "dshow-amtuner-mode" );
2109 if( !i_channel && !i_country && !i_input ) return; /* Nothing to do */
2111 msg_Dbg( p_this, "tuner config: channel %i, country %i, input type %i",
2112 i_channel, i_country, i_input );
2114 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Interleaved,
2115 p_device_filter, IID_IAMTVTuner,
2119 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
2120 p_device_filter, IID_IAMTVTuner,
2126 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Stream,
2127 p_device_filter, IID_IAMTVTuner,
2133 msg_Dbg( p_this, "couldn't find tuner interface" );
2137 hr = p_TV->GetAvailableModes( &l_modes );
2138 if( SUCCEEDED(hr) && (l_modes & i_amtuner_mode) )
2140 hr = p_TV->put_Mode( (AMTunerModeType)i_amtuner_mode );
2143 if( i_input == 1 ) p_TV->put_InputType( 0, TunerInputCable );
2144 else if( i_input == 2 ) p_TV->put_InputType( 0, TunerInputAntenna );
2146 p_TV->put_CountryCode( i_country );
2147 p_TV->put_Channel( i_channel, AMTUNER_SUBCHAN_NO_TUNE,
2148 AMTUNER_SUBCHAN_NO_TUNE );