1
/* GStreamer libsndfile plugin
2
* Copyright (C) 2007 Andy Wingo <wingo at pobox dot com>
4
* This library is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU Library General Public
6
* License as published by the Free Software Foundation; either
7
* version 2 of the License, or (at your option) any later version.
9
* This library is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
* Library General Public License for more details.
14
* You should have received a copy of the GNU Library General Public
15
* License along with this library; if not, write to the
16
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
* Boston, MA 02111-1307, USA.
25
#include <gst/audio/audio.h>
27
#include <gst/gst-i18n-plugin.h>
29
#include "gstsfsink.h"
32
static const GstElementDetails sfsink_details =
33
GST_ELEMENT_DETAILS ("Sndfile sink",
35
"Write audio streams to disk using libsndfile",
36
"Andy Wingo <wingo at pobox dot com>");
47
#define DEFAULT_BUFFER_FRAMES (256)
49
static GstStaticPadTemplate sf_sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
52
GST_STATIC_CAPS ("audio/x-raw-float, "
53
"rate = (int) [ 1, MAX ], "
54
"channels = (int) [ 1, MAX ], "
55
"endianness = (int) BYTE_ORDER, "
58
"rate = (int) [ 1, MAX ], "
59
"channels = (int) [ 1, MAX ], "
60
"endianness = (int) BYTE_ORDER, "
61
"width = (int) {16, 32}, "
62
"depth = (int) {16, 32}, " "signed = (boolean) true")
65
GST_BOILERPLATE (GstSFSink, gst_sf_sink, GstBaseSink, GST_TYPE_BASE_SINK);
67
static void gst_sf_sink_set_property (GObject * object, guint prop_id,
68
const GValue * value, GParamSpec * pspec);
69
static void gst_sf_sink_get_property (GObject * object, guint prop_id,
70
GValue * value, GParamSpec * pspec);
72
static gboolean gst_sf_sink_start (GstBaseSink * bsink);
73
static gboolean gst_sf_sink_stop (GstBaseSink * bsink);
74
static void gst_sf_sink_fixate (GstBaseSink * bsink, GstCaps * caps);
75
static gboolean gst_sf_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
76
static gboolean gst_sf_sink_activate_pull (GstBaseSink * bsink,
78
static GstFlowReturn gst_sf_sink_render (GstBaseSink * bsink,
80
static gboolean gst_sf_sink_event (GstBaseSink * bsink, GstEvent * event);
82
static gboolean gst_sf_sink_open_file (GstSFSink * this);
83
static void gst_sf_sink_close_file (GstSFSink * this);
85
GST_DEBUG_CATEGORY_STATIC (gst_sf_debug);
86
#define GST_CAT_DEFAULT gst_sf_debug
89
gst_sf_sink_base_init (gpointer g_class)
91
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
93
GST_DEBUG_CATEGORY_INIT (gst_sf_debug, "sfsink", 0, "sfsink element");
94
gst_element_class_add_pad_template (element_class,
95
gst_static_pad_template_get (&sf_sink_factory));
96
gst_element_class_set_details (element_class, &sfsink_details);
100
gst_sf_sink_class_init (GstSFSinkClass * klass)
102
GObjectClass *gobject_class;
103
GstBaseSinkClass *basesink_class;
106
gobject_class = (GObjectClass *) klass;
107
basesink_class = (GstBaseSinkClass *) klass;
109
gobject_class->set_property = gst_sf_sink_set_property;
110
gobject_class->get_property = gst_sf_sink_get_property;
112
g_object_class_install_property (gobject_class, PROP_LOCATION,
113
g_param_spec_string ("location", "File Location",
114
"Location of the file to write", NULL, G_PARAM_READWRITE));
115
pspec = g_param_spec_enum
116
("major-type", "Major type", "Major output type", GST_TYPE_SF_MAJOR_TYPES,
117
SF_FORMAT_WAV, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
118
g_object_class_install_property (gobject_class, PROP_MAJOR_TYPE, pspec);
119
pspec = g_param_spec_enum
120
("minor-type", "Minor type", "Minor output type", GST_TYPE_SF_MINOR_TYPES,
121
SF_FORMAT_FLOAT, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
122
g_object_class_install_property (gobject_class, PROP_MINOR_TYPE, pspec);
123
pspec = g_param_spec_int
124
("buffer-frames", "Buffer frames",
125
"Number of frames per buffer, in pull mode", 1, G_MAXINT,
126
DEFAULT_BUFFER_FRAMES, G_PARAM_READWRITE | G_PARAM_CONSTRUCT);
127
g_object_class_install_property (gobject_class, PROP_BUFFER_FRAMES, pspec);
129
basesink_class->get_times = NULL;
130
basesink_class->start = GST_DEBUG_FUNCPTR (gst_sf_sink_start);
131
basesink_class->stop = GST_DEBUG_FUNCPTR (gst_sf_sink_stop);
132
basesink_class->fixate = GST_DEBUG_FUNCPTR (gst_sf_sink_fixate);
133
basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_sf_sink_set_caps);
134
basesink_class->activate_pull = GST_DEBUG_FUNCPTR (gst_sf_sink_activate_pull);
135
basesink_class->render = GST_DEBUG_FUNCPTR (gst_sf_sink_render);
136
basesink_class->event = GST_DEBUG_FUNCPTR (gst_sf_sink_event);
140
gst_sf_sink_init (GstSFSink * this, GstSFSinkClass * klass)
142
GST_BASE_SINK (this)->can_activate_pull = TRUE;
146
gst_sf_sink_set_location (GstSFSink * this, const gchar * location)
152
g_free (this->location);
154
this->location = location ? g_strdup (location) : NULL;
160
g_warning ("Changing the `location' property on sfsink when "
161
"a file is open not supported.");
168
gst_sf_sink_set_property (GObject * object, guint prop_id, const GValue * value,
171
GstSFSink *this = GST_SF_SINK (object);
175
gst_sf_sink_set_location (this, g_value_get_string (value));
178
case PROP_MAJOR_TYPE:
179
this->format_major = g_value_get_enum (value);
182
case PROP_MINOR_TYPE:
183
this->format_subtype = g_value_get_enum (value);
186
case PROP_BUFFER_FRAMES:
187
this->buffer_frames = g_value_get_int (value);
196
gst_sf_sink_get_property (GObject * object, guint prop_id, GValue * value,
199
GstSFSink *this = GST_SF_SINK (object);
203
g_value_set_string (value, this->location);
206
case PROP_MAJOR_TYPE:
207
g_value_set_enum (value, this->format_major);
210
case PROP_MINOR_TYPE:
211
g_value_set_enum (value, this->format_subtype);
214
case PROP_BUFFER_FRAMES:
215
g_value_set_int (value, this->buffer_frames);
219
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
225
gst_sf_sink_start (GstBaseSink * bsink)
232
gst_sf_sink_stop (GstBaseSink * bsink)
234
GstSFSink *this = GST_SF_SINK (bsink);
237
gst_sf_sink_close_file (this);
243
gst_sf_sink_open_file (GstSFSink * this)
248
g_return_val_if_fail (this->file == NULL, FALSE);
249
g_return_val_if_fail (this->rate > 0, FALSE);
250
g_return_val_if_fail (this->channels > 0, FALSE);
256
this->format = this->format_major | this->format_subtype;
257
info.samplerate = this->rate;
258
info.channels = this->channels;
259
info.format = this->format;
261
GST_INFO_OBJECT (this, "Opening %s with rate %d, %d channels, format 0x%x",
262
this->location, info.samplerate, info.channels, info.format);
264
if (!sf_format_check (&info))
267
this->file = sf_open (this->location, mode, &info);
276
GST_ELEMENT_ERROR (this, RESOURCE, NOT_FOUND,
277
(_("No file name specified for writing.")), (NULL));
282
GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
283
("Input parameters (rate:%d, channels:%d, format:0x%x) invalid",
284
info.samplerate, info.channels, info.format));
289
GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE,
290
(_("Could not open file \"%s\" for writing."), this->location),
291
("soundfile error: %s", sf_strerror (NULL)));
297
gst_sf_sink_close_file (GstSFSink * this)
301
g_return_if_fail (this->file != NULL);
303
GST_INFO_OBJECT (this, "Closing file %s", this->location);
305
if ((err = sf_close (this->file)))
314
GST_ELEMENT_ERROR (this, RESOURCE, CLOSE,
315
("Could not close file file \"%s\".", this->location),
316
("soundfile error: %s", sf_error_number (err)));
322
gst_sf_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
327
s = gst_caps_get_structure (caps, 0);
329
/* fields for all formats */
330
gst_structure_fixate_field_nearest_int (s, "rate", 44100);
331
gst_structure_fixate_field_nearest_int (s, "channels", 2);
332
gst_structure_fixate_field_nearest_int (s, "width", 16);
335
if (gst_structure_has_field (s, "depth")) {
336
gst_structure_get_int (s, "width", &width);
337
/* round width to nearest multiple of 8 for the depth */
338
depth = GST_ROUND_UP_8 (width);
339
gst_structure_fixate_field_nearest_int (s, "depth", depth);
341
if (gst_structure_has_field (s, "signed"))
342
gst_structure_fixate_field_boolean (s, "signed", TRUE);
343
if (gst_structure_has_field (s, "endianness"))
344
gst_structure_fixate_field_nearest_int (s, "endianness", G_BYTE_ORDER);
348
gst_sf_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
350
GstSFSink *this = (GstSFSink *) bsink;
351
GstStructure *structure;
352
gint width, channels, rate;
354
structure = gst_caps_get_structure (caps, 0);
356
if (!gst_structure_get_int (structure, "width", &width)
357
|| !gst_structure_get_int (structure, "channels", &channels)
358
|| !gst_structure_get_int (structure, "rate", &rate))
361
if (gst_structure_has_name (structure, "audio/x-raw-int")) {
364
this->writer = (GstSFWriter) sf_writef_short;
367
this->writer = (GstSFWriter) sf_writef_int;
375
this->writer = (GstSFWriter) sf_writef_float;
382
this->bytes_per_frame = width * channels / 8;
384
this->channels = channels;
386
return gst_sf_sink_open_file (this);
390
g_warning ("something impossible happened");
398
gst_sf_sink_loop (GstPad * pad)
401
GstBaseSink *basesink;
402
GstBuffer *buf = NULL;
403
GstFlowReturn result;
405
this = GST_SF_SINK (gst_pad_get_parent (pad));
406
basesink = GST_BASE_SINK (this);
408
result = gst_pad_pull_range (pad, basesink->offset,
409
this->buffer_frames * this->bytes_per_frame, &buf);
410
if (G_UNLIKELY (result != GST_FLOW_OK))
413
if (G_UNLIKELY (buf == NULL))
416
basesink->offset += GST_BUFFER_SIZE (buf);
418
GST_PAD_PREROLL_LOCK (pad);
419
result = gst_sf_sink_render (basesink, buf);
420
GST_PAD_PREROLL_UNLOCK (pad);
421
if (G_UNLIKELY (result != GST_FLOW_OK))
424
gst_object_unref (this);
431
GST_INFO_OBJECT (basesink, "pausing task, reason %s",
432
gst_flow_get_name (result));
433
gst_pad_pause_task (pad);
434
/* fatal errors and NOT_LINKED cause EOS */
435
if (GST_FLOW_IS_FATAL (result) || result == GST_FLOW_NOT_LINKED) {
436
gst_pad_send_event (pad, gst_event_new_eos ());
437
/* EOS does not cause an ERROR message */
438
if (result != GST_FLOW_UNEXPECTED) {
439
GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
440
(_("Internal data stream error.")),
441
("stream stopped, reason %s", gst_flow_get_name (result)));
444
gst_object_unref (this);
449
GST_INFO_OBJECT (this, "no buffer, pausing");
450
result = GST_FLOW_ERROR;
456
gst_sf_sink_activate_pull (GstBaseSink * basesink, gboolean active)
462
result = gst_pad_start_task (basesink->sinkpad,
463
(GstTaskFunction) gst_sf_sink_loop, basesink->sinkpad);
465
/* step 2, make sure streaming finishes */
466
result = gst_pad_stop_task (basesink->sinkpad);
473
gst_sf_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
476
sf_count_t written, num_to_write;
478
this = (GstSFSink *) bsink;
480
if (GST_BUFFER_SIZE (buffer) % this->bytes_per_frame)
483
num_to_write = GST_BUFFER_SIZE (buffer) / this->bytes_per_frame;
485
written = this->writer (this->file, GST_BUFFER_DATA (buffer), num_to_write);
486
if (written != num_to_write)
493
GST_ELEMENT_ERROR (this, RESOURCE, WRITE,
494
(_("Could not write to file \"%s\"."), this->location),
495
("bad buffer size: %u %% %d != 0", GST_BUFFER_SIZE (buffer),
496
this->bytes_per_frame));
497
return GST_FLOW_ERROR;
501
GST_ELEMENT_ERROR (this, RESOURCE, WRITE,
502
(_("Could not write to file \"%s\"."), this->location),
503
("soundfile error: %s", sf_strerror (this->file)));
504
return GST_FLOW_ERROR;
509
gst_sf_sink_event (GstBaseSink * bsink, GstEvent * event)
514
this = (GstSFSink *) bsink;
516
type = GST_EVENT_TYPE (event);
521
sf_write_sync (this->file);