3 * Copyright (C) 2004-6 Zaheer Abbas Merali <zaheerabbas at merali dot org>
4 * Copyright (C) 2007,2008,2009 Pioneers of the Inevitable <songbird@songbirdnest.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * The development of this code was made possible due to the involvement of
22 * Pioneers of the Inevitable, the creators of the Songbird Music player.
27 * SECTION:element-osxvideosink
29 * The OSXVideoSink renders video frames to a MacOSX window. The video output
30 * must be directed to a window embedded in an existing NSApp.
32 * When the NSView to be embedded is created an element #GstMessage with a
33 * name of 'have-ns-view' will be created and posted on the bus.
34 * The pointer to the NSView to embed will be in the 'nsview' field of that
35 * message. The application MUST handle this message and embed the view
40 #include <gst/interfaces/xoverlay.h>
42 #include "osxvideosink.h"
44 #import "cocoawindow.h"
46 GST_DEBUG_CATEGORY (gst_debug_osx_video_sink);
47 #define GST_CAT_DEFAULT gst_debug_osx_video_sink
49 static GstStaticPadTemplate gst_osx_video_sink_sink_template_factory =
50 GST_STATIC_PAD_TEMPLATE ("sink",
53 GST_STATIC_CAPS ("video/x-raw-yuv, "
54 "framerate = (fraction) [ 0, MAX ], "
55 "width = (int) [ 1, MAX ], "
56 "height = (int) [ 1, MAX ], "
57 #if G_BYTE_ORDER == G_BIG_ENDIAN
58 "format = (fourcc) YUY2")
60 "format = (fourcc) UYVY")
70 static void gst_osx_video_sink_osxwindow_destroy (GstOSXVideoSink * osxvideosink);
72 static GstVideoSinkClass *parent_class = NULL;
74 /* This function handles osx window creation */
76 gst_osx_video_sink_osxwindow_create (GstOSXVideoSink * osxvideosink, gint width,
80 GstOSXWindow *osxwindow = NULL;
84 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
86 g_return_val_if_fail (GST_IS_OSX_VIDEO_SINK (osxvideosink), FALSE);
88 GST_DEBUG_OBJECT (osxvideosink, "Creating new OSX window");
90 osxvideosink->osxwindow = osxwindow = g_new0 (GstOSXWindow, 1);
92 osxwindow->width = width;
93 osxwindow->height = height;
95 /* Allocate our GstGLView for the window, and then tell the application
96 * about it (hopefully it's listening...) */
99 rect.size.width = (float) osxwindow->width;
100 rect.size.height = (float) osxwindow->height;
101 osxwindow->gstview =[[GstGLView alloc] initWithFrame:rect];
103 s = gst_structure_new ("have-ns-view",
104 "nsview", G_TYPE_POINTER, osxwindow->gstview,
107 msg = gst_message_new_element (GST_OBJECT (osxvideosink), s);
108 gst_element_post_message (GST_ELEMENT (osxvideosink), msg);
110 GST_INFO_OBJECT (osxvideosink, "'have-ns-view' message sent");
112 /* check if have-ns-view was handled and osxwindow->gstview was added to a
115 if ([osxwindow->gstview haveSuperview] == NO) {
116 /* have-ns-view wasn't handled, post prepare-xwindow-id */
117 if (osxvideosink->superview == NULL) {
118 GST_INFO_OBJECT (osxvideosink, "emitting prepare-xwindow-id");
119 gst_x_overlay_prepare_xwindow_id (GST_X_OVERLAY (osxvideosink));
122 if (osxvideosink->superview != NULL) {
123 /* prepare-xwindow-id was handled, we have the superview in
124 * osxvideosink->superview. We now add osxwindow->gstview to the superview
125 * from the main thread
127 GST_INFO_OBJECT (osxvideosink, "we have a superview, adding our view to it");
128 [osxwindow->gstview performSelectorOnMainThread:@selector(addToSuperview:)
129 withObject:osxvideosink->superview waitUntilDone:YES];
131 /* the view wasn't added to a superview. It's possible that the
132 * application handled have-ns-view, stored our view internally and is
133 * going to add it to a superview later (webkit does that now).
135 GST_INFO_OBJECT (osxvideosink, "no superview");
145 gst_osx_video_sink_osxwindow_destroy (GstOSXVideoSink * osxvideosink)
147 NSAutoreleasePool *pool;
149 g_return_if_fail (GST_IS_OSX_VIDEO_SINK (osxvideosink));
150 pool = [[NSAutoreleasePool alloc] init];
152 if (osxvideosink->osxwindow) {
153 if (osxvideosink->superview) {
154 [osxvideosink->osxwindow->gstview
155 performSelectorOnMainThread:@selector(removeFromSuperview:)
156 withObject:(id)nil waitUntilDone:YES];
158 [osxvideosink->osxwindow->gstview release];
160 g_free (osxvideosink->osxwindow);
161 osxvideosink->osxwindow = NULL;
166 /* This function resizes a GstXWindow */
168 gst_osx_video_sink_osxwindow_resize (GstOSXVideoSink * osxvideosink,
169 GstOSXWindow * osxwindow, guint width, guint height)
171 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
172 g_return_if_fail (osxwindow != NULL);
173 g_return_if_fail (GST_IS_OSX_VIDEO_SINK (osxvideosink));
175 osxwindow->width = width;
176 osxwindow->height = height;
178 GST_DEBUG_OBJECT (osxvideosink, "Resizing window to (%d,%d)", width, height);
180 /* Directly resize the underlying view */
181 GST_DEBUG_OBJECT (osxvideosink, "Calling setVideoSize on %p", osxwindow->gstview);
182 [osxwindow->gstview setVideoSize:width :height];
188 gst_osx_video_sink_setcaps (GstBaseSink * bsink, GstCaps * caps)
190 GstOSXVideoSink *osxvideosink;
191 GstStructure *structure;
192 gboolean res, result = FALSE;
193 gint video_width, video_height;
195 osxvideosink = GST_OSX_VIDEO_SINK (bsink);
197 GST_DEBUG_OBJECT (osxvideosink, "caps: %" GST_PTR_FORMAT, caps);
199 structure = gst_caps_get_structure (caps, 0);
200 res = gst_structure_get_int (structure, "width", &video_width);
201 res &= gst_structure_get_int (structure, "height", &video_height);
207 GST_DEBUG_OBJECT (osxvideosink, "our format is: %dx%d video",
208 video_width, video_height);
210 GST_VIDEO_SINK_WIDTH (osxvideosink) = video_width;
211 GST_VIDEO_SINK_HEIGHT (osxvideosink) = video_height;
213 gst_osx_video_sink_osxwindow_resize (osxvideosink, osxvideosink->osxwindow,
214 video_width, video_height);
222 static GstStateChangeReturn
223 gst_osx_video_sink_change_state (GstElement * element,
224 GstStateChange transition)
226 GstOSXVideoSink *osxvideosink;
227 GstStateChangeReturn ret;
229 osxvideosink = GST_OSX_VIDEO_SINK (element);
231 GST_DEBUG_OBJECT (osxvideosink, "%s => %s",
232 gst_element_state_get_name(GST_STATE_TRANSITION_CURRENT (transition)),
233 gst_element_state_get_name(GST_STATE_TRANSITION_NEXT (transition)));
235 switch (transition) {
236 case GST_STATE_CHANGE_NULL_TO_READY:
238 case GST_STATE_CHANGE_READY_TO_PAUSED:
239 /* Creating our window and our image */
240 GST_VIDEO_SINK_WIDTH (osxvideosink) = 320;
241 GST_VIDEO_SINK_HEIGHT (osxvideosink) = 240;
242 if (!gst_osx_video_sink_osxwindow_create (osxvideosink,
243 GST_VIDEO_SINK_WIDTH (osxvideosink),
244 GST_VIDEO_SINK_HEIGHT (osxvideosink))) {
245 ret = GST_STATE_CHANGE_FAILURE;
253 ret = (GST_ELEMENT_CLASS (parent_class))->change_state (element, transition);
255 switch (transition) {
256 case GST_STATE_CHANGE_PAUSED_TO_READY:
257 GST_VIDEO_SINK_WIDTH (osxvideosink) = 0;
258 GST_VIDEO_SINK_HEIGHT (osxvideosink) = 0;
259 gst_osx_video_sink_osxwindow_destroy (osxvideosink);
261 case GST_STATE_CHANGE_READY_TO_NULL:
272 gst_osx_video_sink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
274 GstOSXVideoSink *osxvideosink;
276 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
278 osxvideosink = GST_OSX_VIDEO_SINK (bsink);
279 viewdata = (guint8 *) [osxvideosink->osxwindow->gstview getTextureBuffer];
281 GST_DEBUG ("show_frame");
282 memcpy (viewdata, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
283 [osxvideosink->osxwindow->gstview displayTexture];
290 /* Buffer management */
294 /* =========================================== */
296 /* Init & Class init */
298 /* =========================================== */
301 gst_osx_video_sink_set_property (GObject * object, guint prop_id,
302 const GValue * value, GParamSpec * pspec)
304 GstOSXVideoSink *osxvideosink;
306 g_return_if_fail (GST_IS_OSX_VIDEO_SINK (object));
308 osxvideosink = GST_OSX_VIDEO_SINK (object);
312 /* Ignore, just here for backwards compatibility */
315 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
321 gst_osx_video_sink_get_property (GObject * object, guint prop_id,
322 GValue * value, GParamSpec * pspec)
324 GstOSXVideoSink *osxvideosink;
326 g_return_if_fail (GST_IS_OSX_VIDEO_SINK (object));
328 osxvideosink = GST_OSX_VIDEO_SINK (object);
332 g_value_set_boolean (value, TRUE);
335 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
342 gst_osx_video_sink_init (GstOSXVideoSink * osxvideosink)
344 osxvideosink->osxwindow = NULL;
345 osxvideosink->superview = NULL;
349 gst_osx_video_sink_base_init (gpointer g_class)
351 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
353 gst_element_class_set_details_simple (element_class, "OSX Video sink",
354 "Sink/Video", "OSX native videosink",
355 "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
357 gst_element_class_add_static_pad_template (element_class,
358 &gst_osx_video_sink_sink_template_factory);
362 gst_osx_video_sink_finalize (GObject *object)
364 GstOSXVideoSink *osxvideosink = GST_OSX_VIDEO_SINK (object);
366 if (osxvideosink->superview)
367 [osxvideosink->superview release];
369 G_OBJECT_CLASS (parent_class)->finalize (object);
373 gst_osx_video_sink_class_init (GstOSXVideoSinkClass * klass)
375 GObjectClass *gobject_class;
376 GstElementClass *gstelement_class;
377 GstBaseSinkClass *gstbasesink_class;
379 gobject_class = (GObjectClass *) klass;
380 gstelement_class = (GstElementClass *) klass;
381 gstbasesink_class = (GstBaseSinkClass *) klass;
384 parent_class = g_type_class_ref (GST_TYPE_VIDEO_SINK);
386 gobject_class->set_property = gst_osx_video_sink_set_property;
387 gobject_class->get_property = gst_osx_video_sink_get_property;
388 gobject_class->finalize = gst_osx_video_sink_finalize;
390 gstbasesink_class->set_caps = gst_osx_video_sink_setcaps;
391 gstbasesink_class->preroll = gst_osx_video_sink_show_frame;
392 gstbasesink_class->render = gst_osx_video_sink_show_frame;
393 gstelement_class->change_state = gst_osx_video_sink_change_state;
396 * GstOSXVideoSink:embed
398 * Set to #TRUE if you are embedding the video window in an application.
402 g_object_class_install_property (gobject_class, ARG_EMBED,
403 g_param_spec_boolean ("embed", "embed", "For ABI compatiblity only, do not use",
404 FALSE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
408 gst_osx_video_sink_interface_supported (GstImplementsInterface * iface, GType type)
410 g_assert (type == GST_TYPE_X_OVERLAY);
415 gst_osx_video_sink_interface_init (GstImplementsInterfaceClass * klass)
417 klass->supported = gst_osx_video_sink_interface_supported;
421 gst_osx_video_sink_set_window_handle (GstXOverlay * overlay, guintptr handle_id)
423 GstOSXVideoSink *osxvideosink = GST_OSX_VIDEO_SINK (overlay);
424 gulong window_id = (gulong) handle_id;
426 if (osxvideosink->superview) {
427 GST_INFO_OBJECT (osxvideosink, "old xwindow id %p", osxvideosink->superview);
428 if (osxvideosink->osxwindow) {
429 [osxvideosink->osxwindow->gstview
430 performSelectorOnMainThread:@selector(removeFromSuperview:)
431 withObject:(id)nil waitUntilDone:YES];
433 [osxvideosink->superview release];
436 GST_INFO_OBJECT (osxvideosink, "set xwindow id 0x%lx", window_id);
437 osxvideosink->superview = [((NSView *) window_id) retain];
438 if (osxvideosink->osxwindow) {
439 [osxvideosink->osxwindow->gstview performSelectorOnMainThread:@selector(addToSuperview:)
440 withObject:osxvideosink->superview waitUntilDone:YES];
445 gst_osx_video_sink_xoverlay_init (GstXOverlayClass * iface)
447 iface->set_window_handle = gst_osx_video_sink_set_window_handle;
448 iface->expose = NULL;
449 iface->handle_events = NULL;
452 /* ============================================================= */
456 /* ============================================================= */
458 /* =========================================== */
460 /* Object typing & Creation */
462 /* =========================================== */
465 gst_osx_video_sink_get_type (void)
467 static GType osxvideosink_type = 0;
469 if (!osxvideosink_type) {
470 static const GTypeInfo osxvideosink_info = {
471 sizeof (GstOSXVideoSinkClass),
472 gst_osx_video_sink_base_init,
474 (GClassInitFunc) gst_osx_video_sink_class_init,
477 sizeof (GstOSXVideoSink),
479 (GInstanceInitFunc) gst_osx_video_sink_init,
482 static const GInterfaceInfo iface_info = {
483 (GInterfaceInitFunc) gst_osx_video_sink_interface_init,
488 static const GInterfaceInfo overlay_info = {
489 (GInterfaceInitFunc) gst_osx_video_sink_xoverlay_init,
494 osxvideosink_type = g_type_register_static (GST_TYPE_VIDEO_SINK,
495 "GstOSXVideoSink", &osxvideosink_info, 0);
497 g_type_add_interface_static (osxvideosink_type,
498 GST_TYPE_IMPLEMENTS_INTERFACE, &iface_info);
499 g_type_add_interface_static (osxvideosink_type, GST_TYPE_X_OVERLAY,
503 return osxvideosink_type;
507 plugin_init (GstPlugin * plugin)
510 if (!gst_element_register (plugin, "osxvideosink",
511 GST_RANK_PRIMARY, GST_TYPE_OSX_VIDEO_SINK))
514 GST_DEBUG_CATEGORY_INIT (gst_debug_osx_video_sink, "osxvideosink", 0,
515 "osxvideosink element");
520 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
523 "OSX native video output plugin",
524 plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)