Title: [169874] tags/Safari-538.39.41/Source/WebCore

Diff

Modified: tags/Safari-538.39.41/Source/WebCore/ChangeLog (169873 => 169874)


--- tags/Safari-538.39.41/Source/WebCore/ChangeLog	2014-06-12 02:55:22 UTC (rev 169873)
+++ tags/Safari-538.39.41/Source/WebCore/ChangeLog	2014-06-12 03:09:29 UTC (rev 169874)
@@ -1,3 +1,57 @@
+2014-06-11  Babak Shafiei  <[email protected]>
+
+        Merge r169830. <rdar://problem/16746383>     
+
+    2014-06-11  Jer Noble  <[email protected]>
+
+            [EME][Mac] Propagate errors from AVSampleBufferDisplayLayer through to MSE
+            https://bugs.webkit.org/show_bug.cgi?id=133747
+
+            Reviewed by Eric Carlson.
+
+            Abstract out the delivery of errors in MediaKeySession into its own method, which can be triggered by 
+            CDMSession objects through the CDMSessionClient interface:
+            * Modules/encryptedmedia/MediaKeySession.cpp:
+            (WebCore::MediaKeySession::keyRequestTimerFired):
+            (WebCore::MediaKeySession::addKeyTimerFired):
+            (WebCore::MediaKeySession::sendMessage):
+            (WebCore::MediaKeySession::sendError):
+            * Modules/encryptedmedia/MediaKeySession.h:
+            * Modules/mediasource/SourceBuffer.cpp:
+            (WebCore::SourceBuffer::sourceBufferPrivateDidReceiveRenderingError):
+            * Modules/mediasource/SourceBuffer.h:
+            * platform/graphics/SourceBufferPrivateClient.h:
+            * platform/graphics/CDMSession.h:
+
+            Add an Objective-C object which can listen for error KVO notifications:
+            * platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h:
+            (WebCore::SourceBufferPrivateAVFObjCErrorClient::~SourceBufferPrivateAVFObjCErrorClient):
+            * platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm:
+            (-[WebAVSampleBufferErrorListener initWithParent:WebCore::]):
+            (-[WebAVSampleBufferErrorListener dealloc]):
+            (-[WebAVSampleBufferErrorListener invalidate]):
+            (-[WebAVSampleBufferErrorListener beginObservingLayer:]):
+            (-[WebAVSampleBufferErrorListener stopObservingLayer:]):
+            (-[WebAVSampleBufferErrorListener beginObservingRenderer:]):
+            (-[WebAVSampleBufferErrorListener stopObservingRenderer:]):
+            (-[WebAVSampleBufferErrorListener observeValueForKeyPath:ofObject:change:context:]):
+            (-[WebAVSampleBufferErrorListener layerFailedToDecode:]):
+            (WebCore::SourceBufferPrivateAVFObjC::SourceBufferPrivateAVFObjC):
+            (WebCore::SourceBufferPrivateAVFObjC::trackDidChangeEnabled):
+            (WebCore::SourceBufferPrivateAVFObjC::registerForErrorNotifications):
+            (WebCore::SourceBufferPrivateAVFObjC::unregisterForErrorNotifications):
+            (WebCore::SourceBufferPrivateAVFObjC::layerDidReceiveError):
+            (WebCore::SourceBufferPrivateAVFObjC::rendererDidReceiveError):
+
+            Register for the new error notifications:
+            * platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.h:
+            (WebCore::CDMSessionMediaSourceAVFObjC::~CDMSessionMediaSourceAVFObjC): Moved to source file.
+            * platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.mm:
+            (WebCore::CDMSessionMediaSourceAVFObjC::CDMSessionMediaSourceAVFObjC): Register for error notifications.
+            (WebCore::CDMSessionMediaSourceAVFObjC::~CDMSessionMediaSourceAVFObjC): Unregister for error notifications.
+            (WebCore::CDMSessionMediaSourceAVFObjC::layerDidReceiveError): Pass through to CDMSessionClient.
+            (WebCore::CDMSessionMediaSourceAVFObjC::rendererDidReceiveError): Ditto.
+
 2014-06-10  Matthew Hanson  <[email protected]>
 
         Merge r169681.

