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 #include <vlc/input.h>
35 #include <vlc_interaction.h>
40 /*****************************************************************************
41 * Access: local prototypes
42 *****************************************************************************/
43 static block_t *ReadCompressed( access_t * );
44 static int AccessControl ( access_t *, int, va_list );
46 static int Demux ( demux_t * );
47 static int DemuxControl( demux_t *, int, va_list );
49 static int OpenDevice( vlc_object_t *, access_sys_t *, string, vlc_bool_t );
50 static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
51 list<string> *, vlc_bool_t );
52 static size_t EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
53 int, int, int, int, int, int,
54 AM_MEDIA_TYPE *mt, size_t );
55 static bool ConnectFilters( vlc_object_t *, access_sys_t *,
56 IBaseFilter *, CaptureFilter * );
57 static int FindDevicesCallback( vlc_object_t *, char const *,
58 vlc_value_t, vlc_value_t, void * );
59 static int ConfigDevicesCallback( vlc_object_t *, char const *,
60 vlc_value_t, vlc_value_t, void * );
62 static void ShowPropertyPage( IUnknown * );
63 static void ShowDeviceProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
64 IBaseFilter *, vlc_bool_t );
65 static void ShowTunerProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
66 IBaseFilter *, vlc_bool_t );
67 static void ConfigTuner( vlc_object_t *, ICaptureGraphBuilder2 *,
70 /*****************************************************************************
72 *****************************************************************************/
73 static const char *ppsz_vdev[] = { "", "none" };
74 static const char *ppsz_vdev_text[] = { N_("Default"), N_("None") };
75 static const char *ppsz_adev[] = { "", "none" };
76 static const char *ppsz_adev_text[] = { N_("Default"), N_("None") };
77 static int pi_tuner_input[] = { 0, 1, 2 };
78 static const char *ppsz_tuner_input_text[] =
79 {N_("Default"), N_("Cable"), N_("Antenna")};
80 static int pi_amtuner_mode[] = { AMTUNER_MODE_DEFAULT,
82 AMTUNER_MODE_FM_RADIO,
83 AMTUNER_MODE_AM_RADIO,
85 static char *ppsz_amtuner_mode_text[] = { N_("Default"),
91 #define CACHING_TEXT N_("Caching value in ms")
92 #define CACHING_LONGTEXT N_( \
93 "Caching value for DirectShow streams. " \
94 "This value should be set in millisecondss." )
95 #define VDEV_TEXT N_("Video device name")
96 #define VDEV_LONGTEXT N_( \
97 "Name of the video device that will be used by the " \
98 "DirectShow plugin. If you don't specify anything, the default device " \
100 /// \bug [String] size stuff should be on video !
101 #define ADEV_TEXT N_("Audio device name")
102 #define ADEV_LONGTEXT N_( \
103 "Name of the audio device that will be used by the " \
104 "DirectShow plugin. If you don't specify anything, the default device " \
105 "will be used. You can specify a standard size (cif, d1, ...) or <width>x<height>")
106 #define SIZE_TEXT N_("Video size")
107 #define SIZE_LONGTEXT N_( \
108 "Size of the video that will be displayed by the " \
109 "DirectShow plugin. If you don't specify anything the default size for " \
110 "your device will be used.")
111 #define CHROMA_TEXT N_("Video input chroma format")
112 #define CHROMA_LONGTEXT N_( \
113 "Force the DirectShow video input to use a specific chroma format " \
114 "(eg. I420 (default), RV24, etc.)")
115 #define FPS_TEXT N_("Video input frame rate")
116 #define FPS_LONGTEXT N_( \
117 "Force the DirectShow video input to use a specific frame rate" \
118 "(eg. 0 means default, 25, 29.97, 50, 59.94, etc.)")
119 #define CONFIG_TEXT N_("Device properties")
120 #define CONFIG_LONGTEXT N_( \
121 "Show the properties dialog of the selected device before starting the " \
123 #define TUNER_TEXT N_("Tuner properties")
124 #define TUNER_LONGTEXT N_( \
125 "Show the tuner properties [channel selection] page." )
126 #define CHANNEL_TEXT N_("Tuner TV Channel")
127 #define CHANNEL_LONGTEXT N_( \
128 "Set the TV channel the tuner will set to " \
129 "(0 means default)." )
130 #define COUNTRY_TEXT N_("Tuner country code")
131 #define COUNTRY_LONGTEXT N_( \
132 "Set the tuner country code that establishes the current " \
133 "channel-to-frequency mapping (0 means default)." )
134 #define TUNER_INPUT_TEXT N_("Tuner input type")
135 #define TUNER_INPUT_LONGTEXT N_( \
136 "Select the tuner input type (Cable/Antenna)." )
137 #define VIDEO_IN_TEXT N_("Video input pin")
138 #define VIDEO_IN_LONGTEXT N_( \
139 "Select the video input source, such as composite, s-video, " \
140 "or tuner. Since these settings are hardware-specfic, you should find good " \
141 "settings in the \"Device config\" area, and use those numbers here. -1 " \
142 "means that settings will not be changed.")
143 #define AUDIO_IN_TEXT N_("Audio input pin")
144 #define AUDIO_IN_LONGTEXT N_( \
145 "Select the audio input source. See the \"video input\" option." )
146 #define VIDEO_OUT_TEXT N_("Video output pin")
147 #define VIDEO_OUT_LONGTEXT N_( \
148 "Select the video output type. See the \"video input\" option." )
149 #define AUDIO_OUT_TEXT N_("Audio output pin")
150 #define AUDIO_OUT_LONGTEXT N_( \
151 "Select the audio output type. See the \"video input\" option." )
153 #define AMTUNER_MODE_TEXT N_("AM Tuner mode")
154 #define AMTUNER_MODE_LONGTEXT N_( \
155 "AM Tuner mode. Can be one of DEFAULT, TV, AM_RADIO, FM_RADIO or DSS.")
157 static int CommonOpen ( vlc_object_t *, access_sys_t *, vlc_bool_t );
158 static void CommonClose( vlc_object_t *, access_sys_t * );
160 static int AccessOpen ( vlc_object_t * );
161 static void AccessClose( vlc_object_t * );
163 static int DemuxOpen ( vlc_object_t * );
164 static void DemuxClose ( vlc_object_t * );
167 set_shortname( _("DirectShow") );
168 set_description( _("DirectShow input") );
169 set_category( CAT_INPUT );
170 set_subcategory( SUBCAT_INPUT_ACCESS );
171 add_integer( "dshow-caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
172 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
174 add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, VLC_FALSE);
175 change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback );
176 change_action_add( FindDevicesCallback, N_("Refresh list") );
177 change_action_add( ConfigDevicesCallback, N_("Configure") );
179 add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE);
180 change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback );
181 change_action_add( FindDevicesCallback, N_("Refresh list") );
182 change_action_add( ConfigDevicesCallback, N_("Configure") );
184 add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE);
186 add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
189 add_float( "dshow-fps", 0.0f, NULL, FPS_TEXT, FPS_LONGTEXT,
192 add_bool( "dshow-config", VLC_FALSE, NULL, CONFIG_TEXT, CONFIG_LONGTEXT,
195 add_bool( "dshow-tuner", VLC_FALSE, NULL, TUNER_TEXT, TUNER_LONGTEXT,
198 add_integer( "dshow-tuner-channel", 0, NULL, CHANNEL_TEXT,
199 CHANNEL_LONGTEXT, VLC_TRUE );
201 add_integer( "dshow-tuner-country", 0, NULL, COUNTRY_TEXT,
202 COUNTRY_LONGTEXT, VLC_TRUE );
204 add_integer( "dshow-tuner-input", 0, NULL, TUNER_INPUT_TEXT,
205 TUNER_INPUT_LONGTEXT, VLC_TRUE );
206 change_integer_list( pi_tuner_input, ppsz_tuner_input_text, 0 );
208 add_integer( "dshow-video-input", -1, NULL, VIDEO_IN_TEXT,
209 VIDEO_IN_LONGTEXT, VLC_TRUE );
211 add_integer( "dshow-audio-input", -1, NULL, AUDIO_IN_TEXT,
212 AUDIO_IN_LONGTEXT, VLC_TRUE );
214 add_integer( "dshow-video-output", -1, NULL, VIDEO_OUT_TEXT,
215 VIDEO_OUT_LONGTEXT, VLC_TRUE );
217 add_integer( "dshow-audio-output", -1, NULL, AUDIO_OUT_TEXT,
218 AUDIO_OUT_LONGTEXT, VLC_TRUE );
220 add_integer( "dshow-amtuner-mode", AMTUNER_MODE_TV, NULL,
221 AMTUNER_MODE_TEXT, AMTUNER_MODE_LONGTEXT, VLC_FALSE);
222 change_integer_list( pi_amtuner_mode, ppsz_amtuner_mode_text, 0 );
224 add_shortcut( "dshow" );
225 set_capability( "access_demux", 0 );
226 set_callbacks( DemuxOpen, DemuxClose );
229 set_description( _("DirectShow input") );
230 add_shortcut( "dshow" );
231 set_capability( "access2", 0 );
232 set_callbacks( AccessOpen, AccessClose );
236 /*****************************************************************************
237 * DirectShow elementary stream descriptor
238 *****************************************************************************/
239 typedef struct dshow_stream_t
242 IBaseFilter *p_device_filter;
243 CaptureFilter *p_capture_filter;
248 VIDEOINFOHEADER video;
259 /*****************************************************************************
260 * DirectShow utility functions
261 *****************************************************************************/
262 static void CreateDirectShowGraph( access_sys_t *p_sys )
264 p_sys->i_crossbar_route_depth = 0;
266 /* Create directshow filter graph */
267 if( SUCCEEDED( CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
268 (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph) ) )
270 /* Create directshow capture graph builder if available */
271 if( SUCCEEDED( CoCreateInstance( CLSID_CaptureGraphBuilder2, 0,
272 CLSCTX_INPROC, (REFIID)IID_ICaptureGraphBuilder2,
273 (void **)&p_sys->p_capture_graph_builder2 ) ) )
275 p_sys->p_capture_graph_builder2->
276 SetFiltergraph((IGraphBuilder *)p_sys->p_graph);
279 p_sys->p_graph->QueryInterface( IID_IMediaControl,
280 (void **)&p_sys->p_control );
284 static void DeleteDirectShowGraph( access_sys_t *p_sys )
286 DeleteCrossbarRoutes( p_sys );
288 /* Remove filters from graph */
289 for( int i = 0; i < p_sys->i_streams; i++ )
291 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_capture_filter );
292 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_device_filter );
293 p_sys->pp_streams[i]->p_capture_filter->Release();
294 p_sys->pp_streams[i]->p_device_filter->Release();
297 /* Release directshow objects */
298 if( p_sys->p_control )
300 p_sys->p_control->Release();
301 p_sys->p_control = NULL;
303 if( p_sys->p_capture_graph_builder2 )
305 p_sys->p_capture_graph_builder2->Release();
306 p_sys->p_capture_graph_builder2 = NULL;
311 p_sys->p_graph->Release();
312 p_sys->p_graph = NULL;
316 /*****************************************************************************
317 * CommonOpen: open direct show device
318 *****************************************************************************/
319 static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
320 vlc_bool_t b_access_demux )
325 /* Get/parse options and open device(s) */
326 string vdevname, adevname;
327 int i_width = 0, i_height = 0, i_chroma = 0;
328 vlc_bool_t b_audio = VLC_TRUE;
330 var_Create( p_this, "dshow-config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
331 var_Create( p_this, "dshow-tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
332 var_Create( p_this, "dshow-vdev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
333 var_Get( p_this, "dshow-vdev", &val );
334 if( val.psz_string ) vdevname = string( val.psz_string );
335 if( val.psz_string ) free( val.psz_string );
337 var_Create( p_this, "dshow-adev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
338 var_Get( p_this, "dshow-adev", &val );
339 if( val.psz_string ) adevname = string( val.psz_string );
340 if( val.psz_string ) free( val.psz_string );
342 static struct {char *psz_size; int i_width; int i_height;} size_table[] =
343 { { "subqcif", 128, 96 }, { "qsif", 160, 120 }, { "qcif", 176, 144 },
344 { "sif", 320, 240 }, { "cif", 352, 288 }, { "d1", 640, 480 },
348 var_Create( p_this, "dshow-size", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
349 var_Get( p_this, "dshow-size", &val );
350 if( val.psz_string && *val.psz_string )
352 for( i = 0; size_table[i].psz_size; i++ )
354 if( !strcmp( val.psz_string, size_table[i].psz_size ) )
356 i_width = size_table[i].i_width;
357 i_height = size_table[i].i_height;
361 if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
364 i_width = strtol( val.psz_string, &psz_parser, 0 );
365 if( *psz_parser == 'x' || *psz_parser == 'X')
367 i_height = strtol( psz_parser + 1, &psz_parser, 0 );
369 msg_Dbg( p_this, "width x height %dx%d", i_width, i_height );
372 if( val.psz_string ) free( val.psz_string );
374 p_sys->b_chroma = VLC_FALSE;
375 var_Create( p_this, "dshow-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
376 var_Get( p_this, "dshow-chroma", &val );
377 if( val.psz_string && strlen( val.psz_string ) >= 4 )
379 i_chroma = VLC_FOURCC( val.psz_string[0], val.psz_string[1],
380 val.psz_string[2], val.psz_string[3] );
381 p_sys->b_chroma = VLC_TRUE;
383 if( val.psz_string ) free( val.psz_string );
385 var_Create( p_this, "dshow-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
386 var_Create( p_this, "dshow-tuner-channel",
387 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
388 var_Create( p_this, "dshow-tuner-country",
389 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
390 var_Create( p_this, "dshow-tuner-input",
391 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
393 var_Create( p_this, "dshow-amtuner-mode",
394 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
396 var_Create( p_this, "dshow-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
398 var_Create( p_this, "dshow-video-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
399 var_Create( p_this, "dshow-audio-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
400 var_Create( p_this, "dshow-video-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
401 var_Create( p_this, "dshow-audio-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
403 /* Initialize OLE/COM */
406 /* Initialize some data */
407 p_sys->i_streams = 0;
408 p_sys->pp_streams = 0;
409 p_sys->i_width = i_width;
410 p_sys->i_height = i_height;
411 p_sys->i_chroma = i_chroma;
413 p_sys->p_graph = NULL;
414 p_sys->p_capture_graph_builder2 = NULL;
415 p_sys->p_control = NULL;
417 vlc_mutex_init( p_this, &p_sys->lock );
418 vlc_cond_init( p_this, &p_sys->wait );
420 /* Build directshow graph */
421 CreateDirectShowGraph( p_sys );
423 if( OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
425 msg_Err( p_this, "can't open video");
429 /* Check if we can handle the demuxing ourselves or need to spawn
430 * a demuxer module */
431 dshow_stream_t *p_stream = p_sys->pp_streams[p_sys->i_streams-1];
433 if( p_stream->mt.majortype == MEDIATYPE_Video )
435 if( /* Raw DV stream */
436 p_stream->i_fourcc == VLC_FOURCC('d','v','s','l') ||
437 p_stream->i_fourcc == VLC_FOURCC('d','v','s','d') ||
438 p_stream->i_fourcc == VLC_FOURCC('d','v','h','d') ||
439 /* Raw MPEG video stream */
440 p_stream->i_fourcc == VLC_FOURCC('m','p','2','v') )
446 /* Let the access (only) take care of that */
452 if( p_stream->mt.majortype == MEDIATYPE_Stream )
458 /* Let the access (only) take care of that */
462 var_Get( p_this, "dshow-tuner", &val );
465 /* FIXME: we do MEDIATYPE_Stream here so we don't do
467 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
468 p_stream->p_device_filter, 0 );
473 if( b_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
475 msg_Err( p_this, "can't open audio");
478 for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
480 var_Get( p_this, "dshow-video-input", &val );
482 p_sys->crossbar_routes[i].VideoInputIndex=val.i_int;
483 var_Get( p_this, "dshow-video-output", &val );
485 p_sys->crossbar_routes[i].VideoOutputIndex=val.i_int;
486 var_Get( p_this, "dshow-audio-input", &val );
488 p_sys->crossbar_routes[i].AudioInputIndex=val.i_int;
489 var_Get( p_this, "dshow-audio-output", &val );
491 p_sys->crossbar_routes[i].AudioOutputIndex=val.i_int;
493 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
494 LONG VideoInputIndex = p_sys->crossbar_routes[i].VideoInputIndex;
495 LONG VideoOutputIndex = p_sys->crossbar_routes[i].VideoOutputIndex;
496 LONG AudioInputIndex = p_sys->crossbar_routes[i].AudioInputIndex;
497 LONG AudioOutputIndex = p_sys->crossbar_routes[i].AudioOutputIndex;
499 if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
501 msg_Dbg( p_this, "crossbar at depth %d, routed video "
502 "output %ld to video input %ld", i, VideoOutputIndex,
505 if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
507 if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
510 msg_Dbg(p_this, "crossbar at depth %d, routed audio "
511 "output %ld to audio input %ld", i,
512 AudioOutputIndex, AudioInputIndex );
519 ** Show properties pages from other filters in graph
521 var_Get( p_this, "dshow-config", &val );
524 for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
526 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
529 if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
532 ShowPropertyPage( p_XF );
538 /* Initialize some data */
539 p_sys->i_current_stream = 0;
541 if( !p_sys->i_streams ) return VLC_EGENERIC;
546 /*****************************************************************************
547 * DemuxOpen: open direct show device as an access_demux module
548 *****************************************************************************/
549 static int DemuxOpen( vlc_object_t *p_this )
551 demux_t *p_demux = (demux_t *)p_this;
555 p_sys = (access_sys_t *)malloc( sizeof( access_sys_t ) );
556 memset( p_sys, 0, sizeof( access_sys_t ) );
557 p_demux->p_sys = (demux_sys_t *)p_sys;
559 if( CommonOpen( p_this, p_sys, VLC_TRUE ) != VLC_SUCCESS )
561 CommonClose( p_this, p_sys );
565 /* Everything is ready. Let's rock baby */
566 msg_Dbg( p_this, "Playing...");
567 p_sys->p_control->Run();
569 p_demux->pf_demux = Demux;
570 p_demux->pf_control = DemuxControl;
571 p_demux->info.i_update = 0;
572 p_demux->info.i_title = 0;
573 p_demux->info.i_seekpoint = 0;
575 for( i = 0; i < p_sys->i_streams; i++ )
577 dshow_stream_t *p_stream = p_sys->pp_streams[i];
580 if( p_stream->mt.majortype == MEDIATYPE_Video )
582 es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
584 fmt.video.i_width = p_stream->header.video.bmiHeader.biWidth;
585 fmt.video.i_height = p_stream->header.video.bmiHeader.biHeight;
586 fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
588 if( !p_stream->header.video.bmiHeader.biCompression )
590 /* RGB DIB are coded from bottom to top */
591 fmt.video.i_height = (unsigned int)(-(int)fmt.video.i_height);
594 /* Setup rgb mask for RGB formats */
595 if( p_stream->i_fourcc == VLC_FOURCC('R','V','2','4') )
597 /* This is in BGR format */
598 fmt.video.i_bmask = 0x00ff0000;
599 fmt.video.i_gmask = 0x0000ff00;
600 fmt.video.i_rmask = 0x000000ff;
603 if( p_stream->header.video.AvgTimePerFrame )
605 fmt.video.i_frame_rate = 10000000;
606 fmt.video.i_frame_rate_base =
607 p_stream->header.video.AvgTimePerFrame;
610 else if( p_stream->mt.majortype == MEDIATYPE_Audio )
612 es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
614 fmt.audio.i_channels = p_stream->header.audio.nChannels;
615 fmt.audio.i_rate = p_stream->header.audio.nSamplesPerSec;
616 fmt.audio.i_bitspersample = p_stream->header.audio.wBitsPerSample;
617 fmt.audio.i_blockalign = fmt.audio.i_channels *
618 fmt.audio.i_bitspersample / 8;
619 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
620 fmt.audio.i_bitspersample;
623 p_stream->p_es = es_out_Add( p_demux->out, &fmt );
629 /*****************************************************************************
630 * AccessOpen: open direct show device as an access module
631 *****************************************************************************/
632 static int AccessOpen( vlc_object_t *p_this )
634 access_t *p_access = (access_t*)p_this;
637 p_access->p_sys = p_sys = (access_sys_t *)malloc( sizeof( access_sys_t ) );
638 memset( p_sys, 0, sizeof( access_sys_t ) );
640 if( CommonOpen( p_this, p_sys, VLC_FALSE ) != VLC_SUCCESS )
642 CommonClose( p_this, p_sys );
646 dshow_stream_t *p_stream = p_sys->pp_streams[0];
648 /* Check if we need to force demuxers */
649 if( !p_access->psz_demux || !*p_access->psz_demux )
651 if( p_stream->i_fourcc == VLC_FOURCC('d','v','s','l') ||
652 p_stream->i_fourcc == VLC_FOURCC('d','v','s','d') ||
653 p_stream->i_fourcc == VLC_FOURCC('d','v','h','d') )
655 p_access->psz_demux = strdup( "rawdv" );
657 else if( p_stream->i_fourcc == VLC_FOURCC('m','p','2','v') )
659 p_access->psz_demux = "mpgv";
664 p_access->pf_read = NULL;
665 p_access->pf_block = ReadCompressed;
666 p_access->pf_control = AccessControl;
667 p_access->pf_seek = NULL;
668 p_access->info.i_update = 0;
669 p_access->info.i_size = 0;
670 p_access->info.i_pos = 0;
671 p_access->info.b_eof = VLC_FALSE;
672 p_access->info.i_title = 0;
673 p_access->info.i_seekpoint = 0;
674 p_access->p_sys = p_sys;
676 /* Everything is ready. Let's rock baby */
677 msg_Dbg( p_this, "Playing...");
678 p_sys->p_control->Run();
683 /*****************************************************************************
684 * CommonClose: close device
685 *****************************************************************************/
686 static void CommonClose( vlc_object_t *p_this, access_sys_t *p_sys )
688 msg_Dbg( p_this, "releasing DirectShow");
690 DeleteDirectShowGraph( p_sys );
692 /* Uninitialize OLE/COM */
695 for( int i = 0; i < p_sys->i_streams; i++ ) delete p_sys->pp_streams[i];
696 if( p_sys->i_streams ) free( p_sys->pp_streams );
698 vlc_mutex_destroy( &p_sys->lock );
699 vlc_cond_destroy( &p_sys->wait );
704 /*****************************************************************************
705 * AccessClose: close device
706 *****************************************************************************/
707 static void AccessClose( vlc_object_t *p_this )
709 access_t *p_access = (access_t *)p_this;
710 access_sys_t *p_sys = p_access->p_sys;
712 /* Stop capturing stuff */
713 p_sys->p_control->Stop();
715 CommonClose( p_this, p_sys );
718 /*****************************************************************************
719 * DemuxClose: close device
720 *****************************************************************************/
721 static void DemuxClose( vlc_object_t *p_this )
723 demux_t *p_demux = (demux_t *)p_this;
724 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
726 /* Stop capturing stuff */
727 p_sys->p_control->Stop();
729 CommonClose( p_this, p_sys );
732 /****************************************************************************
734 ****************************************************************************/
735 static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
736 IBaseFilter *p_filter,
737 CaptureFilter *p_capture_filter )
739 CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
741 AM_MEDIA_TYPE mediaType = p_input_pin->CustomGetMediaType();
743 if( p_sys->p_capture_graph_builder2 )
745 if( FAILED(p_sys->p_capture_graph_builder2->
746 RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
747 p_filter, 0, (IBaseFilter *)p_capture_filter )) )
752 // Sort out all the possible video inputs
753 // The class needs to be given the capture filters ANALOGVIDEO input pin
755 if( ( mediaType.majortype == MEDIATYPE_Video ||
756 mediaType.majortype == MEDIATYPE_Stream ) &&
757 SUCCEEDED(p_filter->EnumPins(&pins)) )
763 IKsPropertySet *pKs=0;
767 while( !Found && ( S_OK == pins->Next(1, &pP, &n) ) )
769 if( S_OK == pP->QueryPinInfo(&pinInfo) )
771 // is this pin an ANALOGVIDEOIN input pin?
772 if( pinInfo.dir == PINDIR_INPUT &&
773 pP->QueryInterface( IID_IKsPropertySet,
774 (void **)&pKs ) == S_OK )
776 if( pKs->Get( AMPROPSETID_Pin,
777 AMPROPERTY_PIN_CATEGORY, NULL, 0,
778 &guid, sizeof(GUID), &dw ) == S_OK )
780 if( guid == PIN_CATEGORY_ANALOGVIDEOIN )
782 // recursively search crossbar routes
783 FindCrossbarRoutes( p_this, p_sys, pP, 0 );
790 pinInfo.pFilter->Release();
800 IEnumPins *p_enumpins;
803 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;
805 while( S_OK == p_enumpins->Next( 1, &p_pin, NULL ) )
807 PIN_DIRECTION pin_dir;
808 p_pin->QueryDirection( &pin_dir );
810 if( pin_dir == PINDIR_OUTPUT &&
811 p_sys->p_graph->ConnectDirect( p_pin, (IPin *)p_input_pin,
815 p_enumpins->Release();
821 p_enumpins->Release();
827 * get fourcc priority from arbritary preference, the higher the better
829 static int GetFourCCPriority( int i_fourcc )
833 case VLC_FOURCC('I','4','2','0'):
834 case VLC_FOURCC('f','l','3','2'):
836 case VLC_FOURCC('Y','V','1','2'):
837 case VLC_FOURCC('a','r','a','w'):
839 case VLC_FOURCC('R','V','2','4'):
841 case VLC_FOURCC('Y','U','Y','2'):
842 case VLC_FOURCC('R','V','3','2'):
843 case VLC_FOURCC('R','G','B','A'):
850 #define MAX_MEDIA_TYPES 32
852 static int OpenDevice( vlc_object_t *p_this, access_sys_t *p_sys,
853 string devicename, vlc_bool_t b_audio )
855 /* See if device is already opened */
856 for( int i = 0; i < p_sys->i_streams; i++ )
858 if( devicename.size() &&
859 p_sys->pp_streams[i]->devicename == devicename )
866 list<string> list_devices;
868 /* Enumerate devices and display their names */
869 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
871 if( !list_devices.size() )
874 list<string>::iterator iter;
875 for( iter = list_devices.begin(); iter != list_devices.end(); iter++ )
876 msg_Dbg( p_this, "found device: %s", iter->c_str() );
878 /* If no device name was specified, pick the 1st one */
879 if( devicename.size() == 0 )
881 devicename = *list_devices.begin();
884 // Use the system device enumerator and class enumerator to find
885 // a capture/preview device, such as a desktop USB video camera.
886 IBaseFilter *p_device_filter =
887 FindCaptureDevice( p_this, &devicename, 0, b_audio );
888 if( p_device_filter )
889 msg_Dbg( p_this, "using device: %s", devicename.c_str() );
892 msg_Err( p_this, "can't use device: %s, unsupported device type",
893 devicename.c_str() );
894 intf_UserFatal( p_this, VLC_FALSE, _("Capturing failed"),
895 _("VLC cannot use the device \"%s\", because its device "
896 "type is not supported.") );
900 // Retreive acceptable media types supported by device
901 AM_MEDIA_TYPE media_types[MAX_MEDIA_TYPES];
903 EnumDeviceCaps( p_this, p_device_filter, b_audio ? 0 : p_sys->i_chroma,
904 p_sys->i_width, p_sys->i_height,
905 0, 0, 0, media_types, MAX_MEDIA_TYPES );
907 AM_MEDIA_TYPE *mt = NULL;
909 if( media_count > 0 )
911 mt = (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * media_count);
913 // Order and copy returned media types according to arbitrary
915 for( size_t c = 0; c < media_count; c++ )
918 GetFourCCPriority(GetFourCCFromMediaType(media_types[c]));
919 size_t slot_copy = c;
920 for( size_t d = c+1; d < media_count; d++ )
923 GetFourCCPriority(GetFourCCFromMediaType(media_types[d]));
924 if( priority > slot_priority )
926 slot_priority = priority;
932 mt[c] = media_types[slot_copy];
933 media_types[slot_copy] = media_types[c];
937 mt[c] = media_types[c];
943 msg_Err( p_this, "capture device '%s' does not support required parameters !", devicename.c_str() );
944 intf_UserFatal( p_this, VLC_FALSE, _("Capturing failed"),
945 _("The capture device \"%s\" does not support the "
946 "required parameters."), devicename.c_str() );
947 p_device_filter->Release();
951 /* Create and add our capture filter */
952 CaptureFilter *p_capture_filter =
953 new CaptureFilter( p_this, p_sys, mt, media_count );
954 p_sys->p_graph->AddFilter( p_capture_filter, 0 );
956 /* Add the device filter to the graph (seems necessary with VfW before
957 * accessing pin attributes). */
958 p_sys->p_graph->AddFilter( p_device_filter, 0 );
960 /* Attempt to connect one of this device's capture output pins */
961 msg_Dbg( p_this, "connecting filters" );
962 if( ConnectFilters( p_this, p_sys, p_device_filter, p_capture_filter ) )
965 msg_Dbg( p_this, "filters connected successfully !" );
967 dshow_stream_t dshow_stream;
968 dshow_stream.b_pts = VLC_FALSE;
969 dshow_stream.p_es = 0;
971 p_capture_filter->CustomGetPin()->CustomGetMediaType();
973 /* Show Device properties. Done here so the VLC stream is setup with
974 * the proper parameters. */
976 var_Get( p_this, "dshow-config", &val );
979 ShowDeviceProperties( p_this, p_sys->p_capture_graph_builder2,
980 p_device_filter, b_audio );
983 ConfigTuner( p_this, p_sys->p_capture_graph_builder2,
986 var_Get( p_this, "dshow-tuner", &val );
987 if( val.b_bool && dshow_stream.mt.majortype != MEDIATYPE_Stream )
989 /* FIXME: we do MEDIATYPE_Stream later so we don't do it twice. */
990 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
991 p_device_filter, b_audio );
995 p_capture_filter->CustomGetPin()->CustomGetMediaType();
997 dshow_stream.i_fourcc = GetFourCCFromMediaType( dshow_stream.mt );
998 if( dshow_stream.i_fourcc )
1000 if( dshow_stream.mt.majortype == MEDIATYPE_Video )
1002 dshow_stream.header.video =
1003 *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
1004 msg_Dbg( p_this, "MEDIATYPE_Video" );
1005 msg_Dbg( p_this, "selected video pin accepts format: %4.4s",
1006 (char *)&dshow_stream.i_fourcc);
1008 else if( dshow_stream.mt.majortype == MEDIATYPE_Audio )
1010 dshow_stream.header.audio =
1011 *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
1012 msg_Dbg( p_this, "MEDIATYPE_Audio" );
1013 msg_Dbg( p_this, "selected audio pin accepts format: %4.4s",
1014 (char *)&dshow_stream.i_fourcc);
1016 else if( dshow_stream.mt.majortype == MEDIATYPE_Stream )
1018 msg_Dbg( p_this, "MEDIATYPE_Stream" );
1019 msg_Dbg( p_this, "selected stream pin accepts format: %4.4s",
1020 (char *)&dshow_stream.i_fourcc);
1024 msg_Dbg( p_this, "unknown stream majortype" );
1028 /* Add directshow elementary stream to our list */
1029 dshow_stream.p_device_filter = p_device_filter;
1030 dshow_stream.p_capture_filter = p_capture_filter;
1032 p_sys->pp_streams = (dshow_stream_t **)realloc( p_sys->pp_streams,
1033 sizeof(dshow_stream_t *) * (p_sys->i_streams + 1) );
1034 p_sys->pp_streams[p_sys->i_streams] = new dshow_stream_t;
1035 *p_sys->pp_streams[p_sys->i_streams++] = dshow_stream;
1042 /* Remove filters from graph */
1043 p_sys->p_graph->RemoveFilter( p_device_filter );
1044 p_sys->p_graph->RemoveFilter( p_capture_filter );
1046 /* Release objects */
1047 p_device_filter->Release();
1048 p_capture_filter->Release();
1050 return VLC_EGENERIC;
1053 static IBaseFilter *
1054 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
1055 list<string> *p_listdevices, vlc_bool_t b_audio )
1057 IBaseFilter *p_base_filter = NULL;
1058 IMoniker *p_moniker = NULL;
1062 /* Create the system device enumerator */
1063 ICreateDevEnum *p_dev_enum = NULL;
1065 hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
1066 IID_ICreateDevEnum, (void **)&p_dev_enum );
1069 msg_Err( p_this, "failed to create the device enumerator (0x%lx)", hr);
1073 /* Create an enumerator for the video capture devices */
1074 IEnumMoniker *p_class_enum = NULL;
1076 hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
1079 hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
1081 p_dev_enum->Release();
1084 msg_Err( p_this, "failed to create the class enumerator (0x%lx)", hr );
1088 /* If there are no enumerators for the requested type, then
1089 * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
1090 if( p_class_enum == NULL )
1092 msg_Err( p_this, "no capture device was detected" );
1096 /* Enumerate the devices */
1098 /* Note that if the Next() call succeeds but there are no monikers,
1099 * it will return S_FALSE (which is not a failure). Therefore, we check
1100 * that the return code is S_OK instead of using SUCCEEDED() macro. */
1102 while( p_class_enum->Next( 1, &p_moniker, &i_fetched ) == S_OK )
1104 /* Getting the property page to get the device name */
1105 IPropertyBag *p_bag;
1106 hr = p_moniker->BindToStorage( 0, 0, IID_IPropertyBag,
1112 hr = p_bag->Read( L"FriendlyName", &var, NULL );
1116 int i_convert = WideCharToMultiByte(CP_ACP, 0, var.bstrVal,
1117 SysStringLen(var.bstrVal), NULL, 0, NULL, NULL);
1118 char *p_buf = (char *)alloca( i_convert+1 ); p_buf[0] = 0;
1119 WideCharToMultiByte( CP_ACP, 0, var.bstrVal,
1120 SysStringLen(var.bstrVal), p_buf, i_convert, NULL, NULL );
1121 SysFreeString(var.bstrVal);
1122 p_buf[i_convert] = '\0';
1124 if( p_listdevices ) p_listdevices->push_back( p_buf );
1126 if( p_devicename && *p_devicename == string(p_buf) )
1128 /* Bind Moniker to a filter object */
1129 hr = p_moniker->BindToObject( 0, 0, IID_IBaseFilter,
1130 (void **)&p_base_filter );
1133 msg_Err( p_this, "couldn't bind moniker to filter "
1134 "object (0x%lx)", hr );
1135 p_moniker->Release();
1136 p_class_enum->Release();
1139 p_moniker->Release();
1140 p_class_enum->Release();
1141 return p_base_filter;
1146 p_moniker->Release();
1149 p_class_enum->Release();
1153 static size_t EnumDeviceCaps( vlc_object_t *p_this, IBaseFilter *p_filter,
1154 int i_fourcc, int i_width, int i_height,
1155 int i_channels, int i_samplespersec,
1156 int i_bitspersample, AM_MEDIA_TYPE *mt,
1159 IEnumPins *p_enumpins;
1161 IEnumMediaTypes *p_enummt;
1162 size_t mt_count = 0;
1164 LONGLONG i_AvgTimePerFrame = 0;
1165 float r_fps = var_GetFloat( p_this, "dshow-fps" );
1167 i_AvgTimePerFrame = 10000000000LL/(LONGLONG)(r_fps*1000.0f);
1169 if( FAILED(p_filter->EnumPins( &p_enumpins )) )
1171 msg_Dbg( p_this, "EnumDeviceCaps failed: no pin enumeration !");
1175 while( S_OK == p_enumpins->Next( 1, &p_output_pin, NULL ) )
1179 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1181 msg_Dbg( p_this, "EnumDeviceCaps: %s pin: %S",
1182 info.dir == PINDIR_INPUT ? "input" : "output",
1184 if( info.pFilter ) info.pFilter->Release();
1187 p_output_pin->Release();
1190 p_enumpins->Reset();
1192 while( !mt_count && p_enumpins->Next( 1, &p_output_pin, NULL ) == S_OK )
1196 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1198 if( info.pFilter ) info.pFilter->Release();
1199 if( info.dir == PINDIR_INPUT )
1201 p_output_pin->Release();
1204 msg_Dbg( p_this, "EnumDeviceCaps: trying pin %S", info.achName );
1207 AM_MEDIA_TYPE *p_mt;
1210 ** Configure pin with a default compatible media if possible
1213 IAMStreamConfig *pSC;
1214 if( SUCCEEDED(p_output_pin->QueryInterface( IID_IAMStreamConfig,
1217 int piCount, piSize;
1218 if( SUCCEEDED(pSC->GetNumberOfCapabilities(&piCount, &piSize)) )
1220 BYTE *pSCC= (BYTE *)CoTaskMemAlloc(piSize);
1223 int i_priority = -1;
1224 for( int i=0; i<piCount; ++i )
1226 if( SUCCEEDED(pSC->GetStreamCaps(i, &p_mt, pSCC)) )
1228 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1229 int i_current_priority = GetFourCCPriority(i_current_fourcc);
1231 if( (i_fourcc && (i_current_fourcc != i_fourcc))
1232 || (i_priority > i_current_priority) )
1234 // unwanted chroma, try next media type
1235 FreeMediaType( *p_mt );
1236 CoTaskMemFree( (PVOID)p_mt );
1240 if( MEDIATYPE_Video == p_mt->majortype
1241 && FORMAT_VideoInfo == p_mt->formattype )
1243 VIDEO_STREAM_CONFIG_CAPS *pVSCC = reinterpret_cast<VIDEO_STREAM_CONFIG_CAPS*>(pSCC);
1244 VIDEOINFOHEADER *pVih = reinterpret_cast<VIDEOINFOHEADER*>(p_mt->pbFormat);
1246 if( i_AvgTimePerFrame )
1248 if( pVSCC->MinFrameInterval > i_AvgTimePerFrame
1249 || i_AvgTimePerFrame > pVSCC->MaxFrameInterval )
1251 // required frame rate not compatible, try next media type
1252 FreeMediaType( *p_mt );
1253 CoTaskMemFree( (PVOID)p_mt );
1256 pVih->AvgTimePerFrame = i_AvgTimePerFrame;
1261 if( i_width % pVSCC->OutputGranularityX
1262 || pVSCC->MinOutputSize.cx > i_width
1263 || i_width > pVSCC->MaxOutputSize.cx )
1265 // required width not compatible, try next media type
1266 FreeMediaType( *p_mt );
1267 CoTaskMemFree( (PVOID)p_mt );
1270 pVih->bmiHeader.biWidth = i_width;
1275 if( i_height % pVSCC->OutputGranularityY
1276 || pVSCC->MinOutputSize.cy > i_height
1277 || i_height > pVSCC->MaxOutputSize.cy )
1279 // required height not compatible, try next media type
1280 FreeMediaType( *p_mt );
1281 CoTaskMemFree( (PVOID)p_mt );
1284 pVih->bmiHeader.biHeight = i_height;
1287 // Set the sample size and image size.
1288 // (Round the image width up to a DWORD boundary.)
1289 p_mt->lSampleSize = pVih->bmiHeader.biSizeImage =
1290 ((pVih->bmiHeader.biWidth + 3) & ~3) *
1291 pVih->bmiHeader.biHeight * (pVih->bmiHeader.biBitCount>>3);
1293 // no cropping, use full video input buffer
1294 memset(&(pVih->rcSource), 0, sizeof(RECT));
1295 memset(&(pVih->rcTarget), 0, sizeof(RECT));
1297 // select this format as default
1298 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1300 i_priority = i_current_priority;
1302 // no need to check any more media types
1306 else if( p_mt->majortype == MEDIATYPE_Audio
1307 && p_mt->formattype == FORMAT_WaveFormatEx )
1309 AUDIO_STREAM_CONFIG_CAPS *pASCC = reinterpret_cast<AUDIO_STREAM_CONFIG_CAPS*>(pSCC);
1310 WAVEFORMATEX *pWfx = reinterpret_cast<WAVEFORMATEX*>(p_mt->pbFormat);
1312 if( i_current_fourcc && (WAVE_FORMAT_PCM == pWfx->wFormatTag) )
1314 int val = i_channels;
1318 if( val % pASCC->ChannelsGranularity
1319 || (unsigned int)val < pASCC->MinimumChannels
1320 || (unsigned int)val > pASCC->MaximumChannels )
1322 // required number channels not available, try next media type
1323 FreeMediaType( *p_mt );
1324 CoTaskMemFree( (PVOID)p_mt );
1327 pWfx->nChannels = val;
1329 val = i_samplespersec;
1333 if( val % pASCC->SampleFrequencyGranularity
1334 || (unsigned int)val < pASCC->MinimumSampleFrequency
1335 || (unsigned int)val > pASCC->MaximumSampleFrequency )
1337 // required sampling rate not available, try next media type
1338 FreeMediaType( *p_mt );
1339 CoTaskMemFree( (PVOID)p_mt );
1342 pWfx->nSamplesPerSec = val;
1344 val = i_bitspersample;
1347 if( VLC_FOURCC('f', 'l', '3', '2') == i_current_fourcc )
1353 if( val % pASCC->BitsPerSampleGranularity
1354 || (unsigned int)val < pASCC->MinimumBitsPerSample
1355 || (unsigned int)val > pASCC->MaximumBitsPerSample )
1357 // required sample size not available, try next media type
1358 FreeMediaType( *p_mt );
1359 CoTaskMemFree( (PVOID)p_mt );
1363 pWfx->wBitsPerSample = val;
1364 pWfx->nBlockAlign = (pWfx->wBitsPerSample * pWfx->nChannels)/8;
1365 pWfx->nAvgBytesPerSec = pWfx->nSamplesPerSec * pWfx->nBlockAlign;
1367 // select this format as default
1368 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1370 i_priority = i_current_priority;
1374 FreeMediaType( *p_mt );
1375 CoTaskMemFree( (PVOID)p_mt );
1378 CoTaskMemFree( (LPVOID)pSCC );
1379 if( i_priority >= 0 )
1380 msg_Dbg( p_this, "EnumDeviceCaps: input pin default format configured");
1387 ** Probe pin for available medias (may be a previously configured one)
1390 if( FAILED( p_output_pin->EnumMediaTypes( &p_enummt ) ) )
1392 p_output_pin->Release();
1396 while( p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1398 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1399 if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Video
1400 && p_mt->formattype == FORMAT_VideoInfo )
1402 int i_current_width = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1403 int i_current_height = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1404 LONGLONG i_current_atpf = ((VIDEOINFOHEADER *)p_mt->pbFormat)->AvgTimePerFrame;
1406 if( i_current_height < 0 )
1407 i_current_height = -i_current_height;
1409 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1410 "accepts chroma: %4.4s, width:%i, height:%i, fps:%f",
1411 (char *)&i_current_fourcc, i_current_width,
1412 i_current_height, (10000000.0f/((float)i_current_atpf)) );
1414 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1415 ( !i_width || i_width == i_current_width ) &&
1416 ( !i_height || i_height == i_current_height ) &&
1417 ( !i_AvgTimePerFrame || i_AvgTimePerFrame == i_current_atpf ) &&
1421 mt[mt_count++] = *p_mt;
1423 else FreeMediaType( *p_mt );
1425 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Audio
1426 && p_mt->formattype == FORMAT_WaveFormatEx)
1428 int i_current_channels =
1429 ((WAVEFORMATEX *)p_mt->pbFormat)->nChannels;
1430 int i_current_samplespersec =
1431 ((WAVEFORMATEX *)p_mt->pbFormat)->nSamplesPerSec;
1432 int i_current_bitspersample =
1433 ((WAVEFORMATEX *)p_mt->pbFormat)->wBitsPerSample;
1435 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1436 "accepts format: %4.4s, channels:%i, "
1437 "samples/sec:%i bits/sample:%i",
1438 (char *)&i_current_fourcc, i_current_channels,
1439 i_current_samplespersec, i_current_bitspersample);
1441 if( (!i_channels || i_channels == i_current_channels) &&
1442 (!i_samplespersec ||
1443 i_samplespersec == i_current_samplespersec) &&
1444 (!i_bitspersample ||
1445 i_bitspersample == i_current_bitspersample) &&
1449 mt[mt_count++] = *p_mt;
1451 /* Setup a few properties like the audio latency */
1452 IAMBufferNegotiation *p_ambuf;
1453 if( SUCCEEDED( p_output_pin->QueryInterface(
1454 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
1456 ALLOCATOR_PROPERTIES AllocProp;
1457 AllocProp.cbAlign = -1;
1459 /* 100 ms of latency */
1460 AllocProp.cbBuffer = i_current_channels *
1461 i_current_samplespersec *
1462 i_current_bitspersample / 8 / 10;
1464 AllocProp.cbPrefix = -1;
1465 AllocProp.cBuffers = -1;
1466 p_ambuf->SuggestAllocatorProperties( &AllocProp );
1470 else FreeMediaType( *p_mt );
1472 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Stream )
1474 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1475 "accepts stream format: %4.4s",
1476 (char *)&i_current_fourcc );
1478 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1482 mt[mt_count++] = *p_mt;
1483 i_fourcc = i_current_fourcc;
1485 else FreeMediaType( *p_mt );
1489 char *psz_type = "unknown";
1490 if( p_mt->majortype == MEDIATYPE_Video ) psz_type = "video";
1491 if( p_mt->majortype == MEDIATYPE_Audio ) psz_type = "audio";
1492 if( p_mt->majortype == MEDIATYPE_Stream ) psz_type = "stream";
1493 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: unsupported format "
1494 "(%s %4.4s)", psz_type, (char *)&p_mt->subtype );
1496 FreeMediaType( *p_mt );
1498 CoTaskMemFree( (PVOID)p_mt );
1501 if( !mt_count && p_enummt->Reset() == S_OK )
1503 // VLC did not find any supported MEDIATYPE for this output pin.
1504 // However the graph builder might insert converter filters in
1505 // the graph if we use a different codec in VLC filter input pin.
1506 // however, in order to avoid nasty surprises, make use of this
1507 // facility only for known unsupported codecs.
1509 while( !mt_count && p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1511 // the first four bytes of subtype GUID contains the codec FOURCC
1512 const char *pfcc = (char *)&p_mt->subtype;
1513 int i_current_fourcc = VLC_FOURCC(pfcc[0], pfcc[1], pfcc[2], pfcc[3]);
1514 if( VLC_FOURCC('H','C','W','2') == i_current_fourcc
1515 && p_mt->majortype == MEDIATYPE_Video )
1517 // output format for 'Hauppauge WinTV PVR PCI II Capture'
1518 // try I420 as an input format
1519 i_current_fourcc = VLC_FOURCC('I','4','2','0');
1520 if( !i_fourcc || i_fourcc == i_current_fourcc )
1522 // return alternative media type
1526 mtr.majortype = MEDIATYPE_Video;
1527 mtr.subtype = MEDIASUBTYPE_I420;
1528 mtr.bFixedSizeSamples = TRUE;
1529 mtr.bTemporalCompression = FALSE;
1531 mtr.formattype = FORMAT_VideoInfo;
1532 mtr.cbFormat = sizeof(vh);
1533 mtr.pbFormat = (BYTE *)&vh;
1535 memset(&vh, 0, sizeof(vh));
1537 vh.bmiHeader.biSize = sizeof(vh.bmiHeader);
1538 vh.bmiHeader.biWidth = i_width > 0 ? i_width :
1539 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1540 vh.bmiHeader.biHeight = i_height > 0 ? i_height :
1541 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1542 vh.bmiHeader.biPlanes = 3;
1543 vh.bmiHeader.biBitCount = 12;
1544 vh.bmiHeader.biCompression = VLC_FOURCC('I','4','2','0');
1545 vh.bmiHeader.biSizeImage = vh.bmiHeader.biWidth * 12 *
1546 vh.bmiHeader.biHeight / 8;
1547 mtr.lSampleSize = vh.bmiHeader.biSizeImage;
1549 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: using 'I420' in place of unsupported format 'HCW2'");
1551 if( SUCCEEDED(CopyMediaType(mt+mt_count, &mtr)) )
1555 FreeMediaType( *p_mt );
1559 p_enummt->Release();
1560 p_output_pin->Release();
1563 p_enumpins->Release();
1567 /*****************************************************************************
1568 * ReadCompressed: reads compressed (MPEG/DV) data from the device.
1569 *****************************************************************************
1570 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1572 *****************************************************************************/
1573 static block_t *ReadCompressed( access_t *p_access )
1575 access_sys_t *p_sys = p_access->p_sys;
1576 dshow_stream_t *p_stream = NULL;
1577 VLCMediaSample sample;
1579 /* Read 1 DV/MPEG frame (they contain the video and audio data) */
1581 /* There must be only 1 elementary stream to produce a valid stream
1582 * of MPEG or DV data */
1583 p_stream = p_sys->pp_streams[0];
1587 if( p_access->b_die || p_access->b_error ) return 0;
1589 /* Get new sample/frame from the elementary stream (blocking). */
1590 vlc_mutex_lock( &p_sys->lock );
1592 if( p_stream->p_capture_filter->CustomGetPin()
1593 ->CustomGetSample( &sample ) != S_OK )
1595 /* No data available. Wait until some data has arrived */
1596 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1597 vlc_mutex_unlock( &p_sys->lock );
1601 vlc_mutex_unlock( &p_sys->lock );
1608 int i_data_size = sample.p_sample->GetActualDataLength();
1610 if( !i_data_size || !(p_block = block_New( p_access, i_data_size )) )
1612 sample.p_sample->Release();
1616 sample.p_sample->GetPointer( &p_data );
1617 p_access->p_libvlc->pf_memcpy( p_block->p_buffer, p_data, i_data_size );
1618 sample.p_sample->Release();
1620 /* The caller got what he wanted */
1624 return 0; /* never reached */
1627 /****************************************************************************
1629 ****************************************************************************/
1630 static int Demux( demux_t *p_demux )
1632 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
1633 dshow_stream_t *p_stream = NULL;
1634 VLCMediaSample sample;
1635 int i_data_size, i_stream;
1639 vlc_mutex_lock( &p_sys->lock );
1641 /* Try to grab an audio sample (audio has a higher priority) */
1642 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1644 p_stream = p_sys->pp_streams[i_stream];
1645 if( p_stream->mt.majortype == MEDIATYPE_Audio &&
1646 p_stream->p_capture_filter &&
1647 p_stream->p_capture_filter->CustomGetPin()
1648 ->CustomGetSample( &sample ) == S_OK )
1653 /* Try to grab a video sample */
1654 if( i_stream == p_sys->i_streams )
1656 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1658 p_stream = p_sys->pp_streams[i_stream];
1659 if( p_stream->p_capture_filter &&
1660 p_stream->p_capture_filter->CustomGetPin()
1661 ->CustomGetSample( &sample ) == S_OK )
1668 vlc_mutex_unlock( &p_sys->lock );
1670 if( i_stream == p_sys->i_streams )
1672 /* Sleep so we do not consume all the cpu, 10ms seems
1673 * like a good value (100fps) */
1681 i_data_size = sample.p_sample->GetActualDataLength();
1682 sample.p_sample->GetPointer( &p_data );
1684 REFERENCE_TIME i_pts, i_end_date;
1685 HRESULT hr = sample.p_sample->GetTime( &i_pts, &i_end_date );
1686 if( hr != VFW_S_NO_STOP_TIME && hr != S_OK ) i_pts = 0;
1690 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
1692 /* Use our data timestamp */
1693 i_pts = sample.i_timestamp;
1694 p_stream->b_pts = VLC_TRUE;
1698 i_pts /= 10; /* Dshow works with 100 nano-seconds resolution */
1701 msg_Dbg( p_demux, "Read() stream: %i, size: %i, PTS: "I64Fd,
1702 i_stream, i_data_size, i_pts );
1705 p_block = block_New( p_demux, i_data_size );
1706 p_demux->p_libvlc->pf_memcpy( p_block->p_buffer, p_data, i_data_size );
1707 p_block->i_pts = p_block->i_dts = i_pts;
1708 sample.p_sample->Release();
1710 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pts > 0 ? i_pts : 0 );
1711 es_out_Send( p_demux->out, p_stream->p_es, p_block );
1716 /*****************************************************************************
1718 *****************************************************************************/
1719 static int AccessControl( access_t *p_access, int i_query, va_list args )
1721 vlc_bool_t *pb_bool;
1728 case ACCESS_CAN_SEEK:
1729 case ACCESS_CAN_FASTSEEK:
1730 case ACCESS_CAN_PAUSE:
1731 case ACCESS_CAN_CONTROL_PACE:
1732 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1733 *pb_bool = VLC_FALSE;
1737 case ACCESS_GET_MTU:
1738 pi_int = (int*)va_arg( args, int * );
1742 case ACCESS_GET_PTS_DELAY:
1743 pi_64 = (int64_t*)va_arg( args, int64_t * );
1744 *pi_64 = (int64_t)var_GetInteger( p_access, "dshow-caching" ) * 1000;
1748 case ACCESS_SET_PAUSE_STATE:
1749 case ACCESS_GET_TITLE_INFO:
1750 case ACCESS_SET_TITLE:
1751 case ACCESS_SET_SEEKPOINT:
1752 case ACCESS_SET_PRIVATE_ID_STATE:
1753 return VLC_EGENERIC;
1756 msg_Warn( p_access, "unimplemented query in control" );
1757 return VLC_EGENERIC;
1763 /****************************************************************************
1765 ****************************************************************************/
1766 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1773 /* Special for access_demux */
1774 case DEMUX_CAN_PAUSE:
1775 case DEMUX_SET_PAUSE_STATE:
1776 case DEMUX_CAN_CONTROL_PACE:
1777 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1781 case DEMUX_GET_PTS_DELAY:
1782 pi64 = (int64_t*)va_arg( args, int64_t * );
1783 *pi64 = (int64_t)var_GetInteger( p_demux, "dshow-caching" ) * 1000;
1786 case DEMUX_GET_TIME:
1787 pi64 = (int64_t*)va_arg( args, int64_t * );
1791 /* TODO implement others */
1793 return VLC_EGENERIC;
1796 return VLC_EGENERIC;
1799 /*****************************************************************************
1800 * config variable callback
1801 *****************************************************************************/
1802 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1803 vlc_value_t newval, vlc_value_t oldval, void * )
1805 module_config_t *p_item;
1806 vlc_bool_t b_audio = VLC_FALSE;
1809 p_item = config_FindConfig( p_this, psz_name );
1810 if( !p_item ) return VLC_SUCCESS;
1812 if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = VLC_TRUE;
1814 /* Clear-up the current list */
1815 if( p_item->i_list )
1817 /* Keep the 2 first entries */
1818 for( i = 2; i < p_item->i_list; i++ )
1820 free( p_item->ppsz_list[i] );
1821 free( p_item->ppsz_list_text[i] );
1823 /* TODO: Remove when no more needed */
1824 p_item->ppsz_list[i] = NULL;
1825 p_item->ppsz_list_text[i] = NULL;
1829 /* Find list of devices */
1830 list<string> list_devices;
1832 /* Initialize OLE/COM */
1835 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1837 /* Uninitialize OLE/COM */
1840 if( !list_devices.size() ) return VLC_SUCCESS;
1843 (char **)realloc( p_item->ppsz_list,
1844 (list_devices.size()+3) * sizeof(char *) );
1845 p_item->ppsz_list_text =
1846 (char **)realloc( p_item->ppsz_list_text,
1847 (list_devices.size()+3) * sizeof(char *) );
1849 list<string>::iterator iter;
1850 for( iter = list_devices.begin(), i = 2; iter != list_devices.end();
1853 p_item->ppsz_list[i] = strdup( iter->c_str() );
1854 p_item->ppsz_list_text[i] = NULL;
1857 p_item->ppsz_list[i] = NULL;
1858 p_item->ppsz_list_text[i] = NULL;
1860 /* Signal change to the interface */
1861 p_item->b_dirty = VLC_TRUE;
1866 static int ConfigDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1867 vlc_value_t newval, vlc_value_t oldval, void * )
1869 module_config_t *p_item;
1870 vlc_bool_t b_audio = VLC_FALSE;
1872 /* Initialize OLE/COM */
1875 p_item = config_FindConfig( p_this, psz_name );
1876 if( !p_item ) return VLC_SUCCESS;
1878 if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = VLC_TRUE;
1882 if( newval.psz_string && *newval.psz_string )
1884 devicename = newval.psz_string;
1888 /* If no device name was specified, pick the 1st one */
1889 list<string> list_devices;
1891 /* Enumerate devices */
1892 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1893 if( !list_devices.size() ) return VLC_EGENERIC;
1894 devicename = *list_devices.begin();
1897 IBaseFilter *p_device_filter =
1898 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
1899 if( p_device_filter )
1901 ShowPropertyPage( p_device_filter );
1902 p_device_filter->Release();
1906 /* Uninitialize OLE/COM */
1909 msg_Err( p_this, "didn't find device: %s", devicename.c_str() );
1910 return VLC_EGENERIC;
1913 /* Uninitialize OLE/COM */
1919 /*****************************************************************************
1921 *****************************************************************************/
1922 static void ShowPropertyPage( IUnknown *obj )
1924 ISpecifyPropertyPages *p_spec;
1927 HRESULT hr = obj->QueryInterface( IID_ISpecifyPropertyPages,
1929 if( FAILED(hr) ) return;
1931 if( SUCCEEDED(p_spec->GetPages( &cauuid )) )
1933 if( cauuid.cElems > 0 )
1935 HWND hwnd_desktop = ::GetDesktopWindow();
1937 OleCreatePropertyFrame( hwnd_desktop, 30, 30, NULL, 1, &obj,
1938 cauuid.cElems, cauuid.pElems, 0, 0, NULL );
1940 CoTaskMemFree( cauuid.pElems );
1946 static void ShowDeviceProperties( vlc_object_t *p_this,
1947 ICaptureGraphBuilder2 *p_graph,
1948 IBaseFilter *p_device_filter,
1949 vlc_bool_t b_audio )
1952 msg_Dbg( p_this, "configuring Device Properties" );
1955 * Video or audio capture filter page
1957 ShowPropertyPage( p_device_filter );
1962 if( p_graph && b_audio )
1964 IAMStreamConfig *p_SC;
1966 msg_Dbg( p_this, "showing WDM Audio Configuration Pages" );
1968 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
1969 &MEDIATYPE_Audio, p_device_filter,
1970 IID_IAMStreamConfig, (void **)&p_SC );
1973 ShowPropertyPage(p_SC);
1981 HRESULT hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
1982 &MEDIATYPE_Audio, p_device_filter,
1983 IID_IAMTVAudio, (void **)&p_TVA );
1986 ShowPropertyPage(p_TVA);
1994 if( p_graph && !b_audio )
1996 IAMStreamConfig *p_SC;
1998 msg_Dbg( p_this, "showing WDM Video Configuration Pages" );
2000 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2001 &MEDIATYPE_Interleaved, p_device_filter,
2002 IID_IAMStreamConfig, (void **)&p_SC );
2005 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2006 &MEDIATYPE_Video, p_device_filter,
2007 IID_IAMStreamConfig, (void **)&p_SC );
2012 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2013 &MEDIATYPE_Stream, p_device_filter,
2014 IID_IAMStreamConfig, (void **)&p_SC );
2019 ShowPropertyPage(p_SC);
2025 static void ShowTunerProperties( vlc_object_t *p_this,
2026 ICaptureGraphBuilder2 *p_graph,
2027 IBaseFilter *p_device_filter,
2028 vlc_bool_t b_audio )
2031 msg_Dbg( p_this, "configuring Tuner Properties" );
2033 if( !p_graph || b_audio ) return;
2036 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2037 &MEDIATYPE_Interleaved, p_device_filter,
2038 IID_IAMTVTuner, (void **)&p_TV );
2041 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2042 &MEDIATYPE_Video, p_device_filter,
2043 IID_IAMTVTuner, (void **)&p_TV );
2048 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2049 &MEDIATYPE_Stream, p_device_filter,
2050 IID_IAMTVTuner, (void **)&p_TV );
2055 ShowPropertyPage(p_TV);
2060 static void ConfigTuner( vlc_object_t *p_this, ICaptureGraphBuilder2 *p_graph,
2061 IBaseFilter *p_device_filter )
2063 int i_channel, i_country, i_input, i_amtuner_mode;
2068 if( !p_graph ) return;
2070 i_channel = var_GetInteger( p_this, "dshow-tuner-channel" );
2071 i_country = var_GetInteger( p_this, "dshow-tuner-country" );
2072 i_input = var_GetInteger( p_this, "dshow-tuner-input" );
2073 i_amtuner_mode = var_GetInteger( p_this, "dshow-amtuner-mode" );
2075 if( !i_channel && !i_country && !i_input ) return; /* Nothing to do */
2077 msg_Dbg( p_this, "tuner config: channel %i, country %i, input type %i",
2078 i_channel, i_country, i_input );
2080 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Interleaved,
2081 p_device_filter, IID_IAMTVTuner,
2085 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
2086 p_device_filter, IID_IAMTVTuner,
2092 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Stream,
2093 p_device_filter, IID_IAMTVTuner,
2099 msg_Dbg( p_this, "couldn't find tuner interface" );
2103 hr = p_TV->GetAvailableModes( &l_modes );
2104 if( SUCCEEDED(hr) && (l_modes & i_amtuner_mode) )
2106 hr = p_TV->put_Mode( (AMTunerModeType)i_amtuner_mode );
2109 if( i_input == 1 ) p_TV->put_InputType( 0, TunerInputCable );
2110 else if( i_input == 2 ) p_TV->put_InputType( 0, TunerInputAntenna );
2112 p_TV->put_CountryCode( i_country );
2113 p_TV->put_Channel( i_channel, AMTUNER_SUBCHAN_NO_TUNE,
2114 AMTUNER_SUBCHAN_NO_TUNE );