summaryrefslogtreecommitdiff
path: root/ext/xine/xineaudiosink.c
blob: 2073bcfb96b0b3bcb0bcea449fd028468b87fb8a (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/* GStreamer
 * Copyright (C) 2004 Benjamin Otte <in7y118@public.uni-hamburg.de>
 *
 * 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.
 */

#include <gst/gst.h>
#include "gstxine.h"
#include <xine/audio_out.h>
#include <xine/xine_internal.h>
#include <xine/plugin_catalog.h>

#define GST_TYPE_XINE_AUDIO_SINK \
  (gst_xine_audio_sink_get_type())
#define GST_XINE_AUDIO_SINK(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_XINE_AUDIO_SINK,GstXineAudioSink))
#define GST_XINE_AUDIO_SINK_GET_CLASS(obj) \
  (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_XINE_AUDIO_SINK, GstXineAudioSinkClass))
#define GST_XINE_AUDIO_SINK_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_XINE_AUDIO_SINK,GstXineAudioSinkClass))
#define GST_IS_XINE_AUDIO_SINK(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_XINE_AUDIO_SINK))
#define GST_IS_XINE_AUDIO_SINK_CLASS(obj) \
  (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_XINE_AUDIO_SINK))

GType gst_xine_audio_sink_get_type (void);

typedef struct _GstXineAudioSink GstXineAudioSink;
typedef struct _GstXineAudioSinkClass GstXineAudioSinkClass;

struct _GstXineAudioSink
{
  GstXine parent;

  GstPad *sinkpad;

  ao_driver_t *driver;
  guint open;                   /* number of bytes per sample or 0 if driver not open */
};

struct _GstXineAudioSinkClass
{
  GstXineClass parent_class;

  plugin_node_t *plugin_node;
};

/** GstXineAudioSink ***********************************************************/

GST_BOILERPLATE (GstXineAudioSink, gst_xine_audio_sink, GstXine, GST_TYPE_XINE)

     static GstStateChangeReturn
         gst_xine_audio_sink_change_state (GstElement * element,
    GstStateChange transition);

     static void gst_xine_audio_sink_base_init (gpointer g_class)
{
}

static void
gst_xine_audio_sink_class_init (GstXineAudioSinkClass * klass)
{
  GstElementClass *element = GST_ELEMENT_CLASS (klass);

  element->change_state = gst_xine_audio_sink_change_state;
}

static void
gst_xine_audio_sink_init (GstXineAudioSink * xine,
    GstXineAudioSinkClass * g_class)
{
}

static void
gst_xine_audio_sink_chain (GstPad * pad, GstData * data)
{
  GstXineAudioSink *xine =
      GST_XINE_AUDIO_SINK (gst_object_get_parent (GST_OBJECT (pad)));

  while (xine->driver->write (xine->driver, (guint16 *) GST_BUFFER_DATA (data),
          GST_BUFFER_SIZE (data) / xine->open) == 0);
  gst_data_unref (GST_DATA (data));
}

static GstStateChangeReturn
gst_xine_audio_sink_change_state (GstElement * element,
    GstStateChange transition)
{
  GstXineAudioSink *xine = GST_XINE_AUDIO_SINK (element);
  audio_driver_class_t *driver =
      (audio_driver_class_t *) GST_XINE_AUDIO_SINK_GET_CLASS (xine)->
      plugin_node->plugin_class;

  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
      if (driver == NULL) {
        xine_audio_port_t *port =
            xine_open_audio_driver (GST_XINE_GET_CLASS (xine)->xine,
            GST_XINE_AUDIO_SINK_GET_CLASS (xine)->plugin_node->info->id, NULL);

        if (!port)
          return GST_STATE_CHANGE_FAILURE;
        port->exit (port);
        driver =
            (audio_driver_class_t *) GST_XINE_AUDIO_SINK_GET_CLASS (xine)->
            plugin_node->plugin_class;
        if (driver == NULL)
          return GST_STATE_CHANGE_FAILURE;
      }
      xine->driver = driver->open_plugin (driver, NULL);
      if (!xine->driver)
        return GST_STATE_CHANGE_FAILURE;
      break;
    case GST_STATE_CHANGE_READY_TO_PAUSED:
      break;
    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
      break;
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
      break;
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      if (xine->open != 0)
        xine->driver->close (xine->driver);
      xine->open = 0;
      break;
    case GST_STATE_CHANGE_READY_TO_NULL:
      xine->driver->exit (xine->driver);
      xine->driver = NULL;
      break;
    default:
      GST_ERROR_OBJECT (element, "invalid state change");
      break;
  }

  return GST_CALL_PARENT_WITH_DEFAULT (GST_ELEMENT_CLASS, change_state,
      (element), GST_STATE_CHANGE_SUCCESS);
}

