summaryrefslogtreecommitdiff
path: root/ext/audiofile/gstafsink.c
blob: 22c66cb3c1e70bd3741576360cd4eda31e9d8211 (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
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wtay@chello.be>
 *
 * gstafsink.c: 
 *
 * 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., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */


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

#include "gst/gst-i18n-plugin.h"

#include <gst/gst.h>
#include <string.h>
#include <errno.h>

#include "gstafsink.h"

/* AFSink signals and args */
enum
{
  /* FILL ME */
  SIGNAL_HANDOFF,
  LAST_SIGNAL
};

enum
{
  ARG_0,
  ARG_TYPE,
  ARG_OUTPUT_ENDIANNESS,
  ARG_LOCATION
};

/* added a sink factory function to force audio/raw MIME type */
/* I think the caps can be broader, we need to change that somehow */
static GstStaticPadTemplate afsink_sink_factory =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("audio/x-raw-int, "
        "rate = (int) [ 1, MAX ], "
        "channels = (int) [ 1, 2 ], "
        "endianness = (int) BYTE_ORDER, "
        "width = (int) { 8, 16 }, "
        "depth = (int) { 8, 16 }, " "signed = (boolean) { true, false }")
    );

/* we use an enum for the output type arg */

#define GST_TYPE_AFSINK_TYPES (gst_afsink_types_get_type())
/* FIXME: fix the string ints to be string-converted from the audiofile.h types */
static GType
gst_afsink_types_get_type (void)
{
  static GType afsink_types_type = 0;
  static const GEnumValue afsink_types[] = {
    {AF_FILE_RAWDATA, "0", "raw PCM"},
    {AF_FILE_AIFFC, "1", "AIFFC"},
    {AF_FILE_AIFF, "2", "AIFF"},
    {AF_FILE_NEXTSND, "3", "Next/SND"},
    {AF_FILE_WAVE, "4", "Wave"},
    {0, NULL, NULL},
  };

  if (!afsink_types_type) {
    afsink_types_type =
        g_enum_register_static ("GstAudiosinkTypes", afsink_types);
  }
  return afsink_types_type;
}

static void gst_afsink_base_init (gpointer g_class);
static void gst_afsink_class_init (GstAFSinkClass * klass);
static void gst_afsink_init (GstAFSink * afsink);

static gboolean gst_afsink_open_file (GstAFSink * sink);
static void gst_afsink_close_file (GstAFSink * sink);

static void gst_afsink_chain (GstPad * pad, GstData * _data);

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

static gboolean gst_afsink_handle_event (GstPad * pad, GstEvent * event);

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

static GstElementClass *parent_class = NULL;
static guint gst_afsink_signals[LAST_SIGNAL] = { 0 };

GType
gst_afsink_get_type (void)
{
  static GType afsink_type = 0;

  if (!afsink_type) {
    static const GTypeInfo afsink_info = {
      sizeof (GstAFSinkClass),
      gst_afsink_base_init,
      NULL,
      (GClassInitFunc) gst_afsink_class_init,
      NULL,
      NULL,
      sizeof (GstAFSink),
      0,
      (GInstanceInitFunc) gst_afsink_init,
    };

    afsink_type =
        g_type_register_static (GST_TYPE_ELEMENT, "GstAFSink", &afsink_info, 0);
  }
  return afsink_type;
}

static void
gst_afsink_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 (&afsink_sink_factory));
  gst_element_class_set_static_metadata (element_class, "Audiofile sink",
      "Sink/Audio",
      "Write audio streams to disk using libaudiofile",
      "Thomas Vander Stichele <thomas@apestaart.org>");
}

