aboutsummaryrefslogtreecommitdiffstats
path: root/openbsc/tests/mgcp/mgcp_transcoding_test.c
blob: 1dbdb51f9fc51742477f7856d7472b21de8514c0 (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
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <err.h>

#include <osmocom/core/talloc.h>
#include <osmocom/core/application.h>

#include <openbsc/debug.h>
#include <openbsc/gsm_data.h>
#include <openbsc/mgcp.h>
#include <openbsc/mgcp_internal.h>
#include <openbsc/rtp.h>

#include "bscconfig.h"
#ifndef BUILD_MGCP_TRANSCODING
#error "Requires MGCP transcoding enabled (see --enable-mgcp-transcoding)"
#endif

#include "openbsc/mgcp_transcode.h"

uint8_t *audio_frame_l16[] = {
};

struct rtp_packets {
	float t;
	int len;
	char *data;
};

struct rtp_packets audio_packets_l16[] = {
	/* RTP: SeqNo=1, TS=160 */
	{0.020000, 332,
		"\x80\x0B\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
		"\x00\x00\x40\x13\x5A\x9E\x40\x13\x00\x00\xBF\xED\xA5\x62\xBF\xED"
	},
};

struct rtp_packets audio_packets_gsm[] = {
	/* RTP: SeqNo=1, TS=160 */
	{0.020000, 45,
		"\x80\x03\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
		"\xD4\x7C\xE3\xE9\x62\x50\x39\xF0\xF8\xB4\x68\xEA\x6C\x0E\x81\x1B"
		"\x56\x2A\xD5\xBC\x69\x9C\xD1\xF0\x66\x7A\xEC\x49\x7A\x33\x3D\x0A"
		"\xDE"
	},
};

struct rtp_packets audio_packets_gsm_invalid_size[] = {
	/* RTP: SeqNo=1, TS=160 */
	{0.020000, 41,
		"\x80\x03\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
		"\xD4\x7C\xE3\xE9\x62\x50\x39\xF0\xF8\xB4\x68\xEA\x6C\x0E\x81\x1B"
		"\x56\x2A\xD5\xBC\x69\x9C\xD1\xF0\x66\x7A\xEC\x49\x7A\x33\x3D\x0A"
		"\xDE"
	},
};

struct rtp_packets audio_packets_gsm_invalid_data[] = {
	/* RTP: SeqNo=1, TS=160 */
	{0.020000, 45,
		"\x80\x03\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
		"\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE"
		"\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE\xEE"
		"\xEE"
	},
};

struct rtp_packets audio_packets_gsm_invalid_ptype[] = {
	/* RTP: SeqNo=1, TS=160 */
	{0.020000, 45,
		"\x80\x08\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
		"\xD4\x7C\xE3\xE9\x62\x50\x39\xF0\xF8\xB4\x68\xEA\x6C\x0E\x81\x1B"
		"\x56\x2A\xD5\xBC\x69\x9C\xD1\xF0\x66\x7A\xEC\x49\x7A\x33\x3D\x0A"
		"\xDE"
	},
};

struct rtp_packets audio_packets_g729[] = {
	/* RTP: SeqNo=1, TS=160 */
	{0.020000, 32,
		"\x80\x12\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
		"\xAF\xC2\x81\x40\x00\xFA\xCE\xA4\x21\x7C\xC5\xC3\x4F\xA5\x98\xF5"
		"\xB2\x95\xC4\xAD"
	},
};

struct rtp_packets audio_packets_pcma[] = {
	/* RTP: SeqNo=1, TS=160 */
	{0.020000, 172,
		"\x80\x08\x00\x01\x00\x00\x00\xA0\x11\x22\x33\x44"
		"\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25"
		"\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25"
		"\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25"
		"\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25"
		"\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25"
		"\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25"
		"\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25"
		"\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25"
		"\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25"
		"\xD5\xA5\xA3\xA5\xD5\x25\x23\x25\xD5\xA5\xA3\xA5\xD5\x25\x23\x25"
	},
	/* RTP: SeqNo=26527, TS=232640 */
	{0.020000, 92,
		"\x80\x08\x67\x9f\x00\x03\x8c\xc0\x04\xaa\x67\x9f\xd5\xd5\xd5\xd5"
		"\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5"
		"\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5"
		"\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5\xd5"
		"\xd5\xd5\xd5\xd5\xd5\xd5\x55\x55\xd5\xd5\x55\x55\xd5\xd5\x55\x55"
		"\xd5\xd5\xd5\x55\x55\xd5\xd5\xd5\x55\x55\xd5\xd5"
	},
	/* RTP: SeqNo=26528, TS=232720 */
	{0.020000, 92,
		"\x80\x08\x67\xa0\x00\x03\x8d\x10\x04\xaa\x67\x9f\x55\xd5\xd5\x55"
		"\xd5\x55\xd5\xd5\xd5\x55\xd5\x55\xd5\xd5\x55\xd5\x55\xd5\x55\xd5"
		"\x55\x55\xd5\x55\xd5\xd5\x55\x55\x55\x55\x55\xd5\xd5\x55\xd5\xd5"
		"\xd5\x55\xd5\xd5\xd5\x55\x54\x55\xd5\xd5\x55\xd5\xd5\xd5\xd5\x55"
		"\x54\x55\xd5\x55\xd5\x55\x55\x55\x55\x55\xd5\xd5\xd5\xd5\xd5\xd4"
		"\xd5\x54\x55\xd5\xd4\xd5\x54\xd5\x55\xd5\xd5\xd5"
	},
};



static int audio_name_to_type(const char *name)
{
	if (!strcasecmp(name, "gsm"))
		return 3;
#ifdef HAVE_BCG729
	else if (!strcasecmp(name, "g729"))
		return 18;
#endif
	else if (!strcasecmp(name, "pcma"))
		return 8;
	else if (!strcasecmp(name, "l16"))
		return 11;
	return -1;
}

int mgcp_get_trans_frame_size(void *state_, int nsamples, int dst);

static int given_configured_endpoint(int in_samples, int out_samples,
				const char *srcfmt, const char *dstfmt,
				void **out_ctx, struct mgcp_endpoint **out_endp)
{
	int rc;
	struct mgcp_rtp_end *dst_end;
	struct mgcp_rtp_end *src_end;
	struct mgcp_config *cfg;
	struct mgcp_trunk_config *tcfg;
	struct mgcp_endpoint *endp;

	cfg = mgcp_config_alloc();
	tcfg = talloc_zero(cfg, struct mgcp_trunk_config);
	endp = talloc_zero(tcfg, struct mgcp_endpoint);

	cfg->setup_rtp_processing_cb = mgcp_transcoding_setup;
	cfg->rtp_processing_cb = mgcp_transcoding_process_rtp;
	cfg->get_net_downlink_format_cb = mgcp_transcoding_net_downlink_format;

	tcfg->endpoints = endp;
	tcfg->number_endpoints = 1;
	tcfg->cfg = cfg;
	endp->tcfg = tcfg;
	endp->cfg = cfg;
	mgcp_initialize_endp(endp);

	dst_end = &endp->bts_end;
	dst_end->codec.payload_type = audio_name_to_type(dstfmt);

	src_end = &endp->net_end;
	src_end->codec.payload_type = audio_name_to_type(srcfmt);

	if (out_samples) {
		dst_end->codec.frame_duration_den = dst_end->codec.rate;
		dst_end->codec.frame_duration_num = out_samples;
		dst_end->frames_per_packet = 1;
		dst_end->force_output_ptime = 1;
	}

	rc = mgcp_transcoding_setup(endp, dst_end, src_end);
	if (rc < 0) {
		printf("setup failed: %s", strerror(-rc));
		abort();
	}

	*out_ctx = cfg;
	*out_endp = endp;
	return 0;
}


static int transcode_test(const char *srcfmt, const char *dstfmt,
			  uint8_t *src_pkts, size_t src_pkt_size)
{
	char buf[4096] = {0x80, 0};
	void *ctx;

	struct mgcp_rtp_end *dst_end;
	struct mgcp_process_rtp_state *state;
	struct mgcp_endpoint *endp;
	int in_size;
	const int in_samples = 160;
	int len, cont;

	printf("== Transcoding test ==\n");
	printf("converting %s -> %s\n", srcfmt, dstfmt);

	given_configured_endpoint(in_samples, 0, srcfmt, dstfmt, &ctx, &endp);

	dst_end = &endp->bts_end;
	state = dst_end->rtp_process_data;
	OSMO_ASSERT(state != NULL);

	in_size = mgcp_transcoding_get_frame_size(state, in_samples, 0);
	OSMO_ASSERT(sizeof(buf) >= in_size + 12);

	memcpy(buf, src_pkts, src_pkt_size);

	len = src_pkt_size;

	cont = mgcp_transcoding_process_rtp(endp, dst_end,
					    buf, &len, sizeof(buf));
	if (cont < 0) {
		printf("Nothing encoded due: %s\n", strerror(-cont));
		talloc_free(ctx);
		return -1;
	}

	if (len < 24) {
		printf("encoded: %s\n", osmo_hexdump((unsigned char *)buf, len));
	} else {
		const char *str = osmo_hexdump((unsigned char *)buf, len);
		int i = 0;
		const int prefix = 4;
		const int cutlen = 48;
		int nchars = 0;

		printf("encoded:\n");
		do {
			nchars = printf("%*s%-.*s", prefix, "", cutlen, str + i);
			i += nchars - prefix;
			printf("\n");
		} while (nchars - prefix >= cutlen);
	}
	printf("counted: %d\n", cont);
	talloc_free(ctx);
	return 0;
}

static void test_rtp_seq_state(void)
{
	char buf[4096];
	int len;
	int cont;
	void *ctx;
	struct mgcp_endpoint *endp;
	struct mgcp_process_rtp_state *state;
	struct rtp_hdr *hdr;
	uint32_t ts_no;
	uint16_t seq_no;

	given_configured_endpoint(160, 0, "pcma", "l16", &ctx, &endp);
	state = endp->bts_end.rtp_process_data;
	OSMO_ASSERT(!state->is_running);
	OSMO_ASSERT(state->next_seq == 0);
	OSMO_ASSERT(state->next_time == 0);

	/* initialize packet */
	len = audio_packets_pcma[0].len;
	memcpy(buf, audio_packets_pcma[0].data, len);
	cont = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, len);
	OSMO_ASSERT(cont >= 0);
	OSMO_ASSERT(state->is_running);
	OSMO_ASSERT(state->next_seq == 2);
	OSMO_ASSERT(state->next_time = 240);

	/* verify that the right timestamp was written */
	OSMO_ASSERT(len == audio_packets_pcma[0].len);
	hdr = (struct rtp_hdr *) &buf[0];

	memcpy(&ts_no, &hdr->timestamp, sizeof(ts_no));
	OSMO_ASSERT(htonl(ts_no) == 160);
	memcpy(&seq_no, &hdr->sequence, sizeof(seq_no));
	OSMO_ASSERT(htons(seq_no) == 1);
	/* Check the right sequence number is written */
	state->next_seq = 1234;
	len = audio_packets_pcma[0].len;
	memcpy(buf, audio_packets_pcma[0].data, len);
	cont = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, len);
	OSMO_ASSERT(cont >= 0);
	OSMO_ASSERT(len == audio_packets_pcma[0].len);
	hdr = (struct rtp_hdr *) &buf[0];

	memcpy(&seq_no, &hdr->sequence, sizeof(seq_no));
	OSMO_ASSERT(htons(seq_no) == 1234);

	talloc_free(ctx);
}

static void test_transcode_result(void)
{
	char buf[4096];
	int len, res;
	void *ctx;
	struct mgcp_endpoint *endp;
	struct mgcp_process_rtp_state *state;

	{
		/* from GSM to PCMA and same ptime */
		given_configured_endpoint(160, 0, "gsm", "pcma", &ctx, &endp);
		state = endp->bts_end.rtp_process_data;

		/* result */
		len = audio_packets_gsm[0].len;
		memcpy(buf, audio_packets_gsm[0].data, len);
		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
		OSMO_ASSERT(res == sizeof(struct rtp_hdr));
		OSMO_ASSERT(state->sample_cnt == 0);

		len = res;
		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
		OSMO_ASSERT(res == -ENOMSG);

		talloc_free(ctx);
	}

	{
		/* from GSM to PCMA and same ptime */
		given_configured_endpoint(160, 160, "gsm", "pcma", &ctx, &endp);
		state = endp->bts_end.rtp_process_data;

		/* result */
		len = audio_packets_gsm[0].len;
		memcpy(buf, audio_packets_gsm[0].data, len);
		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
		OSMO_ASSERT(res == sizeof(struct rtp_hdr));
		OSMO_ASSERT(state->sample_cnt == 0);

		len = res;
		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
		OSMO_ASSERT(res == -EAGAIN);

		talloc_free(ctx);
	}

	{
		/* from PCMA to GSM and wrong different ptime */
		given_configured_endpoint(80, 160, "pcma", "gsm", &ctx, &endp);
		state = endp->bts_end.rtp_process_data;

		/* Add the first sample */
		len = audio_packets_pcma[1].len;
		memcpy(buf, audio_packets_pcma[1].data, len);
		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
		OSMO_ASSERT(state->sample_cnt == 80);
		OSMO_ASSERT(state->next_time == 232640);
		OSMO_ASSERT(res < 0);

		/* Add the second sample and it should be consumable */
		len = audio_packets_pcma[2].len;
		memcpy(buf, audio_packets_pcma[2].data, len);
		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
		OSMO_ASSERT(state->sample_cnt == 0);
		OSMO_ASSERT(state->next_time == 232640 + 80 + 160);
		OSMO_ASSERT(res == sizeof(struct rtp_hdr));

		talloc_free(ctx);
	}

	{
		/* from PCMA to GSM with a big time jump */
		struct rtp_hdr *hdr;
		uint32_t ts;

		given_configured_endpoint(80, 160, "pcma", "gsm", &ctx, &endp);
		state = endp->bts_end.rtp_process_data;

		/* Add the first sample */
		len = audio_packets_pcma[1].len;
		memcpy(buf, audio_packets_pcma[1].data, len);
		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
		OSMO_ASSERT(state->sample_cnt == 80);
		OSMO_ASSERT(state->next_time == 232640);
		OSMO_ASSERT(state->next_seq == 26527);
		OSMO_ASSERT(res < 0);

		/* Add a skip to the packet to force a 'resync' */
		len = audio_packets_pcma[2].len;
		memcpy(buf, audio_packets_pcma[2].data, len);
		hdr = (struct rtp_hdr *) &buf[0];
		/* jump the time and add alignment error */
		ts = ntohl(hdr->timestamp) + 123 * 80 + 2;
		hdr->timestamp = htonl(ts);
		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
		OSMO_ASSERT(res < 0);
		OSMO_ASSERT(state->sample_cnt == 80);
		OSMO_ASSERT(state->next_time == ts);
		OSMO_ASSERT(state->next_seq == 26527);
		/* TODO: this can create alignment errors */


		/* Now attempt to consume 160 samples */
		len = audio_packets_pcma[2].len;
		memcpy(buf, audio_packets_pcma[2].data, len);
		hdr = (struct rtp_hdr *) &buf[0];
		ts += 80;
		hdr->timestamp = htonl(ts);
		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
		OSMO_ASSERT(res == 12);
		OSMO_ASSERT(state->sample_cnt == 0);
		OSMO_ASSERT(state->next_time == ts + 160);
		OSMO_ASSERT(state->next_seq == 26528);

		talloc_free(ctx);
	}
}

