summaryrefslogtreecommitdiff
path: root/Source/WebCore/Modules/mediasource/MediaSource.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Source/WebCore/Modules/mediasource/MediaSource.cpp')
-rw-r--r--Source/WebCore/Modules/mediasource/MediaSource.cpp786
1 files changed, 533 insertions, 253 deletions
diff --git a/Source/WebCore/Modules/mediasource/MediaSource.cpp b/Source/WebCore/Modules/mediasource/MediaSource.cpp
index 7a3294676..4f188b852 100644
--- a/Source/WebCore/Modules/mediasource/MediaSource.cpp
+++ b/Source/WebCore/Modules/mediasource/MediaSource.cpp
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2013 Google Inc. All rights reserved.
+ * Copyright (C) 2013-2017 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
@@ -33,79 +34,103 @@
#if ENABLE(MEDIA_SOURCE)
-#include "AudioTrack.h"
#include "AudioTrackList.h"
#include "ContentType.h"
#include "Event.h"
+#include "EventNames.h"
#include "ExceptionCode.h"
-#include "ExceptionCodePlaceholder.h"
-#include "GenericEventQueue.h"
#include "HTMLMediaElement.h"
#include "Logging.h"
-#include "MIMETypeRegistry.h"
-#include "MediaError.h"
-#include "MediaPlayer.h"
+#include "MediaSourcePrivate.h"
#include "MediaSourceRegistry.h"
+#include "SourceBuffer.h"
+#include "SourceBufferList.h"
#include "SourceBufferPrivate.h"
-#include "TextTrack.h"
#include "TextTrackList.h"
#include "TimeRanges.h"
-#include "VideoTrack.h"
#include "VideoTrackList.h"
-#include <runtime/Uint8Array.h>
-#include <wtf/text/CString.h>
-#include <wtf/text/WTFString.h>
namespace WebCore {
-PassRefPtr<MediaSource> MediaSource::create(ScriptExecutionContext& context)
+URLRegistry* MediaSource::s_registry;
+
+void MediaSource::setRegistry(URLRegistry* registry)
+{
+ ASSERT(!s_registry);
+ s_registry = registry;
+}
+
+Ref<MediaSource> MediaSource::create(ScriptExecutionContext& context)
{
- RefPtr<MediaSource> mediaSource(adoptRef(new MediaSource(context)));
+ auto mediaSource = adoptRef(*new MediaSource(context));
mediaSource->suspendIfNeeded();
- return mediaSource.release();
+ return mediaSource;
}
MediaSource::MediaSource(ScriptExecutionContext& context)
: ActiveDOMObject(&context)
- , m_mediaElement(0)
+ , m_duration(MediaTime::invalidTime())
+ , m_pendingSeekTime(MediaTime::invalidTime())
, m_readyState(closedKeyword())
, m_asyncEventQueue(*this)
{
- LOG(Media, "MediaSource::MediaSource %p", this);
+ LOG(MediaSource, "MediaSource::MediaSource %p", this);
m_sourceBuffers = SourceBufferList::create(scriptExecutionContext());
m_activeSourceBuffers = SourceBufferList::create(scriptExecutionContext());
}
MediaSource::~MediaSource()
{
- LOG(Media, "MediaSource::~MediaSource %p", this);
+ LOG(MediaSource, "MediaSource::~MediaSource %p", this);
ASSERT(isClosed());
}
const AtomicString& MediaSource::openKeyword()
{
- DEFINE_STATIC_LOCAL(const AtomicString, open, ("open", AtomicString::ConstructFromLiteral));
+ static NeverDestroyed<const AtomicString> open("open", AtomicString::ConstructFromLiteral);
return open;
}
const AtomicString& MediaSource::closedKeyword()
{
- DEFINE_STATIC_LOCAL(const AtomicString, closed, ("closed", AtomicString::ConstructFromLiteral));
+ static NeverDestroyed<const AtomicString> closed("closed", AtomicString::ConstructFromLiteral);
return closed;
}
const AtomicString& MediaSource::endedKeyword()
{
- DEFINE_STATIC_LOCAL(const AtomicString, ended, ("ended", AtomicString::ConstructFromLiteral));
+ static NeverDestroyed<const AtomicString> ended("ended", AtomicString::ConstructFromLiteral);
return ended;
}
-void MediaSource::setPrivateAndOpen(PassRef<MediaSourcePrivate> mediaSourcePrivate)
+void MediaSource::setPrivateAndOpen(Ref<MediaSourcePrivate>&& mediaSourcePrivate)
{
ASSERT(!m_private);
ASSERT(m_mediaElement);
- m_private = std::move(mediaSourcePrivate);
+ m_private = WTFMove(mediaSourcePrivate);
+
+ // 2.4.1 Attaching to a media element
+ // https://rawgit.com/w3c/media-source/45627646344eea0170dd1cbc5a3d508ca751abb8/media-source-respec.html#mediasource-attach
+
+ // ↳ If readyState is NOT set to "closed"
+ // Run the "If the media data cannot be fetched at all, due to network errors, causing the user agent to give up trying
+ // to fetch the resource" steps of the resource fetch algorithm's media data processing steps list.
+ if (!isClosed()) {
+ m_mediaElement->mediaLoadingFailedFatally(MediaPlayer::NetworkError);
+ return;
+ }
+
+ // ↳ Otherwise
+ // 1. Set the media element's delaying-the-load-event-flag to false.
+ m_mediaElement->setShouldDelayLoadEvent(false);
+
+ // 2. Set the readyState attribute to "open".
+ // 3. Queue a task to fire a simple event named sourceopen at the MediaSource.
setReadyState(openKeyword());
+
+ // 4. Continue the resource fetch algorithm by running the remaining "Otherwise (mode is local)" steps,
+ // with these clarifications:
+ // NOTE: This is handled in HTMLMediaElement.
}
void MediaSource::addedToRegistry()
@@ -118,110 +143,268 @@ void MediaSource::removedFromRegistry()
unsetPendingActivity(this);
}
-double MediaSource::duration() const
+MediaTime MediaSource::duration() const
{
- return isClosed() ? std::numeric_limits<float>::quiet_NaN() : m_private->duration();
+ return m_duration;
}
-PassRefPtr<TimeRanges> MediaSource::buffered() const
+void MediaSource::durationChanged(const MediaTime& duration)
{
+ m_duration = duration;
+}
+
+MediaTime MediaSource::currentTime() const
+{
+ return m_mediaElement ? m_mediaElement->currentMediaTime() : MediaTime::zeroTime();
+}
+
+std::unique_ptr<PlatformTimeRanges> MediaSource::buffered() const
+{
+ if (m_buffered && m_activeSourceBuffers->length() && std::all_of(m_activeSourceBuffers->begin(), m_activeSourceBuffers->end(), [](auto& buffer) { return !buffer->isBufferedDirty(); }))
+ return std::make_unique<PlatformTimeRanges>(*m_buffered);
+
+ m_buffered = std::make_unique<PlatformTimeRanges>();
+ for (auto& sourceBuffer : *m_activeSourceBuffers)
+ sourceBuffer->setBufferedDirty(false);
+
// Implements MediaSource algorithm for HTMLMediaElement.buffered.
// https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#htmlmediaelement-extensions
- Vector<RefPtr<TimeRanges>> ranges = activeRanges();
+ Vector<PlatformTimeRanges> activeRanges = this->activeRanges();
// 1. If activeSourceBuffers.length equals 0 then return an empty TimeRanges object and abort these steps.
- if (ranges.isEmpty())
- return TimeRanges::create();
+ if (activeRanges.isEmpty())
+ return std::make_unique<PlatformTimeRanges>(*m_buffered);
// 2. Let active ranges be the ranges returned by buffered for each SourceBuffer object in activeSourceBuffers.
// 3. Let highest end time be the largest range end time in the active ranges.
- double highestEndTime = -1;
- for (size_t i = 0; i < ranges.size(); ++i) {
- unsigned length = ranges[i]->length();
+ MediaTime highestEndTime = MediaTime::zeroTime();
+ for (auto& ranges : activeRanges) {
+ unsigned length = ranges.length();
if (length)
- highestEndTime = std::max(highestEndTime, ranges[i]->end(length - 1, ASSERT_NO_EXCEPTION));
+ highestEndTime = std::max(highestEndTime, ranges.end(length - 1));
}
// Return an empty range if all ranges are empty.
- if (highestEndTime < 0)
- return TimeRanges::create();
+ if (!highestEndTime)
+ return std::make_unique<PlatformTimeRanges>(*m_buffered);
// 4. Let intersection ranges equal a TimeRange object containing a single range from 0 to highest end time.
- RefPtr<TimeRanges> intersectionRanges = TimeRanges::create(0, highestEndTime);
+ m_buffered->add(MediaTime::zeroTime(), highestEndTime);
// 5. For each SourceBuffer object in activeSourceBuffers run the following steps:
bool ended = readyState() == endedKeyword();
- for (size_t i = 0; i < ranges.size(); ++i) {
+ for (auto& sourceRanges : activeRanges) {
// 5.1 Let source ranges equal the ranges returned by the buffered attribute on the current SourceBuffer.
- TimeRanges* sourceRanges = ranges[i].get();
-
// 5.2 If readyState is "ended", then set the end time on the last range in source ranges to highest end time.
- if (ended && sourceRanges->length())
- sourceRanges->add(sourceRanges->start(sourceRanges->length() - 1, ASSERT_NO_EXCEPTION), highestEndTime);
+ if (ended && sourceRanges.length())
+ sourceRanges.add(sourceRanges.start(sourceRanges.length() - 1), highestEndTime);
// 5.3 Let new intersection ranges equal the the intersection between the intersection ranges and the source ranges.
// 5.4 Replace the ranges in intersection ranges with the new intersection ranges.
- intersectionRanges->intersectWith(sourceRanges);
+ m_buffered->intersectWith(sourceRanges);
}
- return intersectionRanges.release();
+ return std::make_unique<PlatformTimeRanges>(*m_buffered);
}
-class SourceBufferBufferedDoesNotContainTime {
-public:
- SourceBufferBufferedDoesNotContainTime(double time) : m_time(time) { }
- bool operator()(RefPtr<SourceBuffer> sourceBuffer)
- {
- return !sourceBuffer->buffered()->contain(m_time);
- }
+void MediaSource::seekToTime(const MediaTime& time)
+{
+ if (isClosed())
+ return;
- double m_time;
-};
+ // 2.4.3 Seeking
+ // https://rawgit.com/w3c/media-source/45627646344eea0170dd1cbc5a3d508ca751abb8/media-source-respec.html#mediasource-seeking
-class SourceBufferBufferedHasEnough {
-public:
- SourceBufferBufferedHasEnough(double time, double duration) : m_time(time), m_duration(duration) { }
- bool operator()(RefPtr<SourceBuffer> sourceBuffer)
- {
- size_t rangePos = sourceBuffer->buffered()->find(m_time);
- if (rangePos == notFound)
- return false;
+ m_pendingSeekTime = time;
- double endTime = sourceBuffer->buffered()->end(rangePos, IGNORE_EXCEPTION);
- return m_duration - endTime < 1;
+ // Run the following steps as part of the "Wait until the user agent has established whether or not the
+ // media data for the new playback position is available, and, if it is, until it has decoded enough data
+ // to play back that position" step of the seek algorithm:
+ // ↳ If new playback position is not in any TimeRange of HTMLMediaElement.buffered
+ if (!hasBufferedTime(time)) {
+ // 1. If the HTMLMediaElement.readyState attribute is greater than HAVE_METADATA,
+ // then set the HTMLMediaElement.readyState attribute to HAVE_METADATA.
+ m_private->setReadyState(MediaPlayer::HaveMetadata);
+
+ // 2. The media element waits until an appendBuffer() or an appendStream() call causes the coded
+ // frame processing algorithm to set the HTMLMediaElement.readyState attribute to a value greater
+ // than HAVE_METADATA.
+ LOG(MediaSource, "MediaSource::seekToTime(%p) - waitForSeekCompleted()", this);
+ m_private->waitForSeekCompleted();
+ return;
}
+ // ↳ Otherwise
+ // Continue
+
+ completeSeek();
+}
+
+void MediaSource::completeSeek()
+{
+ if (isClosed())
+ return;
+
+ // 2.4.3 Seeking, ctd.
+ // https://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-source.html#mediasource-seeking
+
+ ASSERT(m_pendingSeekTime.isValid());
+
+ // 2. The media element resets all decoders and initializes each one with data from the appropriate
+ // initialization segment.
+ // 3. The media element feeds coded frames from the active track buffers into the decoders starting
+ // with the closest random access point before the new playback position.
+ MediaTime pendingSeekTime = m_pendingSeekTime;
+ m_pendingSeekTime = MediaTime::invalidTime();
+ for (auto& sourceBuffer : *m_activeSourceBuffers)
+ sourceBuffer->seekToTime(pendingSeekTime);
+
+ // 4. Resume the seek algorithm at the "Await a stable state" step.
+ m_private->seekCompleted();
+
+ monitorSourceBuffers();
+}
+
+Ref<TimeRanges> MediaSource::seekable()
+{
+ // 6. HTMLMediaElement Extensions, seekable
+ // W3C Editor's Draft 16 September 2016
+ // https://rawgit.com/w3c/media-source/45627646344eea0170dd1cbc5a3d508ca751abb8/media-source-respec.html#htmlmediaelement-extensions
+
+ // ↳ If duration equals NaN:
+ // Return an empty TimeRanges object.
+ if (m_duration.isInvalid())
+ return TimeRanges::create();
- double m_time;
- double m_duration;
-};
-
-class SourceBufferBufferedHasFuture {
-public:
- SourceBufferBufferedHasFuture(double time) : m_time(time) { }
- bool operator()(RefPtr<SourceBuffer> sourceBuffer)
- {
- size_t rangePos = sourceBuffer->buffered()->find(m_time);
- if (rangePos == notFound)
- return false;
-
- double endTime = sourceBuffer->buffered()->end(rangePos, IGNORE_EXCEPTION);
- return endTime - m_time > 1;
+ // ↳ If duration equals positive Infinity:
+ if (m_duration.isPositiveInfinite()) {
+ auto buffered = this->buffered();
+ // If live seekable range is not empty:
+ if (m_liveSeekable && m_liveSeekable->length()) {
+ // Let union ranges be the union of live seekable range and the HTMLMediaElement.buffered attribute.
+ buffered->unionWith(*m_liveSeekable);
+ // Return a single range with a start time equal to the earliest start time in union ranges
+ // and an end time equal to the highest end time in union ranges and abort these steps.
+ buffered->add(buffered->start(0), buffered->maximumBufferedTime());
+ return TimeRanges::create(*buffered);
+ }
+
+ // If the HTMLMediaElement.buffered attribute returns an empty TimeRanges object, then return
+ // an empty TimeRanges object and abort these steps.
+ if (!buffered->length())
+ return TimeRanges::create();
+
+ // Return a single range with a start time of 0 and an end time equal to the highest end time
+ // reported by the HTMLMediaElement.buffered attribute.
+ return TimeRanges::create({MediaTime::zeroTime(), buffered->maximumBufferedTime()});
}
- double m_time;
-};
+ // ↳ Otherwise:
+ // Return a single range with a start time of 0 and an end time equal to duration.
+ return TimeRanges::create({MediaTime::zeroTime(), m_duration});
+}
+
+ExceptionOr<void> MediaSource::setLiveSeekableRange(double start, double end)
+{
+ // W3C Editor's Draft 16 September 2016
+ // https://rawgit.com/w3c/media-source/45627646344eea0170dd1cbc5a3d508ca751abb8/media-source-respec.html#dom-mediasource-setliveseekablerange
+
+ // If the readyState attribute is not "open" then throw an InvalidStateError exception and abort these steps.
+ if (!isOpen())
+ return Exception { INVALID_STATE_ERR };
+
+ // If start is negative or greater than end, then throw a TypeError exception and abort these steps.
+ if (start < 0 || start > end)
+ return Exception { TypeError };
+
+ // Set live seekable range to be a new normalized TimeRanges object containing a single range
+ // whose start position is start and end position is end.
+ m_liveSeekable = std::make_unique<PlatformTimeRanges>(MediaTime::createWithDouble(start), MediaTime::createWithDouble(end));
+
+ return { };
+}
+
+ExceptionOr<void> MediaSource::clearLiveSeekableRange()
+{
+ // W3C Editor's Draft 16 September 2016
+ // https://rawgit.com/w3c/media-source/45627646344eea0170dd1cbc5a3d508ca751abb8/media-source-respec.html#dom-mediasource-clearliveseekablerange
+
+ // If the readyState attribute is not "open" then throw an InvalidStateError exception and abort these steps.
+ if (!isOpen())
+ return Exception { INVALID_STATE_ERR };
+ m_liveSeekable = nullptr;
+ return { };
+}
+
+const MediaTime& MediaSource::currentTimeFudgeFactor()
+{
+ // Allow hasCurrentTime() to be off by as much as the length of two 24fps video frames
+ static NeverDestroyed<MediaTime> fudgeFactor(2002, 24000);
+ return fudgeFactor;
+}
+
+bool MediaSource::hasBufferedTime(const MediaTime& time)
+{
+ if (time > duration())
+ return false;
+
+ auto ranges = buffered();
+ if (!ranges->length())
+ return false;
+
+ return abs(ranges->nearest(time) - time) <= currentTimeFudgeFactor();
+}
+
+bool MediaSource::hasCurrentTime()
+{
+ return hasBufferedTime(currentTime());
+}
+
+bool MediaSource::hasFutureTime()
+{
+ MediaTime currentTime = this->currentTime();
+ MediaTime duration = this->duration();
+
+ if (currentTime >= duration)
+ return true;
+
+ auto ranges = buffered();
+ MediaTime nearest = ranges->nearest(currentTime);
+ if (abs(nearest - currentTime) > currentTimeFudgeFactor())
+ return false;
+
+ size_t found = ranges->find(nearest);
+ if (found == notFound)
+ return false;
+
+ MediaTime localEnd = ranges->end(found);
+ if (localEnd == duration)
+ return true;
+
+ return localEnd - currentTime > currentTimeFudgeFactor();
+}
void MediaSource::monitorSourceBuffers()
{
- double currentTime = mediaElement()->currentTime();
+ if (isClosed())
+ return;
// 2.4.4 SourceBuffer Monitoring
- // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#buffer-monitoring
- // ↳ If buffered for all objects in activeSourceBuffers do not contain TimeRanges for the current
- // playback position:
- auto begin = m_activeSourceBuffers->begin();
- auto end = m_activeSourceBuffers->end();
- if (std::all_of(begin, end, SourceBufferBufferedDoesNotContainTime(currentTime))) {
+ // https://rawgit.com/w3c/media-source/45627646344eea0170dd1cbc5a3d508ca751abb8/media-source-respec.html#buffer-monitoring
+
+ // Note, the behavior if activeSourceBuffers is empty is undefined.
+ if (!m_activeSourceBuffers) {
+ m_private->setReadyState(MediaPlayer::HaveNothing);
+ return;
+ }
+
+ // ↳ If the the HTMLMediaElement.readyState attribute equals HAVE_NOTHING:
+ if (mediaElement()->readyState() == HTMLMediaElement::HAVE_NOTHING) {
+ // 1. Abort these steps.
+ return;
+ }
+
+ // ↳ If HTMLMediaElement.buffered does not contain a TimeRange for the current playback position:
+ if (!hasCurrentTime()) {
// 1. Set the HTMLMediaElement.readyState attribute to HAVE_METADATA.
// 2. If this is the first transition to HAVE_METADATA, then queue a task to fire a simple event
// named loadedmetadata at the media element.
@@ -231,33 +414,40 @@ void MediaSource::monitorSourceBuffers()
return;
}
- // ↳ If buffered for all objects in activeSourceBuffers contain TimeRanges that include the current
- // playback position and enough data to ensure uninterrupted playback:
- if (std::all_of(begin, end, SourceBufferBufferedHasEnough(currentTime, mediaElement()->duration()))) {
+ // ↳ If HTMLMediaElement.buffered contains a TimeRange that includes the current
+ // playback position and enough data to ensure uninterrupted playback:
+ auto ranges = buffered();
+ if (std::all_of(m_activeSourceBuffers->begin(), m_activeSourceBuffers->end(), [&](auto& sourceBuffer) {
+ return sourceBuffer->canPlayThroughRange(*ranges);
+ })) {
// 1. Set the HTMLMediaElement.readyState attribute to HAVE_ENOUGH_DATA.
// 2. Queue a task to fire a simple event named canplaythrough at the media element.
// 3. Playback may resume at this point if it was previously suspended by a transition to HAVE_CURRENT_DATA.
m_private->setReadyState(MediaPlayer::HaveEnoughData);
+ if (m_pendingSeekTime.isValid())
+ completeSeek();
+
// 4. Abort these steps.
return;
}
- // ↳ If buffered for at least one object in activeSourceBuffers contains a TimeRange that includes
- // the current playback position but not enough data to ensure uninterrupted playback:
- if (std::any_of(begin, end, SourceBufferBufferedHasFuture(currentTime))) {
+ // ↳ If HTMLMediaElement.buffered contains a TimeRange that includes the current playback
+ // position and some time beyond the current playback position, then run the following steps:
+ if (hasFutureTime()) {
// 1. Set the HTMLMediaElement.readyState attribute to HAVE_FUTURE_DATA.
// 2. If the previous value of HTMLMediaElement.readyState was less than HAVE_FUTURE_DATA, then queue a task to fire a simple event named canplay at the media element.
// 3. Playback may resume at this point if it was previously suspended by a transition to HAVE_CURRENT_DATA.
m_private->setReadyState(MediaPlayer::HaveFutureData);
+ if (m_pendingSeekTime.isValid())
+ completeSeek();
+
// 4. Abort these steps.
return;
}
- // ↳ If buffered for at least one object in activeSourceBuffers contains a TimeRange that ends
- // at the current playback position and does not have a range covering the time immediately
- // after the current position:
+ // ↳ If HTMLMediaElement.buffered contains a TimeRange that ends at the current playback position and does not have a range covering the time immediately after the current position:
// NOTE: Logically, !(all objects do not contain currentTime) == (some objects contain current time)
// 1. Set the HTMLMediaElement.readyState attribute to HAVE_CURRENT_DATA.
@@ -266,35 +456,84 @@ void MediaSource::monitorSourceBuffers()
// 3. Playback is suspended at this point since the media element doesn't have enough data to
// advance the media timeline.
m_private->setReadyState(MediaPlayer::HaveCurrentData);
-
+
+ if (m_pendingSeekTime.isValid())
+ completeSeek();
+
// 4. Abort these steps.
}
-void MediaSource::setDuration(double duration, ExceptionCode& ec)
+ExceptionOr<void> MediaSource::setDuration(double duration)
{
- if (duration < 0.0 || std::isnan(duration)) {
- ec = INVALID_ACCESS_ERR;
- return;
- }
- if (!isOpen()) {
- ec = INVALID_STATE_ERR;
- return;
+ // 2.1 Attributes - Duration
+ // https://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-source.html#attributes
+
+ // On setting, run the following steps:
+ // 1. If the value being set is negative or NaN then throw an INVALID_ACCESS_ERR exception and abort these steps.
+ if (duration < 0.0 || std::isnan(duration))
+ return Exception { INVALID_ACCESS_ERR };
+
+ // 2. If the readyState attribute is not "open" then throw an INVALID_STATE_ERR exception and abort these steps.
+ if (!isOpen())
+ return Exception { INVALID_STATE_ERR };
+
+ // 3. If the updating attribute equals true on any SourceBuffer in sourceBuffers, then throw an INVALID_STATE_ERR
+ // exception and abort these steps.
+ for (auto& sourceBuffer : *m_sourceBuffers) {
+ if (sourceBuffer->updating())
+ return Exception { INVALID_STATE_ERR };
}
- m_private->setDuration(duration);
+
+ // 4. Run the duration change algorithm with new duration set to the value being assigned to this attribute.
+ return setDurationInternal(MediaTime::createWithDouble(duration));
}
+ExceptionOr<void> MediaSource::setDurationInternal(const MediaTime& duration)
+{
+ // 2.4.6 Duration Change
+ // https://rawgit.com/w3c/media-source/45627646344eea0170dd1cbc5a3d508ca751abb8/media-source-respec.html#duration-change-algorithm
+
+ MediaTime newDuration = duration;
+
+ // 1. If the current value of duration is equal to new duration, then return.
+ if (newDuration == m_duration)
+ return { };
+
+ // 2. If new duration is less than the highest presentation timestamp of any buffered coded frames
+ // for all SourceBuffer objects in sourceBuffers, then throw an InvalidStateError exception and
+ // abort these steps.
+ // 3. Let highest end time be the largest track buffer ranges end time across all the track buffers
+ // across all SourceBuffer objects in sourceBuffers.
+ MediaTime highestPresentationTimestamp;
+ MediaTime highestEndTime;
+ for (auto& sourceBuffer : *m_sourceBuffers) {
+ highestPresentationTimestamp = std::max(highestPresentationTimestamp, sourceBuffer->highestPresentationTimestamp());
+ highestEndTime = std::max(highestEndTime, sourceBuffer->bufferedInternal().ranges().maximumBufferedTime());
+ }
+ if (highestPresentationTimestamp.isValid() && newDuration < highestPresentationTimestamp)
+ return Exception { INVALID_STATE_ERR };
+
+ // 4. If new duration is less than highest end time, then
+ // 4.1. Update new duration to equal highest end time.
+ if (highestEndTime.isValid() && newDuration < highestEndTime)
+ newDuration = highestEndTime;
+
+ // 5. Update duration to new duration.
+ m_duration = newDuration;
+
+ // 6. Update the media duration to new duration and run the HTMLMediaElement duration change algorithm.
+ LOG(MediaSource, "MediaSource::setDurationInternal(%p) - duration(%g)", this, duration.toDouble());
+ m_private->durationChanged();
+
+ return { };
+}
void MediaSource::setReadyState(const AtomicString& state)
{
ASSERT(state == openKeyword() || state == closedKeyword() || state == endedKeyword());
AtomicString oldState = readyState();
- LOG(Media, "MediaSource::setReadyState() %p : %s -> %s", this, oldState.string().ascii().data(), state.string().ascii().data());
-
- if (state == closedKeyword()) {
- m_private.clear();
- m_mediaElement = 0;
- }
+ LOG(MediaSource, "MediaSource::setReadyState(%p) : %s -> %s", this, oldState.string().ascii().data(), state.string().ascii().data());
if (oldState == state)
return;
@@ -304,55 +543,52 @@ void MediaSource::setReadyState(const AtomicString& state)
onReadyStateChange(oldState, state);
}
-static bool SourceBufferIsUpdating(RefPtr<SourceBuffer>& sourceBuffer)
-{
- return sourceBuffer->updating();
-}
-
-void MediaSource::endOfStream(const AtomicString& error, ExceptionCode& ec)
+ExceptionOr<void> MediaSource::endOfStream(std::optional<EndOfStreamError> error)
{
// 2.2 https://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-source.html#widl-MediaSource-endOfStream-void-EndOfStreamError-error
// 1. If the readyState attribute is not in the "open" state then throw an
// INVALID_STATE_ERR exception and abort these steps.
- if (!isOpen()) {
- ec = INVALID_STATE_ERR;
- return;
- }
+ if (!isOpen())
+ return Exception { INVALID_STATE_ERR };
// 2. If the updating attribute equals true on any SourceBuffer in sourceBuffers, then throw an
// INVALID_STATE_ERR exception and abort these steps.
- if (std::any_of(m_sourceBuffers->begin(), m_sourceBuffers->end(), SourceBufferIsUpdating)) {
- ec = INVALID_STATE_ERR;
- return;
- }
+ if (std::any_of(m_sourceBuffers->begin(), m_sourceBuffers->end(), [](auto& sourceBuffer) { return sourceBuffer->updating(); }))
+ return Exception { INVALID_STATE_ERR };
// 3. Run the end of stream algorithm with the error parameter set to error.
- streamEndedWithError(error, ec);
+ streamEndedWithError(error);
+
+ return { };
}
-void MediaSource::streamEndedWithError(const AtomicString& error, ExceptionCode& ec)
+void MediaSource::streamEndedWithError(std::optional<EndOfStreamError> error)
{
- DEFINE_STATIC_LOCAL(const AtomicString, network, ("network", AtomicString::ConstructFromLiteral));
- DEFINE_STATIC_LOCAL(const AtomicString, decode, ("decode", AtomicString::ConstructFromLiteral));
+ LOG(MediaSource, "MediaSource::streamEndedWithError(%p)", this);
+ if (isClosed())
+ return;
// 2.4.7 https://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-source.html#end-of-stream-algorithm
+
// 1. Change the readyState attribute value to "ended".
// 2. Queue a task to fire a simple event named sourceended at the MediaSource.
setReadyState(endedKeyword());
// 3.
- if (error.isEmpty()) {
+ if (!error) {
// ↳ If error is not set, is null, or is an empty string
- // 1. Run the duration change algorithm with new duration set to the highest end timestamp
- // across all SourceBuffer objects in sourceBuffers.
- MediaTime maxEndTimestamp;
- for (auto it = m_sourceBuffers->begin(), end = m_sourceBuffers->end(); it != end; ++it)
- maxEndTimestamp = std::max((*it)->highestPresentationEndTimestamp(), maxEndTimestamp);
- m_private->setDuration(maxEndTimestamp.toDouble());
+ // 1. Run the duration change algorithm with new duration set to the highest end time reported by
+ // the buffered attribute across all SourceBuffer objects in sourceBuffers.
+ MediaTime maxEndTime;
+ for (auto& sourceBuffer : *m_sourceBuffers) {
+ if (auto length = sourceBuffer->bufferedInternal().length())
+ maxEndTime = std::max(sourceBuffer->bufferedInternal().ranges().end(length - 1), maxEndTime);
+ }
+ setDurationInternal(maxEndTime);
// 2. Notify the media element that it now has all of the media data.
m_private->markEndOfStream(MediaSourcePrivate::EosNoError);
- } else if (error == network) {
+ } else if (error == EndOfStreamError::Network) {
// ↳ If error is set to "network"
ASSERT(m_mediaElement);
if (m_mediaElement->readyState() == HTMLMediaElement::HAVE_NOTHING) {
@@ -368,8 +604,9 @@ void MediaSource::streamEndedWithError(const AtomicString& error, ExceptionCode&
// NOTE: This step is handled by HTMLMediaElement::mediaLoadingFailedFatally().
m_mediaElement->mediaLoadingFailedFatally(MediaPlayer::NetworkError);
}
- } else if (error == decode) {
+ } else {
// ↳ If error is set to "decode"
+ ASSERT(error == EndOfStreamError::Decode);
ASSERT(m_mediaElement);
if (m_mediaElement->readyState() == HTMLMediaElement::HAVE_NOTHING) {
// ↳ If the HTMLMediaElement.readyState attribute equals HAVE_NOTHING
@@ -383,101 +620,98 @@ void MediaSource::streamEndedWithError(const AtomicString& error, ExceptionCode&
// NOTE: This step is handled by HTMLMediaElement::mediaLoadingFailedFatally().
m_mediaElement->mediaLoadingFailedFatally(MediaPlayer::DecodeError);
}
- } else {
- // ↳ Otherwise
- // Throw an INVALID_ACCESS_ERR exception.
- ec = INVALID_ACCESS_ERR;
}
}
-SourceBuffer* MediaSource::addSourceBuffer(const String& type, ExceptionCode& ec)
+ExceptionOr<SourceBuffer&> MediaSource::addSourceBuffer(const String& type)
{
- LOG(Media, "MediaSource::addSourceBuffer(%s) %p", type.ascii().data(), this);
+ LOG(MediaSource, "MediaSource::addSourceBuffer(%s) %p", type.ascii().data(), this);
- // 2.2 https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#widl-MediaSource-addSourceBuffer-SourceBuffer-DOMString-type
- // 1. If type is null or an empty then throw an INVALID_ACCESS_ERR exception and
- // abort these steps.
- if (type.isNull() || type.isEmpty()) {
- ec = INVALID_ACCESS_ERR;
- return nullptr;
- }
+ // 2.2 http://www.w3.org/TR/media-source/#widl-MediaSource-addSourceBuffer-SourceBuffer-DOMString-type
+ // When this method is invoked, the user agent must run the following steps:
+
+ // 1. If type is an empty string then throw a TypeError exception and abort these steps.
+ if (type.isEmpty())
+ return Exception { TypeError };
// 2. If type contains a MIME type that is not supported ..., then throw a
// NOT_SUPPORTED_ERR exception and abort these steps.
- if (!isTypeSupported(type)) {
- ec = NOT_SUPPORTED_ERR;
- return nullptr;
- }
+ if (!isTypeSupported(type))
+ return Exception { NOT_SUPPORTED_ERR };
// 4. If the readyState attribute is not in the "open" state then throw an
// INVALID_STATE_ERR exception and abort these steps.
- if (!isOpen()) {
- ec = INVALID_STATE_ERR;
- return nullptr;
- }
+ if (!isOpen())
+ return Exception { INVALID_STATE_ERR };
// 5. Create a new SourceBuffer object and associated resources.
ContentType contentType(type);
- RefPtr<SourceBufferPrivate> sourceBufferPrivate = createSourceBufferPrivate(contentType, ec);
+ auto sourceBufferPrivate = createSourceBufferPrivate(contentType);
- if (!sourceBufferPrivate) {
- ASSERT(ec == NOT_SUPPORTED_ERR || ec == QUOTA_EXCEEDED_ERR);
+ if (sourceBufferPrivate.hasException()) {
// 2. If type contains a MIME type that is not supported ..., then throw a NOT_SUPPORTED_ERR exception and abort these steps.
// 3. If the user agent can't handle any more SourceBuffer objects then throw a QUOTA_EXCEEDED_ERR exception and abort these steps
- return nullptr;
+ return sourceBufferPrivate.releaseException();
}
- RefPtr<SourceBuffer> buffer = SourceBuffer::create(sourceBufferPrivate.releaseNonNull(), this);
- // 6. Add the new object to sourceBuffers and fire a addsourcebuffer on that object.
- m_sourceBuffers->add(buffer);
- m_activeSourceBuffers->add(buffer);
- // 7. Return the new object to the caller.
- return buffer.get();
+ auto buffer = SourceBuffer::create(sourceBufferPrivate.releaseReturnValue(), this);
+
+ // 6. Set the generate timestamps flag on the new object to the value in the "Generate Timestamps Flag"
+ // column of the byte stream format registry [MSE-REGISTRY] entry that is associated with type.
+ // NOTE: In the current byte stream format registry <http://www.w3.org/2013/12/byte-stream-format-registry/>
+ // only the "MPEG Audio Byte Stream Format" has the "Generate Timestamps Flag" value set.
+ bool shouldGenerateTimestamps = contentType.type() == "audio/aac" || contentType.type() == "audio/mpeg";
+ buffer->setShouldGenerateTimestamps(shouldGenerateTimestamps);
+
+ // 7. If the generate timestamps flag equals true:
+ // ↳ Set the mode attribute on the new object to "sequence".
+ // Otherwise:
+ // ↳ Set the mode attribute on the new object to "segments".
+ buffer->setMode(shouldGenerateTimestamps ? SourceBuffer::AppendMode::Sequence : SourceBuffer::AppendMode::Segments);
+
+ auto& result = buffer.get();
+
+ // 8. Add the new object to sourceBuffers and fire a addsourcebuffer on that object.
+ m_sourceBuffers->add(WTFMove(buffer));
+ regenerateActiveSourceBuffers();
+
+ // 9. Return the new object to the caller.
+ return result;
}
-void MediaSource::removeSourceBuffer(SourceBuffer* buffer, ExceptionCode& ec)
+ExceptionOr<void> MediaSource::removeSourceBuffer(SourceBuffer& buffer)
{
- LOG(Media, "MediaSource::removeSourceBuffer() %p", this);
- RefPtr<SourceBuffer> protect(buffer);
-
- // 2.2 https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#widl-MediaSource-removeSourceBuffer-void-SourceBuffer-sourceBuffer
- // 1. If sourceBuffer is null then throw an INVALID_ACCESS_ERR exception and
- // abort these steps.
- if (!buffer) {
- ec = INVALID_ACCESS_ERR;
- return;
- }
+ LOG(MediaSource, "MediaSource::removeSourceBuffer() %p", this);
+ Ref<SourceBuffer> protect(buffer);
// 2. If sourceBuffer specifies an object that is not in sourceBuffers then
// throw a NOT_FOUND_ERR exception and abort these steps.
- if (!m_sourceBuffers->length() || !m_sourceBuffers->contains(buffer)) {
- ec = NOT_FOUND_ERR;
- return;
- }
+ if (!m_sourceBuffers->length() || !m_sourceBuffers->contains(buffer))
+ return Exception { NOT_FOUND_ERR };
// 3. If the sourceBuffer.updating attribute equals true, then run the following steps: ...
- buffer->abortIfUpdating();
+ buffer.abortIfUpdating();
// 4. Let SourceBuffer audioTracks list equal the AudioTrackList object returned by sourceBuffer.audioTracks.
- RefPtr<AudioTrackList> audioTracks = buffer->audioTracks();
+ auto& audioTracks = buffer.audioTracks();
// 5. If the SourceBuffer audioTracks list is not empty, then run the following steps:
- if (audioTracks->length()) {
+ if (audioTracks.length()) {
// 5.1 Let HTMLMediaElement audioTracks list equal the AudioTrackList object returned by the audioTracks
// attribute on the HTMLMediaElement.
// 5.2 Let the removed enabled audio track flag equal false.
bool removedEnabledAudioTrack = false;
// 5.3 For each AudioTrack object in the SourceBuffer audioTracks list, run the following steps:
- while (audioTracks->length()) {
- AudioTrack* track = audioTracks->lastItem();
+ while (audioTracks.length()) {
+ auto& track = *audioTracks.lastItem();
// 5.3.1 Set the sourceBuffer attribute on the AudioTrack object to null.
- track->setSourceBuffer(0);
+ track.setSourceBuffer(nullptr);
// 5.3.2 If the enabled attribute on the AudioTrack object is true, then set the removed enabled
// audio track flag to true.
- if (track->enabled())
+ if (track.enabled())
removedEnabledAudioTrack = true;
// 5.3.3 Remove the AudioTrack object from the HTMLMediaElement audioTracks list.
@@ -489,35 +723,35 @@ void MediaSource::removeSourceBuffer(SourceBuffer* buffer, ExceptionCode& ec)
// 5.3.5 Remove the AudioTrack object from the SourceBuffer audioTracks list.
// 5.3.6 Queue a task to fire a trusted event named removetrack, that does not bubble and is not
// cancelable, and that uses the TrackEvent interface, at the SourceBuffer audioTracks list.
- audioTracks->remove(track);
+ audioTracks.remove(track);
}
// 5.4 If the removed enabled audio track flag equals true, then queue a task to fire a simple event
// named change at the HTMLMediaElement audioTracks list.
if (removedEnabledAudioTrack)
- mediaElement()->audioTracks()->scheduleChangeEvent();
+ mediaElement()->audioTracks().scheduleChangeEvent();
}
// 6. Let SourceBuffer videoTracks list equal the VideoTrackList object returned by sourceBuffer.videoTracks.
- RefPtr<VideoTrackList> videoTracks = buffer->videoTracks();
+ auto& videoTracks = buffer.videoTracks();
// 7. If the SourceBuffer videoTracks list is not empty, then run the following steps:
- if (videoTracks->length()) {
+ if (videoTracks.length()) {
// 7.1 Let HTMLMediaElement videoTracks list equal the VideoTrackList object returned by the videoTracks
// attribute on the HTMLMediaElement.
// 7.2 Let the removed selected video track flag equal false.
bool removedSelectedVideoTrack = false;
// 7.3 For each VideoTrack object in the SourceBuffer videoTracks list, run the following steps:
- while (videoTracks->length()) {
- VideoTrack* track = videoTracks->lastItem();
+ while (videoTracks.length()) {
+ auto& track = *videoTracks.lastItem();
// 7.3.1 Set the sourceBuffer attribute on the VideoTrack object to null.
- track->setSourceBuffer(0);
+ track.setSourceBuffer(nullptr);
// 7.3.2 If the selected attribute on the VideoTrack object is true, then set the removed selected
// video track flag to true.
- if (track->selected())
+ if (track.selected())
removedSelectedVideoTrack = true;
// 7.3.3 Remove the VideoTrack object from the HTMLMediaElement videoTracks list.
@@ -529,35 +763,35 @@ void MediaSource::removeSourceBuffer(SourceBuffer* buffer, ExceptionCode& ec)
// 7.3.5 Remove the VideoTrack object from the SourceBuffer videoTracks list.
// 7.3.6 Queue a task to fire a trusted event named removetrack, that does not bubble and is not
// cancelable, and that uses the TrackEvent interface, at the SourceBuffer videoTracks list.
- videoTracks->remove(track);
+ videoTracks.remove(track);
}
// 7.4 If the removed selected video track flag equals true, then queue a task to fire a simple event
// named change at the HTMLMediaElement videoTracks list.
if (removedSelectedVideoTrack)
- mediaElement()->videoTracks()->scheduleChangeEvent();
+ mediaElement()->videoTracks().scheduleChangeEvent();
}
// 8. Let SourceBuffer textTracks list equal the TextTrackList object returned by sourceBuffer.textTracks.
- RefPtr<TextTrackList> textTracks = buffer->textTracks();
+ auto& textTracks = buffer.textTracks();
// 9. If the SourceBuffer textTracks list is not empty, then run the following steps:
- if (textTracks->length()) {
+ if (textTracks.length()) {
// 9.1 Let HTMLMediaElement textTracks list equal the TextTrackList object returned by the textTracks
// attribute on the HTMLMediaElement.
// 9.2 Let the removed enabled text track flag equal false.
bool removedEnabledTextTrack = false;
// 9.3 For each TextTrack object in the SourceBuffer textTracks list, run the following steps:
- while (textTracks->length()) {
- TextTrack* track = textTracks->lastItem();
+ while (textTracks.length()) {
+ auto& track = *textTracks.lastItem();
// 9.3.1 Set the sourceBuffer attribute on the TextTrack object to null.
- track->setSourceBuffer(0);
+ track.setSourceBuffer(nullptr);
// 9.3.2 If the mode attribute on the TextTrack object is set to "showing" or "hidden", then
// set the removed enabled text track flag to true.
- if (track->mode() == TextTrack::showingKeyword() || track->mode() == TextTrack::hiddenKeyword())
+ if (track.mode() == TextTrack::Mode::Showing || track.mode() == TextTrack::Mode::Hidden)
removedEnabledTextTrack = true;
// 9.3.3 Remove the TextTrack object from the HTMLMediaElement textTracks list.
@@ -569,30 +803,31 @@ void MediaSource::removeSourceBuffer(SourceBuffer* buffer, ExceptionCode& ec)
// 9.3.5 Remove the TextTrack object from the SourceBuffer textTracks list.
// 9.3.6 Queue a task to fire a trusted event named removetrack, that does not bubble and is not
// cancelable, and that uses the TrackEvent interface, at the SourceBuffer textTracks list.
- textTracks->remove(track);
+ textTracks.remove(track);
}
-
+
// 9.4 If the removed enabled text track flag equals true, then queue a task to fire a simple event
// named change at the HTMLMediaElement textTracks list.
if (removedEnabledTextTrack)
- mediaElement()->textTracks()->scheduleChangeEvent();
+ mediaElement()->textTracks().scheduleChangeEvent();
}
-
-
+
// 10. If sourceBuffer is in activeSourceBuffers, then remove sourceBuffer from activeSourceBuffers ...
m_activeSourceBuffers->remove(buffer);
-
+
// 11. Remove sourceBuffer from sourceBuffers and fire a removesourcebuffer event
// on that object.
m_sourceBuffers->remove(buffer);
-
+
// 12. Destroy all resources for sourceBuffer.
- buffer->removedFromMediaSource();
+ buffer.removedFromMediaSource();
+
+ return { };
}
bool MediaSource::isTypeSupported(const String& type)
{
- LOG(Media, "MediaSource::isTypeSupported(%s)", type.ascii().data());
+ LOG(MediaSource, "MediaSource::isTypeSupported(%s)", type.ascii().data());
// Section 2.2 isTypeSupported() method steps.
// https://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-source.html#widl-MediaSource-isTypeSupported-boolean-DOMString-type
@@ -600,11 +835,12 @@ bool MediaSource::isTypeSupported(const String& type)
if (type.isNull() || type.isEmpty())
return false;
- ContentType contentType(type);
+ // FIXME: Why do we convert to lowercase here, but not in MediaSource::addSourceBuffer?
+ ContentType contentType(type.convertToASCIILowercase());
String codecs = contentType.parameter("codecs");
// 2. If type does not contain a valid MIME type string, then return false.
- if (contentType.type().isEmpty() || codecs.isEmpty())
+ if (contentType.type().isEmpty())
return false;
// 3. If type contains a media type or media subtype that the MediaSource does not support, then return false.
@@ -615,7 +851,12 @@ bool MediaSource::isTypeSupported(const String& type)
parameters.type = contentType.type();
parameters.codecs = codecs;
parameters.isMediaSource = true;
- return MediaPlayer::supportsType(parameters, 0) != MediaPlayer::IsNotSupported;
+ MediaPlayer::SupportsType supported = MediaPlayer::supportsType(parameters, 0);
+
+ if (codecs.isEmpty())
+ return supported != MediaPlayer::IsNotSupported;
+
+ return supported == MediaPlayer::IsSupported;
}
bool MediaSource::isOpen() const
@@ -628,27 +869,52 @@ bool MediaSource::isClosed() const
return readyState() == closedKeyword();
}
-void MediaSource::close()
+bool MediaSource::isEnded() const
{
+ return readyState() == endedKeyword();
+}
+
+void MediaSource::detachFromElement(HTMLMediaElement& element)
+{
+ ASSERT_UNUSED(element, m_mediaElement == &element);
+
+ // 2.4.2 Detaching from a media element
+ // https://rawgit.com/w3c/media-source/45627646344eea0170dd1cbc5a3d508ca751abb8/media-source-respec.html#mediasource-detach
+
+ // 1. Set the readyState attribute to "closed".
+ // 7. Queue a task to fire a simple event named sourceclose at the MediaSource.
setReadyState(closedKeyword());
+
+ // 2. Update duration to NaN.
+ m_duration = MediaTime::invalidTime();
+
+ // 3. Remove all the SourceBuffer objects from activeSourceBuffers.
+ // 4. Queue a task to fire a simple event named removesourcebuffer at activeSourceBuffers.
+ while (m_activeSourceBuffers->length())
+ removeSourceBuffer(*m_activeSourceBuffers->item(0));
+
+ // 5. Remove all the SourceBuffer objects from sourceBuffers.
+ // 6. Queue a task to fire a simple event named removesourcebuffer at sourceBuffers.
+ while (m_sourceBuffers->length())
+ removeSourceBuffer(*m_sourceBuffers->item(0));
+
+ m_private = nullptr;
+ m_mediaElement = nullptr;
}
-void MediaSource::sourceBufferDidChangeAcitveState(SourceBuffer* sourceBuffer, bool active)
+void MediaSource::sourceBufferDidChangeActiveState(SourceBuffer&, bool)
{
- if (active && !m_activeSourceBuffers->contains(sourceBuffer))
- m_activeSourceBuffers->add(sourceBuffer);
- else if (!active && m_activeSourceBuffers->contains(sourceBuffer))
- m_activeSourceBuffers->remove(sourceBuffer);
+ regenerateActiveSourceBuffers();
}
-bool MediaSource::attachToElement(HTMLMediaElement* element)
+bool MediaSource::attachToElement(HTMLMediaElement& element)
{
if (m_mediaElement)
return false;
ASSERT(isClosed());
- m_mediaElement = element;
+ m_mediaElement = &element;
return true;
}
@@ -670,13 +936,27 @@ bool MediaSource::hasPendingActivity() const
void MediaSource::stop()
{
m_asyncEventQueue.close();
- if (!isClosed())
- setReadyState(closedKeyword());
- m_private.clear();
+ if (m_mediaElement)
+ m_mediaElement->detachMediaSource();
+ m_readyState = closedKeyword();
+ m_private = nullptr;
+}
+
+bool MediaSource::canSuspendForDocumentSuspension() const
+{
+ return isClosed() && !m_asyncEventQueue.hasPendingEvents();
+}
+
+const char* MediaSource::activeDOMObjectName() const
+{
+ return "MediaSource";
}
void MediaSource::onReadyStateChange(const AtomicString& oldState, const AtomicString& newState)
{
+ for (auto& buffer : *m_sourceBuffers)
+ buffer->readyStateChanged();
+
if (isOpen()) {
scheduleEvent(eventNames().sourceopenEvent);
return;
@@ -688,58 +968,46 @@ void MediaSource::onReadyStateChange(const AtomicString& oldState, const AtomicS
}
ASSERT(isClosed());
-
- m_activeSourceBuffers->clear();
-
- // Clear SourceBuffer references to this object.
- for (unsigned long i = 0, length = m_sourceBuffers->length(); i < length; ++i)
- m_sourceBuffers->item(i)->removedFromMediaSource();
- m_sourceBuffers->clear();
-
scheduleEvent(eventNames().sourcecloseEvent);
}
-Vector<RefPtr<TimeRanges>> MediaSource::activeRanges() const
+Vector<PlatformTimeRanges> MediaSource::activeRanges() const
{
- Vector<RefPtr<TimeRanges>> activeRanges(m_activeSourceBuffers->length());
- for (size_t i = 0, length = m_activeSourceBuffers->length(); i < length; ++i)
- activeRanges[i] = m_activeSourceBuffers->item(i)->buffered(ASSERT_NO_EXCEPTION);
-
+ Vector<PlatformTimeRanges> activeRanges;
+ for (auto& sourceBuffer : *m_activeSourceBuffers)
+ activeRanges.append(sourceBuffer->bufferedInternal().ranges());
return activeRanges;
}
-RefPtr<SourceBufferPrivate> MediaSource::createSourceBufferPrivate(const ContentType& type, ExceptionCode& ec)
+ExceptionOr<Ref<SourceBufferPrivate>> MediaSource::createSourceBufferPrivate(const ContentType& type)
{
RefPtr<SourceBufferPrivate> sourceBufferPrivate;
switch (m_private->addSourceBuffer(type, sourceBufferPrivate)) {
- case MediaSourcePrivate::Ok: {
- return sourceBufferPrivate;
- }
+ case MediaSourcePrivate::Ok:
+ return sourceBufferPrivate.releaseNonNull();
case MediaSourcePrivate::NotSupported:
// 2.2 https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#widl-MediaSource-addSourceBuffer-SourceBuffer-DOMString-type
// Step 2: If type contains a MIME type ... that is not supported with the types
// specified for the other SourceBuffer objects in sourceBuffers, then throw
// a NOT_SUPPORTED_ERR exception and abort these steps.
- ec = NOT_SUPPORTED_ERR;
- return nullptr;
+ return Exception { NOT_SUPPORTED_ERR };
case MediaSourcePrivate::ReachedIdLimit:
// 2.2 https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#widl-MediaSource-addSourceBuffer-SourceBuffer-DOMString-type
// Step 3: If the user agent can't handle any more SourceBuffer objects then throw
// a QUOTA_EXCEEDED_ERR exception and abort these steps.
- ec = QUOTA_EXCEEDED_ERR;
- return nullptr;
+ return Exception { QUOTA_EXCEEDED_ERR };
}
ASSERT_NOT_REACHED();
- return nullptr;
+ return Exception { QUOTA_EXCEEDED_ERR };
}
void MediaSource::scheduleEvent(const AtomicString& eventName)
{
- RefPtr<Event> event = Event::create(eventName, false, false);
+ auto event = Event::create(eventName, false, false);
event->setTarget(this);
- m_asyncEventQueue.enqueueEvent(event.release());
+ m_asyncEventQueue.enqueueEvent(WTFMove(event));
}
ScriptExecutionContext* MediaSource::scriptExecutionContext() const
@@ -757,6 +1025,18 @@ URLRegistry& MediaSource::registry() const
return MediaSourceRegistry::registry();
}
+void MediaSource::regenerateActiveSourceBuffers()
+{
+ Vector<RefPtr<SourceBuffer>> newList;
+ for (auto& sourceBuffer : *m_sourceBuffers) {
+ if (sourceBuffer->active())
+ newList.append(sourceBuffer);
+ }
+ m_activeSourceBuffers->swap(newList);
+ for (auto& sourceBuffer : *m_activeSourceBuffers)
+ sourceBuffer->setBufferedDirty(true);
+}
+
}
#endif