static void
gst_afsink_class_init (GstAFSinkClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;

  parent_class = g_type_class_peek_parent (klass);

  gst_element_class_install_std_props (GST_ELEMENT_CLASS (klass),
      "location", ARG_LOCATION, G_PARAM_READWRITE, NULL);

  /* FIXME: add long property descriptions */
  g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_TYPE,
      g_param_spec_enum ("type", "type", "type", GST_TYPE_AFSINK_TYPES, 0,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
  g_object_class_install_property (G_OBJECT_CLASS (klass),
      ARG_OUTPUT_ENDIANNESS, g_param_spec_int ("endianness", "endianness",
          "endianness", G_MININT, G_MAXINT, 0,
          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));

  gst_afsink_signals[SIGNAL_HANDOFF] =
      g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
      G_STRUCT_OFFSET (GstAFSinkClass, handoff), NULL, NULL,
      g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);


  gobject_class->set_property = gst_afsink_set_property;
  gobject_class->get_property = gst_afsink_get_property;

  gstelement_class->change_state = gst_afsink_change_state;
}

static void
gst_afsink_init (GstAFSink * afsink)
{
  /* GstPad *pad;   this is now done in the struct */

  afsink->sinkpad =
      gst_pad_new_from_template (gst_element_get_pad_template (GST_ELEMENT
          (afsink), "sink"), "sink");
  gst_element_add_pad (GST_ELEMENT (afsink), afsink->sinkpad);

  gst_pad_set_chain_function (afsink->sinkpad, gst_afsink_chain);

  afsink->filename = NULL;
  afsink->file = NULL;
  /* default values, should never be needed */
  afsink->channels = 2;
  afsink->width = 16;
  afsink->rate = 44100;
  afsink->type = AF_FILE_WAVE;
  afsink->endianness_data = 1234;
  afsink->endianness_wanted = 1234;
}

static void
gst_afsink_set_property (GObject * object, guint prop_id, const GValue * value,
    GParamSpec * pspec)
{
  GstAFSink *sink;

  sink = GST_AFSINK (object);

  switch (prop_id) {
    case ARG_LOCATION:
      /* the element must be stopped or paused in order to do this */
      g_return_if_fail ((GST_STATE (sink) < GST_STATE_PLAYING)
          || (GST_STATE (sink) == GST_STATE_PAUSED));
      if (sink->filename)
        g_free (sink->filename);
      sink->filename = g_strdup (g_value_get_string (value));
      if ((GST_STATE (sink) == GST_STATE_PAUSED)
          && (sink->filename != NULL)) {
        gst_afsink_close_file (sink);
        gst_afsink_open_file (sink);
      }

      break;
    case ARG_TYPE:
      sink->type = g_value_get_enum (value);
      break;
    case ARG_OUTPUT_ENDIANNESS:
    {
      int end = g_value_get_int (value);

      if (end == 1234 || end == 4321)
        sink->endianness_output = end;
    }
      break;
    default:
      break;
  }
}

