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_END_OF_FRAGMENT) {
301 /* The sub-class wants to stop the fragment immediately */
302 stream->fragment.finished = TRUE;
303 ret = klass->finish_fragment (demux, stream);
305 GST_DEBUG_OBJECT (stream, "finish_fragment ret %d %s", ret,
306 gst_flow_get_name (ret));
307 } else if (ret == GST_ADAPTIVE_DEMUX_FLOW_RESTART_FRAGMENT) {
308 GST_DEBUG_OBJECT (stream, "Restarting download as requested");
309 /* Just mark the fragment as finished */
310 stream->fragment.finished = TRUE;
312 } else if (!klass->need_another_chunk || stream->fragment.chunk_size == -1
313 || !klass->need_another_chunk (stream)
314 || stream->fragment.chunk_size == 0) {
315 stream->fragment.finished = TRUE;
316 ret = klass->finish_fragment (stream->demux, stream);
318 GST_DEBUG_OBJECT (stream, "finish_fragment ret %d %s", ret,
319 gst_flow_get_name (ret));
320 } else if (stream->fragment.chunk_size != 0
321 && schedule_another_chunk (stream)) {
322 /* Another download has already begun, no need to queue anything below */
326 /* For HLS, we might be enqueueing data into tracks that aren't
327 * selected. Drain those ones out */
328 drain_inactive_tracks (stream->demux, stream);
330 /* Now that we've called finish_fragment we can clear these flags the
331 * sub-class might have checked */
332 if (stream->downloading_header) {
333 stream->need_header = FALSE;
334 stream->downloading_header = FALSE;
335 } else if (stream->downloading_index) {
336 stream->need_index = FALSE;
337 stream->downloading_index = FALSE;
338 /* Restart the fragment again now that header + index were loaded
339 * so that get_fragment_info() will be called again */
340 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_START_FRAGMENT;
342 /* Finishing a fragment data download. Try for another */
343 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_START_FRAGMENT;
346 /* if GST_FLOW_EOS was passed in that means this download is finished,
347 * but it's the result returned from finish_fragment() we really care
348 * about, as that tells us if the manifest has run out of fragments
350 if (ret == GST_FLOW_EOS) {
351 stream->last_ret = ret;
353 gst_adaptive_demux2_stream_handle_playlist_eos (stream);
357 /* Now finally, if ret is anything other than success, we should stop this
360 GST_DEBUG_OBJECT (stream,
361 "Stopping stream due to finish fragment ret %s",
362 gst_flow_get_name (ret));
363 gst_adaptive_demux2_stream_stop (stream);
367 /* Clear the last_ret marker before starting a fresh download */
368 stream->last_ret = GST_FLOW_OK;
370 GST_LOG_OBJECT (stream, "Scheduling next_download() call");
371 stream->pending_cb_id =
372 gst_adaptive_demux_loop_call (demux->priv->scheduler_task,
373 (GSourceFunc) gst_adaptive_demux2_stream_next_download,
374 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
377 /* Must be called from the scheduler context */
379 gst_adaptive_demux2_stream_parse_error (GstAdaptiveDemux2Stream * stream,
382 GstAdaptiveDemux *demux = stream->demux;
384 if (stream->state != GST_ADAPTIVE_DEMUX2_STREAM_STATE_DOWNLOADING)
387 downloadhelper_cancel_request (demux->download_helper,
388 stream->download_request);
390 /* cancellation is async, so recycle our download request to avoid races */
391 download_request_unref (stream->download_request);
392 stream->download_request = download_request_new ();
394 gst_adaptive_demux2_stream_finish_download (stream, GST_FLOW_CUSTOM_ERROR,
399 gst_adaptive_demux2_stream_prepare_segment (GstAdaptiveDemux * demux,
400 GstAdaptiveDemux2Stream * stream, gboolean first_and_live)
402 GstClockTime period_start = gst_adaptive_demux_get_period_start_time (demux);
403 GstClockTime offset =
404 gst_adaptive_demux2_stream_get_presentation_offset (demux, stream);
406 stream->parse_segment = demux->segment;
408 /* The demuxer segment is just built from seek events, but for each stream
409 * we have to adjust segments according to the current period and the
410 * stream specific presentation time offset.
412 * For each period, buffer timestamps start again from 0. Additionally the
413 * buffer timestamps are shifted by the stream specific presentation time
414 * offset, so the first buffer timestamp of a period is 0 + presentation
415 * time offset. If the stream contains timestamps itself, this is also
416 * supposed to be the presentation time stored inside the stream.
418 * The stream time over periods is supposed to be continuous, that is the
419 * buffer timestamp 0 + presentation time offset should map to the start
420 * time of the current period.
423 * The adjustment of the stream segments as such works the following.
425 * If the demuxer segment start is bigger than the period start, this
426 * means that we have to drop some media at the beginning of the current
427 * period, e.g. because a seek into the middle of the period has
428 * happened. The amount of media to drop is the difference between the
429 * period start and the demuxer segment start, and as each period starts
430 * again from 0, this difference is going to be the actual stream's
431 * segment start. As all timestamps of the stream are shifted by the
432 * presentation time offset, we will also have to move the segment start
435 * Likewise, the demuxer segment stop value is adjusted in the same
438 * Now the running time and stream time at the stream's segment start has
439 * to be the one that is stored inside the demuxer's segment, which means
440 * that segment.base and segment.time have to be copied over (done just
444 * If the demuxer segment start is smaller than the period start time,
445 * this means that the whole period is inside the segment. As each period
446 * starts timestamps from 0, and additionally timestamps are shifted by
447 * the presentation time offset, the stream's first timestamp (and as such
448 * the stream's segment start) has to be the presentation time offset.
449 * The stream time at the segment start is supposed to be the stream time
450 * of the period start according to the demuxer segment, so the stream
451 * segment's time would be set to that. The same goes for the stream
452 * segment's base, which is supposed to be the running time of the period
453 * start according to the demuxer's segment.
455 * The same logic applies for negative rates with the segment stop and
456 * the period stop time (which gets clamped).
459 * For the first case where not the complete period is inside the segment,
460 * the segment time and base as calculated by the second case would be
463 GST_DEBUG_OBJECT (demux, "Using demux segment %" GST_SEGMENT_FORMAT,
465 GST_DEBUG_OBJECT (demux,
466 "period_start: %" GST_TIME_FORMAT " offset: %" GST_TIME_FORMAT,
467 GST_TIME_ARGS (period_start), GST_TIME_ARGS (offset));
469 * Since stream->parse_segment is initially a copy of demux->segment,
470 * only the values that need updating are modified below. */
471 if (first_and_live) {
472 /* If first and live, demuxer did seek to the current position already */
473 stream->parse_segment.start = demux->segment.start - period_start + offset;
474 if (GST_CLOCK_TIME_IS_VALID (demux->segment.stop))
475 stream->parse_segment.stop = demux->segment.stop - period_start + offset;
476 /* FIXME : Do we need to handle negative rates for this ? */
477 stream->parse_segment.position = stream->parse_segment.start;
478 } else if (demux->segment.start > period_start) {
479 /* seek within a period */
480 stream->parse_segment.start = demux->segment.start - period_start + offset;
481 if (GST_CLOCK_TIME_IS_VALID (demux->segment.stop))
482 stream->parse_segment.stop = demux->segment.stop - period_start + offset;
483 if (stream->parse_segment.rate >= 0)
484 stream->parse_segment.position = offset;
486 stream->parse_segment.position = stream->parse_segment.stop;
488 stream->parse_segment.start = offset;
489 if (GST_CLOCK_TIME_IS_VALID (demux->segment.stop))
490 stream->parse_segment.stop = demux->segment.stop - period_start + offset;
491 if (stream->parse_segment.rate >= 0) {
492 stream->parse_segment.position = offset;
493 stream->parse_segment.base =
494 gst_segment_to_running_time (&demux->segment, GST_FORMAT_TIME,
497 stream->parse_segment.position = stream->parse_segment.stop;
498 stream->parse_segment.base =
499 gst_segment_to_running_time (&demux->segment, GST_FORMAT_TIME,
500 period_start + demux->segment.stop - demux->segment.start);
502 stream->parse_segment.time =
503 gst_segment_to_stream_time (&demux->segment, GST_FORMAT_TIME,
507 stream->send_segment = TRUE;
509 GST_DEBUG_OBJECT (stream, "Prepared segment %" GST_SEGMENT_FORMAT,
510 &stream->parse_segment);
513 /* Segment lock hold */
515 update_buffer_pts_and_demux_position_locked (GstAdaptiveDemux * demux,
516 GstAdaptiveDemux2Stream * stream, GstBuffer * buffer)
518 GstClockTimeDiff pos;
520 GST_DEBUG_OBJECT (stream, "stream->fragment.stream_time %" GST_STIME_FORMAT,
521 GST_STIME_ARGS (stream->fragment.stream_time));
523 pos = stream->fragment.stream_time;
525 if (GST_CLOCK_STIME_IS_VALID (pos)) {
526 GstClockTime offset =
527 gst_adaptive_demux2_stream_get_presentation_offset (demux, stream);
532 GST_WARNING_OBJECT (stream, "Clamping segment and buffer position to 0");
536 GST_BUFFER_PTS (buffer) = pos;
538 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
541 GST_DEBUG_OBJECT (stream, "Buffer/stream position is now: %" GST_TIME_FORMAT,
542 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)));
545 /* Must be called from the scheduler context */
547 gst_adaptive_demux2_stream_push_buffer (GstAdaptiveDemux2Stream * stream,
550 GstAdaptiveDemux *demux = stream->demux;
551 GstFlowReturn ret = GST_FLOW_OK;
552 gboolean discont = FALSE;
554 GstEvent *pending_caps = NULL, *pending_segment = NULL, *pending_tags =
555 NULL, *stream_start = NULL, *buffer_gap = NULL;
556 GList *pending_events = NULL;
558 if (stream->compute_segment) {
559 gst_adaptive_demux2_stream_prepare_segment (demux, stream,
560 stream->first_and_live);
561 stream->compute_segment = FALSE;
562 stream->first_and_live = FALSE;
565 if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DROPPABLE)) {
566 GST_DEBUG_OBJECT (stream, "Creating gap event for droppable buffer");
568 gst_event_new_gap (GST_BUFFER_PTS (buffer),
569 GST_BUFFER_DURATION (buffer));
572 if (stream->first_fragment_buffer) {
573 GST_ADAPTIVE_DEMUX_SEGMENT_LOCK (demux);
574 if (demux->segment.rate < 0)
575 /* Set DISCONT flag for every first buffer in reverse playback mode
576 * as each fragment for its own has to be reversed */
578 update_buffer_pts_and_demux_position_locked (demux, stream, buffer);
579 GST_ADAPTIVE_DEMUX_SEGMENT_UNLOCK (demux);
581 GST_LOG_OBJECT (stream, "Handling initial buffer %" GST_PTR_FORMAT, buffer);
583 /* Do we need to inject STREAM_START and SEGMENT events ?
585 * This can happen when a stream is restarted, and also when switching to a
586 * variant which needs a header (in which case downloading_header will be
589 if (G_UNLIKELY (stream->send_segment || stream->downloading_header)) {
590 GST_ADAPTIVE_DEMUX_SEGMENT_LOCK (demux);
591 pending_segment = gst_event_new_segment (&stream->parse_segment);
592 gst_event_set_seqnum (pending_segment, demux->priv->segment_seqnum);
593 stream->send_segment = FALSE;
594 GST_DEBUG_OBJECT (stream, "Sending %" GST_PTR_FORMAT, pending_segment);
595 GST_ADAPTIVE_DEMUX_SEGMENT_UNLOCK (demux);
596 stream_start = gst_event_new_stream_start ("bogus");
597 if (demux->have_group_id)
598 gst_event_set_group_id (stream_start, demux->group_id);
601 GST_BUFFER_PTS (buffer) = GST_CLOCK_TIME_NONE;
603 stream->first_fragment_buffer = FALSE;
605 if (stream->discont) {
607 stream->discont = FALSE;
611 GST_DEBUG_OBJECT (stream, "Marking fragment as discontinuous");
612 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
614 GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DISCONT);
617 GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
618 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
619 if (G_UNLIKELY (stream->pending_caps)) {
620 pending_caps = gst_event_new_caps (stream->pending_caps);
621 gst_caps_unref (stream->pending_caps);
622 stream->pending_caps = NULL;
625 if (G_UNLIKELY (stream->pending_tags)) {
626 GstTagList *tags = stream->pending_tags;
628 stream->pending_tags = NULL;
631 pending_tags = gst_event_new_tag (tags);
633 if (G_UNLIKELY (stream->pending_events)) {
634 pending_events = stream->pending_events;
635 stream->pending_events = NULL;
638 /* Do not push events or buffers holding the manifest lock */
639 if (G_UNLIKELY (stream_start)) {
640 GST_DEBUG_OBJECT (stream,
641 "Setting stream start: %" GST_PTR_FORMAT, stream_start);
642 gst_pad_send_event (stream->parsebin_sink, stream_start);
644 if (G_UNLIKELY (pending_caps)) {
645 GST_DEBUG_OBJECT (stream,
646 "Setting pending caps: %" GST_PTR_FORMAT, pending_caps);
647 gst_pad_send_event (stream->parsebin_sink, pending_caps);
649 if (G_UNLIKELY (pending_segment)) {
650 GST_DEBUG_OBJECT (stream,
651 "Sending pending seg: %" GST_PTR_FORMAT, pending_segment);
652 gst_pad_send_event (stream->parsebin_sink, pending_segment);
654 if (G_UNLIKELY (pending_tags)) {
655 GST_DEBUG_OBJECT (stream,
656 "Sending pending tags: %" GST_PTR_FORMAT, pending_tags);
657 gst_pad_send_event (stream->parsebin_sink, pending_tags);
659 while (pending_events != NULL) {
660 GstEvent *event = pending_events->data;
662 GST_DEBUG_OBJECT (stream, "Sending pending event: %" GST_PTR_FORMAT, event);
663 if (!gst_pad_send_event (stream->parsebin_sink, event))
664 GST_ERROR_OBJECT (stream, "Failed to send pending event");
666 pending_events = g_list_delete_link (pending_events, pending_events);
669 GST_DEBUG_OBJECT (stream,
670 "About to push buffer of size %" G_GSIZE_FORMAT " offset %"
671 G_GUINT64_FORMAT, gst_buffer_get_size (buffer),
672 GST_BUFFER_OFFSET (buffer));
674 ret = gst_pad_chain (stream->parsebin_sink, buffer);
677 GST_DEBUG_OBJECT (stream, "Sending %" GST_PTR_FORMAT, buffer_gap);
678 gst_pad_send_event (stream->parsebin_sink, buffer_gap);
681 if (G_UNLIKELY (stream->cancelled)) {
682 GST_LOG_OBJECT (demux, "Stream was cancelled");
683 return GST_FLOW_FLUSHING;
686 GST_LOG_OBJECT (stream, "Push result: %d %s", ret, gst_flow_get_name (ret));
692 gst_adaptive_demux2_stream_parse_buffer (GstAdaptiveDemux2Stream * stream,
695 GstAdaptiveDemux *demux = stream->demux;
696 GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
697 GstFlowReturn ret = GST_FLOW_OK;
699 /* do not make any changes if the stream is cancelled */
700 if (G_UNLIKELY (stream->cancelled)) {
701 gst_buffer_unref (buffer);
702 return GST_FLOW_FLUSHING;
705 /* starting_fragment is set to TRUE at the beginning of
706 * _stream_download_fragment()
707 * /!\ If there is a header/index being downloaded, then this will
708 * be TRUE for the first one ... but FALSE for the remaining ones,
709 * including the *actual* fragment ! */
710 if (stream->starting_fragment) {
711 stream->starting_fragment = FALSE;
712 if (klass->start_fragment != NULL && !klass->start_fragment (demux, stream))
713 return GST_FLOW_ERROR;
716 stream->download_total_bytes += gst_buffer_get_size (buffer);
718 GST_TRACE_OBJECT (stream,
719 "Received %s buffer of size %" G_GSIZE_FORMAT, uritype (stream),
720 gst_buffer_get_size (buffer));
722 ret = klass->data_received (demux, stream, buffer);
724 if (ret != GST_FLOW_OK) {
725 GST_DEBUG_OBJECT (stream, "data_received returned %s",
726 gst_flow_get_name (ret));
728 if (ret == GST_FLOW_FLUSHING) {
729 /* do not make any changes if the stream is cancelled */
730 if (G_UNLIKELY (stream->cancelled)) {
735 if (ret < GST_FLOW_EOS) {
736 GstEvent *eos = gst_event_new_eos ();
737 GST_ELEMENT_FLOW_ERROR (demux, ret);
739 GST_DEBUG_OBJECT (stream, "Pushing EOS to parser");
741 /* TODO push this on all pads */
742 gst_event_set_seqnum (eos, stream->demux->priv->segment_seqnum);
743 gst_pad_send_event (stream->parsebin_sink, eos);
744 ret = GST_FLOW_ERROR;
746 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_ERRORED;
753 /* Calculate the low and high download buffering watermarks
754 * in time as MAX (low-watermark-time, low-watermark-fragments) and
755 * MIN (high-watermark-time, high-watermark-fragments) respectively
758 calculate_track_thresholds (GstAdaptiveDemux * demux,
759 GstClockTime fragment_duration, GstClockTime * low_threshold,
760 GstClockTime * high_threshold)
762 GST_OBJECT_LOCK (demux);
763 *low_threshold = demux->buffering_low_watermark_fragments * fragment_duration;
764 if (*low_threshold == 0 ||
765 (demux->buffering_low_watermark_time != 0
766 && demux->buffering_low_watermark_time > *low_threshold)) {
767 *low_threshold = demux->buffering_low_watermark_time;
771 demux->buffering_high_watermark_fragments * fragment_duration;
772 if (*high_threshold == 0 || (demux->buffering_high_watermark_time != 0
773 && demux->buffering_high_watermark_time < *high_threshold)) {
774 *high_threshold = demux->buffering_high_watermark_time;
777 /* Make sure the low and high thresholds are less than the maximum buffering
779 if (*high_threshold == 0 ||
780 (demux->max_buffering_time != 0
781 && demux->max_buffering_time < *high_threshold)) {
782 *high_threshold = demux->max_buffering_time;
785 if (*low_threshold == 0 ||
786 (demux->max_buffering_time != 0
787 && demux->max_buffering_time < *low_threshold)) {
788 *low_threshold = demux->max_buffering_time;
791 /* Make sure the high threshold is higher than (or equal to) the low threshold.
792 * It's OK if they are the same, as the minimum download is 1 fragment */
793 if (*high_threshold == 0 ||
794 (*low_threshold != 0 && *low_threshold > *high_threshold)) {
795 *high_threshold = *low_threshold;
797 GST_OBJECT_UNLOCK (demux);
801 gst_adaptive_demux2_stream_wait_for_output_space (GstAdaptiveDemux * demux,
802 GstAdaptiveDemux2Stream * stream, GstClockTime fragment_duration)
804 gboolean need_to_wait = TRUE;
805 gboolean have_any_tracks = FALSE;
806 gboolean have_active_tracks = FALSE;
807 gboolean have_filled_inactive = FALSE;
808 gboolean update_buffering = FALSE;
810 GstClockTime low_threshold = 0, high_threshold = 0;
813 calculate_track_thresholds (demux, fragment_duration,
814 &low_threshold, &high_threshold);
816 /* If there are no tracks at all, don't wait. If there are no active
817 * tracks, keep filling until at least one track is full. If there
818 * are active tracks, require that they are all full */
820 for (iter = stream->tracks; iter; iter = iter->next) {
821 GstAdaptiveDemuxTrack *track = (GstAdaptiveDemuxTrack *) iter->data;
823 /* Update the buffering threshold */
824 if (low_threshold != track->buffering_threshold) {
825 /* The buffering threshold for this track changed, make sure to
826 * re-check buffering status */
827 update_buffering = TRUE;
828 track->buffering_threshold = low_threshold;
831 have_any_tracks = TRUE;
833 have_active_tracks = TRUE;
835 if (track->level_time < high_threshold) {
837 need_to_wait = FALSE;
838 GST_DEBUG_OBJECT (demux,
839 "stream %p track %s has level %" GST_TIME_FORMAT
840 " - needs more data (target %" GST_TIME_FORMAT
841 ") (fragment duration %" GST_TIME_FORMAT ")", stream,
842 track->stream_id, GST_TIME_ARGS (track->level_time),
843 GST_TIME_ARGS (high_threshold), GST_TIME_ARGS (fragment_duration));
846 } else if (!track->active) { /* track is over threshold and inactive */
847 have_filled_inactive = TRUE;
850 GST_DEBUG_OBJECT (demux,
851 "stream %p track %s active (%d) has level %" GST_TIME_FORMAT,
852 stream, track->stream_id, track->active,
853 GST_TIME_ARGS (track->level_time));
856 /* If there are no tracks, don't wait (we might need data to create them),
857 * or if there are active tracks that need more data to hit the threshold,
858 * don't wait. Otherwise it means all active tracks are full and we should wait */
859 if (!have_any_tracks) {
860 GST_DEBUG_OBJECT (demux, "stream %p has no tracks - not waiting", stream);
861 need_to_wait = FALSE;
862 } else if (!have_active_tracks && !have_filled_inactive) {
863 GST_DEBUG_OBJECT (demux,
864 "stream %p has inactive tracks that need more data - not waiting",
866 need_to_wait = FALSE;
870 for (iter = stream->tracks; iter; iter = iter->next) {
871 GstAdaptiveDemuxTrack *track = (GstAdaptiveDemuxTrack *) iter->data;
872 track->waiting_del_level = high_threshold;
873 GST_DEBUG_OBJECT (demux,
874 "Waiting for queued data on stream %p track %s to drop below %"
875 GST_TIME_FORMAT " (fragment duration %" GST_TIME_FORMAT ")",
876 stream, track->stream_id, GST_TIME_ARGS (track->waiting_del_level),
877 GST_TIME_ARGS (fragment_duration));
881 if (update_buffering) {
882 demux_update_buffering_locked (demux);
883 demux_post_buffering_locked (demux);
886 TRACKS_UNLOCK (demux);
891 static GstAdaptiveDemuxTrack *
892 match_parsebin_to_track (GstAdaptiveDemux2Stream * stream, GstPad * pad)
895 GstAdaptiveDemuxTrack *found_track = NULL, *first_matched_track = NULL;
896 gint num_possible_tracks = 0;
897 GstStream *gst_stream;
898 const gchar *internal_stream_id;
899 GstStreamType stream_type;
901 gst_stream = gst_pad_get_stream (pad);
903 /* FIXME: Edward: Added assertion because I don't see in what cases we would
904 * end up with a pad from parsebin which wouldn't have an associated
906 g_assert (gst_stream);
908 internal_stream_id = gst_stream_get_stream_id (gst_stream);
909 stream_type = gst_stream_get_stream_type (gst_stream);
911 GST_DEBUG_OBJECT (pad,
912 "Trying to match pad from parsebin with internal streamid %s and caps %"
913 GST_PTR_FORMAT, GST_STR_NULL (internal_stream_id),
914 gst_stream_get_caps (gst_stream));
916 /* Try to match directly by the track's pending upstream_stream_id */
917 for (tmp = stream->tracks; tmp; tmp = tmp->next) {
918 GstAdaptiveDemuxTrack *track = (GstAdaptiveDemuxTrack *) tmp->data;
920 if (stream_type != GST_STREAM_TYPE_UNKNOWN && track->type != stream_type)
923 GST_DEBUG_OBJECT (pad, "track upstream_stream_id: %s",
924 track->upstream_stream_id);
926 if (first_matched_track == NULL)
927 first_matched_track = track;
928 num_possible_tracks++;
930 /* If this track has a desired upstream stream id, match on it */
931 if (track->upstream_stream_id == NULL ||
932 g_strcmp0 (track->upstream_stream_id, internal_stream_id)) {
933 /* This is not the track for this pad */
937 /* Remove pending upstream id (we have matched it for the pending
939 g_free (track->upstream_stream_id);
940 track->upstream_stream_id = NULL;
945 if (found_track == NULL) {
946 /* If we arrive here, it means the stream is switching pads after
947 * the stream has already started running */
948 /* No track is currently waiting for this particular stream id -
949 * try and match an existing linked track. If there's only 1 possible,
951 if (num_possible_tracks == 1 && first_matched_track != NULL) {
952 GST_LOG_OBJECT (pad, "Only one possible track to link to");
953 found_track = first_matched_track;
957 if (found_track == NULL) {
958 /* TODO: There are multiple possible tracks, need to match based
959 * on language code and caps. Have you found a stream like this? */
960 GST_FIXME_OBJECT (pad, "Need to match track based on caps and language");
963 if (found_track != NULL) {
964 if (!gst_pad_is_linked (found_track->sinkpad)) {
965 GST_LOG_OBJECT (pad, "Linking to track pad %" GST_PTR_FORMAT,
966 found_track->sinkpad);
968 if (gst_pad_link (pad, found_track->sinkpad) != GST_PAD_LINK_OK) {
969 GST_ERROR_OBJECT (pad, "Couldn't connect to track sinkpad");
970 /* FIXME : Do something if we can't link ? */
973 /* Store pad as pending link */
975 "Remembering pad to be linked when current pad is unlinked");
976 g_assert (found_track->pending_srcpad == NULL);
977 found_track->pending_srcpad = gst_object_ref (pad);
982 gst_object_unref (gst_stream);
988 parsebin_pad_removed_cb (GstElement * parsebin, GstPad * pad,
989 GstAdaptiveDemux2Stream * stream)
992 GST_DEBUG_OBJECT (stream, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
994 /* Remove from pending source pad */
995 TRACKS_LOCK (stream->demux);
996 for (iter = stream->tracks; iter; iter = iter->next) {
997 GstAdaptiveDemuxTrack *track = iter->data;
998 if (track->pending_srcpad == pad) {
999 gst_object_unref (track->pending_srcpad);
1000 track->pending_srcpad = NULL;
1004 TRACKS_UNLOCK (stream->demux);
1008 parsebin_pad_added_cb (GstElement * parsebin, GstPad * pad,
1009 GstAdaptiveDemux2Stream * stream)
1011 if (!GST_PAD_IS_SRC (pad))
1014 GST_DEBUG_OBJECT (stream, "pad %s:%s", GST_DEBUG_PAD_NAME (pad));
1016 if (!match_parsebin_to_track (stream, pad))
1017 GST_WARNING_OBJECT (pad, "Found no track to handle pad");
1019 GST_DEBUG_OBJECT (stream->demux, "Done linking");
1023 parsebin_deep_element_added_cb (GstBin * parsebin, GstBin * unused,
1024 GstElement * element, GstAdaptiveDemux * demux)
1026 if (G_OBJECT_TYPE (element) == tsdemux_type) {
1027 GST_DEBUG_OBJECT (demux, "Overriding tsdemux ignore-pcr to TRUE");
1028 g_object_set (element, "ignore-pcr", TRUE, NULL);
1032 /* must be called with manifest_lock taken */
1034 gst_adaptive_demux2_stream_create_parser (GstAdaptiveDemux2Stream * stream)
1036 GstAdaptiveDemux *demux = stream->demux;
1038 if (stream->parsebin == NULL) {
1041 GST_DEBUG_OBJECT (demux, "Setting up new parsing source");
1043 /* Workaround to detect if tsdemux is being used */
1044 if (tsdemux_type == 0) {
1045 GstElement *element = gst_element_factory_make ("tsdemux", NULL);
1047 tsdemux_type = G_OBJECT_TYPE (element);
1048 gst_object_unref (element);
1052 stream->parsebin = gst_element_factory_make ("parsebin", NULL);
1054 g_signal_connect (stream->parsebin, "deep-element-added",
1055 (GCallback) parsebin_deep_element_added_cb, demux);
1056 gst_bin_add (GST_BIN_CAST (demux), gst_object_ref (stream->parsebin));
1057 stream->parsebin_sink =
1058 gst_element_get_static_pad (stream->parsebin, "sink");
1059 stream->pad_added_id = g_signal_connect (stream->parsebin, "pad-added",
1060 G_CALLBACK (parsebin_pad_added_cb), stream);
1061 stream->pad_removed_id = g_signal_connect (stream->parsebin, "pad-removed",
1062 G_CALLBACK (parsebin_pad_removed_cb), stream);
1064 event = gst_event_new_stream_start ("bogus");
1065 if (demux->have_group_id)
1066 gst_event_set_group_id (event, demux->group_id);
1068 gst_pad_send_event (stream->parsebin_sink, event);
1070 /* Not sure if these need to be outside the manifest lock: */
1071 gst_element_sync_state_with_parent (stream->parsebin);
1072 stream->last_status_code = 200; /* default to OK */
1078 on_download_cancellation (DownloadRequest * request, DownloadRequestState state,
1079 GstAdaptiveDemux2Stream * stream)
1084 on_download_error (DownloadRequest * request, DownloadRequestState state,
1085 GstAdaptiveDemux2Stream * stream)
1087 GstAdaptiveDemux *demux = stream->demux;
1088 guint last_status_code = request->status_code;
1091 stream->download_active = FALSE;
1092 stream->last_status_code = last_status_code;
1094 GST_DEBUG_OBJECT (stream,
1095 "Download finished with error, request state %d http status %u, dc %d",
1096 request->state, last_status_code, stream->download_error_count);
1098 live = gst_adaptive_demux_is_live (demux);
1099 if (((last_status_code / 100 == 4 && live)
1100 || last_status_code / 100 == 5)) {
1102 /* if current position is before available start, switch to next */
1103 if (!gst_adaptive_demux2_stream_has_next_fragment (demux, stream))
1107 gint64 range_start, range_stop;
1109 if (!gst_adaptive_demux_get_live_seek_range (demux, &range_start,
1113 if (demux->segment.position < range_start) {
1116 GST_DEBUG_OBJECT (stream, "Retrying once with next segment");
1117 gst_adaptive_demux2_stream_finish_download (stream, GST_FLOW_EOS, NULL);
1119 GST_DEBUG_OBJECT (demux, "Calling update_fragment_info");
1121 ret = gst_adaptive_demux2_stream_update_fragment_info (demux, stream);
1122 GST_DEBUG_OBJECT (stream, "update_fragment_info ret: %s",
1123 gst_flow_get_name (ret));
1125 if (ret == GST_FLOW_OK)
1128 } else if (demux->segment.position > range_stop) {
1129 /* wait a bit to be in range, we don't have any locks at that point */
1130 GstClockTime wait_time =
1131 gst_adaptive_demux2_stream_get_fragment_waiting_time (demux,
1133 if (wait_time > 0) {
1134 GST_DEBUG_OBJECT (stream,
1135 "Download waiting for %" GST_TIME_FORMAT,
1136 GST_TIME_ARGS (wait_time));
1137 g_assert (stream->pending_cb_id == 0);
1138 GST_LOG_OBJECT (stream, "Scheduling delayed load_a_fragment() call");
1139 stream->pending_cb_id =
1140 gst_adaptive_demux_loop_call_delayed (demux->priv->scheduler_task,
1142 (GSourceFunc) gst_adaptive_demux2_stream_load_a_fragment,
1143 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
1149 if (stream->download_error_count >= MAX_DOWNLOAD_ERROR_COUNT) {
1150 /* looks like there is no way of knowing when a live stream has ended
1151 * Have to assume we are falling behind and cause a manifest reload */
1152 GST_DEBUG_OBJECT (stream, "Converting error of live stream to EOS");
1153 gst_adaptive_demux2_stream_handle_playlist_eos (stream);
1156 } else if (!gst_adaptive_demux2_stream_has_next_fragment (demux, stream)) {
1157 /* If this is the last fragment, consider failures EOS and not actual
1158 * errors. Due to rounding errors in the durations, the last fragment
1159 * might not actually exist */
1160 GST_DEBUG_OBJECT (stream, "Converting error for last fragment to EOS");
1161 gst_adaptive_demux2_stream_handle_playlist_eos (stream);
1164 /* retry same segment */
1165 if (++stream->download_error_count > MAX_DOWNLOAD_ERROR_COUNT) {
1166 gst_adaptive_demux2_stream_error (stream);
1173 /* wait a short time in case the server needs a bit to recover */
1174 GST_LOG_OBJECT (stream,
1175 "Scheduling delayed load_a_fragment() call to retry in 10 milliseconds");
1176 g_assert (stream->pending_cb_id == 0);
1177 stream->pending_cb_id = gst_adaptive_demux_loop_call_delayed (demux->priv->scheduler_task, 10 * GST_MSECOND, /* Retry in 10 ms */
1178 (GSourceFunc) gst_adaptive_demux2_stream_load_a_fragment,
1179 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
1183 update_stream_bitrate (GstAdaptiveDemux2Stream * stream,
1184 DownloadRequest * request)
1186 GstClockTimeDiff last_download_duration;
1187 guint64 fragment_bytes_downloaded = request->content_received;
1189 /* The stream last_download time tracks the full download time for now */
1190 stream->last_download_time =
1191 GST_CLOCK_DIFF (request->download_request_time,
1192 request->download_end_time);
1194 /* Here we only track the time the data took to arrive and ignore request delay, so we can estimate bitrate */
1195 last_download_duration =
1196 GST_CLOCK_DIFF (request->download_start_time, request->download_end_time);
1198 /* If the entire response arrived in the first buffer
1199 * though, include the request time to get a valid
1200 * bitrate estimate */
1201 if (last_download_duration < 2 * stream->last_download_time)
1202 last_download_duration = stream->last_download_time;
1204 if (last_download_duration > 0) {
1205 stream->last_bitrate =
1206 gst_util_uint64_scale (fragment_bytes_downloaded,
1207 8 * GST_SECOND, last_download_duration);
1209 GST_DEBUG_OBJECT (stream,
1210 "Updated stream bitrate. %" G_GUINT64_FORMAT
1211 " bytes. download time %" GST_TIME_FORMAT " bitrate %"
1212 G_GUINT64_FORMAT " bps", fragment_bytes_downloaded,
1213 GST_TIME_ARGS (last_download_duration), stream->last_bitrate);
1218 on_download_progress (DownloadRequest * request, DownloadRequestState state,
1219 GstAdaptiveDemux2Stream * stream)
1221 GstAdaptiveDemux *demux = stream->demux;
1222 GstBuffer *buffer = download_request_take_buffer (request);
1227 GST_DEBUG_OBJECT (stream,
1228 "Handling buffer of %" G_GSIZE_FORMAT
1229 " bytes of ongoing download progress - %" G_GUINT64_FORMAT " / %"
1230 G_GUINT64_FORMAT " bytes", gst_buffer_get_size (buffer),
1231 request->content_received, request->content_length);
1233 /* Drop the request lock when parsing data. FIXME: Check and comment why this is needed */
1234 download_request_unlock (request);
1235 ret = gst_adaptive_demux2_stream_parse_buffer (stream, buffer);
1236 download_request_lock (request);
1238 if (stream->state != GST_ADAPTIVE_DEMUX2_STREAM_STATE_DOWNLOADING)
1241 if (ret != GST_FLOW_OK) {
1242 GST_DEBUG_OBJECT (stream,
1243 "Buffer parsing returned: %d %s. Aborting download", ret,
1244 gst_flow_get_name (ret));
1246 if (!stream->downloading_header && !stream->downloading_index)
1247 update_stream_bitrate (stream, request);
1249 downloadhelper_cancel_request (demux->download_helper, request);
1251 /* cancellation is async, so recycle our download request to avoid races */
1252 download_request_unref (stream->download_request);
1253 stream->download_request = download_request_new ();
1255 gst_adaptive_demux2_stream_finish_download (stream, ret, NULL);
1261 on_download_complete (DownloadRequest * request, DownloadRequestState state,
1262 GstAdaptiveDemux2Stream * stream)
1264 GstFlowReturn ret = GST_FLOW_OK;
1267 stream->download_active = FALSE;
1269 if (G_UNLIKELY (stream->cancelled))
1272 GST_DEBUG_OBJECT (stream,
1273 "Stream %p %s download for %s is complete with state %d",
1274 stream, uritype (stream), request->uri, request->state);
1276 /* Update bitrate for fragment downloads */
1277 if (!stream->downloading_header && !stream->downloading_index)
1278 update_stream_bitrate (stream, request);
1280 buffer = download_request_take_buffer (request);
1282 ret = gst_adaptive_demux2_stream_parse_buffer (stream, buffer);
1284 GST_DEBUG_OBJECT (stream,
1285 "%s download finished: %s ret %d %s. Stream state %d", uritype (stream),
1286 request->uri, ret, gst_flow_get_name (ret), stream->state);
1288 if (stream->state != GST_ADAPTIVE_DEMUX2_STREAM_STATE_DOWNLOADING)
1291 g_assert (stream->pending_cb_id == 0);
1292 gst_adaptive_demux2_stream_finish_download (stream, ret, NULL);
1295 /* must be called from the scheduler context
1297 * Will submit the request only, which will complete asynchronously
1299 static GstFlowReturn
1300 gst_adaptive_demux2_stream_begin_download_uri (GstAdaptiveDemux * demux,
1301 GstAdaptiveDemux2Stream * stream, const gchar * uri, gint64 start,
1304 DownloadRequest *request = stream->download_request;
1306 GST_DEBUG_OBJECT (demux,
1307 "Downloading %s uri: %s, range:%" G_GINT64_FORMAT " - %" G_GINT64_FORMAT,
1308 uritype (stream), uri, start, end);
1310 if (!gst_adaptive_demux2_stream_create_parser (stream))
1311 return GST_FLOW_ERROR;
1313 /* Configure our download request */
1314 download_request_set_uri (request, uri, start, end);
1316 if (stream->downloading_header || stream->downloading_index) {
1317 download_request_set_callbacks (request,
1318 (DownloadRequestEventCallback) on_download_complete,
1319 (DownloadRequestEventCallback) on_download_error,
1320 (DownloadRequestEventCallback) on_download_cancellation,
1321 (DownloadRequestEventCallback) NULL, stream);
1323 download_request_set_callbacks (request,
1324 (DownloadRequestEventCallback) on_download_complete,
1325 (DownloadRequestEventCallback) on_download_error,
1326 (DownloadRequestEventCallback) on_download_cancellation,
1327 (DownloadRequestEventCallback) on_download_progress, stream);
1330 if (!downloadhelper_submit_request (demux->download_helper,
1331 demux->manifest_uri, DOWNLOAD_FLAG_NONE, request, NULL))
1332 return GST_FLOW_ERROR;
1334 stream->download_active = TRUE;
1339 /* must be called from the scheduler context */
1340 static GstFlowReturn
1341 gst_adaptive_demux2_stream_download_fragment (GstAdaptiveDemux2Stream * stream)
1343 GstAdaptiveDemux *demux = stream->demux;
1344 GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
1348 /* THERE ARE THREE DIFFERENT VARIABLES FOR THE "BEGINNING" OF A FRAGMENT ! */
1349 if (stream->starting_fragment) {
1350 GST_DEBUG_OBJECT (stream, "Downloading %s%s%s",
1351 stream->fragment.uri ? "FRAGMENT " : "",
1352 stream->need_header && stream->fragment.header_uri ? "HEADER " : "",
1353 stream->need_index && stream->fragment.index_uri ? "INDEX" : "");
1355 if (stream->fragment.uri == NULL && stream->fragment.header_uri == NULL &&
1356 stream->fragment.index_uri == NULL)
1359 stream->first_fragment_buffer = TRUE;
1360 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_DOWNLOADING;
1363 if (stream->need_header && stream->fragment.header_uri != NULL) {
1365 /* Set the need_index flag when we start the header if we'll also need the index */
1366 stream->need_index = (stream->fragment.index_uri != NULL);
1368 GST_DEBUG_OBJECT (stream, "Fetching header %s %" G_GINT64_FORMAT "-%"
1369 G_GINT64_FORMAT, stream->fragment.header_uri,
1370 stream->fragment.header_range_start, stream->fragment.header_range_end);
1372 stream->downloading_header = TRUE;
1374 return gst_adaptive_demux2_stream_begin_download_uri (demux, stream,
1375 stream->fragment.header_uri, stream->fragment.header_range_start,
1376 stream->fragment.header_range_end);
1379 /* check if we have an index */
1380 if (stream->need_index && stream->fragment.index_uri != NULL) {
1381 GST_DEBUG_OBJECT (stream,
1382 "Fetching index %s %" G_GINT64_FORMAT "-%" G_GINT64_FORMAT,
1383 stream->fragment.index_uri,
1384 stream->fragment.index_range_start, stream->fragment.index_range_end);
1386 stream->downloading_index = TRUE;
1388 return gst_adaptive_demux2_stream_begin_download_uri (demux, stream,
1389 stream->fragment.index_uri, stream->fragment.index_range_start,
1390 stream->fragment.index_range_end);
1393 url = stream->fragment.uri;
1394 GST_DEBUG_OBJECT (stream, "Got url '%s' for stream %p", url, stream);
1398 /* Download the actual fragment, either in chunks or in one go */
1399 stream->first_fragment_buffer = TRUE;
1401 if (klass->need_another_chunk && klass->need_another_chunk (stream)
1402 && stream->fragment.chunk_size != 0) {
1403 /* Handle chunk downloading */
1404 gint64 range_start = stream->fragment.range_start;
1405 gint64 range_end = stream->fragment.range_end;
1406 gint chunk_size = stream->fragment.chunk_size;
1409 /* HTTP ranges are inclusive for the end */
1410 if (chunk_size != -1) {
1411 chunk_end = range_start + chunk_size - 1;
1412 if (range_end != -1 && range_end < chunk_end)
1413 chunk_end = range_end;
1415 chunk_end = range_end;
1418 GST_DEBUG_OBJECT (stream,
1419 "Starting chunked download %s %" G_GINT64_FORMAT "-%" G_GINT64_FORMAT,
1420 url, range_start, chunk_end);
1421 return gst_adaptive_demux2_stream_begin_download_uri (demux, stream, url,
1422 range_start, chunk_end);
1425 /* regular single chunk download */
1426 stream->fragment.chunk_size = 0;
1428 return gst_adaptive_demux2_stream_begin_download_uri (demux, stream, url,
1429 stream->fragment.range_start, stream->fragment.range_end);
1433 GST_ELEMENT_ERROR (demux, STREAM, DEMUX,
1434 (_("Failed to get fragment URL.")),
1435 ("An error happened when getting fragment URL"));
1436 return GST_FLOW_ERROR;
1441 gst_adaptive_demux2_stream_push_event (GstAdaptiveDemux2Stream * stream,
1444 gboolean ret = TRUE;
1447 /* If there's a parsebin, push the event through it */
1448 if (stream->parsebin_sink != NULL) {
1449 pad = gst_object_ref (stream->parsebin_sink);
1450 GST_DEBUG_OBJECT (pad, "Pushing event %" GST_PTR_FORMAT, event);
1451 ret = gst_pad_send_event (pad, gst_event_ref (event));
1452 gst_object_unref (pad);
1455 /* If the event is EOS, ensure that all tracks are EOS. This catches
1456 * the case where the parsebin hasn't parsed anything yet (we switched
1457 * to a never before used track right near EOS, or it didn't parse enough
1458 * to create pads and be able to send EOS through to the tracks.
1460 * We don't need to care about any other events
1462 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
1465 for (iter = stream->tracks; iter; iter = iter->next) {
1466 GstAdaptiveDemuxTrack *track = (GstAdaptiveDemuxTrack *) iter->data;
1467 ret &= gst_pad_send_event (track->sinkpad, gst_event_ref (event));
1471 gst_event_unref (event);
1476 gst_adaptive_demux2_stream_error (GstAdaptiveDemux2Stream * stream)
1478 GstAdaptiveDemux *demux = stream->demux;
1480 GstStructure *details;
1482 details = gst_structure_new_empty ("details");
1483 gst_structure_set (details, "http-status-code", G_TYPE_UINT,
1484 stream->last_status_code, NULL);
1486 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_ERRORED;
1488 if (stream->last_error) {
1489 gchar *debug = g_strdup_printf ("Error on stream %s",
1490 GST_OBJECT_NAME (stream));
1492 gst_message_new_error_with_details (GST_OBJECT_CAST (demux),
1493 stream->last_error, debug, details);
1494 GST_ERROR_OBJECT (stream, "Download error: %s",
1495 stream->last_error->message);
1498 GError *err = g_error_new (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_NOT_FOUND,
1499 _("Couldn't download fragments"));
1501 gst_message_new_error_with_details (GST_OBJECT_CAST (demux), err,
1502 "Fragment downloading has failed consecutive times", details);
1504 GST_ERROR_OBJECT (stream,
1505 "Download error: Couldn't download fragments, too many failures");
1508 gst_element_post_message (GST_ELEMENT_CAST (demux), msg);
1511 /* Called when a stream reaches the end of a playback segment */
1513 gst_adaptive_demux2_stream_end_of_manifest (GstAdaptiveDemux2Stream * stream)
1515 GstAdaptiveDemux *demux = stream->demux;
1516 GstFlowReturn combined =
1517 gst_adaptive_demux_period_combine_stream_flows (demux->input_period);
1519 GST_DEBUG_OBJECT (stream, "Combined flow %s", gst_flow_get_name (combined));
1521 if (gst_adaptive_demux_has_next_period (demux)) {
1522 if (combined == GST_FLOW_EOS) {
1523 GST_DEBUG_OBJECT (stream, "Next period available, advancing");
1524 gst_adaptive_demux_advance_period (demux);
1526 /* Ensure the 'has_next_period' flag is set on the period before
1527 * pushing EOS to the stream, so that the output loop knows not
1528 * to actually output the event */
1529 GST_DEBUG_OBJECT (stream, "Marking current period has a next one");
1530 demux->input_period->has_next_period = TRUE;
1534 if (demux->priv->outputs) {
1535 GstEvent *eos = gst_event_new_eos ();
1537 GST_DEBUG_OBJECT (stream, "Stream is EOS. Stopping.");
1538 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_EOS;
1540 gst_event_set_seqnum (eos, stream->demux->priv->segment_seqnum);
1541 gst_adaptive_demux2_stream_push_event (stream, eos);
1543 GST_ERROR_OBJECT (demux, "Can't push EOS on non-exposed pad");
1544 gst_adaptive_demux2_stream_error (stream);
1549 gst_adaptive_demux2_stream_reload_manifest_cb (GstAdaptiveDemux2Stream * stream)
1551 GstAdaptiveDemux *demux = stream->demux;
1553 gboolean is_live = gst_adaptive_demux_is_live (demux);
1555 stream->pending_cb_id = 0;
1557 /* Refetch the playlist now after we waited */
1558 /* FIXME: Make this manifest update async and handle it on completion */
1559 if (!is_live && gst_adaptive_demux_update_manifest (demux) == GST_FLOW_OK) {
1560 GST_DEBUG_OBJECT (demux, "Updated the playlist");
1563 /* We were called here from a timeout, so if the load function wants to loop
1564 * again, schedule an immediate callback but return G_SOURCE_REMOVE either
1566 while (gst_adaptive_demux2_stream_next_download (stream));
1568 return G_SOURCE_REMOVE;
1572 gst_adaptive_demux2_stream_on_output_space_available_cb (GstAdaptiveDemux2Stream
1575 /* If the state already moved on, the stream was stopped, or another track
1576 * already woke up and needed data */
1577 if (stream->state != GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_OUTPUT_SPACE)
1578 return G_SOURCE_REMOVE;
1580 while (gst_adaptive_demux2_stream_load_a_fragment (stream));
1582 return G_SOURCE_REMOVE;
1586 gst_adaptive_demux2_stream_on_output_space_available (GstAdaptiveDemux2Stream *
1589 GstAdaptiveDemux *demux = stream->demux;
1592 for (iter = stream->tracks; iter; iter = iter->next) {
1593 GstAdaptiveDemuxTrack *tmp_track = (GstAdaptiveDemuxTrack *) iter->data;
1594 tmp_track->waiting_del_level = 0;
1597 GST_LOG_OBJECT (stream, "Scheduling output_space_available() call");
1599 gst_adaptive_demux_loop_call (demux->priv->scheduler_task,
1600 (GSourceFunc) gst_adaptive_demux2_stream_on_output_space_available_cb,
1601 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
1605 gst_adaptive_demux2_stream_on_manifest_update (GstAdaptiveDemux2Stream * stream)
1607 GstAdaptiveDemux *demux = stream->demux;
1609 if (stream->state != GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_MANIFEST_UPDATE)
1612 g_assert (stream->pending_cb_id == 0);
1614 GST_LOG_OBJECT (stream, "Scheduling load_a_fragment() call");
1615 stream->pending_cb_id =
1616 gst_adaptive_demux_loop_call (demux->priv->scheduler_task,
1617 (GSourceFunc) gst_adaptive_demux2_stream_load_a_fragment,
1618 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
1622 gst_adaptive_demux2_stream_handle_playlist_eos (GstAdaptiveDemux2Stream *
1625 GstAdaptiveDemux *demux = stream->demux;
1627 if (gst_adaptive_demux_is_live (demux) && (demux->segment.rate == 1.0
1628 || gst_adaptive_demux2_stream_in_live_seek_range (demux, stream))) {
1630 if (!gst_adaptive_demux_has_next_period (demux)) {
1631 /* Wait only if we can ensure current manifest has been expired.
1632 * The meaning "we have next period" *WITH* EOS is that, current
1633 * period has been ended but we can continue to the next period */
1634 GST_DEBUG_OBJECT (stream,
1635 "Live playlist EOS - waiting for manifest update");
1636 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_MANIFEST_UPDATE;
1637 gst_adaptive_demux2_stream_wants_manifest_update (demux);
1641 if (stream->replaced)
1645 gst_adaptive_demux2_stream_end_of_manifest (stream);
1649 gst_adaptive_demux2_stream_load_a_fragment (GstAdaptiveDemux2Stream * stream)
1651 GstAdaptiveDemux *demux = stream->demux;
1652 gboolean live = gst_adaptive_demux_is_live (demux);
1653 GstFlowReturn ret = GST_FLOW_OK;
1655 stream->pending_cb_id = 0;
1657 GST_LOG_OBJECT (stream, "entering, state = %d.", stream->state);
1659 switch (stream->state) {
1660 case GST_ADAPTIVE_DEMUX2_STREAM_STATE_RESTART:
1661 case GST_ADAPTIVE_DEMUX2_STREAM_STATE_START_FRAGMENT:
1662 case GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_LIVE:
1663 case GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_OUTPUT_SPACE:
1664 case GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_MANIFEST_UPDATE:
1665 /* Get information about the fragment to download */
1666 GST_DEBUG_OBJECT (demux, "Calling update_fragment_info");
1667 ret = gst_adaptive_demux2_stream_update_fragment_info (demux, stream);
1668 GST_DEBUG_OBJECT (stream,
1669 "Fragment info update result: %d %s", ret, gst_flow_get_name (ret));
1671 if (ret == GST_FLOW_OK)
1672 stream->starting_fragment = TRUE;
1674 case GST_ADAPTIVE_DEMUX2_STREAM_STATE_DOWNLOADING:
1676 case GST_ADAPTIVE_DEMUX2_STREAM_STATE_EOS:
1677 GST_ERROR_OBJECT (stream,
1678 "Unexpected stream state EOS. The stream should not be running now.");
1681 GST_ERROR_OBJECT (stream, "Unexpected stream state %d", stream->state);
1682 g_assert_not_reached ();
1686 if (ret == GST_FLOW_OK) {
1687 /* Wait for room in the output tracks */
1688 if (gst_adaptive_demux2_stream_wait_for_output_space (demux, stream,
1689 stream->fragment.duration)) {
1690 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_OUTPUT_SPACE;
1695 if (ret == GST_FLOW_OK) {
1696 /* wait for live fragments to be available */
1698 GstClockTime wait_time =
1699 gst_adaptive_demux2_stream_get_fragment_waiting_time (demux, stream);
1700 if (wait_time > 0) {
1701 GST_DEBUG_OBJECT (stream,
1702 "Download waiting for %" GST_TIME_FORMAT,
1703 GST_TIME_ARGS (wait_time));
1705 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_WAITING_LIVE;
1707 GST_LOG_OBJECT (stream, "Scheduling delayed load_a_fragment() call");
1708 g_assert (stream->pending_cb_id == 0);
1709 stream->pending_cb_id =
1710 gst_adaptive_demux_loop_call_delayed (demux->priv->scheduler_task,
1711 wait_time, (GSourceFunc) gst_adaptive_demux2_stream_load_a_fragment,
1712 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
1717 if (gst_adaptive_demux2_stream_download_fragment (stream) != GST_FLOW_OK) {
1718 GST_ERROR_OBJECT (demux,
1719 "Failed to begin fragment download for stream %p", stream);
1726 break; /* all is good, let's go */
1728 GST_DEBUG_OBJECT (stream, "EOS, checking to stop download loop");
1729 gst_adaptive_demux2_stream_handle_playlist_eos (stream);
1731 case GST_ADAPTIVE_DEMUX_FLOW_LOST_SYNC:
1732 GST_DEBUG_OBJECT (stream, "Lost sync, asking reset to current position");
1733 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_STOPPED;
1734 gst_adaptive_demux_handle_lost_sync (demux);
1736 case GST_FLOW_NOT_LINKED:
1738 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_EOS;
1740 if (gst_adaptive_demux_period_combine_stream_flows (demux->input_period)
1741 == GST_FLOW_NOT_LINKED) {
1742 GST_ELEMENT_FLOW_ERROR (demux, ret);
1747 case GST_FLOW_FLUSHING:
1748 /* Flushing is normal, the target track might have been unselected */
1749 if (G_UNLIKELY (stream->cancelled))
1753 if (ret <= GST_FLOW_ERROR) {
1754 GST_WARNING_OBJECT (demux, "Error while downloading fragment");
1755 if (++stream->download_error_count > MAX_DOWNLOAD_ERROR_COUNT) {
1756 gst_adaptive_demux2_stream_error (stream);
1760 g_clear_error (&stream->last_error);
1762 /* First try to update the playlist for non-live playlists
1763 * in case the URIs have changed in the meantime. But only
1764 * try it the first time, after that we're going to wait a
1765 * a bit to not flood the server */
1766 if (stream->download_error_count == 1
1767 && !gst_adaptive_demux_is_live (demux)) {
1768 /* TODO hlsdemux had more options to this function (boolean and err) */
1769 if (gst_adaptive_demux_update_manifest (demux) == GST_FLOW_OK) {
1770 /* Retry immediately, the playlist actually has changed */
1771 GST_DEBUG_OBJECT (demux, "Updated the playlist");
1776 /* Wait half the fragment duration before retrying */
1777 GST_LOG_OBJECT (stream, "Scheduling delayed reload_manifest_cb() call");
1778 g_assert (stream->pending_cb_id == 0);
1779 stream->pending_cb_id =
1780 gst_adaptive_demux_loop_call_delayed (demux->priv->scheduler_task,
1781 stream->fragment.duration / 2,
1782 (GSourceFunc) gst_adaptive_demux2_stream_reload_manifest_cb,
1783 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
1793 gst_adaptive_demux2_stream_next_download (GstAdaptiveDemux2Stream * stream)
1795 GstAdaptiveDemux *demux = stream->demux;
1796 gboolean end_of_manifest = FALSE;
1798 GST_LOG_OBJECT (stream, "Looking for next download");
1800 /* Restarting download, figure out new position
1801 * FIXME : Move this to a separate function ? */
1802 if (G_UNLIKELY (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_RESTART)) {
1803 GstClockTimeDiff stream_time = 0;
1805 GST_DEBUG_OBJECT (stream, "Activating stream after restart");
1807 if (stream->parsebin_sink != NULL) {
1808 /* If the parsebin already exists, we need to clear it out (if it doesn't,
1809 * this is the first time we've used this stream, so it's all good) */
1810 gst_adaptive_demux2_stream_push_event (stream,
1811 gst_event_new_flush_start ());
1812 gst_adaptive_demux2_stream_push_event (stream,
1813 gst_event_new_flush_stop (FALSE));
1816 GST_ADAPTIVE_DEMUX_SEGMENT_LOCK (demux);
1817 stream_time = stream->start_position;
1819 GST_DEBUG_OBJECT (stream, "Restarting stream at "
1820 "stream position %" GST_STIME_FORMAT, GST_STIME_ARGS (stream_time));
1822 if (GST_CLOCK_STIME_IS_VALID (stream_time)) {
1823 /* TODO check return */
1824 gst_adaptive_demux2_stream_seek (demux, stream, demux->segment.rate >= 0,
1825 0, stream_time, &stream_time);
1826 stream->current_position = stream->start_position;
1828 GST_DEBUG_OBJECT (stream,
1829 "stream_time after restart seek: %" GST_STIME_FORMAT
1830 " position %" GST_STIME_FORMAT, GST_STIME_ARGS (stream_time),
1831 GST_STIME_ARGS (stream->current_position));
1834 /* Trigger (re)computation of the parsebin input segment */
1835 stream->compute_segment = TRUE;
1837 GST_ADAPTIVE_DEMUX_SEGMENT_UNLOCK (demux);
1839 stream->discont = TRUE;
1840 stream->need_header = TRUE;
1841 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_START_FRAGMENT;
1844 /* Check if we're done with our segment */
1845 GST_ADAPTIVE_DEMUX_SEGMENT_LOCK (demux);
1846 if (demux->segment.rate > 0) {
1847 if (GST_CLOCK_TIME_IS_VALID (demux->segment.stop)
1848 && stream->current_position >= demux->segment.stop) {
1849 end_of_manifest = TRUE;
1852 if (GST_CLOCK_TIME_IS_VALID (demux->segment.start)
1853 && stream->current_position <= demux->segment.start) {
1854 end_of_manifest = TRUE;
1857 GST_ADAPTIVE_DEMUX_SEGMENT_UNLOCK (demux);
1859 if (end_of_manifest) {
1860 gst_adaptive_demux2_stream_end_of_manifest (stream);
1863 return gst_adaptive_demux2_stream_load_a_fragment (stream);
1867 gst_adaptive_demux2_stream_can_start (GstAdaptiveDemux2Stream * stream)
1869 GstAdaptiveDemux *demux = stream->demux;
1870 GstAdaptiveDemuxClass *klass = GST_ADAPTIVE_DEMUX_GET_CLASS (demux);
1872 if (!klass->stream_can_start)
1874 return klass->stream_can_start (demux, stream);
1878 * gst_adaptive_demux2_stream_start:
1879 * @stream: a #GstAdaptiveDemux2Stream
1881 * Start the given @stream. Should be called by subclasses that previously
1882 * returned %FALSE in `GstAdaptiveDemux::stream_can_start()`
1885 gst_adaptive_demux2_stream_start (GstAdaptiveDemux2Stream * stream)
1887 GstAdaptiveDemux *demux;
1889 g_return_if_fail (stream && stream->demux);
1891 demux = stream->demux;
1893 if (stream->pending_cb_id != 0 || stream->download_active) {
1894 /* There is already something active / pending on this stream */
1895 GST_LOG_OBJECT (stream, "Stream already running");
1899 /* Some streams require a delayed start, i.e. they need more information
1900 * before they can actually be started */
1901 if (!gst_adaptive_demux2_stream_can_start (stream)) {
1902 GST_LOG_OBJECT (stream, "Stream will be started asynchronously");
1906 if (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_EOS) {
1907 GST_LOG_OBJECT (stream, "Stream is EOS already");
1911 if (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_STOPPED ||
1912 stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_RESTART) {
1913 GST_LOG_OBJECT (stream, "Activating stream. Current state %d",
1915 stream->cancelled = FALSE;
1916 stream->replaced = FALSE;
1917 stream->last_ret = GST_FLOW_OK;
1919 if (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_STOPPED)
1920 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_START_FRAGMENT;
1923 GST_LOG_OBJECT (stream, "Scheduling next_download() call");
1924 stream->pending_cb_id =
1925 gst_adaptive_demux_loop_call (demux->priv->scheduler_task,
1926 (GSourceFunc) gst_adaptive_demux2_stream_next_download,
1927 gst_object_ref (stream), (GDestroyNotify) gst_object_unref);
1931 gst_adaptive_demux2_stream_stop (GstAdaptiveDemux2Stream * stream)
1933 GstAdaptiveDemux *demux = stream->demux;
1935 GST_DEBUG_OBJECT (stream, "Stopping stream (from state %d)", stream->state);
1936 stream->state = GST_ADAPTIVE_DEMUX2_STREAM_STATE_STOPPED;
1938 if (stream->pending_cb_id != 0) {
1939 gst_adaptive_demux_loop_cancel_call (demux->priv->scheduler_task,
1940 stream->pending_cb_id);
1941 stream->pending_cb_id = 0;
1944 /* Cancel and drop the existing download request */
1945 downloadhelper_cancel_request (demux->download_helper,
1946 stream->download_request);
1947 download_request_unref (stream->download_request);
1948 stream->downloading_header = stream->downloading_index = FALSE;
1949 stream->download_request = download_request_new ();
1950 stream->download_active = FALSE;
1954 gst_adaptive_demux2_stream_is_running (GstAdaptiveDemux2Stream * stream)
1956 if (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_STOPPED)
1958 if (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_RESTART)
1960 if (stream->state == GST_ADAPTIVE_DEMUX2_STREAM_STATE_EOS)
1966 gst_adaptive_demux2_stream_is_selected_locked (GstAdaptiveDemux2Stream * stream)
1970 for (tmp = stream->tracks; tmp; tmp = tmp->next) {
1971 GstAdaptiveDemuxTrack *track = tmp->data;
1972 if (track->selected)
1980 * gst_adaptive_demux2_stream_is_selected:
1981 * @stream: A #GstAdaptiveDemux2Stream
1983 * Returns: %TRUE if any of the tracks targetted by @stream is selected
1986 gst_adaptive_demux2_stream_is_selected (GstAdaptiveDemux2Stream * stream)
1990 g_return_val_if_fail (stream && stream->demux, FALSE);
1992 TRACKS_LOCK (stream->demux);
1993 ret = gst_adaptive_demux2_stream_is_selected_locked (stream);
1994 TRACKS_UNLOCK (stream->demux);