Skip to content
Snippets Groups Projects
Commit 26f46726 authored by Adrien Béraud's avatar Adrien Béraud
Browse files

opensl: fix crashes

Change-Id: I790fd96b17309d2203d5ba18b675d998124b758a
parent a66b10c3
No related branches found
No related tags found
No related merge requests found
...@@ -197,6 +197,7 @@ bool ...@@ -197,6 +197,7 @@ bool
AudioPlayer::start() AudioPlayer::start()
{ {
JAMI_DBG("OpenSL playback start"); JAMI_DBG("OpenSL playback start");
std::unique_lock<std::mutex> lk(m_);
SLuint32 state; SLuint32 state;
SLresult result = (*playItf_)->GetPlayState(playItf_, &state); SLresult result = (*playItf_)->GetPlayState(playItf_, &state);
if (result != SL_RESULT_SUCCESS) if (result != SL_RESULT_SUCCESS)
...@@ -211,6 +212,7 @@ AudioPlayer::start() ...@@ -211,6 +212,7 @@ AudioPlayer::start()
(*playBufferQueueItf_)->Enqueue(playBufferQueueItf_, silentBuf_.buf_, silentBuf_.size_)); (*playBufferQueueItf_)->Enqueue(playBufferQueueItf_, silentBuf_.buf_, silentBuf_.size_));
devShadowQueue_.push(&silentBuf_); devShadowQueue_.push(&silentBuf_);
lk.unlock();
result = (*playItf_)->SetPlayState(playItf_, SL_PLAYSTATE_PLAYING); result = (*playItf_)->SetPlayState(playItf_, SL_PLAYSTATE_PLAYING);
SLASSERT(result); SLASSERT(result);
...@@ -233,13 +235,13 @@ AudioPlayer::stop() ...@@ -233,13 +235,13 @@ AudioPlayer::stop()
JAMI_DBG("OpenSL playback stop"); JAMI_DBG("OpenSL playback stop");
SLuint32 state; SLuint32 state;
std::lock_guard<std::mutex> lk(m_);
SLresult result = (*playItf_)->GetPlayState(playItf_, &state); SLresult result = (*playItf_)->GetPlayState(playItf_, &state);
SLASSERT(result); SLASSERT(result);
if (state == SL_PLAYSTATE_STOPPED) if (state == SL_PLAYSTATE_STOPPED)
return; return;
std::lock_guard<std::mutex> lk(m_);
callback_ = {}; callback_ = {};
result = (*playItf_)->SetPlayState(playItf_, SL_PLAYSTATE_STOPPED); result = (*playItf_)->SetPlayState(playItf_, SL_PLAYSTATE_STOPPED);
SLASSERT(result); SLASSERT(result);
......
...@@ -112,7 +112,7 @@ OpenSLLayer::startStream(AudioDeviceType stream) ...@@ -112,7 +112,7 @@ OpenSLLayer::startStream(AudioDeviceType stream)
void void
OpenSLLayer::stopStream(AudioDeviceType stream) OpenSLLayer::stopStream(AudioDeviceType stream)
{ {
std::unique_lock<std::mutex> lock(mutex_); std::lock_guard<std::mutex> lock(mutex_);
JAMI_WARN("Stopping OpenSL audio layer for type %u", (unsigned) stream); JAMI_WARN("Stopping OpenSL audio layer for type %u", (unsigned) stream);
if (stream == AudioDeviceType::PLAYBACK) { if (stream == AudioDeviceType::PLAYBACK) {
...@@ -152,6 +152,7 @@ allocateSampleBufs(unsigned count, size_t sizeInByte) ...@@ -152,6 +152,7 @@ allocateSampleBufs(unsigned count, size_t sizeInByte)
void void
OpenSLLayer::initAudioEngine() OpenSLLayer::initAudioEngine()
{ {
JAMI_WARN("OpenSL init started");
std::vector<int32_t> hw_infos; std::vector<int32_t> hw_infos;
hw_infos.reserve(4); hw_infos.reserve(4);
emitSignal<DRing::ConfigurationSignal::GetHardwareAudioFormat>(&hw_infos); emitSignal<DRing::ConfigurationSignal::GetHardwareAudioFormat>(&hw_infos);
...@@ -159,6 +160,7 @@ OpenSLLayer::initAudioEngine() ...@@ -159,6 +160,7 @@ OpenSLLayer::initAudioEngine()
hardwareBuffSize_ = hw_infos[1]; hardwareBuffSize_ = hw_infos[1];
hardwareFormatAvailable(hardwareFormat_, hardwareBuffSize_); hardwareFormatAvailable(hardwareFormat_, hardwareBuffSize_);
std::lock_guard<std::mutex> lock(mutex_);
SLASSERT(slCreateEngine(&engineObject_, 0, nullptr, 0, nullptr, nullptr)); SLASSERT(slCreateEngine(&engineObject_, 0, nullptr, 0, nullptr, nullptr));
SLASSERT((*engineObject_)->Realize(engineObject_, SL_BOOLEAN_FALSE)); SLASSERT((*engineObject_)->Realize(engineObject_, SL_BOOLEAN_FALSE));
SLASSERT((*engineObject_)->GetInterface(engineObject_, SL_IID_ENGINE, &engineInterface_)); SLASSERT((*engineObject_)->GetInterface(engineObject_, SL_IID_ENGINE, &engineInterface_));
...@@ -171,11 +173,13 @@ OpenSLLayer::initAudioEngine() ...@@ -171,11 +173,13 @@ OpenSLLayer::initAudioEngine()
freeRingBufQueue_.push(&bufs_[i]); freeRingBufQueue_.push(&bufs_[i]);
for (int i = 2 * BUF_COUNT; i < 3 * BUF_COUNT; i++) for (int i = 2 * BUF_COUNT; i < 3 * BUF_COUNT; i++)
freeRecBufQueue_.push(&bufs_[i]); freeRecBufQueue_.push(&bufs_[i]);
JAMI_WARN("OpenSL init ended");
} }
void void
OpenSLLayer::shutdownAudioEngine() OpenSLLayer::shutdownAudioEngine()
{ {
JAMI_DBG("Stopping OpenSL");
stopAudioCapture(); stopAudioCapture();
freeRecBufQueue_.clear(); freeRecBufQueue_.clear();
recBufQueue_.clear(); recBufQueue_.clear();
...@@ -241,6 +245,15 @@ OpenSLLayer::engineServicePlay() ...@@ -241,6 +245,15 @@ OpenSLLayer::engineServicePlay()
if (auto dat = getToPlay(hardwareFormat_, hardwareBuffSize_)) { if (auto dat = getToPlay(hardwareFormat_, hardwareBuffSize_)) {
buf->size_ = dat->pointer()->nb_samples * dat->pointer()->channels buf->size_ = dat->pointer()->nb_samples * dat->pointer()->channels
* sizeof(AudioSample); * sizeof(AudioSample);
if (buf->size_ > buf->cap_) {
JAMI_ERR("buf->size_(%zu) > buf->cap_(%zu)", buf->size_, buf->cap_);
break;
}
if (not dat->pointer()->data[0] or not buf->buf_) {
JAMI_ERR("null bufer %p -> %p %d", dat->pointer()->data[0], buf->buf_, dat->pointer()->nb_samples);
break;
}
//JAMI_ERR("std::copy_n %p -> %p %zu", dat->pointer()->data[0], buf->buf_, dat->pointer()->nb_samples);
std::copy_n((const AudioSample*) dat->pointer()->data[0], std::copy_n((const AudioSample*) dat->pointer()->data[0],
dat->pointer()->nb_samples, dat->pointer()->nb_samples,
(AudioSample*) buf->buf_); (AudioSample*) buf->buf_);
...@@ -264,6 +277,14 @@ OpenSLLayer::engineServiceRing() ...@@ -264,6 +277,14 @@ OpenSLLayer::engineServiceRing()
if (auto dat = getToRing(hardwareFormat_, hardwareBuffSize_)) { if (auto dat = getToRing(hardwareFormat_, hardwareBuffSize_)) {
buf->size_ = dat->pointer()->nb_samples * dat->pointer()->channels buf->size_ = dat->pointer()->nb_samples * dat->pointer()->channels
* sizeof(AudioSample); * sizeof(AudioSample);
if (buf->size_ > buf->cap_) {
JAMI_ERR("buf->size_(%zu) > buf->cap_(%zu)", buf->size_, buf->cap_);
break;
}
if (not dat->pointer()->data[0] or not buf->buf_) {
JAMI_ERR("null bufer %p -> %p %d", dat->pointer()->data[0], buf->buf_, dat->pointer()->nb_samples);
break;
}
std::copy_n((const AudioSample*) dat->pointer()->data[0], std::copy_n((const AudioSample*) dat->pointer()->data[0],
dat->pointer()->nb_samples, dat->pointer()->nb_samples,
(AudioSample*) buf->buf_); (AudioSample*) buf->buf_);
......
...@@ -150,6 +150,8 @@ private: ...@@ -150,6 +150,8 @@ private:
AudioFormat hardwareFormat_ {AudioFormat::MONO()}; AudioFormat hardwareFormat_ {AudioFormat::MONO()};
size_t hardwareBuffSize_ {BUFFER_SIZE}; size_t hardwareBuffSize_ {BUFFER_SIZE};
std::vector<sample_buf> bufs_ {};
AudioQueue freePlayBufQueue_ {BUF_COUNT}; AudioQueue freePlayBufQueue_ {BUF_COUNT};
AudioQueue playBufQueue_ {BUF_COUNT}; AudioQueue playBufQueue_ {BUF_COUNT};
...@@ -159,8 +161,6 @@ private: ...@@ -159,8 +161,6 @@ private:
AudioQueue freeRecBufQueue_ {BUF_COUNT}; // Owner of the queue AudioQueue freeRecBufQueue_ {BUF_COUNT}; // Owner of the queue
AudioQueue recBufQueue_ {BUF_COUNT}; // Owner of the queue AudioQueue recBufQueue_ {BUF_COUNT}; // Owner of the queue
std::vector<sample_buf> bufs_ {};
std::unique_ptr<opensl::AudioPlayer> player_ {}; std::unique_ptr<opensl::AudioPlayer> player_ {};
std::unique_ptr<opensl::AudioPlayer> ringtone_ {}; std::unique_ptr<opensl::AudioPlayer> ringtone_ {};
std::unique_ptr<opensl::AudioRecorder> recorder_ {}; std::unique_ptr<opensl::AudioRecorder> recorder_ {};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment