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