From e8e10c732749e532ca1fa52bc050c2cf602ff7c6 Mon Sep 17 00:00:00 2001
From: basos g <noxelia 4t gmail d0t c0m>
Date: Wed, 11 Feb 2009 12:52:36 +0200
Subject: [PATCH] FIX: DSHOW "none" device open (#2201). Some cleaning

 -Fix dshow "none" deivice be recognized as none
 Note that another patch concerning var_optionParse string
 dequoting should be applied for this to be fixed
 ( the concept is not to provide quoted strings to dshow options )
 -Account for the case when BOTH devices could not be opened
   (file an error, instead of seeming to play)
 -Cleaned a little
 -Added dbg and error msgs
 -Added coments

Rev 3
---
 modules/access/dshow/crossbar.cpp |   47 +++++++++--
 modules/access/dshow/dshow.cpp    |  179 +++++++++++++++++++++++++------------
 2 files changed, 162 insertions(+), 64 deletions(-)

diff --git a/modules/access/dshow/crossbar.cpp b/modules/access/dshow/crossbar.cpp
index 18b4878..c34b290 100644
--- a/modules/access/dshow/crossbar.cpp
+++ b/modules/access/dshow/crossbar.cpp
@@ -39,6 +39,41 @@
 
 #include "common.h"
 
+
+
+// Helper function to associate a crossbar pin name with the type.
+static const char * GetPhysicalPinName(long lType)
+{
+    switch (lType)
+    {
+    case PhysConn_Video_Tuner:            return "Video Tuner";
+    case PhysConn_Video_Composite:        return "Video Composite";
+    case PhysConn_Video_SVideo:           return "S-Video";
+    case PhysConn_Video_RGB:              return "Video RGB";
+    case PhysConn_Video_YRYBY:            return "Video YRYBY";
+    case PhysConn_Video_SerialDigital:    return "Video Serial Digital";
+    case PhysConn_Video_ParallelDigital:  return "Video Parallel Digital";
+    case PhysConn_Video_SCSI:             return "Video SCSI";
+    case PhysConn_Video_AUX:              return "Video AUX";
+    case PhysConn_Video_1394:             return "Video 1394";
+    case PhysConn_Video_USB:              return "Video USB";
+    case PhysConn_Video_VideoDecoder:     return "Video Decoder";
+    case PhysConn_Video_VideoEncoder:     return "Video Encoder";
+
+    case PhysConn_Audio_Tuner:            return "Audio Tuner";
+    case PhysConn_Audio_Line:             return "Audio Line";
+    case PhysConn_Audio_Mic:              return "Audio Microphone";
+    case PhysConn_Audio_AESDigital:       return "Audio AES/EBU Digital";
+    case PhysConn_Audio_SPDIFDigital:     return "Audio S/PDIF";
+    case PhysConn_Audio_SCSI:             return "Audio SCSI";
+    case PhysConn_Audio_AUX:              return "Audio AUX";
+    case PhysConn_Audio_1394:             return "Audio 1394";
+    case PhysConn_Audio_USB:              return "Audio USB";
+    case PhysConn_Audio_AudioDecoder:     return "Audio Decoder";
+
+    default:                              return "Unknown Type";
+    }
+}
 /*****************************************************************************
  * DeleteCrossbarRoutes
  *****************************************************************************/
@@ -208,9 +243,9 @@ HRESULT FindCrossbarRoutes( vlc_object_t *p_this, access_sys_t *p_sys,
                 // remember connector type
                 physicalType = inputPinPhysicalType;
  
-                msg_Dbg( p_this, "found existing route for output %ld (type %ld) to input %ld (type %ld)",
-                         outputPinIndex, outputPinPhysicalType, inputPinIndex,
-                         inputPinPhysicalType );
+                msg_Dbg( p_this, "found existing route for output %ld (type %s) to input %ld (type %s)",
+                         outputPinIndex, GetPhysicalPinName( outputPinPhysicalType ),
+                         inputPinIndex, GetPhysicalPinName( inputPinPhysicalType ) );
  
                 // fall through to for loop, note 'inputPinIndex' is set to the pin we are looking for
                 // hence, loop iteration should not wind back
@@ -260,9 +295,9 @@ HRESULT FindCrossbarRoutes( vlc_object_t *p_this, access_sys_t *p_sys,
             p_sys->crossbar_routes[depth].AudioOutputIndex = outputPinIndexRelated;
 
             msg_Dbg( p_this, "crossbar at depth %d, found route for "
-                     "output %ld (type %ld) to input %ld (type %ld)", depth,
-                     outputPinIndex, outputPinPhysicalType, inputPinIndex,
-                     inputPinPhysicalType );
+                     "output %ld (type %s) to input %ld (type %s)", depth,
+                     outputPinIndex, GetPhysicalPinName( outputPinPhysicalType ),
+                     inputPinIndex, GetPhysicalPinName( inputPinPhysicalType ) );
 
             result = S_OK;
         }
diff --git a/modules/access/dshow/dshow.cpp b/modules/access/dshow/dshow.cpp
index c81bb07..afefc23 100644
--- a/modules/access/dshow/dshow.cpp
+++ b/modules/access/dshow/dshow.cpp
@@ -202,23 +202,19 @@ vlc_module_begin ()
 
     add_string( "dshow-size", NULL, NULL, SIZE_TEXT, SIZE_LONGTEXT, false)
 
-    add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
-                true )
+    add_string( "dshow-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT, true )
 
-    add_float( "dshow-fps", 0.0f, NULL, FPS_TEXT, FPS_LONGTEXT,
-                true )
+    add_float( "dshow-fps", 0.0f, NULL, FPS_TEXT, FPS_LONGTEXT, true )
 
-    add_bool( "dshow-config", false, NULL, CONFIG_TEXT, CONFIG_LONGTEXT,
-              true )
+    add_bool( "dshow-config", false, NULL, CONFIG_TEXT, CONFIG_LONGTEXT, true )
 
-    add_bool( "dshow-tuner", false, NULL, TUNER_TEXT, TUNER_LONGTEXT,
-              true )
+    add_bool( "dshow-tuner", false, NULL, TUNER_TEXT, TUNER_LONGTEXT, true )
 
-    add_integer( "dshow-tuner-channel", 0, NULL, CHANNEL_TEXT,
-                 CHANNEL_LONGTEXT, true )
+    add_integer( "dshow-tuner-channel", 0, NULL, CHANNEL_TEXT, CHANNEL_LONGTEXT,
+                true )
 
-    add_integer( "dshow-tuner-country", 0, NULL, COUNTRY_TEXT,
-                 COUNTRY_LONGTEXT, true )
+    add_integer( "dshow-tuner-country", 0, NULL, COUNTRY_TEXT, COUNTRY_LONGTEXT,
+                true )
 
     add_integer( "dshow-tuner-input", 0, NULL, TUNER_INPUT_TEXT,
                  TUNER_INPUT_LONGTEXT, true )
@@ -227,12 +223,12 @@ vlc_module_begin ()
     add_integer( "dshow-video-input",  -1, NULL, VIDEO_IN_TEXT,
                  VIDEO_IN_LONGTEXT, true )
 
-    add_integer( "dshow-audio-input",  -1, NULL, AUDIO_IN_TEXT,
-                 AUDIO_IN_LONGTEXT, true )
-
     add_integer( "dshow-video-output", -1, NULL, VIDEO_OUT_TEXT,
                  VIDEO_OUT_LONGTEXT, true )
 
+    add_integer( "dshow-audio-input",  -1, NULL, AUDIO_IN_TEXT,
+                 AUDIO_IN_LONGTEXT, true )
+
     add_integer( "dshow-audio-output", -1, NULL, AUDIO_OUT_TEXT,
                  AUDIO_OUT_LONGTEXT, true )
 
@@ -352,21 +348,41 @@ static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
     /* Get/parse options and open device(s) */
     string vdevname, adevname;
     int i_width = 0, i_height = 0, i_chroma = 0;
-    bool b_audio = true;
+    bool b_use_audio = true;
+    bool b_use_video = true;
+
+    /* Initialize OLE/COM */
+    CoInitialize( 0 );
 
     var_Create( p_this, "dshow-config", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
     var_Create( p_this, "dshow-tuner", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
     var_Create( p_this, "dshow-vdev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
     var_Get( p_this, "dshow-vdev", &val );
-    if( val.psz_string ) vdevname = string( val.psz_string );
+    if( val.psz_string )
+    {
+        msg_Dbg( p_this, "dshow-vdev: %s", val.psz_string ) ;
+        /* skip none device */
+        if ( strncasecmp( val.psz_string, "none", 4 ) != 0 )
+            vdevname = string( val.psz_string );
+        else
+            b_use_video = false ;
+    }
     free( val.psz_string );
 
     var_Create( p_this, "dshow-adev", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
     var_Get( p_this, "dshow-adev", &val );
-    if( val.psz_string ) adevname = string( val.psz_string );
+    if( val.psz_string )
+    {
+        msg_Dbg( p_this, "dshow-adev: %s", val.psz_string ) ;
+        /* skip none device */
+        if ( strncasecmp( val.psz_string, "none", 4 ) != 0 )
+            adevname = string( val.psz_string );
+        else
+            b_use_audio = false ;
+    }
     free( val.psz_string );
 
-    static struct {char *psz_size; int  i_width; int  i_height;} size_table[] =
+    static struct {const char *psz_size; int  i_width; int  i_height;} size_table[] =
     { { "subqcif", 128, 96 }, { "qsif", 160, 120 }, { "qcif", 176, 144 },
       { "sif", 320, 240 }, { "cif", 352, 288 }, { "d1", 640, 480 },
       { 0, 0, 0 },
@@ -427,9 +443,6 @@ static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
     var_Create( p_this, "dshow-video-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
     var_Create( p_this, "dshow-audio-output", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
 
-    /* Initialize OLE/COM */
-    CoInitialize( 0 );
-
     /* Initialize some data */
     p_sys->i_streams = 0;
     p_sys->pp_streams = NULL;
@@ -441,17 +454,31 @@ static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
     p_sys->p_capture_graph_builder2 = NULL;
     p_sys->p_control = NULL;
 
+    /* Build directshow graph */
+    CreateDirectShowGraph( p_sys );
+
     vlc_mutex_init( &p_sys->lock );
     vlc_cond_init( &p_sys->wait );
 
-    /* Build directshow graph */
-    CreateDirectShowGraph( p_sys );
+    if( !b_use_video && !b_use_audio )
+    {
+        msg_Err( p_this, "No video or audio device selected" );
+        intf_UserFatal( p_this, false, _("Capturing failed"),
+                        _("No video or audio device delected.") );
+        return VLC_EGENERIC ;
+    }
 
-    if( OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
+    if( !b_use_video )
+        msg_Dbg( p_this, "skipping video device" ) ;
+    bool b_err_video = false ;
+
+    if( b_use_video && OpenDevice( p_this, p_sys, vdevname, 0 ) != VLC_SUCCESS )
     {
-        msg_Err( p_this, "can't open video");
+        msg_Err( p_this, "can't open video device");
+        b_err_video = true ;
     }
-    else
+
+    if ( b_use_video && !b_err_video )
     {
         /* Check if we can handle the demuxing ourselves or need to spawn
          * a demuxer module */
@@ -466,7 +493,7 @@ static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
                 /* Raw MPEG video stream */
                 p_stream->i_fourcc == VLC_FOURCC('m','p','2','v') )
             {
-                b_audio = false;
+                b_use_audio = false;
 
                 if( b_access_demux )
                 {
@@ -478,7 +505,7 @@ static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
 
         if( p_stream->mt.majortype == MEDIATYPE_Stream )
         {
-            b_audio = false;
+            b_use_audio = false;
 
             if( b_access_demux )
             {
@@ -497,9 +524,26 @@ static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
         }
     }
 
-    if( b_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
+    if( !b_use_audio )
+        msg_Dbg( p_this, "skipping audio device") ;
+
+    bool b_err_audio = false ;
+
+    if( b_use_audio && OpenDevice( p_this, p_sys, adevname, 1 ) != VLC_SUCCESS )
     {
-        msg_Err( p_this, "can't open audio");
+        msg_Err( p_this, "can't open audio device");
+        b_err_audio = true ;
+    }
+
+    if( ( b_use_video && b_err_video && b_use_audio && b_err_audio ) ||
+        ( !b_use_video && b_use_audio && b_err_audio ) ||
+        ( b_use_video && !b_use_audio && b_err_video ) )
+    {
+        msg_Err( p_this, "FATAL: could not open ANY device" ) ;
+        intf_UserFatal( p_this, false, _("Capturing failed"),
+                        _("VLC cannot open ANY capture device."
+                          "Check the error log for details.") );
+        return VLC_EGENERIC ;
     }
 
     for( i = p_sys->i_crossbar_route_depth-1; i >= 0 ; --i )
@@ -540,6 +584,9 @@ static int CommonOpen( vlc_object_t *p_this, access_sys_t *p_sys,
                 }
             }
         }
+        else
+            msg_Err( p_this, "crossbar at depth %d could not route video "
+                     "output %ld to input %ld", i, VideoOutputIndex, VideoInputIndex );
     }
 
     /*
@@ -823,6 +870,9 @@ static bool ConnectFilters( vlc_object_t *p_this, access_sys_t *p_sys,
                 pP->Release();
             }
             pins->Release();
+            msg_Dbg( p_this, "ConnectFilters: graph_builder2 available.") ;
+            if ( !Found )
+                msg_Warn( p_this, "ConnectFilters: No crossBar routes found (incobatible pin types)" ) ;
         }
         return true;
     }
@@ -898,7 +948,6 @@ static int OpenDevice( vlc_object_t *p_this, access_sys_t *p_sys,
 
     /* Enumerate devices and display their names */
     FindCaptureDevice( p_this, NULL, &list_devices, b_audio );
-
     if( !list_devices.size() )
         return VLC_EGENERIC;
 
@@ -909,13 +958,17 @@ static int OpenDevice( vlc_object_t *p_this, access_sys_t *p_sys,
     /* If no device name was specified, pick the 1st one */
     if( devicename.size() == 0 )
     {
+        /* When none selected */
         devicename = *list_devices.begin();
+        msg_Dbg( p_this, "asking for default device: %s", devicename.c_str() ) ;
     }
-
+    else
+        msg_Dbg( p_this, "asking for device: %s", devicename.c_str() ) ;
     // Use the system device enumerator and class enumerator to find
     // a capture/preview device, such as a desktop USB video camera.
     IBaseFilter *p_device_filter =
-        FindCaptureDevice( p_this, &devicename, 0, b_audio );
+        FindCaptureDevice( p_this, &devicename, NULL, b_audio );
+
     if( p_device_filter )
         msg_Dbg( p_this, "using device: %s", devicename.c_str() );
     else
@@ -1084,6 +1137,11 @@ static int OpenDevice( vlc_object_t *p_this, access_sys_t *p_sys,
     return VLC_EGENERIC;
 }
 
+/* FindCaptureDevices:: This Function had two purposes :
+    Returns the list of capture devices when p_listdevices != NULL
+    Creates an IBaseFilter when p_devicename corresponds to an existing devname
+   These actions *may* be requested whith a single call.
+*/
 static IBaseFilter *
 FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
                    list<string> *p_listdevices, bool b_audio )
@@ -1124,7 +1182,7 @@ FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
      * CreateClassEnumerator will succeed, but p_class_enum will be NULL */
     if( p_class_enum == NULL )
     {
-        msg_Err( p_this, "no capture device was detected" );
+        msg_Err( p_this, "no %s capture device was detected", ( b_audio ? "audio" : "video" ) );
         return NULL;
     }
 
@@ -1156,32 +1214,35 @@ FindCaptureDevice( vlc_object_t *p_this, string *p_devicename,
                 SysFreeString(var.bstrVal);
                 p_buf[i_convert] = '\0';
 
-        string devname = string(p_buf);
+                string devname = string(p_buf);
 
-        int dup = 0;
-        /* find out if this name is already used by a previously found device */
-        list<string>::const_iterator iter = devicelist.begin();
-        list<string>::const_iterator end = devicelist.end();
-        while ( iter != end )
-        {
-            if( 0 == (*iter).compare(0, devname.size(), devname) )
-            ++dup;
-            ++iter;
-        }
-        if( dup )
-        {
-            /* we have a duplicate device name, append a sequence number to name
-               to provive a unique list back to the user */
-            char seq[16];
-            sprintf(seq, " #%d", dup);
-            devname.append(seq);
-        }
-        devicelist.push_back( devname );
+                free( p_buf) ;
+
+                int dup = 0;
+                /* find out if this name is already used by a previously found device */
+                list<string>::const_iterator iter = devicelist.begin();
+                list<string>::const_iterator end = devicelist.end();
+                string ordevname = devname ;
+                while ( iter != end )
+                {
+                    if( 0 == (*iter).compare( devname ) )
+                    { /* devname is on the list. Try another name with sequence
+                         number apended and then rescan until a unique entry is found*/
+                         char seq[16];
+                         snprintf(seq, 16, " #%d", ++dup);
+                         devname = ordevname + seq;
+                         iter = devicelist.begin();
+                    }
+                    else
+                         ++iter;
+                }
+                devicelist.push_back( devname );
 
                 if( p_devicename && *p_devicename == devname )
                 {
-                    /* Bind Moniker to a filter object */
-                    hr = p_moniker->BindToObject( 0, 0, IID_IBaseFilter,
+                    msg_Dbg( p_this, "asked for %s, binding to %s", p_devicename->c_str() , devname.c_str() ) ;
+                    /* NULL possibly means we don't need BindMoniker BindCtx ?? */
+                    hr = p_moniker->BindToObject( NULL, 0, IID_IBaseFilter,
                                                   (void **)&p_base_filter );
                     if( FAILED(hr) )
                     {
@@ -1571,7 +1632,7 @@ static size_t EnumDeviceCaps( vlc_object_t *p_this, IBaseFilter *p_filter,
             }
             else
             {
-                char *psz_type = "unknown";
+                const char * psz_type = "unknown";
                 if( p_mt->majortype == MEDIATYPE_Video ) psz_type = "video";
                 if( p_mt->majortype == MEDIATYPE_Audio ) psz_type = "audio";
                 if( p_mt->majortype == MEDIATYPE_Stream ) psz_type = "stream";
@@ -2005,11 +2066,13 @@ static int ConfigDevicesCallback( vlc_object_t *p_this, char const *psz_name,
 /*****************************************************************************
  * Properties
  *****************************************************************************/
+
 static void ShowPropertyPage( IUnknown *obj )
 {
     ISpecifyPropertyPages *p_spec;
     CAUUID cauuid;
 
+    //get Ipersist interface if it is available
     HRESULT hr = obj->QueryInterface( IID_ISpecifyPropertyPages,
                                       (void **)&p_spec );
     if( FAILED(hr) ) return;
-- 
1.5.6.5

