Skip to content
Snippets Groups Projects
Commit a3378736 authored by Andreas Traczyk's avatar Andreas Traczyk Committed by Adrien Béraud
Browse files

audioinput: use a mutex to protect against async resource changes

Replaces an atomic flag with a mutex preventing async calls to
switchInput from just failing or possibly causing a crash if a
resource already in progress.

jami-client-qt#522

Change-Id: I5f726306a2d54b1dcaa396a463d221a6efca13d8
parent b10101d8
No related branches found
No related tags found
No related merge requests found
......@@ -71,13 +71,6 @@ AudioInput::~AudioInput()
void
AudioInput::process()
{
if (switchPending_.exchange(false)) {
if (devOpts_.input.empty())
JAMI_DBG() << "Switching to default audio input";
else
JAMI_DBG() << "Switching audio input to '" << devOpts_.input << "'";
}
readFromDevice();
}
......@@ -110,14 +103,16 @@ AudioInput::setSeekTime(int64_t time)
void
AudioInput::readFromDevice()
{
{
std::lock_guard<std::mutex> lk(resourceMutex_);
if (decodingFile_)
while (fileBuf_->isEmpty())
readFromFile();
if (playingFile_) {
readFromQueue();
return;
}
}
// Note: read for device is called in an audio thread and we don't
// want to have a loop which takes 100% of the CPU.
......@@ -266,10 +261,7 @@ std::shared_future<DeviceParams>
AudioInput::switchInput(const std::string& resource)
{
// Always switch inputs, even if it's the same resource, so audio will be in sync with video
if (switchPending_) {
JAMI_ERR() << "Audio switch already requested";
return {};
}
std::unique_lock<std::mutex> lk(resourceMutex_);
JAMI_DBG() << "Switching audio source to match '" << resource << "'";
......@@ -315,9 +307,9 @@ AudioInput::switchInput(const std::string& resource)
foundDevOpts(devOpts_);
}
switchPending_ = true;
futureDevOpts_ = foundDevOpts_.get_future().share();
wakeUp_ = std::chrono::high_resolution_clock::now() + MS_PER_PACKET;
lk.unlock();
loop_.start();
if (onSuccessfulSetup_)
onSuccessfulSetup_(MEDIA_AUDIO, 0);
......
......@@ -94,7 +94,7 @@ private:
std::shared_ptr<RingBuffer> fileBuf_;
std::string currentResource_;
std::atomic_bool switchPending_ {false};
std::mutex resourceMutex_ {};
DeviceParams devOpts_;
std::promise<DeviceParams> foundDevOpts_;
std::shared_future<DeviceParams> futureDevOpts_;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment