master
Mark Hills 2009-09-11 12:05:02 +01:00
parent 8d8040f5a6
commit c7af697019
3 changed files with 36 additions and 21 deletions

View File

@ -5,7 +5,7 @@
#define DEFAULT_BUFFER 16 #define DEFAULT_BUFFER 16
#define DEFAULT_SERVICE "1350" #define DEFAULT_SERVICE "1350"
#define DEFAULT_FRAME 96 #define DEFAULT_FRAME 128
#define DEFAULT_RATE 44100 #define DEFAULT_RATE 44100
#define DEFAULT_CHANNELS 2 #define DEFAULT_CHANNELS 2

4
rx.c
View File

@ -10,7 +10,7 @@
#include "jitter.h" #include "jitter.h"
#include "sched.h" #include "sched.h"
#define ENCODED_BYTES 128 #define ENCODED_BYTES 71
/* /*
* Bind to a network socket for receiving packets on the given port * Bind to a network socket for receiving packets on the given port
@ -157,7 +157,7 @@ static int run_rx(int sd, CELTDecoder *decoder, snd_pcm_t *snd)
return -1; return -1;
} }
jitbuf_debug(&jb, stderr); //jitbuf_debug(&jb, stderr);
if (play_audio(&jb, decoder, snd) == -1) if (play_audio(&jb, decoder, snd) == -1)
return -1; return -1;

51
tx.c
View File

@ -9,9 +9,7 @@
#include "device.h" #include "device.h"
#include "sched.h" #include "sched.h"
#define ENCODED_BYTES 128 typedef unsigned int seq_t;
typedef int seq_t;
/* /*
* Bind to network socket, for sending to the given host and port * Bind to network socket, for sending to the given host and port
@ -55,7 +53,11 @@ static int bind_to_network(const char *host, const char *service,
* Encode a frame of audio and send it over the network * Encode a frame of audio and send it over the network
*/ */
static int send_audio(snd_pcm_t *snd, CELTEncoder *encoder, static int send_audio(snd_pcm_t *snd,
const snd_pcm_uframes_t samples,
const unsigned int channels,
CELTEncoder *encoder,
const size_t bytes_per_frame,
int sd, struct addrinfo *theirs, seq_t *seq) int sd, struct addrinfo *theirs, seq_t *seq)
{ {
float *pcm; float *pcm;
@ -63,11 +65,11 @@ static int send_audio(snd_pcm_t *snd, CELTEncoder *encoder,
ssize_t z; ssize_t z;
snd_pcm_sframes_t f; snd_pcm_sframes_t f;
pcm = alloca(sizeof(float) * DEFAULT_FRAME * DEFAULT_CHANNELS); pcm = alloca(sizeof(float) * samples * channels);
packet = alloca(ENCODED_BYTES + sizeof(uint32_t)); packet = alloca(bytes_per_frame + sizeof(uint32_t));
encoded = packet + sizeof(uint32_t); encoded = packet + sizeof(uint32_t);
f = snd_pcm_readi(snd, pcm, DEFAULT_FRAME); f = snd_pcm_readi(snd, pcm, samples);
if (f < 0) { if (f < 0) {
aerror("snd_pcm_readi", f); aerror("snd_pcm_readi", f);
return -1; return -1;
@ -75,7 +77,7 @@ static int send_audio(snd_pcm_t *snd, CELTEncoder *encoder,
if (f < DEFAULT_FRAME) if (f < DEFAULT_FRAME)
fprintf(stderr, "Short read, %ld\n", f); fprintf(stderr, "Short read, %ld\n", f);
z = celt_encode_float(encoder, pcm, NULL, encoded, ENCODED_BYTES); z = celt_encode_float(encoder, pcm, NULL, encoded, bytes_per_frame);
if (z < 0) { if (z < 0) {
fputs("celt_encode_float failed\n", stderr); fputs("celt_encode_float failed\n", stderr);
return -1; return -1;
@ -84,18 +86,22 @@ static int send_audio(snd_pcm_t *snd, CELTEncoder *encoder,
*(uint32_t*)packet = htonl(*seq); *(uint32_t*)packet = htonl(*seq);
(*seq)++; (*seq)++;
z = sendto(sd, packet, sizeof(uint32_t) + ENCODED_BYTES, 0, z = sendto(sd, packet, sizeof(uint32_t) + bytes_per_frame, 0,
theirs->ai_addr, theirs->ai_addrlen); theirs->ai_addr, theirs->ai_addrlen);
if (z == -1) { if (z == -1) {
perror("sendto"); perror("sendto");
return -1; return -1;
} }
fputc('>', stderr); //fputc('>', stderr);
return 0; return 0;
} }
static int run_tx(snd_pcm_t *snd, CELTEncoder *encoder, static int run_tx(snd_pcm_t *snd,
const snd_pcm_uframes_t samples,
const unsigned int channels,
CELTEncoder *encoder,
const size_t bytes_per_frame,
int sd, struct addrinfo *theirs) int sd, struct addrinfo *theirs)
{ {
seq_t seq = 0; seq_t seq = 0;
@ -103,7 +109,9 @@ static int run_tx(snd_pcm_t *snd, CELTEncoder *encoder,
for (;;) { for (;;) {
int r; int r;
r = send_audio(snd, encoder, sd, theirs, &seq); r = send_audio(snd, samples, channels,
encoder, bytes_per_frame,
sd, theirs, &seq);
if (r == -1) if (r == -1)
return -1; return -1;
} }
@ -141,6 +149,7 @@ int main(int argc, char *argv[])
snd_pcm_t *snd; snd_pcm_t *snd;
CELTMode *mode; CELTMode *mode;
CELTEncoder *encoder; CELTEncoder *encoder;
size_t bytes_per_frame;
/* command-line options */ /* command-line options */
const char *device = DEFAULT_DEVICE, const char *device = DEFAULT_DEVICE,
@ -148,17 +157,21 @@ int main(int argc, char *argv[])
*host; *host;
unsigned int buffer = DEFAULT_BUFFER, unsigned int buffer = DEFAULT_BUFFER,
rate = DEFAULT_RATE, rate = DEFAULT_RATE,
channels = DEFAULT_CHANNELS; channels = DEFAULT_CHANNELS,
size_t frame = DEFAULT_FRAME; frame = DEFAULT_FRAME,
kbps = DEFAULT_BITRATE;
for (;;) { for (;;) {
int c; int c;
c = getopt(argc, argv, "c:d:f:m:p:r:"); c = getopt(argc, argv, "b:c:d:f:m:p:r:");
if (c == -1) if (c == -1)
break; break;
switch (c) { switch (c) {
case 'b':
kbps = atoi(optarg);
break;
case 'c': case 'c':
channels = atoi(optarg); channels = atoi(optarg);
break; break;
@ -200,6 +213,8 @@ int main(int argc, char *argv[])
fputs("celt_encoder_create failed\n", stderr); fputs("celt_encoder_create failed\n", stderr);
return -1; return -1;
} }
bytes_per_frame = kbps * 1024 * frame / rate / 8;
fprintf(stderr, "bytes_per_frame = %d\n", bytes_per_frame);
sd = bind_to_network(host, service, &servinfo, &theirs); sd = bind_to_network(host, service, &servinfo, &theirs);
if (sd == -1) if (sd == -1)
@ -218,7 +233,7 @@ int main(int argc, char *argv[])
if (go_realtime() != 0) if (go_realtime() != 0)
return -1; return -1;
r = run_tx(snd, encoder, sd, theirs); r = run_tx(snd, frame, channels, encoder, bytes_per_frame, sd, theirs);
freeaddrinfo(servinfo); freeaddrinfo(servinfo);