AVStreamSession isn't always available, make a HAVE compile flag for it
authoreric.carlson@apple.com <eric.carlson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 28 Jan 2019 19:19:20 +0000 (19:19 +0000)
committereric.carlson@apple.com <eric.carlson@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 28 Jan 2019 19:19:20 +0000 (19:19 +0000)
https://bugs.webkit.org/show_bug.cgi?id=193889
<rdar://problem/47452863>

Reviewed by Jer Noble.

Source/WebCore:

No new tests, no functional change.

* page/Settings.yaml:
* page/SettingsBase.cpp:
(WebCore::SettingsBase::platformDefaultMediaSourceEnabled):
* page/SettingsBase.h:
* page/cocoa/SettingsBaseCocoa.mm:
(WebCore::SettingsBase::platformDefaultMediaSourceEnabled):
* platform/graphics/avfoundation/CDMPrivateMediaSourceAVFObjC.mm:
(WebCore::CDMPrivateMediaSourceAVFObjC::createSession):
* platform/graphics/avfoundation/objc/CDMSessionAVStreamSession.h:
* platform/graphics/avfoundation/objc/CDMSessionAVStreamSession.mm:

* platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.h:
* platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.mm:
(WebCore::MediaPlayerPrivateMediaSourceAVFObjC::durationChanged): Fix logging.
(WebCore::MediaPlayerPrivateMediaSourceAVFObjC::outputObscuredDueToInsufficientExternalProtectionChanged):

* platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h:
* platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm:

Use a HashMap to associate CMSampleBuffer with SourceBufferPrivateAVFObjC.

(-[WebAVSampleBufferErrorListener observeValueForKeyPath:ofObject:change:context:]): isEqualTo
-> isEqualToString.
(WebCore::sourceBufferMap):
(WebCore::nextMapID):
(WebCore::bufferWasConsumedCallback):
(WebCore::SourceBufferPrivateAVFObjC::SourceBufferPrivateAVFObjC):
(WebCore::SourceBufferPrivateAVFObjC::~SourceBufferPrivateAVFObjC):
(WebCore::SourceBufferPrivateAVFObjC::willProvideContentKeyRequestInitializationDataForTrackID):
(WebCore::SourceBufferPrivateAVFObjC::didProvideContentKeyRequestInitializationDataForTrackID):
(WebCore::SourceBufferPrivateAVFObjC::append):
(WebCore::SourceBufferPrivateAVFObjC::destroyParser):
(WebCore::SourceBufferPrivateAVFObjC::layerDidReceiveError):
(WebCore::SourceBufferPrivateAVFObjC::enqueueSample):
(WebCore::SourceBufferPrivateAVFObjC::bufferWasConsumed):
(-[WebBufferConsumedContext initWithParent:]): Deleted.
(-[WebBufferConsumedContext parent]): Deleted.

Source/WTF:

* wtf/FeatureDefines.h:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@240592 268f45cc-cd09-0410-ab3c-d52691b4dbfc

15 files changed:
Source/WTF/ChangeLog
Source/WTF/wtf/FeatureDefines.h
Source/WebCore/ChangeLog
Source/WebCore/PAL/pal/spi/mac/AVFoundationSPI.h
Source/WebCore/page/Settings.yaml
Source/WebCore/page/SettingsBase.cpp
Source/WebCore/page/SettingsBase.h
Source/WebCore/page/cocoa/SettingsBaseCocoa.mm
Source/WebCore/platform/graphics/avfoundation/CDMPrivateMediaSourceAVFObjC.mm
Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionAVStreamSession.h
Source/WebCore/platform/graphics/avfoundation/objc/CDMSessionAVStreamSession.mm
Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.h
Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.mm
Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h
Source/WebCore/platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm

index 8c19465..d168918 100644 (file)
@@ -1,3 +1,13 @@
+2019-01-28  Eric Carlson  <eric.carlson@apple.com>
+
+        AVStreamSession isn't always available, make a HAVE compile flag for it
+        https://bugs.webkit.org/show_bug.cgi?id=193889
+        <rdar://problem/47452863>
+
+        Reviewed by Jer Noble.
+
+        * wtf/FeatureDefines.h:
+
 2018-12-15  Darin Adler  <darin@apple.com>
 
         Replace many uses of String::format with more type-safe alternatives
