1 /*****************************************************************************
2 * dshow.cpp : DirectShow access module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: dshow.cpp,v 1.11 2003/10/25 00:49:13 sam Exp $
7 * Author: Gildas Bazin <gbazin@netcourrier.com>
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>
37 /*****************************************************************************
39 *****************************************************************************/
40 static int AccessOpen ( vlc_object_t * );
41 static void AccessClose ( vlc_object_t * );
42 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
44 static int DemuxOpen ( vlc_object_t * );
45 static void DemuxClose ( vlc_object_t * );
46 static int Demux ( input_thread_t * );
48 static int OpenDevice( input_thread_t *, string, vlc_bool_t );
49 static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
50 list<string> *, vlc_bool_t );
51 static AM_MEDIA_TYPE EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
52 int, int, int, int, int, int );
53 static bool ConnectFilters( IFilterGraph *, IBaseFilter *, IPin * );
56 /* Debug only, use this to find out GUIDs */
58 UuidToString( (IID *)&IID_IAMBufferNegotiation, &p_st );
59 msg_Err( p_input, "BufferNegotiation: %s" , p_st );
62 /*****************************************************************************
64 *****************************************************************************/
65 #define CACHING_TEXT N_("Caching value in ms")
66 #define CACHING_LONGTEXT N_( \
67 "Allows you to modify the default caching value for directshow streams. " \
68 "This value should be set in miliseconds units." )
69 #define VDEV_TEXT N_("Video device name")
70 #define VDEV_LONGTEXT N_( \
71 "You can specify the name of the video device that will be used by the " \
72 "DirectShow plugin. If you don't specify anything, the default device " \
74 #define ADEV_TEXT N_("Audio device name")
75 #define ADEV_LONGTEXT N_( \
76 "You can specify the name of the audio device that will be used by the " \
77 "DirectShow plugin. If you don't specify anything, the default device " \
79 #define SIZE_TEXT N_("Video size")
80 #define SIZE_LONGTEXT N_( \
81 "You can specify the size of the video that will be displayed by the " \
82 "DirectShow plugin. If you don't specify anything the default size for " \
83 "your device will be used.")
84 #define CHROMA_TEXT N_("Video input chroma format")
85 #define CHROMA_LONGTEXT N_( \
86 "Force the DirectShow video input to use a specific chroma format " \
87 "(eg. I420 (default), RV24, etc...)")
90 set_description( _("DirectShow input") );
91 add_category_hint( N_("dshow"), NULL, VLC_TRUE );
92 add_integer( "dshow-caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
93 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
94 add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, VLC_FALSE);
95 add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE);
96 add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE);
97 add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
99 add_shortcut( "dshow" );
100 set_capability( "access", 0 );
101 set_callbacks( AccessOpen, AccessClose );
104 set_description( _("DirectShow demuxer") );
105 add_shortcut( "dshow" );
106 set_capability( "demux", 200 );
107 set_callbacks( DemuxOpen, DemuxClose );
111 /****************************************************************************
113 ****************************************************************************/
119 * fcc "auds"|"vids" 0
136 static void SetDWBE( uint8_t *p, uint32_t dw )
138 p[0] = (dw >> 24)&0xff;
139 p[1] = (dw >> 16)&0xff;
140 p[2] = (dw >> 8)&0xff;
144 static void SetQWBE( uint8_t *p, uint64_t qw )
146 SetDWBE( p, (qw >> 32)&0xffffffff );
147 SetDWBE( &p[4], qw&0xffffffff );
150 /****************************************************************************
151 * DirectShow elementary stream descriptor
152 ****************************************************************************/
153 typedef struct dshow_stream_t
156 IBaseFilter *p_device_filter;
157 CaptureFilter *p_capture_filter;
164 VIDEOINFOHEADER video;
173 /****************************************************************************
174 * Access descriptor declaration
175 ****************************************************************************/
181 IFilterGraph *p_graph;
182 IMediaControl *p_control;
189 /* list of elementary streams */
190 dshow_stream_t **pp_streams;
192 int i_current_stream;
194 /* misc properties */
200 /*****************************************************************************
201 * Open: open direct show device
202 *****************************************************************************/
203 static int AccessOpen( vlc_object_t *p_this )
205 input_thread_t *p_input = (input_thread_t *)p_this;
209 /* Get/parse options and open device(s) */
210 string vdevname, adevname;
211 int i_width = 0, i_height = 0, i_chroma = VLC_FOURCC('I','4','2','0');
213 var_Create( p_input, "dshow-vdev", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
214 var_Get( p_input, "dshow-vdev", &val );
215 if( val.psz_string ) vdevname = string( val.psz_string );
216 if( val.psz_string ) free( val.psz_string );
218 var_Create( p_input, "dshow-adev", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
219 var_Get( p_input, "dshow-adev", &val );
220 if( val.psz_string ) adevname = string( val.psz_string );
221 if( val.psz_string ) free( val.psz_string );
223 var_Create( p_input, "dshow-size", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
224 var_Get( p_input, "dshow-size", &val );
225 if( val.psz_string && *val.psz_string )
227 if( !strcmp( val.psz_string, "subqcif" ) )
229 i_width = 128; i_height = 96;
231 else if( !strcmp( val.psz_string, "qsif" ) )
233 i_width = 160; i_height = 120;
235 else if( !strcmp( val.psz_string, "qcif" ) )
237 i_width = 176; i_height = 144;
239 else if( !strcmp( val.psz_string, "sif" ) )
241 i_width = 320; i_height = 240;
243 else if( !strcmp( val.psz_string, "cif" ) )
245 i_width = 352; i_height = 288;
247 else if( !strcmp( val.psz_string, "vga" ) )
249 i_width = 640; i_height = 480;
255 i_width = strtol( val.psz_string, &psz_parser, 0 );
256 if( *psz_parser == 'x' || *psz_parser == 'X')
258 i_height = strtol( psz_parser + 1, &psz_parser, 0 );
260 msg_Dbg( p_input, "Width x Height %dx%d", i_width, i_height );
263 if( val.psz_string ) free( val.psz_string );
265 var_Create( p_input, "dshow-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
266 var_Get( p_input, "dshow-chroma", &val );
267 if( val.psz_string && strlen( val.psz_string ) >= 4 )
269 i_chroma = VLC_FOURCC( val.psz_string[0], val.psz_string[1],
270 val.psz_string[2], val.psz_string[3] );
272 if( val.psz_string ) free( val.psz_string );
274 p_input->pf_read = Read;
275 p_input->pf_seek = NULL;
276 p_input->pf_set_area = NULL;
277 p_input->pf_set_program = NULL;
279 vlc_mutex_lock( &p_input->stream.stream_lock );
280 p_input->stream.b_pace_control = 0;
281 p_input->stream.b_seekable = 0;
282 p_input->stream.p_selected_area->i_size = 0;
283 p_input->stream.p_selected_area->i_tell = 0;
284 p_input->stream.i_method = INPUT_METHOD_FILE;
285 vlc_mutex_unlock( &p_input->stream.stream_lock );
287 var_Create( p_input, "dshow-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
288 var_Get( p_input, "dshow-caching", &val );
289 p_input->i_pts_delay = val.i_int * 1000;
291 /* Initialize OLE/COM */
292 CoInitializeEx( 0, COINIT_APARTMENTTHREADED );
294 /* create access private data */
295 p_input->p_access_data = p_sys =
296 (access_sys_t *)malloc( sizeof( access_sys_t ) );
298 /* Initialize some data */
299 p_sys->i_streams = 0;
300 p_sys->pp_streams = (dshow_stream_t **)malloc( 1 );
301 p_sys->i_width = i_width;
302 p_sys->i_height = i_height;
303 p_sys->i_chroma = i_chroma;
306 p_sys->i_header_size = 8;
307 p_sys->p_header = (uint8_t *)malloc( p_sys->i_header_size );
308 memcpy( &p_sys->p_header[0], ".dsh", 4 );
309 SetDWBE( &p_sys->p_header[4], 1 );
310 p_sys->i_header_pos = p_sys->i_header_size;
312 /* Build directshow graph */
313 CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
314 (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph );
316 p_sys->p_graph->QueryInterface( IID_IMediaControl,
317 (void **)&p_sys->p_control );
319 if( OpenDevice( p_input, vdevname, 0 ) != VLC_SUCCESS )
321 msg_Err( p_input, "can't open video");
324 if( OpenDevice( p_input, adevname, 1 ) != VLC_SUCCESS )
326 msg_Err( p_input, "can't open audio");
329 if( !p_sys->i_streams )
331 /* Release directshow objects */
332 if( p_sys->p_control ) p_sys->p_control->Release();
333 p_sys->p_graph->Release();
335 /* Uninitialize OLE/COM */
338 free( p_sys->p_header );
339 free( p_sys->pp_streams );
344 /* Initialize some data */
345 p_sys->i_current_stream = 0;
346 p_input->i_mtu += p_sys->i_header_size + 16 /* data header size */;
348 vlc_mutex_init( p_input, &p_sys->lock );
349 vlc_cond_init( p_input, &p_sys->wait );
351 /* Everything is ready. Let's rock baby */
352 p_sys->p_control->Run();
357 /*****************************************************************************
358 * AccessClose: close device
359 *****************************************************************************/
360 static void AccessClose( vlc_object_t *p_this )
362 input_thread_t *p_input = (input_thread_t *)p_this;
363 access_sys_t *p_sys = p_input->p_access_data;
365 /* Stop capturing stuff */
366 p_sys->p_control->Stop();
367 p_sys->p_control->Release();
369 /* Remove filters from graph */
370 for( int i = 0; i < p_sys->i_streams; i++ )
372 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_capture_filter );
373 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_device_filter );
374 p_sys->pp_streams[i]->p_capture_filter->Release();
375 p_sys->pp_streams[i]->p_device_filter->Release();
377 p_sys->p_graph->Release();
379 /* Uninitialize OLE/COM */
382 free( p_sys->p_header );
383 for( int i = 0; i < p_sys->i_streams; i++ ) delete p_sys->pp_streams[i];
384 free( p_sys->pp_streams );
388 /****************************************************************************
390 ****************************************************************************/
391 static bool ConnectFilters( IFilterGraph *p_graph, IBaseFilter *p_filter,
394 IEnumPins *p_enumpins;
398 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;
400 while( S_OK == p_enumpins->Next( 1, &p_output_pin, &i_fetched ) )
402 if( S_OK == p_graph->ConnectDirect( p_output_pin, p_input_pin, 0 ) )
404 p_enumpins->Release();
409 p_enumpins->Release();
413 static int OpenDevice( input_thread_t *p_input, string devicename,
416 access_sys_t *p_sys = p_input->p_access_data;
417 list<string> list_devices;
419 /* Enumerate devices and display their names */
420 FindCaptureDevice( (vlc_object_t *)p_input, NULL, &list_devices, b_audio );
422 if( !list_devices.size() )
425 list<string>::iterator iter;
426 for( iter = list_devices.begin(); iter != list_devices.end(); iter++ )
427 msg_Dbg( p_input, "found device: %s", iter->c_str() );
429 /* If no device name was specified, pick the 1st one */
430 if( devicename.size() == 0 )
432 devicename = *list_devices.begin();
435 // Use the system device enumerator and class enumerator to find
436 // a capture/preview device, such as a desktop USB video camera.
437 IBaseFilter *p_device_filter =
438 FindCaptureDevice( (vlc_object_t *)p_input, &devicename,
440 if( p_device_filter )
441 msg_Dbg( p_input, "using device: %s", devicename.c_str() );
444 msg_Err( p_input, "can't use device: %s", devicename.c_str() );
448 AM_MEDIA_TYPE media_type =
449 EnumDeviceCaps( (vlc_object_t *)p_input, p_device_filter,
450 p_sys->i_chroma, p_sys->i_width, p_sys->i_height,
453 /* Create and add our capture filter */
454 CaptureFilter *p_capture_filter = new CaptureFilter( p_input, media_type );
455 p_sys->p_graph->AddFilter( p_capture_filter, 0 );
457 /* Add the device filter to the graph (seems necessary with VfW before
458 * accessing pin attributes). */
459 p_sys->p_graph->AddFilter( p_device_filter, 0 );
461 /* Attempt to connect one of this device's capture output pins */
462 msg_Dbg( p_input, "connecting filters" );
463 if( ConnectFilters( p_sys->p_graph, p_device_filter,
464 p_capture_filter->CustomGetPin() ) )
467 dshow_stream_t dshow_stream;
468 dshow_stream.b_invert = VLC_FALSE;
469 dshow_stream.b_pts = VLC_FALSE;
471 p_capture_filter->CustomGetPin()->CustomGetMediaType();
473 if( dshow_stream.mt.majortype == MEDIATYPE_Video )
475 msg_Dbg( p_input, "MEDIATYPE_Video");
477 /* Packed RGB formats */
478 if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB1 )
479 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '1' );
480 if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB4 )
481 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '4' );
482 if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB8 )
483 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '8' );
484 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB555 )
485 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '1', '5' );
486 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB565 )
487 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '1', '6' );
488 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB24 )
489 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '2', '4' );
490 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB32 )
491 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '3', '2' );
492 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_ARGB32 )
493 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', 'A' );
495 /* Packed YUV formats */
496 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YVYU )
497 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'V', 'Y', 'U' );
498 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YUYV )
499 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'U', 'Y', 'V' );
500 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_Y411 )
501 dshow_stream.i_fourcc = VLC_FOURCC( 'I', '4', '1', 'N' );
502 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_Y211 )
503 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', '2', '1', '1' );
504 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YUY2 ||
505 dshow_stream.mt.subtype == MEDIASUBTYPE_UYVY )
506 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'U', 'Y', '2' );
508 /* Planar YUV formats */
509 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_I420 )
510 dshow_stream.i_fourcc = VLC_FOURCC( 'I', '4', '2', '0' );
511 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_Y41P )
512 dshow_stream.i_fourcc = VLC_FOURCC( 'I', '4', '1', '1' );
513 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YV12 ||
514 dshow_stream.mt.subtype == MEDIASUBTYPE_IYUV )
515 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'V', '1', '2' );
516 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YVU9 )
517 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'V', 'U', '9' );
521 dshow_stream.header.video =
522 *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
524 int i_height = dshow_stream.header.video.bmiHeader.biHeight;
526 /* Check if the image is inverted (bottom to top) */
527 if( dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', '1' ) ||
528 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', '4' ) ||
529 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', '8' ) ||
530 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
531 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
532 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
533 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
534 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', 'A' ) )
536 if( i_height > 0 ) dshow_stream.b_invert = VLC_TRUE;
537 else i_height = - i_height;
540 /* Add video stream to header */
541 p_sys->i_header_size += 20;
542 p_sys->p_header = (uint8_t *)realloc( p_sys->p_header,
543 p_sys->i_header_size );
544 memcpy( &p_sys->p_header[p_sys->i_header_pos], "vids", 4 );
545 memcpy( &p_sys->p_header[p_sys->i_header_pos + 4],
546 &dshow_stream.i_fourcc, 4 );
547 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 8],
548 dshow_stream.header.video.bmiHeader.biWidth );
549 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 12], i_height );
550 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 16], 0 );
551 p_sys->i_header_pos = p_sys->i_header_size;
553 /* Greatly simplifies the reading routine */
554 int i_mtu = dshow_stream.header.video.bmiHeader.biWidth *
555 dshow_stream.header.video.bmiHeader.biHeight * 4;
556 p_input->i_mtu = __MAX(p_input->i_mtu,i_mtu);
559 else if( dshow_stream.mt.majortype == MEDIATYPE_Audio &&
560 dshow_stream.mt.formattype == FORMAT_WaveFormatEx )
562 msg_Dbg( p_input, "MEDIATYPE_Audio");
564 if( dshow_stream.mt.subtype == MEDIASUBTYPE_PCM )
565 dshow_stream.i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
566 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_IEEE_FLOAT )
567 dshow_stream.i_fourcc = VLC_FOURCC( 'f', 'l', '3', '2' );
570 dshow_stream.header.audio =
571 *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
573 /* Add audio stream to header */
574 p_sys->i_header_size += 20;
575 p_sys->p_header = (uint8_t *)realloc( p_sys->p_header,
576 p_sys->i_header_size );
577 memcpy( &p_sys->p_header[p_sys->i_header_pos], "auds", 4 );
578 memcpy( &p_sys->p_header[p_sys->i_header_pos + 4],
579 &dshow_stream.i_fourcc, 4 );
580 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 8],
581 dshow_stream.header.audio.nChannels );
582 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 12],
583 dshow_stream.header.audio.nSamplesPerSec );
584 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 16],
585 dshow_stream.header.audio.wBitsPerSample );
586 p_sys->i_header_pos = p_sys->i_header_size;
588 /* Greatly simplifies the reading routine */
589 IAMBufferNegotiation *p_ambuf;
593 p_capture_filter->CustomGetPin()->ConnectedTo( &p_pin );
594 if( SUCCEEDED( p_pin->QueryInterface(
595 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
597 ALLOCATOR_PROPERTIES AllocProp;
598 memset( &AllocProp, 0, sizeof( ALLOCATOR_PROPERTIES ) );
599 p_ambuf->GetAllocatorProperties( &AllocProp );
601 i_mtu = AllocProp.cbBuffer;
606 i_mtu = dshow_stream.header.audio.nSamplesPerSec *
607 dshow_stream.header.audio.nChannels *
608 dshow_stream.header.audio.wBitsPerSample / 8;
611 p_input->i_mtu = __MAX( p_input->i_mtu, i_mtu );
615 /* Add directshow elementary stream to our list */
616 dshow_stream.p_device_filter = p_device_filter;
617 dshow_stream.p_capture_filter = p_capture_filter;
620 (dshow_stream_t **)realloc( p_sys->pp_streams,
621 sizeof(dshow_stream_t *)
622 * (p_sys->i_streams + 1) );
623 p_sys->pp_streams[p_sys->i_streams] = new dshow_stream_t;
624 *p_sys->pp_streams[p_sys->i_streams++] = dshow_stream;
625 SetDWBE( &p_sys->p_header[4], (uint32_t)p_sys->i_streams );
631 /* Remove filters from graph */
632 p_sys->p_graph->RemoveFilter( p_device_filter );
633 p_sys->p_graph->RemoveFilter( p_capture_filter );
635 /* Release objects */
636 p_device_filter->Release();
637 p_capture_filter->Release();
643 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
644 list<string> *p_listdevices, vlc_bool_t b_audio )
646 IBaseFilter *p_base_filter = NULL;
647 IMoniker *p_moniker = NULL;
651 /* Create the system device enumerator */
652 ICreateDevEnum *p_dev_enum = NULL;
654 hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
655 IID_ICreateDevEnum, (void **)&p_dev_enum );
658 msg_Err( p_this, "failed to create the device enumerator (0x%x)", hr);
662 /* Create an enumerator for the video capture devices */
663 IEnumMoniker *p_class_enum = NULL;
665 hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
668 hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
670 p_dev_enum->Release();
673 msg_Err( p_this, "failed to create the class enumerator (0x%x)", hr );
677 /* If there are no enumerators for the requested type, then
678 * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
679 if( p_class_enum == NULL )
681 msg_Err( p_this, "no capture device was detected." );
685 /* Enumerate the devices */
687 /* Note that if the Next() call succeeds but there are no monikers,
688 * it will return S_FALSE (which is not a failure). Therefore, we check
689 * that the return code is S_OK instead of using SUCCEEDED() macro. */
691 while( p_class_enum->Next( 1, &p_moniker, &i_fetched ) == S_OK )
693 /* Getting the property page to get the device name */
695 hr = p_moniker->BindToStorage( 0, 0, IID_IPropertyBag,
701 hr = p_bag->Read( L"FriendlyName", &var, NULL );
705 int i_convert = ( lstrlenW( var.bstrVal ) + 1 ) * 2;
706 char *p_buf = (char *)alloca( i_convert ); p_buf[0] = 0;
707 WideCharToMultiByte( CP_ACP, 0, var.bstrVal, -1, p_buf,
708 i_convert, NULL, NULL );
709 SysFreeString(var.bstrVal);
711 if( p_listdevices ) p_listdevices->push_back( p_buf );
713 if( p_devicename && *p_devicename == string(p_buf) )
715 /* Bind Moniker to a filter object */
716 hr = p_moniker->BindToObject( 0, 0, IID_IBaseFilter,
717 (void **)&p_base_filter );
720 msg_Err( p_this, "couldn't bind moniker to filter "
721 "object (0x%x)", hr );
722 p_moniker->Release();
723 p_class_enum->Release();
726 p_moniker->Release();
727 p_class_enum->Release();
728 return p_base_filter;
733 p_moniker->Release();
736 p_class_enum->Release();
740 static AM_MEDIA_TYPE EnumDeviceCaps( vlc_object_t *p_this,
741 IBaseFilter *p_filter,
742 int i_chroma, int i_width, int i_height,
743 int i_channels, int i_samplespersec,
744 int i_bitspersample )
746 IEnumPins *p_enumpins;
748 IEnumMediaTypes *p_enummt;
750 AM_MEDIA_TYPE media_type;
751 media_type.majortype = GUID_NULL;
752 media_type.subtype = GUID_NULL;
753 media_type.formattype = GUID_NULL;
754 media_type.pUnk = NULL;
755 media_type.cbFormat = 0;
756 media_type.pbFormat = NULL;
758 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return media_type;
760 /*while*/if( p_enumpins->Next( 1, &p_output_pin, NULL ) == S_OK )
763 if( SUCCEEDED( p_output_pin->EnumMediaTypes( &p_enummt ) ) )
766 while( p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
769 if( p_mt->majortype == MEDIATYPE_Video )
771 int i_fourcc = VLC_FOURCC(' ', ' ', ' ', ' ');
773 /* Packed RGB formats */
774 if( p_mt->subtype == MEDIASUBTYPE_RGB1 )
775 i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '1' );
776 if( p_mt->subtype == MEDIASUBTYPE_RGB4 )
777 i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '4' );
778 if( p_mt->subtype == MEDIASUBTYPE_RGB8 )
779 i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '8' );
780 else if( p_mt->subtype == MEDIASUBTYPE_RGB555 )
781 i_fourcc = VLC_FOURCC( 'R', 'V', '1', '5' );
782 else if( p_mt->subtype == MEDIASUBTYPE_RGB565 )
783 i_fourcc = VLC_FOURCC( 'R', 'V', '1', '6' );
784 else if( p_mt->subtype == MEDIASUBTYPE_RGB24 )
785 i_fourcc = VLC_FOURCC( 'R', 'V', '2', '4' );
786 else if( p_mt->subtype == MEDIASUBTYPE_RGB32 )
787 i_fourcc = VLC_FOURCC( 'R', 'V', '3', '2' );
788 else if( p_mt->subtype == MEDIASUBTYPE_ARGB32 )
789 i_fourcc = VLC_FOURCC( 'R', 'G', 'B', 'A' );
790 else i_fourcc = *((int *)&p_mt->subtype);
792 int i_current_width = p_mt->pbFormat ?
793 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth : 0;
794 int i_current_height = p_mt->pbFormat ?
795 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight : 0;
797 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
798 "accepts chroma: %4.4s, width:%i, height:%i",
799 (char *)&i_fourcc, i_current_width,
802 if( (!i_chroma || i_fourcc == i_chroma) &&
803 (!i_width || i_width == i_current_width) &&
804 (!i_height || i_height == i_current_height) )
806 /* Pick the 1st match */
809 i_width = i_current_width;
810 i_height = i_current_height;
814 FreeMediaType( *p_mt );
817 else if( p_mt->majortype == MEDIATYPE_Audio )
820 int i_current_channels =
821 ((WAVEFORMATEX *)p_mt->pbFormat)->nChannels;
822 int i_current_samplespersec =
823 ((WAVEFORMATEX *)p_mt->pbFormat)->nSamplesPerSec;
824 int i_current_bitspersample =
825 ((WAVEFORMATEX *)p_mt->pbFormat)->wBitsPerSample;
827 if( p_mt->subtype == MEDIASUBTYPE_PCM )
828 i_fourcc = VLC_FOURCC( 'p', 'c', 'm', ' ' );
829 else i_fourcc = *((int *)&p_mt->subtype);
831 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
832 "accepts format: %4.4s, channels:%i, "
833 "samples/sec:%i bits/sample:%i",
834 (char *)&i_fourcc, i_current_channels,
835 i_current_samplespersec, i_current_bitspersample);
837 if( (!i_channels || i_channels == i_current_channels) &&
839 i_samplespersec == i_current_samplespersec) &&
841 i_bitspersample == i_current_bitspersample) )
843 /* Pick the 1st match */
845 i_channels = i_current_channels;
846 i_samplespersec = i_current_samplespersec;
847 i_bitspersample = i_current_bitspersample;
849 /* Setup a few properties like the audio latency */
850 IAMBufferNegotiation *p_ambuf;
852 if( SUCCEEDED( p_output_pin->QueryInterface(
853 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
855 ALLOCATOR_PROPERTIES AllocProp;
856 AllocProp.cbAlign = -1;
857 AllocProp.cbBuffer = i_channels * i_samplespersec *
858 i_bitspersample / 8 / 10 ; /*100 ms of latency*/
859 AllocProp.cbPrefix = -1;
860 AllocProp.cBuffers = -1;
861 p_ambuf->SuggestAllocatorProperties( &AllocProp );
867 FreeMediaType( *p_mt );
871 CoTaskMemFree( (PVOID)p_mt );
876 p_output_pin->Release();
879 p_enumpins->Release();
883 /*****************************************************************************
884 * Read: reads from the device into PES packets.
885 *****************************************************************************
886 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
888 *****************************************************************************/
889 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer,
892 access_sys_t *p_sys = p_input->p_access_data;
893 dshow_stream_t *p_stream = NULL;
894 byte_t *p_buf_orig = p_buffer;
895 VLCMediaSample sample;
899 if( p_sys->i_header_pos )
901 /* First header of the stream */
902 memcpy( p_buffer, p_sys->p_header, p_sys->i_header_size );
903 p_buffer += p_sys->i_header_size;
904 p_sys->i_header_pos = 0;
909 /* Get new sample/frame from next elementary stream.
910 * We first loop through all the elementary streams and if all our
911 * fifos are empty we block until we are signaled some new data has
913 vlc_mutex_lock( &p_sys->lock );
916 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
918 p_stream = p_sys->pp_streams[i_stream];
919 if( p_stream->mt.majortype == MEDIATYPE_Audio &&
920 p_stream->p_capture_filter &&
921 p_stream->p_capture_filter->CustomGetPin()
922 ->CustomGetSample( &sample ) == S_OK )
927 if( i_stream == p_sys->i_streams )
928 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
930 p_stream = p_sys->pp_streams[i_stream];
931 if( p_stream->p_capture_filter &&
932 p_stream->p_capture_filter->CustomGetPin()
933 ->CustomGetSample( &sample ) == S_OK )
938 if( i_stream == p_sys->i_streams )
940 /* No data available. Wait until some data has arrived */
941 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
942 vlc_mutex_unlock( &p_sys->lock );
946 vlc_mutex_unlock( &p_sys->lock );
951 i_data_size = sample.p_sample->GetActualDataLength();
952 sample.p_sample->GetPointer( &p_data );
954 REFERENCE_TIME i_pts, i_end_date;
955 HRESULT hr = sample.p_sample->GetTime( &i_pts, &i_end_date );
956 if( hr != VFW_S_NO_STOP_TIME && hr != S_OK ) i_pts = 0;
960 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
962 /* Use our data timestamp */
963 i_pts = sample.i_timestamp;
964 p_stream->b_pts = VLC_TRUE;
969 msg_Dbg( p_input, "Read() stream: %i PTS: "I64Fd, i_stream, i_pts );
972 /* Create pseudo header */
973 SetDWBE( &p_sys->p_header[0], i_stream );
974 SetDWBE( &p_sys->p_header[4], i_data_size );
975 SetQWBE( &p_sys->p_header[8], i_pts * 9 / 1000 );
978 msg_Info( p_input, "access read %i data_size %i", i_len, i_data_size );
981 /* First copy header */
982 memcpy( p_buffer, p_sys->p_header, 16 /* header size */ );
983 p_buffer += 16 /* header size */;
985 /* Then copy stream data if any */
986 if( !p_stream->b_invert )
988 p_input->p_vlc->pf_memcpy( p_buffer, p_data, i_data_size );
989 p_buffer += i_data_size;
993 int i_width = p_stream->header.video.bmiHeader.biWidth;
994 int i_height = p_stream->header.video.bmiHeader.biHeight;
996 switch( p_stream->i_fourcc )
998 case VLC_FOURCC( 'R', 'V', '1', '5' ):
999 case VLC_FOURCC( 'R', 'V', '1', '6' ):
1002 case VLC_FOURCC( 'R', 'V', '2', '4' ):
1005 case VLC_FOURCC( 'R', 'V', '3', '2' ):
1006 case VLC_FOURCC( 'R', 'G', 'B', 'A' ):
1011 for( int i = i_height - 1; i >= 0; i-- )
1013 p_input->p_vlc->pf_memcpy( p_buffer,
1014 &p_data[i * i_width], i_width );
1016 p_buffer += i_width;
1020 sample.p_sample->Release();
1022 /* The caller got what he wanted */
1023 return p_buffer - p_buf_orig;
1026 return 0; /* never reached */
1029 /****************************************************************************
1031 ****************************************************************************/
1032 static int DemuxOpen( vlc_object_t *p_this )
1034 input_thread_t *p_input = (input_thread_t *)p_this;
1040 data_packet_t *p_pk;
1042 /* a little test to see if it's a dshow stream */
1043 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
1045 msg_Warn( p_input, "dshow plugin discarded (cannot peek)" );
1046 return VLC_EGENERIC;
1049 if( strcmp( (const char *)p_peek, ".dsh" ) ||
1050 GetDWBE( &p_peek[4] ) <= 0 )
1052 msg_Warn( p_input, "dshow plugin discarded (not a valid stream)" );
1053 return VLC_EGENERIC;
1056 /* create one program */
1057 vlc_mutex_lock( &p_input->stream.stream_lock );
1058 if( input_InitStream( p_input, 0 ) == -1)
1060 vlc_mutex_unlock( &p_input->stream.stream_lock );
1061 msg_Err( p_input, "cannot init stream" );
1062 return VLC_EGENERIC;
1064 if( input_AddProgram( p_input, 0, 0) == NULL )
1066 vlc_mutex_unlock( &p_input->stream.stream_lock );
1067 msg_Err( p_input, "cannot add program" );
1068 return VLC_EGENERIC;
1071 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1072 p_input->stream.i_mux_rate = 0;
1074 i_streams = GetDWBE( &p_peek[4] );
1075 if( input_Peek( p_input, &p_peek, 8 + 20 * i_streams )
1076 < 8 + 20 * i_streams )
1078 msg_Err( p_input, "dshow plugin discarded (cannot peek)" );
1079 return VLC_EGENERIC;
1083 for( i = 0; i < i_streams; i++ )
1085 es_descriptor_t *p_es;
1087 if( !strncmp( (const char *)p_peek, "auds", 4 ) )
1089 #define wf ((WAVEFORMATEX*)p_es->p_waveformatex)
1090 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
1091 i + 1, AUDIO_ES, NULL, 0 );
1092 p_es->i_stream_id = i + 1;
1094 VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
1096 p_es->p_waveformatex= malloc( sizeof( WAVEFORMATEX ) );
1098 wf->wFormatTag = 0;//WAVE_FORMAT_UNKNOWN;
1099 wf->nChannels = GetDWBE( &p_peek[8] );
1100 wf->nSamplesPerSec = GetDWBE( &p_peek[12] );
1101 wf->wBitsPerSample = GetDWBE( &p_peek[16] );
1102 wf->nBlockAlign = wf->wBitsPerSample * wf->nChannels / 8;
1103 wf->nAvgBytesPerSec = wf->nBlockAlign * wf->nSamplesPerSec;
1106 msg_Dbg( p_input, "added new audio es %d channels %dHz",
1107 wf->nChannels, wf->nSamplesPerSec );
1109 input_SelectES( p_input, p_es );
1112 else if( !strncmp( (const char *)p_peek, "vids", 4 ) )
1114 #define bih ((BITMAPINFOHEADER*)p_es->p_bitmapinfoheader)
1115 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
1116 i + 1, VIDEO_ES, NULL, 0 );
1117 p_es->i_stream_id = i + 1;
1119 VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
1121 p_es->p_bitmapinfoheader = malloc( sizeof( BITMAPINFOHEADER ) );
1123 bih->biSize = sizeof( BITMAPINFOHEADER );
1124 bih->biWidth = GetDWBE( &p_peek[8] );
1125 bih->biHeight = GetDWBE( &p_peek[12] );
1127 bih->biBitCount = 0;
1128 bih->biCompression = 0;
1129 bih->biSizeImage= 0;
1130 bih->biXPelsPerMeter = 0;
1131 bih->biYPelsPerMeter = 0;
1133 bih->biClrImportant = 0;
1135 msg_Dbg( p_input, "added new video es %4.4s %dx%d",
1136 (char*)&p_es->i_fourcc, bih->biWidth, bih->biHeight );
1138 input_SelectES( p_input, p_es );
1145 p_input->stream.p_selected_program->b_is_ok = 1;
1146 vlc_mutex_unlock( &p_input->stream.stream_lock );
1148 if( input_SplitBuffer( p_input, &p_pk, 8 + i_streams * 20 ) > 0 )
1150 input_DeletePacket( p_input->p_method_data, p_pk );
1153 p_input->pf_demux = Demux;
1154 p_input->pf_demux_control = demux_vaControlDefault;
1158 static void DemuxClose( vlc_object_t *p_this )
1163 static int Demux( input_thread_t *p_input )
1165 es_descriptor_t *p_es;
1166 pes_packet_t *p_pes;
1173 if( input_Peek( p_input, &p_peek, 16 ) < 16 )
1175 msg_Warn( p_input, "cannot peek (EOF ?)" );
1179 i_stream = GetDWBE( &p_peek[0] );
1180 i_size = GetDWBE( &p_peek[4] );
1181 i_pcr = GetQWBE( &p_peek[8] );
1183 p_es = p_input->stream.p_selected_program->pp_es[i_stream];
1186 msg_Err( p_input, "cannot find ES" );
1189 p_pes = input_NewPES( p_input->p_method_data );
1192 msg_Warn( p_input, "cannot allocate PES" );
1199 data_packet_t *p_data;
1202 if( (i_read = input_SplitBuffer( p_input, &p_data, i_size ) ) <= 0 )
1204 input_DeletePES( p_input->p_method_data, p_pes );
1207 if( !p_pes->p_first )
1209 p_pes->p_first = p_data;
1210 p_pes->i_nb_data = 1;
1211 p_pes->i_pes_size = i_read;
1215 p_pes->p_last->p_next = p_data;
1217 p_pes->i_pes_size += i_read;
1219 p_pes->p_last = p_data;
1223 p_pes->p_first->p_payload_start += 16;
1224 p_pes->i_pes_size -= 16;
1226 if( p_es && p_es->p_decoder_fifo )
1228 /* Call the pace control. */
1229 input_ClockManageRef( p_input, p_input->stream.p_selected_program,
1232 p_pes->i_pts = p_pes->i_dts = i_pcr <= 0 ? 0 :
1233 input_ClockGetTS( p_input, p_input->stream.p_selected_program,
1236 input_DecodePES( p_es->p_decoder_fifo, p_pes );
1240 input_DeletePES( p_input->p_method_data, p_pes );