macos: Remove old NSApp workaround related code

This is no longer needed since the introduction of `gst_macos_main()` in 1.22.
Before that existed, we had a patch for GLib in Cerbero, which did work but made it
impossible to update GLib at all. The code being removed was a fail-safe in case of
running without said patch being applied. It's no longer needed, since for macOS
we just wrap our GStreamer with an NSApplication using `gst_macos_main()`.

Warnings will be displayed if no NSApp/NSRunLoop is found wherever needed,
pointing the user towards using the new API.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/4366>
This commit is contained in:
Piotr Brzeziński 2023-05-10 22:35:27 +02:00
parent 0c4a702e82
commit f60c87769f
6 changed files with 15 additions and 531 deletions

View file

@ -36,148 +36,7 @@ G_DEFINE_TYPE (GstVulkanDisplayCocoa, gst_vulkan_display_cocoa,
static void gst_vulkan_display_cocoa_finalize (GObject * object);
static gpointer gst_vulkan_display_cocoa_get_handle (GstVulkanDisplay * display);
/* Define this if the GLib patch from
* https://bugzilla.gnome.org/show_bug.cgi?id=741450
* is used
*/
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
static GstVulkanDisplayCocoa *singleton = NULL;
static gint nsapp_source_id = 0;
static GMutex nsapp_lock;
static GCond nsapp_cond;
static gboolean
gst_vulkan_display_cocoa_nsapp_iteration (gpointer data)
{
NSEvent *event = nil;
if (![NSThread isMainThread]) {
GST_WARNING ("NSApp iteration not running in the main thread");
return FALSE;
}
while ((event = ([NSApp nextEventMatchingMask:NSEventMaskAny
untilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]
inMode:NSDefaultRunLoopMode dequeue:YES])) != nil) {
[NSApp sendEvent:event];
}
return TRUE;
}
static void
gst_vulkan_display_cocoa_open_and_attach_source (gpointer data)
{
if ([NSThread isMainThread]) {
/* The sharedApplication class method initializes
* the display environment and connects your program
* to the window server and the display server.
* It has to be done in the main thread.
*/
[NSApplication sharedApplication];
GST_DEBUG ("Custom NSApp initialization done");
nsapp_source_id = g_timeout_add (60, gst_vulkan_display_cocoa_nsapp_iteration,
NULL);
GST_DEBUG ("NSApp iteration loop attached, id %d", nsapp_source_id);
}
}
static gboolean
gst_vulkan_display_cocoa_init_nsapp (gpointer data)
{
g_mutex_lock (&nsapp_lock);
gst_vulkan_display_cocoa_open_and_attach_source (data);
g_cond_signal (&nsapp_cond);
g_mutex_unlock (&nsapp_lock);
return FALSE;
}
static GstVulkanDisplayCocoa *
gst_vulkan_display_cocoa_setup_nsapp (gpointer data)
{
GMainContext *context = g_main_context_default ();
gint delta_ms = 0;
g_mutex_lock (&nsapp_lock);
if (singleton) {
GST_DEBUG ("Get existing display");
singleton = gst_object_ref (singleton);
g_mutex_unlock (&nsapp_lock);
return singleton;
}
if (NSApp != nil && !singleton) {
GstVulkanDisplayCocoa *ret = g_object_new (GST_TYPE_VULKAN_DISPLAY_COCOA, NULL);
gst_object_ref_sink (ret);
g_mutex_unlock (&nsapp_lock);
return ret;
}
/* All application have to start with [NSApplication sharedApplication]
* so if NSApp is nil here let's assume this is a debugging application
* that runs a glib main loop. */
g_assert (NSApp == nil);
GST_DEBUG ("The application has not initialized NSApp");
if ([NSThread isMainThread]) {
GST_DEBUG ("Setting up NSApp from the main thread");
if (g_main_context_is_owner (context)) {
GST_DEBUG ("The main thread own the context");
gst_vulkan_display_cocoa_open_and_attach_source (data);
} else if (g_main_context_acquire (context)) {
GST_DEBUG ("The main loop should be shortly running in the main thread");
gst_vulkan_display_cocoa_open_and_attach_source (data);
g_main_context_release (context);
} else {
GST_WARNING ("Main loop running in another thread");
}
} else {
GST_DEBUG ("Setting up NSApp not from the main thread");
if (g_main_context_is_owner (context)) {
GST_WARNING ("Default context not own by the main thread");
delta_ms = -1;
} else if (g_main_context_acquire (context)) {
GST_DEBUG ("The main loop should be shortly running in the main thread");
delta_ms = 1000;
g_main_context_release (context);
} else {
GST_DEBUG ("Main loop running in main thread");
delta_ms = 500;
}
if (delta_ms > 0) {
gint64 end_time = g_get_monotonic_time () + delta_ms * 1000;;
g_idle_add_full (G_PRIORITY_HIGH, gst_vulkan_display_cocoa_init_nsapp, data, NULL);
g_cond_wait_until (&nsapp_cond, &nsapp_lock, end_time);
}
}
if (NSApp == nil) {
GST_ERROR ("Custom NSApp initialization failed");
} else {
GST_DEBUG ("Create display");
singleton = g_object_new (GST_TYPE_VULKAN_DISPLAY_COCOA, NULL);
gst_object_ref_sink (singleton);
}
g_mutex_unlock (&nsapp_lock);
return singleton;
}
#endif
static void
gst_vulkan_display_cocoa_class_init (GstVulkanDisplayCocoaClass * klass)
{
@ -198,21 +57,6 @@ gst_vulkan_display_cocoa_init (GstVulkanDisplayCocoa * display_cocoa)
static void
gst_vulkan_display_cocoa_finalize (GObject * object)
{
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
g_mutex_lock (&nsapp_lock);
if (singleton) {
GST_DEBUG ("Destroy display");
singleton = NULL;
if (nsapp_source_id) {
GST_DEBUG ("Remove NSApp loop iteration, id %d", nsapp_source_id);
g_source_remove (nsapp_source_id);
}
nsapp_source_id = 0;
g_mutex_unlock (&nsapp_lock);
}
g_mutex_unlock (&nsapp_lock);
#endif
G_OBJECT_CLASS (gst_vulkan_display_cocoa_parent_class)->finalize (object);
}
@ -230,12 +74,13 @@ gst_vulkan_display_cocoa_new (void)
GST_DEBUG_CATEGORY_GET (gst_vulkan_display_debug, "vulkandisplay");
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
ret = gst_vulkan_display_cocoa_setup_nsapp (NULL);
#else
if (NSApp == nil)
g_warning ("An NSApplication needs to be running on the main thread "
"to ensure correct behaviour on macOS. Use gst_macos_main() or call "
"[NSApplication sharedApplication] in your code before using this element.");
ret = g_object_new (GST_TYPE_VULKAN_DISPLAY_COCOA, NULL);
gst_object_ref_sink (ret);
#endif
return ret;
}

