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"
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);
257 tp_call_got_contact_cb (EmpathyTpContactFactory *factory,
262 EmpathyTpCallPriv *priv = GET_PRIV (call);
264 priv->contact = g_object_ref (contacts->data);
265 priv->is_incoming = TRUE;
266 priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
267 g_object_notify (G_OBJECT (call), "is-incoming");
268 g_object_notify (G_OBJECT (call), "contact");
269 g_object_notify (G_OBJECT (call), "status");
273 tp_call_update_status (EmpathyTpCall *call)
275 EmpathyTpCallPriv *priv = GET_PRIV (call);
276 TpHandle self_handle;
282 self_handle = tp_channel_group_get_self_handle (priv->channel);
283 set = tp_channel_group_get_members (priv->channel);
284 tp_intset_iter_init (&iter, set);
285 while (tp_intset_iter_next (&iter))
287 if (priv->contact == NULL && iter.element != self_handle)
289 EmpathyTpContactFactory *factory;
290 TpConnection *connection;
292 /* We found the remote contact */
293 connection = tp_channel_borrow_connection (priv->channel);
294 factory = empathy_tp_contact_factory_dup_singleton (connection);
295 empathy_tp_contact_factory_get_from_handles (factory, 1,
296 &iter.element, tp_call_got_contact_cb, NULL, NULL,
298 g_object_unref (factory);
301 if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING &&
302 ((priv->is_incoming && iter.element == self_handle) ||
303 (!priv->is_incoming && iter.element != self_handle)))
305 priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
306 g_object_notify (G_OBJECT (call), "status");
310 g_object_unref (call);
314 empathy_tp_call_to (EmpathyTpCall *call, EmpathyContact *contact)
316 EmpathyTpCallPriv *priv = GET_PRIV (call);
318 priv->contact = g_object_ref (contact);
319 priv->is_incoming = FALSE;
320 priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
321 g_object_notify (G_OBJECT (call), "is-incoming");
322 g_object_notify (G_OBJECT (call), "contact");
323 g_object_notify (G_OBJECT (call), "status");
324 tp_call_request_streams_for_capabilities (call, EMPATHY_CAPABILITIES_AUDIO);
328 tp_call_channel_invalidated_cb (TpChannel *channel,
334 EmpathyTpCallPriv *priv = GET_PRIV (call);
336 DEBUG ("Channel invalidated: %s", message);
337 priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
338 g_object_notify (G_OBJECT (call), "status");
342 tp_call_async_cb (TpProxy *proxy,
348 DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
352 tp_call_constructor (GType type,
353 guint n_construct_params,
354 GObjectConstructParam *construct_params)
358 EmpathyTpCallPriv *priv;
360 object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
361 n_construct_params, construct_params);
363 call = EMPATHY_TP_CALL (object);
364 priv = GET_PRIV (call);
366 /* Setup streamed media channel */
367 g_signal_connect (priv->channel, "invalidated",
368 G_CALLBACK (tp_call_channel_invalidated_cb), call);
369 tp_cli_channel_type_streamed_media_connect_to_stream_added (priv->channel,
370 tp_call_stream_added_cb, NULL, NULL, G_OBJECT (call), NULL);
371 tp_cli_channel_type_streamed_media_connect_to_stream_removed (priv->channel,
372 tp_call_stream_removed_cb, NULL, NULL, G_OBJECT (call), NULL);
373 tp_cli_channel_type_streamed_media_connect_to_stream_state_changed (priv->channel,
374 tp_call_stream_state_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
375 tp_cli_channel_type_streamed_media_connect_to_stream_direction_changed (priv->channel,
376 tp_call_stream_direction_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
377 tp_cli_channel_type_streamed_media_call_list_streams (priv->channel, -1,
378 tp_call_request_streams_cb, NULL, NULL, G_OBJECT (call));
380 /* Update status when members changes */
381 tp_call_update_status (call);
382 g_signal_connect_swapped (priv->channel, "group-members-changed",
383 G_CALLBACK (tp_call_update_status), call);
388 tp_call_dispose (GObject *object)
390 EmpathyTpCallPriv *priv = GET_PRIV (object);
392 DEBUG ("Disposing: %p, %d", object, priv->dispose_has_run);
394 if (priv->dispose_has_run)
397 priv->dispose_has_run = TRUE;
399 if (priv->channel != NULL)
401 g_signal_handlers_disconnect_by_func (priv->channel,
402 tp_call_channel_invalidated_cb, object);
404 g_object_unref (priv->channel);
405 priv->channel = NULL;
408 if (priv->contact != NULL)
409 g_object_unref (priv->contact);
411 if (G_OBJECT_CLASS (empathy_tp_call_parent_class)->dispose)
412 G_OBJECT_CLASS (empathy_tp_call_parent_class)->dispose (object);
416 tp_call_finalize (GObject *object)
418 EmpathyTpCallPriv *priv = GET_PRIV (object);
420 DEBUG ("Finalizing: %p", object);
422 g_slice_free (EmpathyTpCallStream, priv->audio);
423 g_slice_free (EmpathyTpCallStream, priv->video);
425 (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
429 tp_call_set_property (GObject *object,
434 EmpathyTpCallPriv *priv = GET_PRIV (object);
439 priv->channel = g_value_dup_object (value);
442 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
448 tp_call_get_property (GObject *object,
453 EmpathyTpCallPriv *priv = GET_PRIV (object);
458 g_value_set_object (value, priv->channel);
461 g_value_set_object (value, priv->contact);
463 case PROP_IS_INCOMING:
464 g_value_set_boolean (value, priv->is_incoming);
467 g_value_set_uint (value, priv->status);
469 case PROP_AUDIO_STREAM:
470 g_value_set_pointer (value, priv->audio);
472 case PROP_VIDEO_STREAM:
473 g_value_set_pointer (value, priv->video);
476 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
482 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
484 GObjectClass *object_class = G_OBJECT_CLASS (klass);
486 object_class->constructor = tp_call_constructor;
487 object_class->dispose = tp_call_dispose;
488 object_class->finalize = tp_call_finalize;
489 object_class->set_property = tp_call_set_property;
490 object_class->get_property = tp_call_get_property;
492 g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
494 g_object_class_install_property (object_class, PROP_CHANNEL,
495 g_param_spec_object ("channel", "channel", "channel",
497 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
498 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
499 g_object_class_install_property (object_class, PROP_CONTACT,
500 g_param_spec_object ("contact", "Call contact", "Call contact",
501 EMPATHY_TYPE_CONTACT,
502 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
503 g_object_class_install_property (object_class, PROP_IS_INCOMING,
504 g_param_spec_boolean ("is-incoming", "Is media stream incoming",
505 "Is media stream incoming", FALSE, G_PARAM_READABLE |
506 G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
507 g_object_class_install_property (object_class, PROP_STATUS,
508 g_param_spec_uint ("status", "Call status",
509 "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
510 G_PARAM_STATIC_BLURB));
511 g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
512 g_param_spec_pointer ("audio-stream", "Audio stream data",
514 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
515 g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
516 g_param_spec_pointer ("video-stream", "Video stream data",
518 G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
522 empathy_tp_call_init (EmpathyTpCall *call)
524 EmpathyTpCallPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (call,
525 EMPATHY_TYPE_TP_CALL, EmpathyTpCallPriv);
528 priv->status = EMPATHY_TP_CALL_STATUS_READYING;
529 priv->contact = NULL;
530 priv->audio = g_slice_new0 (EmpathyTpCallStream);
531 priv->video = g_slice_new0 (EmpathyTpCallStream);
532 priv->audio->exists = FALSE;
533 priv->video->exists = FALSE;
537 empathy_tp_call_new (TpChannel *channel)
539 g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
541 return g_object_new (EMPATHY_TYPE_TP_CALL,
547 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
549 EmpathyTpCallPriv *priv = GET_PRIV (call);
550 TpHandle self_handle;
551 GArray handles = {(gchar *) &self_handle, 1};
553 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
554 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_PENDING);
555 g_return_if_fail (priv->is_incoming);
557 DEBUG ("Accepting incoming call");
559 self_handle = tp_channel_group_get_self_handle (priv->channel);
560 tp_cli_channel_interface_group_call_add_members (priv->channel, -1,
561 &handles, NULL, NULL, NULL, NULL, NULL);
565 empathy_tp_call_close (EmpathyTpCall *call)
567 EmpathyTpCallPriv *priv = GET_PRIV (call);
569 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
571 if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
574 DEBUG ("Closing channel");
576 tp_cli_channel_call_close (priv->channel, -1,
577 NULL, NULL, NULL, NULL);
579 priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
580 g_object_notify (G_OBJECT (call), "status");
584 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
587 EmpathyTpCallPriv *priv = GET_PRIV (call);
590 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
591 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
593 DEBUG ("Requesting video stream direction - is_sending: %d", is_sending);
595 if (!priv->video->exists)
598 tp_call_request_streams_for_capabilities (call,
599 EMPATHY_CAPABILITIES_VIDEO);
604 new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
606 new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
608 tp_cli_channel_type_streamed_media_call_request_stream_direction (priv->channel,
609 -1, priv->video->id, new_direction,
610 (tp_cli_channel_type_streamed_media_callback_for_request_stream_direction)
611 tp_call_async_cb, NULL, NULL, G_OBJECT (call));
615 empathy_tp_call_start_tone (EmpathyTpCall *call, TpDTMFEvent event)
617 EmpathyTpCallPriv *priv = GET_PRIV (call);
619 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
620 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
622 if (!priv->audio->exists)
625 tp_cli_channel_interface_dtmf_call_start_tone (priv->channel, -1,
626 priv->audio->id, event,
627 (tp_cli_channel_interface_dtmf_callback_for_start_tone) tp_call_async_cb,
628 "starting tone", NULL, G_OBJECT (call));
632 empathy_tp_call_stop_tone (EmpathyTpCall *call)
634 EmpathyTpCallPriv *priv = GET_PRIV (call);
636 g_return_if_fail (EMPATHY_IS_TP_CALL (call));
637 g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
639 if (!priv->audio->exists)
642 tp_cli_channel_interface_dtmf_call_stop_tone (priv->channel, -1,
644 (tp_cli_channel_interface_dtmf_callback_for_stop_tone) tp_call_async_cb,
645 "stoping tone", NULL, G_OBJECT (call));
649 empathy_tp_call_has_dtmf (EmpathyTpCall *call)
651 EmpathyTpCallPriv *priv = GET_PRIV (call);
653 g_return_val_if_fail (EMPATHY_IS_TP_CALL (call), FALSE);
655 return tp_proxy_has_interface_by_id (priv->channel,
656 TP_IFACE_QUARK_CHANNEL_INTERFACE_DTMF);