Commit 01810bbb authored by Adrien Béraud's avatar Adrien Béraud

opensl: fix race condition

Layer could be destroyed while being created

Change-Id: Ifc6ff8039f98eaa05dd027767ad27ae109cb4f17
parent 916a9a42
......@@ -1681,7 +1681,7 @@ Manager::addAudio(Call& call)
return;
}
pimpl_->audiodriver_->flushUrgent();
pimpl_->audiodriver_->flushMain();
getRingBufferPool().flushAllBuffers();
}
startAudioDriverStream();
}
......@@ -1992,7 +1992,7 @@ Manager::peerAnsweredCall(Call& call)
if (pimpl_->audiodriver_) {
std::lock_guard<std::mutex> lock(pimpl_->audioLayerMutex_);
pimpl_->audiodriver_->flushMain();
getRingBufferPool().flushAllBuffers();
pimpl_->audiodriver_->flushUrgent();
}
......@@ -2209,6 +2209,7 @@ Manager::setAudioPlugin(const std::string& audioPlugin)
bool wasStarted = pimpl_->audiodriver_->isStarted();
// Recreate audio driver with new settings
pimpl_->audiodriver_.reset();
pimpl_->audiodriver_.reset(audioPreference.createAudioLayer());
if (pimpl_->audiodriver_ and wasStarted)
......
......@@ -53,7 +53,6 @@ AudioLayer::~AudioLayer()
void AudioLayer::hardwareFormatAvailable(AudioFormat playback)
{
std::lock_guard<std::mutex> lock(mutex_);
JAMI_DBG("Hardware audio format available : %s", playback.toString().c_str());
audioFormat_ = Manager::instance().hardwareAudioFormatChanged(playback);
urgentRingBuffer_.setFormat(audioFormat_);
......@@ -72,7 +71,6 @@ void AudioLayer::devicesChanged()
void AudioLayer::flushMain()
{
std::lock_guard<std::mutex> lock(mutex_);
// should pass call id
Manager::instance().getRingBufferPool().flushAllBuffers();
}
......@@ -82,6 +80,12 @@ void AudioLayer::flushUrgent()
urgentRingBuffer_.flushAll();
}
void AudioLayer::flush()
{
Manager::instance().getRingBufferPool().flushAllBuffers();
urgentRingBuffer_.flushAll();
}
void AudioLayer::putUrgent(AudioBuffer& buffer)
{
std::lock_guard<std::mutex> lock(mutex_);
......
......@@ -239,6 +239,8 @@ class AudioLayer {
return ringBuff ? ringBuff : playBuff;
}
void flush();
/**
* True if capture is not to be used
*/
......
......@@ -64,23 +64,17 @@ OpenSLLayer::init()
initAudioPlayback();
initAudioCapture();
flushMain();
flushUrgent();
flush();
}
void
OpenSLLayer::startStream()
{
dcblocker_.reset();
{
std::lock_guard<std::mutex> lock(mutex_);
if (status_ != Status::Idle)
return;
status_ = Status::Starting;
}
JAMI_DBG("Start OpenSL audio layer");
std::lock_guard<std::mutex> lock(mutex_);
if (status_ != Status::Idle)
return;
status_ = Status::Starting;
JAMI_WARN("Start OpenSL audio layer");
std::vector<int32_t> hw_infos;
hw_infos.reserve(4);
......@@ -89,7 +83,7 @@ OpenSLLayer::startStream()
hardwareBuffSize_ = hw_infos[1];
hardwareFormatAvailable(hardwareFormat_);
std::thread launcher([this](){
startThread_ = std::thread([this](){
init();
startAudioPlayback();
startAudioCapture();
......@@ -100,26 +94,23 @@ OpenSLLayer::startStream()
}
startedCv_.notify_all();
});
launcher.detach();
}
void
OpenSLLayer::stopStream()
{
{
std::lock_guard<std::mutex> lock(mutex_);
if (status_ != Status::Started)
return;
status_ = Status::Idle;
std::lock_guard<std::mutex> lock(mutex_);
if (startThread_.joinable()) {
startThread_.join();
}
JAMI_WARN("Stop OpenSL audio layer");
if (status_ != Status::Started)
return;
status_ = Status::Idle;
JAMI_WARN("Stopping OpenSL audio layer");
stopAudioPlayback();
stopAudioCapture();
flushMain();
flushUrgent();
flush();
if (engineObject_ != nullptr) {
(*engineObject_)->Destroy(engineObject_);
......@@ -134,6 +125,7 @@ OpenSLLayer::stopStream()
freeRecBufQueue_.clear();
recBufQueue_.clear();
bufs_.clear();
dcblocker_.reset();
}
std::vector<sample_buf>
......
......@@ -190,6 +190,7 @@ class OpenSLLayer : public AudioLayer {
size_t hardwareBuffSize_ {BUFFER_SIZE};
std::shared_ptr<RingBuffer> mainRingBuffer_;
std::thread startThread_;
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment