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/gst-i18n-plugin.h>
60 GST_DEBUG_CATEGORY_STATIC (wavparse_debug);
61 #define GST_CAT_DEFAULT (wavparse_debug)
63 static void gst_wavparse_dispose (GObject * object);
65 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad);
66 static gboolean gst_wavparse_sink_activate_pull (GstPad * sinkpad,
68 static gboolean gst_wavparse_send_event (GstElement * element,
70 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
71 GstStateChange transition);
73 static const GstQueryType *gst_wavparse_get_query_types (GstPad * pad);
74 static gboolean gst_wavparse_pad_query (GstPad * pad, GstQuery * query);
75 static gboolean gst_wavparse_pad_convert (GstPad * pad,
77 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
79 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstBuffer * buf);
80 static gboolean gst_wavparse_sink_event (GstPad * pad, GstEvent * event);
81 static void gst_wavparse_loop (GstPad * pad);
82 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstEvent * event);
84 static const GstElementDetails gst_wavparse_details =
85 GST_ELEMENT_DETAILS ("WAV audio demuxer",
86 "Codec/Demuxer/Audio",
87 "Parse a .wav file into raw audio",
88 "Erik Walthinsen <omega@cse.ogi.edu>");
90 static GstStaticPadTemplate sink_template_factory =
91 GST_STATIC_PAD_TEMPLATE ("wavparse_sink",
94 GST_STATIC_CAPS ("audio/x-wav")
97 #define DEBUG_INIT(bla) \
98 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
100 GST_BOILERPLATE_FULL (GstWavParse, gst_wavparse, GstElement,
101 GST_TYPE_ELEMENT, DEBUG_INIT);
104 gst_wavparse_base_init (gpointer g_class)
106 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
107 GstPadTemplate *src_template;
110 gst_element_class_add_pad_template (element_class,
111 gst_static_pad_template_get (&sink_template_factory));
113 src_template = gst_pad_template_new ("wavparse_src", GST_PAD_SRC,
114 GST_PAD_SOMETIMES, gst_riff_create_audio_template_caps ());
115 gst_element_class_add_pad_template (element_class, src_template);
116 gst_object_unref (src_template);
118 gst_element_class_set_details (element_class, &gst_wavparse_details);
122 gst_wavparse_class_init (GstWavParseClass * klass)
124 GstElementClass *gstelement_class;
125 GObjectClass *object_class;
127 gstelement_class = (GstElementClass *) klass;
128 object_class = (GObjectClass *) klass;
130 parent_class = g_type_class_peek_parent (klass);
132 object_class->dispose = gst_wavparse_dispose;
134 gstelement_class->change_state = gst_wavparse_change_state;
135 gstelement_class->send_event = gst_wavparse_send_event;
139 gst_wavparse_reset (GstWavParse * wav)
141 wav->state = GST_WAVPARSE_START;
143 /* These will all be set correctly in the fmt chunk */
157 wav->got_fmt = FALSE;
161 gst_event_unref (wav->seek_event);
162 wav->seek_event = NULL;
164 gst_adapter_clear (wav->adapter);
165 g_object_unref (wav->adapter);
169 gst_tag_list_free (wav->tags);
174 gst_wavparse_dispose (GObject * object)
176 GstWavParse *wav = GST_WAVPARSE (object);
178 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
179 gst_wavparse_reset (wav);
181 G_OBJECT_CLASS (parent_class)->dispose (object);
185 gst_wavparse_init (GstWavParse * wavparse, GstWavParseClass * g_class)
187 gst_wavparse_reset (wavparse);
191 gst_pad_new_from_static_template (&sink_template_factory, "sink");
192 gst_pad_set_activate_function (wavparse->sinkpad,
193 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
194 gst_pad_set_activatepull_function (wavparse->sinkpad,
195 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_pull));
196 gst_pad_set_chain_function (wavparse->sinkpad,
197 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
198 gst_pad_set_event_function (wavparse->sinkpad,
199 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
200 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
202 /* src, will be created later */
203 wavparse->srcpad = NULL;
207 gst_wavparse_destroy_sourcepad (GstWavParse * wavparse)
209 if (wavparse->srcpad) {
210 gst_element_remove_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
211 wavparse->srcpad = NULL;
216 gst_wavparse_create_sourcepad (GstWavParse * wavparse)
218 GstElementClass *klass = GST_ELEMENT_GET_CLASS (wavparse);
219 GstPadTemplate *src_template;
221 /* destroy previous one */
222 gst_wavparse_destroy_sourcepad (wavparse);
225 src_template = gst_element_class_get_pad_template (klass, "wavparse_src");
226 wavparse->srcpad = gst_pad_new_from_template (src_template, "src");
227 gst_pad_use_fixed_caps (wavparse->srcpad);
228 gst_pad_set_query_type_function (wavparse->srcpad,
229 GST_DEBUG_FUNCPTR (gst_wavparse_get_query_types));
230 gst_pad_set_query_function (wavparse->srcpad,
231 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
232 gst_pad_set_event_function (wavparse->srcpad,
233 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
235 GST_DEBUG_OBJECT (wavparse, "srcpad created");
238 /* Compute (value * nom) % denom, avoiding overflow. This can be used
239 * to perform ceiling or rounding division together with
240 * gst_util_uint64_scale[_int]. */
241 #define uint64_scale_modulo(val, nom, denom) \
242 ((val % denom) * (nom % denom) % denom)
244 /* Like gst_util_uint64_scale, but performs ceiling division. */
246 uint64_ceiling_scale_int (guint64 val, gint num, gint denom)
248 guint64 result = gst_util_uint64_scale_int (val, num, denom);
250 if (uint64_scale_modulo (val, num, denom) == 0)
256 /* Like gst_util_uint64_scale, but performs ceiling division. */
258 uint64_ceiling_scale (guint64 val, guint64 num, guint64 denom)
260 guint64 result = gst_util_uint64_scale (val, num, denom);
262 if (uint64_scale_modulo (val, num, denom) == 0)
269 /* FIXME: why is that not in use? */
272 gst_wavparse_parse_adtl (GstWavParse * wavparse, int len)
275 GstByteStream *bs = wavparse->bs;
276 gst_riff_chunk *temp_chunk, chunk;
278 struct _gst_riff_labl labl, *temp_labl;
279 struct _gst_riff_ltxt ltxt, *temp_ltxt;
280 struct _gst_riff_note note, *temp_note;
283 GstPropsEntry *entry;
287 props = wavparse->metadata->properties;
291 gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
292 if (got_bytes != sizeof (gst_riff_chunk)) {
295 temp_chunk = (gst_riff_chunk *) tempdata;
297 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
298 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
300 if (chunk.size == 0) {
301 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
302 len -= sizeof (gst_riff_chunk);
307 case GST_RIFF_adtl_labl:
309 gst_bytestream_peek_bytes (bs, &tempdata,
310 sizeof (struct _gst_riff_labl));
311 if (got_bytes != sizeof (struct _gst_riff_labl)) {
315 temp_labl = (struct _gst_riff_labl *) tempdata;
316 labl.id = GUINT32_FROM_LE (temp_labl->id);
317 labl.size = GUINT32_FROM_LE (temp_labl->size);
318 labl.identifier = GUINT32_FROM_LE (temp_labl->identifier);
320 gst_bytestream_flush (bs, sizeof (struct _gst_riff_labl));
321 len -= sizeof (struct _gst_riff_labl);
323 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, labl.size - 4);
324 if (got_bytes != labl.size - 4) {
328 label_name = (char *) tempdata;
330 gst_bytestream_flush (bs, ((labl.size - 4) + 1) & ~1);
331 len -= (((labl.size - 4) + 1) & ~1);
333 new_caps = gst_caps_new ("label",
334 "application/x-gst-metadata",
335 gst_props_new ("identifier", G_TYPE_INT (labl.identifier),
336 "name", G_TYPE_STRING (label_name), NULL));
338 if (gst_props_get (props, "labels", &caps, NULL)) {
339 caps = g_list_append (caps, new_caps);
341 caps = g_list_append (NULL, new_caps);
343 entry = gst_props_entry_new ("labels", GST_PROPS_GLIST (caps));
344 gst_props_add_entry (props, entry);
349 case GST_RIFF_adtl_ltxt:
351 gst_bytestream_peek_bytes (bs, &tempdata,
352 sizeof (struct _gst_riff_ltxt));
353 if (got_bytes != sizeof (struct _gst_riff_ltxt)) {
357 temp_ltxt = (struct _gst_riff_ltxt *) tempdata;
358 ltxt.id = GUINT32_FROM_LE (temp_ltxt->id);
359 ltxt.size = GUINT32_FROM_LE (temp_ltxt->size);
360 ltxt.identifier = GUINT32_FROM_LE (temp_ltxt->identifier);
361 ltxt.length = GUINT32_FROM_LE (temp_ltxt->length);
362 ltxt.purpose = GUINT32_FROM_LE (temp_ltxt->purpose);
363 ltxt.country = GUINT16_FROM_LE (temp_ltxt->country);
364 ltxt.language = GUINT16_FROM_LE (temp_ltxt->language);
365 ltxt.dialect = GUINT16_FROM_LE (temp_ltxt->dialect);
366 ltxt.codepage = GUINT16_FROM_LE (temp_ltxt->codepage);
368 gst_bytestream_flush (bs, sizeof (struct _gst_riff_ltxt));
369 len -= sizeof (struct _gst_riff_ltxt);
371 if (ltxt.size - 20 > 0) {
372 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, ltxt.size - 20);
373 if (got_bytes != ltxt.size - 20) {
377 gst_bytestream_flush (bs, ((ltxt.size - 20) + 1) & ~1);
378 len -= (((ltxt.size - 20) + 1) & ~1);
380 label_name = (char *) tempdata;
385 new_caps = gst_caps_new ("ltxt",
386 "application/x-gst-metadata",
387 gst_props_new ("identifier", G_TYPE_INT (ltxt.identifier),
388 "name", G_TYPE_STRING (label_name),
389 "length", G_TYPE_INT (ltxt.length), NULL));
391 if (gst_props_get (props, "ltxts", &caps, NULL)) {
392 caps = g_list_append (caps, new_caps);
394 caps = g_list_append (NULL, new_caps);
396 entry = gst_props_entry_new ("ltxts", GST_PROPS_GLIST (caps));
397 gst_props_add_entry (props, entry);
402 case GST_RIFF_adtl_note:
404 gst_bytestream_peek_bytes (bs, &tempdata,
405 sizeof (struct _gst_riff_note));
406 if (got_bytes != sizeof (struct _gst_riff_note)) {
410 temp_note = (struct _gst_riff_note *) tempdata;
411 note.id = GUINT32_FROM_LE (temp_note->id);
412 note.size = GUINT32_FROM_LE (temp_note->size);
413 note.identifier = GUINT32_FROM_LE (temp_note->identifier);
415 gst_bytestream_flush (bs, sizeof (struct _gst_riff_note));
416 len -= sizeof (struct _gst_riff_note);
418 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, note.size - 4);
419 if (got_bytes != note.size - 4) {
423 gst_bytestream_flush (bs, ((note.size - 4) + 1) & ~1);
424 len -= (((note.size - 4) + 1) & ~1);
426 label_name = (char *) tempdata;
428 new_caps = gst_caps_new ("note",
429 "application/x-gst-metadata",
430 gst_props_new ("identifier", G_TYPE_INT (note.identifier),
431 "name", G_TYPE_STRING (label_name), NULL));
433 if (gst_props_get (props, "notes", &caps, NULL)) {
434 caps = g_list_append (caps, new_caps);
436 caps = g_list_append (NULL, new_caps);
438 entry = gst_props_entry_new ("notes", GST_PROPS_GLIST (caps));
439 gst_props_add_entry (props, entry);
445 g_print ("Unknown chunk: %" GST_FOURCC_FORMAT "\n",
446 GST_FOURCC_ARGS (chunk.id));
451 g_object_notify (G_OBJECT (wavparse), "metadata");
455 gst_wavparse_parse_cues (GstWavParse * wavparse, int len)
458 GstByteStream *bs = wavparse->bs;
459 struct _gst_riff_cue *temp_cue, cue;
460 struct _gst_riff_cuepoints *points;
464 GstPropsEntry *entry;
470 gst_bytestream_peek_bytes (bs, &tempdata,
471 sizeof (struct _gst_riff_cue));
472 temp_cue = (struct _gst_riff_cue *) tempdata;
474 /* fixup for our big endian friends */
475 cue.id = GUINT32_FROM_LE (temp_cue->id);
476 cue.size = GUINT32_FROM_LE (temp_cue->size);
477 cue.cuepoints = GUINT32_FROM_LE (temp_cue->cuepoints);
479 gst_bytestream_flush (bs, sizeof (struct _gst_riff_cue));
480 if (got_bytes != sizeof (struct _gst_riff_cue)) {
484 len -= sizeof (struct _gst_riff_cue);
486 /* -4 because cue.size contains the cuepoints size
487 and we've already flushed that out of the system */
488 required = cue.size - 4;
489 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, required);
490 gst_bytestream_flush (bs, ((required) + 1) & ~1);
491 if (got_bytes != required) {
495 len -= (((cue.size - 4) + 1) & ~1);
497 /* now we have an array of struct _gst_riff_cuepoints in tempdata */
498 points = (struct _gst_riff_cuepoints *) tempdata;
500 for (i = 0; i < cue.cuepoints; i++) {
503 caps = gst_caps_new ("cues",
504 "application/x-gst-metadata",
505 gst_props_new ("identifier", G_TYPE_INT (points[i].identifier),
506 "position", G_TYPE_INT (points[i].offset), NULL));
507 cues = g_list_append (cues, caps);
510 entry = gst_props_entry_new ("cues", GST_PROPS_GLIST (cues));
511 gst_props_add_entry (wavparse->metadata->properties, entry);
514 g_object_notify (G_OBJECT (wavparse), "metadata");
517 /* Read 'fmt ' header */
519 gst_wavparse_fmt (GstWavParse * wav)
521 gst_riff_strf_auds *header = NULL;
524 if (!gst_riff_read_strf_auds (wav, &header))
527 wav->format = header->format;
528 wav->rate = header->rate;
529 wav->channels = header->channels;
530 if (wav->channels == 0)
533 wav->blockalign = header->blockalign;
534 wav->width = (header->blockalign * 8) / header->channels;
535 wav->depth = header->size;
536 wav->bps = header->av_bps;
540 /* Note: gst_riff_create_audio_caps might need to fix values in
541 * the header header depending on the format, so call it first */
542 caps = gst_riff_create_audio_caps (header->format, NULL, header, NULL);
548 gst_wavparse_create_sourcepad (wav);
549 gst_pad_use_fixed_caps (wav->srcpad);
550 gst_pad_set_active (wav->srcpad, TRUE);
551 gst_pad_set_caps (wav->srcpad, caps);
552 gst_caps_free (caps);
553 gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
554 gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
556 GST_DEBUG ("frequency %d, channels %d", wav->rate, wav->channels);
563 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
564 ("No FMT tag found"));
569 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
570 ("Stream claims to contain zero channels - invalid data"));
576 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
577 ("Stream claims to bitrate of <= zero - invalid data"));
583 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
589 gst_wavparse_other (GstWavParse * wav)
593 if (!gst_riff_peek_head (wav, &tag, &length, NULL)) {
594 GST_WARNING_OBJECT (wav, "could not peek head");
597 GST_DEBUG_OBJECT (wav, "got tag (%08x) %4.4s, length %d", tag,
598 (gchar *) & tag, length);
601 case GST_RIFF_TAG_LIST:
602 if (!(tag = gst_riff_peek_list (wav))) {
603 GST_WARNING_OBJECT (wav, "could not peek list");
608 case GST_RIFF_LIST_INFO:
609 if (!gst_riff_read_list (wav, &tag) || !gst_riff_read_info (wav)) {
610 GST_WARNING_OBJECT (wav, "could not read list");
615 case GST_RIFF_LIST_adtl:
616 if (!gst_riff_read_skip (wav)) {
617 GST_WARNING_OBJECT (wav, "could not read skip");
623 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag,
625 if (!gst_riff_read_skip (wav)) {
626 GST_WARNING_OBJECT (wav, "could not read skip");
634 case GST_RIFF_TAG_data:
635 if (!gst_bytestream_flush (wav->bs, 8)) {
636 GST_WARNING_OBJECT (wav, "could not flush 8 bytes");
640 GST_DEBUG_OBJECT (wav, "switching to data mode");
641 wav->state = GST_WAVPARSE_DATA;
642 wav->datastart = gst_bytestream_tell (wav->bs);
646 /* length is 0, data probably stretches to the end
648 GST_DEBUG_OBJECT (wav, "length is 0 trying to find length");
649 /* get length of file */
650 file_length = gst_bytestream_length (wav->bs);
651 if (file_length == -1) {
652 GST_DEBUG_OBJECT (wav,
653 "could not get file length, assuming data to eof");
654 /* could not get length, assuming till eof */
655 length = G_MAXUINT32;
657 if (file_length > G_MAXUINT32) {
658 GST_DEBUG_OBJECT (wav, "file length %lld, clipping to 32 bits");
659 /* could not get length, assuming till eof */
660 length = G_MAXUINT32;
662 GST_DEBUG_OBJECT (wav, "file length %lld, datalength",
663 file_length, length);
664 /* substract offset of datastart from length */
665 length = file_length - wav->datastart;
666 GST_DEBUG_OBJECT (wav, "datalength %lld", length);
669 wav->datasize = (guint64) length;
670 GST_DEBUG_OBJECT (wav, "datasize = %ld", length)
673 case GST_RIFF_TAG_cue:
674 if (!gst_riff_read_skip (wav)) {
675 GST_WARNING_OBJECT (wav, "could not read skip");
681 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag, (gchar *) & tag);
682 if (!gst_riff_read_skip (wav))
693 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
697 if (!gst_riff_parse_file_header (element, buf, &doctype))
700 if (doctype != GST_RIFF_RIFF_WAVE)
708 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
709 ("File is not a WAVE file: %" GST_FOURCC_FORMAT,
710 GST_FOURCC_ARGS (doctype)));
716 gst_wavparse_stream_init (GstWavParse * wav)
719 GstBuffer *buf = NULL;
721 if ((res = gst_pad_pull_range (wav->sinkpad,
722 wav->offset, 12, &buf)) != GST_FLOW_OK)
724 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
725 return GST_FLOW_ERROR;
733 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
735 /* -1 always maps to -1 */
741 /* 0 always maps to 0 */
748 *bytepos = uint64_ceiling_scale (ts, (guint64) wav->bps, GST_SECOND);
750 } else if (wav->fact) {
752 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
753 *bytepos = uint64_ceiling_scale (ts, bps, GST_SECOND);
760 /* This function is used to perform seeks on the element.
762 * It also works when event is NULL, in which case it will just
763 * start from the last configured segment. This technique is
764 * used when activating the element and to perform the seek in
768 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
772 GstFormat format, bformat;
774 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
775 gint64 cur, stop, upstream_size;
778 GstSegment seeksegment = { 0, };
782 GST_DEBUG_OBJECT (wav, "doing seek with event");
784 gst_event_parse_seek (event, &rate, &format, &flags,
785 &cur_type, &cur, &stop_type, &stop);
787 /* no negative rates yet */
791 if (format != wav->segment.format) {
792 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
793 gst_format_get_name (format),
794 gst_format_get_name (wav->segment.format));
796 if (cur_type != GST_SEEK_TYPE_NONE)
798 gst_pad_query_convert (wav->srcpad, format, cur,
799 &wav->segment.format, &cur);
800 if (res && stop_type != GST_SEEK_TYPE_NONE)
802 gst_pad_query_convert (wav->srcpad, format, stop,
803 &wav->segment.format, &stop);
807 format = wav->segment.format;
810 GST_DEBUG_OBJECT (wav, "doing seek without event");
813 cur_type = GST_SEEK_TYPE_SET;
814 stop_type = GST_SEEK_TYPE_SET;
817 /* in push mode, we must delegate to upstream */
818 if (wav->streaming) {
819 gboolean res = FALSE;
821 /* if streaming not yet started; only prepare initial newsegment */
822 if (!event || wav->state != GST_WAVPARSE_DATA) {
823 if (wav->start_segment)
824 gst_event_unref (wav->start_segment);
826 gst_event_new_new_segment (FALSE, wav->segment.rate,
827 wav->segment.format, wav->segment.last_stop, wav->segment.duration,
828 wav->segment.last_stop);
831 /* convert seek positions to byte positions in data sections */
832 if (format == GST_FORMAT_TIME) {
833 /* should not fail */
834 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
836 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
839 /* mind sample boundary and header */
841 cur -= (cur % wav->bytes_per_sample);
842 cur += wav->datastart;
845 stop -= (stop % wav->bytes_per_sample);
846 stop += wav->datastart;
848 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
849 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
851 /* BYTE seek event */
852 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
854 res = gst_pad_push_event (wav->sinkpad, event);
860 flush = flags & GST_SEEK_FLAG_FLUSH;
862 /* now we need to make sure the streaming thread is stopped. We do this by
863 * either sending a FLUSH_START event downstream which will cause the
864 * streaming thread to stop with a WRONG_STATE.
865 * For a non-flushing seek we simply pause the task, which will happen as soon
866 * as it completes one iteration (and thus might block when the sink is
867 * blocking in preroll). */
870 GST_DEBUG_OBJECT (wav, "sending flush start");
871 gst_pad_push_event (wav->srcpad, gst_event_new_flush_start ());
874 gst_pad_pause_task (wav->sinkpad);
877 /* we should now be able to grab the streaming thread because we stopped it
878 * with the above flush/pause code */
879 GST_PAD_STREAM_LOCK (wav->sinkpad);
881 /* save current position */
882 last_stop = wav->segment.last_stop;
884 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
886 /* copy segment, we need this because we still need the old
887 * segment when we close the current segment. */
888 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
890 /* configure the seek parameters in the seeksegment. We will then have the
891 * right values in the segment to perform the seek */
893 GST_DEBUG_OBJECT (wav, "configuring seek");
894 gst_segment_set_seek (&seeksegment, rate, format, flags,
895 cur_type, cur, stop_type, stop, &update);
898 /* figure out the last position we need to play. If it's configured (stop !=
899 * -1), use that, else we play until the total duration of the file */
900 if ((stop = seeksegment.stop) == -1)
901 stop = seeksegment.duration;
903 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
904 if ((cur_type != GST_SEEK_TYPE_NONE)) {
905 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
906 * we can just copy the last_stop. If not, we use the bps to convert TIME to
908 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.last_stop,
909 (gint64 *) & wav->offset))
910 wav->offset = seeksegment.last_stop;
911 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
912 wav->offset -= (wav->offset % wav->bytes_per_sample);
913 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
914 wav->offset += wav->datastart;
915 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
917 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
921 if (stop_type != GST_SEEK_TYPE_NONE) {
922 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
923 wav->end_offset = stop;
924 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
925 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
926 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
927 wav->end_offset += wav->datastart;
928 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
930 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
934 /* make sure filesize is not exceeded due to rounding errors or so,
935 * same precaution as in _stream_headers */
936 bformat = GST_FORMAT_BYTES;
937 if (gst_pad_query_peer_duration (wav->sinkpad, &bformat, &upstream_size))
938 wav->end_offset = MIN (wav->end_offset, upstream_size);
940 /* this is the range of bytes we will use for playback */
941 wav->offset = MIN (wav->offset, wav->end_offset);
942 wav->dataleft = wav->end_offset - wav->offset;
944 GST_DEBUG_OBJECT (wav,
945 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
946 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
947 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
949 /* prepare for streaming again */
952 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
953 GST_DEBUG_OBJECT (wav, "sending flush stop");
954 gst_pad_push_event (wav->srcpad, gst_event_new_flush_stop ());
955 } else if (wav->segment_running) {
956 /* we are running the current segment and doing a non-flushing seek,
957 * close the segment first based on the previous last_stop. */
958 GST_DEBUG_OBJECT (wav, "closing running segment %" G_GINT64_FORMAT
959 " to %" G_GINT64_FORMAT, wav->segment.accum, wav->segment.last_stop);
961 /* queue the segment for sending in the stream thread */
962 if (wav->close_segment)
963 gst_event_unref (wav->close_segment);
964 wav->close_segment = gst_event_new_new_segment (TRUE,
965 wav->segment.rate, wav->segment.format,
966 wav->segment.accum, wav->segment.last_stop, wav->segment.accum);
968 /* keep track of our last_stop */
969 seeksegment.accum = wav->segment.last_stop;
973 /* now we did the seek and can activate the new segment values */
974 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
976 /* if we're doing a segment seek, post a SEGMENT_START message */
977 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
978 gst_element_post_message (GST_ELEMENT_CAST (wav),
979 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
980 wav->segment.format, wav->segment.last_stop));
983 /* now create the newsegment */
984 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
985 " to %" G_GINT64_FORMAT, wav->segment.last_stop, stop);
987 /* store the newsegment event so it can be sent from the streaming thread. */
988 if (wav->start_segment)
989 gst_event_unref (wav->start_segment);
991 gst_event_new_new_segment (FALSE, wav->segment.rate,
992 wav->segment.format, wav->segment.last_stop, stop,
993 wav->segment.last_stop);
995 /* mark discont if we are going to stream from another position. */
996 if (last_stop != wav->segment.last_stop) {
997 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
1001 /* and start the streaming task again */
1002 wav->segment_running = TRUE;
1003 if (!wav->streaming) {
1004 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
1008 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
1015 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
1020 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
1025 GST_DEBUG_OBJECT (wav,
1026 "Could not determine byte position for desired time");
1032 * gst_wavparse_peek_chunk_info:
1033 * @wav Wavparse object
1034 * @tag holder for tag
1035 * @size holder for tag size
1037 * Peek next chunk info (tag and size)
1039 * Returns: %TRUE when the chunk info (header) is available
1042 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
1044 const guint8 *data = NULL;
1046 if (gst_adapter_available (wav->adapter) < 8)
1049 data = gst_adapter_peek (wav->adapter, 8);
1050 *tag = GST_READ_UINT32_LE (data);
1051 *size = GST_READ_UINT32_LE (data + 4);
1053 GST_DEBUG ("Next chunk size is %d bytes, type %" GST_FOURCC_FORMAT, *size,
1054 GST_FOURCC_ARGS (*tag));
1060 * gst_wavparse_peek_chunk:
1061 * @wav Wavparse object
1062 * @tag holder for tag
1063 * @size holder for tag size
1065 * Peek enough data for one full chunk
1067 * Returns: %TRUE when the full chunk is available
1070 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
1072 guint32 peek_size = 0;
1075 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
1078 GST_DEBUG ("Need to peek chunk of %d bytes", *size);
1079 peek_size = (*size + 1) & ~1;
1081 available = gst_adapter_available (wav->adapter);
1082 if (available >= (8 + peek_size)) {
1085 GST_LOG ("but only %u bytes available now", available);
1091 * gst_wavparse_calculate_duration:
1092 * @wav: wavparse object
1094 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
1097 * Returns: %TRUE if duration is available.
1100 gst_wavparse_calculate_duration (GstWavParse * wav)
1102 if (wav->duration > 0)
1106 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
1108 uint64_ceiling_scale (wav->datasize, GST_SECOND, (guint64) wav->bps);
1109 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
1110 GST_TIME_ARGS (wav->duration));
1112 } else if (wav->fact) {
1113 wav->duration = uint64_ceiling_scale_int (GST_SECOND, wav->fact, wav->rate);
1114 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
1115 GST_TIME_ARGS (wav->duration));
1122 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
1127 if (wav->streaming) {
1128 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1131 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
1132 GST_FOURCC_ARGS (tag));
1133 flush = 8 + ((size + 1) & ~1);
1134 wav->offset += flush;
1135 if (wav->streaming) {
1136 gst_adapter_flush (wav->adapter, flush);
1138 gst_buffer_unref (buf);
1142 static GstFlowReturn
1143 gst_wavparse_stream_headers (GstWavParse * wav)
1147 gst_riff_strf_auds *header = NULL;
1149 gboolean gotdata = FALSE;
1151 gchar *codec_name = NULL;
1154 gint64 upstream_size = 0;
1156 /* search for "_fmt" chunk, which should be first */
1157 while (!wav->got_fmt) {
1160 /* The header starts with a 'fmt ' tag */
1161 if (wav->streaming) {
1162 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1165 gst_adapter_flush (wav->adapter, 8);
1168 buf = gst_adapter_take_buffer (wav->adapter, size);
1170 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1171 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1175 if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_bext ||
1176 tag == GST_RIFF_TAG_BEXT || tag == GST_RIFF_TAG_LIST) {
1177 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1178 GST_FOURCC_ARGS (tag));
1179 gst_buffer_unref (buf);
1184 if (tag != GST_RIFF_TAG_fmt)
1187 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1189 goto parse_header_error;
1191 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1193 /* do sanity checks of header fields */
1194 if (header->channels == 0)
1196 if (header->rate == 0)
1199 GST_DEBUG_OBJECT (wav, "creating the caps");
1201 /* Note: gst_riff_create_audio_caps might need to fix values in
1202 * the header header depending on the format, so call it first */
1203 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1207 gst_buffer_unref (extra);
1210 goto unknown_format;
1212 /* do more sanity checks of header fields
1213 * (these can be sanitized by gst_riff_create_audio_caps()
1215 wav->format = header->format;
1216 wav->rate = header->rate;
1217 wav->channels = header->channels;
1218 wav->blockalign = header->blockalign;
1219 wav->depth = header->size;
1220 wav->av_bps = header->av_bps;
1226 /* do format specific handling */
1227 switch (wav->format) {
1228 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1229 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1231 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1232 * bitrate inside the mpeg stream */
1233 GST_INFO ("resetting bps from %d to 0 for mp2/3", wav->av_bps);
1237 case GST_RIFF_WAVE_FORMAT_PCM:
1238 if (wav->blockalign > wav->channels * (guint) ceil (wav->depth / 8.0))
1239 goto invalid_blockalign;
1242 if (wav->av_bps > wav->blockalign * wav->rate)
1244 /* use the configured bps */
1245 wav->bps = wav->av_bps;
1249 wav->width = (wav->blockalign * 8) / wav->channels;
1250 wav->bytes_per_sample = wav->channels * wav->width / 8;
1252 if (wav->bytes_per_sample <= 0)
1253 goto no_bytes_per_sample;
1255 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1256 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1257 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1258 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1259 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1260 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1261 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1263 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1264 * formats). This will make the element output a BYTE format segment and
1265 * will not timestamp the outgoing buffers.
1267 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1269 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1271 /* create pad later so we can sniff the first few bytes
1272 * of the real data and correct our caps if necessary */
1273 gst_caps_replace (&wav->caps, caps);
1274 gst_caps_replace (&caps, NULL);
1276 wav->got_fmt = TRUE;
1279 wav->tags = gst_tag_list_new ();
1281 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1282 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1284 g_free (codec_name);
1290 bformat = GST_FORMAT_BYTES;
1291 gst_pad_query_peer_duration (wav->sinkpad, &bformat, &upstream_size);
1292 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1294 /* loop headers until we get data */
1296 if (wav->streaming) {
1297 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1301 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1302 &buf)) != GST_FLOW_OK)
1303 goto header_read_error;
1304 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
1305 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
1308 GST_INFO_OBJECT (wav,
1309 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT,
1310 GST_FOURCC_ARGS (tag), wav->offset);
1312 /* wav is a st00pid format, we don't know for sure where data starts.
1313 * So we have to go bit by bit until we find the 'data' header
1316 case GST_RIFF_TAG_data:{
1317 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %d", size);
1318 if (wav->streaming) {
1319 gst_adapter_flush (wav->adapter, 8);
1322 gst_buffer_unref (buf);
1325 wav->datastart = wav->offset;
1326 /* file might be truncated */
1327 if (upstream_size) {
1328 size = MIN (size, (upstream_size - wav->datastart));
1330 wav->datasize = (guint64) size;
1331 wav->dataleft = (guint64) size;
1332 wav->end_offset = size + wav->datastart;
1333 if (!wav->streaming) {
1334 /* We will continue parsing tags 'till end */
1335 wav->offset += size;
1337 GST_DEBUG_OBJECT (wav, "datasize = %d", size);
1340 case GST_RIFF_TAG_fact:{
1341 if (wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1342 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1343 const guint data_size = 4;
1345 /* number of samples (for compressed formats) */
1346 if (wav->streaming) {
1347 const guint8 *data = NULL;
1349 if (gst_adapter_available (wav->adapter) < 8 + data_size) {
1352 gst_adapter_flush (wav->adapter, 8);
1353 data = gst_adapter_peek (wav->adapter, data_size);
1354 wav->fact = GST_READ_UINT32_LE (data);
1355 gst_adapter_flush (wav->adapter, data_size);
1357 gst_buffer_unref (buf);
1359 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1360 data_size, &buf)) != GST_FLOW_OK)
1361 goto header_read_error;
1362 wav->fact = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
1363 gst_buffer_unref (buf);
1365 GST_DEBUG_OBJECT (wav, "have fact %u", wav->fact);
1366 wav->offset += 8 + data_size;
1369 gst_waveparse_ignore_chunk (wav, buf, tag, size);
1373 case GST_RIFF_TAG_acid:{
1374 const gst_riff_acid *acid = NULL;
1375 const guint data_size = sizeof (gst_riff_acid);
1377 if (wav->streaming) {
1378 if (gst_adapter_available (wav->adapter) < 8 + data_size) {
1381 gst_adapter_flush (wav->adapter, 8);
1382 acid = (const gst_riff_acid *) gst_adapter_peek (wav->adapter,
1385 gst_buffer_unref (buf);
1387 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1388 data_size, &buf)) != GST_FLOW_OK)
1389 goto header_read_error;
1390 acid = (const gst_riff_acid *) GST_BUFFER_DATA (buf);
1392 GST_INFO_OBJECT (wav, "Have acid chunk");
1393 /* send data as tags */
1395 wav->tags = gst_tag_list_new ();
1396 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1397 GST_TAG_BEATS_PER_MINUTE, acid->tempo, NULL);
1399 if (wav->streaming) {
1400 gst_adapter_flush (wav->adapter, data_size);
1402 gst_buffer_unref (buf);
1403 wav->offset += 8 + data_size;
1407 /* FIXME: all list tags after data are ignored in streaming mode */
1408 case GST_RIFF_TAG_LIST:{
1411 if (wav->streaming) {
1412 const guint8 *data = NULL;
1414 if (gst_adapter_available (wav->adapter) < 12) {
1417 data = gst_adapter_peek (wav->adapter, 12);
1418 ltag = GST_READ_UINT32_LE (data + 8);
1420 gst_buffer_unref (buf);
1422 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1423 &buf)) != GST_FLOW_OK)
1424 goto header_read_error;
1425 ltag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 8);
1428 case GST_RIFF_LIST_INFO:{
1429 const guint data_size = size - 4;
1432 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1433 if (wav->streaming) {
1434 gst_adapter_flush (wav->adapter, 12);
1435 if (gst_adapter_available (wav->adapter) < data_size) {
1438 gst_buffer_unref (buf);
1440 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1443 gst_buffer_unref (buf);
1444 if (data_size > 0) {
1446 gst_pad_pull_range (wav->sinkpad, wav->offset,
1447 data_size, &buf)) != GST_FLOW_OK)
1448 goto header_read_error;
1451 if (data_size > 0) {
1453 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1455 GstTagList *old = wav->tags;
1457 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1459 gst_tag_list_free (old);
1460 gst_tag_list_free (new);
1462 if (wav->streaming) {
1463 gst_adapter_flush (wav->adapter, data_size);
1465 gst_buffer_unref (buf);
1466 wav->offset += data_size;
1472 GST_INFO_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1473 GST_FOURCC_ARGS (ltag));
1474 gst_waveparse_ignore_chunk (wav, buf, tag, size);
1480 gst_waveparse_ignore_chunk (wav, buf, tag, size);
1483 if (upstream_size && (wav->offset >= upstream_size)) {
1484 /* Now we are gone through the whole file */
1489 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1491 if (wav->bps <= 0 && wav->fact) {
1493 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1495 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1496 (guint64) wav->fact);
1497 GST_INFO_OBJECT (wav, "calculated bps : %d, enabling VBR", wav->bps);
1502 if (gst_wavparse_calculate_duration (wav)) {
1503 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1504 gst_segment_set_duration (&wav->segment, GST_FORMAT_TIME, wav->duration);
1506 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1507 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1508 gst_segment_set_duration (&wav->segment, GST_FORMAT_BYTES, wav->datasize);
1511 /* now we have all the info to perform a pending seek if any, if no
1512 * event, this will still do the right thing and it will also send
1513 * the right newsegment event downstream. */
1514 gst_wavparse_perform_seek (wav, wav->seek_event);
1515 /* remove pending event */
1516 event_p = &wav->seek_event;
1517 gst_event_replace (event_p, NULL);
1519 /* we just started, we are discont */
1520 wav->discont = TRUE;
1522 wav->state = GST_WAVPARSE_DATA;
1529 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1530 ("Invalid WAV header (no fmt at start): %"
1531 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1532 g_free (codec_name);
1533 return GST_FLOW_ERROR;
1537 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1538 ("Couldn't parse audio header"));
1539 g_free (codec_name);
1540 return GST_FLOW_ERROR;
1544 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1545 ("Stream claims to contain no channels - invalid data"));
1547 g_free (codec_name);
1548 return GST_FLOW_ERROR;
1552 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1553 ("Stream with sample_rate == 0 - invalid data"));
1555 g_free (codec_name);
1556 return GST_FLOW_ERROR;
1560 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1561 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1562 wav->blockalign, wav->channels * (guint) ceil (wav->depth / 8.0)));
1563 g_free (codec_name);
1564 return GST_FLOW_ERROR;
1568 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1569 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1570 wav->av_bps, wav->blockalign * wav->rate));
1571 g_free (codec_name);
1572 return GST_FLOW_ERROR;
1574 no_bytes_per_sample:
1576 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1577 ("Could not caluclate bytes per sample - invalid data"));
1578 g_free (codec_name);
1579 return GST_FLOW_ERROR;
1583 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1584 ("No caps found for format 0x%x, %d channels, %d Hz",
1585 wav->format, wav->channels, wav->rate));
1587 g_free (codec_name);
1588 return GST_FLOW_ERROR;
1592 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1593 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1594 g_free (codec_name);
1595 return GST_FLOW_ERROR;
1600 * Read WAV file tag when streaming
1602 static GstFlowReturn
1603 gst_wavparse_parse_stream_init (GstWavParse * wav)
1605 if (gst_adapter_available (wav->adapter) >= 12) {
1608 /* _take flushes the data */
1609 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1611 GST_DEBUG ("Parsing wav header");
1612 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1613 return GST_FLOW_ERROR;
1616 /* Go to next state */
1617 wav->state = GST_WAVPARSE_HEADER;
1622 /* handle an event sent directly to the element.
1624 * This event can be sent either in the READY state or the
1625 * >READY state. The only event of interest really is the seek
1628 * In the READY state we can only store the event and try to
1629 * respect it when going to PAUSED. We assume we are in the
1630 * READY state when our parsing state != GST_WAVPARSE_DATA.
1632 * When we are steaming, we can simply perform the seek right
1636 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1638 GstWavParse *wav = GST_WAVPARSE (element);
1639 gboolean res = FALSE;
1642 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1644 switch (GST_EVENT_TYPE (event)) {
1645 case GST_EVENT_SEEK:
1646 if (wav->state == GST_WAVPARSE_DATA) {
1647 /* we can handle the seek directly when streaming data */
1648 res = gst_wavparse_perform_seek (wav, event);
1650 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1652 event_p = &wav->seek_event;
1653 gst_event_replace (event_p, event);
1655 /* we always return true */
1662 gst_event_unref (event);
1667 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1670 const guint8 dts_marker[] = { 0xFF, 0x1F, 0x00, 0xE8, 0xF1, 0x07 };
1672 GST_DEBUG_OBJECT (wav, "adding src pad");
1675 s = gst_caps_get_structure (wav->caps, 0);
1676 if (s && gst_structure_has_name (s, "audio/x-raw-int") && buf &&
1677 GST_BUFFER_SIZE (buf) > 6 &&
1678 memcmp (GST_BUFFER_DATA (buf), dts_marker, 6) == 0) {
1680 GST_WARNING_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1681 gst_caps_unref (wav->caps);
1682 wav->caps = gst_caps_from_string ("audio/x-dts");
1684 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1685 GST_TAG_AUDIO_CODEC, "dts", NULL);
1689 gst_wavparse_create_sourcepad (wav);
1690 gst_pad_set_active (wav->srcpad, TRUE);
1691 gst_pad_set_caps (wav->srcpad, wav->caps);
1692 gst_caps_replace (&wav->caps, NULL);
1694 gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
1695 gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
1697 if (wav->close_segment) {
1698 GST_DEBUG_OBJECT (wav, "Send close segment event on newpad");
1699 gst_pad_push_event (wav->srcpad, wav->close_segment);
1700 wav->close_segment = NULL;
1702 if (wav->start_segment) {
1703 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1704 gst_pad_push_event (wav->srcpad, wav->start_segment);
1705 wav->start_segment = NULL;
1709 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (wav), wav->srcpad,
1715 #define MAX_BUFFER_SIZE 4096
1717 static GstFlowReturn
1718 gst_wavparse_stream_data (GstWavParse * wav)
1720 GstBuffer *buf = NULL;
1721 GstFlowReturn res = GST_FLOW_OK;
1722 guint64 desired, obtained;
1723 GstClockTime timestamp, next_timestamp, duration;
1724 guint64 pos, nextpos;
1727 GST_LOG_OBJECT (wav,
1728 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1729 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1731 /* Get the next n bytes and output them */
1732 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1735 /* scale the amount of data by the segment rate so we get equal
1736 * amounts of data regardless of the playback rate */
1738 MIN (gst_guint64_to_gdouble (wav->dataleft),
1739 MAX_BUFFER_SIZE * wav->segment.abs_rate);
1741 if (desired >= wav->blockalign && wav->blockalign > 0)
1742 desired -= (desired % wav->blockalign);
1744 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1745 "from the sinkpad", desired);
1747 if (wav->streaming) {
1748 guint avail = gst_adapter_available (wav->adapter);
1751 /* flush some bytes if evil upstream sends segment that starts
1752 * before data or does is not send sample aligned segment */
1753 if (G_LIKELY (wav->offset >= wav->datastart)) {
1754 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1756 extra = wav->datastart - wav->offset;
1759 if (G_UNLIKELY (extra)) {
1760 extra = wav->bytes_per_sample - extra;
1761 if (extra <= avail) {
1762 GST_DEBUG_OBJECT (wav, "flushing %d bytes to sample boundary", extra);
1763 gst_adapter_flush (wav->adapter, extra);
1764 wav->offset += extra;
1765 wav->dataleft -= extra;
1766 goto iterate_adapter;
1768 GST_DEBUG_OBJECT (wav, "flushing %d bytes", avail);
1769 gst_adapter_clear (wav->adapter);
1770 wav->offset += avail;
1771 wav->dataleft -= avail;
1776 if (avail < desired) {
1777 GST_LOG_OBJECT (wav, "Got only %d bytes of data from the sinkpad", avail);
1781 buf = gst_adapter_take_buffer (wav->adapter, desired);
1783 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
1784 desired, &buf)) != GST_FLOW_OK)
1788 /* first chunk of data? create the source pad. We do this only here so
1789 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
1790 if (G_UNLIKELY (wav->first)) {
1792 /* this will also push the segment events */
1793 gst_wavparse_add_src_pad (wav, buf);
1795 /* If we have a pending close/start segment, send it now. */
1796 if (G_UNLIKELY (wav->close_segment != NULL)) {
1797 gst_pad_push_event (wav->srcpad, wav->close_segment);
1798 wav->close_segment = NULL;
1800 if (G_UNLIKELY (wav->start_segment != NULL)) {
1801 gst_pad_push_event (wav->srcpad, wav->start_segment);
1802 wav->start_segment = NULL;
1806 obtained = GST_BUFFER_SIZE (buf);
1808 /* our positions in bytes */
1809 pos = wav->offset - wav->datastart;
1810 nextpos = pos + obtained;
1812 /* update offsets, does not overflow. */
1813 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
1814 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
1817 /* and timestamps if we have a bitrate, be careful for overflows */
1818 timestamp = uint64_ceiling_scale (pos, GST_SECOND, (guint64) wav->bps);
1820 uint64_ceiling_scale (nextpos, GST_SECOND, (guint64) wav->bps);
1821 duration = next_timestamp - timestamp;
1823 /* update current running segment position */
1824 gst_segment_set_last_stop (&wav->segment, GST_FORMAT_TIME, next_timestamp);
1825 } else if (wav->fact) {
1827 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
1828 /* and timestamps if we have a bitrate, be careful for overflows */
1829 timestamp = uint64_ceiling_scale (pos, GST_SECOND, bps);
1830 next_timestamp = uint64_ceiling_scale (nextpos, GST_SECOND, bps);
1831 duration = next_timestamp - timestamp;
1833 /* no bitrate, all we know is that the first sample has timestamp 0, all
1834 * other positions and durations have unknown timestamp. */
1838 timestamp = GST_CLOCK_TIME_NONE;
1839 duration = GST_CLOCK_TIME_NONE;
1840 /* update current running segment position with byte offset */
1841 gst_segment_set_last_stop (&wav->segment, GST_FORMAT_BYTES, nextpos);
1843 if ((pos > 0) && wav->vbr) {
1844 /* don't set timestamps for VBR files if it's not the first buffer */
1845 timestamp = GST_CLOCK_TIME_NONE;
1846 duration = GST_CLOCK_TIME_NONE;
1849 GST_DEBUG_OBJECT (wav, "marking DISCONT");
1850 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1851 wav->discont = FALSE;
1854 GST_BUFFER_TIMESTAMP (buf) = timestamp;
1855 GST_BUFFER_DURATION (buf) = duration;
1857 /* don't forget to set the caps on the buffer */
1858 gst_buffer_set_caps (buf, GST_PAD_CAPS (wav->srcpad));
1860 GST_LOG_OBJECT (wav,
1861 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
1862 ", size:%u", GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration),
1863 GST_BUFFER_SIZE (buf));
1865 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
1868 if (obtained < wav->dataleft) {
1869 wav->offset += obtained;
1870 wav->dataleft -= obtained;
1872 wav->offset += wav->dataleft;
1876 /* Iterate until need more data, so adapter size won't grow */
1877 if (wav->streaming) {
1878 GST_LOG_OBJECT (wav,
1879 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
1881 goto iterate_adapter;
1888 GST_DEBUG_OBJECT (wav, "found EOS");
1889 return GST_FLOW_UNEXPECTED;
1893 /* check if we got EOS */
1894 if (res == GST_FLOW_UNEXPECTED)
1897 GST_WARNING_OBJECT (wav,
1898 "Error getting %" G_GINT64_FORMAT " bytes from the "
1899 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
1904 GST_INFO_OBJECT (wav,
1905 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
1906 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
1907 gst_pad_is_linked (wav->srcpad));
1913 gst_wavparse_loop (GstPad * pad)
1916 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
1918 GST_LOG_OBJECT (wav, "process data");
1920 switch (wav->state) {
1921 case GST_WAVPARSE_START:
1922 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
1923 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
1926 wav->state = GST_WAVPARSE_HEADER;
1929 case GST_WAVPARSE_HEADER:
1930 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
1931 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
1934 wav->state = GST_WAVPARSE_DATA;
1935 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
1938 case GST_WAVPARSE_DATA:
1939 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
1943 g_assert_not_reached ();
1950 const gchar *reason = gst_flow_get_name (ret);
1952 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
1953 wav->segment_running = FALSE;
1954 gst_pad_pause_task (pad);
1956 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
1957 if (ret == GST_FLOW_UNEXPECTED) {
1958 /* add pad before we perform EOS */
1959 if (G_UNLIKELY (wav->first)) {
1961 gst_wavparse_add_src_pad (wav, NULL);
1963 /* perform EOS logic */
1964 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1967 if ((stop = wav->segment.stop) == -1)
1968 stop = wav->segment.duration;
1970 gst_element_post_message (GST_ELEMENT_CAST (wav),
1971 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
1972 wav->segment.format, stop));
1974 if (wav->srcpad != NULL)
1975 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
1978 /* for fatal errors we post an error message, post the error
1979 * first so the app knows about the error first. */
1980 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
1981 (_("Internal data flow error.")),
1982 ("streaming task paused, reason %s (%d)", reason, ret));
1983 if (wav->srcpad != NULL)
1984 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
1991 static GstFlowReturn
1992 gst_wavparse_chain (GstPad * pad, GstBuffer * buf)
1995 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
1997 GST_LOG_OBJECT (wav, "adapter_push %u bytes", GST_BUFFER_SIZE (buf));
1999 gst_adapter_push (wav->adapter, buf);
2001 switch (wav->state) {
2002 case GST_WAVPARSE_START:
2003 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2004 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2007 if (wav->state != GST_WAVPARSE_HEADER)
2010 /* otherwise fall-through */
2011 case GST_WAVPARSE_HEADER:
2012 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2013 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2016 if (!wav->got_fmt || wav->datastart == 0)
2019 wav->state = GST_WAVPARSE_DATA;
2020 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2023 case GST_WAVPARSE_DATA:
2024 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2025 wav->discont = TRUE;
2026 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2030 g_return_val_if_reached (GST_FLOW_ERROR);
2036 static GstFlowReturn
2037 gst_wavparse_flush_data (GstWavParse * wav)
2039 GstFlowReturn ret = GST_FLOW_OK;
2042 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2044 wav->end_offset = wav->offset + av;
2045 ret = gst_wavparse_stream_data (wav);
2052 gst_wavparse_sink_event (GstPad * pad, GstEvent * event)
2054 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2055 gboolean ret = TRUE;
2057 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2059 switch (GST_EVENT_TYPE (event)) {
2060 case GST_EVENT_NEWSEGMENT:
2063 gdouble rate, arate;
2064 gint64 start, stop, time, offset = 0, end_offset = -1;
2068 /* some debug output */
2069 gst_segment_init (&segment, GST_FORMAT_UNDEFINED);
2070 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
2071 &start, &stop, &time);
2072 gst_segment_set_newsegment_full (&segment, update, rate, arate, format,
2074 GST_DEBUG_OBJECT (wav,
2075 "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
2078 if (wav->state != GST_WAVPARSE_DATA) {
2079 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2083 /* now we are either committed to TIME or BYTE format,
2084 * and we only expect a BYTE segment, e.g. following a seek */
2085 if (format == GST_FORMAT_BYTES) {
2088 start -= wav->datastart;
2089 start = MAX (start, 0);
2093 stop -= wav->datastart;
2094 stop = MAX (stop, 0);
2096 if (wav->segment.format == GST_FORMAT_TIME) {
2097 guint64 bps = wav->bps;
2099 /* operating in format TIME, so we can convert */
2100 if (!bps && wav->fact)
2102 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2106 uint64_ceiling_scale (start, GST_SECOND, (guint64) wav->bps);
2109 uint64_ceiling_scale (stop, GST_SECOND, (guint64) wav->bps);
2113 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2117 /* accept upstream's notion of segment and distribute along */
2118 gst_segment_set_newsegment_full (&wav->segment, update, rate, arate,
2119 wav->segment.format, start, stop, start);
2120 /* also store the newsegment event for the streaming thread */
2121 if (wav->start_segment)
2122 gst_event_unref (wav->start_segment);
2123 wav->start_segment =
2124 gst_event_new_new_segment_full (update, rate, arate,
2125 wav->segment.format, start, stop, start);
2126 GST_DEBUG_OBJECT (wav, "Pushing newseg update %d, rate %g, "
2127 "applied rate %g, format %d, start %" G_GINT64_FORMAT ", "
2128 "stop %" G_GINT64_FORMAT, update, rate, arate, wav->segment.format,
2131 /* stream leftover data in current segment */
2132 gst_wavparse_flush_data (wav);
2133 /* and set up streaming thread for next one */
2134 wav->offset = offset;
2135 wav->end_offset = end_offset;
2136 if (wav->end_offset > 0) {
2137 wav->dataleft = wav->end_offset - wav->offset;
2139 /* infinity; upstream will EOS when done */
2140 wav->dataleft = G_MAXUINT64;
2143 gst_event_unref (event);
2147 /* stream leftover data in current segment */
2148 gst_wavparse_flush_data (wav);
2150 case GST_EVENT_FLUSH_STOP:
2151 gst_adapter_clear (wav->adapter);
2152 wav->discont = TRUE;
2155 ret = gst_pad_event_default (wav->sinkpad, event);
2163 /* convert and query stuff */
2164 static const GstFormat *
2165 gst_wavparse_get_formats (GstPad * pad)
2167 static GstFormat formats[] = {
2170 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2179 gst_wavparse_pad_convert (GstPad * pad,
2180 GstFormat src_format, gint64 src_value,
2181 GstFormat * dest_format, gint64 * dest_value)
2183 GstWavParse *wavparse;
2184 gboolean res = TRUE;
2186 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2188 if (*dest_format == src_format) {
2189 *dest_value = src_value;
2193 if ((wavparse->bps == 0) && !wavparse->fact)
2196 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2197 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2199 switch (src_format) {
2200 case GST_FORMAT_BYTES:
2201 switch (*dest_format) {
2202 case GST_FORMAT_DEFAULT:
2203 *dest_value = src_value / wavparse->bytes_per_sample;
2204 /* make sure we end up on a sample boundary */
2205 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2207 case GST_FORMAT_TIME:
2208 /* src_value + datastart = offset */
2209 GST_INFO_OBJECT (wavparse,
2210 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2212 if (wavparse->bps > 0)
2213 *dest_value = uint64_ceiling_scale (src_value, GST_SECOND,
2214 (guint64) wavparse->bps);
2215 else if (wavparse->fact) {
2216 guint64 bps = uint64_ceiling_scale_int (wavparse->datasize,
2217 wavparse->rate, wavparse->fact);
2219 *dest_value = uint64_ceiling_scale_int (src_value, GST_SECOND, bps);
2230 case GST_FORMAT_DEFAULT:
2231 switch (*dest_format) {
2232 case GST_FORMAT_BYTES:
2233 *dest_value = src_value * wavparse->bytes_per_sample;
2235 case GST_FORMAT_TIME:
2236 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2237 (guint64) wavparse->rate);
2245 case GST_FORMAT_TIME:
2246 switch (*dest_format) {
2247 case GST_FORMAT_BYTES:
2248 if (wavparse->bps > 0)
2249 *dest_value = gst_util_uint64_scale (src_value,
2250 (guint64) wavparse->bps, GST_SECOND);
2252 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2253 wavparse->rate, wavparse->fact);
2255 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2257 /* make sure we end up on a sample boundary */
2258 *dest_value -= *dest_value % wavparse->blockalign;
2260 case GST_FORMAT_DEFAULT:
2261 *dest_value = gst_util_uint64_scale (src_value,
2262 (guint64) wavparse->rate, GST_SECOND);
2281 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2287 static const GstQueryType *
2288 gst_wavparse_get_query_types (GstPad * pad)
2290 static const GstQueryType types[] = {
2301 /* handle queries for location and length in requested format */
2303 gst_wavparse_pad_query (GstPad * pad, GstQuery * query)
2305 gboolean res = TRUE;
2306 GstWavParse *wav = GST_WAVPARSE (gst_pad_get_parent (pad));
2308 /* only if we know */
2309 if (wav->state != GST_WAVPARSE_DATA) {
2310 gst_object_unref (wav);
2314 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2316 switch (GST_QUERY_TYPE (query)) {
2317 case GST_QUERY_POSITION:
2323 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2324 curb = wav->offset - wav->datastart;
2325 gst_query_parse_position (query, &format, NULL);
2326 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2329 case GST_FORMAT_TIME:
2330 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2334 format = GST_FORMAT_BYTES;
2339 gst_query_set_position (query, format, cur);
2342 case GST_QUERY_DURATION:
2344 gint64 duration = 0;
2347 gst_query_parse_duration (query, &format, NULL);
2350 case GST_FORMAT_TIME:{
2351 if ((res = gst_wavparse_calculate_duration (wav))) {
2352 duration = wav->duration;
2357 format = GST_FORMAT_BYTES;
2358 duration = wav->datasize;
2361 gst_query_set_duration (query, format, duration);
2364 case GST_QUERY_CONVERT:
2366 gint64 srcvalue, dstvalue;
2367 GstFormat srcformat, dstformat;
2369 gst_query_parse_convert (query, &srcformat, &srcvalue,
2370 &dstformat, &dstvalue);
2371 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2372 &dstformat, &dstvalue);
2374 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2377 case GST_QUERY_SEEKING:{
2379 gboolean seekable = FALSE;
2381 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2382 if (fmt == wav->segment.format) {
2383 if (wav->streaming) {
2386 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2387 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2388 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2389 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2391 gst_query_unref (q);
2393 GST_LOG_OBJECT (wav, "looping => seekable");
2397 } else if (fmt == GST_FORMAT_TIME) {
2401 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2406 res = gst_pad_query_default (pad, query);
2409 gst_object_unref (wav);
2414 gst_wavparse_srcpad_event (GstPad * pad, GstEvent * event)
2416 GstWavParse *wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
2417 gboolean res = FALSE;
2419 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2421 switch (GST_EVENT_TYPE (event)) {
2422 case GST_EVENT_SEEK:
2423 /* can only handle events when we are in the data state */
2424 if (wavparse->state == GST_WAVPARSE_DATA) {
2425 res = gst_wavparse_perform_seek (wavparse, event);
2427 gst_event_unref (event);
2430 res = gst_pad_push_event (wavparse->sinkpad, event);
2433 gst_object_unref (wavparse);
2438 gst_wavparse_sink_activate (GstPad * sinkpad)
2440 GstWavParse *wav = GST_WAVPARSE (gst_pad_get_parent (sinkpad));
2444 gst_adapter_clear (wav->adapter);
2445 g_object_unref (wav->adapter);
2446 wav->adapter = NULL;
2449 if (gst_pad_check_pull_range (sinkpad)) {
2450 GST_DEBUG ("going to pull mode");
2451 wav->streaming = FALSE;
2452 res = gst_pad_activate_pull (sinkpad, TRUE);
2454 GST_DEBUG ("going to push (streaming) mode");
2455 wav->streaming = TRUE;
2456 wav->adapter = gst_adapter_new ();
2457 res = gst_pad_activate_push (sinkpad, TRUE);
2459 gst_object_unref (wav);
2465 gst_wavparse_sink_activate_pull (GstPad * sinkpad, gboolean active)
2467 GstWavParse *wav = GST_WAVPARSE (GST_OBJECT_PARENT (sinkpad));
2470 /* if we have a scheduler we can start the task */
2471 wav->segment_running = TRUE;
2472 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2475 wav->segment_running = FALSE;
2476 return gst_pad_stop_task (sinkpad);
2480 static GstStateChangeReturn
2481 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2483 GstStateChangeReturn ret;
2484 GstWavParse *wav = GST_WAVPARSE (element);
2486 switch (transition) {
2487 case GST_STATE_CHANGE_NULL_TO_READY:
2489 case GST_STATE_CHANGE_READY_TO_PAUSED:
2490 gst_wavparse_reset (wav);
2492 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2498 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2500 switch (transition) {
2501 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2503 case GST_STATE_CHANGE_PAUSED_TO_READY:
2504 gst_wavparse_destroy_sourcepad (wav);
2505 gst_wavparse_reset (wav);
2507 case GST_STATE_CHANGE_READY_TO_NULL:
2516 plugin_init (GstPlugin * plugin)
2520 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2524 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2527 "Parse a .wav file into raw audio",
2528 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)