aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/trau2rtp/trau2rtp.c
blob: fc9462d86d72971eeddaf5d6059e306a4ec6d0ad (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
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include <dahdi/user.h>

#include <osmocom/core/utils.h>
#include <osmocom/core/application.h>
#include <osmocom/gsm/i460_mux.h>
#include <osmocom/trau/trau_sync.h>
#include <osmocom/trau/trau_frame.h>
#include <osmocom/trau/trau_rtp.h>
#include <osmocom/trau/osmo_ortp.h>

#define D_BCHAN_TX_GRAN 160

/***********************************************************************
 * BEGIN CONFIGURATION
 ***********************************************************************/
/* HACK: Those do not have getopt but need to be modified in source code to match your enviroment */

/* do we just locally loop the calls from 1->2, or do we interface with RTP? */
static bool g_local_loop = false;

/* remote IP address to which we send RTP data (unless g_local_loop mode) */
const char *g_remote_host = "192.168.101.131";

/* remote UDP base port to which we send RTP data: +2 for every sub-slot */
const int g_remote_port = 9000;

/* local UDP base port on which we receive RTP data: +2 for every sub-slot */
const int g_local_port = 8000;

/* codec; can be OSMO_TRAU16_FT_FR or OSMO_TRAU16_FT_EFR */
const enum osmo_trau_frame_type g_ftype = OSMO_TRAU16_FT_EFR;

/***********************************************************************
 * END CONFIGURATION
 ***********************************************************************/

/* do we have a DAHDI chardev to which we can write RTP->TRAU conversion data/ */
static bool g_ts_is_writable = false;

struct sc_state {
	unsigned int num;
	struct osmo_i460_subchan *i460_sc;
	struct osmo_fsm_inst *sync_fi;
	struct osmo_rtp_socket *rtps;
	struct osmo_trau2rtp_state t2r;
};

struct state {
	int in_fd;
	struct osmo_i460_timeslot i460_ts;
	struct sc_state sc[4];
};
static struct state g_st;

#define LOGSC(sc, fmt, args...) printf("SC%u: " fmt, (sc)->num, ## args)

static struct sc_state *opposite_schan(struct sc_state *sc)
{
	/* we blindly assume that there is a call between sub-channel 1 + sub-channel 2 */
	switch (sc->num) {
	case 1:
		return &g_st.sc[2];
	case 2:
		return &g_st.sc[1];
	default:
		OSMO_ASSERT(0);
	}
}

/* called by I.460 de-multeiplexer; feed output of I.460 demux into TRAU frame sync */
static void i460_demux_bits_cb(struct osmo_i460_subchan *schan, void *user_data,
				const ubit_t *bits, unsigned int num_bits)
{
	struct sc_state *sc = user_data;
	//printf("I460: %s\n", osmo_ubit_dump(bits, num_bits));
	osmo_trau_sync_rx_ubits(sc->sync_fi, bits, num_bits);
}

/* called for each synchronized TRAU frame received; decode frame + convert to RTP */
static void sync_frame_out_cb(void *user_data, const ubit_t *bits, unsigned int num_bits)
{
	struct sc_state *sc = user_data;
	struct osmo_trau_frame fr;
	int rc;

	LOGSC(sc, "Rx TRAU: %s\n", osmo_ubit_dump(bits, num_bits));
	if (!bits)
		goto skip;

	rc = osmo_trau_frame_decode_16k(&fr, bits, OSMO_TRAU_DIR_UL);
	if (rc != 0)
		goto skip;

	uint8_t sid;
	switch (fr.type) {
	case OSMO_TRAU16_FT_FR:
	case OSMO_TRAU16_FT_EFR:
		sid = (fr.c_bits[13-1]) << 1 | (fr.c_bits[14-1] << 0);
		LOGSC(sc, "-> FT=%s, BFI=%u, SID=%u, TAF=%u DTXd=%u\n",
			osmo_trau_frame_type_name(fr.type), fr.c_bits[12-1], sid, fr.c_bits[15-1], fr.c_bits[17-1]);
		break;
	default:
		LOGSC(sc, "-> FT=%s\n", osmo_trau_frame_type_name(fr.type));
		break;
	}

	if (g_local_loop) {
		/* Mirror back to other sub-slot */
		struct sc_state *peer = opposite_schan(sc);
		if (peer) {
			struct msgb *msg = msgb_alloc(2*40*8, "mirror");
			fr.c_bits[12-1] = 1; /* C12 = good u-link frame */
			memset(&fr.c_bits[13-1], 1, 3); /* C13..C15: spare  */
			fr.c_bits[16-1] = 1; /* C16 = SP[eech]; no DTX */
			memset(&fr.c_bits[6-1], 0, 6); /* C6..C11: tie alignment */
			fr.dir = OSMO_TRAU_DIR_DL;
			rc = osmo_trau_frame_encode(msgb_data(msg), 2*40*8, &fr);
			OSMO_ASSERT(rc >= 0);
			msgb_put(msg, rc);
			osmo_i460_mux_enqueue(peer->i460_sc, msg);
		}
	} else {
		/* Convert to RTP */
		if (fr.type != OSMO_TRAU16_FT_FR && fr.type != OSMO_TRAU16_FT_EFR)
			goto skip;

		uint8_t rtpbuf[35];
		struct osmo_trau2rtp_state t2rs = {
			.type = fr.type,
		};
		memset(rtpbuf, 0, sizeof(rtpbuf));
		rc = osmo_trau2rtp(rtpbuf, sizeof(rtpbuf), &fr, &t2rs);
		LOGSC(sc, "Tx RTP: %s\n", osmo_hexdump(rtpbuf, rc));
		if (rc)
			osmo_rtp_send_frame_ext(sc->rtps, rtpbuf, rc, 160, false);
		else {
			osmo_rtp_skipped_frame(sc->rtps, 160);
		}
		return;
	}
skip:
	if (!g_local_loop)
		osmo_rtp_skipped_frame(sc->rtps, 160);
}

static int dahdi_set_bufinfo(int fd, int as_sigchan)
{
	struct dahdi_bufferinfo bi;
	int x = 0;

	if (ioctl(fd, DAHDI_GET_BUFINFO, &bi)) {
		LOGP(DLINP, LOGL_ERROR, "Error getting bufinfo\n");
		return -EIO;
	}

	if (as_sigchan) {
		bi.numbufs = 4;
		bi.bufsize = 512;
	} else {
		bi.numbufs = 8;
		bi.bufsize = D_BCHAN_TX_GRAN;
		bi.txbufpolicy = DAHDI_POLICY_WHEN_FULL;
	}

	if (ioctl(fd, DAHDI_SET_BUFINFO, &bi)) {
		fprintf(stderr, "Error setting DAHDI bufinfo\n");
		return -EIO;
	}

	if (!as_sigchan) {
		if (ioctl(fd, DAHDI_AUDIOMODE, &x)) {
			fprintf(stderr, "Error setting DAHDI bufinfo\n");
			return -EIO;
		}
	} else {
		int one = 1;
		ioctl(fd, DAHDI_HDLCFCSMODE, &one);
		/* we cannot reliably check for the ioctl return value here
		 * as this command will fail if the slot _already_ was a
		 * signalling slot before :( */
	}
	return 0;
}

static void mux_q_empty_cb(struct osmo_i460_subchan *schan, void *user_data);

/* RTP data was received on the socket */
static void ortp_rx_cb(struct osmo_rtp_socket *rs, const uint8_t *payload,
			unsigned int payload_len, uint16_t seq_number, uint32_t timestamp, bool marker)
{
	struct sc_state *sc = rs->priv;
	struct osmo_trau_frame fr;
	int rc;

	LOGSC(sc, "RTP Rx: %s\n", osmo_hexdump_nospc(payload, payload_len));

	sc->t2r.type = g_ftype;

	memset(&fr, 0, sizeof(fr));
	fr.dir = OSMO_TRAU_DIR_DL;
	rc = osmo_rtp2trau(&fr, payload, payload_len, &sc->t2r);
	if (rc < 0) {
		LOGSC(sc, "Failed to convert RTP to TRAU");
		return;
	}

	struct msgb *msg = msgb_alloc(2*40*8, "rtp2trau");
	rc = osmo_trau_frame_encode(msgb_data(msg), 2*40*8, &fr);
	OSMO_ASSERT(rc >= 0);
	msgb_put(msg, rc);
	osmo_i460_mux_enqueue(sc->i460_sc, msg);
}

static void init(const char *fname)
{
	struct stat st;
	int rc;

	struct osmo_i460_schan_desc scd16_0 = {
		.rate = OSMO_I460_RATE_16k,
		.demux = {
			.num_bits = 40*8,
			.out_cb_bytes = NULL,
		},
	};

	rc = stat(fname, &st);
	OSMO_ASSERT(rc == 0);
	if (S_ISCHR(st.st_mode)) {
		/* if this is a char device, we assume DAHDI */
		g_ts_is_writable = true;
		g_st.in_fd = open(fname, O_RDWR);
		OSMO_ASSERT(g_st.in_fd >= 0);
		dahdi_set_bufinfo(g_st.in_fd, false);
	} else {
		g_st.in_fd = open(fname, O_RDONLY);
		OSMO_ASSERT(g_st.in_fd >= 0);
	}

	osmo_i460_ts_init(&g_st.i460_ts);

	//for (i = 0; i < ARRAY_SIZE(g_st.sc); i++) {
	for (int i = 1; i < 3; i++) {
		struct sc_state *sc = &g_st.sc[i];
		sc->num = i;

		scd16_0.bit_offset = i * 2;
		scd16_0.demux.user_data = sc;
		scd16_0.demux.out_cb_bits = i460_demux_bits_cb,
		scd16_0.mux.in_cb_queue_empty = mux_q_empty_cb;
		scd16_0.mux.user_data = sc;
		sc->i460_sc = osmo_i460_subchan_add(NULL, &g_st.i460_ts, &scd16_0);
		OSMO_ASSERT(sc->i460_sc != NULL);

		char strbuf[16];
		snprintf(strbuf, sizeof(strbuf), "SC%u", sc->num);
		sc->sync_fi = osmo_trau_sync_alloc(NULL, strbuf, sync_frame_out_cb, OSMO_TRAU_SYNCP_16_FR_EFR, sc);
		OSMO_ASSERT(sc->sync_fi);
		sc->rtps = osmo_rtp_socket_create(NULL, OSMO_RTP_F_POLL);
		OSMO_ASSERT(sc->rtps);
		osmo_rtp_socket_set_pt(sc->rtps, RTP_PT_GSM_FULL);
		sc->rtps->rx_cb = ortp_rx_cb;
		sc->rtps->priv = sc;
		osmo_rtp_socket_bind(sc->rtps, "0.0.0.0", g_local_port + i*2);
		osmo_rtp_socket_connect(sc->rtps, g_remote_host, g_remote_port + i*2);
		//osmo_rtp_socket_autoconnect(sc->rtps);
	}
}

static void mux_q_empty_cb(struct osmo_i460_subchan *schan, void *user_data)
{
	struct sc_state *sc = user_data;
	struct msgb *msg = msgb_alloc(2*40*8, "mux-enq");
	struct osmo_trau_frame traufr = {
		.type = g_ftype,
		.dir = OSMO_TRAU_DIR_DL,
	};
	int rc;

	LOGSC(sc, "EMPTY -> Generating Tx\n");

	if (traufr.type == OSMO_TRAU16_FT_EFR) {
		traufr.c_bits[12-1] = 1; /* C12 = good u-link frame */
		traufr.c_bits[16-1] = 1; /* C16 = SP[eech]; no DTX */
	}

	rc = osmo_trau_frame_encode(msgb_data(msg), 2*40*8, &traufr);
	OSMO_ASSERT(rc >= 0);
	msgb_put(msg, rc);
	//LOGSC(sc, "Tx TRAU: %s\n", osmo_ubit_dump(cur, 40*8));
	osmo_i460_mux_enqueue(sc->i460_sc, msg);
}

static void process(void)
{
	uint8_t buf[D_BCHAN_TX_GRAN];
	int rc, nread;

	while (rc = read(g_st.in_fd, buf, sizeof(buf))) {
		OSMO_ASSERT(rc == sizeof(buf));
		nread = rc;
		osmo_i460_demux_in(&g_st.i460_ts, buf, nread);

		for (int i = 1; i < 3; i++) {
			struct sc_state *sc = &g_st.sc[i];
			int rc2 = osmo_rtp_socket_poll(sc->rtps);
			sc->rtps->rx_user_ts += 160;
			//printf("rtp_recv=%d (flags=%x)\n",rc2, sc->rtps->flags);
		}

		/* write as many bytes as we just received */
		osmo_i460_mux_out(&g_st.i460_ts, buf, nread);
		if (g_ts_is_writable) {
			rc = write(g_st.in_fd, buf, nread);
			if (rc != nread)
				printf("rc=%d, nread=%d (%s)\n", rc, nread, strerror(errno));
			OSMO_ASSERT(rc == nread);
		}
		//usleep(20000);
	}
}

int main(int argc, char **argv)
{
	osmo_init_logging2(NULL, NULL);
	osmo_fsm_log_addr(false);
	log_set_print_filename2(osmo_stderr_target, LOG_FILENAME_BASENAME);
	log_set_category_filter(osmo_stderr_target, DLMIB, true, LOGL_DEBUG);
	osmo_rtp_init(NULL);

	init(argv[1]);
	process();
}