[vlc-devel] [PATCH 2/3] VLCOpenGLES2VideoView: use dedicated CFRunLoop mode

Alexandre Janniaux ajanni at videolabs.io
Thu Feb 18 15:23:55 UTC 2021


The vlc_runloop mode is designed to be executed even when an event is
being reported by the vout_window used, ie when there is a call to
CFRunLoopInMode(CFRunLoopGetMain(), CFSTR("vlc_runloop"), ..).

By adding the default mode too, it ensures it would run in the normal
CFRunLoop too.

Async tasks can still be dispatched without the "vlc_runloop" mode but
every sync tasks in the display must be done under this mode to prevent
deadlock from happening between the main thread and the vout_thread.

Fixes #23571
---
 .../apple/VLCOpenGLES2VideoView.m             | 28 +++++++++++++++++--
 1 file changed, 25 insertions(+), 3 deletions(-)

diff --git a/modules/video_output/apple/VLCOpenGLES2VideoView.m b/modules/video_output/apple/VLCOpenGLES2VideoView.m
index 0d617d30b6..40fcfbc333 100644
--- a/modules/video_output/apple/VLCOpenGLES2VideoView.m
+++ b/modules/video_output/apple/VLCOpenGLES2VideoView.m
@@ -466,14 +466,36 @@ static int Open(vlc_gl_t *gl, unsigned width, unsigned height)
     if (wnd->type != VOUT_WINDOW_TYPE_NSOBJECT)
         return VLC_EGENERIC;
 
-   @autoreleasepool {
+    __block vlc_sem_t configured;
+    vlc_sem_init(&configured, 0);
+
+    @autoreleasepool {
+        CFRunLoopRef runloop = CFRunLoopGetMain();
+
+        CFStringRef modes_cfstrings[] = {
+            kCFRunLoopDefaultMode,
+            CFSTR("vlc_runloop"),
+        };
+
+        CFArrayRef modes = CFArrayCreate(NULL, (const void **)modes_cfstrings,
+                                         ARRAY_SIZE(modes_cfstrings),
+                                         &kCFTypeArrayCallBacks);
         /* setup the actual OpenGL ES view */
-        dispatch_sync(dispatch_get_main_queue(), ^{
+
+        /* NOTE: we're using CFRunLoopPerformBlock with the "vlc_runloop" tag
+         * to avoid deadlocks between the window module (main thread) and the
+         * display module, which would happen when using dispatch_sycn here. */
+        CFRunLoopPerformBlock(runloop, modes, ^{
             gl->sys = (__bridge_retained void*)[[VLCOpenGLES2VideoView alloc]
                // TODO better rect
-               initWithFrame:CGRectMake(0.,0.,320.,240.) gl:gl];
+               initWithFrame:CGRectMake(0.,0.,width,height) gl:gl];
+
+            vlc_sem_post(&configured);
         });
+        CFRunLoopWakeUp(runloop);
 
+        vlc_sem_wait(&configured);
+        CFRelease(modes);
         if (gl->sys == NULL)
         {
             msg_Err(gl, "Creating OpenGL ES 2 view failed");
-- 
2.30.1



More information about the vlc-devel mailing list