prepare 2.30.1.1 release
[empathy-mirror.git] / libempathy-gtk / empathy-video-widget.c
blob902e5cd7f37e8502eed889b00d8ac0f0478aa803
1 /*
2 * empathy-gst-gtk-widget.c - Source for EmpathyVideoWidget
3 * Copyright (C) 2008 Collabora Ltd.
4 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <stdio.h>
23 #include <stdlib.h>
25 #include <gdk/gdkx.h>
26 #include <gst/interfaces/xoverlay.h>
27 #include <gst/farsight/fs-element-added-notifier.h>
29 #include "empathy-video-widget.h"
31 G_DEFINE_TYPE(EmpathyVideoWidget, empathy_video_widget,
32 GTK_TYPE_DRAWING_AREA)
34 static void empathy_video_widget_element_added_cb (
35 FsElementAddedNotifier *notifier, GstBin *bin, GstElement *element,
36 EmpathyVideoWidget *self);
38 static void empathy_video_widget_sync_message_cb (
39 GstBus *bus, GstMessage *message, EmpathyVideoWidget *self);
41 /* signal enum */
42 #if 0
43 enum
45 LAST_SIGNAL
48 static guint signals[LAST_SIGNAL] = {0};
49 #endif
51 enum {
52 PROP_GST_ELEMENT = 1,
53 PROP_GST_BUS,
54 PROP_MIN_WIDTH,
55 PROP_MIN_HEIGHT,
56 PROP_SYNC,
57 PROP_ASYNC,
60 /* private structure */
61 typedef struct _EmpathyVideoWidgetPriv EmpathyVideoWidgetPriv;
63 struct _EmpathyVideoWidgetPriv
65 gboolean dispose_has_run;
66 GstBus *bus;
67 GstElement *videosink;
68 GstPad *sink_pad;
69 GstElement *overlay;
70 FsElementAddedNotifier *notifier;
71 gint min_width;
72 gint min_height;
73 gboolean sync;
74 gboolean async;
76 GMutex *lock;
79 #define GET_PRIV(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
80 EMPATHY_TYPE_VIDEO_WIDGET, EmpathyVideoWidgetPriv))
82 static void
83 empathy_video_widget_init (EmpathyVideoWidget *obj)
85 EmpathyVideoWidgetPriv *priv = GET_PRIV (obj);
86 GdkColor black;
88 priv->lock = g_mutex_new ();
90 priv->notifier = fs_element_added_notifier_new ();
91 g_signal_connect (priv->notifier, "element-added",
92 G_CALLBACK (empathy_video_widget_element_added_cb),
93 obj);
95 if (gdk_color_parse ("Black", &black))
96 gtk_widget_modify_bg (GTK_WIDGET (obj), GTK_STATE_NORMAL,
97 &black);
99 gtk_widget_set_double_buffered (GTK_WIDGET (obj), FALSE);
102 static void
103 empathy_video_widget_realized (GtkWidget *widget, gpointer user_data)
105 /* requesting the XID forces the GdkWindow to be native in GTK+ 2.18
106 * onwards, requesting the native window in a thread causes a BadWindowID,
107 * so we need to request it now. We could call gdk_window_ensure_native(),
108 * but that would mean we require GTK+ 2.18, so instead we call this */
109 GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (widget)));
112 static void
113 empathy_video_widget_constructed (GObject *object)
115 EmpathyVideoWidgetPriv *priv = GET_PRIV (object);
116 GstElement *colorspace, *videoscale, *sink;
117 GstPad *pad;
119 g_signal_connect (object, "realize",
120 G_CALLBACK (empathy_video_widget_realized), NULL);
122 priv->videosink = gst_bin_new (NULL);
124 gst_object_ref (priv->videosink);
125 gst_object_sink (priv->videosink);
127 priv->sink_pad = gst_element_get_static_pad (priv->videosink, "sink");
129 sink = gst_element_factory_make ("gconfvideosink", NULL);
130 g_assert (sink != NULL);
132 videoscale = gst_element_factory_make ("videoscale", NULL);
133 g_assert (videoscale != NULL);
135 g_object_set (videoscale, "qos", FALSE, NULL);
137 colorspace = gst_element_factory_make ("ffmpegcolorspace", NULL);
138 g_assert (colorspace != NULL);
140 g_object_set (colorspace, "qos", FALSE, NULL);
142 gst_bin_add_many (GST_BIN (priv->videosink), colorspace, videoscale,
143 sink, NULL);
145 if (!gst_element_link (colorspace, videoscale))
146 g_error ("Failed to link ffmpegcolorspace and videoscale");
148 if (!gst_element_link (videoscale, sink))
149 g_error ("Failed to link videoscale and gconfvideosink");
151 pad = gst_element_get_static_pad (colorspace, "sink");
152 g_assert (pad != NULL);
154 priv->sink_pad = gst_ghost_pad_new ("sink", pad);
155 if (!gst_element_add_pad (priv->videosink, priv->sink_pad))
156 g_error ("Couldn't add sink ghostpad to the bin");
158 gst_object_unref (pad);
160 fs_element_added_notifier_add (priv->notifier, GST_BIN (priv->videosink));
161 gst_bus_enable_sync_message_emission (priv->bus);
163 g_signal_connect (priv->bus, "sync-message",
164 G_CALLBACK (empathy_video_widget_sync_message_cb), object);
166 gtk_widget_set_size_request (GTK_WIDGET (object), priv->min_width,
167 priv->min_height);
170 static void empathy_video_widget_dispose (GObject *object);
171 static void empathy_video_widget_finalize (GObject *object);
173 static gboolean empathy_video_widget_expose_event (GtkWidget *widget,
174 GdkEventExpose *event);
175 static void
176 empathy_video_widget_element_set_sink_properties (EmpathyVideoWidget *self);
178 static void
179 empathy_video_widget_set_property (GObject *object,
180 guint property_id, const GValue *value, GParamSpec *pspec)
182 EmpathyVideoWidgetPriv *priv = GET_PRIV (object);
184 switch (property_id)
186 case PROP_GST_BUS:
187 priv->bus = g_value_dup_object (value);
188 break;
189 case PROP_MIN_WIDTH:
190 priv->min_width = g_value_get_int (value);
191 break;
192 case PROP_MIN_HEIGHT:
193 priv->min_height = g_value_get_int (value);
194 break;
195 case PROP_SYNC:
196 priv->sync = g_value_get_boolean (value);
197 empathy_video_widget_element_set_sink_properties (
198 EMPATHY_VIDEO_WIDGET (object));
199 break;
200 case PROP_ASYNC:
201 priv->async = g_value_get_boolean (value);
202 empathy_video_widget_element_set_sink_properties (
203 EMPATHY_VIDEO_WIDGET (object));
204 break;
205 default:
206 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
210 static void
211 empathy_video_widget_get_property (GObject *object,
212 guint property_id, GValue *value, GParamSpec *pspec)
214 EmpathyVideoWidgetPriv *priv = GET_PRIV (object);
216 switch (property_id)
218 case PROP_GST_ELEMENT:
219 g_value_set_object (value, priv->videosink);
220 break;
221 case PROP_GST_BUS:
222 g_value_set_object (value, priv->bus);
223 break;
224 case PROP_MIN_WIDTH:
225 g_value_set_int (value, priv->min_width);
226 break;
227 case PROP_MIN_HEIGHT:
228 g_value_set_int (value, priv->min_height);
229 break;
230 case PROP_SYNC:
231 g_value_set_boolean (value, priv->sync);
232 break;
233 case PROP_ASYNC:
234 g_value_set_boolean (value, priv->async);
235 break;
236 default:
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
242 static void
243 empathy_video_widget_class_init (
244 EmpathyVideoWidgetClass *empathy_video_widget_class)
246 GObjectClass *object_class = G_OBJECT_CLASS (empathy_video_widget_class);
247 GtkWidgetClass *widget_class =
248 GTK_WIDGET_CLASS (empathy_video_widget_class);
249 GParamSpec *param_spec;
251 g_type_class_add_private (empathy_video_widget_class,
252 sizeof (EmpathyVideoWidgetPriv));
254 object_class->dispose = empathy_video_widget_dispose;
255 object_class->finalize = empathy_video_widget_finalize;
256 object_class->constructed = empathy_video_widget_constructed;
258 object_class->set_property = empathy_video_widget_set_property;
259 object_class->get_property = empathy_video_widget_get_property;
261 widget_class->expose_event = empathy_video_widget_expose_event;
263 param_spec = g_param_spec_object ("gst-element",
264 "gst-element", "The underlaying gstreamer element",
265 GST_TYPE_ELEMENT,
266 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
267 g_object_class_install_property (object_class, PROP_GST_ELEMENT, param_spec);
269 param_spec = g_param_spec_object ("gst-bus",
270 "gst-bus",
271 "The toplevel bus from the pipeline in which this bin will be added",
272 GST_TYPE_BUS,
273 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
274 g_object_class_install_property (object_class, PROP_GST_BUS, param_spec);
276 param_spec = g_param_spec_int ("min-width",
277 "min-width",
278 "Minimal width of the widget",
279 0, G_MAXINT, EMPATHY_VIDEO_WIDGET_DEFAULT_WIDTH,
280 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
281 g_object_class_install_property (object_class, PROP_MIN_WIDTH, param_spec);
283 param_spec = g_param_spec_int ("min-height",
284 "min-height",
285 "Minimal height of the widget",
286 0, G_MAXINT, EMPATHY_VIDEO_WIDGET_DEFAULT_HEIGHT,
287 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
288 g_object_class_install_property (object_class, PROP_MIN_HEIGHT, param_spec);
290 param_spec = g_param_spec_boolean ("sync",
291 "sync",
292 "Whether the underlying sink should be sync or not",
293 TRUE,
294 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
295 g_object_class_install_property (object_class, PROP_SYNC, param_spec);
297 param_spec = g_param_spec_boolean ("async",
298 "async",
299 "Whether the underlying sink should be async or not",
300 TRUE,
301 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
302 g_object_class_install_property (object_class, PROP_ASYNC, param_spec);
305 void
306 empathy_video_widget_dispose (GObject *object)
308 EmpathyVideoWidget *self = EMPATHY_VIDEO_WIDGET (object);
309 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
311 if (priv->dispose_has_run)
312 return;
314 priv->dispose_has_run = TRUE;
316 g_signal_handlers_disconnect_by_func (priv->bus,
317 empathy_video_widget_sync_message_cb, object);
319 if (priv->bus != NULL)
320 g_object_unref (priv->bus);
322 priv->bus = NULL;
324 if (priv->videosink != NULL)
325 g_object_unref (priv->videosink);
327 priv->videosink = NULL;
330 /* release any references held by the object here */
332 if (G_OBJECT_CLASS (empathy_video_widget_parent_class)->dispose)
333 G_OBJECT_CLASS (empathy_video_widget_parent_class)->dispose (object);
336 void
337 empathy_video_widget_finalize (GObject *object)
339 EmpathyVideoWidget *self = EMPATHY_VIDEO_WIDGET (object);
340 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
342 /* free any data held directly by the object here */
343 g_mutex_free (priv->lock);
345 G_OBJECT_CLASS (empathy_video_widget_parent_class)->finalize (object);
349 static void
350 empathy_video_widget_element_set_sink_properties_unlocked (
351 EmpathyVideoWidget *self)
353 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
355 if (priv->overlay == NULL)
356 return;
358 if (g_object_class_find_property (G_OBJECT_GET_CLASS (priv->overlay),
359 "force-aspect-ratio"))
360 g_object_set (G_OBJECT (priv->overlay), "force-aspect-ratio", TRUE, NULL);
362 if (g_object_class_find_property (
363 G_OBJECT_GET_CLASS (priv->overlay), "sync"))
364 g_object_set (G_OBJECT (priv->overlay), "sync", priv->sync, NULL);
366 if (g_object_class_find_property (G_OBJECT_GET_CLASS (priv->overlay),
367 "async"))
368 g_object_set (G_OBJECT (priv->overlay), "async", priv->async, NULL);
371 static void
372 empathy_video_widget_element_set_sink_properties (EmpathyVideoWidget *self)
374 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
376 g_mutex_lock (priv->lock);
377 empathy_video_widget_element_set_sink_properties_unlocked (self);
378 g_mutex_unlock (priv->lock);
381 static void
382 empathy_video_widget_element_added_cb (FsElementAddedNotifier *notifier,
383 GstBin *bin, GstElement *element, EmpathyVideoWidget *self)
385 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
387 /* We assume the overlay is the sink */
388 g_mutex_lock (priv->lock);
389 if (priv->overlay == NULL && GST_IS_X_OVERLAY (element))
391 priv->overlay = element;
392 g_object_add_weak_pointer (G_OBJECT (element),
393 (gpointer) &priv->overlay);
394 empathy_video_widget_element_set_sink_properties_unlocked (self);
395 gst_x_overlay_expose (GST_X_OVERLAY (priv->overlay));
397 g_mutex_unlock (priv->lock);
400 static void
401 empathy_video_widget_sync_message_cb (GstBus *bus, GstMessage *message,
402 EmpathyVideoWidget *self)
404 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
405 const GstStructure *s;
407 if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT)
408 return;
410 if (GST_MESSAGE_SRC (message) != (GstObject *) priv->overlay)
411 return;
413 s = gst_message_get_structure (message);
415 if (gst_structure_has_name (s, "prepare-xwindow-id"))
417 g_assert (GTK_WIDGET_REALIZED (GTK_WIDGET (self)));
418 gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (priv->overlay),
419 GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (self))));
423 static gboolean
424 empathy_video_widget_expose_event (GtkWidget *widget, GdkEventExpose *event)
426 EmpathyVideoWidget *self = EMPATHY_VIDEO_WIDGET (widget);
427 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
428 GtkAllocation allocation;
430 if (event != NULL && event->count > 0)
431 return TRUE;
433 if (priv->overlay == NULL)
435 gtk_widget_get_allocation (widget, &allocation);
436 gdk_window_clear_area (gtk_widget_get_window (widget), 0, 0,
437 allocation.width, allocation.height);
438 return TRUE;
441 gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (priv->overlay),
442 GDK_WINDOW_XID (gtk_widget_get_window (widget)));
444 gst_x_overlay_expose (GST_X_OVERLAY (priv->overlay));
446 return TRUE;
449 GtkWidget *
450 empathy_video_widget_new_with_size (GstBus *bus, gint width, gint height)
452 g_return_val_if_fail (bus != NULL, NULL);
454 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_VIDEO_WIDGET,
455 "gst-bus", bus,
456 "min-width", width,
457 "min-height", height,
458 NULL));
461 GtkWidget *
462 empathy_video_widget_new (GstBus *bus)
464 g_return_val_if_fail (bus != NULL, NULL);
466 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_VIDEO_WIDGET,
467 "gst-bus", bus,
468 NULL));
471 GstPad *
472 empathy_video_widget_get_sink (EmpathyVideoWidget *widget)
474 EmpathyVideoWidgetPriv *priv = GET_PRIV (widget);
476 return priv->sink_pad;
479 GstElement *
480 empathy_video_widget_get_element (EmpathyVideoWidget *widget)
482 EmpathyVideoWidgetPriv *priv = GET_PRIV (widget);
484 return priv->videosink;