1 /* GStreamer 2 * 3 * Copyright (C) 2014 Samsung Electronics. All rights reserved. 4 * Author: Thiago Santos <thiagoss@osg.samsung.com> 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Library General Public 8 * License as published by the Free Software Foundation; either 9 * version 2 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Library General Public License for more details. 15 * 16 * You should have received a copy of the GNU Library General Public 17 * License along with this library; if not, write to the 18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 19 * Boston, MA 02110-1301, USA. 20 */ 21 22 #ifndef _GST_ADAPTIVE_DEMUX_H_ 23 #define _GST_ADAPTIVE_DEMUX_H_ 24 25 #include <gst/gst.h> 26 #include <gst/base/gstadapter.h> 27 #include <gst/uridownloader/gsturidownloader.h> 28 #include <gst/adaptivedemux/adaptive-demux-prelude.h> 29 30 G_BEGIN_DECLS 31 32 #define GST_TYPE_ADAPTIVE_DEMUX \ 33 (gst_adaptive_demux_get_type()) 34 #define GST_ADAPTIVE_DEMUX(obj) \ 35 (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_ADAPTIVE_DEMUX,GstAdaptiveDemux)) 36 #define GST_ADAPTIVE_DEMUX_CLASS(klass) \ 37 (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_ADAPTIVE_DEMUX,GstAdaptiveDemuxClass)) 38 #define GST_ADAPTIVE_DEMUX_GET_CLASS(obj) \ 39 (G_TYPE_INSTANCE_GET_CLASS((obj),GST_TYPE_ADAPTIVE_DEMUX,GstAdaptiveDemuxClass)) 40 #define GST_IS_ADAPTIVE_DEMUX(obj) \ 41 (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_ADAPTIVE_DEMUX)) 42 #define GST_IS_ADAPTIVE_DEMUX_CLASS(obj) \ 43 (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_ADAPTIVE_DEMUX)) 44 #define GST_ADAPTIVE_DEMUX_CAST(obj) ((GstAdaptiveDemux *)obj) 45 46 #define GST_ADAPTIVE_DEMUX_STREAM_CAST(obj) ((GstAdaptiveDemuxStream *)obj) 47 48 /** 49 * GST_ADAPTIVE_DEMUX_SINK_NAME: 50 * 51 * The name of the templates for the sink pad. 52 */ 53 #define GST_ADAPTIVE_DEMUX_SINK_NAME "sink" 54 55 /** 56 * GST_ADAPTIVE_DEMUX_SINK_PAD: 57 * @obj: a #GstAdaptiveDemux 58 * 59 * Gives the pointer to the sink #GstPad object of the element. 60 */ 61 #define GST_ADAPTIVE_DEMUX_SINK_PAD(obj) (((GstAdaptiveDemux *) (obj))->sinkpad) 62 63 #define GST_ADAPTIVE_DEMUX_IN_TRICKMODE_KEY_UNITS(obj) ((((GstAdaptiveDemux*)(obj))->segment.flags & GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) == GST_SEGMENT_FLAG_TRICKMODE_KEY_UNITS) 64 65 #define GST_ADAPTIVE_DEMUX_STREAM_PAD(obj) (((GstAdaptiveDemuxStream *) (obj))->pad) 66 67 #define GST_ADAPTIVE_DEMUX_STREAM_NEED_HEADER(obj) (((GstAdaptiveDemuxStream *) (obj))->need_header) 68 69 /** 70 * GST_ADAPTIVE_DEMUX_STATISTICS_MESSAGE_NAME: 71 * 72 * Name of the ELEMENT type messages posted by dashdemux with statistics. 73 * 74 * Since: 1.6 75 */ 76 #define GST_ADAPTIVE_DEMUX_STATISTICS_MESSAGE_NAME "adaptive-streaming-statistics" 77 78 #define GST_ELEMENT_ERROR_FROM_ERROR(el, msg, err) G_STMT_START { \ 79 gchar *__dbg = g_strdup_printf ("%s: %s", msg, err->message); \ 80 GST_WARNING_OBJECT (el, "error: %s", __dbg); \ 81 gst_element_message_full (GST_ELEMENT(el), GST_MESSAGE_ERROR, \ 82 err->domain, err->code, \ 83 NULL, __dbg, __FILE__, GST_FUNCTION, __LINE__); \ 84 g_clear_error (&err); \ 85 } G_STMT_END 86 87 /* DEPRECATED */ 88 #define GST_ADAPTIVE_DEMUX_FLOW_END_OF_FRAGMENT GST_FLOW_CUSTOM_SUCCESS_1 89 90 typedef struct _GstAdaptiveDemuxStreamFragment GstAdaptiveDemuxStreamFragment; 91 typedef struct _GstAdaptiveDemuxStream GstAdaptiveDemuxStream; 92 typedef struct _GstAdaptiveDemux GstAdaptiveDemux; 93 typedef struct _GstAdaptiveDemuxClass GstAdaptiveDemuxClass; 94 typedef struct _GstAdaptiveDemuxPrivate GstAdaptiveDemuxPrivate; 95 96 struct _GstAdaptiveDemuxStreamFragment 97 { 98 GstClockTime timestamp; 99 GstClockTime duration; 100 101 gchar *uri; 102 gint64 range_start; 103 gint64 range_end; 104 105 /* when chunked downloading is used, may be be updated need_another_chunk() */ 106 guint chunk_size; 107 108 /* when headers are needed */ 109 gchar *header_uri; 110 gint64 header_range_start; 111 gint64 header_range_end; 112 113 /* when index is needed */ 114 gchar *index_uri; 115 gint64 index_range_start; 116 gint64 index_range_end; 117 118 /* Nominal bitrate as provided by 119 * sub-class or calculated by base-class */ 120 guint bitrate; 121 122 gboolean finished; 123 }; 124 125 struct _GstAdaptiveDemuxStream 126 { 127 GstPad *pad; 128 GstPad *internal_pad; 129 130 GstAdaptiveDemux *demux; 131 132 GstSegment segment; 133 134 GstCaps *pending_caps; 135 GstEvent *pending_segment; 136 GstTagList *pending_tags; 137 gboolean need_header; 138 GList *pending_events; 139 140 GstFlowReturn last_ret; 141 GError *last_error; 142 143 GstTask *download_task; 144 GRecMutex download_lock; 145 146 gboolean restart_download; 147 gboolean discont; 148 149 gboolean downloading_first_buffer; 150 gboolean downloading_header; 151 gboolean downloading_index; 152 153 gboolean bitrate_changed; 154 155 /* download tooling */ 156 GstElement *src; 157 guint last_status_code; 158 GstPad *src_srcpad; 159 GstElement *uri_handler; 160 GstElement *queue; 161 GMutex fragment_download_lock; 162 GCond fragment_download_cond; 163 gboolean download_finished; /* protected by fragment_download_lock */ 164 gboolean cancelled; /* protected by fragment_download_lock */ 165 gboolean replaced; /* replaced in a bitrate switch (used with cancelled) */ 166 gboolean src_at_ready; /* protected by fragment_download_lock */ 167 gboolean starting_fragment; 168 gboolean first_fragment_buffer; 169 gint64 download_start_time; 170 gint64 download_total_bytes; 171 guint64 current_download_rate; 172 173 /* amount of data downloaded in current fragment (pre-queue2) */ 174 guint64 fragment_bytes_downloaded; 175 /* bitrate of the previous fragment (pre-queue2) */ 176 guint64 last_bitrate; 177 /* latency (request to first byte) and full download time (request to EOS) 178 * of previous fragment (pre-queue2) */ 179 GstClockTime last_latency; 180 GstClockTime last_download_time; 181 182 /* Average for the last fragments */ 183 guint64 moving_bitrate; 184 guint moving_index; 185 guint64 *fragment_bitrates; 186 187 /* QoS data */ 188 GstClockTime qos_earliest_time; 189 190 GstAdaptiveDemuxStreamFragment fragment; 191 192 guint download_error_count; 193 194 /* TODO check if used */ 195 gboolean eos; 196 197 gboolean do_block; /* TRUE if stream should block on preroll */ 198 }; 199 200 /** 201 * GstAdaptiveDemux: 202 * 203 * The opaque #GstAdaptiveDemux data structure. 204 */ 205 struct _GstAdaptiveDemux 206 { 207 /*< private >*/ 208 GstBin bin; 209 210 gint running; 211 212 gsize stream_struct_size; 213 214 /*< protected >*/ 215 GstPad *sinkpad; 216 217 GstUriDownloader *downloader; 218 219 GList *streams; 220 GList *prepared_streams; 221 GList *next_streams; 222 223 GstSegment segment; 224 225 gchar *manifest_uri; 226 gchar *manifest_base_uri; 227 228 /* Properties */ 229 gfloat bitrate_limit; /* limit of the available bitrate to use */ 230 guint connection_speed; 231 232 gboolean have_group_id; 233 guint group_id; 234 235 /* Realtime clock */ 236 GstClock *realtime_clock; 237 gint64 clock_offset; /* offset between realtime_clock and UTC (in usec) */ 238 239 /* < private > */ 240 GstAdaptiveDemuxPrivate *priv; 241 }; 242 243 /** 244 * GstAdaptiveDemuxClass: 245 * 246 */ 247 struct _GstAdaptiveDemuxClass 248 { 249 /*< private >*/ 250 GstBinClass bin_class; 251 252 /*< public >*/ 253 254 /** 255 * process_manifest: Parse the manifest 256 * @demux: #GstAdaptiveDemux 257 * @manifest: the manifest to be parsed 258 * 259 * Parse the manifest and add the created streams using 260 * gst_adaptive_demux_stream_new() 261 * 262 * Returns: %TRUE if successful 263 */ 264 gboolean (*process_manifest) (GstAdaptiveDemux * demux, GstBuffer * manifest); 265 266 /** 267 * get_manifest_update_interval: 268 * @demux: #GstAdaptiveDemux 269 * 270 * Used during live streaming, the subclass should return the interval 271 * between successive manifest updates 272 * 273 * Returns: the update interval in microseconds 274 */ 275 gint64 (*get_manifest_update_interval) (GstAdaptiveDemux * demux); 276 277 /** 278 * update_manifest: 279 * @demux: #GstAdaptiveDemux 280 * 281 * During live streaming, this will be called for the subclass to update its 282 * manifest with the new version. By default it fetches the manifest URI 283 * and passes it to GstAdaptiveDemux::update_manifest_data(). 284 * 285 * Returns: #GST_FLOW_OK is all succeeded, #GST_FLOW_EOS if the stream ended 286 * or #GST_FLOW_ERROR if an error happened 287 */ 288 GstFlowReturn (*update_manifest) (GstAdaptiveDemux * demux); 289 290 /** 291 * update_manifest_data: 292 * @demux: #GstAdaptiveDemux 293 * @buf: Downloaded manifest data 294 * 295 * During live streaming, this will be called for the subclass to update its 296 * manifest with the new version 297 * 298 * Returns: #GST_FLOW_OK is all succeeded, #GST_FLOW_EOS if the stream ended 299 * or #GST_FLOW_ERROR if an error happened 300 */ 301 GstFlowReturn (*update_manifest_data) (GstAdaptiveDemux * demux, GstBuffer * buf); 302 303 gboolean (*is_live) (GstAdaptiveDemux * demux); 304 GstClockTime (*get_duration) (GstAdaptiveDemux * demux); 305 306 /** 307 * reset: 308 * @demux: #GstAdaptiveDemux 309 * 310 * Reset the internal state of the subclass, getting ready to restart with 311 * a new stream afterwards 312 */ 313 void (*reset) (GstAdaptiveDemux * demux); 314 315 /** 316 * seek: 317 * @demux: #GstAdaptiveDemux 318 * @seek: a seek #GstEvent 319 * 320 * The demuxer should seek on all its streams to the specified position 321 * in the seek event 322 * 323 * Returns: %TRUE if successful 324 */ 325 gboolean (*seek) (GstAdaptiveDemux * demux, GstEvent * seek); 326 327 /** 328 * has_next_period: 329 * @demux: #GstAdaptiveDemux 330 * 331 * Checks if there is a next period following the current one. 332 * DASH can have multiple medias chained in its manifest, when one finishes 333 * this function is called to verify if there is a new period to be played 334 * in sequence. 335 * 336 * Returns: %TRUE if there is another period 337 */ 338 gboolean (*has_next_period) (GstAdaptiveDemux * demux); 339 /** 340 * advance_period: 341 * @demux: #GstAdaptiveDemux 342 * 343 * Advances the manifest to the next period. New streams should be created 344 * using gst_adaptive_demux_stream_new(). 345 */ 346 void (*advance_period) (GstAdaptiveDemux * demux); 347 348 void (*stream_free) (GstAdaptiveDemuxStream * stream); 349 GstFlowReturn (*stream_seek) (GstAdaptiveDemuxStream * stream, gboolean forward, GstSeekFlags flags, GstClockTime target_ts, GstClockTime * final_ts); 350 gboolean (*stream_has_next_fragment) (GstAdaptiveDemuxStream * stream); 351 GstFlowReturn (*stream_advance_fragment) (GstAdaptiveDemuxStream * stream); 352 353 /** 354 * need_another_chunk: 355 * @stream: #GstAdaptiveDemuxStream 356 * 357 * If chunked downloading is used (chunk_size != 0) this is called once a 358 * chunk is finished to decide whether more has to be downloaded or not. 359 * May update chunk_size to a different value 360 */ 361 gboolean (*need_another_chunk) (GstAdaptiveDemuxStream * stream); 362 363 /** 364 * stream_update_fragment_info: 365 * @stream: #GstAdaptiveDemuxStream 366 * 367 * Requests the stream to set the information about the current fragment to its 368 * current fragment struct 369 * 370 * Returns: #GST_FLOW_OK in success, #GST_FLOW_ERROR on error and #GST_FLOW_EOS 371 * if there is no fragment. 372 */ 373 GstFlowReturn (*stream_update_fragment_info) (GstAdaptiveDemuxStream * stream); 374 /** 375 * stream_select_bitrate: 376 * @stream: #GstAdaptiveDemuxStream 377 * @bitrate: the bitrate to select (in bytes per second) 378 * 379 * The stream should try to select the bitrate that is the greater, but not 380 * greater than the requested bitrate. If it needs a codec change it should 381 * create the new stream using gst_adaptive_demux_stream_new(). If it only 382 * needs a caps change it should set the new caps using 383 * gst_adaptive_demux_stream_set_caps(). 384 * 385 * Returns: %TRUE if the stream changed bitrate, %FALSE otherwise 386 */ 387 gboolean (*stream_select_bitrate) (GstAdaptiveDemuxStream * stream, guint64 bitrate); 388 /** 389 * stream_get_fragment_waiting_time: 390 * @stream: #GstAdaptiveDemuxStream 391 * 392 * For live streams, requests how much time should be waited before starting 393 * to download the fragment. This is useful to avoid downloading a fragment that 394 * isn't available yet. 395 * 396 * Returns: The waiting time in microseconds 397 */ 398 gint64 (*stream_get_fragment_waiting_time) (GstAdaptiveDemuxStream * stream); 399 400 /** 401 * start_fragment: 402 * @demux: #GstAdaptiveDemux 403 * @stream: #GstAdaptiveDemuxStream 404 * 405 * Notifies the subclass that the given stream is starting the download 406 * of a new fragment. Can be used to reset/init internal state that is 407 * needed before each fragment, like decryption engines. 408 * 409 * Returns: %TRUE if successful. 410 */ 411 gboolean (*start_fragment) (GstAdaptiveDemux * demux, GstAdaptiveDemuxStream * stream); 412 /** 413 * finish_fragment: 414 * @demux: #GstAdaptiveDemux 415 * @stream: #GstAdaptiveDemuxStream 416 * 417 * Notifies the subclass that a fragment download was finished. 418 * It can be used to cleanup internal state after a fragment and 419 * also push any pending data before moving to the next fragment. 420 */ 421 GstFlowReturn (*finish_fragment) (GstAdaptiveDemux * demux, GstAdaptiveDemuxStream * stream); 422 /** 423 * data_received: 424 * @demux: #GstAdaptiveDemux 425 * @stream: #GstAdaptiveDemuxStream 426 * @buffer: #GstBuffer 427 * 428 * Notifies the subclass that a fragment chunk was downloaded. The subclass 429 * can look at the data and modify/push data as desired. 430 * 431 * Returns: #GST_FLOW_OK if successful, #GST_FLOW_ERROR in case of error. 432 */ 433 GstFlowReturn (*data_received) (GstAdaptiveDemux * demux, GstAdaptiveDemuxStream * stream, GstBuffer * buffer); 434 435 /** 436 * get_live_seek_range: 437 * @demux: #GstAdaptiveDemux 438 * @start: pointer to put the start position allowed to seek to 439 * @stop: pointer to put the stop position allowed to seek to 440 * 441 * Gets the allowed seek start and stop positions for the current live stream 442 * 443 * Return: %TRUE if successful 444 */ 445 gboolean (*get_live_seek_range) (GstAdaptiveDemux * demux, gint64 * start, gint64 * stop); 446 447 /** 448 * get_presentation_offset: 449 * @demux: #GstAdaptiveDemux 450 * @stream: #GstAdaptiveDemuxStream 451 * 452 * Gets the delay to apply to @stream. 453 * 454 * Return: a #GstClockTime representing the (positive) time offset to apply to 455 * @stream. 456 */ 457 GstClockTime (*get_presentation_offset) (GstAdaptiveDemux *demux, GstAdaptiveDemuxStream *stream); 458 459 /** 460 * get_period_start_time: 461 * @demux: #GstAdaptiveDemux 462 * 463 * Gets the start time of the current period. Timestamps are resetting to 0 464 * after each period but we have to maintain a continuous stream and running 465 * time so need to know the start time of the current period. 466 * 467 * Return: a #GstClockTime representing the start time of the currently 468 * selected period. 469 */ 470 GstClockTime (*get_period_start_time) (GstAdaptiveDemux *demux); 471 472 /** 473 * requires_periodical_playlist_update: 474 * @demux: #GstAdaptiveDemux 475 * 476 * Some adaptive streaming protocols allow the client to download 477 * the playlist once and build up the fragment list based on the 478 * current fragment metadata. For those protocols the demuxer 479 * doesn't need to periodically refresh the playlist. This vfunc 480 * is relevant only for live playback scenarios. 481 * 482 * Return: %TRUE if the playlist needs to be refreshed periodically by the demuxer. 483 */ 484 gboolean (*requires_periodical_playlist_update) (GstAdaptiveDemux * demux); 485 }; 486 487 GST_ADAPTIVE_DEMUX_API 488 GType gst_adaptive_demux_get_type (void); 489 490 GST_ADAPTIVE_DEMUX_API 491 void gst_adaptive_demux_set_stream_struct_size (GstAdaptiveDemux * demux, 492 gsize struct_size); 493 494 495 GST_ADAPTIVE_DEMUX_API 496 GstAdaptiveDemuxStream *gst_adaptive_demux_stream_new (GstAdaptiveDemux * demux, 497 GstPad * pad); 498 499 GST_ADAPTIVE_DEMUX_API 500 GstAdaptiveDemuxStream *gst_adaptive_demux_find_stream_for_pad (GstAdaptiveDemux * demux, 501 GstPad * pad); 502 503 GST_ADAPTIVE_DEMUX_API 504 void gst_adaptive_demux_stream_set_caps (GstAdaptiveDemuxStream * stream, 505 GstCaps * caps); 506 507 GST_ADAPTIVE_DEMUX_API 508 void gst_adaptive_demux_stream_set_tags (GstAdaptiveDemuxStream * stream, 509 GstTagList * tags); 510 511 GST_ADAPTIVE_DEMUX_API 512 void gst_adaptive_demux_stream_fragment_clear (GstAdaptiveDemuxStreamFragment * f); 513 514 GST_ADAPTIVE_DEMUX_API 515 GstFlowReturn gst_adaptive_demux_stream_push_buffer (GstAdaptiveDemuxStream * stream, GstBuffer * buffer); 516 517 GST_ADAPTIVE_DEMUX_API 518 GstFlowReturn 519 gst_adaptive_demux_stream_advance_fragment (GstAdaptiveDemux * demux, 520 GstAdaptiveDemuxStream * stream, GstClockTime duration); 521 522 GST_ADAPTIVE_DEMUX_API 523 void gst_adaptive_demux_stream_queue_event (GstAdaptiveDemuxStream * stream, 524 GstEvent * event); 525 526 GST_ADAPTIVE_DEMUX_API 527 GstClockTime gst_adaptive_demux_get_monotonic_time (GstAdaptiveDemux * demux); 528 529 GST_ADAPTIVE_DEMUX_API 530 GDateTime *gst_adaptive_demux_get_client_now_utc (GstAdaptiveDemux * demux); 531 532 GST_ADAPTIVE_DEMUX_API 533 gboolean gst_adaptive_demux_is_running (GstAdaptiveDemux * demux); 534 535 G_END_DECLS 536 537 #endif 538 539