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_static_pad_template (element_class,
106 &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 || tag == GST_RIFF_TAG_ID32 ||
1204 tag == GST_RIFF_TAG_IDVX) {
1205 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1206 GST_FOURCC_ARGS (tag));
1207 gst_buffer_unref (buf);
1212 if (tag != GST_RIFF_TAG_fmt)
1215 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1217 goto parse_header_error;
1219 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1221 /* do sanity checks of header fields */
1222 if (header->channels == 0)
1224 if (header->rate == 0)
1227 GST_DEBUG_OBJECT (wav, "creating the caps");
1229 /* Note: gst_riff_create_audio_caps might need to fix values in
1230 * the header header depending on the format, so call it first */
1231 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1235 gst_buffer_unref (extra);
1238 goto unknown_format;
1240 /* do more sanity checks of header fields
1241 * (these can be sanitized by gst_riff_create_audio_caps()
1243 wav->format = header->format;
1244 wav->rate = header->rate;
1245 wav->channels = header->channels;
1246 wav->blockalign = header->blockalign;
1247 wav->depth = header->size;
1248 wav->av_bps = header->av_bps;
1254 /* do format specific handling */
1255 switch (wav->format) {
1256 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1257 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1259 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1260 * bitrate inside the mpeg stream */
1261 GST_INFO ("resetting bps from %d to 0 for mp2/3", wav->av_bps);
1265 case GST_RIFF_WAVE_FORMAT_PCM:
1266 if (wav->blockalign > wav->channels * (guint) ceil (wav->depth / 8.0))
1267 goto invalid_blockalign;
1270 if (wav->av_bps > wav->blockalign * wav->rate)
1272 /* use the configured bps */
1273 wav->bps = wav->av_bps;
1277 wav->width = (wav->blockalign * 8) / wav->channels;
1278 wav->bytes_per_sample = wav->channels * wav->width / 8;
1280 if (wav->bytes_per_sample <= 0)
1281 goto no_bytes_per_sample;
1283 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1284 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1285 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1286 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1287 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1288 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1289 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1291 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1292 * formats). This will make the element output a BYTE format segment and
1293 * will not timestamp the outgoing buffers.
1295 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1297 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1299 /* create pad later so we can sniff the first few bytes
1300 * of the real data and correct our caps if necessary */
1301 gst_caps_replace (&wav->caps, caps);
1302 gst_caps_replace (&caps, NULL);
1304 wav->got_fmt = TRUE;
1307 wav->tags = gst_tag_list_new ();
1309 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1310 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1312 g_free (codec_name);
1318 bformat = GST_FORMAT_BYTES;
1319 gst_pad_query_peer_duration (wav->sinkpad, &bformat, &upstream_size);
1320 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1322 /* loop headers until we get data */
1324 if (wav->streaming) {
1325 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1329 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1330 &buf)) != GST_FLOW_OK)
1331 goto header_read_error;
1332 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
1333 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
1336 GST_INFO_OBJECT (wav,
1337 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT,
1338 GST_FOURCC_ARGS (tag), wav->offset);
1340 /* wav is a st00pid format, we don't know for sure where data starts.
1341 * So we have to go bit by bit until we find the 'data' header
1344 case GST_RIFF_TAG_data:{
1345 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %d", size);
1346 if (wav->streaming) {
1347 gst_adapter_flush (wav->adapter, 8);
1350 gst_buffer_unref (buf);
1353 wav->datastart = wav->offset;
1354 /* If size is zero, then the data chunk probably actually extends to
1355 the end of the file */
1356 if (size == 0 && upstream_size) {
1357 size = upstream_size - wav->datastart;
1359 /* Or the file might be truncated */
1360 else if (upstream_size) {
1361 size = MIN (size, (upstream_size - wav->datastart));
1363 wav->datasize = (guint64) size;
1364 wav->dataleft = (guint64) size;
1365 wav->end_offset = size + wav->datastart;
1366 if (!wav->streaming) {
1367 /* We will continue parsing tags 'till end */
1368 wav->offset += size;
1370 GST_DEBUG_OBJECT (wav, "datasize = %d", size);
1373 case GST_RIFF_TAG_fact:{
1374 if (wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1375 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1376 const guint data_size = 4;
1378 GST_INFO_OBJECT (wav, "Have fact chunk");
1379 if (size < data_size) {
1380 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1381 /* need more data */
1384 GST_DEBUG_OBJECT (wav, "need %d, available %d; ignoring chunk",
1388 /* number of samples (for compressed formats) */
1389 if (wav->streaming) {
1390 const guint8 *data = NULL;
1392 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1395 gst_adapter_flush (wav->adapter, 8);
1396 data = gst_adapter_peek (wav->adapter, data_size);
1397 wav->fact = GST_READ_UINT32_LE (data);
1398 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1400 gst_buffer_unref (buf);
1402 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1403 data_size, &buf)) != GST_FLOW_OK)
1404 goto header_read_error;
1405 wav->fact = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
1406 gst_buffer_unref (buf);
1408 GST_DEBUG_OBJECT (wav, "have fact %u", wav->fact);
1409 wav->offset += 8 + GST_ROUND_UP_2 (size);
1412 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1413 /* need more data */
1419 case GST_RIFF_TAG_acid:{
1420 const gst_riff_acid *acid = NULL;
1421 const guint data_size = sizeof (gst_riff_acid);
1423 GST_INFO_OBJECT (wav, "Have acid chunk");
1424 if (size < data_size) {
1425 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1426 /* need more data */
1429 GST_DEBUG_OBJECT (wav, "need %d, available %d; ignoring chunk",
1433 if (wav->streaming) {
1434 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1437 gst_adapter_flush (wav->adapter, 8);
1438 acid = (const gst_riff_acid *) gst_adapter_peek (wav->adapter,
1441 gst_buffer_unref (buf);
1443 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1444 size, &buf)) != GST_FLOW_OK)
1445 goto header_read_error;
1446 acid = (const gst_riff_acid *) GST_BUFFER_DATA (buf);
1448 /* send data as tags */
1450 wav->tags = gst_tag_list_new ();
1451 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1452 GST_TAG_BEATS_PER_MINUTE, acid->tempo, NULL);
1454 size = GST_ROUND_UP_2 (size);
1455 if (wav->streaming) {
1456 gst_adapter_flush (wav->adapter, size);
1458 gst_buffer_unref (buf);
1460 wav->offset += 8 + size;
1463 /* FIXME: all list tags after data are ignored in streaming mode */
1464 case GST_RIFF_TAG_LIST:{
1467 if (wav->streaming) {
1468 const guint8 *data = NULL;
1470 if (gst_adapter_available (wav->adapter) < 12) {
1473 data = gst_adapter_peek (wav->adapter, 12);
1474 ltag = GST_READ_UINT32_LE (data + 8);
1476 gst_buffer_unref (buf);
1478 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1479 &buf)) != GST_FLOW_OK)
1480 goto header_read_error;
1481 ltag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 8);
1484 case GST_RIFF_LIST_INFO:{
1485 const gint data_size = size - 4;
1488 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1489 if (wav->streaming) {
1490 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1493 gst_adapter_flush (wav->adapter, 12);
1495 if (data_size > 0) {
1496 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1498 gst_adapter_flush (wav->adapter, 1);
1502 gst_buffer_unref (buf);
1503 if (data_size > 0) {
1505 gst_pad_pull_range (wav->sinkpad, wav->offset,
1506 data_size, &buf)) != GST_FLOW_OK)
1507 goto header_read_error;
1510 if (data_size > 0) {
1512 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1514 GstTagList *old = wav->tags;
1516 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1518 gst_tag_list_free (old);
1519 gst_tag_list_free (new);
1521 gst_buffer_unref (buf);
1522 wav->offset += GST_ROUND_UP_2 (data_size);
1527 GST_INFO_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1528 GST_FOURCC_ARGS (ltag));
1529 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1530 /* need more data */
1537 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1538 /* need more data */
1543 if (upstream_size && (wav->offset >= upstream_size)) {
1544 /* Now we are gone through the whole file */
1549 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1551 if (wav->bps <= 0 && wav->fact) {
1553 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1555 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1556 (guint64) wav->fact);
1557 GST_INFO_OBJECT (wav, "calculated bps : %d, enabling VBR", wav->bps);
1562 if (gst_wavparse_calculate_duration (wav)) {
1563 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1564 gst_segment_set_duration (&wav->segment, GST_FORMAT_TIME, wav->duration);
1566 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1567 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1568 gst_segment_set_duration (&wav->segment, GST_FORMAT_BYTES, wav->datasize);
1571 /* now we have all the info to perform a pending seek if any, if no
1572 * event, this will still do the right thing and it will also send
1573 * the right newsegment event downstream. */
1574 gst_wavparse_perform_seek (wav, wav->seek_event);
1575 /* remove pending event */
1576 event_p = &wav->seek_event;
1577 gst_event_replace (event_p, NULL);
1579 /* we just started, we are discont */
1580 wav->discont = TRUE;
1582 wav->state = GST_WAVPARSE_DATA;
1584 /* determine reasonable max buffer size,
1585 * that is, buffers not too small either size or time wise
1586 * so we do not end up with too many of them */
1589 gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size);
1590 wav->max_buf_size = upstream_size;
1591 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1592 if (wav->blockalign > 0)
1593 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1595 GST_DEBUG_OBJECT (wav, "max buffer size %d", wav->max_buf_size);
1603 g_free (codec_name);
1607 gst_caps_unref (caps);
1612 res = GST_FLOW_ERROR;
1617 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1618 ("Invalid WAV header (no fmt at start): %"
1619 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1624 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1625 ("Couldn't parse audio header"));
1630 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1631 ("Stream claims to contain no channels - invalid data"));
1636 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1637 ("Stream with sample_rate == 0 - invalid data"));
1642 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1643 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1644 wav->blockalign, wav->channels * (guint) ceil (wav->depth / 8.0)));
1649 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1650 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1651 wav->av_bps, wav->blockalign * wav->rate));
1654 no_bytes_per_sample:
1656 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1657 ("Could not caluclate bytes per sample - invalid data"));
1662 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1663 ("No caps found for format 0x%x, %d channels, %d Hz",
1664 wav->format, wav->channels, wav->rate));
1669 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1670 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1676 * Read WAV file tag when streaming
1678 static GstFlowReturn
1679 gst_wavparse_parse_stream_init (GstWavParse * wav)
1681 if (gst_adapter_available (wav->adapter) >= 12) {
1684 /* _take flushes the data */
1685 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1687 GST_DEBUG ("Parsing wav header");
1688 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1689 return GST_FLOW_ERROR;
1692 /* Go to next state */
1693 wav->state = GST_WAVPARSE_HEADER;
1698 /* handle an event sent directly to the element.
1700 * This event can be sent either in the READY state or the
1701 * >READY state. The only event of interest really is the seek
1704 * In the READY state we can only store the event and try to
1705 * respect it when going to PAUSED. We assume we are in the
1706 * READY state when our parsing state != GST_WAVPARSE_DATA.
1708 * When we are steaming, we can simply perform the seek right
1712 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1714 GstWavParse *wav = GST_WAVPARSE (element);
1715 gboolean res = FALSE;
1718 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1720 switch (GST_EVENT_TYPE (event)) {
1721 case GST_EVENT_SEEK:
1722 if (wav->state == GST_WAVPARSE_DATA) {
1723 /* we can handle the seek directly when streaming data */
1724 res = gst_wavparse_perform_seek (wav, event);
1726 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1728 event_p = &wav->seek_event;
1729 gst_event_replace (event_p, event);
1731 /* we always return true */
1738 gst_event_unref (event);
1743 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1747 s = gst_caps_get_structure (caps, 0);
1748 if (!gst_structure_has_name (s, "audio/x-dts"))
1750 if (prob >= GST_TYPE_FIND_LIKELY)
1752 /* DTS at non-0 offsets and without second sync may yield POSSIBLE .. */
1753 if (prob < GST_TYPE_FIND_POSSIBLE)
1755 /* .. in which case we want at least a valid-looking rate and channels */
1756 if (!gst_structure_has_field (s, "channels"))
1758 /* and for extra assurance we could also check the rate from the DTS frame
1759 * against the one in the wav header, but for now let's not do that */
1760 return gst_structure_has_field (s, "rate");
1764 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1768 GST_DEBUG_OBJECT (wav, "adding src pad");
1771 s = gst_caps_get_structure (wav->caps, 0);
1772 if (s && gst_structure_has_name (s, "audio/x-raw-int") && buf != NULL) {
1773 GstTypeFindProbability prob;
1776 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1777 if (tf_caps != NULL) {
1778 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1779 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1780 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1781 gst_caps_unref (wav->caps);
1782 wav->caps = tf_caps;
1784 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1785 GST_TAG_AUDIO_CODEC, "dts", NULL);
1787 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1788 "marked as raw PCM audio, but ignoring for now", tf_caps);
1789 gst_caps_unref (tf_caps);
1795 gst_wavparse_create_sourcepad (wav);
1796 gst_pad_set_active (wav->srcpad, TRUE);
1797 gst_pad_set_caps (wav->srcpad, wav->caps);
1798 gst_caps_replace (&wav->caps, NULL);
1800 gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
1801 gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
1803 if (wav->close_segment) {
1804 GST_DEBUG_OBJECT (wav, "Send close segment event on newpad");
1805 gst_pad_push_event (wav->srcpad, wav->close_segment);
1806 wav->close_segment = NULL;
1808 if (wav->start_segment) {
1809 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1810 gst_pad_push_event (wav->srcpad, wav->start_segment);
1811 wav->start_segment = NULL;
1815 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (wav), wav->srcpad,
1821 static GstFlowReturn
1822 gst_wavparse_stream_data (GstWavParse * wav)
1824 GstBuffer *buf = NULL;
1825 GstFlowReturn res = GST_FLOW_OK;
1826 guint64 desired, obtained;
1827 GstClockTime timestamp, next_timestamp, duration;
1828 guint64 pos, nextpos;
1831 GST_LOG_OBJECT (wav,
1832 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1833 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1835 /* Get the next n bytes and output them */
1836 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1839 /* scale the amount of data by the segment rate so we get equal
1840 * amounts of data regardless of the playback rate */
1842 MIN (gst_guint64_to_gdouble (wav->dataleft),
1843 wav->max_buf_size * wav->segment.abs_rate);
1845 if (desired >= wav->blockalign && wav->blockalign > 0)
1846 desired -= (desired % wav->blockalign);
1848 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1849 "from the sinkpad", desired);
1851 if (wav->streaming) {
1852 guint avail = gst_adapter_available (wav->adapter);
1855 /* flush some bytes if evil upstream sends segment that starts
1856 * before data or does is not send sample aligned segment */
1857 if (G_LIKELY (wav->offset >= wav->datastart)) {
1858 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1860 extra = wav->datastart - wav->offset;
1863 if (G_UNLIKELY (extra)) {
1864 extra = wav->bytes_per_sample - extra;
1865 if (extra <= avail) {
1866 GST_DEBUG_OBJECT (wav, "flushing %d bytes to sample boundary", extra);
1867 gst_adapter_flush (wav->adapter, extra);
1868 wav->offset += extra;
1869 wav->dataleft -= extra;
1870 goto iterate_adapter;
1872 GST_DEBUG_OBJECT (wav, "flushing %d bytes", avail);
1873 gst_adapter_clear (wav->adapter);
1874 wav->offset += avail;
1875 wav->dataleft -= avail;
1880 if (avail < desired) {
1881 GST_LOG_OBJECT (wav, "Got only %d bytes of data from the sinkpad", avail);
1885 buf = gst_adapter_take_buffer (wav->adapter, desired);
1887 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
1888 desired, &buf)) != GST_FLOW_OK)
1891 /* we may get a short buffer at the end of the file */
1892 if (GST_BUFFER_SIZE (buf) < desired) {
1893 GST_LOG_OBJECT (wav, "Got only %u bytes of data", GST_BUFFER_SIZE (buf));
1894 if (GST_BUFFER_SIZE (buf) >= wav->blockalign) {
1895 buf = gst_buffer_make_metadata_writable (buf);
1896 GST_BUFFER_SIZE (buf) -= (GST_BUFFER_SIZE (buf) % wav->blockalign);
1898 gst_buffer_unref (buf);
1904 obtained = GST_BUFFER_SIZE (buf);
1906 /* our positions in bytes */
1907 pos = wav->offset - wav->datastart;
1908 nextpos = pos + obtained;
1910 /* update offsets, does not overflow. */
1911 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
1912 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
1914 /* first chunk of data? create the source pad. We do this only here so
1915 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
1916 if (G_UNLIKELY (wav->first)) {
1918 /* this will also push the segment events */
1919 gst_wavparse_add_src_pad (wav, buf);
1921 /* If we have a pending close/start segment, send it now. */
1922 if (G_UNLIKELY (wav->close_segment != NULL)) {
1923 gst_pad_push_event (wav->srcpad, wav->close_segment);
1924 wav->close_segment = NULL;
1926 if (G_UNLIKELY (wav->start_segment != NULL)) {
1927 gst_pad_push_event (wav->srcpad, wav->start_segment);
1928 wav->start_segment = NULL;
1933 /* and timestamps if we have a bitrate, be careful for overflows */
1934 timestamp = uint64_ceiling_scale (pos, GST_SECOND, (guint64) wav->bps);
1936 uint64_ceiling_scale (nextpos, GST_SECOND, (guint64) wav->bps);
1937 duration = next_timestamp - timestamp;
1939 /* update current running segment position */
1940 if (G_LIKELY (next_timestamp >= wav->segment.start))
1941 gst_segment_set_last_stop (&wav->segment, GST_FORMAT_TIME,
1943 } else if (wav->fact) {
1945 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
1946 /* and timestamps if we have a bitrate, be careful for overflows */
1947 timestamp = uint64_ceiling_scale (pos, GST_SECOND, bps);
1948 next_timestamp = uint64_ceiling_scale (nextpos, GST_SECOND, bps);
1949 duration = next_timestamp - timestamp;
1951 /* no bitrate, all we know is that the first sample has timestamp 0, all
1952 * other positions and durations have unknown timestamp. */
1956 timestamp = GST_CLOCK_TIME_NONE;
1957 duration = GST_CLOCK_TIME_NONE;
1958 /* update current running segment position with byte offset */
1959 if (G_LIKELY (nextpos >= wav->segment.start))
1960 gst_segment_set_last_stop (&wav->segment, GST_FORMAT_BYTES, nextpos);
1962 if ((pos > 0) && wav->vbr) {
1963 /* don't set timestamps for VBR files if it's not the first buffer */
1964 timestamp = GST_CLOCK_TIME_NONE;
1965 duration = GST_CLOCK_TIME_NONE;
1968 GST_DEBUG_OBJECT (wav, "marking DISCONT");
1969 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1970 wav->discont = FALSE;
1973 GST_BUFFER_TIMESTAMP (buf) = timestamp;
1974 GST_BUFFER_DURATION (buf) = duration;
1976 /* don't forget to set the caps on the buffer */
1977 gst_buffer_set_caps (buf, GST_PAD_CAPS (wav->srcpad));
1979 GST_LOG_OBJECT (wav,
1980 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
1981 ", size:%u", GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration),
1982 GST_BUFFER_SIZE (buf));
1984 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
1987 if (obtained < wav->dataleft) {
1988 wav->offset += obtained;
1989 wav->dataleft -= obtained;
1991 wav->offset += wav->dataleft;
1995 /* Iterate until need more data, so adapter size won't grow */
1996 if (wav->streaming) {
1997 GST_LOG_OBJECT (wav,
1998 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
2000 goto iterate_adapter;
2007 GST_DEBUG_OBJECT (wav, "found EOS");
2008 return GST_FLOW_UNEXPECTED;
2012 /* check if we got EOS */
2013 if (res == GST_FLOW_UNEXPECTED)
2016 GST_WARNING_OBJECT (wav,
2017 "Error getting %" G_GINT64_FORMAT " bytes from the "
2018 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
2023 GST_INFO_OBJECT (wav,
2024 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
2025 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
2026 gst_pad_is_linked (wav->srcpad));
2032 gst_wavparse_loop (GstPad * pad)
2035 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2037 GST_LOG_OBJECT (wav, "process data");
2039 switch (wav->state) {
2040 case GST_WAVPARSE_START:
2041 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2042 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2045 wav->state = GST_WAVPARSE_HEADER;
2048 case GST_WAVPARSE_HEADER:
2049 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2050 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2053 wav->state = GST_WAVPARSE_DATA;
2054 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2057 case GST_WAVPARSE_DATA:
2058 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2062 g_assert_not_reached ();
2069 const gchar *reason = gst_flow_get_name (ret);
2071 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2072 wav->segment_running = FALSE;
2073 gst_pad_pause_task (pad);
2075 if (ret == GST_FLOW_UNEXPECTED) {
2076 /* add pad before we perform EOS */
2077 if (G_UNLIKELY (wav->first)) {
2079 gst_wavparse_add_src_pad (wav, NULL);
2082 if (wav->state == GST_WAVPARSE_START)
2083 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2084 ("No valid input found before end of stream"), (NULL));
2086 /* perform EOS logic */
2087 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2090 if ((stop = wav->segment.stop) == -1)
2091 stop = wav->segment.duration;
2093 gst_element_post_message (GST_ELEMENT_CAST (wav),
2094 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2095 wav->segment.format, stop));
2097 if (wav->srcpad != NULL)
2098 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2100 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2101 /* for fatal errors we post an error message, post the error
2102 * first so the app knows about the error first. */
2103 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
2104 (_("Internal data flow error.")),
2105 ("streaming task paused, reason %s (%d)", reason, ret));
2106 if (wav->srcpad != NULL)
2107 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2113 static GstFlowReturn
2114 gst_wavparse_chain (GstPad * pad, GstBuffer * buf)
2117 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2119 GST_LOG_OBJECT (wav, "adapter_push %u bytes", GST_BUFFER_SIZE (buf));
2121 gst_adapter_push (wav->adapter, buf);
2123 switch (wav->state) {
2124 case GST_WAVPARSE_START:
2125 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2126 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2129 if (wav->state != GST_WAVPARSE_HEADER)
2132 /* otherwise fall-through */
2133 case GST_WAVPARSE_HEADER:
2134 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2135 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2138 if (!wav->got_fmt || wav->datastart == 0)
2141 wav->state = GST_WAVPARSE_DATA;
2142 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2145 case GST_WAVPARSE_DATA:
2146 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2147 wav->discont = TRUE;
2148 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2152 g_return_val_if_reached (GST_FLOW_ERROR);
2155 if (G_UNLIKELY (wav->abort_buffering)) {
2156 wav->abort_buffering = FALSE;
2157 ret = GST_FLOW_ERROR;
2158 /* sort of demux/parse error */
2159 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2165 static GstFlowReturn
2166 gst_wavparse_flush_data (GstWavParse * wav)
2168 GstFlowReturn ret = GST_FLOW_OK;
2171 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2173 wav->end_offset = wav->offset + av;
2174 ret = gst_wavparse_stream_data (wav);
2181 gst_wavparse_sink_event (GstPad * pad, GstEvent * event)
2183 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2184 gboolean ret = TRUE;
2186 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2188 switch (GST_EVENT_TYPE (event)) {
2189 case GST_EVENT_NEWSEGMENT:
2192 gdouble rate, arate;
2193 gint64 start, stop, time, offset = 0, end_offset = -1;
2197 /* some debug output */
2198 gst_segment_init (&segment, GST_FORMAT_UNDEFINED);
2199 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
2200 &start, &stop, &time);
2201 gst_segment_set_newsegment_full (&segment, update, rate, arate, format,
2203 GST_DEBUG_OBJECT (wav,
2204 "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
2207 if (wav->state != GST_WAVPARSE_DATA) {
2208 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2212 /* now we are either committed to TIME or BYTE format,
2213 * and we only expect a BYTE segment, e.g. following a seek */
2214 if (format == GST_FORMAT_BYTES) {
2217 start -= wav->datastart;
2218 start = MAX (start, 0);
2222 stop -= wav->datastart;
2223 stop = MAX (stop, 0);
2225 if (wav->segment.format == GST_FORMAT_TIME) {
2226 guint64 bps = wav->bps;
2228 /* operating in format TIME, so we can convert */
2229 if (!bps && wav->fact)
2231 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2235 uint64_ceiling_scale (start, GST_SECOND, (guint64) wav->bps);
2238 uint64_ceiling_scale (stop, GST_SECOND, (guint64) wav->bps);
2242 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2246 /* accept upstream's notion of segment and distribute along */
2247 gst_segment_set_newsegment_full (&wav->segment, update, rate, arate,
2248 wav->segment.format, start, stop, start);
2249 /* also store the newsegment event for the streaming thread */
2250 if (wav->start_segment)
2251 gst_event_unref (wav->start_segment);
2252 wav->start_segment =
2253 gst_event_new_new_segment_full (update, rate, arate,
2254 wav->segment.format, start, stop, start);
2255 GST_DEBUG_OBJECT (wav, "Pushing newseg update %d, rate %g, "
2256 "applied rate %g, format %d, start %" G_GINT64_FORMAT ", "
2257 "stop %" G_GINT64_FORMAT, update, rate, arate, wav->segment.format,
2260 /* stream leftover data in current segment */
2261 gst_wavparse_flush_data (wav);
2262 /* and set up streaming thread for next one */
2263 wav->offset = offset;
2264 wav->end_offset = end_offset;
2265 if (wav->end_offset > 0) {
2266 wav->dataleft = wav->end_offset - wav->offset;
2268 /* infinity; upstream will EOS when done */
2269 wav->dataleft = G_MAXUINT64;
2272 gst_event_unref (event);
2276 /* add pad if needed so EOS is seen downstream */
2277 if (G_UNLIKELY (wav->first)) {
2279 gst_wavparse_add_src_pad (wav, NULL);
2281 /* stream leftover data in current segment */
2282 gst_wavparse_flush_data (wav);
2285 if (wav->state == GST_WAVPARSE_START)
2286 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2287 ("No valid input found before end of stream"), (NULL));
2290 case GST_EVENT_FLUSH_STOP:
2291 gst_adapter_clear (wav->adapter);
2292 wav->discont = TRUE;
2295 ret = gst_pad_event_default (wav->sinkpad, event);
2303 /* convert and query stuff */
2304 static const GstFormat *
2305 gst_wavparse_get_formats (GstPad * pad)
2307 static GstFormat formats[] = {
2310 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2319 gst_wavparse_pad_convert (GstPad * pad,
2320 GstFormat src_format, gint64 src_value,
2321 GstFormat * dest_format, gint64 * dest_value)
2323 GstWavParse *wavparse;
2324 gboolean res = TRUE;
2326 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2328 if (*dest_format == src_format) {
2329 *dest_value = src_value;
2333 if ((wavparse->bps == 0) && !wavparse->fact)
2336 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2337 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2339 switch (src_format) {
2340 case GST_FORMAT_BYTES:
2341 switch (*dest_format) {
2342 case GST_FORMAT_DEFAULT:
2343 *dest_value = src_value / wavparse->bytes_per_sample;
2344 /* make sure we end up on a sample boundary */
2345 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2347 case GST_FORMAT_TIME:
2348 /* src_value + datastart = offset */
2349 GST_INFO_OBJECT (wavparse,
2350 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2352 if (wavparse->bps > 0)
2353 *dest_value = uint64_ceiling_scale (src_value, GST_SECOND,
2354 (guint64) wavparse->bps);
2355 else if (wavparse->fact) {
2356 guint64 bps = uint64_ceiling_scale_int (wavparse->datasize,
2357 wavparse->rate, wavparse->fact);
2359 *dest_value = uint64_ceiling_scale_int (src_value, GST_SECOND, bps);
2370 case GST_FORMAT_DEFAULT:
2371 switch (*dest_format) {
2372 case GST_FORMAT_BYTES:
2373 *dest_value = src_value * wavparse->bytes_per_sample;
2375 case GST_FORMAT_TIME:
2376 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2377 (guint64) wavparse->rate);
2385 case GST_FORMAT_TIME:
2386 switch (*dest_format) {
2387 case GST_FORMAT_BYTES:
2388 if (wavparse->bps > 0)
2389 *dest_value = gst_util_uint64_scale (src_value,
2390 (guint64) wavparse->bps, GST_SECOND);
2392 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2393 wavparse->rate, wavparse->fact);
2395 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2397 /* make sure we end up on a sample boundary */
2398 *dest_value -= *dest_value % wavparse->blockalign;
2400 case GST_FORMAT_DEFAULT:
2401 *dest_value = gst_util_uint64_scale (src_value,
2402 (guint64) wavparse->rate, GST_SECOND);
2421 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2427 static const GstQueryType *
2428 gst_wavparse_get_query_types (GstPad * pad)
2430 static const GstQueryType types[] = {
2441 /* handle queries for location and length in requested format */
2443 gst_wavparse_pad_query (GstPad * pad, GstQuery * query)
2445 gboolean res = TRUE;
2446 GstWavParse *wav = GST_WAVPARSE (gst_pad_get_parent (pad));
2448 /* only if we know */
2449 if (wav->state != GST_WAVPARSE_DATA) {
2450 gst_object_unref (wav);
2454 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2456 switch (GST_QUERY_TYPE (query)) {
2457 case GST_QUERY_POSITION:
2463 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2464 curb = wav->offset - wav->datastart;
2465 gst_query_parse_position (query, &format, NULL);
2466 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2469 case GST_FORMAT_TIME:
2470 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2474 format = GST_FORMAT_BYTES;
2479 gst_query_set_position (query, format, cur);
2482 case GST_QUERY_DURATION:
2484 gint64 duration = 0;
2487 gst_query_parse_duration (query, &format, NULL);
2490 case GST_FORMAT_TIME:{
2491 if ((res = gst_wavparse_calculate_duration (wav))) {
2492 duration = wav->duration;
2497 format = GST_FORMAT_BYTES;
2498 duration = wav->datasize;
2501 gst_query_set_duration (query, format, duration);
2504 case GST_QUERY_CONVERT:
2506 gint64 srcvalue, dstvalue;
2507 GstFormat srcformat, dstformat;
2509 gst_query_parse_convert (query, &srcformat, &srcvalue,
2510 &dstformat, &dstvalue);
2511 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2512 &dstformat, &dstvalue);
2514 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2517 case GST_QUERY_SEEKING:{
2519 gboolean seekable = FALSE;
2521 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2522 if (fmt == wav->segment.format) {
2523 if (wav->streaming) {
2526 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2527 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2528 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2529 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2531 gst_query_unref (q);
2533 GST_LOG_OBJECT (wav, "looping => seekable");
2537 } else if (fmt == GST_FORMAT_TIME) {
2541 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2546 res = gst_pad_query_default (pad, query);
2549 gst_object_unref (wav);
2554 gst_wavparse_srcpad_event (GstPad * pad, GstEvent * event)
2556 GstWavParse *wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
2557 gboolean res = FALSE;
2559 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2561 switch (GST_EVENT_TYPE (event)) {
2562 case GST_EVENT_SEEK:
2563 /* can only handle events when we are in the data state */
2564 if (wavparse->state == GST_WAVPARSE_DATA) {
2565 res = gst_wavparse_perform_seek (wavparse, event);
2567 gst_event_unref (event);
2570 res = gst_pad_push_event (wavparse->sinkpad, event);
2573 gst_object_unref (wavparse);
2578 gst_wavparse_sink_activate (GstPad * sinkpad)
2580 GstWavParse *wav = GST_WAVPARSE (gst_pad_get_parent (sinkpad));
2584 gst_adapter_clear (wav->adapter);
2585 g_object_unref (wav->adapter);
2586 wav->adapter = NULL;
2589 if (gst_pad_check_pull_range (sinkpad)) {
2590 GST_DEBUG ("going to pull mode");
2591 wav->streaming = FALSE;
2592 res = gst_pad_activate_pull (sinkpad, TRUE);
2594 GST_DEBUG ("going to push (streaming) mode");
2595 wav->streaming = TRUE;
2596 wav->adapter = gst_adapter_new ();
2597 res = gst_pad_activate_push (sinkpad, TRUE);
2599 gst_object_unref (wav);
2605 gst_wavparse_sink_activate_pull (GstPad * sinkpad, gboolean active)
2607 GstWavParse *wav = GST_WAVPARSE (GST_OBJECT_PARENT (sinkpad));
2610 /* if we have a scheduler we can start the task */
2611 wav->segment_running = TRUE;
2612 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2615 wav->segment_running = FALSE;
2616 return gst_pad_stop_task (sinkpad);
2620 static GstStateChangeReturn
2621 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2623 GstStateChangeReturn ret;
2624 GstWavParse *wav = GST_WAVPARSE (element);
2626 switch (transition) {
2627 case GST_STATE_CHANGE_NULL_TO_READY:
2629 case GST_STATE_CHANGE_READY_TO_PAUSED:
2630 gst_wavparse_reset (wav);
2632 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2638 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2640 switch (transition) {
2641 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2643 case GST_STATE_CHANGE_PAUSED_TO_READY:
2644 gst_wavparse_destroy_sourcepad (wav);
2645 gst_wavparse_reset (wav);
2647 case GST_STATE_CHANGE_READY_TO_NULL:
2656 plugin_init (GstPlugin * plugin)
2660 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2664 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2667 "Parse a .wav file into raw audio",
2668 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)