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

New namespace, constexprs and scoped enums #18

Merged
merged 11 commits into from
Dec 1, 2017
2 changes: 1 addition & 1 deletion include/oboe/Stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class Stream : public StreamBase {

bool isPlaying();

StreamCallback *getCallback() const { return mStreamCallback; }
std::shared_ptr<StreamCallback> getCallback() const { return mStreamCallback; }

int32_t getBytesPerFrame() const { return mChannelCount * getBytesPerSample(); }

Expand Down
25 changes: 13 additions & 12 deletions include/oboe/StreamBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef OBOE_STREAM_BASE_H_
#define OBOE_STREAM_BASE_H_

#include <memory>
#include "oboe/StreamCallback.h"
#include "oboe/Definitions.h"

Expand Down Expand Up @@ -89,22 +90,22 @@ class StreamBase {

int32_t getDeviceId() const { return mDeviceId; }

StreamCallback *getCallback() const {
std::shared_ptr<StreamCallback> getCallback() const {
return mStreamCallback;
}

protected:
StreamCallback *mStreamCallback = NULL;
int32_t mFramesPerCallback = kUnspecified;
int32_t mChannelCount = kUnspecified;
int32_t mSampleRate = kUnspecified;
int32_t mDeviceId = kUnspecified;
int32_t mBufferCapacityInFrames = kUnspecified;

SharingMode mSharingMode = SharingMode::Shared;
AudioFormat mFormat = AudioFormat::Unspecified;
Direction mDirection = Direction::Output;
PerformanceMode mPerformanceMode = PerformanceMode::None;
std::shared_ptr<StreamCallback> mStreamCallback;
int32_t mFramesPerCallback = kUnspecified;
int32_t mChannelCount = kUnspecified;
int32_t mSampleRate = kUnspecified;
int32_t mDeviceId = kUnspecified;
int32_t mBufferCapacityInFrames = kUnspecified;

SharingMode mSharingMode = SharingMode::Shared;
AudioFormat mFormat = AudioFormat::Unspecified;
Direction mDirection = Direction::Output;
PerformanceMode mPerformanceMode = PerformanceMode::None;
};

} // namespace oboe
Expand Down
18 changes: 9 additions & 9 deletions include/oboe/StreamBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class StreamBuilder : public StreamBase {
/**
* Request a specific number of channels.
*
* Default is OBOE_UNSPECIFIED. If the value is unspecified then
* Default is kUnspecified. If the value is unspecified then
* the application should query for the actual value after the stream is opened.
*/
StreamBuilder *setChannelCount(int channelCount) {
Expand All @@ -62,7 +62,7 @@ class StreamBuilder : public StreamBase {
}

/**
* Request the direction for a stream. The default is OBOE_DIRECTION_OUTPUT.
* Request the direction for a stream. The default is Direction::Output.
*
* @param direction Direction::Output or Direction::Input
*/
Expand All @@ -74,7 +74,7 @@ class StreamBuilder : public StreamBase {
/**
* Request a specific sample rate in Hz.
*
* Default is OBOE_UNSPECIFIED. If the value is unspecified then
* Default is kUnspecified. If the value is unspecified then
* the application should query for the actual value after the stream is opened.
*
* Technically, this should be called the "frame rate" or "frames per second",
Expand Down Expand Up @@ -121,9 +121,9 @@ class StreamBuilder : public StreamBase {
* Set the requested maximum buffer capacity in frames.
* The final stream capacity may differ, but will probably be at least this big.
*
* Default is OBOE_UNSPECIFIED.
* Default is kUnspecified.
*
* @param frames the desired buffer capacity in frames or OBOE_UNSPECIFIED
* @param frames the desired buffer capacity in frames or kUnspecified
* @return pointer to the builder so calls can be chained
*/
StreamBuilder *setBufferCapacityInFrames(int32_t bufferCapacityInFrames) {
Expand All @@ -136,7 +136,7 @@ class StreamBuilder : public StreamBase {
/**
* Normally you would leave this unspecified, and Oboe will chose the best API
* for the device at runtime.
* @param Must be API_UNSPECIFIED, API_OPENSL_ES or API_AAUDIO.
* @param Must be AudioApi::Unspecified, AudioApi::OpenSLES or AudioApi::AAudio.
* @return pointer to the builder so calls can be chained
*/
StreamBuilder *setApiIndex(AudioApi apiIndex) {
Expand Down Expand Up @@ -171,7 +171,7 @@ class StreamBuilder : public StreamBase {
* This will determine the latency, the power consumption, and the level of
* protection from glitches.
*
* @param performanceMode for example, OBOE_PERFORMANCE_MODE_LOW_LATENCY
* @param performanceMode for example, PerformanceMode::LowLatency
* @return pointer to the builder so calls can be chained
*/
StreamBuilder *setPerformanceMode(PerformanceMode performanceMode) {
Expand All @@ -185,7 +185,7 @@ class StreamBuilder : public StreamBase {
*
* By default, the primary device will be used.
*
* @param deviceId device identifier or OBOE_DEVICE_UNSPECIFIED
* @param deviceId device identifier or kUnspecified
* @return pointer to the builder so calls can be chained
*/
StreamBuilder *setDeviceId(int32_t deviceId) {
Expand All @@ -202,7 +202,7 @@ class StreamBuilder : public StreamBase {
* @param streamCallback
* @return
*/
StreamBuilder *setCallback(StreamCallback *streamCallback) {
StreamBuilder *setCallback(std::shared_ptr<StreamCallback> streamCallback) {
mStreamCallback = streamCallback;
return this;
}
Expand Down
10 changes: 2 additions & 8 deletions src/opensles/StreamBuffered.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace oboe {
StreamBuffered::StreamBuffered(const StreamBuilder &builder)
: Stream(builder)
, mFifoBuffer(NULL)
, mInternalCallback(NULL)
{
}

Expand All @@ -44,17 +43,12 @@ Result StreamBuffered::open() {
LOGD("StreamBuffered(): new FifoBuffer");
mFifoBuffer = new FifoBuffer(getBytesPerFrame(), 1024); // TODO size?
// Create a callback that reads from the FIFO
mInternalCallback = new AudioStreamBufferedCallback(this);
mStreamCallback = mInternalCallback;
LOGD("StreamBuffered(): mInternalCallback = %p", mInternalCallback);
mStreamCallback = std::make_shared<AudioStreamBufferedCallback>(this);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me nervous. Can this object be deleted properly if it holds a reference to itself?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is "this" actually an AudioStreamBufferedCallback?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"this" is the argument for AudioStreamBufferedCallback's constructor. Thus, the constructed callback will hold a raw pointer to the stream, while the stream will be holding a refcounted pointer to the callback.

Copy link
Collaborator Author

@dturner dturner Nov 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an AudioStreamBuffered. The call is equivalent to doing new AudioStreamBufferedCallback(this) except that instead of a raw pointer the result is a shared_ptr which will ensure deletion when the refcount drops to zero.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Thanks for the explanation.

LOGD("StreamBuffered(): mStreamCallback = %p", mStreamCallback.get());
}
return Result::OK;
}

StreamBuffered::~StreamBuffered() {
delete mInternalCallback;
}

// TODO: This method should return a tuple of Result,int32_t where the 2nd return param is the frames written
int32_t StreamBuffered::write(const void *buffer,
int32_t numFrames,
Expand Down
2 changes: 0 additions & 2 deletions src/opensles/StreamBuffered.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class StreamBuffered : public Stream {

StreamBuffered();
explicit StreamBuffered(const StreamBuilder &builder);
virtual ~StreamBuffered();

Result open() override;

Expand Down Expand Up @@ -71,7 +70,6 @@ class StreamBuffered : public Stream {
private:

FifoBuffer *mFifoBuffer;
AudioStreamBufferedCallback *mInternalCallback;
};

} // namespace oboe
Expand Down