osxvideosink.m 15.3 KB
Newer Older
1 2 3
/* GStreamer
 * OSX video sink
 * 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>
5
 * 
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
20
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
21 22
 * The development of this code was made possible due to the involvement of
 * Pioneers of the Inevitable, the creators of the Songbird Music player.
23
 * 
24 25
 */

26 27 28 29
/**
 * SECTION:element-osxvideosink
 *
 * The OSXVideoSink renders video frames to a MacOSX window. The video output
30
 * must be directed to a window embedded in an existing NSApp.
31
 *
32 33 34 35 36
 * 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.
37 38
 */

39
#include "config.h"
40
#include <gst/interfaces/xoverlay.h>
41 42

#include "osxvideosink.h"
43
#include <unistd.h>
44 45 46 47 48 49 50 51 52 53 54 55
#import "cocoawindow.h"

GST_DEBUG_CATEGORY (gst_debug_osx_video_sink);
#define GST_CAT_DEFAULT gst_debug_osx_video_sink

static GstStaticPadTemplate gst_osx_video_sink_sink_template_factory =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-raw-yuv, "
        "framerate = (fraction) [ 0, MAX ], "
        "width = (int) [ 1, MAX ], "
56
        "height = (int) [ 1, MAX ], "
57
#if G_BYTE_ORDER == G_BIG_ENDIAN
58 59
       "format = (fourcc) YUY2")
#else
60
        "format = (fourcc) UYVY")
61
#endif
62 63 64 65 66
    );

enum
{
  ARG_0,
67
  ARG_EMBED,
68 69
};

70 71
static void gst_osx_video_sink_osxwindow_destroy (GstOSXVideoSink * osxvideosink);

72 73 74
static GstVideoSinkClass *parent_class = NULL;

/* This function handles osx window creation */
75 76
static gboolean
gst_osx_video_sink_osxwindow_create (GstOSXVideoSink * osxvideosink, gint width,
77 78 79 80
    gint height)
{
  NSRect rect;
  GstOSXWindow *osxwindow = NULL;
81 82
  GstStructure *s;
  GstMessage *msg;
83
  gboolean res = TRUE;
84
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
85

86
  g_return_val_if_fail (GST_IS_OSX_VIDEO_SINK (osxvideosink), FALSE);
87

88 89
  GST_DEBUG_OBJECT (osxvideosink, "Creating new OSX window");

90
  osxvideosink->osxwindow = osxwindow = g_new0 (GstOSXWindow, 1);
91 92 93

  osxwindow->width = width;
  osxwindow->height = height;
94 95 96 97 98 99 100 101 102 103

  /* 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",
104 105
     "nsview", G_TYPE_POINTER, osxwindow->gstview,
     nil);
106 107 108 109

  msg = gst_message_new_element (GST_OBJECT (osxvideosink), s);
  gst_element_post_message (GST_ELEMENT (osxvideosink), msg);

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
  GST_INFO_OBJECT (osxvideosink, "'have-ns-view' message sent");

  /* 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");
      gst_x_overlay_prepare_xwindow_id (GST_X_OVERLAY (osxvideosink));
    }

    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");
      [osxwindow->gstview performSelectorOnMainThread:@selector(addToSuperview:)
          withObject:osxvideosink->superview waitUntilDone:YES];
    } else {
      /* 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");
    }
  }
138 139 140

  [pool release];

141
  return res;
142 143 144
}

static void
145
gst_osx_video_sink_osxwindow_destroy (GstOSXVideoSink * osxvideosink)
146
{
147 148
  NSAutoreleasePool *pool;

149
  g_return_if_fail (GST_IS_OSX_VIDEO_SINK (osxvideosink));
150
  pool = [[NSAutoreleasePool alloc] init];
151

152
  if (osxvideosink->osxwindow) {
153 154 155 156 157
    if (osxvideosink->superview) {
      [osxvideosink->osxwindow->gstview
          performSelectorOnMainThread:@selector(removeFromSuperview:)
            withObject:(id)nil waitUntilDone:YES];
    }
158
    [osxvideosink->osxwindow->gstview release];
159

160 161
    g_free (osxvideosink->osxwindow);
    osxvideosink->osxwindow = NULL;
162
  }
163
  [pool release];
164 165 166 167 168 169 170
}

/* This function resizes a GstXWindow */
static void
gst_osx_video_sink_osxwindow_resize (GstOSXVideoSink * osxvideosink,
    GstOSXWindow * osxwindow, guint width, guint height)
{
171
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
172 173 174 175 176 177
  g_return_if_fail (osxwindow != NULL);
  g_return_if_fail (GST_IS_OSX_VIDEO_SINK (osxvideosink));

  osxwindow->width = width;
  osxwindow->height = height;

178
  GST_DEBUG_OBJECT (osxvideosink, "Resizing window to (%d,%d)", width, height);
179

180 181 182
  /* Directly resize the underlying view */
  GST_DEBUG_OBJECT (osxvideosink, "Calling setVideoSize on %p", osxwindow->gstview); 
  [osxwindow->gstview setVideoSize:width :height];
183

184
  [pool release];
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
}

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;
  }

