2013-02-11 10:34:58 +00:00
|
|
|
/* 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 <unistd.h>
|
|
|
|
#include <string.h>
|
2017-09-09 09:10:08 +00:00
|
|
|
#include <talloc.h>
|
2013-02-11 10:34:58 +00:00
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2017-09-07 13:36:56 +00:00
|
|
|
#include <osmocom/gapk/logging.h>
|
2017-08-30 13:26:02 +00:00
|
|
|
#include <osmocom/gapk/codecs.h>
|
|
|
|
#include <osmocom/gapk/formats.h>
|
|
|
|
#include <osmocom/gapk/procqueue.h>
|
2013-02-11 10:34:58 +00:00
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2017-09-07 13:36:56 +00:00
|
|
|
#define rtp_err(err_msg, args...) \
|
|
|
|
LOGPGAPK(LOGL_ERROR, "%s():" err_msg, __func__, ## args)
|
2013-02-11 10:34:58 +00:00
|
|
|
|
|
|
|
static int
|
2017-05-28 08:20:54 +00:00
|
|
|
pq_cb_rtp_input(void *_state, uint8_t *out, const uint8_t *in, unsigned int in_len)
|
2013-02-11 10:34:58 +00:00
|
|
|
{
|
|
|
|
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));
|
2017-05-27 23:55:31 +00:00
|
|
|
if (rv <= 0) {
|
2017-05-28 17:43:21 +00:00
|
|
|
rtp_err("error during read\n");
|
2013-02-11 10:34:58 +00:00
|
|
|
return -1;
|
2017-05-27 23:55:31 +00:00
|
|
|
}
|
2013-02-11 10:34:58 +00:00
|
|
|
|
2017-05-27 23:55:31 +00:00
|
|
|
if (rv < sizeof(struct rtp_hdr)) {
|
2017-05-28 17:43:21 +00:00
|
|
|
rtp_err("%d smaller than rtp header\n", rv);
|
2013-02-11 10:34:58 +00:00
|
|
|
return -1;
|
2017-05-27 23:55:31 +00:00
|
|
|
}
|
2013-02-11 10:34:58 +00:00
|
|
|
|
2017-05-27 23:55:31 +00:00
|
|
|
if (rtph->version != RTP_VERSION) {
|
2017-05-28 17:43:21 +00:00
|
|
|
rtp_err("unknown RTP version %u\n", rtph->version);
|
2013-02-11 10:34:58 +00:00
|
|
|
return -1;
|
2017-05-27 23:55:31 +00:00
|
|
|
}
|
2013-02-11 10:34:58 +00:00
|
|
|
|
|
|
|
payload = buf + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
|
|
|
|
payload_len = rv - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
|
2017-05-27 23:55:31 +00:00
|
|
|
if (payload_len < 0) {
|
2019-07-17 20:45:14 +00:00
|
|
|
rtp_err("non-existent RTP payload length %d\n", payload_len);
|
2013-02-11 10:34:58 +00:00
|
|
|
return -1;
|
2017-05-27 23:55:31 +00:00
|
|
|
}
|
2013-02-11 10:34:58 +00:00
|
|
|
|
|
|
|
if (rtph->extension) {
|
2017-05-27 23:55:31 +00:00
|
|
|
if (payload_len < sizeof(struct rtp_x_hdr)) {
|
2017-05-28 17:43:21 +00:00
|
|
|
rtp_err("short extension header: %d\n", payload_len);
|
2013-02-11 10:34:58 +00:00
|
|
|
return -1;
|
2017-05-27 23:55:31 +00:00
|
|
|
}
|
2013-02-11 10:34:58 +00:00
|
|
|
rtpxh = (struct rtp_x_hdr *)payload;
|
|
|
|
x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
|
|
|
|
payload += x_len;
|
|
|
|
payload_len -= x_len;
|
2017-05-27 23:55:31 +00:00
|
|
|
if (payload_len < 0) {
|
2017-05-28 17:43:21 +00:00
|
|
|
rtp_err("short RTP payload length %d\n", payload_len);
|
2013-02-11 10:34:58 +00:00
|
|
|
return -1;
|
2017-05-27 23:55:31 +00:00
|
|
|
}
|
2013-02-11 10:34:58 +00:00
|
|
|
}
|
|
|
|
if (rtph->padding) {
|
2017-05-27 23:55:31 +00:00
|
|
|
if (payload_len < 0) {
|
2017-05-28 17:43:21 +00:00
|
|
|
rtp_err("padding but no payload length %d\n", payload_len);
|
2013-02-11 10:34:58 +00:00
|
|
|
return -1;
|
2017-05-27 23:55:31 +00:00
|
|
|
}
|
2013-02-11 10:34:58 +00:00
|
|
|
payload_len -= payload[payload_len -1];
|
2017-05-27 23:55:31 +00:00
|
|
|
if (payload_len < 0) {
|
2017-05-28 17:43:21 +00:00
|
|
|
rtp_err("no payload left after padding %d\n", payload_len);
|
2013-02-11 10:34:58 +00:00
|
|
|
return -1;
|
2017-05-27 23:55:31 +00:00
|
|
|
}
|
2013-02-11 10:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
state->ssrc = ntohl(rtph->ssrc);
|
|
|
|
state->timestamp = ntohl(rtph->timestamp);
|
|
|
|
state->sequence = ntohs(rtph->sequence);
|
|
|
|
/* FIXME: check for discontinuity, ... */
|
|
|
|
|
|
|
|
memcpy(out, payload, payload_len);
|
|
|
|
|
2017-05-28 08:20:54 +00:00
|
|
|
return payload_len;
|
2013-02-11 10:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2017-05-28 08:20:54 +00:00
|
|
|
pq_cb_rtp_output(void *_state, uint8_t *out, const uint8_t *in, unsigned int in_len)
|
2013-02-11 10:34:58 +00:00
|
|
|
{
|
|
|
|
struct pq_state_rtp *state = _state;
|
2017-05-28 08:20:54 +00:00
|
|
|
int len = in_len + sizeof(struct rtp_hdr);
|
2013-02-11 10:34:58 +00:00
|
|
|
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);
|
2017-05-28 08:20:54 +00:00
|
|
|
memcpy(payload, in, in_len);
|
2013-02-11 10:34:58 +00:00
|
|
|
|
|
|
|
rv = write(state->fd, buf, len);
|
|
|
|
return rv == len ? 0 : -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pq_cb_rtp_exit(void *_state)
|
|
|
|
{
|
2017-09-09 09:10:08 +00:00
|
|
|
talloc_free(_state);
|
2013-02-11 10:34:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2018-07-09 13:09:50 +00:00
|
|
|
pq_queue_rtp_op(struct osmo_gapk_pq *pq, int udp_fd,
|
|
|
|
unsigned int blk_len, int in_out_n, uint8_t pt)
|
2013-02-11 10:34:58 +00:00
|
|
|
{
|
2017-08-31 10:22:56 +00:00
|
|
|
struct osmo_gapk_pq_item *item;
|
2013-02-11 10:34:58 +00:00
|
|
|
struct pq_state_rtp *state;
|
|
|
|
|
2017-09-09 09:10:08 +00:00
|
|
|
state = talloc_zero(pq, struct pq_state_rtp);
|
2013-02-11 10:34:58 +00:00
|
|
|
if (!state)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
state->fd = udp_fd;
|
|
|
|
state->blk_len = blk_len;
|
|
|
|
|
2017-05-27 14:42:14 +00:00
|
|
|
/* as we're working in GSM, the sample clock is 8000 Hz and we
|
|
|
|
* operate at 50 Hz (20ms) codec frames; 8000/50 = 160 samples
|
|
|
|
* per RTP frame */
|
2013-02-11 10:34:58 +00:00
|
|
|
state->duration = 160;
|
|
|
|
|
2018-07-09 13:09:50 +00:00
|
|
|
/**
|
|
|
|
* RTP payload type according to RFC 3551,
|
|
|
|
* section "6. Payload Type Definitions".
|
|
|
|
*
|
|
|
|
* Only GSM FR has a static payload type value (see table 4).
|
|
|
|
* For other codecs the payload type may be negotiated
|
|
|
|
* between the both sides dynamically (i.e. in range 96-127).
|
|
|
|
*/
|
|
|
|
state->payload_type = pt;
|
|
|
|
|
2013-02-11 10:34:58 +00:00
|
|
|
if (in_out_n == 0) {
|
|
|
|
state->ssrc = rand();
|
|
|
|
state->sequence = random();
|
|
|
|
state->timestamp = random();
|
|
|
|
}
|
|
|
|
|
2017-08-31 10:22:56 +00:00
|
|
|
item = osmo_gapk_pq_add_item(pq);
|
2013-02-11 10:34:58 +00:00
|
|
|
if (!item) {
|
2017-09-09 09:10:08 +00:00
|
|
|
talloc_free(state);
|
2013-02-11 10:34:58 +00:00
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
2017-09-09 17:57:13 +00:00
|
|
|
item->type = in_out_n ?
|
|
|
|
OSMO_GAPK_ITEM_TYPE_SOURCE : OSMO_GAPK_ITEM_TYPE_SINK;
|
2018-01-14 18:25:44 +00:00
|
|
|
item->cat_name = in_out_n ?
|
|
|
|
OSMO_GAPK_CAT_NAME_SOURCE : OSMO_GAPK_CAT_NAME_SINK;
|
2017-09-09 18:44:16 +00:00
|
|
|
item->sub_name = "rtp";
|
2017-09-09 17:57:13 +00:00
|
|
|
|
2013-02-11 10:34:58 +00:00
|
|
|
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;
|
2017-09-07 15:42:49 +00:00
|
|
|
item->wait = NULL;
|
2013-02-11 10:34:58 +00:00
|
|
|
item->exit = pq_cb_rtp_exit;
|
|
|
|
|
2017-09-09 09:10:08 +00:00
|
|
|
/* Change state's talloc context from pq to item */
|
|
|
|
talloc_steal(item, state);
|
|
|
|
|
2013-02-11 10:34:58 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-27 14:42:14 +00:00
|
|
|
/*! Add RTP input to processing queue.
|
|
|
|
* This typically only makes sense as first item in the queue
|
|
|
|
* \param pq Processing Queue to add this RTP input to
|
|
|
|
* \param[in] udp_fd UDP file descriptor for the RTP input
|
2018-07-09 13:09:50 +00:00
|
|
|
* \param[in] blk_len Block Length to read from RTP
|
|
|
|
* \param[in] pt Payload type according to RFC 3551
|
|
|
|
*/
|
2013-02-11 10:34:58 +00:00
|
|
|
int
|
2018-07-09 13:09:50 +00:00
|
|
|
osmo_gapk_pq_queue_rtp_input(struct osmo_gapk_pq *pq, int udp_fd,
|
|
|
|
unsigned int blk_len, uint8_t pt)
|
2013-02-11 10:34:58 +00:00
|
|
|
{
|
2017-09-10 13:27:04 +00:00
|
|
|
LOGPGAPK(LOGL_DEBUG, "PQ '%s': Adding RTP input (blk_len=%u)\n",
|
|
|
|
pq->name, blk_len);
|
2018-07-09 13:09:50 +00:00
|
|
|
return pq_queue_rtp_op(pq, udp_fd, blk_len, 1, pt);
|
2013-02-11 10:34:58 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 14:42:14 +00:00
|
|
|
/*! Add RTP output to processing queue.
|
|
|
|
* This typically only makes sense as last item in the queue
|
|
|
|
* \param pq Processing Queue to add this RTP output to
|
|
|
|
* \param[in] udp_fd UDP file descriptor for the RTP output
|
2018-07-09 13:09:50 +00:00
|
|
|
* \param[in] blk_len Block Length to read from RTP
|
|
|
|
* \param[in] pt Payload type according to RFC 3551
|
|
|
|
*/
|
2013-02-11 10:34:58 +00:00
|
|
|
int
|
2018-07-09 13:09:50 +00:00
|
|
|
osmo_gapk_pq_queue_rtp_output(struct osmo_gapk_pq *pq, int udp_fd,
|
|
|
|
unsigned int blk_len, uint8_t pt)
|
2013-02-11 10:34:58 +00:00
|
|
|
{
|
2017-09-10 13:27:04 +00:00
|
|
|
LOGPGAPK(LOGL_DEBUG, "PQ '%s': Adding RTP output (blk_len=%u)\n",
|
|
|
|
pq->name, blk_len);
|
2018-07-09 13:09:50 +00:00
|
|
|
return pq_queue_rtp_op(pq, udp_fd, blk_len, 0, pt);
|
2013-02-11 10:34:58 +00:00
|
|
|
}
|