index 7f17755..7bbf970 100644 (file)
@@ -177,6 +177,14 @@ the public iOS SDK. See <https://webkit.org/b/179167>. */
 #endif
 #endif
 
+#if !defined(HAVE_AVSTREAMSESSION)
+#define HAVE_AVSTREAMSESSION 0
+#endif
+
+#if !defined(ENABLE_MEDIA_SOURCE)
+#define ENABLE_MEDIA_SOURCE 0
+#endif
+
 #endif /* PLATFORM(IOS_FAMILY) */
 
 /* --------- Apple WATCHOS port --------- */
@@ -255,6 +263,14 @@ the public iOS SDK. See <https://webkit.org/b/179167>. */
 #define ENABLE_WEBPROCESS_WINDOWSERVER_BLOCKING ENABLE_WEBPROCESS_NSRUNLOOP
 #endif
 
+#if !defined(HAVE_AVSTREAMSESSION)
+#define HAVE_AVSTREAMSESSION 1
+#endif
+
+#if !defined(ENABLE_MEDIA_SOURCE)
+#define ENABLE_MEDIA_SOURCE 1
+#endif
+
 #endif /* PLATFORM(MAC) */
 
 #if PLATFORM(COCOA)
index 6d76ee4..4b1e07a 100644 (file)
@@ -1,3 +1,51 @@
+2019-01-28  Eric Carlson  <eric.carlson@apple.com>
+
+        AVStreamSession isn't always available, make a HAVE compile flag for it
+        https://bugs.webkit.org/show_bug.cgi?id=193889
+        <rdar://problem/47452863>
+
+        Reviewed by Jer Noble.
+
+        No new tests, no functional change.
+
+        * page/Settings.yaml:
+        * page/SettingsBase.cpp:
+        (WebCore::SettingsBase::platformDefaultMediaSourceEnabled):
+        * page/SettingsBase.h:
+        * page/cocoa/SettingsBaseCocoa.mm:
+        (WebCore::SettingsBase::platformDefaultMediaSourceEnabled):
+        * platform/graphics/avfoundation/CDMPrivateMediaSourceAVFObjC.mm:
+        (WebCore::CDMPrivateMediaSourceAVFObjC::createSession):
+        * platform/graphics/avfoundation/objc/CDMSessionAVStreamSession.h:
+        * platform/graphics/avfoundation/objc/CDMSessionAVStreamSession.mm:
+
+        * platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.h:
+        * platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.mm:
+        (WebCore::MediaPlayerPrivateMediaSourceAVFObjC::durationChanged): Fix logging.
+        (WebCore::MediaPlayerPrivateMediaSourceAVFObjC::outputObscuredDueToInsufficientExternalProtectionChanged):
+
+        * platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.h:
+        * platform/graphics/avfoundation/objc/SourceBufferPrivateAVFObjC.mm:
+        
+        Use a HashMap to associate CMSampleBuffer with SourceBufferPrivateAVFObjC.
+
+        (-[WebAVSampleBufferErrorListener observeValueForKeyPath:ofObject:change:context:]): isEqualTo
+        -> isEqualToString.
+        (WebCore::sourceBufferMap):
+        (WebCore::nextMapID):
+        (WebCore::bufferWasConsumedCallback):
+        (WebCore::SourceBufferPrivateAVFObjC::SourceBufferPrivateAVFObjC):
+        (WebCore::SourceBufferPrivateAVFObjC::~SourceBufferPrivateAVFObjC):
+        (WebCore::SourceBufferPrivateAVFObjC::willProvideContentKeyRequestInitializationDataForTrackID):
+        (WebCore::SourceBufferPrivateAVFObjC::didProvideContentKeyRequestInitializationDataForTrackID):
+        (WebCore::SourceBufferPrivateAVFObjC::append):
+        (WebCore::SourceBufferPrivateAVFObjC::destroyParser):
+        (WebCore::SourceBufferPrivateAVFObjC::layerDidReceiveError):
+        (WebCore::SourceBufferPrivateAVFObjC::enqueueSample):
+        (WebCore::SourceBufferPrivateAVFObjC::bufferWasConsumed):
+        (-[WebBufferConsumedContext initWithParent:]): Deleted.
+        (-[WebBufferConsumedContext parent]): Deleted.
+
 2019-01-28  Rob Buis  <rbuis@igalia.com>
 
         Update MIME type parser
