1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
3 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * Copyright (C) <2006> Nokia Corporation, Stefan Kost <stefan.kost@nokia.com>.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-wavparse
25 * Parse a .wav file into raw or compressed audio.
27 * Wavparse supports both push and pull mode operations, making it possible to
28 * stream from a network source.
31 * <title>Example launch line</title>
33 * gst-launch filesrc location=sine.wav ! wavparse ! audioconvert ! alsasink
34 * ]| Read a wav file and output to the soundcard using the ALSA element. The
35 * wav file is assumed to contain raw uncompressed samples.
37 * gst-launch gnomevfssrc location=http://www.example.org/sine.wav ! queue ! wavparse ! audioconvert ! alsasink
38 * ]| Stream data from a network url.
41 * Last reviewed on 2007-02-14 (0.10.6)
46 * http://replaygain.hydrogenaudio.org/file_format_wav.html
55 #include "gstwavparse.h"
56 #include "gst/riff/riff-ids.h"
57 #include "gst/riff/riff-media.h"
58 #include <gst/base/gsttypefindhelper.h>
59 #include <gst/gst-i18n-plugin.h>
61 GST_DEBUG_CATEGORY_STATIC (wavparse_debug);
62 #define GST_CAT_DEFAULT (wavparse_debug)
64 static void gst_wavparse_dispose (GObject * object);
66 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad);
67 static gboolean gst_wavparse_sink_activate_pull (GstPad * sinkpad,
69 static gboolean gst_wavparse_send_event (GstElement * element,
71 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
72 GstStateChange transition);
74 static gboolean gst_wavparse_pad_query (GstPad * pad, GstQuery * query);
75 static gboolean gst_wavparse_pad_convert (GstPad * pad,
77 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
79 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstBuffer * buf);
80 static gboolean gst_wavparse_sink_event (GstPad * pad, GstEvent * event);
81 static void gst_wavparse_loop (GstPad * pad);
82 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstEvent * event);
84 static GstStaticPadTemplate sink_template_factory =
85 GST_STATIC_PAD_TEMPLATE ("sink",
88 GST_STATIC_CAPS ("audio/x-wav")
92 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
94 #define gst_wavparse_parent_class parent_class
95 G_DEFINE_TYPE_WITH_CODE (GstWavParse, gst_wavparse, GST_TYPE_ELEMENT,
99 gst_wavparse_class_init (GstWavParseClass * klass)
101 GstElementClass *gstelement_class;
102 GObjectClass *object_class;
103 GstPadTemplate *src_template;
105 gstelement_class = (GstElementClass *) klass;
106 object_class = (GObjectClass *) klass;
108 parent_class = g_type_class_peek_parent (klass);
110 object_class->dispose = gst_wavparse_dispose;
112 gstelement_class->change_state = gst_wavparse_change_state;
113 gstelement_class->send_event = gst_wavparse_send_event;
116 gst_element_class_add_pad_template (gstelement_class,
117 gst_static_pad_template_get (&sink_template_factory));
119 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
120 GST_PAD_ALWAYS, gst_riff_create_audio_template_caps ());
121 gst_element_class_add_pad_template (gstelement_class, src_template);
123 gst_element_class_set_details_simple (gstelement_class, "WAV audio demuxer",
124 "Codec/Demuxer/Audio",
125 "Parse a .wav file into raw audio",
126 "Erik Walthinsen <omega@cse.ogi.edu>");
130 gst_wavparse_reset (GstWavParse * wav)
132 wav->state = GST_WAVPARSE_START;
134 /* These will all be set correctly in the fmt chunk */
148 wav->got_fmt = FALSE;
152 gst_event_unref (wav->seek_event);
153 wav->seek_event = NULL;
155 gst_adapter_clear (wav->adapter);
156 g_object_unref (wav->adapter);
160 gst_tag_list_free (wav->tags);
163 gst_caps_unref (wav->caps);
165 if (wav->start_segment)
166 gst_event_unref (wav->start_segment);
167 wav->start_segment = NULL;
171 gst_wavparse_dispose (GObject * object)
173 GstWavParse *wav = GST_WAVPARSE (object);
175 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
176 gst_wavparse_reset (wav);
178 G_OBJECT_CLASS (parent_class)->dispose (object);
182 gst_wavparse_init (GstWavParse * wavparse)
184 gst_wavparse_reset (wavparse);
188 gst_pad_new_from_static_template (&sink_template_factory, "sink");
189 gst_pad_set_activate_function (wavparse->sinkpad,
190 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
191 gst_pad_set_activatepull_function (wavparse->sinkpad,
192 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_pull));
193 gst_pad_set_chain_function (wavparse->sinkpad,
194 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
195 gst_pad_set_event_function (wavparse->sinkpad,
196 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
197 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
201 gst_pad_new_from_template (gst_element_class_get_pad_template
202 (GST_ELEMENT_GET_CLASS (wavparse), "src"), "src");
203 gst_pad_use_fixed_caps (wavparse->srcpad);
204 gst_pad_set_query_function (wavparse->srcpad,
205 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
206 gst_pad_set_event_function (wavparse->srcpad,
207 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
208 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
212 gst_wavparse_destroy_sourcepad (GstWavParse * wavparse)
214 if (wavparse->srcpad) {
215 gst_element_remove_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
216 wavparse->srcpad = NULL;
220 /* Compute (value * nom) % denom, avoiding overflow. This can be used
221 * to perform ceiling or rounding division together with
222 * gst_util_uint64_scale[_int]. */
223 #define uint64_scale_modulo(val, nom, denom) \
224 ((val % denom) * (nom % denom) % denom)
226 /* Like gst_util_uint64_scale, but performs ceiling division. */
228 uint64_ceiling_scale_int (guint64 val, gint num, gint denom)
230 guint64 result = gst_util_uint64_scale_int (val, num, denom);
232 if (uint64_scale_modulo (val, num, denom) == 0)
238 /* Like gst_util_uint64_scale, but performs ceiling division. */
240 uint64_ceiling_scale (guint64 val, guint64 num, guint64 denom)
242 guint64 result = gst_util_uint64_scale (val, num, denom);
244 if (uint64_scale_modulo (val, num, denom) == 0)
251 /* FIXME: why is that not in use? */
254 gst_wavparse_parse_adtl (GstWavParse * wavparse, int len)
257 GstByteStream *bs = wavparse->bs;
258 gst_riff_chunk *temp_chunk, chunk;
260 struct _gst_riff_labl labl, *temp_labl;
261 struct _gst_riff_ltxt ltxt, *temp_ltxt;
262 struct _gst_riff_note note, *temp_note;
265 GstPropsEntry *entry;
269 props = wavparse->metadata->properties;
273 gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
274 if (got_bytes != sizeof (gst_riff_chunk)) {
277 temp_chunk = (gst_riff_chunk *) tempdata;
279 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
280 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
282 if (chunk.size == 0) {
283 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
284 len -= sizeof (gst_riff_chunk);
289 case GST_RIFF_adtl_labl:
291 gst_bytestream_peek_bytes (bs, &tempdata,
292 sizeof (struct _gst_riff_labl));
293 if (got_bytes != sizeof (struct _gst_riff_labl)) {
297 temp_labl = (struct _gst_riff_labl *) tempdata;
298 labl.id = GUINT32_FROM_LE (temp_labl->id);
299 labl.size = GUINT32_FROM_LE (temp_labl->size);
300 labl.identifier = GUINT32_FROM_LE (temp_labl->identifier);
302 gst_bytestream_flush (bs, sizeof (struct _gst_riff_labl));
303 len -= sizeof (struct _gst_riff_labl);
305 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, labl.size - 4);
306 if (got_bytes != labl.size - 4) {
310 label_name = (char *) tempdata;
312 gst_bytestream_flush (bs, ((labl.size - 4) + 1) & ~1);
313 len -= (((labl.size - 4) + 1) & ~1);
315 new_caps = gst_caps_new ("label",
316 "application/x-gst-metadata",
317 gst_props_new ("identifier", G_TYPE_INT (labl.identifier),
318 "name", G_TYPE_STRING (label_name), NULL));
320 if (gst_props_get (props, "labels", &caps, NULL)) {
321 caps = g_list_append (caps, new_caps);
323 caps = g_list_append (NULL, new_caps);
325 entry = gst_props_entry_new ("labels", GST_PROPS_GLIST (caps));
326 gst_props_add_entry (props, entry);
331 case GST_RIFF_adtl_ltxt:
333 gst_bytestream_peek_bytes (bs, &tempdata,
334 sizeof (struct _gst_riff_ltxt));
335 if (got_bytes != sizeof (struct _gst_riff_ltxt)) {
339 temp_ltxt = (struct _gst_riff_ltxt *) tempdata;
340 ltxt.id = GUINT32_FROM_LE (temp_ltxt->id);
341 ltxt.size = GUINT32_FROM_LE (temp_ltxt->size);
342 ltxt.identifier = GUINT32_FROM_LE (temp_ltxt->identifier);
343 ltxt.length = GUINT32_FROM_LE (temp_ltxt->length);
344 ltxt.purpose = GUINT32_FROM_LE (temp_ltxt->purpose);
345 ltxt.country = GUINT16_FROM_LE (temp_ltxt->country);
346 ltxt.language = GUINT16_FROM_LE (temp_ltxt->language);
347 ltxt.dialect = GUINT16_FROM_LE (temp_ltxt->dialect);
348 ltxt.codepage = GUINT16_FROM_LE (temp_ltxt->codepage);
350 gst_bytestream_flush (bs, sizeof (struct _gst_riff_ltxt));
351 len -= sizeof (struct _gst_riff_ltxt);
353 if (ltxt.size - 20 > 0) {
354 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, ltxt.size - 20);
355 if (got_bytes != ltxt.size - 20) {
359 gst_bytestream_flush (bs, ((ltxt.size - 20) + 1) & ~1);
360 len -= (((ltxt.size - 20) + 1) & ~1);
362 label_name = (char *) tempdata;
367 new_caps = gst_caps_new ("ltxt",
368 "application/x-gst-metadata",
369 gst_props_new ("identifier", G_TYPE_INT (ltxt.identifier),
370 "name", G_TYPE_STRING (label_name),
371 "length", G_TYPE_INT (ltxt.length), NULL));
373 if (gst_props_get (props, "ltxts", &caps, NULL)) {
374 caps = g_list_append (caps, new_caps);
376 caps = g_list_append (NULL, new_caps);
378 entry = gst_props_entry_new ("ltxts", GST_PROPS_GLIST (caps));
379 gst_props_add_entry (props, entry);
384 case GST_RIFF_adtl_note:
386 gst_bytestream_peek_bytes (bs, &tempdata,
387 sizeof (struct _gst_riff_note));
388 if (got_bytes != sizeof (struct _gst_riff_note)) {
392 temp_note = (struct _gst_riff_note *) tempdata;
393 note.id = GUINT32_FROM_LE (temp_note->id);
394 note.size = GUINT32_FROM_LE (temp_note->size);
395 note.identifier = GUINT32_FROM_LE (temp_note->identifier);
397 gst_bytestream_flush (bs, sizeof (struct _gst_riff_note));
398 len -= sizeof (struct _gst_riff_note);
400 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, note.size - 4);
401 if (got_bytes != note.size - 4) {
405 gst_bytestream_flush (bs, ((note.size - 4) + 1) & ~1);
406 len -= (((note.size - 4) + 1) & ~1);
408 label_name = (char *) tempdata;
410 new_caps = gst_caps_new ("note",
411 "application/x-gst-metadata",
412 gst_props_new ("identifier", G_TYPE_INT (note.identifier),
413 "name", G_TYPE_STRING (label_name), NULL));
415 if (gst_props_get (props, "notes", &caps, NULL)) {
416 caps = g_list_append (caps, new_caps);
418 caps = g_list_append (NULL, new_caps);
420 entry = gst_props_entry_new ("notes", GST_PROPS_GLIST (caps));
421 gst_props_add_entry (props, entry);
427 g_print ("Unknown chunk: %" GST_FOURCC_FORMAT "\n",
428 GST_FOURCC_ARGS (chunk.id));
433 g_object_notify (G_OBJECT (wavparse), "metadata");
437 gst_wavparse_parse_cues (GstWavParse * wavparse, int len)
440 GstByteStream *bs = wavparse->bs;
441 struct _gst_riff_cue *temp_cue, cue;
442 struct _gst_riff_cuepoints *points;
446 GstPropsEntry *entry;
452 gst_bytestream_peek_bytes (bs, &tempdata,
453 sizeof (struct _gst_riff_cue));
454 temp_cue = (struct _gst_riff_cue *) tempdata;
456 /* fixup for our big endian friends */
457 cue.id = GUINT32_FROM_LE (temp_cue->id);
458 cue.size = GUINT32_FROM_LE (temp_cue->size);
459 cue.cuepoints = GUINT32_FROM_LE (temp_cue->cuepoints);
461 gst_bytestream_flush (bs, sizeof (struct _gst_riff_cue));
462 if (got_bytes != sizeof (struct _gst_riff_cue)) {
466 len -= sizeof (struct _gst_riff_cue);
468 /* -4 because cue.size contains the cuepoints size
469 and we've already flushed that out of the system */
470 required = cue.size - 4;
471 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, required);
472 gst_bytestream_flush (bs, ((required) + 1) & ~1);
473 if (got_bytes != required) {
477 len -= (((cue.size - 4) + 1) & ~1);
479 /* now we have an array of struct _gst_riff_cuepoints in tempdata */
480 points = (struct _gst_riff_cuepoints *) tempdata;
482 for (i = 0; i < cue.cuepoints; i++) {
485 caps = gst_caps_new ("cues",
486 "application/x-gst-metadata",
487 gst_props_new ("identifier", G_TYPE_INT (points[i].identifier),
488 "position", G_TYPE_INT (points[i].offset), NULL));
489 cues = g_list_append (cues, caps);
492 entry = gst_props_entry_new ("cues", GST_PROPS_GLIST (cues));
493 gst_props_add_entry (wavparse->metadata->properties, entry);
496 g_object_notify (G_OBJECT (wavparse), "metadata");
499 /* Read 'fmt ' header */
501 gst_wavparse_fmt (GstWavParse * wav)
503 gst_riff_strf_auds *header = NULL;
506 if (!gst_riff_read_strf_auds (wav, &header))
509 wav->format = header->format;
510 wav->rate = header->rate;
511 wav->channels = header->channels;
512 if (wav->channels == 0)
515 wav->blockalign = header->blockalign;
516 wav->width = (header->blockalign * 8) / header->channels;
517 wav->depth = header->size;
518 wav->bps = header->av_bps;
522 /* Note: gst_riff_create_audio_caps might need to fix values in
523 * the header header depending on the format, so call it first */
524 caps = gst_riff_create_audio_caps (header->format, NULL, header, NULL);
530 gst_wavparse_create_sourcepad (wav);
531 gst_pad_use_fixed_caps (wav->srcpad);
532 gst_pad_set_active (wav->srcpad, TRUE);
533 gst_pad_set_caps (wav->srcpad, caps);
534 gst_caps_free (caps);
535 gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
536 gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
538 GST_DEBUG ("frequency %d, channels %d", wav->rate, wav->channels);
545 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
546 ("No FMT tag found"));
551 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
552 ("Stream claims to contain zero channels - invalid data"));
558 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
559 ("Stream claims to bitrate of <= zero - invalid data"));
565 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
571 gst_wavparse_other (GstWavParse * wav)
575 if (!gst_riff_peek_head (wav, &tag, &length, NULL)) {
576 GST_WARNING_OBJECT (wav, "could not peek head");
579 GST_DEBUG_OBJECT (wav, "got tag (%08x) %4.4s, length %d", tag,
580 (gchar *) & tag, length);
583 case GST_RIFF_TAG_LIST:
584 if (!(tag = gst_riff_peek_list (wav))) {
585 GST_WARNING_OBJECT (wav, "could not peek list");
590 case GST_RIFF_LIST_INFO:
591 if (!gst_riff_read_list (wav, &tag) || !gst_riff_read_info (wav)) {
592 GST_WARNING_OBJECT (wav, "could not read list");
597 case GST_RIFF_LIST_adtl:
598 if (!gst_riff_read_skip (wav)) {
599 GST_WARNING_OBJECT (wav, "could not read skip");
605 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag,
607 if (!gst_riff_read_skip (wav)) {
608 GST_WARNING_OBJECT (wav, "could not read skip");
616 case GST_RIFF_TAG_data:
617 if (!gst_bytestream_flush (wav->bs, 8)) {
618 GST_WARNING_OBJECT (wav, "could not flush 8 bytes");
622 GST_DEBUG_OBJECT (wav, "switching to data mode");
623 wav->state = GST_WAVPARSE_DATA;
624 wav->datastart = gst_bytestream_tell (wav->bs);
628 /* length is 0, data probably stretches to the end
630 GST_DEBUG_OBJECT (wav, "length is 0 trying to find length");
631 /* get length of file */
632 file_length = gst_bytestream_length (wav->bs);
633 if (file_length == -1) {
634 GST_DEBUG_OBJECT (wav,
635 "could not get file length, assuming data to eof");
636 /* could not get length, assuming till eof */
637 length = G_MAXUINT32;
639 if (file_length > G_MAXUINT32) {
640 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
641 ", clipping to 32 bits", file_length);
642 /* could not get length, assuming till eof */
643 length = G_MAXUINT32;
645 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
646 ", datalength %u", file_length, length);
647 /* substract offset of datastart from length */
648 length = file_length - wav->datastart;
649 GST_DEBUG_OBJECT (wav, "datalength %u", length);
652 wav->datasize = (guint64) length;
653 GST_DEBUG_OBJECT (wav, "datasize = %ld", length)
656 case GST_RIFF_TAG_cue:
657 if (!gst_riff_read_skip (wav)) {
658 GST_WARNING_OBJECT (wav, "could not read skip");
664 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag, (gchar *) & tag);
665 if (!gst_riff_read_skip (wav))
676 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
680 if (!gst_riff_parse_file_header (element, buf, &doctype))
683 if (doctype != GST_RIFF_RIFF_WAVE)
691 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
692 ("File is not a WAVE file: %" GST_FOURCC_FORMAT,
693 GST_FOURCC_ARGS (doctype)));
699 gst_wavparse_stream_init (GstWavParse * wav)
702 GstBuffer *buf = NULL;
704 if ((res = gst_pad_pull_range (wav->sinkpad,
705 wav->offset, 12, &buf)) != GST_FLOW_OK)
707 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
708 return GST_FLOW_ERROR;
716 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
718 /* -1 always maps to -1 */
724 /* 0 always maps to 0 */
731 *bytepos = uint64_ceiling_scale (ts, (guint64) wav->bps, GST_SECOND);
733 } else if (wav->fact) {
735 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
736 *bytepos = uint64_ceiling_scale (ts, bps, GST_SECOND);
743 /* This function is used to perform seeks on the element.
745 * It also works when event is NULL, in which case it will just
746 * start from the last configured segment. This technique is
747 * used when activating the element and to perform the seek in
751 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
755 GstFormat format, bformat;
757 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
758 gint64 cur, stop, upstream_size;
761 GstSegment seeksegment = { 0, };
765 GST_DEBUG_OBJECT (wav, "doing seek with event");
767 gst_event_parse_seek (event, &rate, &format, &flags,
768 &cur_type, &cur, &stop_type, &stop);
770 /* no negative rates yet */
774 if (format != wav->segment.format) {
775 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
776 gst_format_get_name (format),
777 gst_format_get_name (wav->segment.format));
779 if (cur_type != GST_SEEK_TYPE_NONE)
781 gst_pad_query_convert (wav->srcpad, format, cur,
782 wav->segment.format, &cur);
783 if (res && stop_type != GST_SEEK_TYPE_NONE)
785 gst_pad_query_convert (wav->srcpad, format, stop,
786 wav->segment.format, &stop);
790 format = wav->segment.format;
793 GST_DEBUG_OBJECT (wav, "doing seek without event");
796 cur_type = GST_SEEK_TYPE_SET;
797 stop_type = GST_SEEK_TYPE_SET;
800 /* in push mode, we must delegate to upstream */
801 if (wav->streaming) {
802 gboolean res = FALSE;
804 /* if streaming not yet started; only prepare initial newsegment */
805 if (!event || wav->state != GST_WAVPARSE_DATA) {
806 if (wav->start_segment)
807 gst_event_unref (wav->start_segment);
809 /* wav->start_segment =
810 gst_event_new_new_segment (FALSE, wav->segment.rate,
811 wav->segment.format, wav->segment.last_stop, wav->segment.duration,
812 wav->segment.last_stop);*/
815 /* convert seek positions to byte positions in data sections */
816 if (format == GST_FORMAT_TIME) {
817 /* should not fail */
818 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
820 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
823 /* mind sample boundary and header */
825 cur -= (cur % wav->bytes_per_sample);
826 cur += wav->datastart;
829 stop -= (stop % wav->bytes_per_sample);
830 stop += wav->datastart;
832 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
833 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
835 /* BYTE seek event */
836 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
838 res = gst_pad_push_event (wav->sinkpad, event);
844 flush = flags & GST_SEEK_FLAG_FLUSH;
846 /* now we need to make sure the streaming thread is stopped. We do this by
847 * either sending a FLUSH_START event downstream which will cause the
848 * streaming thread to stop with a WRONG_STATE.
849 * For a non-flushing seek we simply pause the task, which will happen as soon
850 * as it completes one iteration (and thus might block when the sink is
851 * blocking in preroll). */
854 GST_DEBUG_OBJECT (wav, "sending flush start");
855 gst_pad_push_event (wav->srcpad, gst_event_new_flush_start ());
858 gst_pad_pause_task (wav->sinkpad);
861 /* we should now be able to grab the streaming thread because we stopped it
862 * with the above flush/pause code */
863 GST_PAD_STREAM_LOCK (wav->sinkpad);
865 /* save current position */
866 last_stop = wav->segment.position;
868 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
870 /* copy segment, we need this because we still need the old
871 * segment when we close the current segment. */
872 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
874 /* configure the seek parameters in the seeksegment. We will then have the
875 * right values in the segment to perform the seek */
877 GST_DEBUG_OBJECT (wav, "configuring seek");
878 gst_segment_do_seek (&seeksegment, rate, format, flags,
879 cur_type, cur, stop_type, stop, &update);
882 /* figure out the last position we need to play. If it's configured (stop !=
883 * -1), use that, else we play until the total duration of the file */
884 if ((stop = seeksegment.stop) == -1)
885 stop = seeksegment.duration;
887 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
888 if ((cur_type != GST_SEEK_TYPE_NONE)) {
889 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
890 * we can just copy the last_stop. If not, we use the bps to convert TIME to
892 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
893 (gint64 *) & wav->offset))
894 wav->offset = seeksegment.position;
895 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
896 wav->offset -= (wav->offset % wav->bytes_per_sample);
897 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
898 wav->offset += wav->datastart;
899 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
901 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
905 if (stop_type != GST_SEEK_TYPE_NONE) {
906 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
907 wav->end_offset = stop;
908 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
909 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
910 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
911 wav->end_offset += wav->datastart;
912 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
914 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
918 /* make sure filesize is not exceeded due to rounding errors or so,
919 * same precaution as in _stream_headers */
920 bformat = GST_FORMAT_BYTES;
921 if (gst_pad_query_peer_duration (wav->sinkpad, bformat, &upstream_size))
922 wav->end_offset = MIN (wav->end_offset, upstream_size);
924 /* this is the range of bytes we will use for playback */
925 wav->offset = MIN (wav->offset, wav->end_offset);
926 wav->dataleft = wav->end_offset - wav->offset;
928 GST_DEBUG_OBJECT (wav,
929 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
930 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
931 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
933 /* prepare for streaming again */
936 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
937 GST_DEBUG_OBJECT (wav, "sending flush stop");
938 gst_pad_push_event (wav->srcpad, gst_event_new_flush_stop (TRUE));
942 /* now we did the seek and can activate the new segment values */
943 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
945 /* if we're doing a segment seek, post a SEGMENT_START message */
946 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
947 gst_element_post_message (GST_ELEMENT_CAST (wav),
948 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
949 wav->segment.format, wav->segment.position));
952 /* now create the newsegment */
953 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
954 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
956 /* store the newsegment event so it can be sent from the streaming thread. */
957 if (wav->start_segment)
958 gst_event_unref (wav->start_segment);
959 wav->start_segment = gst_event_new_segment (&wav->segment);
961 /* mark discont if we are going to stream from another position. */
962 if (last_stop != wav->segment.position) {
963 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
967 /* and start the streaming task again */
968 if (!wav->streaming) {
969 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
973 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
980 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
985 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
990 GST_DEBUG_OBJECT (wav,
991 "Could not determine byte position for desired time");
997 * gst_wavparse_peek_chunk_info:
998 * @wav Wavparse object
999 * @tag holder for tag
1000 * @size holder for tag size
1002 * Peek next chunk info (tag and size)
1004 * Returns: %TRUE when the chunk info (header) is available
1007 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
1009 const guint8 *data = NULL;
1011 if (gst_adapter_available (wav->adapter) < 8)
1014 data = gst_adapter_map (wav->adapter, 8);
1015 *tag = GST_READ_UINT32_LE (data);
1016 *size = GST_READ_UINT32_LE (data + 4);
1017 gst_adapter_unmap (wav->adapter, 0);
1019 GST_DEBUG ("Next chunk size is %d bytes, type %" GST_FOURCC_FORMAT, *size,
1020 GST_FOURCC_ARGS (*tag));
1026 * gst_wavparse_peek_chunk:
1027 * @wav Wavparse object
1028 * @tag holder for tag
1029 * @size holder for tag size
1031 * Peek enough data for one full chunk
1033 * Returns: %TRUE when the full chunk is available
1036 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
1038 guint32 peek_size = 0;
1041 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
1044 /* size 0 -> empty data buffer would surprise most callers,
1045 * large size -> do not bother trying to squeeze that into adapter,
1046 * so we throw poor man's exception, which can be caught if caller really
1047 * wants to handle 0 size chunk */
1048 if (!(*size) || (*size) >= (1 << 30)) {
1049 GST_INFO ("Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT,
1050 *size, GST_FOURCC_ARGS (*tag));
1051 /* chain should give up */
1052 wav->abort_buffering = TRUE;
1055 peek_size = (*size + 1) & ~1;
1056 available = gst_adapter_available (wav->adapter);
1058 if (available >= (8 + peek_size)) {
1061 GST_LOG ("but only %u bytes available now", available);
1067 * gst_wavparse_calculate_duration:
1068 * @wav: wavparse object
1070 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
1073 * Returns: %TRUE if duration is available.
1076 gst_wavparse_calculate_duration (GstWavParse * wav)
1078 if (wav->duration > 0)
1082 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
1084 uint64_ceiling_scale (wav->datasize, GST_SECOND, (guint64) wav->bps);
1085 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
1086 GST_TIME_ARGS (wav->duration));
1088 } else if (wav->fact) {
1089 wav->duration = uint64_ceiling_scale_int (GST_SECOND, wav->fact, wav->rate);
1090 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
1091 GST_TIME_ARGS (wav->duration));
1098 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
1103 if (wav->streaming) {
1104 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1107 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
1108 GST_FOURCC_ARGS (tag));
1109 flush = 8 + ((size + 1) & ~1);
1110 wav->offset += flush;
1111 if (wav->streaming) {
1112 gst_adapter_flush (wav->adapter, flush);
1114 gst_buffer_unref (buf);
1120 #define MAX_BUFFER_SIZE 4096
1122 static GstFlowReturn
1123 gst_wavparse_stream_headers (GstWavParse * wav)
1125 GstFlowReturn res = GST_FLOW_OK;
1126 GstBuffer *buf = NULL;
1127 gst_riff_strf_auds *header = NULL;
1129 gboolean gotdata = FALSE;
1130 GstCaps *caps = NULL;
1131 gchar *codec_name = NULL;
1133 gint64 upstream_size = 0;
1135 /* search for "_fmt" chunk, which should be first */
1136 while (!wav->got_fmt) {
1139 /* The header starts with a 'fmt ' tag */
1140 if (wav->streaming) {
1141 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1144 gst_adapter_flush (wav->adapter, 8);
1148 buf = gst_adapter_take_buffer (wav->adapter, size);
1150 gst_adapter_flush (wav->adapter, 1);
1151 wav->offset += GST_ROUND_UP_2 (size);
1153 buf = gst_buffer_new ();
1156 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1157 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1161 if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ ||
1162 tag == GST_RIFF_TAG_bext || tag == GST_RIFF_TAG_BEXT ||
1163 tag == GST_RIFF_TAG_LIST) {
1164 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1165 GST_FOURCC_ARGS (tag));
1166 gst_buffer_unref (buf);
1171 if (tag != GST_RIFF_TAG_fmt)
1174 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1176 goto parse_header_error;
1178 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1180 /* do sanity checks of header fields */
1181 if (header->channels == 0)
1183 if (header->rate == 0)
1186 GST_DEBUG_OBJECT (wav, "creating the caps");
1188 /* Note: gst_riff_create_audio_caps might need to fix values in
1189 * the header header depending on the format, so call it first */
1190 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1194 gst_buffer_unref (extra);
1197 goto unknown_format;
1199 /* do more sanity checks of header fields
1200 * (these can be sanitized by gst_riff_create_audio_caps()
1202 wav->format = header->format;
1203 wav->rate = header->rate;
1204 wav->channels = header->channels;
1205 wav->blockalign = header->blockalign;
1206 wav->depth = header->size;
1207 wav->av_bps = header->av_bps;
1213 /* do format specific handling */
1214 switch (wav->format) {
1215 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1216 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1218 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1219 * bitrate inside the mpeg stream */
1220 GST_INFO ("resetting bps from %d to 0 for mp2/3", wav->av_bps);
1224 case GST_RIFF_WAVE_FORMAT_PCM:
1225 if (wav->blockalign > wav->channels * (guint) ceil (wav->depth / 8.0))
1226 goto invalid_blockalign;
1229 if (wav->av_bps > wav->blockalign * wav->rate)
1231 /* use the configured bps */
1232 wav->bps = wav->av_bps;
1236 wav->width = (wav->blockalign * 8) / wav->channels;
1237 wav->bytes_per_sample = wav->channels * wav->width / 8;
1239 if (wav->bytes_per_sample <= 0)
1240 goto no_bytes_per_sample;
1242 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1243 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1244 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1245 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1246 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1247 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1248 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1250 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1251 * formats). This will make the element output a BYTE format segment and
1252 * will not timestamp the outgoing buffers.
1254 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1256 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1258 /* create pad later so we can sniff the first few bytes
1259 * of the real data and correct our caps if necessary */
1260 gst_caps_replace (&wav->caps, caps);
1261 gst_caps_replace (&caps, NULL);
1263 wav->got_fmt = TRUE;
1266 wav->tags = gst_tag_list_new_empty ();
1268 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1269 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1271 g_free (codec_name);
1277 gst_pad_query_peer_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1278 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1280 /* loop headers until we get data */
1282 if (wav->streaming) {
1283 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1289 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1290 &buf)) != GST_FLOW_OK)
1291 goto header_read_error;
1292 data = gst_buffer_map (buf, NULL, NULL, -1);
1293 tag = GST_READ_UINT32_LE (data);
1294 size = GST_READ_UINT32_LE (data + 4);
1295 gst_buffer_unmap (buf, data, -1);
1298 GST_INFO_OBJECT (wav,
1299 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT,
1300 GST_FOURCC_ARGS (tag), wav->offset);
1302 /* wav is a st00pid format, we don't know for sure where data starts.
1303 * So we have to go bit by bit until we find the 'data' header
1306 case GST_RIFF_TAG_data:{
1307 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %d", size);
1308 if (wav->streaming) {
1309 gst_adapter_flush (wav->adapter, 8);
1312 gst_buffer_unref (buf);
1315 wav->datastart = wav->offset;
1316 /* If size is zero, then the data chunk probably actually extends to
1317 the end of the file */
1318 if (size == 0 && upstream_size) {
1319 size = upstream_size - wav->datastart;
1321 /* Or the file might be truncated */
1322 else if (upstream_size) {
1323 size = MIN (size, (upstream_size - wav->datastart));
1325 wav->datasize = (guint64) size;
1326 wav->dataleft = (guint64) size;
1327 wav->end_offset = size + wav->datastart;
1328 if (!wav->streaming) {
1329 /* We will continue parsing tags 'till end */
1330 wav->offset += size;
1332 GST_DEBUG_OBJECT (wav, "datasize = %d", size);
1335 case GST_RIFF_TAG_fact:{
1336 if (wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1337 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1338 const guint data_size = 4;
1340 GST_INFO_OBJECT (wav, "Have fact chunk");
1341 if (size < data_size) {
1342 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1343 /* need more data */
1346 GST_DEBUG_OBJECT (wav, "need %d, available %d; ignoring chunk",
1350 /* number of samples (for compressed formats) */
1351 if (wav->streaming) {
1352 const guint8 *data = NULL;
1354 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1357 gst_adapter_flush (wav->adapter, 8);
1358 data = gst_adapter_map (wav->adapter, data_size);
1359 wav->fact = GST_READ_UINT32_LE (data);
1360 gst_adapter_unmap (wav->adapter, GST_ROUND_UP_2 (size));
1362 gst_buffer_unref (buf);
1364 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1365 data_size, &buf)) != GST_FLOW_OK)
1366 goto header_read_error;
1367 gst_buffer_extract (buf, 0, &wav->fact, 4);
1368 wav->fact = GUINT32_FROM_LE (wav->fact);
1369 gst_buffer_unref (buf);
1371 GST_DEBUG_OBJECT (wav, "have fact %u", wav->fact);
1372 wav->offset += 8 + GST_ROUND_UP_2 (size);
1375 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1376 /* need more data */
1382 case GST_RIFF_TAG_acid:{
1383 const gst_riff_acid *acid = NULL;
1384 const guint data_size = sizeof (gst_riff_acid);
1387 GST_INFO_OBJECT (wav, "Have acid chunk");
1388 if (size < data_size) {
1389 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1390 /* need more data */
1393 GST_DEBUG_OBJECT (wav, "need %d, available %d; ignoring chunk",
1397 if (wav->streaming) {
1398 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1401 gst_adapter_flush (wav->adapter, 8);
1402 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1404 tempo = acid->tempo;
1405 gst_adapter_unmap (wav->adapter, 0);
1407 gst_buffer_unref (buf);
1409 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1410 size, &buf)) != GST_FLOW_OK)
1411 goto header_read_error;
1412 acid = (const gst_riff_acid *) gst_buffer_map (buf, NULL, NULL,
1414 tempo = acid->tempo;
1415 gst_buffer_unmap (buf, (guint8 *) acid, -1);
1417 /* send data as tags */
1419 wav->tags = gst_tag_list_new_empty ();
1420 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1421 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1423 size = GST_ROUND_UP_2 (size);
1424 if (wav->streaming) {
1425 gst_adapter_flush (wav->adapter, size);
1427 gst_buffer_unref (buf);
1429 wav->offset += 8 + size;
1432 /* FIXME: all list tags after data are ignored in streaming mode */
1433 case GST_RIFF_TAG_LIST:{
1436 if (wav->streaming) {
1437 const guint8 *data = NULL;
1439 if (gst_adapter_available (wav->adapter) < 12) {
1442 data = gst_adapter_map (wav->adapter, 12);
1443 ltag = GST_READ_UINT32_LE (data + 8);
1444 gst_adapter_unmap (wav->adapter, 0);
1446 gst_buffer_unref (buf);
1448 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1449 &buf)) != GST_FLOW_OK)
1450 goto header_read_error;
1451 gst_buffer_extract (buf, 8, <ag, 4);
1452 ltag = GUINT32_FROM_LE (ltag);
1455 case GST_RIFF_LIST_INFO:{
1456 const gint data_size = size - 4;
1459 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1460 if (wav->streaming) {
1461 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1464 gst_adapter_flush (wav->adapter, 12);
1466 if (data_size > 0) {
1467 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1469 gst_adapter_flush (wav->adapter, 1);
1473 gst_buffer_unref (buf);
1474 if (data_size > 0) {
1476 gst_pad_pull_range (wav->sinkpad, wav->offset,
1477 data_size, &buf)) != GST_FLOW_OK)
1478 goto header_read_error;
1481 if (data_size > 0) {
1483 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1485 GstTagList *old = wav->tags;
1487 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1489 gst_tag_list_free (old);
1490 gst_tag_list_free (new);
1492 gst_buffer_unref (buf);
1493 wav->offset += GST_ROUND_UP_2 (data_size);
1498 GST_INFO_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1499 GST_FOURCC_ARGS (ltag));
1500 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1501 /* need more data */
1508 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1509 /* need more data */
1514 if (upstream_size && (wav->offset >= upstream_size)) {
1515 /* Now we are gone through the whole file */
1520 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1522 if (wav->bps <= 0 && wav->fact) {
1524 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1526 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1527 (guint64) wav->fact);
1528 GST_INFO_OBJECT (wav, "calculated bps : %d, enabling VBR", wav->bps);
1533 if (gst_wavparse_calculate_duration (wav)) {
1534 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1535 wav->segment.duration = wav->duration;
1537 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1538 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1539 wav->segment.duration = wav->datasize;
1542 /* now we have all the info to perform a pending seek if any, if no
1543 * event, this will still do the right thing and it will also send
1544 * the right newsegment event downstream. */
1545 gst_wavparse_perform_seek (wav, wav->seek_event);
1546 /* remove pending event */
1547 event_p = &wav->seek_event;
1548 gst_event_replace (event_p, NULL);
1550 /* we just started, we are discont */
1551 wav->discont = TRUE;
1553 wav->state = GST_WAVPARSE_DATA;
1555 /* determine reasonable max buffer size,
1556 * that is, buffers not too small either size or time wise
1557 * so we do not end up with too many of them */
1560 gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size);
1561 wav->max_buf_size = upstream_size;
1562 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1563 if (wav->blockalign > 0)
1564 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1566 GST_DEBUG_OBJECT (wav, "max buffer size %d", wav->max_buf_size);
1574 g_free (codec_name);
1578 gst_caps_unref (caps);
1583 res = GST_FLOW_ERROR;
1588 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1589 ("Invalid WAV header (no fmt at start): %"
1590 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1595 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1596 ("Couldn't parse audio header"));
1601 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1602 ("Stream claims to contain no channels - invalid data"));
1607 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1608 ("Stream with sample_rate == 0 - invalid data"));
1613 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1614 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1615 wav->blockalign, wav->channels * (guint) ceil (wav->depth / 8.0)));
1620 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1621 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1622 wav->av_bps, wav->blockalign * wav->rate));
1625 no_bytes_per_sample:
1627 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1628 ("Could not caluclate bytes per sample - invalid data"));
1633 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1634 ("No caps found for format 0x%x, %d channels, %d Hz",
1635 wav->format, wav->channels, wav->rate));
1640 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1641 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1647 * Read WAV file tag when streaming
1649 static GstFlowReturn
1650 gst_wavparse_parse_stream_init (GstWavParse * wav)
1652 if (gst_adapter_available (wav->adapter) >= 12) {
1655 /* _take flushes the data */
1656 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1658 GST_DEBUG ("Parsing wav header");
1659 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1660 return GST_FLOW_ERROR;
1663 /* Go to next state */
1664 wav->state = GST_WAVPARSE_HEADER;
1669 /* handle an event sent directly to the element.
1671 * This event can be sent either in the READY state or the
1672 * >READY state. The only event of interest really is the seek
1675 * In the READY state we can only store the event and try to
1676 * respect it when going to PAUSED. We assume we are in the
1677 * READY state when our parsing state != GST_WAVPARSE_DATA.
1679 * When we are steaming, we can simply perform the seek right
1683 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1685 GstWavParse *wav = GST_WAVPARSE (element);
1686 gboolean res = FALSE;
1689 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1691 switch (GST_EVENT_TYPE (event)) {
1692 case GST_EVENT_SEEK:
1693 if (wav->state == GST_WAVPARSE_DATA) {
1694 /* we can handle the seek directly when streaming data */
1695 res = gst_wavparse_perform_seek (wav, event);
1697 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1699 event_p = &wav->seek_event;
1700 gst_event_replace (event_p, event);
1702 /* we always return true */
1709 gst_event_unref (event);
1714 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1718 s = gst_caps_get_structure (caps, 0);
1719 if (!gst_structure_has_name (s, "audio/x-dts"))
1721 if (prob >= GST_TYPE_FIND_LIKELY)
1723 /* DTS at non-0 offsets and without second sync may yield POSSIBLE .. */
1724 if (prob < GST_TYPE_FIND_POSSIBLE)
1726 /* .. in which case we want at least a valid-looking rate and channels */
1727 if (!gst_structure_has_field (s, "channels"))
1729 /* and for extra assurance we could also check the rate from the DTS frame
1730 * against the one in the wav header, but for now let's not do that */
1731 return gst_structure_has_field (s, "rate");
1735 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1739 GST_DEBUG_OBJECT (wav, "adding src pad");
1742 s = gst_caps_get_structure (wav->caps, 0);
1743 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
1744 GstTypeFindProbability prob;
1747 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1748 if (tf_caps != NULL) {
1749 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1750 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1751 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1752 gst_caps_unref (wav->caps);
1753 wav->caps = tf_caps;
1755 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1756 GST_TAG_AUDIO_CODEC, "dts", NULL);
1758 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1759 "marked as raw PCM audio, but ignoring for now", tf_caps);
1760 gst_caps_unref (tf_caps);
1766 gst_pad_set_caps (wav->srcpad, wav->caps);
1767 gst_caps_replace (&wav->caps, NULL);
1769 if (wav->start_segment) {
1770 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1771 gst_pad_push_event (wav->srcpad, wav->start_segment);
1772 wav->start_segment = NULL;
1776 gst_pad_push_event (wav->srcpad, gst_event_new_tag (wav->tags));
1781 static GstFlowReturn
1782 gst_wavparse_stream_data (GstWavParse * wav)
1784 GstBuffer *buf = NULL;
1785 GstFlowReturn res = GST_FLOW_OK;
1786 guint64 desired, obtained;
1787 GstClockTime timestamp, next_timestamp, duration;
1788 guint64 pos, nextpos;
1791 GST_LOG_OBJECT (wav,
1792 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1793 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1795 /* Get the next n bytes and output them */
1796 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1799 /* scale the amount of data by the segment rate so we get equal
1800 * amounts of data regardless of the playback rate */
1802 MIN (gst_guint64_to_gdouble (wav->dataleft),
1803 wav->max_buf_size * ABS (wav->segment.rate));
1805 if (desired >= wav->blockalign && wav->blockalign > 0)
1806 desired -= (desired % wav->blockalign);
1808 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1809 "from the sinkpad", desired);
1811 if (wav->streaming) {
1812 guint avail = gst_adapter_available (wav->adapter);
1815 /* flush some bytes if evil upstream sends segment that starts
1816 * before data or does is not send sample aligned segment */
1817 if (G_LIKELY (wav->offset >= wav->datastart)) {
1818 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1820 extra = wav->datastart - wav->offset;
1823 if (G_UNLIKELY (extra)) {
1824 extra = wav->bytes_per_sample - extra;
1825 if (extra <= avail) {
1826 GST_DEBUG_OBJECT (wav, "flushing %d bytes to sample boundary", extra);
1827 gst_adapter_flush (wav->adapter, extra);
1828 wav->offset += extra;
1829 wav->dataleft -= extra;
1830 goto iterate_adapter;
1832 GST_DEBUG_OBJECT (wav, "flushing %d bytes", avail);
1833 gst_adapter_clear (wav->adapter);
1834 wav->offset += avail;
1835 wav->dataleft -= avail;
1840 if (avail < desired) {
1841 GST_LOG_OBJECT (wav, "Got only %d bytes of data from the sinkpad", avail);
1845 buf = gst_adapter_take_buffer (wav->adapter, desired);
1847 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
1848 desired, &buf)) != GST_FLOW_OK)
1851 /* we may get a short buffer at the end of the file */
1852 if (gst_buffer_get_size (buf) < desired) {
1853 gsize size = gst_buffer_get_size (buf);
1855 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
1856 if (size >= wav->blockalign) {
1857 buf = gst_buffer_make_writable (buf);
1858 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
1860 gst_buffer_unref (buf);
1866 obtained = gst_buffer_get_size (buf);
1868 /* our positions in bytes */
1869 pos = wav->offset - wav->datastart;
1870 nextpos = pos + obtained;
1872 /* update offsets, does not overflow. */
1873 buf = gst_buffer_make_writable (buf);
1874 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
1875 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
1877 /* first chunk of data? create the source pad. We do this only here so
1878 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
1879 if (G_UNLIKELY (wav->first)) {
1881 /* this will also push the segment events */
1882 gst_wavparse_add_src_pad (wav, buf);
1884 /* If we have a pending start segment, send it now. */
1885 if (G_UNLIKELY (wav->start_segment != NULL)) {
1886 gst_pad_push_event (wav->srcpad, wav->start_segment);
1887 wav->start_segment = NULL;
1892 /* and timestamps if we have a bitrate, be careful for overflows */
1893 timestamp = uint64_ceiling_scale (pos, GST_SECOND, (guint64) wav->bps);
1895 uint64_ceiling_scale (nextpos, GST_SECOND, (guint64) wav->bps);
1896 duration = next_timestamp - timestamp;
1898 /* update current running segment position */
1899 if (G_LIKELY (next_timestamp >= wav->segment.start))
1900 wav->segment.position = next_timestamp;
1901 } else if (wav->fact) {
1903 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
1904 /* and timestamps if we have a bitrate, be careful for overflows */
1905 timestamp = uint64_ceiling_scale (pos, GST_SECOND, bps);
1906 next_timestamp = uint64_ceiling_scale (nextpos, GST_SECOND, bps);
1907 duration = next_timestamp - timestamp;
1909 /* no bitrate, all we know is that the first sample has timestamp 0, all
1910 * other positions and durations have unknown timestamp. */
1914 timestamp = GST_CLOCK_TIME_NONE;
1915 duration = GST_CLOCK_TIME_NONE;
1916 /* update current running segment position with byte offset */
1917 if (G_LIKELY (nextpos >= wav->segment.start))
1918 wav->segment.position = nextpos;
1920 if ((pos > 0) && wav->vbr) {
1921 /* don't set timestamps for VBR files if it's not the first buffer */
1922 timestamp = GST_CLOCK_TIME_NONE;
1923 duration = GST_CLOCK_TIME_NONE;
1926 GST_DEBUG_OBJECT (wav, "marking DISCONT");
1927 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1928 wav->discont = FALSE;
1931 GST_BUFFER_TIMESTAMP (buf) = timestamp;
1932 GST_BUFFER_DURATION (buf) = duration;
1934 GST_LOG_OBJECT (wav,
1935 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
1936 ", size:%u", GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration),
1937 gst_buffer_get_size (buf));
1939 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
1942 if (obtained < wav->dataleft) {
1943 wav->offset += obtained;
1944 wav->dataleft -= obtained;
1946 wav->offset += wav->dataleft;
1950 /* Iterate until need more data, so adapter size won't grow */
1951 if (wav->streaming) {
1952 GST_LOG_OBJECT (wav,
1953 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
1955 goto iterate_adapter;
1962 GST_DEBUG_OBJECT (wav, "found EOS");
1963 return GST_FLOW_UNEXPECTED;
1967 /* check if we got EOS */
1968 if (res == GST_FLOW_UNEXPECTED)
1971 GST_WARNING_OBJECT (wav,
1972 "Error getting %" G_GINT64_FORMAT " bytes from the "
1973 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
1978 GST_INFO_OBJECT (wav,
1979 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
1980 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
1981 gst_pad_is_linked (wav->srcpad));
1987 gst_wavparse_loop (GstPad * pad)
1990 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
1992 GST_LOG_OBJECT (wav, "process data");
1994 switch (wav->state) {
1995 case GST_WAVPARSE_START:
1996 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
1997 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2000 wav->state = GST_WAVPARSE_HEADER;
2003 case GST_WAVPARSE_HEADER:
2004 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2005 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2008 wav->state = GST_WAVPARSE_DATA;
2009 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2012 case GST_WAVPARSE_DATA:
2013 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2017 g_assert_not_reached ();
2024 const gchar *reason = gst_flow_get_name (ret);
2026 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2027 gst_pad_pause_task (pad);
2029 if (ret == GST_FLOW_UNEXPECTED) {
2030 /* handle end-of-stream/segment */
2031 /* so align our position with the end of it, if there is one
2032 * this ensures a subsequent will arrive at correct base/acc time */
2033 if (wav->segment.format == GST_FORMAT_TIME) {
2034 if (wav->segment.rate > 0.0 &&
2035 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2036 wav->segment.position = wav->segment.stop;
2037 else if (wav->segment.rate < 0.0)
2038 wav->segment.position = wav->segment.start;
2040 /* add pad before we perform EOS */
2041 if (G_UNLIKELY (wav->first)) {
2043 gst_wavparse_add_src_pad (wav, NULL);
2046 if (wav->state == GST_WAVPARSE_START)
2047 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2048 ("No valid input found before end of stream"), (NULL));
2050 /* perform EOS logic */
2051 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2054 if ((stop = wav->segment.stop) == -1)
2055 stop = wav->segment.duration;
2057 gst_element_post_message (GST_ELEMENT_CAST (wav),
2058 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2059 wav->segment.format, stop));
2061 if (wav->srcpad != NULL)
2062 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2064 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2065 /* for fatal errors we post an error message, post the error
2066 * first so the app knows about the error first. */
2067 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
2068 (_("Internal data flow error.")),
2069 ("streaming task paused, reason %s (%d)", reason, ret));
2070 if (wav->srcpad != NULL)
2071 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2077 static GstFlowReturn
2078 gst_wavparse_chain (GstPad * pad, GstBuffer * buf)
2081 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2083 GST_LOG_OBJECT (wav, "adapter_push %u bytes", gst_buffer_get_size (buf));
2085 gst_adapter_push (wav->adapter, buf);
2087 switch (wav->state) {
2088 case GST_WAVPARSE_START:
2089 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2090 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2093 if (wav->state != GST_WAVPARSE_HEADER)
2096 /* otherwise fall-through */
2097 case GST_WAVPARSE_HEADER:
2098 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2099 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2102 if (!wav->got_fmt || wav->datastart == 0)
2105 wav->state = GST_WAVPARSE_DATA;
2106 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2109 case GST_WAVPARSE_DATA:
2110 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2111 wav->discont = TRUE;
2112 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2116 g_return_val_if_reached (GST_FLOW_ERROR);
2119 if (G_UNLIKELY (wav->abort_buffering)) {
2120 wav->abort_buffering = FALSE;
2121 ret = GST_FLOW_ERROR;
2122 /* sort of demux/parse error */
2123 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2129 static GstFlowReturn
2130 gst_wavparse_flush_data (GstWavParse * wav)
2132 GstFlowReturn ret = GST_FLOW_OK;
2135 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2137 wav->end_offset = wav->offset + av;
2138 ret = gst_wavparse_stream_data (wav);
2145 gst_wavparse_sink_event (GstPad * pad, GstEvent * event)
2147 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2148 gboolean ret = TRUE;
2150 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2152 switch (GST_EVENT_TYPE (event)) {
2153 case GST_EVENT_CAPS:
2155 /* discard, we'll come up with proper src caps */
2156 gst_event_unref (event);
2159 case GST_EVENT_SEGMENT:
2161 gint64 start, stop, offset = 0, end_offset = -1;
2164 /* some debug output */
2165 gst_event_copy_segment (event, &segment);
2166 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2169 if (wav->state != GST_WAVPARSE_DATA) {
2170 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2174 /* now we are either committed to TIME or BYTE format,
2175 * and we only expect a BYTE segment, e.g. following a seek */
2176 if (segment.format == GST_FORMAT_BYTES) {
2177 /* handle (un)signed issues */
2178 start = segment.start;
2179 stop = segment.stop;
2182 start -= wav->datastart;
2183 start = MAX (start, 0);
2187 segment.stop -= wav->datastart;
2188 segment.stop = MAX (stop, 0);
2190 if (wav->segment.format == GST_FORMAT_TIME) {
2191 guint64 bps = wav->bps;
2193 /* operating in format TIME, so we can convert */
2194 if (!bps && wav->fact)
2196 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2200 uint64_ceiling_scale (start, GST_SECOND, (guint64) wav->bps);
2203 uint64_ceiling_scale (stop, GST_SECOND, (guint64) wav->bps);
2207 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2211 segment.start = start;
2212 segment.stop = stop;
2214 /* accept upstream's notion of segment and distribute along */
2215 segment.time = segment.start = segment.position;
2216 segment.duration = wav->segment.duration;
2217 segment.base = gst_segment_to_running_time (&wav->segment,
2218 GST_FORMAT_TIME, wav->segment.position);
2220 gst_segment_copy_into (&segment, &wav->segment);
2222 /* also store the newsegment event for the streaming thread */
2223 if (wav->start_segment)
2224 gst_event_unref (wav->start_segment);
2225 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2226 wav->start_segment = gst_event_new_segment (&segment);
2228 /* stream leftover data in current segment */
2229 gst_wavparse_flush_data (wav);
2230 /* and set up streaming thread for next one */
2231 wav->offset = offset;
2232 wav->end_offset = end_offset;
2233 if (wav->end_offset > 0) {
2234 wav->dataleft = wav->end_offset - wav->offset;
2236 /* infinity; upstream will EOS when done */
2237 wav->dataleft = G_MAXUINT64;
2240 gst_event_unref (event);
2244 /* add pad if needed so EOS is seen downstream */
2245 if (G_UNLIKELY (wav->first)) {
2247 gst_wavparse_add_src_pad (wav, NULL);
2249 /* stream leftover data in current segment */
2250 gst_wavparse_flush_data (wav);
2253 if (wav->state == GST_WAVPARSE_START)
2254 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2255 ("No valid input found before end of stream"), (NULL));
2258 case GST_EVENT_FLUSH_STOP:
2262 gst_adapter_clear (wav->adapter);
2263 wav->discont = TRUE;
2264 dur = wav->segment.duration;
2265 gst_segment_init (&wav->segment, wav->segment.format);
2266 wav->segment.duration = dur;
2270 ret = gst_pad_event_default (wav->sinkpad, event);
2278 /* convert and query stuff */
2279 static const GstFormat *
2280 gst_wavparse_get_formats (GstPad * pad)
2282 static GstFormat formats[] = {
2285 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2294 gst_wavparse_pad_convert (GstPad * pad,
2295 GstFormat src_format, gint64 src_value,
2296 GstFormat * dest_format, gint64 * dest_value)
2298 GstWavParse *wavparse;
2299 gboolean res = TRUE;
2301 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2303 if (*dest_format == src_format) {
2304 *dest_value = src_value;
2308 if ((wavparse->bps == 0) && !wavparse->fact)
2311 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2312 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2314 switch (src_format) {
2315 case GST_FORMAT_BYTES:
2316 switch (*dest_format) {
2317 case GST_FORMAT_DEFAULT:
2318 *dest_value = src_value / wavparse->bytes_per_sample;
2319 /* make sure we end up on a sample boundary */
2320 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2322 case GST_FORMAT_TIME:
2323 /* src_value + datastart = offset */
2324 GST_INFO_OBJECT (wavparse,
2325 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2327 if (wavparse->bps > 0)
2328 *dest_value = uint64_ceiling_scale (src_value, GST_SECOND,
2329 (guint64) wavparse->bps);
2330 else if (wavparse->fact) {
2331 guint64 bps = uint64_ceiling_scale_int (wavparse->datasize,
2332 wavparse->rate, wavparse->fact);
2334 *dest_value = uint64_ceiling_scale_int (src_value, GST_SECOND, bps);
2345 case GST_FORMAT_DEFAULT:
2346 switch (*dest_format) {
2347 case GST_FORMAT_BYTES:
2348 *dest_value = src_value * wavparse->bytes_per_sample;
2350 case GST_FORMAT_TIME:
2351 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2352 (guint64) wavparse->rate);
2360 case GST_FORMAT_TIME:
2361 switch (*dest_format) {
2362 case GST_FORMAT_BYTES:
2363 if (wavparse->bps > 0)
2364 *dest_value = gst_util_uint64_scale (src_value,
2365 (guint64) wavparse->bps, GST_SECOND);
2367 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2368 wavparse->rate, wavparse->fact);
2370 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2372 /* make sure we end up on a sample boundary */
2373 *dest_value -= *dest_value % wavparse->blockalign;
2375 case GST_FORMAT_DEFAULT:
2376 *dest_value = gst_util_uint64_scale (src_value,
2377 (guint64) wavparse->rate, GST_SECOND);
2396 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2402 /* handle queries for location and length in requested format */
2404 gst_wavparse_pad_query (GstPad * pad, GstQuery * query)
2406 gboolean res = TRUE;
2407 GstWavParse *wav = GST_WAVPARSE (gst_pad_get_parent (pad));
2409 /* only if we know */
2410 if (wav->state != GST_WAVPARSE_DATA) {
2411 gst_object_unref (wav);
2415 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2417 switch (GST_QUERY_TYPE (query)) {
2418 case GST_QUERY_POSITION:
2424 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2425 curb = wav->offset - wav->datastart;
2426 gst_query_parse_position (query, &format, NULL);
2427 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2430 case GST_FORMAT_TIME:
2431 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2435 format = GST_FORMAT_BYTES;
2440 gst_query_set_position (query, format, cur);
2443 case GST_QUERY_DURATION:
2445 gint64 duration = 0;
2448 gst_query_parse_duration (query, &format, NULL);
2451 case GST_FORMAT_TIME:{
2452 if ((res = gst_wavparse_calculate_duration (wav))) {
2453 duration = wav->duration;
2458 format = GST_FORMAT_BYTES;
2459 duration = wav->datasize;
2462 gst_query_set_duration (query, format, duration);
2465 case GST_QUERY_CONVERT:
2467 gint64 srcvalue, dstvalue;
2468 GstFormat srcformat, dstformat;
2470 gst_query_parse_convert (query, &srcformat, &srcvalue,
2471 &dstformat, &dstvalue);
2472 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2473 &dstformat, &dstvalue);
2475 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2478 case GST_QUERY_SEEKING:{
2480 gboolean seekable = FALSE;
2482 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2483 if (fmt == wav->segment.format) {
2484 if (wav->streaming) {
2487 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2488 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2489 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2490 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2492 gst_query_unref (q);
2494 GST_LOG_OBJECT (wav, "looping => seekable");
2498 } else if (fmt == GST_FORMAT_TIME) {
2502 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2507 res = gst_pad_query_default (pad, query);
2510 gst_object_unref (wav);
2515 gst_wavparse_srcpad_event (GstPad * pad, GstEvent * event)
2517 GstWavParse *wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
2518 gboolean res = FALSE;
2520 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2522 switch (GST_EVENT_TYPE (event)) {
2523 case GST_EVENT_SEEK:
2524 /* can only handle events when we are in the data state */
2525 if (wavparse->state == GST_WAVPARSE_DATA) {
2526 res = gst_wavparse_perform_seek (wavparse, event);
2528 gst_event_unref (event);
2531 res = gst_pad_push_event (wavparse->sinkpad, event);
2534 gst_object_unref (wavparse);
2539 gst_wavparse_sink_activate (GstPad * sinkpad)
2541 GstWavParse *wav = GST_WAVPARSE (gst_pad_get_parent (sinkpad));
2546 gst_adapter_clear (wav->adapter);
2547 g_object_unref (wav->adapter);
2548 wav->adapter = NULL;
2551 query = gst_query_new_scheduling ();
2553 if (!gst_pad_peer_query (sinkpad, query)) {
2554 gst_query_unref (query);
2558 gst_query_parse_scheduling (query, &pull_mode, NULL, NULL, NULL, NULL, NULL);
2559 gst_query_unref (query);
2564 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2565 wav->streaming = FALSE;
2566 gst_object_unref (wav);
2567 return gst_pad_activate_pull (sinkpad, TRUE);
2571 GST_DEBUG_OBJECT (sinkpad, "activating push");
2572 wav->streaming = TRUE;
2573 wav->adapter = gst_adapter_new ();
2574 gst_object_unref (wav);
2575 return gst_pad_activate_push (sinkpad, TRUE);
2581 gst_wavparse_sink_activate_pull (GstPad * sinkpad, gboolean active)
2584 /* if we have a scheduler we can start the task */
2585 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2588 return gst_pad_stop_task (sinkpad);
2592 static GstStateChangeReturn
2593 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2595 GstStateChangeReturn ret;
2596 GstWavParse *wav = GST_WAVPARSE (element);
2598 switch (transition) {
2599 case GST_STATE_CHANGE_NULL_TO_READY:
2601 case GST_STATE_CHANGE_READY_TO_PAUSED:
2602 gst_wavparse_reset (wav);
2604 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2610 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2612 switch (transition) {
2613 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2615 case GST_STATE_CHANGE_PAUSED_TO_READY:
2616 gst_wavparse_destroy_sourcepad (wav);
2617 gst_wavparse_reset (wav);
2619 case GST_STATE_CHANGE_READY_TO_NULL:
2628 plugin_init (GstPlugin * plugin)
2632 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2636 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2639 "Parse a .wav file into raw audio",
2640 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)