static GstCaps *
_xine_audio_sink_get_caps (GstPad * pad)
{
  GstXineAudioSink *xine =
      GST_XINE_AUDIO_SINK (gst_object_get_parent (GST_OBJECT (pad)));
  GstCaps *caps, *ret = gst_caps_new_empty ();
  guint32 capa, channels;

  if (!xine->driver)
    return gst_caps_copy (gst_pad_get_pad_template_caps (pad));

  capa = xine->driver->get_capabilities (xine->driver);
  channels = capa & (AO_CAP_MODE_MONO | AO_CAP_MODE_STEREO);

  if (channels == 0) {
    /* neither mono nor stereo supported, die */
    return ret;
  }

  /* this loop is messy */
  capa &= AO_CAP_8BITS;
  do {
    if (capa & AO_CAP_8BITS) {
      caps = gst_caps_from_string ("audio/x-raw-int, "
          "signed = (boolean) FALSE, "
          "width = (int) 8, "
          "depth = (int) 8, " "rate = (int) [ 8000, 192000 ]");
      capa &= ~AO_CAP_8BITS;
    } else {
      caps = gst_caps_from_string ("audio/x-raw-int, "
          "endianness = (int) BYTE_ORDER, "
          "signed = (boolean) TRUE, "
          "width = (int) 16, "
          "depth = (int) 16, " "rate = (int) [ 8000, 192000 ]");
      capa = -1;
    }
    switch (channels) {
      case AO_CAP_MODE_MONO | AO_CAP_MODE_STEREO:
        gst_caps_set_simple (caps, "channels", GST_TYPE_INT_RANGE, 1, 2, NULL);
        break;
      case AO_CAP_MODE_MONO:
        gst_caps_set_simple (caps, "channels", G_TYPE_INT, 1, NULL);
        break;
      case AO_CAP_MODE_STEREO:
        gst_caps_set_simple (caps, "channels", G_TYPE_INT, 2, NULL);
        break;
      default:
        g_assert_not_reached ();
        break;
    }
    gst_caps_append (ret, caps);
  } while (capa != -1);

  return ret;
}

static GstPadLinkReturn
_xine_audio_sink_link (GstPad * pad, const GstCaps * caps)
{
  GstStructure *structure = gst_caps_get_structure (caps, 0);
  GstXineAudioSink *xine =
      GST_XINE_AUDIO_SINK (gst_object_get_parent (GST_OBJECT (pad)));
  guint channels, temp, rate, width;
  int mode;

  if (!gst_structure_get_int (structure, "channels", &channels))
    return GST_PAD_LINK_REFUSED;
  mode = (channels == 1) ? AO_CAP_MODE_MONO : AO_CAP_MODE_STEREO;
  if (!gst_structure_get_int (structure, "rate", &rate))
    return GST_PAD_LINK_REFUSED;
  if (!gst_structure_get_int (structure, "width", &width))
    return GST_PAD_LINK_REFUSED;

  if (xine->open != 0)
    xine->driver->close (xine->driver);
  xine->open = 0;
  temp = xine->driver->open (xine->driver, width, rate, mode);
  if (temp == 0)
    return GST_PAD_LINK_REFUSED;

  xine->open = channels * width / 8;
  if (temp != rate) {
    /* FIXME? */
    GST_WARNING_OBJECT (xine, "rates don't match (requested: %u, got %u)", rate,
        temp);
  }

  return GST_PAD_LINK_OK;
}

/** GstXineAudioSink subclasses ************************************************/

static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/x-raw-int, "
        "signed = (boolean) FALSE, "
        "width = (int) 8, "
        "depth = (int) 8, "
        "rate = (int) [ 8000, 192000 ], "
        "channels = (int) [1, 2]; "
        "audio/x-raw-int, "
        "endianness = (int) BYTE_ORDER, "
        "signed = (boolean) TRUE, "
        "width = (int) 16, "
        "depth = (int) 16, "
        "rate = (int) [ 8000, 192000 ], " "channels = (int) [1, 2]")
    );

static void
gst_xine_audio_sink_subclass_init (gpointer g_class, gpointer class_data)
{
  GstXineAudioSinkClass *xine_class = GST_XINE_AUDIO_SINK_CLASS (g_class);
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
  GstElementDetails details = GST_ELEMENT_DETAILS (NULL,
      "Source",
      NULL,
      "Benjamin Otte <otte@gnome.org>");

  xine_class->plugin_node = class_data;
  details.longname =
      g_strdup_printf ("%s xine audio sink", xine_class->plugin_node->info->id);
  details.description =
      g_strdup_printf ("%s audio output using Xine",
      xine_class->plugin_node->info->id);
  gst_element_class_set_details (element_class, &details);
  g_free (details.longname);
  g_free (details.description);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&sink_template));
}

static void
gst_xine_audio_sink_sub_init (GTypeInstance * instance, gpointer g_class)
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (instance);
  GstXineAudioSink *xine = GST_XINE_AUDIO_SINK (instance);

  xine->sinkpad =
      gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
          "sink"), "sink");
  gst_pad_set_chain_function (xine->sinkpad, gst_xine_audio_sink_chain);
  gst_pad_set_getcaps_function (xine->sinkpad, _xine_audio_sink_get_caps);
  gst_pad_set_link_function (xine->sinkpad, _xine_audio_sink_link);
  gst_element_add_pad (GST_ELEMENT (xine), xine->sinkpad);
}

gboolean
gst_xine_audio_sink_init_plugin (GstPlugin * plugin)
{
  GTypeInfo plugin_info = {
    sizeof (GstXineAudioSinkClass),
    NULL,
    NULL,
    gst_xine_audio_sink_subclass_init,
    NULL,
    NULL,
    sizeof (GstXineAudioSink),
    0,
    gst_xine_audio_sink_sub_init,
  };
  plugin_node_t *node;
  GstXineClass *klass;

  klass = g_type_class_ref (GST_TYPE_XINE);

  node = xine_list_first_content (klass->xine->plugin_catalog->aout);
  while (node) {
    gchar *plugin_name = g_strdup_printf ("xineaudiosink_%s", node->info->id);
    gchar *type_name = g_strdup_printf ("GstXineAudioSink%s", node->info->id);
    GType type;

    plugin_info.class_data = node;
    type =
        g_type_register_static (GST_TYPE_XINE_AUDIO_SINK, type_name,
        &plugin_info, 0);
    g_free (type_name);
    if (!gst_element_register (plugin, plugin_name, GST_RANK_MARGINAL, type)) {
      g_free (plugin_name);
      return FALSE;
    }
    g_free (plugin_name);

    node = xine_list_next_content (klass->xine->plugin_catalog->aout);
  }

  g_type_class_unref (klass);
  return TRUE;
}