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();
|
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,
|
2009-09-11 17:40:27 +02:00
|
|
|
RtpSession *session)
|
2009-09-05 00:04:44 +02:00
|
|
|
{
|
|
|
|
float *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;
|
2009-09-11 17:40:27 +02:00
|
|
|
static int ts = 0;
|
2009-09-05 00:04:44 +02:00
|
|
|
|
2009-09-11 13:05:02 +02:00
|
|
|
pcm = alloca(sizeof(float) * 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) {
|
|
|
|
aerror("snd_pcm_readi", f);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-09-11 17:40:27 +02:00
|
|
|
if (f < samples)
|
2009-09-05 00:04:44 +02:00
|
|
|
fprintf(stderr, "Short read, %ld\n", f);
|
|
|
|
|
2012-07-21 17:14:13 +02:00
|
|
|
z = opus_encode_float(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);
|
|
|
|
ts += samples;
|
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,
|
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,
|
2009-09-11 13:05:02 +02:00
|
|
|
encoder, bytes_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)
|
|
|
|
{
|
2009-09-11 17:40:27 +02:00
|
|
|
fprintf(fd, "Usage: tx [<parameters>]\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);
|
2009-09-11 17:40:27 +02:00
|
|
|
fprintf(fd, " -m <ms> Buffer time (milliseconds, default %d)\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");
|
2009-09-11 17:40:27 +02:00
|
|
|
fprintf(fd, " -r <rate> Sample rate (default %d)\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);
|
2009-09-11 17:40:27 +02:00
|
|
|
fprintf(fd, " -f <bytes> Frame size (default %d)\n",
|
|
|
|
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
|
|
|
|
|
|
|
fprintf(fd, "\nDisplay parameters:\n");
|
|
|
|
fprintf(fd, " -v <n> Verbosity level (default %d)\n",
|
|
|
|
DEFAULT_VERBOSE);
|
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;
|
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,
|
2009-09-11 17:40:27 +02:00
|
|
|
*addr = DEFAULT_ADDR;
|
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-21 14:32:40 +02:00
|
|
|
fputs("tx " COPYRIGHT "\n", stderr);
|
|
|
|
|
2009-09-05 00:04:44 +02:00
|
|
|
for (;;) {
|
|
|
|
int c;
|
|
|
|
|
2009-09-11 17:40:27 +02:00
|
|
|
c = getopt(argc, argv, "b:c:d:f:h:m:p:r:v:");
|
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;
|
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
|
|
|
|
2009-09-11 17:40:27 +02:00
|
|
|
if (go_realtime() != 0)
|
2009-09-05 00:04:44 +02:00
|
|
|
return -1;
|
|
|
|
|
2009-09-11 17:40:27 +02:00
|
|
|
ortp_init();
|
|
|
|
ortp_scheduler_init();
|
|
|
|
ortp_set_log_level_mask(ORTP_MESSAGE|ORTP_WARNING|ORTP_ERROR);
|
|
|
|
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;
|
|
|
|
|
2009-09-11 17:40:27 +02:00
|
|
|
r = run_tx(snd, channels, frame, encoder, bytes_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;
|
|
|
|
}
|