<!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>[186918] trunk/Source/WebCore</title>
</head>
<body>

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

<h3>Log Message</h3>
<pre>Media Session: handle 'Transient' and 'Transient Solo' interruption events https://bugs.webkit.org/show_bug.cgi?id=146840

Reviewed by Eric Carlson.

* Modules/mediasession/MediaSession.cpp:
(WebCore::MediaSession::handleDuckInterruption): Added stub.
(WebCore::MediaSession::handleUnduckInterruption): Added stub.
(WebCore::MediaSession::handlePauseInterruption): Pause active media elements. We use our helper method to do this
  &quot;safely&quot; since pausing a media element can change the collection we're iterating.
(WebCore::MediaSession::handleUnpauseInterruption): Unpause active media elements. We use our helper method to do this
  &quot;safely&quot; since unpausing a media element can change the collection we're iterating.
(WebCore::MediaSession::togglePlayback): Factored out &quot;safe&quot; iteration into a helper method elsewhere.
(WebCore::MediaSession::safelyIterateActiveMediaElements): Safely iterate through the collection of active media
  elements.
* Modules/mediasession/MediaSession.h:
* Modules/mediasession/MediaSessionManager.cpp:
(WebCore::MediaSessionManager::MediaSessionManager): On Mac, register for platform-specific interruptions.
(WebCore::MediaSessionManager::didReceiveStartOfInterruptionNotification): Implemented section 4.5.2 of Media Session spec.
(WebCore::MediaSessionManager::didReceiveEndOfInterruptionNotification): Ditto.
* Modules/mediasession/MediaSessionManager.h:</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreModulesmediasessionMediaSessioncpp">trunk/Source/WebCore/Modules/mediasession/MediaSession.cpp</a></li>
<li><a href="#trunkSourceWebCoreModulesmediasessionMediaSessionh">trunk/Source/WebCore/Modules/mediasession/MediaSession.h</a></li>
<li><a href="#trunkSourceWebCoreModulesmediasessionMediaSessionManagercpp">trunk/Source/WebCore/Modules/mediasession/MediaSessionManager.cpp</a></li>
<li><a href="#trunkSourceWebCoreModulesmediasessionMediaSessionManagerh">trunk/Source/WebCore/Modules/mediasession/MediaSessionManager.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (186917 => 186918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-07-16 23:22:36 UTC (rev 186917)
+++ trunk/Source/WebCore/ChangeLog        2015-07-16 23:36:13 UTC (rev 186918)
</span><span class="lines">@@ -1,5 +1,29 @@
</span><span class="cx"> 2015-07-16  Matt Rajca  &lt;mrajca@apple.com&gt;
</span><span class="cx"> 
</span><ins>+        Media Session: handle 'Transient' and 'Transient Solo' interruption events
+        https://bugs.webkit.org/show_bug.cgi?id=146840
+
+        Reviewed by Eric Carlson.
+
+        * Modules/mediasession/MediaSession.cpp:
+        (WebCore::MediaSession::handleDuckInterruption): Added stub.
+        (WebCore::MediaSession::handleUnduckInterruption): Added stub.
+        (WebCore::MediaSession::handlePauseInterruption): Pause active media elements. We use our helper method to do this
+          &quot;safely&quot; since pausing a media element can change the collection we're iterating.
+        (WebCore::MediaSession::handleUnpauseInterruption): Unpause active media elements. We use our helper method to do this
+          &quot;safely&quot; since unpausing a media element can change the collection we're iterating.
+        (WebCore::MediaSession::togglePlayback): Factored out &quot;safe&quot; iteration into a helper method elsewhere.
+        (WebCore::MediaSession::safelyIterateActiveMediaElements): Safely iterate through the collection of active media
+          elements.
+        * Modules/mediasession/MediaSession.h:
+        * Modules/mediasession/MediaSessionManager.cpp:
+        (WebCore::MediaSessionManager::MediaSessionManager): On Mac, register for platform-specific interruptions.
+        (WebCore::MediaSessionManager::didReceiveStartOfInterruptionNotification): Implemented section 4.5.2 of Media Session spec.
+        (WebCore::MediaSessionManager::didReceiveEndOfInterruptionNotification): Ditto.
+        * Modules/mediasession/MediaSessionManager.h:
+
+2015-07-16  Matt Rajca  &lt;mrajca@apple.com&gt;
+
</ins><span class="cx">         Media Session: add support for platform-specific interruption providers
</span><span class="cx">         https://bugs.webkit.org/show_bug.cgi?id=147008
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasessionMediaSessioncpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasession/MediaSession.cpp (186917 => 186918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasession/MediaSession.cpp        2015-07-16 23:22:36 UTC (rev 186917)
+++ trunk/Source/WebCore/Modules/mediasession/MediaSession.cpp        2015-07-16 23:36:13 UTC (rev 186918)
</span><span class="lines">@@ -238,22 +238,58 @@
</span><span class="cx">     return true;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void MediaSession::togglePlayback()
</del><ins>+void MediaSession::handleDuckInterruption()
</ins><span class="cx"> {
</span><del>-    ASSERT(!m_iteratedActiveParticipatingElements);
</del><ins>+    // FIXME: Duck media elements.
</ins><span class="cx"> 
</span><del>-    HashSet&lt;HTMLMediaElement*&gt; activeParticipatingElementsCopy = m_activeParticipatingElements;
-    m_iteratedActiveParticipatingElements = &amp;activeParticipatingElementsCopy;
</del><ins>+    m_currentState = State::Interrupted;
+}
</ins><span class="cx"> 
</span><del>-    while (!activeParticipatingElementsCopy.isEmpty()) {
-        HTMLMediaElement* element = activeParticipatingElementsCopy.takeAny();
</del><ins>+void MediaSession::handleUnduckInterruption()
+{
+    // FIXME: Unduck media elements.
</ins><span class="cx"> 
</span><ins>+    m_currentState = State::Active;
+}
+
+void MediaSession::handlePauseInterruption()
+{
+    m_currentState = State::Interrupted;
+
+    safelyIterateActiveMediaElements([](HTMLMediaElement* element) {
+        element-&gt;pause();
+    });
+}
+
+void MediaSession::handleUnpauseInterruption()
+{
+    m_currentState = State::Active;
+
+    safelyIterateActiveMediaElements([](HTMLMediaElement* element) {
+        element-&gt;play();
+    });
+}
+
+void MediaSession::togglePlayback()
+{
+    safelyIterateActiveMediaElements([](HTMLMediaElement* element) {
</ins><span class="cx">         if (element-&gt;paused())
</span><span class="cx">             element-&gt;play();
</span><span class="cx">         else
</span><span class="cx">             element-&gt;pause();
</span><del>-    }
</del><ins>+    });
+}
</ins><span class="cx"> 
</span><ins>+void MediaSession::safelyIterateActiveMediaElements(std::function&lt;void(HTMLMediaElement*)&gt; handler)
+{
+    ASSERT(!m_iteratedActiveParticipatingElements);
+
+    HashSet&lt;HTMLMediaElement*&gt; activeParticipatingElementsCopy = m_activeParticipatingElements;
+    m_iteratedActiveParticipatingElements = &amp;activeParticipatingElementsCopy;
+
+    while (!activeParticipatingElementsCopy.isEmpty())
+        handler(activeParticipatingElementsCopy.takeAny());
+
</ins><span class="cx">     m_iteratedActiveParticipatingElements = nullptr;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasessionMediaSessionh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasession/MediaSession.h (186917 => 186918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasession/MediaSession.h        2015-07-16 23:22:36 UTC (rev 186917)
+++ trunk/Source/WebCore/Modules/mediasession/MediaSession.h        2015-07-16 23:36:13 UTC (rev 186918)
</span><span class="lines">@@ -45,6 +45,14 @@
</span><span class="cx">         Interrupted
</span><span class="cx">     };
</span><span class="cx"> 
</span><ins>+    enum class Kind {
+        Default,
+        Content,
+        Transient,
+        TransientSolo,
+        Ambient
+    };
+
</ins><span class="cx">     static Ref&lt;MediaSession&gt; create(ScriptExecutionContext&amp; context, const String&amp; kind)
</span><span class="cx">     {
</span><span class="cx">         return adoptRef(*new MediaSession(context, kind));
</span><span class="lines">@@ -55,6 +63,7 @@
</span><span class="cx">     ~MediaSession();
</span><span class="cx"> 
</span><span class="cx">     String kind() const;
</span><ins>+    Kind kindEnum() const { return m_kind; }
</ins><span class="cx">     MediaRemoteControls* controls(bool&amp; isNull);
</span><span class="cx"> 
</span><span class="cx">     State currentState() const { return m_currentState; }
</span><span class="lines">@@ -63,10 +72,15 @@
</span><span class="cx">     void setMetadata(const Dictionary&amp;);
</span><span class="cx"> 
</span><span class="cx">     void releaseSession();
</span><del>-    
</del><ins>+
</ins><span class="cx">     // Runs the media session invocation algorithm and returns true on success.
</span><span class="cx">     bool invoke();
</span><span class="cx"> 
</span><ins>+    void handleDuckInterruption();
+    void handlePauseInterruption();
+    void handleUnduckInterruption();
+    void handleUnpauseInterruption();
+
</ins><span class="cx">     void togglePlayback();
</span><span class="cx">     void skipToNextTrack();
</span><span class="cx">     void skipToPreviousTrack();
</span><span class="lines">@@ -74,20 +88,12 @@
</span><span class="cx"> private:
</span><span class="cx">     friend class HTMLMediaElement;
</span><span class="cx"> 
</span><del>-    enum class Kind {
-        Default,
-        Content,
-        Transient,
-        TransientSolo,
-        Ambient
-    };
-
</del><span class="cx">     static Kind parseKind(const String&amp;);
</span><del>-    Kind kindEnum() const { return m_kind; }
</del><span class="cx"> 
</span><span class="cx">     void addMediaElement(HTMLMediaElement&amp;);
</span><span class="cx">     void removeMediaElement(HTMLMediaElement&amp;);
</span><span class="cx"> 
</span><ins>+    void safelyIterateActiveMediaElements(std::function&lt;void(HTMLMediaElement*)&gt;);
</ins><span class="cx">     void changeActiveMediaElements(std::function&lt;void(void)&gt;);
</span><span class="cx">     void addActiveMediaElement(HTMLMediaElement&amp;);
</span><span class="cx">     bool isMediaElementActive(HTMLMediaElement&amp;);
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasessionMediaSessionManagercpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasession/MediaSessionManager.cpp (186917 => 186918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasession/MediaSessionManager.cpp        2015-07-16 23:22:36 UTC (rev 186917)
+++ trunk/Source/WebCore/Modules/mediasession/MediaSessionManager.cpp        2015-07-16 23:36:13 UTC (rev 186918)
</span><span class="lines">@@ -29,7 +29,7 @@
</span><span class="cx"> #if ENABLE(MEDIA_SESSION)
</span><span class="cx"> 
</span><span class="cx"> #include &quot;MediaSession.h&quot;
</span><del>-#include &lt;wtf/NeverDestroyed.h&gt;
</del><ins>+#include &quot;MediaSessionInterruptionProviderMac.h&quot;
</ins><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="lines">@@ -41,6 +41,13 @@
</span><span class="cx">     return manager;
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+MediaSessionManager::MediaSessionManager()
+{
+#if PLATFORM(MAC)
+    m_interruptionProvider = adoptRef(new MediaSessionInterruptionProviderMac(*this));
+#endif
+}
+
</ins><span class="cx"> bool MediaSessionManager::hasActiveMediaElements() const
</span><span class="cx"> {
</span><span class="cx">     for (auto* session : m_sessions) {
</span><span class="lines">@@ -92,6 +99,66 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void MediaSessionManager::didReceiveStartOfInterruptionNotification(MediaSessionInterruptingCategory interruptingCategory)
+{
+    // 4.5.2 Interrupting a media session
+    // When a start-of-interruption notification event is received from the platform, then the user agent must run the
+    // media session interruption algorithm against all known media sessions, passing in each media session as media session.
+    for (auto* session : m_sessions) {
+        // 1. If media session's current state is not active, then terminate these steps.
+        if (session-&gt;currentState() != MediaSession::State::Active)
+            continue;
+
+        // 2. Let interrupting media session category be the media session category that triggered this interruption.
+        //    If this interruption has no known media session category, let interrupting media session category be Default.
+
+        // 3. Run these substeps:
+        if (interruptingCategory == MediaSessionInterruptingCategory::Transient) {
+            // - If interrupting media session category is Transient
+            //   If media session's current media session type is Default or Content then duck all of media session's active
+            //   audio-producing participants and set media session's current state to interrupted.
+            if (session-&gt;kindEnum() == MediaSession::Kind::Default || session-&gt;kindEnum() == MediaSession::Kind::Content)
+                session-&gt;handleDuckInterruption();
+        } else if (interruptingCategory == MediaSessionInterruptingCategory::TransientSolo) {
+            // - If interrupting media session category is Transient Solo:
+            //   If media session's current media session type is Default, Content, Transient or Transient Solo then pause
+            //   all of media session's active audio-producing participants and set current media session's current state to interrupted.
+            if (session-&gt;kindEnum() != MediaSession::Kind::Ambient)
+                session-&gt;handlePauseInterruption();
+        }
+    }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void MediaSessionManager::didReceiveEndOfInterruptionNotification(MediaSessionInterruptingCategory interruptingCategory)
+{
+    // 4.5.2 Interrupting a media session
+    // When an end-of-interruption notification event is received from the platform, then the user agent must run the
+    // media session continuation algorithm against all known media sessions, passing in each media session as media session.
+    for (auto* session : m_sessions) {
+        // 1. If media session's current state is not interrupted, then terminate these steps.
+        if (session-&gt;currentState() != MediaSession::State::Interrupted)
+            continue;
+
+        // 2. Let interrupting media session category be the media session category that initially triggered this interruption.
+        //    If this interruption has no known media session category, let interrupting media session category be Default.
+
+        // 3. Run these substeps:
+        if (interruptingCategory == MediaSessionInterruptingCategory::Transient) {
+            // - If interrupting media session category is Transient:
+            //   If media session's current media session type is Default or Content, then unduck all of media session's
+            //   active audio-producing participants and set media session's current state to active.
+            if (session-&gt;kindEnum() == MediaSession::Kind::Default || session-&gt;kindEnum() == MediaSession::Kind::Content)
+                session-&gt;handleUnduckInterruption();
+        } else if (interruptingCategory == MediaSessionInterruptingCategory::TransientSolo) {
+            // - If interrupting media session category is Transient Solo:
+            //   If media session's current media session type is Default, Content, Transient, or Transient Solo, then
+            //   unpause the media session's active audio-producing participants and set media session's current state to active.
+            if (session-&gt;kindEnum() != MediaSession::Kind::Ambient)
+                session-&gt;handleUnpauseInterruption();
+        }
+    }
+}
+
+} // namespace WebCore
+
</ins><span class="cx"> #endif /* ENABLE(MEDIA_SESSION) */
</span></span></pre></div>
<a id="trunkSourceWebCoreModulesmediasessionMediaSessionManagerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/Modules/mediasession/MediaSessionManager.h (186917 => 186918)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/Modules/mediasession/MediaSessionManager.h        2015-07-16 23:22:36 UTC (rev 186917)
+++ trunk/Source/WebCore/Modules/mediasession/MediaSessionManager.h        2015-07-16 23:36:13 UTC (rev 186918)
</span><span class="lines">@@ -28,13 +28,16 @@
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(MEDIA_SESSION)
</span><span class="cx"> 
</span><ins>+#include &quot;MediaSessionInterruptionProvider.h&quot;
</ins><span class="cx"> #include &lt;wtf/HashSet.h&gt;
</span><ins>+#include &lt;wtf/NeverDestroyed.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="cx"> class MediaSession;
</span><span class="cx"> 
</span><del>-class MediaSessionManager {
</del><ins>+class MediaSessionManager : public MediaSessionInterruptionProviderClient {
+    friend class NeverDestroyed&lt;MediaSessionManager&gt;;
</ins><span class="cx"> public:
</span><span class="cx">     static MediaSessionManager&amp; singleton();
</span><span class="cx"> 
</span><span class="lines">@@ -42,14 +45,20 @@
</span><span class="cx">     void skipToNextTrack();
</span><span class="cx">     void skipToPreviousTrack();
</span><span class="cx"> 
</span><ins>+    void didReceiveStartOfInterruptionNotification(MediaSessionInterruptingCategory) override;
+    void didReceiveEndOfInterruptionNotification(MediaSessionInterruptingCategory) override;
+
</ins><span class="cx"> private:
</span><span class="cx">     friend class MediaSession;
</span><span class="cx"> 
</span><ins>+    MediaSessionManager();
+
</ins><span class="cx">     bool hasActiveMediaElements() const;
</span><span class="cx"> 
</span><span class="cx">     void addMediaSession(MediaSession&amp;);
</span><span class="cx">     void removeMediaSession(MediaSession&amp;);
</span><span class="cx"> 
</span><ins>+    RefPtr&lt;MediaSessionInterruptionProvider&gt; m_interruptionProvider;
</ins><span class="cx">     HashSet&lt;MediaSession*&gt; m_sessions;
</span><span class="cx"> };
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>