Modified: tags/Safari-538.39.41/Source/WebCore/Modules/encryptedmedia/MediaKeySession.cpp (169873 => 169874)


--- tags/Safari-538.39.41/Source/WebCore/Modules/encryptedmedia/MediaKeySession.cpp	2014-06-12 02:55:22 UTC (rev 169873)
+++ tags/Safari-538.39.41/Source/WebCore/Modules/encryptedmedia/MediaKeySession.cpp	2014-06-12 03:09:29 UTC (rev 169874)
@@ -112,16 +112,9 @@
             // 3.1. Create a new MediaKeyError object with the following attributes:
             //      code = the appropriate MediaKeyError code
             //      systemCode = a Key System-specific value, if provided, and 0 otherwise
-            RefPtr<MediaKeyError> error = MediaKeyError::create(errorCode, systemCode).get();
-
             // 3.2. Set the MediaKeySession object's error attribute to the error object created in the previous step.
-            setError(error.get());
-
             // 3.3. queue a task to fire a simple event named keyerror at the MediaKeySession object.
-            RefPtr<Event> event = Event::create(eventNames().webkitkeyerrorEvent, false, false);
-            event->setTarget(this);
-            m_asyncEventQueue.enqueueEvent(event.release());
-
+            sendError(errorCode, systemCode);
             // 3.4. Abort the task.
             continue;
         }
@@ -130,14 +123,7 @@
         //    The event is of type MediaKeyMessageEvent and has:
         //    message = key request
         //    destinationURL = destinationURL
-        MediaKeyMessageEventInit init;
-        init.bubbles = false;
-        init.cancelable = false;
-        init.message = keyRequest;
-        init.destinationURL = destinationURL;
-        RefPtr<MediaKeyMessageEvent> event = MediaKeyMessageEvent::create(eventNames().webkitkeymessageEvent, init);
-        event->setTarget(this);
-        m_asyncEventQueue.enqueueEvent(event.release());
+        sendMessage(keyRequest.get(), destinationURL);
     }
 }
 
@@ -184,15 +170,8 @@
         //      The event is of type MediaKeyMessageEvent and has:
         //      message = next message
         //      destinationURL = null
-        if (nextMessage) {
-            MediaKeyMessageEventInit init;
-            init.bubbles = false;
-            init.cancelable = false;
-            init.message = nextMessage;
-            RefPtr<MediaKeyMessageEvent> event = MediaKeyMessageEvent::create(eventNames().webkitkeymessageEvent, init);
-            event->setTarget(this);
-            m_asyncEventQueue.enqueueEvent(event.release());
-        }
+        if (nextMessage)
+            sendMessage(nextMessage.get(), emptyString());
 
         // 2.7. If did store key is true, queue a task to fire a simple event named keyadded at the MediaKeySession object.
         if (didStoreKey) {
@@ -206,22 +185,37 @@
             // 2.8.1. Create a new MediaKeyError object with the following attributes:
             //        code = the appropriate MediaKeyError code
             //        systemCode = a Key System-specific value, if provided, and 0 otherwise
-            RefPtr<MediaKeyError> error = MediaKeyError::create(errorCode, systemCode).get();
-
             // 2.8.2. Set the MediaKeySession object's error attribute to the error object created in the previous step.
-            setError(error.get());
-
             // 2.8.3. queue a task to fire a simple event named keyerror at the MediaKeySession object.
-            RefPtr<Event> keyerrorEvent = Event::create(eventNames().webkitkeyerrorEvent, false, false);
-            keyerrorEvent->setTarget(this);
-            m_asyncEventQueue.enqueueEvent(keyerrorEvent.release());
-            
+            sendError(errorCode, systemCode);
             // 2.8.4. Abort the task.
             // NOTE: no-op
         }
     }
 }
 
