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 * @title: GtkGstWidget
33 * @short_description: a #GtkWidget that renders GStreamer video #GstBuffers
34 * @see_also: #GtkDrawingArea, #GstBuffer
35 *
36 * #GtkGstWidget is an #GtkWidget that renders GStreamer video buffers.
37 */
38
39 G_DEFINE_TYPE (GtkGstWidget, gtk_gst_widget, GTK_TYPE_DRAWING_AREA);
40
41 static void
_drawing_area_draw(GtkDrawingArea * da,cairo_t * cr,gint widget_width,gint widget_height,gpointer data)42 _drawing_area_draw (GtkDrawingArea * da, cairo_t * cr,
43 gint widget_width, gint widget_height, gpointer data)
44 {
45 GtkWidget *widget = GTK_WIDGET (da);
46 GtkGstBaseWidget *gst_widget = (GtkGstBaseWidget *) widget;
47 cairo_surface_t *surface;
48 GstVideoFrame frame;
49
50 GTK_GST_BASE_WIDGET_LOCK (gst_widget);
51
52 /* There is not much to optimize in term of redisplay, so simply swap the
53 * pending_buffer with the active buffer */
54 if (gst_widget->pending_buffer) {
55 if (gst_widget->buffer)
56 gst_buffer_unref (gst_widget->buffer);
57 gst_widget->buffer = gst_widget->pending_buffer;
58 gst_widget->pending_buffer = NULL;
59 }
60
61 /* failed to map the video frame */
62 if (gst_widget->negotiated && gst_widget->buffer
63 && gst_video_frame_map (&frame, &gst_widget->v_info,
64 gst_widget->buffer, GST_MAP_READ)) {
65 gdouble scale_x = (gdouble) widget_width / gst_widget->display_width;
66 gdouble scale_y = (gdouble) widget_height / gst_widget->display_height;
67 GstVideoRectangle result;
68 cairo_format_t format;
69
70 gst_widget->v_info = frame.info;
71 if (frame.info.finfo->format == GST_VIDEO_FORMAT_ARGB ||
72 frame.info.finfo->format == GST_VIDEO_FORMAT_BGRA) {
73 format = CAIRO_FORMAT_ARGB32;
74 } else {
75 format = CAIRO_FORMAT_RGB24;
76 }
77
78 surface = cairo_image_surface_create_for_data (frame.data[0],
79 format, frame.info.width, frame.info.height, frame.info.stride[0]);
80
81 if (gst_widget->force_aspect_ratio) {
82 GstVideoRectangle src, dst;
83
84 src.x = 0;
85 src.y = 0;
86 src.w = gst_widget->display_width;
87 src.h = gst_widget->display_height;
88
89 dst.x = 0;
90 dst.y = 0;
91 dst.w = widget_width;
92 dst.h = widget_height;
93
94 gst_video_sink_center_rect (src, dst, &result, TRUE);
95
96 scale_x = scale_y = MIN (scale_x, scale_y);
97 } else {
98 result.x = 0;
99 result.y = 0;
100 result.w = widget_width;
101 result.h = widget_height;
102 }
103
104 if (gst_widget->ignore_alpha) {
105 GdkRGBA color = { 0.0, 0.0, 0.0, 1.0 };
106
107 gdk_cairo_set_source_rgba (cr, &color);
108 if (result.x > 0) {
109 cairo_rectangle (cr, 0, 0, result.x, widget_height);
110 cairo_fill (cr);
111 }
112 if (result.y > 0) {
113 cairo_rectangle (cr, 0, 0, widget_width, result.y);
114 cairo_fill (cr);
115 }
116 if (result.w < widget_width) {
117 cairo_rectangle (cr, result.x + result.w, 0, widget_width - result.w,
118 widget_height);
119 cairo_fill (cr);
120 }
121 if (result.h < widget_height) {
122 cairo_rectangle (cr, 0, result.y + result.h, widget_width,
123 widget_height - result.h);
124 cairo_fill (cr);
125 }
126 }
127
128 scale_x *= (gdouble) gst_widget->display_width / (gdouble) frame.info.width;
129 scale_y *=
130 (gdouble) gst_widget->display_height / (gdouble) frame.info.height;
131
132 cairo_translate (cr, result.x, result.y);
133 cairo_scale (cr, scale_x, scale_y);
134 cairo_rectangle (cr, 0, 0, result.w, result.h);
135 cairo_set_source_surface (cr, surface, 0, 0);
136 cairo_paint (cr);
137
138 cairo_surface_destroy (surface);
139
140 gst_video_frame_unmap (&frame);
141 } else {
142 GdkRGBA color;
143
144 if (gst_widget->ignore_alpha) {
145 color.red = color.blue = color.green = 0.0;
146 color.alpha = 1.0;
147 } else {
148 gtk_style_context_get_color (gtk_widget_get_style_context (widget),
149 #if !defined(BUILD_FOR_GTK4)
150 GTK_STATE_FLAG_NORMAL,
151 #endif
152 &color);
153 }
154 gdk_cairo_set_source_rgba (cr, &color);
155 cairo_rectangle (cr, 0, 0, widget_width, widget_height);
156 cairo_fill (cr);
157 }
158
159 GTK_GST_BASE_WIDGET_UNLOCK (gst_widget);
160 }
161
162 #if !defined(BUILD_FOR_GTK4)
163 static gboolean
gtk_gst_widget_draw(GtkWidget * widget,cairo_t * cr)164 gtk_gst_widget_draw (GtkWidget * widget, cairo_t * cr)
165 {
166 gint width = gtk_widget_get_allocated_width (widget);
167 gint height = gtk_widget_get_allocated_height (widget);
168
169 _drawing_area_draw (GTK_DRAWING_AREA (widget), cr, width, height, NULL);
170
171 return FALSE;
172 }
173 #endif
174
175 static void
gtk_gst_widget_finalize(GObject * object)176 gtk_gst_widget_finalize (GObject * object)
177 {
178 gtk_gst_base_widget_finalize (object);
179
180 G_OBJECT_CLASS (gtk_gst_widget_parent_class)->finalize (object);
181 }
182
183 static void
gtk_gst_widget_class_init(GtkGstWidgetClass * klass)184 gtk_gst_widget_class_init (GtkGstWidgetClass * klass)
185 {
186 GObjectClass *gobject_klass = (GObjectClass *) klass;
187 #if !defined(BUILD_FOR_GTK4)
188 GtkWidgetClass *widget_klass = (GtkWidgetClass *) klass;
189 #endif
190
191 gtk_gst_base_widget_class_init (GTK_GST_BASE_WIDGET_CLASS (klass));
192 gobject_klass->finalize = gtk_gst_widget_finalize;
193 #if !defined(BUILD_FOR_GTK4)
194 widget_klass->draw = gtk_gst_widget_draw;
195 #endif
196 }
197
198 static void
gtk_gst_widget_init(GtkGstWidget * widget)199 gtk_gst_widget_init (GtkGstWidget * widget)
200 {
201 gtk_gst_base_widget_init (GTK_GST_BASE_WIDGET (widget));
202 #if defined(BUILD_FOR_GTK4)
203 gtk_drawing_area_set_draw_func (GTK_DRAWING_AREA (widget),
204 _drawing_area_draw, NULL, NULL);
205 #endif
206 }
207
208 GtkWidget *
gtk_gst_widget_new(void)209 gtk_gst_widget_new (void)
210 {
211 return (GtkWidget *) g_object_new (GTK_TYPE_GST_WIDGET, NULL);
212 }
213