207 208
  GST_DEBUG_OBJECT (osxvideosink, "our format is: %dx%d video",
      video_width, video_height);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226

  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;
227
  GstStateChangeReturn ret;
228 229 230

  osxvideosink = GST_OSX_VIDEO_SINK (element);

231
  GST_DEBUG_OBJECT (osxvideosink, "%s => %s", 
232 233
        gst_element_state_get_name(GST_STATE_TRANSITION_CURRENT (transition)),
        gst_element_state_get_name(GST_STATE_TRANSITION_NEXT (transition)));
234

235 236
  switch (transition) {
    case GST_STATE_CHANGE_NULL_TO_READY:
237 238
      break;
    case GST_STATE_CHANGE_READY_TO_PAUSED:
239
      /* Creating our window and our image */
240 241
      GST_VIDEO_SINK_WIDTH (osxvideosink) = 320;
      GST_VIDEO_SINK_HEIGHT (osxvideosink) = 240;
242
      if (!gst_osx_video_sink_osxwindow_create (osxvideosink,
243
          GST_VIDEO_SINK_WIDTH (osxvideosink),
244 245 246 247
          GST_VIDEO_SINK_HEIGHT (osxvideosink))) {
        ret = GST_STATE_CHANGE_FAILURE;
        goto done;
      }
248
      break;
249
    default:
250
      break;
251 252 253 254 255
  }

  ret = (GST_ELEMENT_CLASS (parent_class))->change_state (element, transition);

  switch (transition) {
256 257 258
    case GST_STATE_CHANGE_PAUSED_TO_READY:
      GST_VIDEO_SINK_WIDTH (osxvideosink) = 0;
      GST_VIDEO_SINK_HEIGHT (osxvideosink) = 0;
259
      gst_osx_video_sink_osxwindow_destroy (osxvideosink);
260 261 262
      break;
    case GST_STATE_CHANGE_READY_TO_NULL:
      break;
263 264
    default:
      break;
265 266
  }

267
done:
268
  return ret;
269 270 271 272 273 274
}

static GstFlowReturn
gst_osx_video_sink_show_frame (GstBaseSink * bsink, GstBuffer * buf)
{
  GstOSXVideoSink *osxvideosink;
275 276
  guint8 *viewdata;
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
277 278

  osxvideosink = GST_OSX_VIDEO_SINK (bsink);
279
  viewdata = (guint8 *) [osxvideosink->osxwindow->gstview getTextureBuffer];
280 281 282 283 284

  GST_DEBUG ("show_frame");
  memcpy (viewdata, GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
  [osxvideosink->osxwindow->gstview displayTexture];

285 286
  [pool release];

287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  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:
312
      /* Ignore, just here for backwards compatibility */
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
      break;
    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:
332
      g_value_set_boolean (value, TRUE);
333 334 335 336 337 338 339
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}

340

341 342 343 344
static void
gst_osx_video_sink_init (GstOSXVideoSink * osxvideosink)
{
  osxvideosink->osxwindow = NULL;
345
  osxvideosink->superview = NULL;
346 347 348 349 350 351 352
}

static void
gst_osx_video_sink_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

353 354 355
  gst_element_class_set_details_simple (element_class, "OSX Video sink",
      "Sink/Video", "OSX native videosink",
      "Zaheer Abbas Merali <zaheerabbas at merali dot org>");
356 357 358 359 360

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&gst_osx_video_sink_sink_template_factory));
}

