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;
73 /* Pending queued idles callback */
79 gtk_gst_widget_get_preferred_width (GtkWidget * widget, gint * min,
82 GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
83 gint video_width = gst_widget->priv->display_width;
85 if (!gst_widget->priv->negotiated)
91 *natural = video_width;
95 gtk_gst_widget_get_preferred_height (GtkWidget * widget, gint * min,
98 GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
99 gint video_height = gst_widget->priv->display_height;
101 if (!gst_widget->priv->negotiated)
107 *natural = video_height;
111 gtk_gst_widget_draw (GtkWidget * widget, cairo_t * cr)
113 GtkGstWidget *gst_widget = (GtkGstWidget *) widget;
114 guint widget_width, widget_height;
115 cairo_surface_t *surface;
118 widget_width = gtk_widget_get_allocated_width (widget);
119 widget_height = gtk_widget_get_allocated_height (widget);
121 g_mutex_lock (&gst_widget->priv->lock);
123 /* failed to map the video frame */
124 if (gst_widget->priv->negotiated && gst_widget->priv->buffer
125 && gst_video_frame_map (&frame, &gst_widget->priv->v_info,
126 gst_widget->priv->buffer, GST_MAP_READ)) {
127 gdouble scale_x = (gdouble) widget_width / gst_widget->priv->display_width;
129 (gdouble) widget_height / gst_widget->priv->display_height;
130 GstVideoRectangle result;
131 cairo_format_t format;
133 gst_widget->priv->v_info = frame.info;
134 if (frame.info.finfo->format == GST_VIDEO_FORMAT_ARGB ||
135 frame.info.finfo->format == GST_VIDEO_FORMAT_BGRA) {
136 format = CAIRO_FORMAT_ARGB32;
138 format = CAIRO_FORMAT_RGB24;
141 surface = cairo_image_surface_create_for_data (frame.data[0],
142 format, frame.info.width, frame.info.height, frame.info.stride[0]);
144 if (gst_widget->priv->force_aspect_ratio) {
145 GstVideoRectangle src, dst;
149 src.w = gst_widget->priv->display_width;
150 src.h = gst_widget->priv->display_height;
154 dst.w = widget_width;
155 dst.h = widget_height;
157 gst_video_sink_center_rect (src, dst, &result, TRUE);
159 scale_x = scale_y = MIN (scale_x, scale_y);
163 result.w = widget_width;
164 result.h = widget_height;
167 if (gst_widget->priv->ignore_alpha) {
168 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
170 gdk_cairo_set_source_rgba (cr, &color);
172 cairo_rectangle (cr, 0, 0, result.x, widget_height);
176 cairo_rectangle (cr, 0, 0, widget_width, result.y);
179 if (result.w < widget_width) {
180 cairo_rectangle (cr, result.x + result.w, 0, widget_width - result.w,
184 if (result.h < widget_height) {
185 cairo_rectangle (cr, 0, result.y + result.h, widget_width,
186 widget_height - result.h);
192 (gdouble) gst_widget->priv->display_width / (gdouble) frame.info.width;
194 (gdouble) gst_widget->priv->display_height /
195 (gdouble) frame.info.height;
197 cairo_translate (cr, result.x, result.y);
198 cairo_scale (cr, scale_x, scale_y);
199 cairo_rectangle (cr, 0, 0, result.w, result.h);
200 cairo_set_source_surface (cr, surface, 0, 0);
203 cairo_surface_destroy (surface);
205 gst_video_frame_unmap (&frame);
209 if (gst_widget->priv->ignore_alpha) {
210 color.red = color.blue = color.green = 0.0;
213 gtk_style_context_get_color (gtk_widget_get_style_context (widget),
214 GTK_STATE_FLAG_NORMAL, &color);
216 gdk_cairo_set_source_rgba (cr, &color);
217 cairo_rectangle (cr, 0, 0, widget_width, widget_height);
221 g_mutex_unlock (&gst_widget->priv->lock);
226 gtk_gst_widget_finalize (GObject * object)
228 GtkGstWidget *widget = GTK_GST_WIDGET_CAST (object);
230 gst_buffer_replace (&widget->priv->buffer, NULL);
231 g_mutex_clear (&widget->priv->lock);
233 if (widget->priv->draw_id)
234 g_source_remove (widget->priv->draw_id);
236 if (widget->priv->resize_id)
237 g_source_remove (widget->priv->resize_id);
239 G_OBJECT_CLASS (gtk_gst_widget_parent_class)->finalize (object);
243 gtk_gst_widget_set_property (GObject * object, guint prop_id,
244 const GValue * value, GParamSpec * pspec)
246 GtkGstWidget *gtk_widget = GTK_GST_WIDGET (object);
249 case PROP_FORCE_ASPECT_RATIO:
250 gtk_widget->priv->force_aspect_ratio = g_value_get_boolean (value);
252 case PROP_PIXEL_ASPECT_RATIO:
253 gtk_widget->priv->par_n = gst_value_get_fraction_numerator (value);
254 gtk_widget->priv->par_d = gst_value_get_fraction_denominator (value);
256 case PROP_IGNORE_ALPHA:
257 gtk_widget->priv->ignore_alpha = g_value_get_boolean (value);
260 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
266 gtk_gst_widget_get_property (GObject * object, guint prop_id, GValue * value,
269 GtkGstWidget *gtk_widget = GTK_GST_WIDGET (object);
272 case PROP_FORCE_ASPECT_RATIO:
273 g_value_set_boolean (value, gtk_widget->priv->force_aspect_ratio);
275 case PROP_PIXEL_ASPECT_RATIO:
276 gst_value_set_fraction (value, gtk_widget->priv->par_n,
277 gtk_widget->priv->par_d);
279 case PROP_IGNORE_ALPHA:
280 g_value_set_boolean (value, gtk_widget->priv->ignore_alpha);
283 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
289 gtk_gst_widget_class_init (GtkGstWidgetClass * klass)
291 GObjectClass *gobject_klass = (GObjectClass *) klass;
292 GtkWidgetClass *widget_klass = (GtkWidgetClass *) klass;
294 g_type_class_add_private (klass, sizeof (GtkGstWidgetPrivate));
296 gobject_klass->set_property = gtk_gst_widget_set_property;
297 gobject_klass->get_property = gtk_gst_widget_get_property;
298 gobject_klass->finalize = gtk_gst_widget_finalize;
300 g_object_class_install_property (gobject_klass, PROP_FORCE_ASPECT_RATIO,
301 g_param_spec_boolean ("force-aspect-ratio",
302 "Force aspect ratio",
303 "When enabled, scaling will respect original aspect ratio",
304 DEFAULT_FORCE_ASPECT_RATIO,
305 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
307 g_object_class_install_property (gobject_klass, PROP_PIXEL_ASPECT_RATIO,
308 gst_param_spec_fraction ("pixel-aspect-ratio", "Pixel Aspect Ratio",
309 "The pixel aspect ratio of the device", DEFAULT_PAR_N, DEFAULT_PAR_D,
310 G_MAXINT, 1, 1, 1, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
312 g_object_class_install_property (gobject_klass, PROP_IGNORE_ALPHA,
313 g_param_spec_boolean ("ignore-alpha", "Ignore Alpha",
314 "When enabled, alpha will be ignored and converted to black",
315 DEFAULT_IGNORE_ALPHA, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
317 widget_klass->draw = gtk_gst_widget_draw;
318 widget_klass->get_preferred_width = gtk_gst_widget_get_preferred_width;
319 widget_klass->get_preferred_height = gtk_gst_widget_get_preferred_height;
323 gtk_gst_widget_init (GtkGstWidget * widget)
325 widget->priv = GTK_GST_WIDGET_GET_PRIVATE (widget);
327 widget->priv->force_aspect_ratio = DEFAULT_FORCE_ASPECT_RATIO;
328 widget->priv->par_n = DEFAULT_PAR_N;
329 widget->priv->par_d = DEFAULT_PAR_D;
330 widget->priv->ignore_alpha = DEFAULT_IGNORE_ALPHA;
332 g_mutex_init (&widget->priv->lock);
336 gtk_gst_widget_new (void)
338 return (GtkWidget *) g_object_new (GTK_TYPE_GST_WIDGET, NULL);
342 _queue_draw (GtkGstWidget * widget)
344 g_mutex_lock (&widget->priv->lock);
345 widget->priv->draw_id = 0;
346 g_mutex_unlock (&widget->priv->lock);
348 gtk_widget_queue_draw (GTK_WIDGET (widget));
350 return G_SOURCE_REMOVE;
354 gtk_gst_widget_set_buffer (GtkGstWidget * widget, GstBuffer * buffer)
356 g_return_if_fail (GTK_IS_GST_WIDGET (widget));
357 g_return_if_fail (buffer == NULL || widget->priv->negotiated);
359 g_mutex_lock (&widget->priv->lock);
361 gst_buffer_replace (&widget->priv->buffer, buffer);
363 if (!widget->priv->draw_id) {
364 widget->priv->draw_id = g_idle_add_full (G_PRIORITY_DEFAULT,
365 (GSourceFunc) _queue_draw, widget, NULL);
368 g_mutex_unlock (&widget->priv->lock);
372 _queue_resize (GtkGstWidget * widget)
374 g_mutex_lock (&widget->priv->lock);
375 widget->priv->resize_id = 0;
376 g_mutex_unlock (&widget->priv->lock);
378 gtk_widget_queue_resize (GTK_WIDGET (widget));
380 return G_SOURCE_REMOVE;
384 _calculate_par (GtkGstWidget * widget, GstVideoInfo * info)
389 gint display_par_n, display_par_d;
390 guint display_ratio_num, display_ratio_den;
392 width = GST_VIDEO_INFO_WIDTH (info);
393 height = GST_VIDEO_INFO_HEIGHT (info);
395 par_n = GST_VIDEO_INFO_PAR_N (info);
396 par_d = GST_VIDEO_INFO_PAR_D (info);
401 /* get display's PAR */
402 if (widget->priv->par_n != 0 && widget->priv->par_d != 0) {
403 display_par_n = widget->priv->par_n;
404 display_par_d = widget->priv->par_d;
410 ok = gst_video_calculate_display_ratio (&display_ratio_num,
411 &display_ratio_den, width, height, par_n, par_d, display_par_n,
417 GST_LOG ("PAR: %u/%u DAR:%u/%u", par_n, par_d, display_par_n, display_par_d);
419 if (height % display_ratio_den == 0) {
420 GST_DEBUG ("keeping video height");
421 widget->priv->display_width = (guint)
422 gst_util_uint64_scale_int (height, display_ratio_num,
424 widget->priv->display_height = height;
425 } else if (width % display_ratio_num == 0) {
426 GST_DEBUG ("keeping video width");
427 widget->priv->display_width = width;
428 widget->priv->display_height = (guint)
429 gst_util_uint64_scale_int (width, display_ratio_den, display_ratio_num);
431 GST_DEBUG ("approximating while keeping video height");
432 widget->priv->display_width = (guint)
433 gst_util_uint64_scale_int (height, display_ratio_num,
435 widget->priv->display_height = height;
437 GST_DEBUG ("scaling to %dx%d", widget->priv->display_width,
438 widget->priv->display_height);
444 gtk_gst_widget_set_caps (GtkGstWidget * widget, GstCaps * caps)
448 g_return_val_if_fail (GTK_IS_GST_WIDGET (widget), FALSE);
449 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
450 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
452 if (widget->priv->caps && gst_caps_is_equal_fixed (widget->priv->caps, caps))
455 if (!gst_video_info_from_caps (&v_info, caps))
458 /* FIXME: support other formats */
459 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
460 g_return_val_if_fail (GST_VIDEO_INFO_FORMAT (&v_info) ==
461 GST_VIDEO_FORMAT_BGRA || GST_VIDEO_INFO_FORMAT (&v_info) ==
462 GST_VIDEO_FORMAT_BGRx, FALSE);
464 g_return_val_if_fail (GST_VIDEO_INFO_FORMAT (&v_info) ==
465 GST_VIDEO_FORMAT_ARGB || GST_VIDEO_INFO_FORMAT (&v_info) ==
466 GST_VIDEO_FORMAT_xRGB, FALSE);
469 g_mutex_lock (&widget->priv->lock);
471 if (!_calculate_par (widget, &v_info)) {
472 g_mutex_unlock (&widget->priv->lock);
476 gst_buffer_replace (&widget->priv->buffer, NULL);
477 gst_caps_replace (&widget->priv->caps, caps);
478 widget->priv->v_info = v_info;
479 widget->priv->negotiated = TRUE;
481 if (!widget->priv->resize_id) {
482 widget->priv->resize_id = g_idle_add_full (G_PRIORITY_DEFAULT,
483 (GSourceFunc) _queue_resize, widget, NULL);
486 g_mutex_unlock (&widget->priv->lock);