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_pad_template (element_class,
88 gst_static_pad_template_get (&sf_sink_factory));
89 gst_element_class_set_static_metadata (element_class, "Sndfile sink",
91 "Write audio streams to disk using libsndfile",
92 "Andy Wingo <wingo at pobox dot com>");
96 gst_sf_sink_class_init (GstSFSinkClass * klass)
98 GObjectClass *gobject_class;
99 GstBaseSinkClass *basesink_class;
102 gobject_class = (GObjectClass *) klass;
103 basesink_class = (GstBaseSinkClass *) klass;
105 gobject_class->set_property = gst_sf_sink_set_property;
106 gobject_class->get_property = gst_sf_sink_get_property;
108 g_object_class_install_property (gobject_class, PROP_LOCATION,
109 g_param_spec_string ("location", "File Location",
110 "Location of the file to write", NULL,
111 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
112 pspec = g_param_spec_enum
113 ("major-type", "Major type", "Major output type", GST_TYPE_SF_MAJOR_TYPES,
115 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
116 g_object_class_install_property (gobject_class, PROP_MAJOR_TYPE, pspec);
117 pspec = g_param_spec_enum
118 ("minor-type", "Minor type", "Minor output type", GST_TYPE_SF_MINOR_TYPES,
120 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
121 g_object_class_install_property (gobject_class, PROP_MINOR_TYPE, pspec);
122 pspec = g_param_spec_int
123 ("buffer-frames", "Buffer frames",
124 "Number of frames per buffer, in pull mode", 1, G_MAXINT,
125 DEFAULT_BUFFER_FRAMES,
126 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS);
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_BASE_SINK_PREROLL_LOCK (basesink);
419 result = gst_sf_sink_render (basesink, buf);
420 GST_BASE_SINK_PREROLL_UNLOCK (basesink);
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 (result == GST_FLOW_UNEXPECTED) {
436 gst_pad_send_event (pad, gst_event_new_eos ());
437 } else if (result < GST_FLOW_UNEXPECTED || result == GST_FLOW_NOT_LINKED) {
438 GST_ELEMENT_ERROR (basesink, STREAM, FAILED,
439 (_("Internal data stream error.")),
440 ("stream stopped, reason %s", gst_flow_get_name (result)));
441 gst_pad_send_event (pad, gst_event_new_eos ());
443 gst_object_unref (this);
448 GST_INFO_OBJECT (this, "no buffer, pausing");
449 result = GST_FLOW_ERROR;
455 gst_sf_sink_activate_pull (GstBaseSink * basesink, gboolean active)
461 result = gst_pad_start_task (basesink->sinkpad,
462 (GstTaskFunction) gst_sf_sink_loop, basesink->sinkpad, NULL);
464 /* step 2, make sure streaming finishes */
465 result = gst_pad_stop_task (basesink->sinkpad);
472 gst_sf_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
475 sf_count_t written, num_to_write;
477 this = (GstSFSink *) bsink;
479 if (GST_BUFFER_SIZE (buffer) % this->bytes_per_frame)
482 num_to_write = GST_BUFFER_SIZE (buffer) / this->bytes_per_frame;
484 written = this->writer (this->file, GST_BUFFER_DATA (buffer), num_to_write);
485 if (written != num_to_write)
492 GST_ELEMENT_ERROR (this, RESOURCE, WRITE,
493 (_("Could not write to file \"%s\"."), this->location),
494 ("bad buffer size: %u %% %d != 0", GST_BUFFER_SIZE (buffer),
495 this->bytes_per_frame));
496 return GST_FLOW_ERROR;
500 GST_ELEMENT_ERROR (this, RESOURCE, WRITE,
501 (_("Could not write to file \"%s\"."), this->location),
502 ("soundfile error: %s", sf_strerror (this->file)));
503 return GST_FLOW_ERROR;
508 gst_sf_sink_event (GstBaseSink * bsink, GstEvent * event)
513 this = (GstSFSink *) bsink;
515 type = GST_EVENT_TYPE (event);
520 sf_write_sync (this->file);