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
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);
48 static guint signals[LAST_SIGNAL] = {0};
60 /* private structure */
61 typedef struct _EmpathyVideoWidgetPriv EmpathyVideoWidgetPriv;
63 struct _EmpathyVideoWidgetPriv
65 gboolean dispose_has_run;
67 GstElement *videosink;
70 FsElementAddedNotifier *notifier;
79 #define GET_PRIV(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
80 EMPATHY_TYPE_VIDEO_WIDGET, EmpathyVideoWidgetPriv))
83 empathy_video_widget_init (EmpathyVideoWidget *obj)
85 EmpathyVideoWidgetPriv *priv = GET_PRIV (obj);
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),
95 if (gdk_color_parse ("Black", &black))
96 gtk_widget_modify_bg (GTK_WIDGET (obj), GTK_STATE_NORMAL,
99 gtk_widget_set_double_buffered (GTK_WIDGET (obj), FALSE);
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)));
113 empathy_video_widget_constructed (GObject *object)
115 EmpathyVideoWidgetPriv *priv = GET_PRIV (object);
116 GstElement *colorspace, *videoscale, *sink;
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,
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,
170 static void empathy_video_widget_dispose (GObject *object);
171 static void empathy_video_widget_finalize (GObject *object);
174 static gboolean empathy_video_widget_draw (GtkWidget *widget,
178 empathy_video_widget_element_set_sink_properties (EmpathyVideoWidget *self);
181 empathy_video_widget_set_property (GObject *object,
182 guint property_id, const GValue *value, GParamSpec *pspec)
184 EmpathyVideoWidgetPriv *priv = GET_PRIV (object);
189 priv->bus = g_value_dup_object (value);
192 priv->min_width = g_value_get_int (value);
194 case PROP_MIN_HEIGHT:
195 priv->min_height = g_value_get_int (value);
198 priv->sync = g_value_get_boolean (value);
199 empathy_video_widget_element_set_sink_properties (
200 EMPATHY_VIDEO_WIDGET (object));
203 priv->async = g_value_get_boolean (value);
204 empathy_video_widget_element_set_sink_properties (
205 EMPATHY_VIDEO_WIDGET (object));
208 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
213 empathy_video_widget_get_property (GObject *object,
214 guint property_id, GValue *value, GParamSpec *pspec)
216 EmpathyVideoWidgetPriv *priv = GET_PRIV (object);
220 case PROP_GST_ELEMENT:
221 g_value_set_object (value, priv->videosink);
224 g_value_set_object (value, priv->bus);
227 g_value_set_int (value, priv->min_width);
229 case PROP_MIN_HEIGHT:
230 g_value_set_int (value, priv->min_height);
233 g_value_set_boolean (value, priv->sync);
236 g_value_set_boolean (value, priv->async);
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
245 empathy_video_widget_class_init (
246 EmpathyVideoWidgetClass *empathy_video_widget_class)
248 GObjectClass *object_class = G_OBJECT_CLASS (empathy_video_widget_class);
249 GtkWidgetClass *widget_class =
250 GTK_WIDGET_CLASS (empathy_video_widget_class);
251 GParamSpec *param_spec;
253 g_type_class_add_private (empathy_video_widget_class,
254 sizeof (EmpathyVideoWidgetPriv));
256 object_class->dispose = empathy_video_widget_dispose;
257 object_class->finalize = empathy_video_widget_finalize;
258 object_class->constructed = empathy_video_widget_constructed;
260 object_class->set_property = empathy_video_widget_set_property;
261 object_class->get_property = empathy_video_widget_get_property;
263 widget_class->draw = empathy_video_widget_draw;
265 param_spec = g_param_spec_object ("gst-element",
266 "gst-element", "The underlaying gstreamer element",
268 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
269 g_object_class_install_property (object_class, PROP_GST_ELEMENT, param_spec);
271 param_spec = g_param_spec_object ("gst-bus",
273 "The toplevel bus from the pipeline in which this bin will be added",
275 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
276 g_object_class_install_property (object_class, PROP_GST_BUS, param_spec);
278 param_spec = g_param_spec_int ("min-width",
280 "Minimal width of the widget",
281 0, G_MAXINT, EMPATHY_VIDEO_WIDGET_DEFAULT_WIDTH,
282 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
283 g_object_class_install_property (object_class, PROP_MIN_WIDTH, param_spec);
285 param_spec = g_param_spec_int ("min-height",
287 "Minimal height of the widget",
288 0, G_MAXINT, EMPATHY_VIDEO_WIDGET_DEFAULT_HEIGHT,
289 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
290 g_object_class_install_property (object_class, PROP_MIN_HEIGHT, param_spec);
292 param_spec = g_param_spec_boolean ("sync",
294 "Whether the underlying sink should be sync or not",
296 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
297 g_object_class_install_property (object_class, PROP_SYNC, param_spec);
299 param_spec = g_param_spec_boolean ("async",
301 "Whether the underlying sink should be async or not",
303 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
304 g_object_class_install_property (object_class, PROP_ASYNC, param_spec);
308 empathy_video_widget_dispose (GObject *object)
310 EmpathyVideoWidget *self = EMPATHY_VIDEO_WIDGET (object);
311 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
313 if (priv->dispose_has_run)
316 priv->dispose_has_run = TRUE;
318 g_signal_handlers_disconnect_by_func (priv->bus,
319 empathy_video_widget_sync_message_cb, object);
321 if (priv->bus != NULL)
322 g_object_unref (priv->bus);
326 if (priv->videosink != NULL)
327 g_object_unref (priv->videosink);
329 priv->videosink = NULL;
332 /* release any references held by the object here */
334 if (G_OBJECT_CLASS (empathy_video_widget_parent_class)->dispose)
335 G_OBJECT_CLASS (empathy_video_widget_parent_class)->dispose (object);
339 empathy_video_widget_finalize (GObject *object)
341 EmpathyVideoWidget *self = EMPATHY_VIDEO_WIDGET (object);
342 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
344 /* free any data held directly by the object here */
345 g_mutex_free (priv->lock);
347 G_OBJECT_CLASS (empathy_video_widget_parent_class)->finalize (object);
352 empathy_video_widget_element_set_sink_properties_unlocked (
353 EmpathyVideoWidget *self)
355 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
357 if (priv->overlay == NULL)
360 if (g_object_class_find_property (G_OBJECT_GET_CLASS (priv->overlay),
361 "force-aspect-ratio"))
362 g_object_set (G_OBJECT (priv->overlay), "force-aspect-ratio", TRUE, NULL);
364 if (g_object_class_find_property (
365 G_OBJECT_GET_CLASS (priv->overlay), "sync"))
366 g_object_set (G_OBJECT (priv->overlay), "sync", priv->sync, NULL);
368 if (g_object_class_find_property (G_OBJECT_GET_CLASS (priv->overlay),
370 g_object_set (G_OBJECT (priv->overlay), "async", priv->async, NULL);
374 empathy_video_widget_element_set_sink_properties (EmpathyVideoWidget *self)
376 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
378 g_mutex_lock (priv->lock);
379 empathy_video_widget_element_set_sink_properties_unlocked (self);
380 g_mutex_unlock (priv->lock);
384 empathy_video_widget_element_added_cb (FsElementAddedNotifier *notifier,
385 GstBin *bin, GstElement *element, EmpathyVideoWidget *self)
387 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
389 /* We assume the overlay is the sink */
390 g_mutex_lock (priv->lock);
391 if (priv->overlay == NULL && GST_IS_X_OVERLAY (element))
393 priv->overlay = element;
394 g_object_add_weak_pointer (G_OBJECT (element),
395 (gpointer) &priv->overlay);
396 empathy_video_widget_element_set_sink_properties_unlocked (self);
397 gst_x_overlay_expose (GST_X_OVERLAY (priv->overlay));
399 g_mutex_unlock (priv->lock);
403 empathy_video_widget_sync_message_cb (GstBus *bus, GstMessage *message,
404 EmpathyVideoWidget *self)
406 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
407 const GstStructure *s;
409 if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT)
412 if (GST_MESSAGE_SRC (message) != (GstObject *) priv->overlay)
415 s = gst_message_get_structure (message);
417 if (gst_structure_has_name (s, "prepare-xwindow-id"))
419 g_assert (gtk_widget_get_realized (GTK_WIDGET (self)));
420 gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (priv->overlay),
421 GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (self))));
426 empathy_video_widget_draw (GtkWidget *widget,
429 EmpathyVideoWidget *self = EMPATHY_VIDEO_WIDGET (widget);
430 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
431 GtkAllocation allocation;
433 if (priv->overlay == NULL)
435 gtk_widget_get_allocation (widget, &allocation);
437 gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
438 GTK_STATE_NORMAL, GTK_SHADOW_NONE, widget, NULL,
440 gtk_widget_get_allocated_width (widget),
441 gtk_widget_get_allocated_height (widget));
445 gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (priv->overlay),
446 GDK_WINDOW_XID (gtk_widget_get_window (widget)));
448 gst_x_overlay_expose (GST_X_OVERLAY (priv->overlay));
454 empathy_video_widget_new_with_size (GstBus *bus, gint width, gint height)
456 g_return_val_if_fail (bus != NULL, NULL);
458 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_VIDEO_WIDGET,
461 "min-height", height,
466 empathy_video_widget_new (GstBus *bus)
468 g_return_val_if_fail (bus != NULL, NULL);
470 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_VIDEO_WIDGET,
476 empathy_video_widget_get_sink (EmpathyVideoWidget *widget)
478 EmpathyVideoWidgetPriv *priv = GET_PRIV (widget);
480 return priv->sink_pad;
484 empathy_video_widget_get_element (EmpathyVideoWidget *widget)
486 EmpathyVideoWidgetPriv *priv = GET_PRIV (widget);
488 return priv->videosink;