static void test_transcode_change(void)
{
	char buf[4096] = {0x80, 0};
	void *ctx;

	struct mgcp_endpoint *endp;
	struct mgcp_process_rtp_state *state;
	struct rtp_hdr *hdr;

	int len, res;

	{
		/* from GSM to PCMA and same ptime */
		printf("Testing Initial L16->GSM, PCMA->GSM\n");
		given_configured_endpoint(160, 0, "l16", "gsm", &ctx, &endp);
		endp->net_end.alt_codec = endp->net_end.codec;
		endp->net_end.alt_codec.payload_type = audio_name_to_type("pcma");
		state = endp->bts_end.rtp_process_data;

		/* initial transcoding work */
		OSMO_ASSERT(state->src_fmt == AF_L16);
		OSMO_ASSERT(state->dst_fmt == AF_GSM);
		OSMO_ASSERT(endp->net_end.alt_codec.payload_type == 8);
		OSMO_ASSERT(endp->net_end.codec.payload_type == 11);

		/* result */
		len = audio_packets_pcma[0].len;
		memcpy(buf, audio_packets_pcma[0].data, len);
		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
		OSMO_ASSERT(res == sizeof(struct rtp_hdr));
		OSMO_ASSERT(state->sample_cnt == 0);
		OSMO_ASSERT(state->src_fmt == AF_PCMA);
		OSMO_ASSERT(state->dst_fmt == AF_GSM);
		OSMO_ASSERT(endp->net_end.alt_codec.payload_type == 11);
		OSMO_ASSERT(endp->net_end.codec.payload_type == 8);

		len = res;
		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
		OSMO_ASSERT(res == -ENOMSG);


		/* now check that comfort noise doesn't change anything */
		len = audio_packets_pcma[1].len;
		memcpy(buf, audio_packets_pcma[1].data, len);
		hdr = (struct rtp_hdr *) buf;
		hdr->payload_type = 12;
		res = mgcp_transcoding_process_rtp(endp, &endp->bts_end, buf, &len, ARRAY_SIZE(buf));
		OSMO_ASSERT(state->sample_cnt == 80);
		OSMO_ASSERT(state->src_fmt == AF_PCMA);
		OSMO_ASSERT(state->dst_fmt == AF_GSM);
		OSMO_ASSERT(endp->net_end.alt_codec.payload_type == 11);
		OSMO_ASSERT(endp->net_end.codec.payload_type == 8);

		talloc_free(ctx);
	}
}

