aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorPhil Burk <philburk@mobileer.com>2020-04-15 13:49:11 -0700
committerPhil Burk <philburk@mobileer.com>2020-04-15 13:49:11 -0700
commit68f7df0d535e8d282cd7bc5feeca9ac5a77fc081 (patch)
treedf31885caee2c2fd0dacc72db5b3475b874055a5 /include
parent55d878a4e85e1994f2b5883366079b991500a25f (diff)
downloadoboe-68f7df0d535e8d282cd7bc5feeca9ac5a77fc081.tar.gz
oboe: add openSharedStream
To prevent race conditions with the onError callbacks that were resulting in the use of a deleted stream. For bug #820
Diffstat (limited to 'include')
-rw-r--r--include/oboe/AudioStream.h21
-rw-r--r--include/oboe/AudioStreamBuilder.h3
2 files changed, 22 insertions, 2 deletions
diff --git a/include/oboe/AudioStream.h b/include/oboe/AudioStream.h
index d9b2046a..f2452585 100644
--- a/include/oboe/AudioStream.h
+++ b/include/oboe/AudioStream.h
@@ -418,6 +418,22 @@ public:
ResultWithValue<int32_t> waitForAvailableFrames(int32_t numFrames,
int64_t timeoutNanoseconds);
+ /*
+ * INTERNAL USE ONLY
+ * Set a weak_ptr to this stream from the shared_ptr so that we can
+ * later use a shared_ptr in the error callback.
+ */
+ void setWeakThis(std::shared_ptr<oboe::AudioStream> &sharedStream) {
+ mWeakThis = sharedStream;
+ }
+
+ /*
+ * Make a shared_ptr that will prevent this stream from being deleted.
+ */
+ std::shared_ptr<oboe::AudioStream> lockWeakThis() {
+ return mWeakThis.lock();
+ }
+
protected:
/**
@@ -497,17 +513,18 @@ protected:
std::mutex mLock; // for synchronizing start/stop/close
+
private:
int mPreviousScheduler = -1;
std::atomic<bool> mDataCallbackEnabled{false};
std::atomic<bool> mErrorCallbackCalled{false};
-
+ std::weak_ptr<AudioStream> mWeakThis;
};
/**
- * This struct is a stateless functor which closes a audiostream prior to its deletion.
+ * This struct is a stateless functor which closes an AudioStream prior to its deletion.
* This means it can be used to safely delete a smart pointer referring to an open stream.
*/
struct StreamDeleterFunctor {
diff --git a/include/oboe/AudioStreamBuilder.h b/include/oboe/AudioStreamBuilder.h
index b0f7de8e..636803a7 100644
--- a/include/oboe/AudioStreamBuilder.h
+++ b/include/oboe/AudioStreamBuilder.h
@@ -25,6 +25,7 @@ namespace oboe {
// This depends on AudioStream, so we use forward declaration, it will close and delete the stream
struct StreamDeleterFunctor;
using ManagedStream = std::unique_ptr<AudioStream, StreamDeleterFunctor>;
+
/**
* Factory class for an audio Stream.
*/
@@ -399,6 +400,8 @@ public:
*/
Result openManagedStream(ManagedStream &stream);
+ Result openSharedStream(std::shared_ptr<oboe::AudioStream> &sharedStream);
+
private:
/**