]> git.0d.be Git - empathy.git/blob - libempathy/empathy-tp-call.c
Do not use a global variable but a field in the call. Use a boolean since we don...
[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 <libtelepathy/tp-chan-type-streamed-media-gen.h>
25 #include <libtelepathy/tp-connmgr.h>
26 #include <libtelepathy/tp-helpers.h>
27 #include <telepathy-glib/proxy-subclass.h>
28 #include <telepathy-glib/dbus.h>
29
30 #include <libmissioncontrol/mc-account.h>
31
32 #include <extensions/extensions.h>
33 #include <libempathy/empathy-contact-factory.h>
34 #include <libempathy/empathy-debug.h>
35 #include <libempathy/empathy-tp-group.h>
36 #include <libempathy/empathy-utils.h>
37
38 #include "empathy-tp-call.h"
39
40 #define DEBUG_DOMAIN "TpCall"
41
42 #define GET_PRIV(object) (G_TYPE_INSTANCE_GET_PRIVATE \
43     ((object), EMPATHY_TYPE_TP_CALL, EmpathyTpCallPriv))
44
45 #define STREAM_ENGINE_BUS_NAME "org.freedesktop.Telepathy.StreamEngine"
46 #define STREAM_ENGINE_OBJECT_PATH "/org/freedesktop/Telepathy/StreamEngine"
47
48 typedef struct _EmpathyTpCallPriv EmpathyTpCallPriv;
49
50 struct _EmpathyTpCallPriv
51 {
52   TpConn *connection;
53   TpChan *channel;
54   TpProxy *stream_engine;
55   TpDBusDaemon *dbus_daemon;
56   EmpathyTpGroup *group;
57   EmpathyContact *contact;
58   gboolean is_incoming;
59   guint status;
60   gboolean stream_engine_started;
61
62   EmpathyTpCallStream *audio;
63   EmpathyTpCallStream *video;
64 };
65
66 enum
67 {
68   STATUS_CHANGED_SIGNAL,
69   RECEIVING_VIDEO_SIGNAL,
70   SENDING_VIDEO_SIGNAL,
71   LAST_SIGNAL
72 };
73
74 enum
75 {
76   PROP_0,
77   PROP_CONNECTION,
78   PROP_CHANNEL,
79   PROP_CONTACT,
80   PROP_IS_INCOMING,
81   PROP_STATUS,
82   PROP_AUDIO_STREAM,
83   PROP_VIDEO_STREAM
84 };
85
86 static guint signals[LAST_SIGNAL];
87
88 G_DEFINE_TYPE (EmpathyTpCall, empathy_tp_call, G_TYPE_OBJECT)
89
90 static void
91 tp_call_stream_state_changed_cb (DBusGProxy *channel,
92                                  guint stream_id,
93                                  guint stream_state,
94                                  EmpathyTpCall *call)
95 {
96   EmpathyTpCallPriv *priv = GET_PRIV (call);
97
98   empathy_debug (DEBUG_DOMAIN,
99       "Stream state changed - stream id: %d, state state: %d",
100       stream_id, stream_state);
101
102   if (stream_id == priv->audio->id)
103     {
104       priv->audio->state = stream_state;
105     }
106   else if (stream_id == priv->video->id)
107     {
108       priv->video->state = stream_state;
109       if (stream_state == TP_MEDIA_STREAM_STATE_CONNECTED)
110       {
111         if (priv->video->direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE)
112           {
113             empathy_debug (DEBUG_DOMAIN, "RECEIVING");
114             g_signal_emit (call, signals[RECEIVING_VIDEO_SIGNAL], 0, TRUE);
115           }
116         if (priv->video->direction & TP_MEDIA_STREAM_DIRECTION_SEND)
117           {
118             empathy_debug (DEBUG_DOMAIN, "SENDING");
119             g_signal_emit (call, signals[SENDING_VIDEO_SIGNAL], 0, TRUE);
120           }
121       }
122     }
123
124   g_signal_emit (call, signals[STATUS_CHANGED_SIGNAL], 0);
125 }
126
127 static void
128 tp_call_identify_streams (EmpathyTpCall *call)
129 {
130   EmpathyTpCallPriv *priv = GET_PRIV (call);
131   GPtrArray *stream_infos;
132   DBusGProxy *streamed_iface;
133   GError *error = NULL;
134   guint i;
135
136   empathy_debug (DEBUG_DOMAIN, "Identifying audio/video streams");
137
138   streamed_iface = tp_chan_get_interface (priv->channel,
139       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
140
141   if (!tp_chan_type_streamed_media_list_streams (streamed_iface, &stream_infos,
142         &error))
143     {
144       empathy_debug (DEBUG_DOMAIN, "Couldn't list audio/video streams: %s",
145           error->message);
146       g_clear_error (&error);
147       return;
148     }
149
150   for (i = 0; i < stream_infos->len; i++)
151     {
152       GValueArray *values;
153       guint stream_id;
154       guint stream_handle;
155       guint stream_type;
156       guint stream_state;
157       guint stream_direction;
158
159       values = g_ptr_array_index (stream_infos, i);
160       stream_id = g_value_get_uint (g_value_array_get_nth (values, 0));
161       stream_handle = g_value_get_uint (g_value_array_get_nth (values, 1));
162       stream_type = g_value_get_uint (g_value_array_get_nth (values, 2));
163       stream_state = g_value_get_uint (g_value_array_get_nth (values, 3));
164       stream_direction = g_value_get_uint (g_value_array_get_nth (values, 4));
165
166       switch (stream_type)
167         {
168         case TP_MEDIA_STREAM_TYPE_AUDIO:
169           empathy_debug (DEBUG_DOMAIN,
170               "Audio stream - id: %d, state: %d, direction: %d",
171               stream_id, stream_state, stream_direction);
172           priv->audio->exists = TRUE;
173           priv->audio->id = stream_id;
174           priv->audio->state = stream_state;
175           priv->audio->direction = stream_direction;
176           break;
177         case TP_MEDIA_STREAM_TYPE_VIDEO:
178           empathy_debug (DEBUG_DOMAIN,
179               "Video stream - id: %d, state: %d, direction: %d",
180               stream_id, stream_state, stream_direction);
181           priv->video->exists = TRUE;
182           priv->video->id = stream_id;
183           priv->video->state = stream_state;
184           priv->video->direction = stream_direction;
185           break;
186         default:
187           empathy_debug (DEBUG_DOMAIN, "Unknown stream type: %d",
188               stream_type);
189         }
190
191       g_value_array_free (values);
192     }
193 }
194
195 static void
196 tp_call_stream_added_cb (DBusGProxy *channel,
197                          guint stream_id,
198                          guint contact_handle,
199                          guint stream_type,
200                          EmpathyTpCall *call)
201 {
202   empathy_debug (DEBUG_DOMAIN,
203       "Stream added - stream id: %d, contact handle: %d, stream type: %d",
204       stream_id, contact_handle, stream_type);
205
206   tp_call_identify_streams (call);
207 }
208
209
210 static void
211 tp_call_stream_removed_cb (DBusGProxy *channel,
212                            guint stream_id,
213                            EmpathyTpCall *call)
214 {
215   EmpathyTpCallPriv *priv = GET_PRIV (call);
216
217   empathy_debug (DEBUG_DOMAIN, "Stream removed - stream id: %d", stream_id);
218
219   if (stream_id == priv->audio->id)
220     {
221       priv->audio->exists = FALSE;
222     }
223   else if (stream_id == priv->video->id)
224     {
225       priv->video->exists = FALSE;
226     }
227 }
228
229 static void
230 tp_call_channel_closed_cb (TpChan *channel,
231                            EmpathyTpCall *call)
232 {
233   EmpathyTpCallPriv *priv = GET_PRIV (call);
234   DBusGProxy *streamed_iface;
235   DBusGProxy *group_iface;
236
237   empathy_debug (DEBUG_DOMAIN, "Channel closed");
238
239   priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
240   g_signal_emit (call, signals[STATUS_CHANGED_SIGNAL], 0);
241
242   streamed_iface = tp_chan_get_interface (priv->channel,
243       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
244   group_iface = tp_chan_get_interface (priv->channel,
245       TELEPATHY_CHAN_IFACE_GROUP_QUARK);
246
247   dbus_g_proxy_disconnect_signal (DBUS_G_PROXY (priv->channel), "Closed",
248       G_CALLBACK (tp_call_channel_closed_cb), (gpointer) call);
249   dbus_g_proxy_disconnect_signal (streamed_iface, "StreamStateChanged",
250       G_CALLBACK (tp_call_stream_state_changed_cb), (gpointer) call);
251   dbus_g_proxy_disconnect_signal (streamed_iface, "StreamAdded",
252       G_CALLBACK (tp_call_stream_added_cb), (gpointer) call);
253   dbus_g_proxy_disconnect_signal (streamed_iface, "StreamRemoved",
254       G_CALLBACK (tp_call_stream_removed_cb), (gpointer) call);
255 }
256
257 static void
258 tp_call_stream_direction_changed_cb (DBusGProxy *channel,
259                                      guint stream_id,
260                                      guint stream_direction,
261                                      guint flags,
262                                      EmpathyTpCall *call)
263 {
264   EmpathyTpCallPriv *priv = GET_PRIV (call);
265
266   empathy_debug (DEBUG_DOMAIN,
267       "Stream direction changed - stream: %d, direction: %d",
268       stream_id, stream_direction);
269
270   if (stream_id == priv->audio->id)
271     {
272       priv->audio->direction = stream_direction;
273     }
274   else if (stream_id == priv->video->id)
275     {
276       priv->video->direction = stream_direction;
277
278       if (stream_direction & TP_MEDIA_STREAM_DIRECTION_RECEIVE)
279         {
280           empathy_debug (DEBUG_DOMAIN, "RECEIVING");
281           g_signal_emit (call, signals[RECEIVING_VIDEO_SIGNAL], 0, TRUE);
282         }
283       else
284         {
285           empathy_debug (DEBUG_DOMAIN, "NOT RECEIVING");
286           g_signal_emit (call, signals[RECEIVING_VIDEO_SIGNAL], 0, FALSE);
287         }
288
289       if (stream_direction & TP_MEDIA_STREAM_DIRECTION_SEND)
290         {
291           empathy_debug (DEBUG_DOMAIN, "SENDING");
292           g_signal_emit (call, signals[SENDING_VIDEO_SIGNAL], 0, TRUE);
293         }
294       else
295         {
296           empathy_debug (DEBUG_DOMAIN, "NOT SENDING");
297           g_signal_emit (call, signals[SENDING_VIDEO_SIGNAL], 0, FALSE);
298         }
299     }
300 }
301
302 static void
303 tp_call_request_streams_for_capabilities (EmpathyTpCall *call,
304                                           EmpathyCapabilities capabilities)
305 {
306   EmpathyTpCallPriv *priv = GET_PRIV (call);
307   DBusGProxy *streamed_iface;
308   GArray *stream_types;
309   guint handle;
310   guint stream_type;
311   GError *error = NULL;
312
313   empathy_debug (DEBUG_DOMAIN, "Requesting new stream for capabilities %d",
314       capabilities);
315
316   streamed_iface = tp_chan_get_interface (priv->channel,
317       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
318   stream_types = g_array_new (FALSE, FALSE, sizeof (guint));
319   handle = empathy_contact_get_handle (priv->contact);
320
321   if (capabilities & EMPATHY_CAPABILITIES_AUDIO)
322     {
323       stream_type = TP_MEDIA_STREAM_TYPE_AUDIO;
324       g_array_append_val (stream_types, stream_type);
325     }
326   if (capabilities & EMPATHY_CAPABILITIES_VIDEO)
327     {
328       stream_type = TP_MEDIA_STREAM_TYPE_VIDEO;
329       g_array_append_val (stream_types, stream_type);
330     }
331
332   if (!tp_chan_type_streamed_media_request_streams (streamed_iface, handle,
333         stream_types, NULL, &error))
334     {
335       empathy_debug (DEBUG_DOMAIN, "Couldn't request new stream: %s",
336           error->message);
337       g_clear_error (&error);
338     }
339
340   g_array_free (stream_types, TRUE);
341 }
342
343 static void
344 tp_call_request_streams_capabilities_cb (EmpathyContact *contact,
345                                          GParamSpec *property,
346                                          gpointer user_data)
347 {
348   EmpathyTpCall *call = EMPATHY_TP_CALL (user_data);
349
350   g_signal_handlers_disconnect_by_func (contact,
351       tp_call_request_streams_capabilities_cb,
352       user_data);
353
354   tp_call_request_streams_for_capabilities (call,
355      empathy_contact_get_capabilities (contact));
356 }
357
358 static void
359 tp_call_request_streams (EmpathyTpCall *call)
360 {
361   EmpathyTpCallPriv *priv = GET_PRIV (call);
362   EmpathyCapabilities capabilities;
363   DBusGProxy *capabilities_iface;
364
365   empathy_debug (DEBUG_DOMAIN,
366       "Requesting appropriate audio/video streams from contact");
367
368
369   /* FIXME: SIP don't have capabilities interface but we know it supports
370    *        only audio and not video. */
371   capabilities_iface = tp_conn_get_interface (priv->connection,
372       TP_IFACE_QUARK_CONNECTION_INTERFACE_CAPABILITIES);
373   if (!capabilities_iface)
374     {
375       capabilities = EMPATHY_CAPABILITIES_AUDIO;
376     }
377   else
378     {
379       capabilities = empathy_contact_get_capabilities (priv->contact);
380       if (capabilities == EMPATHY_CAPABILITIES_UNKNOWN)
381         {
382           g_signal_connect (priv->contact, "notify::capabilities",
383               G_CALLBACK (tp_call_request_streams_capabilities_cb), call);
384           return;
385         }
386     }
387
388   tp_call_request_streams_for_capabilities (call, capabilities);
389 }
390
391 static void
392 tp_call_is_ready (EmpathyTpCall *call)
393 {
394   EmpathyTpCallPriv *priv = GET_PRIV (call);
395   EmpathyContact *self_contact;
396   GList *members;
397   GList *local_pendings;
398   GList *remote_pendings;
399
400   if (priv->status > EMPATHY_TP_CALL_STATUS_READYING)
401     return;
402
403   members = empathy_tp_group_get_members (priv->group);
404   if (!members)
405     return;
406
407   self_contact = empathy_tp_group_get_self_contact (priv->group);
408   local_pendings = empathy_tp_group_get_local_pendings (priv->group);
409   remote_pendings = empathy_tp_group_get_remote_pendings (priv->group);
410
411   if (local_pendings &&
412       empathy_contact_equal (EMPATHY_CONTACT (((EmpathyPendingInfo *)
413             local_pendings->data)->member), self_contact))
414     {
415       empathy_debug (DEBUG_DOMAIN,
416           "Incoming call is ready - %p",
417           ((EmpathyPendingInfo *) local_pendings->data)->member);
418       priv->is_incoming = TRUE;
419       priv->contact = g_object_ref (members->data);
420     }
421   else if (remote_pendings &&
422       empathy_contact_equal (EMPATHY_CONTACT (members->data), self_contact))
423     {
424       empathy_debug (DEBUG_DOMAIN,
425           "Outgoing call is ready - %p", remote_pendings->data);
426       priv->is_incoming = FALSE;
427       priv->contact = g_object_ref (remote_pendings->data);
428       tp_call_request_streams (call);
429     }
430
431   g_object_unref (self_contact);
432   g_list_foreach (members, (GFunc) g_object_unref, NULL);
433   g_list_free (members);
434   g_list_foreach (local_pendings, (GFunc) empathy_pending_info_free, NULL);
435   g_list_free (local_pendings);
436   g_list_foreach (remote_pendings, (GFunc) g_object_unref, NULL);
437   g_list_free (remote_pendings);
438
439   if (priv->contact)
440     {
441       priv->status = EMPATHY_TP_CALL_STATUS_PENDING;
442       g_signal_emit (call, signals[STATUS_CHANGED_SIGNAL], 0);
443     }
444 }
445
446 static void
447 tp_call_member_added_cb (EmpathyTpGroup *group,
448                          EmpathyContact *contact,
449                          EmpathyContact *actor,
450                          guint reason,
451                          const gchar *message,
452                          EmpathyTpCall *call)
453 {
454   EmpathyTpCallPriv *priv = GET_PRIV (call);
455
456   empathy_debug (DEBUG_DOMAIN, "New member added callback %p", contact);
457   tp_call_is_ready (call);
458
459   if (priv->status == EMPATHY_TP_CALL_STATUS_PENDING)
460     {
461       if ((priv->is_incoming &&
462             !empathy_contact_equal (contact, priv->contact))
463           || (!priv->is_incoming &&
464             empathy_contact_equal (contact, priv->contact)))
465         {
466           priv->status = EMPATHY_TP_CALL_STATUS_ACCEPTED;
467           g_signal_emit (call, signals[STATUS_CHANGED_SIGNAL], 0);
468         }
469     }
470 }
471
472 static void
473 tp_call_local_pending_cb (EmpathyTpGroup *group,
474                           EmpathyContact *contact,
475                           EmpathyContact *actor,
476                           guint reason,
477                           const gchar *message,
478                           EmpathyTpCall *call)
479 {
480   empathy_debug (DEBUG_DOMAIN, "New local pending added callback %p", contact);
481   tp_call_is_ready (call);
482 }
483
484 static void
485 tp_call_remote_pending_cb (EmpathyTpGroup *group,
486                            EmpathyContact *contact,
487                            EmpathyContact *actor,
488                            guint reason,
489                            const gchar *message,
490                            EmpathyTpCall *call)
491 {
492   empathy_debug (DEBUG_DOMAIN, "New remote pending added callback %p", contact);
493   tp_call_is_ready (call);
494 }
495
496 static void
497 tp_call_async_cb (TpProxy *proxy,
498                   const GError *error,
499                   gpointer user_data,
500                   GObject *call)
501 {
502   if (error)
503     {
504       empathy_debug (DEBUG_DOMAIN, "Error %s: %s",
505           user_data, error->message);
506     }
507 }
508
509 static void
510 tp_call_invalidated_cb (TpProxy       *stream_engine,
511                         GQuark         domain,
512                         gint           code,
513                         gchar         *message,
514                         EmpathyTpCall *call)
515 {
516   empathy_debug (DEBUG_DOMAIN, "Stream engine proxy invalidated: %s",
517       message);
518   empathy_tp_call_close_channel (call);
519 }
520
521 static void
522 tp_call_watch_name_owner_cb (TpDBusDaemon *daemon,
523                              const gchar *name,
524                              const gchar *new_owner,
525                              gpointer call)
526 {
527   EmpathyTpCallPriv *priv = GET_PRIV (call);
528
529   /* G_STR_EMPTY(new_owner) means either stream-engine has not started yet or
530    * has crashed. We want to close the channel if stream-engine has crashed.
531    * */
532   empathy_debug (DEBUG_DOMAIN,
533                  "Watch SE: name='%s' SE started='%s' new_owner='%s'",
534                  name, priv->stream_engine_started ? "yes" : "no",
535                  new_owner ? new_owner : "none");
536   if (priv->stream_engine_started && G_STR_EMPTY (new_owner))
537     {
538       empathy_debug (DEBUG_DOMAIN, "Stream engine falled off the bus");
539       empathy_tp_call_close_channel (call);
540     }
541   priv->stream_engine_started = ! G_STR_EMPTY (new_owner);
542 }
543
544 static void
545 tp_call_start_stream_engine (EmpathyTpCall *call)
546 {
547   EmpathyTpCallPriv *priv = GET_PRIV (call);
548
549   empathy_debug (DEBUG_DOMAIN, "Revving up the stream engine");
550
551   priv->stream_engine = g_object_new (TP_TYPE_PROXY,
552       "bus-name", STREAM_ENGINE_BUS_NAME,
553       "dbus-connection", tp_get_bus (),
554       "object-path", STREAM_ENGINE_OBJECT_PATH,
555        NULL);
556   tp_proxy_add_interface_by_id (priv->stream_engine,
557       EMP_IFACE_QUARK_STREAM_ENGINE);
558   tp_proxy_add_interface_by_id (priv->stream_engine,
559       EMP_IFACE_QUARK_CHANNEL_HANDLER);
560
561   g_signal_connect (priv->stream_engine, "invalidated",
562       G_CALLBACK (tp_call_invalidated_cb),
563       call);
564   
565   /* FIXME: dbus daemon should be unique */
566   priv->dbus_daemon = tp_dbus_daemon_new (tp_get_bus ());
567   tp_dbus_daemon_watch_name_owner (priv->dbus_daemon, STREAM_ENGINE_BUS_NAME,
568       tp_call_watch_name_owner_cb,
569       call, NULL);
570
571   emp_cli_channel_handler_call_handle_channel (priv->stream_engine, -1,
572         dbus_g_proxy_get_bus_name (DBUS_G_PROXY (priv->connection)),
573         dbus_g_proxy_get_path (DBUS_G_PROXY (priv->connection)),
574         priv->channel->type,
575         dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel)),
576         priv->channel->handle_type, priv->channel->handle,
577         tp_call_async_cb,
578         "calling handle channel", NULL,
579         G_OBJECT (call));
580 }
581
582 static GObject *
583 tp_call_constructor (GType type,
584                      guint n_construct_params,
585                      GObjectConstructParam *construct_params)
586 {
587   GObject *object;
588   EmpathyTpCall *call;
589   EmpathyTpCallPriv *priv;
590   DBusGProxy *streamed_iface;
591   MissionControl *mc;
592   McAccount *account;
593
594   object = G_OBJECT_CLASS (empathy_tp_call_parent_class)->constructor (type,
595       n_construct_params, construct_params);
596
597   call = EMPATHY_TP_CALL (object);
598   priv = GET_PRIV (call);
599
600   dbus_g_proxy_connect_signal (DBUS_G_PROXY (priv->channel), "Closed",
601      G_CALLBACK (tp_call_channel_closed_cb), (gpointer) call, NULL);
602
603   streamed_iface = tp_chan_get_interface (priv->channel,
604       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
605   dbus_g_proxy_connect_signal (streamed_iface, "StreamStateChanged",
606       G_CALLBACK (tp_call_stream_state_changed_cb),
607       (gpointer) call, NULL);
608   dbus_g_proxy_connect_signal (streamed_iface, "StreamDirectionChanged",
609       G_CALLBACK (tp_call_stream_direction_changed_cb),
610       (gpointer) call, NULL);
611   dbus_g_proxy_connect_signal (streamed_iface, "StreamAdded",
612       G_CALLBACK (tp_call_stream_added_cb), (gpointer) call, NULL);
613   dbus_g_proxy_connect_signal (streamed_iface, "StreamRemoved",
614       G_CALLBACK (tp_call_stream_removed_cb), (gpointer) call, NULL);
615
616   mc = empathy_mission_control_new ();
617   account = mission_control_get_account_for_connection (mc, priv->connection,
618       NULL);
619   priv->group = empathy_tp_group_new (account, priv->channel);
620   g_object_unref (mc);
621
622   g_signal_connect (G_OBJECT (priv->group), "member-added",
623       G_CALLBACK (tp_call_member_added_cb), (gpointer) call);
624   g_signal_connect (G_OBJECT (priv->group), "local-pending",
625       G_CALLBACK (tp_call_local_pending_cb), (gpointer) call);
626   g_signal_connect (G_OBJECT (priv->group), "remote-pending",
627       G_CALLBACK (tp_call_remote_pending_cb), (gpointer) call);
628
629   tp_call_start_stream_engine (call);
630   /* FIXME: unnecessary for outgoing? */
631   tp_call_identify_streams (call);
632
633   return object;
634 }
635
636 static void 
637 tp_call_finalize (GObject *object)
638 {
639   EmpathyTpCallPriv *priv = GET_PRIV (object);
640
641   empathy_debug (DEBUG_DOMAIN, "Finalizing: %p", object);
642
643   g_slice_free (EmpathyTpCallStream, priv->audio);
644   g_slice_free (EmpathyTpCallStream, priv->video);
645   g_object_unref (priv->group);
646
647   if (priv->connection != NULL)
648     g_object_unref (priv->connection);
649
650   if (priv->channel != NULL)
651     g_object_unref (priv->channel);
652
653   if (priv->stream_engine != NULL)
654     g_object_unref (priv->stream_engine);
655
656   if (priv->contact != NULL)
657       g_object_unref (priv->contact);
658
659   if (priv->dbus_daemon != NULL)
660     {
661       tp_dbus_daemon_cancel_name_owner_watch (priv->dbus_daemon,
662           STREAM_ENGINE_BUS_NAME,
663           tp_call_watch_name_owner_cb,
664           object);
665       g_object_unref (priv->dbus_daemon);
666     }
667
668   (G_OBJECT_CLASS (empathy_tp_call_parent_class)->finalize) (object);
669 }
670
671 static void 
672 tp_call_set_property (GObject *object,
673                       guint prop_id,
674                       const GValue *value,
675                       GParamSpec *pspec)
676 {
677   EmpathyTpCallPriv *priv = GET_PRIV (object);
678
679   switch (prop_id)
680     {
681     case PROP_CONNECTION:
682       priv->connection = g_value_dup_object (value);
683       break;
684     case PROP_CHANNEL:
685       priv->channel = g_value_dup_object (value);
686       break;
687     case PROP_CONTACT:
688       /* FIXME should this one be writable in the first place ? */
689       g_assert (priv->contact == NULL);
690       priv->contact = g_value_dup_object (value);
691       break;
692     case PROP_IS_INCOMING:
693       priv->is_incoming = g_value_get_boolean (value);
694       break;
695     case PROP_STATUS:
696       priv->status = g_value_get_uint (value);
697       break;
698     case PROP_AUDIO_STREAM:
699       priv->audio = g_value_get_pointer (value);
700       break;
701     case PROP_VIDEO_STREAM:
702       priv->video = g_value_get_pointer (value);
703       break;
704     default:
705       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
706       break;
707   }
708 }
709
710
711 static void
712 tp_call_get_property (GObject *object,
713                       guint prop_id,
714                       GValue *value,
715                       GParamSpec *pspec)
716 {
717   EmpathyTpCallPriv *priv = GET_PRIV (object);
718
719   switch (prop_id)
720     {
721     case PROP_CONNECTION:
722       g_value_set_object (value, priv->connection);
723       break;
724     case PROP_CHANNEL:
725       g_value_set_object (value, priv->channel);
726       break;
727     case PROP_CONTACT:
728       g_value_set_object (value, priv->contact);
729       break;
730     case PROP_IS_INCOMING:
731       g_value_set_boolean (value, priv->is_incoming);
732       break;
733     case PROP_STATUS:
734       g_value_set_uint (value, priv->status);
735       break;
736     case PROP_AUDIO_STREAM:
737       g_value_set_pointer (value, priv->audio);
738       break;
739     case PROP_VIDEO_STREAM:
740       g_value_set_pointer (value, priv->video);
741       break;
742     default:
743       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
744       break;
745   }
746 }
747
748 static void
749 empathy_tp_call_class_init (EmpathyTpCallClass *klass)
750 {
751   GObjectClass *object_class = G_OBJECT_CLASS (klass);
752
753   emp_cli_init ();
754
755   object_class->constructor = tp_call_constructor;
756   object_class->finalize = tp_call_finalize;
757   object_class->set_property = tp_call_set_property;
758   object_class->get_property = tp_call_get_property;
759
760   g_type_class_add_private (klass, sizeof (EmpathyTpCallPriv));
761
762   signals[STATUS_CHANGED_SIGNAL] =
763       g_signal_new ("status-changed", G_TYPE_FROM_CLASS (klass),
764       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID,
765       G_TYPE_NONE, 0);
766   signals[RECEIVING_VIDEO_SIGNAL] =
767       g_signal_new ("receiving-video", G_TYPE_FROM_CLASS (klass),
768       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
769       G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
770   signals[SENDING_VIDEO_SIGNAL] =
771       g_signal_new ("sending-video", G_TYPE_FROM_CLASS (klass),
772       G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN,
773       G_TYPE_NONE, 1, G_TYPE_BOOLEAN);
774
775   g_object_class_install_property (object_class, PROP_CONNECTION,
776       g_param_spec_object ("connection", "connection", "connection",
777       TELEPATHY_CONN_TYPE,
778       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
779       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
780   g_object_class_install_property (object_class, PROP_CHANNEL,
781       g_param_spec_object ("channel", "channel", "channel",
782       TELEPATHY_CHAN_TYPE,
783       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE |
784       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
785   g_object_class_install_property (object_class, PROP_CONTACT,
786       g_param_spec_object ("contact", "Call contact", "Call contact",
787       EMPATHY_TYPE_CONTACT,
788       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
789   g_object_class_install_property (object_class, PROP_IS_INCOMING,
790       g_param_spec_boolean ("is-incoming", "Is media stream incoming",
791       "Is media stream incoming", FALSE, G_PARAM_READABLE |
792       G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
793   g_object_class_install_property (object_class, PROP_STATUS,
794       g_param_spec_uint ("status", "Call status",
795       "Call status", 0, 255, 0, G_PARAM_READABLE | G_PARAM_STATIC_NICK |
796       G_PARAM_STATIC_BLURB));
797   g_object_class_install_property (object_class, PROP_AUDIO_STREAM,
798       g_param_spec_pointer ("audio-stream", "Audio stream data",
799       "Audio stream data",
800       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
801   g_object_class_install_property (object_class, PROP_VIDEO_STREAM,
802       g_param_spec_pointer ("video-stream", "Video stream data",
803       "Video stream data",
804       G_PARAM_READABLE | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB));
805 }
806
807 static void
808 empathy_tp_call_init (EmpathyTpCall *call)
809 {
810   EmpathyTpCallPriv *priv = GET_PRIV (call);
811
812   priv->status = EMPATHY_TP_CALL_STATUS_READYING;
813   priv->contact = NULL;
814   priv->stream_engine_started = FALSE;
815   priv->audio = g_slice_new0 (EmpathyTpCallStream);
816   priv->video = g_slice_new0 (EmpathyTpCallStream);
817   priv->audio->exists = FALSE;
818   priv->video->exists = FALSE;
819 }
820
821 EmpathyTpCall *
822 empathy_tp_call_new (TpConn *connection, TpChan *channel)
823 {
824   return g_object_new (EMPATHY_TYPE_TP_CALL,
825       "connection", connection,
826       "channel", channel,
827       NULL);
828 }
829
830 void
831 empathy_tp_call_accept_incoming_call (EmpathyTpCall *call)
832 {
833   EmpathyTpCallPriv *priv = GET_PRIV (call);
834   GList *local_pendings;
835
836   empathy_debug (DEBUG_DOMAIN, "Accepting incoming call");
837
838   local_pendings = empathy_tp_group_get_local_pendings (priv->group);
839
840   empathy_tp_group_add_member (priv->group, EMPATHY_CONTACT
841       (((EmpathyPendingInfo *) local_pendings->data)->member), NULL);
842
843   g_list_foreach (local_pendings, (GFunc) empathy_pending_info_free, NULL);
844   g_list_free (local_pendings);
845 }
846
847 void
848 empathy_tp_call_request_video_stream_direction (EmpathyTpCall *call,
849                                                 gboolean is_sending)
850 {
851   EmpathyTpCallPriv *priv = GET_PRIV (call);
852   DBusGProxy *streamed_iface;
853   guint new_direction;
854   GError *error = NULL;
855
856   empathy_debug (DEBUG_DOMAIN,
857       "Requesting video stream direction - is_sending: %d", is_sending);
858
859   if (!priv->video->exists)
860     {
861       tp_call_request_streams_for_capabilities (call, EMPATHY_CAPABILITIES_VIDEO);
862       return;
863     }
864
865   streamed_iface = tp_chan_get_interface (priv->channel,
866       TELEPATHY_CHAN_IFACE_STREAMED_QUARK);
867
868   if (is_sending)
869     {
870       new_direction = priv->video->direction | TP_MEDIA_STREAM_DIRECTION_SEND;
871     }
872   else
873     {
874       new_direction = priv->video->direction & ~TP_MEDIA_STREAM_DIRECTION_SEND;
875     }
876
877   if (!tp_chan_type_streamed_media_request_stream_direction (streamed_iface,
878         priv->video->id, new_direction, &error))
879     {
880       empathy_debug (DEBUG_DOMAIN,
881           "Couldn't request video stream direction: %s", error->message);
882       g_clear_error (&error);
883     }
884 }
885
886 void
887 empathy_tp_call_close_channel (EmpathyTpCall *call)
888 {
889   EmpathyTpCallPriv *priv = GET_PRIV (call);
890   GError *error = NULL;
891
892   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
893       return;
894
895   empathy_debug (DEBUG_DOMAIN, "Closing channel");
896
897   if (!tp_chan_close (DBUS_G_PROXY (priv->channel), &error))
898     {
899       empathy_debug (DEBUG_DOMAIN, "Error closing channel: %s",
900           error ? error->message : "No error given");
901       g_clear_error (&error);
902     }
903   else
904         priv->status = EMPATHY_TP_CALL_STATUS_CLOSED;
905 }
906
907 void
908 empathy_tp_call_add_preview_video (EmpathyTpCall *call,
909                                    guint preview_video_socket_id)
910 {
911   EmpathyTpCallPriv *priv = GET_PRIV (call);
912
913   empathy_debug (DEBUG_DOMAIN, "Adding preview video");
914
915   emp_cli_stream_engine_call_add_preview_window (priv->stream_engine, -1,
916       preview_video_socket_id,
917       tp_call_async_cb,
918       "adding preview window", NULL,
919       G_OBJECT (call));
920 }
921
922 void
923 empathy_tp_call_remove_preview_video (EmpathyTpCall *call,
924                                       guint preview_video_socket_id)
925 {
926   EmpathyTpCallPriv *priv = GET_PRIV (call);
927
928   empathy_debug (DEBUG_DOMAIN, "Removing preview video");
929
930   emp_cli_stream_engine_call_remove_preview_window (priv->stream_engine, -1,
931       preview_video_socket_id,
932       tp_call_async_cb,
933       "removing preview window", NULL,
934       G_OBJECT (call));
935 }
936
937 void
938 empathy_tp_call_add_output_video (EmpathyTpCall *call,
939                                   guint output_video_socket_id)
940 {
941   EmpathyTpCallPriv *priv = GET_PRIV (call);
942
943   empathy_debug (DEBUG_DOMAIN, "Adding output video - socket: %d",
944       output_video_socket_id);
945
946   emp_cli_stream_engine_call_set_output_window (priv->stream_engine, -1,
947       dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel)),
948       priv->video->id, output_video_socket_id,
949       tp_call_async_cb,
950       "setting output window", NULL,
951       G_OBJECT (call));
952 }
953
954 void
955 empathy_tp_call_set_output_volume (EmpathyTpCall *call,
956                                    guint volume)
957 {
958   EmpathyTpCallPriv *priv = GET_PRIV (call);
959
960   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
961     return;
962
963   empathy_debug (DEBUG_DOMAIN, "Setting output volume: %d", volume);
964
965   emp_cli_stream_engine_call_set_output_volume (priv->stream_engine, -1,
966       dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel)),
967       priv->audio->id, volume,
968       tp_call_async_cb,
969       "setting output volume", NULL,
970       G_OBJECT (call));
971 }
972
973 void
974 empathy_tp_call_mute_output (EmpathyTpCall *call,
975                              gboolean is_muted)
976 {
977   EmpathyTpCallPriv *priv = GET_PRIV (call);
978
979   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
980     return;
981
982   empathy_debug (DEBUG_DOMAIN, "Setting output mute: %d", is_muted);
983
984   emp_cli_stream_engine_call_mute_output (priv->stream_engine, -1,
985       dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel)),
986       priv->audio->id, is_muted,
987       tp_call_async_cb,
988       "muting output", NULL,
989       G_OBJECT (call));
990 }
991
992 void
993 empathy_tp_call_mute_input (EmpathyTpCall *call,
994                             gboolean is_muted)
995 {
996   EmpathyTpCallPriv *priv = GET_PRIV (call);
997
998   if (priv->status == EMPATHY_TP_CALL_STATUS_CLOSED)
999     return;
1000
1001   empathy_debug (DEBUG_DOMAIN, "Setting input mute: %d", is_muted);
1002
1003   emp_cli_stream_engine_call_mute_input (priv->stream_engine, -1,
1004       dbus_g_proxy_get_path (DBUS_G_PROXY (priv->channel)),
1005       priv->audio->id, is_muted,
1006       tp_call_async_cb,
1007       "muting input", NULL,
1008       G_OBJECT (call));
1009 }
1010