Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 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 | * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com> |
| 10 | * |
| 11 | * This library is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU Library General Public |
| 13 | * License as published by the Free Software Foundation; either |
| 14 | * version 2 of the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This library is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 19 | * Library General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU Library General Public |
| 22 | * License along with this library; if not, write to the |
Sebastian Dröge | 6bf128e | 2013-07-14 12:07:04 +0200 | [diff] [blame] | 23 | * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
| 24 | * Boston, MA 02110-1301, USA. |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | #ifdef HAVE_CONFIG_H |
| 28 | # include "config.h" |
| 29 | #endif |
| 30 | |
| 31 | #include "rmdemux.h" |
| 32 | #include "rmutils.h" |
| 33 | |
| 34 | #include <string.h> |
| 35 | #include <ctype.h> |
| 36 | |
| 37 | #define RMDEMUX_GUINT32_GET(a) GST_READ_UINT32_BE(a) |
| 38 | #define RMDEMUX_GUINT16_GET(a) GST_READ_UINT16_BE(a) |
| 39 | #define RMDEMUX_FOURCC_GET(a) GST_READ_UINT32_LE(a) |
| 40 | #define HEADER_SIZE 10 |
| 41 | #define DATA_SIZE 8 |
| 42 | |
| 43 | #define MAX_FRAGS 256 |
| 44 | |
| 45 | static const guint8 sipr_subpk_size[4] = { 29, 19, 37, 20 }; |
| 46 | |
| 47 | typedef struct _GstRMDemuxIndex GstRMDemuxIndex; |
| 48 | |
| 49 | struct _GstRMDemuxStream |
| 50 | { |
| 51 | guint32 subtype; |
| 52 | guint32 fourcc; |
| 53 | guint32 subformat; |
| 54 | guint32 format; |
| 55 | |
| 56 | int id; |
| 57 | GstPad *pad; |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 58 | gboolean discont; |
| 59 | int timescale; |
| 60 | |
| 61 | int sample_index; |
| 62 | GstRMDemuxIndex *index; |
| 63 | int index_length; |
| 64 | gint framerate_numerator; |
| 65 | gint framerate_denominator; |
| 66 | guint32 seek_offset; |
| 67 | |
| 68 | guint16 width; |
| 69 | guint16 height; |
| 70 | guint16 flavor; |
| 71 | guint16 rate; /* samplerate */ |
| 72 | guint16 n_channels; /* channels */ |
| 73 | guint16 sample_width; /* bits_per_sample */ |
| 74 | guint16 leaf_size; /* subpacket_size */ |
| 75 | guint32 packet_size; /* coded_frame_size */ |
| 76 | guint16 version; |
| 77 | guint32 extra_data_size; /* codec_data_length */ |
| 78 | guint8 *extra_data; /* extras */ |
| 79 | guint32 bitrate; |
| 80 | |
| 81 | gboolean needs_descrambling; |
| 82 | guint subpackets_needed; /* subpackets needed for descrambling */ |
| 83 | GPtrArray *subpackets; /* array containing subpacket GstBuffers */ |
| 84 | |
| 85 | /* Variables needed for fixing timestamps. */ |
| 86 | GstClockTime next_ts, last_ts; |
| 87 | guint16 next_seq, last_seq; |
| 88 | |
| 89 | gint frag_seqnum; |
| 90 | gint frag_subseq; |
| 91 | guint frag_length; |
| 92 | guint frag_current; |
| 93 | guint frag_count; |
| 94 | guint frag_offset[MAX_FRAGS]; |
| 95 | GstAdapter *adapter; |
| 96 | |
| 97 | GstTagList *pending_tags; |
| 98 | }; |
| 99 | |
| 100 | struct _GstRMDemuxIndex |
| 101 | { |
| 102 | guint32 offset; |
| 103 | GstClockTime timestamp; |
| 104 | }; |
| 105 | |
| 106 | static GstStaticPadTemplate gst_rmdemux_sink_template = |
| 107 | GST_STATIC_PAD_TEMPLATE ("sink", |
| 108 | GST_PAD_SINK, |
| 109 | GST_PAD_ALWAYS, |
| 110 | GST_STATIC_CAPS ("application/vnd.rn-realmedia") |
| 111 | ); |
| 112 | |
| 113 | static GstStaticPadTemplate gst_rmdemux_videosrc_template = |
| 114 | GST_STATIC_PAD_TEMPLATE ("video_%u", |
| 115 | GST_PAD_SRC, |
| 116 | GST_PAD_SOMETIMES, |
| 117 | GST_STATIC_CAPS_ANY); |
| 118 | |
| 119 | static GstStaticPadTemplate gst_rmdemux_audiosrc_template = |
| 120 | GST_STATIC_PAD_TEMPLATE ("audio_%u", |
| 121 | GST_PAD_SRC, |
| 122 | GST_PAD_SOMETIMES, |
| 123 | GST_STATIC_CAPS_ANY); |
| 124 | |
| 125 | GST_DEBUG_CATEGORY_STATIC (rmdemux_debug); |
| 126 | #define GST_CAT_DEFAULT rmdemux_debug |
| 127 | |
| 128 | static GstElementClass *parent_class = NULL; |
| 129 | |
| 130 | static void gst_rmdemux_class_init (GstRMDemuxClass * klass); |
| 131 | static void gst_rmdemux_base_init (GstRMDemuxClass * klass); |
| 132 | static void gst_rmdemux_init (GstRMDemux * rmdemux); |
| 133 | static void gst_rmdemux_finalize (GObject * object); |
| 134 | static GstStateChangeReturn gst_rmdemux_change_state (GstElement * element, |
| 135 | GstStateChange transition); |
| 136 | static GstFlowReturn gst_rmdemux_chain (GstPad * pad, GstObject * parent, |
| 137 | GstBuffer * buffer); |
| 138 | static void gst_rmdemux_loop (GstPad * pad); |
| 139 | static gboolean gst_rmdemux_sink_activate (GstPad * sinkpad, |
| 140 | GstObject * parent); |
| 141 | static gboolean gst_rmdemux_sink_activate_mode (GstPad * sinkpad, |
| 142 | GstObject * parent, GstPadMode mode, gboolean active); |
| 143 | static gboolean gst_rmdemux_sink_event (GstPad * pad, GstObject * parent, |
| 144 | GstEvent * event); |
| 145 | static gboolean gst_rmdemux_src_event (GstPad * pad, GstObject * parent, |
| 146 | GstEvent * event); |
| 147 | static void gst_rmdemux_send_event (GstRMDemux * rmdemux, GstEvent * event); |
| 148 | static gboolean gst_rmdemux_src_query (GstPad * pad, GstObject * parent, |
| 149 | GstQuery * query); |
| 150 | static gboolean gst_rmdemux_perform_seek (GstRMDemux * rmdemux, |
| 151 | GstEvent * event); |
| 152 | |
| 153 | static void gst_rmdemux_parse__rmf (GstRMDemux * rmdemux, const guint8 * data, |
| 154 | int length); |
| 155 | static void gst_rmdemux_parse_prop (GstRMDemux * rmdemux, const guint8 * data, |
| 156 | int length); |
| 157 | static void gst_rmdemux_parse_mdpr (GstRMDemux * rmdemux, |
| 158 | const guint8 * data, int length); |
| 159 | static guint gst_rmdemux_parse_indx (GstRMDemux * rmdemux, const guint8 * data, |
| 160 | int length); |
| 161 | static void gst_rmdemux_parse_data (GstRMDemux * rmdemux, const guint8 * data, |
| 162 | int length); |
| 163 | static void gst_rmdemux_parse_cont (GstRMDemux * rmdemux, const guint8 * data, |
| 164 | int length); |
| 165 | static GstFlowReturn gst_rmdemux_parse_packet (GstRMDemux * rmdemux, |
| 166 | GstBuffer * in, guint16 version); |
| 167 | static void gst_rmdemux_parse_indx_data (GstRMDemux * rmdemux, |
| 168 | const guint8 * data, int length); |
| 169 | static void gst_rmdemux_stream_clear_cached_subpackets (GstRMDemux * rmdemux, |
| 170 | GstRMDemuxStream * stream); |
| 171 | static GstRMDemuxStream *gst_rmdemux_get_stream_by_id (GstRMDemux * rmdemux, |
| 172 | int id); |
| 173 | |
| 174 | static GType |
| 175 | gst_rmdemux_get_type (void) |
| 176 | { |
| 177 | static GType rmdemux_type = 0; |
| 178 | |
| 179 | if (!rmdemux_type) { |
| 180 | static const GTypeInfo rmdemux_info = { |
| 181 | sizeof (GstRMDemuxClass), |
| 182 | (GBaseInitFunc) gst_rmdemux_base_init, NULL, |
| 183 | (GClassInitFunc) gst_rmdemux_class_init, |
| 184 | NULL, NULL, sizeof (GstRMDemux), 0, |
| 185 | (GInstanceInitFunc) gst_rmdemux_init, |
| 186 | }; |
| 187 | |
| 188 | rmdemux_type = |
| 189 | g_type_register_static (GST_TYPE_ELEMENT, "GstRMDemux", &rmdemux_info, |
| 190 | 0); |
| 191 | } |
| 192 | return rmdemux_type; |
| 193 | } |
| 194 | |
| 195 | static void |
| 196 | gst_rmdemux_base_init (GstRMDemuxClass * klass) |
| 197 | { |
| 198 | GstElementClass *element_class = GST_ELEMENT_CLASS (klass); |
| 199 | |
Sebastian Dröge | 2dec810 | 2016-07-06 13:15:17 +0300 | [diff] [blame] | 200 | gst_element_class_add_static_pad_template (element_class, |
| 201 | &gst_rmdemux_sink_template); |
| 202 | gst_element_class_add_static_pad_template (element_class, |
| 203 | &gst_rmdemux_videosrc_template); |
| 204 | gst_element_class_add_static_pad_template (element_class, |
| 205 | &gst_rmdemux_audiosrc_template); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 206 | gst_element_class_set_static_metadata (element_class, "RealMedia Demuxer", |
| 207 | "Codec/Demuxer", |
| 208 | "Demultiplex a RealMedia file into audio and video streams", |
| 209 | "David Schleef <ds@schleef.org>"); |
| 210 | } |
| 211 | |
| 212 | static void |
| 213 | gst_rmdemux_class_init (GstRMDemuxClass * klass) |
| 214 | { |
| 215 | GObjectClass *gobject_class; |
| 216 | GstElementClass *gstelement_class; |
| 217 | |
| 218 | gobject_class = (GObjectClass *) klass; |
| 219 | gstelement_class = (GstElementClass *) klass; |
| 220 | |
| 221 | parent_class = g_type_class_peek_parent (klass); |
| 222 | |
| 223 | gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_rmdemux_change_state); |
| 224 | |
| 225 | GST_DEBUG_CATEGORY_INIT (rmdemux_debug, "rmdemux", |
| 226 | 0, "Demuxer for Realmedia streams"); |
| 227 | |
| 228 | gobject_class->finalize = gst_rmdemux_finalize; |
| 229 | } |
| 230 | |
| 231 | static void |
| 232 | gst_rmdemux_finalize (GObject * object) |
| 233 | { |
| 234 | GstRMDemux *rmdemux = GST_RMDEMUX (object); |
| 235 | |
| 236 | if (rmdemux->adapter) { |
| 237 | g_object_unref (rmdemux->adapter); |
| 238 | rmdemux->adapter = NULL; |
| 239 | } |
Sebastian Dröge | c679a0b | 2014-06-22 18:11:42 +0200 | [diff] [blame] | 240 | if (rmdemux->flowcombiner) { |
| 241 | gst_flow_combiner_free (rmdemux->flowcombiner); |
| 242 | rmdemux->flowcombiner = NULL; |
| 243 | } |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 244 | |
| 245 | GST_CALL_PARENT (G_OBJECT_CLASS, finalize, (object)); |
| 246 | } |
| 247 | |
| 248 | static void |
| 249 | gst_rmdemux_init (GstRMDemux * rmdemux) |
| 250 | { |
| 251 | rmdemux->sinkpad = |
| 252 | gst_pad_new_from_static_template (&gst_rmdemux_sink_template, "sink"); |
| 253 | gst_pad_set_event_function (rmdemux->sinkpad, |
| 254 | GST_DEBUG_FUNCPTR (gst_rmdemux_sink_event)); |
| 255 | gst_pad_set_chain_function (rmdemux->sinkpad, |
| 256 | GST_DEBUG_FUNCPTR (gst_rmdemux_chain)); |
| 257 | gst_pad_set_activate_function (rmdemux->sinkpad, |
| 258 | GST_DEBUG_FUNCPTR (gst_rmdemux_sink_activate)); |
| 259 | gst_pad_set_activatemode_function (rmdemux->sinkpad, |
| 260 | GST_DEBUG_FUNCPTR (gst_rmdemux_sink_activate_mode)); |
| 261 | |
| 262 | gst_element_add_pad (GST_ELEMENT (rmdemux), rmdemux->sinkpad); |
| 263 | |
| 264 | rmdemux->adapter = gst_adapter_new (); |
| 265 | rmdemux->first_ts = GST_CLOCK_TIME_NONE; |
| 266 | rmdemux->base_ts = GST_CLOCK_TIME_NONE; |
| 267 | rmdemux->need_newsegment = TRUE; |
Sebastian Dröge | 7efeeb0 | 2013-07-30 08:51:53 +0200 | [diff] [blame] | 268 | rmdemux->have_group_id = FALSE; |
| 269 | rmdemux->group_id = G_MAXUINT; |
Sebastian Dröge | c679a0b | 2014-06-22 18:11:42 +0200 | [diff] [blame] | 270 | rmdemux->flowcombiner = gst_flow_combiner_new (); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 271 | |
| 272 | gst_rm_utils_run_tests (); |
| 273 | } |
| 274 | |
| 275 | static gboolean |
| 276 | gst_rmdemux_sink_event (GstPad * pad, GstObject * parent, GstEvent * event) |
| 277 | { |
| 278 | gboolean ret; |
| 279 | |
| 280 | switch (GST_EVENT_TYPE (event)) { |
| 281 | case GST_EVENT_SEGMENT: |
| 282 | gst_event_unref (event); |
| 283 | ret = TRUE; |
| 284 | break; |
| 285 | default: |
| 286 | ret = gst_pad_event_default (pad, parent, event); |
| 287 | break; |
| 288 | } |
| 289 | return ret; |
| 290 | } |
| 291 | |
| 292 | static gboolean |
| 293 | gst_rmdemux_src_event (GstPad * pad, GstObject * parent, GstEvent * event) |
| 294 | { |
| 295 | gboolean ret = TRUE; |
| 296 | |
| 297 | GstRMDemux *rmdemux = GST_RMDEMUX (parent); |
| 298 | |
| 299 | GST_LOG_OBJECT (rmdemux, "handling src event"); |
| 300 | |
| 301 | switch (GST_EVENT_TYPE (event)) { |
| 302 | case GST_EVENT_SEEK: |
| 303 | { |
| 304 | gboolean running; |
| 305 | |
| 306 | GST_LOG_OBJECT (rmdemux, "Event on src: SEEK"); |
| 307 | /* can't seek if we are not seekable, FIXME could pass the |
| 308 | * seek query upstream after converting it to bytes using |
| 309 | * the average bitrate of the stream. */ |
| 310 | if (!rmdemux->seekable) { |
| 311 | ret = FALSE; |
| 312 | GST_DEBUG ("seek on non seekable stream"); |
| 313 | goto done_unref; |
| 314 | } |
| 315 | |
| 316 | GST_OBJECT_LOCK (rmdemux); |
| 317 | /* check if we can do the seek now */ |
| 318 | running = rmdemux->running; |
| 319 | GST_OBJECT_UNLOCK (rmdemux); |
| 320 | |
| 321 | /* now do the seek */ |
| 322 | if (running) { |
| 323 | ret = gst_rmdemux_perform_seek (rmdemux, event); |
| 324 | } else |
| 325 | ret = TRUE; |
| 326 | |
| 327 | gst_event_unref (event); |
| 328 | break; |
| 329 | } |
| 330 | default: |
| 331 | GST_LOG_OBJECT (rmdemux, "Event on src: type=%d", GST_EVENT_TYPE (event)); |
| 332 | ret = gst_pad_event_default (pad, parent, event); |
| 333 | break; |
| 334 | } |
| 335 | |
| 336 | return ret; |
| 337 | |
| 338 | done_unref: |
| 339 | GST_DEBUG ("error handling event"); |
| 340 | gst_event_unref (event); |
| 341 | return ret; |
| 342 | } |
| 343 | |
| 344 | /* Validate that this looks like a reasonable point to seek to */ |
| 345 | static gboolean |
| 346 | gst_rmdemux_validate_offset (GstRMDemux * rmdemux) |
| 347 | { |
| 348 | GstBuffer *buffer; |
| 349 | GstFlowReturn flowret; |
| 350 | guint16 version, length; |
| 351 | gboolean ret = TRUE; |
| 352 | GstMapInfo map; |
| 353 | |
| 354 | buffer = NULL; |
| 355 | flowret = gst_pad_pull_range (rmdemux->sinkpad, rmdemux->offset, 4, &buffer); |
| 356 | |
| 357 | if (flowret != GST_FLOW_OK) { |
| 358 | GST_DEBUG_OBJECT (rmdemux, "Failed to pull data at offset %d", |
| 359 | rmdemux->offset); |
| 360 | return FALSE; |
| 361 | } |
| 362 | /* TODO: Can we also be seeking to a 'DATA' chunk header? Check this. |
| 363 | * Also, for the case we currently handle, can we check any more? It's pretty |
| 364 | * sucky to not be validating a little more heavily than this... */ |
| 365 | /* This should now be the start of a data packet header. That begins with |
| 366 | * a 2-byte 'version' field, which has to be 0 or 1, then a length. I'm not |
| 367 | * certain what values are valid for length, but it must always be at least |
| 368 | * 4 bytes, and we can check that it won't take us past our known total size |
| 369 | */ |
| 370 | |
| 371 | gst_buffer_map (buffer, &map, GST_MAP_READ); |
| 372 | version = RMDEMUX_GUINT16_GET (map.data); |
| 373 | if (version != 0 && version != 1) { |
| 374 | GST_DEBUG_OBJECT (rmdemux, "Expected version 0 or 1, got %d", |
| 375 | (int) version); |
| 376 | ret = FALSE; |
| 377 | } |
| 378 | |
| 379 | length = RMDEMUX_GUINT16_GET (map.data + 2); |
| 380 | /* TODO: Also check against total stream length */ |
| 381 | if (length < 4) { |
| 382 | GST_DEBUG_OBJECT (rmdemux, "Expected length >= 4, got %d", (int) length); |
| 383 | ret = FALSE; |
| 384 | } |
| 385 | gst_buffer_unmap (buffer, &map); |
| 386 | |
| 387 | if (ret) { |
| 388 | rmdemux->offset += 4; |
| 389 | gst_adapter_clear (rmdemux->adapter); |
| 390 | gst_adapter_push (rmdemux->adapter, buffer); |
| 391 | } else { |
| 392 | GST_WARNING_OBJECT (rmdemux, "Failed to validate seek offset at %d", |
| 393 | rmdemux->offset); |
| 394 | gst_buffer_unref (buffer); |
| 395 | } |
| 396 | |
| 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | static gboolean |
| 401 | find_seek_offset_bytes (GstRMDemux * rmdemux, guint target) |
| 402 | { |
| 403 | int i; |
| 404 | GSList *cur; |
| 405 | gboolean ret = FALSE; |
| 406 | |
| 407 | for (cur = rmdemux->streams; cur; cur = cur->next) { |
| 408 | GstRMDemuxStream *stream = cur->data; |
| 409 | |
| 410 | /* Search backwards through this stream's index until we find the first |
| 411 | * timestamp before our target time */ |
| 412 | for (i = stream->index_length - 1; i >= 0; i--) { |
| 413 | if (stream->index[i].offset <= target) { |
| 414 | /* Set the seek_offset for the stream so we don't bother parsing it |
| 415 | * until we've passed that point */ |
| 416 | stream->seek_offset = stream->index[i].offset; |
| 417 | rmdemux->offset = stream->index[i].offset; |
| 418 | ret = TRUE; |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 | return ret; |
| 424 | } |
| 425 | |
| 426 | static gboolean |
| 427 | find_seek_offset_time (GstRMDemux * rmdemux, GstClockTime time) |
| 428 | { |
| 429 | int i, n_stream; |
| 430 | gboolean ret = FALSE; |
| 431 | GSList *cur; |
| 432 | GstClockTime earliest = GST_CLOCK_TIME_NONE; |
| 433 | |
| 434 | n_stream = 0; |
| 435 | for (cur = rmdemux->streams; cur; cur = cur->next, n_stream++) { |
| 436 | GstRMDemuxStream *stream = cur->data; |
| 437 | |
| 438 | /* Search backwards through this stream's index until we find the first |
| 439 | * timestamp before our target time */ |
| 440 | for (i = stream->index_length - 1; i >= 0; i--) { |
| 441 | if (stream->index[i].timestamp <= time) { |
| 442 | /* Set the seek_offset for the stream so we don't bother parsing it |
| 443 | * until we've passed that point */ |
| 444 | stream->seek_offset = stream->index[i].offset; |
| 445 | |
| 446 | /* If it's also the earliest timestamp we've seen of all streams, then |
| 447 | * that's our target! |
| 448 | */ |
| 449 | if (earliest == GST_CLOCK_TIME_NONE || |
| 450 | stream->index[i].timestamp < earliest) { |
| 451 | earliest = stream->index[i].timestamp; |
| 452 | rmdemux->offset = stream->index[i].offset; |
| 453 | GST_DEBUG_OBJECT (rmdemux, |
| 454 | "We're looking for %" GST_TIME_FORMAT |
| 455 | " and we found that stream %d has the latest index at %" |
| 456 | GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start), n_stream, |
| 457 | GST_TIME_ARGS (earliest)); |
| 458 | } |
| 459 | |
| 460 | ret = TRUE; |
| 461 | |
| 462 | break; |
| 463 | } |
| 464 | } |
| 465 | stream->discont = TRUE; |
| 466 | } |
| 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | static gboolean |
| 471 | gst_rmdemux_perform_seek (GstRMDemux * rmdemux, GstEvent * event) |
| 472 | { |
| 473 | gboolean validated; |
| 474 | gboolean ret = TRUE; |
| 475 | gboolean flush; |
| 476 | GstFormat format; |
| 477 | gdouble rate; |
| 478 | GstSeekFlags flags; |
| 479 | GstSeekType cur_type, stop_type; |
| 480 | gint64 cur, stop; |
| 481 | gboolean update; |
| 482 | |
| 483 | if (event) { |
| 484 | GST_DEBUG_OBJECT (rmdemux, "seek with event"); |
| 485 | |
| 486 | gst_event_parse_seek (event, &rate, &format, &flags, |
| 487 | &cur_type, &cur, &stop_type, &stop); |
| 488 | |
| 489 | /* we can only seek on time */ |
| 490 | if (format != GST_FORMAT_TIME) { |
| 491 | GST_DEBUG_OBJECT (rmdemux, "can only seek on TIME"); |
| 492 | goto error; |
| 493 | } |
| 494 | /* cannot yet do backwards playback */ |
| 495 | if (rate <= 0.0) { |
| 496 | GST_DEBUG_OBJECT (rmdemux, "can only seek with positive rate, not %lf", |
| 497 | rate); |
| 498 | goto error; |
| 499 | } |
| 500 | } else { |
| 501 | GST_DEBUG_OBJECT (rmdemux, "seek without event"); |
| 502 | |
| 503 | flags = 0; |
| 504 | rate = 1.0; |
| 505 | } |
| 506 | |
| 507 | GST_DEBUG_OBJECT (rmdemux, "seek, rate %g", rate); |
| 508 | |
| 509 | flush = flags & GST_SEEK_FLAG_FLUSH; |
| 510 | |
| 511 | /* first step is to unlock the streaming thread if it is |
| 512 | * blocked in a chain call, we do this by starting the flush. */ |
| 513 | if (flush) { |
| 514 | gst_pad_push_event (rmdemux->sinkpad, gst_event_new_flush_start ()); |
| 515 | gst_rmdemux_send_event (rmdemux, gst_event_new_flush_start ()); |
| 516 | } else { |
| 517 | gst_pad_pause_task (rmdemux->sinkpad); |
| 518 | } |
| 519 | |
| 520 | GST_LOG_OBJECT (rmdemux, "Done starting flushes"); |
| 521 | |
| 522 | /* now grab the stream lock so that streaming cannot continue, for |
| 523 | * non flushing seeks when the element is in PAUSED this could block |
| 524 | * forever. */ |
| 525 | GST_PAD_STREAM_LOCK (rmdemux->sinkpad); |
| 526 | |
| 527 | GST_LOG_OBJECT (rmdemux, "Took streamlock"); |
| 528 | |
| 529 | if (event) { |
| 530 | gst_segment_do_seek (&rmdemux->segment, rate, format, flags, |
| 531 | cur_type, cur, stop_type, stop, &update); |
| 532 | } |
| 533 | |
| 534 | GST_DEBUG_OBJECT (rmdemux, "segment positions set to %" GST_TIME_FORMAT "-%" |
| 535 | GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start), |
| 536 | GST_TIME_ARGS (rmdemux->segment.stop)); |
| 537 | |
| 538 | /* we need to stop flushing on the sinkpad as we're going to use it |
| 539 | * next. We can do this as we have the STREAM lock now. */ |
| 540 | gst_pad_push_event (rmdemux->sinkpad, gst_event_new_flush_stop (TRUE)); |
| 541 | |
| 542 | GST_LOG_OBJECT (rmdemux, "Pushed FLUSH_STOP event"); |
| 543 | |
| 544 | /* For each stream, find the first index offset equal to or before our seek |
| 545 | * target. Of these, find the smallest offset. That's where we seek to. |
| 546 | * |
| 547 | * Then we pull 4 bytes from that offset, and validate that we've seeked to a |
| 548 | * what looks like a plausible packet. |
| 549 | * If that fails, restart, with the seek target set to one less than the |
| 550 | * offset we just tried. If we run out of places to try, treat that as a fatal |
| 551 | * error. |
| 552 | */ |
| 553 | if (!find_seek_offset_time (rmdemux, rmdemux->segment.position)) { |
| 554 | GST_LOG_OBJECT (rmdemux, "Failed to find seek offset by time"); |
| 555 | ret = FALSE; |
| 556 | goto done; |
| 557 | } |
| 558 | |
| 559 | GST_LOG_OBJECT (rmdemux, "Validating offset %u", rmdemux->offset); |
| 560 | validated = gst_rmdemux_validate_offset (rmdemux); |
| 561 | while (!validated) { |
| 562 | GST_INFO_OBJECT (rmdemux, "Failed to validate offset at %u", |
| 563 | rmdemux->offset); |
| 564 | if (!find_seek_offset_bytes (rmdemux, rmdemux->offset - 1)) { |
| 565 | ret = FALSE; |
| 566 | goto done; |
| 567 | } |
| 568 | validated = gst_rmdemux_validate_offset (rmdemux); |
| 569 | } |
| 570 | |
| 571 | GST_LOG_OBJECT (rmdemux, "Found final offset. Excellent!"); |
| 572 | |
| 573 | /* now we have a new position, prepare for streaming again */ |
| 574 | { |
| 575 | /* Reset the demuxer state */ |
| 576 | rmdemux->state = RMDEMUX_STATE_DATA_PACKET; |
| 577 | |
| 578 | if (flush) |
| 579 | gst_rmdemux_send_event (rmdemux, gst_event_new_flush_stop (TRUE)); |
| 580 | |
| 581 | /* must send newsegment event from streaming thread, so just set flag */ |
| 582 | rmdemux->need_newsegment = TRUE; |
| 583 | |
| 584 | /* notify start of new segment */ |
| 585 | if (rmdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) { |
| 586 | gst_element_post_message (GST_ELEMENT_CAST (rmdemux), |
| 587 | gst_message_new_segment_start (GST_OBJECT_CAST (rmdemux), |
| 588 | GST_FORMAT_TIME, rmdemux->segment.position)); |
| 589 | } |
| 590 | |
| 591 | /* restart our task since it might have been stopped when we did the |
| 592 | * flush. */ |
| 593 | gst_pad_start_task (rmdemux->sinkpad, (GstTaskFunction) gst_rmdemux_loop, |
Sebastian Dröge | c167d67 | 2012-08-09 11:25:39 +0200 | [diff] [blame] | 594 | rmdemux->sinkpad, NULL); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | done: |
| 598 | /* streaming can continue now */ |
| 599 | GST_PAD_STREAM_UNLOCK (rmdemux->sinkpad); |
| 600 | |
| 601 | return ret; |
| 602 | |
| 603 | error: |
| 604 | { |
| 605 | GST_DEBUG_OBJECT (rmdemux, "seek failed"); |
| 606 | return FALSE; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | |
| 611 | static gboolean |
| 612 | gst_rmdemux_src_query (GstPad * pad, GstObject * parent, GstQuery * query) |
| 613 | { |
| 614 | gboolean res = FALSE; |
| 615 | GstRMDemux *rmdemux; |
| 616 | |
| 617 | rmdemux = GST_RMDEMUX (parent); |
| 618 | |
| 619 | switch (GST_QUERY_TYPE (query)) { |
| 620 | case GST_QUERY_POSITION: |
| 621 | GST_DEBUG_OBJECT (rmdemux, "Position query: no idea from demuxer!"); |
| 622 | break; |
| 623 | case GST_QUERY_DURATION:{ |
| 624 | GstFormat fmt; |
| 625 | |
| 626 | gst_query_parse_duration (query, &fmt, NULL); |
| 627 | if (fmt == GST_FORMAT_TIME) { |
| 628 | GST_OBJECT_LOCK (rmdemux); |
| 629 | if (G_LIKELY (rmdemux->running)) { |
| 630 | gst_query_set_duration (query, GST_FORMAT_TIME, rmdemux->duration); |
| 631 | GST_DEBUG_OBJECT (rmdemux, "duration set to %" GST_TIME_FORMAT, |
| 632 | GST_TIME_ARGS (rmdemux->duration)); |
| 633 | res = TRUE; |
| 634 | } |
| 635 | GST_OBJECT_UNLOCK (rmdemux); |
| 636 | } |
| 637 | break; |
| 638 | } |
| 639 | case GST_QUERY_SEEKING:{ |
| 640 | GstFormat fmt; |
| 641 | |
| 642 | gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL); |
| 643 | if (fmt == GST_FORMAT_TIME) { |
| 644 | GST_OBJECT_LOCK (rmdemux); |
| 645 | if (G_LIKELY (rmdemux->running)) { |
| 646 | gst_query_set_seeking (query, GST_FORMAT_TIME, rmdemux->seekable, |
| 647 | 0, rmdemux->duration); |
| 648 | res = TRUE; |
| 649 | } |
| 650 | GST_OBJECT_UNLOCK (rmdemux); |
| 651 | } |
| 652 | break; |
| 653 | } |
Sebastian Dröge | 7efeeb0 | 2013-07-30 08:51:53 +0200 | [diff] [blame] | 654 | case GST_QUERY_SEGMENT: |
| 655 | { |
| 656 | GstFormat format; |
| 657 | gint64 start, stop; |
| 658 | |
| 659 | format = rmdemux->segment.format; |
| 660 | |
| 661 | start = |
| 662 | gst_segment_to_stream_time (&rmdemux->segment, format, |
| 663 | rmdemux->segment.start); |
| 664 | if ((stop = rmdemux->segment.stop) == -1) |
| 665 | stop = rmdemux->segment.duration; |
| 666 | else |
| 667 | stop = gst_segment_to_stream_time (&rmdemux->segment, format, stop); |
| 668 | |
| 669 | gst_query_set_segment (query, rmdemux->segment.rate, format, start, stop); |
| 670 | res = TRUE; |
| 671 | break; |
| 672 | } |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 673 | default: |
| 674 | res = gst_pad_query_default (pad, parent, query); |
| 675 | break; |
| 676 | } |
| 677 | |
| 678 | return res; |
| 679 | } |
| 680 | |
| 681 | static void |
Sebastian Dröge | d8a669d | 2015-08-19 14:01:25 +0300 | [diff] [blame] | 682 | gst_rmdemux_free_stream (GstRMDemux * rmdemux, GstRMDemuxStream * stream) |
| 683 | { |
| 684 | g_object_unref (stream->adapter); |
| 685 | gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream); |
| 686 | if (stream->pending_tags) |
| 687 | gst_tag_list_unref (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 | |
| 694 | static void |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 695 | gst_rmdemux_reset (GstRMDemux * rmdemux) |
| 696 | { |
| 697 | GSList *cur; |
| 698 | |
| 699 | GST_OBJECT_LOCK (rmdemux); |
| 700 | rmdemux->running = FALSE; |
| 701 | GST_OBJECT_UNLOCK (rmdemux); |
| 702 | |
| 703 | for (cur = rmdemux->streams; cur; cur = cur->next) { |
| 704 | GstRMDemuxStream *stream = cur->data; |
| 705 | |
Sebastian Dröge | c679a0b | 2014-06-22 18:11:42 +0200 | [diff] [blame] | 706 | gst_flow_combiner_remove_pad (rmdemux->flowcombiner, stream->pad); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 707 | gst_element_remove_pad (GST_ELEMENT (rmdemux), stream->pad); |
Sebastian Dröge | d8a669d | 2015-08-19 14:01:25 +0300 | [diff] [blame] | 708 | gst_rmdemux_free_stream (rmdemux, stream); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 709 | } |
| 710 | g_slist_free (rmdemux->streams); |
| 711 | rmdemux->streams = NULL; |
| 712 | rmdemux->n_audio_streams = 0; |
| 713 | rmdemux->n_video_streams = 0; |
| 714 | |
| 715 | if (rmdemux->pending_tags != NULL) { |
Sebastian Dröge | c167d67 | 2012-08-09 11:25:39 +0200 | [diff] [blame] | 716 | gst_tag_list_unref (rmdemux->pending_tags); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 717 | rmdemux->pending_tags = NULL; |
| 718 | } |
| 719 | |
| 720 | gst_adapter_clear (rmdemux->adapter); |
| 721 | rmdemux->state = RMDEMUX_STATE_HEADER; |
| 722 | rmdemux->have_pads = FALSE; |
| 723 | |
| 724 | gst_segment_init (&rmdemux->segment, GST_FORMAT_UNDEFINED); |
| 725 | rmdemux->first_ts = GST_CLOCK_TIME_NONE; |
| 726 | rmdemux->base_ts = GST_CLOCK_TIME_NONE; |
| 727 | rmdemux->need_newsegment = TRUE; |
Sebastian Dröge | 7efeeb0 | 2013-07-30 08:51:53 +0200 | [diff] [blame] | 728 | |
| 729 | rmdemux->have_group_id = FALSE; |
| 730 | rmdemux->group_id = G_MAXUINT; |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | static GstStateChangeReturn |
| 734 | gst_rmdemux_change_state (GstElement * element, GstStateChange transition) |
| 735 | { |
| 736 | GstRMDemux *rmdemux = GST_RMDEMUX (element); |
| 737 | GstStateChangeReturn res; |
| 738 | |
| 739 | switch (transition) { |
| 740 | case GST_STATE_CHANGE_NULL_TO_READY: |
| 741 | break; |
| 742 | case GST_STATE_CHANGE_READY_TO_PAUSED: |
| 743 | rmdemux->state = RMDEMUX_STATE_HEADER; |
| 744 | rmdemux->have_pads = FALSE; |
| 745 | gst_segment_init (&rmdemux->segment, GST_FORMAT_TIME); |
| 746 | rmdemux->running = FALSE; |
| 747 | break; |
| 748 | case GST_STATE_CHANGE_PAUSED_TO_PLAYING: |
| 749 | break; |
| 750 | default: |
| 751 | break; |
| 752 | } |
| 753 | |
| 754 | res = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition); |
| 755 | |
| 756 | switch (transition) { |
| 757 | case GST_STATE_CHANGE_PLAYING_TO_PAUSED: |
| 758 | break; |
| 759 | case GST_STATE_CHANGE_PAUSED_TO_READY:{ |
| 760 | gst_rmdemux_reset (rmdemux); |
| 761 | break; |
| 762 | } |
| 763 | case GST_STATE_CHANGE_READY_TO_NULL: |
| 764 | break; |
| 765 | default: |
| 766 | break; |
| 767 | } |
| 768 | |
| 769 | return res; |
| 770 | } |
| 771 | |
| 772 | /* this function is called when the pad is activated and should start |
| 773 | * processing data. |
| 774 | * |
| 775 | * We check if we can do random access to decide if we work push or |
| 776 | * pull based. |
| 777 | */ |
| 778 | static gboolean |
| 779 | gst_rmdemux_sink_activate (GstPad * sinkpad, GstObject * parent) |
| 780 | { |
| 781 | GstQuery *query; |
| 782 | gboolean pull_mode; |
| 783 | |
| 784 | query = gst_query_new_scheduling (); |
| 785 | |
| 786 | if (!gst_pad_peer_query (sinkpad, query)) { |
| 787 | gst_query_unref (query); |
| 788 | goto activate_push; |
| 789 | } |
| 790 | |
Sebastian Dröge | a60e713 | 2012-09-14 10:36:23 +0200 | [diff] [blame] | 791 | pull_mode = gst_query_has_scheduling_mode_with_flags (query, |
| 792 | GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 793 | gst_query_unref (query); |
| 794 | |
| 795 | if (!pull_mode) |
| 796 | goto activate_push; |
| 797 | |
| 798 | GST_DEBUG_OBJECT (sinkpad, "activating pull"); |
| 799 | return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PULL, TRUE); |
| 800 | |
| 801 | activate_push: |
| 802 | { |
| 803 | GST_DEBUG_OBJECT (sinkpad, "activating push"); |
| 804 | return gst_pad_activate_mode (sinkpad, GST_PAD_MODE_PUSH, TRUE); |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | static gboolean |
| 809 | gst_rmdemux_sink_activate_mode (GstPad * sinkpad, GstObject * parent, |
| 810 | GstPadMode mode, gboolean active) |
| 811 | { |
| 812 | gboolean res; |
| 813 | GstRMDemux *demux; |
| 814 | |
| 815 | demux = GST_RMDEMUX (parent); |
| 816 | |
| 817 | switch (mode) { |
| 818 | case GST_PAD_MODE_PUSH: |
| 819 | demux->seekable = FALSE; |
Sebastian Dröge | 7efeeb0 | 2013-07-30 08:51:53 +0200 | [diff] [blame] | 820 | demux->running = active; |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 821 | res = TRUE; |
| 822 | break; |
| 823 | case GST_PAD_MODE_PULL: |
| 824 | if (active) { |
| 825 | demux->seekable = TRUE; |
| 826 | demux->offset = 0; |
| 827 | demux->loop_state = RMDEMUX_LOOP_STATE_HEADER; |
| 828 | demux->data_offset = G_MAXUINT; |
| 829 | res = |
| 830 | gst_pad_start_task (sinkpad, (GstTaskFunction) gst_rmdemux_loop, |
Sebastian Dröge | c167d67 | 2012-08-09 11:25:39 +0200 | [diff] [blame] | 831 | sinkpad, NULL); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 832 | } else { |
| 833 | res = gst_pad_stop_task (sinkpad); |
| 834 | } |
| 835 | break; |
| 836 | default: |
| 837 | res = FALSE; |
| 838 | break; |
| 839 | } |
| 840 | return res; |
| 841 | } |
| 842 | |
| 843 | |
| 844 | /* random access mode - just pass over to our chain function */ |
| 845 | static void |
| 846 | gst_rmdemux_loop (GstPad * pad) |
| 847 | { |
| 848 | GstRMDemux *rmdemux; |
| 849 | GstBuffer *buffer; |
| 850 | GstFlowReturn ret = GST_FLOW_OK; |
| 851 | guint size; |
| 852 | |
| 853 | rmdemux = GST_RMDEMUX (GST_PAD_PARENT (pad)); |
| 854 | |
| 855 | GST_LOG_OBJECT (rmdemux, "loop with state=%d and offset=0x%x", |
| 856 | rmdemux->loop_state, rmdemux->offset); |
| 857 | |
| 858 | switch (rmdemux->state) { |
| 859 | case RMDEMUX_STATE_HEADER: |
| 860 | size = HEADER_SIZE; |
| 861 | break; |
| 862 | case RMDEMUX_STATE_HEADER_DATA: |
| 863 | size = DATA_SIZE; |
| 864 | break; |
| 865 | case RMDEMUX_STATE_DATA_PACKET: |
| 866 | size = rmdemux->avg_packet_size; |
| 867 | break; |
| 868 | case RMDEMUX_STATE_EOS: |
| 869 | GST_LOG_OBJECT (rmdemux, "At EOS, pausing task"); |
| 870 | ret = GST_FLOW_EOS; |
| 871 | goto need_pause; |
| 872 | default: |
| 873 | GST_LOG_OBJECT (rmdemux, "Default: requires %d bytes (state is %d)", |
| 874 | (int) rmdemux->size, rmdemux->state); |
| 875 | size = rmdemux->size; |
| 876 | } |
| 877 | |
| 878 | buffer = NULL; |
| 879 | ret = gst_pad_pull_range (pad, rmdemux->offset, size, &buffer); |
| 880 | if (ret != GST_FLOW_OK) { |
| 881 | if (rmdemux->offset == rmdemux->index_offset) { |
| 882 | /* The index isn't available so forget about it */ |
| 883 | rmdemux->loop_state = RMDEMUX_LOOP_STATE_DATA; |
| 884 | rmdemux->offset = rmdemux->data_offset; |
| 885 | GST_OBJECT_LOCK (rmdemux); |
| 886 | rmdemux->running = TRUE; |
| 887 | rmdemux->seekable = FALSE; |
| 888 | GST_OBJECT_UNLOCK (rmdemux); |
| 889 | return; |
| 890 | } else { |
| 891 | GST_DEBUG_OBJECT (rmdemux, "Unable to pull %d bytes at offset 0x%08x " |
| 892 | "(pull_range returned flow %s, state is %d)", (gint) size, |
| 893 | rmdemux->offset, gst_flow_get_name (ret), GST_STATE (rmdemux)); |
| 894 | goto need_pause; |
| 895 | } |
| 896 | } |
| 897 | |
| 898 | size = gst_buffer_get_size (buffer); |
| 899 | |
| 900 | /* Defer to the chain function */ |
| 901 | ret = gst_rmdemux_chain (pad, GST_OBJECT_CAST (rmdemux), buffer); |
| 902 | if (ret != GST_FLOW_OK) { |
| 903 | GST_DEBUG_OBJECT (rmdemux, "Chain flow failed at offset 0x%08x", |
| 904 | rmdemux->offset); |
| 905 | goto need_pause; |
| 906 | } |
| 907 | |
| 908 | rmdemux->offset += size; |
| 909 | |
| 910 | switch (rmdemux->loop_state) { |
| 911 | case RMDEMUX_LOOP_STATE_HEADER: |
| 912 | if (rmdemux->offset >= rmdemux->data_offset) { |
| 913 | /* It's the end of the header */ |
| 914 | rmdemux->loop_state = RMDEMUX_LOOP_STATE_INDEX; |
| 915 | rmdemux->offset = rmdemux->index_offset; |
| 916 | } |
| 917 | break; |
| 918 | case RMDEMUX_LOOP_STATE_INDEX: |
| 919 | if (rmdemux->state == RMDEMUX_STATE_HEADER) { |
| 920 | if (rmdemux->index_offset == 0) { |
| 921 | /* We've read the last index */ |
| 922 | rmdemux->loop_state = RMDEMUX_LOOP_STATE_DATA; |
| 923 | rmdemux->offset = rmdemux->data_offset; |
| 924 | GST_OBJECT_LOCK (rmdemux); |
| 925 | rmdemux->running = TRUE; |
| 926 | GST_OBJECT_UNLOCK (rmdemux); |
| 927 | } else { |
| 928 | /* Get the next index */ |
| 929 | rmdemux->offset = rmdemux->index_offset; |
| 930 | } |
| 931 | } |
| 932 | break; |
| 933 | case RMDEMUX_LOOP_STATE_DATA: |
| 934 | break; |
| 935 | } |
| 936 | |
| 937 | return; |
| 938 | |
| 939 | /* ERRORS */ |
| 940 | need_pause: |
| 941 | { |
| 942 | const gchar *reason = gst_flow_get_name (ret); |
| 943 | |
| 944 | GST_LOG_OBJECT (rmdemux, "pausing task, reason %s", reason); |
| 945 | rmdemux->segment_running = FALSE; |
| 946 | gst_pad_pause_task (rmdemux->sinkpad); |
| 947 | |
| 948 | if (ret == GST_FLOW_EOS) { |
| 949 | /* perform EOS logic */ |
| 950 | if (rmdemux->segment.flags & GST_SEEK_FLAG_SEGMENT) { |
| 951 | gint64 stop; |
| 952 | |
| 953 | /* for segment playback we need to post when (in stream time) |
| 954 | * we stopped, this is either stop (when set) or the duration. */ |
| 955 | if ((stop = rmdemux->segment.stop) == -1) |
| 956 | stop = rmdemux->segment.duration; |
| 957 | |
| 958 | GST_LOG_OBJECT (rmdemux, "Sending segment done, at end of segment"); |
| 959 | gst_element_post_message (GST_ELEMENT (rmdemux), |
| 960 | gst_message_new_segment_done (GST_OBJECT (rmdemux), |
| 961 | GST_FORMAT_TIME, stop)); |
Sebastian Dröge | c167d67 | 2012-08-09 11:25:39 +0200 | [diff] [blame] | 962 | gst_rmdemux_send_event (rmdemux, |
| 963 | gst_event_new_segment_done (GST_FORMAT_TIME, stop)); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 964 | } else { |
| 965 | /* normal playback, send EOS to all linked pads */ |
| 966 | GST_LOG_OBJECT (rmdemux, "Sending EOS, at end of stream"); |
| 967 | gst_rmdemux_send_event (rmdemux, gst_event_new_eos ()); |
| 968 | } |
| 969 | } else if (ret == GST_FLOW_NOT_LINKED || ret < GST_FLOW_EOS) { |
Sebastian Dröge | 40a1a6c | 2016-09-01 12:15:32 +0300 | [diff] [blame] | 970 | GST_ELEMENT_FLOW_ERROR (rmdemux, ret); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 971 | gst_rmdemux_send_event (rmdemux, gst_event_new_eos ()); |
| 972 | } |
| 973 | return; |
| 974 | } |
| 975 | } |
| 976 | |
| 977 | static gboolean |
| 978 | gst_rmdemux_fourcc_isplausible (guint32 fourcc) |
| 979 | { |
| 980 | int i; |
| 981 | |
| 982 | for (i = 0; i < 4; i++) { |
| 983 | if (!isprint ((int) ((unsigned char *) (&fourcc))[i])) { |
| 984 | return FALSE; |
| 985 | } |
| 986 | } |
| 987 | return TRUE; |
| 988 | } |
| 989 | |
| 990 | static GstFlowReturn |
| 991 | gst_rmdemux_chain (GstPad * pad, GstObject * parent, GstBuffer * buffer) |
| 992 | { |
| 993 | GstFlowReturn ret = GST_FLOW_OK; |
| 994 | const guint8 *data; |
| 995 | guint16 version; |
| 996 | guint avail; |
| 997 | |
| 998 | GstRMDemux *rmdemux = GST_RMDEMUX (parent); |
| 999 | |
| 1000 | if (rmdemux->base_ts == -1) { |
Sebastian Dröge | a60e713 | 2012-09-14 10:36:23 +0200 | [diff] [blame] | 1001 | if (GST_BUFFER_DTS_IS_VALID (buffer)) |
| 1002 | rmdemux->base_ts = GST_BUFFER_DTS (buffer); |
| 1003 | else |
| 1004 | rmdemux->base_ts = GST_BUFFER_PTS (buffer); |
| 1005 | |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 1006 | GST_LOG_OBJECT (rmdemux, "base_ts %" GST_TIME_FORMAT, |
| 1007 | GST_TIME_ARGS (rmdemux->base_ts)); |
| 1008 | } |
| 1009 | |
| 1010 | gst_adapter_push (rmdemux->adapter, buffer); |
| 1011 | |
| 1012 | GST_LOG_OBJECT (rmdemux, "Chaining buffer of size %" G_GSIZE_FORMAT, |
| 1013 | gst_buffer_get_size (buffer)); |
| 1014 | |
| 1015 | while (TRUE) { |
| 1016 | avail = gst_adapter_available (rmdemux->adapter); |
| 1017 | |
| 1018 | GST_LOG_OBJECT (rmdemux, "looping in chain, avail %u", avail); |
| 1019 | switch (rmdemux->state) { |
| 1020 | case RMDEMUX_STATE_HEADER: |
| 1021 | { |
| 1022 | if (gst_adapter_available (rmdemux->adapter) < HEADER_SIZE) |
| 1023 | goto unlock; |
| 1024 | |
| 1025 | data = gst_adapter_map (rmdemux->adapter, HEADER_SIZE); |
| 1026 | |
| 1027 | rmdemux->object_id = RMDEMUX_FOURCC_GET (data + 0); |
| 1028 | rmdemux->size = RMDEMUX_GUINT32_GET (data + 4) - HEADER_SIZE; |
| 1029 | rmdemux->object_version = RMDEMUX_GUINT16_GET (data + 8); |
| 1030 | |
| 1031 | /* Sanity-check. We assume that the FOURCC is printable ASCII */ |
| 1032 | if (!gst_rmdemux_fourcc_isplausible (rmdemux->object_id)) { |
| 1033 | /* Failed. Remain in HEADER state, try again... We flush only |
| 1034 | * the actual FOURCC, not the entire header, because we could |
| 1035 | * need to resync anywhere at all... really, this should never |
| 1036 | * happen. */ |
| 1037 | GST_WARNING_OBJECT (rmdemux, "Bogus looking header, unprintable " |
| 1038 | "FOURCC"); |
| 1039 | gst_adapter_unmap (rmdemux->adapter); |
| 1040 | gst_adapter_flush (rmdemux->adapter, 4); |
| 1041 | |
| 1042 | break; |
| 1043 | } |
| 1044 | |
| 1045 | GST_LOG_OBJECT (rmdemux, "header found with object_id=%" |
| 1046 | GST_FOURCC_FORMAT |
| 1047 | " size=%08x object_version=%d", |
| 1048 | GST_FOURCC_ARGS (rmdemux->object_id), rmdemux->size, |
| 1049 | rmdemux->object_version); |
| 1050 | |
| 1051 | gst_adapter_unmap (rmdemux->adapter); |
| 1052 | gst_adapter_flush (rmdemux->adapter, HEADER_SIZE); |
| 1053 | |
| 1054 | switch (rmdemux->object_id) { |
| 1055 | case GST_MAKE_FOURCC ('.', 'R', 'M', 'F'): |
| 1056 | rmdemux->state = RMDEMUX_STATE_HEADER_RMF; |
| 1057 | break; |
| 1058 | case GST_MAKE_FOURCC ('P', 'R', 'O', 'P'): |
| 1059 | rmdemux->state = RMDEMUX_STATE_HEADER_PROP; |
| 1060 | break; |
| 1061 | case GST_MAKE_FOURCC ('M', 'D', 'P', 'R'): |
| 1062 | rmdemux->state = RMDEMUX_STATE_HEADER_MDPR; |
| 1063 | break; |
| 1064 | case GST_MAKE_FOURCC ('I', 'N', 'D', 'X'): |
| 1065 | rmdemux->state = RMDEMUX_STATE_HEADER_INDX; |
| 1066 | break; |
| 1067 | case GST_MAKE_FOURCC ('D', 'A', 'T', 'A'): |
| 1068 | rmdemux->state = RMDEMUX_STATE_HEADER_DATA; |
| 1069 | break; |
| 1070 | case GST_MAKE_FOURCC ('C', 'O', 'N', 'T'): |
| 1071 | rmdemux->state = RMDEMUX_STATE_HEADER_CONT; |
| 1072 | break; |
| 1073 | default: |
| 1074 | rmdemux->state = RMDEMUX_STATE_HEADER_UNKNOWN; |
| 1075 | break; |
| 1076 | } |
| 1077 | break; |
| 1078 | } |
| 1079 | case RMDEMUX_STATE_HEADER_UNKNOWN: |
| 1080 | { |
| 1081 | if (gst_adapter_available (rmdemux->adapter) < rmdemux->size) |
| 1082 | goto unlock; |
| 1083 | |
| 1084 | GST_WARNING_OBJECT (rmdemux, "Unknown object_id %" GST_FOURCC_FORMAT, |
| 1085 | GST_FOURCC_ARGS (rmdemux->object_id)); |
| 1086 | |
| 1087 | gst_adapter_flush (rmdemux->adapter, rmdemux->size); |
| 1088 | rmdemux->state = RMDEMUX_STATE_HEADER; |
| 1089 | break; |
| 1090 | } |
| 1091 | case RMDEMUX_STATE_HEADER_RMF: |
| 1092 | { |
| 1093 | if (gst_adapter_available (rmdemux->adapter) < rmdemux->size) |
| 1094 | goto unlock; |
| 1095 | |
| 1096 | if ((rmdemux->object_version == 0) || (rmdemux->object_version == 1)) { |
| 1097 | data = gst_adapter_map (rmdemux->adapter, rmdemux->size); |
| 1098 | gst_rmdemux_parse__rmf (rmdemux, data, rmdemux->size); |
| 1099 | gst_adapter_unmap (rmdemux->adapter); |
| 1100 | gst_adapter_flush (rmdemux->adapter, rmdemux->size); |
| 1101 | } else { |
| 1102 | gst_adapter_flush (rmdemux->adapter, rmdemux->size); |
| 1103 | } |
| 1104 | rmdemux->state = RMDEMUX_STATE_HEADER; |
| 1105 | break; |
| 1106 | } |
| 1107 | case RMDEMUX_STATE_HEADER_PROP: |
| 1108 | { |
| 1109 | if (gst_adapter_available (rmdemux->adapter) < rmdemux->size) |
| 1110 | goto unlock; |
| 1111 | |
| 1112 | data = gst_adapter_map (rmdemux->adapter, rmdemux->size); |
| 1113 | gst_rmdemux_parse_prop (rmdemux, data, rmdemux->size); |
| 1114 | gst_adapter_unmap (rmdemux->adapter); |
| 1115 | gst_adapter_flush (rmdemux->adapter, rmdemux->size); |
| 1116 | |
| 1117 | rmdemux->state = RMDEMUX_STATE_HEADER; |
| 1118 | break; |
| 1119 | } |
| 1120 | case RMDEMUX_STATE_HEADER_MDPR: |
| 1121 | { |
| 1122 | if (gst_adapter_available (rmdemux->adapter) < rmdemux->size) |
| 1123 | goto unlock; |
| 1124 | |
| 1125 | data = gst_adapter_map (rmdemux->adapter, rmdemux->size); |
| 1126 | gst_rmdemux_parse_mdpr (rmdemux, data, rmdemux->size); |
| 1127 | gst_adapter_unmap (rmdemux->adapter); |
| 1128 | gst_adapter_flush (rmdemux->adapter, rmdemux->size); |
| 1129 | |
| 1130 | rmdemux->state = RMDEMUX_STATE_HEADER; |
| 1131 | break; |
| 1132 | } |
| 1133 | case RMDEMUX_STATE_HEADER_CONT: |
| 1134 | { |
| 1135 | if (gst_adapter_available (rmdemux->adapter) < rmdemux->size) |
| 1136 | goto unlock; |
| 1137 | |
| 1138 | data = gst_adapter_map (rmdemux->adapter, rmdemux->size); |
| 1139 | gst_rmdemux_parse_cont (rmdemux, data, rmdemux->size); |
| 1140 | gst_adapter_unmap (rmdemux->adapter); |
| 1141 | gst_adapter_flush (rmdemux->adapter, rmdemux->size); |
| 1142 | |
| 1143 | rmdemux->state = RMDEMUX_STATE_HEADER; |
| 1144 | break; |
| 1145 | } |
| 1146 | case RMDEMUX_STATE_HEADER_DATA: |
| 1147 | { |
| 1148 | /* If we haven't already done so then signal there are no more pads */ |
| 1149 | if (!rmdemux->have_pads) { |
| 1150 | GST_LOG_OBJECT (rmdemux, "no more pads"); |
| 1151 | gst_element_no_more_pads (GST_ELEMENT (rmdemux)); |
| 1152 | rmdemux->have_pads = TRUE; |
| 1153 | } |
| 1154 | |
| 1155 | /* The actual header is only 8 bytes */ |
| 1156 | rmdemux->size = DATA_SIZE; |
| 1157 | GST_LOG_OBJECT (rmdemux, "data available %" G_GSIZE_FORMAT, |
| 1158 | gst_adapter_available (rmdemux->adapter)); |
| 1159 | if (gst_adapter_available (rmdemux->adapter) < rmdemux->size) |
| 1160 | goto unlock; |
| 1161 | |
| 1162 | data = gst_adapter_map (rmdemux->adapter, rmdemux->size); |
| 1163 | gst_rmdemux_parse_data (rmdemux, data, rmdemux->size); |
| 1164 | gst_adapter_unmap (rmdemux->adapter); |
| 1165 | gst_adapter_flush (rmdemux->adapter, rmdemux->size); |
| 1166 | |
| 1167 | rmdemux->state = RMDEMUX_STATE_DATA_PACKET; |
| 1168 | break; |
| 1169 | } |
| 1170 | case RMDEMUX_STATE_HEADER_INDX: |
| 1171 | { |
| 1172 | if (gst_adapter_available (rmdemux->adapter) < rmdemux->size) |
| 1173 | goto unlock; |
| 1174 | |
| 1175 | data = gst_adapter_map (rmdemux->adapter, rmdemux->size); |
| 1176 | rmdemux->size = gst_rmdemux_parse_indx (rmdemux, data, rmdemux->size); |
| 1177 | /* Only flush the header */ |
| 1178 | gst_adapter_unmap (rmdemux->adapter); |
| 1179 | gst_adapter_flush (rmdemux->adapter, HEADER_SIZE); |
| 1180 | |
| 1181 | rmdemux->state = RMDEMUX_STATE_INDX_DATA; |
| 1182 | break; |
| 1183 | } |
| 1184 | case RMDEMUX_STATE_INDX_DATA: |
| 1185 | { |
| 1186 | /* There's not always an data to get... */ |
| 1187 | if (rmdemux->size > 0) { |
| 1188 | if (gst_adapter_available (rmdemux->adapter) < rmdemux->size) |
| 1189 | goto unlock; |
| 1190 | |
| 1191 | data = gst_adapter_map (rmdemux->adapter, rmdemux->size); |
| 1192 | gst_rmdemux_parse_indx_data (rmdemux, data, rmdemux->size); |
| 1193 | gst_adapter_unmap (rmdemux->adapter); |
| 1194 | gst_adapter_flush (rmdemux->adapter, rmdemux->size); |
| 1195 | } |
| 1196 | |
| 1197 | rmdemux->state = RMDEMUX_STATE_HEADER; |
| 1198 | break; |
| 1199 | } |
| 1200 | case RMDEMUX_STATE_DATA_PACKET: |
| 1201 | { |
| 1202 | guint8 header[4]; |
| 1203 | |
| 1204 | if (gst_adapter_available (rmdemux->adapter) < 2) |
| 1205 | goto unlock; |
| 1206 | |
| 1207 | gst_adapter_copy (rmdemux->adapter, header, 0, 2); |
| 1208 | version = RMDEMUX_GUINT16_GET (header); |
| 1209 | GST_LOG_OBJECT (rmdemux, "Data packet with version=%d", version); |
| 1210 | |
| 1211 | if (version == 0 || version == 1) { |
| 1212 | guint16 length; |
| 1213 | |
| 1214 | if (gst_adapter_available (rmdemux->adapter) < 4) |
| 1215 | goto unlock; |
| 1216 | |
| 1217 | gst_adapter_copy (rmdemux->adapter, header, 0, 4); |
| 1218 | |
| 1219 | length = RMDEMUX_GUINT16_GET (header + 2); |
| 1220 | GST_LOG_OBJECT (rmdemux, "Got length %d", length); |
| 1221 | |
| 1222 | if (length < 4) { |
| 1223 | GST_LOG_OBJECT (rmdemux, "length too small, dropping"); |
| 1224 | /* Invalid, just drop it */ |
| 1225 | gst_adapter_flush (rmdemux->adapter, 4); |
| 1226 | } else { |
| 1227 | GstBuffer *buffer; |
| 1228 | |
| 1229 | avail = gst_adapter_available (rmdemux->adapter); |
| 1230 | if (avail < length) |
| 1231 | goto unlock; |
| 1232 | |
| 1233 | GST_LOG_OBJECT (rmdemux, "we have %u available and we needed %d", |
| 1234 | avail, length); |
| 1235 | |
| 1236 | /* flush version and length */ |
| 1237 | gst_adapter_flush (rmdemux->adapter, 4); |
| 1238 | length -= 4; |
| 1239 | |
| 1240 | buffer = gst_adapter_take_buffer (rmdemux->adapter, length); |
| 1241 | |
| 1242 | ret = gst_rmdemux_parse_packet (rmdemux, buffer, version); |
| 1243 | rmdemux->chunk_index++; |
| 1244 | } |
| 1245 | |
| 1246 | if (rmdemux->chunk_index == rmdemux->n_chunks || length == 0) |
| 1247 | rmdemux->state = RMDEMUX_STATE_HEADER; |
| 1248 | } else { |
| 1249 | /* Stream done */ |
| 1250 | gst_adapter_flush (rmdemux->adapter, 2); |
| 1251 | |
| 1252 | if (rmdemux->data_offset == 0) { |
| 1253 | GST_LOG_OBJECT (rmdemux, |
| 1254 | "No further data, internal demux state EOS"); |
| 1255 | rmdemux->state = RMDEMUX_STATE_EOS; |
| 1256 | } else |
| 1257 | rmdemux->state = RMDEMUX_STATE_HEADER; |
| 1258 | } |
| 1259 | break; |
| 1260 | } |
| 1261 | case RMDEMUX_STATE_EOS: |
| 1262 | gst_rmdemux_send_event (rmdemux, gst_event_new_eos ()); |
| 1263 | goto unlock; |
| 1264 | default: |
| 1265 | GST_WARNING_OBJECT (rmdemux, "Unhandled state %d", rmdemux->state); |
| 1266 | goto unlock; |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | unlock: |
| 1271 | return ret; |
| 1272 | } |
| 1273 | |
| 1274 | static GstRMDemuxStream * |
| 1275 | gst_rmdemux_get_stream_by_id (GstRMDemux * rmdemux, int id) |
| 1276 | { |
| 1277 | GSList *cur; |
| 1278 | |
| 1279 | for (cur = rmdemux->streams; cur; cur = cur->next) { |
| 1280 | GstRMDemuxStream *stream = cur->data; |
| 1281 | |
| 1282 | if (stream->id == id) { |
| 1283 | return stream; |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | return NULL; |
| 1288 | } |
| 1289 | |
| 1290 | static void |
| 1291 | gst_rmdemux_send_event (GstRMDemux * rmdemux, GstEvent * event) |
| 1292 | { |
| 1293 | GSList *cur; |
| 1294 | |
| 1295 | for (cur = rmdemux->streams; cur; cur = cur->next) { |
| 1296 | GstRMDemuxStream *stream = cur->data; |
| 1297 | |
| 1298 | GST_DEBUG_OBJECT (rmdemux, "Pushing %s event on pad %s", |
| 1299 | GST_EVENT_TYPE_NAME (event), GST_PAD_NAME (stream->pad)); |
| 1300 | |
| 1301 | switch (GST_EVENT_TYPE (event)) { |
| 1302 | case GST_EVENT_FLUSH_STOP: |
| 1303 | stream->last_ts = -1; |
| 1304 | stream->next_ts = -1; |
| 1305 | stream->last_seq = -1; |
| 1306 | stream->next_seq = -1; |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 1307 | break; |
| 1308 | default: |
| 1309 | break; |
| 1310 | } |
| 1311 | gst_event_ref (event); |
| 1312 | gst_pad_push_event (stream->pad, event); |
| 1313 | } |
| 1314 | gst_event_unref (event); |
| 1315 | } |
| 1316 | |
| 1317 | static void |
| 1318 | gst_rmdemux_add_stream (GstRMDemux * rmdemux, GstRMDemuxStream * stream) |
| 1319 | { |
| 1320 | GstCaps *stream_caps = NULL; |
| 1321 | const gchar *codec_tag = NULL; |
| 1322 | gchar *codec_name = NULL; |
Sebastian Dröge | c167d67 | 2012-08-09 11:25:39 +0200 | [diff] [blame] | 1323 | gchar *stream_id; |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 1324 | int version = 0; |
| 1325 | |
| 1326 | if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) { |
| 1327 | char *name = g_strdup_printf ("video_%u", rmdemux->n_video_streams); |
| 1328 | |
| 1329 | stream->pad = |
| 1330 | gst_pad_new_from_static_template (&gst_rmdemux_videosrc_template, name); |
| 1331 | g_free (name); |
| 1332 | |
| 1333 | codec_tag = GST_TAG_VIDEO_CODEC; |
| 1334 | |
| 1335 | switch (stream->fourcc) { |
| 1336 | case GST_RM_VDO_RV10: |
| 1337 | version = 1; |
| 1338 | break; |
| 1339 | case GST_RM_VDO_RV20: |
| 1340 | version = 2; |
| 1341 | break; |
| 1342 | case GST_RM_VDO_RV30: |
| 1343 | version = 3; |
| 1344 | break; |
| 1345 | case GST_RM_VDO_RV40: |
| 1346 | version = 4; |
| 1347 | break; |
| 1348 | default: |
| 1349 | stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc", |
| 1350 | "fourcc", G_TYPE_UINT, stream->fourcc, NULL); |
| 1351 | GST_WARNING_OBJECT (rmdemux, |
| 1352 | "Unknown video FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)", |
| 1353 | GST_FOURCC_ARGS (stream->fourcc), stream->fourcc); |
| 1354 | } |
| 1355 | |
| 1356 | if (version) { |
| 1357 | stream_caps = |
| 1358 | gst_caps_new_simple ("video/x-pn-realvideo", "rmversion", G_TYPE_INT, |
| 1359 | (int) version, |
| 1360 | "format", G_TYPE_INT, |
| 1361 | (int) stream->format, |
| 1362 | "subformat", G_TYPE_INT, (int) stream->subformat, NULL); |
| 1363 | } |
| 1364 | |
| 1365 | if (stream_caps) { |
| 1366 | gst_caps_set_simple (stream_caps, |
| 1367 | "width", G_TYPE_INT, stream->width, |
| 1368 | "height", G_TYPE_INT, stream->height, |
| 1369 | "framerate", GST_TYPE_FRACTION, stream->framerate_numerator, |
| 1370 | stream->framerate_denominator, NULL); |
| 1371 | } |
| 1372 | rmdemux->n_video_streams++; |
| 1373 | |
| 1374 | } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) { |
| 1375 | char *name = g_strdup_printf ("audio_%u", rmdemux->n_audio_streams); |
| 1376 | |
| 1377 | stream->pad = |
| 1378 | gst_pad_new_from_static_template (&gst_rmdemux_audiosrc_template, name); |
| 1379 | GST_LOG_OBJECT (rmdemux, "Created audio pad \"%s\"", name); |
| 1380 | g_free (name); |
| 1381 | |
| 1382 | codec_tag = GST_TAG_AUDIO_CODEC; |
| 1383 | |
| 1384 | switch (stream->fourcc) { |
| 1385 | /* Older RealAudio Codecs */ |
| 1386 | case GST_RM_AUD_14_4: |
| 1387 | version = 1; |
| 1388 | break; |
| 1389 | |
| 1390 | case GST_RM_AUD_28_8: |
| 1391 | version = 2; |
| 1392 | break; |
| 1393 | |
| 1394 | /* DolbyNet (Dolby AC3, low bitrate) */ |
| 1395 | case GST_RM_AUD_DNET: |
| 1396 | stream_caps = |
| 1397 | gst_caps_new_simple ("audio/x-ac3", "rate", G_TYPE_INT, |
| 1398 | (int) stream->rate, NULL); |
| 1399 | stream->needs_descrambling = TRUE; |
| 1400 | stream->subpackets_needed = 1; |
| 1401 | stream->subpackets = NULL; |
| 1402 | break; |
| 1403 | |
| 1404 | /* MPEG-4 based */ |
| 1405 | case GST_RM_AUD_RAAC: |
| 1406 | case GST_RM_AUD_RACP: |
| 1407 | stream_caps = |
| 1408 | gst_caps_new_simple ("audio/mpeg", "mpegversion", G_TYPE_INT, |
| 1409 | (int) 4, "framed", G_TYPE_BOOLEAN, TRUE, NULL); |
| 1410 | if (stream->extra_data_size > 0) { |
| 1411 | /* strip off an unknown byte in the extra data */ |
| 1412 | stream->extra_data_size--; |
| 1413 | stream->extra_data++; |
| 1414 | } |
| 1415 | stream->needs_descrambling = TRUE; |
| 1416 | stream->subpackets_needed = 1; |
| 1417 | stream->subpackets = NULL; |
| 1418 | break; |
| 1419 | |
| 1420 | /* Sony ATRAC3 */ |
| 1421 | case GST_RM_AUD_ATRC: |
| 1422 | stream_caps = gst_caps_new_empty_simple ("audio/x-vnd.sony.atrac3"); |
| 1423 | stream->needs_descrambling = TRUE; |
| 1424 | stream->subpackets_needed = stream->height; |
| 1425 | stream->subpackets = NULL; |
| 1426 | break; |
| 1427 | |
| 1428 | /* RealAudio G2 audio */ |
| 1429 | case GST_RM_AUD_COOK: |
| 1430 | version = 8; |
| 1431 | stream->needs_descrambling = TRUE; |
| 1432 | stream->subpackets_needed = stream->height; |
| 1433 | stream->subpackets = NULL; |
| 1434 | break; |
| 1435 | |
| 1436 | /* RALF is lossless */ |
| 1437 | case GST_RM_AUD_RALF: |
| 1438 | GST_DEBUG_OBJECT (rmdemux, "RALF"); |
| 1439 | stream_caps = gst_caps_new_empty_simple ("audio/x-ralf-mpeg4-generic"); |
| 1440 | break; |
| 1441 | |
| 1442 | case GST_RM_AUD_SIPR: |
| 1443 | |
| 1444 | if (stream->flavor > 3) { |
| 1445 | GST_WARNING_OBJECT (rmdemux, "bad SIPR flavor %d, freeing it", |
| 1446 | stream->flavor); |
Sebastian Dröge | d8a669d | 2015-08-19 14:01:25 +0300 | [diff] [blame] | 1447 | g_object_unref (stream->pad); |
| 1448 | gst_rmdemux_free_stream (rmdemux, stream); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 1449 | goto beach; |
| 1450 | } |
| 1451 | |
| 1452 | GST_DEBUG_OBJECT (rmdemux, "SIPR"); |
| 1453 | stream_caps = gst_caps_new_empty_simple ("audio/x-sipro"); |
| 1454 | stream->needs_descrambling = TRUE; |
| 1455 | stream->subpackets_needed = stream->height; |
| 1456 | stream->subpackets = NULL; |
| 1457 | stream->leaf_size = sipr_subpk_size[stream->flavor]; |
| 1458 | |
| 1459 | break; |
| 1460 | |
| 1461 | default: |
| 1462 | stream_caps = gst_caps_new_simple ("video/x-unknown-fourcc", |
| 1463 | "fourcc", G_TYPE_UINT, stream->fourcc, NULL); |
| 1464 | GST_WARNING_OBJECT (rmdemux, |
| 1465 | "Unknown audio FOURCC code \"%" GST_FOURCC_FORMAT "\" (%08x)", |
| 1466 | GST_FOURCC_ARGS (stream->fourcc), stream->fourcc); |
| 1467 | break; |
| 1468 | } |
| 1469 | |
| 1470 | if (version) { |
| 1471 | stream_caps = |
| 1472 | gst_caps_new_simple ("audio/x-pn-realaudio", "raversion", G_TYPE_INT, |
| 1473 | (int) version, NULL); |
| 1474 | } |
| 1475 | |
| 1476 | if (stream_caps) { |
| 1477 | gst_caps_set_simple (stream_caps, |
| 1478 | "flavor", G_TYPE_INT, (int) stream->flavor, |
| 1479 | "rate", G_TYPE_INT, (int) stream->rate, |
| 1480 | "channels", G_TYPE_INT, (int) stream->n_channels, |
| 1481 | "width", G_TYPE_INT, (int) stream->sample_width, |
| 1482 | "leaf_size", G_TYPE_INT, (int) stream->leaf_size, |
| 1483 | "packet_size", G_TYPE_INT, (int) stream->packet_size, |
| 1484 | "bitrate", G_TYPE_INT, (int) stream->bitrate, |
| 1485 | "height", G_TYPE_INT, (int) stream->height, NULL); |
| 1486 | } |
| 1487 | rmdemux->n_audio_streams++; |
| 1488 | } else { |
| 1489 | GST_WARNING_OBJECT (rmdemux, "not adding stream of type %d, freeing it", |
| 1490 | stream->subtype); |
Sebastian Dröge | d8a669d | 2015-08-19 14:01:25 +0300 | [diff] [blame] | 1491 | gst_rmdemux_free_stream (rmdemux, stream); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 1492 | goto beach; |
| 1493 | } |
| 1494 | |
| 1495 | GST_PAD_ELEMENT_PRIVATE (stream->pad) = stream; |
| 1496 | rmdemux->streams = g_slist_append (rmdemux->streams, stream); |
| 1497 | GST_LOG_OBJECT (rmdemux, "n_streams is now %d", |
| 1498 | g_slist_length (rmdemux->streams)); |
| 1499 | |
| 1500 | GST_LOG ("stream->pad = %p, stream_caps = %" GST_PTR_FORMAT, stream->pad, |
| 1501 | stream_caps); |
| 1502 | |
| 1503 | if (stream->pad && stream_caps) { |
Sebastian Dröge | 7efeeb0 | 2013-07-30 08:51:53 +0200 | [diff] [blame] | 1504 | GstEvent *event; |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 1505 | |
| 1506 | GST_LOG_OBJECT (rmdemux, "%d bytes of extra data for stream %s", |
| 1507 | stream->extra_data_size, GST_PAD_NAME (stream->pad)); |
| 1508 | |
| 1509 | /* add codec_data if there is any */ |
| 1510 | if (stream->extra_data_size > 0) { |
| 1511 | GstBuffer *buffer; |
| 1512 | |
| 1513 | buffer = gst_buffer_new_and_alloc (stream->extra_data_size); |
| 1514 | gst_buffer_fill (buffer, 0, stream->extra_data, stream->extra_data_size); |
| 1515 | |
| 1516 | gst_caps_set_simple (stream_caps, "codec_data", GST_TYPE_BUFFER, |
| 1517 | buffer, NULL); |
| 1518 | |
| 1519 | gst_buffer_unref (buffer); |
| 1520 | } |
| 1521 | |
| 1522 | gst_pad_use_fixed_caps (stream->pad); |
| 1523 | |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 1524 | gst_pad_set_event_function (stream->pad, |
| 1525 | GST_DEBUG_FUNCPTR (gst_rmdemux_src_event)); |
| 1526 | gst_pad_set_query_function (stream->pad, |
| 1527 | GST_DEBUG_FUNCPTR (gst_rmdemux_src_query)); |
| 1528 | |
| 1529 | GST_DEBUG_OBJECT (rmdemux, "adding pad %s with caps %" GST_PTR_FORMAT |
| 1530 | ", stream_id=%d", GST_PAD_NAME (stream->pad), stream_caps, stream->id); |
| 1531 | gst_pad_set_active (stream->pad, TRUE); |
Sebastian Dröge | c167d67 | 2012-08-09 11:25:39 +0200 | [diff] [blame] | 1532 | |
| 1533 | stream_id = |
| 1534 | gst_pad_create_stream_id_printf (stream->pad, |
Sebastian Dröge | 6bf128e | 2013-07-14 12:07:04 +0200 | [diff] [blame] | 1535 | GST_ELEMENT_CAST (rmdemux), "%03u", stream->id); |
Sebastian Dröge | 7efeeb0 | 2013-07-30 08:51:53 +0200 | [diff] [blame] | 1536 | |
| 1537 | event = |
| 1538 | gst_pad_get_sticky_event (rmdemux->sinkpad, GST_EVENT_STREAM_START, 0); |
| 1539 | if (event) { |
| 1540 | if (gst_event_parse_group_id (event, &rmdemux->group_id)) |
| 1541 | rmdemux->have_group_id = TRUE; |
| 1542 | else |
| 1543 | rmdemux->have_group_id = FALSE; |
| 1544 | gst_event_unref (event); |
| 1545 | } else if (!rmdemux->have_group_id) { |
| 1546 | rmdemux->have_group_id = TRUE; |
| 1547 | rmdemux->group_id = gst_util_group_id_next (); |
| 1548 | } |
| 1549 | |
| 1550 | event = gst_event_new_stream_start (stream_id); |
| 1551 | if (rmdemux->have_group_id) |
| 1552 | gst_event_set_group_id (event, rmdemux->group_id); |
| 1553 | |
| 1554 | gst_pad_push_event (stream->pad, event); |
Sebastian Dröge | c167d67 | 2012-08-09 11:25:39 +0200 | [diff] [blame] | 1555 | g_free (stream_id); |
| 1556 | |
| 1557 | gst_pad_set_caps (stream->pad, stream_caps); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 1558 | |
| 1559 | codec_name = gst_pb_utils_get_codec_description (stream_caps); |
| 1560 | |
| 1561 | /* save for later, we must send the tags after the newsegment event */ |
| 1562 | if (codec_tag != NULL && codec_name != NULL) { |
| 1563 | if (stream->pending_tags == NULL) |
| 1564 | stream->pending_tags = gst_tag_list_new_empty (); |
| 1565 | gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_KEEP, |
| 1566 | codec_tag, codec_name, NULL); |
| 1567 | g_free (codec_name); |
| 1568 | } |
Sebastian Dröge | c167d67 | 2012-08-09 11:25:39 +0200 | [diff] [blame] | 1569 | gst_element_add_pad (GST_ELEMENT_CAST (rmdemux), stream->pad); |
Sebastian Dröge | c679a0b | 2014-06-22 18:11:42 +0200 | [diff] [blame] | 1570 | gst_flow_combiner_add_pad (rmdemux->flowcombiner, stream->pad); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | beach: |
| 1574 | |
| 1575 | if (stream_caps) |
| 1576 | gst_caps_unref (stream_caps); |
| 1577 | } |
| 1578 | |
| 1579 | static int |
| 1580 | re_skip_pascal_string (const guint8 * ptr) |
| 1581 | { |
| 1582 | int length; |
| 1583 | |
| 1584 | length = ptr[0]; |
| 1585 | |
| 1586 | return length + 1; |
| 1587 | } |
| 1588 | |
| 1589 | static void |
| 1590 | gst_rmdemux_parse__rmf (GstRMDemux * rmdemux, const guint8 * data, int length) |
| 1591 | { |
| 1592 | GST_LOG_OBJECT (rmdemux, "file_version: %d", RMDEMUX_GUINT32_GET (data)); |
| 1593 | GST_LOG_OBJECT (rmdemux, "num_headers: %d", RMDEMUX_GUINT32_GET (data + 4)); |
| 1594 | } |
| 1595 | |
| 1596 | static void |
| 1597 | gst_rmdemux_parse_prop (GstRMDemux * rmdemux, const guint8 * data, int length) |
| 1598 | { |
| 1599 | GST_LOG_OBJECT (rmdemux, "max bitrate: %d", RMDEMUX_GUINT32_GET (data)); |
| 1600 | GST_LOG_OBJECT (rmdemux, "avg bitrate: %d", RMDEMUX_GUINT32_GET (data + 4)); |
| 1601 | GST_LOG_OBJECT (rmdemux, "max packet size: %d", |
| 1602 | RMDEMUX_GUINT32_GET (data + 8)); |
| 1603 | rmdemux->avg_packet_size = RMDEMUX_GUINT32_GET (data + 12); |
| 1604 | GST_LOG_OBJECT (rmdemux, "avg packet size: %d", rmdemux->avg_packet_size); |
| 1605 | rmdemux->num_packets = RMDEMUX_GUINT32_GET (data + 16); |
| 1606 | GST_LOG_OBJECT (rmdemux, "number of packets: %d", rmdemux->num_packets); |
| 1607 | |
| 1608 | GST_LOG_OBJECT (rmdemux, "duration: %d", RMDEMUX_GUINT32_GET (data + 20)); |
| 1609 | rmdemux->duration = RMDEMUX_GUINT32_GET (data + 20) * GST_MSECOND; |
| 1610 | |
| 1611 | GST_LOG_OBJECT (rmdemux, "preroll: %d", RMDEMUX_GUINT32_GET (data + 24)); |
| 1612 | rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 28); |
| 1613 | GST_LOG_OBJECT (rmdemux, "offset of INDX section: 0x%08x", |
| 1614 | rmdemux->index_offset); |
| 1615 | rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 32); |
| 1616 | GST_LOG_OBJECT (rmdemux, "offset of DATA section: 0x%08x", |
| 1617 | rmdemux->data_offset); |
| 1618 | GST_LOG_OBJECT (rmdemux, "n streams: %d", RMDEMUX_GUINT16_GET (data + 36)); |
| 1619 | GST_LOG_OBJECT (rmdemux, "flags: 0x%04x", RMDEMUX_GUINT16_GET (data + 38)); |
| 1620 | } |
| 1621 | |
| 1622 | static void |
| 1623 | gst_rmdemux_parse_mdpr (GstRMDemux * rmdemux, const guint8 * data, int length) |
| 1624 | { |
| 1625 | GstRMDemuxStream *stream; |
| 1626 | char *stream1_type_string; |
| 1627 | char *stream2_type_string; |
| 1628 | guint str_len = 0; |
| 1629 | int stream_type; |
| 1630 | int offset; |
| 1631 | guint32 max_bitrate; |
| 1632 | guint32 avg_bitrate; |
| 1633 | |
| 1634 | stream = g_new0 (GstRMDemuxStream, 1); |
| 1635 | |
| 1636 | stream->id = RMDEMUX_GUINT16_GET (data); |
| 1637 | stream->index = NULL; |
| 1638 | stream->seek_offset = 0; |
| 1639 | stream->last_ts = -1; |
| 1640 | stream->next_ts = -1; |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 1641 | stream->discont = TRUE; |
| 1642 | stream->adapter = gst_adapter_new (); |
| 1643 | GST_LOG_OBJECT (rmdemux, "stream_number=%d", stream->id); |
| 1644 | |
| 1645 | /* parse the bitrates */ |
| 1646 | max_bitrate = RMDEMUX_GUINT32_GET (data + 2); |
| 1647 | avg_bitrate = RMDEMUX_GUINT32_GET (data + 6); |
| 1648 | stream->bitrate = avg_bitrate; |
| 1649 | GST_LOG_OBJECT (rmdemux, "Stream max bitrate=%u", max_bitrate); |
| 1650 | GST_LOG_OBJECT (rmdemux, "Stream avg bitrate=%u", avg_bitrate); |
| 1651 | if (max_bitrate != 0) { |
| 1652 | if (stream->pending_tags == NULL) |
| 1653 | stream->pending_tags = gst_tag_list_new_empty (); |
| 1654 | gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_REPLACE, |
| 1655 | GST_TAG_MAXIMUM_BITRATE, max_bitrate, NULL); |
| 1656 | } |
| 1657 | if (avg_bitrate != 0) { |
| 1658 | if (stream->pending_tags == NULL) |
| 1659 | stream->pending_tags = gst_tag_list_new_empty (); |
| 1660 | gst_tag_list_add (stream->pending_tags, GST_TAG_MERGE_REPLACE, |
| 1661 | GST_TAG_BITRATE, avg_bitrate, NULL); |
| 1662 | } |
| 1663 | |
| 1664 | offset = 30; |
| 1665 | stream1_type_string = gst_rm_utils_read_string8 (data + offset, |
| 1666 | length - offset, &str_len); |
| 1667 | offset += str_len; |
| 1668 | stream2_type_string = gst_rm_utils_read_string8 (data + offset, |
| 1669 | length - offset, &str_len); |
| 1670 | offset += str_len; |
| 1671 | |
| 1672 | /* stream1_type_string for audio and video stream is a "put_whatever_you_want" field : |
| 1673 | * observed values : |
| 1674 | * - "[The ]Video/Audio Stream" (File produced by an official Real encoder) |
| 1675 | * - "RealVideoPremierePlugIn-VIDEO/AUDIO" (File produced by Abobe Premiere) |
| 1676 | * |
| 1677 | * so, we should not rely on it to know which stream type it is |
| 1678 | */ |
| 1679 | |
| 1680 | GST_LOG_OBJECT (rmdemux, "stream type: %s", stream1_type_string); |
| 1681 | GST_LOG_OBJECT (rmdemux, "MIME type=%s", stream2_type_string); |
| 1682 | |
| 1683 | if (strcmp (stream2_type_string, "video/x-pn-realvideo") == 0) { |
| 1684 | stream_type = GST_RMDEMUX_STREAM_VIDEO; |
| 1685 | } else if (strcmp (stream2_type_string, |
| 1686 | "video/x-pn-multirate-realvideo") == 0) { |
| 1687 | stream_type = GST_RMDEMUX_STREAM_VIDEO; |
| 1688 | } else if (strcmp (stream2_type_string, "audio/x-pn-realaudio") == 0) { |
| 1689 | stream_type = GST_RMDEMUX_STREAM_AUDIO; |
| 1690 | } else if (strcmp (stream2_type_string, |
| 1691 | "audio/x-pn-multirate-realaudio") == 0) { |
| 1692 | stream_type = GST_RMDEMUX_STREAM_AUDIO; |
| 1693 | } else if (strcmp (stream2_type_string, |
| 1694 | "audio/x-pn-multirate-realaudio-live") == 0) { |
| 1695 | stream_type = GST_RMDEMUX_STREAM_AUDIO; |
| 1696 | } else if (strcmp (stream2_type_string, "audio/x-ralf-mpeg4-generic") == 0) { |
| 1697 | /* Another audio type found in the real testsuite */ |
| 1698 | stream_type = GST_RMDEMUX_STREAM_AUDIO; |
| 1699 | } else if (strcmp (stream1_type_string, "") == 0 && |
| 1700 | strcmp (stream2_type_string, "logical-fileinfo") == 0) { |
| 1701 | stream_type = GST_RMDEMUX_STREAM_FILEINFO; |
| 1702 | } else { |
| 1703 | stream_type = GST_RMDEMUX_STREAM_UNKNOWN; |
| 1704 | GST_WARNING_OBJECT (rmdemux, "unknown stream type \"%s\",\"%s\"", |
| 1705 | stream1_type_string, stream2_type_string); |
| 1706 | } |
| 1707 | g_free (stream1_type_string); |
| 1708 | g_free (stream2_type_string); |
| 1709 | |
| 1710 | offset += 4; |
| 1711 | |
| 1712 | stream->subtype = stream_type; |
| 1713 | switch (stream_type) { |
| 1714 | |
| 1715 | case GST_RMDEMUX_STREAM_VIDEO: |
| 1716 | /* RV10/RV20/RV30/RV40 => video/x-pn-realvideo, version=1,2,3,4 */ |
| 1717 | stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 8); |
| 1718 | stream->width = RMDEMUX_GUINT16_GET (data + offset + 12); |
| 1719 | stream->height = RMDEMUX_GUINT16_GET (data + offset + 14); |
| 1720 | stream->rate = RMDEMUX_GUINT16_GET (data + offset + 16); |
| 1721 | stream->subformat = RMDEMUX_GUINT32_GET (data + offset + 26); |
| 1722 | stream->format = RMDEMUX_GUINT32_GET (data + offset + 30); |
| 1723 | stream->extra_data_size = length - (offset + 26); |
| 1724 | stream->extra_data = (guint8 *) data + offset + 26; |
| 1725 | /* Natural way to represent framerates here requires unsigned 32 bit |
| 1726 | * numerator, which we don't have. For the nasty case, approximate... |
| 1727 | */ |
| 1728 | { |
| 1729 | guint32 numerator = RMDEMUX_GUINT16_GET (data + offset + 22) * 65536 + |
| 1730 | RMDEMUX_GUINT16_GET (data + offset + 24); |
| 1731 | if (numerator > G_MAXINT) { |
| 1732 | stream->framerate_numerator = (gint) (numerator >> 1); |
| 1733 | stream->framerate_denominator = 32768; |
| 1734 | } else { |
| 1735 | stream->framerate_numerator = (gint) numerator; |
| 1736 | stream->framerate_denominator = 65536; |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | GST_DEBUG_OBJECT (rmdemux, |
| 1741 | "Video stream with fourcc=%" GST_FOURCC_FORMAT |
| 1742 | " width=%d height=%d rate=%d framerate=%d/%d subformat=%x format=%x extra_data_size=%d", |
| 1743 | GST_FOURCC_ARGS (stream->fourcc), stream->width, stream->height, |
| 1744 | stream->rate, stream->framerate_numerator, |
| 1745 | stream->framerate_denominator, stream->subformat, stream->format, |
| 1746 | stream->extra_data_size); |
| 1747 | break; |
| 1748 | case GST_RMDEMUX_STREAM_AUDIO:{ |
| 1749 | stream->version = RMDEMUX_GUINT16_GET (data + offset + 4); |
| 1750 | GST_INFO ("stream version = %u", stream->version); |
| 1751 | switch (stream->version) { |
| 1752 | case 3: |
| 1753 | stream->fourcc = GST_RM_AUD_14_4; |
| 1754 | stream->packet_size = 20; |
| 1755 | stream->rate = 8000; |
| 1756 | stream->n_channels = 1; |
| 1757 | stream->sample_width = 16; |
| 1758 | stream->flavor = 1; |
| 1759 | stream->leaf_size = 0; |
| 1760 | stream->height = 0; |
| 1761 | break; |
| 1762 | case 4: |
| 1763 | stream->flavor = RMDEMUX_GUINT16_GET (data + offset + 22); |
| 1764 | stream->packet_size = RMDEMUX_GUINT32_GET (data + offset + 24); |
| 1765 | /* stream->frame_size = RMDEMUX_GUINT32_GET (data + offset + 42); */ |
| 1766 | stream->leaf_size = RMDEMUX_GUINT16_GET (data + offset + 44); |
| 1767 | stream->height = RMDEMUX_GUINT16_GET (data + offset + 40); |
| 1768 | stream->rate = RMDEMUX_GUINT16_GET (data + offset + 48); |
| 1769 | stream->sample_width = RMDEMUX_GUINT16_GET (data + offset + 52); |
| 1770 | stream->n_channels = RMDEMUX_GUINT16_GET (data + offset + 54); |
| 1771 | stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 62); |
| 1772 | stream->extra_data_size = RMDEMUX_GUINT32_GET (data + offset + 69); |
| 1773 | GST_DEBUG_OBJECT (rmdemux, "%u bytes of extra codec data", |
| 1774 | stream->extra_data_size); |
| 1775 | if (length - (offset + 73) >= stream->extra_data_size) { |
| 1776 | stream->extra_data = (guint8 *) data + offset + 73; |
| 1777 | } else { |
| 1778 | GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk"); |
| 1779 | stream->extra_data_size = 0; |
| 1780 | } |
| 1781 | break; |
| 1782 | case 5: |
| 1783 | stream->flavor = RMDEMUX_GUINT16_GET (data + offset + 22); |
| 1784 | stream->packet_size = RMDEMUX_GUINT32_GET (data + offset + 24); |
| 1785 | /* stream->frame_size = RMDEMUX_GUINT32_GET (data + offset + 42); */ |
| 1786 | stream->leaf_size = RMDEMUX_GUINT16_GET (data + offset + 44); |
| 1787 | stream->height = RMDEMUX_GUINT16_GET (data + offset + 40); |
| 1788 | stream->rate = RMDEMUX_GUINT16_GET (data + offset + 54); |
| 1789 | stream->sample_width = RMDEMUX_GUINT16_GET (data + offset + 58); |
| 1790 | stream->n_channels = RMDEMUX_GUINT16_GET (data + offset + 60); |
| 1791 | stream->fourcc = RMDEMUX_FOURCC_GET (data + offset + 66); |
| 1792 | stream->extra_data_size = RMDEMUX_GUINT32_GET (data + offset + 74); |
| 1793 | GST_DEBUG_OBJECT (rmdemux, "%u bytes of extra codec data", |
| 1794 | stream->extra_data_size); |
| 1795 | if (length - (offset + 78) >= stream->extra_data_size) { |
| 1796 | stream->extra_data = (guint8 *) data + offset + 78; |
| 1797 | } else { |
| 1798 | GST_WARNING_OBJECT (rmdemux, "codec data runs beyond MDPR chunk"); |
| 1799 | stream->extra_data_size = 0; |
| 1800 | } |
| 1801 | break; |
| 1802 | default:{ |
| 1803 | GST_WARNING_OBJECT (rmdemux, "Unhandled audio stream version %d", |
| 1804 | stream->version); |
| 1805 | break; |
| 1806 | } |
| 1807 | } |
| 1808 | /* 14_4, 28_8, cook, dnet, sipr, raac, racp, ralf, atrc */ |
| 1809 | GST_DEBUG_OBJECT (rmdemux, |
| 1810 | "Audio stream with rate=%d sample_width=%d n_channels=%d", |
| 1811 | stream->rate, stream->sample_width, stream->n_channels); |
| 1812 | |
| 1813 | break; |
| 1814 | } |
| 1815 | case GST_RMDEMUX_STREAM_FILEINFO: |
| 1816 | { |
| 1817 | int element_nb; |
| 1818 | |
| 1819 | /* Length of this section */ |
| 1820 | GST_DEBUG_OBJECT (rmdemux, "length2: 0x%08x", |
| 1821 | RMDEMUX_GUINT32_GET (data + offset)); |
| 1822 | offset += 4; |
| 1823 | |
| 1824 | /* Unknown : 00 00 00 00 */ |
| 1825 | offset += 4; |
| 1826 | |
| 1827 | /* Number of variables that would follow (loop iterations) */ |
| 1828 | element_nb = RMDEMUX_GUINT32_GET (data + offset); |
| 1829 | offset += 4; |
| 1830 | |
| 1831 | while (element_nb) { |
| 1832 | /* Category Id : 00 00 00 XX 00 00 */ |
| 1833 | offset += 6; |
| 1834 | |
| 1835 | /* Variable Name */ |
| 1836 | offset += re_skip_pascal_string (data + offset); |
| 1837 | |
| 1838 | /* Variable Value Type */ |
| 1839 | /* 00 00 00 00 00 => integer/boolean, preceded by length */ |
| 1840 | /* 00 00 00 02 00 => pascal string, preceded by length, no trailing \0 */ |
| 1841 | offset += 5; |
| 1842 | |
| 1843 | /* Variable Value */ |
| 1844 | offset += re_skip_pascal_string (data + offset); |
| 1845 | |
| 1846 | element_nb--; |
| 1847 | } |
| 1848 | } |
| 1849 | break; |
| 1850 | case GST_RMDEMUX_STREAM_UNKNOWN: |
| 1851 | default: |
| 1852 | break; |
| 1853 | } |
| 1854 | |
| 1855 | gst_rmdemux_add_stream (rmdemux, stream); |
| 1856 | } |
| 1857 | |
| 1858 | static guint |
| 1859 | gst_rmdemux_parse_indx (GstRMDemux * rmdemux, const guint8 * data, int length) |
| 1860 | { |
| 1861 | int n; |
| 1862 | int id; |
| 1863 | |
| 1864 | n = RMDEMUX_GUINT32_GET (data); |
| 1865 | id = RMDEMUX_GUINT16_GET (data + 4); |
| 1866 | rmdemux->index_offset = RMDEMUX_GUINT32_GET (data + 6); |
| 1867 | |
| 1868 | GST_DEBUG_OBJECT (rmdemux, "Number of indices=%d Stream ID=%d length=%d", n, |
| 1869 | id, length); |
| 1870 | |
| 1871 | /* Point to the next index_stream */ |
| 1872 | rmdemux->index_stream = gst_rmdemux_get_stream_by_id (rmdemux, id); |
| 1873 | |
| 1874 | /* Return the length of the index */ |
| 1875 | return 14 * n; |
| 1876 | } |
| 1877 | |
| 1878 | static void |
| 1879 | gst_rmdemux_parse_indx_data (GstRMDemux * rmdemux, const guint8 * data, |
| 1880 | int length) |
| 1881 | { |
| 1882 | int i; |
| 1883 | int n; |
| 1884 | GstRMDemuxIndex *index; |
| 1885 | |
| 1886 | /* The number of index records */ |
| 1887 | n = length / 14; |
| 1888 | |
| 1889 | if (rmdemux->index_stream == NULL) |
| 1890 | return; |
| 1891 | |
| 1892 | /* don't parse the index a second time when operating pull-based and |
| 1893 | * reaching the end of the file */ |
| 1894 | if (rmdemux->index_stream->index_length > 0) { |
| 1895 | GST_DEBUG_OBJECT (rmdemux, "Already have an index for this stream"); |
| 1896 | return; |
| 1897 | } |
| 1898 | |
| 1899 | index = g_malloc (sizeof (GstRMDemuxIndex) * n); |
| 1900 | rmdemux->index_stream->index = index; |
| 1901 | rmdemux->index_stream->index_length = n; |
| 1902 | |
| 1903 | for (i = 0; i < n; i++) { |
| 1904 | index[i].timestamp = RMDEMUX_GUINT32_GET (data + 2) * GST_MSECOND; |
| 1905 | index[i].offset = RMDEMUX_GUINT32_GET (data + 6); |
| 1906 | |
| 1907 | GST_DEBUG_OBJECT (rmdemux, "Index found for timestamp=%f (at offset=%x)", |
| 1908 | gst_guint64_to_gdouble (index[i].timestamp) / GST_SECOND, |
| 1909 | index[i].offset); |
| 1910 | data += 14; |
| 1911 | } |
| 1912 | } |
| 1913 | |
| 1914 | static void |
| 1915 | gst_rmdemux_parse_data (GstRMDemux * rmdemux, const guint8 * data, int length) |
| 1916 | { |
| 1917 | rmdemux->n_chunks = RMDEMUX_GUINT32_GET (data); |
| 1918 | rmdemux->data_offset = RMDEMUX_GUINT32_GET (data + 4); |
| 1919 | rmdemux->chunk_index = 0; |
| 1920 | GST_DEBUG_OBJECT (rmdemux, "Data chunk found with %d packets " |
| 1921 | "(next data at 0x%08x)", rmdemux->n_chunks, rmdemux->data_offset); |
| 1922 | } |
| 1923 | |
| 1924 | static void |
| 1925 | gst_rmdemux_parse_cont (GstRMDemux * rmdemux, const guint8 * data, int length) |
| 1926 | { |
| 1927 | GstTagList *tags; |
| 1928 | |
| 1929 | tags = gst_rm_utils_read_tags (data, length, gst_rm_utils_read_string16); |
| 1930 | |
Sebastian Dröge | d8a669d | 2015-08-19 14:01:25 +0300 | [diff] [blame] | 1931 | if (tags) { |
| 1932 | GstTagList *old_tags = rmdemux->pending_tags; |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 1933 | |
Sebastian Dröge | d8a669d | 2015-08-19 14:01:25 +0300 | [diff] [blame] | 1934 | GST_LOG_OBJECT (rmdemux, "tags: %" GST_PTR_FORMAT, tags); |
| 1935 | |
| 1936 | rmdemux->pending_tags = |
| 1937 | gst_tag_list_merge (old_tags, tags, GST_TAG_MERGE_APPEND); |
| 1938 | |
| 1939 | gst_tag_list_unref (tags); |
| 1940 | if (old_tags) |
| 1941 | gst_tag_list_unref (old_tags); |
| 1942 | |
| 1943 | gst_tag_list_set_scope (rmdemux->pending_tags, GST_TAG_SCOPE_GLOBAL); |
| 1944 | } |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 1945 | } |
| 1946 | |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 1947 | static void |
| 1948 | gst_rmdemux_stream_clear_cached_subpackets (GstRMDemux * rmdemux, |
| 1949 | GstRMDemuxStream * stream) |
| 1950 | { |
| 1951 | if (stream->subpackets == NULL || stream->subpackets->len == 0) |
| 1952 | return; |
| 1953 | |
| 1954 | GST_DEBUG_OBJECT (rmdemux, "discarding %u previously collected subpackets", |
| 1955 | stream->subpackets->len); |
| 1956 | g_ptr_array_foreach (stream->subpackets, (GFunc) gst_mini_object_unref, NULL); |
| 1957 | g_ptr_array_set_size (stream->subpackets, 0); |
| 1958 | } |
| 1959 | |
| 1960 | static GstFlowReturn |
| 1961 | gst_rmdemux_descramble_audio (GstRMDemux * rmdemux, GstRMDemuxStream * stream) |
| 1962 | { |
| 1963 | GstFlowReturn ret = GST_FLOW_ERROR; |
| 1964 | GstBuffer *outbuf; |
| 1965 | GstMapInfo outmap; |
| 1966 | guint packet_size = stream->packet_size; |
| 1967 | guint height = stream->subpackets->len; |
| 1968 | guint leaf_size = stream->leaf_size; |
| 1969 | guint p, x; |
| 1970 | |
| 1971 | g_assert (stream->height == height); |
| 1972 | |
| 1973 | GST_LOG ("packet_size = %u, leaf_size = %u, height= %u", packet_size, |
| 1974 | leaf_size, height); |
| 1975 | |
| 1976 | outbuf = gst_buffer_new_and_alloc (height * packet_size); |
| 1977 | gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE); |
| 1978 | |
| 1979 | for (p = 0; p < height; ++p) { |
| 1980 | GstBuffer *b = g_ptr_array_index (stream->subpackets, p); |
| 1981 | GstMapInfo map; |
| 1982 | |
| 1983 | gst_buffer_map (b, &map, GST_MAP_READ); |
| 1984 | |
Sebastian Dröge | a60e713 | 2012-09-14 10:36:23 +0200 | [diff] [blame] | 1985 | if (p == 0) { |
| 1986 | GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (b); |
| 1987 | GST_BUFFER_DTS (outbuf) = GST_BUFFER_DTS (b); |
| 1988 | } |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 1989 | |
| 1990 | for (x = 0; x < packet_size / leaf_size; ++x) { |
| 1991 | guint idx; |
| 1992 | |
| 1993 | idx = height * x + ((height + 1) / 2) * (p % 2) + (p / 2); |
| 1994 | |
| 1995 | /* GST_LOG ("%3u => %3u", (height * p) + x, idx); */ |
| 1996 | memcpy (outmap.data + leaf_size * idx, map.data + leaf_size * x, |
| 1997 | leaf_size); |
| 1998 | } |
| 1999 | gst_buffer_unmap (b, &map); |
| 2000 | } |
| 2001 | gst_buffer_unmap (outbuf, &outmap); |
| 2002 | |
| 2003 | /* some decoders, such as realaudiodec, need to be fed in packet units */ |
| 2004 | for (p = 0; p < height; ++p) { |
| 2005 | GstBuffer *subbuf; |
| 2006 | |
| 2007 | subbuf = |
| 2008 | gst_buffer_copy_region (outbuf, GST_BUFFER_COPY_ALL, p * packet_size, |
| 2009 | packet_size); |
| 2010 | |
Sebastian Dröge | a60e713 | 2012-09-14 10:36:23 +0200 | [diff] [blame] | 2011 | GST_LOG_OBJECT (rmdemux, "pushing buffer dts %" GST_TIME_FORMAT ", pts %" |
| 2012 | GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_DTS (subbuf)), |
| 2013 | GST_TIME_ARGS (GST_BUFFER_PTS (subbuf))); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2014 | |
| 2015 | if (stream->discont) { |
| 2016 | GST_BUFFER_FLAG_SET (subbuf, GST_BUFFER_FLAG_DISCONT); |
| 2017 | stream->discont = FALSE; |
| 2018 | } |
| 2019 | |
| 2020 | ret = gst_pad_push (stream->pad, subbuf); |
| 2021 | if (ret != GST_FLOW_OK) |
| 2022 | break; |
| 2023 | } |
| 2024 | |
| 2025 | gst_buffer_unref (outbuf); |
| 2026 | |
| 2027 | gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream); |
| 2028 | |
| 2029 | return ret; |
| 2030 | } |
| 2031 | |
| 2032 | static GstFlowReturn |
| 2033 | gst_rmdemux_descramble_dnet_audio (GstRMDemux * rmdemux, |
| 2034 | GstRMDemuxStream * stream) |
| 2035 | { |
| 2036 | GstBuffer *buf; |
| 2037 | |
| 2038 | buf = g_ptr_array_index (stream->subpackets, 0); |
| 2039 | g_ptr_array_index (stream->subpackets, 0) = NULL; |
| 2040 | g_ptr_array_set_size (stream->subpackets, 0); |
| 2041 | |
| 2042 | buf = gst_rm_utils_descramble_dnet_buffer (buf); |
| 2043 | |
| 2044 | if (stream->discont) { |
| 2045 | GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT); |
| 2046 | stream->discont = FALSE; |
| 2047 | } |
| 2048 | return gst_pad_push (stream->pad, buf); |
| 2049 | } |
| 2050 | |
| 2051 | static GstFlowReturn |
| 2052 | gst_rmdemux_descramble_mp4a_audio (GstRMDemux * rmdemux, |
| 2053 | GstRMDemuxStream * stream) |
| 2054 | { |
| 2055 | GstFlowReturn res; |
| 2056 | GstBuffer *buf, *outbuf; |
| 2057 | guint frames, index, i; |
| 2058 | GstMapInfo map; |
| 2059 | GstClockTime timestamp; |
| 2060 | |
| 2061 | res = GST_FLOW_OK; |
| 2062 | |
| 2063 | buf = g_ptr_array_index (stream->subpackets, 0); |
| 2064 | g_ptr_array_index (stream->subpackets, 0) = NULL; |
| 2065 | g_ptr_array_set_size (stream->subpackets, 0); |
| 2066 | |
| 2067 | gst_buffer_map (buf, &map, GST_MAP_READ); |
Sebastian Dröge | a60e713 | 2012-09-14 10:36:23 +0200 | [diff] [blame] | 2068 | timestamp = GST_BUFFER_PTS (buf); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2069 | |
| 2070 | frames = (map.data[1] & 0xf0) >> 4; |
| 2071 | index = 2 * frames + 2; |
| 2072 | |
| 2073 | for (i = 0; i < frames; i++) { |
| 2074 | guint len = (map.data[i * 2 + 2] << 8) | map.data[i * 2 + 3]; |
| 2075 | |
| 2076 | outbuf = gst_buffer_copy_region (buf, GST_BUFFER_COPY_ALL, index, len); |
Sebastian Dröge | a60e713 | 2012-09-14 10:36:23 +0200 | [diff] [blame] | 2077 | if (i == 0) { |
| 2078 | GST_BUFFER_PTS (outbuf) = timestamp; |
| 2079 | GST_BUFFER_DTS (outbuf) = timestamp; |
| 2080 | } |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2081 | |
| 2082 | index += len; |
| 2083 | |
| 2084 | if (stream->discont) { |
| 2085 | GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_DISCONT); |
| 2086 | stream->discont = FALSE; |
| 2087 | } |
| 2088 | res = gst_pad_push (stream->pad, outbuf); |
| 2089 | if (res != GST_FLOW_OK) |
| 2090 | break; |
| 2091 | } |
| 2092 | gst_buffer_unmap (buf, &map); |
| 2093 | gst_buffer_unref (buf); |
| 2094 | return res; |
| 2095 | } |
| 2096 | |
| 2097 | static GstFlowReturn |
| 2098 | gst_rmdemux_descramble_sipr_audio (GstRMDemux * rmdemux, |
| 2099 | GstRMDemuxStream * stream) |
| 2100 | { |
| 2101 | GstFlowReturn ret; |
| 2102 | GstBuffer *outbuf; |
| 2103 | GstMapInfo outmap; |
| 2104 | guint packet_size = stream->packet_size; |
| 2105 | guint height = stream->subpackets->len; |
| 2106 | guint p; |
| 2107 | |
| 2108 | g_assert (stream->height == height); |
| 2109 | |
| 2110 | GST_LOG ("packet_size = %u, leaf_size = %u, height= %u", packet_size, |
| 2111 | stream->leaf_size, height); |
| 2112 | |
| 2113 | outbuf = gst_buffer_new_and_alloc (height * packet_size); |
| 2114 | gst_buffer_map (outbuf, &outmap, GST_MAP_WRITE); |
| 2115 | |
| 2116 | for (p = 0; p < height; ++p) { |
| 2117 | GstBuffer *b = g_ptr_array_index (stream->subpackets, p); |
| 2118 | |
Sebastian Dröge | a60e713 | 2012-09-14 10:36:23 +0200 | [diff] [blame] | 2119 | if (p == 0) { |
| 2120 | GST_BUFFER_DTS (outbuf) = GST_BUFFER_DTS (b); |
| 2121 | GST_BUFFER_PTS (outbuf) = GST_BUFFER_PTS (b); |
| 2122 | } |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2123 | |
| 2124 | gst_buffer_extract (b, 0, outmap.data + packet_size * p, packet_size); |
| 2125 | } |
| 2126 | gst_buffer_unmap (outbuf, &outmap); |
| 2127 | |
Sebastian Dröge | a60e713 | 2012-09-14 10:36:23 +0200 | [diff] [blame] | 2128 | GST_LOG_OBJECT (rmdemux, "pushing buffer dts %" GST_TIME_FORMAT ", pts %" |
| 2129 | GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_DTS (outbuf)), |
| 2130 | GST_TIME_ARGS (GST_BUFFER_PTS (outbuf))); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2131 | |
| 2132 | if (stream->discont) { |
| 2133 | GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DISCONT); |
| 2134 | stream->discont = FALSE; |
| 2135 | } |
| 2136 | |
| 2137 | outbuf = gst_rm_utils_descramble_sipr_buffer (outbuf); |
| 2138 | |
| 2139 | ret = gst_pad_push (stream->pad, outbuf); |
| 2140 | |
| 2141 | gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream); |
| 2142 | |
| 2143 | return ret; |
| 2144 | } |
| 2145 | |
| 2146 | static GstFlowReturn |
| 2147 | gst_rmdemux_handle_scrambled_packet (GstRMDemux * rmdemux, |
| 2148 | GstRMDemuxStream * stream, GstBuffer * buf, gboolean keyframe) |
| 2149 | { |
| 2150 | GstFlowReturn ret; |
| 2151 | |
| 2152 | if (stream->subpackets == NULL) |
| 2153 | stream->subpackets = g_ptr_array_sized_new (stream->subpackets_needed); |
| 2154 | |
| 2155 | GST_LOG ("Got subpacket %u/%u, len=%" G_GSIZE_FORMAT ", key=%d", |
| 2156 | stream->subpackets->len + 1, stream->subpackets_needed, |
| 2157 | gst_buffer_get_size (buf), keyframe); |
| 2158 | |
| 2159 | if (keyframe && stream->subpackets->len > 0) { |
| 2160 | gst_rmdemux_stream_clear_cached_subpackets (rmdemux, stream); |
| 2161 | } |
| 2162 | |
| 2163 | g_ptr_array_add (stream->subpackets, buf); |
| 2164 | |
| 2165 | if (stream->subpackets->len < stream->subpackets_needed) |
| 2166 | return GST_FLOW_OK; |
| 2167 | |
| 2168 | g_assert (stream->subpackets->len >= 1); |
| 2169 | |
| 2170 | switch (stream->fourcc) { |
| 2171 | case GST_RM_AUD_DNET: |
| 2172 | ret = gst_rmdemux_descramble_dnet_audio (rmdemux, stream); |
| 2173 | break; |
| 2174 | case GST_RM_AUD_COOK: |
| 2175 | case GST_RM_AUD_ATRC: |
| 2176 | ret = gst_rmdemux_descramble_audio (rmdemux, stream); |
| 2177 | break; |
| 2178 | case GST_RM_AUD_RAAC: |
| 2179 | case GST_RM_AUD_RACP: |
| 2180 | ret = gst_rmdemux_descramble_mp4a_audio (rmdemux, stream); |
| 2181 | break; |
| 2182 | case GST_RM_AUD_SIPR: |
| 2183 | ret = gst_rmdemux_descramble_sipr_audio (rmdemux, stream); |
| 2184 | break; |
| 2185 | default: |
Sebastian Dröge | c167d67 | 2012-08-09 11:25:39 +0200 | [diff] [blame] | 2186 | ret = GST_FLOW_ERROR; |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2187 | g_assert_not_reached (); |
| 2188 | } |
| 2189 | |
| 2190 | return ret; |
| 2191 | } |
| 2192 | |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2193 | #define PARSE_NUMBER(data, size, number, label) \ |
| 2194 | G_STMT_START { \ |
| 2195 | if (size < 2) \ |
| 2196 | goto label; \ |
| 2197 | number = GST_READ_UINT16_BE (data); \ |
| 2198 | if (!(number & 0xc000)) { \ |
| 2199 | if (size < 4) \ |
| 2200 | goto label; \ |
| 2201 | number = GST_READ_UINT32_BE (data); \ |
| 2202 | data += 4; \ |
| 2203 | size -= 4; \ |
| 2204 | } else { \ |
| 2205 | number &= 0x3fff; \ |
| 2206 | data += 2; \ |
| 2207 | size -= 2; \ |
| 2208 | } \ |
| 2209 | } G_STMT_END |
| 2210 | |
| 2211 | static GstFlowReturn |
| 2212 | gst_rmdemux_parse_video_packet (GstRMDemux * rmdemux, GstRMDemuxStream * stream, |
| 2213 | GstBuffer * in, guint offset, guint16 version, |
| 2214 | GstClockTime timestamp, gboolean key) |
| 2215 | { |
| 2216 | GstFlowReturn ret; |
| 2217 | GstMapInfo map; |
| 2218 | const guint8 *data; |
| 2219 | gsize size; |
| 2220 | |
| 2221 | gst_buffer_map (in, &map, GST_MAP_READ); |
| 2222 | |
| 2223 | data = map.data + offset; |
| 2224 | size = map.size - offset; |
| 2225 | |
| 2226 | /* if size <= 2, we want this method to return the same GstFlowReturn as it |
| 2227 | * was previously for that given stream. */ |
Sebastian Dröge | c679a0b | 2014-06-22 18:11:42 +0200 | [diff] [blame] | 2228 | ret = GST_PAD_LAST_FLOW_RETURN (stream->pad); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2229 | |
| 2230 | while (size > 2) { |
| 2231 | guint8 pkg_header; |
| 2232 | guint pkg_offset; |
| 2233 | guint pkg_length; |
| 2234 | guint pkg_subseq = 0, pkg_seqnum = G_MAXUINT; |
| 2235 | guint fragment_size; |
| 2236 | GstBuffer *fragment; |
| 2237 | |
| 2238 | pkg_header = *data++; |
| 2239 | size--; |
| 2240 | |
| 2241 | /* packet header |
| 2242 | * bit 7: 1=last block in block chain |
| 2243 | * bit 6: 1=short header (only one block?) |
| 2244 | */ |
| 2245 | if ((pkg_header & 0xc0) == 0x40) { |
| 2246 | /* skip unknown byte */ |
| 2247 | data++; |
| 2248 | size--; |
| 2249 | pkg_offset = 0; |
| 2250 | pkg_length = size; |
| 2251 | } else { |
| 2252 | if ((pkg_header & 0x40) == 0) { |
| 2253 | pkg_subseq = (*data++) & 0x7f; |
| 2254 | size--; |
| 2255 | } else { |
| 2256 | pkg_subseq = 0; |
| 2257 | } |
| 2258 | |
| 2259 | /* length */ |
| 2260 | PARSE_NUMBER (data, size, pkg_length, not_enough_data); |
| 2261 | |
| 2262 | /* offset */ |
| 2263 | PARSE_NUMBER (data, size, pkg_offset, not_enough_data); |
| 2264 | |
| 2265 | /* seqnum */ |
| 2266 | if (size < 1) |
| 2267 | goto not_enough_data; |
| 2268 | |
| 2269 | pkg_seqnum = *data++; |
| 2270 | size--; |
| 2271 | } |
| 2272 | |
| 2273 | GST_DEBUG_OBJECT (rmdemux, |
| 2274 | "seq %d, subseq %d, offset %d, length %d, size %" G_GSIZE_FORMAT |
| 2275 | ", header %02x", pkg_seqnum, pkg_subseq, pkg_offset, pkg_length, size, |
| 2276 | pkg_header); |
| 2277 | |
| 2278 | /* calc size of fragment */ |
| 2279 | if ((pkg_header & 0xc0) == 0x80) { |
| 2280 | fragment_size = pkg_offset; |
| 2281 | } else { |
| 2282 | if ((pkg_header & 0xc0) == 0) |
| 2283 | fragment_size = size; |
| 2284 | else |
| 2285 | fragment_size = pkg_length; |
| 2286 | } |
| 2287 | GST_DEBUG_OBJECT (rmdemux, "fragment size %d", fragment_size); |
| 2288 | |
| 2289 | /* get the fragment */ |
| 2290 | fragment = |
| 2291 | gst_buffer_copy_region (in, GST_BUFFER_COPY_ALL, data - map.data, |
| 2292 | fragment_size); |
| 2293 | |
| 2294 | if (pkg_subseq == 1) { |
| 2295 | GST_DEBUG_OBJECT (rmdemux, "start new fragment"); |
| 2296 | gst_adapter_clear (stream->adapter); |
| 2297 | stream->frag_current = 0; |
| 2298 | stream->frag_count = 0; |
| 2299 | stream->frag_length = pkg_length; |
| 2300 | } else if (pkg_subseq == 0) { |
| 2301 | GST_DEBUG_OBJECT (rmdemux, "non fragmented packet"); |
| 2302 | stream->frag_current = 0; |
| 2303 | stream->frag_count = 0; |
| 2304 | stream->frag_length = fragment_size; |
| 2305 | } |
| 2306 | |
| 2307 | /* put fragment in adapter */ |
| 2308 | gst_adapter_push (stream->adapter, fragment); |
| 2309 | stream->frag_offset[stream->frag_count] = stream->frag_current; |
| 2310 | stream->frag_current += fragment_size; |
| 2311 | stream->frag_count++; |
| 2312 | |
| 2313 | if (stream->frag_count > MAX_FRAGS) |
| 2314 | goto too_many_fragments; |
| 2315 | |
| 2316 | GST_DEBUG_OBJECT (rmdemux, "stored fragment in adapter %d/%d", |
| 2317 | stream->frag_current, stream->frag_length); |
| 2318 | |
| 2319 | /* flush fragment when complete */ |
| 2320 | if (stream->frag_current >= stream->frag_length) { |
| 2321 | GstBuffer *out; |
| 2322 | GstMapInfo outmap; |
| 2323 | guint8 *outdata; |
| 2324 | guint header_size; |
| 2325 | gint i, avail; |
| 2326 | |
| 2327 | /* calculate header size, which is: |
| 2328 | * 1 byte for the number of fragments - 1 |
| 2329 | * for each fragment: |
| 2330 | * 4 bytes 0x00000001 little endian |
| 2331 | * 4 bytes fragment offset |
| 2332 | * |
| 2333 | * This is also the matroska header for realvideo, the decoder needs the |
| 2334 | * fragment offsets, both in ffmpeg and real .so, so we just give it that |
| 2335 | * in front of the data. |
| 2336 | */ |
| 2337 | header_size = 1 + (8 * (stream->frag_count)); |
| 2338 | |
| 2339 | GST_DEBUG_OBJECT (rmdemux, |
| 2340 | "fragmented completed. count %d, header_size %u", stream->frag_count, |
| 2341 | header_size); |
| 2342 | |
| 2343 | avail = gst_adapter_available (stream->adapter); |
| 2344 | |
| 2345 | out = gst_buffer_new_and_alloc (header_size + avail); |
| 2346 | gst_buffer_map (out, &outmap, GST_MAP_WRITE); |
| 2347 | outdata = outmap.data; |
| 2348 | |
| 2349 | /* create header */ |
| 2350 | *outdata++ = stream->frag_count - 1; |
| 2351 | for (i = 0; i < stream->frag_count; i++) { |
| 2352 | GST_WRITE_UINT32_LE (outdata, 0x00000001); |
| 2353 | outdata += 4; |
| 2354 | GST_WRITE_UINT32_LE (outdata, stream->frag_offset[i]); |
| 2355 | outdata += 4; |
| 2356 | } |
| 2357 | |
| 2358 | /* copy packet data after the header now */ |
| 2359 | gst_adapter_copy (stream->adapter, outdata, 0, avail); |
| 2360 | gst_adapter_flush (stream->adapter, avail); |
| 2361 | |
| 2362 | stream->frag_current = 0; |
| 2363 | stream->frag_count = 0; |
| 2364 | stream->frag_length = 0; |
| 2365 | |
| 2366 | if (timestamp != -1) { |
| 2367 | if (rmdemux->first_ts != -1 && timestamp > rmdemux->first_ts) |
| 2368 | timestamp -= rmdemux->first_ts; |
| 2369 | else |
| 2370 | timestamp = 0; |
| 2371 | |
| 2372 | if (rmdemux->base_ts != -1) |
| 2373 | timestamp += rmdemux->base_ts; |
| 2374 | } |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2375 | gst_buffer_unmap (out, &outmap); |
| 2376 | |
Sebastian Dröge | a60e713 | 2012-09-14 10:36:23 +0200 | [diff] [blame] | 2377 | /* video has DTS */ |
| 2378 | GST_BUFFER_DTS (out) = timestamp; |
| 2379 | GST_BUFFER_PTS (out) = GST_CLOCK_TIME_NONE; |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2380 | |
| 2381 | GST_LOG_OBJECT (rmdemux, "pushing timestamp %" GST_TIME_FORMAT, |
| 2382 | GST_TIME_ARGS (timestamp)); |
| 2383 | |
| 2384 | if (stream->discont) { |
| 2385 | GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DISCONT); |
| 2386 | stream->discont = FALSE; |
| 2387 | } |
| 2388 | |
| 2389 | if (!key) { |
| 2390 | GST_BUFFER_FLAG_SET (out, GST_BUFFER_FLAG_DELTA_UNIT); |
| 2391 | } |
| 2392 | |
| 2393 | ret = gst_pad_push (stream->pad, out); |
Sebastian Dröge | c679a0b | 2014-06-22 18:11:42 +0200 | [diff] [blame] | 2394 | ret = gst_flow_combiner_update_flow (rmdemux->flowcombiner, ret); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2395 | if (ret != GST_FLOW_OK) |
| 2396 | break; |
| 2397 | |
| 2398 | timestamp = GST_CLOCK_TIME_NONE; |
| 2399 | } |
| 2400 | data += fragment_size; |
| 2401 | size -= fragment_size; |
| 2402 | } |
| 2403 | GST_DEBUG_OBJECT (rmdemux, "%" G_GSIZE_FORMAT " bytes left", size); |
| 2404 | |
| 2405 | done: |
| 2406 | gst_buffer_unmap (in, &map); |
| 2407 | gst_buffer_unref (in); |
| 2408 | |
| 2409 | return ret; |
| 2410 | |
| 2411 | /* ERRORS */ |
| 2412 | not_enough_data: |
| 2413 | { |
| 2414 | GST_ELEMENT_WARNING (rmdemux, STREAM, DECODE, ("Skipping bad packet."), |
| 2415 | (NULL)); |
| 2416 | ret = GST_FLOW_OK; |
| 2417 | goto done; |
| 2418 | } |
| 2419 | too_many_fragments: |
| 2420 | { |
| 2421 | GST_ELEMENT_ERROR (rmdemux, STREAM, DECODE, |
| 2422 | ("Got more fragments (%u) than can be handled (%u)", |
| 2423 | stream->frag_count, MAX_FRAGS), (NULL)); |
| 2424 | ret = GST_FLOW_ERROR; |
| 2425 | goto done; |
| 2426 | } |
| 2427 | } |
| 2428 | |
| 2429 | static GstFlowReturn |
| 2430 | gst_rmdemux_parse_audio_packet (GstRMDemux * rmdemux, GstRMDemuxStream * stream, |
| 2431 | GstBuffer * in, guint offset, guint16 version, |
| 2432 | GstClockTime timestamp, gboolean key) |
| 2433 | { |
| 2434 | GstFlowReturn ret; |
| 2435 | GstBuffer *buffer; |
| 2436 | |
| 2437 | buffer = gst_buffer_copy_region (in, GST_BUFFER_COPY_MEMORY, offset, -1); |
| 2438 | |
| 2439 | if (rmdemux->first_ts != -1 && timestamp > rmdemux->first_ts) |
| 2440 | timestamp -= rmdemux->first_ts; |
| 2441 | else |
| 2442 | timestamp = 0; |
| 2443 | |
| 2444 | if (rmdemux->base_ts != -1) |
| 2445 | timestamp += rmdemux->base_ts; |
| 2446 | |
Sebastian Dröge | a60e713 | 2012-09-14 10:36:23 +0200 | [diff] [blame] | 2447 | GST_BUFFER_PTS (buffer) = timestamp; |
| 2448 | GST_BUFFER_DTS (buffer) = timestamp; |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2449 | |
| 2450 | if (stream->needs_descrambling) { |
| 2451 | GST_LOG_OBJECT (rmdemux, "descramble timestamp %" GST_TIME_FORMAT, |
| 2452 | GST_TIME_ARGS (timestamp)); |
| 2453 | ret = gst_rmdemux_handle_scrambled_packet (rmdemux, stream, buffer, key); |
| 2454 | } else { |
| 2455 | GST_LOG_OBJECT (rmdemux, |
| 2456 | "Pushing buffer of size %" G_GSIZE_FORMAT ", timestamp %" |
| 2457 | GST_TIME_FORMAT "to pad %s", gst_buffer_get_size (buffer), |
| 2458 | GST_TIME_ARGS (timestamp), GST_PAD_NAME (stream->pad)); |
| 2459 | |
| 2460 | if (stream->discont) { |
| 2461 | GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT); |
| 2462 | stream->discont = FALSE; |
| 2463 | } |
| 2464 | ret = gst_pad_push (stream->pad, buffer); |
| 2465 | } |
| 2466 | |
| 2467 | gst_buffer_unref (in); |
| 2468 | |
| 2469 | return ret; |
| 2470 | } |
| 2471 | |
| 2472 | static GstFlowReturn |
| 2473 | gst_rmdemux_parse_packet (GstRMDemux * rmdemux, GstBuffer * in, guint16 version) |
| 2474 | { |
| 2475 | guint16 id; |
| 2476 | GstRMDemuxStream *stream; |
| 2477 | gsize size, offset; |
| 2478 | GstFlowReturn cret, ret; |
| 2479 | GstClockTime timestamp; |
| 2480 | gboolean key; |
| 2481 | GstMapInfo map; |
| 2482 | guint8 *data; |
| 2483 | guint8 flags; |
| 2484 | guint32 ts; |
| 2485 | |
| 2486 | gst_buffer_map (in, &map, GST_MAP_READ); |
| 2487 | data = map.data; |
| 2488 | size = map.size; |
| 2489 | |
| 2490 | /* stream number */ |
| 2491 | id = RMDEMUX_GUINT16_GET (data); |
| 2492 | |
| 2493 | stream = gst_rmdemux_get_stream_by_id (rmdemux, id); |
| 2494 | if (!stream || !stream->pad) |
| 2495 | goto unknown_stream; |
| 2496 | |
| 2497 | /* timestamp in Msec */ |
| 2498 | ts = RMDEMUX_GUINT32_GET (data + 2); |
| 2499 | timestamp = ts * GST_MSECOND; |
| 2500 | |
| 2501 | rmdemux->segment.position = timestamp; |
| 2502 | |
| 2503 | GST_LOG_OBJECT (rmdemux, "Parsing a packet for stream=%d, timestamp=%" |
| 2504 | GST_TIME_FORMAT ", size %" G_GSIZE_FORMAT ", version=%d, ts=%u", id, |
| 2505 | GST_TIME_ARGS (timestamp), size, version, ts); |
| 2506 | |
| 2507 | if (rmdemux->first_ts == GST_CLOCK_TIME_NONE) { |
| 2508 | GST_DEBUG_OBJECT (rmdemux, "First timestamp: %" GST_TIME_FORMAT, |
| 2509 | GST_TIME_ARGS (timestamp)); |
| 2510 | rmdemux->first_ts = timestamp; |
| 2511 | } |
| 2512 | |
| 2513 | /* skip stream_id and timestamp */ |
| 2514 | data += (2 + 4); |
| 2515 | size -= (2 + 4); |
| 2516 | |
| 2517 | /* get flags */ |
| 2518 | flags = GST_READ_UINT8 (data + 1); |
| 2519 | |
| 2520 | data += 2; |
| 2521 | size -= 2; |
| 2522 | |
| 2523 | /* version 1 has an extra byte */ |
| 2524 | if (version == 1) { |
| 2525 | data += 1; |
| 2526 | size -= 1; |
| 2527 | } |
| 2528 | offset = data - map.data; |
| 2529 | gst_buffer_unmap (in, &map); |
| 2530 | |
| 2531 | key = (flags & 0x02) != 0; |
| 2532 | GST_DEBUG_OBJECT (rmdemux, "flags %d, Keyframe %d", flags, key); |
| 2533 | |
| 2534 | if (rmdemux->need_newsegment) { |
| 2535 | GstEvent *event; |
| 2536 | |
| 2537 | event = gst_event_new_segment (&rmdemux->segment); |
| 2538 | |
| 2539 | GST_DEBUG_OBJECT (rmdemux, "sending NEWSEGMENT event, segment.start= %" |
| 2540 | GST_TIME_FORMAT, GST_TIME_ARGS (rmdemux->segment.start)); |
| 2541 | |
| 2542 | gst_rmdemux_send_event (rmdemux, event); |
| 2543 | rmdemux->need_newsegment = FALSE; |
| 2544 | |
| 2545 | if (rmdemux->pending_tags != NULL) { |
| 2546 | gst_rmdemux_send_event (rmdemux, |
Sebastian Dröge | c167d67 | 2012-08-09 11:25:39 +0200 | [diff] [blame] | 2547 | gst_event_new_tag (rmdemux->pending_tags)); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2548 | rmdemux->pending_tags = NULL; |
| 2549 | } |
| 2550 | } |
| 2551 | |
| 2552 | if (stream->pending_tags != NULL) { |
| 2553 | GST_LOG_OBJECT (stream->pad, "tags %" GST_PTR_FORMAT, stream->pending_tags); |
Sebastian Dröge | c167d67 | 2012-08-09 11:25:39 +0200 | [diff] [blame] | 2554 | gst_pad_push_event (stream->pad, gst_event_new_tag (stream->pending_tags)); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2555 | stream->pending_tags = NULL; |
| 2556 | } |
| 2557 | |
| 2558 | if ((rmdemux->offset + size) <= stream->seek_offset) { |
| 2559 | GST_DEBUG_OBJECT (rmdemux, |
| 2560 | "Stream %d is skipping: seek_offset=%d, offset=%d, size=%" |
| 2561 | G_GSIZE_FORMAT, stream->id, stream->seek_offset, rmdemux->offset, size); |
| 2562 | cret = GST_FLOW_OK; |
| 2563 | gst_buffer_unref (in); |
| 2564 | goto beach; |
| 2565 | } |
| 2566 | |
| 2567 | /* do special headers */ |
| 2568 | if (stream->subtype == GST_RMDEMUX_STREAM_VIDEO) { |
| 2569 | ret = |
| 2570 | gst_rmdemux_parse_video_packet (rmdemux, stream, in, offset, |
| 2571 | version, timestamp, key); |
| 2572 | } else if (stream->subtype == GST_RMDEMUX_STREAM_AUDIO) { |
| 2573 | ret = |
| 2574 | gst_rmdemux_parse_audio_packet (rmdemux, stream, in, offset, |
| 2575 | version, timestamp, key); |
| 2576 | } else { |
| 2577 | gst_buffer_unref (in); |
| 2578 | ret = GST_FLOW_OK; |
| 2579 | } |
| 2580 | |
Sebastian Dröge | 0dfe98b | 2015-05-13 13:25:17 +0300 | [diff] [blame] | 2581 | cret = gst_flow_combiner_update_pad_flow (rmdemux->flowcombiner, stream->pad, |
| 2582 | ret); |
Olivier Naudan | 2556a70 | 2012-04-16 08:57:32 -0400 | [diff] [blame] | 2583 | |
| 2584 | beach: |
| 2585 | return cret; |
| 2586 | |
| 2587 | /* ERRORS */ |
| 2588 | unknown_stream: |
| 2589 | { |
| 2590 | GST_WARNING_OBJECT (rmdemux, "No stream for stream id %d in parsing " |
| 2591 | "data packet", id); |
| 2592 | gst_buffer_unmap (in, &map); |
| 2593 | gst_buffer_unref (in); |
| 2594 | return GST_FLOW_OK; |
| 2595 | } |
| 2596 | } |
| 2597 | |
| 2598 | gboolean |
| 2599 | gst_rmdemux_plugin_init (GstPlugin * plugin) |
| 2600 | { |
| 2601 | return gst_element_register (plugin, "rmdemux", |
| 2602 | GST_RANK_PRIMARY, GST_TYPE_RMDEMUX); |
| 2603 | } |