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
|
/* GStreamer libsndfile plugin
* Copyright (C) 2007 Andy Wingo <wingo at pobox dot com>
*
* 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/audio/audio.h>
#include <gst/gst-i18n-plugin.h>
#include "gstsfsink.h"
enum
{
PROP_0,
PROP_LOCATION,
PROP_MAJOR_TYPE,
PROP_MINOR_TYPE,
PROP_BUFFER_FRAMES
};
#define DEFAULT_BUFFER_FRAMES (256)
static GstStaticPadTemplate sf_sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
GST_PAD_ALWAYS,
GST_STATIC_CAPS ("audio/x-raw-float, "
"rate = (int) [ 1, MAX ], "
"channels = (int) [ 1, MAX ], "
"endianness = (int) BYTE_ORDER, "
"width = (int) 32; "
"audio/x-raw-int, "
"rate = (int) [ 1, MAX ], "
"channels = (int) [ 1, MAX ], "
"endianness = (int) BYTE_ORDER, "
"width = (int) {16, 32}, "
"depth = (int) {16, 32}, " "signed = (boolean) true")
);
GST_BOILERPLATE (GstSFSink, gst_sf_sink, GstBaseSink, GST_TYPE_BASE_SINK);
static void gst_sf_sink_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_sf_sink_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
static gboolean gst_sf_sink_start (GstBaseSink * bsink);
static gboolean gst_sf_sink_stop (GstBaseSink * bsink);
static void gst_sf_sink_fixate (GstBaseSink * bsink, GstCaps * caps);
static gboolean gst_sf_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
static gboolean gst_sf_sink_activate_pull (GstBaseSink * bsink,
gboolean active);
static GstFlowReturn gst_sf_sink_render (GstBaseSink * bsink,
GstBuffer * buffer);
static gboolean gst_sf_sink_event (GstBaseSink * bsink, GstEvent * event);
static gboolean gst_sf_sink_open_file (GstSFSink * this);
static void gst_sf_sink_close_file (GstSFSink * this);
GST_DEBUG_CATEGORY_STATIC (gst_sf_debug);
#define GST_CAT_DEFAULT gst_sf_debug
static void
gst_sf_sink_base_init (gpointer g_class)
{
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
GST_DEBUG_CATEGORY_INIT (gst_sf_debug, "sfsink", 0, "sfsink element");
gst_element_class_add_pad_template (element_class,
gst_static_pad_template_get (&sf_sink_factory));
gst_element_class_set_details_simple (element_class, "Sndfile sink",
"Sink/Audio",
"Write audio streams to disk using libsndfile",
"Andy Wingo <wingo at pobox dot com>");
}
static void
gst_sf_sink_class_init (GstSFSinkClass * klass)
{
GObjectClass *gobject_class;
GstBaseSinkClass *basesink_class;
GParamSpec *pspec;
gobject_class = (GObjectClass *) klass;
basesink_class = (GstBaseSinkClass *) klass;
gobject_class->set_property = gst_sf_sink_set_property;
gobject_class->get_property = gst_sf_sink_get_property;
g_object_class_install_property (gobject_class, PROP_LOCATION,
g_param_spec_string ("location", "File Location",
"Location of the file to write", NULL,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
pspec = g_param_spec_enum
("major-type", "Major type", "Major output type", GST_TYPE_SF_MAJOR_TYPES,
SF_FORMAT_WAV,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (gobject_class, PROP_MAJOR_TYPE, pspec);
pspec = g_param_spec_enum
("minor-type", "Minor type", "Minor output type", GST_TYPE_SF_MINOR_TYPES,
SF_FORMAT_FLOAT,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (gobject_class, PROP_MINOR_TYPE, pspec);
pspec = g_param_spec_int
("buffer-frames", "Buffer frames",
"Number of frames per buffer, in pull mode", 1, G_MAXINT,
DEFAULT_BUFFER_FRAMES,
G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
g_object_class_install_property (gobject_class, PROP_BUFFER_FRAMES, pspec);
basesink_class->get_times = NULL;
basesink_class->start = GST_DEBUG_FUNCPTR (gst_sf_sink_start);
basesink_class->stop = GST_DEBUG_FUNCPTR (gst_sf_sink_stop);
basesink_class->fixate = GST_DEBUG_FUNCPTR (gst_sf_sink_fixate);
basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_sf_sink_set_caps);
basesink_class->activate_pull = GST_DEBUG_FUNCPTR (gst_sf_sink_activate_pull);
basesink_class->render = GST_DEBUG_FUNCPTR (gst_sf_sink_render);
basesink_class->event = GST_DEBUG_FUNCPTR (gst_sf_sink_event);
}
static void
gst_sf_sink_init (GstSFSink * this, GstSFSinkClass * klass)
{
GST_BASE_SINK (this)->can_activate_pull = TRUE;
}
static void
gst_sf_sink_set_location (GstSFSink * this, const gchar * location)
{
if (this->file)
goto was_open;
if (this->location)
g_free (this->location);
this->location = location ? g_strdup (location) : NULL;
return;
was_open:
{
g_warning ("Changing the `location' property on sfsink when "
"a file is open not supported.");
return;
}
}
static void
gst_sf_sink_set_property (GObject * object, guint prop_id, const GValue * value,
GParamSpec * pspec)
{
GstSFSink *this = GST_SF_SINK (object);
switch (prop_id) {
case PROP_LOCATION:
gst_sf_sink_set_location (this, g_value_get_string (value));
break;
case PROP_MAJOR_TYPE:
this->format_major = g_value_get_enum (value);
break;
case PROP_MINOR_TYPE:
this->format_subtype = g_value_get_enum (value);
break;
case PROP_BUFFER_FRAMES:
this->buffer_frames = g_value_get_int (value);
break;
default:
break;
}
}
static void
gst_sf_sink_get_property (GObject * object, guint prop_id, GValue * value,
GParamSpec * pspec)
{
GstSFSink *this = GST_SF_SINK (object);
switch (prop_id) {
case PROP_LOCATION:
g_value_set_string (value, this->location);
break;
case PROP_MAJOR_TYPE:
g_value_set_enum (value, this->format_major);
break;
case PROP_MINOR_TYPE:
g_value_set_enum (value, this->format_subtype);
break;
case PROP_BUFFER_FRAMES:
g_value_set_int (value, this->buffer_frames);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean
gst_sf_sink_start (GstBaseSink * bsink)
{
/* pass */
return TRUE;
}
static gboolean
gst_sf_sink_stop (GstBaseSink * bsink)
{
GstSFSink *this = GST_SF_SINK (bsink);
if (this->file)
gst_sf_sink_close_file (this);
return TRUE;
}
static gboolean
gst_sf_sink_open_file (GstSFSink * this)
{
int mode;
SF_INFO info;
g_return_val_if_fail (this->file == NULL, FALSE);
g_return_val_if_fail (this->rate > 0, FALSE);
g_return_val_if_fail (this->channels > 0, FALSE);
if (!this->location)
goto no_filename;
mode = SFM_WRITE;
this->format = this->format_major | this->format_subtype;
info.samplerate = this->rate;
info.channels = this->channels;
info.format = this->format;
GST_INFO_OBJECT (this, "Opening %s with rate %d, %d channels, format 0x%x",
this->location, info.samplerate, info.channels, info.format);
if (!sf_format_check (&info))
goto bad_format;
this->file = sf_open (this->location, mode, &info);
if (!this->file)
goto open_failed;
return TRUE;
no_filename:
{
GST_ELEMENT_ERROR (this, RESOURCE, NOT_FOUND,
(_("No file name specified for writing.")), (NULL));
return FALSE;
}
bad_format:
{
GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
("Input parameters (rate:%d, channels:%d, format:0x%x) invalid",
info.samplerate, info.channels, info.format));
return FALSE;
}
open_failed:
{
GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE,
(_("Could not open file \"%s\" for writing."), this->location),
("soundfile error: %s", sf_strerror (NULL)));
return FALSE;
}
}
static void
gst_sf_sink_close_file (GstSFSink * this)
{
int err = 0;
g_return_if_fail (this->file != NULL);
GST_INFO_OBJECT (this, "Closing file %s", this->location);
if ((err = sf_close (this->file)))
goto close_failed;
this->file = NULL;
return;
close_failed:
{
GST_ELEMENT_ERROR (this, RESOURCE, CLOSE,
("Could not close file file \"%s\".", this->location),
("soundfile error: %s", sf_error_number (err)));
return;
}
}
static void
gst_sf_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
{
GstStructure *s;
gint width, depth;
s = gst_caps_get_structure (caps, 0);
/* fields for all formats */
gst_structure_fixate_field_nearest_int (s, "rate", 44100);
gst_structure_fixate_field_nearest_int (s, "channels", 2);
gst_structure_fixate_field_nearest_int (s, "width", 16);
/* fields for int */
if (gst_structure_has_field (s, "depth")) {
gst_structure_get_int (s, "width", &width);
/* round width to nearest multiple of 8 for the depth */
depth = GST_ROUND_UP_8 (width);
gst_structure_fixate_field_nearest_int (s, "depth", depth);
}
if (gst_structure_has_field (s, "signed"))
gst_structure_fixate_field_boolean (s, "signed", TRUE);
if (gst_structure_has_field (s, "endianness"))
gst_structure_fixate_field_nearest_int (s, "endianness", G_BYTE_ORDER);
}
static gboolean
gst_sf_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
{
GstSFSink *this = (GstSFSink *) bsink;
GstStructure *structure;
gint width, channels, rate;
structure = gst_caps_get_structure (caps, 0);
if (!gst_structure_get_int (structure, "width", &width)
|| !gst_structure_get_int (structure, "channels", &channels)
|| !gst_structure_get_int (structure, "rate", &rate))
goto impossible;
if (gst_structure_has_name (structure, "audio/x-raw-int")) {
switch (width) {
case 16:
this->writer = (GstSFWriter) sf_writef_short;
break;
case 32:
this->writer = (GstSFWriter) sf_writef_int;
break;
default:
goto impossible;
}
} else {
switch (width) {
case 32:
this->writer = (GstSFWriter) sf_writef_float;
break;
default:
goto impossible;
}
}
this->bytes_per_frame = width * channels / 8;
this->rate = rate;
this->channels = channels;
return gst_sf_sink_open_file (this);
impossible:
{
g_warning ("something impossible happened");
return FALSE;
}
}
/* with STREAM_LOCK
*/
static void
gst_sf_sink_loop (GstPad * pad)
{
GstSFSink *this;
GstBaseSink *basesink;
GstBuffer *buf = NULL;
GstFlowReturn result;
this = GST_SF_SINK (gst_pad_get_parent (pad));
basesink = GST_BASE_SINK (this);
result = gst_pad_pull_range (pad, basesink->offset,
this->buffer_frames * this->bytes_per_frame, &buf);
if (G_UNLIKELY (result != GST_FLOW_OK))
goto paused;
if (G_UNLIKELY (buf == NULL))
goto no_buffer;
basesink->offset += GST_BUFFER_SIZE (buf);
GST_BASE_SINK_PREROLL_LOCK (basesink);
result = gst_sf_sink_render (basesink, buf);
GST_BASE_SINK_PREROLL_UNLOCK (basesink);
if (G_UNLIKELY (result != GST_FLOW_OK))
goto paused;
gst_object_unref (this);
return;
/* ERRORS */
paused:
{
GST_INFO_OBJECT (basesink, "pausing task, reason %s",
gst_flow_get_name (result));
gst_pad_pause_task (pad);
/* fatal errors and NOT_LINKED cause EOS */
if (result == GST_FLOW_UNEXPECTED) {
gst_pad_send_event (pad, gst_event_new_eos ());
} else if (result < GST_FLOW_UNEXPECTED || result == GST_FLOW_NOT_LINKED) {
GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
(_("Internal data stream error.")),
("stream stopped, reason %s", gst_flow_get_name (result)));
gst_pad_send_event (pad, gst_event_new_eos ());
}
gst_object_unref (this);
return;
}
no_buffer:
{
GST_INFO_OBJECT (this, "no buffer, pausing");
result = GST_FLOW_ERROR;
goto paused;
}
}
static gboolean
gst_sf_sink_activate_pull (GstBaseSink * basesink, gboolean active)
{
gboolean result;
if (active) {
/* start task */
result = gst_pad_start_task (basesink->sinkpad,
(GstTaskFunction) gst_sf_sink_loop, basesink->sinkpad, NULL);
} else {
/* step 2, make sure streaming finishes */
result = gst_pad_stop_task (basesink->sinkpad);
}
return result;
}
static GstFlowReturn
gst_sf_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
{
GstSFSink *this;
sf_count_t written, num_to_write;
this = (GstSFSink *) bsink;
if (GST_BUFFER_SIZE (buffer) % this->bytes_per_frame)
goto bad_length;
num_to_write = GST_BUFFER_SIZE (buffer) / this->bytes_per_frame;
written = this->writer (this->file, GST_BUFFER_DATA (buffer), num_to_write);
if (written != num_to_write)
goto short_write;
return GST_FLOW_OK;
bad_length:
{
GST_ELEMENT_ERROR (this, RESOURCE, WRITE,
(_("Could not write to file \"%s\"."), this->location),
("bad buffer size: %u %% %d != 0", GST_BUFFER_SIZE (buffer),
this->bytes_per_frame));
return GST_FLOW_ERROR;
}
short_write:
{
GST_ELEMENT_ERROR (this, RESOURCE, WRITE,
(_("Could not write to file \"%s\"."), this->location),
("soundfile error: %s", sf_strerror (this->file)));
return GST_FLOW_ERROR;
}
}
static gboolean
gst_sf_sink_event (GstBaseSink * bsink, GstEvent * event)
{
GstSFSink *this;
GstEventType type;
this = (GstSFSink *) bsink;
type = GST_EVENT_TYPE (event);
switch (type) {
case GST_EVENT_EOS:
if (this->file)
sf_write_sync (this->file);
break;
default:
break;
}
return TRUE;
}
|