summaryrefslogtreecommitdiff
path: root/src/call-member-content.c
blob: 4500de14ccb89c95a6bece068553d74464e6f4ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * call-member-content.c - Source for GabbleCallMemberContent
 * Copyright (C) 2010 Collabora Ltd.
 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>

#include "call-member.h"
#include "call-member-content.h"
#include "util.h"
#include "namespaces.h"

#define DEBUG_FLAG GABBLE_DEBUG_MEDIA
#include "debug.h"

G_DEFINE_TYPE(GabbleCallMemberContent,
  gabble_call_member_content, G_TYPE_OBJECT)


/* properties */
enum {
  PROP_JINGLE_CONTENT = 1,
  PROP_CONTENT_NAME,
  PROP_MEDIA_TYPE,
  PROP_MEMBER
};

/* signal enum */
enum
{
    CODECS_CHANGED,
    GOT_JINGLE_CONTENT,
    REMOVED,
    LAST_SIGNAL
};

static guint signals[LAST_SIGNAL] = {0};

/* private structure */
struct _GabbleCallMemberContentPrivate
{
  gboolean dispose_has_run;

  GabbleCallMember *member;

  WockyJingleContent *jingle_content;
  gchar *name;
  WockyJingleMediaType media_type;

  GList *remote_codecs;
  gboolean removed;
};

#define GABBLE_CALL_MEMBER_CONTENT_GET_PRIVATE(o)  \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), GABBLE_TYPE_CALL_MEMBER_CONTENT, \
    GabbleCallMemberContentPrivate))

static void
gabble_call_member_content_init (GabbleCallMemberContent *self)
{
  GabbleCallMemberContentPrivate *priv =
    GABBLE_CALL_MEMBER_CONTENT_GET_PRIVATE (self);

  self->priv = priv;
}

static void
gabble_call_member_content_get_property (GObject    *object,
    guint       property_id,
    GValue     *value,
    GParamSpec *pspec)
{
  GabbleCallMemberContent *self = GABBLE_CALL_MEMBER_CONTENT (object);
  GabbleCallMemberContentPrivate *priv = self->priv;

  switch (property_id)
    {
      case PROP_MEMBER:
        g_value_set_object (value, priv->member);
        break;
      case PROP_JINGLE_CONTENT:
        g_value_set_object (value, priv->jingle_content);
        break;
      case PROP_CONTENT_NAME:
        g_value_set_string (value, priv->name);
        break;
      case PROP_MEDIA_TYPE:
        g_value_set_uint (value, priv->media_type);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
    }
}

static void
gabble_call_member_content_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  GabbleCallMemberContent *self = GABBLE_CALL_MEMBER_CONTENT (object);
  GabbleCallMemberContentPrivate *priv = self->priv;

  switch (property_id)
    {
      case PROP_JINGLE_CONTENT:
        gabble_call_member_content_set_jingle_content (self,
          g_value_get_object (value));
        break;
      case PROP_MEMBER:
        priv->member = g_value_get_object (value);
        break;
      case PROP_CONTENT_NAME:
        priv->name = g_value_dup_string (value);
        g_assert (priv->name != NULL);
        break;
      case PROP_MEDIA_TYPE:
        priv->media_type = g_value_get_uint (value);
        g_assert (priv->media_type != WOCKY_JINGLE_MEDIA_TYPE_NONE);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
        break;
  }
}

static void gabble_call_member_content_dispose (GObject *object);
static void gabble_call_member_content_finalize (GObject *object);

void
gabble_call_member_content_add_to_session (GabbleCallMemberContent *self)
{
  GabbleCallMemberContentPrivate *priv = self->priv;
  const gchar *content_ns;
  WockyJingleSession *session;
  WockyJingleContent *content;
  const gchar *peer_resource;
  const gchar *transport_ns;


  if (priv->jingle_content != NULL)
    return;

  DEBUG ("Session set for: %s (current jingle %p)",
    priv->name, priv->jingle_content);

  session = gabble_call_member_get_session (priv->member);
  transport_ns = gabble_call_member_get_transport_ns (priv->member);
  content_ns = NS_JINGLE_RTP;

  g_assert (session != NULL);

  peer_resource = wocky_jingle_session_get_peer_resource (session);

  if (peer_resource != NULL)
    DEBUG ("existing call, using peer resource %s", peer_resource);
  else
    DEBUG ("existing call, using bare JID");

  DEBUG ("Creating new jingle content with ns %s : %s",
    content_ns, transport_ns);

  content = wocky_jingle_session_add_content (session,
      priv->media_type, WOCKY_JINGLE_CONTENT_SENDERS_BOTH,
      priv->name, content_ns, transport_ns);

  gabble_call_member_content_set_jingle_content (self, content);
}