static int test_repacking(int in_samples, int out_samples, int no_transcode)
{
	char buf[4096] = {0x80, 0};
	int cc;
	struct mgcp_endpoint *endp;
	void *ctx;

	struct mgcp_process_rtp_state *state;
	int in_cnt;
	int out_size;
	int in_size;
	uint32_t ts = 0;
	uint16_t seq = 0;
	const char *srcfmt = "pcma";
	const char *dstfmt = no_transcode ? "pcma" : "l16";

	printf("== Transcoding test ==\n");
	printf("converting %s -> %s\n", srcfmt, dstfmt);

	given_configured_endpoint(in_samples, out_samples, srcfmt, dstfmt, &ctx, &endp);

	state = endp->bts_end.rtp_process_data;
	OSMO_ASSERT(state != NULL);

	in_size = mgcp_transcoding_get_frame_size(state, in_samples, 0);
	OSMO_ASSERT(sizeof(buf) >= in_size + 12);

	out_size = mgcp_transcoding_get_frame_size(state, -1, 1);
	OSMO_ASSERT(sizeof(buf) >= out_size + 12);

	buf[1] = endp->net_end.codec.payload_type;
	*(uint16_t*)(buf+2) = htons(1);
	*(uint32_t*)(buf+4) = htonl(0);
	*(uint32_t*)(buf+8) = htonl(0xaabbccdd);

	for (in_cnt = 0; in_cnt < 16; in_cnt++) {
		int cont;
		int len;

		/* fake PCMA data */
		printf("generating %d %s input samples\n", in_samples, srcfmt);
		for (cc = 0; cc < in_samples; cc++)
			buf[12+cc] = cc;

		*(uint16_t*)(buf+2) = htonl(seq);
		*(uint32_t*)(buf+4) = htonl(ts);

		seq += 1;
		ts += in_samples;

		cc += 12; /* include RTP header */

		len = cc;

		do {
			cont = mgcp_transcoding_process_rtp(endp, &endp->bts_end,
							    buf, &len, sizeof(buf));
			if (cont == -EAGAIN) {
				fprintf(stderr, "Got EAGAIN\n");
				break;
			}

			if (cont < 0) {
				printf("processing failed: %s", strerror(-cont));
				abort();
			}

			len -= 12; /* ignore RTP header */

			printf("got %d %s output frames (%d octets) count=%d\n",
			       len / out_size, dstfmt, len, cont);

			len = cont;
		} while (len > 0);
	}

	talloc_free(ctx);
	return 0;
}

