aboutsummaryrefslogtreecommitdiffstats
path: root/examples/rtp-udp-test-server.c
blob: 2f6c72feaa6e5299b88d9c2e976f6e2c98b8a157 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>

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

#include <osmocom/netif/rtp.h>
#include <osmocom/netif/datagram.h>

#define DRTP_TEST 0

struct log_info_cat rtp_test_cat[] = {
	[DRTP_TEST] = {
		.name = "DRTP_TEST",
		.description = "RPT-server test",
		.color = "\033[1;35m",
		.enabled = 1, .loglevel = LOGL_DEBUG,
	},
};

const struct log_info rtp_test_log_info = {
	.filter_fn = NULL,
	.cat = rtp_test_cat,
	.num_cat = ARRAY_SIZE(rtp_test_cat),
};

static struct osmo_dgram_conn *conn;
static struct osmo_rtp_handle *rtp;

int read_cb(struct osmo_dgram_conn *conn)
{
	struct msgb *msg;
	char dummy_data[RTP_PT_GSM_FULL_PAYLOAD_LEN] = "payload test";
	int payload_type;

	LOGP(DRTP_TEST, LOGL_DEBUG, "received message from datagram\n");

	msg = msgb_alloc(RTP_MSGB_SIZE, "RTP/test");
	if (msg == NULL) {
		LOGP(DRTP_TEST, LOGL_ERROR, "cannot allocate message\n");
		return -1;
	}
	if (osmo_dgram_conn_recv(conn, msg) < 0) {
		LOGP(DRTP_TEST, LOGL_ERROR, "cannot receive message\n");
		return -1;
	}
	payload_type = osmo_rtp_parse(rtp, msg);
	if (payload_type < 0) {
		msgb_free(msg);
		LOGP(DRTP_TEST, LOGL_ERROR, "cannot parse RTP message\n");
		return -1;
	}
	LOGP(DLINP, LOGL_DEBUG, "received message with RTP payload type: %d "
		"and size: %d (%s)\n", payload_type, msg->len, msg->data);

	/*
	 * ... now build gsm_data_frame, set callref and msg_type based
	 * on the rtp payload type (map RTP_PT_GSM_FULL to GSM_THCF_FRAME).
	 * Then, pass it to the RSL layer.
	 */

	msg = msgb_alloc(1200, "RTP/test");
	if (msg == NULL) {
		LOGP(DRTP_TEST, LOGL_ERROR, "cannot allocate message\n");
		return -1;
	}

	/* build reply. */
	msg = osmo_rtp_build(rtp, RTP_PT_GSM_FULL,
			     RTP_PT_GSM_FULL_PAYLOAD_LEN,
			     dummy_data, RTP_PT_GSM_FULL_DURATION);
	if (msg == NULL) {
		LOGP(DLINP, LOGL_ERROR, "OOM\n");
		return -1;
	}
	osmo_dgram_conn_send(conn, msg);

	return 0;
}

void sighandler(int foo)
{
	LOGP(DLINP, LOGL_NOTICE, "closing RTP.\n");
	osmo_dgram_conn_close(conn);
	osmo_dgram_conn_destroy(conn);
	osmo_rtp_handle_free(rtp);
	exit(EXIT_SUCCESS);
}

static void *tall_test;

int main(int argc, char *argv[])
{
	signal(SIGINT, sighandler);

	tall_test = talloc_named_const(NULL, 1, "udp_rtp_test");

	osmo_init_logging(&rtp_test_log_info);
	log_set_log_level(osmo_stderr_target, LOGL_DEBUG);

	/*
	 * initialize RTP handler.
	 */
	rtp = osmo_rtp_handle_create(tall_test);
	if (rtp == NULL) {
		LOGP(DRTP_TEST, LOGL_ERROR, "Error init RTP handler\n");
		exit(EXIT_FAILURE);
	}
	osmo_rtp_handle_tx_set_sequence(rtp, random());
	osmo_rtp_handle_tx_set_ssrc(rtp, random());
	osmo_rtp_handle_tx_set_timestamp(rtp, time(NULL));

	/*
	 * initialize datagram server.
	 */

	conn = osmo_dgram_conn_create(tall_test);
	if (conn == NULL) {
		LOGP(DRTP_TEST, LOGL_ERROR, "cannot create UDP socket\n");
		exit(EXIT_FAILURE);
	}
	osmo_dgram_conn_set_local_addr(conn, "127.0.0.1");
	osmo_dgram_conn_set_local_port(conn, 20000);
	osmo_dgram_conn_set_remote_addr(conn, "127.0.0.1");
	osmo_dgram_conn_set_remote_port(conn, 20001);
	osmo_dgram_conn_set_read_cb(conn, read_cb);

	if (osmo_dgram_conn_open(conn) < 0) {
		fprintf(stderr, "cannot open client\n");
		exit(EXIT_FAILURE);
	}

	LOGP(DRTP_TEST, LOGL_NOTICE, "Entering main loop\n");

	while(1) {
		osmo_select_main(0);
	}
}