1 /* GStreamer libsndfile plugin
2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000,2005 Wim Taymans <wim@fluendo.com>
4 * 2003,2007 Andy Wingo <wingo@pobox.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
28 #include <gst/gst-i18n-plugin.h>
38 static GstStaticPadTemplate sf_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
41 GST_STATIC_CAPS ("audio/x-raw-float, "
42 "rate = (int) [ 1, MAX ], "
43 "channels = (int) [ 1, MAX ], "
44 "endianness = (int) BYTE_ORDER, "
47 "rate = (int) [ 1, MAX ], "
48 "channels = (int) [ 1, MAX ], "
49 "endianness = (int) BYTE_ORDER, "
50 "width = (int) {16, 32}, "
51 "depth = (int) {16, 32}, " "signed = (boolean) true")
55 GST_DEBUG_CATEGORY_STATIC (gst_sf_src_debug);
56 #define GST_CAT_DEFAULT gst_sf_src_debug
59 #define DEFAULT_BUFFER_FRAMES (256)
62 static void gst_sf_src_finalize (GObject * object);
63 static void gst_sf_src_set_property (GObject * object, guint prop_id,
64 const GValue * value, GParamSpec * pspec);
65 static void gst_sf_src_get_property (GObject * object, guint prop_id,
66 GValue * value, GParamSpec * pspec);
68 static gboolean gst_sf_src_start (GstBaseSrc * bsrc);
69 static gboolean gst_sf_src_stop (GstBaseSrc * bsrc);
70 static gboolean gst_sf_src_is_seekable (GstBaseSrc * bsrc);
71 static gboolean gst_sf_src_get_size (GstBaseSrc * bsrc, guint64 * size);
72 static GstFlowReturn gst_sf_src_create (GstBaseSrc * bsrc, guint64 offset,
73 guint length, GstBuffer ** buffer);
74 static GstCaps *gst_sf_src_get_caps (GstBaseSrc * bsrc);
75 static gboolean gst_sf_src_set_caps (GstBaseSrc * bsrc, GstCaps * caps);
76 static gboolean gst_sf_src_check_get_range (GstBaseSrc * bsrc);
77 static void gst_sf_src_fixate (GstBaseSrc * bsrc, GstCaps * caps);
79 GST_BOILERPLATE (GstSFSrc, gst_sf_src, GstBaseSrc, GST_TYPE_BASE_SRC);
82 gst_sf_src_base_init (gpointer g_class)
84 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
86 gst_element_class_add_static_pad_template (gstelement_class, &sf_src_factory);
88 gst_element_class_set_static_metadata (gstelement_class, "Sndfile source",
90 "Read audio streams from disk using libsndfile",
91 "Andy Wingo <wingo at pobox dot com>");
92 GST_DEBUG_CATEGORY_INIT (gst_sf_src_debug, "sfsrc", 0, "sfsrc element");
96 gst_sf_src_class_init (GstSFSrcClass * klass)
98 GObjectClass *gobject_class;
99 GstBaseSrcClass *gstbasesrc_class;
101 gobject_class = G_OBJECT_CLASS (klass);
102 gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
104 gobject_class->set_property = gst_sf_src_set_property;
105 gobject_class->get_property = gst_sf_src_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 read", NULL, G_PARAM_READWRITE));
111 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_sf_src_finalize);
113 gstbasesrc_class->start = GST_DEBUG_FUNCPTR (gst_sf_src_start);
114 gstbasesrc_class->stop = GST_DEBUG_FUNCPTR (gst_sf_src_stop);
115 gstbasesrc_class->is_seekable = GST_DEBUG_FUNCPTR (gst_sf_src_is_seekable);
116 gstbasesrc_class->get_size = GST_DEBUG_FUNCPTR (gst_sf_src_get_size);
117 gstbasesrc_class->create = GST_DEBUG_FUNCPTR (gst_sf_src_create);
118 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_sf_src_get_caps);
119 gstbasesrc_class->set_caps = GST_DEBUG_FUNCPTR (gst_sf_src_set_caps);
120 gstbasesrc_class->check_get_range =
121 GST_DEBUG_FUNCPTR (gst_sf_src_check_get_range);
122 gstbasesrc_class->fixate = GST_DEBUG_FUNCPTR (gst_sf_src_fixate);
126 gst_sf_src_init (GstSFSrc * src, GstSFSrcClass * g_class)
131 gst_sf_src_finalize (GObject * object)
135 src = GST_SF_SRC (object);
137 g_free (src->location);
139 G_OBJECT_CLASS (parent_class)->finalize (object);
143 gst_sf_src_set_location (GstSFSrc * this, const gchar * location)
148 g_free (this->location);
150 this->location = location ? g_strdup (location) : NULL;
156 g_warning ("Changing the `location' property on sfsrc when "
157 "a file is open not supported.");
163 gst_sf_src_set_property (GObject * object, guint prop_id, const GValue * value,
166 GstSFSrc *this = GST_SF_SRC (object);
170 gst_sf_src_set_location (this, g_value_get_string (value));
179 gst_sf_src_get_property (GObject * object, guint prop_id, GValue * value,
182 GstSFSrc *this = GST_SF_SRC (object);
186 g_value_set_string (value, this->location);
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
196 gst_sf_src_create (GstBaseSrc * bsrc, guint64 offset, guint length,
201 /* FIXME discont is set but not used */
203 gboolean discont = FALSE;
205 sf_count_t bytes_read;
207 this = GST_SF_SRC (bsrc);
209 if (G_UNLIKELY (offset % this->bytes_per_frame))
211 if (G_UNLIKELY (length % this->bytes_per_frame))
214 offset /= this->bytes_per_frame;
216 if (G_UNLIKELY (this->offset != offset)) {
219 pos = sf_seek (this->file, offset, SEEK_SET);
221 if (G_UNLIKELY (pos < 0 || pos != offset))
224 this->offset = offset;
230 buf = gst_buffer_new_and_alloc (length);
232 /* now make length in frames */
233 length /= this->bytes_per_frame;
235 bytes_read = this->reader (this->file, GST_BUFFER_DATA (buf), length);
236 if (G_UNLIKELY (bytes_read < 0))
239 if (G_UNLIKELY (bytes_read == 0 && length > 0))
242 GST_BUFFER_SIZE (buf) = bytes_read * this->bytes_per_frame;
244 GST_BUFFER_OFFSET (buf) = offset;
245 GST_BUFFER_OFFSET_END (buf) = offset + length;
246 GST_BUFFER_TIMESTAMP (buf) = gst_util_uint64_scale_int (offset,
247 GST_SECOND, this->rate);
248 GST_BUFFER_DURATION (buf) = gst_util_uint64_scale_int (offset + length,
249 GST_SECOND, this->rate) - GST_BUFFER_TIMESTAMP (buf);
251 gst_buffer_set_caps (buf, GST_PAD_CAPS (GST_BASE_SRC_PAD (bsrc)));
255 this->offset += length;
262 GST_ELEMENT_ERROR (this, RESOURCE, SEEK,
263 (NULL), ("offset %" G_GUINT64_FORMAT " not divisible by %d bytes per "
264 "frame", offset, this->bytes_per_frame));
265 return GST_FLOW_ERROR;
269 GST_ELEMENT_ERROR (this, RESOURCE, SEEK, (NULL),
270 ("length %u not divisible by %d bytes per frame", length,
271 this->bytes_per_frame));
272 return GST_FLOW_ERROR;
276 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
277 return GST_FLOW_ERROR;
281 GST_ELEMENT_ERROR (this, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
282 gst_buffer_unref (buf);
283 return GST_FLOW_ERROR;
287 GST_DEBUG ("EOS, baby");
288 gst_buffer_unref (buf);
289 return GST_FLOW_UNEXPECTED;
294 gst_sf_src_is_seekable (GstBaseSrc * basesrc)
300 gst_sf_src_get_size (GstBaseSrc * basesrc, guint64 * size)
305 this = GST_SF_SRC (basesrc);
307 end = sf_seek (this->file, 0, SEEK_END);
309 sf_seek (this->file, this->offset, SEEK_SET);
311 *size = end * this->bytes_per_frame;
317 gst_sf_src_open_file (GstSFSrc * this)
322 g_return_val_if_fail (this->file == NULL, FALSE);
330 this->file = sf_open (this->location, mode, &info);
335 this->channels = info.channels;
336 this->rate = info.samplerate;
337 /* do something with info.seekable? */
343 GST_ELEMENT_ERROR (this, RESOURCE, NOT_FOUND,
344 (_("No file name specified for writing.")), (NULL));
349 GST_ELEMENT_ERROR (this, RESOURCE, OPEN_WRITE,
350 (_("Could not open file \"%s\" for writing."), this->location),
351 ("soundfile error: %s", sf_strerror (NULL)));
357 gst_sf_src_close_file (GstSFSrc * this)
361 g_return_if_fail (this->file != NULL);
363 GST_INFO_OBJECT (this, "Closing file %s", this->location);
365 if ((err = sf_close (this->file)))
377 GST_ELEMENT_ERROR (this, RESOURCE, CLOSE,
378 ("Could not close file file \"%s\".", this->location),
379 ("soundfile error: %s", sf_error_number (err)));
385 gst_sf_src_start (GstBaseSrc * basesrc)
387 GstSFSrc *this = GST_SF_SRC (basesrc);
389 return gst_sf_src_open_file (this);
392 /* unmap and close the file */
394 gst_sf_src_stop (GstBaseSrc * basesrc)
396 GstSFSrc *this = GST_SF_SRC (basesrc);
398 gst_sf_src_close_file (this);
404 gst_sf_src_get_caps (GstBaseSrc * bsrc)
409 this = GST_SF_SRC (bsrc);
411 ret = gst_caps_copy (gst_pad_get_pad_template_caps (bsrc->srcpad));
417 for (i = 0; i < gst_caps_get_size (ret); i++) {
418 s = gst_caps_get_structure (ret, i);
419 gst_structure_set (s, "channels", G_TYPE_INT, this->channels,
420 "rate", G_TYPE_INT, this->rate, NULL);
428 gst_sf_src_set_caps (GstBaseSrc * bsrc, GstCaps * caps)
430 GstSFSrc *this = (GstSFSrc *) bsrc;
431 GstStructure *structure;
434 structure = gst_caps_get_structure (caps, 0);
439 if (!gst_structure_get_int (structure, "width", &width))
442 if (gst_structure_has_name (structure, "audio/x-raw-int")) {
445 this->reader = (GstSFReader) sf_readf_short;
448 this->reader = (GstSFReader) sf_readf_int;
456 this->reader = (GstSFReader) sf_readf_float;
463 this->bytes_per_frame = width * this->channels / 8;
469 g_warning ("something impossible happened");
474 GST_WARNING_OBJECT (this, "file has to be open in order to set caps");
480 gst_sf_src_check_get_range (GstBaseSrc * bsrc)
486 gst_sf_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
491 s = gst_caps_get_structure (caps, 0);
493 gst_structure_fixate_field_nearest_int (s, "width", 16);
496 if (gst_structure_has_field (s, "depth")) {
497 gst_structure_get_int (s, "width", &width);
498 /* round width to nearest multiple of 8 for the depth */
499 depth = GST_ROUND_UP_8 (width);
500 gst_structure_fixate_field_nearest_int (s, "depth", depth);
502 if (gst_structure_has_field (s, "signed"))
503 gst_structure_fixate_field_boolean (s, "signed", TRUE);
504 if (gst_structure_has_field (s, "endianness"))
505 gst_structure_fixate_field_nearest_int (s, "endianness", G_BYTE_ORDER);