Debian Codegraph @

gst-libs/gst/mse/gstmsesrc.c

raw symbols SCIP
1 /* GStreamer
2 *
3 * SPDX-License-Identifier: LGPL-2.1
4 *
5 * Copyright (C) 2009, 2010 Sebastian Dröge <[email protected]>
6 * Copyright (C) 2013, 2022, 2023 Collabora Ltd.
7 * Copyright (C) 2013 Orange
8 * Copyright (C) 2014, 2015 Sebastian Dröge <[email protected]>
9 * Copyright (C) 2015, 2016, 2018, 2019, 2020, 2021 Igalia, S.L
10 * Copyright (C) 2015, 2016, 2018, 2019, 2020, 2021 Metrological Group B.V.
11 *
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Library General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
16 *
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Library General Public License for more details.
21 *
22 * You should have received a copy of the GNU Library General Public
23 * License along with this library; if not, write to the
24 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 * Boston, MA 02110-1301, USA.
26 */
27
28 /**
29 * SECTION:gstmsesrc
30 * @title: GstMseSrc
31 * @short_description: Source Element for Media Source playback
32 *
33 * #GstMseSrc is a source Element that interacts with a #GstMediaSource to
34 * consume #GstSample<!-- -->s processed by the Media Source and supplies them
35 * to the containing #GstPipeline. In the perspective of the Media Source API,
36 * this element fulfills the basis of the Media Element's role relating to
37 * working with a Media Source. The remaining responsibilities are meant to be
38 * fulfilled by the application and #GstPlay can be used to satisfy many of
39 * them.
40 *
41 * Once added to a Pipeline, this element should be attached to a Media Source
42 * using gst_media_source_attach().
43 *
44 * Since: 1.24
45 */
46
47 #ifdef HAVE_CONFIG_H
48 #include "config.h"
49 #endif
50
51 #include <gst/gst.h>
52 #include <gst/base/base.h>
53
54 #include <gst/mse/mse-enumtypes.h>
55 #include "gstmsesrc.h"
56 #include "gstmsesrc-private.h"
57
58 #include "gstmselogging-private.h"
59
60 #include "gstmediasource.h"
61 #include "gstmediasource-private.h"
62 #include "gstmediasourcetrack-private.h"
63 #include "gstsourcebuffer.h"
64 #include "gstsourcebuffer-private.h"
65
66 #define DEFAULT_POSITION 0
67 #define DEFAULT_DURATION GST_CLOCK_TIME_NONE
68 #define DEFAULT_READY_STATE GST_MSE_SRC_READY_STATE_HAVE_NOTHING
69 #define DECODE_ERROR "decode error"
70 #define NETWORK_ERROR "network error"
71
72 enum
73 {
74 PROP_0,
75
76 PROP_POSITION,
77 PROP_DURATION,
78 PROP_READY_STATE,
79
80 PROP_N_AUDIO,
81 PROP_N_TEXT,
82 PROP_N_VIDEO,
83
84 N_PROPS,
85 };
86
87 enum
88 {
89 THRESHOLD_FUTURE_DATA = GST_SECOND * 5,
90 THRESHOLD_ENOUGH_DATA = GST_SECOND * 50,
91 };
92
93 static GParamSpec *properties[N_PROPS];
94
95 static GstStaticPadTemplate gst_mse_src_template =
96 GST_STATIC_PAD_TEMPLATE ("src_%s", GST_PAD_SRC, GST_PAD_SOMETIMES,
97 GST_STATIC_CAPS_ANY);
98
99 typedef struct
100 {
101 GWeakRef parent;
102 GstTask *task;
103 GRecMutex mutex;
104 } ReadyStateUpdateTask;
105
106 /**
107 * GstMseSrcPad:
108 *
109 * Since: 1.24
110 */
111 struct _GstMseSrcPad
112 {
113 GstPad base;
114
115 GstStream *stream;
116 GstMediaSourceTrack *track;
117 GstCaps *most_recent_caps;
118 GstSegment segment;
119
120 gboolean sent_stream_collection;
121 gboolean sent_stream_start;
122 gboolean sent_initial_caps;
123 gboolean does_need_segment;
124
125 GCond linked_or_flushing_cond;
126 GMutex linked_or_flushing_lock;
127 gboolean flushing;
128 };
129
130 #define MEDIA_SOURCE_LOCK(a) g_mutex_lock (&(a)->media_source_lock)
131 #define MEDIA_SOURCE_UNLOCK(a) g_mutex_unlock (&(a)->media_source_lock)
132
133 #define STREAMS_LOCK(a) (g_mutex_lock (&a->streams_lock))
134 #define STREAMS_UNLOCK(a) (g_mutex_unlock (&a->streams_lock))
135
136 #define LINKED_OR_FLUSHING_LOCK(a) (g_mutex_lock (&a->linked_or_flushing_lock))
137 #define LINKED_OR_FLUSHING_UNLOCK(a) (g_mutex_unlock (&a->linked_or_flushing_lock))
138 #define LINKED_OR_FLUSHING_SIGNAL(a) (g_cond_signal (&a->linked_or_flushing_cond))
139 #define LINKED_OR_FLUSHING_WAIT(a) \
140 (g_cond_wait (&a->linked_or_flushing_cond, &a->linked_or_flushing_lock))
141
142 #define FLOW_COMBINER_LOCK(a) (g_mutex_lock (&a->flow_combiner_lock))
143 #define FLOW_COMBINER_UNLOCK(a) (g_mutex_unlock (&a->flow_combiner_lock))
144
145 G_DEFINE_TYPE (GstMseSrcPad, gst_mse_src_pad, GST_TYPE_PAD);
146
147 static gboolean pad_activate_mode (GstMseSrcPad * pad, GstObject * parent,
148 GstPadMode mode, gboolean active);
149
150 static GstPadLinkReturn pad_linked (GstMseSrcPad * pad, GstMseSrc * parent,
151 GstPad * sink);
152 static gboolean pad_event (GstMseSrcPad * pad, GstMseSrc * parent,
153 GstEvent * event);
154 static gboolean pad_query (GstMseSrcPad * pad, GstObject * parent,
155 GstQuery * query);
156 static void pad_task (GstMseSrcPad * pad);
157
158 static ReadyStateUpdateTask *ready_state_update_task_new (GstMseSrc * parent);
159 static void ready_state_update_task_free (ReadyStateUpdateTask * task);
160 static void ready_state_update_task_func (ReadyStateUpdateTask * task);
161 static void ready_state_update_task_start (ReadyStateUpdateTask * task);
162 static void ready_state_update_task_stop (ReadyStateUpdateTask * task);
163 static void ready_state_update_task_join (ReadyStateUpdateTask * task);
164
165 static const gchar *
166 mse_src_ready_state_name (GstMseSrcReadyState state)
167 {
168 return gst_mse_enum_value_nick (gst_mse_src_ready_state_get_type (), state);
169 }
170
171 static GstPad *
172 gst_mse_src_pad_new (GstMediaSourceTrack * track, GstStream * stream,
173 guint id, GstClockTime start, gdouble rate)
174 {
175 gchar *name = g_strdup_printf ("src_%u", id);
176 GstMseSrcPad *self = g_object_new (GST_TYPE_MSE_SRC_PAD, "name", name,
177 "direction", GST_PAD_SRC, NULL);
178 g_free (name);
179 self->stream = stream;
180 self->track = track;
181 self->segment.start = start;
182 self->segment.rate = rate;
183
184 return GST_PAD (self);
185 }
186
187 static void
188 gst_mse_src_pad_init (GstMseSrcPad * self)
189 {
190 gst_segment_init (&self->segment, GST_FORMAT_TIME);
191 self->sent_stream_collection = FALSE;
192 self->sent_stream_start = FALSE;
193 self->sent_initial_caps = FALSE;
194 self->does_need_segment = TRUE;
195 self->flushing = FALSE;
196 g_mutex_init (&self->linked_or_flushing_lock);
197 g_cond_init (&self->linked_or_flushing_cond);
198
199 gst_pad_set_activatemode_function (GST_PAD (self),
200 (GstPadActivateModeFunction) pad_activate_mode);
201 gst_pad_set_link_function (GST_PAD (self), (GstPadLinkFunction) pad_linked);
202 gst_pad_set_event_function (GST_PAD (self), (GstPadEventFunction) pad_event);
203 gst_pad_set_query_function (GST_PAD (self), (GstPadQueryFunction) pad_query);
204 }
205
206 static void
207 gst_mse_src_pad_finalize (GObject * object)
208 {
209 GstMseSrcPad *self = GST_MSE_SRC_PAD (object);
210
211 gst_clear_caps (&self->most_recent_caps);
212 g_mutex_clear (&self->linked_or_flushing_lock);
213 g_cond_clear (&self->linked_or_flushing_cond);
214
215 G_OBJECT_CLASS (gst_mse_src_pad_parent_class)->finalize (object);
216 }
217
218 static void
219 gst_mse_src_pad_class_init (GstMseSrcPadClass * klass)
220 {
221 GObjectClass *oclass = G_OBJECT_CLASS (klass);
222 oclass->finalize = GST_DEBUG_FUNCPTR (gst_mse_src_pad_finalize);
223 }
224
225 // TODO: Check if this struct is even necessary.
226 // The custom pad should be able to keep track of information for each track.
227 typedef struct
228 {
229 GstMediaSourceTrack *track;
230 GstPad *pad;
231 GstStream *info;
232 } Stream;
233
234 /**
235 * GstMseSrc:
236 *
237 * Since: 1.24
238 */
239 struct _GstMseSrc
240 {
241 GstElement base;
242
243 GstMediaSource *media_source;
244 GMutex media_source_lock;
245
246 guint group_id;
247 GstStreamCollection *collection;
248 GHashTable *streams;
249 GMutex streams_lock;
250
251 GstClockTime duration;
252 GstClockTime start_time;
253 gdouble rate;
254 GstMseSrcReadyState ready_state;
255
256 ReadyStateUpdateTask *ready_state_update_task;
257
258 GstFlowCombiner *flow_combiner;
259 GMutex flow_combiner_lock;
260
261 gchar *uri;
262 };
263
264 static void gst_mse_src_uri_handler_init (gpointer g_iface,
265 gpointer iface_data);
266 static GstStateChangeReturn gst_mse_src_change_state (GstElement * element,
267 GstStateChange transition);
268 static gboolean gst_mse_src_send_event (GstElement * element, GstEvent * event);
269 static void update_ready_state_for_init_segment (GstMseSrc * self);
270 static void update_ready_state (GstMseSrc * self);
271
272 G_DEFINE_TYPE_WITH_CODE (GstMseSrc, gst_mse_src, GST_TYPE_ELEMENT,
273 G_IMPLEMENT_INTERFACE (GST_TYPE_URI_HANDLER, gst_mse_src_uri_handler_init));
274
275 static void
276 gst_mse_src_constructed (GObject * object)
277 {
278 GstMseSrc *self = GST_MSE_SRC (object);
279
280 G_OBJECT_CLASS (gst_mse_src_parent_class)->constructed (object);
281 GST_OBJECT_FLAG_SET (self, GST_ELEMENT_FLAG_SOURCE);
282 }
283
284 static void
285 gst_mse_src_dispose (GObject * object)
286 {
287 GstMseSrc *self = GST_MSE_SRC (object);
288 g_clear_pointer (&self->ready_state_update_task,
289 ready_state_update_task_free);
290 gst_clear_object (&self->media_source);
291 g_mutex_clear (&self->media_source_lock);
292 gst_clear_object (&self->collection);
293 g_clear_pointer (&self->streams, g_hash_table_unref);
294 g_mutex_clear (&self->streams_lock);
295 g_clear_pointer (&self->flow_combiner, gst_flow_combiner_free);
296 g_mutex_clear (&self->flow_combiner_lock);
297 G_OBJECT_CLASS (gst_mse_src_parent_class)->dispose (object);
298 }
299
300 static void
301 gst_mse_src_finalize (GObject * object)
302 {
303 GstMseSrc *self = GST_MSE_SRC (object);
304
305 g_clear_pointer (&self->uri, g_free);
306
307 G_OBJECT_CLASS (gst_mse_src_parent_class)->finalize (object);
308 }
309
310 static void
311 gst_mse_src_get_property (GObject * object, guint prop_id, GValue * value,
312 GParamSpec * pspec)
313 {
314 GstMseSrc *self = GST_MSE_SRC (object);
315
316 switch (prop_id) {
317 case PROP_DURATION:
318 g_value_set_uint64 (value, gst_mse_src_get_duration (self));
319 break;
320 case PROP_POSITION:
321 g_value_set_uint64 (value, gst_mse_src_get_position (self));
322 break;
323 case PROP_READY_STATE:
324 g_value_set_enum (value, gst_mse_src_get_ready_state (self));
325 break;
326 case PROP_N_AUDIO:
327 g_value_set_uint (value, gst_mse_src_get_n_audio (self));
328 break;
329 case PROP_N_TEXT:
330 g_value_set_uint (value, gst_mse_src_get_n_text (self));
331 break;
332 case PROP_N_VIDEO:
333 g_value_set_uint (value, gst_mse_src_get_n_video (self));
334 break;
335 default:
336 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
337 break;
338 }
339 }
340
341 static void
342 gst_mse_src_set_property (GObject * object, guint prop_id,
343 const GValue * value, GParamSpec * pspec)
344 {
345 GstMseSrc *self = GST_MSE_SRC (object);
346
347 switch (prop_id) {
348 case PROP_DURATION:
349 gst_mse_src_set_duration (self, g_value_get_uint64 (value));
350 break;
351 default:
352 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
353 break;
354 }
355 }
356
357 static void
358 gst_mse_src_class_init (GstMseSrcClass * klass)
359 {
360 GObjectClass *oclass = (GObjectClass *) klass;
361 GstElementClass *eclass = (GstElementClass *) klass;
362
363 oclass->constructed = GST_DEBUG_FUNCPTR (gst_mse_src_constructed);
364 oclass->finalize = GST_DEBUG_FUNCPTR (gst_mse_src_finalize);
365 oclass->dispose = GST_DEBUG_FUNCPTR (gst_mse_src_dispose);
366 oclass->get_property = GST_DEBUG_FUNCPTR (gst_mse_src_get_property);
367 oclass->set_property = GST_DEBUG_FUNCPTR (gst_mse_src_set_property);
368
369 eclass->change_state = GST_DEBUG_FUNCPTR (gst_mse_src_change_state);
370 eclass->send_event = GST_DEBUG_FUNCPTR (gst_mse_src_send_event);
371
372 /**
373 * GstMseSrc:position:
374 *
375 * The playback position as a #GstClockTime
376 *
377 * [Specification](https://html.spec.whatwg.org/multipage/media.html#current-playback-position)
378 *
379 * Since: 1.24
380 */
381 properties[PROP_POSITION] = g_param_spec_uint64 ("position",
382 "Position",
383 "The playback position as a GstClockTime",
384 0, G_MAXUINT64, DEFAULT_POSITION, G_PARAM_READABLE |
385 G_PARAM_STATIC_STRINGS);
386
387 /**
388 * GstMseSrc:duration:
389 *
390 * The duration of the stream as a #GstClockTime
391 *
392 * [Specification](https://html.spec.whatwg.org/multipage/media.html#dom-media-duration)
393 *
394 * Since: 1.24
395 */
396 properties[PROP_DURATION] = g_param_spec_uint64 ("duration",
397 "Duration",
398 "The duration of the stream as a GstClockTime",
399 0, G_MAXUINT64, DEFAULT_DURATION, G_PARAM_READWRITE |
400 G_PARAM_STATIC_STRINGS);
401
402 /**
403 * GstMseSrc:ready-state:
404 *
405 * The Ready State of this element, describing to what level it can supply
406 * content for the current #GstMseSrc:position. This is a separate concept
407 * from #GstMediaSource:ready-state: and corresponds to the HTML Media
408 * Element's Ready State.
409 *
410 * [Specification](https://html.spec.whatwg.org/multipage/media.html#ready-states)
411 *
412 * Since: 1.24
413 */
414 properties[PROP_READY_STATE] = g_param_spec_enum ("ready-state",
415 "Ready State",
416 "The Ready State of this Element",
417 GST_TYPE_MSE_SRC_READY_STATE,
418 DEFAULT_READY_STATE, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
419
420 /**
421 * GstMseSrc:n-audio:
422 *
423 * The number of audio tracks in the Media Source
424 *
425 * Since: 1.24
426 */
427 properties[PROP_N_AUDIO] = g_param_spec_uint ("n-audio",
428 "Number of Audio Tracks",
429 "The number of audio tracks in the Media Source",
430 0, G_MAXINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
431
432 /**
433 * GstMseSrc:n-text:
434 *
435 * The number of text tracks in the Media Source
436 *
437 * Since: 1.24
438 */
439 properties[PROP_N_TEXT] = g_param_spec_uint ("n-text",
440 "Number of Text Tracks",
441 "The number of text tracks in the Media Source",
442 0, G_MAXINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
443
444 /**
445 * GstMseSrc:n-video:
446 *
447 * The number of video tracks in the Media Source
448 *
449 * Since: 1.24
450 */
451 properties[PROP_N_VIDEO] = g_param_spec_uint ("n-video",
452 "Number of Video Tracks",
453 "The number of video tracks in the Media Source",
454 0, G_MAXINT, 0, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
455
456 g_object_class_install_properties (oclass, N_PROPS, properties);
457
458 gst_element_class_set_static_metadata (eclass, "MseSrc",
459 "Generic/Source",
460 "Implements a GStreamer Source for the gstreamer-mse API", "Collabora");
461 gst_element_class_add_static_pad_template (eclass, &gst_mse_src_template);
462
463 gst_mse_init_logging ();
464 }
465
466 static void
467 clear_stream (Stream * stream)
468 {
469 gst_clear_object (&stream->track);
470 gst_clear_object (&stream->info);
471 g_free (stream);
472 }
473
474 static GHashTable *
475 streams_init (const GstMseSrc * self)
476 {
477 return g_hash_table_new_full (g_direct_hash, g_direct_equal,
478 NULL, (GDestroyNotify) clear_stream);
479 }
480
481 static GstStreamCollection *
482 collection_init (const GstMseSrc * self)
483 {
484 return gst_stream_collection_new (G_OBJECT_TYPE_NAME (self));
485 }
486
487 static ReadyStateUpdateTask *
488 ready_state_update_task_new (GstMseSrc * parent)
489 {
490 ReadyStateUpdateTask *task = g_new0 (ReadyStateUpdateTask, 1);
491 g_rec_mutex_init (&task->mutex);
492 g_weak_ref_init (&task->parent, parent);
493 task->task =
494 gst_task_new ((GstTaskFunction) ready_state_update_task_func, task, NULL);
495 gst_task_set_lock (task->task, &task->mutex);
496 return task;
497 }
498
499 static void
500 ready_state_update_task_free (ReadyStateUpdateTask * task)
501 {
502 g_weak_ref_set (&task->parent, NULL);
503 gst_task_join (task->task);
504 gst_clear_object (&task->task);
505 g_weak_ref_clear (&task->parent);
506 g_rec_mutex_clear (&task->mutex);
507 g_free (task);
508 }
509
510 static void
511 ready_state_update_task_start (ReadyStateUpdateTask * task)
512 {
513 GstMseSrc *parent = g_weak_ref_get (&task->parent);
514 if (parent) {
515 gchar *name = g_strdup_printf ("%s:ready-state", GST_OBJECT_NAME (parent));
516 g_object_set (task->task, "name", name, NULL);
517 g_clear_pointer (&name, g_free);
518 }
519 gst_clear_object (&parent);
520 gst_task_start (task->task);
521 }
522
523 static void
524 ready_state_update_task_stop (ReadyStateUpdateTask * task)
525 {
526 gst_task_stop (task->task);
527 }
528
529 static void
530 ready_state_update_task_join (ReadyStateUpdateTask * task)
531 {
532 gst_task_join (task->task);
533 }
534
535 static void
536 ready_state_update_task_func (ReadyStateUpdateTask * task)
537 {
538 GstMseSrc *self = (GstMseSrc *) g_weak_ref_get (&task->parent);
539
540 if (self == NULL) {
541 GST_ERROR_OBJECT (task->task, "parent object is gone, stopping");
542 gst_task_stop (task->task);
543 return;
544 }
545
546 update_ready_state (self);
547 gst_object_unref (self);
548 g_usleep (G_TIME_SPAN_SECOND);
549 }
550
551 static void
552 gst_mse_src_init (GstMseSrc * self)
553 {
554 self->group_id = gst_util_group_id_next ();
555 self->streams = streams_init (self);
556 self->collection = collection_init (self);
557 self->uri = NULL;
558 self->start_time = 0;
559 self->rate = 1;
560 self->media_source = NULL;
561 g_mutex_init (&self->media_source_lock);
562 g_mutex_init (&self->streams_lock);
563 self->flow_combiner = gst_flow_combiner_new ();
564 g_mutex_init (&self->flow_combiner_lock);
565 self->ready_state_update_task = ready_state_update_task_new (self);
566 }
567
568 /**
569 * gst_mse_src_get_position:
570 * @self: #GstMseSrc instance
571 *
572 * Gets the current playback position of @self.
573 *
574 * [Specification](https://html.spec.whatwg.org/multipage/media.html#current-playback-position)
575 *
576 * Returns: The playback position of this Element as a #GstClockTime
577 * Since: 1.24
578 */
579 GstClockTime
580 gst_mse_src_get_position (GstMseSrc * self)
581 {
582 g_return_val_if_fail (GST_IS_MSE_SRC (self), GST_CLOCK_TIME_NONE);
583 gint64 position;
584 gboolean success = gst_element_query_position (GST_ELEMENT (self),
585 GST_FORMAT_TIME, &position);
586 if (success)
587 return (GstClockTime) position;
588 else
589 return DEFAULT_POSITION;
590 }
591
592 static void
593 update_pad_duration (GstMseSrc * self, GstMseSrcPad * pad)
594 {
595 pad->segment.duration = self->duration;
596 pad->does_need_segment = TRUE;
597 }
598
599 void
600 gst_mse_src_set_duration (GstMseSrc * self, GstClockTime duration)
601 {
602 g_return_if_fail (GST_IS_MSE_SRC (self));
603
604 self->duration = duration;
605
606 gst_element_foreach_src_pad (GST_ELEMENT (self),
607 (GstElementForeachPadFunc) update_pad_duration, NULL);
608
609 gst_element_post_message (GST_ELEMENT (self),
610 gst_message_new_duration_changed (GST_OBJECT (self)));
611 }
612
613 /**
614 * gst_mse_src_get_duration:
615 * @self: #GstMseSrc instance
616 *
617 * Gets the duration of @self.
618 *
619 * [Specification](https://html.spec.whatwg.org/multipage/media.html#dom-media-duration)
620 *
621 * Returns: The duration of this stream as a #GstClockTime
622 * Since: 1.24
623 */
624 GstClockTime
625 gst_mse_src_get_duration (GstMseSrc * self)
626 {
627 g_return_val_if_fail (GST_IS_MSE_SRC (self), DEFAULT_DURATION);
628 return self->duration;
629 }
630
631 /**
632 * gst_mse_src_get_ready_state:
633 * @self: #GstMseSrc instance
634 *
635 * The Ready State of @self, describing to what level it can supply content for
636 * the current #GstMseSrc:position. This is a separate concept from
637 * #GstMediaSource:ready-state: and corresponds to the HTML Media Element's
638 * Ready State.
639 *
640 * [Specification](https://html.spec.whatwg.org/multipage/media.html#ready-states)
641 *
642 * Returns: the current #GstMseSrcReadyState
643 * Since: 1.24
644 */
645 GstMseSrcReadyState
646 gst_mse_src_get_ready_state (GstMseSrc * self)
647 {
648 g_return_val_if_fail (GST_IS_MSE_SRC (self), DEFAULT_READY_STATE);
649 return self->ready_state;
650 }
651
652 static guint
653 n_streams_by_type (GstMseSrc * self, GstMediaSourceTrackType type)
654 {
655 guint count = 0;
656 GHashTableIter iter;
657 g_hash_table_iter_init (&iter, self->streams);
658 for (gpointer key; g_hash_table_iter_next (&iter, &key, NULL);) {
659 GstMediaSourceTrack *track = GST_MEDIA_SOURCE_TRACK (key);
660 GstMediaSourceTrackType stream_type =
661 gst_media_source_track_get_track_type (track);
662 if (type == stream_type) {
663 count++;
664 }
665 }
666 return count;
667 }
668
669 /**
670 * gst_mse_src_get_n_audio:
671 * @self: #GstMseSrc instance
672 *
673 * Returns: the number of audio tracks available from this source
674 * Since: 1.24
675 */
676 guint
677 gst_mse_src_get_n_audio (GstMseSrc * self)
678 {
679 g_return_val_if_fail (GST_IS_MSE_SRC (self), 0);
680 return n_streams_by_type (self, GST_MEDIA_SOURCE_TRACK_TYPE_AUDIO);
681 }
682
683 /**
684 * gst_mse_src_get_n_text:
685 * @self: #GstMseSrc instance
686 *
687 * Returns: the number of text tracks available from this source
688 * Since: 1.24
689 */
690 guint
691 gst_mse_src_get_n_text (GstMseSrc * self)
692 {
693 g_return_val_if_fail (GST_IS_MSE_SRC (self), 0);
694 return n_streams_by_type (self, GST_MEDIA_SOURCE_TRACK_TYPE_TEXT);
695 }
696
697 /**
698 * gst_mse_src_get_n_video:
699 * @self: #GstMseSrc instance
700 *
701 * Returns: the number of video tracks available from this source
702 * Since: 1.24
703 */
704 guint
705 gst_mse_src_get_n_video (GstMseSrc * self)
706 {
707 g_return_val_if_fail (GST_IS_MSE_SRC (self), 0);
708 return n_streams_by_type (self, GST_MEDIA_SOURCE_TRACK_TYPE_VIDEO);
709 }
710
711 void
712 gst_mse_src_decode_error (GstMseSrc * self)
713 {
714 g_return_if_fail (GST_IS_MSE_SRC (self));
715 GstMseSrcReadyState ready_state = g_atomic_int_get (&self->ready_state);
716 if (ready_state == GST_MSE_SRC_READY_STATE_HAVE_NOTHING) {
717 GST_ELEMENT_ERROR (self, STREAM, DECODE, (DECODE_ERROR),
718 ("the necessary decoder may be missing from this installation"));
719 } else {
720 GST_ELEMENT_ERROR (self, STREAM, DECODE, (DECODE_ERROR),
721 ("the stream may be corrupt"));
722 }
723 }
724
725 void
726 gst_mse_src_network_error (GstMseSrc * self)
727 {
728 g_return_if_fail (GST_IS_MSE_SRC (self));
729 GstMseSrcReadyState ready_state = g_atomic_int_get (&self->ready_state);
730 if (ready_state == GST_MSE_SRC_READY_STATE_HAVE_NOTHING) {
731 GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ, (NETWORK_ERROR),
732 ("an error occurred before any media was read"));
733 } else {
734 GST_ELEMENT_ERROR (self, RESOURCE, READ, (NETWORK_ERROR),
735 ("an error occurred while reading media"));
736 }
737 }
738
739 static inline gboolean
740 is_streamable (GstMediaSourceTrack * track)
741 {
742 switch (gst_media_source_track_get_track_type (track)) {
743 case GST_MEDIA_SOURCE_TRACK_TYPE_AUDIO:
744 case GST_MEDIA_SOURCE_TRACK_TYPE_TEXT:
745 case GST_MEDIA_SOURCE_TRACK_TYPE_VIDEO:
746 return TRUE;
747 default:
748 return FALSE;
749 }
750 }
751
752 static GstStream *
753 create_gst_stream (GstMediaSourceTrack * track)
754 {
755 gchar *stream_id = g_strdup_printf ("%s-%s",
756 GST_OBJECT_NAME (track), gst_media_source_track_get_id (track));
757 GstStream *stream = gst_stream_new (stream_id,
758 gst_media_source_track_get_initial_caps (track),
759 gst_media_source_track_get_stream_type (track), GST_STREAM_FLAG_SELECT);
760 g_free (stream_id);
761 return stream;
762 }
763
764 static void
765 set_flushing_and_signal (GstMseSrcPad * pad)
766 {
767 GST_TRACE_OBJECT (pad, "locking");
768 LINKED_OR_FLUSHING_LOCK (pad);
769 g_atomic_int_set (&pad->flushing, TRUE);
770 LINKED_OR_FLUSHING_SIGNAL (pad);
771 LINKED_OR_FLUSHING_UNLOCK (pad);
772 GST_TRACE_OBJECT (pad, "done");
773 }
774
775 static void
776 clear_flushing (GstMseSrcPad * pad)
777 {
778 GST_TRACE_OBJECT (pad, "locking");
779 LINKED_OR_FLUSHING_LOCK (pad);
780 g_atomic_int_set (&pad->flushing, FALSE);
781 LINKED_OR_FLUSHING_UNLOCK (pad);
782 GST_TRACE_OBJECT (pad, "done");
783 }
784
785 static void
786 flush_stream (GstMseSrc * self, Stream * stream, gboolean is_seek)
787 {
788 GstMseSrcPad *pad = GST_MSE_SRC_PAD (stream->pad);
789 gst_pad_push_event (GST_PAD (pad), gst_event_new_flush_start ());
790
791 if (is_seek) {
792 GST_DEBUG_OBJECT (pad, "flushing for seek to %" GST_TIMEP_FORMAT,
793 &self->start_time);
794 set_flushing_and_signal (pad);
795 gst_media_source_track_flush (stream->track);
796 gst_pad_stop_task (GST_PAD (pad));
797 GST_DEBUG_OBJECT (pad, "stopped task");
798 GstSegment *segment = &(pad->segment);
799 segment->base = 0;
800 segment->start = self->start_time;
801 segment->position = self->start_time;
802 segment->time = self->start_time;
803 segment->rate = self->rate;
804 } else {
805 gst_media_source_track_flush (stream->track);
806 }
807
808 g_atomic_int_set (&pad->does_need_segment, TRUE);
809
810 gst_pad_push_event (GST_PAD (pad), gst_event_new_flush_stop (is_seek));
811 }
812
813 static void
814 flush_all_streams (GstMseSrc * self, gboolean is_seek)
815 {
816 GHashTableIter iter;
817 g_hash_table_iter_init (&iter, self->streams);
818 for (gpointer value; g_hash_table_iter_next (&iter, NULL, &value);) {
819 flush_stream (self, (Stream *) value, is_seek);
820 }
821 }
822
823 static void
824 resume_all_streams (GstMseSrc * self)
825 {
826 GstState state;
827 gst_element_get_state (GST_ELEMENT (self), &state, NULL, 0);
828 gboolean active = state > GST_STATE_READY;
829
830 GHashTableIter iter;
831 g_hash_table_iter_init (&iter, self->streams);
832 for (gpointer value; g_hash_table_iter_next (&iter, NULL, &value);) {
833 Stream *stream = value;
834 GstPad *pad = GST_PAD (stream->pad);
835 if (active) {
836 clear_flushing (GST_MSE_SRC_PAD (pad));
837 gst_pad_start_task (pad, (GstTaskFunction) pad_task, pad, NULL);
838 }
839 }
840 }
841
842 static void
843 tear_down_stream (GstMseSrc * self, Stream * stream)
844 {
845 GST_DEBUG_OBJECT (self, "tearing down stream %s",
846 gst_media_source_track_get_id (stream->track));
847
848 flush_stream (self, stream, FALSE);
849 gst_pad_set_active (stream->pad, FALSE);
850
851 if (gst_stream_collection_get_size (self->collection) > 0) {
852 gst_element_remove_pad (GST_ELEMENT (self), stream->pad);
853 FLOW_COMBINER_LOCK (self);
854 gst_flow_combiner_remove_pad (self->flow_combiner, stream->pad);
855 FLOW_COMBINER_UNLOCK (self);
856 }
857 }
858
859
860 static void
861 tear_down_all_streams (GstMseSrc * self)
862 {
863 GHashTableIter iter;
864 g_hash_table_iter_init (&iter, self->streams);
865 for (gpointer value; g_hash_table_iter_next (&iter, NULL, &value);) {
866 tear_down_stream (self, (Stream *) value);
867 g_hash_table_iter_remove (&iter);
868 }
869 }
870
871 static GstStateChangeReturn
872 gst_mse_src_change_state (GstElement * element, GstStateChange transition)
873 {
874 GstMseSrc *self = GST_MSE_SRC (element);
875 switch (transition) {
876 case GST_STATE_CHANGE_PAUSED_TO_READY:
877 ready_state_update_task_stop (self->ready_state_update_task);
878 tear_down_all_streams (self);
879 break;
880 case GST_STATE_CHANGE_READY_TO_NULL:
881 ready_state_update_task_join (self->ready_state_update_task);
882 gst_mse_src_detach (self);
883 break;
884 case GST_STATE_CHANGE_READY_TO_PAUSED:
885 ready_state_update_task_start (self->ready_state_update_task);
886 break;
887 default:
888 break;
889 }
890 return GST_ELEMENT_CLASS (gst_mse_src_parent_class)->change_state (element,
891 transition);
892 }
893
894 static void
895 gst_mse_src_seek (GstMseSrc * self, GstClockTime start_time, gdouble rate)
896 {
897 GST_OBJECT_LOCK (self);
898 self->start_time = start_time;
899 self->rate = rate;
900 GST_OBJECT_UNLOCK (self);
901
902 MEDIA_SOURCE_LOCK (self);
903 flush_all_streams (self, TRUE);
904 if (self->media_source) {
905 GST_DEBUG_OBJECT (self, "seeking on media source %" GST_PTR_FORMAT,
906 self->media_source);
907 gst_media_source_seek (self->media_source, start_time);
908 } else {
909 GST_DEBUG_OBJECT (self, "detached, not seeking on media source");
910 }
911 MEDIA_SOURCE_UNLOCK (self);
912 resume_all_streams (self);
913 }
914
915 static gboolean
916 gst_mse_src_send_event (GstElement * element, GstEvent * event)
917 {
918 if (GST_EVENT_TYPE (event) != GST_EVENT_SEEK) {
919 return GST_ELEMENT_CLASS (gst_mse_src_parent_class)->send_event (element,
920 event);
921 }
922
923 GstMseSrc *self = GST_MSE_SRC (element);
924
925 gdouble rate;
926 GstFormat format;
927 GstSeekType seek_type;
928 gint64 start;
929 gst_event_parse_seek (event, &rate, &format, NULL, &seek_type, &start, NULL,
930 NULL);
931
932 gst_event_unref (event);
933
934 if (format != GST_FORMAT_TIME || seek_type != GST_SEEK_TYPE_SET || rate < 0) {
935 GST_ERROR_OBJECT (self,
936 "Rejecting unsupported seek event: %" GST_PTR_FORMAT, event);
937 return FALSE;
938 }
939
940 GST_DEBUG_OBJECT (self, "handling %" GST_PTR_FORMAT, event);
941 gst_mse_src_seek (self, start, rate);
942 return TRUE;
943 }
944
945 static inline gboolean
946 is_flushing (GstMseSrcPad * pad)
947 {
948 return g_atomic_int_get (&pad->flushing) || GST_PAD_IS_FLUSHING (pad);
949 }
950
951 static gboolean
952 await_pad_linked_or_flushing (GstMseSrcPad * pad)
953 {
954 GST_TRACE_OBJECT (pad, "waiting for link");
955 LINKED_OR_FLUSHING_LOCK (pad);
956 gboolean flushing = FALSE;
957 while (!gst_pad_is_linked (GST_PAD_CAST (pad)) && !is_flushing (pad)) {
958 LINKED_OR_FLUSHING_WAIT (pad);
959 }
960 flushing = is_flushing (pad);
961 LINKED_OR_FLUSHING_UNLOCK (pad);
962 GST_TRACE_OBJECT (pad, "linked");
963 return flushing;
964 }
965
966 static void
967 pad_task (GstMseSrcPad * pad)
968 {
969 GstMseSrc *self = NULL;
970 gboolean flushing = await_pad_linked_or_flushing (pad);
971 if (flushing) {
972 GST_TRACE_OBJECT (pad, "pad is flushing");
973 goto pause;
974 }
975
976 self = GST_MSE_SRC (gst_pad_get_parent_element (GST_PAD (pad)));
977
978 GstMediaSourceTrack *track = pad->track;
979
980 GstMiniObject *object = gst_media_source_track_pop (track);
981
982 if (object == NULL) {
983 GST_DEBUG_OBJECT (pad, "nothing was popped from track, must be flushing");
984 gst_media_source_track_flush (track);
985 goto pause;
986 }
987
988 if (!g_atomic_int_get (&pad->sent_stream_start)) {
989 const gchar *track_id = gst_media_source_track_get_id (track);
990 GstEvent *event = gst_event_new_stream_start (track_id);
991 gst_event_set_group_id (event, self->group_id);
992 gst_event_set_stream (event, pad->stream);
993 if (!gst_pad_push_event (GST_PAD (pad), event)) {
994 GST_ERROR_OBJECT (pad, "failed to push stream start");
995 goto pause;
996 }
997 GST_TRACE_OBJECT (pad, "stream start");
998 g_atomic_int_set (&pad->sent_stream_start, TRUE);
999 }
1000
1001 GstCaps *caps = gst_media_source_track_get_initial_caps (track);
1002 if (!g_atomic_int_get (&pad->sent_initial_caps) && GST_IS_CAPS (caps)) {
1003 GST_DEBUG_OBJECT (pad, "sending initial caps");
1004 gst_caps_replace (&pad->most_recent_caps, caps);
1005 GstEvent *event = gst_event_new_caps (caps);
1006 if (!gst_pad_push_event (GST_PAD (pad), event)) {
1007 GST_ERROR_OBJECT (pad, "failed to push caps update");
1008 goto pause;
1009 }
1010 GST_TRACE_OBJECT (pad, "initial caps %" GST_PTR_FORMAT, caps);
1011 g_atomic_int_set (&pad->sent_initial_caps, TRUE);
1012 }
1013
1014 if (g_atomic_int_get (&pad->does_need_segment)) {
1015 GST_DEBUG_OBJECT (pad, "sending new segment starting@%" GST_TIMEP_FORMAT,
1016 &pad->segment.time);
1017 GstEvent *event = gst_event_new_segment (&pad->segment);
1018 if (!gst_pad_push_event (GST_PAD (pad), event)) {
1019 GST_ERROR_OBJECT (pad, "failed to push new segment");
1020 goto pause;
1021 }
1022 GST_TRACE_OBJECT (pad, "segment");
1023 g_atomic_int_set (&pad->does_need_segment, FALSE);
1024 }
1025
1026 if (!g_atomic_int_get (&pad->sent_stream_collection)) {
1027 GstEvent *event = gst_event_new_stream_collection (self->collection);
1028 if (!gst_pad_push_event (GST_PAD (pad), event)) {
1029 GST_ERROR_OBJECT (pad, "failed to push stream collection");
1030 goto pause;
1031 }
1032 GST_TRACE_OBJECT (pad, "stream collection");
1033 g_atomic_int_set (&pad->sent_stream_collection, TRUE);
1034 }
1035
1036 if (GST_IS_SAMPLE (object)) {
1037 GstSample *sample = GST_SAMPLE (object);
1038 GstCaps *sample_caps = gst_sample_get_caps (sample);
1039
1040 if (!gst_caps_is_equal (pad->most_recent_caps, sample_caps)) {
1041 gst_caps_replace (&pad->most_recent_caps, sample_caps);
1042 GstEvent *event = gst_event_new_caps (gst_caps_ref (sample_caps));
1043 if (!gst_pad_push_event (GST_PAD (pad), event)) {
1044 GST_ERROR_OBJECT (pad, "failed to push new caps");
1045 goto pause;
1046 }
1047 GST_TRACE_OBJECT (pad, "new caps %" GST_PTR_FORMAT, sample_caps);
1048 }
1049
1050 GstBuffer *buffer = gst_buffer_copy (gst_sample_get_buffer (sample));
1051 if (GST_BUFFER_DTS_IS_VALID (buffer)) {
1052 GstClockTime duration =
1053 GST_BUFFER_DURATION_IS_VALID (buffer) ? GST_BUFFER_DURATION (buffer) :
1054 1;
1055 GstClockTime buffer_end = GST_BUFFER_DTS (buffer) + duration;
1056 pad->segment.position = MAX (pad->segment.position, buffer_end);
1057 }
1058
1059 GstFlowReturn push_result = gst_pad_push (GST_PAD (pad), buffer);
1060
1061 FLOW_COMBINER_LOCK (self);
1062 GstFlowReturn combined_result =
1063 gst_flow_combiner_update_pad_flow (self->flow_combiner,
1064 GST_PAD_CAST (pad), push_result);
1065 FLOW_COMBINER_UNLOCK (self);
1066
1067 switch (combined_result) {
1068 case GST_FLOW_OK:
1069 break;
1070 case GST_FLOW_FLUSHING:
1071 goto pause;
1072 default:
1073 GST_ELEMENT_ERROR (self, CORE, PAD, ("failed to push data downstream"),
1074 ("pad result: %s, combined result %s",
1075 gst_flow_get_name (push_result),
1076 gst_flow_get_name (combined_result)));
1077 goto pause;
1078 }
1079 } else if (GST_IS_EVENT (object)) {
1080 GstEvent *event = GST_EVENT (g_steal_pointer (&object));
1081 if (GST_EVENT_TYPE (event) == GST_EVENT_EOS) {
1082 GST_LOG_OBJECT (self, "EOS");
1083 }
1084 if (!gst_pad_push_event (GST_PAD (pad), event)) {
1085 GST_ERROR_OBJECT (self, "failed to push enqueued event");
1086 goto pause;
1087 }
1088 } else {
1089 GST_ERROR_OBJECT (self, "unexpected object on track queue"
1090 ", only samples and events are supported");
1091 g_assert_not_reached ();
1092 }
1093
1094 gst_clear_object (&self);
1095 return;
1096
1097 pause:
1098 if (!g_atomic_int_get (&pad->flushing)) {
1099 gst_pad_pause_task (GST_PAD (pad));
1100 }
1101 gst_clear_object (&self);
1102 }
1103
1104 static gboolean
1105 pad_activate_mode (GstMseSrcPad * pad, GstObject * parent, GstPadMode mode,
1106 gboolean active)
1107 {
1108 if (mode != GST_PAD_MODE_PUSH) {
1109 GST_ERROR_OBJECT (parent, "msesrc only supports push mode");
1110 return FALSE;
1111 }
1112
1113 if (active) {
1114 gst_pad_start_task (GST_PAD (pad), (GstTaskFunction) pad_task, pad, NULL);
1115 } else {
1116 set_flushing_and_signal (pad);
1117 gst_media_source_track_flush (pad->track);
1118 gst_pad_stop_task (GST_PAD (pad));
1119 clear_flushing (pad);
1120 }
1121
1122 return TRUE;
1123 }
1124
1125 static GstPadLinkReturn
1126 pad_linked (GstMseSrcPad * pad, GstMseSrc * parent, GstPad * sink)
1127 {
1128 GST_DEBUG_OBJECT (pad, "pad is linked to %" GST_PTR_FORMAT ", resuming task",
1129 sink);
1130 LINKED_OR_FLUSHING_LOCK (pad);
1131 LINKED_OR_FLUSHING_SIGNAL (pad);
1132 LINKED_OR_FLUSHING_UNLOCK (pad);
1133 return GST_PAD_LINK_OK;
1134 }
1135
1136 static gboolean
1137 pad_event (GstMseSrcPad * pad, GstMseSrc * parent, GstEvent * event)
1138 {
1139 switch (GST_EVENT_TYPE (event)) {
1140 case GST_EVENT_SEEK:
1141 return gst_element_send_event (GST_ELEMENT (parent), event);
1142 default:
1143 return gst_pad_event_default (GST_PAD (pad), GST_OBJECT (parent), event);
1144 }
1145 }
1146
1147 static gboolean
1148 pad_query (GstMseSrcPad * pad, GstObject * parent, GstQuery * query)
1149 {
1150 GstMseSrc *self = GST_MSE_SRC (parent);
1151 switch (GST_QUERY_TYPE (query)) {
1152 case GST_QUERY_POSITION:{
1153 GstClockTime position = pad->segment.position;
1154 GstFormat fmt;
1155 gst_query_parse_position (query, &fmt, NULL);
1156 if (fmt == GST_FORMAT_TIME && GST_CLOCK_TIME_IS_VALID (position)) {
1157 GST_TRACE_OBJECT (pad, "position query returning %" GST_TIMEP_FORMAT,
1158 &position);
1159 gst_query_set_position (query, GST_FORMAT_TIME, position);
1160 return TRUE;
1161 }
1162 break;
1163 }
1164 case GST_QUERY_DURATION:{
1165 GstFormat fmt;
1166 gst_query_parse_duration (query, &fmt, NULL);
1167 if (fmt == GST_FORMAT_TIME) {
1168 gst_query_set_duration (query, GST_FORMAT_TIME, self->duration);
1169 return TRUE;
1170 } else {
1171 return FALSE;
1172 }
1173 }
1174 case GST_QUERY_SEEKING:{
1175 GstFormat fmt;
1176 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
1177 if (fmt != GST_FORMAT_TIME) {
1178 return FALSE;
1179 }
1180 gst_query_set_seeking (query, GST_FORMAT_TIME, TRUE, 0, self->duration);
1181 return TRUE;
1182 }
1183 default:
1184 break;
1185 }
1186 return gst_pad_query_default (GST_PAD (pad), parent, query);
1187 }
1188
1189 static void
1190 append_stream (GstMseSrc * self, GstMediaSourceTrack * track)
1191 {
1192 if (g_hash_table_contains (self->streams, track)) {
1193 GST_DEBUG_OBJECT (self, "skipping processed %" GST_PTR_FORMAT, track);
1194 return;
1195 }
1196 GST_DEBUG_OBJECT (self, "creating stream for %" GST_PTR_FORMAT, track);
1197 guint pad_index = g_hash_table_size (self->streams);
1198 GstStream *info = create_gst_stream (track);
1199 Stream stream = {
1200 .info = gst_object_ref (info),
1201 .track = gst_object_ref (track),
1202 .pad = gst_mse_src_pad_new (track, info, pad_index,
1203 self->start_time, self->rate),
1204 };
1205 g_hash_table_insert (self->streams, track,
1206 g_memdup2 (&stream, sizeof (Stream)));
1207 gst_stream_collection_add_stream (self->collection, stream.info);
1208 }
1209
1210 void
1211 gst_mse_src_emit_streams (GstMseSrc * self, GstMediaSourceTrack ** tracks,
1212 gsize n_tracks)
1213 {
1214 g_return_if_fail (GST_IS_MSE_SRC (self));
1215
1216 GstElement *element = GST_ELEMENT (self);
1217 GstObject *object = GST_OBJECT (self);
1218
1219 update_ready_state_for_init_segment (self);
1220
1221 STREAMS_LOCK (self);
1222
1223 for (gsize i = 0; i < n_tracks; i++) {
1224 GstMediaSourceTrack *track = tracks[i];
1225 if (!is_streamable (track)) {
1226 continue;
1227 }
1228 append_stream (self, track);
1229 }
1230
1231 GstState state;
1232 gst_element_get_state (element, &state, NULL, 0);
1233 gboolean active = state > GST_STATE_READY;
1234
1235 GHashTableIter iter;
1236 g_hash_table_iter_init (&iter, self->streams);
1237 for (gpointer value; g_hash_table_iter_next (&iter, NULL, &value);) {
1238 Stream *stream = value;
1239 GstPad *pad = stream->pad;
1240 if (active) {
1241 gst_pad_set_active (pad, TRUE);
1242 }
1243 GstElement *parent = gst_pad_get_parent_element (pad);
1244 if (parent) {
1245 GST_DEBUG_OBJECT (self, "skipping parented pad %" GST_PTR_FORMAT, pad);
1246 gst_object_unref (parent);
1247 continue;
1248 }
1249 gst_element_add_pad (element, pad);
1250 FLOW_COMBINER_LOCK (self);
1251 gst_flow_combiner_add_pad (self->flow_combiner, pad);
1252 FLOW_COMBINER_UNLOCK (self);
1253 }
1254 STREAMS_UNLOCK (self);
1255
1256 gst_element_no_more_pads (element);
1257 gst_element_post_message (element, gst_message_new_stream_collection (object,
1258 self->collection));
1259 }
1260
1261 static GstURIType
1262 gst_mse_src_uri_get_type (GType type)
1263 {
1264 return GST_URI_SRC;
1265 }
1266
1267 static const gchar *const *
1268 gst_mse_src_uri_get_protocols (GType type)
1269 {
1270 static const gchar *protocols[] = { "mse", NULL };
1271 return protocols;
1272 }
1273
1274 static gchar *
1275 gst_mse_src_uri_get_uri (GstURIHandler * handler)
1276 {
1277 GstMseSrc *self = GST_MSE_SRC (handler);
1278 GST_OBJECT_LOCK (self);
1279 gchar *uri = g_strdup (self->uri);
1280 GST_OBJECT_UNLOCK (self);
1281 return uri;
1282 }
1283
1284 static gboolean
1285 gst_mse_src_uri_set_uri (GstURIHandler * handler, const gchar * uri,
1286 GError ** error)
1287 {
1288 GstMseSrc *self = GST_MSE_SRC (handler);
1289 GST_OBJECT_LOCK (self);
1290 g_free (self->uri);
1291 self->uri = g_strdup (uri);
1292 GST_OBJECT_UNLOCK (self);
1293 return TRUE;
1294 }
1295
1296 static void
1297 gst_mse_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
1298 {
1299 GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
1300 iface->get_type = gst_mse_src_uri_get_type;
1301 iface->get_protocols = gst_mse_src_uri_get_protocols;
1302 iface->get_uri = gst_mse_src_uri_get_uri;
1303 iface->set_uri = gst_mse_src_uri_set_uri;
1304 }
1305
1306 void
1307 gst_mse_src_attach (GstMseSrc * self, GstMediaSource * media_source)
1308 {
1309 g_return_if_fail (GST_IS_MSE_SRC (self));
1310 g_return_if_fail (GST_IS_MEDIA_SOURCE (media_source));
1311
1312 MEDIA_SOURCE_LOCK (self);
1313 g_set_object (&self->media_source, media_source);
1314 MEDIA_SOURCE_UNLOCK (self);
1315 }
1316
1317 void
1318 gst_mse_src_detach (GstMseSrc * self)
1319 {
1320 g_return_if_fail (GST_IS_MSE_SRC (self));
1321
1322 MEDIA_SOURCE_LOCK (self);
1323 gst_clear_object (&self->media_source);
1324 MEDIA_SOURCE_UNLOCK (self);
1325 }
1326
1327 static void
1328 set_ready_state (GstMseSrc * self, GstMseSrcReadyState ready_state)
1329 {
1330 if (ready_state == self->ready_state) {
1331 return;
1332 }
1333 GST_DEBUG_OBJECT (self, "ready state %s=>%s",
1334 mse_src_ready_state_name (self->ready_state),
1335 mse_src_ready_state_name (ready_state));
1336 self->ready_state = ready_state;
1337 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_READY_STATE]);
1338 }
1339
1340 static void
1341 update_ready_state_for_init_segment (GstMseSrc * self)
1342 {
1343 MEDIA_SOURCE_LOCK (self);
1344
1345 if (self->media_source == NULL) {
1346 goto done;
1347 }
1348 if (self->ready_state != GST_MSE_SRC_READY_STATE_HAVE_NOTHING) {
1349 goto done;
1350 }
1351 GstSourceBufferList *buffers = gst_media_source_get_source_buffers
1352 (self->media_source);
1353 gboolean all_received_init_segment = TRUE;
1354 for (guint i = 0; all_received_init_segment; i++) {
1355 GstSourceBuffer *buf = gst_source_buffer_list_index (buffers, i);
1356 if (buf == NULL) {
1357 break;
1358 }
1359 all_received_init_segment &= gst_source_buffer_has_init_segment (buf);
1360 gst_object_unref (buf);
1361 }
1362 if (!all_received_init_segment) {
1363 return;
1364 }
1365 set_ready_state (self, MAX (self->ready_state,
1366 GST_MSE_SRC_READY_STATE_HAVE_METADATA));
1367
1368 done:
1369 MEDIA_SOURCE_UNLOCK (self);
1370 }
1371
1372 static gboolean
1373 has_current_data (GstMseSrc * self, GstClockTime position,
1374 GstClockTime duration)
1375 {
1376 if (!GST_CLOCK_TIME_IS_VALID (position)) {
1377 return FALSE;
1378 }
1379 GstSourceBufferList *active =
1380 gst_media_source_get_active_source_buffers (self->media_source);
1381 gboolean has_data = TRUE;
1382 for (guint i = 0; has_data; i++) {
1383 GstSourceBuffer *buf = gst_source_buffer_list_index (active, i);
1384 if (buf == NULL) {
1385 if (i == 0) {
1386 has_data = FALSE;
1387 GST_DEBUG_OBJECT (self,
1388 "no active source buffers, nothing is buffered");
1389 }
1390 break;
1391 }
1392 has_data = gst_source_buffer_is_buffered (buf, position);
1393 gst_object_unref (buf);
1394 }
1395 g_object_unref (active);
1396 return has_data;
1397 }
1398
1399 static gboolean
1400 has_future_data (GstMseSrc * self, GstClockTime position, GstClockTime duration)
1401 {
1402 if (!GST_CLOCK_TIME_IS_VALID (position)
1403 || !GST_CLOCK_TIME_IS_VALID (duration)) {
1404 return FALSE;
1405 }
1406 GstClockTime target_position = MIN (position + THRESHOLD_FUTURE_DATA,
1407 duration);
1408 GstSourceBufferList *active =
1409 gst_media_source_get_active_source_buffers (self->media_source);
1410 gboolean has_data = TRUE;
1411 for (guint i = 0; has_data; i++) {
1412 GstSourceBuffer *buf = gst_source_buffer_list_index (active, i);
1413 if (buf == NULL) {
1414 if (i == 0) {
1415 has_data = FALSE;
1416 GST_DEBUG_OBJECT (self,
1417 "no active source buffers, nothing is buffered");
1418 }
1419 break;
1420 }
1421 has_data = gst_source_buffer_is_range_buffered (buf, position,
1422 target_position);
1423 gst_object_unref (buf);
1424 }
1425 g_object_unref (active);
1426 return has_data;
1427 }
1428
1429 static gboolean
1430 has_enough_data (GstMseSrc * self, GstClockTime position, GstClockTime duration)
1431 {
1432 if (!GST_CLOCK_TIME_IS_VALID (position)
1433 || !GST_CLOCK_TIME_IS_VALID (duration)) {
1434 return FALSE;
1435 }
1436 GstClockTime target_position = MIN (position + THRESHOLD_ENOUGH_DATA,
1437 duration);
1438 GstSourceBufferList *active =
1439 gst_media_source_get_active_source_buffers (self->media_source);
1440 gboolean has_data = TRUE;
1441 for (guint i = 0; has_data; i++) {
1442 GstSourceBuffer *buf = gst_source_buffer_list_index (active, i);
1443 if (buf == NULL) {
1444 if (i == 0) {
1445 has_data = FALSE;
1446 GST_DEBUG_OBJECT (self,
1447 "no active source buffers, nothing is buffered");
1448 }
1449 break;
1450 }
1451 has_data = gst_source_buffer_is_range_buffered (buf, position,
1452 target_position);
1453 gst_object_unref (buf);
1454 }
1455 g_object_unref (active);
1456 return has_data;
1457 }
1458
1459 static void
1460 update_ready_state (GstMseSrc * self)
1461 {
1462 MEDIA_SOURCE_LOCK (self);
1463
1464 if (self->media_source == NULL) {
1465 goto done;
1466 }
1467
1468 if (self->ready_state < GST_MSE_SRC_READY_STATE_HAVE_METADATA) {
1469 goto done;
1470 }
1471
1472 GstClockTime position = gst_mse_src_get_position (self);
1473 GstClockTime duration = self->duration;
1474
1475 if (has_enough_data (self, position, duration)) {
1476 set_ready_state (self, GST_MSE_SRC_READY_STATE_HAVE_ENOUGH_DATA);
1477 } else if (has_future_data (self, position, duration)) {
1478 set_ready_state (self, GST_MSE_SRC_READY_STATE_HAVE_FUTURE_DATA);
1479 } else if (has_current_data (self, position, duration)) {
1480 set_ready_state (self, GST_MSE_SRC_READY_STATE_HAVE_CURRENT_DATA);
1481 } else {
1482 set_ready_state (self, GST_MSE_SRC_READY_STATE_HAVE_METADATA);
1483 }
1484
1485 done:
1486 MEDIA_SOURCE_UNLOCK (self);
1487 }