gapk: Add support for RTP input/output streams

Instead of having only file-based I/O, this enables gapk to receive and
send RTP streams, e.g. from live GSM network equipment like
sysmoBTS/nanoBTS.

Support is currently simplistic.  On transmit, there is hard-coded codec
type of full-rate GSM.  On receive-side, we should auto-detect the
format based on frame size and/or payload type, but we don't do that yet
at all.
This commit is contained in:
Harald Welte 2013-02-11 11:34:58 +01:00
parent f7f0c91ca8
commit ce94d971e1
5 changed files with 349 additions and 13 deletions

View File

@ -49,6 +49,7 @@ AC_PROG_LIBTOOL
# Checks for libraries.
# libosmocore (codec module)
PKG_CHECK_MODULES(LIBOSMOCORE, libosmocore >= 0.3.2)
PKG_CHECK_MODULES(LIBOSMOCODEC, libosmocodec >= 0.1.25)
# opencore-amrnb for AMR and EFR decoding

View File

@ -44,6 +44,11 @@ int pq_execute(struct pq *pq);
int pq_queue_file_input(struct pq *pq, FILE *src, unsigned int block_len);
int pq_queue_file_output(struct pq *pq, FILE *dst, unsigned int block_len);
/* RTP */
int pq_queue_rtp_input(struct pq *pq, int rtp_fd, unsigned int block_len);
int pq_queue_rtp_output(struct pq *pq, int rtp_fd, unsigned int block_len);
/* Format */
struct format_desc;
int pq_queue_fmt_convert(struct pq *pq, const struct format_desc *fmt, int to_from_n);

View File

@ -1,14 +1,18 @@
INCLUDES = $(all_includes) -I$(top_srcdir)/include -I$(top_builddir)
AM_CFLAGS=-Wall $(LIBOSMOCODEC_CFLAGS) ${OPENCORE_AMRNB_CFLAGS}
AM_LDFLAGS=$(LIBOSMOCODEC_LIBS) ${OPENCORE_AMRNB_LIBS} ${LIBGSM_LIBS}
AM_CFLAGS=-Wall $(LIBOSMOCODEC_CFLAGS) $(LIBOSMOCORE_CFLAGS) \
${OPENCORE_AMRNB_CFLAGS}
AM_LDFLAGS=$(LIBOSMOCODEC_LIBS) $(LIBOSMOCORE_LIBS) \
${OPENCORE_AMRNB_LIBS} ${LIBGSM_LIBS}
bin_PROGRAMS = gapk
gapk_SOURCES = main.c \
procqueue.c pq_file.c pq_format.c pq_codec.c \
COM_SOURCES = procqueue.c pq_file.c pq_format.c pq_codec.c pq_rtp.c \
formats.c fmt_amr.c fmt_gsm.c fmt_hr_ref.c fmt_racal.c \
fmt_rawpcm.c fmt_ti.c \
codecs.c codec_pcm.c codec_hr.c codec_fr.c codec_efr.c
bin_PROGRAMS = gapk
gapk_SOURCES = main.c $(COM_SOURCES)
if ENABLE_GSMHR
gapk_LDADD = $(top_builddir)/libgsmhr/libgsmhr.la
endif

View File

