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
56 #include "gstwavparse.h"
57 #include "gst/riff/riff-ids.h"
58 #include "gst/riff/riff-media.h"
59 #include <gst/base/gsttypefindhelper.h>
60 #include <gst/gst-i18n-plugin.h>
62 GST_DEBUG_CATEGORY_STATIC (wavparse_debug);
63 #define GST_CAT_DEFAULT (wavparse_debug)
65 static void gst_wavparse_dispose (GObject * object);
67 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad,
69 static gboolean gst_wavparse_sink_activate_mode (GstPad * sinkpad,
70 GstObject * parent, GstPadMode mode, gboolean active);
71 static gboolean gst_wavparse_send_event (GstElement * element,
73 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
74 GstStateChange transition);
76 static gboolean gst_wavparse_pad_query (GstPad * pad, GstObject * parent,
78 static gboolean gst_wavparse_pad_convert (GstPad * pad, GstFormat src_format,
79 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
81 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstObject * parent,
83 static gboolean gst_wavparse_sink_event (GstPad * pad, GstObject * parent,
85 static void gst_wavparse_loop (GstPad * pad);
86 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent,
89 static void gst_wavparse_set_property (GObject * object, guint prop_id,
90 const GValue * value, GParamSpec * pspec);
91 static void gst_wavparse_get_property (GObject * object, guint prop_id,
92 GValue * value, GParamSpec * pspec);
94 #define DEFAULT_IGNORE_LENGTH FALSE
102 static GstStaticPadTemplate sink_template_factory =
103 GST_STATIC_PAD_TEMPLATE ("sink",
106 GST_STATIC_CAPS ("audio/x-wav")
110 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
112 #define gst_wavparse_parent_class parent_class
113 G_DEFINE_TYPE_WITH_CODE (GstWavParse, gst_wavparse, GST_TYPE_ELEMENT,
117 gst_wavparse_class_init (GstWavParseClass * klass)
119 GstElementClass *gstelement_class;
120 GObjectClass *object_class;
121 GstPadTemplate *src_template;
123 gstelement_class = (GstElementClass *) klass;
124 object_class = (GObjectClass *) klass;
126 parent_class = g_type_class_peek_parent (klass);
128 object_class->dispose = gst_wavparse_dispose;
130 object_class->set_property = gst_wavparse_set_property;
131 object_class->get_property = gst_wavparse_get_property;
134 * GstWavParse:ignore-length
136 * This selects whether the length found in a data chunk
137 * should be ignored. This may be useful for streamed audio
138 * where the length is unknown until the end of streaming,
139 * and various software/hardware just puts some random value
140 * in there and hopes it doesn't break too much.
144 g_object_class_install_property (object_class, PROP_IGNORE_LENGTH,
145 g_param_spec_boolean ("ignore-length",
147 "Ignore length from the Wave header",
148 DEFAULT_IGNORE_LENGTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
151 gstelement_class->change_state = gst_wavparse_change_state;
152 gstelement_class->send_event = gst_wavparse_send_event;
155 gst_element_class_add_pad_template (gstelement_class,
156 gst_static_pad_template_get (&sink_template_factory));
158 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
159 GST_PAD_ALWAYS, gst_riff_create_audio_template_caps ());
160 gst_element_class_add_pad_template (gstelement_class, src_template);
162 gst_element_class_set_static_metadata (gstelement_class, "WAV audio demuxer",
163 "Codec/Demuxer/Audio",
164 "Parse a .wav file into raw audio",
165 "Erik Walthinsen <omega@cse.ogi.edu>");
169 gst_wavparse_reset (GstWavParse * wav)
171 wav->state = GST_WAVPARSE_START;
173 /* These will all be set correctly in the fmt chunk */
187 wav->got_fmt = FALSE;
191 gst_event_unref (wav->seek_event);
192 wav->seek_event = NULL;
194 gst_adapter_clear (wav->adapter);
195 g_object_unref (wav->adapter);
199 gst_tag_list_free (wav->tags);
202 gst_caps_unref (wav->caps);
204 if (wav->start_segment)
205 gst_event_unref (wav->start_segment);
206 wav->start_segment = NULL;
210 gst_wavparse_dispose (GObject * object)
212 GstWavParse *wav = GST_WAVPARSE (object);
214 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
215 gst_wavparse_reset (wav);
217 G_OBJECT_CLASS (parent_class)->dispose (object);
221 gst_wavparse_init (GstWavParse * wavparse)
223 gst_wavparse_reset (wavparse);
227 gst_pad_new_from_static_template (&sink_template_factory, "sink");
228 gst_pad_set_activate_function (wavparse->sinkpad,
229 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
230 gst_pad_set_activatemode_function (wavparse->sinkpad,
231 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_mode));
232 gst_pad_set_chain_function (wavparse->sinkpad,
233 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
234 gst_pad_set_event_function (wavparse->sinkpad,
235 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
236 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
240 gst_pad_new_from_template (gst_element_class_get_pad_template
241 (GST_ELEMENT_GET_CLASS (wavparse), "src"), "src");
242 gst_pad_use_fixed_caps (wavparse->srcpad);
243 gst_pad_set_query_function (wavparse->srcpad,
244 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
245 gst_pad_set_event_function (wavparse->srcpad,
246 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
247 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
250 /* FIXME: why is that not in use? */
253 gst_wavparse_parse_adtl (GstWavParse * wavparse, int len)
256 GstByteStream *bs = wavparse->bs;
257 gst_riff_chunk *temp_chunk, chunk;
259 struct _gst_riff_labl labl, *temp_labl;
260 struct _gst_riff_ltxt ltxt, *temp_ltxt;
261 struct _gst_riff_note note, *temp_note;
264 GstPropsEntry *entry;
268 props = wavparse->metadata->properties;
272 gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
273 if (got_bytes != sizeof (gst_riff_chunk)) {
276 temp_chunk = (gst_riff_chunk *) tempdata;
278 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
279 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
281 if (chunk.size == 0) {
282 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
283 len -= sizeof (gst_riff_chunk);
288 case GST_RIFF_adtl_labl:
290 gst_bytestream_peek_bytes (bs, &tempdata,
291 sizeof (struct _gst_riff_labl));
292 if (got_bytes != sizeof (struct _gst_riff_labl)) {
296 temp_labl = (struct _gst_riff_labl *) tempdata;
297 labl.id = GUINT32_FROM_LE (temp_labl->id);
298 labl.size = GUINT32_FROM_LE (temp_labl->size);
299 labl.identifier = GUINT32_FROM_LE (temp_labl->identifier);
301 gst_bytestream_flush (bs, sizeof (struct _gst_riff_labl));
302 len -= sizeof (struct _gst_riff_labl);
304 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, labl.size - 4);
305 if (got_bytes != labl.size - 4) {
309 label_name = (char *) tempdata;
311 gst_bytestream_flush (bs, ((labl.size - 4) + 1) & ~1);
312 len -= (((labl.size - 4) + 1) & ~1);
314 new_caps = gst_caps_new ("label",
315 "application/x-gst-metadata",
316 gst_props_new ("identifier", G_TYPE_INT (labl.identifier),
317 "name", G_TYPE_STRING (label_name), NULL));
319 if (gst_props_get (props, "labels", &caps, NULL)) {
320 caps = g_list_append (caps, new_caps);
322 caps = g_list_append (NULL, new_caps);
324 entry = gst_props_entry_new ("labels", GST_PROPS_GLIST (caps));
325 gst_props_add_entry (props, entry);
330 case GST_RIFF_adtl_ltxt:
332 gst_bytestream_peek_bytes (bs, &tempdata,
333 sizeof (struct _gst_riff_ltxt));
334 if (got_bytes != sizeof (struct _gst_riff_ltxt)) {
338 temp_ltxt = (struct _gst_riff_ltxt *) tempdata;
339 ltxt.id = GUINT32_FROM_LE (temp_ltxt->id);
340 ltxt.size = GUINT32_FROM_LE (temp_ltxt->size);
341 ltxt.identifier = GUINT32_FROM_LE (temp_ltxt->identifier);
342 ltxt.length = GUINT32_FROM_LE (temp_ltxt->length);
343 ltxt.purpose = GUINT32_FROM_LE (temp_ltxt->purpose);
344 ltxt.country = GUINT16_FROM_LE (temp_ltxt->country);
345 ltxt.language = GUINT16_FROM_LE (temp_ltxt->language);
346 ltxt.dialect = GUINT16_FROM_LE (temp_ltxt->dialect);
347 ltxt.codepage = GUINT16_FROM_LE (temp_ltxt->codepage);
349 gst_bytestream_flush (bs, sizeof (struct _gst_riff_ltxt));
350 len -= sizeof (struct _gst_riff_ltxt);
352 if (ltxt.size - 20 > 0) {
353 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, ltxt.size - 20);
354 if (got_bytes != ltxt.size - 20) {
358 gst_bytestream_flush (bs, ((ltxt.size - 20) + 1) & ~1);
359 len -= (((ltxt.size - 20) + 1) & ~1);
361 label_name = (char *) tempdata;
366 new_caps = gst_caps_new ("ltxt",
367 "application/x-gst-metadata",
368 gst_props_new ("identifier", G_TYPE_INT (ltxt.identifier),
369 "name", G_TYPE_STRING (label_name),
370 "length", G_TYPE_INT (ltxt.length), NULL));
372 if (gst_props_get (props, "ltxts", &caps, NULL)) {
373 caps = g_list_append (caps, new_caps);
375 caps = g_list_append (NULL, new_caps);
377 entry = gst_props_entry_new ("ltxts", GST_PROPS_GLIST (caps));
378 gst_props_add_entry (props, entry);
383 case GST_RIFF_adtl_note:
385 gst_bytestream_peek_bytes (bs, &tempdata,
386 sizeof (struct _gst_riff_note));
387 if (got_bytes != sizeof (struct _gst_riff_note)) {
391 temp_note = (struct _gst_riff_note *) tempdata;
392 note.id = GUINT32_FROM_LE (temp_note->id);
393 note.size = GUINT32_FROM_LE (temp_note->size);
394 note.identifier = GUINT32_FROM_LE (temp_note->identifier);
396 gst_bytestream_flush (bs, sizeof (struct _gst_riff_note));
397 len -= sizeof (struct _gst_riff_note);
399 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, note.size - 4);
400 if (got_bytes != note.size - 4) {
404 gst_bytestream_flush (bs, ((note.size - 4) + 1) & ~1);
405 len -= (((note.size - 4) + 1) & ~1);
407 label_name = (char *) tempdata;
409 new_caps = gst_caps_new ("note",
410 "application/x-gst-metadata",
411 gst_props_new ("identifier", G_TYPE_INT (note.identifier),
412 "name", G_TYPE_STRING (label_name), NULL));
414 if (gst_props_get (props, "notes", &caps, NULL)) {
415 caps = g_list_append (caps, new_caps);
417 caps = g_list_append (NULL, new_caps);
419 entry = gst_props_entry_new ("notes", GST_PROPS_GLIST (caps));
420 gst_props_add_entry (props, entry);
426 g_print ("Unknown chunk: %" GST_FOURCC_FORMAT "\n",
427 GST_FOURCC_ARGS (chunk.id));
432 g_object_notify (G_OBJECT (wavparse), "metadata");
436 gst_wavparse_parse_cues (GstWavParse * wavparse, int len)
439 GstByteStream *bs = wavparse->bs;
440 struct _gst_riff_cue *temp_cue, cue;
441 struct _gst_riff_cuepoints *points;
445 GstPropsEntry *entry;
451 gst_bytestream_peek_bytes (bs, &tempdata,
452 sizeof (struct _gst_riff_cue));
453 temp_cue = (struct _gst_riff_cue *) tempdata;
455 /* fixup for our big endian friends */
456 cue.id = GUINT32_FROM_LE (temp_cue->id);
457 cue.size = GUINT32_FROM_LE (temp_cue->size);
458 cue.cuepoints = GUINT32_FROM_LE (temp_cue->cuepoints);
460 gst_bytestream_flush (bs, sizeof (struct _gst_riff_cue));
461 if (got_bytes != sizeof (struct _gst_riff_cue)) {
465 len -= sizeof (struct _gst_riff_cue);
467 /* -4 because cue.size contains the cuepoints size
468 and we've already flushed that out of the system */
469 required = cue.size - 4;
470 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, required);
471 gst_bytestream_flush (bs, ((required) + 1) & ~1);
472 if (got_bytes != required) {
476 len -= (((cue.size - 4) + 1) & ~1);
478 /* now we have an array of struct _gst_riff_cuepoints in tempdata */
479 points = (struct _gst_riff_cuepoints *) tempdata;
481 for (i = 0; i < cue.cuepoints; i++) {
484 caps = gst_caps_new ("cues",
485 "application/x-gst-metadata",
486 gst_props_new ("identifier", G_TYPE_INT (points[i].identifier),
487 "position", G_TYPE_INT (points[i].offset), NULL));
488 cues = g_list_append (cues, caps);
491 entry = gst_props_entry_new ("cues", GST_PROPS_GLIST (cues));
492 gst_props_add_entry (wavparse->metadata->properties, entry);
495 g_object_notify (G_OBJECT (wavparse), "metadata");
498 /* Read 'fmt ' header */
500 gst_wavparse_fmt (GstWavParse * wav)
502 gst_riff_strf_auds *header = NULL;
505 if (!gst_riff_read_strf_auds (wav, &header))
508 wav->format = header->format;
509 wav->rate = header->rate;
510 wav->channels = header->channels;
511 if (wav->channels == 0)
514 wav->blockalign = header->blockalign;
515 wav->width = (header->blockalign * 8) / header->channels;
516 wav->depth = header->size;
517 wav->bps = header->av_bps;
521 /* Note: gst_riff_create_audio_caps might need to fix values in
522 * the header header depending on the format, so call it first */
523 /* FIXME: Need to handle the channel reorder map */
524 caps = gst_riff_create_audio_caps (header->format, NULL, header, NULL, NULL);
530 gst_wavparse_create_sourcepad (wav);
531 gst_pad_use_fixed_caps (wav->srcpad);
532 gst_pad_set_active (wav->srcpad, TRUE);
533 gst_pad_set_caps (wav->srcpad, caps);
534 gst_caps_free (caps);
535 gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
536 gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
538 GST_DEBUG ("frequency %u, channels %u", wav->rate, wav->channels);
545 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
546 ("No FMT tag found"));
551 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
552 ("Stream claims to contain zero channels - invalid data"));
558 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
559 ("Stream claims to bitrate of <= zero - invalid data"));
565 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
571 gst_wavparse_other (GstWavParse * wav)
575 if (!gst_riff_peek_head (wav, &tag, &length, NULL)) {
576 GST_WARNING_OBJECT (wav, "could not peek head");
579 GST_DEBUG_OBJECT (wav, "got tag (%08x) %4.4s, length %u", tag,
580 (const gchar *) &tag, length);
583 case GST_RIFF_TAG_LIST:
584 if (!(tag = gst_riff_peek_list (wav))) {
585 GST_WARNING_OBJECT (wav, "could not peek list");
590 case GST_RIFF_LIST_INFO:
591 if (!gst_riff_read_list (wav, &tag) || !gst_riff_read_info (wav)) {
592 GST_WARNING_OBJECT (wav, "could not read list");
597 case GST_RIFF_LIST_adtl:
598 if (!gst_riff_read_skip (wav)) {
599 GST_WARNING_OBJECT (wav, "could not read skip");
605 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag,
607 if (!gst_riff_read_skip (wav)) {
608 GST_WARNING_OBJECT (wav, "could not read skip");
616 case GST_RIFF_TAG_data:
617 if (!gst_bytestream_flush (wav->bs, 8)) {
618 GST_WARNING_OBJECT (wav, "could not flush 8 bytes");
622 GST_DEBUG_OBJECT (wav, "switching to data mode");
623 wav->state = GST_WAVPARSE_DATA;
624 wav->datastart = gst_bytestream_tell (wav->bs);
628 /* length is 0, data probably stretches to the end
630 GST_DEBUG_OBJECT (wav, "length is 0 trying to find length");
631 /* get length of file */
632 file_length = gst_bytestream_length (wav->bs);
633 if (file_length == -1) {
634 GST_DEBUG_OBJECT (wav,
635 "could not get file length, assuming data to eof");
636 /* could not get length, assuming till eof */
637 length = G_MAXUINT32;
639 if (file_length > G_MAXUINT32) {
640 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
641 ", clipping to 32 bits", file_length);
642 /* could not get length, assuming till eof */
643 length = G_MAXUINT32;
645 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
646 ", datalength %u", file_length, length);
647 /* substract offset of datastart from length */
648 length = file_length - wav->datastart;
649 GST_DEBUG_OBJECT (wav, "datalength %u", length);
652 wav->datasize = (guint64) length;
653 GST_DEBUG_OBJECT (wav, "datasize = %ld", length)
656 case GST_RIFF_TAG_cue:
657 if (!gst_riff_read_skip (wav)) {
658 GST_WARNING_OBJECT (wav, "could not read skip");
664 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag, (gchar *) & tag);
665 if (!gst_riff_read_skip (wav))
676 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
680 if (!gst_riff_parse_file_header (element, buf, &doctype))
683 if (doctype != GST_RIFF_RIFF_WAVE)
691 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
692 ("File is not a WAVE file: %" GST_FOURCC_FORMAT,
693 GST_FOURCC_ARGS (doctype)));
699 gst_wavparse_stream_init (GstWavParse * wav)
702 GstBuffer *buf = NULL;
704 if ((res = gst_pad_pull_range (wav->sinkpad,
705 wav->offset, 12, &buf)) != GST_FLOW_OK)
707 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
708 return GST_FLOW_ERROR;
716 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
718 /* -1 always maps to -1 */
724 /* 0 always maps to 0 */
731 *bytepos = gst_util_uint64_scale_ceil (ts, (guint64) wav->bps, GST_SECOND);
733 } else if (wav->fact) {
735 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
736 *bytepos = gst_util_uint64_scale_ceil (ts, bps, GST_SECOND);
743 /* This function is used to perform seeks on the element.
745 * It also works when event is NULL, in which case it will just
746 * start from the last configured segment. This technique is
747 * used when activating the element and to perform the seek in
751 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
755 GstFormat format, bformat;
757 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
758 gint64 cur, stop, upstream_size;
761 GstSegment seeksegment = { 0, };
765 GST_DEBUG_OBJECT (wav, "doing seek with event");
767 gst_event_parse_seek (event, &rate, &format, &flags,
768 &cur_type, &cur, &stop_type, &stop);
770 /* no negative rates yet */
774 if (format != wav->segment.format) {
775 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
776 gst_format_get_name (format),
777 gst_format_get_name (wav->segment.format));
779 if (cur_type != GST_SEEK_TYPE_NONE)
781 gst_pad_query_convert (wav->srcpad, format, cur,
782 wav->segment.format, &cur);
783 if (res && stop_type != GST_SEEK_TYPE_NONE)
785 gst_pad_query_convert (wav->srcpad, format, stop,
786 wav->segment.format, &stop);
790 format = wav->segment.format;
793 GST_DEBUG_OBJECT (wav, "doing seek without event");
796 cur_type = GST_SEEK_TYPE_SET;
797 stop_type = GST_SEEK_TYPE_SET;
800 /* in push mode, we must delegate to upstream */
801 if (wav->streaming) {
802 gboolean res = FALSE;
804 /* if streaming not yet started; only prepare initial newsegment */
805 if (!event || wav->state != GST_WAVPARSE_DATA) {
806 if (wav->start_segment)
807 gst_event_unref (wav->start_segment);
809 /* wav->start_segment =
810 gst_event_new_new_segment (FALSE, wav->segment.rate,
811 wav->segment.format, wav->segment.last_stop, wav->segment.duration,
812 wav->segment.last_stop);*/
815 /* convert seek positions to byte positions in data sections */
816 if (format == GST_FORMAT_TIME) {
817 /* should not fail */
818 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
820 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
823 /* mind sample boundary and header */
825 cur -= (cur % wav->bytes_per_sample);
826 cur += wav->datastart;
829 stop -= (stop % wav->bytes_per_sample);
830 stop += wav->datastart;
832 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
833 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
835 /* BYTE seek event */
836 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
838 res = gst_pad_push_event (wav->sinkpad, event);
844 flush = flags & GST_SEEK_FLAG_FLUSH;
846 /* now we need to make sure the streaming thread is stopped. We do this by
847 * either sending a FLUSH_START event downstream which will cause the
848 * streaming thread to stop with a WRONG_STATE.
849 * For a non-flushing seek we simply pause the task, which will happen as soon
850 * as it completes one iteration (and thus might block when the sink is
851 * blocking in preroll). */
853 GST_DEBUG_OBJECT (wav, "sending flush start");
854 gst_pad_push_event (wav->srcpad, gst_event_new_flush_start ());
856 gst_pad_pause_task (wav->sinkpad);
859 /* we should now be able to grab the streaming thread because we stopped it
860 * with the above flush/pause code */
861 GST_PAD_STREAM_LOCK (wav->sinkpad);
863 /* save current position */
864 last_stop = wav->segment.position;
866 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
868 /* copy segment, we need this because we still need the old
869 * segment when we close the current segment. */
870 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
872 /* configure the seek parameters in the seeksegment. We will then have the
873 * right values in the segment to perform the seek */
875 GST_DEBUG_OBJECT (wav, "configuring seek");
876 gst_segment_do_seek (&seeksegment, rate, format, flags,
877 cur_type, cur, stop_type, stop, &update);
880 /* figure out the last position we need to play. If it's configured (stop !=
881 * -1), use that, else we play until the total duration of the file */
882 if ((stop = seeksegment.stop) == -1)
883 stop = seeksegment.duration;
885 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
886 if ((cur_type != GST_SEEK_TYPE_NONE)) {
887 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
888 * we can just copy the last_stop. If not, we use the bps to convert TIME to
890 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
891 (gint64 *) & wav->offset))
892 wav->offset = seeksegment.position;
893 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
894 wav->offset -= (wav->offset % wav->bytes_per_sample);
895 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
896 wav->offset += wav->datastart;
897 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
899 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
903 if (stop_type != GST_SEEK_TYPE_NONE) {
904 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
905 wav->end_offset = stop;
906 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
907 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
908 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
909 wav->end_offset += wav->datastart;
910 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
912 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
916 /* make sure filesize is not exceeded due to rounding errors or so,
917 * same precaution as in _stream_headers */
918 bformat = GST_FORMAT_BYTES;
919 if (gst_pad_peer_query_duration (wav->sinkpad, bformat, &upstream_size))
920 wav->end_offset = MIN (wav->end_offset, upstream_size);
922 /* this is the range of bytes we will use for playback */
923 wav->offset = MIN (wav->offset, wav->end_offset);
924 wav->dataleft = wav->end_offset - wav->offset;
926 GST_DEBUG_OBJECT (wav,
927 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
928 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
929 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
931 /* prepare for streaming again */
933 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
934 GST_DEBUG_OBJECT (wav, "sending flush stop");
935 gst_pad_push_event (wav->srcpad, gst_event_new_flush_stop (TRUE));
938 /* now we did the seek and can activate the new segment values */
939 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
941 /* if we're doing a segment seek, post a SEGMENT_START message */
942 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
943 gst_element_post_message (GST_ELEMENT_CAST (wav),
944 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
945 wav->segment.format, wav->segment.position));
948 /* now create the newsegment */
949 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
950 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
952 /* store the newsegment event so it can be sent from the streaming thread. */
953 if (wav->start_segment)
954 gst_event_unref (wav->start_segment);
955 wav->start_segment = gst_event_new_segment (&wav->segment);
957 /* mark discont if we are going to stream from another position. */
958 if (last_stop != wav->segment.position) {
959 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
963 /* and start the streaming task again */
964 if (!wav->streaming) {
965 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
969 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
976 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
981 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
986 GST_DEBUG_OBJECT (wav,
987 "Could not determine byte position for desired time");
993 * gst_wavparse_peek_chunk_info:
994 * @wav Wavparse object
995 * @tag holder for tag
996 * @size holder for tag size
998 * Peek next chunk info (tag and size)
1000 * Returns: %TRUE when the chunk info (header) is available
1003 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
1005 const guint8 *data = NULL;
1007 if (gst_adapter_available (wav->adapter) < 8)
1010 data = gst_adapter_map (wav->adapter, 8);
1011 *tag = GST_READ_UINT32_LE (data);
1012 *size = GST_READ_UINT32_LE (data + 4);
1013 gst_adapter_unmap (wav->adapter);
1015 GST_DEBUG ("Next chunk size is %u bytes, type %" GST_FOURCC_FORMAT, *size,
1016 GST_FOURCC_ARGS (*tag));
1022 * gst_wavparse_peek_chunk:
1023 * @wav Wavparse object
1024 * @tag holder for tag
1025 * @size holder for tag size
1027 * Peek enough data for one full chunk
1029 * Returns: %TRUE when the full chunk is available
1032 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
1034 guint32 peek_size = 0;
1037 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
1040 /* size 0 -> empty data buffer would surprise most callers,
1041 * large size -> do not bother trying to squeeze that into adapter,
1042 * so we throw poor man's exception, which can be caught if caller really
1043 * wants to handle 0 size chunk */
1044 if (!(*size) || (*size) >= (1 << 30)) {
1045 GST_INFO ("Invalid/unexpected chunk size %u for tag %" GST_FOURCC_FORMAT,
1046 *size, GST_FOURCC_ARGS (*tag));
1047 /* chain should give up */
1048 wav->abort_buffering = TRUE;
1051 peek_size = (*size + 1) & ~1;
1052 available = gst_adapter_available (wav->adapter);
1054 if (available >= (8 + peek_size)) {
1057 GST_LOG ("but only %u bytes available now", available);
1063 * gst_wavparse_calculate_duration:
1064 * @wav: wavparse object
1066 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
1069 * Returns: %TRUE if duration is available.
1072 gst_wavparse_calculate_duration (GstWavParse * wav)
1074 if (wav->duration > 0)
1078 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
1080 gst_util_uint64_scale_ceil (wav->datasize, GST_SECOND,
1081 (guint64) wav->bps);
1082 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
1083 GST_TIME_ARGS (wav->duration));
1085 } else if (wav->fact) {
1087 gst_util_uint64_scale_int_ceil (GST_SECOND, wav->fact, wav->rate);
1088 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
1089 GST_TIME_ARGS (wav->duration));
1096 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
1101 if (wav->streaming) {
1102 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1105 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
1106 GST_FOURCC_ARGS (tag));
1107 flush = 8 + ((size + 1) & ~1);
1108 wav->offset += flush;
1109 if (wav->streaming) {
1110 gst_adapter_flush (wav->adapter, flush);
1112 gst_buffer_unref (buf);
1118 #define MAX_BUFFER_SIZE 4096
1120 static GstFlowReturn
1121 gst_wavparse_stream_headers (GstWavParse * wav)
1123 GstFlowReturn res = GST_FLOW_OK;
1124 GstBuffer *buf = NULL;
1125 gst_riff_strf_auds *header = NULL;
1127 gboolean gotdata = FALSE;
1128 GstCaps *caps = NULL;
1129 gchar *codec_name = NULL;
1131 gint64 upstream_size = 0;
1133 /* search for "_fmt" chunk, which should be first */
1134 while (!wav->got_fmt) {
1137 /* The header starts with a 'fmt ' tag */
1138 if (wav->streaming) {
1139 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1142 gst_adapter_flush (wav->adapter, 8);
1146 buf = gst_adapter_take_buffer (wav->adapter, size);
1148 gst_adapter_flush (wav->adapter, 1);
1149 wav->offset += GST_ROUND_UP_2 (size);
1151 buf = gst_buffer_new ();
1154 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1155 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1159 if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ ||
1160 tag == GST_RIFF_TAG_bext || tag == GST_RIFF_TAG_BEXT ||
1161 tag == GST_RIFF_TAG_LIST || tag == GST_RIFF_TAG_ID32 ||
1162 tag == GST_RIFF_TAG_IDVX) {
1163 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1164 GST_FOURCC_ARGS (tag));
1165 gst_buffer_unref (buf);
1170 if (tag != GST_RIFF_TAG_fmt)
1173 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1175 goto parse_header_error;
1177 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1179 /* do sanity checks of header fields */
1180 if (header->channels == 0)
1182 if (header->rate == 0)
1185 GST_DEBUG_OBJECT (wav, "creating the caps");
1187 /* Note: gst_riff_create_audio_caps might need to fix values in
1188 * the header header depending on the format, so call it first */
1189 /* FIXME: Need to handle the channel reorder map */
1190 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1191 NULL, &codec_name, NULL);
1194 gst_buffer_unref (extra);
1197 goto unknown_format;
1199 /* do more sanity checks of header fields
1200 * (these can be sanitized by gst_riff_create_audio_caps()
1202 wav->format = header->format;
1203 wav->rate = header->rate;
1204 wav->channels = header->channels;
1205 wav->blockalign = header->blockalign;
1206 wav->depth = header->size;
1207 wav->av_bps = header->av_bps;
1213 /* do format specific handling */
1214 switch (wav->format) {
1215 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1216 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1218 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1219 * bitrate inside the mpeg stream */
1220 GST_INFO ("resetting bps from %u to 0 for mp2/3", wav->av_bps);
1224 case GST_RIFF_WAVE_FORMAT_PCM:
1225 if (wav->blockalign > wav->channels * ((wav->depth + 7) / 8))
1226 goto invalid_blockalign;
1229 if (wav->av_bps > wav->blockalign * wav->rate)
1231 /* use the configured bps */
1232 wav->bps = wav->av_bps;
1236 wav->width = (wav->blockalign * 8) / wav->channels;
1237 wav->bytes_per_sample = wav->channels * wav->width / 8;
1239 if (wav->bytes_per_sample <= 0)
1240 goto no_bytes_per_sample;
1242 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1243 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1244 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1245 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1246 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1247 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1248 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1250 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1251 * formats). This will make the element output a BYTE format segment and
1252 * will not timestamp the outgoing buffers.
1254 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1256 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1258 /* create pad later so we can sniff the first few bytes
1259 * of the real data and correct our caps if necessary */
1260 gst_caps_replace (&wav->caps, caps);
1261 gst_caps_replace (&caps, NULL);
1263 wav->got_fmt = TRUE;
1266 wav->tags = gst_tag_list_new_empty ();
1268 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1269 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1271 g_free (codec_name);
1277 gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1278 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1280 /* loop headers until we get data */
1282 if (wav->streaming) {
1283 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1290 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1291 &buf)) != GST_FLOW_OK)
1292 goto header_read_error;
1293 gst_buffer_map (buf, &map, GST_MAP_READ);
1294 tag = GST_READ_UINT32_LE (map.data);
1295 size = GST_READ_UINT32_LE (map.data + 4);
1296 gst_buffer_unmap (buf, &map);
1299 GST_INFO_OBJECT (wav,
1300 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT,
1301 GST_FOURCC_ARGS (tag), wav->offset);
1303 /* wav is a st00pid format, we don't know for sure where data starts.
1304 * So we have to go bit by bit until we find the 'data' header
1307 case GST_RIFF_TAG_data:{
1308 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %u", size);
1309 if (wav->ignore_length) {
1310 GST_DEBUG_OBJECT (wav, "Ignoring length");
1313 if (wav->streaming) {
1314 gst_adapter_flush (wav->adapter, 8);
1317 gst_buffer_unref (buf);
1320 wav->datastart = wav->offset;
1321 /* If size is zero, then the data chunk probably actually extends to
1322 the end of the file */
1323 if (size == 0 && upstream_size) {
1324 size = upstream_size - wav->datastart;
1326 /* Or the file might be truncated */
1327 else 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 = %u", 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 GST_INFO_OBJECT (wav, "Have fact chunk");
1346 if (size < data_size) {
1347 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1348 /* need more data */
1351 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1355 /* number of samples (for compressed formats) */
1356 if (wav->streaming) {
1357 const guint8 *data = NULL;
1359 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1362 gst_adapter_flush (wav->adapter, 8);
1363 data = gst_adapter_map (wav->adapter, data_size);
1364 wav->fact = GST_READ_UINT32_LE (data);
1365 gst_adapter_unmap (wav->adapter);
1366 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1368 gst_buffer_unref (buf);
1371 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1372 data_size, &buf)) != GST_FLOW_OK)
1373 goto header_read_error;
1374 gst_buffer_extract (buf, 0, &wav->fact, 4);
1375 wav->fact = GUINT32_FROM_LE (wav->fact);
1376 gst_buffer_unref (buf);
1378 GST_DEBUG_OBJECT (wav, "have fact %u", wav->fact);
1379 wav->offset += 8 + GST_ROUND_UP_2 (size);
1382 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1383 /* need more data */
1389 case GST_RIFF_TAG_acid:{
1390 const gst_riff_acid *acid = NULL;
1391 const guint data_size = sizeof (gst_riff_acid);
1394 GST_INFO_OBJECT (wav, "Have acid chunk");
1395 if (size < data_size) {
1396 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1397 /* need more data */
1400 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1404 if (wav->streaming) {
1405 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1408 gst_adapter_flush (wav->adapter, 8);
1409 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1411 tempo = acid->tempo;
1412 gst_adapter_unmap (wav->adapter);
1415 gst_buffer_unref (buf);
1418 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1419 size, &buf)) != GST_FLOW_OK)
1420 goto header_read_error;
1421 gst_buffer_map (buf, &map, GST_MAP_READ);
1422 acid = (const gst_riff_acid *) map.data;
1423 tempo = acid->tempo;
1424 gst_buffer_unmap (buf, &map);
1426 /* send data as tags */
1428 wav->tags = gst_tag_list_new_empty ();
1429 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1430 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1432 size = GST_ROUND_UP_2 (size);
1433 if (wav->streaming) {
1434 gst_adapter_flush (wav->adapter, size);
1436 gst_buffer_unref (buf);
1438 wav->offset += 8 + size;
1441 /* FIXME: all list tags after data are ignored in streaming mode */
1442 case GST_RIFF_TAG_LIST:{
1445 if (wav->streaming) {
1446 const guint8 *data = NULL;
1448 if (gst_adapter_available (wav->adapter) < 12) {
1451 data = gst_adapter_map (wav->adapter, 12);
1452 ltag = GST_READ_UINT32_LE (data + 8);
1453 gst_adapter_unmap (wav->adapter);
1455 gst_buffer_unref (buf);
1458 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1459 &buf)) != GST_FLOW_OK)
1460 goto header_read_error;
1461 gst_buffer_extract (buf, 8, <ag, 4);
1462 ltag = GUINT32_FROM_LE (ltag);
1465 case GST_RIFF_LIST_INFO:{
1466 const gint data_size = size - 4;
1469 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1470 if (wav->streaming) {
1471 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1474 gst_adapter_flush (wav->adapter, 12);
1476 if (data_size > 0) {
1477 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1479 gst_adapter_flush (wav->adapter, 1);
1483 gst_buffer_unref (buf);
1485 if (data_size > 0) {
1487 gst_pad_pull_range (wav->sinkpad, wav->offset,
1488 data_size, &buf)) != GST_FLOW_OK)
1489 goto header_read_error;
1492 if (data_size > 0) {
1494 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1496 GstTagList *old = wav->tags;
1498 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1500 gst_tag_list_free (old);
1501 gst_tag_list_free (new);
1503 gst_buffer_unref (buf);
1504 wav->offset += GST_ROUND_UP_2 (data_size);
1509 GST_INFO_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1510 GST_FOURCC_ARGS (ltag));
1511 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1512 /* need more data */
1519 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1520 /* need more data */
1525 if (upstream_size && (wav->offset >= upstream_size)) {
1526 /* Now we are gone through the whole file */
1531 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1533 if (wav->bps <= 0 && wav->fact) {
1535 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1537 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1538 (guint64) wav->fact);
1539 GST_INFO_OBJECT (wav, "calculated bps : %u, enabling VBR", wav->bps);
1544 if (gst_wavparse_calculate_duration (wav)) {
1545 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1546 if (!wav->ignore_length)
1547 wav->segment.duration = wav->duration;
1549 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1550 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1551 if (!wav->ignore_length)
1552 wav->segment.duration = wav->datasize;
1555 /* now we have all the info to perform a pending seek if any, if no
1556 * event, this will still do the right thing and it will also send
1557 * the right newsegment event downstream. */
1558 gst_wavparse_perform_seek (wav, wav->seek_event);
1559 /* remove pending event */
1560 event_p = &wav->seek_event;
1561 gst_event_replace (event_p, NULL);
1563 /* we just started, we are discont */
1564 wav->discont = TRUE;
1566 wav->state = GST_WAVPARSE_DATA;
1568 /* determine reasonable max buffer size,
1569 * that is, buffers not too small either size or time wise
1570 * so we do not end up with too many of them */
1573 gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size);
1574 wav->max_buf_size = upstream_size;
1575 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1576 if (wav->blockalign > 0)
1577 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1579 GST_DEBUG_OBJECT (wav, "max buffer size %u", wav->max_buf_size);
1587 g_free (codec_name);
1591 gst_caps_unref (caps);
1596 res = GST_FLOW_ERROR;
1601 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1602 ("Invalid WAV header (no fmt at start): %"
1603 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1608 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1609 ("Couldn't parse audio header"));
1614 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1615 ("Stream claims to contain no channels - invalid data"));
1620 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1621 ("Stream with sample_rate == 0 - invalid data"));
1626 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1627 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1628 wav->blockalign, wav->channels * ((wav->depth + 7) / 8)));
1633 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1634 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1635 wav->av_bps, wav->blockalign * wav->rate));
1638 no_bytes_per_sample:
1640 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1641 ("Could not caluclate bytes per sample - invalid data"));
1646 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1647 ("No caps found for format 0x%x, %u channels, %u Hz",
1648 wav->format, wav->channels, wav->rate));
1653 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1654 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1660 * Read WAV file tag when streaming
1662 static GstFlowReturn
1663 gst_wavparse_parse_stream_init (GstWavParse * wav)
1665 if (gst_adapter_available (wav->adapter) >= 12) {
1668 /* _take flushes the data */
1669 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1671 GST_DEBUG ("Parsing wav header");
1672 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1673 return GST_FLOW_ERROR;
1676 /* Go to next state */
1677 wav->state = GST_WAVPARSE_HEADER;
1682 /* handle an event sent directly to the element.
1684 * This event can be sent either in the READY state or the
1685 * >READY state. The only event of interest really is the seek
1688 * In the READY state we can only store the event and try to
1689 * respect it when going to PAUSED. We assume we are in the
1690 * READY state when our parsing state != GST_WAVPARSE_DATA.
1692 * When we are steaming, we can simply perform the seek right
1696 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1698 GstWavParse *wav = GST_WAVPARSE (element);
1699 gboolean res = FALSE;
1702 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1704 switch (GST_EVENT_TYPE (event)) {
1705 case GST_EVENT_SEEK:
1706 if (wav->state == GST_WAVPARSE_DATA) {
1707 /* we can handle the seek directly when streaming data */
1708 res = gst_wavparse_perform_seek (wav, event);
1710 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1712 event_p = &wav->seek_event;
1713 gst_event_replace (event_p, event);
1715 /* we always return true */
1722 gst_event_unref (event);
1727 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1731 s = gst_caps_get_structure (caps, 0);
1732 if (!gst_structure_has_name (s, "audio/x-dts"))
1734 if (prob >= GST_TYPE_FIND_LIKELY)
1736 /* DTS at non-0 offsets and without second sync may yield POSSIBLE .. */
1737 if (prob < GST_TYPE_FIND_POSSIBLE)
1739 /* .. in which case we want at least a valid-looking rate and channels */
1740 if (!gst_structure_has_field (s, "channels"))
1742 /* and for extra assurance we could also check the rate from the DTS frame
1743 * against the one in the wav header, but for now let's not do that */
1744 return gst_structure_has_field (s, "rate");
1748 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1752 GST_DEBUG_OBJECT (wav, "adding src pad");
1755 s = gst_caps_get_structure (wav->caps, 0);
1756 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
1757 GstTypeFindProbability prob;
1760 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1761 if (tf_caps != NULL) {
1762 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1763 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1764 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1765 gst_caps_unref (wav->caps);
1766 wav->caps = tf_caps;
1768 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1769 GST_TAG_AUDIO_CODEC, "dts", NULL);
1771 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1772 "marked as raw PCM audio, but ignoring for now", tf_caps);
1773 gst_caps_unref (tf_caps);
1779 gst_pad_set_caps (wav->srcpad, wav->caps);
1780 gst_caps_replace (&wav->caps, NULL);
1782 if (wav->start_segment) {
1783 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1784 gst_pad_push_event (wav->srcpad, wav->start_segment);
1785 wav->start_segment = NULL;
1789 gst_pad_push_event (wav->srcpad, gst_event_new_tag ("GstParser",
1795 static GstFlowReturn
1796 gst_wavparse_stream_data (GstWavParse * wav)
1798 GstBuffer *buf = NULL;
1799 GstFlowReturn res = GST_FLOW_OK;
1800 guint64 desired, obtained;
1801 GstClockTime timestamp, next_timestamp, duration;
1802 guint64 pos, nextpos;
1805 GST_LOG_OBJECT (wav,
1806 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1807 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1809 /* Get the next n bytes and output them */
1810 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1813 /* scale the amount of data by the segment rate so we get equal
1814 * amounts of data regardless of the playback rate */
1816 MIN (gst_guint64_to_gdouble (wav->dataleft),
1817 wav->max_buf_size * ABS (wav->segment.rate));
1819 if (desired >= wav->blockalign && wav->blockalign > 0)
1820 desired -= (desired % wav->blockalign);
1822 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1823 "from the sinkpad", desired);
1825 if (wav->streaming) {
1826 guint avail = gst_adapter_available (wav->adapter);
1829 /* flush some bytes if evil upstream sends segment that starts
1830 * before data or does is not send sample aligned segment */
1831 if (G_LIKELY (wav->offset >= wav->datastart)) {
1832 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1834 extra = wav->datastart - wav->offset;
1837 if (G_UNLIKELY (extra)) {
1838 extra = wav->bytes_per_sample - extra;
1839 if (extra <= avail) {
1840 GST_DEBUG_OBJECT (wav, "flushing %u bytes to sample boundary", extra);
1841 gst_adapter_flush (wav->adapter, extra);
1842 wav->offset += extra;
1843 wav->dataleft -= extra;
1844 goto iterate_adapter;
1846 GST_DEBUG_OBJECT (wav, "flushing %u bytes", avail);
1847 gst_adapter_clear (wav->adapter);
1848 wav->offset += avail;
1849 wav->dataleft -= avail;
1854 if (avail < desired) {
1855 GST_LOG_OBJECT (wav, "Got only %u bytes of data from the sinkpad", avail);
1859 buf = gst_adapter_take_buffer (wav->adapter, desired);
1861 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
1862 desired, &buf)) != GST_FLOW_OK)
1865 /* we may get a short buffer at the end of the file */
1866 if (gst_buffer_get_size (buf) < desired) {
1867 gsize size = gst_buffer_get_size (buf);
1869 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
1870 if (size >= wav->blockalign) {
1871 buf = gst_buffer_make_writable (buf);
1872 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
1874 gst_buffer_unref (buf);
1880 obtained = gst_buffer_get_size (buf);
1882 /* our positions in bytes */
1883 pos = wav->offset - wav->datastart;
1884 nextpos = pos + obtained;
1886 /* update offsets, does not overflow. */
1887 buf = gst_buffer_make_writable (buf);
1888 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
1889 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
1891 /* first chunk of data? create the source pad. We do this only here so
1892 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
1893 if (G_UNLIKELY (wav->first)) {
1895 /* this will also push the segment events */
1896 gst_wavparse_add_src_pad (wav, buf);
1898 /* If we have a pending start segment, send it now. */
1899 if (G_UNLIKELY (wav->start_segment != NULL)) {
1900 gst_pad_push_event (wav->srcpad, wav->start_segment);
1901 wav->start_segment = NULL;
1906 /* and timestamps if we have a bitrate, be careful for overflows */
1908 gst_util_uint64_scale_ceil (pos, GST_SECOND, (guint64) wav->bps);
1910 gst_util_uint64_scale_ceil (nextpos, GST_SECOND, (guint64) wav->bps);
1911 duration = next_timestamp - timestamp;
1913 /* update current running segment position */
1914 if (G_LIKELY (next_timestamp >= wav->segment.start))
1915 wav->segment.position = next_timestamp;
1916 } else if (wav->fact) {
1918 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
1919 /* and timestamps if we have a bitrate, be careful for overflows */
1920 timestamp = gst_util_uint64_scale_ceil (pos, GST_SECOND, bps);
1921 next_timestamp = gst_util_uint64_scale_ceil (nextpos, GST_SECOND, bps);
1922 duration = next_timestamp - timestamp;
1924 /* no bitrate, all we know is that the first sample has timestamp 0, all
1925 * other positions and durations have unknown timestamp. */
1929 timestamp = GST_CLOCK_TIME_NONE;
1930 duration = GST_CLOCK_TIME_NONE;
1931 /* update current running segment position with byte offset */
1932 if (G_LIKELY (nextpos >= wav->segment.start))
1933 wav->segment.position = nextpos;
1935 if ((pos > 0) && wav->vbr) {
1936 /* don't set timestamps for VBR files if it's not the first buffer */
1937 timestamp = GST_CLOCK_TIME_NONE;
1938 duration = GST_CLOCK_TIME_NONE;
1941 GST_DEBUG_OBJECT (wav, "marking DISCONT");
1942 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1943 wav->discont = FALSE;
1946 GST_BUFFER_TIMESTAMP (buf) = timestamp;
1947 GST_BUFFER_DURATION (buf) = duration;
1949 GST_LOG_OBJECT (wav,
1950 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
1951 ", size:%" G_GSIZE_FORMAT, GST_TIME_ARGS (timestamp),
1952 GST_TIME_ARGS (duration), gst_buffer_get_size (buf));
1954 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
1957 if (obtained < wav->dataleft) {
1958 wav->offset += obtained;
1959 wav->dataleft -= obtained;
1961 wav->offset += wav->dataleft;
1965 /* Iterate until need more data, so adapter size won't grow */
1966 if (wav->streaming) {
1967 GST_LOG_OBJECT (wav,
1968 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
1970 goto iterate_adapter;
1977 GST_DEBUG_OBJECT (wav, "found EOS");
1978 return GST_FLOW_EOS;
1982 /* check if we got EOS */
1983 if (res == GST_FLOW_EOS)
1986 GST_WARNING_OBJECT (wav,
1987 "Error getting %" G_GINT64_FORMAT " bytes from the "
1988 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
1993 GST_INFO_OBJECT (wav,
1994 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
1995 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
1996 gst_pad_is_linked (wav->srcpad));
2002 gst_wavparse_loop (GstPad * pad)
2005 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2007 GST_LOG_OBJECT (wav, "process data");
2009 switch (wav->state) {
2010 case GST_WAVPARSE_START:
2011 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2012 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2015 wav->state = GST_WAVPARSE_HEADER;
2018 case GST_WAVPARSE_HEADER:
2019 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2020 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2023 wav->state = GST_WAVPARSE_DATA;
2024 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2027 case GST_WAVPARSE_DATA:
2028 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2032 g_assert_not_reached ();
2039 const gchar *reason = gst_flow_get_name (ret);
2041 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2042 gst_pad_pause_task (pad);
2044 if (ret == GST_FLOW_EOS) {
2045 /* handle end-of-stream/segment */
2046 /* so align our position with the end of it, if there is one
2047 * this ensures a subsequent will arrive at correct base/acc time */
2048 if (wav->segment.format == GST_FORMAT_TIME) {
2049 if (wav->segment.rate > 0.0 &&
2050 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2051 wav->segment.position = wav->segment.stop;
2052 else if (wav->segment.rate < 0.0)
2053 wav->segment.position = wav->segment.start;
2055 /* add pad before we perform EOS */
2056 if (G_UNLIKELY (wav->first)) {
2058 gst_wavparse_add_src_pad (wav, NULL);
2061 if (wav->state == GST_WAVPARSE_START)
2062 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2063 ("No valid input found before end of stream"), (NULL));
2065 /* perform EOS logic */
2066 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2069 if ((stop = wav->segment.stop) == -1)
2070 stop = wav->segment.duration;
2072 gst_element_post_message (GST_ELEMENT_CAST (wav),
2073 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2074 wav->segment.format, stop));
2076 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2078 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) {
2079 /* for fatal errors we post an error message, post the error
2080 * first so the app knows about the error first. */
2081 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
2082 (_("Internal data flow error.")),
2083 ("streaming task paused, reason %s (%d)", reason, ret));
2084 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2090 static GstFlowReturn
2091 gst_wavparse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2094 GstWavParse *wav = GST_WAVPARSE (parent);
2096 GST_LOG_OBJECT (wav, "adapter_push %" G_GSIZE_FORMAT " bytes",
2097 gst_buffer_get_size (buf));
2099 gst_adapter_push (wav->adapter, buf);
2101 switch (wav->state) {
2102 case GST_WAVPARSE_START:
2103 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2104 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2107 if (wav->state != GST_WAVPARSE_HEADER)
2110 /* otherwise fall-through */
2111 case GST_WAVPARSE_HEADER:
2112 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2113 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2116 if (!wav->got_fmt || wav->datastart == 0)
2119 wav->state = GST_WAVPARSE_DATA;
2120 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2123 case GST_WAVPARSE_DATA:
2124 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2125 wav->discont = TRUE;
2126 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2130 g_return_val_if_reached (GST_FLOW_ERROR);
2133 if (G_UNLIKELY (wav->abort_buffering)) {
2134 wav->abort_buffering = FALSE;
2135 ret = GST_FLOW_ERROR;
2136 /* sort of demux/parse error */
2137 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2143 static GstFlowReturn
2144 gst_wavparse_flush_data (GstWavParse * wav)
2146 GstFlowReturn ret = GST_FLOW_OK;
2149 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2151 wav->end_offset = wav->offset + av;
2152 ret = gst_wavparse_stream_data (wav);
2159 gst_wavparse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2161 GstWavParse *wav = GST_WAVPARSE (parent);
2162 gboolean ret = TRUE;
2164 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2166 switch (GST_EVENT_TYPE (event)) {
2167 case GST_EVENT_CAPS:
2169 /* discard, we'll come up with proper src caps */
2170 gst_event_unref (event);
2173 case GST_EVENT_SEGMENT:
2175 gint64 start, stop, offset = 0, end_offset = -1;
2178 /* some debug output */
2179 gst_event_copy_segment (event, &segment);
2180 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2183 if (wav->state != GST_WAVPARSE_DATA) {
2184 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2188 /* now we are either committed to TIME or BYTE format,
2189 * and we only expect a BYTE segment, e.g. following a seek */
2190 if (segment.format == GST_FORMAT_BYTES) {
2191 /* handle (un)signed issues */
2192 start = segment.start;
2193 stop = segment.stop;
2196 start -= wav->datastart;
2197 start = MAX (start, 0);
2201 segment.stop -= wav->datastart;
2202 segment.stop = MAX (stop, 0);
2204 if (wav->segment.format == GST_FORMAT_TIME) {
2205 guint64 bps = wav->bps;
2207 /* operating in format TIME, so we can convert */
2208 if (!bps && wav->fact)
2210 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2214 gst_util_uint64_scale_ceil (start, GST_SECOND,
2215 (guint64) wav->bps);
2218 gst_util_uint64_scale_ceil (stop, GST_SECOND,
2219 (guint64) wav->bps);
2223 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2227 segment.start = start;
2228 segment.stop = stop;
2230 /* accept upstream's notion of segment and distribute along */
2231 segment.time = segment.start = segment.position;
2232 segment.duration = wav->segment.duration;
2233 segment.base = gst_segment_to_running_time (&wav->segment,
2234 GST_FORMAT_TIME, wav->segment.position);
2236 gst_segment_copy_into (&segment, &wav->segment);
2238 /* also store the newsegment event for the streaming thread */
2239 if (wav->start_segment)
2240 gst_event_unref (wav->start_segment);
2241 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2242 wav->start_segment = gst_event_new_segment (&segment);
2244 /* stream leftover data in current segment */
2245 gst_wavparse_flush_data (wav);
2246 /* and set up streaming thread for next one */
2247 wav->offset = offset;
2248 wav->end_offset = end_offset;
2249 if (wav->end_offset > 0) {
2250 wav->dataleft = wav->end_offset - wav->offset;
2252 /* infinity; upstream will EOS when done */
2253 wav->dataleft = G_MAXUINT64;
2256 gst_event_unref (event);
2260 /* add pad if needed so EOS is seen downstream */
2261 if (G_UNLIKELY (wav->first)) {
2263 gst_wavparse_add_src_pad (wav, NULL);
2265 /* stream leftover data in current segment */
2266 gst_wavparse_flush_data (wav);
2269 if (wav->state == GST_WAVPARSE_START)
2270 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2271 ("No valid input found before end of stream"), (NULL));
2274 case GST_EVENT_FLUSH_STOP:
2278 gst_adapter_clear (wav->adapter);
2279 wav->discont = TRUE;
2280 dur = wav->segment.duration;
2281 gst_segment_init (&wav->segment, wav->segment.format);
2282 wav->segment.duration = dur;
2286 ret = gst_pad_event_default (wav->sinkpad, parent, event);
2294 /* convert and query stuff */
2295 static const GstFormat *
2296 gst_wavparse_get_formats (GstPad * pad)
2298 static GstFormat formats[] = {
2301 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2310 gst_wavparse_pad_convert (GstPad * pad,
2311 GstFormat src_format, gint64 src_value,
2312 GstFormat * dest_format, gint64 * dest_value)
2314 GstWavParse *wavparse;
2315 gboolean res = TRUE;
2317 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2319 if (*dest_format == src_format) {
2320 *dest_value = src_value;
2324 if ((wavparse->bps == 0) && !wavparse->fact)
2327 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2328 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2330 switch (src_format) {
2331 case GST_FORMAT_BYTES:
2332 switch (*dest_format) {
2333 case GST_FORMAT_DEFAULT:
2334 *dest_value = src_value / wavparse->bytes_per_sample;
2335 /* make sure we end up on a sample boundary */
2336 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2338 case GST_FORMAT_TIME:
2339 /* src_value + datastart = offset */
2340 GST_INFO_OBJECT (wavparse,
2341 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2343 if (wavparse->bps > 0)
2344 *dest_value = gst_util_uint64_scale_ceil (src_value, GST_SECOND,
2345 (guint64) wavparse->bps);
2346 else if (wavparse->fact) {
2347 guint64 bps = gst_util_uint64_scale_int_ceil (wavparse->datasize,
2348 wavparse->rate, wavparse->fact);
2351 gst_util_uint64_scale_int_ceil (src_value, GST_SECOND, bps);
2362 case GST_FORMAT_DEFAULT:
2363 switch (*dest_format) {
2364 case GST_FORMAT_BYTES:
2365 *dest_value = src_value * wavparse->bytes_per_sample;
2367 case GST_FORMAT_TIME:
2368 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2369 (guint64) wavparse->rate);
2377 case GST_FORMAT_TIME:
2378 switch (*dest_format) {
2379 case GST_FORMAT_BYTES:
2380 if (wavparse->bps > 0)
2381 *dest_value = gst_util_uint64_scale (src_value,
2382 (guint64) wavparse->bps, GST_SECOND);
2384 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2385 wavparse->rate, wavparse->fact);
2387 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2389 /* make sure we end up on a sample boundary */
2390 *dest_value -= *dest_value % wavparse->blockalign;
2392 case GST_FORMAT_DEFAULT:
2393 *dest_value = gst_util_uint64_scale (src_value,
2394 (guint64) wavparse->rate, GST_SECOND);
2413 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2419 /* handle queries for location and length in requested format */
2421 gst_wavparse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
2423 gboolean res = TRUE;
2424 GstWavParse *wav = GST_WAVPARSE (parent);
2426 /* only if we know */
2427 if (wav->state != GST_WAVPARSE_DATA) {
2431 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2433 switch (GST_QUERY_TYPE (query)) {
2434 case GST_QUERY_POSITION:
2440 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2441 curb = wav->offset - wav->datastart;
2442 gst_query_parse_position (query, &format, NULL);
2443 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2446 case GST_FORMAT_TIME:
2447 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2451 format = GST_FORMAT_BYTES;
2456 gst_query_set_position (query, format, cur);
2459 case GST_QUERY_DURATION:
2461 gint64 duration = 0;
2464 if (wav->ignore_length) {
2469 gst_query_parse_duration (query, &format, NULL);
2472 case GST_FORMAT_TIME:{
2473 if ((res = gst_wavparse_calculate_duration (wav))) {
2474 duration = wav->duration;
2479 format = GST_FORMAT_BYTES;
2480 duration = wav->datasize;
2483 gst_query_set_duration (query, format, duration);
2486 case GST_QUERY_CONVERT:
2488 gint64 srcvalue, dstvalue;
2489 GstFormat srcformat, dstformat;
2491 gst_query_parse_convert (query, &srcformat, &srcvalue,
2492 &dstformat, &dstvalue);
2493 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2494 &dstformat, &dstvalue);
2496 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2499 case GST_QUERY_SEEKING:{
2501 gboolean seekable = FALSE;
2503 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2504 if (fmt == wav->segment.format) {
2505 if (wav->streaming) {
2508 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2509 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2510 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2511 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2513 gst_query_unref (q);
2515 GST_LOG_OBJECT (wav, "looping => seekable");
2519 } else if (fmt == GST_FORMAT_TIME) {
2523 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2528 res = gst_pad_query_default (pad, parent, query);
2535 gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
2537 GstWavParse *wavparse = GST_WAVPARSE (parent);
2538 gboolean res = FALSE;
2540 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2542 switch (GST_EVENT_TYPE (event)) {
2543 case GST_EVENT_SEEK:
2544 /* can only handle events when we are in the data state */
2545 if (wavparse->state == GST_WAVPARSE_DATA) {
2546 res = gst_wavparse_perform_seek (wavparse, event);
2548 gst_event_unref (event);
2551 res = gst_pad_push_event (wavparse->sinkpad, event);
2558 gst_wavparse_sink_activate (GstPad * sinkpad, GstObject * parent)
2560 GstWavParse *wav = GST_WAVPARSE (parent);
2565 gst_adapter_clear (wav->adapter);
2566 g_object_unref (wav->adapter);
2567 wav->adapter = NULL;
2570 query = gst_query_new_scheduling ();
2572 if (!gst_pad_peer_query (sinkpad, query)) {
2573 gst_query_unref (query);
2577 pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
2578 gst_query_unref (query);
2583 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2584 wav->streaming = FALSE;
2585 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE);
2589 GST_DEBUG_OBJECT (sinkpad, "activating push");
2590 wav->streaming = TRUE;
2591 wav->adapter = gst_adapter_new ();
2592 return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE);
2598 gst_wavparse_sink_activate_mode (GstPad * sinkpad, GstObject * parent,
2599 GstPadMode mode, gboolean active)
2604 case GST_PAD_MODE_PUSH:
2607 case GST_PAD_MODE_PULL:
2609 /* if we have a scheduler we can start the task */
2610 res = gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2613 res = gst_pad_stop_task (sinkpad);
2623 static GstStateChangeReturn
2624 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2626 GstStateChangeReturn ret;
2627 GstWavParse *wav = GST_WAVPARSE (element);
2629 switch (transition) {
2630 case GST_STATE_CHANGE_NULL_TO_READY:
2632 case GST_STATE_CHANGE_READY_TO_PAUSED:
2633 gst_wavparse_reset (wav);
2635 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2641 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2643 switch (transition) {
2644 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2646 case GST_STATE_CHANGE_PAUSED_TO_READY:
2647 gst_wavparse_reset (wav);
2649 case GST_STATE_CHANGE_READY_TO_NULL:
2658 gst_wavparse_set_property (GObject * object, guint prop_id,
2659 const GValue * value, GParamSpec * pspec)
2663 g_return_if_fail (GST_IS_WAVPARSE (object));
2664 self = GST_WAVPARSE (object);
2667 case PROP_IGNORE_LENGTH:
2668 self->ignore_length = g_value_get_boolean (value);
2671 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2677 gst_wavparse_get_property (GObject * object, guint prop_id,
2678 GValue * value, GParamSpec * pspec)
2682 g_return_if_fail (GST_IS_WAVPARSE (object));
2683 self = GST_WAVPARSE (object);
2686 case PROP_IGNORE_LENGTH:
2687 g_value_set_boolean (value, self->ignore_length);
2690 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2695 plugin_init (GstPlugin * plugin)
2699 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2703 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2706 "Parse a .wav file into raw audio",
2707 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)