gtkglsink: Fix unsafe handling of buffer life time
[platform/upstream/gst-plugins-good.git] / ext / gtk / gtkgstwidget.c
1 /*
2  * GStreamer
3  * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26
27 #include "gtkgstwidget.h"
28 #include <gst/video/video.h>
29
30 /**
31  * SECTION:gtkgstwidget
32  * @short_description: a #GtkWidget that renders GStreamer video #GstBuffers
33  * @see_also: #GtkDrawingArea, #GstBuffer
34  *
35  * #GtkGstWidget is an #GtkWidget that renders GStreamer video buffers.
36  */
37
38 G_DEFINE_TYPE (GtkGstWidget, gtk_gst_widget, GTK_TYPE_DRAWING_AREA);
39
40 static gboolean
41 gtk_gst_widget_draw (GtkWidget * widget, cairo_t * cr)
42 {
43   GtkGstBaseWidget *gst_widget = (GtkGstBaseWidget *) widget;
44   guint widget_width, widget_height;
45   cairo_surface_t *surface;
46   GstVideoFrame frame;
47
48   widget_width = gtk_widget_get_allocated_width (widget);
49   widget_height = gtk_widget_get_allocated_height (widget);
50
51   GTK_GST_BASE_WIDGET_LOCK (gst_widget);
52
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;
60   }
61
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;
70
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;
75     } else {
76       format = CAIRO_FORMAT_RGB24;
77     }
78
79     surface = cairo_image_surface_create_for_data (frame.data[0],
80         format, frame.info.width, frame.info.height, frame.info.stride[0]);
81
82     if (gst_widget->force_aspect_ratio) {
83       GstVideoRectangle src, dst;
84
85       src.x = 0;
86       src.y = 0;
87       src.w = gst_widget->display_width;
88       src.h = gst_widget->display_height;
89
90       dst.x = 0;
91       dst.y = 0;
92       dst.w = widget_width;
93       dst.h = widget_height;
94
95       gst_video_sink_center_rect (src, dst, &result, TRUE);
96
97       scale_x = scale_y = MIN (scale_x, scale_y);
98     } else {
99       result.x = 0;
100       result.y = 0;
101       result.w = widget_width;
102       result.h = widget_height;
103     }
104
105     if (gst_widget->ignore_alpha) {
106       GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
107
108       gdk_cairo_set_source_rgba (cr, &color);
109       if (result.x > 0) {
110         cairo_rectangle (cr, 0, 0, result.x, widget_height);
111         cairo_fill (cr);
112       }
113       if (result.y > 0) {
114         cairo_rectangle (cr, 0, 0, widget_width, result.y);
115         cairo_fill (cr);
116       }
117       if (result.w < widget_width) {
118         cairo_rectangle (cr, result.x + result.w, 0, widget_width - result.w,
119             widget_height);
120         cairo_fill (cr);
121       }
122       if (result.h < widget_height) {
123         cairo_rectangle (cr, 0, result.y + result.h, widget_width,
124             widget_height - result.h);
125         cairo_fill (cr);
126       }
127     }
128
129     scale_x *= (gdouble) gst_widget->display_width / (gdouble) frame.info.width;
130     scale_y *=
131         (gdouble) gst_widget->display_height / (gdouble) frame.info.height;
132
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);
137     cairo_paint (cr);
138
139     cairo_surface_destroy (surface);
140
141     gst_video_frame_unmap (&frame);
142   } else {
143     GdkRGBA color;
144
145     if (gst_widget->ignore_alpha) {
146       color.red = color.blue = color.green = 0.0;
147       color.alpha = 1.0;
148     } else {
149       gtk_style_context_get_color (gtk_widget_get_style_context (widget),
150           GTK_STATE_FLAG_NORMAL, &color);
151     }
152     gdk_cairo_set_source_rgba (cr, &color);
153     cairo_rectangle (cr, 0, 0, widget_width, widget_height);
154     cairo_fill (cr);
155   }
156
157   GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
158   return FALSE;
159 }
160
161 static void
162 gtk_gst_widget_finalize (GObject * object)
163 {
164   gtk_gst_base_widget_finalize (object);
165
166   G_OBJECT_CLASS (gtk_gst_widget_parent_class)->finalize (object);
167 }
168
169 static void
170 gtk_gst_widget_class_init (GtkGstWidgetClass * klass)
171 {
172   GObjectClass *gobject_klass = (GObjectClass *) klass;
173   GtkWidgetClass *widget_klass = (GtkWidgetClass *) klass;
174
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;
178 }
179
180 static void
181 gtk_gst_widget_init (GtkGstWidget * widget)
182 {
183   gtk_gst_base_widget_init (GTK_GST_BASE_WIDGET (widget));
184 }
185
186 GtkWidget *
187 gtk_gst_widget_new (void)
188 {
189   return (GtkWidget *) g_object_new (GTK_TYPE_GST_WIDGET, NULL);
190 }