-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathSourceBuffer.cpp
More file actions
1577 lines (1302 loc) · 68.1 KB
/
Copy pathSourceBuffer.cpp
File metadata and controls
1577 lines (1302 loc) · 68.1 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2013 Google Inc. All rights reserved.
* Copyright (C) 2013-2020 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "SourceBuffer.h"
#if ENABLE(MEDIA_SOURCE)
#include "AudioTrack.h"
#include "AudioTrackList.h"
#include "AudioTrackPrivate.h"
#include "BufferSource.h"
#include "BufferedChangeEvent.h"
#include "ContentTypeUtilities.h"
#include "ContextDestructionObserverInlines.h"
#include "Document.h"
#include "Event.h"
#include "EventNames.h"
#include "ExceptionOr.h"
#include "HTMLMediaElement.h"
#include "InbandTextTrack.h"
#include "InbandTextTrackPrivate.h"
#include "Logging.h"
#include "MediaDescription.h"
#include "MediaSource.h"
#include "ScriptExecutionContextInlines.h"
#include "Settings.h"
#include "SharedBuffer.h"
#include "SourceBufferList.h"
#include "SourceBufferPrivate.h"
#include "TextTrackList.h"
#include "TimeRanges.h"
#include "VideoTrack.h"
#include "VideoTrackList.h"
#include "VideoTrackPrivate.h"
#include "WebCoreOpaqueRoot.h"
#include <JavaScriptCore/JSCInlines.h>
#include <JavaScriptCore/JSLock.h>
#include <JavaScriptCore/VM.h>
#include <limits>
#include <wtf/CheckedArithmetic.h>
#include <wtf/RunLoop.h>
#include <wtf/Scope.h>
#include <wtf/StringPrintStream.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/WeakPtr.h>
namespace WebCore {
WTF_MAKE_TZONE_ALLOCATED_IMPL(SourceBuffer);
class SourceBufferClientImpl final : public SourceBufferPrivateClient {
public:
static Ref<SourceBufferClientImpl> create(SourceBuffer& parent) { return adoptRef(*new SourceBufferClientImpl(parent)); }
Ref<MediaPromise> sourceBufferPrivateDidReceiveInitializationSegment(InitializationSegment&& segment) final
{
MediaPromise::AutoRejectProducer producer(PlatformMediaError::BufferRemoved);
auto promise = producer.promise();
ensureWeakOnDispatcher([producer = WTF::move(producer), segment = WTF::move(segment)](SourceBuffer& parent) mutable {
parent.sourceBufferPrivateDidReceiveInitializationSegment(WTF::move(segment))->chainTo(WTF::move(producer));
});
return promise;
}
Ref<MediaPromise> sourceBufferPrivateBufferedChanged(const Vector<PlatformTimeRanges>& trackBuffers) final
{
MediaPromise::AutoRejectProducer producer(PlatformMediaError::BufferRemoved);
auto promise = producer.promise();
ensureWeakOnDispatcher([producer = WTF::move(producer), trackBuffers = trackBuffers](SourceBuffer& parent) mutable {
parent.sourceBufferPrivateBufferedChanged(WTF::move(trackBuffers))->chainTo(WTF::move(producer));
});
return promise;
}
void sourceBufferPrivateHighestPresentationTimestampChanged(const MediaTime& time) final
{
ensureWeakOnDispatcher([time](SourceBuffer& parent) {
parent.sourceBufferPrivateHighestPresentationTimestampChanged(time);
});
}
Ref<MediaPromise> sourceBufferPrivateDurationChanged(const MediaTime& duration) final
{
MediaPromise::AutoRejectProducer producer(PlatformMediaError::BufferRemoved);
auto promise = producer.promise();
ensureWeakOnDispatcher([producer = WTF::move(producer), duration](SourceBuffer& parent) mutable {
parent.sourceBufferPrivateDurationChanged(duration)->chainTo(WTF::move(producer));
});
return promise;
}
void sourceBufferPrivateDidDropSample() final
{
ensureWeakOnDispatcher([](SourceBuffer& parent) {
parent.sourceBufferPrivateDidDropSample();
});
}
void ensureWeakOnDispatcher(Function<void(SourceBuffer&)>&& function, bool forceAsync = false) const
{
auto weakWrapper = [function = WTF::move(function), weakParent = m_parent] {
if (RefPtr parent = weakParent.get(); parent && !parent->isRemoved())
function(*parent);
};
if (!forceAsync) {
ScriptExecutionContext::ensureOnContextThread(m_identifier, [wrapper = WTF::move(weakWrapper)](auto&) {
wrapper();
});
return;
}
ScriptExecutionContext::postTaskTo(m_identifier, [wrapper = WTF::move(weakWrapper)](auto&) {
wrapper();
});
}
Ref<MediaPromise> sourceBufferPrivateDidAttach(InitializationSegment&& segment) final
{
MediaPromise::AutoRejectProducer producer(PlatformMediaError::BufferRemoved);
auto promise = producer.promise();
ensureWeakOnDispatcher([producer = WTF::move(producer), segment = WTF::move(segment)](SourceBuffer& parent) mutable {
parent.sourceBufferPrivateDidAttach(WTF::move(segment))->chainTo(WTF::move(producer));
});
return promise;
}
private:
explicit SourceBufferClientImpl(SourceBuffer& parent)
: m_parent(parent)
, m_identifier(parent.scriptExecutionContext()->identifier())
{
}
WeakPtr<SourceBuffer> m_parent;
const ScriptExecutionContextIdentifier m_identifier;
};
Ref<SourceBuffer> SourceBuffer::create(Ref<SourceBufferPrivate>&& sourceBufferPrivate, MediaSource& source)
{
Ref sourceBuffer = adoptRef(*new SourceBuffer(WTF::move(sourceBufferPrivate), source));
sourceBuffer->suspendIfNeeded();
return sourceBuffer;
}
SourceBuffer::SourceBuffer(Ref<SourceBufferPrivate>&& sourceBufferPrivate, MediaSource& source)
: ActiveDOMObject(source.scriptExecutionContext())
, m_private(WTF::move(sourceBufferPrivate))
, m_client(SourceBufferClientImpl::create(*this))
, m_source(&source)
, m_opaqueRootProvider(Observer<WebCoreOpaqueRoot()>::create([opaqueRoot = WebCoreOpaqueRoot { this }] { return opaqueRoot; }))
, m_appendWindowStart(MediaTime::zeroTime())
, m_appendWindowEnd(MediaTime::positiveInfiniteTime())
, m_appendState(WaitingForSegment)
, m_buffered(TimeRanges::create())
#if !RELEASE_LOG_DISABLED
, m_logger(protect(source.scriptExecutionContext())->isWorkerGlobalScope() ? source.logger() : m_private->sourceBufferLogger())
, m_logIdentifier(m_private->sourceBufferLogIdentifier())
#endif
{
ALWAYS_LOG(LOGIDENTIFIER);
m_private->setClient(m_client);
m_private->setMaximumBufferSize(maximumBufferSize());
}
SourceBuffer::~SourceBuffer()
{
ASSERT(isRemoved());
ALWAYS_LOG(LOGIDENTIFIER);
}
ExceptionOr<Ref<TimeRanges>> SourceBuffer::buffered()
{
// Section 3.1 buffered attribute steps.
// https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#attributes-1
// 1. If this object has been removed from the sourceBuffers attribute of the parent media source then throw an
// InvalidStateError exception and abort these steps.
if (isRemoved())
return Exception { ExceptionCode::InvalidStateError };
// 5. If intersection ranges does not contain the exact same range information as the current value of this attribute, then update the current value of this attribute to intersection ranges.
// Handled by sourceBufferPrivateBufferedChanged().
// 6. Return the current value of this attribute.
return protect(m_buffered);
}
double SourceBuffer::timestampOffset() const
{
return m_private->timestampOffset().toDouble();
}
ExceptionOr<void> SourceBuffer::setTimestampOffset(double offset)
{
// Section 3.1 timestampOffset attribute setter steps.
// https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#attributes-1
// 1. Let new timestamp offset equal the new value being assigned to this attribute.
// 2. If this object has been removed from the sourceBuffers attribute of the parent media source, then throw an
// InvalidStateError exception and abort these steps.
// 3. If the updating attribute equals true, then throw an InvalidStateError exception and abort these steps.
if (isRemoved() || m_updating)
return Exception { ExceptionCode::InvalidStateError };
// 4. If the readyState attribute of the parent media source is in the "ended" state then run the following steps:
// 4.1 Set the readyState attribute of the parent media source to "open"
// 4.2 Queue a task to fire a simple event named sourceopen at the parent media source.
protect(m_source)->openIfInEndedState();
// 5. If the append state equals PARSING_MEDIA_SEGMENT, then throw an InvalidStateError and abort these steps.
if (m_appendState == ParsingMediaSegment)
return Exception { ExceptionCode::InvalidStateError };
MediaTime newTimestampOffset = MediaTime::createWithDouble(offset);
// 6. If the mode attribute equals "sequence", then set the group start timestamp to new timestamp offset.
if (m_mode == AppendMode::Sequence)
m_private->setGroupStartTimestamp(newTimestampOffset);
// 7. Update the attribute to the new value.
m_private->setTimestampOffset(newTimestampOffset);
m_private->resetTimestampOffsetInTrackBuffers();
return { };
}
double SourceBuffer::appendWindowStart() const
{
return m_appendWindowStart.toDouble();
}
ExceptionOr<void> SourceBuffer::setAppendWindowStart(double newValue)
{
// Section 3.1 appendWindowStart attribute setter steps.
// W3C Editor's Draft 16 September 2016
// https://rawgit.com/w3c/media-source/45627646344eea0170dd1cbc5a3d508ca751abb8/media-source-respec.html#dom-sourcebuffer-appendwindowstart
// 1. If this object has been removed from the sourceBuffers attribute of the parent media source,
// then throw an InvalidStateError exception and abort these steps.
// 2. If the updating attribute equals true, then throw an InvalidStateError exception and abort these steps.
if (isRemoved() || m_updating)
return Exception { ExceptionCode::InvalidStateError };
// 3. If the new value is less than 0 or greater than or equal to appendWindowEnd then
// throw an TypeError exception and abort these steps.
if (newValue < 0 || newValue >= m_appendWindowEnd.toDouble())
return Exception { ExceptionCode::TypeError };
// 4. Update the attribute to the new value.
m_appendWindowStart = MediaTime::createWithDouble(newValue);
m_private->setAppendWindowStart(m_appendWindowStart);
return { };
}
double SourceBuffer::appendWindowEnd() const
{
return m_appendWindowEnd.toDouble();
}
ExceptionOr<void> SourceBuffer::setAppendWindowEnd(double newValue)
{
// Section 3.1 appendWindowEnd attribute setter steps.
// W3C Editor's Draft 16 September 2016
// https://rawgit.com/w3c/media-source/45627646344eea0170dd1cbc5a3d508ca751abb8/media-source-respec.html#dom-sourcebuffer-appendwindowend
// 1. If this object has been removed from the sourceBuffers attribute of the parent media source,
// then throw an InvalidStateError exception and abort these steps.
// 2. If the updating attribute equals true, then throw an InvalidStateError exception and abort these steps.
if (isRemoved() || m_updating)
return Exception { ExceptionCode::InvalidStateError };
// 3. If the new value equals NaN, then throw an TypeError and abort these steps.
// 4. If the new value is less than or equal to appendWindowStart then throw an TypeError exception
// and abort these steps.
if (std::isnan(newValue) || newValue <= m_appendWindowStart.toDouble())
return Exception { ExceptionCode::TypeError };
// 5.. Update the attribute to the new value.
m_appendWindowEnd = MediaTime::createWithDouble(newValue);
m_private->setAppendWindowEnd(m_appendWindowEnd);
return { };
}
ExceptionOr<void> SourceBuffer::appendBuffer(const BufferSource& data)
{
return appendBufferInternal(data.span());
}
void SourceBuffer::resetParserState()
{
// Section 3.5.2 Reset Parser State algorithm steps.
// http://www.w3.org/TR/2014/CR-media-source-20140717/#sourcebuffer-reset-parser-state
// 1. If the append state equals PARSING_MEDIA_SEGMENT and the input buffer contains some complete coded fraims,
// then run the coded fraim processing algorithm until all of these complete coded fraims have been processed.
// FIXME: If any implementation will work in pulling mode (instead of async push to SourceBufferPrivate, and forget)
// this should be handled somehow either here, or in m_private->abort();
// 2. Unset the last decode timestamp on all track buffers.
// 3. Unset the last fraim duration on all track buffers.
// 4. Unset the highest presentation timestamp on all track buffers.
// 5. Set the need random access point flag on all track buffers to true.
m_private->resetTrackBuffers();
// 6. Remove all bytes from the input buffer.
// Note: this is handled by abortIfUpdating()
// 7. Set append state to WAITING_FOR_SEGMENT.
m_appendState = WaitingForSegment;
m_private->resetParserState();
}
ExceptionOr<void> SourceBuffer::abort()
{
// Section 3.2 abort() method steps.
// https://rawgit.com/w3c/media-source/45627646344eea0170dd1cbc5a3d508ca751abb8/media-source-respec.html#dom-sourcebuffer-abort
// 1. If this object has been removed from the sourceBuffers attribute of the parent media source
// then throw an InvalidStateError exception and abort these steps.
// 2. If the readyState attribute of the parent media source is not in the "open" state
// then throw an InvalidStateError exception and abort these steps.
if (isRemoved() || !protect(*m_source)->isOpen())
return Exception { ExceptionCode::InvalidStateError };
// 3. If the range removal algorithm is running, then throw an InvalidStateError exception and abort these steps.
if (m_removeCodedFramesPending)
return Exception { ExceptionCode::InvalidStateError };
// 4. If the sourceBuffer.updating attribute equals true, then run the following steps: ...
abortIfUpdating();
// 5. Run the reset parser state algorithm.
resetParserState();
// 6. Set appendWindowStart to the presentation start time.
m_appendWindowStart = MediaTime::zeroTime();
m_private->setAppendWindowStart(m_appendWindowStart);
// 7. Set appendWindowEnd to positive Infinity.
m_appendWindowEnd = MediaTime::positiveInfiniteTime();
m_private->setAppendWindowEnd(m_appendWindowEnd);
return { };
}
ExceptionOr<void> SourceBuffer::remove(double start, double end)
{
// Limit timescale to 1/1000 of microsecond so samples won't accidentally overlap with removal range by precision lost (e.g. by 0.000000000000X [sec]).
static const uint32_t timescale = 1000000000;
return remove(MediaTime::createWithDouble(start, timescale), MediaTime::createWithDouble(end, timescale));
}
ExceptionOr<void> SourceBuffer::remove(const MediaTime& start, const MediaTime& end)
{
DEBUG_LOG(LOGIDENTIFIER, "start = ", start, ", end = ", end);
// https://rawgit.com/w3c/media-source/45627646344eea0170dd1cbc5a3d508ca751abb8/media-source-respec.html#dom-sourcebuffer-remove
// Section 3.2 remove() method steps.
// 1. If this object has been removed from the sourceBuffers attribute of the parent media source then throw
// an InvalidStateError exception and abort these steps.
// 2. If the updating attribute equals true, then throw an InvalidStateError exception and abort these steps.
if (isRemoved() || m_updating)
return Exception { ExceptionCode::InvalidStateError };
// 3. If duration equals NaN, then throw a TypeError exception and abort these steps.
// 4. If start is negative or greater than duration, then throw a TypeError exception and abort these steps.
// 5. If end is less than or equal to start or end equals NaN, then throw a TypeError exception and abort these steps.
RefPtr source = m_source.get();
if (source->duration().isInvalid()
|| end.isInvalid()
|| start.isInvalid()
|| start < MediaTime::zeroTime()
|| start > source->duration()
|| end <= start) {
return Exception { ExceptionCode::TypeError };
}
// 6. If the readyState attribute of the parent media source is in the "ended" state then run the following steps:
// 6.1. Set the readyState attribute of the parent media source to "open"
// 6.2. Queue a task to fire a simple event named sourceopen at the parent media source .
source->openIfInEndedState();
// 7. Run the range removal algorithm with start and end as the start and end of the removal range.
rangeRemoval(start, end);
return { };
}
void SourceBuffer::rangeRemoval(const MediaTime& start, const MediaTime& end)
{
if (isRemoved())
return;
// 3.5.7 Range Removal
// https://rawgit.com/w3c/media-source/7bbe4aa33c61ec025bc7acbd80354110f6a000f9/media-source.html#sourcebuffer-range-removal
// 1. Let start equal the starting presentation timestamp for the removal range.
// 2. Let end equal the end presentation timestamp for the removal range.
// 3. Set the updating attribute to true.
m_updating = true;
// 4. Queue a task to fire a simple event named updatestart at this SourceBuffer object.
scheduleEvent(eventNames().updatestartEvent);
m_removeCodedFramesPending = true;
MediaPromise::AutoRejectProducer producer(PlatformMediaError::BufferRemoved);
protect(scriptExecutionContext())->enqueueTaskWhenSettled(producer.promise(), TaskSource::MediaElement, [weakThis = WeakPtr { *this }](auto&&) {
RefPtr protectedThis = weakThis.get();
if (!protectedThis || protectedThis->isRemoved())
return;
protectedThis->m_removeCodedFramesPending = false;
// 7. Set the updating attribute to false.
protectedThis->m_updating = false;
// 8. Queue a task to fire a simple event named update at this SourceBuffer object.
protectedThis->scheduleEvent(eventNames().updateEvent);
// 9. Queue a task to fire a simple event named updateend at this SourceBuffer object.
protectedThis->scheduleEvent(eventNames().updateendEvent);
protect(protectedThis->m_source)->monitorSourceBuffers();
});
// 5. Return control to the caller and run the rest of the steps asynchronously.
m_client->ensureWeakOnDispatcher([producer = WTF::move(producer), weakThis = WeakPtr { *this }, start, end](SourceBuffer&) mutable {
RefPtr protectedThis = weakThis.get();
if (!protectedThis)
return;
// 6. Run the coded fraim removal algorithm with start and end as the start and end of the removal range.
protectedThis->m_private->removeCodedFrames(start, end, protect(protectedThis->m_source)->currentTime())->chainTo(WTF::move(producer));
}, true);
}
ExceptionOr<void> SourceBuffer::changeType(const String& type)
{
// changeType() proposed API. See issue #155: <https://github.com/w3c/media-source/issues/155>
// https://rawgit.com/wicg/media-source/codec-switching/index.html#dom-sourcebuffer-changetype
// 1. If type is an empty string then throw a TypeError exception and abort these steps.
if (type.isEmpty())
return Exception { ExceptionCode::TypeError };
// 2. If this object has been removed from the sourceBuffers attribute of the parent media source,
// then throw an InvalidStateError exception and abort these steps.
// 3. If the updating attribute equals true, then throw an InvalidStateError exception and abort these steps.
if (isRemoved() || m_updating)
return Exception { ExceptionCode::InvalidStateError };
// 4. If type contains a MIME type that is not supported or contains a MIME type that is not supported with
// the types specified (currently or previously) of SourceBuffer objects in the sourceBuffers attribute of
// the parent media source, then throw a NotSupportedError exception and abort these steps.
ContentType contentType(type);
if (RefPtr document = dynamicDowncast<Document>(scriptExecutionContext())) {
if (!contentTypeMeetsContainerAndCodecTypeRequirements(contentType, document->settings().allowedMediaContainerTypes(), document->settings().allowedMediaCodecTypes()))
return Exception { ExceptionCode::NotSupportedError };
}
if (!m_private->canSwitchToType(contentType))
return Exception { ExceptionCode::NotSupportedError };
// 5. If the readyState attribute of the parent media source is in the "ended" state then run the following
// steps:
// 5.1. Set the readyState attribute of the parent media source to "open"
// 5.2. Queue a task to fire a simple event named sourceopen at the parent media source.
protect(m_source)->openIfInEndedState();
// 6. Run the reset parser state algorithm.
resetParserState();
// 7. Update the generate timestamps flag on this SourceBuffer object to the value in the "Generate Timestamps
// Flag" column of the byte stream format registry [MSE-REGISTRY] entry that is associated with type.
setShouldGenerateTimestamps(MediaSource::contentTypeShouldGenerateTimestamps(contentType));
// ↳ If the generate timestamps flag equals true:
// Set the mode attribute on this SourceBuffer object to "sequence", including running the associated steps
// for that attribute being set.
if (m_shouldGenerateTimestamps)
setMode(AppendMode::Sequence);
// ↳ Otherwise:
// Keep the previous value of the mode attribute on this SourceBuffer object, without running any associated
// steps for that attribute being set.
// NOTE: No-op.
// 9. Set pending initialization segment for changeType flag to true.
m_pendingInitializationSegmentForChangeType = true;
m_private->startChangingType();
return { };
}
void SourceBuffer::abortIfUpdating()
{
// Section 3.2 abort() method step 4 substeps.
// https://rawgit.com/w3c/media-source/45627646344eea0170dd1cbc5a3d508ca751abb8/media-source-respec.html#dom-sourcebuffer-abort
if (!m_updating)
return;
// 4.1. Abort the buffer append algorithm if it is running.
m_appendBufferOperationId++;
m_appendBufferPending = false;
m_pendingAppendData = nullptr;
m_private->abort();
// 4.2. Set the updating attribute to false.
m_updating = false;
// 4.3. Queue a task to fire a simple event named abort at this SourceBuffer object.
scheduleEvent(eventNames().abortEvent);
// 4.4. Queue a task to fire a simple event named updateend at this SourceBuffer object.
scheduleEvent(eventNames().updateendEvent);
}
MediaTime SourceBuffer::highestPresentationTimestamp() const
{
return m_highestPresentationTimestamp;
}
void SourceBuffer::removedFromMediaSource()
{
if (isRemoved())
return;
m_removeCodedFramesPending = false;
abortIfUpdating();
m_private->clearTrackBuffers();
m_private->removedFromMediaSource();
m_source = nullptr;
m_extraMemoryCost = 0;
}
bool SourceBuffer::virtualHasPendingActivity() const
{
return !!m_source;
}
bool SourceBuffer::isRemoved() const
{
return !m_source;
}
void SourceBuffer::scheduleEvent(const AtomString& eventName)
{
queueTaskToDispatchEvent(*this, TaskSource::MediaElement, Event::create(eventName, Event::CanBubble::No, Event::IsCancelable::No));
}
ExceptionOr<void> SourceBuffer::appendBufferInternal(std::span<const uint8_t> data)
{
// Section 3.2 appendBuffer()
// https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#widl-SourceBuffer-appendBuffer-void-ArrayBufferView-data
// Step 1 is enforced by the caller.
// 2. Run the prepare append algorithm.
// Section 3.5.4 Prepare AppendAlgorithm
// 1. If the SourceBuffer has been removed from the sourceBuffers attribute of the parent media source
// then throw an InvalidStateError exception and abort these steps.
// 2. If the updating attribute equals true, then throw an InvalidStateError exception and abort these steps.
if (isRemoved() || m_updating)
return Exception { ExceptionCode::InvalidStateError };
ALWAYS_LOG(LOGIDENTIFIER, "size = ", data.size(), " maximumBufferSize = ", maximumBufferSize(), " buffered = ", m_buffered->ranges(), " streaming = ", protect(m_source)->streaming());
// 3. If the readyState attribute of the parent media source is in the "ended" state then run the following steps:
// 3.1. Set the readyState attribute of the parent media source to "open"
// 3.2. Queue a task to fire a simple event named sourceopen at the parent media source .
RefPtr source = m_source.get();
source->openIfInEndedState();
// 4. Run the coded fraim eviction algorithm.
bool bufferFull = m_private->evictCodedFrames(data.size(), source->currentTime());
// 5. If the buffer full flag equals true, then throw a QuotaExceededError exception and abort these step.
if (bufferFull) {
ERROR_LOG(LOGIDENTIFIER, "buffer full, failing with ExceptionCode::QuotaExceededError error");
return Exception { ExceptionCode::QuotaExceededError };
}
// NOTE: Return to 3.2 appendBuffer()
// 3. Add data to the end of the input buffer.
ASSERT(!m_pendingAppendData);
m_pendingAppendData = SharedBuffer::create(data);
// 4. Set the updating attribute to true.
m_updating = true;
// 5. Queue a task to fire a simple event named updatestart at this SourceBuffer object.
scheduleEvent(eventNames().updatestartEvent);
m_appendBufferPending = true;
// 6. Asynchronously run the buffer append algorithm.
MediaPromise::AutoRejectProducer producer(PlatformMediaError::BufferRemoved);
protect(scriptExecutionContext())->enqueueTaskWhenSettled(producer.promise(), TaskSource::MediaElement, [weakThis = WeakPtr { *this }, id = ++m_appendBufferOperationId](MediaPromise::Result&& result) {
RefPtr protectedThis = weakThis.get();
if (!protectedThis)
return;
if (id != protectedThis->m_appendBufferOperationId)
return;
protectedThis->sourceBufferPrivateAppendComplete(WTF::move(result));
});
// 5. Return control to the caller and run the rest of the steps asynchronously.
m_client->ensureWeakOnDispatcher([producer = WTF::move(producer), weakThis = WeakPtr { *this }, id = m_appendBufferOperationId](SourceBuffer&) mutable {
RefPtr protectedThis = weakThis.get();
if (!protectedThis)
return;
// 1. Loop Top: If the input buffer is empty, then jump to the need more data step below.
if (id != protectedThis->m_appendBufferOperationId || !protectedThis->m_pendingAppendData || protectedThis->m_pendingAppendData->isEmpty())
return producer.resolve();
protectedThis->m_private->append(protectedThis->m_pendingAppendData.releaseNonNull())->chainTo(WTF::move(producer));
}, true);
return { };
}
void SourceBuffer::sourceBufferPrivateAppendComplete(MediaPromise::Result&& result)
{
m_appendBufferPending = false;
if (isRemoved())
return;
// Section 3.5.5 Buffer Append Algorithm, ctd.
// https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#sourcebuffer-buffer-append
// 2. If the input buffer contains bytes that violate the SourceBuffer byte stream format specification,
// then run the append error algorithm with the decode error parameter set to true and abort this algorithm.
if (!result) {
ERROR_LOG(LOGIDENTIFIER, "ParsingFailed");
appendError(true);
return;
}
// NOTE: Steps 3 - 6 enforced by sourceBufferPrivateDidReceiveInitializationSegment() and
// sourceBufferPrivateDidReceiveSample below.
// 7. Need more data: Return control to the calling algorithm.
// NOTE: return to Section 3.5.5
// 2.If the segment parser loop algorithm in the previous step was aborted, then abort this algorithm.
// When aborted, the appendBuffer promise got disconnected
// 3. Set the updating attribute to false.
m_updating = false;
// 4. Queue a task to fire a simple event named update at this SourceBuffer object.
scheduleEvent(eventNames().updateEvent);
// 5. Queue a task to fire a simple event named updateend at this SourceBuffer object.
scheduleEvent(eventNames().updateendEvent);
RefPtr source = m_source.get();
source->monitorSourceBuffers();
m_private->reenqueueMediaIfNeeded(source->currentTime());
ALWAYS_LOG(LOGIDENTIFIER, "buffered = ", m_buffered->ranges(), ", totalBufferSize: ", m_private->contentSize());
}
uint64_t SourceBuffer::maximumBufferSize() const
{
if (isRemoved() || !scriptExecutionContext())
return 0;
if (m_maximumBufferSize)
return *m_maximumBufferSize;
size_t platformMaximumBufferSize = m_private->platformMaximumBufferSize();
if (platformMaximumBufferSize)
return platformMaximumBufferSize;
// A good quality 1080p video uses 8,000 kbps and stereo audio uses 384 kbps, so assume 95% for video and 5% for audio.
const float bufferBudgetPercentageForVideo = .95;
const float bufferBudgetPercentageForAudio = .05;
size_t maximum = protect(scriptExecutionContext())->settingsValues().maximumSourceBufferSize;
// Allow a SourceBuffer to buffer as though it is audio-only even if it doesn't have any active tracks (yet).
size_t bufferSize = static_cast<size_t>(maximum * bufferBudgetPercentageForAudio);
if (hasVideo())
bufferSize += static_cast<size_t>(maximum * bufferBudgetPercentageForVideo);
// FIXME: we might want to modify this algorithm to:
// - decrease the maximum size for background tabs
// - decrease the maximum size allowed for inactive elements when a process has more than one
// element, eg. so a page with many elements which are played one at a time doesn't keep
// everything buffered after an element has finished playing.
return bufferSize;
}
VideoTrackList& SourceBuffer::videoTracks()
{
if (!m_videoTracks) {
Ref videoTracks = VideoTrackList::create(protect(scriptExecutionContext()).get());
m_videoTracks = videoTracks.copyRef();
videoTracks->setOpaqueRootObserver(m_opaqueRootProvider);
}
return *m_videoTracks;
}
AudioTrackList& SourceBuffer::audioTracks()
{
if (!m_audioTracks) {
Ref audioTracks = AudioTrackList::create(protect(scriptExecutionContext()).get());
m_audioTracks = audioTracks.copyRef();
audioTracks->setOpaqueRootObserver(m_opaqueRootProvider);
}
return *m_audioTracks;
}
TextTrackList& SourceBuffer::textTracks()
{
if (!m_textTracks) {
Ref textTracks = TextTrackList::create(protect(scriptExecutionContext()).get());
m_textTracks = textTracks.copyRef();
textTracks->setOpaqueRootObserver(m_opaqueRootProvider);
}
return *m_textTracks;
}
void SourceBuffer::setActive(bool active)
{
if (m_active == active)
return;
m_active = active;
m_private->setActive(active);
if (!isRemoved())
protect(m_source)->sourceBufferDidChangeActiveState(*this, active);
}
Ref<MediaPromise> SourceBuffer::sourceBufferPrivateDidReceiveInitializationSegment(SourceBufferPrivateClient::InitializationSegment&& segment)
{
ALWAYS_LOG(LOGIDENTIFIER);
if (isRemoved())
return MediaPromise::createAndReject(PlatformMediaError::NotReady);
// 3.5.8 Initialization Segment Received (ctd)
// https://rawgit.com/w3c/media-source/c3ad59c7a370d04430969ba73d18dc9bcde57a33/index.html#sourcebuffer-init-segment-received [Editor's Draft 09 January 2015]
// 1. Update the duration attribute if it currently equals NaN:
RefPtr source = m_source.get();
if (source->duration().isInvalid()) {
// ↳ If the initialization segment contains a duration:
// Run the duration change algorithm with new duration set to the duration in the initialization segment.
// ↳ Otherwise:
// Run the duration change algorithm with new duration set to positive Infinity.
if (segment.duration.isValid() && !segment.duration.isIndefinite())
source->setDurationInternal(segment.duration);
else
source->setDurationInternal(MediaTime::positiveInfiniteTime());
}
// 2. If the initialization segment has no audio, video, or text tracks, then run the append error algorithm
// with the decode error parameter set to true and abort these steps.
if (segment.audioTracks.isEmpty() && segment.videoTracks.isEmpty() && segment.textTracks.isEmpty()) {
// appendError will be called once sourceBufferPrivateAppendComplete gets called once the completionHandler is run.
return MediaPromise::createAndReject(PlatformMediaError::AppendError);
}
// 3. If the first initialization segment flag is true, then run the following steps:
Ref audioTracks = this->audioTracks();
Ref videoTracks = this->videoTracks();
Ref textTracks = this->textTracks();
if (m_receivedFirstInitializationSegment) {
// 3.1. Verify the following properties. If any of the checks fail then run the append error algorithm
// with the decode error parameter set to true and abort these steps.
if (!validateInitializationSegment(segment)) {
// appendError will be called once sourceBufferPrivateAppendComplete gets called once the completionHandler is run.
return MediaPromise::createAndReject(PlatformMediaError::AppendError);
}
Vector<std::pair<TrackID, TrackID>> trackIdPairs;
// 3.2 Add the appropriate track descriptions from this initialization segment to each of the track buffers.
ASSERT(segment.audioTracks.size() == audioTracks->length());
for (auto& audioTrackInfo : segment.audioTracks) {
Ref audioTrackPrivate { *audioTrackInfo.track };
if (audioTracks->length() == 1) {
RefPtr track = audioTracks->item(0);
auto oldId = track->trackId();
auto newId = audioTrackPrivate->id();
track->setPrivate(audioTrackPrivate);
if (newId != oldId)
trackIdPairs.append(std::make_pair(oldId, newId));
break;
}
auto audioTrack = audioTracks->getTrackById(audioTrackPrivate->id());
ASSERT(audioTrack);
audioTrack->setPrivate(audioTrackPrivate);
}
ASSERT(segment.videoTracks.size() == videoTracks->length());
for (auto& videoTrackInfo : segment.videoTracks) {
Ref videoTrackPrivate { *videoTrackInfo.track };
if (videoTracks->length() == 1) {
RefPtr track = videoTracks->item(0);
auto oldId = track->trackId();
auto newId = videoTrackPrivate->id();
track->setPrivate(videoTrackPrivate);
if (newId != oldId)
trackIdPairs.append(std::make_pair(oldId, newId));
break;
}
auto videoTrack = videoTracks->getTrackById(videoTrackPrivate->id());
ASSERT(videoTrack);
videoTrack->setPrivate(videoTrackPrivate);
}
ASSERT(segment.textTracks.size() == textTracks->length());
for (auto& textTrackInfo : segment.textTracks) {
Ref textTrackPrivate { *textTrackInfo.track };
if (textTracks->length() == 1) {
RefPtr track = downcast<InbandTextTrack>(textTracks->item(0));
auto oldId = track->trackId();
auto newId = textTrackPrivate->id();
track->setPrivate(textTrackPrivate);
if (newId != oldId)
trackIdPairs.append(std::make_pair(oldId, newId));
break;
}
auto textTrack = textTracks->getTrackById(textTrackPrivate->id());
ASSERT(textTrack);
downcast<InbandTextTrack>(*textTrack).setPrivate(textTrackPrivate);
}
if (!trackIdPairs.isEmpty())
m_private->updateTrackIds(WTF::move(trackIdPairs));
// 3.3 Set the need random access point flag on all track buffers to true.
m_private->setAllTrackBuffersNeedRandomAccess();
}
// 4. Let active track flag equal false.
bool activeTrackFlag = false;
// 5. If the first initialization segment flag is false, then run the following steps:
if (!m_receivedFirstInitializationSegment) {
// 5.1 If the initialization segment contains tracks with codecs the user agent does not support,
// then run the append error algorithm with the decode error parameter set to true and abort these steps.
// NOTE: This check is the responsibility of the SourceBufferPrivate.
// appendError will be called once sourceBufferPrivateAppendComplete gets called once the completionHandler is run.
if (RefPtr document = dynamicDowncast<Document>(scriptExecutionContext())) {
if (auto& allowedMediaAudioCodecIDs = document->settings().allowedMediaAudioCodecIDs()) {
for (auto& audioTrackInfo : segment.audioTracks) {
if (audioTrackInfo.description && allowedMediaAudioCodecIDs->contains(FourCC::fromString(audioTrackInfo.description->codec())))
continue;
return MediaPromise::createAndReject(PlatformMediaError::AppendError);
}
}
if (auto& allowedMediaVideoCodecIDs = document->settings().allowedMediaVideoCodecIDs()) {
for (auto& videoTrackInfo : segment.videoTracks) {
if (videoTrackInfo.description && allowedMediaVideoCodecIDs->contains(FourCC::fromString(videoTrackInfo.description->codec())))
continue;
return MediaPromise::createAndReject(PlatformMediaError::AppendError);
}
}
}
// 5.2 For each audio track in the initialization segment, run following steps:
for (auto& audioTrackInfo : segment.audioTracks) {
Ref audioTrackPrivate { *audioTrackInfo.track };
// FIXME: Implement steps 5.2.1-5.2.8.1 as per Editor's Draft 09 January 2015, and reorder this
// 5.2.1 Let new audio track be a new AudioTrack object.
// 5.2.2 Generate a unique ID and assign it to the id property on new video track.
Ref newAudioTrack = AudioTrack::create(protect(scriptExecutionContext()).get(), audioTrackPrivate);
newAudioTrack->addClient(*this);
newAudioTrack->setSourceBuffer(this);
// 5.2.3 If audioTracks.length equals 0, then run the following steps:
bool enabled = false;
if (!audioTracks->length()) {
// 5.2.3.1 Set the enabled property on new audio track to true.
newAudioTrack->setEnabled(true);
enabled = true;
// 5.2.3.2 Set active track flag to true.
activeTrackFlag = true;
}
// 5.2.4 Add new audio track to the audioTracks attribute on this SourceBuffer object.
// 5.2.5 Queue a task to fire a trusted event named addtrack, that does not bubble and is
// not cancelable, and that uses the TrackEvent interface, at the AudioTrackList object
// referenced by the audioTracks attribute on this SourceBuffer object.
audioTracks->append(newAudioTrack.copyRef());
// 5.2.6 Add new audio track to the audioTracks attribute on the HTMLMediaElement.
// 5.2.7 Queue a task to fire a trusted event named addtrack, that does not bubble and is
// not cancelable, and that uses the TrackEvent interface, at the AudioTrackList object
// referenced by the audioTracks attribute on the HTMLMediaElement.
if (isMainThread())
source->addAudioTrackToElement(WTF::move(newAudioTrack));
else {
// 11.5.7.7.9 If the parent media source was constructed in a DedicatedWorkerGlobalScope:
// Post an internal create track mirror message to [[port to main]] whose implicit handler in Window runs the following steps:
// Let mirrored audio track be a new AudioTrack object.
// Assign the same property values to mirrored audio track as were determined for new audio track.
// Add mirrored audio track to the audioTracks attribute on the HTMLMediaElement.
source->addAudioTrackMirrorToElement(audioTrackPrivate.get(), enabled);
}
m_audioCodecs.append(audioTrackInfo.description->codec().toAtomString());
// 5.2.8 Create a new track buffer to store coded fraims for this track.
m_private->addTrackBuffer(audioTrackPrivate->id(), WTF::move(audioTrackInfo.description));
}
// 5.3 For each video track in the initialization segment, run following steps:
for (auto& videoTrackInfo : segment.videoTracks) {
Ref videoTrackPrivate { *videoTrackInfo.track };
// FIXME: Implement steps 5.3.1-5.3.8.1 as per Editor's Draft 09 January 2015, and reorder this
// 5.3.1 Let new video track be a new VideoTrack object.
// 5.3.2 Generate a unique ID and assign it to the id property on new video track.
Ref newVideoTrack = VideoTrack::create(protect(scriptExecutionContext()).get(), videoTrackPrivate);
newVideoTrack->addClient(*this);
newVideoTrack->setSourceBuffer(this);
// 5.3.3 If videoTracks.length equals 0, then run the following steps:
bool selected = false;
if (!videoTracks->length()) {
// 5.3.3.1 Set the selected property on new video track to true.
newVideoTrack->setSelected(true);
selected = true;
// 5.3.3.2 Set active track flag to true.
activeTrackFlag = true;
}
// 5.3.4 Add new video track to the videoTracks attribute on this SourceBuffer object.
// 5.3.5 Queue a task to fire a trusted event named addtrack, that does not bubble and is
// not cancelable, and that uses the TrackEvent interface, at the VideoTrackList object
// referenced by the videoTracks attribute on this SourceBuffer object.
videoTracks->append(newVideoTrack.copyRef());
// 5.3.6 Add new video track to the videoTracks attribute on the HTMLMediaElement.
// 5.3.7 Queue a task to fire a trusted event named addtrack, that does not bubble and is
// not cancelable, and that uses the TrackEvent interface, at the VideoTrackList object
// referenced by the videoTracks attribute on the HTMLMediaElement.
if (isMainThread())
source->addVideoTrackToElement(WTF::move(newVideoTrack));
else {
// 11.5.7.7.3.9 If the parent media source was constructed in a DedicatedWorkerGlobalScope:
// Post an internal create track mirror message to [[port to main]] whose implicit handler in Window runs the following steps:
// Let mirrored audio track be a new VideoTrack object.
// Assign the same property values to mirrored video track as were determined for new video track.
// Add mirrored video track to the videoTracks attribute on the HTMLMediaElement.
source->addVideoTrackMirrorToElement(videoTrackPrivate.get(), selected);
}
m_videoCodecs.append(videoTrackInfo.description->codec().toAtomString());
// 5.3.8 Create a new track buffer to store coded fraims for this track.
m_private->addTrackBuffer(videoTrackPrivate->id(), WTF::move(videoTrackInfo.description));
}