]> git.sesse.net Git - mlt/commitdiff
improve selecting rtaudio device by name
authorDan Dennedy <dan@dennedy.org>
Tue, 6 Dec 2011 04:43:54 +0000 (20:43 -0800)
committerDan Dennedy <dan@dennedy.org>
Tue, 6 Dec 2011 04:57:12 +0000 (20:57 -0800)
src/modules/rtaudio/RtAudio.cpp
src/modules/rtaudio/RtAudio.h
src/modules/rtaudio/consumer_rtaudio.cpp

index f01ed4e1ea7c21eb8bebf3f2b7eff552141885eb..f6b4685243372d380fdd2a632078efb23db96cbc 100644 (file)
@@ -265,14 +265,14 @@ void RtApi :: openStream( RtAudio::StreamParameters *oParams,
   if ( oChannels > 0 ) {\r
 \r
     result = probeDeviceOpen( oParams->deviceId, OUTPUT, oChannels, oParams->firstChannel,\r
-                              sampleRate, format, bufferFrames, options );\r
+                              sampleRate, format, bufferFrames, options, oParams->deviceName );\r
     if ( result == false ) error( RtError::SYSTEM_ERROR );\r
   }\r
 \r
   if ( iChannels > 0 ) {\r
 \r
     result = probeDeviceOpen( iParams->deviceId, INPUT, iChannels, iParams->firstChannel,\r
-                              sampleRate, format, bufferFrames, options );\r
+                              sampleRate, format, bufferFrames, options, iParams->deviceName );\r
     if ( result == false ) {\r
       if ( oChannels > 0 ) closeStream();\r
       error( RtError::SYSTEM_ERROR );\r
@@ -307,7 +307,7 @@ void RtApi :: closeStream( void )
 bool RtApi :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels,\r
                                unsigned int firstChannel, unsigned int sampleRate,\r
                                RtAudioFormat format, unsigned int *bufferSize,\r
-                               RtAudio::StreamOptions *options )\r
+                               RtAudio::StreamOptions *options, const std::string &deviceName )\r
 {\r
   // MUST be implemented in subclasses!\r
   return FAILURE;\r
@@ -772,7 +772,7 @@ OSStatus rateListener( AudioObjectID inDevice,
 bool RtApiCore :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels,\r
                                    unsigned int firstChannel, unsigned int sampleRate,\r
                                    RtAudioFormat format, unsigned int *bufferSize,\r
-                                   RtAudio::StreamOptions *options )\r
+                                   RtAudio::StreamOptions *options, const std::string &deviceName )\r
 {\r
   // Get device ID\r
   unsigned int nDevices = getDeviceCount();\r
@@ -1986,7 +1986,7 @@ int jackXrun( void *infoPointer )
 bool RtApiJack :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels,\r
                                    unsigned int firstChannel, unsigned int sampleRate,\r
                                    RtAudioFormat format, unsigned int *bufferSize,\r
-                                   RtAudio::StreamOptions *options )\r
+                                   RtAudio::StreamOptions *options, const std::string &aDeviceName )\r
 {\r
   JackHandle *handle = (JackHandle *) stream_.apiHandle;\r
 \r
@@ -2010,31 +2010,33 @@ bool RtApiJack :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigne
     client = handle->client;\r
   }\r
 \r
+  std::string deviceName = aDeviceName;\r
   const char **ports;\r