+void MediaKeySession::sendMessage(Uint8Array* message, String destinationURL)
+{
+    MediaKeyMessageEventInit init;
+    init.bubbles = false;
+    init.cancelable = false;
+    init.message = message;
+    init.destinationURL = destinationURL;
+    RefPtr<MediaKeyMessageEvent> event = MediaKeyMessageEvent::create(eventNames().webkitkeymessageEvent, init);
+    event->setTarget(this);
+    m_asyncEventQueue.enqueueEvent(event.release());
 }
 
+void MediaKeySession::sendError(CDMSessionClient::MediaKeyErrorCode errorCode, unsigned long systemCode)
+{
+    RefPtr<MediaKeyError> error = MediaKeyError::create(errorCode, systemCode).get();
+    setError(error.get());
+
+    RefPtr<Event> keyerrorEvent = Event::create(eventNames().webkitkeyerrorEvent, false, false);
+    keyerrorEvent->setTarget(this);
+    m_asyncEventQueue.enqueueEvent(keyerrorEvent.release());
+}
+
+}
+
 #endif

Modified: tags/Safari-538.39.41/Source/WebCore/Modules/encryptedmedia/MediaKeySession.h (169873 => 169874)


--- tags/Safari-538.39.41/Source/WebCore/Modules/encryptedmedia/MediaKeySession.h	2014-06-12 02:55:22 UTC (rev 169873)
+++ tags/Safari-538.39.41/Source/WebCore/Modules/encryptedmedia/MediaKeySession.h	2014-06-12 03:09:29 UTC (rev 169874)
@@ -29,6 +29,7 @@
 #if ENABLE(ENCRYPTED_MEDIA_V2)
 
 #include "ActiveDOMObject.h"
+#include "CDMSession.h"
 #include "EventTarget.h"
 #include "ExceptionCode.h"
 #include "GenericEventQueue.h"
@@ -43,9 +44,8 @@
 
 class MediaKeyError;
 class MediaKeys;
-class CDMSession;
 
