Index: gstalsa.c
===================================================================
RCS file: /cvs/gstreamer/gst-plugins/ext/alsa/Attic/gstalsa.c,v
retrieving revision 1.136.2.7
diff -u -p -r1.136.2.7 gstalsa.c
--- gstalsa.c	19 Jun 2005 16:10:49 -0000	1.136.2.7
+++ gstalsa.c	11 Oct 2005 13:38:42 -0000
@@ -987,10 +987,14 @@ gst_alsa_get_caps (GstPad * pad)
 
   this = GST_ALSA (gst_pad_get_parent (pad));
 
-  if (!GST_FLAG_IS_SET (this, GST_ALSA_OPEN))
+  if (!GST_FLAG_IS_SET (this, GST_ALSA_OPEN)) {
+    GST_DEBUG ("device not open, returning template caps");
     return gst_caps_copy (GST_PAD_TEMPLATE_CAPS (GST_PAD_PAD_TEMPLATE (pad)));
-  if (this->cached_caps)
+  }
+  if (this->cached_caps) {
+    GST_DEBUG ("returning cached caps");
     return gst_caps_copy (this->cached_caps);
+  }
 
   snd_pcm_hw_params_alloca (&hw_params);
   ERROR_CHECK (snd_pcm_hw_params_any (this->handle, hw_params),
@@ -1084,10 +1088,13 @@ gst_alsa_get_caps (GstPad * pad)
 
   snd_pcm_format_mask_alloca (&mask);
   snd_pcm_hw_params_get_format_mask (hw_params, mask);
+
   for (i = 0; i <= SND_PCM_FORMAT_LAST; i++) {
     if (snd_pcm_format_mask_test (mask, i)) {
       GstCaps *caps = gst_alsa_get_caps_internal (i);
 
+      GST_DEBUG ("supported format: %02d %s", i, snd_pcm_format_name (i));
+
       /* we can never use a format we can't set caps for */
       if (caps != NULL) {
         gint n;
@@ -1155,7 +1162,7 @@ gst_alsa_get_caps (GstPad * pad)
     ret = gst_caps_new_empty ();
   }
   gst_caps_do_simplify (ret);
-  GST_LOG_OBJECT (this, "get_caps returns %P", ret);
+  GST_LOG_OBJECT (this, "get_caps returns %" GST_PTR_FORMAT, ret);
 
   g_value_unset (&supported_rates);
 
@@ -1602,10 +1609,83 @@ gst_alsa_set_eos (GstAlsa * this)
 }
 
 static gboolean
+gst_alsa_open_audio_device (GstAlsa * this, const gchar * device,
+    gboolean ignore_error)
+{
+  snd_pcm_stream_t stream_type;
+  snd_pcm_t *handle;
+  int ret;
+
+  stream_type = GST_ALSA_GET_CLASS (this)->stream;
+  ret = snd_pcm_open (&handle, device, stream_type, SND_PCM_NONBLOCK);
+  if (ret == 0) {
+    GST_DEBUG_OBJECT (this, "opened device '%s'", device);
+    this->handle = handle;
+    return TRUE;
+  }
+
+  if (ignore_error)
+    return FALSE;
+
+  /* ALSA inverts standard errno.h error codes */
+  switch (-ret) {
+    case EBUSY:
+      GST_ELEMENT_ERROR (GST_ELEMENT (this), RESOURCE, BUSY,
+          (_("ALSA device \"%s\" is already in use by another program."),
+              this->device), (NULL));
+      break;
+    case EACCES:
+    case ETXTBSY:
+      GST_ELEMENT_ERROR (GST_ELEMENT (this), RESOURCE, OPEN_READ_WRITE,
+          (_("Could not access ALSA device \"%s\", check its permissions."),
+              this->device), GST_ERROR_SYSTEM);
+      break;
+
+    case ENXIO:
+    case ENODEV:
+    case ENOENT:
+      GST_ELEMENT_ERROR (GST_ELEMENT (this), RESOURCE, NOT_FOUND,
+          (_("ALSA device \"%s\" does not exist."), this->device), (NULL));
+      break;
+    default:
+      GST_ELEMENT_ERROR (GST_ELEMENT (this), RESOURCE, OPEN_READ_WRITE,
+          (_("ALSA device \"%s\" had an error."),
+              this->device), ("ALSA error %d: %s", ret, snd_strerror (ret)));
+      break;
+  }
+  return FALSE;
+}
+
+static gchar *
+gst_snd_pcm_info_get_real_device (snd_pcm_info_t * info)
+{
+  const gchar *id, *name, *subdev_name, *s;
+  gint card_num;
+
+  id = snd_pcm_info_get_id (info);
+  name = snd_pcm_info_get_name (info);
+  subdev_name = snd_pcm_info_get_subdevice_name (info);
+
+  GST_DEBUG ("PCM info: id='%s', name='%s', subdevice_name='%s', card=%d, "
+      "subdevice=%u", GST_STR_NULL (id), GST_STR_NULL (name),
+      GST_STR_NULL (subdev_name), snd_pcm_info_get_card (info),
+      snd_pcm_info_get_subdevice (info));
+
+  if (!id || *id == '\0')
+    return NULL;
+
+  s = strchr (id, ':');
+  if (!s || sscanf (s, ":%d", &card_num) != 1)
+    return NULL;
+
+  return g_strdup (id);
+}
+
+static gboolean
 gst_alsa_open_audio (GstAlsa * this)
 {
   snd_pcm_info_t *info;
-  int ret;
+  gchar *real_dev = NULL;
 
   g_assert (this != NULL);
   g_assert (this->handle == NULL);
@@ -1622,42 +1702,39 @@ gst_alsa_open_audio (GstAlsa * this)
       "error opening log output: %s");
 #endif
 
-  if ((ret = snd_pcm_open (&this->handle, this->device,
-              GST_ALSA_GET_CLASS (this)->stream, SND_PCM_NONBLOCK)) < 0) {
-    /* ALSA inverts standard errno.h error codes */
-    switch (-ret) {
-      case EBUSY:
-        GST_ELEMENT_ERROR (GST_ELEMENT (this), RESOURCE, BUSY,
-            (_("ALSA device \"%s\" is already in use by another program."),
-                this->device), (NULL));
-        break;
-      case EACCES:
-      case ETXTBSY:
-        GST_ELEMENT_ERROR (GST_ELEMENT (this), RESOURCE, OPEN_READ_WRITE,
-            (_("Could not access ALSA device \"%s\", check its permissions."),
-                this->device), GST_ERROR_SYSTEM);
-        break;
-
-      case ENXIO:
-      case ENODEV:
-      case ENOENT:
-        GST_ELEMENT_ERROR (GST_ELEMENT (this), RESOURCE, NOT_FOUND,
-            (_("ALSA device \"%s\" does not exist."), this->device), (NULL));
-        break;
-      default:
-        GST_ELEMENT_ERROR (GST_ELEMENT (this), RESOURCE, OPEN_READ_WRITE,
-            (_("ALSA device \"%s\" had an error."),
-                this->device), ("ALSA error %d: %s", ret, snd_strerror (ret)));
-        break;
-    }
+  if (!gst_alsa_open_audio_device (this, this->device, FALSE))
     return FALSE;
-  }
 
   snd_pcm_info_malloc (&info);
   snd_pcm_info (this->handle, info);
   this->cardname = g_strdup (snd_pcm_info_get_name (info));
+  if (strcmp (this->device, "default") == 0)
+    real_dev = gst_snd_pcm_info_get_real_device (info);
   snd_pcm_info_free (info);
 
+  /* If we have opened the default device, chances are that there's a plughw
+   * layer somewhere between us and the driver and that layer will claim it
+   * can do almost any format in the world, while we really just want the
+   * formats we can use directly; so what we do here is to try and open the
+   * underlying device of the 'default' device instead, which should give
+   * us much better results when probing the device capabilities later on */
+
+  if (real_dev != NULL) {
+    GST_DEBUG ("Closing %s, opening %s instead", this->device, real_dev);
+    snd_pcm_close (this->handle);
+    this->handle = NULL;
+    if (!gst_alsa_open_audio_device (this, real_dev, TRUE)) {
+      GST_DEBUG ("Cannot open %s, reverting to %s", real_dev, this->device);
+      if (!gst_alsa_open_audio_device (this, this->device, FALSE)) {
+        g_free (real_dev);
+        return FALSE;
+      }
+    } else {
+      GST_DEBUG ("Opened device %s", real_dev);
+    }
+    g_free (real_dev);
+  }
+
   GST_FLAG_SET (this, GST_ALSA_OPEN);
   return TRUE;
 }
