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_UNSET_FLAGS (GTK_WIDGET (obj
), GTK_DOUBLE_BUFFERED
);
103 empathy_video_widget_constructed (GObject
*object
)
105 EmpathyVideoWidgetPriv
*priv
= GET_PRIV (object
);
106 GstElement
*colorspace
, *videoscale
, *sink
;
109 priv
->videosink
= gst_bin_new (NULL
);
111 gst_object_ref (priv
->videosink
);
112 gst_object_sink (priv
->videosink
);
114 priv
->sink_pad
= gst_element_get_static_pad (priv
->videosink
, "sink");
116 sink
= gst_element_factory_make ("gconfvideosink", NULL
);
117 g_assert (sink
!= NULL
);
119 videoscale
= gst_element_factory_make ("videoscale", NULL
);
120 g_assert (videoscale
!= NULL
);
122 g_object_set (videoscale
, "qos", FALSE
, NULL
);
124 colorspace
= gst_element_factory_make ("ffmpegcolorspace", NULL
);
125 g_assert (colorspace
!= NULL
);
127 g_object_set (colorspace
, "qos", FALSE
, NULL
);
129 gst_bin_add_many (GST_BIN (priv
->videosink
), colorspace
, videoscale
,
132 if (!gst_element_link (colorspace
, videoscale
))
133 g_error ("Failed to link ffmpegcolorspace and videoscale");
135 if (!gst_element_link (videoscale
, sink
))
136 g_error ("Failed to link videoscale and gconfvideosink");
138 pad
= gst_element_get_static_pad (colorspace
, "sink");
139 g_assert (pad
!= NULL
);
141 priv
->sink_pad
= gst_ghost_pad_new ("sink", pad
);
142 if (!gst_element_add_pad (priv
->videosink
, priv
->sink_pad
))
143 g_error ("Couldn't add sink ghostpad to the bin");
145 gst_object_unref (pad
);
147 fs_element_added_notifier_add (priv
->notifier
, GST_BIN (priv
->videosink
));
148 gst_bus_enable_sync_message_emission (priv
->bus
);
150 g_signal_connect (priv
->bus
, "sync-message",
151 G_CALLBACK (empathy_video_widget_sync_message_cb
), object
);
153 gtk_widget_set_size_request (GTK_WIDGET (object
), priv
->min_width
,
157 static void empathy_video_widget_dispose (GObject
*object
);
158 static void empathy_video_widget_finalize (GObject
*object
);
160 static gboolean
empathy_video_widget_expose_event (GtkWidget
*widget
,
161 GdkEventExpose
*event
);
163 empathy_video_widget_element_set_sink_properties (EmpathyVideoWidget
*self
);
166 empathy_video_widget_set_property (GObject
*object
,
167 guint property_id
, const GValue
*value
, GParamSpec
*pspec
)
169 EmpathyVideoWidgetPriv
*priv
= GET_PRIV (object
);
174 priv
->bus
= g_value_dup_object (value
);
177 priv
->min_width
= g_value_get_int (value
);
179 case PROP_MIN_HEIGHT
:
180 priv
->min_height
= g_value_get_int (value
);
183 priv
->sync
= g_value_get_boolean (value
);
184 empathy_video_widget_element_set_sink_properties (
185 EMPATHY_VIDEO_WIDGET (object
));
188 priv
->async
= g_value_get_boolean (value
);
189 empathy_video_widget_element_set_sink_properties (
190 EMPATHY_VIDEO_WIDGET (object
));
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
198 empathy_video_widget_get_property (GObject
*object
,
199 guint property_id
, GValue
*value
, GParamSpec
*pspec
)
201 EmpathyVideoWidgetPriv
*priv
= GET_PRIV (object
);
205 case PROP_GST_ELEMENT
:
206 g_value_set_object (value
, priv
->videosink
);
209 g_value_set_object (value
, priv
->bus
);
212 g_value_set_int (value
, priv
->min_width
);
214 case PROP_MIN_HEIGHT
:
215 g_value_set_int (value
, priv
->min_height
);
218 g_value_set_boolean (value
, priv
->sync
);
221 g_value_set_boolean (value
, priv
->async
);
224 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
230 empathy_video_widget_class_init (
231 EmpathyVideoWidgetClass
*empathy_video_widget_class
)
233 GObjectClass
*object_class
= G_OBJECT_CLASS (empathy_video_widget_class
);
234 GtkWidgetClass
*widget_class
=
235 GTK_WIDGET_CLASS (empathy_video_widget_class
);
236 GParamSpec
*param_spec
;
238 g_type_class_add_private (empathy_video_widget_class
,
239 sizeof (EmpathyVideoWidgetPriv
));
241 object_class
->dispose
= empathy_video_widget_dispose
;
242 object_class
->finalize
= empathy_video_widget_finalize
;
243 object_class
->constructed
= empathy_video_widget_constructed
;
245 object_class
->set_property
= empathy_video_widget_set_property
;
246 object_class
->get_property
= empathy_video_widget_get_property
;
248 widget_class
->expose_event
= empathy_video_widget_expose_event
;
250 param_spec
= g_param_spec_object ("gst-element",
251 "gst-element", "The underlaying gstreamer element",
253 G_PARAM_READABLE
| G_PARAM_STATIC_STRINGS
);
254 g_object_class_install_property (object_class
, PROP_GST_ELEMENT
, param_spec
);
256 param_spec
= g_param_spec_object ("gst-bus",
258 "The toplevel bus from the pipeline in which this bin will be added",
260 G_PARAM_CONSTRUCT_ONLY
| G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
);
261 g_object_class_install_property (object_class
, PROP_GST_BUS
, param_spec
);
263 param_spec
= g_param_spec_int ("min-width",
265 "Minimal width of the widget",
267 G_PARAM_CONSTRUCT_ONLY
| G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
);
268 g_object_class_install_property (object_class
, PROP_MIN_WIDTH
, param_spec
);
270 param_spec
= g_param_spec_int ("min-height",
272 "Minimal height of the widget",
274 G_PARAM_CONSTRUCT_ONLY
| G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
);
275 g_object_class_install_property (object_class
, PROP_MIN_HEIGHT
, param_spec
);
277 param_spec
= g_param_spec_boolean ("sync",
279 "Whether the underlying sink should be sync or not",
281 G_PARAM_CONSTRUCT
| G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
);
282 g_object_class_install_property (object_class
, PROP_SYNC
, param_spec
);
284 param_spec
= g_param_spec_boolean ("async",
286 "Whether the underlying sink should be async or not",
288 G_PARAM_CONSTRUCT
| G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
);
289 g_object_class_install_property (object_class
, PROP_ASYNC
, param_spec
);
293 empathy_video_widget_dispose (GObject
*object
)
295 EmpathyVideoWidget
*self
= EMPATHY_VIDEO_WIDGET (object
);
296 EmpathyVideoWidgetPriv
*priv
= GET_PRIV (self
);
298 if (priv
->dispose_has_run
)
301 priv
->dispose_has_run
= TRUE
;
303 if (priv
->bus
!= NULL
)
304 g_object_unref (priv
->bus
);
308 if (priv
->videosink
!= NULL
)
309 g_object_unref (priv
->videosink
);
311 priv
->videosink
= NULL
;
314 /* release any references held by the object here */
316 if (G_OBJECT_CLASS (empathy_video_widget_parent_class
)->dispose
)
317 G_OBJECT_CLASS (empathy_video_widget_parent_class
)->dispose (object
);
321 empathy_video_widget_finalize (GObject
*object
)
323 EmpathyVideoWidget
*self
= EMPATHY_VIDEO_WIDGET (object
);
324 EmpathyVideoWidgetPriv
*priv
= GET_PRIV (self
);
326 /* free any data held directly by the object here */
327 g_mutex_free (priv
->lock
);
329 G_OBJECT_CLASS (empathy_video_widget_parent_class
)->finalize (object
);
334 empathy_video_widget_element_set_sink_properties_unlocked (
335 EmpathyVideoWidget
*self
)
337 EmpathyVideoWidgetPriv
*priv
= GET_PRIV (self
);
339 if (priv
->overlay
== NULL
)
342 if (g_object_class_find_property (G_OBJECT_GET_CLASS (priv
->overlay
),
343 "force-aspect-ratio"))
344 g_object_set (G_OBJECT (priv
->overlay
), "force-aspect-ratio", TRUE
, NULL
);
346 if (g_object_class_find_property (
347 G_OBJECT_GET_CLASS (priv
->overlay
), "sync"))
348 g_object_set (G_OBJECT (priv
->overlay
), "sync", priv
->sync
, NULL
);
350 if (g_object_class_find_property (G_OBJECT_GET_CLASS (priv
->overlay
),
352 g_object_set (G_OBJECT (priv
->overlay
), "async", priv
->async
, NULL
);
356 empathy_video_widget_element_set_sink_properties (EmpathyVideoWidget
*self
)
358 EmpathyVideoWidgetPriv
*priv
= GET_PRIV (self
);
360 g_mutex_lock (priv
->lock
);
361 empathy_video_widget_element_set_sink_properties_unlocked (self
);
362 g_mutex_unlock (priv
->lock
);
366 empathy_video_widget_element_added_cb (FsElementAddedNotifier
*notifier
,
367 GstBin
*bin
, GstElement
*element
, EmpathyVideoWidget
*self
)
369 EmpathyVideoWidgetPriv
*priv
= GET_PRIV (self
);
371 /* We assume the overlay is the sink */
372 g_mutex_lock (priv
->lock
);
373 if (priv
->overlay
== NULL
&& GST_IS_X_OVERLAY (element
))
375 priv
->overlay
= element
;
376 g_object_add_weak_pointer (G_OBJECT (element
),
377 (gpointer
) &priv
->overlay
);
378 empathy_video_widget_element_set_sink_properties_unlocked (self
);
379 gst_x_overlay_expose (GST_X_OVERLAY (priv
->overlay
));
381 g_mutex_unlock (priv
->lock
);
385 empathy_video_widget_sync_message_cb (GstBus
*bus
, GstMessage
*message
,
386 EmpathyVideoWidget
*self
)
388 EmpathyVideoWidgetPriv
*priv
= GET_PRIV (self
);
389 const GstStructure
*s
;
391 if (GST_MESSAGE_TYPE (message
) != GST_MESSAGE_ELEMENT
)
394 if (GST_MESSAGE_SRC (message
) != (GstObject
*) priv
->overlay
)
397 s
= gst_message_get_structure (message
);
399 if (gst_structure_has_name (s
, "prepare-xwindow-id"))
401 g_assert (GTK_WIDGET_REALIZED (GTK_WIDGET (self
)));
402 gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (priv
->overlay
),
403 GDK_WINDOW_XID (GTK_WIDGET (self
)->window
));
408 empathy_video_widget_expose_event (GtkWidget
*widget
, GdkEventExpose
*event
)
410 EmpathyVideoWidget
*self
= EMPATHY_VIDEO_WIDGET (widget
);
411 EmpathyVideoWidgetPriv
*priv
= GET_PRIV (self
);
413 if (event
!= NULL
&& event
->count
> 0)
416 if (priv
->overlay
== NULL
)
418 gdk_window_clear_area (widget
->window
, 0, 0,
419 widget
->allocation
.width
, widget
->allocation
.height
);
423 gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (priv
->overlay
),
424 GDK_WINDOW_XID (widget
->window
));
426 gst_x_overlay_expose (GST_X_OVERLAY (priv
->overlay
));
432 empathy_video_widget_new_with_size (GstBus
*bus
, gint width
, gint height
)
434 g_return_val_if_fail (bus
!= NULL
, NULL
);
436 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_VIDEO_WIDGET
,
439 "min-height", height
,
444 empathy_video_widget_new (GstBus
*bus
)
446 g_return_val_if_fail (bus
!= NULL
, NULL
);
448 return GTK_WIDGET (g_object_new (EMPATHY_TYPE_VIDEO_WIDGET
,
454 empathy_video_widget_get_sink (EmpathyVideoWidget
*widget
)
456 EmpathyVideoWidgetPriv
*priv
= GET_PRIV (widget
);
458 return priv
->sink_pad
;
462 empathy_video_widget_get_element (EmpathyVideoWidget
*widget
)
464 EmpathyVideoWidgetPriv
*priv
= GET_PRIV (widget
);
466 return priv
->videosink
;