-class MediaKeySession final : public RefCounted<MediaKeySession>, public EventTargetWithInlineData, public ActiveDOMObject {
+class MediaKeySession final : public RefCounted<MediaKeySession>, public EventTargetWithInlineData, public ActiveDOMObject, public CDMSessionClient {
 public:
     static PassRefPtr<MediaKeySession> create(ScriptExecutionContext*, MediaKeys*, const String& keySystem);
     ~MediaKeySession();
@@ -85,6 +85,10 @@
     void keyRequestTimerFired(Timer<MediaKeySession>&);
     void addKeyTimerFired(Timer<MediaKeySession>&);
 
+    // CDMSessionClient
+    virtual void sendMessage(Uint8Array*, String destinationURL);
+    virtual void sendError(MediaKeyErrorCode, unsigned long systemCode);
+
     MediaKeys* m_keys;
     String m_keySystem;
     String m_sessionId;

Modified: tags/Safari-538.39.41/Source/WebCore/Modules/mediasource/SourceBuffer.cpp (169873 => 169874)


--- tags/Safari-538.39.41/Source/WebCore/Modules/mediasource/SourceBuffer.cpp	2014-06-12 02:55:22 UTC (rev 169873)
+++ tags/Safari-538.39.41/Source/WebCore/Modules/mediasource/SourceBuffer.cpp	2014-06-12 03:09:29 UTC (rev 169874)
@@ -535,6 +535,11 @@
     reportExtraMemoryCost();
 }
 
+void SourceBuffer::sourceBufferPrivateDidReceiveRenderingError(SourceBufferPrivate*, int)
+{
+    m_source->streamEndedWithError(decodeError(), IgnorableExceptionCode());
+}
+
 void SourceBuffer::removeCodedFrames(const MediaTime& start, const MediaTime& end)
 {
     // 3.5.9 Coded Frame Removal Algorithm

Modified: tags/Safari-538.39.41/Source/WebCore/Modules/mediasource/SourceBuffer.h (169873 => 169874)


--- tags/Safari-538.39.41/Source/WebCore/Modules/mediasource/SourceBuffer.h	2014-06-12 02:55:22 UTC (rev 169873)
+++ tags/Safari-538.39.41/Source/WebCore/Modules/mediasource/SourceBuffer.h	2014-06-12 03:09:29 UTC (rev 169874)
@@ -119,6 +119,7 @@
     virtual void sourceBufferPrivateSeekToTime(SourceBufferPrivate*, const MediaTime&);
     virtual MediaTime sourceBufferPrivateFastSeekTimeForMediaTime(SourceBufferPrivate*, const MediaTime&, const MediaTime& negativeThreshold, const MediaTime& positiveThreshold);
     virtual void sourceBufferPrivateAppendComplete(SourceBufferPrivate*, AppendResult) override;
+    virtual void sourceBufferPrivateDidReceiveRenderingError(SourceBufferPrivate*, int errorCode) override;
 
     // AudioTrackClient
     virtual void audioTrackEnabledChanged(AudioTrack*) override;

Modified: tags/Safari-538.39.41/Source/WebCore/platform/graphics/CDMSession.h (169873 => 169874)


--- tags/Safari-538.39.41/Source/WebCore/platform/graphics/CDMSession.h	2014-06-12 02:55:22 UTC (rev 169873)
+++ tags/Safari-538.39.41/Source/WebCore/platform/graphics/CDMSession.h	2014-06-12 03:09:29 UTC (rev 169874)
@@ -39,6 +39,17 @@
 public:
     virtual ~CDMSessionClient() { };
     virtual void sendMessage(Uint8Array*, String destinationURL) = 0;
+
+    enum {
+        MediaKeyErrorUnknown = 1,
+        MediaKeyErrorClient,
+        MediaKeyErrorService,
+        MediaKeyErrorOutput,
+        MediaKeyErrorHardwareChange,
+        MediaKeyErrorDomain,
+    };
+    typedef unsigned short MediaKeyErrorCode;
+    virtual void sendError(MediaKeyErrorCode, unsigned long systemCode) = 0;
 };
 
 class CDMSession {

Modified: tags/Safari-538.39.41/Source/WebCore/platform/graphics/SourceBufferPrivateClient.h (169873 => 169874)


--- tags/Safari-538.39.41/Source/WebCore/platform/graphics/SourceBufferPrivateClient.h	2014-06-12 02:55:22 UTC (rev 169873)
+++ tags/Safari-538.39.41/Source/WebCore/platform/graphics/SourceBufferPrivateClient.h	2014-06-12 03:09:29 UTC (rev 169874)
@@ -84,6 +84,7 @@
         ParsingFailed,
     };
     virtual void sourceBufferPrivateAppendComplete(SourceBufferPrivate*, AppendResult) = 0;
+    virtual void sourceBufferPrivateDidReceiveRenderingError(SourceBufferPrivate*, int errocCode) = 0;
 };
 
 }

Modified: tags/Safari-538.39.41/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.h (169873 => 169874)


--- tags/Safari-538.39.41/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.h	2014-06-12 02:55:22 UTC (rev 169873)
+++ tags/Safari-538.39.41/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.h	2014-06-12 03:09:29 UTC (rev 169874)
@@ -27,6 +27,7 @@
 #define CDMSessionMediaSourceAVFObjC_h
 
 #include "CDMSession.h"
+#include "SourceBufferPrivateAVFObjC.h"
 #include <wtf/PassOwnPtr.h>
 #include <wtf/RetainPtr.h>
 
