return fudgeFactor;
}
+const AtomicString& SourceBuffer::segmentsKeyword()
+{
+ static NeverDestroyed<AtomicString> segments("segments");
+ return segments.get();
+}
+
+const AtomicString& SourceBuffer::sequenceKeyword()
+{
+ static NeverDestroyed<AtomicString> segments("sequence");
+ return segments.get();
+}
+
struct SourceBuffer::TrackBuffer {
MediaTime lastDecodeTimestamp;
MediaTime lastFrameDuration;
, m_private(WTF::move(sourceBufferPrivate))
, m_source(source)
, m_asyncEventQueue(*this)
+ , m_mode(segmentsKeyword())
, m_appendBufferTimer(*this, &SourceBuffer::appendBufferTimerFired)
- , m_highestPresentationEndTimestamp(MediaTime::invalidTime())
+ , m_groupStartTimestamp(MediaTime::invalidTime())
+ , m_groupEndTimestamp(MediaTime::zeroTime())
, m_buffered(TimeRanges::create())
, m_appendState(WaitingForSegment)
, m_timeOfBufferingMonitor(monotonicallyIncreasingTime())
, m_receivedFirstInitializationSegment(false)
, m_active(false)
, m_bufferFull(false)
+ , m_shouldGenerateTimestamps(false)
{
ASSERT(m_source);
return;
}
- // FIXME: Add step 6 text when mode attribute is implemented.
+ MediaTime newTimestampOffset = MediaTime::createWithDouble(offset);
+
+ // 6. If the mode attribute equals "sequence", then set the group start timestamp to new timestamp offset.
+ if (m_mode == sequenceKeyword())
+ m_groupStartTimestamp = newTimestampOffset;
+
// 7. Update the attribute to the new value.
- m_timestampOffset = MediaTime::createWithDouble(offset);
+ m_timestampOffset = newTimestampOffset;
}
void SourceBuffer::appendBuffer(PassRefPtr<ArrayBuffer> data, ExceptionCode& ec)
RefPtr<MediaSample> sample = prpSample;
// 3.5.8 Coded Frame Processing
+ // http://www.w3.org/TR/media-source/#sourcebuffer-coded-frame-processing
+
// When complete coded frames have been parsed by the segment parser loop then the following steps
// are run:
// 1. For each coded frame in the media segment run the following steps:
// 1.1. Loop Top
do {
- // 1.1 (ctd) Let presentation timestamp be a double precision floating point representation of
- // the coded frame's presentation timestamp in seconds.
- MediaTime presentationTimestamp = sample->presentationTime();
-
- // 1.2 Let decode timestamp be a double precision floating point representation of the coded frame's
- // decode timestamp in seconds.
- MediaTime decodeTimestamp = sample->decodeTime();
+ MediaTime presentationTimestamp;
+ MediaTime decodeTimestamp;
+
+ if (m_shouldGenerateTimestamps) {
+ // ↳ If generate timestamps flag equals true:
+ // 1. Let presentation timestamp equal 0.
+ presentationTimestamp = MediaTime::zeroTime();
+
+ // 2. Let decode timestamp equal 0.
+ decodeTimestamp = MediaTime::zeroTime();
+ } else {
+ // ↳ Otherwise:
+ // 1. Let presentation timestamp be a double precision floating point representation of
+ // the coded frame's presentation timestamp in seconds.
+ presentationTimestamp = sample->presentationTime();
+
+ // 2. Let decode timestamp be a double precision floating point representation of the coded frame's
+ // decode timestamp in seconds.
+ decodeTimestamp = sample->decodeTime();
+ }
- // 1.3 Let frame duration be a double precision floating point representation of the coded frame's
+ // 1.2 Let frame duration be a double precision floating point representation of the coded frame's
// duration in seconds.
MediaTime frameDuration = sample->duration();
- // 1.4 If mode equals "sequence" and group start timestamp is set, then run the following steps:
- // FIXME: add support for "sequence" mode
+ // 1.3 If mode equals "sequence" and group start timestamp is set, then run the following steps:
+ if (m_mode == sequenceKeyword()) {
+ // 1.3.1 Set timestampOffset equal to group start timestamp - presentation timestamp.
+ m_timestampOffset = m_groupStartTimestamp;
+
+ // 1.3.2 Set group end timestamp equal to group start timestamp.
+ m_groupEndTimestamp = m_groupStartTimestamp;
+
+ // 1.3.3 Set the need random access point flag on all track buffers to true.
+ for (auto& trackBuffer : m_trackBufferMap.values())
+ trackBuffer.needRandomAccessFlag = true;
+
+ // 1.3.4 Unset group start timestamp.
+ m_groupStartTimestamp = MediaTime::invalidTime();
+ }
- // 1.5 If timestampOffset is not 0, then run the following steps:
+ // 1.4 If timestampOffset is not 0, then run the following steps:
if (m_timestampOffset) {
- // 1.5.1 Add timestampOffset to the presentation timestamp.
+ // 1.4.1 Add timestampOffset to the presentation timestamp.
presentationTimestamp += m_timestampOffset;
- // 1.5.2 Add timestampOffset to the decode timestamp.
+ // 1.4.2 Add timestampOffset to the decode timestamp.
decodeTimestamp += m_timestampOffset;
-
- // 1.5.3 If the presentation timestamp or decode timestamp is less than the presentation start
- // time, then run the end of stream algorithm with the error parameter set to "decode", and
- // abort these steps.
- MediaTime presentationStartTime = MediaTime::zeroTime();
- if (presentationTimestamp < presentationStartTime || decodeTimestamp < presentationStartTime) {
-#if !LOG_DISABLED
- LOG(MediaSource, "SourceBuffer::sourceBufferPrivateDidReceiveSample(%p) - failing because %s", this, presentationTimestamp < presentationStartTime ? "presentationTimestamp < presentationStartTime" : "decodeTimestamp < presentationStartTime");
-#endif
- m_source->streamEndedWithError(decodeError(), IgnorableExceptionCode());
- return;
- }
}
- // 1.6 Let track buffer equal the track buffer that the coded frame will be added to.
+ // 1.5 Let track buffer equal the track buffer that the coded frame will be added to.
AtomicString trackID = sample->trackID();
auto it = m_trackBufferMap.find(trackID);
if (it == m_trackBufferMap.end())
it = m_trackBufferMap.add(trackID, TrackBuffer()).iterator;
TrackBuffer& trackBuffer = it->value;
- // 1.7 If last decode timestamp for track buffer is set and decode timestamp is less than last
+ // 1.6 ↳ If last decode timestamp for track buffer is set and decode timestamp is less than last
// decode timestamp:
// OR
- // If last decode timestamp for track buffer is set and the difference between decode timestamp and
+ // ↳ If last decode timestamp for track buffer is set and the difference between decode timestamp and
// last decode timestamp is greater than 2 times last frame duration:
if (trackBuffer.lastDecodeTimestamp.isValid() && (decodeTimestamp < trackBuffer.lastDecodeTimestamp
|| abs(decodeTimestamp - trackBuffer.lastDecodeTimestamp) > (trackBuffer.lastFrameDuration * 2))) {
- // 1.7.1 If mode equals "segments":
- // Set highest presentation end timestamp to presentation timestamp.
- m_highestPresentationEndTimestamp = presentationTimestamp;
- // If mode equals "sequence":
- // Set group start timestamp equal to the highest presentation end timestamp.
- // FIXME: Add support for "sequence" mode.
+ // 1.6.1:
+ if (m_mode == segmentsKeyword()) {
+ // ↳ If mode equals "segments":
+ // Set group end timestamp to presentation timestamp.
+ m_groupEndTimestamp = presentationTimestamp;
+ } else if (m_mode == sequenceKeyword()) {
+ // ↳ If mode equals "sequence":
+ // Set group start timestamp equal to the group end timestamp.
+ m_groupStartTimestamp = m_groupEndTimestamp;
+ }
for (auto& trackBuffer : m_trackBufferMap.values()) {
- // 1.7.2 Unset the last decode timestamp on all track buffers.
+ // 1.6.2 Unset the last decode timestamp on all track buffers.
trackBuffer.lastDecodeTimestamp = MediaTime::invalidTime();
- // 1.7.3 Unset the last frame duration on all track buffers.
+ // 1.6.3 Unset the last frame duration on all track buffers.
trackBuffer.lastFrameDuration = MediaTime::invalidTime();
- // 1.7.4 Unset the highest presentation timestamp on all track buffers.
+ // 1.6.4 Unset the highest presentation timestamp on all track buffers.
trackBuffer.highestPresentationTimestamp = MediaTime::invalidTime();
- // 1.7.5 Set the need random access point flag on all track buffers to true.
+ // 1.6.5 Set the need random access point flag on all track buffers to true.
trackBuffer.needRandomAccessFlag = true;
}
- // 1.7.6 Jump to the Loop Top step above to restart processing of the current coded frame.
+ // 1.6.6 Jump to the Loop Top step above to restart processing of the current coded frame.
continue;
}
- // 1.8 Let frame end timestamp equal the sum of presentation timestamp and frame duration.
+ // 1.7 Let frame end timestamp equal the sum of presentation timestamp and frame duration.
MediaTime frameEndTimestamp = presentationTimestamp + frameDuration;
- // 1.9 If presentation timestamp is less than appendWindowStart, then set the need random access
+ // 1.8 If presentation timestamp is less than appendWindowStart, then set the need random access
// point flag to true, drop the coded frame, and jump to the top of the loop to start processing
// the next coded frame.
- // 1.10 If frame end timestamp is greater than appendWindowEnd, then set the need random access
+ // 1.9 If frame end timestamp is greater than appendWindowEnd, then set the need random access
// point flag to true, drop the coded frame, and jump to the top of the loop to start processing
// the next coded frame.
// FIXME: implement append windows
+ // 1.10 If the decode timestamp is less than the presentation start time, then run the end of stream
+ // algorithm with the error parameter set to "decode", and abort these steps.
+ // NOTE: Until <https://www.w3.org/Bugs/Public/show_bug.cgi?id=27487> is resolved, we will only check
+ // the presentation timestamp.
+ MediaTime presentationStartTime = MediaTime::zeroTime();
+ if (presentationTimestamp < presentationStartTime) {
+ LOG(MediaSource, "SourceBuffer::sourceBufferPrivateDidReceiveSample(%p) - failing because presentationTimestamp < presentationStartTime", this);
+ m_source->streamEndedWithError(decodeError(), IgnorableExceptionCode());
+ return;
+ }
+
// 1.11 If the need random access point flag on track buffer equals true, then run the following steps:
if (trackBuffer.needRandomAccessFlag) {
// 1.11.1 If the coded frame is not a random access point, then drop the coded frame and jump
erasedSamples.addRange(iter_pair.first, iter_pair.second);
}
- // If highest presentation timestamp for track buffer is set and less than presentation timestamp
+ // If highest presentation timestamp for track buffer is set and less than or equal to presentation timestamp
if (trackBuffer.highestPresentationTimestamp.isValid() && trackBuffer.highestPresentationTimestamp <= presentationTimestamp) {
// Remove all coded frames from track buffer that have a presentation timestamp greater than highest
// presentation timestamp and less than or equal to frame end timestamp.
if (!bufferedLength)
break;
- bool ignoreValid;
- MediaTime highestBufferedTime = m_buffered->ranges().end(bufferedLength - 1, ignoreValid);
+ MediaTime highestBufferedTime = m_buffered->ranges().maximumBufferedTime();
PresentationOrderSampleMap::iterator_range range;
if (highestBufferedTime - trackBuffer.highestPresentationTimestamp < trackBuffer.lastFrameDuration)
if (trackBuffer.highestPresentationTimestamp.isInvalid() || frameEndTimestamp > trackBuffer.highestPresentationTimestamp)
trackBuffer.highestPresentationTimestamp = frameEndTimestamp;
- // 1.21 If highest presentation end timestamp is unset or frame end timestamp is greater than highest
- // presentation end timestamp, then set highest presentation end timestamp equal to frame end timestamp.
- if (m_highestPresentationEndTimestamp.isInvalid() || frameEndTimestamp > m_highestPresentationEndTimestamp)
- m_highestPresentationEndTimestamp = frameEndTimestamp;
+ // 1.21 If frame end timestamp is greater than group end timestamp, then set group end timestamp equal
+ // to frame end timestamp.
+ if (m_groupEndTimestamp.isInvalid() || frameEndTimestamp > m_groupEndTimestamp)
+ m_groupEndTimestamp = frameEndTimestamp;
+
+ // 1.22 If generate timestamps flag equals true, then set timestampOffset equal to frame end timestamp.
+ if (m_shouldGenerateTimestamps)
+ m_timestampOffset = frameEndTimestamp;
m_buffered->add(presentationTimestamp.toDouble(), (presentationTimestamp + frameDuration + microsecond).toDouble());
m_bufferedSinceLastMonitor += frameDuration.toDouble();
// Steps 2-4 will be handled by MediaSource::monitorSourceBuffers()
// 5. If the media segment contains data beyond the current duration, then run the duration change algorithm with new
- // duration set to the maximum of the current duration and the highest end timestamp reported by HTMLMediaElement.buffered.
- if (highestPresentationEndTimestamp() > m_source->duration())
- m_source->setDurationInternal(highestPresentationEndTimestamp());
+ // duration set to the maximum of the current duration and the group end timestamp.
+ if (m_groupEndTimestamp > m_source->duration())
+ m_source->setDurationInternal(m_groupEndTimestamp);
}
bool SourceBuffer::hasAudio() const
return downcast<Document>(*scriptExecutionContext());
}
+void SourceBuffer::setMode(const AtomicString& newMode, ExceptionCode& ec)
+{
+ // 3.1 Attributes - mode
+ // http://www.w3.org/TR/media-source/#widl-SourceBuffer-mode
+
+ // On setting, run the following steps:
+
+ // 1. Let new mode equal the new value being assigned to this attribute.
+ // 2. If generate timestamps flag equals true and new mode equals "segments", then throw an INVALID_ACCESS_ERR exception and abort these steps.
+ if (m_shouldGenerateTimestamps && newMode == segmentsKeyword()) {
+ ec = INVALID_ACCESS_ERR;
+ return;
+ }
+
+ // 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.
+ // 4. If the updating attribute equals true, then throw an INVALID_STATE_ERR exception and abort these steps.
+ if (isRemoved() || m_updating) {
+ ec = INVALID_STATE_ERR;
+ return;
+ }
+
+ // 5. If the readyState attribute of the parent media source is in the "ended" state then run the following steps:
+ if (m_source->readyState() == MediaSource::endedKeyword()) {
+ // 5.1. Set the readyState attribute of the parent media source to "open"
+ // 5.2. Queue a task to fire a simple event named sourceopen at the parent media source.
+ m_source->openIfInEndedState();
+ }
+
+ // 6. If the append state equals PARSING_MEDIA_SEGMENT, then throw an INVALID_STATE_ERR and abort these steps.
+ if (m_appendState == ParsingMediaSegment) {
+ ec = INVALID_STATE_ERR;
+ return;
+ }
+
+ // 7. If the new mode equals "sequence", then set the group start timestamp to the group end timestamp.
+ if (newMode == sequenceKeyword())
+ m_groupStartTimestamp = m_groupEndTimestamp;
+
+ // 8. Update the attribute to new mode.
+ m_mode = newMode;
+}
+
} // namespace WebCore
#endif