summaryrefslogtreecommitdiff
path: root/ext/nas/nassink.c
blob: 6f5a003eea793c36dbf24ed264c51884f6280d91 (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/* GStreamer
 * Copyright (C) <2003> Laurent Vivier <Laurent.Vivier@bull.net>
 * Copyright (C) <2004> Arwed v. Merkatz <v.merkatz@gmx.net>
 *
 * Based on esdsink.c:
 * Copyright (C) <2001> Richard Boulton <richard-gst@tartarus.org>
 *
 * 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.
 */

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

#include <gst/gst.h>
#include <string.h>
#include <audio/audiolib.h>
#include <audio/soundlib.h>
#include "nassink.h"

#define NAS_SOUND_PORT_DURATION (2)

GST_DEBUG_CATEGORY_STATIC (nas_debug);
#define GST_CAT_DEFAULT nas_debug

enum
{
  ARG_0,
  ARG_MUTE,
  ARG_HOST
};

#define DEFAULT_MUTE  FALSE
#define DEFAULT_HOST  NULL

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

static void gst_nas_sink_finalize (GObject * object);

static gboolean gst_nas_sink_open (GstAudioSink * sink);
static gboolean gst_nas_sink_close (GstAudioSink * sink);
static gboolean gst_nas_sink_prepare (GstAudioSink * sink,
    GstRingBufferSpec * spec);
static gboolean gst_nas_sink_unprepare (GstAudioSink * sink);
static guint gst_nas_sink_write (GstAudioSink * asink, gpointer data,
    guint length);
static guint gst_nas_sink_delay (GstAudioSink * asink);
static void gst_nas_sink_reset (GstAudioSink * asink);
static GstCaps *gst_nas_sink_getcaps (GstBaseSink * pad);

static void gst_nas_sink_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec);
static void gst_nas_sink_get_property (GObject * object, guint prop_id,
    GValue * value, GParamSpec * pspec);

static void NAS_flush (GstNasSink * sink);
static void NAS_sendData (GstNasSink * sink, AuUint32 numBytes);
static AuBool NAS_EventHandler (AuServer * aud, AuEvent * ev,
    AuEventHandlerRec * handler);
static AuDeviceID NAS_getDevice (AuServer * aud, int numTracks);
static int NAS_createFlow (GstNasSink * sink, GstRingBufferSpec * spec);

static const GstElementDetails nas_sink_details =
GST_ELEMENT_DETAILS ("NAS audio sink",
    "Sink/Audio",
    "Plays audio to a Network Audio Server",
    "Laurent Vivier <Laurent.Vivier@bull.net>, "
    "Arwed v. Merkatz <v.merkatz@gmx.net>");

GST_BOILERPLATE (GstNasSink, gst_nas_sink, GstAudioSink, GST_TYPE_AUDIO_SINK);

static void
gst_nas_sink_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&sink_factory));
  gst_element_class_set_details (element_class, &nas_sink_details);
}

static void
gst_nas_sink_class_init (GstNasSinkClass * klass)
{
  GObjectClass *gobject_class;
  GstBaseSinkClass *gstbasesink_class;
  GstAudioSinkClass *gstaudiosink_class;

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

  gobject_class->set_property = gst_nas_sink_set_property;
  gobject_class->get_property = gst_nas_sink_get_property;
  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_nas_sink_finalize);

  g_object_class_install_property (gobject_class, ARG_MUTE,
      g_param_spec_boolean ("mute", "mute", "Whether to mute playback",
          DEFAULT_MUTE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
  g_object_class_install_property (gobject_class, ARG_HOST,
      g_param_spec_string ("host", "host",
          "host running the NAS daemon (name of X/Terminal, default is "
          "$AUDIOSERVER or $DISPLAY)", DEFAULT_HOST,
          G_PARAM_READWRITE | G_PARAM_CONSTRUCT));

  gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_nas_sink_getcaps);

  gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_nas_sink_open);
  gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_nas_sink_close);
  gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_nas_sink_prepare);
  gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_nas_sink_unprepare);
  gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_nas_sink_write);
  gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_nas_sink_delay);
  gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_nas_sink_reset);
}

static void
gst_nas_sink_init (GstNasSink * nassink, GstNasSinkClass * klass)
{
  /* properties will automatically be set to their default values */
  nassink->audio = NULL;
  nassink->flow = AuNone;
  nassink->need_data = 0;
}