@@ -34,12 +35,10 @@
 
 namespace WebCore {
 
-class SourceBufferPrivateAVFObjC;
-
-class CDMSessionMediaSourceAVFObjC : public CDMSession {
+class CDMSessionMediaSourceAVFObjC : public CDMSession, public SourceBufferPrivateAVFObjCErrorClient {
 public:
     CDMSessionMediaSourceAVFObjC(SourceBufferPrivateAVFObjC* parent);
-    virtual ~CDMSessionMediaSourceAVFObjC() { }
+    virtual ~CDMSessionMediaSourceAVFObjC();
 
     virtual void setClient(CDMSessionClient* client) override { m_client = client; }
     virtual const String& sessionId() const override { return m_sessionId; }
@@ -47,8 +46,11 @@
     virtual void releaseKeys() override;
     virtual bool update(Uint8Array*, RefPtr<Uint8Array>& nextMessage, unsigned short& errorCode, unsigned long& systemCode) override;
 
+    virtual void layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *);
+    virtual void rendererDidReceiveError(AVSampleBufferAudioRenderer *, NSError *);
+
 protected:
-    SourceBufferPrivateAVFObjC* m_parent;
+    RefPtr<SourceBufferPrivateAVFObjC> m_parent;
     CDMSessionClient* m_client;
     RefPtr<Uint8Array> m_initData;
     RefPtr<Uint8Array> m_certificate;

Modified: tags/Safari-538.39.41/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.mm (169873 => 169874)


--- tags/Safari-538.39.41/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.mm	2014-06-12 02:55:22 UTC (rev 169873)
+++ tags/Safari-538.39.41/Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionMediaSourceAVFObjC.mm	2014-06-12 03:09:29 UTC (rev 169874)
@@ -58,8 +58,16 @@
     , m_client(nullptr)
     , m_sessionId(createCanonicalUUIDString())
 {
+    ASSERT(parent);
+    parent->registerForErrorNotifications(this);
 }
 
+CDMSessionMediaSourceAVFObjC::~CDMSessionMediaSourceAVFObjC()
+{
+    m_parent->unregisterForErrorNotifications(this);
+    m_client = nullptr;
+}
+
 PassRefPtr<Uint8Array> CDMSessionMediaSourceAVFObjC::generateKeyRequest(const String& mimeType, Uint8Array* initData, String& destinationURL, unsigned short& errorCode, unsigned long& systemCode)
 {
     UNUSED_PARAM(mimeType);
@@ -137,6 +145,22 @@
     return true;
 }
 
+void CDMSessionMediaSourceAVFObjC::layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *error)
+{
+    if (!m_client)
+        return;
+
+    m_client->sendError(CDMSessionClient::MediaKeyErrorDomain, [error code]);
 }
 
+void CDMSessionMediaSourceAVFObjC::rendererDidReceiveError(AVSampleBufferAudioRenderer *, NSError *error)
+{
+    if (!m_client)
+        return;
+
+    m_client->sendError(CDMSessionClient::MediaKeyErrorDomain, [error code]);
+}
+
+}
+
 #endif

Modified: tags/Safari-538.39.41/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h (169873 => 169874)


--- tags/Safari-538.39.41/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h	2014-06-12 02:55:22 UTC (rev 169873)
+++ tags/Safari-538.39.41/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h	2014-06-12 03:09:29 UTC (rev 169874)
@@ -47,6 +47,8 @@
 OBJC_CLASS NSError;
 OBJC_CLASS NSObject;
 OBJC_CLASS WebAVStreamDataParserListener;
+OBJC_CLASS WebAVSampleBufferErrorListener;
+
 typedef struct opaqueCMSampleBuffer *CMSampleBufferRef;
 typedef const struct opaqueCMFormatDescription *CMFormatDescriptionRef;
 
@@ -59,6 +61,13 @@
 class AudioTrackPrivateMediaSourceAVFObjC;
 class VideoTrackPrivateMediaSourceAVFObjC;
 
