1 /* GStreamer libsndfile plugin
2 * Copyright (C) 2013 Stefan Sauer <ensonic@users.sf.net>
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 self 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/gst-i18n-plugin.h>
26 #include <gst/audio/audio.h>
31 "{ "GST_AUDIO_NE (F32)", "GST_AUDIO_NE (S32)", "GST_AUDIO_NE (S16)" }"
33 static GstStaticPadTemplate sf_dec_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
36 GST_STATIC_CAPS ("audio/x-raw, "
37 "format = (string) " FORMATS ", "
38 "layout = (string) interleaved, "
39 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]"));
41 GST_DEBUG_CATEGORY_STATIC (gst_sf_dec_debug);
42 #define GST_CAT_DEFAULT gst_sf_dec_debug
44 #define DEFAULT_BUFFER_FRAMES (256)
46 static gboolean gst_sf_dec_src_event (GstPad * pad, GstObject * parent,
48 static gboolean gst_sf_dec_src_query (GstPad * pad, GstObject * parent,
50 static GstStateChangeReturn gst_sf_dec_change_state (GstElement * element,
51 GstStateChange transition);
53 static gboolean gst_sf_dec_sink_activate (GstPad * pad, GstObject * parent);
54 static gboolean gst_sf_dec_sink_activate_mode (GstPad * sinkpad,
55 GstObject * parent, GstPadMode mode, gboolean active);
56 static void gst_sf_dec_loop (GstPad * pad);
58 static gboolean gst_sf_dec_start (GstSFDec * bsrc);
59 static gboolean gst_sf_dec_stop (GstSFDec * bsrc);
62 GST_DEBUG_CATEGORY_INIT (gst_sf_dec_debug, "sfdec", 0, "sfdec element");
63 #define gst_sf_dec_parent_class parent_class
64 G_DEFINE_TYPE_WITH_CODE (GstSFDec, gst_sf_dec, GST_TYPE_ELEMENT, _do_init);
69 gst_sf_vio_get_filelen (void *user_data)
71 GstSFDec *self = GST_SF_DEC (user_data);
74 if (gst_pad_peer_query_duration (self->sinkpad, GST_FORMAT_BYTES, &dur)) {
75 return (sf_count_t) dur;
77 GST_WARNING_OBJECT (self, "query_duration failed");
82 gst_sf_vio_tell (void *user_data)
84 GstSFDec *self = GST_SF_DEC (user_data);
89 gst_sf_vio_seek (sf_count_t offset, int whence, void *user_data)
91 GstSFDec *self = GST_SF_DEC (user_data);
101 self->pos = gst_sf_vio_get_filelen (user_data) - offset;
104 return (sf_count_t) self->pos;
108 gst_sf_vio_read (void *ptr, sf_count_t count, void *user_data)
110 GstSFDec *self = GST_SF_DEC (user_data);
111 GstBuffer *buffer = gst_buffer_new_wrapped_full (0, ptr, count, 0, count,
114 if (gst_pad_pull_range (self->sinkpad, self->pos, count, &buffer) ==
116 GST_DEBUG_OBJECT (self, "read %d bytes @ pos %" G_GUINT64_FORMAT,
117 (gint) count, self->pos);
121 GST_WARNING_OBJECT (self, "read failed");
126 gst_sf_vio_write (const void *ptr, sf_count_t count, void *user_data)
128 GstSFDec *self = GST_SF_DEC (user_data);
129 GstBuffer *buffer = gst_buffer_new_wrapped (g_memdup (ptr, count), count);
131 if (gst_pad_push (self->srcpad, buffer) == GST_FLOW_OK) {
134 GST_WARNING_OBJECT (self, "write failed");
138 SF_VIRTUAL_IO gst_sf_vio = {
139 &gst_sf_vio_get_filelen,
148 gst_sf_dec_class_init (GstSFDecClass * klass)
150 GstElementClass *gstelement_class;
152 gstelement_class = GST_ELEMENT_CLASS (klass);
153 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_sf_dec_change_state);
155 gst_element_class_set_static_metadata (gstelement_class, "Sndfile decoder",
157 "Read audio streams using libsndfile",
158 "Stefan Sauer <ensonic@user.sf.net>");
160 gst_element_class_add_pad_template (gstelement_class,
161 gst_static_pad_template_get (&sf_dec_src_factory));
163 gst_element_class_add_pad_template (gstelement_class,
164 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
165 gst_sf_create_audio_template_caps ()));
170 gst_sf_dec_init (GstSFDec * self)
172 self->sinkpad = gst_pad_new_from_template (gst_element_class_get_pad_template
173 (GST_ELEMENT_GET_CLASS (self), "sink"), "sink");
174 gst_pad_set_activate_function (self->sinkpad,
175 GST_DEBUG_FUNCPTR (gst_sf_dec_sink_activate));
176 gst_pad_set_activatemode_function (self->sinkpad,
177 GST_DEBUG_FUNCPTR (gst_sf_dec_sink_activate_mode));
178 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
180 self->srcpad = gst_pad_new_from_static_template (&sf_dec_src_factory, "src");
181 gst_pad_set_event_function (self->srcpad,
182 GST_DEBUG_FUNCPTR (gst_sf_dec_src_event));
183 gst_pad_set_query_function (self->srcpad,
184 GST_DEBUG_FUNCPTR (gst_sf_dec_src_query));
185 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
189 gst_sf_dec_do_seek (GstSFDec * self, GstEvent * event)
194 GstSeekType cur_type, stop_type;
196 gint64 cur, stop, pos;
198 guint64 song_length = gst_util_uint64_scale_int (self->duration, GST_SECOND,
201 gst_event_parse_seek (event, &rate, &format, &flags, &cur_type, &cur,
204 if (format != GST_FORMAT_TIME)
205 goto unsupported_format;
207 /* FIXME: we should be using GstSegment for all this */
208 if (cur_type != GST_SEEK_TYPE_SET || stop_type != GST_SEEK_TYPE_NONE)
209 goto unsuported_type;
211 if (stop_type == GST_SEEK_TYPE_NONE)
212 stop = GST_CLOCK_TIME_NONE;
213 if (!GST_CLOCK_TIME_IS_VALID (stop) && song_length > 0)
216 cur = CLAMP (cur, -1, song_length);
219 pos = gst_util_uint64_scale_int (cur, self->rate, GST_SECOND);
220 if ((pos = sf_seek (self->file, pos, SEEK_SET) == -1))
224 cur = gst_util_uint64_scale_int (pos, GST_SECOND, self->rate);
226 GST_DEBUG_OBJECT (self, "seek to %" GST_TIME_FORMAT,
227 GST_TIME_ARGS ((guint64) cur));
229 flush = ((flags & GST_SEEK_FLAG_FLUSH) == GST_SEEK_FLAG_FLUSH);
232 gst_pad_push_event (self->srcpad, gst_event_new_flush_start ());
234 gst_pad_stop_task (self->sinkpad);
237 GST_PAD_STREAM_LOCK (self->sinkpad);
239 if (flags & GST_SEEK_FLAG_SEGMENT) {
240 gst_element_post_message (GST_ELEMENT (self),
241 gst_message_new_segment_start (GST_OBJECT (self), format, cur));
245 gst_pad_push_event (self->srcpad, gst_event_new_flush_stop (TRUE));
248 GST_LOG_OBJECT (self, "sending newsegment from %" GST_TIME_FORMAT "-%"
249 GST_TIME_FORMAT ", pos=%" GST_TIME_FORMAT,
250 GST_TIME_ARGS ((guint64) cur), GST_TIME_ARGS ((guint64) stop),
251 GST_TIME_ARGS ((guint64) cur));
253 gst_segment_init (&seg, GST_FORMAT_TIME);
258 gst_pad_push_event (self->srcpad, gst_event_new_segment (&seg));
260 gst_pad_start_task (self->sinkpad,
261 (GstTaskFunction) gst_sf_dec_loop, self, NULL);
263 GST_PAD_STREAM_UNLOCK (self->sinkpad);
270 GST_DEBUG_OBJECT (self, "seeking is only supported in TIME format");
275 GST_DEBUG_OBJECT (self, "unsupported seek type");
280 GST_DEBUG_OBJECT (self, "seek failed");
286 gst_sf_dec_src_event (GstPad * pad, GstObject * parent, GstEvent * event)
288 GstSFDec *self = GST_SF_DEC (parent);
289 gboolean res = FALSE;
291 GST_DEBUG_OBJECT (self, "event %s, %" GST_PTR_FORMAT,
292 GST_EVENT_TYPE_NAME (event), event);
294 switch (GST_EVENT_TYPE (event)) {
296 if (!self->file || !self->seekable)
298 res = gst_sf_dec_do_seek (self, event);
301 res = gst_pad_event_default (pad, parent, event);
305 GST_DEBUG_OBJECT (self, "event %s: %d", GST_EVENT_TYPE_NAME (event), res);
310 gst_sf_dec_src_query (GstPad * pad, GstObject * parent, GstQuery * query)
312 GstSFDec *self = GST_SF_DEC (parent);
314 gboolean res = FALSE;
316 GST_DEBUG_OBJECT (self, "query %s, %" GST_PTR_FORMAT,
317 GST_QUERY_TYPE_NAME (query), query);
319 switch (GST_QUERY_TYPE (query)) {
320 case GST_QUERY_DURATION:
323 gst_query_parse_duration (query, &format, NULL);
324 if (format == GST_FORMAT_TIME) {
325 gst_query_set_duration (query, format,
326 gst_util_uint64_scale_int (self->duration, GST_SECOND, self->rate));
330 case GST_QUERY_POSITION:
333 gst_query_parse_position (query, &format, NULL);
334 if (format == GST_FORMAT_TIME) {
335 gst_query_set_position (query, format,
336 gst_util_uint64_scale_int (self->pos, GST_SECOND, self->rate));
341 res = gst_pad_query_default (pad, parent, query);
346 GST_DEBUG_OBJECT (self, "query %s: %d", GST_QUERY_TYPE_NAME (query), res);
350 static GstStateChangeReturn
351 gst_sf_dec_change_state (GstElement * element, GstStateChange transition)
353 GstStateChangeReturn ret;
354 GstSFDec *self = GST_SF_DEC (element);
356 GST_INFO_OBJECT (self, "transition: %s -> %s",
357 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
358 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
360 switch (transition) {
361 case GST_STATE_CHANGE_READY_TO_PAUSED:
362 gst_sf_dec_start (self);
368 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
370 switch (transition) {
371 case GST_STATE_CHANGE_PAUSED_TO_READY:
372 gst_sf_dec_stop (self);
381 gst_sf_dec_start (GstSFDec * self)
387 gst_sf_dec_stop (GstSFDec * self)
391 GST_INFO_OBJECT (self, "Closing sndfile stream");
393 if (self->file && (err = sf_close (self->file)))
408 GST_ELEMENT_ERROR (self, RESOURCE, CLOSE,
409 ("Could not close sndfile stream."),
410 ("soundfile error: %s", sf_error_number (err)));
416 gst_sf_dec_sink_activate (GstPad * sinkpad, GstObject * parent)
421 query = gst_query_new_scheduling ();
423 if (!gst_pad_peer_query (sinkpad, query)) {
424 gst_query_unref (query);
428 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
429 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
430 gst_query_unref (query);
435 GST_DEBUG_OBJECT (sinkpad, "activating pull");
436 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
440 GST_DEBUG_OBJECT (sinkpad, "activating push");
441 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
446 gst_sf_dec_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
447 GstPadMode mode, gboolean active)
452 case GST_PAD_MODE_PUSH:
453 res = FALSE; /* no push support */
455 case GST_PAD_MODE_PULL:
457 /* if we have a scheduler we can start the task */
458 GST_DEBUG_OBJECT (sinkpad, "start task");
459 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_sf_dec_loop,
462 res = gst_pad_stop_task (sinkpad);
473 create_and_send_tags (GstSFDec * self, SF_INFO * info, SF_LOOP_INFO * loop_info,
474 SF_INSTRUMENT * instrument)
478 const gchar *codec_name;
481 tags = gst_tag_list_new_empty ();
482 if ((tag = sf_get_string (self->file, SF_STR_TITLE)) && *tag) {
483 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, tag, NULL);
485 if ((tag = sf_get_string (self->file, SF_STR_COMMENT)) && *tag) {
486 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_COMMENT, tag, NULL);
488 if ((tag = sf_get_string (self->file, SF_STR_ARTIST)) && *tag) {
489 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ARTIST, tag, NULL);
491 if ((tag = sf_get_string (self->file, SF_STR_ALBUM)) && *tag) {
492 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_ALBUM, tag, NULL);
494 if ((tag = sf_get_string (self->file, SF_STR_GENRE)) && *tag) {
495 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_GENRE, tag, NULL);
497 if ((tag = sf_get_string (self->file, SF_STR_COPYRIGHT)) && *tag) {
498 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_COPYRIGHT, tag, NULL);
500 if ((tag = sf_get_string (self->file, SF_STR_LICENSE)) && *tag) {
501 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_LICENSE, tag, NULL);
503 if ((tag = sf_get_string (self->file, SF_STR_SOFTWARE)) && *tag) {
504 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_APPLICATION_NAME, tag,
507 if ((tag = sf_get_string (self->file, SF_STR_TRACKNUMBER)) && *tag) {
508 guint track = atoi (tag);
509 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_TRACK_NUMBER, track,
512 if ((tag = sf_get_string (self->file, SF_STR_DATE)) && *tag) {
513 GValue tag_val = { 0, };
514 GType tag_type = gst_tag_get_type (GST_TAG_DATE_TIME);
516 g_value_init (&tag_val, tag_type);
517 if (gst_value_deserialize (&tag_val, tag)) {
518 gst_tag_list_add_value (tags, GST_TAG_MERGE_APPEND, GST_TAG_DATE_TIME,
521 GST_WARNING_OBJECT (self, "could not deserialize '%s' into a "
522 "tag %s of type %s", tag, GST_TAG_DATE_TIME, g_type_name (tag_type));
524 g_value_unset (&tag_val);
527 if (loop_info->bpm != 0.0) {
528 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_BEATS_PER_MINUTE,
529 (gdouble) loop_info->bpm, NULL);
531 if (loop_info->root_key != -1) {
532 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_MIDI_BASE_NOTE,
533 (guint) loop_info->root_key, NULL);
537 gst_tag_list_add (tags, GST_TAG_MERGE_REPLACE, GST_TAG_MIDI_BASE_NOTE,
538 (guint) instrument->basenote, NULL);
540 /* TODO: calculate bitrate: GST_TAG_BITRATE */
541 switch (info->format & SF_FORMAT_SUBMASK) {
542 case SF_FORMAT_PCM_S8:
543 case SF_FORMAT_PCM_16:
544 case SF_FORMAT_PCM_24:
545 case SF_FORMAT_PCM_32:
546 case SF_FORMAT_PCM_U8:
547 codec_name = "Uncompressed PCM audio";
549 case SF_FORMAT_FLOAT:
550 case SF_FORMAT_DOUBLE:
551 codec_name = "Uncompressed IEEE float audio";
554 codec_name = "µ-law audio";
557 codec_name = "A-law audio";
559 case SF_FORMAT_IMA_ADPCM:
560 case SF_FORMAT_MS_ADPCM:
561 case SF_FORMAT_VOX_ADPCM:
562 case SF_FORMAT_G721_32:
563 case SF_FORMAT_G723_24:
564 case SF_FORMAT_G723_40:
565 codec_name = "ADPCM audio";
567 case SF_FORMAT_GSM610:
568 codec_name = "MS GSM audio";
570 case SF_FORMAT_DWVW_12:
571 case SF_FORMAT_DWVW_16:
572 case SF_FORMAT_DWVW_24:
573 case SF_FORMAT_DWVW_N:
574 codec_name = "Delta Width Variable Word encoded audio";
576 case SF_FORMAT_DPCM_8:
577 case SF_FORMAT_DPCM_16:
578 codec_name = "differential PCM audio";
580 case SF_FORMAT_VORBIS:
581 codec_name = "Vorbis";
585 GST_WARNING_OBJECT (self, "unmapped codec_type: %d",
586 info->format & SF_FORMAT_SUBMASK);
590 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_AUDIO_CODEC,
594 if (!gst_tag_list_is_empty (tags)) {
595 GST_DEBUG_OBJECT (self, "have tags");
596 gst_pad_push_event (self->srcpad, gst_event_new_tag (tags));
598 gst_tag_list_unref (tags);
603 is_valid_loop (gint mode, guint start, guint end)
616 create_and_send_toc (GstSFDec * self, SF_INFO * info, SF_LOOP_INFO * loop_info,
617 SF_INSTRUMENT * instrument)
620 GstTocEntry *entry = NULL, *subentry = NULL;
624 gboolean have_loops = FALSE;
629 for (i = 0; i < 16; i++) {
630 if (is_valid_loop (instrument->loops[i].mode, instrument->loops[i].start,
631 instrument->loops[i].end)) {
637 GST_INFO_OBJECT (self, "Have no loops");
642 toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
643 GST_DEBUG_OBJECT (self, "have toc");
645 /* add cue edition */
646 entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, "loops");
647 stop = gst_util_uint64_scale_int (self->duration, GST_SECOND, self->rate);
648 gst_toc_entry_set_start_stop_times (entry, 0, stop);
649 gst_toc_append_entry (toc, entry);
651 for (i = 0; i < 16; i++) {
652 GST_DEBUG_OBJECT (self,
653 "loop[%2d]: mode=%d, start=%u, end=%u, count=%u", i,
654 instrument->loops[i].mode, instrument->loops[i].start,
655 instrument->loops[i].end, instrument->loops[i].count);
656 if (is_valid_loop (instrument->loops[i].mode, instrument->loops[i].start,
657 instrument->loops[i].end)) {
658 id = g_strdup_printf ("%08x", i);
659 subentry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_CHAPTER, id);
661 start = gst_util_uint64_scale_int (instrument->loops[i].start,
662 GST_SECOND, self->rate);
663 stop = gst_util_uint64_scale_int (instrument->loops[i].end,
664 GST_SECOND, self->rate);
665 gst_toc_entry_set_start_stop_times (subentry, start, stop);
666 gst_toc_entry_append_sub_entry (entry, subentry);
670 gst_pad_push_event (self->srcpad, gst_event_new_toc (toc, FALSE));
674 gst_sf_dec_open_file (GstSFDec * self)
676 SF_INFO info = { 0, };
677 SF_LOOP_INFO loop_info = { 0, };
678 SF_INSTRUMENT instrument = { 0, };
685 gboolean have_loop_info = FALSE;
686 gboolean have_instrument = FALSE;
688 GST_DEBUG_OBJECT (self, "opening the stream");
689 if (!(self->file = sf_open_virtual (&gst_sf_vio, SFM_READ, &info, self)))
693 gst_pad_create_stream_id (self->srcpad, GST_ELEMENT_CAST (self), NULL);
694 gst_pad_push_event (self->srcpad, gst_event_new_stream_start (stream_id));
697 self->channels = info.channels;
698 self->rate = info.samplerate;
699 self->duration = info.frames;
700 self->seekable = info.seekable;
701 GST_DEBUG_OBJECT (self, "stream openend: channels=%d, rate=%d, seekable=%d",
702 info.channels, info.samplerate, info.seekable);
704 /* negotiate srcpad caps */
705 if ((caps = gst_pad_get_allowed_caps (self->srcpad)) == NULL) {
706 caps = gst_pad_get_pad_template_caps (self->srcpad);
708 caps = gst_caps_make_writable (caps);
709 GST_DEBUG_OBJECT (self, "allowed caps %" GST_PTR_FORMAT, caps);
711 s = gst_caps_get_structure (caps, 0);
712 gst_structure_set (s,
713 "channels", G_TYPE_INT, self->channels,
714 "rate", G_TYPE_INT, self->rate, NULL);
716 if (!gst_structure_fixate_field_string (s, "format", GST_AUDIO_NE (S16)))
717 GST_WARNING_OBJECT (self, "Failed to fixate format to S16NE");
719 caps = gst_caps_fixate (caps);
721 GST_DEBUG_OBJECT (self, "fixated caps %" GST_PTR_FORMAT, caps);
723 /* configure to output the negotiated format */
724 s = gst_caps_get_structure (caps, 0);
725 format = gst_structure_get_string (s, "format");
726 if (g_str_equal (format, GST_AUDIO_NE (S32))) {
727 self->reader = (GstSFReader) sf_readf_int;
729 } else if (g_str_equal (format, GST_AUDIO_NE (S16))) {
730 self->reader = (GstSFReader) sf_readf_short;
733 self->reader = (GstSFReader) sf_readf_float;
736 self->bytes_per_frame = width * self->channels / 8;
738 gst_pad_set_caps (self->srcpad, caps);
739 gst_caps_unref (caps);
741 /* push initial segment */
742 gst_segment_init (&seg, GST_FORMAT_TIME);
743 seg.stop = gst_util_uint64_scale_int (self->duration, GST_SECOND, self->rate);
744 gst_pad_push_event (self->srcpad, gst_event_new_segment (&seg));
746 /* get extra details */
747 if (sf_command (self->file, SFC_GET_LOOP_INFO, &loop_info,
748 sizeof (loop_info))) {
749 GST_DEBUG_OBJECT (self, "have loop info");
750 have_loop_info = TRUE;
752 if (sf_command (self->file, SFC_GET_INSTRUMENT, &instrument,
753 sizeof (instrument))) {
754 GST_DEBUG_OBJECT (self, "have instrument");
755 have_instrument = TRUE;
758 create_and_send_tags (self, &info, (have_loop_info ? &loop_info : NULL),
759 (have_instrument ? &instrument : NULL));
761 create_and_send_toc (self, &info, (have_loop_info ? &loop_info : NULL),
762 (have_instrument ? &instrument : NULL));
768 GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ,
769 (_("Could not open sndfile stream for reading.")),
770 ("soundfile error: %s", sf_strerror (NULL)));
776 gst_sf_dec_loop (GstPad * pad)
778 GstSFDec *self = GST_SF_DEC (GST_PAD_PARENT (pad));
782 sf_count_t frames_read;
783 guint num_frames = 1024; /* arbitrary */
785 if (G_UNLIKELY (!self->file)) {
786 /* not started yet */
787 if (!gst_sf_dec_open_file (self))
791 buf = gst_buffer_new_and_alloc (self->bytes_per_frame * num_frames);
792 gst_buffer_map (buf, &map, GST_MAP_WRITE);
793 frames_read = self->reader (self->file, map.data, num_frames);
794 GST_LOG_OBJECT (self, "read %d / %d bytes = %d frames of audio",
795 (gint) frames_read, (gint) map.size, num_frames);
796 gst_buffer_unmap (buf, &map);
798 if (G_UNLIKELY (frames_read < 0))
801 if (G_UNLIKELY (frames_read == 0))
804 GST_BUFFER_OFFSET (buf) = self->offset;
805 GST_BUFFER_TIMESTAMP (buf) = gst_util_uint64_scale_int (self->offset,
806 GST_SECOND, self->rate);
807 self->offset += frames_read;
808 GST_BUFFER_DURATION (buf) = gst_util_uint64_scale_int (self->offset,
809 GST_SECOND, self->rate) - GST_BUFFER_TIMESTAMP (buf);
811 flow = gst_pad_push (self->srcpad, buf);
812 if (flow != GST_FLOW_OK) {
813 GST_LOG_OBJECT (self, "pad push flow: %s", gst_flow_get_name (flow));
822 GST_ELEMENT_ERROR (self, RESOURCE, READ, (NULL), GST_ERROR_SYSTEM);
823 gst_buffer_unref (buf);
828 GST_DEBUG_OBJECT (self, "EOS");
829 gst_buffer_unref (buf);
830 gst_pad_push_event (self->srcpad, gst_event_new_eos ());
835 GST_INFO_OBJECT (self, "Pausing");
836 gst_pad_pause_task (self->sinkpad);