Commit 1a4b4d03 authored by Adrien Béraud's avatar Adrien Béraud Committed by gerrit2

audiolayer: properly wait for initialisation

Issue: #80355
Change-Id: I79b5a51549c30f0af14889d4e1d79ff32685de5c
parent 0a6522dd
......@@ -1505,9 +1505,6 @@ Manager::playDtmf(char code)
std::lock_guard<std::mutex> lock(audioLayerMutex_);
// numbers of int = length in milliseconds / 1000 (number of seconds)
// = number of seconds * SAMPLING_RATE by SECONDS
// fast return, no sound, so no dtmf
if (not audiodriver_ or not dtmfKey_) {
RING_DBG("No audio layer...");
......@@ -1515,6 +1512,10 @@ Manager::playDtmf(char code)
}
audiodriver_->startStream();
if (not audiodriver_->waitForStart(std::chrono::seconds(1))) {
RING_ERR("Failed to start audio layer...");
return;
}
// number of data sampling in one pulselen depends on samplerate
// size (n sampling) = time_ms * sampling/s
......@@ -1533,12 +1534,6 @@ Manager::playDtmf(char code)
// so size * 1 channel (mono) * sizeof (bytes for the data)
// audiolayer->flushUrgent();
// FIXME: do real synchronization
int tries = 10;
while (not audiodriver_->isStarted() and tries--) {
RING_WARN("Audio layer not ready yet");
usleep(10000);
}
audiodriver_->putUrgent(dtmfBuf_);
}
......
......@@ -42,6 +42,8 @@
#include <sys/time.h>
#include <mutex>
#include <vector>
#include <atomic>
#include <condition_variable>
/**
* @file audiolayer.h
......@@ -110,6 +112,13 @@ class AudioLayer {
return isStarted_;
}
template< class Rep, class Period >
bool waitForStart(const std::chrono::duration<Rep, Period>& rel_time) const {
std::unique_lock<std::mutex> lk(mutex_);
startedCv_.wait_for(lk, rel_time, [&]{return isStarted_.load();});
return isStarted_;
}
/**
* Send a chunk of data to the hardware buffer to start the playback
* Copy data in the urgent buffer.
......@@ -236,7 +245,8 @@ class AudioLayer {
/**
* Whether or not the audio layer stream is started
*/
bool isStarted_;
std::atomic_bool isStarted_;
mutable std::condition_variable startedCv_;
/**
* Sample Rate Ring should send sound data to the sound card
......@@ -256,7 +266,7 @@ class AudioLayer {
/**
* Lock for the entire audio layer
*/
std::mutex mutex_;
mutable std::mutex mutex_;
/**
* Remove audio offset that can be introduced by certain cheap audio device
......
......@@ -132,6 +132,7 @@ OpenSLLayer::startStream()
startAudioPlayback();
startAudioCapture();
isStarted_ = true;
startedCv_.notify_all();
});
launcher.detach();
}
......
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