diff options
-rw-r--r-- | include/baresip.h | 4 | ||||
-rw-r--r-- | modules/alsa/alsa_play.c | 16 | ||||
-rw-r--r-- | modules/aubridge/device.c | 5 | ||||
-rw-r--r-- | modules/audiounit/player.c | 11 | ||||
-rw-r--r-- | modules/audiounit/recorder.c | 2 | ||||
-rw-r--r-- | modules/auloop/auloop.c | 14 | ||||
-rw-r--r-- | modules/coreaudio/player.c | 5 | ||||
-rw-r--r-- | modules/coreaudio/recorder.c | 23 | ||||
-rw-r--r-- | modules/gst/gst.c | 13 | ||||
-rw-r--r-- | modules/oss/oss.c | 18 | ||||
-rw-r--r-- | modules/portaudio/portaudio.c | 4 | ||||
-rw-r--r-- | modules/rst/audio.c | 18 | ||||
-rw-r--r-- | src/audio.c | 30 | ||||
-rw-r--r-- | src/play.c | 14 |
14 files changed, 71 insertions, 106 deletions
diff --git a/include/baresip.h b/include/baresip.h index 8abb2ee..8bfd75d 100644 --- a/include/baresip.h +++ b/include/baresip.h @@ -261,7 +261,7 @@ struct ausrc_prm { 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, @@ -292,7 +292,7 @@ struct auplay_prm { 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 8c9de96..1826f15 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,7 +90,6 @@ 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; @@ -112,10 +112,10 @@ 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); + st->sampv = mem_alloc(2 * st->sampc); if (!st->mbw) { 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 a55997f..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; } diff --git a/modules/coreaudio/player.c b/modules/coreaudio/player.c index 1243450..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); } diff --git a/modules/coreaudio/recorder.c b/modules/coreaudio/recorder.c index 57f2390..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); @@ -134,12 +119,6 @@ int coreaudio_recorder_alloc(struct ausrc_st **stp, struct ausrc *as, sampc = prm->srate * prm->ch * prm->ptime / 1000; bytc = sampc * 2; - st->mb = mbuf_alloc(bytc); - if (!st->mb) { - err = ENOMEM; - goto out; - } - err = pthread_mutex_init(&st->mutex, NULL); if (err) goto out; diff --git a/modules/gst/gst.c b/modules/gst/gst.c index 2f4d7f2..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; @@ -386,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/oss/oss.c b/modules/oss/oss.c index a69d44c..f0644e2 100644 --- a/modules/oss/oss.c +++ b/modules/oss/oss.c @@ -34,8 +34,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 +139,7 @@ static void auplay_destructor(void *arg) (void)close(st->fd); } - mem_deref(st->buf); + mem_deref(st->sampv); mem_deref(st->ap); } @@ -187,9 +187,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; @@ -266,7 +266,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) @@ -283,11 +282,10 @@ static int play_alloc(struct auplay_st **stp, struct auplay *ap, if (!device) device = oss_dev; - 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; } diff --git a/modules/portaudio/portaudio.c b/modules/portaudio/portaudio.c index 764aa81..0e81dff 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; } diff --git a/modules/rst/audio.c b/modules/rst/audio.c index 8809e37..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,10 +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; + st->psize = st->sampc * 2; re_printf("rst: audio ptime=%u psize=%u aubuf=[%u:%u]\n", st->ptime, st->psize, diff --git a/src/audio.c b/src/audio.c index dd43b0a..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); @@ -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; } |