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>
34 #include <vlc_access.h>
35 #include <vlc_demux.h>
37 #include <vlc_interface.h>
42 /*****************************************************************************
43 * Access: local prototypes
44 *****************************************************************************/
45 static block_t *ReadCompressed( access_t * );
46 static int AccessControl ( access_t *, int, va_list );
48 static int Demux ( demux_t * );
49 static int DemuxControl( demux_t *, int, va_list );
51 static int OpenDevice( vlc_object_t *, access_sys_t *, string, vlc_bool_t );
52 static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
53 list<string> *, vlc_bool_t );
54 static size_t EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
55 int, int, int, int, int, int,
56 AM_MEDIA_TYPE *mt, size_t );
57 static bool ConnectFilters( vlc_object_t *, access_sys_t *,
58 IBaseFilter *, CaptureFilter * );
59 static int FindDevicesCallback( vlc_object_t *, char const *,
60 vlc_value_t, vlc_value_t, void * );
61 static int ConfigDevicesCallback( vlc_object_t *, char const *,
62 vlc_value_t, vlc_value_t, void * );
64 static void ShowPropertyPage( IUnknown * );
65 static void ShowDeviceProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
66 IBaseFilter *, vlc_bool_t );
67 static void ShowTunerProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
68 IBaseFilter *, vlc_bool_t );
69 static void ConfigTuner( vlc_object_t *, ICaptureGraphBuilder2 *,
72 /*****************************************************************************
74 *****************************************************************************/
75 static const char *ppsz_vdev[] = { "", "none" };
76 static const char *ppsz_vdev_text[] = { N_("Default"), N_("None") };
77 static const char *ppsz_adev[] = { "", "none" };
78 static const char *ppsz_adev_text[] = { N_("Default"), N_("None") };
79 static int pi_tuner_input[] = { 0, 1, 2 };
80 static const char *ppsz_tuner_input_text[] =
81 {N_("Default"), N_("Cable"), N_("Antenna")};
82 static const int pi_amtuner_mode[] = { AMTUNER_MODE_DEFAULT,
84 AMTUNER_MODE_FM_RADIO,
85 AMTUNER_MODE_AM_RADIO,
87 static const char *ppsz_amtuner_mode_text[] = { N_("Default"),
93 #define CACHING_TEXT N_("Caching value in ms")
94 #define CACHING_LONGTEXT N_( \
95 "Caching value for DirectShow streams. " \
96 "This value should be set in millisecondss." )
97 #define VDEV_TEXT N_("Video device name")
98 #define VDEV_LONGTEXT N_( \
99 "Name of the video device that will be used by the " \
100 "DirectShow plugin. If you don't specify anything, the default device " \
102 /// \bug [String] size stuff should be on video !
103 #define ADEV_TEXT N_("Audio device name")
104 #define ADEV_LONGTEXT N_( \
105 "Name of the audio device that will be used by the " \
106 "DirectShow plugin. If you don't specify anything, the default device " \
107 "will be used. You can specify a standard size (cif, d1, ...) or <width>x<height>")
108 #define SIZE_TEXT N_("Video size")
109 #define SIZE_LONGTEXT N_( \
110 "Size of the video that will be displayed by the " \
111 "DirectShow plugin. If you don't specify anything the default size for " \
112 "your device will be used.")
113 #define CHROMA_TEXT N_("Video input chroma format")
114 #define CHROMA_LONGTEXT N_( \
115 "Force the DirectShow video input to use a specific chroma format " \
116 "(eg. I420 (default), RV24, etc.)")
117 #define FPS_TEXT N_("Video input frame rate")
118 #define FPS_LONGTEXT N_( \
119 "Force the DirectShow video input to use a specific frame rate" \
120 "(eg. 0 means default, 25, 29.97, 50, 59.94, etc.)")
121 #define CONFIG_TEXT N_("Device properties")
122 #define CONFIG_LONGTEXT N_( \
123 "Show the properties dialog of the selected device before starting the " \
125 #define TUNER_TEXT N_("Tuner properties")
126 #define TUNER_LONGTEXT N_( \
127 "Show the tuner properties [channel selection] page." )
128 #define CHANNEL_TEXT N_("Tuner TV Channel")
129 #define CHANNEL_LONGTEXT N_( \
130 "Set the TV channel the tuner will set to " \
131 "(0 means default)." )
132 #define COUNTRY_TEXT N_("Tuner country code")
133 #define COUNTRY_LONGTEXT N_( \
134 "Set the tuner country code that establishes the current " \
135 "channel-to-frequency mapping (0 means default)." )
136 #define TUNER_INPUT_TEXT N_("Tuner input type")
137 #define TUNER_INPUT_LONGTEXT N_( \
138 "Select the tuner input type (Cable/Antenna)." )
139 #define VIDEO_IN_TEXT N_("Video input pin")
140 #define VIDEO_IN_LONGTEXT N_( \
141 "Select the video input source, such as composite, s-video, " \
142 "or tuner. Since these settings are hardware-specfic, you should find good " \
143 "settings in the \"Device config\" area, and use those numbers here. -1 " \
144 "means that settings will not be changed.")
145 #define AUDIO_IN_TEXT N_("Audio input pin")
146 #define AUDIO_IN_LONGTEXT N_( \
147 "Select the audio input source. See the \"video input\" option." )
148 #define VIDEO_OUT_TEXT N_("Video output pin")
149 #define VIDEO_OUT_LONGTEXT N_( \
150 "Select the video output type. See the \"video input\" option." )
151 #define AUDIO_OUT_TEXT N_("Audio output pin")
152 #define AUDIO_OUT_LONGTEXT N_( \
153 "Select the audio output type. See the \"video input\" option." )
155 #define AMTUNER_MODE_TEXT N_("AM Tuner mode")
156 #define AMTUNER_MODE_LONGTEXT N_( \
157 "AM Tuner mode. Can be one of DEFAULT, TV, AM_RADIO, FM_RADIO or DSS.")
159 static int CommonOpen ( vlc_object_t *, access_sys_t *, vlc_bool_t );
160 static void CommonClose( vlc_object_t *, access_sys_t * );
162 static int AccessOpen ( vlc_object_t * );
163 static void AccessClose( vlc_object_t * );
165 static int DemuxOpen ( vlc_object_t * );
166 static void DemuxClose ( vlc_object_t * );
169 set_shortname( _("DirectShow") );
170 set_description( _("DirectShow input") );
171 set_category( CAT_INPUT );
172 set_subcategory( SUBCAT_INPUT_ACCESS );
173 add_integer( "dshow-caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
174 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
176 add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, VLC_FALSE);
177 change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback );
178 change_action_add( FindDevicesCallback, N_("Refresh list") );
179 change_action_add( ConfigDevicesCallback, N_("Configure") );
181 add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE);
182 change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback );
183 change_action_add( FindDevicesCallback, N_("Refresh list") );
184 change_action_add( ConfigDevicesCallback, N_("Configure") );
186 add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE);
188 add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
191 add_float( "dshow-fps", 0.0f, NULL, FPS_TEXT, FPS_LONGTEXT,
194 add_bool( "dshow-config", VLC_FALSE, NULL, CONFIG_TEXT, CONFIG_LONGTEXT,
197 add_bool( "dshow-tuner", VLC_FALSE, NULL, TUNER_TEXT, TUNER_LONGTEXT,
200 add_integer( "dshow-tuner-channel", 0, NULL, CHANNEL_TEXT,
201 CHANNEL_LONGTEXT, VLC_TRUE );
203 add_integer( "dshow-tuner-country", 0, NULL, COUNTRY_TEXT,
204 COUNTRY_LONGTEXT, VLC_TRUE );
206 add_integer( "dshow-tuner-input", 0, NULL, TUNER_INPUT_TEXT,
207 TUNER_INPUT_LONGTEXT, VLC_TRUE );
208 change_integer_list( pi_tuner_input, ppsz_tuner_input_text, 0 );
210 add_integer( "dshow-video-input", -1, NULL, VIDEO_IN_TEXT,
211 VIDEO_IN_LONGTEXT, VLC_TRUE );
213 add_integer( "dshow-audio-input", -1, NULL, AUDIO_IN_TEXT,
214 AUDIO_IN_LONGTEXT, VLC_TRUE );
216 add_integer( "dshow-video-output", -1, NULL, VIDEO_OUT_TEXT,
217 VIDEO_OUT_LONGTEXT, VLC_TRUE );
219 add_integer( "dshow-audio-output", -1, NULL, AUDIO_OUT_TEXT,
220 AUDIO_OUT_LONGTEXT, VLC_TRUE );
222 add_integer( "dshow-amtuner-mode", AMTUNER_MODE_TV, NULL,
223 AMTUNER_MODE_TEXT, AMTUNER_MODE_LONGTEXT, VLC_FALSE);
224 change_integer_list( pi_amtuner_mode, ppsz_amtuner_mode_text, 0 );
226 add_shortcut( "dshow" );
227 set_capability( "access_demux", 0 );
228 set_callbacks( DemuxOpen, DemuxClose );
231 set_description( _("DirectShow input") );
232 add_shortcut( "dshow" );
233 set_capability( "access2", 0 );
234 set_callbacks( AccessOpen, AccessClose );
238 /*****************************************************************************
239 * DirectShow elementary stream descriptor
240 *****************************************************************************/
241 typedef struct dshow_stream_t
244 IBaseFilter *p_device_filter;
245 CaptureFilter *p_capture_filter;
250 VIDEOINFOHEADER video;
261 /*****************************************************************************
262 * DirectShow utility functions
263 *****************************************************************************/
264 static void CreateDirectShowGraph( access_sys_t *p_sys )
266 p_sys->i_crossbar_route_depth = 0;
268 /* Create directshow filter graph */
269 if( SUCCEEDED( CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
270 (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph) ) )
272 /* Create directshow capture graph builder if available */
273 if( SUCCEEDED( CoCreateInstance( CLSID_CaptureGraphBuilder2, 0,
274 CLSCTX_INPROC, (REFIID)IID_ICaptureGraphBuilder2,
275 (void **)&p_sys->p_capture_graph_builder2 ) ) )
277 p_sys->p_capture_graph_builder2->
278 SetFiltergraph((IGraphBuilder *)p_sys->p_graph);
281 p_sys->p_graph->QueryInterface( IID_IMediaControl,
282 (void **)&p_sys->p_control );
286 static void DeleteDirectShowGraph( access_sys_t *p_sys )
288 DeleteCrossbarRoutes( p_sys );
290 /* Remove filters from graph */
291 for( int i = 0; i < p_sys->i_streams; i++ )
293 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_capture_filter );
294 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_device_filter );
295 p_sys->pp_streams[i]->p_capture_filter->Release();
296 p_sys->pp_streams[i]->p_device_filter->Release();
299 /* Release directshow objects */
300 if( p_sys->p_control )
302 p_sys->p_control->Release();
303 p_sys->p_control = NULL;
305 if( p_sys->p_capture_graph_builder2 )
307 p_sys->p_capture_graph_builder2->Release();
308 p_sys->p_capture_graph_builder2 = NULL;
313 p_sys->p_graph->Release();
314 p_sys->p_graph = NULL;
318 /*****************************************************************************
319 * CommonOpen: open direct show device
320 *****************************************************************************/
321 static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
322 vlc_bool_t b_access_demux )
327 /* Get/parse options and open device(s) */
328 string vdevname, adevname;
329 int i_width = 0, i_height = 0, i_chroma = 0;
330 vlc_bool_t b_audio = VLC_TRUE;
332 var_Create( p_this, "dshow-config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
333 var_Create( p_this, "dshow-tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
334 var_Create( p_this, "dshow-vdev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
335 var_Get( p_this, "dshow-vdev", &val );
336 if( val.psz_string ) vdevname = string( val.psz_string );
337 if( val.psz_string ) free( val.psz_string );
339 var_Create( p_this, "dshow-adev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
340 var_Get( p_this, "dshow-adev", &val );
341 if( val.psz_string ) adevname = string( val.psz_string );
342 if( val.psz_string ) free( val.psz_string );
344 static struct {char *psz_size; int i_width; int i_height;} size_table[] =
345 { { "subqcif", 128, 96 }, { "qsif", 160, 120 }, { "qcif", 176, 144 },
346 { "sif", 320, 240 }, { "cif", 352, 288 }, { "d1", 640, 480 },
350 var_Create( p_this, "dshow-size", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
351 var_Get( p_this, "dshow-size", &val );
352 if( val.psz_string && *val.psz_string )
354 for( i = 0; size_table[i].psz_size; i++ )
356 if( !strcmp( val.psz_string, size_table[i].psz_size ) )
358 i_width = size_table[i].i_width;
359 i_height = size_table[i].i_height;
363 if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
366 i_width = strtol( val.psz_string, &psz_parser, 0 );
367 if( *psz_parser == 'x' || *psz_parser == 'X')
369 i_height = strtol( psz_parser + 1, &psz_parser, 0 );
371 msg_Dbg( p_this, "width x height %dx%d", i_width, i_height );
374 if( val.psz_string ) free( val.psz_string );
376 p_sys->b_chroma = VLC_FALSE;
377 var_Create( p_this, "dshow-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
378 var_Get( p_this, "dshow-chroma", &val );
379 if( val.psz_string && strlen( val.psz_string ) >= 4 )
381 i_chroma = VLC_FOURCC( val.psz_string[0], val.psz_string[1],
382 val.psz_string[2], val.psz_string[3] );
383 p_sys->b_chroma = VLC_TRUE;
385 if( val.psz_string ) free( val.psz_string );
387 var_Create( p_this, "dshow-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
388 var_Create( p_this, "dshow-tuner-channel",
389 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
390 var_Create( p_this, "dshow-tuner-country",
391 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
392 var_Create( p_this, "dshow-tuner-input",
393 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
395 var_Create( p_this, "dshow-amtuner-mode",
396 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
398 var_Create( p_this, "dshow-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
400 var_Create( p_this, "dshow-video-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
401 var_Create( p_this, "dshow-audio-input", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
402 var_Create( p_this, "dshow-video-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
403 var_Create( p_this, "dshow-audio-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
405 /* Initialize OLE/COM */
408 /* Initialize some data */
409 p_sys->i_streams = 0;
410 p_sys->pp_streams = 0;
411 p_sys->i_width = i_width;
412 p_sys->i_height = i_height;
413 p_sys->i_chroma = i_chroma;
415 p_sys->p_graph = NULL;
416 p_sys->p_capture_graph_builder2 = NULL;
417 p_sys->p_control = NULL;
419 vlc_mutex_init( p_this, &p_sys->lock );
420 vlc_cond_init( p_this, &p_sys->wait );
422 /* Build directshow graph */
423 CreateDirectShowGraph( p_sys );
425 if( OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
427 msg_Err( p_this, "can't open video");
431 /* Check if we can handle the demuxing ourselves or need to spawn
432 * a demuxer module */
433 dshow_stream_t *p_stream = p_sys->pp_streams[p_sys->i_streams-1];
435 if( p_stream->mt.majortype == MEDIATYPE_Video )
437 if( /* Raw DV stream */
438 p_stream->i_fourcc == VLC_FOURCC('d','v','s','l') ||
439 p_stream->i_fourcc == VLC_FOURCC('d','v','s','d') ||
440 p_stream->i_fourcc == VLC_FOURCC('d','v','h','d') ||
441 /* Raw MPEG video stream */
442 p_stream->i_fourcc == VLC_FOURCC('m','p','2','v') )
448 /* Let the access (only) take care of that */
454 if( p_stream->mt.majortype == MEDIATYPE_Stream )
460 /* Let the access (only) take care of that */
464 var_Get( p_this, "dshow-tuner", &val );
467 /* FIXME: we do MEDIATYPE_Stream here so we don't do
469 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
470 p_stream->p_device_filter, 0 );
475 if( b_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
477 msg_Err( p_this, "can't open audio");
480 for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
482 var_Get( p_this, "dshow-video-input", &val );
484 p_sys->crossbar_routes[i].VideoInputIndex=val.i_int;
485 var_Get( p_this, "dshow-video-output", &val );
487 p_sys->crossbar_routes[i].VideoOutputIndex=val.i_int;
488 var_Get( p_this, "dshow-audio-input", &val );
490 p_sys->crossbar_routes[i].AudioInputIndex=val.i_int;
491 var_Get( p_this, "dshow-audio-output", &val );
493 p_sys->crossbar_routes[i].AudioOutputIndex=val.i_int;
495 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
496 LONG VideoInputIndex = p_sys->crossbar_routes[i].VideoInputIndex;
497 LONG VideoOutputIndex = p_sys->crossbar_routes[i].VideoOutputIndex;
498 LONG AudioInputIndex = p_sys->crossbar_routes[i].AudioInputIndex;
499 LONG AudioOutputIndex = p_sys->crossbar_routes[i].AudioOutputIndex;
501 if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
503 msg_Dbg( p_this, "crossbar at depth %d, routed video "
504 "output %ld to video input %ld", i, VideoOutputIndex,
507 if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
509 if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
512 msg_Dbg(p_this, "crossbar at depth %d, routed audio "
513 "output %ld to audio input %ld", i,
514 AudioOutputIndex, AudioInputIndex );
521 ** Show properties pages from other filters in graph
523 var_Get( p_this, "dshow-config", &val );
526 for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
528 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
531 if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
534 ShowPropertyPage( p_XF );
540 /* Initialize some data */
541 p_sys->i_current_stream = 0;
543 if( !p_sys->i_streams ) return VLC_EGENERIC;
548 /*****************************************************************************
549 * DemuxOpen: open direct show device as an access_demux module
550 *****************************************************************************/
551 static int DemuxOpen( vlc_object_t *p_this )
553 demux_t *p_demux = (demux_t *)p_this;
557 p_sys = (access_sys_t *)malloc( sizeof( access_sys_t ) );
558 memset( p_sys, 0, sizeof( access_sys_t ) );
559 p_demux->p_sys = (demux_sys_t *)p_sys;
561 if( CommonOpen( p_this, p_sys, VLC_TRUE ) != VLC_SUCCESS )
563 CommonClose( p_this, p_sys );
567 /* Everything is ready. Let's rock baby */
568 msg_Dbg( p_this, "Playing...");
569 p_sys->p_control->Run();
571 p_demux->pf_demux = Demux;
572 p_demux->pf_control = DemuxControl;
573 p_demux->info.i_update = 0;
574 p_demux->info.i_title = 0;
575 p_demux->info.i_seekpoint = 0;
577 for( i = 0; i < p_sys->i_streams; i++ )
579 dshow_stream_t *p_stream = p_sys->pp_streams[i];
582 if( p_stream->mt.majortype == MEDIATYPE_Video )
584 es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
586 fmt.video.i_width = p_stream->header.video.bmiHeader.biWidth;
587 fmt.video.i_height = p_stream->header.video.bmiHeader.biHeight;
588 fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
590 if( !p_stream->header.video.bmiHeader.biCompression )
592 /* RGB DIB are coded from bottom to top */
593 fmt.video.i_height = (unsigned int)(-(int)fmt.video.i_height);
596 /* Setup rgb mask for RGB formats */
597 if( p_stream->i_fourcc == VLC_FOURCC('R','V','2','4') )
599 /* This is in BGR format */
600 fmt.video.i_bmask = 0x00ff0000;
601 fmt.video.i_gmask = 0x0000ff00;
602 fmt.video.i_rmask = 0x000000ff;
605 if( p_stream->header.video.AvgTimePerFrame )
607 fmt.video.i_frame_rate = 10000000;
608 fmt.video.i_frame_rate_base =
609 p_stream->header.video.AvgTimePerFrame;
612 else if( p_stream->mt.majortype == MEDIATYPE_Audio )
614 es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
616 fmt.audio.i_channels = p_stream->header.audio.nChannels;
617 fmt.audio.i_rate = p_stream->header.audio.nSamplesPerSec;
618 fmt.audio.i_bitspersample = p_stream->header.audio.wBitsPerSample;
619 fmt.audio.i_blockalign = fmt.audio.i_channels *
620 fmt.audio.i_bitspersample / 8;
621 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
622 fmt.audio.i_bitspersample;
625 p_stream->p_es = es_out_Add( p_demux->out, &fmt );
631 /*****************************************************************************
632 * AccessOpen: open direct show device as an access module
633 *****************************************************************************/
634 static int AccessOpen( vlc_object_t *p_this )
636 access_t *p_access = (access_t*)p_this;
639 p_access->p_sys = p_sys = (access_sys_t *)malloc( sizeof( access_sys_t ) );
640 memset( p_sys, 0, sizeof( access_sys_t ) );
642 if( CommonOpen( p_this, p_sys, VLC_FALSE ) != VLC_SUCCESS )
644 CommonClose( p_this, p_sys );
648 dshow_stream_t *p_stream = p_sys->pp_streams[0];
650 /* Check if we need to force demuxers */
651 if( !p_access->psz_demux || !*p_access->psz_demux )
653 if( p_stream->i_fourcc == VLC_FOURCC('d','v','s','l') ||
654 p_stream->i_fourcc == VLC_FOURCC('d','v','s','d') ||
655 p_stream->i_fourcc == VLC_FOURCC('d','v','h','d') )
657 p_access->psz_demux = strdup( "rawdv" );
659 else if( p_stream->i_fourcc == VLC_FOURCC('m','p','2','v') )
661 p_access->psz_demux = "mpgv";
666 p_access->pf_read = NULL;
667 p_access->pf_block = ReadCompressed;
668 p_access->pf_control = AccessControl;
669 p_access->pf_seek = NULL;
670 p_access->info.i_update = 0;
671 p_access->info.i_size = 0;
672 p_access->info.i_pos = 0;
673 p_access->info.b_eof = VLC_FALSE;
674 p_access->info.i_title = 0;
675 p_access->info.i_seekpoint = 0;
676 p_access->p_sys = p_sys;
678 /* Everything is ready. Let's rock baby */
679 msg_Dbg( p_this, "Playing...");
680 p_sys->p_control->Run();
685 /*****************************************************************************
686 * CommonClose: close device
687 *****************************************************************************/
688 static void CommonClose( vlc_object_t *p_this, access_sys_t *p_sys )
690 msg_Dbg( p_this, "releasing DirectShow");
692 DeleteDirectShowGraph( p_sys );
694 /* Uninitialize OLE/COM */
697 for( int i = 0; i < p_sys->i_streams; i++ ) delete p_sys->pp_streams[i];
698 if( p_sys->i_streams ) free( p_sys->pp_streams );
700 vlc_mutex_destroy( &p_sys->lock );
701 vlc_cond_destroy( &p_sys->wait );
706 /*****************************************************************************
707 * AccessClose: close device
708 *****************************************************************************/
709 static void AccessClose( vlc_object_t *p_this )
711 access_t *p_access = (access_t *)p_this;
712 access_sys_t *p_sys = p_access->p_sys;
714 /* Stop capturing stuff */
715 p_sys->p_control->Stop();
717 CommonClose( p_this, p_sys );
720 /*****************************************************************************
721 * DemuxClose: close device
722 *****************************************************************************/
723 static void DemuxClose( vlc_object_t *p_this )
725 demux_t *p_demux = (demux_t *)p_this;
726 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
728 /* Stop capturing stuff */
729 p_sys->p_control->Stop();
731 CommonClose( p_this, p_sys );
734 /****************************************************************************
736 ****************************************************************************/
737 static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
738 IBaseFilter *p_filter,
739 CaptureFilter *p_capture_filter )
741 CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
743 AM_MEDIA_TYPE mediaType = p_input_pin->CustomGetMediaType();
745 if( p_sys->p_capture_graph_builder2 )
747 if( FAILED(p_sys->p_capture_graph_builder2->
748 RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
749 p_filter, 0, (IBaseFilter *)p_capture_filter )) )
754 // Sort out all the possible video inputs
755 // The class needs to be given the capture filters ANALOGVIDEO input pin
757 if( ( mediaType.majortype == MEDIATYPE_Video ||
758 mediaType.majortype == MEDIATYPE_Stream ) &&
759 SUCCEEDED(p_filter->EnumPins(&pins)) )
765 IKsPropertySet *pKs=0;
769 while( !Found && ( S_OK == pins->Next(1, &pP, &n) ) )
771 if( S_OK == pP->QueryPinInfo(&pinInfo) )
773 // is this pin an ANALOGVIDEOIN input pin?
774 if( pinInfo.dir == PINDIR_INPUT &&
775 pP->QueryInterface( IID_IKsPropertySet,
776 (void **)&pKs ) == S_OK )
778 if( pKs->Get( AMPROPSETID_Pin,
779 AMPROPERTY_PIN_CATEGORY, NULL, 0,
780 &guid, sizeof(GUID), &dw ) == S_OK )
782 if( guid == PIN_CATEGORY_ANALOGVIDEOIN )
784 // recursively search crossbar routes
785 FindCrossbarRoutes( p_this, p_sys, pP, 0 );
792 pinInfo.pFilter->Release();
802 IEnumPins *p_enumpins;
805 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;
807 while( S_OK == p_enumpins->Next( 1, &p_pin, NULL ) )
809 PIN_DIRECTION pin_dir;
810 p_pin->QueryDirection( &pin_dir );
812 if( pin_dir == PINDIR_OUTPUT &&
813 p_sys->p_graph->ConnectDirect( p_pin, (IPin *)p_input_pin,
817 p_enumpins->Release();
823 p_enumpins->Release();
829 * get fourcc priority from arbritary preference, the higher the better
831 static int GetFourCCPriority( int i_fourcc )
835 case VLC_FOURCC('I','4','2','0'):
836 case VLC_FOURCC('f','l','3','2'):
838 case VLC_FOURCC('Y','V','1','2'):
839 case VLC_FOURCC('a','r','a','w'):
841 case VLC_FOURCC('R','V','2','4'):
843 case VLC_FOURCC('Y','U','Y','2'):
844 case VLC_FOURCC('R','V','3','2'):
845 case VLC_FOURCC('R','G','B','A'):
852 #define MAX_MEDIA_TYPES 32
854 static int OpenDevice( vlc_object_t *p_this, access_sys_t *p_sys,
855 string devicename, vlc_bool_t b_audio )
857 /* See if device is already opened */
858 for( int i = 0; i < p_sys->i_streams; i++ )
860 if( devicename.size() &&
861 p_sys->pp_streams[i]->devicename == devicename )
868 list<string> list_devices;
870 /* Enumerate devices and display their names */
871 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
873 if( !list_devices.size() )
876 list<string>::iterator iter;
877 for( iter = list_devices.begin(); iter != list_devices.end(); iter++ )
878 msg_Dbg( p_this, "found device: %s", iter->c_str() );
880 /* If no device name was specified, pick the 1st one */
881 if( devicename.size() == 0 )
883 devicename = *list_devices.begin();
886 // Use the system device enumerator and class enumerator to find
887 // a capture/preview device, such as a desktop USB video camera.
888 IBaseFilter *p_device_filter =
889 FindCaptureDevice( p_this, &devicename, 0, b_audio );
890 if( p_device_filter )
891 msg_Dbg( p_this, "using device: %s", devicename.c_str() );
894 msg_Err( p_this, "can't use device: %s, unsupported device type",
895 devicename.c_str() );
896 intf_UserFatal( p_this, VLC_FALSE, _("Capturing failed"),
897 _("VLC cannot use the device \"%s\", because its device "
898 "type is not supported.") );
902 // Retreive acceptable media types supported by device
903 AM_MEDIA_TYPE media_types[MAX_MEDIA_TYPES];
905 EnumDeviceCaps( p_this, p_device_filter, b_audio ? 0 : p_sys->i_chroma,
906 p_sys->i_width, p_sys->i_height,
907 0, 0, 0, media_types, MAX_MEDIA_TYPES );
909 AM_MEDIA_TYPE *mt = NULL;
911 if( media_count > 0 )
913 mt = (AM_MEDIA_TYPE *)CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE) * media_count);
915 // Order and copy returned media types according to arbitrary
917 for( size_t c = 0; c < media_count; c++ )
920 GetFourCCPriority(GetFourCCFromMediaType(media_types[c]));
921 size_t slot_copy = c;
922 for( size_t d = c+1; d < media_count; d++ )
925 GetFourCCPriority(GetFourCCFromMediaType(media_types[d]));
926 if( priority > slot_priority )
928 slot_priority = priority;
934 mt[c] = media_types[slot_copy];
935 media_types[slot_copy] = media_types[c];
939 mt[c] = media_types[c];
945 msg_Err( p_this, "capture device '%s' does not support required parameters !", devicename.c_str() );
946 intf_UserFatal( p_this, VLC_FALSE, _("Capturing failed"),
947 _("The capture device \"%s\" does not support the "
948 "required parameters."), devicename.c_str() );
949 p_device_filter->Release();
953 /* Create and add our capture filter */
954 CaptureFilter *p_capture_filter =
955 new CaptureFilter( p_this, p_sys, mt, media_count );
956 p_sys->p_graph->AddFilter( p_capture_filter, 0 );
958 /* Add the device filter to the graph (seems necessary with VfW before
959 * accessing pin attributes). */
960 p_sys->p_graph->AddFilter( p_device_filter, 0 );
962 /* Attempt to connect one of this device's capture output pins */
963 msg_Dbg( p_this, "connecting filters" );
964 if( ConnectFilters( p_this, p_sys, p_device_filter, p_capture_filter ) )
967 msg_Dbg( p_this, "filters connected successfully !" );
969 dshow_stream_t dshow_stream;
970 dshow_stream.b_pts = VLC_FALSE;
971 dshow_stream.p_es = 0;
973 p_capture_filter->CustomGetPin()->CustomGetMediaType();
975 /* Show Device properties. Done here so the VLC stream is setup with
976 * the proper parameters. */
978 var_Get( p_this, "dshow-config", &val );
981 ShowDeviceProperties( p_this, p_sys->p_capture_graph_builder2,
982 p_device_filter, b_audio );
985 ConfigTuner( p_this, p_sys->p_capture_graph_builder2,
988 var_Get( p_this, "dshow-tuner", &val );
989 if( val.b_bool && dshow_stream.mt.majortype != MEDIATYPE_Stream )
991 /* FIXME: we do MEDIATYPE_Stream later so we don't do it twice. */
992 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
993 p_device_filter, b_audio );
997 p_capture_filter->CustomGetPin()->CustomGetMediaType();
999 dshow_stream.i_fourcc = GetFourCCFromMediaType( dshow_stream.mt );
1000 if( dshow_stream.i_fourcc )
1002 if( dshow_stream.mt.majortype == MEDIATYPE_Video )
1004 dshow_stream.header.video =
1005 *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
1006 msg_Dbg( p_this, "MEDIATYPE_Video" );
1007 msg_Dbg( p_this, "selected video pin accepts format: %4.4s",
1008 (char *)&dshow_stream.i_fourcc);
1010 else if( dshow_stream.mt.majortype == MEDIATYPE_Audio )
1012 dshow_stream.header.audio =
1013 *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
1014 msg_Dbg( p_this, "MEDIATYPE_Audio" );
1015 msg_Dbg( p_this, "selected audio pin accepts format: %4.4s",
1016 (char *)&dshow_stream.i_fourcc);
1018 else if( dshow_stream.mt.majortype == MEDIATYPE_Stream )
1020 msg_Dbg( p_this, "MEDIATYPE_Stream" );
1021 msg_Dbg( p_this, "selected stream pin accepts format: %4.4s",
1022 (char *)&dshow_stream.i_fourcc);
1026 msg_Dbg( p_this, "unknown stream majortype" );
1030 /* Add directshow elementary stream to our list */
1031 dshow_stream.p_device_filter = p_device_filter;
1032 dshow_stream.p_capture_filter = p_capture_filter;
1034 p_sys->pp_streams = (dshow_stream_t **)realloc( p_sys->pp_streams,
1035 sizeof(dshow_stream_t *) * (p_sys->i_streams + 1) );
1036 p_sys->pp_streams[p_sys->i_streams] = new dshow_stream_t;
1037 *p_sys->pp_streams[p_sys->i_streams++] = dshow_stream;
1044 /* Remove filters from graph */
1045 p_sys->p_graph->RemoveFilter( p_device_filter );
1046 p_sys->p_graph->RemoveFilter( p_capture_filter );
1048 /* Release objects */
1049 p_device_filter->Release();
1050 p_capture_filter->Release();
1052 return VLC_EGENERIC;
1055 static IBaseFilter *
1056 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
1057 list<string> *p_listdevices, vlc_bool_t b_audio )
1059 IBaseFilter *p_base_filter = NULL;
1060 IMoniker *p_moniker = NULL;
1064 /* Create the system device enumerator */
1065 ICreateDevEnum *p_dev_enum = NULL;
1067 hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
1068 IID_ICreateDevEnum, (void **)&p_dev_enum );
1071 msg_Err( p_this, "failed to create the device enumerator (0x%lx)", hr);
1075 /* Create an enumerator for the video capture devices */
1076 IEnumMoniker *p_class_enum = NULL;
1078 hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
1081 hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
1083 p_dev_enum->Release();
1086 msg_Err( p_this, "failed to create the class enumerator (0x%lx)", hr );
1090 /* If there are no enumerators for the requested type, then
1091 * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
1092 if( p_class_enum == NULL )
1094 msg_Err( p_this, "no capture device was detected" );
1098 /* Enumerate the devices */
1100 /* Note that if the Next() call succeeds but there are no monikers,
1101 * it will return S_FALSE (which is not a failure). Therefore, we check
1102 * that the return code is S_OK instead of using SUCCEEDED() macro. */
1104 while( p_class_enum->Next( 1, &p_moniker, &i_fetched ) == S_OK )
1106 /* Getting the property page to get the device name */
1107 IPropertyBag *p_bag;
1108 hr = p_moniker->BindToStorage( 0, 0, IID_IPropertyBag,
1114 hr = p_bag->Read( L"FriendlyName", &var, NULL );
1118 int i_convert = WideCharToMultiByte(CP_ACP, 0, var.bstrVal,
1119 SysStringLen(var.bstrVal), NULL, 0, NULL, NULL);
1120 char *p_buf = (char *)alloca( i_convert+1 ); p_buf[0] = 0;
1121 WideCharToMultiByte( CP_ACP, 0, var.bstrVal,
1122 SysStringLen(var.bstrVal), p_buf, i_convert, NULL, NULL );
1123 SysFreeString(var.bstrVal);
1124 p_buf[i_convert] = '\0';
1126 if( p_listdevices ) p_listdevices->push_back( p_buf );
1128 if( p_devicename && *p_devicename == string(p_buf) )
1130 /* Bind Moniker to a filter object */
1131 hr = p_moniker->BindToObject( 0, 0, IID_IBaseFilter,
1132 (void **)&p_base_filter );
1135 msg_Err( p_this, "couldn't bind moniker to filter "
1136 "object (0x%lx)", hr );
1137 p_moniker->Release();
1138 p_class_enum->Release();
1141 p_moniker->Release();
1142 p_class_enum->Release();
1143 return p_base_filter;
1148 p_moniker->Release();
1151 p_class_enum->Release();
1155 static size_t EnumDeviceCaps( vlc_object_t *p_this, IBaseFilter *p_filter,
1156 int i_fourcc, int i_width, int i_height,
1157 int i_channels, int i_samplespersec,
1158 int i_bitspersample, AM_MEDIA_TYPE *mt,
1161 IEnumPins *p_enumpins;
1163 IEnumMediaTypes *p_enummt;
1164 size_t mt_count = 0;
1166 LONGLONG i_AvgTimePerFrame = 0;
1167 float r_fps = var_GetFloat( p_this, "dshow-fps" );
1169 i_AvgTimePerFrame = 10000000000LL/(LONGLONG)(r_fps*1000.0f);
1171 if( FAILED(p_filter->EnumPins( &p_enumpins )) )
1173 msg_Dbg( p_this, "EnumDeviceCaps failed: no pin enumeration !");
1177 while( S_OK == p_enumpins->Next( 1, &p_output_pin, NULL ) )
1181 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1183 msg_Dbg( p_this, "EnumDeviceCaps: %s pin: %S",
1184 info.dir == PINDIR_INPUT ? "input" : "output",
1186 if( info.pFilter ) info.pFilter->Release();
1189 p_output_pin->Release();
1192 p_enumpins->Reset();
1194 while( !mt_count && p_enumpins->Next( 1, &p_output_pin, NULL ) == S_OK )
1198 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1200 if( info.pFilter ) info.pFilter->Release();
1201 if( info.dir == PINDIR_INPUT )
1203 p_output_pin->Release();
1206 msg_Dbg( p_this, "EnumDeviceCaps: trying pin %S", info.achName );
1209 AM_MEDIA_TYPE *p_mt;
1212 ** Configure pin with a default compatible media if possible
1215 IAMStreamConfig *pSC;
1216 if( SUCCEEDED(p_output_pin->QueryInterface( IID_IAMStreamConfig,
1219 int piCount, piSize;
1220 if( SUCCEEDED(pSC->GetNumberOfCapabilities(&piCount, &piSize)) )
1222 BYTE *pSCC= (BYTE *)CoTaskMemAlloc(piSize);
1225 int i_priority = -1;
1226 for( int i=0; i<piCount; ++i )
1228 if( SUCCEEDED(pSC->GetStreamCaps(i, &p_mt, pSCC)) )
1230 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1231 int i_current_priority = GetFourCCPriority(i_current_fourcc);
1233 if( (i_fourcc && (i_current_fourcc != i_fourcc))
1234 || (i_priority > i_current_priority) )
1236 // unwanted chroma, try next media type
1237 FreeMediaType( *p_mt );
1238 CoTaskMemFree( (PVOID)p_mt );
1242 if( MEDIATYPE_Video == p_mt->majortype
1243 && FORMAT_VideoInfo == p_mt->formattype )
1245 VIDEO_STREAM_CONFIG_CAPS *pVSCC = reinterpret_cast<VIDEO_STREAM_CONFIG_CAPS*>(pSCC);
1246 VIDEOINFOHEADER *pVih = reinterpret_cast<VIDEOINFOHEADER*>(p_mt->pbFormat);
1248 if( i_AvgTimePerFrame )
1250 if( pVSCC->MinFrameInterval > i_AvgTimePerFrame
1251 || i_AvgTimePerFrame > pVSCC->MaxFrameInterval )
1253 // required frame rate not compatible, try next media type
1254 FreeMediaType( *p_mt );
1255 CoTaskMemFree( (PVOID)p_mt );
1258 pVih->AvgTimePerFrame = i_AvgTimePerFrame;
1263 if( i_width % pVSCC->OutputGranularityX
1264 || pVSCC->MinOutputSize.cx > i_width
1265 || i_width > pVSCC->MaxOutputSize.cx )
1267 // required width not compatible, try next media type
1268 FreeMediaType( *p_mt );
1269 CoTaskMemFree( (PVOID)p_mt );
1272 pVih->bmiHeader.biWidth = i_width;
1277 if( i_height % pVSCC->OutputGranularityY
1278 || pVSCC->MinOutputSize.cy > i_height
1279 || i_height > pVSCC->MaxOutputSize.cy )
1281 // required height not compatible, try next media type
1282 FreeMediaType( *p_mt );
1283 CoTaskMemFree( (PVOID)p_mt );
1286 pVih->bmiHeader.biHeight = i_height;
1289 // Set the sample size and image size.
1290 // (Round the image width up to a DWORD boundary.)
1291 p_mt->lSampleSize = pVih->bmiHeader.biSizeImage =
1292 ((pVih->bmiHeader.biWidth + 3) & ~3) *
1293 pVih->bmiHeader.biHeight * (pVih->bmiHeader.biBitCount>>3);
1295 // no cropping, use full video input buffer
1296 memset(&(pVih->rcSource), 0, sizeof(RECT));
1297 memset(&(pVih->rcTarget), 0, sizeof(RECT));
1299 // select this format as default
1300 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1302 i_priority = i_current_priority;
1304 // no need to check any more media types
1308 else if( p_mt->majortype == MEDIATYPE_Audio
1309 && p_mt->formattype == FORMAT_WaveFormatEx )
1311 AUDIO_STREAM_CONFIG_CAPS *pASCC = reinterpret_cast<AUDIO_STREAM_CONFIG_CAPS*>(pSCC);
1312 WAVEFORMATEX *pWfx = reinterpret_cast<WAVEFORMATEX*>(p_mt->pbFormat);
1314 if( i_current_fourcc && (WAVE_FORMAT_PCM == pWfx->wFormatTag) )
1316 int val = i_channels;
1320 if( val % pASCC->ChannelsGranularity
1321 || (unsigned int)val < pASCC->MinimumChannels
1322 || (unsigned int)val > pASCC->MaximumChannels )
1324 // required number channels not available, try next media type
1325 FreeMediaType( *p_mt );
1326 CoTaskMemFree( (PVOID)p_mt );
1329 pWfx->nChannels = val;
1331 val = i_samplespersec;
1335 if( val % pASCC->SampleFrequencyGranularity
1336 || (unsigned int)val < pASCC->MinimumSampleFrequency
1337 || (unsigned int)val > pASCC->MaximumSampleFrequency )
1339 // required sampling rate not available, try next media type
1340 FreeMediaType( *p_mt );
1341 CoTaskMemFree( (PVOID)p_mt );
1344 pWfx->nSamplesPerSec = val;
1346 val = i_bitspersample;
1349 if( VLC_FOURCC('f', 'l', '3', '2') == i_current_fourcc )
1355 if( val % pASCC->BitsPerSampleGranularity
1356 || (unsigned int)val < pASCC->MinimumBitsPerSample
1357 || (unsigned int)val > pASCC->MaximumBitsPerSample )
1359 // required sample size not available, try next media type
1360 FreeMediaType( *p_mt );
1361 CoTaskMemFree( (PVOID)p_mt );
1365 pWfx->wBitsPerSample = val;
1366 pWfx->nBlockAlign = (pWfx->wBitsPerSample * pWfx->nChannels)/8;
1367 pWfx->nAvgBytesPerSec = pWfx->nSamplesPerSec * pWfx->nBlockAlign;
1369 // select this format as default
1370 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1372 i_priority = i_current_priority;
1376 FreeMediaType( *p_mt );
1377 CoTaskMemFree( (PVOID)p_mt );
1380 CoTaskMemFree( (LPVOID)pSCC );
1381 if( i_priority >= 0 )
1382 msg_Dbg( p_this, "EnumDeviceCaps: input pin default format configured");
1389 ** Probe pin for available medias (may be a previously configured one)
1392 if( FAILED( p_output_pin->EnumMediaTypes( &p_enummt ) ) )
1394 p_output_pin->Release();
1398 while( p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1400 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1401 if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Video
1402 && p_mt->formattype == FORMAT_VideoInfo )
1404 int i_current_width = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1405 int i_current_height = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1406 LONGLONG i_current_atpf = ((VIDEOINFOHEADER *)p_mt->pbFormat)->AvgTimePerFrame;
1408 if( i_current_height < 0 )
1409 i_current_height = -i_current_height;
1411 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1412 "accepts chroma: %4.4s, width:%i, height:%i, fps:%f",
1413 (char *)&i_current_fourcc, i_current_width,
1414 i_current_height, (10000000.0f/((float)i_current_atpf)) );
1416 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1417 ( !i_width || i_width == i_current_width ) &&
1418 ( !i_height || i_height == i_current_height ) &&
1419 ( !i_AvgTimePerFrame || i_AvgTimePerFrame == i_current_atpf ) &&
1423 mt[mt_count++] = *p_mt;
1425 else FreeMediaType( *p_mt );
1427 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Audio
1428 && p_mt->formattype == FORMAT_WaveFormatEx)
1430 int i_current_channels =
1431 ((WAVEFORMATEX *)p_mt->pbFormat)->nChannels;
1432 int i_current_samplespersec =
1433 ((WAVEFORMATEX *)p_mt->pbFormat)->nSamplesPerSec;
1434 int i_current_bitspersample =
1435 ((WAVEFORMATEX *)p_mt->pbFormat)->wBitsPerSample;
1437 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1438 "accepts format: %4.4s, channels:%i, "
1439 "samples/sec:%i bits/sample:%i",
1440 (char *)&i_current_fourcc, i_current_channels,
1441 i_current_samplespersec, i_current_bitspersample);
1443 if( (!i_channels || i_channels == i_current_channels) &&
1444 (!i_samplespersec ||
1445 i_samplespersec == i_current_samplespersec) &&
1446 (!i_bitspersample ||
1447 i_bitspersample == i_current_bitspersample) &&
1451 mt[mt_count++] = *p_mt;
1453 /* Setup a few properties like the audio latency */
1454 IAMBufferNegotiation *p_ambuf;
1455 if( SUCCEEDED( p_output_pin->QueryInterface(
1456 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
1458 ALLOCATOR_PROPERTIES AllocProp;
1459 AllocProp.cbAlign = -1;
1461 /* 100 ms of latency */
1462 AllocProp.cbBuffer = i_current_channels *
1463 i_current_samplespersec *
1464 i_current_bitspersample / 8 / 10;
1466 AllocProp.cbPrefix = -1;
1467 AllocProp.cBuffers = -1;
1468 p_ambuf->SuggestAllocatorProperties( &AllocProp );
1472 else FreeMediaType( *p_mt );
1474 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Stream )
1476 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1477 "accepts stream format: %4.4s",
1478 (char *)&i_current_fourcc );
1480 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1484 mt[mt_count++] = *p_mt;
1485 i_fourcc = i_current_fourcc;
1487 else FreeMediaType( *p_mt );
1491 char *psz_type = "unknown";
1492 if( p_mt->majortype == MEDIATYPE_Video ) psz_type = "video";
1493 if( p_mt->majortype == MEDIATYPE_Audio ) psz_type = "audio";
1494 if( p_mt->majortype == MEDIATYPE_Stream ) psz_type = "stream";
1495 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: unsupported format "
1496 "(%s %4.4s)", psz_type, (char *)&p_mt->subtype );
1498 FreeMediaType( *p_mt );
1500 CoTaskMemFree( (PVOID)p_mt );
1503 if( !mt_count && p_enummt->Reset() == S_OK )
1505 // VLC did not find any supported MEDIATYPE for this output pin.
1506 // However the graph builder might insert converter filters in
1507 // the graph if we use a different codec in VLC filter input pin.
1508 // however, in order to avoid nasty surprises, make use of this
1509 // facility only for known unsupported codecs.
1511 while( !mt_count && p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1513 // the first four bytes of subtype GUID contains the codec FOURCC
1514 const char *pfcc = (char *)&p_mt->subtype;
1515 int i_current_fourcc = VLC_FOURCC(pfcc[0], pfcc[1], pfcc[2], pfcc[3]);
1516 if( VLC_FOURCC('H','C','W','2') == i_current_fourcc
1517 && p_mt->majortype == MEDIATYPE_Video )
1519 // output format for 'Hauppauge WinTV PVR PCI II Capture'
1520 // try I420 as an input format
1521 i_current_fourcc = VLC_FOURCC('I','4','2','0');
1522 if( !i_fourcc || i_fourcc == i_current_fourcc )
1524 // return alternative media type
1528 mtr.majortype = MEDIATYPE_Video;
1529 mtr.subtype = MEDIASUBTYPE_I420;
1530 mtr.bFixedSizeSamples = TRUE;
1531 mtr.bTemporalCompression = FALSE;
1533 mtr.formattype = FORMAT_VideoInfo;
1534 mtr.cbFormat = sizeof(vh);
1535 mtr.pbFormat = (BYTE *)&vh;
1537 memset(&vh, 0, sizeof(vh));
1539 vh.bmiHeader.biSize = sizeof(vh.bmiHeader);
1540 vh.bmiHeader.biWidth = i_width > 0 ? i_width :
1541 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1542 vh.bmiHeader.biHeight = i_height > 0 ? i_height :
1543 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1544 vh.bmiHeader.biPlanes = 3;
1545 vh.bmiHeader.biBitCount = 12;
1546 vh.bmiHeader.biCompression = VLC_FOURCC('I','4','2','0');
1547 vh.bmiHeader.biSizeImage = vh.bmiHeader.biWidth * 12 *
1548 vh.bmiHeader.biHeight / 8;
1549 mtr.lSampleSize = vh.bmiHeader.biSizeImage;
1551 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: using 'I420' in place of unsupported format 'HCW2'");
1553 if( SUCCEEDED(CopyMediaType(mt+mt_count, &mtr)) )
1557 FreeMediaType( *p_mt );
1561 p_enummt->Release();
1562 p_output_pin->Release();
1565 p_enumpins->Release();
1569 /*****************************************************************************
1570 * ReadCompressed: reads compressed (MPEG/DV) data from the device.
1571 *****************************************************************************
1572 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1574 *****************************************************************************/
1575 static block_t *ReadCompressed( access_t *p_access )
1577 access_sys_t *p_sys = p_access->p_sys;
1578 dshow_stream_t *p_stream = NULL;
1579 VLCMediaSample sample;
1581 /* Read 1 DV/MPEG frame (they contain the video and audio data) */
1583 /* There must be only 1 elementary stream to produce a valid stream
1584 * of MPEG or DV data */
1585 p_stream = p_sys->pp_streams[0];
1589 if( p_access->b_die || p_access->b_error ) return 0;
1591 /* Get new sample/frame from the elementary stream (blocking). */
1592 vlc_mutex_lock( &p_sys->lock );
1594 if( p_stream->p_capture_filter->CustomGetPin()
1595 ->CustomGetSample( &sample ) != S_OK )
1597 /* No data available. Wait until some data has arrived */
1598 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1599 vlc_mutex_unlock( &p_sys->lock );
1603 vlc_mutex_unlock( &p_sys->lock );
1610 int i_data_size = sample.p_sample->GetActualDataLength();
1612 if( !i_data_size || !(p_block = block_New( p_access, i_data_size )) )
1614 sample.p_sample->Release();
1618 sample.p_sample->GetPointer( &p_data );
1619 p_access->p_libvlc->pf_memcpy( p_block->p_buffer, p_data, i_data_size );
1620 sample.p_sample->Release();
1622 /* The caller got what he wanted */
1626 return 0; /* never reached */
1629 /****************************************************************************
1631 ****************************************************************************/
1632 static int Demux( demux_t *p_demux )
1634 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
1635 dshow_stream_t *p_stream = NULL;
1636 VLCMediaSample sample;
1637 int i_data_size, i_stream;
1641 vlc_mutex_lock( &p_sys->lock );
1643 /* Try to grab an audio sample (audio has a higher priority) */
1644 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1646 p_stream = p_sys->pp_streams[i_stream];
1647 if( p_stream->mt.majortype == MEDIATYPE_Audio &&
1648 p_stream->p_capture_filter &&
1649 p_stream->p_capture_filter->CustomGetPin()
1650 ->CustomGetSample( &sample ) == S_OK )
1655 /* Try to grab a video sample */
1656 if( i_stream == p_sys->i_streams )
1658 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1660 p_stream = p_sys->pp_streams[i_stream];
1661 if( p_stream->p_capture_filter &&
1662 p_stream->p_capture_filter->CustomGetPin()
1663 ->CustomGetSample( &sample ) == S_OK )
1670 vlc_mutex_unlock( &p_sys->lock );
1672 if( i_stream == p_sys->i_streams )
1674 /* Sleep so we do not consume all the cpu, 10ms seems
1675 * like a good value (100fps) */
1683 i_data_size = sample.p_sample->GetActualDataLength();
1684 sample.p_sample->GetPointer( &p_data );
1686 REFERENCE_TIME i_pts, i_end_date;
1687 HRESULT hr = sample.p_sample->GetTime( &i_pts, &i_end_date );
1688 if( hr != VFW_S_NO_STOP_TIME && hr != S_OK ) i_pts = 0;
1692 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
1694 /* Use our data timestamp */
1695 i_pts = sample.i_timestamp;
1696 p_stream->b_pts = VLC_TRUE;
1700 i_pts /= 10; /* Dshow works with 100 nano-seconds resolution */
1703 msg_Dbg( p_demux, "Read() stream: %i, size: %i, PTS: "I64Fd,
1704 i_stream, i_data_size, i_pts );
1707 p_block = block_New( p_demux, i_data_size );
1708 p_demux->p_libvlc->pf_memcpy( p_block->p_buffer, p_data, i_data_size );
1709 p_block->i_pts = p_block->i_dts = i_pts;
1710 sample.p_sample->Release();
1712 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pts > 0 ? i_pts : 0 );
1713 es_out_Send( p_demux->out, p_stream->p_es, p_block );
1718 /*****************************************************************************
1720 *****************************************************************************/
1721 static int AccessControl( access_t *p_access, int i_query, va_list args )
1723 vlc_bool_t *pb_bool;
1730 case ACCESS_CAN_SEEK:
1731 case ACCESS_CAN_FASTSEEK:
1732 case ACCESS_CAN_PAUSE:
1733 case ACCESS_CAN_CONTROL_PACE:
1734 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1735 *pb_bool = VLC_FALSE;
1739 case ACCESS_GET_MTU:
1740 pi_int = (int*)va_arg( args, int * );
1744 case ACCESS_GET_PTS_DELAY:
1745 pi_64 = (int64_t*)va_arg( args, int64_t * );
1746 *pi_64 = (int64_t)var_GetInteger( p_access, "dshow-caching" ) * 1000;
1750 case ACCESS_SET_PAUSE_STATE:
1751 case ACCESS_GET_TITLE_INFO:
1752 case ACCESS_SET_TITLE:
1753 case ACCESS_SET_SEEKPOINT:
1754 case ACCESS_SET_PRIVATE_ID_STATE:
1755 return VLC_EGENERIC;
1758 msg_Warn( p_access, "unimplemented query in control" );
1759 return VLC_EGENERIC;
1765 /****************************************************************************
1767 ****************************************************************************/
1768 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1775 /* Special for access_demux */
1776 case DEMUX_CAN_PAUSE:
1777 case DEMUX_SET_PAUSE_STATE:
1778 case DEMUX_CAN_CONTROL_PACE:
1779 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1783 case DEMUX_GET_PTS_DELAY:
1784 pi64 = (int64_t*)va_arg( args, int64_t * );
1785 *pi64 = (int64_t)var_GetInteger( p_demux, "dshow-caching" ) * 1000;
1788 case DEMUX_GET_TIME:
1789 pi64 = (int64_t*)va_arg( args, int64_t * );
1793 /* TODO implement others */
1795 return VLC_EGENERIC;
1798 return VLC_EGENERIC;
1801 /*****************************************************************************
1802 * config variable callback
1803 *****************************************************************************/
1804 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1805 vlc_value_t newval, vlc_value_t oldval, void * )
1807 module_config_t *p_item;
1808 vlc_bool_t b_audio = VLC_FALSE;
1811 p_item = config_FindConfig( p_this, psz_name );
1812 if( !p_item ) return VLC_SUCCESS;
1814 if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = VLC_TRUE;
1816 /* Clear-up the current list */
1817 if( p_item->i_list )
1819 /* Keep the 2 first entries */
1820 for( i = 2; i < p_item->i_list; i++ )
1822 free( const_cast<char *>(p_item->ppsz_list[i]) );
1823 free( const_cast<char *>(p_item->ppsz_list_text[i]) );
1825 /* TODO: Remove when no more needed */
1826 p_item->ppsz_list[i] = NULL;
1827 p_item->ppsz_list_text[i] = NULL;
1831 /* Find list of devices */
1832 list<string> list_devices;
1834 /* Initialize OLE/COM */
1837 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1839 /* Uninitialize OLE/COM */
1842 if( !list_devices.size() ) return VLC_SUCCESS;
1845 (const char **)realloc( p_item->ppsz_list,
1846 (list_devices.size()+3) * sizeof(char *) );
1847 p_item->ppsz_list_text =
1848 (const char **)realloc( p_item->ppsz_list_text,
1849 (list_devices.size()+3) * sizeof(char *) );
1851 list<string>::iterator iter;
1852 for( iter = list_devices.begin(), i = 2; iter != list_devices.end();
1855 p_item->ppsz_list[i] = strdup( iter->c_str() );
1856 p_item->ppsz_list_text[i] = NULL;
1859 p_item->ppsz_list[i] = NULL;
1860 p_item->ppsz_list_text[i] = NULL;
1862 /* Signal change to the interface */
1863 p_item->b_dirty = VLC_TRUE;
1868 static int ConfigDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1869 vlc_value_t newval, vlc_value_t oldval, void * )
1871 module_config_t *p_item;
1872 vlc_bool_t b_audio = VLC_FALSE;
1874 /* Initialize OLE/COM */
1877 p_item = config_FindConfig( p_this, psz_name );
1878 if( !p_item ) return VLC_SUCCESS;
1880 if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = VLC_TRUE;
1884 if( newval.psz_string && *newval.psz_string )
1886 devicename = newval.psz_string;
1890 /* If no device name was specified, pick the 1st one */
1891 list<string> list_devices;
1893 /* Enumerate devices */
1894 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1895 if( !list_devices.size() ) return VLC_EGENERIC;
1896 devicename = *list_devices.begin();
1899 IBaseFilter *p_device_filter =
1900 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
1901 if( p_device_filter )
1903 ShowPropertyPage( p_device_filter );
1904 p_device_filter->Release();
1908 /* Uninitialize OLE/COM */
1911 msg_Err( p_this, "didn't find device: %s", devicename.c_str() );
1912 return VLC_EGENERIC;
1915 /* Uninitialize OLE/COM */
1921 /*****************************************************************************
1923 *****************************************************************************/
1924 static void ShowPropertyPage( IUnknown *obj )
1926 ISpecifyPropertyPages *p_spec;
1929 HRESULT hr = obj->QueryInterface( IID_ISpecifyPropertyPages,
1931 if( FAILED(hr) ) return;
1933 if( SUCCEEDED(p_spec->GetPages( &cauuid )) )
1935 if( cauuid.cElems > 0 )
1937 HWND hwnd_desktop = ::GetDesktopWindow();
1939 OleCreatePropertyFrame( hwnd_desktop, 30, 30, NULL, 1, &obj,
1940 cauuid.cElems, cauuid.pElems, 0, 0, NULL );
1942 CoTaskMemFree( cauuid.pElems );
1948 static void ShowDeviceProperties( vlc_object_t *p_this,
1949 ICaptureGraphBuilder2 *p_graph,
1950 IBaseFilter *p_device_filter,
1951 vlc_bool_t b_audio )
1954 msg_Dbg( p_this, "configuring Device Properties" );
1957 * Video or audio capture filter page
1959 ShowPropertyPage( p_device_filter );
1964 if( p_graph && b_audio )
1966 IAMStreamConfig *p_SC;
1968 msg_Dbg( p_this, "showing WDM Audio Configuration Pages" );
1970 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
1971 &MEDIATYPE_Audio, p_device_filter,
1972 IID_IAMStreamConfig, (void **)&p_SC );
1975 ShowPropertyPage(p_SC);
1983 HRESULT hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
1984 &MEDIATYPE_Audio, p_device_filter,
1985 IID_IAMTVAudio, (void **)&p_TVA );
1988 ShowPropertyPage(p_TVA);
1996 if( p_graph && !b_audio )
1998 IAMStreamConfig *p_SC;
2000 msg_Dbg( p_this, "showing WDM Video Configuration Pages" );
2002 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2003 &MEDIATYPE_Interleaved, p_device_filter,
2004 IID_IAMStreamConfig, (void **)&p_SC );
2007 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2008 &MEDIATYPE_Video, p_device_filter,
2009 IID_IAMStreamConfig, (void **)&p_SC );
2014 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2015 &MEDIATYPE_Stream, p_device_filter,
2016 IID_IAMStreamConfig, (void **)&p_SC );
2021 ShowPropertyPage(p_SC);
2027 static void ShowTunerProperties( vlc_object_t *p_this,
2028 ICaptureGraphBuilder2 *p_graph,
2029 IBaseFilter *p_device_filter,
2030 vlc_bool_t b_audio )
2033 msg_Dbg( p_this, "configuring Tuner Properties" );
2035 if( !p_graph || b_audio ) return;
2038 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2039 &MEDIATYPE_Interleaved, p_device_filter,
2040 IID_IAMTVTuner, (void **)&p_TV );
2043 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2044 &MEDIATYPE_Video, p_device_filter,
2045 IID_IAMTVTuner, (void **)&p_TV );
2050 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2051 &MEDIATYPE_Stream, p_device_filter,
2052 IID_IAMTVTuner, (void **)&p_TV );
2057 ShowPropertyPage(p_TV);
2062 static void ConfigTuner( vlc_object_t *p_this, ICaptureGraphBuilder2 *p_graph,
2063 IBaseFilter *p_device_filter )
2065 int i_channel, i_country, i_input, i_amtuner_mode;
2070 if( !p_graph ) return;
2072 i_channel = var_GetInteger( p_this, "dshow-tuner-channel" );
2073 i_country = var_GetInteger( p_this, "dshow-tuner-country" );
2074 i_input = var_GetInteger( p_this, "dshow-tuner-input" );
2075 i_amtuner_mode = var_GetInteger( p_this, "dshow-amtuner-mode" );
2077 if( !i_channel && !i_country && !i_input ) return; /* Nothing to do */
2079 msg_Dbg( p_this, "tuner config: channel %i, country %i, input type %i",
2080 i_channel, i_country, i_input );
2082 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Interleaved,
2083 p_device_filter, IID_IAMTVTuner,
2087 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
2088 p_device_filter, IID_IAMTVTuner,
2094 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Stream,
2095 p_device_filter, IID_IAMTVTuner,
2101 msg_Dbg( p_this, "couldn't find tuner interface" );
2105 hr = p_TV->GetAvailableModes( &l_modes );
2106 if( SUCCEEDED(hr) && (l_modes & i_amtuner_mode) )
2108 hr = p_TV->put_Mode( (AMTunerModeType)i_amtuner_mode );
2111 if( i_input == 1 ) p_TV->put_InputType( 0, TunerInputCable );
2112 else if( i_input == 2 ) p_TV->put_InputType( 0, TunerInputAntenna );
2114 p_TV->put_CountryCode( i_country );
2115 p_TV->put_Channel( i_channel, AMTUNER_SUBCHAN_NO_TUNE,
2116 AMTUNER_SUBCHAN_NO_TUNE );