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-tp-contact-factory.h"
31 #include "empathy-utils.h"
32 #include "empathy-marshal.h"
34 #define DEBUG_FLAG EMPATHY_DEBUG_TP
35 #include "empathy-debug.h"
37 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpCall)
40 gboolean dispose_has_run;
42 EmpathyContact *contact;
46 EmpathyTpCallStream *audio;
47 EmpathyTpCallStream *video;
57 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 (EmpathyTpContactFactory *factory,
268 EmpathyContact *contact,
273 EmpathyTpCallPriv *priv = GET_PRIV (call);
277 DEBUG ("Error: %s", error->message);
281 priv->contact = g_object_ref (contact);
282 priv->is_incoming = TRUE;
283 priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
284 g_object_notify (G_OBJECT (call), "is-incoming");
285 g_object_notify (G_OBJECT (call), "contact");
286 g_object_notify (G_OBJECT (call), "status");
290 tp_call_update_status (EmpathyTpCall *call)
292 EmpathyTpCallPriv *priv = GET_PRIV (call);
293 TpHandle self_handle;
299 self_handle = tp_channel_group_get_self_handle (priv->channel);
300 set = tp_channel_group_get_members (priv->channel);
301 tp_intset_iter_init (&iter, set);
302 while (tp_intset_iter_next (&iter))
304 if (priv->contact == NULL && iter.element != self_handle)
306 EmpathyTpContactFactory *factory;
307 TpConnection *connection;
309 /* We found the remote contact */
310 connection = tp_channel_borrow_connection (priv->channel);
311 factory = empathy_tp_contact_factory_dup_singleton (connection);
312 empathy_tp_contact_factory_get_from_handle (factory, iter.element,
313 tp_call_got_contact_cb, NULL, NULL, G_OBJECT (call));
314 g_object_unref (factory);
317 if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING &&
318 ((priv->is_incoming && iter.element == self_handle) ||
319 (!priv->is_incoming && iter.element != self_handle)))
321 priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
322 g_object_notify (G_OBJECT (call), "status");
326 g_object_unref (call);
330 empathy_tp_call_to (EmpathyTpCall *call, EmpathyContact *contact,
331 gboolean audio, gboolean video)
333 EmpathyTpCallPriv *priv = GET_PRIV (call);
334 EmpathyCapabilities capabilities = 0;
336 g_assert (audio || video);
338 priv->contact = g_object_ref (contact);
339 priv->is_incoming = FALSE;
340 priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
341 g_object_notify (G_OBJECT (call), "is-incoming");
342 g_object_notify (G_OBJECT (call), "contact");
343 g_object_notify (G_OBJECT (call), "status");
346 capabilities |= EMPATHY_CAPABILITIES_VIDEO;
348 capabilities |= EMPATHY_CAPABILITIES_AUDIO;
350 tp_call_request_streams_for_capabilities (call, capabilities);
354 tp_call_channel_invalidated_cb (TpChannel *channel,
360 EmpathyTpCallPriv *priv = GET_PRIV (call);
362 DEBUG ("Channel invalidated: %s", message);
363 priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
364 g_object_notify (G_OBJECT (call), "status");
368 tp_call_async_cb (TpProxy *proxy,
374 DEBUG ("Error %s: %s", (gchar *) user_data, error->message);
378 tp_call_stream_error_cb (TpChannel *channel,
385 EmpathyTpCall *self = EMPATHY_TP_CALL (call);
386 EmpathyTpCallPriv *priv = GET_PRIV (self);
388 DEBUG ("Stream error on stream %u: %s (code: %u)", stream_id, msg,
391 if (priv->audio->id == stream_id)
393 g_signal_emit (call, signals[AUDIO_STREAM_ERROR], 0, error_code, msg);
395 else if (priv->video->id == stream_id)
397 g_signal_emit (call, signals[VIDEO_STREAM_ERROR], 0, error_code, msg);
401 DEBUG ("Unknown stream id: %u", stream_id);
406 tp_call_constructor (GType type,
407 guint n_construct_params,
408 GObjectConstructParam *construct_params)
412 EmpathyTpCallPriv *priv;
414 object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
415 n_construct_params, construct_params);
417 call = EMPATHY_TP_CALL (object);
418 priv = GET_PRIV (call);
420 /* Setup streamed media channel */
421 g_signal_connect (priv->channel, "invalidated",
422 G_CALLBACK (tp_call_channel_invalidated_cb), call);
423 tp_cli_channel_type_streamed_media_connect_to_stream_added (priv->channel,
424 tp_call_stream_added_cb, NULL, NULL, G_OBJECT (call), NULL);
425 tp_cli_channel_type_streamed_media_connect_to_stream_removed (priv->channel,
426 tp_call_stream_removed_cb, NULL, NULL, G_OBJECT (call), NULL);
427 tp_cli_channel_type_streamed_media_connect_to_stream_state_changed (priv->channel,
428 tp_call_stream_state_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
429 tp_cli_channel_type_streamed_media_connect_to_stream_direction_changed (priv->channel,
430 tp_call_stream_direction_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
431 tp_cli_channel_type_streamed_media_connect_to_stream_error (priv->channel,
432 tp_call_stream_error_cb, NULL, NULL, G_OBJECT (call), NULL);
433 tp_cli_channel_type_streamed_media_call_list_streams (priv->channel, -1,
434 tp_call_request_streams_cb, NULL, NULL, G_OBJECT (call));
436 /* Update status when members changes */
437 tp_call_update_status (call);
438 g_signal_connect_swapped (priv->channel, "group-members-changed",
439 G_CALLBACK (tp_call_update_status), call);
444 tp_call_dispose (GObject *object)
446 EmpathyTpCallPriv *priv = GET_PRIV (object);
448 DEBUG ("Disposing: %p, %d", object, priv->dispose_has_run);
450 if (priv->dispose_has_run)
453 priv->dispose_has_run = TRUE;
455 if (priv->channel != NULL)
457 g_signal_handlers_disconnect_by_func (priv->channel,
458 tp_call_channel_invalidated_cb, object);
460 g_object_unref (priv->channel);
461 priv->channel = NULL;
464 if (priv->contact != NULL)
465 g_object_unref (priv->contact);
467 if (G_OBJECT_CLASS (empathy_tp_call_parent_class)->dispose)
468 G_OBJECT_CLASS (empathy_tp_call_parent_class)->dispose (object);
472 tp_call_finalize (GObject *object)
474 EmpathyTpCallPriv *priv = GET_PRIV (object);
476 DEBUG ("Finalizing: %p", object);
478 g_slice_free (EmpathyTpCallStream, priv->audio);
479 g_slice_free (EmpathyTpCallStream, priv->video);
481 (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
485 tp_call_set_property (GObject *object,
490 EmpathyTpCallPriv *priv = GET_PRIV (object);
495 priv->channel = g_value_dup_object (value);
498 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
504 tp_call_get_property (GObject *object,
509 EmpathyTpCallPriv *priv = GET_PRIV (object);
514 g_value_set_object (value, priv->channel);
517 g_value_set_object (value, priv->contact);
519 case PROP_IS_INCOMING:
520 g_value_set_boolean (value, priv->is_incoming);
523 g_value_set_uint (value, priv->status);
525 case PROP_AUDIO_STREAM:
526 g_value_set_pointer (value, priv->audio);
528 case PROP_VIDEO_STREAM:
529 g_value_set_pointer (value, priv->video);
532 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
538 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
540 GObjectClass *object_class = G_OBJECT_CLASS (klass);
542 object_class->constructor = tp_call_constructor;
543 object_class->dispose = tp_call_dispose;
544 object_class->finalize = tp_call_finalize;
545 object_class->set_property = tp_call_set_property;
546 object_class->get_property = tp_call_get_property;
548 g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
550 g_object_class_install_property (object_class, PROP_CHANNEL,
551 g_param_spec_object ("channel", "channel", "channel",
553 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
554 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
555 g_object_class_install_property (object_class, PROP_CONTACT,
556 g_param_spec_object ("contact", "Call contact", "Call contact",
557 EMPATHY_TYPE_CONTACT,
558 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
559 g_object_class_install_property (object_class, PROP_IS_INCOMING,
560 g_param_spec_boolean ("is-incoming", "Is media stream incoming",
561 "Is media stream incoming", FALSE, G_PARAM_READABLE |
562 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
563 g_object_class_install_property (object_class, PROP_STATUS,
564 g_param_spec_uint ("status", "Call status",
565 "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
566 G_PARAM_STATIC_BLURB));
567 g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
568 g_param_spec_pointer ("audio-stream", "Audio stream data",
570 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
571 g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
572 g_param_spec_pointer ("video-stream", "Video stream data",
574 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
576 signals[AUDIO_STREAM_ERROR] =
577 g_signal_new ("audio-stream-error",
578 G_TYPE_FROM_CLASS (klass),
579 G_SIGNAL_RUN_LAST, 0,
581 _empathy_marshal_VOID__UINT_STRING,
583 2, G_TYPE_UINT, G_TYPE_STRING);
585 signals[VIDEO_STREAM_ERROR] =
586 g_signal_new ("video-stream-error",
587 G_TYPE_FROM_CLASS (klass),
588 G_SIGNAL_RUN_LAST, 0,
590 _empathy_marshal_VOID__UINT_STRING,
592 2, G_TYPE_UINT, G_TYPE_STRING);
596 empathy_tp_call_init (EmpathyTpCall *call)
598 EmpathyTpCallPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (call,
599 EMPATHY_TYPE_TP_CALL, EmpathyTpCallPriv);
602 priv->status = EMPATHY_TP_CALL_STATUS_READYING;
603 priv->contact = NULL;
604 priv->audio = g_slice_new0 (EmpathyTpCallStream);
605 priv->video = g_slice_new0 (EmpathyTpCallStream);
606 priv->audio->exists = FALSE;
607 priv->video->exists = FALSE;
611 empathy_tp_call_new (TpChannel *channel)
613 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
615 return g_object_new (EMPATHY_TYPE_TP_CALL,
621 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
623 EmpathyTpCallPriv *priv = GET_PRIV (call);
624 TpHandle self_handle;
625 GArray handles = {(gchar *) &self_handle, 1};
627 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
628 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_PENDING);
629 g_return_if_fail (priv->is_incoming);
631 DEBUG ("Accepting incoming call");
633 self_handle = tp_channel_group_get_self_handle (priv->channel);
634 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
635 &handles, NULL, NULL, NULL, NULL, NULL);
639 empathy_tp_call_close (EmpathyTpCall *call)
641 EmpathyTpCallPriv *priv = GET_PRIV (call);
643 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
645 if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
648 DEBUG ("Closing channel");
650 tp_cli_channel_call_close (priv->channel, -1,
651 NULL, NULL, NULL, NULL);
653 priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
654 g_object_notify (G_OBJECT (call), "status");
658 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
661 EmpathyTpCallPriv *priv = GET_PRIV (call);
664 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
665 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
667 DEBUG ("Requesting video stream direction - is_sending: %d", is_sending);
669 if (!priv->video->exists)
672 tp_call_request_streams_for_capabilities (call,
673 EMPATHY_CAPABILITIES_VIDEO);
678 new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
680 new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
682 tp_cli_channel_type_streamed_media_call_request_stream_direction (priv->channel,
683 -1, priv->video->id, new_direction,
684 (tp_cli_channel_type_streamed_media_callback_for_request_stream_direction)
685 tp_call_async_cb, NULL, NULL, G_OBJECT (call));
689 empathy_tp_call_start_tone (EmpathyTpCall *call, TpDTMFEvent event)
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_ACCEPTED);
696 if (!priv->audio->exists)
699 tp_cli_channel_interface_dtmf_call_start_tone (priv->channel, -1,
700 priv->audio->id, event,
701 (tp_cli_channel_interface_dtmf_callback_for_start_tone) tp_call_async_cb,
702 "starting tone", NULL, G_OBJECT (call));
706 empathy_tp_call_stop_tone (EmpathyTpCall *call)
708 EmpathyTpCallPriv *priv = GET_PRIV (call);
710 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
711 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
713 if (!priv->audio->exists)
716 tp_cli_channel_interface_dtmf_call_stop_tone (priv->channel, -1,
718 (tp_cli_channel_interface_dtmf_callback_for_stop_tone) tp_call_async_cb,
719 "stoping tone", NULL, G_OBJECT (call));
723 empathy_tp_call_has_dtmf (EmpathyTpCall *call)
725 EmpathyTpCallPriv *priv = GET_PRIV (call);
727 g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
729 return tp_proxy_has_interface_by_id (priv->channel,
730 TP_IFACE_QUARK_CHANNEL_INTERFACE_DTMF);
734 * empathy_tp_call_is_receiving_video:
737 * Indicates if the call is receiving video or not.
739 * Returns: %TRUE if the call is currently receiving video, %FALSE otherwise.
742 empathy_tp_call_is_receiving_video (EmpathyTpCall *call)
744 EmpathyTpCallPriv *priv = GET_PRIV (call);
746 g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
748 if (!priv->video->exists)
751 return priv->video->direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE ?
756 * empathy_tp_call_is_sending_video:
759 * Indicates if the call is sending video or not.
761 * Returns: %TRUE if the call is currently sending video, %FALSE otherwise.
764 empathy_tp_call_is_sending_video (EmpathyTpCall *call)
766 EmpathyTpCallPriv *priv = GET_PRIV (call);
768 g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
770 if (!priv->video->exists)
773 return priv->video->direction & TP_MEDIA_STREAM_DIRECTION_SEND ?