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