esdsink.c 11.5 KB
Newer Older
1
/* GStreamer
2
3
4
 * Copyright (C) <2005> Arwed v. Merkatz <v.merkatz@gmx.net>
 *
 * Roughly based on the gstreamer 0.8 esdsink plugin:
5
6
 * Copyright (C) <2001> Richard Boulton <richard-gst@tartarus.org>
 *
7
 * esdsink.c: an EsounD audio sink
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * 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.
 */

25
26
27
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
28

29
30
#include "esdsink.h"
#include <esd.h>
31
#include <unistd.h>
32
#include <errno.h>
33

34
35
#include <gst/gst-i18n-plugin.h>

36
37
38
39
40
/* wtay: from my esd.h (debian unstable libesd0-dev 0.2.36-3) */
#ifndef ESD_MAX_WRITE_SIZE
#define ESD_MAX_WRITE_SIZE (21 * 4096)
#endif

41
GST_DEBUG_CATEGORY_EXTERN (esd_debug);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
42
#define GST_CAT_DEFAULT esd_debug
43

44
/* elementfactory information */
45
static const GstElementDetails esdsink_details =
Wim Taymans's avatar
Wim Taymans committed
46
47
48
49
GST_ELEMENT_DETAILS ("Esound audio sink",
    "Sink/Audio",
    "Plays audio to an esound server",
    "Arwed von Merkatz <v.merkatz@gmx.net>");
50

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
51
52
enum
{
53
54
  PROP_0,
  PROP_HOST
55
56
};

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
57
58
59
60
static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/x-raw-int, "
61
62
63
        "endianness = (int) " G_STRINGIFY (G_BYTE_ORDER) ", "
        "signed = (boolean) TRUE, "
        "width = (int) 16, "
64
65
66
67
68
69
70
71
        "depth = (int) 16, "
        "rate = (int) [ 1, MAX ], "
        "channels = (int) [ 1, 2 ]; "
        "audio/x-raw-int, "
        "signed = (boolean) { true, false }, "
        "width = (int) 8, "
        "depth = (int) 8, "
        "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, 2 ]")
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
72
73
74
    );

static void gst_esdsink_base_init (gpointer g_class);
75
76
static void gst_esdsink_class_init (GstEsdSinkClass * klass);
static void gst_esdsink_init (GstEsdSink * esdsink);
77
static void gst_esdsink_finalize (GObject * object);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
78

79
static GstCaps *gst_esdsink_getcaps (GstBaseSink * bsink);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
80

81
static gboolean gst_esdsink_open (GstAudioSink * asink);
82
static gboolean gst_esdsink_close (GstAudioSink * asink);
83
84
85
static gboolean gst_esdsink_prepare (GstAudioSink * asink,
    GstRingBufferSpec * spec);
static gboolean gst_esdsink_unprepare (GstAudioSink * asink);
86
87
88
89
static guint gst_esdsink_write (GstAudioSink * asink, gpointer data,
    guint length);
static guint gst_esdsink_delay (GstAudioSink * asink);
static void gst_esdsink_reset (GstAudioSink * asink);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
90
91
92
93
94

static void gst_esdsink_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_esdsink_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);
95
96

static GstElementClass *parent_class = NULL;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
97

98
99
100
101
102
103
104
GType
gst_esdsink_get_type (void)
{
  static GType esdsink_type = 0;

  if (!esdsink_type) {
    static const GTypeInfo esdsink_info = {
105
      sizeof (GstEsdSinkClass),
Iain Holmes's avatar
Iain Holmes committed
106
      gst_esdsink_base_init,
107
      NULL,
108
      (GClassInitFunc) gst_esdsink_class_init,
109
110
      NULL,
      NULL,
111
      sizeof (GstEsdSink),
112
      0,
113
      (GInstanceInitFunc) gst_esdsink_init,
114
    };
115

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
116
    esdsink_type =
117
118
        g_type_register_static (GST_TYPE_AUDIO_SINK, "GstEsdSink",
        &esdsink_info, 0);
119
120
121
122
  }
  return esdsink_type;
}

Iain Holmes's avatar
Iain Holmes committed
123
124
125
126
127
static void
gst_esdsink_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
128
  gst_element_class_add_pad_template (element_class,
David Schleef's avatar
David Schleef committed
129
      gst_static_pad_template_get (&sink_factory));
Iain Holmes's avatar
Iain Holmes committed
130
131
132
  gst_element_class_set_details (element_class, &esdsink_details);
}

133
static void
134
gst_esdsink_class_init (GstEsdSinkClass * klass)
135
{
136
137
138
139
140
141
142
143
144
145
  GObjectClass *gobject_class;
  GstBaseSinkClass *gstbasesink_class;
  GstBaseAudioSinkClass *gstbaseaudiosink_class;
  GstAudioSinkClass *gstaudiosink_class;

  gobject_class = (GObjectClass *) klass;
  gstbasesink_class = (GstBaseSinkClass *) klass;
  gstbaseaudiosink_class = (GstBaseAudioSinkClass *) klass;
  gstaudiosink_class = (GstAudioSinkClass *) klass;

146
  parent_class = g_type_class_peek_parent (klass);
147

148
  gobject_class->finalize = gst_esdsink_finalize;
149
150
151
152
153

  gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_esdsink_getcaps);

  gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_esdsink_open);
  gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_esdsink_close);
