<!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>[201524] 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/201524">201524</a></dd>
<dt>Author</dt> <dd>jer.noble@apple.com</dd>
<dt>Date</dt> <dd>2016-05-31 14:24:19 -0700 (Tue, 31 May 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>[EME] Sound continues playing when video's src is changed
https://bugs.webkit.org/show_bug.cgi?id=158233

Reviewed by Eric Carlson.

When CDMSessionAVFoundation began listening for outputObscuredDueToInsufficientExternalProtection
KVO notifications, it retained the AVPlayer owned by MediaPlayerPrivateAVFoundationObjC, which
caused the AVPlayer to outlive its original owner, and to continue playing even after the
MediaPlayerPrivateAVFoundationObjC had been destroyed.

Rather than observe for outputObscuredDueToInsufficientExternalProtection changes in
CDMSessionAVFoundation, add a backreference from the media player to the session, listen for changes
in the player, and have the player notify the session when the value of that property changes.

* platform/graphics/avfoundation/objc/CDMSessionAVFoundationObjC.h:
(WebCore::CDMSessionAVFoundationObjC::createWeakPtr):
* platform/graphics/avfoundation/objc/CDMSessionAVFoundationObjC.mm:
(WebCore::CDMSessionAVFoundationObjC::CDMSessionAVFoundationObjC):
(-[WebCDMSessionAVFoundationObjCListener initWithParent:player:]): Deleted.
(-[WebCDMSessionAVFoundationObjCListener invalidate]): Deleted.
(-[WebCDMSessionAVFoundationObjCListener observeValueForKeyPath:ofObject:change:context:]): Deleted.
(WebCore::CDMSessionAVFoundationObjC::~CDMSessionAVFoundationObjC): Deleted.
* platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.h:
* platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:
(WebCore::MediaPlayerPrivateAVFoundationObjC::removeSession):
(WebCore::MediaPlayerPrivateAVFoundationObjC::createSession):
(WebCore::MediaPlayerPrivateAVFoundationObjC::outputObscuredDueToInsufficientExternalProtectionChanged):
(WebCore::playerKVOProperties):
(-[WebCoreAVFMovieObserver observeValueForKeyPath:ofObject:change:context:]):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsavfoundationobjcCDMSessionAVFoundationObjCh">trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionAVFoundationObjC.h</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsavfoundationobjcCDMSessionAVFoundationObjCmm">trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionAVFoundationObjC.mm</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsavfoundationobjcMediaPlayerPrivateAVFoundationObjCh">trunk/Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.h</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsavfoundationobjcMediaPlayerPrivateAVFoundationObjCmm">trunk/Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (201523 => 201524)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2016-05-31 20:57:20 UTC (rev 201523)
+++ trunk/Source/WebCore/ChangeLog        2016-05-31 21:24:19 UTC (rev 201524)
</span><span class="lines">@@ -1,3 +1,35 @@
</span><ins>+2016-05-31  Jer Noble  &lt;jer.noble@apple.com&gt;
+
+        [EME] Sound continues playing when video's src is changed
+        https://bugs.webkit.org/show_bug.cgi?id=158233
+
+        Reviewed by Eric Carlson.
+
+        When CDMSessionAVFoundation began listening for outputObscuredDueToInsufficientExternalProtection
+        KVO notifications, it retained the AVPlayer owned by MediaPlayerPrivateAVFoundationObjC, which
+        caused the AVPlayer to outlive its original owner, and to continue playing even after the
+        MediaPlayerPrivateAVFoundationObjC had been destroyed.
+
+        Rather than observe for outputObscuredDueToInsufficientExternalProtection changes in
+        CDMSessionAVFoundation, add a backreference from the media player to the session, listen for changes
+        in the player, and have the player notify the session when the value of that property changes.
+
+        * platform/graphics/avfoundation/objc/CDMSessionAVFoundationObjC.h:
+        (WebCore::CDMSessionAVFoundationObjC::createWeakPtr):
+        * platform/graphics/avfoundation/objc/CDMSessionAVFoundationObjC.mm:
+        (WebCore::CDMSessionAVFoundationObjC::CDMSessionAVFoundationObjC):
+        (-[WebCDMSessionAVFoundationObjCListener initWithParent:player:]): Deleted.
+        (-[WebCDMSessionAVFoundationObjCListener invalidate]): Deleted.
+        (-[WebCDMSessionAVFoundationObjCListener observeValueForKeyPath:ofObject:change:context:]): Deleted.
+        (WebCore::CDMSessionAVFoundationObjC::~CDMSessionAVFoundationObjC): Deleted.
+        * platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.h:
+        * platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm:
+        (WebCore::MediaPlayerPrivateAVFoundationObjC::removeSession):
+        (WebCore::MediaPlayerPrivateAVFoundationObjC::createSession):
+        (WebCore::MediaPlayerPrivateAVFoundationObjC::outputObscuredDueToInsufficientExternalProtectionChanged):
+        (WebCore::playerKVOProperties):
+        (-[WebCoreAVFMovieObserver observeValueForKeyPath:ofObject:change:context:]):
+
</ins><span class="cx"> 2016-05-31  Eric Carlson  &lt;eric.carlson@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [Mac] AirPlay route is sometimes reset when changing video.src
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsavfoundationobjcCDMSessionAVFoundationObjCh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionAVFoundationObjC.h (201523 => 201524)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionAVFoundationObjC.h        2016-05-31 20:57:20 UTC (rev 201523)
+++ trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionAVFoundationObjC.h        2016-05-31 21:24:19 UTC (rev 201524)
</span><span class="lines">@@ -53,12 +53,14 @@
</span><span class="cx"> 
</span><span class="cx">     void playerDidReceiveError(NSError *);
</span><span class="cx"> 
</span><ins>+    WeakPtr&lt;CDMSessionAVFoundationObjC&gt; createWeakPtr() { return m_weakPtrFactory.createWeakPtr(); }
+
</ins><span class="cx"> protected:
</span><span class="cx">     WeakPtr&lt;MediaPlayerPrivateAVFoundationObjC&gt; m_parent;
</span><span class="cx">     CDMSessionClient* m_client;
</span><span class="cx">     String m_sessionId;
</span><span class="cx">     RetainPtr&lt;AVAssetResourceLoadingRequest&gt; m_request;
</span><del>-    RetainPtr&lt;WebCDMSessionAVFoundationObjCListener&gt; m_listener;
</del><ins>+    WeakPtrFactory&lt;CDMSessionAVFoundationObjC&gt; m_weakPtrFactory;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsavfoundationobjcCDMSessionAVFoundationObjCmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionAVFoundationObjC.mm (201523 => 201524)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionAVFoundationObjC.mm        2016-05-31 20:57:20 UTC (rev 201523)
+++ trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionAVFoundationObjC.mm        2016-05-31 21:24:19 UTC (rev 201524)
</span><span class="lines">@@ -46,68 +46,18 @@
</span><span class="cx"> #define AVURLAsset getAVURLAssetClass()
</span><span class="cx"> #define AVAssetResourceLoadingRequest getAVAssetResourceLoadingRequest()
</span><span class="cx"> 
</span><del>-@interface WebCDMSessionAVFoundationObjCListener : NSObject {
-    WebCore::CDMSessionAVFoundationObjC* _parent;
-    RetainPtr&lt;AVPlayer&gt; _player;
-}
-- (id)initWithParent:(WebCore::CDMSessionAVFoundationObjC*)parent player:(AVPlayer *)player;
-- (void)invalidate;
-@end
-
-@implementation WebCDMSessionAVFoundationObjCListener
-- (id)initWithParent:(WebCore::CDMSessionAVFoundationObjC*)parent player:(AVPlayer *)player
-{
-    self = [super init];
-    if (!self)
-        return nil;
-
-    _parent = parent;
-    _player = player;
-    [player addObserver:self forKeyPath:@&quot;outputObscuredDueToInsufficientExternalProtection&quot; options:NSKeyValueObservingOptionNew context:nil];
-
-    return self;
-}
-
-- (void)invalidate
-{
-    _parent = nullptr;
-    [_player removeObserver:self forKeyPath:@&quot;outputObscuredDueToInsufficientExternalProtection&quot;];
-    _player = nullptr;
-}
-
-- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
-{
-    UNUSED_PARAM(context);
-    UNUSED_PARAM(object);
-    ASSERT(_parent);
-
-    if ([keyPath isEqualTo:@&quot;outputObscuredDueToInsufficientExternalProtection&quot;]) {
-        if ([[change valueForKey:NSKeyValueChangeNewKey] intValue] == 1) {
-            RetainPtr&lt;NSError&gt; error = [NSError errorWithDomain:@&quot;com.apple.WebKit&quot; code:'HDCP' userInfo:nil];
-            RetainPtr&lt;WebCDMSessionAVFoundationObjCListener&gt; protectedSelf = { self };
-            callOnMainThread([protectedSelf = WTFMove(protectedSelf), error = WTFMove(error)] {
-                if (protectedSelf-&gt;_parent)
-                    protectedSelf-&gt;_parent-&gt;playerDidReceiveError(error.get());
-            });
-        }
-    } else
-        ASSERT_NOT_REACHED();
-}
-@end
-
</del><span class="cx"> namespace WebCore {
</span><span class="cx"> 
</span><span class="cx"> CDMSessionAVFoundationObjC::CDMSessionAVFoundationObjC(MediaPlayerPrivateAVFoundationObjC* parent, CDMSessionClient* client)
</span><span class="cx">     : m_parent(parent-&gt;createWeakPtr())
</span><span class="cx">     , m_client(client)
</span><span class="cx">     , m_sessionId(createCanonicalUUIDString())
</span><del>-    , m_listener(adoptNS([[WebCDMSessionAVFoundationObjCListener alloc] initWithParent:this player:parent-&gt;avPlayer()]))
</del><ins>+    , m_weakPtrFactory(this)
</ins><span class="cx"> {
</span><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> CDMSessionAVFoundationObjC::~CDMSessionAVFoundationObjC()
</span><span class="cx"> {
</span><del>-    [m_listener invalidate];
</del><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> RefPtr&lt;Uint8Array&gt; CDMSessionAVFoundationObjC::generateKeyRequest(const String&amp; mimeType, Uint8Array* initData, String&amp; destinationURL, unsigned short&amp; errorCode, uint32_t&amp; systemCode)
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsavfoundationobjcMediaPlayerPrivateAVFoundationObjCh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.h (201523 => 201524)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.h        2016-05-31 20:57:20 UTC (rev 201523)
+++ trunk/Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.h        2016-05-31 21:24:19 UTC (rev 201524)
</span><span class="lines">@@ -67,6 +67,7 @@
</span><span class="cx"> 
</span><span class="cx"> class AudioSourceProviderAVFObjC;
</span><span class="cx"> class AudioTrackPrivateAVFObjC;
</span><ins>+class CDMSessionAVFoundationObjC;
</ins><span class="cx"> class InbandMetadataTextTrackPrivateAVF;
</span><span class="cx"> class InbandTextTrackPrivateAVFObjC;
</span><span class="cx"> class MediaSelectionGroupAVFObjC;
</span><span class="lines">@@ -142,12 +143,20 @@
</span><span class="cx"> #if ENABLE(WIRELESS_PLAYBACK_TARGET)
</span><span class="cx">     void playbackTargetIsWirelessDidChange();
</span><span class="cx"> #endif
</span><del>-    
</del><ins>+
+#if ENABLE(ENCRYPTED_MEDIA_V2)
+    void outputObscuredDueToInsufficientExternalProtectionChanged(bool);
+#endif
+
</ins><span class="cx"> #if ENABLE(AVF_CAPTIONS)
</span><span class="cx">     void notifyTrackModeChanged() override;
</span><span class="cx">     void synchronizeTextTrackState() override;
</span><span class="cx"> #endif
</span><del>-    
</del><ins>+
+#if ENABLE(ENCRYPTED_MEDIA_V2)
+    void removeSession(CDMSession&amp;);
+#endif
+
</ins><span class="cx">     WeakPtr&lt;MediaPlayerPrivateAVFoundationObjC&gt; createWeakPtr() { return m_weakPtrFactory.createWeakPtr(); }
</span><span class="cx"> 
</span><span class="cx"> private:
</span><span class="lines">@@ -392,6 +401,10 @@
</span><span class="cx">     RefPtr&lt;MediaPlaybackTarget&gt; m_playbackTarget { nullptr };
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><ins>+#if ENABLE(ENCRYPTED_MEDIA_V2)
+    WeakPtr&lt;CDMSessionAVFoundationObjC&gt; m_session;
+#endif
+
</ins><span class="cx">     mutable RetainPtr&lt;NSArray&gt; m_cachedSeekableRanges;
</span><span class="cx">     mutable RetainPtr&lt;NSArray&gt; m_cachedLoadedRanges;
</span><span class="cx">     RetainPtr&lt;NSArray&gt; m_cachedTracks;
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsavfoundationobjcMediaPlayerPrivateAVFoundationObjCmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm (201523 => 201524)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm        2016-05-31 20:57:20 UTC (rev 201523)
+++ trunk/Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateAVFoundationObjC.mm        2016-05-31 21:24:19 UTC (rev 201524)
</span><span class="lines">@@ -2585,12 +2585,25 @@
</span><span class="cx">         m_keyURIToRequestMap.remove(keyId);
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void MediaPlayerPrivateAVFoundationObjC::removeSession(CDMSession&amp; session)
+{
+    ASSERT(&amp;session == m_session);
+    m_session = nullptr;
+}
+
</ins><span class="cx"> std::unique_ptr&lt;CDMSession&gt; MediaPlayerPrivateAVFoundationObjC::createSession(const String&amp; keySystem, CDMSessionClient* client)
</span><span class="cx"> {
</span><span class="cx">     if (!keySystemIsSupported(keySystem))
</span><span class="cx">         return nullptr;
</span><ins>+    auto session = std::make_unique&lt;CDMSessionAVFoundationObjC&gt;(this, client);
+    m_session = session-&gt;createWeakPtr();
+    return WTFMove(session);
+}
</ins><span class="cx"> 
</span><del>-    return std::make_unique&lt;CDMSessionAVFoundationObjC&gt;(this, client);
</del><ins>+void MediaPlayerPrivateAVFoundationObjC::outputObscuredDueToInsufficientExternalProtectionChanged(bool newValue)
+{
+    if (m_session &amp;&amp; newValue)
+        m_session-&gt;playerDidReceiveError([NSError errorWithDomain:@&quot;com.apple.WebKit&quot; code:'HDCP' userInfo:nil]);
</ins><span class="cx"> }
</span><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="lines">@@ -3294,6 +3307,9 @@
</span><span class="cx"> #if ENABLE(WIRELESS_PLAYBACK_TARGET)
</span><span class="cx">                             @&quot;externalPlaybackActive&quot;, @&quot;allowsExternalPlayback&quot;,
</span><span class="cx"> #endif
</span><ins>+#if ENABLE(ENCRYPTED_MEDIA_V2)
+                            @&quot;outputObscuredDueToInsufficientExternalProtection&quot;,
+#endif
</ins><span class="cx">                             nil];
</span><span class="cx">     return keys;
</span><span class="cx"> }
</span><span class="lines">@@ -3416,6 +3432,10 @@
</span><span class="cx">         else if ([keyPath isEqualToString:@&quot;externalPlaybackActive&quot;] || [keyPath isEqualToString:@&quot;allowsExternalPlayback&quot;])
</span><span class="cx">             function = std::bind(&amp;MediaPlayerPrivateAVFoundationObjC::playbackTargetIsWirelessDidChange, m_callback);
</span><span class="cx"> #endif
</span><ins>+#if ENABLE(ENCRYPTED_MEDIA_V2)
+        else if ([keyPath isEqualToString:@&quot;outputObscuredDueToInsufficientExternalProtection&quot;])
+            function = std::bind(&amp;MediaPlayerPrivateAVFoundationObjC::outputObscuredDueToInsufficientExternalProtectionChanged, m_callback, [newValue boolValue]);
+#endif
</ins><span class="cx">     }
</span><span class="cx">     
</span><span class="cx">     if (!function)
</span></span></pre>
</div>
</div>

</body>
</html>