1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
3 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * Copyright (C) <2006> Nokia Corporation, Stefan Kost <stefan.kost@nokia.com>.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-wavparse
25 * Parse a .wav file into raw or compressed audio.
27 * Wavparse supports both push and pull mode operations, making it possible to
28 * stream from a network source.
31 * <title>Example launch line</title>
33 * gst-launch filesrc location=sine.wav ! wavparse ! audioconvert ! alsasink
34 * ]| Read a wav file and output to the soundcard using the ALSA element. The
35 * wav file is assumed to contain raw uncompressed samples.
37 * gst-launch gnomevfssrc location=http://www.example.org/sine.wav ! queue ! wavparse ! audioconvert ! alsasink
38 * ]| Stream data from a network url.
41 * Last reviewed on 2007-02-14 (0.10.6)
46 * http://replaygain.hydrogenaudio.org/file_format_wav.html
55 #include "gstwavparse.h"
56 #include "gst/riff/riff-ids.h"
57 #include "gst/riff/riff-media.h"
58 #include <gst/base/gsttypefindhelper.h>
59 #include <gst/gst-i18n-plugin.h>
61 GST_DEBUG_CATEGORY_STATIC (wavparse_debug);
62 #define GST_CAT_DEFAULT (wavparse_debug)
64 static void gst_wavparse_dispose (GObject * object);
66 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad);
67 static gboolean gst_wavparse_sink_activate_pull (GstPad * sinkpad,
69 static gboolean gst_wavparse_send_event (GstElement * element,
71 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
72 GstStateChange transition);
74 static const GstQueryType *gst_wavparse_get_query_types (GstPad * pad);
75 static gboolean gst_wavparse_pad_query (GstPad * pad, GstQuery * query);
76 static gboolean gst_wavparse_pad_convert (GstPad * pad,
78 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
80 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstBuffer * buf);
81 static gboolean gst_wavparse_sink_event (GstPad * pad, GstEvent * event);
82 static void gst_wavparse_loop (GstPad * pad);
83 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstEvent * event);
85 static GstStaticPadTemplate sink_template_factory =
86 GST_STATIC_PAD_TEMPLATE ("wavparse_sink",
89 GST_STATIC_CAPS ("audio/x-wav")
92 #define DEBUG_INIT(bla) \
93 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
95 GST_BOILERPLATE_FULL (GstWavParse, gst_wavparse, GstElement,
96 GST_TYPE_ELEMENT, DEBUG_INIT);
99 gst_wavparse_base_init (gpointer g_class)
101 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
102 GstPadTemplate *src_template;
105 gst_element_class_add_pad_template (element_class,
106 gst_static_pad_template_get (&sink_template_factory));
108 src_template = gst_pad_template_new ("wavparse_src", GST_PAD_SRC,
109 GST_PAD_SOMETIMES, gst_riff_create_audio_template_caps ());
110 gst_element_class_add_pad_template (element_class, src_template);
111 gst_object_unref (src_template);
113 gst_element_class_set_details_simple (element_class, "WAV audio demuxer",
114 "Codec/Demuxer/Audio",
115 "Parse a .wav file into raw audio",
116 "Erik Walthinsen <omega@cse.ogi.edu>");
120 gst_wavparse_class_init (GstWavParseClass * klass)
122 GstElementClass *gstelement_class;
123 GObjectClass *object_class;
125 gstelement_class = (GstElementClass *) klass;
126 object_class = (GObjectClass *) klass;
128 parent_class = g_type_class_peek_parent (klass);
130 object_class->dispose = gst_wavparse_dispose;
132 gstelement_class->change_state = gst_wavparse_change_state;
133 gstelement_class->send_event = gst_wavparse_send_event;
137 gst_wavparse_reset (GstWavParse * wav)
139 wav->state = GST_WAVPARSE_START;
141 /* These will all be set correctly in the fmt chunk */
155 wav->got_fmt = FALSE;
159 gst_event_unref (wav->seek_event);
160 wav->seek_event = NULL;
162 gst_adapter_clear (wav->adapter);
163 g_object_unref (wav->adapter);
167 gst_tag_list_free (wav->tags);
170 gst_caps_unref (wav->caps);
172 if (wav->start_segment)
173 gst_event_unref (wav->start_segment);
174 wav->start_segment = NULL;
175 if (wav->close_segment)
176 gst_event_unref (wav->close_segment);
177 wav->close_segment = NULL;
181 gst_wavparse_dispose (GObject * object)
183 GstWavParse *wav = GST_WAVPARSE (object);
185 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
186 gst_wavparse_reset (wav);
188 G_OBJECT_CLASS (parent_class)->dispose (object);
192 gst_wavparse_init (GstWavParse * wavparse, GstWavParseClass * g_class)
194 gst_wavparse_reset (wavparse);
198 gst_pad_new_from_static_template (&sink_template_factory, "sink");
199 gst_pad_set_activate_function (wavparse->sinkpad,
200 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
201 gst_pad_set_activatepull_function (wavparse->sinkpad,
202 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_pull));
203 gst_pad_set_chain_function (wavparse->sinkpad,
204 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
205 gst_pad_set_event_function (wavparse->sinkpad,
206 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
207 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
209 /* src, will be created later */
210 wavparse->srcpad = NULL;
214 gst_wavparse_destroy_sourcepad (GstWavParse * wavparse)
216 if (wavparse->srcpad) {
217 gst_element_remove_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
218 wavparse->srcpad = NULL;
223 gst_wavparse_create_sourcepad (GstWavParse * wavparse)
225 GstElementClass *klass = GST_ELEMENT_GET_CLASS (wavparse);
226 GstPadTemplate *src_template;
228 /* destroy previous one */
229 gst_wavparse_destroy_sourcepad (wavparse);
232 src_template = gst_element_class_get_pad_template (klass, "wavparse_src");
233 wavparse->srcpad = gst_pad_new_from_template (src_template, "src");
234 gst_pad_use_fixed_caps (wavparse->srcpad);
235 gst_pad_set_query_type_function (wavparse->srcpad,
236 GST_DEBUG_FUNCPTR (gst_wavparse_get_query_types));
237 gst_pad_set_query_function (wavparse->srcpad,
238 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
239 gst_pad_set_event_function (wavparse->srcpad,
240 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
242 GST_DEBUG_OBJECT (wavparse, "srcpad created");
245 /* Compute (value * nom) % denom, avoiding overflow. This can be used
246 * to perform ceiling or rounding division together with
247 * gst_util_uint64_scale[_int]. */
248 #define uint64_scale_modulo(val, nom, denom) \
249 ((val % denom) * (nom % denom) % denom)
251 /* Like gst_util_uint64_scale, but performs ceiling division. */
253 uint64_ceiling_scale_int (guint64 val, gint num, gint denom)
255 guint64 result = gst_util_uint64_scale_int (val, num, denom);
257 if (uint64_scale_modulo (val, num, denom) == 0)
263 /* Like gst_util_uint64_scale, but performs ceiling division. */
265 uint64_ceiling_scale (guint64 val, guint64 num, guint64 denom)
267 guint64 result = gst_util_uint64_scale (val, num, denom);
269 if (uint64_scale_modulo (val, num, denom) == 0)
276 /* FIXME: why is that not in use? */
279 gst_wavparse_parse_adtl (GstWavParse * wavparse, int len)
282 GstByteStream *bs = wavparse->bs;
283 gst_riff_chunk *temp_chunk, chunk;
285 struct _gst_riff_labl labl, *temp_labl;
286 struct _gst_riff_ltxt ltxt, *temp_ltxt;
287 struct _gst_riff_note note, *temp_note;
290 GstPropsEntry *entry;
294 props = wavparse->metadata->properties;
298 gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
299 if (got_bytes != sizeof (gst_riff_chunk)) {
302 temp_chunk = (gst_riff_chunk *) tempdata;
304 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
305 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
307 if (chunk.size == 0) {
308 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
309 len -= sizeof (gst_riff_chunk);
314 case GST_RIFF_adtl_labl:
316 gst_bytestream_peek_bytes (bs, &tempdata,
317 sizeof (struct _gst_riff_labl));
318 if (got_bytes != sizeof (struct _gst_riff_labl)) {
322 temp_labl = (struct _gst_riff_labl *) tempdata;
323 labl.id = GUINT32_FROM_LE (temp_labl->id);
324 labl.size = GUINT32_FROM_LE (temp_labl->size);
325 labl.identifier = GUINT32_FROM_LE (temp_labl->identifier);
327 gst_bytestream_flush (bs, sizeof (struct _gst_riff_labl));
328 len -= sizeof (struct _gst_riff_labl);
330 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, labl.size - 4);
331 if (got_bytes != labl.size - 4) {
335 label_name = (char *) tempdata;
337 gst_bytestream_flush (bs, ((labl.size - 4) + 1) & ~1);
338 len -= (((labl.size - 4) + 1) & ~1);
340 new_caps = gst_caps_new ("label",
341 "application/x-gst-metadata",
342 gst_props_new ("identifier", G_TYPE_INT (labl.identifier),
343 "name", G_TYPE_STRING (label_name), NULL));
345 if (gst_props_get (props, "labels", &caps, NULL)) {
346 caps = g_list_append (caps, new_caps);
348 caps = g_list_append (NULL, new_caps);
350 entry = gst_props_entry_new ("labels", GST_PROPS_GLIST (caps));
351 gst_props_add_entry (props, entry);
356 case GST_RIFF_adtl_ltxt:
358 gst_bytestream_peek_bytes (bs, &tempdata,
359 sizeof (struct _gst_riff_ltxt));
360 if (got_bytes != sizeof (struct _gst_riff_ltxt)) {
364 temp_ltxt = (struct _gst_riff_ltxt *) tempdata;
365 ltxt.id = GUINT32_FROM_LE (temp_ltxt->id);
366 ltxt.size = GUINT32_FROM_LE (temp_ltxt->size);
367 ltxt.identifier = GUINT32_FROM_LE (temp_ltxt->identifier);
368 ltxt.length = GUINT32_FROM_LE (temp_ltxt->length);
369 ltxt.purpose = GUINT32_FROM_LE (temp_ltxt->purpose);
370 ltxt.country = GUINT16_FROM_LE (temp_ltxt->country);
371 ltxt.language = GUINT16_FROM_LE (temp_ltxt->language);
372 ltxt.dialect = GUINT16_FROM_LE (temp_ltxt->dialect);
373 ltxt.codepage = GUINT16_FROM_LE (temp_ltxt->codepage);
375 gst_bytestream_flush (bs, sizeof (struct _gst_riff_ltxt));
376 len -= sizeof (struct _gst_riff_ltxt);
378 if (ltxt.size - 20 > 0) {
379 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, ltxt.size - 20);
380 if (got_bytes != ltxt.size - 20) {
384 gst_bytestream_flush (bs, ((ltxt.size - 20) + 1) & ~1);
385 len -= (((ltxt.size - 20) + 1) & ~1);
387 label_name = (char *) tempdata;
392 new_caps = gst_caps_new ("ltxt",
393 "application/x-gst-metadata",
394 gst_props_new ("identifier", G_TYPE_INT (ltxt.identifier),
395 "name", G_TYPE_STRING (label_name),
396 "length", G_TYPE_INT (ltxt.length), NULL));
398 if (gst_props_get (props, "ltxts", &caps, NULL)) {
399 caps = g_list_append (caps, new_caps);
401 caps = g_list_append (NULL, new_caps);
403 entry = gst_props_entry_new ("ltxts", GST_PROPS_GLIST (caps));
404 gst_props_add_entry (props, entry);
409 case GST_RIFF_adtl_note:
411 gst_bytestream_peek_bytes (bs, &tempdata,
412 sizeof (struct _gst_riff_note));
413 if (got_bytes != sizeof (struct _gst_riff_note)) {
417 temp_note = (struct _gst_riff_note *) tempdata;
418 note.id = GUINT32_FROM_LE (temp_note->id);
419 note.size = GUINT32_FROM_LE (temp_note->size);
420 note.identifier = GUINT32_FROM_LE (temp_note->identifier);
422 gst_bytestream_flush (bs, sizeof (struct _gst_riff_note));
423 len -= sizeof (struct _gst_riff_note);
425 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, note.size - 4);
426 if (got_bytes != note.size - 4) {
430 gst_bytestream_flush (bs, ((note.size - 4) + 1) & ~1);
431 len -= (((note.size - 4) + 1) & ~1);
433 label_name = (char *) tempdata;
435 new_caps = gst_caps_new ("note",
436 "application/x-gst-metadata",
437 gst_props_new ("identifier", G_TYPE_INT (note.identifier),
438 "name", G_TYPE_STRING (label_name), NULL));
440 if (gst_props_get (props, "notes", &caps, NULL)) {
441 caps = g_list_append (caps, new_caps);
443 caps = g_list_append (NULL, new_caps);
445 entry = gst_props_entry_new ("notes", GST_PROPS_GLIST (caps));
446 gst_props_add_entry (props, entry);
452 g_print ("Unknown chunk: %" GST_FOURCC_FORMAT "\n",
453 GST_FOURCC_ARGS (chunk.id));
458 g_object_notify (G_OBJECT (wavparse), "metadata");
462 gst_wavparse_parse_cues (GstWavParse * wavparse, int len)
465 GstByteStream *bs = wavparse->bs;
466 struct _gst_riff_cue *temp_cue, cue;
467 struct _gst_riff_cuepoints *points;
471 GstPropsEntry *entry;
477 gst_bytestream_peek_bytes (bs, &tempdata,
478 sizeof (struct _gst_riff_cue));
479 temp_cue = (struct _gst_riff_cue *) tempdata;
481 /* fixup for our big endian friends */
482 cue.id = GUINT32_FROM_LE (temp_cue->id);
483 cue.size = GUINT32_FROM_LE (temp_cue->size);
484 cue.cuepoints = GUINT32_FROM_LE (temp_cue->cuepoints);
486 gst_bytestream_flush (bs, sizeof (struct _gst_riff_cue));
487 if (got_bytes != sizeof (struct _gst_riff_cue)) {
491 len -= sizeof (struct _gst_riff_cue);
493 /* -4 because cue.size contains the cuepoints size
494 and we've already flushed that out of the system */
495 required = cue.size - 4;
496 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, required);
497 gst_bytestream_flush (bs, ((required) + 1) & ~1);
498 if (got_bytes != required) {
502 len -= (((cue.size - 4) + 1) & ~1);
504 /* now we have an array of struct _gst_riff_cuepoints in tempdata */
505 points = (struct _gst_riff_cuepoints *) tempdata;
507 for (i = 0; i < cue.cuepoints; i++) {
510 caps = gst_caps_new ("cues",
511 "application/x-gst-metadata",
512 gst_props_new ("identifier", G_TYPE_INT (points[i].identifier),
513 "position", G_TYPE_INT (points[i].offset), NULL));
514 cues = g_list_append (cues, caps);
517 entry = gst_props_entry_new ("cues", GST_PROPS_GLIST (cues));
518 gst_props_add_entry (wavparse->metadata->properties, entry);
521 g_object_notify (G_OBJECT (wavparse), "metadata");
524 /* Read 'fmt ' header */
526 gst_wavparse_fmt (GstWavParse * wav)
528 gst_riff_strf_auds *header = NULL;
531 if (!gst_riff_read_strf_auds (wav, &header))
534 wav->format = header->format;
535 wav->rate = header->rate;
536 wav->channels = header->channels;
537 if (wav->channels == 0)
540 wav->blockalign = header->blockalign;
541 wav->width = (header->blockalign * 8) / header->channels;
542 wav->depth = header->size;
543 wav->bps = header->av_bps;
547 /* Note: gst_riff_create_audio_caps might need to fix values in
548 * the header header depending on the format, so call it first */
549 caps = gst_riff_create_audio_caps (header->format, NULL, header, NULL);
555 gst_wavparse_create_sourcepad (wav);
556 gst_pad_use_fixed_caps (wav->srcpad);
557 gst_pad_set_active (wav->srcpad, TRUE);
558 gst_pad_set_caps (wav->srcpad, caps);
559 gst_caps_free (caps);
560 gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
561 gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
563 GST_DEBUG ("frequency %d, channels %d", wav->rate, wav->channels);
570 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
571 ("No FMT tag found"));
576 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
577 ("Stream claims to contain zero channels - invalid data"));
583 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
584 ("Stream claims to bitrate of <= zero - invalid data"));
590 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
596 gst_wavparse_other (GstWavParse * wav)
600 if (!gst_riff_peek_head (wav, &tag, &length, NULL)) {
601 GST_WARNING_OBJECT (wav, "could not peek head");
604 GST_DEBUG_OBJECT (wav, "got tag (%08x) %4.4s, length %d", tag,
605 (gchar *) & tag, length);
608 case GST_RIFF_TAG_LIST:
609 if (!(tag = gst_riff_peek_list (wav))) {
610 GST_WARNING_OBJECT (wav, "could not peek list");
615 case GST_RIFF_LIST_INFO:
616 if (!gst_riff_read_list (wav, &tag) || !gst_riff_read_info (wav)) {
617 GST_WARNING_OBJECT (wav, "could not read list");
622 case GST_RIFF_LIST_adtl:
623 if (!gst_riff_read_skip (wav)) {
624 GST_WARNING_OBJECT (wav, "could not read skip");
630 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag,
632 if (!gst_riff_read_skip (wav)) {
633 GST_WARNING_OBJECT (wav, "could not read skip");
641 case GST_RIFF_TAG_data:
642 if (!gst_bytestream_flush (wav->bs, 8)) {
643 GST_WARNING_OBJECT (wav, "could not flush 8 bytes");
647 GST_DEBUG_OBJECT (wav, "switching to data mode");
648 wav->state = GST_WAVPARSE_DATA;
649 wav->datastart = gst_bytestream_tell (wav->bs);
653 /* length is 0, data probably stretches to the end
655 GST_DEBUG_OBJECT (wav, "length is 0 trying to find length");
656 /* get length of file */
657 file_length = gst_bytestream_length (wav->bs);
658 if (file_length == -1) {
659 GST_DEBUG_OBJECT (wav,
660 "could not get file length, assuming data to eof");
661 /* could not get length, assuming till eof */
662 length = G_MAXUINT32;
664 if (file_length > G_MAXUINT32) {
665 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
666 ", clipping to 32 bits", file_length);
667 /* could not get length, assuming till eof */
668 length = G_MAXUINT32;
670 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
671 ", datalength %u", file_length, length);
672 /* substract offset of datastart from length */
673 length = file_length - wav->datastart;
674 GST_DEBUG_OBJECT (wav, "datalength %u", length);
677 wav->datasize = (guint64) length;
678 GST_DEBUG_OBJECT (wav, "datasize = %ld", length)
681 case GST_RIFF_TAG_cue:
682 if (!gst_riff_read_skip (wav)) {
683 GST_WARNING_OBJECT (wav, "could not read skip");
689 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag, (gchar *) & tag);
690 if (!gst_riff_read_skip (wav))
701 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
705 if (!gst_riff_parse_file_header (element, buf, &doctype))
708 if (doctype != GST_RIFF_RIFF_WAVE)
716 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
717 ("File is not a WAVE file: %" GST_FOURCC_FORMAT,
718 GST_FOURCC_ARGS (doctype)));
724 gst_wavparse_stream_init (GstWavParse * wav)
727 GstBuffer *buf = NULL;
729 if ((res = gst_pad_pull_range (wav->sinkpad,
730 wav->offset, 12, &buf)) != GST_FLOW_OK)
732 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
733 return GST_FLOW_ERROR;
741 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
743 /* -1 always maps to -1 */
749 /* 0 always maps to 0 */
756 *bytepos = uint64_ceiling_scale (ts, (guint64) wav->bps, GST_SECOND);
758 } else if (wav->fact) {
760 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
761 *bytepos = uint64_ceiling_scale (ts, bps, GST_SECOND);
768 /* This function is used to perform seeks on the element.
770 * It also works when event is NULL, in which case it will just
771 * start from the last configured segment. This technique is
772 * used when activating the element and to perform the seek in
776 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
780 GstFormat format, bformat;
782 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
783 gint64 cur, stop, upstream_size;
786 GstSegment seeksegment = { 0, };
790 GST_DEBUG_OBJECT (wav, "doing seek with event");
792 gst_event_parse_seek (event, &rate, &format, &flags,
793 &cur_type, &cur, &stop_type, &stop);
795 /* no negative rates yet */
799 if (format != wav->segment.format) {
800 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
801 gst_format_get_name (format),
802 gst_format_get_name (wav->segment.format));
804 if (cur_type != GST_SEEK_TYPE_NONE)
806 gst_pad_query_convert (wav->srcpad, format, cur,
807 &wav->segment.format, &cur);
808 if (res && stop_type != GST_SEEK_TYPE_NONE)
810 gst_pad_query_convert (wav->srcpad, format, stop,
811 &wav->segment.format, &stop);
815 format = wav->segment.format;
818 GST_DEBUG_OBJECT (wav, "doing seek without event");
821 cur_type = GST_SEEK_TYPE_SET;
822 stop_type = GST_SEEK_TYPE_SET;
825 /* in push mode, we must delegate to upstream */
826 if (wav->streaming) {
827 gboolean res = FALSE;
829 /* if streaming not yet started; only prepare initial newsegment */
830 if (!event || wav->state != GST_WAVPARSE_DATA) {
831 if (wav->start_segment)
832 gst_event_unref (wav->start_segment);
834 gst_event_new_new_segment (FALSE, wav->segment.rate,
835 wav->segment.format, wav->segment.last_stop, wav->segment.duration,
836 wav->segment.last_stop);
839 /* convert seek positions to byte positions in data sections */
840 if (format == GST_FORMAT_TIME) {
841 /* should not fail */
842 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
844 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
847 /* mind sample boundary and header */
849 cur -= (cur % wav->bytes_per_sample);
850 cur += wav->datastart;
853 stop -= (stop % wav->bytes_per_sample);
854 stop += wav->datastart;
856 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
857 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
859 /* BYTE seek event */
860 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
862 res = gst_pad_push_event (wav->sinkpad, event);
868 flush = flags & GST_SEEK_FLAG_FLUSH;
870 /* now we need to make sure the streaming thread is stopped. We do this by
871 * either sending a FLUSH_START event downstream which will cause the
872 * streaming thread to stop with a WRONG_STATE.
873 * For a non-flushing seek we simply pause the task, which will happen as soon
874 * as it completes one iteration (and thus might block when the sink is
875 * blocking in preroll). */
878 GST_DEBUG_OBJECT (wav, "sending flush start");
879 gst_pad_push_event (wav->srcpad, gst_event_new_flush_start ());
882 gst_pad_pause_task (wav->sinkpad);
885 /* we should now be able to grab the streaming thread because we stopped it
886 * with the above flush/pause code */
887 GST_PAD_STREAM_LOCK (wav->sinkpad);
889 /* save current position */
890 last_stop = wav->segment.last_stop;
892 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
894 /* copy segment, we need this because we still need the old
895 * segment when we close the current segment. */
896 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
898 /* configure the seek parameters in the seeksegment. We will then have the
899 * right values in the segment to perform the seek */
901 GST_DEBUG_OBJECT (wav, "configuring seek");
902 gst_segment_set_seek (&seeksegment, rate, format, flags,
903 cur_type, cur, stop_type, stop, &update);
906 /* figure out the last position we need to play. If it's configured (stop !=
907 * -1), use that, else we play until the total duration of the file */
908 if ((stop = seeksegment.stop) == -1)
909 stop = seeksegment.duration;
911 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
912 if ((cur_type != GST_SEEK_TYPE_NONE)) {
913 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
914 * we can just copy the last_stop. If not, we use the bps to convert TIME to
916 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.last_stop,
917 (gint64 *) & wav->offset))
918 wav->offset = seeksegment.last_stop;
919 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
920 wav->offset -= (wav->offset % wav->bytes_per_sample);
921 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
922 wav->offset += wav->datastart;
923 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
925 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
929 if (stop_type != GST_SEEK_TYPE_NONE) {
930 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
931 wav->end_offset = stop;
932 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
933 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
934 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
935 wav->end_offset += wav->datastart;
936 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
938 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
942 /* make sure filesize is not exceeded due to rounding errors or so,
943 * same precaution as in _stream_headers */
944 bformat = GST_FORMAT_BYTES;
945 if (gst_pad_query_peer_duration (wav->sinkpad, &bformat, &upstream_size))
946 wav->end_offset = MIN (wav->end_offset, upstream_size);
948 /* this is the range of bytes we will use for playback */
949 wav->offset = MIN (wav->offset, wav->end_offset);
950 wav->dataleft = wav->end_offset - wav->offset;
952 GST_DEBUG_OBJECT (wav,
953 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
954 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
955 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
957 /* prepare for streaming again */
960 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
961 GST_DEBUG_OBJECT (wav, "sending flush stop");
962 gst_pad_push_event (wav->srcpad, gst_event_new_flush_stop ());
963 } else if (wav->segment_running) {
964 /* we are running the current segment and doing a non-flushing seek,
965 * close the segment first based on the previous last_stop. */
966 GST_DEBUG_OBJECT (wav, "closing running segment %" G_GINT64_FORMAT
967 " to %" G_GINT64_FORMAT, wav->segment.start, wav->segment.last_stop);
969 /* queue the segment for sending in the stream thread */
970 if (wav->close_segment)
971 gst_event_unref (wav->close_segment);
972 wav->close_segment = gst_event_new_new_segment (TRUE,
973 wav->segment.rate, wav->segment.format,
974 wav->segment.start, wav->segment.last_stop, wav->segment.start);
978 /* now we did the seek and can activate the new segment values */
979 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
981 /* if we're doing a segment seek, post a SEGMENT_START message */
982 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
983 gst_element_post_message (GST_ELEMENT_CAST (wav),
984 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
985 wav->segment.format, wav->segment.last_stop));
988 /* now create the newsegment */
989 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
990 " to %" G_GINT64_FORMAT, wav->segment.last_stop, stop);
992 /* store the newsegment event so it can be sent from the streaming thread. */
993 if (wav->start_segment)
994 gst_event_unref (wav->start_segment);
996 gst_event_new_new_segment (FALSE, wav->segment.rate,
997 wav->segment.format, wav->segment.last_stop, stop,
998 wav->segment.last_stop);
1000 /* mark discont if we are going to stream from another position. */
1001 if (last_stop != wav->segment.last_stop) {
1002 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
1003 wav->discont = TRUE;
1006 /* and start the streaming task again */
1007 wav->segment_running = TRUE;
1008 if (!wav->streaming) {
1009 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
1013 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
1020 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
1025 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
1030 GST_DEBUG_OBJECT (wav,
1031 "Could not determine byte position for desired time");
1037 * gst_wavparse_peek_chunk_info:
1038 * @wav Wavparse object
1039 * @tag holder for tag
1040 * @size holder for tag size
1042 * Peek next chunk info (tag and size)
1044 * Returns: %TRUE when the chunk info (header) is available
1047 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
1049 const guint8 *data = NULL;
1051 if (gst_adapter_available (wav->adapter) < 8)
1054 data = gst_adapter_peek (wav->adapter, 8);
1055 *tag = GST_READ_UINT32_LE (data);
1056 *size = GST_READ_UINT32_LE (data + 4);
1058 GST_DEBUG ("Next chunk size is %d bytes, type %" GST_FOURCC_FORMAT, *size,
1059 GST_FOURCC_ARGS (*tag));
1065 * gst_wavparse_peek_chunk:
1066 * @wav Wavparse object
1067 * @tag holder for tag
1068 * @size holder for tag size
1070 * Peek enough data for one full chunk
1072 * Returns: %TRUE when the full chunk is available
1075 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
1077 guint32 peek_size = 0;
1080 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
1083 /* size 0 -> empty data buffer would surprise most callers,
1084 * large size -> do not bother trying to squeeze that into adapter,
1085 * so we throw poor man's exception, which can be caught if caller really
1086 * wants to handle 0 size chunk */
1087 if (!(*size) || (*size) >= (1 << 30)) {
1088 GST_INFO ("Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT,
1089 *size, GST_FOURCC_ARGS (*tag));
1090 /* chain should give up */
1091 wav->abort_buffering = TRUE;
1094 peek_size = (*size + 1) & ~1;
1095 available = gst_adapter_available (wav->adapter);
1097 if (available >= (8 + peek_size)) {
1100 GST_LOG ("but only %u bytes available now", available);
1106 * gst_wavparse_calculate_duration:
1107 * @wav: wavparse object
1109 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
1112 * Returns: %TRUE if duration is available.
1115 gst_wavparse_calculate_duration (GstWavParse * wav)
1117 if (wav->duration > 0)
1121 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
1123 uint64_ceiling_scale (wav->datasize, GST_SECOND, (guint64) wav->bps);
1124 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
1125 GST_TIME_ARGS (wav->duration));
1127 } else if (wav->fact) {
1128 wav->duration = uint64_ceiling_scale_int (GST_SECOND, wav->fact, wav->rate);
1129 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
1130 GST_TIME_ARGS (wav->duration));
1137 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
1142 if (wav->streaming) {
1143 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1146 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
1147 GST_FOURCC_ARGS (tag));
1148 flush = 8 + ((size + 1) & ~1);
1149 wav->offset += flush;
1150 if (wav->streaming) {
1151 gst_adapter_flush (wav->adapter, flush);
1153 gst_buffer_unref (buf);
1159 #define MAX_BUFFER_SIZE 4096
1161 static GstFlowReturn
1162 gst_wavparse_stream_headers (GstWavParse * wav)
1164 GstFlowReturn res = GST_FLOW_OK;
1165 GstBuffer *buf = NULL;
1166 gst_riff_strf_auds *header = NULL;
1168 gboolean gotdata = FALSE;
1169 GstCaps *caps = NULL;
1170 gchar *codec_name = NULL;
1173 gint64 upstream_size = 0;
1175 /* search for "_fmt" chunk, which should be first */
1176 while (!wav->got_fmt) {
1179 /* The header starts with a 'fmt ' tag */
1180 if (wav->streaming) {
1181 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1184 gst_adapter_flush (wav->adapter, 8);
1188 buf = gst_adapter_take_buffer (wav->adapter, size);
1190 gst_adapter_flush (wav->adapter, 1);
1191 wav->offset += GST_ROUND_UP_2 (size);
1193 buf = gst_buffer_new ();
1196 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1197 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1201 if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ ||
1202 tag == GST_RIFF_TAG_bext || tag == GST_RIFF_TAG_BEXT ||
1203 tag == GST_RIFF_TAG_LIST) {
1204 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1205 GST_FOURCC_ARGS (tag));
1206 gst_buffer_unref (buf);
1211 if (tag != GST_RIFF_TAG_fmt)
1214 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1216 goto parse_header_error;
1218 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1220 /* do sanity checks of header fields */
1221 if (header->channels == 0)
1223 if (header->rate == 0)
1226 GST_DEBUG_OBJECT (wav, "creating the caps");
1228 /* Note: gst_riff_create_audio_caps might need to fix values in
1229 * the header header depending on the format, so call it first */
1230 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1234 gst_buffer_unref (extra);
1237 goto unknown_format;
1239 /* do more sanity checks of header fields
1240 * (these can be sanitized by gst_riff_create_audio_caps()
1242 wav->format = header->format;
1243 wav->rate = header->rate;
1244 wav->channels = header->channels;
1245 wav->blockalign = header->blockalign;
1246 wav->depth = header->size;
1247 wav->av_bps = header->av_bps;
1253 /* do format specific handling */
1254 switch (wav->format) {
1255 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1256 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1258 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1259 * bitrate inside the mpeg stream */
1260 GST_INFO ("resetting bps from %d to 0 for mp2/3", wav->av_bps);
1264 case GST_RIFF_WAVE_FORMAT_PCM:
1265 if (wav->blockalign > wav->channels * (guint) ceil (wav->depth / 8.0))
1266 goto invalid_blockalign;
1269 if (wav->av_bps > wav->blockalign * wav->rate)
1271 /* use the configured bps */
1272 wav->bps = wav->av_bps;
1276 wav->width = (wav->blockalign * 8) / wav->channels;
1277 wav->bytes_per_sample = wav->channels * wav->width / 8;
1279 if (wav->bytes_per_sample <= 0)
1280 goto no_bytes_per_sample;
1282 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1283 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1284 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1285 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1286 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1287 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1288 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1290 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1291 * formats). This will make the element output a BYTE format segment and
1292 * will not timestamp the outgoing buffers.
1294 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1296 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1298 /* create pad later so we can sniff the first few bytes
1299 * of the real data and correct our caps if necessary */
1300 gst_caps_replace (&wav->caps, caps);
1301 gst_caps_replace (&caps, NULL);
1303 wav->got_fmt = TRUE;
1306 wav->tags = gst_tag_list_new ();
1308 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1309 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1311 g_free (codec_name);
1317 bformat = GST_FORMAT_BYTES;
1318 gst_pad_query_peer_duration (wav->sinkpad, &bformat, &upstream_size);
1319 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1321 /* loop headers until we get data */
1323 if (wav->streaming) {
1324 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1328 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1329 &buf)) != GST_FLOW_OK)
1330 goto header_read_error;
1331 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
1332 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
1335 GST_INFO_OBJECT (wav,
1336 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT,
1337 GST_FOURCC_ARGS (tag), wav->offset);
1339 /* wav is a st00pid format, we don't know for sure where data starts.
1340 * So we have to go bit by bit until we find the 'data' header
1343 case GST_RIFF_TAG_data:{
1344 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %d", size);
1345 if (wav->streaming) {
1346 gst_adapter_flush (wav->adapter, 8);
1349 gst_buffer_unref (buf);
1352 wav->datastart = wav->offset;
1353 /* If size is zero, then the data chunk probably actually extends to
1354 the end of the file */
1355 if (size == 0 && upstream_size) {
1356 size = upstream_size - wav->datastart;
1358 /* Or the file might be truncated */
1359 else if (upstream_size) {
1360 size = MIN (size, (upstream_size - wav->datastart));
1362 wav->datasize = (guint64) size;
1363 wav->dataleft = (guint64) size;
1364 wav->end_offset = size + wav->datastart;
1365 if (!wav->streaming) {
1366 /* We will continue parsing tags 'till end */
1367 wav->offset += size;
1369 GST_DEBUG_OBJECT (wav, "datasize = %d", size);
1372 case GST_RIFF_TAG_fact:{
1373 if (wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1374 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1375 const guint data_size = 4;
1377 GST_INFO_OBJECT (wav, "Have fact chunk");
1378 if (size < data_size) {
1379 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1380 /* need more data */
1383 GST_DEBUG_OBJECT (wav, "need %d, available %d; ignoring chunk",
1387 /* number of samples (for compressed formats) */
1388 if (wav->streaming) {
1389 const guint8 *data = NULL;
1391 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1394 gst_adapter_flush (wav->adapter, 8);
1395 data = gst_adapter_peek (wav->adapter, data_size);
1396 wav->fact = GST_READ_UINT32_LE (data);
1397 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1399 gst_buffer_unref (buf);
1401 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1402 data_size, &buf)) != GST_FLOW_OK)
1403 goto header_read_error;
1404 wav->fact = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
1405 gst_buffer_unref (buf);
1407 GST_DEBUG_OBJECT (wav, "have fact %u", wav->fact);
1408 wav->offset += 8 + GST_ROUND_UP_2 (size);
1411 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1412 /* need more data */
1418 case GST_RIFF_TAG_acid:{
1419 const gst_riff_acid *acid = NULL;
1420 const guint data_size = sizeof (gst_riff_acid);
1422 GST_INFO_OBJECT (wav, "Have acid chunk");
1423 if (size < data_size) {
1424 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1425 /* need more data */
1428 GST_DEBUG_OBJECT (wav, "need %d, available %d; ignoring chunk",
1432 if (wav->streaming) {
1433 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1436 gst_adapter_flush (wav->adapter, 8);
1437 acid = (const gst_riff_acid *) gst_adapter_peek (wav->adapter,
1440 gst_buffer_unref (buf);
1442 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1443 size, &buf)) != GST_FLOW_OK)
1444 goto header_read_error;
1445 acid = (const gst_riff_acid *) GST_BUFFER_DATA (buf);
1447 /* send data as tags */
1449 wav->tags = gst_tag_list_new ();
1450 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1451 GST_TAG_BEATS_PER_MINUTE, acid->tempo, NULL);
1453 size = GST_ROUND_UP_2 (size);
1454 if (wav->streaming) {
1455 gst_adapter_flush (wav->adapter, size);
1457 gst_buffer_unref (buf);
1459 wav->offset += 8 + size;
1462 /* FIXME: all list tags after data are ignored in streaming mode */
1463 case GST_RIFF_TAG_LIST:{
1466 if (wav->streaming) {
1467 const guint8 *data = NULL;
1469 if (gst_adapter_available (wav->adapter) < 12) {
1472 data = gst_adapter_peek (wav->adapter, 12);
1473 ltag = GST_READ_UINT32_LE (data + 8);
1475 gst_buffer_unref (buf);
1477 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1478 &buf)) != GST_FLOW_OK)
1479 goto header_read_error;
1480 ltag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 8);
1483 case GST_RIFF_LIST_INFO:{
1484 const gint data_size = size - 4;
1487 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1488 if (wav->streaming) {
1489 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1492 gst_adapter_flush (wav->adapter, 12);
1494 if (data_size > 0) {
1495 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1497 gst_adapter_flush (wav->adapter, 1);
1501 gst_buffer_unref (buf);
1502 if (data_size > 0) {
1504 gst_pad_pull_range (wav->sinkpad, wav->offset,
1505 data_size, &buf)) != GST_FLOW_OK)
1506 goto header_read_error;
1509 if (data_size > 0) {
1511 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1513 GstTagList *old = wav->tags;
1515 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1517 gst_tag_list_free (old);
1518 gst_tag_list_free (new);
1520 gst_buffer_unref (buf);
1521 wav->offset += GST_ROUND_UP_2 (data_size);
1526 GST_INFO_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1527 GST_FOURCC_ARGS (ltag));
1528 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1529 /* need more data */
1536 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1537 /* need more data */
1542 if (upstream_size && (wav->offset >= upstream_size)) {
1543 /* Now we are gone through the whole file */
1548 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1550 if (wav->bps <= 0 && wav->fact) {
1552 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1554 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1555 (guint64) wav->fact);
1556 GST_INFO_OBJECT (wav, "calculated bps : %d, enabling VBR", wav->bps);
1561 if (gst_wavparse_calculate_duration (wav)) {
1562 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1563 gst_segment_set_duration (&wav->segment, GST_FORMAT_TIME, wav->duration);
1565 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1566 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1567 gst_segment_set_duration (&wav->segment, GST_FORMAT_BYTES, wav->datasize);
1570 /* now we have all the info to perform a pending seek if any, if no
1571 * event, this will still do the right thing and it will also send
1572 * the right newsegment event downstream. */
1573 gst_wavparse_perform_seek (wav, wav->seek_event);
1574 /* remove pending event */
1575 event_p = &wav->seek_event;
1576 gst_event_replace (event_p, NULL);
1578 /* we just started, we are discont */
1579 wav->discont = TRUE;
1581 wav->state = GST_WAVPARSE_DATA;
1583 /* determine reasonable max buffer size,
1584 * that is, buffers not too small either size or time wise
1585 * so we do not end up with too many of them */
1588 gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size);
1589 wav->max_buf_size = upstream_size;
1590 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1591 if (wav->blockalign > 0)
1592 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1594 GST_DEBUG_OBJECT (wav, "max buffer size %d", wav->max_buf_size);
1602 g_free (codec_name);
1606 gst_caps_unref (caps);
1611 res = GST_FLOW_ERROR;
1616 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1617 ("Invalid WAV header (no fmt at start): %"
1618 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1623 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1624 ("Couldn't parse audio header"));
1629 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1630 ("Stream claims to contain no channels - invalid data"));
1635 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1636 ("Stream with sample_rate == 0 - invalid data"));
1641 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1642 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1643 wav->blockalign, wav->channels * (guint) ceil (wav->depth / 8.0)));
1648 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1649 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1650 wav->av_bps, wav->blockalign * wav->rate));
1653 no_bytes_per_sample:
1655 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1656 ("Could not caluclate bytes per sample - invalid data"));
1661 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1662 ("No caps found for format 0x%x, %d channels, %d Hz",
1663 wav->format, wav->channels, wav->rate));
1668 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1669 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1675 * Read WAV file tag when streaming
1677 static GstFlowReturn
1678 gst_wavparse_parse_stream_init (GstWavParse * wav)
1680 if (gst_adapter_available (wav->adapter) >= 12) {
1683 /* _take flushes the data */
1684 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1686 GST_DEBUG ("Parsing wav header");
1687 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1688 return GST_FLOW_ERROR;
1691 /* Go to next state */
1692 wav->state = GST_WAVPARSE_HEADER;
1697 /* handle an event sent directly to the element.
1699 * This event can be sent either in the READY state or the
1700 * >READY state. The only event of interest really is the seek
1703 * In the READY state we can only store the event and try to
1704 * respect it when going to PAUSED. We assume we are in the
1705 * READY state when our parsing state != GST_WAVPARSE_DATA.
1707 * When we are steaming, we can simply perform the seek right
1711 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1713 GstWavParse *wav = GST_WAVPARSE (element);
1714 gboolean res = FALSE;
1717 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1719 switch (GST_EVENT_TYPE (event)) {
1720 case GST_EVENT_SEEK:
1721 if (wav->state == GST_WAVPARSE_DATA) {
1722 /* we can handle the seek directly when streaming data */
1723 res = gst_wavparse_perform_seek (wav, event);
1725 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1727 event_p = &wav->seek_event;
1728 gst_event_replace (event_p, event);
1730 /* we always return true */
1737 gst_event_unref (event);
1742 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1746 s = gst_caps_get_structure (caps, 0);
1747 if (!gst_structure_has_name (s, "audio/x-dts"))
1749 if (prob >= GST_TYPE_FIND_LIKELY)
1751 /* DTS at non-0 offsets and without second sync may yield POSSIBLE .. */
1752 if (prob < GST_TYPE_FIND_POSSIBLE)
1754 /* .. in which case we want at least a valid-looking rate and channels */
1755 if (!gst_structure_has_field (s, "channels"))
1757 /* and for extra assurance we could also check the rate from the DTS frame
1758 * against the one in the wav header, but for now let's not do that */
1759 return gst_structure_has_field (s, "rate");
1763 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1767 GST_DEBUG_OBJECT (wav, "adding src pad");
1770 s = gst_caps_get_structure (wav->caps, 0);
1771 if (s && gst_structure_has_name (s, "audio/x-raw-int") && buf != NULL) {
1772 GstTypeFindProbability prob;
1775 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1776 if (tf_caps != NULL) {
1777 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1778 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1779 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1780 gst_caps_unref (wav->caps);
1781 wav->caps = tf_caps;
1783 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1784 GST_TAG_AUDIO_CODEC, "dts", NULL);
1786 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1787 "marked as raw PCM audio, but ignoring for now", tf_caps);
1788 gst_caps_unref (tf_caps);
1794 gst_wavparse_create_sourcepad (wav);
1795 gst_pad_set_active (wav->srcpad, TRUE);
1796 gst_pad_set_caps (wav->srcpad, wav->caps);
1797 gst_caps_replace (&wav->caps, NULL);
1799 gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
1800 gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
1802 if (wav->close_segment) {
1803 GST_DEBUG_OBJECT (wav, "Send close segment event on newpad");
1804 gst_pad_push_event (wav->srcpad, wav->close_segment);
1805 wav->close_segment = NULL;
1807 if (wav->start_segment) {
1808 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1809 gst_pad_push_event (wav->srcpad, wav->start_segment);
1810 wav->start_segment = NULL;
1814 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (wav), wav->srcpad,
1820 static GstFlowReturn
1821 gst_wavparse_stream_data (GstWavParse * wav)
1823 GstBuffer *buf = NULL;
1824 GstFlowReturn res = GST_FLOW_OK;
1825 guint64 desired, obtained;
1826 GstClockTime timestamp, next_timestamp, duration;
1827 guint64 pos, nextpos;
1830 GST_LOG_OBJECT (wav,
1831 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1832 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1834 /* Get the next n bytes and output them */
1835 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1838 /* scale the amount of data by the segment rate so we get equal
1839 * amounts of data regardless of the playback rate */
1841 MIN (gst_guint64_to_gdouble (wav->dataleft),
1842 wav->max_buf_size * wav->segment.abs_rate);
1844 if (desired >= wav->blockalign && wav->blockalign > 0)
1845 desired -= (desired % wav->blockalign);
1847 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1848 "from the sinkpad", desired);
1850 if (wav->streaming) {
1851 guint avail = gst_adapter_available (wav->adapter);
1854 /* flush some bytes if evil upstream sends segment that starts
1855 * before data or does is not send sample aligned segment */
1856 if (G_LIKELY (wav->offset >= wav->datastart)) {
1857 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1859 extra = wav->datastart - wav->offset;
1862 if (G_UNLIKELY (extra)) {
1863 extra = wav->bytes_per_sample - extra;
1864 if (extra <= avail) {
1865 GST_DEBUG_OBJECT (wav, "flushing %d bytes to sample boundary", extra);
1866 gst_adapter_flush (wav->adapter, extra);
1867 wav->offset += extra;
1868 wav->dataleft -= extra;
1869 goto iterate_adapter;
1871 GST_DEBUG_OBJECT (wav, "flushing %d bytes", avail);
1872 gst_adapter_clear (wav->adapter);
1873 wav->offset += avail;
1874 wav->dataleft -= avail;
1879 if (avail < desired) {
1880 GST_LOG_OBJECT (wav, "Got only %d bytes of data from the sinkpad", avail);
1884 buf = gst_adapter_take_buffer (wav->adapter, desired);
1886 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
1887 desired, &buf)) != GST_FLOW_OK)
1890 /* we may get a short buffer at the end of the file */
1891 if (GST_BUFFER_SIZE (buf) < desired) {
1892 GST_LOG_OBJECT (wav, "Got only %u bytes of data", GST_BUFFER_SIZE (buf));
1893 if (GST_BUFFER_SIZE (buf) >= wav->blockalign) {
1894 buf = gst_buffer_make_metadata_writable (buf);
1895 GST_BUFFER_SIZE (buf) -= (GST_BUFFER_SIZE (buf) % wav->blockalign);
1897 gst_buffer_unref (buf);
1903 obtained = GST_BUFFER_SIZE (buf);
1905 /* our positions in bytes */
1906 pos = wav->offset - wav->datastart;
1907 nextpos = pos + obtained;
1909 /* update offsets, does not overflow. */
1910 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
1911 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
1913 /* first chunk of data? create the source pad. We do this only here so
1914 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
1915 if (G_UNLIKELY (wav->first)) {
1917 /* this will also push the segment events */
1918 gst_wavparse_add_src_pad (wav, buf);
1920 /* If we have a pending close/start segment, send it now. */
1921 if (G_UNLIKELY (wav->close_segment != NULL)) {
1922 gst_pad_push_event (wav->srcpad, wav->close_segment);
1923 wav->close_segment = NULL;
1925 if (G_UNLIKELY (wav->start_segment != NULL)) {
1926 gst_pad_push_event (wav->srcpad, wav->start_segment);
1927 wav->start_segment = NULL;
1932 /* and timestamps if we have a bitrate, be careful for overflows */
1933 timestamp = uint64_ceiling_scale (pos, GST_SECOND, (guint64) wav->bps);
1935 uint64_ceiling_scale (nextpos, GST_SECOND, (guint64) wav->bps);
1936 duration = next_timestamp - timestamp;
1938 /* update current running segment position */
1939 if (G_LIKELY (next_timestamp >= wav->segment.start))
1940 gst_segment_set_last_stop (&wav->segment, GST_FORMAT_TIME,
1942 } else if (wav->fact) {
1944 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
1945 /* and timestamps if we have a bitrate, be careful for overflows */
1946 timestamp = uint64_ceiling_scale (pos, GST_SECOND, bps);
1947 next_timestamp = uint64_ceiling_scale (nextpos, GST_SECOND, bps);
1948 duration = next_timestamp - timestamp;
1950 /* no bitrate, all we know is that the first sample has timestamp 0, all
1951 * other positions and durations have unknown timestamp. */
1955 timestamp = GST_CLOCK_TIME_NONE;
1956 duration = GST_CLOCK_TIME_NONE;
1957 /* update current running segment position with byte offset */
1958 if (G_LIKELY (nextpos >= wav->segment.start))
1959 gst_segment_set_last_stop (&wav->segment, GST_FORMAT_BYTES, nextpos);
1961 if ((pos > 0) && wav->vbr) {
1962 /* don't set timestamps for VBR files if it's not the first buffer */
1963 timestamp = GST_CLOCK_TIME_NONE;
1964 duration = GST_CLOCK_TIME_NONE;
1967 GST_DEBUG_OBJECT (wav, "marking DISCONT");
1968 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1969 wav->discont = FALSE;
1972 GST_BUFFER_TIMESTAMP (buf) = timestamp;
1973 GST_BUFFER_DURATION (buf) = duration;
1975 /* don't forget to set the caps on the buffer */
1976 gst_buffer_set_caps (buf, GST_PAD_CAPS (wav->srcpad));
1978 GST_LOG_OBJECT (wav,
1979 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
1980 ", size:%u", GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration),
1981 GST_BUFFER_SIZE (buf));
1983 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
1986 if (obtained < wav->dataleft) {
1987 wav->offset += obtained;
1988 wav->dataleft -= obtained;
1990 wav->offset += wav->dataleft;
1994 /* Iterate until need more data, so adapter size won't grow */
1995 if (wav->streaming) {
1996 GST_LOG_OBJECT (wav,
1997 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
1999 goto iterate_adapter;
2006 GST_DEBUG_OBJECT (wav, "found EOS");
2007 return GST_FLOW_UNEXPECTED;
2011 /* check if we got EOS */
2012 if (res == GST_FLOW_UNEXPECTED)
2015 GST_WARNING_OBJECT (wav,
2016 "Error getting %" G_GINT64_FORMAT " bytes from the "
2017 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
2022 GST_INFO_OBJECT (wav,
2023 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
2024 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
2025 gst_pad_is_linked (wav->srcpad));
2031 gst_wavparse_loop (GstPad * pad)
2034 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2036 GST_LOG_OBJECT (wav, "process data");
2038 switch (wav->state) {
2039 case GST_WAVPARSE_START:
2040 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2041 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2044 wav->state = GST_WAVPARSE_HEADER;
2047 case GST_WAVPARSE_HEADER:
2048 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2049 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2052 wav->state = GST_WAVPARSE_DATA;
2053 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2056 case GST_WAVPARSE_DATA:
2057 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2061 g_assert_not_reached ();
2068 const gchar *reason = gst_flow_get_name (ret);
2070 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2071 wav->segment_running = FALSE;
2072 gst_pad_pause_task (pad);
2074 if (ret == GST_FLOW_UNEXPECTED) {
2075 /* add pad before we perform EOS */
2076 if (G_UNLIKELY (wav->first)) {
2078 gst_wavparse_add_src_pad (wav, NULL);
2081 if (wav->state == GST_WAVPARSE_START)
2082 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2083 ("No valid input found before end of stream"), (NULL));
2085 /* perform EOS logic */
2086 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2089 if ((stop = wav->segment.stop) == -1)
2090 stop = wav->segment.duration;
2092 gst_element_post_message (GST_ELEMENT_CAST (wav),
2093 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2094 wav->segment.format, stop));
2096 if (wav->srcpad != NULL)
2097 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2099 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2100 /* for fatal errors we post an error message, post the error
2101 * first so the app knows about the error first. */
2102 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
2103 (_("Internal data flow error.")),
2104 ("streaming task paused, reason %s (%d)", reason, ret));
2105 if (wav->srcpad != NULL)
2106 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2112 static GstFlowReturn
2113 gst_wavparse_chain (GstPad * pad, GstBuffer * buf)
2116 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2118 GST_LOG_OBJECT (wav, "adapter_push %u bytes", GST_BUFFER_SIZE (buf));
2120 gst_adapter_push (wav->adapter, buf);
2122 switch (wav->state) {
2123 case GST_WAVPARSE_START:
2124 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2125 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2128 if (wav->state != GST_WAVPARSE_HEADER)
2131 /* otherwise fall-through */
2132 case GST_WAVPARSE_HEADER:
2133 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2134 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2137 if (!wav->got_fmt || wav->datastart == 0)
2140 wav->state = GST_WAVPARSE_DATA;
2141 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2144 case GST_WAVPARSE_DATA:
2145 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2146 wav->discont = TRUE;
2147 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2151 g_return_val_if_reached (GST_FLOW_ERROR);
2154 if (G_UNLIKELY (wav->abort_buffering)) {
2155 wav->abort_buffering = FALSE;
2156 ret = GST_FLOW_ERROR;
2157 /* sort of demux/parse error */
2158 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2164 static GstFlowReturn
2165 gst_wavparse_flush_data (GstWavParse * wav)
2167 GstFlowReturn ret = GST_FLOW_OK;
2170 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2172 wav->end_offset = wav->offset + av;
2173 ret = gst_wavparse_stream_data (wav);
2180 gst_wavparse_sink_event (GstPad * pad, GstEvent * event)
2182 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2183 gboolean ret = TRUE;
2185 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2187 switch (GST_EVENT_TYPE (event)) {
2188 case GST_EVENT_NEWSEGMENT:
2191 gdouble rate, arate;
2192 gint64 start, stop, time, offset = 0, end_offset = -1;
2196 /* some debug output */
2197 gst_segment_init (&segment, GST_FORMAT_UNDEFINED);
2198 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
2199 &start, &stop, &time);
2200 gst_segment_set_newsegment_full (&segment, update, rate, arate, format,
2202 GST_DEBUG_OBJECT (wav,
2203 "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
2206 if (wav->state != GST_WAVPARSE_DATA) {
2207 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2211 /* now we are either committed to TIME or BYTE format,
2212 * and we only expect a BYTE segment, e.g. following a seek */
2213 if (format == GST_FORMAT_BYTES) {
2216 start -= wav->datastart;
2217 start = MAX (start, 0);
2221 stop -= wav->datastart;
2222 stop = MAX (stop, 0);
2224 if (wav->segment.format == GST_FORMAT_TIME) {
2225 guint64 bps = wav->bps;
2227 /* operating in format TIME, so we can convert */
2228 if (!bps && wav->fact)
2230 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2234 uint64_ceiling_scale (start, GST_SECOND, (guint64) wav->bps);
2237 uint64_ceiling_scale (stop, GST_SECOND, (guint64) wav->bps);
2241 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2245 /* accept upstream's notion of segment and distribute along */
2246 gst_segment_set_newsegment_full (&wav->segment, update, rate, arate,
2247 wav->segment.format, start, stop, start);
2248 /* also store the newsegment event for the streaming thread */
2249 if (wav->start_segment)
2250 gst_event_unref (wav->start_segment);
2251 wav->start_segment =
2252 gst_event_new_new_segment_full (update, rate, arate,
2253 wav->segment.format, start, stop, start);
2254 GST_DEBUG_OBJECT (wav, "Pushing newseg update %d, rate %g, "
2255 "applied rate %g, format %d, start %" G_GINT64_FORMAT ", "
2256 "stop %" G_GINT64_FORMAT, update, rate, arate, wav->segment.format,
2259 /* stream leftover data in current segment */
2260 gst_wavparse_flush_data (wav);
2261 /* and set up streaming thread for next one */
2262 wav->offset = offset;
2263 wav->end_offset = end_offset;
2264 if (wav->end_offset > 0) {
2265 wav->dataleft = wav->end_offset - wav->offset;
2267 /* infinity; upstream will EOS when done */
2268 wav->dataleft = G_MAXUINT64;
2271 gst_event_unref (event);
2275 /* add pad if needed so EOS is seen downstream */
2276 if (G_UNLIKELY (wav->first)) {
2278 gst_wavparse_add_src_pad (wav, NULL);
2280 /* stream leftover data in current segment */
2281 gst_wavparse_flush_data (wav);
2284 if (wav->state == GST_WAVPARSE_START)
2285 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2286 ("No valid input found before end of stream"), (NULL));
2289 case GST_EVENT_FLUSH_STOP:
2290 gst_adapter_clear (wav->adapter);
2291 wav->discont = TRUE;
2294 ret = gst_pad_event_default (wav->sinkpad, event);
2302 /* convert and query stuff */
2303 static const GstFormat *
2304 gst_wavparse_get_formats (GstPad * pad)
2306 static GstFormat formats[] = {
2309 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2318 gst_wavparse_pad_convert (GstPad * pad,
2319 GstFormat src_format, gint64 src_value,
2320 GstFormat * dest_format, gint64 * dest_value)
2322 GstWavParse *wavparse;
2323 gboolean res = TRUE;
2325 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2327 if (*dest_format == src_format) {
2328 *dest_value = src_value;
2332 if ((wavparse->bps == 0) && !wavparse->fact)
2335 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2336 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2338 switch (src_format) {
2339 case GST_FORMAT_BYTES:
2340 switch (*dest_format) {
2341 case GST_FORMAT_DEFAULT:
2342 *dest_value = src_value / wavparse->bytes_per_sample;
2343 /* make sure we end up on a sample boundary */
2344 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2346 case GST_FORMAT_TIME:
2347 /* src_value + datastart = offset */
2348 GST_INFO_OBJECT (wavparse,
2349 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2351 if (wavparse->bps > 0)
2352 *dest_value = uint64_ceiling_scale (src_value, GST_SECOND,
2353 (guint64) wavparse->bps);
2354 else if (wavparse->fact) {
2355 guint64 bps = uint64_ceiling_scale_int (wavparse->datasize,
2356 wavparse->rate, wavparse->fact);
2358 *dest_value = uint64_ceiling_scale_int (src_value, GST_SECOND, bps);
2369 case GST_FORMAT_DEFAULT:
2370 switch (*dest_format) {
2371 case GST_FORMAT_BYTES:
2372 *dest_value = src_value * wavparse->bytes_per_sample;
2374 case GST_FORMAT_TIME:
2375 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2376 (guint64) wavparse->rate);
2384 case GST_FORMAT_TIME:
2385 switch (*dest_format) {
2386 case GST_FORMAT_BYTES:
2387 if (wavparse->bps > 0)
2388 *dest_value = gst_util_uint64_scale (src_value,
2389 (guint64) wavparse->bps, GST_SECOND);
2391 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2392 wavparse->rate, wavparse->fact);
2394 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2396 /* make sure we end up on a sample boundary */
2397 *dest_value -= *dest_value % wavparse->blockalign;
2399 case GST_FORMAT_DEFAULT:
2400 *dest_value = gst_util_uint64_scale (src_value,
2401 (guint64) wavparse->rate, GST_SECOND);
2420 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2426 static const GstQueryType *
2427 gst_wavparse_get_query_types (GstPad * pad)
2429 static const GstQueryType types[] = {
2440 /* handle queries for location and length in requested format */
2442 gst_wavparse_pad_query (GstPad * pad, GstQuery * query)
2444 gboolean res = TRUE;
2445 GstWavParse *wav = GST_WAVPARSE (gst_pad_get_parent (pad));
2447 /* only if we know */
2448 if (wav->state != GST_WAVPARSE_DATA) {
2449 gst_object_unref (wav);
2453 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2455 switch (GST_QUERY_TYPE (query)) {
2456 case GST_QUERY_POSITION:
2462 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2463 curb = wav->offset - wav->datastart;
2464 gst_query_parse_position (query, &format, NULL);
2465 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2468 case GST_FORMAT_TIME:
2469 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2473 format = GST_FORMAT_BYTES;
2478 gst_query_set_position (query, format, cur);
2481 case GST_QUERY_DURATION:
2483 gint64 duration = 0;
2486 gst_query_parse_duration (query, &format, NULL);
2489 case GST_FORMAT_TIME:{
2490 if ((res = gst_wavparse_calculate_duration (wav))) {
2491 duration = wav->duration;
2496 format = GST_FORMAT_BYTES;
2497 duration = wav->datasize;
2500 gst_query_set_duration (query, format, duration);
2503 case GST_QUERY_CONVERT:
2505 gint64 srcvalue, dstvalue;
2506 GstFormat srcformat, dstformat;
2508 gst_query_parse_convert (query, &srcformat, &srcvalue,
2509 &dstformat, &dstvalue);
2510 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2511 &dstformat, &dstvalue);
2513 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2516 case GST_QUERY_SEEKING:{
2518 gboolean seekable = FALSE;
2520 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2521 if (fmt == wav->segment.format) {
2522 if (wav->streaming) {
2525 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2526 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2527 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2528 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2530 gst_query_unref (q);
2532 GST_LOG_OBJECT (wav, "looping => seekable");
2536 } else if (fmt == GST_FORMAT_TIME) {
2540 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2545 res = gst_pad_query_default (pad, query);
2548 gst_object_unref (wav);
2553 gst_wavparse_srcpad_event (GstPad * pad, GstEvent * event)
2555 GstWavParse *wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
2556 gboolean res = FALSE;
2558 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2560 switch (GST_EVENT_TYPE (event)) {
2561 case GST_EVENT_SEEK:
2562 /* can only handle events when we are in the data state */
2563 if (wavparse->state == GST_WAVPARSE_DATA) {
2564 res = gst_wavparse_perform_seek (wavparse, event);
2566 gst_event_unref (event);
2569 res = gst_pad_push_event (wavparse->sinkpad, event);
2572 gst_object_unref (wavparse);
2577 gst_wavparse_sink_activate (GstPad * sinkpad)
2579 GstWavParse *wav = GST_WAVPARSE (gst_pad_get_parent (sinkpad));
2583 gst_adapter_clear (wav->adapter);
2584 g_object_unref (wav->adapter);
2585 wav->adapter = NULL;
2588 if (gst_pad_check_pull_range (sinkpad)) {
2589 GST_DEBUG ("going to pull mode");
2590 wav->streaming = FALSE;
2591 res = gst_pad_activate_pull (sinkpad, TRUE);
2593 GST_DEBUG ("going to push (streaming) mode");
2594 wav->streaming = TRUE;
2595 wav->adapter = gst_adapter_new ();
2596 res = gst_pad_activate_push (sinkpad, TRUE);
2598 gst_object_unref (wav);
2604 gst_wavparse_sink_activate_pull (GstPad * sinkpad, gboolean active)
2606 GstWavParse *wav = GST_WAVPARSE (GST_OBJECT_PARENT (sinkpad));
2609 /* if we have a scheduler we can start the task */
2610 wav->segment_running = TRUE;
2611 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2614 wav->segment_running = FALSE;
2615 return gst_pad_stop_task (sinkpad);
2619 static GstStateChangeReturn
2620 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2622 GstStateChangeReturn ret;
2623 GstWavParse *wav = GST_WAVPARSE (element);
2625 switch (transition) {
2626 case GST_STATE_CHANGE_NULL_TO_READY:
2628 case GST_STATE_CHANGE_READY_TO_PAUSED:
2629 gst_wavparse_reset (wav);
2631 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2637 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2639 switch (transition) {
2640 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2642 case GST_STATE_CHANGE_PAUSED_TO_READY:
2643 gst_wavparse_destroy_sourcepad (wav);
2644 gst_wavparse_reset (wav);
2646 case GST_STATE_CHANGE_READY_TO_NULL:
2655 plugin_init (GstPlugin * plugin)
2659 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2663 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2666 "Parse a .wav file into raw audio",
2667 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)