<!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>[165916] 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/165916">165916</a></dd>
<dt>Author</dt> <dd>eric.carlson@apple.com</dd>
<dt>Date</dt> <dd>2014-03-19 14:08:24 -0700 (Wed, 19 Mar 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>Make Remote Control commands testable
https://bugs.webkit.org/show_bug.cgi?id=130358

Reviewed by Jer Noble.

Source/WebCore:

Test: media/video-remote-control-playpause.html

* html/HTMLMediaElement.cpp:
(WebCore::HTMLMediaElement::mediaPlayerTimeChanged): Call session-&gt;clientWillPausePlayback.
(WebCore::HTMLMediaElement::didReceiveRemoteControlCommand): Log the command.

* platform/audio/MediaSession.cpp:
(WebCore::MediaSessionClient::mediaSessionTitle): Move from header file.
(WebCore::MediaSessionClient::mediaSessionDuration): Ditto.
(WebCore::MediaSessionClient::mediaSessionCurrentTime): Ditto.
* platform/audio/MediaSession.h:

* platform/audio/MediaSessionManager.cpp:
(WebCore::MediaSessionManager::MediaSessionManager): Get rid of m_activeSession.
(WebCore::MediaSessionManager::removeSession): No need to clear m_activeSession because
    we use the m_sessions vector.
(WebCore::MediaSessionManager::sessionWillBeginPlayback): setCurrentSession takes a
    MediaSession&amp;.
(WebCore::MediaSessionManager::sessionWillEndPlayback): Keep m_sessions sorted so playing
    sessions are before paused sessions.
(WebCore::MediaSessionManager::setCurrentSession): Move the session that is beginning to
    play to the head of m_sessions.
(WebCore::MediaSessionManager::currentSession): Return m_sessions[0] or nullptr.
(WebCore::MediaSessionManager::didReceiveRemoteControlCommand):
* platform/audio/MediaSessionManager.h:

* platform/audio/ios/MediaSessionManagerIOS.mm: Include MediaPlayer.h.

* testing/Internals.cpp:
(WebCore::Internals::postRemoteControlCommand): New.
* testing/Internals.h:
* testing/Internals.idl:

LayoutTests:

* media/video-remote-control-playpause-expected.txt: Added.
* media/video-remote-control-playpause.html: Added.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsChangeLog">trunk/LayoutTests/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCorehtmlHTMLMediaElementcpp">trunk/Source/WebCore/html/HTMLMediaElement.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformaudioMediaSessioncpp">trunk/Source/WebCore/platform/audio/MediaSession.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformaudioMediaSessionh">trunk/Source/WebCore/platform/audio/MediaSession.h</a></li>
<li><a href="#trunkSourceWebCoreplatformaudioMediaSessionManagercpp">trunk/Source/WebCore/platform/audio/MediaSessionManager.cpp</a></li>
<li><a href="#trunkSourceWebCoreplatformaudioMediaSessionManagerh">trunk/Source/WebCore/platform/audio/MediaSessionManager.h</a></li>
<li><a href="#trunkSourceWebCoreplatformaudioiosMediaSessionManagerIOSmm">trunk/Source/WebCore/platform/audio/ios/MediaSessionManagerIOS.mm</a></li>
<li><a href="#trunkSourceWebCoretestingInternalscpp">trunk/Source/WebCore/testing/Internals.cpp</a></li>
<li><a href="#trunkSourceWebCoretestingInternalsh">trunk/Source/WebCore/testing/Internals.h</a></li>
<li><a href="#trunkSourceWebCoretestingInternalsidl">trunk/Source/WebCore/testing/Internals.idl</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#trunkLayoutTestsmediavideoremotecontrolplaypauseexpectedtxt">trunk/LayoutTests/media/video-remote-control-playpause-expected.txt</a></li>
<li><a href="#trunkLayoutTestsmediavideoremotecontrolplaypausehtml">trunk/LayoutTests/media/video-remote-control-playpause.html</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkLayoutTestsChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/LayoutTests/ChangeLog (165915 => 165916)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/ChangeLog        2014-03-19 21:06:38 UTC (rev 165915)
+++ trunk/LayoutTests/ChangeLog        2014-03-19 21:08:24 UTC (rev 165916)
</span><span class="lines">@@ -1,3 +1,13 @@
</span><ins>+2014-03-19  Eric Carlson  &lt;eric.carlson@apple.com&gt;
+
+        Make Remote Control commands testable
+        https://bugs.webkit.org/show_bug.cgi?id=130358
+
+        Reviewed by Jer Noble.
+
+        * media/video-remote-control-playpause-expected.txt: Added.
+        * media/video-remote-control-playpause.html: Added.
+
</ins><span class="cx"> 2014-03-19  Thiago de Barros Lacerda  &lt;thiago.lacerda@openbossa.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Improving webkitGetUserMedia error handling and error messages
</span></span></pre></div>
<a id="trunkLayoutTestsmediavideoremotecontrolplaypauseexpectedtxt"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/media/video-remote-control-playpause-expected.txt (0 => 165916)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/media/video-remote-control-playpause-expected.txt                                (rev 0)
+++ trunk/LayoutTests/media/video-remote-control-playpause-expected.txt        2014-03-19 21:08:24 UTC (rev 165916)
</span><span class="lines">@@ -0,0 +1,63 @@
</span><ins>+  
+Test play and pause with &quot;remote control&quot; interface.
+
+EVENT(canplaythrough)
+EVENT(canplaythrough)
+
+** Play both videos, the last one started (video[1]) should get focus...
+RUN(videos[0].play())
+RUN(videos[1].play())
+
+EVENT(playing)
+EVENT(playing)
+
+** Send a pause command, it should go to video[1].
+RUN(internals.postRemoteControlCommand('pause'))
+
+EVENT(pause)
+EXPECTED (event.target.id == 'two') OK
+EXPECTED (videos[0].paused == 'false') OK
+EXPECTED (videos[1].paused == 'true') OK
+
+** Pause video[0], so they both stop playing...
+RUN(videos[0].pause())
+
+EVENT(pause)
+EXPECTED (event.target.id == 'one') OK
+EXPECTED (videos[0].paused == 'true') OK
+EXPECTED (videos[1].paused == 'true') OK
+
+** Seek so video[1] will pause automatically when playback ends.
+RUN(videos[0].currentTime = 0)
+RUN(videos[1].currentTime = 5.5)
+
+EVENT(seeked)
+EVENT(seeked)
+
+** Send a play command, it should go to videos[0] because it was playing most recently.
+RUN(internals.postRemoteControlCommand('play'))
+
+EVENT(playing)
+EXPECTED (event.target.id == 'one') OK
+
+** Start videos[1], wait for it to pause at duration.
+RUN(videos[1].play())
+
+EVENT(playing)
+EXPECTED (event.target.id == 'two') OK
+
+EVENT(pause)
+EXPECTED (event.target.id == 'two') OK
+EXPECTED (videos[0].paused == 'false') OK
+EXPECTED (videos[1].paused == 'true') OK
+
+** Send a pause command, videos[0] is still playing so it should get the command.
+RUN(internals.postRemoteControlCommand('pause'))
+
+EVENT(pause)
+EXPECTED (event.target.id == 'one') OK
+EXPECTED (videos[0].paused == 'true') OK
+EXPECTED (videos[1].paused == 'true') OK
+
+END OF TEST
+
</ins></span></pre></div>
<a id="trunkLayoutTestsmediavideoremotecontrolplaypausehtml"></a>
<div class="addfile"><h4>Added: trunk/LayoutTests/media/video-remote-control-playpause.html (0 => 165916)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/LayoutTests/media/video-remote-control-playpause.html                                (rev 0)
+++ trunk/LayoutTests/media/video-remote-control-playpause.html        2014-03-19 21:08:24 UTC (rev 165916)
</span><span class="lines">@@ -0,0 +1,146 @@
</span><ins>+&lt;html&gt;
+    &lt;head&gt;
+        &lt;script src=media-file.js&gt;&lt;/script&gt;
+        &lt;script src=video-test.js&gt;&lt;/script&gt;
+        &lt;script&gt;
+            var playCount = 0;
+            var pauseCount = 0;
+            var seekedCount = 0;
+            var playThroughCount = 0;
+            var videos = [];
+
+            function logEvent(evt)
+            {
+                consoleWrite(&quot;EVENT(&quot; + evt.type + &quot;)&quot;);
+            }
+
+            function ended(evt)
+            {
+                logEvent(evt);
+            }
+
+            function seeked(evt)
+            {
+                logEvent(evt);
+
+                switch (++seekedCount)
+                {
+                case 2:
+                    consoleWrite(&quot;&lt;br&gt;** Send a play command, it should go to videos[0] because it was playing most recently.&quot;);
+                    run(&quot;internals.postRemoteControlCommand('play')&quot;);
+                    consoleWrite(&quot;&quot;);
+                    break;
+                }
+            }
+
+            function pause(evt)
+            {
+                logEvent(evt);
+
+                switch (++pauseCount)
+                {
+                case 1:
+                    testExpected(&quot;event.target.id&quot;, &quot;two&quot;);
+                    testExpected(&quot;videos[0].paused&quot;, false);
+                    testExpected(&quot;videos[1].paused&quot;, true);
+                    consoleWrite(&quot;&lt;br&gt;** Pause video[0], so they both stop playing...&quot;);
+                    run(&quot;videos[0].pause()&quot;);
+                    consoleWrite(&quot;&quot;);
+                    break;
+                case 2:
+                    testExpected(&quot;event.target.id&quot;, &quot;one&quot;);
+                    testExpected(&quot;videos[0].paused&quot;, true);
+                    testExpected(&quot;videos[1].paused&quot;, true);
+
+                    consoleWrite(&quot;&lt;br&gt;** Seek so video[1] will pause automatically when playback ends.&quot;);
+                    
+                    var seekTime = (videos[1].duration - .5).toFixed(1);
+                    run(&quot;videos[0].currentTime = 0&quot;);
+                    run(&quot;videos[1].currentTime = &quot; + seekTime);
+                    consoleWrite(&quot;&quot;);
+                    break;
+                case 3:
+                    testExpected(&quot;event.target.id&quot;, &quot;two&quot;);
+                    testExpected(&quot;videos[0].paused&quot;, false);
+                    testExpected(&quot;videos[1].paused&quot;, true);
+
+                    consoleWrite(&quot;&lt;br&gt;** Send a pause command, videos[0] is still playing so it should get the command.&quot;);
+                    run(&quot;internals.postRemoteControlCommand('pause')&quot;);
+                    consoleWrite(&quot;&quot;);
+                    break;
+                case 4:
+                    testExpected(&quot;event.target.id&quot;, &quot;one&quot;);
+                    testExpected(&quot;videos[0].paused&quot;, true);
+                    testExpected(&quot;videos[1].paused&quot;, true);
+                    consoleWrite(&quot;&quot;);
+                    endTest();
+                    break;
+                case 4:
+                }
+            }
+
+            function playing(evt)
+            {
+                logEvent(evt);
+
+                switch (++playCount)
+                {
+                case 2:
+                    consoleWrite(&quot;&lt;br&gt;** Send a pause command, it should go to video[1].&quot;);
+                    run(&quot;internals.postRemoteControlCommand('pause')&quot;);
+                    consoleWrite(&quot;&quot;);
+                    break;
+                case 3:
+                    testExpected(&quot;event.target.id&quot;, &quot;one&quot;);
+                    consoleWrite(&quot;&lt;br&gt;** Start videos[1], wait for it to pause at duration.&quot;);
+                    run(&quot;videos[1].play()&quot;);
+                    consoleWrite(&quot;&quot;);
+                    break;
+                case 4:
+                    testExpected(&quot;event.target.id&quot;, &quot;two&quot;);
+                    consoleWrite(&quot;&quot;);
+                    break;
+                }
+            }
+
+            function canplaythrough(evt)
+            {
+                logEvent(evt);
+
+                if (++playThroughCount &lt; 2)
+                    return;
+
+                consoleWrite(&quot;&lt;br&gt;** Play both videos, the last one started (video[1]) should get focus...&quot;);
+                run(&quot;videos[0].play()&quot;);
+                run(&quot;videos[1].play()&quot;);
+
+                consoleWrite(&quot;&quot;);
+            }
+
+            function start()
+            {
+                if (!window.internals) {
+                    failTest('This test requires window.internals.');
+                    return;
+                }
+
+                videos = document.getElementsByTagName('video');
+                for (var i = 0; i &lt; videos.length; ++i) {
+                    video = videos[i];
+                    video.addEventListener(&quot;canplaythrough&quot;, canplaythrough);
+                    video.addEventListener('playing', playing);
+                    video.addEventListener('pause', pause);
+                    video.addEventListener('seeked', seeked);
+                    video.addEventListener('ended');
+                    video.src = findMediaFile(&quot;video&quot;, &quot;content/test&quot;);
+                }
+            }
+        &lt;/script&gt;
+    &lt;/head&gt;
+
+    &lt;body onload=&quot;start()&quot;&gt;
+        &lt;video controls id=&quot;one&quot;&gt;&lt;/video&gt;
+        &lt;video controls id=&quot;two&quot;&gt;&lt;/video&gt;
+        &lt;p&gt;Test play and pause with &quot;remote control&quot; interface.&lt;/p&gt;
+    &lt;/body&gt;
+&lt;/html&gt;
</ins></span></pre></div>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (165915 => 165916)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2014-03-19 21:06:38 UTC (rev 165915)
+++ trunk/Source/WebCore/ChangeLog        2014-03-19 21:08:24 UTC (rev 165916)
</span><span class="lines">@@ -1,3 +1,43 @@
</span><ins>+2014-03-19  Eric Carlson  &lt;eric.carlson@apple.com&gt;
+
+        Make Remote Control commands testable
+        https://bugs.webkit.org/show_bug.cgi?id=130358
+
+        Reviewed by Jer Noble.
+
+        Test: media/video-remote-control-playpause.html
+
+        * html/HTMLMediaElement.cpp:
+        (WebCore::HTMLMediaElement::mediaPlayerTimeChanged): Call session-&gt;clientWillPausePlayback.
+        (WebCore::HTMLMediaElement::didReceiveRemoteControlCommand): Log the command.
+
+        * platform/audio/MediaSession.cpp:
+        (WebCore::MediaSessionClient::mediaSessionTitle): Move from header file.
+        (WebCore::MediaSessionClient::mediaSessionDuration): Ditto.
+        (WebCore::MediaSessionClient::mediaSessionCurrentTime): Ditto.
+        * platform/audio/MediaSession.h:
+
+        * platform/audio/MediaSessionManager.cpp:
+        (WebCore::MediaSessionManager::MediaSessionManager): Get rid of m_activeSession.
+        (WebCore::MediaSessionManager::removeSession): No need to clear m_activeSession because 
+            we use the m_sessions vector.
+        (WebCore::MediaSessionManager::sessionWillBeginPlayback): setCurrentSession takes a 
+            MediaSession&amp;.
+        (WebCore::MediaSessionManager::sessionWillEndPlayback): Keep m_sessions sorted so playing
+            sessions are before paused sessions.
+        (WebCore::MediaSessionManager::setCurrentSession): Move the session that is beginning to
+            play to the head of m_sessions.
+        (WebCore::MediaSessionManager::currentSession): Return m_sessions[0] or nullptr.
+        (WebCore::MediaSessionManager::didReceiveRemoteControlCommand):
+        * platform/audio/MediaSessionManager.h:
+
+        * platform/audio/ios/MediaSessionManagerIOS.mm: Include MediaPlayer.h.
+
+        * testing/Internals.cpp:
+        (WebCore::Internals::postRemoteControlCommand): New.
+        * testing/Internals.h:
+        * testing/Internals.idl:
+
</ins><span class="cx"> 2014-03-19  Thiago de Barros Lacerda  &lt;thiago.lacerda@openbossa.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Improving webkitGetUserMedia error handling and error messages
</span></span></pre></div>
<a id="trunkSourceWebCorehtmlHTMLMediaElementcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/html/HTMLMediaElement.cpp (165915 => 165916)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/html/HTMLMediaElement.cpp        2014-03-19 21:06:38 UTC (rev 165915)
+++ trunk/Source/WebCore/html/HTMLMediaElement.cpp        2014-03-19 21:08:24 UTC (rev 165916)
</span><span class="lines">@@ -3996,6 +3996,7 @@
</span><span class="cx">                 // changes paused to true and fires a simple event named pause at the media element.
</span><span class="cx">                 m_paused = true;
</span><span class="cx">                 scheduleEvent(eventNames().pauseEvent);
</span><ins>+                m_mediaSession-&gt;clientWillPausePlayback();
</ins><span class="cx">             }
</span><span class="cx">             // Queue a task to fire a simple event named ended at the media element.
</span><span class="cx">             if (!m_sentEndEvent) {
</span><span class="lines">@@ -5975,6 +5976,8 @@
</span><span class="cx"> 
</span><span class="cx"> void HTMLMediaElement::didReceiveRemoteControlCommand(MediaSession::RemoteControlCommandType command)
</span><span class="cx"> {
</span><ins>+    LOG(Media, &quot;HTMLMediaElement::didReceiveRemoteControlCommand(%i)&quot;, static_cast&lt;int&gt;(command));
+
</ins><span class="cx">     switch (command) {
</span><span class="cx">     case MediaSession::PlayCommand:
</span><span class="cx">         play();
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformaudioMediaSessioncpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/audio/MediaSession.cpp (165915 => 165916)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/audio/MediaSession.cpp        2014-03-19 21:06:38 UTC (rev 165915)
+++ trunk/Source/WebCore/platform/audio/MediaSession.cpp        2014-03-19 21:08:24 UTC (rev 165916)
</span><span class="lines">@@ -28,6 +28,7 @@
</span><span class="cx"> 
</span><span class="cx"> #include &quot;HTMLMediaElement.h&quot;
</span><span class="cx"> #include &quot;Logging.h&quot;
</span><ins>+#include &quot;MediaPlayer.h&quot;
</ins><span class="cx"> #include &quot;MediaSessionManager.h&quot;
</span><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="lines">@@ -70,13 +71,13 @@
</span><span class="cx"> 
</span><span class="cx"> void MediaSession::setState(State state)
</span><span class="cx"> {
</span><del>-    LOG(Media, &quot;MediaSession::setState - %s&quot;, stateName(state));
</del><ins>+    LOG(Media, &quot;MediaSession::setState(%p) - %s&quot;, this, stateName(state));
</ins><span class="cx">     m_state = state;
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void MediaSession::beginInterruption()
</span><span class="cx"> {
</span><del>-    LOG(Media, &quot;MediaSession::beginInterruption&quot;);
</del><ins>+    LOG(Media, &quot;MediaSession::beginInterruption(%p), state = %s&quot;, this, stateName(m_state));
</ins><span class="cx"> 
</span><span class="cx">     m_stateToRestore = state();
</span><span class="cx">     m_notifyingClient = true;
</span><span class="lines">@@ -87,7 +88,7 @@
</span><span class="cx"> 
</span><span class="cx"> void MediaSession::endInterruption(EndInterruptionFlags flags)
</span><span class="cx"> {
</span><del>-    LOG(Media, &quot;MediaSession::endInterruption - flags = %i, stateToRestore = %s&quot;, (int)flags, stateName(m_stateToRestore));
</del><ins>+    LOG(Media, &quot;MediaSession::endInterruption(%p) - flags = %i, stateToRestore = %s&quot;, this, (int)flags, stateName(m_stateToRestore));
</ins><span class="cx"> 
</span><span class="cx">     State stateToRestore = m_stateToRestore;
</span><span class="cx">     m_stateToRestore = Idle;
</span><span class="lines">@@ -108,9 +109,12 @@
</span><span class="cx"> 
</span><span class="cx"> bool MediaSession::clientWillPausePlayback()
</span><span class="cx"> {
</span><ins>+    LOG(Media, &quot;MediaSession::clientWillPausePlayback(%p)- state = %s&quot;, this, stateName(m_state));
</ins><span class="cx">     if (state() == Interrupted) {
</span><del>-        if (!m_notifyingClient)
</del><ins>+        if (!m_notifyingClient) {
</ins><span class="cx">             m_stateToRestore = Paused;
</span><ins>+            LOG(Media, &quot;      setting stateToRestore to \&quot;Paused\&quot;&quot;);
+        }
</ins><span class="cx">         return false;
</span><span class="cx">     }
</span><span class="cx">     
</span><span class="lines">@@ -121,7 +125,7 @@
</span><span class="cx"> 
</span><span class="cx"> void MediaSession::pauseSession()
</span><span class="cx"> {
</span><del>-    LOG(Media, &quot;MediaSession::pauseSession&quot;);
</del><ins>+    LOG(Media, &quot;MediaSession::pauseSession(%p)&quot;, this);
</ins><span class="cx">     m_client.pausePlayback();
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -154,5 +158,20 @@
</span><span class="cx"> {
</span><span class="cx">     m_client.didReceiveRemoteControlCommand(command);
</span><span class="cx"> }
</span><ins>+    
+String MediaSessionClient::mediaSessionTitle() const
+{
+    return String();
+}
</ins><span class="cx"> 
</span><ins>+double MediaSessionClient::mediaSessionDuration() const
+{
+    return MediaPlayer::invalidTime();
</ins><span class="cx"> }
</span><ins>+
+double MediaSessionClient::mediaSessionCurrentTime() const
+{
+    return MediaPlayer::invalidTime();
+}
+
+}
</ins></span></pre></div>
<a id="trunkSourceWebCoreplatformaudioMediaSessionh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/audio/MediaSession.h (165915 => 165916)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/audio/MediaSession.h        2014-03-19 21:06:38 UTC (rev 165915)
+++ trunk/Source/WebCore/platform/audio/MediaSession.h        2014-03-19 21:08:24 UTC (rev 165916)
</span><span class="lines">@@ -26,8 +26,8 @@
</span><span class="cx"> #ifndef MediaSession_h
</span><span class="cx"> #define MediaSession_h
</span><span class="cx"> 
</span><del>-#include &quot;MediaPlayer.h&quot;
</del><span class="cx"> #include &lt;wtf/Noncopyable.h&gt;
</span><ins>+#include &lt;wtf/text/WTFString.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="lines">@@ -109,9 +109,9 @@
</span><span class="cx">     virtual void resumePlayback() = 0;
</span><span class="cx">     virtual void pausePlayback() = 0;
</span><span class="cx"> 
</span><del>-    virtual String mediaSessionTitle() const { return String(); }
-    virtual double mediaSessionDuration() const { return MediaPlayer::invalidTime(); }
-    virtual double mediaSessionCurrentTime() const { return MediaPlayer::invalidTime(); }
</del><ins>+    virtual String mediaSessionTitle() const;
+    virtual double mediaSessionDuration() const;
+    virtual double mediaSessionCurrentTime() const;
</ins><span class="cx">     
</span><span class="cx">     virtual bool canReceiveRemoteControlCommands() const = 0;
</span><span class="cx">     virtual void didReceiveRemoteControlCommand(MediaSession::RemoteControlCommandType) = 0;
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformaudioMediaSessionManagercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/audio/MediaSessionManager.cpp (165915 => 165916)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/audio/MediaSessionManager.cpp        2014-03-19 21:06:38 UTC (rev 165915)
+++ trunk/Source/WebCore/platform/audio/MediaSessionManager.cpp        2014-03-19 21:08:24 UTC (rev 165916)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2013 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2013-2014 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -40,8 +40,7 @@
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx"> MediaSessionManager::MediaSessionManager()
</span><del>-    : m_activeSession(nullptr)
-    , m_interrupted(false)
</del><ins>+    : m_interrupted(false)
</ins><span class="cx"> {
</span><span class="cx">     resetRestrictions();
</span><span class="cx"> }
</span><span class="lines">@@ -68,7 +67,7 @@
</span><span class="cx"> int MediaSessionManager::count(MediaSession::MediaType type) const
</span><span class="cx"> {
</span><span class="cx">     ASSERT(type &gt;= MediaSession::None &amp;&amp; type &lt;= MediaSession::WebAudio);
</span><del>-    
</del><ins>+
</ins><span class="cx">     int count = 0;
</span><span class="cx">     for (auto* session : m_sessions) {
</span><span class="cx">         if (session-&gt;mediaType() == type)
</span><span class="lines">@@ -83,7 +82,8 @@
</span><span class="cx">     LOG(Media, &quot;MediaSessionManager::beginInterruption&quot;);
</span><span class="cx"> 
</span><span class="cx">     m_interrupted = true;
</span><del>-    for (auto* session : m_sessions)
</del><ins>+    Vector&lt;MediaSession*&gt; sessions = m_sessions;
+    for (auto* session : sessions)
</ins><span class="cx">         session-&gt;beginInterruption();
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="lines">@@ -92,12 +92,15 @@
</span><span class="cx">     LOG(Media, &quot;MediaSessionManager::endInterruption&quot;);
</span><span class="cx"> 
</span><span class="cx">     m_interrupted = false;
</span><del>-    for (auto* session : m_sessions)
</del><ins>+    Vector&lt;MediaSession*&gt; sessions = m_sessions;
+    for (auto* session : sessions)
</ins><span class="cx">         session-&gt;endInterruption(flags);
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void MediaSessionManager::addSession(MediaSession&amp; session)
</span><span class="cx"> {
</span><ins>+    LOG(Media, &quot;MediaSessionManager::addSession - %p&quot;, &amp;session);
+    
</ins><span class="cx">     m_sessions.append(&amp;session);
</span><span class="cx">     if (m_interrupted)
</span><span class="cx">         session.setState(MediaSession::Interrupted);
</span><span class="lines">@@ -115,14 +118,13 @@
</span><span class="cx"> 
</span><span class="cx"> void MediaSessionManager::removeSession(MediaSession&amp; session)
</span><span class="cx"> {
</span><ins>+    LOG(Media, &quot;MediaSessionManager::removeSession - %p&quot;, &amp;session);
+    
</ins><span class="cx">     size_t index = m_sessions.find(&amp;session);
</span><span class="cx">     ASSERT(index != notFound);
</span><span class="cx">     if (index == notFound)
</span><span class="cx">         return;
</span><span class="cx">     
</span><del>-    if (m_activeSession == &amp;session)
-        setCurrentSession(nullptr);
-    
</del><span class="cx">     m_sessions.remove(index);
</span><span class="cx">     updateSessionState();
</span><span class="cx"> 
</span><span class="lines">@@ -156,7 +158,9 @@
</span><span class="cx"> 
</span><span class="cx"> void MediaSessionManager::sessionWillBeginPlayback(MediaSession&amp; session)
</span><span class="cx"> {
</span><del>-    setCurrentSession(&amp;session);
</del><ins>+    LOG(Media, &quot;MediaSessionManager::sessionWillBeginPlayback - %p&quot;, &amp;session);
+    
+    setCurrentSession(session);
</ins><span class="cx"> 
</span><span class="cx">     if (!m_clients.isEmpty() &amp;&amp; (session.mediaType() == MediaSession::Video || session.mediaType() == MediaSession::Audio)) {
</span><span class="cx">         for (auto&amp; client : m_clients)
</span><span class="lines">@@ -168,7 +172,8 @@
</span><span class="cx">     if (!restrictions &amp; ConcurrentPlaybackNotPermitted)
</span><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    for (auto* oneSession : m_sessions) {
</del><ins>+    Vector&lt;MediaSession*&gt; sessions = m_sessions;
+    for (auto* oneSession : sessions) {
</ins><span class="cx">         if (oneSession == &amp;session)
</span><span class="cx">             continue;
</span><span class="cx">         if (oneSession-&gt;mediaType() != sessionType)
</span><span class="lines">@@ -177,7 +182,68 @@
</span><span class="cx">             oneSession-&gt;pauseSession();
</span><span class="cx">     }
</span><span class="cx"> }
</span><ins>+    
+void MediaSessionManager::sessionWillEndPlayback(MediaSession&amp; session)
+{
+    LOG(Media, &quot;MediaSessionManager::sessionWillEndPlayback - %p&quot;, &amp;session);
+    
+    if (m_sessions.size() &lt; 2)
+        return;
+    
+    size_t pausingSessionIndex = notFound;
+    size_t lastPlayingSessionIndex = notFound;
+    for (size_t i = 0; i &lt; m_sessions.size(); ++i) {
+        MediaSession* oneSession = m_sessions[i];
+        
+        if (oneSession == &amp;session) {
+            pausingSessionIndex = i;
+            continue;
+        }
+        if (oneSession-&gt;state() == MediaSession::Playing) {
+            lastPlayingSessionIndex = i;
+            continue;
+        }
+        if (oneSession-&gt;state() != MediaSession::Playing)
+            break;
+    }
+    if (lastPlayingSessionIndex == notFound || pausingSessionIndex == notFound)
+        return;
+    
+    if (pausingSessionIndex &gt; lastPlayingSessionIndex)
+        return;
+    
+    m_sessions.remove(pausingSessionIndex);
+    m_sessions.insert(lastPlayingSessionIndex, &amp;session);
+    
+    LOG(Media, &quot;MediaSessionManager::sessionWillEndPlayback - session moved from index %zu to %zu&quot;, pausingSessionIndex, lastPlayingSessionIndex);
+}
</ins><span class="cx"> 
</span><ins>+void MediaSessionManager::setCurrentSession(MediaSession&amp; session)
+{
+    LOG(Media, &quot;MediaSessionManager::setCurrentSession - %p&quot;, &amp;session);
+    
+    if (m_sessions.size() &lt; 2)
+        return;
+    
+    size_t index = m_sessions.find(&amp;session);
+    ASSERT(index != notFound);
+    if (!index || index == notFound)
+        return;
+
+    m_sessions.remove(index);
+    m_sessions.insert(0, &amp;session);
+    
+    LOG(Media, &quot;MediaSessionManager::setCurrentSession - session moved from index %zu to 0&quot;, index);
+}
+    
+MediaSession* MediaSessionManager::currentSession()
+{
+    if (!m_sessions.size())
+        return nullptr;
+
+    return m_sessions[0];
+}
+    
</ins><span class="cx"> bool MediaSessionManager::sessionRestrictsInlineVideoPlayback(const MediaSession&amp; session) const
</span><span class="cx"> {
</span><span class="cx">     MediaSession::MediaType sessionType = session.mediaType();
</span><span class="lines">@@ -190,7 +256,8 @@
</span><span class="cx"> void MediaSessionManager::applicationWillEnterBackground() const
</span><span class="cx"> {
</span><span class="cx">     LOG(Media, &quot;MediaSessionManager::applicationWillEnterBackground&quot;);
</span><del>-    for (auto* session : m_sessions) {
</del><ins>+    Vector&lt;MediaSession*&gt; sessions = m_sessions;
+    for (auto* session : sessions) {
</ins><span class="cx">         if (m_restrictions[session-&gt;mediaType()] &amp; BackgroundPlaybackNotPermitted)
</span><span class="cx">             session-&gt;beginInterruption();
</span><span class="cx">     }
</span><span class="lines">@@ -199,7 +266,8 @@
</span><span class="cx"> void MediaSessionManager::applicationWillEnterForeground() const
</span><span class="cx"> {
</span><span class="cx">     LOG(Media, &quot;MediaSessionManager::applicationWillEnterForeground&quot;);
</span><del>-    for (auto* session : m_sessions) {
</del><ins>+    Vector&lt;MediaSession*&gt; sessions = m_sessions;
+    for (auto* session : sessions) {
</ins><span class="cx">         if (m_restrictions[session-&gt;mediaType()] &amp; BackgroundPlaybackNotPermitted)
</span><span class="cx">             session-&gt;endInterruption(MediaSession::MayResumePlaying);
</span><span class="cx">     }
</span><span class="lines">@@ -213,9 +281,10 @@
</span><span class="cx"> 
</span><span class="cx"> void MediaSessionManager::didReceiveRemoteControlCommand(MediaSession::RemoteControlCommandType command)
</span><span class="cx"> {
</span><del>-    if (!m_activeSession || !m_activeSession-&gt;canReceiveRemoteControlCommands())
</del><ins>+    MediaSession* activeSession = currentSession();
+    if (!activeSession || !activeSession-&gt;canReceiveRemoteControlCommands())
</ins><span class="cx">         return;
</span><del>-    m_activeSession-&gt;didReceiveRemoteControlCommand(command);
</del><ins>+    activeSession-&gt;didReceiveRemoteControlCommand(command);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void MediaSessionManager::addClient(MediaSessionManagerClient* client)
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformaudioMediaSessionManagerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/audio/MediaSessionManager.h (165915 => 165916)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/audio/MediaSessionManager.h        2014-03-19 21:06:38 UTC (rev 165915)
+++ trunk/Source/WebCore/platform/audio/MediaSessionManager.h        2014-03-19 21:08:24 UTC (rev 165916)
</span><span class="lines">@@ -1,5 +1,5 @@
</span><span class="cx"> /*
</span><del>- * Copyright (C) 2013 Apple Inc. All rights reserved.
</del><ins>+ * Copyright (C) 2013-2014 Apple Inc. All rights reserved.
</ins><span class="cx">  *
</span><span class="cx">  * Redistribution and use in source and binary forms, with or without
</span><span class="cx">  * modification, are permitted provided that the following conditions
</span><span class="lines">@@ -81,7 +81,7 @@
</span><span class="cx">     virtual void resetRestrictions();
</span><span class="cx"> 
</span><span class="cx">     virtual void sessionWillBeginPlayback(MediaSession&amp;);
</span><del>-    virtual void sessionWillEndPlayback(MediaSession&amp;) { }
</del><ins>+    virtual void sessionWillEndPlayback(MediaSession&amp;);
</ins><span class="cx">     
</span><span class="cx">     bool sessionRestrictsInlineVideoPlayback(const MediaSession&amp;) const;
</span><span class="cx"> 
</span><span class="lines">@@ -101,8 +101,8 @@
</span><span class="cx">     void addSession(MediaSession&amp;);
</span><span class="cx">     void removeSession(MediaSession&amp;);
</span><span class="cx">     
</span><del>-    void setCurrentSession(MediaSession* session) { m_activeSession = session; }
-    MediaSession* currentSession() { return m_activeSession; }
</del><ins>+    void setCurrentSession(MediaSession&amp;);
+    MediaSession* currentSession();
</ins><span class="cx">     
</span><span class="cx"> private:
</span><span class="cx">     void updateSessionState();
</span><span class="lines">@@ -112,7 +112,6 @@
</span><span class="cx">     Vector&lt;MediaSession*&gt; m_sessions;
</span><span class="cx">     Vector&lt;MediaSessionManagerClient*&gt; m_clients;
</span><span class="cx">     std::unique_ptr&lt;RemoteCommandListener&gt; m_remoteCommandListener;
</span><del>-    MediaSession* m_activeSession;
</del><span class="cx">     bool m_interrupted;
</span><span class="cx"> };
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformaudioiosMediaSessionManagerIOSmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/audio/ios/MediaSessionManagerIOS.mm (165915 => 165916)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/audio/ios/MediaSessionManagerIOS.mm        2014-03-19 21:06:38 UTC (rev 165915)
+++ trunk/Source/WebCore/platform/audio/ios/MediaSessionManagerIOS.mm        2014-03-19 21:08:24 UTC (rev 165916)
</span><span class="lines">@@ -29,6 +29,7 @@
</span><span class="cx"> #if PLATFORM(IOS)
</span><span class="cx"> 
</span><span class="cx"> #import &quot;Logging.h&quot;
</span><ins>+#import &quot;MediaPlayer.h&quot;
</ins><span class="cx"> #import &quot;MediaSession.h&quot;
</span><span class="cx"> #import &quot;NotImplemented.h&quot;
</span><span class="cx"> #import &quot;SoftLinking.h&quot;
</span></span></pre></div>
<a id="trunkSourceWebCoretestingInternalscpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/testing/Internals.cpp (165915 => 165916)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/testing/Internals.cpp        2014-03-19 21:06:38 UTC (rev 165915)
+++ trunk/Source/WebCore/testing/Internals.cpp        2014-03-19 21:08:24 UTC (rev 165916)
</span><span class="lines">@@ -2290,5 +2290,33 @@
</span><span class="cx"> 
</span><span class="cx">     MediaSessionManager::sharedManager().addRestriction(mediaType, restrictions);
</span><span class="cx"> }
</span><del>-
</del><ins>+    
+void Internals::postRemoteControlCommand(const String&amp; commandString, ExceptionCode&amp; ec)
+{
+    MediaSession::RemoteControlCommandType command;
+    
+    if (equalIgnoringCase(commandString, &quot;Play&quot;))
+        command = MediaSession::PlayCommand;
+    else if (equalIgnoringCase(commandString, &quot;Pause&quot;))
+        command = MediaSession::PauseCommand;
+    else if (equalIgnoringCase(commandString, &quot;Stop&quot;))
+        command = MediaSession::StopCommand;
+    else if (equalIgnoringCase(commandString, &quot;TogglePlayPause&quot;))
+        command = MediaSession::TogglePlayPauseCommand;
+    else if (equalIgnoringCase(commandString, &quot;BeginSeekingBackward&quot;))
+        command = MediaSession::BeginSeekingBackwardCommand;
+    else if (equalIgnoringCase(commandString, &quot;EndSeekingBackward&quot;))
+        command = MediaSession::EndSeekingBackwardCommand;
+    else if (equalIgnoringCase(commandString, &quot;BeginSeekingForward&quot;))
+        command = MediaSession::BeginSeekingForwardCommand;
+    else if (equalIgnoringCase(commandString, &quot;EndSeekingForward&quot;))
+        command = MediaSession::EndSeekingForwardCommand;
+    else {
+        ec = INVALID_ACCESS_ERR;
+        return;
+    }
+    
+    MediaSessionManager::sharedManager().didReceiveRemoteControlCommand(command);
</ins><span class="cx"> }
</span><ins>+    
+}
</ins></span></pre></div>
<a id="trunkSourceWebCoretestingInternalsh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/testing/Internals.h (165915 => 165916)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/testing/Internals.h        2014-03-19 21:06:38 UTC (rev 165915)
+++ trunk/Source/WebCore/testing/Internals.h        2014-03-19 21:08:24 UTC (rev 165916)
</span><span class="lines">@@ -333,7 +333,8 @@
</span><span class="cx">     void endMediaSessionInterruption(const String&amp;);
</span><span class="cx">     void applicationWillEnterForeground() const;
</span><span class="cx">     void applicationWillEnterBackground() const;
</span><del>-    void setMediaSessionRestrictions(const String&amp; mediaType, const String&amp; restrictions, ExceptionCode&amp; ec);
</del><ins>+    void setMediaSessionRestrictions(const String&amp; mediaType, const String&amp; restrictions, ExceptionCode&amp;);
+    void postRemoteControlCommand(const String&amp;, ExceptionCode&amp;);
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     explicit Internals(Document*);
</span></span></pre></div>
<a id="trunkSourceWebCoretestingInternalsidl"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/testing/Internals.idl (165915 => 165916)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/testing/Internals.idl        2014-03-19 21:06:38 UTC (rev 165915)
+++ trunk/Source/WebCore/testing/Internals.idl        2014-03-19 21:08:24 UTC (rev 165916)
</span><span class="lines">@@ -283,4 +283,5 @@
</span><span class="cx">     void applicationWillEnterForeground();
</span><span class="cx">     void applicationWillEnterBackground();
</span><span class="cx">     [RaisesException] void setMediaSessionRestrictions(DOMString mediaType, DOMString restrictions);
</span><ins>+    [RaisesException] void postRemoteControlCommand(DOMString command);
</ins><span class="cx"> };
</span></span></pre>
</div>
</div>

</body>
</html>