2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * Copyright (C) 2013-2014 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "SourceBuffer.h"
35 #if ENABLE(MEDIA_SOURCE)
37 #include "AudioTrackList.h"
39 #include "EventNames.h"
40 #include "ExceptionCodePlaceholder.h"
41 #include "GenericEventQueue.h"
42 #include "HTMLMediaElement.h"
43 #include "InbandTextTrack.h"
45 #include "MediaDescription.h"
46 #include "MediaSample.h"
47 #include "MediaSource.h"
48 #include "SampleMap.h"
49 #include "SourceBufferPrivate.h"
50 #include "TextTrackList.h"
51 #include "TimeRanges.h"
52 #include "VideoTrackList.h"
55 #include <runtime/JSCInlines.h>
56 #include <runtime/JSLock.h>
57 #include <runtime/VM.h>
58 #include <wtf/CurrentTime.h>
59 #include <wtf/NeverDestroyed.h>
61 #include <wtf/text/StringBuilder.h>
66 static const double ExponentialMovingAverageCoefficient = 0.1;
68 // Allow hasCurrentTime() to be off by as much as the length of two 24fps video frames
69 static const MediaTime& currentTimeFudgeFactor()
71 static NeverDestroyed<MediaTime> fudgeFactor(2002, 24000);
75 struct SourceBuffer::TrackBuffer {
76 MediaTime lastDecodeTimestamp;
77 MediaTime lastFrameDuration;
78 MediaTime highestPresentationTimestamp;
79 MediaTime lastEnqueuedPresentationTime;
80 MediaTime lastEnqueuedDecodeEndTime;
81 bool needRandomAccessFlag { true };
82 bool enabled { false };
83 bool needsReenqueueing { false };
85 DecodeOrderSampleMap::MapType decodeQueue;
86 RefPtr<MediaDescription> description;
89 : lastDecodeTimestamp(MediaTime::invalidTime())
90 , lastFrameDuration(MediaTime::invalidTime())
91 , highestPresentationTimestamp(MediaTime::invalidTime())
92 , lastEnqueuedPresentationTime(MediaTime::invalidTime())
93 , lastEnqueuedDecodeEndTime(MediaTime::invalidTime())
98 Ref<SourceBuffer> SourceBuffer::create(Ref<SourceBufferPrivate>&& sourceBufferPrivate, MediaSource* source)
100 auto sourceBuffer = adoptRef(*new SourceBuffer(WTFMove(sourceBufferPrivate), source));
101 sourceBuffer->suspendIfNeeded();
105 SourceBuffer::SourceBuffer(Ref<SourceBufferPrivate>&& sourceBufferPrivate, MediaSource* source)
106 : ActiveDOMObject(source->scriptExecutionContext())
107 , m_private(WTFMove(sourceBufferPrivate))
109 , m_asyncEventQueue(*this)
110 , m_appendBufferTimer(*this, &SourceBuffer::appendBufferTimerFired)
111 , m_appendWindowStart(MediaTime::zeroTime())
112 , m_appendWindowEnd(MediaTime::positiveInfiniteTime())
113 , m_groupStartTimestamp(MediaTime::invalidTime())
114 , m_groupEndTimestamp(MediaTime::zeroTime())
115 , m_buffered(TimeRanges::create())
116 , m_appendState(WaitingForSegment)
117 , m_timeOfBufferingMonitor(monotonicallyIncreasingTime())
118 , m_pendingRemoveStart(MediaTime::invalidTime())
119 , m_pendingRemoveEnd(MediaTime::invalidTime())
120 , m_removeTimer(*this, &SourceBuffer::removeTimerFired)
124 m_private->setClient(this);
127 SourceBuffer::~SourceBuffer()
131 m_private->setClient(nullptr);
134 RefPtr<TimeRanges> SourceBuffer::buffered(ExceptionCode& ec) const
136 // Section 3.1 buffered attribute steps.
137 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#attributes-1
138 // 1. If this object has been removed from the sourceBuffers attribute of the parent media source then throw an
139 // INVALID_STATE_ERR exception and abort these steps.
141 ec = INVALID_STATE_ERR;
145 // 2. Return a new static normalized TimeRanges object for the media segments buffered.
146 return m_buffered->copy();
149 const RefPtr<TimeRanges>& SourceBuffer::buffered() const
154 double SourceBuffer::timestampOffset() const
156 return m_timestampOffset.toDouble();
159 void SourceBuffer::setTimestampOffset(double offset, ExceptionCode& ec)
161 // Section 3.1 timestampOffset attribute setter steps.
162 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#attributes-1
163 // 1. Let new timestamp offset equal the new value being assigned to this attribute.
164 // 2. If this object has been removed from the sourceBuffers attribute of the parent media source, then throw an
165 // INVALID_STATE_ERR exception and abort these steps.
166 // 3. If the updating attribute equals true, then throw an INVALID_STATE_ERR exception and abort these steps.
167 if (isRemoved() || m_updating) {
168 ec = INVALID_STATE_ERR;
172 // 4. If the readyState attribute of the parent media source is in the "ended" state then run the following steps:
173 // 4.1 Set the readyState attribute of the parent media source to "open"
174 // 4.2 Queue a task to fire a simple event named sourceopen at the parent media source.
175 m_source->openIfInEndedState();
177 // 5. If the append state equals PARSING_MEDIA_SEGMENT, then throw an INVALID_STATE_ERR and abort these steps.
178 if (m_appendState == ParsingMediaSegment) {
179 ec = INVALID_STATE_ERR;
183 MediaTime newTimestampOffset = MediaTime::createWithDouble(offset);
185 // 6. If the mode attribute equals "sequence", then set the group start timestamp to new timestamp offset.
186 if (m_mode == AppendMode::Sequence)
187 m_groupStartTimestamp = newTimestampOffset;
189 // 7. Update the attribute to the new value.
190 m_timestampOffset = newTimestampOffset;
193 double SourceBuffer::appendWindowStart() const
195 return m_appendWindowStart.toDouble();
198 void SourceBuffer::setAppendWindowStart(double newValue, ExceptionCode& ec)
200 // Section 3.1 appendWindowStart attribute setter steps.
201 // http://www.w3.org/TR/media-source/#widl-SourceBuffer-appendWindowStart
202 // 1. If this object has been removed from the sourceBuffers attribute of the parent media source,
203 // then throw an INVALID_STATE_ERR exception and abort these steps.
204 // 2. If the updating attribute equals true, then throw an INVALID_STATE_ERR exception and abort these steps.
205 if (isRemoved() || m_updating) {
206 ec = INVALID_STATE_ERR;
210 // 3. If the new value is less than 0 or greater than or equal to appendWindowEnd then
211 // throw an INVALID_ACCESS_ERR exception and abort these steps.
212 if (newValue < 0 || newValue >= m_appendWindowEnd.toDouble()) {
213 ec = INVALID_ACCESS_ERR;
217 // 4. Update the attribute to the new value.
218 m_appendWindowStart = MediaTime::createWithDouble(newValue);
221 double SourceBuffer::appendWindowEnd() const
223 return m_appendWindowEnd.toDouble();
226 void SourceBuffer::setAppendWindowEnd(double newValue, ExceptionCode& ec)
228 // Section 3.1 appendWindowEnd attribute setter steps.
229 // http://www.w3.org/TR/media-source/#widl-SourceBuffer-appendWindowEnd
230 // 1. If this object has been removed from the sourceBuffers attribute of the parent media source,
231 // then throw an INVALID_STATE_ERR exception and abort these steps.
232 // 2. If the updating attribute equals true, then throw an INVALID_STATE_ERR exception and abort these steps.
233 if (isRemoved() || m_updating) {
234 ec = INVALID_STATE_ERR;
238 // 3. If the new value equals NaN, then throw an INVALID_ACCESS_ERR and abort these steps.
239 // 4. If the new value is less than or equal to appendWindowStart then throw an INVALID_ACCESS_ERR exception
240 // and abort these steps.
241 if (std::isnan(newValue) || newValue <= m_appendWindowStart.toDouble()) {
242 ec = INVALID_ACCESS_ERR;
246 // 5.. Update the attribute to the new value.
247 m_appendWindowEnd = MediaTime::createWithDouble(newValue);
251 void SourceBuffer::appendBuffer(ArrayBuffer& data, ExceptionCode& ec)
253 appendBufferInternal(static_cast<unsigned char*>(data.data()), data.byteLength(), ec);
256 void SourceBuffer::appendBuffer(ArrayBufferView& data, ExceptionCode& ec)
258 appendBufferInternal(static_cast<unsigned char*>(data.baseAddress()), data.byteLength(), ec);
261 void SourceBuffer::resetParserState()
263 // Section 3.5.2 Reset Parser State algorithm steps.
264 // http://www.w3.org/TR/2014/CR-media-source-20140717/#sourcebuffer-reset-parser-state
265 // 1. If the append state equals PARSING_MEDIA_SEGMENT and the input buffer contains some complete coded frames,
266 // then run the coded frame processing algorithm until all of these complete coded frames have been processed.
267 // FIXME: If any implementation will work in pulling mode (instead of async push to SourceBufferPrivate, and forget)
268 // this should be handled somehow either here, or in m_private->abort();
270 // 2. Unset the last decode timestamp on all track buffers.
271 // 3. Unset the last frame duration on all track buffers.
272 // 4. Unset the highest presentation timestamp on all track buffers.
273 // 5. Set the need random access point flag on all track buffers to true.
274 for (auto& trackBufferPair : m_trackBufferMap.values()) {
275 trackBufferPair.lastDecodeTimestamp = MediaTime::invalidTime();
276 trackBufferPair.lastFrameDuration = MediaTime::invalidTime();
277 trackBufferPair.highestPresentationTimestamp = MediaTime::invalidTime();
278 trackBufferPair.needRandomAccessFlag = true;
280 // 6. Remove all bytes from the input buffer.
281 // Note: this is handled by abortIfUpdating()
282 // 7. Set append state to WAITING_FOR_SEGMENT.
283 m_appendState = WaitingForSegment;
288 void SourceBuffer::abort(ExceptionCode& ec)
290 // Section 3.2 abort() method steps.
291 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#widl-SourceBuffer-abort-void
292 // 1. If this object has been removed from the sourceBuffers attribute of the parent media source
293 // then throw an INVALID_STATE_ERR exception and abort these steps.
294 // 2. If the readyState attribute of the parent media source is not in the "open" state
295 // then throw an INVALID_STATE_ERR exception and abort these steps.
296 if (isRemoved() || !m_source->isOpen()) {
297 ec = INVALID_STATE_ERR;
301 // 3. If the sourceBuffer.updating attribute equals true, then run the following steps: ...
304 // 4. Run the reset parser state algorithm.
307 // 5. Set appendWindowStart to the presentation start time.
308 m_appendWindowStart = MediaTime::zeroTime();
310 // 6. Set appendWindowEnd to positive Infinity.
311 m_appendWindowEnd = MediaTime::positiveInfiniteTime();
314 void SourceBuffer::remove(double start, double end, ExceptionCode& ec)
316 remove(MediaTime::createWithDouble(start), MediaTime::createWithDouble(end), ec);
319 void SourceBuffer::remove(const MediaTime& start, const MediaTime& end, ExceptionCode& ec)
321 LOG(MediaSource, "SourceBuffer::remove(%p) - start(%lf), end(%lf)", this, start.toDouble(), end.toDouble());
323 // Section 3.2 remove() method steps.
324 // 1. If duration equals NaN, then throw an InvalidAccessError exception and abort these steps.
325 // 2. If start is negative or greater than duration, then throw an InvalidAccessError exception and abort these steps.
326 // 3. If end is less than or equal to start, then throw an InvalidAccessError exception and abort these steps.
328 // FIXME: reorder/revisit this section once <https://www.w3.org/Bugs/Public/show_bug.cgi?id=27857> got resolved
329 // as it seems wrong to check mediaSource duration before checking isRemoved().
330 if ((m_source && m_source->duration().isInvalid())
331 || start < MediaTime::zeroTime() || (m_source && start > m_source->duration())
333 ec = INVALID_ACCESS_ERR;
337 // 4. If this object has been removed from the sourceBuffers attribute of the parent media source then throw an
338 // InvalidStateError exception and abort these steps.
339 // 5. If the updating attribute equals true, then throw an InvalidStateError exception and abort these steps.
340 if (isRemoved() || m_updating) {
341 ec = INVALID_STATE_ERR;
345 // 6. If the readyState attribute of the parent media source is in the "ended" state then run the following steps:
346 // 6.1. Set the readyState attribute of the parent media source to "open"
347 // 6.2. Queue a task to fire a simple event named sourceopen at the parent media source .
348 m_source->openIfInEndedState();
350 // 7. Run the range removal algorithm with start and end as the start and end of the removal range.
351 rangeRemoval(start, end);
354 void SourceBuffer::rangeRemoval(const MediaTime& start, const MediaTime& end)
356 // 3.5.7 Range Removal
357 // https://rawgit.com/w3c/media-source/7bbe4aa33c61ec025bc7acbd80354110f6a000f9/media-source.html#sourcebuffer-range-removal
358 // 1. Let start equal the starting presentation timestamp for the removal range.
359 // 2. Let end equal the end presentation timestamp for the removal range.
360 // 3. Set the updating attribute to true.
363 // 4. Queue a task to fire a simple event named updatestart at this SourceBuffer object.
364 scheduleEvent(eventNames().updatestartEvent);
366 // 5. Return control to the caller and run the rest of the steps asynchronously.
367 m_pendingRemoveStart = start;
368 m_pendingRemoveEnd = end;
369 m_removeTimer.startOneShot(0);
372 void SourceBuffer::abortIfUpdating()
374 // Section 3.2 abort() method step 3 substeps.
375 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#widl-SourceBuffer-abort-void
380 // 3.1. Abort the buffer append and stream append loop algorithms if they are running.
381 m_appendBufferTimer.stop();
382 m_pendingAppendData.clear();
384 m_removeTimer.stop();
385 m_pendingRemoveStart = MediaTime::invalidTime();
386 m_pendingRemoveEnd = MediaTime::invalidTime();
388 // 3.2. Set the updating attribute to false.
391 // 3.3. Queue a task to fire a simple event named abort at this SourceBuffer object.
392 scheduleEvent(eventNames().abortEvent);
394 // 3.4. Queue a task to fire a simple event named updateend at this SourceBuffer object.
395 scheduleEvent(eventNames().updateendEvent);
398 void SourceBuffer::removedFromMediaSource()
405 for (auto& trackBufferPair : m_trackBufferMap.values()) {
406 trackBufferPair.samples.clear();
407 trackBufferPair.decodeQueue.clear();
410 m_private->removedFromMediaSource();
414 void SourceBuffer::seekToTime(const MediaTime& time)
416 LOG(MediaSource, "SourceBuffer::seekToTime(%p) - time(%s)", this, toString(time).utf8().data());
418 for (auto& trackBufferPair : m_trackBufferMap) {
419 TrackBuffer& trackBuffer = trackBufferPair.value;
420 const AtomicString& trackID = trackBufferPair.key;
422 trackBuffer.needsReenqueueing = true;
423 reenqueueMediaForTime(trackBuffer, trackID, time);
427 MediaTime SourceBuffer::sourceBufferPrivateFastSeekTimeForMediaTime(SourceBufferPrivate*, const MediaTime& targetTime, const MediaTime& negativeThreshold, const MediaTime& positiveThreshold)
429 MediaTime seekTime = targetTime;
430 MediaTime lowerBoundTime = targetTime - negativeThreshold;
431 MediaTime upperBoundTime = targetTime + positiveThreshold;
433 for (auto& trackBuffer : m_trackBufferMap.values()) {
434 // Find the sample which contains the target time time.
435 auto futureSyncSampleIterator = trackBuffer.samples.decodeOrder().findSyncSampleAfterPresentationTime(targetTime, positiveThreshold);
436 auto pastSyncSampleIterator = trackBuffer.samples.decodeOrder().findSyncSamplePriorToPresentationTime(targetTime, negativeThreshold);
437 auto upperBound = trackBuffer.samples.decodeOrder().end();
438 auto lowerBound = trackBuffer.samples.decodeOrder().rend();
440 if (futureSyncSampleIterator == upperBound && pastSyncSampleIterator == lowerBound)
443 MediaTime futureSeekTime = MediaTime::positiveInfiniteTime();
444 if (futureSyncSampleIterator != upperBound) {
445 RefPtr<MediaSample>& sample = futureSyncSampleIterator->second;
446 futureSeekTime = sample->presentationTime();
449 MediaTime pastSeekTime = MediaTime::negativeInfiniteTime();
450 if (pastSyncSampleIterator != lowerBound) {
451 RefPtr<MediaSample>& sample = pastSyncSampleIterator->second;
452 pastSeekTime = sample->presentationTime();
455 MediaTime trackSeekTime = abs(targetTime - futureSeekTime) < abs(targetTime - pastSeekTime) ? futureSeekTime : pastSeekTime;
456 if (abs(targetTime - trackSeekTime) > abs(targetTime - seekTime))
457 seekTime = trackSeekTime;
463 bool SourceBuffer::hasPendingActivity() const
465 return m_source || m_asyncEventQueue.hasPendingEvents();
468 void SourceBuffer::stop()
470 m_appendBufferTimer.stop();
471 m_removeTimer.stop();
474 bool SourceBuffer::canSuspendForDocumentSuspension() const
476 return !hasPendingActivity();
479 const char* SourceBuffer::activeDOMObjectName() const
481 return "SourceBuffer";
484 bool SourceBuffer::isRemoved() const
489 void SourceBuffer::scheduleEvent(const AtomicString& eventName)
491 auto event = Event::create(eventName, false, false);
492 event->setTarget(this);
494 m_asyncEventQueue.enqueueEvent(WTFMove(event));
497 void SourceBuffer::appendBufferInternal(unsigned char* data, unsigned size, ExceptionCode& ec)
499 // Section 3.2 appendBuffer()
500 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#widl-SourceBuffer-appendBuffer-void-ArrayBufferView-data
502 // Step 1 is enforced by the caller.
503 // 2. Run the prepare append algorithm.
504 // Section 3.5.4 Prepare AppendAlgorithm
506 // 1. If the SourceBuffer has been removed from the sourceBuffers attribute of the parent media source
507 // then throw an INVALID_STATE_ERR exception and abort these steps.
508 // 2. If the updating attribute equals true, then throw an INVALID_STATE_ERR exception and abort these steps.
509 if (isRemoved() || m_updating) {
510 ec = INVALID_STATE_ERR;
514 // 3. If the readyState attribute of the parent media source is in the "ended" state then run the following steps:
515 // 3.1. Set the readyState attribute of the parent media source to "open"
516 // 3.2. Queue a task to fire a simple event named sourceopen at the parent media source .
517 m_source->openIfInEndedState();
519 // 4. Run the coded frame eviction algorithm.
520 evictCodedFrames(size);
522 // FIXME: enable this code when MSE libraries have been updated to support it.
524 // 5. If the buffer full flag equals true, then throw a QUOTA_EXCEEDED_ERR exception and abort these step.
526 LOG(MediaSource, "SourceBuffer::appendBufferInternal(%p) - buffer full, failing with QUOTA_EXCEEDED_ERR error", this);
527 ec = QUOTA_EXCEEDED_ERR;
532 // NOTE: Return to 3.2 appendBuffer()
533 // 3. Add data to the end of the input buffer.
534 m_pendingAppendData.append(data, size);
536 // 4. Set the updating attribute to true.
539 // 5. Queue a task to fire a simple event named updatestart at this SourceBuffer object.
540 scheduleEvent(eventNames().updatestartEvent);
542 // 6. Asynchronously run the buffer append algorithm.
543 m_appendBufferTimer.startOneShot(0);
545 reportExtraMemoryAllocated();
548 void SourceBuffer::appendBufferTimerFired()
555 // Section 3.5.5 Buffer Append Algorithm
556 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#sourcebuffer-buffer-append
558 // 1. Run the segment parser loop algorithm.
559 size_t appendSize = m_pendingAppendData.size();
561 // Resize buffer for 0 byte appends so we always have a valid pointer.
562 // We need to convey all appends, even 0 byte ones to |m_private| so
563 // that it can clear its end of stream state if necessary.
564 m_pendingAppendData.resize(1);
567 // Section 3.5.1 Segment Parser Loop
568 // https://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-source.html#sourcebuffer-segment-parser-loop
569 // When the segment parser loop algorithm is invoked, run the following steps:
571 // 1. Loop Top: If the input buffer is empty, then jump to the need more data step below.
572 if (!m_pendingAppendData.size()) {
573 sourceBufferPrivateAppendComplete(&m_private.get(), AppendSucceeded);
577 m_private->append(m_pendingAppendData.data(), appendSize);
578 m_pendingAppendData.clear();
581 void SourceBuffer::sourceBufferPrivateAppendComplete(SourceBufferPrivate*, AppendResult result)
586 // Section 3.5.5 Buffer Append Algorithm, ctd.
587 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#sourcebuffer-buffer-append
589 // 2. If the input buffer contains bytes that violate the SourceBuffer byte stream format specification,
590 // then run the append error algorithm with the decode error parameter set to true and abort this algorithm.
591 if (result == ParsingFailed) {
592 LOG(MediaSource, "SourceBuffer::sourceBufferPrivateAppendComplete(%p) - result = ParsingFailed", this);
597 // NOTE: Steps 3 - 6 enforced by sourceBufferPrivateDidReceiveInitializationSegment() and
598 // sourceBufferPrivateDidReceiveSample below.
600 // 7. Need more data: Return control to the calling algorithm.
602 // NOTE: return to Section 3.5.5
603 // 2.If the segment parser loop algorithm in the previous step was aborted, then abort this algorithm.
604 if (result != AppendSucceeded)
607 // 3. Set the updating attribute to false.
610 // 4. Queue a task to fire a simple event named update at this SourceBuffer object.
611 scheduleEvent(eventNames().updateEvent);
613 // 5. Queue a task to fire a simple event named updateend at this SourceBuffer object.
614 scheduleEvent(eventNames().updateendEvent);
617 m_source->monitorSourceBuffers();
619 MediaTime currentMediaTime = m_source->currentTime();
620 for (auto& trackBufferPair : m_trackBufferMap) {
621 TrackBuffer& trackBuffer = trackBufferPair.value;
622 const AtomicString& trackID = trackBufferPair.key;
624 if (trackBuffer.needsReenqueueing) {
625 LOG(MediaSource, "SourceBuffer::sourceBufferPrivateAppendComplete(%p) - reenqueuing at time (%s)", this, toString(currentMediaTime).utf8().data());
626 reenqueueMediaForTime(trackBuffer, trackID, currentMediaTime);
628 provideMediaData(trackBuffer, trackID);
631 reportExtraMemoryAllocated();
632 if (extraMemoryCost() > this->maximumBufferSize())
635 LOG(Media, "SourceBuffer::sourceBufferPrivateAppendComplete(%p) - buffered = %s", this, toString(m_buffered->ranges()).utf8().data());
638 void SourceBuffer::sourceBufferPrivateDidReceiveRenderingError(SourceBufferPrivate*, int error)
644 LOG(MediaSource, "SourceBuffer::sourceBufferPrivateDidReceiveRenderingError(%p) - result = %i", this, error);
647 m_source->streamEndedWithError(MediaSource::EndOfStreamError::Decode);
650 static bool decodeTimeComparator(const PresentationOrderSampleMap::MapType::value_type& a, const PresentationOrderSampleMap::MapType::value_type& b)
652 return a.second->decodeTime() < b.second->decodeTime();
655 static PlatformTimeRanges removeSamplesFromTrackBuffer(const DecodeOrderSampleMap::MapType& samples, SourceBuffer::TrackBuffer& trackBuffer, const SourceBuffer* buffer, const char* logPrefix)
658 MediaTime earliestSample = MediaTime::positiveInfiniteTime();
659 MediaTime latestSample = MediaTime::zeroTime();
660 size_t bytesRemoved = 0;
662 UNUSED_PARAM(logPrefix);
663 UNUSED_PARAM(buffer);
666 PlatformTimeRanges erasedRanges;
667 for (auto sampleIt : samples) {
668 const DecodeOrderSampleMap::KeyType& decodeKey = sampleIt.first;
670 size_t startBufferSize = trackBuffer.samples.sizeInBytes();
673 RefPtr<MediaSample>& sample = sampleIt.second;
674 LOG(MediaSource, "SourceBuffer::%s(%p) - removing sample(%s)", logPrefix, buffer, toString(*sampleIt.second).utf8().data());
676 // Remove the erased samples from the TrackBuffer sample map.
677 trackBuffer.samples.removeSample(sample.get());
679 // Also remove the erased samples from the TrackBuffer decodeQueue.
680 trackBuffer.decodeQueue.erase(decodeKey);
682 auto startTime = sample->presentationTime();
683 auto endTime = startTime + sample->duration();
684 erasedRanges.add(startTime, endTime);
687 bytesRemoved += startBufferSize - trackBuffer.samples.sizeInBytes();
688 if (startTime < earliestSample)
689 earliestSample = startTime;
690 if (endTime > latestSample)
691 latestSample = endTime;
695 // Because we may have added artificial padding in the buffered ranges when adding samples, we may
696 // need to remove that padding when removing those same samples. Walk over the erased ranges looking
697 // for unbuffered areas and expand erasedRanges to encompass those areas.
698 PlatformTimeRanges additionalErasedRanges;
699 for (unsigned i = 0; i < erasedRanges.length(); ++i) {
700 auto erasedStart = erasedRanges.start(i);
701 auto erasedEnd = erasedRanges.end(i);
702 auto startIterator = trackBuffer.samples.presentationOrder().reverseFindSampleBeforePresentationTime(erasedStart);
703 if (startIterator == trackBuffer.samples.presentationOrder().rend())
704 additionalErasedRanges.add(MediaTime::zeroTime(), erasedStart);
706 auto& previousSample = *startIterator->second;
707 if (previousSample.presentationTime() + previousSample.duration() < erasedStart)
708 additionalErasedRanges.add(previousSample.presentationTime() + previousSample.duration(), erasedStart);
711 auto endIterator = trackBuffer.samples.presentationOrder().findSampleOnOrAfterPresentationTime(erasedEnd);
712 if (endIterator == trackBuffer.samples.presentationOrder().end())
713 additionalErasedRanges.add(erasedEnd, MediaTime::positiveInfiniteTime());
715 auto& nextSample = *endIterator->second;
716 if (nextSample.presentationTime() > erasedEnd)
717 additionalErasedRanges.add(erasedEnd, nextSample.presentationTime());
720 if (additionalErasedRanges.length())
721 erasedRanges.unionWith(additionalErasedRanges);
725 LOG(MediaSource, "SourceBuffer::%s(%p) removed %zu bytes, start(%lf), end(%lf)", logPrefix, buffer, bytesRemoved, earliestSample.toDouble(), latestSample.toDouble());
731 void SourceBuffer::removeCodedFrames(const MediaTime& start, const MediaTime& end)
733 LOG(MediaSource, "SourceBuffer::removeCodedFrames(%p) - start(%s), end(%s)", this, toString(start).utf8().data(), toString(end).utf8().data());
735 // 3.5.9 Coded Frame Removal Algorithm
736 // https://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-source.html#sourcebuffer-coded-frame-removal
738 // 1. Let start be the starting presentation timestamp for the removal range.
739 MediaTime durationMediaTime = m_source->duration();
740 MediaTime currentMediaTime = m_source->currentTime();
742 // 2. Let end be the end presentation timestamp for the removal range.
743 // 3. For each track buffer in this source buffer, run the following steps:
744 for (auto& iter : m_trackBufferMap) {
745 TrackBuffer& trackBuffer = iter.value;
747 // 3.1. Let remove end timestamp be the current value of duration
748 // 3.2 If this track buffer has a random access point timestamp that is greater than or equal to end, then update
749 // remove end timestamp to that random access point timestamp.
750 // NOTE: findSyncSampleAfterPresentationTime will return the next sync sample on or after the presentation time
751 // or decodeOrder().end() if no sync sample exists after that presentation time.
752 DecodeOrderSampleMap::iterator removeDecodeEnd = trackBuffer.samples.decodeOrder().findSyncSampleAfterPresentationTime(end);
753 PresentationOrderSampleMap::iterator removePresentationEnd;
754 if (removeDecodeEnd == trackBuffer.samples.decodeOrder().end())
755 removePresentationEnd = trackBuffer.samples.presentationOrder().end();
757 removePresentationEnd = trackBuffer.samples.presentationOrder().findSampleWithPresentationTime(removeDecodeEnd->second->presentationTime());
759 PresentationOrderSampleMap::iterator removePresentationStart = trackBuffer.samples.presentationOrder().findSampleOnOrAfterPresentationTime(start);
760 if (removePresentationStart == removePresentationEnd)
763 // 3.3 Remove all media data, from this track buffer, that contain starting timestamps greater than or equal to
764 // start and less than the remove end timestamp.
765 // NOTE: frames must be removed in decode order, so that all dependant frames between the frame to be removed
766 // and the next sync sample frame are removed. But we must start from the first sample in decode order, not
767 // presentation order.
768 PresentationOrderSampleMap::iterator minDecodeTimeIter = std::min_element(removePresentationStart, removePresentationEnd, decodeTimeComparator);
769 DecodeOrderSampleMap::KeyType decodeKey(minDecodeTimeIter->second->decodeTime(), minDecodeTimeIter->second->presentationTime());
770 DecodeOrderSampleMap::iterator removeDecodeStart = trackBuffer.samples.decodeOrder().findSampleWithDecodeKey(decodeKey);
772 DecodeOrderSampleMap::MapType erasedSamples(removeDecodeStart, removeDecodeEnd);
773 PlatformTimeRanges erasedRanges = removeSamplesFromTrackBuffer(erasedSamples, trackBuffer, this, "removeCodedFrames");
775 // Only force the TrackBuffer to re-enqueue if the removed ranges overlap with enqueued and possibly
776 // not yet displayed samples.
777 if (currentMediaTime < trackBuffer.lastEnqueuedPresentationTime) {
778 PlatformTimeRanges possiblyEnqueuedRanges(currentMediaTime, trackBuffer.lastEnqueuedPresentationTime);
779 possiblyEnqueuedRanges.intersectWith(erasedRanges);
780 if (possiblyEnqueuedRanges.length())
781 trackBuffer.needsReenqueueing = true;
784 erasedRanges.invert();
785 m_buffered->ranges().intersectWith(erasedRanges);
786 setBufferedDirty(true);
788 // 3.4 If this object is in activeSourceBuffers, the current playback position is greater than or equal to start
789 // and less than the remove end timestamp, and HTMLMediaElement.readyState is greater than HAVE_METADATA, then set
790 // the HTMLMediaElement.readyState attribute to HAVE_METADATA and stall playback.
791 if (m_active && currentMediaTime >= start && currentMediaTime < end && m_private->readyState() > MediaPlayer::HaveMetadata)
792 m_private->setReadyState(MediaPlayer::HaveMetadata);
795 // 4. If buffer full flag equals true and this object is ready to accept more bytes, then set the buffer full flag to false.
798 LOG(Media, "SourceBuffer::removeCodedFrames(%p) - buffered = %s", this, toString(m_buffered->ranges()).utf8().data());
801 void SourceBuffer::removeTimerFired()
804 ASSERT(m_pendingRemoveStart.isValid());
805 ASSERT(m_pendingRemoveStart < m_pendingRemoveEnd);
807 // Section 3.5.7 Range Removal
808 // http://w3c.github.io/media-source/#sourcebuffer-range-removal
810 // 6. Run the coded frame removal algorithm with start and end as the start and end of the removal range.
811 removeCodedFrames(m_pendingRemoveStart, m_pendingRemoveEnd);
813 // 7. Set the updating attribute to false.
815 m_pendingRemoveStart = MediaTime::invalidTime();
816 m_pendingRemoveEnd = MediaTime::invalidTime();
818 // 8. Queue a task to fire a simple event named update at this SourceBuffer object.
819 scheduleEvent(eventNames().updateEvent);
821 // 9. Queue a task to fire a simple event named updateend at this SourceBuffer object.
822 scheduleEvent(eventNames().updateendEvent);
825 void SourceBuffer::evictCodedFrames(size_t newDataSize)
827 // 3.5.13 Coded Frame Eviction Algorithm
828 // http://www.w3.org/TR/media-source/#sourcebuffer-coded-frame-eviction
833 // This algorithm is run to free up space in this source buffer when new data is appended.
834 // 1. Let new data equal the data that is about to be appended to this SourceBuffer.
835 // 2. If the buffer full flag equals false, then abort these steps.
839 size_t maximumBufferSize = this->maximumBufferSize();
841 // 3. Let removal ranges equal a list of presentation time ranges that can be evicted from
842 // the presentation to make room for the new data.
844 // NOTE: begin by removing data from the beginning of the buffered ranges, 30 seconds at
845 // a time, up to 30 seconds before currentTime.
846 MediaTime thirtySeconds = MediaTime(30, 1);
847 MediaTime currentTime = m_source->currentTime();
848 MediaTime maximumRangeEnd = currentTime - thirtySeconds;
851 LOG(MediaSource, "SourceBuffer::evictCodedFrames(%p) - currentTime = %lf, require %zu bytes, maximum buffer size is %zu", this, m_source->currentTime().toDouble(), extraMemoryCost() + newDataSize, maximumBufferSize);
852 size_t initialBufferedSize = extraMemoryCost();
855 MediaTime rangeStart = MediaTime::zeroTime();
856 MediaTime rangeEnd = rangeStart + thirtySeconds;
857 while (rangeStart < maximumRangeEnd) {
858 // 4. For each range in removal ranges, run the coded frame removal algorithm with start and
859 // end equal to the removal range start and end timestamp respectively.
860 removeCodedFrames(rangeStart, std::min(rangeEnd, maximumRangeEnd));
861 if (extraMemoryCost() + newDataSize < maximumBufferSize) {
862 m_bufferFull = false;
866 rangeStart += thirtySeconds;
867 rangeEnd += thirtySeconds;
871 LOG(MediaSource, "SourceBuffer::evictCodedFrames(%p) - evicted %zu bytes", this, initialBufferedSize - extraMemoryCost());
875 // If there still isn't enough free space and there buffers in time ranges after the current range (ie. there is a gap after
876 // the current buffered range), delete 30 seconds at a time from duration back to the current time range or 30 seconds after
877 // currenTime whichever we hit first.
878 auto buffered = m_buffered->ranges();
879 size_t currentTimeRange = buffered.find(currentTime);
880 if (currentTimeRange == notFound || currentTimeRange == buffered.length() - 1) {
881 LOG(MediaSource, "SourceBuffer::evictCodedFrames(%p) - evicted %zu bytes but FAILED to free enough", this, initialBufferedSize - extraMemoryCost());
885 MediaTime minimumRangeStart = currentTime + thirtySeconds;
887 rangeEnd = m_source->duration();
888 rangeStart = rangeEnd - thirtySeconds;
889 while (rangeStart > minimumRangeStart) {
891 // Do not evict data from the time range that contains currentTime.
892 size_t startTimeRange = buffered.find(rangeStart);
893 if (startTimeRange == currentTimeRange) {
894 size_t endTimeRange = buffered.find(rangeEnd);
895 if (endTimeRange == currentTimeRange)
898 rangeEnd = buffered.start(endTimeRange);
901 // 4. For each range in removal ranges, run the coded frame removal algorithm with start and
902 // end equal to the removal range start and end timestamp respectively.
903 removeCodedFrames(std::max(minimumRangeStart, rangeStart), rangeEnd);
904 if (extraMemoryCost() + newDataSize < maximumBufferSize) {
905 m_bufferFull = false;
909 rangeStart -= thirtySeconds;
910 rangeEnd -= thirtySeconds;
913 LOG(MediaSource, "SourceBuffer::evictCodedFrames(%p) - evicted %zu bytes%s", this, initialBufferedSize - extraMemoryCost(), m_bufferFull ? "" : " but FAILED to free enough");
916 size_t SourceBuffer::maximumBufferSize() const
921 HTMLMediaElement* element = m_source->mediaElement();
925 return element->maximumSourceBufferSize(*this);
928 VideoTrackList* SourceBuffer::videoTracks()
930 if (!m_source || !m_source->mediaElement())
934 m_videoTracks = VideoTrackList::create(m_source->mediaElement(), ActiveDOMObject::scriptExecutionContext());
936 return m_videoTracks.get();
939 AudioTrackList* SourceBuffer::audioTracks()
941 if (!m_source || !m_source->mediaElement())
945 m_audioTracks = AudioTrackList::create(m_source->mediaElement(), ActiveDOMObject::scriptExecutionContext());
947 return m_audioTracks.get();
950 TextTrackList* SourceBuffer::textTracks()
952 if (!m_source || !m_source->mediaElement())
956 m_textTracks = TextTrackList::create(m_source->mediaElement(), ActiveDOMObject::scriptExecutionContext());
958 return m_textTracks.get();
961 void SourceBuffer::setActive(bool active)
963 if (m_active == active)
967 m_private->setActive(active);
969 m_source->sourceBufferDidChangeActiveState(*this, active);
972 void SourceBuffer::sourceBufferPrivateDidReceiveInitializationSegment(SourceBufferPrivate*, const InitializationSegment& segment)
977 LOG(MediaSource, "SourceBuffer::sourceBufferPrivateDidReceiveInitializationSegment(%p)", this);
979 // 3.5.8 Initialization Segment Received (ctd)
980 // https://rawgit.com/w3c/media-source/c3ad59c7a370d04430969ba73d18dc9bcde57a33/index.html#sourcebuffer-init-segment-received [Editor's Draft 09 January 2015]
982 // 1. Update the duration attribute if it currently equals NaN:
983 if (m_source->duration().isInvalid()) {
984 // ↳ If the initialization segment contains a duration:
985 // Run the duration change algorithm with new duration set to the duration in the initialization segment.
987 // Run the duration change algorithm with new duration set to positive Infinity.
988 MediaTime newDuration = segment.duration.isValid() ? segment.duration : MediaTime::positiveInfiniteTime();
989 m_source->setDurationInternal(newDuration);
992 // 2. If the initialization segment has no audio, video, or text tracks, then run the append error algorithm
993 // with the decode error parameter set to true and abort these steps.
994 if (segment.audioTracks.isEmpty() && segment.videoTracks.isEmpty() && segment.textTracks.isEmpty()) {
999 // 3. If the first initialization segment flag is true, then run the following steps:
1000 if (m_receivedFirstInitializationSegment) {
1002 // 3.1. Verify the following properties. If any of the checks fail then run the append error algorithm
1003 // with the decode error parameter set to true and abort these steps.
1004 if (!validateInitializationSegment(segment)) {
1008 // 3.2 Add the appropriate track descriptions from this initialization segment to each of the track buffers.
1009 ASSERT(segment.audioTracks.size() == audioTracks()->length());
1010 for (auto& audioTrackInfo : segment.audioTracks) {
1011 if (audioTracks()->length() == 1) {
1012 audioTracks()->item(0)->setPrivate(audioTrackInfo.track);
1016 auto audioTrack = audioTracks()->getTrackById(audioTrackInfo.track->id());
1018 audioTrack->setPrivate(audioTrackInfo.track);
1021 ASSERT(segment.videoTracks.size() == videoTracks()->length());
1022 for (auto& videoTrackInfo : segment.videoTracks) {
1023 if (videoTracks()->length() == 1) {
1024 videoTracks()->item(0)->setPrivate(videoTrackInfo.track);
1028 auto videoTrack = videoTracks()->getTrackById(videoTrackInfo.track->id());
1030 videoTrack->setPrivate(videoTrackInfo.track);
1033 ASSERT(segment.textTracks.size() == textTracks()->length());
1034 for (auto& textTrackInfo : segment.textTracks) {
1035 if (textTracks()->length() == 1) {
1036 downcast<InbandTextTrack>(*textTracks()->item(0)).setPrivate(textTrackInfo.track);
1040 auto textTrack = textTracks()->getTrackById(textTrackInfo.track->id());
1042 downcast<InbandTextTrack>(*textTrack).setPrivate(textTrackInfo.track);
1045 // 3.3 Set the need random access point flag on all track buffers to true.
1046 for (auto& trackBuffer : m_trackBufferMap.values())
1047 trackBuffer.needRandomAccessFlag = true;
1050 // 4. Let active track flag equal false.
1051 bool activeTrackFlag = false;
1053 // 5. If the first initialization segment flag is false, then run the following steps:
1054 if (!m_receivedFirstInitializationSegment) {
1055 // 5.1 If the initialization segment contains tracks with codecs the user agent does not support,
1056 // then run the append error algorithm with the decode error parameter set to true and abort these steps.
1057 // NOTE: This check is the responsibility of the SourceBufferPrivate.
1059 // 5.2 For each audio track in the initialization segment, run following steps:
1060 for (auto& audioTrackInfo : segment.audioTracks) {
1061 AudioTrackPrivate* audioTrackPrivate = audioTrackInfo.track.get();
1063 // FIXME: Implement steps 5.2.1-5.2.8.1 as per Editor's Draft 09 January 2015, and reorder this
1064 // 5.2.1 Let new audio track be a new AudioTrack object.
1065 // 5.2.2 Generate a unique ID and assign it to the id property on new video track.
1066 auto newAudioTrack = AudioTrack::create(this, audioTrackPrivate);
1067 newAudioTrack->setSourceBuffer(this);
1069 // 5.2.3 If audioTracks.length equals 0, then run the following steps:
1070 if (!audioTracks()->length()) {
1071 // 5.2.3.1 Set the enabled property on new audio track to true.
1072 newAudioTrack->setEnabled(true);
1074 // 5.2.3.2 Set active track flag to true.
1075 activeTrackFlag = true;
1078 // 5.2.4 Add new audio track to the audioTracks attribute on this SourceBuffer object.
1079 // 5.2.5 Queue a task to fire a trusted event named addtrack, that does not bubble and is
1080 // not cancelable, and that uses the TrackEvent interface, at the AudioTrackList object
1081 // referenced by the audioTracks attribute on this SourceBuffer object.
1082 audioTracks()->append(newAudioTrack.copyRef());
1084 // 5.2.6 Add new audio track to the audioTracks attribute on the HTMLMediaElement.
1085 // 5.2.7 Queue a task to fire a trusted event named addtrack, that does not bubble and is
1086 // not cancelable, and that uses the TrackEvent interface, at the AudioTrackList object
1087 // referenced by the audioTracks attribute on the HTMLMediaElement.
1088 m_source->mediaElement()->audioTracks().append(newAudioTrack.copyRef());
1090 // 5.2.8 Create a new track buffer to store coded frames for this track.
1091 ASSERT(!m_trackBufferMap.contains(newAudioTrack->id()));
1092 TrackBuffer& trackBuffer = m_trackBufferMap.add(newAudioTrack->id(), TrackBuffer()).iterator->value;
1094 // 5.2.9 Add the track description for this track to the track buffer.
1095 trackBuffer.description = audioTrackInfo.description;
1097 m_audioCodecs.append(trackBuffer.description->codec());
1100 // 5.3 For each video track in the initialization segment, run following steps:
1101 for (auto& videoTrackInfo : segment.videoTracks) {
1102 VideoTrackPrivate* videoTrackPrivate = videoTrackInfo.track.get();
1104 // FIXME: Implement steps 5.3.1-5.3.8.1 as per Editor's Draft 09 January 2015, and reorder this
1105 // 5.3.1 Let new video track be a new VideoTrack object.
1106 // 5.3.2 Generate a unique ID and assign it to the id property on new video track.
1107 auto newVideoTrack = VideoTrack::create(this, videoTrackPrivate);
1108 newVideoTrack->setSourceBuffer(this);
1110 // 5.3.3 If videoTracks.length equals 0, then run the following steps:
1111 if (!videoTracks()->length()) {
1112 // 5.3.3.1 Set the selected property on new video track to true.
1113 newVideoTrack->setSelected(true);
1115 // 5.3.3.2 Set active track flag to true.
1116 activeTrackFlag = true;
1119 // 5.3.4 Add new video track to the videoTracks attribute on this SourceBuffer object.
1120 // 5.3.5 Queue a task to fire a trusted event named addtrack, that does not bubble and is
1121 // not cancelable, and that uses the TrackEvent interface, at the VideoTrackList object
1122 // referenced by the videoTracks attribute on this SourceBuffer object.
1123 videoTracks()->append(newVideoTrack.copyRef());
1125 // 5.3.6 Add new video track to the videoTracks attribute on the HTMLMediaElement.
1126 // 5.3.7 Queue a task to fire a trusted event named addtrack, that does not bubble and is
1127 // not cancelable, and that uses the TrackEvent interface, at the VideoTrackList object
1128 // referenced by the videoTracks attribute on the HTMLMediaElement.
1129 m_source->mediaElement()->videoTracks().append(newVideoTrack.copyRef());
1131 // 5.3.8 Create a new track buffer to store coded frames for this track.
1132 ASSERT(!m_trackBufferMap.contains(newVideoTrack->id()));
1133 TrackBuffer& trackBuffer = m_trackBufferMap.add(newVideoTrack->id(), TrackBuffer()).iterator->value;
1135 // 5.3.9 Add the track description for this track to the track buffer.
1136 trackBuffer.description = videoTrackInfo.description;
1138 m_videoCodecs.append(trackBuffer.description->codec());
1141 // 5.4 For each text track in the initialization segment, run following steps:
1142 for (auto& textTrackInfo : segment.textTracks) {
1143 InbandTextTrackPrivate* textTrackPrivate = textTrackInfo.track.get();
1145 // FIXME: Implement steps 5.4.1-5.4.8.1 as per Editor's Draft 09 January 2015, and reorder this
1146 // 5.4.1 Let new text track be a new TextTrack object with its properties populated with the
1147 // appropriate information from the initialization segment.
1148 RefPtr<InbandTextTrack> newTextTrack = InbandTextTrack::create(scriptExecutionContext(), this, textTrackPrivate);
1150 // 5.4.2 If the mode property on new text track equals "showing" or "hidden", then set active
1151 // track flag to true.
1152 if (textTrackPrivate->mode() != InbandTextTrackPrivate::Disabled)
1153 activeTrackFlag = true;
1155 // 5.4.3 Add new text track to the textTracks attribute on this SourceBuffer object.
1156 // 5.4.4 Queue a task to fire a trusted event named addtrack, that does not bubble and is
1157 // not cancelable, and that uses the TrackEvent interface, at textTracks attribute on this
1158 // SourceBuffer object.
1159 textTracks()->append(*newTextTrack);
1161 // 5.4.5 Add new text track to the textTracks attribute on the HTMLMediaElement.
1162 // 5.4.6 Queue a task to fire a trusted event named addtrack, that does not bubble and is
1163 // not cancelable, and that uses the TrackEvent interface, at the TextTrackList object
1164 // referenced by the textTracks attribute on the HTMLMediaElement.
1165 m_source->mediaElement()->textTracks().append(newTextTrack.releaseNonNull());
1167 // 5.4.7 Create a new track buffer to store coded frames for this track.
1168 ASSERT(!m_trackBufferMap.contains(textTrackPrivate->id()));
1169 TrackBuffer& trackBuffer = m_trackBufferMap.add(textTrackPrivate->id(), TrackBuffer()).iterator->value;
1171 // 5.4.8 Add the track description for this track to the track buffer.
1172 trackBuffer.description = textTrackInfo.description;
1174 m_textCodecs.append(trackBuffer.description->codec());
1177 // 5.5 If active track flag equals true, then run the following steps:
1178 if (activeTrackFlag) {
1179 // 5.5.1 Add this SourceBuffer to activeSourceBuffers.
1180 // 5.5.2 Queue a task to fire a simple event named addsourcebuffer at activeSourceBuffers
1184 // 5.6 Set first initialization segment flag to true.
1185 m_receivedFirstInitializationSegment = true;
1188 // 6. If the HTMLMediaElement.readyState attribute is HAVE_NOTHING, then run the following steps:
1189 if (m_private->readyState() == MediaPlayer::HaveNothing) {
1190 // 6.1 If one or more objects in sourceBuffers have first initialization segment flag set to false, then abort these steps.
1191 for (auto& sourceBuffer : *m_source->sourceBuffers()) {
1192 if (!sourceBuffer->m_receivedFirstInitializationSegment)
1196 // 6.2 Set the HTMLMediaElement.readyState attribute to HAVE_METADATA.
1197 // 6.3 Queue a task to fire a simple event named loadedmetadata at the media element.
1198 m_private->setReadyState(MediaPlayer::HaveMetadata);
1201 // 7. If the active track flag equals true and the HTMLMediaElement.readyState
1202 // attribute is greater than HAVE_CURRENT_DATA, then set the HTMLMediaElement.readyState
1203 // attribute to HAVE_METADATA.
1204 if (activeTrackFlag && m_private->readyState() > MediaPlayer::HaveCurrentData)
1205 m_private->setReadyState(MediaPlayer::HaveMetadata);
1208 bool SourceBuffer::validateInitializationSegment(const InitializationSegment& segment)
1210 // FIXME: ordering of all 3.5.X (X>=7) functions needs to be updated to post-[24 July 2014 Editor's Draft] version
1211 // 3.5.8 Initialization Segment Received (ctd)
1212 // https://rawgit.com/w3c/media-source/c3ad59c7a370d04430969ba73d18dc9bcde57a33/index.html#sourcebuffer-init-segment-received [Editor's Draft 09 January 2015]
1214 // Note: those are checks from step 3.1
1215 // * The number of audio, video, and text tracks match what was in the first initialization segment.
1216 if (segment.audioTracks.size() != audioTracks()->length()
1217 || segment.videoTracks.size() != videoTracks()->length()
1218 || segment.textTracks.size() != textTracks()->length())
1221 // * The codecs for each track, match what was specified in the first initialization segment.
1222 for (auto& audioTrackInfo : segment.audioTracks) {
1223 if (!m_audioCodecs.contains(audioTrackInfo.description->codec()))
1227 for (auto& videoTrackInfo : segment.videoTracks) {
1228 if (!m_videoCodecs.contains(videoTrackInfo.description->codec()))
1232 for (auto& textTrackInfo : segment.textTracks) {
1233 if (!m_textCodecs.contains(textTrackInfo.description->codec()))
1237 // * If more than one track for a single type are present (ie 2 audio tracks), then the Track
1238 // IDs match the ones in the first initialization segment.
1239 if (segment.audioTracks.size() >= 2) {
1240 for (auto& audioTrackInfo : segment.audioTracks) {
1241 if (!m_trackBufferMap.contains(audioTrackInfo.track->id()))
1246 if (segment.videoTracks.size() >= 2) {
1247 for (auto& videoTrackInfo : segment.videoTracks) {
1248 if (!m_trackBufferMap.contains(videoTrackInfo.track->id()))
1253 if (segment.textTracks.size() >= 2) {
1254 for (auto& textTrackInfo : segment.videoTracks) {
1255 if (!m_trackBufferMap.contains(textTrackInfo.track->id()))
1263 class SampleLessThanComparator {
1265 bool operator()(std::pair<MediaTime, RefPtr<MediaSample>> value1, std::pair<MediaTime, RefPtr<MediaSample>> value2)
1267 return value1.first < value2.first;
1270 bool operator()(MediaTime value1, std::pair<MediaTime, RefPtr<MediaSample>> value2)
1272 return value1 < value2.first;
1275 bool operator()(std::pair<MediaTime, RefPtr<MediaSample>> value1, MediaTime value2)
1277 return value1.first < value2;
1281 void SourceBuffer::appendError(bool decodeErrorParam)
1283 // 3.5.3 Append Error Algorithm
1284 // https://rawgit.com/w3c/media-source/c3ad59c7a370d04430969ba73d18dc9bcde57a33/index.html#sourcebuffer-append-error [Editor's Draft 09 January 2015]
1287 // 1. Run the reset parser state algorithm.
1290 // 2. Set the updating attribute to false.
1293 // 3. Queue a task to fire a simple event named error at this SourceBuffer object.
1294 scheduleEvent(eventNames().errorEvent);
1296 // 4. Queue a task to fire a simple event named updateend at this SourceBuffer object.
1297 scheduleEvent(eventNames().updateendEvent);
1299 // 5. If decode error is true, then run the end of stream algorithm with the error parameter set to "decode".
1300 if (decodeErrorParam)
1301 m_source->streamEndedWithError(MediaSource::EndOfStreamError::Decode);
1304 void SourceBuffer::sourceBufferPrivateDidReceiveSample(SourceBufferPrivate*, MediaSample& sample)
1309 // 3.5.1 Segment Parser Loop
1310 // 6.1 If the first initialization segment received flag is false, then run the append error algorithm
1311 // with the decode error parameter set to true and abort this algorithm.
1312 // Note: current design makes SourceBuffer somehow ignorant of append state - it's more a thing
1313 // of SourceBufferPrivate. That's why this check can't really be done in appendInternal.
1314 // unless we force some kind of design with state machine switching.
1315 if (!m_receivedFirstInitializationSegment) {
1320 // 3.5.8 Coded Frame Processing
1321 // http://www.w3.org/TR/media-source/#sourcebuffer-coded-frame-processing
1323 // When complete coded frames have been parsed by the segment parser loop then the following steps
1325 // 1. For each coded frame in the media segment run the following steps:
1328 MediaTime presentationTimestamp;
1329 MediaTime decodeTimestamp;
1331 if (m_shouldGenerateTimestamps) {
1332 // ↳ If generate timestamps flag equals true:
1333 // 1. Let presentation timestamp equal 0.
1334 presentationTimestamp = MediaTime::zeroTime();
1336 // 2. Let decode timestamp equal 0.
1337 decodeTimestamp = MediaTime::zeroTime();
1340 // 1. Let presentation timestamp be a double precision floating point representation of
1341 // the coded frame's presentation timestamp in seconds.
1342 presentationTimestamp = sample.presentationTime();
1344 // 2. Let decode timestamp be a double precision floating point representation of the coded frame's
1345 // decode timestamp in seconds.
1346 decodeTimestamp = sample.decodeTime();
1349 // 1.2 Let frame duration be a double precision floating point representation of the coded frame's
1350 // duration in seconds.
1351 MediaTime frameDuration = sample.duration();
1353 // 1.3 If mode equals "sequence" and group start timestamp is set, then run the following steps:
1354 if (m_mode == AppendMode::Sequence && m_groupStartTimestamp.isValid()) {
1355 // 1.3.1 Set timestampOffset equal to group start timestamp - presentation timestamp.
1356 m_timestampOffset = m_groupStartTimestamp;
1358 // 1.3.2 Set group end timestamp equal to group start timestamp.
1359 m_groupEndTimestamp = m_groupStartTimestamp;
1361 // 1.3.3 Set the need random access point flag on all track buffers to true.
1362 for (auto& trackBuffer : m_trackBufferMap.values())
1363 trackBuffer.needRandomAccessFlag = true;
1365 // 1.3.4 Unset group start timestamp.
1366 m_groupStartTimestamp = MediaTime::invalidTime();
1369 // 1.4 If timestampOffset is not 0, then run the following steps:
1370 if (m_timestampOffset) {
1371 // 1.4.1 Add timestampOffset to the presentation timestamp.
1372 presentationTimestamp += m_timestampOffset;
1374 // 1.4.2 Add timestampOffset to the decode timestamp.
1375 decodeTimestamp += m_timestampOffset;
1378 // 1.5 Let track buffer equal the track buffer that the coded frame will be added to.
1379 AtomicString trackID = sample.trackID();
1380 auto it = m_trackBufferMap.find(trackID);
1381 if (it == m_trackBufferMap.end())
1382 it = m_trackBufferMap.add(trackID, TrackBuffer()).iterator;
1383 TrackBuffer& trackBuffer = it->value;
1385 // 1.6 ↳ If last decode timestamp for track buffer is set and decode timestamp is less than last
1386 // decode timestamp:
1388 // ↳ If last decode timestamp for track buffer is set and the difference between decode timestamp and
1389 // last decode timestamp is greater than 2 times last frame duration:
1390 if (trackBuffer.lastDecodeTimestamp.isValid() && (decodeTimestamp < trackBuffer.lastDecodeTimestamp
1391 || abs(decodeTimestamp - trackBuffer.lastDecodeTimestamp) > (trackBuffer.lastFrameDuration * 2))) {
1394 if (m_mode == AppendMode::Segments) {
1395 // ↳ If mode equals "segments":
1396 // Set group end timestamp to presentation timestamp.
1397 m_groupEndTimestamp = presentationTimestamp;
1399 // ↳ If mode equals "sequence":
1400 // Set group start timestamp equal to the group end timestamp.
1401 m_groupStartTimestamp = m_groupEndTimestamp;
1404 for (auto& trackBuffer : m_trackBufferMap.values()) {
1405 // 1.6.2 Unset the last decode timestamp on all track buffers.
1406 trackBuffer.lastDecodeTimestamp = MediaTime::invalidTime();
1407 // 1.6.3 Unset the last frame duration on all track buffers.
1408 trackBuffer.lastFrameDuration = MediaTime::invalidTime();
1409 // 1.6.4 Unset the highest presentation timestamp on all track buffers.
1410 trackBuffer.highestPresentationTimestamp = MediaTime::invalidTime();
1411 // 1.6.5 Set the need random access point flag on all track buffers to true.
1412 trackBuffer.needRandomAccessFlag = true;
1415 // 1.6.6 Jump to the Loop Top step above to restart processing of the current coded frame.
1419 if (m_mode == AppendMode::Sequence) {
1420 // Use the generated timestamps instead of the sample's timestamps.
1421 sample.setTimestamps(presentationTimestamp, decodeTimestamp);
1422 } else if (m_timestampOffset) {
1423 // Reflect the timestamp offset into the sample.
1424 sample.offsetTimestampsBy(m_timestampOffset);
1427 // 1.7 Let frame end timestamp equal the sum of presentation timestamp and frame duration.
1428 MediaTime frameEndTimestamp = presentationTimestamp + frameDuration;
1430 // 1.8 If presentation timestamp is less than appendWindowStart, then set the need random access
1431 // point flag to true, drop the coded frame, and jump to the top of the loop to start processing
1432 // the next coded frame.
1433 // 1.9 If frame end timestamp is greater than appendWindowEnd, then set the need random access
1434 // point flag to true, drop the coded frame, and jump to the top of the loop to start processing
1435 // the next coded frame.
1436 if (presentationTimestamp < m_appendWindowStart || frameEndTimestamp > m_appendWindowEnd) {
1437 trackBuffer.needRandomAccessFlag = true;
1443 // 1.10 If the decode timestamp is less than the presentation start time, then run the end of stream
1444 // algorithm with the error parameter set to "decode", and abort these steps.
1445 // NOTE: Until <https://www.w3.org/Bugs/Public/show_bug.cgi?id=27487> is resolved, we will only check
1446 // the presentation timestamp.
1447 MediaTime presentationStartTime = MediaTime::zeroTime();
1448 if (presentationTimestamp < presentationStartTime) {
1449 LOG(MediaSource, "SourceBuffer::sourceBufferPrivateDidReceiveSample(%p) - failing because presentationTimestamp < presentationStartTime", this);
1450 m_source->streamEndedWithError(MediaSource::EndOfStreamError::Decode);
1454 // 1.11 If the need random access point flag on track buffer equals true, then run the following steps:
1455 if (trackBuffer.needRandomAccessFlag) {
1456 // 1.11.1 If the coded frame is not a random access point, then drop the coded frame and jump
1457 // to the top of the loop to start processing the next coded frame.
1458 if (!sample.isSync()) {
1463 // 1.11.2 Set the need random access point flag on track buffer to false.
1464 trackBuffer.needRandomAccessFlag = false;
1467 // 1.12 Let spliced audio frame be an unset variable for holding audio splice information
1468 // 1.13 Let spliced timed text frame be an unset variable for holding timed text splice information
1469 // FIXME: Add support for sample splicing.
1471 SampleMap erasedSamples;
1472 MediaTime microsecond(1, 1000000);
1474 // 1.14 If last decode timestamp for track buffer is unset and presentation timestamp falls
1475 // falls within the presentation interval of a coded frame in track buffer, then run the
1477 if (trackBuffer.lastDecodeTimestamp.isInvalid()) {
1478 auto iter = trackBuffer.samples.presentationOrder().findSampleContainingPresentationTime(presentationTimestamp);
1479 if (iter != trackBuffer.samples.presentationOrder().end()) {
1480 // 1.14.1 Let overlapped frame be the coded frame in track buffer that matches the condition above.
1481 RefPtr<MediaSample> overlappedFrame = iter->second;
1483 // 1.14.2 If track buffer contains audio coded frames:
1484 // Run the audio splice frame algorithm and if a splice frame is returned, assign it to
1485 // spliced audio frame.
1486 // FIXME: Add support for sample splicing.
1488 // If track buffer contains video coded frames:
1489 if (trackBuffer.description->isVideo()) {
1490 // 1.14.2.1 Let overlapped frame presentation timestamp equal the presentation timestamp
1491 // of overlapped frame.
1492 MediaTime overlappedFramePresentationTimestamp = overlappedFrame->presentationTime();
1494 // 1.14.2.2 Let remove window timestamp equal overlapped frame presentation timestamp
1495 // plus 1 microsecond.
1496 MediaTime removeWindowTimestamp = overlappedFramePresentationTimestamp + microsecond;
1498 // 1.14.2.3 If the presentation timestamp is less than the remove window timestamp,
1499 // then remove overlapped frame and any coded frames that depend on it from track buffer.
1500 if (presentationTimestamp < removeWindowTimestamp)
1501 erasedSamples.addSample(*iter->second);
1504 // If track buffer contains timed text coded frames:
1505 // Run the text splice frame algorithm and if a splice frame is returned, assign it to spliced timed text frame.
1506 // FIXME: Add support for sample splicing.
1510 // 1.15 Remove existing coded frames in track buffer:
1511 // If highest presentation timestamp for track buffer is not set:
1512 if (trackBuffer.highestPresentationTimestamp.isInvalid()) {
1513 // Remove all coded frames from track buffer that have a presentation timestamp greater than or
1514 // equal to presentation timestamp and less than frame end timestamp.
1515 auto iter_pair = trackBuffer.samples.presentationOrder().findSamplesBetweenPresentationTimes(presentationTimestamp, frameEndTimestamp);
1516 if (iter_pair.first != trackBuffer.samples.presentationOrder().end())
1517 erasedSamples.addRange(iter_pair.first, iter_pair.second);
1520 // If highest presentation timestamp for track buffer is set and less than or equal to presentation timestamp
1521 if (trackBuffer.highestPresentationTimestamp.isValid() && trackBuffer.highestPresentationTimestamp <= presentationTimestamp) {
1522 // Remove all coded frames from track buffer that have a presentation timestamp greater than highest
1523 // presentation timestamp and less than or equal to frame end timestamp.
1525 // NOTE: Searching from the end of the trackBuffer will be vastly more efficient if the search range is
1526 // near the end of the buffered range. Use a linear-backwards search if the search range is within one
1527 // frame duration of the end:
1531 unsigned bufferedLength = m_buffered->ranges().length();
1532 if (!bufferedLength)
1535 MediaTime highestBufferedTime = m_buffered->ranges().maximumBufferedTime();
1537 PresentationOrderSampleMap::iterator_range range;
1538 if (highestBufferedTime - trackBuffer.highestPresentationTimestamp < trackBuffer.lastFrameDuration)
1539 range = trackBuffer.samples.presentationOrder().findSamplesWithinPresentationRangeFromEnd(trackBuffer.highestPresentationTimestamp, frameEndTimestamp);
1541 range = trackBuffer.samples.presentationOrder().findSamplesWithinPresentationRange(trackBuffer.highestPresentationTimestamp, frameEndTimestamp);
1543 if (range.first != trackBuffer.samples.presentationOrder().end())
1544 erasedSamples.addRange(range.first, range.second);
1548 // 1.16 Remove decoding dependencies of the coded frames removed in the previous step:
1549 DecodeOrderSampleMap::MapType dependentSamples;
1550 if (!erasedSamples.empty()) {
1551 // If detailed information about decoding dependencies is available:
1552 // FIXME: Add support for detailed dependency information
1554 // Otherwise: Remove all coded frames between the coded frames removed in the previous step
1555 // and the next random access point after those removed frames.
1556 auto firstDecodeIter = trackBuffer.samples.decodeOrder().findSampleWithDecodeKey(erasedSamples.decodeOrder().begin()->first);
1557 auto lastDecodeIter = trackBuffer.samples.decodeOrder().findSampleWithDecodeKey(erasedSamples.decodeOrder().rbegin()->first);
1558 auto nextSyncIter = trackBuffer.samples.decodeOrder().findSyncSampleAfterDecodeIterator(lastDecodeIter);
1559 dependentSamples.insert(firstDecodeIter, nextSyncIter);
1561 PlatformTimeRanges erasedRanges = removeSamplesFromTrackBuffer(dependentSamples, trackBuffer, this, "sourceBufferPrivateDidReceiveSample");
1563 // Only force the TrackBuffer to re-enqueue if the removed ranges overlap with enqueued and possibly
1564 // not yet displayed samples.
1565 MediaTime currentMediaTime = m_source->currentTime();
1566 if (currentMediaTime < trackBuffer.lastEnqueuedPresentationTime) {
1567 PlatformTimeRanges possiblyEnqueuedRanges(currentMediaTime, trackBuffer.lastEnqueuedPresentationTime);
1568 possiblyEnqueuedRanges.intersectWith(erasedRanges);
1569 if (possiblyEnqueuedRanges.length())
1570 trackBuffer.needsReenqueueing = true;
1573 erasedRanges.invert();
1574 m_buffered->ranges().intersectWith(erasedRanges);
1575 setBufferedDirty(true);
1578 // 1.17 If spliced audio frame is set:
1579 // Add spliced audio frame to the track buffer.
1580 // If spliced timed text frame is set:
1581 // Add spliced timed text frame to the track buffer.
1582 // FIXME: Add support for sample splicing.
1585 // Add the coded frame with the presentation timestamp, decode timestamp, and frame duration to the track buffer.
1586 trackBuffer.samples.addSample(sample);
1588 if (trackBuffer.lastEnqueuedDecodeEndTime.isInvalid() || decodeTimestamp >= trackBuffer.lastEnqueuedDecodeEndTime) {
1589 DecodeOrderSampleMap::KeyType decodeKey(decodeTimestamp, presentationTimestamp);
1590 trackBuffer.decodeQueue.insert(DecodeOrderSampleMap::MapType::value_type(decodeKey, &sample));
1593 // 1.18 Set last decode timestamp for track buffer to decode timestamp.
1594 trackBuffer.lastDecodeTimestamp = decodeTimestamp;
1596 // 1.19 Set last frame duration for track buffer to frame duration.
1597 trackBuffer.lastFrameDuration = frameDuration;
1599 // 1.20 If highest presentation timestamp for track buffer is unset or frame end timestamp is greater
1600 // than highest presentation timestamp, then set highest presentation timestamp for track buffer
1601 // to frame end timestamp.
1602 if (trackBuffer.highestPresentationTimestamp.isInvalid() || frameEndTimestamp > trackBuffer.highestPresentationTimestamp)
1603 trackBuffer.highestPresentationTimestamp = frameEndTimestamp;
1605 // 1.21 If frame end timestamp is greater than group end timestamp, then set group end timestamp equal
1606 // to frame end timestamp.
1607 if (m_groupEndTimestamp.isInvalid() || frameEndTimestamp > m_groupEndTimestamp)
1608 m_groupEndTimestamp = frameEndTimestamp;
1610 // 1.22 If generate timestamps flag equals true, then set timestampOffset equal to frame end timestamp.
1611 if (m_shouldGenerateTimestamps)
1612 m_timestampOffset = frameEndTimestamp;
1614 // Eliminate small gaps between buffered ranges by coalescing
1615 // disjoint ranges separated by less than a "fudge factor".
1616 auto presentationEndTime = presentationTimestamp + frameDuration;
1617 auto nearestToPresentationStartTime = m_buffered->ranges().nearest(presentationTimestamp);
1618 if ((presentationTimestamp - nearestToPresentationStartTime).isBetween(MediaTime::zeroTime(), currentTimeFudgeFactor()))
1619 presentationTimestamp = nearestToPresentationStartTime;
1621 auto nearestToPresentationEndTime = m_buffered->ranges().nearest(presentationEndTime);
1622 if ((nearestToPresentationEndTime - presentationEndTime).isBetween(MediaTime::zeroTime(), currentTimeFudgeFactor()))
1623 presentationEndTime = nearestToPresentationEndTime;
1625 m_buffered->ranges().add(presentationTimestamp, presentationEndTime);
1626 m_bufferedSinceLastMonitor += frameDuration.toDouble();
1627 setBufferedDirty(true);
1632 // Steps 2-4 will be handled by MediaSource::monitorSourceBuffers()
1634 // 5. If the media segment contains data beyond the current duration, then run the duration change algorithm with new
1635 // duration set to the maximum of the current duration and the group end timestamp.
1636 if (m_groupEndTimestamp > m_source->duration())
1637 m_source->setDurationInternal(m_groupEndTimestamp);
1640 bool SourceBuffer::hasAudio() const
1642 return m_audioTracks && m_audioTracks->length();
1645 bool SourceBuffer::hasVideo() const
1647 return m_videoTracks && m_videoTracks->length();
1650 bool SourceBuffer::sourceBufferPrivateHasAudio(const SourceBufferPrivate*) const
1655 bool SourceBuffer::sourceBufferPrivateHasVideo(const SourceBufferPrivate*) const
1660 void SourceBuffer::videoTrackSelectedChanged(VideoTrack* track)
1662 // 2.4.5 Changes to selected/enabled track state
1663 // If the selected video track changes, then run the following steps:
1664 // 1. If the SourceBuffer associated with the previously selected video track is not associated with
1665 // any other enabled tracks, run the following steps:
1666 if (track->selected()
1667 && (!m_videoTracks || !m_videoTracks->isAnyTrackEnabled())
1668 && (!m_audioTracks || !m_audioTracks->isAnyTrackEnabled())
1669 && (!m_textTracks || !m_textTracks->isAnyTrackEnabled())) {
1670 // 1.1 Remove the SourceBuffer from activeSourceBuffers.
1671 // 1.2 Queue a task to fire a simple event named removesourcebuffer at activeSourceBuffers
1673 } else if (!track->selected()) {
1674 // 2. If the SourceBuffer associated with the newly selected video track is not already in activeSourceBuffers,
1675 // run the following steps:
1676 // 2.1 Add the SourceBuffer to activeSourceBuffers.
1677 // 2.2 Queue a task to fire a simple event named addsourcebuffer at activeSourceBuffers
1682 m_source->mediaElement()->videoTrackSelectedChanged(track);
1685 void SourceBuffer::audioTrackEnabledChanged(AudioTrack* track)
1687 // 2.4.5 Changes to selected/enabled track state
1688 // If an audio track becomes disabled and the SourceBuffer associated with this track is not
1689 // associated with any other enabled or selected track, then run the following steps:
1690 if (track->enabled()
1691 && (!m_videoTracks || !m_videoTracks->isAnyTrackEnabled())
1692 && (!m_audioTracks || !m_audioTracks->isAnyTrackEnabled())
1693 && (!m_textTracks || !m_textTracks->isAnyTrackEnabled())) {
1694 // 1. Remove the SourceBuffer associated with the audio track from activeSourceBuffers
1695 // 2. Queue a task to fire a simple event named removesourcebuffer at activeSourceBuffers
1697 } else if (!track->enabled()) {
1698 // If an audio track becomes enabled and the SourceBuffer associated with this track is
1699 // not already in activeSourceBuffers, then run the following steps:
1700 // 1. Add the SourceBuffer associated with the audio track to activeSourceBuffers
1701 // 2. Queue a task to fire a simple event named addsourcebuffer at activeSourceBuffers
1706 m_source->mediaElement()->audioTrackEnabledChanged(track);
1709 void SourceBuffer::textTrackModeChanged(TextTrack* track)
1711 // 2.4.5 Changes to selected/enabled track state
1712 // If a text track mode becomes "disabled" and the SourceBuffer associated with this track is not
1713 // associated with any other enabled or selected track, then run the following steps:
1714 if (track->mode() == TextTrack::Mode::Disabled
1715 && (!m_videoTracks || !m_videoTracks->isAnyTrackEnabled())
1716 && (!m_audioTracks || !m_audioTracks->isAnyTrackEnabled())
1717 && (!m_textTracks || !m_textTracks->isAnyTrackEnabled())) {
1718 // 1. Remove the SourceBuffer associated with the audio track from activeSourceBuffers
1719 // 2. Queue a task to fire a simple event named removesourcebuffer at activeSourceBuffers
1722 // If a text track mode becomes "showing" or "hidden" and the SourceBuffer associated with this
1723 // track is not already in activeSourceBuffers, then run the following steps:
1724 // 1. Add the SourceBuffer associated with the text track to activeSourceBuffers
1725 // 2. Queue a task to fire a simple event named addsourcebuffer at activeSourceBuffers
1730 m_source->mediaElement()->textTrackModeChanged(track);
1733 void SourceBuffer::textTrackAddCue(TextTrack* track, TextTrackCue& cue)
1736 m_source->mediaElement()->textTrackAddCue(track, cue);
1739 void SourceBuffer::textTrackAddCues(TextTrack* track, TextTrackCueList const* cueList)
1742 m_source->mediaElement()->textTrackAddCues(track, cueList);
1745 void SourceBuffer::textTrackRemoveCue(TextTrack* track, TextTrackCue& cue)
1748 m_source->mediaElement()->textTrackRemoveCue(track, cue);
1751 void SourceBuffer::textTrackRemoveCues(TextTrack* track, TextTrackCueList const* cueList)
1754 m_source->mediaElement()->textTrackRemoveCues(track, cueList);
1757 void SourceBuffer::textTrackKindChanged(TextTrack* track)
1760 m_source->mediaElement()->textTrackKindChanged(track);
1763 void SourceBuffer::sourceBufferPrivateDidBecomeReadyForMoreSamples(SourceBufferPrivate*, AtomicString trackID)
1765 LOG(MediaSource, "SourceBuffer::sourceBufferPrivateDidBecomeReadyForMoreSamples(%p)", this);
1766 auto it = m_trackBufferMap.find(trackID);
1767 if (it == m_trackBufferMap.end())
1770 TrackBuffer& trackBuffer = it->value;
1771 if (!trackBuffer.needsReenqueueing && !m_source->isSeeking())
1772 provideMediaData(trackBuffer, trackID);
1775 void SourceBuffer::provideMediaData(TrackBuffer& trackBuffer, AtomicString trackID)
1778 unsigned enqueuedSamples = 0;
1781 while (!trackBuffer.decodeQueue.empty()) {
1782 if (!m_private->isReadyForMoreSamples(trackID)) {
1783 m_private->notifyClientWhenReadyForMoreSamples(trackID);
1787 // FIXME(rdar://problem/20635969): Remove this re-entrancy protection when the aforementioned radar is resolved; protecting
1788 // against re-entrancy introduces a small inefficency when removing appended samples from the decode queue one at a time
1789 // rather than when all samples have been enqueued.
1790 auto sample = trackBuffer.decodeQueue.begin()->second;
1791 trackBuffer.decodeQueue.erase(trackBuffer.decodeQueue.begin());
1793 // Do not enqueue samples spanning a significant unbuffered gap.
1794 // NOTE: one second is somewhat arbitrary. MediaSource::monitorSourceBuffers() is run
1795 // on the playbackTimer, which is effectively every 350ms. Allowing > 350ms gap between
1796 // enqueued samples allows for situations where we overrun the end of a buffered range
1797 // but don't notice for 350s of playback time, and the client can enqueue data for the
1798 // new current time without triggering this early return.
1799 // FIXME(135867): Make this gap detection logic less arbitrary.
1800 MediaTime oneSecond(1, 1);
1801 if (trackBuffer.lastEnqueuedDecodeEndTime.isValid() && sample->decodeTime() - trackBuffer.lastEnqueuedDecodeEndTime > oneSecond)
1804 trackBuffer.lastEnqueuedPresentationTime = sample->presentationTime();
1805 trackBuffer.lastEnqueuedDecodeEndTime = sample->decodeTime() + sample->duration();
1806 m_private->enqueueSample(WTFMove(sample), trackID);
1812 LOG(MediaSource, "SourceBuffer::provideMediaData(%p) - Enqueued %u samples", this, enqueuedSamples);
1815 void SourceBuffer::reenqueueMediaForTime(TrackBuffer& trackBuffer, AtomicString trackID, const MediaTime& time)
1817 // Find the sample which contains the current presentation time.
1818 auto currentSamplePTSIterator = trackBuffer.samples.presentationOrder().findSampleContainingPresentationTime(time);
1820 if (currentSamplePTSIterator == trackBuffer.samples.presentationOrder().end()) {
1821 trackBuffer.decodeQueue.clear();
1822 m_private->flushAndEnqueueNonDisplayingSamples(Vector<RefPtr<MediaSample>>(), trackID);
1826 // Seach backward for the previous sync sample.
1827 DecodeOrderSampleMap::KeyType decodeKey(currentSamplePTSIterator->second->decodeTime(), currentSamplePTSIterator->second->presentationTime());
1828 auto currentSampleDTSIterator = trackBuffer.samples.decodeOrder().findSampleWithDecodeKey(decodeKey);
1829 ASSERT(currentSampleDTSIterator != trackBuffer.samples.decodeOrder().end());
1831 auto reverseCurrentSampleIter = --DecodeOrderSampleMap::reverse_iterator(currentSampleDTSIterator);
1832 auto reverseLastSyncSampleIter = trackBuffer.samples.decodeOrder().findSyncSamplePriorToDecodeIterator(reverseCurrentSampleIter);
1833 if (reverseLastSyncSampleIter == trackBuffer.samples.decodeOrder().rend()) {
1834 trackBuffer.decodeQueue.clear();
1835 m_private->flushAndEnqueueNonDisplayingSamples(Vector<RefPtr<MediaSample>>(), trackID);
1839 Vector<RefPtr<MediaSample>> nonDisplayingSamples;
1840 for (auto iter = reverseLastSyncSampleIter; iter != reverseCurrentSampleIter; --iter)
1841 nonDisplayingSamples.append(iter->second);
1843 m_private->flushAndEnqueueNonDisplayingSamples(nonDisplayingSamples, trackID);
1845 if (!nonDisplayingSamples.isEmpty()) {
1846 trackBuffer.lastEnqueuedPresentationTime = nonDisplayingSamples.last()->presentationTime();
1847 trackBuffer.lastEnqueuedDecodeEndTime = nonDisplayingSamples.last()->decodeTime();
1849 trackBuffer.lastEnqueuedPresentationTime = MediaTime::invalidTime();
1850 trackBuffer.lastEnqueuedDecodeEndTime = MediaTime::invalidTime();
1853 // Fill the decode queue with the remaining samples.
1854 trackBuffer.decodeQueue.clear();
1855 for (auto iter = currentSampleDTSIterator; iter != trackBuffer.samples.decodeOrder().end(); ++iter)
1856 trackBuffer.decodeQueue.insert(*iter);
1857 provideMediaData(trackBuffer, trackID);
1859 trackBuffer.needsReenqueueing = false;
1863 void SourceBuffer::didDropSample()
1866 m_source->mediaElement()->incrementDroppedFrameCount();
1869 void SourceBuffer::monitorBufferingRate()
1871 if (!m_bufferedSinceLastMonitor)
1874 double now = monotonicallyIncreasingTime();
1875 double interval = now - m_timeOfBufferingMonitor;
1876 double rateSinceLastMonitor = m_bufferedSinceLastMonitor / interval;
1878 m_timeOfBufferingMonitor = now;
1879 m_bufferedSinceLastMonitor = 0;
1881 m_averageBufferRate = m_averageBufferRate * (1 - ExponentialMovingAverageCoefficient) + rateSinceLastMonitor * ExponentialMovingAverageCoefficient;
1883 LOG(MediaSource, "SourceBuffer::monitorBufferingRate(%p) - m_avegareBufferRate: %lf", this, m_averageBufferRate);
1886 std::unique_ptr<PlatformTimeRanges> SourceBuffer::bufferedAccountingForEndOfStream() const
1888 // FIXME: Revisit this method once the spec bug <https://www.w3.org/Bugs/Public/show_bug.cgi?id=26436> is resolved.
1889 auto virtualRanges = std::make_unique<PlatformTimeRanges>(m_buffered->ranges());
1890 if (m_source->isEnded()) {
1891 MediaTime start = virtualRanges->maximumBufferedTime();
1892 MediaTime end = m_source->duration();
1894 virtualRanges->add(start, end);
1896 return virtualRanges;
1899 bool SourceBuffer::hasCurrentTime() const
1901 if (isRemoved() || !m_buffered->length())
1904 MediaTime currentTime = m_source->currentTime();
1905 MediaTime duration = m_source->duration();
1906 if (currentTime >= duration)
1909 std::unique_ptr<PlatformTimeRanges> ranges = bufferedAccountingForEndOfStream();
1910 return abs(ranges->nearest(currentTime) - currentTime) <= currentTimeFudgeFactor();
1913 bool SourceBuffer::hasFutureTime() const
1918 std::unique_ptr<PlatformTimeRanges> ranges = bufferedAccountingForEndOfStream();
1919 if (!ranges->length())
1922 MediaTime currentTime = m_source->currentTime();
1923 MediaTime duration = m_source->duration();
1924 if (currentTime >= duration)
1927 MediaTime nearest = ranges->nearest(currentTime);
1928 if (abs(nearest - currentTime) > currentTimeFudgeFactor())
1931 size_t found = ranges->find(nearest);
1932 if (found == notFound)
1935 MediaTime localEnd = ranges->end(found);
1936 if (localEnd == duration)
1939 return localEnd - currentTime > currentTimeFudgeFactor();
1942 bool SourceBuffer::canPlayThrough()
1947 monitorBufferingRate();
1949 // Assuming no fluctuations in the buffering rate, loading 1 second per second or greater
1950 // means indefinite playback. This could be improved by taking jitter into account.
1951 if (m_averageBufferRate > 1)
1954 // Add up all the time yet to be buffered.
1955 MediaTime currentTime = m_source->currentTime();
1956 MediaTime duration = m_source->duration();
1958 std::unique_ptr<PlatformTimeRanges> unbufferedRanges = bufferedAccountingForEndOfStream();
1959 unbufferedRanges->invert();
1960 unbufferedRanges->intersectWith(PlatformTimeRanges(currentTime, std::max(currentTime, duration)));
1961 MediaTime unbufferedTime = unbufferedRanges->totalDuration();
1962 if (!unbufferedTime.isValid())
1965 MediaTime timeRemaining = duration - currentTime;
1966 return unbufferedTime.toDouble() / m_averageBufferRate < timeRemaining.toDouble();
1969 size_t SourceBuffer::extraMemoryCost() const
1971 size_t extraMemoryCost = m_pendingAppendData.capacity();
1972 for (auto& trackBuffer : m_trackBufferMap.values())
1973 extraMemoryCost += trackBuffer.samples.sizeInBytes();
1975 return extraMemoryCost;
1978 void SourceBuffer::reportExtraMemoryAllocated()
1980 size_t extraMemoryCost = this->extraMemoryCost();
1981 if (extraMemoryCost <= m_reportedExtraMemoryCost)
1984 size_t extraMemoryCostDelta = extraMemoryCost - m_reportedExtraMemoryCost;
1985 m_reportedExtraMemoryCost = extraMemoryCost;
1987 JSC::JSLockHolder lock(scriptExecutionContext()->vm());
1988 // FIXME: Adopt reportExtraMemoryVisited, and switch to reportExtraMemoryAllocated.
1989 // https://bugs.webkit.org/show_bug.cgi?id=142595
1990 scriptExecutionContext()->vm().heap.deprecatedReportExtraMemory(extraMemoryCostDelta);
1993 Vector<String> SourceBuffer::bufferedSamplesForTrackID(const AtomicString& trackID)
1995 auto it = m_trackBufferMap.find(trackID);
1996 if (it == m_trackBufferMap.end())
1997 return Vector<String>();
1999 TrackBuffer& trackBuffer = it->value;
2000 Vector<String> sampleDescriptions;
2001 for (auto& pair : trackBuffer.samples.decodeOrder())
2002 sampleDescriptions.append(toString(*pair.second));
2004 return sampleDescriptions;
2007 Document& SourceBuffer::document() const
2009 ASSERT(scriptExecutionContext());
2010 return downcast<Document>(*scriptExecutionContext());
2013 void SourceBuffer::setMode(AppendMode newMode, ExceptionCode& ec)
2015 // 3.1 Attributes - mode
2016 // http://www.w3.org/TR/media-source/#widl-SourceBuffer-mode
2018 // On setting, run the following steps:
2020 // 1. Let new mode equal the new value being assigned to this attribute.
2021 // 2. If generate timestamps flag equals true and new mode equals "segments", then throw an INVALID_ACCESS_ERR exception and abort these steps.
2022 if (m_shouldGenerateTimestamps && newMode == AppendMode::Segments) {
2023 ec = INVALID_ACCESS_ERR;
2027 // 3. If this object has been removed from the sourceBuffers attribute of the parent media source, then throw an INVALID_STATE_ERR exception and abort these steps.
2028 // 4. If the updating attribute equals true, then throw an INVALID_STATE_ERR exception and abort these steps.
2029 if (isRemoved() || m_updating) {
2030 ec = INVALID_STATE_ERR;
2034 // 5. If the readyState attribute of the parent media source is in the "ended" state then run the following steps:
2035 if (m_source->readyState() == MediaSource::endedKeyword()) {
2036 // 5.1. Set the readyState attribute of the parent media source to "open"
2037 // 5.2. Queue a task to fire a simple event named sourceopen at the parent media source.
2038 m_source->openIfInEndedState();
2041 // 6. If the append state equals PARSING_MEDIA_SEGMENT, then throw an INVALID_STATE_ERR and abort these steps.
2042 if (m_appendState == ParsingMediaSegment) {
2043 ec = INVALID_STATE_ERR;
2047 // 7. If the new mode equals "sequence", then set the group start timestamp to the group end timestamp.
2048 if (newMode == AppendMode::Sequence)
2049 m_groupStartTimestamp = m_groupEndTimestamp;
2051 // 8. Update the attribute to new mode.
2055 } // namespace WebCore