1 /* GStreamer libsndfile plugin
2 * Copyright (C) 2007 Andy Wingo <wingo at pobox dot com>
3 *
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.
8 *
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.
13 *
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.
18 */
19
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gst/audio/audio.h>
26
27 #include <gst/gst-i18n-plugin.h>
28
29 #include "gstsfsink.h"
30
31 enum
32 {
33 PROP_0,
34 PROP_LOCATION,
35 PROP_MAJOR_TYPE,
36 PROP_MINOR_TYPE,
37 PROP_BUFFER_FRAMES
38 };
39
40 #define DEFAULT_BUFFER_FRAMES (256)
41
42 static GstStaticPadTemplate sf_sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
43 GST_PAD_SINK,
44 GST_PAD_ALWAYS,
45 GST_STATIC_CAPS ("audio/x-raw-float, "
46 "rate = (int) [ 1, MAX ], "
47 "channels = (int) [ 1, MAX ], "
48 "endianness = (int) BYTE_ORDER, "
49 "width = (int) 32; "
50 "audio/x-raw-int, "
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")
56 );
57
58 GST_BOILERPLATE (GstSFSink, gst_sf_sink, GstBaseSink, GST_TYPE_BASE_SINK);
59
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);
64
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,
70 gboolean active);
71 static GstFlowReturn gst_sf_sink_render (GstBaseSink * bsink,
72 GstBuffer * buffer);
73 static gboolean gst_sf_sink_event (GstBaseSink * bsink, GstEvent * event);
74
75 static gboolean gst_sf_sink_open_file (GstSFSink * this);
76 static void gst_sf_sink_close_file (GstSFSink * this);
77
78 GST_DEBUG_CATEGORY_STATIC (gst_sf_debug);
79 #define GST_CAT_DEFAULT gst_sf_debug
80
81 static void
gst_sf_sink_base_init(gpointer g_class)82 gst_sf_sink_base_init (gpointer g_class)
83 {
84 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
85
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",
89 "Sink/Audio",
90 "Write audio streams to disk using libsndfile",
91 "Andy Wingo <wingo at pobox dot com>");
92 }
93
94 static void
gst_sf_sink_class_init(GstSFSinkClass * klass)95 gst_sf_sink_class_init (GstSFSinkClass * klass)
96 {
97 GObjectClass *gobject_class;
98 GstBaseSinkClass *basesink_class;
99 GParamSpec *pspec;
100
101 gobject_class = (GObjectClass *) klass;
102 basesink_class = (GstBaseSinkClass *) klass;
103
104 gobject_class->set_property = gst_sf_sink_set_property;
105 gobject_class->get_property = gst_sf_sink_get_property;
106
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,
113 SF_FORMAT_WAV,
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,
118 SF_FORMAT_FLOAT,
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);
127
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);
136 }
137
138 static void
gst_sf_sink_init(GstSFSink * this,GstSFSinkClass * klass)139 gst_sf_sink_init (GstSFSink * this, GstSFSinkClass * klass)
140 {
141 GST_BASE_SINK (this)->can_activate_pull = TRUE;
142 }
143
144 static void
gst_sf_sink_set_location(GstSFSink * this,const gchar * location)145 gst_sf_sink_set_location (GstSFSink * this, const gchar * location)
146 {
147 if (this->file)
148 goto was_open;
149
150 g_free (this->location);
151
152 this->location = location ? g_strdup (location) : NULL;
153
154 return;
155
156 was_open:
157 {
158 g_warning ("Changing the `location' property on sfsink when "
159 "a file is open not supported.");
160 return;
161 }
162 }
163
164
165 static void
gst_sf_sink_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)166 gst_sf_sink_set_property (GObject * object, guint prop_id, const GValue * value,
167 GParamSpec * pspec)
168 {
169 GstSFSink *this = GST_SF_SINK (object);
170
171 switch (prop_id) {
172 case PROP_LOCATION:
173 gst_sf_sink_set_location (this, g_value_get_string (value));
174 break;
175
176 case PROP_MAJOR_TYPE:
177 this->format_major = g_value_get_enum (value);
178 break;
179
180 case PROP_MINOR_TYPE:
181 this->format_subtype = g_value_get_enum (value);
182 break;
183
184 case PROP_BUFFER_FRAMES:
185 this->buffer_frames = g_value_get_int (value);
186 break;
187
188 default:
189 break;
190 }
191 }
192
193 static void
gst_sf_sink_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)194 gst_sf_sink_get_property (GObject * object, guint prop_id, GValue * value,
195 GParamSpec * pspec)
196 {
197 GstSFSink *this = GST_SF_SINK (object);
198
199 switch (prop_id) {
200 case PROP_LOCATION:
201 g_value_set_string (value, this->location);
202 break;
203
204 case PROP_MAJOR_TYPE:
205 g_value_set_enum (value, this->format_major);
206 break;
207
208 case PROP_MINOR_TYPE:
209 g_value_set_enum (value, this->format_subtype);
210 break;
211
212 case PROP_BUFFER_FRAMES:
213 g_value_set_int (value, this->buffer_frames);
214 break;
215
216 default:
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 break;
219 }
220 }
221
222 static gboolean
gst_sf_sink_start(GstBaseSink * bsink)223 gst_sf_sink_start (GstBaseSink * bsink)
224 {
225 /* pass */
226 return TRUE;
227 }
228
229 static gboolean
gst_sf_sink_stop(GstBaseSink * bsink)230 gst_sf_sink_stop (GstBaseSink * bsink)
231 {
232 GstSFSink *this = GST_SF_SINK (bsink);
233
234 if (this->file)
235 gst_sf_sink_close_file (this);
236
237 return TRUE;
238 }
239
240 static gboolean
gst_sf_sink_open_file(GstSFSink * this)241 gst_sf_sink_open_file (GstSFSink * this)
242 {
243 int mode;
244 SF_INFO info;
245
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);
249
250 if (!this->location)
251 goto no_filename;
252
253 mode = SFM_WRITE;
254 this->format = this->format_major | this->format_subtype;
255 info.samplerate = this->rate;
256 info.channels = this->channels;
257 info.format = this->format;
258
259 GST_INFO_OBJECT (this, "Opening %s with rate %d, %d channels, format 0x%x",
260 this->location, info.samplerate, info.channels, info.format);
261
262 if (!sf_format_check (&info))
263 goto bad_format;
264
265 this->file = sf_open (this->location, mode, &info);
266
267 if (!this->file)
268 goto open_failed;
269
270 return TRUE;
271
272 no_filename:
273 {
274 GST_ELEMENT_ERROR (this, RESOURCE, NOT_FOUND,
275 (_("No file name specified for writing.")), (NULL));
276 return FALSE;
277 }
278 bad_format:
279 {
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));
283 return FALSE;
284 }
285 open_failed:
286 {
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)));
290 return FALSE;
291 }
292 }
293
294 static void
gst_sf_sink_close_file(GstSFSink * this)295 gst_sf_sink_close_file (GstSFSink * this)
296 {
297 int err = 0;
298
299 g_return_if_fail (this->file != NULL);
300
301 GST_INFO_OBJECT (this, "Closing file %s", this->location);
302
303 if ((err = sf_close (this->file)))
304 goto close_failed;
305
306 this->file = NULL;
307
308 return;
309
310 close_failed:
311 {
312 GST_ELEMENT_ERROR (this, RESOURCE, CLOSE,
313 ("Could not close file file \"%s\".", this->location),
314 ("soundfile error: %s", sf_error_number (err)));
315 return;
316 }
317 }
318
319 static void
gst_sf_sink_fixate(GstBaseSink * bsink,GstCaps * caps)320 gst_sf_sink_fixate (GstBaseSink * bsink, GstCaps * caps)
321 {
322 GstStructure *s;
323 gint width, depth;
324
325 s = gst_caps_get_structure (caps, 0);
326
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);
331
332 /* fields for int */
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);
338 }
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);
343 }
344
345 static gboolean
gst_sf_sink_set_caps(GstBaseSink * bsink,GstCaps * caps)346 gst_sf_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
347 {
348 GstSFSink *this = (GstSFSink *) bsink;
349 GstStructure *structure;
350 gint width, channels, rate;
351
352 structure = gst_caps_get_structure (caps, 0);
353
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))
357 goto impossible;
358
359 if (gst_structure_has_name (structure, "audio/x-raw-int")) {
360 switch (width) {
361 case 16:
362 this->writer = (GstSFWriter) sf_writef_short;
363 break;
364 case 32:
365 this->writer = (GstSFWriter) sf_writef_int;
366 break;
367 default:
368 goto impossible;
369 }
370 } else {
371 switch (width) {
372 case 32:
373 this->writer = (GstSFWriter) sf_writef_float;
374 break;
375 default:
376 goto impossible;
377 }
378 }
379
380 this->bytes_per_frame = width * channels / 8;
381 this->rate = rate;
382 this->channels = channels;
383
384 return gst_sf_sink_open_file (this);
385
386 impossible:
387 {
388 g_warning ("something impossible happened");
389 return FALSE;
390 }
391 }
392
393 /* with STREAM_LOCK
394 */
395 static void
gst_sf_sink_loop(GstPad * pad)396 gst_sf_sink_loop (GstPad * pad)
397 {
398 GstSFSink *this;
399 GstBaseSink *basesink;
400 GstBuffer *buf = NULL;
401 GstFlowReturn result;
402
403 this = GST_SF_SINK (gst_pad_get_parent (pad));
404 basesink = GST_BASE_SINK (this);
405
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))
409 goto paused;
410
411 if (G_UNLIKELY (buf == NULL))
412 goto no_buffer;
413
414 basesink->offset += GST_BUFFER_SIZE (buf);
415
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))
420 goto paused;
421
422 gst_object_unref (this);
423
424 return;
425
426 /* ERRORS */
427 paused:
428 {
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 ());
438 }
439 gst_object_unref (this);
440 return;
441 }
442 no_buffer:
443 {
444 GST_INFO_OBJECT (this, "no buffer, pausing");
445 result = GST_FLOW_ERROR;
446 goto paused;
447 }
448 }
449
450 static gboolean
gst_sf_sink_activate_pull(GstBaseSink * basesink,gboolean active)451 gst_sf_sink_activate_pull (GstBaseSink * basesink, gboolean active)
452 {
453 gboolean result;
454
455 if (active) {
456 /* start task */
457 result = gst_pad_start_task (basesink->sinkpad,
458 (GstTaskFunction) gst_sf_sink_loop, basesink->sinkpad, NULL);
459 } else {
460 /* step 2, make sure streaming finishes */
461 result = gst_pad_stop_task (basesink->sinkpad);
462 }
463
464 return result;
465 }
466
467 static GstFlowReturn
gst_sf_sink_render(GstBaseSink * bsink,GstBuffer * buffer)468 gst_sf_sink_render (GstBaseSink * bsink, GstBuffer * buffer)
469 {
470 GstSFSink *this;
471 sf_count_t written, num_to_write;
472
473 this = (GstSFSink *) bsink;
474
475 if (GST_BUFFER_SIZE (buffer) % this->bytes_per_frame)
476 goto bad_length;
477
478 num_to_write = GST_BUFFER_SIZE (buffer) / this->bytes_per_frame;
479
480 written = this->writer (this->file, GST_BUFFER_DATA (buffer), num_to_write);
481 if (written != num_to_write)
482 goto short_write;
483
484 return GST_FLOW_OK;
485
486 bad_length:
487 {
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;
493 }
494 short_write:
495 {
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;
500 }
501 }
502
503 static gboolean
gst_sf_sink_event(GstBaseSink * bsink,GstEvent * event)504 gst_sf_sink_event (GstBaseSink * bsink, GstEvent * event)
505 {
506 GstSFSink *this;
507 GstEventType type;
508
509 this = (GstSFSink *) bsink;
510
511 type = GST_EVENT_TYPE (event);
512
513 switch (type) {
514 case GST_EVENT_EOS:
515 if (this->file)
516 sf_write_sync (this->file);
517 break;
518 default:
519 break;
520 }
521
522 return TRUE;
523 }
524