summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlfred E. Heggestad <aeh@db.org>2014-04-11 21:39:11 +0200
committerAlfred E. Heggestad <aeh@db.org>2014-04-11 21:39:11 +0200
commitcfa7d320a82be9c85ec31235de4c7b79336bcbe2 (patch)
tree9aa268294e73e0a432fda5f7f7065edf6da31244
parent1790b152bbe7f97906603f53e64172fad273a5e7 (diff)
parent454fb8da6d1408da743e682a00d84f767610863e (diff)
Merge pull request #6 from alfredh/ausrc_api
Ausrc api
-rw-r--r--include/baresip.h6
-rw-r--r--modules/alsa/alsa_play.c20
-rw-r--r--modules/alsa/alsa_src.c18
-rw-r--r--modules/aubridge/device.c5
-rw-r--r--modules/audiounit/player.c11
-rw-r--r--modules/audiounit/recorder.c2
-rw-r--r--modules/auloop/auloop.c16
-rw-r--r--modules/coreaudio/coreaudio.c26
-rw-r--r--modules/coreaudio/coreaudio.h2
-rw-r--r--modules/coreaudio/player.c15
-rw-r--r--modules/coreaudio/recorder.c33
-rw-r--r--modules/gst/gst.c15
-rw-r--r--modules/opensles/player.c16
-rw-r--r--modules/opensles/recorder.c20
-rw-r--r--modules/oss/oss.c50
-rw-r--r--modules/portaudio/portaudio.c8
-rw-r--r--modules/rst/audio.c20
-rw-r--r--modules/sndio/sndio.c43
-rw-r--r--modules/winwave/play.c4
-rw-r--r--modules/winwave/src.c4
-rw-r--r--src/audio.c32
-rw-r--r--src/play.c15
22 files changed, 153 insertions, 228 deletions
diff --git a/include/baresip.h b/include/baresip.h
index 1e5f402..6f0deae 100644
--- a/include/baresip.h
+++ b/include/baresip.h
@@ -257,13 +257,12 @@ struct ausrc_st;
/** Audio Source parameters */
struct ausrc_prm {
- int fmt; /**< Audio format (enum aufmt) */
uint32_t srate; /**< Sampling rate in [Hz] */
uint8_t ch; /**< Number of channels */
uint32_t ptime; /**< Wanted packet-time in [ms] */
};
-typedef void (ausrc_read_h)(const uint8_t *buf, size_t sz, void *arg);
+typedef void (ausrc_read_h)(const int16_t *sampv, size_t sampc, void *arg);
typedef void (ausrc_error_h)(int err, const char *str, void *arg);
typedef int (ausrc_alloc_h)(struct ausrc_st **stp, struct ausrc *ausrc,
@@ -289,13 +288,12 @@ struct auplay_st;
/** Audio Player parameters */
struct auplay_prm {
- int fmt; /**< Audio format (enum aufmt) */
uint32_t srate; /**< Sampling rate in [Hz] */
uint8_t ch; /**< Number of channels */
uint32_t ptime; /**< Wanted packet-time in [ms] */
};
-typedef bool (auplay_write_h)(uint8_t *buf, size_t sz, void *arg);
+typedef void (auplay_write_h)(int16_t *sampv, size_t sampc, void *arg);
typedef int (auplay_alloc_h)(struct auplay_st **stp, struct auplay *ap,
struct auplay_prm *prm, const char *device,
diff --git a/modules/alsa/alsa_play.c b/modules/alsa/alsa_play.c
index a0afa4e..ebd8146 100644
--- a/modules/alsa/alsa_play.c
+++ b/modules/alsa/alsa_play.c
@@ -21,7 +21,8 @@ struct auplay_st {
pthread_t thread;
bool run;
snd_pcm_t *write;
- struct mbuf *mbw;
+ int16_t *sampv;
+ size_t sampc;
auplay_write_h *wh;
void *arg;
struct auplay_prm prm;
@@ -42,7 +43,7 @@ static void auplay_destructor(void *arg)
if (st->write)
snd_pcm_close(st->write);
- mem_deref(st->mbw);
+ mem_deref(st->sampv);
mem_deref(st->ap);
mem_deref(st->device);
}
@@ -59,13 +60,13 @@ static void *write_thread(void *arg)
while (st->run) {
const int samples = num_frames;
- st->wh(st->mbw->buf, st->mbw->size, st->arg);
+ st->wh(st->sampv, st->sampc, st->arg);
- n = snd_pcm_writei(st->write, st->mbw->buf, samples);
+ n = snd_pcm_writei(st->write, st->sampv, samples);
if (-EPIPE == n) {
snd_pcm_prepare(st->write);
- n = snd_pcm_writei(st->write, st->mbw->buf, samples);
+ n = snd_pcm_writei(st->write, st->sampv, samples);
if (n != samples) {
warning("alsa: write error: %s\n",
snd_strerror(n));
@@ -89,14 +90,11 @@ int alsa_play_alloc(struct auplay_st **stp, struct auplay *ap,
auplay_write_h *wh, void *arg)
{
struct auplay_st *st;
- uint32_t sampc;
int num_frames;
int err;
if (!stp || !ap || !prm || !wh)
return EINVAL;
- if (prm->fmt != AUFMT_S16LE)
- return EINVAL;
if (!str_isset(device))
device = alsa_dev;
@@ -114,11 +112,11 @@ int alsa_play_alloc(struct auplay_st **stp, struct auplay *ap,
st->wh = wh;
st->arg = arg;
- sampc = prm->srate * prm->ch * prm->ptime / 1000;
+ st->sampc = prm->srate * prm->ch * prm->ptime / 1000;
num_frames = st->prm.srate * st->prm.ptime / 1000;
- st->mbw = mbuf_alloc(2 * sampc);
- if (!st->mbw) {
+ st->sampv = mem_alloc(2 * st->sampc, NULL);
+ if (!st->sampv) {
err = ENOMEM;
goto out;
}
diff --git a/modules/alsa/alsa_src.c b/modules/alsa/alsa_src.c
index 67e6bcb..f95765c 100644
--- a/modules/alsa/alsa_src.c
+++ b/modules/alsa/alsa_src.c
@@ -21,7 +21,8 @@ struct ausrc_st {
pthread_t thread;
bool run;
snd_pcm_t *read;
- struct mbuf *mbr;
+ int16_t *sampv;
+ size_t sampc;
ausrc_read_h *rh;
void *arg;
struct ausrc_prm prm;
@@ -42,7 +43,7 @@ static void ausrc_destructor(void *arg)
if (st->read)
snd_pcm_close(st->read);
- mem_deref(st->mbr);
+ mem_deref(st->sampv);
mem_deref(st->as);
mem_deref(st->device);
}
@@ -65,7 +66,7 @@ static void *read_thread(void *arg)
}
while (st->run) {
- err = snd_pcm_readi(st->read, st->mbr->buf, num_frames);
+ err = snd_pcm_readi(st->read, st->sampv, num_frames);
if (err == -EPIPE) {
snd_pcm_prepare(st->read);
continue;
@@ -74,7 +75,7 @@ static void *read_thread(void *arg)
continue;
}
- st->rh(st->mbr->buf, err * 2 * st->prm.ch, st->arg);
+ st->rh(st->sampv, err * st->prm.ch, st->arg);
}
out:
@@ -88,7 +89,6 @@ int alsa_src_alloc(struct ausrc_st **stp, struct ausrc *as,
ausrc_read_h *rh, ausrc_error_h *errh, void *arg)
{
struct ausrc_st *st;
- uint32_t sampc;
int num_frames;
int err;
(void)ctx;
@@ -96,8 +96,6 @@ int alsa_src_alloc(struct ausrc_st **stp, struct ausrc *as,
if (!stp || !as || !prm || !rh)
return EINVAL;
- if (prm->fmt != AUFMT_S16LE)
- return EINVAL;
if (!str_isset(device))
device = alsa_dev;
@@ -115,11 +113,11 @@ int alsa_src_alloc(struct ausrc_st **stp, struct ausrc *as,
st->rh = rh;
st->arg = arg;
- sampc = prm->srate * prm->ch * prm->ptime / 1000;
+ st->sampc = prm->srate * prm->ch * prm->ptime / 1000;
num_frames = st->prm.srate * st->prm.ptime / 1000;
- st->mbr = mbuf_alloc(2 * sampc);
- if (!st->mbr) {
+ st->sampv = mem_alloc(2 * st->sampc, NULL);
+ if (!st->sampv) {
err = ENOMEM;
goto out;
}
diff --git a/modules/aubridge/device.c b/modules/aubridge/device.c
index b6b7e09..2cd9a60 100644
--- a/modules/aubridge/device.c
+++ b/modules/aubridge/device.c
@@ -88,8 +88,7 @@ static void *device_thread(void *arg)
continue;
if (dev->auplay && dev->auplay->wh) {
- dev->auplay->wh((void *)sampv_in, 2 * sampc_in,
- dev->auplay->arg);
+ dev->auplay->wh(sampv_in, sampc_in, dev->auplay->arg);
}
err = auresamp(&rs,
@@ -100,7 +99,7 @@ static void *device_thread(void *arg)
}
if (dev->ausrc && dev->ausrc->rh) {
- dev->ausrc->rh((void *)sampv_out, 2 * sampc_out,
+ dev->ausrc->rh(sampv_out, sampc_out,
dev->ausrc->arg);
}
diff --git a/modules/audiounit/player.c b/modules/audiounit/player.c
index 0c3a2d1..b875568 100644
--- a/modules/audiounit/player.c
+++ b/modules/audiounit/player.c
@@ -11,9 +11,6 @@
#include "audiounit.h"
-static uint8_t silbuf[4096]; /* silence */
-
-
struct auplay_st {
struct auplay *ap; /* inheritance */
struct audiosess_st *sess;
@@ -72,13 +69,7 @@ static OSStatus output_callback(void *inRefCon,
AudioBuffer *ab = &ioData->mBuffers[i];
- if (!wh(ab->mData, ab->mDataByteSize, arg)) {
-
- if (ab->mDataByteSize < sizeof(silbuf))
- ab->mData = silbuf;
- else
- memset(ab->mData, 0, ab->mDataByteSize);
- }
+ wh(ab->mData, ab->mDataByteSize/2, arg);
}
return 0;
diff --git a/modules/audiounit/recorder.c b/modules/audiounit/recorder.c
index 4b460d6..83943e3 100644
--- a/modules/audiounit/recorder.c
+++ b/modules/audiounit/recorder.c
@@ -78,7 +78,7 @@ static OSStatus input_callback(void *inRefCon,
if (ret)
return ret;
- rh(abl.mBuffers[0].mData, abl.mBuffers[0].mDataByteSize, arg);
+ rh(abl.mBuffers[0].mData, abl.mBuffers[0].mDataByteSize/2, arg);
return 0;
}
diff --git a/modules/auloop/auloop.c b/modules/auloop/auloop.c
index 821c5e6..b432da9 100644
--- a/modules/auloop/auloop.c
+++ b/modules/auloop/auloop.c
@@ -112,21 +112,21 @@ static int codec_read(struct audio_loop *al, int16_t *sampv, size_t sampc)
}
-static void read_handler(const uint8_t *buf, size_t sz, void *arg)
+static void read_handler(const int16_t *sampv, size_t sampc, void *arg)
{
struct audio_loop *al = arg;
int err;
++al->n_read;
- err = aubuf_write(al->ab, buf, sz);
+ err = aubuf_write_samp(al->ab, sampv, sampc);
if (err) {
warning("auloop: aubuf_write: %m\n", err);
}
}
-static bool write_handler(uint8_t *buf, size_t sz, void *arg)
+static void write_handler(int16_t *sampv, size_t sampc, void *arg)
{
struct audio_loop *al = arg;
int err;
@@ -135,17 +135,15 @@ static bool write_handler(uint8_t *buf, size_t sz, void *arg)
/* read from beginning */
if (al->ac) {
- err = codec_read(al, (void *)buf, sz/2);
+ err = codec_read(al, sampv, sampc);
if (err) {
warning("auloop: codec_read error "
- "on %u bytes (%m)\n", sz, err);
+ "on %zu samples (%m)\n", sampc, err);
}
}
else {
- aubuf_read(al->ab, buf, sz);
+ aubuf_read_samp(al->ab, sampv, sampc);
}
-
- return true;
}
@@ -223,7 +221,6 @@ static int auloop_reset(struct audio_loop *al)
if (err)
return err;
- auplay_prm.fmt = AUFMT_S16LE;
auplay_prm.srate = al->srate;
auplay_prm.ch = al->ch;
auplay_prm.ptime = PTIME;
@@ -236,7 +233,6 @@ static int auloop_reset(struct audio_loop *al)
return err;
}
- ausrc_prm.fmt = AUFMT_S16LE;
ausrc_prm.srate = al->srate;
ausrc_prm.ch = al->ch;
ausrc_prm.ptime = PTIME;
diff --git a/modules/coreaudio/coreaudio.c b/modules/coreaudio/coreaudio.c
index bb6ea64..0d21e2b 100644
--- a/modules/coreaudio/coreaudio.c
+++ b/modules/coreaudio/coreaudio.c
@@ -14,32 +14,6 @@ static struct auplay *auplay;
static struct ausrc *ausrc;
-int audio_fmt(enum aufmt fmt)
-{
- switch (fmt) {
-
- case AUFMT_S16LE: return kAudioFormatLinearPCM;
- case AUFMT_PCMA: return kAudioFormatALaw;
- case AUFMT_PCMU: return kAudioFormatULaw;
- default:
- warning("coreaudio: unknown format %d\n", fmt);
- return -1;
- }
-}
-
-
-int bytesps(enum aufmt fmt)
-{
- switch (fmt) {
-
- case AUFMT_S16LE: return 2;
- case AUFMT_PCMA: return 1;
- case AUFMT_PCMU: return 1;
- default: return 0;
- }
-}
-
-
#if TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_2_0
static void interruptionListener(void *data, UInt32 inInterruptionState)
{
diff --git a/modules/coreaudio/coreaudio.h b/modules/coreaudio/coreaudio.h
index 530e45e..67cb7d5 100644
--- a/modules/coreaudio/coreaudio.h
+++ b/modules/coreaudio/coreaudio.h
@@ -8,8 +8,6 @@
int audio_session_enable(void);
void audio_session_disable(void);
-int audio_fmt(enum aufmt fmt);
-int bytesps(enum aufmt fmt);
int coreaudio_player_alloc(struct auplay_st **stp, struct auplay *ap,
struct auplay_prm *prm, const char *device,
diff --git a/modules/coreaudio/player.c b/modules/coreaudio/player.c
index 68aca4e..fc53b8c 100644
--- a/modules/coreaudio/player.c
+++ b/modules/coreaudio/player.c
@@ -72,10 +72,7 @@ static void play_handler(void *userData, AudioQueueRef outQ,
if (!wh)
return;
- if (!wh(outQB->mAudioData, outQB->mAudioDataByteSize, arg)) {
- /* Set the buffer to silence */
- memset(outQB->mAudioData, 0, outQB->mAudioDataByteSize);
- }
+ wh(outQB->mAudioData, outQB->mAudioDataByteSize/2, arg);
AudioQueueEnqueueBuffer(outQ, outQB, 0, NULL);
}
@@ -110,17 +107,17 @@ int coreaudio_player_alloc(struct auplay_st **stp, struct auplay *ap,
goto out;
fmt.mSampleRate = (Float64)prm->srate;
- fmt.mFormatID = audio_fmt(prm->fmt);
+ fmt.mFormatID = kAudioFormatLinearPCM;
fmt.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger |
kAudioFormatFlagIsPacked;
#ifdef __BIG_ENDIAN__
fmt.mFormatFlags |= kAudioFormatFlagIsBigEndian;
#endif
fmt.mFramesPerPacket = 1;
- fmt.mBytesPerFrame = prm->ch * bytesps(prm->fmt);
- fmt.mBytesPerPacket = prm->ch * bytesps(prm->fmt);
+ fmt.mBytesPerFrame = prm->ch * 2;
+ fmt.mBytesPerPacket = prm->ch * 2;
fmt.mChannelsPerFrame = prm->ch;
- fmt.mBitsPerChannel = 8*bytesps(prm->fmt);
+ fmt.mBitsPerChannel = 16;
status = AudioQueueNewOutput(&fmt, play_handler, st, NULL,
kCFRunLoopCommonModes, 0, &st->queue);
@@ -131,7 +128,7 @@ int coreaudio_player_alloc(struct auplay_st **stp, struct auplay *ap,
}
sampc = prm->srate * prm->ch * prm->ptime / 1000;
- bytc = sampc * bytesps(prm->fmt);
+ bytc = sampc * 2;
for (i=0; i<ARRAY_SIZE(st->buf); i++) {
diff --git a/modules/coreaudio/recorder.c b/modules/coreaudio/recorder.c
index b1f91fc..40156fa 100644
--- a/modules/coreaudio/recorder.c
+++ b/modules/coreaudio/recorder.c
@@ -20,7 +20,6 @@ struct ausrc_st {
AudioQueueRef queue;
AudioQueueBufferRef buf[BUFC];
pthread_mutex_t mutex;
- struct mbuf *mb;
ausrc_read_h *rh;
void *arg;
unsigned int ptime;
@@ -49,7 +48,6 @@ static void ausrc_destructor(void *arg)
AudioQueueDispose(st->queue, true);
}
- mem_deref(st->mb);
mem_deref(st->as);
pthread_mutex_destroy(&st->mutex);
@@ -63,10 +61,8 @@ static void record_handler(void *userData, AudioQueueRef inQ,
const AudioStreamPacketDescription *inPacketDesc)
{
struct ausrc_st *st = userData;
- struct mbuf *mb = st->mb;
unsigned int ptime;
ausrc_read_h *rh;
- size_t sz, sp;
void *arg;
(void)inStartTime;
(void)inNumPackets;
@@ -81,18 +77,7 @@ static void record_handler(void *userData, AudioQueueRef inQ,
if (!rh)
return;
- sz = inQB->mAudioDataByteSize;
- sp = mbuf_get_space(mb);
-
- if (sz >= sp) {
- mbuf_write_mem(mb, inQB->mAudioData, sp);
- rh(mb->buf, (uint32_t)mb->size, arg);
- mb->pos = 0;
- mbuf_write_mem(mb, (uint8_t *)inQB->mAudioData + sp, sz - sp);
- }
- else {
- mbuf_write_mem(mb, inQB->mAudioData, sz);
- }
+ rh(inQB->mAudioData, inQB->mAudioDataByteSize/2, arg);
AudioQueueEnqueueBuffer(inQ, inQB, 0, NULL);
@@ -132,13 +117,7 @@ int coreaudio_recorder_alloc(struct ausrc_st **stp, struct ausrc *as,
st->arg = arg;
sampc = prm->srate * prm->ch * prm->ptime / 1000;
- bytc = sampc * bytesps(prm->fmt);
-
- st->mb = mbuf_alloc(bytc);
- if (!st->mb) {
- err = ENOMEM;
- goto out;
- }
+ bytc = sampc * 2;
err = pthread_mutex_init(&st->mutex, NULL);
if (err)
@@ -149,7 +128,7 @@ int coreaudio_recorder_alloc(struct ausrc_st **stp, struct ausrc *as,
goto out;
fmt.mSampleRate = (Float64)prm->srate;
- fmt.mFormatID = audio_fmt(prm->fmt);
+ fmt.mFormatID = kAudioFormatLinearPCM;
fmt.mFormatFlags = kLinearPCMFormatFlagIsSignedInteger |
kAudioFormatFlagIsPacked;
#ifdef __BIG_ENDIAN__
@@ -157,10 +136,10 @@ int coreaudio_recorder_alloc(struct ausrc_st **stp, struct ausrc *as,
#endif
fmt.mFramesPerPacket = 1;
- fmt.mBytesPerFrame = prm->ch * bytesps(prm->fmt);
- fmt.mBytesPerPacket = prm->ch * bytesps(prm->fmt);
+ fmt.mBytesPerFrame = prm->ch * 2;
+ fmt.mBytesPerPacket = prm->ch * 2;
fmt.mChannelsPerFrame = prm->ch;
- fmt.mBitsPerChannel = 8*bytesps(prm->fmt);
+ fmt.mBitsPerChannel = 16;
status = AudioQueueNewInput(&fmt, record_handler, st, NULL,
kCFRunLoopCommonModes, 0, &st->queue);
diff --git a/modules/gst/gst.c b/modules/gst/gst.c
index 7af74f4..aefd9de 100644
--- a/modules/gst/gst.c
+++ b/modules/gst/gst.c
@@ -43,6 +43,7 @@ struct ausrc_st {
struct ausrc_prm prm; /**< Read parameters */
struct aubuf *aubuf; /**< Packet buffer */
uint32_t psize; /**< Packet size in bytes */
+ size_t sampc;
/* Gstreamer */
char *uri;
@@ -168,15 +169,15 @@ static void format_check(struct ausrc_st *st, GstStructure *s)
static void play_packet(struct ausrc_st *st)
{
- uint8_t buf[st->psize];
+ int16_t buf[st->sampc];
/* timed read from audio-buffer */
- if (aubuf_get(st->aubuf, st->prm.ptime, buf, sizeof(buf)))
+ if (aubuf_get_samp(st->aubuf, st->prm.ptime, buf, st->sampc))
return;
/* call read handler */
if (st->rh)
- st->rh(buf, sizeof(buf), st->arg);
+ st->rh(buf, st->sampc, st->arg);
}
@@ -360,7 +361,6 @@ static int gst_alloc(struct ausrc_st **stp, struct ausrc *as,
ausrc_read_h *rh, ausrc_error_h *errh, void *arg)
{
struct ausrc_st *st;
- unsigned sampc;
int err;
(void)ctx;
@@ -371,8 +371,6 @@ static int gst_alloc(struct ausrc_st **stp, struct ausrc *as,
if (!prm)
return EINVAL;
- prm->fmt = AUFMT_S16LE;
-
st = mem_zalloc(sizeof(*st), gst_destructor);
if (!st)
return ENOMEM;
@@ -388,9 +386,8 @@ static int gst_alloc(struct ausrc_st **stp, struct ausrc *as,
st->prm = *prm;
- sampc = prm->srate * prm->ch * prm->ptime / 1000;
-
- st->psize = 2 * sampc;
+ st->sampc = prm->srate * prm->ch * prm->ptime / 1000;
+ st->psize = 2 * st->sampc;
err = aubuf_alloc(&st->aubuf, st->psize, 0);
if (err)
diff --git a/modules/opensles/player.c b/modules/opensles/player.c
index a317e5d..b9c9b68 100644
--- a/modules/opensles/player.c
+++ b/modules/opensles/player.c
@@ -17,7 +17,8 @@
struct auplay_st {
struct auplay *ap; /* inheritance */
- int16_t buf[160 * 2];
+ int16_t *sampv;
+ size_t sampc;
auplay_write_h *wh;
void *arg;
@@ -38,6 +39,7 @@ static void auplay_destructor(void *arg)
if (st->outputMixObject != NULL)
(*st->outputMixObject)->Destroy(st->outputMixObject);
+ mem_deref(st->sampv);
mem_deref(st->ap);
}
@@ -46,9 +48,9 @@ static void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
{
struct auplay_st *st = context;
- st->wh((void *)st->buf, sizeof(st->buf), st->arg);
+ st->wh(st->sampv, st->sampc, st->arg);
- (*st->BufferQueue)->Enqueue(bq, st->buf, sizeof(st->buf));
+ (*st->BufferQueue)->Enqueue(bq, st->sampv, st->sampc * 2);
}
@@ -151,6 +153,14 @@ int opensles_player_alloc(struct auplay_st **stp, struct auplay *ap,
st->wh = wh;
st->arg = arg;
+ st->sampc = prm->srate * prm->ch * prm->ptime / 1000;
+
+ st->sampv = mem_alloc(2 * st->sampc, NULL);
+ if (!st->sampv) {
+ err = ENOMEM;
+ goto out;
+ }
+
err = createOutput(st);
if (err)
goto out;
diff --git a/modules/opensles/recorder.c b/modules/opensles/recorder.c
index 6301451..4e1e6c7 100644
--- a/modules/opensles/recorder.c
+++ b/modules/opensles/recorder.c
@@ -18,7 +18,9 @@
struct ausrc_st {
struct ausrc *as; /* inheritance */
- int16_t buf[160];
+ int16_t *sampv;
+ size_t sampc;
+ uint32_t ptime;
pthread_t thread;
bool run;
ausrc_read_h *rh;
@@ -42,6 +44,7 @@ static void ausrc_destructor(void *arg)
if (st->recObject != NULL)
(*st->recObject)->Destroy(st->recObject);
+ mem_deref(st->sampv);
mem_deref(st->as);
}
@@ -68,12 +71,13 @@ static void *record_thread(void *arg)
#endif
r = (*st->recBufferQueue)->Enqueue(st->recBufferQueue,
- st->buf, sizeof(st->buf));
+ st->sampv,
+ st->sampc * 2);
if (r != SL_RESULT_SUCCESS) {
DEBUG_WARNING("Enqueue: r = %d\n", r);
}
- ts += 20;
+ ts += st->ptime;
}
return NULL;
@@ -85,7 +89,7 @@ static void bqRecorderCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
struct ausrc_st *st = context;
(void)bq;
- st->rh((void *)st->buf, sizeof(st->buf), st->arg);
+ st->rh(st->sampv, st->sampc, st->arg);
}
@@ -192,6 +196,14 @@ int opensles_recorder_alloc(struct ausrc_st **stp, struct ausrc *as,
st->as = mem_ref(as);
st->rh = rh;
st->arg = arg;
+ st->ptime = prm->ptime;
+ st->sampc = prm->srate * prm->ch * prm->ptime / 1000;
+
+ st->sampv = mem_alloc(2 * st->sampc, NULL);
+ if (!st->sampv) {
+ err = ENOMEM;
+ goto out;
+ }
err = createAudioRecorder(st, prm);
if (err) {
diff --git a/modules/oss/oss.c b/modules/oss/oss.c
index d7bff51..38d34f3 100644
--- a/modules/oss/oss.c
+++ b/modules/oss/oss.c
@@ -23,7 +23,8 @@
struct ausrc_st {
struct ausrc *as; /* inheritance */
int fd;
- struct mbuf *mb;
+ int16_t *sampv;
+ size_t sampc;
ausrc_read_h *rh;
ausrc_error_h *errh;
void *arg;
@@ -34,8 +35,8 @@ struct auplay_st {
pthread_t thread;
bool run;
int fd;
- uint8_t *buf;
- uint32_t sz;
+ int16_t *sampv;
+ size_t sampc;
auplay_write_h *wh;
void *arg;
};
@@ -139,7 +140,7 @@ static void auplay_destructor(void *arg)
(void)close(st->fd);
}
- mem_deref(st->buf);
+ mem_deref(st->sampv);
mem_deref(st->ap);
}
@@ -153,7 +154,7 @@ static void ausrc_destructor(void *arg)
(void)close(st->fd);
}
- mem_deref(st->mb);
+ mem_deref(st->sampv);
mem_deref(st->as);
}
@@ -161,22 +162,14 @@ static void ausrc_destructor(void *arg)
static void read_handler(int flags, void *arg)
{
struct ausrc_st *st = arg;
- struct mbuf *mb = st->mb;
int n;
(void)flags;
- n = read(st->fd, mbuf_buf(mb), mbuf_get_space(mb));
+ n = read(st->fd, st->sampv, st->sampc*2);
if (n <= 0)
return;
- mb->pos += n;
-
- if (mb->pos < mb->size)
- return;
-
- st->rh(mb->buf, mb->size, st->arg);
-
- mb->pos = 0;
+ st->rh(st->sampv, n/2, st->arg);
}
@@ -187,9 +180,9 @@ static void *play_thread(void *arg)
while (st->run) {
- st->wh(st->buf, st->sz, st->arg);
+ st->wh(st->sampv, st->sampc, st->arg);
- n = write(st->fd, st->buf, st->sz);
+ n = write(st->fd, st->sampv, st->sampc*2);
if (n < 0) {
warning("oss: write: %m\n", errno);
break;
@@ -206,7 +199,6 @@ static int src_alloc(struct ausrc_st **stp, struct ausrc *as,
ausrc_read_h *rh, ausrc_error_h *errh, void *arg)
{
struct ausrc_st *st;
- unsigned sampc;
int err;
(void)ctx;
@@ -227,12 +219,10 @@ static int src_alloc(struct ausrc_st **stp, struct ausrc *as,
if (!device)
device = oss_dev;
- prm->fmt = AUFMT_S16LE;
-
- sampc = prm->srate * prm->ch * prm->ptime / 1000;
+ st->sampc = prm->srate * prm->ch * prm->ptime / 1000;
- st->mb = mbuf_alloc(2 * sampc);
- if (!st->mb) {
+ st->sampv = mem_alloc(2 * st->sampc, NULL);
+ if (!st->sampv) {
err = ENOMEM;
goto out;
}
@@ -247,7 +237,7 @@ static int src_alloc(struct ausrc_st **stp, struct ausrc *as,
if (err)
goto out;
- err = oss_reset(st->fd, prm->srate, prm->ch, sampc, 1);
+ err = oss_reset(st->fd, prm->srate, prm->ch, st->sampc, 1);
if (err)
goto out;
@@ -268,7 +258,6 @@ static int play_alloc(struct auplay_st **stp, struct auplay *ap,
auplay_write_h *wh, void *arg)
{
struct auplay_st *st;
- unsigned sampc;
int err;
if (!stp || !ap || !prm || !wh)
@@ -285,13 +274,10 @@ static int play_alloc(struct auplay_st **stp, struct auplay *ap,
if (!device)
device = oss_dev;
- prm->fmt = AUFMT_S16LE;
-
- sampc = prm->srate * prm->ch * prm->ptime / 1000;
+ st->sampc = prm->srate * prm->ch * prm->ptime / 1000;
- st->sz = 2 * sampc;
- st->buf = mem_alloc(st->sz, NULL);
- if (!st->buf) {
+ st->sampv = mem_alloc(st->sampc * 2, NULL);
+ if (!st->sampv) {
err = ENOMEM;
goto out;
}
@@ -302,7 +288,7 @@ static int play_alloc(struct auplay_st **stp, struct auplay *ap,
goto out;
}
- err = oss_reset(st->fd, prm->srate, prm->ch, sampc, 0);
+ err = oss_reset(st->fd, prm->srate, prm->ch, st->sampc, 0);
if (err)
goto out;
diff --git a/modules/portaudio/portaudio.c b/modules/portaudio/portaudio.c
index 305266a..2da18fc 100644
--- a/modules/portaudio/portaudio.c
+++ b/modules/portaudio/portaudio.c
@@ -60,7 +60,7 @@ static int read_callback(const void *inputBuffer, void *outputBuffer,
sampc = frameCount * st->ch;
- st->rh(inputBuffer, 2*sampc, st->arg);
+ st->rh(inputBuffer, sampc, st->arg);
return paContinue;
}
@@ -83,7 +83,7 @@ static int write_callback(const void *inputBuffer, void *outputBuffer,
sampc = frameCount * st->ch;
- st->wh(outputBuffer, 2*sampc, st->arg);
+ st->wh(outputBuffer, sampc, st->arg);
return paContinue;
}
@@ -206,8 +206,6 @@ static int src_alloc(struct ausrc_st **stp, struct ausrc *as,
else
dev_index = Pa_GetDefaultInputDevice();
- prm->fmt = AUFMT_S16LE;
-
st = mem_zalloc(sizeof(*st), ausrc_destructor);
if (!st)
return ENOMEM;
@@ -251,8 +249,6 @@ static int play_alloc(struct auplay_st **stp, struct auplay *ap,
else
dev_index = Pa_GetDefaultOutputDevice();
- prm->fmt = AUFMT_S16LE;
-
st = mem_zalloc(sizeof(*st), auplay_destructor);
if (!st)
return ENOMEM;
diff --git a/modules/rst/audio.c b/modules/rst/audio.c
index 554ec21..8d2a2a8 100644
--- a/modules/rst/audio.c
+++ b/modules/rst/audio.c
@@ -28,6 +28,7 @@ struct ausrc_st {
bool run;
uint32_t psize;
uint32_t ptime;
+ size_t sampc;
};
@@ -60,10 +61,10 @@ static void *play_thread(void *arg)
{
uint64_t now, ts = tmr_jiffies();
struct ausrc_st *st = arg;
- uint8_t *buf;
+ int16_t *sampv;
- buf = mem_alloc(st->psize, NULL);
- if (!buf)
+ sampv = mem_alloc(st->psize, NULL);
+ if (!sampv)
return NULL;
while (st->run) {
@@ -81,14 +82,14 @@ static void *play_thread(void *arg)
}
#endif
- aubuf_read(st->aubuf, buf, st->psize);
+ aubuf_read_samp(st->aubuf, sampv, st->sampc);
- st->rh(buf, st->psize, st->arg);
+ st->rh(sampv, st->sampc, st->arg);
ts += st->ptime;
}
- mem_deref(buf);
+ mem_deref(sampv);
return NULL;
}
@@ -155,7 +156,6 @@ static int alloc_handler(struct ausrc_st **stp, struct ausrc *as,
ausrc_read_h *rh, ausrc_error_h *errh, void *arg)
{
struct ausrc_st *st;
- unsigned sampc;
int err;
if (!stp || !as || !prm || !rh)
@@ -189,12 +189,10 @@ static int alloc_handler(struct ausrc_st **stp, struct ausrc *as,
mpg123_format(st->mp3, prm->srate, prm->ch, MPG123_ENC_SIGNED_16);
mpg123_volume(st->mp3, 0.3);
- sampc = prm->srate * prm->ch * prm->ptime / 1000;
+ st->sampc = prm->srate * prm->ch * prm->ptime / 1000;
st->ptime = prm->ptime;
- st->psize = sampc * 2;
-
- prm->fmt = AUFMT_S16LE;
+ st->psize = st->sampc * 2;
re_printf("rst: audio ptime=%u psize=%u aubuf=[%u:%u]\n",
st->ptime, st->psize,
diff --git a/modules/sndio/sndio.c b/modules/sndio/sndio.c
index ee0fd46..1b54a5a 100644
--- a/modules/sndio/sndio.c
+++ b/modules/sndio/sndio.c
@@ -16,8 +16,8 @@ struct ausrc_st {
struct ausrc *as;
struct sio_hdl *hdl;
pthread_t thread;
- size_t nbytes;
- void *buf;
+ int16_t *sampv;
+ size_t sampc;
int run;
ausrc_read_h *rh;
void *arg;
@@ -27,8 +27,8 @@ struct auplay_st {
struct auplay *ap;
struct sio_hdl *hdl;
pthread_t thread;
- size_t nbytes;
- void *buf;
+ int16_t *sampv;
+ size_t sampc;
int run;
auplay_write_h *wh;
void *arg;
@@ -49,7 +49,6 @@ static struct sio_par *sndio_initpar(void *arg)
sio_initpar(par);
/* sndio doesn't support a-low and u-low */
- prm->fmt = AUFMT_S16LE;
par->bits = 16;
par->bps = SIO_BPS(par->bits);
par->sig = 1;
@@ -73,8 +72,8 @@ static void *read_thread(void *arg)
}
while (st->run) {
- sio_read(st->hdl, st->buf, st->nbytes);
- st->rh(st->buf, st->nbytes, st->arg);
+ size_t n = sio_read(st->hdl, st->sampv, st->sampc*2);
+ st->rh(st->sampv, n/2, st->arg);
}
out:
@@ -92,8 +91,8 @@ static void *write_thread(void *arg)
}
while (st->run) {
- st->wh(st->buf, st->nbytes, st->arg);
- sio_write(st->hdl, st->buf, st->nbytes);
+ st->wh(st->sampv, st->sampc, st->arg);
+ sio_write(st->hdl, st->sampv, st->sampc*2);
}
out:
@@ -110,9 +109,10 @@ static void ausrc_destructor(void *arg)
(void)pthread_join(st->thread, NULL);
}
- sio_close(st->hdl);
+ if (st->hdl)
+ sio_close(st->hdl);
- mem_deref(st->buf);
+ mem_deref(st->sampv);
mem_deref(st->as);
}
@@ -126,9 +126,10 @@ static void auplay_destructor(void *arg)
(void)pthread_join(st->thread, NULL);
}
- sio_close(st->hdl);
+ if (st->hdl)
+ sio_close(st->hdl);
- mem_deref(st->buf);
+ mem_deref(st->sampv);
mem_deref(st->ap);
}
@@ -183,10 +184,10 @@ static int src_alloc(struct ausrc_st **stp, struct ausrc *as,
goto out;
}
- st->nbytes = 2 * par->appbufsz;
- st->buf = mem_alloc(st->nbytes, NULL);
- if (!st->buf) {
- free(par);
+ st->sampc = prm->srate * prm->ch * prm->ptime / 1000;
+
+ st->sampv = mem_alloc(2 * st->sampc, NULL);
+ if (!st->sampv) {
err = ENOMEM;
goto out;
}
@@ -254,10 +255,10 @@ static int play_alloc(struct auplay_st **stp, struct auplay *ap,
goto out;
}
- st->nbytes = 2 * par->appbufsz;
- st->buf = mem_alloc(st->nbytes, NULL);
- if (!st->buf) {
- free(par);
+ st->sampc = prm->srate * prm->ch * prm->ptime / 1000;
+
+ st->sampv = mem_alloc(2 * st->sampc, NULL);
+ if (!st->sampv) {
err = ENOMEM;
goto out;
}
diff --git a/modules/winwave/play.c b/modules/winwave/play.c
index 1de2e1f..29568ba 100644
--- a/modules/winwave/play.c
+++ b/modules/winwave/play.c
@@ -75,7 +75,7 @@ static int dsp_write(struct auplay_st *st)
wh->lpData = (LPSTR)mb->buf;
if (st->wh) {
- st->wh(mb->buf, mb->size, st->arg);
+ st->wh((void *)mb->buf, mb->size/2, st->arg);
}
wh->dwBufferLength = mb->size;
@@ -201,8 +201,6 @@ int winwave_play_alloc(struct auplay_st **stp, struct auplay *ap,
st->wh = wh;
st->arg = arg;
- prm->fmt = AUFMT_S16LE;
-
err = write_stream_open(st, prm);
if (err)
goto out;
diff --git a/modules/winwave/src.c b/modules/winwave/src.c
index 1793bf3..9d23c1e 100644
--- a/modules/winwave/src.c
+++ b/modules/winwave/src.c
@@ -110,7 +110,7 @@ static void CALLBACK waveInCallback(HWAVEOUT hwo,
if (st->inuse < 3)
add_wave_in(st);
- st->rh((uint8_t *)wh->lpData, wh->dwBytesRecorded, st->arg);
+ st->rh((void *)wh->lpData, wh->dwBytesRecorded/2, st->arg);
waveInUnprepareHeader(st->wavein, wh, sizeof(*wh));
st->inuse--;
@@ -193,8 +193,6 @@ int winwave_src_alloc(struct ausrc_st **stp, struct ausrc *as,
st->rh = rh;
st->arg = arg;
- prm->fmt = AUFMT_S16LE;
-
err = read_stream_open(st, prm);
if (err)
diff --git a/src/audio.c b/src/audio.c
index 548b5ac..a3f2600 100644
--- a/src/audio.c
+++ b/src/audio.c
@@ -349,8 +349,7 @@ static void poll_aubuf_tx(struct audio *a)
sampc = tx->psize / 2;
/* timed read from audio-buffer */
- if (aubuf_get_samp(tx->aubuf, tx->ptime, tx->sampv, sampc))
- return;
+ aubuf_read_samp(tx->aubuf, tx->sampv, sampc);
/* optional resampler */
if (tx->resamp.resample) {
@@ -422,16 +421,12 @@ static void check_telev(struct audio *a, struct autx *tx)
* @param buf Buffer to fill with audio samples
* @param sz Number of bytes in buffer
* @param arg Handler argument
- *
- * @return true for valid audio samples, false for silence
*/
-static bool auplay_write_handler(uint8_t *buf, size_t sz, void *arg)
+static void auplay_write_handler(int16_t *sampv, size_t sampc, void *arg)
{
struct aurx *rx = arg;
- aubuf_read(rx->aubuf, buf, sz);
-
- return true;
+ aubuf_read_samp(rx->aubuf, sampv, sampc);
}
@@ -446,18 +441,27 @@ static bool auplay_write_handler(uint8_t *buf, size_t sz, void *arg)
* @param sz Number of bytes in buffer
* @param arg Handler argument
*/
-static void ausrc_read_handler(const uint8_t *buf, size_t sz, void *arg)
+static void ausrc_read_handler(const int16_t *sampv, size_t sampc, void *arg)
{
struct audio *a = arg;
struct autx *tx = &a->tx;
if (tx->muted)
- memset((void *)buf, 0, sz);
+ memset((void *)sampv, 0, sampc*2);
- (void)aubuf_write(tx->aubuf, buf, sz);
+ (void)aubuf_write_samp(tx->aubuf, sampv, sampc);
- if (a->cfg.txmode == AUDIO_MODE_POLL)
- poll_aubuf_tx(a);
+ if (a->cfg.txmode == AUDIO_MODE_POLL) {
+ unsigned i;
+
+ for (i=0; i<16; i++) {
+
+ if (aubuf_cur_size(tx->aubuf) < tx->psize)
+ break;
+
+ poll_aubuf_tx(a);
+ }
+ }
/* Exact timing: send Telephony-Events from here */
check_telev(a, tx);
@@ -927,7 +931,6 @@ static int start_player(struct aurx *rx, struct audio *a)
struct auplay_prm prm;
- prm.fmt = AUFMT_S16LE;
prm.srate = srate_dsp;
prm.ch = channels_dsp;
prm.ptime = rx->ptime;
@@ -1004,7 +1007,6 @@ static int start_source(struct autx *tx, struct audio *a)
struct ausrc_prm prm;
- prm.fmt = AUFMT_S16LE;
prm.srate = srate_dsp;
prm.ch = channels_dsp;
prm.ptime = tx->ptime;
diff --git a/src/play.c b/src/play.c
index fcd06a2..c2faa7b 100644
--- a/src/play.c
+++ b/src/play.c
@@ -83,9 +83,10 @@ static void tmr_polling(void *arg)
/**
* NOTE: DSP cannot be destroyed inside handler
*/
-static bool write_handler(uint8_t *buf, size_t sz, void *arg)
+static void write_handler(int16_t *sampv, size_t sampc, void *arg)
{
struct play *play = arg;
+ size_t sz = sampc * 2;
lock_write_get(play->lock);
@@ -94,22 +95,21 @@ static bool write_handler(uint8_t *buf, size_t sz, void *arg)
if (mbuf_get_left(play->mb) < sz) {
- memset(buf, 0, sz);
- (void)mbuf_read_mem(play->mb, buf, mbuf_get_left(play->mb));
+ memset(sampv, 0, sz);
+ (void)mbuf_read_mem(play->mb, (void *)sampv,
+ mbuf_get_left(play->mb));
play->eof = true;
}
else {
- (void)mbuf_read_mem(play->mb, buf, sz);
+ (void)mbuf_read_mem(play->mb, (void *)sampv, sz);
}
silence:
if (play->eof)
- memset(buf, 0, sz);
+ memset(sampv, 0, sz);
lock_rel(play->lock);
-
- return true;
}
@@ -237,7 +237,6 @@ int play_tone(struct play **playp, struct mbuf *tone, uint32_t srate,
if (err)
goto out;
- wprm.fmt = AUFMT_S16LE;
wprm.ch = ch;
wprm.srate = srate;
wprm.ptime = PTIME;