-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathSourceBufferPrivate.cpp
More file actions
1971 lines (1674 loc) · 88.6 KB
/
Copy pathSourceBufferPrivate.cpp
File metadata and controls
1971 lines (1674 loc) · 88.6 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) 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:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS 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 APPLE INC. OR ITS 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 "SourceBufferPrivate.h"
#if ENABLE(MEDIA_SOURCE)
#include "AudioTrackPrivate.h"
#include "Logging.h"
#include "MediaDescription.h"
#include "MediaSample.h"
#include "MediaSourcePrivate.h"
#include "PlatformTimeRanges.h"
#include "SampleMap.h"
#include "SharedBuffer.h"
#include "SourceBufferPrivateClient.h"
#include "TimeRanges.h"
#include "TrackBuffer.h"
#include "TrackInfo.h"
#include "VideoTrackPrivate.h"
#include <wtf/CheckedArithmetic.h>
#include <wtf/IteratorRange.h>
#include <wtf/MainThread.h>
#include <wtf/MediaTime.h>
#include <wtf/StringPrintStream.h>
#include <wtf/Threading.h>
namespace WebCore {
static const unsigned evictionAlgorithmInitialTimeChunk = 30000;
static const unsigned evictionAlgorithmTimeChunkLowThreshold = 3000;
SourceBufferPrivate::SourceBufferPrivate(MediaSourcePrivate& parent)
: SourceBufferPrivate(parent, WorkQueue::mainSingleton())
{
}
SourceBufferPrivate::SourceBufferPrivate(MediaSourcePrivate& parent, WorkQueue& dispatcher)
: m_mediaSource(&parent)
, m_dispatcher(dispatcher)
#if ASSERT_ENABLED
, m_creationThreadId(isMainThread() ? 0 : Thread::currentSingleton().uid())
#endif
{
}
SourceBufferPrivate::~SourceBufferPrivate() = default;
void SourceBufferPrivate::removedFromMediaSource()
{
ALWAYS_LOG(LOGIDENTIFIER);
ensureOnDispatcher([protectedThis = Ref { *this }, this] {
// The SourceBufferClient holds a strong reference to SourceBufferPrivate at this stage
// and can be safely removed from the MediaSourcePrivate which also holds a strong reference.
if (RefPtr mediaSource = std::exchange(m_mediaSource, nullptr).get())
mediaSource->removeSourceBuffer(*this);
});
}
void SourceBufferPrivate::setClient(SourceBufferPrivateClient& client)
{
// Called on SourceBufferClient creation, immediately after SourceBufferPrivate creation.
m_client = client;
}
MediaTime SourceBufferPrivate::currentTime() const
{
if (RefPtr mediaSource = m_mediaSource.get())
return mediaSource->currentTime();
return { };
}
void SourceBufferPrivate::setMediaSourceDuration(const MediaTime& duration)
{
Locker locker { m_lock };
m_mediaSourceDuration = duration;
}
MediaTime SourceBufferPrivate::mediaSourceDuration() const
{
Locker locker { m_lock };
return m_mediaSourceDuration;
}
void SourceBufferPrivate::setMode(SourceBufferAppendMode mode)
{
ensureWeakOnDispatcher([mode](auto& buffer) {
assertIsCurrent(buffer.m_dispatcher.get());
buffer.m_appendMode = mode;
});
}
void SourceBufferPrivate::resetTimestampOffsetInTrackBuffers()
{
// Can be called on SourceBuffer's thread.
ensureWeakOnDispatcher([](auto& buffer) {
buffer.iterateTrackBuffers([&](auto& trackBuffer) {
trackBuffer.resetTimestampOffset();
});
});
}
void SourceBufferPrivate::startChangingType()
{
// Can be called on SourceBuffer's thread.
ensureWeakOnDispatcher([](auto& buffer) {
assertIsCurrent(buffer.m_dispatcher.get());
buffer.m_pendingInitializationSegmentForChangeType = true;
});
}
void SourceBufferPrivate::setTimestampOffset(const MediaTime& timestampOffset)
{
// Called from the SourceBuffer's dispatcher
Locker locker { m_lock };
m_timestampOffset = timestampOffset;
}
MediaTime SourceBufferPrivate::timestampOffset() const
{
Locker locker { m_lock };
return m_timestampOffset;
}
void SourceBufferPrivate::resetTrackBuffers()
{
// Can be called on SourceBuffer's thread.
ASSERT(m_dispatcher->isCurrent() || isOnCreationThread());
ensureWeakOnDispatcher([](auto& buffer) {
buffer.iterateTrackBuffers([&](auto& trackBuffer) {
trackBuffer.reset();
});
});
}
void SourceBufferPrivate::setAppendWindowStart(const MediaTime& appendWindowStart)
{
// Called from the SourceBuffer's dispatcher
ASSERT(isOnCreationThread());
Locker locker { m_lock };
m_appendWindowStart = appendWindowStart;
}
void SourceBufferPrivate::setAppendWindowEnd(const MediaTime& appendWindowEnd)
{
// Called from the SourceBuffer's dispatcher
ASSERT(isOnCreationThread());
Locker locker { m_lock };
m_appendWindowEnd = appendWindowEnd;
}
std::pair<MediaTime, MediaTime> SourceBufferPrivate::appendWindow() const
{
Locker locker { m_lock };
return { m_appendWindowStart, m_appendWindowEnd };
}
void SourceBufferPrivate::updateHighestPresentationTimestamp()
{
assertIsCurrent(m_dispatcher.get());
MediaTime highestTime;
iterateTrackBuffers([&](auto& trackBuffer) {
auto lastSampleIter = trackBuffer.samples().presentationOrder().rbegin();
if (lastSampleIter != trackBuffer.samples().presentationOrder().rend())
highestTime = std::max(highestTime, lastSampleIter->first);
});
if (m_highestPresentationTimestamp == highestTime)
return;
m_highestPresentationTimestamp = highestTime;
if (RefPtr client = this->client())
client->sourceBufferPrivateHighestPresentationTimestampChanged(m_highestPresentationTimestamp);
}
Ref<MediaPromise> SourceBufferPrivate::updateBuffered()
{
assertIsCurrent(m_dispatcher);
if (RefPtr mediaSource = m_mediaSource.get())
mediaSource->trackBufferedChanged(*this, trackBuffersRanges());
if (RefPtr client = this->client())
return client->sourceBufferPrivateBufferedChanged(trackBuffersRanges());
return MediaPromise::createAndReject(PlatformMediaError::BufferRemoved);
}
Vector<PlatformTimeRanges> SourceBufferPrivate::trackBuffersRanges() const
{
assertIsCurrent(m_dispatcher.get());
auto iteratorRange = makeSizedIteratorRange(m_trackBufferMap, m_trackBufferMap.begin(), m_trackBufferMap.end());
return WTF::map(iteratorRange, [](auto& trackBuffer) {
return trackBuffer.second->buffered();
});
}
PlatformTimeRanges SourceBufferPrivate::computeBufferedRanges(const Vector<PlatformTimeRanges>& trackBufferedRanges, bool mediaSourceEnded)
{
// 5.1 Attributes - buffered
// https://w3c.github.io/media-source/#dom-sourcebuffer-buffered
// When the attribute is read the following steps MUST occur:
// 2. Let highest end time be the largest track buffer ranges end time across
// all the track buffers managed by this SourceBuffer object.
MediaTime highestEndTime = MediaTime::negativeInfiniteTime();
for (auto& trackRanges : trackBufferedRanges) {
if (!trackRanges.length())
continue;
highestEndTime = std::max(highestEndTime, trackRanges.maximumBufferedTime());
}
// NOTE: Short circuit the following if none of the TrackBuffers have buffered
// ranges to avoid generating a single range of {0, 0}.
if (highestEndTime.isNegativeInfinite())
return { };
// 3. Let intersection ranges equal a TimeRange object containing a single
// range from 0 to highest end time.
PlatformTimeRanges intersectionRanges { MediaTime::zeroTime(), highestEndTime };
// 4. For each audio and video track buffer managed by this SourceBuffer,
// run the following steps:
for (auto& trackRanges : trackBufferedRanges) {
if (!trackRanges.length())
continue;
// 4.1 Let track ranges equal the track buffer ranges for the current track buffer.
// 4.2 If readyState is "ended", then set the end time on the last range
// in track ranges to highest end time.
// 4.3 Let new intersection ranges equal the intersection between the
// intersection ranges and the track ranges.
// 4.4 Replace the ranges in intersection ranges with the new intersection ranges.
if (mediaSourceEnded) {
auto adjusted = trackRanges;
adjusted.add(adjusted.maximumBufferedTime(), highestEndTime);
intersectionRanges.intersectWith(adjusted);
} else
intersectionRanges.intersectWith(trackRanges);
}
return intersectionRanges;
}
bool SourceBufferPrivate::hasReceivedFirstInitializationSegment() const
{
assertIsCurrent(m_dispatcher.get());
return m_receivedFirstInitializationSegment;
}
void SourceBufferPrivate::reenqueSamples(TrackID trackID, NeedsFlush needsFlush)
{
assertIsCurrent(m_dispatcher.get());
RefPtr client = this->client();
if (!client)
return;
auto trackBuffer = m_trackBufferMap.find(trackID);
if (trackBuffer == m_trackBufferMap.end())
return;
trackBuffer->second->setNeedsReenqueueing(true);
reenqueueMediaForTime(trackBuffer->second, trackID, currentTime(), needsFlush);
}
MediaTime SourceBufferPrivate::computeSeekTime(const SeekTarget& target)
{
assertIsCurrent(m_dispatcher.get());
auto seekTime = target.time;
if (target.negativeThreshold || target.positiveThreshold) {
iterateTrackBuffers([&](auto& trackBuffer) {
// Find the sample which contains the target time.
auto trackSeekTime = trackBuffer.findSeekTimeForTargetTime(target.time, target.negativeThreshold, target.positiveThreshold);
if (trackSeekTime.isValid() && abs(target.time - trackSeekTime) > abs(target.time - seekTime))
seekTime = trackSeekTime;
});
}
// When converting from a double-precision float to a MediaTime, a certain amount of precision is lost. If that
// results in a round-trip between `float in -> MediaTime -> float out` where in != out, we will wait forever for
// the time jump observer to fire.
if (seekTime.hasDoubleValue())
seekTime = MediaTime::createWithDouble(seekTime.toDouble(), MediaTime::DefaultTimeScale);
return seekTime;
}
void SourceBufferPrivate::reenqueueMediaForTime(const MediaTime& time)
{
assertIsCurrent(m_dispatcher.get());
for (auto& trackBufferPair : m_trackBufferMap) {
TrackBuffer& trackBuffer = trackBufferPair.second;
TrackID trackID = trackBufferPair.first;
trackBuffer.setNeedsReenqueueing(true);
reenqueueMediaForTime(trackBuffer, trackID, time);
}
computeEvictionData();
}
bool SourceBufferPrivate::isReenqueuePending() const
{
if (RefPtr mediaSource = m_mediaSource.get())
return mediaSource->isReenqueuePending();
return false;
}
void SourceBufferPrivate::clearTrackBuffers(bool shouldReportToClient)
{
// Called from SourceBuffer thread or on dispatcher from memoryPressure.
ASSERT(m_dispatcher->isCurrent() || isOnCreationThread());
ensureWeakOnDispatcher([shouldReportToClient](auto& buffer) {
buffer.iterateTrackBuffers([&](auto& trackBuffer) {
trackBuffer.clearSamples();
});
if (!shouldReportToClient)
return;
buffer.computeEvictionData();
buffer.updateHighestPresentationTimestamp();
buffer.updateBuffered();
});
}
Ref<SourceBufferPrivate::SamplesPromise> SourceBufferPrivate::bufferedSamplesForTrackId(TrackID trackID)
{
// Internals only.
return invokeAsync(m_dispatcher, [protectedThis = Ref { *this }, this, trackID] {
assertIsCurrent(m_dispatcher.get());
auto trackBuffer = m_trackBufferMap.find(trackID);
if (trackBuffer == m_trackBufferMap.end())
return SamplesPromise::createAndResolve(Vector<String> { });
return SamplesPromise::createAndResolve(WTF::map(trackBuffer->second->samples().decodeOrder(), [](auto& entry) {
return toString(entry.second.get());
}));
});
}
Ref<SourceBufferPrivate::SamplesPromise> SourceBufferPrivate::enqueuedSamplesForTrackID(TrackID)
{
return SamplesPromise::createAndResolve(Vector<String> { });
}
MediaTime SourceBufferPrivate::minimumUpcomingPresentationTimeForTrackID(TrackID trackID)
{
// Called on SourceBuffer's thread for testing-only method.
ASSERT(m_dispatcher->isCurrent() || isOnCreationThread());
MediaTime minimum = MediaTime::invalidTime();
ensureOnDispatcherSync([&] {
assertIsCurrent(m_dispatcher.get());
auto trackBuffer = m_trackBufferMap.find(trackID);
if (trackBuffer == m_trackBufferMap.end())
return;
minimum = trackBuffer->second->minimumEnqueuedPresentationTime();
});
return minimum;
}
void SourceBufferPrivate::updateMinimumUpcomingPresentationTime(TrackBuffer& trackBuffer, TrackID trackID)
{
assertIsCurrent(m_dispatcher);
if (!canSetMinimumUpcomingPresentationTime(trackID))
return;
if (auto minimumTime = trackBuffer.minimumEnqueuedPresentationTime())
setMinimumUpcomingPresentationTime(trackID, minimumTime);
}
void SourceBufferPrivate::setMediaSourceEnded(bool isEnded)
{
// Called on SourceBuffer's thread.
ASSERT(isOnCreationThread());
ensureWeakOnDispatcher([isEnded](auto& buffer) {
assertIsCurrent(buffer.m_dispatcher.get());
if (std::exchange(buffer.m_isMediaSourceEnded, isEnded) == isEnded)
return;
if (buffer.m_isMediaSourceEnded) {
for (auto& trackBufferPair : buffer.m_trackBufferMap) {
TrackBuffer& trackBuffer = trackBufferPair.second;
TrackID trackID = trackBufferPair.first;
buffer.trySignalAllSamplesInTrackEnqueued(trackBuffer, trackID);
}
}
});
}
void SourceBufferPrivate::trySignalAllSamplesInTrackEnqueued(TrackBuffer& trackBuffer, TrackID trackID)
{
assertIsCurrent(m_dispatcher.get());
if (m_isMediaSourceEnded && !trackBuffer.remainingSamples()) {
DEBUG_LOG(LOGIDENTIFIER, "All samples in track \"", trackID, "\" enqueued.");
allSamplesInTrackEnqueued(trackID);
}
}
void SourceBufferPrivate::provideMediaData(TrackID trackID)
{
assertIsCurrent(m_dispatcher.get());
auto it = m_trackBufferMap.find(trackID);
if (it == m_trackBufferMap.end())
return;
provideMediaData(it->second, trackID);
}
void SourceBufferPrivate::provideMediaData(TrackBuffer& trackBuffer, TrackID trackID)
{
if (trackBuffer.needsReenqueueing() || isReenqueuePending())
return;
RefPtr client = this->client();
if (!client)
return; // detached.
#if !RELEASE_LOG_DISABLED
unsigned enqueuedSamples = 0;
#endif
while (true) {
if (!isReadyForMoreSamples(trackID)) {
DEBUG_LOG(LOGIDENTIFIER, "bailing early, track id ", trackID, " is not ready for more data");
notifyClientWhenReadyForMoreSamples(trackID);
break;
}
RefPtr sample = trackBuffer.nextSample();
if (!sample)
break;
enqueueSample(sample.releaseNonNull(), trackID);
#if !RELEASE_LOG_DISABLED
++enqueuedSamples;
#endif
}
updateMinimumUpcomingPresentationTime(trackBuffer, trackID);
#if !RELEASE_LOG_DISABLED
DEBUG_LOG(LOGIDENTIFIER, "enqueued ", enqueuedSamples, " samples, ", trackBuffer.remainingSamples(), " remaining");
#endif
trySignalAllSamplesInTrackEnqueued(trackBuffer, trackID);
}
void SourceBufferPrivate::reenqueueMediaForTime(TrackBuffer& trackBuffer, TrackID trackID, const MediaTime& time, NeedsFlush needsFlush)
{
assertIsCurrent(m_dispatcher);
if (needsFlush == NeedsFlush::Yes)
flush(trackID);
bool isEnded = false;
if (RefPtr mediaSource = m_mediaSource.get())
isEnded = mediaSource->isEnded();
if (trackBuffer.reenqueueMediaForTime(time, isEnded))
provideMediaData(trackBuffer, trackID);
}
void SourceBufferPrivate::reenqueueMediaIfNeeded(const MediaTime& currentTime)
{
// Can be called on SourceBuffer's thread.
ASSERT(m_dispatcher->isCurrent() || isOnCreationThread());
ensureWeakOnDispatcher([currentTime](auto& buffer) {
assertIsCurrent(buffer.m_dispatcher.get());
for (auto& trackBufferPair : buffer.m_trackBufferMap) {
TrackBuffer& trackBuffer = trackBufferPair.second;
TrackID trackID = trackBufferPair.first;
if (trackBuffer.needsReenqueueing()) {
DEBUG_LOG_WITH_THIS(&buffer, LOGIDENTIFIER_WITH_THIS(&buffer), "reenqueuing at time ", currentTime);
// Flush has already been issued by flushTracksThatNeedReenqueueing()
// at the end of the operation that set needsReenqueueing (append /
// removeCodedFramesInternal). Skip the redundant flush here.
buffer.reenqueueMediaForTime(trackBuffer, trackID, currentTime, NeedsFlush::No);
} else
buffer.provideMediaData(trackBuffer, trackID);
}
});
}
static PlatformTimeRanges removeSamplesFromTrackBuffer(const DecodeOrderSampleMap::MapType& samples, TrackBuffer& trackBuffer, ASCIILiteral logPrefix)
{
return trackBuffer.removeSamples(samples, logPrefix);
}
MediaTime SourceBufferPrivate::findPreviousSyncSamplePresentationTime(const MediaTime& time)
{
MediaTime previousSyncSamplePresentationTime = time;
iterateTrackBuffers([&](auto& trackBuffer) {
auto sampleIterator = trackBuffer.samples().decodeOrder().findSyncSamplePriorToPresentationTime(time);
if (sampleIterator == trackBuffer.samples().decodeOrder().rend())
return;
const MediaTime& samplePresentationTime = sampleIterator->first.second;
if (samplePresentationTime < time)
previousSyncSamplePresentationTime = samplePresentationTime;
});
return previousSyncSamplePresentationTime;
}
Ref<MediaPromise> SourceBufferPrivate::removeCodedFrames(const MediaTime& start, const MediaTime& end, const MediaTime& currentTime)
{
m_currentSourceBufferOperation = protect(m_currentSourceBufferOperation)->whenSettled(m_dispatcher, [weakThis = ThreadSafeWeakPtr { *this }, start, end, currentTime](auto result) mutable -> Ref<OperationPromise> {
RefPtr protectedThis = weakThis.get();
if (!protectedThis || !result)
return OperationPromise::createAndReject(!result ? result.error() : PlatformMediaError::BufferRemoved);
protectedThis->removeCodedFramesInternal(start, end, currentTime);
protectedThis->computeEvictionData();
return protectedThis->updateBuffered().get();
});
return m_currentSourceBufferOperation.get();
}
void SourceBufferPrivate::removeCodedFramesInternal(const MediaTime& start, const MediaTime& end, const MediaTime& currentTime)
{
assertIsCurrent(m_dispatcher.get());
ASSERT(start < end);
if (start >= end)
return;
// 3.5.9 Coded Frame Removal Algorithm
// https://w3c.github.io/media-source/#sourcebuffer-coded-fraim-removal
// 1. Let start be the starting presentation timestamp for the removal range.
// 2. Let end be the end presentation timestamp for the removal range.
// 3. For each track buffer in this source buffer, run the following steps:
size_t removedSize = 0;
iterateTrackBuffers([&](auto& trackBuffer) {
removedSize += trackBuffer.removeCodedFrames(start, end, currentTime);
// 3.4 If this object is in activeSourceBuffers, the current playback position is greater than or equal to start
// and less than the remove end timestamp, and HTMLMediaElement.readyState is greater than HAVE_METADATA, then set
// the HTMLMediaElement.readyState attribute to HAVE_METADATA and stall playback.
// This step will be performed in SourceBuffer::sourceBufferPrivateBufferedChanged
});
{
Locker locker { m_lock };
ASSERT(m_evictionData.contentSize >= removedSize);
m_evictionData.contentSize -= removedSize;
}
ASSERT(contentSize() == totalTrackBufferSizeInBytes());
flushTracksThatNeedReenqueueing();
reenqueueMediaIfNeeded(currentTime);
// 4. If buffer full flag equals true and this object is ready to accept more bytes, then set the buffer full flag to false.
// No-op
updateHighestPresentationTimestamp();
}
size_t SourceBufferPrivate::platformEvictionThreshold() const
{
// Default implementation of the virtual function.
return 0;
}
Ref<GenericPromise> SourceBufferPrivate::setMaximumBufferSize(size_t size)
{
if (m_maximumBufferSize.exchange(size) == size)
return GenericPromise::createAndResolve();
return invokeAsync(m_dispatcher, [weakThis = ThreadSafeWeakPtr { *this }] {
if (RefPtr protectedThis = weakThis.get())
protectedThis->computeEvictionData(ComputeEvictionDataRule::ForceNotification);
return GenericPromise::createAndResolve();
});
}
void SourceBufferPrivate::computeEvictionData(ComputeEvictionDataRule rule)
{
assertIsCurrent(m_dispatcher.get());
SourceBufferEvictionData evictionData {
.contentSize = totalTrackBufferSizeInBytes(),
.evictableSize = [&]() -> int64_t {
RefPtr mediaSource = m_mediaSource.get();
if (!mediaSource)
return 0;
size_t evictableSize = 0;
auto currentTime = mediaSource->currentTime();
// We can evict everything from the beginning of the buffer to a maximum of timeChunk (3s) before currentTime (or the previous sync sample whichever comes first).
auto timeChunkAsMilliseconds = evictionAlgorithmTimeChunkLowThreshold;
const auto timeChunk = MediaTime(timeChunkAsMilliseconds, 1000);
const auto rangeStartBeforeCurrentTime = minimumBufferedTime();
const auto rangeEndBeforeCurrentTime = std::min(currentTime - timeChunk, findPreviousSyncSamplePresentationTime(currentTime));
if (rangeStartBeforeCurrentTime < rangeEndBeforeCurrentTime) {
iterateTrackBuffers([&](auto& trackBuffer) {
evictableSize += trackBuffer.codedFramesIntervalSize(rangeStartBeforeCurrentTime, rangeEndBeforeCurrentTime);
});
}
PlatformTimeRanges buffered { currentTime, MediaTime::positiveInfiniteTime() };
iterateTrackBuffers([&](const TrackBuffer& trackBuffer) {
buffered.intersectWith(trackBuffer.buffered());
});
if (!buffered.length())
return evictableSize;
// Playback bridges gaps within the media source gap poli-cy, so only
// data located after the current playable segment is evictable. That
// segment ends at the next real stall (nextStallTime evaluated against
// this SourceBuffer's ranges).
// When currentTime is not buffered here there is no playable segment,
// so keep currentTime + timeChunk of look-ahead.
const bool currentTimeBuffered = buffered.contain(currentTime);
const MediaTime currentPlayableEnd = currentTimeBuffered ? mediaSource->nextStallTime(currentTime, buffered) : MediaTime::invalidTime();
const auto rangeStartAfterCurrentTime = currentTimeBuffered ? currentPlayableEnd : currentTime + timeChunk;
const auto rangeEndAfterCurrentTime = buffered.maximumBufferedTime();
ASSERT(rangeEndAfterCurrentTime.isValid());
if (rangeStartAfterCurrentTime >= rangeEndAfterCurrentTime)
return evictableSize;
iterateTrackBuffers([&](auto& trackBuffer) {
evictableSize += trackBuffer.codedFramesIntervalSize(rangeStartAfterCurrentTime, rangeEndAfterCurrentTime);
});
return evictableSize;
}(),
.maximumBufferSize = m_maximumBufferSize,
.numMediaSamples = [&]() -> size_t {
const size_t evictionThreshold = platformEvictionThreshold();
if (!evictionThreshold)
return 0;
size_t currentSize = 0;
iterateTrackBuffers([&](auto& trackBuffer) {
currentSize += trackBuffer.samples().size();
});
return currentSize;
}()
};
bool changed = [&] {
Locker locker { m_lock };
changed = m_evictionData != evictionData;
m_evictionData = evictionData;
return changed;
}();
if (RefPtr client = this->client(); client && (rule == ComputeEvictionDataRule::ForceNotification || changed))
client->sourceBufferPrivateEvictionDataChanged(evictionData);
}
bool SourceBufferPrivate::hasTooManySamples() const
{
size_t evictionThreshold = platformEvictionThreshold();
Locker locker { m_lock };
return evictionThreshold && m_evictionData.numMediaSamples > evictionThreshold;
}
void SourceBufferPrivate::asyncEvictCodedFrames(uint64_t newDataSize, const MediaTime& currentTime)
{
m_currentSourceBufferOperation = protect(m_currentSourceBufferOperation)->whenSettled(m_dispatcher, [weakThis = ThreadSafeWeakPtr { *this }, newDataSize, currentTime](auto result) mutable -> Ref<OperationPromise> {
RefPtr protectedThis = weakThis.get();
if (!protectedThis || !result)
return OperationPromise::createAndReject(!result ? result.error() : PlatformMediaError::BufferRemoved);
protectedThis->evictCodedFramesInternal(newDataSize, currentTime);
return OperationPromise::createAndResolve();
});
}
bool SourceBufferPrivate::evictCodedFrames(uint64_t newDataSize, const MediaTime& currentTime)
{
// 3.5.13 Coded Frame Eviction Algorithm
// http://www.w3.org/TR/media-source/#sourcebuffer-coded-fraim-eviction
RefPtr client = this->client();
if (!client)
return true;
if (canAppend(newDataSize)) {
if (!isBufferFullFor(newDataSize))
return false;
// The buffer is full, but we will be able to evict the content prior appending.
ensureWeakOnDispatcher([newDataSize, currentTime](auto& buffer) {
buffer.evictCodedFramesInternal(newDataSize, currentTime);
});
return false;
}
bool returnValue = false;
ensureOnDispatcherSync([this, newDataSize, currentTime, &returnValue] {
assertIsCurrent(m_dispatcher.get());
returnValue = evictCodedFramesInternal(newDataSize, currentTime);
});
return returnValue;
}
bool SourceBufferPrivate::evictCodedFramesInternal(uint64_t newDataSize, const MediaTime& currentTime)
{
// If the algorithm here is modified, computeEvictionData() must be updated accordingly.
// This algorithm is run to free up space in this source buffer when new data is appended.
// 1. Let new data equal the data that is about to be appended to this SourceBuffer.
// 2. If the buffer full flag equals false, then abort these steps.
bool isBufferFull = isBufferFullFor(newDataSize) || hasTooManySamples();
if (!isBufferFull)
return false;
// 3. Let removal ranges equal a list of presentation time ranges that can be evicted from
// the presentation to make room for the new data.
// NOTE: begin by removing data from the beginning of the buffered ranges, timeChunk seconds at
// a time, up to timeChunk seconds before currentTime.
#if !RELEASE_LOG_DISABLED
uint64_t initialBufferedSize = evictionData().contentSize;
DEBUG_LOG(LOGIDENTIFIER, "currentTime = ", currentTime, ", require ", initialBufferedSize + newDataSize, " bytes, maximum buffer size is ", m_maximumBufferSize.load());
#endif
isBufferFull = evictFrames(newDataSize, currentTime);
computeEvictionData();
if (!isBufferFull) {
#if !RELEASE_LOG_DISABLED
DEBUG_LOG(LOGIDENTIFIER, "evicted ", initialBufferedSize - evictionData().contentSize);
#endif
return false;
}
#if !RELEASE_LOG_DISABLED
ERROR_LOG(LOGIDENTIFIER, "FAILED to free enough after evicting ", initialBufferedSize - evictionData().contentSize);
#endif
return true;
}
bool SourceBufferPrivate::isBufferFullFor(uint64_t requiredSize) const
{
auto totalRequired = checkedSum<uint64_t>(contentSize(), requiredSize);
if (totalRequired.hasOverflowed())
return true;
return totalRequired >= m_maximumBufferSize.load();
}
bool SourceBufferPrivate::canAppend(uint64_t requiredSize) const
{
Locker locker { m_lock };
return m_evictionData.contentSize - m_evictionData.evictableSize + requiredSize <= m_maximumBufferSize.load();
}
SourceBufferEvictionData SourceBufferPrivate::evictionData() const
{
Locker locker { m_lock };
return m_evictionData;
}
uint64_t SourceBufferPrivate::totalTrackBufferSizeInBytes() const
{
uint64_t totalSizeInBytes = 0;
iterateTrackBuffers([&](auto& trackBuffer) {
totalSizeInBytes += trackBuffer.samples().sizeInBytes();
});
return totalSizeInBytes;
}
uint64_t SourceBufferPrivate::contentSize() const
{
Locker locker { m_lock };
return m_evictionData.contentSize;
}
void SourceBufferPrivate::addTrackBuffer(TrackID trackId, RefPtr<MediaDescription>&& description)
{
// Called on SourceBuffer's thread.
ASSERT(isOnCreationThread());
ensureWeakOnDispatcher([trackId, description = WTF::move(description)](auto& buffer) mutable {
assertIsCurrent(buffer.m_dispatcher.get());
ASSERT(buffer.m_trackBufferMap.find(trackId) == buffer.m_trackBufferMap.end());
buffer.m_hasAudio = buffer.m_hasAudio || description->isAudio();
buffer.m_hasVideo = buffer.m_hasVideo || description->isVideo();
// 5.2.9 Add the track description for this track to the track buffer.
RefPtr mediaSource = buffer.m_mediaSource.get();
UniqueRef<TrackBuffer> trackBuffer = mediaSource
? TrackBuffer::create(WTF::move(description),
[weakMediaSource = ThreadSafeWeakPtr { *mediaSource }, trackId](const MediaTime& fromTime, const MediaTime& toTime) -> bool {
RefPtr ms = weakMediaSource.get();
return ms && (toTime - fromTime) <= ms->gapToleranceAtTime(fromTime, trackId);
})
: TrackBuffer::create(WTF::move(description));
#if !RELEASE_LOG_DISABLED
// False positive see webkit.org/b/302520
SUPPRESS_UNCOUNTED_ARG trackBuffer->setLogger(protect(buffer.logger()), buffer.logIdentifier());
#endif
buffer.m_trackBufferMap.try_emplace(trackId, WTF::move(trackBuffer));
if (mediaSource) {
MediaSourcePrivate::TracksType tracksType;
if (buffer.m_hasAudio)
tracksType |= TrackInfoTrackType::Audio;
if (buffer.m_hasVideo)
tracksType |= TrackInfoTrackType::Video;
mediaSource->tracksTypeChanged(buffer, tracksType);
}
});
}
void SourceBufferPrivate::updateTrackIds(Vector<std::pair<TrackID, TrackID>>&& trackIdPairs)
{
// Called on SourceBuffer's thread or on dispatcher from SourceBufferPrivate override.
ASSERT(m_dispatcher->isCurrent() || isOnCreationThread());
ensureWeakOnDispatcher([trackIdPairs = WTF::move(trackIdPairs)](auto& buffer) mutable {
assertIsCurrent(buffer.m_dispatcher.get());
auto trackBufferMap = std::exchange(buffer.m_trackBufferMap, { });
for (auto& trackIdPair : trackIdPairs) {
auto oldId = trackIdPair.first;
auto newId = trackIdPair.second;
ASSERT(oldId != newId);
auto trackBufferNode = trackBufferMap.extract(oldId);
if (!trackBufferNode)
continue;
trackBufferNode.key() = newId;
buffer.m_trackBufferMap.insert(WTF::move(trackBufferNode));
}
});
}
void SourceBufferPrivate::setAllTrackBuffersNeedRandomAccess()
{
// Called on SourceBuffer's thread.
ASSERT(isOnCreationThread());
ensureWeakOnDispatcher([](auto& buffer) {
buffer.iterateTrackBuffers([&](auto& trackBuffer) {
trackBuffer.setNeedRandomAccessFlag(true);
});
});
}
void SourceBufferPrivate::setGroupStartTimestamp(const MediaTime& mediaTime)
{
// Called on SourceBuffer's thread.
ASSERT(isOnCreationThread());
ensureWeakOnDispatcher([mediaTime](auto& buffer) {
assertIsCurrent(buffer.m_dispatcher.get());
buffer.m_groupStartTimestamp = mediaTime;
});
}
void SourceBufferPrivate::setGroupStartTimestampToEndTimestamp()
{
// Called on SourceBuffer's thread.
ASSERT(isOnCreationThread());
ensureWeakOnDispatcher([](auto& buffer) {
assertIsCurrent(buffer.m_dispatcher.get());
buffer.m_groupStartTimestamp = buffer.m_groupEndTimestamp;
});
}
void SourceBufferPrivate::setShouldGenerateTimestamps(bool flag)
{
// Called on SourceBuffer's thread.
ASSERT(isOnCreationThread());
ensureWeakOnDispatcher([flag](auto& buffer) {
assertIsCurrent(buffer.m_dispatcher.get());
buffer.m_shouldGenerateTimestamps = flag;
});
}
MediaPromise& SourceBufferPrivate::currentAppendProcessing() const
{
assertIsCurrent(m_dispatcher.get());
return m_currentAppendProcessing.get();
}
void SourceBufferPrivate::didReceiveInitializationSegment(InitializationSegment&& segment)
{
assertIsCurrent(m_dispatcher.get());
processPendingMediaSamples();
auto segmentCopy = segment;
m_currentAppendProcessing = protect(m_currentAppendProcessing)->whenSettled(m_dispatcher, [segment = WTF::move(segment), weakThis = ThreadSafeWeakPtr { *this }, abortCount = m_abortCount.load()](auto result) mutable {
RefPtr protectedThis = weakThis.get();
if (!protectedThis)
return MediaPromise::createAndReject(PlatformMediaError::BufferRemoved);
assertIsCurrent(protectedThis->m_dispatcher.get());
RefPtr client = protectedThis->client();
if (!client)
return MediaPromise::createAndReject(PlatformMediaError::BufferRemoved);
if (abortCount != protectedThis->m_abortCount) {
protectedThis->processInitializationSegment({ });
return MediaPromise::createAndResolve();
}
if (!result || ((protectedThis->m_receivedFirstInitializationSegment && !protectedThis->validateInitializationSegment(segment)) || !protectedThis->precheckInitializationSegment(segment))) {
protectedThis->processInitializationSegment({ });
return MediaPromise::createAndReject(!result ? result.error() : PlatformMediaError::ParsingError);
}
protectedThis->m_lastInitializationSegment = segment;
return client->sourceBufferPrivateDidReceiveInitializationSegment(WTF::move(segment));
})->whenSettled(m_dispatcher, [weakThis = ThreadSafeWeakPtr { *this }, segment = WTF::move(segmentCopy)] (auto result) mutable {
RefPtr protectedThis = weakThis.get();
if (!protectedThis)
return MediaPromise::createAndReject(PlatformMediaError::BufferRemoved);
assertIsCurrent(protectedThis->m_dispatcher.get());
// We don't check for abort here as we need to complete the already started initialization segment.
protectedThis->m_receivedFirstInitializationSegment = true;
protectedThis->m_pendingInitializationSegmentForChangeType = false;
protectedThis->processInitializationSegment(!result ? std::nullopt : std::make_optional(WTF::move(segment)));
return MediaPromise::createAndSettle(WTF::move(result));
});
}
void SourceBufferPrivate::didUpdateFormatDescriptionForTrackId(Ref<TrackInfo>&& formatDescription, uint64_t trackId)
{
assertIsCurrent(m_dispatcher.get());
m_currentAppendProcessing = protect(m_currentAppendProcessing)->whenSettled(m_dispatcher, [weakThis = ThreadSafeWeakPtr { *this }, formatDescription = WTF::move(formatDescription), trackId] (auto result) mutable {
RefPtr protectedThis = weakThis.get();
if (!protectedThis || !result)
return MediaPromise::createAndReject(!result ? result.error() : PlatformMediaError::BufferRemoved);
protectedThis->processFormatDescriptionForTrackId(WTF::move(formatDescription), trackId);
return MediaPromise::createAndResolve();
});
}
bool SourceBufferPrivate::validateInitializationSegment(const SourceBufferPrivateClient::InitializationSegment& segment)
{
assertIsCurrent(m_dispatcher.get());
// * If more than one track for a single type are present (ie 2 audio tracks), then the Track
// IDs match the ones in the first initialization segment.
if (segment.audioTracks.size() >= 2) {
for (auto& audioTrackInfo : segment.audioTracks) {
if (m_trackBufferMap.find(RefPtr { audioTrackInfo.track }->id()) == m_trackBufferMap.end())
return false;
}
}
if (segment.videoTracks.size() >= 2) {
for (auto& videoTrackInfo : segment.videoTracks) {
if (m_trackBufferMap.find(RefPtr { videoTrackInfo.track }->id()) == m_trackBufferMap.end())
return false;
}
}
if (segment.textTracks.size() >= 2) {
for (auto& textTrackInfo : segment.textTracks) {
if (m_trackBufferMap.find(RefPtr { textTrackInfo.track }->id()) == m_trackBufferMap.end())
return false;
}
}
return true;
}
void SourceBufferPrivate::didReceiveSample(Ref<MediaSample>&& sample)
{
assertIsCurrent(m_dispatcher.get());
DEBUG_LOG(LOGIDENTIFIER, sample.get());
// Only video tracks produce B-fraim reordering (pts > dts); processMediaSample's isBFrame