From bb042178b3296cacc11a53c4ce9b7f3cc6732270 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 27 Sep 2014 19:21:29 +0200 Subject: Switch to libaudioio. Fix playback stuttering. --- .gitmodules | 4 +-- Makefile.am | 2 +- autogen.sh | 8 +++--- configure.ac | 7 +---- src/Makefile.am | 8 +++--- src/audiobackend-alsa.cc | 73 +++++++++++++++++------------------------------- src/audiobackend-alsa.h | 7 ++--- src/audiobackend.h | 5 ++++ src/soundplayer.cc | 40 ++++++++++++-------------- src/soundplayer.h | 1 + 10 files changed, 64 insertions(+), 91 deletions(-) diff --git a/.gitmodules b/.gitmodules index a294735..5524e26 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,6 @@ -[submodule "libaudioin"] +[submodule "libaudioio"] path = libaudioin - url = http://git.aasimon.org/public/libaudioin.git + url = http://git.aasimon.org/public/libaudioio.git [submodule "lrtp"] path = lrtp url = http://git.aasimon.org/public/lrtp.git diff --git a/Makefile.am b/Makefile.am index a59c531..b175782 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,5 +1,5 @@ AUTOMAKE_OPTIONS = gnu -SUBDIRS = libaudioin lrtp src icons tools +SUBDIRS = libaudioio lrtp src icons tools DISTDIRS = src icons tools EXTRA_DIST = diff --git a/autogen.sh b/autogen.sh index 1c3417c..01cd53a 100755 --- a/autogen.sh +++ b/autogen.sh @@ -1,9 +1,9 @@ #!/bin/sh -e # Check if hugin has been obtained: -if [ ! -f lrtp/autogen.sh ] || [ ! -f libaudioin/autogen.sh ] +if [ ! -f lrtp/autogen.sh ] || [ ! -f libaudioio/autogen.sh ] then - echo "You are missing the lrtp and/or libaudioin submodule." + echo "You are missing the lrtp and/or libaudioio submodule." echo "Run" echo " git submodule init" echo " git submodule update" @@ -13,5 +13,5 @@ fi # Now run autoreconf ${AUTORECONF:-autoreconf} -fiv -(cd lrtp; ./autogen.sh) -(cd libaudioin; ./autogen.sh) \ No newline at end of file +#(cd lrtp; ./autogen.sh) +#(cd libaudioio; ./autogen.sh) diff --git a/configure.ac b/configure.ac index 48ef55d..685ddd3 100644 --- a/configure.ac +++ b/configure.ac @@ -45,11 +45,6 @@ if (test "$QT_UIC" = ""); then AC_MSG_ERROR([QT4 uic is required.]) fi -dnl ====================== -dnl Check for libao -dnl ====================== -PKG_CHECK_MODULES(AO, ao >= 0.8.8) - dnl ====================== dnl Check for opus dnl ====================== @@ -77,7 +72,7 @@ AC_SUBST(CXXFLAGS) AC_SUBST(LDFLAGS) AC_CONFIG_SUBDIRS([lrtp]) -AC_CONFIG_SUBDIRS([libaudioin]) +AC_CONFIG_SUBDIRS([libaudioio]) AC_OUTPUT( Makefile diff --git a/src/Makefile.am b/src/Makefile.am index 416bbcf..30ec6e0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -3,13 +3,13 @@ bin_PROGRAMS = simplertp simplertp-genkey simplertp_LDADD = $(QT_LIBS) $(AO_LIBS) $(V4LC_LIBS) -ljpeg \ $(OPUS_LIBS) $(AO_LIBS) $(PULSE_LIBS) \ $(top_srcdir)/lrtp/src/liblrtp.la \ - $(top_srcdir)/libaudioin/src/libaudioin.la \ + $(top_srcdir)/libaudioio/src/libaudioio.la \ $(shell ../tools/MocList o ) qrc_simplertp.o -simplertp_CXXFLAGS = $(QT_CFLAGS) $(AO_CFLAGS) $(V4LC_CFLAGS) \ +simplertp_CXXFLAGS = $(QT_CFLAGS) $(V4LC_CFLAGS) \ -I$(top_srcdir)/lrtp/src \ - -I$(top_srcdir)/libaudioin/src \ - $(OPUS_CFLAGS) $(AO_CFLAGS) $(PULSE_CFLAGS) + -I$(top_srcdir)/libaudioio/src \ + $(OPUS_CFLAGS) $(PULSE_CFLAGS) AM_CXXFLAGS = $(simplertp_CXXFLAGS) diff --git a/src/audiobackend-alsa.cc b/src/audiobackend-alsa.cc index b4401ca..5f3db40 100644 --- a/src/audiobackend-alsa.cc +++ b/src/audiobackend-alsa.cc @@ -27,85 +27,62 @@ #include "audiobackend-alsa.h" #include +#include #include "mediaconfig.h" AudioBackendAlsa::AudioBackendAlsa(const char *device) { - // - // LibAO: - // - ao_initialize(); - - ao_sample_format sf; - memset(&sf, 0, sizeof(sf)); - sf.bits = 16; - sf.rate = SAMPLERATE; - sf.channels = 1; - sf.byte_format = AO_FMT_NATIVE; - - dev = ao_open_live(ao_default_driver_id(), &sf, 0); - - // - // LibAudioIn: - // int err; - ai = ai_init(&err, device, "", SAMPLERATE, 1); - if(ai == NULL || err) { + aio = aio_init(&err, "default", NULL, device, NULL, SAMPLERATE, 1); + if(aio == NULL || err) { printf("ai_init: %d (device: %s)\n", err, device); exit(1); return; } - int srate = ai_get_samplerate(&err, ai); + int srate = aio_get_samplerate(aio); if(err) printf("ai_get_samplerate: %d\n", err); if(srate != SAMPLERATE) { printf("Samplerate: %d\n", srate); } + + printf("buffer size: %d\n", aio_get_buffer_size(aio)); } AudioBackendAlsa::~AudioBackendAlsa() { - // - // LibAO: - // - ao_close(dev); - ao_shutdown(); - - // - // LibAudioIn: - // - if(ai == NULL) { - printf("ai_err: no handle\n"); - return; - } - - int err; - ai_close(&err, ai); + int err = aio_close(aio); if(err) printf("ai_err: %d\n", err); } int AudioBackendAlsa::read(char *pcm, size_t maxsize) { - // - // LibAudioIn: - // - if(ai == NULL) { + if(aio == NULL) { printf("ai_err: no handle\n"); return 0; } - int err; - int ret = ai_read(&err, ai, pcm, maxsize); - if(err) printf("ai_read: %d\n", err); + int ret = aio_read(aio, pcm, maxsize); + if(ret < 0) printf("ai_read: %d\n", ret); return ret; } int AudioBackendAlsa::write(const char *pcm, size_t size) { - // - // LibAO: - // - ao_play(dev, (char *)pcm, size); - return 0; + if(aio == NULL) { + printf("aio_err: no handle\n"); + return 0; + } + + int ret = aio_write(aio, pcm, size); + if(ret < 0) printf("ai_write: %d\n", ret); + return ret; +} + +int AudioBackendAlsa::getBufferSize() +{ + int ret = aio_get_buffer_size(aio); + if(ret > 0) return ret; + return AudioBackend::getBufferSize(); } diff --git a/src/audiobackend-alsa.h b/src/audiobackend-alsa.h index 4a79a5d..c744fe2 100644 --- a/src/audiobackend-alsa.h +++ b/src/audiobackend-alsa.h @@ -29,8 +29,7 @@ #include "audiobackend.h" -#include -#include +#include class AudioBackendAlsa : public AudioBackend { public: @@ -39,10 +38,10 @@ public: int read(char *pcm, size_t maxsize); int write(const char *pcm, size_t size); + int getBufferSize(); private: - struct ai_t *ai; - ao_device *dev; + struct aio_t *aio; }; #endif/*__SIMPLERTP_AUDIOBACKEND_ALSA_H__*/ diff --git a/src/audiobackend.h b/src/audiobackend.h index 4c3232d..8e65a82 100644 --- a/src/audiobackend.h +++ b/src/audiobackend.h @@ -36,6 +36,11 @@ public: virtual int read(char *pcm, size_t maxsize) = 0; virtual int write(const char *pcm, size_t size) = 0; + + /** + * Reimplement to use other buffersize + */ + virtual int getBufferSize() { return 1024; } }; // Declared in audiobackend.cc diff --git a/src/soundplayer.cc b/src/soundplayer.cc index b6d14c1..0ae978f 100644 --- a/src/soundplayer.cc +++ b/src/soundplayer.cc @@ -29,14 +29,13 @@ #include "audiobackend.h" -#define BUFSZ 128 - SoundPlayer::SoundPlayer() { start(); pread = 0; for(int i = 0; i < 10; i++) pwrite[i] = 0; memset(ringbuffer, 0, sizeof(ringbuffer)); + buffer_size = g_audiobackend->getBufferSize(); } SoundPlayer::~SoundPlayer() @@ -49,42 +48,39 @@ void SoundPlayer::run() { running = true; - short s[BUFSZ]; + short *s = (short*)malloc(buffer_size * sizeof(short)); memset(s, 0, sizeof(s)); while(running) { - for(int i = 0; i < BUFSZ; i++) { + for(int i = 0; i < buffer_size; i++) { s[i] = ringbuffer[pread % RINGBUFFER]; ringbuffer[pread % RINGBUFFER] = 0; pread++; } - g_audiobackend->write((const char *)s, sizeof(s)); + g_audiobackend->write((const char *)s, buffer_size * sizeof(short)); } + + free(s); } void SoundPlayer::playSamples(int peer, const char *pcm, size_t size) { - if(pwrite[peer] == 0) pwrite[peer] = pread + (1.5 * BUFSZ); + // printf("%d\t%d\t(diff: %d)\n", pwrite[peer], pread, pwrite[peer] - pread); - short *p = (short*)pcm; - for(int i = 0; i < size / sizeof(short); i++) { - ringbuffer[pwrite[peer] % RINGBUFFER] += p[i]; - pwrite[peer]++; + if(pwrite[peer] < pread) { + pwrite[peer] = pread + (1.5 * buffer_size); + // printf(">>\n"); } - // If pwrite is behind, repeat the last sample. - if((pwrite[peer] - pread) < BUFSZ) { - ringbuffer[pwrite[peer] % RINGBUFFER] = - ringbuffer[(pwrite[peer] - 1) % RINGBUFFER]; - pwrite[peer]++; - // printf("repeat\n"); + if(pwrite[peer] > (pread + 6 * buffer_size)) { + pwrite[peer] = pread + (1.5 * buffer_size); + // printf("<<\n"); } - // If pwrite is too far ahead skip the last sample. - if((pwrite[peer] - pread) > (2 * BUFSZ)) { - pwrite[peer]--; - // printf("skip\n"); - } - // printf("pread: %d, pwrite[%d]: %d\n", pread, peer, pwrite[peer]); + short *p = (short*)pcm; + for(int i = 0; i < size / sizeof(short); i++) { + ringbuffer[pwrite[peer] % RINGBUFFER] += p[i]; + pwrite[peer]++; + } } diff --git a/src/soundplayer.h b/src/soundplayer.h index 3679fd2..a236d4f 100644 --- a/src/soundplayer.h +++ b/src/soundplayer.h @@ -60,6 +60,7 @@ private: short ringbuffer[RINGBUFFER]; int pwrite[16]; int pread; + int buffer_size; }; #endif/*__KAIMAN_SOUNDPLAYER_H__*/ -- cgit v1.2.3