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,
68 static gboolean gst_wavparse_sink_activate_pull (GstPad * sinkpad,
69 GstObject * parent, gboolean active);
70 static gboolean gst_wavparse_send_event (GstElement * element,
72 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
73 GstStateChange transition);
75 static gboolean gst_wavparse_pad_query (GstPad * pad, GstObject * parent,
77 static gboolean gst_wavparse_pad_convert (GstPad * pad, GstFormat src_format,
78 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
80 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstObject * parent,
82 static gboolean gst_wavparse_sink_event (GstPad * pad, GstObject * parent,
84 static void gst_wavparse_loop (GstPad * pad);
85 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent,
88 static GstStaticPadTemplate sink_template_factory =
89 GST_STATIC_PAD_TEMPLATE ("sink",
92 GST_STATIC_CAPS ("audio/x-wav")
96 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
98 #define gst_wavparse_parent_class parent_class
99 G_DEFINE_TYPE_WITH_CODE (GstWavParse, gst_wavparse, GST_TYPE_ELEMENT,
103 gst_wavparse_class_init (GstWavParseClass * klass)
105 GstElementClass *gstelement_class;
106 GObjectClass *object_class;
107 GstPadTemplate *src_template;
109 gstelement_class = (GstElementClass *) klass;
110 object_class = (GObjectClass *) klass;
112 parent_class = g_type_class_peek_parent (klass);
114 object_class->dispose = gst_wavparse_dispose;
116 gstelement_class->change_state = gst_wavparse_change_state;
117 gstelement_class->send_event = gst_wavparse_send_event;
120 gst_element_class_add_pad_template (gstelement_class,
121 gst_static_pad_template_get (&sink_template_factory));
123 src_template = gst_pad_template_new ("src", GST_PAD_SRC,
124 GST_PAD_ALWAYS, gst_riff_create_audio_template_caps ());
125 gst_element_class_add_pad_template (gstelement_class, src_template);
127 gst_element_class_set_details_simple (gstelement_class, "WAV audio demuxer",
128 "Codec/Demuxer/Audio",
129 "Parse a .wav file into raw audio",
130 "Erik Walthinsen <omega@cse.ogi.edu>");
134 gst_wavparse_reset (GstWavParse * wav)
136 wav->state = GST_WAVPARSE_START;
138 /* These will all be set correctly in the fmt chunk */
152 wav->got_fmt = FALSE;
156 gst_event_unref (wav->seek_event);
157 wav->seek_event = NULL;
159 gst_adapter_clear (wav->adapter);
160 g_object_unref (wav->adapter);
164 gst_tag_list_free (wav->tags);
167 gst_caps_unref (wav->caps);
169 if (wav->start_segment)
170 gst_event_unref (wav->start_segment);
171 wav->start_segment = NULL;
175 gst_wavparse_dispose (GObject * object)
177 GstWavParse *wav = GST_WAVPARSE (object);
179 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
180 gst_wavparse_reset (wav);
182 G_OBJECT_CLASS (parent_class)->dispose (object);
186 gst_wavparse_init (GstWavParse * wavparse)
188 gst_wavparse_reset (wavparse);
192 gst_pad_new_from_static_template (&sink_template_factory, "sink");
193 gst_pad_set_activate_function (wavparse->sinkpad,
194 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
195 gst_pad_set_activatepull_function (wavparse->sinkpad,
196 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_pull));
197 gst_pad_set_chain_function (wavparse->sinkpad,
198 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
199 gst_pad_set_event_function (wavparse->sinkpad,
200 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
201 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
205 gst_pad_new_from_template (gst_element_class_get_pad_template
206 (GST_ELEMENT_GET_CLASS (wavparse), "src"), "src");
207 gst_pad_use_fixed_caps (wavparse->srcpad);
208 gst_pad_set_query_function (wavparse->srcpad,
209 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
210 gst_pad_set_event_function (wavparse->srcpad,
211 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
212 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
216 gst_wavparse_destroy_sourcepad (GstWavParse * wavparse)
218 if (wavparse->srcpad) {
219 gst_element_remove_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
220 wavparse->srcpad = NULL;
224 /* Compute (value * nom) % denom, avoiding overflow. This can be used
225 * to perform ceiling or rounding division together with
226 * gst_util_uint64_scale[_int]. */
227 #define uint64_scale_modulo(val, nom, denom) \
228 ((val % denom) * (nom % denom) % denom)
230 /* Like gst_util_uint64_scale, but performs ceiling division. */
232 uint64_ceiling_scale_int (guint64 val, gint num, gint denom)
234 guint64 result = gst_util_uint64_scale_int (val, num, denom);
236 if (uint64_scale_modulo (val, num, denom) == 0)
242 /* Like gst_util_uint64_scale, but performs ceiling division. */
244 uint64_ceiling_scale (guint64 val, guint64 num, guint64 denom)
246 guint64 result = gst_util_uint64_scale (val, num, denom);
248 if (uint64_scale_modulo (val, num, denom) == 0)
255 /* FIXME: why is that not in use? */
258 gst_wavparse_parse_adtl (GstWavParse * wavparse, int len)
261 GstByteStream *bs = wavparse->bs;
262 gst_riff_chunk *temp_chunk, chunk;
264 struct _gst_riff_labl labl, *temp_labl;
265 struct _gst_riff_ltxt ltxt, *temp_ltxt;
266 struct _gst_riff_note note, *temp_note;
269 GstPropsEntry *entry;
273 props = wavparse->metadata->properties;
277 gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
278 if (got_bytes != sizeof (gst_riff_chunk)) {
281 temp_chunk = (gst_riff_chunk *) tempdata;
283 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
284 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
286 if (chunk.size == 0) {
287 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
288 len -= sizeof (gst_riff_chunk);
293 case GST_RIFF_adtl_labl:
295 gst_bytestream_peek_bytes (bs, &tempdata,
296 sizeof (struct _gst_riff_labl));
297 if (got_bytes != sizeof (struct _gst_riff_labl)) {
301 temp_labl = (struct _gst_riff_labl *) tempdata;
302 labl.id = GUINT32_FROM_LE (temp_labl->id);
303 labl.size = GUINT32_FROM_LE (temp_labl->size);
304 labl.identifier = GUINT32_FROM_LE (temp_labl->identifier);
306 gst_bytestream_flush (bs, sizeof (struct _gst_riff_labl));
307 len -= sizeof (struct _gst_riff_labl);
309 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, labl.size - 4);
310 if (got_bytes != labl.size - 4) {
314 label_name = (char *) tempdata;
316 gst_bytestream_flush (bs, ((labl.size - 4) + 1) & ~1);
317 len -= (((labl.size - 4) + 1) & ~1);
319 new_caps = gst_caps_new ("label",
320 "application/x-gst-metadata",
321 gst_props_new ("identifier", G_TYPE_INT (labl.identifier),
322 "name", G_TYPE_STRING (label_name), NULL));
324 if (gst_props_get (props, "labels", &caps, NULL)) {
325 caps = g_list_append (caps, new_caps);
327 caps = g_list_append (NULL, new_caps);
329 entry = gst_props_entry_new ("labels", GST_PROPS_GLIST (caps));
330 gst_props_add_entry (props, entry);
335 case GST_RIFF_adtl_ltxt:
337 gst_bytestream_peek_bytes (bs, &tempdata,
338 sizeof (struct _gst_riff_ltxt));
339 if (got_bytes != sizeof (struct _gst_riff_ltxt)) {
343 temp_ltxt = (struct _gst_riff_ltxt *) tempdata;
344 ltxt.id = GUINT32_FROM_LE (temp_ltxt->id);
345 ltxt.size = GUINT32_FROM_LE (temp_ltxt->size);
346 ltxt.identifier = GUINT32_FROM_LE (temp_ltxt->identifier);
347 ltxt.length = GUINT32_FROM_LE (temp_ltxt->length);
348 ltxt.purpose = GUINT32_FROM_LE (temp_ltxt->purpose);
349 ltxt.country = GUINT16_FROM_LE (temp_ltxt->country);
350 ltxt.language = GUINT16_FROM_LE (temp_ltxt->language);
351 ltxt.dialect = GUINT16_FROM_LE (temp_ltxt->dialect);
352 ltxt.codepage = GUINT16_FROM_LE (temp_ltxt->codepage);
354 gst_bytestream_flush (bs, sizeof (struct _gst_riff_ltxt));
355 len -= sizeof (struct _gst_riff_ltxt);
357 if (ltxt.size - 20 > 0) {
358 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, ltxt.size - 20);
359 if (got_bytes != ltxt.size - 20) {
363 gst_bytestream_flush (bs, ((ltxt.size - 20) + 1) & ~1);
364 len -= (((ltxt.size - 20) + 1) & ~1);
366 label_name = (char *) tempdata;
371 new_caps = gst_caps_new ("ltxt",
372 "application/x-gst-metadata",
373 gst_props_new ("identifier", G_TYPE_INT (ltxt.identifier),
374 "name", G_TYPE_STRING (label_name),
375 "length", G_TYPE_INT (ltxt.length), NULL));
377 if (gst_props_get (props, "ltxts", &caps, NULL)) {
378 caps = g_list_append (caps, new_caps);
380 caps = g_list_append (NULL, new_caps);
382 entry = gst_props_entry_new ("ltxts", GST_PROPS_GLIST (caps));
383 gst_props_add_entry (props, entry);
388 case GST_RIFF_adtl_note:
390 gst_bytestream_peek_bytes (bs, &tempdata,
391 sizeof (struct _gst_riff_note));
392 if (got_bytes != sizeof (struct _gst_riff_note)) {
396 temp_note = (struct _gst_riff_note *) tempdata;
397 note.id = GUINT32_FROM_LE (temp_note->id);
398 note.size = GUINT32_FROM_LE (temp_note->size);
399 note.identifier = GUINT32_FROM_LE (temp_note->identifier);
401 gst_bytestream_flush (bs, sizeof (struct _gst_riff_note));
402 len -= sizeof (struct _gst_riff_note);
404 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, note.size - 4);
405 if (got_bytes != note.size - 4) {
409 gst_bytestream_flush (bs, ((note.size - 4) + 1) & ~1);
410 len -= (((note.size - 4) + 1) & ~1);
412 label_name = (char *) tempdata;
414 new_caps = gst_caps_new ("note",
415 "application/x-gst-metadata",
416 gst_props_new ("identifier", G_TYPE_INT (note.identifier),
417 "name", G_TYPE_STRING (label_name), NULL));
419 if (gst_props_get (props, "notes", &caps, NULL)) {
420 caps = g_list_append (caps, new_caps);
422 caps = g_list_append (NULL, new_caps);
424 entry = gst_props_entry_new ("notes", GST_PROPS_GLIST (caps));
425 gst_props_add_entry (props, entry);
431 g_print ("Unknown chunk: %" GST_FOURCC_FORMAT "\n",
432 GST_FOURCC_ARGS (chunk.id));
437 g_object_notify (G_OBJECT (wavparse), "metadata");
441 gst_wavparse_parse_cues (GstWavParse * wavparse, int len)
444 GstByteStream *bs = wavparse->bs;
445 struct _gst_riff_cue *temp_cue, cue;
446 struct _gst_riff_cuepoints *points;
450 GstPropsEntry *entry;
456 gst_bytestream_peek_bytes (bs, &tempdata,
457 sizeof (struct _gst_riff_cue));
458 temp_cue = (struct _gst_riff_cue *) tempdata;
460 /* fixup for our big endian friends */
461 cue.id = GUINT32_FROM_LE (temp_cue->id);
462 cue.size = GUINT32_FROM_LE (temp_cue->size);
463 cue.cuepoints = GUINT32_FROM_LE (temp_cue->cuepoints);
465 gst_bytestream_flush (bs, sizeof (struct _gst_riff_cue));
466 if (got_bytes != sizeof (struct _gst_riff_cue)) {
470 len -= sizeof (struct _gst_riff_cue);
472 /* -4 because cue.size contains the cuepoints size
473 and we've already flushed that out of the system */
474 required = cue.size - 4;
475 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, required);
476 gst_bytestream_flush (bs, ((required) + 1) & ~1);
477 if (got_bytes != required) {
481 len -= (((cue.size - 4) + 1) & ~1);
483 /* now we have an array of struct _gst_riff_cuepoints in tempdata */
484 points = (struct _gst_riff_cuepoints *) tempdata;
486 for (i = 0; i < cue.cuepoints; i++) {
489 caps = gst_caps_new ("cues",
490 "application/x-gst-metadata",
491 gst_props_new ("identifier", G_TYPE_INT (points[i].identifier),
492 "position", G_TYPE_INT (points[i].offset), NULL));
493 cues = g_list_append (cues, caps);
496 entry = gst_props_entry_new ("cues", GST_PROPS_GLIST (cues));
497 gst_props_add_entry (wavparse->metadata->properties, entry);
500 g_object_notify (G_OBJECT (wavparse), "metadata");
503 /* Read 'fmt ' header */
505 gst_wavparse_fmt (GstWavParse * wav)
507 gst_riff_strf_auds *header = NULL;
510 if (!gst_riff_read_strf_auds (wav, &header))
513 wav->format = header->format;
514 wav->rate = header->rate;
515 wav->channels = header->channels;
516 if (wav->channels == 0)
519 wav->blockalign = header->blockalign;
520 wav->width = (header->blockalign * 8) / header->channels;
521 wav->depth = header->size;
522 wav->bps = header->av_bps;
526 /* Note: gst_riff_create_audio_caps might need to fix values in
527 * the header header depending on the format, so call it first */
528 caps = gst_riff_create_audio_caps (header->format, NULL, header, NULL);
534 gst_wavparse_create_sourcepad (wav);
535 gst_pad_use_fixed_caps (wav->srcpad);
536 gst_pad_set_active (wav->srcpad, TRUE);
537 gst_pad_set_caps (wav->srcpad, caps);
538 gst_caps_free (caps);
539 gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
540 gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
542 GST_DEBUG ("frequency %d, channels %d", wav->rate, wav->channels);
549 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
550 ("No FMT tag found"));
555 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
556 ("Stream claims to contain zero channels - invalid data"));
562 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
563 ("Stream claims to bitrate of <= zero - invalid data"));
569 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
575 gst_wavparse_other (GstWavParse * wav)
579 if (!gst_riff_peek_head (wav, &tag, &length, NULL)) {
580 GST_WARNING_OBJECT (wav, "could not peek head");
583 GST_DEBUG_OBJECT (wav, "got tag (%08x) %4.4s, length %d", tag,
584 (gchar *) & tag, length);
587 case GST_RIFF_TAG_LIST:
588 if (!(tag = gst_riff_peek_list (wav))) {
589 GST_WARNING_OBJECT (wav, "could not peek list");
594 case GST_RIFF_LIST_INFO:
595 if (!gst_riff_read_list (wav, &tag) || !gst_riff_read_info (wav)) {
596 GST_WARNING_OBJECT (wav, "could not read list");
601 case GST_RIFF_LIST_adtl:
602 if (!gst_riff_read_skip (wav)) {
603 GST_WARNING_OBJECT (wav, "could not read skip");
609 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag,
611 if (!gst_riff_read_skip (wav)) {
612 GST_WARNING_OBJECT (wav, "could not read skip");
620 case GST_RIFF_TAG_data:
621 if (!gst_bytestream_flush (wav->bs, 8)) {
622 GST_WARNING_OBJECT (wav, "could not flush 8 bytes");
626 GST_DEBUG_OBJECT (wav, "switching to data mode");
627 wav->state = GST_WAVPARSE_DATA;
628 wav->datastart = gst_bytestream_tell (wav->bs);
632 /* length is 0, data probably stretches to the end
634 GST_DEBUG_OBJECT (wav, "length is 0 trying to find length");
635 /* get length of file */
636 file_length = gst_bytestream_length (wav->bs);
637 if (file_length == -1) {
638 GST_DEBUG_OBJECT (wav,
639 "could not get file length, assuming data to eof");
640 /* could not get length, assuming till eof */
641 length = G_MAXUINT32;
643 if (file_length > G_MAXUINT32) {
644 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
645 ", clipping to 32 bits", file_length);
646 /* could not get length, assuming till eof */
647 length = G_MAXUINT32;
649 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
650 ", datalength %u", file_length, length);
651 /* substract offset of datastart from length */
652 length = file_length - wav->datastart;
653 GST_DEBUG_OBJECT (wav, "datalength %u", length);
656 wav->datasize = (guint64) length;
657 GST_DEBUG_OBJECT (wav, "datasize = %ld", length)
660 case GST_RIFF_TAG_cue:
661 if (!gst_riff_read_skip (wav)) {
662 GST_WARNING_OBJECT (wav, "could not read skip");
668 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag, (gchar *) & tag);
669 if (!gst_riff_read_skip (wav))
680 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
684 if (!gst_riff_parse_file_header (element, buf, &doctype))
687 if (doctype != GST_RIFF_RIFF_WAVE)
695 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
696 ("File is not a WAVE file: %" GST_FOURCC_FORMAT,
697 GST_FOURCC_ARGS (doctype)));
703 gst_wavparse_stream_init (GstWavParse * wav)
706 GstBuffer *buf = NULL;
708 if ((res = gst_pad_pull_range (wav->sinkpad,
709 wav->offset, 12, &buf)) != GST_FLOW_OK)
711 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
712 return GST_FLOW_ERROR;
720 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
722 /* -1 always maps to -1 */
728 /* 0 always maps to 0 */
735 *bytepos = uint64_ceiling_scale (ts, (guint64) wav->bps, GST_SECOND);
737 } else if (wav->fact) {
739 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
740 *bytepos = uint64_ceiling_scale (ts, bps, GST_SECOND);
747 /* This function is used to perform seeks on the element.
749 * It also works when event is NULL, in which case it will just
750 * start from the last configured segment. This technique is
751 * used when activating the element and to perform the seek in
755 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
759 GstFormat format, bformat;
761 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
762 gint64 cur, stop, upstream_size;
765 GstSegment seeksegment = { 0, };
769 GST_DEBUG_OBJECT (wav, "doing seek with event");
771 gst_event_parse_seek (event, &rate, &format, &flags,
772 &cur_type, &cur, &stop_type, &stop);
774 /* no negative rates yet */
778 if (format != wav->segment.format) {
779 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
780 gst_format_get_name (format),
781 gst_format_get_name (wav->segment.format));
783 if (cur_type != GST_SEEK_TYPE_NONE)
785 gst_pad_query_convert (wav->srcpad, format, cur,
786 wav->segment.format, &cur);
787 if (res && stop_type != GST_SEEK_TYPE_NONE)
789 gst_pad_query_convert (wav->srcpad, format, stop,
790 wav->segment.format, &stop);
794 format = wav->segment.format;
797 GST_DEBUG_OBJECT (wav, "doing seek without event");
800 cur_type = GST_SEEK_TYPE_SET;
801 stop_type = GST_SEEK_TYPE_SET;
804 /* in push mode, we must delegate to upstream */
805 if (wav->streaming) {
806 gboolean res = FALSE;
808 /* if streaming not yet started; only prepare initial newsegment */
809 if (!event || wav->state != GST_WAVPARSE_DATA) {
810 if (wav->start_segment)
811 gst_event_unref (wav->start_segment);
813 /* wav->start_segment =
814 gst_event_new_new_segment (FALSE, wav->segment.rate,
815 wav->segment.format, wav->segment.last_stop, wav->segment.duration,
816 wav->segment.last_stop);*/
819 /* convert seek positions to byte positions in data sections */
820 if (format == GST_FORMAT_TIME) {
821 /* should not fail */
822 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
824 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
827 /* mind sample boundary and header */
829 cur -= (cur % wav->bytes_per_sample);
830 cur += wav->datastart;
833 stop -= (stop % wav->bytes_per_sample);
834 stop += wav->datastart;
836 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
837 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
839 /* BYTE seek event */
840 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
842 res = gst_pad_push_event (wav->sinkpad, event);
848 flush = flags & GST_SEEK_FLAG_FLUSH;
850 /* now we need to make sure the streaming thread is stopped. We do this by
851 * either sending a FLUSH_START event downstream which will cause the
852 * streaming thread to stop with a WRONG_STATE.
853 * For a non-flushing seek we simply pause the task, which will happen as soon
854 * as it completes one iteration (and thus might block when the sink is
855 * blocking in preroll). */
858 GST_DEBUG_OBJECT (wav, "sending flush start");
859 gst_pad_push_event (wav->srcpad, gst_event_new_flush_start ());
862 gst_pad_pause_task (wav->sinkpad);
865 /* we should now be able to grab the streaming thread because we stopped it
866 * with the above flush/pause code */
867 GST_PAD_STREAM_LOCK (wav->sinkpad);
869 /* save current position */
870 last_stop = wav->segment.position;
872 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
874 /* copy segment, we need this because we still need the old
875 * segment when we close the current segment. */
876 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
878 /* configure the seek parameters in the seeksegment. We will then have the
879 * right values in the segment to perform the seek */
881 GST_DEBUG_OBJECT (wav, "configuring seek");
882 gst_segment_do_seek (&seeksegment, rate, format, flags,
883 cur_type, cur, stop_type, stop, &update);
886 /* figure out the last position we need to play. If it's configured (stop !=
887 * -1), use that, else we play until the total duration of the file */
888 if ((stop = seeksegment.stop) == -1)
889 stop = seeksegment.duration;
891 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
892 if ((cur_type != GST_SEEK_TYPE_NONE)) {
893 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
894 * we can just copy the last_stop. If not, we use the bps to convert TIME to
896 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.position,
897 (gint64 *) & wav->offset))
898 wav->offset = seeksegment.position;
899 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
900 wav->offset -= (wav->offset % wav->bytes_per_sample);
901 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
902 wav->offset += wav->datastart;
903 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
905 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
909 if (stop_type != GST_SEEK_TYPE_NONE) {
910 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
911 wav->end_offset = stop;
912 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
913 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
914 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
915 wav->end_offset += wav->datastart;
916 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
918 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
922 /* make sure filesize is not exceeded due to rounding errors or so,
923 * same precaution as in _stream_headers */
924 bformat = GST_FORMAT_BYTES;
925 if (gst_pad_peer_query_duration (wav->sinkpad, bformat, &upstream_size))
926 wav->end_offset = MIN (wav->end_offset, upstream_size);
928 /* this is the range of bytes we will use for playback */
929 wav->offset = MIN (wav->offset, wav->end_offset);
930 wav->dataleft = wav->end_offset - wav->offset;
932 GST_DEBUG_OBJECT (wav,
933 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
934 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
935 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
937 /* prepare for streaming again */
940 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
941 GST_DEBUG_OBJECT (wav, "sending flush stop");
942 gst_pad_push_event (wav->srcpad, gst_event_new_flush_stop (TRUE));
946 /* now we did the seek and can activate the new segment values */
947 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
949 /* if we're doing a segment seek, post a SEGMENT_START message */
950 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
951 gst_element_post_message (GST_ELEMENT_CAST (wav),
952 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
953 wav->segment.format, wav->segment.position));
956 /* now create the newsegment */
957 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
958 " to %" G_GINT64_FORMAT, wav->segment.position, stop);
960 /* store the newsegment event so it can be sent from the streaming thread. */
961 if (wav->start_segment)
962 gst_event_unref (wav->start_segment);
963 wav->start_segment = gst_event_new_segment (&wav->segment);
965 /* mark discont if we are going to stream from another position. */
966 if (last_stop != wav->segment.position) {
967 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
971 /* and start the streaming task again */
972 if (!wav->streaming) {
973 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
977 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
984 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
989 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
994 GST_DEBUG_OBJECT (wav,
995 "Could not determine byte position for desired time");
1001 * gst_wavparse_peek_chunk_info:
1002 * @wav Wavparse object
1003 * @tag holder for tag
1004 * @size holder for tag size
1006 * Peek next chunk info (tag and size)
1008 * Returns: %TRUE when the chunk info (header) is available
1011 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
1013 const guint8 *data = NULL;
1015 if (gst_adapter_available (wav->adapter) < 8)
1018 data = gst_adapter_map (wav->adapter, 8);
1019 *tag = GST_READ_UINT32_LE (data);
1020 *size = GST_READ_UINT32_LE (data + 4);
1021 gst_adapter_unmap (wav->adapter);
1023 GST_DEBUG ("Next chunk size is %d bytes, type %" GST_FOURCC_FORMAT, *size,
1024 GST_FOURCC_ARGS (*tag));
1030 * gst_wavparse_peek_chunk:
1031 * @wav Wavparse object
1032 * @tag holder for tag
1033 * @size holder for tag size
1035 * Peek enough data for one full chunk
1037 * Returns: %TRUE when the full chunk is available
1040 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
1042 guint32 peek_size = 0;
1045 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
1048 /* size 0 -> empty data buffer would surprise most callers,
1049 * large size -> do not bother trying to squeeze that into adapter,
1050 * so we throw poor man's exception, which can be caught if caller really
1051 * wants to handle 0 size chunk */
1052 if (!(*size) || (*size) >= (1 << 30)) {
1053 GST_INFO ("Invalid/unexpected chunk size %d for tag %" GST_FOURCC_FORMAT,
1054 *size, GST_FOURCC_ARGS (*tag));
1055 /* chain should give up */
1056 wav->abort_buffering = TRUE;
1059 peek_size = (*size + 1) & ~1;
1060 available = gst_adapter_available (wav->adapter);
1062 if (available >= (8 + peek_size)) {
1065 GST_LOG ("but only %u bytes available now", available);
1071 * gst_wavparse_calculate_duration:
1072 * @wav: wavparse object
1074 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
1077 * Returns: %TRUE if duration is available.
1080 gst_wavparse_calculate_duration (GstWavParse * wav)
1082 if (wav->duration > 0)
1086 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
1088 uint64_ceiling_scale (wav->datasize, GST_SECOND, (guint64) wav->bps);
1089 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
1090 GST_TIME_ARGS (wav->duration));
1092 } else if (wav->fact) {
1093 wav->duration = uint64_ceiling_scale_int (GST_SECOND, wav->fact, wav->rate);
1094 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
1095 GST_TIME_ARGS (wav->duration));
1102 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
1107 if (wav->streaming) {
1108 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1111 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
1112 GST_FOURCC_ARGS (tag));
1113 flush = 8 + ((size + 1) & ~1);
1114 wav->offset += flush;
1115 if (wav->streaming) {
1116 gst_adapter_flush (wav->adapter, flush);
1118 gst_buffer_unref (buf);
1124 #define MAX_BUFFER_SIZE 4096
1126 static GstFlowReturn
1127 gst_wavparse_stream_headers (GstWavParse * wav)
1129 GstFlowReturn res = GST_FLOW_OK;
1130 GstBuffer *buf = NULL;
1131 gst_riff_strf_auds *header = NULL;
1133 gboolean gotdata = FALSE;
1134 GstCaps *caps = NULL;
1135 gchar *codec_name = NULL;
1137 gint64 upstream_size = 0;
1139 /* search for "_fmt" chunk, which should be first */
1140 while (!wav->got_fmt) {
1143 /* The header starts with a 'fmt ' tag */
1144 if (wav->streaming) {
1145 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1148 gst_adapter_flush (wav->adapter, 8);
1152 buf = gst_adapter_take_buffer (wav->adapter, size);
1154 gst_adapter_flush (wav->adapter, 1);
1155 wav->offset += GST_ROUND_UP_2 (size);
1157 buf = gst_buffer_new ();
1160 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1161 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1165 if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ ||
1166 tag == GST_RIFF_TAG_bext || tag == GST_RIFF_TAG_BEXT ||
1167 tag == GST_RIFF_TAG_LIST) {
1168 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1169 GST_FOURCC_ARGS (tag));
1170 gst_buffer_unref (buf);
1175 if (tag != GST_RIFF_TAG_fmt)
1178 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1180 goto parse_header_error;
1182 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1184 /* do sanity checks of header fields */
1185 if (header->channels == 0)
1187 if (header->rate == 0)
1190 GST_DEBUG_OBJECT (wav, "creating the caps");
1192 /* Note: gst_riff_create_audio_caps might need to fix values in
1193 * the header header depending on the format, so call it first */
1194 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1198 gst_buffer_unref (extra);
1201 goto unknown_format;
1203 /* do more sanity checks of header fields
1204 * (these can be sanitized by gst_riff_create_audio_caps()
1206 wav->format = header->format;
1207 wav->rate = header->rate;
1208 wav->channels = header->channels;
1209 wav->blockalign = header->blockalign;
1210 wav->depth = header->size;
1211 wav->av_bps = header->av_bps;
1217 /* do format specific handling */
1218 switch (wav->format) {
1219 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1220 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1222 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1223 * bitrate inside the mpeg stream */
1224 GST_INFO ("resetting bps from %d to 0 for mp2/3", wav->av_bps);
1228 case GST_RIFF_WAVE_FORMAT_PCM:
1229 if (wav->blockalign > wav->channels * (guint) ceil (wav->depth / 8.0))
1230 goto invalid_blockalign;
1233 if (wav->av_bps > wav->blockalign * wav->rate)
1235 /* use the configured bps */
1236 wav->bps = wav->av_bps;
1240 wav->width = (wav->blockalign * 8) / wav->channels;
1241 wav->bytes_per_sample = wav->channels * wav->width / 8;
1243 if (wav->bytes_per_sample <= 0)
1244 goto no_bytes_per_sample;
1246 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1247 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1248 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1249 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1250 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1251 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1252 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1254 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1255 * formats). This will make the element output a BYTE format segment and
1256 * will not timestamp the outgoing buffers.
1258 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1260 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1262 /* create pad later so we can sniff the first few bytes
1263 * of the real data and correct our caps if necessary */
1264 gst_caps_replace (&wav->caps, caps);
1265 gst_caps_replace (&caps, NULL);
1267 wav->got_fmt = TRUE;
1270 wav->tags = gst_tag_list_new_empty ();
1272 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1273 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1275 g_free (codec_name);
1281 gst_pad_peer_query_duration (wav->sinkpad, GST_FORMAT_BYTES, &upstream_size);
1282 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1284 /* loop headers until we get data */
1286 if (wav->streaming) {
1287 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1293 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1294 &buf)) != GST_FLOW_OK)
1295 goto header_read_error;
1296 data = gst_buffer_map (buf, NULL, NULL, -1);
1297 tag = GST_READ_UINT32_LE (data);
1298 size = GST_READ_UINT32_LE (data + 4);
1299 gst_buffer_unmap (buf, data, -1);
1302 GST_INFO_OBJECT (wav,
1303 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT,
1304 GST_FOURCC_ARGS (tag), wav->offset);
1306 /* wav is a st00pid format, we don't know for sure where data starts.
1307 * So we have to go bit by bit until we find the 'data' header
1310 case GST_RIFF_TAG_data:{
1311 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %d", size);
1312 if (wav->streaming) {
1313 gst_adapter_flush (wav->adapter, 8);
1316 gst_buffer_unref (buf);
1319 wav->datastart = wav->offset;
1320 /* If size is zero, then the data chunk probably actually extends to
1321 the end of the file */
1322 if (size == 0 && upstream_size) {
1323 size = upstream_size - wav->datastart;
1325 /* Or the file might be truncated */
1326 else if (upstream_size) {
1327 size = MIN (size, (upstream_size - wav->datastart));
1329 wav->datasize = (guint64) size;
1330 wav->dataleft = (guint64) size;
1331 wav->end_offset = size + wav->datastart;
1332 if (!wav->streaming) {
1333 /* We will continue parsing tags 'till end */
1334 wav->offset += size;
1336 GST_DEBUG_OBJECT (wav, "datasize = %d", size);
1339 case GST_RIFF_TAG_fact:{
1340 if (wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1341 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1342 const guint data_size = 4;
1344 GST_INFO_OBJECT (wav, "Have fact chunk");
1345 if (size < data_size) {
1346 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1347 /* need more data */
1350 GST_DEBUG_OBJECT (wav, "need %d, available %d; ignoring chunk",
1354 /* number of samples (for compressed formats) */
1355 if (wav->streaming) {
1356 const guint8 *data = NULL;
1358 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1361 gst_adapter_flush (wav->adapter, 8);
1362 data = gst_adapter_map (wav->adapter, data_size);
1363 wav->fact = GST_READ_UINT32_LE (data);
1364 gst_adapter_unmap (wav->adapter);
1365 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1367 gst_buffer_unref (buf);
1369 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1370 data_size, &buf)) != GST_FLOW_OK)
1371 goto header_read_error;
1372 gst_buffer_extract (buf, 0, &wav->fact, 4);
1373 wav->fact = GUINT32_FROM_LE (wav->fact);
1374 gst_buffer_unref (buf);
1376 GST_DEBUG_OBJECT (wav, "have fact %u", wav->fact);
1377 wav->offset += 8 + GST_ROUND_UP_2 (size);
1380 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1381 /* need more data */
1387 case GST_RIFF_TAG_acid:{
1388 const gst_riff_acid *acid = NULL;
1389 const guint data_size = sizeof (gst_riff_acid);
1392 GST_INFO_OBJECT (wav, "Have acid chunk");
1393 if (size < data_size) {
1394 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1395 /* need more data */
1398 GST_DEBUG_OBJECT (wav, "need %d, available %d; ignoring chunk",
1402 if (wav->streaming) {
1403 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1406 gst_adapter_flush (wav->adapter, 8);
1407 acid = (const gst_riff_acid *) gst_adapter_map (wav->adapter,
1409 tempo = acid->tempo;
1410 gst_adapter_unmap (wav->adapter);
1412 gst_buffer_unref (buf);
1414 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1415 size, &buf)) != GST_FLOW_OK)
1416 goto header_read_error;
1417 acid = (const gst_riff_acid *) gst_buffer_map (buf, NULL, NULL,
1419 tempo = acid->tempo;
1420 gst_buffer_unmap (buf, (guint8 *) acid, -1);
1422 /* send data as tags */
1424 wav->tags = gst_tag_list_new_empty ();
1425 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1426 GST_TAG_BEATS_PER_MINUTE, tempo, NULL);
1428 size = GST_ROUND_UP_2 (size);
1429 if (wav->streaming) {
1430 gst_adapter_flush (wav->adapter, size);
1432 gst_buffer_unref (buf);
1434 wav->offset += 8 + size;
1437 /* FIXME: all list tags after data are ignored in streaming mode */
1438 case GST_RIFF_TAG_LIST:{
1441 if (wav->streaming) {
1442 const guint8 *data = NULL;
1444 if (gst_adapter_available (wav->adapter) < 12) {
1447 data = gst_adapter_map (wav->adapter, 12);
1448 ltag = GST_READ_UINT32_LE (data + 8);
1449 gst_adapter_unmap (wav->adapter);
1451 gst_buffer_unref (buf);
1453 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1454 &buf)) != GST_FLOW_OK)
1455 goto header_read_error;
1456 gst_buffer_extract (buf, 8, <ag, 4);
1457 ltag = GUINT32_FROM_LE (ltag);
1460 case GST_RIFF_LIST_INFO:{
1461 const gint data_size = size - 4;
1464 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1465 if (wav->streaming) {
1466 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1469 gst_adapter_flush (wav->adapter, 12);
1471 if (data_size > 0) {
1472 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1474 gst_adapter_flush (wav->adapter, 1);
1478 gst_buffer_unref (buf);
1479 if (data_size > 0) {
1481 gst_pad_pull_range (wav->sinkpad, wav->offset,
1482 data_size, &buf)) != GST_FLOW_OK)
1483 goto header_read_error;
1486 if (data_size > 0) {
1488 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1490 GstTagList *old = wav->tags;
1492 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1494 gst_tag_list_free (old);
1495 gst_tag_list_free (new);
1497 gst_buffer_unref (buf);
1498 wav->offset += GST_ROUND_UP_2 (data_size);
1503 GST_INFO_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1504 GST_FOURCC_ARGS (ltag));
1505 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1506 /* need more data */
1513 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1514 /* need more data */
1519 if (upstream_size && (wav->offset >= upstream_size)) {
1520 /* Now we are gone through the whole file */
1525 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1527 if (wav->bps <= 0 && wav->fact) {
1529 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1531 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1532 (guint64) wav->fact);
1533 GST_INFO_OBJECT (wav, "calculated bps : %d, enabling VBR", wav->bps);
1538 if (gst_wavparse_calculate_duration (wav)) {
1539 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1540 wav->segment.duration = wav->duration;
1542 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1543 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1544 wav->segment.duration = wav->datasize;
1547 /* now we have all the info to perform a pending seek if any, if no
1548 * event, this will still do the right thing and it will also send
1549 * the right newsegment event downstream. */
1550 gst_wavparse_perform_seek (wav, wav->seek_event);
1551 /* remove pending event */
1552 event_p = &wav->seek_event;
1553 gst_event_replace (event_p, NULL);
1555 /* we just started, we are discont */
1556 wav->discont = TRUE;
1558 wav->state = GST_WAVPARSE_DATA;
1560 /* determine reasonable max buffer size,
1561 * that is, buffers not too small either size or time wise
1562 * so we do not end up with too many of them */
1565 gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size);
1566 wav->max_buf_size = upstream_size;
1567 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1568 if (wav->blockalign > 0)
1569 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1571 GST_DEBUG_OBJECT (wav, "max buffer size %d", wav->max_buf_size);
1579 g_free (codec_name);
1583 gst_caps_unref (caps);
1588 res = GST_FLOW_ERROR;
1593 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1594 ("Invalid WAV header (no fmt at start): %"
1595 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1600 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1601 ("Couldn't parse audio header"));
1606 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1607 ("Stream claims to contain no channels - invalid data"));
1612 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1613 ("Stream with sample_rate == 0 - invalid data"));
1618 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1619 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1620 wav->blockalign, wav->channels * (guint) ceil (wav->depth / 8.0)));
1625 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1626 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1627 wav->av_bps, wav->blockalign * wav->rate));
1630 no_bytes_per_sample:
1632 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1633 ("Could not caluclate bytes per sample - invalid data"));
1638 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1639 ("No caps found for format 0x%x, %d channels, %d Hz",
1640 wav->format, wav->channels, wav->rate));
1645 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1646 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1652 * Read WAV file tag when streaming
1654 static GstFlowReturn
1655 gst_wavparse_parse_stream_init (GstWavParse * wav)
1657 if (gst_adapter_available (wav->adapter) >= 12) {
1660 /* _take flushes the data */
1661 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1663 GST_DEBUG ("Parsing wav header");
1664 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1665 return GST_FLOW_ERROR;
1668 /* Go to next state */
1669 wav->state = GST_WAVPARSE_HEADER;
1674 /* handle an event sent directly to the element.
1676 * This event can be sent either in the READY state or the
1677 * >READY state. The only event of interest really is the seek
1680 * In the READY state we can only store the event and try to
1681 * respect it when going to PAUSED. We assume we are in the
1682 * READY state when our parsing state != GST_WAVPARSE_DATA.
1684 * When we are steaming, we can simply perform the seek right
1688 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1690 GstWavParse *wav = GST_WAVPARSE (element);
1691 gboolean res = FALSE;
1694 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1696 switch (GST_EVENT_TYPE (event)) {
1697 case GST_EVENT_SEEK:
1698 if (wav->state == GST_WAVPARSE_DATA) {
1699 /* we can handle the seek directly when streaming data */
1700 res = gst_wavparse_perform_seek (wav, event);
1702 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1704 event_p = &wav->seek_event;
1705 gst_event_replace (event_p, event);
1707 /* we always return true */
1714 gst_event_unref (event);
1719 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1723 s = gst_caps_get_structure (caps, 0);
1724 if (!gst_structure_has_name (s, "audio/x-dts"))
1726 if (prob >= GST_TYPE_FIND_LIKELY)
1728 /* DTS at non-0 offsets and without second sync may yield POSSIBLE .. */
1729 if (prob < GST_TYPE_FIND_POSSIBLE)
1731 /* .. in which case we want at least a valid-looking rate and channels */
1732 if (!gst_structure_has_field (s, "channels"))
1734 /* and for extra assurance we could also check the rate from the DTS frame
1735 * against the one in the wav header, but for now let's not do that */
1736 return gst_structure_has_field (s, "rate");
1740 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1744 GST_DEBUG_OBJECT (wav, "adding src pad");
1747 s = gst_caps_get_structure (wav->caps, 0);
1748 if (s && gst_structure_has_name (s, "audio/x-raw") && buf != NULL) {
1749 GstTypeFindProbability prob;
1752 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1753 if (tf_caps != NULL) {
1754 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1755 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1756 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1757 gst_caps_unref (wav->caps);
1758 wav->caps = tf_caps;
1760 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1761 GST_TAG_AUDIO_CODEC, "dts", NULL);
1763 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1764 "marked as raw PCM audio, but ignoring for now", tf_caps);
1765 gst_caps_unref (tf_caps);
1771 gst_pad_set_caps (wav->srcpad, wav->caps);
1772 gst_caps_replace (&wav->caps, NULL);
1774 if (wav->start_segment) {
1775 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1776 gst_pad_push_event (wav->srcpad, wav->start_segment);
1777 wav->start_segment = NULL;
1781 gst_pad_push_event (wav->srcpad, gst_event_new_tag (wav->tags));
1786 static GstFlowReturn
1787 gst_wavparse_stream_data (GstWavParse * wav)
1789 GstBuffer *buf = NULL;
1790 GstFlowReturn res = GST_FLOW_OK;
1791 guint64 desired, obtained;
1792 GstClockTime timestamp, next_timestamp, duration;
1793 guint64 pos, nextpos;
1796 GST_LOG_OBJECT (wav,
1797 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1798 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1800 /* Get the next n bytes and output them */
1801 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1804 /* scale the amount of data by the segment rate so we get equal
1805 * amounts of data regardless of the playback rate */
1807 MIN (gst_guint64_to_gdouble (wav->dataleft),
1808 wav->max_buf_size * ABS (wav->segment.rate));
1810 if (desired >= wav->blockalign && wav->blockalign > 0)
1811 desired -= (desired % wav->blockalign);
1813 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1814 "from the sinkpad", desired);
1816 if (wav->streaming) {
1817 guint avail = gst_adapter_available (wav->adapter);
1820 /* flush some bytes if evil upstream sends segment that starts
1821 * before data or does is not send sample aligned segment */
1822 if (G_LIKELY (wav->offset >= wav->datastart)) {
1823 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1825 extra = wav->datastart - wav->offset;
1828 if (G_UNLIKELY (extra)) {
1829 extra = wav->bytes_per_sample - extra;
1830 if (extra <= avail) {
1831 GST_DEBUG_OBJECT (wav, "flushing %d bytes to sample boundary", extra);
1832 gst_adapter_flush (wav->adapter, extra);
1833 wav->offset += extra;
1834 wav->dataleft -= extra;
1835 goto iterate_adapter;
1837 GST_DEBUG_OBJECT (wav, "flushing %d bytes", avail);
1838 gst_adapter_clear (wav->adapter);
1839 wav->offset += avail;
1840 wav->dataleft -= avail;
1845 if (avail < desired) {
1846 GST_LOG_OBJECT (wav, "Got only %d bytes of data from the sinkpad", avail);
1850 buf = gst_adapter_take_buffer (wav->adapter, desired);
1852 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
1853 desired, &buf)) != GST_FLOW_OK)
1856 /* we may get a short buffer at the end of the file */
1857 if (gst_buffer_get_size (buf) < desired) {
1858 gsize size = gst_buffer_get_size (buf);
1860 GST_LOG_OBJECT (wav, "Got only %" G_GSIZE_FORMAT " bytes of data", size);
1861 if (size >= wav->blockalign) {
1862 buf = gst_buffer_make_writable (buf);
1863 gst_buffer_resize (buf, 0, size - (size % wav->blockalign));
1865 gst_buffer_unref (buf);
1871 obtained = gst_buffer_get_size (buf);
1873 /* our positions in bytes */
1874 pos = wav->offset - wav->datastart;
1875 nextpos = pos + obtained;
1877 /* update offsets, does not overflow. */
1878 buf = gst_buffer_make_writable (buf);
1879 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
1880 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
1882 /* first chunk of data? create the source pad. We do this only here so
1883 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
1884 if (G_UNLIKELY (wav->first)) {
1886 /* this will also push the segment events */
1887 gst_wavparse_add_src_pad (wav, buf);
1889 /* If we have a pending start segment, send it now. */
1890 if (G_UNLIKELY (wav->start_segment != NULL)) {
1891 gst_pad_push_event (wav->srcpad, wav->start_segment);
1892 wav->start_segment = NULL;
1897 /* and timestamps if we have a bitrate, be careful for overflows */
1898 timestamp = uint64_ceiling_scale (pos, GST_SECOND, (guint64) wav->bps);
1900 uint64_ceiling_scale (nextpos, GST_SECOND, (guint64) wav->bps);
1901 duration = next_timestamp - timestamp;
1903 /* update current running segment position */
1904 if (G_LIKELY (next_timestamp >= wav->segment.start))
1905 wav->segment.position = next_timestamp;
1906 } else if (wav->fact) {
1908 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
1909 /* and timestamps if we have a bitrate, be careful for overflows */
1910 timestamp = uint64_ceiling_scale (pos, GST_SECOND, bps);
1911 next_timestamp = uint64_ceiling_scale (nextpos, GST_SECOND, bps);
1912 duration = next_timestamp - timestamp;
1914 /* no bitrate, all we know is that the first sample has timestamp 0, all
1915 * other positions and durations have unknown timestamp. */
1919 timestamp = GST_CLOCK_TIME_NONE;
1920 duration = GST_CLOCK_TIME_NONE;
1921 /* update current running segment position with byte offset */
1922 if (G_LIKELY (nextpos >= wav->segment.start))
1923 wav->segment.position = nextpos;
1925 if ((pos > 0) && wav->vbr) {
1926 /* don't set timestamps for VBR files if it's not the first buffer */
1927 timestamp = GST_CLOCK_TIME_NONE;
1928 duration = GST_CLOCK_TIME_NONE;
1931 GST_DEBUG_OBJECT (wav, "marking DISCONT");
1932 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
1933 wav->discont = FALSE;
1936 GST_BUFFER_TIMESTAMP (buf) = timestamp;
1937 GST_BUFFER_DURATION (buf) = duration;
1939 GST_LOG_OBJECT (wav,
1940 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
1941 ", size:%u", GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration),
1942 gst_buffer_get_size (buf));
1944 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
1947 if (obtained < wav->dataleft) {
1948 wav->offset += obtained;
1949 wav->dataleft -= obtained;
1951 wav->offset += wav->dataleft;
1955 /* Iterate until need more data, so adapter size won't grow */
1956 if (wav->streaming) {
1957 GST_LOG_OBJECT (wav,
1958 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
1960 goto iterate_adapter;
1967 GST_DEBUG_OBJECT (wav, "found EOS");
1968 return GST_FLOW_UNEXPECTED;
1972 /* check if we got EOS */
1973 if (res == GST_FLOW_UNEXPECTED)
1976 GST_WARNING_OBJECT (wav,
1977 "Error getting %" G_GINT64_FORMAT " bytes from the "
1978 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
1983 GST_INFO_OBJECT (wav,
1984 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
1985 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
1986 gst_pad_is_linked (wav->srcpad));
1992 gst_wavparse_loop (GstPad * pad)
1995 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
1997 GST_LOG_OBJECT (wav, "process data");
1999 switch (wav->state) {
2000 case GST_WAVPARSE_START:
2001 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2002 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2005 wav->state = GST_WAVPARSE_HEADER;
2008 case GST_WAVPARSE_HEADER:
2009 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2010 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2013 wav->state = GST_WAVPARSE_DATA;
2014 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2017 case GST_WAVPARSE_DATA:
2018 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2022 g_assert_not_reached ();
2029 const gchar *reason = gst_flow_get_name (ret);
2031 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2032 gst_pad_pause_task (pad);
2034 if (ret == GST_FLOW_UNEXPECTED) {
2035 /* handle end-of-stream/segment */
2036 /* so align our position with the end of it, if there is one
2037 * this ensures a subsequent will arrive at correct base/acc time */
2038 if (wav->segment.format == GST_FORMAT_TIME) {
2039 if (wav->segment.rate > 0.0 &&
2040 GST_CLOCK_TIME_IS_VALID (wav->segment.stop))
2041 wav->segment.position = wav->segment.stop;
2042 else if (wav->segment.rate < 0.0)
2043 wav->segment.position = wav->segment.start;
2045 /* add pad before we perform EOS */
2046 if (G_UNLIKELY (wav->first)) {
2048 gst_wavparse_add_src_pad (wav, NULL);
2051 if (wav->state == GST_WAVPARSE_START)
2052 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2053 ("No valid input found before end of stream"), (NULL));
2055 /* perform EOS logic */
2056 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2059 if ((stop = wav->segment.stop) == -1)
2060 stop = wav->segment.duration;
2062 gst_element_post_message (GST_ELEMENT_CAST (wav),
2063 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2064 wav->segment.format, stop));
2066 if (wav->srcpad != NULL)
2067 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2069 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2070 /* for fatal errors we post an error message, post the error
2071 * first so the app knows about the error first. */
2072 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
2073 (_("Internal data flow error.")),
2074 ("streaming task paused, reason %s (%d)", reason, ret));
2075 if (wav->srcpad != NULL)
2076 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2082 static GstFlowReturn
2083 gst_wavparse_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
2086 GstWavParse *wav = GST_WAVPARSE (parent);
2088 GST_LOG_OBJECT (wav, "adapter_push %u bytes", gst_buffer_get_size (buf));
2090 gst_adapter_push (wav->adapter, buf);
2092 switch (wav->state) {
2093 case GST_WAVPARSE_START:
2094 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2095 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2098 if (wav->state != GST_WAVPARSE_HEADER)
2101 /* otherwise fall-through */
2102 case GST_WAVPARSE_HEADER:
2103 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2104 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2107 if (!wav->got_fmt || wav->datastart == 0)
2110 wav->state = GST_WAVPARSE_DATA;
2111 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2114 case GST_WAVPARSE_DATA:
2115 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2116 wav->discont = TRUE;
2117 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2121 g_return_val_if_reached (GST_FLOW_ERROR);
2124 if (G_UNLIKELY (wav->abort_buffering)) {
2125 wav->abort_buffering = FALSE;
2126 ret = GST_FLOW_ERROR;
2127 /* sort of demux/parse error */
2128 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2134 static GstFlowReturn
2135 gst_wavparse_flush_data (GstWavParse * wav)
2137 GstFlowReturn ret = GST_FLOW_OK;
2140 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2142 wav->end_offset = wav->offset + av;
2143 ret = gst_wavparse_stream_data (wav);
2150 gst_wavparse_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
2152 GstWavParse *wav = GST_WAVPARSE (parent);
2153 gboolean ret = TRUE;
2155 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2157 switch (GST_EVENT_TYPE (event)) {
2158 case GST_EVENT_CAPS:
2160 /* discard, we'll come up with proper src caps */
2161 gst_event_unref (event);
2164 case GST_EVENT_SEGMENT:
2166 gint64 start, stop, offset = 0, end_offset = -1;
2169 /* some debug output */
2170 gst_event_copy_segment (event, &segment);
2171 GST_DEBUG_OBJECT (wav, "received newsegment %" GST_SEGMENT_FORMAT,
2174 if (wav->state != GST_WAVPARSE_DATA) {
2175 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2179 /* now we are either committed to TIME or BYTE format,
2180 * and we only expect a BYTE segment, e.g. following a seek */
2181 if (segment.format == GST_FORMAT_BYTES) {
2182 /* handle (un)signed issues */
2183 start = segment.start;
2184 stop = segment.stop;
2187 start -= wav->datastart;
2188 start = MAX (start, 0);
2192 segment.stop -= wav->datastart;
2193 segment.stop = MAX (stop, 0);
2195 if (wav->segment.format == GST_FORMAT_TIME) {
2196 guint64 bps = wav->bps;
2198 /* operating in format TIME, so we can convert */
2199 if (!bps && wav->fact)
2201 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2205 uint64_ceiling_scale (start, GST_SECOND, (guint64) wav->bps);
2208 uint64_ceiling_scale (stop, GST_SECOND, (guint64) wav->bps);
2212 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2216 segment.start = start;
2217 segment.stop = stop;
2219 /* accept upstream's notion of segment and distribute along */
2220 segment.time = segment.start = segment.position;
2221 segment.duration = wav->segment.duration;
2222 segment.base = gst_segment_to_running_time (&wav->segment,
2223 GST_FORMAT_TIME, wav->segment.position);
2225 gst_segment_copy_into (&segment, &wav->segment);
2227 /* also store the newsegment event for the streaming thread */
2228 if (wav->start_segment)
2229 gst_event_unref (wav->start_segment);
2230 GST_DEBUG_OBJECT (wav, "Storing newseg %" GST_SEGMENT_FORMAT, &segment);
2231 wav->start_segment = gst_event_new_segment (&segment);
2233 /* stream leftover data in current segment */
2234 gst_wavparse_flush_data (wav);
2235 /* and set up streaming thread for next one */
2236 wav->offset = offset;
2237 wav->end_offset = end_offset;
2238 if (wav->end_offset > 0) {
2239 wav->dataleft = wav->end_offset - wav->offset;
2241 /* infinity; upstream will EOS when done */
2242 wav->dataleft = G_MAXUINT64;
2245 gst_event_unref (event);
2249 /* add pad if needed so EOS is seen downstream */
2250 if (G_UNLIKELY (wav->first)) {
2252 gst_wavparse_add_src_pad (wav, NULL);
2254 /* stream leftover data in current segment */
2255 gst_wavparse_flush_data (wav);
2258 if (wav->state == GST_WAVPARSE_START)
2259 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2260 ("No valid input found before end of stream"), (NULL));
2263 case GST_EVENT_FLUSH_STOP:
2267 gst_adapter_clear (wav->adapter);
2268 wav->discont = TRUE;
2269 dur = wav->segment.duration;
2270 gst_segment_init (&wav->segment, wav->segment.format);
2271 wav->segment.duration = dur;
2275 ret = gst_pad_event_default (wav->sinkpad, parent, event);
2283 /* convert and query stuff */
2284 static const GstFormat *
2285 gst_wavparse_get_formats (GstPad * pad)
2287 static GstFormat formats[] = {
2290 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2299 gst_wavparse_pad_convert (GstPad * pad,
2300 GstFormat src_format, gint64 src_value,
2301 GstFormat * dest_format, gint64 * dest_value)
2303 GstWavParse *wavparse;
2304 gboolean res = TRUE;
2306 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2308 if (*dest_format == src_format) {
2309 *dest_value = src_value;
2313 if ((wavparse->bps == 0) && !wavparse->fact)
2316 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2317 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2319 switch (src_format) {
2320 case GST_FORMAT_BYTES:
2321 switch (*dest_format) {
2322 case GST_FORMAT_DEFAULT:
2323 *dest_value = src_value / wavparse->bytes_per_sample;
2324 /* make sure we end up on a sample boundary */
2325 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2327 case GST_FORMAT_TIME:
2328 /* src_value + datastart = offset */
2329 GST_INFO_OBJECT (wavparse,
2330 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2332 if (wavparse->bps > 0)
2333 *dest_value = uint64_ceiling_scale (src_value, GST_SECOND,
2334 (guint64) wavparse->bps);
2335 else if (wavparse->fact) {
2336 guint64 bps = uint64_ceiling_scale_int (wavparse->datasize,
2337 wavparse->rate, wavparse->fact);
2339 *dest_value = uint64_ceiling_scale_int (src_value, GST_SECOND, bps);
2350 case GST_FORMAT_DEFAULT:
2351 switch (*dest_format) {
2352 case GST_FORMAT_BYTES:
2353 *dest_value = src_value * wavparse->bytes_per_sample;
2355 case GST_FORMAT_TIME:
2356 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2357 (guint64) wavparse->rate);
2365 case GST_FORMAT_TIME:
2366 switch (*dest_format) {
2367 case GST_FORMAT_BYTES:
2368 if (wavparse->bps > 0)
2369 *dest_value = gst_util_uint64_scale (src_value,
2370 (guint64) wavparse->bps, GST_SECOND);
2372 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2373 wavparse->rate, wavparse->fact);
2375 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2377 /* make sure we end up on a sample boundary */
2378 *dest_value -= *dest_value % wavparse->blockalign;
2380 case GST_FORMAT_DEFAULT:
2381 *dest_value = gst_util_uint64_scale (src_value,
2382 (guint64) wavparse->rate, GST_SECOND);
2401 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2407 /* handle queries for location and length in requested format */
2409 gst_wavparse_pad_query (GstPad * pad, GstObject * parent, GstQuery * query)
2411 gboolean res = TRUE;
2412 GstWavParse *wav = GST_WAVPARSE (parent);
2414 /* only if we know */
2415 if (wav->state != GST_WAVPARSE_DATA) {
2419 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2421 switch (GST_QUERY_TYPE (query)) {
2422 case GST_QUERY_POSITION:
2428 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2429 curb = wav->offset - wav->datastart;
2430 gst_query_parse_position (query, &format, NULL);
2431 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2434 case GST_FORMAT_TIME:
2435 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2439 format = GST_FORMAT_BYTES;
2444 gst_query_set_position (query, format, cur);
2447 case GST_QUERY_DURATION:
2449 gint64 duration = 0;
2452 gst_query_parse_duration (query, &format, NULL);
2455 case GST_FORMAT_TIME:{
2456 if ((res = gst_wavparse_calculate_duration (wav))) {
2457 duration = wav->duration;
2462 format = GST_FORMAT_BYTES;
2463 duration = wav->datasize;
2466 gst_query_set_duration (query, format, duration);
2469 case GST_QUERY_CONVERT:
2471 gint64 srcvalue, dstvalue;
2472 GstFormat srcformat, dstformat;
2474 gst_query_parse_convert (query, &srcformat, &srcvalue,
2475 &dstformat, &dstvalue);
2476 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2477 &dstformat, &dstvalue);
2479 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2482 case GST_QUERY_SEEKING:{
2484 gboolean seekable = FALSE;
2486 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2487 if (fmt == wav->segment.format) {
2488 if (wav->streaming) {
2491 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2492 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2493 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2494 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2496 gst_query_unref (q);
2498 GST_LOG_OBJECT (wav, "looping => seekable");
2502 } else if (fmt == GST_FORMAT_TIME) {
2506 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2511 res = gst_pad_query_default (pad, parent, query);
2518 gst_wavparse_srcpad_event (GstPad * pad, GstObject * parent, GstEvent * event)
2520 GstWavParse *wavparse = GST_WAVPARSE (parent);
2521 gboolean res = FALSE;
2523 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2525 switch (GST_EVENT_TYPE (event)) {
2526 case GST_EVENT_SEEK:
2527 /* can only handle events when we are in the data state */
2528 if (wavparse->state == GST_WAVPARSE_DATA) {
2529 res = gst_wavparse_perform_seek (wavparse, event);
2531 gst_event_unref (event);
2534 res = gst_pad_push_event (wavparse->sinkpad, event);
2541 gst_wavparse_sink_activate (GstPad * sinkpad, GstObject * parent)
2543 GstWavParse *wav = GST_WAVPARSE (parent);
2548 gst_adapter_clear (wav->adapter);
2549 g_object_unref (wav->adapter);
2550 wav->adapter = NULL;
2553 query = gst_query_new_scheduling ();
2555 if (!gst_pad_peer_query (sinkpad, query)) {
2556 gst_query_unref (query);
2560 gst_query_parse_scheduling (query, &pull_mode, NULL, NULL, NULL, NULL, NULL);
2561 gst_query_unref (query);
2566 GST_DEBUG_OBJECT (sinkpad, "activating pull");
2567 wav->streaming = FALSE;
2568 return gst_pad_activate_pull (sinkpad, TRUE);
2572 GST_DEBUG_OBJECT (sinkpad, "activating push");
2573 wav->streaming = TRUE;
2574 wav->adapter = gst_adapter_new ();
2575 return gst_pad_activate_push (sinkpad, TRUE);
2581 gst_wavparse_sink_activate_pull (GstPad * sinkpad, GstObject * parent,
2585 /* if we have a scheduler we can start the task */
2586 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2589 return gst_pad_stop_task (sinkpad);
2593 static GstStateChangeReturn
2594 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2596 GstStateChangeReturn ret;
2597 GstWavParse *wav = GST_WAVPARSE (element);
2599 switch (transition) {
2600 case GST_STATE_CHANGE_NULL_TO_READY:
2602 case GST_STATE_CHANGE_READY_TO_PAUSED:
2603 gst_wavparse_reset (wav);
2605 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2611 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2613 switch (transition) {
2614 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2616 case GST_STATE_CHANGE_PAUSED_TO_READY:
2617 gst_wavparse_destroy_sourcepad (wav);
2618 gst_wavparse_reset (wav);
2620 case GST_STATE_CHANGE_READY_TO_NULL:
2629 plugin_init (GstPlugin * plugin)
2633 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2637 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2640 "Parse a .wav file into raw audio",
2641 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)