index fb30cc8..ff6bf80 100644 (file)
@@ -40,7 +40,7 @@
 #import <AVKit/AVPlayerViewController_WebKitOnly.h>
 #endif
 
-#if !PLATFORM(IOS_FAMILY)
+#if ENABLE(MEDIA_SOURCE)
 #import <AVFoundation/AVStreamDataParser.h>
 #endif
 
index ca4c57c..4989faa 100644 (file)
@@ -450,7 +450,7 @@ useGiantTiles:
   initial: false
 
 mediaSourceEnabled:
-  initial: true
+  initial: platformDefaultMediaSourceEnabled()
   conditional: MEDIA_SOURCE
 
 sourceBufferChangeTypeEnabled:
index cd4c4e8..c318ece 100644 (file)
@@ -117,6 +117,13 @@ void SettingsBase::initializeDefaultFontFamilies()
 }
 #endif
 
+#if ENABLE(MEDIA_SOURCE) && !PLATFORM(COCOA)
+bool SettingsBase::platformDefaultMediaSourceEnabled()
+{
+    return true;
+}
+#endif
+
 const AtomicString& SettingsBase::standardFontFamily(UScriptCode script) const
 {
     return m_fontGenericFamilies->standardFontFamily(script);
index d6471c2..d74e34a 100644 (file)
@@ -116,6 +116,10 @@ public:
     WEBCORE_EXPORT static float defaultMinimumZoomFontSize();
     WEBCORE_EXPORT static bool defaultDownloadableBinaryFontsEnabled();
 
+#if ENABLE(MEDIA_SOURCE)
+    static bool platformDefaultMediaSourceEnabled();
+#endif
+
     static const unsigned defaultMaximumHTMLParserDOMTreeDepth = 512;
     static const unsigned defaultMaximumRenderTreeDepth = 512;
 
index 1797422..57f825d 100644 (file)
@@ -90,6 +90,11 @@ void SettingsBase::initializeDefaultFontFamilies()
     setSansSerifFontFamily("Helvetica", USCRIPT_COMMON);
 }
 
+bool SettingsBase::platformDefaultMediaSourceEnabled()
+{
+    return true;
+}
+
 #else
 
 void SettingsBase::initializeDefaultFontFamilies()
