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