-  std::string port, previousPort, deviceName;\r
-  unsigned int nPorts = 0, nDevices = 0;\r
-  ports = jack_get_ports( client, NULL, NULL, 0 );\r
-  if ( ports ) {\r
-    // Parse the port names up to the first colon (:).\r
-    size_t iColon = 0;\r
-    do {\r
-      port = (char *) ports[ nPorts ];\r
-      iColon = port.find(":");\r
-      if ( iColon != std::string::npos ) {\r
-        port = port.substr( 0, iColon );\r
-        if ( port != previousPort ) {\r
-          if ( nDevices == device ) deviceName = port;\r
-          nDevices++;\r
-          previousPort = port;\r
-        }\r
+  if ( deviceName.size() == 0 ) {\r
+      std::string port, previousPort, deviceName;\r
+      unsigned int nPorts = 0, nDevices = 0;\r
+      ports = jack_get_ports( client, NULL, NULL, 0 );\r
+      if ( ports ) {\r
+        // Parse the port names up to the first colon (:).\r
+        size_t iColon = 0;\r
+        do {\r
+          port = (char *) ports[ nPorts ];\r
+          iColon = port.find(":");\r
+          if ( iColon != std::string::npos ) {\r
+            port = port.substr( 0, iColon );\r
+            if ( port != previousPort ) {\r
+              if ( nDevices == device ) deviceName = port;\r
+              nDevices++;\r
+              previousPort = port;\r
+            }\r
+          }\r
+        } while ( ports[++nPorts] );\r
+        free( ports );\r
+      }\r
+      if ( device >= nDevices ) {\r
+        errorText_ = "RtApiJack::probeDeviceOpen: device ID is invalid!";\r
+        return FAILURE;\r
       }\r
-    } while ( ports[++nPorts] );\r
-    free( ports );\r
-  }\r
-\r
-  if ( device >= nDevices ) {\r
-    errorText_ = "RtApiJack::probeDeviceOpen: device ID is invalid!";\r
-    return FAILURE;\r
   }\r
 \r
   // Count the available ports containing the client name as device\r
@@ -2730,7 +2732,7 @@ void RtApiAsio :: saveDeviceInfo( void )
 bool RtApiAsio :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels,\r
                                    unsigned int firstChannel, unsigned int sampleRate,\r
                                    RtAudioFormat format, unsigned int *bufferSize,\r
-                                   RtAudio::StreamOptions *options )\r
+                                   RtAudio::StreamOptions *options, const std::string &deviceName )\r
 {\r
   // For ASIO, a duplex stream MUST use the same driver.\r
   if ( mode == INPUT && stream_.mode == OUTPUT && stream_.device[0] != device ) {\r
@@ -3832,7 +3834,7 @@ RtAudio::DeviceInfo RtApiDs :: getDeviceInfo( unsigned int device )
 bool RtApiDs :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels,\r
                                  unsigned int firstChannel, unsigned int sampleRate,\r
                                  RtAudioFormat format, unsigned int *bufferSize,\r
-                                 RtAudio::StreamOptions *options )\r
+                                 RtAudio::StreamOptions *options, const std::string &deviceName )\r
 {\r
   if ( channels + firstChannel > 2 ) {\r
     errorText_ = "RtApiDs::probeDeviceOpen: DirectSound does not support more than 2 channels per device.";\r
@@ -5468,7 +5470,7 @@ void RtApiAlsa :: saveDeviceInfo( void )
 bool RtApiAlsa :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels,\r
                                    unsigned int firstChannel, unsigned int sampleRate,\r
                                    RtAudioFormat format, unsigned int *bufferSize,\r
-                                   RtAudio::StreamOptions *options )\r
+                                   RtAudio::StreamOptions *options, const std::string &deviceName )\r
 \r
 {\r
 #if defined(__RTAUDIO_DEBUG__)\r
@@ -5485,7 +5487,8 @@ bool RtApiAlsa :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigne
 \r
   if ( options && options->flags & RTAUDIO_ALSA_USE_DEFAULT )\r
     snprintf(name, sizeof(name), "%s", "default");\r
-//    snprintf(name, sizeof(name), "%s", "dmix:0,0");\r
+  else if ( deviceName.size() > 0 )\r
+    snprintf(name, sizeof(name), "%s", deviceName.c_str());\r
   else {\r
     // Count cards and devices\r
     card = -1;\r
@@ -6530,7 +6533,7 @@ RtAudio::DeviceInfo RtApiOss :: getDeviceInfo( unsigned int device )
 bool RtApiOss :: probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels,\r
                                   unsigned int firstChannel, unsigned int sampleRate,\r
                                   RtAudioFormat format, unsigned int *bufferSize,\r
-                                  RtAudio::StreamOptions *options )\r
+                                  RtAudio::StreamOptions *options, const std::string &deviceName )\r
 {\r
   int mixerfd = open( "/dev/mixer", O_RDWR, 0 );\r
   if ( mixerfd == -1 ) {\r
index 368fda3268bd6ec4e4b2919fa43222e7f0e11b81..2c254ef8e753910972491ca2be2dcc72975baac0 100644 (file)
@@ -241,6 +241,7 @@ class RtAudio
     unsigned int deviceId;     /*!< Device index (0 to getDeviceCount() - 1). */
     unsigned int nChannels;    /*!< Number of channels. */
     unsigned int firstChannel; /*!< First channel index on device (default = 0). */
+    std::string  deviceName;   /*!< Device name to use instead of using deviceId (ALSA, JACK). */
 
     // Default constructor.
     StreamParameters()
@@ -673,7 +674,7 @@ protected:
   virtual bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, 
                                 unsigned int firstChannel, unsigned int sampleRate,
                                 RtAudioFormat format, unsigned int *bufferSize,
-                                RtAudio::StreamOptions *options );
+                                RtAudio::StreamOptions *options, const std::string &deviceName );
 
   //! A protected function used to increment the stream time.
   void tickStreamTime( void );
