<!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>[181005] 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/181005">181005</a></dd>
<dt>Author</dt> <dd>jer.noble@apple.com</dd>
<dt>Date</dt> <dd>2015-03-04 11:22:59 -0800 (Wed, 04 Mar 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>[MSE][EME][Mac] Calling close on a MediaKeysSession will cause many decoding errors to be emitted
https://bugs.webkit.org/show_bug.cgi?id=142285

Reviewed by Eric Carlson.

When a MediaKeySession (backed by CDMSessionMediaSourceAVFObjC) is closed and the
underlying AVStreamSession is invalidated, the decryption context for in-flight
CMSampleBuffers is also invalidated, and the AVSampleBufferDisplayLayer will issue
one error for each enqueued and un-displayed sample in its image-queue. -flush-ing
the AVSampleBufferDisplayLayer is not enough, as the flush only takes effect
asynchronously the next time the layer needs new samples.

Add a workaround until framework-level support lands to fully flush enqueued and
encrypted frames.

When the CDMSessionMediaSOurceAVFObjC object recieves an error from the layer,
check to see if the session has been stopped. If so, and if the error in question is
one that indicates that the samples decryption context has been invalidated, suppress
the error and instruct the sender to suppress the error as well. This workaround will
be removed once real support for synchronous flushing lands in &lt;rdar://problem/20027434.&gt;

Still, we'll make our best effort to flush undisplayed frames when our CDM session is
invalidated. Move away from std::map and instead use HashMap to store the set of
AVSampleBufferAudioRenderers. This allows us to use C++11 style loops against just
the HashMap's set of values.

* platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.h:
* platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.mm:
(WebCore::CDMSessionMediaSourceAVFObjC::releaseKeys): Flush and set m_stopped.
(WebCore::CDMSessionMediaSourceAVFObjC::layerDidReceiveError): Check m_stopped and the
    error code and bail before issuing the error.
(WebCore::CDMSessionMediaSourceAVFObjC::rendererDidReceiveError): Ditto.
* platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h:
* platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm:
(-[WebAVSampleBufferErrorListener layerFailedToDecode:]): Drive-by fix. Check whether
    the layer is in the set of listened-to layers only back in the main thread; the
    listnener may have been unregistered by the time the main thread was called.
(WebCore::SourceBufferPrivateAVFObjC::destroyRenderers): std::map -&gt; HashMap.
(WebCore::SourceBufferPrivateAVFObjC::trackDidChangeEnabled): Ditto.
(WebCore::SourceBufferPrivateAVFObjC::flushAndEnqueueNonDisplayingSamples): Ditto.
(WebCore::SourceBufferPrivateAVFObjC::enqueueSample): Ditto.
(WebCore::SourceBufferPrivateAVFObjC::isReadyForMoreSamples): Ditto.
(WebCore::SourceBufferPrivateAVFObjC::didBecomeReadyForMoreSamples): Ditto.
(WebCore::SourceBufferPrivateAVFObjC::notifyClientWhenReadyForMoreSamples): Ditto.
(WebCore::SourceBufferPrivateAVFObjC::flush): Added; call -flush on all the display
    layers and audio renderers.
(WebCore::SourceBufferPrivateAVFObjC::layerDidReceiveError): Check if any clients
    asked to ignore the error, and if so, bail.
(WebCore::SourceBufferPrivateAVFObjC::rendererDidReceiveError): Ditto.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebCoreChangeLog">trunk/Source/WebCore/ChangeLog</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsavfoundationobjcCDMSessionMediaSourceAVFObjCh">trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.h</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsavfoundationobjcCDMSessionMediaSourceAVFObjCmm">trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.mm</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsavfoundationobjcSourceBufferPrivateAVFObjCh">trunk/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h</a></li>
<li><a href="#trunkSourceWebCoreplatformgraphicsavfoundationobjcSourceBufferPrivateAVFObjCmm">trunk/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/ChangeLog (181004 => 181005)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/ChangeLog        2015-03-04 18:48:50 UTC (rev 181004)
+++ trunk/Source/WebCore/ChangeLog        2015-03-04 19:22:59 UTC (rev 181005)
</span><span class="lines">@@ -1,3 +1,55 @@
</span><ins>+2015-03-04  Jer Noble  &lt;jer.noble@apple.com&gt;
+
+        [MSE][EME][Mac] Calling close on a MediaKeysSession will cause many decoding errors to be emitted
+        https://bugs.webkit.org/show_bug.cgi?id=142285
+
+        Reviewed by Eric Carlson.
+
+        When a MediaKeySession (backed by CDMSessionMediaSourceAVFObjC) is closed and the
+        underlying AVStreamSession is invalidated, the decryption context for in-flight
+        CMSampleBuffers is also invalidated, and the AVSampleBufferDisplayLayer will issue
+        one error for each enqueued and un-displayed sample in its image-queue. -flush-ing
+        the AVSampleBufferDisplayLayer is not enough, as the flush only takes effect
+        asynchronously the next time the layer needs new samples.
+
+        Add a workaround until framework-level support lands to fully flush enqueued and
+        encrypted frames.
+
+        When the CDMSessionMediaSOurceAVFObjC object recieves an error from the layer,
+        check to see if the session has been stopped. If so, and if the error in question is
+        one that indicates that the samples decryption context has been invalidated, suppress
+        the error and instruct the sender to suppress the error as well. This workaround will
+        be removed once real support for synchronous flushing lands in &lt;rdar://problem/20027434.&gt;
+
+        Still, we'll make our best effort to flush undisplayed frames when our CDM session is
+        invalidated. Move away from std::map and instead use HashMap to store the set of 
+        AVSampleBufferAudioRenderers. This allows us to use C++11 style loops against just
+        the HashMap's set of values.
+
+        * platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.h:
+        * platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.mm:
+        (WebCore::CDMSessionMediaSourceAVFObjC::releaseKeys): Flush and set m_stopped.
+        (WebCore::CDMSessionMediaSourceAVFObjC::layerDidReceiveError): Check m_stopped and the
+            error code and bail before issuing the error.
+        (WebCore::CDMSessionMediaSourceAVFObjC::rendererDidReceiveError): Ditto.
+        * platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h:
+        * platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm:
+        (-[WebAVSampleBufferErrorListener layerFailedToDecode:]): Drive-by fix. Check whether
+            the layer is in the set of listened-to layers only back in the main thread; the
+            listnener may have been unregistered by the time the main thread was called.
+        (WebCore::SourceBufferPrivateAVFObjC::destroyRenderers): std::map -&gt; HashMap.
+        (WebCore::SourceBufferPrivateAVFObjC::trackDidChangeEnabled): Ditto.
+        (WebCore::SourceBufferPrivateAVFObjC::flushAndEnqueueNonDisplayingSamples): Ditto.
+        (WebCore::SourceBufferPrivateAVFObjC::enqueueSample): Ditto.
+        (WebCore::SourceBufferPrivateAVFObjC::isReadyForMoreSamples): Ditto.
+        (WebCore::SourceBufferPrivateAVFObjC::didBecomeReadyForMoreSamples): Ditto.
+        (WebCore::SourceBufferPrivateAVFObjC::notifyClientWhenReadyForMoreSamples): Ditto.
+        (WebCore::SourceBufferPrivateAVFObjC::flush): Added; call -flush on all the display
+            layers and audio renderers.
+        (WebCore::SourceBufferPrivateAVFObjC::layerDidReceiveError): Check if any clients
+            asked to ignore the error, and if so, bail.
+        (WebCore::SourceBufferPrivateAVFObjC::rendererDidReceiveError): Ditto.
+
</ins><span class="cx"> 2015-03-04  Alex Christensen  &lt;achristensen@webkit.org&gt;
</span><span class="cx"> 
</span><span class="cx">         Update bindings tests after r181001.
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsavfoundationobjcCDMSessionMediaSourceAVFObjCh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.h (181004 => 181005)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.h        2015-03-04 18:48:50 UTC (rev 181004)
+++ trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.h        2015-03-04 19:22:59 UTC (rev 181005)
</span><span class="lines">@@ -50,8 +50,8 @@
</span><span class="cx">     virtual void releaseKeys() override;
</span><span class="cx">     virtual bool update(Uint8Array*, RefPtr&lt;Uint8Array&gt;&amp; nextMessage, unsigned short&amp; errorCode, unsigned long&amp; systemCode) override;
</span><span class="cx"> 
</span><del>-    virtual void layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *);
-    virtual void rendererDidReceiveError(AVSampleBufferAudioRenderer *, NSError *);
</del><ins>+    virtual void layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *, bool&amp; shouldIgnore);
+    virtual void rendererDidReceiveError(AVSampleBufferAudioRenderer *, NSError *, bool&amp; shouldIgnore);
</ins><span class="cx"> 
</span><span class="cx">     void setStreamSession(AVStreamSession *);
</span><span class="cx"> 
</span><span class="lines">@@ -74,6 +74,7 @@
</span><span class="cx">     Vector&lt;int&gt; m_protocolVersions;
</span><span class="cx">     String m_sessionId;
</span><span class="cx">     enum { Normal, KeyRelease } m_mode;
</span><ins>+    bool m_stopped = { false };
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> inline CDMSessionMediaSourceAVFObjC* toCDMSessionMediaSourceAVFObjC(CDMSession* session)
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsavfoundationobjcCDMSessionMediaSourceAVFObjCmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.mm (181004 => 181005)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.mm        2015-03-04 18:48:50 UTC (rev 181004)
+++ trunk/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.mm        2015-03-04 19:22:59 UTC (rev 181005)
</span><span class="lines">@@ -141,6 +141,10 @@
</span><span class="cx"> void CDMSessionMediaSourceAVFObjC::releaseKeys()
</span><span class="cx"> {
</span><span class="cx">     if (m_streamSession) {
</span><ins>+        m_stopped = true;
+        for (auto&amp; sourceBuffer : m_sourceBuffers)
+            sourceBuffer-&gt;flush();
+
</ins><span class="cx">         LOG(Media, &quot;CDMSessionMediaSourceAVFObjC::releaseKeys(%p) - expiring stream session&quot;, this);
</span><span class="cx">         [m_streamSession expire];
</span><span class="cx"> 
</span><span class="lines">@@ -282,20 +286,30 @@
</span><span class="cx">     return true;
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-void CDMSessionMediaSourceAVFObjC::layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *error)
</del><ins>+void CDMSessionMediaSourceAVFObjC::layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *error, bool&amp; shouldIgnore)
</ins><span class="cx"> {
</span><span class="cx">     if (!m_client)
</span><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    m_client-&gt;sendError(CDMSessionClient::MediaKeyErrorDomain, std::abs(systemCodeForError(error)));
</del><ins>+    unsigned long code = std::abs(systemCodeForError(error));
+
+    // FIXME(142246): Remove the following once &lt;rdar://problem/20027434&gt; is resolved.
+    shouldIgnore = m_stopped &amp;&amp; code == 12785;
+    if (!shouldIgnore)
+        m_client-&gt;sendError(CDMSessionClient::MediaKeyErrorDomain, code);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-void CDMSessionMediaSourceAVFObjC::rendererDidReceiveError(AVSampleBufferAudioRenderer *, NSError *error)
</del><ins>+void CDMSessionMediaSourceAVFObjC::rendererDidReceiveError(AVSampleBufferAudioRenderer *, NSError *error, bool&amp; shouldIgnore)
</ins><span class="cx"> {
</span><span class="cx">     if (!m_client)
</span><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    m_client-&gt;sendError(CDMSessionClient::MediaKeyErrorDomain, std::abs(systemCodeForError(error)));
</del><ins>+    unsigned long code = std::abs(systemCodeForError(error));
+
+    // FIXME(142246): Remove the following once &lt;rdar://problem/20027434&gt; is resolved.
+    shouldIgnore = m_stopped &amp;&amp; code == 12785;
+    if (!shouldIgnore)
+        m_client-&gt;sendError(CDMSessionClient::MediaKeyErrorDomain, code);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void CDMSessionMediaSourceAVFObjC::setStreamSession(AVStreamSession *streamSession)
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsavfoundationobjcSourceBufferPrivateAVFObjCh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h (181004 => 181005)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h        2015-03-04 18:48:50 UTC (rev 181004)
+++ trunk/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h        2015-03-04 19:22:59 UTC (rev 181005)
</span><span class="lines">@@ -29,7 +29,6 @@
</span><span class="cx"> #if ENABLE(MEDIA_SOURCE) &amp;&amp; USE(AVFOUNDATION)
</span><span class="cx"> 
</span><span class="cx"> #include &quot;SourceBufferPrivate.h&quot;
</span><del>-#include &lt;map&gt;
</del><span class="cx"> #include &lt;wtf/Deque.h&gt;
</span><span class="cx"> #include &lt;wtf/HashMap.h&gt;
</span><span class="cx"> #include &lt;wtf/MediaTime.h&gt;
</span><span class="lines">@@ -64,8 +63,8 @@
</span><span class="cx"> class SourceBufferPrivateAVFObjCErrorClient {
</span><span class="cx"> public:
</span><span class="cx">     virtual ~SourceBufferPrivateAVFObjCErrorClient() { }
</span><del>-    virtual void layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *) = 0;
-    virtual void rendererDidReceiveError(AVSampleBufferAudioRenderer *, NSError *) = 0;
</del><ins>+    virtual void layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *, bool&amp; shouldIgnore) = 0;
+    virtual void rendererDidReceiveError(AVSampleBufferAudioRenderer *, NSError *, bool&amp; shouldIgnore) = 0;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> class SourceBufferPrivateAVFObjC final : public SourceBufferPrivate {
</span><span class="lines">@@ -98,6 +97,8 @@
</span><span class="cx">     int protectedTrackID() const { return m_protectedTrackID; }
</span><span class="cx">     AVStreamDataParser* parser() const { return m_parser.get(); }
</span><span class="cx"> 
</span><ins>+    void flush();
+
</ins><span class="cx">     void registerForErrorNotifications(SourceBufferPrivateAVFObjCErrorClient*);
</span><span class="cx">     void unregisterForErrorNotifications(SourceBufferPrivateAVFObjCErrorClient*);
</span><span class="cx">     void layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *);
</span><span class="lines">@@ -138,7 +139,7 @@
</span><span class="cx">     RetainPtr&lt;AVStreamDataParser&gt; m_parser;
</span><span class="cx">     RetainPtr&lt;AVAsset&gt; m_asset;
</span><span class="cx">     RetainPtr&lt;AVSampleBufferDisplayLayer&gt; m_displayLayer;
</span><del>-    std::map&lt;int, RetainPtr&lt;AVSampleBufferAudioRenderer&gt;&gt; m_audioRenderers;
</del><ins>+    HashMap&lt;int, RetainPtr&lt;AVSampleBufferAudioRenderer&gt;&gt; m_audioRenderers;
</ins><span class="cx">     RetainPtr&lt;WebAVStreamDataParserListener&gt; m_delegate;
</span><span class="cx">     RetainPtr&lt;WebAVSampleBufferErrorListener&gt; m_errorListener;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebCoreplatformgraphicsavfoundationobjcSourceBufferPrivateAVFObjCmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm (181004 => 181005)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm        2015-03-04 18:48:50 UTC (rev 181004)
+++ trunk/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm        2015-03-04 19:22:59 UTC (rev 181005)
</span><span class="lines">@@ -29,6 +29,7 @@
</span><span class="cx"> #if ENABLE(MEDIA_SOURCE) &amp;&amp; USE(AVFOUNDATION)
</span><span class="cx"> 
</span><span class="cx"> #import &quot;BlockExceptions.h&quot;
</span><ins>+#import &quot;CDMSessionMediaSourceAVFObjC.h&quot;
</ins><span class="cx"> #import &quot;ExceptionCodePlaceholder.h&quot;
</span><span class="cx"> #import &quot;Logging.h&quot;
</span><span class="cx"> #import &quot;MediaDescription.h&quot;
</span><span class="lines">@@ -149,6 +150,7 @@
</span><span class="cx"> - (NSError*)error;
</span><span class="cx"> - (void)enqueueSampleBuffer:(CMSampleBufferRef)sampleBuffer;
</span><span class="cx"> - (void)flush;
</span><ins>+- (void)flushAndRemoveImage;
</ins><span class="cx"> - (BOOL)isReadyForMoreMediaData;
</span><span class="cx"> - (void)requestMediaDataWhenReadyOnQueue:(dispatch_queue_t)queue usingBlock:(void (^)(void))block;
</span><span class="cx"> - (void)stopRequestingMediaData;
</span><span class="lines">@@ -452,12 +454,12 @@
</span><span class="cx"> - (void)layerFailedToDecode:(NSNotification*)note
</span><span class="cx"> {
</span><span class="cx">     RetainPtr&lt;AVSampleBufferDisplayLayer&gt; layer = (AVSampleBufferDisplayLayer *)[note object];
</span><del>-    ASSERT(_layers.contains(layer.get()));
-
</del><span class="cx">     RetainPtr&lt;NSError&gt; error = [[note userInfo] valueForKey:AVSampleBufferDisplayLayerFailedToDecodeNotificationErrorKey];
</span><span class="cx"> 
</span><span class="cx">     RetainPtr&lt;WebAVSampleBufferErrorListener&gt; strongSelf = self;
</span><span class="cx">     callOnMainThread([strongSelf, layer, error] {
</span><ins>+        if (!strongSelf-&gt;_parent || !strongSelf-&gt;_layers.contains(layer.get()))
+            return;
</ins><span class="cx">         strongSelf-&gt;_parent-&gt;layerDidReceiveError(layer.get(), error.get());
</span><span class="cx">     });
</span><span class="cx"> }
</span><span class="lines">@@ -829,13 +831,12 @@
</span><span class="cx">         m_displayLayer = nullptr;
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    for (auto it = m_audioRenderers.begin(), end = m_audioRenderers.end(); it != end; ++it) {
-        AVSampleBufferAudioRenderer* renderer = it-&gt;second.get();
</del><ins>+    for (auto&amp; renderer : m_audioRenderers.values()) {
</ins><span class="cx">         if (m_mediaSource)
</span><del>-            m_mediaSource-&gt;player()-&gt;removeAudioRenderer(renderer);
</del><ins>+            m_mediaSource-&gt;player()-&gt;removeAudioRenderer(renderer.get());
</ins><span class="cx">         [renderer flush];
</span><span class="cx">         [renderer stopRequestingMediaData];
</span><del>-        [m_errorListener stopObservingRenderer:renderer];
</del><ins>+        [m_errorListener stopObservingRenderer:renderer.get()];
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     m_audioRenderers.clear();
</span><span class="lines">@@ -908,28 +909,37 @@
</span><span class="cx">     int trackID = track-&gt;trackID();
</span><span class="cx"> 
</span><span class="cx">     if (!track-&gt;enabled()) {
</span><del>-        AVSampleBufferAudioRenderer* renderer = m_audioRenderers[trackID].get();
</del><ins>+        RetainPtr&lt;AVSampleBufferAudioRenderer&gt; renderer = m_audioRenderers.get(trackID);
</ins><span class="cx">         [m_parser setShouldProvideMediaData:NO forTrackID:trackID];
</span><span class="cx">         if (m_mediaSource)
</span><del>-            m_mediaSource-&gt;player()-&gt;removeAudioRenderer(renderer);
</del><ins>+            m_mediaSource-&gt;player()-&gt;removeAudioRenderer(renderer.get());
</ins><span class="cx">     } else {
</span><span class="cx">         [m_parser setShouldProvideMediaData:YES forTrackID:trackID];
</span><span class="cx">         RetainPtr&lt;AVSampleBufferAudioRenderer&gt; renderer;
</span><del>-        if (!m_audioRenderers.count(trackID)) {
</del><ins>+        if (!m_audioRenderers.contains(trackID)) {
</ins><span class="cx">             renderer = adoptNS([allocAVSampleBufferAudioRendererInstance() init]);
</span><span class="cx">             [renderer requestMediaDataWhenReadyOnQueue:dispatch_get_main_queue() usingBlock:^{
</span><span class="cx">                 didBecomeReadyForMoreSamples(trackID);
</span><span class="cx">             }];
</span><del>-            m_audioRenderers[trackID] = renderer;
</del><ins>+            m_audioRenderers.set(trackID, renderer);
</ins><span class="cx">             [m_errorListener beginObservingRenderer:renderer.get()];
</span><span class="cx">         } else
</span><del>-            renderer = m_audioRenderers[trackID].get();
</del><ins>+            renderer = m_audioRenderers.get(trackID);
</ins><span class="cx"> 
</span><span class="cx">         if (m_mediaSource)
</span><span class="cx">             m_mediaSource-&gt;player()-&gt;addAudioRenderer(renderer.get());
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+void SourceBufferPrivateAVFObjC::flush()
+{
+    if (m_displayLayer)
+        [m_displayLayer flushAndRemoveImage];
+
+    for (auto&amp; renderer : m_audioRenderers.values())
+        [renderer flush];
+}
+
</ins><span class="cx"> void SourceBufferPrivateAVFObjC::registerForErrorNotifications(SourceBufferPrivateAVFObjCErrorClient* client)
</span><span class="cx"> {
</span><span class="cx">     ASSERT(!m_errorClients.contains(client));
</span><span class="lines">@@ -945,9 +955,17 @@
</span><span class="cx"> void SourceBufferPrivateAVFObjC::layerDidReceiveError(AVSampleBufferDisplayLayer *layer, NSError *error)
</span><span class="cx"> {
</span><span class="cx">     LOG(MediaSource, &quot;SourceBufferPrivateAVFObjC::layerDidReceiveError(%p): layer(%p), error(%@)&quot;, this, layer, [error description]);
</span><del>-    for (auto&amp; client : m_errorClients)
-        client-&gt;layerDidReceiveError(layer, error);
</del><span class="cx"> 
</span><ins>+    // FIXME(142246): Remove the following once &lt;rdar://problem/20027434&gt; is resolved.
+    bool anyIgnored = false;
+    for (auto&amp; client : m_errorClients) {
+        bool shouldIgnore = false;
+        client-&gt;layerDidReceiveError(layer, error, shouldIgnore);
+        anyIgnored |= shouldIgnore;
+    }
+    if (anyIgnored)
+        return;
+
</ins><span class="cx">     int errorCode = [[[error userInfo] valueForKey:@&quot;OSStatus&quot;] intValue];
</span><span class="cx"> 
</span><span class="cx">     if (m_client)
</span><span class="lines">@@ -957,8 +975,16 @@
</span><span class="cx"> void SourceBufferPrivateAVFObjC::rendererDidReceiveError(AVSampleBufferAudioRenderer *renderer, NSError *error)
</span><span class="cx"> {
</span><span class="cx">     LOG(MediaSource, &quot;SourceBufferPrivateAVFObjC::rendererDidReceiveError(%p): renderer(%p), error(%@)&quot;, this, renderer, [error description]);
</span><del>-    for (auto&amp; client : m_errorClients)
-        client-&gt;rendererDidReceiveError(renderer, error);
</del><ins>+
+    // FIXME(142246): Remove the following once &lt;rdar://problem/20027434&gt; is resolved.
+    bool anyIgnored = false;
+    for (auto&amp; client : m_errorClients) {
+        bool shouldIgnore = false;
+        client-&gt;rendererDidReceiveError(renderer, error, shouldIgnore);
+        anyIgnored |= shouldIgnore;
+    }
+    if (anyIgnored)
+        return;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static RetainPtr&lt;CMSampleBufferRef&gt; createNonDisplayingCopy(CMSampleBufferRef sampleBuffer)
</span><span class="lines">@@ -984,8 +1010,8 @@
</span><span class="cx"> 
</span><span class="cx">     if (trackID == m_enabledVideoTrackID)
</span><span class="cx">         flushAndEnqueueNonDisplayingSamples(mediaSamples, m_displayLayer.get());
</span><del>-    else if (m_audioRenderers.count(trackID))
-        flushAndEnqueueNonDisplayingSamples(mediaSamples, m_audioRenderers[trackID].get());
</del><ins>+    else if (m_audioRenderers.contains(trackID))
+        flushAndEnqueueNonDisplayingSamples(mediaSamples, m_audioRenderers.get(trackID).get());
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void SourceBufferPrivateAVFObjC::flushAndEnqueueNonDisplayingSamples(Vector&lt;RefPtr&lt;MediaSample&gt;&gt; mediaSamples, AVSampleBufferAudioRenderer* renderer)
</span><span class="lines">@@ -1028,7 +1054,7 @@
</span><span class="cx"> void SourceBufferPrivateAVFObjC::enqueueSample(PassRefPtr&lt;MediaSample&gt; prpMediaSample, AtomicString trackIDString)
</span><span class="cx"> {
</span><span class="cx">     int trackID = trackIDString.toInt();
</span><del>-    if (trackID != m_enabledVideoTrackID &amp;&amp; !m_audioRenderers.count(trackID))
</del><ins>+    if (trackID != m_enabledVideoTrackID &amp;&amp; !m_audioRenderers.contains(trackID))
</ins><span class="cx">         return;
</span><span class="cx"> 
</span><span class="cx">     RefPtr&lt;MediaSample&gt; mediaSample = prpMediaSample;
</span><span class="lines">@@ -1044,7 +1070,7 @@
</span><span class="cx">         if (m_mediaSource)
</span><span class="cx">             m_mediaSource-&gt;player()-&gt;setHasAvailableVideoFrame(true);
</span><span class="cx">     } else
</span><del>-        [m_audioRenderers[trackID] enqueueSampleBuffer:platformSample.sample.cmSampleBuffer];
</del><ins>+        [m_audioRenderers.get(trackID) enqueueSampleBuffer:platformSample.sample.cmSampleBuffer];
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> bool SourceBufferPrivateAVFObjC::isReadyForMoreSamples(AtomicString trackIDString)
</span><span class="lines">@@ -1052,8 +1078,8 @@
</span><span class="cx">     int trackID = trackIDString.toInt();
</span><span class="cx">     if (trackID == m_enabledVideoTrackID)
</span><span class="cx">         return [m_displayLayer isReadyForMoreMediaData];
</span><del>-    else if (m_audioRenderers.count(trackID))
-        return [m_audioRenderers[trackID] isReadyForMoreMediaData];
</del><ins>+    else if (m_audioRenderers.contains(trackID))
+        return [m_audioRenderers.get(trackID) isReadyForMoreMediaData];
</ins><span class="cx">     else
</span><span class="cx">         ASSERT_NOT_REACHED();
</span><span class="cx"> 
</span><span class="lines">@@ -1088,8 +1114,8 @@
</span><span class="cx"> {
</span><span class="cx">     if (trackID == m_enabledVideoTrackID)
</span><span class="cx">         [m_displayLayer stopRequestingMediaData];
</span><del>-    else if (m_audioRenderers.count(trackID))
-        [m_audioRenderers[trackID] stopRequestingMediaData];
</del><ins>+    else if (m_audioRenderers.contains(trackID))
+        [m_audioRenderers.get(trackID) stopRequestingMediaData];
</ins><span class="cx">     else {
</span><span class="cx">         ASSERT_NOT_REACHED();
</span><span class="cx">         return;
</span><span class="lines">@@ -1106,8 +1132,8 @@
</span><span class="cx">         [m_displayLayer requestMediaDataWhenReadyOnQueue:dispatch_get_main_queue() usingBlock:^{
</span><span class="cx">             didBecomeReadyForMoreSamples(trackID);
</span><span class="cx">         }];
</span><del>-    } else if (m_audioRenderers.count(trackID)) {
-        [m_audioRenderers[trackID] requestMediaDataWhenReadyOnQueue:dispatch_get_main_queue() usingBlock:^{
</del><ins>+    } else if (m_audioRenderers.contains(trackID)) {
+        [m_audioRenderers.get(trackID) requestMediaDataWhenReadyOnQueue:dispatch_get_main_queue() usingBlock:^{
</ins><span class="cx">             didBecomeReadyForMoreSamples(trackID);
</span><span class="cx">         }];
</span><span class="cx">     } else
</span></span></pre>
</div>
</div>

</body>
</html>