index a5cbf34..54ec37d 100644 (file)
@@ -124,7 +124,11 @@ std::unique_ptr<LegacyCDMSession> CDMPrivateMediaSourceAVFObjC::createSession(Le
     if (keySystemStringView.substring(14, 1).toInt() == 3 && CDMSessionAVContentKeySession::isAvailable())
         session = std::make_unique<CDMSessionAVContentKeySession>(protocolVersions, *this, client);
     else
+#if HAVE(AVSTREAMSESSION) && ENABLE(LEGACY_ENCRYPTED_MEDIA)
         session = std::make_unique<CDMSessionAVStreamSession>(protocolVersions, *this, client);
+#else
+        return nullptr;
+#endif
 
     m_sessions.append(session.get());
     return WTFMove(session);
index e1de72a..0797ab2 100644 (file)
@@ -31,7 +31,7 @@
 #include <wtf/RetainPtr.h>
 #include <wtf/WeakPtr.h>
 
-#if ENABLE(LEGACY_ENCRYPTED_MEDIA) && ENABLE(MEDIA_SOURCE)
+#if HAVE(AVSTREAMSESSION) && ENABLE(LEGACY_ENCRYPTED_MEDIA) && ENABLE(MEDIA_SOURCE)
 
 OBJC_CLASS AVStreamSession;
 OBJC_CLASS WebCDMSessionAVStreamSessionObserver;
index c3573e5..f3bc9c2 100644 (file)
@@ -26,7 +26,7 @@
 #import "config.h"
 #import "CDMSessionAVStreamSession.h"
 
-#if ENABLE(LEGACY_ENCRYPTED_MEDIA) && ENABLE(MEDIA_SOURCE)
+#if HAVE(AVSTREAMSESSION) && ENABLE(LEGACY_ENCRYPTED_MEDIA) && ENABLE(MEDIA_SOURCE)
 
 #import "CDMPrivateMediaSourceAVFObjC.h"
 #import "LegacyCDM.h"
index 958a218..f846adf 100644 (file)
@@ -109,7 +109,7 @@ public:
     void setTextTrackRepresentation(TextTrackRepresentation*) override;
     void syncTextTrackBounds() override;
     
-#if ENABLE(LEGACY_ENCRYPTED_MEDIA)
+#if HAVE(AVSTREAMSESSION) && ENABLE(LEGACY_ENCRYPTED_MEDIA)
     bool hasStreamSession() { return m_streamSession; }
     AVStreamSession *streamSession();
     void setCDMSession(LegacyCDMSession*) override;
@@ -121,17 +121,17 @@ public:
     void cdmInstanceDetached(CDMInstance&) final;
     void attemptToDecryptWithInstance(CDMInstance&) final;
     bool waitingForKey() const final;
-
     void waitingForKeyChanged();
 #endif
 
-#if ENABLE(LEGACY_ENCRYPTED_MEDIA) || ENABLE(ENCRYPTED_MEDIA)
-    void keyNeeded(Uint8Array*);
-
     void outputObscuredDueToInsufficientExternalProtectionChanged(bool);
     void beginSimulatedHDCPError() override { outputObscuredDueToInsufficientExternalProtectionChanged(true); }
     void endSimulatedHDCPError() override { outputObscuredDueToInsufficientExternalProtectionChanged(false); }
+
+#if ENABLE(LEGACY_ENCRYPTED_MEDIA) || ENABLE(ENCRYPTED_MEDIA)
+    void keyNeeded(Uint8Array*);
 #endif
+
 #if ENABLE(ENCRYPTED_MEDIA)
     void initializationDataEncountered(const String&, RefPtr<ArrayBuffer>&&);
 #endif
index c0374ca..4c18fcb 100644 (file)
@@ -55,6 +55,7 @@
 #pragma mark - Soft Linking
 
 #import <pal/cf/CoreMediaSoftLink.h>
+#import "CoreVideoSoftLink.h"
 
 SOFT_LINK_FRAMEWORK_OPTIONAL(AVFoundation)
 
@@ -829,7 +830,7 @@ void MediaPlayerPrivateMediaSourceAVFObjC::durationChanged()
 
         weakThis->pauseInternal();
         if (now < duration) {
-            LOG(MediaSource, "   ERROR: boundary time observer called before duration!", weakThis.get());
+            LOG(MediaSource, "MediaPlayerPrivateMediaSourceAVFObjC::durationChanged(%p) ERROR: boundary time observer called before duration!", weakThis.get());
             [weakThis->m_synchronizer setRate:0 time:PAL::toCMTime(duration)];
         }
         weakThis->m_player->timeChanged();
@@ -884,7 +885,7 @@ void MediaPlayerPrivateMediaSourceAVFObjC::flushPendingSizeChanges()
     m_sizeChangeObserverWeakPtrFactory.revokeAll();
 }
 
