3 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
4 * Author: Thiago Santos <thiagoss@osg.samsung.com>
6 * Copyright (C) 2021-2022 Centricular Ltd
7 * Author: Edward Hervey <edward@centricular.com>
8 * Author: Jan Schmidt <jan@centricular.com>
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Library General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Library General Public License for more details.
20 * You should have received a copy of the GNU Library General Public
21 * License along with this library; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301, USA.
30 #include "gstadaptivedemux.h"
31 #include "gstadaptivedemux-private.h"
33 #include <glib/gi18n-lib.h>
34 #include <gst/app/gstappsrc.h>
36 GST_DEBUG_CATEGORY_EXTERN (adaptivedemux2_debug);
37 #define GST_CAT_DEFAULT adaptivedemux2_debug
39 static void gst_adaptive_demux2_stream_finalize (GObject * object);
40 static void gst_adaptive_demux2_stream_error (GstAdaptiveDemux2Stream * stream);
42 #define gst_adaptive_demux2_stream_parent_class parent_class
43 G_DEFINE_ABSTRACT_TYPE (GstAdaptiveDemux2Stream, gst_adaptive_demux2_stream,
47 gst_adaptive_demux2_stream_class_init (GstAdaptiveDemux2StreamClass * klass)
49 GObjectClass *gobject_class = (GObjectClass *) klass;
51 gobject_class->finalize = gst_adaptive_demux2_stream_finalize;
54 static GType tsdemux_type = 0;
57 gst_adaptive_demux2_stream_init (GstAdaptiveDemux2Stream * stream)
59 stream->download_request = download_request_new ();
60 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_STOPPED;
61 stream->last_ret = GST_FLOW_OK;
63 stream->fragment_bitrates =
64 g_malloc0 (sizeof (guint64) * NUM_LOOKBACK_FRAGMENTS);
66 stream->start_position = stream->current_position = GST_CLOCK_TIME_NONE;
68 gst_segment_init (&stream->parse_segment, GST_FORMAT_TIME);
71 /* must be called with manifest_lock taken.
72 * It will temporarily drop the manifest_lock in order to join the task.
73 * It will join only the old_streams (the demux->streams are joined by
74 * gst_adaptive_demux_stop_tasks before gst_adaptive_demux2_stream_free is
78 gst_adaptive_demux2_stream_finalize (GObject * object)
80 GstAdaptiveDemux2Stream *stream = (GstAdaptiveDemux2Stream *) object;
82 GST_LOG_OBJECT (object, "Finalizing");
84 if (stream->download_request)
85 download_request_unref (stream->download_request);
87 stream->cancelled = TRUE;
88 g_clear_error (&stream->last_error);
90 gst_adaptive_demux2_stream_fragment_clear (&stream->fragment);
92 if (stream->pending_events) {
93 g_list_free_full (stream->pending_events, (GDestroyNotify) gst_event_unref);
94 stream->pending_events = NULL;
97 if (stream->parsebin_sink) {
98 gst_object_unref (stream->parsebin_sink);
99 stream->parsebin_sink = NULL;
102 if (stream->pad_added_id)
103 g_signal_handler_disconnect (stream->parsebin, stream->pad_added_id);
104 if (stream->pad_removed_id)
105 g_signal_handler_disconnect (stream->parsebin, stream->pad_removed_id);
107 if (stream->parsebin != NULL) {
108 GST_LOG_OBJECT (stream, "Removing parsebin");
109 gst_bin_remove (GST_BIN_CAST (stream->demux), stream->parsebin);
110 gst_element_set_state (stream->parsebin, GST_STATE_NULL);
111 gst_object_unref (stream->parsebin);
112 stream->parsebin = NULL;
115 g_free (stream->fragment_bitrates);
117 g_list_free_full (stream->tracks,
118 (GDestroyNotify) gst_adaptive_demux_track_unref);
120 if (stream->pending_caps)
121 gst_caps_unref (stream->pending_caps);
123 g_clear_pointer (&stream->pending_tags, gst_tag_list_unref);
124 g_clear_pointer (&stream->stream_collection, gst_object_unref);
126 G_OBJECT_CLASS (parent_class)->finalize (object);
130 * gst_adaptive_demux2_stream_add_track:
131 * @stream: A #GstAdaptiveDemux2Stream
132 * @track: (transfer none): A #GstAdaptiveDemuxTrack to assign to the @stream
134 * This function is called when a subclass knows of a target @track that this
135 * @stream can provide.
138 gst_adaptive_demux2_stream_add_track (GstAdaptiveDemux2Stream * stream,
139 GstAdaptiveDemuxTrack * track)
141 g_return_val_if_fail (track != NULL, FALSE);
143 GST_DEBUG_OBJECT (stream->demux, "stream:%p track:%s", stream,
145 if (g_list_find (stream->tracks, track)) {
146 GST_DEBUG_OBJECT (stream->demux,
147 "track '%s' already handled by this stream", track->stream_id);
152 g_list_append (stream->tracks, gst_adaptive_demux_track_ref (track));
154 g_assert (stream->period);
155 gst_adaptive_demux_period_add_track (stream->period, track);
161 gst_adaptive_demux2_stream_next_download (GstAdaptiveDemux2Stream * stream);
163 gst_adaptive_demux2_stream_load_a_fragment (GstAdaptiveDemux2Stream * stream);
165 gst_adaptive_demux2_stream_handle_playlist_eos (GstAdaptiveDemux2Stream *
168 gst_adaptive_demux2_stream_begin_download_uri (GstAdaptiveDemux * demux,
169 GstAdaptiveDemux2Stream * stream, const gchar * uri, gint64 start,
172 #ifndef GST_DISABLE_GST_DEBUG
174 uritype (GstAdaptiveDemux2Stream * s)
176 if (s->downloading_header)
178 if (s->downloading_index)
184 /* Schedules another chunked download (returns TRUE) or FALSE if no more chunks */
186 schedule_another_chunk (GstAdaptiveDemux2Stream * stream)
188 GstAdaptiveDemux *demux = stream->demux;
189 DownloadRequest *request = stream->download_request;
192 gchar *uri = request->uri;
193 gint64 range_start = request->range_start;
194 gint64 range_end = request->range_end;
199 return FALSE; /* This was a request to the end, no more to load */
201 /* The size of the request that just completed: */
202 chunk_size = range_end + 1 - range_start;
204 if (request->content_received < chunk_size)
205 return FALSE; /* Short read - we're done */
207 /* Accumulate the data we just fetched, to figure out the next
208 * request start position and update the target chunk size from
209 * the updated stream fragment info */
210 range_start += chunk_size;
211 range_end = stream->fragment.range_end;
212 chunk_size = stream->fragment.chunk_size;
215 return FALSE; /* Sub-class doesn't want another chunk */
217 /* HTTP ranges are inclusive for the end */
218 if (chunk_size != -1) {
219 chunk_end = range_start + chunk_size - 1;
220 if (range_end != -1 && range_end < chunk_end)
221 chunk_end = range_end;
223 chunk_end = range_end;
226 GST_DEBUG_OBJECT (stream,
227 "Starting next chunk %s %" G_GINT64_FORMAT "-%" G_GINT64_FORMAT
228 " chunk_size %" G_GINT64_FORMAT, uri, range_start, chunk_end, chunk_size);
231 gst_adaptive_demux2_stream_begin_download_uri (demux, stream, uri,
232 range_start, chunk_end);
233 if (ret != GST_FLOW_OK) {
234 GST_DEBUG_OBJECT (stream,
235 "Stopping stream due to begin download failure - ret %s",
236 gst_flow_get_name (ret));
237 gst_adaptive_demux2_stream_stop (stream);
245 drain_inactive_tracks (GstAdaptiveDemux * demux,
246 GstAdaptiveDemux2Stream * stream)
251 for (iter = stream->tracks; iter; iter = iter->next) {
252 GstAdaptiveDemuxTrack *track = (GstAdaptiveDemuxTrack *) iter->data;
253 if (!track->selected) {
254 gst_adaptive_demux_track_drain_to (track,
255 demux->priv->global_output_position);
259 TRACKS_UNLOCK (demux);
262 /* Called to complete a download, either due to failure or completion
263 * Should set up the next download if necessary */
265 gst_adaptive_demux2_stream_finish_download (GstAdaptiveDemux2Stream *
266 stream, GstFlowReturn ret, GError * err)
268 GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (stream->demux);
269 GstAdaptiveDemux *demux = stream->demux;
271 GST_DEBUG_OBJECT (stream,
272 "%s download finish: %d %s - err: %p", uritype (stream), ret,
273 gst_flow_get_name (ret), err);
275 stream->download_finished = TRUE;
277 /* finish_fragment might call gst_adaptive_demux2_stream_advance_fragment,
278 * which can look at the last_ret - so make sure it's stored before calling that.
279 * Also, for not-linked or other errors passed in that are going to make
280 * this stream stop, we'll need to store it */
281 stream->last_ret = ret;
284 g_clear_error (&stream->last_error);
285 stream->last_error = g_error_copy (err);
288 /* For actual errors, stop now, no need to call finish_fragment and get
289 * confused if it returns a non-error status, but if EOS was passed in,
290 * continue and check whether finish_fragment() says we've finished
291 * the whole manifest or just this fragment */
292 if (ret < 0 && ret != GST_FLOW_EOS) {
293 GST_INFO_OBJECT (stream,
294 "Stopping stream due to error ret %s", gst_flow_get_name (ret));
295 gst_adaptive_demux2_stream_stop (stream);
299 /* Handle all the possible flow returns here: */
300 if (ret == GST_ADAPTIVE_DEMUX_FLOW_LOST_SYNC) {
301 /* We lost sync, seek back to live and return */
302 GST_WARNING_OBJECT (stream, "Lost sync when downloading");
303 gst_adaptive_demux_handle_lost_sync (demux);
305 } else if (ret == GST_ADAPTIVE_DEMUX_FLOW_END_OF_FRAGMENT) {
306 /* The sub-class wants to stop the fragment immediately */
307 stream->fragment.finished = TRUE;
308 ret = klass->finish_fragment (demux, stream);
310 GST_DEBUG_OBJECT (stream, "finish_fragment ret %d %s", ret,
311 gst_flow_get_name (ret));
312 } else if (ret == GST_ADAPTIVE_DEMUX_FLOW_RESTART_FRAGMENT) {
313 GST_DEBUG_OBJECT (stream, "Restarting download as requested");
314 /* Just mark the fragment as finished */
315 stream->fragment.finished = TRUE;
317 } else if (!klass->need_another_chunk || stream->fragment.chunk_size == -1
318 || !klass->need_another_chunk (stream)
319 || stream->fragment.chunk_size == 0) {
320 stream->fragment.finished = TRUE;
321 ret = klass->finish_fragment (stream->demux, stream);
323 GST_DEBUG_OBJECT (stream, "finish_fragment ret %d %s", ret,
324 gst_flow_get_name (ret));
325 } else if (stream->fragment.chunk_size != 0
326 && schedule_another_chunk (stream)) {
327 /* Another download has already begun, no need to queue anything below */
331 /* For HLS, we might be enqueueing data into tracks that aren't
332 * selected. Drain those ones out */
333 drain_inactive_tracks (stream->demux, stream);
335 /* Now that we've called finish_fragment we can clear these flags the
336 * sub-class might have checked */
337 if (stream->downloading_header) {
338 stream->need_header = FALSE;
339 stream->downloading_header = FALSE;
340 } else if (stream->downloading_index) {
341 stream->need_index = FALSE;
342 stream->downloading_index = FALSE;
343 /* Restart the fragment again now that header + index were loaded
344 * so that get_fragment_info() will be called again */
345 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_START_FRAGMENT;
347 /* Finishing a fragment data download. Try for another */
348 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_START_FRAGMENT;
351 /* if GST_FLOW_EOS was passed in that means this download is finished,
352 * but it's the result returned from finish_fragment() we really care
353 * about, as that tells us if the manifest has run out of fragments
355 if (ret == GST_FLOW_EOS) {
356 stream->last_ret = ret;
358 gst_adaptive_demux2_stream_handle_playlist_eos (stream);
362 /* Now finally, if ret is anything other than success, we should stop this
365 GST_DEBUG_OBJECT (stream,
366 "Stopping stream due to finish fragment ret %s",
367 gst_flow_get_name (ret));
368 gst_adaptive_demux2_stream_stop (stream);
372 /* Clear the last_ret marker before starting a fresh download */
373 stream->last_ret = GST_FLOW_OK;
375 GST_LOG_OBJECT (stream, "Scheduling next_download() call");
376 stream->pending_cb_id =
377 gst_adaptive_demux_loop_call (demux->priv->scheduler_task,
378 (GSourceFunc) gst_adaptive_demux2_stream_next_download,
379 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
382 /* Must be called from the scheduler context */
384 gst_adaptive_demux2_stream_parse_error (GstAdaptiveDemux2Stream * stream,
387 GstAdaptiveDemux *demux = stream->demux;
389 if (stream->state != GST_ADAPTIVE_DEMUX2_STREAM_STATE_DOWNLOADING)
392 downloadhelper_cancel_request (demux->download_helper,
393 stream->download_request);
395 /* cancellation is async, so recycle our download request to avoid races */
396 download_request_unref (stream->download_request);
397 stream->download_request = download_request_new ();
399 gst_adaptive_demux2_stream_finish_download (stream, GST_FLOW_CUSTOM_ERROR,
404 gst_adaptive_demux2_stream_prepare_segment (GstAdaptiveDemux * demux,
405 GstAdaptiveDemux2Stream * stream, gboolean first_and_live)
407 GstClockTime period_start = gst_adaptive_demux_get_period_start_time (demux);
408 GstClockTime offset =
409 gst_adaptive_demux2_stream_get_presentation_offset (demux, stream);
411 stream->parse_segment = demux->segment;
413 /* The demuxer segment is just built from seek events, but for each stream
414 * we have to adjust segments according to the current period and the
415 * stream specific presentation time offset.
417 * For each period, buffer timestamps start again from 0. Additionally the
418 * buffer timestamps are shifted by the stream specific presentation time
419 * offset, so the first buffer timestamp of a period is 0 + presentation
420 * time offset. If the stream contains timestamps itself, this is also
421 * supposed to be the presentation time stored inside the stream.
423 * The stream time over periods is supposed to be continuous, that is the
424 * buffer timestamp 0 + presentation time offset should map to the start
425 * time of the current period.
428 * The adjustment of the stream segments as such works the following.
430 * If the demuxer segment start is bigger than the period start, this
431 * means that we have to drop some media at the beginning of the current
432 * period, e.g. because a seek into the middle of the period has
433 * happened. The amount of media to drop is the difference between the
434 * period start and the demuxer segment start, and as each period starts
435 * again from 0, this difference is going to be the actual stream's
436 * segment start. As all timestamps of the stream are shifted by the
437 * presentation time offset, we will also have to move the segment start
440 * Likewise, the demuxer segment stop value is adjusted in the same
443 * Now the running time and stream time at the stream's segment start has
444 * to be the one that is stored inside the demuxer's segment, which means
445 * that segment.base and segment.time have to be copied over (done just
449 * If the demuxer segment start is smaller than the period start time,
450 * this means that the whole period is inside the segment. As each period
451 * starts timestamps from 0, and additionally timestamps are shifted by
452 * the presentation time offset, the stream's first timestamp (and as such
453 * the stream's segment start) has to be the presentation time offset.
454 * The stream time at the segment start is supposed to be the stream time
455 * of the period start according to the demuxer segment, so the stream
456 * segment's time would be set to that. The same goes for the stream
457 * segment's base, which is supposed to be the running time of the period
458 * start according to the demuxer's segment.
460 * The same logic applies for negative rates with the segment stop and
461 * the period stop time (which gets clamped).
464 * For the first case where not the complete period is inside the segment,
465 * the segment time and base as calculated by the second case would be
468 GST_DEBUG_OBJECT (demux, "Using demux segment %" GST_SEGMENT_FORMAT,
470 GST_DEBUG_OBJECT (demux,
471 "period_start: %" GST_TIME_FORMAT " offset: %" GST_TIME_FORMAT,
472 GST_TIME_ARGS (period_start), GST_TIME_ARGS (offset));
474 * Since stream->parse_segment is initially a copy of demux->segment,
475 * only the values that need updating are modified below. */
476 if (first_and_live) {
477 /* If first and live, demuxer did seek to the current position already */
478 stream->parse_segment.start = demux->segment.start - period_start + offset;
479 if (GST_CLOCK_TIME_IS_VALID (demux->segment.stop))
480 stream->parse_segment.stop = demux->segment.stop - period_start + offset;
481 /* FIXME : Do we need to handle negative rates for this ? */
482 stream->parse_segment.position = stream->parse_segment.start;
483 } else if (demux->segment.start > period_start) {
484 /* seek within a period */
485 stream->parse_segment.start = demux->segment.start - period_start + offset;
486 if (GST_CLOCK_TIME_IS_VALID (demux->segment.stop))
487 stream->parse_segment.stop = demux->segment.stop - period_start + offset;
488 if (stream->parse_segment.rate >= 0)
489 stream->parse_segment.position = offset;
491 stream->parse_segment.position = stream->parse_segment.stop;
493 stream->parse_segment.start = offset;
494 if (GST_CLOCK_TIME_IS_VALID (demux->segment.stop))
495 stream->parse_segment.stop = demux->segment.stop - period_start + offset;
496 if (stream->parse_segment.rate >= 0) {
497 stream->parse_segment.position = offset;
498 stream->parse_segment.base =
499 gst_segment_to_running_time (&demux->segment, GST_FORMAT_TIME,
502 stream->parse_segment.position = stream->parse_segment.stop;
503 stream->parse_segment.base =
504 gst_segment_to_running_time (&demux->segment, GST_FORMAT_TIME,
505 period_start + demux->segment.stop - demux->segment.start);
507 stream->parse_segment.time =
508 gst_segment_to_stream_time (&demux->segment, GST_FORMAT_TIME,
512 stream->send_segment = TRUE;
514 GST_DEBUG_OBJECT (stream, "Prepared segment %" GST_SEGMENT_FORMAT,
515 &stream->parse_segment);
518 /* Segment lock hold */
520 update_buffer_pts_and_demux_position_locked (GstAdaptiveDemux * demux,
521 GstAdaptiveDemux2Stream * stream, GstBuffer * buffer)
523 GstClockTimeDiff pos;
525 GST_DEBUG_OBJECT (stream, "stream->fragment.stream_time %" GST_STIME_FORMAT,
526 GST_STIME_ARGS (stream->fragment.stream_time));
528 pos = stream->fragment.stream_time;
530 if (GST_CLOCK_STIME_IS_VALID (pos)) {
531 GstClockTime offset =
532 gst_adaptive_demux2_stream_get_presentation_offset (demux, stream);
537 GST_WARNING_OBJECT (stream, "Clamping segment and buffer position to 0");
541 GST_BUFFER_PTS (buffer) = pos;
543 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
546 GST_DEBUG_OBJECT (stream, "Buffer/stream position is now: %" GST_TIME_FORMAT,
547 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)));
550 /* Must be called from the scheduler context */
552 gst_adaptive_demux2_stream_push_buffer (GstAdaptiveDemux2Stream * stream,
555 GstAdaptiveDemux *demux = stream->demux;
556 GstFlowReturn ret = GST_FLOW_OK;
557 gboolean discont = FALSE;
559 GstEvent *pending_caps = NULL, *pending_segment = NULL, *pending_tags =
560 NULL, *stream_start = NULL, *buffer_gap = NULL;
561 GList *pending_events = NULL;
563 if (stream->compute_segment) {
564 gst_adaptive_demux2_stream_prepare_segment (demux, stream,
565 stream->first_and_live);
566 stream->compute_segment = FALSE;
567 stream->first_and_live = FALSE;
570 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DROPPABLE)) {
571 GST_DEBUG_OBJECT (stream, "Creating gap event for droppable buffer");
573 gst_event_new_gap (GST_BUFFER_PTS (buffer),
574 GST_BUFFER_DURATION (buffer));
577 if (stream->first_fragment_buffer) {
578 GST_ADAPTIVE_DEMUX_SEGMENT_LOCK (demux);
579 if (demux->segment.rate < 0)
580 /* Set DISCONT flag for every first buffer in reverse playback mode
581 * as each fragment for its own has to be reversed */
583 update_buffer_pts_and_demux_position_locked (demux, stream, buffer);
584 GST_ADAPTIVE_DEMUX_SEGMENT_UNLOCK (demux);
586 GST_LOG_OBJECT (stream, "Handling initial buffer %" GST_PTR_FORMAT, buffer);
588 /* Do we need to inject STREAM_START and SEGMENT events ?
590 * This can happen when a stream is restarted, and also when switching to a
591 * variant which needs a header (in which case downloading_header will be
594 if (G_UNLIKELY (stream->send_segment || stream->downloading_header)) {
595 GST_ADAPTIVE_DEMUX_SEGMENT_LOCK (demux);
596 pending_segment = gst_event_new_segment (&stream->parse_segment);
597 gst_event_set_seqnum (pending_segment, demux->priv->segment_seqnum);
598 stream->send_segment = FALSE;
599 GST_DEBUG_OBJECT (stream, "Sending %" GST_PTR_FORMAT, pending_segment);
600 GST_ADAPTIVE_DEMUX_SEGMENT_UNLOCK (demux);
601 stream_start = gst_event_new_stream_start ("bogus");
602 if (demux->have_group_id)
603 gst_event_set_group_id (stream_start, demux->group_id);
606 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
608 stream->first_fragment_buffer = FALSE;
610 if (stream->discont) {
612 stream->discont = FALSE;
616 GST_DEBUG_OBJECT (stream, "Marking fragment as discontinuous");
617 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
619 GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DISCONT);
622 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
623 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
624 if (G_UNLIKELY (stream->pending_caps)) {
625 pending_caps = gst_event_new_caps (stream->pending_caps);
626 gst_caps_unref (stream->pending_caps);
627 stream->pending_caps = NULL;
630 if (G_UNLIKELY (stream->pending_tags)) {
631 GstTagList *tags = stream->pending_tags;
633 stream->pending_tags = NULL;
636 pending_tags = gst_event_new_tag (tags);
638 if (G_UNLIKELY (stream->pending_events)) {
639 pending_events = stream->pending_events;
640 stream->pending_events = NULL;
643 /* Do not push events or buffers holding the manifest lock */
644 if (G_UNLIKELY (stream_start)) {
645 GST_DEBUG_OBJECT (stream,
646 "Setting stream start: %" GST_PTR_FORMAT, stream_start);
647 gst_pad_send_event (stream->parsebin_sink, stream_start);
649 if (G_UNLIKELY (pending_caps)) {
650 GST_DEBUG_OBJECT (stream,
651 "Setting pending caps: %" GST_PTR_FORMAT, pending_caps);
652 gst_pad_send_event (stream->parsebin_sink, pending_caps);
654 if (G_UNLIKELY (pending_segment)) {
655 GST_DEBUG_OBJECT (stream,
656 "Sending pending seg: %" GST_PTR_FORMAT, pending_segment);
657 gst_pad_send_event (stream->parsebin_sink, pending_segment);
659 if (G_UNLIKELY (pending_tags)) {
660 GST_DEBUG_OBJECT (stream,
661 "Sending pending tags: %" GST_PTR_FORMAT, pending_tags);
662 gst_pad_send_event (stream->parsebin_sink, pending_tags);
664 while (pending_events != NULL) {
665 GstEvent *event = pending_events->data;
667 GST_DEBUG_OBJECT (stream, "Sending pending event: %" GST_PTR_FORMAT, event);
668 if (!gst_pad_send_event (stream->parsebin_sink, event))
669 GST_ERROR_OBJECT (stream, "Failed to send pending event");
671 pending_events = g_list_delete_link (pending_events, pending_events);
674 GST_DEBUG_OBJECT (stream,
675 "About to push buffer of size %" G_GSIZE_FORMAT " offset %"
676 G_GUINT64_FORMAT, gst_buffer_get_size (buffer),
677 GST_BUFFER_OFFSET (buffer));
679 ret = gst_pad_chain (stream->parsebin_sink, buffer);
682 GST_DEBUG_OBJECT (stream, "Sending %" GST_PTR_FORMAT, buffer_gap);
683 gst_pad_send_event (stream->parsebin_sink, buffer_gap);
686 if (G_UNLIKELY (stream->cancelled)) {
687 GST_LOG_OBJECT (demux, "Stream was cancelled");
688 return GST_FLOW_FLUSHING;
691 GST_LOG_OBJECT (stream, "Push result: %d %s", ret, gst_flow_get_name (ret));
697 gst_adaptive_demux2_stream_parse_buffer (GstAdaptiveDemux2Stream * stream,
700 GstAdaptiveDemux *demux = stream->demux;
701 GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
702 GstFlowReturn ret = GST_FLOW_OK;
704 /* do not make any changes if the stream is cancelled */
705 if (G_UNLIKELY (stream->cancelled)) {
706 gst_buffer_unref (buffer);
707 return GST_FLOW_FLUSHING;
710 /* starting_fragment is set to TRUE at the beginning of
711 * _stream_download_fragment()
712 * /!\ If there is a header/index being downloaded, then this will
713 * be TRUE for the first one ... but FALSE for the remaining ones,
714 * including the *actual* fragment ! */
715 if (stream->starting_fragment) {
716 stream->starting_fragment = FALSE;
717 if (klass->start_fragment != NULL && !klass->start_fragment (demux, stream))
718 return GST_FLOW_ERROR;
721 stream->download_total_bytes += gst_buffer_get_size (buffer);
723 GST_TRACE_OBJECT (stream,
724 "Received %s buffer of size %" G_GSIZE_FORMAT, uritype (stream),
725 gst_buffer_get_size (buffer));
727 ret = klass->data_received (demux, stream, buffer);
729 if (ret != GST_FLOW_OK) {
730 GST_DEBUG_OBJECT (stream, "data_received returned %s",
731 gst_flow_get_name (ret));
733 if (ret == GST_FLOW_FLUSHING) {
734 /* do not make any changes if the stream is cancelled */
735 if (G_UNLIKELY (stream->cancelled)) {
740 if (ret < GST_FLOW_EOS) {
741 GstEvent *eos = gst_event_new_eos ();
742 GST_ELEMENT_FLOW_ERROR (demux, ret);
744 GST_DEBUG_OBJECT (stream, "Pushing EOS to parser");
746 /* TODO push this on all pads */
747 gst_event_set_seqnum (eos, stream->demux->priv->segment_seqnum);
748 gst_pad_send_event (stream->parsebin_sink, eos);
749 ret = GST_FLOW_ERROR;
751 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_ERRORED;
758 /* Calculate the low and high download buffering watermarks
759 * in time as MAX (low-watermark-time, low-watermark-fragments) and
760 * MIN (high-watermark-time, high-watermark-fragments) respectively
763 calculate_track_thresholds (GstAdaptiveDemux * demux,
764 GstClockTime fragment_duration, GstClockTime * low_threshold,
765 GstClockTime * high_threshold)
767 GST_OBJECT_LOCK (demux);
768 *low_threshold = demux->buffering_low_watermark_fragments * fragment_duration;
769 if (*low_threshold == 0 ||
770 (demux->buffering_low_watermark_time != 0
771 && demux->buffering_low_watermark_time > *low_threshold)) {
772 *low_threshold = demux->buffering_low_watermark_time;
776 demux->buffering_high_watermark_fragments * fragment_duration;
777 if (*high_threshold == 0 || (demux->buffering_high_watermark_time != 0
778 && demux->buffering_high_watermark_time < *high_threshold)) {
779 *high_threshold = demux->buffering_high_watermark_time;
782 /* Make sure the low and high thresholds are less than the maximum buffering
784 if (*high_threshold == 0 ||
785 (demux->max_buffering_time != 0
786 && demux->max_buffering_time < *high_threshold)) {
787 *high_threshold = demux->max_buffering_time;
790 if (*low_threshold == 0 ||
791 (demux->max_buffering_time != 0
792 && demux->max_buffering_time < *low_threshold)) {
793 *low_threshold = demux->max_buffering_time;
796 /* Make sure the high threshold is higher than (or equal to) the low threshold.
797 * It's OK if they are the same, as the minimum download is 1 fragment */
798 if (*high_threshold == 0 ||
799 (*low_threshold != 0 && *low_threshold > *high_threshold)) {
800 *high_threshold = *low_threshold;
802 GST_OBJECT_UNLOCK (demux);
806 gst_adaptive_demux2_stream_wait_for_output_space (GstAdaptiveDemux * demux,
807 GstAdaptiveDemux2Stream * stream, GstClockTime fragment_duration)
809 gboolean need_to_wait = TRUE;
810 gboolean have_any_tracks = FALSE;
811 gboolean have_active_tracks = FALSE;
812 gboolean have_filled_inactive = FALSE;
813 gboolean update_buffering = FALSE;
815 GstClockTime low_threshold = 0, high_threshold = 0;
818 calculate_track_thresholds (demux, fragment_duration,
819 &low_threshold, &high_threshold);
821 /* If there are no tracks at all, don't wait. If there are no active
822 * tracks, keep filling until at least one track is full. If there
823 * are active tracks, require that they are all full */
825 for (iter = stream->tracks; iter; iter = iter->next) {
826 GstAdaptiveDemuxTrack *track = (GstAdaptiveDemuxTrack *) iter->data;
828 /* Update the buffering threshold */
829 if (low_threshold != track->buffering_threshold) {
830 /* The buffering threshold for this track changed, make sure to
831 * re-check buffering status */
832 update_buffering = TRUE;
833 track->buffering_threshold = low_threshold;
836 have_any_tracks = TRUE;
838 have_active_tracks = TRUE;
840 if (track->level_time < high_threshold) {
842 need_to_wait = FALSE;
843 GST_DEBUG_OBJECT (demux,
844 "stream %p track %s has level %" GST_TIME_FORMAT
845 " - needs more data (target %" GST_TIME_FORMAT
846 ") (fragment duration %" GST_TIME_FORMAT ")", stream,
847 track->stream_id, GST_TIME_ARGS (track->level_time),
848 GST_TIME_ARGS (high_threshold), GST_TIME_ARGS (fragment_duration));
851 } else if (!track->active) { /* track is over threshold and inactive */
852 have_filled_inactive = TRUE;
855 GST_DEBUG_OBJECT (demux,
856 "stream %p track %s active (%d) has level %" GST_TIME_FORMAT,
857 stream, track->stream_id, track->active,
858 GST_TIME_ARGS (track->level_time));
861 /* If there are no tracks, don't wait (we might need data to create them),
862 * or if there are active tracks that need more data to hit the threshold,
863 * don't wait. Otherwise it means all active tracks are full and we should wait */
864 if (!have_any_tracks) {
865 GST_DEBUG_OBJECT (demux, "stream %p has no tracks - not waiting", stream);
866 need_to_wait = FALSE;
867 } else if (!have_active_tracks && !have_filled_inactive) {
868 GST_DEBUG_OBJECT (demux,
869 "stream %p has inactive tracks that need more data - not waiting",
871 need_to_wait = FALSE;
875 for (iter = stream->tracks; iter; iter = iter->next) {
876 GstAdaptiveDemuxTrack *track = (GstAdaptiveDemuxTrack *) iter->data;
877 track->waiting_del_level = high_threshold;
878 GST_DEBUG_OBJECT (demux,
879 "Waiting for queued data on stream %p track %s to drop below %"
880 GST_TIME_FORMAT " (fragment duration %" GST_TIME_FORMAT ")",
881 stream, track->stream_id, GST_TIME_ARGS (track->waiting_del_level),
882 GST_TIME_ARGS (fragment_duration));
886 if (update_buffering) {
887 demux_update_buffering_locked (demux);
888 demux_post_buffering_locked (demux);
891 TRACKS_UNLOCK (demux);
896 static GstAdaptiveDemuxTrack *
897 match_parsebin_to_track (GstAdaptiveDemux2Stream * stream, GstPad * pad)
900 GstAdaptiveDemuxTrack *found_track = NULL, *first_matched_track = NULL;
901 gint num_possible_tracks = 0;
902 GstStream *gst_stream;
903 const gchar *internal_stream_id;
904 GstStreamType stream_type;
906 gst_stream = gst_pad_get_stream (pad);
908 /* FIXME: Edward: Added assertion because I don't see in what cases we would
909 * end up with a pad from parsebin which wouldn't have an associated
911 g_assert (gst_stream);
913 internal_stream_id = gst_stream_get_stream_id (gst_stream);
914 stream_type = gst_stream_get_stream_type (gst_stream);
916 GST_DEBUG_OBJECT (pad,
917 "Trying to match pad from parsebin with internal streamid %s and caps %"
918 GST_PTR_FORMAT, GST_STR_NULL (internal_stream_id),
919 gst_stream_get_caps (gst_stream));
921 /* Try to match directly by the track's pending upstream_stream_id */
922 for (tmp = stream->tracks; tmp; tmp = tmp->next) {
923 GstAdaptiveDemuxTrack *track = (GstAdaptiveDemuxTrack *) tmp->data;
925 if (stream_type != GST_STREAM_TYPE_UNKNOWN && track->type != stream_type)
928 GST_DEBUG_OBJECT (pad, "track upstream_stream_id: %s",
929 track->upstream_stream_id);
931 if (first_matched_track == NULL)
932 first_matched_track = track;
933 num_possible_tracks++;
935 /* If this track has a desired upstream stream id, match on it */
936 if (track->upstream_stream_id == NULL ||
937 g_strcmp0 (track->upstream_stream_id, internal_stream_id)) {
938 /* This is not the track for this pad */
942 /* Remove pending upstream id (we have matched it for the pending
944 g_free (track->upstream_stream_id);
945 track->upstream_stream_id = NULL;
950 if (found_track == NULL) {
951 /* If we arrive here, it means the stream is switching pads after
952 * the stream has already started running */
953 /* No track is currently waiting for this particular stream id -
954 * try and match an existing linked track. If there's only 1 possible,
956 if (num_possible_tracks == 1 && first_matched_track != NULL) {
957 GST_LOG_OBJECT (pad, "Only one possible track to link to");
958 found_track = first_matched_track;
962 if (found_track == NULL) {
963 /* TODO: There are multiple possible tracks, need to match based
964 * on language code and caps. Have you found a stream like this? */
965 GST_FIXME_OBJECT (pad, "Need to match track based on caps and language");
968 if (found_track != NULL) {
969 if (!gst_pad_is_linked (found_track->sinkpad)) {
970 GST_LOG_OBJECT (pad, "Linking to track pad %" GST_PTR_FORMAT,
971 found_track->sinkpad);
973 if (gst_pad_link (pad, found_track->sinkpad) != GST_PAD_LINK_OK) {
974 GST_ERROR_OBJECT (pad, "Couldn't connect to track sinkpad");
975 /* FIXME : Do something if we can't link ? */
978 /* Store pad as pending link */
980 "Remembering pad to be linked when current pad is unlinked");
981 g_assert (found_track->pending_srcpad == NULL);
982 found_track->pending_srcpad = gst_object_ref (pad);
987 gst_object_unref (gst_stream);
993 parsebin_pad_removed_cb (GstElement * parsebin, GstPad * pad,
994 GstAdaptiveDemux2Stream * stream)
997 GST_DEBUG_OBJECT (stream, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
999 /* Remove from pending source pad */
1000 TRACKS_LOCK (stream->demux);
1001 for (iter = stream->tracks; iter; iter = iter->next) {
1002 GstAdaptiveDemuxTrack *track = iter->data;
1003 if (track->pending_srcpad == pad) {
1004 gst_object_unref (track->pending_srcpad);
1005 track->pending_srcpad = NULL;
1009 TRACKS_UNLOCK (stream->demux);
1013 parsebin_pad_added_cb (GstElement * parsebin, GstPad * pad,
1014 GstAdaptiveDemux2Stream * stream)
1016 if (!GST_PAD_IS_SRC (pad))
1019 GST_DEBUG_OBJECT (stream, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1021 if (!match_parsebin_to_track (stream, pad))
1022 GST_WARNING_OBJECT (pad, "Found no track to handle pad");
1024 GST_DEBUG_OBJECT (stream->demux, "Done linking");
1028 parsebin_deep_element_added_cb (GstBin * parsebin, GstBin * unused,
1029 GstElement * element, GstAdaptiveDemux * demux)
1031 if (G_OBJECT_TYPE (element) == tsdemux_type) {
1032 GST_DEBUG_OBJECT (demux, "Overriding tsdemux ignore-pcr to TRUE");
1033 g_object_set (element, "ignore-pcr", TRUE, NULL);
1037 /* must be called with manifest_lock taken */
1039 gst_adaptive_demux2_stream_create_parser (GstAdaptiveDemux2Stream * stream)
1041 GstAdaptiveDemux *demux = stream->demux;
1043 if (stream->parsebin == NULL) {
1046 GST_DEBUG_OBJECT (demux, "Setting up new parsing source");
1048 /* Workaround to detect if tsdemux is being used */
1049 if (tsdemux_type == 0) {
1050 GstElement *element = gst_element_factory_make ("tsdemux", NULL);
1052 tsdemux_type = G_OBJECT_TYPE (element);
1053 gst_object_unref (element);
1057 stream->parsebin = gst_element_factory_make ("parsebin", NULL);
1059 g_signal_connect (stream->parsebin, "deep-element-added",
1060 (GCallback) parsebin_deep_element_added_cb, demux);
1061 gst_bin_add (GST_BIN_CAST (demux), gst_object_ref (stream->parsebin));
1062 stream->parsebin_sink =
1063 gst_element_get_static_pad (stream->parsebin, "sink");
1064 stream->pad_added_id = g_signal_connect (stream->parsebin, "pad-added",
1065 G_CALLBACK (parsebin_pad_added_cb), stream);
1066 stream->pad_removed_id = g_signal_connect (stream->parsebin, "pad-removed",
1067 G_CALLBACK (parsebin_pad_removed_cb), stream);
1069 event = gst_event_new_stream_start ("bogus");
1070 if (demux->have_group_id)
1071 gst_event_set_group_id (event, demux->group_id);
1073 gst_pad_send_event (stream->parsebin_sink, event);
1075 /* Not sure if these need to be outside the manifest lock: */
1076 gst_element_sync_state_with_parent (stream->parsebin);
1077 stream->last_status_code = 200; /* default to OK */
1083 on_download_cancellation (DownloadRequest * request, DownloadRequestState state,
1084 GstAdaptiveDemux2Stream * stream)
1089 on_download_error (DownloadRequest * request, DownloadRequestState state,
1090 GstAdaptiveDemux2Stream * stream)
1092 GstAdaptiveDemux *demux = stream->demux;
1093 guint last_status_code = request->status_code;
1096 stream->download_active = FALSE;
1097 stream->last_status_code = last_status_code;
1099 GST_DEBUG_OBJECT (stream,
1100 "Download finished with error, request state %d http status %u, dc %d",
1101 request->state, last_status_code, stream->download_error_count);
1103 live = gst_adaptive_demux_is_live (demux);
1104 if (((last_status_code / 100 == 4 && live)
1105 || last_status_code / 100 == 5)) {
1107 /* if current position is before available start, switch to next */
1108 if (!gst_adaptive_demux2_stream_has_next_fragment (demux, stream))
1112 gint64 range_start, range_stop;
1114 if (!gst_adaptive_demux_get_live_seek_range (demux, &range_start,
1118 if (demux->segment.position < range_start) {
1121 GST_DEBUG_OBJECT (stream, "Retrying once with next segment");
1122 gst_adaptive_demux2_stream_finish_download (stream, GST_FLOW_EOS, NULL);
1124 GST_DEBUG_OBJECT (demux, "Calling update_fragment_info");
1126 ret = gst_adaptive_demux2_stream_update_fragment_info (demux, stream);
1127 GST_DEBUG_OBJECT (stream, "update_fragment_info ret: %s",
1128 gst_flow_get_name (ret));
1130 if (ret == GST_FLOW_OK)
1133 } else if (demux->segment.position > range_stop) {
1134 /* wait a bit to be in range, we don't have any locks at that point */
1135 GstClockTime wait_time =
1136 gst_adaptive_demux2_stream_get_fragment_waiting_time (demux,
1138 if (wait_time > 0) {
1139 GST_DEBUG_OBJECT (stream,
1140 "Download waiting for %" GST_TIME_FORMAT,
1141 GST_TIME_ARGS (wait_time));
1142 g_assert (stream->pending_cb_id == 0);
1143 GST_LOG_OBJECT (stream, "Scheduling delayed load_a_fragment() call");
1144 stream->pending_cb_id =
1145 gst_adaptive_demux_loop_call_delayed (demux->priv->scheduler_task,
1147 (GSourceFunc) gst_adaptive_demux2_stream_load_a_fragment,
1148 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
1154 if (stream->download_error_count >= MAX_DOWNLOAD_ERROR_COUNT) {
1155 /* looks like there is no way of knowing when a live stream has ended
1156 * Have to assume we are falling behind and cause a manifest reload */
1157 GST_DEBUG_OBJECT (stream, "Converting error of live stream to EOS");
1158 gst_adaptive_demux2_stream_handle_playlist_eos (stream);
1161 } else if (!gst_adaptive_demux2_stream_has_next_fragment (demux, stream)) {
1162 /* If this is the last fragment, consider failures EOS and not actual
1163 * errors. Due to rounding errors in the durations, the last fragment
1164 * might not actually exist */
1165 GST_DEBUG_OBJECT (stream, "Converting error for last fragment to EOS");
1166 gst_adaptive_demux2_stream_handle_playlist_eos (stream);
1169 /* retry same segment */
1170 if (++stream->download_error_count > MAX_DOWNLOAD_ERROR_COUNT) {
1171 gst_adaptive_demux2_stream_error (stream);
1178 /* wait a short time in case the server needs a bit to recover */
1179 GST_LOG_OBJECT (stream,
1180 "Scheduling delayed load_a_fragment() call to retry in 10 milliseconds");
1181 g_assert (stream->pending_cb_id == 0);
1182 stream->pending_cb_id = gst_adaptive_demux_loop_call_delayed (demux->priv->scheduler_task, 10 * GST_MSECOND, /* Retry in 10 ms */
1183 (GSourceFunc) gst_adaptive_demux2_stream_load_a_fragment,
1184 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
1188 update_stream_bitrate (GstAdaptiveDemux2Stream * stream,
1189 DownloadRequest * request)
1191 GstClockTimeDiff last_download_duration;
1192 guint64 fragment_bytes_downloaded = request->content_received;
1194 /* The stream last_download time tracks the full download time for now */
1195 stream->last_download_time =
1196 GST_CLOCK_DIFF (request->download_request_time,
1197 request->download_end_time);
1199 /* Here we only track the time the data took to arrive and ignore request delay, so we can estimate bitrate */
1200 last_download_duration =
1201 GST_CLOCK_DIFF (request->download_start_time, request->download_end_time);
1203 /* If the entire response arrived in the first buffer
1204 * though, include the request time to get a valid
1205 * bitrate estimate */
1206 if (last_download_duration < 2 * stream->last_download_time)
1207 last_download_duration = stream->last_download_time;
1209 if (last_download_duration > 0) {
1210 stream->last_bitrate =
1211 gst_util_uint64_scale (fragment_bytes_downloaded,
1212 8 * GST_SECOND, last_download_duration);
1214 GST_DEBUG_OBJECT (stream,
1215 "Updated stream bitrate. %" G_GUINT64_FORMAT
1216 " bytes. download time %" GST_TIME_FORMAT " bitrate %"
1217 G_GUINT64_FORMAT " bps", fragment_bytes_downloaded,
1218 GST_TIME_ARGS (last_download_duration), stream->last_bitrate);
1223 on_download_progress (DownloadRequest * request, DownloadRequestState state,
1224 GstAdaptiveDemux2Stream * stream)
1226 GstAdaptiveDemux *demux = stream->demux;
1227 GstBuffer *buffer = download_request_take_buffer (request);
1232 GST_DEBUG_OBJECT (stream,
1233 "Handling buffer of %" G_GSIZE_FORMAT
1234 " bytes of ongoing download progress - %" G_GUINT64_FORMAT " / %"
1235 G_GUINT64_FORMAT " bytes", gst_buffer_get_size (buffer),
1236 request->content_received, request->content_length);
1238 /* Drop the request lock when parsing data. FIXME: Check and comment why this is needed */
1239 download_request_unlock (request);
1240 ret = gst_adaptive_demux2_stream_parse_buffer (stream, buffer);
1241 download_request_lock (request);
1243 if (stream->state != GST_ADAPTIVE_DEMUX2_STREAM_STATE_DOWNLOADING)
1246 if (ret != GST_FLOW_OK) {
1247 GST_DEBUG_OBJECT (stream,
1248 "Buffer parsing returned: %d %s. Aborting download", ret,
1249 gst_flow_get_name (ret));
1251 if (!stream->downloading_header && !stream->downloading_index)
1252 update_stream_bitrate (stream, request);
1254 downloadhelper_cancel_request (demux->download_helper, request);
1256 /* cancellation is async, so recycle our download request to avoid races */
1257 download_request_unref (stream->download_request);
1258 stream->download_request = download_request_new ();
1260 gst_adaptive_demux2_stream_finish_download (stream, ret, NULL);
1266 on_download_complete (DownloadRequest * request, DownloadRequestState state,
1267 GstAdaptiveDemux2Stream * stream)
1269 GstFlowReturn ret = GST_FLOW_OK;
1272 stream->download_active = FALSE;
1274 if (G_UNLIKELY (stream->cancelled))
1277 GST_DEBUG_OBJECT (stream,
1278 "Stream %p %s download for %s is complete with state %d",
1279 stream, uritype (stream), request->uri, request->state);
1281 /* Update bitrate for fragment downloads */
1282 if (!stream->downloading_header && !stream->downloading_index)
1283 update_stream_bitrate (stream, request);
1285 buffer = download_request_take_buffer (request);
1287 ret = gst_adaptive_demux2_stream_parse_buffer (stream, buffer);
1289 GST_DEBUG_OBJECT (stream,
1290 "%s download finished: %s ret %d %s. Stream state %d", uritype (stream),
1291 request->uri, ret, gst_flow_get_name (ret), stream->state);
1293 if (stream->state != GST_ADAPTIVE_DEMUX2_STREAM_STATE_DOWNLOADING)
1296 g_assert (stream->pending_cb_id == 0);
1297 gst_adaptive_demux2_stream_finish_download (stream, ret, NULL);
1300 /* must be called from the scheduler context
1302 * Will submit the request only, which will complete asynchronously
1304 static GstFlowReturn
1305 gst_adaptive_demux2_stream_begin_download_uri (GstAdaptiveDemux * demux,
1306 GstAdaptiveDemux2Stream * stream, const gchar * uri, gint64 start,
1309 DownloadRequest *request = stream->download_request;
1311 GST_DEBUG_OBJECT (demux,
1312 "Downloading %s uri: %s, range:%" G_GINT64_FORMAT " - %" G_GINT64_FORMAT,
1313 uritype (stream), uri, start, end);
1315 if (!gst_adaptive_demux2_stream_create_parser (stream))
1316 return GST_FLOW_ERROR;
1318 /* Configure our download request */
1319 download_request_set_uri (request, uri, start, end);
1321 if (stream->downloading_header || stream->downloading_index) {
1322 download_request_set_callbacks (request,
1323 (DownloadRequestEventCallback) on_download_complete,
1324 (DownloadRequestEventCallback) on_download_error,
1325 (DownloadRequestEventCallback) on_download_cancellation,
1326 (DownloadRequestEventCallback) NULL, stream);
1328 download_request_set_callbacks (request,
1329 (DownloadRequestEventCallback) on_download_complete,
1330 (DownloadRequestEventCallback) on_download_error,
1331 (DownloadRequestEventCallback) on_download_cancellation,
1332 (DownloadRequestEventCallback) on_download_progress, stream);
1335 if (!downloadhelper_submit_request (demux->download_helper,
1336 demux->manifest_uri, DOWNLOAD_FLAG_NONE, request, NULL))
1337 return GST_FLOW_ERROR;
1339 stream->download_active = TRUE;
1344 /* must be called from the scheduler context */
1345 static GstFlowReturn
1346 gst_adaptive_demux2_stream_download_fragment (GstAdaptiveDemux2Stream * stream)
1348 GstAdaptiveDemux *demux = stream->demux;
1349 GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
1353 /* THERE ARE THREE DIFFERENT VARIABLES FOR THE "BEGINNING" OF A FRAGMENT ! */
1354 if (stream->starting_fragment) {
1355 GST_DEBUG_OBJECT (stream, "Downloading %s%s%s",
1356 stream->fragment.uri ? "FRAGMENT " : "",
1357 stream->need_header && stream->fragment.header_uri ? "HEADER " : "",
1358 stream->need_index && stream->fragment.index_uri ? "INDEX" : "");
1360 if (stream->fragment.uri == NULL && stream->fragment.header_uri == NULL &&
1361 stream->fragment.index_uri == NULL)
1364 stream->first_fragment_buffer = TRUE;
1365 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_DOWNLOADING;
1368 if (stream->need_header && stream->fragment.header_uri != NULL) {
1370 /* Set the need_index flag when we start the header if we'll also need the index */
1371 stream->need_index = (stream->fragment.index_uri != NULL);
1373 GST_DEBUG_OBJECT (stream, "Fetching header %s %" G_GINT64_FORMAT "-%"
1374 G_GINT64_FORMAT, stream->fragment.header_uri,
1375 stream->fragment.header_range_start, stream->fragment.header_range_end);
1377 stream->downloading_header = TRUE;
1379 return gst_adaptive_demux2_stream_begin_download_uri (demux, stream,
1380 stream->fragment.header_uri, stream->fragment.header_range_start,
1381 stream->fragment.header_range_end);
1384 /* check if we have an index */
1385 if (stream->need_index && stream->fragment.index_uri != NULL) {
1386 GST_DEBUG_OBJECT (stream,
1387 "Fetching index %s %" G_GINT64_FORMAT "-%" G_GINT64_FORMAT,
1388 stream->fragment.index_uri,
1389 stream->fragment.index_range_start, stream->fragment.index_range_end);
1391 stream->downloading_index = TRUE;
1393 return gst_adaptive_demux2_stream_begin_download_uri (demux, stream,
1394 stream->fragment.index_uri, stream->fragment.index_range_start,
1395 stream->fragment.index_range_end);
1398 url = stream->fragment.uri;
1399 GST_DEBUG_OBJECT (stream, "Got url '%s' for stream %p", url, stream);
1403 /* Download the actual fragment, either in chunks or in one go */
1404 stream->first_fragment_buffer = TRUE;
1406 if (klass->need_another_chunk && klass->need_another_chunk (stream)
1407 && stream->fragment.chunk_size != 0) {
1408 /* Handle chunk downloading */
1409 gint64 range_start = stream->fragment.range_start;
1410 gint64 range_end = stream->fragment.range_end;
1411 gint chunk_size = stream->fragment.chunk_size;
1414 /* HTTP ranges are inclusive for the end */
1415 if (chunk_size != -1) {
1416 chunk_end = range_start + chunk_size - 1;
1417 if (range_end != -1 && range_end < chunk_end)
1418 chunk_end = range_end;
1420 chunk_end = range_end;
1423 GST_DEBUG_OBJECT (stream,
1424 "Starting chunked download %s %" G_GINT64_FORMAT "-%" G_GINT64_FORMAT,
1425 url, range_start, chunk_end);
1426 return gst_adaptive_demux2_stream_begin_download_uri (demux, stream, url,
1427 range_start, chunk_end);
1430 /* regular single chunk download */
1431 stream->fragment.chunk_size = 0;
1433 return gst_adaptive_demux2_stream_begin_download_uri (demux, stream, url,
1434 stream->fragment.range_start, stream->fragment.range_end);
1438 GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
1439 (_("Failed to get fragment URL.")),
1440 ("An error happened when getting fragment URL"));
1441 return GST_FLOW_ERROR;
1446 gst_adaptive_demux2_stream_push_event (GstAdaptiveDemux2Stream * stream,
1449 gboolean ret = TRUE;
1452 /* If there's a parsebin, push the event through it */
1453 if (stream->parsebin_sink != NULL) {
1454 pad = gst_object_ref (stream->parsebin_sink);
1455 GST_DEBUG_OBJECT (pad, "Pushing event %" GST_PTR_FORMAT, event);
1456 ret = gst_pad_send_event (pad, gst_event_ref (event));
1457 gst_object_unref (pad);
1460 /* If the event is EOS, ensure that all tracks are EOS. This catches
1461 * the case where the parsebin hasn't parsed anything yet (we switched
1462 * to a never before used track right near EOS, or it didn't parse enough
1463 * to create pads and be able to send EOS through to the tracks.
1465 * We don't need to care about any other events
1467 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
1470 for (iter = stream->tracks; iter; iter = iter->next) {
1471 GstAdaptiveDemuxTrack *track = (GstAdaptiveDemuxTrack *) iter->data;
1472 ret &= gst_pad_send_event (track->sinkpad, gst_event_ref (event));
1476 gst_event_unref (event);
1481 gst_adaptive_demux2_stream_error (GstAdaptiveDemux2Stream * stream)
1483 GstAdaptiveDemux *demux = stream->demux;
1485 GstStructure *details;
1487 details = gst_structure_new_empty ("details");
1488 gst_structure_set (details, "http-status-code", G_TYPE_UINT,
1489 stream->last_status_code, NULL);
1491 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_ERRORED;
1493 if (stream->last_error) {
1494 gchar *debug = g_strdup_printf ("Error on stream %s",
1495 GST_OBJECT_NAME (stream));
1497 gst_message_new_error_with_details (GST_OBJECT_CAST (demux),
1498 stream->last_error, debug, details);
1499 GST_ERROR_OBJECT (stream, "Download error: %s",
1500 stream->last_error->message);
1503 GError *err = g_error_new (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_NOT_FOUND,
1504 _("Couldn't download fragments"));
1506 gst_message_new_error_with_details (GST_OBJECT_CAST (demux), err,
1507 "Fragment downloading has failed consecutive times", details);
1509 GST_ERROR_OBJECT (stream,
1510 "Download error: Couldn't download fragments, too many failures");
1513 gst_element_post_message (GST_ELEMENT_CAST (demux), msg);
1516 /* Called when a stream reaches the end of a playback segment */
1518 gst_adaptive_demux2_stream_end_of_manifest (GstAdaptiveDemux2Stream * stream)
1520 GstAdaptiveDemux *demux = stream->demux;
1521 GstFlowReturn combined =
1522 gst_adaptive_demux_period_combine_stream_flows (demux->input_period);
1524 GST_DEBUG_OBJECT (stream, "Combined flow %s", gst_flow_get_name (combined));
1526 if (gst_adaptive_demux_has_next_period (demux)) {
1527 if (combined == GST_FLOW_EOS) {
1528 GST_DEBUG_OBJECT (stream, "Next period available, advancing");
1529 gst_adaptive_demux_advance_period (demux);
1531 /* Ensure the 'has_next_period' flag is set on the period before
1532 * pushing EOS to the stream, so that the output loop knows not
1533 * to actually output the event */
1534 GST_DEBUG_OBJECT (stream, "Marking current period has a next one");
1535 demux->input_period->has_next_period = TRUE;
1539 if (demux->priv->outputs) {
1540 GstEvent *eos = gst_event_new_eos ();
1542 GST_DEBUG_OBJECT (stream, "Stream is EOS. Stopping.");
1543 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_EOS;
1545 gst_event_set_seqnum (eos, stream->demux->priv->segment_seqnum);
1546 gst_adaptive_demux2_stream_push_event (stream, eos);
1548 GST_ERROR_OBJECT (demux, "Can't push EOS on non-exposed pad");
1549 gst_adaptive_demux2_stream_error (stream);
1554 gst_adaptive_demux2_stream_reload_manifest_cb (GstAdaptiveDemux2Stream * stream)
1556 GstAdaptiveDemux *demux = stream->demux;
1558 gboolean is_live = gst_adaptive_demux_is_live (demux);
1560 stream->pending_cb_id = 0;
1562 /* Refetch the playlist now after we waited */
1563 /* FIXME: Make this manifest update async and handle it on completion */
1564 if (!is_live && gst_adaptive_demux_update_manifest (demux) == GST_FLOW_OK) {
1565 GST_DEBUG_OBJECT (demux, "Updated the playlist");
1568 /* We were called here from a timeout, so if the load function wants to loop
1569 * again, schedule an immediate callback but return G_SOURCE_REMOVE either
1571 while (gst_adaptive_demux2_stream_next_download (stream));
1573 return G_SOURCE_REMOVE;
1577 gst_adaptive_demux2_stream_on_output_space_available_cb (GstAdaptiveDemux2Stream
1580 /* If the state already moved on, the stream was stopped, or another track
1581 * already woke up and needed data */
1582 if (stream->state != GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_OUTPUT_SPACE)
1583 return G_SOURCE_REMOVE;
1585 while (gst_adaptive_demux2_stream_load_a_fragment (stream));
1587 return G_SOURCE_REMOVE;
1591 gst_adaptive_demux2_stream_on_output_space_available (GstAdaptiveDemux2Stream *
1594 GstAdaptiveDemux *demux = stream->demux;
1597 for (iter = stream->tracks; iter; iter = iter->next) {
1598 GstAdaptiveDemuxTrack *tmp_track = (GstAdaptiveDemuxTrack *) iter->data;
1599 tmp_track->waiting_del_level = 0;
1602 GST_LOG_OBJECT (stream, "Scheduling output_space_available() call");
1604 gst_adaptive_demux_loop_call (demux->priv->scheduler_task,
1605 (GSourceFunc) gst_adaptive_demux2_stream_on_output_space_available_cb,
1606 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
1610 gst_adaptive_demux2_stream_on_manifest_update (GstAdaptiveDemux2Stream * stream)
1612 GstAdaptiveDemux *demux = stream->demux;
1614 if (stream->state != GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_MANIFEST_UPDATE)
1617 g_assert (stream->pending_cb_id == 0);
1619 GST_LOG_OBJECT (stream, "Scheduling load_a_fragment() call");
1620 stream->pending_cb_id =
1621 gst_adaptive_demux_loop_call (demux->priv->scheduler_task,
1622 (GSourceFunc) gst_adaptive_demux2_stream_load_a_fragment,
1623 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
1627 gst_adaptive_demux2_stream_handle_playlist_eos (GstAdaptiveDemux2Stream *
1630 GstAdaptiveDemux *demux = stream->demux;
1632 if (gst_adaptive_demux_is_live (demux) && (demux->segment.rate == 1.0
1633 || gst_adaptive_demux2_stream_in_live_seek_range (demux, stream))) {
1635 if (!gst_adaptive_demux_has_next_period (demux)) {
1636 /* Wait only if we can ensure current manifest has been expired.
1637 * The meaning "we have next period" *WITH* EOS is that, current
1638 * period has been ended but we can continue to the next period */
1639 GST_DEBUG_OBJECT (stream,
1640 "Live playlist EOS - waiting for manifest update");
1641 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_MANIFEST_UPDATE;
1642 gst_adaptive_demux2_stream_wants_manifest_update (demux);
1646 if (stream->replaced)
1650 gst_adaptive_demux2_stream_end_of_manifest (stream);
1654 gst_adaptive_demux2_stream_load_a_fragment (GstAdaptiveDemux2Stream * stream)
1656 GstAdaptiveDemux *demux = stream->demux;
1657 gboolean live = gst_adaptive_demux_is_live (demux);
1658 GstFlowReturn ret = GST_FLOW_OK;
1660 stream->pending_cb_id = 0;
1662 GST_LOG_OBJECT (stream, "entering, state = %d.", stream->state);
1664 switch (stream->state) {
1665 case GST_ADAPTIVE_DEMUX2_STREAM_STATE_RESTART:
1666 case GST_ADAPTIVE_DEMUX2_STREAM_STATE_START_FRAGMENT:
1667 case GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_LIVE:
1668 case GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_OUTPUT_SPACE:
1669 case GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_MANIFEST_UPDATE:
1670 /* Get information about the fragment to download */
1671 GST_DEBUG_OBJECT (demux, "Calling update_fragment_info");
1672 ret = gst_adaptive_demux2_stream_update_fragment_info (demux, stream);
1673 GST_DEBUG_OBJECT (stream,
1674 "Fragment info update result: %d %s", ret, gst_flow_get_name (ret));
1676 if (ret == GST_FLOW_OK)
1677 stream->starting_fragment = TRUE;
1679 case GST_ADAPTIVE_DEMUX2_STREAM_STATE_DOWNLOADING:
1681 case GST_ADAPTIVE_DEMUX2_STREAM_STATE_EOS:
1682 GST_ERROR_OBJECT (stream,
1683 "Unexpected stream state EOS. The stream should not be running now.");
1686 GST_ERROR_OBJECT (stream, "Unexpected stream state %d", stream->state);
1687 g_assert_not_reached ();
1691 if (ret == GST_FLOW_OK) {
1692 /* Wait for room in the output tracks */
1693 if (gst_adaptive_demux2_stream_wait_for_output_space (demux, stream,
1694 stream->fragment.duration)) {
1695 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_OUTPUT_SPACE;
1700 if (ret == GST_FLOW_OK) {
1701 /* wait for live fragments to be available */
1703 GstClockTime wait_time =
1704 gst_adaptive_demux2_stream_get_fragment_waiting_time (demux, stream);
1705 if (wait_time > 0) {
1706 GST_DEBUG_OBJECT (stream,
1707 "Download waiting for %" GST_TIME_FORMAT,
1708 GST_TIME_ARGS (wait_time));
1710 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_LIVE;
1712 GST_LOG_OBJECT (stream, "Scheduling delayed load_a_fragment() call");
1713 g_assert (stream->pending_cb_id == 0);
1714 stream->pending_cb_id =
1715 gst_adaptive_demux_loop_call_delayed (demux->priv->scheduler_task,
1716 wait_time, (GSourceFunc) gst_adaptive_demux2_stream_load_a_fragment,
1717 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
1722 if (gst_adaptive_demux2_stream_download_fragment (stream) != GST_FLOW_OK) {
1723 GST_ERROR_OBJECT (demux,
1724 "Failed to begin fragment download for stream %p", stream);
1731 break; /* all is good, let's go */
1733 GST_DEBUG_OBJECT (stream, "EOS, checking to stop download loop");
1734 gst_adaptive_demux2_stream_handle_playlist_eos (stream);
1736 case GST_ADAPTIVE_DEMUX_FLOW_LOST_SYNC:
1737 GST_DEBUG_OBJECT (stream, "Lost sync, asking reset to current position");
1738 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_STOPPED;
1739 gst_adaptive_demux_handle_lost_sync (demux);
1741 case GST_FLOW_NOT_LINKED:
1743 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_EOS;
1745 if (gst_adaptive_demux_period_combine_stream_flows (demux->input_period)
1746 == GST_FLOW_NOT_LINKED) {
1747 GST_ELEMENT_FLOW_ERROR (demux, ret);
1752 case GST_FLOW_FLUSHING:
1753 /* Flushing is normal, the target track might have been unselected */
1754 if (G_UNLIKELY (stream->cancelled))
1758 if (ret <= GST_FLOW_ERROR) {
1759 GST_WARNING_OBJECT (demux, "Error while downloading fragment");
1760 if (++stream->download_error_count > MAX_DOWNLOAD_ERROR_COUNT) {
1761 gst_adaptive_demux2_stream_error (stream);
1765 g_clear_error (&stream->last_error);
1767 /* First try to update the playlist for non-live playlists
1768 * in case the URIs have changed in the meantime. But only
1769 * try it the first time, after that we're going to wait a
1770 * a bit to not flood the server */
1771 if (stream->download_error_count == 1
1772 && !gst_adaptive_demux_is_live (demux)) {
1773 /* TODO hlsdemux had more options to this function (boolean and err) */
1774 if (gst_adaptive_demux_update_manifest (demux) == GST_FLOW_OK) {
1775 /* Retry immediately, the playlist actually has changed */
1776 GST_DEBUG_OBJECT (demux, "Updated the playlist");
1781 /* Wait half the fragment duration before retrying */
1782 GST_LOG_OBJECT (stream, "Scheduling delayed reload_manifest_cb() call");
1783 g_assert (stream->pending_cb_id == 0);
1784 stream->pending_cb_id =
1785 gst_adaptive_demux_loop_call_delayed (demux->priv->scheduler_task,
1786 stream->fragment.duration / 2,
1787 (GSourceFunc) gst_adaptive_demux2_stream_reload_manifest_cb,
1788 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
1798 gst_adaptive_demux2_stream_next_download (GstAdaptiveDemux2Stream * stream)
1800 GstAdaptiveDemux *demux = stream->demux;
1801 gboolean end_of_manifest = FALSE;
1803 GST_LOG_OBJECT (stream, "Looking for next download");
1805 /* Restarting download, figure out new position
1806 * FIXME : Move this to a separate function ? */
1807 if (G_UNLIKELY (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_RESTART)) {
1808 GstClockTimeDiff stream_time = 0;
1810 GST_DEBUG_OBJECT (stream, "Activating stream after restart");
1812 if (stream->parsebin_sink != NULL) {
1813 /* If the parsebin already exists, we need to clear it out (if it doesn't,
1814 * this is the first time we've used this stream, so it's all good) */
1815 gst_adaptive_demux2_stream_push_event (stream,
1816 gst_event_new_flush_start ());
1817 gst_adaptive_demux2_stream_push_event (stream,
1818 gst_event_new_flush_stop (FALSE));
1821 GST_ADAPTIVE_DEMUX_SEGMENT_LOCK (demux);
1822 stream_time = stream->start_position;
1824 GST_DEBUG_OBJECT (stream, "Restarting stream at "
1825 "stream position %" GST_STIME_FORMAT, GST_STIME_ARGS (stream_time));
1827 if (GST_CLOCK_STIME_IS_VALID (stream_time)) {
1828 /* TODO check return */
1829 gst_adaptive_demux2_stream_seek (demux, stream, demux->segment.rate >= 0,
1830 0, stream_time, &stream_time);
1831 stream->current_position = stream->start_position;
1833 GST_DEBUG_OBJECT (stream,
1834 "stream_time after restart seek: %" GST_STIME_FORMAT
1835 " position %" GST_STIME_FORMAT, GST_STIME_ARGS (stream_time),
1836 GST_STIME_ARGS (stream->current_position));
1839 /* Trigger (re)computation of the parsebin input segment */
1840 stream->compute_segment = TRUE;
1842 GST_ADAPTIVE_DEMUX_SEGMENT_UNLOCK (demux);
1844 stream->discont = TRUE;
1845 stream->need_header = TRUE;
1846 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_START_FRAGMENT;
1849 /* Check if we're done with our segment */
1850 GST_ADAPTIVE_DEMUX_SEGMENT_LOCK (demux);
1851 if (demux->segment.rate > 0) {
1852 if (GST_CLOCK_TIME_IS_VALID (demux->segment.stop)
1853 && stream->current_position >= demux->segment.stop) {
1854 end_of_manifest = TRUE;
1857 if (GST_CLOCK_TIME_IS_VALID (demux->segment.start)
1858 && stream->current_position <= demux->segment.start) {
1859 end_of_manifest = TRUE;
1862 GST_ADAPTIVE_DEMUX_SEGMENT_UNLOCK (demux);
1864 if (end_of_manifest) {
1865 gst_adaptive_demux2_stream_end_of_manifest (stream);
1868 return gst_adaptive_demux2_stream_load_a_fragment (stream);
1872 gst_adaptive_demux2_stream_can_start (GstAdaptiveDemux2Stream * stream)
1874 GstAdaptiveDemux *demux = stream->demux;
1875 GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
1877 if (!klass->stream_can_start)
1879 return klass->stream_can_start (demux, stream);
1883 * gst_adaptive_demux2_stream_start:
1884 * @stream: a #GstAdaptiveDemux2Stream
1886 * Start the given @stream. Should be called by subclasses that previously
1887 * returned %FALSE in `GstAdaptiveDemux::stream_can_start()`
1890 gst_adaptive_demux2_stream_start (GstAdaptiveDemux2Stream * stream)
1892 GstAdaptiveDemux *demux;
1894 g_return_if_fail (stream && stream->demux);
1896 demux = stream->demux;
1898 if (stream->pending_cb_id != 0 || stream->download_active) {
1899 /* There is already something active / pending on this stream */
1900 GST_LOG_OBJECT (stream, "Stream already running");
1904 /* Some streams require a delayed start, i.e. they need more information
1905 * before they can actually be started */
1906 if (!gst_adaptive_demux2_stream_can_start (stream)) {
1907 GST_LOG_OBJECT (stream, "Stream will be started asynchronously");
1911 if (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_EOS) {
1912 GST_LOG_OBJECT (stream, "Stream is EOS already");
1916 if (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_STOPPED ||
1917 stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_RESTART) {
1918 GST_LOG_OBJECT (stream, "Activating stream. Current state %d",
1920 stream->cancelled = FALSE;
1921 stream->replaced = FALSE;
1922 stream->last_ret = GST_FLOW_OK;
1924 if (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_STOPPED)
1925 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_START_FRAGMENT;
1928 GST_LOG_OBJECT (stream, "Scheduling next_download() call");
1929 stream->pending_cb_id =
1930 gst_adaptive_demux_loop_call (demux->priv->scheduler_task,
1931 (GSourceFunc) gst_adaptive_demux2_stream_next_download,
1932 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
1936 gst_adaptive_demux2_stream_stop (GstAdaptiveDemux2Stream * stream)
1938 GstAdaptiveDemux *demux = stream->demux;
1940 GST_DEBUG_OBJECT (stream, "Stopping stream (from state %d)", stream->state);
1941 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_STOPPED;
1943 if (stream->pending_cb_id != 0) {
1944 gst_adaptive_demux_loop_cancel_call (demux->priv->scheduler_task,
1945 stream->pending_cb_id);
1946 stream->pending_cb_id = 0;
1949 /* Cancel and drop the existing download request */
1950 downloadhelper_cancel_request (demux->download_helper,
1951 stream->download_request);
1952 download_request_unref (stream->download_request);
1953 stream->downloading_header = stream->downloading_index = FALSE;
1954 stream->download_request = download_request_new ();
1955 stream->download_active = FALSE;
1959 gst_adaptive_demux2_stream_is_running (GstAdaptiveDemux2Stream * stream)
1961 if (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_STOPPED)
1963 if (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_RESTART)
1965 if (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_EOS)
1971 gst_adaptive_demux2_stream_is_selected_locked (GstAdaptiveDemux2Stream * stream)
1975 for (tmp = stream->tracks; tmp; tmp = tmp->next) {
1976 GstAdaptiveDemuxTrack *track = tmp->data;
1977 if (track->selected)
1985 * gst_adaptive_demux2_stream_is_selected:
1986 * @stream: A #GstAdaptiveDemux2Stream
1988 * Returns: %TRUE if any of the tracks targetted by @stream is selected
1991 gst_adaptive_demux2_stream_is_selected (GstAdaptiveDemux2Stream * stream)
1995 g_return_val_if_fail (stream && stream->demux, FALSE);
1997 TRACKS_LOCK (stream->demux);
1998 ret = gst_adaptive_demux2_stream_is_selected_locked (stream);
1999 TRACKS_UNLOCK (stream->demux);