int main(int argc, char **argv)
{
	int rc;
	osmo_init_logging(&log_info);

	printf("=== Transcoding Good Cases ===\n");

	transcode_test("l16", "l16",
		       (uint8_t *)audio_packets_l16[0].data,
		       audio_packets_l16[0].len);
	transcode_test("l16", "gsm",
		       (uint8_t *)audio_packets_l16[0].data,
		       audio_packets_l16[0].len);
	transcode_test("l16", "pcma",
		       (uint8_t *)audio_packets_l16[0].data,
		       audio_packets_l16[0].len);
	transcode_test("gsm", "l16",
		       (uint8_t *)audio_packets_gsm[0].data,
		       audio_packets_gsm[0].len);
	transcode_test("gsm", "gsm",
		       (uint8_t *)audio_packets_gsm[0].data,
		       audio_packets_gsm[0].len);
	transcode_test("gsm", "pcma",
		       (uint8_t *)audio_packets_gsm[0].data,
		       audio_packets_gsm[0].len);
	transcode_test("pcma", "l16",
		       (uint8_t *)audio_packets_pcma[0].data,
		       audio_packets_pcma[0].len);
	transcode_test("pcma", "gsm",
		       (uint8_t *)audio_packets_pcma[0].data,
		       audio_packets_pcma[0].len);
	transcode_test("pcma", "pcma",
		       (uint8_t *)audio_packets_pcma[0].data,
		       audio_packets_pcma[0].len);

	printf("=== Transcoding Bad Cases ===\n");

	printf("Invalid size:\n");
	rc = transcode_test("gsm", "pcma",
		       (uint8_t *)audio_packets_gsm_invalid_size[0].data,
		       audio_packets_gsm_invalid_size[0].len);
	OSMO_ASSERT(rc < 0);

	printf("Invalid data:\n");
	rc = transcode_test("gsm", "pcma",
		       (uint8_t *)audio_packets_gsm_invalid_data[0].data,
		       audio_packets_gsm_invalid_data[0].len);
	OSMO_ASSERT(rc < 0);

	printf("Invalid payload type:\n");
	rc = transcode_test("gsm", "pcma",
		       (uint8_t *)audio_packets_gsm_invalid_ptype[0].data,
		       audio_packets_gsm_invalid_ptype[0].len);
	OSMO_ASSERT(rc == 0);

	printf("=== Repacking ===\n");

	test_repacking(160, 160, 0);
	test_repacking(160, 160, 1);
	test_repacking(160, 80, 0);
	test_repacking(160, 80, 1);
	test_repacking(160, 320, 0);
	test_repacking(160, 320, 1);
	test_repacking(160, 240, 0);
	test_repacking(160, 240, 1);
	test_repacking(160, 100, 0);
	test_repacking(160, 100, 1);
	test_rtp_seq_state();
	test_transcode_result();
	test_transcode_change();

	return 0;
}