154
155
  gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_esdsink_prepare);
  gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_esdsink_unprepare);
156
157
158
  gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_esdsink_write);
  gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_esdsink_delay);
  gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_esdsink_reset);
159
160
161
162

  gobject_class->set_property = gst_esdsink_set_property;
  gobject_class->get_property = gst_esdsink_get_property;

163
164
165
166
  /* default value is filled in the _init method */
  g_object_class_install_property (gobject_class, PROP_HOST,
      g_param_spec_string ("host", "Host",
          "The host running the esound daemon", NULL, G_PARAM_READWRITE));
167
168
169
}

static void
170
gst_esdsink_init (GstEsdSink * esdsink)
171
172
{
  esdsink->fd = -1;
173
  esdsink->ctrl_fd = -1;
174
  esdsink->host = g_strdup (g_getenv ("ESPEAKER"));
175
176
}

177
static void
178
gst_esdsink_finalize (GObject * object)
179
{
180
  GstEsdSink *esdsink = GST_ESDSINK (object);
181
182

  g_free (esdsink->host);
183

184
  G_OBJECT_CLASS (parent_class)->finalize (object);
185
186
}

187
188
189
static GstCaps *
gst_esdsink_getcaps (GstBaseSink * bsink)
{
190
191
  GstEsdSink *esdsink;
  GstPadTemplate *pad_template;
192
  GstCaps *caps = NULL;
193
  gint i;
194
195
196
197
  esd_server_info_t *server_info;

  esdsink = GST_ESDSINK (bsink);

198
199
  GST_DEBUG_OBJECT (esdsink, "getcaps called");

200
201
202
  pad_template = gst_static_pad_template_get (&sink_factory);
  caps = gst_caps_copy (gst_pad_template_get_caps (pad_template));

203
204
205
  /* no fd, we're done with the template caps */
  if (esdsink->ctrl_fd < 0)
    goto done;
206

207
  /* get server info */
208
  server_info = esd_get_server_info (esdsink->ctrl_fd);
209
210
  if (!server_info)
    goto no_info;
211

212
  GST_DEBUG_OBJECT (esdsink, "got server info rate: %i", server_info->rate);
213

214
215
216
217
218
219
220
  for (i = 0; i < caps->structs->len; i++) {
    GstStructure *s;

    s = gst_caps_get_structure (caps, i);
    gst_structure_set (s, "rate", G_TYPE_INT, server_info->rate, NULL);
  }
  esd_free_server_info (server_info);
221

222
223
done:
  return caps;
224

225
226
227
  /* ERRORS */
no_info:
  {
228
229
230
231
232
233
234
235
236
237
238
    GST_WARNING_OBJECT (esdsink, "couldn't get server info!");
    gst_caps_unref (caps);
    return NULL;
  }
}

static gboolean
gst_esdsink_open (GstAudioSink * asink)
{
  GstEsdSink *esdsink = GST_ESDSINK (asink);

239
240
  GST_DEBUG_OBJECT (esdsink, "open");

241
242
243
244
245
246
  esdsink->ctrl_fd = esd_open_sound (esdsink->host);
  if (esdsink->ctrl_fd < 0)
    goto couldnt_connect;

  return TRUE;

247
  /* ERRORS */
248
249
couldnt_connect:
  {
250
251
    GST_ELEMENT_ERROR (esdsink, RESOURCE, OPEN_WRITE,
        (_("Could not establish connection to sound server")),
252
253
        ("can't open connection to esound server"));
    return FALSE;
254
  }
255
}
256

257
static gboolean
258
259
260
261
gst_esdsink_close (GstAudioSink * asink)
{
  GstEsdSink *esdsink = GST_ESDSINK (asink);

262
263
  GST_DEBUG_OBJECT (esdsink, "close");

264
  esd_close (esdsink->ctrl_fd);
265
  esdsink->ctrl_fd = -1;
266
267
268
269
270
271

  return TRUE;
}

