<!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>[170932] trunk</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/170932">170932</a></dd>
<dt>Author</dt> <dd>jer.noble@apple.com</dd>
<dt>Date</dt> <dd>2014-07-09 14:37:17 -0700 (Wed, 09 Jul 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Source/WebCore: [MSE] http/tests/media/media-source/mediasource-remove.html is failing
https://bugs.webkit.org/show_bug.cgi?id=134768

Reviewed by Eric Carlson.

Fix multiple bugs causing mediasource-remove.html to fail:

Separate out setDuration() into setDurationInternal() so that steps which require
us to run the &quot;duration change algorithm&quot; don't bail out if the SoureBuffer is
inside updating().

* Modules/mediasource/MediaSource.cpp:
(WebCore::MediaSource::setDuration):
(WebCore::MediaSource::setDurationInternal):
* Modules/mediasource/MediaSource.h:
* Modules/mediasource/SourceBuffer.cpp:
(WebCore::SourceBuffer::sourceBufferPrivateDidReceiveInitializationSegment):
(WebCore::SourceBuffer::sourceBufferPrivateDidReceiveSample):

When removing coded frames, do so starting from the first sample in the range in
decode order, so that frames dependant on removed frames are themselves removed. Add
a convenience method in SampleMap findSampleWithPresentationTime(), and rename
findSampleAfterPresentationTime() to findSampleOnOrAfterPresentationTime() to correctly
reflect what the method does, and simplify its implementation by searching the map's keys
directly.

* Modules/mediasource/SampleMap.cpp:
(WebCore::PresentationOrderSampleMap::findSampleWithPresentationTime):
(WebCore::PresentationOrderSampleMap::findSampleOnOrAfterPresentationTime):
(WebCore::DecodeOrderSampleMap::findSyncSampleAfterPresentationTime):
(WebCore::PresentationOrderSampleMap::findSampleAfterPresentationTime): Deleted.
* Modules/mediasource/SampleMap.h:
* Modules/mediasource/SourceBuffer.cpp:
(WebCore::decodeTimeComparator):
(WebCore::SourceBuffer::removeCodedFrames):

Throw the correct exception (INVALID_STATE_ERR) from SourceBuffer::remove().

* Modules/mediasource/SourceBuffer.cpp:
(WebCore::SourceBuffer::remove):

LayoutTests: [MSE] http/tests/media/media-source/mediasource-remove.html is failing.
https://bugs.webkit.org/show_bug.cgi?id=134768

Reviewed by Eric Carlson.

Update mediasource-remove.html with the correct locations of sync-samples.

* http/tests/media/media-source/mediasource-remove.html:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkLayoutTestshttptestsmediamediasourcemediasourceremovehtml">trunk/LayoutTests/http/tests/media/media-source/mediasource-remove.html</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreModulesmediasourceMediaSourcecpp">trunk/Source/WebCore/Modules/mediasource/MediaSource.cpp</a></li>
<li><a href="#trunkSourceWebCoreModulesmediasourceMediaSourceh">trunk/Source/WebCore/Modules/mediasource/MediaSource.h</a></li>
<li><a href="#trunkSourceWebCoreModulesmediasourceSampleMapcpp">trunk/Source/WebCore/Modules/mediasource/SampleMap.cpp</a></li>
<li><a href="#trunkSourceWebCoreModulesmediasourceSampleMaph">trunk/Source/WebCore/Modules/mediasource/SampleMap.h</a></li>
<li><a href="#trunkSourceWebCoreModulesmediasourceSourceBuffercpp">trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (170931 => 170932)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2014-07-09 21:36:16 UTC (rev 170931)
+++ trunk/LayoutTests/ChangeLog        2014-07-09 21:37:17 UTC (rev 170932)
</span><span class="lines">@@ -1,3 +1,14 @@
</span><ins>+2014-06-28  Jer Noble  &lt;jer.noble@apple.com&gt;
+
+        [MSE] http/tests/media/media-source/mediasource-remove.html is failing.
+        https://bugs.webkit.org/show_bug.cgi?id=134768
+
+        Reviewed by Eric Carlson.
+
+        Update mediasource-remove.html with the correct locations of sync-samples.
+
+        * http/tests/media/media-source/mediasource-remove.html:
+
</ins><span class="cx"> 2014-07-09  Antti Koivisto  &lt;antti@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         fast/dom/HTMLImageElement/sizes/image-sizes-js-change.html is failing in some bots
</span></span></pre></div>
<a id="trunkLayoutTestshttptestsmediamediasourcemediasourceremovehtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/http/tests/media/media-source/mediasource-remove.html (170931 => 170932)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/http/tests/media/media-source/mediasource-remove.html        2014-07-09 21:36:16 UTC (rev 170931)
+++ trunk/LayoutTests/http/tests/media/media-source/mediasource-remove.html        2014-07-09 21:37:17 UTC (rev 170932)
</span><span class="lines">@@ -187,7 +187,7 @@
</span><span class="cx">           {
</span><span class="cx">               var expectations = {
</span><span class="cx">                 webm: (&quot;{ [3.187, &quot; + duration + &quot;) }&quot;),
</span><del>-                mp4: (&quot;{ [3.021, &quot; + duration + &quot;) }&quot;),
</del><ins>+                mp4: (&quot;{ [3.228, &quot; + duration + &quot;) }&quot;),
</ins><span class="cx">               };
</span><span class="cx"> 
</span><span class="cx">               // Note: Range doesn't start exactly at the end of the remove range because there isn't
</span><span class="lines">@@ -199,7 +199,7 @@
</span><span class="cx">           {
</span><span class="cx">               var expectations = {
</span><span class="cx">                 webm: (&quot;{ [0.000, 1.012) [3.187, &quot; + duration + &quot;) }&quot;),
</span><del>-                mp4: (&quot;{ [0.000, 1.022) [3.021, &quot; + duration + &quot;) }&quot;),
</del><ins>+                mp4: (&quot;{ [0.000, 1.029) [3.228, &quot; + duration + &quot;) }&quot;),
</ins><span class="cx">               };
</span><span class="cx"> 
</span><span class="cx">               // Note: The first resulting range ends slightly after start because the removal algorithm only removes
</span><span class="lines">@@ -212,7 +212,7 @@
</span><span class="cx">           {
</span><span class="cx">               var expectations = {
</span><span class="cx">                 webm: &quot;{ [0.000, 1.012) }&quot;,
</span><del>-                mp4: &quot;{ [0.000, 1.022) }&quot;,
</del><ins>+                mp4: &quot;{ [0.000, 1.029) }&quot;,
</ins><span class="cx">               };
</span><span class="cx"> 
</span><span class="cx">               removeAndCheckBufferedRanges(test, sourceBuffer, 1, Number.POSITIVE_INFINITY, expectations[subType]);
</span></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (170931 => 170932)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2014-07-09 21:36:16 UTC (rev 170931)
+++ trunk/Source/WebCore/ChangeLog        2014-07-09 21:37:17 UTC (rev 170932)
</span><span class="lines">@@ -1,3 +1,46 @@
</span><ins>+2014-06-28  Jer Noble  &lt;jer.noble@apple.com&gt;
+
+        [MSE] http/tests/media/media-source/mediasource-remove.html is failing
+        https://bugs.webkit.org/show_bug.cgi?id=134768
+
+        Reviewed by Eric Carlson.
+
+        Fix multiple bugs causing mediasource-remove.html to fail:
+
+        Separate out setDuration() into setDurationInternal() so that steps which require
+        us to run the &quot;duration change algorithm&quot; don't bail out if the SoureBuffer is
+        inside updating().
+
+        * Modules/mediasource/MediaSource.cpp:
+        (WebCore::MediaSource::setDuration):
+        (WebCore::MediaSource::setDurationInternal):
+        * Modules/mediasource/MediaSource.h:
+        * Modules/mediasource/SourceBuffer.cpp:
+        (WebCore::SourceBuffer::sourceBufferPrivateDidReceiveInitializationSegment):
+        (WebCore::SourceBuffer::sourceBufferPrivateDidReceiveSample):
+
+        When removing coded frames, do so starting from the first sample in the range in
+        decode order, so that frames dependant on removed frames are themselves removed. Add
+        a convenience method in SampleMap findSampleWithPresentationTime(), and rename
+        findSampleAfterPresentationTime() to findSampleOnOrAfterPresentationTime() to correctly
+        reflect what the method does, and simplify its implementation by searching the map's keys
+        directly.
+
+        * Modules/mediasource/SampleMap.cpp:
+        (WebCore::PresentationOrderSampleMap::findSampleWithPresentationTime):
+        (WebCore::PresentationOrderSampleMap::findSampleOnOrAfterPresentationTime):
+        (WebCore::DecodeOrderSampleMap::findSyncSampleAfterPresentationTime):
+        (WebCore::PresentationOrderSampleMap::findSampleAfterPresentationTime): Deleted.
+        * Modules/mediasource/SampleMap.h:
+        * Modules/mediasource/SourceBuffer.cpp:
+        (WebCore::decodeTimeComparator):
+        (WebCore::SourceBuffer::removeCodedFrames):
+
+        Throw the correct exception (INVALID_STATE_ERR) from SourceBuffer::remove().
+
+        * Modules/mediasource/SourceBuffer.cpp:
+        (WebCore::SourceBuffer::remove):
+
</ins><span class="cx"> 2014-07-09  Pratik Solanki  &lt;psolanki@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Add SharedBuffer::wrapCFDataArray() and use it
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasourceMediaSourcecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasource/MediaSource.cpp (170931 => 170932)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasource/MediaSource.cpp        2014-07-09 21:36:16 UTC (rev 170931)
+++ trunk/Source/WebCore/Modules/mediasource/MediaSource.cpp        2014-07-09 21:37:17 UTC (rev 170932)
</span><span class="lines">@@ -278,6 +278,11 @@
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     // 4. Run the duration change algorithm with new duration set to the value being assigned to this attribute.
</span><ins>+    setDurationInternal(duration);
+}
+
+void MediaSource::setDurationInternal(double duration)
+{
</ins><span class="cx">     m_private-&gt;setDuration(MediaTime::createWithDouble(duration));
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasourceMediaSourceh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasource/MediaSource.h (170931 => 170932)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasource/MediaSource.h        2014-07-09 21:36:16 UTC (rev 170931)
+++ trunk/Source/WebCore/Modules/mediasource/MediaSource.h        2014-07-09 21:37:17 UTC (rev 170932)
</span><span class="lines">@@ -79,6 +79,7 @@
</span><span class="cx">     void monitorSourceBuffers();
</span><span class="cx"> 
</span><span class="cx">     void setDuration(double, ExceptionCode&amp;);
</span><ins>+    void setDurationInternal(double);
</ins><span class="cx">     double currentTime() const;
</span><span class="cx">     const AtomicString&amp; readyState() const { return m_readyState; }
</span><span class="cx">     void setReadyState(const AtomicString&amp;);
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasourceSampleMapcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasource/SampleMap.cpp (170931 => 170932)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasource/SampleMap.cpp        2014-07-09 21:36:16 UTC (rev 170931)
+++ trunk/Source/WebCore/Modules/mediasource/SampleMap.cpp        2014-07-09 21:37:17 UTC (rev 170932)
</span><span class="lines">@@ -122,6 +122,14 @@
</span><span class="cx">     m_totalSize -= sample-&gt;sizeInBytes();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+PresentationOrderSampleMap::iterator PresentationOrderSampleMap::findSampleWithPresentationTime(const MediaTime&amp; time)
+{
+    auto range = m_samples.equal_range(time);
+    if (range.first == range.second)
+        return end();
+    return range.first;
+}
+
</ins><span class="cx"> PresentationOrderSampleMap::iterator PresentationOrderSampleMap::findSampleContainingPresentationTime(const MediaTime&amp; time)
</span><span class="cx"> {
</span><span class="cx">     auto range = std::equal_range(begin(), end(), time, SampleIsLessThanMediaTimeComparator&lt;MapType&gt;());
</span><span class="lines">@@ -130,9 +138,9 @@
</span><span class="cx">     return range.first;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-PresentationOrderSampleMap::iterator PresentationOrderSampleMap::findSampleAfterPresentationTime(const MediaTime&amp; time)
</del><ins>+PresentationOrderSampleMap::iterator PresentationOrderSampleMap::findSampleOnOrAfterPresentationTime(const MediaTime&amp; time)
</ins><span class="cx"> {
</span><del>-    return std::lower_bound(begin(), end(), time, SampleIsLessThanMediaTimeComparator&lt;MapType&gt;());
</del><ins>+    return m_samples.lower_bound(time);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> DecodeOrderSampleMap::iterator DecodeOrderSampleMap::findSampleWithDecodeTime(const MediaTime&amp; time)
</span><span class="lines">@@ -184,7 +192,7 @@
</span><span class="cx"> 
</span><span class="cx"> DecodeOrderSampleMap::iterator DecodeOrderSampleMap::findSyncSampleAfterPresentationTime(const MediaTime&amp; time, const MediaTime&amp; threshold)
</span><span class="cx"> {
</span><del>-    PresentationOrderSampleMap::iterator currentSamplePTS = m_presentationOrder.findSampleAfterPresentationTime(time);
</del><ins>+    PresentationOrderSampleMap::iterator currentSamplePTS = m_presentationOrder.findSampleOnOrAfterPresentationTime(time);
</ins><span class="cx">     if (currentSamplePTS == m_presentationOrder.end())
</span><span class="cx">         return end();
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasourceSampleMaph"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasource/SampleMap.h (170931 => 170932)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasource/SampleMap.h        2014-07-09 21:36:16 UTC (rev 170931)
+++ trunk/Source/WebCore/Modules/mediasource/SampleMap.h        2014-07-09 21:37:17 UTC (rev 170932)
</span><span class="lines">@@ -50,8 +50,9 @@
</span><span class="cx">     reverse_iterator rbegin() { return m_samples.rbegin(); }
</span><span class="cx">     reverse_iterator rend() { return m_samples.rend(); }
</span><span class="cx"> 
</span><ins>+    iterator findSampleWithPresentationTime(const MediaTime&amp;);
</ins><span class="cx">     iterator findSampleContainingPresentationTime(const MediaTime&amp;);
</span><del>-    iterator findSampleAfterPresentationTime(const MediaTime&amp;);
</del><ins>+    iterator findSampleOnOrAfterPresentationTime(const MediaTime&amp;);
</ins><span class="cx">     reverse_iterator reverseFindSampleContainingPresentationTime(const MediaTime&amp;);
</span><span class="cx">     reverse_iterator reverseFindSampleBeforePresentationTime(const MediaTime&amp;);
</span><span class="cx">     iterator_range findSamplesBetweenPresentationTimes(const MediaTime&amp;, const MediaTime&amp;);
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasourceSourceBuffercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp (170931 => 170932)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp        2014-07-09 21:36:16 UTC (rev 170931)
+++ trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp        2014-07-09 21:37:17 UTC (rev 170932)
</span><span class="lines">@@ -243,7 +243,7 @@
</span><span class="cx">     //    InvalidStateError exception and abort these steps.
</span><span class="cx">     // 4. If the updating attribute equals true, then throw an InvalidStateError exception and abort these steps.
</span><span class="cx">     if (isRemoved() || m_updating) {
</span><del>-        ec = INVALID_ACCESS_ERR;
</del><ins>+        ec = INVALID_STATE_ERR;
</ins><span class="cx">         return;
</span><span class="cx">     }
</span><span class="cx"> 
</span><span class="lines">@@ -541,6 +541,11 @@
</span><span class="cx">         m_source-&gt;streamEndedWithError(decodeError(), IgnorableExceptionCode());
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static bool decodeTimeComparator(const PresentationOrderSampleMap::MapType::value_type&amp; a, const PresentationOrderSampleMap::MapType::value_type&amp; b)
+{
+    return a.second-&gt;decodeTime() &lt; b.second-&gt;decodeTime();
+}
+
</ins><span class="cx"> void SourceBuffer::removeCodedFrames(const MediaTime&amp; start, const MediaTime&amp; end)
</span><span class="cx"> {
</span><span class="cx">     // 3.5.9 Coded Frame Removal Algorithm
</span><span class="lines">@@ -561,20 +566,33 @@
</span><span class="cx">         // NOTE: findSyncSampleAfterPresentationTime will return the next sync sample on or after the presentation time
</span><span class="cx">         // or decodeOrder().end() if no sync sample exists after that presentation time.
</span><span class="cx">         DecodeOrderSampleMap::iterator removeDecodeEnd = trackBuffer.samples.decodeOrder().findSyncSampleAfterPresentationTime(end);
</span><ins>+        PresentationOrderSampleMap::iterator removePresentationEnd;
+        if (removeDecodeEnd == trackBuffer.samples.decodeOrder().end())
+            removePresentationEnd = trackBuffer.samples.presentationOrder().end();
+        else
+            removePresentationEnd = trackBuffer.samples.presentationOrder().findSampleWithPresentationTime(removeDecodeEnd-&gt;second-&gt;presentationTime());
</ins><span class="cx"> 
</span><ins>+        PresentationOrderSampleMap::iterator removePresentationStart = trackBuffer.samples.presentationOrder().findSampleOnOrAfterPresentationTime(start);
+        if (removePresentationStart == removePresentationEnd)
+            continue;
+
</ins><span class="cx">         // 3.3 Remove all media data, from this track buffer, that contain starting timestamps greater than or equal to
</span><span class="cx">         // start and less than the remove end timestamp.
</span><span class="cx">         // NOTE: frames must be removed in decode order, so that all dependant frames between the frame to be removed
</span><del>-        // and the next sync sample frame are removed.
-        PresentationOrderSampleMap::iterator removePresentaionStart = trackBuffer.samples.presentationOrder().findSampleAfterPresentationTime(start);
-        DecodeOrderSampleMap::iterator removeDecodeStart = trackBuffer.samples.decodeOrder().findSampleWithDecodeTime(removePresentaionStart-&gt;second-&gt;decodeTime());
</del><ins>+        // and the next sync sample frame are removed. But we must start from the first sample in decode order, not
+        // presentation order.
+        PresentationOrderSampleMap::iterator minDecodeTimeIter = std::min_element(removePresentationStart, removePresentationEnd, decodeTimeComparator);
+        DecodeOrderSampleMap::iterator removeDecodeStart = trackBuffer.samples.decodeOrder().findSampleWithDecodeTime(minDecodeTimeIter-&gt;second-&gt;decodeTime());
+
</ins><span class="cx">         DecodeOrderSampleMap::MapType erasedSamples(removeDecodeStart, removeDecodeEnd);
</span><ins>+
</ins><span class="cx">         RefPtr&lt;TimeRanges&gt; erasedRanges = TimeRanges::create();
</span><span class="cx">         MediaTime microsecond(1, 1000000);
</span><span class="cx">         for (auto erasedIt : erasedSamples) {
</span><del>-            trackBuffer.samples.removeSample(erasedIt.second.get());
-            double startTime = erasedIt.first.toDouble();
-            double endTime = ((erasedIt.first + erasedIt.second-&gt;duration()) + microsecond).toDouble();
</del><ins>+            RefPtr&lt;MediaSample&gt;&amp; sample = erasedIt.second;
+            trackBuffer.samples.removeSample(sample.get());
+            double startTime = sample-&gt;presentationTime().toDouble();
+            double endTime = startTime + (sample-&gt;duration() + microsecond).toDouble();
</ins><span class="cx">             erasedRanges-&gt;add(startTime, endTime);
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="lines">@@ -692,7 +710,7 @@
</span><span class="cx">         // ↳ Otherwise:
</span><span class="cx">         //   Run the duration change algorithm with new duration set to positive Infinity.
</span><span class="cx">         MediaTime newDuration = segment.duration.isValid() ? segment.duration : MediaTime::positiveInfiniteTime();
</span><del>-        m_source-&gt;setDuration(newDuration.toDouble(), IGNORE_EXCEPTION);
</del><ins>+        m_source-&gt;setDurationInternal(newDuration.toDouble());
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     // 2. If the initialization segment has no audio, video, or text tracks, then run the end of stream
</span><span class="lines">@@ -1217,7 +1235,7 @@
</span><span class="cx">     // 5. If the media segment contains data beyond the current duration, then run the duration change algorithm with new
</span><span class="cx">     // duration set to the maximum of the current duration and the highest end timestamp reported by HTMLMediaElement.buffered.
</span><span class="cx">     if (highestPresentationEndTimestamp().toDouble() &gt; m_source-&gt;duration())
</span><del>-        m_source-&gt;setDuration(highestPresentationEndTimestamp().toDouble(), IgnorableExceptionCode());
</del><ins>+        m_source-&gt;setDurationInternal(highestPresentationEndTimestamp().toDouble());
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool SourceBuffer::sourceBufferPrivateHasAudio(const SourceBufferPrivate*) const
</span></span></pre>
</div>
</div>

</body>
</html>