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);
41 gtk_gst_widget_draw (GtkWidget * widget, cairo_t * cr)
43 GtkGstBaseWidget *gst_widget = (GtkGstBaseWidget *) widget;
44 guint widget_width, widget_height;
45 cairo_surface_t *surface;
48 widget_width = gtk_widget_get_allocated_width (widget);
49 widget_height = gtk_widget_get_allocated_height (widget);
51 GTK_GST_BASE_WIDGET_LOCK (gst_widget);
53 /* There is not much to optimize in term of redisplay, so simply swap the
54 * pending_buffer with the active buffer */
55 if (gst_widget->pending_buffer) {
56 if (gst_widget->buffer)
57 gst_buffer_unref (gst_widget->buffer);
58 gst_widget->buffer = gst_widget->pending_buffer;
59 gst_widget->pending_buffer = NULL;
62 /* failed to map the video frame */
63 if (gst_widget->negotiated && gst_widget->buffer
64 && gst_video_frame_map (&frame, &gst_widget->v_info,
65 gst_widget->buffer, GST_MAP_READ)) {
66 gdouble scale_x = (gdouble) widget_width / gst_widget->display_width;
67 gdouble scale_y = (gdouble) widget_height / gst_widget->display_height;
68 GstVideoRectangle result;
69 cairo_format_t format;
71 gst_widget->v_info = frame.info;
72 if (frame.info.finfo->format == GST_VIDEO_FORMAT_ARGB ||
73 frame.info.finfo->format == GST_VIDEO_FORMAT_BGRA) {
74 format = CAIRO_FORMAT_ARGB32;
76 format = CAIRO_FORMAT_RGB24;
79 surface = cairo_image_surface_create_for_data (frame.data[0],
80 format, frame.info.width, frame.info.height, frame.info.stride[0]);
82 if (gst_widget->force_aspect_ratio) {
83 GstVideoRectangle src, dst;
87 src.w = gst_widget->display_width;
88 src.h = gst_widget->display_height;
93 dst.h = widget_height;
95 gst_video_sink_center_rect (src, dst, &result, TRUE);
97 scale_x = scale_y = MIN (scale_x, scale_y);
101 result.w = widget_width;
102 result.h = widget_height;
105 if (gst_widget->ignore_alpha) {
106 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
108 gdk_cairo_set_source_rgba (cr, &color);
110 cairo_rectangle (cr, 0, 0, result.x, widget_height);
114 cairo_rectangle (cr, 0, 0, widget_width, result.y);
117 if (result.w < widget_width) {
118 cairo_rectangle (cr, result.x + result.w, 0, widget_width - result.w,
122 if (result.h < widget_height) {
123 cairo_rectangle (cr, 0, result.y + result.h, widget_width,
124 widget_height - result.h);
129 scale_x *= (gdouble) gst_widget->display_width / (gdouble) frame.info.width;
131 (gdouble) gst_widget->display_height / (gdouble) frame.info.height;
133 cairo_translate (cr, result.x, result.y);
134 cairo_scale (cr, scale_x, scale_y);
135 cairo_rectangle (cr, 0, 0, result.w, result.h);
136 cairo_set_source_surface (cr, surface, 0, 0);
139 cairo_surface_destroy (surface);
141 gst_video_frame_unmap (&frame);
145 if (gst_widget->ignore_alpha) {
146 color.red = color.blue = color.green = 0.0;
149 gtk_style_context_get_color (gtk_widget_get_style_context (widget),
150 GTK_STATE_FLAG_NORMAL, &color);
152 gdk_cairo_set_source_rgba (cr, &color);
153 cairo_rectangle (cr, 0, 0, widget_width, widget_height);
157 GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
162 gtk_gst_widget_finalize (GObject * object)
164 gtk_gst_base_widget_finalize (object);
166 G_OBJECT_CLASS (gtk_gst_widget_parent_class)->finalize (object);
170 gtk_gst_widget_class_init (GtkGstWidgetClass * klass)
172 GObjectClass *gobject_klass = (GObjectClass *) klass;
173 GtkWidgetClass *widget_klass = (GtkWidgetClass *) klass;
175 gtk_gst_base_widget_class_init (GTK_GST_BASE_WIDGET_CLASS (klass));
176 gobject_klass->finalize = gtk_gst_widget_finalize;
177 widget_klass->draw = gtk_gst_widget_draw;
181 gtk_gst_widget_init (GtkGstWidget * widget)
183 gtk_gst_base_widget_init (GTK_GST_BASE_WIDGET (widget));
187 gtk_gst_widget_new (void)
189 return (GtkWidget *) g_object_new (GTK_TYPE_GST_WIDGET, NULL);