audio: moved output to its own high priority thread

macOS audio playback failed due to audio being processed in the main thread.
Now all audio playback has been placed into its own high priority thread to
always keep up better with the operating system's demands.
This commit is contained in:
itsmattkc
2020-04-21 19:31:29 +10:00
parent 05eae1e28a
commit 412a6dfdc2
6 changed files with 133 additions and 166 deletions
+32 -31
View File
@@ -38,30 +38,23 @@ AudioOutputManager::AudioOutputManager(QObject *parent) :
AudioOutputManager::~AudioOutputManager()
{
delete output_;
}
bool AudioOutputManager::OutputIsSet()
{
return output_;
Close();
}
void AudioOutputManager::Push(const QByteArray& samples)
{
// If no output device, nothing to be done
if (!output_) {
return;
}
// This function is not queued and is intended to be called from the caller's thread
QMutexLocker lock(&push_sample_lock_);
// Replace sample buffer with this one
pushed_samples_ = samples;
pushed_sample_index_ = 0;
push_samples_ = samples;
push_sample_index_ = 0;
// If we had another device connected, disconnect it now
ResetToPushMode();
QMetaObject::invokeMethod(this, "ResetToPushMode", Qt::QueuedConnection);
// Start pushing samples to the output
PushMoreSamples();
QMetaObject::invokeMethod(this, "PushMoreSamples", Qt::QueuedConnection);
}
void AudioOutputManager::ResetToPushMode()
@@ -77,11 +70,25 @@ void AudioOutputManager::ResetToPushMode()
}
}
void AudioOutputManager::SetParameters(const AudioRenderingParams &params)
void AudioOutputManager::SetParameters(AudioRenderingParams params)
{
device_proxy_.SetParameters(params);
}
void AudioOutputManager::Close()
{
if (output_) {
output_->stop();
if (device_proxy_.isOpen()) {
device_proxy_.close();
}
delete output_;
output_ = nullptr;
}
}
void AudioOutputManager::PullFromDevice(const QString &filename, qint64 offset, int playback_speed)
{
if (!output_) {
@@ -91,7 +98,7 @@ void AudioOutputManager::PullFromDevice(const QString &filename, qint64 offset,
// Stop any current output and disable push mode
output_->stop();
push_device_ = nullptr;
pushed_samples_.clear();
push_samples_.clear();
// Pull from the device
device_proxy_.SetDevice(filename, offset, playback_speed);
@@ -101,38 +108,32 @@ void AudioOutputManager::PullFromDevice(const QString &filename, qint64 offset,
void AudioOutputManager::PushMoreSamples()
{
QMutexLocker lock(&push_sample_lock_);
// Check if we're currently in push mode and if we have samples to push
if (!push_device_ || pushed_samples_.isEmpty()) {
if (!push_device_ || push_samples_.isEmpty()) {
return;
}
const char* read_ptr = pushed_samples_.constData() + pushed_sample_index_;
const char* read_ptr = push_samples_.constData() + push_sample_index_;
// Push the bytes we have to the audio output
qint64 write_count = push_device_->write(read_ptr,
pushed_samples_.size() - pushed_sample_index_);
push_samples_.size() - push_sample_index_);
// Increment sample buffer index (faster than shift the bytes up)
pushed_sample_index_ += static_cast<int>(write_count);
push_sample_index_ += static_cast<int>(write_count);
// If we've pushed all samples, we can clear this array
if (pushed_sample_index_ == pushed_samples_.size()) {
pushed_samples_.clear();
if (push_sample_index_ == push_samples_.size()) {
push_samples_.clear();
}
}
void AudioOutputManager::SetOutputDevice(QAudioDeviceInfo info, QAudioFormat format)
{
// Whatever the output is doing right now, stop it
if (output_) {
output_->stop();
if (device_proxy_.isOpen()) {
device_proxy_.close();
}
delete output_;
}
Close();
// Create a new output device and start it in push mode
output_ = new QAudioOutput(info, format, this);