+class SourceBufferPrivateAVFObjCErrorClient {
+public:
+    virtual ~SourceBufferPrivateAVFObjCErrorClient() { }
+    virtual void layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *) = 0;
+    virtual void rendererDidReceiveError(AVSampleBufferAudioRenderer *, NSError *) = 0;
+};
+
 class SourceBufferPrivateAVFObjC final : public SourceBufferPrivate {
 public:
     static RefPtr<SourceBufferPrivateAVFObjC> create(MediaSourcePrivateAVFObjC*);
@@ -88,6 +97,11 @@
     int protectedTrackID() const { return m_protectedTrackID; }
     AVStreamDataParser* parser() const { return m_parser.get(); }
 
+    void registerForErrorNotifications(SourceBufferPrivateAVFObjCErrorClient*);
+    void unregisterForErrorNotifications(SourceBufferPrivateAVFObjCErrorClient*);
+    void layerDidReceiveError(AVSampleBufferDisplayLayer *, NSError *);
+    void rendererDidReceiveError(AVSampleBufferAudioRenderer *, NSError *);
+
 private:
     explicit SourceBufferPrivateAVFObjC(MediaSourcePrivateAVFObjC*);
 
@@ -118,6 +132,7 @@
 
     Vector<RefPtr<VideoTrackPrivateMediaSourceAVFObjC>> m_videoTracks;
     Vector<RefPtr<AudioTrackPrivateMediaSourceAVFObjC>> m_audioTracks;
+    Vector<SourceBufferPrivateAVFObjCErrorClient*> m_errorClients;
 
     WeakPtrFactory<SourceBufferPrivateAVFObjC> m_weakFactory;
 
@@ -126,6 +141,7 @@
     RetainPtr<AVSampleBufferDisplayLayer> m_displayLayer;
     std::map<int, RetainPtr<AVSampleBufferAudioRenderer>> m_audioRenderers;
     RetainPtr<WebAVStreamDataParserListener> m_delegate;
+    RetainPtr<WebAVSampleBufferErrorListener> m_errorListener;
 
     MediaSourcePrivateAVFObjC* m_mediaSource;
     SourceBufferPrivateClient* m_client;

Modified: tags/Safari-538.39.41/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm (169873 => 169874)


--- tags/Safari-538.39.41/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm	2014-06-12 02:55:22 UTC (rev 169873)
+++ tags/Safari-538.39.41/Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm	2014-06-12 03:09:29 UTC (rev 169874)
@@ -79,6 +79,8 @@
 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicVisual, NSString*)
 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicAudible, NSString*)
 SOFT_LINK_CONSTANT(AVFoundation, AVMediaCharacteristicLegible, NSString*)
+SOFT_LINK_CONSTANT(AVFoundation, AVSampleBufferDisplayLayerFailedToDecodeNotification, NSString*)
+SOFT_LINK_CONSTANT(AVFoundation, AVSampleBufferDisplayLayerFailedToDecodeNotificationErrorKey, NSString*)
 
 SOFT_LINK(CoreMedia, CMFormatDescriptionGetMediaType, CMMediaType, (CMFormatDescriptionRef desc), (desc))
 SOFT_LINK(CoreMedia, CMSampleBufferCreate, OSStatus, (CFAllocatorRef allocator, CMBlockBufferRef dataBuffer, Boolean dataReady, CMSampleBufferMakeDataReadyCallback makeDataReadyCallback, void *makeDataReadyRefcon, CMFormatDescriptionRef formatDescription, CMItemCount numSamples, CMItemCount numSampleTimingEntries, const CMSampleTimingInfo *sampleTimingArray, CMItemCount numSampleSizeEntries, const size_t *sampleSizeArray, CMSampleBufferRef *sBufOut), (allocator, dataBuffer, dataReady, makeDataReadyCallback, makeDataReadyRefcon, formatDescription, numSamples, numSampleTimingEntries, sampleTimingArray, numSampleSizeEntries, sampleSizeArray, sBufOut))
@@ -97,6 +99,8 @@
 #define AVMediaTypeVideo getAVMediaTypeVideo()
 #define AVMediaTypeAudio getAVMediaTypeAudio()
 #define AVMediaTypeText getAVMediaTypeText()
+#define AVSampleBufferDisplayLayerFailedToDecodeNotification getAVSampleBufferDisplayLayerFailedToDecodeNotification()
+#define AVSampleBufferDisplayLayerFailedToDecodeNotificationErrorKey getAVSampleBufferDisplayLayerFailedToDecodeNotificationErrorKey()
 #define kCMTimeZero getkCMTimeZero()
 #define kCMTimeInvalid getkCMTimeInvalid()
 #define kCMSampleAttachmentKey_NotSync getkCMSampleAttachmentKey_NotSync()
@@ -279,6 +283,137 @@
 }
 @end
 
