1 /*****************************************************************************
2 * dshow.cpp : DirectShow access module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002, 2003 VideoLAN
7 * Author: Gildas Bazin <gbazin@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc/input.h>
38 /*****************************************************************************
39 * Access: local prototypes
40 *****************************************************************************/
41 static block_t *ReadCompressed( access_t * );
42 static int AccessControl ( access_t *, int, va_list );
44 static int Demux ( demux_t * );
45 static int DemuxControl( demux_t *, int, va_list );
47 static int OpenDevice( vlc_object_t *, access_sys_t *, string, vlc_bool_t );
48 static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
49 list<string> *, vlc_bool_t );
50 static size_t EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
51 int, int, int, int, int, int,
52 AM_MEDIA_TYPE *mt, size_t );
53 static bool ConnectFilters( vlc_object_t *, access_sys_t *,
54 IBaseFilter *, CaptureFilter * );
55 static int FindDevicesCallback( vlc_object_t *, char const *,
56 vlc_value_t, vlc_value_t, void * );
57 static int ConfigDevicesCallback( vlc_object_t *, char const *,
58 vlc_value_t, vlc_value_t, void * );
60 static void ShowPropertyPage( IUnknown * );
61 static void ShowDeviceProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
62 IBaseFilter *, vlc_bool_t );
63 static void ShowTunerProperties( vlc_object_t *, ICaptureGraphBuilder2 *,
64 IBaseFilter *, vlc_bool_t );
65 static void ConfigTuner( vlc_object_t *, ICaptureGraphBuilder2 *,
68 /*****************************************************************************
70 *****************************************************************************/
71 static char *ppsz_vdev[] = { "", "none" };
72 static char *ppsz_vdev_text[] = { N_("Default"), N_("None") };
73 static char *ppsz_adev[] = { "", "none" };
74 static char *ppsz_adev_text[] = { N_("Default"), N_("None") };
75 static int pi_tuner_input[] = { 0, 1, 2 };
76 static char *ppsz_tuner_input_text[] =
77 {N_("Default"), N_("Cable"), N_("Antenna")};
79 #define CACHING_TEXT N_("Caching value in ms")
80 #define CACHING_LONGTEXT N_( \
81 "Allows you to modify the default caching value for DirectShow streams. " \
82 "This value should be set in milliseconds units." )
83 #define VDEV_TEXT N_("Video device name")
84 #define VDEV_LONGTEXT N_( \
85 "You can specify the name of the video device that will be used by the " \
86 "DirectShow plugin. If you don't specify anything, the default device " \
88 #define ADEV_TEXT N_("Audio device name")
89 #define ADEV_LONGTEXT N_( \
90 "You can specify the name of the audio device that will be used by the " \
91 "DirectShow plugin. If you don't specify anything, the default device " \
93 #define SIZE_TEXT N_("Video size")
94 #define SIZE_LONGTEXT N_( \
95 "You can specify the size of the video that will be displayed by the " \
96 "DirectShow plugin. If you don't specify anything the default size for " \
97 "your device will be used.")
98 #define CHROMA_TEXT N_("Video input chroma format")
99 #define CHROMA_LONGTEXT N_( \
100 "Force the DirectShow video input to use a specific chroma format " \
101 "(eg. I420 (default), RV24, etc.)")
102 #define FPS_TEXT N_("Video input frame rate")
103 #define FPS_LONGTEXT N_( \
104 "Force the DirectShow video input to use a specific frame rate" \
105 "(eg. 0 means default, 25, 29.97, 50, 59.94, etc.)")
106 #define CONFIG_TEXT N_("Device properties")
107 #define CONFIG_LONGTEXT N_( \
108 "Show the properties dialog of the selected device before starting the " \
110 #define TUNER_TEXT N_("Tuner properties")
111 #define TUNER_LONGTEXT N_( \
112 "Show the tuner properties [channel selection] page." )
113 #define CHANNEL_TEXT N_("Tuner TV Channel")
114 #define CHANNEL_LONGTEXT N_( \
115 "Allows you to set the TV channel the tuner will set to " \
116 "(0 means default)." )
117 #define COUNTRY_TEXT N_("Tuner country code")
118 #define COUNTRY_LONGTEXT N_( \
119 "Allows you to set the tuner country code that establishes the current " \
120 "channel-to-frequency mapping (0 means default)." )
121 #define TUNER_INPUT_TEXT N_("Tuner input type")
122 #define TUNER_INPUT_LONGTEXT N_( \
123 "Allows you to select the tuner input type (Cable/Antenna)." )
125 static int CommonOpen ( vlc_object_t *, access_sys_t *, vlc_bool_t );
126 static void CommonClose( vlc_object_t *, access_sys_t * );
128 static int AccessOpen ( vlc_object_t * );
129 static void AccessClose( vlc_object_t * );
131 static int DemuxOpen ( vlc_object_t * );
132 static void DemuxClose ( vlc_object_t * );
135 set_shortname( _("DirectShow") );
136 set_description( _("DirectShow input") );
137 set_category( CAT_INPUT );
138 set_subcategory( SUBCAT_INPUT_ACCESS );
139 add_integer( "dshow-caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
140 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
142 add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, VLC_FALSE);
143 change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback );
144 change_action_add( FindDevicesCallback, N_("Refresh list") );
145 change_action_add( ConfigDevicesCallback, N_("Configure") );
147 add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE);
148 change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback );
149 change_action_add( FindDevicesCallback, N_("Refresh list") );
150 change_action_add( ConfigDevicesCallback, N_("Configure") );
152 add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE);
154 add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
157 add_float( "dshow-fps", 0.0f, NULL, FPS_TEXT, FPS_LONGTEXT,
160 add_bool( "dshow-config", VLC_FALSE, NULL, CONFIG_TEXT, CONFIG_LONGTEXT,
163 add_bool( "dshow-tuner", VLC_FALSE, NULL, TUNER_TEXT, TUNER_LONGTEXT,
166 add_integer( "dshow-tuner-channel", 0, NULL, CHANNEL_TEXT,
167 CHANNEL_LONGTEXT, VLC_TRUE );
169 add_integer( "dshow-tuner-country", 0, NULL, COUNTRY_TEXT,
170 COUNTRY_LONGTEXT, VLC_TRUE );
172 add_integer( "dshow-tuner-input", 0, NULL, TUNER_INPUT_TEXT,
173 TUNER_INPUT_LONGTEXT, VLC_TRUE );
174 change_integer_list( pi_tuner_input, ppsz_tuner_input_text, 0 );
176 add_shortcut( "dshow" );
177 set_capability( "access_demux", 0 );
178 set_callbacks( DemuxOpen, DemuxClose );
181 set_description( _("DirectShow input") );
182 add_shortcut( "dshow" );
183 set_capability( "access2", 0 );
184 set_callbacks( AccessOpen, AccessClose );
188 /*****************************************************************************
189 * DirectShow elementary stream descriptor
190 *****************************************************************************/
191 typedef struct dshow_stream_t
194 IBaseFilter *p_device_filter;
195 CaptureFilter *p_capture_filter;
200 VIDEOINFOHEADER video;
212 /*****************************************************************************
213 * DirectShow utility functions
214 *****************************************************************************/
215 static void CreateDirectShowGraph( access_sys_t *p_sys )
217 p_sys->i_crossbar_route_depth = 0;
219 /* Create directshow filter graph */
220 if( SUCCEEDED( CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
221 (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph) ) )
223 /* Create directshow capture graph builder if available */
224 if( SUCCEEDED( CoCreateInstance( CLSID_CaptureGraphBuilder2, 0,
225 CLSCTX_INPROC, (REFIID)IID_ICaptureGraphBuilder2,
226 (void **)&p_sys->p_capture_graph_builder2 ) ) )
228 p_sys->p_capture_graph_builder2->
229 SetFiltergraph((IGraphBuilder *)p_sys->p_graph);
232 p_sys->p_graph->QueryInterface( IID_IMediaControl,
233 (void **)&p_sys->p_control );
237 static void DeleteDirectShowGraph( access_sys_t *p_sys )
239 DeleteCrossbarRoutes( p_sys );
241 /* Remove filters from graph */
242 for( int i = 0; i < p_sys->i_streams; i++ )
244 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_capture_filter );
245 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_device_filter );
246 p_sys->pp_streams[i]->p_capture_filter->Release();
247 p_sys->pp_streams[i]->p_device_filter->Release();
250 /* Release directshow objects */
251 if( p_sys->p_control )
253 p_sys->p_control->Release();
254 p_sys->p_control = NULL;
256 if( p_sys->p_capture_graph_builder2 )
258 p_sys->p_capture_graph_builder2->Release();
259 p_sys->p_capture_graph_builder2 = NULL;
264 p_sys->p_graph->Release();
265 p_sys->p_graph = NULL;
269 /*****************************************************************************
270 * CommonOpen: open direct show device
271 *****************************************************************************/
272 static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
273 vlc_bool_t b_access_demux )
278 /* Get/parse options and open device(s) */
279 string vdevname, adevname;
280 int i_width = 0, i_height = 0, i_chroma = 0;
281 vlc_bool_t b_audio = VLC_TRUE;
283 var_Create( p_this, "dshow-config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
284 var_Create( p_this, "dshow-tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
286 var_Create( p_this, "dshow-vdev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
287 var_Get( p_this, "dshow-vdev", &val );
288 if( val.psz_string ) vdevname = string( val.psz_string );
289 if( val.psz_string ) free( val.psz_string );
291 var_Create( p_this, "dshow-adev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
292 var_Get( p_this, "dshow-adev", &val );
293 if( val.psz_string ) adevname = string( val.psz_string );
294 if( val.psz_string ) free( val.psz_string );
296 static struct {char *psz_size; int i_width; int i_height;} size_table[] =
297 { { "subqcif", 128, 96 }, { "qsif", 160, 120 }, { "qcif", 176, 144 },
298 { "sif", 320, 240 }, { "cif", 352, 288 }, { "cif", 640, 480 },
302 var_Create( p_this, "dshow-size", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
303 var_Get( p_this, "dshow-size", &val );
304 if( val.psz_string && *val.psz_string )
306 for( i = 0; size_table[i].psz_size; i++ )
308 if( !strcmp( val.psz_string, size_table[i].psz_size ) )
310 i_width = size_table[i].i_width;
311 i_height = size_table[i].i_height;
315 if( !size_table[i].psz_size ) /* Try to parse "WidthxHeight" */
318 i_width = strtol( val.psz_string, &psz_parser, 0 );
319 if( *psz_parser == 'x' || *psz_parser == 'X')
321 i_height = strtol( psz_parser + 1, &psz_parser, 0 );
323 msg_Dbg( p_this, "Width x Height %dx%d", i_width, i_height );
326 if( val.psz_string ) free( val.psz_string );
328 var_Create( p_this, "dshow-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
329 var_Get( p_this, "dshow-chroma", &val );
330 if( val.psz_string && strlen( val.psz_string ) >= 4 )
332 i_chroma = VLC_FOURCC( val.psz_string[0], val.psz_string[1],
333 val.psz_string[2], val.psz_string[3] );
335 if( val.psz_string ) free( val.psz_string );
337 var_Create( p_this, "dshow-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
338 var_Create( p_this, "dshow-tuner-channel",
339 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
340 var_Create( p_this, "dshow-tuner-country",
341 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
342 var_Create( p_this, "dshow-tuner-input",
343 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
345 var_Create( p_this, "dshow-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
347 /* Initialize OLE/COM */
350 /* Initialize some data */
351 p_sys->i_streams = 0;
352 p_sys->pp_streams = 0;
353 p_sys->i_width = i_width;
354 p_sys->i_height = i_height;
355 p_sys->i_chroma = i_chroma;
357 p_sys->p_graph = NULL;
358 p_sys->p_capture_graph_builder2 = NULL;
359 p_sys->p_control = NULL;
361 vlc_mutex_init( p_this, &p_sys->lock );
362 vlc_cond_init( p_this, &p_sys->wait );
364 /* Build directshow graph */
365 CreateDirectShowGraph( p_sys );
367 if( OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
369 msg_Err( p_this, "can't open video");
373 /* Check if we can handle the demuxing ourselves or need to spawn
374 * a demuxer module */
375 dshow_stream_t *p_stream = p_sys->pp_streams[p_sys->i_streams-1];
377 if( p_stream->mt.majortype == MEDIATYPE_Video )
379 if( /* Raw DV stream */
380 p_stream->i_fourcc == VLC_FOURCC('d','v','s','l') ||
381 p_stream->i_fourcc == VLC_FOURCC('d','v','s','d') ||
382 p_stream->i_fourcc == VLC_FOURCC('d','v','h','d') ||
383 /* Raw MPEG video stream */
384 p_stream->i_fourcc == VLC_FOURCC('m','p','2','v') )
390 /* Let the access (only) take care of that */
396 if( p_stream->mt.majortype == MEDIATYPE_Stream )
402 /* Let the access (only) take care of that */
406 var_Get( p_this, "dshow-tuner", &val );
409 /* FIXME: we do MEDIATYPE_Stream here so we don't do
411 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
412 p_stream->p_device_filter, 0 );
417 if( b_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
419 msg_Err( p_this, "can't open audio");
422 for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
424 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
425 LONG VideoInputIndex = p_sys->crossbar_routes[i].VideoInputIndex;
426 LONG VideoOutputIndex = p_sys->crossbar_routes[i].VideoOutputIndex;
427 LONG AudioInputIndex = p_sys->crossbar_routes[i].AudioInputIndex;
428 LONG AudioOutputIndex = p_sys->crossbar_routes[i].AudioOutputIndex;
430 if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
432 msg_Dbg( p_this, "Crossbar at depth %d, Routed video "
433 "ouput %ld to video input %ld", i, VideoOutputIndex,
436 if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
438 if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
441 msg_Dbg(p_this, "Crossbar at depth %d, Routed audio "
442 "ouput %ld to audio input %ld", i,
443 AudioOutputIndex, AudioInputIndex );
450 ** Show properties pages from other filters in graph
452 var_Get( p_this, "dshow-config", &val );
455 for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
457 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
460 if( SUCCEEDED( pXbar->QueryInterface( IID_IBaseFilter,
463 ShowPropertyPage( p_XF );
469 /* Initialize some data */
470 p_sys->i_current_stream = 0;
472 if( !p_sys->i_streams ) return VLC_EGENERIC;
477 /*****************************************************************************
478 * DemuxOpen: open direct show device as an access_demux module
479 *****************************************************************************/
480 static int DemuxOpen( vlc_object_t *p_this )
482 demux_t *p_demux = (demux_t *)p_this;
486 p_sys = (access_sys_t *)malloc( sizeof( access_sys_t ) );
487 memset( p_sys, 0, sizeof( access_sys_t ) );
488 p_demux->p_sys = (demux_sys_t *)p_sys;
490 if( CommonOpen( p_this, p_sys, VLC_TRUE ) != VLC_SUCCESS )
492 CommonClose( p_this, p_sys );
496 /* Everything is ready. Let's rock baby */
497 msg_Dbg( p_this, "Playing...");
498 p_sys->p_control->Run();
500 p_demux->pf_demux = Demux;
501 p_demux->pf_control = DemuxControl;
502 p_demux->info.i_update = 0;
503 p_demux->info.i_title = 0;
504 p_demux->info.i_seekpoint = 0;
506 for( i = 0; i < p_sys->i_streams; i++ )
508 dshow_stream_t *p_stream = p_sys->pp_streams[i];
511 if( p_stream->mt.majortype == MEDIATYPE_Video )
513 es_format_Init( &fmt, VIDEO_ES, p_stream->i_fourcc );
515 fmt.video.i_width = p_stream->header.video.bmiHeader.biWidth;
516 fmt.video.i_height = p_stream->header.video.bmiHeader.biHeight;
517 fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
519 if( !p_stream->header.video.bmiHeader.biCompression )
521 /* RGB DIB are coded from bottom to top */
522 fmt.video.i_height = (unsigned int)(-(int)fmt.video.i_height);
525 /* Setup rgb mask for RGB formats */
526 if( p_stream->i_fourcc == VLC_FOURCC('R','V','2','4') )
528 /* This is in BGR format */
529 fmt.video.i_bmask = 0x00ff0000;
530 fmt.video.i_gmask = 0x0000ff00;
531 fmt.video.i_rmask = 0x000000ff;
533 fmt.video.i_frame_rate = 10000000000ULL/p_stream->header.video.AvgTimePerFrame;
534 fmt.video.i_frame_rate_base = 1000;
536 else if( p_stream->mt.majortype == MEDIATYPE_Audio )
538 es_format_Init( &fmt, AUDIO_ES, p_stream->i_fourcc );
540 fmt.audio.i_channels = p_stream->header.audio.nChannels;
541 fmt.audio.i_rate = p_stream->header.audio.nSamplesPerSec;
542 fmt.audio.i_bitspersample = p_stream->header.audio.wBitsPerSample;
543 fmt.audio.i_blockalign = fmt.audio.i_channels *
544 fmt.audio.i_bitspersample / 8;
545 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
546 fmt.audio.i_bitspersample;
549 p_stream->p_es = es_out_Add( p_demux->out, &fmt );
555 /*****************************************************************************
556 * AccessOpen: open direct show device as an access module
557 *****************************************************************************/
558 static int AccessOpen( vlc_object_t *p_this )
560 access_t *p_access = (access_t*)p_this;
563 p_access->p_sys = p_sys = (access_sys_t *)malloc( sizeof( access_sys_t ) );
564 memset( p_sys, 0, sizeof( access_sys_t ) );
566 if( CommonOpen( p_this, p_sys, VLC_FALSE ) != VLC_SUCCESS )
568 CommonClose( p_this, p_sys );
572 dshow_stream_t *p_stream = p_sys->pp_streams[0];
574 /* Check if we need to force demuxers */
575 if( !p_access->psz_demux || !*p_access->psz_demux )
577 if( p_stream->i_fourcc == VLC_FOURCC('d','v','s','l') ||
578 p_stream->i_fourcc == VLC_FOURCC('d','v','s','d') ||
579 p_stream->i_fourcc == VLC_FOURCC('d','v','h','d') )
581 p_access->psz_demux = strdup( "rawdv" );
583 else if( p_stream->i_fourcc == VLC_FOURCC('m','p','2','v') )
585 p_access->psz_demux = "mpgv";
590 p_access->pf_read = NULL;
591 p_access->pf_block = ReadCompressed;
592 p_access->pf_control = AccessControl;
593 p_access->pf_seek = NULL;
594 p_access->info.i_update = 0;
595 p_access->info.i_size = 0;
596 p_access->info.i_pos = 0;
597 p_access->info.b_eof = VLC_FALSE;
598 p_access->info.i_title = 0;
599 p_access->info.i_seekpoint = 0;
600 p_access->p_sys = p_sys;
602 /* Everything is ready. Let's rock baby */
603 msg_Dbg( p_this, "Playing...");
604 p_sys->p_control->Run();
609 /*****************************************************************************
610 * CommonClose: close device
611 *****************************************************************************/
612 static void CommonClose( vlc_object_t *p_this, access_sys_t *p_sys )
614 msg_Dbg( p_this, "Releasing DirectShow");
616 DeleteDirectShowGraph( p_sys );
618 /* Uninitialize OLE/COM */
621 for( int i = 0; i < p_sys->i_streams; i++ ) delete p_sys->pp_streams[i];
622 if( p_sys->i_streams ) free( p_sys->pp_streams );
624 vlc_mutex_destroy( &p_sys->lock );
625 vlc_cond_destroy( &p_sys->wait );
630 /*****************************************************************************
631 * AccessClose: close device
632 *****************************************************************************/
633 static void AccessClose( vlc_object_t *p_this )
635 access_t *p_access = (access_t *)p_this;
636 access_sys_t *p_sys = p_access->p_sys;
638 /* Stop capturing stuff */
639 p_sys->p_control->Stop();
641 CommonClose( p_this, p_sys );
644 /*****************************************************************************
645 * DemuxClose: close device
646 *****************************************************************************/
647 static void DemuxClose( vlc_object_t *p_this )
649 demux_t *p_demux = (demux_t *)p_this;
650 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
652 /* Stop capturing stuff */
653 p_sys->p_control->Stop();
655 CommonClose( p_this, p_sys );
658 /****************************************************************************
660 ****************************************************************************/
661 static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
662 IBaseFilter *p_filter,
663 CaptureFilter *p_capture_filter )
665 CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
667 AM_MEDIA_TYPE mediaType = p_input_pin->CustomGetMediaType();
669 if( p_sys->p_capture_graph_builder2 )
671 if( FAILED(p_sys->p_capture_graph_builder2->
672 RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
673 p_filter, 0, (IBaseFilter *)p_capture_filter )) )
678 // Sort out all the possible video inputs
679 // The class needs to be given the capture filters ANALOGVIDEO input pin
681 if( ( mediaType.majortype == MEDIATYPE_Video ||
682 mediaType.majortype == MEDIATYPE_Stream ) &&
683 SUCCEEDED(p_filter->EnumPins(&pins)) )
689 IKsPropertySet *pKs=0;
693 while( !Found && ( S_OK == pins->Next(1, &pP, &n) ) )
695 if( S_OK == pP->QueryPinInfo(&pinInfo) )
697 // is this pin an ANALOGVIDEOIN input pin?
698 if( pinInfo.dir == PINDIR_INPUT &&
699 pP->QueryInterface( IID_IKsPropertySet,
700 (void **)&pKs ) == S_OK )
702 if( pKs->Get( AMPROPSETID_Pin,
703 AMPROPERTY_PIN_CATEGORY, NULL, 0,
704 &guid, sizeof(GUID), &dw ) == S_OK )
706 if( guid == PIN_CATEGORY_ANALOGVIDEOIN )
708 // recursively search crossbar routes
709 FindCrossbarRoutes( p_this, p_sys, pP, 0 );
716 pinInfo.pFilter->Release();
726 IEnumPins *p_enumpins;
729 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;
731 while( S_OK == p_enumpins->Next( 1, &p_pin, NULL ) )
733 PIN_DIRECTION pin_dir;
734 p_pin->QueryDirection( &pin_dir );
736 if( pin_dir == PINDIR_OUTPUT &&
737 p_sys->p_graph->ConnectDirect( p_pin, (IPin *)p_input_pin,
741 p_enumpins->Release();
747 p_enumpins->Release();
753 * get fourcc priority from arbritary preference, the higher the better
755 static int GetFourCCPriority( int i_fourcc )
759 case VLC_FOURCC('I','4','2','0'):
760 case VLC_FOURCC('f','l','3','2'):
762 case VLC_FOURCC('Y','V','1','2'):
763 case VLC_FOURCC('a','r','a','w'):
765 case VLC_FOURCC('R','V','2','4'):
767 case VLC_FOURCC('Y','U','Y','2'):
768 case VLC_FOURCC('R','V','3','2'):
769 case VLC_FOURCC('R','G','B','A'):
776 #define MAX_MEDIA_TYPES 32
778 static int OpenDevice( vlc_object_t *p_this, access_sys_t *p_sys,
779 string devicename, vlc_bool_t b_audio )
781 /* See if device is already opened */
782 for( int i = 0; i < p_sys->i_streams; i++ )
784 if( devicename.size() &&
785 p_sys->pp_streams[i]->devicename == devicename )
792 list<string> list_devices;
794 /* Enumerate devices and display their names */
795 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
797 if( !list_devices.size() )
800 list<string>::iterator iter;
801 for( iter = list_devices.begin(); iter != list_devices.end(); iter++ )
802 msg_Dbg( p_this, "found device: %s", iter->c_str() );
804 /* If no device name was specified, pick the 1st one */
805 if( devicename.size() == 0 )
807 devicename = *list_devices.begin();
810 // Use the system device enumerator and class enumerator to find
811 // a capture/preview device, such as a desktop USB video camera.
812 IBaseFilter *p_device_filter =
813 FindCaptureDevice( p_this, &devicename, 0, b_audio );
814 if( p_device_filter )
815 msg_Dbg( p_this, "using device: %s", devicename.c_str() );
818 msg_Err( p_this, "can't use device: %s, unsupported device type",
819 devicename.c_str() );
823 // Retreive acceptable media types supported by device
824 AM_MEDIA_TYPE media_types[MAX_MEDIA_TYPES];
826 EnumDeviceCaps( p_this, p_device_filter, p_sys->i_chroma,
827 p_sys->i_width, p_sys->i_height,
828 0, 0, 0, media_types, MAX_MEDIA_TYPES );
830 /* Find out if the pin handles MEDIATYPE_Stream, in which case we
831 * won't add a prefered media type as this doesn't seem to work well
832 * -- to investigate. */
833 vlc_bool_t b_stream_type = VLC_FALSE;
834 for( size_t i = 0; i < media_count; i++ )
836 if( media_types[i].majortype == MEDIATYPE_Stream )
838 b_stream_type = VLC_TRUE;
844 AM_MEDIA_TYPE *mt = NULL;
846 if( !b_stream_type && !b_audio )
848 // Insert prefered video media type
852 mtr.majortype = MEDIATYPE_Video;
853 mtr.subtype = MEDIASUBTYPE_I420;
854 mtr.bFixedSizeSamples = TRUE;
855 mtr.bTemporalCompression = FALSE;
857 mtr.formattype = FORMAT_VideoInfo;
858 mtr.cbFormat = sizeof(vh);
859 mtr.pbFormat = (BYTE *)&vh;
861 memset(&vh, 0, sizeof(vh));
863 vh.bmiHeader.biSize = sizeof(vh.bmiHeader);
864 vh.bmiHeader.biWidth = p_sys->i_width > 0 ? p_sys->i_width : 320;
865 vh.bmiHeader.biHeight = p_sys->i_height > 0 ? p_sys->i_height : 240;
866 vh.bmiHeader.biPlanes = 3;
867 vh.bmiHeader.biBitCount = 12;
868 vh.bmiHeader.biCompression = VLC_FOURCC('I','4','2','0');
869 vh.bmiHeader.biSizeImage = vh.bmiHeader.biWidth * 12 *
870 vh.bmiHeader.biHeight / 8;
871 mtr.lSampleSize = vh.bmiHeader.biSizeImage;
874 mt = (AM_MEDIA_TYPE *)malloc( sizeof(AM_MEDIA_TYPE)*mt_count );
875 CopyMediaType(mt, &mtr);
877 else if( !b_stream_type )
879 // Insert prefered audio media type
883 mtr.majortype = MEDIATYPE_Audio;
884 mtr.subtype = MEDIASUBTYPE_PCM;
885 mtr.bFixedSizeSamples = TRUE;
886 mtr.bTemporalCompression = FALSE;
889 mtr.formattype = FORMAT_WaveFormatEx;
890 mtr.cbFormat = sizeof(wf);
891 mtr.pbFormat = (BYTE *)&wf;
893 memset(&wf, 0, sizeof(wf));
895 wf.wFormatTag = WAVE_FORMAT_PCM;
897 wf.nSamplesPerSec = 44100;
898 wf.wBitsPerSample = 16;
899 wf.nBlockAlign = wf.nSamplesPerSec * wf.wBitsPerSample / 8;
900 wf.nAvgBytesPerSec = wf.nSamplesPerSec * wf.nBlockAlign;
904 mt = (AM_MEDIA_TYPE *)malloc( sizeof(AM_MEDIA_TYPE)*mt_count );
905 CopyMediaType(mt, &mtr);
908 if( media_count > 0 )
910 mt = (AM_MEDIA_TYPE *)realloc( mt, sizeof(AM_MEDIA_TYPE) *
911 (mt_count + 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+mt_count] = media_types[slot_copy];
933 media_types[slot_copy] = media_types[c];
937 mt[c+mt_count] = media_types[c];
940 mt_count += media_count;
943 /* Create and add our capture filter */
944 CaptureFilter *p_capture_filter =
945 new CaptureFilter( p_this, p_sys, mt, mt_count );
946 p_sys->p_graph->AddFilter( p_capture_filter, 0 );
948 /* Add the device filter to the graph (seems necessary with VfW before
949 * accessing pin attributes). */
950 p_sys->p_graph->AddFilter( p_device_filter, 0 );
952 /* Attempt to connect one of this device's capture output pins */
953 msg_Dbg( p_this, "connecting filters" );
954 if( ConnectFilters( p_this, p_sys, p_device_filter, p_capture_filter ) )
957 msg_Dbg( p_this, "filters connected successfully !" );
959 dshow_stream_t dshow_stream;
960 dshow_stream.b_pts = VLC_FALSE;
961 dshow_stream.p_es = 0;
963 p_capture_filter->CustomGetPin()->CustomGetMediaType();
965 /* Show Device properties. Done here so the VLC stream is setup with
966 * the proper parameters. */
968 var_Get( p_this, "dshow-config", &val );
971 ShowDeviceProperties( p_this, p_sys->p_capture_graph_builder2,
972 p_device_filter, b_audio );
975 ConfigTuner( p_this, p_sys->p_capture_graph_builder2,
978 var_Get( p_this, "dshow-tuner", &val );
979 if( val.b_bool && dshow_stream.mt.majortype != MEDIATYPE_Stream )
981 /* FIXME: we do MEDIATYPE_Stream later so we don't do it twice. */
982 ShowTunerProperties( p_this, p_sys->p_capture_graph_builder2,
983 p_device_filter, b_audio );
987 p_capture_filter->CustomGetPin()->CustomGetMediaType();
989 dshow_stream.i_fourcc = GetFourCCFromMediaType( dshow_stream.mt );
990 if( dshow_stream.i_fourcc )
992 if( dshow_stream.mt.majortype == MEDIATYPE_Video )
994 dshow_stream.header.video =
995 *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
996 msg_Dbg( p_this, "MEDIATYPE_Video" );
997 msg_Dbg( p_this, "selected video pin accepts format: %4.4s",
998 (char *)&dshow_stream.i_fourcc);
1000 else if( dshow_stream.mt.majortype == MEDIATYPE_Audio )
1002 dshow_stream.header.audio =
1003 *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
1004 msg_Dbg( p_this, "MEDIATYPE_Audio" );
1005 msg_Dbg( p_this, "selected audio pin accepts format: %4.4s",
1006 (char *)&dshow_stream.i_fourcc);
1008 else if( dshow_stream.mt.majortype == MEDIATYPE_Stream )
1010 msg_Dbg( p_this, "MEDIATYPE_Stream" );
1011 msg_Dbg( p_this, "selected stream pin accepts format: %4.4s",
1012 (char *)&dshow_stream.i_fourcc);
1016 msg_Dbg( p_this, "unknown stream majortype" );
1020 /* Add directshow elementary stream to our list */
1021 dshow_stream.p_device_filter = p_device_filter;
1022 dshow_stream.p_capture_filter = p_capture_filter;
1024 p_sys->pp_streams = (dshow_stream_t **)realloc( p_sys->pp_streams,
1025 sizeof(dshow_stream_t *) * (p_sys->i_streams + 1) );
1026 p_sys->pp_streams[p_sys->i_streams] = new dshow_stream_t;
1027 *p_sys->pp_streams[p_sys->i_streams++] = dshow_stream;
1034 /* Remove filters from graph */
1035 p_sys->p_graph->RemoveFilter( p_device_filter );
1036 p_sys->p_graph->RemoveFilter( p_capture_filter );
1038 /* Release objects */
1039 p_device_filter->Release();
1040 p_capture_filter->Release();
1042 return VLC_EGENERIC;
1045 static IBaseFilter *
1046 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
1047 list<string> *p_listdevices, vlc_bool_t b_audio )
1049 IBaseFilter *p_base_filter = NULL;
1050 IMoniker *p_moniker = NULL;
1054 /* Create the system device enumerator */
1055 ICreateDevEnum *p_dev_enum = NULL;
1057 hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
1058 IID_ICreateDevEnum, (void **)&p_dev_enum );
1061 msg_Err( p_this, "failed to create the device enumerator (0x%lx)", hr);
1065 /* Create an enumerator for the video capture devices */
1066 IEnumMoniker *p_class_enum = NULL;
1068 hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
1071 hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
1073 p_dev_enum->Release();
1076 msg_Err( p_this, "failed to create the class enumerator (0x%lx)", hr );
1080 /* If there are no enumerators for the requested type, then
1081 * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
1082 if( p_class_enum == NULL )
1084 msg_Err( p_this, "no capture device was detected" );
1088 /* Enumerate the devices */
1090 /* Note that if the Next() call succeeds but there are no monikers,
1091 * it will return S_FALSE (which is not a failure). Therefore, we check
1092 * that the return code is S_OK instead of using SUCCEEDED() macro. */
1094 while( p_class_enum->Next( 1, &p_moniker, &i_fetched ) == S_OK )
1096 /* Getting the property page to get the device name */
1097 IPropertyBag *p_bag;
1098 hr = p_moniker->BindToStorage( 0, 0, IID_IPropertyBag,
1104 hr = p_bag->Read( L"FriendlyName", &var, NULL );
1108 int i_convert = WideCharToMultiByte(CP_ACP, 0, var.bstrVal,
1109 SysStringLen(var.bstrVal), NULL, 0, NULL, NULL);
1110 char *p_buf = (char *)alloca( i_convert ); p_buf[0] = 0;
1111 WideCharToMultiByte( CP_ACP, 0, var.bstrVal,
1112 SysStringLen(var.bstrVal), p_buf, i_convert, NULL, NULL );
1113 SysFreeString(var.bstrVal);
1115 if( p_listdevices ) p_listdevices->push_back( p_buf );
1117 if( p_devicename && *p_devicename == string(p_buf) )
1119 /* Bind Moniker to a filter object */
1120 hr = p_moniker->BindToObject( 0, 0, IID_IBaseFilter,
1121 (void **)&p_base_filter );
1124 msg_Err( p_this, "couldn't bind moniker to filter "
1125 "object (0x%lx)", hr );
1126 p_moniker->Release();
1127 p_class_enum->Release();
1130 p_moniker->Release();
1131 p_class_enum->Release();
1132 return p_base_filter;
1137 p_moniker->Release();
1140 p_class_enum->Release();
1144 static size_t EnumDeviceCaps( vlc_object_t *p_this, IBaseFilter *p_filter,
1145 int i_fourcc, int i_width, int i_height,
1146 int i_channels, int i_samplespersec,
1147 int i_bitspersample, AM_MEDIA_TYPE *mt,
1150 IEnumPins *p_enumpins;
1152 IEnumMediaTypes *p_enummt;
1153 size_t mt_count = 0;
1155 LONGLONG i_AvgTimePerFrame = 0;
1156 float r_fps = var_GetFloat( p_this, "dshow-fps" );
1158 i_AvgTimePerFrame = 10000000000LL/(LONGLONG)(r_fps*1000.0f);
1160 if( FAILED(p_filter->EnumPins( &p_enumpins )) )
1162 msg_Dbg( p_this, "EnumDeviceCaps failed: no pin enumeration !");
1166 while( S_OK == p_enumpins->Next( 1, &p_output_pin, NULL ) )
1170 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1172 msg_Dbg( p_this, "EnumDeviceCaps: %s pin: %S",
1173 info.dir == PINDIR_INPUT ? "input" : "output",
1175 if( info.pFilter ) info.pFilter->Release();
1178 p_output_pin->Release();
1181 p_enumpins->Reset();
1183 while( !mt_count && p_enumpins->Next( 1, &p_output_pin, NULL ) == S_OK )
1187 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1189 if( info.pFilter ) info.pFilter->Release();
1190 if( info.dir == PINDIR_INPUT )
1192 p_output_pin->Release();
1195 msg_Dbg( p_this, "EnumDeviceCaps: trying pin %S", info.achName );
1198 AM_MEDIA_TYPE *p_mt;
1201 ** Configure pin with a default compatible media if possible
1204 IAMStreamConfig *pSC;
1205 if( SUCCEEDED(p_output_pin->QueryInterface( IID_IAMStreamConfig,
1208 int piCount, piSize;
1209 if( SUCCEEDED(pSC->GetNumberOfCapabilities(&piCount, &piSize)) )
1211 BYTE *pSCC= (BYTE *)CoTaskMemAlloc(piSize);
1214 for( int i=0; i<piCount; ++i )
1216 if( SUCCEEDED(pSC->GetStreamCaps(i, &p_mt, pSCC)) )
1218 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1220 if( !i_current_fourcc || (i_fourcc && (i_current_fourcc != i_fourcc)) )
1222 // incompatible or unrecognized chroma, try next media type
1223 FreeMediaType( *p_mt );
1224 CoTaskMemFree( (PVOID)p_mt );
1228 if( MEDIATYPE_Video == p_mt->majortype
1229 && FORMAT_VideoInfo == p_mt->formattype )
1231 VIDEO_STREAM_CONFIG_CAPS *pVSCC = reinterpret_cast<VIDEO_STREAM_CONFIG_CAPS*>(pSCC);
1232 VIDEOINFOHEADER *pVih = reinterpret_cast<VIDEOINFOHEADER*>(p_mt->pbFormat);
1234 if( i_AvgTimePerFrame )
1236 if( pVSCC->MinFrameInterval > i_AvgTimePerFrame
1237 || i_AvgTimePerFrame > pVSCC->MaxFrameInterval )
1239 // required frame rate not compatible, try next media type
1240 FreeMediaType( *p_mt );
1241 CoTaskMemFree( (PVOID)p_mt );
1244 pVih->AvgTimePerFrame = i_AvgTimePerFrame;
1249 if( i_width % pVSCC->OutputGranularityX
1250 || pVSCC->MinOutputSize.cx > i_width
1251 || i_width > pVSCC->MaxOutputSize.cx )
1253 // required width not compatible, try next media type
1254 FreeMediaType( *p_mt );
1255 CoTaskMemFree( (PVOID)p_mt );
1258 pVih->bmiHeader.biWidth = i_width;
1263 if( i_height % pVSCC->OutputGranularityY
1264 || pVSCC->MinOutputSize.cy > i_height
1265 || i_height > pVSCC->MaxOutputSize.cy )
1267 // required height not compatible, try next media type
1268 FreeMediaType( *p_mt );
1269 CoTaskMemFree( (PVOID)p_mt );
1272 pVih->bmiHeader.biHeight = i_height;
1275 // Set the sample size and image size.
1276 // (Round the image width up to a DWORD boundary.)
1277 p_mt->lSampleSize = pVih->bmiHeader.biSizeImage =
1278 ((pVih->bmiHeader.biWidth + 3) & ~3) *
1279 pVih->bmiHeader.biHeight * (pVih->bmiHeader.biBitCount>>3);
1281 // no cropping, use full video input buffer
1282 memset(&(pVih->rcSource), 0, sizeof(RECT));
1283 memset(&(pVih->rcTarget), 0, sizeof(RECT));
1285 // select this format as default
1286 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1288 msg_Dbg( p_this, "EnumDeviceCaps: input pin video format configured");
1289 // no need to check any more media types
1292 else FreeMediaType( *p_mt );
1294 else if( p_mt->majortype == MEDIATYPE_Audio
1295 && p_mt->formattype == FORMAT_WaveFormatEx )
1297 AUDIO_STREAM_CONFIG_CAPS *pASCC = reinterpret_cast<AUDIO_STREAM_CONFIG_CAPS*>(pSCC);
1298 WAVEFORMATEX *pWfx = reinterpret_cast<WAVEFORMATEX*>(p_mt->pbFormat);
1302 if( i_channels % pASCC->ChannelsGranularity
1303 || (unsigned int)i_channels < pASCC->MinimumChannels
1304 || (unsigned int)i_channels > pASCC->MaximumChannels )
1306 // required channels not compatible, try next media type
1307 FreeMediaType( *p_mt );
1308 CoTaskMemFree( (PVOID)p_mt );
1311 pWfx->nChannels = i_channels;
1314 if( i_samplespersec )
1316 if( i_samplespersec % pASCC->BitsPerSampleGranularity
1317 || (unsigned int)i_samplespersec < pASCC->MinimumSampleFrequency
1318 || (unsigned int)i_samplespersec > pASCC->MaximumSampleFrequency )
1320 // required sampling rate not compatible, try next media type
1321 FreeMediaType( *p_mt );
1322 CoTaskMemFree( (PVOID)p_mt );
1325 pWfx->nSamplesPerSec = i_samplespersec;
1328 if( i_bitspersample )
1330 if( i_bitspersample % pASCC->BitsPerSampleGranularity
1331 || (unsigned int)i_bitspersample < pASCC->MinimumBitsPerSample
1332 || (unsigned int)i_bitspersample > pASCC->MaximumBitsPerSample )
1334 // required sample size not compatible, try next media type
1335 FreeMediaType( *p_mt );
1336 CoTaskMemFree( (PVOID)p_mt );
1339 pWfx->wBitsPerSample = i_bitspersample;
1342 // select this format as default
1343 if( SUCCEEDED( pSC->SetFormat(p_mt) ) )
1345 msg_Dbg( p_this, "EnumDeviceCaps: input pin default format configured");
1346 // no need to check any more media types
1350 FreeMediaType( *p_mt );
1351 CoTaskMemFree( (PVOID)p_mt );
1354 CoTaskMemFree( (LPVOID)pSCC );
1361 ** Probe pin for available medias (may be a previously configured one)
1364 if( FAILED( p_output_pin->EnumMediaTypes( &p_enummt ) ) )
1366 p_output_pin->Release();
1371 while( p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1373 int i_current_fourcc = GetFourCCFromMediaType( *p_mt );
1374 if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Video
1375 && p_mt->formattype == FORMAT_VideoInfo )
1377 int i_current_width = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth;
1378 int i_current_height = ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight;
1379 LONGLONG i_current_atpf = ((VIDEOINFOHEADER *)p_mt->pbFormat)->AvgTimePerFrame;
1381 if( i_current_height < 0 )
1382 i_current_height = -i_current_height;
1384 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1385 "accepts chroma: %4.4s, width:%i, height:%i, fps:%f",
1386 (char *)&i_current_fourcc, i_current_width,
1387 i_current_height, (10000000.0f/((float)i_current_atpf)) );
1389 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1390 ( !i_width || i_width == i_current_width ) &&
1391 ( !i_height || i_height == i_current_height ) &&
1392 ( !i_AvgTimePerFrame || i_AvgTimePerFrame == i_current_atpf ) &&
1396 mt[mt_count++] = *p_mt;
1398 else FreeMediaType( *p_mt );
1400 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Audio
1401 && p_mt->formattype == FORMAT_WaveFormatEx)
1403 int i_current_channels =
1404 ((WAVEFORMATEX *)p_mt->pbFormat)->nChannels;
1405 int i_current_samplespersec =
1406 ((WAVEFORMATEX *)p_mt->pbFormat)->nSamplesPerSec;
1407 int i_current_bitspersample =
1408 ((WAVEFORMATEX *)p_mt->pbFormat)->wBitsPerSample;
1410 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1411 "accepts format: %4.4s, channels:%i, "
1412 "samples/sec:%i bits/sample:%i",
1413 (char *)&i_current_fourcc, i_current_channels,
1414 i_current_samplespersec, i_current_bitspersample);
1416 if( (!i_channels || i_channels == i_current_channels) &&
1417 (!i_samplespersec ||
1418 i_samplespersec == i_current_samplespersec) &&
1419 (!i_bitspersample ||
1420 i_bitspersample == i_current_bitspersample) &&
1424 mt[mt_count++] = *p_mt;
1426 /* Setup a few properties like the audio latency */
1427 IAMBufferNegotiation *p_ambuf;
1428 if( SUCCEEDED( p_output_pin->QueryInterface(
1429 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
1431 ALLOCATOR_PROPERTIES AllocProp;
1432 AllocProp.cbAlign = -1;
1434 /* 100 ms of latency */
1435 AllocProp.cbBuffer = i_current_channels *
1436 i_current_samplespersec *
1437 i_current_bitspersample / 8 / 10;
1439 AllocProp.cbPrefix = -1;
1440 AllocProp.cBuffers = -1;
1441 p_ambuf->SuggestAllocatorProperties( &AllocProp );
1445 else FreeMediaType( *p_mt );
1447 else if( i_current_fourcc && p_mt->majortype == MEDIATYPE_Stream )
1449 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1450 "accepts stream format: %4.4s",
1451 (char *)&i_current_fourcc );
1453 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) &&
1457 mt[mt_count++] = *p_mt;
1458 i_fourcc = i_current_fourcc;
1460 else FreeMediaType( *p_mt );
1464 char *psz_type = "unknown";
1465 if( p_mt->majortype == MEDIATYPE_Video ) psz_type = "video";
1466 if( p_mt->majortype == MEDIATYPE_Audio ) psz_type = "audio";
1467 if( p_mt->majortype == MEDIATYPE_Stream ) psz_type = "stream";
1468 msg_Dbg( p_this, "EnumDeviceCaps: input pin media: unknown format "
1469 "(%s %4.4s)", psz_type, (char *)&p_mt->subtype );
1470 FreeMediaType( *p_mt );
1472 CoTaskMemFree( (PVOID)p_mt );
1475 p_enummt->Release();
1476 p_output_pin->Release();
1479 p_enumpins->Release();
1483 /*****************************************************************************
1484 * ReadCompressed: reads compressed (MPEG/DV) data from the device.
1485 *****************************************************************************
1486 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1488 *****************************************************************************/
1489 static block_t *ReadCompressed( access_t *p_access )
1491 access_sys_t *p_sys = p_access->p_sys;
1492 dshow_stream_t *p_stream = NULL;
1493 VLCMediaSample sample;
1495 /* Read 1 DV/MPEG frame (they contain the video and audio data) */
1497 /* There must be only 1 elementary stream to produce a valid stream
1498 * of MPEG or DV data */
1499 p_stream = p_sys->pp_streams[0];
1503 if( p_access->b_die || p_access->b_error ) return 0;
1505 /* Get new sample/frame from the elementary stream (blocking). */
1506 vlc_mutex_lock( &p_sys->lock );
1508 if( p_stream->p_capture_filter->CustomGetPin()
1509 ->CustomGetSample( &sample ) != S_OK )
1511 /* No data available. Wait until some data has arrived */
1512 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1513 vlc_mutex_unlock( &p_sys->lock );
1517 vlc_mutex_unlock( &p_sys->lock );
1524 int i_data_size = sample.p_sample->GetActualDataLength();
1526 if( !i_data_size || !(p_block = block_New( p_access, i_data_size )) )
1528 sample.p_sample->Release();
1532 sample.p_sample->GetPointer( &p_data );
1533 p_access->p_vlc->pf_memcpy( p_block->p_buffer, p_data, i_data_size );
1534 sample.p_sample->Release();
1536 /* The caller got what he wanted */
1540 return 0; /* never reached */
1543 /****************************************************************************
1545 ****************************************************************************/
1546 static int Demux( demux_t *p_demux )
1548 access_sys_t *p_sys = (access_sys_t *)p_demux->p_sys;
1549 dshow_stream_t *p_stream = NULL;
1550 VLCMediaSample sample;
1551 int i_data_size, i_stream;
1555 vlc_mutex_lock( &p_sys->lock );
1557 /* Try to grab an audio sample (audio has a higher priority) */
1558 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1560 p_stream = p_sys->pp_streams[i_stream];
1561 if( p_stream->mt.majortype == MEDIATYPE_Audio &&
1562 p_stream->p_capture_filter &&
1563 p_stream->p_capture_filter->CustomGetPin()
1564 ->CustomGetSample( &sample ) == S_OK )
1569 /* Try to grab a video sample */
1570 if( i_stream == p_sys->i_streams )
1572 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1574 p_stream = p_sys->pp_streams[i_stream];
1575 if( p_stream->p_capture_filter &&
1576 p_stream->p_capture_filter->CustomGetPin()
1577 ->CustomGetSample( &sample ) == S_OK )
1584 vlc_mutex_unlock( &p_sys->lock );
1586 if( i_stream == p_sys->i_streams )
1588 /* Sleep so we do not consume all the cpu, 10ms seems
1589 * like a good value (100fps) */
1597 i_data_size = sample.p_sample->GetActualDataLength();
1598 sample.p_sample->GetPointer( &p_data );
1600 REFERENCE_TIME i_pts, i_end_date;
1601 HRESULT hr = sample.p_sample->GetTime( &i_pts, &i_end_date );
1602 if( hr != VFW_S_NO_STOP_TIME && hr != S_OK ) i_pts = 0;
1606 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
1608 /* Use our data timestamp */
1609 i_pts = sample.i_timestamp;
1610 p_stream->b_pts = VLC_TRUE;
1614 i_pts /= 10; /* Dshow works with 100 nano-seconds resolution */
1617 msg_Dbg( p_demux, "Read() stream: %i, size: %i, PTS: "I64Fd,
1618 i_stream, i_data_size, i_pts );
1621 p_block = block_New( p_demux, i_data_size );
1622 p_demux->p_vlc->pf_memcpy( p_block->p_buffer, p_data, i_data_size );
1623 p_block->i_pts = p_block->i_dts = i_pts;
1624 sample.p_sample->Release();
1626 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pts > 0 ? i_pts : 0 );
1627 es_out_Send( p_demux->out, p_stream->p_es, p_block );
1632 /*****************************************************************************
1634 *****************************************************************************/
1635 static int AccessControl( access_t *p_access, int i_query, va_list args )
1637 vlc_bool_t *pb_bool;
1644 case ACCESS_CAN_SEEK:
1645 case ACCESS_CAN_FASTSEEK:
1646 case ACCESS_CAN_PAUSE:
1647 case ACCESS_CAN_CONTROL_PACE:
1648 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1649 *pb_bool = VLC_FALSE;
1653 case ACCESS_GET_MTU:
1654 pi_int = (int*)va_arg( args, int * );
1658 case ACCESS_GET_PTS_DELAY:
1659 pi_64 = (int64_t*)va_arg( args, int64_t * );
1660 *pi_64 = (int64_t)var_GetInteger( p_access, "dshow-caching" ) * 1000;
1664 case ACCESS_SET_PAUSE_STATE:
1665 case ACCESS_GET_TITLE_INFO:
1666 case ACCESS_SET_TITLE:
1667 case ACCESS_SET_SEEKPOINT:
1668 case ACCESS_SET_PRIVATE_ID_STATE:
1669 return VLC_EGENERIC;
1672 msg_Warn( p_access, "unimplemented query in control" );
1673 return VLC_EGENERIC;
1679 /****************************************************************************
1681 ****************************************************************************/
1682 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
1689 /* Special for access_demux */
1690 case DEMUX_CAN_PAUSE:
1691 case DEMUX_SET_PAUSE_STATE:
1692 case DEMUX_CAN_CONTROL_PACE:
1693 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
1697 case DEMUX_GET_PTS_DELAY:
1698 pi64 = (int64_t*)va_arg( args, int64_t * );
1699 *pi64 = (int64_t)var_GetInteger( p_demux, "dshow-caching" ) * 1000;
1702 case DEMUX_GET_TIME:
1703 pi64 = (int64_t*)va_arg( args, int64_t * );
1707 /* TODO implement others */
1709 return VLC_EGENERIC;
1712 return VLC_EGENERIC;
1715 /*****************************************************************************
1716 * config variable callback
1717 *****************************************************************************/
1718 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1719 vlc_value_t newval, vlc_value_t oldval, void * )
1721 module_config_t *p_item;
1722 vlc_bool_t b_audio = VLC_FALSE;
1725 p_item = config_FindConfig( p_this, psz_name );
1726 if( !p_item ) return VLC_SUCCESS;
1728 if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = VLC_TRUE;
1730 /* Clear-up the current list */
1731 if( p_item->i_list )
1733 /* Keep the 2 first entries */
1734 for( i = 2; i < p_item->i_list; i++ )
1736 free( p_item->ppsz_list[i] );
1737 free( p_item->ppsz_list_text[i] );
1739 /* TODO: Remove when no more needed */
1740 p_item->ppsz_list[i] = NULL;
1741 p_item->ppsz_list_text[i] = NULL;
1745 /* Find list of devices */
1746 list<string> list_devices;
1748 /* Initialize OLE/COM */
1751 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1753 /* Uninitialize OLE/COM */
1756 if( !list_devices.size() ) return VLC_SUCCESS;
1759 (char **)realloc( p_item->ppsz_list,
1760 (list_devices.size()+3) * sizeof(char *) );
1761 p_item->ppsz_list_text =
1762 (char **)realloc( p_item->ppsz_list_text,
1763 (list_devices.size()+3) * sizeof(char *) );
1765 list<string>::iterator iter;
1766 for( iter = list_devices.begin(), i = 2; iter != list_devices.end();
1769 p_item->ppsz_list[i] = strdup( iter->c_str() );
1770 p_item->ppsz_list_text[i] = NULL;
1773 p_item->ppsz_list[i] = NULL;
1774 p_item->ppsz_list_text[i] = NULL;
1776 /* Signal change to the interface */
1777 p_item->b_dirty = VLC_TRUE;
1782 static int ConfigDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1783 vlc_value_t newval, vlc_value_t oldval, void * )
1785 module_config_t *p_item;
1786 vlc_bool_t b_audio = VLC_FALSE;
1788 /* Initialize OLE/COM */
1791 p_item = config_FindConfig( p_this, psz_name );
1792 if( !p_item ) return VLC_SUCCESS;
1794 if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = VLC_TRUE;
1798 if( newval.psz_string && *newval.psz_string )
1800 devicename = newval.psz_string;
1804 /* If no device name was specified, pick the 1st one */
1805 list<string> list_devices;
1807 /* Enumerate devices */
1808 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1809 if( !list_devices.size() ) return VLC_EGENERIC;
1810 devicename = *list_devices.begin();
1813 IBaseFilter *p_device_filter =
1814 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
1815 if( p_device_filter )
1817 ShowPropertyPage( p_device_filter );
1821 /* Uninitialize OLE/COM */
1824 msg_Err( p_this, "didn't find device: %s", devicename.c_str() );
1825 return VLC_EGENERIC;
1828 /* Uninitialize OLE/COM */
1834 /*****************************************************************************
1836 *****************************************************************************/
1837 static void ShowPropertyPage( IUnknown *obj )
1839 ISpecifyPropertyPages *p_spec;
1842 HRESULT hr = obj->QueryInterface( IID_ISpecifyPropertyPages,
1844 if( FAILED(hr) ) return;
1846 if( SUCCEEDED(p_spec->GetPages( &cauuid )) )
1848 if( cauuid.cElems > 0 )
1850 HWND hwnd_desktop = ::GetDesktopWindow();
1852 OleCreatePropertyFrame( hwnd_desktop, 30, 30, NULL, 1, &obj,
1853 cauuid.cElems, cauuid.pElems, 0, 0, NULL );
1855 CoTaskMemFree( cauuid.pElems );
1861 static void ShowDeviceProperties( vlc_object_t *p_this,
1862 ICaptureGraphBuilder2 *p_graph,
1863 IBaseFilter *p_device_filter,
1864 vlc_bool_t b_audio )
1867 msg_Dbg( p_this, "Configuring Device Properties" );
1870 * Video or audio capture filter page
1872 ShowPropertyPage( p_device_filter );
1877 if( p_graph && b_audio )
1879 IAMStreamConfig *p_SC;
1881 msg_Dbg( p_this, "Showing WDM Audio Configuration Pages" );
1883 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
1884 &MEDIATYPE_Audio, p_device_filter,
1885 IID_IAMStreamConfig, (void **)&p_SC );
1888 ShowPropertyPage(p_SC);
1896 HRESULT hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
1897 &MEDIATYPE_Audio, p_device_filter,
1898 IID_IAMTVAudio, (void **)&p_TVA );
1901 ShowPropertyPage(p_TVA);
1909 if( p_graph && !b_audio )
1911 IAMStreamConfig *p_SC;
1913 msg_Dbg( p_this, "Showing WDM Video Configuration Pages" );
1915 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
1916 &MEDIATYPE_Interleaved, p_device_filter,
1917 IID_IAMStreamConfig, (void **)&p_SC );
1920 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
1921 &MEDIATYPE_Video, p_device_filter,
1922 IID_IAMStreamConfig, (void **)&p_SC );
1927 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
1928 &MEDIATYPE_Stream, p_device_filter,
1929 IID_IAMStreamConfig, (void **)&p_SC );
1934 ShowPropertyPage(p_SC);
1940 static void ShowTunerProperties( vlc_object_t *p_this,
1941 ICaptureGraphBuilder2 *p_graph,
1942 IBaseFilter *p_device_filter,
1943 vlc_bool_t b_audio )
1946 msg_Dbg( p_this, "Configuring Tuner Properties" );
1948 if( !p_graph || b_audio ) return;
1951 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
1952 &MEDIATYPE_Interleaved, p_device_filter,
1953 IID_IAMTVTuner, (void **)&p_TV );
1956 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
1957 &MEDIATYPE_Video, p_device_filter,
1958 IID_IAMTVTuner, (void **)&p_TV );
1963 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
1964 &MEDIATYPE_Stream, p_device_filter,
1965 IID_IAMTVTuner, (void **)&p_TV );
1970 ShowPropertyPage(p_TV);
1975 static void ConfigTuner( vlc_object_t *p_this, ICaptureGraphBuilder2 *p_graph,
1976 IBaseFilter *p_device_filter )
1978 int i_channel, i_country, i_input;
1983 if( !p_graph ) return;
1985 i_channel = var_GetInteger( p_this, "dshow-tuner-channel" );
1986 i_country = var_GetInteger( p_this, "dshow-tuner-country" );
1987 i_input = var_GetInteger( p_this, "dshow-tuner-input" );
1989 if( !i_channel && !i_country && !i_input ) return; /* Nothing to do */
1991 msg_Dbg( p_this, "tuner config: channel %i, country %i, input type %i",
1992 i_channel, i_country, i_input );
1994 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Interleaved,
1995 p_device_filter, IID_IAMTVTuner,
1999 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Video,
2000 p_device_filter, IID_IAMTVTuner,
2006 hr = p_graph->FindInterface( &PIN_CATEGORY_CAPTURE, &MEDIATYPE_Stream,
2007 p_device_filter, IID_IAMTVTuner,
2013 msg_Dbg( p_this, "couldn't find tuner interface" );
2017 hr = p_TV->GetAvailableModes( &l_modes );
2018 if( SUCCEEDED(hr) && (l_modes & AMTUNER_MODE_TV) )
2020 hr = p_TV->put_Mode( AMTUNER_MODE_TV );
2023 if( i_input == 1 ) p_TV->put_InputType( 0, TunerInputCable );
2024 else if( i_input == 2 ) p_TV->put_InputType( 0, TunerInputAntenna );
2026 p_TV->put_CountryCode( i_country );
2027 p_TV->put_Channel( i_channel, AMTUNER_SUBCHAN_NO_TUNE,
2028 AMTUNER_SUBCHAN_NO_TUNE );