| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
> |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 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 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 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 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 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 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 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 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 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 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 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 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 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 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 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 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 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 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 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 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 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 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 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 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 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 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 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 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 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 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 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 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 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 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 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 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 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 |
¤t, &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 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 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 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 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 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 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 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 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 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 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 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 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 |
} |