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
53 /* FIXME 0.11: suppress warnings for deprecated API such as GStaticRecMutex
54 * with newer GLib versions (>= 2.31.0) */
55 #define GLIB_DISABLE_DEPRECATION_WARNINGS
60 #include "gstwavparse.h"
61 #include "gst/riff/riff-ids.h"
62 #include "gst/riff/riff-media.h"
63 #include <gst/base/gsttypefindhelper.h>
64 #include <gst/gst-i18n-plugin.h>
66 GST_DEBUG_CATEGORY_STATIC (wavparse_debug);
67 #define GST_CAT_DEFAULT (wavparse_debug)
69 static void gst_wavparse_dispose (GObject * object);
71 static gboolean gst_wavparse_sink_activate (GstPad * sinkpad);
72 static gboolean gst_wavparse_sink_activate_pull (GstPad * sinkpad,
74 static gboolean gst_wavparse_send_event (GstElement * element,
76 static GstStateChangeReturn gst_wavparse_change_state (GstElement * element,
77 GstStateChange transition);
79 static const GstQueryType *gst_wavparse_get_query_types (GstPad * pad);
80 static gboolean gst_wavparse_pad_query (GstPad * pad, GstQuery * query);
81 static gboolean gst_wavparse_pad_convert (GstPad * pad,
83 gint64 src_value, GstFormat * dest_format, gint64 * dest_value);
85 static GstFlowReturn gst_wavparse_chain (GstPad * pad, GstBuffer * buf);
86 static gboolean gst_wavparse_sink_event (GstPad * pad, GstEvent * event);
87 static void gst_wavparse_loop (GstPad * pad);
88 static gboolean gst_wavparse_srcpad_event (GstPad * pad, GstEvent * event);
90 static void gst_wavparse_set_property (GObject * object, guint prop_id,
91 const GValue * value, GParamSpec * pspec);
92 static void gst_wavparse_get_property (GObject * object, guint prop_id,
93 GValue * value, GParamSpec * pspec);
95 #define DEFAULT_IGNORE_LENGTH FALSE
103 static GstStaticPadTemplate sink_template_factory =
104 GST_STATIC_PAD_TEMPLATE ("wavparse_sink",
107 GST_STATIC_CAPS ("audio/x-wav")
110 #define DEBUG_INIT(bla) \
111 GST_DEBUG_CATEGORY_INIT (wavparse_debug, "wavparse", 0, "WAV parser");
113 GST_BOILERPLATE_FULL (GstWavParse, gst_wavparse, GstElement,
114 GST_TYPE_ELEMENT, DEBUG_INIT);
117 gst_wavparse_base_init (gpointer g_class)
119 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
120 GstPadTemplate *src_template;
123 gst_element_class_add_static_pad_template (element_class,
124 &sink_template_factory);
126 src_template = gst_pad_template_new ("wavparse_src", GST_PAD_SRC,
127 GST_PAD_SOMETIMES, gst_riff_create_audio_template_caps ());
128 gst_element_class_add_pad_template (element_class, src_template);
129 gst_object_unref (src_template);
131 gst_element_class_set_details_simple (element_class, "WAV audio demuxer",
132 "Codec/Demuxer/Audio",
133 "Parse a .wav file into raw audio",
134 "Erik Walthinsen <omega@cse.ogi.edu>");
138 gst_wavparse_class_init (GstWavParseClass * klass)
140 GstElementClass *gstelement_class;
141 GObjectClass *object_class;
143 gstelement_class = (GstElementClass *) klass;
144 object_class = (GObjectClass *) klass;
146 parent_class = g_type_class_peek_parent (klass);
148 object_class->dispose = gst_wavparse_dispose;
150 object_class->set_property = gst_wavparse_set_property;
151 object_class->get_property = gst_wavparse_get_property;
154 * GstWavParse:ignore-length
156 * This selects whether the length found in a data chunk
157 * should be ignored. This may be useful for streamed audio
158 * where the length is unknown until the end of streaming,
159 * and various software/hardware just puts some random value
160 * in there and hopes it doesn't break too much.
164 g_object_class_install_property (object_class, PROP_IGNORE_LENGTH,
165 g_param_spec_boolean ("ignore-length",
167 "Ignore length from the Wave header",
168 DEFAULT_IGNORE_LENGTH, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)
171 gstelement_class->change_state = gst_wavparse_change_state;
172 gstelement_class->send_event = gst_wavparse_send_event;
176 gst_wavparse_reset (GstWavParse * wav)
178 wav->state = GST_WAVPARSE_START;
180 /* These will all be set correctly in the fmt chunk */
194 wav->got_fmt = FALSE;
198 gst_event_unref (wav->seek_event);
199 wav->seek_event = NULL;
201 gst_adapter_clear (wav->adapter);
202 g_object_unref (wav->adapter);
206 gst_tag_list_free (wav->tags);
209 gst_caps_unref (wav->caps);
211 if (wav->start_segment)
212 gst_event_unref (wav->start_segment);
213 wav->start_segment = NULL;
214 if (wav->close_segment)
215 gst_event_unref (wav->close_segment);
216 wav->close_segment = NULL;
220 gst_wavparse_dispose (GObject * object)
222 GstWavParse *wav = GST_WAVPARSE (object);
224 GST_DEBUG_OBJECT (wav, "WAV: Dispose");
225 gst_wavparse_reset (wav);
227 G_OBJECT_CLASS (parent_class)->dispose (object);
231 gst_wavparse_init (GstWavParse * wavparse, GstWavParseClass * g_class)
233 gst_wavparse_reset (wavparse);
237 gst_pad_new_from_static_template (&sink_template_factory, "sink");
238 gst_pad_set_activate_function (wavparse->sinkpad,
239 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate));
240 gst_pad_set_activatepull_function (wavparse->sinkpad,
241 GST_DEBUG_FUNCPTR (gst_wavparse_sink_activate_pull));
242 gst_pad_set_chain_function (wavparse->sinkpad,
243 GST_DEBUG_FUNCPTR (gst_wavparse_chain));
244 gst_pad_set_event_function (wavparse->sinkpad,
245 GST_DEBUG_FUNCPTR (gst_wavparse_sink_event));
246 gst_element_add_pad (GST_ELEMENT_CAST (wavparse), wavparse->sinkpad);
248 /* src, will be created later */
249 wavparse->srcpad = NULL;
253 gst_wavparse_destroy_sourcepad (GstWavParse * wavparse)
255 if (wavparse->srcpad) {
256 gst_element_remove_pad (GST_ELEMENT_CAST (wavparse), wavparse->srcpad);
257 wavparse->srcpad = NULL;
262 gst_wavparse_create_sourcepad (GstWavParse * wavparse)
264 GstElementClass *klass = GST_ELEMENT_GET_CLASS (wavparse);
265 GstPadTemplate *src_template;
267 /* destroy previous one */
268 gst_wavparse_destroy_sourcepad (wavparse);
271 src_template = gst_element_class_get_pad_template (klass, "wavparse_src");
272 wavparse->srcpad = gst_pad_new_from_template (src_template, "src");
273 gst_pad_use_fixed_caps (wavparse->srcpad);
274 gst_pad_set_query_type_function (wavparse->srcpad,
275 GST_DEBUG_FUNCPTR (gst_wavparse_get_query_types));
276 gst_pad_set_query_function (wavparse->srcpad,
277 GST_DEBUG_FUNCPTR (gst_wavparse_pad_query));
278 gst_pad_set_event_function (wavparse->srcpad,
279 GST_DEBUG_FUNCPTR (gst_wavparse_srcpad_event));
281 GST_DEBUG_OBJECT (wavparse, "srcpad created");
284 /* Compute (value * nom) % denom, avoiding overflow. This can be used
285 * to perform ceiling or rounding division together with
286 * gst_util_uint64_scale[_int]. */
287 #define uint64_scale_modulo(val, nom, denom) \
288 ((val % denom) * (nom % denom) % denom)
290 /* Like gst_util_uint64_scale, but performs ceiling division. */
292 uint64_ceiling_scale_int (guint64 val, gint num, gint denom)
294 guint64 result = gst_util_uint64_scale_int (val, num, denom);
296 if (uint64_scale_modulo (val, num, denom) == 0)
302 /* Like gst_util_uint64_scale, but performs ceiling division. */
304 uint64_ceiling_scale (guint64 val, guint64 num, guint64 denom)
306 guint64 result = gst_util_uint64_scale (val, num, denom);
308 if (uint64_scale_modulo (val, num, denom) == 0)
315 /* FIXME: why is that not in use? */
318 gst_wavparse_parse_adtl (GstWavParse * wavparse, int len)
321 GstByteStream *bs = wavparse->bs;
322 gst_riff_chunk *temp_chunk, chunk;
324 struct _gst_riff_labl labl, *temp_labl;
325 struct _gst_riff_ltxt ltxt, *temp_ltxt;
326 struct _gst_riff_note note, *temp_note;
329 GstPropsEntry *entry;
333 props = wavparse->metadata->properties;
337 gst_bytestream_peek_bytes (bs, &tempdata, sizeof (gst_riff_chunk));
338 if (got_bytes != sizeof (gst_riff_chunk)) {
341 temp_chunk = (gst_riff_chunk *) tempdata;
343 chunk.id = GUINT32_FROM_LE (temp_chunk->id);
344 chunk.size = GUINT32_FROM_LE (temp_chunk->size);
346 if (chunk.size == 0) {
347 gst_bytestream_flush (bs, sizeof (gst_riff_chunk));
348 len -= sizeof (gst_riff_chunk);
353 case GST_RIFF_adtl_labl:
355 gst_bytestream_peek_bytes (bs, &tempdata,
356 sizeof (struct _gst_riff_labl));
357 if (got_bytes != sizeof (struct _gst_riff_labl)) {
361 temp_labl = (struct _gst_riff_labl *) tempdata;
362 labl.id = GUINT32_FROM_LE (temp_labl->id);
363 labl.size = GUINT32_FROM_LE (temp_labl->size);
364 labl.identifier = GUINT32_FROM_LE (temp_labl->identifier);
366 gst_bytestream_flush (bs, sizeof (struct _gst_riff_labl));
367 len -= sizeof (struct _gst_riff_labl);
369 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, labl.size - 4);
370 if (got_bytes != labl.size - 4) {
374 label_name = (char *) tempdata;
376 gst_bytestream_flush (bs, ((labl.size - 4) + 1) & ~1);
377 len -= (((labl.size - 4) + 1) & ~1);
379 new_caps = gst_caps_new ("label",
380 "application/x-gst-metadata",
381 gst_props_new ("identifier", G_TYPE_INT (labl.identifier),
382 "name", G_TYPE_STRING (label_name), NULL));
384 if (gst_props_get (props, "labels", &caps, NULL)) {
385 caps = g_list_append (caps, new_caps);
387 caps = g_list_append (NULL, new_caps);
389 entry = gst_props_entry_new ("labels", GST_PROPS_GLIST (caps));
390 gst_props_add_entry (props, entry);
395 case GST_RIFF_adtl_ltxt:
397 gst_bytestream_peek_bytes (bs, &tempdata,
398 sizeof (struct _gst_riff_ltxt));
399 if (got_bytes != sizeof (struct _gst_riff_ltxt)) {
403 temp_ltxt = (struct _gst_riff_ltxt *) tempdata;
404 ltxt.id = GUINT32_FROM_LE (temp_ltxt->id);
405 ltxt.size = GUINT32_FROM_LE (temp_ltxt->size);
406 ltxt.identifier = GUINT32_FROM_LE (temp_ltxt->identifier);
407 ltxt.length = GUINT32_FROM_LE (temp_ltxt->length);
408 ltxt.purpose = GUINT32_FROM_LE (temp_ltxt->purpose);
409 ltxt.country = GUINT16_FROM_LE (temp_ltxt->country);
410 ltxt.language = GUINT16_FROM_LE (temp_ltxt->language);
411 ltxt.dialect = GUINT16_FROM_LE (temp_ltxt->dialect);
412 ltxt.codepage = GUINT16_FROM_LE (temp_ltxt->codepage);
414 gst_bytestream_flush (bs, sizeof (struct _gst_riff_ltxt));
415 len -= sizeof (struct _gst_riff_ltxt);
417 if (ltxt.size - 20 > 0) {
418 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, ltxt.size - 20);
419 if (got_bytes != ltxt.size - 20) {
423 gst_bytestream_flush (bs, ((ltxt.size - 20) + 1) & ~1);
424 len -= (((ltxt.size - 20) + 1) & ~1);
426 label_name = (char *) tempdata;
431 new_caps = gst_caps_new ("ltxt",
432 "application/x-gst-metadata",
433 gst_props_new ("identifier", G_TYPE_INT (ltxt.identifier),
434 "name", G_TYPE_STRING (label_name),
435 "length", G_TYPE_INT (ltxt.length), NULL));
437 if (gst_props_get (props, "ltxts", &caps, NULL)) {
438 caps = g_list_append (caps, new_caps);
440 caps = g_list_append (NULL, new_caps);
442 entry = gst_props_entry_new ("ltxts", GST_PROPS_GLIST (caps));
443 gst_props_add_entry (props, entry);
448 case GST_RIFF_adtl_note:
450 gst_bytestream_peek_bytes (bs, &tempdata,
451 sizeof (struct _gst_riff_note));
452 if (got_bytes != sizeof (struct _gst_riff_note)) {
456 temp_note = (struct _gst_riff_note *) tempdata;
457 note.id = GUINT32_FROM_LE (temp_note->id);
458 note.size = GUINT32_FROM_LE (temp_note->size);
459 note.identifier = GUINT32_FROM_LE (temp_note->identifier);
461 gst_bytestream_flush (bs, sizeof (struct _gst_riff_note));
462 len -= sizeof (struct _gst_riff_note);
464 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, note.size - 4);
465 if (got_bytes != note.size - 4) {
469 gst_bytestream_flush (bs, ((note.size - 4) + 1) & ~1);
470 len -= (((note.size - 4) + 1) & ~1);
472 label_name = (char *) tempdata;
474 new_caps = gst_caps_new ("note",
475 "application/x-gst-metadata",
476 gst_props_new ("identifier", G_TYPE_INT (note.identifier),
477 "name", G_TYPE_STRING (label_name), NULL));
479 if (gst_props_get (props, "notes", &caps, NULL)) {
480 caps = g_list_append (caps, new_caps);
482 caps = g_list_append (NULL, new_caps);
484 entry = gst_props_entry_new ("notes", GST_PROPS_GLIST (caps));
485 gst_props_add_entry (props, entry);
491 g_print ("Unknown chunk: %" GST_FOURCC_FORMAT "\n",
492 GST_FOURCC_ARGS (chunk.id));
497 g_object_notify (G_OBJECT (wavparse), "metadata");
501 gst_wavparse_parse_cues (GstWavParse * wavparse, int len)
504 GstByteStream *bs = wavparse->bs;
505 struct _gst_riff_cue *temp_cue, cue;
506 struct _gst_riff_cuepoints *points;
510 GstPropsEntry *entry;
516 gst_bytestream_peek_bytes (bs, &tempdata,
517 sizeof (struct _gst_riff_cue));
518 temp_cue = (struct _gst_riff_cue *) tempdata;
520 /* fixup for our big endian friends */
521 cue.id = GUINT32_FROM_LE (temp_cue->id);
522 cue.size = GUINT32_FROM_LE (temp_cue->size);
523 cue.cuepoints = GUINT32_FROM_LE (temp_cue->cuepoints);
525 gst_bytestream_flush (bs, sizeof (struct _gst_riff_cue));
526 if (got_bytes != sizeof (struct _gst_riff_cue)) {
530 len -= sizeof (struct _gst_riff_cue);
532 /* -4 because cue.size contains the cuepoints size
533 and we've already flushed that out of the system */
534 required = cue.size - 4;
535 got_bytes = gst_bytestream_peek_bytes (bs, &tempdata, required);
536 gst_bytestream_flush (bs, ((required) + 1) & ~1);
537 if (got_bytes != required) {
541 len -= (((cue.size - 4) + 1) & ~1);
543 /* now we have an array of struct _gst_riff_cuepoints in tempdata */
544 points = (struct _gst_riff_cuepoints *) tempdata;
546 for (i = 0; i < cue.cuepoints; i++) {
549 caps = gst_caps_new ("cues",
550 "application/x-gst-metadata",
551 gst_props_new ("identifier", G_TYPE_INT (points[i].identifier),
552 "position", G_TYPE_INT (points[i].offset), NULL));
553 cues = g_list_append (cues, caps);
556 entry = gst_props_entry_new ("cues", GST_PROPS_GLIST (cues));
557 gst_props_add_entry (wavparse->metadata->properties, entry);
560 g_object_notify (G_OBJECT (wavparse), "metadata");
563 /* Read 'fmt ' header */
565 gst_wavparse_fmt (GstWavParse * wav)
567 gst_riff_strf_auds *header = NULL;
570 if (!gst_riff_read_strf_auds (wav, &header))
573 wav->format = header->format;
574 wav->rate = header->rate;
575 wav->channels = header->channels;
576 if (wav->channels == 0)
579 wav->blockalign = header->blockalign;
580 wav->width = (header->blockalign * 8) / header->channels;
581 wav->depth = header->size;
582 wav->bps = header->av_bps;
586 /* Note: gst_riff_create_audio_caps might need to fix values in
587 * the header header depending on the format, so call it first */
588 caps = gst_riff_create_audio_caps (header->format, NULL, header, NULL);
594 gst_wavparse_create_sourcepad (wav);
595 gst_pad_use_fixed_caps (wav->srcpad);
596 gst_pad_set_active (wav->srcpad, TRUE);
597 gst_pad_set_caps (wav->srcpad, caps);
598 gst_caps_free (caps);
599 gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
600 gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
602 GST_DEBUG ("frequency %u, channels %u", wav->rate, wav->channels);
609 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
610 ("No FMT tag found"));
615 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
616 ("Stream claims to contain zero channels - invalid data"));
622 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
623 ("Stream claims to bitrate of <= zero - invalid data"));
629 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL), (NULL));
635 gst_wavparse_other (GstWavParse * wav)
639 if (!gst_riff_peek_head (wav, &tag, &length, NULL)) {
640 GST_WARNING_OBJECT (wav, "could not peek head");
643 GST_DEBUG_OBJECT (wav, "got tag (%08x) %4.4s, length %u", tag,
644 (const gchar *) &tag, length);
647 case GST_RIFF_TAG_LIST:
648 if (!(tag = gst_riff_peek_list (wav))) {
649 GST_WARNING_OBJECT (wav, "could not peek list");
654 case GST_RIFF_LIST_INFO:
655 if (!gst_riff_read_list (wav, &tag) || !gst_riff_read_info (wav)) {
656 GST_WARNING_OBJECT (wav, "could not read list");
661 case GST_RIFF_LIST_adtl:
662 if (!gst_riff_read_skip (wav)) {
663 GST_WARNING_OBJECT (wav, "could not read skip");
669 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag,
671 if (!gst_riff_read_skip (wav)) {
672 GST_WARNING_OBJECT (wav, "could not read skip");
680 case GST_RIFF_TAG_data:
681 if (!gst_bytestream_flush (wav->bs, 8)) {
682 GST_WARNING_OBJECT (wav, "could not flush 8 bytes");
686 GST_DEBUG_OBJECT (wav, "switching to data mode");
687 wav->state = GST_WAVPARSE_DATA;
688 wav->datastart = gst_bytestream_tell (wav->bs);
692 /* length is 0, data probably stretches to the end
694 GST_DEBUG_OBJECT (wav, "length is 0 trying to find length");
695 /* get length of file */
696 file_length = gst_bytestream_length (wav->bs);
697 if (file_length == -1) {
698 GST_DEBUG_OBJECT (wav,
699 "could not get file length, assuming data to eof");
700 /* could not get length, assuming till eof */
701 length = G_MAXUINT32;
703 if (file_length > G_MAXUINT32) {
704 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
705 ", clipping to 32 bits", file_length);
706 /* could not get length, assuming till eof */
707 length = G_MAXUINT32;
709 GST_DEBUG_OBJECT (wav, "file length %" G_GUINT64_FORMAT
710 ", datalength %u", file_length, length);
711 /* substract offset of datastart from length */
712 length = file_length - wav->datastart;
713 GST_DEBUG_OBJECT (wav, "datalength %u", length);
716 wav->datasize = (guint64) length;
717 GST_DEBUG_OBJECT (wav, "datasize = %ld", length)
720 case GST_RIFF_TAG_cue:
721 if (!gst_riff_read_skip (wav)) {
722 GST_WARNING_OBJECT (wav, "could not read skip");
728 GST_DEBUG_OBJECT (wav, "skipping tag (%08x) %4.4s", tag, (gchar *) & tag);
729 if (!gst_riff_read_skip (wav))
740 gst_wavparse_parse_file_header (GstElement * element, GstBuffer * buf)
744 if (!gst_riff_parse_file_header (element, buf, &doctype))
747 if (doctype != GST_RIFF_RIFF_WAVE)
755 GST_ELEMENT_ERROR (element, STREAM, WRONG_TYPE, (NULL),
756 ("File is not a WAVE file: %" GST_FOURCC_FORMAT,
757 GST_FOURCC_ARGS (doctype)));
763 gst_wavparse_stream_init (GstWavParse * wav)
766 GstBuffer *buf = NULL;
768 if ((res = gst_pad_pull_range (wav->sinkpad,
769 wav->offset, 12, &buf)) != GST_FLOW_OK)
771 else if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), buf))
772 return GST_FLOW_ERROR;
780 gst_wavparse_time_to_bytepos (GstWavParse * wav, gint64 ts, gint64 * bytepos)
782 /* -1 always maps to -1 */
788 /* 0 always maps to 0 */
795 *bytepos = uint64_ceiling_scale (ts, (guint64) wav->bps, GST_SECOND);
797 } else if (wav->fact) {
799 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
800 *bytepos = uint64_ceiling_scale (ts, bps, GST_SECOND);
807 /* This function is used to perform seeks on the element.
809 * It also works when event is NULL, in which case it will just
810 * start from the last configured segment. This technique is
811 * used when activating the element and to perform the seek in
815 gst_wavparse_perform_seek (GstWavParse * wav, GstEvent * event)
819 GstFormat format, bformat;
821 GstSeekType cur_type = GST_SEEK_TYPE_NONE, stop_type;
822 gint64 cur, stop, upstream_size;
825 GstSegment seeksegment = { 0, };
829 GST_DEBUG_OBJECT (wav, "doing seek with event");
831 gst_event_parse_seek (event, &rate, &format, &flags,
832 &cur_type, &cur, &stop_type, &stop);
834 /* no negative rates yet */
838 if (format != wav->segment.format) {
839 GST_INFO_OBJECT (wav, "converting seek-event from %s to %s",
840 gst_format_get_name (format),
841 gst_format_get_name (wav->segment.format));
843 if (cur_type != GST_SEEK_TYPE_NONE)
845 gst_pad_query_convert (wav->srcpad, format, cur,
846 &wav->segment.format, &cur);
847 if (res && stop_type != GST_SEEK_TYPE_NONE)
849 gst_pad_query_convert (wav->srcpad, format, stop,
850 &wav->segment.format, &stop);
854 format = wav->segment.format;
857 GST_DEBUG_OBJECT (wav, "doing seek without event");
860 cur_type = GST_SEEK_TYPE_SET;
861 stop_type = GST_SEEK_TYPE_SET;
864 /* in push mode, we must delegate to upstream */
865 if (wav->streaming) {
866 gboolean res = FALSE;
868 /* if streaming not yet started; only prepare initial newsegment */
869 if (!event || wav->state != GST_WAVPARSE_DATA) {
870 if (wav->start_segment)
871 gst_event_unref (wav->start_segment);
873 gst_event_new_new_segment (FALSE, wav->segment.rate,
874 wav->segment.format, wav->segment.last_stop, wav->segment.duration,
875 wav->segment.last_stop);
878 /* convert seek positions to byte positions in data sections */
879 if (format == GST_FORMAT_TIME) {
880 /* should not fail */
881 if (!gst_wavparse_time_to_bytepos (wav, cur, &cur))
883 if (!gst_wavparse_time_to_bytepos (wav, stop, &stop))
886 /* mind sample boundary and header */
888 cur -= (cur % wav->bytes_per_sample);
889 cur += wav->datastart;
892 stop -= (stop % wav->bytes_per_sample);
893 stop += wav->datastart;
895 GST_DEBUG_OBJECT (wav, "Pushing BYTE seek rate %g, "
896 "start %" G_GINT64_FORMAT ", stop %" G_GINT64_FORMAT, rate, cur,
898 /* BYTE seek event */
899 event = gst_event_new_seek (rate, GST_FORMAT_BYTES, flags, cur_type, cur,
901 res = gst_pad_push_event (wav->sinkpad, event);
907 flush = flags & GST_SEEK_FLAG_FLUSH;
909 /* now we need to make sure the streaming thread is stopped. We do this by
910 * either sending a FLUSH_START event downstream which will cause the
911 * streaming thread to stop with a WRONG_STATE.
912 * For a non-flushing seek we simply pause the task, which will happen as soon
913 * as it completes one iteration (and thus might block when the sink is
914 * blocking in preroll). */
917 GST_DEBUG_OBJECT (wav, "sending flush start");
918 gst_pad_push_event (wav->srcpad, gst_event_new_flush_start ());
921 gst_pad_pause_task (wav->sinkpad);
924 /* we should now be able to grab the streaming thread because we stopped it
925 * with the above flush/pause code */
926 GST_PAD_STREAM_LOCK (wav->sinkpad);
928 /* save current position */
929 last_stop = wav->segment.last_stop;
931 GST_DEBUG_OBJECT (wav, "stopped streaming at %" G_GINT64_FORMAT, last_stop);
933 /* copy segment, we need this because we still need the old
934 * segment when we close the current segment. */
935 memcpy (&seeksegment, &wav->segment, sizeof (GstSegment));
937 /* configure the seek parameters in the seeksegment. We will then have the
938 * right values in the segment to perform the seek */
940 GST_DEBUG_OBJECT (wav, "configuring seek");
941 gst_segment_set_seek (&seeksegment, rate, format, flags,
942 cur_type, cur, stop_type, stop, &update);
945 /* figure out the last position we need to play. If it's configured (stop !=
946 * -1), use that, else we play until the total duration of the file */
947 if ((stop = seeksegment.stop) == -1)
948 stop = seeksegment.duration;
950 GST_DEBUG_OBJECT (wav, "cur_type =%d", cur_type);
951 if ((cur_type != GST_SEEK_TYPE_NONE)) {
952 /* bring offset to bytes, if the bps is 0, we have the segment in BYTES and
953 * we can just copy the last_stop. If not, we use the bps to convert TIME to
955 if (!gst_wavparse_time_to_bytepos (wav, seeksegment.last_stop,
956 (gint64 *) & wav->offset))
957 wav->offset = seeksegment.last_stop;
958 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
959 wav->offset -= (wav->offset % wav->bytes_per_sample);
960 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
961 wav->offset += wav->datastart;
962 GST_LOG_OBJECT (wav, "offset=%" G_GUINT64_FORMAT, wav->offset);
964 GST_LOG_OBJECT (wav, "continue from offset=%" G_GUINT64_FORMAT,
968 if (stop_type != GST_SEEK_TYPE_NONE) {
969 if (!gst_wavparse_time_to_bytepos (wav, stop, (gint64 *) & wav->end_offset))
970 wav->end_offset = stop;
971 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
972 wav->end_offset -= (wav->end_offset % wav->bytes_per_sample);
973 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
974 wav->end_offset += wav->datastart;
975 GST_LOG_OBJECT (wav, "end_offset=%" G_GUINT64_FORMAT, wav->end_offset);
977 GST_LOG_OBJECT (wav, "continue to end_offset=%" G_GUINT64_FORMAT,
981 /* make sure filesize is not exceeded due to rounding errors or so,
982 * same precaution as in _stream_headers */
983 bformat = GST_FORMAT_BYTES;
984 if (gst_pad_query_peer_duration (wav->sinkpad, &bformat, &upstream_size))
985 wav->end_offset = MIN (wav->end_offset, upstream_size);
987 /* this is the range of bytes we will use for playback */
988 wav->offset = MIN (wav->offset, wav->end_offset);
989 wav->dataleft = wav->end_offset - wav->offset;
991 GST_DEBUG_OBJECT (wav,
992 "seek: rate %lf, offset %" G_GUINT64_FORMAT ", end %" G_GUINT64_FORMAT
993 ", segment %" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT, rate, wav->offset,
994 wav->end_offset, GST_TIME_ARGS (seeksegment.start), GST_TIME_ARGS (stop));
996 /* prepare for streaming again */
999 /* if we sent a FLUSH_START, we now send a FLUSH_STOP */
1000 GST_DEBUG_OBJECT (wav, "sending flush stop");
1001 gst_pad_push_event (wav->srcpad, gst_event_new_flush_stop ());
1002 } else if (wav->segment_running) {
1003 /* we are running the current segment and doing a non-flushing seek,
1004 * close the segment first based on the previous last_stop. */
1005 GST_DEBUG_OBJECT (wav, "closing running segment %" G_GINT64_FORMAT
1006 " to %" G_GINT64_FORMAT, wav->segment.start, wav->segment.last_stop);
1008 /* queue the segment for sending in the stream thread */
1009 if (wav->close_segment)
1010 gst_event_unref (wav->close_segment);
1011 wav->close_segment = gst_event_new_new_segment (TRUE,
1012 wav->segment.rate, wav->segment.format,
1013 wav->segment.start, wav->segment.last_stop, wav->segment.start);
1017 /* now we did the seek and can activate the new segment values */
1018 memcpy (&wav->segment, &seeksegment, sizeof (GstSegment));
1020 /* if we're doing a segment seek, post a SEGMENT_START message */
1021 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
1022 gst_element_post_message (GST_ELEMENT_CAST (wav),
1023 gst_message_new_segment_start (GST_OBJECT_CAST (wav),
1024 wav->segment.format, wav->segment.last_stop));
1027 /* now create the newsegment */
1028 GST_DEBUG_OBJECT (wav, "Creating newsegment from %" G_GINT64_FORMAT
1029 " to %" G_GINT64_FORMAT, wav->segment.last_stop, stop);
1031 /* store the newsegment event so it can be sent from the streaming thread. */
1032 if (wav->start_segment)
1033 gst_event_unref (wav->start_segment);
1034 wav->start_segment =
1035 gst_event_new_new_segment (FALSE, wav->segment.rate,
1036 wav->segment.format, wav->segment.last_stop, stop,
1037 wav->segment.last_stop);
1039 /* mark discont if we are going to stream from another position. */
1040 if (last_stop != wav->segment.last_stop) {
1041 GST_DEBUG_OBJECT (wav, "mark DISCONT, we did a seek to another position");
1042 wav->discont = TRUE;
1045 /* and start the streaming task again */
1046 wav->segment_running = TRUE;
1047 if (!wav->streaming) {
1048 gst_pad_start_task (wav->sinkpad, (GstTaskFunction) gst_wavparse_loop,
1052 GST_PAD_STREAM_UNLOCK (wav->sinkpad);
1059 GST_DEBUG_OBJECT (wav, "negative playback rates are not supported yet.");
1064 GST_DEBUG_OBJECT (wav, "unsupported format given, seek aborted.");
1069 GST_DEBUG_OBJECT (wav,
1070 "Could not determine byte position for desired time");
1076 * gst_wavparse_peek_chunk_info:
1077 * @wav Wavparse object
1078 * @tag holder for tag
1079 * @size holder for tag size
1081 * Peek next chunk info (tag and size)
1083 * Returns: %TRUE when the chunk info (header) is available
1086 gst_wavparse_peek_chunk_info (GstWavParse * wav, guint32 * tag, guint32 * size)
1088 const guint8 *data = NULL;
1090 if (gst_adapter_available (wav->adapter) < 8)
1093 data = gst_adapter_peek (wav->adapter, 8);
1094 *tag = GST_READ_UINT32_LE (data);
1095 *size = GST_READ_UINT32_LE (data + 4);
1097 GST_DEBUG ("Next chunk size is %u bytes, type %" GST_FOURCC_FORMAT, *size,
1098 GST_FOURCC_ARGS (*tag));
1104 * gst_wavparse_peek_chunk:
1105 * @wav Wavparse object
1106 * @tag holder for tag
1107 * @size holder for tag size
1109 * Peek enough data for one full chunk
1111 * Returns: %TRUE when the full chunk is available
1114 gst_wavparse_peek_chunk (GstWavParse * wav, guint32 * tag, guint32 * size)
1116 guint32 peek_size = 0;
1119 if (!gst_wavparse_peek_chunk_info (wav, tag, size))
1122 /* size 0 -> empty data buffer would surprise most callers,
1123 * large size -> do not bother trying to squeeze that into adapter,
1124 * so we throw poor man's exception, which can be caught if caller really
1125 * wants to handle 0 size chunk */
1126 if (!(*size) || (*size) >= (1 << 30)) {
1127 GST_INFO ("Invalid/unexpected chunk size %u for tag %" GST_FOURCC_FORMAT,
1128 *size, GST_FOURCC_ARGS (*tag));
1129 /* chain should give up */
1130 wav->abort_buffering = TRUE;
1133 peek_size = (*size + 1) & ~1;
1134 available = gst_adapter_available (wav->adapter);
1136 if (available >= (8 + peek_size)) {
1139 GST_LOG ("but only %u bytes available now", available);
1145 * gst_wavparse_calculate_duration:
1146 * @wav: wavparse object
1148 * Calculate duration on demand and store in @wav. Prefer bps, but use fact as a
1151 * Returns: %TRUE if duration is available.
1154 gst_wavparse_calculate_duration (GstWavParse * wav)
1156 if (wav->duration > 0)
1160 GST_INFO_OBJECT (wav, "Got datasize %" G_GUINT64_FORMAT, wav->datasize);
1162 uint64_ceiling_scale (wav->datasize, GST_SECOND, (guint64) wav->bps);
1163 GST_INFO_OBJECT (wav, "Got duration (bps) %" GST_TIME_FORMAT,
1164 GST_TIME_ARGS (wav->duration));
1166 } else if (wav->fact) {
1167 wav->duration = uint64_ceiling_scale_int (GST_SECOND, wav->fact, wav->rate);
1168 GST_INFO_OBJECT (wav, "Got duration (fact) %" GST_TIME_FORMAT,
1169 GST_TIME_ARGS (wav->duration));
1176 gst_waveparse_ignore_chunk (GstWavParse * wav, GstBuffer * buf, guint32 tag,
1181 if (wav->streaming) {
1182 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1185 GST_DEBUG_OBJECT (wav, "Ignoring tag %" GST_FOURCC_FORMAT,
1186 GST_FOURCC_ARGS (tag));
1187 flush = 8 + ((size + 1) & ~1);
1188 wav->offset += flush;
1189 if (wav->streaming) {
1190 gst_adapter_flush (wav->adapter, flush);
1192 gst_buffer_unref (buf);
1198 #define MAX_BUFFER_SIZE 4096
1200 static GstFlowReturn
1201 gst_wavparse_stream_headers (GstWavParse * wav)
1203 GstFlowReturn res = GST_FLOW_OK;
1204 GstBuffer *buf = NULL;
1205 gst_riff_strf_auds *header = NULL;
1207 gboolean gotdata = FALSE;
1208 GstCaps *caps = NULL;
1209 gchar *codec_name = NULL;
1212 gint64 upstream_size = 0;
1214 /* search for "_fmt" chunk, which should be first */
1215 while (!wav->got_fmt) {
1218 /* The header starts with a 'fmt ' tag */
1219 if (wav->streaming) {
1220 if (!gst_wavparse_peek_chunk (wav, &tag, &size))
1223 gst_adapter_flush (wav->adapter, 8);
1227 buf = gst_adapter_take_buffer (wav->adapter, size);
1229 gst_adapter_flush (wav->adapter, 1);
1230 wav->offset += GST_ROUND_UP_2 (size);
1232 buf = gst_buffer_new ();
1235 if ((res = gst_riff_read_chunk (GST_ELEMENT_CAST (wav), wav->sinkpad,
1236 &wav->offset, &tag, &buf)) != GST_FLOW_OK)
1240 if (tag == GST_RIFF_TAG_JUNK || tag == GST_RIFF_TAG_JUNQ ||
1241 tag == GST_RIFF_TAG_bext || tag == GST_RIFF_TAG_BEXT ||
1242 tag == GST_RIFF_TAG_LIST || tag == GST_RIFF_TAG_ID32 ||
1243 tag == GST_RIFF_TAG_IDVX) {
1244 GST_DEBUG_OBJECT (wav, "skipping %" GST_FOURCC_FORMAT " chunk",
1245 GST_FOURCC_ARGS (tag));
1246 gst_buffer_unref (buf);
1251 if (tag != GST_RIFF_TAG_fmt)
1254 if (!(gst_riff_parse_strf_auds (GST_ELEMENT_CAST (wav), buf, &header,
1256 goto parse_header_error;
1258 buf = NULL; /* parse_strf_auds() took ownership of buffer */
1260 /* do sanity checks of header fields */
1261 if (header->channels == 0)
1263 if (header->rate == 0)
1266 GST_DEBUG_OBJECT (wav, "creating the caps");
1268 /* Note: gst_riff_create_audio_caps might need to fix values in
1269 * the header header depending on the format, so call it first */
1270 caps = gst_riff_create_audio_caps (header->format, NULL, header, extra,
1274 gst_buffer_unref (extra);
1277 goto unknown_format;
1279 /* do more sanity checks of header fields
1280 * (these can be sanitized by gst_riff_create_audio_caps()
1282 wav->format = header->format;
1283 wav->rate = header->rate;
1284 wav->channels = header->channels;
1285 wav->blockalign = header->blockalign;
1286 wav->depth = header->size;
1287 wav->av_bps = header->av_bps;
1293 /* do format specific handling */
1294 switch (wav->format) {
1295 case GST_RIFF_WAVE_FORMAT_MPEGL12:
1296 case GST_RIFF_WAVE_FORMAT_MPEGL3:
1298 /* Note: workaround for mp2/mp3 embedded in wav, that relies on the
1299 * bitrate inside the mpeg stream */
1300 GST_INFO ("resetting bps from %u to 0 for mp2/3", wav->av_bps);
1304 case GST_RIFF_WAVE_FORMAT_PCM:
1305 if (wav->blockalign > wav->channels * (guint) ceil (wav->depth / 8.0))
1306 goto invalid_blockalign;
1309 if (wav->av_bps > wav->blockalign * wav->rate)
1311 /* use the configured bps */
1312 wav->bps = wav->av_bps;
1316 wav->width = (wav->blockalign * 8) / wav->channels;
1317 wav->bytes_per_sample = wav->channels * wav->width / 8;
1319 if (wav->bytes_per_sample <= 0)
1320 goto no_bytes_per_sample;
1322 GST_DEBUG_OBJECT (wav, "blockalign = %u", (guint) wav->blockalign);
1323 GST_DEBUG_OBJECT (wav, "width = %u", (guint) wav->width);
1324 GST_DEBUG_OBJECT (wav, "depth = %u", (guint) wav->depth);
1325 GST_DEBUG_OBJECT (wav, "av_bps = %u", (guint) wav->av_bps);
1326 GST_DEBUG_OBJECT (wav, "frequency = %u", (guint) wav->rate);
1327 GST_DEBUG_OBJECT (wav, "channels = %u", (guint) wav->channels);
1328 GST_DEBUG_OBJECT (wav, "bytes_per_sample = %u", wav->bytes_per_sample);
1330 /* bps can be 0 when we don't have a valid bitrate (mostly for compressed
1331 * formats). This will make the element output a BYTE format segment and
1332 * will not timestamp the outgoing buffers.
1334 GST_DEBUG_OBJECT (wav, "bps = %u", (guint) wav->bps);
1336 GST_DEBUG_OBJECT (wav, "caps = %" GST_PTR_FORMAT, caps);
1338 /* create pad later so we can sniff the first few bytes
1339 * of the real data and correct our caps if necessary */
1340 gst_caps_replace (&wav->caps, caps);
1341 gst_caps_replace (&caps, NULL);
1343 wav->got_fmt = TRUE;
1346 wav->tags = gst_tag_list_new ();
1348 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1349 GST_TAG_AUDIO_CODEC, codec_name, NULL);
1351 g_free (codec_name);
1357 bformat = GST_FORMAT_BYTES;
1358 gst_pad_query_peer_duration (wav->sinkpad, &bformat, &upstream_size);
1359 GST_DEBUG_OBJECT (wav, "upstream size %" G_GUINT64_FORMAT, upstream_size);
1361 /* loop headers until we get data */
1363 if (wav->streaming) {
1364 if (!gst_wavparse_peek_chunk_info (wav, &tag, &size))
1368 gst_pad_pull_range (wav->sinkpad, wav->offset, 8,
1369 &buf)) != GST_FLOW_OK)
1370 goto header_read_error;
1371 tag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
1372 size = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 4);
1375 GST_INFO_OBJECT (wav,
1376 "Got TAG: %" GST_FOURCC_FORMAT ", offset %" G_GUINT64_FORMAT,
1377 GST_FOURCC_ARGS (tag), wav->offset);
1379 /* wav is a st00pid format, we don't know for sure where data starts.
1380 * So we have to go bit by bit until we find the 'data' header
1383 case GST_RIFF_TAG_data:{
1384 GST_DEBUG_OBJECT (wav, "Got 'data' TAG, size : %u", size);
1385 if (wav->ignore_length) {
1386 GST_DEBUG_OBJECT (wav, "Ignoring length");
1389 if (wav->streaming) {
1390 gst_adapter_flush (wav->adapter, 8);
1393 gst_buffer_unref (buf);
1396 wav->datastart = wav->offset;
1397 /* If size is zero, then the data chunk probably actually extends to
1398 the end of the file */
1399 if (size == 0 && upstream_size) {
1400 size = upstream_size - wav->datastart;
1402 /* Or the file might be truncated */
1403 else if (upstream_size) {
1404 size = MIN (size, (upstream_size - wav->datastart));
1406 wav->datasize = (guint64) size;
1407 wav->dataleft = (guint64) size;
1408 wav->end_offset = size + wav->datastart;
1409 if (!wav->streaming) {
1410 /* We will continue parsing tags 'till end */
1411 wav->offset += size;
1413 GST_DEBUG_OBJECT (wav, "datasize = %u", size);
1416 case GST_RIFF_TAG_fact:{
1417 if (wav->format != GST_RIFF_WAVE_FORMAT_MPEGL12 &&
1418 wav->format != GST_RIFF_WAVE_FORMAT_MPEGL3) {
1419 const guint data_size = 4;
1421 GST_INFO_OBJECT (wav, "Have fact chunk");
1422 if (size < data_size) {
1423 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1424 /* need more data */
1427 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1431 /* number of samples (for compressed formats) */
1432 if (wav->streaming) {
1433 const guint8 *data = NULL;
1435 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1438 gst_adapter_flush (wav->adapter, 8);
1439 data = gst_adapter_peek (wav->adapter, data_size);
1440 wav->fact = GST_READ_UINT32_LE (data);
1441 gst_adapter_flush (wav->adapter, GST_ROUND_UP_2 (size));
1443 gst_buffer_unref (buf);
1445 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1446 data_size, &buf)) != GST_FLOW_OK)
1447 goto header_read_error;
1448 wav->fact = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf));
1449 gst_buffer_unref (buf);
1451 GST_DEBUG_OBJECT (wav, "have fact %u", wav->fact);
1452 wav->offset += 8 + GST_ROUND_UP_2 (size);
1455 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1456 /* need more data */
1462 case GST_RIFF_TAG_acid:{
1463 const gst_riff_acid *acid = NULL;
1464 const guint data_size = sizeof (gst_riff_acid);
1466 GST_INFO_OBJECT (wav, "Have acid chunk");
1467 if (size < data_size) {
1468 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size)) {
1469 /* need more data */
1472 GST_DEBUG_OBJECT (wav, "need %u, available %u; ignoring chunk",
1476 if (wav->streaming) {
1477 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1480 gst_adapter_flush (wav->adapter, 8);
1481 acid = (const gst_riff_acid *) gst_adapter_peek (wav->adapter,
1484 gst_buffer_unref (buf);
1486 gst_pad_pull_range (wav->sinkpad, wav->offset + 8,
1487 size, &buf)) != GST_FLOW_OK)
1488 goto header_read_error;
1489 acid = (const gst_riff_acid *) GST_BUFFER_DATA (buf);
1491 /* send data as tags */
1493 wav->tags = gst_tag_list_new ();
1494 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1495 GST_TAG_BEATS_PER_MINUTE, acid->tempo, NULL);
1497 size = GST_ROUND_UP_2 (size);
1498 if (wav->streaming) {
1499 gst_adapter_flush (wav->adapter, size);
1501 gst_buffer_unref (buf);
1503 wav->offset += 8 + size;
1506 /* FIXME: all list tags after data are ignored in streaming mode */
1507 case GST_RIFF_TAG_LIST:{
1510 if (wav->streaming) {
1511 const guint8 *data = NULL;
1513 if (gst_adapter_available (wav->adapter) < 12) {
1516 data = gst_adapter_peek (wav->adapter, 12);
1517 ltag = GST_READ_UINT32_LE (data + 8);
1519 gst_buffer_unref (buf);
1521 gst_pad_pull_range (wav->sinkpad, wav->offset, 12,
1522 &buf)) != GST_FLOW_OK)
1523 goto header_read_error;
1524 ltag = GST_READ_UINT32_LE (GST_BUFFER_DATA (buf) + 8);
1527 case GST_RIFF_LIST_INFO:{
1528 const gint data_size = size - 4;
1531 GST_INFO_OBJECT (wav, "Have LIST chunk INFO size %u", data_size);
1532 if (wav->streaming) {
1533 if (!gst_wavparse_peek_chunk (wav, &tag, &size)) {
1536 gst_adapter_flush (wav->adapter, 12);
1538 if (data_size > 0) {
1539 buf = gst_adapter_take_buffer (wav->adapter, data_size);
1541 gst_adapter_flush (wav->adapter, 1);
1545 gst_buffer_unref (buf);
1546 if (data_size > 0) {
1548 gst_pad_pull_range (wav->sinkpad, wav->offset,
1549 data_size, &buf)) != GST_FLOW_OK)
1550 goto header_read_error;
1553 if (data_size > 0) {
1555 gst_riff_parse_info (GST_ELEMENT (wav), buf, &new);
1557 GstTagList *old = wav->tags;
1559 gst_tag_list_merge (old, new, GST_TAG_MERGE_REPLACE);
1561 gst_tag_list_free (old);
1562 gst_tag_list_free (new);
1564 gst_buffer_unref (buf);
1565 wav->offset += GST_ROUND_UP_2 (data_size);
1570 GST_INFO_OBJECT (wav, "Ignoring LIST chunk %" GST_FOURCC_FORMAT,
1571 GST_FOURCC_ARGS (ltag));
1572 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1573 /* need more data */
1580 if (!gst_waveparse_ignore_chunk (wav, buf, tag, size))
1581 /* need more data */
1586 if (upstream_size && (wav->offset >= upstream_size)) {
1587 /* Now we are gone through the whole file */
1592 GST_DEBUG_OBJECT (wav, "Finished parsing headers");
1594 if (wav->bps <= 0 && wav->fact) {
1596 /* not a good idea, as for embedded mp2/mp3 we set bps to 0 earlier */
1598 (guint32) gst_util_uint64_scale ((guint64) wav->rate, wav->datasize,
1599 (guint64) wav->fact);
1600 GST_INFO_OBJECT (wav, "calculated bps : %u, enabling VBR", wav->bps);
1605 if (gst_wavparse_calculate_duration (wav)) {
1606 gst_segment_init (&wav->segment, GST_FORMAT_TIME);
1607 if (!wav->ignore_length)
1608 gst_segment_set_duration (&wav->segment, GST_FORMAT_TIME, wav->duration);
1610 /* no bitrate, let downstream peer do the math, we'll feed it bytes. */
1611 gst_segment_init (&wav->segment, GST_FORMAT_BYTES);
1612 if (!wav->ignore_length)
1613 gst_segment_set_duration (&wav->segment, GST_FORMAT_BYTES, wav->datasize);
1616 /* now we have all the info to perform a pending seek if any, if no
1617 * event, this will still do the right thing and it will also send
1618 * the right newsegment event downstream. */
1619 gst_wavparse_perform_seek (wav, wav->seek_event);
1620 /* remove pending event */
1621 event_p = &wav->seek_event;
1622 gst_event_replace (event_p, NULL);
1624 /* we just started, we are discont */
1625 wav->discont = TRUE;
1627 wav->state = GST_WAVPARSE_DATA;
1629 /* determine reasonable max buffer size,
1630 * that is, buffers not too small either size or time wise
1631 * so we do not end up with too many of them */
1634 gst_wavparse_time_to_bytepos (wav, 40 * GST_MSECOND, &upstream_size);
1635 wav->max_buf_size = upstream_size;
1636 wav->max_buf_size = MAX (wav->max_buf_size, MAX_BUFFER_SIZE);
1637 if (wav->blockalign > 0)
1638 wav->max_buf_size -= (wav->max_buf_size % wav->blockalign);
1640 GST_DEBUG_OBJECT (wav, "max buffer size %u", wav->max_buf_size);
1648 g_free (codec_name);
1652 gst_caps_unref (caps);
1657 res = GST_FLOW_ERROR;
1662 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1663 ("Invalid WAV header (no fmt at start): %"
1664 GST_FOURCC_FORMAT, GST_FOURCC_ARGS (tag)));
1669 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1670 ("Couldn't parse audio header"));
1675 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1676 ("Stream claims to contain no channels - invalid data"));
1681 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1682 ("Stream with sample_rate == 0 - invalid data"));
1687 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1688 ("Stream claims blockalign = %u, which is more than %u - invalid data",
1689 wav->blockalign, wav->channels * (guint) ceil (wav->depth / 8.0)));
1694 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1695 ("Stream claims av_bsp = %u, which is more than %u - invalid data",
1696 wav->av_bps, wav->blockalign * wav->rate));
1699 no_bytes_per_sample:
1701 GST_ELEMENT_ERROR (wav, STREAM, FAILED, (NULL),
1702 ("Could not caluclate bytes per sample - invalid data"));
1707 GST_ELEMENT_ERROR (wav, STREAM, TYPE_NOT_FOUND, (NULL),
1708 ("No caps found for format 0x%x, %u channels, %u Hz",
1709 wav->format, wav->channels, wav->rate));
1714 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL),
1715 ("Couldn't read in header %d (%s)", res, gst_flow_get_name (res)));
1721 * Read WAV file tag when streaming
1723 static GstFlowReturn
1724 gst_wavparse_parse_stream_init (GstWavParse * wav)
1726 if (gst_adapter_available (wav->adapter) >= 12) {
1729 /* _take flushes the data */
1730 tmp = gst_adapter_take_buffer (wav->adapter, 12);
1732 GST_DEBUG ("Parsing wav header");
1733 if (!gst_wavparse_parse_file_header (GST_ELEMENT_CAST (wav), tmp))
1734 return GST_FLOW_ERROR;
1737 /* Go to next state */
1738 wav->state = GST_WAVPARSE_HEADER;
1743 /* handle an event sent directly to the element.
1745 * This event can be sent either in the READY state or the
1746 * >READY state. The only event of interest really is the seek
1749 * In the READY state we can only store the event and try to
1750 * respect it when going to PAUSED. We assume we are in the
1751 * READY state when our parsing state != GST_WAVPARSE_DATA.
1753 * When we are steaming, we can simply perform the seek right
1757 gst_wavparse_send_event (GstElement * element, GstEvent * event)
1759 GstWavParse *wav = GST_WAVPARSE (element);
1760 gboolean res = FALSE;
1763 GST_DEBUG_OBJECT (wav, "received event %s", GST_EVENT_TYPE_NAME (event));
1765 switch (GST_EVENT_TYPE (event)) {
1766 case GST_EVENT_SEEK:
1767 if (wav->state == GST_WAVPARSE_DATA) {
1768 /* we can handle the seek directly when streaming data */
1769 res = gst_wavparse_perform_seek (wav, event);
1771 GST_DEBUG_OBJECT (wav, "queuing seek for later");
1773 event_p = &wav->seek_event;
1774 gst_event_replace (event_p, event);
1776 /* we always return true */
1783 gst_event_unref (event);
1788 gst_wavparse_have_dts_caps (const GstCaps * caps, GstTypeFindProbability prob)
1792 s = gst_caps_get_structure (caps, 0);
1793 if (!gst_structure_has_name (s, "audio/x-dts"))
1795 if (prob >= GST_TYPE_FIND_LIKELY)
1797 /* DTS at non-0 offsets and without second sync may yield POSSIBLE .. */
1798 if (prob < GST_TYPE_FIND_POSSIBLE)
1800 /* .. in which case we want at least a valid-looking rate and channels */
1801 if (!gst_structure_has_field (s, "channels"))
1803 /* and for extra assurance we could also check the rate from the DTS frame
1804 * against the one in the wav header, but for now let's not do that */
1805 return gst_structure_has_field (s, "rate");
1809 gst_wavparse_add_src_pad (GstWavParse * wav, GstBuffer * buf)
1813 GST_DEBUG_OBJECT (wav, "adding src pad");
1816 s = gst_caps_get_structure (wav->caps, 0);
1817 if (s && gst_structure_has_name (s, "audio/x-raw-int") && buf != NULL) {
1818 GstTypeFindProbability prob;
1821 tf_caps = gst_type_find_helper_for_buffer (GST_OBJECT (wav), buf, &prob);
1822 if (tf_caps != NULL) {
1823 GST_LOG ("typefind caps = %" GST_PTR_FORMAT ", P=%d", tf_caps, prob);
1824 if (gst_wavparse_have_dts_caps (tf_caps, prob)) {
1825 GST_INFO_OBJECT (wav, "Found DTS marker in file marked as raw PCM");
1826 gst_caps_unref (wav->caps);
1827 wav->caps = tf_caps;
1829 gst_tag_list_add (wav->tags, GST_TAG_MERGE_REPLACE,
1830 GST_TAG_AUDIO_CODEC, "dts", NULL);
1832 GST_DEBUG_OBJECT (wav, "found caps %" GST_PTR_FORMAT " for stream "
1833 "marked as raw PCM audio, but ignoring for now", tf_caps);
1834 gst_caps_unref (tf_caps);
1840 gst_wavparse_create_sourcepad (wav);
1841 gst_pad_set_active (wav->srcpad, TRUE);
1842 gst_pad_set_caps (wav->srcpad, wav->caps);
1843 gst_caps_replace (&wav->caps, NULL);
1845 gst_element_add_pad (GST_ELEMENT_CAST (wav), wav->srcpad);
1846 gst_element_no_more_pads (GST_ELEMENT_CAST (wav));
1848 if (wav->close_segment) {
1849 GST_DEBUG_OBJECT (wav, "Send close segment event on newpad");
1850 gst_pad_push_event (wav->srcpad, wav->close_segment);
1851 wav->close_segment = NULL;
1853 if (wav->start_segment) {
1854 GST_DEBUG_OBJECT (wav, "Send start segment event on newpad");
1855 gst_pad_push_event (wav->srcpad, wav->start_segment);
1856 wav->start_segment = NULL;
1860 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (wav), wav->srcpad,
1866 static GstFlowReturn
1867 gst_wavparse_stream_data (GstWavParse * wav)
1869 GstBuffer *buf = NULL;
1870 GstFlowReturn res = GST_FLOW_OK;
1871 guint64 desired, obtained;
1872 GstClockTime timestamp, next_timestamp, duration;
1873 guint64 pos, nextpos;
1876 GST_LOG_OBJECT (wav,
1877 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT " , dataleft: %"
1878 G_GINT64_FORMAT, wav->offset, wav->end_offset, wav->dataleft);
1880 /* Get the next n bytes and output them */
1881 if (wav->dataleft == 0 || wav->dataleft < wav->blockalign)
1884 /* scale the amount of data by the segment rate so we get equal
1885 * amounts of data regardless of the playback rate */
1887 MIN (gst_guint64_to_gdouble (wav->dataleft),
1888 wav->max_buf_size * wav->segment.abs_rate);
1890 if (desired >= wav->blockalign && wav->blockalign > 0)
1891 desired -= (desired % wav->blockalign);
1893 GST_LOG_OBJECT (wav, "Fetching %" G_GINT64_FORMAT " bytes of data "
1894 "from the sinkpad", desired);
1896 if (wav->streaming) {
1897 guint avail = gst_adapter_available (wav->adapter);
1900 /* flush some bytes if evil upstream sends segment that starts
1901 * before data or does is not send sample aligned segment */
1902 if (G_LIKELY (wav->offset >= wav->datastart)) {
1903 extra = (wav->offset - wav->datastart) % wav->bytes_per_sample;
1905 extra = wav->datastart - wav->offset;
1908 if (G_UNLIKELY (extra)) {
1909 extra = wav->bytes_per_sample - extra;
1910 if (extra <= avail) {
1911 GST_DEBUG_OBJECT (wav, "flushing %u bytes to sample boundary", extra);
1912 gst_adapter_flush (wav->adapter, extra);
1913 wav->offset += extra;
1914 wav->dataleft -= extra;
1915 goto iterate_adapter;
1917 GST_DEBUG_OBJECT (wav, "flushing %u bytes", avail);
1918 gst_adapter_clear (wav->adapter);
1919 wav->offset += avail;
1920 wav->dataleft -= avail;
1925 if (avail < desired) {
1926 GST_LOG_OBJECT (wav, "Got only %u bytes of data from the sinkpad", avail);
1930 buf = gst_adapter_take_buffer (wav->adapter, desired);
1932 if ((res = gst_pad_pull_range (wav->sinkpad, wav->offset,
1933 desired, &buf)) != GST_FLOW_OK)
1936 /* we may get a short buffer at the end of the file */
1937 if (GST_BUFFER_SIZE (buf) < desired) {
1938 GST_LOG_OBJECT (wav, "Got only %u bytes of data", GST_BUFFER_SIZE (buf));
1939 if (GST_BUFFER_SIZE (buf) >= wav->blockalign) {
1940 buf = gst_buffer_make_metadata_writable (buf);
1941 GST_BUFFER_SIZE (buf) -= (GST_BUFFER_SIZE (buf) % wav->blockalign);
1943 gst_buffer_unref (buf);
1949 obtained = GST_BUFFER_SIZE (buf);
1951 /* our positions in bytes */
1952 pos = wav->offset - wav->datastart;
1953 nextpos = pos + obtained;
1955 /* update offsets, does not overflow. */
1956 GST_BUFFER_OFFSET (buf) = pos / wav->bytes_per_sample;
1957 GST_BUFFER_OFFSET_END (buf) = nextpos / wav->bytes_per_sample;
1959 /* first chunk of data? create the source pad. We do this only here so
1960 * we can detect broken .wav files with dts disguised as raw PCM (sigh) */
1961 if (G_UNLIKELY (wav->first)) {
1963 /* this will also push the segment events */
1964 gst_wavparse_add_src_pad (wav, buf);
1966 /* If we have a pending close/start segment, send it now. */
1967 if (G_UNLIKELY (wav->close_segment != NULL)) {
1968 gst_pad_push_event (wav->srcpad, wav->close_segment);
1969 wav->close_segment = NULL;
1971 if (G_UNLIKELY (wav->start_segment != NULL)) {
1972 gst_pad_push_event (wav->srcpad, wav->start_segment);
1973 wav->start_segment = NULL;
1978 /* and timestamps if we have a bitrate, be careful for overflows */
1979 timestamp = uint64_ceiling_scale (pos, GST_SECOND, (guint64) wav->bps);
1981 uint64_ceiling_scale (nextpos, GST_SECOND, (guint64) wav->bps);
1982 duration = next_timestamp - timestamp;
1984 /* update current running segment position */
1985 if (G_LIKELY (next_timestamp >= wav->segment.start))
1986 gst_segment_set_last_stop (&wav->segment, GST_FORMAT_TIME,
1988 } else if (wav->fact) {
1990 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
1991 /* and timestamps if we have a bitrate, be careful for overflows */
1992 timestamp = uint64_ceiling_scale (pos, GST_SECOND, bps);
1993 next_timestamp = uint64_ceiling_scale (nextpos, GST_SECOND, bps);
1994 duration = next_timestamp - timestamp;
1996 /* no bitrate, all we know is that the first sample has timestamp 0, all
1997 * other positions and durations have unknown timestamp. */
2001 timestamp = GST_CLOCK_TIME_NONE;
2002 duration = GST_CLOCK_TIME_NONE;
2003 /* update current running segment position with byte offset */
2004 if (G_LIKELY (nextpos >= wav->segment.start))
2005 gst_segment_set_last_stop (&wav->segment, GST_FORMAT_BYTES, nextpos);
2007 if ((pos > 0) && wav->vbr) {
2008 /* don't set timestamps for VBR files if it's not the first buffer */
2009 timestamp = GST_CLOCK_TIME_NONE;
2010 duration = GST_CLOCK_TIME_NONE;
2013 GST_DEBUG_OBJECT (wav, "marking DISCONT");
2014 GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT);
2015 wav->discont = FALSE;
2018 GST_BUFFER_TIMESTAMP (buf) = timestamp;
2019 GST_BUFFER_DURATION (buf) = duration;
2021 /* don't forget to set the caps on the buffer */
2022 gst_buffer_set_caps (buf, GST_PAD_CAPS (wav->srcpad));
2024 GST_LOG_OBJECT (wav,
2025 "Got buffer. timestamp:%" GST_TIME_FORMAT " , duration:%" GST_TIME_FORMAT
2026 ", size:%u", GST_TIME_ARGS (timestamp), GST_TIME_ARGS (duration),
2027 GST_BUFFER_SIZE (buf));
2029 if ((res = gst_pad_push (wav->srcpad, buf)) != GST_FLOW_OK)
2032 if (obtained < wav->dataleft) {
2033 wav->offset += obtained;
2034 wav->dataleft -= obtained;
2036 wav->offset += wav->dataleft;
2040 /* Iterate until need more data, so adapter size won't grow */
2041 if (wav->streaming) {
2042 GST_LOG_OBJECT (wav,
2043 "offset: %" G_GINT64_FORMAT " , end: %" G_GINT64_FORMAT, wav->offset,
2045 goto iterate_adapter;
2052 GST_DEBUG_OBJECT (wav, "found EOS");
2053 return GST_FLOW_UNEXPECTED;
2057 /* check if we got EOS */
2058 if (res == GST_FLOW_UNEXPECTED)
2061 GST_WARNING_OBJECT (wav,
2062 "Error getting %" G_GINT64_FORMAT " bytes from the "
2063 "sinkpad (dataleft = %" G_GINT64_FORMAT ")", desired, wav->dataleft);
2068 GST_INFO_OBJECT (wav,
2069 "Error pushing on srcpad %s:%s, reason %s, is linked? = %d",
2070 GST_DEBUG_PAD_NAME (wav->srcpad), gst_flow_get_name (res),
2071 gst_pad_is_linked (wav->srcpad));
2077 gst_wavparse_loop (GstPad * pad)
2080 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2082 GST_LOG_OBJECT (wav, "process data");
2084 switch (wav->state) {
2085 case GST_WAVPARSE_START:
2086 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2087 if ((ret = gst_wavparse_stream_init (wav)) != GST_FLOW_OK)
2090 wav->state = GST_WAVPARSE_HEADER;
2093 case GST_WAVPARSE_HEADER:
2094 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2095 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2098 wav->state = GST_WAVPARSE_DATA;
2099 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2102 case GST_WAVPARSE_DATA:
2103 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2107 g_assert_not_reached ();
2114 const gchar *reason = gst_flow_get_name (ret);
2116 GST_DEBUG_OBJECT (wav, "pausing task, reason %s", reason);
2117 wav->segment_running = FALSE;
2118 gst_pad_pause_task (pad);
2120 if (ret == GST_FLOW_UNEXPECTED) {
2121 /* add pad before we perform EOS */
2122 if (G_UNLIKELY (wav->first)) {
2124 gst_wavparse_add_src_pad (wav, NULL);
2127 if (wav->state == GST_WAVPARSE_START)
2128 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2129 ("No valid input found before end of stream"), (NULL));
2131 /* perform EOS logic */
2132 if (wav->segment.flags & GST_SEEK_FLAG_SEGMENT) {
2135 if ((stop = wav->segment.stop) == -1)
2136 stop = wav->segment.duration;
2138 gst_element_post_message (GST_ELEMENT_CAST (wav),
2139 gst_message_new_segment_done (GST_OBJECT_CAST (wav),
2140 wav->segment.format, stop));
2142 if (wav->srcpad != NULL)
2143 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2145 } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_UNEXPECTED) {
2146 /* for fatal errors we post an error message, post the error
2147 * first so the app knows about the error first. */
2148 GST_ELEMENT_ERROR (wav, STREAM, FAILED,
2149 (_("Internal data flow error.")),
2150 ("streaming task paused, reason %s (%d)", reason, ret));
2151 if (wav->srcpad != NULL)
2152 gst_pad_push_event (wav->srcpad, gst_event_new_eos ());
2158 static GstFlowReturn
2159 gst_wavparse_chain (GstPad * pad, GstBuffer * buf)
2162 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2164 GST_LOG_OBJECT (wav, "adapter_push %u bytes", GST_BUFFER_SIZE (buf));
2166 gst_adapter_push (wav->adapter, buf);
2168 switch (wav->state) {
2169 case GST_WAVPARSE_START:
2170 GST_INFO_OBJECT (wav, "GST_WAVPARSE_START");
2171 if ((ret = gst_wavparse_parse_stream_init (wav)) != GST_FLOW_OK)
2174 if (wav->state != GST_WAVPARSE_HEADER)
2177 /* otherwise fall-through */
2178 case GST_WAVPARSE_HEADER:
2179 GST_INFO_OBJECT (wav, "GST_WAVPARSE_HEADER");
2180 if ((ret = gst_wavparse_stream_headers (wav)) != GST_FLOW_OK)
2183 if (!wav->got_fmt || wav->datastart == 0)
2186 wav->state = GST_WAVPARSE_DATA;
2187 GST_INFO_OBJECT (wav, "GST_WAVPARSE_DATA");
2190 case GST_WAVPARSE_DATA:
2191 if (buf && GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DISCONT))
2192 wav->discont = TRUE;
2193 if ((ret = gst_wavparse_stream_data (wav)) != GST_FLOW_OK)
2197 g_return_val_if_reached (GST_FLOW_ERROR);
2200 if (G_UNLIKELY (wav->abort_buffering)) {
2201 wav->abort_buffering = FALSE;
2202 ret = GST_FLOW_ERROR;
2203 /* sort of demux/parse error */
2204 GST_ELEMENT_ERROR (wav, STREAM, DEMUX, (NULL), ("unhandled buffer size"));
2210 static GstFlowReturn
2211 gst_wavparse_flush_data (GstWavParse * wav)
2213 GstFlowReturn ret = GST_FLOW_OK;
2216 if ((av = gst_adapter_available (wav->adapter)) > 0) {
2218 wav->end_offset = wav->offset + av;
2219 ret = gst_wavparse_stream_data (wav);
2226 gst_wavparse_sink_event (GstPad * pad, GstEvent * event)
2228 GstWavParse *wav = GST_WAVPARSE (GST_PAD_PARENT (pad));
2229 gboolean ret = TRUE;
2231 GST_LOG_OBJECT (wav, "handling %s event", GST_EVENT_TYPE_NAME (event));
2233 switch (GST_EVENT_TYPE (event)) {
2234 case GST_EVENT_NEWSEGMENT:
2237 gdouble rate, arate;
2238 gint64 start, stop, time, offset = 0, end_offset = -1;
2242 /* some debug output */
2243 gst_segment_init (&segment, GST_FORMAT_UNDEFINED);
2244 gst_event_parse_new_segment_full (event, &update, &rate, &arate, &format,
2245 &start, &stop, &time);
2246 gst_segment_set_newsegment_full (&segment, update, rate, arate, format,
2248 GST_DEBUG_OBJECT (wav,
2249 "received format %d newsegment %" GST_SEGMENT_FORMAT, format,
2252 if (wav->state != GST_WAVPARSE_DATA) {
2253 GST_DEBUG_OBJECT (wav, "still starting, eating event");
2257 /* now we are either committed to TIME or BYTE format,
2258 * and we only expect a BYTE segment, e.g. following a seek */
2259 if (format == GST_FORMAT_BYTES) {
2262 start -= wav->datastart;
2263 start = MAX (start, 0);
2267 stop -= wav->datastart;
2268 stop = MAX (stop, 0);
2270 if (wav->segment.format == GST_FORMAT_TIME) {
2271 guint64 bps = wav->bps;
2273 /* operating in format TIME, so we can convert */
2274 if (!bps && wav->fact)
2276 gst_util_uint64_scale_int (wav->datasize, wav->rate, wav->fact);
2280 uint64_ceiling_scale (start, GST_SECOND, (guint64) wav->bps);
2283 uint64_ceiling_scale (stop, GST_SECOND, (guint64) wav->bps);
2287 GST_DEBUG_OBJECT (wav, "unsupported segment format, ignoring");
2291 /* accept upstream's notion of segment and distribute along */
2292 gst_segment_set_newsegment_full (&wav->segment, update, rate, arate,
2293 wav->segment.format, start, stop, start);
2294 /* also store the newsegment event for the streaming thread */
2295 if (wav->start_segment)
2296 gst_event_unref (wav->start_segment);
2297 wav->start_segment =
2298 gst_event_new_new_segment_full (update, rate, arate,
2299 wav->segment.format, start, stop, start);
2300 GST_DEBUG_OBJECT (wav, "Pushing newseg update %d, rate %g, "
2301 "applied rate %g, format %d, start %" G_GINT64_FORMAT ", "
2302 "stop %" G_GINT64_FORMAT, update, rate, arate, wav->segment.format,
2305 /* stream leftover data in current segment */
2306 gst_wavparse_flush_data (wav);
2307 /* and set up streaming thread for next one */
2308 wav->offset = offset;
2309 wav->end_offset = end_offset;
2310 if (wav->end_offset > 0) {
2311 wav->dataleft = wav->end_offset - wav->offset;
2313 /* infinity; upstream will EOS when done */
2314 wav->dataleft = G_MAXUINT64;
2317 gst_event_unref (event);
2321 /* add pad if needed so EOS is seen downstream */
2322 if (G_UNLIKELY (wav->first)) {
2324 gst_wavparse_add_src_pad (wav, NULL);
2326 /* stream leftover data in current segment */
2327 gst_wavparse_flush_data (wav);
2330 if (wav->state == GST_WAVPARSE_START)
2331 GST_ELEMENT_ERROR (wav, STREAM, WRONG_TYPE,
2332 ("No valid input found before end of stream"), (NULL));
2335 case GST_EVENT_FLUSH_STOP:
2336 gst_adapter_clear (wav->adapter);
2337 wav->discont = TRUE;
2340 ret = gst_pad_event_default (wav->sinkpad, event);
2348 /* convert and query stuff */
2349 static const GstFormat *
2350 gst_wavparse_get_formats (GstPad * pad)
2352 static GstFormat formats[] = {
2355 GST_FORMAT_DEFAULT, /* a "frame", ie a set of samples per Hz */
2364 gst_wavparse_pad_convert (GstPad * pad,
2365 GstFormat src_format, gint64 src_value,
2366 GstFormat * dest_format, gint64 * dest_value)
2368 GstWavParse *wavparse;
2369 gboolean res = TRUE;
2371 wavparse = GST_WAVPARSE (GST_PAD_PARENT (pad));
2373 if (*dest_format == src_format) {
2374 *dest_value = src_value;
2378 if ((wavparse->bps == 0) && !wavparse->fact)
2381 GST_INFO_OBJECT (wavparse, "converting value from %s to %s",
2382 gst_format_get_name (src_format), gst_format_get_name (*dest_format));
2384 switch (src_format) {
2385 case GST_FORMAT_BYTES:
2386 switch (*dest_format) {
2387 case GST_FORMAT_DEFAULT:
2388 *dest_value = src_value / wavparse->bytes_per_sample;
2389 /* make sure we end up on a sample boundary */
2390 *dest_value -= *dest_value % wavparse->bytes_per_sample;
2392 case GST_FORMAT_TIME:
2393 /* src_value + datastart = offset */
2394 GST_INFO_OBJECT (wavparse,
2395 "src=%" G_GINT64_FORMAT ", offset=%" G_GINT64_FORMAT, src_value,
2397 if (wavparse->bps > 0)
2398 *dest_value = uint64_ceiling_scale (src_value, GST_SECOND,
2399 (guint64) wavparse->bps);
2400 else if (wavparse->fact) {
2401 guint64 bps = uint64_ceiling_scale_int (wavparse->datasize,
2402 wavparse->rate, wavparse->fact);
2404 *dest_value = uint64_ceiling_scale_int (src_value, GST_SECOND, bps);
2415 case GST_FORMAT_DEFAULT:
2416 switch (*dest_format) {
2417 case GST_FORMAT_BYTES:
2418 *dest_value = src_value * wavparse->bytes_per_sample;
2420 case GST_FORMAT_TIME:
2421 *dest_value = gst_util_uint64_scale (src_value, GST_SECOND,
2422 (guint64) wavparse->rate);
2430 case GST_FORMAT_TIME:
2431 switch (*dest_format) {
2432 case GST_FORMAT_BYTES:
2433 if (wavparse->bps > 0)
2434 *dest_value = gst_util_uint64_scale (src_value,
2435 (guint64) wavparse->bps, GST_SECOND);
2437 guint64 bps = gst_util_uint64_scale_int (wavparse->datasize,
2438 wavparse->rate, wavparse->fact);
2440 *dest_value = gst_util_uint64_scale (src_value, bps, GST_SECOND);
2442 /* make sure we end up on a sample boundary */
2443 *dest_value -= *dest_value % wavparse->blockalign;
2445 case GST_FORMAT_DEFAULT:
2446 *dest_value = gst_util_uint64_scale (src_value,
2447 (guint64) wavparse->rate, GST_SECOND);
2466 GST_DEBUG_OBJECT (wavparse, "bps 0 or no fact chunk, cannot convert");
2472 static const GstQueryType *
2473 gst_wavparse_get_query_types (GstPad * pad)
2475 static const GstQueryType types[] = {
2486 /* handle queries for location and length in requested format */
2488 gst_wavparse_pad_query (GstPad * pad, GstQuery * query)
2490 gboolean res = TRUE;
2491 GstWavParse *wav = GST_WAVPARSE (gst_pad_get_parent (pad));
2493 /* only if we know */
2494 if (wav->state != GST_WAVPARSE_DATA) {
2495 gst_object_unref (wav);
2499 GST_LOG_OBJECT (pad, "%s query", GST_QUERY_TYPE_NAME (query));
2501 switch (GST_QUERY_TYPE (query)) {
2502 case GST_QUERY_POSITION:
2508 /* this is not very precise, as we have pushed severla buffer upstream for prerolling */
2509 curb = wav->offset - wav->datastart;
2510 gst_query_parse_position (query, &format, NULL);
2511 GST_INFO_OBJECT (wav, "pos query at %" G_GINT64_FORMAT, curb);
2514 case GST_FORMAT_TIME:
2515 res = gst_wavparse_pad_convert (pad, GST_FORMAT_BYTES, curb,
2519 format = GST_FORMAT_BYTES;
2524 gst_query_set_position (query, format, cur);
2527 case GST_QUERY_DURATION:
2529 gint64 duration = 0;
2532 if (wav->ignore_length) {
2537 gst_query_parse_duration (query, &format, NULL);
2540 case GST_FORMAT_TIME:{
2541 if ((res = gst_wavparse_calculate_duration (wav))) {
2542 duration = wav->duration;
2547 format = GST_FORMAT_BYTES;
2548 duration = wav->datasize;
2551 gst_query_set_duration (query, format, duration);
2554 case GST_QUERY_CONVERT:
2556 gint64 srcvalue, dstvalue;
2557 GstFormat srcformat, dstformat;
2559 gst_query_parse_convert (query, &srcformat, &srcvalue,
2560 &dstformat, &dstvalue);
2561 res = gst_wavparse_pad_convert (pad, srcformat, srcvalue,
2562 &dstformat, &dstvalue);
2564 gst_query_set_convert (query, srcformat, srcvalue, dstformat, dstvalue);
2567 case GST_QUERY_SEEKING:{
2569 gboolean seekable = FALSE;
2571 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
2572 if (fmt == wav->segment.format) {
2573 if (wav->streaming) {
2576 q = gst_query_new_seeking (GST_FORMAT_BYTES);
2577 if ((res = gst_pad_peer_query (wav->sinkpad, q))) {
2578 gst_query_parse_seeking (q, &fmt, &seekable, NULL, NULL);
2579 GST_LOG_OBJECT (wav, "upstream BYTE seekable %d", seekable);
2581 gst_query_unref (q);
2583 GST_LOG_OBJECT (wav, "looping => seekable");
2587 } else if (fmt == GST_FORMAT_TIME) {
2591 gst_query_set_seeking (query, fmt, seekable, 0, wav->segment.duration);
2596 res = gst_pad_query_default (pad, query);
2599 gst_object_unref (wav);
2604 gst_wavparse_srcpad_event (GstPad * pad, GstEvent * event)
2606 GstWavParse *wavparse = GST_WAVPARSE (gst_pad_get_parent (pad));
2607 gboolean res = FALSE;
2609 GST_DEBUG_OBJECT (wavparse, "%s event", GST_EVENT_TYPE_NAME (event));
2611 switch (GST_EVENT_TYPE (event)) {
2612 case GST_EVENT_SEEK:
2613 /* can only handle events when we are in the data state */
2614 if (wavparse->state == GST_WAVPARSE_DATA) {
2615 res = gst_wavparse_perform_seek (wavparse, event);
2617 gst_event_unref (event);
2620 res = gst_pad_push_event (wavparse->sinkpad, event);
2623 gst_object_unref (wavparse);
2628 gst_wavparse_sink_activate (GstPad * sinkpad)
2630 GstWavParse *wav = GST_WAVPARSE (gst_pad_get_parent (sinkpad));
2634 gst_adapter_clear (wav->adapter);
2635 g_object_unref (wav->adapter);
2636 wav->adapter = NULL;
2639 if (gst_pad_check_pull_range (sinkpad)) {
2640 GST_DEBUG ("going to pull mode");
2641 wav->streaming = FALSE;
2642 res = gst_pad_activate_pull (sinkpad, TRUE);
2644 GST_DEBUG ("going to push (streaming) mode");
2645 wav->streaming = TRUE;
2646 wav->adapter = gst_adapter_new ();
2647 res = gst_pad_activate_push (sinkpad, TRUE);
2649 gst_object_unref (wav);
2655 gst_wavparse_sink_activate_pull (GstPad * sinkpad, gboolean active)
2657 GstWavParse *wav = GST_WAVPARSE (GST_OBJECT_PARENT (sinkpad));
2660 /* if we have a scheduler we can start the task */
2661 wav->segment_running = TRUE;
2662 return gst_pad_start_task (sinkpad, (GstTaskFunction) gst_wavparse_loop,
2665 wav->segment_running = FALSE;
2666 return gst_pad_stop_task (sinkpad);
2670 static GstStateChangeReturn
2671 gst_wavparse_change_state (GstElement * element, GstStateChange transition)
2673 GstStateChangeReturn ret;
2674 GstWavParse *wav = GST_WAVPARSE (element);
2676 switch (transition) {
2677 case GST_STATE_CHANGE_NULL_TO_READY:
2679 case GST_STATE_CHANGE_READY_TO_PAUSED:
2680 gst_wavparse_reset (wav);
2682 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
2688 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
2690 switch (transition) {
2691 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
2693 case GST_STATE_CHANGE_PAUSED_TO_READY:
2694 gst_wavparse_destroy_sourcepad (wav);
2695 gst_wavparse_reset (wav);
2697 case GST_STATE_CHANGE_READY_TO_NULL:
2706 gst_wavparse_set_property (GObject * object, guint prop_id,
2707 const GValue * value, GParamSpec * pspec)
2711 g_return_if_fail (GST_IS_WAVPARSE (object));
2712 self = GST_WAVPARSE (object);
2715 case PROP_IGNORE_LENGTH:
2716 self->ignore_length = g_value_get_boolean (value);
2719 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2725 gst_wavparse_get_property (GObject * object, guint prop_id,
2726 GValue * value, GParamSpec * pspec)
2730 g_return_if_fail (GST_IS_WAVPARSE (object));
2731 self = GST_WAVPARSE (object);
2734 case PROP_IGNORE_LENGTH:
2735 g_value_set_boolean (value, self->ignore_length);
2738 G_OBJECT_WARN_INVALID_PROPERTY_ID (self, prop_id, pspec);
2743 plugin_init (GstPlugin * plugin)
2747 return gst_element_register (plugin, "wavparse", GST_RANK_PRIMARY,
2751 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2754 "Parse a .wav file into raw audio",
2755 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)