static void
member_got_session_cb (GabbleCallMember *member,
  GParamSpec *param, gpointer user_data)
{
  gabble_call_member_content_add_to_session (
      GABBLE_CALL_MEMBER_CONTENT (user_data));
}

static void
gabble_call_member_content_constructed (GObject *obj)
{
  GabbleCallMemberContent *self = GABBLE_CALL_MEMBER_CONTENT (obj);
  GabbleCallMemberContentPrivate *priv = self->priv;

  gabble_signal_connect_weak (priv->member, "notify::session",
    G_CALLBACK (member_got_session_cb), G_OBJECT (self));

  if (G_OBJECT_CLASS (gabble_call_member_content_parent_class)->constructed
      != NULL)
    G_OBJECT_CLASS (
      gabble_call_member_content_parent_class)->constructed (obj);
}

static void
gabble_call_member_content_class_init (
  GabbleCallMemberContentClass *gabble_call_member_content_class)
{
  GObjectClass *object_class =
      G_OBJECT_CLASS (gabble_call_member_content_class);
  GParamSpec *param_spec;

  g_type_class_add_private (gabble_call_member_content_class,
    sizeof (GabbleCallMemberContentPrivate));

  object_class->dispose = gabble_call_member_content_dispose;
  object_class->finalize = gabble_call_member_content_finalize;

  object_class->get_property = gabble_call_member_content_get_property;
  object_class->set_property = gabble_call_member_content_set_property;
  object_class->constructed = gabble_call_member_content_constructed;

  param_spec = g_param_spec_string ("name", "Name",
      "The name of this jingle content",
      NULL,
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_CONTENT_NAME,
    param_spec);

  param_spec = g_param_spec_uint ("media-type", "MediaType",
      "The media type of this jingle content",
      WOCKY_JINGLE_MEDIA_TYPE_NONE,
      WOCKY_JINGLE_MEDIA_TYPE_VIDEO,
      WOCKY_JINGLE_MEDIA_TYPE_NONE,
      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_MEDIA_TYPE,
    param_spec);

  param_spec = g_param_spec_object ("jingle-content", "JingleContent",
      "The jingle content corresponding to this members content",
      WOCKY_TYPE_JINGLE_CONTENT,
      G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_JINGLE_CONTENT,
    param_spec);

  param_spec = g_param_spec_object ("member", "CallMember",
      "The call member that has this as a content",
      GABBLE_TYPE_CALL_MEMBER,
      G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
  g_object_class_install_property (object_class, PROP_MEMBER,
    param_spec);

  signals[CODECS_CHANGED] = g_signal_new ("codecs-changed",
      G_OBJECT_CLASS_TYPE (gabble_call_member_content_class),
      G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
      0,
      NULL, NULL,
      g_cclosure_marshal_VOID__VOID,
      G_TYPE_NONE, 0);

  signals[GOT_JINGLE_CONTENT] = g_signal_new ("got-jingle-content",
      G_OBJECT_CLASS_TYPE (gabble_call_member_content_class),
      G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
      0,
      NULL, NULL,
      g_cclosure_marshal_VOID__VOID,
      G_TYPE_NONE, 0);

  signals[REMOVED] = g_signal_new ("removed",
      G_OBJECT_CLASS_TYPE (gabble_call_member_content_class),
      G_SIGNAL_RUN_LAST | G_SIGNAL_DETAILED,
      0,
      NULL, NULL,
      g_cclosure_marshal_VOID__VOID,
      G_TYPE_NONE, 0);
}

void
gabble_call_member_content_dispose (GObject *object)
{
  GabbleCallMemberContent *self = GABBLE_CALL_MEMBER_CONTENT (object);
  GabbleCallMemberContentPrivate *priv = self->priv;

  if (priv->dispose_has_run)
    return;

  priv->dispose_has_run = TRUE;

  tp_clear_object (&priv->jingle_content);

  if (G_OBJECT_CLASS (gabble_call_member_content_parent_class)->dispose)
    G_OBJECT_CLASS (gabble_call_member_content_parent_class)->dispose (object);
}

void
gabble_call_member_content_finalize (GObject *object)
{
  GabbleCallMemberContent *self = GABBLE_CALL_MEMBER_CONTENT (object);
  GabbleCallMemberContentPrivate *priv = self->priv;

  g_free (priv->name);

  G_OBJECT_CLASS (gabble_call_member_content_parent_class)->finalize (object);
}

WockyJingleMediaType
gabble_call_member_content_get_media_type (GabbleCallMemberContent *self)
{
  return self->priv->media_type;
}

const gchar *
gabble_call_member_content_get_name (GabbleCallMemberContent *self)
{
  return self->priv->name;
}

WockyJingleContent *
gabble_call_member_content_get_jingle_content (
    GabbleCallMemberContent *self)
{
  return self->priv->jingle_content;
}

GabbleCallMemberContent *
gabble_call_member_content_new (const gchar *name,
    WockyJingleMediaType type,
    GabbleCallMember *member)
{
  return GABBLE_CALL_MEMBER_CONTENT (g_object_new (
    GABBLE_TYPE_CALL_MEMBER_CONTENT,
    "name", name,
    "media-type", type,
    "member", member,
    NULL));
}

static void
call_member_content_jingle_removed_cb (WockyJingleContent *jingle_content,
    GabbleCallMemberContent *content)
{
  if (!content->priv->removed)
    {
      content->priv->removed = TRUE;
      g_signal_emit (content, signals[REMOVED], 0);
    }
}

static void
call_member_content_jingle_media_description_cb (WockyJingleMediaRtp *media,
    WockyJingleMediaDescription *md,
    gpointer user_data)
{
  GabbleCallMemberContent *self = GABBLE_CALL_MEMBER_CONTENT (user_data);

  DEBUG ("New codecs from jingle");

  g_signal_emit (self, signals[CODECS_CHANGED], 0);
}

GabbleCallMemberContent *
gabble_call_member_content_from_jingle_content (
  WockyJingleContent *jingle_content,
  GabbleCallMember *member)
{
  GabbleCallMemberContent *content;
  gchar *name;
  WockyJingleMediaType mtype;

  g_object_get (jingle_content,
    "name", &name,
    "media-type", &mtype,
    NULL);

  content = gabble_call_member_content_new (name, mtype, member);

  gabble_call_member_content_set_jingle_content (content, jingle_content);

  g_free (name);

  return content;
}

gboolean
gabble_call_member_content_has_jingle_content (
    GabbleCallMemberContent *self)
{
  return self->priv->jingle_content != NULL;
}

GList *
gabble_call_member_content_get_remote_codecs (GabbleCallMemberContent *self)
{
  GList *jcodecs = NULL;

  if (self->priv->jingle_content != NULL)
    {
      WockyJingleMediaDescription *md;
      md = wocky_jingle_media_rtp_get_remote_media_description (
          WOCKY_JINGLE_MEDIA_RTP (self->priv->jingle_content));
      if (md != NULL)
        jcodecs = md->codecs;
    }

  return jcodecs != NULL ? jcodecs : self->priv->remote_codecs;
}

void
gabble_call_member_content_set_remote_codecs (GabbleCallMemberContent *self,
    GList *codecs)
{
  GabbleCallMemberContentPrivate *priv = self->priv;

  DEBUG ("New codecs set directly on the member");

  if (priv->remote_codecs != NULL)
    {
      GList *changed = NULL;

      if (!jingle_media_rtp_compare_codecs (priv->remote_codecs, codecs,
            &changed, NULL) || changed == NULL)
        return;

      g_list_free (changed);
    }

  jingle_media_rtp_free_codecs (priv->remote_codecs);
  priv->remote_codecs = codecs;


  g_signal_emit (self, signals[CODECS_CHANGED], 0);
}

GabbleCallMember *
gabble_call_member_content_get_member (GabbleCallMemberContent *self)
{
  return self->priv->member;
}

void
gabble_call_member_content_set_jingle_content (GabbleCallMemberContent *self,
    WockyJingleContent *content)
{
  g_assert (self->priv->jingle_content == NULL);

  if (content == NULL)
    return;

  self->priv->jingle_content = g_object_ref (content);

  gabble_signal_connect_weak (content, "removed",
      G_CALLBACK (call_member_content_jingle_removed_cb), G_OBJECT (self));
  gabble_signal_connect_weak (content, "remote-media-description",
    G_CALLBACK (call_member_content_jingle_media_description_cb),
      G_OBJECT (self));

  g_signal_emit (self, signals[GOT_JINGLE_CONTENT], 0);
}

void
gabble_call_member_content_remove (GabbleCallMemberContent *self)
{
  GabbleCallMemberContentPrivate *priv = self->priv;

  if (priv->removed)
    return;

  priv->removed = TRUE;

  g_object_ref (self);
  /* Remove ourselves from the sesison */
  if (priv->jingle_content != NULL)
      wocky_jingle_session_remove_content (priv->jingle_content->session,
          priv->jingle_content);

  g_signal_emit (self, signals[REMOVED], 0);
  g_object_unref (self);
}