Make open-by-longname work.
authorbruceperens <bruceperens@01035d8c-6547-0410-b346-abe4f91aad63>
Wed, 19 Mar 2014 23:02:50 +0000 (23:02 +0000)
committerbruceperens <bruceperens@01035d8c-6547-0410-b346-abe4f91aad63>
Wed, 19 Mar 2014 23:02:50 +0000 (23:02 +0000)
git-svn-id: https://svn.code.sf.net/p/freetel/code@1456 01035d8c-6547-0410-b346-abe4f91aad63

freedv-server/source/platform/linux/alsa.cpp
freedv-server/source/platform/linux/audio_in_alsa.cpp
freedv-server/source/platform/linux/audio_out_alsa.cpp

index 851845669265fb13ac222c9e07a751c98c6879cb..dc91065412602b7f7fb9f48db84c020e1c5719f1 100644 (file)
@@ -81,6 +81,34 @@ namespace FreeDV {
     return stream;
   }
 
+  static int
+  open_by_longname(
+   snd_pcm_t * *       handle,
+   const char *                name,
+   snd_pcm_stream_t    stream)
+  {
+    const int  length = strlen(name);
+    int                card_index = -1;
+
+    // Let the normal open handle "default".
+    if ( strcmp(name, "default") == 0 )
+      return -ENODEV;
+
+    while ( snd_card_next(&card_index) == 0 && card_index >= 0 ) {
+      char             device_name[20];
+      char *           longname = 0;
+  
+      if ( snd_card_get_longname(card_index, &longname) == 0 ) {
+        if ( strncmp(name, longname, length) == 0 ) {
+          sprintf(device_name, "hw:%d", card_index);
+          return snd_pcm_open(handle, device_name, stream, 0);
+        }
+      }
+    }
+  
+    return -ENODEV;
+  }
+
   static void
   do_throw(
    const int error,
@@ -120,14 +148,17 @@ namespace FreeDV {
     snd_pcm_t *                handle = 0;
     snd_pcm_hw_params_t *      hw_params = 0;
  
-    error = snd_pcm_open(
-     &handle,
-     name,
-     stream,
-     mode);
-    if ( error < 0 )
-      return 0;
+    error = open_by_longname(&handle, name, stream);
+    if ( error < 0 ) {
+      error = snd_pcm_open(
+       &handle,
+       name,
+       stream,
+       0);
+
+       if ( error < 0 )
+         return 0;
+    }
  
     if ( (error = snd_pcm_hw_params_malloc(&hw_params)) < 0 ) {
       snd_pcm_close(handle);
index cb8f5c7ed8c7d8232abce4b14fae47f4b057e9fe..ab43998b1ae8337c490bcd4b50b448510e4e8d6f 100644 (file)
@@ -25,7 +25,7 @@ namespace FreeDV {
     {
       std::ostringstream str;
 
-      str << "Error on ALSA audio input " << parameters << ": ";
+      str << "Error on ALSA audio input \"" << parameters << "\": ";
        if ( message )
          str << message << ": ";
        str << snd_strerror(error) << '.';
@@ -69,6 +69,9 @@ namespace FreeDV {
      AudioFrameSamples / 2,
      AudioFrameSamples);
 
+    if ( handle == 0 )
+      do_throw(-ENODEV);
+
     snd_pcm_start(handle);
   }
 
index deeb5145e5b48e0651d744574a93240180fe50bc..d9e30c357482c03f4d187063a9094f6ba372cfe5 100644 (file)
@@ -24,7 +24,7 @@ namespace FreeDV {
     {
       std::ostringstream str;
 
-      str << "Error on ALSA audio output " << parameters << ": ";
+      str << "Error on ALSA audio output \"" << parameters << "\": ";
        if ( message )
          str << message << ": ";
        str << snd_strerror(error) << '.';
@@ -68,6 +68,9 @@ namespace FreeDV {
      SampleRate,
      AudioFrameSamples / 2,
      AudioFrameSamples);
+
+    if ( handle == 0 )
+      do_throw(-ENODEV);
   }
 
   AudioOutALSA::~AudioOutALSA()