]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-call.c
Ref the call object when emiting notify signals because signal handles could unref it
[empathy.git] / libempathy / empathy-tp-call.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3  * Copyright (C) 2007 Elliot Fairweather
4  * Copyright (C) 2007-2008 Collabora Ltd.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * Authors: Elliot Fairweather <elliot.fairweather@collabora.co.uk>
21  *          Xavier Claessens <xclaesse@gmail.com>
22  */
23
24 #include <string.h>
25
26 #include <telepathy-glib/proxy-subclass.h>
27 #include <telepathy-glib/dbus.h>
28
29 #include <extensions/extensions.h>
30
31 #include "empathy-tp-call.h"
32 #include "empathy-contact-factory.h"
33 #include "empathy-tp-group.h"
34 #include "empathy-utils.h"
35
36 #define DEBUG_FLAG EMPATHY_DEBUG_TP
37 #include "empathy-debug.h"
38
39 #define STREAM_ENGINE_BUS_NAME "org.freedesktop.Telepathy.StreamEngine"
40 #define STREAM_ENGINE_OBJECT_PATH "/org/freedesktop/Telepathy/StreamEngine"
41
42 #define GET_PRIV(obj) EMPATHY_GET_PRIV (obj, EmpathyTpCall)
43 typedef struct
44 {
45   TpChannel *channel;
46   TpProxy *stream_engine;
47   TpDBusDaemon *dbus_daemon;
48   EmpathyTpGroup *group;
49   EmpathyContact *contact;
50   gboolean is_incoming;
51   guint status;
52   gboolean stream_engine_running;
53
54   EmpathyTpCallStream *audio;
55   EmpathyTpCallStream *video;
56 } EmpathyTpCallPriv;
57
58 enum
59 {
60   PROP_0,
61   PROP_CHANNEL,
62   PROP_CONTACT,
63   PROP_IS_INCOMING,
64   PROP_STATUS,
65   PROP_AUDIO_STREAM,
66   PROP_VIDEO_STREAM
67 };
68
69 G_DEFINE_TYPE (EmpathyTpCall, empathy_tp_call, G_TYPE_OBJECT)
70
71 static void
72 tp_call_add_stream (EmpathyTpCall *call,
73                     guint stream_id,
74                     guint contact_handle,
75                     guint stream_type,
76                     guint stream_state,
77                     guint stream_direction)
78 {
79   EmpathyTpCallPriv *priv = GET_PRIV (call);
80
81   switch (stream_type)
82     {
83       case TP_MEDIA_STREAM_TYPE_AUDIO:
84         DEBUG ("Audio stream - id: %d, state: %d, direction: %d",
85             stream_id, stream_state, stream_direction);
86         priv->audio->exists = TRUE;
87         priv->audio->id = stream_id;
88         priv->audio->state = stream_state;
89         priv->audio->direction = stream_direction;
90         g_object_notify (G_OBJECT (call), "audio-stream");
91         break;
92       case TP_MEDIA_STREAM_TYPE_VIDEO:
93         DEBUG ("Video stream - id: %d, state: %d, direction: %d",
94             stream_id, stream_state, stream_direction);
95         priv->video->exists = TRUE;
96         priv->video->id = stream_id;
97         priv->video->state = stream_state;
98         priv->video->direction = stream_direction;
99         g_object_notify (G_OBJECT (call), "video-stream");
100         break;
101       default:
102         DEBUG ("Unknown stream type: %d", stream_type);
103     }
104 }
105
106 static void
107 tp_call_stream_added_cb (TpChannel *channel,
108                          guint stream_id,
109                          guint contact_handle,
110                          guint stream_type,
111                          gpointer user_data,
112                          GObject *call)
113 {
114   DEBUG ("Stream added - stream id: %d, contact handle: %d, stream type: %d",
115       stream_id, contact_handle, stream_type);
116
117   tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
118       stream_type, TP_MEDIA_STREAM_STATE_DISCONNECTED,
119       TP_MEDIA_STREAM_DIRECTION_NONE);
120 }
121
122 static void
123 tp_call_stream_removed_cb (TpChannel *channel,
124                            guint stream_id,
125                            gpointer user_data,
126                            GObject *call)
127 {
128   EmpathyTpCallPriv *priv = GET_PRIV (call);
129
130   DEBUG ("Stream removed - stream id: %d", stream_id);
131
132   if (stream_id == priv->audio->id)
133     {
134       priv->audio->exists = FALSE;
135       g_object_notify (call, "audio-stream");
136     }
137   else if (stream_id == priv->video->id)
138     {
139       priv->video->exists = FALSE;
140       g_object_notify (call, "video-stream");
141     }
142 }
143
144 static void
145 tp_call_stream_state_changed_cb (TpChannel *proxy,
146                                  guint stream_id,
147                                  guint stream_state,
148                                  gpointer user_data,
149                                  GObject *call)
150 {
151   EmpathyTpCallPriv *priv = GET_PRIV (call);
152
153   DEBUG ("Stream state changed - stream id: %d, state state: %d",
154       stream_id, stream_state);
155
156   if (stream_id == priv->audio->id)
157     {
158       priv->audio->state = stream_state;
159       g_object_notify (call, "audio-stream");
160     }
161   else if (stream_id == priv->video->id)
162     {
163       priv->video->state = stream_state;
164       g_object_notify (call, "video-stream");
165     }
166 }
167
168 static void
169 tp_call_stream_direction_changed_cb (TpChannel *channel,
170                                      guint stream_id,
171                                      guint stream_direction,
172                                      guint pending_flags,
173                                      gpointer user_data,
174                                      GObject *call)
175 {
176   EmpathyTpCallPriv *priv = GET_PRIV (call);
177
178   DEBUG ("Stream direction changed - stream: %d, direction: %d",
179       stream_id, stream_direction);
180
181   if (stream_id == priv->audio->id)
182     {
183       priv->audio->direction = stream_direction;
184       g_object_notify (call, "audio-stream");
185     }
186   else if (stream_id == priv->video->id)
187     {
188       priv->video->direction = stream_direction;
189       g_object_notify (call, "video-stream");
190     }
191 }
192
193 static void
194 tp_call_request_streams_cb (TpChannel *channel,
195                             const GPtrArray *streams,
196                             const GError *error,
197                             gpointer user_data,
198                             GObject *call)
199 {
200   guint i;
201
202   if (error)
203     {
204       DEBUG ("Error requesting streams: %s", error->message);
205       return;
206     }
207
208   for (i = 0; i < streams->len; i++)
209     {
210       GValueArray *values;
211       guint stream_id;
212       guint contact_handle;
213       guint stream_type;
214       guint stream_state;
215       guint stream_direction;
216
217       values = g_ptr_array_index (streams, i);
218       stream_id = g_value_get_uint (g_value_array_get_nth (values, 0));
219       contact_handle = g_value_get_uint (g_value_array_get_nth (values, 1));
220       stream_type = g_value_get_uint (g_value_array_get_nth (values, 2));
221       stream_state = g_value_get_uint (g_value_array_get_nth (values, 3));
222       stream_direction = g_value_get_uint (g_value_array_get_nth (values, 4));
223
224       tp_call_add_stream (EMPATHY_TP_CALL (call), stream_id, contact_handle,
225           stream_type, stream_state, stream_direction);
226   }
227 }
228
229 static void
230 tp_call_request_streams_for_capabilities (EmpathyTpCall *call,
231                                           EmpathyCapabilities capabilities)
232 {
233   EmpathyTpCallPriv *priv = GET_PRIV (call);
234   GArray *stream_types;
235   guint handle;
236   guint stream_type;
237
238   if (capabilities == EMPATHY_CAPABILITIES_UNKNOWN)
239       capabilities = EMPATHY_CAPABILITIES_AUDIO | EMPATHY_CAPABILITIES_VIDEO;
240
241   DEBUG ("Requesting new stream for capabilities %d",
242       capabilities);
243
244   stream_types = g_array_new (FALSE, FALSE, sizeof (guint));
245   handle = empathy_contact_get_handle (priv->contact);
246
247   if (capabilities & EMPATHY_CAPABILITIES_AUDIO)
248     {
249       stream_type = TP_MEDIA_STREAM_TYPE_AUDIO;
250       g_array_append_val (stream_types, stream_type);
251     }
252   if (capabilities & EMPATHY_CAPABILITIES_VIDEO)
253     {
254       stream_type = TP_MEDIA_STREAM_TYPE_VIDEO;
255       g_array_append_val (stream_types, stream_type);
256     }
257
258   tp_cli_channel_type_streamed_media_call_request_streams (priv->channel, -1,
259       handle, stream_types, tp_call_request_streams_cb, NULL, NULL,
260       G_OBJECT (call));
261
262   g_array_free (stream_types, TRUE);
263 }
264
265 static void
266 tp_call_member_added_cb (EmpathyTpGroup *group,
267                          EmpathyContact *contact,
268                          EmpathyContact *actor,
269                          guint reason,
270                          const gchar *message,
271                          EmpathyTpCall *call)
272 {
273   EmpathyTpCallPriv *priv = GET_PRIV (call);
274
275   g_object_ref (call);
276   if (!priv->contact && !empathy_contact_is_user (contact))
277     {
278       priv->contact = g_object_ref (contact);
279       priv->is_incoming = TRUE;
280       priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
281       g_object_notify (G_OBJECT (call), "is-incoming");
282       g_object_notify (G_OBJECT (call), "contact");
283       g_object_notify (G_OBJECT (call), "status");
284       tp_call_request_streams_for_capabilities (call,
285           EMPATHY_CAPABILITIES_AUDIO);
286
287     }
288
289   if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING &&
290       ((priv->is_incoming && contact != priv->contact) ||
291        (!priv->is_incoming && contact == priv->contact)))
292     {
293       priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
294       g_object_notify (G_OBJECT (call), "status");
295     }
296   g_object_unref (call);
297 }
298
299 static void
300 tp_call_remote_pending_cb (EmpathyTpGroup *group,
301                            EmpathyContact *contact,
302                            EmpathyContact *actor,
303                            guint reason,
304                            const gchar *message,
305                            EmpathyTpCall *call)
306 {
307   EmpathyTpCallPriv *priv = GET_PRIV (call);
308
309   g_object_ref (call);
310   if (!priv->contact && !empathy_contact_is_user (contact))
311     {
312       priv->contact = g_object_ref (contact);
313       priv->is_incoming = FALSE;
314       priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
315       g_object_notify (G_OBJECT (call), "is-incoming");
316       g_object_notify (G_OBJECT (call), "contact"); 
317       g_object_notify (G_OBJECT (call), "status");      
318       tp_call_request_streams_for_capabilities (call,
319           EMPATHY_CAPABILITIES_AUDIO);
320     }
321   g_object_unref (call);
322 }
323
324 static void
325 tp_call_channel_invalidated_cb (TpChannel     *channel,
326                                 GQuark         domain,
327                                 gint           code,
328                                 gchar         *message,
329                                 EmpathyTpCall *call)
330 {
331   EmpathyTpCallPriv *priv = GET_PRIV (call);
332
333   DEBUG ("Channel invalidated: %s", message);
334   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
335   g_object_notify (G_OBJECT (call), "status");
336 }
337
338 static void
339 tp_call_async_cb (TpProxy *proxy,
340                   const GError *error,
341                   gpointer user_data,
342                   GObject *call)
343 {
344   if (error)
345       DEBUG ("Error %s: %s", (gchar*) user_data, error->message);
346 }
347
348 static void
349 tp_call_close_channel (EmpathyTpCall *call)
350 {
351   EmpathyTpCallPriv *priv = GET_PRIV (call);
352
353   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
354       return;
355
356   DEBUG ("Closing channel");
357
358   tp_cli_channel_call_close (priv->channel, -1,
359       NULL, NULL, NULL, NULL);
360
361   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
362   g_object_notify (G_OBJECT (call), "status");
363 }
364
365 static void
366 tp_call_stream_engine_invalidated_cb (TpProxy       *stream_engine,
367                                       GQuark         domain,
368                                       gint           code,
369                                       gchar         *message,
370                                       EmpathyTpCall *call)
371 {
372   DEBUG ("Stream engine proxy invalidated: %s", message);
373   tp_call_close_channel (call);
374 }
375
376 static void
377 tp_call_stream_engine_watch_name_owner_cb (TpDBusDaemon *daemon,
378                                            const gchar *name,
379                                            const gchar *new_owner,
380                                            gpointer call)
381 {
382   EmpathyTpCallPriv *priv = GET_PRIV (call);
383
384   /* G_STR_EMPTY(new_owner) means either stream-engine has not started yet or
385    * has crashed. We want to close the channel if stream-engine has crashed.
386    * */
387   DEBUG ("Watch SE: name='%s' SE running='%s' new_owner='%s'",
388       name, priv->stream_engine_running ? "yes" : "no",
389       new_owner ? new_owner : "none");
390   if (priv->stream_engine_running && G_STR_EMPTY (new_owner))
391     {
392       DEBUG ("Stream engine falled off the bus");
393       tp_call_close_channel (call);
394       return;
395     }
396
397   priv->stream_engine_running = !G_STR_EMPTY (new_owner);
398 }
399
400 static void
401 tp_call_stream_engine_handle_channel (EmpathyTpCall *call)
402 {
403   EmpathyTpCallPriv *priv = GET_PRIV (call);
404   gchar *channel_type;
405   gchar *object_path;
406   guint handle_type;
407   guint handle;
408   TpProxy *connection;
409
410   DEBUG ("Revving up the stream engine");
411
412   priv->stream_engine = g_object_new (TP_TYPE_PROXY,
413       "bus-name", STREAM_ENGINE_BUS_NAME,
414       "dbus-connection", tp_get_bus (),
415       "object-path", STREAM_ENGINE_OBJECT_PATH,
416        NULL);
417   tp_proxy_add_interface_by_id (priv->stream_engine,
418       EMP_IFACE_QUARK_STREAM_ENGINE);
419   tp_proxy_add_interface_by_id (priv->stream_engine,
420       EMP_IFACE_QUARK_CHANNEL_HANDLER);
421
422   g_signal_connect (priv->stream_engine, "invalidated",
423       G_CALLBACK (tp_call_stream_engine_invalidated_cb),
424       call);
425   
426   /* FIXME: dbus daemon should be unique */
427   priv->dbus_daemon = tp_dbus_daemon_new (tp_get_bus ());
428   tp_dbus_daemon_watch_name_owner (priv->dbus_daemon, STREAM_ENGINE_BUS_NAME,
429       tp_call_stream_engine_watch_name_owner_cb,
430       call, NULL);
431
432   g_object_get (priv->channel,
433       "connection", &connection,
434       "channel-type", &channel_type,
435       "object-path", &object_path,
436       "handle_type", &handle_type,
437       "handle", &handle,
438       NULL);
439
440   emp_cli_channel_handler_call_handle_channel (priv->stream_engine, -1,
441         connection->bus_name,
442         connection->object_path,
443         channel_type, object_path, handle_type, handle,
444         tp_call_async_cb, "calling handle channel", NULL,
445         G_OBJECT (call));
446
447   g_object_unref (connection);
448   g_free (channel_type);
449   g_free (object_path);
450 }
451
452 static GObject *
453 tp_call_constructor (GType type,
454                      guint n_construct_params,
455                      GObjectConstructParam *construct_params)
456 {
457   GObject *object;
458   EmpathyTpCall *call;
459   EmpathyTpCallPriv *priv;
460
461   object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
462       n_construct_params, construct_params);
463
464   call = EMPATHY_TP_CALL (object);
465   priv = GET_PRIV (call);
466
467   /* Setup streamed media channel */
468   g_signal_connect (priv->channel, "invalidated",
469       G_CALLBACK (tp_call_channel_invalidated_cb), call);
470   tp_cli_channel_type_streamed_media_connect_to_stream_added (priv->channel,
471       tp_call_stream_added_cb, NULL, NULL, G_OBJECT (call), NULL);
472   tp_cli_channel_type_streamed_media_connect_to_stream_removed (priv->channel,
473       tp_call_stream_removed_cb, NULL, NULL, G_OBJECT (call), NULL);
474   tp_cli_channel_type_streamed_media_connect_to_stream_state_changed (priv->channel,
475       tp_call_stream_state_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
476   tp_cli_channel_type_streamed_media_connect_to_stream_direction_changed (priv->channel,
477       tp_call_stream_direction_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
478   tp_cli_channel_type_streamed_media_call_list_streams (priv->channel, -1,
479       tp_call_request_streams_cb, NULL, NULL, G_OBJECT (call));
480
481   /* Setup group interface */
482   priv->group = empathy_tp_group_new (priv->channel);
483
484   g_signal_connect (priv->group, "member-added",
485       G_CALLBACK (tp_call_member_added_cb), call);
486   g_signal_connect (priv->group, "remote-pending",
487       G_CALLBACK (tp_call_remote_pending_cb), call);
488
489   /* Start stream engine */
490   tp_call_stream_engine_handle_channel (call);
491
492   return object;
493 }
494
495 static void 
496 tp_call_finalize (GObject *object)
497 {
498   EmpathyTpCallPriv *priv = GET_PRIV (object);
499
500   DEBUG ("Finalizing: %p", object);
501
502   g_slice_free (EmpathyTpCallStream, priv->audio);
503   g_slice_free (EmpathyTpCallStream, priv->video);
504   g_object_unref (priv->group);
505
506   if (priv->channel != NULL)
507     {
508       g_signal_handlers_disconnect_by_func (priv->channel,
509           tp_call_channel_invalidated_cb, object);
510       tp_call_close_channel (EMPATHY_TP_CALL (object));
511       g_object_unref (priv->channel);
512     }
513
514   if (priv->stream_engine != NULL)
515     {
516       g_signal_handlers_disconnect_by_func (priv->stream_engine,
517           tp_call_stream_engine_invalidated_cb, object);
518       g_object_unref (priv->stream_engine);
519     }
520
521   if (priv->contact != NULL)
522       g_object_unref (priv->contact);
523
524   if (priv->dbus_daemon != NULL)
525     {
526       tp_dbus_daemon_cancel_name_owner_watch (priv->dbus_daemon,
527           STREAM_ENGINE_BUS_NAME,
528           tp_call_stream_engine_watch_name_owner_cb,
529           object);
530       g_object_unref (priv->dbus_daemon);
531     }
532
533   (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
534 }
535
536 static void 
537 tp_call_set_property (GObject *object,
538                       guint prop_id,
539                       const GValue *value,
540                       GParamSpec *pspec)
541 {
542   EmpathyTpCallPriv *priv = GET_PRIV (object);
543
544   switch (prop_id)
545     {
546     case PROP_CHANNEL:
547       priv->channel = g_value_dup_object (value);
548       break;
549     default:
550       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
551       break;
552   }
553 }
554
555 static void
556 tp_call_get_property (GObject *object,
557                       guint prop_id,
558                       GValue *value,
559                       GParamSpec *pspec)
560 {
561   EmpathyTpCallPriv *priv = GET_PRIV (object);
562
563   switch (prop_id)
564     {
565     case PROP_CHANNEL:
566       g_value_set_object (value, priv->channel);
567       break;
568     case PROP_CONTACT:
569       g_value_set_object (value, priv->contact);
570       break;
571     case PROP_IS_INCOMING:
572       g_value_set_boolean (value, priv->is_incoming);
573       break;
574     case PROP_STATUS:
575       g_value_set_uint (value, priv->status);
576       break;
577     case PROP_AUDIO_STREAM:
578       g_value_set_pointer (value, priv->audio);
579       break;
580     case PROP_VIDEO_STREAM:
581       g_value_set_pointer (value, priv->video);
582       break;
583     default:
584       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
585       break;
586   }
587 }
588
589 static void
590 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
591 {
592   GObjectClass *object_class = G_OBJECT_CLASS (klass);
593
594   emp_cli_init ();
595
596   object_class->constructor = tp_call_constructor;
597   object_class->finalize = tp_call_finalize;
598   object_class->set_property = tp_call_set_property;
599   object_class->get_property = tp_call_get_property;
600
601   g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
602
603   g_object_class_install_property (object_class, PROP_CHANNEL,
604       g_param_spec_object ("channel", "channel", "channel",
605       TP_TYPE_CHANNEL,
606       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
607       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
608   g_object_class_install_property (object_class, PROP_CONTACT,
609       g_param_spec_object ("contact", "Call contact", "Call contact",
610       EMPATHY_TYPE_CONTACT,
611       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
612   g_object_class_install_property (object_class, PROP_IS_INCOMING,
613       g_param_spec_boolean ("is-incoming", "Is media stream incoming",
614       "Is media stream incoming", FALSE, G_PARAM_READABLE |
615       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
616   g_object_class_install_property (object_class, PROP_STATUS,
617       g_param_spec_uint ("status", "Call status",
618       "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
619       G_PARAM_STATIC_BLURB));
620   g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
621       g_param_spec_pointer ("audio-stream", "Audio stream data",
622       "Audio stream data",
623       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
624   g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
625       g_param_spec_pointer ("video-stream", "Video stream data",
626       "Video stream data",
627       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
628 }
629
630 static void
631 empathy_tp_call_init (EmpathyTpCall *call)
632 {
633   EmpathyTpCallPriv *priv = G_TYPE_INSTANCE_GET_PRIVATE (call,
634                 EMPATHY_TYPE_TP_CALL, EmpathyTpCallPriv);
635
636   call->priv = priv;
637   priv->status = EMPATHY_TP_CALL_STATUS_READYING;
638   priv->contact = NULL;
639   priv->stream_engine_running = FALSE;
640   priv->audio = g_slice_new0 (EmpathyTpCallStream);
641   priv->video = g_slice_new0 (EmpathyTpCallStream);
642   priv->audio->exists = FALSE;
643   priv->video->exists = FALSE;
644 }
645
646 EmpathyTpCall *
647 empathy_tp_call_new (TpChannel *channel)
648 {
649   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
650
651   return g_object_new (EMPATHY_TYPE_TP_CALL,
652       "channel", channel,
653       NULL);
654 }
655
656 void
657 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
658 {
659   EmpathyTpCallPriv *priv = GET_PRIV (call);
660   EmpathyContact *self_contact;
661
662   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
663   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_PENDING);
664
665   DEBUG ("Accepting incoming call");
666
667   self_contact = empathy_tp_group_get_self_contact (priv->group);
668   empathy_tp_group_add_member (priv->group, self_contact, NULL);
669   g_object_unref (self_contact);
670 }
671
672 void
673 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
674                                                 gboolean is_sending)
675 {
676   EmpathyTpCallPriv *priv = GET_PRIV (call);
677   guint new_direction;
678
679   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
680   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
681
682   DEBUG ("Requesting video stream direction - is_sending: %d", is_sending);
683
684   if (!priv->video->exists)
685     {
686       if (is_sending)
687           tp_call_request_streams_for_capabilities (call,
688               EMPATHY_CAPABILITIES_VIDEO);
689       return;
690     }
691
692   if (is_sending)
693       new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
694   else
695       new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
696
697   tp_cli_channel_type_streamed_media_call_request_stream_direction (priv->channel,
698       -1, priv->video->id, new_direction,
699       (tp_cli_channel_type_streamed_media_callback_for_request_stream_direction)
700       tp_call_async_cb, NULL, NULL, G_OBJECT (call));
701 }
702
703 void
704 empathy_tp_call_add_preview_video (EmpathyTpCall *call,
705                                    guint preview_video_socket_id)
706 {
707   EmpathyTpCallPriv *priv = GET_PRIV (call);
708
709   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
710
711   DEBUG ("Adding preview video");
712
713   emp_cli_stream_engine_call_add_preview_window (priv->stream_engine, -1,
714       preview_video_socket_id,
715       tp_call_async_cb,
716       "adding preview window", NULL,
717       G_OBJECT (call));
718 }
719
720 void
721 empathy_tp_call_remove_preview_video (EmpathyTpCall *call,
722                                       guint preview_video_socket_id)
723 {
724   EmpathyTpCallPriv *priv = GET_PRIV (call);
725
726   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
727
728   DEBUG ("Removing preview video");
729
730   emp_cli_stream_engine_call_remove_preview_window (priv->stream_engine, -1,
731       preview_video_socket_id,
732       tp_call_async_cb,
733       "removing preview window", NULL,
734       G_OBJECT (call));
735 }
736
737 void
738 empathy_tp_call_add_output_video (EmpathyTpCall *call,
739                                   guint output_video_socket_id)
740 {
741   EmpathyTpCallPriv *priv = GET_PRIV (call);
742
743   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
744
745   DEBUG ("Adding output video - socket: %d", output_video_socket_id);
746
747   emp_cli_stream_engine_call_set_output_window (priv->stream_engine, -1,
748       TP_PROXY (priv->channel)->object_path,
749       priv->video->id, output_video_socket_id,
750       tp_call_async_cb,
751       "setting output window", NULL,
752       G_OBJECT (call));
753 }
754
755 void
756 empathy_tp_call_set_output_volume (EmpathyTpCall *call,
757                                    guint volume)
758 {
759   EmpathyTpCallPriv *priv = GET_PRIV (call);
760
761   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
762   g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
763
764   DEBUG ("Setting output volume: %d", volume);
765
766   emp_cli_stream_engine_call_set_output_volume (priv->stream_engine, -1,
767       TP_PROXY (priv->channel)->object_path,
768       priv->audio->id, volume,
769       tp_call_async_cb,
770       "setting output volume", NULL,
771       G_OBJECT (call));
772 }
773
774 void
775 empathy_tp_call_mute_output (EmpathyTpCall *call,
776                              gboolean is_muted)
777 {
778   EmpathyTpCallPriv *priv = GET_PRIV (call);
779
780   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
781
782   DEBUG ("Setting output mute: %d", is_muted);
783
784   emp_cli_stream_engine_call_mute_output (priv->stream_engine, -1,
785       TP_PROXY (priv->channel)->object_path,
786       priv->audio->id, is_muted,
787       tp_call_async_cb,
788       "muting output", NULL,
789       G_OBJECT (call));
790 }
791
792 void
793 empathy_tp_call_mute_input (EmpathyTpCall *call,
794                             gboolean is_muted)
795 {
796   EmpathyTpCallPriv *priv = GET_PRIV (call);
797
798   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
799
800   DEBUG ("Setting input mute: %d", is_muted);
801
802   emp_cli_stream_engine_call_mute_input (priv->stream_engine, -1,
803       TP_PROXY (priv->channel)->object_path,
804       priv->audio->id, is_muted,
805       tp_call_async_cb,
806       "muting input", NULL,
807       G_OBJECT (call));
808 }
809
810 void
811 empathy_tp_call_start_tone (EmpathyTpCall *call, TpDTMFEvent event)
812 {
813   EmpathyTpCallPriv *priv = GET_PRIV (call);
814
815   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
816   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
817
818   if (!priv->audio->exists)
819       return;
820
821   tp_cli_channel_interface_dtmf_call_start_tone (priv->channel, -1,
822       priv->audio->id, event,
823       (tp_cli_channel_interface_dtmf_callback_for_start_tone) tp_call_async_cb,
824       "starting tone", NULL, G_OBJECT (call));
825 }
826
827 void
828 empathy_tp_call_stop_tone (EmpathyTpCall *call)
829 {
830   EmpathyTpCallPriv *priv = GET_PRIV (call);
831
832   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
833   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
834
835   if (!priv->audio->exists)
836       return;
837
838   tp_cli_channel_interface_dtmf_call_stop_tone (priv->channel, -1,
839       priv->audio->id,
840       (tp_cli_channel_interface_dtmf_callback_for_stop_tone) tp_call_async_cb,
841       "stoping tone", NULL, G_OBJECT (call));
842 }
843