1
0
mirror of http://www.pogo.org.uk/~mark/trx.git synced 2024-11-21 18:05:06 +01:00

Port to use opus codec

This commit is contained in:
Mark Hills 2012-07-21 16:14:13 +01:00
parent 52f99e9531
commit 8c5bec31da
3 changed files with 32 additions and 46 deletions

View File

@ -3,10 +3,10 @@
CFLAGS+=-MMD -Wall CFLAGS+=-MMD -Wall
LDLIBS_ASOUND?=-lasound LDLIBS_ASOUND?=-lasound
LDLIBS_CELT?=-lcelt LDLIBS_OPUS?=-lopus
LDLIBS_ORTP?=-lortp LDLIBS_ORTP?=-lortp
LDLIBS+=$(LDLIBS_ASOUND) $(LDLIBS_CELT) $(LDLIBS_ORTP) LDLIBS+=$(LDLIBS_ASOUND) $(LDLIBS_OPUS) $(LDLIBS_ORTP)
.PHONY: all clean .PHONY: all clean

40
rx.c
View File

@ -1,7 +1,7 @@
#include <netdb.h> #include <netdb.h>
#include <string.h> #include <string.h>
#include <alsa/asoundlib.h> #include <alsa/asoundlib.h>
#include <celt/celt.h> #include <opus/opus.h>
#include <ortp/ortp.h> #include <ortp/ortp.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
@ -38,7 +38,7 @@ static RtpSession* create_rtp_recv(const char *addr_desc, const int port,
static int play_one_frame(void *packet, static int play_one_frame(void *packet,
size_t len, size_t len,
CELTDecoder *decoder, OpusDecoder *decoder,
snd_pcm_t *snd, snd_pcm_t *snd,
const unsigned int channels, const unsigned int channels,
const snd_pcm_uframes_t samples) const snd_pcm_uframes_t samples)
@ -50,28 +50,28 @@ static int play_one_frame(void *packet,
pcm = alloca(sizeof(float) * samples * channels); pcm = alloca(sizeof(float) * samples * channels);
if (packet == NULL) { if (packet == NULL) {
r = celt_decode_float(decoder, NULL, 0, pcm); r = opus_decode_float(decoder, NULL, 0, pcm, samples, 1);
} else { } else {
r = celt_decode_float(decoder, packet, len, pcm); r = opus_decode_float(decoder, packet, len, pcm, samples, 0);
} }
if (r != 0) { if (r < 0) {
fputs("Error in celt_decode\n", stderr); fprintf(stderr, "opus_decode: %s\n", opus_strerror(r));
return -1; return -1;
} }
f = snd_pcm_writei(snd, pcm, samples); f = snd_pcm_writei(snd, pcm, r);
if (f < 0) { if (f < 0) {
aerror("snd_pcm_writei", f); aerror("snd_pcm_writei", f);
return -1; return -1;
} }
if (f < samples) if (f < r)
fprintf(stderr, "Short write %ld\n", f); fprintf(stderr, "Short write %ld\n", f);
return 0; return r;
} }
static int run_rx(RtpSession *session, static int run_rx(RtpSession *session,
CELTDecoder *decoder, OpusDecoder *decoder,
snd_pcm_t *snd, snd_pcm_t *snd,
const unsigned int channels, const unsigned int channels,
const snd_pcm_uframes_t frame) const snd_pcm_uframes_t frame)
@ -101,7 +101,7 @@ static int run_rx(RtpSession *session,
if (r == -1) if (r == -1)
return -1; return -1;
ts += frame; ts += r;
} }
} }
@ -138,10 +138,9 @@ static void usage(FILE *fd)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int r; int r, error;
snd_pcm_t *snd; snd_pcm_t *snd;
CELTMode *mode; OpusDecoder *decoder;
CELTDecoder *decoder;
RtpSession *session; RtpSession *session;
/* command-line options */ /* command-line options */
@ -191,14 +190,10 @@ int main(int argc, char *argv[])
} }
} }
mode = celt_mode_create(rate, channels, frame, NULL); decoder = opus_decoder_create(rate, channels, &error);
if (mode == NULL) {
fputs("celt_mode_create failed\n", stderr);
return -1;
}
decoder = celt_decoder_create(mode);
if (decoder == NULL) { if (decoder == NULL) {
fputs("celt_decoder_create failed\n", stderr); fprintf(stderr, "opus_decoder_create: %s\n",
opus_strerror(error));
return -1; return -1;
} }
@ -229,8 +224,7 @@ int main(int argc, char *argv[])
ortp_exit(); ortp_exit();
ortp_global_stats_display(); ortp_global_stats_display();
celt_decoder_destroy(decoder); opus_decoder_destroy(decoder);
celt_mode_destroy(mode);
return r; return r;
} }

