Skip to content

Commit

Permalink
Enable playback only when pcm data are available
Browse files Browse the repository at this point in the history
  • Loading branch information
f1xpl committed Mar 25, 2018
1 parent 488d70c commit 18fa86b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/f1x/openauto/autoapp/Projection/RtAudioOutput.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class RtAudioOutput: public IAudioOutput
SequentialBuffer audioBuffer_;
std::unique_ptr<RtAudio> dac_;
std::mutex mutex_;
bool playbackRequested_;
};

}
Expand Down
17 changes: 12 additions & 5 deletions src/autoapp/Projection/RtAudioOutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ RtAudioOutput::RtAudioOutput(uint32_t channelCount, uint32_t sampleSize, uint32_
: channelCount_(channelCount)
, sampleSize_(sampleSize)
, sampleRate_(sampleRate)
, playbackRequested_(false)
{
std::vector<RtAudio::Api> apis;
RtAudio::getCompiledApi(apis);
Expand All @@ -54,7 +55,7 @@ bool RtAudioOutput::open()
RtAudio::StreamOptions streamOptions;
streamOptions.numberOfBuffers = 1;
streamOptions.flags = RTAUDIO_MINIMIZE_LATENCY | RTAUDIO_SCHEDULE_REALTIME;
uint32_t bufferFrames = 64;
uint32_t bufferFrames = 256;
dac_->openStream(&parameters, nullptr, RTAUDIO_SINT16, sampleRate_, &bufferFrames, &RtAudioOutput::audioBufferReadHandler, static_cast<void*>(this), &streamOptions);
return audioBuffer_.open(QIODevice::ReadWrite);
}
Expand All @@ -74,13 +75,10 @@ bool RtAudioOutput::open()
void RtAudioOutput::write(const aasdk::common::DataConstBuffer& buffer)
{
audioBuffer_.write(reinterpret_cast<const char*>(buffer.cdata), buffer.size);
}

void RtAudioOutput::start()
{
std::lock_guard<decltype(mutex_)> lock(mutex_);

if(dac_->isStreamOpen() && !dac_->isStreamRunning())
if(playbackRequested_ && dac_->isStreamOpen() && !dac_->isStreamRunning())
{
try
{
Expand All @@ -91,6 +89,15 @@ void RtAudioOutput::start()
OPENAUTO_LOG(error) << "[RtAudioOutput] Failed to start audio output, what: " << e.what();
}
}

playbackRequested_ = false;
}

void RtAudioOutput::start()
{
std::lock_guard<decltype(mutex_)> lock(mutex_);

playbackRequested_ = true;
}

void RtAudioOutput::stop()
Expand Down

0 comments on commit 18fa86b

Please sign in to comment.