Blame ext/alsa/gstalsadeviceprobe.c

Packit 971217
/* Copyright (C) 2001 CodeFactory AB
Packit 971217
 * Copyright (C) 2001 Thomas Nyberg <thomas@codefactory.se>
Packit 971217
 * Copyright (C) 2001-2002 Andy Wingo <apwingo@eos.ncsu.edu>
Packit 971217
 * Copyright (C) 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
Packit 971217
 * Copyright (C) 2005 Tim-Philipp Müller <tim centricular net>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the Free
Packit 971217
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
/* FIXME 0.11: suppress warnings for deprecated API such as GValueArray
Packit 971217
 * with newer GLib versions (>= 2.31.0) */
Packit 971217
#define GLIB_DISABLE_DEPRECATION_WARNINGS
Packit 971217
Packit 971217
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include "gstalsadeviceprobe.h"
Packit 971217
Packit 971217
#if 0
Packit 971217
G_LOCK_DEFINE_STATIC (probe_lock);
Packit 971217
Packit 971217
static const GList *
Packit 971217
gst_alsa_device_property_probe_get_properties (GstPropertyProbe * probe)
Packit 971217
{
Packit 971217
  GObjectClass *klass = G_OBJECT_GET_CLASS (probe);
Packit 971217
  static GList *list = NULL;
Packit 971217
Packit 971217
  G_LOCK (probe_lock);
Packit 971217
Packit 971217
  if (!list) {
Packit 971217
    GParamSpec *pspec;
Packit 971217
Packit 971217
    pspec = g_object_class_find_property (klass, "device");
Packit 971217
    list = g_list_append (NULL, pspec);
Packit 971217
  }
Packit 971217
Packit 971217
  G_UNLOCK (probe_lock);
Packit 971217
Packit 971217
  return list;
Packit 971217
}
Packit 971217
Packit 971217
static GList *
Packit 971217
gst_alsa_get_device_list (snd_pcm_stream_t stream)
Packit 971217
{
Packit 971217
  snd_ctl_t *handle;
Packit 971217
  int card, dev;
Packit 971217
  snd_ctl_card_info_t *info;
Packit 971217
  snd_pcm_info_t *pcminfo;
Packit 971217
  gboolean mixer = (stream == -1);
Packit 971217
  GList *list = NULL;
Packit 971217
Packit 971217
  if (stream == -1)
Packit 971217
    stream = 0;
Packit 971217
Packit 971217
  snd_ctl_card_info_malloc (&info;;
Packit 971217
  snd_pcm_info_malloc (&pcminfo);
Packit 971217
  card = -1;
Packit 971217
Packit 971217
  if (snd_card_next (&card) < 0 || card < 0) {
Packit 971217
    /* no soundcard found */
Packit 971217
    GST_WARNING ("No soundcard found");
Packit 971217
    goto beach;
Packit 971217
  }
Packit 971217
Packit 971217
  while (card >= 0) {
Packit 971217
    gchar name[32];
Packit 971217
Packit 971217
    g_snprintf (name, sizeof (name), "hw:%d", card);
Packit 971217
    if (snd_ctl_open (&handle, name, 0) < 0) {
Packit 971217
      goto next_card;
Packit 971217
    }
Packit 971217
    if (snd_ctl_card_info (handle, info) < 0) {
Packit 971217
      snd_ctl_close (handle);
Packit 971217
      goto next_card;
Packit 971217
    }
Packit 971217
Packit 971217
    if (mixer) {
Packit 971217
      list = g_list_append (list, g_strdup (name));
Packit 971217
    } else {
Packit 971217
      dev = -1;
Packit 971217
      while (1) {
Packit 971217
        gchar *gst_device;
Packit 971217
Packit 971217
        snd_ctl_pcm_next_device (handle, &dev;;
Packit 971217
Packit 971217
        if (dev < 0)
Packit 971217
          break;
Packit 971217
        snd_pcm_info_set_device (pcminfo, dev);
Packit 971217
        snd_pcm_info_set_subdevice (pcminfo, 0);
Packit 971217
        snd_pcm_info_set_stream (pcminfo, stream);
Packit 971217
        if (snd_ctl_pcm_info (handle, pcminfo) < 0) {
Packit 971217
          continue;
Packit 971217
        }
Packit 971217
Packit 971217
        gst_device = g_strdup_printf ("hw:%d,%d", card, dev);
Packit 971217
        list = g_list_append (list, gst_device);
Packit 971217
      }
Packit 971217
    }
Packit 971217
    snd_ctl_close (handle);
Packit 971217
  next_card:
Packit 971217
    if (snd_card_next (&card) < 0) {
Packit 971217
      break;
Packit 971217
    }
Packit 971217
  }
Packit 971217
Packit 971217
beach:
Packit 971217
  snd_ctl_card_info_free (info);
Packit 971217
  snd_pcm_info_free (pcminfo);
Packit 971217
Packit 971217
  return list;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_alsa_device_property_probe_probe_property (GstPropertyProbe * probe,
Packit 971217
    guint prop_id, const GParamSpec * pspec)
Packit 971217
{
Packit 971217
  if (!g_str_equal (pspec->name, "device")) {
Packit 971217
    G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
Packit 971217
  }
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
gst_alsa_device_property_probe_needs_probe (GstPropertyProbe * probe,
Packit 971217
    guint prop_id, const GParamSpec * pspec)
Packit 971217
{
Packit 971217
  /* don't cache probed data */
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static GValueArray *
Packit 971217
gst_alsa_device_property_probe_get_values (GstPropertyProbe * probe,
Packit 971217
    guint prop_id, const GParamSpec * pspec)
Packit 971217
{
Packit 971217
  GstElementClass *klass;
Packit 971217
  const GList *templates;
Packit 971217
  snd_pcm_stream_t mode = -1;
Packit 971217
  GValueArray *array;
Packit 971217
  GValue value = { 0, };
Packit 971217
  GList *l, *list;
Packit 971217
Packit 971217
  if (!g_str_equal (pspec->name, "device")) {
Packit 971217
    G_OBJECT_WARN_INVALID_PROPERTY_ID (probe, prop_id, pspec);
Packit 971217
    return NULL;
Packit 971217
  }
Packit 971217
Packit 971217
  klass = GST_ELEMENT_GET_CLASS (GST_ELEMENT (probe));
Packit 971217
Packit 971217
  /* I'm pretty sure ALSA has a good way to do this. However, their cool
Packit 971217
   * auto-generated documentation is pretty much useless if you try to
Packit 971217
   * do function-wise look-ups. */
Packit 971217
  /* we assume one pad template at max [zero=mixer] */
Packit 971217
  templates = gst_element_class_get_pad_template_list (klass);
Packit 971217
  if (templates) {
Packit 971217
    if (GST_PAD_TEMPLATE_DIRECTION (templates->data) == GST_PAD_SRC)
Packit 971217
      mode = SND_PCM_STREAM_CAPTURE;
Packit 971217
    else
Packit 971217
      mode = SND_PCM_STREAM_PLAYBACK;
Packit 971217
  }
Packit 971217
Packit 971217
  list = gst_alsa_get_device_list (mode);
Packit 971217
Packit 971217
  if (list == NULL) {
Packit 971217
    GST_LOG_OBJECT (probe, "No devices found");
Packit 971217
    return NULL;
Packit 971217
  }
Packit 971217
Packit 971217
  array = g_value_array_new (g_list_length (list));
Packit 971217
  g_value_init (&value, G_TYPE_STRING);
Packit 971217
  for (l = list; l != NULL; l = l->next) {
Packit 971217
    GST_LOG_OBJECT (probe, "Found device: %s", (gchar *) l->data);
Packit 971217
    g_value_take_string (&value, (gchar *) l->data);
Packit 971217
    l->data = NULL;
Packit 971217
    g_value_array_append (array, &value);
Packit 971217
  }
Packit 971217
  g_value_unset (&value);
Packit 971217
  g_list_free (list);
Packit 971217
Packit 971217
  return array;
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
gst_alsa_property_probe_interface_init (GstPropertyProbeInterface * iface)
Packit 971217
{
Packit 971217
  iface->get_properties = gst_alsa_device_property_probe_get_properties;
Packit 971217
  iface->probe_property = gst_alsa_device_property_probe_probe_property;
Packit 971217
  iface->needs_probe = gst_alsa_device_property_probe_needs_probe;
Packit 971217
  iface->get_values = gst_alsa_device_property_probe_get_values;
Packit 971217
}
Packit 971217
Packit 971217
void
Packit 971217
gst_alsa_type_add_device_property_probe_interface (GType type)
Packit 971217
{
Packit 971217
  static const GInterfaceInfo probe_iface_info = {
Packit 971217
    (GInterfaceInitFunc) gst_alsa_property_probe_interface_init,
Packit 971217
    NULL,
Packit 971217
    NULL,
Packit 971217
  };
Packit 971217
Packit 971217
  g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
Packit 971217
      &probe_iface_info);
Packit 971217
}
Packit 971217
#endif