-#if ENABLE(LEGACY_ENCRYPTED_MEDIA)
+#if HAVE(AVSTREAMSESSION) && ENABLE(LEGACY_ENCRYPTED_MEDIA)
 AVStreamSession* MediaPlayerPrivateMediaSourceAVFObjC::streamSession()
 {
     if (!getAVStreamSessionClass() || ![getAVStreamSessionClass() instancesRespondToSelector:@selector(initWithStorageDirectoryAtURL:)])
@@ -918,11 +919,14 @@ void MediaPlayerPrivateMediaSourceAVFObjC::setCDMSession(LegacyCDMSession* sessi
     for (auto& sourceBuffer : m_mediaSourcePrivate->sourceBuffers())
         sourceBuffer->setCDMSession(m_session.get());
 }
+#endif // HAVE(AVSTREAMSESSION) && ENABLE(LEGACY_ENCRYPTED_MEDIA)
 
+#if ENABLE(LEGACY_ENCRYPTED_MEDIA) || ENABLE(ENCRYPTED_MEDIA)
 void MediaPlayerPrivateMediaSourceAVFObjC::keyNeeded(Uint8Array* initData)
 {
     m_player->keyNeeded(initData);
 }
+#endif
 
 void MediaPlayerPrivateMediaSourceAVFObjC::outputObscuredDueToInsufficientExternalProtectionChanged(bool obscured)
 {
@@ -933,7 +937,6 @@ void MediaPlayerPrivateMediaSourceAVFObjC::outputObscuredDueToInsufficientExtern
     UNUSED_PARAM(obscured);
 #endif
 }
-#endif
 
 #if ENABLE(ENCRYPTED_MEDIA)
 void MediaPlayerPrivateMediaSourceAVFObjC::cdmInstanceAttached(CDMInstance& instance)
index d2d8bd5..f9866e0 100644 (file)
@@ -77,7 +77,7 @@ public:
     ALLOW_NEW_API_WITHOUT_GUARDS_END
 };
 
-class SourceBufferPrivateAVFObjC final : public SourceBufferPrivate {
+class SourceBufferPrivateAVFObjC final : public SourceBufferPrivate, public CanMakeWeakPtr<SourceBufferPrivateAVFObjC> {
 public:
     static Ref<SourceBufferPrivateAVFObjC> create(MediaSourcePrivateAVFObjC*);
     virtual ~SourceBufferPrivateAVFObjC();
@@ -195,6 +195,7 @@ private:
     bool m_waitingForKey { true };
     int m_enabledVideoTrackID { -1 };
     int m_protectedTrackID { -1 };
+    uint64_t m_mapID;
 };
 
 }
index 85f257c..3831c7e 100644 (file)
@@ -354,13 +354,13 @@ ALLOW_NEW_API_WITHOUT_GUARDS_END
         RetainPtr<AVSampleBufferDisplayLayer> layer = (AVSampleBufferDisplayLayer *)object;
         ASSERT(_layers.contains(layer.get()));
 
