summaryrefslogtreecommitdiff
path: root/telepathy-farstream/content.c
blob: a088c18c7b5bc011d602975734d739797b3ec5a1 (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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550

#include "content.h"
#include "content-priv.h"

#include <farstream/fs-conference.h>

#include "channel.h"


/**
 * SECTION:content
 * @short_description: Represent the Content of a channel handled by #TfChannel
 *
 * Objects of this class allow the user to handle the media side of a Telepathy
 * channel handled by #TfChannel.
 *
 * This object is created by the #TfChannel and the user is notified
 * of its creation by the #TfChannel::content-added signal. In the
 * callback for this signal, the user should connect to the
 * #TfContent::src-pad-added signal.
 *
 */


G_DEFINE_ABSTRACT_TYPE (TfContent, tf_content, G_TYPE_OBJECT);


enum
{
  PROP_TF_CHANNEL = 1,
  PROP_FS_CONFERENCE,
  PROP_FS_SESSION,
  PROP_MEDIA_TYPE,
  PROP_SINK_PAD,
  PROP_OBJECT_PATH
};

enum
{
  SIGNAL_START_SENDING,
  SIGNAL_STOP_SENDING,
  SIGNAL_SRC_PAD_ADDED,
  SIGNAL_START_RECEIVING,
  SIGNAL_STOP_RECEIVING,
  SIGNAL_RESTART_SOURCE,
  SIGNAL_COUNT
};

static guint signals[SIGNAL_COUNT] = {0};

static void
tf_content_get_property (GObject    *object,
    guint       property_id,
    GValue     *value,
    GParamSpec *pspec)
{
  switch (property_id)
    {
      /* Other properties need to be overwritten */
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
tf_content_class_init (TfContentClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->get_property = tf_content_get_property;

  g_object_class_install_property (object_class, PROP_TF_CHANNEL,
      g_param_spec_object ("tf-channel",
          "Parent TfChannel object ",
          "The Telepathy-Farstream Channel for this object",
          TF_TYPE_CHANNEL,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (object_class, PROP_FS_CONFERENCE,
      g_param_spec_object ("fs-conference",
          "Farstream FsConference used by the Content ",
          "The Farstream conference for this content "
          "(could be the same as other contents)",
          FS_TYPE_CONFERENCE,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (object_class, PROP_FS_SESSION,
      g_param_spec_object ("fs-session",
          "Farstream FsSession ",
          "The Farstream session for this content",
          FS_TYPE_SESSION,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (object_class, PROP_SINK_PAD,
      g_param_spec_object ("sink-pad",
          "Sink Pad",
          "Sink GstPad for this content",
          GST_TYPE_PAD,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (object_class, PROP_MEDIA_TYPE,
      g_param_spec_enum ("media-type",
          "MediaType",
          "The FsMediaType for this content",
          FS_TYPE_MEDIA_TYPE,
          0,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));

  g_object_class_install_property (object_class, PROP_OBJECT_PATH,
      g_param_spec_string ("object-path",
          "content object path",
          "D-Bus object path of the Telepathy content which this content"
          " operates on",
          NULL,
          G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));


  /**
   * TfContent::start-sending:
   * @content: the #TfContent
   *
   * This signal is emitted when the connection manager ask to send media.
   * For example, this can be used to open a camera, start recording from a
   * microphone or play back a file. The application should start
   * sending data on the #TfContent:sink-pad
   *
   * Returns: %TRUE if the application can start providing data or %FALSE
   * otherwise
   */

  signals[SIGNAL_START_SENDING] =
      g_signal_new ("start-sending",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0,
          g_signal_accumulator_true_handled, NULL, NULL,
          G_TYPE_BOOLEAN, 0);

  /**
   * TfContent::stop-sending:
   * @content: the #TfContent
   *
   * This signal is emitted when the connection manager ask to stop
   * sending media
   */

  signals[SIGNAL_STOP_SENDING] =
      g_signal_new ("stop-sending",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0, NULL, NULL, NULL,
          G_TYPE_NONE, 0);

  /**
   * TfContent::src-pad-added:
   * @content: the #TfContent
   * @handle: the handle of the remote party producing the content on this pad
   *    or 0 if unknown
   * @stream: the #FsStream for this pad
   * @pad: a #GstPad
   * @codec: the #FsCodec for this pad
   *
   * This signal is emitted when a data is coming on a new pad. This signal
   * is not emitted on the main thread, so special care must be made to lock
   * the relevant data. When the callback returns from this signal, data will
   * start flowing through the pad, so the application MUST connect a sink.
   */

  signals[SIGNAL_SRC_PAD_ADDED] =
      g_signal_new ("src-pad-added",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0, NULL, NULL, NULL,
          G_TYPE_NONE, 4,
          G_TYPE_UINT, FS_TYPE_STREAM, GST_TYPE_PAD, FS_TYPE_CODEC);

  /**
   * TfContent::start-receiving:
   * @content: the #TfContent
   * @handles: a 0-terminated array of #guint containing the handles
   * @handle_count: The number of handles in the @handles array
   *
   * This signal is emitted when the connection managers requests that the
   * application prepares itself to start receiving data again from certain
   * handles.
   *
   * This signal will only be emitted after the #TfContent::stop-receiving
   * signal has succeeded. It will not be emitted right after
   *  #TfContent::src-pad-added.
   *
   * Returns: %TRUE if the application can start receiving data or %FALSE
   * otherwise
   */

  signals[SIGNAL_START_RECEIVING] =
      g_signal_new ("start-receiving",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0,
          g_signal_accumulator_true_handled, NULL, NULL,
          G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_UINT);

  /**
   * TfContent::stop-receiving:
   * @content: the #TfContent
   * @handles: a 0-terminated array of #guint containing the handles
   * @handle_count: The number of handles in the @handles array
   *
   * This signal is emitted when the connection manager wants to tell the
   * application that it is now allowed to stop receiving.
   */

  signals[SIGNAL_STOP_RECEIVING] =
      g_signal_new ("stop-receiving",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0, NULL, NULL, NULL,
          G_TYPE_NONE, 2, G_TYPE_POINTER, G_TYPE_UINT);

  /**
   * TfContent::restart-source:
   * @content: the #TfContent
   *
   * This signal requests that the source be restarted so that the caps can
   * be renegotiated with a new resolutions and framerate.
   */

  signals[SIGNAL_RESTART_SOURCE] =
      g_signal_new ("restart-source",
          G_OBJECT_CLASS_TYPE (klass),
          G_SIGNAL_RUN_LAST,
          0, NULL, NULL, NULL,
          G_TYPE_NONE, 0);
}


static void
tf_content_init (TfContent *self)
{
}


gboolean
_tf_content_start_sending (TfContent *self)
{
  GValue instance = {0};
  GValue sending_success_val = {0,};
  gboolean sending_success;


  if (self->sending_count)
    {
      self->sending_count ++;
      return TRUE;
    }

  g_value_init (&sending_success_val, G_TYPE_BOOLEAN);
  g_value_set_boolean (&sending_success_val, TRUE);

  g_value_init (&instance, TF_TYPE_CONTENT);
  g_value_set_object (&instance, self);

  g_debug ("Requesting that the application start sending");

  g_signal_emitv (&instance, signals[SIGNAL_START_SENDING], 0,
      &sending_success_val);
  sending_success = g_value_get_boolean (&sending_success_val);

  g_value_unset (&instance);

  g_debug ("Request to start sending %s",
      sending_success ? "succeeded" : "failed");

  self->sending_count = 1;

  return sending_success;
}

void
_tf_content_stop_sending (TfContent *self)
{
  self->sending_count --;

  if (self->sending_count == 0)
    {
      g_signal_emit (self, signals[SIGNAL_STOP_SENDING], 0);
    }
}


void
_tf_content_emit_src_pad_added (TfContent *self, guint handle,
    FsStream *stream, GstPad *pad, FsCodec *codec)
{
  g_signal_emit (self, signals[SIGNAL_SRC_PAD_ADDED], 0, handle,
      stream, pad, codec);
}

/**
 * tf_content_error_literal:
 * @content: a #TfContent
 * @message: error Message
 *
 * Send a fatal streaming error to the Content to the CM, the effect is most
 * likely that the content will be removed.
 *
 * Rename to: tf_content_error
 */

void
tf_content_error_literal (TfContent *content,
    const gchar *message)
{
   TfContentClass *klass = TF_CONTENT_GET_CLASS (content);

   g_return_if_fail (content != NULL);
   g_return_if_fail (message != NULL);

   if (klass->content_error)
     klass->content_error (content, message);
   else
     GST_WARNING ("content_error not defined in class: %s", message);
}

/**
 * tf_content_error:
 * @content: a #TfContent
 * @message_format: error Message with printf style formatting
 * @...:  Parameters to insert into the @message_format string
 *
 * Send a fatal streaming error to the Content to the CM, the effect is most
 * likely that the content will be removed.
 */

void
tf_content_error (TfContent *content,
    const gchar *message_format,
    ...)
{
  gchar *message;
  va_list valist;

  g_return_if_fail (content != NULL);
  g_return_if_fail (message_format != NULL);

  va_start (valist, message_format);
  message = g_strdup_vprintf (message_format, valist);
  va_end (valist);

  tf_content_error_literal (content, message);
  g_free (message);
}

/**
 * tf_content_iterate_src_pads:
 * @content: a #TfContent
 * @handles: a 0 terminated array of #guint representing Telepathy handles
 * @handle_count: the numner of handles in @handles
 *
 * Provides a iterator that can be used to iterate through all of the src
 * pads that are are used to receive from a group of Telepathy handles.
 *
 * Returns: a #GstIterator
 */

GstIterator *
tf_content_iterate_src_pads (TfContent *content, guint *handles,
    guint handle_count)
{
  TfContentClass *klass = TF_CONTENT_GET_CLASS (content);

  g_return_val_if_fail (content != NULL, NULL);

  if (klass->iterate_src_pads)
    return klass->iterate_src_pads (content, handles, handle_count);
  else
    GST_WARNING ("iterate_src_pads not defined in class");

  return NULL;
}

gboolean
_tf_content_start_receiving (TfContent *self, guint *handles,
    guint handle_count)
{
  GValue instance_and_params[3] = {{0} , {0}, {0}};
  GValue receiving_success_val = {0,};
  gboolean receiving_success;

  g_value_init (&receiving_success_val, G_TYPE_BOOLEAN);
  g_value_set_boolean (&receiving_success_val, TRUE);

  g_value_init (&instance_and_params[0], TF_TYPE_CONTENT);
  g_value_set_object (&instance_and_params[0], self);

  g_value_init (&instance_and_params[1], G_TYPE_POINTER);
  g_value_set_pointer (&instance_and_params[1], handles);

  g_value_init (&instance_and_params[2], G_TYPE_UINT);
  g_value_set_uint (&instance_and_params[2], handle_count);

  g_debug ("Requesting that the application start receiving");

  g_signal_emitv (instance_and_params, signals[SIGNAL_START_RECEIVING], 0,
      &receiving_success_val);
  receiving_success = g_value_get_boolean (&receiving_success_val);

  g_value_unset (&instance_and_params[0]);

  g_debug ("Request to start receiving %s",
      receiving_success ? "succeeded" : "failed");

  return receiving_success;
}

void
_tf_content_stop_receiving (TfContent *self, guint *handles,
    guint handle_count)
{
  g_debug ("Requesting that the application stop receiving");
  g_signal_emit (self, signals[SIGNAL_STOP_RECEIVING], 0, handles,
      handle_count);
}


/**
 * tf_content_sending_failed_literal:
 * @content: a #TfContent
 * @message: The error message
 *
 * Informs the Connection Manager that sending has failed for this
 * content. This is a transient error and it may or not not end the Content
 * and the call.
 *
 * Rename to: tf_content_sending_failed
 */

void
tf_content_sending_failed_literal (TfContent *content,
    const gchar *message)
{
  TfContentClass *klass = TF_CONTENT_GET_CLASS (content);

  g_return_if_fail (content != NULL);
  g_return_if_fail (message != NULL);

   if (klass->content_error)
     klass->sending_failed (content, message);
   else
     GST_WARNING ("sending_failed not defined in class, ignoring error: %s",
         message);
}

/**
 * tf_content_sending_failed:
 * @content: a #TfContent
 * @message_format: Message with printf style formatting
 * @...:  Parameters to insert into the @message_format string
 *
 * Informs the Connection Manager that sending has failed for this
 * content. This is a transient error and it may or not not end the Content
 * and the call.
 */

void
tf_content_sending_failed (TfContent *content,
    const gchar *message_format, ...)
{
  gchar *message;
  va_list valist;

  g_return_if_fail (content != NULL);
  g_return_if_fail (message_format != NULL);

  va_start (valist, message_format);
  message = g_strdup_vprintf (message_format, valist);
  va_end (valist);

  tf_content_sending_failed_literal (content, message);
  g_free (message);
}

/**
 * tf_content_receiving_failed_literal:
 * @content: a #TfContent
 * @handles: an array of #guint representing Telepathy handles, may be %NULL
 * @handle_count: the numner of handles in @handles
 * @message: The error message
 *
 * Informs the Connection Manager that receiving has failed for this
 * content. This is a transient error and it may or not not end the Content
 * and the call.
 *
 * If handles are not specific, it assumes that it is valid for all handles.
 *
 * Rename to: tf_content_receiving_failed
 */

void
tf_content_receiving_failed_literal (TfContent *content,
    guint *handles, guint handle_count,
    const gchar *message)
{
  TfContentClass *klass = TF_CONTENT_GET_CLASS (content);

  g_return_if_fail (content != NULL);
  g_return_if_fail (message != NULL);

  if (klass->content_error)
    klass->receiving_failed (content, handles, handle_count, message);
  else
    GST_WARNING ("receiving_failed not defined in class, ignoring error: %s",
        message);
}


/**
 * tf_content_receiving_failed:
 * @content: a #TfContent
 * @handles: an array of #guint representing Telepathy handles, may be %NULL
 * @handle_count: the numner of handles in @handles
 * @message_format: Message with printf style formatting
 * @...:  Parameters to insert into the @message_format string
 *
 * Informs the Connection Manager that receiving has failed for this
 * content. This is a transient error and it may or not not end the Content
 * and the call.
 *
 * If handles are not specific, it assumes that it is valid for all handles.
 */

void
tf_content_receiving_failed (TfContent *content,
    guint *handles, guint handle_count,
    const gchar *message_format, ...)
{
  gchar *message;
  va_list valist;

  g_return_if_fail (content != NULL);
  g_return_if_fail (message_format != NULL);

  va_start (valist, message_format);
  message = g_strdup_vprintf (message_format, valist);
  va_end (valist);

  tf_content_receiving_failed_literal (content, handles, handle_count, message);
  g_free (message);
}