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>
37 /*****************************************************************************
38 * Access: local prototypes
39 *****************************************************************************/
40 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
41 static ssize_t ReadCompressed( input_thread_t *, byte_t *, size_t );
43 static int OpenDevice( input_thread_t *, string, vlc_bool_t );
44 static IBaseFilter *FindCaptureDevice( vlc_object_t *, string *,
45 list<string> *, vlc_bool_t );
46 static AM_MEDIA_TYPE EnumDeviceCaps( vlc_object_t *, IBaseFilter *,
47 int, int, int, int, int, int );
48 static bool ConnectFilters( vlc_object_t *, IBaseFilter *, CaptureFilter * );
50 static int FindDevicesCallback( vlc_object_t *, char const *,
51 vlc_value_t, vlc_value_t, void * );
52 static int ConfigDevicesCallback( vlc_object_t *, char const *,
53 vlc_value_t, vlc_value_t, void * );
55 static void PropertiesPage( vlc_object_t *, IBaseFilter *,
56 ICaptureGraphBuilder2 *, vlc_bool_t );
59 /* Debug only, use this to find out GUIDs */
61 UuidToString( (IID *)&IID_IAMBufferNegotiation, &p_st );
62 msg_Err( p_input, "BufferNegotiation: %s" , p_st );
86 static void SetDWBE( uint8_t *p, uint32_t dw )
88 p[0] = (dw >> 24)&0xff;
89 p[1] = (dw >> 16)&0xff;
90 p[2] = (dw >> 8)&0xff;
94 static void SetQWBE( uint8_t *p, uint64_t qw )
96 SetDWBE( p, (qw >> 32)&0xffffffff );
97 SetDWBE( &p[4], qw&0xffffffff );
100 /*****************************************************************************
102 *****************************************************************************/
103 static char *ppsz_vdev[] = { "", "none" };
104 static char *ppsz_vdev_text[] = { N_("Default"), N_("None") };
105 static char *ppsz_adev[] = { "", "none" };
106 static char *ppsz_adev_text[] = { N_("Default"), N_("None") };
108 #define CACHING_TEXT N_("Caching value in ms")
109 #define CACHING_LONGTEXT N_( \
110 "Allows you to modify the default caching value for DirectShow streams. " \
111 "This value should be set in milliseconds units." )
112 #define VDEV_TEXT N_("Video device name")
113 #define VDEV_LONGTEXT N_( \
114 "You can specify the name of the video device that will be used by the " \
115 "DirectShow plugin. If you don't specify anything, the default device " \
117 #define ADEV_TEXT N_("Audio device name")
118 #define ADEV_LONGTEXT N_( \
119 "You can specify the name of the audio device that will be used by the " \
120 "DirectShow plugin. If you don't specify anything, the default device " \
122 #define SIZE_TEXT N_("Video size")
123 #define SIZE_LONGTEXT N_( \
124 "You can specify the size of the video that will be displayed by the " \
125 "DirectShow plugin. If you don't specify anything the default size for " \
126 "your device will be used.")
127 #define CHROMA_TEXT N_("Video input chroma format")
128 #define CHROMA_LONGTEXT N_( \
129 "Force the DirectShow video input to use a specific chroma format " \
130 "(eg. I420 (default), RV24, etc.)")
131 #define CONFIG_TEXT N_("Device properties")
132 #define CONFIG_LONGTEXT N_( \
133 "Show the properties dialog of the selected device before starting the " \
136 static int AccessOpen ( vlc_object_t * );
137 static void AccessClose( vlc_object_t * );
139 static int DemuxOpen ( vlc_object_t * );
140 static void DemuxClose ( vlc_object_t * );
143 set_shortname( _("DirectShow") );
144 set_description( _("DirectShow input") );
145 add_integer( "dshow-caching", (mtime_t)(0.2*CLOCK_FREQ) / 1000, NULL,
146 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
148 add_string( "dshow-vdev", NULL, NULL, VDEV_TEXT, VDEV_LONGTEXT, VLC_FALSE);
149 change_string_list( ppsz_vdev, ppsz_vdev_text, FindDevicesCallback );
150 change_action_add( FindDevicesCallback, N_("Refresh list") );
151 change_action_add( ConfigDevicesCallback, N_("Configure") );
153 add_string( "dshow-adev", NULL, NULL, ADEV_TEXT, ADEV_LONGTEXT, VLC_FALSE);
154 change_string_list( ppsz_adev, ppsz_adev_text, FindDevicesCallback );
155 change_action_add( FindDevicesCallback, N_("Refresh list") );
156 change_action_add( ConfigDevicesCallback, N_("Configure") );
158 add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE);
160 add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
163 add_bool( "dshow-config", VLC_FALSE, NULL, CONFIG_TEXT, CONFIG_LONGTEXT,
166 add_shortcut( "dshow" );
167 set_capability( "access", 0 );
168 set_callbacks( AccessOpen, AccessClose );
171 set_description( _("DirectShow demuxer") );
172 add_shortcut( "dshow" );
173 set_capability( "demux", 200 );
174 set_callbacks( DemuxOpen, DemuxClose );
178 /****************************************************************************
179 * DirectShow elementary stream descriptor
180 ****************************************************************************/
181 typedef struct dshow_stream_t
184 IBaseFilter *p_device_filter;
185 CaptureFilter *p_capture_filter;
192 VIDEOINFOHEADER video;
201 /****************************************************************************
202 * Access descriptor declaration
203 ****************************************************************************/
204 #define MAX_CROSSBAR_DEPTH 10
206 typedef struct CrossbarRouteRec {
208 LONG VideoInputIndex;
209 LONG VideoOutputIndex;
210 LONG AudioInputIndex;
211 LONG AudioOutputIndex;
219 IFilterGraph *p_graph;
220 ICaptureGraphBuilder2 *p_capture_graph_builder2;
221 IMediaControl *p_control;
223 int i_crossbar_route_depth;
224 CrossbarRoute crossbar_routes[MAX_CROSSBAR_DEPTH];
231 /* list of elementary streams */
232 dshow_stream_t **pp_streams;
234 int i_current_stream;
236 /* misc properties */
243 /****************************************************************************
244 * DirectShow utility functions
245 ****************************************************************************/
246 static void CreateDirectShowGraph( access_sys_t *p_sys )
248 p_sys->i_crossbar_route_depth = 0;
250 /* Create directshow filter graph */
251 if( SUCCEEDED( CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,
252 (REFIID)IID_IFilterGraph, (void **)&p_sys->p_graph) ) )
254 /* Create directshow capture graph builder if available */
255 if( SUCCEEDED( CoCreateInstance( CLSID_CaptureGraphBuilder2, 0,
256 CLSCTX_INPROC, (REFIID)IID_ICaptureGraphBuilder2,
257 (void **)&p_sys->p_capture_graph_builder2 ) ) )
259 p_sys->p_capture_graph_builder2->
260 SetFiltergraph((IGraphBuilder *)p_sys->p_graph);
263 p_sys->p_graph->QueryInterface( IID_IMediaControl,
264 (void **)&p_sys->p_control );
268 static void DeleteCrossbarRoutes( access_sys_t *p_sys )
270 /* Remove crossbar filters from graph */
271 for( int i = 0; i < p_sys->i_crossbar_route_depth; i++ )
273 p_sys->crossbar_routes[i].pXbar->Release();
275 p_sys->i_crossbar_route_depth = 0;
278 static void DeleteDirectShowGraph( access_sys_t *p_sys )
280 DeleteCrossbarRoutes( p_sys );
282 /* Remove filters from graph */
283 for( int i = 0; i < p_sys->i_streams; i++ )
285 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_capture_filter );
286 p_sys->p_graph->RemoveFilter( p_sys->pp_streams[i]->p_device_filter );
287 p_sys->pp_streams[i]->p_capture_filter->Release();
288 p_sys->pp_streams[i]->p_device_filter->Release();
291 /* Release directshow objects */
292 if( p_sys->p_control )
294 p_sys->p_control->Release();
295 p_sys->p_control = NULL;
297 if( p_sys->p_capture_graph_builder2 )
299 p_sys->p_capture_graph_builder2->Release();
300 p_sys->p_capture_graph_builder2 = NULL;
305 p_sys->p_graph->Release();
306 p_sys->p_graph = NULL;
310 static void ReleaseDirectShow( input_thread_t *p_input )
312 msg_Dbg( p_input, "Releasing DirectShow");
314 access_sys_t *p_sys = p_input->p_access_data;
316 DeleteDirectShowGraph( p_sys );
318 /* Uninitialize OLE/COM */
321 free( p_sys->p_header );
322 /* Remove filters from graph */
323 for( int i = 0; i < p_sys->i_streams; i++ )
325 delete p_sys->pp_streams[i];
327 free( p_sys->pp_streams );
330 p_input->p_access_data = NULL;
333 /*****************************************************************************
334 * Open: open direct show device
335 *****************************************************************************/
336 static int AccessOpen( vlc_object_t *p_this )
338 input_thread_t *p_input = (input_thread_t *)p_this;
342 /* Get/parse options and open device(s) */
343 string vdevname, adevname;
344 int i_width = 0, i_height = 0, i_chroma = 0;
346 var_Create( p_input, "dshow-config", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
348 var_Create( p_input, "dshow-vdev", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
349 var_Get( p_input, "dshow-vdev", &val );
350 if( val.psz_string ) vdevname = string( val.psz_string );
351 if( val.psz_string ) free( val.psz_string );
353 var_Create( p_input, "dshow-adev", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
354 var_Get( p_input, "dshow-adev", &val );
355 if( val.psz_string ) adevname = string( val.psz_string );
356 if( val.psz_string ) free( val.psz_string );
358 var_Create( p_input, "dshow-size", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
359 var_Get( p_input, "dshow-size", &val );
360 if( val.psz_string && *val.psz_string )
362 if( !strcmp( val.psz_string, "subqcif" ) )
364 i_width = 128; i_height = 96;
366 else if( !strcmp( val.psz_string, "qsif" ) )
368 i_width = 160; i_height = 120;
370 else if( !strcmp( val.psz_string, "qcif" ) )
372 i_width = 176; i_height = 144;
374 else if( !strcmp( val.psz_string, "sif" ) )
376 i_width = 320; i_height = 240;
378 else if( !strcmp( val.psz_string, "cif" ) )
380 i_width = 352; i_height = 288;
382 else if( !strcmp( val.psz_string, "vga" ) )
384 i_width = 640; i_height = 480;
390 i_width = strtol( val.psz_string, &psz_parser, 0 );
391 if( *psz_parser == 'x' || *psz_parser == 'X')
393 i_height = strtol( psz_parser + 1, &psz_parser, 0 );
395 msg_Dbg( p_input, "Width x Height %dx%d", i_width, i_height );
398 if( val.psz_string ) free( val.psz_string );
400 var_Create( p_input, "dshow-chroma", VLC_VAR_STRING | VLC_VAR_DOINHERIT);
401 var_Get( p_input, "dshow-chroma", &val );
402 if( val.psz_string && strlen( val.psz_string ) >= 4 )
404 i_chroma = VLC_FOURCC( val.psz_string[0], val.psz_string[1],
405 val.psz_string[2], val.psz_string[3] );
407 if( val.psz_string ) free( val.psz_string );
409 p_input->pf_read = Read;
410 p_input->pf_seek = NULL;
411 p_input->pf_set_area = NULL;
412 p_input->pf_set_program = NULL;
414 vlc_mutex_lock( &p_input->stream.stream_lock );
415 p_input->stream.b_pace_control = 0;
416 p_input->stream.b_seekable = 0;
417 p_input->stream.p_selected_area->i_size = 0;
418 p_input->stream.p_selected_area->i_tell = 0;
419 p_input->stream.i_method = INPUT_METHOD_FILE;
420 vlc_mutex_unlock( &p_input->stream.stream_lock );
422 var_Create( p_input, "dshow-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
423 var_Get( p_input, "dshow-caching", &val );
424 p_input->i_pts_delay = val.i_int * 1000;
426 /* Initialize OLE/COM */
429 /* create access private data */
430 p_input->p_access_data = p_sys =
431 (access_sys_t *)malloc( sizeof( access_sys_t ) );
433 /* Initialize some data */
434 p_sys->i_streams = 0;
435 p_sys->pp_streams = (dshow_stream_t **)malloc( 1 );
436 p_sys->i_width = i_width;
437 p_sys->i_height = i_height;
438 p_sys->i_chroma = i_chroma;
439 p_sys->b_audio = VLC_TRUE;
442 p_sys->i_header_size = 8;
443 p_sys->p_header = (uint8_t *)malloc( p_sys->i_header_size );
444 memcpy( &p_sys->p_header[0], ".dsh", 4 );
445 SetDWBE( &p_sys->p_header[4], 1 );
446 p_sys->i_header_pos = p_sys->i_header_size;
448 p_sys->p_graph = NULL;
449 p_sys->p_capture_graph_builder2 = NULL;
450 p_sys->p_control = NULL;
452 /* Build directshow graph */
453 CreateDirectShowGraph( p_sys );
455 if( OpenDevice( p_input, vdevname, 0 ) != VLC_SUCCESS )
457 msg_Err( p_input, "can't open video");
460 if( p_sys->b_audio && OpenDevice( p_input, adevname, 1 ) != VLC_SUCCESS )
462 msg_Err( p_input, "can't open audio");
465 for( int i = 0; i < p_sys->i_crossbar_route_depth; i++ )
467 IAMCrossbar *pXbar = p_sys->crossbar_routes[i].pXbar;
468 LONG VideoInputIndex = p_sys->crossbar_routes[i].VideoInputIndex;
469 LONG VideoOutputIndex = p_sys->crossbar_routes[i].VideoOutputIndex;
470 LONG AudioInputIndex = p_sys->crossbar_routes[i].AudioInputIndex;
471 LONG AudioOutputIndex = p_sys->crossbar_routes[i].AudioOutputIndex;
473 if( SUCCEEDED(pXbar->Route(VideoOutputIndex, VideoInputIndex)) )
475 msg_Dbg( p_input, "Crossbar at depth %d, Routed video ouput %d to "
476 "video input %d", i, VideoOutputIndex, VideoInputIndex );
478 if( AudioOutputIndex != -1 && AudioInputIndex != -1 )
480 if( SUCCEEDED( pXbar->Route(AudioOutputIndex,
483 msg_Dbg(p_input, "Crossbar at depth %d, Routed audio "
484 "ouput %d to audio input %d", i,
485 AudioOutputIndex, AudioInputIndex );
491 if( !p_sys->i_streams )
493 ReleaseDirectShow( p_input );
497 /* Initialize some data */
498 p_sys->i_current_stream = 0;
499 p_input->i_mtu += p_sys->i_header_size + 16 /* data header size */;
501 vlc_mutex_init( p_input, &p_sys->lock );
502 vlc_cond_init( p_input, &p_sys->wait );
504 msg_Dbg( p_input, "Playing...");
506 /* Everything is ready. Let's rock baby */
507 p_sys->p_control->Run();
512 /*****************************************************************************
513 * AccessClose: close device
514 *****************************************************************************/
515 static void AccessClose( vlc_object_t *p_this )
517 input_thread_t *p_input = (input_thread_t *)p_this;
518 access_sys_t *p_sys = p_input->p_access_data;
520 /* Stop capturing stuff */
521 p_sys->p_control->Stop();
523 ReleaseDirectShow(p_input);
526 /****************************************************************************
527 * RouteCrossbars (Does not AddRef the returned *Pin)
528 ****************************************************************************/
529 static HRESULT GetCrossbarIPinAtIndex( IAMCrossbar *pXbar, LONG PinIndex,
530 BOOL IsInputPin, IPin ** ppPin )
532 LONG cntInPins, cntOutPins;
534 IBaseFilter *pFilter = NULL;
538 if( !pXbar || !ppPin ) return E_POINTER;
542 if( S_OK != pXbar->get_PinCounts(&cntOutPins, &cntInPins) ) return E_FAIL;
544 LONG TrueIndex = IsInputPin ? PinIndex : PinIndex + cntInPins;
546 if( pXbar->QueryInterface(IID_IBaseFilter, (void **)&pFilter) == S_OK )
548 if( SUCCEEDED(pFilter->EnumPins(&pins)) )
551 while( pins->Next(1, &pP, &n) == S_OK )
566 return *ppPin ? S_OK : E_FAIL;
569 /****************************************************************************
570 * GetCrossbarIndexFromIPin: Find corresponding index of an IPin on a crossbar
571 ****************************************************************************/
572 static HRESULT GetCrossbarIndexFromIPin( IAMCrossbar * pXbar, LONG * PinIndex,
573 BOOL IsInputPin, IPin * pPin )
575 LONG cntInPins, cntOutPins;
577 IBaseFilter *pFilter = NULL;
582 if(!pXbar || !PinIndex || !pPin )
585 if( S_OK != pXbar->get_PinCounts(&cntOutPins, &cntInPins) )
588 if( pXbar->QueryInterface(IID_IBaseFilter, (void **)&pFilter) == S_OK )
590 if( SUCCEEDED(pFilter->EnumPins(&pins)) )
594 while( pins->Next(1, &pP, &n) == S_OK )
599 *PinIndex = IsInputPin ? i : i - cntInPins;
610 return fOK ? S_OK : E_FAIL;
613 /****************************************************************************
615 ****************************************************************************/
616 static HRESULT FindCrossbarRoutes( vlc_object_t *p_this, IPin *p_input_pin,
617 LONG physicalType, int depth = 0 )
619 access_sys_t *p_sys = ((input_thread_t *)p_this)->p_access_data;
620 HRESULT result = S_FALSE;
623 if( FAILED(p_input_pin->ConnectedTo(&p_output_pin)) ) return S_FALSE;
625 // It is connected, so now find out if the filter supports IAMCrossbar
627 if( FAILED(p_output_pin->QueryPinInfo(&pinInfo)) ||
628 PINDIR_OUTPUT != pinInfo.dir )
630 p_output_pin->Release ();
634 IAMCrossbar *pXbar=0;
635 if( FAILED(pinInfo.pFilter->QueryInterface(IID_IAMCrossbar,
638 pinInfo.pFilter->Release();
639 p_output_pin->Release ();
643 LONG inputPinCount, outputPinCount;
644 if( FAILED(pXbar->get_PinCounts(&outputPinCount, &inputPinCount)) )
647 pinInfo.pFilter->Release();
648 p_output_pin->Release ();
652 LONG inputPinIndexRelated, outputPinIndexRelated;
653 LONG inputPinPhysicalType, outputPinPhysicalType;
654 LONG inputPinIndex, outputPinIndex;
655 if( FAILED(GetCrossbarIndexFromIPin( pXbar, &outputPinIndex,
656 FALSE, p_output_pin )) ||
657 FAILED(pXbar->get_CrossbarPinInfo( FALSE, outputPinIndex,
658 &outputPinIndexRelated,
659 &outputPinPhysicalType )) )
662 pinInfo.pFilter->Release();
663 p_output_pin->Release ();
668 // for all input pins
670 for( inputPinIndex = 0; S_OK != result && inputPinIndex < inputPinCount;
673 if( FAILED(pXbar->get_CrossbarPinInfo( TRUE, inputPinIndex,
674 &inputPinIndexRelated, &inputPinPhysicalType )) ) continue;
676 // Is the pin a video pin?
677 if( inputPinPhysicalType != physicalType ) continue;
680 if( FAILED(pXbar->CanRoute(outputPinIndex, inputPinIndex)) ) continue;
683 if( FAILED(GetCrossbarIPinAtIndex( pXbar, inputPinIndex,
684 TRUE, &pPin)) ) continue;
686 result = FindCrossbarRoutes( p_this, pPin, physicalType, depth+1 );
687 if( S_OK == result || (S_FALSE == result &&
688 physicalType == inputPinPhysicalType &&
689 (p_sys->i_crossbar_route_depth = depth+1) < MAX_CROSSBAR_DEPTH) )
694 // remember crossbar route
695 p_sys->crossbar_routes[depth].pXbar = pXbar;
696 p_sys->crossbar_routes[depth].VideoInputIndex = inputPinIndex;
697 p_sys->crossbar_routes[depth].VideoOutputIndex = outputPinIndex;
698 p_sys->crossbar_routes[depth].AudioInputIndex = inputPinIndexRelated;
699 p_sys->crossbar_routes[depth].AudioOutputIndex = outputPinIndexRelated;
701 msg_Dbg( p_this, "Crossbar at depth %d, Found Route For ouput %ld "
702 "(type %ld) to input %d (type %ld)", depth,
703 outputPinIndex, outputPinPhysicalType, inputPinIndex,
704 inputPinPhysicalType );
711 pinInfo.pFilter->Release();
712 p_output_pin->Release ();
717 /****************************************************************************
719 ****************************************************************************/
720 static bool ConnectFilters( vlc_object_t *p_this, IBaseFilter *p_filter,
721 CaptureFilter *p_capture_filter )
723 access_sys_t *p_sys = ((input_thread_t *)p_this)->p_access_data;
724 CapturePin *p_input_pin = p_capture_filter->CustomGetPin();
726 AM_MEDIA_TYPE mediaType = p_input_pin->CustomGetMediaType();
728 if( p_sys->p_capture_graph_builder2 )
730 if( FAILED(p_sys->p_capture_graph_builder2->
731 RenderStream( &PIN_CATEGORY_CAPTURE, &mediaType.majortype,
733 (IBaseFilter *)p_capture_filter )) )
738 // Sort out all the possible video inputs
739 // The class needs to be given the capture filters ANALOGVIDEO input pin
741 if( mediaType.majortype == MEDIATYPE_Video &&
742 SUCCEEDED(p_filter->EnumPins(&pins)) )
748 IKsPropertySet *pKs=0;
752 while( !Found && (S_OK == pins->Next(1, &pP, &n)) )
754 if(S_OK == pP->QueryPinInfo(&pinInfo))
756 if(pinInfo.dir == PINDIR_INPUT)
758 // is this pin an ANALOGVIDEOIN input pin?
759 if( pP->QueryInterface(IID_IKsPropertySet,
760 (void **)&pKs) == S_OK )
762 if( pKs->Get(AMPROPSETID_Pin,
763 AMPROPERTY_PIN_CATEGORY, NULL, 0,
764 &guid, sizeof(GUID), &dw) == S_OK )
766 if( guid == PIN_CATEGORY_ANALOGVIDEOIN )
768 // recursively search crossbar routes
769 FindCrossbarRoutes( p_this, pP,
770 PhysConn_Video_Tuner );
778 pinInfo.pFilter->Release();
788 IEnumPins *p_enumpins;
791 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return false;
793 while( S_OK == p_enumpins->Next( 1, &p_pin, NULL ) )
795 PIN_DIRECTION pin_dir;
796 p_pin->QueryDirection( &pin_dir );
798 if( pin_dir == PINDIR_OUTPUT &&
799 p_sys->p_graph->ConnectDirect( p_pin, (IPin *)p_input_pin,
803 p_enumpins->Release();
809 p_enumpins->Release();
814 static int OpenDevice( input_thread_t *p_input, string devicename,
817 access_sys_t *p_sys = p_input->p_access_data;
819 /* See if device is already opened */
820 for( int i = 0; i < p_sys->i_streams; i++ )
822 if( p_sys->pp_streams[i]->devicename == devicename )
829 list<string> list_devices;
831 /* Enumerate devices and display their names */
832 FindCaptureDevice( (vlc_object_t *)p_input, NULL, &list_devices, b_audio );
834 if( !list_devices.size() )
837 list<string>::iterator iter;
838 for( iter = list_devices.begin(); iter != list_devices.end(); iter++ )
839 msg_Dbg( p_input, "found device: %s", iter->c_str() );
841 /* If no device name was specified, pick the 1st one */
842 if( devicename.size() == 0 )
844 devicename = *list_devices.begin();
847 // Use the system device enumerator and class enumerator to find
848 // a capture/preview device, such as a desktop USB video camera.
849 IBaseFilter *p_device_filter =
850 FindCaptureDevice( (vlc_object_t *)p_input, &devicename,
852 if( p_device_filter )
853 msg_Dbg( p_input, "using device: %s", devicename.c_str() );
856 msg_Err( p_input, "can't use device: %s", devicename.c_str() );
860 AM_MEDIA_TYPE media_type =
861 EnumDeviceCaps( (vlc_object_t *)p_input, p_device_filter,
862 p_sys->i_chroma, p_sys->i_width, p_sys->i_height,
865 /* Create and add our capture filter */
866 CaptureFilter *p_capture_filter = new CaptureFilter( p_input, media_type );
867 p_sys->p_graph->AddFilter( p_capture_filter, 0 );
869 /* Add the device filter to the graph (seems necessary with VfW before
870 * accessing pin attributes). */
871 p_sys->p_graph->AddFilter( p_device_filter, 0 );
873 /* Attempt to connect one of this device's capture output pins */
874 msg_Dbg( p_input, "connecting filters" );
875 if( ConnectFilters( VLC_OBJECT(p_input), p_device_filter,
879 msg_Dbg( p_input, "filters connected successfully !" );
881 dshow_stream_t dshow_stream;
882 dshow_stream.b_invert = VLC_FALSE;
883 dshow_stream.b_pts = VLC_FALSE;
885 p_capture_filter->CustomGetPin()->CustomGetMediaType();
887 /* Show properties. Done here so the VLC stream is setup with the
888 * proper parameters. */
890 var_Get( p_input, "dshow-config", &val );
893 PropertiesPage( VLC_OBJECT(p_input), p_device_filter,
894 p_sys->p_capture_graph_builder2,
895 dshow_stream.mt.majortype == MEDIATYPE_Audio ||
896 dshow_stream.mt.formattype == FORMAT_WaveFormatEx);
900 p_capture_filter->CustomGetPin()->CustomGetMediaType();
902 if( dshow_stream.mt.majortype == MEDIATYPE_Video )
904 msg_Dbg( p_input, "MEDIATYPE_Video");
906 /* Packed RGB formats */
907 if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB1 )
908 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '1' );
909 if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB4 )
910 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '4' );
911 if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB8 )
912 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', '8' );
913 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB555 )
914 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '1', '5' );
915 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB565 )
916 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '1', '6' );
917 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB24 )
918 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '2', '4' );
919 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_RGB32 )
920 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'V', '3', '2' );
921 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_ARGB32 )
922 dshow_stream.i_fourcc = VLC_FOURCC( 'R', 'G', 'B', 'A' );
924 /* Packed YUV formats */
925 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YVYU )
926 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'V', 'Y', 'U' );
927 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YUYV )
928 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'U', 'Y', 'V' );
929 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_Y411 )
930 dshow_stream.i_fourcc = VLC_FOURCC( 'I', '4', '1', 'N' );
931 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_Y211 )
932 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', '2', '1', '1' );
933 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YUY2 ||
934 dshow_stream.mt.subtype == MEDIASUBTYPE_UYVY )
935 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'U', 'Y', '2' );
937 /* Planar YUV formats */
938 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_I420 )
939 dshow_stream.i_fourcc = VLC_FOURCC( 'I', '4', '2', '0' );
940 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_Y41P )
941 dshow_stream.i_fourcc = VLC_FOURCC( 'I', '4', '1', '1' );
942 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YV12 ||
943 dshow_stream.mt.subtype == MEDIASUBTYPE_IYUV )
944 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'V', '1', '2' );
945 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_YVU9 )
946 dshow_stream.i_fourcc = VLC_FOURCC( 'Y', 'V', 'U', '9' );
949 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_dvsl )
950 dshow_stream.i_fourcc = VLC_FOURCC( 'd', 'v', 's', 'l' );
951 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_dvsd )
952 dshow_stream.i_fourcc = VLC_FOURCC( 'd', 'v', 's', 'd' );
953 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_dvhd )
954 dshow_stream.i_fourcc = VLC_FOURCC( 'd', 'v', 'h', 'd' );
956 /* MPEG video formats */
957 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_MPEG2_VIDEO )
958 dshow_stream.i_fourcc = VLC_FOURCC( 'm', 'p', '2', 'v' );
962 dshow_stream.header.video =
963 *(VIDEOINFOHEADER *)dshow_stream.mt.pbFormat;
965 int i_height = dshow_stream.header.video.bmiHeader.biHeight;
967 /* Check if the image is inverted (bottom to top) */
968 if( dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', '1' ) ||
969 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', '4' ) ||
970 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', '8' ) ||
971 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '1', '5' ) ||
972 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '1', '6' ) ||
973 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '2', '4' ) ||
974 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'V', '3', '2' ) ||
975 dshow_stream.i_fourcc == VLC_FOURCC( 'R', 'G', 'B', 'A' ) )
977 if( i_height > 0 ) dshow_stream.b_invert = VLC_TRUE;
978 else i_height = - i_height;
981 /* Check if we are dealing with a DV stream */
982 if( dshow_stream.i_fourcc == VLC_FOURCC( 'd', 'v', 's', 'l' ) ||
983 dshow_stream.i_fourcc == VLC_FOURCC( 'd', 'v', 's', 'd' ) ||
984 dshow_stream.i_fourcc == VLC_FOURCC( 'd', 'v', 'h', 'd' ) )
986 p_input->pf_read = ReadCompressed;
987 if( !p_input->psz_demux || !*p_input->psz_demux )
989 p_input->psz_demux = "rawdv";
991 p_sys->b_audio = VLC_FALSE;
994 /* Check if we are dealing with an MPEG video stream */
995 if( dshow_stream.i_fourcc == VLC_FOURCC( 'm', 'p', '2', 'v' ) )
997 p_input->pf_read = ReadCompressed;
998 if( !p_input->psz_demux || !*p_input->psz_demux )
1000 p_input->psz_demux = "mpgv";
1002 p_sys->b_audio = VLC_FALSE;
1005 /* Add video stream to header */
1006 p_sys->i_header_size += 20;
1007 p_sys->p_header = (uint8_t *)realloc( p_sys->p_header,
1008 p_sys->i_header_size );
1009 memcpy( &p_sys->p_header[p_sys->i_header_pos], "vids", 4 );
1010 memcpy( &p_sys->p_header[p_sys->i_header_pos + 4],
1011 &dshow_stream.i_fourcc, 4 );
1012 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 8],
1013 dshow_stream.header.video.bmiHeader.biWidth );
1014 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 12], i_height );
1015 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 16], 0 );
1016 p_sys->i_header_pos = p_sys->i_header_size;
1018 /* Greatly simplifies the reading routine */
1019 int i_mtu = dshow_stream.header.video.bmiHeader.biWidth *
1021 p_input->i_mtu = __MAX( p_input->i_mtu, (unsigned int)i_mtu );
1024 else if( dshow_stream.mt.majortype == MEDIATYPE_Audio &&
1025 dshow_stream.mt.formattype == FORMAT_WaveFormatEx )
1027 msg_Dbg( p_input, "MEDIATYPE_Audio");
1029 if( dshow_stream.mt.subtype == MEDIASUBTYPE_PCM )
1030 dshow_stream.i_fourcc = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1031 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_IEEE_FLOAT )
1032 dshow_stream.i_fourcc = VLC_FOURCC( 'f', 'l', '3', '2' );
1035 dshow_stream.header.audio =
1036 *(WAVEFORMATEX *)dshow_stream.mt.pbFormat;
1038 /* Add audio stream to header */
1039 p_sys->i_header_size += 20;
1040 p_sys->p_header = (uint8_t *)realloc( p_sys->p_header,
1041 p_sys->i_header_size );
1042 memcpy( &p_sys->p_header[p_sys->i_header_pos], "auds", 4 );
1043 memcpy( &p_sys->p_header[p_sys->i_header_pos + 4],
1044 &dshow_stream.i_fourcc, 4 );
1045 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 8],
1046 dshow_stream.header.audio.nChannels );
1047 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 12],
1048 dshow_stream.header.audio.nSamplesPerSec );
1049 SetDWBE( &p_sys->p_header[p_sys->i_header_pos + 16],
1050 dshow_stream.header.audio.wBitsPerSample );
1051 p_sys->i_header_pos = p_sys->i_header_size;
1053 /* Greatly simplifies the reading routine */
1054 IAMBufferNegotiation *p_ambuf;
1058 p_capture_filter->CustomGetPin()->ConnectedTo( &p_pin );
1059 if( SUCCEEDED( p_pin->QueryInterface(
1060 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
1062 ALLOCATOR_PROPERTIES AllocProp;
1063 memset( &AllocProp, 0, sizeof( ALLOCATOR_PROPERTIES ) );
1064 p_ambuf->GetAllocatorProperties( &AllocProp );
1066 i_mtu = AllocProp.cbBuffer;
1071 i_mtu = dshow_stream.header.audio.nSamplesPerSec *
1072 dshow_stream.header.audio.nChannels *
1073 dshow_stream.header.audio.wBitsPerSample / 8;
1076 p_input->i_mtu = __MAX( p_input->i_mtu, (unsigned int)i_mtu );
1079 else if( dshow_stream.mt.majortype == MEDIATYPE_Stream )
1081 msg_Dbg( p_input, "MEDIATYPE_Stream" );
1083 if( dshow_stream.mt.subtype == MEDIASUBTYPE_MPEG2_PROGRAM )
1084 dshow_stream.i_fourcc = VLC_FOURCC( 'm', 'p', '2', 'p' );
1085 else if( dshow_stream.mt.subtype == MEDIASUBTYPE_MPEG2_TRANSPORT )
1086 dshow_stream.i_fourcc = VLC_FOURCC( 'm', 'p', '2', 't' );
1088 msg_Dbg( p_input, "selected stream pin accepts format: %4.4s",
1089 (char *)&dshow_stream.i_fourcc);
1091 p_sys->b_audio = VLC_FALSE;
1092 p_sys->i_header_size = 0;
1093 p_sys->i_header_pos = 0;
1094 p_input->i_mtu = INPUT_DEFAULT_BUFSIZE;
1096 p_input->pf_read = ReadCompressed;
1097 p_input->pf_set_program = input_SetProgram;
1102 msg_Dbg( p_input, "unknown stream majortype" );
1106 /* Add directshow elementary stream to our list */
1107 dshow_stream.p_device_filter = p_device_filter;
1108 dshow_stream.p_capture_filter = p_capture_filter;
1111 (dshow_stream_t **)realloc( p_sys->pp_streams,
1112 sizeof(dshow_stream_t *)
1113 * (p_sys->i_streams + 1) );
1114 p_sys->pp_streams[p_sys->i_streams] = new dshow_stream_t;
1115 *p_sys->pp_streams[p_sys->i_streams++] = dshow_stream;
1116 SetDWBE( &p_sys->p_header[4], (uint32_t)p_sys->i_streams );
1122 /* Remove filters from graph */
1123 p_sys->p_graph->RemoveFilter( p_device_filter );
1124 p_sys->p_graph->RemoveFilter( p_capture_filter );
1126 /* Release objects */
1127 p_device_filter->Release();
1128 p_capture_filter->Release();
1130 return VLC_EGENERIC;
1133 static IBaseFilter *
1134 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
1135 list<string> *p_listdevices, vlc_bool_t b_audio )
1137 IBaseFilter *p_base_filter = NULL;
1138 IMoniker *p_moniker = NULL;
1142 /* Create the system device enumerator */
1143 ICreateDevEnum *p_dev_enum = NULL;
1145 hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC,
1146 IID_ICreateDevEnum, (void **)&p_dev_enum );
1149 msg_Err( p_this, "failed to create the device enumerator (0x%x)", hr);
1153 /* Create an enumerator for the video capture devices */
1154 IEnumMoniker *p_class_enum = NULL;
1156 hr = p_dev_enum->CreateClassEnumerator( CLSID_VideoInputDeviceCategory,
1159 hr = p_dev_enum->CreateClassEnumerator( CLSID_AudioInputDeviceCategory,
1161 p_dev_enum->Release();
1164 msg_Err( p_this, "failed to create the class enumerator (0x%x)", hr );
1168 /* If there are no enumerators for the requested type, then
1169 * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
1170 if( p_class_enum == NULL )
1172 msg_Err( p_this, "no capture device was detected" );
1176 /* Enumerate the devices */
1178 /* Note that if the Next() call succeeds but there are no monikers,
1179 * it will return S_FALSE (which is not a failure). Therefore, we check
1180 * that the return code is S_OK instead of using SUCCEEDED() macro. */
1182 while( p_class_enum->Next( 1, &p_moniker, &i_fetched ) == S_OK )
1184 /* Getting the property page to get the device name */
1185 IPropertyBag *p_bag;
1186 hr = p_moniker->BindToStorage( 0, 0, IID_IPropertyBag,
1192 hr = p_bag->Read( L"FriendlyName", &var, NULL );
1196 int i_convert = ( lstrlenW( var.bstrVal ) + 1 ) * 2;
1197 char *p_buf = (char *)alloca( i_convert ); p_buf[0] = 0;
1198 WideCharToMultiByte( CP_ACP, 0, var.bstrVal, -1, p_buf,
1199 i_convert, NULL, NULL );
1200 SysFreeString(var.bstrVal);
1202 if( p_listdevices ) p_listdevices->push_back( p_buf );
1204 if( p_devicename && *p_devicename == string(p_buf) )
1206 /* Bind Moniker to a filter object */
1207 hr = p_moniker->BindToObject( 0, 0, IID_IBaseFilter,
1208 (void **)&p_base_filter );
1211 msg_Err( p_this, "couldn't bind moniker to filter "
1212 "object (0x%x)", hr );
1213 p_moniker->Release();
1214 p_class_enum->Release();
1217 p_moniker->Release();
1218 p_class_enum->Release();
1219 return p_base_filter;
1224 p_moniker->Release();
1227 p_class_enum->Release();
1231 static AM_MEDIA_TYPE EnumDeviceCaps( vlc_object_t *p_this,
1232 IBaseFilter *p_filter,
1233 int i_fourcc, int i_width, int i_height,
1234 int i_channels, int i_samplespersec,
1235 int i_bitspersample )
1237 IEnumPins *p_enumpins;
1239 IEnumMediaTypes *p_enummt;
1240 int i_orig_fourcc = i_fourcc;
1241 vlc_bool_t b_found = VLC_FALSE;
1243 AM_MEDIA_TYPE media_type;
1244 media_type.majortype = GUID_NULL;
1245 media_type.subtype = GUID_NULL;
1246 media_type.formattype = GUID_NULL;
1247 media_type.pUnk = NULL;
1248 media_type.cbFormat = 0;
1249 media_type.pbFormat = NULL;
1251 if( S_OK != p_filter->EnumPins( &p_enumpins ) ) return media_type;
1253 while( S_OK == p_enumpins->Next( 1, &p_output_pin, NULL ) )
1257 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1259 msg_Dbg( p_this, "EnumDeviceCaps: %s pin: %S",
1260 info.dir == PINDIR_INPUT ? "input" : "output",
1262 if( info.pFilter ) info.pFilter->Release();
1265 p_output_pin->Release();
1267 p_enumpins->Reset();
1269 while( !b_found && p_enumpins->Next( 1, &p_output_pin, NULL ) == S_OK )
1273 if( S_OK == p_output_pin->QueryPinInfo( &info ) )
1275 if( info.pFilter ) info.pFilter->Release();
1276 if( info.dir == PINDIR_INPUT )
1278 p_output_pin->Release();
1281 msg_Dbg( p_this, "EnumDeviceCaps: trying pin %S", info.achName );
1285 if( SUCCEEDED( p_output_pin->EnumMediaTypes( &p_enummt ) ) )
1287 AM_MEDIA_TYPE *p_mt;
1288 while( p_enummt->Next( 1, &p_mt, NULL ) == S_OK )
1291 if( p_mt->majortype == MEDIATYPE_Video )
1293 int i_current_fourcc = VLC_FOURCC(' ', ' ', ' ', ' ');
1295 /* Packed RGB formats */
1296 if( p_mt->subtype == MEDIASUBTYPE_RGB1 )
1297 i_current_fourcc = VLC_FOURCC( 'R', 'G', 'B', '1' );
1298 if( p_mt->subtype == MEDIASUBTYPE_RGB4 )
1299 i_current_fourcc = VLC_FOURCC( 'R', 'G', 'B', '4' );
1300 if( p_mt->subtype == MEDIASUBTYPE_RGB8 )
1301 i_current_fourcc = VLC_FOURCC( 'R', 'G', 'B', '8' );
1302 else if( p_mt->subtype == MEDIASUBTYPE_RGB555 )
1303 i_current_fourcc = VLC_FOURCC( 'R', 'V', '1', '5' );
1304 else if( p_mt->subtype == MEDIASUBTYPE_RGB565 )
1305 i_current_fourcc = VLC_FOURCC( 'R', 'V', '1', '6' );
1306 else if( p_mt->subtype == MEDIASUBTYPE_RGB24 )
1307 i_current_fourcc = VLC_FOURCC( 'R', 'V', '2', '4' );
1308 else if( p_mt->subtype == MEDIASUBTYPE_RGB32 )
1309 i_current_fourcc = VLC_FOURCC( 'R', 'V', '3', '2' );
1310 else if( p_mt->subtype == MEDIASUBTYPE_ARGB32 )
1311 i_current_fourcc = VLC_FOURCC( 'R', 'G', 'B', 'A' );
1313 /* Packed YUV formats */
1314 else if( p_mt->subtype == MEDIASUBTYPE_YVYU )
1315 i_current_fourcc = VLC_FOURCC( 'Y', 'V', 'Y', 'U' );
1316 else if( p_mt->subtype == MEDIASUBTYPE_YUYV )
1317 i_current_fourcc = VLC_FOURCC( 'Y', 'U', 'Y', 'V' );
1318 else if( p_mt->subtype == MEDIASUBTYPE_Y411 )
1319 i_current_fourcc = VLC_FOURCC( 'I', '4', '1', 'N' );
1320 else if( p_mt->subtype == MEDIASUBTYPE_Y211 )
1321 i_current_fourcc = VLC_FOURCC( 'Y', '2', '1', '1' );
1322 else if( p_mt->subtype == MEDIASUBTYPE_YUY2 ||
1323 p_mt->subtype == MEDIASUBTYPE_UYVY )
1324 i_current_fourcc = VLC_FOURCC( 'Y', 'U', 'Y', '2' );
1326 /* MPEG2 video elementary stream */
1327 else if( p_mt->subtype == MEDIASUBTYPE_MPEG2_VIDEO )
1328 i_current_fourcc = VLC_FOURCC( 'm', 'p', '2', 'v' );
1330 /* hauppauge pvr video preview */
1331 else if( p_mt->subtype == MEDIASUBTYPE_PREVIEW_VIDEO )
1332 i_current_fourcc = VLC_FOURCC( 'P', 'V', 'R', 'V' );
1334 else i_current_fourcc = *((int *)&p_mt->subtype);
1336 int i_current_width = p_mt->pbFormat ?
1337 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biWidth : 0;
1338 int i_current_height = p_mt->pbFormat ?
1339 ((VIDEOINFOHEADER *)p_mt->pbFormat)->bmiHeader.biHeight : 0;
1340 if( i_current_height < 0 )
1341 i_current_height = -i_current_height;
1343 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1344 "accepts chroma: %4.4s, width:%i, height:%i",
1345 (char *)&i_current_fourcc, i_current_width,
1348 if( ( !i_fourcc || i_fourcc == i_current_fourcc ||
1349 ( !i_orig_fourcc && i_current_fourcc ==
1350 VLC_FOURCC('I','4','2','0') ) ) &&
1351 ( !i_width || i_width == i_current_width ) &&
1352 ( !i_height || i_height == i_current_height ) )
1354 /* Pick the 1st match */
1356 i_fourcc = i_current_fourcc;
1357 i_width = i_current_width;
1358 i_height = i_current_height;
1363 FreeMediaType( *p_mt );
1366 else if( p_mt->majortype == MEDIATYPE_Audio )
1368 int i_current_fourcc;
1369 int i_current_channels =
1370 ((WAVEFORMATEX *)p_mt->pbFormat)->nChannels;
1371 int i_current_samplespersec =
1372 ((WAVEFORMATEX *)p_mt->pbFormat)->nSamplesPerSec;
1373 int i_current_bitspersample =
1374 ((WAVEFORMATEX *)p_mt->pbFormat)->wBitsPerSample;
1376 if( p_mt->subtype == MEDIASUBTYPE_PCM )
1377 i_current_fourcc = VLC_FOURCC( 'p', 'c', 'm', ' ' );
1378 else i_current_fourcc = *((int *)&p_mt->subtype);
1380 msg_Dbg( p_this, "EnumDeviceCaps: input pin "
1381 "accepts format: %4.4s, channels:%i, "
1382 "samples/sec:%i bits/sample:%i",
1383 (char *)&i_current_fourcc, i_current_channels,
1384 i_current_samplespersec, i_current_bitspersample);
1386 if( (!i_channels || i_channels == i_current_channels) &&
1387 (!i_samplespersec ||
1388 i_samplespersec == i_current_samplespersec) &&
1389 (!i_bitspersample ||
1390 i_bitspersample == i_current_bitspersample) )
1392 /* Pick the 1st match */
1394 i_channels = i_current_channels;
1395 i_samplespersec = i_current_samplespersec;
1396 i_bitspersample = i_current_bitspersample;
1399 /* Setup a few properties like the audio latency */
1400 IAMBufferNegotiation *p_ambuf;
1402 if( SUCCEEDED( p_output_pin->QueryInterface(
1403 IID_IAMBufferNegotiation, (void **)&p_ambuf ) ) )
1405 ALLOCATOR_PROPERTIES AllocProp;
1406 AllocProp.cbAlign = -1;
1407 AllocProp.cbBuffer = i_channels * i_samplespersec *
1408 i_bitspersample / 8 / 10 ; /*100 ms of latency*/
1409 AllocProp.cbPrefix = -1;
1410 AllocProp.cBuffers = -1;
1411 p_ambuf->SuggestAllocatorProperties( &AllocProp );
1417 FreeMediaType( *p_mt );
1420 else if( p_mt->majortype == MEDIATYPE_Stream )
1422 msg_Dbg( p_this, "EnumDeviceCaps: MEDIATYPE_Stream" );
1424 int i_current_fourcc = VLC_FOURCC(' ', ' ', ' ', ' ');
1426 if( p_mt->subtype == MEDIASUBTYPE_MPEG2_PROGRAM )
1427 i_current_fourcc = VLC_FOURCC( 'm', 'p', '2', 'p' );
1428 else if( p_mt->subtype == MEDIASUBTYPE_MPEG2_TRANSPORT )
1429 i_current_fourcc = VLC_FOURCC( 'm', 'p', '2', 't' );
1431 if( ( !i_fourcc || i_fourcc == i_current_fourcc ) )
1433 /* Pick the 1st match */
1435 i_fourcc = i_current_fourcc;
1440 FreeMediaType( *p_mt );
1446 "EnumDeviceCaps: input pin: unknown format" );
1447 FreeMediaType( *p_mt );
1450 CoTaskMemFree( (PVOID)p_mt );
1452 p_enummt->Release();
1455 p_output_pin->Release();
1458 p_enumpins->Release();
1462 /*****************************************************************************
1463 * Read: reads from the device into PES packets.
1464 *****************************************************************************
1465 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1467 *****************************************************************************/
1468 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer,
1471 access_sys_t *p_sys = p_input->p_access_data;
1472 dshow_stream_t *p_stream = NULL;
1473 byte_t *p_buf_orig = p_buffer;
1474 VLCMediaSample sample;
1478 if( p_sys->i_header_pos )
1480 /* First header of the stream */
1481 memcpy( p_buffer, p_sys->p_header, p_sys->i_header_size );
1482 p_buffer += p_sys->i_header_size;
1483 p_sys->i_header_pos = 0;
1488 /* Get new sample/frame from next elementary stream.
1489 * We first loop through all the elementary streams and if all our
1490 * fifos are empty we block until we are signaled some new data has
1492 vlc_mutex_lock( &p_sys->lock );
1495 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1497 p_stream = p_sys->pp_streams[i_stream];
1498 if( p_stream->mt.majortype == MEDIATYPE_Audio &&
1499 p_stream->p_capture_filter &&
1500 p_stream->p_capture_filter->CustomGetPin()
1501 ->CustomGetSample( &sample ) == S_OK )
1506 if( i_stream == p_sys->i_streams )
1507 for( i_stream = 0; i_stream < p_sys->i_streams; i_stream++ )
1509 p_stream = p_sys->pp_streams[i_stream];
1510 if( p_stream->p_capture_filter &&
1511 p_stream->p_capture_filter->CustomGetPin()
1512 ->CustomGetSample( &sample ) == S_OK )
1517 if( i_stream == p_sys->i_streams )
1519 /* No data available. Wait until some data has arrived */
1520 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1521 vlc_mutex_unlock( &p_sys->lock );
1525 vlc_mutex_unlock( &p_sys->lock );
1530 i_data_size = sample.p_sample->GetActualDataLength();
1531 sample.p_sample->GetPointer( &p_data );
1533 REFERENCE_TIME i_pts, i_end_date;
1534 HRESULT hr = sample.p_sample->GetTime( &i_pts, &i_end_date );
1535 if( hr != VFW_S_NO_STOP_TIME && hr != S_OK ) i_pts = 0;
1539 if( p_stream->mt.majortype == MEDIATYPE_Video || !p_stream->b_pts )
1541 /* Use our data timestamp */
1542 i_pts = sample.i_timestamp;
1543 p_stream->b_pts = VLC_TRUE;
1548 msg_Dbg( p_input, "Read() stream: %i PTS: "I64Fd, i_stream, i_pts );
1551 /* Create pseudo header */
1552 SetDWBE( &p_sys->p_header[0], i_stream );
1553 SetDWBE( &p_sys->p_header[4], i_data_size );
1554 SetQWBE( &p_sys->p_header[8], i_pts * 9 / 1000 );
1557 msg_Info( p_input, "access read %i data_size %i", i_len, i_data_size );
1560 /* First copy header */
1561 memcpy( p_buffer, p_sys->p_header, 16 /* header size */ );
1562 p_buffer += 16 /* header size */;
1564 /* Then copy stream data if any */
1565 if( !p_stream->b_invert )
1567 p_input->p_vlc->pf_memcpy( p_buffer, p_data, i_data_size );
1568 p_buffer += i_data_size;
1572 int i_width = p_stream->header.video.bmiHeader.biWidth;
1573 int i_height = p_stream->header.video.bmiHeader.biHeight;
1574 if( i_height < 0 ) i_height = - i_height;
1576 switch( p_stream->i_fourcc )
1578 case VLC_FOURCC( 'R', 'V', '1', '5' ):
1579 case VLC_FOURCC( 'R', 'V', '1', '6' ):
1582 case VLC_FOURCC( 'R', 'V', '2', '4' ):
1585 case VLC_FOURCC( 'R', 'V', '3', '2' ):
1586 case VLC_FOURCC( 'R', 'G', 'B', 'A' ):
1591 for( int i = i_height - 1; i >= 0; i-- )
1593 p_input->p_vlc->pf_memcpy( p_buffer,
1594 &p_data[i * i_width], i_width );
1596 p_buffer += i_width;
1600 sample.p_sample->Release();
1602 /* The caller got what he wanted */
1603 return p_buffer - p_buf_orig;
1606 return 0; /* never reached */
1609 /*****************************************************************************
1610 * ReadCompressed: reads compressed (MPEG/DV) data from the device.
1611 *****************************************************************************
1612 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1614 *****************************************************************************/
1615 static ssize_t ReadCompressed( input_thread_t * p_input, byte_t * p_buffer,
1618 access_sys_t *p_sys = p_input->p_access_data;
1619 dshow_stream_t *p_stream = NULL;
1620 VLCMediaSample sample;
1624 /* Read 1 DV/MPEG frame (they contain the video and audio data) */
1626 /* There must be only 1 elementary stream to produce a valid stream
1627 * of MPEG or DV data */
1628 p_stream = p_sys->pp_streams[0];
1632 if( p_input->b_die || p_input->b_error ) return 0;
1634 /* Get new sample/frame from the elementary stream (blocking). */
1635 vlc_mutex_lock( &p_sys->lock );
1637 if( p_stream->p_capture_filter->CustomGetPin()
1638 ->CustomGetSample( &sample ) != S_OK )
1640 /* No data available. Wait until some data has arrived */
1641 vlc_cond_wait( &p_sys->wait, &p_sys->lock );
1642 vlc_mutex_unlock( &p_sys->lock );
1646 vlc_mutex_unlock( &p_sys->lock );
1651 i_data_size = sample.p_sample->GetActualDataLength();
1652 sample.p_sample->GetPointer( &p_data );
1655 msg_Info( p_input, "access read %i data_size %i", i_len, i_data_size );
1657 i_data_size = __MIN( i_data_size, (int)i_len );
1659 p_input->p_vlc->pf_memcpy( p_buffer, p_data, i_data_size );
1661 sample.p_sample->Release();
1663 /* The caller got what he wanted */
1667 return 0; /* never reached */
1670 /*****************************************************************************
1671 * Demux: local prototypes
1672 *****************************************************************************/
1679 static int Demux ( input_thread_t * );
1681 /****************************************************************************
1683 ****************************************************************************/
1684 static int DemuxOpen( vlc_object_t *p_this )
1686 input_thread_t *p_input = (input_thread_t *)p_this;
1693 /* a little test to see if it's a dshow stream */
1694 if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
1696 msg_Warn( p_input, "dshow plugin discarded (cannot peek)" );
1697 return VLC_EGENERIC;
1700 if( memcmp( p_peek, ".dsh", 4 ) ||
1701 ( i_es = GetDWBE( &p_peek[4] ) ) <= 0 )
1703 msg_Warn( p_input, "dshow plugin discarded (not a valid stream)" );
1704 return VLC_EGENERIC;
1707 vlc_mutex_lock( &p_input->stream.stream_lock );
1708 if( input_InitStream( p_input, 0 ) == -1)
1710 vlc_mutex_unlock( &p_input->stream.stream_lock );
1711 msg_Err( p_input, "cannot init stream" );
1712 return VLC_EGENERIC;
1714 p_input->stream.i_mux_rate = 0 / 50;
1715 vlc_mutex_unlock( &p_input->stream.stream_lock );
1717 p_input->pf_demux = Demux;
1718 p_input->pf_demux_control = demux_vaControlDefault;
1719 p_input->p_demux_data = p_sys =
1720 (demux_sys_t *)malloc( sizeof( demux_sys_t ) );
1724 if( stream_Peek( p_input->s, &p_peek, 8 + 20 * i_es ) < 8 + 20 * i_es )
1726 msg_Err( p_input, "dshow plugin discarded (cannot peek)" );
1727 return VLC_EGENERIC;
1731 for( i = 0; i < i_es; i++ )
1735 if( !memcmp( p_peek, "auds", 4 ) )
1737 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( p_peek[4], p_peek[5],
1738 p_peek[6], p_peek[7] ) );
1740 fmt.audio.i_channels = GetDWBE( &p_peek[8] );
1741 fmt.audio.i_rate = GetDWBE( &p_peek[12] );
1742 fmt.audio.i_bitspersample = GetDWBE( &p_peek[16] );
1743 fmt.audio.i_blockalign = fmt.audio.i_channels *
1744 fmt.audio.i_bitspersample / 8;
1745 fmt.i_bitrate = fmt.audio.i_channels *
1747 fmt.audio.i_bitspersample;
1749 msg_Dbg( p_input, "new audio es %d channels %dHz",
1750 fmt.audio.i_channels, fmt.audio.i_rate );
1752 p_sys->es = (es_out_id_t **)realloc( p_sys->es,
1753 sizeof(es_out_id_t *) * (p_sys->i_es + 1) );
1754 p_sys->es[p_sys->i_es++] = es_out_Add( p_input->p_es_out, &fmt );
1756 else if( !memcmp( p_peek, "vids", 4 ) )
1758 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[4], p_peek[5],
1759 p_peek[6], p_peek[7] ) );
1760 fmt.video.i_width = GetDWBE( &p_peek[8] );
1761 fmt.video.i_height = GetDWBE( &p_peek[12] );
1763 msg_Dbg( p_input, "added new video es %4.4s %dx%d",
1764 (char*)&fmt.i_codec,
1765 fmt.video.i_width, fmt.video.i_height );
1767 p_sys->es = (es_out_id_t **)realloc( p_sys->es,
1768 sizeof(es_out_id_t *) * (p_sys->i_es + 1) );
1769 p_sys->es[p_sys->i_es++] = es_out_Add( p_input->p_es_out, &fmt );
1776 stream_Read( p_input->s, NULL, 8 + 20 * i_es );
1781 /****************************************************************************
1783 ****************************************************************************/
1784 static void DemuxClose( vlc_object_t *p_this )
1786 input_thread_t *p_input = (input_thread_t *)p_this;
1787 demux_sys_t *p_sys = p_input->p_demux_data;
1789 if( p_sys->i_es > 0 )
1796 /****************************************************************************
1798 ****************************************************************************/
1799 static int Demux( input_thread_t *p_input )
1801 demux_sys_t *p_sys = p_input->p_demux_data;
1810 if( stream_Peek( p_input->s, &p_peek, 16 ) < 16 )
1812 msg_Warn( p_input, "cannot peek (EOF ?)" );
1816 i_es = GetDWBE( &p_peek[0] );
1817 if( i_es < 0 || i_es >= p_sys->i_es )
1819 msg_Err( p_input, "cannot find ES" );
1823 i_size = GetDWBE( &p_peek[4] );
1824 i_pcr = GetQWBE( &p_peek[8] );
1826 if( ( p_block = stream_Block( p_input->s, 16 + i_size ) ) == NULL )
1828 msg_Warn( p_input, "cannot read data" );
1832 p_block->p_buffer += 16;
1833 p_block->i_buffer -= 16;
1835 /* Call the pace control. */
1836 input_ClockManageRef( p_input, p_input->stream.p_selected_program, i_pcr );
1839 p_block->i_pts = i_pcr <= 0 ? 0 :
1840 input_ClockGetTS( p_input, p_input->stream.p_selected_program, i_pcr );
1842 es_out_Send( p_input->p_es_out, p_sys->es[i_es], p_block );
1848 /*****************************************************************************
1849 * config variable callback
1850 *****************************************************************************/
1851 static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1852 vlc_value_t newval, vlc_value_t oldval, void * )
1854 module_config_t *p_item;
1855 vlc_bool_t b_audio = VLC_FALSE;
1858 p_item = config_FindConfig( p_this, psz_name );
1859 if( !p_item ) return VLC_SUCCESS;
1861 if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = VLC_TRUE;
1863 /* Clear-up the current list */
1864 if( p_item->i_list )
1866 /* Keep the 2 first entries */
1867 for( i = 2; i < p_item->i_list; i++ )
1869 free( p_item->ppsz_list[i] );
1870 free( p_item->ppsz_list_text[i] );
1872 /* TODO: Remove when no more needed */
1873 p_item->ppsz_list[i] = NULL;
1874 p_item->ppsz_list_text[i] = NULL;
1878 /* Find list of devices */
1879 list<string> list_devices;
1881 /* Initialize OLE/COM */
1884 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1886 /* Uninitialize OLE/COM */
1889 if( !list_devices.size() ) return VLC_SUCCESS;
1892 (char **)realloc( p_item->ppsz_list,
1893 (list_devices.size()+3) * sizeof(char *) );
1894 p_item->ppsz_list_text =
1895 (char **)realloc( p_item->ppsz_list_text,
1896 (list_devices.size()+3) * sizeof(char *) );
1898 list<string>::iterator iter;
1899 for( iter = list_devices.begin(), i = 2; iter != list_devices.end();
1902 p_item->ppsz_list[i] = strdup( iter->c_str() );
1903 p_item->ppsz_list_text[i] = NULL;
1906 p_item->ppsz_list[i] = NULL;
1907 p_item->ppsz_list_text[i] = NULL;
1909 /* Signal change to the interface */
1910 p_item->b_dirty = VLC_TRUE;
1915 static int ConfigDevicesCallback( vlc_object_t *p_this, char const *psz_name,
1916 vlc_value_t newval, vlc_value_t oldval, void * )
1918 module_config_t *p_item;
1919 vlc_bool_t b_audio = VLC_FALSE;
1921 /* Initialize OLE/COM */
1924 p_item = config_FindConfig( p_this, psz_name );
1925 if( !p_item ) return VLC_SUCCESS;
1927 if( !strcmp( psz_name, "dshow-adev" ) ) b_audio = VLC_TRUE;
1931 if( newval.psz_string && *newval.psz_string )
1933 devicename = newval.psz_string;
1937 /* If no device name was specified, pick the 1st one */
1938 list<string> list_devices;
1940 /* Enumerate devices */
1941 FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
1942 if( !list_devices.size() ) return VLC_EGENERIC;
1943 devicename = *list_devices.begin();
1946 IBaseFilter *p_device_filter =
1947 FindCaptureDevice( p_this, &devicename, NULL, b_audio );
1948 if( p_device_filter )
1950 PropertiesPage( p_this, p_device_filter, NULL, b_audio );
1954 /* Uninitialize OLE/COM */
1957 msg_Err( p_this, "didn't find device: %s", devicename.c_str() );
1958 return VLC_EGENERIC;
1961 /* Uninitialize OLE/COM */
1967 static void ShowPropertyPage( IUnknown *obj, CAUUID *cauuid )
1969 if( cauuid->cElems > 0 )
1971 HWND hwnd_desktop = ::GetDesktopWindow();
1973 OleCreatePropertyFrame( hwnd_desktop, 30, 30, NULL, 1, &obj,
1974 cauuid->cElems, cauuid->pElems, 0, 0, NULL );
1976 CoTaskMemFree( cauuid->pElems );
1980 static void PropertiesPage( vlc_object_t *p_this, IBaseFilter *p_device_filter,
1981 ICaptureGraphBuilder2 *p_capture_graph,
1982 vlc_bool_t b_audio )
1986 msg_Dbg( p_this, "Configuring Device Properties" );
1989 * Video or audio capture filter page
1991 ISpecifyPropertyPages *p_spec;
1993 HRESULT hr = p_device_filter->QueryInterface( IID_ISpecifyPropertyPages,
1997 if( SUCCEEDED(p_spec->GetPages( &cauuid )) )
1999 ShowPropertyPage( p_device_filter, &cauuid );
2004 msg_Dbg( p_this, "looking for WDM Configuration Pages" );
2006 if( p_capture_graph )
2007 msg_Dbg( p_this, "got capture graph for WDM Configuration Pages" );
2012 if( p_capture_graph && b_audio )
2014 IAMStreamConfig *p_SC;
2016 hr = p_capture_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2017 &MEDIATYPE_Audio, p_device_filter,
2018 IID_IAMStreamConfig,
2022 hr = p_SC->QueryInterface( IID_ISpecifyPropertyPages,
2026 hr = p_spec->GetPages( &cauuid );
2029 for( unsigned int c = 0; c < cauuid.cElems; c++ )
2031 ShowPropertyPage( p_SC, &cauuid );
2033 CoTaskMemFree( cauuid.pElems );
2044 hr = p_capture_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2045 &MEDIATYPE_Audio, p_device_filter,
2046 IID_IAMTVAudio, (void **)&p_TVA );
2049 hr = p_TVA->QueryInterface( IID_ISpecifyPropertyPages,
2053 if( SUCCEEDED( p_spec->GetPages( &cauuid ) ) )
2054 ShowPropertyPage(p_TVA, &cauuid);
2065 if( p_capture_graph && !b_audio )
2067 IAMStreamConfig *p_SC;
2069 hr = p_capture_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2070 &MEDIATYPE_Interleaved,
2072 IID_IAMStreamConfig,
2076 hr = p_capture_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2079 IID_IAMStreamConfig,
2085 hr = p_SC->QueryInterface( IID_ISpecifyPropertyPages,
2089 if( SUCCEEDED( p_spec->GetPages(&cauuid) ) )
2091 ShowPropertyPage(p_SC, &cauuid);
2099 * Video crossbar, and a possible second crossbar
2101 IAMCrossbar *p_X, *p_X2;
2104 hr = p_capture_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2105 &MEDIATYPE_Interleaved,
2107 IID_IAMCrossbar, (void **)&p_X );
2110 hr = p_capture_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2119 hr = p_X->QueryInterface( IID_IBaseFilter, (void **)&p_XF );
2122 hr = p_X->QueryInterface( IID_ISpecifyPropertyPages,
2126 hr = p_spec->GetPages(&cauuid);
2127 if( hr == S_OK && cauuid.cElems > 0 )
2129 ShowPropertyPage( p_X, &cauuid );
2134 hr = p_capture_graph->FindInterface( &LOOK_UPSTREAM_ONLY, NULL,
2135 p_XF, IID_IAMCrossbar,
2139 hr = p_X2->QueryInterface( IID_ISpecifyPropertyPages,
2143 hr = p_spec->GetPages( &cauuid );
2146 ShowPropertyPage( p_X2, &cauuid );
2163 hr = p_capture_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2164 &MEDIATYPE_Interleaved,
2166 IID_IAMTVTuner, (void **)&p_TV );
2169 hr = p_capture_graph->FindInterface( &PIN_CATEGORY_CAPTURE,
2178 hr = p_TV->QueryInterface( IID_ISpecifyPropertyPages,
2182 hr = p_spec->GetPages(&cauuid);
2185 ShowPropertyPage(p_TV, &cauuid);