@ -25,6 +25,11 @@
#include <unistd.h>
#include <getopt.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <osmocom/core/socket.h>
#include <gapk/codecs.h>
#include <gapk/formats.h>
#include <gapk/procqueue.h>
@ -33,9 +38,17 @@
struct gapk_options
{
const char *fname_in;
struct {
const char *hostname;
uint16_t port;
} rtp_in;
const struct format_desc *fmt_in;
const char *fname_out;
struct {
const char *hostname;
uint16_t port;
} rtp_out;
const struct format_desc *fmt_out;
};
@ -45,7 +58,7 @@ struct gapk_state
struct pq *pq;
union {
struct {
struct {
FILE *fh;
} file;
@ -54,7 +67,7 @@ struct gapk_state
} rtp;
} in;
union {
struct {
struct {
FILE *fh;
} file;
@ -75,7 +88,9 @@ print_help(char *progname)
fprintf(stdout, "\n");
fprintf(stdout, "Options:\n");
fprintf(stdout, " -i, --input-file=FILE\t\tInput file\n");
fprintf(stdout, " -I, --input-rtp=HOST/PORT\t\tInput RTP stream\n");
fprintf(stdout, " -o, --output-file=FILE\tOutput file\n");
fprintf(stdout, " -O, --output-rtp=HOST/PORT\tOutput RTP stream\n");
fprintf(stdout, " -f, --input-format=FMT\tInput format (see below)\n");
fprintf(stdout, " -g, --output-format=FMT\tOutput format (see below)\n");
fprintf(stdout, "\n");
@ -113,17 +128,40 @@ print_help(char *progname)
fprintf(stdout, "\n");
}
static int
parse_host_port(const char *host_port, const char **host)
{
char *dup = strdup(host_port);
char *tok;
if (!dup)
return -ENOMEM;
tok = strtok(dup, "/");
if (!tok)
return -EINVAL;
*host = tok;
tok = strtok(NULL, "/");
if (!tok)
return -EINVAL;
return atoi(tok);
}
static int
parse_options(struct gapk_state *state, int argc, char *argv[])
{
const struct option long_options[] = {
{"input-file", 1, 0, 'i'},
{"output-file", 1, 0, 'o'},
{"input-rtp", 1, 0, 'I'},
{"output-rtp", 1, 0, 'O'},
{"input-format", 1, 0, 'f'},
{"output-format", 1, 0, 'g'},
{"help", 0, 0, 'h'},
};
const char *short_options = "i:o:f:g:h";
const char *short_options = "i:o:I:O:f:g:h";
struct gapk_options *opt = &state->opts;
@ -132,7 +170,7 @@ parse_options(struct gapk_state *state, int argc, char *argv[])
/* Parse */
while (1) {
int c;
int c, rv;
int opt_idx;
c = getopt_long(
@ -149,6 +187,24 @@ parse_options(struct gapk_state *state, int argc, char *argv[])
opt->fname_out = optarg;
break;
case 'I':
rv = parse_host_port(optarg, &opt->rtp_in.hostname);
if (rv < 0 || rv > 0xffff) {
fprintf(stderr, "[!] Invalid port: %d\n", rv);
return -EINVAL;
}
opt->rtp_in.port = rv;
break;
case 'O':
rv = parse_host_port(optarg, &opt->rtp_out.hostname);
if (rv < 0 || rv > 0xffff) {
fprintf(stderr, "[!] Invalid port: %d\n", rv);
return -EINVAL;
}
opt->rtp_out.port = rv;
break;
case 'f':
opt->fmt_in = fmt_get_from_name(optarg);
if (!opt->fmt_in) {
@ -214,6 +270,18 @@ check_options(struct gapk_state *gs)
}
}
/* Input combinations */
if (gs->opts.fname_in && gs->opts.rtp_in.port) {
fprintf(stderr, "[!] You have to decide on either file or RTP input\n");
return -EINVAL;
}
/* Output combinations */
if (gs->opts.fname_out && gs->opts.rtp_out.port) {
fprintf(stderr, "[!] You have to decide on either file or RTP output\n");
return -EINVAL;
}
return 0;
}
@ -227,6 +295,16 @@ files_open(struct gapk_state *gs)
perror("fopen");
return -errno;
}
} else if (gs->opts.rtp_in.port) {
gs->in.rtp.fd = osmo_sock_init(AF_UNSPEC, SOCK_DGRAM,
IPPROTO_UDP,
gs->opts.rtp_in.hostname,
gs->opts.rtp_in.port,
OSMO_SOCK_F_BIND);
if (gs->in.rtp.fd < 0) {
fprintf(stderr, "[!] Error while opening input socket\n");
return gs->in.rtp.fd;
}
} else
gs->in.file.fh = stdin;
@ -237,6 +315,16 @@ files_open(struct gapk_state *gs)
perror("fopen");
return -errno;
}
} else if (gs->opts.rtp_out.port) {
gs->out.rtp.fd = osmo_sock_init(AF_UNSPEC, SOCK_DGRAM,
IPPROTO_UDP,
gs->opts.rtp_out.hostname,
gs->opts.rtp_out.port,
OSMO_SOCK_F_CONNECT);
if (gs->out.rtp.fd < 0) {
fprintf(stderr, "[!] Error while opening output socket\n");
return gs->out.rtp.fd;
}
} else
gs->out.file.fh = stdout;
@ -248,8 +336,12 @@ files_close(struct gapk_state *gs)
{
if (gs->in.file.fh && gs->in.file.fh != stdin)
fclose(gs->in.file.fh);
if (gs->in.rtp.fd >= 0)
close(gs->in.rtp.fd);
if (gs->out.file.fh && gs->out.file.fh != stdout)
fclose(gs->out.file.fh);
if (gs->out.rtp.fd >= 0)
close(gs->out.rtp.fd);
}
static int
@ -260,7 +352,7 @@ handle_headers(struct gapk_state *gs)
/* Input file header (remove & check it) */
len = gs->opts.fmt_in->header_len;
if (len) {
if (len && gs->in.file.fh) {
uint8_t *buf;
buf = malloc(len);
@ -281,7 +373,7 @@ handle_headers(struct gapk_state *gs)
/* Output file header (write it) */
len = gs->opts.fmt_out->header_len;
if (len) {
if (len && gs->out.file.fh) {
rv = fwrite(gs->opts.fmt_out->header, len, 1, gs->out.file.fh);
if (rv != 1)
return -ENOSPC;
@ -310,7 +402,10 @@ make_processing_chain(struct gapk_state *gs)
(fmt_out->codec_type != fmt_in->codec_type);
/* File read */
pq_queue_file_input(gs->pq, gs->in.file.fh, fmt_in->frame_len);
if (gs->in.file.fh)
pq_queue_file_input(gs->pq, gs->in.file.fh, fmt_in->frame_len);
else
pq_queue_rtp_input(gs->pq, gs->in.rtp.fd, fmt_in->frame_len);
/* Decoding to PCM ? */
if (need_dec)
@ -363,7 +458,10 @@ make_processing_chain(struct gapk_state *gs)
}
/* File write */
pq_queue_file_output(gs->pq, gs->out.file.fh, fmt_out->frame_len);
if (gs->out.file.fh)
pq_queue_file_output(gs->pq, gs->out.file.fh, fmt_out->frame_len);
else
pq_queue_rtp_output(gs->pq, gs->out.rtp.fd, fmt_out->frame_len);
return 0;
}
@ -391,6 +489,8 @@ int main(int argc, char *argv[])
/* Clear state */
memset(gs, 0x00, sizeof(struct gapk_state));
gs->in.rtp.fd = -1;
gs->out.rtp.fd = -1;
/* Parse / check options */
rv = parse_options(gs, argc, argv);

226
src/pq_rtp.c Normal file
View File

@ -0,0 +1,226 @@
/* Process Queue: RTP handling tasks */
/* (C) 2013 by Harald Welte <laforge@gnumonks.org>
*
* This file is part of gapk (GSM Audio Pocket Knife).
*
* gapk 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 3 of the License, or
* (at your option) any later version.
*
* gapk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with gapk. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <gapk/codecs.h>
#include <gapk/formats.h>
#include <gapk/procqueue.h>
#ifndef __BYTE_ORDER
# ifdef __APPLE__
# define __BYTE_ORDER __DARWIN_BYTE_ORDER
# define __LITTLE_ENDIAN __DARWIN_LITTLE_ENDIAN
# define __BIG_ENDIAN __DARWIN_BIG_ENDIAN
# else
# error "__BYTE_ORDER should be defined by someone"
# endif
#endif
/* according to RFC 3550 */
struct rtp_hdr {
#if __BYTE_ORDER == __LITTLE_ENDIAN
uint8_t csrc_count:4,
extension:1,
padding:1,
version:2;
uint8_t payload_type:7,
marker:1;
#elif __BYTE_ORDER == __BIG_ENDIAN
uint8_t version:2,
padding:1,
extension:1,
csrc_count:4;
uint8_t marker:1,
payload_type:7;
#endif
uint16_t sequence;
uint32_t timestamp;
uint32_t ssrc;
} __attribute__((packed));
struct rtp_x_hdr {
uint16_t by_profile;
uint16_t length;
} __attribute__((packed));
#define RTP_VERSION 2
#define RTP_PT_GSM_FULL 3
struct pq_state_rtp {
int fd;
int blk_len;
/* configurable */
uint32_t duration;
uint8_t payload_type;
/* auto-increment */
uint16_t sequence;
uint32_t timestamp;
uint32_t ssrc;
};
static int
pq_cb_rtp_input(void *_state, uint8_t *out, const uint8_t *in)
{
struct pq_state_rtp *state = _state;
uint8_t buf[state->blk_len+256];
struct rtp_hdr *rtph = (struct rtp_hdr *)buf;
struct rtp_x_hdr *rtpxh;
uint8_t *payload;
int rv, x_len, payload_len;
rv = read(state->fd, buf, sizeof(buf));
if (rv <= 0)
return -1;
if (rv < sizeof(struct rtp_hdr))
return -1;
if (rtph->version != RTP_VERSION)
return -1;
payload = buf + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
payload_len = rv - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
if (payload_len < 0)
return -1;
if (rtph->extension) {
if (payload_len < sizeof(struct rtp_x_hdr))
return -1;
rtpxh = (struct rtp_x_hdr *)payload;
x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
payload += x_len;
payload_len -= x_len;
if (payload_len < 0)
return -1;
}
if (rtph->padding) {
if (payload_len < 0)
return -1;
payload_len -= payload[payload_len -1];
if (payload_len < 0)
return -1;
}
state->ssrc = ntohl(rtph->ssrc);
state->timestamp = ntohl(rtph->timestamp);
state->sequence = ntohs(rtph->sequence);
/* FIXME: check for discontinuity, ... */
memcpy(out, payload, payload_len);
return 0;
}
static int
pq_cb_rtp_output(void *_state, uint8_t *out, const uint8_t *in)
{
struct pq_state_rtp *state = _state;
int len = state->blk_len + sizeof(struct rtp_hdr);
uint8_t buf[len];
struct rtp_hdr *rtph = (struct rtp_hdr *)buf;
uint8_t *payload;
int rv;
rtph->version = RTP_VERSION;
rtph->padding = 0;
rtph->extension = 0;
rtph->csrc_count = 0;
rtph->marker = 0;
rtph->payload_type = state->payload_type;
rtph->sequence = htons(state->sequence++);
rtph->timestamp = htonl(state->timestamp);
state->timestamp += state->duration;
rtph->ssrc = htonl(state->ssrc);
payload = buf + sizeof(*rtph);
memcpy(payload, in, state->blk_len);
rv = write(state->fd, buf, len);
return rv == len ? 0 : -1;
}
static void
pq_cb_rtp_exit(void *_state)
{
free(_state);
}
static int
pq_queue_rtp_op(struct pq *pq, int udp_fd, unsigned int blk_len, int in_out_n)
{
struct pq_item *item;
struct pq_state_rtp *state;
state = calloc(1, sizeof(struct pq_state_rtp));
if (!state)
return -ENOMEM;
state->fd = udp_fd;
state->blk_len = blk_len;
state->duration = 160;
if (in_out_n == 0) {
state->ssrc = rand();
state->sequence = random();
state->timestamp = random();
/* FIXME: other payload types!! */
state->payload_type = RTP_PT_GSM_FULL;
}
item = pq_add_item(pq);
if (!item) {
free(state);
return -ENOMEM;
}
item->len_in = in_out_n ? 0 : blk_len;
item->len_out = in_out_n ? blk_len : 0;
item->state = state;
item->proc = in_out_n ? pq_cb_rtp_input : pq_cb_rtp_output;
item->exit = pq_cb_rtp_exit;
return 0;
}
int
pq_queue_rtp_input(struct pq *pq, int udp_fd, unsigned int blk_len)
{
return pq_queue_rtp_op(pq, udp_fd, blk_len, 1);
}
int
pq_queue_rtp_output(struct pq *pq, int udp_fd, unsigned int blk_len)
{
return pq_queue_rtp_op(pq, udp_fd, blk_len, 0);
}