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-media.h"
58 #include <gst/base/gsttypefindhelper.h>
59 #include <gst/gst-i18n-plugin.h>
61 GST_DEBUG_CATEGORY_STATIC (wavparse_debug);
62 #define GST_CAT_DEFAULT (wavparse_debug)
64 static void gst_wavparse_dispose (GObject * object);
66 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad,
68 static gboolean gst_wavparse_sink_activate_mode (GstPad * sinkpad,
69 GstObject * parent, GstPadMode mode, gboolean active);
70 static gboolean gst_wavparse_send_event (GstElement * element,
72 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
73 GstStateChange transition);
75 static gboolean gst_wavparse_pad_query (GstPad * pad, GstObject * parent,
77 static gboolean gst_wavparse_pad_convert (GstPad * pad, GstFormat src_format,
78 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
80 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstObject * parent,
82 static gboolean gst_wavparse_sink_event (GstPad * pad, GstObject * parent,
84 static void gst_wavparse_loop (GstPad * pad);
85 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent,
88 static void gst_wavparse_set_property (GObject * object, guint prop_id,
89 const GValue * value, GParamSpec * pspec);
90 static void gst_wavparse_get_property (GObject * object, guint prop_id,
91 GValue * value, GParamSpec * pspec);
93 #define DEFAULT_IGNORE_LENGTH FALSE
101 static GstStaticPadTemplate sink_template_factory =
102 GST_STATIC_PAD_TEMPLATE ("sink",
105 GST_STATIC_CAPS ("audio/x-wav")
109 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
111 #define gst_wavparse_parent_class parent_class
112 G_DEFINE_TYPE_WITH_CODE (GstWavParse, gst_wavparse, GST_TYPE_ELEMENT,
117 /* Offset Size Description Value
118 * 0x00 4 ID unique identification value
119 * 0x04 4 Position play order position
120 * 0x08 4 Data Chunk ID RIFF ID of corresponding data chunk
121 * 0x0c 4 Chunk Start Byte Offset of Data Chunk *
122 * 0x10 4 Block Start Byte Offset to sample of First Channel
123 * 0x14 4 Sample Offset Byte Offset to sample byte of First Channel
127 guint32 data_chunk_id;
130 guint32 sample_offset;
135 /* Offset Size Description Value
136 * 0x08 4 Cue Point ID 0 - 0xFFFFFFFF
139 guint32 cue_point_id;
141 } GstWavParseLabl, GstWavParseNote;
144 gst_wavparse_class_init (GstWavParseClass * klass)
146 GstElementClass *gstelement_class;
147 GObjectClass *object_class;
148 GstPadTemplate *src_template;
150 gstelement_class = (GstElementClass *) klass;
151 object_class = (GObjectClass *) klass;
153 parent_class = g_type_class_peek_parent (klass);
155 object_class->dispose = gst_wavparse_dispose;
157 object_class->set_property = gst_wavparse_set_property;
158 object_class->get_property = gst_wavparse_get_property;
161 * GstWavParse:ignore-length:
163 * This selects whether the length found in a data chunk
164 * should be ignored. This may be useful for streamed audio
165 * where the length is unknown until the end of streaming,
166 * and various software/hardware just puts some random value
167 * in there and hopes it doesn't break too much.
169 g_object_class_install_property (object_class, PROP_IGNORE_LENGTH,
170 g_param_spec_boolean ("ignore-length",
172 "Ignore length from the Wave header",
173 DEFAULT_IGNORE_LENGTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
176 gstelement_class->change_state = gst_wavparse_change_state;
177 gstelement_class->send_event = gst_wavparse_send_event;
180 gst_element_class_add_pad_template (gstelement_class,
181 gst_static_pad_template_get (&sink_template_factory));
183 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
184 GST_PAD_ALWAYS, gst_riff_create_audio_template_caps ());
185 gst_element_class_add_pad_template (gstelement_class, src_template);
187 gst_element_class_set_static_metadata (gstelement_class, "WAV audio demuxer",
188 "Codec/Demuxer/Audio",
189 "Parse a .wav file into raw audio",
190 "Erik Walthinsen <omega@cse.ogi.edu>");
194 gst_wavparse_reset (GstWavParse * wav)
196 wav->state = GST_WAVPARSE_START;
198 /* These will all be set correctly in the fmt chunk */
212 wav->got_fmt = FALSE;
216 gst_event_unref (wav->seek_event);
217 wav->seek_event = NULL;
219 gst_adapter_clear (wav->adapter);
220 g_object_unref (wav->adapter);
224 gst_tag_list_unref (wav->tags);
227 gst_toc_unref (wav->toc);
230 g_list_free_full (wav->cues, g_free);
233 g_list_free_full (wav->labls, g_free);
236 gst_caps_unref (wav->caps);
238 if (wav->start_segment)
239 gst_event_unref (wav->start_segment);
240 wav->start_segment = NULL;
244 gst_wavparse_dispose (GObject * object)
246 GstWavParse *wav = GST_WAVPARSE (object);
248 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
249 gst_wavparse_reset (wav);
251 G_OBJECT_CLASS (parent_class)->dispose (object);
255 gst_wavparse_init (GstWavParse * wavparse)
257 gst_wavparse_reset (wavparse);
261 gst_pad_new_from_static_template (&sink_template_factory, "sink");
262 gst_pad_set_activate_function (wavparse->sinkpad,
263 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
264 gst_pad_set_activatemode_function (wavparse->sinkpad,
265 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_mode));
266 gst_pad_set_chain_function (wavparse->sinkpad,
267 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
268 gst_pad_set_event_function (wavparse->sinkpad,
269 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
270 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
274 gst_pad_new_from_template (gst_element_class_get_pad_template
275 (GST_ELEMENT_GET_CLASS (wavparse), "src"), "src");
276 gst_pad_use_fixed_caps (wavparse->srcpad);
277 gst_pad_set_query_function (wavparse->srcpad,
278 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
279 gst_pad_set_event_function (wavparse->srcpad,
280 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
281 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
284 /* FIXME: why is that not in use? */
287 gst_wavparse_parse_adtl (GstWavParse * wavparse, int len)
290 GstByteStream *bs = wavparse->bs;
291 gst_riff_chunk *temp_chunk, chunk;
293 struct _gst_riff_labl labl, *temp_labl;
294 struct _gst_riff_ltxt ltxt, *temp_ltxt;
295 struct _gst_riff_note note, *temp_note;
298 GstPropsEntry *entry;
302 props = wavparse->metadata->properties;
306 gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
307 if (got_bytes != sizeof (gst_riff_chunk)) {
310 temp_chunk = (gst_riff_chunk *) tempdata;
312 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
313 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
315 if (chunk.size == 0) {
316 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
317 len -= sizeof (gst_riff_chunk);
322 case GST_RIFF_adtl_labl:
324 gst_bytestream_peek_bytes (bs, &tempdata,
325 sizeof (struct _gst_riff_labl));
326 if (got_bytes != sizeof (struct _gst_riff_labl)) {
330 temp_labl = (struct _gst_riff_labl *) tempdata;
331 labl.id = GUINT32_FROM_LE (temp_labl->id);
332 labl.size = GUINT32_FROM_LE (temp_labl->size);
333 labl.identifier = GUINT32_FROM_LE (temp_labl->identifier);
335 gst_bytestream_flush (bs, sizeof (struct _gst_riff_labl));
336 len -= sizeof (struct _gst_riff_labl);
338 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, labl.size - 4);
339 if (got_bytes != labl.size - 4) {
343 label_name = (char *) tempdata;
345 gst_bytestream_flush (bs, ((labl.size - 4) + 1) & ~1);
346 len -= (((labl.size - 4) + 1) & ~1);
348 new_caps = gst_caps_new ("label",
349 "application/x-gst-metadata",
350 gst_props_new ("identifier", G_TYPE_INT (labl.identifier),
351 "name", G_TYPE_STRING (label_name), NULL));
353 if (gst_props_get (props, "labels", &caps, NULL)) {
354 caps = g_list_append (caps, new_caps);
356 caps = g_list_append (NULL, new_caps);
358 entry = gst_props_entry_new ("labels", GST_PROPS_GLIST (caps));
359 gst_props_add_entry (props, entry);
364 case GST_RIFF_adtl_ltxt:
366 gst_bytestream_peek_bytes (bs, &tempdata,
367 sizeof (struct _gst_riff_ltxt));
368 if (got_bytes != sizeof (struct _gst_riff_ltxt)) {
372 temp_ltxt = (struct _gst_riff_ltxt *) tempdata;
373 ltxt.id = GUINT32_FROM_LE (temp_ltxt->id);
374 ltxt.size = GUINT32_FROM_LE (temp_ltxt->size);
375 ltxt.identifier = GUINT32_FROM_LE (temp_ltxt->identifier);
376 ltxt.length = GUINT32_FROM_LE (temp_ltxt->length);
377 ltxt.purpose = GUINT32_FROM_LE (temp_ltxt->purpose);
378 ltxt.country = GUINT16_FROM_LE (temp_ltxt->country);
379 ltxt.language = GUINT16_FROM_LE (temp_ltxt->language);
380 ltxt.dialect = GUINT16_FROM_LE (temp_ltxt->dialect);
381 ltxt.codepage = GUINT16_FROM_LE (temp_ltxt->codepage);
383 gst_bytestream_flush (bs, sizeof (struct _gst_riff_ltxt));
384 len -= sizeof (struct _gst_riff_ltxt);
386 if (ltxt.size - 20 > 0) {
387 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, ltxt.size - 20);
388 if (got_bytes != ltxt.size - 20) {
392 gst_bytestream_flush (bs, ((ltxt.size - 20) + 1) & ~1);
393 len -= (((ltxt.size - 20) + 1) & ~1);
395 label_name = (char *) tempdata;
400 new_caps = gst_caps_new ("ltxt",
401 "application/x-gst-metadata",
402 gst_props_new ("identifier", G_TYPE_INT (ltxt.identifier),
403 "name", G_TYPE_STRING (label_name),
404 "length", G_TYPE_INT (ltxt.length), NULL));
406 if (gst_props_get (props, "ltxts", &caps, NULL)) {
407 caps = g_list_append (caps, new_caps);
409 caps = g_list_append (NULL, new_caps);
411 entry = gst_props_entry_new ("ltxts", GST_PROPS_GLIST (caps));
412 gst_props_add_entry (props, entry);
417 case GST_RIFF_adtl_note:
419 gst_bytestream_peek_bytes (bs, &tempdata,
420 sizeof (struct _gst_riff_note));
421 if (got_bytes != sizeof (struct _gst_riff_note)) {
425 temp_note = (struct _gst_riff_note *) tempdata;
426 note.id = GUINT32_FROM_LE (temp_note->id);
427 note.size = GUINT32_FROM_LE (temp_note->size);
428 note.identifier = GUINT32_FROM_LE (temp_note->identifier);
430 gst_bytestream_flush (bs, sizeof (struct _gst_riff_note));
431 len -= sizeof (struct _gst_riff_note);
433 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, note.size - 4);
434 if (got_bytes != note.size - 4) {
438 gst_bytestream_flush (bs, ((note.size - 4) + 1) & ~1);
439 len -= (((note.size - 4) + 1) & ~1);
441 label_name = (char *) tempdata;
443 new_caps = gst_caps_new ("note",
444 "application/x-gst-metadata",
445 gst_props_new ("identifier", G_TYPE_INT (note.identifier),
446 "name", G_TYPE_STRING (label_name), NULL));
448 if (gst_props_get (props, "notes", &caps, NULL)) {
449 caps = g_list_append (caps, new_caps);
451 caps = g_list_append (NULL, new_caps);
453 entry = gst_props_entry_new ("notes", GST_PROPS_GLIST (caps));
454 gst_props_add_entry (props, entry);
460 g_print ("Unknown chunk: %" GST_FOURCC_FORMAT "\n",
461 GST_FOURCC_ARGS (chunk.id));
466 g_object_notify (G_OBJECT (wavparse), "metadata");
470 gst_wavparse_parse_cues (GstWavParse * wavparse, int len)
473 GstByteStream *bs = wavparse->bs;
474 struct _gst_riff_cue *temp_cue, cue;
475 struct _gst_riff_cuepoints *points;
479 GstPropsEntry *entry;
485 gst_bytestream_peek_bytes (bs, &tempdata,
486 sizeof (struct _gst_riff_cue));
487 temp_cue = (struct _gst_riff_cue *) tempdata;
489 /* fixup for our big endian friends */
490 cue.id = GUINT32_FROM_LE (temp_cue->id);
491 cue.size = GUINT32_FROM_LE (temp_cue->size);
492 cue.cuepoints = GUINT32_FROM_LE (temp_cue->cuepoints);
494 gst_bytestream_flush (bs, sizeof (struct _gst_riff_cue));
495 if (got_bytes != sizeof (struct _gst_riff_cue)) {
499 len -= sizeof (struct _gst_riff_cue);
501 /* -4 because cue.size contains the cuepoints size
502 and we've already flushed that out of the system */
503 required = cue.size - 4;
504 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, required);
505 gst_bytestream_flush (bs, ((required) + 1) & ~1);
506 if (got_bytes != required) {
510 len -= (((cue.size - 4) + 1) & ~1);
512 /* now we have an array of struct _gst_riff_cuepoints in tempdata */
513 points = (struct _gst_riff_cuepoints *) tempdata;
515 for (i = 0; i < cue.cuepoints; i++) {
518 caps = gst_caps_new ("cues",
519 "application/x-gst-metadata",
520 gst_props_new ("identifier", G_TYPE_INT (points[i].identifier),
521 "position", G_TYPE_INT (points[i].offset), NULL));
522 cues = g_list_append (cues, caps);
525 entry = gst_props_entry_new ("cues", GST_PROPS_GLIST (cues));
526 gst_props_add_entry (wavparse->metadata->properties, entry);
529 g_object_notify (G_OBJECT (wavparse), "metadata");
532 /* Read 'fmt ' header */
534 gst_wavparse_fmt (GstWavParse * wav)
536 gst_riff_strf_auds *header = NULL;
539 if (!gst_riff_read_strf_auds (wav, &header))
542 wav->format = header->format;
543 wav->rate = header->rate;
544 wav->channels = header->channels;
545 if (wav->channels == 0)
548 wav->blockalign = header->blockalign;
549 wav->width = (header->blockalign * 8) / header->channels;
550 wav->depth = header->size;
551 wav->bps = header->av_bps;
555 /* Note: gst_riff_create_audio_caps might need to fix values in
556 * the header header depending on the format, so call it first */
557 /* FIXME: Need to handle the channel reorder map */
558 caps = gst_riff_create_audio_caps (header->format, NULL, header, NULL, NULL);
564 gst_wavparse_create_sourcepad (wav);
565 gst_pad_use_fixed_caps (wav->srcpad);
566 gst_pad_set_active (wav->srcpad, TRUE);
567 gst_pad_set_caps (wav->srcpad, caps);
568 gst_caps_free (caps);
569 gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
570 gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
572 GST_DEBUG ("frequency %u, channels %u", wav->rate, wav->channels);
579 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
580 ("No FMT tag found"));
585 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
586 ("Stream claims to contain zero channels - invalid data"));
592 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
593 ("Stream claims to bitrate of <= zero - invalid data"));
599 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
605 gst_wavparse_other (GstWavParse * wav)
609 if (!gst_riff_peek_head (wav, &tag, &length, NULL)) {
610 GST_WARNING_OBJECT (wav, "could not peek head");
613 GST_DEBUG_OBJECT (wav, "got tag (%08x) %4.4s, length %u", tag,
614 (const gchar *) &tag, length);
617 case GST_RIFF_TAG_LIST:
618 if (!(tag = gst_riff_peek_list (wav))) {
619 GST_WARNING_OBJECT (wav, "could not peek list");
624 case GST_RIFF_LIST_INFO:
625 if (!gst_riff_read_list (wav, &tag) || !gst_riff_read_info (wav)) {
626 GST_WARNING_OBJECT (wav, "could not read list");
631 case GST_RIFF_LIST_adtl:
632 if (!gst_riff_read_skip (wav)) {
633 GST_WARNING_OBJECT (wav, "could not read skip");
639 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag,
641 if (!gst_riff_read_skip (wav)) {
642 GST_WARNING_OBJECT (wav, "could not read skip");
650 case GST_RIFF_TAG_data:
651 if (!gst_bytestream_flush (wav->bs, 8)) {
652 GST_WARNING_OBJECT (wav, "could not flush 8 bytes");
656 GST_DEBUG_OBJECT (wav, "switching to data mode");
657 wav->state = GST_WAVPARSE_DATA;
658 wav->datastart = gst_bytestream_tell (wav->bs);
662 /* length is 0, data probably stretches to the end
664 GST_DEBUG_OBJECT (wav, "length is 0 trying to find length");
665 /* get length of file */
666 file_length = gst_bytestream_length (wav->bs);
667 if (file_length == -1) {
668 GST_DEBUG_OBJECT (wav,
669 "could not get file length, assuming data to eof");
670 /* could not get length, assuming till eof */
671 length = G_MAXUINT32;
673 if (file_length > G_MAXUINT32) {
674 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
675 ", clipping to 32 bits", file_length);
676 /* could not get length, assuming till eof */
677 length = G_MAXUINT32;
679 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
680 ", datalength %u", file_length, length);
681 /* substract offset of datastart from length */
682 length = file_length - wav->datastart;
683 GST_DEBUG_OBJECT (wav, "datalength %u", length);
686 wav->datasize = (guint64) length;
687 GST_DEBUG_OBJECT (wav, "datasize = %ld", length)
690 case GST_RIFF_TAG_cue:
691 if (!gst_riff_read_skip (wav)) {
692 GST_WARNING_OBJECT (wav, "could not read skip");
698 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag, (gchar *) & tag);
699 if (!gst_riff_read_skip (wav))
710 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
714 if (!gst_riff_parse_file_header (element, buf, &doctype))
717 if (doctype != GST_RIFF_RIFF_WAVE)
725 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
726 ("File is not a WAVE file: %" GST_FOURCC_FORMAT,
727 GST_FOURCC_ARGS (doctype)));
733 gst_wavparse_stream_init (GstWavParse * wav)
736 GstBuffer *buf = NULL;
738 if ((res = gst_pad_pull_range (wav->sinkpad,
739 wav->offset, 12, &buf)) != GST_FLOW_OK)
741 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
742 return GST_FLOW_ERROR;
750 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
752 /* -1 always maps to -1 */
758 /* 0 always maps to 0 */
765 *bytepos = gst_util_uint64_scale_ceil (ts, (guint64) wav->bps, GST_SECOND);
767 } else if (wav->fact) {
769 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
770 *bytepos = gst_util_uint64_scale_ceil (ts, bps, GST_SECOND);
777 /* This function is used to perform seeks on the element.
779 * It also works when event is NULL, in which case it will just
780 * start from the last configured segment. This technique is
781 * used when activating the element and to perform the seek in
785 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
789 GstFormat format, bformat;
791 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
792 gint64 cur, stop, upstream_size;
795 GstSegment seeksegment = { 0, };
799 GST_DEBUG_OBJECT (wav, "doing seek with event");
801 gst_event_parse_seek (event, &rate, &format, &flags,
802 &cur_type, &cur, &stop_type, &stop);
804 /* no negative rates yet */
808 if (format != wav->segment.format) {
809 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
810 gst_format_get_name (format),
811 gst_format_get_name (wav->segment.format));
813 if (cur_type != GST_SEEK_TYPE_NONE)
815 gst_pad_query_convert (wav->srcpad, format, cur,
816 wav->segment.format, &cur);
817 if (res && stop_type != GST_SEEK_TYPE_NONE)
819 gst_pad_query_convert (wav->srcpad, format, stop,
820 wav->segment.format, &stop);
824 format = wav->segment.format;
827 GST_DEBUG_OBJECT (wav, "doing seek without event");
830 cur_type = GST_SEEK_TYPE_SET;
831 stop_type = GST_SEEK_TYPE_SET;
834 /* in push mode, we must delegate to upstream */
835 if (wav->streaming) {
836 gboolean res = FALSE;
838 /* if streaming not yet started; only prepare initial newsegment */
839 if (!event || wav->state != GST_WAVPARSE_DATA) {
840 if (wav->start_segment)
841 gst_event_unref (wav->start_segment);
842 wav->start_segment = gst_event_new_segment (&wav->segment);
845 /* convert seek positions to byte positions in data sections */
846 if (format == GST_FORMAT_TIME) {
847 /* should not fail */
848 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
850 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
853 /* mind sample boundary and header */
855 cur -= (cur % wav->bytes_per_sample);
856 cur += wav->datastart;
859 stop -= (stop % wav->bytes_per_sample);
860 stop += wav->datastart;
862 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
863 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
865 /* BYTE seek event */
866 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
868 res = gst_pad_push_event (wav->sinkpad, event);
874 flush = flags & GST_SEEK_FLAG_FLUSH;
876 /* now we need to make sure the streaming thread is stopped. We do this by
877 * either sending a FLUSH_START event downstream which will cause the
878 * streaming thread to stop with a WRONG_STATE.
879 * For a non-flushing seek we simply pause the task, which will happen as soon
880 * as it completes one iteration (and thus might block when the sink is
881 * blocking in preroll). */
883 GST_DEBUG_OBJECT (wav, "sending flush start");
884 gst_pad_push_event (wav->srcpad, gst_event_new_flush_start ());
886 gst_pad_pause_task (wav->sinkpad);
889 /* we should now be able to grab the streaming thread because we stopped it
890 * with the above flush/pause code */
891 GST_PAD_STREAM_LOCK (wav->sinkpad);
893 /* save current position */
894 last_stop = wav->segment.position;
896 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
898 /* copy segment, we need this because we still need the old
899 * segment when we close the current segment. */
900 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
902 /* configure the seek parameters in the seeksegment. We will then have the
903 * right values in the segment to perform the seek */
905 GST_DEBUG_OBJECT (wav, "configuring seek");
906 gst_segment_do_seek (&seeksegment, rate, format, flags,
907 cur_type, cur, stop_type, stop, &update);
910 /* figure out the last position we need to play. If it's configured (stop !=
911 * -1), use that, else we play until the total duration of the file */
912 if ((stop = seeksegment.stop) == -1)
913 stop = seeksegment.duration;
915 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
916 if ((cur_type != GST_SEEK_TYPE_NONE)) {
917 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
918 * we can just copy the last_stop. If not, we use the bps to convert TIME to
920 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
921 (gint64 *) & wav->offset))
922 wav->offset = seeksegment.position;
923 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
924 wav->offset -= (wav->offset % wav->bytes_per_sample);
925 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
926 wav->offset += wav->datastart;
927 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
929 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
933 if (stop_type != GST_SEEK_TYPE_NONE) {
934 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
935 wav->end_offset = stop;
936 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
937 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
938 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
939 wav->end_offset += wav->datastart;
940 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
942 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
946 /* make sure filesize is not exceeded due to rounding errors or so,
947 * same precaution as in _stream_headers */
948 bformat = GST_FORMAT_BYTES;
949 if (gst_pad_peer_query_duration (wav->sinkpad, bformat, &upstream_size))
950 wav->end_offset = MIN (wav->end_offset, upstream_size);
952 /* this is the range of bytes we will use for playback */
953 wav->offset = MIN (wav->offset, wav->end_offset);
954 wav->dataleft = wav->end_offset - wav->offset;
956 GST_DEBUG_OBJECT (wav,
957 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
958 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
959 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
961 /* prepare for streaming again */
963 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
964 GST_DEBUG_OBJECT (wav, "sending flush stop");
965 gst_pad_push_event (wav->srcpad, gst_event_new_flush_stop (TRUE));
968 /* now we did the seek and can activate the new segment values */
969 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
971 /* if we're doing a segment seek, post a SEGMENT_START message */
972 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
973 gst_element_post_message (GST_ELEMENT_CAST (wav),
974 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
975 wav->segment.format, wav->segment.position));
978 /* now create the newsegment */
979 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
980 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
982 /* store the newsegment event so it can be sent from the streaming thread. */
983 if (wav->start_segment)
984 gst_event_unref (wav->start_segment);
985 wav->start_segment = gst_event_new_segment (&wav->segment);
987 /* mark discont if we are going to stream from another position. */
988 if (last_stop != wav->segment.position) {
989 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
993 /* and start the streaming task again */
994 if (!wav->streaming) {
995 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
999 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
1006 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
1011 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
1016 GST_DEBUG_OBJECT (wav,
1017 "Could not determine byte position for desired time");
1023 * gst_wavparse_peek_chunk_info:
1024 * @wav Wavparse object
1025 * @tag holder for tag
1026 * @size holder for tag size
1028 * Peek next chunk info (tag and size)
1030 * Returns: %TRUE when the chunk info (header) is available
1033 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
1035 const guint8 *data = NULL;
1037 if (gst_adapter_available (wav->adapter) < 8)
1040 data = gst_adapter_map (wav->adapter, 8);
1041 *tag = GST_READ_UINT32_LE (data);
1042 *size = GST_READ_UINT32_LE (data + 4);
1043 gst_adapter_unmap (wav->adapter);
1045 GST_DEBUG ("Next chunk size is %u bytes, type %" GST_FOURCC_FORMAT, *size,
1046 GST_FOURCC_ARGS (*tag));
1052 * gst_wavparse_peek_chunk:
1053 * @wav Wavparse object
1054 * @tag holder for tag
1055 * @size holder for tag size
1057 * Peek enough data for one full chunk
1059 * Returns: %TRUE when the full chunk is available
1062 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
1064 guint32 peek_size = 0;
1067 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
1070 /* size 0 -> empty data buffer would surprise most callers,
1071 * large size -> do not bother trying to squeeze that into adapter,
1072 * so we throw poor man's exception, which can be caught if caller really
1073 * wants to handle 0 size chunk */
1074 if (!(*size) || (*size) >= (1 << 30)) {
1075 GST_INFO ("Invalid/unexpected chunk size %u for tag %" GST_FOURCC_FORMAT,
1076 *size, GST_FOURCC_ARGS (*tag));
1077 /* chain should give up */
1078 wav->abort_buffering = TRUE;
1081 peek_size = (*size + 1) & ~1;
1082 available = gst_adapter_available (wav->adapter);
1084 if (available >= (8 + peek_size)) {
1087 GST_LOG ("but only %u bytes available now", available);
1093 * gst_wavparse_calculate_duration:
1094 * @wav: wavparse object
1096 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
1099 * Returns: %TRUE if duration is available.
1102 gst_wavparse_calculate_duration (GstWavParse * wav)
1104 if (wav->duration > 0)
1108 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
1110 gst_util_uint64_scale_ceil (wav->datasize, GST_SECOND,
1111 (guint64) wav->bps);
1112 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
1113 GST_TIME_ARGS (wav->duration));
1115 } else if (wav->fact) {
1117 gst_util_uint64_scale_int_ceil (GST_SECOND, wav->fact, wav->rate);
1118 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
1119 GST_TIME_ARGS (wav->duration));
1126 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
1131 if (wav->streaming) {
1132 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1135 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
1136 GST_FOURCC_ARGS (tag));
1137 flush = 8 + ((size + 1) & ~1);
1138 wav->offset += flush;
1139 if (wav->streaming) {
1140 gst_adapter_flush (wav->adapter, flush);
1142 gst_buffer_unref (buf);
1149 * gst_wavparse_cue_chunk:
1150 * @wav GstWavParse object
1151 * @data holder for data
1152 * @size holder for data size
1154 * Parse cue chunk from @data to wav->cues.
1156 * Returns: %TRUE when cue chunk is available
1159 gst_wavparse_cue_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
1163 GstWavParseCue *cue;
1166 GST_WARNING_OBJECT (wav, "found another cue's");
1170 ncues = GST_READ_UINT32_LE (data);
1172 if (size < 4 + ncues * 24) {
1173 GST_WARNING_OBJECT (wav, "broken file %d %d", size, ncues);
1179 for (i = 0; i < ncues; i++) {
1180 cue = g_new0 (GstWavParseCue, 1);
1181 cue->id = GST_READ_UINT32_LE (data);
1182 cue->position = GST_READ_UINT32_LE (data + 4);
1183 cue->data_chunk_id = GST_READ_UINT32_LE (data + 8);
1184 cue->chunk_start = GST_READ_UINT32_LE (data + 12);
1185 cue->block_start = GST_READ_UINT32_LE (data + 16);
1186 cue->sample_offset = GST_READ_UINT32_LE (data + 20);
1187 cues = g_list_append (cues, cue);
1197 * gst_wavparse_labl_chunk:
1198 * @wav GstWavParse object
1199 * @data holder for data
1200 * @size holder for data size
1202 * Parse labl from @data to wav->labls.
1204 * Returns: %TRUE when labl chunk is available
1207 gst_wavparse_labl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
1209 GstWavParseLabl *labl;
1214 labl = g_new0 (GstWavParseLabl, 1);
1218 labl->cue_point_id = GST_READ_UINT32_LE (data);
1219 labl->text = g_memdup (data + 4, size - 4);
1221 wav->labls = g_list_append (wav->labls, labl);
1227 * gst_wavparse_note_chunk:
1228 * @wav GstWavParse object
1229 * @data holder for data
1230 * @size holder for data size
1232 * Parse note from @data to wav->notes.
1234 * Returns: %TRUE when note chunk is available
1237 gst_wavparse_note_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
1239 GstWavParseNote *note;
1244 note = g_new0 (GstWavParseNote, 1);
1248 note->cue_point_id = GST_READ_UINT32_LE (data);
1249 note->text = g_memdup (data + 4, size - 4);
1251 wav->notes = g_list_append (wav->notes, note);
1257 * gst_wavparse_adtl_chunk:
1258 * @wav GstWavParse object
1259 * @data holder for data
1260 * @size holder for data size
1262 * Parse adtl from @data.
1264 * Returns: %TRUE when adtl chunk is available
1267 gst_wavparse_adtl_chunk (GstWavParse * wav, const guint8 * data, guint32 size)
1269 guint32 ltag, lsize, offset = 0;
1272 ltag = GST_READ_UINT32_LE (data + offset);
1273 lsize = GST_READ_UINT32_LE (data + offset + 4);
1275 case GST_RIFF_TAG_labl:
1276 gst_wavparse_labl_chunk (wav, data + offset, size);
1278 case GST_RIFF_TAG_note:
1279 gst_wavparse_note_chunk (wav, data + offset, size);
1284 offset += 8 + GST_ROUND_UP_2 (lsize);
1285 size -= 8 + GST_ROUND_UP_2 (lsize);
1292 gst_wavparse_get_tags_toc_entry (GstToc * toc, gchar * id)
1294 GstTagList *tags = NULL;
1295 GstTocEntry *entry = NULL;
1297 entry = gst_toc_find_entry (toc, id);
1298 if (entry != NULL) {
1299 tags = gst_toc_entry_get_tags (entry);
1301 tags = gst_tag_list_new_empty ();
1302 gst_toc_entry_set_tags (entry, tags);
1310 * gst_wavparse_create_toc:
1311 * @wav GstWavParse object
1313 * Create TOC from wav->cues and wav->labls.
1316 gst_wavparse_create_toc (GstWavParse * wav)
1321 GstWavParseCue *cue;
1322 GstWavParseLabl *labl;
1323 GstWavParseNote *note;
1326 GstTocEntry *entry = NULL, *cur_subentry = NULL, *prev_subentry = NULL;
1328 GST_OBJECT_LOCK (wav);
1330 GST_OBJECT_UNLOCK (wav);
1331 GST_WARNING_OBJECT (wav, "found another TOC");
1336 GST_OBJECT_UNLOCK (wav);
1340 /* FIXME: send CURRENT scope toc too */
1341 toc = gst_toc_new (GST_TOC_SCOPE_GLOBAL);
1343 /* add cue edition */
1344 entry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_EDITION, "cue");
1345 gst_toc_entry_set_start_stop_times (entry, 0, wav->duration);
1346 gst_toc_append_entry (toc, entry);
1348 /* add tracks in cue edition */
1352 prev_subentry = cur_subentry;
1353 /* previous track stop time = current track start time */
1354 if (prev_subentry != NULL) {
1355 gst_toc_entry_get_start_stop_times (prev_subentry, &start, NULL);
1356 stop = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
1357 gst_toc_entry_set_start_stop_times (prev_subentry, start, stop);
1359 id = g_strdup_printf ("%08x", cue->id);
1360 cur_subentry = gst_toc_entry_new (GST_TOC_ENTRY_TYPE_TRACK, id);
1362 start = gst_util_uint64_scale_round (cue->position, GST_SECOND, wav->rate);
1363 stop = wav->duration;
1364 gst_toc_entry_set_start_stop_times (cur_subentry, start, stop);
1365 gst_toc_entry_append_sub_entry (entry, cur_subentry);
1366 list = g_list_next (list);
1369 /* add tags in tracks */
1373 id = g_strdup_printf ("%08x", labl->cue_point_id);
1374 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1377 gst_tag_list_add (tags, GST_TAG_MERGE_APPEND, GST_TAG_TITLE, labl->text,
1380 list = g_list_next (list);
1385 id = g_strdup_printf ("%08x", note->cue_point_id);
1386 tags = gst_wavparse_get_tags_toc_entry (toc, id);
1389 gst_tag_list_add (tags, GST_TAG_MERGE_PREPEND, GST_TAG_COMMENT,
1392 list = g_list_next (list);
1395 /* send data as TOC */
1398 /* send TOC event */
1400 GST_OBJECT_UNLOCK (wav);
1401 gst_pad_push_event (wav->srcpad, gst_event_new_toc (wav->toc, FALSE));
1407 #define MAX_BUFFER_SIZE 4096
1409 static GstFlowReturn
1410 gst_wavparse_stream_headers (GstWavParse * wav)
1412 GstFlowReturn res = GST_FLOW_OK;
1413 GstBuffer *buf = NULL;
1414 gst_riff_strf_auds *header = NULL;
1416 gboolean gotdata = FALSE;
1417 GstCaps *caps = NULL;
1418 gchar *codec_name = NULL;
1420 gint64 upstream_size = 0;
1422 /* search for "_fmt" chunk, which should be first */
1423 while (!wav->got_fmt) {
1426 /* The header starts with a 'fmt ' tag */
1427 if (wav->streaming) {
1428 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1431 gst_adapter_flush (wav->adapter, 8);
1435 buf = gst_adapter_take_buffer (wav->adapter, size);
1437 gst_adapter_flush (wav->adapter, 1);
1438 wav->offset += GST_ROUND_UP_2 (size);
1440 buf = gst_buffer_new ();
1443 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1444 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1448 if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ ||
1449 tag == GST_RIFF_TAG_bext || tag == GST_RIFF_TAG_BEXT ||
1450 tag == GST_RIFF_TAG_LIST || tag == GST_RIFF_TAG_ID32 ||
1451 tag == GST_RIFF_TAG_IDVX) {
1452 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1453 GST_FOURCC_ARGS (tag));
1454 gst_buffer_unref (buf);
1459 if (tag != GST_RIFF_TAG_fmt)
1462 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1464 goto parse_header_error;
1466 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1468 /* do sanity checks of header fields */
1469 if (header->channels == 0)
1471 if (header->rate == 0)
1474 GST_DEBUG_OBJECT (wav, "creating the caps");
1476 /* Note: gst_riff_create_audio_caps might need to fix values in
1477 * the header header depending on the format, so call it first */
1478 /* FIXME: Need to handle the channel reorder map */
1479 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1480 NULL, &codec_name, NULL);
1483 gst_buffer_unref (extra);
1486 goto unknown_format;
1488 /* do more sanity checks of header fields
1489 * (these can be sanitized by gst_riff_create_audio_caps()
1491 wav->format = header->format;
1492 wav->rate = header->rate;
1493 wav->channels = header->channels;
1494 wav->blockalign = header->blockalign;
1495 wav->depth = header->bits_per_sample;
1496 wav->av_bps = header->av_bps;
1502 /* do format specific handling */
1503 switch (wav->format) {
1504 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1505 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1507 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1508 * bitrate inside the mpeg stream */
1509 GST_INFO ("resetting bps from %u to 0 for mp2/3", wav->av_bps);
1513 case GST_RIFF_WAVE_FORMAT_PCM:
1514 if (wav->blockalign > wav->channels * ((wav->depth + 7) / 8))
1515 goto invalid_blockalign;
1518 if (wav->av_bps > wav->blockalign * wav->rate)
1520 /* use the configured bps */
1521 wav->bps = wav->av_bps;
1525 wav->width = (wav->blockalign * 8) / wav->channels;
1526 wav->bytes_per_sample = wav->channels * wav->width / 8;
1528 if (wav->bytes_per_sample <= 0)
1529 goto no_bytes_per_sample;
1531 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1532 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1533 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1534 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1535 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1536 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1537 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1539 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1540 * formats). This will make the element output a BYTE format segment and
1541 * will not timestamp the outgoing buffers.
1543 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1545 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1547 /* create pad later so we can sniff the first few bytes
1548 * of the real data and correct our caps if necessary */
1549 gst_caps_replace (&wav->caps, caps);
1550 gst_caps_replace (&caps, NULL);
1552 wav->got_fmt = TRUE;
1555 wav->tags = gst_tag_list_new_empty ();
1557 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1558 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1560 g_free (codec_name);
1566 gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1567 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1569 /* loop headers until we get data */
1571 if (wav->streaming) {
1572 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1579 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1580 &buf)) != GST_FLOW_OK)
1581 goto header_read_error;
1582 gst_buffer_map (buf, &map, GST_MAP_READ);
1583 tag = GST_READ_UINT32_LE (map.data);
1584 size = GST_READ_UINT32_LE (map.data + 4);
1585 gst_buffer_unmap (buf, &map);
1588 GST_INFO_OBJECT (wav,
1589 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT,
1590 GST_FOURCC_ARGS (tag), wav->offset);
1592 /* wav is a st00pid format, we don't know for sure where data starts.
1593 * So we have to go bit by bit until we find the 'data' header
1596 case GST_RIFF_TAG_data:{
1597 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %u", size);
1598 if (wav->ignore_length) {
1599 GST_DEBUG_OBJECT (wav, "Ignoring length");
1602 if (wav->streaming) {
1603 gst_adapter_flush (wav->adapter, 8);
1606 gst_buffer_unref (buf);
1609 wav->datastart = wav->offset;
1610 /* If size is zero, then the data chunk probably actually extends to
1611 the end of the file */
1612 if (size == 0 && upstream_size) {
1613 size = upstream_size - wav->datastart;
1615 /* Or the file might be truncated */
1616 else if (upstream_size) {
1617 size = MIN (size, (upstream_size - wav->datastart));
1619 wav->datasize = (guint64) size;
1620 wav->dataleft = (guint64) size;
1621 wav->end_offset = size + wav->datastart;
1622 if (!wav->streaming) {
1623 /* We will continue parsing tags 'till end */
1624 wav->offset += size;
1626 GST_DEBUG_OBJECT (wav, "datasize = %u", size);
1629 case GST_RIFF_TAG_fact:{
1630 if (wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1631 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1632 const guint data_size = 4;
1634 GST_INFO_OBJECT (wav, "Have fact chunk");
1635 if (size < data_size) {
1636 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1637 /* need more data */
1640 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1644 /* number of samples (for compressed formats) */
1645 if (wav->streaming) {
1646 const guint8 *data = NULL;
1648 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1651 gst_adapter_flush (wav->adapter, 8);
1652 data = gst_adapter_map (wav->adapter, data_size);
1653 wav->fact = GST_READ_UINT32_LE (data);
1654 gst_adapter_unmap (wav->adapter);
1655 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1657 gst_buffer_unref (buf);
1660 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1661 data_size, &buf)) != GST_FLOW_OK)
1662 goto header_read_error;
1663 gst_buffer_extract (buf, 0, &wav->fact, 4);
1664 wav->fact = GUINT32_FROM_LE (wav->fact);
1665 gst_buffer_unref (buf);
1667 GST_DEBUG_OBJECT (wav, "have fact %u", wav->fact);
1668 wav->offset += 8 + GST_ROUND_UP_2 (size);
1671 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1672 /* need more data */
1678 case GST_RIFF_TAG_acid:{
1679 const gst_riff_acid *acid = NULL;
1680 const guint data_size = sizeof (gst_riff_acid);
1683 GST_INFO_OBJECT (wav, "Have acid chunk");
1684 if (size < data_size) {
1685 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1686 /* need more data */
1689 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1693 if (wav->streaming) {
1694 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1697 gst_adapter_flush (wav->adapter, 8);
1698 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1700 tempo = acid->tempo;
1701 gst_adapter_unmap (wav->adapter);
1704 gst_buffer_unref (buf);
1707 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1708 size, &buf)) != GST_FLOW_OK)
1709 goto header_read_error;
1710 gst_buffer_map (buf, &map, GST_MAP_READ);
1711 acid = (const gst_riff_acid *) map.data;
1712 tempo = acid->tempo;
1713 gst_buffer_unmap (buf, &map);
1715 /* send data as tags */
1717 wav->tags = gst_tag_list_new_empty ();
1718 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1719 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1721 size = GST_ROUND_UP_2 (size);
1722 if (wav->streaming) {
1723 gst_adapter_flush (wav->adapter, size);
1725 gst_buffer_unref (buf);
1727 wav->offset += 8 + size;
1730 /* FIXME: all list tags after data are ignored in streaming mode */
1731 case GST_RIFF_TAG_LIST:{
1734 if (wav->streaming) {
1735 const guint8 *data = NULL;
1737 if (gst_adapter_available (wav->adapter) < 12) {
1740 data = gst_adapter_map (wav->adapter, 12);
1741 ltag = GST_READ_UINT32_LE (data + 8);
1742 gst_adapter_unmap (wav->adapter);
1744 gst_buffer_unref (buf);
1747 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1748 &buf)) != GST_FLOW_OK)
1749 goto header_read_error;
1750 gst_buffer_extract (buf, 8, <ag, 4);
1751 ltag = GUINT32_FROM_LE (ltag);
1754 case GST_RIFF_LIST_INFO:{
1755 const gint data_size = size - 4;
1758 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1759 if (wav->streaming) {
1760 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1763 gst_adapter_flush (wav->adapter, 12);
1765 if (data_size > 0) {
1766 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1768 gst_adapter_flush (wav->adapter, 1);
1772 gst_buffer_unref (buf);
1774 if (data_size > 0) {
1776 gst_pad_pull_range (wav->sinkpad, wav->offset,
1777 data_size, &buf)) != GST_FLOW_OK)
1778 goto header_read_error;
1781 if (data_size > 0) {
1783 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1785 GstTagList *old = wav->tags;
1787 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1789 gst_tag_list_unref (old);
1790 gst_tag_list_unref (new);
1792 gst_buffer_unref (buf);
1793 wav->offset += GST_ROUND_UP_2 (data_size);
1797 case GST_RIFF_LIST_adtl:{
1798 const gint data_size = size;
1800 GST_INFO_OBJECT (wav, "Have 'adtl' LIST, size %u", data_size);
1801 if (wav->streaming) {
1802 const guint8 *data = NULL;
1804 gst_adapter_flush (wav->adapter, 12);
1805 data = gst_adapter_map (wav->adapter, data_size);
1806 gst_wavparse_adtl_chunk (wav, data, data_size);
1807 gst_adapter_unmap (wav->adapter);
1811 gst_buffer_unref (buf);
1814 gst_pad_pull_range (wav->sinkpad, wav->offset + 12,
1815 data_size, &buf)) != GST_FLOW_OK)
1816 goto header_read_error;
1817 gst_buffer_map (buf, &map, GST_MAP_READ);
1818 gst_wavparse_adtl_chunk (wav, (const guint8 *) map.data,
1820 gst_buffer_unmap (buf, &map);
1824 GST_INFO_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1825 GST_FOURCC_ARGS (ltag));
1826 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1827 /* need more data */
1833 case GST_RIFF_TAG_cue:{
1834 const guint data_size = size;
1836 GST_DEBUG_OBJECT (wav, "Have 'cue' TAG, size : %u", data_size);
1837 if (wav->streaming) {
1838 const guint8 *data = NULL;
1840 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1843 gst_adapter_flush (wav->adapter, 8);
1845 data = gst_adapter_map (wav->adapter, data_size);
1846 if (!gst_wavparse_cue_chunk (wav, data, data_size)) {
1847 goto header_read_error;
1849 gst_adapter_unmap (wav->adapter);
1854 gst_buffer_unref (buf);
1857 gst_pad_pull_range (wav->sinkpad, wav->offset,
1858 data_size, &buf)) != GST_FLOW_OK)
1859 goto header_read_error;
1860 gst_buffer_map (buf, &map, GST_MAP_READ);
1861 if (!gst_wavparse_cue_chunk (wav, (const guint8 *) map.data,
1863 goto header_read_error;
1865 gst_buffer_unmap (buf, &map);
1867 size = GST_ROUND_UP_2 (size);
1868 if (wav->streaming) {
1869 gst_adapter_flush (wav->adapter, size);
1871 gst_buffer_unref (buf);
1873 size = GST_ROUND_UP_2 (size);
1874 wav->offset += size;
1878 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1879 /* need more data */
1884 if (upstream_size && (wav->offset >= upstream_size)) {
1885 /* Now we are gone through the whole file */
1890 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1892 if (wav->bps <= 0 && wav->fact) {
1894 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1896 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1897 (guint64) wav->fact);
1898 GST_INFO_OBJECT (wav, "calculated bps : %u, enabling VBR", wav->bps);
1903 if (gst_wavparse_calculate_duration (wav)) {
1904 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1905 if (!wav->ignore_length)
1906 wav->segment.duration = wav->duration;
1908 gst_wavparse_create_toc (wav);
1910 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1911 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1912 if (!wav->ignore_length)
1913 wav->segment.duration = wav->datasize;
1916 /* now we have all the info to perform a pending seek if any, if no
1917 * event, this will still do the right thing and it will also send
1918 * the right newsegment event downstream. */
1919 gst_wavparse_perform_seek (wav, wav->seek_event);
1920 /* remove pending event */
1921 event_p = &wav->seek_event;
1922 gst_event_replace (event_p, NULL);
1924 /* we just started, we are discont */
1925 wav->discont = TRUE;
1927 wav->state = GST_WAVPARSE_DATA;
1929 /* determine reasonable max buffer size,
1930 * that is, buffers not too small either size or time wise
1931 * so we do not end up with too many of them */
1934 gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size);
1935 wav->max_buf_size = upstream_size;
1936 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1937 if (wav->blockalign > 0)
1938 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1940 GST_DEBUG_OBJECT (wav, "max buffer size %u", wav->max_buf_size);
1948 g_free (codec_name);
1952 gst_caps_unref (caps);
1957 res = GST_FLOW_ERROR;
1962 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1963 ("Invalid WAV header (no fmt at start): %"
1964 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1969 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1970 ("Couldn't parse audio header"));
1975 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1976 ("Stream claims to contain no channels - invalid data"));
1981 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1982 ("Stream with sample_rate == 0 - invalid data"));
1987 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1988 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1989 wav->blockalign, wav->channels * ((wav->depth + 7) / 8)));
1994 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1995 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1996 wav->av_bps, wav->blockalign * wav->rate));
1999 no_bytes_per_sample:
2001 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
2002 ("Could not caluclate bytes per sample - invalid data"));
2007 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
2008 ("No caps found for format 0x%x, %u channels, %u Hz",
2009 wav->format, wav->channels, wav->rate));
2014 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
2015 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
2021 * Read WAV file tag when streaming
2023 static GstFlowReturn
2024 gst_wavparse_parse_stream_init (GstWavParse * wav)
2026 if (gst_adapter_available (wav->adapter) >= 12) {
2029 /* _take flushes the data */
2030 tmp = gst_adapter_take_buffer (wav->adapter, 12);
2032 GST_DEBUG ("Parsing wav header");
2033 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
2034 return GST_FLOW_ERROR;
2037 /* Go to next state */
2038 wav->state = GST_WAVPARSE_HEADER;
2043 /* handle an event sent directly to the element.
2045 * This event can be sent either in the READY state or the
2046 * >READY state. The only event of interest really is the seek
2049 * In the READY state we can only store the event and try to
2050 * respect it when going to PAUSED. We assume we are in the
2051 * READY state when our parsing state != GST_WAVPARSE_DATA.
2053 * When we are steaming, we can simply perform the seek right
2057 gst_wavparse_send_event (GstElement * element, GstEvent * event)
2059 GstWavParse *wav = GST_WAVPARSE (element);
2060 gboolean res = FALSE;
2063 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
2065 switch (GST_EVENT_TYPE (event)) {
2066 case GST_EVENT_SEEK:
2067 if (wav->state == GST_WAVPARSE_DATA) {
2068 /* we can handle the seek directly when streaming data */
2069 res = gst_wavparse_perform_seek (wav, event);
2071 GST_DEBUG_OBJECT (wav, "queuing seek for later");
2073 event_p = &wav->seek_event;
2074 gst_event_replace (event_p, event);
2076 /* we always return true */
2083 gst_event_unref (event);
2088 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
2092 s = gst_caps_get_structure (caps, 0);
2093 if (!gst_structure_has_name (s, "audio/x-dts"))
2095 if (prob >= GST_TYPE_FIND_LIKELY)
2097 /* DTS at non-0 offsets and without second sync may yield POSSIBLE .. */
2098 if (prob < GST_TYPE_FIND_POSSIBLE)
2100 /* .. in which case we want at least a valid-looking rate and channels */
2101 if (!gst_structure_has_field (s, "channels"))
2103 /* and for extra assurance we could also check the rate from the DTS frame
2104 * against the one in the wav header, but for now let's not do that */
2105 return gst_structure_has_field (s, "rate");
2109 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
2113 GST_DEBUG_OBJECT (wav, "adding src pad");
2115 g_assert (wav->caps != NULL);
2117 s = gst_caps_get_structure (wav->caps, 0);
2118 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
2119 GstTypeFindProbability prob;
2122 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
2123 if (tf_caps != NULL) {
2124 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
2125 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
2126 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
2127 gst_caps_unref (wav->caps);
2128 wav->caps = tf_caps;
2130 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
2131 GST_TAG_AUDIO_CODEC, "dts", NULL);
2133 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
2134 "marked as raw PCM audio, but ignoring for now", tf_caps);
2135 gst_caps_unref (tf_caps);
2140 gst_pad_set_caps (wav->srcpad, wav->caps);
2141 gst_caps_replace (&wav->caps, NULL);
2143 if (wav->start_segment) {
2144 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
2145 gst_pad_push_event (wav->srcpad, wav->start_segment);
2146 wav->start_segment = NULL;
2150 gst_pad_push_event (wav->srcpad, gst_event_new_tag (wav->tags));
2155 static GstFlowReturn
2156 gst_wavparse_stream_data (GstWavParse * wav)
2158 GstBuffer *buf = NULL;
2159 GstFlowReturn res = GST_FLOW_OK;
2160 guint64 desired, obtained;
2161 GstClockTime timestamp, next_timestamp, duration;
2162 guint64 pos, nextpos;
2165 GST_LOG_OBJECT (wav,
2166 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
2167 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
2169 /* Get the next n bytes and output them */
2170 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
2173 /* scale the amount of data by the segment rate so we get equal
2174 * amounts of data regardless of the playback rate */
2176 MIN (gst_guint64_to_gdouble (wav->dataleft),
2177 wav->max_buf_size * ABS (wav->segment.rate));
2179 if (desired >= wav->blockalign && wav->blockalign > 0)
2180 desired -= (desired % wav->blockalign);
2182 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
2183 "from the sinkpad", desired);
2185 if (wav->streaming) {
2186 guint avail = gst_adapter_available (wav->adapter);
2189 /* flush some bytes if evil upstream sends segment that starts
2190 * before data or does is not send sample aligned segment */
2191 if (G_LIKELY (wav->offset >= wav->datastart)) {
2192 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
2194 extra = wav->datastart - wav->offset;
2197 if (G_UNLIKELY (extra)) {
2198 extra = wav->bytes_per_sample - extra;
2199 if (extra <= avail) {
2200 GST_DEBUG_OBJECT (wav, "flushing %u bytes to sample boundary", extra);
2201 gst_adapter_flush (wav->adapter, extra);
2202 wav->offset += extra;
2203 wav->dataleft -= extra;
2204 goto iterate_adapter;
2206 GST_DEBUG_OBJECT (wav, "flushing %u bytes", avail);
2207 gst_adapter_clear (wav->adapter);
2208 wav->offset += avail;
2209 wav->dataleft -= avail;
2214 if (avail < desired) {
2215 GST_LOG_OBJECT (wav, "Got only %u bytes of data from the sinkpad", avail);
2219 buf = gst_adapter_take_buffer (wav->adapter, desired);
2221 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
2222 desired, &buf)) != GST_FLOW_OK)
2225 /* we may get a short buffer at the end of the file */
2226 if (gst_buffer_get_size (buf) < desired) {
2227 gsize size = gst_buffer_get_size (buf);
2229 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
2230 if (size >= wav->blockalign) {
2231 buf = gst_buffer_make_writable (buf);
2232 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
2234 gst_buffer_unref (buf);
2240 obtained = gst_buffer_get_size (buf);
2242 /* our positions in bytes */
2243 pos = wav->offset - wav->datastart;
2244 nextpos = pos + obtained;
2246 /* update offsets, does not overflow. */
2247 buf = gst_buffer_make_writable (buf);
2248 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
2249 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
2251 /* first chunk of data? create the source pad. We do this only here so
2252 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
2253 if (G_UNLIKELY (wav->first)) {
2255 /* this will also push the segment events */
2256 gst_wavparse_add_src_pad (wav, buf);
2258 /* If we have a pending start segment, send it now. */
2259 if (G_UNLIKELY (wav->start_segment != NULL)) {
2260 gst_pad_push_event (wav->srcpad, wav->start_segment);
2261 wav->start_segment = NULL;
2266 /* and timestamps if we have a bitrate, be careful for overflows */
2268 gst_util_uint64_scale_ceil (pos, GST_SECOND, (guint64) wav->bps);
2270 gst_util_uint64_scale_ceil (nextpos, GST_SECOND, (guint64) wav->bps);
2271 duration = next_timestamp - timestamp;
2273 /* update current running segment position */
2274 if (G_LIKELY (next_timestamp >= wav->segment.start))
2275 wav->segment.position = next_timestamp;
2276 } else if (wav->fact) {
2278 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2279 /* and timestamps if we have a bitrate, be careful for overflows */
2280 timestamp = gst_util_uint64_scale_ceil (pos, GST_SECOND, bps);
2281 next_timestamp = gst_util_uint64_scale_ceil (nextpos, GST_SECOND, bps);
2282 duration = next_timestamp - timestamp;
2284 /* no bitrate, all we know is that the first sample has timestamp 0, all
2285 * other positions and durations have unknown timestamp. */
2289 timestamp = GST_CLOCK_TIME_NONE;
2290 duration = GST_CLOCK_TIME_NONE;
2291 /* update current running segment position with byte offset */
2292 if (G_LIKELY (nextpos >= wav->segment.start))
2293 wav->segment.position = nextpos;
2295 if ((pos > 0) && wav->vbr) {
2296 /* don't set timestamps for VBR files if it's not the first buffer */
2297 timestamp = GST_CLOCK_TIME_NONE;
2298 duration = GST_CLOCK_TIME_NONE;
2301 GST_DEBUG_OBJECT (wav, "marking DISCONT");
2302 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2303 wav->discont = FALSE;
2306 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2307 GST_BUFFER_DURATION (buf) = duration;
2309 GST_LOG_OBJECT (wav,
2310 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
2311 ", size:%" G_GSIZE_FORMAT, GST_TIME_ARGS (timestamp),
2312 GST_TIME_ARGS (duration), gst_buffer_get_size (buf));
2314 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
2317 if (obtained < wav->dataleft) {
2318 wav->offset += obtained;
2319 wav->dataleft -= obtained;
2321 wav->offset += wav->dataleft;
2325 /* Iterate until need more data, so adapter size won't grow */
2326 if (wav->streaming) {
2327 GST_LOG_OBJECT (wav,
2328 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
2330 goto iterate_adapter;
2337 GST_DEBUG_OBJECT (wav, "found EOS");
2338 return GST_FLOW_EOS;
2342 /* check if we got EOS */
2343 if (res == GST_FLOW_EOS)
2346 GST_WARNING_OBJECT (wav,
2347 "Error getting %" G_GINT64_FORMAT " bytes from the "
2348 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
2353 GST_INFO_OBJECT (wav,
2354 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
2355 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
2356 gst_pad_is_linked (wav->srcpad));
2362 gst_wavparse_loop (GstPad * pad)
2365 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2369 GST_LOG_OBJECT (wav, "process data");
2371 switch (wav->state) {
2372 case GST_WAVPARSE_START:
2373 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2374 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2378 gst_pad_create_stream_id (wav->srcpad, GST_ELEMENT_CAST (wav), NULL);
2379 event = gst_event_new_stream_start (stream_id);
2380 gst_event_set_group_id (event, gst_util_group_id_next ());
2381 gst_pad_push_event (wav->srcpad, event);
2384 wav->state = GST_WAVPARSE_HEADER;
2387 case GST_WAVPARSE_HEADER:
2388 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2389 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2392 wav->state = GST_WAVPARSE_DATA;
2393 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2396 case GST_WAVPARSE_DATA:
2397 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2401 g_assert_not_reached ();
2408 const gchar *reason = gst_flow_get_name (ret);
2410 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2411 gst_pad_pause_task (pad);
2413 if (ret == GST_FLOW_EOS) {
2414 /* handle end-of-stream/segment */
2415 /* so align our position with the end of it, if there is one
2416 * this ensures a subsequent will arrive at correct base/acc time */
2417 if (wav->segment.format == GST_FORMAT_TIME) {
2418 if (wav->segment.rate > 0.0 &&
2419 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2420 wav->segment.position = wav->segment.stop;
2421 else if (wav->segment.rate < 0.0)
2422 wav->segment.position = wav->segment.start;
2424 if (wav->state == GST_WAVPARSE_START) {
2425 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2426 ("No valid input found before end of stream"));
2427 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2429 /* add pad before we perform EOS */
2430 if (G_UNLIKELY (wav->first)) {
2432 gst_wavparse_add_src_pad (wav, NULL);
2435 /* perform EOS logic */
2436 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2439 if ((stop = wav->segment.stop) == -1)
2440 stop = wav->segment.duration;
2442 gst_element_post_message (GST_ELEMENT_CAST (wav),
2443 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2444 wav->segment.format, stop));
2445 gst_pad_push_event (wav->srcpad,
2446 gst_event_new_segment_done (wav->segment.format, stop));
2448 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2451 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2452 /* for fatal errors we post an error message, post the error
2453 * first so the app knows about the error first. */
2454 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
2455 (_("Internal data flow error.")),
2456 ("streaming task paused, reason %s (%d)", reason, ret));
2457 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2463 static GstFlowReturn
2464 gst_wavparse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2467 GstWavParse *wav = GST_WAVPARSE (parent);
2469 GST_LOG_OBJECT (wav, "adapter_push %" G_GSIZE_FORMAT " bytes",
2470 gst_buffer_get_size (buf));
2472 gst_adapter_push (wav->adapter, buf);
2474 switch (wav->state) {
2475 case GST_WAVPARSE_START:
2476 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2477 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2480 if (wav->state != GST_WAVPARSE_HEADER)
2483 /* otherwise fall-through */
2484 case GST_WAVPARSE_HEADER:
2485 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2486 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2489 if (!wav->got_fmt || wav->datastart == 0)
2492 wav->state = GST_WAVPARSE_DATA;
2493 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2496 case GST_WAVPARSE_DATA:
2497 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2498 wav->discont = TRUE;
2499 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2503 g_return_val_if_reached (GST_FLOW_ERROR);
2506 if (G_UNLIKELY (wav->abort_buffering)) {
2507 wav->abort_buffering = FALSE;
2508 ret = GST_FLOW_ERROR;
2509 /* sort of demux/parse error */
2510 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2516 static GstFlowReturn
2517 gst_wavparse_flush_data (GstWavParse * wav)
2519 GstFlowReturn ret = GST_FLOW_OK;
2522 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2524 wav->end_offset = wav->offset + av;
2525 ret = gst_wavparse_stream_data (wav);
2532 gst_wavparse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2534 GstWavParse *wav = GST_WAVPARSE (parent);
2535 gboolean ret = TRUE;
2537 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2539 switch (GST_EVENT_TYPE (event)) {
2540 case GST_EVENT_CAPS:
2542 /* discard, we'll come up with proper src caps */
2543 gst_event_unref (event);
2546 case GST_EVENT_SEGMENT:
2548 gint64 start, stop, offset = 0, end_offset = -1;
2551 /* some debug output */
2552 gst_event_copy_segment (event, &segment);
2553 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2556 if (wav->state != GST_WAVPARSE_DATA) {
2557 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2561 /* now we are either committed to TIME or BYTE format,
2562 * and we only expect a BYTE segment, e.g. following a seek */
2563 if (segment.format == GST_FORMAT_BYTES) {
2564 /* handle (un)signed issues */
2565 start = segment.start;
2566 stop = segment.stop;
2569 start -= wav->datastart;
2570 start = MAX (start, 0);
2574 segment.stop -= wav->datastart;
2575 segment.stop = MAX (stop, 0);
2577 if (wav->segment.format == GST_FORMAT_TIME) {
2578 guint64 bps = wav->bps;
2580 /* operating in format TIME, so we can convert */
2581 if (!bps && wav->fact)
2583 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2587 gst_util_uint64_scale_ceil (start, GST_SECOND,
2588 (guint64) wav->bps);
2591 gst_util_uint64_scale_ceil (stop, GST_SECOND,
2592 (guint64) wav->bps);
2596 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2600 segment.start = start;
2601 segment.stop = stop;
2603 /* accept upstream's notion of segment and distribute along */
2604 segment.format = wav->segment.format;
2605 segment.time = segment.position = segment.start;
2606 segment.duration = wav->segment.duration;
2607 segment.base = gst_segment_to_running_time (&wav->segment,
2608 GST_FORMAT_TIME, wav->segment.position);
2610 gst_segment_copy_into (&segment, &wav->segment);
2612 /* also store the newsegment event for the streaming thread */
2613 if (wav->start_segment)
2614 gst_event_unref (wav->start_segment);
2615 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2616 wav->start_segment = gst_event_new_segment (&segment);
2618 /* stream leftover data in current segment */
2619 gst_wavparse_flush_data (wav);
2620 /* and set up streaming thread for next one */
2621 wav->offset = offset;
2622 wav->end_offset = end_offset;
2623 if (wav->end_offset > 0) {
2624 wav->dataleft = wav->end_offset - wav->offset;
2626 /* infinity; upstream will EOS when done */
2627 wav->dataleft = G_MAXUINT64;
2630 gst_event_unref (event);
2634 if (wav->state == GST_WAVPARSE_START) {
2635 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE, (NULL),
2636 ("No valid input found before end of stream"));
2638 /* add pad if needed so EOS is seen downstream */
2639 if (G_UNLIKELY (wav->first)) {
2641 gst_wavparse_add_src_pad (wav, NULL);
2643 /* stream leftover data in current segment */
2644 gst_wavparse_flush_data (wav);
2649 case GST_EVENT_FLUSH_STOP:
2653 gst_adapter_clear (wav->adapter);
2654 wav->discont = TRUE;
2655 dur = wav->segment.duration;
2656 gst_segment_init (&wav->segment, wav->segment.format);
2657 wav->segment.duration = dur;
2661 ret = gst_pad_event_default (wav->sinkpad, parent, event);
2669 /* convert and query stuff */
2670 static const GstFormat *
2671 gst_wavparse_get_formats (GstPad * pad)
2673 static GstFormat formats[] = {
2676 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2685 gst_wavparse_pad_convert (GstPad * pad,
2686 GstFormat src_format, gint64 src_value,
2687 GstFormat * dest_format, gint64 * dest_value)
2689 GstWavParse *wavparse;
2690 gboolean res = TRUE;
2692 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2694 if (*dest_format == src_format) {
2695 *dest_value = src_value;
2699 if ((wavparse->bps == 0) && !wavparse->fact)
2702 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2703 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2705 switch (src_format) {
2706 case GST_FORMAT_BYTES:
2707 switch (*dest_format) {
2708 case GST_FORMAT_DEFAULT:
2709 *dest_value = src_value / wavparse->bytes_per_sample;
2710 /* make sure we end up on a sample boundary */
2711 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2713 case GST_FORMAT_TIME:
2714 /* src_value + datastart = offset */
2715 GST_INFO_OBJECT (wavparse,
2716 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2718 if (wavparse->bps > 0)
2719 *dest_value = gst_util_uint64_scale_ceil (src_value, GST_SECOND,
2720 (guint64) wavparse->bps);
2721 else if (wavparse->fact) {
2722 guint64 bps = gst_util_uint64_scale_int_ceil (wavparse->datasize,
2723 wavparse->rate, wavparse->fact);
2726 gst_util_uint64_scale_int_ceil (src_value, GST_SECOND, bps);
2737 case GST_FORMAT_DEFAULT:
2738 switch (*dest_format) {
2739 case GST_FORMAT_BYTES:
2740 *dest_value = src_value * wavparse->bytes_per_sample;
2742 case GST_FORMAT_TIME:
2743 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2744 (guint64) wavparse->rate);
2752 case GST_FORMAT_TIME:
2753 switch (*dest_format) {
2754 case GST_FORMAT_BYTES:
2755 if (wavparse->bps > 0)
2756 *dest_value = gst_util_uint64_scale (src_value,
2757 (guint64) wavparse->bps, GST_SECOND);
2759 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2760 wavparse->rate, wavparse->fact);
2762 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2764 /* make sure we end up on a sample boundary */
2765 *dest_value -= *dest_value % wavparse->blockalign;
2767 case GST_FORMAT_DEFAULT:
2768 *dest_value = gst_util_uint64_scale (src_value,
2769 (guint64) wavparse->rate, GST_SECOND);
2788 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2794 /* handle queries for location and length in requested format */
2796 gst_wavparse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
2798 gboolean res = TRUE;
2799 GstWavParse *wav = GST_WAVPARSE (parent);
2801 /* only if we know */
2802 if (wav->state != GST_WAVPARSE_DATA) {
2806 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2808 switch (GST_QUERY_TYPE (query)) {
2809 case GST_QUERY_POSITION:
2815 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2816 curb = wav->offset - wav->datastart;
2817 gst_query_parse_position (query, &format, NULL);
2818 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2821 case GST_FORMAT_BYTES:
2822 format = GST_FORMAT_BYTES;
2826 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2831 gst_query_set_position (query, format, cur);
2834 case GST_QUERY_DURATION:
2836 gint64 duration = 0;
2839 if (wav->ignore_length) {
2844 gst_query_parse_duration (query, &format, NULL);
2847 case GST_FORMAT_BYTES:{
2848 format = GST_FORMAT_BYTES;
2849 duration = wav->datasize;
2852 case GST_FORMAT_TIME:
2853 if ((res = gst_wavparse_calculate_duration (wav))) {
2854 duration = wav->duration;
2862 gst_query_set_duration (query, format, duration);
2865 case GST_QUERY_CONVERT:
2867 gint64 srcvalue, dstvalue;
2868 GstFormat srcformat, dstformat;
2870 gst_query_parse_convert (query, &srcformat, &srcvalue,
2871 &dstformat, &dstvalue);
2872 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2873 &dstformat, &dstvalue);
2875 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2878 case GST_QUERY_SEEKING:{
2880 gboolean seekable = FALSE;
2882 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2883 if (fmt == wav->segment.format) {
2884 if (wav->streaming) {
2887 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2888 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2889 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2890 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2892 gst_query_unref (q);
2894 GST_LOG_OBJECT (wav, "looping => seekable");
2898 } else if (fmt == GST_FORMAT_TIME) {
2902 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2907 res = gst_pad_query_default (pad, parent, query);
2914 gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
2916 GstWavParse *wavparse = GST_WAVPARSE (parent);
2917 gboolean res = FALSE;
2919 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2921 switch (GST_EVENT_TYPE (event)) {
2922 case GST_EVENT_SEEK:
2923 /* can only handle events when we are in the data state */
2924 if (wavparse->state == GST_WAVPARSE_DATA) {
2925 res = gst_wavparse_perform_seek (wavparse, event);
2927 gst_event_unref (event);
2930 case GST_EVENT_TOC_SELECT:
2933 GstTocEntry *entry = NULL;
2934 GstEvent *seek_event;
2937 if (!wavparse->toc) {
2938 GST_DEBUG_OBJECT (wavparse, "no TOC to select");
2941 gst_event_parse_toc_select (event, &uid);
2943 GST_OBJECT_LOCK (wavparse);
2944 entry = gst_toc_find_entry (wavparse->toc, uid);
2945 if (entry == NULL) {
2946 GST_OBJECT_UNLOCK (wavparse);
2947 GST_WARNING_OBJECT (wavparse, "no TOC entry with given UID: %s",
2951 gst_toc_entry_get_start_stop_times (entry, &start_pos, NULL);
2952 GST_OBJECT_UNLOCK (wavparse);
2953 seek_event = gst_event_new_seek (1.0,
2955 GST_SEEK_FLAG_FLUSH,
2956 GST_SEEK_TYPE_SET, start_pos, GST_SEEK_TYPE_SET, -1);
2957 res = gst_wavparse_perform_seek (wavparse, seek_event);
2958 gst_event_unref (seek_event);
2962 GST_WARNING_OBJECT (wavparse, "received empty TOC select event");
2966 gst_event_unref (event);
2971 res = gst_pad_push_event (wavparse->sinkpad, event);
2978 gst_wavparse_sink_activate (GstPad * sinkpad, GstObject * parent)
2980 GstWavParse *wav = GST_WAVPARSE (parent);
2985 gst_adapter_clear (wav->adapter);
2986 g_object_unref (wav->adapter);
2987 wav->adapter = NULL;
2990 query = gst_query_new_scheduling ();
2992 if (!gst_pad_peer_query (sinkpad, query)) {
2993 gst_query_unref (query);
2997 pull_mode = gst_query_has_scheduling_mode_with_flags (query,
2998 GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
2999 gst_query_unref (query);
3004 GST_DEBUG_OBJECT (sinkpad, "activating pull");
3005 wav->streaming = FALSE;
3006 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
3010 GST_DEBUG_OBJECT (sinkpad, "activating push");
3011 wav->streaming = TRUE;
3012 wav->adapter = gst_adapter_new ();
3013 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
3019 gst_wavparse_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
3020 GstPadMode mode, gboolean active)
3025 case GST_PAD_MODE_PUSH:
3028 case GST_PAD_MODE_PULL:
3030 /* if we have a scheduler we can start the task */
3031 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
3034 res = gst_pad_stop_task (sinkpad);
3044 static GstStateChangeReturn
3045 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
3047 GstStateChangeReturn ret;
3048 GstWavParse *wav = GST_WAVPARSE (element);
3050 switch (transition) {
3051 case GST_STATE_CHANGE_NULL_TO_READY:
3053 case GST_STATE_CHANGE_READY_TO_PAUSED:
3054 gst_wavparse_reset (wav);
3056 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
3062 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
3064 switch (transition) {
3065 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
3067 case GST_STATE_CHANGE_PAUSED_TO_READY:
3068 gst_wavparse_reset (wav);
3070 case GST_STATE_CHANGE_READY_TO_NULL:
3079 gst_wavparse_set_property (GObject * object, guint prop_id,
3080 const GValue * value, GParamSpec * pspec)
3084 g_return_if_fail (GST_IS_WAVPARSE (object));
3085 self = GST_WAVPARSE (object);
3088 case PROP_IGNORE_LENGTH:
3089 self->ignore_length = g_value_get_boolean (value);
3092 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
3098 gst_wavparse_get_property (GObject * object, guint prop_id,
3099 GValue * value, GParamSpec * pspec)
3103 g_return_if_fail (GST_IS_WAVPARSE (object));
3104 self = GST_WAVPARSE (object);
3107 case PROP_IGNORE_LENGTH:
3108 g_value_set_boolean (value, self->ignore_length);
3111 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
3116 plugin_init (GstPlugin * plugin)
3120 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
3124 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
3127 "Parse a .wav file into raw audio",
3128 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)