trx/tx.c

279 lines
6.4 KiB
C
Raw Permalink Normal View History

2012-07-27 13:50:03 +02:00
/*
2020-01-04 12:31:45 +01:00
* Copyright (C) 2020 Mark Hills <mark@xwax.org>
2012-07-27 13:50:03 +02:00
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, as published by the Free Software Foundation.
*
* This program 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 version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
2009-09-05 00:04:44 +02:00
#include <netdb.h>
#include <string.h>
#include <alsa/asoundlib.h>
2012-07-21 17:14:13 +02:00
#include <opus/opus.h>
2009-09-11 17:40:27 +02:00
#include <ortp/ortp.h>
2009-09-05 00:04:44 +02:00
#include <sys/socket.h>
#include <sys/types.h>
#include "defaults.h"
#include "device.h"
2012-07-21 14:32:40 +02:00
#include "notice.h"
2009-09-05 00:04:44 +02:00
#include "sched.h"
2009-09-11 17:40:27 +02:00
static unsigned int verbose = DEFAULT_VERBOSE;
2009-09-05 00:04:44 +02:00
2009-09-11 17:40:27 +02:00
static RtpSession* create_rtp_send(const char *addr_desc, const int port)
2009-09-05 00:04:44 +02:00
{
2009-09-11 17:40:27 +02:00
RtpSession *session;
2009-09-05 00:04:44 +02:00
2009-09-11 17:40:27 +02:00
session = rtp_session_new(RTP_SESSION_SENDONLY);
assert(session != NULL);
2009-09-05 00:04:44 +02:00
2009-09-11 17:40:27 +02:00
rtp_session_set_scheduling_mode(session, 0);
rtp_session_set_blocking_mode(session, 0);
rtp_session_set_connected_mode(session, FALSE);
if (rtp_session_set_remote_addr(session, addr_desc, port) != 0)
abort();
if (rtp_session_set_payload_type(session, 0) != 0)
abort();
if (rtp_session_set_multicast_ttl(session, 16) != 0)
abort();
2018-01-23 17:21:51 +01:00
if (rtp_session_set_dscp(session, 40) != 0)
abort();
2009-09-05 00:04:44 +02:00
2009-09-11 17:40:27 +02:00
return session;
2009-09-05 00:04:44 +02:00
}
2009-09-11 17:40:27 +02:00
static int send_one_frame(snd_pcm_t *snd,
2009-09-11 13:05:02 +02:00
const unsigned int channels,
2009-09-11 17:40:27 +02:00
const snd_pcm_uframes_t samples,
2012-07-21 17:14:13 +02:00
OpusEncoder *encoder,
2009-09-11 13:05:02 +02:00
const size_t bytes_per_frame,
2012-07-28 21:44:19 +02:00
const unsigned int ts_per_frame,
2009-09-11 17:40:27 +02:00
RtpSession *session)
2009-09-05 00:04:44 +02:00
{
2014-05-30 12:18:31 +02:00
int16_t *pcm;
2009-09-11 17:40:27 +02:00
void *packet;
2009-09-05 00:04:44 +02:00
ssize_t z;
snd_pcm_sframes_t f;
2012-07-28 21:44:19 +02:00
static unsigned int ts = 0;
2009-09-05 00:04:44 +02:00
2014-05-30 12:18:31 +02:00
pcm = alloca(sizeof(*pcm) * samples * channels);
2009-09-11 17:40:27 +02:00
packet = alloca(bytes_per_frame);
2009-09-05 00:04:44 +02:00
2009-09-11 13:05:02 +02:00
f = snd_pcm_readi(snd, pcm, samples);
2009-09-05 00:04:44 +02:00
if (f < 0) {
if (f == -ESTRPIPE)
ts = 0;
2014-03-23 19:49:21 +01:00
f = snd_pcm_recover(snd, f, 0);
if (f < 0) {
aerror("snd_pcm_readi", f);
return -1;
}
return 0;
2009-09-05 00:04:44 +02:00
}
2014-03-23 19:49:21 +01:00
/* Opus encoder requires a complete frame, so if we xrun
* mid-frame then we discard the incomplete audio. The next
* read will catch the error condition and recover */
if (f < samples) {
2009-09-05 00:04:44 +02:00
fprintf(stderr, "Short read, %ld\n", f);
2014-03-23 19:49:21 +01:00
return 0;
}
2009-09-05 00:04:44 +02:00
2014-05-30 12:18:31 +02:00
z = opus_encode(encoder, pcm, samples, packet, bytes_per_frame);
2009-09-05 00:04:44 +02:00
if (z < 0) {
2012-07-21 17:14:13 +02:00
fprintf(stderr, "opus_encode_float: %s\n", opus_strerror(z));
2009-09-05 00:04:44 +02:00
return -1;
}
2009-09-11 17:40:27 +02:00
rtp_session_send_with_ts(session, packet, z, ts);
2012-07-28 21:44:19 +02:00
ts += ts_per_frame;
2009-09-05 00:04:44 +02:00
return 0;
}
2009-09-11 13:05:02 +02:00
static int run_tx(snd_pcm_t *snd,
const unsigned int channels,
2009-09-11 17:40:27 +02:00
const snd_pcm_uframes_t frame,
2012-07-21 17:14:13 +02:00
OpusEncoder *encoder,
2009-09-11 13:05:02 +02:00
const size_t bytes_per_frame,
2012-07-28 21:44:19 +02:00
const unsigned int ts_per_frame,
2009-09-11 17:40:27 +02:00
RtpSession *session)
2009-09-05 00:04:44 +02:00
{
for (;;) {
int r;
2009-09-11 17:40:27 +02:00
r = send_one_frame(snd, channels, frame,
2012-07-28 21:44:19 +02:00
encoder, bytes_per_frame, ts_per_frame,
2009-09-11 17:40:27 +02:00
session);
2009-09-05 00:04:44 +02:00
if (r == -1)
return -1;
2009-09-11 17:40:27 +02:00
if (verbose > 1)
fputc('>', stderr);
2009-09-05 00:04:44 +02:00
}
}
static void usage(FILE *fd)
{
2012-07-27 13:46:03 +02:00
fprintf(fd, "Usage: tx [<parameters>]\n"
"Real-time audio transmitter over IP\n");
2009-09-05 00:04:44 +02:00
fprintf(fd, "\nAudio device (ALSA) parameters:\n");
2009-09-11 17:40:27 +02:00
fprintf(fd, " -d <dev> Device name (default '%s')\n",
2009-09-05 00:04:44 +02:00
DEFAULT_DEVICE);
2012-07-27 13:46:03 +02:00
fprintf(fd, " -m <ms> Buffer time (default %d milliseconds)\n",
2009-09-05 00:04:44 +02:00
DEFAULT_BUFFER);
fprintf(fd, "\nNetwork parameters:\n");
2009-09-11 17:40:27 +02:00
fprintf(fd, " -h <addr> IP address to send to (default %s)\n",
DEFAULT_ADDR);
fprintf(fd, " -p <port> UDP port number (default %d)\n",
DEFAULT_PORT);
2009-09-05 00:04:44 +02:00
fprintf(fd, "\nEncoding parameters:\n");
2012-07-27 13:46:03 +02:00
fprintf(fd, " -r <rate> Sample rate (default %dHz)\n",
2009-09-05 00:04:44 +02:00
DEFAULT_RATE);
2009-09-11 17:40:27 +02:00
fprintf(fd, " -c <n> Number of channels (default %d)\n",
2009-09-05 00:04:44 +02:00
DEFAULT_CHANNELS);
2012-07-27 13:46:03 +02:00
fprintf(fd, " -f <n> Frame size (default %d samples, see below)\n",
2009-09-11 17:40:27 +02:00
DEFAULT_FRAME);
fprintf(fd, " -b <kbps> Bitrate (approx., default %d)\n",
2009-09-05 00:04:44 +02:00
DEFAULT_BITRATE);
2009-09-11 17:40:27 +02:00
2014-10-22 22:21:14 +02:00
fprintf(fd, "\nProgram parameters:\n");
2009-09-11 17:40:27 +02:00
fprintf(fd, " -v <n> Verbosity level (default %d)\n",
DEFAULT_VERBOSE);
2014-10-22 22:21:14 +02:00
fprintf(fd, " -D <file> Run as a daemon, writing process ID to the given file\n");
2012-07-27 13:46:03 +02:00
fprintf(fd, "\nAllowed frame sizes (-f) are defined by the Opus codec. For example,\n"
"at 48000Hz the permitted values are 120, 240, 480 or 960.\n");
2009-09-05 00:04:44 +02:00
}
int main(int argc, char *argv[])
{
2012-07-21 17:14:13 +02:00
int r, error;
2009-09-11 17:40:27 +02:00
size_t bytes_per_frame;
2012-07-28 21:44:19 +02:00
unsigned int ts_per_frame;
2009-09-05 00:04:44 +02:00
snd_pcm_t *snd;
2012-07-21 17:14:13 +02:00
OpusEncoder *encoder;
2009-09-11 17:40:27 +02:00
RtpSession *session;
2009-09-05 00:04:44 +02:00
/* command-line options */
const char *device = DEFAULT_DEVICE,
2014-10-22 22:21:14 +02:00
*addr = DEFAULT_ADDR,
*pid = NULL;
2009-09-05 00:04:44 +02:00
unsigned int buffer = DEFAULT_BUFFER,
rate = DEFAULT_RATE,
2009-09-11 13:05:02 +02:00
channels = DEFAULT_CHANNELS,
frame = DEFAULT_FRAME,
2009-09-11 17:40:27 +02:00
kbps = DEFAULT_BITRATE,
port = DEFAULT_PORT;
2009-09-11 13:05:02 +02:00
2012-07-27 13:46:03 +02:00
fputs(COPYRIGHT "\n", stderr);
2012-07-21 14:32:40 +02:00
2009-09-05 00:04:44 +02:00
for (;;) {
int c;
2014-10-22 22:21:14 +02:00
c = getopt(argc, argv, "b:c:d:f:h:m:p:r:v:D:");
2009-09-05 00:04:44 +02:00
if (c == -1)
break;
switch (c) {
2009-09-11 13:05:02 +02:00
case 'b':
kbps = atoi(optarg);
break;
2009-09-05 00:04:44 +02:00
case 'c':
channels = atoi(optarg);
break;
case 'd':
device = optarg;
break;
case 'f':
frame = atol(optarg);
break;
2009-09-11 17:40:27 +02:00
case 'h':
addr = optarg;
break;
2009-09-05 00:04:44 +02:00
case 'm':
buffer = atoi(optarg);
break;
case 'p':
2009-09-11 17:40:27 +02:00
port = atoi(optarg);
2009-09-05 00:04:44 +02:00
break;
case 'r':
rate = atoi(optarg);
break;
2009-09-11 17:40:27 +02:00
case 'v':
verbose = atoi(optarg);
break;
2014-10-22 22:21:14 +02:00
case 'D':
pid = optarg;
break;
2009-09-05 00:04:44 +02:00
default:
usage(stderr);
return -1;
}
}
2012-07-21 17:14:13 +02:00
encoder = opus_encoder_create(rate, channels, OPUS_APPLICATION_AUDIO,
&error);
2009-09-05 00:04:44 +02:00
if (encoder == NULL) {
2012-07-21 17:14:13 +02:00
fprintf(stderr, "opus_encoder_create: %s\n",
opus_strerror(error));
2009-09-11 18:19:47 +02:00
return -1;
}
2012-07-21 17:14:13 +02:00
2009-09-11 13:05:02 +02:00
bytes_per_frame = kbps * 1024 * frame / rate / 8;
2009-09-05 00:04:44 +02:00
/* Follow the RFC, payload 0 has 8kHz reference rate */
2012-07-28 21:44:19 +02:00
ts_per_frame = frame * 8000 / rate;
2012-07-28 21:44:19 +02:00
2009-09-11 17:40:27 +02:00
ortp_init();
ortp_scheduler_init();
session = create_rtp_send(addr, port);
assert(session != NULL);
2009-09-05 00:04:44 +02:00
r = snd_pcm_open(&snd, device, SND_PCM_STREAM_CAPTURE, 0);
if (r < 0) {
aerror("snd_pcm_open", r);
return -1;
}
if (set_alsa_hw(snd, rate, channels, buffer * 1000) == -1)
return -1;
if (set_alsa_sw(snd) == -1)
return -1;
2014-10-22 22:21:14 +02:00
if (pid)
go_daemon(pid);
go_realtime();
2012-07-28 21:44:19 +02:00
r = run_tx(snd, channels, frame, encoder, bytes_per_frame,
ts_per_frame, session);
2009-09-05 00:04:44 +02:00
if (snd_pcm_close(snd) < 0)
abort();
2009-09-11 17:40:27 +02:00
rtp_session_destroy(session);
ortp_exit();
ortp_global_stats_display();
2012-07-21 17:14:13 +02:00
opus_encoder_destroy(encoder);
2009-09-05 00:04:44 +02:00
return r;
}