2007-01-30 17:19:33 +00:00
|
|
|
/* GStreamer
|
|
|
|
* OSX video sink
|
|
|
|
* Copyright (C) 2004-6 Zaheer Abbas Merali <zaheerabbas at merali dot org>
|
2009-02-05 18:19:37 +00:00
|
|
|
* Copyright (C) 2007,2008,2009 Pioneers of the Inevitable <songbird@songbirdnest.com>
|
2007-02-27 12:02:03 +00:00
|
|
|
*
|
2007-01-30 17:19:33 +00:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2012-11-04 00:07:18 +00:00
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
2007-02-27 12:02:03 +00:00
|
|
|
*
|
2007-04-29 13:56:18 +00:00
|
|
|
* The development of this code was made possible due to the involvement of
|
|
|
|
* Pioneers of the Inevitable, the creators of the Songbird Music player.
|
2007-02-27 12:02:03 +00:00
|
|
|
*
|
2007-01-30 17:19:33 +00:00
|
|
|
*/
|
|
|
|
|
2007-05-22 11:03:30 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-osxvideosink
|
|
|
|
*
|
|
|
|
* The OSXVideoSink renders video frames to a MacOSX window. The video output
|
2009-02-05 18:19:37 +00:00
|
|
|
* must be directed to a window embedded in an existing NSApp.
|
2009-01-29 08:10:08 +00:00
|
|
|
*
|
2009-02-05 18:19:37 +00:00
|
|
|
* When the NSView to be embedded is created an element #GstMessage with a
|
|
|
|
* name of 'have-ns-view' will be created and posted on the bus.
|
|
|
|
* The pointer to the NSView to embed will be in the 'nsview' field of that
|
|
|
|
* message. The application MUST handle this message and embed the view
|
|
|
|
* appropriately.
|
2007-05-22 11:03:30 +00:00
|
|
|
*/
|
|
|
|
|
2007-01-30 17:19:33 +00:00
|
|
|
#include "config.h"
|
2012-06-01 04:19:35 +00:00
|
|
|
#include <gst/video/videooverlay.h>
|
|
|
|
#include <gst/video/navigation.h>
|
2007-01-30 17:19:33 +00:00
|
|
|
|
|
|
|
#include "osxvideosink.h"
|
2007-03-14 16:30:19 +00:00
|
|
|
#include <unistd.h>
|
2007-01-30 17:19:33 +00:00
|
|
|
#import "cocoawindow.h"
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY (gst_debug_osx_video_sink);
|
|
|
|
#define GST_CAT_DEFAULT gst_debug_osx_video_sink
|
|
|
|
|
2012-05-22 14:45:28 +00:00
|
|
|
#ifdef RUN_NS_APP_THREAD
|
2013-04-05 08:34:23 +00:00
|
|
|
#include <pthread.h>
|
2012-05-22 14:45:28 +00:00
|
|
|
extern void _CFRunLoopSetCurrent(CFRunLoopRef rl);
|
|
|
|
extern pthread_t _CFMainPThread;
|
|
|
|
#endif
|
|
|
|
|
2013-07-07 19:14:22 +00:00
|
|
|
|
|
|
|
|
2007-01-30 17:19:33 +00:00
|
|
|
static GstStaticPadTemplate gst_osx_video_sink_sink_template_factory =
|
|
|
|
GST_STATIC_PAD_TEMPLATE ("sink",
|
|
|
|
GST_PAD_SINK,
|
|
|
|
GST_PAD_ALWAYS,
|
2012-06-01 04:19:35 +00:00
|
|
|
GST_STATIC_CAPS ("video/x-raw, "
|
2007-01-30 17:19:33 +00:00
|
|
|
"framerate = (fraction) [ 0, MAX ], "
|
|
|
|
"width = (int) [ 1, MAX ], "
|
2010-05-11 08:52:58 +00:00
|
|
|
"height = (int) [ 1, MAX ], "
|
2007-05-17 09:41:48 +00:00
|
|
|
#if G_BYTE_ORDER == G_BIG_ENDIAN
|
2012-06-01 04:19:35 +00:00
|
|
|
"format = (string) YUY2")
|
2007-03-14 16:30:19 +00:00
|
|
|
#else
|
2012-06-01 04:19:35 +00:00
|
|
|
"format = (string) UYVY")
|
2007-03-14 16:30:19 +00:00
|
|
|
#endif
|
2007-01-30 17:19:33 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
ARG_0,
|
2010-05-11 08:52:58 +00:00
|
|
|
ARG_EMBED,
|
2012-05-16 16:52:45 +00:00
|
|
|
ARG_FORCE_PAR,
|
2007-01-30 17:19:33 +00:00
|
|
|
};
|
|
|
|
|
2010-05-11 08:52:58 +00:00
|
|
|
static void gst_osx_video_sink_osxwindow_destroy (GstOSXVideoSink * osxvideosink);
|
2013-07-07 19:14:22 +00:00
|
|
|
static GMutex _run_loop_check_mutex;
|
|
|
|
static GMutex _run_loop_mutex;
|
|
|
|
static GCond _run_loop_cond;
|
|
|
|
static GstOSXVideoSinkClass *sink_class = NULL;
|
2007-01-30 17:19:33 +00:00
|
|
|
static GstVideoSinkClass *parent_class = NULL;
|
|
|
|
|
2012-05-11 16:24:08 +00:00
|
|
|
/* Helper to trigger calls from the main thread */
|
|
|
|
static void
|
2012-05-24 18:43:16 +00:00
|
|
|
gst_osx_video_sink_call_from_main_thread(GstOSXVideoSink *osxvideosink,
|
|
|
|
NSObject * object, SEL function, NSObject *data, BOOL waitUntilDone)
|
2012-05-11 16:24:08 +00:00
|
|
|
{
|
2012-05-24 18:43:16 +00:00
|
|
|
|
2013-07-09 13:33:18 +00:00
|
|
|
NSThread *thread;
|
2012-05-11 16:24:08 +00:00
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
2013-07-09 13:33:18 +00:00
|
|
|
if (sink_class->ns_app_thread == NULL){
|
|
|
|
thread = [NSThread mainThread];
|
|
|
|
} else {
|
|
|
|
thread = sink_class->ns_app_thread;
|
|
|
|
}
|
|
|
|
|
|
|
|
[object performSelector:function onThread:thread
|
2012-05-11 16:24:08 +00:00
|
|
|
withObject:data waitUntilDone:waitUntilDone];
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
2012-05-14 16:01:02 +00:00
|
|
|
/* Poll for cocoa events */
|
|
|
|
static void
|
2012-05-22 14:45:28 +00:00
|
|
|
run_ns_app_loop (void) {
|
2012-05-14 16:01:02 +00:00
|
|
|
NSEvent *event;
|
|
|
|
NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
|
2012-05-22 14:45:28 +00:00
|
|
|
NSDate *pollTime = nil;
|
|
|
|
|
|
|
|
#ifdef RUN_NS_APP_THREAD
|
|
|
|
/* when running the loop in a thread we want to sleep as long as possible */
|
|
|
|
pollTime = [NSDate distantFuture];
|
|
|
|
#else
|
|
|
|
pollTime = [NSDate distantPast];
|
|
|
|
#endif
|
2012-05-14 16:01:02 +00:00
|
|
|
|
|
|
|
do {
|
2012-05-22 14:45:28 +00:00
|
|
|
event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:pollTime
|
|
|
|
inMode:NSDefaultRunLoopMode dequeue:YES];
|
2012-05-14 16:01:02 +00:00
|
|
|
[NSApp sendEvent:event];
|
|
|
|
}
|
|
|
|
while (event != nil);
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
2012-08-01 23:48:29 +00:00
|
|
|
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;
|
2012-08-22 09:21:38 +00:00
|
|
|
gint64 abstime;
|
2012-08-01 23:48:29 +00:00
|
|
|
|
2013-07-07 19:14:22 +00:00
|
|
|
g_mutex_lock (&_run_loop_mutex);
|
2012-08-01 23:48:29 +00:00
|
|
|
[object performSelectorOnMainThread:
|
|
|
|
@selector(checkMainRunLoop)
|
|
|
|
withObject:nil waitUntilDone:NO];
|
|
|
|
/* Wait 100 ms */
|
2012-08-22 09:21:38 +00:00
|
|
|
abstime = g_get_monotonic_time () + 100 * 1000;
|
2013-07-07 19:14:22 +00:00
|
|
|
is_running = g_cond_wait_until (&_run_loop_cond,
|
|
|
|
&_run_loop_mutex, abstime);
|
|
|
|
g_mutex_unlock (&_run_loop_mutex);
|
2012-08-01 23:48:29 +00:00
|
|
|
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
|
|
|
exit:
|
|
|
|
{
|
|
|
|
GST_DEBUG_OBJECT(sink, "The main runloop %s is running",
|
|
|
|
is_running ? "" : " not ");
|
2013-07-07 19:14:22 +00:00
|
|
|
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;
|
|
|
|
}
|
2012-08-01 23:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-14 16:01:02 +00:00
|
|
|
static void
|
2012-05-26 10:21:18 +00:00
|
|
|
gst_osx_video_sink_run_cocoa_loop (GstOSXVideoSink * sink )
|
2012-05-14 16:01:02 +00:00
|
|
|
{
|
2012-05-22 14:45:28 +00:00
|
|
|
/* Cocoa applications require a main runloop running to dispatch UI
|
2012-05-14 16:01:02 +00:00
|
|
|
* events and process deferred calls to the main thread through
|
|
|
|
* perfermSelectorOnMainThread.
|
|
|
|
* Since the sink needs to create it's own Cocoa window when no
|
2012-06-01 04:19:35 +00:00
|
|
|
* external NSView is passed to the sink through the GstVideoOverlay API,
|
2012-05-22 14:45:28 +00:00
|
|
|
* we need to run the cocoa mainloop somehow.
|
2013-07-07 19:14:22 +00:00
|
|
|
* This run loop can only be started once, by the first sink needing it
|
2012-05-14 16:01:02 +00:00
|
|
|
*/
|
2013-07-07 19:14:22 +00:00
|
|
|
|
|
|
|
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) {
|
2012-05-22 14:45:28 +00:00
|
|
|
#ifdef RUN_NS_APP_THREAD
|
2012-05-24 11:08:13 +00:00
|
|
|
/* run the main runloop in a separate thread */
|
2012-05-22 14:45:28 +00:00
|
|
|
|
2012-05-24 11:08:13 +00:00
|
|
|
/* override [NSThread isMainThread] with our own implementation so that we can
|
2013-07-07 19:14:22 +00:00
|
|
|
* make it believe our dedicated thread is the main thread
|
2012-05-24 11:08:13 +00:00
|
|
|
*/
|
|
|
|
Method origIsMainThread = class_getClassMethod([NSThread class],
|
|
|
|
NSSelectorFromString(@"isMainThread"));
|
|
|
|
Method ourIsMainThread = class_getClassMethod([GstOSXVideoSinkObject class],
|
|
|
|
NSSelectorFromString(@"isMainThread"));
|
2012-05-22 14:45:28 +00:00
|
|
|
|
2012-05-24 11:08:13 +00:00
|
|
|
method_exchangeImplementations(origIsMainThread, ourIsMainThread);
|
2012-05-22 14:45:28 +00:00
|
|
|
|
2013-07-07 19:14:22 +00:00
|
|
|
sink_class->ns_app_thread = [[NSThread alloc]
|
2012-05-26 10:21:18 +00:00
|
|
|
initWithTarget:sink->osxvideosinkobject
|
2012-05-24 18:43:16 +00:00
|
|
|
selector:@selector(nsAppThread) object:nil];
|
2013-07-07 19:14:22 +00:00
|
|
|
[sink_class->ns_app_thread start];
|
2012-05-26 10:21:18 +00:00
|
|
|
|
2013-07-07 19:14:22 +00:00
|
|
|
g_mutex_lock (&_run_loop_mutex);
|
|
|
|
g_cond_wait (&_run_loop_cond, &_run_loop_mutex);
|
|
|
|
g_mutex_unlock (&_run_loop_mutex);
|
2012-05-22 14:45:28 +00:00
|
|
|
#else
|
|
|
|
/* assume that there is a GMainLoop and iterate the main runloop from there
|
|
|
|
*/
|
2013-07-07 19:14:22 +00:00
|
|
|
sink_class->cocoa_timeout = g_timeout_add (10,
|
2012-05-24 11:08:13 +00:00
|
|
|
(GSourceFunc) run_ns_app_loop, NULL);
|
2012-05-22 14:45:28 +00:00
|
|
|
#endif
|
2012-05-14 16:01:02 +00:00
|
|
|
}
|
2013-07-07 19:14:22 +00:00
|
|
|
|
|
|
|
g_mutex_unlock (&_run_loop_check_mutex);
|
2012-05-14 16:01:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-05-22 12:49:17 +00:00
|
|
|
gst_osx_video_sink_stop_cocoa_loop (GstOSXVideoSink * osxvideosink)
|
2012-05-14 16:01:02 +00:00
|
|
|
{
|
2012-05-22 14:45:28 +00:00
|
|
|
#ifndef RUN_NS_APP_THREAD
|
2013-07-07 19:14:22 +00:00
|
|
|
if (sink_class->cocoa_timeout)
|
|
|
|
g_source_remove(sink_klass->cocoa_timeout);
|
2012-05-22 14:45:28 +00:00
|
|
|
#endif
|
2012-05-14 16:01:02 +00:00
|
|
|
}
|
|
|
|
|
2007-01-30 17:19:33 +00:00
|
|
|
/* This function handles osx window creation */
|
2010-05-11 08:52:58 +00:00
|
|
|
static gboolean
|
|
|
|
gst_osx_video_sink_osxwindow_create (GstOSXVideoSink * osxvideosink, gint width,
|
2007-01-30 17:19:33 +00:00
|
|
|
gint height)
|
|
|
|
{
|
|
|
|
NSRect rect;
|
|
|
|
GstOSXWindow *osxwindow = NULL;
|
2009-02-05 18:19:37 +00:00
|
|
|
GstStructure *s;
|
|
|
|
GstMessage *msg;
|
2010-05-11 08:52:58 +00:00
|
|
|
gboolean res = TRUE;
|
2009-02-05 18:19:37 +00:00
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
2007-01-30 17:19:33 +00:00
|
|
|
|
2010-05-11 08:52:58 +00:00
|
|
|
g_return_val_if_fail (GST_IS_OSX_VIDEO_SINK (osxvideosink), FALSE);
|
2007-01-30 17:19:33 +00:00
|
|
|
|
2008-11-20 23:59:07 +00:00
|
|
|
GST_DEBUG_OBJECT (osxvideosink, "Creating new OSX window");
|
|
|
|
|
2010-05-11 08:52:58 +00:00
|
|
|
osxvideosink->osxwindow = osxwindow = g_new0 (GstOSXWindow, 1);
|
2007-01-30 17:19:33 +00:00
|
|
|
|
|
|
|
osxwindow->width = width;
|
|
|
|
osxwindow->height = height;
|
2012-05-14 16:01:02 +00:00
|
|
|
osxwindow->closed = FALSE;
|
2013-04-15 14:32:46 +00:00
|
|
|
osxwindow->internal = FALSE;
|
2009-02-05 18:19:37 +00:00
|
|
|
|
|
|
|
/* Allocate our GstGLView for the window, and then tell the application
|
|
|
|
* about it (hopefully it's listening...) */
|
|
|
|
rect.origin.x = 0.0;
|
|
|
|
rect.origin.y = 0.0;
|
|
|
|
rect.size.width = (float) osxwindow->width;
|
|
|
|
rect.size.height = (float) osxwindow->height;
|
|
|
|
osxwindow->gstview =[[GstGLView alloc] initWithFrame:rect];
|
|
|
|
|
|
|
|
s = gst_structure_new ("have-ns-view",
|
2010-05-11 08:52:58 +00:00
|
|
|
"nsview", G_TYPE_POINTER, osxwindow->gstview,
|
|
|
|
nil);
|
2009-02-05 18:19:37 +00:00
|
|
|
|
|
|
|
msg = gst_message_new_element (GST_OBJECT (osxvideosink), s);
|
|
|
|
gst_element_post_message (GST_ELEMENT (osxvideosink), msg);
|
|
|
|
|
2010-05-11 08:52:58 +00:00
|
|
|
GST_INFO_OBJECT (osxvideosink, "'have-ns-view' message sent");
|
|
|
|
|
2012-05-24 11:08:35 +00:00
|
|
|
gst_osx_video_sink_run_cocoa_loop (osxvideosink);
|
2013-07-07 19:14:22 +00:00
|
|
|
[osxwindow->gstview setMainThread:sink_class->ns_app_thread];
|
2012-05-24 18:43:16 +00:00
|
|
|
|
2010-05-11 08:52:58 +00:00
|
|
|
/* check if have-ns-view was handled and osxwindow->gstview was added to a
|
|
|
|
* superview
|
|
|
|
*/
|
|
|
|
if ([osxwindow->gstview haveSuperview] == NO) {
|
|
|
|
/* have-ns-view wasn't handled, post prepare-xwindow-id */
|
|
|
|
if (osxvideosink->superview == NULL) {
|
|
|
|
GST_INFO_OBJECT (osxvideosink, "emitting prepare-xwindow-id");
|
2012-06-01 04:19:35 +00:00
|
|
|
gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (osxvideosink));
|
2010-05-11 08:52:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (osxvideosink->superview != NULL) {
|
|
|
|
/* prepare-xwindow-id was handled, we have the superview in
|
|
|
|
* osxvideosink->superview. We now add osxwindow->gstview to the superview
|
|
|
|
* from the main thread
|
|
|
|
*/
|
|
|
|
GST_INFO_OBJECT (osxvideosink, "we have a superview, adding our view to it");
|
2012-05-24 18:43:16 +00:00
|
|
|
gst_osx_video_sink_call_from_main_thread(osxvideosink, osxwindow->gstview,
|
2012-05-11 16:24:08 +00:00
|
|
|
@selector(addToSuperview:), osxvideosink->superview, NO);
|
|
|
|
|
2010-05-11 08:52:58 +00:00
|
|
|
} else {
|
2012-05-16 19:52:45 +00:00
|
|
|
if (osxvideosink->embed) {
|
|
|
|
/* the view wasn't added to a superview. It's possible that the
|
|
|
|
* application handled have-ns-view, stored our view internally and is
|
|
|
|
* going to add it to a superview later (webkit does that now).
|
|
|
|
*/
|
|
|
|
GST_INFO_OBJECT (osxvideosink, "no superview");
|
|
|
|
} else {
|
2012-05-24 18:43:16 +00:00
|
|
|
gst_osx_video_sink_call_from_main_thread(osxvideosink,
|
|
|
|
osxvideosink->osxvideosinkobject,
|
2012-05-14 16:01:02 +00:00
|
|
|
@selector(createInternalWindow), nil, YES);
|
2012-05-16 19:52:45 +00:00
|
|
|
GST_INFO_OBJECT (osxvideosink, "No superview, creating an internal window.");
|
|
|
|
}
|
2010-05-11 08:52:58 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-14 12:27:58 +00:00
|
|
|
[osxwindow->gstview setNavigation: GST_NAVIGATION(osxvideosink)];
|
2012-05-16 16:52:45 +00:00
|
|
|
[osxvideosink->osxwindow->gstview setKeepAspectRatio: osxvideosink->keep_par];
|
2009-02-05 18:19:37 +00:00
|
|
|
|
|
|
|
[pool release];
|
|
|
|
|
2010-05-11 08:52:58 +00:00
|
|
|
return res;
|
2007-01-30 17:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-02-05 18:19:37 +00:00
|
|
|
gst_osx_video_sink_osxwindow_destroy (GstOSXVideoSink * osxvideosink)
|
2007-01-30 17:19:33 +00:00
|
|
|
{
|
2009-02-10 07:43:59 +00:00
|
|
|
NSAutoreleasePool *pool;
|
|
|
|
|
2007-01-30 17:19:33 +00:00
|
|
|
g_return_if_fail (GST_IS_OSX_VIDEO_SINK (osxvideosink));
|
2009-02-10 07:43:59 +00:00
|
|
|
pool = [[NSAutoreleasePool alloc] init];
|
2007-01-30 17:19:33 +00:00
|
|
|
|
2012-05-24 18:43:16 +00:00
|
|
|
gst_osx_video_sink_call_from_main_thread(osxvideosink,
|
|
|
|
osxvideosink->osxvideosinkobject,
|
|
|
|
@selector(destroy), (id) nil, YES);
|
2012-05-22 12:49:17 +00:00
|
|
|
gst_osx_video_sink_stop_cocoa_loop (osxvideosink);
|
2009-02-05 18:19:37 +00:00
|
|
|
[pool release];
|
2007-01-30 17:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This function resizes a GstXWindow */
|
|
|
|
static void
|
|
|
|
gst_osx_video_sink_osxwindow_resize (GstOSXVideoSink * osxvideosink,
|
|
|
|
GstOSXWindow * osxwindow, guint width, guint height)
|
|
|
|
{
|
2012-05-11 16:24:08 +00:00
|
|
|
GstOSXVideoSinkObject *object = osxvideosink->osxvideosinkobject;
|
|
|
|
|
2009-02-05 18:19:37 +00:00
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
2007-01-30 17:19:33 +00:00
|
|
|
g_return_if_fail (osxwindow != NULL);
|
|
|
|
g_return_if_fail (GST_IS_OSX_VIDEO_SINK (osxvideosink));
|
|
|
|
|
|
|
|
osxwindow->width = width;
|
|
|
|
osxwindow->height = height;
|
|
|
|
|
2008-11-20 23:59:07 +00:00
|
|
|
GST_DEBUG_OBJECT (osxvideosink, "Resizing window to (%d,%d)", width, height);
|
2007-01-30 17:19:33 +00:00
|
|
|
|
2009-02-05 18:19:37 +00:00
|
|
|
/* Directly resize the underlying view */
|
2012-05-11 16:24:08 +00:00
|
|
|
GST_DEBUG_OBJECT (osxvideosink, "Calling setVideoSize on %p", osxwindow->gstview);
|
2012-05-24 18:43:16 +00:00
|
|
|
gst_osx_video_sink_call_from_main_thread(osxvideosink, object,
|
|
|
|
@selector(resize), (id)nil, YES);
|
2007-01-30 17:19:33 +00:00
|
|
|
|
2009-02-05 18:19:37 +00:00
|
|
|
[pool release];
|
2007-01-30 17:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
gst_osx_video_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
|
|
|
|
{
|
|
|
|
GstOSXVideoSink *osxvideosink;
|
|
|
|
GstStructure *structure;
|
|
|
|
gboolean res, result = FALSE;
|
|
|
|
gint video_width, video_height;
|
|
|
|
|
|
|
|
osxvideosink = GST_OSX_VIDEO_SINK (bsink);
|
|
|
|
|
|
|
|
GST_DEBUG_OBJECT (osxvideosink, "caps: %" GST_PTR_FORMAT, caps);
|
|
|
|
|
|
|
|
structure = gst_caps_get_structure (caps, 0);
|
|
|
|
res = gst_structure_get_int (structure, "width", &video_width);
|
|
|
|
res &= gst_structure_get_int (structure, "height", &video_height);
|
|
|
|
|
|
|
|
if (!res) {
|
|
|
|
goto beach;
|
|
|
|
}
|
|
|
|
|
2008-11-20 23:59:07 +00:00
|
|
|
GST_DEBUG_OBJECT (osxvideosink, "our format is: %dx%d video",
|
|
|
|
video_width, video_height);
|
2007-01-30 17:19:33 +00:00
|
|
|
|
|
|
|
GST_VIDEO_SINK_WIDTH (osxvideosink) = video_width;
|
|
|
|
GST_VIDEO_SINK_HEIGHT (osxvideosink) = video_height;
|
|
|
|
|
|
|
|
gst_osx_video_sink_osxwindow_resize (osxvideosink, osxvideosink->osxwindow,
|
|
|
|
video_width, video_height);
|
|
|
|
result = TRUE;
|
|
|
|
|
|
|
|
beach:
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static GstStateChangeReturn
|
|
|
|
gst_osx_video_sink_change_state (GstElement * element,
|
|
|
|
GstStateChange transition)
|
|
|
|
{
|
|
|
|
GstOSXVideoSink *osxvideosink;
|
2009-02-05 18:19:37 +00:00
|
|
|
GstStateChangeReturn ret;
|
2007-01-30 17:19:33 +00:00
|
|
|
|
|
|
|
osxvideosink = GST_OSX_VIDEO_SINK (element);
|
|
|
|
|
2007-05-17 09:41:48 +00:00
|
|
|
GST_DEBUG_OBJECT (osxvideosink, "%s => %s",
|
2010-05-11 08:52:58 +00:00
|
|
|
gst_element_state_get_name(GST_STATE_TRANSITION_CURRENT (transition)),
|
|
|
|
gst_element_state_get_name(GST_STATE_TRANSITION_NEXT (transition)));
|
2007-05-17 09:41:48 +00:00
|
|
|
|
2007-01-30 17:19:33 +00:00
|
|
|
switch (transition) {
|
|
|
|
case GST_STATE_CHANGE_NULL_TO_READY:
|
2009-10-24 01:31:14 +00:00
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:
|
2007-01-30 17:19:33 +00:00
|
|
|
/* Creating our window and our image */
|
2009-02-05 18:19:37 +00:00
|
|
|
GST_VIDEO_SINK_WIDTH (osxvideosink) = 320;
|
|
|
|
GST_VIDEO_SINK_HEIGHT (osxvideosink) = 240;
|
2010-05-11 08:52:58 +00:00
|
|
|
if (!gst_osx_video_sink_osxwindow_create (osxvideosink,
|
2009-02-05 18:19:37 +00:00
|
|
|
GST_VIDEO_SINK_WIDTH (osxvideosink),
|
2010-05-11 08:52:58 +00:00
|
|
|
GST_VIDEO_SINK_HEIGHT (osxvideosink))) {
|
|
|
|
ret = GST_STATE_CHANGE_FAILURE;
|
|
|
|
goto done;
|
|
|
|
}
|
2007-01-30 17:19:33 +00:00
|
|
|
break;
|
2009-02-10 07:43:59 +00:00
|
|
|
default:
|
2007-01-30 17:19:33 +00:00
|
|
|
break;
|
2009-02-05 18:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = (GST_ELEMENT_CLASS (parent_class))->change_state (element, transition);
|
|
|
|
|
|
|
|
switch (transition) {
|
2007-01-30 17:19:33 +00:00
|
|
|
case GST_STATE_CHANGE_PAUSED_TO_READY:
|
|
|
|
GST_VIDEO_SINK_WIDTH (osxvideosink) = 0;
|
|
|
|
GST_VIDEO_SINK_HEIGHT (osxvideosink) = 0;
|
2009-10-24 01:31:14 +00:00
|
|
|
gst_osx_video_sink_osxwindow_destroy (osxvideosink);
|
2007-01-30 17:19:33 +00:00
|
|
|
break;
|
|
|
|
case GST_STATE_CHANGE_READY_TO_NULL:
|
|
|
|
break;
|
2009-02-10 07:43:59 +00:00
|
|
|
default:
|
|
|
|
break;
|
2007-01-30 17:19:33 +00:00
|
|
|
}
|
|
|
|
|
2010-05-11 08:52:58 +00:00
|
|
|
done:
|
2009-02-05 18:19:37 +00:00
|
|
|
return ret;
|
2007-01-30 17:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static GstFlowReturn
|
|
|
|
gst_osx_video_sink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
|
|
|
|
{
|
|
|
|
GstOSXVideoSink *osxvideosink;
|
2012-05-11 16:24:08 +00:00
|
|
|
GstBufferObject* bufferobject;
|
2009-02-05 18:19:37 +00:00
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
2007-01-30 17:19:33 +00:00
|
|
|
|
|
|
|
osxvideosink = GST_OSX_VIDEO_SINK (bsink);
|
|
|
|
|
|
|
|
GST_DEBUG ("show_frame");
|
2012-05-11 16:24:08 +00:00
|
|
|
bufferobject = [[GstBufferObject alloc] initWithBuffer:buf];
|
2012-05-24 18:43:16 +00:00
|
|
|
gst_osx_video_sink_call_from_main_thread(osxvideosink,
|
|
|
|
osxvideosink->osxvideosinkobject,
|
2012-05-11 16:24:08 +00:00
|
|
|
@selector(showFrame:), bufferobject, NO);
|
2009-02-05 18:19:37 +00:00
|
|
|
[pool release];
|
2007-01-30 17:19:33 +00:00
|
|
|
return GST_FLOW_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Buffer management */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* =========================================== */
|
|
|
|
/* */
|
|
|
|
/* Init & Class init */
|
|
|
|
/* */
|
|
|
|
/* =========================================== */
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_osx_video_sink_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstOSXVideoSink *osxvideosink;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_OSX_VIDEO_SINK (object));
|
|
|
|
|
|
|
|
osxvideosink = GST_OSX_VIDEO_SINK (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_EMBED:
|
2012-05-16 19:52:45 +00:00
|
|
|
osxvideosink->embed = g_value_get_boolean(value);
|
2013-07-11 17:45:17 +00:00
|
|
|
g_warning ("The \"embed\" property of osxvideosink is deprecated and "
|
|
|
|
"will be removed in the near future. Use the GstVideoOverlay "
|
|
|
|
"instead.");
|
2007-01-30 17:19:33 +00:00
|
|
|
break;
|
2012-05-16 16:52:45 +00:00
|
|
|
case ARG_FORCE_PAR:
|
|
|
|
osxvideosink->keep_par = g_value_get_boolean(value);
|
|
|
|
if (osxvideosink->osxwindow)
|
|
|
|
[osxvideosink->osxwindow->gstview
|
|
|
|
setKeepAspectRatio: osxvideosink->keep_par];
|
|
|
|
break;
|
2007-01-30 17:19:33 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_osx_video_sink_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
|
|
|
{
|
|
|
|
GstOSXVideoSink *osxvideosink;
|
|
|
|
|
|
|
|
g_return_if_fail (GST_IS_OSX_VIDEO_SINK (object));
|
|
|
|
|
|
|
|
osxvideosink = GST_OSX_VIDEO_SINK (object);
|
|
|
|
|
|
|
|
switch (prop_id) {
|
|
|
|
case ARG_EMBED:
|
2012-05-16 19:52:45 +00:00
|
|
|
g_value_set_boolean (value, osxvideosink->embed);
|
2007-01-30 17:19:33 +00:00
|
|
|
break;
|
2012-05-16 16:52:45 +00:00
|
|
|
case ARG_FORCE_PAR:
|
|
|
|
g_value_set_boolean (value, osxvideosink->keep_par);
|
|
|
|
break;
|
2007-01-30 17:19:33 +00:00
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-11 08:52:58 +00:00
|
|
|
|
2007-01-30 17:19:33 +00:00
|
|
|
static void
|
2012-05-26 10:21:18 +00:00
|
|
|
gst_osx_video_sink_init (GstOSXVideoSink * sink)
|
2007-01-30 17:19:33 +00:00
|
|
|
{
|
2012-05-26 10:21:18 +00:00
|
|
|
sink->osxwindow = NULL;
|
|
|
|
sink->superview = NULL;
|
|
|
|
sink->osxvideosinkobject = [[GstOSXVideoSinkObject alloc] initWithSink:sink];
|
|
|
|
sink->keep_par = FALSE;
|
2007-01-30 17:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_osx_video_sink_base_init (gpointer g_class)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
|
|
|
|
|
2012-04-09 23:51:41 +00:00
|
|
|
gst_element_class_set_static_metadata (element_class, "OSX Video sink",
|
2010-03-18 13:31:35 +00:00
|
|
|
"Sink/Video", "OSX native videosink",
|
|
|
|
"Zaheer Abbas Merali <zaheerabbas at merali dot org>");
|
2007-01-30 17:19:33 +00:00
|
|
|
|
|
|
|
gst_element_class_add_pad_template (element_class,
|
|
|
|
gst_static_pad_template_get (&gst_osx_video_sink_sink_template_factory));
|
|
|
|
}
|
|
|
|
|
2010-05-11 08:52:58 +00:00
|
|
|
static void
|
|
|
|
gst_osx_video_sink_finalize (GObject *object)
|
|
|
|
{
|
|
|
|
GstOSXVideoSink *osxvideosink = GST_OSX_VIDEO_SINK (object);
|
|
|
|
|
|
|
|
if (osxvideosink->superview)
|
|
|
|
[osxvideosink->superview release];
|
|
|
|
|
2012-05-11 16:24:08 +00:00
|
|
|
if (osxvideosink->osxvideosinkobject)
|
|
|
|
[(GstOSXVideoSinkObject*)(osxvideosink->osxvideosinkobject) release];
|
|
|
|
|
2010-05-11 08:52:58 +00:00
|
|
|
G_OBJECT_CLASS (parent_class)->finalize (object);
|
|
|
|
}
|
|
|
|
|
2007-01-30 17:19:33 +00:00
|
|
|
static void
|
|
|
|
gst_osx_video_sink_class_init (GstOSXVideoSinkClass * klass)
|
|
|
|
{
|
|
|
|
GObjectClass *gobject_class;
|
|
|
|
GstElementClass *gstelement_class;
|
|
|
|
GstBaseSinkClass *gstbasesink_class;
|
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gstelement_class = (GstElementClass *) klass;
|
|
|
|
gstbasesink_class = (GstBaseSinkClass *) klass;
|
|
|
|
|
|
|
|
parent_class = g_type_class_ref (GST_TYPE_VIDEO_SINK);
|
2013-07-07 19:14:22 +00:00
|
|
|
sink_class = klass;
|
|
|
|
|
|
|
|
klass->run_loop_state = GST_OSX_VIDEO_SINK_RUN_LOOP_STATE_UNKNOWN;
|
|
|
|
klass->ns_app_thread = NULL;
|
2007-01-30 17:19:33 +00:00
|
|
|
|
|
|
|
gobject_class->set_property = gst_osx_video_sink_set_property;
|
|
|
|
gobject_class->get_property = gst_osx_video_sink_get_property;
|
2010-05-11 08:52:58 +00:00
|
|
|
gobject_class->finalize = gst_osx_video_sink_finalize;
|
2007-01-30 17:19:33 +00:00
|
|
|
|
|
|
|
gstbasesink_class->set_caps = gst_osx_video_sink_setcaps;
|
|
|
|
gstbasesink_class->preroll = gst_osx_video_sink_show_frame;
|
|
|
|
gstbasesink_class->render = gst_osx_video_sink_show_frame;
|
|
|
|
gstelement_class->change_state = gst_osx_video_sink_change_state;
|
|
|
|
|
2007-05-22 11:03:30 +00:00
|
|
|
/**
|
|
|
|
* GstOSXVideoSink:embed
|
|
|
|
*
|
|
|
|
* Set to #TRUE if you are embedding the video window in an application.
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
2007-01-30 17:19:33 +00:00
|
|
|
g_object_class_install_property (gobject_class, ARG_EMBED,
|
2009-02-05 18:19:37 +00:00
|
|
|
g_param_spec_boolean ("embed", "embed", "For ABI compatiblity only, do not use",
|
2010-10-13 14:13:04 +00:00
|
|
|
FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2012-05-16 16:52:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* GstOSXVideoSink:force-aspect-ratio
|
|
|
|
*
|
|
|
|
* When enabled, scaling will respect original aspect ratio.
|
|
|
|
*
|
|
|
|
**/
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class, ARG_FORCE_PAR,
|
|
|
|
g_param_spec_boolean ("force-aspect-ratio", "force aspect ration",
|
|
|
|
"When enabled, scaling will respect original aspect ration",
|
2012-06-29 10:51:30 +00:00
|
|
|
TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
|
2007-01-30 17:19:33 +00:00
|
|
|
}
|
|
|
|
|
2012-05-14 12:27:58 +00:00
|
|
|
static void
|
|
|
|
gst_osx_video_sink_navigation_send_event (GstNavigation * navigation,
|
|
|
|
GstStructure * structure)
|
|
|
|
{
|
|
|
|
GstOSXVideoSink *osxvideosink = GST_OSX_VIDEO_SINK (navigation);
|
|
|
|
GstPad *peer;
|
|
|
|
GstEvent *event;
|
|
|
|
GstVideoRectangle src, dst, result;
|
2012-05-16 19:12:22 +00:00
|
|
|
NSRect bounds;
|
2012-05-14 12:27:58 +00:00
|
|
|
gdouble x, y, xscale = 1.0, yscale = 1.0;
|
|
|
|
|
|
|
|
peer = gst_pad_get_peer (GST_VIDEO_SINK_PAD (osxvideosink));
|
|
|
|
|
|
|
|
if (!peer || !osxvideosink->osxwindow)
|
|
|
|
return;
|
|
|
|
|
|
|
|
event = gst_event_new_navigation (structure);
|
|
|
|
|
2012-05-16 19:12:22 +00:00
|
|
|
bounds = [osxvideosink->osxwindow->gstview getDrawingBounds];
|
|
|
|
|
|
|
|
if (osxvideosink->keep_par) {
|
2012-05-14 12:27:58 +00:00
|
|
|
/* We get the frame position using the calculated geometry from _setcaps
|
|
|
|
that respect pixel aspect ratios */
|
|
|
|
src.w = GST_VIDEO_SINK_WIDTH (osxvideosink);
|
|
|
|
src.h = GST_VIDEO_SINK_HEIGHT (osxvideosink);
|
2012-05-16 19:12:22 +00:00
|
|
|
dst.w = bounds.size.width;
|
|
|
|
dst.h = bounds.size.height;
|
2012-05-14 12:27:58 +00:00
|
|
|
|
|
|
|
gst_video_sink_center_rect (src, dst, &result, TRUE);
|
2012-05-16 19:12:22 +00:00
|
|
|
result.x += bounds.origin.x;
|
|
|
|
result.y += bounds.origin.y;
|
2012-05-14 12:27:58 +00:00
|
|
|
} else {
|
2012-05-16 19:12:22 +00:00
|
|
|
result.x = bounds.origin.x;
|
|
|
|
result.y = bounds.origin.y;
|
|
|
|
result.w = bounds.size.width;
|
|
|
|
result.h = bounds.size.height;
|
2012-05-14 12:27:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We calculate scaling using the original video frames geometry to include
|
|
|
|
pixel aspect ratio scaling. */
|
|
|
|
xscale = (gdouble) osxvideosink->osxwindow->width / result.w;
|
|
|
|
yscale = (gdouble) osxvideosink->osxwindow->height / result.h;
|
|
|
|
|
|
|
|
/* Converting pointer coordinates to the non scaled geometry */
|
|
|
|
if (gst_structure_get_double (structure, "pointer_x", &x)) {
|
|
|
|
x = MIN (x, result.x + result.w);
|
|
|
|
x = MAX (x - result.x, 0);
|
|
|
|
gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
|
|
|
|
(gdouble) x * xscale, NULL);
|
|
|
|
}
|
|
|
|
if (gst_structure_get_double (structure, "pointer_y", &y)) {
|
|
|
|
y = MIN (y, result.y + result.h);
|
|
|
|
y = MAX (y - result.y, 0);
|
|
|
|
gst_structure_set (structure, "pointer_y", G_TYPE_DOUBLE,
|
|
|
|
(gdouble) y * yscale, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_pad_send_event (peer, event);
|
|
|
|
gst_object_unref (peer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_osx_video_sink_navigation_init (GstNavigationInterface * iface)
|
|
|
|
{
|
|
|
|
iface->send_event = gst_osx_video_sink_navigation_send_event;
|
|
|
|
}
|
|
|
|
|
2010-05-11 08:52:58 +00:00
|
|
|
static void
|
2012-06-01 04:19:35 +00:00
|
|
|
gst_osx_video_sink_set_window_handle (GstVideoOverlay * overlay, guintptr handle_id)
|
2010-05-11 08:52:58 +00:00
|
|
|
{
|
|
|
|
GstOSXVideoSink *osxvideosink = GST_OSX_VIDEO_SINK (overlay);
|
2013-07-09 13:34:04 +00:00
|
|
|
NSView *view = (NSView *) handle_id;
|
2013-01-11 15:24:43 +00:00
|
|
|
|
2013-07-09 13:34:04 +00:00
|
|
|
gst_osx_video_sink_call_from_main_thread(osxvideosink,
|
|
|
|
osxvideosink->osxvideosinkobject,
|
|
|
|
@selector(setView:), view, YES);
|
2010-05-11 08:52:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2012-06-01 04:19:35 +00:00
|
|
|
gst_osx_video_sink_xoverlay_init (GstVideoOverlayInterface * iface)
|
2010-05-11 08:52:58 +00:00
|
|
|
{
|
2010-09-21 10:48:34 +00:00
|
|
|
iface->set_window_handle = gst_osx_video_sink_set_window_handle;
|
2010-05-11 08:52:58 +00:00
|
|
|
iface->expose = NULL;
|
|
|
|
iface->handle_events = NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-30 17:19:33 +00:00
|
|
|
/* ============================================================= */
|
|
|
|
/* */
|
|
|
|
/* Public Methods */
|
|
|
|
/* */
|
|
|
|
/* ============================================================= */
|
|
|
|
|
|
|
|
/* =========================================== */
|
|
|
|
/* */
|
|
|
|
/* Object typing & Creation */
|
|
|
|
/* */
|
|
|
|
/* =========================================== */
|
|
|
|
|
|
|
|
GType
|
|
|
|
gst_osx_video_sink_get_type (void)
|
|
|
|
{
|
|
|
|
static GType osxvideosink_type = 0;
|
|
|
|
|
|
|
|
if (!osxvideosink_type) {
|
|
|
|
static const GTypeInfo osxvideosink_info = {
|
|
|
|
sizeof (GstOSXVideoSinkClass),
|
|
|
|
gst_osx_video_sink_base_init,
|
|
|
|
NULL,
|
|
|
|
(GClassInitFunc) gst_osx_video_sink_class_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
sizeof (GstOSXVideoSink),
|
|
|
|
0,
|
|
|
|
(GInstanceInitFunc) gst_osx_video_sink_init,
|
|
|
|
};
|
|
|
|
|
2010-05-11 08:52:58 +00:00
|
|
|
static const GInterfaceInfo overlay_info = {
|
|
|
|
(GInterfaceInitFunc) gst_osx_video_sink_xoverlay_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
|
2012-05-14 12:27:58 +00:00
|
|
|
static const GInterfaceInfo navigation_info = {
|
|
|
|
(GInterfaceInitFunc) gst_osx_video_sink_navigation_init,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
};
|
2007-01-30 17:19:33 +00:00
|
|
|
osxvideosink_type = g_type_register_static (GST_TYPE_VIDEO_SINK,
|
|
|
|
"GstOSXVideoSink", &osxvideosink_info, 0);
|
|
|
|
|
2012-06-01 04:19:35 +00:00
|
|
|
g_type_add_interface_static (osxvideosink_type, GST_TYPE_VIDEO_OVERLAY,
|
2010-05-11 08:52:58 +00:00
|
|
|
&overlay_info);
|
2012-05-14 16:01:02 +00:00
|
|
|
g_type_add_interface_static (osxvideosink_type, GST_TYPE_NAVIGATION,
|
|
|
|
&navigation_info);
|
2007-01-30 17:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return osxvideosink_type;
|
|
|
|
}
|
|
|
|
|
2012-05-14 16:01:02 +00:00
|
|
|
@implementation GstWindowDelegate
|
|
|
|
- (id) initWithSink: (GstOSXVideoSink *) sink
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
self->osxvideosink = sink;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)windowWillClose:(NSNotification *)notification {
|
|
|
|
/* Only handle close events if the window was closed manually by the user
|
|
|
|
* and not becuase of a state change state to READY */
|
2013-07-07 20:16:05 +00:00
|
|
|
if (osxvideosink->osxwindow == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2012-05-14 16:01:02 +00:00
|
|
|
if (!osxvideosink->osxwindow->closed) {
|
|
|
|
osxvideosink->osxwindow->closed = TRUE;
|
|
|
|
GST_ELEMENT_ERROR (osxvideosink, RESOURCE, NOT_FOUND, ("Output window was closed"), (NULL));
|
|
|
|
gst_osx_video_sink_osxwindow_destroy(osxvideosink);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2012-05-11 16:24:08 +00:00
|
|
|
@ implementation GstOSXVideoSinkObject
|
|
|
|
|
|
|
|
-(id) initWithSink: (GstOSXVideoSink*) sink
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
self->osxvideosink = sink;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2012-05-14 16:01:02 +00:00
|
|
|
-(void) createInternalWindow
|
|
|
|
{
|
|
|
|
GstOSXWindow *osxwindow = osxvideosink->osxwindow;
|
|
|
|
ProcessSerialNumber psn;
|
|
|
|
NSRect rect;
|
|
|
|
unsigned int mask;
|
|
|
|
|
|
|
|
osxwindow->internal = TRUE;
|
|
|
|
|
|
|
|
mask = NSTitledWindowMask |
|
|
|
|
NSClosableWindowMask |
|
|
|
|
NSResizableWindowMask |
|
|
|
|
NSTexturedBackgroundWindowMask |
|
|
|
|
NSMiniaturizableWindowMask;
|
|
|
|
|
|
|
|
rect.origin.x = 100.0;
|
|
|
|
rect.origin.y = 100.0;
|
|
|
|
rect.size.width = (float) osxwindow->width;
|
|
|
|
rect.size.height = (float) osxwindow->height;
|
|
|
|
|
2012-05-22 14:45:28 +00:00
|
|
|
#ifndef RUN_NS_APP_THREAD
|
2012-05-14 16:01:02 +00:00
|
|
|
if (!osxvideosink->app_started) {
|
|
|
|
[NSApplication sharedApplication];
|
|
|
|
[NSApp finishLaunching];
|
|
|
|
osxvideosink->app_started = TRUE;
|
|
|
|
}
|
2012-05-22 14:45:28 +00:00
|
|
|
#endif
|
2012-05-14 16:01:02 +00:00
|
|
|
|
|
|
|
if (!GetCurrentProcess(&psn)) {
|
|
|
|
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
|
|
|
|
SetFrontProcess(&psn);
|
|
|
|
}
|
|
|
|
|
2013-07-07 20:16:05 +00:00
|
|
|
osxwindow->win =[[[GstOSXVideoSinkWindow alloc]
|
2012-05-14 16:01:02 +00:00
|
|
|
initWithContentNSRect: rect
|
|
|
|
styleMask: mask
|
|
|
|
backing: NSBackingStoreBuffered
|
|
|
|
defer: NO
|
2013-07-07 20:16:05 +00:00
|
|
|
screen: nil] retain];
|
2012-05-14 16:01:02 +00:00
|
|
|
GST_DEBUG("VideoSinkWindow created, %p", osxwindow->win);
|
|
|
|
[osxwindow->win makeKeyAndOrderFront:NSApp];
|
|
|
|
osxwindow->gstview =[osxwindow->win gstView];
|
|
|
|
[osxwindow->win setDelegate:[[GstWindowDelegate alloc]
|
|
|
|
initWithSink:osxvideosink]];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-05-22 14:45:28 +00:00
|
|
|
#ifdef RUN_NS_APP_THREAD
|
|
|
|
+ (BOOL) isMainThread
|
|
|
|
{
|
|
|
|
/* FIXME: ideally we should return YES only for ->ns_app_thread here */
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-07-09 13:34:04 +00:00
|
|
|
- (void) setView: (NSView*)view
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
if (osxvideosink->superview) {
|
|
|
|
GST_INFO_OBJECT (osxvideosink, "old xwindow id %p", osxvideosink->superview);
|
|
|
|
if (osxvideosink->osxwindow) {
|
|
|
|
[osxvideosink->osxwindow->gstview removeFromSuperview];
|
|
|
|
}
|
|
|
|
[osxvideosink->superview release];
|
|
|
|
}
|
|
|
|
if (osxvideosink->osxwindow != NULL && view != NULL) {
|
|
|
|
if (osxvideosink->osxwindow->internal) {
|
|
|
|
GST_INFO_OBJECT (osxvideosink, "closing internal window");
|
|
|
|
osxvideosink->osxwindow->closed = TRUE;
|
|
|
|
[osxvideosink->osxwindow->win close];
|
|
|
|
[osxvideosink->osxwindow->win release];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (osxvideosink, "set xwindow id %p", view);
|
|
|
|
osxvideosink->superview = [view retain];
|
|
|
|
if (osxvideosink->osxwindow) {
|
|
|
|
[osxvideosink->osxwindow->gstview addToSuperview: osxvideosink->superview];
|
|
|
|
if (view) {
|
|
|
|
osxvideosink->osxwindow->internal = FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
2012-05-11 16:24:08 +00:00
|
|
|
- (void) resize
|
|
|
|
{
|
|
|
|
GstOSXWindow *osxwindow = osxvideosink->osxwindow;
|
|
|
|
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
GST_INFO_OBJECT (osxvideosink, "resizing");
|
2012-12-10 11:44:26 +00:00
|
|
|
NSSize size = {osxwindow->width, osxwindow->height};
|
2013-01-11 15:24:43 +00:00
|
|
|
if (osxwindow->internal) {
|
|
|
|
[osxwindow->win setContentSize:size];
|
|
|
|
}
|
|
|
|
if (osxwindow->gstview) {
|
|
|
|
[osxwindow->gstview setVideoSize :(int)osxwindow->width :(int)osxwindow->height];
|
|
|
|
}
|
2012-05-11 16:24:08 +00:00
|
|
|
GST_INFO_OBJECT (osxvideosink, "done");
|
|
|
|
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) showFrame: (GstBufferObject *) object
|
|
|
|
{
|
2012-06-01 04:19:35 +00:00
|
|
|
GstMapInfo info;
|
2012-05-11 16:24:08 +00:00
|
|
|
guint8 *viewdata;
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
GstBuffer *buf = object->buf;
|
|
|
|
|
2013-04-15 15:37:01 +00:00
|
|
|
if (osxvideosink->osxwindow != NULL)
|
2012-05-11 16:24:08 +00:00
|
|
|
{
|
2012-06-01 04:19:35 +00:00
|
|
|
gst_buffer_map (buf, &info, GST_MAP_READ);
|
2012-05-11 16:24:08 +00:00
|
|
|
viewdata = (guint8 *) [osxvideosink->osxwindow->gstview getTextureBuffer];
|
|
|
|
|
2013-06-05 15:02:49 +00:00
|
|
|
if (G_UNLIKELY (viewdata == NULL)) {
|
|
|
|
GST_ELEMENT_ERROR (osxvideosink, RESOURCE, WRITE,
|
|
|
|
("Could not get a texture buffer"), (NULL));
|
|
|
|
} else {
|
|
|
|
memcpy (viewdata, info.data, info.size);
|
|
|
|
[osxvideosink->osxwindow->gstview displayTexture];
|
|
|
|
gst_buffer_unmap (buf, &info);
|
|
|
|
}
|
2012-05-11 16:24:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[object release];
|
|
|
|
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) destroy
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *pool;
|
2013-04-15 15:37:01 +00:00
|
|
|
GstOSXWindow *osxwindow;
|
2012-05-11 16:24:08 +00:00
|
|
|
|
|
|
|
pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
2013-04-15 15:37:01 +00:00
|
|
|
osxwindow = osxvideosink->osxwindow;
|
|
|
|
osxvideosink->osxwindow = NULL;
|
2012-05-14 16:01:02 +00:00
|
|
|
|
2013-04-15 15:37:01 +00:00
|
|
|
if (osxwindow) {
|
2012-05-11 16:24:08 +00:00
|
|
|
if (osxvideosink->superview) {
|
2013-04-15 15:37:01 +00:00
|
|
|
[osxwindow->gstview removeFromSuperview];
|
2012-05-11 16:24:08 +00:00
|
|
|
}
|
2013-04-15 15:37:01 +00:00
|
|
|
[osxwindow->gstview release];
|
|
|
|
if (osxwindow->internal) {
|
|
|
|
if (!osxwindow->closed) {
|
|
|
|
osxwindow->closed = TRUE;
|
2013-07-07 20:16:05 +00:00
|
|
|
[osxwindow->win close];
|
2013-04-15 15:37:01 +00:00
|
|
|
[osxwindow->win release];
|
2012-05-14 16:01:02 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-15 15:37:01 +00:00
|
|
|
g_free (osxwindow);
|
2012-05-11 16:24:08 +00:00
|
|
|
}
|
|
|
|
[pool release];
|
|
|
|
}
|
2012-05-14 16:01:02 +00:00
|
|
|
|
2012-05-24 18:43:16 +00:00
|
|
|
#ifdef RUN_NS_APP_THREAD
|
|
|
|
-(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];
|
|
|
|
|
2013-07-07 19:14:22 +00:00
|
|
|
g_mutex_lock (&_run_loop_mutex);
|
|
|
|
g_cond_signal (&_run_loop_cond);
|
|
|
|
g_mutex_unlock (&_run_loop_mutex);
|
2012-05-26 10:21:18 +00:00
|
|
|
|
2012-05-24 18:43:16 +00:00
|
|
|
/* run the loop */
|
|
|
|
run_ns_app_loop ();
|
|
|
|
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-08-01 23:48:29 +00:00
|
|
|
-(void) checkMainRunLoop
|
|
|
|
{
|
2013-07-07 19:14:22 +00:00
|
|
|
g_mutex_lock (&_run_loop_mutex);
|
|
|
|
g_cond_signal (&_run_loop_cond);
|
|
|
|
g_mutex_unlock (&_run_loop_mutex);
|
2012-08-01 23:48:29 +00:00
|
|
|
}
|
|
|
|
|
2012-05-11 16:24:08 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@ implementation GstBufferObject
|
|
|
|
-(id) initWithBuffer: (GstBuffer*) buffer
|
|
|
|
{
|
|
|
|
self = [super init];
|
|
|
|
gst_buffer_ref(buffer);
|
|
|
|
self->buf = buffer;
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
-(void) dealloc{
|
|
|
|
gst_buffer_unref(buf);
|
|
|
|
[super dealloc];
|
|
|
|
}
|
|
|
|
@end
|
|
|
|
|
2007-01-30 17:19:33 +00:00
|
|
|
static gboolean
|
|
|
|
plugin_init (GstPlugin * plugin)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (!gst_element_register (plugin, "osxvideosink",
|
|
|
|
GST_RANK_PRIMARY, GST_TYPE_OSX_VIDEO_SINK))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_debug_osx_video_sink, "osxvideosink", 0,
|
|
|
|
"osxvideosink element");
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
|
|
|
|
GST_VERSION_MINOR,
|
2012-04-05 15:36:38 +00:00
|
|
|
osxvideo,
|
2007-01-30 17:19:33 +00:00
|
|
|
"OSX native video output plugin",
|
|
|
|
plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)
|