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