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
56 #include "gstwavparse.h"
57 #include "gst/riff/riff-ids.h"
58 #include "gst/riff/riff-media.h"
59 #include <gst/base/gsttypefindhelper.h>
60 #include <gst/gst-i18n-plugin.h>
62 GST_DEBUG_CATEGORY_STATIC (wavparse_debug);
63 #define GST_CAT_DEFAULT (wavparse_debug)
65 static void gst_wavparse_dispose (GObject * object);
67 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad,
69 static gboolean gst_wavparse_sink_activate_mode (GstPad * sinkpad,
70 GstObject * parent, GstPadMode mode, gboolean active);
71 static gboolean gst_wavparse_send_event (GstElement * element,
73 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
74 GstStateChange transition);
76 static gboolean gst_wavparse_pad_query (GstPad * pad, GstObject * parent,
78 static gboolean gst_wavparse_pad_convert (GstPad * pad, GstFormat src_format,
79 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
81 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstObject * parent,
83 static gboolean gst_wavparse_sink_event (GstPad * pad, GstObject * parent,
85 static void gst_wavparse_loop (GstPad * pad);
86 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent,
89 static void gst_wavparse_set_property (GObject * object, guint prop_id,
90 const GValue * value, GParamSpec * pspec);
91 static void gst_wavparse_get_property (GObject * object, guint prop_id,
92 GValue * value, GParamSpec * pspec);
94 #define DEFAULT_IGNORE_LENGTH FALSE
102 static GstStaticPadTemplate sink_template_factory =
103 GST_STATIC_PAD_TEMPLATE ("sink",
106 GST_STATIC_CAPS ("audio/x-wav")
110 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
112 #define gst_wavparse_parent_class parent_class
113 G_DEFINE_TYPE_WITH_CODE (GstWavParse, gst_wavparse, GST_TYPE_ELEMENT,
117 gst_wavparse_class_init (GstWavParseClass * klass)
119 GstElementClass *gstelement_class;
120 GObjectClass *object_class;
121 GstPadTemplate *src_template;
123 gstelement_class = (GstElementClass *) klass;
124 object_class = (GObjectClass *) klass;
126 parent_class = g_type_class_peek_parent (klass);
128 object_class->dispose = gst_wavparse_dispose;
130 object_class->set_property = gst_wavparse_set_property;
131 object_class->get_property = gst_wavparse_get_property;
134 * GstWavParse:ignore-length
136 * This selects whether the length found in a data chunk
137 * should be ignored. This may be useful for streamed audio
138 * where the length is unknown until the end of streaming,
139 * and various software/hardware just puts some random value
140 * in there and hopes it doesn't break too much.
144 g_object_class_install_property (object_class, PROP_IGNORE_LENGTH,
145 g_param_spec_boolean ("ignore-length",
147 "Ignore length from the Wave header",
148 DEFAULT_IGNORE_LENGTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
151 gstelement_class->change_state = gst_wavparse_change_state;
152 gstelement_class->send_event = gst_wavparse_send_event;
155 gst_element_class_add_pad_template (gstelement_class,
156 gst_static_pad_template_get (&sink_template_factory));
158 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
159 GST_PAD_ALWAYS, gst_riff_create_audio_template_caps ());
160 gst_element_class_add_pad_template (gstelement_class, src_template);
162 gst_element_class_set_details_simple (gstelement_class, "WAV audio demuxer",
163 "Codec/Demuxer/Audio",
164 "Parse a .wav file into raw audio",
165 "Erik Walthinsen <omega@cse.ogi.edu>");
169 gst_wavparse_reset (GstWavParse * wav)
171 wav->state = GST_WAVPARSE_START;
173 /* These will all be set correctly in the fmt chunk */
187 wav->got_fmt = FALSE;
191 gst_event_unref (wav->seek_event);
192 wav->seek_event = NULL;
194 gst_adapter_clear (wav->adapter);
195 g_object_unref (wav->adapter);
199 gst_tag_list_free (wav->tags);
202 gst_caps_unref (wav->caps);
204 if (wav->start_segment)
205 gst_event_unref (wav->start_segment);
206 wav->start_segment = NULL;
210 gst_wavparse_dispose (GObject * object)
212 GstWavParse *wav = GST_WAVPARSE (object);
214 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
215 gst_wavparse_reset (wav);
217 G_OBJECT_CLASS (parent_class)->dispose (object);
221 gst_wavparse_init (GstWavParse * wavparse)
223 gst_wavparse_reset (wavparse);
227 gst_pad_new_from_static_template (&sink_template_factory, "sink");
228 gst_pad_set_activate_function (wavparse->sinkpad,
229 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
230 gst_pad_set_activatemode_function (wavparse->sinkpad,
231 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_mode));
232 gst_pad_set_chain_function (wavparse->sinkpad,
233 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
234 gst_pad_set_event_function (wavparse->sinkpad,
235 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
236 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
240 gst_pad_new_from_template (gst_element_class_get_pad_template
241 (GST_ELEMENT_GET_CLASS (wavparse), "src"), "src");
242 gst_pad_use_fixed_caps (wavparse->srcpad);
243 gst_pad_set_query_function (wavparse->srcpad,
244 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
245 gst_pad_set_event_function (wavparse->srcpad,
246 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
247 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
250 /* FIXME: why is that not in use? */
253 gst_wavparse_parse_adtl (GstWavParse * wavparse, int len)
256 GstByteStream *bs = wavparse->bs;
257 gst_riff_chunk *temp_chunk, chunk;
259 struct _gst_riff_labl labl, *temp_labl;
260 struct _gst_riff_ltxt ltxt, *temp_ltxt;
261 struct _gst_riff_note note, *temp_note;
264 GstPropsEntry *entry;
268 props = wavparse->metadata->properties;
272 gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
273 if (got_bytes != sizeof (gst_riff_chunk)) {
276 temp_chunk = (gst_riff_chunk *) tempdata;
278 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
279 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
281 if (chunk.size == 0) {
282 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
283 len -= sizeof (gst_riff_chunk);
288 case GST_RIFF_adtl_labl:
290 gst_bytestream_peek_bytes (bs, &tempdata,
291 sizeof (struct _gst_riff_labl));
292 if (got_bytes != sizeof (struct _gst_riff_labl)) {
296 temp_labl = (struct _gst_riff_labl *) tempdata;
297 labl.id = GUINT32_FROM_LE (temp_labl->id);
298 labl.size = GUINT32_FROM_LE (temp_labl->size);
299 labl.identifier = GUINT32_FROM_LE (temp_labl->identifier);
301 gst_bytestream_flush (bs, sizeof (struct _gst_riff_labl));
302 len -= sizeof (struct _gst_riff_labl);
304 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, labl.size - 4);
305 if (got_bytes != labl.size - 4) {
309 label_name = (char *) tempdata;
311 gst_bytestream_flush (bs, ((labl.size - 4) + 1) & ~1);
312 len -= (((labl.size - 4) + 1) & ~1);
314 new_caps = gst_caps_new ("label",
315 "application/x-gst-metadata",
316 gst_props_new ("identifier", G_TYPE_INT (labl.identifier),
317 "name", G_TYPE_STRING (label_name), NULL));
319 if (gst_props_get (props, "labels", &caps, NULL)) {
320 caps = g_list_append (caps, new_caps);
322 caps = g_list_append (NULL, new_caps);
324 entry = gst_props_entry_new ("labels", GST_PROPS_GLIST (caps));
325 gst_props_add_entry (props, entry);
330 case GST_RIFF_adtl_ltxt:
332 gst_bytestream_peek_bytes (bs, &tempdata,
333 sizeof (struct _gst_riff_ltxt));
334 if (got_bytes != sizeof (struct _gst_riff_ltxt)) {
338 temp_ltxt = (struct _gst_riff_ltxt *) tempdata;
339 ltxt.id = GUINT32_FROM_LE (temp_ltxt->id);
340 ltxt.size = GUINT32_FROM_LE (temp_ltxt->size);
341 ltxt.identifier = GUINT32_FROM_LE (temp_ltxt->identifier);
342 ltxt.length = GUINT32_FROM_LE (temp_ltxt->length);
343 ltxt.purpose = GUINT32_FROM_LE (temp_ltxt->purpose);
344 ltxt.country = GUINT16_FROM_LE (temp_ltxt->country);
345 ltxt.language = GUINT16_FROM_LE (temp_ltxt->language);
346 ltxt.dialect = GUINT16_FROM_LE (temp_ltxt->dialect);
347 ltxt.codepage = GUINT16_FROM_LE (temp_ltxt->codepage);
349 gst_bytestream_flush (bs, sizeof (struct _gst_riff_ltxt));
350 len -= sizeof (struct _gst_riff_ltxt);
352 if (ltxt.size - 20 > 0) {
353 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, ltxt.size - 20);
354 if (got_bytes != ltxt.size - 20) {
358 gst_bytestream_flush (bs, ((ltxt.size - 20) + 1) & ~1);
359 len -= (((ltxt.size - 20) + 1) & ~1);
361 label_name = (char *) tempdata;
366 new_caps = gst_caps_new ("ltxt",
367 "application/x-gst-metadata",
368 gst_props_new ("identifier", G_TYPE_INT (ltxt.identifier),
369 "name", G_TYPE_STRING (label_name),
370 "length", G_TYPE_INT (ltxt.length), NULL));
372 if (gst_props_get (props, "ltxts", &caps, NULL)) {
373 caps = g_list_append (caps, new_caps);
375 caps = g_list_append (NULL, new_caps);
377 entry = gst_props_entry_new ("ltxts", GST_PROPS_GLIST (caps));
378 gst_props_add_entry (props, entry);
383 case GST_RIFF_adtl_note:
385 gst_bytestream_peek_bytes (bs, &tempdata,
386 sizeof (struct _gst_riff_note));
387 if (got_bytes != sizeof (struct _gst_riff_note)) {
391 temp_note = (struct _gst_riff_note *) tempdata;
392 note.id = GUINT32_FROM_LE (temp_note->id);
393 note.size = GUINT32_FROM_LE (temp_note->size);
394 note.identifier = GUINT32_FROM_LE (temp_note->identifier);
396 gst_bytestream_flush (bs, sizeof (struct _gst_riff_note));
397 len -= sizeof (struct _gst_riff_note);
399 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, note.size - 4);
400 if (got_bytes != note.size - 4) {
404 gst_bytestream_flush (bs, ((note.size - 4) + 1) & ~1);
405 len -= (((note.size - 4) + 1) & ~1);
407 label_name = (char *) tempdata;
409 new_caps = gst_caps_new ("note",
410 "application/x-gst-metadata",
411 gst_props_new ("identifier", G_TYPE_INT (note.identifier),
412 "name", G_TYPE_STRING (label_name), NULL));
414 if (gst_props_get (props, "notes", &caps, NULL)) {
415 caps = g_list_append (caps, new_caps);
417 caps = g_list_append (NULL, new_caps);
419 entry = gst_props_entry_new ("notes", GST_PROPS_GLIST (caps));
420 gst_props_add_entry (props, entry);
426 g_print ("Unknown chunk: %" GST_FOURCC_FORMAT "\n",
427 GST_FOURCC_ARGS (chunk.id));
432 g_object_notify (G_OBJECT (wavparse), "metadata");
436 gst_wavparse_parse_cues (GstWavParse * wavparse, int len)
439 GstByteStream *bs = wavparse->bs;
440 struct _gst_riff_cue *temp_cue, cue;
441 struct _gst_riff_cuepoints *points;
445 GstPropsEntry *entry;
451 gst_bytestream_peek_bytes (bs, &tempdata,
452 sizeof (struct _gst_riff_cue));
453 temp_cue = (struct _gst_riff_cue *) tempdata;
455 /* fixup for our big endian friends */
456 cue.id = GUINT32_FROM_LE (temp_cue->id);
457 cue.size = GUINT32_FROM_LE (temp_cue->size);
458 cue.cuepoints = GUINT32_FROM_LE (temp_cue->cuepoints);
460 gst_bytestream_flush (bs, sizeof (struct _gst_riff_cue));
461 if (got_bytes != sizeof (struct _gst_riff_cue)) {
465 len -= sizeof (struct _gst_riff_cue);
467 /* -4 because cue.size contains the cuepoints size
468 and we've already flushed that out of the system */
469 required = cue.size - 4;
470 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, required);
471 gst_bytestream_flush (bs, ((required) + 1) & ~1);
472 if (got_bytes != required) {
476 len -= (((cue.size - 4) + 1) & ~1);
478 /* now we have an array of struct _gst_riff_cuepoints in tempdata */
479 points = (struct _gst_riff_cuepoints *) tempdata;
481 for (i = 0; i < cue.cuepoints; i++) {
484 caps = gst_caps_new ("cues",
485 "application/x-gst-metadata",
486 gst_props_new ("identifier", G_TYPE_INT (points[i].identifier),
487 "position", G_TYPE_INT (points[i].offset), NULL));
488 cues = g_list_append (cues, caps);
491 entry = gst_props_entry_new ("cues", GST_PROPS_GLIST (cues));
492 gst_props_add_entry (wavparse->metadata->properties, entry);
495 g_object_notify (G_OBJECT (wavparse), "metadata");
498 /* Read 'fmt ' header */
500 gst_wavparse_fmt (GstWavParse * wav)
502 gst_riff_strf_auds *header = NULL;
505 if (!gst_riff_read_strf_auds (wav, &header))
508 wav->format = header->format;
509 wav->rate = header->rate;
510 wav->channels = header->channels;
511 if (wav->channels == 0)
514 wav->blockalign = header->blockalign;
515 wav->width = (header->blockalign * 8) / header->channels;
516 wav->depth = header->size;
517 wav->bps = header->av_bps;
521 /* Note: gst_riff_create_audio_caps might need to fix values in
522 * the header header depending on the format, so call it first */
523 /* FIXME: Need to handle the channel reorder map */
524 caps = gst_riff_create_audio_caps (header->format, NULL, header, NULL, 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 %u, channels %u", 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 %u", tag,
580 (const 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 = gst_util_uint64_scale_ceil (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 = gst_util_uint64_scale_ceil (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). */
853 GST_DEBUG_OBJECT (wav, "sending flush start");
854 gst_pad_push_event (wav->srcpad, gst_event_new_flush_start ());
856 gst_pad_pause_task (wav->sinkpad);
859 /* we should now be able to grab the streaming thread because we stopped it
860 * with the above flush/pause code */
861 GST_PAD_STREAM_LOCK (wav->sinkpad);
863 /* save current position */
864 last_stop = wav->segment.position;
866 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
868 /* copy segment, we need this because we still need the old
869 * segment when we close the current segment. */
870 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
872 /* configure the seek parameters in the seeksegment. We will then have the
873 * right values in the segment to perform the seek */
875 GST_DEBUG_OBJECT (wav, "configuring seek");
876 gst_segment_do_seek (&seeksegment, rate, format, flags,
877 cur_type, cur, stop_type, stop, &update);
880 /* figure out the last position we need to play. If it's configured (stop !=
881 * -1), use that, else we play until the total duration of the file */
882 if ((stop = seeksegment.stop) == -1)
883 stop = seeksegment.duration;
885 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
886 if ((cur_type != GST_SEEK_TYPE_NONE)) {
887 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
888 * we can just copy the last_stop. If not, we use the bps to convert TIME to
890 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
891 (gint64 *) & wav->offset))
892 wav->offset = seeksegment.position;
893 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
894 wav->offset -= (wav->offset % wav->bytes_per_sample);
895 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
896 wav->offset += wav->datastart;
897 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
899 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
903 if (stop_type != GST_SEEK_TYPE_NONE) {
904 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
905 wav->end_offset = stop;
906 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
907 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
908 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
909 wav->end_offset += wav->datastart;
910 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
912 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
916 /* make sure filesize is not exceeded due to rounding errors or so,
917 * same precaution as in _stream_headers */
918 bformat = GST_FORMAT_BYTES;
919 if (gst_pad_peer_query_duration (wav->sinkpad, bformat, &upstream_size))
920 wav->end_offset = MIN (wav->end_offset, upstream_size);
922 /* this is the range of bytes we will use for playback */
923 wav->offset = MIN (wav->offset, wav->end_offset);
924 wav->dataleft = wav->end_offset - wav->offset;
926 GST_DEBUG_OBJECT (wav,
927 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
928 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
929 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
931 /* prepare for streaming again */
933 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
934 GST_DEBUG_OBJECT (wav, "sending flush stop");
935 gst_pad_push_event (wav->srcpad, gst_event_new_flush_stop (TRUE));
938 /* now we did the seek and can activate the new segment values */
939 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
941 /* if we're doing a segment seek, post a SEGMENT_START message */
942 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
943 gst_element_post_message (GST_ELEMENT_CAST (wav),
944 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
945 wav->segment.format, wav->segment.position));
948 /* now create the newsegment */
949 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
950 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
952 /* store the newsegment event so it can be sent from the streaming thread. */
953 if (wav->start_segment)
954 gst_event_unref (wav->start_segment);
955 wav->start_segment = gst_event_new_segment (&wav->segment);
957 /* mark discont if we are going to stream from another position. */
958 if (last_stop != wav->segment.position) {
959 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
963 /* and start the streaming task again */
964 if (!wav->streaming) {
965 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
969 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
976 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
981 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
986 GST_DEBUG_OBJECT (wav,
987 "Could not determine byte position for desired time");
993 * gst_wavparse_peek_chunk_info:
994 * @wav Wavparse object
995 * @tag holder for tag
996 * @size holder for tag size
998 * Peek next chunk info (tag and size)
1000 * Returns: %TRUE when the chunk info (header) is available
1003 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
1005 const guint8 *data = NULL;
1007 if (gst_adapter_available (wav->adapter) < 8)
1010 data = gst_adapter_map (wav->adapter, 8);
1011 *tag = GST_READ_UINT32_LE (data);
1012 *size = GST_READ_UINT32_LE (data + 4);
1013 gst_adapter_unmap (wav->adapter);
1015 GST_DEBUG ("Next chunk size is %u bytes, type %" GST_FOURCC_FORMAT, *size,
1016 GST_FOURCC_ARGS (*tag));
1022 * gst_wavparse_peek_chunk:
1023 * @wav Wavparse object
1024 * @tag holder for tag
1025 * @size holder for tag size
1027 * Peek enough data for one full chunk
1029 * Returns: %TRUE when the full chunk is available
1032 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
1034 guint32 peek_size = 0;
1037 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
1040 /* size 0 -> empty data buffer would surprise most callers,
1041 * large size -> do not bother trying to squeeze that into adapter,
1042 * so we throw poor man's exception, which can be caught if caller really
1043 * wants to handle 0 size chunk */
1044 if (!(*size) || (*size) >= (1 << 30)) {
1045 GST_INFO ("Invalid/unexpected chunk size %u for tag %" GST_FOURCC_FORMAT,
1046 *size, GST_FOURCC_ARGS (*tag));
1047 /* chain should give up */
1048 wav->abort_buffering = TRUE;
1051 peek_size = (*size + 1) & ~1;
1052 available = gst_adapter_available (wav->adapter);
1054 if (available >= (8 + peek_size)) {
1057 GST_LOG ("but only %u bytes available now", available);
1063 * gst_wavparse_calculate_duration:
1064 * @wav: wavparse object
1066 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
1069 * Returns: %TRUE if duration is available.
1072 gst_wavparse_calculate_duration (GstWavParse * wav)
1074 if (wav->duration > 0)
1078 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
1080 gst_util_uint64_scale_ceil (wav->datasize, GST_SECOND,
1081 (guint64) wav->bps);
1082 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
1083 GST_TIME_ARGS (wav->duration));
1085 } else if (wav->fact) {
1087 gst_util_uint64_scale_int_ceil (GST_SECOND, wav->fact, wav->rate);
1088 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
1089 GST_TIME_ARGS (wav->duration));
1096 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
1101 if (wav->streaming) {
1102 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1105 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
1106 GST_FOURCC_ARGS (tag));
1107 flush = 8 + ((size + 1) & ~1);
1108 wav->offset += flush;
1109 if (wav->streaming) {
1110 gst_adapter_flush (wav->adapter, flush);
1112 gst_buffer_unref (buf);
1118 #define MAX_BUFFER_SIZE 4096
1120 static GstFlowReturn
1121 gst_wavparse_stream_headers (GstWavParse * wav)
1123 GstFlowReturn res = GST_FLOW_OK;
1124 GstBuffer *buf = NULL;
1125 gst_riff_strf_auds *header = NULL;
1127 gboolean gotdata = FALSE;
1128 GstCaps *caps = NULL;
1129 gchar *codec_name = NULL;
1131 gint64 upstream_size = 0;
1133 /* search for "_fmt" chunk, which should be first */
1134 while (!wav->got_fmt) {
1137 /* The header starts with a 'fmt ' tag */
1138 if (wav->streaming) {
1139 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1142 gst_adapter_flush (wav->adapter, 8);
1146 buf = gst_adapter_take_buffer (wav->adapter, size);
1148 gst_adapter_flush (wav->adapter, 1);
1149 wav->offset += GST_ROUND_UP_2 (size);
1151 buf = gst_buffer_new ();
1154 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1155 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1159 if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ ||
1160 tag == GST_RIFF_TAG_bext || tag == GST_RIFF_TAG_BEXT ||
1161 tag == GST_RIFF_TAG_LIST || tag == GST_RIFF_TAG_ID32 ||
1162 tag == GST_RIFF_TAG_IDVX) {
1163 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1164 GST_FOURCC_ARGS (tag));
1165 gst_buffer_unref (buf);
1170 if (tag != GST_RIFF_TAG_fmt)
1173 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1175 goto parse_header_error;
1177 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1179 /* do sanity checks of header fields */
1180 if (header->channels == 0)
1182 if (header->rate == 0)
1185 GST_DEBUG_OBJECT (wav, "creating the caps");
1187 /* Note: gst_riff_create_audio_caps might need to fix values in
1188 * the header header depending on the format, so call it first */
1189 /* FIXME: Need to handle the channel reorder map */
1190 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1191 NULL, &codec_name, NULL);
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 %u to 0 for mp2/3", wav->av_bps);
1224 case GST_RIFF_WAVE_FORMAT_PCM:
1225 if (wav->blockalign > wav->channels * ((wav->depth + 7) / 8))
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_peer_query_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 gst_buffer_map (buf, &map, GST_MAP_READ);
1293 tag = GST_READ_UINT32_LE (map.data);
1294 size = GST_READ_UINT32_LE (map.data + 4);
1295 gst_buffer_unmap (buf, &map);
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 : %u", size);
1308 if (wav->ignore_length) {
1309 GST_DEBUG_OBJECT (wav, "Ignoring length");
1312 if (wav->streaming) {
1313 gst_adapter_flush (wav->adapter, 8);
1316 gst_buffer_unref (buf);
1319 wav->datastart = wav->offset;
1320 /* If size is zero, then the data chunk probably actually extends to
1321 the end of the file */
1322 if (size == 0 && upstream_size) {
1323 size = upstream_size - wav->datastart;
1325 /* Or the file might be truncated */
1326 else if (upstream_size) {
1327 size = MIN (size, (upstream_size - wav->datastart));
1329 wav->datasize = (guint64) size;
1330 wav->dataleft = (guint64) size;
1331 wav->end_offset = size + wav->datastart;
1332 if (!wav->streaming) {
1333 /* We will continue parsing tags 'till end */
1334 wav->offset += size;
1336 GST_DEBUG_OBJECT (wav, "datasize = %u", size);
1339 case GST_RIFF_TAG_fact:{
1340 if (wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1341 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1342 const guint data_size = 4;
1344 GST_INFO_OBJECT (wav, "Have fact chunk");
1345 if (size < data_size) {
1346 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1347 /* need more data */
1350 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1354 /* number of samples (for compressed formats) */
1355 if (wav->streaming) {
1356 const guint8 *data = NULL;
1358 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1361 gst_adapter_flush (wav->adapter, 8);
1362 data = gst_adapter_map (wav->adapter, data_size);
1363 wav->fact = GST_READ_UINT32_LE (data);
1364 gst_adapter_unmap (wav->adapter);
1365 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1367 gst_buffer_unref (buf);
1369 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1370 data_size, &buf)) != GST_FLOW_OK)
1371 goto header_read_error;
1372 gst_buffer_extract (buf, 0, &wav->fact, 4);
1373 wav->fact = GUINT32_FROM_LE (wav->fact);
1374 gst_buffer_unref (buf);
1376 GST_DEBUG_OBJECT (wav, "have fact %u", wav->fact);
1377 wav->offset += 8 + GST_ROUND_UP_2 (size);
1380 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1381 /* need more data */
1387 case GST_RIFF_TAG_acid:{
1388 const gst_riff_acid *acid = NULL;
1389 const guint data_size = sizeof (gst_riff_acid);
1392 GST_INFO_OBJECT (wav, "Have acid chunk");
1393 if (size < data_size) {
1394 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1395 /* need more data */
1398 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1402 if (wav->streaming) {
1403 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1406 gst_adapter_flush (wav->adapter, 8);
1407 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1409 tempo = acid->tempo;
1410 gst_adapter_unmap (wav->adapter);
1413 gst_buffer_unref (buf);
1415 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1416 size, &buf)) != GST_FLOW_OK)
1417 goto header_read_error;
1418 gst_buffer_map (buf, &map, GST_MAP_READ);
1419 acid = (const gst_riff_acid *) map.data;
1420 tempo = acid->tempo;
1421 gst_buffer_unmap (buf, &map);
1423 /* send data as tags */
1425 wav->tags = gst_tag_list_new_empty ();
1426 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1427 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1429 size = GST_ROUND_UP_2 (size);
1430 if (wav->streaming) {
1431 gst_adapter_flush (wav->adapter, size);
1433 gst_buffer_unref (buf);
1435 wav->offset += 8 + size;
1438 /* FIXME: all list tags after data are ignored in streaming mode */
1439 case GST_RIFF_TAG_LIST:{
1442 if (wav->streaming) {
1443 const guint8 *data = NULL;
1445 if (gst_adapter_available (wav->adapter) < 12) {
1448 data = gst_adapter_map (wav->adapter, 12);
1449 ltag = GST_READ_UINT32_LE (data + 8);
1450 gst_adapter_unmap (wav->adapter);
1452 gst_buffer_unref (buf);
1454 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1455 &buf)) != GST_FLOW_OK)
1456 goto header_read_error;
1457 gst_buffer_extract (buf, 8, <ag, 4);
1458 ltag = GUINT32_FROM_LE (ltag);
1461 case GST_RIFF_LIST_INFO:{
1462 const gint data_size = size - 4;
1465 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1466 if (wav->streaming) {
1467 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1470 gst_adapter_flush (wav->adapter, 12);
1472 if (data_size > 0) {
1473 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1475 gst_adapter_flush (wav->adapter, 1);
1479 gst_buffer_unref (buf);
1480 if (data_size > 0) {
1482 gst_pad_pull_range (wav->sinkpad, wav->offset,
1483 data_size, &buf)) != GST_FLOW_OK)
1484 goto header_read_error;
1487 if (data_size > 0) {
1489 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1491 GstTagList *old = wav->tags;
1493 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1495 gst_tag_list_free (old);
1496 gst_tag_list_free (new);
1498 gst_buffer_unref (buf);
1499 wav->offset += GST_ROUND_UP_2 (data_size);
1504 GST_INFO_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1505 GST_FOURCC_ARGS (ltag));
1506 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1507 /* need more data */
1514 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1515 /* need more data */
1520 if (upstream_size && (wav->offset >= upstream_size)) {
1521 /* Now we are gone through the whole file */
1526 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1528 if (wav->bps <= 0 && wav->fact) {
1530 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1532 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1533 (guint64) wav->fact);
1534 GST_INFO_OBJECT (wav, "calculated bps : %u, enabling VBR", wav->bps);
1539 if (gst_wavparse_calculate_duration (wav)) {
1540 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1541 if (!wav->ignore_length)
1542 wav->segment.duration = wav->duration;
1544 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1545 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1546 if (!wav->ignore_length)
1547 wav->segment.duration = wav->datasize;
1550 /* now we have all the info to perform a pending seek if any, if no
1551 * event, this will still do the right thing and it will also send
1552 * the right newsegment event downstream. */
1553 gst_wavparse_perform_seek (wav, wav->seek_event);
1554 /* remove pending event */
1555 event_p = &wav->seek_event;
1556 gst_event_replace (event_p, NULL);
1558 /* we just started, we are discont */
1559 wav->discont = TRUE;
1561 wav->state = GST_WAVPARSE_DATA;
1563 /* determine reasonable max buffer size,
1564 * that is, buffers not too small either size or time wise
1565 * so we do not end up with too many of them */
1568 gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size);
1569 wav->max_buf_size = upstream_size;
1570 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1571 if (wav->blockalign > 0)
1572 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1574 GST_DEBUG_OBJECT (wav, "max buffer size %u", wav->max_buf_size);
1582 g_free (codec_name);
1586 gst_caps_unref (caps);
1591 res = GST_FLOW_ERROR;
1596 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1597 ("Invalid WAV header (no fmt at start): %"
1598 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1603 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1604 ("Couldn't parse audio header"));
1609 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1610 ("Stream claims to contain no channels - invalid data"));
1615 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1616 ("Stream with sample_rate == 0 - invalid data"));
1621 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1622 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1623 wav->blockalign, wav->channels * ((wav->depth + 7) / 8)));
1628 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1629 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1630 wav->av_bps, wav->blockalign * wav->rate));
1633 no_bytes_per_sample:
1635 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1636 ("Could not caluclate bytes per sample - invalid data"));
1641 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1642 ("No caps found for format 0x%x, %u channels, %u Hz",
1643 wav->format, wav->channels, wav->rate));
1648 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1649 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1655 * Read WAV file tag when streaming
1657 static GstFlowReturn
1658 gst_wavparse_parse_stream_init (GstWavParse * wav)
1660 if (gst_adapter_available (wav->adapter) >= 12) {
1663 /* _take flushes the data */
1664 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1666 GST_DEBUG ("Parsing wav header");
1667 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1668 return GST_FLOW_ERROR;
1671 /* Go to next state */
1672 wav->state = GST_WAVPARSE_HEADER;
1677 /* handle an event sent directly to the element.
1679 * This event can be sent either in the READY state or the
1680 * >READY state. The only event of interest really is the seek
1683 * In the READY state we can only store the event and try to
1684 * respect it when going to PAUSED. We assume we are in the
1685 * READY state when our parsing state != GST_WAVPARSE_DATA.
1687 * When we are steaming, we can simply perform the seek right
1691 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1693 GstWavParse *wav = GST_WAVPARSE (element);
1694 gboolean res = FALSE;
1697 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1699 switch (GST_EVENT_TYPE (event)) {
1700 case GST_EVENT_SEEK:
1701 if (wav->state == GST_WAVPARSE_DATA) {
1702 /* we can handle the seek directly when streaming data */
1703 res = gst_wavparse_perform_seek (wav, event);
1705 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1707 event_p = &wav->seek_event;
1708 gst_event_replace (event_p, event);
1710 /* we always return true */
1717 gst_event_unref (event);
1722 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1726 s = gst_caps_get_structure (caps, 0);
1727 if (!gst_structure_has_name (s, "audio/x-dts"))
1729 if (prob >= GST_TYPE_FIND_LIKELY)
1731 /* DTS at non-0 offsets and without second sync may yield POSSIBLE .. */
1732 if (prob < GST_TYPE_FIND_POSSIBLE)
1734 /* .. in which case we want at least a valid-looking rate and channels */
1735 if (!gst_structure_has_field (s, "channels"))
1737 /* and for extra assurance we could also check the rate from the DTS frame
1738 * against the one in the wav header, but for now let's not do that */
1739 return gst_structure_has_field (s, "rate");
1743 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1747 GST_DEBUG_OBJECT (wav, "adding src pad");
1750 s = gst_caps_get_structure (wav->caps, 0);
1751 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
1752 GstTypeFindProbability prob;
1755 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1756 if (tf_caps != NULL) {
1757 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1758 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1759 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1760 gst_caps_unref (wav->caps);
1761 wav->caps = tf_caps;
1763 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1764 GST_TAG_AUDIO_CODEC, "dts", NULL);
1766 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1767 "marked as raw PCM audio, but ignoring for now", tf_caps);
1768 gst_caps_unref (tf_caps);
1774 gst_pad_set_caps (wav->srcpad, wav->caps);
1775 gst_caps_replace (&wav->caps, NULL);
1777 if (wav->start_segment) {
1778 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1779 gst_pad_push_event (wav->srcpad, wav->start_segment);
1780 wav->start_segment = NULL;
1784 gst_pad_push_event (wav->srcpad, gst_event_new_tag (wav->tags));
1789 static GstFlowReturn
1790 gst_wavparse_stream_data (GstWavParse * wav)
1792 GstBuffer *buf = NULL;
1793 GstFlowReturn res = GST_FLOW_OK;
1794 guint64 desired, obtained;
1795 GstClockTime timestamp, next_timestamp, duration;
1796 guint64 pos, nextpos;
1799 GST_LOG_OBJECT (wav,
1800 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1801 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1803 /* Get the next n bytes and output them */
1804 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1807 /* scale the amount of data by the segment rate so we get equal
1808 * amounts of data regardless of the playback rate */
1810 MIN (gst_guint64_to_gdouble (wav->dataleft),
1811 wav->max_buf_size * ABS (wav->segment.rate));
1813 if (desired >= wav->blockalign && wav->blockalign > 0)
1814 desired -= (desired % wav->blockalign);
1816 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1817 "from the sinkpad", desired);
1819 if (wav->streaming) {
1820 guint avail = gst_adapter_available (wav->adapter);
1823 /* flush some bytes if evil upstream sends segment that starts
1824 * before data or does is not send sample aligned segment */
1825 if (G_LIKELY (wav->offset >= wav->datastart)) {
1826 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1828 extra = wav->datastart - wav->offset;
1831 if (G_UNLIKELY (extra)) {
1832 extra = wav->bytes_per_sample - extra;
1833 if (extra <= avail) {
1834 GST_DEBUG_OBJECT (wav, "flushing %u bytes to sample boundary", extra);
1835 gst_adapter_flush (wav->adapter, extra);
1836 wav->offset += extra;
1837 wav->dataleft -= extra;
1838 goto iterate_adapter;
1840 GST_DEBUG_OBJECT (wav, "flushing %u bytes", avail);
1841 gst_adapter_clear (wav->adapter);
1842 wav->offset += avail;
1843 wav->dataleft -= avail;
1848 if (avail < desired) {
1849 GST_LOG_OBJECT (wav, "Got only %u bytes of data from the sinkpad", avail);
1853 buf = gst_adapter_take_buffer (wav->adapter, desired);
1855 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
1856 desired, &buf)) != GST_FLOW_OK)
1859 /* we may get a short buffer at the end of the file */
1860 if (gst_buffer_get_size (buf) < desired) {
1861 gsize size = gst_buffer_get_size (buf);
1863 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
1864 if (size >= wav->blockalign) {
1865 buf = gst_buffer_make_writable (buf);
1866 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
1868 gst_buffer_unref (buf);
1874 obtained = gst_buffer_get_size (buf);
1876 /* our positions in bytes */
1877 pos = wav->offset - wav->datastart;
1878 nextpos = pos + obtained;
1880 /* update offsets, does not overflow. */
1881 buf = gst_buffer_make_writable (buf);
1882 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
1883 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
1885 /* first chunk of data? create the source pad. We do this only here so
1886 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
1887 if (G_UNLIKELY (wav->first)) {
1889 /* this will also push the segment events */
1890 gst_wavparse_add_src_pad (wav, buf);
1892 /* If we have a pending start segment, send it now. */
1893 if (G_UNLIKELY (wav->start_segment != NULL)) {
1894 gst_pad_push_event (wav->srcpad, wav->start_segment);
1895 wav->start_segment = NULL;
1900 /* and timestamps if we have a bitrate, be careful for overflows */
1902 gst_util_uint64_scale_ceil (pos, GST_SECOND, (guint64) wav->bps);
1904 gst_util_uint64_scale_ceil (nextpos, GST_SECOND, (guint64) wav->bps);
1905 duration = next_timestamp - timestamp;
1907 /* update current running segment position */
1908 if (G_LIKELY (next_timestamp >= wav->segment.start))
1909 wav->segment.position = next_timestamp;
1910 } else if (wav->fact) {
1912 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
1913 /* and timestamps if we have a bitrate, be careful for overflows */
1914 timestamp = gst_util_uint64_scale_ceil (pos, GST_SECOND, bps);
1915 next_timestamp = gst_util_uint64_scale_ceil (nextpos, GST_SECOND, bps);
1916 duration = next_timestamp - timestamp;
1918 /* no bitrate, all we know is that the first sample has timestamp 0, all
1919 * other positions and durations have unknown timestamp. */
1923 timestamp = GST_CLOCK_TIME_NONE;
1924 duration = GST_CLOCK_TIME_NONE;
1925 /* update current running segment position with byte offset */
1926 if (G_LIKELY (nextpos >= wav->segment.start))
1927 wav->segment.position = nextpos;
1929 if ((pos > 0) && wav->vbr) {
1930 /* don't set timestamps for VBR files if it's not the first buffer */
1931 timestamp = GST_CLOCK_TIME_NONE;
1932 duration = GST_CLOCK_TIME_NONE;
1935 GST_DEBUG_OBJECT (wav, "marking DISCONT");
1936 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1937 wav->discont = FALSE;
1940 GST_BUFFER_TIMESTAMP (buf) = timestamp;
1941 GST_BUFFER_DURATION (buf) = duration;
1943 GST_LOG_OBJECT (wav,
1944 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
1945 ", size:%" G_GSIZE_FORMAT, GST_TIME_ARGS (timestamp),
1946 GST_TIME_ARGS (duration), gst_buffer_get_size (buf));
1948 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
1951 if (obtained < wav->dataleft) {
1952 wav->offset += obtained;
1953 wav->dataleft -= obtained;
1955 wav->offset += wav->dataleft;
1959 /* Iterate until need more data, so adapter size won't grow */
1960 if (wav->streaming) {
1961 GST_LOG_OBJECT (wav,
1962 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
1964 goto iterate_adapter;
1971 GST_DEBUG_OBJECT (wav, "found EOS");
1972 return GST_FLOW_EOS;
1976 /* check if we got EOS */
1977 if (res == GST_FLOW_EOS)
1980 GST_WARNING_OBJECT (wav,
1981 "Error getting %" G_GINT64_FORMAT " bytes from the "
1982 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
1987 GST_INFO_OBJECT (wav,
1988 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
1989 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
1990 gst_pad_is_linked (wav->srcpad));
1996 gst_wavparse_loop (GstPad * pad)
1999 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2001 GST_LOG_OBJECT (wav, "process data");
2003 switch (wav->state) {
2004 case GST_WAVPARSE_START:
2005 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2006 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2009 wav->state = GST_WAVPARSE_HEADER;
2012 case GST_WAVPARSE_HEADER:
2013 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2014 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2017 wav->state = GST_WAVPARSE_DATA;
2018 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2021 case GST_WAVPARSE_DATA:
2022 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2026 g_assert_not_reached ();
2033 const gchar *reason = gst_flow_get_name (ret);
2035 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2036 gst_pad_pause_task (pad);
2038 if (ret == GST_FLOW_EOS) {
2039 /* handle end-of-stream/segment */
2040 /* so align our position with the end of it, if there is one
2041 * this ensures a subsequent will arrive at correct base/acc time */
2042 if (wav->segment.format == GST_FORMAT_TIME) {
2043 if (wav->segment.rate > 0.0 &&
2044 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2045 wav->segment.position = wav->segment.stop;
2046 else if (wav->segment.rate < 0.0)
2047 wav->segment.position = wav->segment.start;
2049 /* add pad before we perform EOS */
2050 if (G_UNLIKELY (wav->first)) {
2052 gst_wavparse_add_src_pad (wav, NULL);
2055 if (wav->state == GST_WAVPARSE_START)
2056 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2057 ("No valid input found before end of stream"), (NULL));
2059 /* perform EOS logic */
2060 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2063 if ((stop = wav->segment.stop) == -1)
2064 stop = wav->segment.duration;
2066 gst_element_post_message (GST_ELEMENT_CAST (wav),
2067 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2068 wav->segment.format, stop));
2070 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2072 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2073 /* for fatal errors we post an error message, post the error
2074 * first so the app knows about the error first. */
2075 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
2076 (_("Internal data flow error.")),
2077 ("streaming task paused, reason %s (%d)", reason, ret));
2078 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2084 static GstFlowReturn
2085 gst_wavparse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2088 GstWavParse *wav = GST_WAVPARSE (parent);
2090 GST_LOG_OBJECT (wav, "adapter_push %" G_GSIZE_FORMAT " bytes",
2091 gst_buffer_get_size (buf));
2093 gst_adapter_push (wav->adapter, buf);
2095 switch (wav->state) {
2096 case GST_WAVPARSE_START:
2097 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2098 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2101 if (wav->state != GST_WAVPARSE_HEADER)
2104 /* otherwise fall-through */
2105 case GST_WAVPARSE_HEADER:
2106 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2107 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2110 if (!wav->got_fmt || wav->datastart == 0)
2113 wav->state = GST_WAVPARSE_DATA;
2114 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2117 case GST_WAVPARSE_DATA:
2118 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2119 wav->discont = TRUE;
2120 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2124 g_return_val_if_reached (GST_FLOW_ERROR);
2127 if (G_UNLIKELY (wav->abort_buffering)) {
2128 wav->abort_buffering = FALSE;
2129 ret = GST_FLOW_ERROR;
2130 /* sort of demux/parse error */
2131 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2137 static GstFlowReturn
2138 gst_wavparse_flush_data (GstWavParse * wav)
2140 GstFlowReturn ret = GST_FLOW_OK;
2143 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2145 wav->end_offset = wav->offset + av;
2146 ret = gst_wavparse_stream_data (wav);
2153 gst_wavparse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2155 GstWavParse *wav = GST_WAVPARSE (parent);
2156 gboolean ret = TRUE;
2158 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2160 switch (GST_EVENT_TYPE (event)) {
2161 case GST_EVENT_CAPS:
2163 /* discard, we'll come up with proper src caps */
2164 gst_event_unref (event);
2167 case GST_EVENT_SEGMENT:
2169 gint64 start, stop, offset = 0, end_offset = -1;
2172 /* some debug output */
2173 gst_event_copy_segment (event, &segment);
2174 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2177 if (wav->state != GST_WAVPARSE_DATA) {
2178 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2182 /* now we are either committed to TIME or BYTE format,
2183 * and we only expect a BYTE segment, e.g. following a seek */
2184 if (segment.format == GST_FORMAT_BYTES) {
2185 /* handle (un)signed issues */
2186 start = segment.start;
2187 stop = segment.stop;
2190 start -= wav->datastart;
2191 start = MAX (start, 0);
2195 segment.stop -= wav->datastart;
2196 segment.stop = MAX (stop, 0);
2198 if (wav->segment.format == GST_FORMAT_TIME) {
2199 guint64 bps = wav->bps;
2201 /* operating in format TIME, so we can convert */
2202 if (!bps && wav->fact)
2204 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2208 gst_util_uint64_scale_ceil (start, GST_SECOND,
2209 (guint64) wav->bps);
2212 gst_util_uint64_scale_ceil (stop, GST_SECOND,
2213 (guint64) wav->bps);
2217 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2221 segment.start = start;
2222 segment.stop = stop;
2224 /* accept upstream's notion of segment and distribute along */
2225 segment.time = segment.start = segment.position;
2226 segment.duration = wav->segment.duration;
2227 segment.base = gst_segment_to_running_time (&wav->segment,
2228 GST_FORMAT_TIME, wav->segment.position);
2230 gst_segment_copy_into (&segment, &wav->segment);
2232 /* also store the newsegment event for the streaming thread */
2233 if (wav->start_segment)
2234 gst_event_unref (wav->start_segment);
2235 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2236 wav->start_segment = gst_event_new_segment (&segment);
2238 /* stream leftover data in current segment */
2239 gst_wavparse_flush_data (wav);
2240 /* and set up streaming thread for next one */
2241 wav->offset = offset;
2242 wav->end_offset = end_offset;
2243 if (wav->end_offset > 0) {
2244 wav->dataleft = wav->end_offset - wav->offset;
2246 /* infinity; upstream will EOS when done */
2247 wav->dataleft = G_MAXUINT64;
2250 gst_event_unref (event);
2254 /* add pad if needed so EOS is seen downstream */
2255 if (G_UNLIKELY (wav->first)) {
2257 gst_wavparse_add_src_pad (wav, NULL);
2259 /* stream leftover data in current segment */
2260 gst_wavparse_flush_data (wav);
2263 if (wav->state == GST_WAVPARSE_START)
2264 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2265 ("No valid input found before end of stream"), (NULL));
2268 case GST_EVENT_FLUSH_STOP:
2272 gst_adapter_clear (wav->adapter);
2273 wav->discont = TRUE;
2274 dur = wav->segment.duration;
2275 gst_segment_init (&wav->segment, wav->segment.format);
2276 wav->segment.duration = dur;
2280 ret = gst_pad_event_default (wav->sinkpad, parent, event);
2288 /* convert and query stuff */
2289 static const GstFormat *
2290 gst_wavparse_get_formats (GstPad * pad)
2292 static GstFormat formats[] = {
2295 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2304 gst_wavparse_pad_convert (GstPad * pad,
2305 GstFormat src_format, gint64 src_value,
2306 GstFormat * dest_format, gint64 * dest_value)
2308 GstWavParse *wavparse;
2309 gboolean res = TRUE;
2311 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2313 if (*dest_format == src_format) {
2314 *dest_value = src_value;
2318 if ((wavparse->bps == 0) && !wavparse->fact)
2321 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2322 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2324 switch (src_format) {
2325 case GST_FORMAT_BYTES:
2326 switch (*dest_format) {
2327 case GST_FORMAT_DEFAULT:
2328 *dest_value = src_value / wavparse->bytes_per_sample;
2329 /* make sure we end up on a sample boundary */
2330 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2332 case GST_FORMAT_TIME:
2333 /* src_value + datastart = offset */
2334 GST_INFO_OBJECT (wavparse,
2335 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2337 if (wavparse->bps > 0)
2338 *dest_value = gst_util_uint64_scale_ceil (src_value, GST_SECOND,
2339 (guint64) wavparse->bps);
2340 else if (wavparse->fact) {
2341 guint64 bps = gst_util_uint64_scale_int_ceil (wavparse->datasize,
2342 wavparse->rate, wavparse->fact);
2345 gst_util_uint64_scale_int_ceil (src_value, GST_SECOND, bps);
2356 case GST_FORMAT_DEFAULT:
2357 switch (*dest_format) {
2358 case GST_FORMAT_BYTES:
2359 *dest_value = src_value * wavparse->bytes_per_sample;
2361 case GST_FORMAT_TIME:
2362 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2363 (guint64) wavparse->rate);
2371 case GST_FORMAT_TIME:
2372 switch (*dest_format) {
2373 case GST_FORMAT_BYTES:
2374 if (wavparse->bps > 0)
2375 *dest_value = gst_util_uint64_scale (src_value,
2376 (guint64) wavparse->bps, GST_SECOND);
2378 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2379 wavparse->rate, wavparse->fact);
2381 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2383 /* make sure we end up on a sample boundary */
2384 *dest_value -= *dest_value % wavparse->blockalign;
2386 case GST_FORMAT_DEFAULT:
2387 *dest_value = gst_util_uint64_scale (src_value,
2388 (guint64) wavparse->rate, GST_SECOND);
2407 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2413 /* handle queries for location and length in requested format */
2415 gst_wavparse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
2417 gboolean res = TRUE;
2418 GstWavParse *wav = GST_WAVPARSE (parent);
2420 /* only if we know */
2421 if (wav->state != GST_WAVPARSE_DATA) {
2425 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2427 switch (GST_QUERY_TYPE (query)) {
2428 case GST_QUERY_POSITION:
2434 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2435 curb = wav->offset - wav->datastart;
2436 gst_query_parse_position (query, &format, NULL);
2437 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2440 case GST_FORMAT_TIME:
2441 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2445 format = GST_FORMAT_BYTES;
2450 gst_query_set_position (query, format, cur);
2453 case GST_QUERY_DURATION:
2455 gint64 duration = 0;
2458 if (wav->ignore_length) {
2463 gst_query_parse_duration (query, &format, NULL);
2466 case GST_FORMAT_TIME:{
2467 if ((res = gst_wavparse_calculate_duration (wav))) {
2468 duration = wav->duration;
2473 format = GST_FORMAT_BYTES;
2474 duration = wav->datasize;
2477 gst_query_set_duration (query, format, duration);
2480 case GST_QUERY_CONVERT:
2482 gint64 srcvalue, dstvalue;
2483 GstFormat srcformat, dstformat;
2485 gst_query_parse_convert (query, &srcformat, &srcvalue,
2486 &dstformat, &dstvalue);
2487 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2488 &dstformat, &dstvalue);
2490 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2493 case GST_QUERY_SEEKING:{
2495 gboolean seekable = FALSE;
2497 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2498 if (fmt == wav->segment.format) {
2499 if (wav->streaming) {
2502 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2503 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2504 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2505 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2507 gst_query_unref (q);
2509 GST_LOG_OBJECT (wav, "looping => seekable");
2513 } else if (fmt == GST_FORMAT_TIME) {
2517 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2522 res = gst_pad_query_default (pad, parent, query);
2529 gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
2531 GstWavParse *wavparse = GST_WAVPARSE (parent);
2532 gboolean res = FALSE;
2534 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2536 switch (GST_EVENT_TYPE (event)) {
2537 case GST_EVENT_SEEK:
2538 /* can only handle events when we are in the data state */
2539 if (wavparse->state == GST_WAVPARSE_DATA) {
2540 res = gst_wavparse_perform_seek (wavparse, event);
2542 gst_event_unref (event);
2545 res = gst_pad_push_event (wavparse->sinkpad, event);
2552 gst_wavparse_sink_activate (GstPad * sinkpad, GstObject * parent)
2554 GstWavParse *wav = GST_WAVPARSE (parent);
2559 gst_adapter_clear (wav->adapter);
2560 g_object_unref (wav->adapter);
2561 wav->adapter = NULL;
2564 query = gst_query_new_scheduling ();
2566 if (!gst_pad_peer_query (sinkpad, query)) {
2567 gst_query_unref (query);
2571 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
2572 gst_query_unref (query);
2577 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2578 wav->streaming = FALSE;
2579 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
2583 GST_DEBUG_OBJECT (sinkpad, "activating push");
2584 wav->streaming = TRUE;
2585 wav->adapter = gst_adapter_new ();
2586 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
2592 gst_wavparse_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
2593 GstPadMode mode, gboolean active)
2598 case GST_PAD_MODE_PUSH:
2601 case GST_PAD_MODE_PULL:
2603 /* if we have a scheduler we can start the task */
2604 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2607 res = gst_pad_stop_task (sinkpad);
2617 static GstStateChangeReturn
2618 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2620 GstStateChangeReturn ret;
2621 GstWavParse *wav = GST_WAVPARSE (element);
2623 switch (transition) {
2624 case GST_STATE_CHANGE_NULL_TO_READY:
2626 case GST_STATE_CHANGE_READY_TO_PAUSED:
2627 gst_wavparse_reset (wav);
2629 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2635 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2637 switch (transition) {
2638 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2640 case GST_STATE_CHANGE_PAUSED_TO_READY:
2641 gst_wavparse_reset (wav);
2643 case GST_STATE_CHANGE_READY_TO_NULL:
2652 gst_wavparse_set_property (GObject * object, guint prop_id,
2653 const GValue * value, GParamSpec * pspec)
2657 g_return_if_fail (GST_IS_WAVPARSE (object));
2658 self = GST_WAVPARSE (object);
2661 case PROP_IGNORE_LENGTH:
2662 self->ignore_length = g_value_get_boolean (value);
2665 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2671 gst_wavparse_get_property (GObject * object, guint prop_id,
2672 GValue * value, GParamSpec * pspec)
2676 g_return_if_fail (GST_IS_WAVPARSE (object));
2677 self = GST_WAVPARSE (object);
2680 case PROP_IGNORE_LENGTH:
2681 g_value_set_boolean (value, self->ignore_length);
2684 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2689 plugin_init (GstPlugin * plugin)
2693 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2697 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2700 "Parse a .wav file into raw audio",
2701 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)