diff options
author | Don Turner <donturner@google.com> | 2020-11-18 16:57:05 +0000 |
---|---|---|
committer | Don Turner <dturner@users.noreply.github.com> | 2020-11-24 18:46:38 +0000 |
commit | 56f51912b3a19eae9c68399dd69125a1eeebe992 (patch) | |
tree | b4329ba529503fb0fa5701044c2c0369a406bd3f /samples | |
parent | 016449e4d82d88608eedad6d3432a7cd43c3181c (diff) | |
download | oboe-56f51912b3a19eae9c68399dd69125a1eeebe992.tar.gz |
Update samples
Diffstat (limited to 'samples')
-rw-r--r-- | samples/LiveEffect/src/main/cpp/LiveEffectEngine.cpp | 5 | ||||
-rw-r--r-- | samples/LiveEffect/src/main/cpp/LiveEffectEngine.h | 6 | ||||
-rw-r--r-- | samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp | 17 | ||||
-rw-r--r-- | samples/MegaDrone/src/main/cpp/MegaDroneEngine.h | 6 | ||||
-rw-r--r-- | samples/RhythmGame/src/main/cpp/Game.cpp | 3 | ||||
-rw-r--r-- | samples/RhythmGame/src/main/cpp/Game.h | 4 | ||||
-rw-r--r-- | samples/hello-oboe/src/main/cpp/HelloOboeEngine.cpp | 7 | ||||
-rw-r--r-- | samples/hello-oboe/src/main/cpp/HelloOboeEngine.h | 3 | ||||
-rw-r--r-- | samples/hello-oboe/src/main/cpp/LatencyTuningCallback.cpp | 2 | ||||
-rw-r--r-- | samples/hello-oboe/src/main/cpp/LatencyTuningCallback.h | 8 | ||||
-rw-r--r-- | samples/shared/DefaultDataCallback.h (renamed from samples/shared/DefaultAudioStreamCallback.h) | 37 | ||||
-rw-r--r-- | samples/shared/DefaultErrorCallback.h | 56 |
12 files changed, 110 insertions, 44 deletions
diff --git a/samples/LiveEffect/src/main/cpp/LiveEffectEngine.cpp b/samples/LiveEffect/src/main/cpp/LiveEffectEngine.cpp index 5ab46398..586dd133 100644 --- a/samples/LiveEffect/src/main/cpp/LiveEffectEngine.cpp +++ b/samples/LiveEffect/src/main/cpp/LiveEffectEngine.cpp @@ -116,7 +116,7 @@ oboe::Result LiveEffectEngine::openStreams() { oboe::AudioStreamBuilder *LiveEffectEngine::setupRecordingStreamParameters( oboe::AudioStreamBuilder *builder) { // This sample uses blocking read() by setting callback to null - builder->setCallback(nullptr) + builder->setDataCallback(nullptr) ->setDeviceId(mRecordingDeviceId) ->setDirection(oboe::Direction::Input) ->setSampleRate(mSampleRate) @@ -132,7 +132,8 @@ oboe::AudioStreamBuilder *LiveEffectEngine::setupRecordingStreamParameters( */ oboe::AudioStreamBuilder *LiveEffectEngine::setupPlaybackStreamParameters( oboe::AudioStreamBuilder *builder) { - builder->setCallback(this) + builder->setDataCallback(this) + ->setErrorCallback(this) ->setDeviceId(mPlaybackDeviceId) ->setDirection(oboe::Direction::Output) ->setChannelCount(mOutputChannelCount); diff --git a/samples/LiveEffect/src/main/cpp/LiveEffectEngine.h b/samples/LiveEffect/src/main/cpp/LiveEffectEngine.h index 47a7fcad..f79d5965 100644 --- a/samples/LiveEffect/src/main/cpp/LiveEffectEngine.h +++ b/samples/LiveEffect/src/main/cpp/LiveEffectEngine.h @@ -36,10 +36,14 @@ class LiveEffectEngine : public oboe::AudioStreamCallback { bool setEffectOn(bool isOn); /* - * oboe::AudioStreamCallback interface implementation + * oboe::AudioStreamDataCallback interface implementation */ oboe::DataCallbackResult onAudioReady(oboe::AudioStream *oboeStream, void *audioData, int32_t numFrames) override; + + /* + * oboe::AudioStreamErrorCallback interface implementation + */ void onErrorBeforeClose(oboe::AudioStream *oboeStream, oboe::Result error) override; void onErrorAfterClose(oboe::AudioStream *oboeStream, oboe::Result error) override; diff --git a/samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp b/samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp index 5bf94ff9..ee4de176 100644 --- a/samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp +++ b/samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp @@ -56,20 +56,24 @@ oboe::Result MegaDroneEngine::createPlaybackStream() { return builder.setSharingMode(oboe::SharingMode::Exclusive) ->setPerformanceMode(oboe::PerformanceMode::LowLatency) ->setFormat(oboe::AudioFormat::Float) - ->setCallback(mCallback.get()) + ->setDataCallback(mDataCallback.get()) + ->setErrorCallback(mErrorCallback.get()) ->openStream(mStream); } // Create the callback and set its thread affinity to the supplied CPU core IDs void MegaDroneEngine::createCallback(std::vector<int> cpuIds){ - // Create the callback, we supply ourselves as the parent so that we can restart the stream + + mDataCallback = std::make_unique<DefaultDataCallback>(); + + // Create the error callback, we supply ourselves as the parent so that we can restart the stream // when it's disconnected - mCallback = std::make_unique<DefaultAudioStreamCallback>(*this); + mErrorCallback = std::make_unique<DefaultErrorCallback>(*this); // Bind the audio callback to specific CPU cores as this can help avoid underruns caused by // core migrations - mCallback->setCpuIds(cpuIds); - mCallback->setThreadAffinityEnabled(true); + mDataCallback->setCpuIds(cpuIds); + mDataCallback->setThreadAffinityEnabled(true); } bool MegaDroneEngine::start(){ @@ -77,7 +81,8 @@ bool MegaDroneEngine::start(){ if (result == Result::OK){ // Create our synthesizer audio source using the properties of the stream mAudioSource = std::make_shared<Synth>(mStream->getSampleRate(), mStream->getChannelCount()); - mCallback->setSource(std::dynamic_pointer_cast<IRenderableAudio>(mAudioSource)); + mDataCallback->reset(); + mDataCallback->setSource(std::dynamic_pointer_cast<IRenderableAudio>(mAudioSource)); mStream->start(); return true; } else { diff --git a/samples/MegaDrone/src/main/cpp/MegaDroneEngine.h b/samples/MegaDrone/src/main/cpp/MegaDroneEngine.h index 9f387bbd..d0b84a41 100644 --- a/samples/MegaDrone/src/main/cpp/MegaDroneEngine.h +++ b/samples/MegaDrone/src/main/cpp/MegaDroneEngine.h @@ -22,9 +22,10 @@ #include <vector> #include "Synth.h" -#include <DefaultAudioStreamCallback.h> +#include <DefaultDataCallback.h> #include <TappableAudioSource.h> #include <IRestartable.h> +#include <DefaultErrorCallback.h> using namespace oboe; @@ -46,7 +47,8 @@ public: private: std::shared_ptr<AudioStream> mStream; std::shared_ptr<TappableAudioSource> mAudioSource; - std::unique_ptr<DefaultAudioStreamCallback> mCallback; + std::unique_ptr<DefaultDataCallback> mDataCallback; + std::unique_ptr<DefaultErrorCallback> mErrorCallback; oboe::Result createPlaybackStream(); void createCallback(std::vector<int> cpuIds); diff --git a/samples/RhythmGame/src/main/cpp/Game.cpp b/samples/RhythmGame/src/main/cpp/Game.cpp index c1ef0808..1f50df45 100644 --- a/samples/RhythmGame/src/main/cpp/Game.cpp +++ b/samples/RhythmGame/src/main/cpp/Game.cpp @@ -175,7 +175,8 @@ bool Game::openStream() { // Create an audio stream AudioStreamBuilder builder; - builder.setCallback(this); + builder.setDataCallback(this); + builder.setErrorCallback(this); builder.setPerformanceMode(PerformanceMode::LowLatency); builder.setSharingMode(SharingMode::Exclusive); diff --git a/samples/RhythmGame/src/main/cpp/Game.h b/samples/RhythmGame/src/main/cpp/Game.h index 60cce809..cfe1c15d 100644 --- a/samples/RhythmGame/src/main/cpp/Game.h +++ b/samples/RhythmGame/src/main/cpp/Game.h @@ -51,9 +51,11 @@ public: void tick(); void tap(int64_t eventTimeAsUptime); - // Inherited from oboe::AudioStreamCallback + // Inherited from oboe::AudioStreamDataCallback DataCallbackResult onAudioReady(AudioStream *oboeStream, void *audioData, int32_t numFrames) override; + + // Inherited from oboe::AudioStreamErrorCallback void onErrorAfterClose(AudioStream *oboeStream, Result error) override; private: diff --git a/samples/hello-oboe/src/main/cpp/HelloOboeEngine.cpp b/samples/hello-oboe/src/main/cpp/HelloOboeEngine.cpp index bb195cbb..fe3b8f3f 100644 --- a/samples/hello-oboe/src/main/cpp/HelloOboeEngine.cpp +++ b/samples/hello-oboe/src/main/cpp/HelloOboeEngine.cpp @@ -35,7 +35,8 @@ * */ HelloOboeEngine::HelloOboeEngine() - : mLatencyCallback(std::make_unique<LatencyTuningCallback>(*this)) { + : mLatencyCallback(std::make_unique<LatencyTuningCallback>()), + mErrorCallback(std::make_unique<DefaultErrorCallback>(*this)){ } double HelloOboeEngine::getCurrentOutputLatencyMillis() { @@ -119,7 +120,8 @@ oboe::Result HelloOboeEngine::createPlaybackStream() { return builder.setSharingMode(oboe::SharingMode::Exclusive) ->setPerformanceMode(oboe::PerformanceMode::LowLatency) ->setFormat(oboe::AudioFormat::Float) - ->setCallback(mLatencyCallback.get()) + ->setDataCallback(mLatencyCallback.get()) + ->setErrorCallback(mErrorCallback.get()) ->setAudioApi(mAudioApi) ->setChannelCount(mChannelCount) ->setDeviceId(mDeviceId) @@ -128,6 +130,7 @@ oboe::Result HelloOboeEngine::createPlaybackStream() { void HelloOboeEngine::restart() { // The stream will have already been closed by the error callback. + mLatencyCallback->reset(); start(); } diff --git a/samples/hello-oboe/src/main/cpp/HelloOboeEngine.h b/samples/hello-oboe/src/main/cpp/HelloOboeEngine.h index 7826c784..29231eec 100644 --- a/samples/hello-oboe/src/main/cpp/HelloOboeEngine.h +++ b/samples/hello-oboe/src/main/cpp/HelloOboeEngine.h @@ -22,6 +22,7 @@ #include "SoundGenerator.h" #include "LatencyTuningCallback.h" #include "IRestartable.h" +#include "DefaultErrorCallback.h" constexpr int32_t kBufferSizeAutomatic = 0; @@ -88,10 +89,10 @@ public: private: oboe::Result reopenStream(); oboe::Result createPlaybackStream(); - void updateLatencyDetection(); std::shared_ptr<oboe::AudioStream> mStream; std::unique_ptr<LatencyTuningCallback> mLatencyCallback; + std::unique_ptr<DefaultErrorCallback> mErrorCallback; std::shared_ptr<SoundGenerator> mAudioSource; bool mIsLatencyDetectionSupported = false; diff --git a/samples/hello-oboe/src/main/cpp/LatencyTuningCallback.cpp b/samples/hello-oboe/src/main/cpp/LatencyTuningCallback.cpp index eb6c9a8c..7d53c430 100644 --- a/samples/hello-oboe/src/main/cpp/LatencyTuningCallback.cpp +++ b/samples/hello-oboe/src/main/cpp/LatencyTuningCallback.cpp @@ -37,7 +37,7 @@ oboe::DataCallbackResult LatencyTuningCallback::onAudioReady( */ if (Trace::isEnabled()) Trace::beginSection("numFrames %d, Underruns %d, buffer size %d", numFrames, underrunCountResult.value(), bufferSize); - auto result = DefaultAudioStreamCallback::onAudioReady(oboeStream, audioData, numFrames); + auto result = DefaultDataCallback::onAudioReady(oboeStream, audioData, numFrames); if (Trace::isEnabled()) Trace::endSection(); return result; } diff --git a/samples/hello-oboe/src/main/cpp/LatencyTuningCallback.h b/samples/hello-oboe/src/main/cpp/LatencyTuningCallback.h index 3b61a16d..16051546 100644 --- a/samples/hello-oboe/src/main/cpp/LatencyTuningCallback.h +++ b/samples/hello-oboe/src/main/cpp/LatencyTuningCallback.h @@ -22,19 +22,19 @@ #include <oboe/LatencyTuner.h> #include <TappableAudioSource.h> -#include <DefaultAudioStreamCallback.h> +#include <DefaultDataCallback.h> #include <trace.h> /** - * This callback object extends the functionality of `DefaultAudioStreamCallback` by automatically + * This callback object extends the functionality of `DefaultDataCallback` by automatically * tuning the latency of the audio stream. @see onAudioReady for more details on this. * * It also demonstrates how to use tracing functions for logging inside the audio callback without * blocking. */ -class LatencyTuningCallback: public DefaultAudioStreamCallback { +class LatencyTuningCallback: public DefaultDataCallback { public: - LatencyTuningCallback(IRestartable &mParent) : DefaultAudioStreamCallback(mParent) { + LatencyTuningCallback() : DefaultDataCallback() { // Initialize the trace functions, this enables you to output trace statements without // blocking. See https://developer.android.com/studio/profile/systrace-commandline.html diff --git a/samples/shared/DefaultAudioStreamCallback.h b/samples/shared/DefaultDataCallback.h index 8fd07eba..e7f858e0 100644 --- a/samples/shared/DefaultAudioStreamCallback.h +++ b/samples/shared/DefaultDataCallback.h @@ -14,8 +14,8 @@ * limitations under the License. */ -#ifndef SAMPLES_DEFAULT_AUDIO_STREAM_CALLBACK_H -#define SAMPLES_DEFAULT_AUDIO_STREAM_CALLBACK_H +#ifndef SAMPLES_DEFAULT_DATA_CALLBACK_H +#define SAMPLES_DEFAULT_DATA_CALLBACK_H #include <vector> @@ -26,16 +26,12 @@ #include "IRestartable.h" /** - * This is a callback object which will render data from an `IRenderableAudio` source. It is - * constructed using an `IRestartable` which allows it to automatically restart the parent object - * if the stream is disconnected (for example, when headphones are attached). - * - * @param IRestartable - the object which should be restarted when the stream is disconnected + * This is a callback object which will render data from an `IRenderableAudio` source. */ -class DefaultAudioStreamCallback : public oboe::AudioStreamCallback { +class DefaultDataCallback : public oboe::AudioStreamDataCallback { public: - DefaultAudioStreamCallback(IRestartable &parent): mParent(parent) {} - virtual ~DefaultAudioStreamCallback() = default; + DefaultDataCallback() {} + virtual ~DefaultDataCallback() = default; virtual oboe::DataCallbackResult onAudioReady(oboe::AudioStream *oboeStream, void *audioData, int32_t numFrames) override { @@ -56,21 +52,17 @@ public: return oboe::DataCallbackResult::Continue; } - virtual void onErrorAfterClose(oboe::AudioStream *oboeStream, oboe::Result error) override { - // Restart the stream when it errors out with disconnect - if (error == oboe::Result::ErrorDisconnected) { - LOGE("Restarting AudioStream after disconnect"); - mParent.restart(); - } else { - LOGE("Unknown error"); - } - mIsThreadAffinitySet = false; - } - void setSource(std::shared_ptr<IRenderableAudio> renderable) { mRenderable = renderable; } + /** + * Reset the callback to its initial state. + */ + void reset(){ + mIsThreadAffinitySet = false; + } + std::shared_ptr<IRenderableAudio> getSource() { return mRenderable; } @@ -98,7 +90,6 @@ public: private: std::shared_ptr<IRenderableAudio> mRenderable; - IRestartable &mParent; std::vector<int> mCpuIds; // IDs of CPU cores which the audio callback should be bound to std::atomic<bool> mIsThreadAffinityEnabled { false }; std::atomic<bool> mIsThreadAffinitySet { false }; @@ -139,4 +130,4 @@ private: }; -#endif //SAMPLES_DEFAULT_AUDIO_STREAM_CALLBACK_H +#endif //SAMPLES_DEFAULT_DATA_CALLBACK_H diff --git a/samples/shared/DefaultErrorCallback.h b/samples/shared/DefaultErrorCallback.h new file mode 100644 index 00000000..12a80749 --- /dev/null +++ b/samples/shared/DefaultErrorCallback.h @@ -0,0 +1,56 @@ +/* + * Copyright 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef SAMPLES_DEFAULT_ERROR_CALLBACK_H +#define SAMPLES_DEFAULT_ERROR_CALLBACK_H + +#include <vector> +#include <oboe/AudioStreamCallback.h> +#include <logging_macros.h> + +#include "IRestartable.h" + +/** + * This is a callback object which will be called when a stream error occurs. + * + * It is constructed using an `IRestartable` which allows it to automatically restart the parent + * object if the stream is disconnected (for example, when headphones are attached). + * + * @param IRestartable - the object which should be restarted when the stream is disconnected + */ +class DefaultErrorCallback : public oboe::AudioStreamErrorCallback { +public: + + DefaultErrorCallback(IRestartable &parent): mParent(parent) {} + virtual ~DefaultErrorCallback() = default; + + virtual void onErrorAfterClose(oboe::AudioStream *oboeStream, oboe::Result error) override { + // Restart the stream when it errors out with disconnect + if (error == oboe::Result::ErrorDisconnected) { + LOGE("Restarting AudioStream after disconnect"); + mParent.restart(); + } else { + LOGE("Unknown error"); + } + } + +private: + IRestartable &mParent; + +}; + + +#endif //SAMPLES_DEFAULT_ERROR_CALLBACK_H |