361 362 363 364 365 366 367 368 369 370 371
static void
gst_osx_video_sink_finalize (GObject *object)
{
  GstOSXVideoSink *osxvideosink = GST_OSX_VIDEO_SINK (object);

  if (osxvideosink->superview)
    [osxvideosink->superview release];

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
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);

  gobject_class->set_property = gst_osx_video_sink_set_property;
  gobject_class->get_property = gst_osx_video_sink_get_property;
388
  gobject_class->finalize = gst_osx_video_sink_finalize;
389 390 391 392 393 394

  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;

395 396 397 398 399 400 401
  /**
   * GstOSXVideoSink:embed
   *
   * 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));
405 406
}

407 408 409 410 411 412 413 414 415 416 417 418 419 420
static gboolean
gst_osx_video_sink_interface_supported (GstImplementsInterface * iface, GType type)
{
  g_assert (type == GST_TYPE_X_OVERLAY);
  return TRUE;
}

static void
gst_osx_video_sink_interface_init (GstImplementsInterfaceClass * klass)
{
  klass->supported = gst_osx_video_sink_interface_supported;
}

static void
421
gst_osx_video_sink_set_window_handle (GstXOverlay * overlay, guintptr handle_id)
422 423
{
  GstOSXVideoSink *osxvideosink = GST_OSX_VIDEO_SINK (overlay);
424
  gulong window_id = (gulong) handle_id;
425 426 427

  if (osxvideosink->superview) {
    GST_INFO_OBJECT (osxvideosink, "old xwindow id %p", osxvideosink->superview);
428 429 430 431 432
    if (osxvideosink->osxwindow) {
      [osxvideosink->osxwindow->gstview
          performSelectorOnMainThread:@selector(removeFromSuperview:)
            withObject:(id)nil waitUntilDone:YES];
    }
433 434 435 436 437
    [osxvideosink->superview release];
  }

  GST_INFO_OBJECT (osxvideosink, "set xwindow id 0x%lx", window_id);
  osxvideosink->superview = [((NSView *) window_id) retain];
438 439 440 441
  if (osxvideosink->osxwindow) {
      [osxvideosink->osxwindow->gstview performSelectorOnMainThread:@selector(addToSuperview:)
          withObject:osxvideosink->superview waitUntilDone:YES];
  }
442 443 444 445 446
}

static void
gst_osx_video_sink_xoverlay_init (GstXOverlayClass * iface)
{
447
  iface->set_window_handle = gst_osx_video_sink_set_window_handle;
448 449 450 451
  iface->expose = NULL;
  iface->handle_events = NULL;
}

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
/* ============================================================= */
/*                                                               */
/*                       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,
    };

482 483 484 485 486 487 488 489 490 491 492 493
    static const GInterfaceInfo iface_info = {
      (GInterfaceInitFunc) gst_osx_video_sink_interface_init,
      NULL,
      NULL,
    };

    static const GInterfaceInfo overlay_info = {
      (GInterfaceInitFunc) gst_osx_video_sink_xoverlay_init,
      NULL,
      NULL,
    };

494 495 496
    osxvideosink_type = g_type_register_static (GST_TYPE_VIDEO_SINK,
        "GstOSXVideoSink", &osxvideosink_info, 0);

497 498 499 500
    g_type_add_interface_static (osxvideosink_type,
        GST_TYPE_IMPLEMENTS_INTERFACE, &iface_info);
    g_type_add_interface_static (osxvideosink_type, GST_TYPE_X_OVERLAY,
        &overlay_info);
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
  }

  return osxvideosink_type;
}

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,
522
    osxvideo,
523 524
    "OSX native video output plugin",
    plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN)