Debian Codegraph @

gst-libs/gst/mse/gstmediasource.c

raw symbols SCIP
1 /* GStreamer
2 *
3 * SPDX-License-Identifier: LGPL-2.1
4 *
5 * Copyright (C) 2013 Google Inc. All rights reserved.
6 * Copyright (C) 2013 Orange
7 * Copyright (C) 2013-2017 Apple Inc. All rights reserved.
8 * Copyright (C) 2014 Sebastian Dröge <[email protected]>
9 * Copyright (C) 2015, 2016 Igalia, S.L
10 * Copyright (C) 2015, 2016 Metrological Group B.V.
11 * Copyright (C) 2022, 2023 Collabora Ltd.
12 *
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
17 *
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Library General Public License for more details.
22 *
23 * You should have received a copy of the GNU Library General Public
24 * License along with this library; if not, write to the
25 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
26 * Boston, MA 02110-1301, USA.
27 */
28
29 /**
30 * SECTION:gstmediasource
31 * @title: GstMediaSource
32 * @short_description: Media Source
33 * @symbols:
34 * - GstMediaSource
35 *
36 * #GstMediaSource is the entry point into the W3C Media Source API. It offers
37 * functionality similar to #GstAppSrc for client-side web or JavaScript
38 * applications decoupling the source of media from its processing and playback.
39 *
40 * To interact with a Media Source, connect it to a #GstMseSrc that is in some
41 * #GstPipeline using gst_media_source_attach(). Then create at least one
42 * #GstSourceBuffer using gst_media_source_add_source_buffer(). Finally, feed
43 * some media data to the Source Buffer(s) using
44 * gst_source_buffer_append_buffer() and play the pipeline.
45 *
46 * Since: 1.24
47 */
48
49 /**
50 * GstMediaSource:
51 * Since: 1.24
52 */
53
54 #ifdef HAVE_CONFIG_H
55 #include "config.h"
56 #endif
57
58 #include <gst/mse/mse-enumtypes.h>
59 #include "gstmediasource.h"
60 #include "gstmediasource-private.h"
61
62 #include "gstmselogging-private.h"
63 #include "gstmsemediatype-private.h"
64 #include "gstsourcebuffer-private.h"
65 #include "gstsourcebufferlist-private.h"
66
67 #include "gstmsesrc.h"
68 #include "gstmsesrc-private.h"
69
70 G_DEFINE_TYPE (GstMediaSource, gst_media_source, GST_TYPE_OBJECT);
71 G_DEFINE_QUARK (gst_media_source_error_quark, gst_media_source_error);
72
73 enum
74 {
75 PROP_0,
76
77 PROP_SOURCE_BUFFERS,
78 PROP_ACTIVE_SOURCE_BUFFERS,
79 PROP_READY_STATE,
80 PROP_POSITION,
81 PROP_DURATION,
82
83 N_PROPS,
84 };
85
86 typedef enum
87 {
88 ON_SOURCE_OPEN,
89 ON_SOURCE_ENDED,
90 ON_SOURCE_CLOSE,
91
92 N_SIGNALS,
93 } MediaSourceEvent;
94
95 typedef struct
96 {
97 GstDataQueueItem item;
98 MediaSourceEvent event;
99 } MediaSourceEventItem;
100
101 static GParamSpec *properties[N_PROPS];
102 static guint signals[N_SIGNALS];
103
104 #define DEFAULT_READY_STATE GST_MEDIA_SOURCE_READY_STATE_CLOSED
105 #define DEFAULT_POSITION GST_CLOCK_TIME_NONE
106 #define DEFAULT_DURATION GST_CLOCK_TIME_NONE
107
108 static void rebuild_active_source_buffers_unlocked (GstMediaSource * self);
109 static void detach_unlocked (GstMediaSource * self);
110 static void set_duration_unlocked (GstMediaSource * self,
111 GstClockTime duration);
112
113 /**
114 * gst_media_source_is_type_supported:
115 * @type: (transfer none): A MIME type value
116 *
117 * Determines whether the current Media Source configuration can process media
118 * of the supplied @type.
119 *
120 * Returns: `TRUE` when supported, `FALSE` otherwise
121 *
122 * Since: 1.24
123 */
124 gboolean
125 gst_media_source_is_type_supported (const gchar * type)
126 {
127 gst_mse_init_logging ();
128 g_return_val_if_fail (type != NULL, FALSE);
129
130 if (g_strcmp0 (type, "") == 0) {
131 return FALSE;
132 }
133
134 GstMediaSourceMediaType media_type = GST_MEDIA_SOURCE_MEDIA_TYPE_INIT;
135 if (!gst_media_source_media_type_parse (&media_type, type)) {
136 return FALSE;
137 }
138
139 gboolean supported = gst_media_source_media_type_is_supported (&media_type);
140
141 gst_media_source_media_type_reset (&media_type);
142
143 return supported;
144 }
145
146 /**
147 * gst_media_source_new:
148 *
149 * Creates a new #GstMediaSource instance. The instance is in the
150 * %GST_MEDIA_SOURCE_READY_STATE_CLOSED state and is not associated with any
151 * media player.
152 *
153 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-constructor)
154 *
155 * Returns: (transfer full): a new #GstMediaSource instance
156 * Since: 1.24
157 */
158 GstMediaSource *
159 gst_media_source_new (void)
160 {
161 gst_mse_init_logging ();
162 return g_object_ref_sink (g_object_new (GST_TYPE_MEDIA_SOURCE, NULL));
163 }
164
165 static inline void
166 empty_buffers (GstMediaSource * self)
167 {
168 for (guint i = 0;; i++) {
169 GstSourceBuffer *buf = gst_source_buffer_list_index (self->buffers, i);
170 if (buf == NULL) {
171 break;
172 }
173 gst_object_unparent (GST_OBJECT_CAST (buf));
174 gst_object_unref (buf);
175 }
176 gst_source_buffer_list_remove_all (self->buffers);
177 }
178
179 static void
180 gst_media_source_dispose (GObject * object)
181 {
182 GstMediaSource *self = (GstMediaSource *) object;
183
184 detach_unlocked (self);
185
186 g_clear_object (&self->active_buffers);
187
188 if (self->buffers) {
189 empty_buffers (self);
190 }
191 gst_clear_object (&self->buffers);
192
193 gst_clear_object (&self->event_queue);
194
195 G_OBJECT_CLASS (gst_media_source_parent_class)->dispose (object);
196 }
197
198 static void
199 gst_media_source_get_property (GObject * object, guint prop_id, GValue * value,
200 GParamSpec * pspec)
201 {
202 GstMediaSource *self = GST_MEDIA_SOURCE (object);
203
204 switch (prop_id) {
205 case PROP_SOURCE_BUFFERS:
206 g_value_take_object (value, gst_media_source_get_source_buffers (self));
207 break;
208 case PROP_ACTIVE_SOURCE_BUFFERS:
209 g_value_take_object (value,
210 gst_media_source_get_active_source_buffers (self));
211 break;
212 case PROP_READY_STATE:
213 g_value_set_enum (value, gst_media_source_get_ready_state (self));
214 break;
215 case PROP_POSITION:
216 g_value_set_uint64 (value, gst_media_source_get_position (self));
217 break;
218 case PROP_DURATION:
219 g_value_set_uint64 (value, gst_media_source_get_duration (self));
220 break;
221 default:
222 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 }
224 }
225
226 static void
227 gst_media_source_set_property (GObject * object, guint prop_id,
228 const GValue * value, GParamSpec * pspec)
229 {
230 GstMediaSource *self = GST_MEDIA_SOURCE (object);
231
232 switch (prop_id) {
233 case PROP_DURATION:{
234 GstClockTime duration = (GstClockTime) g_value_get_uint64 (value);
235 gst_media_source_set_duration (self, duration, NULL);
236 break;
237 }
238 default:
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
240 }
241 }
242
243 static void
244 gst_media_source_class_init (GstMediaSourceClass * klass)
245 {
246 GObjectClass *oclass = G_OBJECT_CLASS (klass);
247
248 oclass->dispose = GST_DEBUG_FUNCPTR (gst_media_source_dispose);
249 oclass->get_property = GST_DEBUG_FUNCPTR (gst_media_source_get_property);
250 oclass->set_property = GST_DEBUG_FUNCPTR (gst_media_source_set_property);
251
252 /**
253 * GstMediaSource:source-buffers:
254 *
255 * A #GstSourceBufferList of every #GstSourceBuffer in this Media Source
256 *
257 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-sourcebuffers)
258 *
259 * Since: 1.24
260 */
261 properties[PROP_SOURCE_BUFFERS] = g_param_spec_object ("source-buffers",
262 "Source Buffers",
263 "A SourceBufferList of all SourceBuffers in this Media Source",
264 GST_TYPE_SOURCE_BUFFER_LIST, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
265
266 /**
267 * GstMediaSource:active-source-buffers:
268 *
269 * A #GstSourceBufferList of every #GstSourceBuffer in this Media Source that
270 * is considered active
271 *
272 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-activesourcebuffers)
273 *
274 * Since: 1.24
275 */
276 properties[PROP_ACTIVE_SOURCE_BUFFERS] =
277 g_param_spec_object ("active-source-buffers", "Active Source Buffers",
278 "A SourceBufferList of all SourceBuffers that are active in this Media Source",
279 GST_TYPE_SOURCE_BUFFER_LIST, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
280
281 /**
282 * GstMediaSource:ready-state:
283 *
284 * The Ready State of the Media Source
285 *
286 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-readystate)
287 *
288 * Since: 1.24
289 */
290 properties[PROP_READY_STATE] = g_param_spec_enum ("ready-state",
291 "Ready State",
292 "The Ready State of the Media Source",
293 GST_TYPE_MEDIA_SOURCE_READY_STATE, DEFAULT_READY_STATE, G_PARAM_READABLE |
294 G_PARAM_STATIC_STRINGS);
295
296 /**
297 * GstMediaSource:position:
298 *
299 * The position of the player consuming from the Media Source
300 *
301 * Since: 1.24
302 */
303 properties[PROP_POSITION] = g_param_spec_uint64 ("position",
304 "Position",
305 "The Position of the Media Source as a GstClockTime",
306 GST_CLOCK_TIME_NONE, G_MAXUINT64, DEFAULT_DURATION, G_PARAM_READWRITE |
307 G_PARAM_STATIC_STRINGS);
308
309 /**
310 * GstMediaSource:duration:
311 *
312 * The Duration of the Media Source as a #GstClockTime
313 *
314 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-duration)
315 *
316 * Since: 1.24
317 */
318 properties[PROP_DURATION] = g_param_spec_uint64 ("duration",
319 "Duration",
320 "The Duration of the Media Source as a GstClockTime",
321 GST_CLOCK_TIME_NONE, G_MAXUINT64, DEFAULT_DURATION, G_PARAM_READWRITE |
322 G_PARAM_STATIC_STRINGS);
323
324 g_object_class_install_properties (oclass, N_PROPS, properties);
325
326 /**
327 * GstMediaSource::on-source-open:
328 * @self: The #GstMediaSource that has just opened
329 *
330 * Emitted when @self has been opened.
331 *
332 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-onsourceopen)
333 *
334 * Since: 1.24
335 */
336 signals[ON_SOURCE_OPEN] = g_signal_new ("on-source-open",
337 G_TYPE_FROM_CLASS (klass),
338 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
339
340 /**
341 * GstMediaSource::on-source-ended:
342 * @self: The #GstMediaSource that has just ended
343 *
344 * Emitted when @self has ended, normally through
345 * gst_media_source_end_of_stream().
346 *
347 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-onsourceended)
348 *
349 * Since: 1.24
350 */
351 signals[ON_SOURCE_ENDED] = g_signal_new ("on-source-ended",
352 G_TYPE_FROM_CLASS (klass),
353 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
354
355 /**
356 * GstMediaSource::on-source-closed:
357 * @self: The #GstMediaSource that has just closed
358 *
359 * Emitted when @self has closed, normally when detached from a #GstMseSrc.
360 *
361 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-onsourceclose)
362 *
363 * Since: 1.24
364 */
365 signals[ON_SOURCE_CLOSE] = g_signal_new ("on-source-close",
366 G_TYPE_FROM_CLASS (klass),
367 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
368
369 }
370
371 static inline void
372 reset_live_seekable_range (GstMediaSource * self)
373 {
374 self->live_seekable_range.start = 0;
375 self->live_seekable_range.end = 0;
376 }
377
378 static inline gboolean
379 is_updating (GstMediaSource * self)
380 {
381 for (guint i = 0;; i++) {
382 GstSourceBuffer *buf = gst_source_buffer_list_index (self->buffers, i);
383 if (buf == NULL)
384 break;
385 gboolean updating = gst_source_buffer_get_updating (buf);
386 gst_object_unref (buf);
387 if (updating) {
388 return TRUE;
389 }
390 }
391 return FALSE;
392 }
393
394 static inline gboolean
395 is_attached (GstMediaSource * self)
396 {
397 return GST_IS_MSE_SRC (self->element);
398 }
399
400 static inline void
401 network_error (GstMediaSource * self)
402 {
403 if (is_attached (self)) {
404 gst_mse_src_network_error (self->element);
405 }
406 }
407
408 static inline void
409 decode_error (GstMediaSource * self)
410 {
411 if (is_attached (self)) {
412 gst_mse_src_decode_error (self->element);
413 }
414 }
415
416 static inline void
417 update_duration (GstMediaSource * self)
418 {
419 if (is_attached (self)) {
420 gst_mse_src_set_duration (self->element, self->duration);
421 }
422 }
423
424 static void
425 schedule_event (GstMediaSource * self, MediaSourceEvent event)
426 {
427 MediaSourceEventItem item = {
428 .item = {.destroy = g_free,.visible = TRUE,.size = 1,.object = NULL},
429 .event = event,
430 };
431
432 gst_mse_event_queue_push (self->event_queue, g_memdup2 (&item,
433 sizeof (MediaSourceEventItem)));
434 }
435
436 static void
437 dispatch_event (MediaSourceEventItem * item, GstMediaSource * self)
438 {
439 g_signal_emit (self, signals[item->event], 0);
440 }
441
442 static void
443 gst_media_source_init (GstMediaSource * self)
444 {
445 self->buffers = gst_source_buffer_list_new ();
446 self->active_buffers = gst_source_buffer_list_new ();
447 self->ready_state = DEFAULT_READY_STATE;
448 self->duration = DEFAULT_DURATION;
449 reset_live_seekable_range (self);
450 self->element = NULL;
451 self->event_queue =
452 gst_mse_event_queue_new ((GstMseEventQueueCallback) dispatch_event, self);
453 }
454
455 /**
456 * gst_media_source_attach:
457 * @self: #GstMediaSource instance
458 * @element: (transfer none): #GstMseSrc source Element
459 *
460 * Associates @self with @element.
461 * Normally, the Element will be part of a #GstPipeline that plays back the data
462 * submitted to the Media Source's Source Buffers.
463 *
464 * #GstMseSrc is a special source element that is designed to consume media from
465 * a #GstMediaSource.
466 *
467 * [Specification](https://www.w3.org/TR/media-source-2/#dfn-attaching-to-a-media-element)
468 *
469 * Since: 1.24
470 */
471 void
472 gst_media_source_attach (GstMediaSource * self, GstMseSrc * element)
473 {
474 g_return_if_fail (GST_IS_MEDIA_SOURCE (self));
475 g_return_if_fail (GST_IS_MSE_SRC (element));
476
477 GST_OBJECT_LOCK (self);
478 if (is_attached (self)) {
479 detach_unlocked (self);
480 }
481
482 self->element = gst_object_ref (element);
483 gst_mse_src_attach (element, self);
484
485 self->ready_state = GST_MEDIA_SOURCE_READY_STATE_OPEN;
486 GST_OBJECT_UNLOCK (self);
487
488 schedule_event (self, ON_SOURCE_OPEN);
489 }
490
491 static void
492 detach_unlocked (GstMediaSource * self)
493 {
494 self->ready_state = GST_MEDIA_SOURCE_READY_STATE_CLOSED;
495 set_duration_unlocked (self, GST_CLOCK_TIME_NONE);
496
497 gst_source_buffer_list_remove_all (self->active_buffers);
498 empty_buffers (self);
499
500 if (is_attached (self)) {
501 gst_mse_src_detach (self->element);
502 gst_clear_object (&self->element);
503 }
504
505 schedule_event (self, ON_SOURCE_CLOSE);
506 }
507
508 /**
509 * gst_media_source_detach:
510 * @self: #GstMediaSource instance
511 *
512 * Detaches @self from any #GstMseSrc element that it may be associated with.
513 *
514 * Since: 1.24
515 */
516 void
517 gst_media_source_detach (GstMediaSource * self)
518 {
519 g_return_if_fail (GST_IS_MEDIA_SOURCE (self));
520
521 GST_OBJECT_LOCK (self);
522 detach_unlocked (self);
523 GST_OBJECT_UNLOCK (self);
524 }
525
526 /**
527 * gst_media_source_get_source_element:
528 * @self: #GstMediaSource instance
529 *
530 * Gets the #GstMseSrc currently attached to @self or `NULL` if there is none.
531 *
532 * Returns: (transfer full) (nullable): a #GstMseSrc instance or `NULL`
533 */
534 GstMseSrc *
535 gst_media_source_get_source_element (GstMediaSource * self)
536 {
537 g_return_val_if_fail (GST_IS_MEDIA_SOURCE (self), NULL);
538 GST_OBJECT_LOCK (self);
539 GstMseSrc *element = self->element == NULL ? NULL
540 : gst_object_ref (self->element);
541 GST_OBJECT_UNLOCK (self);
542 return element;
543 }
544
545 void
546 gst_media_source_open (GstMediaSource * self)
547 {
548 g_return_if_fail (GST_IS_MEDIA_SOURCE (self));
549 if (self->ready_state != GST_MEDIA_SOURCE_READY_STATE_OPEN) {
550 self->ready_state = GST_MEDIA_SOURCE_READY_STATE_OPEN;
551 schedule_event (self, ON_SOURCE_OPEN);
552 }
553 }
554
555 /**
556 * gst_media_source_get_source_buffers:
557 * @self: #GstMediaSource instance
558 *
559 * Gets a #GstSourceBufferList containing all the Source Buffers currently
560 * associated with this Media Source. This object will reflect any future
561 * changes to the parent Media Source as well.
562 *
563 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-sourcebuffers)
564 *
565 * Returns: (transfer full): a #GstSourceBufferList instance
566 * Since: 1.24
567 */
568 GstSourceBufferList *
569 gst_media_source_get_source_buffers (GstMediaSource * self)
570 {
571 g_return_val_if_fail (GST_IS_MEDIA_SOURCE (self), NULL);
572 return g_object_ref (self->buffers);
573 }
574
575 /**
576 * gst_media_source_get_active_source_buffers:
577 * @self: #GstMediaSource instance
578 *
579 * Gets a #GstSourceBufferList containing all the Source Buffers currently
580 * associated with this Media Source that are considered "active."
581 * For a Source Buffer to be considered active, either its video track is
582 * selected, its audio track is enabled, or its text track is visible or hidden.
583 * This object will reflect any future changes to the parent Media Source as
584 * well.
585 *
586 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-activesourcebuffers)
587 *
588 * Returns: (transfer full): a new #GstSourceBufferList instance
589 * Since: 1.24
590 */
591 GstSourceBufferList *
592 gst_media_source_get_active_source_buffers (GstMediaSource * self)
593 {
594 g_return_val_if_fail (GST_IS_MEDIA_SOURCE (self), NULL);
595 return g_object_ref (self->active_buffers);
596 }
597
598 /**
599 * gst_media_source_get_ready_state:
600 * @self: #GstMediaSource instance
601 *
602 * Gets the current Ready State of the Media Source.
603 *
604 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-readystate)
605 *
606 * Returns: the current #GstMediaSourceReadyState value
607 * Since: 1.24
608 */
609 GstMediaSourceReadyState
610 gst_media_source_get_ready_state (GstMediaSource * self)
611 {
612 g_return_val_if_fail (GST_IS_MEDIA_SOURCE (self), DEFAULT_READY_STATE);
613
614 GST_OBJECT_LOCK (self);
615 GstMediaSourceReadyState ready_state = self->ready_state;
616 GST_OBJECT_UNLOCK (self);
617
618 return ready_state;
619 }
620
621 /**
622 * gst_media_source_get_position:
623 * @self: #GstMediaSource instance
624 *
625 * Gets the current playback position of the Media Source.
626 *
627 * Returns: the current playback position as a #GstClockTime
628 * Since: 1.24
629 */
630 GstClockTime
631 gst_media_source_get_position (GstMediaSource * self)
632 {
633 g_return_val_if_fail (GST_IS_MEDIA_SOURCE (self), DEFAULT_POSITION);
634
635 GST_OBJECT_LOCK (self);
636 GstClockTime position =
637 is_attached (self) ? gst_mse_src_get_position (self->element) :
638 DEFAULT_POSITION;
639 GST_OBJECT_UNLOCK (self);
640
641 return position;
642 }
643
644 /**
645 * gst_media_source_get_duration:
646 * @self: #GstMediaSource instance
647 *
648 * Gets the current duration of @self.
649 *
650 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-duration)
651 *
652 * Returns: the current duration as a #GstClockTime
653 * Since: 1.24
654 */
655 GstClockTime
656 gst_media_source_get_duration (GstMediaSource * self)
657 {
658 g_return_val_if_fail (GST_IS_MEDIA_SOURCE (self), DEFAULT_DURATION);
659
660 GST_OBJECT_LOCK (self);
661 GstClockTime duration =
662 self->ready_state ==
663 GST_MEDIA_SOURCE_READY_STATE_CLOSED ? GST_CLOCK_TIME_NONE :
664 self->duration;
665 GST_OBJECT_UNLOCK (self);
666
667 return duration;
668 }
669
670 static void
671 set_duration_unlocked (GstMediaSource * self, GstClockTime duration)
672 {
673 self->duration = duration;
674 update_duration (self);
675 }
676
677 /**
678 * gst_media_source_set_duration:
679 * @self: #GstMediaSource instance
680 * @duration: The new duration to apply to @self.
681 * @error: (out) (optional) (nullable) (transfer full): the resulting error or `NULL`
682 *
683 * Sets the duration of @self.
684 *
685 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-duration)
686 *
687 * Returns: `TRUE` on success, `FALSE` otherwise
688 * Since: 1.24
689 */
690 gboolean
691 gst_media_source_set_duration (GstMediaSource * self, GstClockTime duration,
692 GError ** error)
693 {
694 g_return_val_if_fail (GST_IS_MEDIA_SOURCE (self), FALSE);
695
696 GST_OBJECT_LOCK (self);
697 self->duration = duration;
698 update_duration (self);
699 GST_OBJECT_UNLOCK (self);
700
701 g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DURATION]);
702 return TRUE;
703 }
704
705 static void
706 on_received_init_segment (G_GNUC_UNUSED GstSourceBuffer * source_buffer,
707 gpointer user_data)
708 {
709 GstMediaSource *self = GST_MEDIA_SOURCE (user_data);
710
711 GST_OBJECT_LOCK (self);
712
713 if (!is_attached (self)) {
714 GST_DEBUG_OBJECT (self, "received init segment while detached, ignoring");
715 GST_OBJECT_UNLOCK (self);
716 return;
717 }
718
719 GPtrArray *all_tracks = g_ptr_array_new ();
720
721 for (guint i = 0;; i++) {
722 GstSourceBuffer *buf = gst_source_buffer_list_index (self->buffers, i);
723 if (buf == NULL) {
724 break;
725 }
726 GPtrArray *tracks = gst_source_buffer_get_all_tracks (buf);
727 g_ptr_array_extend (all_tracks, tracks, NULL, NULL);
728 g_ptr_array_unref (tracks);
729 gst_object_unref (buf);
730 }
731
732 GST_OBJECT_UNLOCK (self);
733
734 gst_mse_src_emit_streams (self->element,
735 (GstMediaSourceTrack **) all_tracks->pdata, all_tracks->len);
736
737 g_ptr_array_unref (all_tracks);
738 }
739
740 static void
741 on_duration_changed (G_GNUC_UNUSED GstSourceBuffer * source_buffer,
742 gpointer user_data)
743 {
744 GstMediaSource *self = GST_MEDIA_SOURCE (user_data);
745 GstClockTime current = self->duration;
746 GstClockTime max = 0;
747 for (guint i = 0;; i++) {
748 GstSourceBuffer *buf = gst_source_buffer_list_index (self->buffers, i);
749 if (buf == NULL) {
750 break;
751 }
752 GstClockTime duration = gst_source_buffer_get_duration (buf);
753 if (GST_CLOCK_TIME_IS_VALID (duration)) {
754 max = MAX (max, duration);
755 }
756 gst_object_unref (buf);
757 }
758 if (current == max) {
759 return;
760 }
761 GST_DEBUG_OBJECT (self, "updating %" GST_TIMEP_FORMAT "=>%" GST_TIMEP_FORMAT,
762 &current, &max);
763 gst_media_source_set_duration (self, max, NULL);
764 }
765
766 static GHashTable *
767 source_buffer_list_as_set (GstSourceBufferList * list)
768 {
769 GHashTable *buffers = g_hash_table_new_full (g_direct_hash, g_direct_equal,
770 gst_object_unref, NULL);
771 for (guint i = 0;; i++) {
772 GstSourceBuffer *buf = gst_source_buffer_list_index (list, i);
773 if (buf == NULL) {
774 break;
775 }
776 g_hash_table_add (buffers, buf);
777 }
778 return buffers;
779 }
780
781 static void
782 rebuild_active_source_buffers_unlocked (GstMediaSource * self)
783 {
784 GST_DEBUG_OBJECT (self, "rebuilding active source buffers");
785 GHashTable *previously_active =
786 source_buffer_list_as_set (self->active_buffers);
787
788 gst_source_buffer_list_notify_freeze (self->active_buffers);
789 gst_source_buffer_list_remove_all (self->active_buffers);
790
791 gboolean added = FALSE;
792 gboolean removed = FALSE;
793
794 for (guint i = 0;; i++) {
795 GstSourceBuffer *buf = gst_source_buffer_list_index (self->buffers, i);
796 if (buf == NULL) {
797 break;
798 }
799 if (gst_source_buffer_get_active (buf)) {
800 gst_source_buffer_list_append (self->active_buffers, buf);
801 added |= !g_hash_table_contains (previously_active, buf);
802 } else {
803 gst_source_buffer_list_append (self->active_buffers, buf);
804 removed |= g_hash_table_contains (previously_active, buf);
805 }
806 gst_object_unref (buf);
807 }
808 g_hash_table_unref (previously_active);
809
810 gst_source_buffer_list_notify_cancel (self->active_buffers);
811 gst_source_buffer_list_notify_thaw (self->active_buffers);
812
813 if (added) {
814 GST_DEBUG_OBJECT (self, "notifying active source buffer added");
815 gst_source_buffer_list_notify_added (self->active_buffers);
816 }
817 if (removed) {
818 GST_DEBUG_OBJECT (self, "notifying active source buffer removed");
819 gst_source_buffer_list_notify_removed (self->active_buffers);
820 }
821 }
822
823 static void
824 on_active_state_changed (GstSourceBuffer * source_buffer, gpointer user_data)
825 {
826 GstMediaSource *self = GST_MEDIA_SOURCE (user_data);
827
828 GST_OBJECT_LOCK (self);
829 rebuild_active_source_buffers_unlocked (self);
830 GST_OBJECT_UNLOCK (self);
831 }
832
833 /**
834 * gst_media_source_add_source_buffer:
835 * @self: #GstMediaSource instance
836 * @type: (transfer none): A MIME type describing the format of the incoming media
837 * @error: (out) (optional) (nullable) (transfer full): the resulting error or `NULL`
838 *
839 * Add a #GstSourceBuffer to this #GstMediaSource of the specified media type.
840 * The Media Source must be in the #GstMediaSourceReadyState %GST_MEDIA_SOURCE_READY_STATE_OPEN.
841 *
842 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-addsourcebuffer)
843 *
844 * Returns: (transfer full): a new #GstSourceBuffer instance on success, otherwise `NULL`
845 * Since: 1.24
846 */
847 GstSourceBuffer *
848 gst_media_source_add_source_buffer (GstMediaSource * self, const gchar * type,
849 GError ** error)
850 {
851 g_return_val_if_fail (GST_IS_MEDIA_SOURCE (self), NULL);
852 g_return_val_if_fail (type != NULL, NULL);
853
854 if (g_strcmp0 (type, "") == 0) {
855 g_set_error (error,
856 GST_MEDIA_SOURCE_ERROR, GST_MEDIA_SOURCE_ERROR_TYPE,
857 "supplied content type is empty");
858 return NULL;
859 }
860
861 if (!gst_media_source_is_type_supported (type)) {
862 g_set_error (error,
863 GST_MEDIA_SOURCE_ERROR, GST_MEDIA_SOURCE_ERROR_NOT_SUPPORTED,
864 "unsupported content type");
865 return NULL;
866 }
867
868 GST_OBJECT_LOCK (self);
869
870 if (self->ready_state != GST_MEDIA_SOURCE_READY_STATE_OPEN) {
871 g_set_error (error,
872 GST_MEDIA_SOURCE_ERROR, GST_MEDIA_SOURCE_ERROR_INVALID_STATE,
873 "media source is not open");
874 goto error;
875 }
876
877 GstSourceBufferCallbacks callbacks = {
878 .duration_changed = on_duration_changed,
879 .received_init_segment = on_received_init_segment,
880 .active_state_changed = on_active_state_changed,
881 };
882
883 GError *source_buffer_error = NULL;
884 GstSourceBuffer *buf = gst_source_buffer_new_with_callbacks (type,
885 GST_OBJECT (self), &callbacks, self, &source_buffer_error);
886 if (source_buffer_error) {
887 g_propagate_prefixed_error (error, source_buffer_error,
888 "failed to create source buffer");
889 gst_clear_object (&buf);
890 goto error;
891 }
892
893 gst_source_buffer_list_append (self->buffers, buf);
894
895 GST_OBJECT_UNLOCK (self);
896
897 return buf;
898
899 error:
900 GST_OBJECT_UNLOCK (self);
901 return NULL;
902 }
903
904 /**
905 * gst_media_source_remove_source_buffer:
906 * @self: #GstMediaSource instance
907 * @buffer: (transfer none): #GstSourceBuffer instance
908 * @error: (out) (optional) (nullable) (transfer full): the resulting error or `NULL`
909 *
910 * Remove @buffer from @self.
911 *
912 * @buffer must have been created as a child of @self and @self must be in the
913 * #GstMediaSourceReadyState %GST_MEDIA_SOURCE_READY_STATE_OPEN.
914 *
915 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-removesourcebuffer)
916 *
917 * Returns: `TRUE` on success, `FALSE` otherwise
918 * Since: 1.24
919 */
920 gboolean
921 gst_media_source_remove_source_buffer (GstMediaSource * self,
922 GstSourceBuffer * buffer, GError ** error)
923 {
924 g_return_val_if_fail (GST_IS_MEDIA_SOURCE (self), FALSE);
925 g_return_val_if_fail (GST_IS_SOURCE_BUFFER (buffer), FALSE);
926
927 GST_OBJECT_LOCK (self);
928
929 if (!gst_source_buffer_list_contains (self->buffers, buffer)) {
930 g_set_error (error,
931 GST_MEDIA_SOURCE_ERROR, GST_MEDIA_SOURCE_ERROR_NOT_FOUND,
932 "the supplied source buffer was not found in this media source");
933 goto error;
934 }
935
936 if (gst_source_buffer_get_updating (buffer))
937 gst_source_buffer_teardown (buffer);
938
939 gst_source_buffer_list_remove (self->active_buffers, buffer);
940
941 gst_object_unparent (GST_OBJECT (buffer));
942 gst_source_buffer_list_remove (self->buffers, buffer);
943
944 GST_OBJECT_UNLOCK (self);
945
946 return TRUE;
947
948 error:
949 GST_OBJECT_UNLOCK (self);
950 return FALSE;
951 }
952
953 static void
954 abort_all_source_buffers (GstMediaSource * self)
955 {
956 for (guint i = 0;; i++) {
957 GstSourceBuffer *buf = gst_source_buffer_list_index (self->buffers, i);
958 if (buf == NULL) {
959 return;
960 }
961 GST_LOG_OBJECT (self, "shutting down %" GST_PTR_FORMAT, buf);
962 gst_source_buffer_abort (buf, NULL);
963 gst_object_unref (buf);
964 }
965 }
966
967 /**
968 * gst_media_source_end_of_stream:
969 * @self: #GstMediaSource instance
970 * @eos_error: The error type, if any
971 * @error: (out) (optional) (nullable) (transfer full): the resulting error or `NULL`
972 *
973 * Mark @self as reaching the end of stream, disallowing new data inputs.
974 *
975 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-endofstream)
976 *
977 * Returns: `TRUE` on success, `FALSE` otherwise
978 * Since: 1.24
979 */
980 gboolean
981 gst_media_source_end_of_stream (GstMediaSource * self,
982 GstMediaSourceEOSError eos_error, GError ** error)
983 {
984 g_return_val_if_fail (GST_IS_MEDIA_SOURCE (self), FALSE);
985
986 GST_OBJECT_LOCK (self);
987
988 if (self->ready_state != GST_MEDIA_SOURCE_READY_STATE_OPEN) {
989 g_set_error (error,
990 GST_MEDIA_SOURCE_ERROR, GST_MEDIA_SOURCE_ERROR_INVALID_STATE,
991 "media source is not open");
992 goto error;
993 }
994
995 if (is_updating (self)) {
996 g_set_error (error,
997 GST_MEDIA_SOURCE_ERROR, GST_MEDIA_SOURCE_ERROR_INVALID_STATE,
998 "some buffers are still updating");
999 goto error;
1000 }
1001
1002 self->ready_state = GST_MEDIA_SOURCE_READY_STATE_ENDED;
1003 schedule_event (self, ON_SOURCE_ENDED);
1004
1005 GST_OBJECT_UNLOCK (self);
1006
1007 switch (eos_error) {
1008 case GST_MEDIA_SOURCE_EOS_ERROR_NETWORK:
1009 network_error (self);
1010 break;
1011 case GST_MEDIA_SOURCE_EOS_ERROR_DECODE:
1012 decode_error (self);
1013 break;
1014 default:
1015 update_duration (self);
1016 abort_all_source_buffers (self);
1017 break;
1018 }
1019
1020 return TRUE;
1021
1022 error:
1023 GST_OBJECT_UNLOCK (self);
1024 return FALSE;
1025 }
1026
1027 /**
1028 * gst_media_source_set_live_seekable_range:
1029 * @self: #GstMediaSource instance
1030 * @start: The earliest point in the stream considered seekable
1031 * @end: The latest point in the stream considered seekable
1032 * @error: (out) (optional) (nullable) (transfer full): the resulting error or `NULL`
1033 *
1034 * Set the live seekable range for @self. This range informs the component
1035 * playing this Media Source what it can allow the user to seek through.
1036 *
1037 * If the ready state is not %GST_MEDIA_SOURCE_READY_STATE_OPEN, or the supplied
1038 * @start time is later than @end it will fail and set an error.
1039 *
1040 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-setliveseekablerange)
1041 *
1042 * Returns: `TRUE` on success, `FALSE` otherwise
1043 * Since: 1.24
1044 */
1045 gboolean
1046 gst_media_source_set_live_seekable_range (GstMediaSource * self,
1047 GstClockTime start, GstClockTime end, GError ** error)
1048 {
1049 g_return_val_if_fail (GST_IS_MEDIA_SOURCE (self), FALSE);
1050
1051 GST_OBJECT_LOCK (self);
1052
1053 if (self->ready_state != GST_MEDIA_SOURCE_READY_STATE_OPEN) {
1054 g_set_error (error,
1055 GST_MEDIA_SOURCE_ERROR, GST_MEDIA_SOURCE_ERROR_INVALID_STATE,
1056 "media source is not open");
1057 goto error;
1058 }
1059
1060 if (start > end) {
1061 g_set_error (error,
1062 GST_MEDIA_SOURCE_ERROR, GST_MEDIA_SOURCE_ERROR_TYPE,
1063 "bad time range: start must be earlier than end");
1064 goto error;
1065 }
1066
1067 self->live_seekable_range.start = start;
1068 self->live_seekable_range.end = end;
1069
1070 GST_OBJECT_UNLOCK (self);
1071
1072 return TRUE;
1073
1074 error:
1075 GST_OBJECT_UNLOCK (self);
1076 return FALSE;
1077 }
1078
1079 /**
1080 * gst_media_source_clear_live_seekable_range:
1081 * @self: #GstMediaSource instance
1082 * @error: (out) (optional) (nullable) (transfer full): the resulting error or `NULL`
1083 *
1084 * Clear the live seekable range for @self. This will inform the component
1085 * playing this Media Source that there is no seekable time range.
1086 *
1087 * If the ready state is not %GST_MEDIA_SOURCE_READY_STATE_OPEN, it will fail
1088 * and set an error.
1089 *
1090 * [Specification](https://www.w3.org/TR/media-source-2/#dom-mediasource-clearliveseekablerange)
1091 *
1092 * Returns: `TRUE` on success, `FALSE` otherwise
1093 * Since: 1.24
1094 */
1095 gboolean
1096 gst_media_source_clear_live_seekable_range (GstMediaSource * self,
1097 GError ** error)
1098 {
1099 g_return_val_if_fail (GST_IS_MEDIA_SOURCE (self), FALSE);
1100
1101 GST_OBJECT_LOCK (self);
1102
1103 if (self->ready_state != GST_MEDIA_SOURCE_READY_STATE_OPEN) {
1104 g_set_error (error,
1105 GST_MEDIA_SOURCE_ERROR, GST_MEDIA_SOURCE_ERROR_INVALID_STATE,
1106 "media source is not open");
1107 goto error;
1108 }
1109
1110 reset_live_seekable_range (self);
1111
1112 GST_OBJECT_UNLOCK (self);
1113
1114 return TRUE;
1115
1116 error:
1117 GST_OBJECT_UNLOCK (self);
1118 return FALSE;
1119 }
1120
1121 /**
1122 * gst_media_source_get_live_seekable_range:
1123 * @self: #GstMediaSource instance
1124 * @range: (out caller-allocates) (transfer none): time range
1125 *
1126 * Get the live seekable range of @self. Will fill in the supplied @range with
1127 * the current live seekable range.
1128 *
1129 * Since: 1.24
1130 */
1131 void
1132 gst_media_source_get_live_seekable_range (GstMediaSource * self,
1133 GstMediaSourceRange * range)
1134 {
1135 g_return_if_fail (GST_IS_MEDIA_SOURCE (self));
1136 g_return_if_fail (range != NULL);
1137
1138 GST_OBJECT_LOCK (self);
1139 range->start = self->live_seekable_range.start;
1140 range->end = self->live_seekable_range.end;
1141 GST_OBJECT_UNLOCK (self);
1142 }
1143
1144 void
1145 gst_media_source_seek (GstMediaSource * self, GstClockTime time)
1146 {
1147 g_return_if_fail (GST_IS_MEDIA_SOURCE (self));
1148
1149 GST_OBJECT_LOCK (self);
1150 for (guint i = 0;; i++) {
1151 GstSourceBuffer *buf = gst_source_buffer_list_index (self->buffers, i);
1152 if (buf == NULL) {
1153 goto done;
1154 }
1155 gst_source_buffer_seek (buf, time);
1156 gst_object_unref (buf);
1157 }
1158 done:
1159 GST_OBJECT_UNLOCK (self);
1160 }