# Oboe: include/oboe/AudioStream.h Source File

AudioStream.h
/*
* Copyright 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OBOE_STREAM_H_
#define OBOE_STREAM_H_
#include <atomic>
#include <cstdint>
#include <ctime>
#include <mutex>
#include "oboe/Definitions.h"
#include "oboe/ResultWithValue.h"
#include "oboe/AudioStreamBuilder.h"
#include "oboe/AudioStreamBase.h"
namespace oboe {
constexpr int64_t kDefaultTimeoutNanos = (2000 * kNanosPerMillisecond);
class AudioStream : public AudioStreamBase {
friend class AudioStreamBuilder; // allow access to setWeakThis() and lockWeakThis()
public:
AudioStream() {}
explicit AudioStream(const AudioStreamBuilder &builder);
virtual ~AudioStream() = default;
virtual Result open() {
return Result::OK; // Called by subclasses. Might do more in the future.
}
virtual Result close();
virtual Result start(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
virtual Result pause(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
virtual Result flush(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
virtual Result stop(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
/* Asynchronous requests.
* Use waitForStateChange() if you need to wait for completion.
*/
virtual Result requestStart() = 0;
virtual Result requestPause() = 0;
virtual Result requestFlush() = 0;
virtual Result requestStop() = 0;
virtual StreamState getState() const = 0;
virtual Result waitForStateChange(StreamState inputState,
StreamState *nextState,
int64_t timeoutNanoseconds) = 0;
virtual ResultWithValue<int32_t> setBufferSizeInFrames(int32_t /* requestedFrames */) {
return Result::ErrorUnimplemented;
}
virtual ResultWithValue<int32_t> getXRunCount() const {
return ResultWithValue<int32_t>(Result::ErrorUnimplemented);
}
virtual bool isXRunCountSupported() const = 0;
virtual int32_t getFramesPerBurst() = 0;
int32_t getBytesPerFrame() const { return mChannelCount * getBytesPerSample(); }
int32_t getBytesPerSample() const;
virtual int64_t getFramesWritten();
virtual int64_t getFramesRead();
virtual ResultWithValue<double> calculateLatencyMillis() {
return ResultWithValue<double>(Result::ErrorUnimplemented);
}
virtual Result getTimestamp(clockid_t /* clockId */,
int64_t* /* framePosition */,
int64_t* /* timeNanoseconds */) {
return Result::ErrorUnimplemented;
}
virtual ResultWithValue<FrameTimestamp> getTimestamp(clockid_t /* clockId */);
// ============== I/O ===========================
virtual ResultWithValue<int32_t> write(const void* /* buffer */,
int32_t /* numFrames */,
int64_t /* timeoutNanoseconds */ ) {
return ResultWithValue<int32_t>(Result::ErrorUnimplemented);
}
virtual ResultWithValue<int32_t> read(void* /* buffer */,
int32_t /* numFrames */,
int64_t /* timeoutNanoseconds */) {
return ResultWithValue<int32_t>(Result::ErrorUnimplemented);
}
virtual AudioApi getAudioApi() const = 0;
bool usesAAudio() const {
return getAudioApi() == AudioApi::AAudio;
}
virtual void *getUnderlyingStream() const {
return nullptr;
}
void launchStopThread();
virtual void updateFramesWritten() = 0;
virtual void updateFramesRead() = 0;
/*
* Swap old callback for new callback.
* This not atomic.
* This should only be used internally.
* @param dataCallback
* @return previous dataCallback
*/
AudioStreamDataCallback *swapDataCallback(AudioStreamDataCallback *dataCallback) {
AudioStreamDataCallback *previousCallback = mDataCallback;
mDataCallback = dataCallback;
return previousCallback;
}
/*
* Swap old callback for new callback.
* This not atomic.
* This should only be used internally.
* @param errorCallback
* @return previous errorCallback
*/
AudioStreamErrorCallback *swapErrorCallback(AudioStreamErrorCallback *errorCallback) {
AudioStreamErrorCallback *previousCallback = mErrorCallback;
mErrorCallback = errorCallback;
return previousCallback;
}
ResultWithValue<int32_t> getAvailableFrames();
ResultWithValue<int32_t> waitForAvailableFrames(int32_t numFrames,
int64_t timeoutNanoseconds);
virtual oboe::Result getLastErrorCallbackResult() const {
return mErrorCallbackResult;
}
protected:
bool wasErrorCallbackCalled() {
return mErrorCallbackCalled.exchange(true);
}
virtual Result waitForStateTransition(StreamState startingState,
StreamState endingState,
int64_t timeoutNanoseconds);
virtual DataCallbackResult onDefaultCallback(void* /* audioData */, int /* numFrames */) {
return DataCallbackResult::Stop;
}
DataCallbackResult fireDataCallback(void *audioData, int numFrames);
bool isDataCallbackEnabled() {
return mDataCallbackEnabled;
}
void setDataCallbackEnabled(bool enabled) {
mDataCallbackEnabled = enabled;
}
/*
* 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();
}
std::weak_ptr<AudioStream> mWeakThis; // weak pointer to this object
std::atomic<int64_t> mFramesWritten{};
std::atomic<int64_t> mFramesRead{};
std::mutex mLock; // for synchronizing start/stop/close
oboe::Result mErrorCallbackResult = oboe::Result::OK;
private:
// Log the scheduler if it changes.
void checkScheduler();
int mPreviousScheduler = -1;
std::atomic<bool> mDataCallbackEnabled{false};
std::atomic<bool> mErrorCallbackCalled{false};
};
struct StreamDeleterFunctor {
void operator()(AudioStream *audioStream) {
if (audioStream) {
audioStream->close();
}
delete audioStream;
}
};
} // namespace oboe
#endif /* OBOE_STREAM_H_ */
oboe::AudioStream::onDefaultCallback
virtual DataCallbackResult onDefaultCallback(void *, int)
Definition: AudioStream.h:474
oboe::AudioStream::stop
virtual Result stop(int64_t timeoutNanoseconds=kDefaultTimeoutNanos)
oboe::AudioApi::AAudio
oboe::AudioStreamBuilder
Definition: AudioStreamBuilder.h:33
oboe::AudioStream::getAvailableFrames
ResultWithValue< int32_t > getAvailableFrames()
oboe::AudioStream::setDataCallbackEnabled
void setDataCallbackEnabled(bool enabled)
Definition: AudioStream.h:499
oboe::AudioStream::usesAAudio
bool usesAAudio() const
Definition: AudioStream.h:359
oboe::AudioStream::flush
virtual Result flush(int64_t timeoutNanoseconds=kDefaultTimeoutNanos)
oboe::AudioStreamBase::mDataCallback
AudioStreamDataCallback * mDataCallback
Definition: AudioStreamBase.h:182
oboe::AudioStream::getBytesPerFrame
int32_t getBytesPerFrame() const
Definition: AudioStream.h:217
oboe::AudioStream::start
virtual Result start(int64_t timeoutNanoseconds=kDefaultTimeoutNanos)
oboe::kDefaultTimeoutNanos
constexpr int64_t kDefaultTimeoutNanos
Definition: AudioStream.h:39
oboe::AudioStream::getXRunCount
virtual ResultWithValue< int32_t > getXRunCount() const
Definition: AudioStream.h:194
oboe::AudioStream::mFramesWritten
std::atomic< int64_t > mFramesWritten
Definition: AudioStream.h:526
oboe::AudioStream::fireDataCallback
DataCallbackResult fireDataCallback(void *audioData, int numFrames)
oboe::AudioStream::getUnderlyingStream
virtual void * getUnderlyingStream() const
Definition: AudioStream.h:371
oboe::AudioStream::updateFramesWritten
virtual void updateFramesWritten()=0
oboe::AudioStream::getFramesWritten
virtual int64_t getFramesWritten()
oboe::AudioStream::waitForAvailableFrames
ResultWithValue< int32_t > waitForAvailableFrames(int32_t numFrames, int64_t timeoutNanoseconds)
oboe::AudioStreamDataCallback
Definition: AudioStreamCallback.h:34
oboe::AudioStreamBase::mChannelCount
int32_t mChannelCount
Definition: AudioStreamBase.h:190
oboe::DataCallbackResult
DataCallbackResult
Definition: Definitions.h:119
oboe::AudioApi
AudioApi
Definition: Definitions.h:213
oboe::AudioStream::waitForStateTransition
virtual Result waitForStateTransition(StreamState startingState, StreamState endingState, int64_t timeoutNanoseconds)
oboe::AudioStream::getFramesPerBurst
virtual int32_t getFramesPerBurst()=0
oboe::AudioStream::close
virtual Result close()
oboe::AudioStream::setBufferSizeInFrames
virtual ResultWithValue< int32_t > setBufferSizeInFrames(int32_t)
Definition: AudioStream.h:178
oboe::AudioStream::calculateLatencyMillis
virtual ResultWithValue< double > calculateLatencyMillis()
Definition: AudioStream.h:266
oboe::AudioStream::getTimestamp
virtual Result getTimestamp(clockid_t, int64_t *, int64_t *)
Definition: AudioStream.h:287
oboe::AudioStream::open
virtual Result open()
Definition: AudioStream.h:67
oboe::AudioStream::getFramesRead
virtual int64_t getFramesRead()
oboe::AudioStream
Definition: AudioStream.h:44
oboe::AudioStream::isDataCallbackEnabled
bool isDataCallbackEnabled()
Definition: AudioStream.h:491
oboe::AudioStream::read
virtual ResultWithValue< int32_t > read(void *, int32_t, int64_t)
Definition: AudioStream.h:341
oboe::AudioStream::requestPause
virtual Result requestPause()=0
oboe::AudioStream::write
virtual ResultWithValue< int32_t > write(const void *, int32_t, int64_t)
Definition: AudioStream.h:323
oboe::AudioStream::mFramesRead
std::atomic< int64_t > mFramesRead
Definition: AudioStream.h:534
oboe::AudioStreamBase
Definition: AudioStreamBase.h:29
oboe::kNanosPerMillisecond
constexpr int64_t kNanosPerMillisecond
Definition: Definitions.h:43
oboe::AudioStream::requestStop
virtual Result requestStop()=0
oboe::AudioStream::getAudioApi
virtual AudioApi getAudioApi() const =0
oboe::AudioStream::launchStopThread
void launchStopThread()
oboe::AudioStream::waitForStateChange
virtual Result waitForStateChange(StreamState inputState, StreamState *nextState, int64_t timeoutNanoseconds)=0
oboe::Result
Result
Definition: Definitions.h:131
oboe::StreamDeleterFunctor
Definition: AudioStream.h:555
oboe
Definition: AudioStream.h:31
oboe::AudioStream::wasErrorCallbackCalled
bool wasErrorCallbackCalled()
Definition: AudioStream.h:453
oboe::AudioStream::isXRunCountSupported
virtual bool isXRunCountSupported() const =0
oboe::AudioStream::requestStart
virtual Result requestStart()=0
oboe::AudioStream::getLastErrorCallbackResult
virtual oboe::Result getLastErrorCallbackResult() const
Definition: AudioStream.h:438
oboe::StreamState
StreamState
Definition: Definitions.h:58
oboe::AudioStream::getState
virtual StreamState getState() const =0
oboe::AudioStream::pause
virtual Result pause(int64_t timeoutNanoseconds=kDefaultTimeoutNanos)
oboe::AudioStream::requestFlush
virtual Result requestFlush()=0
oboe::AudioStream::updateFramesRead
virtual void updateFramesRead()=0
oboe::AudioStreamBase::mErrorCallback
AudioStreamErrorCallback * mErrorCallback
Definition: AudioStreamBase.h:185
oboe::AudioStream::getBytesPerSample
int32_t getBytesPerSample() const
oboe::ResultWithValue
Definition: ResultWithValue.h:47