]> git.0d.be Git - empathy.git/blob - src/empathy-video-widget.c
Update Simplified Chinese help translation.
[empathy.git] / src / empathy-video-widget.c
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>
5  *
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.
10  *
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.
15  *
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
19  */
20
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <gdk/gdkx.h>
26 #include <gst/interfaces/xoverlay.h>
27 #include <gst/farsight/fs-element-added-notifier.h>
28
29 #include "empathy-video-widget.h"
30
31 G_DEFINE_TYPE(EmpathyVideoWidget, empathy_video_widget,
32   GTK_TYPE_DRAWING_AREA)
33
34 static void empathy_video_widget_element_added_cb (
35   FsElementAddedNotifier *notifier, GstBin *bin, GstElement *element,
36   EmpathyVideoWidget *self);
37
38 static void empathy_video_widget_sync_message_cb (
39   GstBus *bus, GstMessage *message, EmpathyVideoWidget *self);
40
41 /* signal enum */
42 #if 0
43 enum
44 {
45     LAST_SIGNAL
46 };
47
48 static guint signals[LAST_SIGNAL] = {0};
49 #endif
50
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,
58   PROP_FLIP_VIDEO,
59 };
60
61 /* private structure */
62 typedef struct _EmpathyVideoWidgetPriv EmpathyVideoWidgetPriv;
63
64 struct _EmpathyVideoWidgetPriv
65 {
66   gboolean dispose_has_run;
67   GstBus *bus;
68   GstElement *videosink;
69   GstPad *sink_pad;
70   GstElement *overlay;
71   GstElement *flip;
72   FsElementAddedNotifier *notifier;
73   gint min_width;
74   gint min_height;
75   gboolean sync;
76   gboolean async;
77   gboolean flip_video;
78
79   GMutex *lock;
80 };
81
82 #define GET_PRIV(o)     (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
83   EMPATHY_TYPE_VIDEO_WIDGET, EmpathyVideoWidgetPriv))
84
85 static void
86 empathy_video_widget_init (EmpathyVideoWidget *obj)
87 {
88   EmpathyVideoWidgetPriv *priv = GET_PRIV (obj);
89   GdkRGBA black;
90
91   priv->lock = g_mutex_new ();
92
93   priv->notifier = fs_element_added_notifier_new ();
94   g_signal_connect (priv->notifier, "element-added",
95     G_CALLBACK (empathy_video_widget_element_added_cb),
96     obj);
97
98   if (gdk_rgba_parse (&black, "Black"))
99     gtk_widget_override_background_color (GTK_WIDGET (obj), 0, &black);
100
101   gtk_widget_set_double_buffered (GTK_WIDGET (obj), FALSE);
102 }
103
104 static void
105 empathy_video_widget_realized (GtkWidget *widget, gpointer user_data)
106 {
107   /* requesting the XID forces the GdkWindow to be native in GTK+ 2.18
108    * onwards, requesting the native window in a thread causes a BadWindowID,
109    * so we need to request it now. We could call gdk_window_ensure_native(),
110    * but that would mean we require GTK+ 2.18, so instead we call this */
111   GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (widget)));
112 }
113
114 static void
115 empathy_video_widget_constructed (GObject *object)
116 {
117   EmpathyVideoWidgetPriv *priv = GET_PRIV (object);
118   GstElement *colorspace, *videoscale, *sink;
119   GstPad *pad;
120
121   g_signal_connect (object, "realize",
122       G_CALLBACK (empathy_video_widget_realized), NULL);
123
124   priv->videosink = gst_bin_new (NULL);
125
126   gst_object_ref (priv->videosink);
127   gst_object_sink (priv->videosink);
128
129   priv->sink_pad = gst_element_get_static_pad (priv->videosink, "sink");
130
131   sink = gst_element_factory_make ("gconfvideosink", NULL);
132   g_assert (sink != NULL);
133
134   videoscale = gst_element_factory_make ("videoscale", NULL);
135   g_assert (videoscale != NULL);
136
137   g_object_set (videoscale, "qos", FALSE, NULL);
138
139   colorspace = gst_element_factory_make ("ffmpegcolorspace", NULL);
140   g_assert (colorspace != NULL);
141
142   g_object_set (colorspace, "qos", FALSE, NULL);
143
144   priv->flip = gst_element_factory_make ("videoflip", NULL);
145   g_assert (priv->flip != NULL);
146
147   gst_bin_add_many (GST_BIN (priv->videosink), colorspace, videoscale,
148     priv->flip, sink, NULL);
149
150   if (!gst_element_link (colorspace, videoscale))
151     g_error ("Failed to link ffmpegcolorspace and videoscale");
152
153   if (!gst_element_link (videoscale, priv->flip))
154     g_error ("Failed to link videoscale and videoflip");
155
156   if (!gst_element_link (priv->flip, sink))
157     g_error ("Failed to link videoflip and gconfvideosink");
158
159   pad = gst_element_get_static_pad (colorspace, "sink");
160   g_assert (pad != NULL);
161
162   priv->sink_pad = gst_ghost_pad_new ("sink", pad);
163   if (!gst_element_add_pad  (priv->videosink, priv->sink_pad))
164     g_error ("Couldn't add sink ghostpad to the bin");
165
166   gst_object_unref (pad);
167
168   fs_element_added_notifier_add (priv->notifier, GST_BIN (priv->videosink));
169   gst_bus_enable_sync_message_emission (priv->bus);
170
171   g_signal_connect (priv->bus, "sync-message",
172     G_CALLBACK (empathy_video_widget_sync_message_cb), object);
173
174   gtk_widget_set_size_request (GTK_WIDGET (object), priv->min_width,
175     priv->min_height);
176 }
177
178 static void empathy_video_widget_dispose (GObject *object);
179 static void empathy_video_widget_finalize (GObject *object);
180
181
182 static gboolean empathy_video_widget_draw (GtkWidget *widget,
183   cairo_t *cr);
184
185 static void
186 empathy_video_widget_element_set_sink_properties (EmpathyVideoWidget *self);
187
188 static void
189 empathy_video_widget_set_property (GObject *object,
190   guint property_id, const GValue *value, GParamSpec *pspec)
191 {
192   EmpathyVideoWidgetPriv *priv = GET_PRIV (object);
193
194   switch (property_id)
195     {
196       case PROP_GST_BUS:
197         priv->bus = g_value_dup_object (value);
198         break;
199       case PROP_MIN_WIDTH:
200         priv->min_width = g_value_get_int (value);
201         break;
202       case PROP_MIN_HEIGHT:
203         priv->min_height = g_value_get_int (value);
204         break;
205       case PROP_SYNC:
206         priv->sync = g_value_get_boolean (value);
207         empathy_video_widget_element_set_sink_properties (
208           EMPATHY_VIDEO_WIDGET (object));
209         break;
210       case PROP_ASYNC:
211         priv->async = g_value_get_boolean (value);
212         empathy_video_widget_element_set_sink_properties (
213           EMPATHY_VIDEO_WIDGET (object));
214         break;
215       case PROP_FLIP_VIDEO:
216         priv->flip_video = g_value_get_boolean (value);
217         gst_util_set_object_arg (G_OBJECT (priv->flip), "method",
218             priv->flip_video ? "horizontal-flip" : "none");
219         break;
220       default:
221         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
222     }
223 }
224
225 static void
226 empathy_video_widget_get_property (GObject *object,
227   guint property_id, GValue *value, GParamSpec *pspec)
228 {
229   EmpathyVideoWidgetPriv *priv = GET_PRIV (object);
230
231   switch (property_id)
232     {
233       case PROP_GST_ELEMENT:
234         g_value_set_object (value, priv->videosink);
235         break;
236       case PROP_GST_BUS:
237         g_value_set_object (value, priv->bus);
238         break;
239       case PROP_MIN_WIDTH:
240         g_value_set_int (value, priv->min_width);
241         break;
242       case PROP_MIN_HEIGHT:
243         g_value_set_int (value, priv->min_height);
244         break;
245       case PROP_SYNC:
246         g_value_set_boolean (value, priv->sync);
247         break;
248       case PROP_ASYNC:
249         g_value_set_boolean (value, priv->async);
250         break;
251       case PROP_FLIP_VIDEO:
252         g_value_set_boolean (value, priv->flip_video);
253         break;
254       default:
255         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
256     }
257 }
258
259
260 static void
261 empathy_video_widget_class_init (
262   EmpathyVideoWidgetClass *empathy_video_widget_class)
263 {
264   GObjectClass *object_class = G_OBJECT_CLASS (empathy_video_widget_class);
265   GtkWidgetClass *widget_class =
266     GTK_WIDGET_CLASS (empathy_video_widget_class);
267   GParamSpec *param_spec;
268
269   g_type_class_add_private (empathy_video_widget_class,
270     sizeof (EmpathyVideoWidgetPriv));
271
272   object_class->dispose = empathy_video_widget_dispose;
273   object_class->finalize = empathy_video_widget_finalize;
274   object_class->constructed = empathy_video_widget_constructed;
275
276   object_class->set_property = empathy_video_widget_set_property;
277   object_class->get_property = empathy_video_widget_get_property;
278
279   widget_class->draw = empathy_video_widget_draw;
280
281   param_spec = g_param_spec_object ("gst-element",
282     "gst-element", "The underlaying gstreamer element",
283     GST_TYPE_ELEMENT,
284     G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
285   g_object_class_install_property (object_class, PROP_GST_ELEMENT, param_spec);
286
287   param_spec = g_param_spec_object ("gst-bus",
288     "gst-bus",
289     "The toplevel bus from the pipeline in which this bin will be added",
290     GST_TYPE_BUS,
291     G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
292   g_object_class_install_property (object_class, PROP_GST_BUS, param_spec);
293
294   param_spec = g_param_spec_int ("min-width",
295     "min-width",
296     "Minimal width of the widget",
297     0, G_MAXINT, EMPATHY_VIDEO_WIDGET_DEFAULT_WIDTH,
298     G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
299   g_object_class_install_property (object_class, PROP_MIN_WIDTH, param_spec);
300
301   param_spec = g_param_spec_int ("min-height",
302     "min-height",
303     "Minimal height of the widget",
304     0, G_MAXINT, EMPATHY_VIDEO_WIDGET_DEFAULT_HEIGHT,
305     G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
306   g_object_class_install_property (object_class, PROP_MIN_HEIGHT, param_spec);
307
308   param_spec = g_param_spec_boolean ("sync",
309     "sync",
310     "Whether the underlying sink should be sync or not",
311     TRUE,
312     G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
313   g_object_class_install_property (object_class, PROP_SYNC, param_spec);
314
315   param_spec = g_param_spec_boolean ("async",
316     "async",
317     "Whether the underlying sink should be async or not",
318     TRUE,
319     G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
320   g_object_class_install_property (object_class, PROP_ASYNC, param_spec);
321
322   param_spec = g_param_spec_boolean ("flip-video",
323     "flip video",
324     "Whether the video should be flipped horizontally or not",
325     FALSE,
326     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
327   g_object_class_install_property (object_class, PROP_FLIP_VIDEO, param_spec);
328 }
329
330 void
331 empathy_video_widget_dispose (GObject *object)
332 {
333   EmpathyVideoWidget *self = EMPATHY_VIDEO_WIDGET (object);
334   EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
335
336   if (priv->dispose_has_run)
337     return;
338
339   priv->dispose_has_run = TRUE;
340
341   g_signal_handlers_disconnect_by_func (priv->bus,
342     empathy_video_widget_sync_message_cb, object);
343
344   if (priv->bus != NULL)
345     g_object_unref (priv->bus);
346
347   priv->bus = NULL;
348
349   if (priv->videosink != NULL)
350     g_object_unref (priv->videosink);
351
352   priv->videosink = NULL;
353
354
355   /* release any references held by the object here */
356
357   if (G_OBJECT_CLASS (empathy_video_widget_parent_class)->dispose)
358     G_OBJECT_CLASS (empathy_video_widget_parent_class)->dispose (object);
359 }
360
361 void
362 empathy_video_widget_finalize (GObject *object)
363 {
364   EmpathyVideoWidget *self = EMPATHY_VIDEO_WIDGET (object);
365   EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
366
367   /* free any data held directly by the object here */
368   g_mutex_free (priv->lock);
369
370   G_OBJECT_CLASS (empathy_video_widget_parent_class)->finalize (object);
371 }
372
373
374 static void
375 empathy_video_widget_element_set_sink_properties_unlocked (
376   EmpathyVideoWidget *self)
377 {
378   EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
379
380   if (priv->overlay == NULL)
381     return;
382
383   if (g_object_class_find_property (G_OBJECT_GET_CLASS (priv->overlay),
384       "force-aspect-ratio"))
385     g_object_set (G_OBJECT (priv->overlay), "force-aspect-ratio", TRUE, NULL);
386
387   if (g_object_class_find_property (
388       G_OBJECT_GET_CLASS (priv->overlay), "sync"))
389     g_object_set (G_OBJECT (priv->overlay), "sync", priv->sync, NULL);
390
391   if (g_object_class_find_property (G_OBJECT_GET_CLASS (priv->overlay),
392       "async"))
393     g_object_set (G_OBJECT (priv->overlay), "async", priv->async, NULL);
394 }
395
396 static void
397 empathy_video_widget_element_set_sink_properties (EmpathyVideoWidget *self)
398 {
399   EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
400
401   g_mutex_lock (priv->lock);
402   empathy_video_widget_element_set_sink_properties_unlocked (self);
403   g_mutex_unlock (priv->lock);
404 }
405
406 static void
407 empathy_video_widget_element_added_cb (FsElementAddedNotifier *notifier,
408   GstBin *bin, GstElement *element, EmpathyVideoWidget *self)
409 {
410   EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
411
412   /* We assume the overlay is the sink */
413   g_mutex_lock (priv->lock);
414   if (priv->overlay == NULL && GST_IS_X_OVERLAY (element))
415     {
416       priv->overlay = element;
417       g_object_add_weak_pointer (G_OBJECT (element),
418         (gpointer) &priv->overlay);
419       empathy_video_widget_element_set_sink_properties_unlocked (self);
420       gst_x_overlay_expose (GST_X_OVERLAY (priv->overlay));
421     }
422   g_mutex_unlock (priv->lock);
423 }
424
425 static void
426 empathy_video_widget_sync_message_cb (GstBus *bus, GstMessage *message,
427   EmpathyVideoWidget *self)
428 {
429   EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
430   const GstStructure *s;
431
432   if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT)
433     return;
434
435   if (GST_MESSAGE_SRC (message) != (GstObject *) priv->overlay)
436     return;
437
438   s = gst_message_get_structure (message);
439
440   if (gst_structure_has_name (s, "prepare-xwindow-id"))
441     {
442       g_assert (gtk_widget_get_realized (GTK_WIDGET (self)));
443       gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (priv->overlay),
444         GDK_WINDOW_XID (gtk_widget_get_window (GTK_WIDGET (self))));
445     }
446 }
447
448 static gboolean
449 empathy_video_widget_draw (GtkWidget *widget,
450     cairo_t *cr)
451 {
452   EmpathyVideoWidget *self = EMPATHY_VIDEO_WIDGET (widget);
453   EmpathyVideoWidgetPriv *priv = GET_PRIV (self);
454   GtkAllocation allocation;
455
456   if (priv->overlay == NULL)
457     {
458       gtk_widget_get_allocation (widget, &allocation);
459
460       gtk_render_frame (gtk_widget_get_style_context (widget), cr,
461           0, 0,
462           gtk_widget_get_allocated_width (widget),
463           gtk_widget_get_allocated_height (widget));
464       return TRUE;
465     }
466
467   gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (priv->overlay),
468     GDK_WINDOW_XID (gtk_widget_get_window (widget)));
469
470   gst_x_overlay_expose (GST_X_OVERLAY (priv->overlay));
471
472   return TRUE;
473 }
474
475 GtkWidget *
476 empathy_video_widget_new_with_size (GstBus *bus, gint width, gint height)
477 {
478   g_return_val_if_fail (bus != NULL, NULL);
479
480   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_VIDEO_WIDGET,
481     "gst-bus", bus,
482     "min-width", width,
483     "min-height", height,
484     NULL));
485 }
486
487 GtkWidget *
488 empathy_video_widget_new (GstBus *bus)
489 {
490   g_return_val_if_fail (bus != NULL, NULL);
491
492   return GTK_WIDGET (g_object_new (EMPATHY_TYPE_VIDEO_WIDGET,
493     "gst-bus", bus,
494     NULL));
495 }
496
497 GstPad *
498 empathy_video_widget_get_sink (EmpathyVideoWidget *widget)
499 {
500   EmpathyVideoWidgetPriv *priv = GET_PRIV (widget);
501
502   return priv->sink_pad;
503 }
504
505 GstElement *
506 empathy_video_widget_get_element (EmpathyVideoWidget *widget)
507 {
508   EmpathyVideoWidgetPriv *priv = GET_PRIV (widget);
509
510   return priv->videosink;
511 }