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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
|
/*
* Farsight2 - Farsight Multicast UDP Stream Transmitter
*
* Copyright 2007 Collabora Ltd.
* @author: Olivier Crete <olivier.crete@collabora.co.uk>
* Copyright 2007 Nokia Corp.
*
* fs-multicast-stream-transmitter.c - A Farsight Multiast UDP stream transmitter
*
* 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* SECTION:fs-multicast-stream-transmitter
* @short_description: A stream transmitter object for Multicast UDP
* @see_also: #FsRawUdpStreamTransmitter
*
* The multicast transmitter allows data to be sent over and received from
* multicasted UDP on IPv4.
*
* This stream transmitter never emits local candidates. It will listen
* to the port specified in the remote candidate. And will also send to that
* port. It accepts only a single remote candidate per component, if a new one
* is given, it will replace the previous one for that component.
*
* The transmitter will only stop sending to a multicast group when all of its
* StreamTransmitters that have this multicast group as destination have their
* "sending" property set to false. Multiple stream transmitters can point to
* the same multicast groups from the same Transmitter (session), and only one
* copy of each packet will be received.
*
* It will only listen to and send from the IP specified in the
* prefered-local-candidates. There can be only one preferred candidate per
* component. Only the component_id and the ip will be used from the preferred
* local candidates, everything else is ignored.
*
* Packets sent will be looped back (so that other clients on the same session
* can be on the same machine.
*
* The name of this transmitter is "multicast".
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "fs-multicast-stream-transmitter.h"
#include "fs-multicast-transmitter.h"
#include <gst/farsight/fs-candidate.h>
#include <gst/farsight/fs-conference.h>
#include <gst/gst.h>
#include <string.h>
#include <sys/types.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef G_OS_WIN32
# include <winsock2.h>
#else /*G_OS_WIN32*/
# include <netdb.h>
# include <sys/socket.h>
# include <netinet/in.h>
# include <arpa/inet.h>
#endif /*G_OS_WIN32*/
GST_DEBUG_CATEGORY_EXTERN (fs_multicast_transmitter_debug);
#define GST_CAT_DEFAULT fs_multicast_transmitter_debug
/* Signals */
enum
{
LAST_SIGNAL
};
/* props */
enum
{
PROP_0,
PROP_SENDING,
PROP_PREFERRED_LOCAL_CANDIDATES
};
struct _FsMulticastStreamTransmitterPrivate
{
gboolean disposed;
/* We don't actually hold a ref to this,
* But since our parent FsStream can not exist without its parent
* FsSession, we should be safe
*/
FsMulticastTransmitter *transmitter;
GMutex *mutex;
/* Protected by the mutex */
gboolean sending;
/*
* We have at most of those per component (index 0 is unused)
*/
FsCandidate **local_candidate;
/* Protected by the mutex */
FsCandidate **remote_candidate;
/* Protected by the mutex */
UdpSock **udpsocks;
GList *preferred_local_candidates;
};
#define FS_MULTICAST_STREAM_TRANSMITTER_GET_PRIVATE(o) \
(G_TYPE_INSTANCE_GET_PRIVATE ((o), FS_TYPE_MULTICAST_STREAM_TRANSMITTER, \
FsMulticastStreamTransmitterPrivate))
#define FS_MULTICAST_STREAM_TRANSMITTER_LOCK(s) \
g_mutex_lock ((s)->priv->mutex)
#define FS_MULTICAST_STREAM_TRANSMITTER_UNLOCK(s) \
g_mutex_unlock ((s)->priv->mutex)
static void fs_multicast_stream_transmitter_class_init (FsMulticastStreamTransmitterClass *klass);
static void fs_multicast_stream_transmitter_init (FsMulticastStreamTransmitter *self);
static void fs_multicast_stream_transmitter_dispose (GObject *object);
static void fs_multicast_stream_transmitter_finalize (GObject *object);
static void fs_multicast_stream_transmitter_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void fs_multicast_stream_transmitter_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static gboolean fs_multicast_stream_transmitter_force_remote_candidates (
FsStreamTransmitter *streamtransmitter, GList *candidates,
GError **error);
static GObjectClass *parent_class = NULL;
// static guint signals[LAST_SIGNAL] = { 0 };
static GType type = 0;
GType
fs_multicast_stream_transmitter_get_type (void)
{
return type;
}
GType
fs_multicast_stream_transmitter_register_type (FsPlugin *module)
{
static const GTypeInfo info = {
sizeof (FsMulticastStreamTransmitterClass),
NULL,
NULL,
(GClassInitFunc) fs_multicast_stream_transmitter_class_init,
NULL,
NULL,
sizeof (FsMulticastStreamTransmitter),
0,
(GInstanceInitFunc) fs_multicast_stream_transmitter_init
};
type = g_type_module_register_type (G_TYPE_MODULE (module),
FS_TYPE_STREAM_TRANSMITTER, "FsMulticastStreamTransmitter", &info, 0);
return type;
}
static void
fs_multicast_stream_transmitter_class_init (FsMulticastStreamTransmitterClass *klass)
{
GObjectClass *gobject_class = (GObjectClass *) klass;
FsStreamTransmitterClass *streamtransmitterclass =
FS_STREAM_TRANSMITTER_CLASS (klass);
parent_class = g_type_class_peek_parent (klass);
gobject_class->set_property = fs_multicast_stream_transmitter_set_property;
gobject_class->get_property = fs_multicast_stream_transmitter_get_property;
streamtransmitterclass->force_remote_candidates =
fs_multicast_stream_transmitter_force_remote_candidates;
g_object_class_override_property (gobject_class, PROP_SENDING, "sending");
g_object_class_override_property (gobject_class,
PROP_PREFERRED_LOCAL_CANDIDATES, "preferred-local-candidates");
gobject_class->dispose = fs_multicast_stream_transmitter_dispose;
gobject_class->finalize = fs_multicast_stream_transmitter_finalize;
g_type_class_add_private (klass, sizeof (FsMulticastStreamTransmitterPrivate));
}
static void
fs_multicast_stream_transmitter_init (FsMulticastStreamTransmitter *self)
{
/* member init */
self->priv = FS_MULTICAST_STREAM_TRANSMITTER_GET_PRIVATE (self);
self->priv->disposed = FALSE;
self->priv->sending = TRUE;
self->priv->mutex = g_mutex_new ();
}
static void
fs_multicast_stream_transmitter_dispose (GObject *object)
{
FsMulticastStreamTransmitter *self = FS_MULTICAST_STREAM_TRANSMITTER (object);
gint c;
if (self->priv->disposed)
/* If dispose did already run, return. */
return;
if (self->priv->udpsocks)
{
for (c = 1; c <= self->priv->transmitter->components; c++)
{
if (self->priv->udpsocks[c])
{
if (self->priv->sending)
fs_multicast_transmitter_udpsock_dec_sending (
self->priv->udpsocks[c]);
fs_multicast_transmitter_put_udpsock (self->priv->transmitter,
self->priv->udpsocks[c], self->priv->remote_candidate[c]->ttl);
self->priv->udpsocks[c] = NULL;
}
}
}
/* Make sure dispose does not run twice. */
self->priv->disposed = TRUE;
parent_class->dispose (object);
}
static void
fs_multicast_stream_transmitter_finalize (GObject *object)
{
FsMulticastStreamTransmitter *self = FS_MULTICAST_STREAM_TRANSMITTER (object);
gint c; /* component_id */
if (self->priv->preferred_local_candidates)
{
fs_candidate_list_destroy (self->priv->preferred_local_candidates);
self->priv->preferred_local_candidates = NULL;
}
if (self->priv->remote_candidate)
{
for (c = 1; c <= self->priv->transmitter->components; c++)
{
if (self->priv->remote_candidate[c])
fs_candidate_destroy (self->priv->remote_candidate[c]);
self->priv->remote_candidate[c] = NULL;
}
g_free (self->priv->remote_candidate);
self->priv->remote_candidate = NULL;
}
if (self->priv->local_candidate)
{
for (c = 1; c <= self->priv->transmitter->components; c++)
{
if (self->priv->local_candidate[c])
fs_candidate_destroy (self->priv->local_candidate[c]);
self->priv->local_candidate[c] = NULL;
}
g_free (self->priv->local_candidate);
self->priv->local_candidate = NULL;
}
g_free (self->priv->udpsocks);
self->priv->udpsocks = NULL;
g_mutex_free (self->priv->mutex);
parent_class->finalize (object);
}
static void
fs_multicast_stream_transmitter_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
FsMulticastStreamTransmitter *self = FS_MULTICAST_STREAM_TRANSMITTER (object);
switch (prop_id)
{
case PROP_SENDING:
FS_MULTICAST_STREAM_TRANSMITTER_LOCK (self);
g_value_set_boolean (value, self->priv->sending);
FS_MULTICAST_STREAM_TRANSMITTER_UNLOCK (self);
break;
case PROP_PREFERRED_LOCAL_CANDIDATES:
g_value_set_boxed (value, self->priv->preferred_local_candidates);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
fs_multicast_stream_transmitter_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
FsMulticastStreamTransmitter *self = FS_MULTICAST_STREAM_TRANSMITTER (object);
switch (prop_id) {
case PROP_SENDING:
{
gboolean old_sending = self->priv->sending;
gboolean sending = g_value_get_boolean (value);
gint c;
FS_MULTICAST_STREAM_TRANSMITTER_LOCK (self);
self->priv->sending = sending;
if (sending != old_sending)
for (c = 1; c <= self->priv->transmitter->components; c++)
if (self->priv->udpsocks[c])
{
guint8 ttl = self->priv->remote_candidate[c]->ttl;
fs_multicast_transmitter_udpsock_ref (self->priv->transmitter,
self->priv->udpsocks[c], ttl);
FS_MULTICAST_STREAM_TRANSMITTER_UNLOCK (self);
if (sending)
fs_multicast_transmitter_udpsock_inc_sending (
self->priv->udpsocks[c]);
else
fs_multicast_transmitter_udpsock_dec_sending (
self->priv->udpsocks[c]);
fs_multicast_transmitter_put_udpsock (self->priv->transmitter,
self->priv->udpsocks[c], ttl);
FS_MULTICAST_STREAM_TRANSMITTER_LOCK (self);
}
FS_MULTICAST_STREAM_TRANSMITTER_UNLOCK (self);
}
break;
case PROP_PREFERRED_LOCAL_CANDIDATES:
self->priv->preferred_local_candidates = g_value_dup_boxed (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static gboolean
fs_multicast_stream_transmitter_build (FsMulticastStreamTransmitter *self,
GError **error)
{
GList *item;
gint c;
self->priv->udpsocks = g_new0 (UdpSock *,
self->priv->transmitter->components + 1);
self->priv->local_candidate = g_new0 (FsCandidate *,
self->priv->transmitter->components + 1);
self->priv->remote_candidate = g_new0 (FsCandidate *,
self->priv->transmitter->components + 1);
for (item = g_list_first (self->priv->preferred_local_candidates);
item;
item = g_list_next (item))
{
FsCandidate *candidate = item->data;
if (candidate->proto != FS_NETWORK_PROTOCOL_UDP)
{
g_set_error (error, FS_ERROR, FS_ERROR_INVALID_ARGUMENTS,
"You set preferred candidate of a type %d that is not"
" FS_NETWORK_PROTOCOL_UDP",
candidate->proto);
return FALSE;
}
if (candidate->component_id == 0)
{
g_set_error (error, FS_ERROR, FS_ERROR_INVALID_ARGUMENTS,
"Component id 0 is invalid");
return FALSE;
}
if (candidate->component_id > self->priv->transmitter->components)
{
g_set_error (error, FS_ERROR, FS_ERROR_INVALID_ARGUMENTS,
"You specified an invalid component id %d with is higher"
" than the maximum %d", candidate->component_id,
self->priv->transmitter->components);
return FALSE;
}
if (self->priv->local_candidate[candidate->component_id])
{
g_set_error (error, FS_ERROR,
FS_ERROR_INVALID_ARGUMENTS,
"You set more than one preferred local candidate for component %u",
candidate->component_id);
return FALSE;
}
if (candidate->ip == NULL)
{
g_set_error (error, FS_ERROR,
FS_ERROR_INVALID_ARGUMENTS,
"You have not set the local ip address for the preferred candidate"
" for this component");
return FALSE;
}
self->priv->local_candidate[candidate->component_id] =
fs_candidate_copy (candidate);
}
for (c = 1; c <= self->priv->transmitter->components; c++)
{
if (!self->priv->local_candidate[c])
{
self->priv->local_candidate[c] = fs_candidate_new (NULL, c,
FS_CANDIDATE_TYPE_MULTICAST, FS_NETWORK_PROTOCOL_UDP, NULL, 0);
}
}
return TRUE;
}
static gboolean
fs_multicast_stream_transmitter_add_remote_candidate (
FsMulticastStreamTransmitter *self, FsCandidate *candidate,
GError **error)
{
UdpSock *newudpsock = NULL;
FS_MULTICAST_STREAM_TRANSMITTER_LOCK (self);
if (self->priv->remote_candidate[candidate->component_id])
{
FsCandidate *old_candidate =
self->priv->remote_candidate[candidate->component_id];
if (old_candidate->port == candidate->port &&
old_candidate->ttl == candidate->ttl &&
!strcmp (old_candidate->ip, candidate->ip))
{
GST_DEBUG ("Re-set the same candidate, ignoring");
FS_MULTICAST_STREAM_TRANSMITTER_UNLOCK (self);
return TRUE;
}
}
FS_MULTICAST_STREAM_TRANSMITTER_UNLOCK (self);
/*
* IMPROVE ME: We should probably check that the candidate's IP
* has the format x.x.x.x where x is [0,255] using GRegex, etc
* We should also check if the address is in the multicast range
*/
newudpsock = fs_multicast_transmitter_get_udpsock (
self->priv->transmitter,
candidate->component_id,
self->priv->local_candidate[candidate->component_id]->ip,
candidate->ip,
candidate->port,
candidate->ttl,
self->priv->sending,
error);
if (!newudpsock)
return FALSE;
FS_MULTICAST_STREAM_TRANSMITTER_LOCK (self);
if (self->priv->udpsocks[candidate->component_id])
{
if (self->priv->sending)
fs_multicast_transmitter_udpsock_dec_sending (
self->priv->udpsocks[candidate->component_id]);
fs_multicast_transmitter_put_udpsock (self->priv->transmitter,
self->priv->udpsocks[candidate->component_id],
self->priv->remote_candidate[candidate->component_id]->ttl);
}
self->priv->udpsocks[candidate->component_id] = newudpsock;
fs_candidate_destroy (self->priv->remote_candidate[candidate->component_id]);
self->priv->remote_candidate[candidate->component_id] =
fs_candidate_copy (candidate);
FS_MULTICAST_STREAM_TRANSMITTER_UNLOCK (self);
g_signal_emit_by_name (self, "new-active-candidate-pair",
self->priv->local_candidate[candidate->component_id],
self->priv->remote_candidate[candidate->component_id]);
return TRUE;
}
static gboolean
fs_multicast_stream_transmitter_force_remote_candidates (
FsStreamTransmitter *streamtransmitter, GList *candidates,
GError **error)
{
GList *item = NULL;
FsMulticastStreamTransmitter *self =
FS_MULTICAST_STREAM_TRANSMITTER (streamtransmitter);
for (item = candidates; item; item = g_list_next (item))
{
FsCandidate *candidate = item->data;
if (candidate->proto != FS_NETWORK_PROTOCOL_UDP) {
g_set_error (error, FS_ERROR, FS_ERROR_INVALID_ARGUMENTS,
"You set a candidate of a type %d that is not"
" FS_NETWORK_PROTOCOL_UDP",
candidate->proto);
return FALSE;
}
if (candidate->type != FS_CANDIDATE_TYPE_MULTICAST)
{
g_set_error (error, FS_ERROR, FS_ERROR_INVALID_ARGUMENTS,
"The remote candidate is not of the right type, it should be"
" FS_CANDIDATE_TYPE_MULTICAST (%d), but it is %d",
FS_CANDIDATE_TYPE_MULTICAST,
candidate->type);
return FALSE;
}
if (!candidate->ip) {
g_set_error (error, FS_ERROR, FS_ERROR_INVALID_ARGUMENTS,
"The candidate passed does not contain a valid ip");
return FALSE;
}
if (candidate->component_id == 0 ||
candidate->component_id > self->priv->transmitter->components) {
g_set_error (error, FS_ERROR, FS_ERROR_INVALID_ARGUMENTS,
"The candidate passed has an invalid component id %u (not in [1,%u])",
candidate->component_id, self->priv->transmitter->components);
return FALSE;
}
if (candidate->ttl == 0)
{
g_set_error (error, FS_ERROR, FS_ERROR_INVALID_ARGUMENTS,
"The TTL for IPv4 multicast candidates must not be 0");
return FALSE;
}
}
for (item = candidates; item; item = g_list_next (item))
if (!fs_multicast_stream_transmitter_add_remote_candidate (self,
item->data, error))
return FALSE;
return TRUE;
}
FsMulticastStreamTransmitter *
fs_multicast_stream_transmitter_newv (FsMulticastTransmitter *transmitter,
guint n_parameters, GParameter *parameters, GError **error)
{
FsMulticastStreamTransmitter *streamtransmitter = NULL;
streamtransmitter = g_object_newv (FS_TYPE_MULTICAST_STREAM_TRANSMITTER,
n_parameters, parameters);
if (!streamtransmitter) {
g_set_error (error, FS_ERROR, FS_ERROR_CONSTRUCTION,
"Could not build the stream transmitter");
return NULL;
}
streamtransmitter->priv->transmitter = transmitter;
if (!fs_multicast_stream_transmitter_build (streamtransmitter, error)) {
g_object_unref (streamtransmitter);
return NULL;
}
return streamtransmitter;
}
|