1 /* GStreamer RealMedia demuxer
2 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
3 * Copyright (C) <2003> David A. Schleef <ds@schleef.org>
4 * Copyright (C) <2004> Stephane Loeuillet <gstreamer@leroutier.net>
5 * Copyright (C) <2005> Owen Fraser-Green <owen@discobabe.net>
6 * Copyright (C) <2005> Michael Smith <fluendo.com>
7 * Copyright (C) <2006> Wim Taymans <wim@fluendo.com>
8 * Copyright (C) <2006> Tim-Philipp Müller <tim centricular net>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 02111-1307, USA.
32 #include "rdtmanager.h"
40 #define RMDEMUX_GUINT32_GET(a) GST_READ_UINT32_BE(a)
41 #define RMDEMUX_GUINT16_GET(a) GST_READ_UINT16_BE(a)
42 #define RMDEMUX_FOURCC_GET(a) GST_READ_UINT32_LE(a)
43 #define HEADER_SIZE 10
46 typedef struct _GstRMDemuxIndex GstRMDemuxIndex;
48 struct _GstRMDemuxStream
57 GstFlowReturn last_flow;
61 GstRMDemuxIndex *index;
63 gint framerate_numerator;
64 gint framerate_denominator;
70 guint16 rate; /* samplerate */
71 guint16 n_channels; /* channels */
72 guint16 sample_width; /* bits_per_sample */
73 guint16 leaf_size; /* subpacket_size */
74 guint32 packet_size; /* coded_frame_size */
76 guint32 extra_data_size; /* codec_data_length */
77 guint8 *extra_data; /* extras */
79 gboolean needs_descrambling;
80 guint subpackets_needed; /* subpackets needed for descrambling */
81 GPtrArray *subpackets; /* array containing subpacket GstBuffers */
83 GstTagList *pending_tags;
86 struct _GstRMDemuxIndex
89 GstClockTime timestamp;
92 static GstElementDetails gst_rmdemux_details = {
95 "Demultiplex a RealMedia file into audio and video streams",
96 "David Schleef <ds@schleef.org>"
99 static GstStaticPadTemplate gst_rmdemux_sink_template =
100 GST_STATIC_PAD_TEMPLATE ("sink",
103 GST_STATIC_CAPS ("application/vnd.rn-realmedia")
106 static GstStaticPadTemplate gst_rmdemux_videosrc_template =
107 GST_STATIC_PAD_TEMPLATE ("video_%02d",
110 GST_STATIC_CAPS_ANY);
112 static GstStaticPadTemplate gst_rmdemux_audiosrc_template =
113 GST_STATIC_PAD_TEMPLATE ("audio_%02d",
116 GST_STATIC_CAPS_ANY);
118 GST_DEBUG_CATEGORY_STATIC (rmdemux_debug);
119 #define GST_CAT_DEFAULT rmdemux_debug
121 static GstElementClass *parent_class = NULL;
123 static void gst_rmdemux_class_init (GstRMDemuxClass * klass);
124 static void gst_rmdemux_base_init (GstRMDemuxClass * klass);
125 static void gst_rmdemux_init (GstRMDemux * rmdemux);
126 static void gst_rmdemux_finalize (GObject * object);
127 static GstStateChangeReturn gst_rmdemux_change_state (GstElement * element,
128 GstStateChange transition);
129 static GstFlowReturn gst_rmdemux_chain (GstPad * pad, GstBuffer * buffer);
130 static void gst_rmdemux_loop (GstPad * pad);
131 static gboolean gst_rmdemux_sink_activate (GstPad * sinkpad);
132 static gboolean gst_rmdemux_sink_activate_push (GstPad * sinkpad,
134 static gboolean gst_rmdemux_sink_activate_pull (GstPad * sinkpad,
136 static gboolean gst_rmdemux_sink_event (GstPad * pad, GstEvent * event);
137 static gboolean gst_rmdemux_src_event (GstPad * pad, GstEvent * event);
138 static void gst_rmdemux_send_event (GstRMDemux * rmdemux, GstEvent * event);
139 static const GstQueryType *gst_rmdemux_src_query_types (GstPad * pad);
140 static gboolean gst_rmdemux_src_query (GstPad * pad, GstQuery * query);
141 static gboolean gst_rmdemux_perform_seek (GstRMDemux * rmdemux,
144 static void gst_rmdemux_parse__rmf (GstRMDemux * rmdemux, const guint8 * data,
146 static void gst_rmdemux_parse_prop (GstRMDemux * rmdemux, const guint8 * data,
148 static void gst_rmdemux_parse_mdpr (GstRMDemux * rmdemux,
149 const guint8 * data, int length);
150 static guint gst_rmdemux_parse_indx (GstRMDemux * rmdemux, const guint8 * data,
152 static void gst_rmdemux_parse_data (GstRMDemux * rmdemux, const guint8 * data,
154 static void gst_rmdemux_parse_cont (GstRMDemux * rmdemux, const guint8 * data,
156 static GstFlowReturn gst_rmdemux_parse_packet (GstRMDemux * rmdemux,
157 const guint8 * data, guint16 version, guint16 length);
158 static void gst_rmdemux_parse_indx_data (GstRMDemux * rmdemux,
159 const guint8 * data, int length);
160 static void gst_rmdemux_stream_clear_cached_subpackets (GstRMDemux * rmdemux,
161 GstRMDemuxStream * stream);
162 static GstRMDemuxStream *gst_rmdemux_get_stream_by_id (GstRMDemux * rmdemux,
166 gst_rmdemux_get_type (void)
168 static GType rmdemux_type = 0;
171 static const GTypeInfo rmdemux_info = {
172 sizeof (GstRMDemuxClass),
173 (GBaseInitFunc) gst_rmdemux_base_init, NULL,
174 (GClassInitFunc) gst_rmdemux_class_init,
175 NULL, NULL, sizeof (GstRMDemux), 0,
176 (GInstanceInitFunc) gst_rmdemux_init,
180 g_type_register_static (GST_TYPE_ELEMENT, "GstRMDemux", &rmdemux_info,
187 gst_rmdemux_base_init (GstRMDemuxClass * klass)
189 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
191 gst_element_class_add_pad_template (element_class,
192 gst_static_pad_template_get (&gst_rmdemux_sink_template));
193 gst_element_class_add_pad_template (element_class,
194 gst_static_pad_template_get (&gst_rmdemux_videosrc_template));
195 gst_element_class_add_pad_template (element_class,
196 gst_static_pad_template_get (&gst_rmdemux_audiosrc_template));
197 gst_element_class_set_details (element_class, &gst_rmdemux_details);
201 gst_rmdemux_class_init (GstRMDemuxClass * klass)
203 GObjectClass *gobject_class;
204 GstElementClass *gstelement_class;
206 gobject_class = (GObjectClass *) klass;
207 gstelement_class = (GstElementClass *) klass;
209 parent_class = g_type_class_peek_parent (klass);
211 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rmdemux_change_state);
213 GST_DEBUG_CATEGORY_INIT (rmdemux_debug, "rmdemux",
214 0, "Demuxer for Realmedia streams");
216 gobject_class->finalize = gst_rmdemux_finalize;
220 gst_rmdemux_finalize (GObject * object)
222 GstRMDemux *rmdemux = GST_RMDEMUX (object);
224 if (rmdemux->adapter) {
225 g_object_unref (rmdemux->adapter);
226 rmdemux->adapter = NULL;
229 GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
233 gst_rmdemux_init (GstRMDemux * rmdemux)
236 gst_pad_new_from_static_template (&gst_rmdemux_sink_template, "sink");
237 gst_pad_set_event_function (rmdemux->sinkpad,
238 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_event));
239 gst_pad_set_chain_function (rmdemux->sinkpad,
240 GST_DEBUG_FUNCPTR (gst_rmdemux_chain));
241 gst_pad_set_activate_function (rmdemux->sinkpad,
242 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_activate));
243 gst_pad_set_activatepull_function (rmdemux->sinkpad,
244 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_activate_pull));
245 gst_pad_set_activatepush_function (rmdemux->sinkpad,
246 GST_DEBUG_FUNCPTR (gst_rmdemux_sink_activate_push));
248 gst_element_add_pad (GST_ELEMENT (rmdemux), rmdemux->sinkpad);
250 rmdemux->adapter = gst_adapter_new ();
251 rmdemux->first_ts = GST_CLOCK_TIME_NONE;
252 rmdemux->need_newsegment = TRUE;
256 gst_rmdemux_sink_event (GstPad * pad, GstEvent * event)
261 rmdemux = GST_RMDEMUX (gst_pad_get_parent (pad));
263 GST_LOG_OBJECT (pad, "%s event", GST_EVENT_TYPE_NAME (event));
265 switch (GST_EVENT_TYPE (event)) {
266 case GST_EVENT_NEWSEGMENT:
267 gst_event_unref (event);
271 ret = gst_pad_event_default (pad, event);
275 gst_object_unref (rmdemux);
280 gst_rmdemux_src_event (GstPad * pad, GstEvent * event)
284 GstRMDemux *rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
286 GST_LOG_OBJECT (rmdemux, "handling src event");
288 switch (GST_EVENT_TYPE (event)) {
293 GST_LOG_OBJECT (rmdemux, "Event on src: SEEK");
294 /* can't seek if we are not seekable, FIXME could pass the
295 * seek query upstream after converting it to bytes using
296 * the average bitrate of the stream. */
297 if (!rmdemux->seekable) {
299 GST_DEBUG ("seek on non seekable stream");
303 GST_OBJECT_LOCK (rmdemux);
304 /* check if we can do the seek now */
305 running = rmdemux->running;
306 GST_OBJECT_UNLOCK (rmdemux);
308 /* now do the seek */
310 ret = gst_rmdemux_perform_seek (rmdemux, event);
314 gst_event_unref (event);
318 GST_LOG_OBJECT (rmdemux, "Event on src: type=%d", GST_EVENT_TYPE (event));
319 ret = gst_pad_event_default (pad, event);
326 GST_DEBUG ("error handling event");
327 gst_event_unref (event);
331 /* Validate that this looks like a reasonable point to seek to */
333 gst_rmdemux_validate_offset (GstRMDemux * rmdemux)
336 GstFlowReturn flowret;
337 guint16 version, length;
340 flowret = gst_pad_pull_range (rmdemux->sinkpad, rmdemux->offset, 4, &buffer);
342 if (flowret != GST_FLOW_OK) {
343 GST_DEBUG_OBJECT (rmdemux, "Failed to pull data at offset %d",
347 /* TODO: Can we also be seeking to a 'DATA' chunk header? Check this.
348 * Also, for the case we currently handle, can we check any more? It's pretty
349 * sucky to not be validating a little more heavily than this... */
350 /* This should now be the start of a data packet header. That begins with
351 * a 2-byte 'version' field, which has to be 0 or 1, then a length. I'm not
352 * certain what values are valid for length, but it must always be at least
353 * 4 bytes, and we can check that it won't take us past our known total size
356 version = RMDEMUX_GUINT16_GET (GST_BUFFER_DATA (buffer));
357 if (version != 0 && version != 1) {
358 GST_DEBUG_OBJECT (rmdemux, "Expected version 0 or 1, got %d",
363 length = RMDEMUX_GUINT16_GET (GST_BUFFER_DATA (buffer) + 2);
364 /* TODO: Also check against total stream length */
366 GST_DEBUG_OBJECT (rmdemux, "Expected length >= 4, got %d", (int) length);
371 rmdemux->offset += 4;
372 gst_adapter_clear (rmdemux->adapter);
373 gst_adapter_push (rmdemux->adapter, buffer);
375 GST_WARNING_OBJECT (rmdemux, "Failed to validate seek offset at %d",
383 find_seek_offset_bytes (GstRMDemux * rmdemux, guint target)
387 gboolean ret = FALSE;
392 for (cur = rmdemux->streams; cur; cur = cur->next) {
393 GstRMDemuxStream *stream = cur->data;
395 /* Search backwards through this stream's index until we find the first
396 * timestamp before our target time */
397 for (i = stream->index_length - 1; i >= 0; i--) {
398 if (stream->index[i].offset <= target) {
399 /* Set the seek_offset for the stream so we don't bother parsing it
400 * until we've passed that point */
401 stream->seek_offset = stream->index[i].offset;
402 rmdemux->offset = stream->index[i].offset;
412 find_seek_offset_time (GstRMDemux * rmdemux, GstClockTime time)
415 gboolean ret = FALSE;
417 GstClockTime earliest = GST_CLOCK_TIME_NONE;
420 for (cur = rmdemux->streams; cur; cur = cur->next, n_stream++) {
421 GstRMDemuxStream *stream = cur->data;
423 /* Search backwards through this stream's index until we find the first
424 * timestamp before our target time */
425 for (i = stream->index_length - 1; i >= 0; i--) {
426 if (stream->index[i].timestamp <= time) {
427 /* Set the seek_offset for the stream so we don't bother parsing it
428 * until we've passed that point */
429 stream->seek_offset = stream->index[i].offset;
431 /* If it's also the earliest timestamp we've seen of all streams, then
434 if (earliest == GST_CLOCK_TIME_NONE ||
435 stream->index[i].timestamp < earliest) {
436 earliest = stream->index[i].timestamp;
437 rmdemux->offset = stream->index[i].offset;
438 GST_DEBUG_OBJECT (rmdemux,
439 "We're looking for %" GST_TIME_FORMAT
440 " and we found that stream %d has the latest index at %"
441 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start), n_stream,
442 GST_TIME_ARGS (earliest));
455 gst_rmdemux_perform_seek (GstRMDemux * rmdemux, GstEvent * event)
459 gboolean flush, accurate;
463 GstSeekType cur_type, stop_type;
468 GST_DEBUG_OBJECT (rmdemux, "seek with event");
470 gst_event_parse_seek (event, &rate, &format, &flags,
471 &cur_type, &cur, &stop_type, &stop);
473 /* we can only seek on time */
474 if (format != GST_FORMAT_TIME) {
475 GST_DEBUG_OBJECT (rmdemux, "can only seek on TIME");
478 /* cannot yet do backwards playback */
480 GST_DEBUG_OBJECT (rmdemux, "can only seek with positive rate, not %lf",
485 GST_DEBUG_OBJECT (rmdemux, "seek without event");
491 GST_DEBUG_OBJECT (rmdemux, "seek, rate %g", rate);
493 flush = flags & GST_SEEK_FLAG_FLUSH;
494 accurate = flags & GST_SEEK_FLAG_ACCURATE;
496 /* first step is to unlock the streaming thread if it is
497 * blocked in a chain call, we do this by starting the flush. */
499 gst_pad_push_event (rmdemux->sinkpad, gst_event_new_flush_start ());
500 gst_rmdemux_send_event (rmdemux, gst_event_new_flush_start ());
502 gst_pad_pause_task (rmdemux->sinkpad);
505 GST_LOG_OBJECT (rmdemux, "Done starting flushes");
507 /* now grab the stream lock so that streaming cannot continue, for
508 * non flushing seeks when the element is in PAUSED this could block
510 GST_PAD_STREAM_LOCK (rmdemux->sinkpad);
512 GST_LOG_OBJECT (rmdemux, "Took streamlock");
514 /* close current segment first */
515 if (rmdemux->segment_running && !flush) {
518 newseg = gst_event_new_new_segment (TRUE, rmdemux->segment.rate,
519 GST_FORMAT_TIME, rmdemux->segment.start,
520 rmdemux->segment.last_stop, rmdemux->segment.time);
522 gst_rmdemux_send_event (rmdemux, newseg);
526 gst_segment_set_seek (&rmdemux->segment, rate, format, flags,
527 cur_type, cur, stop_type, stop, &update);
530 GST_DEBUG_OBJECT (rmdemux, "segment positions set to %" GST_TIME_FORMAT "-%"
531 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start),
532 GST_TIME_ARGS (rmdemux->segment.stop));
534 /* we need to stop flushing on the sinkpad as we're going to use it
535 * next. We can do this as we have the STREAM lock now. */
536 gst_pad_push_event (rmdemux->sinkpad, gst_event_new_flush_stop ());
538 GST_LOG_OBJECT (rmdemux, "Pushed FLUSH_STOP event");
540 /* For each stream, find the first index offset equal to or before our seek
541 * target. Of these, find the smallest offset. That's where we seek to.
543 * Then we pull 4 bytes from that offset, and validate that we've seeked to a
544 * what looks like a plausible packet.
545 * If that fails, restart, with the seek target set to one less than the
546 * offset we just tried. If we run out of places to try, treat that as a fatal
549 if (!find_seek_offset_time (rmdemux, rmdemux->segment.last_stop)) {
550 GST_LOG_OBJECT (rmdemux, "Failed to find seek offset by time");
555 GST_LOG_OBJECT (rmdemux, "Validating offset %u", rmdemux->offset);
556 validated = gst_rmdemux_validate_offset (rmdemux);
558 GST_INFO_OBJECT (rmdemux, "Failed to validate offset at %u",
560 if (!find_seek_offset_bytes (rmdemux, rmdemux->offset - 1)) {
564 validated = gst_rmdemux_validate_offset (rmdemux);
567 GST_LOG_OBJECT (rmdemux, "Found final offset. Excellent!");
569 /* now we have a new position, prepare for streaming again */
571 /* Reset the demuxer state */
572 rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
575 gst_rmdemux_send_event (rmdemux, gst_event_new_flush_stop ());
577 /* must send newsegment event from streaming thread, so just set flag */
578 rmdemux->need_newsegment = TRUE;
580 /* notify start of new segment */
581 if (rmdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
582 gst_element_post_message (GST_ELEMENT_CAST (rmdemux),
583 gst_message_new_segment_start (GST_OBJECT_CAST (rmdemux),
584 GST_FORMAT_TIME, rmdemux->segment.last_stop));
587 /* restart our task since it might have been stopped when we did the
589 gst_pad_start_task (rmdemux->sinkpad, (GstTaskFunction) gst_rmdemux_loop,
594 /* streaming can continue now */
595 GST_PAD_STREAM_UNLOCK (rmdemux->sinkpad);
601 GST_DEBUG_OBJECT (rmdemux, "seek failed");
608 gst_rmdemux_src_query (GstPad * pad, GstQuery * query)
610 gboolean res = FALSE;
613 rmdemux = GST_RMDEMUX (gst_pad_get_parent (pad));
615 switch (GST_QUERY_TYPE (query)) {
616 case GST_QUERY_POSITION:
617 GST_DEBUG_OBJECT (rmdemux, "Position query: no idea from demuxer!");
619 case GST_QUERY_DURATION:{
622 gst_query_parse_duration (query, &fmt, NULL);
623 if (fmt == GST_FORMAT_TIME) {
624 GST_OBJECT_LOCK (rmdemux);
625 if (G_LIKELY (rmdemux->running)) {
626 gst_query_set_duration (query, GST_FORMAT_TIME, rmdemux->duration);
627 GST_DEBUG_OBJECT (rmdemux, "duration set to %" GST_TIME_FORMAT,
628 GST_TIME_ARGS (rmdemux->duration));
631 GST_OBJECT_UNLOCK (rmdemux);
635 case GST_QUERY_SEEKING:{
638 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
639 if (fmt == GST_FORMAT_TIME) {
640 GST_OBJECT_LOCK (rmdemux);
641 if (G_LIKELY (rmdemux->running)) {
642 gst_query_set_seeking (query, GST_FORMAT_TIME, rmdemux->seekable,
643 0, rmdemux->duration);
646 GST_OBJECT_UNLOCK (rmdemux);
651 res = gst_pad_query_default (pad, query);
655 gst_object_unref (rmdemux);
659 static const GstQueryType *
660 gst_rmdemux_src_query_types (GstPad * pad)
662 static const GstQueryType query_types[] = {
673 gst_rmdemux_reset (GstRMDemux * rmdemux)
677 GST_OBJECT_LOCK (rmdemux);
678 rmdemux->running = FALSE;
679 GST_OBJECT_UNLOCK (rmdemux);
681 for (cur = rmdemux->streams; cur; cur = cur->next) {
682 GstRMDemuxStream *stream = cur->data;
684 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
685 gst_element_remove_pad (GST_ELEMENT (rmdemux), stream->pad);
686 if (stream->pending_tags)
687 gst_tag_list_free (stream->pending_tags);
688 if (stream->subpackets)
689 g_ptr_array_free (stream->subpackets, TRUE);
690 g_free (stream->index);
693 g_slist_free (rmdemux->streams);
694 rmdemux->streams = NULL;
695 rmdemux->n_audio_streams = 0;
696 rmdemux->n_video_streams = 0;
698 gst_adapter_clear (rmdemux->adapter);
699 rmdemux->state = RMDEMUX_STATE_HEADER;
700 rmdemux->have_pads = FALSE;
702 gst_segment_init (&rmdemux->segment, GST_FORMAT_UNDEFINED);
703 rmdemux->first_ts = GST_CLOCK_TIME_NONE;
704 rmdemux->need_newsegment = TRUE;
707 static GstStateChangeReturn
708 gst_rmdemux_change_state (GstElement * element, GstStateChange transition)
710 GstRMDemux *rmdemux = GST_RMDEMUX (element);
711 GstStateChangeReturn res;
713 switch (transition) {
714 case GST_STATE_CHANGE_NULL_TO_READY:
716 case GST_STATE_CHANGE_READY_TO_PAUSED:
717 rmdemux->state = RMDEMUX_STATE_HEADER;
718 rmdemux->have_pads = FALSE;
719 gst_segment_init (&rmdemux->segment, GST_FORMAT_TIME);
720 rmdemux->running = FALSE;
722 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
728 res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
730 switch (transition) {
731 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
733 case GST_STATE_CHANGE_PAUSED_TO_READY:{
734 gst_rmdemux_reset (rmdemux);
737 case GST_STATE_CHANGE_READY_TO_NULL:
746 /* this function is called when the pad is activated and should start
749 * We check if we can do random access to decide if we work push or
753 gst_rmdemux_sink_activate (GstPad * sinkpad)
755 if (gst_pad_check_pull_range (sinkpad)) {
756 return gst_pad_activate_pull (sinkpad, TRUE);
758 return gst_pad_activate_push (sinkpad, TRUE);
762 /* this function gets called when we activate ourselves in push mode.
763 * We cannot seek (ourselves) in the stream */
765 gst_rmdemux_sink_activate_push (GstPad * pad, gboolean active)
769 rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
771 GST_DEBUG_OBJECT (rmdemux, "activate_push");
773 rmdemux->seekable = FALSE;
778 /* this function gets called when we activate ourselves in pull mode.
779 * We can perform random access to the resource and we start a task
780 * to start reading */
782 gst_rmdemux_sink_activate_pull (GstPad * pad, gboolean active)
786 rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
788 GST_DEBUG_OBJECT (rmdemux, "activate_pull");
791 rmdemux->seekable = TRUE;
793 rmdemux->loop_state = RMDEMUX_LOOP_STATE_HEADER;
794 rmdemux->data_offset = G_MAXUINT;
796 return gst_pad_start_task (pad, (GstTaskFunction) gst_rmdemux_loop, pad);
798 return gst_pad_stop_task (pad);
802 /* random access mode - just pass over to our chain function */
804 gst_rmdemux_loop (GstPad * pad)
808 GstFlowReturn ret = GST_FLOW_OK;
811 rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
813 GST_LOG_OBJECT (rmdemux, "loop with state=%d and offset=0x%x",
814 rmdemux->loop_state, rmdemux->offset);
816 switch (rmdemux->state) {
817 case RMDEMUX_STATE_HEADER:
820 case RMDEMUX_STATE_HEADER_DATA:
823 case RMDEMUX_STATE_DATA_PACKET:
824 size = rmdemux->avg_packet_size;
826 case RMDEMUX_STATE_EOS:
827 GST_LOG_OBJECT (rmdemux, "At EOS, pausing task");
828 ret = GST_FLOW_UNEXPECTED;
831 GST_LOG_OBJECT (rmdemux, "Default: requires %d bytes (state is %d)",
832 (int) rmdemux->size, rmdemux->state);
833 size = rmdemux->size;
836 ret = gst_pad_pull_range (pad, rmdemux->offset, size, &buffer);
837 if (ret != GST_FLOW_OK) {
838 if (rmdemux->offset == rmdemux->index_offset) {
839 /* The index isn't available so forget about it */
840 rmdemux->loop_state = RMDEMUX_LOOP_STATE_DATA;
841 rmdemux->offset = rmdemux->data_offset;
842 GST_OBJECT_LOCK (rmdemux);
843 rmdemux->running = TRUE;
844 rmdemux->seekable = FALSE;
845 GST_OBJECT_UNLOCK (rmdemux);
848 GST_DEBUG_OBJECT (rmdemux, "Unable to pull %d bytes at offset 0x%08x "
849 "(pull_range returned flow %s, state is %d)", (gint) size,
850 rmdemux->offset, gst_flow_get_name (ret), GST_STATE (rmdemux));
855 size = GST_BUFFER_SIZE (buffer);
857 /* Defer to the chain function */
858 ret = gst_rmdemux_chain (pad, buffer);
859 if (ret != GST_FLOW_OK) {
860 GST_DEBUG_OBJECT (rmdemux, "Chain flow failed at offset 0x%08x",
865 rmdemux->offset += size;
867 switch (rmdemux->loop_state) {
868 case RMDEMUX_LOOP_STATE_HEADER:
869 if (rmdemux->offset >= rmdemux->data_offset) {
870 /* It's the end of the header */
871 rmdemux->loop_state = RMDEMUX_LOOP_STATE_INDEX;
872 rmdemux->offset = rmdemux->index_offset;
875 case RMDEMUX_LOOP_STATE_INDEX:
876 if (rmdemux->state == RMDEMUX_STATE_HEADER) {
877 if (rmdemux->index_offset == 0) {
878 /* We've read the last index */
879 rmdemux->loop_state = RMDEMUX_LOOP_STATE_DATA;
880 rmdemux->offset = rmdemux->data_offset;
881 GST_OBJECT_LOCK (rmdemux);
882 rmdemux->running = TRUE;
883 GST_OBJECT_UNLOCK (rmdemux);
885 /* Get the next index */
886 rmdemux->offset = rmdemux->index_offset;
890 case RMDEMUX_LOOP_STATE_DATA:
899 const gchar *reason = gst_flow_get_name (ret);
901 GST_LOG_OBJECT (rmdemux, "pausing task, reason %s", reason);
902 rmdemux->segment_running = FALSE;
903 gst_pad_pause_task (rmdemux->sinkpad);
905 if (GST_FLOW_IS_FATAL (ret) || ret == GST_FLOW_NOT_LINKED) {
906 if (ret == GST_FLOW_UNEXPECTED) {
907 /* perform EOS logic */
908 if (rmdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) {
911 /* for segment playback we need to post when (in stream time)
912 * we stopped, this is either stop (when set) or the duration. */
913 if ((stop = rmdemux->segment.stop) == -1)
914 stop = rmdemux->segment.duration;
916 GST_LOG_OBJECT (rmdemux, "Sending segment done, at end of segment");
917 gst_element_post_message (GST_ELEMENT (rmdemux),
918 gst_message_new_segment_done (GST_OBJECT (rmdemux),
919 GST_FORMAT_TIME, stop));
921 /* normal playback, send EOS to all linked pads */
922 GST_LOG_OBJECT (rmdemux, "Sending EOS, at end of stream");
923 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
926 GST_ELEMENT_ERROR (rmdemux, STREAM, FAILED,
927 (NULL), ("stream stopped, reason %s", reason));
928 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
936 gst_rmdemux_fourcc_isplausible (guint32 fourcc)
940 for (i = 0; i < 4; i++) {
941 if (!isprint ((int) ((unsigned char *) (&fourcc))[i])) {
949 gst_rmdemux_chain (GstPad * pad, GstBuffer * buffer)
951 GstFlowReturn ret = GST_FLOW_OK;
955 GstRMDemux *rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
957 GST_LOG_OBJECT (rmdemux, "Chaining buffer of size %d",
958 GST_BUFFER_SIZE (buffer));
960 gst_adapter_push (rmdemux->adapter, buffer);
963 GST_LOG_OBJECT (rmdemux, "looping in chain");
964 switch (rmdemux->state) {
965 case RMDEMUX_STATE_HEADER:
967 if (gst_adapter_available (rmdemux->adapter) < HEADER_SIZE)
970 data = gst_adapter_peek (rmdemux->adapter, HEADER_SIZE);
972 rmdemux->object_id = RMDEMUX_FOURCC_GET (data + 0);
973 rmdemux->size = RMDEMUX_GUINT32_GET (data + 4) - HEADER_SIZE;
974 rmdemux->object_version = RMDEMUX_GUINT16_GET (data + 8);
976 /* Sanity-check. We assume that the FOURCC is printable ASCII */
977 if (!gst_rmdemux_fourcc_isplausible (rmdemux->object_id)) {
978 /* Failed. Remain in HEADER state, try again... We flush only
979 * the actual FOURCC, not the entire header, because we could
980 * need to resync anywhere at all... really, this should never
982 GST_WARNING_OBJECT (rmdemux, "Bogus looking header, unprintable "
984 gst_adapter_flush (rmdemux->adapter, 4);
989 GST_LOG_OBJECT (rmdemux, "header found with object_id=%"
991 " size=%08x object_version=%d",
992 GST_FOURCC_ARGS (rmdemux->object_id), rmdemux->size,
993 rmdemux->object_version);
995 gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
997 switch (rmdemux->object_id) {
998 case GST_MAKE_FOURCC ('.', 'R', 'M', 'F'):
999 rmdemux->state = RMDEMUX_STATE_HEADER_RMF;
1001 case GST_MAKE_FOURCC ('P', 'R', 'O', 'P'):
1002 rmdemux->state = RMDEMUX_STATE_HEADER_PROP;
1004 case GST_MAKE_FOURCC ('M', 'D', 'P', 'R'):
1005 rmdemux->state = RMDEMUX_STATE_HEADER_MDPR;
1007 case GST_MAKE_FOURCC ('I', 'N', 'D', 'X'):
1008 rmdemux->state = RMDEMUX_STATE_HEADER_INDX;
1010 case GST_MAKE_FOURCC ('D', 'A', 'T', 'A'):
1011 rmdemux->state = RMDEMUX_STATE_HEADER_DATA;
1013 case GST_MAKE_FOURCC ('C', 'O', 'N', 'T'):
1014 rmdemux->state = RMDEMUX_STATE_HEADER_CONT;
1017 rmdemux->state = RMDEMUX_STATE_HEADER_UNKNOWN;
1022 case RMDEMUX_STATE_HEADER_UNKNOWN:
1024 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1027 GST_WARNING_OBJECT (rmdemux, "Unknown object_id %" GST_FOURCC_FORMAT,
1028 GST_FOURCC_ARGS (rmdemux->object_id));
1030 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1031 rmdemux->state = RMDEMUX_STATE_HEADER;
1034 case RMDEMUX_STATE_HEADER_RMF:
1036 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1039 if ((rmdemux->object_version == 0) || (rmdemux->object_version == 1)) {
1040 data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1042 gst_rmdemux_parse__rmf (rmdemux, data, rmdemux->size);
1045 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1046 rmdemux->state = RMDEMUX_STATE_HEADER;
1049 case RMDEMUX_STATE_HEADER_PROP:
1051 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1053 data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1055 gst_rmdemux_parse_prop (rmdemux, data, rmdemux->size);
1057 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1058 rmdemux->state = RMDEMUX_STATE_HEADER;
1061 case RMDEMUX_STATE_HEADER_MDPR:
1063 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1065 data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1067 gst_rmdemux_parse_mdpr (rmdemux, data, rmdemux->size);
1069 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1070 rmdemux->state = RMDEMUX_STATE_HEADER;
1073 case RMDEMUX_STATE_HEADER_CONT:
1075 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1077 data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1079 gst_rmdemux_parse_cont (rmdemux, data, rmdemux->size);
1081 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1082 rmdemux->state = RMDEMUX_STATE_HEADER;
1085 case RMDEMUX_STATE_HEADER_DATA:
1087 /* If we haven't already done so then signal there are no more pads */
1088 if (!rmdemux->have_pads) {
1089 GST_LOG_OBJECT (rmdemux, "no more pads");
1090 gst_element_no_more_pads (GST_ELEMENT (rmdemux));
1091 rmdemux->have_pads = TRUE;
1094 /* The actual header is only 8 bytes */
1095 rmdemux->size = DATA_SIZE;
1096 GST_LOG_OBJECT (rmdemux, "data available %d",
1097 gst_adapter_available (rmdemux->adapter));
1098 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1101 data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1103 gst_rmdemux_parse_data (rmdemux, data, rmdemux->size);
1105 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1107 rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
1110 case RMDEMUX_STATE_HEADER_INDX:
1112 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1114 data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1116 rmdemux->size = gst_rmdemux_parse_indx (rmdemux, data, rmdemux->size);
1118 /* Only flush the header */
1119 gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
1121 rmdemux->state = RMDEMUX_STATE_INDX_DATA;
1124 case RMDEMUX_STATE_INDX_DATA:
1126 /* There's not always an data to get... */
1127 if (rmdemux->size > 0) {
1128 if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1131 data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1133 gst_rmdemux_parse_indx_data (rmdemux, data, rmdemux->size);
1135 gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1138 rmdemux->state = RMDEMUX_STATE_HEADER;
1141 case RMDEMUX_STATE_DATA_PACKET:
1143 if (gst_adapter_available (rmdemux->adapter) < 2)
1146 data = gst_adapter_peek (rmdemux->adapter, 2);
1147 version = RMDEMUX_GUINT16_GET (data);
1148 GST_LOG_OBJECT (rmdemux, "Data packet with version=%d", version);
1150 if (version == 0 || version == 1) {
1153 if (gst_adapter_available (rmdemux->adapter) < 4)
1155 data = gst_adapter_peek (rmdemux->adapter, 4);
1157 length = RMDEMUX_GUINT16_GET (data + 2);
1159 /* Invalid, just drop it */
1160 gst_adapter_flush (rmdemux->adapter, 4);
1162 if (gst_adapter_available (rmdemux->adapter) < length)
1164 data = gst_adapter_peek (rmdemux->adapter, length);
1167 gst_rmdemux_parse_packet (rmdemux, data + 4, version,
1169 rmdemux->chunk_index++;
1171 gst_adapter_flush (rmdemux->adapter, length);
1174 if (rmdemux->chunk_index == rmdemux->n_chunks || length == 0)
1175 rmdemux->state = RMDEMUX_STATE_HEADER;
1178 gst_adapter_flush (rmdemux->adapter, 2);
1180 if (rmdemux->data_offset == 0) {
1181 GST_LOG_OBJECT (rmdemux,
1182 "No further data, internal demux state EOS");
1183 rmdemux->state = RMDEMUX_STATE_EOS;
1185 rmdemux->state = RMDEMUX_STATE_HEADER;
1189 case RMDEMUX_STATE_EOS:
1190 gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
1193 GST_WARNING_OBJECT (rmdemux, "Unhandled state %d", rmdemux->state);
1202 static GstRMDemuxStream *
1203 gst_rmdemux_get_stream_by_id (GstRMDemux * rmdemux, int id)
1207 for (cur = rmdemux->streams; cur; cur = cur->next) {
1208 GstRMDemuxStream *stream = cur->data;
1210 if (stream->id == id) {
1219 gst_rmdemux_send_event (GstRMDemux * rmdemux, GstEvent * event)
1223 for (cur = rmdemux->streams; cur; cur = cur->next) {
1224 GstRMDemuxStream *stream = cur->data;
1226 GST_DEBUG_OBJECT (rmdemux, "Pushing %s event on pad %s",
1227 GST_EVENT_TYPE_NAME (event), GST_PAD_NAME (stream->pad));
1229 gst_event_ref (event);
1230 gst_pad_push_event (stream->pad, event);
1232 gst_event_unref (event);
1236 gst_rmdemux_add_stream (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
1238 GstCaps *stream_caps = NULL;
1239 const gchar *codec_tag = NULL;
1240 const gchar *codec_name = NULL;
1243 if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) {
1244 char *name = g_strdup_printf ("video_%02d", rmdemux->n_video_streams);
1247 gst_pad_new_from_static_template (&gst_rmdemux_videosrc_template, name);
1250 codec_tag = GST_TAG_VIDEO_CODEC;
1252 switch (stream->fourcc) {
1253 case GST_RM_VDO_RV10:
1254 codec_name = "Real Video 1.0";
1257 case GST_RM_VDO_RV20:
1258 codec_name = "Real Video 2.0";
1261 case GST_RM_VDO_RV30:
1262 codec_name = "Real Video 3.0";
1265 case GST_RM_VDO_RV40:
1266 codec_name = "Real Video 4.0";
1270 stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc",
1271 "fourcc", GST_TYPE_FOURCC, stream->fourcc, NULL);
1272 GST_WARNING_OBJECT (rmdemux,
1273 "Unknown video FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)",
1274 GST_FOURCC_ARGS (stream->fourcc), stream->fourcc);
1279 gst_caps_new_simple ("video/x-pn-realvideo", "rmversion", G_TYPE_INT,
1281 "format", G_TYPE_INT,
1282 (int) stream->format,
1283 "subformat", G_TYPE_INT, (int) stream->subformat, NULL);
1287 gst_caps_set_simple (stream_caps,
1288 "width", G_TYPE_INT, stream->width,
1289 "height", G_TYPE_INT, stream->height,
1290 "framerate", GST_TYPE_FRACTION, stream->framerate_numerator,
1291 stream->framerate_denominator, NULL);
1293 rmdemux->n_video_streams++;
1295 } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) {
1296 char *name = g_strdup_printf ("audio_%02d", rmdemux->n_audio_streams);
1299 gst_pad_new_from_static_template (&gst_rmdemux_audiosrc_template, name);
1300 GST_LOG_OBJECT (rmdemux, "Created audio pad \"%s\"", name);
1303 codec_tag = GST_TAG_AUDIO_CODEC;
1305 switch (stream->fourcc) {
1306 /* Older RealAudio Codecs */
1307 case GST_RM_AUD_14_4:
1308 codec_name = "Real Audio 14.4kbps";
1312 case GST_RM_AUD_28_8:
1313 codec_name = "Real Audio 28.8kbps";
1317 /* DolbyNet (Dolby AC3, low bitrate) */
1318 case GST_RM_AUD_DNET:
1319 codec_name = "AC-3 audio";
1321 gst_caps_new_simple ("audio/x-ac3", "rate", G_TYPE_INT,
1322 (int) stream->rate, NULL);
1323 stream->needs_descrambling = TRUE;
1324 stream->subpackets_needed = 1;
1325 stream->subpackets = NULL;
1328 /* RealAudio 10 (AAC) */
1329 case GST_RM_AUD_RAAC:
1330 codec_name = "Real Audio 10 (AAC)";
1335 case GST_RM_AUD_RACP:
1336 /* FIXME: codec_name = */
1338 gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT,
1343 case GST_RM_AUD_ATRC:
1344 codec_name = "Sony ATRAC3";
1345 stream_caps = gst_caps_new_simple ("audio/x-vnd.sony.atrac3", NULL);
1348 /* RealAudio G2 audio */
1349 case GST_RM_AUD_COOK:
1350 codec_name = "Real Audio G2 (Cook)";
1352 stream->needs_descrambling = TRUE;
1353 stream->subpackets_needed = stream->height;
1354 stream->subpackets = NULL;
1357 /* RALF is lossless */
1358 case GST_RM_AUD_RALF:
1359 /* FIXME: codec_name = */
1360 GST_DEBUG_OBJECT (rmdemux, "RALF");
1361 stream_caps = gst_caps_new_simple ("audio/x-ralf-mpeg4-generic", NULL);
1364 /* Sipro/ACELP.NET Voice Codec (MIME unknown) */
1365 case GST_RM_AUD_SIPR:
1366 /* FIXME: codec_name = */
1367 stream_caps = gst_caps_new_simple ("audio/x-sipro", NULL);
1371 stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc",
1372 "fourcc", GST_TYPE_FOURCC, stream->fourcc, NULL);
1373 GST_WARNING_OBJECT (rmdemux,
1374 "Unknown audio FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)",
1375 GST_FOURCC_ARGS (stream->fourcc), stream->fourcc);
1381 gst_caps_new_simple ("audio/x-pn-realaudio", "raversion", G_TYPE_INT,
1382 (int) version, NULL);
1386 gst_caps_set_simple (stream_caps,
1387 "flavor", G_TYPE_INT, (int) stream->flavor,
1388 "rate", G_TYPE_INT, (int) stream->rate,
1389 "channels", G_TYPE_INT, (int) stream->n_channels,
1390 "width", G_TYPE_INT, (int) stream->sample_width,
1391 "leaf_size", G_TYPE_INT, (int) stream->leaf_size,
1392 "packet_size", G_TYPE_INT, (int) stream->packet_size,
1393 "height", G_TYPE_INT, (int) stream->height, NULL);
1395 rmdemux->n_audio_streams++;
1397 GST_WARNING_OBJECT (rmdemux, "not adding stream of type %d, freeing it",
1403 GST_PAD_ELEMENT_PRIVATE (stream->pad) = stream;
1404 rmdemux->streams = g_slist_append (rmdemux->streams, stream);
1405 GST_LOG_OBJECT (rmdemux, "n_streams is now %d",
1406 g_slist_length (rmdemux->streams));
1408 GST_LOG ("stream->pad = %p, stream_caps = %" GST_PTR_FORMAT, stream->pad,
1411 if (stream->pad && stream_caps) {
1413 GST_LOG_OBJECT (rmdemux, "%d bytes of extra data for stream %s",
1414 stream->extra_data_size, GST_PAD_NAME (stream->pad));
1416 /* add codec_data if there is any */
1417 if (stream->extra_data_size > 0) {
1420 buffer = gst_buffer_new_and_alloc (stream->extra_data_size);
1421 memcpy (GST_BUFFER_DATA (buffer), stream->extra_data,
1422 stream->extra_data_size);
1424 gst_caps_set_simple (stream_caps, "codec_data", GST_TYPE_BUFFER,
1427 gst_buffer_unref (buffer);
1430 gst_pad_use_fixed_caps (stream->pad);
1432 gst_pad_set_caps (stream->pad, stream_caps);
1433 gst_pad_set_event_function (stream->pad,
1434 GST_DEBUG_FUNCPTR (gst_rmdemux_src_event));
1435 gst_pad_set_query_type_function (stream->pad,
1436 GST_DEBUG_FUNCPTR (gst_rmdemux_src_query_types));
1437 gst_pad_set_query_function (stream->pad,
1438 GST_DEBUG_FUNCPTR (gst_rmdemux_src_query));
1440 GST_DEBUG_OBJECT (rmdemux, "adding pad %s with caps %" GST_PTR_FORMAT
1441 ", stream_id=%d", GST_PAD_NAME (stream->pad), stream_caps, stream->id);
1442 gst_pad_set_active (stream->pad, TRUE);
1443 gst_element_add_pad (GST_ELEMENT_CAST (rmdemux), stream->pad);
1445 /* save for later, we must send the tags after the newsegment event */
1446 if (codec_name != NULL && codec_tag != NULL) {
1447 stream->pending_tags = gst_tag_list_new ();
1448 gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_KEEP,
1449 codec_tag, codec_name, NULL);
1456 gst_caps_unref (stream_caps);
1460 re_skip_pascal_string (const guint8 * ptr)
1470 gst_rmdemux_parse__rmf (GstRMDemux * rmdemux, const guint8 * data, int length)
1472 GST_LOG_OBJECT (rmdemux, "file_version: %d", RMDEMUX_GUINT32_GET (data));
1473 GST_LOG_OBJECT (rmdemux, "num_headers: %d", RMDEMUX_GUINT32_GET (data + 4));
1477 gst_rmdemux_parse_prop (GstRMDemux * rmdemux, const guint8 * data, int length)
1479 GST_LOG_OBJECT (rmdemux, "max bitrate: %d", RMDEMUX_GUINT32_GET (data));
1480 GST_LOG_OBJECT (rmdemux, "avg bitrate: %d", RMDEMUX_GUINT32_GET (data + 4));
1481 GST_LOG_OBJECT (rmdemux, "max packet size: %d",
1482 RMDEMUX_GUINT32_GET (data + 8));
1483 rmdemux->avg_packet_size = RMDEMUX_GUINT32_GET (data + 12);
1484 GST_LOG_OBJECT (rmdemux, "avg packet size: %d", rmdemux->avg_packet_size);
1485 rmdemux->num_packets = RMDEMUX_GUINT32_GET (data + 16);
1486 GST_LOG_OBJECT (rmdemux, "number of packets: %d", rmdemux->num_packets);
1488 GST_LOG_OBJECT (rmdemux, "duration: %d", RMDEMUX_GUINT32_GET (data + 20));
1489 rmdemux->duration = RMDEMUX_GUINT32_GET (data + 20) * GST_MSECOND;
1491 GST_LOG_OBJECT (rmdemux, "preroll: %d", RMDEMUX_GUINT32_GET (data + 24));
1492 rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 28);
1493 GST_LOG_OBJECT (rmdemux, "offset of INDX section: 0x%08x",
1494 rmdemux->index_offset);
1495 rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 32);
1496 GST_LOG_OBJECT (rmdemux, "offset of DATA section: 0x%08x",
1497 rmdemux->data_offset);
1498 GST_LOG_OBJECT (rmdemux, "n streams: %d", RMDEMUX_GUINT16_GET (data + 36));
1499 GST_LOG_OBJECT (rmdemux, "flags: 0x%04x", RMDEMUX_GUINT16_GET (data + 38));
1503 gst_rmdemux_parse_mdpr (GstRMDemux * rmdemux, const guint8 * data, int length)
1505 GstRMDemuxStream *stream;
1506 char *stream1_type_string;
1507 char *stream2_type_string;
1512 /* re_hexdump_bytes ((guint8 *) data, length, 0); */
1514 stream = g_new0 (GstRMDemuxStream, 1);
1516 stream->id = RMDEMUX_GUINT16_GET (data);
1517 stream->index = NULL;
1518 stream->seek_offset = 0;
1519 stream->last_flow = GST_FLOW_OK;
1520 GST_LOG_OBJECT (rmdemux, "stream_number=%d", stream->id);
1523 stream_type = GST_RMDEMUX_STREAM_UNKNOWN;
1524 stream1_type_string = gst_rm_utils_read_string8 (data + offset,
1525 length - offset, &str_len);
1527 stream2_type_string = gst_rm_utils_read_string8 (data + offset,
1528 length - offset, &str_len);
1531 /* stream1_type_string for audio and video stream is a "put_whatever_you_want" field :
1533 * - "[The ]Video/Audio Stream" (File produced by an official Real encoder)
1534 * - "RealVideoPremierePlugIn-VIDEO/AUDIO" (File produced by Abobe Premiere)
1536 * so, we should not rely on it to know which stream type it is
1539 GST_LOG_OBJECT (rmdemux, "stream type: %s", stream1_type_string);
1540 GST_LOG_OBJECT (rmdemux, "MIME type=%s", stream2_type_string);
1542 if (strcmp (stream2_type_string, "video/x-pn-realvideo") == 0) {
1543 stream_type = GST_RMDEMUX_STREAM_VIDEO;
1544 } else if (strcmp (stream2_type_string,
1545 "video/x-pn-multirate-realvideo") == 0) {
1546 stream_type = GST_RMDEMUX_STREAM_VIDEO;
1547 } else if (strcmp (stream2_type_string, "audio/x-pn-realaudio") == 0) {
1548 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1549 } else if (strcmp (stream2_type_string,
1550 "audio/x-pn-multirate-realaudio") == 0) {
1551 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1552 } else if (strcmp (stream2_type_string,
1553 "audio/x-pn-multirate-realaudio-live") == 0) {
1554 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1555 } else if (strcmp (stream2_type_string, "audio/x-ralf-mpeg4-generic") == 0) {
1556 /* Another audio type found in the real testsuite */
1557 stream_type = GST_RMDEMUX_STREAM_AUDIO;
1558 } else if (strcmp (stream1_type_string, "") == 0 &&
1559 strcmp (stream2_type_string, "logical-fileinfo") == 0) {
1560 stream_type = GST_RMDEMUX_STREAM_FILEINFO;
1562 stream_type = GST_RMDEMUX_STREAM_UNKNOWN;
1563 GST_WARNING_OBJECT (rmdemux, "unknown stream type \"%s\",\"%s\"",
1564 stream1_type_string, stream2_type_string);
1566 g_free (stream1_type_string);
1567 g_free (stream2_type_string);
1571 stream->subtype = stream_type;
1572 switch (stream_type) {
1574 case GST_RMDEMUX_STREAM_VIDEO:
1575 /* RV10/RV20/RV30/RV40 => video/x-pn-realvideo, version=1,2,3,4 */
1576 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 8);
1577 stream->width = RMDEMUX_GUINT16_GET (data + offset + 12);
1578 stream->height = RMDEMUX_GUINT16_GET (data + offset + 14);
1579 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 16);
1580 stream->subformat = RMDEMUX_GUINT32_GET (data + offset + 26);
1581 stream->format = RMDEMUX_GUINT32_GET (data + offset + 30);
1582 stream->extra_data_size = length - (offset + 34);
1583 stream->extra_data = (guint8 *) data + offset + 34;
1584 /* Natural way to represent framerates here requires unsigned 32 bit
1585 * numerator, which we don't have. For the nasty case, approximate...
1588 guint32 numerator = RMDEMUX_GUINT16_GET (data + offset + 22) * 65536 +
1589 RMDEMUX_GUINT16_GET (data + offset + 24);
1590 if (numerator > G_MAXINT) {
1591 stream->framerate_numerator = (gint) (numerator >> 1);
1592 stream->framerate_denominator = 32768;
1594 stream->framerate_numerator = (gint) numerator;
1595 stream->framerate_denominator = 65536;
1599 GST_DEBUG_OBJECT (rmdemux,
1600 "Video stream with fourcc=%" GST_FOURCC_FORMAT
1601 " width=%d height=%d rate=%d framerate=%d/%d subformat=%x format=%x extra_data_size=%d",
1602 GST_FOURCC_ARGS (stream->fourcc), stream->width, stream->height,
1603 stream->rate, stream->framerate_numerator,
1604 stream->framerate_denominator, stream->subformat, stream->format,
1605 stream->extra_data_size);
1607 case GST_RMDEMUX_STREAM_AUDIO:{
1608 stream->version = RMDEMUX_GUINT16_GET (data + offset + 4);
1609 GST_INFO ("stream version = %u", stream->version);
1610 switch (stream->version) {
1612 stream->fourcc = GST_RM_AUD_14_4;
1613 stream->packet_size = 20;
1614 stream->rate = 8000;
1615 stream->n_channels = 1;
1616 stream->sample_width = 16;
1618 stream->leaf_size = 0;
1622 stream->flavor = RMDEMUX_GUINT16_GET (data + offset + 22);
1623 stream->packet_size = RMDEMUX_GUINT32_GET (data + offset + 24);
1624 /* stream->frame_size = RMDEMUX_GUINT32_GET (data + offset + 42); */
1625 stream->leaf_size = RMDEMUX_GUINT16_GET (data + offset + 44);
1626 stream->height = RMDEMUX_GUINT16_GET (data + offset + 40);
1627 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 48);
1628 stream->sample_width = RMDEMUX_GUINT16_GET (data + offset + 52);
1629 stream->n_channels = RMDEMUX_GUINT16_GET (data + offset + 54);
1630 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 62);
1631 stream->extra_data_size = RMDEMUX_GUINT32_GET (data + offset + 69);
1632 GST_DEBUG_OBJECT (rmdemux, "%u bytes of extra codec data",
1633 stream->extra_data_size);
1634 if (length - (offset + 73) >= stream->extra_data_size) {
1635 stream->extra_data = (guint8 *) data + offset + 73;
1637 GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1638 stream->extra_data_size = 0;
1642 stream->flavor = RMDEMUX_GUINT16_GET (data + offset + 22);
1643 stream->packet_size = RMDEMUX_GUINT32_GET (data + offset + 24);
1644 /* stream->frame_size = RMDEMUX_GUINT32_GET (data + offset + 42); */
1645 stream->leaf_size = RMDEMUX_GUINT16_GET (data + offset + 44);
1646 stream->height = RMDEMUX_GUINT16_GET (data + offset + 40);
1647 stream->rate = RMDEMUX_GUINT16_GET (data + offset + 54);
1648 stream->sample_width = RMDEMUX_GUINT16_GET (data + offset + 58);
1649 stream->n_channels = RMDEMUX_GUINT16_GET (data + offset + 60);
1650 stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 66);
1651 stream->extra_data_size = RMDEMUX_GUINT32_GET (data + offset + 74);
1652 GST_DEBUG_OBJECT (rmdemux, "%u bytes of extra codec data",
1653 stream->extra_data_size);
1654 if (length - (offset + 78) >= stream->extra_data_size) {
1655 stream->extra_data = (guint8 *) data + offset + 78;
1657 GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1658 stream->extra_data_size = 0;
1662 GST_WARNING_OBJECT (rmdemux, "Unhandled audio stream version %d",
1668 /* 14_4, 28_8, cook, dnet, sipr, raac, racp, ralf, atrc */
1669 GST_DEBUG_OBJECT (rmdemux,
1670 "Audio stream with rate=%d sample_width=%d n_channels=%d",
1671 stream->rate, stream->sample_width, stream->n_channels);
1675 case GST_RMDEMUX_STREAM_FILEINFO:
1679 /* Length of this section */
1680 GST_DEBUG_OBJECT (rmdemux, "length2: 0x%08x",
1681 RMDEMUX_GUINT32_GET (data + offset));
1684 /* Unknown : 00 00 00 00 */
1687 /* Number of variables that would follow (loop iterations) */
1688 element_nb = RMDEMUX_GUINT32_GET (data + offset);
1691 while (element_nb) {
1692 /* Category Id : 00 00 00 XX 00 00 */
1696 offset += re_skip_pascal_string (data + offset);
1698 /* Variable Value Type */
1699 /* 00 00 00 00 00 => integer/boolean, preceded by length */
1700 /* 00 00 00 02 00 => pascal string, preceded by length, no trailing \0 */
1703 /* Variable Value */
1704 offset += re_skip_pascal_string (data + offset);
1710 case GST_RMDEMUX_STREAM_UNKNOWN:
1715 gst_rmdemux_add_stream (rmdemux, stream);
1719 gst_rmdemux_parse_indx (GstRMDemux * rmdemux, const guint8 * data, int length)
1724 n = RMDEMUX_GUINT32_GET (data);
1725 id = RMDEMUX_GUINT16_GET (data + 4);
1726 rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 6);
1728 GST_DEBUG_OBJECT (rmdemux, "Number of indices=%d Stream ID=%d length=%d", n,
1731 /* Point to the next index_stream */
1732 rmdemux->index_stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
1734 /* Return the length of the index */
1739 gst_rmdemux_parse_indx_data (GstRMDemux * rmdemux, const guint8 * data,
1744 GstRMDemuxIndex *index;
1746 /* The number of index records */
1749 if (rmdemux->index_stream == NULL)
1752 /* don't parse the index a second time when operating pull-based and
1753 * reaching the end of the file */
1754 if (rmdemux->index_stream->index_length > 0) {
1755 GST_DEBUG_OBJECT (rmdemux, "Already have an index for this stream");
1759 index = g_malloc (sizeof (GstRMDemuxIndex) * n);
1760 rmdemux->index_stream->index = index;
1761 rmdemux->index_stream->index_length = n;
1763 for (i = 0; i < n; i++) {
1764 index[i].timestamp = RMDEMUX_GUINT32_GET (data + 2) * GST_MSECOND;
1765 index[i].offset = RMDEMUX_GUINT32_GET (data + 6);
1767 GST_DEBUG_OBJECT (rmdemux, "Index found for timestamp=%f (at offset=%x)",
1768 gst_guint64_to_gdouble (index[i].timestamp) / GST_SECOND,
1775 gst_rmdemux_parse_data (GstRMDemux * rmdemux, const guint8 * data, int length)
1777 rmdemux->n_chunks = RMDEMUX_GUINT32_GET (data);
1778 rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 4);
1779 rmdemux->chunk_index = 0;
1780 GST_DEBUG_OBJECT (rmdemux, "Data chunk found with %d packets "
1781 "(next data at 0x%08x)", rmdemux->n_chunks, rmdemux->data_offset);
1785 gst_rmdemux_parse_cont (GstRMDemux * rmdemux, const guint8 * data, int length)
1789 tags = gst_rm_utils_read_tags (data, length, gst_rm_utils_read_string16);
1791 gst_element_found_tags (GST_ELEMENT (rmdemux), tags);
1795 static GstFlowReturn
1796 gst_rmdemux_combine_flows (GstRMDemux * rmdemux, GstRMDemuxStream * stream,
1801 /* store the value */
1802 stream->last_flow = ret;
1804 /* if it's success we can return the value right away */
1805 if (GST_FLOW_IS_SUCCESS (ret))
1808 /* any other error that is not-linked can be returned right
1810 if (ret != GST_FLOW_NOT_LINKED)
1813 for (cur = rmdemux->streams; cur; cur = cur->next) {
1814 GstRMDemuxStream *ostream = cur->data;
1816 ret = ostream->last_flow;
1817 /* some other return value (must be SUCCESS but we can return
1818 * other values as well) */
1819 if (ret != GST_FLOW_NOT_LINKED)
1822 /* if we get here, all other pads were unlinked and we return
1823 * NOT_LINKED then */
1829 gst_rmdemux_stream_clear_cached_subpackets (GstRMDemux * rmdemux,
1830 GstRMDemuxStream * stream)
1832 if (stream->subpackets == NULL || stream->subpackets->len == 0)
1835 GST_DEBUG_OBJECT (rmdemux, "discarding %u previously collected subpackets",
1836 stream->subpackets->len);
1837 g_ptr_array_foreach (stream->subpackets, (GFunc) gst_mini_object_unref, NULL);
1838 g_ptr_array_set_size (stream->subpackets, 0);
1841 static GstFlowReturn
1842 gst_rmdemux_descramble_cook_audio (GstRMDemux * rmdemux,
1843 GstRMDemuxStream * stream)
1847 guint packet_size = stream->packet_size;
1848 guint height = stream->subpackets->len;
1849 guint leaf_size = stream->leaf_size;
1852 g_assert (stream->height == height);
1854 GST_LOG ("packet_size = %u, leaf_size = %u, height= %u", packet_size,
1857 ret = gst_pad_alloc_buffer_and_set_caps (stream->pad,
1858 GST_BUFFER_OFFSET_NONE, height * packet_size,
1859 GST_PAD_CAPS (stream->pad), &outbuf);
1861 if (ret != GST_FLOW_OK)
1864 for (p = 0; p < height; ++p) {
1865 GstBuffer *b = g_ptr_array_index (stream->subpackets, p);
1866 guint8 *b_data = GST_BUFFER_DATA (b);
1869 GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (b);
1871 for (x = 0; x < packet_size / leaf_size; ++x) {
1874 idx = height * x + ((height + 1) / 2) * (p % 2) + (p / 2);
1875 /* GST_LOG ("%3u => %3u", (height * p) + x, idx); */
1876 memcpy (GST_BUFFER_DATA (outbuf) + leaf_size * idx, b_data, leaf_size);
1877 b_data += leaf_size;
1881 /* some decoders, such as realaudiodec, need to be fed in packet units */
1882 for (p = 0; p < height; ++p) {
1885 subbuf = gst_buffer_create_sub (outbuf, p * packet_size, packet_size);
1886 gst_buffer_set_caps (subbuf, GST_PAD_CAPS (stream->pad));
1887 ret = gst_pad_push (stream->pad, subbuf);
1888 if (ret != GST_FLOW_OK)
1892 gst_buffer_unref (outbuf);
1896 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
1901 static GstFlowReturn
1902 gst_rmdemux_descramble_dnet_audio (GstRMDemux * rmdemux,
1903 GstRMDemuxStream * stream)
1907 buf = g_ptr_array_index (stream->subpackets, 0);
1908 g_ptr_array_index (stream->subpackets, 0) = NULL;
1909 g_ptr_array_set_size (stream->subpackets, 0);
1911 buf = gst_rm_utils_descramble_dnet_buffer (buf);
1912 return gst_pad_push (stream->pad, buf);
1915 static GstFlowReturn
1916 gst_rmdemux_handle_scrambled_packet (GstRMDemux * rmdemux,
1917 GstRMDemuxStream * stream, GstBuffer * buf, gboolean keyframe)
1921 if (stream->subpackets == NULL)
1922 stream->subpackets = g_ptr_array_sized_new (stream->subpackets_needed);
1924 GST_LOG ("Got subpacket %u/%u, len=%u, key=%d", stream->subpackets->len + 1,
1925 stream->subpackets_needed, GST_BUFFER_SIZE (buf), keyframe);
1927 if (keyframe && stream->subpackets->len > 0) {
1928 gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
1931 g_ptr_array_add (stream->subpackets, buf);
1933 if (stream->subpackets->len < stream->subpackets_needed)
1936 g_assert (stream->subpackets->len >= 1);
1938 switch (stream->fourcc) {
1939 case GST_RM_AUD_DNET:
1940 ret = gst_rmdemux_descramble_dnet_audio (rmdemux, stream);
1942 case GST_RM_AUD_COOK:
1943 ret = gst_rmdemux_descramble_cook_audio (rmdemux, stream);
1946 g_assert_not_reached ();
1952 static GstFlowReturn
1953 gst_rmdemux_parse_packet (GstRMDemux * rmdemux, const guint8 * data,
1954 guint16 version, guint16 length)
1957 GstRMDemuxStream *stream;
1959 guint16 packet_size;
1960 GstFlowReturn cret, ret;
1961 GstClockTime timestamp;
1962 gboolean key = FALSE;
1964 id = RMDEMUX_GUINT16_GET (data);
1965 /* timestamp in Msec */
1966 timestamp = RMDEMUX_GUINT32_GET (data + 2) * GST_MSECOND;
1968 gst_segment_set_last_stop (&rmdemux->segment, GST_FORMAT_TIME, timestamp);
1970 GST_LOG_OBJECT (rmdemux, "Parsing a packet for stream=%d, timestamp=%"
1971 GST_TIME_FORMAT ", version=%d", id, GST_TIME_ARGS (timestamp), version);
1973 /* skip stream_id and timestamp */
1975 packet_size = length - (2 + 4);
1977 /* skip other stuff */
1979 /* uint8 packet_group */
1981 key = ((GST_READ_UINT8 (data + 1) & 0x02) != 0);
1985 /* uint16 asm_rule */
1986 /* uint8 asm_flags */
1991 stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
1992 if (!stream || !stream->pad)
1993 goto unknown_stream;
1995 if (rmdemux->first_ts == GST_CLOCK_TIME_NONE) {
1996 GST_DEBUG_OBJECT (rmdemux, "First timestamp: %" GST_TIME_FORMAT,
1997 GST_TIME_ARGS (timestamp));
1998 rmdemux->first_ts = timestamp;
2001 if (rmdemux->need_newsegment) {
2004 event = gst_event_new_new_segment (FALSE, rmdemux->segment.rate,
2005 rmdemux->segment.format, rmdemux->segment.start,
2006 rmdemux->segment.stop, rmdemux->segment.time);
2008 GST_DEBUG_OBJECT (rmdemux, "sending NEWSEGMENT event, segment.start= %"
2009 GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start));
2011 gst_rmdemux_send_event (rmdemux, event);
2012 rmdemux->need_newsegment = FALSE;
2015 if (stream->pending_tags != NULL) {
2016 GST_LOG_OBJECT (stream->pad, "tags %" GST_PTR_FORMAT, stream->pending_tags);
2017 gst_element_found_tags_for_pad (GST_ELEMENT_CAST (rmdemux), stream->pad,
2018 stream->pending_tags);
2019 stream->pending_tags = NULL;
2022 if ((rmdemux->offset + packet_size) <= stream->seek_offset) {
2023 GST_DEBUG_OBJECT (rmdemux,
2024 "Stream %d is skipping: seek_offset=%d, offset=%d, packet_size=%u",
2025 stream->id, stream->seek_offset, rmdemux->offset, packet_size);
2030 ret = gst_pad_alloc_buffer_and_set_caps (stream->pad,
2031 GST_BUFFER_OFFSET_NONE, packet_size, GST_PAD_CAPS (stream->pad), &buffer);
2033 cret = gst_rmdemux_combine_flows (rmdemux, stream, ret);
2034 if (ret != GST_FLOW_OK)
2037 memcpy (GST_BUFFER_DATA (buffer), (guint8 *) data, packet_size);
2038 GST_BUFFER_TIMESTAMP (buffer) = timestamp - rmdemux->first_ts;
2040 if (stream->needs_descrambling) {
2041 ret = gst_rmdemux_handle_scrambled_packet (rmdemux, stream, buffer, key);
2043 GST_LOG_OBJECT (rmdemux, "Pushing buffer of size %d to pad %s",
2044 GST_BUFFER_SIZE (buffer), GST_PAD_NAME (stream->pad));
2046 ret = gst_pad_push (stream->pad, buffer);
2049 cret = gst_rmdemux_combine_flows (rmdemux, stream, ret);
2057 GST_WARNING_OBJECT (rmdemux, "No stream for stream id %d in parsing "
2063 GST_DEBUG_OBJECT (rmdemux, "pad alloc returned %d", ret);
2069 gst_rmdemux_plugin_init (GstPlugin * plugin)
2071 return gst_element_register (plugin, "rmdemux",
2072 GST_RANK_PRIMARY, GST_TYPE_RMDEMUX) &&
2073 gst_element_register (plugin, "rademux",
2074 GST_RANK_SECONDARY, GST_TYPE_REAL_AUDIO_DEMUX);
2079 plugin_init (GstPlugin * plugin)
2081 if (!gst_rmdemux_plugin_init (plugin))
2084 if (!gst_rdt_depay_plugin_init (plugin))
2087 if (!gst_rdt_manager_plugin_init (plugin))
2090 if (!gst_rtsp_real_plugin_init (plugin))
2096 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2099 "RealMedia demuxer and depayloader",
2100 plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);