3 * Copyright (C) 2003 Julien Moutte <julien@moutte.net>
4 * Copyright (C) 2005,2006,2007 David A. Schleef <ds@schleef.org>
5 * Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
6 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
25 * SECTION:element-glimagesink
28 * glimagesink renders video frames to a drawable on a local or remote
29 * display using OpenGL. This element can receive a Window ID from the
30 * application through the VideoOverlay interface and will then render video
31 * frames in this drawable.
32 * If no Window ID was provided by the application, the element will
33 * create its own internal window and render into it.
35 * See the #GstGLDisplay documentation for a list of environment variables that
36 * can override window/platform detection.
40 * Depends on the driver, OpenGL handles hardware accelerated
41 * scaling of video frames. This means that the element will just accept
42 * incoming video frames no matter their geometry and will then put them to the
43 * drawable scaling them on the fly. Using the #GstGLImageSink:force-aspect-ratio
44 * property it is possible to enforce scaling with a constant aspect ratio,
45 * which means drawing black borders around the video frame.
49 * Through the gl thread, glimagesink handle some events coming from the drawable
50 * to manage its appearance even when the data is not flowing (GST_STATE_PAUSED).
51 * That means that even when the element is paused, it will receive expose events
52 * from the drawable and draw the latest frame with correct borders/aspect-ratio.
56 * gst-launch-1.0 -v videotestsrc ! video/x-raw ! glimagesink
57 * ]| A pipeline to test hardware scaling.
58 * No special opengl extension is used in this pipeline, that's why it should work
59 * with OpenGL >= 1.1. That's the case if you are using the MESA3D driver v1.3.
61 * gst-launch-1.0 -v videotestsrc ! video/x-raw,format=I420 ! glimagesink
62 * ]| A pipeline to test hardware scaling and hardware colorspace conversion.
63 * When your driver supports GLSL (OpenGL Shading Language needs OpenGL >= 2.1),
64 * the 4 following format YUY2, UYVY, I420, YV12 and AYUV are converted to RGB32
65 * through some fragment shaders and using one framebuffer (FBO extension OpenGL >= 1.4).
66 * If your driver does not support GLSL but supports MESA_YCbCr extension then
67 * the you can use YUY2 and UYVY. In this case the colorspace conversion is automatically
68 * made when loading the texture and therefore no framebuffer is used.
70 * gst-launch-1.0 -v gltestsrc ! glimagesink
71 * ]| A pipeline 100% OpenGL.
72 * No special opengl extension is used in this pipeline, that's why it should work
73 * with OpenGL >= 1.1. That's the case if you are using the MESA3D driver v1.3.
75 * gst-plugins-bas/tests/examples/gl/generic/cube
76 * ]| The graphic FPS scene can be greater than the input video FPS.
77 * The graphic scene can be written from a client code through the
78 * two glfilterapp properties.
86 #include <gst/video/videooverlay.h>
87 #include <gst/video/navigation.h>
88 #include <gst/video/gstvideoaffinetransformationmeta.h>
90 #include "gstglimagesink.h"
91 #include "gstglsinkbin.h"
92 #include "gstglutils.h"
94 #include <gst/gl/gstglviewconvert.h>
96 GST_DEBUG_CATEGORY (gst_debug_glimage_sink);
97 #define GST_CAT_DEFAULT gst_debug_glimage_sink
99 #define DEFAULT_SHOW_PREROLL_FRAME TRUE
100 #define DEFAULT_HANDLE_EVENTS TRUE
101 #define DEFAULT_FORCE_ASPECT_RATIO TRUE
102 #define DEFAULT_IGNORE_ALPHA TRUE
104 #define DEFAULT_MULTIVIEW_MODE GST_VIDEO_MULTIVIEW_MODE_MONO
105 #define DEFAULT_MULTIVIEW_FLAGS GST_VIDEO_MULTIVIEW_FLAGS_NONE
106 #define DEFAULT_MULTIVIEW_DOWNMIX GST_GL_STEREO_DOWNMIX_ANAGLYPH_GREEN_MAGENTA_DUBOIS
108 typedef GstGLSinkBin GstGLImageSinkBin;
109 typedef GstGLSinkBinClass GstGLImageSinkBinClass;
111 G_DEFINE_TYPE (GstGLImageSinkBin, gst_gl_image_sink_bin, GST_TYPE_GL_SINK_BIN);
116 PROP_BIN_ROTATE_METHOD,
117 PROP_BIN_FORCE_ASPECT_RATIO,
118 PROP_BIN_PIXEL_ASPECT_RATIO,
119 PROP_BIN_HANDLE_EVENTS,
121 PROP_BIN_IGNORE_ALPHA,
122 PROP_BIN_SHOW_PREROLL_FRAME,
123 PROP_BIN_OUTPUT_MULTIVIEW_LAYOUT,
124 PROP_BIN_OUTPUT_MULTIVIEW_FLAGS,
125 PROP_BIN_OUTPUT_MULTIVIEW_DOWNMIX_MODE,
132 SIGNAL_BIN_CLIENT_DRAW,
133 SIGNAL_BIN_CLIENT_RESHAPE,
137 static guint gst_gl_image_sink_bin_signals[SIGNAL_BIN_LAST] = { 0 };
140 gst_gl_image_sink_bin_set_property (GObject * object, guint prop_id,
141 const GValue * value, GParamSpec * param_spec)
143 g_object_set_property (G_OBJECT (GST_GL_SINK_BIN (object)->sink),
144 param_spec->name, value);
148 gst_gl_image_sink_bin_get_property (GObject * object, guint prop_id,
149 GValue * value, GParamSpec * param_spec)
151 g_object_get_property (G_OBJECT (GST_GL_SINK_BIN (object)->sink),
152 param_spec->name, value);
156 _on_client_reshape (GstGLImageSink * sink, GstGLContext * context,
157 guint width, guint height, gpointer data)
161 g_signal_emit (data, gst_gl_image_sink_bin_signals[SIGNAL_BIN_CLIENT_RESHAPE],
162 0, context, width, height, &ret);
168 _on_client_draw (GstGLImageSink * sink, GstGLContext * context,
169 GstSample * sample, gpointer data)
173 g_signal_emit (data, gst_gl_image_sink_bin_signals[SIGNAL_BIN_CLIENT_DRAW], 0,
174 context, sample, &ret);
179 #define DEFAULT_ROTATE_METHOD GST_GL_ROTATE_METHOD_IDENTITY
181 #define GST_TYPE_GL_ROTATE_METHOD (gst_gl_rotate_method_get_type())
183 static const GEnumValue rotate_methods[] = {
184 {GST_GL_ROTATE_METHOD_IDENTITY, "Identity (no rotation)", "none"},
185 {GST_GL_ROTATE_METHOD_90R, "Rotate clockwise 90 degrees", "clockwise"},
186 {GST_GL_ROTATE_METHOD_180, "Rotate 180 degrees", "rotate-180"},
187 {GST_GL_ROTATE_METHOD_90L, "Rotate counter-clockwise 90 degrees",
189 {GST_GL_ROTATE_METHOD_FLIP_HORIZ, "Flip horizontally", "horizontal-flip"},
190 {GST_GL_ROTATE_METHOD_FLIP_VERT, "Flip vertically", "vertical-flip"},
191 {GST_GL_ROTATE_METHOD_FLIP_UL_LR,
192 "Flip across upper left/lower right diagonal", "upper-left-diagonal"},
193 {GST_GL_ROTATE_METHOD_FLIP_UR_LL,
194 "Flip across upper right/lower left diagonal", "upper-right-diagonal"},
195 {GST_GL_ROTATE_METHOD_AUTO,
196 "Select rotate method based on image-orientation tag", "automatic"},
201 gst_gl_rotate_method_get_type (void)
203 static GType rotate_method_type = 0;
205 if (!rotate_method_type) {
206 rotate_method_type = g_enum_register_static ("GstGLRotateMethod",
209 return rotate_method_type;
213 gst_gl_image_sink_bin_init (GstGLImageSinkBin * self)
215 GstGLImageSink *sink = g_object_new (GST_TYPE_GLIMAGE_SINK, NULL);
217 g_signal_connect (sink, "client-reshape", (GCallback) _on_client_reshape,
219 g_signal_connect (sink, "client-draw", (GCallback) _on_client_draw, self);
221 gst_gl_sink_bin_finish_init_with_element (GST_GL_SINK_BIN (self),
226 gst_gl_image_sink_bin_class_init (GstGLImageSinkBinClass * klass)
228 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
230 gobject_class->get_property = gst_gl_image_sink_bin_get_property;
231 gobject_class->set_property = gst_gl_image_sink_bin_set_property;
234 g_object_class_install_property (gobject_class, PROP_BIN_ROTATE_METHOD,
235 g_param_spec_enum ("rotate-method",
238 GST_TYPE_GL_ROTATE_METHOD, DEFAULT_ROTATE_METHOD,
239 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
240 g_object_class_install_property (gobject_class, PROP_BIN_FORCE_ASPECT_RATIO,
241 g_param_spec_boolean ("force-aspect-ratio",
242 "Force aspect ratio",
243 "When enabled, scaling will respect original aspect ratio",
244 DEFAULT_FORCE_ASPECT_RATIO,
245 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
246 g_object_class_install_property (gobject_class, PROP_BIN_HANDLE_EVENTS,
247 g_param_spec_boolean ("handle-events", "Handle XEvents",
248 "When enabled, XEvents will be selected and handled",
249 DEFAULT_HANDLE_EVENTS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250 g_object_class_install_property (gobject_class, PROP_BIN_IGNORE_ALPHA,
251 g_param_spec_boolean ("ignore-alpha", "Ignore Alpha",
252 "When enabled, alpha will be ignored and converted to black",
253 DEFAULT_IGNORE_ALPHA, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
254 g_object_class_install_property (gobject_class, PROP_BIN_CONTEXT,
255 g_param_spec_object ("context", "OpenGL context", "Get OpenGL context",
256 GST_TYPE_GL_CONTEXT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
257 g_object_class_install_property (gobject_class, PROP_BIN_PIXEL_ASPECT_RATIO,
258 gst_param_spec_fraction ("pixel-aspect-ratio", "Pixel Aspect Ratio",
259 "The pixel aspect ratio of the device", 0, 1, G_MAXINT, 1, 1, 1,
260 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
263 g_object_class_install_property (gobject_class, PROP_BIN_SHOW_PREROLL_FRAME,
264 g_param_spec_boolean ("show-preroll-frame", "Show preroll frame",
265 "Whether to render video frames during preroll",
266 DEFAULT_SHOW_PREROLL_FRAME,
267 G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
269 g_object_class_install_property (gobject_class,
270 PROP_BIN_OUTPUT_MULTIVIEW_LAYOUT,
271 g_param_spec_enum ("output-multiview-mode", "Output Multiview Mode",
272 "Choose output mode for multiview/3D video",
273 GST_TYPE_VIDEO_MULTIVIEW_MODE, DEFAULT_MULTIVIEW_MODE,
274 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
275 g_object_class_install_property (gobject_class,
276 PROP_BIN_OUTPUT_MULTIVIEW_FLAGS,
277 g_param_spec_flags ("output-multiview-flags", "Output Multiview Flags",
278 "Output multiview layout modifier flags",
279 GST_TYPE_VIDEO_MULTIVIEW_FLAGS, DEFAULT_MULTIVIEW_FLAGS,
280 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
281 g_object_class_install_property (gobject_class,
282 PROP_BIN_OUTPUT_MULTIVIEW_DOWNMIX_MODE,
283 g_param_spec_enum ("output-multiview-downmix-mode",
284 "Mode for mono downmixed output",
285 "Output anaglyph type to generate when downmixing to mono",
286 GST_TYPE_GL_STEREO_DOWNMIX, DEFAULT_MULTIVIEW_DOWNMIX,
287 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289 gst_video_overlay_install_properties (gobject_class, PROP_BIN_LAST);
291 gst_gl_image_sink_bin_signals[SIGNAL_BIN_CLIENT_DRAW] =
292 g_signal_new ("client-draw", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
293 0, NULL, NULL, NULL, G_TYPE_BOOLEAN, 2, GST_TYPE_GL_CONTEXT,
296 gst_gl_image_sink_bin_signals[SIGNAL_BIN_CLIENT_RESHAPE] =
297 g_signal_new ("client-reshape", G_TYPE_FROM_CLASS (klass),
298 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_BOOLEAN, 3,
299 GST_TYPE_GL_CONTEXT, G_TYPE_UINT, G_TYPE_UINT);
301 gst_type_mark_as_plugin_api (GST_TYPE_GL_ROTATE_METHOD);
304 #define GST_GLIMAGE_SINK_GET_LOCK(glsink) \
305 (GST_GLIMAGE_SINK(glsink)->drawing_lock)
306 #define GST_GLIMAGE_SINK_LOCK(glsink) \
307 (g_mutex_lock(&GST_GLIMAGE_SINK_GET_LOCK (glsink)))
308 #define GST_GLIMAGE_SINK_UNLOCK(glsink) \
309 (g_mutex_unlock(&GST_GLIMAGE_SINK_GET_LOCK (glsink)))
311 #define USING_OPENGL(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL, 1, 0))
312 #define USING_OPENGL3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_OPENGL3, 3, 1))
313 #define USING_GLES(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES, 1, 0))
314 #define USING_GLES2(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 2, 0))
315 #define USING_GLES3(context) (gst_gl_context_check_gl_version (context, GST_GL_API_GLES2, 3, 0))
317 #define SUPPORTED_GL_APIS GST_GL_API_OPENGL | GST_GL_API_GLES2 | GST_GL_API_OPENGL3
319 static void gst_glimage_sink_thread_init_redisplay (GstGLImageSink * gl_sink);
320 static void gst_glimage_sink_cleanup_glthread (GstGLImageSink * gl_sink);
321 static void gst_glimage_sink_on_close (GstGLImageSink * gl_sink);
322 static void gst_glimage_sink_on_resize (GstGLImageSink * gl_sink,
323 gint width, gint height);
324 static void gst_glimage_sink_on_draw (GstGLImageSink * gl_sink);
325 static gboolean gst_glimage_sink_redisplay (GstGLImageSink * gl_sink);
327 static void gst_glimage_sink_finalize (GObject * object);
328 static void gst_glimage_sink_set_property (GObject * object, guint prop_id,
329 const GValue * value, GParamSpec * param_spec);
330 static void gst_glimage_sink_get_property (GObject * object, guint prop_id,
331 GValue * value, GParamSpec * param_spec);
333 static gboolean gst_glimage_sink_event (GstBaseSink * sink, GstEvent * event);
334 static gboolean gst_glimage_sink_query (GstBaseSink * bsink, GstQuery * query);
335 static void gst_glimage_sink_set_context (GstElement * element,
336 GstContext * context);
338 static GstStateChangeReturn
339 gst_glimage_sink_change_state (GstElement * element, GstStateChange transition);
341 static void gst_glimage_sink_get_times (GstBaseSink * bsink, GstBuffer * buf,
342 GstClockTime * start, GstClockTime * end);
343 static gboolean gst_glimage_sink_set_caps (GstBaseSink * bsink, GstCaps * caps);
344 static GstCaps *gst_glimage_sink_get_caps (GstBaseSink * bsink,
346 static GstFlowReturn gst_glimage_sink_prepare (GstBaseSink * bsink,
348 static GstFlowReturn gst_glimage_sink_show_frame (GstVideoSink * bsink,
350 static gboolean gst_glimage_sink_propose_allocation (GstBaseSink * bsink,
353 static void gst_glimage_sink_video_overlay_init (GstVideoOverlayInterface *
355 static void gst_glimage_sink_set_window_handle (GstVideoOverlay * overlay,
357 static void gst_glimage_sink_expose (GstVideoOverlay * overlay);
358 static void gst_glimage_sink_set_render_rectangle (GstVideoOverlay * overlay,
359 gint x, gint y, gint width, gint height);
360 static void gst_glimage_sink_handle_events (GstVideoOverlay * overlay,
361 gboolean handle_events);
362 static gboolean update_output_format (GstGLImageSink * glimage_sink);
364 #define GST_GL_SINK_CAPS \
365 "video/x-raw(" GST_CAPS_FEATURE_MEMORY_GL_MEMORY "), " \
366 "format = (string) RGBA, " \
367 "width = " GST_VIDEO_SIZE_RANGE ", " \
368 "height = " GST_VIDEO_SIZE_RANGE ", " \
369 "framerate = " GST_VIDEO_FPS_RANGE ", " \
370 "texture-target = (string) { 2D, external-oes } " \
372 "video/x-raw(" GST_CAPS_FEATURE_MEMORY_GL_MEMORY "," \
373 GST_CAPS_FEATURE_META_GST_VIDEO_OVERLAY_COMPOSITION "), " \
374 "format = (string) RGBA, " \
375 "width = " GST_VIDEO_SIZE_RANGE ", " \
376 "height = " GST_VIDEO_SIZE_RANGE ", " \
377 "framerate = " GST_VIDEO_FPS_RANGE ", " \
378 "texture-target = (string) { 2D, external-oes } "
380 static GstStaticPadTemplate gst_glimage_sink_template =
381 GST_STATIC_PAD_TEMPLATE ("sink",
384 GST_STATIC_CAPS (GST_GL_SINK_CAPS));
391 PROP_FORCE_ASPECT_RATIO,
392 PROP_PIXEL_ASPECT_RATIO,
396 PROP_OUTPUT_MULTIVIEW_LAYOUT,
397 PROP_OUTPUT_MULTIVIEW_FLAGS,
398 PROP_OUTPUT_MULTIVIEW_DOWNMIX_MODE,
406 CLIENT_RESHAPE_SIGNAL,
410 static guint gst_glimage_sink_signals[LAST_SIGNAL] = { 0 };
413 _display_size_to_stream_size (GstGLImageSink * gl_sink, gdouble x,
414 gdouble y, gdouble * stream_x, gdouble * stream_y)
416 gdouble stream_width, stream_height;
418 stream_width = (gdouble) GST_VIDEO_INFO_WIDTH (&gl_sink->out_info);
419 stream_height = (gdouble) GST_VIDEO_INFO_HEIGHT (&gl_sink->out_info);
421 /* from display coordinates to stream coordinates */
422 if (gl_sink->display_rect.w > 0)
424 (x - gl_sink->display_rect.x) / gl_sink->display_rect.w * stream_width;
428 /* clip to stream size */
431 if (*stream_x > GST_VIDEO_INFO_WIDTH (&gl_sink->out_info))
432 *stream_x = GST_VIDEO_INFO_WIDTH (&gl_sink->out_info);
434 /* same for y-axis */
435 if (gl_sink->display_rect.h > 0)
437 (y - gl_sink->display_rect.y) / gl_sink->display_rect.h * stream_height;
443 if (*stream_y > GST_VIDEO_INFO_HEIGHT (&gl_sink->out_info))
444 *stream_y = GST_VIDEO_INFO_HEIGHT (&gl_sink->out_info);
446 GST_TRACE ("transform %fx%f into %fx%f", x, y, *stream_x, *stream_y);
450 _display_scroll_value_to_stream_scroll_value (GstGLImageSink * gl_sink,
451 gdouble delta_x, gdouble delta_y, gdouble * stream_delta_x,
452 gdouble * stream_delta_y)
454 gdouble stream_width, stream_height;
456 stream_width = (gdouble) GST_VIDEO_INFO_WIDTH (&gl_sink->out_info);
457 stream_height = (gdouble) GST_VIDEO_INFO_HEIGHT (&gl_sink->out_info);
459 if (delta_x != 0 && gl_sink->display_rect.w > 0)
460 *stream_delta_x = delta_x * (stream_width / gl_sink->display_rect.w);
462 *stream_delta_x = delta_x;
464 if (delta_y != 0 && gl_sink->display_rect.h > 0)
465 *stream_delta_y = delta_y * (stream_height / gl_sink->display_rect.h);
467 *stream_delta_y = delta_y;
469 GST_TRACE_OBJECT (gl_sink, "transform %fx%f into %fx%f", delta_x, delta_y,
470 *stream_delta_x, *stream_delta_y);
474 static const gfloat clockwise_matrix[] = {
475 0.0f, -1.0f, 0.0f, 0.0f,
476 1.0f, 0.0f, 0.0f, 0.0f,
477 0.0f, 0.0f, 1.0f, 0.0f,
478 0.0f, 0.0f, 0.0f, 1.0f,
482 static const gfloat clockwise_180_matrix[] = {
483 -1.0f, 0.0f, 0.0f, 0.0f,
484 0.0f, -1.0f, 0.0f, 0.0f,
485 0.0f, 0.0f, 1.0f, 0.0f,
486 0.0f, 0.0f, 0.0f, 1.0f,
490 static const gfloat counterclockwise_matrix[] = {
491 0.0f, 1.0f, 0.0f, 0.0f,
492 -1.0f, 0.0f, 0.0f, 0.0f,
493 0.0f, 0.0f, 1.0f, 0.0f,
494 0.0f, 0.0f, 0.0f, 1.0f,
497 /* horizontal-flip */
498 static const gfloat horizontal_flip_matrix[] = {
499 -1.0f, 0.0f, 0.0f, 0.0f,
500 0.0f, 1.0f, 0.0f, 0.0f,
501 0.0f, 0.0f, 1.0f, 0.0f,
502 0.0f, 0.0f, 0.0f, 1.0f,
506 static const gfloat vertical_flip_matrix[] = {
507 1.0f, 0.0f, 0.0f, 0.0f,
508 0.0f, -1.0f, 0.0f, 0.0f,
509 0.0f, 0.0f, 1.0f, 0.0f,
510 0.0f, 0.0f, 0.0f, 1.0f,
513 /* upper-left-diagonal */
514 static const gfloat upper_left_matrix[] = {
515 0.0f, -1.0f, 0.0f, 0.0f,
516 -1.0f, 0.0f, 0.0f, 0.0f,
517 0.0f, 0.0f, 1.0f, 0.0f,
518 0.0f, 0.0f, 0.0f, 1.0f,
521 /* upper-right-diagonal */
522 static const gfloat upper_right_matrix[] = {
523 0.0f, 1.0f, 0.0f, 0.0f,
524 1.0f, 0.0f, 0.0f, 0.0f,
525 0.0f, 0.0f, 1.0f, 0.0f,
526 0.0f, 0.0f, 0.0f, 1.0f,
530 gst_glimage_sink_set_rotate_method (GstGLImageSink * gl_sink,
531 GstGLRotateMethod method, gboolean from_tag)
533 GstGLRotateMethod tag_method = DEFAULT_ROTATE_METHOD;
534 GST_GLIMAGE_SINK_LOCK (gl_sink);
538 gl_sink->rotate_method = method;
540 if (gl_sink->rotate_method == GST_GL_ROTATE_METHOD_AUTO)
543 method = gl_sink->rotate_method;
545 if (method != gl_sink->current_rotate_method) {
546 GST_DEBUG_OBJECT (gl_sink, "Changing method from %s to %s",
547 rotate_methods[gl_sink->current_rotate_method].value_nick,
548 rotate_methods[method].value_nick);
551 case GST_GL_ROTATE_METHOD_IDENTITY:
552 gl_sink->transform_matrix = NULL;
553 gl_sink->output_mode_changed = TRUE;
555 case GST_GL_ROTATE_METHOD_90R:
556 gl_sink->transform_matrix = clockwise_matrix;
557 gl_sink->output_mode_changed = TRUE;
559 case GST_GL_ROTATE_METHOD_180:
560 gl_sink->transform_matrix = clockwise_180_matrix;
561 gl_sink->output_mode_changed = TRUE;
563 case GST_GL_ROTATE_METHOD_90L:
564 gl_sink->transform_matrix = counterclockwise_matrix;
565 gl_sink->output_mode_changed = TRUE;
567 case GST_GL_ROTATE_METHOD_FLIP_HORIZ:
568 gl_sink->transform_matrix = horizontal_flip_matrix;
569 gl_sink->output_mode_changed = TRUE;
571 case GST_GL_ROTATE_METHOD_FLIP_VERT:
572 gl_sink->transform_matrix = vertical_flip_matrix;
573 gl_sink->output_mode_changed = TRUE;
575 case GST_GL_ROTATE_METHOD_FLIP_UL_LR:
576 gl_sink->transform_matrix = upper_left_matrix;
577 gl_sink->output_mode_changed = TRUE;
579 case GST_GL_ROTATE_METHOD_FLIP_UR_LL:
580 gl_sink->transform_matrix = upper_right_matrix;
581 gl_sink->output_mode_changed = TRUE;
584 g_assert_not_reached ();
588 gl_sink->current_rotate_method = method;
590 GST_GLIMAGE_SINK_UNLOCK (gl_sink);
594 gst_glimage_sink_navigation_send_event (GstNavigation * navigation, GstStructure
597 GstGLImageSink *sink = GST_GLIMAGE_SINK (navigation);
598 gboolean handled = FALSE;
599 GstEvent *event = NULL;
604 if (!sink->context) {
605 gst_structure_free (structure);
609 window = gst_gl_context_get_window (sink->context);
610 g_return_if_fail (GST_IS_GL_WINDOW (window));
612 width = GST_VIDEO_SINK_WIDTH (sink);
613 height = GST_VIDEO_SINK_HEIGHT (sink);
614 gst_gl_window_get_surface_dimensions (window, &width, &height);
616 /* Converting pointer coordinates to the non scaled geometry */
617 if (width != 0 && gst_structure_get_double (structure, "pointer_x", &x)
618 && height != 0 && gst_structure_get_double (structure, "pointer_y", &y)) {
619 gdouble stream_x, stream_y;
621 _display_size_to_stream_size (sink, x, y, &stream_x, &stream_y);
623 gst_structure_set (structure, "pointer_x", G_TYPE_DOUBLE,
624 stream_x, "pointer_y", G_TYPE_DOUBLE, stream_y, NULL);
627 /* Converting pointer scroll coordinates to the non scaled geometry */
628 if (width != 0 && gst_structure_get_double (structure, "delta_pointer_x", &x)
630 && gst_structure_get_double (structure, "delta_pointer_y", &y)) {
631 gdouble stream_x, stream_y;
633 _display_scroll_value_to_stream_scroll_value (sink, x, y, &stream_x,
636 gst_structure_set (structure, "delta_pointer_x", G_TYPE_DOUBLE,
637 stream_x, "delta_pointer_y", G_TYPE_DOUBLE, stream_y, NULL);
640 event = gst_event_new_navigation (structure);
642 gst_event_ref (event);
643 handled = gst_pad_push_event (GST_VIDEO_SINK_PAD (sink), event);
646 gst_element_post_message ((GstElement *) sink,
647 gst_navigation_message_new_event ((GstObject *) sink, event));
649 gst_event_unref (event);
651 gst_object_unref (window);
655 gst_glimage_sink_navigation_interface_init (GstNavigationInterface * iface)
657 iface->send_event = gst_glimage_sink_navigation_send_event;
660 #define gst_glimage_sink_parent_class parent_class
661 G_DEFINE_TYPE_WITH_CODE (GstGLImageSink, gst_glimage_sink,
662 GST_TYPE_VIDEO_SINK, G_IMPLEMENT_INTERFACE (GST_TYPE_VIDEO_OVERLAY,
663 gst_glimage_sink_video_overlay_init);
664 G_IMPLEMENT_INTERFACE (GST_TYPE_NAVIGATION,
665 gst_glimage_sink_navigation_interface_init);
666 GST_DEBUG_CATEGORY_INIT (gst_debug_glimage_sink, "glimagesink", 0,
667 "OpenGL Video Sink"));
670 gst_glimage_sink_class_init (GstGLImageSinkClass * klass)
672 GObjectClass *gobject_class;
673 GstElementClass *gstelement_class;
674 GstBaseSinkClass *gstbasesink_class;
675 GstVideoSinkClass *gstvideosink_class;
676 GstElementClass *element_class;
678 gobject_class = (GObjectClass *) klass;
679 gstelement_class = (GstElementClass *) klass;
680 gstbasesink_class = (GstBaseSinkClass *) klass;
681 gstvideosink_class = (GstVideoSinkClass *) klass;
682 element_class = GST_ELEMENT_CLASS (klass);
684 gobject_class->set_property = gst_glimage_sink_set_property;
685 gobject_class->get_property = gst_glimage_sink_get_property;
687 g_object_class_install_property (gobject_class, PROP_ROTATE_METHOD,
688 g_param_spec_enum ("rotate-method",
691 GST_TYPE_GL_ROTATE_METHOD, DEFAULT_ROTATE_METHOD,
692 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
694 g_object_class_install_property (gobject_class, PROP_FORCE_ASPECT_RATIO,
695 g_param_spec_boolean ("force-aspect-ratio", "Force aspect ratio",
696 "When enabled, scaling will respect original aspect ratio",
697 DEFAULT_FORCE_ASPECT_RATIO,
698 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
700 g_object_class_install_property (gobject_class, PROP_PIXEL_ASPECT_RATIO,
701 gst_param_spec_fraction ("pixel-aspect-ratio", "Pixel Aspect Ratio",
702 "The pixel aspect ratio of the device", 0, 1, G_MAXINT, 1, 1, 1,
703 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
705 g_object_class_install_property (gobject_class, PROP_CONTEXT,
706 g_param_spec_object ("context", "OpenGL context", "Get OpenGL context",
707 GST_TYPE_GL_CONTEXT, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
709 g_object_class_install_property (gobject_class, PROP_HANDLE_EVENTS,
710 g_param_spec_boolean ("handle-events", "Handle XEvents",
711 "When enabled, XEvents will be selected and handled",
712 DEFAULT_HANDLE_EVENTS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
714 g_object_class_install_property (gobject_class, PROP_IGNORE_ALPHA,
715 g_param_spec_boolean ("ignore-alpha", "Ignore Alpha",
716 "When enabled, alpha will be ignored and converted to black",
717 DEFAULT_IGNORE_ALPHA, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
719 g_object_class_install_property (gobject_class, PROP_OUTPUT_MULTIVIEW_LAYOUT,
720 g_param_spec_enum ("output-multiview-mode",
721 "Output Multiview Mode",
722 "Choose output mode for multiview/3D video",
723 GST_TYPE_VIDEO_MULTIVIEW_MODE, DEFAULT_MULTIVIEW_MODE,
724 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
725 g_object_class_install_property (gobject_class, PROP_OUTPUT_MULTIVIEW_FLAGS,
726 g_param_spec_flags ("output-multiview-flags",
727 "Output Multiview Flags",
728 "Output multiview layout modifier flags",
729 GST_TYPE_VIDEO_MULTIVIEW_FLAGS, DEFAULT_MULTIVIEW_FLAGS,
730 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
731 g_object_class_install_property (gobject_class,
732 PROP_OUTPUT_MULTIVIEW_DOWNMIX_MODE,
733 g_param_spec_enum ("output-multiview-downmix-mode",
734 "Mode for mono downmixed output",
735 "Output anaglyph type to generate when downmixing to mono",
736 GST_TYPE_GL_STEREO_DOWNMIX, DEFAULT_MULTIVIEW_DOWNMIX,
737 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
739 gst_video_overlay_install_properties (gobject_class, PROP_LAST);
741 gst_element_class_set_metadata (element_class, "OpenGL video sink",
742 "Sink/Video", "A videosink based on OpenGL",
743 "Julien Isorce <julien.isorce@gmail.com>");
746 * GstGLImageSink::client-draw:
747 * @object: the #GstGLImageSink
748 * @context: the #GstGLContext
749 * @sample: the #GstSample
751 * Will be emitted before actually drawing the texture. The client should
752 * redraw the surface/contents of @sample and return %TRUE.
754 * Returns: whether the texture was redrawn by the signal. If not, a
755 * default redraw will occur.
757 gst_glimage_sink_signals[CLIENT_DRAW_SIGNAL] =
758 g_signal_new ("client-draw", G_TYPE_FROM_CLASS (klass),
759 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_BOOLEAN, 2,
760 GST_TYPE_GL_CONTEXT, GST_TYPE_SAMPLE);
763 * GstGLImageSink::client-reshape:
764 * @object: the #GstGLImageSink
765 * @context: the #GstGLContext
766 * @width: the #guint width of the texture.
767 * @height: the #guint height of the texture.
769 * The client should resize the surface/window/viewport with the @width and
770 * @height and return %TRUE.
772 * Returns: whether the content area was resized by the signal. If not, a
773 * default viewport resize will occur.
775 gst_glimage_sink_signals[CLIENT_RESHAPE_SIGNAL] =
776 g_signal_new ("client-reshape", G_TYPE_FROM_CLASS (klass),
777 G_SIGNAL_RUN_LAST, 0, NULL, NULL, NULL, G_TYPE_BOOLEAN, 3,
778 GST_TYPE_GL_CONTEXT, G_TYPE_UINT, G_TYPE_UINT);
780 gst_element_class_add_static_pad_template (element_class,
781 &gst_glimage_sink_template);
783 gobject_class->finalize = gst_glimage_sink_finalize;
785 gstelement_class->change_state = gst_glimage_sink_change_state;
786 gstelement_class->set_context = gst_glimage_sink_set_context;
787 gstbasesink_class->event = gst_glimage_sink_event;
788 gstbasesink_class->query = GST_DEBUG_FUNCPTR (gst_glimage_sink_query);
789 gstbasesink_class->set_caps = gst_glimage_sink_set_caps;
790 gstbasesink_class->get_caps = gst_glimage_sink_get_caps;
791 gstbasesink_class->get_times = gst_glimage_sink_get_times;
792 gstbasesink_class->prepare = gst_glimage_sink_prepare;
793 gstbasesink_class->propose_allocation = gst_glimage_sink_propose_allocation;
795 gstvideosink_class->show_frame =
796 GST_DEBUG_FUNCPTR (gst_glimage_sink_show_frame);
800 gst_glimage_sink_init (GstGLImageSink * glimage_sink)
802 glimage_sink->window_id = 0;
803 glimage_sink->new_window_id = 0;
804 glimage_sink->display = NULL;
805 glimage_sink->keep_aspect_ratio = TRUE;
806 glimage_sink->par_n = 0;
807 glimage_sink->par_d = 1;
808 glimage_sink->redisplay_texture = 0;
809 glimage_sink->handle_events = TRUE;
810 glimage_sink->ignore_alpha = TRUE;
811 glimage_sink->overlay_compositor = NULL;
813 glimage_sink->mview_output_mode = DEFAULT_MULTIVIEW_MODE;
814 glimage_sink->mview_output_flags = DEFAULT_MULTIVIEW_FLAGS;
815 glimage_sink->mview_downmix_mode = DEFAULT_MULTIVIEW_DOWNMIX;
817 glimage_sink->current_rotate_method = DEFAULT_ROTATE_METHOD;
818 glimage_sink->transform_matrix = NULL;
820 g_mutex_init (&glimage_sink->drawing_lock);
824 gst_glimage_sink_set_property (GObject * object, guint prop_id,
825 const GValue * value, GParamSpec * pspec)
827 GstGLImageSink *glimage_sink;
829 g_return_if_fail (GST_IS_GLIMAGE_SINK (object));
831 glimage_sink = GST_GLIMAGE_SINK (object);
834 case PROP_ROTATE_METHOD:
835 gst_glimage_sink_set_rotate_method (glimage_sink,
836 g_value_get_enum (value), FALSE);
838 case PROP_FORCE_ASPECT_RATIO:
840 glimage_sink->keep_aspect_ratio = g_value_get_boolean (value);
843 case PROP_PIXEL_ASPECT_RATIO:
845 glimage_sink->par_n = gst_value_get_fraction_numerator (value);
846 glimage_sink->par_d = gst_value_get_fraction_denominator (value);
849 case PROP_HANDLE_EVENTS:
850 gst_glimage_sink_handle_events (GST_VIDEO_OVERLAY (glimage_sink),
851 g_value_get_boolean (value));
853 case PROP_IGNORE_ALPHA:
854 glimage_sink->ignore_alpha = g_value_get_boolean (value);
856 case PROP_OUTPUT_MULTIVIEW_LAYOUT:
857 GST_GLIMAGE_SINK_LOCK (glimage_sink);
858 glimage_sink->mview_output_mode = g_value_get_enum (value);
859 glimage_sink->output_mode_changed = TRUE;
860 GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
862 case PROP_OUTPUT_MULTIVIEW_FLAGS:
863 GST_GLIMAGE_SINK_LOCK (glimage_sink);
864 glimage_sink->mview_output_flags = g_value_get_flags (value);
865 glimage_sink->output_mode_changed = TRUE;
866 GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
868 case PROP_OUTPUT_MULTIVIEW_DOWNMIX_MODE:
869 GST_GLIMAGE_SINK_LOCK (glimage_sink);
870 glimage_sink->mview_downmix_mode = g_value_get_enum (value);
871 glimage_sink->output_mode_changed = TRUE;
872 GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
875 if (!gst_video_overlay_set_property (object, PROP_LAST, prop_id, value))
876 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
882 gst_glimage_sink_finalize (GObject * object)
884 GstGLImageSink *glimage_sink;
886 g_return_if_fail (GST_IS_GLIMAGE_SINK (object));
888 glimage_sink = GST_GLIMAGE_SINK (object);
889 g_mutex_clear (&glimage_sink->drawing_lock);
891 GST_DEBUG ("finalized");
892 G_OBJECT_CLASS (parent_class)->finalize (object);
896 gst_glimage_sink_get_property (GObject * object, guint prop_id,
897 GValue * value, GParamSpec * pspec)
899 GstGLImageSink *glimage_sink;
901 g_return_if_fail (GST_IS_GLIMAGE_SINK (object));
903 glimage_sink = GST_GLIMAGE_SINK (object);
906 case PROP_ROTATE_METHOD:
907 g_value_set_enum (value, glimage_sink->current_rotate_method);
909 case PROP_FORCE_ASPECT_RATIO:
910 g_value_set_boolean (value, glimage_sink->keep_aspect_ratio);
912 case PROP_PIXEL_ASPECT_RATIO:
913 gst_value_set_fraction (value, glimage_sink->par_n, glimage_sink->par_d);
916 g_value_set_object (value, glimage_sink->context);
918 case PROP_HANDLE_EVENTS:
919 g_value_set_boolean (value, glimage_sink->handle_events);
921 case PROP_IGNORE_ALPHA:
922 g_value_set_boolean (value, glimage_sink->ignore_alpha);
924 case PROP_OUTPUT_MULTIVIEW_LAYOUT:
925 g_value_set_enum (value, glimage_sink->mview_output_mode);
927 case PROP_OUTPUT_MULTIVIEW_FLAGS:
928 g_value_set_flags (value, glimage_sink->mview_output_flags);
930 case PROP_OUTPUT_MULTIVIEW_DOWNMIX_MODE:
931 g_value_set_enum (value, glimage_sink->mview_downmix_mode);
934 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
940 gst_glimage_sink_key_event_cb (GstGLWindow * window, char *event_name, char
941 *key_string, GstGLImageSink * gl_sink)
943 GST_DEBUG_OBJECT (gl_sink, "event %s key %s pressed", event_name, key_string);
944 gst_navigation_send_key_event (GST_NAVIGATION (gl_sink),
945 event_name, key_string);
949 gst_glimage_sink_mouse_event_cb (GstGLWindow * window, char *event_name,
950 int button, double posx, double posy, GstGLImageSink * gl_sink)
952 GST_DEBUG_OBJECT (gl_sink, "event %s at %g, %g", event_name, posx, posy);
953 gst_navigation_send_mouse_event (GST_NAVIGATION (gl_sink),
954 event_name, button, posx, posy);
959 gst_glimage_sink_mouse_scroll_event_cb (GstGLWindow * window,
960 double posx, double posy, double delta_x, double delta_y,
961 GstGLImageSink * gl_sink)
963 GST_DEBUG_OBJECT (gl_sink, "event scroll at %g, %g", posx, posy);
964 gst_navigation_send_mouse_scroll_event (GST_NAVIGATION (gl_sink),
965 posx, posy, delta_x, delta_y);
969 _ensure_gl_setup (GstGLImageSink * gl_sink)
971 GError *error = NULL;
973 GST_TRACE_OBJECT (gl_sink, "Ensuring setup");
975 if (!gl_sink->context) {
976 GST_OBJECT_LOCK (gl_sink->display);
978 GstGLContext *other_context = NULL;
979 GstGLWindow *window = NULL;
981 if (gl_sink->context) {
982 gst_object_unref (gl_sink->context);
983 gl_sink->context = NULL;
986 GST_DEBUG_OBJECT (gl_sink,
987 "No current context, creating one for %" GST_PTR_FORMAT,
990 if (gl_sink->other_context) {
991 other_context = gst_object_ref (gl_sink->other_context);
994 gst_gl_display_get_gl_context_for_thread (gl_sink->display, NULL);
997 if (!gst_gl_display_create_context (gl_sink->display,
998 other_context, &gl_sink->context, &error)) {
1000 gst_object_unref (other_context);
1001 GST_OBJECT_UNLOCK (gl_sink->display);
1005 GST_DEBUG_OBJECT (gl_sink,
1006 "created context %" GST_PTR_FORMAT " from other context %"
1007 GST_PTR_FORMAT, gl_sink->context, gl_sink->other_context);
1009 window = gst_gl_context_get_window (gl_sink->context);
1011 GST_DEBUG_OBJECT (gl_sink, "got window %" GST_PTR_FORMAT, window);
1013 if (!gl_sink->window_id && !gl_sink->new_window_id)
1014 gst_video_overlay_prepare_window_handle (GST_VIDEO_OVERLAY (gl_sink));
1016 GST_DEBUG_OBJECT (gl_sink,
1017 "window_id : %" G_GUINTPTR_FORMAT " , new_window_id : %"
1018 G_GUINTPTR_FORMAT, gl_sink->window_id, gl_sink->new_window_id);
1020 if (gl_sink->window_id != gl_sink->new_window_id) {
1021 gl_sink->window_id = gl_sink->new_window_id;
1022 GST_DEBUG_OBJECT (gl_sink, "Setting window handle on gl window");
1023 gst_gl_window_set_window_handle (window, gl_sink->window_id);
1026 gst_gl_window_handle_events (window, gl_sink->handle_events);
1028 /* setup callbacks */
1029 gst_gl_window_set_resize_callback (window,
1030 GST_GL_WINDOW_RESIZE_CB (gst_glimage_sink_on_resize),
1031 gst_object_ref (gl_sink), (GDestroyNotify) gst_object_unref);
1032 gst_gl_window_set_draw_callback (window,
1033 GST_GL_WINDOW_CB (gst_glimage_sink_on_draw),
1034 gst_object_ref (gl_sink), (GDestroyNotify) gst_object_unref);
1035 gst_gl_window_set_close_callback (window,
1036 GST_GL_WINDOW_CB (gst_glimage_sink_on_close),
1037 gst_object_ref (gl_sink), (GDestroyNotify) gst_object_unref);
1038 gl_sink->key_sig_id = g_signal_connect (window, "key-event", G_CALLBACK
1039 (gst_glimage_sink_key_event_cb), gl_sink);
1040 gl_sink->mouse_sig_id =
1041 g_signal_connect (window, "mouse-event",
1042 G_CALLBACK (gst_glimage_sink_mouse_event_cb), gl_sink);
1043 gl_sink->mouse_scroll_sig_id =
1044 g_signal_connect (window, "scroll-event",
1045 G_CALLBACK (gst_glimage_sink_mouse_scroll_event_cb), gl_sink);
1047 gst_gl_window_set_render_rectangle (window, gl_sink->x, gl_sink->y,
1048 gl_sink->width, gl_sink->height);
1051 gst_object_unref (other_context);
1052 gst_object_unref (window);
1053 } while (!gst_gl_display_add_context (gl_sink->display, gl_sink->context));
1054 GST_OBJECT_UNLOCK (gl_sink->display);
1056 GST_TRACE_OBJECT (gl_sink, "Already have a context");
1062 GST_ELEMENT_ERROR (gl_sink, RESOURCE, NOT_FOUND, ("%s", error->message),
1065 if (gl_sink->context) {
1066 gst_object_unref (gl_sink->context);
1067 gl_sink->context = NULL;
1070 g_clear_error (&error);
1077 gst_glimage_sink_event (GstBaseSink * sink, GstEvent * event)
1079 GstGLImageSink *gl_sink = GST_GLIMAGE_SINK (sink);
1080 GstTagList *taglist;
1084 GST_DEBUG_OBJECT (gl_sink, "handling %s event", GST_EVENT_TYPE_NAME (event));
1086 switch (GST_EVENT_TYPE (event)) {
1088 gst_event_parse_tag (event, &taglist);
1090 if (gst_tag_list_get_string (taglist, "image-orientation", &orientation)) {
1091 if (!g_strcmp0 ("rotate-0", orientation))
1092 gst_glimage_sink_set_rotate_method (gl_sink,
1093 GST_GL_ROTATE_METHOD_IDENTITY, TRUE);
1094 else if (!g_strcmp0 ("rotate-90", orientation))
1095 gst_glimage_sink_set_rotate_method (gl_sink, GST_GL_ROTATE_METHOD_90R,
1097 else if (!g_strcmp0 ("rotate-180", orientation))
1098 gst_glimage_sink_set_rotate_method (gl_sink, GST_GL_ROTATE_METHOD_180,
1100 else if (!g_strcmp0 ("rotate-270", orientation))
1101 gst_glimage_sink_set_rotate_method (gl_sink, GST_GL_ROTATE_METHOD_90L,
1103 else if (!g_strcmp0 ("flip-rotate-0", orientation))
1104 gst_glimage_sink_set_rotate_method (gl_sink,
1105 GST_GL_ROTATE_METHOD_FLIP_HORIZ, TRUE);
1106 else if (!g_strcmp0 ("flip-rotate-90", orientation))
1107 gst_glimage_sink_set_rotate_method (gl_sink,
1108 GST_GL_ROTATE_METHOD_FLIP_UR_LL, TRUE);
1109 else if (!g_strcmp0 ("flip-rotate-180", orientation))
1110 gst_glimage_sink_set_rotate_method (gl_sink,
1111 GST_GL_ROTATE_METHOD_FLIP_VERT, TRUE);
1112 else if (!g_strcmp0 ("flip-rotate-270", orientation))
1113 gst_glimage_sink_set_rotate_method (gl_sink,
1114 GST_GL_ROTATE_METHOD_FLIP_UL_LR, TRUE);
1116 g_free (orientation);
1123 ret = GST_BASE_SINK_CLASS (parent_class)->event (sink, event);
1129 gst_glimage_sink_query (GstBaseSink * bsink, GstQuery * query)
1131 GstGLImageSink *glimage_sink = GST_GLIMAGE_SINK (bsink);
1132 gboolean res = FALSE;
1134 switch (GST_QUERY_TYPE (query)) {
1135 case GST_QUERY_CONTEXT:
1137 if (gst_gl_handle_context_query ((GstElement *) glimage_sink, query,
1138 glimage_sink->display, glimage_sink->context,
1139 glimage_sink->other_context))
1143 case GST_QUERY_DRAIN:
1147 GST_GLIMAGE_SINK_LOCK (glimage_sink);
1148 glimage_sink->redisplay_texture = 0;
1149 buf[0] = glimage_sink->stored_buffer[0];
1150 buf[1] = glimage_sink->stored_buffer[1];
1151 glimage_sink->stored_buffer[0] = glimage_sink->stored_buffer[1] = NULL;
1152 glimage_sink->stored_sync_meta = glimage_sink->next_sync_meta = NULL;
1153 GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
1155 gst_buffer_replace (buf, NULL);
1156 gst_buffer_replace (buf + 1, NULL);
1158 gst_buffer_replace (&glimage_sink->input_buffer, NULL);
1159 gst_buffer_replace (&glimage_sink->input_buffer2, NULL);
1160 gst_buffer_replace (&glimage_sink->next_buffer, NULL);
1161 gst_buffer_replace (&glimage_sink->next_buffer2, NULL);
1162 gst_buffer_replace (&glimage_sink->next_sync, NULL);
1164 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
1168 res = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
1176 gst_glimage_sink_set_context (GstElement * element, GstContext * context)
1178 GstGLImageSink *gl_sink = GST_GLIMAGE_SINK (element);
1180 gst_gl_handle_set_context (element, context, &gl_sink->display,
1181 &gl_sink->other_context);
1183 if (gl_sink->display)
1184 gst_gl_display_filter_gl_api (gl_sink->display, SUPPORTED_GL_APIS);
1186 GST_ELEMENT_CLASS (parent_class)->set_context (element, context);
1189 static GstStateChangeReturn
1190 gst_glimage_sink_change_state (GstElement * element, GstStateChange transition)
1192 GstGLImageSink *glimage_sink;
1193 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
1195 GST_DEBUG ("changing state: %s => %s",
1196 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
1197 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
1199 glimage_sink = GST_GLIMAGE_SINK (element);
1201 switch (transition) {
1202 case GST_STATE_CHANGE_NULL_TO_READY:
1203 if (!gst_gl_ensure_element_data (glimage_sink, &glimage_sink->display,
1204 &glimage_sink->other_context))
1205 return GST_STATE_CHANGE_FAILURE;
1207 gst_gl_display_filter_gl_api (glimage_sink->display, SUPPORTED_GL_APIS);
1209 if (!_ensure_gl_setup (glimage_sink))
1210 return GST_STATE_CHANGE_FAILURE;
1212 case GST_STATE_CHANGE_READY_TO_PAUSED:
1213 g_atomic_int_set (&glimage_sink->to_quit, 0);
1215 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1221 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1222 if (ret == GST_STATE_CHANGE_FAILURE)
1225 switch (transition) {
1226 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1228 case GST_STATE_CHANGE_PAUSED_TO_READY:
1232 GST_GLIMAGE_SINK_LOCK (glimage_sink);
1233 /* mark the redisplay_texture as unavailable (=0)
1236 glimage_sink->redisplay_texture = 0;
1237 buf[0] = glimage_sink->stored_buffer[0];
1238 buf[1] = glimage_sink->stored_buffer[1];
1239 glimage_sink->stored_buffer[0] = glimage_sink->stored_buffer[1] = NULL;
1240 glimage_sink->stored_sync_meta = glimage_sink->next_sync_meta = NULL;
1242 if (glimage_sink->stored_sync)
1243 gst_buffer_unref (glimage_sink->stored_sync);
1244 glimage_sink->stored_sync = NULL;
1246 GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
1248 gst_buffer_replace (buf, NULL);
1249 gst_buffer_replace (buf + 1, NULL);
1251 gst_object_replace ((GstObject **) & glimage_sink->convert_views, NULL);
1252 gst_buffer_replace (&glimage_sink->input_buffer, NULL);
1253 gst_buffer_replace (&glimage_sink->input_buffer2, NULL);
1254 gst_buffer_replace (&glimage_sink->next_buffer, NULL);
1255 gst_buffer_replace (&glimage_sink->next_buffer2, NULL);
1256 gst_buffer_replace (&glimage_sink->next_sync, NULL);
1258 glimage_sink->window_id = 0;
1259 /* but do not reset glimage_sink->new_window_id */
1261 GST_VIDEO_SINK_WIDTH (glimage_sink) = 1;
1262 GST_VIDEO_SINK_HEIGHT (glimage_sink) = 1;
1263 /* Clear cached caps */
1264 if (glimage_sink->out_caps) {
1265 gst_caps_unref (glimage_sink->out_caps);
1266 glimage_sink->out_caps = NULL;
1268 if (glimage_sink->in_caps) {
1269 gst_caps_unref (glimage_sink->in_caps);
1270 glimage_sink->in_caps = NULL;
1274 case GST_STATE_CHANGE_READY_TO_NULL:
1275 if (glimage_sink->overlay_compositor) {
1276 gst_object_unref (glimage_sink->overlay_compositor);
1277 glimage_sink->overlay_compositor = NULL;
1280 if (glimage_sink->context) {
1281 GstGLWindow *window = gst_gl_context_get_window (glimage_sink->context);
1283 gst_gl_window_send_message (window,
1284 GST_GL_WINDOW_CB (gst_glimage_sink_cleanup_glthread), glimage_sink);
1286 gst_gl_window_set_resize_callback (window, NULL, NULL, NULL);
1287 gst_gl_window_set_draw_callback (window, NULL, NULL, NULL);
1288 gst_gl_window_set_close_callback (window, NULL, NULL, NULL);
1290 if (glimage_sink->key_sig_id)
1291 g_signal_handler_disconnect (window, glimage_sink->key_sig_id);
1292 glimage_sink->key_sig_id = 0;
1293 if (glimage_sink->mouse_sig_id)
1294 g_signal_handler_disconnect (window, glimage_sink->mouse_sig_id);
1295 glimage_sink->mouse_sig_id = 0;
1296 if (glimage_sink->mouse_scroll_sig_id)
1297 g_signal_handler_disconnect (window,
1298 glimage_sink->mouse_scroll_sig_id);
1299 glimage_sink->mouse_scroll_sig_id = 0;
1301 gst_object_unref (window);
1302 gst_object_unref (glimage_sink->context);
1303 glimage_sink->context = NULL;
1306 glimage_sink->window_id = 0;
1308 if (glimage_sink->other_context) {
1309 gst_object_unref (glimage_sink->other_context);
1310 glimage_sink->other_context = NULL;
1313 if (glimage_sink->display) {
1314 gst_object_unref (glimage_sink->display);
1315 glimage_sink->display = NULL;
1326 gst_glimage_sink_get_times (GstBaseSink * bsink, GstBuffer * buf,
1327 GstClockTime * start, GstClockTime * end)
1329 GstGLImageSink *glimagesink;
1331 glimagesink = GST_GLIMAGE_SINK (bsink);
1333 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
1334 *start = GST_BUFFER_TIMESTAMP (buf);
1335 if (GST_BUFFER_DURATION_IS_VALID (buf))
1336 *end = *start + GST_BUFFER_DURATION (buf);
1338 if (GST_VIDEO_INFO_FPS_N (&glimagesink->out_info) > 0) {
1340 gst_util_uint64_scale_int (GST_SECOND,
1341 GST_VIDEO_INFO_FPS_D (&glimagesink->out_info),
1342 GST_VIDEO_INFO_FPS_N (&glimagesink->out_info));
1349 gst_glimage_sink_get_caps (GstBaseSink * bsink, GstCaps * filter)
1351 GstCaps *tmp = NULL;
1352 GstCaps *result = NULL;
1354 tmp = gst_pad_get_pad_template_caps (GST_BASE_SINK_PAD (bsink));
1357 GST_DEBUG_OBJECT (bsink, "intersecting with filter caps %" GST_PTR_FORMAT,
1360 result = gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
1361 gst_caps_unref (tmp);
1366 result = gst_gl_overlay_compositor_add_caps (result);
1368 GST_DEBUG_OBJECT (bsink, "returning caps: %" GST_PTR_FORMAT, result);
1374 configure_display_from_info (GstGLImageSink * glimage_sink,
1375 GstVideoInfo * vinfo)
1381 gint display_par_n, display_par_d;
1382 guint display_ratio_num, display_ratio_den;
1384 width = GST_VIDEO_INFO_WIDTH (vinfo);
1385 height = GST_VIDEO_INFO_HEIGHT (vinfo);
1387 par_n = GST_VIDEO_INFO_PAR_N (vinfo);
1388 par_d = GST_VIDEO_INFO_PAR_D (vinfo);
1393 /* get display's PAR */
1394 if (glimage_sink->par_n != 0 && glimage_sink->par_d != 0) {
1395 display_par_n = glimage_sink->par_n;
1396 display_par_d = glimage_sink->par_d;
1402 ok = gst_video_calculate_display_ratio (&display_ratio_num,
1403 &display_ratio_den, width, height, par_n, par_d, display_par_n,
1409 GST_TRACE ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n,
1412 if (height % display_ratio_den == 0) {
1413 GST_DEBUG ("keeping video height");
1414 GST_VIDEO_SINK_WIDTH (glimage_sink) = (guint)
1415 gst_util_uint64_scale_int (height, display_ratio_num,
1417 GST_VIDEO_SINK_HEIGHT (glimage_sink) = height;
1418 } else if (width % display_ratio_num == 0) {
1419 GST_DEBUG ("keeping video width");
1420 GST_VIDEO_SINK_WIDTH (glimage_sink) = width;
1421 GST_VIDEO_SINK_HEIGHT (glimage_sink) = (guint)
1422 gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
1424 GST_DEBUG ("approximating while keeping video height");
1425 GST_VIDEO_SINK_WIDTH (glimage_sink) = (guint)
1426 gst_util_uint64_scale_int (height, display_ratio_num,
1428 GST_VIDEO_SINK_HEIGHT (glimage_sink) = height;
1430 GST_DEBUG ("scaling to %dx%d", GST_VIDEO_SINK_WIDTH (glimage_sink),
1431 GST_VIDEO_SINK_HEIGHT (glimage_sink));
1437 _mview_modes_are_equal (GstVideoMultiviewMode a, GstVideoMultiviewMode b)
1441 if (a == GST_VIDEO_MULTIVIEW_MODE_NONE && b == GST_VIDEO_MULTIVIEW_MODE_MONO)
1443 if (a == GST_VIDEO_MULTIVIEW_MODE_MONO && b == GST_VIDEO_MULTIVIEW_MODE_NONE)
1449 /* Called with GST_GLIMAGE_SINK lock held, to
1450 * copy in_info to out_info and update out_caps */
1452 update_output_format (GstGLImageSink * glimage_sink)
1454 GstVideoInfo *out_info = &glimage_sink->out_info;
1455 GstVideoMultiviewMode mv_mode;
1456 GstGLWindow *window = NULL;
1457 GstGLTextureTarget previous_target;
1459 const gchar *target_str;
1463 *out_info = glimage_sink->in_info;
1464 previous_target = glimage_sink->texture_target;
1466 mv_mode = GST_VIDEO_INFO_MULTIVIEW_MODE (&glimage_sink->in_info);
1468 if (!_mview_modes_are_equal (glimage_sink->mview_output_mode, mv_mode)) {
1469 /* Input is multiview, and output wants a conversion - configure 3d converter now,
1470 * otherwise defer it until either the caps or the 3D output mode changes */
1471 gst_video_multiview_video_info_change_mode (out_info,
1472 glimage_sink->mview_output_mode, glimage_sink->mview_output_flags);
1474 if (glimage_sink->convert_views == NULL) {
1475 glimage_sink->convert_views = gst_gl_view_convert_new ();
1476 gst_gl_view_convert_set_context (glimage_sink->convert_views,
1477 glimage_sink->context);
1480 if (glimage_sink->convert_views) {
1481 gst_object_unref (glimage_sink->convert_views);
1482 glimage_sink->convert_views = NULL;
1486 ret = configure_display_from_info (glimage_sink, out_info);
1488 if (glimage_sink->convert_views) {
1492 /* Match actual output window size for pixel-aligned output,
1493 * even though we can't necessarily match the starting left/right
1494 * view parity properly for line-by-line modes, because that
1495 * depends on the window being placed correctly.
1496 * FIXME: Should this rescaling be configurable? */
1497 if (glimage_sink->display_rect.w > 0 && glimage_sink->display_rect.h > 0) {
1498 new_w = glimage_sink->display_rect.w;
1499 new_h = glimage_sink->display_rect.h;
1500 if (new_w != out_info->width || new_h != out_info->height) {
1501 /* Recalculate PAR if rescaling */
1502 gint from_dar_n, from_dar_d;
1503 if (!gst_util_fraction_multiply (out_info->width, out_info->height,
1504 out_info->par_n, out_info->par_d, &from_dar_n,
1506 !gst_util_fraction_multiply (from_dar_n, from_dar_d, new_h, new_w,
1508 par_n = glimage_sink->par_n;
1509 par_d = glimage_sink->par_d;
1511 out_info->par_n = par_n;
1512 out_info->par_d = par_d;
1513 out_info->width = new_w;
1514 out_info->height = new_h;
1517 GST_LOG_OBJECT (glimage_sink, "Set 3D output scale to %dx%d PAR %d/%d",
1518 out_info->width, out_info->height, out_info->par_n, out_info->par_d);
1522 s = gst_caps_get_structure (glimage_sink->in_caps, 0);
1523 target_str = gst_structure_get_string (s, "texture-target");
1526 target_str = GST_GL_TEXTURE_TARGET_2D_STR;
1528 glimage_sink->texture_target = gst_gl_texture_target_from_string (target_str);
1529 if (!glimage_sink->texture_target)
1532 out_caps = gst_video_info_to_caps (out_info);
1533 gst_caps_set_features (out_caps, 0,
1534 gst_caps_features_from_string (GST_CAPS_FEATURE_MEMORY_GL_MEMORY));
1535 gst_caps_set_simple (out_caps, "texture-target", G_TYPE_STRING,
1538 if (glimage_sink->convert_views) {
1539 gst_caps_set_simple (out_caps, "texture-target", G_TYPE_STRING,
1540 GST_GL_TEXTURE_TARGET_2D_STR, NULL);
1542 glimage_sink->texture_target = GST_GL_TEXTURE_TARGET_2D;
1544 GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
1545 gst_gl_view_convert_set_caps (glimage_sink->convert_views,
1546 glimage_sink->in_caps, out_caps);
1547 g_object_set (glimage_sink->convert_views, "downmix-mode",
1548 glimage_sink->mview_downmix_mode, NULL);
1549 GST_GLIMAGE_SINK_LOCK (glimage_sink);
1552 if (glimage_sink->out_caps)
1553 gst_caps_unref (glimage_sink->out_caps);
1554 glimage_sink->out_caps = out_caps;
1556 if (previous_target != GST_GL_TEXTURE_TARGET_NONE &&
1557 glimage_sink->texture_target != previous_target) {
1558 /* regenerate the shader for the changed target */
1559 GstGLWindow *window = gst_gl_context_get_window (glimage_sink->context);
1560 gst_gl_window_send_message (window,
1561 GST_GL_WINDOW_CB (gst_glimage_sink_cleanup_glthread), glimage_sink);
1562 gst_object_unref (window);
1565 glimage_sink->output_mode_changed = FALSE;
1567 if (glimage_sink->context)
1568 window = gst_gl_context_get_window (glimage_sink->context);
1570 gst_gl_window_queue_resize (window);
1571 gst_object_unref (window);
1578 gst_glimage_sink_set_caps (GstBaseSink * bsink, GstCaps * caps)
1580 GstGLImageSink *glimage_sink;
1584 GST_DEBUG_OBJECT (bsink, "set caps with %" GST_PTR_FORMAT, caps);
1586 glimage_sink = GST_GLIMAGE_SINK (bsink);
1588 ok = gst_video_info_from_caps (&vinfo, caps);
1592 if (!_ensure_gl_setup (glimage_sink))
1595 GST_GLIMAGE_SINK_LOCK (glimage_sink);
1596 if (glimage_sink->in_caps)
1597 gst_caps_unref (glimage_sink->in_caps);
1598 glimage_sink->in_caps = gst_caps_ref (caps);
1599 glimage_sink->in_info = vinfo;
1600 ok = update_output_format (glimage_sink);
1602 GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
1607 /* Take the input_buffer and run it through 3D conversion if needed.
1608 * Called with glimagesink lock, but might drop it temporarily */
1610 prepare_next_buffer (GstGLImageSink * glimage_sink)
1612 GstBuffer *in_buffer, *next_buffer, *old_buffer;
1613 GstBuffer *in_buffer2 = NULL, *next_buffer2 = NULL, *old_buffer2;
1614 GstBuffer *next_sync = NULL, *old_sync;
1615 GstGLSyncMeta *sync_meta;
1616 GstVideoFrame gl_frame;
1617 GstGLViewConvert *convert_views = NULL;
1620 if (glimage_sink->input_buffer == NULL)
1621 return TRUE; /* No input buffer to process */
1623 if (GST_VIDEO_INFO_MULTIVIEW_MODE (&glimage_sink->in_info) ==
1624 GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME) {
1625 if (glimage_sink->input_buffer2 == NULL)
1626 return TRUE; /* Need 2nd input buffer to process */
1627 in_buffer2 = gst_buffer_ref (glimage_sink->input_buffer2);
1630 in_buffer = gst_buffer_ref (glimage_sink->input_buffer);
1631 if (glimage_sink->convert_views &&
1632 (GST_VIDEO_INFO_MULTIVIEW_MODE (&glimage_sink->in_info) !=
1633 GST_VIDEO_INFO_MULTIVIEW_MODE (&glimage_sink->out_info) ||
1634 GST_VIDEO_INFO_MULTIVIEW_FLAGS (&glimage_sink->in_info) !=
1635 GST_VIDEO_INFO_MULTIVIEW_FLAGS (&glimage_sink->out_info)))
1636 convert_views = gst_object_ref (glimage_sink->convert_views);
1638 GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
1640 if (convert_views) {
1641 info = &glimage_sink->out_info;
1643 if (gst_gl_view_convert_submit_input_buffer (glimage_sink->convert_views,
1644 GST_BUFFER_IS_DISCONT (in_buffer), in_buffer) != GST_FLOW_OK) {
1645 gst_buffer_replace (&in_buffer2, NULL);
1649 if (gst_gl_view_convert_submit_input_buffer (glimage_sink->convert_views,
1650 GST_BUFFER_IS_DISCONT (in_buffer2), in_buffer2) != GST_FLOW_OK) {
1655 if (gst_gl_view_convert_get_output (glimage_sink->convert_views,
1656 &next_buffer) != GST_FLOW_OK)
1658 if (GST_VIDEO_INFO_MULTIVIEW_MODE (info) ==
1659 GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME) {
1660 if (gst_gl_view_convert_get_output (glimage_sink->convert_views,
1661 &next_buffer2) != GST_FLOW_OK)
1664 gst_object_unref (convert_views);
1665 convert_views = NULL;
1667 if (next_buffer == NULL) {
1668 /* Not ready to paint a buffer yet */
1669 GST_GLIMAGE_SINK_LOCK (glimage_sink);
1673 next_buffer = in_buffer;
1674 info = &glimage_sink->in_info;
1677 if (!glimage_sink->overlay_compositor) {
1678 if (!(glimage_sink->overlay_compositor =
1679 gst_gl_overlay_compositor_new (glimage_sink->context))) {
1680 gst_buffer_unref (next_buffer);
1685 gst_gl_overlay_compositor_upload_overlays (glimage_sink->overlay_compositor,
1688 sync_meta = gst_buffer_get_gl_sync_meta (next_buffer);
1691 next_sync = gst_buffer_new ();
1692 sync_meta = gst_buffer_add_gl_sync_meta (glimage_sink->context, next_sync);
1693 gst_gl_sync_meta_set_sync_point (sync_meta, glimage_sink->context);
1696 /* in_buffer invalid now */
1697 if (!gst_video_frame_map (&gl_frame, info, next_buffer,
1698 GST_MAP_READ | GST_MAP_GL)) {
1699 gst_buffer_unref (next_buffer);
1700 GST_ERROR ("Failed to map video frame.");
1704 GST_GLIMAGE_SINK_LOCK (glimage_sink);
1705 glimage_sink->next_tex = *(guint *) gl_frame.data[0];
1707 old_buffer = glimage_sink->next_buffer;
1708 glimage_sink->next_buffer = next_buffer;
1709 old_buffer2 = glimage_sink->next_buffer2;
1710 glimage_sink->next_buffer2 = next_buffer2;
1712 old_sync = glimage_sink->next_sync;
1713 glimage_sink->next_sync = next_sync;
1714 glimage_sink->next_sync_meta = sync_meta;
1716 /* Need to drop the lock again, to avoid a deadlock if we're
1717 * dropping the last ref on this buffer and it goes back to our
1719 GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
1722 gst_buffer_unref (old_buffer);
1724 gst_buffer_unref (old_buffer2);
1726 gst_buffer_unref (old_sync);
1727 gst_video_frame_unmap (&gl_frame);
1729 GST_GLIMAGE_SINK_LOCK (glimage_sink);
1735 gst_object_unref (convert_views);
1736 GST_GLIMAGE_SINK_LOCK (glimage_sink);
1740 static GstFlowReturn
1741 gst_glimage_sink_prepare (GstBaseSink * bsink, GstBuffer * buf)
1743 GstGLImageSink *glimage_sink;
1744 GstGLSyncMeta *sync_meta;
1746 GstBuffer *old_input;
1748 glimage_sink = GST_GLIMAGE_SINK (bsink);
1750 GST_TRACE ("preparing buffer:%p", buf);
1752 if (GST_VIDEO_SINK_WIDTH (glimage_sink) < 1 ||
1753 GST_VIDEO_SINK_HEIGHT (glimage_sink) < 1) {
1754 return GST_FLOW_NOT_NEGOTIATED;
1757 if (!_ensure_gl_setup (glimage_sink))
1758 return GST_FLOW_NOT_NEGOTIATED;
1760 sync_meta = gst_buffer_get_gl_sync_meta (buf);
1762 gst_gl_sync_meta_wait (sync_meta, glimage_sink->context);
1764 GST_GLIMAGE_SINK_LOCK (glimage_sink);
1765 if (glimage_sink->window_resized) {
1766 glimage_sink->window_resized = FALSE;
1767 GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
1768 GST_DEBUG_OBJECT (glimage_sink, "Sending reconfigure event on sinkpad.");
1769 gst_pad_push_event (GST_BASE_SINK (glimage_sink)->sinkpad,
1770 gst_event_new_reconfigure ());
1771 GST_GLIMAGE_SINK_LOCK (glimage_sink);
1774 target = &glimage_sink->input_buffer;
1775 if (GST_VIDEO_INFO_MULTIVIEW_MODE (&glimage_sink->in_info) ==
1776 GST_VIDEO_MULTIVIEW_MODE_FRAME_BY_FRAME &&
1777 !GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_BUFFER_FLAG_FIRST_IN_BUNDLE)) {
1778 target = &glimage_sink->input_buffer2;
1780 old_input = *target;
1781 *target = gst_buffer_ref (buf);
1783 if (glimage_sink->output_mode_changed)
1784 update_output_format (glimage_sink);
1786 if (!prepare_next_buffer (glimage_sink)) {
1787 GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
1789 gst_buffer_unref (old_input);
1790 goto convert_views_failed;
1792 GST_GLIMAGE_SINK_UNLOCK (glimage_sink);
1795 gst_buffer_unref (old_input);
1797 if (glimage_sink->window_id != glimage_sink->new_window_id) {
1798 GstGLWindow *window = gst_gl_context_get_window (glimage_sink->context);
1800 glimage_sink->window_id = glimage_sink->new_window_id;
1801 gst_gl_window_set_window_handle (window, glimage_sink->window_id);
1803 gst_object_unref (window);
1807 convert_views_failed:
1809 GST_ELEMENT_ERROR (glimage_sink, RESOURCE, NOT_FOUND,
1810 ("%s", "Failed to convert multiview video buffer"), (NULL));
1811 return GST_FLOW_ERROR;
1815 static GstFlowReturn
1816 gst_glimage_sink_show_frame (GstVideoSink * vsink, GstBuffer * buf)
1818 GstGLImageSink *glimage_sink;
1820 GST_TRACE ("rendering buffer:%p", buf);
1822 glimage_sink = GST_GLIMAGE_SINK (vsink);
1824 GST_TRACE ("redisplay texture:%u of size:%ux%u, window size:%ux%u",
1825 glimage_sink->next_tex, GST_VIDEO_INFO_WIDTH (&glimage_sink->out_info),
1826 GST_VIDEO_INFO_HEIGHT (&glimage_sink->out_info),
1827 GST_VIDEO_SINK_WIDTH (glimage_sink),
1828 GST_VIDEO_SINK_HEIGHT (glimage_sink));
1830 /* Ask the underlying window to redraw its content */
1831 if (!gst_glimage_sink_redisplay (glimage_sink))
1832 goto redisplay_failed;
1834 GST_TRACE ("post redisplay");
1836 if (g_atomic_int_get (&glimage_sink->to_quit) != 0) {
1837 GST_ELEMENT_ERROR (glimage_sink, RESOURCE, NOT_FOUND,
1838 ("%s", "Quit requested"), (NULL));
1839 return GST_FLOW_ERROR;
1847 GST_ELEMENT_ERROR (glimage_sink, RESOURCE, NOT_FOUND,
1848 ("%s", "Window redisplay failed"), (NULL));
1849 return GST_FLOW_ERROR;
1854 gst_glimage_sink_video_overlay_init (GstVideoOverlayInterface * iface)
1856 iface->set_window_handle = gst_glimage_sink_set_window_handle;
1857 iface->set_render_rectangle = gst_glimage_sink_set_render_rectangle;
1858 iface->handle_events = gst_glimage_sink_handle_events;
1859 iface->expose = gst_glimage_sink_expose;
1863 gst_glimage_sink_set_window_handle (GstVideoOverlay * overlay, guintptr id)
1865 GstGLImageSink *glimage_sink = GST_GLIMAGE_SINK (overlay);
1866 guintptr window_id = (guintptr) id;
1868 g_return_if_fail (GST_IS_GLIMAGE_SINK (overlay));
1870 GST_DEBUG ("set_xwindow_id %" G_GUINT64_FORMAT, (guint64) window_id);
1872 glimage_sink->new_window_id = window_id;
1877 gst_glimage_sink_expose (GstVideoOverlay * overlay)
1879 GstGLImageSink *glimage_sink = GST_GLIMAGE_SINK (overlay);
1881 /* redisplay opengl scene */
1882 if (glimage_sink->display) {
1883 if (glimage_sink->window_id
1884 && glimage_sink->window_id != glimage_sink->new_window_id) {
1885 GstGLWindow *window = gst_gl_context_get_window (glimage_sink->context);
1887 glimage_sink->window_id = glimage_sink->new_window_id;
1888 gst_gl_window_set_window_handle (window, glimage_sink->window_id);
1890 gst_object_unref (window);
1893 gst_glimage_sink_redisplay (glimage_sink);
1898 gst_glimage_sink_handle_events (GstVideoOverlay * overlay,
1899 gboolean handle_events)
1901 GstGLImageSink *glimage_sink = GST_GLIMAGE_SINK (overlay);
1903 glimage_sink->handle_events = handle_events;
1904 if (G_LIKELY (glimage_sink->context)) {
1905 GstGLWindow *window;
1906 window = gst_gl_context_get_window (glimage_sink->context);
1907 gst_gl_window_handle_events (window, handle_events);
1908 gst_object_unref (window);
1913 gst_glimage_sink_set_render_rectangle (GstVideoOverlay * overlay,
1914 gint x, gint y, gint width, gint height)
1916 GstGLImageSink *glimage_sink = GST_GLIMAGE_SINK (overlay);
1918 if (G_LIKELY (glimage_sink->context)) {
1919 GstGLWindow *window;
1920 window = gst_gl_context_get_window (glimage_sink->context);
1921 gst_gl_window_set_render_rectangle (window, x, y, width, height);
1922 gst_object_unref (window);
1925 glimage_sink->x = x;
1926 glimage_sink->y = y;
1927 glimage_sink->width = width;
1928 glimage_sink->height = height;
1932 gst_glimage_sink_propose_allocation (GstBaseSink * bsink, GstQuery * query)
1934 GstGLImageSink *glimage_sink = GST_GLIMAGE_SINK (bsink);
1935 GstStructure *config;
1937 GstBufferPool *pool = NULL;
1941 GstStructure *allocation_meta = NULL;
1943 if (!_ensure_gl_setup (glimage_sink))
1946 gst_query_parse_allocation (query, &caps, &need_pool);
1951 if (!gst_video_info_from_caps (&info, caps))
1954 /* the normal size of a frame */
1958 GST_DEBUG_OBJECT (glimage_sink, "create new pool");
1960 pool = gst_gl_buffer_pool_new (glimage_sink->context);
1961 config = gst_buffer_pool_get_config (pool);
1962 gst_buffer_pool_config_set_params (config, caps, size, 0, 0);
1963 gst_buffer_pool_config_add_option (config,
1964 GST_BUFFER_POOL_OPTION_GL_SYNC_META);
1966 if (!gst_buffer_pool_set_config (pool, config)) {
1967 g_object_unref (pool);
1972 /* we need at least 2 buffer because we hold on to the last one */
1973 gst_query_add_allocation_pool (query, pool, size, 2, 0);
1975 g_object_unref (pool);
1977 if (glimage_sink->context->gl_vtable->FenceSync)
1978 gst_query_add_allocation_meta (query, GST_GL_SYNC_META_API_TYPE, 0);
1980 if (glimage_sink->window_width != 0 && glimage_sink->window_height != 0) {
1982 gst_structure_new ("GstVideoOverlayCompositionMeta",
1983 "width", G_TYPE_UINT, glimage_sink->window_width,
1984 "height", G_TYPE_UINT, glimage_sink->window_height, NULL);
1985 GST_DEBUG ("sending alloc query with size %dx%d",
1986 glimage_sink->window_width, glimage_sink->window_height);
1989 gst_query_add_allocation_meta (query,
1990 GST_VIDEO_OVERLAY_COMPOSITION_META_API_TYPE, allocation_meta);
1991 gst_query_add_allocation_meta (query,
1992 GST_VIDEO_AFFINE_TRANSFORMATION_META_API_TYPE, 0);
1994 if (allocation_meta)
1995 gst_structure_free (allocation_meta);
2002 GST_WARNING_OBJECT (bsink, "no caps specified");
2007 GST_WARNING_OBJECT (bsink, "invalid caps specified");
2012 GST_WARNING_OBJECT (bsink, "failed setting config");
2018 static const GLfloat vertices[] = {
2019 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
2020 -1.0f, 1.0f, 0.0f, 0.0f, 0.0f,
2021 -1.0f, -1.0f, 0.0f, 0.0f, 1.0f,
2022 1.0f, -1.0f, 0.0f, 1.0f, 1.0f
2025 static const GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
2029 _bind_buffer (GstGLImageSink * gl_sink)
2031 const GstGLFuncs *gl = gl_sink->context->gl_vtable;
2033 gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, gl_sink->vbo_indices);
2034 gl->BindBuffer (GL_ARRAY_BUFFER, gl_sink->vertex_buffer);
2036 /* Load the vertex position */
2037 gl->VertexAttribPointer (gl_sink->attr_position, 3, GL_FLOAT, GL_FALSE,
2038 5 * sizeof (GLfloat), (void *) 0);
2040 /* Load the texture coordinate */
2041 gl->VertexAttribPointer (gl_sink->attr_texture, 2, GL_FLOAT, GL_FALSE,
2042 5 * sizeof (GLfloat), (void *) (3 * sizeof (GLfloat)));
2044 gl->EnableVertexAttribArray (gl_sink->attr_position);
2045 gl->EnableVertexAttribArray (gl_sink->attr_texture);
2049 _unbind_buffer (GstGLImageSink * gl_sink)
2051 const GstGLFuncs *gl = gl_sink->context->gl_vtable;
2053 gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
2054 gl->BindBuffer (GL_ARRAY_BUFFER, 0);
2056 gl->DisableVertexAttribArray (gl_sink->attr_position);
2057 gl->DisableVertexAttribArray (gl_sink->attr_texture);
2060 /* Called in the gl thread */
2062 gst_glimage_sink_thread_init_redisplay (GstGLImageSink * gl_sink)
2064 const GstGLFuncs *gl = gl_sink->context->gl_vtable;
2065 GError *error = NULL;
2066 GstGLSLStage *frag_stage, *vert_stage;
2068 vert_stage = gst_glsl_stage_new_with_string (gl_sink->context,
2069 GL_VERTEX_SHADER, GST_GLSL_VERSION_NONE,
2070 GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY,
2071 gst_gl_shader_string_vertex_mat4_vertex_transform);
2072 if (gl_sink->texture_target == GST_GL_TEXTURE_TARGET_EXTERNAL_OES) {
2075 gst_gl_shader_string_fragment_external_oes_get_default
2076 (gl_sink->context, GST_GLSL_VERSION_NONE,
2077 GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY);
2079 frag_stage = gst_glsl_stage_new_with_string (gl_sink->context,
2080 GL_FRAGMENT_SHADER, GST_GLSL_VERSION_NONE,
2081 GST_GLSL_PROFILE_ES | GST_GLSL_PROFILE_COMPATIBILITY, frag_str);
2085 frag_stage = gst_glsl_stage_new_default_fragment (gl_sink->context);
2087 if (!vert_stage || !frag_stage) {
2088 GST_ERROR_OBJECT (gl_sink, "Failed to retrieve fragment shader for "
2091 gst_object_unref (vert_stage);
2093 gst_object_unref (frag_stage);
2094 gst_glimage_sink_cleanup_glthread (gl_sink);
2098 if (!(gl_sink->redisplay_shader =
2099 gst_gl_shader_new_link_with_stages (gl_sink->context, &error,
2100 vert_stage, frag_stage, NULL))) {
2101 GST_ERROR_OBJECT (gl_sink, "Failed to link shader: %s", error->message);
2102 gst_glimage_sink_cleanup_glthread (gl_sink);
2106 gl_sink->attr_position =
2107 gst_gl_shader_get_attribute_location (gl_sink->redisplay_shader,
2109 gl_sink->attr_texture =
2110 gst_gl_shader_get_attribute_location (gl_sink->redisplay_shader,
2113 if (gl->GenVertexArrays) {
2114 gl->GenVertexArrays (1, &gl_sink->vao);
2115 gl->BindVertexArray (gl_sink->vao);
2118 if (!gl_sink->vertex_buffer) {
2119 gl->GenBuffers (1, &gl_sink->vertex_buffer);
2120 gl->BindBuffer (GL_ARRAY_BUFFER, gl_sink->vertex_buffer);
2121 gl->BufferData (GL_ARRAY_BUFFER, 4 * 5 * sizeof (GLfloat), vertices,
2125 if (!gl_sink->vbo_indices) {
2126 gl->GenBuffers (1, &gl_sink->vbo_indices);
2127 gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, gl_sink->vbo_indices);
2128 gl->BufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof (indices), indices,
2132 if (gl->GenVertexArrays) {
2133 _bind_buffer (gl_sink);
2134 gl->BindVertexArray (0);
2137 gl->BindBuffer (GL_ELEMENT_ARRAY_BUFFER, 0);
2138 gl->BindBuffer (GL_ARRAY_BUFFER, 0);
2142 gst_glimage_sink_cleanup_glthread (GstGLImageSink * gl_sink)
2144 const GstGLFuncs *gl = gl_sink->context->gl_vtable;
2146 if (gl_sink->redisplay_shader) {
2147 gst_object_unref (gl_sink->redisplay_shader);
2148 gl_sink->redisplay_shader = NULL;
2152 gl->DeleteVertexArrays (1, &gl_sink->vao);
2156 if (gl_sink->vertex_buffer) {
2157 gl->DeleteBuffers (1, &gl_sink->vertex_buffer);
2158 gl_sink->vertex_buffer = 0;
2161 if (gl_sink->vbo_indices) {
2162 gl->DeleteBuffers (1, &gl_sink->vbo_indices);
2163 gl_sink->vbo_indices = 0;
2166 if (gl_sink->overlay_compositor)
2167 gst_gl_overlay_compositor_free_overlays (gl_sink->overlay_compositor);
2170 /* Called with object lock held */
2172 gst_glimage_sink_on_resize (GstGLImageSink * gl_sink, gint width, gint height)
2174 /* Here gl_sink members (ex:gl_sink->out_info) have a life time of set_caps.
2175 * It means that they cannot change between two set_caps
2177 const GstGLFuncs *gl;
2178 gboolean do_reshape;
2180 GST_DEBUG_OBJECT (gl_sink, "GL Window resized to %ux%u", width, height);
2182 /* check if a client reshape callback is registered */
2183 g_signal_emit (gl_sink, gst_glimage_sink_signals[CLIENT_RESHAPE_SIGNAL], 0,
2184 gl_sink->context, width, height, &do_reshape);
2185 GST_GLIMAGE_SINK_LOCK (gl_sink);
2187 gl = gl_sink->context->gl_vtable;
2188 width = MAX (1, width);
2189 height = MAX (1, height);
2191 /* Check if we would suggest a different width/height now */
2192 gl_sink->window_resized = ((gl_sink->window_width != width)
2193 || (gl_sink->window_height != height))
2194 && (gl_sink->window_width != 0)
2195 && (gl_sink->window_height != 0);
2197 gl_sink->window_width = width;
2198 gl_sink->window_height = height;
2200 gst_gl_insert_debug_marker (gl_sink->context, "%s window resize to %ix%i",
2201 GST_OBJECT_NAME (gl_sink), width, height);
2203 /* default reshape */
2205 if (gl_sink->keep_aspect_ratio) {
2206 GstVideoRectangle src, dst, result;
2210 if (gl_sink->current_rotate_method == GST_GL_ROTATE_METHOD_90R
2211 || gl_sink->current_rotate_method == GST_GL_ROTATE_METHOD_90L
2212 || gl_sink->current_rotate_method == GST_GL_ROTATE_METHOD_FLIP_UL_LR
2213 || gl_sink->current_rotate_method ==
2214 GST_GL_ROTATE_METHOD_FLIP_UR_LL) {
2215 src.h = GST_VIDEO_SINK_WIDTH (gl_sink);
2216 src.w = GST_VIDEO_SINK_HEIGHT (gl_sink);
2218 src.w = GST_VIDEO_SINK_WIDTH (gl_sink);
2219 src.h = GST_VIDEO_SINK_HEIGHT (gl_sink);
2227 gst_video_sink_center_rect (src, dst, &result, TRUE);
2228 gl_sink->output_mode_changed |= (result.w != gl_sink->display_rect.w);
2229 gl_sink->output_mode_changed |= (result.h != gl_sink->display_rect.h);
2230 gl_sink->display_rect = result;
2232 gl_sink->output_mode_changed |= (width != gl_sink->display_rect.w);
2233 gl_sink->output_mode_changed |= (height != gl_sink->display_rect.h);
2235 gl_sink->display_rect.x = 0;
2236 gl_sink->display_rect.y = 0;
2237 gl_sink->display_rect.w = width;
2238 gl_sink->display_rect.h = height;
2241 gl->Viewport (gl_sink->display_rect.x, gl_sink->display_rect.y,
2242 gl_sink->display_rect.w, gl_sink->display_rect.h);
2243 GST_DEBUG_OBJECT (gl_sink, "GL output area now %u,%u %ux%u",
2244 gl_sink->display_rect.x, gl_sink->display_rect.y,
2245 gl_sink->display_rect.w, gl_sink->display_rect.h);
2247 gint viewport_dims[4];
2249 /* save the viewport for use later */
2250 gl->GetIntegerv (GL_VIEWPORT, viewport_dims);
2252 gl_sink->display_rect.x = viewport_dims[0];
2253 gl_sink->display_rect.y = viewport_dims[1];
2254 gl_sink->display_rect.w = viewport_dims[2];
2255 gl_sink->display_rect.h = viewport_dims[3];
2257 GST_GLIMAGE_SINK_UNLOCK (gl_sink);
2261 gst_glimage_sink_on_draw (GstGLImageSink * gl_sink)
2263 /* Here gl_sink members (ex:gl_sink->out_info) have a life time of set_caps.
2264 * It means that they cannot not change between two set_caps as well as
2265 * for the redisplay_texture size.
2266 * Whereas redisplay_texture id changes every sink_render
2269 const GstGLFuncs *gl = NULL;
2270 GstGLWindow *window = NULL;
2271 gboolean do_redisplay = FALSE;
2272 GstSample *sample = NULL;
2273 guint gl_target = gst_gl_texture_target_to_gl (gl_sink->texture_target);
2275 g_return_if_fail (GST_IS_GLIMAGE_SINK (gl_sink));
2277 gl = gl_sink->context->gl_vtable;
2279 GST_GLIMAGE_SINK_LOCK (gl_sink);
2281 /* check if texture is ready for being drawn */
2282 if (!gl_sink->redisplay_texture) {
2283 GST_GLIMAGE_SINK_UNLOCK (gl_sink);
2287 window = gst_gl_context_get_window (gl_sink->context);
2288 window->is_drawing = TRUE;
2291 gst_gl_insert_debug_marker (gl_sink->context, "%s element drawing texture %u",
2292 GST_OBJECT_NAME (gl_sink), gl_sink->redisplay_texture);
2293 GST_TRACE ("redrawing texture:%u", gl_sink->redisplay_texture);
2295 if (gl_sink->stored_sync_meta)
2296 gst_gl_sync_meta_wait (gl_sink->stored_sync_meta,
2297 gst_gl_context_get_current ());
2299 /* make sure that the environment is clean */
2300 gst_gl_context_clear_shader (gl_sink->context);
2301 gl->BindTexture (gl_target, 0);
2303 if (!gst_gl_window_controls_viewport (window)) {
2304 gl->Viewport (gl_sink->display_rect.x, gl_sink->display_rect.y,
2305 gl_sink->display_rect.w, gl_sink->display_rect.h);
2306 GST_DEBUG_OBJECT (gl_sink, "GL output area now %u,%u %ux%u",
2307 gl_sink->display_rect.x, gl_sink->display_rect.y,
2308 gl_sink->display_rect.w, gl_sink->display_rect.h);
2311 sample = gst_sample_new (gl_sink->stored_buffer[0],
2312 gl_sink->out_caps, &GST_BASE_SINK (gl_sink)->segment, NULL);
2313 g_signal_emit (gl_sink, gst_glimage_sink_signals[CLIENT_DRAW_SIGNAL], 0,
2314 gl_sink->context, sample, &do_redisplay);
2315 gst_sample_unref (sample);
2317 if (gl_sink->stored_buffer[1]) {
2318 sample = gst_sample_new (gl_sink->stored_buffer[1],
2319 gl_sink->out_caps, &GST_BASE_SINK (gl_sink)->segment, NULL);
2320 g_signal_emit (gl_sink, gst_glimage_sink_signals[CLIENT_DRAW_SIGNAL], 0,
2321 gl_sink->context, sample, &do_redisplay);
2322 gst_sample_unref (sample);
2325 if (!do_redisplay) {
2326 gfloat alpha = gl_sink->ignore_alpha ? 1.0f : 0.0f;
2328 gl->ClearColor (0.0, 0.0, 0.0, alpha);
2329 gl->Clear (GL_COLOR_BUFFER_BIT);
2331 if (gl_sink->ignore_alpha) {
2332 gl->BlendColor (0.0, 0.0, 0.0, alpha);
2333 if (gl->BlendFuncSeparate) {
2334 gl->BlendFuncSeparate (GL_SRC_ALPHA, GL_CONSTANT_COLOR, GL_ONE,
2335 GL_ONE_MINUS_SRC_ALPHA);
2337 /* we don't have separate blend modes, perform something close to
2338 * correct instead */
2339 gl->BlendFunc (GL_SRC_ALPHA, GL_CONSTANT_COLOR);
2341 gl->BlendEquation (GL_FUNC_ADD);
2342 gl->Enable (GL_BLEND);
2345 gst_gl_shader_use (gl_sink->redisplay_shader);
2347 if (gl->GenVertexArrays)
2348 gl->BindVertexArray (gl_sink->vao);
2349 _bind_buffer (gl_sink);
2351 gl->ActiveTexture (GL_TEXTURE0);
2352 gl->BindTexture (gl_target, gl_sink->redisplay_texture);
2353 gst_gl_shader_set_uniform_1i (gl_sink->redisplay_shader, "tex", 0);
2355 GstVideoAffineTransformationMeta *af_meta;
2359 gst_buffer_get_video_affine_transformation_meta
2360 (gl_sink->stored_buffer[0]);
2362 if (gl_sink->transform_matrix) {
2365 gst_gl_get_affine_transformation_meta_as_ndc_ext (af_meta, tmp);
2366 gst_gl_multiply_matrix4 (tmp, gl_sink->transform_matrix, matrix);
2368 gst_gl_get_affine_transformation_meta_as_ndc_ext (af_meta, matrix);
2371 gst_gl_shader_set_uniform_matrix_4fv (gl_sink->redisplay_shader,
2372 "u_transformation", 1, FALSE, matrix);
2375 gl->DrawElements (GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
2377 gl->BindTexture (gl_target, 0);
2378 gst_gl_context_clear_shader (gl_sink->context);
2380 if (gl->GenVertexArrays)
2381 gl->BindVertexArray (0);
2383 _unbind_buffer (gl_sink);
2385 if (gl_sink->ignore_alpha)
2386 gl->Disable (GL_BLEND);
2388 gst_gl_overlay_compositor_draw_overlays (gl_sink->overlay_compositor);
2390 /* end default opengl scene */
2391 window->is_drawing = FALSE;
2392 gst_object_unref (window);
2394 GST_GLIMAGE_SINK_UNLOCK (gl_sink);
2398 gst_glimage_sink_on_close (GstGLImageSink * gl_sink)
2400 GstGLWindow *window;
2402 GST_WARNING_OBJECT (gl_sink, "Output window was closed");
2404 window = gst_gl_context_get_window (gl_sink->context);
2406 if (gl_sink->key_sig_id)
2407 g_signal_handler_disconnect (window, gl_sink->key_sig_id);
2408 gl_sink->key_sig_id = 0;
2409 if (gl_sink->mouse_sig_id)
2410 g_signal_handler_disconnect (window, gl_sink->mouse_sig_id);
2411 gl_sink->mouse_sig_id = 0;
2412 if (gl_sink->mouse_scroll_sig_id)
2413 g_signal_handler_disconnect (window, gl_sink->mouse_scroll_sig_id);
2414 gl_sink->mouse_scroll_sig_id = 0;
2416 g_atomic_int_set (&gl_sink->to_quit, 1);
2418 gst_object_unref (window);
2422 gst_glimage_sink_redisplay (GstGLImageSink * gl_sink)
2424 GstGLWindow *window;
2425 GstBuffer *old_stored_buffer[2], *old_sync;
2428 window = gst_gl_context_get_window (gl_sink->context);
2433 g_signal_handler_find (GST_ELEMENT_PARENT (gl_sink), G_SIGNAL_MATCH_ID,
2434 gst_gl_image_sink_bin_signals[SIGNAL_BIN_CLIENT_DRAW], 0,
2437 if (G_UNLIKELY (!gl_sink->redisplay_shader) && (!handler_id
2438 || !gl_sink->other_context)) {
2439 gst_gl_window_send_message (window,
2440 GST_GL_WINDOW_CB (gst_glimage_sink_thread_init_redisplay), gl_sink);
2442 /* if the shader is still null it means it failed to be useable */
2443 if (G_UNLIKELY (!gl_sink->redisplay_shader)) {
2444 gst_object_unref (window);
2448 gst_gl_window_set_preferred_size (window, GST_VIDEO_SINK_WIDTH (gl_sink),
2449 GST_VIDEO_SINK_HEIGHT (gl_sink));
2450 gst_gl_window_show (window);
2453 /* Recreate the output texture if needed */
2454 GST_GLIMAGE_SINK_LOCK (gl_sink);
2455 if (gl_sink->window_resized) {
2456 gl_sink->window_resized = FALSE;
2457 GST_GLIMAGE_SINK_UNLOCK (gl_sink);
2458 GST_DEBUG_OBJECT (gl_sink, "Sending reconfigure event on sinkpad.");
2459 gst_pad_push_event (GST_BASE_SINK (gl_sink)->sinkpad,
2460 gst_event_new_reconfigure ());
2461 GST_GLIMAGE_SINK_LOCK (gl_sink);
2464 if (gl_sink->output_mode_changed && gl_sink->input_buffer != NULL) {
2465 GST_DEBUG ("Recreating output after mode/size change");
2466 update_output_format (gl_sink);
2467 prepare_next_buffer (gl_sink);
2470 if (gl_sink->next_buffer == NULL) {
2471 /* Nothing to display yet */
2472 GST_GLIMAGE_SINK_UNLOCK (gl_sink);
2473 gst_object_unref (window);
2477 /* Avoid to release the texture while drawing */
2478 gl_sink->redisplay_texture = gl_sink->next_tex;
2479 old_stored_buffer[0] = gl_sink->stored_buffer[0];
2480 old_stored_buffer[1] = gl_sink->stored_buffer[1];
2481 gl_sink->stored_buffer[0] = gst_buffer_ref (gl_sink->next_buffer);
2482 if (gl_sink->next_buffer2)
2483 gl_sink->stored_buffer[1] = gst_buffer_ref (gl_sink->next_buffer2);
2485 gl_sink->stored_buffer[1] = NULL;
2487 old_sync = gl_sink->stored_sync;
2488 if (gl_sink->next_sync)
2489 gl_sink->stored_sync = gst_buffer_ref (gl_sink->next_sync);
2491 gl_sink->stored_sync = NULL;
2492 gl_sink->stored_sync_meta = gl_sink->next_sync_meta;
2493 GST_GLIMAGE_SINK_UNLOCK (gl_sink);
2495 gst_buffer_replace (old_stored_buffer, NULL);
2496 gst_buffer_replace (old_stored_buffer + 1, NULL);
2498 gst_buffer_unref (old_sync);
2500 /* Drawing is asynchronous: gst_gl_window_draw is not blocking
2501 * It means that it does not wait for stuff to be executed in other threads
2503 gst_gl_window_draw (window);
2504 gst_object_unref (window);