Title: [247309] trunk/Source/WebCore
Revision
247309
Author
[email protected]
Date
2019-07-10 10:34:19 -0700 (Wed, 10 Jul 2019)

Log Message

Fix unsafe usage of makeWeakPtr() in CMTimebaseEffectiveRateChangedCallback()
https://bugs.webkit.org/show_bug.cgi?id=199656

Reviewed by Eric Carlson.

CMTimebaseEffectiveRateChangedCallback() is getting called on a background thread and would call
makeWeakPtr() on the MediaPlayerPrivateMediaSourceAVFObjC object, which is not safe because
MediaPlayerPrivateMediaSourceAVFObjC is a main thread object.

To address the issue, move the logic for listening to effective rate changes to its own
Listener class which is ThreadSafeRefCounted. Instead of using makeWeakPtr() on the background
thread, we now merely ref the thread-safe listener.

* platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.h:
* platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.mm:
(WebCore::EffectiveRateChangedListener::create):
(WebCore::EffectiveRateChangedListener::effectiveRateChanged):
(WebCore::CMTimebaseEffectiveRateChangedCallback):
(WebCore::EffectiveRateChangedListener::stop):
(WebCore::EffectiveRateChangedListener::EffectiveRateChangedListener):
(WebCore::MediaPlayerPrivateMediaSourceAVFObjC::MediaPlayerPrivateMediaSourceAVFObjC):
(WebCore::MediaPlayerPrivateMediaSourceAVFObjC::~MediaPlayerPrivateMediaSourceAVFObjC):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (247308 => 247309)


--- trunk/Source/WebCore/ChangeLog	2019-07-10 17:17:16 UTC (rev 247308)
+++ trunk/Source/WebCore/ChangeLog	2019-07-10 17:34:19 UTC (rev 247309)
@@ -1,3 +1,28 @@
+2019-07-10  Chris Dumez  <[email protected]>
+
+        Fix unsafe usage of makeWeakPtr() in CMTimebaseEffectiveRateChangedCallback()
+        https://bugs.webkit.org/show_bug.cgi?id=199656
+
+        Reviewed by Eric Carlson.
+
+        CMTimebaseEffectiveRateChangedCallback() is getting called on a background thread and would call
+        makeWeakPtr() on the MediaPlayerPrivateMediaSourceAVFObjC object, which is not safe because
+        MediaPlayerPrivateMediaSourceAVFObjC is a main thread object.
+
+        To address the issue, move the logic for listening to effective rate changes to its own
+        Listener class which is ThreadSafeRefCounted. Instead of using makeWeakPtr() on the background
+        thread, we now merely ref the thread-safe listener.
+
+        * platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.h:
+        * platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.mm:
+        (WebCore::EffectiveRateChangedListener::create):
+        (WebCore::EffectiveRateChangedListener::effectiveRateChanged):
+        (WebCore::CMTimebaseEffectiveRateChangedCallback):
+        (WebCore::EffectiveRateChangedListener::stop):
+        (WebCore::EffectiveRateChangedListener::EffectiveRateChangedListener):
+        (WebCore::MediaPlayerPrivateMediaSourceAVFObjC::MediaPlayerPrivateMediaSourceAVFObjC):
+        (WebCore::MediaPlayerPrivateMediaSourceAVFObjC::~MediaPlayerPrivateMediaSourceAVFObjC):
+
 2019-07-10  Antti Koivisto  <[email protected]>
 
         Remove TouchActionData

Modified: trunk/Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.h (247308 => 247309)


--- trunk/Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.h	2019-07-10 17:17:16 UTC (rev 247308)
+++ trunk/Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.h	2019-07-10 17:34:19 UTC (rev 247309)
@@ -49,6 +49,7 @@
 namespace WebCore {
 
 class CDMSessionMediaSourceAVFObjC;
+class EffectiveRateChangedListener;
 class MediaSourcePrivateAVFObjC;
 class PixelBufferConformerCV;
 class PlatformClockCM;
@@ -329,6 +330,8 @@
 #endif
     std::unique_ptr<VideoFullscreenLayerManagerObjC> m_videoFullscreenLayerManager;
 
+    Ref<EffectiveRateChangedListener> m_effectiveRateChangedListener;
+
 #if !RELEASE_LOG_DISABLED
     Ref<const Logger> m_logger;
     const void* m_logIdentifier;

Modified: trunk/Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.mm (247308 => 247309)


--- trunk/Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.mm	2019-07-10 17:17:16 UTC (rev 247308)
+++ trunk/Source/WebCore/platform/graphics/avfoundation/objc/MediaPlayerPrivateMediaSourceAVFObjC.mm	2019-07-10 17:34:19 UTC (rev 247309)
@@ -85,16 +85,48 @@
 #pragma mark -
 #pragma mark MediaPlayerPrivateMediaSourceAVFObjC
 
+class EffectiveRateChangedListener : public ThreadSafeRefCounted<EffectiveRateChangedListener> {
+public:
+    static Ref<EffectiveRateChangedListener> create(MediaPlayerPrivateMediaSourceAVFObjC& client, CMTimebaseRef timebase)
+    {
+        return adoptRef(*new EffectiveRateChangedListener(client, timebase));
+    }
+
+    void effectiveRateChanged()
+    {
+        callOnMainThread([this, protectedThis = makeRef(*this)] {
+            if (m_client)
+                m_client->effectiveRateChanged();
+        });
+    }
+
+    void stop(CMTimebaseRef);
+
+private:
+    EffectiveRateChangedListener(MediaPlayerPrivateMediaSourceAVFObjC&, CMTimebaseRef);
+
+    WeakPtr<MediaPlayerPrivateMediaSourceAVFObjC> m_client;
+};
+
 static void CMTimebaseEffectiveRateChangedCallback(CMNotificationCenterRef, const void *listener, CFStringRef, const void *, CFTypeRef)
 {
-    MediaPlayerPrivateMediaSourceAVFObjC* player = (MediaPlayerPrivateMediaSourceAVFObjC*)const_cast<void*>(listener);
-    callOnMainThread([weakThis = makeWeakPtr(player)] {
-        if (!weakThis)
-            return;
-        weakThis->effectiveRateChanged();
-    });
+    auto* effectiveRateChangedListener = (EffectiveRateChangedListener*)const_cast<void*>(listener);
+    effectiveRateChangedListener->effectiveRateChanged();
 }
 
+void EffectiveRateChangedListener::stop(CMTimebaseRef timebase)
+{
+    CMNotificationCenterRef nc = CMNotificationCenterGetDefaultLocalCenter();
+    CMNotificationCenterRemoveListener(nc, this, CMTimebaseEffectiveRateChangedCallback, kCMTimebaseNotification_EffectiveRateChanged, timebase);
+}
+
+EffectiveRateChangedListener::EffectiveRateChangedListener(MediaPlayerPrivateMediaSourceAVFObjC& client, CMTimebaseRef timebase)
+    : m_client(makeWeakPtr(client))
+{
+    CMNotificationCenterRef nc = CMNotificationCenterGetDefaultLocalCenter();
+    CMNotificationCenterAddListener(nc, this, CMTimebaseEffectiveRateChangedCallback, kCMTimebaseNotification_EffectiveRateChanged, timebase, 0);
+}
+
 MediaPlayerPrivateMediaSourceAVFObjC::MediaPlayerPrivateMediaSourceAVFObjC(MediaPlayer* player)
     : m_player(player)
     , m_synchronizer(adoptNS([PAL::allocAVSampleBufferRenderSynchronizerInstance() init]))
@@ -106,15 +138,12 @@
     , m_seeking(false)
     , m_loadingProgressed(false)
     , m_videoFullscreenLayerManager(std::make_unique<VideoFullscreenLayerManagerObjC>())
+    , m_effectiveRateChangedListener(EffectiveRateChangedListener::create(*this, [m_synchronizer timebase]))
 #if !RELEASE_LOG_DISABLED
     , m_logger(player->mediaPlayerLogger())
     , m_logIdentifier(player->mediaPlayerLogIdentifier())
 #endif
 {
-    CMTimebaseRef timebase = [m_synchronizer timebase];
-    CMNotificationCenterRef nc = CMNotificationCenterGetDefaultLocalCenter();
-    CMNotificationCenterAddListener(nc, this, CMTimebaseEffectiveRateChangedCallback, kCMTimebaseNotification_EffectiveRateChanged, timebase, 0);
-
     auto logSiteIdentifier = LOGIDENTIFIER;
     ALWAYS_LOG(logSiteIdentifier);
     UNUSED_PARAM(logSiteIdentifier);
@@ -150,9 +179,7 @@
 {
     ALWAYS_LOG(LOGIDENTIFIER);
 
-    CMTimebaseRef timebase = [m_synchronizer timebase];
-    CMNotificationCenterRef nc = CMNotificationCenterGetDefaultLocalCenter();
-    CMNotificationCenterRemoveListener(nc, this, CMTimebaseEffectiveRateChangedCallback, kCMTimebaseNotification_EffectiveRateChanged, timebase);
+    m_effectiveRateChangedListener->stop([m_synchronizer timebase]);
 
     if (m_timeJumpedObserver)
         [m_synchronizer removeTimeObserver:m_timeJumpedObserver.get()];
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to