From 68f7df0d535e8d282cd7bc5feeca9ac5a77fc081 Mon Sep 17 00:00:00 2001 From: Phil Burk Date: Wed, 15 Apr 2020 13:49:11 -0700 Subject: oboe: add openSharedStream To prevent race conditions with the onError callbacks that were resulting in the use of a deleted stream. For bug #820 --- include/oboe/AudioStream.h | 21 +++++++++++++++++++-- include/oboe/AudioStreamBuilder.h | 3 +++ 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'include') 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 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 &sharedStream) { + mWeakThis = sharedStream; + } + + /* + * Make a shared_ptr that will prevent this stream from being deleted. + */ + std::shared_ptr lockWeakThis() { + return mWeakThis.lock(); + } + protected: /** @@ -497,17 +513,18 @@ protected: std::mutex mLock; // for synchronizing start/stop/close + private: int mPreviousScheduler = -1; std::atomic mDataCallbackEnabled{false}; std::atomic mErrorCallbackCalled{false}; - + std::weak_ptr 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; + /** * Factory class for an audio Stream. */ @@ -399,6 +400,8 @@ public: */ Result openManagedStream(ManagedStream &stream); + Result openSharedStream(std::shared_ptr &sharedStream); + private: /** -- cgit v1.2.3