@@ -764,7 +765,7 @@ public:
   bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, 
                         unsigned int firstChannel, unsigned int sampleRate,
                         RtAudioFormat format, unsigned int *bufferSize,
-                        RtAudio::StreamOptions *options );
+                        RtAudio::StreamOptions *options, const std::string &deviceName );
   static const char* getErrorCode( OSStatus code );
 };
 
@@ -798,7 +799,7 @@ public:
   bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, 
                         unsigned int firstChannel, unsigned int sampleRate,
                         RtAudioFormat format, unsigned int *bufferSize,
-                        RtAudio::StreamOptions *options );
+                        RtAudio::StreamOptions *options, const std::string &deviceName );
 };
 
 #endif
@@ -834,7 +835,7 @@ public:
   bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, 
                         unsigned int firstChannel, unsigned int sampleRate,
                         RtAudioFormat format, unsigned int *bufferSize,
-                        RtAudio::StreamOptions *options );
+                        RtAudio::StreamOptions *options, const std::string &deviceName );
 };
 
 #endif
@@ -872,7 +873,7 @@ public:
   bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, 
                         unsigned int firstChannel, unsigned int sampleRate,
                         RtAudioFormat format, unsigned int *bufferSize,
-                        RtAudio::StreamOptions *options );
+                        RtAudio::StreamOptions *options, const std::string &deviceName );
 };
 
 #endif
@@ -906,7 +907,7 @@ public:
   bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, 
                         unsigned int firstChannel, unsigned int sampleRate,
                         RtAudioFormat format, unsigned int *bufferSize,
-                        RtAudio::StreamOptions *options );
+                        RtAudio::StreamOptions *options, const std::string &deviceName );
 };
 
 #endif
@@ -938,7 +939,7 @@ public:
   bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, 
                         unsigned int firstChannel, unsigned int sampleRate,
                         RtAudioFormat format, unsigned int *bufferSize,
-                        RtAudio::StreamOptions *options );
+                        RtAudio::StreamOptions *options, const std::string &deviceName );
 };
 
 #endif
@@ -963,7 +964,8 @@ public:
   bool probeDeviceOpen( unsigned int device, StreamMode mode, unsigned int channels, 
                         unsigned int firstChannel, unsigned int sampleRate,
                         RtAudioFormat format, unsigned int *bufferSize,
-                        RtAudio::StreamOptions *options ) { return false; };
+                        RtAudio::StreamOptions *options, const std::string &deviceName )
+    { return false; };
 };
 
 #endif
index de786450d98d1a1ed75d66c2080c6ca3700dd194..f425fed0fce9638678850a9abc50976611d75c54 100644 (file)
@@ -88,13 +88,10 @@ public:
                        return false;
                }
 
-               if ( !arg || !strcmp( arg, "" ) )
-               {
 #ifndef __LINUX_ALSA__
-                       device_id = rt.getDefaultOutputDevice();
+               device_id = rt.getDefaultOutputDevice();
 #endif
-               }
-               else if ( strcmp( arg, "default" ) )
+               if ( arg && strcmp( arg, "" ) && strcmp( arg, "default" ) )
                {
                        // Get device ID by name
                        unsigned int n = rt.getDeviceCount();
@@ -112,7 +109,7 @@ public:
                                        break;
                                }
                        }
-                       // Name selection failed, treat arg as numeric
+                       // Name selection failed, try arg as numeric
                        if ( i == n )
                                device_id = (int) strtol( arg, NULL, 0 );
                }
@@ -142,6 +139,9 @@ public:
                // Default audio buffer
                mlt_properties_set_int( properties, "audio_buffer", 1024 );
 
+               // Set the resource to the device name arg
+               mlt_properties_set( properties, "resource", arg );
+
                // Ensure we don't join on a non-running object
                joined = 1;
 
@@ -409,14 +409,15 @@ public:
                        parameters.nChannels = channels;
                        parameters.firstChannel = 0;
                        RtAudio::StreamOptions options;
-#ifdef __LINUX_ALSA__
+                       unsigned int bufferFrames = mlt_properties_get_int( properties, "audio_buffer" );
+
                        if ( device_id == -1 )
                        {
                                options.flags = RTAUDIO_ALSA_USE_DEFAULT;
                                parameters.deviceId = 0;
                        }
-#endif
-                       unsigned int bufferFrames = mlt_properties_get_int( properties, "audio_buffer" );
+                       if ( mlt_properties_get( properties, "resource" ) )
+                               parameters.deviceName = mlt_properties_get( properties, "resource" );
 
                        try {
                                rt.openStream( &parameters, NULL, RTAUDIO_SINT16,