diff --git a/src/media/audio/opensl/audio_player.cpp b/src/media/audio/opensl/audio_player.cpp index 57acc75d7e9f54dcbb5805fa2e52c26e802d4861..5fc9b50a5fd671b0ab2be93ecdde36ebb70645d9 100644 --- a/src/media/audio/opensl/audio_player.cpp +++ b/src/media/audio/opensl/audio_player.cpp @@ -165,10 +165,9 @@ AudioPlayer::AudioPlayer(jami::AudioFormat sampleFormat, result = (*playItf_)->SetPlayState(playItf_, SL_PLAYSTATE_STOPPED); SLASSERT(result); - silentBuf_.cap_ = (format_pcm.containerSize >> 3) * format_pcm.numChannels * bufSize; - silentBuf_.buf_ = new uint8_t[silentBuf_.cap_]; - memset(silentBuf_.buf_, 0, silentBuf_.cap_); + silentBuf_ = {(format_pcm.containerSize >> 3) * format_pcm.numChannels * bufSize}; silentBuf_.size_ = silentBuf_.cap_; + memset(silentBuf_.buf_, 0, silentBuf_.cap_); } AudioPlayer::~AudioPlayer() @@ -197,7 +196,6 @@ AudioPlayer::setBufQueue(AudioQueue* playQ, AudioQueue* freeQ) bool AudioPlayer::start() { - std::lock_guard<std::mutex> lk(m_); JAMI_DBG("OpenSL playback start"); SLuint32 state; SLresult result = (*playItf_)->GetPlayState(playItf_, &state); @@ -233,8 +231,6 @@ void AudioPlayer::stop() { JAMI_DBG("OpenSL playback stop"); - std::lock_guard<std::mutex> lk(m_); - callback_ = {}; SLuint32 state; SLresult result = (*playItf_)->GetPlayState(playItf_, &state); @@ -243,6 +239,8 @@ AudioPlayer::stop() if (state == SL_PLAYSTATE_STOPPED) return; + std::lock_guard<std::mutex> lk(m_); + callback_ = {}; result = (*playItf_)->SetPlayState(playItf_, SL_PLAYSTATE_STOPPED); SLASSERT(result); diff --git a/src/media/audio/opensl/audio_recorder.cpp b/src/media/audio/opensl/audio_recorder.cpp index 3f00df441ff48953778d47610d24c7ba0a429080..0e42b7df510cd3aff1570e780ba3d40056ca9592 100644 --- a/src/media/audio/opensl/audio_recorder.cpp +++ b/src/media/audio/opensl/audio_recorder.cpp @@ -36,32 +36,35 @@ void AudioRecorder::processSLCallback(SLAndroidSimpleBufferQueueItf bq) { try { - assert(bq == recBufQueueItf_); + SLASSERT(bq == recBufQueueItf_); sample_buf* dataBuf {nullptr}; - devShadowQueue_.front(&dataBuf); - devShadowQueue_.pop(); - dataBuf->size_ = dataBuf->cap_; // device only calls us when it is really full - recQueue_->push(dataBuf); + if (devShadowQueue_.front(&dataBuf)) { + devShadowQueue_.pop(); + dataBuf->size_ = dataBuf->cap_; // device only calls us when it is really full + if (dataBuf != &silentBuf_) + recQueue_->push(dataBuf); + } sample_buf* freeBuf; while (freeQueue_->front(&freeBuf) && devShadowQueue_.push(freeBuf)) { freeQueue_->pop(); - SLresult result = (*bq)->Enqueue(bq, freeBuf->buf_, freeBuf->cap_); - SLASSERT(result); + SLASSERT((*bq)->Enqueue(bq, freeBuf->buf_, freeBuf->cap_)); } // should leave the device to sleep to save power if no buffers - /*if (devShadowQueue_.size() == 0) { - (*recItf_)->SetRecordState(recItf_, SL_RECORDSTATE_STOPPED); - }*/ + if (devShadowQueue_.size() == 0) { + // JAMI_WARN("OpenSL: processSLCallback empty queue"); + (*bq)->Enqueue(bq, silentBuf_.buf_, silentBuf_.cap_); + devShadowQueue_.push(&silentBuf_); + } if (callback_) callback_(); } catch (const std::exception& e) { - JAMI_ERR("processSLCallback exception: %s", e.what()); + JAMI_ERR("OpenSL: processSLCallback exception: %s", e.what()); } } -AudioRecorder::AudioRecorder(jami::AudioFormat sampleFormat, SLEngineItf slEngine) +AudioRecorder::AudioRecorder(jami::AudioFormat sampleFormat, size_t bufSize, SLEngineItf slEngine) : sampleInfo_(sampleFormat) { JAMI_DBG("Creating OpenSL record stream"); @@ -199,6 +202,10 @@ AudioRecorder::AudioRecorder(jami::AudioFormat sampleFormat, SLEngineItf slEngin result = (*recBufQueueItf_)->RegisterCallback(recBufQueueItf_, bqRecorderCallback, this); SLASSERT(result); + + silentBuf_ = {(format_pcm.containerSize >> 3) * format_pcm.numChannels * bufSize}; + silentBuf_.size_ = silentBuf_.cap_; + memset(silentBuf_.buf_, 0, silentBuf_.cap_); } bool diff --git a/src/media/audio/opensl/audio_recorder.h b/src/media/audio/opensl/audio_recorder.h index 3d1b6bbebf35274700f1f043b273c2a0f6a79437..53fae57ea06e541478949e23ae5696f412624b8a 100644 --- a/src/media/audio/opensl/audio_recorder.h +++ b/src/media/audio/opensl/audio_recorder.h @@ -39,12 +39,13 @@ class AudioRecorder AudioQueue* recQueue_ {nullptr}; // user AudioQueue devShadowQueue_ {DEVICE_SHADOW_BUFFER_QUEUE_LEN}; // owner uint32_t audioBufCount; + sample_buf silentBuf_; EngineCallback callback_ {}; bool hasNativeAEC_ {false}; public: - explicit AudioRecorder(jami::AudioFormat, SLEngineItf engineEngine); + explicit AudioRecorder(jami::AudioFormat, size_t bufSize, SLEngineItf engineEngine); ~AudioRecorder(); NON_COPYABLE(AudioRecorder); diff --git a/src/media/audio/opensl/buf_manager.h b/src/media/audio/opensl/buf_manager.h index 4de1f8eaec935296dfa366d2308d05603eb5a657..62738641b5e1ecd3c80ce6532704acff1a4ace1b 100644 --- a/src/media/audio/opensl/buf_manager.h +++ b/src/media/audio/opensl/buf_manager.h @@ -180,6 +180,10 @@ struct sample_buf : buf_(new uint8_t[alloc]) , cap_(size) {} + sample_buf(size_t alloc) + : buf_(new uint8_t[alloc]) + , cap_(alloc) + {} sample_buf(sample_buf&& o) : buf_(o.buf_) , cap_(o.cap_) diff --git a/src/media/audio/opensl/opensllayer.cpp b/src/media/audio/opensl/opensllayer.cpp index efb2fb040fd5b7eb135852430c70216d7836cd06..0e61f7ee9c70b98ebb566954ec7d881931c9678c 100644 --- a/src/media/audio/opensl/opensllayer.cpp +++ b/src/media/audio/opensl/opensllayer.cpp @@ -93,7 +93,7 @@ OpenSLLayer::startStream(AudioDeviceType stream) if (not recorder_) { std::lock_guard<std::mutex> lck(recMtx); try { - recorder_.reset(new opensl::AudioRecorder(hardwareFormat_, engineInterface_)); + recorder_.reset(new opensl::AudioRecorder(hardwareFormat_, hardwareBuffSize_, engineInterface_)); recorder_->setBufQueues(&freeRecBufQueue_, &recBufQueue_); recorder_->registerCallback(std::bind(&OpenSLLayer::engineServiceRec, this)); setHasNativeAEC(recorder_->hasNativeAEC()); @@ -284,21 +284,6 @@ OpenSLLayer::engineServiceRec() return; } -void -OpenSLLayer::initAudioCapture() -{ - using namespace std::placeholders; - std::lock_guard<std::mutex> lck(recMtx); - try { - recorder_.reset(new opensl::AudioRecorder(hardwareFormat_, engineInterface_)); - recorder_->setBufQueues(&freeRecBufQueue_, &recBufQueue_); - recorder_->registerCallback(std::bind(&OpenSLLayer::engineServiceRec, this)); - setHasNativeAEC(recorder_->hasNativeAEC()); - } catch (const std::exception& e) { - JAMI_ERR("Error initializing audio capture: %s", e.what()); - } -} - void OpenSLLayer::startAudioCapture() { diff --git a/src/media/audio/opensl/opensllayer.h b/src/media/audio/opensl/opensllayer.h index 71a3579cca4728608af58fdf4913d4e3a19ca245..5bd1ce2384efdef7e91481470715b68fe9e588cb 100644 --- a/src/media/audio/opensl/opensllayer.h +++ b/src/media/audio/opensl/opensllayer.h @@ -93,13 +93,12 @@ public: void initAudioEngine(); void shutdownAudioEngine(); - void initAudioCapture(); void startAudioCapture(); void stopAudioCapture(); - virtual int getAudioDeviceIndex(const std::string&, AudioDeviceType) const { return 0; } + virtual int getAudioDeviceIndex(const std::string&, AudioDeviceType) const override { return 0; } - virtual std::string getAudioDeviceName(int, AudioDeviceType) const { return ""; } + virtual std::string getAudioDeviceName(int, AudioDeviceType) const override { return ""; } void engineServicePlay(); void engineServiceRing(); @@ -111,21 +110,21 @@ private: * @return int The index of the card used for capture * 0 for the first available card on the system, 1 ... */ - virtual int getIndexCapture() const { return 0; } + virtual int getIndexCapture() const override { return 0; } /** * Get the index of the audio card for playback * @return int The index of the card used for playback * 0 for the first available card on the system, 1 ... */ - virtual int getIndexPlayback() const { return 0; } + virtual int getIndexPlayback() const override { return 0; } /** * Get the index of the audio card for ringtone (could be differnet from playback) * @return int The index of the card used for ringtone * 0 for the first available card on the system, 1 ... */ - virtual int getIndexRingtone() const { return 0; } + virtual int getIndexRingtone() const override { return 0; } uint32_t dbgEngineGetBufCount(); @@ -133,7 +132,7 @@ private: NON_COPYABLE(OpenSLLayer); - virtual void updatePreference(AudioPreference& pref, int index, AudioDeviceType type); + virtual void updatePreference(AudioPreference& pref, int index, AudioDeviceType type) override; std::mutex recMtx {}; std::condition_variable recCv {};