<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[171995] trunk/Source/WebCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/171995">171995</a></dd>
<dt>Author</dt> <dd>jer.noble@apple.com</dd>
<dt>Date</dt> <dd>2014-08-04 10:26:42 -0700 (Mon, 04 Aug 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>[MSE] Re-enqueing due to overlapping appended samples can cause stuttering playback
https://bugs.webkit.org/show_bug.cgi?id=135424

Reviewed by Eric Carlson.

If it become necessary to re-enqueue samples (due to appending overlapping samples which cause
existing samples to be removed), the previous behavior was to flush and re-enqueue the new
samples dependencies; i.e., everything up to and including the previous sync sample. This causes
the decoder to visibly stall while it decodes those non-displaying samples, which could be
a second or more worth of encoded video samples, depending on the frequency of sync samples.

Instead, when we are asked to re-enqueue, we will look for the next occurring sync sample.
If found, we can switch over to the replacement samples at that point in the decode queue.
This limits the overhead of a stream switch, and should allow for a visually seamless switch,
at the cost of having to wait for the next sync sample to occur to affect the switch.

* Modules/mediasource/SourceBuffer.cpp:
(WebCore::SourceBuffer::seekToTime): Clear the decode queue when seeking.
(WebCore::SourceBuffer::sourceBufferPrivateAppendComplete): Call reenqueueMediaForCurrentTime.
(WebCore::SourceBuffer::reenqueueMediaForCurrentTime): Switch over to the new stream only
    at the next sync sample.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreModulesmediasourceSourceBuffercpp">trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp</a></li>
<li><a href="#trunkSourceWebCoreModulesmediasourceSourceBufferh">trunk/Source/WebCore/Modules/mediasource/SourceBuffer.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (171994 => 171995)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2014-08-04 17:23:49 UTC (rev 171994)
+++ trunk/Source/WebCore/ChangeLog        2014-08-04 17:26:42 UTC (rev 171995)
</span><span class="lines">@@ -1,3 +1,27 @@
</span><ins>+2014-08-04  Jer Noble  &lt;jer.noble@apple.com&gt;
+
+        [MSE] Re-enqueing due to overlapping appended samples can cause stuttering playback
+        https://bugs.webkit.org/show_bug.cgi?id=135424
+
+        Reviewed by Eric Carlson.
+
+        If it become necessary to re-enqueue samples (due to appending overlapping samples which cause
+        existing samples to be removed), the previous behavior was to flush and re-enqueue the new
+        samples dependencies; i.e., everything up to and including the previous sync sample. This causes
+        the decoder to visibly stall while it decodes those non-displaying samples, which could be
+        a second or more worth of encoded video samples, depending on the frequency of sync samples.
+
+        Instead, when we are asked to re-enqueue, we will look for the next occurring sync sample.
+        If found, we can switch over to the replacement samples at that point in the decode queue.
+        This limits the overhead of a stream switch, and should allow for a visually seamless switch,
+        at the cost of having to wait for the next sync sample to occur to affect the switch.
+
+        * Modules/mediasource/SourceBuffer.cpp:
+        (WebCore::SourceBuffer::seekToTime): Clear the decode queue when seeking.
+        (WebCore::SourceBuffer::sourceBufferPrivateAppendComplete): Call reenqueueMediaForCurrentTime.
+        (WebCore::SourceBuffer::reenqueueMediaForCurrentTime): Switch over to the new stream only
+            at the next sync sample.
+
</ins><span class="cx"> 2014-08-04  Chris Fleizach  &lt;cfleizach@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         AX: The Dictation command &quot;Replace &lt;phrase&gt; with &lt;phrase&gt;&quot; always capitalizes the replacement string
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasourceSourceBuffercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp (171994 => 171995)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp        2014-08-04 17:23:49 UTC (rev 171994)
+++ trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp        2014-08-04 17:26:42 UTC (rev 171995)
</span><span class="lines">@@ -317,6 +317,7 @@
</span><span class="cx">         TrackBuffer&amp; trackBuffer = trackBufferPair.value;
</span><span class="cx">         const AtomicString&amp; trackID = trackBufferPair.key;
</span><span class="cx"> 
</span><ins>+        trackBuffer.decodeQueue.clear();
</ins><span class="cx">         reenqueueMediaForTime(trackBuffer, trackID, time);
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="lines">@@ -498,14 +499,13 @@
</span><span class="cx">     if (m_source)
</span><span class="cx">         m_source-&gt;monitorSourceBuffers();
</span><span class="cx"> 
</span><del>-    MediaTime currentMediaTime = MediaTime::createWithDouble(m_source-&gt;currentTime());
</del><span class="cx">     for (auto&amp; trackBufferPair : m_trackBufferMap) {
</span><span class="cx">         TrackBuffer&amp; trackBuffer = trackBufferPair.value;
</span><span class="cx">         const AtomicString&amp; trackID = trackBufferPair.key;
</span><span class="cx"> 
</span><span class="cx">         if (trackBuffer.needsReenqueueing) {
</span><del>-            LOG(MediaSource, &quot;SourceBuffer::sourceBufferPrivateAppendComplete(%p) - reenqueuing at time (%s)&quot;, this, toString(currentMediaTime).utf8().data());
-            reenqueueMediaForTime(trackBuffer, trackID, currentMediaTime);
</del><ins>+            LOG(MediaSource, &quot;SourceBuffer::sourceBufferPrivateAppendComplete(%p) - reenqueuing&quot;, this);
+            reenqueueMediaForCurrentTime(trackBuffer, trackID);
</ins><span class="cx">         } else
</span><span class="cx">             provideMediaData(trackBuffer, trackID);
</span><span class="cx">     }
</span><span class="lines">@@ -1393,6 +1393,33 @@
</span><span class="cx">     LOG(MediaSource, &quot;SourceBuffer::provideMediaData(%p) - Enqueued %u samples&quot;, this, enqueuedSamples);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void SourceBuffer::reenqueueMediaForCurrentTime(TrackBuffer&amp; trackBuffer, AtomicString trackID)
+{
+    if (!trackBuffer.decodeQueue.empty()) {
+        // If the decodeQueue is not empty, attempt to find the next sync sample after the last enqueued presentation time.
+        auto nextSyncSampleIter = trackBuffer.samples.decodeOrder().findSyncSampleAfterPresentationTime(trackBuffer.lastEnqueuedPresentationTime);
+
+        auto decodeEnd = trackBuffer.samples.decodeOrder().end();
+        if (nextSyncSampleIter != decodeEnd) {
+            // If a sync sample is found, remove all existing samples from the decode queue whose decodeTimestamps are
+            // greater-than-or-equal-to the sync sample's decodeTimestamp.
+            auto firstEnqueuedSampleToRemoveIter = trackBuffer.decodeQueue.lower_bound(nextSyncSampleIter-&gt;first);
+            trackBuffer.decodeQueue.erase(firstEnqueuedSampleToRemoveIter, trackBuffer.decodeQueue.end());
+
+            // Add the replacement samples, starting from the sync sample, to the decode queue.
+            for (auto iter = nextSyncSampleIter; iter != decodeEnd; ++iter)
+                trackBuffer.decodeQueue.insert(*iter);
+            trackBuffer.needsReenqueueing = false;
+
+            // And provide those replacement samples to the decoder.
+            provideMediaData(trackBuffer, trackID);
+            return;
+        }
+    }
+
+    reenqueueMediaForTime(trackBuffer, trackID, MediaTime::createWithDouble(m_source-&gt;currentTime()));
+}
+
</ins><span class="cx"> void SourceBuffer::reenqueueMediaForTime(TrackBuffer&amp; trackBuffer, AtomicString trackID, const MediaTime&amp; time)
</span><span class="cx"> {
</span><span class="cx">     // Find the sample which contains the current presentation time.
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasourceSourceBufferh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasource/SourceBuffer.h (171994 => 171995)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasource/SourceBuffer.h        2014-08-04 17:23:49 UTC (rev 171994)
+++ trunk/Source/WebCore/Modules/mediasource/SourceBuffer.h        2014-08-04 17:26:42 UTC (rev 171995)
</span><span class="lines">@@ -150,6 +150,7 @@
</span><span class="cx">     bool validateInitializationSegment(const InitializationSegment&amp;);
</span><span class="cx"> 
</span><span class="cx">     struct TrackBuffer;
</span><ins>+    void reenqueueMediaForCurrentTime(TrackBuffer&amp;, AtomicString trackID);
</ins><span class="cx">     void reenqueueMediaForTime(TrackBuffer&amp;, AtomicString trackID, const MediaTime&amp;);
</span><span class="cx">     void provideMediaData(TrackBuffer&amp;, AtomicString trackID);
</span><span class="cx">     void didDropSample();
</span></span></pre>
</div>
</div>

</body>
</html>