3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
27 #include "gtkgstwidget.h"
28 #include <gst/video/video.h>
31 * SECTION:gtkgstwidget
32 * @short_description: a #GtkWidget that renders GStreamer video #GstBuffers
33 * @see_also: #GtkDrawingArea, #GstBuffer
35 * #GtkGstWidget is an #GtkWidget that renders GStreamer video buffers.
38 G_DEFINE_TYPE (GtkGstWidget, gtk_gst_widget, GTK_TYPE_DRAWING_AREA);
40 #define GTK_GST_WIDGET_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), \
41 GTK_TYPE_GST_WIDGET, GtkGstWidgetPrivate))
43 #define DEFAULT_FORCE_ASPECT_RATIO TRUE
44 #define DEFAULT_PAR_N 0
45 #define DEFAULT_PAR_D 1
46 #define DEFAULT_IGNORE_ALPHA TRUE
51 PROP_FORCE_ASPECT_RATIO,
52 PROP_PIXEL_ASPECT_RATIO,
56 struct _GtkGstWidgetPrivate
61 gboolean force_aspect_ratio;
63 gboolean ignore_alpha;
75 gtk_gst_widget_get_preferred_width (GtkWidget * widget, gint * min,
78 GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
79 gint video_width = GST_VIDEO_INFO_WIDTH (&gst_widget->priv->v_info);
81 if (!gst_widget->priv->negotiated)
87 *natural = video_width;
91 gtk_gst_widget_get_preferred_height (GtkWidget * widget, gint * min,
94 GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
95 gint video_height = GST_VIDEO_INFO_HEIGHT (&gst_widget->priv->v_info);
97 if (!gst_widget->priv->negotiated)
103 *natural = video_height;
107 gtk_gst_widget_draw (GtkWidget * widget, cairo_t * cr)
109 GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
110 guint widget_width, widget_height;
111 cairo_surface_t *surface;
114 widget_width = gtk_widget_get_allocated_width (widget);
115 widget_height = gtk_widget_get_allocated_height (widget);
117 g_mutex_lock (&gst_widget->priv->lock);
119 /* failed to map the video frame */
120 if (gst_widget->priv->negotiated && gst_widget->priv->buffer
121 && gst_video_frame_map (&frame, &gst_widget->priv->v_info,
122 gst_widget->priv->buffer, GST_MAP_READ)) {
123 gdouble scale_x = (gdouble) widget_width / gst_widget->priv->display_width;
125 (gdouble) widget_height / gst_widget->priv->display_height;
126 GstVideoRectangle result;
128 gst_widget->priv->v_info = frame.info;
130 surface = cairo_image_surface_create_for_data (frame.data[0],
131 CAIRO_FORMAT_ARGB32, frame.info.width, frame.info.height,
132 frame.info.stride[0]);
134 if (gst_widget->priv->force_aspect_ratio) {
135 GstVideoRectangle src, dst;
139 src.w = gst_widget->priv->display_width;
140 src.h = gst_widget->priv->display_height;
144 dst.w = widget_width;
145 dst.h = widget_height;
147 gst_video_sink_center_rect (src, dst, &result, TRUE);
149 scale_x = scale_y = MIN (scale_x, scale_y);
153 result.w = widget_width;
154 result.h = widget_height;
157 if (gst_widget->priv->ignore_alpha) {
158 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
160 gdk_cairo_set_source_rgba (cr, &color);
162 cairo_rectangle (cr, 0, 0, result.x, widget_height);
166 cairo_rectangle (cr, 0, 0, widget_width, result.y);
169 if (result.w < widget_width) {
170 cairo_rectangle (cr, result.x + result.w, 0, widget_width - result.w,
174 if (result.h < widget_height) {
175 cairo_rectangle (cr, 0, result.y + result.h, widget_width,
176 widget_height - result.h);
182 (gdouble) gst_widget->priv->display_width / (gdouble) frame.info.width;
184 (gdouble) gst_widget->priv->display_height /
185 (gdouble) frame.info.height;
187 cairo_translate (cr, result.x, result.y);
188 cairo_scale (cr, scale_x, scale_y);
189 cairo_rectangle (cr, 0, 0, result.w, result.h);
190 cairo_set_source_surface (cr, surface, 0, 0);
193 cairo_surface_destroy (surface);
195 gst_video_frame_unmap (&frame);
199 if (gst_widget->priv->ignore_alpha) {
200 color.red = color.blue = color.green = 0.0;
203 gtk_style_context_get_color (gtk_widget_get_style_context (widget),
204 GTK_STATE_FLAG_NORMAL, &color);
206 gdk_cairo_set_source_rgba (cr, &color);
207 cairo_rectangle (cr, 0, 0, widget_width, widget_height);
211 g_mutex_unlock (&gst_widget->priv->lock);
216 gtk_gst_widget_finalize (GObject * object)
218 GtkGstWidget *widget = GTK_GST_WIDGET_CAST (object);
220 gst_buffer_replace (&widget->priv->buffer, NULL);
221 g_mutex_clear (&widget->priv->lock);
223 G_OBJECT_CLASS (gtk_gst_widget_parent_class)->finalize (object);
227 gtk_gst_widget_set_property (GObject * object, guint prop_id,
228 const GValue * value, GParamSpec * pspec)
230 GtkGstWidget *gtk_widget = GTK_GST_WIDGET (object);
233 case PROP_FORCE_ASPECT_RATIO:
234 gtk_widget->priv->force_aspect_ratio = g_value_get_boolean (value);
236 case PROP_PIXEL_ASPECT_RATIO:
237 gtk_widget->priv->par_n = gst_value_get_fraction_numerator (value);
238 gtk_widget->priv->par_d = gst_value_get_fraction_denominator (value);
240 case PROP_IGNORE_ALPHA:
241 gtk_widget->priv->ignore_alpha = g_value_get_boolean (value);
244 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
250 gtk_gst_widget_get_property (GObject * object, guint prop_id, GValue * value,
253 GtkGstWidget *gtk_widget = GTK_GST_WIDGET (object);
256 case PROP_FORCE_ASPECT_RATIO:
257 g_value_set_boolean (value, gtk_widget->priv->force_aspect_ratio);
259 case PROP_PIXEL_ASPECT_RATIO:
260 gst_value_set_fraction (value, gtk_widget->priv->par_n,
261 gtk_widget->priv->par_d);
262 case PROP_IGNORE_ALPHA:
263 g_value_set_boolean (value, gtk_widget->priv->ignore_alpha);
266 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
272 gtk_gst_widget_class_init (GtkGstWidgetClass * klass)
274 GObjectClass *gobject_klass = (GObjectClass *) klass;
275 GtkWidgetClass *widget_klass = (GtkWidgetClass *) klass;
277 g_type_class_add_private (klass, sizeof (GtkGstWidgetPrivate));
279 gobject_klass->set_property = gtk_gst_widget_set_property;
280 gobject_klass->get_property = gtk_gst_widget_get_property;
281 gobject_klass->finalize = gtk_gst_widget_finalize;
283 g_object_class_install_property (gobject_klass, PROP_FORCE_ASPECT_RATIO,
284 g_param_spec_boolean ("force-aspect-ratio",
285 "Force aspect ratio",
286 "When enabled, scaling will respect original aspect ratio",
287 DEFAULT_FORCE_ASPECT_RATIO,
288 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
290 g_object_class_install_property (gobject_klass, PROP_PIXEL_ASPECT_RATIO,
291 gst_param_spec_fraction ("pixel-aspect-ratio", "Pixel Aspect Ratio",
292 "The pixel aspect ratio of the device", DEFAULT_PAR_N, DEFAULT_PAR_D,
293 G_MAXINT, 1, 1, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
295 g_object_class_install_property (gobject_klass, PROP_IGNORE_ALPHA,
296 g_param_spec_boolean ("ignore-alpha", "Ignore Alpha",
297 "When enabled, alpha will be ignored and converted to black",
298 DEFAULT_IGNORE_ALPHA, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
300 widget_klass->draw = gtk_gst_widget_draw;
301 widget_klass->get_preferred_width = gtk_gst_widget_get_preferred_width;
302 widget_klass->get_preferred_height = gtk_gst_widget_get_preferred_height;
306 gtk_gst_widget_init (GtkGstWidget * widget)
308 widget->priv = GTK_GST_WIDGET_GET_PRIVATE (widget);
310 widget->priv->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
311 widget->priv->par_n = DEFAULT_PAR_N;
312 widget->priv->par_d = DEFAULT_PAR_D;
313 widget->priv->ignore_alpha = DEFAULT_IGNORE_ALPHA;
315 g_mutex_init (&widget->priv->lock);
319 gtk_gst_widget_new (void)
321 return (GtkWidget *) g_object_new (GTK_TYPE_GST_WIDGET, NULL);
325 _queue_draw (GtkGstWidget * widget)
327 gtk_widget_queue_draw (GTK_WIDGET (widget));
329 return G_SOURCE_REMOVE;
333 gtk_gst_widget_set_buffer (GtkGstWidget * widget, GstBuffer * buffer)
335 GMainContext *main_context = g_main_context_default ();
337 g_return_if_fail (GTK_IS_GST_WIDGET (widget));
338 g_return_if_fail (widget->priv->negotiated);
340 g_mutex_lock (&widget->priv->lock);
342 gst_buffer_replace (&widget->priv->buffer, buffer);
344 g_mutex_unlock (&widget->priv->lock);
346 g_main_context_invoke (main_context, (GSourceFunc) _queue_draw, widget);
350 _queue_resize (GtkGstWidget * widget)
352 gtk_widget_queue_resize (GTK_WIDGET (widget));
354 return G_SOURCE_REMOVE;
358 _calculate_par (GtkGstWidget * widget, GstVideoInfo * info)
363 gint display_par_n, display_par_d;
364 guint display_ratio_num, display_ratio_den;
366 width = GST_VIDEO_INFO_WIDTH (info);
367 height = GST_VIDEO_INFO_HEIGHT (info);
369 par_n = GST_VIDEO_INFO_PAR_N (info);
370 par_d = GST_VIDEO_INFO_PAR_D (info);
375 /* get display's PAR */
376 if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
377 display_par_n = widget->priv->par_n;
378 display_par_d = widget->priv->par_d;
384 ok = gst_video_calculate_display_ratio (&display_ratio_num,
385 &display_ratio_den, width, height, par_n, par_d, display_par_n,
391 GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
393 if (height % display_ratio_den == 0) {
394 GST_DEBUG ("keeping video height");
395 widget->priv->display_width = (guint)
396 gst_util_uint64_scale_int (height, display_ratio_num,
398 widget->priv->display_height = height;
399 } else if (width % display_ratio_num == 0) {
400 GST_DEBUG ("keeping video width");
401 widget->priv->display_width = width;
402 widget->priv->display_height = (guint)
403 gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
405 GST_DEBUG ("approximating while keeping video height");
406 widget->priv->display_width = (guint)
407 gst_util_uint64_scale_int (height, display_ratio_num,
409 widget->priv->display_height = height;
411 GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
412 widget->priv->display_height);
418 gtk_gst_widget_set_caps (GtkGstWidget * widget, GstCaps * caps)
420 GMainContext *main_context = g_main_context_default ();
423 g_return_val_if_fail (GTK_IS_GST_WIDGET (widget), FALSE);
424 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
425 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
427 if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
430 if (!gst_video_info_from_caps (&v_info, caps))
433 /* FIXME: support other formats */
434 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
435 g_return_val_if_fail (GST_VIDEO_INFO_FORMAT (&v_info) ==
436 GST_VIDEO_FORMAT_BGRA, FALSE);
438 g_return_val_if_fail (GST_VIDEO_INFO_FORMAT (&v_info) ==
439 GST_VIDEO_FORMAT_ARGB, FALSE);
442 g_mutex_lock (&widget->priv->lock);
444 if (!_calculate_par (widget, &v_info)) {
445 g_mutex_unlock (&widget->priv->lock);
449 gst_caps_replace (&widget->priv->caps, caps);
450 widget->priv->v_info = v_info;
451 widget->priv->negotiated = TRUE;
453 g_mutex_unlock (&widget->priv->lock);
455 gtk_widget_queue_resize (GTK_WIDGET (widget));
457 g_main_context_invoke (main_context, (GSourceFunc) _queue_resize, widget);