-        if ([keyPath isEqualTo:@"error"]) {
+        if ([keyPath isEqualToString:@"error"]) {
             RetainPtr<NSError> error = [change valueForKey:NSKeyValueChangeNewKey];
             callOnMainThread([parent = _parent, layer = WTFMove(layer), error = WTFMove(error)] {
                 if (parent)
                     parent->layerDidReceiveError(layer.get(), error.get());
             });
-        } else if ([keyPath isEqualTo:@"outputObscuredDueToInsufficientExternalProtection"]) {
+        } else if ([keyPath isEqualToString:@"outputObscuredDueToInsufficientExternalProtection"]) {
             callOnMainThread([parent = _parent, obscured = [[change valueForKey:NSKeyValueChangeNewKey] boolValue]] {
                 if (parent)
                     parent->outputObscuredDueToInsufficientExternalProtectionChanged(obscured);
@@ -375,7 +375,7 @@ ALLOW_NEW_API_WITHOUT_GUARDS_END
         RetainPtr<NSError> error = [change valueForKey:NSKeyValueChangeNewKey];
 
         ASSERT(_renderers.contains(renderer.get()));
-        ASSERT([keyPath isEqualTo:@"error"]);
+        ASSERT([keyPath isEqualToString:@"error"]);
 
         callOnMainThread([parent = _parent, renderer = WTFMove(renderer), error = WTFMove(error)] {
             if (parent)
@@ -398,30 +398,6 @@ ALLOW_NEW_API_WITHOUT_GUARDS_END
 }
 @end
 
-#pragma mark -
-
-@interface WebBufferConsumedContext : NSObject {
-    WeakPtr<WebCore::SourceBufferPrivateAVFObjC> _parent;
-}
-@property (readonly) WebCore::SourceBufferPrivateAVFObjC* parent;
-@end
-
-@implementation WebBufferConsumedContext
-- (id)initWithParent:(WeakPtr<WebCore::SourceBufferPrivateAVFObjC>)parent
-{
-    self = [super init];
-    if (self)
-        _parent = parent;
-    return self;
-}
-
-@dynamic parent;
-- (WebCore::SourceBufferPrivateAVFObjC*)parent
-{
-    return _parent.get();
-}
-@end
-
 namespace WebCore {
 using namespace PAL;
 
@@ -461,26 +437,39 @@ protected:
 #pragma mark -
 #pragma mark SourceBufferPrivateAVFObjC
 
-static NSString *kBufferConsumedContext = @"BufferConsumedContext";
+static HashMap<uint64_t, WeakPtr<SourceBufferPrivateAVFObjC>>& sourceBufferMap()
+{
+    static NeverDestroyed<HashMap<uint64_t, WeakPtr<SourceBufferPrivateAVFObjC>>> map;
+    return map;
+}
 
-static void bufferWasConsumedCallback(CMNotificationCenterRef, const void*, CFStringRef notificationName, const void*, CFTypeRef payload)
+static uint64_t nextMapID()
 {
+    static uint64_t mapID = 0;
+    return ++mapID;
+}
+
+static void bufferWasConsumedCallback(CMNotificationCenterRef, const void* listener, CFStringRef notificationName, const void*, CFTypeRef)
+{
+    LOG(MediaSource, "bufferWasConsumedCallback - nofication %s", [(__bridge NSString *)notificationName UTF8String]);
+
+    if (!CFEqual(kCMSampleBufferConsumerNotification_BufferConsumed, notificationName))
+        return;
+
     if (!isMainThread()) {
-        callOnMainThread([notificationName, payload = retainPtr(payload)] {
-            bufferWasConsumedCallback(nullptr, nullptr, notificationName, nullptr, payload.get());
+        callOnMainThread([notificationName, listener] {
+            bufferWasConsumedCallback(nullptr, listener, notificationName, nullptr, nullptr);
         });
         return;
     }
 
-    if (!CFEqual(kCMSampleBufferConsumerNotification_BufferConsumed, notificationName))
-        return;
-
-    ASSERT(CFGetTypeID(payload) == CFDictionaryGetTypeID());
-    WebBufferConsumedContext *context = [(__bridge NSDictionary *)payload valueForKey:kBufferConsumedContext];
-    if (!context)
+    uint64_t mapID = reinterpret_cast<uint64_t>(listener);
+    if (!mapID) {
+        LOG(MediaSource, "bufferWasConsumedCallback - ERROR: didn't find ID %llu in map", mapID);
         return;
+    }
 
-    if (auto sourceBuffer = context.parent)
+    if (auto sourceBuffer = sourceBufferMap().get(mapID).get())
         sourceBuffer->bufferWasConsumed();
 }
 
@@ -495,14 +484,18 @@ SourceBufferPrivateAVFObjC::SourceBufferPrivateAVFObjC(MediaSourcePrivateAVFObjC
     , m_errorListener(adoptNS([[WebAVSampleBufferErrorListener alloc] initWithParent:createWeakPtr()]))
     , m_isAppendingGroup(adoptOSObject(dispatch_group_create()))
     , m_mediaSource(parent)
+    , m_mapID(nextMapID())
 {
-    CMNotificationCenterAddListener(CMNotificationCenterGetDefaultLocalCenter(), this, bufferWasConsumedCallback, kCMSampleBufferConsumerNotification_BufferConsumed, nullptr, 0);
+    CMNotificationCenterAddListener(CMNotificationCenterGetDefaultLocalCenter(), reinterpret_cast<void*>(m_mapID), bufferWasConsumedCallback, kCMSampleBufferConsumerNotification_BufferConsumed, nullptr, 0);
     m_delegate.get().abortSemaphore = Box<Semaphore>::create(0);
+
+    sourceBufferMap().add(m_mapID, makeWeakPtr(*this));
 }
 
 SourceBufferPrivateAVFObjC::~SourceBufferPrivateAVFObjC()
 {
     ASSERT(!m_client);
+    sourceBufferMap().remove(m_mapID);
     destroyParser();
     destroyRenderers();
 
@@ -628,7 +621,7 @@ void SourceBufferPrivateAVFObjC::willProvideContentKeyRequestInitializationDataF
 
     ASSERT(m_parser);
 
-#if ENABLE(LEGACY_ENCRYPTED_MEDIA)
+#if HAVE(AVSTREAMSESSION) && ENABLE(LEGACY_ENCRYPTED_MEDIA)
     LOG(MediaSource, "SourceBufferPrivateAVFObjC::willProvideContentKeyRequestInitializationDataForTrackID(%p) - track:%d", this, trackID);
     m_protectedTrackID = trackID;
 
@@ -649,7 +642,7 @@ void SourceBufferPrivateAVFObjC::didProvideContentKeyRequestInitializationDataFo
     if (!m_mediaSource)
         return;
 
-#if ENABLE(LEGACY_ENCRYPTED_MEDIA)
+#if HAVE(AVSTREAMSESSION) && ENABLE(LEGACY_ENCRYPTED_MEDIA)
     LOG(MediaSource, "SourceBufferPrivateAVFObjC::didProvideContentKeyRequestInitializationDataForTrackID(%p) - track:%d", this, trackID);
     m_protectedTrackID = trackID;
     auto initDataArray = Uint8Array::create([initData length]);
@@ -666,7 +659,6 @@ void SourceBufferPrivateAVFObjC::didProvideContentKeyRequestInitializationDataFo
 #endif
 
 #if ENABLE(ENCRYPTED_MEDIA) && HAVE(AVCONTENTKEYSESSION)
-    //
     auto initDataBuffer = SharedBuffer::create(initData);
     auto keyIDs = CDMPrivateFairPlayStreaming::extractKeyIDsSinf(initDataBuffer);
     if (!keyIDs)
@@ -713,7 +705,7 @@ static dispatch_queue_t globalDataParserQueue()
 
 void SourceBufferPrivateAVFObjC::append(Vector<unsigned char>&& data)
 {
-    LOG(MediaSource, "SourceBufferPrivateAVFObjC::append(%p) - data:%p, length:%d", this, data.data(), data.size());
+    LOG(MediaSource, "SourceBufferPrivateAVFObjC::append(%p) - data:%p, length:%d", this, data.data(), (int)data.size());
 
     // FIXME: Avoid the data copy by wrapping around the Vector<> object.
     RetainPtr<NSData> nsData = adoptNS([[NSData alloc] initWithBytes:data.data() length:data.size()]);
@@ -770,7 +762,7 @@ void SourceBufferPrivateAVFObjC::resetParserState()
 
 void SourceBufferPrivateAVFObjC::destroyParser()
 {
-#if ENABLE(LEGACY_ENCRYPTED_MEDIA)
+#if HAVE(AVSTREAMSESSION) && ENABLE(LEGACY_ENCRYPTED_MEDIA)
     if (m_mediaSource && m_mediaSource->player()->hasStreamSession())
         [m_mediaSource->player()->streamSession() removeStreamDataParser:m_parser.get()];
 #endif
@@ -988,7 +980,7 @@ void SourceBufferPrivateAVFObjC::unregisterForErrorNotifications(SourceBufferPri
 
 void SourceBufferPrivateAVFObjC::layerDidReceiveError(AVSampleBufferDisplayLayer *layer, NSError *error)
 {
-    LOG(MediaSource, "SourceBufferPrivateAVFObjC::layerDidReceiveError(%p): layer(%p), error(%@)", this, layer, [error description]);
+    LOG(MediaSource, "SourceBufferPrivateAVFObjC::layerDidReceiveError(%p): layer(%p), error(%s)", this, layer, [[error description] UTF8String]);
 
     // FIXME(142246): Remove the following once <rdar://problem/20027434> is resolved.
     bool anyIgnored = false;
@@ -1025,7 +1017,7 @@ ALLOW_NEW_API_WITHOUT_GUARDS_BEGIN
 void SourceBufferPrivateAVFObjC::rendererDidReceiveError(AVSampleBufferAudioRenderer *renderer, NSError *error)
 ALLOW_NEW_API_WITHOUT_GUARDS_END
 {
-    LOG(MediaSource, "SourceBufferPrivateAVFObjC::rendererDidReceiveError(%p): renderer(%p), error(%@)", this, renderer, [error description]);
+    LOG(MediaSource, "SourceBufferPrivateAVFObjC::rendererDidReceiveError(%p): renderer(%p), error(%s)", this, renderer, [[error description] UTF8String]);
 
     if ([error code] == 'HDCP')
         m_hdcpError = error;
@@ -1098,7 +1090,7 @@ void SourceBufferPrivateAVFObjC::enqueueSample(Ref<MediaSample>&& sample, const
         CMFormatDescriptionRef formatDescription = CMSampleBufferGetFormatDescription(platformSample.sample.cmSampleBuffer);
         FloatSize formatSize = FloatSize(CMVideoFormatDescriptionGetPresentationDimensions(formatDescription, true, true));
         if (!m_cachedSize || formatSize != m_cachedSize.value()) {
-            LOG(MediaSource, "SourceBufferPrivateAVFObjC::enqueueSample(%p) - size change detected: {width=%lf, height=%lf}", formatSize.width(), formatSize.height());
+            LOG(MediaSource, "SourceBufferPrivateAVFObjC::enqueueSample(%p) - size change detected: {width=%lf, height=%lf}", this, formatSize.width(), formatSize.height());
             bool sizeWasNull = !m_cachedSize;
             m_cachedSize = formatSize;
             if (m_mediaSource) {
@@ -1114,11 +1106,10 @@ void SourceBufferPrivateAVFObjC::enqueueSample(Ref<MediaSample>&& sample, const
 
         if (m_displayLayer) {
             if (m_mediaSource && !m_mediaSource->player()->hasAvailableVideoFrame() && !sample->isNonDisplaying()) {
-                auto context = adoptNS([[WebBufferConsumedContext alloc] initWithParent:createWeakPtr()]);
                 CMSampleBufferRef rawSampleCopy;
                 CMSampleBufferCreateCopy(kCFAllocatorDefault, platformSample.sample.cmSampleBuffer, &rawSampleCopy);
                 auto sampleCopy = adoptCF(rawSampleCopy);
-                CMSetAttachment(sampleCopy.get(), kCMSampleBufferAttachmentKey_PostNotificationWhenConsumed, (__bridge CFDictionaryRef)@{kBufferConsumedContext: context.get()}, kCMAttachmentMode_ShouldNotPropagate);
+                CMSetAttachment(sampleCopy.get(), kCMSampleBufferAttachmentKey_PostNotificationWhenConsumed, (__bridge CFDictionaryRef)@{ (__bridge NSString *)kCMSampleBufferAttachmentKey_PostNotificationWhenConsumed : @(YES) }, kCMAttachmentMode_ShouldNotPropagate);
                 [m_displayLayer enqueueSampleBuffer:sampleCopy.get()];
             } else
                 [m_displayLayer enqueueSampleBuffer:platformSample.sample.cmSampleBuffer];
@@ -1133,6 +1124,8 @@ void SourceBufferPrivateAVFObjC::enqueueSample(Ref<MediaSample>&& sample, const
 
 void SourceBufferPrivateAVFObjC::bufferWasConsumed()
 {
+    LOG(MediaSource, "SourceBufferPrivateAVFObjC::bufferWasConsumed(%p)", this);
+
     if (m_mediaSource)
         m_mediaSource->player()->setHasAvailableVideoFrame(true);
 }