aboutsummaryrefslogtreecommitdiffstats
path: root/src/osmux.c
blob: a428b64f5a347b01d5e6c84607dc326e55febdd7 (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
/*
 * (C) 2012 by Pablo Neira Ayuso <pablo@gnumonks.org>
 * (C) 2012 by On Waves ehf <http://www.on-waves.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <stdio.h>
#include <string.h>

#include <osmocom/core/msgb.h>
#include <osmocom/core/timer.h>
#include <osmocom/core/select.h>
#include <osmocom/core/talloc.h>
#include <osmocom/core/logging.h>

#include <osmocom/netif/amr.h>
#include <osmocom/netif/rtp.h>
#include <osmocom/netif/osmux.h>

#include <arpa/inet.h>

#define DEBUG_TIMING		1

/* XXX: MTU - iphdr (20 bytes) - udphdr (8 bytes) */
#define OSMUX_BATCH_MAX		1472

/* delta time between two RTP messages */
#define DELTA_RTP_MSG		20000

struct osmux_hdr *osmux_xfrm_output_pull(struct msgb *msg)
{
	struct osmux_hdr *osmuxh = NULL;

	if (msg->len > sizeof(struct osmux_hdr)) {
		osmuxh = (struct osmux_hdr *)msg->data;

		msgb_pull(msg, sizeof(struct osmux_hdr) +
			  (osmo_amr_bytes(osmuxh->amr_cmr) * (osmuxh->ctr+1)));
	} else if (msg->len > 0) {
		LOGP(DLMIB, LOGL_ERROR,
			"remaining %d bytes, broken osmuxhdr?\n", msg->len);
	}

	return osmuxh;
}

static struct msgb *
osmux_rebuild_rtp(struct osmux_out_handle *h,
		  struct osmux_hdr *osmuxh, void *payload, int payload_len)
{
	struct msgb *out_msg;
	struct rtp_hdr *rtph;
	struct amr_hdr *amrh;
	uint32_t ssrc_from_ccid = osmuxh->circuit_id;
	char buf[4096];

	out_msg = msgb_alloc(sizeof(struct rtp_hdr) +
			     sizeof(struct amr_hdr) +
			     osmo_amr_bytes(osmuxh->amr_cmr),
			     "OSMUX test");
	if (out_msg == NULL)
		return NULL;

	/* Reconstruct RTP header */
	rtph = (struct rtp_hdr *)out_msg->data;
	rtph->csrc_count = 0;
	rtph->extension = 0;
	rtph->version = RTP_VERSION;
	rtph->payload_type = 98;
	/* ... emulate timestamp and ssrc */
	rtph->timestamp = htonl(h->rtp_timestamp[osmuxh->circuit_id]);
	rtph->sequence = htons(h->rtp_seq[osmuxh->circuit_id]);
	rtph->ssrc = htonl(ssrc_from_ccid);

	msgb_put(out_msg, sizeof(struct rtp_hdr));

	/* Reconstruct AMR header */
	amrh = (struct amr_hdr *)out_msg->tail;
	amrh->cmr = osmuxh->amr_cmr;
	amrh->f = osmuxh->amr_f;
	amrh->ft = osmuxh->amr_ft;
	amrh->q = osmuxh->amr_q;

	msgb_put(out_msg, sizeof(struct amr_hdr));

	/* add AMR speech data */
	memcpy(out_msg->tail, payload, payload_len);
	msgb_put(out_msg, payload_len);

	/* bump last RTP sequence number and timestamp that has been used */
	h->rtp_seq[osmuxh->circuit_id]++;
	h->rtp_timestamp[osmuxh->circuit_id] += 160;

	osmo_rtp_snprintf(buf, sizeof(buf), out_msg);
	LOGP(DLMIB, LOGL_DEBUG, "%s\n", buf);

	return out_msg;
}

int osmux_xfrm_output(struct osmux_hdr *osmuxh, struct osmux_out_handle *h,
		      struct llist_head *list)
{
	struct msgb *msg;
	int i;

	INIT_LLIST_HEAD(list);

	for (i=0; i<osmuxh->ctr+1; i++) {
		msg = osmux_rebuild_rtp(h, osmuxh,
					osmux_get_payload(osmuxh) +
					i * osmo_amr_bytes(osmuxh->amr_cmr),
					osmo_amr_bytes(osmuxh->amr_cmr));
		if (msg == NULL)
			break;

		LOGP(DLMIB, LOGL_DEBUG, "extracted RTP message from batch "
					 "msg=%p\n", msg);

		llist_add_tail(&msg->list, list);
	}
	return i;
}

struct osmux_batch {
	struct osmo_timer_list	timer;
	struct osmux_hdr	*osmuxh;
	struct llist_head	msgb_list;
	unsigned int		remaining_bytes;
	uint8_t			seq;
	int64_t			ccid[OSMUX_MAX_CONCURRENT_CALLS];
};

static int
osmux_batch_add(struct osmux_in_handle *h, struct msgb *out_msg,
		struct msgb *msg, struct rtp_hdr *rtph,
		struct amr_hdr *amrh, uint32_t amr_payload_len,
		int add_osmux_header)
{
	struct osmux_batch *batch = (struct osmux_batch *)h->data;
	struct osmux_hdr *osmuxh;

	if (add_osmux_header) {
		osmuxh = (struct osmux_hdr *)out_msg->tail;
		osmuxh->ft = OSMUX_FT_VOICE_AMR;
		osmuxh->ctr = 0;
		osmuxh->amr_f = amrh->f;
		osmuxh->amr_q= amrh->q;
		osmuxh->seq = batch->seq++;
		osmuxh->circuit_id = osmux_xfrm_input_get_ccid(h, rtph->ssrc);
		osmuxh->amr_cmr = amrh->cmr;
		osmuxh->amr_ft = amrh->ft;
		msgb_put(out_msg, sizeof(struct osmux_hdr));

		/* annotate current osmux header */
		batch->osmuxh = osmuxh;
	} else {
		if (batch->osmuxh->ctr == 0x7) {
			LOGP(DLMIB, LOGL_ERROR, "cannot add msg=%p, "
				"too many messages for this RTP ssrc=%u\n",
				msg, rtph->ssrc);
			return 0;
		}
		batch->osmuxh->ctr++;
	}

	memcpy(out_msg->tail, osmo_amr_get_payload(amrh), amr_payload_len);
	msgb_put(out_msg, amr_payload_len);

	return 0;
}

static int
osmux_xfrm_encode_amr(struct osmux_in_handle *h,
		      struct msgb *out_msg,
		      struct rtp_hdr *rtph, struct msgb *msg,
		      int add_osmux_header)
{
	struct amr_hdr *amrh;
	uint32_t amr_len;
	uint32_t amr_payload_len;

	amrh = osmo_rtp_get_payload(rtph, msg, &amr_len);
	if (amrh == NULL)
		return -1;

	amr_payload_len = amr_len - sizeof(struct amr_hdr);

	if (osmux_batch_add(h, out_msg, msg, rtph, amrh, amr_payload_len,
			    add_osmux_header) < 0)
		return -1;

	return 0;
}

static struct msgb *osmux_build_batch(struct osmux_in_handle *h)
{
	struct msgb *cur, *tmp, *batch_msg;
	uint32_t last_rtp_ssrc;
	int last_rtp_ssrc_set = 0, add_osmux_hdr = 1;
	struct osmux_batch *batch = (struct osmux_batch *)h->data;

	batch_msg = msgb_alloc(OSMUX_BATCH_MAX, "OSMUX");
	if (batch_msg == NULL) {
		LOGP(DLMIB, LOGL_ERROR, "Not enough memory\n");
		return NULL;
	}

	LOGP(DLMIB, LOGL_DEBUG, "Now building batch\n");

	llist_for_each_entry_safe(cur, tmp, &batch->msgb_list, list) {
		struct rtp_hdr *rtph;
		char buf[4096];

		rtph = osmo_rtp_get_hdr(cur);
		if (rtph == NULL)
			return NULL;

		if (last_rtp_ssrc_set) {
			add_osmux_hdr = (last_rtp_ssrc != rtph->ssrc);
			if (add_osmux_hdr)
				LOGP(DLMIB, LOGL_DEBUG, "add osmux header\n");
		}

		osmo_rtp_snprintf(buf, sizeof(buf), cur);

		LOGP(DLMIB, LOGL_DEBUG, "%s\n", buf);

		osmux_xfrm_encode_amr(h, batch_msg, rtph, cur, add_osmux_hdr);

		last_rtp_ssrc_set = 1;
		last_rtp_ssrc = rtph->ssrc;

		llist_del(&cur->list);
		msgb_free(cur);
	}
	return batch_msg;
}

void osmux_xfrm_input_deliver(struct osmux_in_handle *h)
{
	struct msgb *batch_msg;
	struct osmux_batch *batch = (struct osmux_batch *)h->data;

	LOGP(DLMIB, LOGL_DEBUG, "invoking delivery function\n");
	batch_msg = osmux_build_batch(h);
	h->deliver(batch_msg);
	osmo_timer_del(&batch->timer);
	batch->remaining_bytes = OSMUX_BATCH_MAX;
}

static void osmux_batch_timer_expired(void *data)
{
	struct osmux_in_handle *h = data;

	LOGP(DLMIB, LOGL_DEBUG, "received message from stream\n");
	osmux_xfrm_input_deliver(h);
}

static int osmux_rtp_amr_payload_len(struct msgb *msg, struct rtp_hdr *rtph)
{
	struct amr_hdr *amrh;
	unsigned int amr_len;

	amrh = osmo_rtp_get_payload(rtph, msg, &amr_len);
	if (amrh == NULL)
		return -1;

	return amr_len - sizeof(struct amr_hdr);
}

static int
osmux_msgb_batch_queue_add(struct osmux_batch *batch, struct msgb *msg)
{
	struct rtp_hdr *rtph;
	struct msgb *cur;
	int found_matching = 0, found_room = 0, bytes = 0;

	rtph = osmo_rtp_get_hdr(msg);
	if (rtph == NULL)
		return -1;

	llist_for_each_entry(cur, &batch->msgb_list, list) {
		struct rtp_hdr *rtph2;

		rtph2 = osmo_rtp_get_hdr(cur);
		if (rtph2 == NULL)
			return -1;

		if (rtph->ssrc == rtph2->ssrc) {
			found_matching = 1;
			continue;
		}

		/* We insert messages in order based on the RTP SSRC. This is
		 * useful to build the batch.
		 */
		if (rtph->ssrc > rtph2->ssrc) {
			found_room = 1;
			break;
		}
	}

	bytes += osmux_rtp_amr_payload_len(msg, rtph);
	if (!found_matching)
		bytes += sizeof(struct osmux_hdr);

	/* Still room in this batch for this message? if there is not
	 * then deliver current batch.
	 */
	if (bytes > batch->remaining_bytes)
		return 1;

	batch->remaining_bytes -= bytes;

	if (found_room)
		llist_add_tail(&msg->list, &cur->list);
	else
		llist_add_tail(&msg->list, &batch->msgb_list);

	LOGP(DLMIB, LOGL_DEBUG, "adding to batch (%p)\n", msg);

	return 0;
}

/**
 * osmux_xfrm_input - add RTP message to OSmux batch
 * \param msg: RTP message that you want to batch into one OSmux message
 *
 * This function returns -1 on error. If 0 is returned, this indicates
 * that the message has been batched. If 1 is returned, you have to
 * invoke osmux_xfrm_input_deliver and try again.
 */
int osmux_xfrm_input(struct osmux_in_handle *h, struct msgb *msg)
{
	int ret;
	struct rtp_hdr *rtph;
	struct osmux_batch *batch = (struct osmux_batch *)h->data;

	rtph = osmo_rtp_get_hdr(msg);
	if (rtph == NULL)
		return -1;

	switch(rtph->payload_type) {
		case RTP_PT_RTCP:
			return 0;
		case RTP_PT_AMR:
			/* This is the first message in the batch, start the
			 * batch timer to deliver it.
			 */
			if (llist_empty(&batch->msgb_list)) {
				LOGP(DLMIB, LOGL_DEBUG,
					"osmux start timer batch\n");

				osmo_timer_schedule(&batch->timer, 0,
					h->batch_factor * DELTA_RTP_MSG);
			}
			ret = osmux_msgb_batch_queue_add(batch, msg);
			break;
		default:
			/* Only AMR supported so far, sorry. */
			ret = -1;
			break;
	}
	return ret;
}

void osmux_xfrm_input_init(struct osmux_in_handle *h)
{
	struct osmux_batch *batch;
	int i;

	LOGP(DLMIB, LOGL_DEBUG, "initialized osmux input converter\n");

	batch = talloc(NULL, struct osmux_batch);
	if (batch == NULL)
		return;

	INIT_LLIST_HEAD(&batch->msgb_list);
	batch->remaining_bytes = OSMUX_BATCH_MAX;
	batch->timer.cb = osmux_batch_timer_expired;
	batch->timer.data = h;

	for (i=0; i<OSMUX_MAX_CONCURRENT_CALLS; i++)
		batch->ccid[i] = -1;

	h->data = (void *)batch;
}

struct osmux_tx_handle {
	struct osmo_timer_list	timer;
	struct msgb		*msg;
	void			(*tx_cb)(struct msgb *msg, void *data);
	void			*data;
#ifdef DEBUG_TIMING
	struct timeval		start;
	struct timeval		when;
#endif
};

static void osmux_tx_cb(void *data)
{
	struct osmux_tx_handle *h = data;
#ifdef DEBUG_TIMING
	struct timeval now, diff;

	gettimeofday(&now, NULL);
	timersub(&now, &h->start, &diff);
	timersub(&diff,&h->when, &diff);
	LOGP(DLMIB, LOGL_DEBUG, "we are lagging %lu.%.6lu in scheduled "
		"transmissions\n", diff.tv_sec, diff.tv_usec);
#endif

	h->tx_cb(h->msg, h->data);

	talloc_free(h);
}

static void
osmux_tx(struct msgb *msg, struct timeval *when,
	 void (*tx_cb)(struct msgb *msg, void *data), void *data)
{
	struct osmux_tx_handle *h;

	h = talloc_zero(NULL, struct osmux_tx_handle);
	if (h == NULL)
		return;

	h->msg = msg;
	h->tx_cb = tx_cb;
	h->data = data;
	h->timer.cb = osmux_tx_cb;
	h->timer.data = h;

#ifdef DEBUG_TIMING
	gettimeofday(&h->start, NULL);
	h->when.tv_sec = when->tv_sec;
	h->when.tv_usec = when->tv_usec;
#endif
	/* send it now */
	if (when->tv_sec == 0 && when->tv_usec == 0) {
		osmux_tx_cb(h);
		return;
	}
	osmo_timer_schedule(&h->timer, when->tv_sec, when->tv_usec);
}

void
osmux_tx_sched(struct llist_head *list,
	       void (*tx_cb)(struct msgb *msg, void *data), void *data)
{
	struct msgb *cur, *tmp;
	struct timeval delta = { .tv_sec = 0, .tv_usec = DELTA_RTP_MSG };
	struct timeval when;

	timerclear(&when);

	llist_for_each_entry_safe(cur, tmp, list, list) {

		LOGP(DLMIB, LOGL_DEBUG, "scheduled transmision in %lu.%6lu "
			"seconds, msg=%p\n", when.tv_sec, when.tv_usec, cur);

		osmux_tx(cur, &when, tx_cb, NULL);
		timeradd(&when, &delta, &when);
		llist_del(&cur->list);
	}
}

void osmux_xfrm_input_register_ccid(struct osmux_in_handle *h, uint32_t ssrc)
{
	struct osmux_batch *batch = (struct osmux_batch *)h->data;;
	int i, found = 0;

	for (i=0; i<OSMUX_MAX_CONCURRENT_CALLS; i++) {
		if (batch->ccid[i] == ssrc)
			continue;
		if (batch->ccid[i] < 0) {
			found = 1;
			break;
		}
	}

	if (found) {
		batch->ccid[i] = ssrc;
		LOGP(DLMIB, LOGL_DEBUG, "mapping ssrc=%u to ccid=%d\n",
			ntohl(ssrc), i);
	} else {
		LOGP(DLMIB, LOGL_ERROR, "cannot map ssrc to ccid!\n");
	}
}

int osmux_xfrm_input_get_ccid(struct osmux_in_handle *h, uint32_t ssrc)
{
	struct osmux_batch *batch = (struct osmux_batch *)h->data;;
	int i, found = 0;

	for (i=0; i<OSMUX_MAX_CONCURRENT_CALLS; i++) {
		if (batch->ccid[i] == ssrc) {
			found = 1;
			break;
		}
	}

	return found ? i : -1;
}

void osmux_xfrm_output_init(struct osmux_out_handle *h)
{
	int i;

	for (i=0; i<OSMUX_MAX_CONCURRENT_CALLS; i++) {
		h->rtp_seq[i] = (uint16_t)random();
		h->rtp_timestamp[i] = (uint32_t)random();
	}
}