Cast back and forth between int and pointer instead of putting pointers
[pidgin-git.git] / pidgin / gtkmedia.c
blob03b09ae629bbf4a06d35bd69986cbac1924b90ee
1 /**
2 * @file media.c Account API
3 * @ingroup core
5 * Pidgin
7 * Pidgin is the legal property of its developers, whose names are too numerous
8 * to list here. Please refer to the COPYRIGHT file distributed with this
9 * source distribution.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 #include "internal.h"
27 #include "debug.h"
28 #include "connection.h"
29 #include "media.h"
30 #include "mediamanager.h"
31 #include "pidgin.h"
32 #include "request.h"
34 #include "gtkmedia.h"
35 #include "gtkutils.h"
36 #include "pidginstock.h"
38 #ifdef USE_VV
39 #include "media-gst.h"
41 #ifdef _WIN32
42 #include <gdk/gdkwin32.h>
43 #endif
45 #include <gst/interfaces/xoverlay.h>
47 #define PIDGIN_TYPE_MEDIA (pidgin_media_get_type())
48 #define PIDGIN_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), PIDGIN_TYPE_MEDIA, PidginMedia))
49 #define PIDGIN_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), PIDGIN_TYPE_MEDIA, PidginMediaClass))
50 #define PIDGIN_IS_MEDIA(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), PIDGIN_TYPE_MEDIA))
51 #define PIDGIN_IS_MEDIA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), PIDGIN_TYPE_MEDIA))
52 #define PIDGIN_MEDIA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), PIDGIN_TYPE_MEDIA, PidginMediaClass))
54 typedef struct _PidginMedia PidginMedia;
55 typedef struct _PidginMediaClass PidginMediaClass;
56 typedef struct _PidginMediaPrivate PidginMediaPrivate;
58 typedef enum
60 /* Waiting for response */
61 PIDGIN_MEDIA_WAITING = 1,
62 /* Got request */
63 PIDGIN_MEDIA_REQUESTED,
64 /* Accepted call */
65 PIDGIN_MEDIA_ACCEPTED,
66 /* Rejected call */
67 PIDGIN_MEDIA_REJECTED,
68 } PidginMediaState;
70 struct _PidginMediaClass
72 GtkWindowClass parent_class;
75 struct _PidginMedia
77 GtkWindow parent;
78 PidginMediaPrivate *priv;
81 struct _PidginMediaPrivate
83 PurpleMedia *media;
84 gchar *screenname;
85 gulong level_handler_id;
87 GtkUIManager *ui;
88 GtkWidget *menubar;
89 GtkWidget *statusbar;
91 GtkWidget *hold;
92 GtkWidget *mute;
93 GtkWidget *pause;
95 GtkWidget *send_progress;
96 GtkWidget *recv_progress;
98 PidginMediaState state;
100 GtkWidget *display;
101 GtkWidget *send_widget;
102 GtkWidget *recv_widget;
103 GtkWidget *button_widget;
104 GtkWidget *local_video;
105 GtkWidget *remote_video;
107 guint timeout_id;
108 PurpleMediaSessionType request_type;
111 #define PIDGIN_MEDIA_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), PIDGIN_TYPE_MEDIA, PidginMediaPrivate))
113 static void pidgin_media_class_init (PidginMediaClass *klass);
114 static void pidgin_media_init (PidginMedia *media);
115 static void pidgin_media_dispose (GObject *object);
116 static void pidgin_media_finalize (GObject *object);
117 static void pidgin_media_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec);
118 static void pidgin_media_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec);
119 static void pidgin_media_set_state(PidginMedia *gtkmedia, PidginMediaState state);
121 static GtkWindowClass *parent_class = NULL;
124 #if 0
125 enum {
126 LAST_SIGNAL
128 static guint pidgin_media_signals[LAST_SIGNAL] = {0};
129 #endif
131 enum {
132 PROP_0,
133 PROP_MEDIA,
134 PROP_SCREENNAME
137 static GType
138 pidgin_media_get_type(void)
140 static GType type = 0;
142 if (type == 0) {
143 static const GTypeInfo info = {
144 sizeof(PidginMediaClass),
145 NULL,
146 NULL,
147 (GClassInitFunc) pidgin_media_class_init,
148 NULL,
149 NULL,
150 sizeof(PidginMedia),
152 (GInstanceInitFunc) pidgin_media_init,
153 NULL
155 type = g_type_register_static(GTK_TYPE_WINDOW, "PidginMedia", &info, 0);
157 return type;
161 static void
162 pidgin_media_class_init (PidginMediaClass *klass)
164 GObjectClass *gobject_class = (GObjectClass*)klass;
165 /* GtkContainerClass *container_class = (GtkContainerClass*)klass; */
166 parent_class = g_type_class_peek_parent(klass);
168 gobject_class->dispose = pidgin_media_dispose;
169 gobject_class->finalize = pidgin_media_finalize;
170 gobject_class->set_property = pidgin_media_set_property;
171 gobject_class->get_property = pidgin_media_get_property;
173 g_object_class_install_property(gobject_class, PROP_MEDIA,
174 g_param_spec_object("media",
175 "PurpleMedia",
176 "The PurpleMedia associated with this media.",
177 PURPLE_TYPE_MEDIA,
178 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
179 g_object_class_install_property(gobject_class, PROP_SCREENNAME,
180 g_param_spec_string("screenname",
181 "Screenname",
182 "The screenname of the user this session is with.",
183 NULL,
184 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE));
186 g_type_class_add_private(klass, sizeof(PidginMediaPrivate));
189 static void
190 pidgin_media_hold_toggled(GtkToggleButton *toggle, PidginMedia *media)
192 purple_media_stream_info(media->priv->media,
193 gtk_toggle_button_get_active(toggle) ?
194 PURPLE_MEDIA_INFO_HOLD : PURPLE_MEDIA_INFO_UNHOLD,
195 NULL, NULL, TRUE);
198 static void
199 pidgin_media_mute_toggled(GtkToggleButton *toggle, PidginMedia *media)
201 purple_media_stream_info(media->priv->media,
202 gtk_toggle_button_get_active(toggle) ?
203 PURPLE_MEDIA_INFO_MUTE : PURPLE_MEDIA_INFO_UNMUTE,
204 NULL, NULL, TRUE);
207 static void
208 pidgin_media_pause_toggled(GtkToggleButton *toggle, PidginMedia *media)
210 purple_media_stream_info(media->priv->media,
211 gtk_toggle_button_get_active(toggle) ?
212 PURPLE_MEDIA_INFO_PAUSE : PURPLE_MEDIA_INFO_UNPAUSE,
213 NULL, NULL, TRUE);
216 static gboolean
217 pidgin_media_delete_event_cb(GtkWidget *widget,
218 GdkEvent *event, PidginMedia *media)
220 if (media->priv->media)
221 purple_media_stream_info(media->priv->media,
222 PURPLE_MEDIA_INFO_HANGUP, NULL, NULL, TRUE);
223 return FALSE;
226 #ifdef HAVE_X11
227 static int
228 pidgin_x_error_handler(Display *display, XErrorEvent *event)
230 const gchar *error_type;
231 switch (event->error_code) {
232 #define XERRORCASE(type) case type: error_type = #type; break
233 XERRORCASE(BadAccess);
234 XERRORCASE(BadAlloc);
235 XERRORCASE(BadAtom);
236 XERRORCASE(BadColor);
237 XERRORCASE(BadCursor);
238 XERRORCASE(BadDrawable);
239 XERRORCASE(BadFont);
240 XERRORCASE(BadGC);
241 XERRORCASE(BadIDChoice);
242 XERRORCASE(BadImplementation);
243 XERRORCASE(BadLength);
244 XERRORCASE(BadMatch);
245 XERRORCASE(BadName);
246 XERRORCASE(BadPixmap);
247 XERRORCASE(BadRequest);
248 XERRORCASE(BadValue);
249 XERRORCASE(BadWindow);
250 #undef XERRORCASE
251 default:
252 error_type = "unknown";
253 break;
255 purple_debug_error("media", "A %s Xlib error has occurred. "
256 "The program would normally crash now.\n",
257 error_type);
258 return 0;
260 #endif
262 static void
263 menu_hangup(GtkAction *action, gpointer data)
265 PidginMedia *gtkmedia = PIDGIN_MEDIA(data);
266 purple_media_stream_info(gtkmedia->priv->media,
267 PURPLE_MEDIA_INFO_HANGUP, NULL, NULL, TRUE);
270 static const GtkActionEntry menu_entries[] = {
271 { "MediaMenu", NULL, N_("_Media"), NULL, NULL, NULL },
272 { "Hangup", NULL, N_("_Hangup"), NULL, NULL, G_CALLBACK(menu_hangup) },
275 static const char *media_menu =
276 "<ui>"
277 "<menubar name='Media'>"
278 "<menu action='MediaMenu'>"
279 "<menuitem action='Hangup'/>"
280 "</menu>"
281 "</menubar>"
282 "</ui>";
284 static GtkWidget *
285 setup_menubar(PidginMedia *window)
287 GtkActionGroup *action_group;
288 GError *error;
289 GtkAccelGroup *accel_group;
290 GtkWidget *menu;
292 action_group = gtk_action_group_new("MediaActions");
293 gtk_action_group_add_actions(action_group,
294 menu_entries,
295 G_N_ELEMENTS(menu_entries),
296 GTK_WINDOW(window));
297 #ifdef ENABLE_NLS
298 gtk_action_group_set_translation_domain(action_group,
299 PACKAGE);
300 #endif
302 window->priv->ui = gtk_ui_manager_new();
303 gtk_ui_manager_insert_action_group(window->priv->ui, action_group, 0);
305 accel_group = gtk_ui_manager_get_accel_group(window->priv->ui);
306 gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);
308 error = NULL;
309 if (!gtk_ui_manager_add_ui_from_string(window->priv->ui, media_menu, -1, &error))
311 g_message("building menus failed: %s", error->message);
312 g_error_free(error);
313 exit(EXIT_FAILURE);
316 menu = gtk_ui_manager_get_widget(window->priv->ui, "/Media");
318 gtk_widget_show(menu);
319 return menu;
322 static void
323 pidgin_media_init (PidginMedia *media)
325 GtkWidget *vbox;
326 media->priv = PIDGIN_MEDIA_GET_PRIVATE(media);
328 #ifdef HAVE_X11
329 XSetErrorHandler(pidgin_x_error_handler);
330 #endif
332 vbox = gtk_vbox_new(FALSE, 0);
333 gtk_container_add(GTK_CONTAINER(media), vbox);
335 media->priv->statusbar = gtk_statusbar_new();
336 gtk_box_pack_end(GTK_BOX(vbox), media->priv->statusbar,
337 FALSE, FALSE, 0);
338 gtk_statusbar_push(GTK_STATUSBAR(media->priv->statusbar),
339 0, _("Calling..."));
340 gtk_widget_show(media->priv->statusbar);
342 media->priv->menubar = setup_menubar(media);
343 gtk_box_pack_start(GTK_BOX(vbox), media->priv->menubar,
344 FALSE, TRUE, 0);
346 media->priv->display = gtk_hbox_new(FALSE, PIDGIN_HIG_BOX_SPACE);
347 gtk_container_set_border_width(GTK_CONTAINER(media->priv->display),
348 PIDGIN_HIG_BOX_SPACE);
349 gtk_box_pack_start(GTK_BOX(vbox), media->priv->display,
350 TRUE, TRUE, PIDGIN_HIG_BOX_SPACE);
351 gtk_widget_show(vbox);
353 g_signal_connect(G_OBJECT(media), "delete-event",
354 G_CALLBACK(pidgin_media_delete_event_cb), media);
357 static void
358 level_message_cb(PurpleMedia *media, gchar *session_id, gchar *participant,
359 double level, PidginMedia *gtkmedia)
361 GtkWidget *progress;
362 if (participant == NULL)
363 progress = gtkmedia->priv->send_progress;
364 else
365 progress = gtkmedia->priv->recv_progress;
366 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(progress), level);
370 static void
371 pidgin_media_disconnect_levels(PurpleMedia *media, PidginMedia *gtkmedia)
373 PurpleMediaManager *manager = purple_media_get_manager(media);
374 GstElement *element = purple_media_manager_get_pipeline(manager);
375 gulong handler_id = g_signal_handler_find(G_OBJECT(gst_pipeline_get_bus(GST_PIPELINE(element))),
376 G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, 0, 0,
377 NULL, G_CALLBACK(level_message_cb), gtkmedia);
378 if (handler_id)
379 g_signal_handler_disconnect(G_OBJECT(gst_pipeline_get_bus(GST_PIPELINE(element))),
380 handler_id);
383 static void
384 pidgin_media_dispose(GObject *media)
386 PidginMedia *gtkmedia = PIDGIN_MEDIA(media);
387 purple_debug_info("gtkmedia", "pidgin_media_dispose\n");
389 if (gtkmedia->priv->media) {
390 purple_request_close_with_handle(gtkmedia);
391 purple_media_remove_output_windows(gtkmedia->priv->media);
392 pidgin_media_disconnect_levels(gtkmedia->priv->media, gtkmedia);
393 g_object_unref(gtkmedia->priv->media);
394 gtkmedia->priv->media = NULL;
397 if (gtkmedia->priv->ui) {
398 g_object_unref(gtkmedia->priv->ui);
399 gtkmedia->priv->ui = NULL;
402 if (gtkmedia->priv->timeout_id != 0)
403 g_source_remove(gtkmedia->priv->timeout_id);
405 G_OBJECT_CLASS(parent_class)->dispose(media);
408 static void
409 pidgin_media_finalize(GObject *media)
411 /* PidginMedia *gtkmedia = PIDGIN_MEDIA(media); */
412 purple_debug_info("gtkmedia", "pidgin_media_finalize\n");
414 G_OBJECT_CLASS(parent_class)->finalize(media);
417 static void
418 pidgin_media_emit_message(PidginMedia *gtkmedia, const char *msg)
420 PurpleConversation *conv = purple_find_conversation_with_account(
421 PURPLE_CONV_TYPE_ANY, gtkmedia->priv->screenname,
422 purple_media_get_account(gtkmedia->priv->media));
423 if (conv != NULL)
424 purple_conversation_write(conv, NULL, msg,
425 PURPLE_MESSAGE_SYSTEM, time(NULL));
428 typedef struct
430 PidginMedia *gtkmedia;
431 gchar *session_id;
432 gchar *participant;
433 } PidginMediaRealizeData;
435 static gboolean
436 realize_cb_cb(PidginMediaRealizeData *data)
438 PidginMediaPrivate *priv = data->gtkmedia->priv;
439 gulong window_id;
441 #ifdef _WIN32
442 if (data->participant == NULL)
443 window_id = GDK_WINDOW_HWND(priv->local_video->window);
444 else
445 window_id = GDK_WINDOW_HWND(priv->remote_video->window);
446 #elif defined(HAVE_X11)
447 if (data->participant == NULL)
448 window_id = GDK_WINDOW_XWINDOW(priv->local_video->window);
449 else
450 window_id = GDK_WINDOW_XWINDOW(priv->remote_video->window);
451 #else
452 # error "Unsupported windowing system"
453 #endif
455 purple_media_set_output_window(priv->media, data->session_id,
456 data->participant, window_id);
458 g_free(data->session_id);
459 g_free(data->participant);
460 g_free(data);
461 return FALSE;
464 static void
465 realize_cb(GtkWidget *widget, PidginMediaRealizeData *data)
467 g_timeout_add(0, (GSourceFunc)realize_cb_cb, data);
470 static void
471 pidgin_media_error_cb(PidginMedia *media, const char *error, PidginMedia *gtkmedia)
473 PurpleConversation *conv = purple_find_conversation_with_account(
474 PURPLE_CONV_TYPE_ANY, gtkmedia->priv->screenname,
475 purple_media_get_account(gtkmedia->priv->media));
476 if (conv != NULL)
477 purple_conversation_write(conv, NULL, error,
478 PURPLE_MESSAGE_ERROR, time(NULL));
479 gtk_statusbar_push(GTK_STATUSBAR(gtkmedia->priv->statusbar),
480 0, error);
483 static void
484 pidgin_media_accept_cb(PurpleMedia *media, int index)
486 purple_media_stream_info(media, PURPLE_MEDIA_INFO_ACCEPT,
487 NULL, NULL, TRUE);
490 static void
491 pidgin_media_reject_cb(PurpleMedia *media, int index)
493 purple_media_stream_info(media, PURPLE_MEDIA_INFO_REJECT,
494 NULL, NULL, TRUE);
497 static gboolean
498 pidgin_request_timeout_cb(PidginMedia *gtkmedia)
500 PurpleAccount *account;
501 PurpleBuddy *buddy;
502 const gchar *alias;
503 PurpleMediaSessionType type;
504 gchar *message = NULL;
506 account = purple_media_get_account(gtkmedia->priv->media);
507 buddy = purple_find_buddy(account, gtkmedia->priv->screenname);
508 alias = buddy ? purple_buddy_get_contact_alias(buddy) :
509 gtkmedia->priv->screenname;
510 type = gtkmedia->priv->request_type;
511 gtkmedia->priv->timeout_id = 0;
513 if (type & PURPLE_MEDIA_AUDIO && type & PURPLE_MEDIA_VIDEO) {
514 message = g_strdup_printf(_("%s wishes to start an audio/video session with you."),
515 alias);
516 } else if (type & PURPLE_MEDIA_AUDIO) {
517 message = g_strdup_printf(_("%s wishes to start an audio session with you."),
518 alias);
519 } else if (type & PURPLE_MEDIA_VIDEO) {
520 message = g_strdup_printf(_("%s wishes to start a video session with you."),
521 alias);
524 gtkmedia->priv->request_type = PURPLE_MEDIA_NONE;
525 if (!purple_media_accepted(gtkmedia->priv->media, NULL, NULL)) {
526 purple_request_accept_cancel(gtkmedia, _("Incoming Call"),
527 message, NULL, PURPLE_DEFAULT_ACTION_NONE,
528 (void*)account, gtkmedia->priv->screenname,
529 NULL, gtkmedia->priv->media,
530 pidgin_media_accept_cb,
531 pidgin_media_reject_cb);
533 pidgin_media_emit_message(gtkmedia, message);
534 g_free(message);
535 return FALSE;
538 static void
539 #if GTK_CHECK_VERSION(2,12,0)
540 pidgin_media_input_volume_changed(GtkScaleButton *range, double value,
541 PurpleMedia *media)
543 double val = (double)value * 100.0;
544 #else
545 pidgin_media_input_volume_changed(GtkRange *range, PurpleMedia *media)
547 double val = (double)gtk_range_get_value(GTK_RANGE(range));
548 #endif
549 purple_media_set_input_volume(media, NULL, val);
552 static void
553 #if GTK_CHECK_VERSION(2,12,0)
554 pidgin_media_output_volume_changed(GtkScaleButton *range, double value,
555 PurpleMedia *media)
557 double val = (double)value * 100.0;
558 #else
559 pidgin_media_output_volume_changed(GtkRange *range, PurpleMedia *media)
561 double val = (double)gtk_range_get_value(GTK_RANGE(range));
562 #endif
563 purple_media_set_output_volume(media, NULL, NULL, val);
566 static GtkWidget *
567 pidgin_media_add_audio_widget(PidginMedia *gtkmedia,
568 PurpleMediaSessionType type)
570 GtkWidget *volume_widget, *progress_parent, *volume, *progress;
571 double value;
573 if (type & PURPLE_MEDIA_SEND_AUDIO) {
574 value = purple_prefs_get_int(
575 "/purple/media/audio/volume/input");
576 } else if (type & PURPLE_MEDIA_RECV_AUDIO) {
577 value = purple_prefs_get_int(
578 "/purple/media/audio/volume/output");
579 } else
580 g_return_val_if_reached(NULL);
582 #if GTK_CHECK_VERSION(2,12,0)
583 /* Setup widget structure */
584 volume_widget = gtk_hbox_new(FALSE, PIDGIN_HIG_BOX_SPACE);
585 progress_parent = gtk_vbox_new(FALSE, 0);
586 gtk_box_pack_start(GTK_BOX(volume_widget),
587 progress_parent, TRUE, TRUE, 0);
589 /* Volume button */
590 volume = gtk_volume_button_new();
591 gtk_scale_button_set_value(GTK_SCALE_BUTTON(volume), value/100.0);
592 gtk_box_pack_end(GTK_BOX(volume_widget),
593 volume, FALSE, FALSE, 0);
594 #else
595 /* Setup widget structure */
596 volume_widget = gtk_vbox_new(FALSE, 0);
597 progress_parent = volume_widget;
599 /* Volume slider */
600 volume = gtk_hscale_new_with_range(0.0, 100.0, 5.0);
601 gtk_range_set_increments(GTK_RANGE(volume), 5.0, 25.0);
602 gtk_range_set_value(GTK_RANGE(volume), value);
603 gtk_scale_set_draw_value(GTK_SCALE(volume), FALSE);
604 gtk_box_pack_end(GTK_BOX(volume_widget),
605 volume, TRUE, FALSE, 0);
606 #endif
608 /* Volume level indicator */
609 progress = gtk_progress_bar_new();
610 gtk_widget_set_size_request(progress, 250, 10);
611 gtk_box_pack_end(GTK_BOX(progress_parent), progress, TRUE, FALSE, 0);
613 if (type & PURPLE_MEDIA_SEND_AUDIO) {
614 g_signal_connect (G_OBJECT(volume), "value-changed",
615 G_CALLBACK(pidgin_media_input_volume_changed),
616 gtkmedia->priv->media);
617 gtkmedia->priv->send_progress = progress;
618 } else if (type & PURPLE_MEDIA_RECV_AUDIO) {
619 g_signal_connect (G_OBJECT(volume), "value-changed",
620 G_CALLBACK(pidgin_media_output_volume_changed),
621 gtkmedia->priv->media);
622 gtkmedia->priv->recv_progress = progress;
625 gtk_widget_show_all(volume_widget);
627 return volume_widget;
630 static void
631 pidgin_media_ready_cb(PurpleMedia *media, PidginMedia *gtkmedia, const gchar *sid)
633 GtkWidget *send_widget = NULL, *recv_widget = NULL, *button_widget = NULL;
634 PurpleMediaSessionType type =
635 purple_media_get_session_type(media, sid);
636 GdkPixbuf *icon = NULL;
638 if (gtkmedia->priv->recv_widget == NULL
639 && type & (PURPLE_MEDIA_RECV_VIDEO |
640 PURPLE_MEDIA_RECV_AUDIO)) {
641 recv_widget = gtk_vbox_new(FALSE, PIDGIN_HIG_BOX_SPACE);
642 gtk_box_pack_start(GTK_BOX(gtkmedia->priv->display),
643 recv_widget, TRUE, TRUE, 0);
644 gtk_widget_show(recv_widget);
645 } else {
646 recv_widget = gtkmedia->priv->recv_widget;
648 if (gtkmedia->priv->send_widget == NULL
649 && type & (PURPLE_MEDIA_SEND_VIDEO |
650 PURPLE_MEDIA_SEND_AUDIO)) {
651 send_widget = gtk_vbox_new(FALSE, PIDGIN_HIG_BOX_SPACE);
652 gtk_box_pack_start(GTK_BOX(gtkmedia->priv->display),
653 send_widget, FALSE, TRUE, 0);
654 button_widget = gtk_hbox_new(FALSE, PIDGIN_HIG_BOX_SPACE);
655 gtk_box_pack_end(GTK_BOX(recv_widget), button_widget,
656 FALSE, TRUE, 0);
657 gtk_widget_show(send_widget);
659 /* Hold button */
660 gtkmedia->priv->hold =
661 gtk_toggle_button_new_with_mnemonic("_Hold");
662 gtk_box_pack_end(GTK_BOX(button_widget), gtkmedia->priv->hold,
663 FALSE, FALSE, 0);
664 gtk_widget_show(gtkmedia->priv->hold);
665 g_signal_connect(gtkmedia->priv->hold, "toggled",
666 G_CALLBACK(pidgin_media_hold_toggled),
667 gtkmedia);
668 } else {
669 send_widget = gtkmedia->priv->send_widget;
670 button_widget = gtkmedia->priv->button_widget;
673 if (type & PURPLE_MEDIA_RECV_VIDEO) {
674 PidginMediaRealizeData *data;
675 GtkWidget *aspect;
676 GtkWidget *remote_video;
677 GdkColor color = {0, 0, 0, 0};
679 aspect = gtk_aspect_frame_new(NULL, 0, 0, 4.0/3.0, FALSE);
680 gtk_frame_set_shadow_type(GTK_FRAME(aspect), GTK_SHADOW_IN);
681 gtk_box_pack_start(GTK_BOX(recv_widget), aspect, TRUE, TRUE, 0);
683 data = g_new0(PidginMediaRealizeData, 1);
684 data->gtkmedia = gtkmedia;
685 data->session_id = g_strdup(sid);
686 data->participant = g_strdup(gtkmedia->priv->screenname);
688 remote_video = gtk_drawing_area_new();
689 gtk_widget_modify_bg(remote_video, GTK_STATE_NORMAL, &color);
690 g_signal_connect(G_OBJECT(remote_video), "realize",
691 G_CALLBACK(realize_cb), data);
692 gtk_container_add(GTK_CONTAINER(aspect), remote_video);
693 gtk_widget_set_size_request (GTK_WIDGET(remote_video), 320, 240);
694 gtk_widget_show(remote_video);
695 gtk_widget_show(aspect);
697 gtkmedia->priv->remote_video = remote_video;
700 if (type & PURPLE_MEDIA_SEND_VIDEO) {
701 PidginMediaRealizeData *data;
702 GtkWidget *aspect;
703 GtkWidget *local_video;
704 GdkColor color = {0, 0, 0, 0};
706 aspect = gtk_aspect_frame_new(NULL, 0, 0, 4.0/3.0, TRUE);
707 gtk_frame_set_shadow_type(GTK_FRAME(aspect), GTK_SHADOW_IN);
708 gtk_box_pack_start(GTK_BOX(send_widget), aspect, FALSE, TRUE, 0);
710 data = g_new0(PidginMediaRealizeData, 1);
711 data->gtkmedia = gtkmedia;
712 data->session_id = g_strdup(sid);
713 data->participant = NULL;
715 local_video = gtk_drawing_area_new();
716 gtk_widget_modify_bg(local_video, GTK_STATE_NORMAL, &color);
717 g_signal_connect(G_OBJECT(local_video), "realize",
718 G_CALLBACK(realize_cb), data);
719 gtk_container_add(GTK_CONTAINER(aspect), local_video);
720 gtk_widget_set_size_request (GTK_WIDGET(local_video), 80, 60);
722 gtk_widget_show(local_video);
723 gtk_widget_show(aspect);
725 gtkmedia->priv->pause =
726 gtk_toggle_button_new_with_mnemonic(_("_Pause"));
727 gtk_box_pack_end(GTK_BOX(button_widget), gtkmedia->priv->pause,
728 FALSE, FALSE, 0);
729 gtk_widget_show(gtkmedia->priv->pause);
730 g_signal_connect(gtkmedia->priv->pause, "toggled",
731 G_CALLBACK(pidgin_media_pause_toggled),
732 gtkmedia);
734 gtkmedia->priv->local_video = local_video;
736 if (type & PURPLE_MEDIA_RECV_AUDIO) {
737 gtk_box_pack_end(GTK_BOX(recv_widget),
738 pidgin_media_add_audio_widget(gtkmedia,
739 PURPLE_MEDIA_RECV_AUDIO), FALSE, FALSE, 0);
742 if (type & PURPLE_MEDIA_SEND_AUDIO) {
743 gtkmedia->priv->mute =
744 gtk_toggle_button_new_with_mnemonic("_Mute");
745 gtk_box_pack_end(GTK_BOX(button_widget), gtkmedia->priv->mute,
746 FALSE, FALSE, 0);
747 gtk_widget_show(gtkmedia->priv->mute);
748 g_signal_connect(gtkmedia->priv->mute, "toggled",
749 G_CALLBACK(pidgin_media_mute_toggled),
750 gtkmedia);
752 gtk_box_pack_end(GTK_BOX(recv_widget),
753 pidgin_media_add_audio_widget(gtkmedia,
754 PURPLE_MEDIA_SEND_AUDIO), FALSE, FALSE, 0);
757 if (type & PURPLE_MEDIA_AUDIO &&
758 gtkmedia->priv->level_handler_id == 0) {
759 gtkmedia->priv->level_handler_id = g_signal_connect(
760 media, "level", G_CALLBACK(level_message_cb),
761 gtkmedia);
764 if (send_widget != NULL)
765 gtkmedia->priv->send_widget = send_widget;
766 if (recv_widget != NULL)
767 gtkmedia->priv->recv_widget = recv_widget;
768 if (button_widget != NULL) {
769 gtkmedia->priv->button_widget = button_widget;
770 gtk_widget_show(GTK_WIDGET(button_widget));
773 if (purple_media_is_initiator(media, sid, NULL) == FALSE) {
774 if (gtkmedia->priv->timeout_id != 0)
775 g_source_remove(gtkmedia->priv->timeout_id);
776 gtkmedia->priv->request_type |= type;
777 gtkmedia->priv->timeout_id = g_timeout_add(500,
778 (GSourceFunc)pidgin_request_timeout_cb,
779 gtkmedia);
782 /* set the window icon according to the type */
783 if (type & PURPLE_MEDIA_VIDEO) {
784 icon = gtk_widget_render_icon(GTK_WIDGET(gtkmedia),
785 PIDGIN_STOCK_TOOLBAR_VIDEO_CALL,
786 gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_LARGE), NULL);
787 } else if (type & PURPLE_MEDIA_AUDIO) {
788 icon = gtk_widget_render_icon(GTK_WIDGET(gtkmedia),
789 PIDGIN_STOCK_TOOLBAR_AUDIO_CALL,
790 gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_LARGE), NULL);
793 if (icon) {
794 gtk_window_set_icon(GTK_WINDOW(gtkmedia), icon);
795 g_object_unref(icon);
798 gtk_widget_show(gtkmedia->priv->display);
801 static void
802 pidgin_media_state_changed_cb(PurpleMedia *media, PurpleMediaState state,
803 gchar *sid, gchar *name, PidginMedia *gtkmedia)
805 purple_debug_info("gtkmedia", "state: %d sid: %s name: %s\n",
806 state, sid ? sid : "(null)", name ? name : "(null)");
807 if (sid == NULL && name == NULL) {
808 if (state == PURPLE_MEDIA_STATE_END) {
809 pidgin_media_emit_message(gtkmedia,
810 _("The call has been terminated."));
811 gtk_widget_destroy(GTK_WIDGET(gtkmedia));
813 } else if (state == PURPLE_MEDIA_STATE_NEW &&
814 sid != NULL && name != NULL) {
815 pidgin_media_ready_cb(media, gtkmedia, sid);
819 static void
820 pidgin_media_stream_info_cb(PurpleMedia *media, PurpleMediaInfoType type,
821 gchar *sid, gchar *name, gboolean local,
822 PidginMedia *gtkmedia)
824 if (type == PURPLE_MEDIA_INFO_REJECT) {
825 pidgin_media_emit_message(gtkmedia,
826 _("You have rejected the call."));
827 } else if (type == PURPLE_MEDIA_INFO_ACCEPT) {
828 if (local == TRUE)
829 purple_request_close_with_handle(gtkmedia);
830 pidgin_media_set_state(gtkmedia, PIDGIN_MEDIA_ACCEPTED);
831 pidgin_media_emit_message(gtkmedia, _("Call in progress."));
832 gtk_statusbar_push(GTK_STATUSBAR(gtkmedia->priv->statusbar),
833 0, _("Call in progress."));
834 gtk_widget_show(GTK_WIDGET(gtkmedia));
838 static void
839 pidgin_media_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
841 PidginMedia *media;
842 g_return_if_fail(PIDGIN_IS_MEDIA(object));
844 media = PIDGIN_MEDIA(object);
845 switch (prop_id) {
846 case PROP_MEDIA:
848 if (media->priv->media)
849 g_object_unref(media->priv->media);
850 media->priv->media = g_value_get_object(value);
851 g_object_ref(media->priv->media);
853 if (purple_media_is_initiator(media->priv->media,
854 NULL, NULL) == TRUE)
855 pidgin_media_set_state(media, PIDGIN_MEDIA_WAITING);
856 else
857 pidgin_media_set_state(media, PIDGIN_MEDIA_REQUESTED);
859 g_signal_connect(G_OBJECT(media->priv->media), "error",
860 G_CALLBACK(pidgin_media_error_cb), media);
861 g_signal_connect(G_OBJECT(media->priv->media), "state-changed",
862 G_CALLBACK(pidgin_media_state_changed_cb), media);
863 g_signal_connect(G_OBJECT(media->priv->media), "stream-info",
864 G_CALLBACK(pidgin_media_stream_info_cb), media);
865 break;
867 case PROP_SCREENNAME:
868 if (media->priv->screenname)
869 g_free(media->priv->screenname);
870 media->priv->screenname = g_value_dup_string(value);
871 break;
872 default:
873 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
874 break;
878 static void
879 pidgin_media_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
881 PidginMedia *media;
882 g_return_if_fail(PIDGIN_IS_MEDIA(object));
884 media = PIDGIN_MEDIA(object);
886 switch (prop_id) {
887 case PROP_MEDIA:
888 g_value_set_object(value, media->priv->media);
889 break;
890 case PROP_SCREENNAME:
891 g_value_set_string(value, media->priv->screenname);
892 break;
893 default:
894 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
895 break;
899 static GtkWidget *
900 pidgin_media_new(PurpleMedia *media, const gchar *screenname)
902 PidginMedia *gtkmedia = g_object_new(pidgin_media_get_type(),
903 "media", media,
904 "screenname", screenname, NULL);
905 return GTK_WIDGET(gtkmedia);
908 static void
909 pidgin_media_set_state(PidginMedia *gtkmedia, PidginMediaState state)
911 gtkmedia->priv->state = state;
914 static gboolean
915 pidgin_media_new_cb(PurpleMediaManager *manager, PurpleMedia *media,
916 PurpleAccount *account, gchar *screenname, gpointer nul)
918 PidginMedia *gtkmedia = PIDGIN_MEDIA(
919 pidgin_media_new(media, screenname));
920 PurpleBuddy *buddy = purple_find_buddy(account, screenname);
921 const gchar *alias = buddy ?
922 purple_buddy_get_contact_alias(buddy) : screenname;
923 gtk_window_set_title(GTK_WINDOW(gtkmedia), alias);
925 if (purple_media_is_initiator(media, NULL, NULL) == TRUE)
926 gtk_widget_show(GTK_WIDGET(gtkmedia));
928 return TRUE;
931 static GstElement *
932 create_default_video_src(PurpleMedia *media,
933 const gchar *session_id, const gchar *participant)
935 GstElement *sendbin, *src, *videoscale, *capsfilter;
936 GstPad *pad;
937 GstPad *ghost;
938 GstCaps *caps;
940 #ifdef _WIN32
941 /* autovideosrc doesn't pick ksvideosrc for some reason */
942 src = gst_element_factory_make("ksvideosrc", NULL);
943 if (src == NULL)
944 src = gst_element_factory_make("dshowvideosrc", NULL);
945 if (src == NULL)
946 src = gst_element_factory_make("autovideosrc", NULL);
947 #else
948 src = gst_element_factory_make("gconfvideosrc", NULL);
949 if (src == NULL)
950 src = gst_element_factory_make("autovideosrc", NULL);
951 if (src == NULL)
952 src = gst_element_factory_make("v4l2src", NULL);
953 if (src == NULL)
954 src = gst_element_factory_make("v4lsrc", NULL);
955 #endif
956 if (src == NULL) {
957 purple_debug_error("gtkmedia", "Unable to find a suitable "
958 "element for the default video source.\n");
959 return NULL;
962 sendbin = gst_bin_new("pidgindefaultvideosrc");
963 videoscale = gst_element_factory_make("videoscale", NULL);
964 capsfilter = gst_element_factory_make("capsfilter", NULL);
966 /* It was recommended to set the size <= 352x288 and framerate <= 20 */
967 caps = gst_caps_from_string("video/x-raw-yuv , width=[250,352] , "
968 "height=[200,288] , framerate=[1/1,20/1]");
969 g_object_set(G_OBJECT(capsfilter), "caps", caps, NULL);
971 gst_bin_add_many(GST_BIN(sendbin), src,
972 videoscale, capsfilter, NULL);
973 gst_element_link_many(src, videoscale, capsfilter, NULL);
975 pad = gst_element_get_static_pad(capsfilter, "src");
976 ghost = gst_ghost_pad_new("ghostsrc", pad);
977 gst_object_unref(pad);
978 gst_element_add_pad(sendbin, ghost);
980 return sendbin;
983 static GstElement *
984 create_default_video_sink(PurpleMedia *media,
985 const gchar *session_id, const gchar *participant)
987 GstElement *sink = gst_element_factory_make("gconfvideosink", NULL);
988 if (sink == NULL)
989 sink = gst_element_factory_make("autovideosink", NULL);
990 if (sink == NULL)
991 purple_debug_error("gtkmedia", "Unable to find a suitable "
992 "element for the default video sink.\n");
993 return sink;
996 static GstElement *
997 create_default_audio_src(PurpleMedia *media,
998 const gchar *session_id, const gchar *participant)
1000 GstElement *src;
1001 src = gst_element_factory_make("gconfaudiosrc", NULL);
1002 if (src == NULL)
1003 src = gst_element_factory_make("autoaudiosrc", NULL);
1004 if (src == NULL)
1005 src = gst_element_factory_make("alsasrc", NULL);
1006 if (src == NULL)
1007 src = gst_element_factory_make("osssrc", NULL);
1008 if (src == NULL)
1009 src = gst_element_factory_make("dshowaudiosrc", NULL);
1010 if (src == NULL) {
1011 purple_debug_error("gtkmedia", "Unable to find a suitable "
1012 "element for the default audio source.\n");
1013 return NULL;
1015 gst_element_set_name(src, "pidgindefaultaudiosrc");
1016 return src;
1019 static GstElement *
1020 create_default_audio_sink(PurpleMedia *media,
1021 const gchar *session_id, const gchar *participant)
1023 GstElement *sink;
1024 sink = gst_element_factory_make("gconfaudiosink", NULL);
1025 if (sink == NULL)
1026 sink = gst_element_factory_make("autoaudiosink",NULL);
1027 if (sink == NULL) {
1028 purple_debug_error("gtkmedia", "Unable to find a suitable "
1029 "element for the default audio sink.\n");
1030 return NULL;
1032 return sink;
1034 #endif /* USE_VV */
1036 void
1037 pidgin_medias_init(void)
1039 #ifdef USE_VV
1040 PurpleMediaManager *manager = purple_media_manager_get();
1041 PurpleMediaElementInfo *default_video_src =
1042 g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
1043 "id", "pidgindefaultvideosrc",
1044 "name", "Pidgin Default Video Source",
1045 "type", PURPLE_MEDIA_ELEMENT_VIDEO
1046 | PURPLE_MEDIA_ELEMENT_SRC
1047 | PURPLE_MEDIA_ELEMENT_ONE_SRC
1048 | PURPLE_MEDIA_ELEMENT_UNIQUE,
1049 "create-cb", create_default_video_src, NULL);
1050 PurpleMediaElementInfo *default_video_sink =
1051 g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
1052 "id", "pidgindefaultvideosink",
1053 "name", "Pidgin Default Video Sink",
1054 "type", PURPLE_MEDIA_ELEMENT_VIDEO
1055 | PURPLE_MEDIA_ELEMENT_SINK
1056 | PURPLE_MEDIA_ELEMENT_ONE_SINK,
1057 "create-cb", create_default_video_sink, NULL);
1058 PurpleMediaElementInfo *default_audio_src =
1059 g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
1060 "id", "pidgindefaultaudiosrc",
1061 "name", "Pidgin Default Audio Source",
1062 "type", PURPLE_MEDIA_ELEMENT_AUDIO
1063 | PURPLE_MEDIA_ELEMENT_SRC
1064 | PURPLE_MEDIA_ELEMENT_ONE_SRC
1065 | PURPLE_MEDIA_ELEMENT_UNIQUE,
1066 "create-cb", create_default_audio_src, NULL);
1067 PurpleMediaElementInfo *default_audio_sink =
1068 g_object_new(PURPLE_TYPE_MEDIA_ELEMENT_INFO,
1069 "id", "pidgindefaultaudiosink",
1070 "name", "Pidgin Default Audio Sink",
1071 "type", PURPLE_MEDIA_ELEMENT_AUDIO
1072 | PURPLE_MEDIA_ELEMENT_SINK
1073 | PURPLE_MEDIA_ELEMENT_ONE_SINK,
1074 "create-cb", create_default_audio_sink, NULL);
1076 g_signal_connect(G_OBJECT(manager), "init-media",
1077 G_CALLBACK(pidgin_media_new_cb), NULL);
1079 purple_media_manager_set_ui_caps(manager,
1080 PURPLE_MEDIA_CAPS_AUDIO |
1081 PURPLE_MEDIA_CAPS_AUDIO_SINGLE_DIRECTION |
1082 PURPLE_MEDIA_CAPS_VIDEO |
1083 PURPLE_MEDIA_CAPS_VIDEO_SINGLE_DIRECTION |
1084 PURPLE_MEDIA_CAPS_AUDIO_VIDEO);
1086 purple_debug_info("gtkmedia", "Registering media element types\n");
1087 purple_media_manager_set_active_element(manager, default_video_src);
1088 purple_media_manager_set_active_element(manager, default_video_sink);
1089 purple_media_manager_set_active_element(manager, default_audio_src);
1090 purple_media_manager_set_active_element(manager, default_audio_sink);
1091 #endif