View file

@ -35,164 +35,17 @@ GST_DEBUG_CATEGORY_STATIC (gst_gl_display_debug);
G_DEFINE_TYPE (GstGLDisplayCocoa, gst_gl_display_cocoa, GST_TYPE_GL_DISPLAY);
static void gst_gl_display_cocoa_finalize (GObject * object);
static guintptr gst_gl_display_cocoa_get_handle (GstGLDisplay * display);
#if MAC_OS_X_VERSION_MAX_ALLOWED < 101200
#define NSEventMaskAny NSAnyEventMask
#endif
/* Define this if the GLib patch from
* https://bugzilla.gnome.org/show_bug.cgi?id=741450
* is used
*/
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
static GstGLDisplayCocoa *singleton = NULL;
static gint nsapp_source_id = 0;
static GMutex nsapp_lock;
static GCond nsapp_cond;
static gboolean
gst_gl_display_cocoa_nsapp_iteration (gpointer data)
{
NSEvent *event = nil;
if (![NSThread isMainThread]) {
GST_WARNING ("NSApp iteration not running in the main thread");
return FALSE;
}
while ((event = ([NSApp nextEventMatchingMask:NSEventMaskAny
untilDate:[NSDate dateWithTimeIntervalSinceNow:0.05]
inMode:NSDefaultRunLoopMode dequeue:YES])) != nil) {
[NSApp sendEvent:event];
}
return TRUE;
}
static void
gst_gl_display_cocoa_open_and_attach_source (gpointer data)
{
if ([NSThread isMainThread]) {
/* The sharedApplication class method initializes
* the display environment and connects your program
* to the window server and the display server.
* It has to be done in the main thread.
*/
[NSApplication sharedApplication];
GST_DEBUG ("Custom NSApp initialization done");
nsapp_source_id = g_timeout_add (60, gst_gl_display_cocoa_nsapp_iteration,
NULL);
GST_DEBUG ("NSApp iteration loop attached, id %d", nsapp_source_id);
}
}
static gboolean
gst_gl_display_cocoa_init_nsapp (gpointer data)
{
g_mutex_lock (&nsapp_lock);
gst_gl_display_cocoa_open_and_attach_source (data);
g_cond_signal (&nsapp_cond);
g_mutex_unlock (&nsapp_lock);
return FALSE;
}
static GstGLDisplayCocoa *
gst_gl_display_cocoa_setup_nsapp (gpointer data)
{
GMainContext *context = g_main_context_default ();
gint delta_ms = 0;
g_mutex_lock (&nsapp_lock);
if (singleton) {
GST_DEBUG ("Get existing display");
singleton = gst_object_ref (singleton);
g_mutex_unlock (&nsapp_lock);
return singleton;
}
if (NSApp != nil && !singleton) {
GstGLDisplayCocoa *ret = g_object_new (GST_TYPE_GL_DISPLAY_COCOA, NULL);
gst_object_ref_sink (ret);
g_mutex_unlock (&nsapp_lock);
return ret;
}
/* All application have to start with [NSApplication sharedApplication]
* so if NSApp is nil here let's assume this is a debugging application
* that runs a glib main loop. */
g_assert (NSApp == nil);
GST_DEBUG ("The application has not initialized NSApp");
if ([NSThread isMainThread]) {
GST_DEBUG ("Setting up NSApp from the main thread");
if (g_main_context_is_owner (context)) {
GST_DEBUG ("The main thread own the context");
gst_gl_display_cocoa_open_and_attach_source (data);
} else if (g_main_context_acquire (context)) {
GST_DEBUG ("The main loop should be shortly running in the main thread");
gst_gl_display_cocoa_open_and_attach_source (data);
g_main_context_release (context);
} else {
GST_WARNING ("Main loop running in another thread");
}
} else {
GST_DEBUG ("Setting up NSApp not from the main thread");
if (g_main_context_is_owner (context)) {
GST_WARNING ("Default context not own by the main thread");
delta_ms = -1;
} else if (g_main_context_acquire (context)) {
GST_DEBUG ("The main loop should be shortly running in the main thread");
delta_ms = 1000;
g_main_context_release (context);
} else {
GST_DEBUG ("Main loop running in main thread");
delta_ms = 500;
}
if (delta_ms > 0) {
gint64 end_time = g_get_monotonic_time () + delta_ms * 1000;;
g_idle_add_full (G_PRIORITY_HIGH, gst_gl_display_cocoa_init_nsapp, data, NULL);
g_cond_wait_until (&nsapp_cond, &nsapp_lock, end_time);
}
}
if (NSApp == nil) {
GST_ERROR ("Custom NSApp initialization failed");
} else {
GST_DEBUG ("Create display");
singleton = g_object_new (GST_TYPE_GL_DISPLAY_COCOA, NULL);
gst_object_ref_sink (singleton);
}
g_mutex_unlock (&nsapp_lock);
return singleton;
}
#endif
static void
gst_gl_display_cocoa_class_init (GstGLDisplayCocoaClass * klass)
{
GST_GL_DISPLAY_CLASS (klass)->get_handle =
GST_DEBUG_FUNCPTR (gst_gl_display_cocoa_get_handle);
G_OBJECT_CLASS (klass)->finalize = gst_gl_display_cocoa_finalize;
}
static void
@ -202,27 +55,6 @@ gst_gl_display_cocoa_init (GstGLDisplayCocoa * display_cocoa)
display->type = GST_GL_DISPLAY_TYPE_COCOA;
}
static void
gst_gl_display_cocoa_finalize (GObject * object)
{
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
g_mutex_lock (&nsapp_lock);
if (singleton) {
GST_DEBUG ("Destroy display");
singleton = NULL;
if (nsapp_source_id) {
GST_DEBUG ("Remove NSApp loop iteration, id %d", nsapp_source_id);
g_source_remove (nsapp_source_id);
}
nsapp_source_id = 0;
g_mutex_unlock (&nsapp_lock);
}
g_mutex_unlock (&nsapp_lock);
#endif
G_OBJECT_CLASS (gst_gl_display_cocoa_parent_class)->finalize (object);
}
/**
* gst_gl_display_cocoa_new:
*
@ -237,12 +69,13 @@ gst_gl_display_cocoa_new (void)
GST_DEBUG_CATEGORY_GET (gst_gl_display_debug, "gldisplay");
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
display = gst_gl_display_cocoa_setup_nsapp (NULL);
#else
if (NSApp == nil)
g_warning ("An NSApplication needs to be running on the main thread "
"to ensure correct behaviour on macOS. Use gst_macos_main() or call "
"[NSApplication sharedApplication] in your code before using this element.");
display = g_object_new (GST_TYPE_GL_DISPLAY_COCOA, NULL);
gst_object_ref_sink (display);
#endif
return display;
}

View file

@ -69,9 +69,6 @@ struct _GstOSXImage;
- (void) addToSuperview: (NSView *)superview;
- (void) removeFromSuperview: (id)unused;
- (void) setNavigation: (GstNavigation *) nav;
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
- (void) setMainThread: (NSThread *) thread;
#endif
@end

View file

@ -667,12 +667,6 @@ const gchar* gst_keycode_to_keyname(gint16 keycode)
[self reshape];
}
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
- (void) setMainThread: (NSThread *) thread {
mainThread = thread;
}
#endif
- (void) haveSuperviewReal:(NSMutableArray *)closure {
BOOL haveSuperview = [self superview] != nil;
[closure addObject:[NSNumber numberWithBool:haveSuperview]];

View file

@ -128,11 +128,6 @@ GType gst_osx_video_sink_get_type(void);
-(void) destroy;
-(void) showFrame: (GstBufferObject*) buf;
-(void) setView: (NSView*) view;
+ (BOOL) isMainThread;
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
-(void) nsAppThread;
-(void) checkMainRunLoop;
#endif
@end
G_END_DECLS

View file

@ -43,12 +43,6 @@
GST_DEBUG_CATEGORY (gst_debug_osx_video_sink);
#define GST_CAT_DEFAULT gst_debug_osx_video_sink
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
#include <pthread.h>
extern void _CFRunLoopSetCurrent (CFRunLoopRef rl);
extern pthread_t _CFMainPThread;
#endif
static GstStaticPadTemplate gst_osx_video_sink_sink_template_factory =
GST_STATIC_PAD_TEMPLATE ("sink",
GST_PAD_SINK,
@ -73,12 +67,6 @@ enum
static void gst_osx_video_sink_osxwindow_destroy (GstOSXVideoSink * osxvideosink);
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
static GMutex _run_loop_check_mutex;
static GMutex _run_loop_mutex;
static GCond _run_loop_cond;
#endif
static GstOSXVideoSinkClass *sink_class = NULL;
static GstVideoSinkClass *parent_class = NULL;
@ -111,126 +99,6 @@ gst_osx_video_sink_call_from_main_thread(GstOSXVideoSink *osxvideosink,
[pool release];
}
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
/* Poll for cocoa events */
static void
run_ns_app_loop (void) {
NSEvent *event;
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
NSDate *pollTime = nil;
/* when running the loop in a thread we want to sleep as long as possible */
pollTime = [NSDate distantFuture];
do {
event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:pollTime
inMode:NSDefaultRunLoopMode dequeue:YES];
[NSApp sendEvent:event];
}
while (event != nil);
[pool release];
}
static void
gst_osx_videosink_check_main_run_loop (GstOSXVideoSink *sink)
{
/* check if the main run loop is running */
gboolean is_running;
/* the easy way */
is_running = [[NSRunLoop mainRunLoop] currentMode] != nil;
if (is_running) {
goto exit;
} else {
/* the previous check doesn't always work with main loops that run
* cocoa's main run loop manually, like the gdk one, giving false
* negatives. This check defers a call to the main thread and waits to
* be awaken by this function. */
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
GstOSXVideoSinkObject * object = (GstOSXVideoSinkObject *) sink->osxvideosinkobject;
gint64 abstime;
g_mutex_lock (&_run_loop_mutex);
[object performSelectorOnMainThread:
@selector(checkMainRunLoop)
withObject:nil waitUntilDone:NO];
/* Wait 100 ms */
abstime = g_get_monotonic_time () + 100 * 1000;
is_running = g_cond_wait_until (&_run_loop_cond,
&_run_loop_mutex, abstime);
g_mutex_unlock (&_run_loop_mutex);
[pool release];
}
exit:
{
GST_DEBUG_OBJECT(sink, "The main runloop %s is running",
is_running ? "" : " not ");
if (is_running) {
sink_class->run_loop_state = GST_OSX_VIDEO_SINK_RUN_LOOP_STATE_RUNNING;
sink_class->ns_app_thread = [NSThread mainThread];
} else {
sink_class->run_loop_state = GST_OSX_VIDEO_SINK_RUN_LOOP_STATE_NOT_RUNNING;
}
}
}
static void
gst_osx_video_sink_run_cocoa_loop (GstOSXVideoSink * sink )
{
/* Cocoa applications require a main runloop running to dispatch UI
* events and process deferred calls to the main thread through
* perfermSelectorOnMainThread.
* Since the sink needs to create it's own Cocoa window when no
* external NSView is passed to the sink through the GstVideoOverlay API,
* we need to run the cocoa mainloop somehow.
* This run loop can only be started once, by the first sink needing it
*/
g_mutex_lock (&_run_loop_check_mutex);
if (sink_class->run_loop_state == GST_OSX_VIDEO_SINK_RUN_LOOP_STATE_UNKNOWN) {
gst_osx_videosink_check_main_run_loop (sink);
}
if (sink_class->run_loop_state == GST_OSX_VIDEO_SINK_RUN_LOOP_STATE_RUNNING) {
g_mutex_unlock (&_run_loop_check_mutex);
return;
}
if (sink_class->ns_app_thread == NULL) {
/* run the main runloop in a separate thread */
/* override [NSThread isMainThread] with our own implementation so that we can
* make it believe our dedicated thread is the main thread
*/
Method origIsMainThread = class_getClassMethod([NSThread class],
NSSelectorFromString(@"isMainThread"));
Method ourIsMainThread = class_getClassMethod([GstOSXVideoSinkObject class],
NSSelectorFromString(@"isMainThread"));
method_exchangeImplementations(origIsMainThread, ourIsMainThread);
sink_class->ns_app_thread = [[NSThread alloc]
initWithTarget:sink->osxvideosinkobject
selector:@selector(nsAppThread) object:nil];
[sink_class->ns_app_thread start];
g_mutex_lock (&_run_loop_mutex);
g_cond_wait (&_run_loop_cond, &_run_loop_mutex);
g_mutex_unlock (&_run_loop_mutex);
}
g_mutex_unlock (&_run_loop_check_mutex);
}
static void
gst_osx_video_sink_stop_cocoa_loop (GstOSXVideoSink * osxvideosink)
{
}
#endif
/* This function handles osx window creation */
static gboolean
gst_osx_video_sink_osxwindow_create (GstOSXVideoSink * osxvideosink, gint width,
@ -260,11 +128,6 @@ gst_osx_video_sink_osxwindow_create (GstOSXVideoSink * osxvideosink, gint width,
rect.size.height = (float) osxwindow->height;
osxwindow->gstview =[[GstGLView alloc] initWithFrame:rect];
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
gst_osx_video_sink_run_cocoa_loop (osxvideosink);
[osxwindow->gstview setMainThread:sink_class->ns_app_thread];
#endif
if (osxvideosink->superview == NULL) {
GST_INFO_OBJECT (osxvideosink, "emitting prepare-xwindow-id");
gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (osxvideosink));
@ -304,9 +167,6 @@ gst_osx_video_sink_osxwindow_destroy (GstOSXVideoSink * osxvideosink)
gst_osx_video_sink_call_from_main_thread(osxvideosink,
osxvideosink->osxvideosinkobject,
@selector(destroy), (id) nil, YES);
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
gst_osx_video_sink_stop_cocoa_loop (osxvideosink);
#endif
[pool release];
}
@ -512,6 +372,11 @@ gst_osx_video_sink_propose_allocation (GstBaseSink * base_sink, GstQuery * query
static void
gst_osx_video_sink_init (GstOSXVideoSink * sink)
{
if ([[NSRunLoop mainRunLoop] currentMode] == nil)
g_warning ("An NSRunLoop needs to be running on the main thread "
"to ensure correct behaviour on macOS. Use gst_macos_main() or call "
"[NSApplication sharedApplication] in your code before using this element.");
sink->osxwindow = NULL;
sink->superview = NULL;
sink->osxvideosinkobject = [[GstOSXVideoSinkObject alloc] initWithSink:sink];
@ -794,12 +659,6 @@ gst_osx_video_sink_get_type (void)
}
+ (BOOL) isMainThread
{
/* FIXME: ideally we should return YES only for ->ns_app_thread here */
return YES;
}
- (void) setView: (NSView*)view
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
@ -931,49 +790,10 @@ no_texture_buffer:
g_free (osxwindow);
}
GST_OBJECT_UNLOCK (osxvideosink);
[pool release];
}
#ifndef GSTREAMER_GLIB_COCOA_NSAPPLICATION
-(void) nsAppThread
{
NSAutoreleasePool *pool;
/* set the main runloop as the runloop for the current thread. This has the
* effect that calling NSApp nextEventMatchingMask:untilDate:inMode:dequeue
* runs the main runloop.
*/
_CFRunLoopSetCurrent(CFRunLoopGetMain());
/* this is needed to make IsMainThread checks in core foundation work from the
* current thread
*/
_CFMainPThread = pthread_self();
pool = [[NSAutoreleasePool alloc] init];
[NSApplication sharedApplication];
[NSApp finishLaunching];
g_mutex_lock (&_run_loop_mutex);
g_cond_signal (&_run_loop_cond);
g_mutex_unlock (&_run_loop_mutex);
/* run the loop */
run_ns_app_loop ();
[pool release];
}
-(void) checkMainRunLoop
{
g_mutex_lock (&_run_loop_mutex);
g_cond_signal (&_run_loop_cond);
g_mutex_unlock (&_run_loop_mutex);
}
#endif
@end
@ implementation GstBufferObject