34
tx.c
View File

@ -1,7 +1,7 @@
#include <netdb.h> #include <netdb.h>
#include <string.h> #include <string.h>
#include <alsa/asoundlib.h> #include <alsa/asoundlib.h>
#include <celt/celt.h> #include <opus/opus.h>
#include <ortp/ortp.h> #include <ortp/ortp.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
@ -36,7 +36,7 @@ static RtpSession* create_rtp_send(const char *addr_desc, const int port)
static int send_one_frame(snd_pcm_t *snd, static int send_one_frame(snd_pcm_t *snd,
const unsigned int channels, const unsigned int channels,
const snd_pcm_uframes_t samples, const snd_pcm_uframes_t samples,
CELTEncoder *encoder, OpusEncoder *encoder,
const size_t bytes_per_frame, const size_t bytes_per_frame,
RtpSession *session) RtpSession *session)
{ {
@ -57,9 +57,9 @@ static int send_one_frame(snd_pcm_t *snd,
if (f < samples) if (f < samples)
fprintf(stderr, "Short read, %ld\n", f); fprintf(stderr, "Short read, %ld\n", f);
z = celt_encode_float(encoder, pcm, NULL, packet, bytes_per_frame); z = opus_encode_float(encoder, pcm, samples, packet, bytes_per_frame);
if (z < 0) { if (z < 0) {
fputs("celt_encode_float failed\n", stderr); fprintf(stderr, "opus_encode_float: %s\n", opus_strerror(z));
return -1; return -1;
} }
@ -72,7 +72,7 @@ static int send_one_frame(snd_pcm_t *snd,
static int run_tx(snd_pcm_t *snd, static int run_tx(snd_pcm_t *snd,
const unsigned int channels, const unsigned int channels,
const snd_pcm_uframes_t frame, const snd_pcm_uframes_t frame,
CELTEncoder *encoder, OpusEncoder *encoder,
const size_t bytes_per_frame, const size_t bytes_per_frame,
RtpSession *session) RtpSession *session)
{ {
@ -123,11 +123,10 @@ static void usage(FILE *fd)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int r; int r, error;
size_t bytes_per_frame; size_t bytes_per_frame;
snd_pcm_t *snd; snd_pcm_t *snd;
CELTMode *mode; OpusEncoder *encoder;
CELTEncoder *encoder;
RtpSession *session; RtpSession *session;
/* command-line options */ /* command-line options */
@ -183,20 +182,14 @@ int main(int argc, char *argv[])
} }
} }
mode = celt_mode_create(rate, channels, frame, NULL); encoder = opus_encoder_create(rate, channels, OPUS_APPLICATION_AUDIO,
if (mode == NULL) { &error);
fputs("celt_mode_create failed\n", stderr);
return -1;
}
encoder = celt_encoder_create(mode);
if (encoder == NULL) { if (encoder == NULL) {
fputs("celt_encoder_create failed\n", stderr); fprintf(stderr, "opus_encoder_create: %s\n",
return -1; opus_strerror(error));
}
if (celt_encoder_ctl(encoder, CELT_SET_PREDICTION(2)) != CELT_OK) {
fputs("CELT_SET_PREDICTION failed\n", stderr);
return -1; return -1;
} }
bytes_per_frame = kbps * 1024 * frame / rate / 8; bytes_per_frame = kbps * 1024 * frame / rate / 8;
if (go_realtime() != 0) if (go_realtime() != 0)
@ -227,8 +220,7 @@ int main(int argc, char *argv[])
ortp_exit(); ortp_exit();
ortp_global_stats_display(); ortp_global_stats_display();
celt_encoder_destroy(encoder); opus_encoder_destroy(encoder);
celt_mode_destroy(mode);
return r; return r;
} }