static void
gst_nas_sink_finalize (GObject * object)
{
  GstNasSink *nassink = GST_NAS_SINK (object);

  g_free (nassink->host);
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static GstCaps *
gst_nas_sink_getcaps (GstBaseSink * bsink)
{
  GstNasSink *nassink = GST_NAS_SINK (bsink);
  const GstCaps *templatecaps;
  AuServer *server;
  GstCaps *fixated, *caps;
  int i;

  server = nassink->audio;

  templatecaps = gst_static_pad_template_get_caps (&sink_factory);

  if (server == NULL)
    return gst_caps_copy (templatecaps);

  fixated = gst_caps_copy (templatecaps);
  for (i = 0; i < gst_caps_get_size (fixated); i++) {
    GstStructure *structure;
    gint min, max;

    min = AuServerMinSampleRate (server);
    max = AuServerMaxSampleRate (server);

    structure = gst_caps_get_structure (fixated, i);

    if (min == max)
      gst_structure_set (structure, "rate", G_TYPE_INT, max, NULL);
    else
      gst_structure_set (structure, "rate", GST_TYPE_INT_RANGE, min, max, NULL);
  }

  caps = gst_caps_intersect (fixated, templatecaps);
  gst_caps_unref (fixated);

  if (nassink->audio == NULL)
    AuCloseServer (server);

  return caps;
}

static gboolean
gst_nas_sink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
{
  GstNasSink *sink = GST_NAS_SINK (asink);

  /*spec->bytes_per_sample = sink->rate * NAS_SOUND_PORT_DURATION; */
  /*spec->bytes_per_sample = (spec->width / 8) * spec->channels; */
  memset (spec->silence_sample, 0, spec->bytes_per_sample);
  GST_DEBUG_OBJECT (sink, "Sample %d", spec->bytes_per_sample);

  if (sink->audio == NULL)
    return TRUE;

  if (sink->flow != AuNone) {
    GST_DEBUG_OBJECT (sink, "flushing buffer");
    NAS_flush (sink);
    AuStopFlow (sink->audio, sink->flow, NULL);
    AuReleaseScratchFlow (sink->audio, sink->flow, NULL);
    sink->flow = AuNone;
  }

  return NAS_createFlow (sink, spec);
}

static gboolean
gst_nas_sink_unprepare (GstAudioSink * asink)
{
  return TRUE;
}

static guint
gst_nas_sink_delay (GstAudioSink * asink)
{
  GST_DEBUG_OBJECT (asink, "nas_sink_delay");
  return 0;
}

static void
gst_nas_sink_reset (GstAudioSink * asink)
{
  GstNasSink *sink = GST_NAS_SINK (asink);

  GST_DEBUG_OBJECT (sink, "reset");

  NAS_flush (sink);
}

static guint
gst_nas_sink_write (GstAudioSink * asink, gpointer data, guint length)
{
  GstNasSink *nassink = GST_NAS_SINK (asink);
  int used = 0;

  NAS_flush (nassink);
  if (!nassink->mute && nassink->audio != NULL && nassink->flow != AuNone) {

    if (nassink->need_data == 0)
      return 0;

    used = nassink->need_data > length ? length : nassink->need_data;
    AuWriteElement (nassink->audio, nassink->flow, 0, used, data, AuFalse,
        NULL);
    nassink->need_data -= used;
    if (used == length)
      AuSync (nassink->audio, AuFalse);
  } else
    used = length;
  return used;
}

static void
gst_nas_sink_set_property (GObject * object, guint prop_id,
    const GValue * value, GParamSpec * pspec)
{
  GstNasSink *nassink;

  nassink = GST_NAS_SINK (object);

  switch (prop_id) {
    case ARG_MUTE:
      nassink->mute = g_value_get_boolean (value);
      break;
    case ARG_HOST:
      g_free (nassink->host);
      nassink->host = g_value_dup_string (value);
      if (nassink->host == NULL)
        nassink->host = g_strdup (g_getenv ("AUDIOSERVER"));
      if (nassink->host == NULL)
        nassink->host = g_strdup (g_getenv ("DISPLAY"));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static void
gst_nas_sink_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
  GstNasSink *nassink;

  nassink = GST_NAS_SINK (object);

  switch (prop_id) {
    case ARG_MUTE:
      g_value_set_boolean (value, nassink->mute);
      break;
    case ARG_HOST:
      g_value_set_string (value, nassink->host);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

static gboolean
gst_nas_sink_open (GstAudioSink * asink)
{
  GstNasSink *sink = GST_NAS_SINK (asink);

  GST_DEBUG_OBJECT (sink, "opening, host = '%s'", GST_STR_NULL (sink->host));

  /* Open Server */
  sink->audio = AuOpenServer (sink->host, 0, NULL, 0, NULL, NULL);
  if (sink->audio == NULL) {
    GST_DEBUG_OBJECT (sink, "opening failed");
    return FALSE;
  }
  sink->flow = AuNone;
  sink->need_data = 0;

  /* Start a flow */
  GST_DEBUG_OBJECT (asink, "opened audio device");
  return TRUE;
}

static gboolean
gst_nas_sink_close (GstAudioSink * asink)
{
  GstNasSink *sink = GST_NAS_SINK (asink);

  if (sink->audio == NULL)
    return TRUE;

  if (sink->flow != AuNone) {
    NAS_flush (sink);

    AuStopFlow (sink->audio, sink->flow, NULL);
    AuReleaseScratchFlow (sink->audio, sink->flow, NULL);
    sink->flow = AuNone;
  }

  sink->need_data = 0;

  AuCloseServer (sink->audio);
  sink->audio = NULL;

  GST_DEBUG_OBJECT (sink, "closed audio device");
  return TRUE;
}

static void
NAS_flush (GstNasSink * sink)
{
  AuEvent ev;

  AuNextEvent (sink->audio, AuTrue, &ev);
  AuDispatchEvent (sink->audio, &ev);
}

static void
NAS_sendData (GstNasSink * sink, AuUint32 numBytes)
{
  sink->need_data += numBytes;
  return;
}

static AuBool
NAS_EventHandler (AuServer * aud, AuEvent * ev, AuEventHandlerRec * handler)
{
  GstNasSink *sink = (GstNasSink *) handler->data;
  AuElementNotifyEvent *notify;

  switch (ev->type) {

    case AuEventTypeElementNotify:

      notify = (AuElementNotifyEvent *) ev;

      switch (notify->kind) {

        case AuElementNotifyKindLowWater:
          NAS_sendData (sink, notify->num_bytes);
          break;

        case AuElementNotifyKindState:

          switch (notify->cur_state) {

            case AuStateStop:

              if (sink->flow != AuNone) {
                if (notify->reason == AuReasonEOF)
                  AuStopFlow (handler->aud, sink->flow, NULL);
                AuReleaseScratchFlow (handler->aud, sink->flow, NULL);
                sink->flow = AuNone;
              }
              AuUnregisterEventHandler (handler->aud, handler);
              break;

            case AuStatePause:

              switch (notify->reason) {
                case AuReasonUnderrun:
                case AuReasonOverrun:
                case AuReasonEOF:
                case AuReasonWatermark:

                  NAS_sendData (sink, notify->num_bytes);

                  break;

                case AuReasonHardware:

                  if (AuSoundRestartHardwarePauses)
                    AuStartFlow (handler->aud, sink->flow, NULL);
                  else
                    AuStopFlow (handler->aud, sink->flow, NULL);

                  break;
              }
              break;
          }
          break;
      }
      break;
  }

  return AuTrue;
}

static AuDeviceID
NAS_getDevice (AuServer * aud, int numTracks)
{
  int i;

  for (i = 0; i < AuServerNumDevices (aud); i++) {
    if ((AuDeviceKind (AuServerDevice (aud, i))
            == AuComponentKindPhysicalOutput) &&
        (AuDeviceNumTracks (AuServerDevice (aud, i)) == numTracks)) {

      return AuDeviceIdentifier (AuServerDevice (aud, i));

    }
  }

  return AuNone;
}

static gint
gst_nas_sink_sink_get_format (const GstRingBufferSpec * spec)
{
  gint result;

  switch (spec->format) {
    case GST_U8:
      result = AuFormatLinearUnsigned8;
      break;
    case GST_S8:
      result = AuFormatLinearSigned8;
      break;
    case GST_S16_LE:
      result = AuFormatLinearSigned16LSB;
      break;
    case GST_S16_BE:
      result = AuFormatLinearSigned16MSB;
      break;
    case GST_U16_LE:
      result = AuFormatLinearUnsigned16LSB;
      break;
    case GST_U16_BE:
      result = AuFormatLinearUnsigned16MSB;
      break;
    default:
      result = 0;
      break;
  }
  return result;
}

static gboolean
NAS_createFlow (GstNasSink * sink, GstRingBufferSpec * spec)
{
  AuElement elements[2];
  AuUint32 buf_samples;
  unsigned char format;

  format = gst_nas_sink_sink_get_format (spec);
  if (format == 0) {
    GST_ELEMENT_ERROR (sink, RESOURCE, SETTINGS, (NULL),
        ("Unable to get format %d", spec->format));
    return FALSE;
  }
  GST_DEBUG_OBJECT (sink, "Format: %d %d\n", spec->format, format);

  sink->flow = AuGetScratchFlow (sink->audio, NULL);
  if (sink->flow == 0) {
    GST_DEBUG_OBJECT (sink, "couldn't get flow");
    return FALSE;
  }

  /* free old Elements and reconnet to server, needed to change samplerate */
  {
    AuBool clocked;
    int num_elements;
    AuStatus status;
    AuElement *oldelems;

    oldelems =
        AuGetElements (sink->audio, sink->flow, &clocked, &num_elements,
        &status);
    if (num_elements > 0) {
      GST_DEBUG_OBJECT (sink, "GetElements status: %i", status);
      if (oldelems)
        AuFreeElements (sink->audio, num_elements, oldelems);
      gst_nas_sink_close (GST_AUDIO_SINK (sink));
      gst_nas_sink_open (GST_AUDIO_SINK (sink));
      sink->flow = AuGetScratchFlow (sink->audio, NULL);
      if (sink->flow == 0) {
        GST_DEBUG_OBJECT (sink, "couldn't get flow");
        return FALSE;
      }
    }
  }

  /* free old Elements and reconnet to server, needed to change samplerate */
  {
    AuBool clocked;
    int num_elements;
    AuStatus status;
    AuElement *oldelems;

    oldelems =
        AuGetElements (sink->audio, sink->flow, &clocked, &num_elements,
        &status);
    if (num_elements > 0) {
      GST_DEBUG_OBJECT (sink, "GetElements status: %i", status);
      if (oldelems)
        AuFreeElements (sink->audio, num_elements, oldelems);
      gst_nas_sink_close (GST_AUDIO_SINK (sink));
      gst_nas_sink_open (GST_AUDIO_SINK (sink));
      sink->flow = AuGetScratchFlow (sink->audio, NULL);
      if (sink->flow == 0) {
        GST_DEBUG_OBJECT (sink, "couldn't get flow");
        return FALSE;
      }
    }
  }

  buf_samples = spec->rate * NAS_SOUND_PORT_DURATION;
  /*
     spec->segsize = gst_util_uint64_scale (buf_samples * spec->bytes_per_sample,
     spec->latency_time, GST_SECOND / GST_USECOND);
     spec->segsize -= spec->segsize % spec->bytes_per_sample;
     spec->segtotal = spec->buffer_time / spec->latency_time;
   */
  spec->segsize = buf_samples * spec->bytes_per_sample;
  spec->segtotal = 1;

  GST_DEBUG_OBJECT (sink, "Rate %d Format %d tracks %d bufs %d %d/%d w %d",
      spec->rate, format, spec->channels, (gint) buf_samples, spec->segsize,
      spec->segtotal, spec->width);
  AuMakeElementImportClient (&elements[0],      /* element */
      spec->rate,               /* rate */
      format,                   /* format */
      spec->channels,           /* number of tracks */
      AuTrue,                   /* discart */
      buf_samples,              /* max samples */
      (AuUint32) (buf_samples / 100 * AuSoundPortLowWaterMark),
      /* low water mark */
      0,                        /* num actions */
      NULL);

  sink->device = NAS_getDevice (sink->audio, spec->channels);
  if (sink->device == AuNone) {
    GST_DEBUG_OBJECT (sink, "no device with %i tracks found", spec->channels);
    return FALSE;
  }

  AuMakeElementExportDevice (&elements[1],      /* element */
      0,                        /* input */
      sink->device,             /* device */
      spec->rate,               /* rate */
      AuUnlimitedSamples,       /* num samples */
      0,                        /* num actions */
      NULL);                    /* actions */

  AuSetElements (sink->audio,   /* server */
      sink->flow,               /* flow ID */
      AuTrue,                   /* clocked */
      2,                        /* num elements */
      elements,                 /* elements */
      NULL);

  AuRegisterEventHandler (sink->audio,  /* server */
      AuEventHandlerIDMask,     /* value mask */
      0,                        /* type */
      sink->flow,               /* flow ID */
      NAS_EventHandler,         /* callback */
      (AuPointer) sink);        /* data */

  AuStartFlow (sink->audio, sink->flow, NULL);

  return TRUE;
}

static gboolean
plugin_init (GstPlugin * plugin)
{
  GST_DEBUG_CATEGORY_INIT (nas_debug, "NAS", 0, NULL);

  if (!gst_element_register (plugin, "nassink", GST_RANK_NONE,
          GST_TYPE_NAS_SINK)) {
    return FALSE;
  }

  return TRUE;
}

GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
    GST_VERSION_MINOR,
    "nas",
    "NAS (Network Audio System) support for GStreamer",
    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);