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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
25 #include <gst/audio/audio.h>
27 #include <gst/gst-i18n-plugin.h>
29 #include "gstsfsink.h"
40 #define DEFAULT_BUFFER_FRAMES (256)
42 static GstStaticPadTemplate sf_sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
45 GST_STATIC_CAPS ("audio/x-raw-float, "
46 "rate = (int) [ 1, MAX ], "
47 "channels = (int) [ 1, MAX ], "
48 "endianness = (int) BYTE_ORDER, "
51 "rate = (int) [ 1, MAX ], "
52 "channels = (int) [ 1, MAX ], "
53 "endianness = (int) BYTE_ORDER, "
54 "width = (int) {16, 32}, "
55 "depth = (int) {16, 32}, " "signed = (boolean) true")
58 GST_BOILERPLATE (GstSFSink, gst_sf_sink, GstBaseSink, GST_TYPE_BASE_SINK);
60 static void gst_sf_sink_set_property (GObject * object, guint prop_id,
61 const GValue * value, GParamSpec * pspec);
62 static void gst_sf_sink_get_property (GObject * object, guint prop_id,
63 GValue * value, GParamSpec * pspec);
65 static gboolean gst_sf_sink_start (GstBaseSink * bsink);
66 static gboolean gst_sf_sink_stop (GstBaseSink * bsink);
67 static void gst_sf_sink_fixate (GstBaseSink * bsink, GstCaps * caps);
68 static gboolean gst_sf_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
69 static gboolean gst_sf_sink_activate_pull (GstBaseSink * bsink,
71 static GstFlowReturn gst_sf_sink_render (GstBaseSink * bsink,
73 static gboolean gst_sf_sink_event (GstBaseSink * bsink, GstEvent * event);
75 static gboolean gst_sf_sink_open_file (GstSFSink * this);
76 static void gst_sf_sink_close_file (GstSFSink * this);
78 GST_DEBUG_CATEGORY_STATIC (gst_sf_debug);
79 #define GST_CAT_DEFAULT gst_sf_debug
82 gst_sf_sink_base_init (gpointer g_class)
84 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
86 GST_DEBUG_CATEGORY_INIT (gst_sf_debug, "sfsink", 0, "sfsink element");
87 gst_element_class_add_static_pad_template (element_class, &sf_sink_factory);
88 gst_element_class_set_static_metadata (element_class, "Sndfile sink",
90 "Write audio streams to disk using libsndfile",
91 "Andy Wingo <wingo at pobox dot com>");
95 gst_sf_sink_class_init (GstSFSinkClass * klass)
97 GObjectClass *gobject_class;
98 GstBaseSinkClass *basesink_class;
101 gobject_class = (GObjectClass *) klass;
102 basesink_class = (GstBaseSinkClass *) klass;
104 gobject_class->set_property = gst_sf_sink_set_property;
105 gobject_class->get_property = gst_sf_sink_get_property;
107 g_object_class_install_property (gobject_class, PROP_LOCATION,
108 g_param_spec_string ("location", "File Location",
109 "Location of the file to write", NULL,
110 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
111 pspec = g_param_spec_enum
112 ("major-type", "Major type", "Major output type", GST_TYPE_SF_MAJOR_TYPES,
114 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
115 g_object_class_install_property (gobject_class, PROP_MAJOR_TYPE, pspec);
116 pspec = g_param_spec_enum
117 ("minor-type", "Minor type", "Minor output type", GST_TYPE_SF_MINOR_TYPES,
119 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
120 g_object_class_install_property (gobject_class, PROP_MINOR_TYPE, pspec);
121 pspec = g_param_spec_int
122 ("buffer-frames", "Buffer frames",
123 "Number of frames per buffer, in pull mode", 1, G_MAXINT,
124 DEFAULT_BUFFER_FRAMES,
125 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
126 g_object_class_install_property (gobject_class, PROP_BUFFER_FRAMES, pspec);
128 basesink_class->get_times = NULL;
129 basesink_class->start = GST_DEBUG_FUNCPTR (gst_sf_sink_start);
130 basesink_class->stop = GST_DEBUG_FUNCPTR (gst_sf_sink_stop);
131 basesink_class->fixate = GST_DEBUG_FUNCPTR (gst_sf_sink_fixate);
132 basesink_class->set_caps = GST_DEBUG_FUNCPTR (gst_sf_sink_set_caps);
133 basesink_class->activate_pull = GST_DEBUG_FUNCPTR (gst_sf_sink_activate_pull);
134 basesink_class->render = GST_DEBUG_FUNCPTR (gst_sf_sink_render);
135 basesink_class->event = GST_DEBUG_FUNCPTR (gst_sf_sink_event);
139 gst_sf_sink_init (GstSFSink * this, GstSFSinkClass * klass)
141 GST_BASE_SINK (this)->can_activate_pull = TRUE;
145 gst_sf_sink_set_location (GstSFSink * this, const gchar * location)
150 g_free (this->location);
152 this->location = location ? g_strdup (location) : NULL;
158 g_warning ("Changing the `location' property on sfsink when "
159 "a file is open not supported.");
166 gst_sf_sink_set_property (GObject * object, guint prop_id, const GValue * value,
169 GstSFSink *this = GST_SF_SINK (object);
173 gst_sf_sink_set_location (this, g_value_get_string (value));
176 case PROP_MAJOR_TYPE:
177 this->format_major = g_value_get_enum (value);
180 case PROP_MINOR_TYPE:
181 this->format_subtype = g_value_get_enum (value);
184 case PROP_BUFFER_FRAMES:
185 this->buffer_frames = g_value_get_int (value);
194 gst_sf_sink_get_property (GObject * object, guint prop_id, GValue * value,
197 GstSFSink *this = GST_SF_SINK (object);
201 g_value_set_string (value, this->location);
204 case PROP_MAJOR_TYPE:
205 g_value_set_enum (value, this->format_major);
208 case PROP_MINOR_TYPE:
209 g_value_set_enum (value, this->format_subtype);
212 case PROP_BUFFER_FRAMES:
213 g_value_set_int (value, this->buffer_frames);
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 gst_sf_sink_start (GstBaseSink * bsink)
230 gst_sf_sink_stop (GstBaseSink * bsink)
232 GstSFSink *this = GST_SF_SINK (bsink);
235 gst_sf_sink_close_file (this);
241 gst_sf_sink_open_file (GstSFSink * this)
246 g_return_val_if_fail (this->file == NULL, FALSE);
247 g_return_val_if_fail (this->rate > 0, FALSE);
248 g_return_val_if_fail (this->channels > 0, FALSE);
254 this->format = this->format_major | this->format_subtype;
255 info.samplerate = this->rate;
256 info.channels = this->channels;
257 info.format = this->format;
259 GST_INFO_OBJECT (this, "Opening %s with rate %d, %d channels, format 0x%x",
260 this->location, info.samplerate, info.channels, info.format);
262 if (!sf_format_check (&info))
265 this->file = sf_open (this->location, mode, &info);
274 GST_ELEMENT_ERROR (this, RESOURCE, NOT_FOUND,
275 (_("No file name specified for writing.")), (NULL));
280 GST_ELEMENT_ERROR (this, STREAM, ENCODE, (NULL),
281 ("Input parameters (rate:%d, channels:%d, format:0x%x) invalid",
282 info.samplerate, info.channels, info.format));
287 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE,
288 (_("Could not open file \"%s\" for writing."), this->location),
289 ("soundfile error: %s", sf_strerror (NULL)));
295 gst_sf_sink_close_file (GstSFSink * this)
299 g_return_if_fail (this->file != NULL);
301 GST_INFO_OBJECT (this, "Closing file %s", this->location);
303 if ((err = sf_close (this->file)))
312 GST_ELEMENT_ERROR (this, RESOURCE, CLOSE,
313 ("Could not close file file \"%s\".", this->location),
314 ("soundfile error: %s", sf_error_number (err)));
320 gst_sf_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
325 s = gst_caps_get_structure (caps, 0);
327 /* fields for all formats */
328 gst_structure_fixate_field_nearest_int (s, "rate", 44100);
329 gst_structure_fixate_field_nearest_int (s, "channels", 2);
330 gst_structure_fixate_field_nearest_int (s, "width", 16);
333 if (gst_structure_has_field (s, "depth")) {
334 gst_structure_get_int (s, "width", &width);
335 /* round width to nearest multiple of 8 for the depth */
336 depth = GST_ROUND_UP_8 (width);
337 gst_structure_fixate_field_nearest_int (s, "depth", depth);
339 if (gst_structure_has_field (s, "signed"))
340 gst_structure_fixate_field_boolean (s, "signed", TRUE);
341 if (gst_structure_has_field (s, "endianness"))
342 gst_structure_fixate_field_nearest_int (s, "endianness", G_BYTE_ORDER);
346 gst_sf_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
348 GstSFSink *this = (GstSFSink *) bsink;
349 GstStructure *structure;
350 gint width, channels, rate;
352 structure = gst_caps_get_structure (caps, 0);
354 if (!gst_structure_get_int (structure, "width", &width)
355 || !gst_structure_get_int (structure, "channels", &channels)
356 || !gst_structure_get_int (structure, "rate", &rate))
359 if (gst_structure_has_name (structure, "audio/x-raw-int")) {
362 this->writer = (GstSFWriter) sf_writef_short;
365 this->writer = (GstSFWriter) sf_writef_int;
373 this->writer = (GstSFWriter) sf_writef_float;
380 this->bytes_per_frame = width * channels / 8;
382 this->channels = channels;
384 return gst_sf_sink_open_file (this);
388 g_warning ("something impossible happened");
396 gst_sf_sink_loop (GstPad * pad)
399 GstBaseSink *basesink;
400 GstBuffer *buf = NULL;
401 GstFlowReturn result;
403 this = GST_SF_SINK (gst_pad_get_parent (pad));
404 basesink = GST_BASE_SINK (this);
406 result = gst_pad_pull_range (pad, basesink->offset,
407 this->buffer_frames * this->bytes_per_frame, &buf);
408 if (G_UNLIKELY (result != GST_FLOW_OK))
411 if (G_UNLIKELY (buf == NULL))
414 basesink->offset += GST_BUFFER_SIZE (buf);
416 GST_BASE_SINK_PREROLL_LOCK (basesink);
417 result = gst_sf_sink_render (basesink, buf);
418 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
419 if (G_UNLIKELY (result != GST_FLOW_OK))
422 gst_object_unref (this);
429 GST_INFO_OBJECT (basesink, "pausing task, reason %s",
430 gst_flow_get_name (result));
431 gst_pad_pause_task (pad);
432 /* fatal errors and NOT_LINKED cause EOS */
433 if (result == GST_FLOW_UNEXPECTED) {
434 gst_pad_send_event (pad, gst_event_new_eos ());
435 } else if (result < GST_FLOW_UNEXPECTED || result == GST_FLOW_NOT_LINKED) {
436 GST_ELEMENT_FLOW_ERROR (basesink, result);
437 gst_pad_send_event (pad, gst_event_new_eos ());
439 gst_object_unref (this);
444 GST_INFO_OBJECT (this, "no buffer, pausing");
445 result = GST_FLOW_ERROR;
451 gst_sf_sink_activate_pull (GstBaseSink * basesink, gboolean active)
457 result = gst_pad_start_task (basesink->sinkpad,
458 (GstTaskFunction) gst_sf_sink_loop, basesink->sinkpad, NULL);
460 /* step 2, make sure streaming finishes */
461 result = gst_pad_stop_task (basesink->sinkpad);
468 gst_sf_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
471 sf_count_t written, num_to_write;
473 this = (GstSFSink *) bsink;
475 if (GST_BUFFER_SIZE (buffer) % this->bytes_per_frame)
478 num_to_write = GST_BUFFER_SIZE (buffer) / this->bytes_per_frame;
480 written = this->writer (this->file, GST_BUFFER_DATA (buffer), num_to_write);
481 if (written != num_to_write)
488 GST_ELEMENT_ERROR (this, RESOURCE, WRITE,
489 (_("Could not write to file \"%s\"."), this->location),
490 ("bad buffer size: %u %% %d != 0", GST_BUFFER_SIZE (buffer),
491 this->bytes_per_frame));
492 return GST_FLOW_ERROR;
496 GST_ELEMENT_ERROR (this, RESOURCE, WRITE,
497 (_("Could not write to file \"%s\"."), this->location),
498 ("soundfile error: %s", sf_strerror (this->file)));
499 return GST_FLOW_ERROR;
504 gst_sf_sink_event (GstBaseSink * bsink, GstEvent * event)
509 this = (GstSFSink *) bsink;
511 type = GST_EVENT_TYPE (event);
516 sf_write_sync (this->file);