2 * Copyright (C) 2007 Elliot Fairweather
3 * Copyright (C) 2007-2008 Collabora Ltd.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Authors: Elliot Fairweather <elliot.fairweather@collabora.co.uk>
20 * Xavier Claessens <xclaesse@gmail.com>
25 #include <telepathy-glib/proxy-subclass.h>
26 #include <telepathy-glib/dbus.h>
27 #include <telepathy-glib/interfaces.h>
29 #include "empathy-tp-call.h"
30 #include "empathy-contact-factory.h"
31 #include "empathy-utils.h"
33 #define DEBUG_FLAG EMPATHY_DEBUG_TP
34 #include "empathy-debug.h"
36 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpCall)
39 gboolean dispose_has_run;
41 EmpathyContact *contact;
45 EmpathyTpCallStream *audio;
46 EmpathyTpCallStream *video;
60 G_DEFINE_TYPE (EmpathyTpCall, empathy_tp_call, G_TYPE_OBJECT)
63 tp_call_add_stream (EmpathyTpCall *call,
68 guint stream_direction)
70 EmpathyTpCallPriv *priv = GET_PRIV (call);
74 case TP_MEDIA_STREAM_TYPE_AUDIO:
75 DEBUG ("Audio stream - id: %d, state: %d, direction: %d",
76 stream_id, stream_state, stream_direction);
77 priv->audio->exists = TRUE;
78 priv->audio->id = stream_id;
79 priv->audio->state = stream_state;
80 priv->audio->direction = stream_direction;
81 g_object_notify (G_OBJECT (call), "audio-stream");
83 case TP_MEDIA_STREAM_TYPE_VIDEO:
84 DEBUG ("Video stream - id: %d, state: %d, direction: %d",
85 stream_id, stream_state, stream_direction);
86 priv->video->exists = TRUE;
87 priv->video->id = stream_id;
88 priv->video->state = stream_state;
89 priv->video->direction = stream_direction;
90 g_object_notify (G_OBJECT (call), "video-stream");
93 DEBUG ("Unknown stream type: %d", stream_type);
98 tp_call_stream_added_cb (TpChannel *channel,
100 guint contact_handle,
105 DEBUG ("Stream added - stream id: %d, contact handle: %d, stream type: %d",
106 stream_id, contact_handle, stream_type);
108 tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
109 stream_type, TP_MEDIA_STREAM_STATE_DISCONNECTED,
110 TP_MEDIA_STREAM_DIRECTION_NONE);
114 tp_call_stream_removed_cb (TpChannel *channel,
119 EmpathyTpCallPriv *priv = GET_PRIV (call);
121 DEBUG ("Stream removed - stream id: %d", stream_id);
123 if (stream_id == priv->audio->id)
125 priv->audio->exists = FALSE;
126 g_object_notify (call, "audio-stream");
128 else if (stream_id == priv->video->id)
130 priv->video->exists = FALSE;
131 g_object_notify (call, "video-stream");
136 tp_call_stream_state_changed_cb (TpChannel *proxy,
142 EmpathyTpCallPriv *priv = GET_PRIV (call);
144 DEBUG ("Stream state changed - stream id: %d, state state: %d",
145 stream_id, stream_state);
147 if (stream_id == priv->audio->id)
149 priv->audio->state = stream_state;
150 g_object_notify (call, "audio-stream");
152 else if (stream_id == priv->video->id)
154 priv->video->state = stream_state;
155 g_object_notify (call, "video-stream");
160 tp_call_stream_direction_changed_cb (TpChannel *channel,
162 guint stream_direction,
167 EmpathyTpCallPriv *priv = GET_PRIV (call);
169 DEBUG ("Stream direction changed - stream: %d, direction: %d",
170 stream_id, stream_direction);
172 if (stream_id == priv->audio->id)
174 priv->audio->direction = stream_direction;
175 g_object_notify (call, "audio-stream");
177 else if (stream_id == priv->video->id)
179 priv->video->direction = stream_direction;
180 g_object_notify (call, "video-stream");
185 tp_call_request_streams_cb (TpChannel *channel,
186 const GPtrArray *streams,
195 DEBUG ("Error requesting streams: %s", error->message);
199 for (i = 0; i < streams->len; i++)
203 guint contact_handle;
206 guint stream_direction;
208 values = g_ptr_array_index (streams, i);
209 stream_id = g_value_get_uint (g_value_array_get_nth (values, 0));
210 contact_handle = g_value_get_uint (g_value_array_get_nth (values, 1));
211 stream_type = g_value_get_uint (g_value_array_get_nth (values, 2));
212 stream_state = g_value_get_uint (g_value_array_get_nth (values, 3));
213 stream_direction = g_value_get_uint (g_value_array_get_nth (values, 4));
215 tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
216 stream_type, stream_state, stream_direction);
221 tp_call_request_streams_for_capabilities (EmpathyTpCall *call,
222 EmpathyCapabilities capabilities)
224 EmpathyTpCallPriv *priv = GET_PRIV (call);
225 GArray *stream_types;
229 if (capabilities == EMPATHY_CAPABILITIES_UNKNOWN)
230 capabilities = EMPATHY_CAPABILITIES_AUDIO | EMPATHY_CAPABILITIES_VIDEO;
232 DEBUG ("Requesting new stream for capabilities %d",
235 stream_types = g_array_new (FALSE, FALSE, sizeof (guint));
236 handle = empathy_contact_get_handle (priv->contact);
238 if (capabilities & EMPATHY_CAPABILITIES_AUDIO)
240 stream_type = TP_MEDIA_STREAM_TYPE_AUDIO;
241 g_array_append_val (stream_types, stream_type);
243 if (capabilities & EMPATHY_CAPABILITIES_VIDEO)
245 stream_type = TP_MEDIA_STREAM_TYPE_VIDEO;
246 g_array_append_val (stream_types, stream_type);
249 tp_cli_channel_type_streamed_media_call_request_streams (priv->channel, -1,
250 handle, stream_types, tp_call_request_streams_cb, NULL, NULL,
253 g_array_free (stream_types, TRUE);
256 static EmpathyContact *
257 tp_call_dup_contact_from_handle (EmpathyTpCall *call, TpHandle handle)
259 EmpathyTpCallPriv *priv = GET_PRIV (call);
260 EmpathyContactFactory *factory;
262 EmpathyContact *contact;
264 factory = empathy_contact_factory_dup_singleton ();
265 account = empathy_channel_get_account (priv->channel);
266 contact = empathy_contact_factory_get_from_handle (factory, account, handle);
268 g_object_unref (factory);
269 g_object_unref (account);
275 tp_call_update_status (EmpathyTpCall *call)
277 EmpathyTpCallPriv *priv = GET_PRIV (call);
278 TpHandle self_handle;
284 self_handle = tp_channel_group_get_self_handle (priv->channel);
285 set = tp_channel_group_get_members (priv->channel);
286 tp_intset_iter_init (&iter, set);
287 while (tp_intset_iter_next (&iter))
289 if (priv->contact == NULL && iter.element != self_handle)
291 /* We found the remote contact */
292 priv->contact = tp_call_dup_contact_from_handle (call, iter.element);
293 priv->is_incoming = TRUE;
294 priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
295 g_object_notify (G_OBJECT (call), "is-incoming");
296 g_object_notify (G_OBJECT (call), "contact");
297 g_object_notify (G_OBJECT (call), "status");
300 if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING &&
301 ((priv->is_incoming && iter.element == self_handle) ||
302 (!priv->is_incoming && iter.element != self_handle)))
304 priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
305 g_object_notify (G_OBJECT (call), "status");
309 g_object_unref (call);
313 tp_call_members_changed_cb (TpChannel *channel,
317 GArray *local_pending,
318 GArray *remote_pending,
323 tp_call_update_status (call);
327 empathy_tp_call_to (EmpathyTpCall *call, EmpathyContact *contact)
329 EmpathyTpCallPriv *priv = GET_PRIV (call);
331 priv->contact = g_object_ref (contact);
332 priv->is_incoming = FALSE;
333 priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
334 g_object_notify (G_OBJECT (call), "is-incoming");
335 g_object_notify (G_OBJECT (call), "contact");
336 g_object_notify (G_OBJECT (call), "status");
337 tp_call_request_streams_for_capabilities (call,
338 EMPATHY_CAPABILITIES_VIDEO | EMPATHY_CAPABILITIES_AUDIO);
342 tp_call_channel_invalidated_cb (TpChannel *channel,
348 EmpathyTpCallPriv *priv = GET_PRIV (call);
350 DEBUG ("Channel invalidated: %s", message);
351 priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
352 g_object_notify (G_OBJECT (call), "status");
356 tp_call_async_cb (TpProxy *proxy,
362 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
366 tp_call_close_channel (EmpathyTpCall *call)
368 EmpathyTpCallPriv *priv = GET_PRIV (call);
370 if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
373 DEBUG ("Closing channel");
375 tp_cli_channel_call_close (priv->channel, -1,
376 NULL, NULL, NULL, NULL);
378 priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
379 g_object_notify (G_OBJECT (call), "status");
383 tp_call_constructor (GType type,
384 guint n_construct_params,
385 GObjectConstructParam *construct_params)
389 EmpathyTpCallPriv *priv;
391 object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
392 n_construct_params, construct_params);
394 call = EMPATHY_TP_CALL (object);
395 priv = GET_PRIV (call);
397 /* Setup streamed media channel */
398 g_signal_connect (priv->channel, "invalidated",
399 G_CALLBACK (tp_call_channel_invalidated_cb), call);
400 tp_cli_channel_type_streamed_media_connect_to_stream_added (priv->channel,
401 tp_call_stream_added_cb, NULL, NULL, G_OBJECT (call), NULL);
402 tp_cli_channel_type_streamed_media_connect_to_stream_removed (priv->channel,
403 tp_call_stream_removed_cb, NULL, NULL, G_OBJECT (call), NULL);
404 tp_cli_channel_type_streamed_media_connect_to_stream_state_changed (priv->channel,
405 tp_call_stream_state_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
406 tp_cli_channel_type_streamed_media_connect_to_stream_direction_changed (priv->channel,
407 tp_call_stream_direction_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
408 tp_cli_channel_type_streamed_media_call_list_streams (priv->channel, -1,
409 tp_call_request_streams_cb, NULL, NULL, G_OBJECT (call));
411 /* Update status when members changes */
412 tp_call_update_status (call);
413 g_signal_connect (priv->channel, "group-members-changed",
414 G_CALLBACK (tp_call_members_changed_cb), call);
419 tp_call_dispose (GObject *object)
421 EmpathyTpCallPriv *priv = GET_PRIV (object);
423 DEBUG ("Disposing: %p, %d", object, priv->dispose_has_run);
425 if (priv->dispose_has_run)
428 priv->dispose_has_run = TRUE;
430 g_slice_free (EmpathyTpCallStream, priv->audio);
431 g_slice_free (EmpathyTpCallStream, priv->video);
433 if (priv->channel != NULL)
435 g_signal_handlers_disconnect_by_func (priv->channel,
436 tp_call_channel_invalidated_cb, object);
438 g_object_unref (priv->channel);
439 priv->channel = NULL;
442 if (priv->contact != NULL)
443 g_object_unref (priv->contact);
445 if (G_OBJECT_CLASS (empathy_tp_call_parent_class)->dispose)
446 G_OBJECT_CLASS (empathy_tp_call_parent_class)->dispose (object);
450 tp_call_finalize (GObject *object)
452 EmpathyTpCallPriv *priv = GET_PRIV (object);
454 DEBUG ("Finalizing: %p", object);
456 g_slice_free (EmpathyTpCallStream, priv->audio);
457 g_slice_free (EmpathyTpCallStream, priv->video);
459 (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
463 tp_call_set_property (GObject *object,
468 EmpathyTpCallPriv *priv = GET_PRIV (object);
473 priv->channel = g_value_dup_object (value);
476 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
482 tp_call_get_property (GObject *object,
487 EmpathyTpCallPriv *priv = GET_PRIV (object);
492 g_value_set_object (value, priv->channel);
495 g_value_set_object (value, priv->contact);
497 case PROP_IS_INCOMING:
498 g_value_set_boolean (value, priv->is_incoming);
501 g_value_set_uint (value, priv->status);
503 case PROP_AUDIO_STREAM:
504 g_value_set_pointer (value, priv->audio);
506 case PROP_VIDEO_STREAM:
507 g_value_set_pointer (value, priv->video);
510 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
516 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
518 GObjectClass *object_class = G_OBJECT_CLASS (klass);
520 object_class->constructor = tp_call_constructor;
521 object_class->dispose = tp_call_dispose;
522 object_class->finalize = tp_call_finalize;
523 object_class->set_property = tp_call_set_property;
524 object_class->get_property = tp_call_get_property;
526 g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
528 g_object_class_install_property (object_class, PROP_CHANNEL,
529 g_param_spec_object ("channel", "channel", "channel",
531 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
532 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
533 g_object_class_install_property (object_class, PROP_CONTACT,
534 g_param_spec_object ("contact", "Call contact", "Call contact",
535 EMPATHY_TYPE_CONTACT,
536 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
537 g_object_class_install_property (object_class, PROP_IS_INCOMING,
538 g_param_spec_boolean ("is-incoming", "Is media stream incoming",
539 "Is media stream incoming", FALSE, G_PARAM_READABLE |
540 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
541 g_object_class_install_property (object_class, PROP_STATUS,
542 g_param_spec_uint ("status", "Call status",
543 "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
544 G_PARAM_STATIC_BLURB));
545 g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
546 g_param_spec_pointer ("audio-stream", "Audio stream data",
548 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
549 g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
550 g_param_spec_pointer ("video-stream", "Video stream data",
552 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
556 empathy_tp_call_init (EmpathyTpCall *call)
558 EmpathyTpCallPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (call,
559 EMPATHY_TYPE_TP_CALL, EmpathyTpCallPriv);
562 priv->status = EMPATHY_TP_CALL_STATUS_READYING;
563 priv->contact = NULL;
564 priv->audio = g_slice_new0 (EmpathyTpCallStream);
565 priv->video = g_slice_new0 (EmpathyTpCallStream);
566 priv->audio->exists = FALSE;
567 priv->video->exists = FALSE;
571 empathy_tp_call_new (TpChannel *channel)
573 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
575 return g_object_new (EMPATHY_TYPE_TP_CALL,
581 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
583 EmpathyTpCallPriv *priv = GET_PRIV (call);
584 TpHandle self_handle;
585 GArray handles = {(gchar *) &self_handle, 1};
587 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
588 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_PENDING);
589 g_return_if_fail (priv->is_incoming);
591 DEBUG ("Accepting incoming call");
593 self_handle = tp_channel_group_get_self_handle (priv->channel);
594 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
595 &handles, NULL, NULL, NULL, NULL, NULL);
599 empathy_tp_call_close (EmpathyTpCall *call)
601 EmpathyTpCallPriv *priv = GET_PRIV (call);
603 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
605 if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
608 DEBUG ("Closing channel");
610 tp_cli_channel_call_close (priv->channel, -1,
611 NULL, NULL, NULL, NULL);
613 priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
614 g_object_notify (G_OBJECT (call), "status");
618 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
621 EmpathyTpCallPriv *priv = GET_PRIV (call);
624 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
625 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
627 DEBUG ("Requesting video stream direction - is_sending: %d", is_sending);
629 if (!priv->video->exists)
632 tp_call_request_streams_for_capabilities (call,
633 EMPATHY_CAPABILITIES_VIDEO);
638 new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
640 new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
642 tp_cli_channel_type_streamed_media_call_request_stream_direction (priv->channel,
643 -1, priv->video->id, new_direction,
644 (tp_cli_channel_type_streamed_media_callback_for_request_stream_direction)
645 tp_call_async_cb, NULL, NULL, G_OBJECT (call));
649 empathy_tp_call_add_preview_video (EmpathyTpCall *call,
650 guint preview_video_socket_id)
652 //EmpathyTpCallPriv *priv = GET_PRIV (call);
654 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
656 DEBUG ("Adding preview video");
658 /* FIXME add the preview window */
662 empathy_tp_call_remove_preview_video (EmpathyTpCall *call,
663 guint preview_video_socket_id)
665 //EmpathyTpCallPriv *priv = GET_PRIV (call);
667 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
669 DEBUG ("Removing preview video");
671 /* FIXME remove the preview window */
675 empathy_tp_call_add_output_video (EmpathyTpCall *call,
676 guint output_video_socket_id)
678 //EmpathyTpCallPriv *priv = GET_PRIV (call);
680 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
682 DEBUG ("Adding output video - socket: %d", output_video_socket_id);
684 /* FIXME add output window */
688 empathy_tp_call_set_output_volume (EmpathyTpCall *call,
691 EmpathyTpCallPriv *priv = GET_PRIV (call);
693 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
694 g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
696 DEBUG ("Setting output volume: %d", volume);
698 /* FIXME set volume */
702 empathy_tp_call_mute_output (EmpathyTpCall *call,
705 //EmpathyTpCallPriv *priv = GET_PRIV (call);
707 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
709 DEBUG ("Setting output mute: %d", is_muted);
710 /* FIXME mute output */
714 empathy_tp_call_mute_input (EmpathyTpCall *call,
717 //EmpathyTpCallPriv *priv = GET_PRIV (call);
719 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
721 DEBUG ("Setting input mute: %d", is_muted);
723 /* FIXME mute input */
727 empathy_tp_call_start_tone (EmpathyTpCall *call, TpDTMFEvent event)
729 EmpathyTpCallPriv *priv = GET_PRIV (call);
731 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
732 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
734 if (!priv->audio->exists)
737 tp_cli_channel_interface_dtmf_call_start_tone (priv->channel, -1,
738 priv->audio->id, event,
739 (tp_cli_channel_interface_dtmf_callback_for_start_tone) tp_call_async_cb,
740 "starting tone", NULL, G_OBJECT (call));
744 empathy_tp_call_stop_tone (EmpathyTpCall *call)
746 EmpathyTpCallPriv *priv = GET_PRIV (call);
748 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
749 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
751 if (!priv->audio->exists)
754 tp_cli_channel_interface_dtmf_call_stop_tone (priv->channel, -1,
756 (tp_cli_channel_interface_dtmf_callback_for_stop_tone) tp_call_async_cb,
757 "stoping tone", NULL, G_OBJECT (call));
761 empathy_tp_call_has_dtmf (EmpathyTpCall *call)
763 EmpathyTpCallPriv *priv = GET_PRIV (call);
765 g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
767 return tp_proxy_has_interface_by_id (priv->channel,
768 TP_IFACE_QUARK_CHANNEL_INTERFACE_DTMF);