summaryrefslogtreecommitdiffstats
path: root/ext/hal/hal.c
blob: 57da8425ec438f7c9d4ef5652eeb4fba45492c4c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* GStreamer
 * Copyright (C) <2002> Thomas Vander Stichele <thomas@apestaart.org>
 * Copyright (C) <2006> Jürg Billeter <j@bitron.ch>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * this library handles interaction with Hal
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <glib.h>
#include "hal.h"


/* external functions */

/**
 * gst_hal_get_string:
 * @key: a #gchar corresponding to the key you want to get.
 *
 * Get Hal UDI @udi's string value.
 *
 * Returns: a newly allocated #gchar string containing the appropriate pipeline
 * for UDI @udi, or NULL in the case of an error..
 */
static gchar *
gst_hal_get_string (const gchar * udi)
{
  DBusConnection *connection;
  DBusError error;
  LibHalContext *ctx;
  char *string;

  /* Don't query HAL for NULL UDIs. Passing NULL as UDI to HAL gives
   * an assertion failure in D-Bus when running with
   * DBUS_FATAL_WARNINGS=1. */
  if (!udi)
    return NULL;

  dbus_error_init (&error);

  connection = dbus_bus_get (DBUS_BUS_SYSTEM, &error);
  g_return_val_if_fail (connection != NULL, NULL);

  ctx = libhal_ctx_new ();
  g_return_val_if_fail (ctx != NULL, NULL);

  libhal_ctx_set_dbus_connection (ctx, connection);
  libhal_ctx_init (ctx, &error);

  string = NULL;

  if (libhal_device_query_capability (ctx, udi, "alsa", &error)) {
    char *type, *element = NULL;

    type = libhal_device_get_property_string (ctx, udi, "alsa.type", &error);
    if (type != NULL && strcmp (type, "playback") == 0) {
      element = "alsasink";
    } else if (type != NULL && strcmp (type, "capture") == 0) {
      element = "alsasrc";
    }
    if (element) {
      int card, device;

      card = libhal_device_get_property_int (ctx, udi, "alsa.card", &error);
      device = libhal_device_get_property_int (ctx, udi, "alsa.device", &error);
      if (device == 0) {
        /* handle default device specially to use
         * dmix, dsnoop, and softvol if appropriate */
        string = g_strdup_printf ("%s device=default:%d", element, card);
      } else {
        string =
            g_strdup_printf ("%s device=plughw:%d,%d", element, card, device);
      }
    }
  }

  libhal_ctx_shutdown (ctx, &error);
  libhal_ctx_free (ctx);

  dbus_error_free (&error);

  if (string == NULL) {
    GST_WARNING ("Problem parsing HAL ALSA capabilities for udi %s", udi);
  }

  return string;
}

/**
 * gst_hal_render_bin_from_udi:
 * @key: a #gchar string corresponding to a Hal UDI.
 *
 * Render bin from Hal UDI @udi.
 *
 * Returns: a #GstElement containing the rendered bin.
 */
GstElement *
gst_hal_render_bin_from_udi (const gchar * udi)
{
  GstElement *bin = NULL;
  gchar *value;

  value = gst_hal_get_string (udi);
  if (value)
    bin = gst_parse_bin_from_description (value, TRUE, NULL);
  g_free (value);
  return bin;
}

/**
 * gst_hal_get_audio_sink:
 *
 * Render audio output bin from GStreamer Hal UDI.
 * If no device with the specified UDI exists, the default audio sink for the
 * platform is used (typically osssink or sunaudiosink).
 *
 * Returns: a #GstElement containing the audio output bin, or NULL if
 * everything failed.
 */
GstElement *
gst_hal_get_audio_sink (const gchar * udi)
{
  GstElement *ret = gst_hal_render_bin_from_udi (udi);

  if (!ret) {
    ret = gst_element_factory_make (DEFAULT_AUDIOSINK, NULL);

    if (!ret)
      g_warning ("No Hal default audio sink key and %s doesn't work",
          DEFAULT_AUDIOSINK);
  }

  return ret;
}

/**
 * gst_hal_get_audio_src:
 *
 * Render audio acquisition bin from GStreamer Hal UDI.
 * If no device with the specified UDI exists, the default audio source for the
 * plaform is used (typically osssrc or sunaudiosrc).
 *
 * Returns: a #GstElement containing the audio source bin, or NULL if
 * everything failed.
 */
GstElement *
gst_hal_get_audio_src (const gchar * udi)
{
  GstElement *ret = gst_hal_render_bin_from_udi (udi);

  if (!ret) {
    ret = gst_element_factory_make (DEFAULT_AUDIOSRC, NULL);

    if (!ret)
      g_warning ("No Hal default audio src key and %s doesn't work",
          DEFAULT_AUDIOSRC);
  }

  return ret;
}