From fc27389227cf30fdf7e658fc33da56f621668079 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 20 Sep 2014 14:07:32 +0200 Subject: Change to 16KHz audio input. A vague attempt at error handling (read crash prevention). --- src/audioinput.cc | 25 ++++++++++++++++++++++--- src/audioinputhandler.cc | 8 +++++--- src/inputstreamer.cc | 16 +++++++++++----- src/opusdecoder.cc | 2 +- src/opusencoder.cc | 20 +++++++++++--------- src/opusencoder.h | 3 --- 6 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/audioinput.cc b/src/audioinput.cc index cc68e4b..a94d208 100644 --- a/src/audioinput.cc +++ b/src/audioinput.cc @@ -31,12 +31,26 @@ AudioInput::AudioInput(const char *device) { int err; - ai = ai_init(&err, device, "", 48000, 1); - if(err) printf("ai_err: %d\n", err); + ai = ai_init(&err, device, "", 16000, 1); + if(ai == NULL || err) { + printf("ai_init: %d\n", err); + return; + } + + int srate = ai_get_samplerate(&err, ai); + if(err) printf("ai_get_samplerate: %d\n", err); + if(srate != 16000) { + printf("Samplerate: %d\n", srate); + } } AudioInput::~AudioInput() { + if(ai == NULL) { + printf("ai_err: no handle\n"); + return; + } + int err; ai_close(&err, ai); if(err) printf("ai_err: %d\n", err); @@ -44,8 +58,13 @@ AudioInput::~AudioInput() int AudioInput::getSamples(void *pcm, size_t size) { + if(ai == NULL) { + printf("ai_err: no handle\n"); + return 0; + } + int err; int ret = ai_read(&err, ai, pcm, size); - if(err) printf("ai_err: %d\n", err); + if(err) printf("ai_read: %d\n", err); return ret; } diff --git a/src/audioinputhandler.cc b/src/audioinputhandler.cc index 29af3cb..e17fce2 100644 --- a/src/audioinputhandler.cc +++ b/src/audioinputhandler.cc @@ -48,8 +48,10 @@ void AudioInputHandler::run() while(running) { int sz = ai.getSamples(pcm, sizeof(pcm)); - // printf("sz: %d\n", sz); - framelist_t fl = oe.encode(pcm, sz); - if(fl.size()) emit newAudio(fl); + //printf("sz: %d\n", sz); + if(sz > 0) { + framelist_t fl = oe.encode(pcm, sz); + if(fl.size()) emit newAudio(fl); + } } } diff --git a/src/inputstreamer.cc b/src/inputstreamer.cc index a1d7aa9..495f199 100644 --- a/src/inputstreamer.cc +++ b/src/inputstreamer.cc @@ -26,6 +26,8 @@ */ #include "inputstreamer.h" +#include + #define KEY "123456789012345678901234567890123456789012345678901234567890" #define SSRC 1234567890 #define CSRC_V 42 @@ -65,12 +67,16 @@ void InputStreamer::run() res = lrtp_create_profile(lrtp, PROFILE_OPUS, CSRC_A, OPTION_END); if(res != 0) printf("O:lrtp_create_profile (a) err: %d\n", res); - char packet[16*1024]; + char packet[64*1024]; while(running) { + if(!socket.hasPendingDatagrams()) { + qApp->processEvents(); + usleep(2000); // sleep 2ms + continue; + } qint64 packetsize = - socket.readDatagram(packet, (quint64)sizeof(packet), 0,0); + socket.readDatagram(packet, (quint64)sizeof(packet), 0, 0); if(packetsize < 1) { - usleep(1000); continue; } total += packetsize; @@ -91,13 +97,13 @@ void InputStreamer::run() Frame f(frame, ret); f.ts = ts; emit newImage(f); - printf("v"); fflush(stdout); + //printf("v"); fflush(stdout); } else if(csrc == CSRC_A) { // Audio frame Frame f(frame, ret); f.ts = ts; emit newAudio(f); - printf("a"); fflush(stdout); + //printf("a"); fflush(stdout); } else { printf("Unknown stream: CSRC: %d\n", csrc); } diff --git a/src/opusdecoder.cc b/src/opusdecoder.cc index 195c699..6366e78 100644 --- a/src/opusdecoder.cc +++ b/src/opusdecoder.cc @@ -29,7 +29,7 @@ OpusDecoder::OpusDecoder() { int err; - decoder = opus_decoder_create(48000, 1, &err); + decoder = opus_decoder_create(16000, 1, &err); if(err) printf("opus_decoder_create: %d\n", err); } diff --git a/src/opusencoder.cc b/src/opusencoder.cc index b5c343a..27bdb90 100644 --- a/src/opusencoder.cc +++ b/src/opusencoder.cc @@ -29,19 +29,21 @@ #include #include +// 500 to 512000 #define OPUS_BITRATE 64000 +// Frame size 10ms +#define OPUS_FRAME_SIZE 10 + OpusEncoder::OpusEncoder() : cache(5760 * 10) { - int error = 0; - encoder = - opus_encoder_create(48000, 1, OPUS_APPLICATION_AUDIO, &error); - if(!encoder || error != OPUS_OK) { - printf("opus_encoder_create: %d\n", error); + int err = 0; + encoder = opus_encoder_create(16000, 1, OPUS_APPLICATION_AUDIO, &err); + if(!encoder || err != OPUS_OK) { + printf("opus_encoder_create: %d\n", err); } - // 500 to 512000 opus_encoder_ctl(encoder, OPUS_SET_BITRATE(OPUS_BITRATE)); // [0-10] with 10 representing the highest complexity. @@ -55,7 +57,7 @@ OpusEncoder::~OpusEncoder() unsigned int OpusEncoder::framesize() { - return 48000 / 1000 * OPUS_FRAME_SIZE; // 10ms audio data + return 16000 / 1000 * OPUS_FRAME_SIZE; // 10ms audio data } framelist_t OpusEncoder::encode(const char *pcm, size_t size) @@ -67,7 +69,7 @@ framelist_t OpusEncoder::encode(const char *pcm, size_t size) printf("encoder == NULL\n"); } - size_t bytes_per_packet = ((OPUS_BITRATE *framesize())/48000+4)/8; + size_t bytes_per_packet = ((OPUS_BITRATE *framesize())/16000+4)/8; cache.addSamples((short*)pcm, size / sizeof(short)); @@ -82,7 +84,7 @@ framelist_t OpusEncoder::encode(const char *pcm, size_t size) (unsigned char *)frame->data, (opus_int32)frame->size); if(n < 0) { - printf("n < 0\n"); + printf("opus_encode: %d\n", n); } frame->size = n; diff --git a/src/opusencoder.h b/src/opusencoder.h index 9aeaf3a..5d2714c 100644 --- a/src/opusencoder.h +++ b/src/opusencoder.h @@ -32,9 +32,6 @@ #include "frame.h" #include "samplecache.h" -// Frame size 10ms -#define OPUS_FRAME_SIZE 10 - class OpusEncoder { public: OpusEncoder(); -- cgit v1.2.3