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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, 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-1.0 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-1.0 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,
118 /* Offset Size Description Value
119 * 0x00 4 ID unique identification value
120 * 0x04 4 Position play order position
121 * 0x08 4 Data Chunk ID RIFF ID of corresponding data chunk
122 * 0x0c 4 Chunk Start Byte Offset of Data Chunk *
123 * 0x10 4 Block Start Byte Offset to sample of First Channel
124 * 0x14 4 Sample Offset Byte Offset to sample byte of First Channel
128 guint32 data_chunk_id;
131 guint32 sample_offset;
136 /* Offset Size Description Value
137 * 0x08 4 Cue Point ID 0 - 0xFFFFFFFF
140 guint32 cue_point_id;
145 gst_wavparse_class_init (GstWavParseClass * klass)
147 GstElementClass *gstelement_class;
148 GObjectClass *object_class;
149 GstPadTemplate *src_template;
151 gstelement_class = (GstElementClass *) klass;
152 object_class = (GObjectClass *) klass;
154 parent_class = g_type_class_peek_parent (klass);
156 object_class->dispose = gst_wavparse_dispose;
158 object_class->set_property = gst_wavparse_set_property;
159 object_class->get_property = gst_wavparse_get_property;
162 * GstWavParse:ignore-length
164 * This selects whether the length found in a data chunk
165 * should be ignored. This may be useful for streamed audio
166 * where the length is unknown until the end of streaming,
167 * and various software/hardware just puts some random value
168 * in there and hopes it doesn't break too much.
172 g_object_class_install_property (object_class, PROP_IGNORE_LENGTH,
173 g_param_spec_boolean ("ignore-length",
175 "Ignore length from the Wave header",
176 DEFAULT_IGNORE_LENGTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
179 gstelement_class->change_state = gst_wavparse_change_state;
180 gstelement_class->send_event = gst_wavparse_send_event;
183 gst_element_class_add_pad_template (gstelement_class,
184 gst_static_pad_template_get (&sink_template_factory));
186 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
187 GST_PAD_ALWAYS, gst_riff_create_audio_template_caps ());
188 gst_element_class_add_pad_template (gstelement_class, src_template);
190 gst_element_class_set_static_metadata (gstelement_class, "WAV audio demuxer",
191 "Codec/Demuxer/Audio",
192 "Parse a .wav file into raw audio",
193 "Erik Walthinsen <omega@cse.ogi.edu>");
197 gst_wavparse_reset (GstWavParse * wav)
199 wav->state = GST_WAVPARSE_START;
201 /* These will all be set correctly in the fmt chunk */
215 wav->got_fmt = FALSE;
219 gst_event_unref (wav->seek_event);
220 wav->seek_event = NULL;
222 gst_adapter_clear (wav->adapter);
223 g_object_unref (wav->adapter);
227 gst_tag_list_unref (wav->tags);
230 gst_toc_unref (wav->toc);
233 g_list_free_full (wav->cues, g_free);
236 g_list_free_full (wav->labls, g_free);
239 gst_caps_unref (wav->caps);
241 if (wav->start_segment)
242 gst_event_unref (wav->start_segment);
243 wav->start_segment = NULL;
247 gst_wavparse_dispose (GObject * object)
249 GstWavParse *wav = GST_WAVPARSE (object);
251 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
252 gst_wavparse_reset (wav);
254 G_OBJECT_CLASS (parent_class)->dispose (object);
258 gst_wavparse_init (GstWavParse * wavparse)
260 gst_wavparse_reset (wavparse);
264 gst_pad_new_from_static_template (&sink_template_factory, "sink");
265 gst_pad_set_activate_function (wavparse->sinkpad,
266 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
267 gst_pad_set_activatemode_function (wavparse->sinkpad,
268 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_mode));
269 gst_pad_set_chain_function (wavparse->sinkpad,
270 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
271 gst_pad_set_event_function (wavparse->sinkpad,
272 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
273 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
277 gst_pad_new_from_template (gst_element_class_get_pad_template
278 (GST_ELEMENT_GET_CLASS (wavparse), "src"), "src");
279 gst_pad_use_fixed_caps (wavparse->srcpad);
280 gst_pad_set_query_function (wavparse->srcpad,
281 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
282 gst_pad_set_event_function (wavparse->srcpad,
283 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
284 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
287 /* FIXME: why is that not in use? */
290 gst_wavparse_parse_adtl (GstWavParse * wavparse, int len)
293 GstByteStream *bs = wavparse->bs;
294 gst_riff_chunk *temp_chunk, chunk;
296 struct _gst_riff_labl labl, *temp_labl;
297 struct _gst_riff_ltxt ltxt, *temp_ltxt;
298 struct _gst_riff_note note, *temp_note;
301 GstPropsEntry *entry;
305 props = wavparse->metadata->properties;
309 gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
310 if (got_bytes != sizeof (gst_riff_chunk)) {
313 temp_chunk = (gst_riff_chunk *) tempdata;
315 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
316 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
318 if (chunk.size == 0) {
319 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
320 len -= sizeof (gst_riff_chunk);
325 case GST_RIFF_adtl_labl:
327 gst_bytestream_peek_bytes (bs, &tempdata,
328 sizeof (struct _gst_riff_labl));
329 if (got_bytes != sizeof (struct _gst_riff_labl)) {
333 temp_labl = (struct _gst_riff_labl *) tempdata;
334 labl.id = GUINT32_FROM_LE (temp_labl->id);
335 labl.size = GUINT32_FROM_LE (temp_labl->size);
336 labl.identifier = GUINT32_FROM_LE (temp_labl->identifier);
338 gst_bytestream_flush (bs, sizeof (struct _gst_riff_labl));
339 len -= sizeof (struct _gst_riff_labl);
341 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, labl.size - 4);
342 if (got_bytes != labl.size - 4) {
346 label_name = (char *) tempdata;
348 gst_bytestream_flush (bs, ((labl.size - 4) + 1) & ~1);
349 len -= (((labl.size - 4) + 1) & ~1);
351 new_caps = gst_caps_new ("label",
352 "application/x-gst-metadata",
353 gst_props_new ("identifier", G_TYPE_INT (labl.identifier),
354 "name", G_TYPE_STRING (label_name), NULL));
356 if (gst_props_get (props, "labels", &caps, NULL)) {
357 caps = g_list_append (caps, new_caps);
359 caps = g_list_append (NULL, new_caps);
361 entry = gst_props_entry_new ("labels", GST_PROPS_GLIST (caps));
362 gst_props_add_entry (props, entry);
367 case GST_RIFF_adtl_ltxt:
369 gst_bytestream_peek_bytes (bs, &tempdata,
370 sizeof (struct _gst_riff_ltxt));
371 if (got_bytes != sizeof (struct _gst_riff_ltxt)) {
375 temp_ltxt = (struct _gst_riff_ltxt *) tempdata;
376 ltxt.id = GUINT32_FROM_LE (temp_ltxt->id);
377 ltxt.size = GUINT32_FROM_LE (temp_ltxt->size);
378 ltxt.identifier = GUINT32_FROM_LE (temp_ltxt->identifier);
379 ltxt.length = GUINT32_FROM_LE (temp_ltxt->length);
380 ltxt.purpose = GUINT32_FROM_LE (temp_ltxt->purpose);
381 ltxt.country = GUINT16_FROM_LE (temp_ltxt->country);
382 ltxt.language = GUINT16_FROM_LE (temp_ltxt->language);
383 ltxt.dialect = GUINT16_FROM_LE (temp_ltxt->dialect);
384 ltxt.codepage = GUINT16_FROM_LE (temp_ltxt->codepage);
386 gst_bytestream_flush (bs, sizeof (struct _gst_riff_ltxt));
387 len -= sizeof (struct _gst_riff_ltxt);
389 if (ltxt.size - 20 > 0) {
390 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, ltxt.size - 20);
391 if (got_bytes != ltxt.size - 20) {
395 gst_bytestream_flush (bs, ((ltxt.size - 20) + 1) & ~1);
396 len -= (((ltxt.size - 20) + 1) & ~1);
398 label_name = (char *) tempdata;
403 new_caps = gst_caps_new ("ltxt",
404 "application/x-gst-metadata",
405 gst_props_new ("identifier", G_TYPE_INT (ltxt.identifier),
406 "name", G_TYPE_STRING (label_name),
407 "length", G_TYPE_INT (ltxt.length), NULL));
409 if (gst_props_get (props, "ltxts", &caps, NULL)) {
410 caps = g_list_append (caps, new_caps);
412 caps = g_list_append (NULL, new_caps);
414 entry = gst_props_entry_new ("ltxts", GST_PROPS_GLIST (caps));
415 gst_props_add_entry (props, entry);
420 case GST_RIFF_adtl_note:
422 gst_bytestream_peek_bytes (bs, &tempdata,
423 sizeof (struct _gst_riff_note));
424 if (got_bytes != sizeof (struct _gst_riff_note)) {
428 temp_note = (struct _gst_riff_note *) tempdata;
429 note.id = GUINT32_FROM_LE (temp_note->id);
430 note.size = GUINT32_FROM_LE (temp_note->size);
431 note.identifier = GUINT32_FROM_LE (temp_note->identifier);
433 gst_bytestream_flush (bs, sizeof (struct _gst_riff_note));
434 len -= sizeof (struct _gst_riff_note);
436 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, note.size - 4);
437 if (got_bytes != note.size - 4) {
441 gst_bytestream_flush (bs, ((note.size - 4) + 1) & ~1);
442 len -= (((note.size - 4) + 1) & ~1);
444 label_name = (char *) tempdata;
446 new_caps = gst_caps_new ("note",
447 "application/x-gst-metadata",
448 gst_props_new ("identifier", G_TYPE_INT (note.identifier),
449 "name", G_TYPE_STRING (label_name), NULL));
451 if (gst_props_get (props, "notes", &caps, NULL)) {
452 caps = g_list_append (caps, new_caps);
454 caps = g_list_append (NULL, new_caps);
456 entry = gst_props_entry_new ("notes", GST_PROPS_GLIST (caps));
457 gst_props_add_entry (props, entry);
463 g_print ("Unknown chunk: %" GST_FOURCC_FORMAT "\n",
464 GST_FOURCC_ARGS (chunk.id));
469 g_object_notify (G_OBJECT (wavparse), "metadata");
473 gst_wavparse_parse_cues (GstWavParse * wavparse, int len)
476 GstByteStream *bs = wavparse->bs;
477 struct _gst_riff_cue *temp_cue, cue;
478 struct _gst_riff_cuepoints *points;
482 GstPropsEntry *entry;
488 gst_bytestream_peek_bytes (bs, &tempdata,
489 sizeof (struct _gst_riff_cue));
490 temp_cue = (struct _gst_riff_cue *) tempdata;
492 /* fixup for our big endian friends */
493 cue.id = GUINT32_FROM_LE (temp_cue->id);
494 cue.size = GUINT32_FROM_LE (temp_cue->size);
495 cue.cuepoints = GUINT32_FROM_LE (temp_cue->cuepoints);
497 gst_bytestream_flush (bs, sizeof (struct _gst_riff_cue));
498 if (got_bytes != sizeof (struct _gst_riff_cue)) {
502 len -= sizeof (struct _gst_riff_cue);
504 /* -4 because cue.size contains the cuepoints size
505 and we've already flushed that out of the system */
506 required = cue.size - 4;
507 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, required);
508 gst_bytestream_flush (bs, ((required) + 1) & ~1);
509 if (got_bytes != required) {
513 len -= (((cue.size - 4) + 1) & ~1);
515 /* now we have an array of struct _gst_riff_cuepoints in tempdata */
516 points = (struct _gst_riff_cuepoints *) tempdata;
518 for (i = 0; i < cue.cuepoints; i++) {
521 caps = gst_caps_new ("cues",
522 "application/x-gst-metadata",
523 gst_props_new ("identifier", G_TYPE_INT (points[i].identifier),
524 "position", G_TYPE_INT (points[i].offset), NULL));
525 cues = g_list_append (cues, caps);
528 entry = gst_props_entry_new ("cues", GST_PROPS_GLIST (cues));
529 gst_props_add_entry (wavparse->metadata->properties, entry);
532 g_object_notify (G_OBJECT (wavparse), "metadata");
535 /* Read 'fmt ' header */
537 gst_wavparse_fmt (GstWavParse * wav)
539 gst_riff_strf_auds *header = NULL;
542 if (!gst_riff_read_strf_auds (wav, &header))
545 wav->format = header->format;
546 wav->rate = header->rate;
547 wav->channels = header->channels;
548 if (wav->channels == 0)
551 wav->blockalign = header->blockalign;
552 wav->width = (header->blockalign * 8) / header->channels;
553 wav->depth = header->size;
554 wav->bps = header->av_bps;
558 /* Note: gst_riff_create_audio_caps might need to fix values in
559 * the header header depending on the format, so call it first */
560 /* FIXME: Need to handle the channel reorder map */
561 caps = gst_riff_create_audio_caps (header->format, NULL, header, NULL, NULL);
567 gst_wavparse_create_sourcepad (wav);
568 gst_pad_use_fixed_caps (wav->srcpad);
569 gst_pad_set_active (wav->srcpad, TRUE);
570 gst_pad_set_caps (wav->srcpad, caps);
571 gst_caps_free (caps);
572 gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
573 gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
575 GST_DEBUG ("frequency %u, channels %u", wav->rate, wav->channels);
582 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
583 ("No FMT tag found"));
588 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
589 ("Stream claims to contain zero channels - invalid data"));
595 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
596 ("Stream claims to bitrate of <= zero - invalid data"));
602 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
608 gst_wavparse_other (GstWavParse * wav)
612 if (!gst_riff_peek_head (wav, &tag, &length, NULL)) {
613 GST_WARNING_OBJECT (wav, "could not peek head");
616 GST_DEBUG_OBJECT (wav, "got tag (%08x) %4.4s, length %u", tag,
617 (const gchar *) &tag, length);
620 case GST_RIFF_TAG_LIST:
621 if (!(tag = gst_riff_peek_list (wav))) {
622 GST_WARNING_OBJECT (wav, "could not peek list");
627 case GST_RIFF_LIST_INFO:
628 if (!gst_riff_read_list (wav, &tag) || !gst_riff_read_info (wav)) {
629 GST_WARNING_OBJECT (wav, "could not read list");
634 case GST_RIFF_LIST_adtl:
635 if (!gst_riff_read_skip (wav)) {
636 GST_WARNING_OBJECT (wav, "could not read skip");
642 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag,
644 if (!gst_riff_read_skip (wav)) {
645 GST_WARNING_OBJECT (wav, "could not read skip");
653 case GST_RIFF_TAG_data:
654 if (!gst_bytestream_flush (wav->bs, 8)) {
655 GST_WARNING_OBJECT (wav, "could not flush 8 bytes");
659 GST_DEBUG_OBJECT (wav, "switching to data mode");
660 wav->state = GST_WAVPARSE_DATA;
661 wav->datastart = gst_bytestream_tell (wav->bs);
665 /* length is 0, data probably stretches to the end
667 GST_DEBUG_OBJECT (wav, "length is 0 trying to find length");
668 /* get length of file */
669 file_length = gst_bytestream_length (wav->bs);
670 if (file_length == -1) {
671 GST_DEBUG_OBJECT (wav,
672 "could not get file length, assuming data to eof");
673 /* could not get length, assuming till eof */
674 length = G_MAXUINT32;
676 if (file_length > G_MAXUINT32) {
677 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
678 ", clipping to 32 bits", file_length);
679 /* could not get length, assuming till eof */
680 length = G_MAXUINT32;
682 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
683 ", datalength %u", file_length, length);
684 /* substract offset of datastart from length */
685 length = file_length - wav->datastart;
686 GST_DEBUG_OBJECT (wav, "datalength %u", length);
689 wav->datasize = (guint64) length;
690 GST_DEBUG_OBJECT (wav, "datasize = %ld", length)
693 case GST_RIFF_TAG_cue:
694 if (!gst_riff_read_skip (wav)) {
695 GST_WARNING_OBJECT (wav, "could not read skip");
701 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag, (gchar *) & tag);
702 if (!gst_riff_read_skip (wav))
713 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
717 if (!gst_riff_parse_file_header (element, buf, &doctype))
720 if (doctype != GST_RIFF_RIFF_WAVE)
728 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
729 ("File is not a WAVE file: %" GST_FOURCC_FORMAT,
730 GST_FOURCC_ARGS (doctype)));
736 gst_wavparse_stream_init (GstWavParse * wav)
739 GstBuffer *buf = NULL;
741 if ((res = gst_pad_pull_range (wav->sinkpad,
742 wav->offset, 12, &buf)) != GST_FLOW_OK)
744 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
745 return GST_FLOW_ERROR;
753 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
755 /* -1 always maps to -1 */
761 /* 0 always maps to 0 */
768 *bytepos = gst_util_uint64_scale_ceil (ts, (guint64) wav->bps, GST_SECOND);
770 } else if (wav->fact) {
772 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
773 *bytepos = gst_util_uint64_scale_ceil (ts, bps, GST_SECOND);
780 /* This function is used to perform seeks on the element.
782 * It also works when event is NULL, in which case it will just
783 * start from the last configured segment. This technique is
784 * used when activating the element and to perform the seek in
788 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
792 GstFormat format, bformat;
794 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
795 gint64 cur, stop, upstream_size;
798 GstSegment seeksegment = { 0, };
802 GST_DEBUG_OBJECT (wav, "doing seek with event");
804 gst_event_parse_seek (event, &rate, &format, &flags,
805 &cur_type, &cur, &stop_type, &stop);
807 /* no negative rates yet */
811 if (format != wav->segment.format) {
812 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
813 gst_format_get_name (format),
814 gst_format_get_name (wav->segment.format));
816 if (cur_type != GST_SEEK_TYPE_NONE)
818 gst_pad_query_convert (wav->srcpad, format, cur,
819 wav->segment.format, &cur);
820 if (res && stop_type != GST_SEEK_TYPE_NONE)
822 gst_pad_query_convert (wav->srcpad, format, stop,
823 wav->segment.format, &stop);
827 format = wav->segment.format;
830 GST_DEBUG_OBJECT (wav, "doing seek without event");
833 cur_type = GST_SEEK_TYPE_SET;
834 stop_type = GST_SEEK_TYPE_SET;
837 /* in push mode, we must delegate to upstream */
838 if (wav->streaming) {
839 gboolean res = FALSE;
841 /* if streaming not yet started; only prepare initial newsegment */
842 if (!event || wav->state != GST_WAVPARSE_DATA) {
843 if (wav->start_segment)
844 gst_event_unref (wav->start_segment);
845 wav->start_segment = gst_event_new_segment (&wav->segment);
848 /* convert seek positions to byte positions in data sections */
849 if (format == GST_FORMAT_TIME) {
850 /* should not fail */
851 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
853 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
856 /* mind sample boundary and header */
858 cur -= (cur % wav->bytes_per_sample);
859 cur += wav->datastart;
862 stop -= (stop % wav->bytes_per_sample);
863 stop += wav->datastart;
865 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
866 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
868 /* BYTE seek event */
869 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
871 res = gst_pad_push_event (wav->sinkpad, event);
877 flush = flags & GST_SEEK_FLAG_FLUSH;
879 /* now we need to make sure the streaming thread is stopped. We do this by
880 * either sending a FLUSH_START event downstream which will cause the
881 * streaming thread to stop with a WRONG_STATE.
882 * For a non-flushing seek we simply pause the task, which will happen as soon
883 * as it completes one iteration (and thus might block when the sink is
884 * blocking in preroll). */
886 GST_DEBUG_OBJECT (wav, "sending flush start");
887 gst_pad_push_event (wav->srcpad, gst_event_new_flush_start ());
889 gst_pad_pause_task (wav->sinkpad);
892 /* we should now be able to grab the streaming thread because we stopped it
893 * with the above flush/pause code */
894 GST_PAD_STREAM_LOCK (wav->sinkpad);
896 /* save current position */
897 last_stop = wav->segment.position;
899 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
901 /* copy segment, we need this because we still need the old
902 * segment when we close the current segment. */
903 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
905 /* configure the seek parameters in the seeksegment. We will then have the
906 * right values in the segment to perform the seek */
908 GST_DEBUG_OBJECT (wav, "configuring seek");
909 gst_segment_do_seek (&seeksegment, rate, format, flags,
910 cur_type, cur, stop_type, stop, &update);
913 /* figure out the last position we need to play. If it's configured (stop !=
914 * -1), use that, else we play until the total duration of the file */
915 if ((stop = seeksegment.stop) == -1)
916 stop = seeksegment.duration;
918 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
919 if ((cur_type != GST_SEEK_TYPE_NONE)) {
920 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
921 * we can just copy the last_stop. If not, we use the bps to convert TIME to
923 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
924 (gint64 *) & wav->offset))
925 wav->offset = seeksegment.position;
926 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
927 wav->offset -= (wav->offset % wav->bytes_per_sample);
928 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
929 wav->offset += wav->datastart;
930 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
932 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
936 if (stop_type != GST_SEEK_TYPE_NONE) {
937 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
938 wav->end_offset = stop;
939 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
940 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
941 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
942 wav->end_offset += wav->datastart;
943 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
945 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
949 /* make sure filesize is not exceeded due to rounding errors or so,
950 * same precaution as in _stream_headers */
951 bformat = GST_FORMAT_BYTES;
952 if (gst_pad_peer_query_duration (wav->sinkpad, bformat, &upstream_size))
953 wav->end_offset = MIN (wav->end_offset, upstream_size);
955 /* this is the range of bytes we will use for playback */
956 wav->offset = MIN (wav->offset, wav->end_offset);
957 wav->dataleft = wav->end_offset - wav->offset;
959 GST_DEBUG_OBJECT (wav,
960 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
961 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
962 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
964 /* prepare for streaming again */
966 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
967 GST_DEBUG_OBJECT (wav, "sending flush stop");
968 gst_pad_push_event (wav->srcpad, gst_event_new_flush_stop (TRUE));
971 /* now we did the seek and can activate the new segment values */
972 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
974 /* if we're doing a segment seek, post a SEGMENT_START message */
975 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
976 gst_element_post_message (GST_ELEMENT_CAST (wav),
977 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
978 wav->segment.format, wav->segment.position));
981 /* now create the newsegment */
982 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
983 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
985 /* store the newsegment event so it can be sent from the streaming thread. */
986 if (wav->start_segment)
987 gst_event_unref (wav->start_segment);
988 wav->start_segment = gst_event_new_segment (&wav->segment);
990 /* mark discont if we are going to stream from another position. */
991 if (last_stop != wav->segment.position) {
992 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
996 /* and start the streaming task again */
997 if (!wav->streaming) {
998 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
1002 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
1009 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
1014 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
1019 GST_DEBUG_OBJECT (wav,
1020 "Could not determine byte position for desired time");
1026 * gst_wavparse_peek_chunk_info:
1027 * @wav Wavparse object
1028 * @tag holder for tag
1029 * @size holder for tag size
1031 * Peek next chunk info (tag and size)
1033 * Returns: %TRUE when the chunk info (header) is available
1036 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
1038 const guint8 *data = NULL;
1040 if (gst_adapter_available (wav->adapter) < 8)
1043 data = gst_adapter_map (wav->adapter, 8);
1044 *tag = GST_READ_UINT32_LE (data);
1045 *size = GST_READ_UINT32_LE (data + 4);
1046 gst_adapter_unmap (wav->adapter);
1048 GST_DEBUG ("Next chunk size is %u bytes, type %" GST_FOURCC_FORMAT, *size,
1049 GST_FOURCC_ARGS (*tag));
1055 * gst_wavparse_peek_chunk:
1056 * @wav Wavparse object
1057 * @tag holder for tag
1058 * @size holder for tag size
1060 * Peek enough data for one full chunk
1062 * Returns: %TRUE when the full chunk is available
1065 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
1067 guint32 peek_size = 0;
1070 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
1073 /* size 0 -> empty data buffer would surprise most callers,
1074 * large size -> do not bother trying to squeeze that into adapter,
1075 * so we throw poor man's exception, which can be caught if caller really
1076 * wants to handle 0 size chunk */
1077 if (!(*size) || (*size) >= (1 << 30)) {
1078 GST_INFO ("Invalid/unexpected chunk size %u for tag %" GST_FOURCC_FORMAT,
1079 *size, GST_FOURCC_ARGS (*tag));
1080 /* chain should give up */
1081 wav->abort_buffering = TRUE;
1084 peek_size = (*size + 1) & ~1;
1085 available = gst_adapter_available (wav->adapter);
1087 if (available >= (8 + peek_size)) {
1090 GST_LOG ("but only %u bytes available now", available);
1096 * gst_wavparse_calculate_duration:
1097 * @wav: wavparse object
1099 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
1102 * Returns: %TRUE if duration is available.
1105 gst_wavparse_calculate_duration (GstWavParse * wav)
1107 if (wav->duration > 0)
1111 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
1113 gst_util_uint64_scale_ceil (wav->datasize, GST_SECOND,
1114 (guint64) wav->bps);
1115 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
1116 GST_TIME_ARGS (wav->duration));
1118 } else if (wav->fact) {
1120 gst_util_uint64_scale_int_ceil (GST_SECOND, wav->fact, wav->rate);
1121 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
1122 GST_TIME_ARGS (wav->duration));
1129 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
1134 if (wav->streaming) {
1135 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1138 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
1139 GST_FOURCC_ARGS (tag));
1140 flush = 8 + ((size + 1) & ~1);
1141 wav->offset += flush;
1142 if (wav->streaming) {
1143 gst_adapter_flush (wav->adapter, flush);
1145 gst_buffer_unref (buf);
1152 * gst_wavparse_cue_chunk:
1153 * @wav GstWavParse object
1154 * @data holder for data
1155 * @size holder for data size
1157 * Parse cue chunk from @data to wav->cues.
1159 * Returns: %TRUE when cue chunk is available
1162 gst_wavparse_cue_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
1166 GstWavParseCue *cue;
1169 GST_WARNING_OBJECT (wav, "found another cue's");
1173 ncues = GST_READ_UINT32_LE (data);
1175 if (size < 4 + ncues * 24) {
1176 GST_WARNING_OBJECT (wav, "broken file %d %d", size, ncues);
1182 for (i = 0; i < ncues; i++) {
1183 cue = g_new0 (GstWavParseCue, 1);
1184 cue->id = GST_READ_UINT32_LE (data);
1185 cue->position = GST_READ_UINT32_LE (data + 4);
1186 cue->data_chunk_id = GST_READ_UINT32_LE (data + 8);
1187 cue->chunk_start = GST_READ_UINT32_LE (data + 12);
1188 cue->block_start = GST_READ_UINT32_LE (data + 16);
1189 cue->sample_offset = GST_READ_UINT32_LE (data + 20);
1190 cues = g_list_append (cues, cue);
1200 * gst_wavparse_labl_chunk:
1201 * @wav GstWavParse object
1202 * @data holder for data
1203 * @size holder for data size
1205 * Parse labl from @data to wav->labls.
1207 * Returns: %TRUE when labl chunk is available
1210 gst_wavparse_labl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
1212 GstWavParseLabl *labl;
1217 labl = g_new0 (GstWavParseLabl, 1);
1221 labl->cue_point_id = GST_READ_UINT32_LE (data);
1222 labl->text = (gchar *) g_new0 (gchar *, size - 4 + 1);
1223 memcpy (labl->text, data + 4, size - 4);
1225 wav->labls = g_list_append (wav->labls, labl);
1231 * gst_wavparse_adtl_chunk:
1232 * @wav GstWavParse object
1233 * @data holder for data
1234 * @size holder for data size
1236 * Parse adtl from @data.
1238 * Returns: %TRUE when adtl chunk is available
1241 gst_wavparse_adtl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
1243 guint32 ltag, lsize, offset = 0;
1246 ltag = GST_READ_UINT32_LE (data + offset);
1247 lsize = GST_READ_UINT32_LE (data + offset + 4);
1249 case GST_RIFF_TAG_labl:
1250 gst_wavparse_labl_chunk (wav, data + offset, size);
1254 offset += 8 + GST_ROUND_UP_2 (lsize);
1255 size -= 8 + GST_ROUND_UP_2 (lsize);
1262 * gst_wavparse_create_toc:
1263 * @wav GstWavParse object
1265 * Create TOC from wav->cues and wav->labls.
1268 gst_wavparse_create_toc (GstWavParse * wav)
1273 GstWavParseCue *cue;
1274 GstWavParseLabl *labl;
1277 GstTocEntry *entry = NULL, *cur_subentry = NULL, *prev_subentry = NULL;
1279 GST_OBJECT_LOCK (wav);
1281 GST_OBJECT_UNLOCK (wav);
1282 GST_WARNING_OBJECT (wav, "found another TOC");
1287 GST_OBJECT_UNLOCK (wav);
1291 /* FIXME: send CURRENT scope toc too */
1292 toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
1294 /* add cue edition */
1295 entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, "cue");
1296 gst_toc_entry_set_start_stop_times (entry, 0, wav->duration);
1297 gst_toc_append_entry (toc, entry);
1299 /* add tracks in cue edition */
1300 list = g_list_first (wav->cues);
1303 prev_subentry = cur_subentry;
1304 /* previous track stop time = current track start time */
1305 if (prev_subentry != NULL) {
1306 gst_toc_entry_get_start_stop_times (prev_subentry, &start, NULL);
1307 stop = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
1308 gst_toc_entry_set_start_stop_times (prev_subentry, start, stop);
1310 id = g_strdup_printf ("%08x", cue->id);
1311 cur_subentry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_TRACK, id);
1313 start = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
1314 stop = wav->duration;
1315 gst_toc_entry_set_start_stop_times (cur_subentry, start, stop);
1316 gst_toc_entry_append_sub_entry (entry, cur_subentry);
1317 list = g_list_next (list);
1320 /* add tags in tracks */
1321 list = g_list_first (wav->labls);
1324 id = g_strdup_printf ("%08x", labl->cue_point_id);
1325 cur_subentry = gst_toc_find_entry (toc, id);
1327 if (cur_subentry != NULL) {
1328 tags = gst_tag_list_new_empty ();
1329 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, labl->text,
1331 gst_toc_entry_set_tags (cur_subentry, tags);
1333 list = g_list_next (list);
1336 /* send data as TOC */
1339 /* send TOC event */
1341 GST_OBJECT_UNLOCK (wav);
1342 gst_pad_push_event (wav->srcpad, gst_event_new_toc (wav->toc, FALSE));
1348 #define MAX_BUFFER_SIZE 4096
1350 static GstFlowReturn
1351 gst_wavparse_stream_headers (GstWavParse * wav)
1353 GstFlowReturn res = GST_FLOW_OK;
1354 GstBuffer *buf = NULL;
1355 gst_riff_strf_auds *header = NULL;
1357 gboolean gotdata = FALSE;
1358 GstCaps *caps = NULL;
1359 gchar *codec_name = NULL;
1361 gint64 upstream_size = 0;
1363 /* search for "_fmt" chunk, which should be first */
1364 while (!wav->got_fmt) {
1367 /* The header starts with a 'fmt ' tag */
1368 if (wav->streaming) {
1369 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1372 gst_adapter_flush (wav->adapter, 8);
1376 buf = gst_adapter_take_buffer (wav->adapter, size);
1378 gst_adapter_flush (wav->adapter, 1);
1379 wav->offset += GST_ROUND_UP_2 (size);
1381 buf = gst_buffer_new ();
1384 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1385 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1389 if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ ||
1390 tag == GST_RIFF_TAG_bext || tag == GST_RIFF_TAG_BEXT ||
1391 tag == GST_RIFF_TAG_LIST || tag == GST_RIFF_TAG_ID32 ||
1392 tag == GST_RIFF_TAG_IDVX) {
1393 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1394 GST_FOURCC_ARGS (tag));
1395 gst_buffer_unref (buf);
1400 if (tag != GST_RIFF_TAG_fmt)
1403 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1405 goto parse_header_error;
1407 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1409 /* do sanity checks of header fields */
1410 if (header->channels == 0)
1412 if (header->rate == 0)
1415 GST_DEBUG_OBJECT (wav, "creating the caps");
1417 /* Note: gst_riff_create_audio_caps might need to fix values in
1418 * the header header depending on the format, so call it first */
1419 /* FIXME: Need to handle the channel reorder map */
1420 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1421 NULL, &codec_name, NULL);
1424 gst_buffer_unref (extra);
1427 goto unknown_format;
1429 /* do more sanity checks of header fields
1430 * (these can be sanitized by gst_riff_create_audio_caps()
1432 wav->format = header->format;
1433 wav->rate = header->rate;
1434 wav->channels = header->channels;
1435 wav->blockalign = header->blockalign;
1436 wav->depth = header->bits_per_sample;
1437 wav->av_bps = header->av_bps;
1443 /* do format specific handling */
1444 switch (wav->format) {
1445 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1446 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1448 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1449 * bitrate inside the mpeg stream */
1450 GST_INFO ("resetting bps from %u to 0 for mp2/3", wav->av_bps);
1454 case GST_RIFF_WAVE_FORMAT_PCM:
1455 if (wav->blockalign > wav->channels * ((wav->depth + 7) / 8))
1456 goto invalid_blockalign;
1459 if (wav->av_bps > wav->blockalign * wav->rate)
1461 /* use the configured bps */
1462 wav->bps = wav->av_bps;
1466 wav->width = (wav->blockalign * 8) / wav->channels;
1467 wav->bytes_per_sample = wav->channels * wav->width / 8;
1469 if (wav->bytes_per_sample <= 0)
1470 goto no_bytes_per_sample;
1472 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1473 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1474 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1475 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1476 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1477 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1478 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1480 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1481 * formats). This will make the element output a BYTE format segment and
1482 * will not timestamp the outgoing buffers.
1484 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1486 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1488 /* create pad later so we can sniff the first few bytes
1489 * of the real data and correct our caps if necessary */
1490 gst_caps_replace (&wav->caps, caps);
1491 gst_caps_replace (&caps, NULL);
1493 wav->got_fmt = TRUE;
1496 wav->tags = gst_tag_list_new_empty ();
1498 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1499 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1501 g_free (codec_name);
1507 gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1508 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1510 /* loop headers until we get data */
1512 if (wav->streaming) {
1513 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1520 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1521 &buf)) != GST_FLOW_OK)
1522 goto header_read_error;
1523 gst_buffer_map (buf, &map, GST_MAP_READ);
1524 tag = GST_READ_UINT32_LE (map.data);
1525 size = GST_READ_UINT32_LE (map.data + 4);
1526 gst_buffer_unmap (buf, &map);
1529 GST_INFO_OBJECT (wav,
1530 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT,
1531 GST_FOURCC_ARGS (tag), wav->offset);
1533 /* wav is a st00pid format, we don't know for sure where data starts.
1534 * So we have to go bit by bit until we find the 'data' header
1537 case GST_RIFF_TAG_data:{
1538 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %u", size);
1539 if (wav->ignore_length) {
1540 GST_DEBUG_OBJECT (wav, "Ignoring length");
1543 if (wav->streaming) {
1544 gst_adapter_flush (wav->adapter, 8);
1547 gst_buffer_unref (buf);
1550 wav->datastart = wav->offset;
1551 /* If size is zero, then the data chunk probably actually extends to
1552 the end of the file */
1553 if (size == 0 && upstream_size) {
1554 size = upstream_size - wav->datastart;
1556 /* Or the file might be truncated */
1557 else if (upstream_size) {
1558 size = MIN (size, (upstream_size - wav->datastart));
1560 wav->datasize = (guint64) size;
1561 wav->dataleft = (guint64) size;
1562 wav->end_offset = size + wav->datastart;
1563 if (!wav->streaming) {
1564 /* We will continue parsing tags 'till end */
1565 wav->offset += size;
1567 GST_DEBUG_OBJECT (wav, "datasize = %u", size);
1570 case GST_RIFF_TAG_fact:{
1571 if (wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1572 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1573 const guint data_size = 4;
1575 GST_INFO_OBJECT (wav, "Have fact chunk");
1576 if (size < data_size) {
1577 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1578 /* need more data */
1581 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1585 /* number of samples (for compressed formats) */
1586 if (wav->streaming) {
1587 const guint8 *data = NULL;
1589 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1592 gst_adapter_flush (wav->adapter, 8);
1593 data = gst_adapter_map (wav->adapter, data_size);
1594 wav->fact = GST_READ_UINT32_LE (data);
1595 gst_adapter_unmap (wav->adapter);
1596 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1598 gst_buffer_unref (buf);
1601 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1602 data_size, &buf)) != GST_FLOW_OK)
1603 goto header_read_error;
1604 gst_buffer_extract (buf, 0, &wav->fact, 4);
1605 wav->fact = GUINT32_FROM_LE (wav->fact);
1606 gst_buffer_unref (buf);
1608 GST_DEBUG_OBJECT (wav, "have fact %u", wav->fact);
1609 wav->offset += 8 + GST_ROUND_UP_2 (size);
1612 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1613 /* need more data */
1619 case GST_RIFF_TAG_acid:{
1620 const gst_riff_acid *acid = NULL;
1621 const guint data_size = sizeof (gst_riff_acid);
1624 GST_INFO_OBJECT (wav, "Have acid chunk");
1625 if (size < data_size) {
1626 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1627 /* need more data */
1630 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1634 if (wav->streaming) {
1635 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1638 gst_adapter_flush (wav->adapter, 8);
1639 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1641 tempo = acid->tempo;
1642 gst_adapter_unmap (wav->adapter);
1645 gst_buffer_unref (buf);
1648 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1649 size, &buf)) != GST_FLOW_OK)
1650 goto header_read_error;
1651 gst_buffer_map (buf, &map, GST_MAP_READ);
1652 acid = (const gst_riff_acid *) map.data;
1653 tempo = acid->tempo;
1654 gst_buffer_unmap (buf, &map);
1656 /* send data as tags */
1658 wav->tags = gst_tag_list_new_empty ();
1659 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1660 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1662 size = GST_ROUND_UP_2 (size);
1663 if (wav->streaming) {
1664 gst_adapter_flush (wav->adapter, size);
1666 gst_buffer_unref (buf);
1668 wav->offset += 8 + size;
1671 /* FIXME: all list tags after data are ignored in streaming mode */
1672 case GST_RIFF_TAG_LIST:{
1675 if (wav->streaming) {
1676 const guint8 *data = NULL;
1678 if (gst_adapter_available (wav->adapter) < 12) {
1681 data = gst_adapter_map (wav->adapter, 12);
1682 ltag = GST_READ_UINT32_LE (data + 8);
1683 gst_adapter_unmap (wav->adapter);
1685 gst_buffer_unref (buf);
1688 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1689 &buf)) != GST_FLOW_OK)
1690 goto header_read_error;
1691 gst_buffer_extract (buf, 8, <ag, 4);
1692 ltag = GUINT32_FROM_LE (ltag);
1695 case GST_RIFF_LIST_INFO:{
1696 const gint data_size = size - 4;
1699 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1700 if (wav->streaming) {
1701 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1704 gst_adapter_flush (wav->adapter, 12);
1706 if (data_size > 0) {
1707 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1709 gst_adapter_flush (wav->adapter, 1);
1713 gst_buffer_unref (buf);
1715 if (data_size > 0) {
1717 gst_pad_pull_range (wav->sinkpad, wav->offset,
1718 data_size, &buf)) != GST_FLOW_OK)
1719 goto header_read_error;
1722 if (data_size > 0) {
1724 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1726 GstTagList *old = wav->tags;
1728 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1730 gst_tag_list_unref (old);
1731 gst_tag_list_unref (new);
1733 gst_buffer_unref (buf);
1734 wav->offset += GST_ROUND_UP_2 (data_size);
1738 case GST_RIFF_LIST_adtl:{
1739 const gint data_size = size;
1741 GST_INFO_OBJECT (wav, "Have 'adtl' LIST, size %u", data_size);
1742 if (wav->streaming) {
1743 const guint8 *data = NULL;
1745 gst_adapter_flush (wav->adapter, 12);
1746 data = gst_adapter_map (wav->adapter, data_size);
1747 gst_wavparse_adtl_chunk (wav, data, data_size);
1748 gst_adapter_unmap (wav->adapter);
1752 gst_buffer_unref (buf);
1755 gst_pad_pull_range (wav->sinkpad, wav->offset + 12,
1756 data_size, &buf)) != GST_FLOW_OK)
1757 goto header_read_error;
1758 gst_buffer_map (buf, &map, GST_MAP_READ);
1759 gst_wavparse_adtl_chunk (wav, (const guint8 *) map.data,
1761 gst_buffer_unmap (buf, &map);
1765 GST_INFO_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1766 GST_FOURCC_ARGS (ltag));
1767 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1768 /* need more data */
1774 case GST_RIFF_TAG_cue:{
1775 const guint data_size = size;
1777 GST_DEBUG_OBJECT (wav, "Have 'cue' TAG, size : %u", data_size);
1778 if (wav->streaming) {
1779 const guint8 *data = NULL;
1781 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1784 gst_adapter_flush (wav->adapter, 8);
1786 data = gst_adapter_map (wav->adapter, data_size);
1787 if (!gst_wavparse_cue_chunk (wav, data, data_size)) {
1788 goto header_read_error;
1790 gst_adapter_unmap (wav->adapter);
1795 gst_buffer_unref (buf);
1798 gst_pad_pull_range (wav->sinkpad, wav->offset,
1799 data_size, &buf)) != GST_FLOW_OK)
1800 goto header_read_error;
1801 gst_buffer_map (buf, &map, GST_MAP_READ);
1802 if (!gst_wavparse_cue_chunk (wav, (const guint8 *) map.data,
1804 goto header_read_error;
1806 gst_buffer_unmap (buf, &map);
1808 size = GST_ROUND_UP_2 (size);
1809 if (wav->streaming) {
1810 gst_adapter_flush (wav->adapter, size);
1812 gst_buffer_unref (buf);
1814 size = GST_ROUND_UP_2 (size);
1815 wav->offset += size;
1819 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1820 /* need more data */
1825 if (upstream_size && (wav->offset >= upstream_size)) {
1826 /* Now we are gone through the whole file */
1831 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1833 if (wav->bps <= 0 && wav->fact) {
1835 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1837 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1838 (guint64) wav->fact);
1839 GST_INFO_OBJECT (wav, "calculated bps : %u, enabling VBR", wav->bps);
1844 if (gst_wavparse_calculate_duration (wav)) {
1845 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1846 if (!wav->ignore_length)
1847 wav->segment.duration = wav->duration;
1849 gst_wavparse_create_toc (wav);
1851 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1852 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1853 if (!wav->ignore_length)
1854 wav->segment.duration = wav->datasize;
1857 /* now we have all the info to perform a pending seek if any, if no
1858 * event, this will still do the right thing and it will also send
1859 * the right newsegment event downstream. */
1860 gst_wavparse_perform_seek (wav, wav->seek_event);
1861 /* remove pending event */
1862 event_p = &wav->seek_event;
1863 gst_event_replace (event_p, NULL);
1865 /* we just started, we are discont */
1866 wav->discont = TRUE;
1868 wav->state = GST_WAVPARSE_DATA;
1870 /* determine reasonable max buffer size,
1871 * that is, buffers not too small either size or time wise
1872 * so we do not end up with too many of them */
1875 gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size);
1876 wav->max_buf_size = upstream_size;
1877 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1878 if (wav->blockalign > 0)
1879 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1881 GST_DEBUG_OBJECT (wav, "max buffer size %u", wav->max_buf_size);
1889 g_free (codec_name);
1893 gst_caps_unref (caps);
1898 res = GST_FLOW_ERROR;
1903 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1904 ("Invalid WAV header (no fmt at start): %"
1905 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1910 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1911 ("Couldn't parse audio header"));
1916 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1917 ("Stream claims to contain no channels - invalid data"));
1922 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1923 ("Stream with sample_rate == 0 - invalid data"));
1928 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1929 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1930 wav->blockalign, wav->channels * ((wav->depth + 7) / 8)));
1935 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1936 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1937 wav->av_bps, wav->blockalign * wav->rate));
1940 no_bytes_per_sample:
1942 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1943 ("Could not caluclate bytes per sample - invalid data"));
1948 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1949 ("No caps found for format 0x%x, %u channels, %u Hz",
1950 wav->format, wav->channels, wav->rate));
1955 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1956 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1962 * Read WAV file tag when streaming
1964 static GstFlowReturn
1965 gst_wavparse_parse_stream_init (GstWavParse * wav)
1967 if (gst_adapter_available (wav->adapter) >= 12) {
1970 /* _take flushes the data */
1971 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1973 GST_DEBUG ("Parsing wav header");
1974 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1975 return GST_FLOW_ERROR;
1978 /* Go to next state */
1979 wav->state = GST_WAVPARSE_HEADER;
1984 /* handle an event sent directly to the element.
1986 * This event can be sent either in the READY state or the
1987 * >READY state. The only event of interest really is the seek
1990 * In the READY state we can only store the event and try to
1991 * respect it when going to PAUSED. We assume we are in the
1992 * READY state when our parsing state != GST_WAVPARSE_DATA.
1994 * When we are steaming, we can simply perform the seek right
1998 gst_wavparse_send_event (GstElement * element, GstEvent * event)
2000 GstWavParse *wav = GST_WAVPARSE (element);
2001 gboolean res = FALSE;
2004 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
2006 switch (GST_EVENT_TYPE (event)) {
2007 case GST_EVENT_SEEK:
2008 if (wav->state == GST_WAVPARSE_DATA) {
2009 /* we can handle the seek directly when streaming data */
2010 res = gst_wavparse_perform_seek (wav, event);
2012 GST_DEBUG_OBJECT (wav, "queuing seek for later");
2014 event_p = &wav->seek_event;
2015 gst_event_replace (event_p, event);
2017 /* we always return true */
2024 gst_event_unref (event);
2029 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
2033 s = gst_caps_get_structure (caps, 0);
2034 if (!gst_structure_has_name (s, "audio/x-dts"))
2036 if (prob >= GST_TYPE_FIND_LIKELY)
2038 /* DTS at non-0 offsets and without second sync may yield POSSIBLE .. */
2039 if (prob < GST_TYPE_FIND_POSSIBLE)
2041 /* .. in which case we want at least a valid-looking rate and channels */
2042 if (!gst_structure_has_field (s, "channels"))
2044 /* and for extra assurance we could also check the rate from the DTS frame
2045 * against the one in the wav header, but for now let's not do that */
2046 return gst_structure_has_field (s, "rate");
2050 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
2054 GST_DEBUG_OBJECT (wav, "adding src pad");
2056 g_assert (wav->caps != NULL);
2058 s = gst_caps_get_structure (wav->caps, 0);
2059 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
2060 GstTypeFindProbability prob;
2063 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
2064 if (tf_caps != NULL) {
2065 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
2066 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
2067 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
2068 gst_caps_unref (wav->caps);
2069 wav->caps = tf_caps;
2071 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
2072 GST_TAG_AUDIO_CODEC, "dts", NULL);
2074 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
2075 "marked as raw PCM audio, but ignoring for now", tf_caps);
2076 gst_caps_unref (tf_caps);
2081 gst_pad_set_caps (wav->srcpad, wav->caps);
2082 gst_caps_replace (&wav->caps, NULL);
2084 if (wav->start_segment) {
2085 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
2086 gst_pad_push_event (wav->srcpad, wav->start_segment);
2087 wav->start_segment = NULL;
2091 gst_pad_push_event (wav->srcpad, gst_event_new_tag (wav->tags));
2096 static GstFlowReturn
2097 gst_wavparse_stream_data (GstWavParse * wav)
2099 GstBuffer *buf = NULL;
2100 GstFlowReturn res = GST_FLOW_OK;
2101 guint64 desired, obtained;
2102 GstClockTime timestamp, next_timestamp, duration;
2103 guint64 pos, nextpos;
2106 GST_LOG_OBJECT (wav,
2107 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
2108 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
2110 /* Get the next n bytes and output them */
2111 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
2114 /* scale the amount of data by the segment rate so we get equal
2115 * amounts of data regardless of the playback rate */
2117 MIN (gst_guint64_to_gdouble (wav->dataleft),
2118 wav->max_buf_size * ABS (wav->segment.rate));
2120 if (desired >= wav->blockalign && wav->blockalign > 0)
2121 desired -= (desired % wav->blockalign);
2123 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
2124 "from the sinkpad", desired);
2126 if (wav->streaming) {
2127 guint avail = gst_adapter_available (wav->adapter);
2130 /* flush some bytes if evil upstream sends segment that starts
2131 * before data or does is not send sample aligned segment */
2132 if (G_LIKELY (wav->offset >= wav->datastart)) {
2133 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
2135 extra = wav->datastart - wav->offset;
2138 if (G_UNLIKELY (extra)) {
2139 extra = wav->bytes_per_sample - extra;
2140 if (extra <= avail) {
2141 GST_DEBUG_OBJECT (wav, "flushing %u bytes to sample boundary", extra);
2142 gst_adapter_flush (wav->adapter, extra);
2143 wav->offset += extra;
2144 wav->dataleft -= extra;
2145 goto iterate_adapter;
2147 GST_DEBUG_OBJECT (wav, "flushing %u bytes", avail);
2148 gst_adapter_clear (wav->adapter);
2149 wav->offset += avail;
2150 wav->dataleft -= avail;
2155 if (avail < desired) {
2156 GST_LOG_OBJECT (wav, "Got only %u bytes of data from the sinkpad", avail);
2160 buf = gst_adapter_take_buffer (wav->adapter, desired);
2162 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
2163 desired, &buf)) != GST_FLOW_OK)
2166 /* we may get a short buffer at the end of the file */
2167 if (gst_buffer_get_size (buf) < desired) {
2168 gsize size = gst_buffer_get_size (buf);
2170 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
2171 if (size >= wav->blockalign) {
2172 buf = gst_buffer_make_writable (buf);
2173 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
2175 gst_buffer_unref (buf);
2181 obtained = gst_buffer_get_size (buf);
2183 /* our positions in bytes */
2184 pos = wav->offset - wav->datastart;
2185 nextpos = pos + obtained;
2187 /* update offsets, does not overflow. */
2188 buf = gst_buffer_make_writable (buf);
2189 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
2190 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
2192 /* first chunk of data? create the source pad. We do this only here so
2193 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
2194 if (G_UNLIKELY (wav->first)) {
2196 /* this will also push the segment events */
2197 gst_wavparse_add_src_pad (wav, buf);
2199 /* If we have a pending start segment, send it now. */
2200 if (G_UNLIKELY (wav->start_segment != NULL)) {
2201 gst_pad_push_event (wav->srcpad, wav->start_segment);
2202 wav->start_segment = NULL;
2207 /* and timestamps if we have a bitrate, be careful for overflows */
2209 gst_util_uint64_scale_ceil (pos, GST_SECOND, (guint64) wav->bps);
2211 gst_util_uint64_scale_ceil (nextpos, GST_SECOND, (guint64) wav->bps);
2212 duration = next_timestamp - timestamp;
2214 /* update current running segment position */
2215 if (G_LIKELY (next_timestamp >= wav->segment.start))
2216 wav->segment.position = next_timestamp;
2217 } else if (wav->fact) {
2219 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2220 /* and timestamps if we have a bitrate, be careful for overflows */
2221 timestamp = gst_util_uint64_scale_ceil (pos, GST_SECOND, bps);
2222 next_timestamp = gst_util_uint64_scale_ceil (nextpos, GST_SECOND, bps);
2223 duration = next_timestamp - timestamp;
2225 /* no bitrate, all we know is that the first sample has timestamp 0, all
2226 * other positions and durations have unknown timestamp. */
2230 timestamp = GST_CLOCK_TIME_NONE;
2231 duration = GST_CLOCK_TIME_NONE;
2232 /* update current running segment position with byte offset */
2233 if (G_LIKELY (nextpos >= wav->segment.start))
2234 wav->segment.position = nextpos;
2236 if ((pos > 0) && wav->vbr) {
2237 /* don't set timestamps for VBR files if it's not the first buffer */
2238 timestamp = GST_CLOCK_TIME_NONE;
2239 duration = GST_CLOCK_TIME_NONE;
2242 GST_DEBUG_OBJECT (wav, "marking DISCONT");
2243 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2244 wav->discont = FALSE;
2247 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2248 GST_BUFFER_DURATION (buf) = duration;
2250 GST_LOG_OBJECT (wav,
2251 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
2252 ", size:%" G_GSIZE_FORMAT, GST_TIME_ARGS (timestamp),
2253 GST_TIME_ARGS (duration), gst_buffer_get_size (buf));
2255 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
2258 if (obtained < wav->dataleft) {
2259 wav->offset += obtained;
2260 wav->dataleft -= obtained;
2262 wav->offset += wav->dataleft;
2266 /* Iterate until need more data, so adapter size won't grow */
2267 if (wav->streaming) {
2268 GST_LOG_OBJECT (wav,
2269 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
2271 goto iterate_adapter;
2278 GST_DEBUG_OBJECT (wav, "found EOS");
2279 return GST_FLOW_EOS;
2283 /* check if we got EOS */
2284 if (res == GST_FLOW_EOS)
2287 GST_WARNING_OBJECT (wav,
2288 "Error getting %" G_GINT64_FORMAT " bytes from the "
2289 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
2294 GST_INFO_OBJECT (wav,
2295 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
2296 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
2297 gst_pad_is_linked (wav->srcpad));
2303 gst_wavparse_loop (GstPad * pad)
2306 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2308 GST_LOG_OBJECT (wav, "process data");
2310 switch (wav->state) {
2311 case GST_WAVPARSE_START:
2312 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2313 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2316 wav->state = GST_WAVPARSE_HEADER;
2319 case GST_WAVPARSE_HEADER:
2320 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2321 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2324 wav->state = GST_WAVPARSE_DATA;
2325 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2328 case GST_WAVPARSE_DATA:
2329 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2333 g_assert_not_reached ();
2340 const gchar *reason = gst_flow_get_name (ret);
2342 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2343 gst_pad_pause_task (pad);
2345 if (ret == GST_FLOW_EOS) {
2346 /* handle end-of-stream/segment */
2347 /* so align our position with the end of it, if there is one
2348 * this ensures a subsequent will arrive at correct base/acc time */
2349 if (wav->segment.format == GST_FORMAT_TIME) {
2350 if (wav->segment.rate > 0.0 &&
2351 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2352 wav->segment.position = wav->segment.stop;
2353 else if (wav->segment.rate < 0.0)
2354 wav->segment.position = wav->segment.start;
2356 if (wav->state == GST_WAVPARSE_START) {
2357 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2358 ("No valid input found before end of stream"));
2359 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2361 /* add pad before we perform EOS */
2362 if (G_UNLIKELY (wav->first)) {
2364 gst_wavparse_add_src_pad (wav, NULL);
2367 /* perform EOS logic */
2368 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2371 if ((stop = wav->segment.stop) == -1)
2372 stop = wav->segment.duration;
2374 gst_element_post_message (GST_ELEMENT_CAST (wav),
2375 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2376 wav->segment.format, stop));
2377 gst_pad_push_event (wav->srcpad,
2378 gst_event_new_segment_done (wav->segment.format, stop));
2380 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2383 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2384 /* for fatal errors we post an error message, post the error
2385 * first so the app knows about the error first. */
2386 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
2387 (_("Internal data flow error.")),
2388 ("streaming task paused, reason %s (%d)", reason, ret));
2389 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2395 static GstFlowReturn
2396 gst_wavparse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2399 GstWavParse *wav = GST_WAVPARSE (parent);
2401 GST_LOG_OBJECT (wav, "adapter_push %" G_GSIZE_FORMAT " bytes",
2402 gst_buffer_get_size (buf));
2404 gst_adapter_push (wav->adapter, buf);
2406 switch (wav->state) {
2407 case GST_WAVPARSE_START:
2408 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2409 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2412 if (wav->state != GST_WAVPARSE_HEADER)
2415 /* otherwise fall-through */
2416 case GST_WAVPARSE_HEADER:
2417 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2418 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2421 if (!wav->got_fmt || wav->datastart == 0)
2424 wav->state = GST_WAVPARSE_DATA;
2425 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2428 case GST_WAVPARSE_DATA:
2429 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2430 wav->discont = TRUE;
2431 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2435 g_return_val_if_reached (GST_FLOW_ERROR);
2438 if (G_UNLIKELY (wav->abort_buffering)) {
2439 wav->abort_buffering = FALSE;
2440 ret = GST_FLOW_ERROR;
2441 /* sort of demux/parse error */
2442 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2448 static GstFlowReturn
2449 gst_wavparse_flush_data (GstWavParse * wav)
2451 GstFlowReturn ret = GST_FLOW_OK;
2454 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2456 wav->end_offset = wav->offset + av;
2457 ret = gst_wavparse_stream_data (wav);
2464 gst_wavparse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2466 GstWavParse *wav = GST_WAVPARSE (parent);
2467 gboolean ret = TRUE;
2469 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2471 switch (GST_EVENT_TYPE (event)) {
2472 case GST_EVENT_CAPS:
2474 /* discard, we'll come up with proper src caps */
2475 gst_event_unref (event);
2478 case GST_EVENT_SEGMENT:
2480 gint64 start, stop, offset = 0, end_offset = -1;
2483 /* some debug output */
2484 gst_event_copy_segment (event, &segment);
2485 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2488 if (wav->state != GST_WAVPARSE_DATA) {
2489 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2493 /* now we are either committed to TIME or BYTE format,
2494 * and we only expect a BYTE segment, e.g. following a seek */
2495 if (segment.format == GST_FORMAT_BYTES) {
2496 /* handle (un)signed issues */
2497 start = segment.start;
2498 stop = segment.stop;
2501 start -= wav->datastart;
2502 start = MAX (start, 0);
2506 segment.stop -= wav->datastart;
2507 segment.stop = MAX (stop, 0);
2509 if (wav->segment.format == GST_FORMAT_TIME) {
2510 guint64 bps = wav->bps;
2512 /* operating in format TIME, so we can convert */
2513 if (!bps && wav->fact)
2515 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2519 gst_util_uint64_scale_ceil (start, GST_SECOND,
2520 (guint64) wav->bps);
2523 gst_util_uint64_scale_ceil (stop, GST_SECOND,
2524 (guint64) wav->bps);
2528 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2532 segment.start = start;
2533 segment.stop = stop;
2535 /* accept upstream's notion of segment and distribute along */
2536 segment.format = wav->segment.format;
2537 segment.time = segment.position = segment.start;
2538 segment.duration = wav->segment.duration;
2539 segment.base = gst_segment_to_running_time (&wav->segment,
2540 GST_FORMAT_TIME, wav->segment.position);
2542 gst_segment_copy_into (&segment, &wav->segment);
2544 /* also store the newsegment event for the streaming thread */
2545 if (wav->start_segment)
2546 gst_event_unref (wav->start_segment);
2547 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2548 wav->start_segment = gst_event_new_segment (&segment);
2550 /* stream leftover data in current segment */
2551 gst_wavparse_flush_data (wav);
2552 /* and set up streaming thread for next one */
2553 wav->offset = offset;
2554 wav->end_offset = end_offset;
2555 if (wav->end_offset > 0) {
2556 wav->dataleft = wav->end_offset - wav->offset;
2558 /* infinity; upstream will EOS when done */
2559 wav->dataleft = G_MAXUINT64;
2562 gst_event_unref (event);
2566 if (wav->state == GST_WAVPARSE_START) {
2567 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2568 ("No valid input found before end of stream"));
2570 /* add pad if needed so EOS is seen downstream */
2571 if (G_UNLIKELY (wav->first)) {
2573 gst_wavparse_add_src_pad (wav, NULL);
2575 /* stream leftover data in current segment */
2576 gst_wavparse_flush_data (wav);
2581 case GST_EVENT_FLUSH_STOP:
2585 gst_adapter_clear (wav->adapter);
2586 wav->discont = TRUE;
2587 dur = wav->segment.duration;
2588 gst_segment_init (&wav->segment, wav->segment.format);
2589 wav->segment.duration = dur;
2593 ret = gst_pad_event_default (wav->sinkpad, parent, event);
2601 /* convert and query stuff */
2602 static const GstFormat *
2603 gst_wavparse_get_formats (GstPad * pad)
2605 static GstFormat formats[] = {
2608 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2617 gst_wavparse_pad_convert (GstPad * pad,
2618 GstFormat src_format, gint64 src_value,
2619 GstFormat * dest_format, gint64 * dest_value)
2621 GstWavParse *wavparse;
2622 gboolean res = TRUE;
2624 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2626 if (*dest_format == src_format) {
2627 *dest_value = src_value;
2631 if ((wavparse->bps == 0) && !wavparse->fact)
2634 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2635 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2637 switch (src_format) {
2638 case GST_FORMAT_BYTES:
2639 switch (*dest_format) {
2640 case GST_FORMAT_DEFAULT:
2641 *dest_value = src_value / wavparse->bytes_per_sample;
2642 /* make sure we end up on a sample boundary */
2643 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2645 case GST_FORMAT_TIME:
2646 /* src_value + datastart = offset */
2647 GST_INFO_OBJECT (wavparse,
2648 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2650 if (wavparse->bps > 0)
2651 *dest_value = gst_util_uint64_scale_ceil (src_value, GST_SECOND,
2652 (guint64) wavparse->bps);
2653 else if (wavparse->fact) {
2654 guint64 bps = gst_util_uint64_scale_int_ceil (wavparse->datasize,
2655 wavparse->rate, wavparse->fact);
2658 gst_util_uint64_scale_int_ceil (src_value, GST_SECOND, bps);
2669 case GST_FORMAT_DEFAULT:
2670 switch (*dest_format) {
2671 case GST_FORMAT_BYTES:
2672 *dest_value = src_value * wavparse->bytes_per_sample;
2674 case GST_FORMAT_TIME:
2675 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2676 (guint64) wavparse->rate);
2684 case GST_FORMAT_TIME:
2685 switch (*dest_format) {
2686 case GST_FORMAT_BYTES:
2687 if (wavparse->bps > 0)
2688 *dest_value = gst_util_uint64_scale (src_value,
2689 (guint64) wavparse->bps, GST_SECOND);
2691 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2692 wavparse->rate, wavparse->fact);
2694 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2696 /* make sure we end up on a sample boundary */
2697 *dest_value -= *dest_value % wavparse->blockalign;
2699 case GST_FORMAT_DEFAULT:
2700 *dest_value = gst_util_uint64_scale (src_value,
2701 (guint64) wavparse->rate, GST_SECOND);
2720 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2726 /* handle queries for location and length in requested format */
2728 gst_wavparse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
2730 gboolean res = TRUE;
2731 GstWavParse *wav = GST_WAVPARSE (parent);
2733 /* only if we know */
2734 if (wav->state != GST_WAVPARSE_DATA) {
2738 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2740 switch (GST_QUERY_TYPE (query)) {
2741 case GST_QUERY_POSITION:
2747 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2748 curb = wav->offset - wav->datastart;
2749 gst_query_parse_position (query, &format, NULL);
2750 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2753 case GST_FORMAT_BYTES:
2754 format = GST_FORMAT_BYTES;
2758 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2763 gst_query_set_position (query, format, cur);
2766 case GST_QUERY_DURATION:
2768 gint64 duration = 0;
2771 if (wav->ignore_length) {
2776 gst_query_parse_duration (query, &format, NULL);
2779 case GST_FORMAT_BYTES:{
2780 format = GST_FORMAT_BYTES;
2781 duration = wav->datasize;
2784 case GST_FORMAT_TIME:
2785 if ((res = gst_wavparse_calculate_duration (wav))) {
2786 duration = wav->duration;
2794 gst_query_set_duration (query, format, duration);
2797 case GST_QUERY_CONVERT:
2799 gint64 srcvalue, dstvalue;
2800 GstFormat srcformat, dstformat;
2802 gst_query_parse_convert (query, &srcformat, &srcvalue,
2803 &dstformat, &dstvalue);
2804 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2805 &dstformat, &dstvalue);
2807 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2810 case GST_QUERY_SEEKING:{
2812 gboolean seekable = FALSE;
2814 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2815 if (fmt == wav->segment.format) {
2816 if (wav->streaming) {
2819 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2820 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2821 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2822 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2824 gst_query_unref (q);
2826 GST_LOG_OBJECT (wav, "looping => seekable");
2830 } else if (fmt == GST_FORMAT_TIME) {
2834 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2839 res = gst_pad_query_default (pad, parent, query);
2846 gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
2848 GstWavParse *wavparse = GST_WAVPARSE (parent);
2849 gboolean res = FALSE;
2851 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2853 switch (GST_EVENT_TYPE (event)) {
2854 case GST_EVENT_SEEK:
2855 /* can only handle events when we are in the data state */
2856 if (wavparse->state == GST_WAVPARSE_DATA) {
2857 res = gst_wavparse_perform_seek (wavparse, event);
2859 gst_event_unref (event);
2862 case GST_EVENT_TOC_SELECT:
2865 GstTocEntry *entry = NULL;
2866 GstEvent *seek_event;
2869 if (!wavparse->toc) {
2870 GST_DEBUG_OBJECT (wavparse, "no TOC to select");
2873 gst_event_parse_toc_select (event, &uid);
2875 GST_OBJECT_LOCK (wavparse);
2876 entry = gst_toc_find_entry (wavparse->toc, uid);
2877 if (entry == NULL) {
2878 GST_OBJECT_UNLOCK (wavparse);
2879 GST_WARNING_OBJECT (wavparse, "no TOC entry with given UID: %s",
2883 gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL);
2884 GST_OBJECT_UNLOCK (wavparse);
2885 seek_event = gst_event_new_seek (1.0,
2887 GST_SEEK_FLAG_FLUSH,
2888 GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_SET, -1);
2889 res = gst_wavparse_perform_seek (wavparse, seek_event);
2890 gst_event_unref (seek_event);
2894 GST_WARNING_OBJECT (wavparse, "received empty TOC select event");
2898 gst_event_unref (event);
2903 res = gst_pad_push_event (wavparse->sinkpad, event);
2910 gst_wavparse_sink_activate (GstPad * sinkpad, GstObject * parent)
2912 GstWavParse *wav = GST_WAVPARSE (parent);
2917 gst_adapter_clear (wav->adapter);
2918 g_object_unref (wav->adapter);
2919 wav->adapter = NULL;
2922 query = gst_query_new_scheduling ();
2924 if (!gst_pad_peer_query (sinkpad, query)) {
2925 gst_query_unref (query);
2929 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
2930 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
2931 gst_query_unref (query);
2936 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2937 wav->streaming = FALSE;
2938 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
2942 GST_DEBUG_OBJECT (sinkpad, "activating push");
2943 wav->streaming = TRUE;
2944 wav->adapter = gst_adapter_new ();
2945 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
2951 gst_wavparse_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
2952 GstPadMode mode, gboolean active)
2957 case GST_PAD_MODE_PUSH:
2960 case GST_PAD_MODE_PULL:
2962 /* if we have a scheduler we can start the task */
2963 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2966 res = gst_pad_stop_task (sinkpad);
2976 static GstStateChangeReturn
2977 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2979 GstStateChangeReturn ret;
2980 GstWavParse *wav = GST_WAVPARSE (element);
2982 switch (transition) {
2983 case GST_STATE_CHANGE_NULL_TO_READY:
2985 case GST_STATE_CHANGE_READY_TO_PAUSED:
2986 gst_wavparse_reset (wav);
2988 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2994 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2996 switch (transition) {
2997 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2999 case GST_STATE_CHANGE_PAUSED_TO_READY:
3000 gst_wavparse_reset (wav);
3002 case GST_STATE_CHANGE_READY_TO_NULL:
3011 gst_wavparse_set_property (GObject * object, guint prop_id,
3012 const GValue * value, GParamSpec * pspec)
3016 g_return_if_fail (GST_IS_WAVPARSE (object));
3017 self = GST_WAVPARSE (object);
3020 case PROP_IGNORE_LENGTH:
3021 self->ignore_length = g_value_get_boolean (value);
3024 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
3030 gst_wavparse_get_property (GObject * object, guint prop_id,
3031 GValue * value, GParamSpec * pspec)
3035 g_return_if_fail (GST_IS_WAVPARSE (object));
3036 self = GST_WAVPARSE (object);
3039 case PROP_IGNORE_LENGTH:
3040 g_value_set_boolean (value, self->ignore_length);
3043 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
3048 plugin_init (GstPlugin * plugin)
3052 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
3056 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
3059 "Parse a .wav file into raw audio",
3060 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)