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 * @title: GtkGstWidget
33 * @short_description: a #GtkWidget that renders GStreamer video #GstBuffers
34 * @see_also: #GtkDrawingArea, #GstBuffer
36 * #GtkGstWidget is an #GtkWidget that renders GStreamer video buffers.
39 G_DEFINE_TYPE (GtkGstWidget, gtk_gst_widget, GTK_TYPE_DRAWING_AREA);
42 gtk_gst_widget_draw (GtkWidget * widget, cairo_t * cr)
44 GtkGstBaseWidget *gst_widget = (GtkGstBaseWidget *) widget;
45 guint widget_width, widget_height;
46 cairo_surface_t *surface;
49 widget_width = gtk_widget_get_allocated_width (widget);
50 widget_height = gtk_widget_get_allocated_height (widget);
52 GTK_GST_BASE_WIDGET_LOCK (gst_widget);
54 /* There is not much to optimize in term of redisplay, so simply swap the
55 * pending_buffer with the active buffer */
56 if (gst_widget->pending_buffer) {
57 if (gst_widget->buffer)
58 gst_buffer_unref (gst_widget->buffer);
59 gst_widget->buffer = gst_widget->pending_buffer;
60 gst_widget->pending_buffer = NULL;
63 /* failed to map the video frame */
64 if (gst_widget->negotiated && gst_widget->buffer
65 && gst_video_frame_map (&frame, &gst_widget->v_info,
66 gst_widget->buffer, GST_MAP_READ)) {
67 gdouble scale_x = (gdouble) widget_width / gst_widget->display_width;
68 gdouble scale_y = (gdouble) widget_height / gst_widget->display_height;
69 GstVideoRectangle result;
70 cairo_format_t format;
72 gst_widget->v_info = frame.info;
73 if (frame.info.finfo->format == GST_VIDEO_FORMAT_ARGB ||
74 frame.info.finfo->format == GST_VIDEO_FORMAT_BGRA) {
75 format = CAIRO_FORMAT_ARGB32;
77 format = CAIRO_FORMAT_RGB24;
80 surface = cairo_image_surface_create_for_data (frame.data[0],
81 format, frame.info.width, frame.info.height, frame.info.stride[0]);
83 if (gst_widget->force_aspect_ratio) {
84 GstVideoRectangle src, dst;
88 src.w = gst_widget->display_width;
89 src.h = gst_widget->display_height;
94 dst.h = widget_height;
96 gst_video_sink_center_rect (src, dst, &result, TRUE);
98 scale_x = scale_y = MIN (scale_x, scale_y);
102 result.w = widget_width;
103 result.h = widget_height;
106 if (gst_widget->ignore_alpha) {
107 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
109 gdk_cairo_set_source_rgba (cr, &color);
111 cairo_rectangle (cr, 0, 0, result.x, widget_height);
115 cairo_rectangle (cr, 0, 0, widget_width, result.y);
118 if (result.w < widget_width) {
119 cairo_rectangle (cr, result.x + result.w, 0, widget_width - result.w,
123 if (result.h < widget_height) {
124 cairo_rectangle (cr, 0, result.y + result.h, widget_width,
125 widget_height - result.h);
130 scale_x *= (gdouble) gst_widget->display_width / (gdouble) frame.info.width;
132 (gdouble) gst_widget->display_height / (gdouble) frame.info.height;
134 cairo_translate (cr, result.x, result.y);
135 cairo_scale (cr, scale_x, scale_y);
136 cairo_rectangle (cr, 0, 0, result.w, result.h);
137 cairo_set_source_surface (cr, surface, 0, 0);
140 cairo_surface_destroy (surface);
142 gst_video_frame_unmap (&frame);
146 if (gst_widget->ignore_alpha) {
147 color.red = color.blue = color.green = 0.0;
150 gtk_style_context_get_color (gtk_widget_get_style_context (widget),
151 GTK_STATE_FLAG_NORMAL, &color);
153 gdk_cairo_set_source_rgba (cr, &color);
154 cairo_rectangle (cr, 0, 0, widget_width, widget_height);
158 GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
163 gtk_gst_widget_finalize (GObject * object)
165 gtk_gst_base_widget_finalize (object);
167 G_OBJECT_CLASS (gtk_gst_widget_parent_class)->finalize (object);
171 gtk_gst_widget_class_init (GtkGstWidgetClass * klass)
173 GObjectClass *gobject_klass = (GObjectClass *) klass;
174 GtkWidgetClass *widget_klass = (GtkWidgetClass *) klass;
176 gtk_gst_base_widget_class_init (GTK_GST_BASE_WIDGET_CLASS (klass));
177 gobject_klass->finalize = gtk_gst_widget_finalize;
178 widget_klass->draw = gtk_gst_widget_draw;
182 gtk_gst_widget_init (GtkGstWidget * widget)
184 gtk_gst_base_widget_init (GTK_GST_BASE_WIDGET (widget));
188 gtk_gst_widget_new (void)
190 return (GtkWidget *) g_object_new (GTK_TYPE_GST_WIDGET, NULL);