Optimization for AudioMixer

Remove resource map and use std::shared_ptr<void>
This commit is contained in:
Attila Uygun 2023-07-10 20:32:56 +02:00
parent 6bad0ac9c0
commit e509588f61
5 changed files with 95 additions and 129 deletions

View File

@ -41,8 +41,6 @@ void ThreadPool::Shutdown() {
} }
void ThreadPool::PostTask(Location from, Closure task, bool front) { void ThreadPool::PostTask(Location from, Closure task, bool front) {
DCHECK((!threads_.empty()));
task_runner_.PostTask(from, std::move(task), front); task_runner_.PostTask(from, std::move(task), front);
semaphore_.release(); semaphore_.release();
} }
@ -51,8 +49,6 @@ void ThreadPool::PostTaskAndReply(Location from,
Closure task, Closure task,
Closure reply, Closure reply,
bool front) { bool front) {
DCHECK((!threads_.empty()));
task_runner_.PostTaskAndReply(from, std::move(task), std::move(reply), front); task_runner_.PostTaskAndReply(from, std::move(task), std::move(reply), front);
semaphore_.release(); semaphore_.release();
} }

View File

@ -17,6 +17,10 @@ using namespace base;
namespace eng { namespace eng {
AudioMixer::Resource::~Resource() {
DLOG(1) << "Destroying audio resource: " << uintptr_t(this);
}
AudioMixer::AudioMixer() AudioMixer::AudioMixer()
: main_thread_task_runner_(TaskRunner::GetThreadLocalTaskRunner()), : main_thread_task_runner_(TaskRunner::GetThreadLocalTaskRunner()),
#if defined(__ANDROID__) #if defined(__ANDROID__)
@ -28,131 +32,102 @@ AudioMixer::AudioMixer()
CHECK(res) << "Failed to initialize audio sink."; CHECK(res) << "Failed to initialize audio sink.";
} }
AudioMixer::~AudioMixer() = default; AudioMixer::~AudioMixer() {
audio_sink_.reset();
uint64_t AudioMixer::CreateResource() {
uint64_t resource_id = ++last_resource_id_;
resources_[resource_id] = std::make_shared<Resource>();
return resource_id;
} }
void AudioMixer::DestroyResource(uint64_t resource_id) { std::shared_ptr<void> AudioMixer::CreateResource() {
auto it = resources_.find(resource_id); auto resource = std::make_shared<Resource>();
if (it == resources_.end()) DLOG(1) << "Audio resource created: " << uintptr_t(resource.get());
return; return resource;
if (it->second->active) {
it->second->restart_cb = nullptr;
it->second->flags.fetch_or(kStopped, std::memory_order_relaxed);
}
resources_.erase(it);
} }
void AudioMixer::Play(uint64_t resource_id, void AudioMixer::Play(std::shared_ptr<void> resource,
std::shared_ptr<AudioBus> audio_bus, std::shared_ptr<AudioBus> audio_bus,
float amplitude, float amplitude,
bool reset_pos) { bool reset_pos) {
if (!audio_enabled_) if (!audio_enabled_)
return; return;
auto it = resources_.find(resource_id); auto resource_ptr = std::static_pointer_cast<Resource>(resource);
if (it == resources_.end())
return;
if (it->second->active) { if (resource_ptr->active) {
if (reset_pos) if (reset_pos)
it->second->flags.fetch_or(kStopped, std::memory_order_relaxed); resource_ptr->flags.fetch_or(kStopped, std::memory_order_relaxed);
if (it->second->flags.load(std::memory_order_relaxed) & kStopped) if (resource_ptr->flags.load(std::memory_order_relaxed) & kStopped)
it->second->restart_cb = [&, resource_id, audio_bus, amplitude, resource_ptr->restart_cb = [&, resource, audio_bus, amplitude,
reset_pos]() -> void { reset_pos]() -> void {
Play(resource_id, audio_bus, amplitude, reset_pos); Play(resource, audio_bus, amplitude, reset_pos);
}; };
return; return;
} }
if (reset_pos) { if (reset_pos) {
it->second->src_index = 0; resource_ptr->src_index = 0;
it->second->accumulator = 0; resource_ptr->accumulator = 0;
audio_bus->ResetStream(); audio_bus->ResetStream();
} else if (it->second->src_index >= audio_bus->samples_per_channel()) { } else if (resource_ptr->src_index >= audio_bus->samples_per_channel()) {
return; return;
} }
it->second->active = true; resource_ptr->active = true;
it->second->flags.fetch_and(~kStopped, std::memory_order_relaxed); resource_ptr->flags.fetch_and(~kStopped, std::memory_order_relaxed);
it->second->audio_bus = audio_bus; resource_ptr->audio_bus = audio_bus;
if (amplitude >= 0) if (amplitude >= 0)
it->second->amplitude = amplitude; resource_ptr->amplitude = amplitude;
std::lock_guard<std::mutex> scoped_lock(lock_); std::lock_guard<std::mutex> scoped_lock(lock_);
play_list_[0].push_back(it->second); play_list_[0].push_back(resource_ptr);
} }
void AudioMixer::Stop(uint64_t resource_id) { void AudioMixer::Stop(std::shared_ptr<void> resource) {
auto it = resources_.find(resource_id); auto resource_ptr = std::static_pointer_cast<Resource>(resource);
if (it == resources_.end()) if (resource_ptr->active) {
return; resource_ptr->restart_cb = nullptr;
resource_ptr->flags.fetch_or(kStopped, std::memory_order_relaxed);
if (it->second->active) {
it->second->restart_cb = nullptr;
it->second->flags.fetch_or(kStopped, std::memory_order_relaxed);
} }
} }
void AudioMixer::SetLoop(uint64_t resource_id, bool loop) { void AudioMixer::SetLoop(std::shared_ptr<void> resource, bool loop) {
auto it = resources_.find(resource_id); auto resource_ptr = std::static_pointer_cast<Resource>(resource);
if (it == resources_.end())
return;
if (loop) if (loop)
it->second->flags.fetch_or(kLoop, std::memory_order_relaxed); resource_ptr->flags.fetch_or(kLoop, std::memory_order_relaxed);
else else
it->second->flags.fetch_and(~kLoop, std::memory_order_relaxed); resource_ptr->flags.fetch_and(~kLoop, std::memory_order_relaxed);
} }
void AudioMixer::SetSimulateStereo(uint64_t resource_id, bool simulate) { void AudioMixer::SetSimulateStereo(std::shared_ptr<void> resource,
auto it = resources_.find(resource_id); bool simulate) {
if (it == resources_.end()) auto resource_ptr = std::static_pointer_cast<Resource>(resource);
return;
if (simulate) if (simulate)
it->second->flags.fetch_or(kSimulateStereo, std::memory_order_relaxed); resource_ptr->flags.fetch_or(kSimulateStereo, std::memory_order_relaxed);
else else
it->second->flags.fetch_and(~kSimulateStereo, std::memory_order_relaxed); resource_ptr->flags.fetch_and(~kSimulateStereo, std::memory_order_relaxed);
} }
void AudioMixer::SetResampleStep(uint64_t resource_id, size_t step) { void AudioMixer::SetResampleStep(std::shared_ptr<void> resource, size_t step) {
auto it = resources_.find(resource_id); auto resource_ptr = std::static_pointer_cast<Resource>(resource);
if (it == resources_.end()) resource_ptr->step.store(step + 100, std::memory_order_relaxed);
return;
it->second->step.store(step + 100, std::memory_order_relaxed);
} }
void AudioMixer::SetMaxAmplitude(uint64_t resource_id, float max_amplitude) { void AudioMixer::SetMaxAmplitude(std::shared_ptr<void> resource,
auto it = resources_.find(resource_id); float max_amplitude) {
if (it == resources_.end()) auto resource_ptr = std::static_pointer_cast<Resource>(resource);
return; resource_ptr->max_amplitude.store(max_amplitude, std::memory_order_relaxed);
it->second->max_amplitude.store(max_amplitude, std::memory_order_relaxed);
} }
void AudioMixer::SetAmplitudeInc(uint64_t resource_id, float amplitude_inc) { void AudioMixer::SetAmplitudeInc(std::shared_ptr<void> resource,
auto it = resources_.find(resource_id); float amplitude_inc) {
if (it == resources_.end()) auto resource_ptr = std::static_pointer_cast<Resource>(resource);
return; resource_ptr->amplitude_inc.store(amplitude_inc, std::memory_order_relaxed);
it->second->amplitude_inc.store(amplitude_inc, std::memory_order_relaxed);
} }
void AudioMixer::SetEndCallback(uint64_t resource_id, base::Closure cb) { void AudioMixer::SetEndCallback(std::shared_ptr<void> resource,
auto it = resources_.find(resource_id); base::Closure cb) {
if (it == resources_.end()) auto resource_ptr = std::static_pointer_cast<Resource>(resource);
return; resource_ptr->end_cb = std::move(cb);
it->second->end_cb = std::move(cb);
} }
void AudioMixer::Suspend() { void AudioMixer::Suspend() {
@ -177,8 +152,8 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
memset(output_buffer, 0, sizeof(float) * num_frames * kChannelCount); memset(output_buffer, 0, sizeof(float) * num_frames * kChannelCount);
for (auto it = play_list_[1].begin(); it != play_list_[1].end();) { for (auto it = play_list_[1].begin(); it != play_list_[1].end();) {
auto audio_bus = it->get()->audio_bus.get(); auto audio_bus = (*it)->audio_bus.get();
unsigned flags = it->get()->flags.load(std::memory_order_relaxed); unsigned flags = (*it)->flags.load(std::memory_order_relaxed);
bool marked_for_removal = false; bool marked_for_removal = false;
if (flags & kStopped) { if (flags & kStopped) {
@ -190,14 +165,14 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
src[1] = src[0]; // mono. src[1] = src[0]; // mono.
size_t num_samples = audio_bus->samples_per_channel(); size_t num_samples = audio_bus->samples_per_channel();
size_t src_index = it->get()->src_index; size_t src_index = (*it)->src_index;
size_t step = it->get()->step.load(std::memory_order_relaxed); size_t step = (*it)->step.load(std::memory_order_relaxed);
size_t accumulator = it->get()->accumulator; size_t accumulator = (*it)->accumulator;
float amplitude = it->get()->amplitude; float amplitude = (*it)->amplitude;
float amplitude_inc = float amplitude_inc =
it->get()->amplitude_inc.load(std::memory_order_relaxed); (*it)->amplitude_inc.load(std::memory_order_relaxed);
float max_amplitude = float max_amplitude =
it->get()->max_amplitude.load(std::memory_order_relaxed); (*it)->max_amplitude.load(std::memory_order_relaxed);
size_t channel_offset = size_t channel_offset =
(flags & kSimulateStereo) ? audio_bus->sample_rate() / 10 : 0; (flags & kSimulateStereo) ? audio_bus->sample_rate() / 10 : 0;
@ -237,11 +212,9 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
break; break;
} }
if (!it->get()->streaming_in_progress.load( if (!(*it)->streaming_in_progress.load(std::memory_order_acquire)) {
std::memory_order_acquire)) {
src_index %= num_samples; src_index %= num_samples;
it->get()->streaming_in_progress.store(true, (*it)->streaming_in_progress.store(true, std::memory_order_relaxed);
std::memory_order_relaxed);
// Swap buffers and start streaming in background. // Swap buffers and start streaming in background.
audio_bus->SwapBuffers(); audio_bus->SwapBuffers();
@ -261,9 +234,9 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
} }
} }
it->get()->src_index = src_index; (*it)->src_index = src_index;
it->get()->accumulator = accumulator; (*it)->accumulator = accumulator;
it->get()->amplitude = amplitude; (*it)->amplitude = amplitude;
} }
if (marked_for_removal) { if (marked_for_removal) {
@ -275,7 +248,7 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
} }
for (auto it = end_list_.begin(); it != end_list_.end();) { for (auto it = end_list_.begin(); it != end_list_.end();) {
if (!it->get()->streaming_in_progress.load(std::memory_order_relaxed)) { if (!(*it)->streaming_in_progress.load(std::memory_order_relaxed)) {
main_thread_task_runner_->PostTask( main_thread_task_runner_->PostTask(
HERE, std::bind(&AudioMixer::EndCallback, this, *it)); HERE, std::bind(&AudioMixer::EndCallback, this, *it));
it = end_list_.erase(it); it = end_list_.erase(it);

View File

@ -5,7 +5,6 @@
#include <list> #include <list>
#include <memory> #include <memory>
#include <mutex> #include <mutex>
#include <unordered_map>
#include "base/closure.h" #include "base/closure.h"
#include "engine/audio/audio_sink.h" #include "engine/audio/audio_sink.h"
@ -26,21 +25,20 @@ class AudioMixer : public AudioSink::Delegate {
AudioMixer(); AudioMixer();
~AudioMixer(); ~AudioMixer();
uint64_t CreateResource(); std::shared_ptr<void> CreateResource();
void DestroyResource(uint64_t resource_id);
void Play(uint64_t resource_id, void Play(std::shared_ptr<void> resource,
std::shared_ptr<AudioBus> audio_bus, std::shared_ptr<AudioBus> audio_bus,
float amplitude, float amplitude,
bool reset_pos); bool reset_pos);
void Stop(uint64_t resource_id); void Stop(std::shared_ptr<void> resource);
void SetLoop(uint64_t resource_id, bool loop); void SetLoop(std::shared_ptr<void> resource, bool loop);
void SetSimulateStereo(uint64_t resource_id, bool simulate); void SetSimulateStereo(std::shared_ptr<void> resource, bool simulate);
void SetResampleStep(uint64_t resource_id, size_t step); void SetResampleStep(std::shared_ptr<void> resource, size_t step);
void SetMaxAmplitude(uint64_t resource_id, float max_amplitude); void SetMaxAmplitude(std::shared_ptr<void> resource, float max_amplitude);
void SetAmplitudeInc(uint64_t resource_id, float amplitude_inc); void SetAmplitudeInc(std::shared_ptr<void> resource, float amplitude_inc);
void SetEndCallback(uint64_t resource_id, base::Closure cb); void SetEndCallback(std::shared_ptr<void> resource, base::Closure cb);
void SetEnableAudio(bool enable) { audio_enabled_ = enable; } void SetEnableAudio(bool enable) { audio_enabled_ = enable; }
@ -74,10 +72,9 @@ class AudioMixer : public AudioSink::Delegate {
// Accessed by audio thread and decoder thread. // Accessed by audio thread and decoder thread.
std::atomic<bool> streaming_in_progress{false}; std::atomic<bool> streaming_in_progress{false};
};
std::unordered_map<uint64_t, std::shared_ptr<Resource>> resources_; ~Resource();
uint64_t last_resource_id_ = 0; };
std::list<std::shared_ptr<Resource>> play_list_[2]; std::list<std::shared_ptr<Resource>> play_list_[2];
std::mutex lock_; std::mutex lock_;

View File

@ -11,10 +11,10 @@ using namespace base;
namespace eng { namespace eng {
SoundPlayer::SoundPlayer() SoundPlayer::SoundPlayer()
: resource_id_(Engine::Get().GetAudioMixer()->CreateResource()) {} : resource_(Engine::Get().GetAudioMixer()->CreateResource()) {}
SoundPlayer::~SoundPlayer() { SoundPlayer::~SoundPlayer() {
Engine::Get().GetAudioMixer()->DestroyResource(resource_id_); Engine::Get().GetAudioMixer()->Stop(resource_);
} }
void SoundPlayer::SetSound(const std::string& asset_name) { void SoundPlayer::SetSound(const std::string& asset_name) {
@ -30,15 +30,15 @@ void SoundPlayer::Play(bool loop, float fade_in_duration) {
return; return;
int step = variate_ ? Engine::Get().GetRandomGenerator().Roll(3) - 2 : 0; int step = variate_ ? Engine::Get().GetRandomGenerator().Roll(3) - 2 : 0;
Engine::Get().GetAudioMixer()->SetResampleStep(resource_id_, step * 12); Engine::Get().GetAudioMixer()->SetResampleStep(resource_, step * 12);
Engine::Get().GetAudioMixer()->SetLoop(resource_id_, loop); Engine::Get().GetAudioMixer()->SetLoop(resource_, loop);
if (fade_in_duration > 0) if (fade_in_duration > 0)
Engine::Get().GetAudioMixer()->SetAmplitudeInc( Engine::Get().GetAudioMixer()->SetAmplitudeInc(
resource_id_, 1.0f / (sound_->sample_rate() * fade_in_duration)); resource_, 1.0f / (sound_->sample_rate() * fade_in_duration));
else else
Engine::Get().GetAudioMixer()->SetAmplitudeInc(resource_id_, 0); Engine::Get().GetAudioMixer()->SetAmplitudeInc(resource_, 0);
Engine::Get().GetAudioMixer()->Play( Engine::Get().GetAudioMixer()->Play(
resource_id_, sound_, fade_in_duration > 0 ? 0 : max_amplitude_, true); resource_, sound_, fade_in_duration > 0 ? 0 : max_amplitude_, true);
} }
void SoundPlayer::Resume(float fade_in_duration) { void SoundPlayer::Resume(float fade_in_duration) {
@ -47,8 +47,8 @@ void SoundPlayer::Resume(float fade_in_duration) {
if (fade_in_duration > 0) if (fade_in_duration > 0)
Engine::Get().GetAudioMixer()->SetAmplitudeInc( Engine::Get().GetAudioMixer()->SetAmplitudeInc(
resource_id_, 1.0f / (sound_->sample_rate() * fade_in_duration)); resource_, 1.0f / (sound_->sample_rate() * fade_in_duration));
Engine::Get().GetAudioMixer()->Play(resource_id_, sound_, Engine::Get().GetAudioMixer()->Play(resource_, sound_,
fade_in_duration > 0 ? 0 : -1, false); fade_in_duration > 0 ? 0 : -1, false);
} }
@ -58,9 +58,9 @@ void SoundPlayer::Stop(float fade_out_duration) {
if (fade_out_duration > 0) if (fade_out_duration > 0)
Engine::Get().GetAudioMixer()->SetAmplitudeInc( Engine::Get().GetAudioMixer()->SetAmplitudeInc(
resource_id_, -1.0f / (sound_->sample_rate() * fade_out_duration)); resource_, -1.0f / (sound_->sample_rate() * fade_out_duration));
else else
Engine::Get().GetAudioMixer()->Stop(resource_id_); Engine::Get().GetAudioMixer()->Stop(resource_);
} }
void SoundPlayer::SetVariate(bool variate) { void SoundPlayer::SetVariate(bool variate) {
@ -68,16 +68,16 @@ void SoundPlayer::SetVariate(bool variate) {
} }
void SoundPlayer::SetSimulateStereo(bool simulate) { void SoundPlayer::SetSimulateStereo(bool simulate) {
Engine::Get().GetAudioMixer()->SetSimulateStereo(resource_id_, simulate); Engine::Get().GetAudioMixer()->SetSimulateStereo(resource_, simulate);
} }
void SoundPlayer::SetMaxAplitude(float max_amplitude) { void SoundPlayer::SetMaxAplitude(float max_amplitude) {
max_amplitude_ = max_amplitude; max_amplitude_ = max_amplitude;
Engine::Get().GetAudioMixer()->SetMaxAmplitude(resource_id_, max_amplitude); Engine::Get().GetAudioMixer()->SetMaxAmplitude(resource_, max_amplitude);
} }
void SoundPlayer::SetEndCallback(base::Closure cb) { void SoundPlayer::SetEndCallback(base::Closure cb) {
Engine::Get().GetAudioMixer()->SetEndCallback(resource_id_, cb); Engine::Get().GetAudioMixer()->SetEndCallback(resource_, cb);
} }
} // namespace eng } // namespace eng

View File

@ -37,7 +37,7 @@ class SoundPlayer {
void SetEndCallback(base::Closure cb); void SetEndCallback(base::Closure cb);
private: private:
uint64_t resource_id_ = 0; std::shared_ptr<void> resource_;
std::shared_ptr<AudioBus> sound_; std::shared_ptr<AudioBus> sound_;
float max_amplitude_ = 1.0f; float max_amplitude_ = 1.0f;