+@interface WebAVSampleBufferErrorListener : NSObject
+- (id)initWithParent:(WebCore::SourceBufferPrivateAVFObjC*)parent;
+- (void)invalidate;
+- (void)beginObservingLayer:(AVSampleBufferDisplayLayer *)layer;
+- (void)stopObservingLayer:(AVSampleBufferDisplayLayer *)layer;
+- (void)beginObservingRenderer:(AVSampleBufferAudioRenderer *)renderer;
+- (void)stopObservingRenderer:(AVSampleBufferAudioRenderer *)renderer;
+@end
+
+@implementation WebAVSampleBufferErrorListener {
+    WebCore::SourceBufferPrivateAVFObjC* _parent;
+    Vector<RetainPtr<AVSampleBufferDisplayLayer>> _layers;
+    Vector<RetainPtr<AVSampleBufferAudioRenderer>> _renderers;
+}
+
+- (id)initWithParent:(WebCore::SourceBufferPrivateAVFObjC*)parent
+{
+    if (!(self = [super init]))
+        return nil;
+
+    _parent = parent;
+    return self;
+}
+
+- (void)dealloc
+{
+    [self invalidate];
+    [super dealloc];
+}
+
+- (void)invalidate
+{
+    if (!_parent && !_layers.size() && !_renderers.size())
+        return;
+
+    for (auto& layer : _layers)
+        [layer removeObserver:self forKeyPath:@"error"];
+    _layers.clear();
+
+    for (auto& renderer : _renderers)
+        [renderer removeObserver:self forKeyPath:@"error"];
+    _renderers.clear();
+
+    [[NSNotificationCenter defaultCenter] removeObserver:self];
+
+    _parent = nullptr;
+}
+
+- (void)beginObservingLayer:(AVSampleBufferDisplayLayer*)layer
+{
+    ASSERT(_parent);
+    ASSERT(!_layers.contains(layer));
+
+    _layers.append(layer);
+    [layer addObserver:self forKeyPath:@"error" options:NSKeyValueObservingOptionNew context:nullptr];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(layerFailedToDecode:) name:AVSampleBufferDisplayLayerFailedToDecodeNotification object:layer];
+}
+
+- (void)stopObservingLayer:(AVSampleBufferDisplayLayer*)layer
+{
+    ASSERT(_parent);
+    ASSERT(_layers.contains(layer));
+
+    [layer removeObserver:self forKeyPath:@"error"];
+    _layers.remove(_layers.find(layer));
+
+    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVSampleBufferDisplayLayerFailedToDecodeNotification object:layer];
+}
+
+- (void)beginObservingRenderer:(AVSampleBufferAudioRenderer*)renderer
+{
+    ASSERT(_parent);
+    ASSERT(!_renderers.contains(renderer));
+
+    _renderers.append(renderer);
+    [renderer addObserver:self forKeyPath:@"error" options:NSKeyValueObservingOptionNew context:nullptr];
+}
+
+- (void)stopObservingRenderer:(AVSampleBufferAudioRenderer*)renderer
+{
+    ASSERT(_parent);
+    ASSERT(_renderers.contains(renderer));
+
+    [renderer removeObserver:self forKeyPath:@"error"];
+    _renderers.remove(_renderers.find(renderer));
+}
+
+- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
+{
+    UNUSED_PARAM(context);
+    ASSERT(_parent);
+
+    RetainPtr<WebAVSampleBufferErrorListener> strongSelf = self;
+    if ([object isKindOfClass:getAVSampleBufferDisplayLayerClass()]) {
+        RetainPtr<AVSampleBufferDisplayLayer> layer = (AVSampleBufferDisplayLayer *)object;
+        RetainPtr<NSError> error = [change valueForKey:NSKeyValueChangeNewKey];
+
+        ASSERT(_layers.contains(layer.get()));
+        ASSERT([keyPath isEqualTo:@"error"]);
+
+        callOnMainThread([strongSelf, layer, error] {
+            strongSelf->_parent->layerDidReceiveError(layer.get(), error.get());
+        });
+    } else if ([object isKindOfClass:getAVSampleBufferAudioRendererClass()]) {
+        RetainPtr<AVSampleBufferAudioRenderer> renderer = (AVSampleBufferAudioRenderer *)object;
+        RetainPtr<NSError> error = [change valueForKey:NSKeyValueChangeNewKey];
+
+        ASSERT(_renderers.contains(renderer.get()));
+        ASSERT([keyPath isEqualTo:@"error"]);
+
+        callOnMainThread([strongSelf, renderer, error] {
+            strongSelf->_parent->rendererDidReceiveError(renderer.get(), error.get());
+        });
+    } else
+        ASSERT_NOT_REACHED();
+}
+
+- (void)layerFailedToDecode:(NSNotification*)note
+{
+    RetainPtr<AVSampleBufferDisplayLayer> layer = (AVSampleBufferDisplayLayer *)[note object];
+    ASSERT(_layers.contains(layer.get()));
+
+    RetainPtr<NSError> error = [[note userInfo] valueForKey:AVSampleBufferDisplayLayerFailedToDecodeNotificationErrorKey];
+
+    RetainPtr<WebAVSampleBufferErrorListener> strongSelf = self;
+    callOnMainThread([strongSelf, layer, error] {
+        strongSelf->_parent->layerDidReceiveError(layer.get(), error.get());
+    });
+}
+@end
+
 namespace WebCore {
 
 #pragma mark -
@@ -400,6 +535,7 @@
     : m_weakFactory(this)
     , m_parser(adoptNS([[getAVStreamDataParserClass() alloc] init]))
     , m_delegate(adoptNS([[WebAVStreamDataParserListener alloc] initWithParser:m_parser.get() parent:createWeakPtr()]))
+    , m_errorListener(adoptNS([[WebAVSampleBufferErrorListener alloc] initWithParent:this]))
     , m_mediaSource(parent)
     , m_client(0)
     , m_parsingSucceeded(true)
@@ -659,6 +795,7 @@
             [m_displayLayer requestMediaDataWhenReadyOnQueue:dispatch_get_main_queue() usingBlock:^{
                 didBecomeReadyForMoreSamples(trackID);
             }];
+            [m_errorListener beginObservingLayer:m_displayLayer.get()];
         }
         if (m_mediaSource)
             m_mediaSource->player()->addDisplayLayer(m_displayLayer.get());
