gst/realmedia/: Add simple rdt manager.
[platform/upstream/gstreamer.git] / gst / realmedia / rmdemux.c
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>
9  *
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.
14  *
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.
19  *
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.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #  include "config.h"
28 #endif
29
30 #include "rmdemux.h"
31 #include "rdtdepay.h"
32 #include "rdtmanager.h"
33 #include "rtspreal.h"
34 #include "rmutils.h"
35 #include "rademux.h"
36
37 #include <string.h>
38 #include <ctype.h>
39
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
44 #define DATA_SIZE 8
45
46 typedef struct _GstRMDemuxIndex GstRMDemuxIndex;
47
48 struct _GstRMDemuxStream
49 {
50   guint32 subtype;
51   guint32 fourcc;
52   guint32 subformat;
53   guint32 format;
54
55   int id;
56   GstPad *pad;
57   GstFlowReturn last_flow;
58   int timescale;
59
60   int sample_index;
61   GstRMDemuxIndex *index;
62   int index_length;
63   gint framerate_numerator;
64   gint framerate_denominator;
65   guint32 seek_offset;
66
67   guint16 width;
68   guint16 height;
69   guint16 flavor;
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   */
75   guint16 version;
76   guint32 extra_data_size;      /* codec_data_length  */
77   guint8 *extra_data;           /* extras             */
78
79   gboolean needs_descrambling;
80   guint subpackets_needed;      /* subpackets needed for descrambling    */
81   GPtrArray *subpackets;        /* array containing subpacket GstBuffers */
82
83   GstTagList *pending_tags;
84 };
85
86 struct _GstRMDemuxIndex
87 {
88   guint32 offset;
89   GstClockTime timestamp;
90 };
91
92 static GstElementDetails gst_rmdemux_details = {
93   "RealMedia Demuxer",
94   "Codec/Demuxer",
95   "Demultiplex a RealMedia file into audio and video streams",
96   "David Schleef <ds@schleef.org>"
97 };
98
99 static GstStaticPadTemplate gst_rmdemux_sink_template =
100 GST_STATIC_PAD_TEMPLATE ("sink",
101     GST_PAD_SINK,
102     GST_PAD_ALWAYS,
103     GST_STATIC_CAPS ("application/vnd.rn-realmedia")
104     );
105
106 static GstStaticPadTemplate gst_rmdemux_videosrc_template =
107 GST_STATIC_PAD_TEMPLATE ("video_%02d",
108     GST_PAD_SRC,
109     GST_PAD_SOMETIMES,
110     GST_STATIC_CAPS_ANY);
111
112 static GstStaticPadTemplate gst_rmdemux_audiosrc_template =
113 GST_STATIC_PAD_TEMPLATE ("audio_%02d",
114     GST_PAD_SRC,
115     GST_PAD_SOMETIMES,
116     GST_STATIC_CAPS_ANY);
117
118 GST_DEBUG_CATEGORY_STATIC (rmdemux_debug);
119 #define GST_CAT_DEFAULT rmdemux_debug
120
121 static GstElementClass *parent_class = NULL;
122
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,
133     gboolean active);
134 static gboolean gst_rmdemux_sink_activate_pull (GstPad * sinkpad,
135     gboolean active);
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,
142     GstEvent * event);
143
144 static void gst_rmdemux_parse__rmf (GstRMDemux * rmdemux, const guint8 * data,
145     int length);
146 static void gst_rmdemux_parse_prop (GstRMDemux * rmdemux, const guint8 * data,
147     int length);
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,
151     int length);
152 static void gst_rmdemux_parse_data (GstRMDemux * rmdemux, const guint8 * data,
153     int length);
154 static void gst_rmdemux_parse_cont (GstRMDemux * rmdemux, const guint8 * data,
155     int length);
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,
163     int id);
164
165 static GType
166 gst_rmdemux_get_type (void)
167 {
168   static GType rmdemux_type = 0;
169
170   if (!rmdemux_type) {
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,
177     };
178
179     rmdemux_type =
180         g_type_register_static (GST_TYPE_ELEMENT, "GstRMDemux", &rmdemux_info,
181         0);
182   }
183   return rmdemux_type;
184 }
185
186 static void
187 gst_rmdemux_base_init (GstRMDemuxClass * klass)
188 {
189   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
190
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);
198 }
199
200 static void
201 gst_rmdemux_class_init (GstRMDemuxClass * klass)
202 {
203   GObjectClass *gobject_class;
204   GstElementClass *gstelement_class;
205
206   gobject_class = (GObjectClass *) klass;
207   gstelement_class = (GstElementClass *) klass;
208
209   parent_class = g_type_class_peek_parent (klass);
210
211   gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rmdemux_change_state);
212
213   GST_DEBUG_CATEGORY_INIT (rmdemux_debug, "rmdemux",
214       0, "Demuxer for Realmedia streams");
215
216   gobject_class->finalize = gst_rmdemux_finalize;
217 }
218
219 static void
220 gst_rmdemux_finalize (GObject * object)
221 {
222   GstRMDemux *rmdemux = GST_RMDEMUX (object);
223
224   if (rmdemux->adapter) {
225     g_object_unref (rmdemux->adapter);
226     rmdemux->adapter = NULL;
227   }
228
229   GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
230 }
231
232 static void
233 gst_rmdemux_init (GstRMDemux * rmdemux)
234 {
235   rmdemux->sinkpad =
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));
247
248   gst_element_add_pad (GST_ELEMENT (rmdemux), rmdemux->sinkpad);
249
250   rmdemux->adapter = gst_adapter_new ();
251   rmdemux->first_ts = GST_CLOCK_TIME_NONE;
252   rmdemux->need_newsegment = TRUE;
253 }
254
255 static gboolean
256 gst_rmdemux_sink_event (GstPad * pad, GstEvent * event)
257 {
258   GstRMDemux *rmdemux;
259   gboolean ret;
260
261   rmdemux = GST_RMDEMUX (gst_pad_get_parent (pad));
262
263   GST_LOG_OBJECT (pad, "%s event", GST_EVENT_TYPE_NAME (event));
264
265   switch (GST_EVENT_TYPE (event)) {
266     case GST_EVENT_NEWSEGMENT:
267       gst_event_unref (event);
268       ret = TRUE;
269       break;
270     default:
271       ret = gst_pad_event_default (pad, event);
272       break;
273   }
274
275   gst_object_unref (rmdemux);
276   return ret;
277 }
278
279 static gboolean
280 gst_rmdemux_src_event (GstPad * pad, GstEvent * event)
281 {
282   gboolean ret = TRUE;
283
284   GstRMDemux *rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
285
286   GST_LOG_OBJECT (rmdemux, "handling src event");
287
288   switch (GST_EVENT_TYPE (event)) {
289     case GST_EVENT_SEEK:
290     {
291       gboolean running;
292
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) {
298         ret = FALSE;
299         GST_DEBUG ("seek on non seekable stream");
300         goto done_unref;
301       }
302
303       GST_OBJECT_LOCK (rmdemux);
304       /* check if we can do the seek now */
305       running = rmdemux->running;
306       GST_OBJECT_UNLOCK (rmdemux);
307
308       /* now do the seek */
309       if (running) {
310         ret = gst_rmdemux_perform_seek (rmdemux, event);
311       } else
312         ret = TRUE;
313
314       gst_event_unref (event);
315       break;
316     }
317     default:
318       GST_LOG_OBJECT (rmdemux, "Event on src: type=%d", GST_EVENT_TYPE (event));
319       ret = gst_pad_event_default (pad, event);
320       break;
321   }
322
323   return ret;
324
325 done_unref:
326   GST_DEBUG ("error handling event");
327   gst_event_unref (event);
328   return ret;
329 }
330
331 /* Validate that this looks like a reasonable point to seek to */
332 static gboolean
333 gst_rmdemux_validate_offset (GstRMDemux * rmdemux)
334 {
335   GstBuffer *buffer;
336   GstFlowReturn flowret;
337   guint16 version, length;
338   gboolean ret = TRUE;
339
340   flowret = gst_pad_pull_range (rmdemux->sinkpad, rmdemux->offset, 4, &buffer);
341
342   if (flowret != GST_FLOW_OK) {
343     GST_DEBUG_OBJECT (rmdemux, "Failed to pull data at offset %d",
344         rmdemux->offset);
345     return FALSE;
346   }
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
354    */
355
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",
359         (int) version);
360     ret = FALSE;
361   }
362
363   length = RMDEMUX_GUINT16_GET (GST_BUFFER_DATA (buffer) + 2);
364   /* TODO: Also check against total stream length */
365   if (length < 4) {
366     GST_DEBUG_OBJECT (rmdemux, "Expected length >= 4, got %d", (int) length);
367     ret = FALSE;
368   }
369
370   if (ret) {
371     rmdemux->offset += 4;
372     gst_adapter_clear (rmdemux->adapter);
373     gst_adapter_push (rmdemux->adapter, buffer);
374   } else {
375     GST_WARNING_OBJECT (rmdemux, "Failed to validate seek offset at %d",
376         rmdemux->offset);
377   }
378
379   return ret;
380 }
381
382 static gboolean
383 find_seek_offset_bytes (GstRMDemux * rmdemux, guint target)
384 {
385   int i;
386   GSList *cur;
387   gboolean ret = FALSE;
388
389   if (target < 0)
390     return FALSE;
391
392   for (cur = rmdemux->streams; cur; cur = cur->next) {
393     GstRMDemuxStream *stream = cur->data;
394
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;
403         ret = TRUE;
404         break;
405       }
406     }
407   }
408   return ret;
409 }
410
411 static gboolean
412 find_seek_offset_time (GstRMDemux * rmdemux, GstClockTime time)
413 {
414   int i, n_stream;
415   gboolean ret = FALSE;
416   GSList *cur;
417   GstClockTime earliest = GST_CLOCK_TIME_NONE;
418
419   n_stream = 0;
420   for (cur = rmdemux->streams; cur; cur = cur->next, n_stream++) {
421     GstRMDemuxStream *stream = cur->data;
422
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;
430
431         /* If it's also the earliest timestamp we've seen of all streams, then
432          * that's our target!
433          */
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));
443         }
444
445         ret = TRUE;
446
447         break;
448       }
449     }
450   }
451   return ret;
452 }
453
454 static gboolean
455 gst_rmdemux_perform_seek (GstRMDemux * rmdemux, GstEvent * event)
456 {
457   gboolean validated;
458   gboolean ret = TRUE;
459   gboolean flush, accurate;
460   GstFormat format;
461   gdouble rate;
462   GstSeekFlags flags;
463   GstSeekType cur_type, stop_type;
464   gint64 cur, stop;
465   gboolean update;
466
467   if (event) {
468     GST_DEBUG_OBJECT (rmdemux, "seek with event");
469
470     gst_event_parse_seek (event, &rate, &format, &flags,
471         &cur_type, &cur, &stop_type, &stop);
472
473     /* we can only seek on time */
474     if (format != GST_FORMAT_TIME) {
475       GST_DEBUG_OBJECT (rmdemux, "can only seek on TIME");
476       goto error;
477     }
478     /* cannot yet do backwards playback */
479     if (rate <= 0.0) {
480       GST_DEBUG_OBJECT (rmdemux, "can only seek with positive rate, not %lf",
481           rate);
482       goto error;
483     }
484   } else {
485     GST_DEBUG_OBJECT (rmdemux, "seek without event");
486
487     flags = 0;
488     rate = 1.0;
489   }
490
491   GST_DEBUG_OBJECT (rmdemux, "seek, rate %g", rate);
492
493   flush = flags & GST_SEEK_FLAG_FLUSH;
494   accurate = flags & GST_SEEK_FLAG_ACCURATE;
495
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. */
498   if (flush) {
499     gst_pad_push_event (rmdemux->sinkpad, gst_event_new_flush_start ());
500     gst_rmdemux_send_event (rmdemux, gst_event_new_flush_start ());
501   } else {
502     gst_pad_pause_task (rmdemux->sinkpad);
503   }
504
505   GST_LOG_OBJECT (rmdemux, "Done starting flushes");
506
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
509    * forever. */
510   GST_PAD_STREAM_LOCK (rmdemux->sinkpad);
511
512   GST_LOG_OBJECT (rmdemux, "Took streamlock");
513
514   /* close current segment first */
515   if (rmdemux->segment_running && !flush) {
516     GstEvent *newseg;
517
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);
521
522     gst_rmdemux_send_event (rmdemux, newseg);
523   }
524
525   if (event) {
526     gst_segment_set_seek (&rmdemux->segment, rate, format, flags,
527         cur_type, cur, stop_type, stop, &update);
528   }
529
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));
533
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 ());
537
538   GST_LOG_OBJECT (rmdemux, "Pushed FLUSH_STOP event");
539
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.
542    *
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
547    * error.
548    */
549   if (!find_seek_offset_time (rmdemux, rmdemux->segment.last_stop)) {
550     GST_LOG_OBJECT (rmdemux, "Failed to find seek offset by time");
551     ret = FALSE;
552     goto done;
553   }
554
555   GST_LOG_OBJECT (rmdemux, "Validating offset %u", rmdemux->offset);
556   validated = gst_rmdemux_validate_offset (rmdemux);
557   while (!validated) {
558     GST_INFO_OBJECT (rmdemux, "Failed to validate offset at %u",
559         rmdemux->offset);
560     if (!find_seek_offset_bytes (rmdemux, rmdemux->offset - 1)) {
561       ret = FALSE;
562       goto done;
563     }
564     validated = gst_rmdemux_validate_offset (rmdemux);
565   }
566
567   GST_LOG_OBJECT (rmdemux, "Found final offset. Excellent!");
568
569   /* now we have a new position, prepare for streaming again */
570   {
571     /* Reset the demuxer state */
572     rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
573
574     if (flush)
575       gst_rmdemux_send_event (rmdemux, gst_event_new_flush_stop ());
576
577     /* must send newsegment event from streaming thread, so just set flag */
578     rmdemux->need_newsegment = TRUE;
579
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));
585     }
586
587     /* restart our task since it might have been stopped when we did the 
588      * flush. */
589     gst_pad_start_task (rmdemux->sinkpad, (GstTaskFunction) gst_rmdemux_loop,
590         rmdemux->sinkpad);
591   }
592
593 done:
594   /* streaming can continue now */
595   GST_PAD_STREAM_UNLOCK (rmdemux->sinkpad);
596
597   return TRUE;
598
599 error:
600   {
601     GST_DEBUG_OBJECT (rmdemux, "seek failed");
602     return FALSE;
603   }
604 }
605
606
607 static gboolean
608 gst_rmdemux_src_query (GstPad * pad, GstQuery * query)
609 {
610   gboolean res = FALSE;
611   GstRMDemux *rmdemux;
612
613   rmdemux = GST_RMDEMUX (gst_pad_get_parent (pad));
614
615   switch (GST_QUERY_TYPE (query)) {
616     case GST_QUERY_POSITION:
617       GST_DEBUG_OBJECT (rmdemux, "Position query: no idea from demuxer!");
618       break;
619     case GST_QUERY_DURATION:{
620       GstFormat fmt;
621
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));
629           res = TRUE;
630         }
631         GST_OBJECT_UNLOCK (rmdemux);
632       }
633       break;
634     }
635     case GST_QUERY_SEEKING:{
636       GstFormat fmt;
637
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);
644           res = TRUE;
645         }
646         GST_OBJECT_UNLOCK (rmdemux);
647       }
648       break;
649     }
650     default:
651       res = gst_pad_query_default (pad, query);
652       break;
653   }
654
655   gst_object_unref (rmdemux);
656   return res;
657 }
658
659 static const GstQueryType *
660 gst_rmdemux_src_query_types (GstPad * pad)
661 {
662   static const GstQueryType query_types[] = {
663     GST_QUERY_POSITION,
664     GST_QUERY_DURATION,
665     GST_QUERY_SEEKING,
666     0
667   };
668
669   return query_types;
670 }
671
672 static void
673 gst_rmdemux_reset (GstRMDemux * rmdemux)
674 {
675   GSList *cur;
676
677   GST_OBJECT_LOCK (rmdemux);
678   rmdemux->running = FALSE;
679   GST_OBJECT_UNLOCK (rmdemux);
680
681   for (cur = rmdemux->streams; cur; cur = cur->next) {
682     GstRMDemuxStream *stream = cur->data;
683
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);
691     g_free (stream);
692   }
693   g_slist_free (rmdemux->streams);
694   rmdemux->streams = NULL;
695   rmdemux->n_audio_streams = 0;
696   rmdemux->n_video_streams = 0;
697
698   gst_adapter_clear (rmdemux->adapter);
699   rmdemux->state = RMDEMUX_STATE_HEADER;
700   rmdemux->have_pads = FALSE;
701
702   gst_segment_init (&rmdemux->segment, GST_FORMAT_UNDEFINED);
703   rmdemux->first_ts = GST_CLOCK_TIME_NONE;
704   rmdemux->need_newsegment = TRUE;
705 }
706
707 static GstStateChangeReturn
708 gst_rmdemux_change_state (GstElement * element, GstStateChange transition)
709 {
710   GstRMDemux *rmdemux = GST_RMDEMUX (element);
711   GstStateChangeReturn res;
712
713   switch (transition) {
714     case GST_STATE_CHANGE_NULL_TO_READY:
715       break;
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;
721       break;
722     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
723       break;
724     default:
725       break;
726   }
727
728   res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
729
730   switch (transition) {
731     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
732       break;
733     case GST_STATE_CHANGE_PAUSED_TO_READY:{
734       gst_rmdemux_reset (rmdemux);
735       break;
736     }
737     case GST_STATE_CHANGE_READY_TO_NULL:
738       break;
739     default:
740       break;
741   }
742
743   return res;
744 }
745
746 /* this function is called when the pad is activated and should start
747  * processing data.
748  *
749  * We check if we can do random access to decide if we work push or
750  * pull based.
751  */
752 static gboolean
753 gst_rmdemux_sink_activate (GstPad * sinkpad)
754 {
755   if (gst_pad_check_pull_range (sinkpad)) {
756     return gst_pad_activate_pull (sinkpad, TRUE);
757   } else {
758     return gst_pad_activate_push (sinkpad, TRUE);
759   }
760 }
761
762 /* this function gets called when we activate ourselves in push mode.
763  * We cannot seek (ourselves) in the stream */
764 static gboolean
765 gst_rmdemux_sink_activate_push (GstPad * pad, gboolean active)
766 {
767   GstRMDemux *rmdemux;
768
769   rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
770
771   GST_DEBUG_OBJECT (rmdemux, "activate_push");
772
773   rmdemux->seekable = FALSE;
774
775   return TRUE;
776 }
777
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 */
781 static gboolean
782 gst_rmdemux_sink_activate_pull (GstPad * pad, gboolean active)
783 {
784   GstRMDemux *rmdemux;
785
786   rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
787
788   GST_DEBUG_OBJECT (rmdemux, "activate_pull");
789
790   if (active) {
791     rmdemux->seekable = TRUE;
792     rmdemux->offset = 0;
793     rmdemux->loop_state = RMDEMUX_LOOP_STATE_HEADER;
794     rmdemux->data_offset = G_MAXUINT;
795
796     return gst_pad_start_task (pad, (GstTaskFunction) gst_rmdemux_loop, pad);
797   } else {
798     return gst_pad_stop_task (pad);
799   }
800 }
801
802 /* random access mode - just pass over to our chain function */
803 static void
804 gst_rmdemux_loop (GstPad * pad)
805 {
806   GstRMDemux *rmdemux;
807   GstBuffer *buffer;
808   GstFlowReturn ret = GST_FLOW_OK;
809   guint size;
810
811   rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
812
813   GST_LOG_OBJECT (rmdemux, "loop with state=%d and offset=0x%x",
814       rmdemux->loop_state, rmdemux->offset);
815
816   switch (rmdemux->state) {
817     case RMDEMUX_STATE_HEADER:
818       size = HEADER_SIZE;
819       break;
820     case RMDEMUX_STATE_HEADER_DATA:
821       size = DATA_SIZE;
822       break;
823     case RMDEMUX_STATE_DATA_PACKET:
824       size = rmdemux->avg_packet_size;
825       break;
826     case RMDEMUX_STATE_EOS:
827       GST_LOG_OBJECT (rmdemux, "At EOS, pausing task");
828       ret = GST_FLOW_UNEXPECTED;
829       goto need_pause;
830     default:
831       GST_LOG_OBJECT (rmdemux, "Default: requires %d bytes (state is %d)",
832           (int) rmdemux->size, rmdemux->state);
833       size = rmdemux->size;
834   }
835
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);
846       return;
847     } else {
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));
851       goto need_pause;
852     }
853   }
854
855   size = GST_BUFFER_SIZE (buffer);
856
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",
861         rmdemux->offset);
862     goto need_pause;
863   }
864
865   rmdemux->offset += size;
866
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;
873       }
874       break;
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);
884         } else {
885           /* Get the next index */
886           rmdemux->offset = rmdemux->index_offset;
887         }
888       }
889       break;
890     case RMDEMUX_LOOP_STATE_DATA:
891       break;
892   }
893
894   return;
895
896   /* ERRORS */
897 need_pause:
898   {
899     const gchar *reason = gst_flow_get_name (ret);
900
901     GST_LOG_OBJECT (rmdemux, "pausing task, reason %s", reason);
902     rmdemux->segment_running = FALSE;
903     gst_pad_pause_task (rmdemux->sinkpad);
904
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) {
909           gint64 stop;
910
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;
915
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));
920         } else {
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 ());
924         }
925       } else {
926         GST_ELEMENT_ERROR (rmdemux, STREAM, FAILED,
927             (NULL), ("stream stopped, reason %s", reason));
928         gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
929       }
930     }
931     return;
932   }
933 }
934
935 static gboolean
936 gst_rmdemux_fourcc_isplausible (guint32 fourcc)
937 {
938   int i;
939
940   for (i = 0; i < 4; i++) {
941     if (!isprint ((int) ((unsigned char *) (&fourcc))[i])) {
942       return FALSE;
943     }
944   }
945   return TRUE;
946 }
947
948 static GstFlowReturn
949 gst_rmdemux_chain (GstPad * pad, GstBuffer * buffer)
950 {
951   GstFlowReturn ret = GST_FLOW_OK;
952   const guint8 *data;
953   guint16 version;
954
955   GstRMDemux *rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad));
956
957   GST_LOG_OBJECT (rmdemux, "Chaining buffer of size %d",
958       GST_BUFFER_SIZE (buffer));
959
960   gst_adapter_push (rmdemux->adapter, buffer);
961
962   while (TRUE) {
963     GST_LOG_OBJECT (rmdemux, "looping in chain");
964     switch (rmdemux->state) {
965       case RMDEMUX_STATE_HEADER:
966       {
967         if (gst_adapter_available (rmdemux->adapter) < HEADER_SIZE)
968           goto unlock;
969
970         data = gst_adapter_peek (rmdemux->adapter, HEADER_SIZE);
971
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);
975
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 
981            * happen. */
982           GST_WARNING_OBJECT (rmdemux, "Bogus looking header, unprintable "
983               "FOURCC");
984           gst_adapter_flush (rmdemux->adapter, 4);
985
986           break;
987         }
988
989         GST_LOG_OBJECT (rmdemux, "header found with object_id=%"
990             GST_FOURCC_FORMAT
991             " size=%08x object_version=%d",
992             GST_FOURCC_ARGS (rmdemux->object_id), rmdemux->size,
993             rmdemux->object_version);
994
995         gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
996
997         switch (rmdemux->object_id) {
998           case GST_MAKE_FOURCC ('.', 'R', 'M', 'F'):
999             rmdemux->state = RMDEMUX_STATE_HEADER_RMF;
1000             break;
1001           case GST_MAKE_FOURCC ('P', 'R', 'O', 'P'):
1002             rmdemux->state = RMDEMUX_STATE_HEADER_PROP;
1003             break;
1004           case GST_MAKE_FOURCC ('M', 'D', 'P', 'R'):
1005             rmdemux->state = RMDEMUX_STATE_HEADER_MDPR;
1006             break;
1007           case GST_MAKE_FOURCC ('I', 'N', 'D', 'X'):
1008             rmdemux->state = RMDEMUX_STATE_HEADER_INDX;
1009             break;
1010           case GST_MAKE_FOURCC ('D', 'A', 'T', 'A'):
1011             rmdemux->state = RMDEMUX_STATE_HEADER_DATA;
1012             break;
1013           case GST_MAKE_FOURCC ('C', 'O', 'N', 'T'):
1014             rmdemux->state = RMDEMUX_STATE_HEADER_CONT;
1015             break;
1016           default:
1017             rmdemux->state = RMDEMUX_STATE_HEADER_UNKNOWN;
1018             break;
1019         }
1020         break;
1021       }
1022       case RMDEMUX_STATE_HEADER_UNKNOWN:
1023       {
1024         if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1025           goto unlock;
1026
1027         GST_WARNING_OBJECT (rmdemux, "Unknown object_id %" GST_FOURCC_FORMAT,
1028             GST_FOURCC_ARGS (rmdemux->object_id));
1029
1030         gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1031         rmdemux->state = RMDEMUX_STATE_HEADER;
1032         break;
1033       }
1034       case RMDEMUX_STATE_HEADER_RMF:
1035       {
1036         if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1037           goto unlock;
1038
1039         if ((rmdemux->object_version == 0) || (rmdemux->object_version == 1)) {
1040           data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1041
1042           gst_rmdemux_parse__rmf (rmdemux, data, rmdemux->size);
1043         }
1044
1045         gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1046         rmdemux->state = RMDEMUX_STATE_HEADER;
1047         break;
1048       }
1049       case RMDEMUX_STATE_HEADER_PROP:
1050       {
1051         if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1052           goto unlock;
1053         data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1054
1055         gst_rmdemux_parse_prop (rmdemux, data, rmdemux->size);
1056
1057         gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1058         rmdemux->state = RMDEMUX_STATE_HEADER;
1059         break;
1060       }
1061       case RMDEMUX_STATE_HEADER_MDPR:
1062       {
1063         if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1064           goto unlock;
1065         data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1066
1067         gst_rmdemux_parse_mdpr (rmdemux, data, rmdemux->size);
1068
1069         gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1070         rmdemux->state = RMDEMUX_STATE_HEADER;
1071         break;
1072       }
1073       case RMDEMUX_STATE_HEADER_CONT:
1074       {
1075         if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1076           goto unlock;
1077         data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1078
1079         gst_rmdemux_parse_cont (rmdemux, data, rmdemux->size);
1080
1081         gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1082         rmdemux->state = RMDEMUX_STATE_HEADER;
1083         break;
1084       }
1085       case RMDEMUX_STATE_HEADER_DATA:
1086       {
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;
1092         }
1093
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)
1099           goto unlock;
1100
1101         data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1102
1103         gst_rmdemux_parse_data (rmdemux, data, rmdemux->size);
1104
1105         gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1106
1107         rmdemux->state = RMDEMUX_STATE_DATA_PACKET;
1108         break;
1109       }
1110       case RMDEMUX_STATE_HEADER_INDX:
1111       {
1112         if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1113           goto unlock;
1114         data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1115
1116         rmdemux->size = gst_rmdemux_parse_indx (rmdemux, data, rmdemux->size);
1117
1118         /* Only flush the header */
1119         gst_adapter_flush (rmdemux->adapter, HEADER_SIZE);
1120
1121         rmdemux->state = RMDEMUX_STATE_INDX_DATA;
1122         break;
1123       }
1124       case RMDEMUX_STATE_INDX_DATA:
1125       {
1126         /* There's not always an data to get... */
1127         if (rmdemux->size > 0) {
1128           if (gst_adapter_available (rmdemux->adapter) < rmdemux->size)
1129             goto unlock;
1130
1131           data = gst_adapter_peek (rmdemux->adapter, rmdemux->size);
1132
1133           gst_rmdemux_parse_indx_data (rmdemux, data, rmdemux->size);
1134
1135           gst_adapter_flush (rmdemux->adapter, rmdemux->size);
1136         }
1137
1138         rmdemux->state = RMDEMUX_STATE_HEADER;
1139         break;
1140       }
1141       case RMDEMUX_STATE_DATA_PACKET:
1142       {
1143         if (gst_adapter_available (rmdemux->adapter) < 2)
1144           goto unlock;
1145
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);
1149
1150         if (version == 0 || version == 1) {
1151           guint16 length;
1152
1153           if (gst_adapter_available (rmdemux->adapter) < 4)
1154             goto unlock;
1155           data = gst_adapter_peek (rmdemux->adapter, 4);
1156
1157           length = RMDEMUX_GUINT16_GET (data + 2);
1158           if (length < 4) {
1159             /* Invalid, just drop it */
1160             gst_adapter_flush (rmdemux->adapter, 4);
1161           } else {
1162             if (gst_adapter_available (rmdemux->adapter) < length)
1163               goto unlock;
1164             data = gst_adapter_peek (rmdemux->adapter, length);
1165
1166             ret =
1167                 gst_rmdemux_parse_packet (rmdemux, data + 4, version,
1168                 length - 4);
1169             rmdemux->chunk_index++;
1170
1171             gst_adapter_flush (rmdemux->adapter, length);
1172           }
1173
1174           if (rmdemux->chunk_index == rmdemux->n_chunks || length == 0)
1175             rmdemux->state = RMDEMUX_STATE_HEADER;
1176         } else {
1177           /* Stream done */
1178           gst_adapter_flush (rmdemux->adapter, 2);
1179
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;
1184           } else
1185             rmdemux->state = RMDEMUX_STATE_HEADER;
1186         }
1187         break;
1188       }
1189       case RMDEMUX_STATE_EOS:
1190         gst_rmdemux_send_event (rmdemux, gst_event_new_eos ());
1191         goto unlock;
1192       default:
1193         GST_WARNING_OBJECT (rmdemux, "Unhandled state %d", rmdemux->state);
1194         goto unlock;
1195     }
1196   }
1197
1198 unlock:
1199   return ret;
1200 }
1201
1202 static GstRMDemuxStream *
1203 gst_rmdemux_get_stream_by_id (GstRMDemux * rmdemux, int id)
1204 {
1205   GSList *cur;
1206
1207   for (cur = rmdemux->streams; cur; cur = cur->next) {
1208     GstRMDemuxStream *stream = cur->data;
1209
1210     if (stream->id == id) {
1211       return stream;
1212     }
1213   }
1214
1215   return NULL;
1216 }
1217
1218 static void
1219 gst_rmdemux_send_event (GstRMDemux * rmdemux, GstEvent * event)
1220 {
1221   GSList *cur;
1222
1223   for (cur = rmdemux->streams; cur; cur = cur->next) {
1224     GstRMDemuxStream *stream = cur->data;
1225
1226     GST_DEBUG_OBJECT (rmdemux, "Pushing %s event on pad %s",
1227         GST_EVENT_TYPE_NAME (event), GST_PAD_NAME (stream->pad));
1228
1229     gst_event_ref (event);
1230     gst_pad_push_event (stream->pad, event);
1231   }
1232   gst_event_unref (event);
1233 }
1234
1235 static void
1236 gst_rmdemux_add_stream (GstRMDemux * rmdemux, GstRMDemuxStream * stream)
1237 {
1238   GstCaps *stream_caps = NULL;
1239   const gchar *codec_tag = NULL;
1240   const gchar *codec_name = NULL;
1241   int version = 0;
1242
1243   if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) {
1244     char *name = g_strdup_printf ("video_%02d", rmdemux->n_video_streams);
1245
1246     stream->pad =
1247         gst_pad_new_from_static_template (&gst_rmdemux_videosrc_template, name);
1248     g_free (name);
1249
1250     codec_tag = GST_TAG_VIDEO_CODEC;
1251
1252     switch (stream->fourcc) {
1253       case GST_RM_VDO_RV10:
1254         codec_name = "Real Video 1.0";
1255         version = 1;
1256         break;
1257       case GST_RM_VDO_RV20:
1258         codec_name = "Real Video 2.0";
1259         version = 2;
1260         break;
1261       case GST_RM_VDO_RV30:
1262         codec_name = "Real Video 3.0";
1263         version = 3;
1264         break;
1265       case GST_RM_VDO_RV40:
1266         codec_name = "Real Video 4.0";
1267         version = 4;
1268         break;
1269       default:
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);
1275     }
1276
1277     if (version) {
1278       stream_caps =
1279           gst_caps_new_simple ("video/x-pn-realvideo", "rmversion", G_TYPE_INT,
1280           (int) version,
1281           "format", G_TYPE_INT,
1282           (int) stream->format,
1283           "subformat", G_TYPE_INT, (int) stream->subformat, NULL);
1284     }
1285
1286     if (stream_caps) {
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);
1292     }
1293     rmdemux->n_video_streams++;
1294
1295   } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) {
1296     char *name = g_strdup_printf ("audio_%02d", rmdemux->n_audio_streams);
1297
1298     stream->pad =
1299         gst_pad_new_from_static_template (&gst_rmdemux_audiosrc_template, name);
1300     GST_LOG_OBJECT (rmdemux, "Created audio pad \"%s\"", name);
1301     g_free (name);
1302
1303     codec_tag = GST_TAG_AUDIO_CODEC;
1304
1305     switch (stream->fourcc) {
1306         /* Older RealAudio Codecs */
1307       case GST_RM_AUD_14_4:
1308         codec_name = "Real Audio 14.4kbps";
1309         version = 1;
1310         break;
1311
1312       case GST_RM_AUD_28_8:
1313         codec_name = "Real Audio 28.8kbps";
1314         version = 2;
1315         break;
1316
1317         /* DolbyNet (Dolby AC3, low bitrate) */
1318       case GST_RM_AUD_DNET:
1319         codec_name = "AC-3 audio";
1320         stream_caps =
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;
1326         break;
1327
1328         /* RealAudio 10 (AAC) */
1329       case GST_RM_AUD_RAAC:
1330         codec_name = "Real Audio 10 (AAC)";
1331         version = 10;
1332         break;
1333
1334         /* MPEG-4 based */
1335       case GST_RM_AUD_RACP:
1336         /* FIXME: codec_name = */
1337         stream_caps =
1338             gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT,
1339             (int) 4, NULL);
1340         break;
1341
1342         /* Sony ATRAC3 */
1343       case GST_RM_AUD_ATRC:
1344         codec_name = "Sony ATRAC3";
1345         stream_caps = gst_caps_new_simple ("audio/x-vnd.sony.atrac3", NULL);
1346         break;
1347
1348         /* RealAudio G2 audio */
1349       case GST_RM_AUD_COOK:
1350         codec_name = "Real Audio G2 (Cook)";
1351         version = 8;
1352         stream->needs_descrambling = TRUE;
1353         stream->subpackets_needed = stream->height;
1354         stream->subpackets = NULL;
1355         break;
1356
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);
1362         break;
1363
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);
1368         break;
1369
1370       default:
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);
1376         break;
1377     }
1378
1379     if (version) {
1380       stream_caps =
1381           gst_caps_new_simple ("audio/x-pn-realaudio", "raversion", G_TYPE_INT,
1382           (int) version, NULL);
1383     }
1384
1385     if (stream_caps) {
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);
1394     }
1395     rmdemux->n_audio_streams++;
1396   } else {
1397     GST_WARNING_OBJECT (rmdemux, "not adding stream of type %d, freeing it",
1398         stream->subtype);
1399     g_free (stream);
1400     goto beach;
1401   }
1402
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));
1407
1408   GST_LOG ("stream->pad = %p, stream_caps = %" GST_PTR_FORMAT, stream->pad,
1409       stream_caps);
1410
1411   if (stream->pad && stream_caps) {
1412
1413     GST_LOG_OBJECT (rmdemux, "%d bytes of extra data for stream %s",
1414         stream->extra_data_size, GST_PAD_NAME (stream->pad));
1415
1416     /* add codec_data if there is any */
1417     if (stream->extra_data_size > 0) {
1418       GstBuffer *buffer;
1419
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);
1423
1424       gst_caps_set_simple (stream_caps, "codec_data", GST_TYPE_BUFFER,
1425           buffer, NULL);
1426
1427       gst_buffer_unref (buffer);
1428     }
1429
1430     gst_pad_use_fixed_caps (stream->pad);
1431
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));
1439
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);
1444
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);
1450     }
1451   }
1452
1453 beach:
1454
1455   if (stream_caps)
1456     gst_caps_unref (stream_caps);
1457 }
1458
1459 static int
1460 re_skip_pascal_string (const guint8 * ptr)
1461 {
1462   int length;
1463
1464   length = ptr[0];
1465
1466   return length + 1;
1467 }
1468
1469 static void
1470 gst_rmdemux_parse__rmf (GstRMDemux * rmdemux, const guint8 * data, int length)
1471 {
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));
1474 }
1475
1476 static void
1477 gst_rmdemux_parse_prop (GstRMDemux * rmdemux, const guint8 * data, int length)
1478 {
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);
1487
1488   GST_LOG_OBJECT (rmdemux, "duration: %d", RMDEMUX_GUINT32_GET (data + 20));
1489   rmdemux->duration = RMDEMUX_GUINT32_GET (data + 20) * GST_MSECOND;
1490
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));
1500 }
1501
1502 static void
1503 gst_rmdemux_parse_mdpr (GstRMDemux * rmdemux, const guint8 * data, int length)
1504 {
1505   GstRMDemuxStream *stream;
1506   char *stream1_type_string;
1507   char *stream2_type_string;
1508   guint str_len = 0;
1509   int stream_type;
1510   int offset;
1511
1512   /* re_hexdump_bytes ((guint8 *) data, length, 0); */
1513
1514   stream = g_new0 (GstRMDemuxStream, 1);
1515
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);
1521
1522   offset = 30;
1523   stream_type = GST_RMDEMUX_STREAM_UNKNOWN;
1524   stream1_type_string = gst_rm_utils_read_string8 (data + offset,
1525       length - offset, &str_len);
1526   offset += str_len;
1527   stream2_type_string = gst_rm_utils_read_string8 (data + offset,
1528       length - offset, &str_len);
1529   offset += str_len;
1530
1531   /* stream1_type_string for audio and video stream is a "put_whatever_you_want" field :
1532    * observed values :
1533    * - "[The ]Video/Audio Stream" (File produced by an official Real encoder)
1534    * - "RealVideoPremierePlugIn-VIDEO/AUDIO" (File produced by Abobe Premiere)
1535    *
1536    * so, we should not rely on it to know which stream type it is
1537    */
1538
1539   GST_LOG_OBJECT (rmdemux, "stream type: %s", stream1_type_string);
1540   GST_LOG_OBJECT (rmdemux, "MIME type=%s", stream2_type_string);
1541
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;
1561   } else {
1562     stream_type = GST_RMDEMUX_STREAM_UNKNOWN;
1563     GST_WARNING_OBJECT (rmdemux, "unknown stream type \"%s\",\"%s\"",
1564         stream1_type_string, stream2_type_string);
1565   }
1566   g_free (stream1_type_string);
1567   g_free (stream2_type_string);
1568
1569   offset += 4;
1570
1571   stream->subtype = stream_type;
1572   switch (stream_type) {
1573
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...
1586        */
1587       {
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;
1593         } else {
1594           stream->framerate_numerator = (gint) numerator;
1595           stream->framerate_denominator = 65536;
1596         }
1597       }
1598
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);
1606       break;
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) {
1611         case 3:
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;
1617           stream->flavor = 1;
1618           stream->leaf_size = 0;
1619           stream->height = 0;
1620           break;
1621         case 4:
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;
1636           } else {
1637             GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1638             stream->extra_data_size = 0;
1639           }
1640           break;
1641         case 5:
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;
1656           } else {
1657             GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk");
1658             stream->extra_data_size = 0;
1659           }
1660           break;
1661         default:{
1662           GST_WARNING_OBJECT (rmdemux, "Unhandled audio stream version %d",
1663               stream->version);
1664           break;
1665         }
1666       }
1667
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);
1672
1673       break;
1674     }
1675     case GST_RMDEMUX_STREAM_FILEINFO:
1676     {
1677       int element_nb;
1678
1679       /* Length of this section */
1680       GST_DEBUG_OBJECT (rmdemux, "length2: 0x%08x",
1681           RMDEMUX_GUINT32_GET (data + offset));
1682       offset += 4;
1683
1684       /* Unknown : 00 00 00 00 */
1685       offset += 4;
1686
1687       /* Number of variables that would follow (loop iterations) */
1688       element_nb = RMDEMUX_GUINT32_GET (data + offset);
1689       offset += 4;
1690
1691       while (element_nb) {
1692         /* Category Id : 00 00 00 XX 00 00 */
1693         offset += 6;
1694
1695         /* Variable Name */
1696         offset += re_skip_pascal_string (data + offset);
1697
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 */
1701         offset += 5;
1702
1703         /* Variable Value */
1704         offset += re_skip_pascal_string (data + offset);
1705
1706         element_nb--;
1707       }
1708     }
1709       break;
1710     case GST_RMDEMUX_STREAM_UNKNOWN:
1711     default:
1712       break;
1713   }
1714
1715   gst_rmdemux_add_stream (rmdemux, stream);
1716 }
1717
1718 static guint
1719 gst_rmdemux_parse_indx (GstRMDemux * rmdemux, const guint8 * data, int length)
1720 {
1721   int n;
1722   int id;
1723
1724   n = RMDEMUX_GUINT32_GET (data);
1725   id = RMDEMUX_GUINT16_GET (data + 4);
1726   rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 6);
1727
1728   GST_DEBUG_OBJECT (rmdemux, "Number of indices=%d Stream ID=%d length=%d", n,
1729       id, length);
1730
1731   /* Point to the next index_stream */
1732   rmdemux->index_stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
1733
1734   /* Return the length of the index */
1735   return 14 * n;
1736 }
1737
1738 static void
1739 gst_rmdemux_parse_indx_data (GstRMDemux * rmdemux, const guint8 * data,
1740     int length)
1741 {
1742   int i;
1743   int n;
1744   GstRMDemuxIndex *index;
1745
1746   /* The number of index records */
1747   n = length / 14;
1748
1749   if (rmdemux->index_stream == NULL)
1750     return;
1751
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");
1756     return;
1757   }
1758
1759   index = g_malloc (sizeof (GstRMDemuxIndex) * n);
1760   rmdemux->index_stream->index = index;
1761   rmdemux->index_stream->index_length = n;
1762
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);
1766
1767     GST_DEBUG_OBJECT (rmdemux, "Index found for timestamp=%f (at offset=%x)",
1768         gst_guint64_to_gdouble (index[i].timestamp) / GST_SECOND,
1769         index[i].offset);
1770     data += 14;
1771   }
1772 }
1773
1774 static void
1775 gst_rmdemux_parse_data (GstRMDemux * rmdemux, const guint8 * data, int length)
1776 {
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);
1782 }
1783
1784 static void
1785 gst_rmdemux_parse_cont (GstRMDemux * rmdemux, const guint8 * data, int length)
1786 {
1787   GstTagList *tags;
1788
1789   tags = gst_rm_utils_read_tags (data, length, gst_rm_utils_read_string16);
1790   if (tags) {
1791     gst_element_found_tags (GST_ELEMENT (rmdemux), tags);
1792   }
1793 }
1794
1795 static GstFlowReturn
1796 gst_rmdemux_combine_flows (GstRMDemux * rmdemux, GstRMDemuxStream * stream,
1797     GstFlowReturn ret)
1798 {
1799   GSList *cur;
1800
1801   /* store the value */
1802   stream->last_flow = ret;
1803
1804   /* if it's success we can return the value right away */
1805   if (GST_FLOW_IS_SUCCESS (ret))
1806     goto done;
1807
1808   /* any other error that is not-linked can be returned right
1809    * away */
1810   if (ret != GST_FLOW_NOT_LINKED)
1811     goto done;
1812
1813   for (cur = rmdemux->streams; cur; cur = cur->next) {
1814     GstRMDemuxStream *ostream = cur->data;
1815
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)
1820       goto done;
1821   }
1822   /* if we get here, all other pads were unlinked and we return
1823    * NOT_LINKED then */
1824 done:
1825   return ret;
1826 }
1827
1828 static void
1829 gst_rmdemux_stream_clear_cached_subpackets (GstRMDemux * rmdemux,
1830     GstRMDemuxStream * stream)
1831 {
1832   if (stream->subpackets == NULL || stream->subpackets->len == 0)
1833     return;
1834
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);
1839 }
1840
1841 static GstFlowReturn
1842 gst_rmdemux_descramble_cook_audio (GstRMDemux * rmdemux,
1843     GstRMDemuxStream * stream)
1844 {
1845   GstFlowReturn ret;
1846   GstBuffer *outbuf;
1847   guint packet_size = stream->packet_size;
1848   guint height = stream->subpackets->len;
1849   guint leaf_size = stream->leaf_size;
1850   guint p, x;
1851
1852   g_assert (stream->height == height);
1853
1854   GST_LOG ("packet_size = %u, leaf_size = %u, height= %u", packet_size,
1855       leaf_size, height);
1856
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);
1860
1861   if (ret != GST_FLOW_OK)
1862     goto done;
1863
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);
1867
1868     if (p == 0)
1869       GST_BUFFER_TIMESTAMP (outbuf) = GST_BUFFER_TIMESTAMP (b);
1870
1871     for (x = 0; x < packet_size / leaf_size; ++x) {
1872       guint idx;
1873
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;
1878     }
1879   }
1880
1881   /* some decoders, such as realaudiodec, need to be fed in packet units */
1882   for (p = 0; p < height; ++p) {
1883     GstBuffer *subbuf;
1884
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)
1889       break;
1890   }
1891
1892   gst_buffer_unref (outbuf);
1893
1894 done:
1895
1896   gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
1897
1898   return ret;
1899 }
1900
1901 static GstFlowReturn
1902 gst_rmdemux_descramble_dnet_audio (GstRMDemux * rmdemux,
1903     GstRMDemuxStream * stream)
1904 {
1905   GstBuffer *buf;
1906
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);
1910
1911   buf = gst_rm_utils_descramble_dnet_buffer (buf);
1912   return gst_pad_push (stream->pad, buf);
1913 }
1914
1915 static GstFlowReturn
1916 gst_rmdemux_handle_scrambled_packet (GstRMDemux * rmdemux,
1917     GstRMDemuxStream * stream, GstBuffer * buf, gboolean keyframe)
1918 {
1919   GstFlowReturn ret;
1920
1921   if (stream->subpackets == NULL)
1922     stream->subpackets = g_ptr_array_sized_new (stream->subpackets_needed);
1923
1924   GST_LOG ("Got subpacket %u/%u, len=%u, key=%d", stream->subpackets->len + 1,
1925       stream->subpackets_needed, GST_BUFFER_SIZE (buf), keyframe);
1926
1927   if (keyframe && stream->subpackets->len > 0) {
1928     gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream);
1929   }
1930
1931   g_ptr_array_add (stream->subpackets, buf);
1932
1933   if (stream->subpackets->len < stream->subpackets_needed)
1934     return GST_FLOW_OK;
1935
1936   g_assert (stream->subpackets->len >= 1);
1937
1938   switch (stream->fourcc) {
1939     case GST_RM_AUD_DNET:
1940       ret = gst_rmdemux_descramble_dnet_audio (rmdemux, stream);
1941       break;
1942     case GST_RM_AUD_COOK:
1943       ret = gst_rmdemux_descramble_cook_audio (rmdemux, stream);
1944       break;
1945     default:
1946       g_assert_not_reached ();
1947   }
1948
1949   return ret;
1950 }
1951
1952 static GstFlowReturn
1953 gst_rmdemux_parse_packet (GstRMDemux * rmdemux, const guint8 * data,
1954     guint16 version, guint16 length)
1955 {
1956   guint16 id;
1957   GstRMDemuxStream *stream;
1958   GstBuffer *buffer;
1959   guint16 packet_size;
1960   GstFlowReturn cret, ret;
1961   GstClockTime timestamp;
1962   gboolean key = FALSE;
1963
1964   id = RMDEMUX_GUINT16_GET (data);
1965   /* timestamp in Msec */
1966   timestamp = RMDEMUX_GUINT32_GET (data + 2) * GST_MSECOND;
1967
1968   gst_segment_set_last_stop (&rmdemux->segment, GST_FORMAT_TIME, timestamp);
1969
1970   GST_LOG_OBJECT (rmdemux, "Parsing a packet for stream=%d, timestamp=%"
1971       GST_TIME_FORMAT ", version=%d", id, GST_TIME_ARGS (timestamp), version);
1972
1973   /* skip stream_id and timestamp */
1974   data += 2 + 4;
1975   packet_size = length - (2 + 4);
1976
1977   /* skip other stuff */
1978   if (version == 0) {
1979     /* uint8 packet_group */
1980     /* uint8 flags        */
1981     key = ((GST_READ_UINT8 (data + 1) & 0x02) != 0);
1982     data += 2;
1983     packet_size -= 2;
1984   } else {
1985     /* uint16 asm_rule */
1986     /* uint8 asm_flags */
1987     data += 3;
1988     packet_size -= 3;
1989   }
1990
1991   stream = gst_rmdemux_get_stream_by_id (rmdemux, id);
1992   if (!stream || !stream->pad)
1993     goto unknown_stream;
1994
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;
1999   }
2000
2001   if (rmdemux->need_newsegment) {
2002     GstEvent *event;
2003
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);
2007
2008     GST_DEBUG_OBJECT (rmdemux, "sending NEWSEGMENT event, segment.start= %"
2009         GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start));
2010
2011     gst_rmdemux_send_event (rmdemux, event);
2012     rmdemux->need_newsegment = FALSE;
2013   }
2014
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;
2020   }
2021
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);
2026     cret = GST_FLOW_OK;
2027     goto beach;
2028   }
2029
2030   ret = gst_pad_alloc_buffer_and_set_caps (stream->pad,
2031       GST_BUFFER_OFFSET_NONE, packet_size, GST_PAD_CAPS (stream->pad), &buffer);
2032
2033   cret = gst_rmdemux_combine_flows (rmdemux, stream, ret);
2034   if (ret != GST_FLOW_OK)
2035     goto alloc_failed;
2036
2037   memcpy (GST_BUFFER_DATA (buffer), (guint8 *) data, packet_size);
2038   GST_BUFFER_TIMESTAMP (buffer) = timestamp - rmdemux->first_ts;
2039
2040   if (stream->needs_descrambling) {
2041     ret = gst_rmdemux_handle_scrambled_packet (rmdemux, stream, buffer, key);
2042   } else {
2043     GST_LOG_OBJECT (rmdemux, "Pushing buffer of size %d to pad %s",
2044         GST_BUFFER_SIZE (buffer), GST_PAD_NAME (stream->pad));
2045
2046     ret = gst_pad_push (stream->pad, buffer);
2047   }
2048
2049   cret = gst_rmdemux_combine_flows (rmdemux, stream, ret);
2050
2051 beach:
2052   return cret;
2053
2054   /* ERRORS */
2055 unknown_stream:
2056   {
2057     GST_WARNING_OBJECT (rmdemux, "No stream for stream id %d in parsing "
2058         "data packet", id);
2059     return GST_FLOW_OK;
2060   }
2061 alloc_failed:
2062   {
2063     GST_DEBUG_OBJECT (rmdemux, "pad alloc returned %d", ret);
2064     return cret;
2065   }
2066 }
2067
2068 static gboolean
2069 gst_rmdemux_plugin_init (GstPlugin * plugin)
2070 {
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);
2075 }
2076
2077
2078 static gboolean
2079 plugin_init (GstPlugin * plugin)
2080 {
2081   if (!gst_rmdemux_plugin_init (plugin))
2082     return FALSE;
2083
2084   if (!gst_rdt_depay_plugin_init (plugin))
2085     return FALSE;
2086
2087   if (!gst_rdt_manager_plugin_init (plugin))
2088     return FALSE;
2089
2090   if (!gst_rtsp_real_plugin_init (plugin))
2091     return FALSE;
2092
2093   return TRUE;
2094 }
2095
2096 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
2097     GST_VERSION_MINOR,
2098     "realmedia",
2099     "RealMedia demuxer and depayloader",
2100     plugin_init, VERSION, "LGPL", GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);