aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Wu <85952307+robertwu1@users.noreply.github.com>2022-11-23 16:11:46 -0800
committerGitHub <noreply@github.com>2022-11-23 16:11:46 -0800
commit32515c8cab3a90811ef4891049dc7b570e8151b3 (patch)
treed14b661a8b9fad92748188eb506c662ce34ef53d
parent2c57fcbf3bc5e190d797890d84eb6b52ffb70f3d (diff)
downloadoboe-32515c8cab3a90811ef4891049dc7b570e8151b3.tar.gz
Update samples to use shared_ptr for setErrorCallback (#1661)
-rw-r--r--docs/FullGuide.md3
-rw-r--r--samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp8
-rw-r--r--samples/MegaDrone/src/main/cpp/MegaDroneEngine.h4
-rw-r--r--samples/SoundBoard/src/main/cpp/SoundBoardEngine.cpp8
-rw-r--r--samples/SoundBoard/src/main/cpp/SoundBoardEngine.h4
-rw-r--r--samples/hello-oboe/src/main/cpp/HelloOboeEngine.cpp8
-rw-r--r--samples/hello-oboe/src/main/cpp/HelloOboeEngine.h4
7 files changed, 20 insertions, 19 deletions
diff --git a/docs/FullGuide.md b/docs/FullGuide.md
index 98d8391e..405e48f2 100644
--- a/docs/FullGuide.md
+++ b/docs/FullGuide.md
@@ -333,7 +333,7 @@ An audio stream can become disconnected at any time if one of these events happe
When a stream is disconnected, it has the state "Disconnected" and calls to `write()` or other functions will return `Result::ErrorDisconnected`. When a stream is disconnected, all you can do is close it.
If you need to be informed when an audio device is disconnected, write a class
-which extends `AudioStreamErrorCallback` and then register your class using `builder.setErrorCallback(yourCallbackClass)`.
+which extends `AudioStreamErrorCallback` and then register your class using `builder.setErrorCallback(yourCallbackClass)`. It is recommended to pass a shared_ptr.
If you register a callback, then it will automatically close the stream in a separate thread if the stream is disconnected.
Your callback can implement the following methods (called in a separate thread):
@@ -349,6 +349,7 @@ Methods that reference the underlying stream should not be called (e.g. `getTime
Opening a separate stream is also a valid use of this callback, especially if the error received is `Error::Disconnected`.
However, it is important to note that the new audio device may have vastly different properties than the stream that was disconnected.
+See the SoundBoard sample for an example of setErrorCallback.
## Optimizing performance
diff --git a/samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp b/samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp
index ee4de176..26c36013 100644
--- a/samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp
+++ b/samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp
@@ -56,19 +56,19 @@ oboe::Result MegaDroneEngine::createPlaybackStream() {
return builder.setSharingMode(oboe::SharingMode::Exclusive)
->setPerformanceMode(oboe::PerformanceMode::LowLatency)
->setFormat(oboe::AudioFormat::Float)
- ->setDataCallback(mDataCallback.get())
- ->setErrorCallback(mErrorCallback.get())
+ ->setDataCallback(mDataCallback)
+ ->setErrorCallback(mErrorCallback)
->openStream(mStream);
}
// Create the callback and set its thread affinity to the supplied CPU core IDs
void MegaDroneEngine::createCallback(std::vector<int> cpuIds){
- mDataCallback = std::make_unique<DefaultDataCallback>();
+ mDataCallback = std::make_shared<DefaultDataCallback>();
// Create the error callback, we supply ourselves as the parent so that we can restart the stream
// when it's disconnected
- mErrorCallback = std::make_unique<DefaultErrorCallback>(*this);
+ mErrorCallback = std::make_shared<DefaultErrorCallback>(*this);
// Bind the audio callback to specific CPU cores as this can help avoid underruns caused by
// core migrations
diff --git a/samples/MegaDrone/src/main/cpp/MegaDroneEngine.h b/samples/MegaDrone/src/main/cpp/MegaDroneEngine.h
index d0b84a41..841ec2d4 100644
--- a/samples/MegaDrone/src/main/cpp/MegaDroneEngine.h
+++ b/samples/MegaDrone/src/main/cpp/MegaDroneEngine.h
@@ -47,8 +47,8 @@ public:
private:
std::shared_ptr<AudioStream> mStream;
std::shared_ptr<TappableAudioSource> mAudioSource;
- std::unique_ptr<DefaultDataCallback> mDataCallback;
- std::unique_ptr<DefaultErrorCallback> mErrorCallback;
+ std::shared_ptr<DefaultDataCallback> mDataCallback;
+ std::shared_ptr<DefaultErrorCallback> mErrorCallback;
oboe::Result createPlaybackStream();
void createCallback(std::vector<int> cpuIds);
diff --git a/samples/SoundBoard/src/main/cpp/SoundBoardEngine.cpp b/samples/SoundBoard/src/main/cpp/SoundBoardEngine.cpp
index af228b55..193e3a14 100644
--- a/samples/SoundBoard/src/main/cpp/SoundBoardEngine.cpp
+++ b/samples/SoundBoard/src/main/cpp/SoundBoardEngine.cpp
@@ -63,19 +63,19 @@ oboe::Result SoundBoardEngine::createPlaybackStream() {
return builder.setSharingMode(oboe::SharingMode::Exclusive)
->setPerformanceMode(oboe::PerformanceMode::LowLatency)
->setFormat(oboe::AudioFormat::Float)
- ->setDataCallback(mDataCallback.get())
- ->setErrorCallback(mErrorCallback.get())
+ ->setDataCallback(mDataCallback)
+ ->setErrorCallback(mErrorCallback)
->openStream(mStream);
}
// Create the callback and set its thread affinity to the supplied CPU core IDs
void SoundBoardEngine::createCallback(int32_t numSignals){
- mDataCallback = std::make_unique<DefaultDataCallback>();
+ mDataCallback = std::make_shared<DefaultDataCallback>();
// Create the error callback, we supply ourselves as the parent so that we can restart the stream
// when it's disconnected
- mErrorCallback = std::make_unique<DefaultErrorCallback>(*this);
+ mErrorCallback = std::make_shared<DefaultErrorCallback>(*this);
mNumSignals = numSignals;
}
diff --git a/samples/SoundBoard/src/main/cpp/SoundBoardEngine.h b/samples/SoundBoard/src/main/cpp/SoundBoardEngine.h
index fb0610da..4d093506 100644
--- a/samples/SoundBoard/src/main/cpp/SoundBoardEngine.h
+++ b/samples/SoundBoard/src/main/cpp/SoundBoardEngine.h
@@ -53,8 +53,8 @@ private:
std::shared_ptr<AudioStream> mStream;
std::shared_ptr<Synth> mSynth;
- std::unique_ptr<DefaultDataCallback> mDataCallback;
- std::unique_ptr<DefaultErrorCallback> mErrorCallback;
+ std::shared_ptr<DefaultDataCallback> mDataCallback;
+ std::shared_ptr<DefaultErrorCallback> mErrorCallback;
oboe::Result createPlaybackStream();
void createCallback(int32_t numSignals);
diff --git a/samples/hello-oboe/src/main/cpp/HelloOboeEngine.cpp b/samples/hello-oboe/src/main/cpp/HelloOboeEngine.cpp
index 275e52c0..13a50a1a 100644
--- a/samples/hello-oboe/src/main/cpp/HelloOboeEngine.cpp
+++ b/samples/hello-oboe/src/main/cpp/HelloOboeEngine.cpp
@@ -35,8 +35,8 @@
*
*/
HelloOboeEngine::HelloOboeEngine()
- : mLatencyCallback(std::make_unique<LatencyTuningCallback>()),
- mErrorCallback(std::make_unique<DefaultErrorCallback>(*this)) {
+ : mLatencyCallback(std::make_shared<LatencyTuningCallback>()),
+ mErrorCallback(std::make_shared<DefaultErrorCallback>(*this)) {
}
double HelloOboeEngine::getCurrentOutputLatencyMillis() {
@@ -112,8 +112,8 @@ oboe::Result HelloOboeEngine::openPlaybackStream() {
->setPerformanceMode(oboe::PerformanceMode::LowLatency)
->setFormat(oboe::AudioFormat::Float)
->setFormatConversionAllowed(true)
- ->setDataCallback(mLatencyCallback.get())
- ->setErrorCallback(mErrorCallback.get())
+ ->setDataCallback(mLatencyCallback)
+ ->setErrorCallback(mErrorCallback)
->setAudioApi(mAudioApi)
->setChannelCount(mChannelCount)
->setDeviceId(mDeviceId)
diff --git a/samples/hello-oboe/src/main/cpp/HelloOboeEngine.h b/samples/hello-oboe/src/main/cpp/HelloOboeEngine.h
index f7ead262..1b255816 100644
--- a/samples/hello-oboe/src/main/cpp/HelloOboeEngine.h
+++ b/samples/hello-oboe/src/main/cpp/HelloOboeEngine.h
@@ -93,8 +93,8 @@ private:
oboe::Result openPlaybackStream();
std::shared_ptr<oboe::AudioStream> mStream;
- std::unique_ptr<LatencyTuningCallback> mLatencyCallback;
- std::unique_ptr<DefaultErrorCallback> mErrorCallback;
+ std::shared_ptr<LatencyTuningCallback> mLatencyCallback;
+ std::shared_ptr<DefaultErrorCallback> mErrorCallback;
std::shared_ptr<SoundGenerator> mAudioSource;
bool mIsLatencyDetectionSupported = false;