static void
gst_afsink_get_property (GObject * object, guint prop_id, GValue * value,
    GParamSpec * pspec)
{
  GstAFSink *sink;

  g_return_if_fail (GST_IS_AFSINK (object));

  sink = GST_AFSINK (object);

  switch (prop_id) {
    case ARG_LOCATION:
      g_value_set_string (value, sink->filename);
      break;
    case ARG_TYPE:
      g_value_set_enum (value, sink->type);
      break;
    case ARG_OUTPUT_ENDIANNESS:
      g_value_set_int (value, sink->endianness_output);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

gboolean
gst_afsink_plugin_init (GstPlugin * plugin)
{
  if (!gst_element_register (plugin, "afsink", GST_RANK_NONE, GST_TYPE_AFSINK))
    return FALSE;
#ifdef ENABLE_NLS
  setlocale (LC_ALL, "");
  bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
#endif /* ENABLE_NLS */

  return TRUE;
}

/* this is where we open the audiofile */
static gboolean
gst_afsink_open_file (GstAFSink * sink)
{
  AFfilesetup outfilesetup;
  const GstCaps *caps;
  GstStructure *structure;
  int sample_format;            /* audiofile's sample format, look in audiofile.h */
  int byte_order = 0;           /* audiofile's byte order defines */

  g_return_val_if_fail (!GST_OBJECT_FLAG_IS_SET (sink, GST_AFSINK_OPEN), FALSE);

  /* get the audio parameters */
  g_return_val_if_fail (GST_IS_PAD (sink->sinkpad), FALSE);
  caps = GST_PAD_CAPS (sink->sinkpad);

  if (caps == NULL) {
    g_critical ("gstafsink chain : Could not get caps of pad !\n");
  } else {
    structure = gst_caps_get_structure (caps, 0);
    gst_structure_get_int (structure, "channels", &sink->channels);
    gst_structure_get_int (structure, "width", &sink->width);
    gst_structure_get_int (structure, "rate", &sink->rate);
    gst_structure_get_boolean (structure, "signed", &sink->is_signed);
    gst_structure_get_int (structure, "endianness", &sink->endianness_data);
  }
  GST_DEBUG ("channels %d, width %d, rate %d, signed %s",
      sink->channels, sink->width, sink->rate, sink->is_signed ? "yes" : "no");
  GST_DEBUG ("endianness: data %d, output %d",
      sink->endianness_data, sink->endianness_output);
  /* setup the output file */
  if (sink->is_signed)
    sample_format = AF_SAMPFMT_TWOSCOMP;
  else
    sample_format = AF_SAMPFMT_UNSIGNED;
  /* FIXME : this check didn't seem to work, so let the output endianness be set */
  /*
     if (sink->endianness_data == sink->endianness_wanted)
     byte_order = AF_BYTEORDER_LITTLEENDIAN;
     else
     byte_order = AF_BYTEORDER_BIGENDIAN;
   */
  if (sink->endianness_output == 1234)
    byte_order = AF_BYTEORDER_LITTLEENDIAN;
  else
    byte_order = AF_BYTEORDER_BIGENDIAN;

  outfilesetup = afNewFileSetup ();
  afInitFileFormat (outfilesetup, sink->type);
  afInitChannels (outfilesetup, AF_DEFAULT_TRACK, sink->channels);
  afInitRate (outfilesetup, AF_DEFAULT_TRACK, sink->rate);
  afInitSampleFormat (outfilesetup, AF_DEFAULT_TRACK,
      sample_format, sink->width);

  /* open it */
  sink->file = afOpenFile (sink->filename, "w", outfilesetup);
  if (sink->file == AF_NULL_FILEHANDLE) {
    GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE,
        (_("Could not open file \"%s\" for writing."), sink->filename),
        ("system error: %s", strerror (errno)));
    return FALSE;
  }

  afFreeFileSetup (outfilesetup);
/*  afSetVirtualByteOrder (sink->file, AF_DEFAULT_TRACK, byte_order); */

  GST_OBJECT_FLAG_SET (sink, GST_AFSINK_OPEN);

  return TRUE;
}

static void
gst_afsink_close_file (GstAFSink * sink)
{
/*  g_print ("DEBUG: closing sinkfile...\n"); */
  g_return_if_fail (GST_OBJECT_FLAG_IS_SET (sink, GST_AFSINK_OPEN));
/*  g_print ("DEBUG: past flag test\n"); */
/*  if (fclose (sink->file) != 0) */
  if (afCloseFile (sink->file) != 0) {
    GST_ELEMENT_ERROR (sink, RESOURCE, CLOSE,
        (_("Error closing file \"%s\"."), sink->filename), GST_ERROR_SYSTEM);
  } else {
    GST_OBJECT_FLAG_UNSET (sink, GST_AFSINK_OPEN);
  }
}

/**
 * gst_afsink_chain:
 * @pad: the pad this afsink is connected to
 * @buf: the buffer that has to be absorbed
 *
 * take the buffer from the pad and write to file if it's open
 */
static void
gst_afsink_chain (GstPad * pad, GstData * _data)
{
  GstBuffer *buf;
  GstAFSink *afsink;
  int ret = 0;

  g_return_if_fail (pad != NULL);
  g_return_if_fail (GST_IS_PAD (pad));

  if (GST_IS_EVENT (_data)) {
    gst_afsink_handle_event (pad, GST_EVENT (_data));
    return;
  }

  buf = GST_BUFFER (_data);
  afsink = GST_AFSINK (gst_pad_get_parent (pad));
/* we use audiofile now
  if (GST_OBJECT_FLAG_IS_SET (afsink, GST_AFSINK_OPEN))
  {
    bytes_written = fwrite (GST_BUFFER_DATA (buf), 1, GST_BUFFER_SIZE (buf), afsink->file);
    if (bytes_written < GST_BUFFER_SIZE (buf))
    {
      printf ("afsink : Warning : %d bytes should be written, only %d bytes written\n",
                  GST_BUFFER_SIZE (buf), bytes_written);
    }
  }
*/

  if (!GST_OBJECT_FLAG_IS_SET (afsink, GST_AFSINK_OPEN)) {
    /* it's not open yet, open it */
    if (!gst_afsink_open_file (afsink))
      g_print ("WARNING: gstafsink: can't open file !\n");
/*        return FALSE;    Can't return value */
  }

  if (GST_OBJECT_FLAG_IS_SET (afsink, GST_AFSINK_OPEN)) {
    int frameCount = 0;

    frameCount =
        GST_BUFFER_SIZE (buf) / ((afsink->width / 8) * afsink->channels);
    /*   g_print ("DEBUG: writing %d frames ", frameCount); */
    ret = afWriteFrames (afsink->file, AF_DEFAULT_TRACK,
        GST_BUFFER_DATA (buf), frameCount);
    if (ret == AF_BAD_WRITE || ret == AF_BAD_LSEEK) {
      printf ("afsink : Warning : afWriteFrames returned an error (%d)\n", ret);
    }
  }

  gst_buffer_unref (buf);

  g_signal_emit (G_OBJECT (afsink), gst_afsink_signals[SIGNAL_HANDOFF], 0);
}

static GstStateChangeReturn
gst_afsink_change_state (GstElement * element, GstStateChange transition)
{
  g_return_val_if_fail (GST_IS_AFSINK (element), GST_STATE_CHANGE_FAILURE);

  /* if going to NULL? then close the file */
  if (GST_STATE_PENDING (element) == GST_STATE_NULL) {
/*    printf ("DEBUG: afsink state change: null pending\n"); */
    if (GST_OBJECT_FLAG_IS_SET (element, GST_AFSINK_OPEN)) {
/*      g_print ("DEBUG: trying to close the sink file\n"); */
      gst_afsink_close_file (GST_AFSINK (element));
    }
  }
/*

  else
  this has been moved to the chain function, since it's only then that 
 the caps are set and can be known 
 {
    g_print ("DEBUG: it's not going to null\n"); 
    if (!GST_OBJECT_FLAG_IS_SET (element, GST_AFSINK_OPEN)) 
    {
      g_print ("DEBUG: GST_AFSINK_OPEN not set\n"); 
      if (!gst_afsink_open_file (GST_AFSINK (element)))
      {
        g_print ("DEBUG: element tries to open file\n"); 
        return GST_STATE_CHANGE_FAILURE;
      }
    }
  }
*/

  if (GST_ELEMENT_CLASS (parent_class)->change_state)
    return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);

  return GST_STATE_CHANGE_SUCCESS;
}

/* this function was copied from sinesrc */

static gboolean
gst_afsink_handle_event (GstPad * pad, GstEvent * event)
{
  GstAFSink *afsink;

  afsink = GST_AFSINK (gst_pad_get_parent (pad));
  GST_DEBUG ("DEBUG: afsink: got event");

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_EOS:
      gst_afsink_close_file (afsink);
      break;
    default:
      break;
  }

  gst_pad_event_default (pad, event);

  return TRUE;
}