]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-call.c
Fix OUTGOING 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->status == EMPATHY_TP_CALL_STATUS_PENDING &&
295       ((priv->is_incoming && contact != priv->contact) ||
296        (!priv->is_incoming && contact == priv->contact)))
297     {
298       priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
299       g_object_notify (G_OBJECT (call), "status");
300     }
301 }
302
303 static void
304 tp_call_local_pending_cb (EmpathyTpGroup *group,
305                           EmpathyContact *contact,
306                           EmpathyContact *actor,
307                           guint reason,
308                           const gchar *message,
309                           EmpathyTpCall *call)
310 {
311   EmpathyTpCallPriv *priv = GET_PRIV (call);
312
313   if (!priv->contact && !empathy_contact_is_user (contact))
314     {
315       priv->contact = g_object_ref (contact);
316       priv->is_incoming = TRUE;
317       priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
318       g_object_notify (G_OBJECT (call), "is-incoming");
319       g_object_notify (G_OBJECT (call), "contact"); 
320       g_object_notify (G_OBJECT (call), "status");      
321     }
322 }
323
324 static void
325 tp_call_remote_pending_cb (EmpathyTpGroup *group,
326                            EmpathyContact *contact,
327                            EmpathyContact *actor,
328                            guint reason,
329                            const gchar *message,
330                            EmpathyTpCall *call)
331 {
332   EmpathyTpCallPriv *priv = GET_PRIV (call);
333
334   if (!priv->contact && !empathy_contact_is_user (contact))
335     {
336       priv->contact = g_object_ref (contact);
337       priv->is_incoming = FALSE;
338       priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
339       tp_call_request_streams (call);
340       g_object_notify (G_OBJECT (call), "is-incoming");
341       g_object_notify (G_OBJECT (call), "contact"); 
342       g_object_notify (G_OBJECT (call), "status");      
343     }
344 }
345
346 static void
347 tp_call_channel_invalidated_cb (TpChannel     *channel,
348                                 GQuark         domain,
349                                 gint           code,
350                                 gchar         *message,
351                                 EmpathyTpCall *call)
352 {
353   EmpathyTpCallPriv *priv = GET_PRIV (call);
354
355   empathy_debug (DEBUG_DOMAIN, "Channel invalidated: %s", message);
356   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
357   g_object_notify (G_OBJECT (call), "status");
358 }
359
360 static void
361 tp_call_async_cb (TpProxy *proxy,
362                   const GError *error,
363                   gpointer user_data,
364                   GObject *call)
365 {
366   if (error)
367     {
368       empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
369           user_data, error->message);
370     }
371 }
372
373 static void
374 tp_call_close_channel (EmpathyTpCall *call)
375 {
376   EmpathyTpCallPriv *priv = GET_PRIV (call);
377
378   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
379       return;
380
381   empathy_debug (DEBUG_DOMAIN, "Closing channel");
382
383   tp_cli_channel_call_close (priv->channel, -1,
384       NULL, NULL, NULL, NULL);
385
386   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
387   g_object_notify (G_OBJECT (call), "status");
388 }
389
390 static void
391 tp_call_stream_engine_invalidated_cb (TpProxy       *stream_engine,
392                                       GQuark         domain,
393                                       gint           code,
394                                       gchar         *message,
395                                       EmpathyTpCall *call)
396 {
397   empathy_debug (DEBUG_DOMAIN, "Stream engine proxy invalidated: %s",
398       message);
399   tp_call_close_channel (call);
400 }
401
402 static void
403 tp_call_stream_engine_watch_name_owner_cb (TpDBusDaemon *daemon,
404                                            const gchar *name,
405                                            const gchar *new_owner,
406                                            gpointer call)
407 {
408   EmpathyTpCallPriv *priv = GET_PRIV (call);
409
410   /* G_STR_EMPTY(new_owner) means either stream-engine has not started yet or
411    * has crashed. We want to close the channel if stream-engine has crashed.
412    * */
413   empathy_debug (DEBUG_DOMAIN,
414                  "Watch SE: name='%s' SE running='%s' new_owner='%s'",
415                  name, priv->stream_engine_running ? "yes" : "no",
416                  new_owner ? new_owner : "none");
417   if (priv->stream_engine_running && G_STR_EMPTY (new_owner))
418     {
419       empathy_debug (DEBUG_DOMAIN, "Stream engine falled off the bus");
420       tp_call_close_channel (call);
421       return;
422     }
423
424   priv->stream_engine_running = !G_STR_EMPTY (new_owner);
425 }
426
427 static void
428 tp_call_stream_engine_handle_channel (EmpathyTpCall *call)
429 {
430   EmpathyTpCallPriv *priv = GET_PRIV (call);
431   gchar *channel_type;
432   gchar *object_path;
433   guint handle_type;
434   guint handle;
435   TpProxy *connection;
436
437   empathy_debug (DEBUG_DOMAIN, "Revving up the stream engine");
438
439   priv->stream_engine = g_object_new (TP_TYPE_PROXY,
440       "bus-name", STREAM_ENGINE_BUS_NAME,
441       "dbus-connection", tp_get_bus (),
442       "object-path", STREAM_ENGINE_OBJECT_PATH,
443        NULL);
444   tp_proxy_add_interface_by_id (priv->stream_engine,
445       EMP_IFACE_QUARK_STREAM_ENGINE);
446   tp_proxy_add_interface_by_id (priv->stream_engine,
447       EMP_IFACE_QUARK_CHANNEL_HANDLER);
448
449   g_signal_connect (priv->stream_engine, "invalidated",
450       G_CALLBACK (tp_call_stream_engine_invalidated_cb),
451       call);
452   
453   /* FIXME: dbus daemon should be unique */
454   priv->dbus_daemon = tp_dbus_daemon_new (tp_get_bus ());
455   tp_dbus_daemon_watch_name_owner (priv->dbus_daemon, STREAM_ENGINE_BUS_NAME,
456       tp_call_stream_engine_watch_name_owner_cb,
457       call, NULL);
458
459   g_object_get (priv->channel,
460       "connection", &connection,
461       "channel-type", &channel_type,
462       "object-path", &object_path,
463       "handle_type", &handle_type,
464       "handle", &handle,
465       NULL);
466
467   emp_cli_channel_handler_call_handle_channel (priv->stream_engine, -1,
468         connection->bus_name,
469         connection->object_path,
470         channel_type, object_path, handle_type, handle,
471         tp_call_async_cb, "calling handle channel", NULL,
472         G_OBJECT (call));
473
474   g_object_unref (connection);
475   g_free (channel_type);
476   g_free (object_path);
477 }
478
479 static GObject *
480 tp_call_constructor (GType type,
481                      guint n_construct_params,
482                      GObjectConstructParam *construct_params)
483 {
484   GObject *object;
485   EmpathyTpCall *call;
486   EmpathyTpCallPriv *priv;
487
488   object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
489       n_construct_params, construct_params);
490
491   call = EMPATHY_TP_CALL (object);
492   priv = GET_PRIV (call);
493
494   /* Setup streamed media channel */
495   g_signal_connect (priv->channel, "invalidated",
496       G_CALLBACK (tp_call_channel_invalidated_cb), call);
497   tp_cli_channel_type_streamed_media_connect_to_stream_added (priv->channel,
498       tp_call_stream_added_cb, NULL, NULL, G_OBJECT (call), NULL);
499   tp_cli_channel_type_streamed_media_connect_to_stream_removed (priv->channel,
500       tp_call_stream_removed_cb, NULL, NULL, G_OBJECT (call), NULL);
501   tp_cli_channel_type_streamed_media_connect_to_stream_state_changed (priv->channel,
502       tp_call_stream_state_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
503   tp_cli_channel_type_streamed_media_connect_to_stream_direction_changed (priv->channel,
504       tp_call_stream_direction_changed_cb, NULL, NULL, G_OBJECT (call), NULL);
505   tp_cli_channel_type_streamed_media_call_list_streams (priv->channel, -1,
506       tp_call_request_streams_cb, NULL, NULL, G_OBJECT (call));
507
508   /* Setup group interface */
509   priv->group = empathy_tp_group_new (priv->channel);
510
511   g_signal_connect (priv->group, "member-added",
512       G_CALLBACK (tp_call_member_added_cb), call);
513   g_signal_connect (priv->group, "local-pending",
514       G_CALLBACK (tp_call_local_pending_cb), call);
515   g_signal_connect (priv->group, "remote-pending",
516       G_CALLBACK (tp_call_remote_pending_cb), call);
517
518   /* Start stream engine */
519   tp_call_stream_engine_handle_channel (call);
520
521   return object;
522 }
523
524 static void 
525 tp_call_finalize (GObject *object)
526 {
527   EmpathyTpCallPriv *priv = GET_PRIV (object);
528
529   empathy_debug (DEBUG_DOMAIN, "Finalizing: %p", object);
530
531   g_slice_free (EmpathyTpCallStream, priv->audio);
532   g_slice_free (EmpathyTpCallStream, priv->video);
533   g_object_unref (priv->group);
534
535   if (priv->channel != NULL)
536     {
537       g_signal_handlers_disconnect_by_func (priv->channel,
538           tp_call_channel_invalidated_cb, object);
539       tp_call_close_channel (EMPATHY_TP_CALL (object));
540       g_object_unref (priv->channel);
541     }
542
543   if (priv->stream_engine != NULL)
544     {
545       g_signal_handlers_disconnect_by_func (priv->stream_engine,
546           tp_call_stream_engine_invalidated_cb, object);
547       g_object_unref (priv->stream_engine);
548     }
549
550   if (priv->contact != NULL)
551       g_object_unref (priv->contact);
552
553   if (priv->dbus_daemon != NULL)
554     {
555       tp_dbus_daemon_cancel_name_owner_watch (priv->dbus_daemon,
556           STREAM_ENGINE_BUS_NAME,
557           tp_call_stream_engine_watch_name_owner_cb,
558           object);
559       g_object_unref (priv->dbus_daemon);
560     }
561
562   (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
563 }
564
565 static void 
566 tp_call_set_property (GObject *object,
567                       guint prop_id,
568                       const GValue *value,
569                       GParamSpec *pspec)
570 {
571   EmpathyTpCallPriv *priv = GET_PRIV (object);
572
573   switch (prop_id)
574     {
575     case PROP_CHANNEL:
576       priv->channel = g_value_dup_object (value);
577       break;
578     default:
579       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
580       break;
581   }
582 }
583
584 static void
585 tp_call_get_property (GObject *object,
586                       guint prop_id,
587                       GValue *value,
588                       GParamSpec *pspec)
589 {
590   EmpathyTpCallPriv *priv = GET_PRIV (object);
591
592   switch (prop_id)
593     {
594     case PROP_CHANNEL:
595       g_value_set_object (value, priv->channel);
596       break;
597     case PROP_CONTACT:
598       g_value_set_object (value, priv->contact);
599       break;
600     case PROP_IS_INCOMING:
601       g_value_set_boolean (value, priv->is_incoming);
602       break;
603     case PROP_STATUS:
604       g_value_set_uint (value, priv->status);
605       break;
606     case PROP_AUDIO_STREAM:
607       g_value_set_pointer (value, priv->audio);
608       break;
609     case PROP_VIDEO_STREAM:
610       g_value_set_pointer (value, priv->video);
611       break;
612     default:
613       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
614       break;
615   }
616 }
617
618 static void
619 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
620 {
621   GObjectClass *object_class = G_OBJECT_CLASS (klass);
622
623   emp_cli_init ();
624
625   object_class->constructor = tp_call_constructor;
626   object_class->finalize = tp_call_finalize;
627   object_class->set_property = tp_call_set_property;
628   object_class->get_property = tp_call_get_property;
629
630   g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
631
632   g_object_class_install_property (object_class, PROP_CHANNEL,
633       g_param_spec_object ("channel", "channel", "channel",
634       TP_TYPE_CHANNEL,
635       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
636       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
637   g_object_class_install_property (object_class, PROP_CONTACT,
638       g_param_spec_object ("contact", "Call contact", "Call contact",
639       EMPATHY_TYPE_CONTACT,
640       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
641   g_object_class_install_property (object_class, PROP_IS_INCOMING,
642       g_param_spec_boolean ("is-incoming", "Is media stream incoming",
643       "Is media stream incoming", FALSE, G_PARAM_READABLE |
644       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
645   g_object_class_install_property (object_class, PROP_STATUS,
646       g_param_spec_uint ("status", "Call status",
647       "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
648       G_PARAM_STATIC_BLURB));
649   g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
650       g_param_spec_pointer ("audio-stream", "Audio stream data",
651       "Audio stream data",
652       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
653   g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
654       g_param_spec_pointer ("video-stream", "Video stream data",
655       "Video stream data",
656       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
657 }
658
659 static void
660 empathy_tp_call_init (EmpathyTpCall *call)
661 {
662   EmpathyTpCallPriv *priv = GET_PRIV (call);
663
664   priv->status = EMPATHY_TP_CALL_STATUS_READYING;
665   priv->contact = NULL;
666   priv->stream_engine_running = FALSE;
667   priv->audio = g_slice_new0 (EmpathyTpCallStream);
668   priv->video = g_slice_new0 (EmpathyTpCallStream);
669   priv->audio->exists = FALSE;
670   priv->video->exists = FALSE;
671 }
672
673 EmpathyTpCall *
674 empathy_tp_call_new (TpChannel *channel)
675 {
676   g_return_val_if_fail (TP_IS_CHANNEL (channel), NULL);
677
678   return g_object_new (EMPATHY_TYPE_TP_CALL,
679       "channel", channel,
680       NULL);
681 }
682
683 void
684 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
685 {
686   EmpathyTpCallPriv *priv = GET_PRIV (call);
687   EmpathyContact *self_contact;
688
689   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
690   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_PENDING);
691
692   empathy_debug (DEBUG_DOMAIN, "Accepting incoming call");
693
694   self_contact = empathy_tp_group_get_self_contact (priv->group);
695   empathy_tp_group_add_member (priv->group, self_contact, NULL);
696   g_object_unref (self_contact);
697 }
698
699 void
700 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
701                                                 gboolean is_sending)
702 {
703   EmpathyTpCallPriv *priv = GET_PRIV (call);
704   guint new_direction;
705
706   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
707   g_return_if_fail (priv->status == EMPATHY_TP_CALL_STATUS_ACCEPTED);
708
709   empathy_debug (DEBUG_DOMAIN,
710       "Requesting video stream direction - is_sending: %d", is_sending);
711
712   if (!priv->video->exists)
713     {
714       if (is_sending)
715           tp_call_request_streams_for_capabilities (call,
716               EMPATHY_CAPABILITIES_VIDEO);
717       return;
718     }
719
720   if (is_sending)
721       new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
722   else
723       new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
724
725   tp_cli_channel_type_streamed_media_call_request_stream_direction (priv->channel,
726       -1, priv->video->id, new_direction,
727       (tp_cli_channel_type_streamed_media_callback_for_request_stream_direction)
728       tp_call_async_cb, NULL, NULL, G_OBJECT (call));
729 }
730
731 void
732 empathy_tp_call_add_preview_video (EmpathyTpCall *call,
733                                    guint preview_video_socket_id)
734 {
735   EmpathyTpCallPriv *priv = GET_PRIV (call);
736
737   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
738
739   empathy_debug (DEBUG_DOMAIN, "Adding preview video");
740
741   emp_cli_stream_engine_call_add_preview_window (priv->stream_engine, -1,
742       preview_video_socket_id,
743       tp_call_async_cb,
744       "adding preview window", NULL,
745       G_OBJECT (call));
746 }
747
748 void
749 empathy_tp_call_remove_preview_video (EmpathyTpCall *call,
750                                       guint preview_video_socket_id)
751 {
752   EmpathyTpCallPriv *priv = GET_PRIV (call);
753
754   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
755
756   empathy_debug (DEBUG_DOMAIN, "Removing preview video");
757
758   emp_cli_stream_engine_call_remove_preview_window (priv->stream_engine, -1,
759       preview_video_socket_id,
760       tp_call_async_cb,
761       "removing preview window", NULL,
762       G_OBJECT (call));
763 }
764
765 void
766 empathy_tp_call_add_output_video (EmpathyTpCall *call,
767                                   guint output_video_socket_id)
768 {
769   EmpathyTpCallPriv *priv = GET_PRIV (call);
770
771   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
772   g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
773
774   empathy_debug (DEBUG_DOMAIN, "Adding output video - socket: %d",
775       output_video_socket_id);
776
777   emp_cli_stream_engine_call_set_output_window (priv->stream_engine, -1,
778       TP_PROXY (priv->channel)->object_path,
779       priv->video->id, output_video_socket_id,
780       tp_call_async_cb,
781       "setting output window", NULL,
782       G_OBJECT (call));
783 }
784
785 void
786 empathy_tp_call_set_output_volume (EmpathyTpCall *call,
787                                    guint volume)
788 {
789   EmpathyTpCallPriv *priv = GET_PRIV (call);
790
791   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
792   g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
793
794   empathy_debug (DEBUG_DOMAIN, "Setting output volume: %d", volume);
795
796   emp_cli_stream_engine_call_set_output_volume (priv->stream_engine, -1,
797       TP_PROXY (priv->channel)->object_path,
798       priv->audio->id, volume,
799       tp_call_async_cb,
800       "setting output volume", NULL,
801       G_OBJECT (call));
802 }
803
804 void
805 empathy_tp_call_mute_output (EmpathyTpCall *call,
806                              gboolean is_muted)
807 {
808   EmpathyTpCallPriv *priv = GET_PRIV (call);
809
810   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
811   g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
812
813   empathy_debug (DEBUG_DOMAIN, "Setting output mute: %d", is_muted);
814
815   emp_cli_stream_engine_call_mute_output (priv->stream_engine, -1,
816       TP_PROXY (priv->channel)->object_path,
817       priv->audio->id, is_muted,
818       tp_call_async_cb,
819       "muting output", NULL,
820       G_OBJECT (call));
821 }
822
823 void
824 empathy_tp_call_mute_input (EmpathyTpCall *call,
825                             gboolean is_muted)
826 {
827   EmpathyTpCallPriv *priv = GET_PRIV (call);
828
829   g_return_if_fail (EMPATHY_IS_TP_CALL (call));
830   g_return_if_fail (priv->status != EMPATHY_TP_CALL_STATUS_CLOSED);
831
832   empathy_debug (DEBUG_DOMAIN, "Setting input mute: %d", is_muted);
833
834   emp_cli_stream_engine_call_mute_input (priv->stream_engine, -1,
835       TP_PROXY (priv->channel)->object_path,
836       priv->audio->id, is_muted,
837       tp_call_async_cb,
838       "muting input", NULL,
839       G_OBJECT (call));
840 }
841