@@ -683,6 +820,7 @@
                 didBecomeReadyForMoreSamples(trackID);
             }];
             m_audioRenderers[trackID] = renderer;
+            [m_errorListener beginObservingRenderer:renderer.get()];
         } else
             renderer = m_audioRenderers[trackID].get();
 
@@ -691,6 +829,37 @@
     }
 }
 
+void SourceBufferPrivateAVFObjC::registerForErrorNotifications(SourceBufferPrivateAVFObjCErrorClient* client)
+{
+    ASSERT(!m_errorClients.contains(client));
+    m_errorClients.append(client);
+}
+
+void SourceBufferPrivateAVFObjC::unregisterForErrorNotifications(SourceBufferPrivateAVFObjCErrorClient* client)
+{
+    ASSERT(m_errorClients.contains(client));
+    m_errorClients.remove(m_errorClients.find(client));
+}
+
+void SourceBufferPrivateAVFObjC::layerDidReceiveError(AVSampleBufferDisplayLayer *layer, NSError *error)
+{
+    LOG(Media, "SourceBufferPrivateAVFObjC::layerDidReceiveError(%p): layer(%p), error(%@)", this, layer, [error description]);
+    for (auto& client : m_errorClients)
+        client->layerDidReceiveError(layer, error);
+
+    int errorCode = [[[error userInfo] valueForKey:@"OSStatus"] intValue];
+
+    if (m_client)
+        m_client->sourceBufferPrivateDidReceiveRenderingError(this, errorCode);
+}
+
+void SourceBufferPrivateAVFObjC::rendererDidReceiveError(AVSampleBufferAudioRenderer *renderer, NSError *error)
+{
+    LOG(Media, "SourceBufferPrivateAVFObjC::rendererDidReceiveError(%p): renderer(%p), error(%@)", this, renderer, [error description]);
+    for (auto& client : m_errorClients)
+        client->rendererDidReceiveError(renderer, error);
+}
+
 static RetainPtr<CMSampleBufferRef> createNonDisplayingCopy(CMSampleBufferRef sampleBuffer)
 {
     CMSampleBufferRef newSampleBuffer = 0;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to