static gboolean
gst_esdsink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
272
{
273
  GstEsdSink *esdsink = GST_ESDSINK (asink);
274
  esd_format_t esdformat;
275

276
  /* Name used by esound for this connection. */
Stefan Kost's avatar
Stefan Kost committed
277
  const char connname[] = "GStreamer";
278
279

  GST_DEBUG_OBJECT (esdsink, "prepare");
280

281
  /* Bitmap describing audio format. */
282
  esdformat = ESD_STREAM | ESD_PLAY;
283

284
285
286
287
288
289
290
291
292
  switch (spec->depth) {
    case 8:
      esdformat |= ESD_BITS8;
      break;
    case 16:
      esdformat |= ESD_BITS16;
      break;
    default:
      goto unsupported_depth;
293
  }
294

295
296
297
298
299
300
301
302
303
  switch (spec->channels) {
    case 1:
      esdformat |= ESD_MONO;
      break;
    case 2:
      esdformat |= ESD_STEREO;
      break;
    default:
      goto unsupported_channels;
304
305
  }

306
307
308
  GST_INFO_OBJECT (esdsink,
      "attempting to open data connection to esound server");

309
310
311
  esdsink->fd =
      esd_play_stream (esdformat, spec->rate, esdsink->host, connname);

312
313
314
  if ((esdsink->fd < 0) || (esdsink->ctrl_fd < 0))
    goto cannot_open;

315
316
  esdsink->rate = spec->rate;

317
318
  spec->segsize = ESD_BUF_SIZE;
  spec->segtotal = (ESD_MAX_WRITE_SIZE / spec->segsize);
319
320
321
322
323
324
  spec->silence_sample[0] = 0;
  spec->silence_sample[1] = 0;
  spec->silence_sample[2] = 0;
  spec->silence_sample[3] = 0;

  GST_INFO_OBJECT (esdsink, "successfully opened connection to esound server");
325
326
327
328

  return TRUE;

  /* ERRORS */
329
330
331
332
333
334
335
336
337
338
339
340
341
unsupported_depth:
  {
    GST_ELEMENT_ERROR (esdsink, STREAM, WRONG_TYPE, (NULL),
        ("can't handle sample depth of %d, only 8 or 16 supported",
            spec->depth));
    return FALSE;
  }
unsupported_channels:
  {
    GST_ELEMENT_ERROR (esdsink, STREAM, WRONG_TYPE, (NULL),
        ("can't handle %d channels, only 1 or 2 supported", spec->channels));
    return FALSE;
  }
342
343
cannot_open:
  {
344
345
    GST_ELEMENT_ERROR (esdsink, RESOURCE, OPEN_WRITE,
        (_("Could not establish connection to sound server")),
346
347
348
        ("can't open connection to esound server"));
    return FALSE;
  }
349
350
}

351
static gboolean
352
gst_esdsink_unprepare (GstAudioSink * asink)
353
{
354
  GstEsdSink *esdsink = GST_ESDSINK (asink);
355

356
357
358
359
360
  if ((esdsink->fd < 0) && (esdsink->ctrl_fd < 0))
    return TRUE;

  close (esdsink->fd);
  esdsink->fd = -1;
361

362
363
  GST_INFO_OBJECT (esdsink, "closed sound device");

364
  return TRUE;
365
366
}

367

368
369
370
371
372
static guint
gst_esdsink_write (GstAudioSink * asink, gpointer data, guint length)
{
  GstEsdSink *esdsink = GST_ESDSINK (asink);
  gint to_write = 0;
373

374
  to_write = length;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
375

376
377
  while (to_write > 0) {
    int done;
378

379
    done = write (esdsink->fd, data, to_write);
380

381
382
    if (done < 0)
      goto write_error;
383
384
385

    to_write -= done;
    data += done;
386
  }
387
  return length;
388
389
390
391
392
393
394
395

  /* ERRORS */
write_error:
  {
    GST_ELEMENT_ERROR (esdsink, RESOURCE, WRITE,
        ("Failed to write data to the esound daemon"), GST_ERROR_SYSTEM);
    return 0;
  }
396
}
397

398
399
400
401
static guint
gst_esdsink_delay (GstAudioSink * asink)
{
  GstEsdSink *esdsink = GST_ESDSINK (asink);
402
403
404
405
  guint latency;

  latency = esd_get_latency (esdsink->ctrl_fd);

406
407
  /* latency is measured in samples at a rate of 44100, this 
   * cannot overflow. */
408
409
410
  latency = latency * 44100LL / esdsink->rate;

  GST_DEBUG_OBJECT (asink, "got latency: %u", latency);
411

412
413
414
415
416
417
  return latency;
}

static void
gst_esdsink_reset (GstAudioSink * asink)
{
418
  GST_DEBUG_OBJECT (asink, "reset called");
419
420
421
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
422
423
gst_esdsink_set_property (GObject * object, guint prop_id, const GValue * value,
    GParamSpec * pspec)
424
{
425
  GstEsdSink *esdsink = GST_ESDSINK (object);
426
427

  switch (prop_id) {
428
    case PROP_HOST:
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
429
      g_free (esdsink->host);
430
      esdsink->host = g_value_dup_string (value);
431
432
433
434
435
436
437
      break;
    default:
      break;
  }
}

static void
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
438
439
gst_esdsink_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
440
{
441
  GstEsdSink *esdsink = GST_ESDSINK (object);
442
443

  switch (prop_id) {
444
    case PROP_HOST:
445
446
447
448
449
450
451
452
      g_value_set_string (value, esdsink->host);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

453
gboolean
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
454
gst_esdsink_factory_init (GstPlugin * plugin)
455
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
456
  if (!gst_element_register (plugin, "esdsink", GST_RANK_NONE,
457
          GST_TYPE_ESDSINK))
Iain Holmes's avatar
Iain Holmes committed
458
    return FALSE;
459
460
461

  return TRUE;
}