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>
28 #include <telepathy-glib/util.h>
30 #include "empathy-tp-call.h"
31 #include "empathy-tp-contact-factory.h"
32 #include "empathy-utils.h"
33 #include "empathy-marshal.h"
35 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
36 #include "empathy-debug.h"
38 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpCall)
41 gboolean dispose_has_run;
43 EmpathyContact *contact;
47 EmpathyTpCallStream *audio;
48 EmpathyTpCallStream *video;
58 static guint signals[LAST_SIGNAL] = {0};
70 G_DEFINE_TYPE (EmpathyTpCall, empathy_tp_call, G_TYPE_OBJECT)
73 tp_call_add_stream (EmpathyTpCall *call,
78 guint stream_direction)
80 EmpathyTpCallPriv *priv = GET_PRIV (call);
84 case TP_MEDIA_STREAM_TYPE_AUDIO:
85 DEBUG ("Audio stream - id: %d, state: %d, direction: %d",
86 stream_id, stream_state, stream_direction);
87 priv->audio->exists = TRUE;
88 priv->audio->id = stream_id;
89 priv->audio->state = stream_state;
90 priv->audio->direction = stream_direction;
91 g_object_notify (G_OBJECT (call), "audio-stream");
93 case TP_MEDIA_STREAM_TYPE_VIDEO:
94 DEBUG ("Video stream - id: %d, state: %d, direction: %d",
95 stream_id, stream_state, stream_direction);
96 priv->video->exists = TRUE;
97 priv->video->id = stream_id;
98 priv->video->state = stream_state;
99 priv->video->direction = stream_direction;
100 g_object_notify (G_OBJECT (call), "video-stream");
103 DEBUG ("Unknown stream type: %d", stream_type);
108 tp_call_stream_added_cb (TpChannel *channel,
110 guint contact_handle,
115 DEBUG ("Stream added - stream id: %d, contact handle: %d, stream type: %d",
116 stream_id, contact_handle, stream_type);
118 tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
119 stream_type, TP_MEDIA_STREAM_STATE_DISCONNECTED,
120 TP_MEDIA_STREAM_DIRECTION_NONE);
124 tp_call_stream_removed_cb (TpChannel *channel,
129 EmpathyTpCallPriv *priv = GET_PRIV (call);
131 DEBUG ("Stream removed - stream id: %d", stream_id);
133 if (stream_id == priv->audio->id)
135 priv->audio->exists = FALSE;
136 g_object_notify (call, "audio-stream");
138 else if (stream_id == priv->video->id)
140 priv->video->exists = FALSE;
141 g_object_notify (call, "video-stream");
146 tp_call_stream_state_changed_cb (TpChannel *proxy,
152 EmpathyTpCallPriv *priv = GET_PRIV (call);
154 DEBUG ("Stream state changed - stream id: %d, state state: %d",
155 stream_id, stream_state);
157 if (stream_id == priv->audio->id)
159 priv->audio->state = stream_state;
160 g_object_notify (call, "audio-stream");
162 else if (stream_id == priv->video->id)
164 priv->video->state = stream_state;
165 g_object_notify (call, "video-stream");
170 tp_call_stream_direction_changed_cb (TpChannel *channel,
172 guint stream_direction,
177 EmpathyTpCallPriv *priv = GET_PRIV (call);
179 DEBUG ("Stream direction changed - stream: %d, direction: %d",
180 stream_id, stream_direction);
182 if (stream_id == priv->audio->id)
184 priv->audio->direction = stream_direction;
185 g_object_notify (call, "audio-stream");
187 else if (stream_id == priv->video->id)
189 priv->video->direction = stream_direction;
190 g_object_notify (call, "video-stream");
195 tp_call_request_streams_cb (TpChannel *channel,
196 const GPtrArray *streams,
205 DEBUG ("Error requesting streams: %s", error->message);
209 for (i = 0; i < streams->len; i++)
213 guint contact_handle;
216 guint stream_direction;
218 values = g_ptr_array_index (streams, i);
219 stream_id = g_value_get_uint (g_value_array_get_nth (values, 0));
220 contact_handle = g_value_get_uint (g_value_array_get_nth (values, 1));
221 stream_type = g_value_get_uint (g_value_array_get_nth (values, 2));
222 stream_state = g_value_get_uint (g_value_array_get_nth (values, 3));
223 stream_direction = g_value_get_uint (g_value_array_get_nth (values, 4));
225 tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
226 stream_type, stream_state, stream_direction);
231 tp_call_request_streams_for_capabilities (EmpathyTpCall *call,
232 EmpathyCapabilities capabilities)
234 EmpathyTpCallPriv *priv = GET_PRIV (call);
235 GArray *stream_types;
239 if (capabilities == EMPATHY_CAPABILITIES_UNKNOWN)
240 capabilities = EMPATHY_CAPABILITIES_AUDIO | EMPATHY_CAPABILITIES_VIDEO;
242 DEBUG ("Requesting new stream for capabilities %d",
245 stream_types = g_array_new (FALSE, FALSE, sizeof (guint));
246 handle = empathy_contact_get_handle (priv->contact);
248 if (capabilities & EMPATHY_CAPABILITIES_AUDIO)
250 stream_type = TP_MEDIA_STREAM_TYPE_AUDIO;
251 g_array_append_val (stream_types, stream_type);
253 if (capabilities & EMPATHY_CAPABILITIES_VIDEO)
255 stream_type = TP_MEDIA_STREAM_TYPE_VIDEO;
256 g_array_append_val (stream_types, stream_type);
259 tp_cli_channel_type_streamed_media_call_request_streams (priv->channel, -1,
260 handle, stream_types, tp_call_request_streams_cb, NULL, NULL,
263 g_array_free (stream_types, TRUE);
267 tp_call_got_contact_cb (TpConnection *connection,
268 EmpathyContact *contact,
273 EmpathyTpCallPriv *priv = GET_PRIV (call);
277 DEBUG ("Error: %s", error->message);
281 priv->contact = g_object_ref (contact);
283 if (priv->status < EMPATHY_TP_CALL_STATUS_PENDING)
285 priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
286 g_object_notify (G_OBJECT (call), "status");
289 g_object_notify (G_OBJECT (call), "contact");
293 tp_call_update_status (EmpathyTpCall *call)
295 EmpathyTpCallPriv *priv = GET_PRIV (call);
296 TpHandle self_handle;
302 self_handle = tp_channel_group_get_self_handle (priv->channel);
303 set = tp_channel_group_get_members (priv->channel);
304 tp_intset_iter_init (&iter, set);
305 while (tp_intset_iter_next (&iter))
307 if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING &&
308 ((priv->is_incoming && iter.element == self_handle) ||
309 (!priv->is_incoming && iter.element != self_handle)))
311 priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
312 g_object_notify (G_OBJECT (call), "status");
316 g_object_unref (call);
320 tp_call_channel_invalidated_cb (TpChannel *channel,
326 EmpathyTpCallPriv *priv = GET_PRIV (call);
328 DEBUG ("Channel invalidated: %s", message);
329 priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
330 g_object_notify (G_OBJECT (call), "status");
334 tp_call_async_cb (TpProxy *proxy,
340 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
344 tp_call_stream_error_cb (TpChannel *channel,
351 EmpathyTpCall *self = EMPATHY_TP_CALL (call);
352 EmpathyTpCallPriv *priv = GET_PRIV (self);
354 DEBUG ("Stream error on stream %u: %s (code: %u)", stream_id, msg,
357 if (priv->audio->id == stream_id)
359 g_signal_emit (call, signals[AUDIO_STREAM_ERROR], 0, error_code, msg);
361 else if (priv->video->id == stream_id)
363 g_signal_emit (call, signals[VIDEO_STREAM_ERROR], 0, error_code, msg);
367 DEBUG ("Unknown stream id: %u", stream_id);
372 tp_call_constructor (GType type,
373 guint n_construct_params,
374 GObjectConstructParam *construct_params)
378 EmpathyTpCallPriv *priv;
382 object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
383 n_construct_params, construct_params);
385 call = EMPATHY_TP_CALL (object);
386 priv = GET_PRIV (call);
388 /* Setup streamed media channel */
389 g_signal_connect (priv->channel, "invalidated",
390 G_CALLBACK (tp_call_channel_invalidated_cb), call);
391 tp_cli_channel_type_streamed_media_connect_to_stream_added (priv->channel,
392 tp_call_stream_added_cb, NULL, NULL, G_OBJECT (call), NULL);
393 tp_cli_channel_type_streamed_media_connect_to_stream_removed (priv->channel,
394 tp_call_stream_removed_cb, NULL, NULL, G_OBJECT (call), NULL);
395 tp_cli_channel_type_streamed_media_connect_to_stream_state_changed (priv->channel,
396 tp_call_stream_state_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
397 tp_cli_channel_type_streamed_media_connect_to_stream_direction_changed (priv->channel,
398 tp_call_stream_direction_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
399 tp_cli_channel_type_streamed_media_connect_to_stream_error (priv->channel,
400 tp_call_stream_error_cb, NULL, NULL, G_OBJECT (call), NULL);
401 tp_cli_channel_type_streamed_media_call_list_streams (priv->channel, -1,
402 tp_call_request_streams_cb, NULL, NULL, G_OBJECT (call));
404 /* Is the call incoming? */
405 props = tp_channel_borrow_immutable_properties (priv->channel);
406 requested = tp_asv_get_boolean (props, TP_PROP_CHANNEL_REQUESTED, NULL);
408 priv->is_incoming = !requested;
410 /* Get the remote contact */
411 empathy_tp_contact_factory_get_from_handle (
412 tp_channel_borrow_connection (priv->channel),
413 tp_channel_get_handle (priv->channel, NULL), tp_call_got_contact_cb,
416 /* Update status when members changes */
417 tp_call_update_status (call);
418 tp_g_signal_connect_object (priv->channel, "group-members-changed",
419 G_CALLBACK (tp_call_update_status), call, G_CONNECT_SWAPPED);
424 tp_call_dispose (GObject *object)
426 EmpathyTpCallPriv *priv = GET_PRIV (object);
428 DEBUG ("Disposing: %p, %d", object, priv->dispose_has_run);
430 if (priv->dispose_has_run)
433 priv->dispose_has_run = TRUE;
435 if (priv->channel != NULL)
437 g_signal_handlers_disconnect_by_func (priv->channel,
438 tp_call_channel_invalidated_cb, object);
440 g_object_unref (priv->channel);
441 priv->channel = NULL;
444 if (priv->contact != NULL)
445 g_object_unref (priv->contact);
447 if (G_OBJECT_CLASS (empathy_tp_call_parent_class)->dispose)
448 G_OBJECT_CLASS (empathy_tp_call_parent_class)->dispose (object);
452 tp_call_finalize (GObject *object)
454 EmpathyTpCallPriv *priv = GET_PRIV (object);
456 DEBUG ("Finalizing: %p", object);
458 g_slice_free (EmpathyTpCallStream, priv->audio);
459 g_slice_free (EmpathyTpCallStream, priv->video);
461 (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
465 tp_call_set_property (GObject *object,
470 EmpathyTpCallPriv *priv = GET_PRIV (object);
475 priv->channel = g_value_dup_object (value);
478 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
484 tp_call_get_property (GObject *object,
489 EmpathyTpCallPriv *priv = GET_PRIV (object);
494 g_value_set_object (value, priv->channel);
497 g_value_set_object (value, priv->contact);
500 g_value_set_uint (value, priv->status);
502 case PROP_AUDIO_STREAM:
503 g_value_set_pointer (value, priv->audio);
505 case PROP_VIDEO_STREAM:
506 g_value_set_pointer (value, priv->video);
509 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
515 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
517 GObjectClass *object_class = G_OBJECT_CLASS (klass);
519 object_class->constructor = tp_call_constructor;
520 object_class->dispose = tp_call_dispose;
521 object_class->finalize = tp_call_finalize;
522 object_class->set_property = tp_call_set_property;
523 object_class->get_property = tp_call_get_property;
525 g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
527 g_object_class_install_property (object_class, PROP_CHANNEL,
528 g_param_spec_object ("channel", "channel", "channel",
530 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
531 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
532 g_object_class_install_property (object_class, PROP_CONTACT,
533 g_param_spec_object ("contact", "Call contact", "Call contact",
534 EMPATHY_TYPE_CONTACT,
535 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
536 g_object_class_install_property (object_class, PROP_STATUS,
537 g_param_spec_uint ("status", "Call status",
538 "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
539 G_PARAM_STATIC_BLURB));
540 g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
541 g_param_spec_pointer ("audio-stream", "Audio stream data",
543 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
544 g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
545 g_param_spec_pointer ("video-stream", "Video stream data",
547 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
549 signals[AUDIO_STREAM_ERROR] =
550 g_signal_new ("audio-stream-error",
551 G_TYPE_FROM_CLASS (klass),
552 G_SIGNAL_RUN_LAST, 0,
554 _empathy_marshal_VOID__UINT_STRING,
556 2, G_TYPE_UINT, G_TYPE_STRING);
558 signals[VIDEO_STREAM_ERROR] =
559 g_signal_new ("video-stream-error",
560 G_TYPE_FROM_CLASS (klass),
561 G_SIGNAL_RUN_LAST, 0,
563 _empathy_marshal_VOID__UINT_STRING,
565 2, G_TYPE_UINT, G_TYPE_STRING);
569 empathy_tp_call_init (EmpathyTpCall *call)
571 EmpathyTpCallPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (call,
572 EMPATHY_TYPE_TP_CALL, EmpathyTpCallPriv);
575 priv->status = EMPATHY_TP_CALL_STATUS_READYING;
576 priv->contact = NULL;
577 priv->audio = g_slice_new0 (EmpathyTpCallStream);
578 priv->video = g_slice_new0 (EmpathyTpCallStream);
579 priv->audio->exists = FALSE;
580 priv->video->exists = FALSE;
584 empathy_tp_call_new (TpChannel *channel)
586 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
588 return g_object_new (EMPATHY_TYPE_TP_CALL,
594 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
596 EmpathyTpCallPriv *priv = GET_PRIV (call);
597 TpHandle self_handle;
598 GArray handles = {(gchar *) &self_handle, 1};
600 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
601 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_PENDING);
603 if (!priv->is_incoming)
606 DEBUG ("Accepting incoming call");
608 self_handle = tp_channel_group_get_self_handle (priv->channel);
609 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
610 &handles, NULL, NULL, NULL, NULL, NULL);
614 empathy_tp_call_close (EmpathyTpCall *call)
616 EmpathyTpCallPriv *priv = GET_PRIV (call);
618 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
620 if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
623 DEBUG ("Closing channel");
625 tp_cli_channel_call_close (priv->channel, -1,
626 NULL, NULL, NULL, NULL);
628 priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
629 g_object_notify (G_OBJECT (call), "status");
633 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
636 EmpathyTpCallPriv *priv = GET_PRIV (call);
639 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
640 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
642 DEBUG ("Requesting video stream direction - is_sending: %d", is_sending);
644 if (!priv->video->exists)
647 tp_call_request_streams_for_capabilities (call,
648 EMPATHY_CAPABILITIES_VIDEO);
653 new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
655 new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
657 tp_cli_channel_type_streamed_media_call_request_stream_direction (priv->channel,
658 -1, priv->video->id, new_direction,
659 (tp_cli_channel_type_streamed_media_callback_for_request_stream_direction)
660 tp_call_async_cb, NULL, NULL, G_OBJECT (call));
664 empathy_tp_call_start_tone (EmpathyTpCall *call, TpDTMFEvent event)
666 EmpathyTpCallPriv *priv = GET_PRIV (call);
668 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
669 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
671 if (!priv->audio->exists)
674 tp_cli_channel_interface_dtmf_call_start_tone (priv->channel, -1,
675 priv->audio->id, event,
676 (tp_cli_channel_interface_dtmf_callback_for_start_tone) tp_call_async_cb,
677 "starting tone", NULL, G_OBJECT (call));
681 empathy_tp_call_stop_tone (EmpathyTpCall *call)
683 EmpathyTpCallPriv *priv = GET_PRIV (call);
685 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
686 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
688 if (!priv->audio->exists)
691 tp_cli_channel_interface_dtmf_call_stop_tone (priv->channel, -1,
693 (tp_cli_channel_interface_dtmf_callback_for_stop_tone) tp_call_async_cb,
694 "stoping tone", NULL, G_OBJECT (call));
698 empathy_tp_call_has_dtmf (EmpathyTpCall *call)
700 EmpathyTpCallPriv *priv = GET_PRIV (call);
702 g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
704 return tp_proxy_has_interface_by_id (priv->channel,
705 TP_IFACE_QUARK_CHANNEL_INTERFACE_DTMF);
709 * empathy_tp_call_is_receiving_video:
712 * Indicates if the call is receiving video or not.
714 * Returns: %TRUE if the call is currently receiving video, %FALSE otherwise.
717 empathy_tp_call_is_receiving_video (EmpathyTpCall *call)
719 EmpathyTpCallPriv *priv = GET_PRIV (call);
721 g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
723 if (!priv->video->exists)
726 return priv->video->direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE ?
731 * empathy_tp_call_is_sending_video:
734 * Indicates if the call is sending video or not.
736 * Returns: %TRUE if the call is currently sending video, %FALSE otherwise.
739 empathy_tp_call_is_sending_video (EmpathyTpCall *call)
741 EmpathyTpCallPriv *priv = GET_PRIV (call);
743 g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
745 if (!priv->video->exists)
748 return priv->video->direction & TP_MEDIA_STREAM_DIRECTION_SEND ?
753 empathy_tp_call_get_connection_manager (EmpathyTpCall *self)
755 EmpathyTpCallPriv *priv = GET_PRIV (self);
759 if (priv->channel == NULL)
762 conn = tp_channel_borrow_connection (priv->channel);
766 account = empathy_get_account_for_connection (conn);
770 return tp_account_get_connection_manager (account);
774 empathy_tp_call_has_initial_video (EmpathyTpCall *self)
776 EmpathyTpCallPriv *priv = GET_PRIV (self);
778 gboolean initial_video;
781 if (priv->channel == NULL)
784 g_object_get (priv->channel, "channel-properties", &props, NULL);
786 initial_video = tp_asv_get_boolean (props,
787 TP_IFACE_CHANNEL_TYPE_STREAMED_MEDIA ".InitialVideo", &valid);
789 initial_video = FALSE;
791 g_hash_table_unref (props);
792 return initial_video;
796 leave_remove_members_cb (TpChannel *proxy,
799 GObject *weak_object)
801 EmpathyTpCall *self = user_data;
806 DEBUG ("RemoveMembers failed (%s); closing the channel", error->message);
807 empathy_tp_call_close (self);
811 empathy_tp_call_leave (EmpathyTpCall *self)
813 EmpathyTpCallPriv *priv = GET_PRIV (self);
814 TpHandle self_handle;
815 GArray array = { (gchar *) &self_handle, 1 };
817 if (!tp_proxy_has_interface_by_id (priv->channel,
818 TP_IFACE_QUARK_CHANNEL_INTERFACE_GROUP))
820 empathy_tp_call_close (self);
824 self_handle = tp_channel_group_get_self_handle (priv->channel);
825 if (self_handle == 0)
827 /* we are not member of the channel */
828 empathy_tp_call_close (self);
832 tp_cli_channel_interface_group_call_remove_members (priv->channel, -1, &array,
833 "", leave_remove_members_cb, self, NULL, G_OBJECT (self));
837 empathy_tp_call_get_status (EmpathyTpCall *self)
839 EmpathyTpCallPriv *priv = GET_PRIV (self);