mirror of https://github.com/auygun/kaliber.git
Rename AudioDriver to AudioSink
This commit is contained in:
parent
8d28d85a09
commit
d5ee1c7313
|
@ -62,8 +62,8 @@ add_library(kaliber SHARED
|
|||
../../../src/demo/sky_quad.cc
|
||||
../../../src/engine/animatable.cc
|
||||
../../../src/engine/animator.cc
|
||||
../../../src/engine/audio/audio_driver_oboe.cc
|
||||
../../../src/engine/audio/audio_mixer.cc
|
||||
../../../src/engine/audio/audio_sink_oboe.cc
|
||||
../../../src/engine/drawable.cc
|
||||
../../../src/engine/engine.cc
|
||||
../../../src/engine/font.cc
|
||||
|
|
|
@ -95,8 +95,8 @@ $(BASE_LIB): $(BASE_OBJS)
|
|||
ENGINE_SRC := \
|
||||
$(SRC_ROOT)/engine/animatable.cc \
|
||||
$(SRC_ROOT)/engine/animator.cc \
|
||||
$(SRC_ROOT)/engine/audio/audio_driver_alsa.cc \
|
||||
$(SRC_ROOT)/engine/audio/audio_mixer.cc \
|
||||
$(SRC_ROOT)/engine/audio/audio_sink_alsa.cc \
|
||||
$(SRC_ROOT)/engine/drawable.cc \
|
||||
$(SRC_ROOT)/engine/engine.cc \
|
||||
$(SRC_ROOT)/engine/font.cc \
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
#ifndef ENGINE_AUDIO_AUDIO_DRIVER_H
|
||||
#define ENGINE_AUDIO_AUDIO_DRIVER_H
|
||||
|
||||
namespace eng {
|
||||
|
||||
// Models an audio sink sending mixed audio to the audio driver. Audio data from
|
||||
// the mixer source is delivered on a pull model using AudioDriverDelegate.
|
||||
class AudioDriver {
|
||||
public:
|
||||
AudioDriver() = default;
|
||||
virtual ~AudioDriver() = default;
|
||||
|
||||
virtual bool Initialize() = 0;
|
||||
|
||||
virtual void Suspend() = 0;
|
||||
virtual void Resume() = 0;
|
||||
|
||||
virtual int GetHardwareSampleRate() = 0;
|
||||
|
||||
private:
|
||||
AudioDriver(const AudioDriver&) = delete;
|
||||
AudioDriver& operator=(const AudioDriver&) = delete;
|
||||
};
|
||||
|
||||
} // namespace eng
|
||||
|
||||
#endif // ENGINE_AUDIO_AUDIO_DRIVER_H
|
|
@ -1,20 +0,0 @@
|
|||
#ifndef ENGINE_AUDIO_AUDIO_DRIVER_DELEGATE_H
|
||||
#define ENGINE_AUDIO_AUDIO_DRIVER_DELEGATE_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
namespace eng {
|
||||
|
||||
class AudioDriverDelegate {
|
||||
public:
|
||||
AudioDriverDelegate() = default;
|
||||
virtual ~AudioDriverDelegate() = default;
|
||||
|
||||
virtual int GetChannelCount() = 0;
|
||||
|
||||
virtual void RenderAudio(float* output_buffer, size_t num_frames) = 0;
|
||||
};
|
||||
|
||||
} // namespace eng
|
||||
|
||||
#endif // ENGINE_AUDIO_AUDIO_DRIVER_DELEGATE_H
|
|
@ -8,9 +8,9 @@
|
|||
#include "engine/sound.h"
|
||||
|
||||
#if defined(__ANDROID__)
|
||||
#include "engine/audio/audio_driver_oboe.h"
|
||||
#include "engine/audio/audio_sink_oboe.h"
|
||||
#elif defined(__linux__)
|
||||
#include "engine/audio/audio_driver_alsa.h"
|
||||
#include "engine/audio/audio_sink_alsa.h"
|
||||
#endif
|
||||
|
||||
using namespace base;
|
||||
|
@ -20,12 +20,12 @@ namespace eng {
|
|||
AudioMixer::AudioMixer()
|
||||
: main_thread_task_runner_(TaskRunner::GetThreadLocalTaskRunner()),
|
||||
#if defined(__ANDROID__)
|
||||
audio_driver_{std::make_unique<AudioDriverOboe>(this)} {
|
||||
audio_sink_{std::make_unique<AudioSinkOboe>(this)} {
|
||||
#elif defined(__linux__)
|
||||
audio_driver_{std::make_unique<AudioDriverAlsa>(this)} {
|
||||
audio_sink_{std::make_unique<AudioSinkAlsa>(this)} {
|
||||
#endif
|
||||
bool res = audio_driver_->Initialize();
|
||||
CHECK(res) << "Failed to initialize audio driver.";
|
||||
bool res = audio_sink_->Initialize();
|
||||
CHECK(res) << "Failed to initialize audio sink.";
|
||||
}
|
||||
|
||||
AudioMixer::~AudioMixer() = default;
|
||||
|
@ -154,15 +154,15 @@ void AudioMixer::SetEndCallback(uint64_t resource_id, base::Closure cb) {
|
|||
}
|
||||
|
||||
void AudioMixer::Suspend() {
|
||||
audio_driver_->Suspend();
|
||||
audio_sink_->Suspend();
|
||||
}
|
||||
|
||||
void AudioMixer::Resume() {
|
||||
audio_driver_->Resume();
|
||||
audio_sink_->Resume();
|
||||
}
|
||||
|
||||
int AudioMixer::GetHardwareSampleRate() {
|
||||
return audio_driver_->GetHardwareSampleRate();
|
||||
return audio_sink_->GetHardwareSampleRate();
|
||||
}
|
||||
|
||||
void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
|
||||
|
@ -264,7 +264,7 @@ void AudioMixer::RenderAudio(float* output_buffer, size_t num_frames) {
|
|||
ThreadPool::Get().PostTask(
|
||||
HERE,
|
||||
std::bind(&AudioMixer::DoStream, this, *it, flags & kLoop));
|
||||
} else if (num_samples) {
|
||||
} else {
|
||||
DLOG << "Mixer buffer underrun!";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
#include <unordered_map>
|
||||
|
||||
#include "base/closure.h"
|
||||
#include "engine/audio/audio_driver_delegate.h"
|
||||
#include "engine/audio/audio_sink_delegate.h"
|
||||
|
||||
namespace base {
|
||||
class TaskRunner;
|
||||
|
@ -16,13 +16,13 @@ class TaskRunner;
|
|||
|
||||
namespace eng {
|
||||
|
||||
class AudioDriver;
|
||||
class AudioSink;
|
||||
class Sound;
|
||||
|
||||
// Mix and render audio with low overhead. A platform specific AudioDriver
|
||||
// Mix and render audio with low overhead. A platform specific AudioSink
|
||||
// implementation is expected to periodically call RenderAudio() in a background
|
||||
// thread.
|
||||
class AudioMixer : public AudioDriverDelegate {
|
||||
class AudioMixer : public AudioSinkDelegate {
|
||||
public:
|
||||
AudioMixer();
|
||||
~AudioMixer();
|
||||
|
@ -54,6 +54,7 @@ class AudioMixer : public AudioDriverDelegate {
|
|||
enum SampleFlags { kLoop = 1, kStopped = 2, kSimulateStereo = 4 };
|
||||
static constexpr int kChannelCount = 2;
|
||||
|
||||
// An audio resource that gets mixed and rendered to the audio sink.
|
||||
struct Resource {
|
||||
// Accessed by main thread only.
|
||||
bool active = false;
|
||||
|
@ -86,11 +87,11 @@ class AudioMixer : public AudioDriverDelegate {
|
|||
|
||||
base::TaskRunner* main_thread_task_runner_;
|
||||
|
||||
std::unique_ptr<AudioDriver> audio_driver_;
|
||||
std::unique_ptr<AudioSink> audio_sink_;
|
||||
|
||||
bool audio_enabled_ = true;
|
||||
|
||||
// AudioDriverDelegate implementation
|
||||
// AudioSinkDelegate implementation
|
||||
int GetChannelCount() final { return kChannelCount; }
|
||||
void RenderAudio(float* output_buffer, size_t num_frames) final;
|
||||
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
#ifndef ENGINE_AUDIO_AUDIO_SINK_H
|
||||
#define ENGINE_AUDIO_AUDIO_SINK_H
|
||||
|
||||
namespace eng {
|
||||
|
||||
// Models an audio sink sending mixed audio to the audio driver. Audio data from
|
||||
// the mixer source is delivered on a pull model using AudioSinkDelegate.
|
||||
class AudioSink {
|
||||
public:
|
||||
AudioSink() = default;
|
||||
virtual ~AudioSink() = default;
|
||||
|
||||
virtual bool Initialize() = 0;
|
||||
|
||||
virtual void Suspend() = 0;
|
||||
virtual void Resume() = 0;
|
||||
|
||||
virtual int GetHardwareSampleRate() = 0;
|
||||
|
||||
private:
|
||||
AudioSink(const AudioSink&) = delete;
|
||||
AudioSink& operator=(const AudioSink&) = delete;
|
||||
};
|
||||
|
||||
} // namespace eng
|
||||
|
||||
#endif // ENGINE_AUDIO_AUDIO_SINK_H
|
|
@ -1,20 +1,20 @@
|
|||
#include "engine/audio/audio_driver_alsa.h"
|
||||
#include "engine/audio/audio_sink_alsa.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <alsa/asoundlib.h>
|
||||
|
||||
#include "base/log.h"
|
||||
#include "engine/audio/audio_driver_delegate.h"
|
||||
#include "engine/audio/audio_sink_delegate.h"
|
||||
|
||||
using namespace base;
|
||||
|
||||
namespace eng {
|
||||
|
||||
AudioDriverAlsa::AudioDriverAlsa(AudioDriverDelegate* delegate)
|
||||
AudioSinkAlsa::AudioSinkAlsa(AudioSinkDelegate* delegate)
|
||||
: delegate_(delegate) {}
|
||||
|
||||
AudioDriverAlsa::~AudioDriverAlsa() {
|
||||
AudioSinkAlsa::~AudioSinkAlsa() {
|
||||
LOG << "Shutting down audio.";
|
||||
|
||||
TerminateAudioThread();
|
||||
|
@ -22,7 +22,7 @@ AudioDriverAlsa::~AudioDriverAlsa() {
|
|||
snd_pcm_close(device_);
|
||||
}
|
||||
|
||||
bool AudioDriverAlsa::Initialize() {
|
||||
bool AudioSinkAlsa::Initialize() {
|
||||
LOG << "Initializing audio.";
|
||||
|
||||
int err;
|
||||
|
@ -144,28 +144,28 @@ bool AudioDriverAlsa::Initialize() {
|
|||
return false;
|
||||
}
|
||||
|
||||
void AudioDriverAlsa::Suspend() {
|
||||
void AudioSinkAlsa::Suspend() {
|
||||
suspend_audio_thread_.store(true, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
void AudioDriverAlsa::Resume() {
|
||||
void AudioSinkAlsa::Resume() {
|
||||
suspend_audio_thread_.store(false, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
int AudioDriverAlsa::GetHardwareSampleRate() {
|
||||
int AudioSinkAlsa::GetHardwareSampleRate() {
|
||||
return sample_rate_;
|
||||
}
|
||||
|
||||
void AudioDriverAlsa::StartAudioThread() {
|
||||
void AudioSinkAlsa::StartAudioThread() {
|
||||
DCHECK(!audio_thread_.joinable());
|
||||
|
||||
LOG << "Starting audio thread.";
|
||||
terminate_audio_thread_.store(false, std::memory_order_relaxed);
|
||||
suspend_audio_thread_.store(false, std::memory_order_relaxed);
|
||||
audio_thread_ = std::thread(&AudioDriverAlsa::AudioThreadMain, this);
|
||||
audio_thread_ = std::thread(&AudioSinkAlsa::AudioThreadMain, this);
|
||||
}
|
||||
|
||||
void AudioDriverAlsa::TerminateAudioThread() {
|
||||
void AudioSinkAlsa::TerminateAudioThread() {
|
||||
if (!audio_thread_.joinable())
|
||||
return;
|
||||
|
||||
|
@ -175,7 +175,7 @@ void AudioDriverAlsa::TerminateAudioThread() {
|
|||
audio_thread_.join();
|
||||
}
|
||||
|
||||
void AudioDriverAlsa::AudioThreadMain() {
|
||||
void AudioSinkAlsa::AudioThreadMain() {
|
||||
DCHECK(delegate_);
|
||||
|
||||
size_t num_frames = period_size_ / (num_channels_ * sizeof(float));
|
|
@ -1,21 +1,21 @@
|
|||
#ifndef ENGINE_AUDIO_AUDIO_DRIVER_ALSA_H
|
||||
#define ENGINE_AUDIO_AUDIO_DRIVER_ALSA_H
|
||||
#ifndef ENGINE_AUDIO_AUDIO_SINK_ALSA_H
|
||||
#define ENGINE_AUDIO_AUDIO_SINK_ALSA_H
|
||||
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
#include "engine/audio/audio_driver.h"
|
||||
#include "engine/audio/audio_sink.h"
|
||||
|
||||
typedef struct _snd_pcm snd_pcm_t;
|
||||
|
||||
namespace eng {
|
||||
|
||||
class AudioDriverDelegate;
|
||||
class AudioSinkDelegate;
|
||||
|
||||
class AudioDriverAlsa final : public AudioDriver {
|
||||
class AudioSinkAlsa final : public AudioSink {
|
||||
public:
|
||||
AudioDriverAlsa(AudioDriverDelegate* delegate);
|
||||
~AudioDriverAlsa() final;
|
||||
AudioSinkAlsa(AudioSinkDelegate* delegate);
|
||||
~AudioSinkAlsa() final;
|
||||
|
||||
bool Initialize() final;
|
||||
|
||||
|
@ -36,7 +36,7 @@ class AudioDriverAlsa final : public AudioDriver {
|
|||
int sample_rate_ = 0;
|
||||
size_t period_size_ = 0;
|
||||
|
||||
AudioDriverDelegate* delegate_ = nullptr;
|
||||
AudioSinkDelegate* delegate_ = nullptr;
|
||||
|
||||
void StartAudioThread();
|
||||
void TerminateAudioThread();
|
||||
|
@ -46,4 +46,4 @@ class AudioDriverAlsa final : public AudioDriver {
|
|||
|
||||
} // namespace eng
|
||||
|
||||
#endif // ENGINE_AUDIO_AUDIO_DRIVER_ALSA_H
|
||||
#endif // ENGINE_AUDIO_AUDIO_SINK_ALSA_H
|
|
@ -0,0 +1,20 @@
|
|||
#ifndef ENGINE_AUDIO_AUDIO_SINK_DELEGATE_H
|
||||
#define ENGINE_AUDIO_AUDIO_SINK_DELEGATE_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
namespace eng {
|
||||
|
||||
class AudioSinkDelegate {
|
||||
public:
|
||||
AudioSinkDelegate() = default;
|
||||
virtual ~AudioSinkDelegate() = default;
|
||||
|
||||
virtual int GetChannelCount() = 0;
|
||||
|
||||
virtual void RenderAudio(float* output_buffer, size_t num_frames) = 0;
|
||||
};
|
||||
|
||||
} // namespace eng
|
||||
|
||||
#endif // ENGINE_AUDIO_AUDIO_SINK_DELEGATE_H
|
|
@ -1,61 +1,61 @@
|
|||
#include "engine/audio/audio_driver_oboe.h"
|
||||
#include "engine/audio/audio_sink_oboe.h"
|
||||
|
||||
#include "base/log.h"
|
||||
#include "engine/audio/audio_driver_delegate.h"
|
||||
#include "engine/audio/audio_sink_delegate.h"
|
||||
#include "third_party/oboe/include/oboe/Oboe.h"
|
||||
|
||||
using namespace base;
|
||||
|
||||
namespace eng {
|
||||
|
||||
AudioDriverOboe::AudioDriverOboe(AudioDriverDelegate* delegate)
|
||||
AudioSinkOboe::AudioSinkOboe(AudioSinkDelegate* delegate)
|
||||
: callback_(std::make_unique<StreamCallback>(this)), delegate_(delegate) {}
|
||||
|
||||
AudioDriverOboe::~AudioDriverOboe() {
|
||||
AudioSinkOboe::~AudioSinkOboe() {
|
||||
LOG << "Shutting down audio.";
|
||||
stream_->stop();
|
||||
}
|
||||
|
||||
bool AudioDriverOboe::Initialize() {
|
||||
bool AudioSinkOboe::Initialize() {
|
||||
LOG << "Initializing audio.";
|
||||
return RestartStream();
|
||||
}
|
||||
|
||||
void AudioDriverOboe::Suspend() {
|
||||
void AudioSinkOboe::Suspend() {
|
||||
stream_->pause();
|
||||
}
|
||||
|
||||
void AudioDriverOboe::Resume() {
|
||||
void AudioSinkOboe::Resume() {
|
||||
stream_->start();
|
||||
}
|
||||
|
||||
int AudioDriverOboe::GetHardwareSampleRate() {
|
||||
int AudioSinkOboe::GetHardwareSampleRate() {
|
||||
return stream_->getSampleRate();
|
||||
}
|
||||
|
||||
AudioDriverOboe::StreamCallback::StreamCallback(AudioDriverOboe* driver)
|
||||
: driver_(driver) {}
|
||||
AudioSinkOboe::StreamCallback::StreamCallback(AudioSinkOboe* audio_sink)
|
||||
: audio_sink_(audio_sink) {}
|
||||
|
||||
AudioDriverOboe::StreamCallback::~StreamCallback() = default;
|
||||
AudioSinkOboe::StreamCallback::~StreamCallback() = default;
|
||||
|
||||
oboe::DataCallbackResult AudioDriverOboe::StreamCallback::onAudioReady(
|
||||
oboe::DataCallbackResult AudioSinkOboe::StreamCallback::onAudioReady(
|
||||
oboe::AudioStream* oboe_stream,
|
||||
void* audio_data,
|
||||
int32_t num_frames) {
|
||||
float* output_buffer = static_cast<float*>(audio_data);
|
||||
driver_->delegate_->RenderAudio(output_buffer, num_frames);
|
||||
audio_sink_->delegate_->RenderAudio(output_buffer, num_frames);
|
||||
return oboe::DataCallbackResult::Continue;
|
||||
}
|
||||
|
||||
void AudioDriverOboe::StreamCallback::onErrorAfterClose(
|
||||
void AudioSinkOboe::StreamCallback::onErrorAfterClose(
|
||||
oboe::AudioStream* oboe_stream,
|
||||
oboe::Result error) {
|
||||
LOG << "Error after close. Error: " << oboe::convertToText(error);
|
||||
|
||||
driver_->RestartStream();
|
||||
audio_sink_->RestartStream();
|
||||
}
|
||||
|
||||
bool AudioDriverOboe::RestartStream() {
|
||||
bool AudioSinkOboe::RestartStream() {
|
||||
oboe::AudioStreamBuilder builder;
|
||||
oboe::Result result =
|
||||
builder.setSharingMode(oboe::SharingMode::Exclusive)
|
|
@ -1,21 +1,21 @@
|
|||
#ifndef ENGINE_AUDIO_AUDIO_DRIVER_OBOE_H
|
||||
#define ENGINE_AUDIO_AUDIO_DRIVER_OBOE_H
|
||||
#ifndef ENGINE_AUDIO_AUDIO_SINK_OBOE_H
|
||||
#define ENGINE_AUDIO_AUDIO_SINK_OBOE_H
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "third_party/oboe/include/oboe/AudioStream.h"
|
||||
#include "third_party/oboe/include/oboe/AudioStreamCallback.h"
|
||||
|
||||
#include "engine/audio/audio_driver.h"
|
||||
#include "engine/audio/audio_sink.h"
|
||||
|
||||
namespace eng {
|
||||
|
||||
class AudioDriverDelegate;
|
||||
class AudioSinkDelegate;
|
||||
|
||||
class AudioDriverOboe final : public AudioDriver {
|
||||
class AudioSinkOboe final : public AudioSink {
|
||||
public:
|
||||
AudioDriverOboe(AudioDriverDelegate* delegate);
|
||||
~AudioDriverOboe() final;
|
||||
AudioSinkOboe(AudioSinkDelegate* delegate);
|
||||
~AudioSinkOboe() final;
|
||||
|
||||
bool Initialize() final;
|
||||
|
||||
|
@ -27,7 +27,7 @@ class AudioDriverOboe final : public AudioDriver {
|
|||
private:
|
||||
class StreamCallback final : public oboe::AudioStreamCallback {
|
||||
public:
|
||||
StreamCallback(AudioDriverOboe* audio);
|
||||
StreamCallback(AudioSinkOboe* audio);
|
||||
~StreamCallback() final;
|
||||
|
||||
oboe::DataCallbackResult onAudioReady(oboe::AudioStream* oboe_stream,
|
||||
|
@ -38,17 +38,17 @@ class AudioDriverOboe final : public AudioDriver {
|
|||
oboe::Result error) final;
|
||||
|
||||
private:
|
||||
AudioDriverOboe* driver_;
|
||||
AudioSinkOboe* audio_sink_;
|
||||
};
|
||||
|
||||
oboe::ManagedStream stream_;
|
||||
std::unique_ptr<StreamCallback> callback_;
|
||||
|
||||
AudioDriverDelegate* delegate_ = nullptr;
|
||||
AudioSinkDelegate* delegate_ = nullptr;
|
||||
|
||||
bool RestartStream();
|
||||
};
|
||||
|
||||
} // namespace eng
|
||||
|
||||
#endif // ENGINE_AUDIO_AUDIO_DRIVER_OBOE_H
|
||||
#endif // ENGINE_AUDIO_AUDIO_SINK_OBOE_H
|
Loading…
Reference in New Issue