<!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>[164999] 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/164999">164999</a></dd>
<dt>Author</dt> <dd>jer.noble@apple.com</dd>
<dt>Date</dt> <dd>2014-03-03 12:47:52 -0800 (Mon, 03 Mar 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>[MSE] YouTube videos fail to play
https://bugs.webkit.org/show_bug.cgi?id=129525

Reviewed by Darin Adler.

Source/WebCore:

Test: media/media-source/media-source-fudge-factor.html

Add a very simple playability metric to SourceBuffer. Track the number of seconds buffered
and use that metric to determine whether the MediaSource has buffered enough data to play
through.

* Modules/mediasource/MediaSource.cpp:
(WebCore::MediaSource::currentTime): Added simple accessor.
(WebCore::MediaSource::monitorSourceBuffers): Replace the functor-based iterators with lambdas.
(WebCore::MediaSource::addSourceBuffer): Drive-by fix; only add new source buffers to the
    activeSourceBuffers list if those buffers are actually active.
* Modules/mediasource/MediaSource.h:
* Modules/mediasource/SourceBuffer.cpp:
(WebCore::SourceBuffer::SourceBuffer): Initialize new ivars.
(WebCore::SourceBuffer::sourceBufferPrivateDidReceiveSample): Measure the number of seconds buffered.
(WebCore::SourceBuffer::monitorBufferingRate): Use a simple Exponential moving average to
    track the buffering rate.
(WebCore::SourceBuffer::hasCurrentTime): Return true if the current time is within 1/24 s
    of a buffered range.
(WebCore::SourceBuffer::hasFutureTime): Moved from MediaSource.
(WebCore::SourceBuffer::canPlayThrough): Return true if the buffering rate is &gt; 1s per
    second, or if the rate is sufficient to keep up over the remaining time.
* Modules/mediasource/SourceBuffer.h:

LayoutTests:

* media/media-source/media-source-addsourcebuffer.html: Updated.
* media/media-source/media-source-addsourcebuffer-expected.txt: Updated.
* media/media-source/media-source-fudge-factor-expected.txt: Added.
* media/media-source/media-source-fudge-factor.html: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkLayoutTestsmediamediasourcemediasourceaddsourcebufferexpectedtxt">trunk/LayoutTests/media/media-source/media-source-addsourcebuffer-expected.txt</a></li>
<li><a href="#trunkLayoutTestsmediamediasourcemediasourceaddsourcebufferhtml">trunk/LayoutTests/media/media-source/media-source-addsourcebuffer.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="#trunkSourceWebCoreModulesmediasourceSourceBuffercpp">trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp</a></li>
<li><a href="#trunkSourceWebCoreModulesmediasourceSourceBufferh">trunk/Source/WebCore/Modules/mediasource/SourceBuffer.h</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsmediamediasourcemediasourcefudgefactorexpectedtxt">trunk/LayoutTests/media/media-source/media-source-fudge-factor-expected.txt</a></li>
<li><a href="#trunkLayoutTestsmediamediasourcemediasourcefudgefactorhtml">trunk/LayoutTests/media/media-source/media-source-fudge-factor.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (164998 => 164999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2014-03-03 20:24:30 UTC (rev 164998)
+++ trunk/LayoutTests/ChangeLog        2014-03-03 20:47:52 UTC (rev 164999)
</span><span class="lines">@@ -1,3 +1,15 @@
</span><ins>+2014-03-03  Jer Noble  &lt;jer.noble@apple.com&gt;
+
+        [MSE] YouTube videos fail to play
+        https://bugs.webkit.org/show_bug.cgi?id=129525
+
+        Reviewed by Darin Adler.
+
+        * media/media-source/media-source-addsourcebuffer.html: Updated.
+        * media/media-source/media-source-addsourcebuffer-expected.txt: Updated.
+        * media/media-source/media-source-fudge-factor-expected.txt: Added.
+        * media/media-source/media-source-fudge-factor.html: Added.
+
</ins><span class="cx"> 2014-03-03  Bear Travis  &lt;betravis@adobe.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [CSS Shapes] Serialize circle positions
</span></span></pre></div>
<a id="trunkLayoutTestsmediamediasourcemediasourceaddsourcebufferexpectedtxt"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/media/media-source/media-source-addsourcebuffer-expected.txt (164998 => 164999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/media/media-source/media-source-addsourcebuffer-expected.txt        2014-03-03 20:24:30 UTC (rev 164998)
+++ trunk/LayoutTests/media/media-source/media-source-addsourcebuffer-expected.txt        2014-03-03 20:47:52 UTC (rev 164999)
</span><span class="lines">@@ -7,6 +7,5 @@
</span><span class="cx"> EVENT(sourceopen)
</span><span class="cx"> RUN(sourceBuffer = source.addSourceBuffer(&quot;video/mock; codecs=mock&quot;))
</span><span class="cx"> EXPECTED (source.sourceBuffers[0] == '[object SourceBuffer]') OK
</span><del>-EXPECTED (source.activeSourceBuffers[0] == '[object SourceBuffer]') OK
</del><span class="cx"> END OF TEST
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkLayoutTestsmediamediasourcemediasourceaddsourcebufferhtml"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/media/media-source/media-source-addsourcebuffer.html (164998 => 164999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/media/media-source/media-source-addsourcebuffer.html        2014-03-03 20:24:30 UTC (rev 164998)
+++ trunk/LayoutTests/media/media-source/media-source-addsourcebuffer.html        2014-03-03 20:47:52 UTC (rev 164999)
</span><span class="lines">@@ -27,7 +27,6 @@
</span><span class="cx">     function sourceOpen() {
</span><span class="cx">         run('sourceBuffer = source.addSourceBuffer(&quot;video/mock; codecs=mock&quot;)');
</span><span class="cx">         testExpected('source.sourceBuffers[0]', sourceBuffer);
</span><del>-        testExpected('source.activeSourceBuffers[0]', sourceBuffer);
</del><span class="cx">         endTest();
</span><span class="cx">     }
</span><span class="cx">     
</span><span class="lines">@@ -36,4 +35,4 @@
</span><span class="cx"> &lt;body onload=&quot;runTest()&quot;&gt;
</span><span class="cx">     &lt;video&gt;&lt;/video&gt;
</span><span class="cx"> &lt;/body&gt;
</span><del>-&lt;/html&gt;
</del><span class="cx">\ No newline at end of file
</span><ins>+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkLayoutTestsmediamediasourcemediasourcefudgefactorexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/media/media-source/media-source-fudge-factor-expected.txt (0 => 164999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/media/media-source/media-source-fudge-factor-expected.txt                                (rev 0)
+++ trunk/LayoutTests/media/media-source/media-source-fudge-factor-expected.txt        2014-03-03 20:47:52 UTC (rev 164999)
</span><span class="lines">@@ -0,0 +1,15 @@
</span><ins>+
+RUN(video.src = URL.createObjectURL(source))
+EVENT(loadedmetadata)
+Samples with presentation times after currentTime should not cause loadedData.
+EXPECTED (video.readyState == '1') OK
+Samples with presentation times very close to currentTime should cause loadedData.
+EVENT(loadeddata)
+EXPECTED (video.readyState == '2') OK
+Samples with presentation end times very close to currentTime should not cause canPlay.
+EXPECTED (video.readyState == '2') OK
+Continuous samples with presentation end times after currentTime should cause canPlay.
+EVENT(canplay)
+EXPECTED (video.readyState &gt;= '3') OK
+END OF TEST
+
</ins></span></pre></div>
<a id="trunkLayoutTestsmediamediasourcemediasourcefudgefactorhtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/media/media-source/media-source-fudge-factor.html (0 => 164999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/media/media-source/media-source-fudge-factor.html                                (rev 0)
+++ trunk/LayoutTests/media/media-source/media-source-fudge-factor.html        2014-03-03 20:47:52 UTC (rev 164999)
</span><span class="lines">@@ -0,0 +1,73 @@
</span><ins>+&lt;!DOCTYPE html&gt;
+&lt;html&gt;
+&lt;head&gt;
+    &lt;title&gt;media-source-fudge-factor&lt;/title&gt;
+    &lt;script src=&quot;mock-media-source.js&quot;&gt;&lt;/script&gt;
+    &lt;script src=&quot;../video-test.js&quot;&gt;&lt;/script&gt;
+    &lt;script&gt;
+    var source;
+    var sourceBuffer;
+
+    var requestLength = 200000;
+    var nextRequest = 0;
+    var totalLength = 100;
+
+    if (window.internals)
+        internals.initializeMockMediaSource();
+
+    function runTest() {
+        findMediaElement();
+        source = new MediaSource();
+        source.addEventListener('sourceopen', startLoad);
+        run('video.src = URL.createObjectURL(source)');
+    }
+
+    function startLoad() {
+        sourceBuffer = source.addSourceBuffer('video/mock; codecs=&quot;mock&quot;');
+
+        // Make an init segment with 1 video track
+        var init = makeAInit(100, [makeATrack(1, 'mock', TRACK_KIND.VIDEO)]);
+        waitForEventOnce('loadedmetadata', loadedMetadata);
+        sourceBuffer.appendBuffer(init)
+    }
+
+    function loadedMetadata() {
+        consoleWrite('Samples with presentation times after currentTime should not cause loadedData.');
+        sourceBuffer.appendBuffer(makeASample(0.5, 0.5, 0.5, 1, SAMPLE_FLAG.SYNC));
+        setTimeout(notLoadedData, 50);
+    }
+
+    function notLoadedData() {
+        testExpected('video.readyState', HTMLMediaElement.HAVE_METADATA);
+
+        consoleWrite('Samples with presentation times very close to currentTime should cause loadedData.');
+        sourceBuffer.appendBuffer(makeASample(0.01, 0.01, 0.01, 1, SAMPLE_FLAG.SYNC));
+        waitForEvent('loadeddata', loadedData);
+    }
+
+    function loadedData() {
+        testExpected('video.readyState', HTMLMediaElement.HAVE_CURRENT_DATA);
+
+        consoleWrite('Samples with presentation end times very close to currentTime should not cause canPlay.');
+        sourceBuffer.appendBuffer(makeASample(0.02, 0.02, 0.01, 1, SAMPLE_FLAG.SYNC));
+        setTimeout(notCanPlay, 50);
+    }
+
+    function notCanPlay() {
+        testExpected('video.readyState', HTMLMediaElement.HAVE_CURRENT_DATA);
+
+        consoleWrite('Continuous samples with presentation end times after currentTime should cause canPlay.');
+        sourceBuffer.appendBuffer(makeASample(0.03, 0.03, 0.47, 1, SAMPLE_FLAG.SYNC));
+        waitForEvent('canplay', canPlay);
+    }
+
+    function canPlay() {
+        testExpected('video.readyState', HTMLMediaElement.HAVE_FUTURE_DATA, '&gt;=');
+        endTest();
+    }
+    &lt;/script&gt;
+&lt;/head&gt;
+&lt;body onload=&quot;runTest()&quot;&gt;
+    &lt;video controls width=960 height=510&gt;&lt;/video&gt;
+&lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (164998 => 164999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2014-03-03 20:24:30 UTC (rev 164998)
+++ trunk/Source/WebCore/ChangeLog        2014-03-03 20:47:52 UTC (rev 164999)
</span><span class="lines">@@ -1,3 +1,34 @@
</span><ins>+2014-02-28  Jer Noble  &lt;jer.noble@apple.com&gt;
+
+        [MSE] YouTube videos fail to play
+        https://bugs.webkit.org/show_bug.cgi?id=129525
+
+        Reviewed by Darin Adler.
+
+        Test: media/media-source/media-source-fudge-factor.html
+
+        Add a very simple playability metric to SourceBuffer. Track the number of seconds buffered
+        and use that metric to determine whether the MediaSource has buffered enough data to play
+        through.
+
+        * Modules/mediasource/MediaSource.cpp:
+        (WebCore::MediaSource::currentTime): Added simple accessor.
+        (WebCore::MediaSource::monitorSourceBuffers): Replace the functor-based iterators with lambdas.
+        (WebCore::MediaSource::addSourceBuffer): Drive-by fix; only add new source buffers to the
+            activeSourceBuffers list if those buffers are actually active.
+        * Modules/mediasource/MediaSource.h:
+        * Modules/mediasource/SourceBuffer.cpp:
+        (WebCore::SourceBuffer::SourceBuffer): Initialize new ivars.
+        (WebCore::SourceBuffer::sourceBufferPrivateDidReceiveSample): Measure the number of seconds buffered.
+        (WebCore::SourceBuffer::monitorBufferingRate): Use a simple Exponential moving average to
+            track the buffering rate.
+        (WebCore::SourceBuffer::hasCurrentTime): Return true if the current time is within 1/24 s
+            of a buffered range.
+        (WebCore::SourceBuffer::hasFutureTime): Moved from MediaSource.
+        (WebCore::SourceBuffer::canPlayThrough): Return true if the buffering rate is &gt; 1s per
+            second, or if the rate is sufficient to keep up over the remaining time.
+        * Modules/mediasource/SourceBuffer.h:
+
</ins><span class="cx"> 2014-03-03  Bear Travis  &lt;betravis@adobe.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [CSS Shapes] Serialize circle positions
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasourceMediaSourcecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasource/MediaSource.cpp (164998 => 164999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasource/MediaSource.cpp        2014-03-03 20:24:30 UTC (rev 164998)
+++ trunk/Source/WebCore/Modules/mediasource/MediaSource.cpp        2014-03-03 20:47:52 UTC (rev 164999)
</span><span class="lines">@@ -131,6 +131,11 @@
</span><span class="cx">     return isClosed() ? std::numeric_limits&lt;float&gt;::quiet_NaN() : m_private-&gt;duration();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+double MediaSource::currentTime() const
+{
+    return m_mediaElement ? m_mediaElement-&gt;currentTime() : 0;
+}
+
</ins><span class="cx"> std::unique_ptr&lt;PlatformTimeRanges&gt; MediaSource::buffered() const
</span><span class="cx"> {
</span><span class="cx">     // Implements MediaSource algorithm for HTMLMediaElement.buffered.
</span><span class="lines">@@ -175,61 +180,17 @@
</span><span class="cx">     return PlatformTimeRanges::create(intersectionRanges-&gt;ranges());
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-class SourceBufferBufferedDoesNotContainTime {
-public:
-    SourceBufferBufferedDoesNotContainTime(double time) : m_time(time) { }
-    bool operator()(RefPtr&lt;SourceBuffer&gt; sourceBuffer)
-    {
-        return !sourceBuffer-&gt;buffered()-&gt;contain(m_time);
-    }
-
-    double m_time;
-};
-
-class SourceBufferBufferedHasEnough {
-public:
-    SourceBufferBufferedHasEnough(double time, double duration) : m_time(time), m_duration(duration) { }
-    bool operator()(RefPtr&lt;SourceBuffer&gt; sourceBuffer)
-    {
-        size_t rangePos = sourceBuffer-&gt;buffered()-&gt;find(m_time);
-        if (rangePos == notFound)
-            return false;
-
-        double endTime = sourceBuffer-&gt;buffered()-&gt;end(rangePos, IGNORE_EXCEPTION);
-        return m_duration - endTime &lt; 1;
-    }
-
-    double m_time;
-    double m_duration;
-};
-
-class SourceBufferBufferedHasFuture {
-public:
-    SourceBufferBufferedHasFuture(double time) : m_time(time) { }
-    bool operator()(RefPtr&lt;SourceBuffer&gt; sourceBuffer)
-    {
-        size_t rangePos = sourceBuffer-&gt;buffered()-&gt;find(m_time);
-        if (rangePos == notFound)
-            return false;
-
-        double endTime = sourceBuffer-&gt;buffered()-&gt;end(rangePos, IGNORE_EXCEPTION);
-        return endTime - m_time &gt; 1;
-    }
-
-    double m_time;
-};
-
</del><span class="cx"> void MediaSource::monitorSourceBuffers()
</span><span class="cx"> {
</span><del>-    double currentTime = mediaElement()-&gt;currentTime();
-
</del><span class="cx">     // 2.4.4 SourceBuffer Monitoring
</span><span class="cx">     // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#buffer-monitoring
</span><span class="cx">     // ↳ If buffered for all objects in activeSourceBuffers do not contain TimeRanges for the current
</span><span class="cx">     // playback position:
</span><span class="cx">     auto begin = m_activeSourceBuffers-&gt;begin();
</span><span class="cx">     auto end = m_activeSourceBuffers-&gt;end();
</span><del>-    if (std::all_of(begin, end, SourceBufferBufferedDoesNotContainTime(currentTime))) {
</del><ins>+    if (std::all_of(begin, end, [](RefPtr&lt;SourceBuffer&gt;&amp; sourceBuffer) {
+        return !sourceBuffer-&gt;hasCurrentTime();
+    })) {
</ins><span class="cx">         // 1. Set the HTMLMediaElement.readyState attribute to HAVE_METADATA.
</span><span class="cx">         // 2. If this is the first transition to HAVE_METADATA, then queue a task to fire a simple event
</span><span class="cx">         // named loadedmetadata at the media element.
</span><span class="lines">@@ -241,7 +202,9 @@
</span><span class="cx"> 
</span><span class="cx">     // ↳ If buffered for all objects in activeSourceBuffers contain TimeRanges that include the current
</span><span class="cx">     // playback position and enough data to ensure uninterrupted playback:
</span><del>-    if (std::all_of(begin, end, SourceBufferBufferedHasEnough(currentTime, mediaElement()-&gt;duration()))) {
</del><ins>+    if (std::all_of(begin, end, [](RefPtr&lt;SourceBuffer&gt;&amp; sourceBuffer) {
+        return sourceBuffer-&gt;hasFutureTime() &amp;&amp; sourceBuffer-&gt;canPlayThrough();
+    })) {
</ins><span class="cx">         // 1. Set the HTMLMediaElement.readyState attribute to HAVE_ENOUGH_DATA.
</span><span class="cx">         // 2. Queue a task to fire a simple event named canplaythrough at the media element.
</span><span class="cx">         // 3. Playback may resume at this point if it was previously suspended by a transition to HAVE_CURRENT_DATA.
</span><span class="lines">@@ -253,7 +216,9 @@
</span><span class="cx"> 
</span><span class="cx">     // ↳ If buffered for at least one object in activeSourceBuffers contains a TimeRange that includes
</span><span class="cx">     // the current playback position but not enough data to ensure uninterrupted playback:
</span><del>-    if (std::any_of(begin, end, SourceBufferBufferedHasFuture(currentTime))) {
</del><ins>+    if (std::any_of(begin, end, [](RefPtr&lt;SourceBuffer&gt;&amp; sourceBuffer) {
+        return sourceBuffer-&gt;hasFutureTime();
+    })) {
</ins><span class="cx">         // 1. Set the HTMLMediaElement.readyState attribute to HAVE_FUTURE_DATA.
</span><span class="cx">         // 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.
</span><span class="cx">         // 3. Playback may resume at this point if it was previously suspended by a transition to HAVE_CURRENT_DATA.
</span><span class="lines">@@ -438,7 +403,9 @@
</span><span class="cx">     RefPtr&lt;SourceBuffer&gt; buffer = SourceBuffer::create(sourceBufferPrivate.releaseNonNull(), this);
</span><span class="cx">     // 6. Add the new object to sourceBuffers and fire a addsourcebuffer on that object.
</span><span class="cx">     m_sourceBuffers-&gt;add(buffer);
</span><del>-    m_activeSourceBuffers-&gt;add(buffer);
</del><ins>+
+    if (buffer-&gt;active())
+        m_activeSourceBuffers-&gt;add(buffer);
</ins><span class="cx">     // 7. Return the new object to the caller.
</span><span class="cx">     return buffer.get();
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasourceMediaSourceh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasource/MediaSource.h (164998 => 164999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasource/MediaSource.h        2014-03-03 20:24:30 UTC (rev 164998)
+++ trunk/Source/WebCore/Modules/mediasource/MediaSource.h        2014-03-03 20:47:52 UTC (rev 164999)
</span><span class="lines">@@ -80,6 +80,7 @@
</span><span class="cx">     void monitorSourceBuffers();
</span><span class="cx"> 
</span><span class="cx">     void setDuration(double, ExceptionCode&amp;);
</span><ins>+    double currentTime() const;
</ins><span class="cx">     const AtomicString&amp; readyState() const { return m_readyState; }
</span><span class="cx">     void setReadyState(const AtomicString&amp;);
</span><span class="cx">     void endOfStream(const AtomicString&amp; error, ExceptionCode&amp;);
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasourceSourceBuffercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp (164998 => 164999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp        2014-03-03 20:24:30 UTC (rev 164998)
+++ trunk/Source/WebCore/Modules/mediasource/SourceBuffer.cpp        2014-03-03 20:47:52 UTC (rev 164999)
</span><span class="lines">@@ -49,10 +49,16 @@
</span><span class="cx"> #include &quot;TimeRanges.h&quot;
</span><span class="cx"> #include &quot;VideoTrackList.h&quot;
</span><span class="cx"> #include &lt;map&gt;
</span><ins>+#include &lt;wtf/CurrentTime.h&gt;
</ins><span class="cx"> #include &lt;wtf/NeverDestroyed.h&gt;
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><ins>+static double ExponentialMovingAverageCoefficient = 0.1;
+
+// Allow hasCurrentTime() to be off by as much as the length of a 24fps video frame
+static double CurrentTimeFudgeFactor = 1. / 24;
+
</ins><span class="cx"> struct SourceBuffer::TrackBuffer {
</span><span class="cx">     MediaTime lastDecodeTimestamp;
</span><span class="cx">     MediaTime lastFrameDuration;
</span><span class="lines">@@ -94,6 +100,9 @@
</span><span class="cx">     , m_buffered(TimeRanges::create())
</span><span class="cx">     , m_active(false)
</span><span class="cx">     , m_appendState(WaitingForSegment)
</span><ins>+    , m_timeOfBufferingMonitor(monotonicallyIncreasingTime())
+    , m_bufferedSinceLastMonitor(0)
+    , m_averageBufferRate(0)
</ins><span class="cx"> {
</span><span class="cx">     ASSERT(m_private);
</span><span class="cx">     ASSERT(m_source);
</span><span class="lines">@@ -999,6 +1008,7 @@
</span><span class="cx">             m_highestPresentationEndTimestamp = frameEndTimestamp;
</span><span class="cx"> 
</span><span class="cx">         m_buffered-&gt;add(presentationTimestamp.toDouble(), (presentationTimestamp + frameDuration + microsecond).toDouble());
</span><ins>+        m_bufferedSinceLastMonitor += frameDuration.toDouble();
</ins><span class="cx"> 
</span><span class="cx">         break;
</span><span class="cx">     } while (1);
</span><span class="lines">@@ -1166,6 +1176,73 @@
</span><span class="cx">         m_source-&gt;mediaElement()-&gt;incrementDroppedFrameCount();
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void SourceBuffer::monitorBufferingRate()
+{
+    if (!m_bufferedSinceLastMonitor)
+        return;
+
+    double now = monotonicallyIncreasingTime();
+    double interval = now - m_timeOfBufferingMonitor;
+    double rateSinceLastMonitor = m_bufferedSinceLastMonitor / interval;
+
+    m_timeOfBufferingMonitor = now;
+    m_bufferedSinceLastMonitor = 0;
+
+    m_averageBufferRate = m_averageBufferRate * (1 - ExponentialMovingAverageCoefficient) + rateSinceLastMonitor * ExponentialMovingAverageCoefficient;
+
+    LOG(Media, &quot;SourceBuffer::monitorBufferingRate(%p) - m_avegareBufferRate: %lf&quot;, this, m_averageBufferRate);
+}
+
+bool SourceBuffer::hasCurrentTime() const
+{
+    if (!m_buffered-&gt;length())
+        return false;
+
+    double currentTime = m_source-&gt;currentTime();
+    return fabs(m_buffered-&gt;nearest(m_source-&gt;currentTime()) - currentTime) &lt;= CurrentTimeFudgeFactor;
+}
+
+bool SourceBuffer::hasFutureTime() const
+{
+    double currentTime = m_source-&gt;currentTime();
+    const PlatformTimeRanges&amp; ranges = m_buffered-&gt;ranges();
+    double nearest = m_buffered-&gt;nearest(m_source-&gt;currentTime());
+    if (fabs(m_buffered-&gt;nearest(m_source-&gt;currentTime()) - currentTime) &gt; CurrentTimeFudgeFactor)
+        return false;
+
+    size_t found = ranges.find(nearest);
+    ASSERT(found != notFound);
+
+    bool ignoredValid = false;
+    return ranges.end(found, ignoredValid) - currentTime &gt; CurrentTimeFudgeFactor;
+}
+
+bool SourceBuffer::canPlayThrough()
+{
+    monitorBufferingRate();
+
+    // Assuming no fluctuations in the buffering rate, loading 1 second per second or greater
+    // means indefinite playback. This could be improved by taking jitter into account.
+    if (m_averageBufferRate &gt; 1)
+        return true;
+
+    // Add up all the time yet to be buffered.
+    double unbufferedTime = 0;
+    double currentTime = m_source-&gt;currentTime();
+    double duration = m_source-&gt;duration();
+
+    PlatformTimeRanges unbufferedRanges = m_buffered-&gt;ranges();
+    unbufferedRanges.invert();
+    unbufferedRanges.intersectWith(PlatformTimeRanges(currentTime, std::max(currentTime, duration)));
+    bool valid = true;
+
+    for (size_t i = 0, end = unbufferedRanges.length(); i &lt; end; ++i)
+        unbufferedTime += unbufferedRanges.end(i, valid) - unbufferedRanges.start(i, valid);
+
+    double timeRemaining = duration - currentTime;
+    return unbufferedTime / m_averageBufferRate &lt; timeRemaining;
+}
+
</ins><span class="cx"> } // namespace WebCore
</span><span class="cx"> 
</span><span class="cx"> #endif
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasourceSourceBufferh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasource/SourceBuffer.h (164998 => 164999)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasource/SourceBuffer.h        2014-03-03 20:24:30 UTC (rev 164998)
+++ trunk/Source/WebCore/Modules/mediasource/SourceBuffer.h        2014-03-03 20:47:52 UTC (rev 164999)
</span><span class="lines">@@ -83,6 +83,12 @@
</span><span class="cx">     TextTrackList* textTracks();
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><ins>+    bool hasCurrentTime() const;
+    bool hasFutureTime() const;
+    bool canPlayThrough();
+
+    bool active() const { return m_active; }
+
</ins><span class="cx">     // ActiveDOMObject interface
</span><span class="cx">     virtual bool hasPendingActivity() const override;
</span><span class="cx">     virtual void stop() override;
</span><span class="lines">@@ -144,6 +150,8 @@
</span><span class="cx">     void provideMediaData(TrackBuffer&amp;, AtomicString trackID);
</span><span class="cx">     void didDropSample();
</span><span class="cx"> 
</span><ins>+    void monitorBufferingRate();
+
</ins><span class="cx">     RefPtr&lt;SourceBufferPrivate&gt; m_private;
</span><span class="cx">     MediaSource* m_source;
</span><span class="cx">     GenericEventQueue m_asyncEventQueue;
</span><span class="lines">@@ -172,6 +180,9 @@
</span><span class="cx">     enum AppendStateType { WaitingForSegment, ParsingInitSegment, ParsingMediaSegment };
</span><span class="cx">     AppendStateType m_appendState;
</span><span class="cx"> 
</span><ins>+    double m_timeOfBufferingMonitor;
+    double m_bufferedSinceLastMonitor;
+    double m_averageBufferRate;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace WebCore
</span></span></pre>
</div>
</div>

</body>
</html>