Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FullDuplexStream: deprecate raw pointers when setting streams #2034

Merged
merged 6 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions apps/OboeTester/app/src/main/cpp/NativeAudioContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ int ActivityContext::open(jint nativeApi,

createRecording();

finishOpen(isInput, oboeStream.get());
finishOpen(isInput, oboeStream);
}

if (!mUseCallback) {
Expand Down Expand Up @@ -652,7 +652,7 @@ void ActivityEcho::configureBuilder(bool isInput, oboe::AudioStreamBuilder &buil
}
}

void ActivityEcho::finishOpen(bool isInput, oboe::AudioStream *oboeStream) {
void ActivityEcho::finishOpen(bool isInput, std::shared_ptr<oboe::AudioStream> &oboeStream) {
if (isInput) {
mFullDuplexEcho->setInputStream(oboeStream);
} else {
Expand All @@ -674,7 +674,8 @@ void ActivityRoundTripLatency::configureBuilder(bool isInput, oboe::AudioStreamB
}
}

void ActivityRoundTripLatency::finishOpen(bool isInput, AudioStream *oboeStream) {
void ActivityRoundTripLatency::finishOpen(bool isInput, std::shared_ptr<oboe::AudioStream>
&oboeStream) {
if (isInput) {
mFullDuplexLatency->setInputStream(oboeStream);
mFullDuplexLatency->setRecording(mRecording.get());
Expand Down Expand Up @@ -729,7 +730,7 @@ void ActivityGlitches::configureBuilder(bool isInput, oboe::AudioStreamBuilder &
}
}

void ActivityGlitches::finishOpen(bool isInput, oboe::AudioStream *oboeStream) {
void ActivityGlitches::finishOpen(bool isInput, std::shared_ptr<oboe::AudioStream> &oboeStream) {
if (isInput) {
mFullDuplexGlitches->setInputStream(oboeStream);
mFullDuplexGlitches->setRecording(mRecording.get());
Expand All @@ -752,7 +753,7 @@ void ActivityDataPath::configureBuilder(bool isInput, oboe::AudioStreamBuilder &
}
}

void ActivityDataPath::finishOpen(bool isInput, oboe::AudioStream *oboeStream) {
void ActivityDataPath::finishOpen(bool isInput, std::shared_ptr<oboe::AudioStream> &oboeStream) {
if (isInput) {
mFullDuplexDataPath->setInputStream(oboeStream);
mFullDuplexDataPath->setRecording(mRecording.get());
Expand Down
10 changes: 5 additions & 5 deletions apps/OboeTester/app/src/main/cpp/NativeAudioContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class ActivityContext {
SECONDS_TO_RECORD * mSampleRate);
}

virtual void finishOpen(bool isInput, oboe::AudioStream *oboeStream) {}
virtual void finishOpen(bool isInput, std::shared_ptr<oboe::AudioStream> &oboeStream) {}

virtual oboe::Result startStreams() = 0;

Expand Down Expand Up @@ -544,7 +544,7 @@ class ActivityEcho : public ActivityFullDuplex {
}

protected:
void finishOpen(bool isInput, oboe::AudioStream *oboeStream) override;
void finishOpen(bool isInput, std::shared_ptr<oboe::AudioStream> &oboeStream) override;

private:
std::unique_ptr<FullDuplexEcho> mFullDuplexEcho{};
Expand Down Expand Up @@ -616,7 +616,7 @@ class ActivityRoundTripLatency : public ActivityFullDuplex {
jdouble measureTimestampLatency();

protected:
void finishOpen(bool isInput, oboe::AudioStream *oboeStream) override;
void finishOpen(bool isInput, std::shared_ptr<oboe::AudioStream> &oboeStream) override;

private:
std::unique_ptr<FullDuplexAnalyzer> mFullDuplexLatency{};
Expand Down Expand Up @@ -658,7 +658,7 @@ class ActivityGlitches : public ActivityFullDuplex {
}

protected:
void finishOpen(bool isInput, oboe::AudioStream *oboeStream) override;
void finishOpen(bool isInput, std::shared_ptr<oboe::AudioStream> &oboeStream) override;

private:
std::unique_ptr<FullDuplexAnalyzer> mFullDuplexGlitches{};
Expand Down Expand Up @@ -700,7 +700,7 @@ class ActivityDataPath : public ActivityFullDuplex {
}

protected:
void finishOpen(bool isInput, oboe::AudioStream *oboeStream) override;
void finishOpen(bool isInput, std::shared_ptr<oboe::AudioStream> &oboeStream) override;

private:
std::unique_ptr<FullDuplexAnalyzer> mFullDuplexDataPath{};
Expand Down
46 changes: 40 additions & 6 deletions include/oboe/FullDuplexStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,39 +53,73 @@ class FullDuplexStream : public AudioStreamDataCallback {
virtual ~FullDuplexStream() = default;

/**
* Sets the input stream. Calling this is mandatory.
* Sets the input stream.
*
* @deprecated Call setInputStream(std::shared_ptr<AudioStream> &stream) instead.
* @param stream the output stream
*/
void setInputStream(AudioStream *stream) {
mInputStream = std::shared_ptr<AudioStream>(stream);
robertwu1 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Sets the input stream. Calling this is mandatory.
*
* @param stream the output stream
*/
void setInputStream(std::shared_ptr<AudioStream> &stream) {
mInputStream = stream;
}

/**
* Releases the shared pointer of the input stream.
*/
void releaseInputStream() {
mInputStream = nullptr;
robertwu1 marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Gets the input stream
*
* @return the input stream
*/
AudioStream *getInputStream() {
robertwu1 marked this conversation as resolved.
Show resolved Hide resolved
return mInputStream;
return mInputStream.get();
}

/**
* Sets the output stream. Calling this is mandatory.
* Sets the output stream.
*
* @deprecated Call setOutputStream(std::shared_ptr<AudioStream> &stream) instead.
* @param stream the output stream
*/
void setOutputStream(AudioStream *stream) {
mOutputStream = std::shared_ptr<AudioStream>(stream);
}

/**
* Sets the output stream. Calling this is mandatory.
*
* @param stream the output stream
*/
void setOutputStream(std::shared_ptr<AudioStream> &stream) {
mOutputStream = stream;
}

/**
* Releases the shared pointer of the output stream.
*/
void releaseOutputStream() {
mOutputStream = nullptr;
}

/**
* Gets the output stream
*
* @return the output stream
*/
AudioStream *getOutputStream() {
return mOutputStream;
return mOutputStream.get();
}

/**
Expand Down Expand Up @@ -312,8 +346,8 @@ class FullDuplexStream : public AudioStreamDataCallback {
// Discard some callbacks so the input and output reach equilibrium.
int32_t mCountCallbacksToDiscard = kNumCallbacksToDiscard;

AudioStream *mInputStream = nullptr;
AudioStream *mOutputStream = nullptr;
std::shared_ptr<AudioStream> mInputStream = nullptr;
std::shared_ptr<AudioStream> mOutputStream = nullptr;

int32_t mBufferSize = 0;
std::unique_ptr<float[]> mInputBuffer;
Expand Down
12 changes: 6 additions & 6 deletions samples/LiveEffect/src/main/cpp/LiveEffectEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ void LiveEffectEngine::closeStreams() {
* null.
*/
closeStream(mPlayStream);
mFullDuplexPass.setOutputStream(nullptr);
mFullDuplexPass.releaseOutputStream();

closeStream(mRecordingStream);
mFullDuplexPass.setInputStream(nullptr);
mFullDuplexPass.releaseInputStream();
}

oboe::Result LiveEffectEngine::openStreams() {
Expand Down Expand Up @@ -102,8 +102,8 @@ oboe::Result LiveEffectEngine::openStreams() {
}
warnIfNotLowLatency(mRecordingStream);

mFullDuplexPass.setInputStream(mRecordingStream.get());
mFullDuplexPass.setOutputStream(mPlayStream.get());
mFullDuplexPass.setInputStream(mRecordingStream);
mFullDuplexPass.setOutputStream(mPlayStream);
return result;
}

Expand Down Expand Up @@ -239,9 +239,9 @@ void LiveEffectEngine::onErrorAfterClose(oboe::AudioStream *oboeStream,
// Stop the Full Duplex stream.
// Since the error callback occurs only for the output stream, close the input stream.
mFullDuplexPass.stop();
mFullDuplexPass.setOutputStream(nullptr);
mFullDuplexPass.releaseOutputStream();
closeStream(mRecordingStream);
mFullDuplexPass.setInputStream(nullptr);
mFullDuplexPass.releaseInputStream();

// Restart the stream if the error is a disconnect.
if (error == oboe::Result::ErrorDisconnected) {
Expand Down
12 changes: 6 additions & 6 deletions tests/testFullDuplexStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class TestFullDuplexStream : public ::testing::Test,
mOutputBuilder.setFormat(AudioFormat::Float);
mOutputBuilder.setDataCallback(this);

Result r = mOutputBuilder.openStream(&mOutputStream);
Result r = mOutputBuilder.openStream(mOutputStream);
ASSERT_EQ(r, Result::OK) << "Failed to open output stream " << convertToText(r);

mInputBuilder.setDirection(Direction::Input);
Expand All @@ -68,7 +68,7 @@ class TestFullDuplexStream : public ::testing::Test,
mInputBuilder.setBufferCapacityInFrames(mOutputStream->getBufferCapacityInFrames() * 2);
mInputBuilder.setSampleRate(mOutputStream->getSampleRate());

r = mInputBuilder.openStream(&mInputStream);
r = mInputBuilder.openStream(mInputStream);
ASSERT_EQ(r, Result::OK) << "Failed to open input stream " << convertToText(r);

setInputStream(mInputStream);
Expand All @@ -88,10 +88,10 @@ class TestFullDuplexStream : public ::testing::Test,
void closeStream() {
Result r = mOutputStream->close();
ASSERT_EQ(r, Result::OK) << "Failed to close output stream " << convertToText(r);
setOutputStream(nullptr);
releaseOutputStream();
r = mInputStream->close();
ASSERT_EQ(r, Result::OK) << "Failed to close input stream " << convertToText(r);
setInputStream(nullptr);
releaseInputStream();
}

void checkXRuns() {
Expand All @@ -107,8 +107,8 @@ class TestFullDuplexStream : public ::testing::Test,

AudioStreamBuilder mInputBuilder;
AudioStreamBuilder mOutputBuilder;
AudioStream *mInputStream = nullptr;
AudioStream *mOutputStream = nullptr;
std::shared_ptr<AudioStream> mInputStream = nullptr;
std::shared_ptr<AudioStream> mOutputStream = nullptr;
std::atomic<int32_t> mCallbackCount{0};
std::atomic<int32_t> mGoodCallbackCount{0};
};
Expand Down