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
27 #include <gst/interfaces/xoverlay.h>
28 #include <gst/farsight/fs-element-added-notifier.h>
30 #include "empathy-video-widget.h"
32 G_DEFINE_TYPE(EmpathyVideoWidget, empathy_video_widget,
33 GTK_TYPE_DRAWING_AREA)
35 static void empathy_video_widget_element_added_cb (
36 FsElementAddedNotifier *notifier, GstBin *bin, GstElement *element,
37 EmpathyVideoWidget *self);
39 static void empathy_video_widget_sync_message_cb (
40 GstBus *bus, GstMessage *message, EmpathyVideoWidget *self);
49 static guint signals[LAST_SIGNAL] = {0};
62 /* private structure */
63 typedef struct _EmpathyVideoWidgetPriv EmpathyVideoWidgetPriv;
65 struct _EmpathyVideoWidgetPriv
67 gboolean dispose_has_run;
69 GstElement *videosink;
73 FsElementAddedNotifier *notifier;
84 #define GET_PRIV(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
85 EMPATHY_TYPE_VIDEO_WIDGET, EmpathyVideoWidgetPriv))
88 empathy_video_widget_init (EmpathyVideoWidget *obj)
90 EmpathyVideoWidgetPriv *priv = GET_PRIV (obj);
93 priv->lock = g_mutex_new ();
95 priv->notifier = fs_element_added_notifier_new ();
96 g_signal_connect (priv->notifier, "element-added",
97 G_CALLBACK (empathy_video_widget_element_added_cb),
100 if (gdk_rgba_parse (&black, "Black"))
101 gtk_widget_override_background_color (GTK_WIDGET (obj), 0, &black);
103 gtk_widget_set_double_buffered (GTK_WIDGET (obj), FALSE);
107 empathy_video_widget_realized (GtkWidget *widget, gpointer user_data)
109 EmpathyVideoWidgetPriv *priv = GET_PRIV (widget);
111 /* requesting the XID forces the GdkWindow to be native in GTK+ 2.18
112 * onwards, requesting the native window in a thread causes a BadWindowID,
113 * so we need to request it now. We could call gdk_window_ensure_native(),
114 * but that would mean we require GTK+ 2.18, so instead we call this */
115 priv->xid = GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (widget)));
119 empathy_video_widget_constructed (GObject *object)
121 EmpathyVideoWidgetPriv *priv = GET_PRIV (object);
122 GstElement *colorspace, *videoscale, *sink;
125 g_signal_connect (object, "realize",
126 G_CALLBACK (empathy_video_widget_realized), NULL);
128 priv->videosink = gst_bin_new (NULL);
130 gst_object_ref (priv->videosink);
131 gst_object_sink (priv->videosink);
133 priv->sink_pad = gst_element_get_static_pad (priv->videosink, "sink");
135 sink = gst_element_factory_make ("gconfvideosink", NULL);
136 g_assert (sink != NULL);
138 videoscale = gst_element_factory_make ("videoscale", NULL);
139 g_assert (videoscale != NULL);
141 g_object_set (videoscale, "qos", FALSE, NULL);
143 colorspace = gst_element_factory_make ("ffmpegcolorspace", NULL);
144 g_assert (colorspace != NULL);
146 g_object_set (colorspace, "qos", FALSE, NULL);
148 priv->flip = gst_element_factory_make ("videoflip", NULL);
149 g_assert (priv->flip != NULL);
151 gst_bin_add_many (GST_BIN (priv->videosink), colorspace, videoscale,
152 priv->flip, sink, NULL);
154 if (!gst_element_link (colorspace, videoscale))
155 g_error ("Failed to link ffmpegcolorspace and videoscale");
157 if (!gst_element_link (videoscale, priv->flip))
158 g_error ("Failed to link videoscale and videoflip");
160 if (!gst_element_link (priv->flip, sink))
161 g_error ("Failed to link videoflip and gconfvideosink");
163 pad = gst_element_get_static_pad (colorspace, "sink");
164 g_assert (pad != NULL);
166 priv->sink_pad = gst_ghost_pad_new ("sink", pad);
167 if (!gst_element_add_pad (priv->videosink, priv->sink_pad))
168 g_error ("Couldn't add sink ghostpad to the bin");
170 gst_object_unref (pad);
172 fs_element_added_notifier_add (priv->notifier, GST_BIN (priv->videosink));
173 gst_bus_enable_sync_message_emission (priv->bus);
175 g_signal_connect (priv->bus, "sync-message",
176 G_CALLBACK (empathy_video_widget_sync_message_cb), object);
178 gtk_widget_set_size_request (GTK_WIDGET (object), priv->min_width,
182 static void empathy_video_widget_dispose (GObject *object);
183 static void empathy_video_widget_finalize (GObject *object);
186 static gboolean empathy_video_widget_draw (GtkWidget *widget,
190 empathy_video_widget_element_set_sink_properties (EmpathyVideoWidget *self);
193 empathy_video_widget_set_property (GObject *object,
194 guint property_id, const GValue *value, GParamSpec *pspec)
196 EmpathyVideoWidgetPriv *priv = GET_PRIV (object);
201 priv->bus = g_value_dup_object (value);
204 priv->min_width = g_value_get_int (value);
206 case PROP_MIN_HEIGHT:
207 priv->min_height = g_value_get_int (value);
210 priv->sync = g_value_get_boolean (value);
211 empathy_video_widget_element_set_sink_properties (
212 EMPATHY_VIDEO_WIDGET (object));
215 priv->async = g_value_get_boolean (value);
216 empathy_video_widget_element_set_sink_properties (
217 EMPATHY_VIDEO_WIDGET (object));
219 case PROP_FLIP_VIDEO:
220 priv->flip_video = g_value_get_boolean (value);
221 gst_util_set_object_arg (G_OBJECT (priv->flip), "method",
222 priv->flip_video ? "horizontal-flip" : "none");
225 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
230 empathy_video_widget_get_property (GObject *object,
231 guint property_id, GValue *value, GParamSpec *pspec)
233 EmpathyVideoWidgetPriv *priv = GET_PRIV (object);
237 case PROP_GST_ELEMENT:
238 g_value_set_object (value, priv->videosink);
241 g_value_set_object (value, priv->bus);
244 g_value_set_int (value, priv->min_width);
246 case PROP_MIN_HEIGHT:
247 g_value_set_int (value, priv->min_height);
250 g_value_set_boolean (value, priv->sync);
253 g_value_set_boolean (value, priv->async);
255 case PROP_FLIP_VIDEO:
256 g_value_set_boolean (value, priv->flip_video);
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
265 empathy_video_widget_class_init (
266 EmpathyVideoWidgetClass *empathy_video_widget_class)
268 GObjectClass *object_class = G_OBJECT_CLASS (empathy_video_widget_class);
269 GtkWidgetClass *widget_class =
270 GTK_WIDGET_CLASS (empathy_video_widget_class);
271 GParamSpec *param_spec;
273 g_type_class_add_private (empathy_video_widget_class,
274 sizeof (EmpathyVideoWidgetPriv));
276 object_class->dispose = empathy_video_widget_dispose;
277 object_class->finalize = empathy_video_widget_finalize;
278 object_class->constructed = empathy_video_widget_constructed;
280 object_class->set_property = empathy_video_widget_set_property;
281 object_class->get_property = empathy_video_widget_get_property;
283 widget_class->draw = empathy_video_widget_draw;
285 param_spec = g_param_spec_object ("gst-element",
286 "gst-element", "The underlaying gstreamer element",
288 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
289 g_object_class_install_property (object_class, PROP_GST_ELEMENT, param_spec);
291 param_spec = g_param_spec_object ("gst-bus",
293 "The toplevel bus from the pipeline in which this bin will be added",
295 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
296 g_object_class_install_property (object_class, PROP_GST_BUS, param_spec);
298 param_spec = g_param_spec_int ("min-width",
300 "Minimal width of the widget",
301 0, G_MAXINT, EMPATHY_VIDEO_WIDGET_DEFAULT_WIDTH,
302 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
303 g_object_class_install_property (object_class, PROP_MIN_WIDTH, param_spec);
305 param_spec = g_param_spec_int ("min-height",
307 "Minimal height of the widget",
308 0, G_MAXINT, EMPATHY_VIDEO_WIDGET_DEFAULT_HEIGHT,
309 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
310 g_object_class_install_property (object_class, PROP_MIN_HEIGHT, param_spec);
312 param_spec = g_param_spec_boolean ("sync",
314 "Whether the underlying sink should be sync or not",
316 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
317 g_object_class_install_property (object_class, PROP_SYNC, param_spec);
319 param_spec = g_param_spec_boolean ("async",
321 "Whether the underlying sink should be async or not",
323 G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
324 g_object_class_install_property (object_class, PROP_ASYNC, param_spec);
326 param_spec = g_param_spec_boolean ("flip-video",
328 "Whether the video should be flipped horizontally or not",
330 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
331 g_object_class_install_property (object_class, PROP_FLIP_VIDEO, param_spec);
335 empathy_video_widget_dispose (GObject *object)
337 EmpathyVideoWidget *self = EMPATHY_VIDEO_WIDGET (object);
338 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
340 if (priv->dispose_has_run)
343 priv->dispose_has_run = TRUE;
345 g_signal_handlers_disconnect_by_func (priv->bus,
346 empathy_video_widget_sync_message_cb, object);
348 if (priv->bus != NULL)
349 g_object_unref (priv->bus);
353 if (priv->videosink != NULL)
354 g_object_unref (priv->videosink);
356 priv->videosink = NULL;
359 /* release any references held by the object here */
361 if (G_OBJECT_CLASS (empathy_video_widget_parent_class)->dispose)
362 G_OBJECT_CLASS (empathy_video_widget_parent_class)->dispose (object);
366 empathy_video_widget_finalize (GObject *object)
368 EmpathyVideoWidget *self = EMPATHY_VIDEO_WIDGET (object);
369 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
371 /* free any data held directly by the object here */
372 g_mutex_free (priv->lock);
374 G_OBJECT_CLASS (empathy_video_widget_parent_class)->finalize (object);
379 empathy_video_widget_element_set_sink_properties_unlocked (
380 EmpathyVideoWidget *self)
382 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
384 if (priv->overlay == NULL)
387 if (g_object_class_find_property (G_OBJECT_GET_CLASS (priv->overlay),
388 "force-aspect-ratio"))
389 g_object_set (G_OBJECT (priv->overlay), "force-aspect-ratio", TRUE, NULL);
391 if (g_object_class_find_property (
392 G_OBJECT_GET_CLASS (priv->overlay), "sync"))
393 g_object_set (G_OBJECT (priv->overlay), "sync", priv->sync, NULL);
395 if (g_object_class_find_property (G_OBJECT_GET_CLASS (priv->overlay),
397 g_object_set (G_OBJECT (priv->overlay), "async", priv->async, NULL);
401 empathy_video_widget_element_set_sink_properties (EmpathyVideoWidget *self)
403 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
405 g_mutex_lock (priv->lock);
406 empathy_video_widget_element_set_sink_properties_unlocked (self);
407 g_mutex_unlock (priv->lock);
411 empathy_video_widget_element_added_cb (FsElementAddedNotifier *notifier,
412 GstBin *bin, GstElement *element, EmpathyVideoWidget *self)
414 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
416 /* We assume the overlay is the sink */
417 g_mutex_lock (priv->lock);
418 if (priv->overlay == NULL && GST_IS_X_OVERLAY (element))
420 priv->overlay = element;
421 g_object_add_weak_pointer (G_OBJECT (element),
422 (gpointer) &priv->overlay);
423 empathy_video_widget_element_set_sink_properties_unlocked (self);
424 gst_x_overlay_expose (GST_X_OVERLAY (priv->overlay));
426 g_mutex_unlock (priv->lock);
430 empathy_video_widget_sync_message_cb (GstBus *bus, GstMessage *message,
431 EmpathyVideoWidget *self)
433 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
434 const GstStructure *s;
436 if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT)
439 if (GST_MESSAGE_SRC (message) != (GstObject *) priv->overlay)
442 s = gst_message_get_structure (message);
444 if (gst_structure_has_name (s, "prepare-xwindow-id"))
446 g_assert (priv->xid != 0);
447 gst_x_overlay_set_window_handle (GST_X_OVERLAY (priv->overlay),
453 empathy_video_widget_draw (GtkWidget *widget,
456 EmpathyVideoWidget *self = EMPATHY_VIDEO_WIDGET (widget);
457 EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
458 GtkAllocation allocation;
460 if (priv->overlay == NULL)
462 gtk_widget_get_allocation (widget, &allocation);
464 gtk_render_frame (gtk_widget_get_style_context (widget), cr,
466 gtk_widget_get_allocated_width (widget),
467 gtk_widget_get_allocated_height (widget));
471 gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (priv->overlay),
472 GDK_WINDOW_XID (gtk_widget_get_window (widget)));
474 gst_x_overlay_expose (GST_X_OVERLAY (priv->overlay));
480 empathy_video_widget_new_with_size (GstBus *bus, gint width, gint height)
482 g_return_val_if_fail (bus != NULL, NULL);
484 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_VIDEO_WIDGET,
487 "min-height", height,
492 empathy_video_widget_new (GstBus *bus)
494 g_return_val_if_fail (bus != NULL, NULL);
496 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_VIDEO_WIDGET,
502 empathy_video_widget_get_sink (EmpathyVideoWidget *widget)
504 EmpathyVideoWidgetPriv *priv = GET_PRIV (widget);
506 return priv->sink_pad;
510 empathy_video_widget_get_element (EmpathyVideoWidget *widget)
512 EmpathyVideoWidgetPriv *priv = GET_PRIV (widget);
514 return priv->videosink;