1 /**
2 * @file mediamanager.c Media Manager API
3 * @ingroup core
4 */
5
6 /* purple
7 *
8 * Purple is the legal property of its developers, whose names are too numerous
9 * to list here. Please refer to the COPYRIGHT file distributed with this
10 * source distribution.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
25 */
26
27 #include "internal.h"
28
29 #include "account.h"
30 #include "debug.h"
31 #include "glibcompat.h"
32 #include "media.h"
33 #include "mediamanager.h"
34
35 #ifdef USE_GSTREAMER
36 #include "marshallers.h"
37 #include "media-gst.h"
38 #endif
39
40 #ifdef USE_VV
41 #include <media/backend-fs2.h>
42
43 #ifdef HAVE_FARSIGHT
44 #include <gst/farsight/fs-element-added-notifier.h>
45 #else
46 #include <farstream/fs-element-added-notifier.h>
47 #endif
48 #endif
49 #ifdef __DragonFly__
50 #ifdef HAVE_MEDIA_APPLICATION
51 #include <gst/app/app.h>
52 #endif
53
54 #if GST_CHECK_VERSION(1,0,0)
55 #include <gst/video/videooverlay.h>
56 #else
57 #include <gst/interfaces/xoverlay.h>
58 #endif
59
60 /** @copydoc _PurpleMediaManagerPrivate */
61 typedef struct _PurpleMediaManagerPrivate PurpleMediaManagerPrivate;
62 /** @copydoc _PurpleMediaOutputWindow */
63 typedef struct _PurpleMediaOutputWindow PurpleMediaOutputWindow;
64 /** @copydoc _PurpleMediaManagerPrivate */
65 typedef struct _PurpleMediaElementInfoPrivate PurpleMediaElementInfoPrivate;
66
67 /** The media manager class. */
68 struct _PurpleMediaManagerClass
69 {
70 GObjectClass parent_class; /**< The parent class. */
71 };
72
73 /** The media manager's data. */
74 struct _PurpleMediaManager
75 {
76 GObject parent; /**< The parent of this manager. */
77 PurpleMediaManagerPrivate *priv; /**< Private data for the manager. */
78 };
79
80 struct _PurpleMediaOutputWindow
81 {
82 gulong id;
83 PurpleMedia *media;
84 gchar *session_id;
85 gchar *participant;
86 gulong window_id;
87 GstElement *sink;
88 guint caps_id;
89 };
90
91 struct _PurpleMediaManagerPrivate
92 {
93 GstElement *pipeline;
94 PurpleMediaCaps ui_caps;
95 GList *medias;
96 GList *private_medias;
97 GList *elements;
98 GList *output_windows;
99 gulong next_output_window_id;
100 GType backend_type;
101 GstCaps *video_caps;
102
103 gchar *video_src_id;
104 gchar *video_sink_id;
105 gchar *audio_src_id;
106 gchar *audio_sink_id;
107 PurpleMediaElementInfo *video_src;
108 PurpleMediaElementInfo *video_sink;
109 PurpleMediaElementInfo *audio_src;
110 PurpleMediaElementInfo *audio_sink;
111
112 #ifdef USE_GSTREAMER
113 #if GST_CHECK_VERSION(1, 4, 0)
114 GstDeviceMonitor *device_monitor;
115 #endif /* GST_CHECK_VERSION(1, 4, 0) */
116 #endif /* USE_GSTREAMER */
117
118 #ifdef HAVE_MEDIA_APPLICATION
119 /* Application data streams */
120 GList *appdata_info; /* holds PurpleMediaAppDataInfo */
121 GMutex appdata_mutex;
122 guint appdata_cb_token; /* last used read/write callback token */
123 #endif
124 };
125
126 #ifdef HAVE_MEDIA_APPLICATION
127 typedef struct {
128 PurpleMedia *media;
129 GWeakRef media_ref;
130 gchar *session_id;
131 gchar *participant;
132 PurpleMediaAppDataCallbacks callbacks;
133 gpointer user_data;
134 GDestroyNotify notify;
135 GstAppSrc *appsrc;
136 GstAppSink *appsink;
137 gint num_samples;
138 GstSample *current_sample;
139 guint sample_offset;
140 gboolean writable;
141 gboolean connected;
142 guint writable_cb_token;
143 guint readable_cb_token;
144 guint writable_timer_id;
145 guint readable_timer_id;
146 GCond readable_cond;
147 } PurpleMediaAppDataInfo;
148 #endif
149
150 #define PURPLE_MEDIA_MANAGER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_MEDIA_MANAGER, PurpleMediaManagerPrivate))
151 #define PURPLE_MEDIA_ELEMENT_INFO_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), PURPLE_TYPE_MEDIA_ELEMENT_INFO, PurpleMediaElementInfoPrivate))
152
153 static void purple_media_manager_class_init (PurpleMediaManagerClass *klass);
154 static void purple_media_manager_init (PurpleMediaManager *media);
155 static void purple_media_manager_finalize (GObject *object);
156 #ifdef HAVE_MEDIA_APPLICATION
157 static void free_appdata_info_locked (PurpleMediaAppDataInfo *info);
158 #endif
159 #ifdef USE_GSTREAMER
160 static void purple_media_manager_init_device_monitor(PurpleMediaManager *manager);
161 static void purple_media_manager_register_static_elements(PurpleMediaManager *manager);
162 #endif
163
164 static GObjectClass *parent_class = NULL;
165
166
167
168 enum {
169 INIT_MEDIA,
170 INIT_PRIVATE_MEDIA,
171 UI_CAPS_CHANGED,
172 ELEMENTS_CHANGED,
173 LAST_SIGNAL
174 };
175 static guint purple_media_manager_signals[LAST_SIGNAL] = {0};
176 #endif
177
178 GType
purple_media_manager_get_type()179 purple_media_manager_get_type()
180 {
181 #ifdef USE_VV
182 static GType type = 0;
183
184 if (type == 0) {
185 static const GTypeInfo info = {
186 sizeof(PurpleMediaManagerClass),
187 NULL,
188 NULL,
189 (GClassInitFunc) purple_media_manager_class_init,
190 NULL,
191 NULL,
192 sizeof(PurpleMediaManager),
193 0,
194 (GInstanceInitFunc) purple_media_manager_init,
195 NULL
196 };
197 type = g_type_register_static(G_TYPE_OBJECT, "PurpleMediaManager", &info, 0);
198 }
199 return type;
200 #else
201 return G_TYPE_NONE;
202 #endif
203 }
204
205 #ifdef USE_VV
206 static void
purple_media_manager_class_init(PurpleMediaManagerClass * klass)207 purple_media_manager_class_init (PurpleMediaManagerClass *klass)
208 {
209 GObjectClass *gobject_class = (GObjectClass*)klass;
210 parent_class = g_type_class_peek_parent(klass);
211
212 gobject_class->finalize = purple_media_manager_finalize;
213
214 purple_media_manager_signals[INIT_MEDIA] = g_signal_new ("init-media",
215 G_TYPE_FROM_CLASS (klass),
216 G_SIGNAL_RUN_LAST,
217 0, NULL, NULL,
218 purple_smarshal_BOOLEAN__OBJECT_POINTER_STRING,
219 G_TYPE_BOOLEAN, 3, PURPLE_TYPE_MEDIA,
220 G_TYPE_POINTER, G_TYPE_STRING);
221
222 purple_media_manager_signals[INIT_PRIVATE_MEDIA] =
223 g_signal_new ("init-private-media",
224 G_TYPE_FROM_CLASS (klass),
225 G_SIGNAL_RUN_LAST,
226 0, NULL, NULL,
227 purple_smarshal_BOOLEAN__OBJECT_POINTER_STRING,
228 G_TYPE_BOOLEAN, 3, PURPLE_TYPE_MEDIA,
229 G_TYPE_POINTER, G_TYPE_STRING);
230
231 purple_media_manager_signals[UI_CAPS_CHANGED] = g_signal_new ("ui-caps-changed",
232 G_TYPE_FROM_CLASS (klass),
233 G_SIGNAL_RUN_LAST,
234 0, NULL, NULL,
235 purple_smarshal_VOID__FLAGS_FLAGS,
236 G_TYPE_NONE, 2, PURPLE_MEDIA_TYPE_CAPS,
237 PURPLE_MEDIA_TYPE_CAPS);
238
239 purple_media_manager_signals[ELEMENTS_CHANGED] =
240 g_signal_new("elements-changed",
241 G_TYPE_FROM_CLASS(klass),
242 G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
243 0, NULL, NULL,
244 g_cclosure_marshal_VOID__VOID,
245 G_TYPE_NONE, 0);
246
247 g_type_class_add_private(klass, sizeof(PurpleMediaManagerPrivate));
248 }
249
250 static void
purple_media_manager_init(PurpleMediaManager * media)251 purple_media_manager_init (PurpleMediaManager *media)
252 {
253 #ifdef USE_GSTREAMER
254 GError *error;
255 #endif /* USE_GSTREAMER */
256
257 media->priv = PURPLE_MEDIA_MANAGER_GET_PRIVATE(media);
258 media->priv->medias = NULL;
259 media->priv->private_medias = NULL;
260 media->priv->next_output_window_id = 1;
261 media->priv->backend_type = PURPLE_TYPE_MEDIA_BACKEND_FS2;
262 #ifdef HAVE_MEDIA_APPLICATION
263 media->priv->appdata_info = NULL;
264 g_mutex_init (&media->priv->appdata_mutex);
265 #endif
266 #ifdef USE_GSTREAMER
267 if (gst_init_check(NULL, NULL, &error)) {
268 purple_media_manager_register_static_elements(media);
269 purple_media_manager_init_device_monitor(media);
270 } else {
271 purple_debug_error("mediamanager",
272 "GStreamer failed to initialize: %s.",
273 error ? error->message : "");
274 if (error) {
275 g_error_free(error);
276 }
277 }
278 #endif /* USE_GSTREAMER */
279
280 purple_prefs_add_none("/purple/media");
281 purple_prefs_add_none("/purple/media/audio");
282 purple_prefs_add_int("/purple/media/audio/silence_threshold", 5);
283 purple_prefs_add_none("/purple/media/audio/volume");
284 purple_prefs_add_int("/purple/media/audio/volume/input", 10);
285 purple_prefs_add_int("/purple/media/audio/volume/output", 10);
286 }
287
288 static void
purple_media_manager_finalize(GObject * media)289 purple_media_manager_finalize (GObject *media)
290 {
291 PurpleMediaManagerPrivate *priv = PURPLE_MEDIA_MANAGER_GET_PRIVATE(media);
292 for (; priv->medias; priv->medias =
293 g_list_delete_link(priv->medias, priv->medias)) {
294 g_object_unref(priv->medias->data);
295 }
296 for (; priv->private_medias; priv->private_medias =
297 g_list_delete_link(priv->private_medias, priv->private_medias)) {
298 g_object_unref(priv->private_medias->data);
299 }
300 for (; priv->elements; priv->elements =
301 g_list_delete_link(priv->elements, priv->elements)) {
302 g_object_unref(priv->elements->data);
303 }
304
305 g_free(priv->audio_src_id);
306 g_free(priv->video_src_id);
307 g_free(priv->audio_sink_id);
308 g_free(priv->video_sink_id);
309
310 if (priv->video_caps)
311 gst_caps_unref(priv->video_caps);
312 #ifdef HAVE_MEDIA_APPLICATION
313 if (priv->appdata_info)
314 g_list_free_full (priv->appdata_info,
315 (GDestroyNotify) free_appdata_info_locked);
316 g_mutex_clear (&priv->appdata_mutex);
317 #endif
318 #ifdef USE_GSTREAMER
319 #if GST_CHECK_VERSION(1, 4, 0)
320 if (priv->device_monitor) {
321 gst_device_monitor_stop(priv->device_monitor);
322 g_object_unref(priv->device_monitor);
323 }
324 #endif /* GST_CHECK_VERSION(1, 4, 0) */
325 #endif /* USE_GSTREAMER */
326
327 parent_class->finalize(media);
328 }
329 #endif
330
331 PurpleMediaManager *
purple_media_manager_get()332 purple_media_manager_get()
333 {
334 #ifdef USE_VV
335 static PurpleMediaManager *manager = NULL;
336
337 if (manager == NULL)
338 manager = PURPLE_MEDIA_MANAGER(g_object_new(purple_media_manager_get_type(), NULL));
339 return manager;
340 #else
341 return NULL;
342 #endif
343 }
344
345 #ifdef USE_VV
346 static gboolean
pipeline_bus_call(GstBus * bus,GstMessage * msg,PurpleMediaManager * manager)347 pipeline_bus_call(GstBus *bus, GstMessage *msg, PurpleMediaManager *manager)
348 {
349 switch(GST_MESSAGE_TYPE(msg)) {
350 case GST_MESSAGE_EOS:
351 purple_debug_info("mediamanager", "End of Stream\n");
352 break;
353 case GST_MESSAGE_ERROR: {
354 gchar *debug = NULL;
355 GError *err = NULL;
356
357 gst_message_parse_error(msg, &err, &debug);
358
359 purple_debug_error("mediamanager",
360 "gst pipeline error: %s\n",
361 err->message);
362 g_error_free(err);
363
364 if (debug) {
365 purple_debug_error("mediamanager",
366 "Debug details: %s\n", debug);
367 g_free (debug);
368 }
369 break;
370 }
371 default:
372 break;
373 }
374 return TRUE;
375 }
376 #endif
377
378 #ifdef USE_GSTREAMER
379 GstElement *
purple_media_manager_get_pipeline(PurpleMediaManager * manager)380 purple_media_manager_get_pipeline(PurpleMediaManager *manager)
381 {
382 #ifdef USE_VV
383 g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), NULL);
384
385 if (manager->priv->pipeline == NULL) {
386 FsElementAddedNotifier *notifier;
387 gchar *filename;
388 GError *err = NULL;
389 GKeyFile *keyfile;
390 GstBus *bus;
391 manager->priv->pipeline = gst_pipeline_new(NULL);
392
393 bus = gst_pipeline_get_bus(
394 GST_PIPELINE(manager->priv->pipeline));
395 gst_bus_add_signal_watch(GST_BUS(bus));
396 g_signal_connect(G_OBJECT(bus), "message",
397 G_CALLBACK(pipeline_bus_call), manager);
398 #if GST_CHECK_VERSION(1,0,0)
399 gst_bus_set_sync_handler(bus, gst_bus_sync_signal_handler, NULL, NULL);
400 #else
401 gst_bus_set_sync_handler(bus, gst_bus_sync_signal_handler, NULL);
402 #endif
403 gst_object_unref(bus);
404
405 filename = g_build_filename(purple_user_dir(),
406 "fs-element.conf", NULL);
407 keyfile = g_key_file_new();
408 if (!g_key_file_load_from_file(keyfile, filename,
409 G_KEY_FILE_NONE, &err)) {
410 if (err->code == 4)
411 purple_debug_info("mediamanager",
412 "Couldn't read "
413 "fs-element.conf: %s\n",
414 err->message);
415 else
416 purple_debug_error("mediamanager",
417 "Error reading "
418 "fs-element.conf: %s\n",
419 err->message);
420 g_error_free(err);
421 }
422 g_free(filename);
423
424 /* Hack to make alsasrc stop messing up audio timestamps */
425 if (!g_key_file_has_key(keyfile,
426 "alsasrc", "slave-method", NULL)) {
427 g_key_file_set_integer(keyfile,
428 "alsasrc", "slave-method", 2);
429 }
430
431 notifier = fs_element_added_notifier_new();
432 fs_element_added_notifier_add(notifier,
433 GST_BIN(manager->priv->pipeline));
434 fs_element_added_notifier_set_properties_from_keyfile(
435 notifier, keyfile);
436
437 gst_element_set_state(manager->priv->pipeline,
438 GST_STATE_PLAYING);
439 }
440
441 return manager->priv->pipeline;
442 #else
443 return NULL;
444 #endif
445 }
446 #endif /* USE_GSTREAMER */
447
448 static PurpleMedia *
create_media(PurpleMediaManager * manager,PurpleAccount * account,const char * conference_type,const char * remote_user,gboolean initiator,gboolean private)449 create_media(PurpleMediaManager *manager,
450 PurpleAccount *account,
451 const char *conference_type,
452 const char *remote_user,
453 gboolean initiator,
454 gboolean private)
455 {
456 #ifdef USE_VV
457 PurpleMedia *media;
458 guint signal_id;
459
460 media = PURPLE_MEDIA(g_object_new(purple_media_get_type(),
461 "manager", manager,
462 "account", account,
463 "conference-type", conference_type,
464 "initiator", initiator,
465 NULL));
466
467 signal_id = private ?
468 purple_media_manager_signals[INIT_PRIVATE_MEDIA] :
469 purple_media_manager_signals[INIT_MEDIA];
470
471 if (g_signal_has_handler_pending(manager, signal_id, 0, FALSE)) {
472 gboolean signal_ret;
473
474 g_signal_emit(manager, signal_id, 0, media, account, remote_user,
475 &signal_ret);
476 if (signal_ret == FALSE) {
477 g_object_unref(media);
478 return NULL;
479 }
480 }
481
482 if (private)
483 manager->priv->private_medias = g_list_append(
484 manager->priv->private_medias, media);
485 else
486 manager->priv->medias = g_list_append(manager->priv->medias, media);
487 return media;
488 #else
489 return NULL;
490 #endif
491 }
492
493 static GList *
get_media(PurpleMediaManager * manager,gboolean private)494 get_media(PurpleMediaManager *manager, gboolean private)
495 {
496 #ifdef USE_VV
497 if (private)
498 return manager->priv->private_medias;
499 else
500 return manager->priv->medias;
501 #else
502 return NULL;
503 #endif
504 }
505
506 static GList *
get_media_by_account(PurpleMediaManager * manager,PurpleAccount * account,gboolean private)507 get_media_by_account(PurpleMediaManager *manager,
508 PurpleAccount *account, gboolean private)
509 {
510 #ifdef USE_VV
511 GList *media = NULL;
512 GList *iter;
513
514 g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), NULL);
515
516 if (private)
517 iter = manager->priv->private_medias;
518 else
519 iter = manager->priv->medias;
520 for (; iter; iter = g_list_next(iter)) {
521 if (purple_media_get_account(iter->data) == account) {
522 media = g_list_prepend(media, iter->data);
523 }
524 }
525
526 return media;
527 #else
528 return NULL;
529 #endif
530 }
531
532 void
purple_media_manager_remove_media(PurpleMediaManager * manager,PurpleMedia * media)533 purple_media_manager_remove_media(PurpleMediaManager *manager, PurpleMedia *media)
534 {
535 #ifdef USE_VV
536 GList *list;
537 GList **medias;
538
539 g_return_if_fail(manager != NULL);
540
541 if ((list = g_list_find(manager->priv->medias, media))) {
542 medias = &manager->priv->medias;
543 } else if ((list = g_list_find(manager->priv->private_medias, media))) {
544 medias = &manager->priv->private_medias;
545 }
546
547 if (list) {
548 *medias = g_list_delete_link(*medias, list);
549
550 #ifdef HAVE_MEDIA_APPLICATION
551 g_mutex_lock (&manager->priv->appdata_mutex);
552 list = manager->priv->appdata_info;
553 while (list) {
554 PurpleMediaAppDataInfo *info = list->data;
555 GList *next = list->next;
556
557 if (info->media == media) {
558 manager->priv->appdata_info = g_list_delete_link (
559 manager->priv->appdata_info, list);
560 free_appdata_info_locked (info);
561 }
562
563 list = next;
564 }
565 g_mutex_unlock (&manager->priv->appdata_mutex);
566 #endif
567 }
568 #endif
569 }
570
571 PurpleMedia *
purple_media_manager_create_media(PurpleMediaManager * manager,PurpleAccount * account,const char * conference_type,const char * remote_user,gboolean initiator)572 purple_media_manager_create_media(PurpleMediaManager *manager,
573 PurpleAccount *account,
574 const char *conference_type,
575 const char *remote_user,
576 gboolean initiator)
577 {
578 return create_media (manager, account, conference_type,
579 remote_user, initiator, FALSE);
580 }
581
582 GList *
purple_media_manager_get_media(PurpleMediaManager * manager)583 purple_media_manager_get_media(PurpleMediaManager *manager)
584 {
585 return get_media (manager, FALSE);
586 }
587
588 GList *
purple_media_manager_get_media_by_account(PurpleMediaManager * manager,PurpleAccount * account)589 purple_media_manager_get_media_by_account(PurpleMediaManager *manager,
590 PurpleAccount *account)
591 {
592 return get_media_by_account (manager, account, FALSE);
593 }
594
595 PurpleMedia *
purple_media_manager_create_private_media(PurpleMediaManager * manager,PurpleAccount * account,const char * conference_type,const char * remote_user,gboolean initiator)596 purple_media_manager_create_private_media(PurpleMediaManager *manager,
597 PurpleAccount *account,
598 const char *conference_type,
599 const char *remote_user,
600 gboolean initiator)
601 {
602 return create_media (manager, account, conference_type,
603 remote_user, initiator, TRUE);
604 }
605
606 GList *
purple_media_manager_get_private_media(PurpleMediaManager * manager)607 purple_media_manager_get_private_media(PurpleMediaManager *manager)
608 {
609 return get_media (manager, TRUE);
610 }
611
612 GList *
purple_media_manager_get_private_media_by_account(PurpleMediaManager * manager,PurpleAccount * account)613 purple_media_manager_get_private_media_by_account(PurpleMediaManager *manager,
614 PurpleAccount *account)
615 {
616 return get_media_by_account (manager, account, TRUE);
617 }
618
619 #ifdef HAVE_MEDIA_APPLICATION
620 static void
free_appdata_info_locked(PurpleMediaAppDataInfo * info)621 free_appdata_info_locked (PurpleMediaAppDataInfo *info)
622 {
623 GstAppSrcCallbacks null_src_cb = { NULL, NULL, NULL, { NULL } };
624 GstAppSinkCallbacks null_sink_cb = { NULL, NULL, NULL , { NULL } };
625
626 if (info->notify)
627 info->notify (info->user_data);
628
629 info->media = NULL;
630 if (info->appsrc) {
631 /* Will call appsrc_destroyed. */
632 gst_app_src_set_callbacks (info->appsrc, &null_src_cb,
633 NULL, NULL);
634 }
635 if (info->appsink) {
636 /* Will call appsink_destroyed. */
637 gst_app_sink_set_callbacks (info->appsink, &null_sink_cb,
638 NULL, NULL);
639 }
640
641 /* Make sure no other thread is using the structure */
642 g_free (info->session_id);
643 g_free (info->participant);
644
645 /* Zeroing out *_cb_token lets the read or write callbacks waiting for
646 * appdata_mutex know the info structure has been destroyed. */
647
648 if (info->readable_cb_token) {
649 purple_timeout_remove (info->readable_timer_id);
650 info->readable_cb_token = 0;
651 }
652
653 if (info->writable_cb_token) {
654 purple_timeout_remove (info->writable_timer_id);
655 info->writable_cb_token = 0;
656 }
657
658 if (info->current_sample)
659 gst_sample_unref (info->current_sample);
660 info->current_sample = NULL;
661
662 /* Unblock any reading thread before destroying the GCond */
663 g_cond_broadcast (&info->readable_cond);
664
665 g_cond_clear (&info->readable_cond);
666
667 g_slice_free (PurpleMediaAppDataInfo, info);
668 }
669
670 /*
671 * Get an app data info struct associated with a session and lock the mutex
672 * We don't want to return an info struct and unlock then it gets destroyed
673 * so we need to return it with the lock still taken
674 */
675 static PurpleMediaAppDataInfo *
get_app_data_info_and_lock(PurpleMediaManager * manager,PurpleMedia * media,const gchar * session_id,const gchar * participant)676 get_app_data_info_and_lock (PurpleMediaManager *manager,
677 PurpleMedia *media, const gchar *session_id, const gchar *participant)
678 {
679 GList *i;
680
681 g_mutex_lock (&manager->priv->appdata_mutex);
682 for (i = manager->priv->appdata_info; i; i = i->next) {
683 PurpleMediaAppDataInfo *info = i->data;
684
685 if (info->media == media &&
686 purple_strequal (info->session_id, session_id) &&
687 (participant == NULL ||
688 purple_strequal (info->participant, participant))) {
689 return info;
690 }
691 }
692
693 return NULL;
694 }
695
696 /*
697 * Get an app data info struct associated with a session and lock the mutex
698 * if it doesn't exist, we create it.
699 */
700 static PurpleMediaAppDataInfo *
ensure_app_data_info_and_lock(PurpleMediaManager * manager,PurpleMedia * media,const gchar * session_id,const gchar * participant)701 ensure_app_data_info_and_lock (PurpleMediaManager *manager, PurpleMedia *media,
702 const gchar *session_id, const gchar *participant)
703 {
704 PurpleMediaAppDataInfo * info = get_app_data_info_and_lock (manager, media,
705 session_id, participant);
706
707 if (info == NULL) {
708 info = g_slice_new0 (PurpleMediaAppDataInfo);
709 info->media = media;
710 g_weak_ref_init (&info->media_ref, media);
711 info->session_id = g_strdup (session_id);
712 info->participant = g_strdup (participant);
713 g_cond_init (&info->readable_cond);
714 manager->priv->appdata_info = g_list_prepend (
715 manager->priv->appdata_info, info);
716 }
717
718 return info;
719 }
720 #endif
721
722
723 #ifdef USE_VV
724 static void
request_pad_unlinked_cb(GstPad * pad,GstPad * peer,gpointer user_data)725 request_pad_unlinked_cb(GstPad *pad, GstPad *peer, gpointer user_data)
726 {
727 GstElement *parent = GST_ELEMENT_PARENT(pad);
728 GstIterator *iter;
729 #if GST_CHECK_VERSION(1,0,0)
730 GValue tmp = G_VALUE_INIT;
731 #else
732 GstPad *remaining_pad;
733 #endif
734 GstIteratorResult result;
735
736 gst_element_release_request_pad(parent, pad);
737
738 iter = gst_element_iterate_src_pads(parent);
739
740 #if GST_CHECK_VERSION(1,0,0)
741 result = gst_iterator_next(iter, &tmp);
742 #else
743 result = gst_iterator_next(iter, (gpointer)&remaining_pad);
744 #endif
745
746 if (result == GST_ITERATOR_DONE) {
747 gst_element_set_locked_state(parent, TRUE);
748 gst_element_set_state(parent, GST_STATE_NULL);
749 gst_bin_remove(GST_BIN(GST_ELEMENT_PARENT(parent)), parent);
750 } else if (result == GST_ITERATOR_OK) {
751 #if GST_CHECK_VERSION(1,0,0)
752 g_value_reset(&tmp);
753 #else
754 gst_object_unref(remaining_pad);
755 #endif
756 }
757
758 gst_iterator_free(iter);
759 }
760
761 static void
nonunique_src_unlinked_cb(GstPad * pad,GstPad * peer,gpointer user_data)762 nonunique_src_unlinked_cb(GstPad *pad, GstPad *peer, gpointer user_data)
763 {
764 GstElement *element = GST_ELEMENT_PARENT(pad);
765 gst_element_set_locked_state(element, TRUE);
766 gst_element_set_state(element, GST_STATE_NULL);
767 gst_bin_remove(GST_BIN(GST_ELEMENT_PARENT(element)), element);
768 }
769
770 #endif
771
772 #ifdef USE_GSTREAMER
773
774 void
purple_media_manager_set_video_caps(PurpleMediaManager * manager,GstCaps * caps)775 purple_media_manager_set_video_caps(PurpleMediaManager *manager, GstCaps *caps)
776 {
777 #ifdef USE_VV
778 if (manager->priv->video_caps)
779 gst_caps_unref(manager->priv->video_caps);
780
781 manager->priv->video_caps = caps;
782
783 if (manager->priv->pipeline && manager->priv->video_src) {
784 gchar *id = purple_media_element_info_get_id(manager->priv->video_src);
785 GstElement *src = gst_bin_get_by_name(GST_BIN(manager->priv->pipeline), id);
786
787 if (src) {
788 GstElement *capsfilter = gst_bin_get_by_name(GST_BIN(src), "prpl_video_caps");
789 if (capsfilter) {
790 g_object_set(G_OBJECT(capsfilter), "caps", caps, NULL);
791 gst_object_unref (capsfilter);
792 }
793 gst_object_unref (src);
794 }
795
796 g_free(id);
797 }
798 #endif
799 }
800
801 GstCaps *
purple_media_manager_get_video_caps(PurpleMediaManager * manager)802 purple_media_manager_get_video_caps(PurpleMediaManager *manager)
803 {
804 #ifdef USE_VV
805 if (manager->priv->video_caps == NULL)
806 #if GST_CHECK_VERSION(1,0,0)
807 manager->priv->video_caps = gst_caps_from_string("video/x-raw,"
808 #else
809 manager->priv->video_caps = gst_caps_from_string("video/x-raw-yuv,"
810 #endif
811 "width=[250,352], height=[200,288], framerate=[1/1,20/1]");
812 return manager->priv->video_caps;
813 #else
814 return NULL;
815 #endif
816 }
817
818 #ifdef HAVE_MEDIA_APPLICATION
819 /*
820 * Calls the appdata writable callback from the main thread.
821 * This needs to grab the appdata lock and make sure it didn't get destroyed
822 * before calling the callback.
823 */
824 static gboolean
appsrc_writable(gpointer user_data)825 appsrc_writable (gpointer user_data)
826 {
827 PurpleMediaManager *manager = purple_media_manager_get ();
828 PurpleMediaAppDataInfo *info = user_data;
829 void (*writable_cb) (PurpleMediaManager *manager, PurpleMedia *media,
830 const gchar *session_id, const gchar *participant, gboolean writable,
831 gpointer user_data);
832 PurpleMedia *media;
833 gchar *session_id;
834 gchar *participant;
835 gboolean writable;
836 gpointer cb_data;
837 guint *cb_token_ptr = &info->writable_cb_token;
838 guint cb_token = *cb_token_ptr;
839
840
841 g_mutex_lock (&manager->priv->appdata_mutex);
842 if (cb_token == 0 || cb_token != *cb_token_ptr) {
843 /* In case info was freed while we were waiting for the mutex to unlock
844 * we still have a pointer to the cb_token which should still be
845 * accessible since it's in the Glib slice allocator. It gets set to 0
846 * just after the timeout is canceled which happens also before the
847 * AppDataInfo is freed, so even if that memory slice gets reused, the
848 * cb_token would be different from its previous value (unless
849 * extremely unlucky). So checking if the value for the cb_token changed
850 * should be enough to prevent any kind of race condition in which the
851 * media/AppDataInfo gets destroyed in one thread while the timeout was
852 * triggered and is waiting on the mutex to get unlocked in this thread
853 */
854 g_mutex_unlock (&manager->priv->appdata_mutex);
855 return FALSE;
856 }
857 writable_cb = info->callbacks.writable;
858 media = g_weak_ref_get (&info->media_ref);
859 session_id = g_strdup (info->session_id);
860 participant = g_strdup (info->participant);
861 writable = info->writable && info->connected;
862 cb_data = info->user_data;
863
864 info->writable_cb_token = 0;
865 g_mutex_unlock (&manager->priv->appdata_mutex);
866
867
868 if (writable_cb && media)
869 writable_cb (manager, media, session_id, participant, writable,
870 cb_data);
871
872 g_object_unref (media);
873 g_free (session_id);
874 g_free (participant);
875
876 return FALSE;
877 }
878
879 /*
880 * Schedule a writable callback to be called from the main thread.
881 * We need to do this because need-data/enough-data signals from appsrc
882 * will come from the streaming thread and we need to create
883 * a source that we attach to the main context but we can't use
884 * g_main_context_invoke since we need to be able to cancel the source if the
885 * media gets destroyed.
886 * We use a timeout source instead of idle source, so the callback gets a higher
887 * priority
888 */
889 static void
call_appsrc_writable_locked(PurpleMediaAppDataInfo * info)890 call_appsrc_writable_locked (PurpleMediaAppDataInfo *info)
891 {
892 PurpleMediaManager *manager = purple_media_manager_get ();
893
894 /* We already have a writable callback scheduled, don't create another one */
895 if (info->writable_cb_token || info->callbacks.writable == NULL)
896 return;
897
898 /* We can't use writable_timer_id as a token, because the timeout is added
899 * into libpurple's main event loop, which runs in a different thread than
900 * from where call_appsrc_writable_locked() was called. Consequently, the
901 * callback may run even before purple_timeout_add() returns the timer ID
902 * to us. */
903 info->writable_cb_token = ++manager->priv->appdata_cb_token;
904 info->writable_timer_id = purple_timeout_add (0, appsrc_writable, info);
905 }
906
907 static void
appsrc_need_data(GstAppSrc * appsrc,guint length,gpointer user_data)908 appsrc_need_data (GstAppSrc *appsrc, guint length, gpointer user_data)
909 {
910 PurpleMediaAppDataInfo *info = user_data;
911 PurpleMediaManager *manager = purple_media_manager_get ();
912
913 g_mutex_lock (&manager->priv->appdata_mutex);
914 if (!info->writable) {
915 info->writable = TRUE;
916 /* Only signal writable if we also established a connection */
917 if (info->connected)
918 call_appsrc_writable_locked (info);
919 }
920 g_mutex_unlock (&manager->priv->appdata_mutex);
921 }
922
923 static void
appsrc_enough_data(GstAppSrc * appsrc,gpointer user_data)924 appsrc_enough_data (GstAppSrc *appsrc, gpointer user_data)
925 {
926 PurpleMediaAppDataInfo *info = user_data;
927 PurpleMediaManager *manager = purple_media_manager_get ();
928
929 g_mutex_lock (&manager->priv->appdata_mutex);
930 if (info->writable) {
931 info->writable = FALSE;
932 call_appsrc_writable_locked (info);
933 }
934 g_mutex_unlock (&manager->priv->appdata_mutex);
935 }
936
937 static gboolean
appsrc_seek_data(GstAppSrc * appsrc,guint64 offset,gpointer user_data)938 appsrc_seek_data (GstAppSrc *appsrc, guint64 offset, gpointer user_data)
939 {
940 return FALSE;
941 }
942
943 static void
appsrc_destroyed(PurpleMediaAppDataInfo * info)944 appsrc_destroyed (PurpleMediaAppDataInfo *info)
945 {
946 PurpleMediaManager *manager;
947
948 if (!info->media) {
949 /* PurpleMediaAppDataInfo is being freed. Return at once. */
950 return;
951 }
952
953 manager = purple_media_manager_get ();
954
955 g_mutex_lock (&manager->priv->appdata_mutex);
956 info->appsrc = NULL;
957 if (info->writable) {
958 info->writable = FALSE;
959 call_appsrc_writable_locked (info);
960 }
961 g_mutex_unlock (&manager->priv->appdata_mutex);
962 }
963
964 static void
media_established_cb(PurpleMedia * media,const gchar * session_id,const gchar * participant,PurpleMediaCandidate * local_candidate,PurpleMediaCandidate * remote_candidate,PurpleMediaAppDataInfo * info)965 media_established_cb (PurpleMedia *media,const gchar *session_id,
966 const gchar *participant, PurpleMediaCandidate *local_candidate,
967 PurpleMediaCandidate *remote_candidate, PurpleMediaAppDataInfo *info)
968 {
969 PurpleMediaManager *manager = purple_media_manager_get ();
970
971 g_mutex_lock (&manager->priv->appdata_mutex);
972 info->connected = TRUE;
973 /* We established the connection, if we were writable, then we need to
974 * signal it now */
975 if (info->writable)
976 call_appsrc_writable_locked (info);
977 g_mutex_unlock (&manager->priv->appdata_mutex);
978 }
979
980 static GstElement *
create_send_appsrc(PurpleMedia * media,const gchar * session_id,const gchar * participant)981 create_send_appsrc(PurpleMedia *media,
982 const gchar *session_id, const gchar *participant)
983 {
984 PurpleMediaManager *manager = purple_media_manager_get ();
985 PurpleMediaAppDataInfo * info = ensure_app_data_info_and_lock (manager,
986 media, session_id, participant);
987 GstElement *appsrc = (GstElement *)info->appsrc;
988
989 if (appsrc == NULL) {
990 GstAppSrcCallbacks callbacks = {appsrc_need_data, appsrc_enough_data,
991 appsrc_seek_data, {NULL}};
992 GstCaps *caps = gst_caps_new_empty_simple ("application/octet-stream");
993
994 appsrc = gst_element_factory_make("appsrc", NULL);
995
996 info->appsrc = (GstAppSrc *)appsrc;
997
998 gst_app_src_set_caps (info->appsrc, caps);
999 gst_app_src_set_callbacks (info->appsrc,
1000 &callbacks, info, (GDestroyNotify) appsrc_destroyed);
1001 g_signal_connect (media, "candidate-pair-established",
1002 (GCallback) media_established_cb, info);
1003 gst_caps_unref (caps);
1004 }
1005
1006 g_mutex_unlock (&manager->priv->appdata_mutex);
1007 return appsrc;
1008 }
1009
1010 static void
appsink_eos(GstAppSink * appsink,gpointer user_data)1011 appsink_eos (GstAppSink *appsink, gpointer user_data)
1012 {
1013 }
1014
1015 static GstFlowReturn
appsink_new_preroll(GstAppSink * appsink,gpointer user_data)1016 appsink_new_preroll (GstAppSink *appsink, gpointer user_data)
1017 {
1018 return GST_FLOW_OK;
1019 }
1020
1021 static gboolean
appsink_readable(gpointer user_data)1022 appsink_readable (gpointer user_data)
1023 {
1024 PurpleMediaManager *manager = purple_media_manager_get ();
1025 PurpleMediaAppDataInfo *info = user_data;
1026 void (*readable_cb) (PurpleMediaManager *manager, PurpleMedia *media,
1027 const gchar *session_id, const gchar *participant, gpointer user_data);
1028 PurpleMedia *media;
1029 gchar *session_id;
1030 gchar *participant;
1031 gpointer cb_data;
1032 guint *cb_token_ptr = &info->readable_cb_token;
1033 guint cb_token = *cb_token_ptr;
1034 gboolean run_again = FALSE;
1035
1036 g_mutex_lock (&manager->priv->appdata_mutex);
1037 if (cb_token == 0 || cb_token != *cb_token_ptr) {
1038 /* Avoided a race condition (see writable callback) */
1039 g_mutex_unlock (&manager->priv->appdata_mutex);
1040 return FALSE;
1041 }
1042
1043 if (info->callbacks.readable &&
1044 (info->num_samples > 0 || info->current_sample != NULL)) {
1045 readable_cb = info->callbacks.readable;
1046 media = g_weak_ref_get (&info->media_ref);
1047 session_id = g_strdup (info->session_id);
1048 participant = g_strdup (info->participant);
1049 cb_data = info->user_data;
1050 g_mutex_unlock (&manager->priv->appdata_mutex);
1051
1052 if (readable_cb)
1053 readable_cb (manager, media, session_id, participant, cb_data);
1054
1055 g_mutex_lock (&manager->priv->appdata_mutex);
1056 g_object_unref (media);
1057 g_free (session_id);
1058 g_free (participant);
1059 if (cb_token == 0 || cb_token != *cb_token_ptr) {
1060 /* We got cancelled */
1061 g_mutex_unlock (&manager->priv->appdata_mutex);
1062 return FALSE;
1063 }
1064 }
1065
1066 /* Do we still have samples? Schedule appsink_readable again. We break here
1067 * so that other events get a chance to be processed too. */
1068 if (info->num_samples > 0 || info->current_sample != NULL) {
1069 run_again = TRUE;
1070 } else {
1071 info->readable_cb_token = 0;
1072 }
1073
1074 g_mutex_unlock (&manager->priv->appdata_mutex);
1075 return run_again;
1076 }
1077
1078 static void
call_appsink_readable_locked(PurpleMediaAppDataInfo * info)1079 call_appsink_readable_locked (PurpleMediaAppDataInfo *info)
1080 {
1081 PurpleMediaManager *manager = purple_media_manager_get ();
1082
1083 /* We must signal that a new sample has arrived to release blocking reads */
1084 g_cond_broadcast (&info->readable_cond);
1085
1086 /* We already have a writable callback scheduled, don't create another one */
1087 if (info->readable_cb_token || info->callbacks.readable == NULL)
1088 return;
1089
1090 info->readable_cb_token = ++manager->priv->appdata_cb_token;
1091 info->readable_timer_id = purple_timeout_add (0, appsink_readable, info);
1092 }
1093
1094 static GstFlowReturn
appsink_new_sample(GstAppSink * appsink,gpointer user_data)1095 appsink_new_sample (GstAppSink *appsink, gpointer user_data)
1096 {
1097 PurpleMediaManager *manager = purple_media_manager_get ();
1098 PurpleMediaAppDataInfo *info = user_data;
1099
1100 g_mutex_lock (&manager->priv->appdata_mutex);
1101 info->num_samples++;
1102 call_appsink_readable_locked (info);
1103 g_mutex_unlock (&manager->priv->appdata_mutex);
1104
1105 return GST_FLOW_OK;
1106 }
1107
1108 static void
appsink_destroyed(PurpleMediaAppDataInfo * info)1109 appsink_destroyed (PurpleMediaAppDataInfo *info)
1110 {
1111 PurpleMediaManager *manager;
1112
1113 if (!info->media) {
1114 /* PurpleMediaAppDataInfo is being freed. Return at once. */
1115 return;
1116 }
1117
1118 manager = purple_media_manager_get ();
1119
1120 g_mutex_lock (&manager->priv->appdata_mutex);
1121 info->appsink = NULL;
1122 info->num_samples = 0;
1123 g_mutex_unlock (&manager->priv->appdata_mutex);
1124 }
1125
1126 static GstElement *
create_recv_appsink(PurpleMedia * media,const gchar * session_id,const gchar * participant)1127 create_recv_appsink(PurpleMedia *media,
1128 const gchar *session_id, const gchar *participant)
1129 {
1130 PurpleMediaManager *manager = purple_media_manager_get ();
1131 PurpleMediaAppDataInfo * info = ensure_app_data_info_and_lock (manager,
1132 media, session_id, participant);
1133 GstElement *appsink = (GstElement *)info->appsink;
1134
1135 if (appsink == NULL) {
1136 GstAppSinkCallbacks callbacks = {appsink_eos, appsink_new_preroll,
1137 appsink_new_sample, {NULL}};
1138 GstCaps *caps = gst_caps_new_empty_simple ("application/octet-stream");
1139
1140 appsink = gst_element_factory_make("appsink", NULL);
1141
1142 info->appsink = (GstAppSink *)appsink;
1143
1144 gst_app_sink_set_caps (info->appsink, caps);
1145 gst_app_sink_set_callbacks (info->appsink,
1146 &callbacks, info, (GDestroyNotify) appsink_destroyed);
1147 gst_caps_unref (caps);
1148
1149 }
1150
1151 g_mutex_unlock (&manager->priv->appdata_mutex);
1152 return appsink;
1153 }
1154 #endif
1155
1156 static PurpleMediaElementInfo *
get_send_application_element_info()1157 get_send_application_element_info ()
1158 {
1159 static PurpleMediaElementInfo *info = NULL;
1160
1161 #ifdef HAVE_MEDIA_APPLICATION
1162 if (info == NULL) {
1163 info = g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
1164 "id", "pidginappsrc",
1165 "name", "Pidgin Application Source",
1166 "type", PURPLE_MEDIA_ELEMENT_APPLICATION
1167 | PURPLE_MEDIA_ELEMENT_SRC
1168 | PURPLE_MEDIA_ELEMENT_ONE_SRC,
1169 "create-cb", create_send_appsrc, NULL);
1170 }
1171 #endif
1172
1173 return info;
1174 }
1175
1176
1177 static PurpleMediaElementInfo *
get_recv_application_element_info()1178 get_recv_application_element_info ()
1179 {
1180 static PurpleMediaElementInfo *info = NULL;
1181
1182 #ifdef HAVE_MEDIA_APPLICATION
1183 if (info == NULL) {
1184 info = g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
1185 "id", "pidginappsink",
1186 "name", "Pidgin Application Sink",
1187 "type", PURPLE_MEDIA_ELEMENT_APPLICATION
1188 | PURPLE_MEDIA_ELEMENT_SINK
1189 | PURPLE_MEDIA_ELEMENT_ONE_SINK,
1190 "create-cb", create_recv_appsink, NULL);
1191 }
1192 #endif
1193
1194 return info;
1195 }
1196
1197 GstElement *
purple_media_manager_get_element(PurpleMediaManager * manager,PurpleMediaSessionType type,PurpleMedia * media,const gchar * session_id,const gchar * participant)1198 purple_media_manager_get_element(PurpleMediaManager *manager,
1199 PurpleMediaSessionType type, PurpleMedia *media,
1200 const gchar *session_id, const gchar *participant)
1201 {
1202 #ifdef USE_VV
1203 GstElement *ret = NULL;
1204 PurpleMediaElementInfo *info = NULL;
1205 PurpleMediaElementType element_type;
1206
1207 if (type & PURPLE_MEDIA_SEND)
1208 info = g_object_get_data(G_OBJECT(media), "src-element");
1209 else
1210 info = g_object_get_data(G_OBJECT(media), "sink-element");
1211
1212 if (info == NULL) {
1213 if (type & PURPLE_MEDIA_SEND_AUDIO)
1214 info = manager->priv->audio_src;
1215 else if (type & PURPLE_MEDIA_RECV_AUDIO)
1216 info = manager->priv->audio_sink;
1217 else if (type & PURPLE_MEDIA_SEND_VIDEO)
1218 info = manager->priv->video_src;
1219 else if (type & PURPLE_MEDIA_RECV_VIDEO)
1220 info = manager->priv->video_sink;
1221 else if (type & PURPLE_MEDIA_SEND_APPLICATION)
1222 info = get_send_application_element_info ();
1223 else if (type & PURPLE_MEDIA_RECV_APPLICATION)
1224 info = get_recv_application_element_info ();
1225 }
1226
1227 if (info == NULL)
1228 return NULL;
1229
1230 element_type = purple_media_element_info_get_element_type(info);
1231
1232 if (element_type & PURPLE_MEDIA_ELEMENT_UNIQUE &&
1233 element_type & PURPLE_MEDIA_ELEMENT_SRC) {
1234 GstElement *tee;
1235 GstPad *pad;
1236 GstPad *ghost;
1237 gchar *id = purple_media_element_info_get_id(info);
1238
1239 ret = gst_bin_get_by_name(GST_BIN(
1240 purple_media_manager_get_pipeline(
1241 manager)), id);
1242
1243 if (ret == NULL) {
1244 GstElement *bin, *fakesink;
1245 ret = purple_media_element_info_call_create(info,
1246 media, session_id, participant);
1247 bin = gst_bin_new(id);
1248 tee = gst_element_factory_make("tee", "tee");
1249 gst_bin_add_many(GST_BIN(bin), ret, tee, NULL);
1250
1251 if (type & PURPLE_MEDIA_SEND_VIDEO) {
1252 GstElement *videoscale;
1253 GstElement *capsfilter;
1254
1255 videoscale = gst_element_factory_make("videoscale", NULL);
1256 capsfilter = gst_element_factory_make("capsfilter", "prpl_video_caps");
1257
1258 g_object_set(G_OBJECT(capsfilter),
1259 "caps", purple_media_manager_get_video_caps(manager), NULL);
1260
1261 gst_bin_add_many(GST_BIN(bin), videoscale, capsfilter, NULL);
1262 gst_element_link_many(ret, videoscale, capsfilter, tee, NULL);
1263 } else
1264 gst_element_link(ret, tee);
1265
1266 /*
1267 * This shouldn't be necessary, but it stops it from
1268 * giving a not-linked error upon destruction
1269 */
1270 fakesink = gst_element_factory_make("fakesink", NULL);
1271 g_object_set(fakesink,
1272 "async", FALSE,
1273 "sync", FALSE,
1274 "enable-last-sample", FALSE,
1275 NULL);
1276 gst_bin_add(GST_BIN(bin), fakesink);
1277 gst_element_link(tee, fakesink);
1278
1279 ret = bin;
1280 gst_object_ref(ret);
1281 gst_bin_add(GST_BIN(purple_media_manager_get_pipeline(
1282 manager)), ret);
1283 }
1284 g_free(id);
1285
1286 tee = gst_bin_get_by_name(GST_BIN(ret), "tee");
1287 #if GST_CHECK_VERSION(1,0,0)
1288 pad = gst_element_get_request_pad(tee, "src_%u");
1289 #else
1290 pad = gst_element_get_request_pad(tee, "src%d");
1291 #endif
1292 gst_object_unref(tee);
1293 ghost = gst_ghost_pad_new(NULL, pad);
1294 gst_object_unref(pad);
1295 g_signal_connect(GST_PAD(ghost), "unlinked",
1296 G_CALLBACK(request_pad_unlinked_cb), NULL);
1297 gst_pad_set_active(ghost, TRUE);
1298 gst_element_add_pad(ret, ghost);
1299 } else {
1300 ret = purple_media_element_info_call_create(info,
1301 media, session_id, participant);
1302 if (element_type & PURPLE_MEDIA_ELEMENT_SRC) {
1303 GstPad *pad = gst_element_get_static_pad(ret, "src");
1304 g_signal_connect(pad, "unlinked",
1305 G_CALLBACK(nonunique_src_unlinked_cb), NULL);
1306 gst_object_unref(pad);
1307 gst_object_ref(ret);
1308 gst_bin_add(GST_BIN(purple_media_manager_get_pipeline(manager)),
1309 ret);
1310 }
1311 }
1312
1313 if (ret == NULL)
1314 purple_debug_error("media", "Error creating source or sink\n");
1315
1316 return ret;
1317 #else
1318 return NULL;
1319 #endif
1320 }
1321
1322 PurpleMediaElementInfo *
purple_media_manager_get_element_info(PurpleMediaManager * manager,const gchar * id)1323 purple_media_manager_get_element_info(PurpleMediaManager *manager,
1324 const gchar *id)
1325 {
1326 #ifdef USE_VV
1327 GList *iter;
1328
1329 g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), NULL);
1330
1331 iter = manager->priv->elements;
1332
1333 for (; iter; iter = g_list_next(iter)) {
1334 gchar *element_id =
1335 purple_media_element_info_get_id(iter->data);
1336 if (purple_strequal(element_id, id)) {
1337 g_free(element_id);
1338 g_object_ref(iter->data);
1339 return iter->data;
1340 }
1341 g_free(element_id);
1342 }
1343 #endif
1344
1345 return NULL;
1346 }
1347
1348 static GQuark
element_info_to_detail(PurpleMediaElementInfo * info)1349 element_info_to_detail(PurpleMediaElementInfo *info)
1350 {
1351 PurpleMediaElementType type;
1352
1353 type = purple_media_element_info_get_element_type(info);
1354
1355 if (type & PURPLE_MEDIA_ELEMENT_AUDIO) {
1356 if (type & PURPLE_MEDIA_ELEMENT_SRC) {
1357 return g_quark_from_string("audiosrc");
1358 } else if (type & PURPLE_MEDIA_ELEMENT_SINK) {
1359 return g_quark_from_string("audiosink");
1360 }
1361 } else if (type & PURPLE_MEDIA_ELEMENT_VIDEO) {
1362 if (type & PURPLE_MEDIA_ELEMENT_SRC) {
1363 return g_quark_from_string("videosrc");
1364 } else if (type & PURPLE_MEDIA_ELEMENT_SINK) {
1365 return g_quark_from_string("videosink");
1366 }
1367 }
1368
1369 return 0;
1370 }
1371
1372 gboolean
purple_media_manager_register_element(PurpleMediaManager * manager,PurpleMediaElementInfo * info)1373 purple_media_manager_register_element(PurpleMediaManager *manager,
1374 PurpleMediaElementInfo *info)
1375 {
1376 #ifdef USE_VV
1377 PurpleMediaElementInfo *info2;
1378 PurpleMediaElementType type;
1379 gchar *id;
1380 GQuark detail;
1381
1382 g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), FALSE);
1383 g_return_val_if_fail(info != NULL, FALSE);
1384
1385 id = purple_media_element_info_get_id(info);
1386 info2 = purple_media_manager_get_element_info(manager, id);
1387
1388 if (info2 != NULL) {
1389 g_free(id);
1390 g_object_unref(info2);
1391 return FALSE;
1392 }
1393
1394 manager->priv->elements =
1395 g_list_prepend(manager->priv->elements, info);
1396
1397 detail = element_info_to_detail(info);
1398 if (detail != 0) {
1399 g_signal_emit(manager,
1400 purple_media_manager_signals[ELEMENTS_CHANGED],
1401 detail);
1402 }
1403
1404 /* Restore previously active src/sink elements if they were replugged */
1405 type = purple_media_element_info_get_element_type(info);
1406 if (type & PURPLE_MEDIA_ELEMENT_SRC) {
1407 if (type & PURPLE_MEDIA_ELEMENT_AUDIO &&
1408 purple_strequal(manager->priv->audio_src_id, id)) {
1409 manager->priv->audio_src = info;
1410 }
1411 if (type & PURPLE_MEDIA_ELEMENT_VIDEO &&
1412 purple_strequal(manager->priv->video_src_id, id)) {
1413 manager->priv->video_src = info;
1414 }
1415 }
1416 if (type & PURPLE_MEDIA_ELEMENT_SINK) {
1417 if (type & PURPLE_MEDIA_ELEMENT_AUDIO &&
1418 purple_strequal(manager->priv->audio_sink_id, id)) {
1419 manager->priv->audio_sink = info;
1420 }
1421 if (type & PURPLE_MEDIA_ELEMENT_VIDEO &&
1422 purple_strequal(manager->priv->video_sink_id, id)) {
1423 manager->priv->video_sink = info;
1424 }
1425 }
1426
1427 g_free(id);
1428
1429 return TRUE;
1430 #else
1431 return FALSE;
1432 #endif
1433 }
1434
1435 gboolean
purple_media_manager_unregister_element(PurpleMediaManager * manager,const gchar * id)1436 purple_media_manager_unregister_element(PurpleMediaManager *manager,
1437 const gchar *id)
1438 {
1439 #ifdef USE_VV
1440 PurpleMediaElementInfo *info;
1441 GQuark detail;
1442
1443 g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), FALSE);
1444
1445 info = purple_media_manager_get_element_info(manager, id);
1446
1447 if (info == NULL) {
1448 g_object_unref(info);
1449 return FALSE;
1450 }
1451
1452 if (manager->priv->audio_src == info)
1453 manager->priv->audio_src = NULL;
1454 if (manager->priv->audio_sink == info)
1455 manager->priv->audio_sink = NULL;
1456 if (manager->priv->video_src == info)
1457 manager->priv->video_src = NULL;
1458 if (manager->priv->video_sink == info)
1459 manager->priv->video_sink = NULL;
1460
1461 detail = element_info_to_detail(info);
1462
1463 manager->priv->elements = g_list_remove(
1464 manager->priv->elements, info);
1465 g_object_unref(info);
1466
1467 if (detail != 0) {
1468 g_signal_emit(manager,
1469 purple_media_manager_signals[ELEMENTS_CHANGED],
1470 detail);
1471 }
1472
1473 return TRUE;
1474 #else
1475 return FALSE;
1476 #endif
1477 }
1478
1479 gboolean
purple_media_manager_set_active_element(PurpleMediaManager * manager,PurpleMediaElementInfo * info)1480 purple_media_manager_set_active_element(PurpleMediaManager *manager,
1481 PurpleMediaElementInfo *info)
1482 {
1483 #ifdef USE_VV
1484 PurpleMediaElementInfo *info2;
1485 PurpleMediaElementType type;
1486 gboolean ret = FALSE;
1487 gchar *id;
1488
1489 g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), FALSE);
1490 g_return_val_if_fail(info != NULL, FALSE);
1491
1492 id = purple_media_element_info_get_id(info);
1493 info2 = purple_media_manager_get_element_info(manager, id);
1494
1495 if (info2 == NULL)
1496 purple_media_manager_register_element(manager, info);
1497 else
1498 g_object_unref(info2);
1499
1500 type = purple_media_element_info_get_element_type(info);
1501
1502 if (type & PURPLE_MEDIA_ELEMENT_SRC) {
1503 if (type & PURPLE_MEDIA_ELEMENT_AUDIO) {
1504 manager->priv->audio_src = info;
1505 g_free(manager->priv->audio_src_id);
1506 manager->priv->audio_src_id = id;
1507 id = NULL;
1508 ret = TRUE;
1509 }
1510 if (type & PURPLE_MEDIA_ELEMENT_VIDEO) {
1511 manager->priv->video_src = info;
1512 g_free(manager->priv->video_src_id);
1513 manager->priv->video_src_id = id;
1514 id = NULL;
1515 ret = TRUE;
1516 }
1517 }
1518 if (type & PURPLE_MEDIA_ELEMENT_SINK) {
1519 if (type & PURPLE_MEDIA_ELEMENT_AUDIO) {
1520 manager->priv->audio_sink = info;
1521 g_free(manager->priv->audio_sink_id);
1522 manager->priv->audio_sink_id = id;
1523 id = NULL;
1524 ret = TRUE;
1525 }
1526 if (type & PURPLE_MEDIA_ELEMENT_VIDEO) {
1527 manager->priv->video_sink = info;
1528 g_free(manager->priv->video_sink_id);
1529 manager->priv->video_sink_id = id;
1530 id = NULL;
1531 ret = TRUE;
1532 }
1533 }
1534
1535 g_free(id);
1536 return ret;
1537 #else
1538 return FALSE;
1539 #endif
1540 }
1541
1542 PurpleMediaElementInfo *
purple_media_manager_get_active_element(PurpleMediaManager * manager,PurpleMediaElementType type)1543 purple_media_manager_get_active_element(PurpleMediaManager *manager,
1544 PurpleMediaElementType type)
1545 {
1546 #ifdef USE_VV
1547 g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), NULL);
1548
1549 if (type & PURPLE_MEDIA_ELEMENT_SRC) {
1550 if (type & PURPLE_MEDIA_ELEMENT_AUDIO)
1551 return manager->priv->audio_src;
1552 else if (type & PURPLE_MEDIA_ELEMENT_VIDEO)
1553 return manager->priv->video_src;
1554 else if (type & PURPLE_MEDIA_ELEMENT_APPLICATION)
1555 return get_send_application_element_info ();
1556 } else if (type & PURPLE_MEDIA_ELEMENT_SINK) {
1557 if (type & PURPLE_MEDIA_ELEMENT_AUDIO)
1558 return manager->priv->audio_sink;
1559 else if (type & PURPLE_MEDIA_ELEMENT_VIDEO)
1560 return manager->priv->video_sink;
1561 else if (type & PURPLE_MEDIA_ELEMENT_APPLICATION)
1562 return get_recv_application_element_info ();
1563
1564 }
1565 #endif
1566
1567 return NULL;
1568 }
1569 #endif /* USE_GSTREAMER */
1570
1571 #ifdef USE_VV
1572 static gboolean
window_caps_cb_cb(PurpleMediaOutputWindow * ow)1573 window_caps_cb_cb(PurpleMediaOutputWindow *ow)
1574 {
1575 GstPad *pad = gst_element_get_static_pad(ow->sink, "sink");
1576 #if GST_CHECK_VERSION(1,0,0)
1577 GstCaps *caps = gst_pad_get_current_caps(pad);
1578 #else
1579 GstCaps *caps = GST_PAD_CAPS(pad);
1580 #endif
1581
1582 if (caps) {
1583 g_signal_emit_by_name(ow->media, "video-caps", ow->session_id, ow->participant, caps);
1584 #if GST_CHECK_VERSION(1,0,0)
1585 gst_caps_unref(caps);
1586 #endif
1587 }
1588
1589 ow->caps_id = 0;
1590
1591 return FALSE;
1592 }
1593
1594 static void
window_caps_cb(GstPad * pad,GParamSpec * pspec,PurpleMediaOutputWindow * ow)1595 window_caps_cb(GstPad *pad, GParamSpec *pspec, PurpleMediaOutputWindow *ow)
1596 {
1597 if (!ow->caps_id)
1598 ow->caps_id = g_timeout_add(0, (GSourceFunc)window_caps_cb_cb, ow);
1599 }
1600
1601 static void
window_id_cb(GstBus * bus,GstMessage * msg,PurpleMediaOutputWindow * ow)1602 window_id_cb(GstBus *bus, GstMessage *msg, PurpleMediaOutputWindow *ow)
1603 {
1604 GstElement *sink;
1605
1606 if (GST_MESSAGE_TYPE(msg) != GST_MESSAGE_ELEMENT
1607 #if GST_CHECK_VERSION(1,0,0)
1608 || !gst_is_video_overlay_prepare_window_handle_message(msg))
1609 #else
1610 || !gst_structure_has_name(msg->structure, "prepare-xwindow-id"))
1611 #endif
1612 return;
1613
1614 sink = GST_ELEMENT(GST_MESSAGE_SRC(msg));
1615 while (sink != ow->sink) {
1616 if (sink == NULL)
1617 return;
1618 sink = GST_ELEMENT_PARENT(sink);
1619 }
1620
1621 g_signal_handlers_disconnect_matched(bus, G_SIGNAL_MATCH_FUNC
1622 | G_SIGNAL_MATCH_DATA, 0, 0, NULL,
1623 window_id_cb, ow);
1624
1625 #if GST_CHECK_VERSION(1,0,0)
1626 gst_video_overlay_set_window_handle(GST_VIDEO_OVERLAY(GST_MESSAGE_SRC(msg)),
1627 ow->window_id);
1628 #elif GST_CHECK_VERSION(0,10,31)
1629 gst_x_overlay_set_window_handle(GST_X_OVERLAY(GST_MESSAGE_SRC(msg)),
1630 ow->window_id);
1631 #else
1632 gst_x_overlay_set_xwindow_id(GST_X_OVERLAY(GST_MESSAGE_SRC(msg)),
1633 ow->window_id);
1634 #endif
1635 }
1636 #endif
1637
1638 gboolean
purple_media_manager_create_output_window(PurpleMediaManager * manager,PurpleMedia * media,const gchar * session_id,const gchar * participant)1639 purple_media_manager_create_output_window(PurpleMediaManager *manager,
1640 PurpleMedia *media, const gchar *session_id,
1641 const gchar *participant)
1642 {
1643 #ifdef USE_VV
1644 GList *iter;
1645
1646 g_return_val_if_fail(PURPLE_IS_MEDIA(media), FALSE);
1647
1648 iter = manager->priv->output_windows;
1649 for(; iter; iter = g_list_next(iter)) {
1650 PurpleMediaOutputWindow *ow = iter->data;
1651
1652 if (ow->sink == NULL && ow->media == media &&
1653 purple_strequal(participant, ow->participant) &&
1654 purple_strequal(session_id, ow->session_id)) {
1655 GstBus *bus;
1656 GstPad *pad;
1657 GstElement *queue, *convert, *scale;
1658 GstElement *tee = purple_media_get_tee(media,
1659 session_id, participant);
1660
1661 if (tee == NULL)
1662 continue;
1663
1664 queue = gst_element_factory_make("queue", NULL);
1665 #if GST_CHECK_VERSION(1,0,0)
1666 convert = gst_element_factory_make("videoconvert", NULL);
1667 #else
1668 convert = gst_element_factory_make("ffmpegcolorspace", NULL);
1669 #endif
1670 scale = gst_element_factory_make("videoscale", NULL);
1671 ow->sink = purple_media_manager_get_element(
1672 manager, PURPLE_MEDIA_RECV_VIDEO,
1673 ow->media, ow->session_id,
1674 ow->participant);
1675
1676 if (participant == NULL) {
1677 /* aka this is a preview sink */
1678 GObjectClass *klass =
1679 G_OBJECT_GET_CLASS(ow->sink);
1680 if (g_object_class_find_property(klass,
1681 "sync"))
1682 g_object_set(G_OBJECT(ow->sink),
1683 "sync", FALSE, NULL);
1684 if (g_object_class_find_property(klass,
1685 "async"))
1686 g_object_set(G_OBJECT(ow->sink),
1687 "async", FALSE, NULL);
1688 }
1689
1690 gst_bin_add_many(GST_BIN(GST_ELEMENT_PARENT(tee)),
1691 queue, convert, scale, ow->sink, NULL);
1692
1693 bus = gst_pipeline_get_bus(GST_PIPELINE(
1694 manager->priv->pipeline));
1695 g_signal_connect(bus, "sync-message::element",
1696 G_CALLBACK(window_id_cb), ow);
1697 gst_object_unref(bus);
1698
1699 pad = gst_element_get_static_pad(ow->sink, "sink");
1700 g_signal_connect(pad, "notify::caps",
1701 G_CALLBACK(window_caps_cb), ow);
1702 gst_object_unref(pad);
1703
1704 gst_element_set_state(ow->sink, GST_STATE_PLAYING);
1705 gst_element_set_state(scale, GST_STATE_PLAYING);
1706 gst_element_set_state(convert, GST_STATE_PLAYING);
1707 gst_element_set_state(queue, GST_STATE_PLAYING);
1708 gst_element_link(scale, ow->sink);
1709 gst_element_link(convert, scale);
1710 gst_element_link(queue, convert);
1711 gst_element_link(tee, queue);
1712 }
1713 }
1714 return TRUE;
1715 #else
1716 return FALSE;
1717 #endif
1718 }
1719
1720 gulong
purple_media_manager_set_output_window(PurpleMediaManager * manager,PurpleMedia * media,const gchar * session_id,const gchar * participant,gulong window_id)1721 purple_media_manager_set_output_window(PurpleMediaManager *manager,
1722 PurpleMedia *media, const gchar *session_id,
1723 const gchar *participant, gulong window_id)
1724 {
1725 #ifdef USE_VV
1726 PurpleMediaOutputWindow *output_window;
1727
1728 g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), FALSE);
1729 g_return_val_if_fail(PURPLE_IS_MEDIA(media), FALSE);
1730
1731 output_window = g_new0(PurpleMediaOutputWindow, 1);
1732 output_window->id = manager->priv->next_output_window_id++;
1733 output_window->media = media;
1734 output_window->session_id = g_strdup(session_id);
1735 output_window->participant = g_strdup(participant);
1736 output_window->window_id = window_id;
1737
1738 manager->priv->output_windows = g_list_prepend(
1739 manager->priv->output_windows, output_window);
1740
1741 if (purple_media_get_tee(media, session_id, participant) != NULL)
1742 purple_media_manager_create_output_window(manager,
1743 media, session_id, participant);
1744
1745 return output_window->id;
1746 #else
1747 return 0;
1748 #endif
1749 }
1750
1751 gboolean
purple_media_manager_remove_output_window(PurpleMediaManager * manager,gulong output_window_id)1752 purple_media_manager_remove_output_window(PurpleMediaManager *manager,
1753 gulong output_window_id)
1754 {
1755 #ifdef USE_VV
1756 PurpleMediaOutputWindow *output_window = NULL;
1757 GList *iter;
1758
1759 g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager), FALSE);
1760
1761 iter = manager->priv->output_windows;
1762 for (; iter; iter = g_list_next(iter)) {
1763 PurpleMediaOutputWindow *ow = iter->data;
1764 if (ow->id == output_window_id) {
1765 manager->priv->output_windows = g_list_delete_link(
1766 manager->priv->output_windows, iter);
1767 output_window = ow;
1768 break;
1769 }
1770 }
1771
1772 if (output_window == NULL)
1773 return FALSE;
1774
1775 if (output_window->sink != NULL) {
1776 GstElement *element = output_window->sink;
1777 GstPad *pad;
1778 GstPad *teepad = NULL;
1779 GSList *to_remove = NULL;
1780
1781 pad = gst_element_get_static_pad(element, "sink");
1782 g_signal_handlers_disconnect_matched(pad, G_SIGNAL_MATCH_FUNC
1783 | G_SIGNAL_MATCH_DATA, 0, 0, NULL,
1784 window_caps_cb, output_window);
1785 gst_object_unref(pad);
1786
1787 /* Find the tee element this output is connected to. */
1788 while (!teepad) {
1789 GstPad *peer;
1790 GstElementFactory *factory;
1791 const gchar *factory_name;
1792
1793 to_remove = g_slist_append(to_remove, element);
1794
1795 pad = gst_element_get_static_pad(element, "sink");
1796 peer = gst_pad_get_peer(pad);
1797 if (!peer) {
1798 /* Output is disconnected from the pipeline. */
1799 gst_object_unref(pad);
1800 break;
1801 }
1802
1803 factory = gst_element_get_factory(GST_PAD_PARENT(peer));
1804 factory_name = gst_plugin_feature_get_name(GST_PLUGIN_FEATURE(factory));
1805 if (purple_strequal(factory_name, "tee")) {
1806 teepad = peer;
1807 }
1808
1809 element = GST_PAD_PARENT(peer);
1810
1811 gst_object_unref(pad);
1812 gst_object_unref(peer);
1813 }
1814
1815 if (teepad) {
1816 gst_element_release_request_pad(GST_PAD_PARENT(teepad),
1817 teepad);
1818 }
1819
1820 while (to_remove) {
1821 GstElement *element = to_remove->data;
1822
1823 gst_element_set_locked_state(element, TRUE);
1824 gst_element_set_state(element, GST_STATE_NULL);
1825 gst_bin_remove(GST_BIN(GST_ELEMENT_PARENT(element)),
1826 element);
1827 to_remove = g_slist_delete_link(to_remove, to_remove);
1828 }
1829 }
1830 if (output_window->caps_id)
1831 g_source_remove(output_window->caps_id);
1832
1833 g_free(output_window->session_id);
1834 g_free(output_window->participant);
1835 g_free(output_window);
1836
1837 return TRUE;
1838 #else
1839 return FALSE;
1840 #endif
1841 }
1842
1843 void
purple_media_manager_remove_output_windows(PurpleMediaManager * manager,PurpleMedia * media,const gchar * session_id,const gchar * participant)1844 purple_media_manager_remove_output_windows(PurpleMediaManager *manager,
1845 PurpleMedia *media, const gchar *session_id,
1846 const gchar *participant)
1847 {
1848 #ifdef USE_VV
1849 GList *iter;
1850
1851 g_return_if_fail(PURPLE_IS_MEDIA(media));
1852
1853 iter = manager->priv->output_windows;
1854
1855 for (; iter;) {
1856 PurpleMediaOutputWindow *ow = iter->data;
1857 iter = g_list_next(iter);
1858
1859 if (media == ow->media &&
1860 purple_strequal(session_id, ow->session_id) &&
1861 purple_strequal(participant, ow->participant))
1862 purple_media_manager_remove_output_window(
1863 manager, ow->id);
1864 }
1865 #endif
1866 }
1867
1868 void
purple_media_manager_set_ui_caps(PurpleMediaManager * manager,PurpleMediaCaps caps)1869 purple_media_manager_set_ui_caps(PurpleMediaManager *manager,
1870 PurpleMediaCaps caps)
1871 {
1872 #ifdef USE_VV
1873 PurpleMediaCaps oldcaps;
1874
1875 g_return_if_fail(PURPLE_IS_MEDIA_MANAGER(manager));
1876
1877 oldcaps = manager->priv->ui_caps;
1878 manager->priv->ui_caps = caps;
1879
1880 if (caps != oldcaps)
1881 g_signal_emit(manager,
1882 purple_media_manager_signals[UI_CAPS_CHANGED],
1883 0, caps, oldcaps);
1884 #endif
1885 }
1886
1887 PurpleMediaCaps
purple_media_manager_get_ui_caps(PurpleMediaManager * manager)1888 purple_media_manager_get_ui_caps(PurpleMediaManager *manager)
1889 {
1890 #ifdef USE_VV
1891 g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager),
1892 PURPLE_MEDIA_CAPS_NONE);
1893 return manager->priv->ui_caps;
1894 #else
1895 return PURPLE_MEDIA_CAPS_NONE;
1896 #endif
1897 }
1898
1899 void
purple_media_manager_set_backend_type(PurpleMediaManager * manager,GType backend_type)1900 purple_media_manager_set_backend_type(PurpleMediaManager *manager,
1901 GType backend_type)
1902 {
1903 #ifdef USE_VV
1904 g_return_if_fail(PURPLE_IS_MEDIA_MANAGER(manager));
1905
1906 manager->priv->backend_type = backend_type;
1907 #endif
1908 }
1909
1910 GType
purple_media_manager_get_backend_type(PurpleMediaManager * manager)1911 purple_media_manager_get_backend_type(PurpleMediaManager *manager)
1912 {
1913 #ifdef USE_VV
1914 g_return_val_if_fail(PURPLE_IS_MEDIA_MANAGER(manager),
1915 PURPLE_MEDIA_CAPS_NONE);
1916
1917 return manager->priv->backend_type;
1918 #else
1919 return G_TYPE_NONE;
1920 #endif
1921 }
1922
1923 void
purple_media_manager_set_application_data_callbacks(PurpleMediaManager * manager,PurpleMedia * media,const gchar * session_id,const gchar * participant,PurpleMediaAppDataCallbacks * callbacks,gpointer user_data,GDestroyNotify notify)1924 purple_media_manager_set_application_data_callbacks(PurpleMediaManager *manager,
1925 PurpleMedia *media, const gchar *session_id,
1926 const gchar *participant, PurpleMediaAppDataCallbacks *callbacks,
1927 gpointer user_data, GDestroyNotify notify)
1928 {
1929 #ifdef HAVE_MEDIA_APPLICATION
1930 PurpleMediaAppDataInfo * info = ensure_app_data_info_and_lock (manager,
1931 media, session_id, participant);
1932
1933 if (info->notify)
1934 info->notify (info->user_data);
1935
1936 if (info->readable_cb_token) {
1937 purple_timeout_remove (info->readable_timer_id);
1938 info->readable_cb_token = 0;
1939 }
1940
1941 if (info->writable_cb_token) {
1942 purple_timeout_remove (info->writable_timer_id);
1943 info->writable_cb_token = 0;
1944 }
1945
1946 if (callbacks) {
1947 info->callbacks = *callbacks;
1948 } else {
1949 info->callbacks.writable = NULL;
1950 info->callbacks.readable = NULL;
1951 }
1952 info->user_data = user_data;
1953 info->notify = notify;
1954
1955 call_appsrc_writable_locked (info);
1956 if (info->num_samples > 0 || info->current_sample != NULL)
1957 call_appsink_readable_locked (info);
1958
1959 g_mutex_unlock (&manager->priv->appdata_mutex);
1960 #endif
1961 }
1962
1963 gint
purple_media_manager_send_application_data(PurpleMediaManager * manager,PurpleMedia * media,const gchar * session_id,const gchar * participant,gpointer buffer,guint size,gboolean blocking)1964 purple_media_manager_send_application_data (
1965 PurpleMediaManager *manager, PurpleMedia *media, const gchar *session_id,
1966 const gchar *participant, gpointer buffer, guint size, gboolean blocking)
1967 {
1968 #ifdef HAVE_MEDIA_APPLICATION
1969 PurpleMediaAppDataInfo * info = get_app_data_info_and_lock (manager,
1970 media, session_id, participant);
1971
1972 if (info && info->appsrc && info->connected) {
1973 GstBuffer *gstbuffer = gst_buffer_new_wrapped (g_memdup2 (buffer, size),
1974 size);
1975 GstAppSrc *appsrc = gst_object_ref (info->appsrc);
1976
1977 g_mutex_unlock (&manager->priv->appdata_mutex);
1978 if (gst_app_src_push_buffer (appsrc, gstbuffer) == GST_FLOW_OK) {
1979 if (blocking) {
1980 GstPad *srcpad;
1981
1982 srcpad = gst_element_get_static_pad (GST_ELEMENT (appsrc),
1983 "src");
1984 if (srcpad) {
1985 gst_pad_peer_query (srcpad, gst_query_new_drain ());
1986 gst_object_unref (srcpad);
1987 }
1988 }
1989 gst_object_unref (appsrc);
1990 return size;
1991 } else {
1992 gst_object_unref (appsrc);
1993 return -1;
1994 }
1995 }
1996 g_mutex_unlock (&manager->priv->appdata_mutex);
1997 return -1;
1998 #else
1999 return -1;
2000 #endif
2001 }
2002
2003 gint
purple_media_manager_receive_application_data(PurpleMediaManager * manager,PurpleMedia * media,const gchar * session_id,const gchar * participant,gpointer buffer,guint max_size,gboolean blocking)2004 purple_media_manager_receive_application_data (
2005 PurpleMediaManager *manager, PurpleMedia *media, const gchar *session_id,
2006 const gchar *participant, gpointer buffer, guint max_size,
2007 gboolean blocking)
2008 {
2009 #ifdef HAVE_MEDIA_APPLICATION
2010 PurpleMediaAppDataInfo * info = get_app_data_info_and_lock (manager,
2011 media, session_id, participant);
2012 guint bytes_read = 0;
2013
2014 if (info) {
2015 /* If we are in a blocking read, we need to loop until max_size data
2016 * is read into the buffer, if we're not, then we need to read as much
2017 * data as possible
2018 */
2019 do {
2020 if (!info->current_sample && info->appsink && info->num_samples > 0) {
2021 info->current_sample = gst_app_sink_pull_sample (info->appsink);
2022 info->sample_offset = 0;
2023 if (info->current_sample)
2024 info->num_samples--;
2025 }
2026
2027 if (info->current_sample) {
2028 GstBuffer *gstbuffer = gst_sample_get_buffer (
2029 info->current_sample);
2030
2031 if (gstbuffer) {
2032 GstMapInfo mapinfo;
2033 guint bytes_to_copy;
2034
2035 gst_buffer_map (gstbuffer, &mapinfo, GST_MAP_READ);
2036 /* We must copy only the data remaining in the buffer without
2037 * overflowing the buffer */
2038 bytes_to_copy = max_size - bytes_read;
2039 if (bytes_to_copy > mapinfo.size - info->sample_offset)
2040 bytes_to_copy = mapinfo.size - info->sample_offset;
2041 memcpy ((guint8 *)buffer + bytes_read,
2042 mapinfo.data + info->sample_offset, bytes_to_copy);
2043
2044 gst_buffer_unmap (gstbuffer, &mapinfo);
2045 info->sample_offset += bytes_to_copy;
2046 bytes_read += bytes_to_copy;
2047 if (info->sample_offset == mapinfo.size) {
2048 gst_sample_unref (info->current_sample);
2049 info->current_sample = NULL;
2050 info->sample_offset = 0;
2051 }
2052 } else {
2053 /* In case there's no buffer in the sample (should never
2054 * happen), we need to at least unref it */
2055 gst_sample_unref (info->current_sample);
2056 info->current_sample = NULL;
2057 info->sample_offset = 0;
2058 }
2059 }
2060
2061 /* If blocking, wait until there's an available sample */
2062 while (bytes_read < max_size && blocking &&
2063 info->current_sample == NULL && info->num_samples == 0) {
2064 g_cond_wait (&info->readable_cond, &manager->priv->appdata_mutex);
2065
2066 /* We've been signaled, we need to unlock and regrab the info
2067 * struct to make sure nothing changed */
2068 g_mutex_unlock (&manager->priv->appdata_mutex);
2069 info = get_app_data_info_and_lock (manager,
2070 media, session_id, participant);
2071 if (info == NULL || info->appsink == NULL) {
2072 /* The session was destroyed while we were waiting, we
2073 * should return here */
2074 g_mutex_unlock (&manager->priv->appdata_mutex);
2075 return bytes_read;
2076 }
2077 }
2078 } while (bytes_read < max_size &&
2079 (blocking || info->num_samples > 0));
2080
2081 g_mutex_unlock (&manager->priv->appdata_mutex);
2082 return bytes_read;
2083 }
2084 g_mutex_unlock (&manager->priv->appdata_mutex);
2085 return -1;
2086 #else
2087 return -1;
2088 #endif
2089 }
2090
2091 #ifdef USE_GSTREAMER
2092
2093 static void
videosink_disable_last_sample(GstElement * sink)2094 videosink_disable_last_sample(GstElement *sink)
2095 {
2096 GObjectClass *klass = G_OBJECT_GET_CLASS(sink);
2097
2098 if (g_object_class_find_property(klass, "enable-last-sample")) {
2099 g_object_set(sink, "enable-last-sample", FALSE, NULL);
2100 }
2101 }
2102
2103 #if GST_CHECK_VERSION(1, 4, 0)
2104
2105 static PurpleMediaElementType
gst_class_to_purple_element_type(const gchar * device_class)2106 gst_class_to_purple_element_type(const gchar *device_class)
2107 {
2108 if (purple_strequal(device_class, "Audio/Source")) {
2109 return PURPLE_MEDIA_ELEMENT_AUDIO
2110 | PURPLE_MEDIA_ELEMENT_SRC
2111 | PURPLE_MEDIA_ELEMENT_ONE_SRC
2112 | PURPLE_MEDIA_ELEMENT_UNIQUE;
2113 } else if (purple_strequal(device_class, "Audio/Sink")) {
2114 return PURPLE_MEDIA_ELEMENT_AUDIO
2115 | PURPLE_MEDIA_ELEMENT_SINK
2116 | PURPLE_MEDIA_ELEMENT_ONE_SINK;
2117 } else if (purple_strequal(device_class, "Video/Source")) {
2118 return PURPLE_MEDIA_ELEMENT_VIDEO
2119 | PURPLE_MEDIA_ELEMENT_SRC
2120 | PURPLE_MEDIA_ELEMENT_ONE_SRC
2121 | PURPLE_MEDIA_ELEMENT_UNIQUE;
2122 } else if (purple_strequal(device_class, "Video/Sink")) {
2123 return PURPLE_MEDIA_ELEMENT_VIDEO
2124 | PURPLE_MEDIA_ELEMENT_SINK
2125 | PURPLE_MEDIA_ELEMENT_ONE_SINK;
2126 }
2127
2128 return PURPLE_MEDIA_ELEMENT_NONE;
2129 }
2130
2131 static GstElement *
gst_device_create_cb(PurpleMediaElementInfo * info,PurpleMedia * media,const gchar * session_id,const gchar * participant)2132 gst_device_create_cb(PurpleMediaElementInfo *info, PurpleMedia *media,
2133 const gchar *session_id, const gchar *participant)
2134 {
2135 GstDevice *device;
2136 GstElement *result;
2137 PurpleMediaElementType type;
2138
2139 device = g_object_get_data(G_OBJECT(info), "gst-device");
2140 if (!device) {
2141 return NULL;
2142 }
2143
2144 result = gst_device_create_element(device, NULL);
2145 if (!result) {
2146 return NULL;
2147 }
2148
2149 type = purple_media_element_info_get_element_type(info);
2150
2151 if ((type & PURPLE_MEDIA_ELEMENT_VIDEO) &&
2152 (type & PURPLE_MEDIA_ELEMENT_SINK)) {
2153 videosink_disable_last_sample(result);
2154 }
2155
2156 return result;
2157 }
2158
2159 static gboolean
device_is_ignored(GstDevice * device)2160 device_is_ignored(GstDevice *device)
2161 {
2162 gboolean result = FALSE;
2163
2164 #if GST_CHECK_VERSION(1, 6, 0)
2165 gchar *device_class;
2166
2167 g_return_val_if_fail(device, TRUE);
2168
2169 device_class = gst_device_get_device_class(device);
2170
2171 /* Ignore PulseAudio monitor audio sources since they have little use
2172 * in the context of telephony.*/
2173 if (purple_strequal(device_class, "Audio/Source")) {
2174 GstStructure *properties;
2175 const gchar *pa_class;
2176
2177 properties = gst_device_get_properties(device);
2178
2179 pa_class = gst_structure_get_string(properties, "device.class");
2180 if (purple_strequal(pa_class, "monitor")) {
2181 result = TRUE;
2182 }
2183
2184 gst_structure_free(properties);
2185 }
2186
2187 g_free(device_class);
2188 #endif /* GST_CHECK_VERSION(1, 6, 0) */
2189
2190 return result;
2191 }
2192
2193 static void
purple_media_manager_register_gst_device(PurpleMediaManager * manager,GstDevice * device)2194 purple_media_manager_register_gst_device(PurpleMediaManager *manager,
2195 GstDevice *device)
2196 {
2197 PurpleMediaElementInfo *info;
2198 PurpleMediaElementType type;
2199 gchar *name;
2200 gchar *device_class;
2201 gchar *id;
2202
2203 if (device_is_ignored(device)) {
2204 return;
2205 }
2206
2207 name = gst_device_get_display_name(device);
2208 device_class = gst_device_get_device_class(device);
2209
2210 id = g_strdup_printf("%s %s", device_class, name);
2211
2212 type = gst_class_to_purple_element_type(device_class);
2213
2214 info = g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
2215 "id", id,
2216 "name", name,
2217 "type", type,
2218 "create-cb", gst_device_create_cb,
2219 NULL);
2220
2221 g_object_set_data(G_OBJECT(info), "gst-device", device);
2222
2223 purple_media_manager_register_element(manager, info);
2224
2225 purple_debug_info("mediamanager", "Registered %s device %s\n",
2226 device_class, name);
2227
2228 g_free(name);
2229 g_free(device_class);
2230 g_free(id);
2231 }
2232
2233 static void
purple_media_manager_unregister_gst_device(PurpleMediaManager * manager,GstDevice * device)2234 purple_media_manager_unregister_gst_device(PurpleMediaManager *manager,
2235 GstDevice *device)
2236 {
2237 #ifdef USE_VV
2238 GList *i;
2239 gchar *name;
2240 gchar *device_class;
2241 gboolean done = FALSE;
2242
2243 name = gst_device_get_display_name(device);
2244 device_class = gst_device_get_device_class(device);
2245
2246 for (i = manager->priv->elements; i && !done;) {
2247 PurpleMediaElementInfo *info = i->data;
2248 GList *next = i->next;
2249 GstDevice *device2;
2250
2251 device2 = g_object_get_data(G_OBJECT(info), "gst-device");
2252 if (device2) {
2253 gchar *name2;
2254 gchar *device_class2;
2255
2256 name2 = gst_device_get_display_name(device2);
2257 device_class2 = gst_device_get_device_class(device2);
2258
2259 if (purple_strequal(name, name2) &&
2260 purple_strequal(device_class, device_class2)) {
2261 gchar *id;
2262
2263 id = purple_media_element_info_get_id(info);
2264 purple_media_manager_unregister_element(manager,
2265 id);
2266
2267 purple_debug_info("mediamanager",
2268 "Unregistered %s device %s",
2269 device_class, name);
2270
2271 g_free(id);
2272
2273 done = TRUE;
2274 }
2275
2276 g_free(name2);
2277 g_free(device_class2);
2278 }
2279 i = next;
2280 }
2281
2282 g_free(name);
2283 g_free(device_class);
2284 #endif /* USE_VV */
2285 }
2286
2287 static gboolean
device_monitor_bus_cb(GstBus * bus,GstMessage * message,gpointer user_data)2288 device_monitor_bus_cb(GstBus *bus, GstMessage *message, gpointer user_data)
2289 {
2290 PurpleMediaManager *manager = user_data;
2291 GstMessageType message_type;
2292 GstDevice *device;
2293
2294 message_type = GST_MESSAGE_TYPE(message);
2295
2296 if (message_type == GST_MESSAGE_DEVICE_ADDED) {
2297 gst_message_parse_device_added(message, &device);
2298 purple_media_manager_register_gst_device(manager, device);
2299 } else if (message_type == GST_MESSAGE_DEVICE_REMOVED) {
2300 gst_message_parse_device_removed (message, &device);
2301 purple_media_manager_unregister_gst_device(manager, device);
2302 }
2303
2304 return G_SOURCE_CONTINUE;
2305 }
2306
2307 #endif /* GST_CHECK_VERSION(1, 4, 0) */
2308
2309 static void
purple_media_manager_init_device_monitor(PurpleMediaManager * manager)2310 purple_media_manager_init_device_monitor(PurpleMediaManager *manager)
2311 {
2312 #if GST_CHECK_VERSION(1, 4, 0) && defined(USE_VV)
2313 GstBus *bus;
2314 GList *i;
2315
2316 manager->priv->device_monitor = gst_device_monitor_new();
2317
2318 bus = gst_device_monitor_get_bus(manager->priv->device_monitor);
2319 gst_bus_add_watch (bus, device_monitor_bus_cb, manager);
2320 gst_object_unref (bus);
2321
2322 /* This avoids warning in GStreamer logs about no filters set */
2323 gst_device_monitor_add_filter(manager->priv->device_monitor, NULL, NULL);
2324
2325 gst_device_monitor_start(manager->priv->device_monitor);
2326
2327 i = gst_device_monitor_get_devices(manager->priv->device_monitor);
2328 for (; i; i = g_list_delete_link(i, i)) {
2329 GstDevice *device = i->data;
2330
2331 purple_media_manager_register_gst_device(manager, device);
2332 gst_object_unref(device);
2333 }
2334 #endif /* GST_CHECK_VERSION(1, 4, 0) */
2335 }
2336
2337 GList *
purple_media_manager_enumerate_elements(PurpleMediaManager * manager,PurpleMediaElementType type)2338 purple_media_manager_enumerate_elements(PurpleMediaManager *manager,
2339 PurpleMediaElementType type)
2340 {
2341 GList *result = NULL;
2342 #ifdef USE_VV
2343 GList *i;
2344
2345 for (i = manager->priv->elements; i; i = i->next) {
2346 PurpleMediaElementInfo *info = i->data;
2347 PurpleMediaElementType type2;
2348
2349 type2 = purple_media_element_info_get_element_type(info);
2350
2351 if ((type2 & type) == type) {
2352 g_object_ref(info);
2353 result = g_list_prepend(result, info);
2354 }
2355 }
2356 #endif /* USE_VV */
2357
2358 return result;
2359 }
2360
2361 static GstElement *
gst_factory_make_cb(PurpleMediaElementInfo * info,PurpleMedia * media,const gchar * session_id,const gchar * participant)2362 gst_factory_make_cb(PurpleMediaElementInfo *info, PurpleMedia *media,
2363 const gchar *session_id, const gchar *participant)
2364 {
2365 gchar *id;
2366 GstElement *element;
2367
2368 id = purple_media_element_info_get_id(info);
2369
2370 element = gst_element_factory_make(id, NULL);
2371
2372 g_free(id);
2373
2374 return element;
2375 }
2376
2377 static void
autovideosink_child_added_cb(GstChildProxy * child_proxy,GObject * object,gchar * name,gpointer user_data)2378 autovideosink_child_added_cb (GstChildProxy *child_proxy, GObject *object,
2379 gchar *name, gpointer user_data)
2380 {
2381 videosink_disable_last_sample(GST_ELEMENT(object));
2382 }
2383
2384 static GstElement *
default_video_sink_create_cb(PurpleMediaElementInfo * info,PurpleMedia * media,const gchar * session_id,const gchar * participant)2385 default_video_sink_create_cb(PurpleMediaElementInfo *info, PurpleMedia *media,
2386 const gchar *session_id, const gchar *participant)
2387 {
2388 GstElement *videosink = gst_element_factory_make("autovideosink", NULL);
2389
2390 g_signal_connect(videosink, "child-added",
2391 G_CALLBACK(autovideosink_child_added_cb), NULL);
2392
2393 return videosink;
2394 }
2395
2396 static GstElement *
disabled_video_create_cb(PurpleMediaElementInfo * info,PurpleMedia * media,const gchar * session_id,const gchar * participant)2397 disabled_video_create_cb(PurpleMediaElementInfo *info, PurpleMedia *media,
2398 const gchar *session_id, const gchar *participant)
2399 {
2400 GstElement *src = gst_element_factory_make("videotestsrc", NULL);
2401
2402 /* GST_VIDEO_TEST_SRC_BLACK */
2403 g_object_set(src, "pattern", 2, NULL);
2404
2405 return src;
2406 }
2407
2408 static GstElement *
test_video_create_cb(PurpleMediaElementInfo * info,PurpleMedia * media,const gchar * session_id,const gchar * participant)2409 test_video_create_cb(PurpleMediaElementInfo *info, PurpleMedia *media,
2410 const gchar *session_id, const gchar *participant)
2411 {
2412 GstElement *src = gst_element_factory_make("videotestsrc", NULL);
2413
2414 g_object_set(src, "is-live", TRUE, NULL);
2415
2416 return src;
2417 }
2418
2419 static void
purple_media_manager_register_static_elements(PurpleMediaManager * manager)2420 purple_media_manager_register_static_elements(PurpleMediaManager *manager)
2421 {
2422 static const gchar *VIDEO_SINK_PLUGINS[] = {
2423 /* "aasink", "AALib", Didn't work for me */
2424 "directdrawsink", N_("DirectDraw"),
2425 "glimagesink", N_("OpenGL"),
2426 "ximagesink", N_("X Window System"),
2427 "xvimagesink", N_("X Window System (Xv)"),
2428 NULL
2429 };
2430 const gchar **sinks = VIDEO_SINK_PLUGINS;
2431
2432 /* Default auto* elements. */
2433
2434 purple_media_manager_register_element(manager,
2435 g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
2436 "id", "autoaudiosrc",
2437 "name", N_("Default"),
2438 "type", PURPLE_MEDIA_ELEMENT_AUDIO
2439 | PURPLE_MEDIA_ELEMENT_SRC
2440 | PURPLE_MEDIA_ELEMENT_ONE_SRC
2441 | PURPLE_MEDIA_ELEMENT_UNIQUE,
2442 "create-cb", gst_factory_make_cb,
2443 NULL));
2444
2445 purple_media_manager_register_element(manager,
2446 g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
2447 "id", "autoaudiosink",
2448 "name", N_("Default"),
2449 "type", PURPLE_MEDIA_ELEMENT_AUDIO
2450 | PURPLE_MEDIA_ELEMENT_SINK
2451 | PURPLE_MEDIA_ELEMENT_ONE_SINK,
2452 "create-cb", gst_factory_make_cb,
2453 NULL));
2454
2455 purple_media_manager_register_element(manager,
2456 g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
2457 "id", "autovideosrc",
2458 "name", N_("Default"),
2459 "type", PURPLE_MEDIA_ELEMENT_VIDEO
2460 | PURPLE_MEDIA_ELEMENT_SRC
2461 | PURPLE_MEDIA_ELEMENT_ONE_SRC
2462 | PURPLE_MEDIA_ELEMENT_UNIQUE,
2463 "create-cb", gst_factory_make_cb,
2464 NULL));
2465
2466 purple_media_manager_register_element(manager,
2467 g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
2468 "id", "autovideosink",
2469 "name", N_("Default"),
2470 "type", PURPLE_MEDIA_ELEMENT_VIDEO
2471 | PURPLE_MEDIA_ELEMENT_SINK
2472 | PURPLE_MEDIA_ELEMENT_ONE_SINK,
2473 "create-cb", default_video_sink_create_cb,
2474 NULL));
2475
2476 /* Special elements */
2477
2478 purple_media_manager_register_element(manager,
2479 g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
2480 "id", "audiotestsrc",
2481 /* Translators: This is a noun that refers to one
2482 * possible audio input device. The device can help the
2483 * user to check if her speakers or headphones have been
2484 * set up correctly for voice calling. */
2485 "name", N_("Test Sound"),
2486 "type", PURPLE_MEDIA_ELEMENT_AUDIO
2487 | PURPLE_MEDIA_ELEMENT_SRC
2488 | PURPLE_MEDIA_ELEMENT_ONE_SRC,
2489 "create-cb", gst_factory_make_cb,
2490 NULL));
2491
2492 purple_media_manager_register_element(manager,
2493 g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
2494 "id", "disabledvideosrc",
2495 "name", N_("Disabled"),
2496 "type", PURPLE_MEDIA_ELEMENT_VIDEO
2497 | PURPLE_MEDIA_ELEMENT_SRC
2498 | PURPLE_MEDIA_ELEMENT_ONE_SINK,
2499 "create-cb", disabled_video_create_cb,
2500 NULL));
2501
2502 purple_media_manager_register_element(manager,
2503 g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
2504 "id", "videotestsrc",
2505 /* Translators: This is a noun that refers to one
2506 * possible video input device. The device produces
2507 * a test "monoscope" image that can help the user check
2508 * the video output has been set up correctly without
2509 * needing a webcam connected to the computer. */
2510 "name", N_("Test Pattern"),
2511 "type", PURPLE_MEDIA_ELEMENT_VIDEO
2512 | PURPLE_MEDIA_ELEMENT_SRC
2513 | PURPLE_MEDIA_ELEMENT_ONE_SRC,
2514 "create-cb", test_video_create_cb,
2515 NULL));
2516
2517 for (sinks = VIDEO_SINK_PLUGINS; sinks[0]; sinks += 2) {
2518 GstElementFactory *factory;
2519
2520 factory = gst_element_factory_find(sinks[0]);
2521 if (!factory) {
2522 continue;
2523 }
2524
2525 purple_media_manager_register_element(manager,
2526 g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
2527 "id", sinks[0],
2528 "name", sinks[1],
2529 "type", PURPLE_MEDIA_ELEMENT_VIDEO
2530 | PURPLE_MEDIA_ELEMENT_SINK
2531 | PURPLE_MEDIA_ELEMENT_ONE_SINK,
2532 "create-cb", gst_factory_make_cb,
2533 NULL));
2534
2535 gst_object_unref(factory);
2536 }
2537 }
2538
2539 /*
2540 * PurpleMediaElementType
2541 */
2542
2543 GType
purple_media_element_type_get_type()2544 purple_media_element_type_get_type()
2545 {
2546 static GType type = 0;
2547 if (type == 0) {
2548 static const GFlagsValue values[] = {
2549 { PURPLE_MEDIA_ELEMENT_NONE,
2550 "PURPLE_MEDIA_ELEMENT_NONE", "none" },
2551 { PURPLE_MEDIA_ELEMENT_AUDIO,
2552 "PURPLE_MEDIA_ELEMENT_AUDIO", "audio" },
2553 { PURPLE_MEDIA_ELEMENT_VIDEO,
2554 "PURPLE_MEDIA_ELEMENT_VIDEO", "video" },
2555 { PURPLE_MEDIA_ELEMENT_AUDIO_VIDEO,
2556 "PURPLE_MEDIA_ELEMENT_AUDIO_VIDEO",
2557 "audio-video" },
2558 { PURPLE_MEDIA_ELEMENT_NO_SRCS,
2559 "PURPLE_MEDIA_ELEMENT_NO_SRCS", "no-srcs" },
2560 { PURPLE_MEDIA_ELEMENT_ONE_SRC,
2561 "PURPLE_MEDIA_ELEMENT_ONE_SRC", "one-src" },
2562 { PURPLE_MEDIA_ELEMENT_MULTI_SRC,
2563 "PURPLE_MEDIA_ELEMENT_MULTI_SRC",
2564 "multi-src" },
2565 { PURPLE_MEDIA_ELEMENT_REQUEST_SRC,
2566 "PURPLE_MEDIA_ELEMENT_REQUEST_SRC",
2567 "request-src" },
2568 { PURPLE_MEDIA_ELEMENT_NO_SINKS,
2569 "PURPLE_MEDIA_ELEMENT_NO_SINKS", "no-sinks" },
2570 { PURPLE_MEDIA_ELEMENT_ONE_SINK,
2571 "PURPLE_MEDIA_ELEMENT_ONE_SINK", "one-sink" },
2572 { PURPLE_MEDIA_ELEMENT_MULTI_SINK,
2573 "PURPLE_MEDIA_ELEMENT_MULTI_SINK",
2574 "multi-sink" },
2575 { PURPLE_MEDIA_ELEMENT_REQUEST_SINK,
2576 "PURPLE_MEDIA_ELEMENT_REQUEST_SINK",
2577 "request-sink" },
2578 { PURPLE_MEDIA_ELEMENT_UNIQUE,
2579 "PURPLE_MEDIA_ELEMENT_UNIQUE", "unique" },
2580 { PURPLE_MEDIA_ELEMENT_SRC,
2581 "PURPLE_MEDIA_ELEMENT_SRC", "src" },
2582 { PURPLE_MEDIA_ELEMENT_SINK,
2583 "PURPLE_MEDIA_ELEMENT_SINK", "sink" },
2584 { PURPLE_MEDIA_ELEMENT_APPLICATION,
2585 "PURPLE_MEDIA_ELEMENT_APPLICATION", "application" },
2586 { 0, NULL, NULL }
2587 };
2588 type = g_flags_register_static(
2589 "PurpleMediaElementType", values);
2590 }
2591 return type;
2592 }
2593
2594 /*
2595 * PurpleMediaElementInfo
2596 */
2597
2598 struct _PurpleMediaElementInfoClass
2599 {
2600 GObjectClass parent_class;
2601 };
2602
2603 struct _PurpleMediaElementInfo
2604 {
2605 GObject parent;
2606 };
2607
2608 #ifdef USE_VV
2609 struct _PurpleMediaElementInfoPrivate
2610 {
2611 gchar *id;
2612 gchar *name;
2613 PurpleMediaElementType type;
2614 PurpleMediaElementCreateCallback create;
2615 };
2616
2617 enum {
2618 PROP_0,
2619 PROP_ID,
2620 PROP_NAME,
2621 PROP_TYPE,
2622 PROP_CREATE_CB,
2623 };
2624
2625 static void
purple_media_element_info_init(PurpleMediaElementInfo * info)2626 purple_media_element_info_init(PurpleMediaElementInfo *info)
2627 {
2628 PurpleMediaElementInfoPrivate *priv =
2629 PURPLE_MEDIA_ELEMENT_INFO_GET_PRIVATE(info);
2630 priv->id = NULL;
2631 priv->name = NULL;
2632 priv->type = PURPLE_MEDIA_ELEMENT_NONE;
2633 priv->create = NULL;
2634 }
2635
2636 static void
purple_media_element_info_finalize(GObject * info)2637 purple_media_element_info_finalize(GObject *info)
2638 {
2639 PurpleMediaElementInfoPrivate *priv =
2640 PURPLE_MEDIA_ELEMENT_INFO_GET_PRIVATE(info);
2641 g_free(priv->id);
2642 g_free(priv->name);
2643
2644 parent_class->finalize(info);
2645 }
2646
2647 static void
purple_media_element_info_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)2648 purple_media_element_info_set_property (GObject *object, guint prop_id,
2649 const GValue *value, GParamSpec *pspec)
2650 {
2651 PurpleMediaElementInfoPrivate *priv;
2652 g_return_if_fail(PURPLE_IS_MEDIA_ELEMENT_INFO(object));
2653
2654 priv = PURPLE_MEDIA_ELEMENT_INFO_GET_PRIVATE(object);
2655
2656 switch (prop_id) {
2657 case PROP_ID:
2658 g_free(priv->id);
2659 priv->id = g_value_dup_string(value);
2660 break;
2661 case PROP_NAME:
2662 g_free(priv->name);
2663 priv->name = g_value_dup_string(value);
2664 break;
2665 case PROP_TYPE: {
2666 priv->type = g_value_get_flags(value);
2667 break;
2668 }
2669 case PROP_CREATE_CB:
2670 priv->create = g_value_get_pointer(value);
2671 break;
2672 default:
2673 G_OBJECT_WARN_INVALID_PROPERTY_ID(
2674 object, prop_id, pspec);
2675 break;
2676 }
2677 }
2678
2679 static void
purple_media_element_info_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)2680 purple_media_element_info_get_property (GObject *object, guint prop_id,
2681 GValue *value, GParamSpec *pspec)
2682 {
2683 PurpleMediaElementInfoPrivate *priv;
2684 g_return_if_fail(PURPLE_IS_MEDIA_ELEMENT_INFO(object));
2685
2686 priv = PURPLE_MEDIA_ELEMENT_INFO_GET_PRIVATE(object);
2687
2688 switch (prop_id) {
2689 case PROP_ID:
2690 g_value_set_string(value, priv->id);
2691 break;
2692 case PROP_NAME:
2693 g_value_set_string(value, priv->name);
2694 break;
2695 case PROP_TYPE:
2696 g_value_set_flags(value, priv->type);
2697 break;
2698 case PROP_CREATE_CB:
2699 g_value_set_pointer(value, priv->create);
2700 break;
2701 default:
2702 G_OBJECT_WARN_INVALID_PROPERTY_ID(
2703 object, prop_id, pspec);
2704 break;
2705 }
2706 }
2707
2708 static void
purple_media_element_info_class_init(PurpleMediaElementInfoClass * klass)2709 purple_media_element_info_class_init(PurpleMediaElementInfoClass *klass)
2710 {
2711 GObjectClass *gobject_class = (GObjectClass*)klass;
2712
2713 gobject_class->finalize = purple_media_element_info_finalize;
2714 gobject_class->set_property = purple_media_element_info_set_property;
2715 gobject_class->get_property = purple_media_element_info_get_property;
2716
2717 g_object_class_install_property(gobject_class, PROP_ID,
2718 g_param_spec_string("id",
2719 "ID",
2720 "The unique identifier of the element.",
2721 NULL,
2722 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
2723
2724 g_object_class_install_property(gobject_class, PROP_NAME,
2725 g_param_spec_string("name",
2726 "Name",
2727 "The friendly/display name of this element.",
2728 NULL,
2729 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
2730
2731 g_object_class_install_property(gobject_class, PROP_TYPE,
2732 g_param_spec_flags("type",
2733 "Element Type",
2734 "The type of element this is.",
2735 PURPLE_TYPE_MEDIA_ELEMENT_TYPE,
2736 PURPLE_MEDIA_ELEMENT_NONE,
2737 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
2738
2739 g_object_class_install_property(gobject_class, PROP_CREATE_CB,
2740 g_param_spec_pointer("create-cb",
2741 "Create Callback",
2742 "The function called to create this element.",
2743 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
2744
2745 g_type_class_add_private(klass, sizeof(PurpleMediaElementInfoPrivate));
2746 }
2747
2748 G_DEFINE_TYPE(PurpleMediaElementInfo,
2749 purple_media_element_info, G_TYPE_OBJECT);
2750 #else
2751 GType
purple_media_element_info_get_type()2752 purple_media_element_info_get_type()
2753 {
2754 return G_TYPE_NONE;
2755 }
2756 #endif
2757
2758 gchar *
purple_media_element_info_get_id(PurpleMediaElementInfo * info)2759 purple_media_element_info_get_id(PurpleMediaElementInfo *info)
2760 {
2761 #ifdef USE_VV
2762 gchar *id;
2763 g_return_val_if_fail(PURPLE_IS_MEDIA_ELEMENT_INFO(info), NULL);
2764 g_object_get(info, "id", &id, NULL);
2765 return id;
2766 #else
2767 return NULL;
2768 #endif
2769 }
2770
2771 gchar *
purple_media_element_info_get_name(PurpleMediaElementInfo * info)2772 purple_media_element_info_get_name(PurpleMediaElementInfo *info)
2773 {
2774 #ifdef USE_VV
2775 gchar *name;
2776 g_return_val_if_fail(PURPLE_IS_MEDIA_ELEMENT_INFO(info), NULL);
2777 g_object_get(info, "name", &name, NULL);
2778 return name;
2779 #else
2780 return NULL;
2781 #endif
2782 }
2783
2784 PurpleMediaElementType
purple_media_element_info_get_element_type(PurpleMediaElementInfo * info)2785 purple_media_element_info_get_element_type(PurpleMediaElementInfo *info)
2786 {
2787 #ifdef USE_VV
2788 PurpleMediaElementType type;
2789 g_return_val_if_fail(PURPLE_IS_MEDIA_ELEMENT_INFO(info),
2790 PURPLE_MEDIA_ELEMENT_NONE);
2791 g_object_get(info, "type", &type, NULL);
2792 return type;
2793 #else
2794 return PURPLE_MEDIA_ELEMENT_NONE;
2795 #endif
2796 }
2797
2798 GstElement *
purple_media_element_info_call_create(PurpleMediaElementInfo * info,PurpleMedia * media,const gchar * session_id,const gchar * participant)2799 purple_media_element_info_call_create(PurpleMediaElementInfo *info,
2800 PurpleMedia *media, const gchar *session_id,
2801 const gchar *participant)
2802 {
2803 #ifdef USE_VV
2804 PurpleMediaElementCreateCallback create;
2805 g_return_val_if_fail(PURPLE_IS_MEDIA_ELEMENT_INFO(info), NULL);
2806 g_object_get(info, "create-cb", &create, NULL);
2807 /*
2808 * In 3.0 the ABI for the callback was changed but we can't do that
2809 * in the 2.x branch. Instead, we have a slightly icky special case.
2810 * since it's purely local to mediamanager.c.
2811 */
2812 if ((void *)create == (void *)&gst_factory_make_cb)
2813 return gst_factory_make_cb(info, media, session_id, participant);
2814 #if GST_CHECK_VERSION(1,4,0)
2815 else if ((void *)create == (void *)&gst_device_create_cb)
2816 return gst_device_create_cb(info, media, session_id, participant);
2817 #endif
2818 else if (create)
2819 return create(media, session_id, participant);
2820 #endif
2821 return NULL;
2822 }
2823
2824
2825 #endif /* USE_GSTREAMER */
2826
2827