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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <arpa/inet.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/osmux.h>
#include <osmocom/netif/datagram.h>
#define DOSMUX_TEST 0
struct log_info_cat osmux_test_cat[] = {
[DOSMUX_TEST] = {
.name = "DOSMUX_TEST",
.description = "osmux test input",
.color = "\033[1;35m",
.enabled = 1, .loglevel = LOGL_DEBUG,
},
};
const struct log_info osmux_test_log_info = {
.filter_fn = NULL,
.cat = osmux_test_cat,
.num_cat = ARRAY_SIZE(osmux_test_cat),
};
static int fd;
static void amr_open(void)
{
fd = open("/tmp/input.amr", O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0) {
perror("open");
exit(EXIT_FAILURE);
}
write(fd, "#!AMR\n", strlen("#!AMR\n"));
}
static void amr_close(void)
{
close(fd);
}
static void amr_write(struct msgb *msg)
{
struct rtp_hdr *rtph;
void *amr;
unsigned int len;
/* as described by rfc4867, see page 35 */
rtph = osmo_rtp_get_hdr(msg);
amr = osmo_rtp_get_payload(rtph, msg, &len);
write(fd, (uint8_t *)amr + 1, len - 1);
}
static struct osmo_dgram *conn;
static struct osmo_rtp_handle *rtp;
static void osmux_deliver(struct msgb *batch_msg, void *data)
{
char buf[1024];
osmux_snprintf(buf, sizeof(buf), batch_msg);
LOGP(DOSMUX_TEST, LOGL_DEBUG, "sending batch (len=%d): %s\n",
batch_msg->len, buf);
osmo_dgram_send(conn, batch_msg);
}
/*
* This is the input handle for osmux. It stores the last osmux sequence that
* has been used and the deliver function that sends the osmux batch.
*/
struct osmux_in_handle *h_input;
#define MAX_CONCURRENT_CALLS 8
static int ccid[MAX_CONCURRENT_CALLS] = { -1, -1, -1, -1, -1, -1, -1, -1 };
static int get_ccid(uint32_t ssrc)
{
int i, found = 0;
for (i=0; i<MAX_CONCURRENT_CALLS; i++) {
if (ccid[i] == ssrc) {
found = 1;
break;
}
}
return found ? i : -1;
}
static void register_ccid(uint32_t ssrc)
{
int i, found = 0;
for (i=0; i<MAX_CONCURRENT_CALLS; i++) {
if (ccid[i] == ssrc)
continue;
if (ccid[i] < 0) {
found = 1;
break;
}
}
if (found) {
ccid[i] = ssrc;
LOGP(DOSMUX_TEST, LOGL_DEBUG, "mapping ssrc=%u to ccid=%d\n",
ntohl(ssrc), i);
} else {
LOGP(DOSMUX_TEST, LOGL_ERROR, "cannot map ssrc to ccid!\n");
}
}
int read_cb(struct osmo_dgram *conn)
{
struct msgb *msg;
struct rtp_hdr *rtph;
int ret, ccid;
msg = msgb_alloc(RTP_MSGB_SIZE, "OSMUX/test");
if (msg == NULL) {
LOGP(DOSMUX_TEST, LOGL_ERROR, "cannot allocate message\n");
return -1;
}
if (osmo_dgram_recv(conn, msg) < 0) {
LOGP(DOSMUX_TEST, LOGL_ERROR, "cannot receive message\n");
return -1;
}
rtph = osmo_rtp_get_hdr(msg);
if (rtph == NULL) {
msgb_free(msg);
LOGP(DOSMUX_TEST, LOGL_ERROR, "cannot parse RTP message\n");
return -1;
}
LOGP(DOSMUX_TEST, LOGL_DEBUG, "received message with RTP payload type: %d\n",
rtph->payload_type);
if (rtph->payload_type == RTP_PT_AMR)
amr_write(msg);
char buf[1024];
osmo_rtp_snprintf(buf, sizeof(buf), msg);
LOGP(DOSMUX_TEST, LOGL_DEBUG, "received RTP (len=%d): %s\n", msg->len, buf);
ccid = get_ccid(rtph->ssrc);
if (ccid < 0)
register_ccid(rtph->ssrc);
while ((ret = osmux_xfrm_input(h_input, msg, ccid)) > 0) {
/* batch full, deliver it */
osmux_xfrm_input_deliver(h_input);
}
if (ret == -1)
printf("something is wrong\n");
return 0;
}
void sighandler(int foo)
{
LOGP(DOSMUX_TEST, LOGL_NOTICE, "closing test.\n");
osmo_dgram_close(conn);
osmo_dgram_destroy(conn);
osmo_rtp_handle_free(rtp);
amr_close();
exit(EXIT_SUCCESS);
}
static void *tall_test;
/*
* This is the output handle for osmux, it stores last RTP sequence and
* timestamp that has been used. There should be one per circuit ID.
*/
int main(int argc, char *argv[])
{
amr_open();
signal(SIGINT, sighandler);
tall_test = talloc_named_const(NULL, 1, "osmux_test");
msgb_talloc_ctx_init(tall_test, 0);
osmo_init_logging2(tall_test, &osmux_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(DOSMUX_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 OSMUX handlers.
*/
h_input = osmux_xfrm_input_alloc(tall_test);
osmux_xfrm_input_set_initial_seqnum(h_input, 0);
osmux_xfrm_input_set_batch_factor(h_input, 4);
osmux_xfrm_input_set_deliver_cb(h_input, osmux_deliver, NULL);
/*
* initialize datagram server.
*/
conn = osmo_dgram_create(tall_test);
if (conn == NULL) {
LOGP(DOSMUX_TEST, LOGL_ERROR, "cannot create UDP socket\n");
exit(EXIT_FAILURE);
}
osmo_dgram_set_local_addr(conn, "127.0.0.1");
osmo_dgram_set_local_port(conn, 20000);
osmo_dgram_set_remote_addr(conn, "127.0.0.1");
osmo_dgram_set_remote_port(conn, 20001);
osmo_dgram_set_read_cb(conn, read_cb);
if (osmo_dgram_open(conn) < 0) {
fprintf(stderr, "cannot open client\n");
exit(EXIT_FAILURE);
}
LOGP(DOSMUX_TEST, LOGL_NOTICE, "Entering main loop\n");
while(1) {
osmo_select_main(0);
}
}
|