aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Wu <85952307+robertwu1@users.noreply.github.com>2022-11-30 15:43:17 -0800
committerGitHub <noreply@github.com>2022-11-30 15:43:17 -0800
commit34e89ecf41f55aad6d4375c7be4a287cb969117a (patch)
treee53acebd6c3d11ae11e7574a4e03031489222c12
parent32515c8cab3a90811ef4891049dc7b570e8151b3 (diff)
downloadoboe-34e89ecf41f55aad6d4375c7be4a287cb969117a.tar.gz
SoundBoard/MegaDrone: Retry open if it fails (#1664)
-rw-r--r--samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp34
-rw-r--r--samples/MegaDrone/src/main/cpp/MegaDroneEngine.h1
-rw-r--r--samples/SoundBoard/src/main/cpp/SoundBoardEngine.cpp33
-rw-r--r--samples/SoundBoard/src/main/cpp/SoundBoardEngine.h1
4 files changed, 59 insertions, 10 deletions
diff --git a/samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp b/samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp
index 26c36013..df3309ef 100644
--- a/samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp
+++ b/samples/MegaDrone/src/main/cpp/MegaDroneEngine.cpp
@@ -76,17 +76,41 @@ void MegaDroneEngine::createCallback(std::vector<int> cpuIds){
mDataCallback->setThreadAffinityEnabled(true);
}
-bool MegaDroneEngine::start(){
+bool MegaDroneEngine::start() {
+ // It is possible for a stream's device to become disconnected during stream open or between
+ // stream open and stream start.
+ // If the stream fails to start, close the old stream and try again.
+ bool didStart = false;
+ int tryCount = 0;
+ do {
+ if (tryCount > 0) {
+ usleep(20 * 1000); // Sleep between tries to give the system time to settle.
+ }
+ didStart = attemptStart();
+ } while (!didStart && tryCount++ < 3);
+ if (!didStart) {
+ LOGE("Failed at starting the stream");
+ }
+ return didStart;
+}
+
+bool MegaDroneEngine::attemptStart() {
auto result = createPlaybackStream();
- if (result == Result::OK){
+
+ if (result == Result::OK) {
// Create our synthesizer audio source using the properties of the stream
mAudioSource = std::make_shared<Synth>(mStream->getSampleRate(), mStream->getChannelCount());
mDataCallback->reset();
mDataCallback->setSource(std::dynamic_pointer_cast<IRenderableAudio>(mAudioSource));
- mStream->start();
- return true;
+ result = mStream->start();
+ if (result == Result::OK) {
+ return true;
+ } else {
+ LOGW("Failed attempt at starting the playback stream. Error: %s", convertToText(result));
+ return false;
+ }
} else {
- LOGE("Failed to create the playback stream. Error: %s", convertToText(result));
+ LOGW("Failed attempt at creating the playback stream. Error: %s", convertToText(result));
return false;
}
}
diff --git a/samples/MegaDrone/src/main/cpp/MegaDroneEngine.h b/samples/MegaDrone/src/main/cpp/MegaDroneEngine.h
index 841ec2d4..17205224 100644
--- a/samples/MegaDrone/src/main/cpp/MegaDroneEngine.h
+++ b/samples/MegaDrone/src/main/cpp/MegaDroneEngine.h
@@ -50,6 +50,7 @@ private:
std::shared_ptr<DefaultDataCallback> mDataCallback;
std::shared_ptr<DefaultErrorCallback> mErrorCallback;
+ bool attemptStart();
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 193e3a14..defb6dcb 100644
--- a/samples/SoundBoard/src/main/cpp/SoundBoardEngine.cpp
+++ b/samples/SoundBoard/src/main/cpp/SoundBoardEngine.cpp
@@ -80,18 +80,41 @@ void SoundBoardEngine::createCallback(int32_t numSignals){
mNumSignals = numSignals;
}
-bool SoundBoardEngine::start(){
+bool SoundBoardEngine::start() {
+ // It is possible for a stream's device to become disconnected during stream open or between
+ // stream open and stream start.
+ // If the stream fails to start, close the old stream and try again.
+ bool didStart = false;
+ int tryCount = 0;
+ do {
+ if (tryCount > 0) {
+ usleep(20 * 1000); // Sleep between tries to give the system time to settle.
+ }
+ didStart = attemptStart();
+ } while (!didStart && tryCount++ < 3);
+ if (!didStart) {
+ LOGE("Failed at starting the stream");
+ }
+ return didStart;
+}
+
+bool SoundBoardEngine::attemptStart() {
auto result = createPlaybackStream();
- if (result == Result::OK){
+ if (result == Result::OK) {
// Create our synthesizer audio source using the properties of the stream
mSynth = Synth::create(mStream->getSampleRate(), mStream->getChannelCount(), mNumSignals);
mDataCallback->reset();
mDataCallback->setSource(std::dynamic_pointer_cast<IRenderableAudio>(mSynth));
- mStream->start();
- return true;
+ result = mStream->start();
+ if (result == Result::OK) {
+ return true;
+ } else {
+ LOGW("Failed attempt at starting the playback stream. Error: %s", convertToText(result));
+ return false;
+ }
} else {
- LOGE("Failed to create the playback stream. Error: %s", convertToText(result));
+ LOGW("Failed attempt at creating the playback stream. Error: %s", convertToText(result));
return false;
}
}
diff --git a/samples/SoundBoard/src/main/cpp/SoundBoardEngine.h b/samples/SoundBoard/src/main/cpp/SoundBoardEngine.h
index 4d093506..033f839f 100644
--- a/samples/SoundBoard/src/main/cpp/SoundBoardEngine.h
+++ b/samples/SoundBoard/src/main/cpp/SoundBoardEngine.h
@@ -56,6 +56,7 @@ private:
std::shared_ptr<DefaultDataCallback> mDataCallback;
std::shared_ptr<DefaultErrorCallback> mErrorCallback;
+ bool attemptStart();
oboe::Result createPlaybackStream();
void createCallback(int32_t numSignals);
};