1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2 -*- */
3 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4 * Copyright (C) <2006> Nokia Corporation, Stefan Kost <stefan.kost@nokia.com>.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-wavparse
25 * Parse a .wav file into raw or compressed audio.
27 * Wavparse supports both push and pull mode operations, making it possible to
28 * stream from a network source.
31 * <title>Example launch line</title>
33 * gst-launch filesrc location=sine.wav ! wavparse ! audioconvert ! alsasink
34 * ]| Read a wav file and output to the soundcard using the ALSA element. The
35 * wav file is assumed to contain raw uncompressed samples.
37 * gst-launch gnomevfssrc location=http://www.example.org/sine.wav ! queue ! wavparse ! audioconvert ! alsasink
38 * ]| Stream data from a network url.
41 * Last reviewed on 2007-02-14 (0.10.6)
46 * http://replaygain.hydrogenaudio.org/file_format_wav.html
55 #include "gstwavparse.h"
56 #include "gst/riff/riff-ids.h"
57 #include "gst/riff/riff-media.h"
58 #include <gst/base/gsttypefindhelper.h>
59 #include <gst/gst-i18n-plugin.h>
61 GST_DEBUG_CATEGORY_STATIC (wavparse_debug);
62 #define GST_CAT_DEFAULT (wavparse_debug)
64 static void gst_wavparse_dispose (GObject * object);
66 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad);
67 static gboolean gst_wavparse_sink_activate_pull (GstPad * sinkpad,
69 static gboolean gst_wavparse_send_event (GstElement * element,
71 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
72 GstStateChange transition);
74 static const GstQueryType *gst_wavparse_get_query_types (GstPad * pad);
75 static gboolean gst_wavparse_pad_query (GstPad * pad, GstQuery * query);
76 static gboolean gst_wavparse_pad_convert (GstPad * pad,
78 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
80 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstBuffer * buf);
81 static gboolean gst_wavparse_sink_event (GstPad * pad, GstEvent * event);
82 static void gst_wavparse_loop (GstPad * pad);
83 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstEvent * event);
85 static GstStaticPadTemplate sink_template_factory =
86 GST_STATIC_PAD_TEMPLATE ("sink",
89 GST_STATIC_CAPS ("audio/x-wav")
93 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
95 #define gst_wavparse_parent_class parent_class
96 G_DEFINE_TYPE_WITH_CODE (GstWavParse, gst_wavparse, GST_TYPE_ELEMENT,
100 gst_wavparse_class_init (GstWavParseClass * klass)
102 GstElementClass *gstelement_class;
103 GObjectClass *object_class;
104 GstPadTemplate *src_template;
106 gstelement_class = (GstElementClass *) klass;
107 object_class = (GObjectClass *) klass;
109 parent_class = g_type_class_peek_parent (klass);
111 object_class->dispose = gst_wavparse_dispose;
113 gstelement_class->change_state = gst_wavparse_change_state;
114 gstelement_class->send_event = gst_wavparse_send_event;
117 gst_element_class_add_pad_template (gstelement_class,
118 gst_static_pad_template_get (&sink_template_factory));
120 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
121 GST_PAD_ALWAYS, gst_riff_create_audio_template_caps ());
122 gst_element_class_add_pad_template (gstelement_class, src_template);
124 gst_element_class_set_details_simple (gstelement_class, "WAV audio demuxer",
125 "Codec/Demuxer/Audio",
126 "Parse a .wav file into raw audio",
127 "Erik Walthinsen <omega@cse.ogi.edu>");
131 gst_wavparse_reset (GstWavParse * wav)
133 wav->state = GST_WAVPARSE_START;
135 /* These will all be set correctly in the fmt chunk */
149 wav->got_fmt = FALSE;
153 gst_event_unref (wav->seek_event);
154 wav->seek_event = NULL;
156 gst_adapter_clear (wav->adapter);
157 g_object_unref (wav->adapter);
161 gst_tag_list_free (wav->tags);
164 gst_caps_unref (wav->caps);
166 if (wav->start_segment)
167 gst_event_unref (wav->start_segment);
168 wav->start_segment = NULL;
172 gst_wavparse_dispose (GObject * object)
174 GstWavParse *wav = GST_WAVPARSE (object);
176 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
177 gst_wavparse_reset (wav);
179 G_OBJECT_CLASS (parent_class)->dispose (object);
183 gst_wavparse_init (GstWavParse * wavparse)
185 gst_wavparse_reset (wavparse);
189 gst_pad_new_from_static_template (&sink_template_factory, "sink");
190 gst_pad_set_activate_function (wavparse->sinkpad,
191 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
192 gst_pad_set_activatepull_function (wavparse->sinkpad,
193 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_pull));
194 gst_pad_set_chain_function (wavparse->sinkpad,
195 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
196 gst_pad_set_event_function (wavparse->sinkpad,
197 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
198 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
202 gst_pad_new_from_template (gst_element_class_get_pad_template
203 (GST_ELEMENT_GET_CLASS (wavparse), "src"), "src");
204 gst_pad_use_fixed_caps (wavparse->srcpad);
205 gst_pad_set_query_type_function (wavparse->srcpad,
206 GST_DEBUG_FUNCPTR (gst_wavparse_get_query_types));
207 gst_pad_set_query_function (wavparse->srcpad,
208 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
209 gst_pad_set_event_function (wavparse->srcpad,
210 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
211 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
215 gst_wavparse_destroy_sourcepad (GstWavParse * wavparse)
217 if (wavparse->srcpad) {
218 gst_element_remove_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
219 wavparse->srcpad = NULL;
223 /* Compute (value * nom) % denom, avoiding overflow. This can be used
224 * to perform ceiling or rounding division together with
225 * gst_util_uint64_scale[_int]. */
226 #define uint64_scale_modulo(val, nom, denom) \
227 ((val % denom) * (nom % denom) % denom)
229 /* Like gst_util_uint64_scale, but performs ceiling division. */
231 uint64_ceiling_scale_int (guint64 val, gint num, gint denom)
233 guint64 result = gst_util_uint64_scale_int (val, num, denom);
235 if (uint64_scale_modulo (val, num, denom) == 0)
241 /* Like gst_util_uint64_scale, but performs ceiling division. */
243 uint64_ceiling_scale (guint64 val, guint64 num, guint64 denom)
245 guint64 result = gst_util_uint64_scale (val, num, denom);
247 if (uint64_scale_modulo (val, num, denom) == 0)
254 /* FIXME: why is that not in use? */
257 gst_wavparse_parse_adtl (GstWavParse * wavparse, int len)
260 GstByteStream *bs = wavparse->bs;
261 gst_riff_chunk *temp_chunk, chunk;
263 struct _gst_riff_labl labl, *temp_labl;
264 struct _gst_riff_ltxt ltxt, *temp_ltxt;
265 struct _gst_riff_note note, *temp_note;
268 GstPropsEntry *entry;
272 props = wavparse->metadata->properties;
276 gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
277 if (got_bytes != sizeof (gst_riff_chunk)) {
280 temp_chunk = (gst_riff_chunk *) tempdata;
282 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
283 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
285 if (chunk.size == 0) {
286 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
287 len -= sizeof (gst_riff_chunk);
292 case GST_RIFF_adtl_labl:
294 gst_bytestream_peek_bytes (bs, &tempdata,
295 sizeof (struct _gst_riff_labl));
296 if (got_bytes != sizeof (struct _gst_riff_labl)) {
300 temp_labl = (struct _gst_riff_labl *) tempdata;
301 labl.id = GUINT32_FROM_LE (temp_labl->id);
302 labl.size = GUINT32_FROM_LE (temp_labl->size);
303 labl.identifier = GUINT32_FROM_LE (temp_labl->identifier);
305 gst_bytestream_flush (bs, sizeof (struct _gst_riff_labl));
306 len -= sizeof (struct _gst_riff_labl);
308 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, labl.size - 4);
309 if (got_bytes != labl.size - 4) {
313 label_name = (char *) tempdata;
315 gst_bytestream_flush (bs, ((labl.size - 4) + 1) & ~1);
316 len -= (((labl.size - 4) + 1) & ~1);
318 new_caps = gst_caps_new ("label",
319 "application/x-gst-metadata",
320 gst_props_new ("identifier", G_TYPE_INT (labl.identifier),
321 "name", G_TYPE_STRING (label_name), NULL));
323 if (gst_props_get (props, "labels", &caps, NULL)) {
324 caps = g_list_append (caps, new_caps);
326 caps = g_list_append (NULL, new_caps);
328 entry = gst_props_entry_new ("labels", GST_PROPS_GLIST (caps));
329 gst_props_add_entry (props, entry);
334 case GST_RIFF_adtl_ltxt:
336 gst_bytestream_peek_bytes (bs, &tempdata,
337 sizeof (struct _gst_riff_ltxt));
338 if (got_bytes != sizeof (struct _gst_riff_ltxt)) {
342 temp_ltxt = (struct _gst_riff_ltxt *) tempdata;
343 ltxt.id = GUINT32_FROM_LE (temp_ltxt->id);
344 ltxt.size = GUINT32_FROM_LE (temp_ltxt->size);
345 ltxt.identifier = GUINT32_FROM_LE (temp_ltxt->identifier);
346 ltxt.length = GUINT32_FROM_LE (temp_ltxt->length);
347 ltxt.purpose = GUINT32_FROM_LE (temp_ltxt->purpose);
348 ltxt.country = GUINT16_FROM_LE (temp_ltxt->country);
349 ltxt.language = GUINT16_FROM_LE (temp_ltxt->language);
350 ltxt.dialect = GUINT16_FROM_LE (temp_ltxt->dialect);
351 ltxt.codepage = GUINT16_FROM_LE (temp_ltxt->codepage);
353 gst_bytestream_flush (bs, sizeof (struct _gst_riff_ltxt));
354 len -= sizeof (struct _gst_riff_ltxt);
356 if (ltxt.size - 20 > 0) {
357 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, ltxt.size - 20);
358 if (got_bytes != ltxt.size - 20) {
362 gst_bytestream_flush (bs, ((ltxt.size - 20) + 1) & ~1);
363 len -= (((ltxt.size - 20) + 1) & ~1);
365 label_name = (char *) tempdata;
370 new_caps = gst_caps_new ("ltxt",
371 "application/x-gst-metadata",
372 gst_props_new ("identifier", G_TYPE_INT (ltxt.identifier),
373 "name", G_TYPE_STRING (label_name),
374 "length", G_TYPE_INT (ltxt.length), NULL));
376 if (gst_props_get (props, "ltxts", &caps, NULL)) {
377 caps = g_list_append (caps, new_caps);
379 caps = g_list_append (NULL, new_caps);
381 entry = gst_props_entry_new ("ltxts", GST_PROPS_GLIST (caps));
382 gst_props_add_entry (props, entry);
387 case GST_RIFF_adtl_note:
389 gst_bytestream_peek_bytes (bs, &tempdata,
390 sizeof (struct _gst_riff_note));
391 if (got_bytes != sizeof (struct _gst_riff_note)) {
395 temp_note = (struct _gst_riff_note *) tempdata;
396 note.id = GUINT32_FROM_LE (temp_note->id);
397 note.size = GUINT32_FROM_LE (temp_note->size);
398 note.identifier = GUINT32_FROM_LE (temp_note->identifier);
400 gst_bytestream_flush (bs, sizeof (struct _gst_riff_note));
401 len -= sizeof (struct _gst_riff_note);
403 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, note.size - 4);
404 if (got_bytes != note.size - 4) {
408 gst_bytestream_flush (bs, ((note.size - 4) + 1) & ~1);
409 len -= (((note.size - 4) + 1) & ~1);
411 label_name = (char *) tempdata;
413 new_caps = gst_caps_new ("note",
414 "application/x-gst-metadata",
415 gst_props_new ("identifier", G_TYPE_INT (note.identifier),
416 "name", G_TYPE_STRING (label_name), NULL));
418 if (gst_props_get (props, "notes", &caps, NULL)) {
419 caps = g_list_append (caps, new_caps);
421 caps = g_list_append (NULL, new_caps);
423 entry = gst_props_entry_new ("notes", GST_PROPS_GLIST (caps));
424 gst_props_add_entry (props, entry);
430 g_print ("Unknown chunk: %" GST_FOURCC_FORMAT "\n",
431 GST_FOURCC_ARGS (chunk.id));
436 g_object_notify (G_OBJECT (wavparse), "metadata");
440 gst_wavparse_parse_cues (GstWavParse * wavparse, int len)
443 GstByteStream *bs = wavparse->bs;
444 struct _gst_riff_cue *temp_cue, cue;
445 struct _gst_riff_cuepoints *points;
449 GstPropsEntry *entry;
455 gst_bytestream_peek_bytes (bs, &tempdata,
456 sizeof (struct _gst_riff_cue));
457 temp_cue = (struct _gst_riff_cue *) tempdata;
459 /* fixup for our big endian friends */
460 cue.id = GUINT32_FROM_LE (temp_cue->id);
461 cue.size = GUINT32_FROM_LE (temp_cue->size);
462 cue.cuepoints = GUINT32_FROM_LE (temp_cue->cuepoints);
464 gst_bytestream_flush (bs, sizeof (struct _gst_riff_cue));
465 if (got_bytes != sizeof (struct _gst_riff_cue)) {
469 len -= sizeof (struct _gst_riff_cue);
471 /* -4 because cue.size contains the cuepoints size
472 and we've already flushed that out of the system */
473 required = cue.size - 4;
474 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, required);
475 gst_bytestream_flush (bs, ((required) + 1) & ~1);
476 if (got_bytes != required) {
480 len -= (((cue.size - 4) + 1) & ~1);
482 /* now we have an array of struct _gst_riff_cuepoints in tempdata */
483 points = (struct _gst_riff_cuepoints *) tempdata;
485 for (i = 0; i < cue.cuepoints; i++) {
488 caps = gst_caps_new ("cues",
489 "application/x-gst-metadata",
490 gst_props_new ("identifier", G_TYPE_INT (points[i].identifier),
491 "position", G_TYPE_INT (points[i].offset), NULL));
492 cues = g_list_append (cues, caps);
495 entry = gst_props_entry_new ("cues", GST_PROPS_GLIST (cues));
496 gst_props_add_entry (wavparse->metadata->properties, entry);
499 g_object_notify (G_OBJECT (wavparse), "metadata");
502 /* Read 'fmt ' header */
504 gst_wavparse_fmt (GstWavParse * wav)
506 gst_riff_strf_auds *header = NULL;
509 if (!gst_riff_read_strf_auds (wav, &header))
512 wav->format = header->format;
513 wav->rate = header->rate;
514 wav->channels = header->channels;
515 if (wav->channels == 0)
518 wav->blockalign = header->blockalign;
519 wav->width = (header->blockalign * 8) / header->channels;
520 wav->depth = header->size;
521 wav->bps = header->av_bps;
525 /* Note: gst_riff_create_audio_caps might need to fix values in
526 * the header header depending on the format, so call it first */
527 caps = gst_riff_create_audio_caps (header->format, NULL, header, NULL);
533 gst_wavparse_create_sourcepad (wav);
534 gst_pad_use_fixed_caps (wav->srcpad);
535 gst_pad_set_active (wav->srcpad, TRUE);
536 gst_pad_set_caps (wav->srcpad, caps);
537 gst_caps_free (caps);
538 gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
539 gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
541 GST_DEBUG ("frequency %d, channels %d", wav->rate, wav->channels);
548 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
549 ("No FMT tag found"));
554 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
555 ("Stream claims to contain zero channels - invalid data"));
561 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
562 ("Stream claims to bitrate of <= zero - invalid data"));
568 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
574 gst_wavparse_other (GstWavParse * wav)
578 if (!gst_riff_peek_head (wav, &tag, &length, NULL)) {
579 GST_WARNING_OBJECT (wav, "could not peek head");
582 GST_DEBUG_OBJECT (wav, "got tag (%08x) %4.4s, length %d", tag,
583 (gchar *) & tag, length);
586 case GST_RIFF_TAG_LIST:
587 if (!(tag = gst_riff_peek_list (wav))) {
588 GST_WARNING_OBJECT (wav, "could not peek list");
593 case GST_RIFF_LIST_INFO:
594 if (!gst_riff_read_list (wav, &tag) || !gst_riff_read_info (wav)) {
595 GST_WARNING_OBJECT (wav, "could not read list");
600 case GST_RIFF_LIST_adtl:
601 if (!gst_riff_read_skip (wav)) {
602 GST_WARNING_OBJECT (wav, "could not read skip");
608 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag,
610 if (!gst_riff_read_skip (wav)) {
611 GST_WARNING_OBJECT (wav, "could not read skip");
619 case GST_RIFF_TAG_data:
620 if (!gst_bytestream_flush (wav->bs, 8)) {
621 GST_WARNING_OBJECT (wav, "could not flush 8 bytes");
625 GST_DEBUG_OBJECT (wav, "switching to data mode");
626 wav->state = GST_WAVPARSE_DATA;
627 wav->datastart = gst_bytestream_tell (wav->bs);
631 /* length is 0, data probably stretches to the end
633 GST_DEBUG_OBJECT (wav, "length is 0 trying to find length");
634 /* get length of file */
635 file_length = gst_bytestream_length (wav->bs);
636 if (file_length == -1) {
637 GST_DEBUG_OBJECT (wav,
638 "could not get file length, assuming data to eof");
639 /* could not get length, assuming till eof */
640 length = G_MAXUINT32;
642 if (file_length > G_MAXUINT32) {
643 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
644 ", clipping to 32 bits", file_length);
645 /* could not get length, assuming till eof */
646 length = G_MAXUINT32;
648 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
649 ", datalength %u", file_length, length);
650 /* substract offset of datastart from length */
651 length = file_length - wav->datastart;
652 GST_DEBUG_OBJECT (wav, "datalength %u", length);
655 wav->datasize = (guint64) length;
656 GST_DEBUG_OBJECT (wav, "datasize = %ld", length)
659 case GST_RIFF_TAG_cue:
660 if (!gst_riff_read_skip (wav)) {
661 GST_WARNING_OBJECT (wav, "could not read skip");
667 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag, (gchar *) & tag);
668 if (!gst_riff_read_skip (wav))
679 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
683 if (!gst_riff_parse_file_header (element, buf, &doctype))
686 if (doctype != GST_RIFF_RIFF_WAVE)
694 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
695 ("File is not a WAVE file: %" GST_FOURCC_FORMAT,
696 GST_FOURCC_ARGS (doctype)));
702 gst_wavparse_stream_init (GstWavParse * wav)
705 GstBuffer *buf = NULL;
707 if ((res = gst_pad_pull_range (wav->sinkpad,
708 wav->offset, 12, &buf)) != GST_FLOW_OK)
710 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
711 return GST_FLOW_ERROR;
719 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
721 /* -1 always maps to -1 */
727 /* 0 always maps to 0 */
734 *bytepos = uint64_ceiling_scale (ts, (guint64) wav->bps, GST_SECOND);
736 } else if (wav->fact) {
738 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
739 *bytepos = uint64_ceiling_scale (ts, bps, GST_SECOND);
746 /* This function is used to perform seeks on the element.
748 * It also works when event is NULL, in which case it will just
749 * start from the last configured segment. This technique is
750 * used when activating the element and to perform the seek in
754 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
758 GstFormat format, bformat;
760 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
761 gint64 cur, stop, upstream_size;
764 GstSegment seeksegment = { 0, };
768 GST_DEBUG_OBJECT (wav, "doing seek with event");
770 gst_event_parse_seek (event, &rate, &format, &flags,
771 &cur_type, &cur, &stop_type, &stop);
773 /* no negative rates yet */
777 if (format != wav->segment.format) {
778 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
779 gst_format_get_name (format),
780 gst_format_get_name (wav->segment.format));
782 if (cur_type != GST_SEEK_TYPE_NONE)
784 gst_pad_query_convert (wav->srcpad, format, cur,
785 wav->segment.format, &cur);
786 if (res && stop_type != GST_SEEK_TYPE_NONE)
788 gst_pad_query_convert (wav->srcpad, format, stop,
789 wav->segment.format, &stop);
793 format = wav->segment.format;
796 GST_DEBUG_OBJECT (wav, "doing seek without event");
799 cur_type = GST_SEEK_TYPE_SET;
800 stop_type = GST_SEEK_TYPE_SET;
803 /* in push mode, we must delegate to upstream */
804 if (wav->streaming) {
805 gboolean res = FALSE;
807 /* if streaming not yet started; only prepare initial newsegment */
808 if (!event || wav->state != GST_WAVPARSE_DATA) {
809 if (wav->start_segment)
810 gst_event_unref (wav->start_segment);
812 /* wav->start_segment =
813 gst_event_new_new_segment (FALSE, wav->segment.rate,
814 wav->segment.format, wav->segment.last_stop, wav->segment.duration,
815 wav->segment.last_stop);*/
818 /* convert seek positions to byte positions in data sections */
819 if (format == GST_FORMAT_TIME) {
820 /* should not fail */
821 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
823 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
826 /* mind sample boundary and header */
828 cur -= (cur % wav->bytes_per_sample);
829 cur += wav->datastart;
832 stop -= (stop % wav->bytes_per_sample);
833 stop += wav->datastart;
835 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
836 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
838 /* BYTE seek event */
839 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
841 res = gst_pad_push_event (wav->sinkpad, event);
847 flush = flags & GST_SEEK_FLAG_FLUSH;
849 /* now we need to make sure the streaming thread is stopped. We do this by
850 * either sending a FLUSH_START event downstream which will cause the
851 * streaming thread to stop with a WRONG_STATE.
852 * For a non-flushing seek we simply pause the task, which will happen as soon
853 * as it completes one iteration (and thus might block when the sink is
854 * blocking in preroll). */
857 GST_DEBUG_OBJECT (wav, "sending flush start");
858 gst_pad_push_event (wav->srcpad, gst_event_new_flush_start ());
861 gst_pad_pause_task (wav->sinkpad);
864 /* we should now be able to grab the streaming thread because we stopped it
865 * with the above flush/pause code */
866 GST_PAD_STREAM_LOCK (wav->sinkpad);
868 /* save current position */
869 last_stop = wav->segment.position;
871 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
873 /* copy segment, we need this because we still need the old
874 * segment when we close the current segment. */
875 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
877 /* configure the seek parameters in the seeksegment. We will then have the
878 * right values in the segment to perform the seek */
880 GST_DEBUG_OBJECT (wav, "configuring seek");
881 gst_segment_do_seek (&seeksegment, rate, format, flags,
882 cur_type, cur, stop_type, stop, &update);
885 /* figure out the last position we need to play. If it's configured (stop !=
886 * -1), use that, else we play until the total duration of the file */
887 if ((stop = seeksegment.stop) == -1)
888 stop = seeksegment.duration;
890 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
891 if ((cur_type != GST_SEEK_TYPE_NONE)) {
892 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
893 * we can just copy the last_stop. If not, we use the bps to convert TIME to
895 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
896 (gint64 *) & wav->offset))
897 wav->offset = seeksegment.position;
898 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
899 wav->offset -= (wav->offset % wav->bytes_per_sample);
900 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
901 wav->offset += wav->datastart;
902 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
904 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
908 if (stop_type != GST_SEEK_TYPE_NONE) {
909 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
910 wav->end_offset = stop;
911 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
912 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
913 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
914 wav->end_offset += wav->datastart;
915 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
917 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
921 /* make sure filesize is not exceeded due to rounding errors or so,
922 * same precaution as in _stream_headers */
923 bformat = GST_FORMAT_BYTES;
924 if (gst_pad_query_peer_duration (wav->sinkpad, bformat, &upstream_size))
925 wav->end_offset = MIN (wav->end_offset, upstream_size);
927 /* this is the range of bytes we will use for playback */
928 wav->offset = MIN (wav->offset, wav->end_offset);
929 wav->dataleft = wav->end_offset - wav->offset;
931 GST_DEBUG_OBJECT (wav,
932 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
933 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
934 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
936 /* prepare for streaming again */
939 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
940 GST_DEBUG_OBJECT (wav, "sending flush stop");
941 gst_pad_push_event (wav->srcpad, gst_event_new_flush_stop (TRUE));
945 /* now we did the seek and can activate the new segment values */
946 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
948 /* if we're doing a segment seek, post a SEGMENT_START message */
949 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
950 gst_element_post_message (GST_ELEMENT_CAST (wav),
951 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
952 wav->segment.format, wav->segment.position));
955 /* now create the newsegment */
956 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
957 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
959 /* store the newsegment event so it can be sent from the streaming thread. */
960 if (wav->start_segment)
961 gst_event_unref (wav->start_segment);
962 wav->start_segment = gst_event_new_segment (&wav->segment);
964 /* mark discont if we are going to stream from another position. */
965 if (last_stop != wav->segment.position) {
966 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
970 /* and start the streaming task again */
971 if (!wav->streaming) {
972 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
976 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
983 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
988 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
993 GST_DEBUG_OBJECT (wav,
994 "Could not determine byte position for desired time");
1000 * gst_wavparse_peek_chunk_info:
1001 * @wav Wavparse object
1002 * @tag holder for tag
1003 * @size holder for tag size
1005 * Peek next chunk info (tag and size)
1007 * Returns: %TRUE when the chunk info (header) is available
1010 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
1012 const guint8 *data = NULL;
1014 if (gst_adapter_available (wav->adapter) < 8)
1017 data = gst_adapter_map (wav->adapter, 8);
1018 *tag = GST_READ_UINT32_LE (data);
1019 *size = GST_READ_UINT32_LE (data + 4);
1020 gst_adapter_unmap (wav->adapter, 0);
1022 GST_DEBUG ("Next chunk size is %d bytes, type %" GST_FOURCC_FORMAT, *size,
1023 GST_FOURCC_ARGS (*tag));
1029 * gst_wavparse_peek_chunk:
1030 * @wav Wavparse object
1031 * @tag holder for tag
1032 * @size holder for tag size
1034 * Peek enough data for one full chunk
1036 * Returns: %TRUE when the full chunk is available
1039 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
1041 guint32 peek_size = 0;
1044 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
1047 /* size 0 -> empty data buffer would surprise most callers,
1048 * large size -> do not bother trying to squeeze that into adapter,
1049 * so we throw poor man's exception, which can be caught if caller really
1050 * wants to handle 0 size chunk */
1051 if (!(*size) || (*size) >= (1 << 30)) {
1052 GST_INFO ("Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT,
1053 *size, GST_FOURCC_ARGS (*tag));
1054 /* chain should give up */
1055 wav->abort_buffering = TRUE;
1058 peek_size = (*size + 1) & ~1;
1059 available = gst_adapter_available (wav->adapter);
1061 if (available >= (8 + peek_size)) {
1064 GST_LOG ("but only %u bytes available now", available);
1070 * gst_wavparse_calculate_duration:
1071 * @wav: wavparse object
1073 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
1076 * Returns: %TRUE if duration is available.
1079 gst_wavparse_calculate_duration (GstWavParse * wav)
1081 if (wav->duration > 0)
1085 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
1087 uint64_ceiling_scale (wav->datasize, GST_SECOND, (guint64) wav->bps);
1088 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
1089 GST_TIME_ARGS (wav->duration));
1091 } else if (wav->fact) {
1092 wav->duration = uint64_ceiling_scale_int (GST_SECOND, wav->fact, wav->rate);
1093 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
1094 GST_TIME_ARGS (wav->duration));
1101 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
1106 if (wav->streaming) {
1107 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1110 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
1111 GST_FOURCC_ARGS (tag));
1112 flush = 8 + ((size + 1) & ~1);
1113 wav->offset += flush;
1114 if (wav->streaming) {
1115 gst_adapter_flush (wav->adapter, flush);
1117 gst_buffer_unref (buf);
1123 #define MAX_BUFFER_SIZE 4096
1125 static GstFlowReturn
1126 gst_wavparse_stream_headers (GstWavParse * wav)
1128 GstFlowReturn res = GST_FLOW_OK;
1129 GstBuffer *buf = NULL;
1130 gst_riff_strf_auds *header = NULL;
1132 gboolean gotdata = FALSE;
1133 GstCaps *caps = NULL;
1134 gchar *codec_name = NULL;
1136 gint64 upstream_size = 0;
1138 /* search for "_fmt" chunk, which should be first */
1139 while (!wav->got_fmt) {
1142 /* The header starts with a 'fmt ' tag */
1143 if (wav->streaming) {
1144 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1147 gst_adapter_flush (wav->adapter, 8);
1151 buf = gst_adapter_take_buffer (wav->adapter, size);
1153 gst_adapter_flush (wav->adapter, 1);
1154 wav->offset += GST_ROUND_UP_2 (size);
1156 buf = gst_buffer_new ();
1159 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1160 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1164 if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ ||
1165 tag == GST_RIFF_TAG_bext || tag == GST_RIFF_TAG_BEXT ||
1166 tag == GST_RIFF_TAG_LIST) {
1167 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1168 GST_FOURCC_ARGS (tag));
1169 gst_buffer_unref (buf);
1174 if (tag != GST_RIFF_TAG_fmt)
1177 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1179 goto parse_header_error;
1181 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1183 /* do sanity checks of header fields */
1184 if (header->channels == 0)
1186 if (header->rate == 0)
1189 GST_DEBUG_OBJECT (wav, "creating the caps");
1191 /* Note: gst_riff_create_audio_caps might need to fix values in
1192 * the header header depending on the format, so call it first */
1193 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1197 gst_buffer_unref (extra);
1200 goto unknown_format;
1202 /* do more sanity checks of header fields
1203 * (these can be sanitized by gst_riff_create_audio_caps()
1205 wav->format = header->format;
1206 wav->rate = header->rate;
1207 wav->channels = header->channels;
1208 wav->blockalign = header->blockalign;
1209 wav->depth = header->size;
1210 wav->av_bps = header->av_bps;
1216 /* do format specific handling */
1217 switch (wav->format) {
1218 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1219 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1221 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1222 * bitrate inside the mpeg stream */
1223 GST_INFO ("resetting bps from %d to 0 for mp2/3", wav->av_bps);
1227 case GST_RIFF_WAVE_FORMAT_PCM:
1228 if (wav->blockalign > wav->channels * (guint) ceil (wav->depth / 8.0))
1229 goto invalid_blockalign;
1232 if (wav->av_bps > wav->blockalign * wav->rate)
1234 /* use the configured bps */
1235 wav->bps = wav->av_bps;
1239 wav->width = (wav->blockalign * 8) / wav->channels;
1240 wav->bytes_per_sample = wav->channels * wav->width / 8;
1242 if (wav->bytes_per_sample <= 0)
1243 goto no_bytes_per_sample;
1245 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1246 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1247 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1248 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1249 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1250 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1251 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1253 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1254 * formats). This will make the element output a BYTE format segment and
1255 * will not timestamp the outgoing buffers.
1257 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1259 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1261 /* create pad later so we can sniff the first few bytes
1262 * of the real data and correct our caps if necessary */
1263 gst_caps_replace (&wav->caps, caps);
1264 gst_caps_replace (&caps, NULL);
1266 wav->got_fmt = TRUE;
1269 wav->tags = gst_tag_list_new_empty ();
1271 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1272 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1274 g_free (codec_name);
1280 gst_pad_query_peer_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1281 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1283 /* loop headers until we get data */
1285 if (wav->streaming) {
1286 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1292 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1293 &buf)) != GST_FLOW_OK)
1294 goto header_read_error;
1295 data = gst_buffer_map (buf, NULL, NULL, -1);
1296 tag = GST_READ_UINT32_LE (data);
1297 size = GST_READ_UINT32_LE (data + 4);
1298 gst_buffer_unmap (buf, data, -1);
1301 GST_INFO_OBJECT (wav,
1302 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT,
1303 GST_FOURCC_ARGS (tag), wav->offset);
1305 /* wav is a st00pid format, we don't know for sure where data starts.
1306 * So we have to go bit by bit until we find the 'data' header
1309 case GST_RIFF_TAG_data:{
1310 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %d", size);
1311 if (wav->streaming) {
1312 gst_adapter_flush (wav->adapter, 8);
1315 gst_buffer_unref (buf);
1318 wav->datastart = wav->offset;
1319 /* If size is zero, then the data chunk probably actually extends to
1320 the end of the file */
1321 if (size == 0 && upstream_size) {
1322 size = upstream_size - wav->datastart;
1324 /* Or the file might be truncated */
1325 else if (upstream_size) {
1326 size = MIN (size, (upstream_size - wav->datastart));
1328 wav->datasize = (guint64) size;
1329 wav->dataleft = (guint64) size;
1330 wav->end_offset = size + wav->datastart;
1331 if (!wav->streaming) {
1332 /* We will continue parsing tags 'till end */
1333 wav->offset += size;
1335 GST_DEBUG_OBJECT (wav, "datasize = %d", size);
1338 case GST_RIFF_TAG_fact:{
1339 if (wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1340 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1341 const guint data_size = 4;
1343 GST_INFO_OBJECT (wav, "Have fact chunk");
1344 if (size < data_size) {
1345 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1346 /* need more data */
1349 GST_DEBUG_OBJECT (wav, "need %d, available %d; ignoring chunk",
1353 /* number of samples (for compressed formats) */
1354 if (wav->streaming) {
1355 const guint8 *data = NULL;
1357 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1360 gst_adapter_flush (wav->adapter, 8);
1361 data = gst_adapter_map (wav->adapter, data_size);
1362 wav->fact = GST_READ_UINT32_LE (data);
1363 gst_adapter_unmap (wav->adapter, GST_ROUND_UP_2 (size));
1365 gst_buffer_unref (buf);
1367 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1368 data_size, &buf)) != GST_FLOW_OK)
1369 goto header_read_error;
1370 gst_buffer_extract (buf, 0, &wav->fact, 4);
1371 wav->fact = GUINT32_FROM_LE (wav->fact);
1372 gst_buffer_unref (buf);
1374 GST_DEBUG_OBJECT (wav, "have fact %u", wav->fact);
1375 wav->offset += 8 + GST_ROUND_UP_2 (size);
1378 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1379 /* need more data */
1385 case GST_RIFF_TAG_acid:{
1386 const gst_riff_acid *acid = NULL;
1387 const guint data_size = sizeof (gst_riff_acid);
1390 GST_INFO_OBJECT (wav, "Have acid chunk");
1391 if (size < data_size) {
1392 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1393 /* need more data */
1396 GST_DEBUG_OBJECT (wav, "need %d, available %d; ignoring chunk",
1400 if (wav->streaming) {
1401 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1404 gst_adapter_flush (wav->adapter, 8);
1405 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1407 tempo = acid->tempo;
1408 gst_adapter_unmap (wav->adapter, 0);
1410 gst_buffer_unref (buf);
1412 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1413 size, &buf)) != GST_FLOW_OK)
1414 goto header_read_error;
1415 acid = (const gst_riff_acid *) gst_buffer_map (buf, NULL, NULL,
1417 tempo = acid->tempo;
1418 gst_buffer_unmap (buf, (guint8 *) acid, -1);
1420 /* send data as tags */
1422 wav->tags = gst_tag_list_new_empty ();
1423 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1424 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1426 size = GST_ROUND_UP_2 (size);
1427 if (wav->streaming) {
1428 gst_adapter_flush (wav->adapter, size);
1430 gst_buffer_unref (buf);
1432 wav->offset += 8 + size;
1435 /* FIXME: all list tags after data are ignored in streaming mode */
1436 case GST_RIFF_TAG_LIST:{
1439 if (wav->streaming) {
1440 const guint8 *data = NULL;
1442 if (gst_adapter_available (wav->adapter) < 12) {
1445 data = gst_adapter_map (wav->adapter, 12);
1446 ltag = GST_READ_UINT32_LE (data + 8);
1447 gst_adapter_unmap (wav->adapter, 0);
1449 gst_buffer_unref (buf);
1451 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1452 &buf)) != GST_FLOW_OK)
1453 goto header_read_error;
1454 gst_buffer_extract (buf, 8, <ag, 4);
1455 ltag = GUINT32_FROM_LE (ltag);
1458 case GST_RIFF_LIST_INFO:{
1459 const gint data_size = size - 4;
1462 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1463 if (wav->streaming) {
1464 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1467 gst_adapter_flush (wav->adapter, 12);
1469 if (data_size > 0) {
1470 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1472 gst_adapter_flush (wav->adapter, 1);
1476 gst_buffer_unref (buf);
1477 if (data_size > 0) {
1479 gst_pad_pull_range (wav->sinkpad, wav->offset,
1480 data_size, &buf)) != GST_FLOW_OK)
1481 goto header_read_error;
1484 if (data_size > 0) {
1486 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1488 GstTagList *old = wav->tags;
1490 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1492 gst_tag_list_free (old);
1493 gst_tag_list_free (new);
1495 gst_buffer_unref (buf);
1496 wav->offset += GST_ROUND_UP_2 (data_size);
1501 GST_INFO_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1502 GST_FOURCC_ARGS (ltag));
1503 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1504 /* need more data */
1511 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1512 /* need more data */
1517 if (upstream_size && (wav->offset >= upstream_size)) {
1518 /* Now we are gone through the whole file */
1523 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1525 if (wav->bps <= 0 && wav->fact) {
1527 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1529 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1530 (guint64) wav->fact);
1531 GST_INFO_OBJECT (wav, "calculated bps : %d, enabling VBR", wav->bps);
1536 if (gst_wavparse_calculate_duration (wav)) {
1537 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1538 wav->segment.duration = wav->duration;
1540 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1541 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1542 wav->segment.duration = wav->datasize;
1545 /* now we have all the info to perform a pending seek if any, if no
1546 * event, this will still do the right thing and it will also send
1547 * the right newsegment event downstream. */
1548 gst_wavparse_perform_seek (wav, wav->seek_event);
1549 /* remove pending event */
1550 event_p = &wav->seek_event;
1551 gst_event_replace (event_p, NULL);
1553 /* we just started, we are discont */
1554 wav->discont = TRUE;
1556 wav->state = GST_WAVPARSE_DATA;
1558 /* determine reasonable max buffer size,
1559 * that is, buffers not too small either size or time wise
1560 * so we do not end up with too many of them */
1563 gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size);
1564 wav->max_buf_size = upstream_size;
1565 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1566 if (wav->blockalign > 0)
1567 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1569 GST_DEBUG_OBJECT (wav, "max buffer size %d", wav->max_buf_size);
1577 g_free (codec_name);
1581 gst_caps_unref (caps);
1586 res = GST_FLOW_ERROR;
1591 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1592 ("Invalid WAV header (no fmt at start): %"
1593 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1598 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1599 ("Couldn't parse audio header"));
1604 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1605 ("Stream claims to contain no channels - invalid data"));
1610 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1611 ("Stream with sample_rate == 0 - invalid data"));
1616 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1617 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1618 wav->blockalign, wav->channels * (guint) ceil (wav->depth / 8.0)));
1623 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1624 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1625 wav->av_bps, wav->blockalign * wav->rate));
1628 no_bytes_per_sample:
1630 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1631 ("Could not caluclate bytes per sample - invalid data"));
1636 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1637 ("No caps found for format 0x%x, %d channels, %d Hz",
1638 wav->format, wav->channels, wav->rate));
1643 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1644 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1650 * Read WAV file tag when streaming
1652 static GstFlowReturn
1653 gst_wavparse_parse_stream_init (GstWavParse * wav)
1655 if (gst_adapter_available (wav->adapter) >= 12) {
1658 /* _take flushes the data */
1659 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1661 GST_DEBUG ("Parsing wav header");
1662 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1663 return GST_FLOW_ERROR;
1666 /* Go to next state */
1667 wav->state = GST_WAVPARSE_HEADER;
1672 /* handle an event sent directly to the element.
1674 * This event can be sent either in the READY state or the
1675 * >READY state. The only event of interest really is the seek
1678 * In the READY state we can only store the event and try to
1679 * respect it when going to PAUSED. We assume we are in the
1680 * READY state when our parsing state != GST_WAVPARSE_DATA.
1682 * When we are steaming, we can simply perform the seek right
1686 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1688 GstWavParse *wav = GST_WAVPARSE (element);
1689 gboolean res = FALSE;
1692 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1694 switch (GST_EVENT_TYPE (event)) {
1695 case GST_EVENT_SEEK:
1696 if (wav->state == GST_WAVPARSE_DATA) {
1697 /* we can handle the seek directly when streaming data */
1698 res = gst_wavparse_perform_seek (wav, event);
1700 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1702 event_p = &wav->seek_event;
1703 gst_event_replace (event_p, event);
1705 /* we always return true */
1712 gst_event_unref (event);
1717 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1721 s = gst_caps_get_structure (caps, 0);
1722 if (!gst_structure_has_name (s, "audio/x-dts"))
1724 if (prob >= GST_TYPE_FIND_LIKELY)
1726 /* DTS at non-0 offsets and without second sync may yield POSSIBLE .. */
1727 if (prob < GST_TYPE_FIND_POSSIBLE)
1729 /* .. in which case we want at least a valid-looking rate and channels */
1730 if (!gst_structure_has_field (s, "channels"))
1732 /* and for extra assurance we could also check the rate from the DTS frame
1733 * against the one in the wav header, but for now let's not do that */
1734 return gst_structure_has_field (s, "rate");
1738 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1742 GST_DEBUG_OBJECT (wav, "adding src pad");
1745 s = gst_caps_get_structure (wav->caps, 0);
1746 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
1747 GstTypeFindProbability prob;
1750 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1751 if (tf_caps != NULL) {
1752 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1753 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1754 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1755 gst_caps_unref (wav->caps);
1756 wav->caps = tf_caps;
1758 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1759 GST_TAG_AUDIO_CODEC, "dts", NULL);
1761 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1762 "marked as raw PCM audio, but ignoring for now", tf_caps);
1763 gst_caps_unref (tf_caps);
1769 gst_pad_set_caps (wav->srcpad, wav->caps);
1770 gst_caps_replace (&wav->caps, NULL);
1772 if (wav->start_segment) {
1773 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1774 gst_pad_push_event (wav->srcpad, wav->start_segment);
1775 wav->start_segment = NULL;
1779 gst_pad_push_event (wav->srcpad, gst_event_new_tag (wav->tags));
1784 static GstFlowReturn
1785 gst_wavparse_stream_data (GstWavParse * wav)
1787 GstBuffer *buf = NULL;
1788 GstFlowReturn res = GST_FLOW_OK;
1789 guint64 desired, obtained;
1790 GstClockTime timestamp, next_timestamp, duration;
1791 guint64 pos, nextpos;
1794 GST_LOG_OBJECT (wav,
1795 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1796 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1798 /* Get the next n bytes and output them */
1799 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1802 /* scale the amount of data by the segment rate so we get equal
1803 * amounts of data regardless of the playback rate */
1805 MIN (gst_guint64_to_gdouble (wav->dataleft),
1806 wav->max_buf_size * ABS (wav->segment.rate));
1808 if (desired >= wav->blockalign && wav->blockalign > 0)
1809 desired -= (desired % wav->blockalign);
1811 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1812 "from the sinkpad", desired);
1814 if (wav->streaming) {
1815 guint avail = gst_adapter_available (wav->adapter);
1818 /* flush some bytes if evil upstream sends segment that starts
1819 * before data or does is not send sample aligned segment */
1820 if (G_LIKELY (wav->offset >= wav->datastart)) {
1821 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1823 extra = wav->datastart - wav->offset;
1826 if (G_UNLIKELY (extra)) {
1827 extra = wav->bytes_per_sample - extra;
1828 if (extra <= avail) {
1829 GST_DEBUG_OBJECT (wav, "flushing %d bytes to sample boundary", extra);
1830 gst_adapter_flush (wav->adapter, extra);
1831 wav->offset += extra;
1832 wav->dataleft -= extra;
1833 goto iterate_adapter;
1835 GST_DEBUG_OBJECT (wav, "flushing %d bytes", avail);
1836 gst_adapter_clear (wav->adapter);
1837 wav->offset += avail;
1838 wav->dataleft -= avail;
1843 if (avail < desired) {
1844 GST_LOG_OBJECT (wav, "Got only %d bytes of data from the sinkpad", avail);
1848 buf = gst_adapter_take_buffer (wav->adapter, desired);
1850 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
1851 desired, &buf)) != GST_FLOW_OK)
1854 /* we may get a short buffer at the end of the file */
1855 if (gst_buffer_get_size (buf) < desired) {
1856 gsize size = gst_buffer_get_size (buf);
1858 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
1859 if (size >= wav->blockalign) {
1860 buf = gst_buffer_make_writable (buf);
1861 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
1863 gst_buffer_unref (buf);
1869 obtained = gst_buffer_get_size (buf);
1871 /* our positions in bytes */
1872 pos = wav->offset - wav->datastart;
1873 nextpos = pos + obtained;
1875 /* update offsets, does not overflow. */
1876 buf = gst_buffer_make_writable (buf);
1877 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
1878 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
1880 /* first chunk of data? create the source pad. We do this only here so
1881 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
1882 if (G_UNLIKELY (wav->first)) {
1884 /* this will also push the segment events */
1885 gst_wavparse_add_src_pad (wav, buf);
1887 /* If we have a pending start segment, send it now. */
1888 if (G_UNLIKELY (wav->start_segment != NULL)) {
1889 gst_pad_push_event (wav->srcpad, wav->start_segment);
1890 wav->start_segment = NULL;
1895 /* and timestamps if we have a bitrate, be careful for overflows */
1896 timestamp = uint64_ceiling_scale (pos, GST_SECOND, (guint64) wav->bps);
1898 uint64_ceiling_scale (nextpos, GST_SECOND, (guint64) wav->bps);
1899 duration = next_timestamp - timestamp;
1901 /* update current running segment position */
1902 if (G_LIKELY (next_timestamp >= wav->segment.start))
1903 wav->segment.position = next_timestamp;
1904 } else if (wav->fact) {
1906 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
1907 /* and timestamps if we have a bitrate, be careful for overflows */
1908 timestamp = uint64_ceiling_scale (pos, GST_SECOND, bps);
1909 next_timestamp = uint64_ceiling_scale (nextpos, GST_SECOND, bps);
1910 duration = next_timestamp - timestamp;
1912 /* no bitrate, all we know is that the first sample has timestamp 0, all
1913 * other positions and durations have unknown timestamp. */
1917 timestamp = GST_CLOCK_TIME_NONE;
1918 duration = GST_CLOCK_TIME_NONE;
1919 /* update current running segment position with byte offset */
1920 if (G_LIKELY (nextpos >= wav->segment.start))
1921 wav->segment.position = nextpos;
1923 if ((pos > 0) && wav->vbr) {
1924 /* don't set timestamps for VBR files if it's not the first buffer */
1925 timestamp = GST_CLOCK_TIME_NONE;
1926 duration = GST_CLOCK_TIME_NONE;
1929 GST_DEBUG_OBJECT (wav, "marking DISCONT");
1930 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1931 wav->discont = FALSE;
1934 GST_BUFFER_TIMESTAMP (buf) = timestamp;
1935 GST_BUFFER_DURATION (buf) = duration;
1937 GST_LOG_OBJECT (wav,
1938 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
1939 ", size:%u", GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration),
1940 gst_buffer_get_size (buf));
1942 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
1945 if (obtained < wav->dataleft) {
1946 wav->offset += obtained;
1947 wav->dataleft -= obtained;
1949 wav->offset += wav->dataleft;
1953 /* Iterate until need more data, so adapter size won't grow */
1954 if (wav->streaming) {
1955 GST_LOG_OBJECT (wav,
1956 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
1958 goto iterate_adapter;
1965 GST_DEBUG_OBJECT (wav, "found EOS");
1966 return GST_FLOW_UNEXPECTED;
1970 /* check if we got EOS */
1971 if (res == GST_FLOW_UNEXPECTED)
1974 GST_WARNING_OBJECT (wav,
1975 "Error getting %" G_GINT64_FORMAT " bytes from the "
1976 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
1981 GST_INFO_OBJECT (wav,
1982 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
1983 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
1984 gst_pad_is_linked (wav->srcpad));
1990 gst_wavparse_loop (GstPad * pad)
1993 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
1995 GST_LOG_OBJECT (wav, "process data");
1997 switch (wav->state) {
1998 case GST_WAVPARSE_START:
1999 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2000 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2003 wav->state = GST_WAVPARSE_HEADER;
2006 case GST_WAVPARSE_HEADER:
2007 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2008 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2011 wav->state = GST_WAVPARSE_DATA;
2012 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2015 case GST_WAVPARSE_DATA:
2016 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2020 g_assert_not_reached ();
2027 const gchar *reason = gst_flow_get_name (ret);
2029 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2030 gst_pad_pause_task (pad);
2032 if (ret == GST_FLOW_UNEXPECTED) {
2033 /* handle end-of-stream/segment */
2034 /* so align our position with the end of it, if there is one
2035 * this ensures a subsequent will arrive at correct base/acc time */
2036 if (wav->segment.format == GST_FORMAT_TIME) {
2037 if (wav->segment.rate > 0.0 &&
2038 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2039 wav->segment.position = wav->segment.stop;
2040 else if (wav->segment.rate < 0.0)
2041 wav->segment.position = wav->segment.start;
2043 /* add pad before we perform EOS */
2044 if (G_UNLIKELY (wav->first)) {
2046 gst_wavparse_add_src_pad (wav, NULL);
2049 if (wav->state == GST_WAVPARSE_START)
2050 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2051 ("No valid input found before end of stream"), (NULL));
2053 /* perform EOS logic */
2054 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2057 if ((stop = wav->segment.stop) == -1)
2058 stop = wav->segment.duration;
2060 gst_element_post_message (GST_ELEMENT_CAST (wav),
2061 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2062 wav->segment.format, stop));
2064 if (wav->srcpad != NULL)
2065 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2067 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2068 /* for fatal errors we post an error message, post the error
2069 * first so the app knows about the error first. */
2070 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
2071 (_("Internal data flow error.")),
2072 ("streaming task paused, reason %s (%d)", reason, ret));
2073 if (wav->srcpad != NULL)
2074 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2080 static GstFlowReturn
2081 gst_wavparse_chain (GstPad * pad, GstBuffer * buf)
2084 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2086 GST_LOG_OBJECT (wav, "adapter_push %u bytes", gst_buffer_get_size (buf));
2088 gst_adapter_push (wav->adapter, buf);
2090 switch (wav->state) {
2091 case GST_WAVPARSE_START:
2092 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2093 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2096 if (wav->state != GST_WAVPARSE_HEADER)
2099 /* otherwise fall-through */
2100 case GST_WAVPARSE_HEADER:
2101 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2102 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2105 if (!wav->got_fmt || wav->datastart == 0)
2108 wav->state = GST_WAVPARSE_DATA;
2109 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2112 case GST_WAVPARSE_DATA:
2113 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2114 wav->discont = TRUE;
2115 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2119 g_return_val_if_reached (GST_FLOW_ERROR);
2122 if (G_UNLIKELY (wav->abort_buffering)) {
2123 wav->abort_buffering = FALSE;
2124 ret = GST_FLOW_ERROR;
2125 /* sort of demux/parse error */
2126 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2132 static GstFlowReturn
2133 gst_wavparse_flush_data (GstWavParse * wav)
2135 GstFlowReturn ret = GST_FLOW_OK;
2138 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2140 wav->end_offset = wav->offset + av;
2141 ret = gst_wavparse_stream_data (wav);
2148 gst_wavparse_sink_event (GstPad * pad, GstEvent * event)
2150 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2151 gboolean ret = TRUE;
2153 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2155 switch (GST_EVENT_TYPE (event)) {
2156 case GST_EVENT_CAPS:
2158 /* discard, we'll come up with proper src caps */
2159 gst_event_unref (event);
2162 case GST_EVENT_SEGMENT:
2164 gint64 start, stop, offset = 0, end_offset = -1;
2167 /* some debug output */
2168 gst_event_copy_segment (event, &segment);
2169 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2172 if (wav->state != GST_WAVPARSE_DATA) {
2173 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2177 /* now we are either committed to TIME or BYTE format,
2178 * and we only expect a BYTE segment, e.g. following a seek */
2179 if (segment.format == GST_FORMAT_BYTES) {
2180 /* handle (un)signed issues */
2181 start = segment.start;
2182 stop = segment.stop;
2185 start -= wav->datastart;
2186 start = MAX (start, 0);
2190 segment.stop -= wav->datastart;
2191 segment.stop = MAX (stop, 0);
2193 if (wav->segment.format == GST_FORMAT_TIME) {
2194 guint64 bps = wav->bps;
2196 /* operating in format TIME, so we can convert */
2197 if (!bps && wav->fact)
2199 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2203 uint64_ceiling_scale (start, GST_SECOND, (guint64) wav->bps);
2206 uint64_ceiling_scale (stop, GST_SECOND, (guint64) wav->bps);
2210 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2214 segment.start = start;
2215 segment.stop = stop;
2217 /* accept upstream's notion of segment and distribute along */
2218 segment.time = segment.start = segment.position;
2219 segment.duration = wav->segment.duration;
2220 segment.base = gst_segment_to_running_time (&wav->segment,
2221 GST_FORMAT_TIME, wav->segment.position);
2223 gst_segment_copy_into (&segment, &wav->segment);
2225 /* also store the newsegment event for the streaming thread */
2226 if (wav->start_segment)
2227 gst_event_unref (wav->start_segment);
2228 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2229 wav->start_segment = gst_event_new_segment (&segment);
2231 /* stream leftover data in current segment */
2232 gst_wavparse_flush_data (wav);
2233 /* and set up streaming thread for next one */
2234 wav->offset = offset;
2235 wav->end_offset = end_offset;
2236 if (wav->end_offset > 0) {
2237 wav->dataleft = wav->end_offset - wav->offset;
2239 /* infinity; upstream will EOS when done */
2240 wav->dataleft = G_MAXUINT64;
2243 gst_event_unref (event);
2247 /* add pad if needed so EOS is seen downstream */
2248 if (G_UNLIKELY (wav->first)) {
2250 gst_wavparse_add_src_pad (wav, NULL);
2252 /* stream leftover data in current segment */
2253 gst_wavparse_flush_data (wav);
2256 if (wav->state == GST_WAVPARSE_START)
2257 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2258 ("No valid input found before end of stream"), (NULL));
2261 case GST_EVENT_FLUSH_STOP:
2265 gst_adapter_clear (wav->adapter);
2266 wav->discont = TRUE;
2267 dur = wav->segment.duration;
2268 gst_segment_init (&wav->segment, wav->segment.format);
2269 wav->segment.duration = dur;
2273 ret = gst_pad_event_default (wav->sinkpad, event);
2281 /* convert and query stuff */
2282 static const GstFormat *
2283 gst_wavparse_get_formats (GstPad * pad)
2285 static GstFormat formats[] = {
2288 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2297 gst_wavparse_pad_convert (GstPad * pad,
2298 GstFormat src_format, gint64 src_value,
2299 GstFormat * dest_format, gint64 * dest_value)
2301 GstWavParse *wavparse;
2302 gboolean res = TRUE;
2304 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2306 if (*dest_format == src_format) {
2307 *dest_value = src_value;
2311 if ((wavparse->bps == 0) && !wavparse->fact)
2314 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2315 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2317 switch (src_format) {
2318 case GST_FORMAT_BYTES:
2319 switch (*dest_format) {
2320 case GST_FORMAT_DEFAULT:
2321 *dest_value = src_value / wavparse->bytes_per_sample;
2322 /* make sure we end up on a sample boundary */
2323 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2325 case GST_FORMAT_TIME:
2326 /* src_value + datastart = offset */
2327 GST_INFO_OBJECT (wavparse,
2328 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2330 if (wavparse->bps > 0)
2331 *dest_value = uint64_ceiling_scale (src_value, GST_SECOND,
2332 (guint64) wavparse->bps);
2333 else if (wavparse->fact) {
2334 guint64 bps = uint64_ceiling_scale_int (wavparse->datasize,
2335 wavparse->rate, wavparse->fact);
2337 *dest_value = uint64_ceiling_scale_int (src_value, GST_SECOND, bps);
2348 case GST_FORMAT_DEFAULT:
2349 switch (*dest_format) {
2350 case GST_FORMAT_BYTES:
2351 *dest_value = src_value * wavparse->bytes_per_sample;
2353 case GST_FORMAT_TIME:
2354 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2355 (guint64) wavparse->rate);
2363 case GST_FORMAT_TIME:
2364 switch (*dest_format) {
2365 case GST_FORMAT_BYTES:
2366 if (wavparse->bps > 0)
2367 *dest_value = gst_util_uint64_scale (src_value,
2368 (guint64) wavparse->bps, GST_SECOND);
2370 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2371 wavparse->rate, wavparse->fact);
2373 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2375 /* make sure we end up on a sample boundary */
2376 *dest_value -= *dest_value % wavparse->blockalign;
2378 case GST_FORMAT_DEFAULT:
2379 *dest_value = gst_util_uint64_scale (src_value,
2380 (guint64) wavparse->rate, GST_SECOND);
2399 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2405 static const GstQueryType *
2406 gst_wavparse_get_query_types (GstPad * pad)
2408 static const GstQueryType types[] = {
2419 /* handle queries for location and length in requested format */
2421 gst_wavparse_pad_query (GstPad * pad, GstQuery * query)
2423 gboolean res = TRUE;
2424 GstWavParse *wav = GST_WAVPARSE (gst_pad_get_parent (pad));
2426 /* only if we know */
2427 if (wav->state != GST_WAVPARSE_DATA) {
2428 gst_object_unref (wav);
2432 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2434 switch (GST_QUERY_TYPE (query)) {
2435 case GST_QUERY_POSITION:
2441 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2442 curb = wav->offset - wav->datastart;
2443 gst_query_parse_position (query, &format, NULL);
2444 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2447 case GST_FORMAT_TIME:
2448 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2452 format = GST_FORMAT_BYTES;
2457 gst_query_set_position (query, format, cur);
2460 case GST_QUERY_DURATION:
2462 gint64 duration = 0;
2465 gst_query_parse_duration (query, &format, NULL);
2468 case GST_FORMAT_TIME:{
2469 if ((res = gst_wavparse_calculate_duration (wav))) {
2470 duration = wav->duration;
2475 format = GST_FORMAT_BYTES;
2476 duration = wav->datasize;
2479 gst_query_set_duration (query, format, duration);
2482 case GST_QUERY_CONVERT:
2484 gint64 srcvalue, dstvalue;
2485 GstFormat srcformat, dstformat;
2487 gst_query_parse_convert (query, &srcformat, &srcvalue,
2488 &dstformat, &dstvalue);
2489 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2490 &dstformat, &dstvalue);
2492 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2495 case GST_QUERY_SEEKING:{
2497 gboolean seekable = FALSE;
2499 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2500 if (fmt == wav->segment.format) {
2501 if (wav->streaming) {
2504 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2505 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2506 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2507 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2509 gst_query_unref (q);
2511 GST_LOG_OBJECT (wav, "looping => seekable");
2515 } else if (fmt == GST_FORMAT_TIME) {
2519 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2524 res = gst_pad_query_default (pad, query);
2527 gst_object_unref (wav);
2532 gst_wavparse_srcpad_event (GstPad * pad, GstEvent * event)
2534 GstWavParse *wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
2535 gboolean res = FALSE;
2537 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2539 switch (GST_EVENT_TYPE (event)) {
2540 case GST_EVENT_SEEK:
2541 /* can only handle events when we are in the data state */
2542 if (wavparse->state == GST_WAVPARSE_DATA) {
2543 res = gst_wavparse_perform_seek (wavparse, event);
2545 gst_event_unref (event);
2548 res = gst_pad_push_event (wavparse->sinkpad, event);
2551 gst_object_unref (wavparse);
2556 gst_wavparse_sink_activate (GstPad * sinkpad)
2558 GstWavParse *wav = GST_WAVPARSE (gst_pad_get_parent (sinkpad));
2563 gst_adapter_clear (wav->adapter);
2564 g_object_unref (wav->adapter);
2565 wav->adapter = NULL;
2568 query = gst_query_new_scheduling ();
2570 if (!gst_pad_peer_query (sinkpad, query)) {
2571 gst_query_unref (query);
2575 gst_query_parse_scheduling (query, &pull_mode, NULL, NULL, NULL, NULL, NULL);
2576 gst_query_unref (query);
2581 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2582 wav->streaming = FALSE;
2583 gst_object_unref (wav);
2584 return gst_pad_activate_pull (sinkpad, TRUE);
2588 GST_DEBUG_OBJECT (sinkpad, "activating push");
2589 wav->streaming = TRUE;
2590 wav->adapter = gst_adapter_new ();
2591 gst_object_unref (wav);
2592 return gst_pad_activate_push (sinkpad, TRUE);
2598 gst_wavparse_sink_activate_pull (GstPad * sinkpad, gboolean active)
2601 /* if we have a scheduler we can start the task */
2602 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2605 return gst_pad_stop_task (sinkpad);
2609 static GstStateChangeReturn
2610 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2612 GstStateChangeReturn ret;
2613 GstWavParse *wav = GST_WAVPARSE (element);
2615 switch (transition) {
2616 case GST_STATE_CHANGE_NULL_TO_READY:
2618 case GST_STATE_CHANGE_READY_TO_PAUSED:
2619 gst_wavparse_reset (wav);
2621 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2627 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2629 switch (transition) {
2630 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2632 case GST_STATE_CHANGE_PAUSED_TO_READY:
2633 gst_wavparse_destroy_sourcepad (wav);
2634 gst_wavparse_reset (wav);
2636 case GST_STATE_CHANGE_READY_TO_NULL:
2645 plugin_init (GstPlugin * plugin)
2649 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2653 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2656 "Parse a .wav file into raw audio",
2657 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)