Oboe
A library for creating real-time audio apps on Android
Loading...
Searching...
No Matches
AudioStream.h
1/*
2 * Copyright 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef OBOE_STREAM_H_
18#define OBOE_STREAM_H_
19
20#include <atomic>
21#include <cstdint>
22#include <ctime>
23#include <mutex>
24#include "oboe/Definitions.h"
25#include "oboe/ResultWithValue.h"
26#include "oboe/AudioStreamBuilder.h"
27#include "oboe/AudioStreamBase.h"
28
29namespace oboe {
30
37constexpr int64_t kDefaultTimeoutNanos = (2000 * kNanosPerMillisecond);
38
43 friend class AudioStreamBuilder; // allow access to setWeakThis() and lockWeakThis()
44public:
45
46 AudioStream() {}
47
53 explicit AudioStream(const AudioStreamBuilder &builder);
54
55 virtual ~AudioStream();
56
65 virtual Result open() {
66 return Result::OK; // Called by subclasses. Might do more in the future.
67 }
68
88 virtual Result release() {
89 return Result::ErrorUnimplemented;
90 }
91
95 virtual Result close();
96
101 virtual Result start(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
102
107 virtual Result pause(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
108
113 virtual Result flush(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
114
119 virtual Result stop(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
120
121 /* Asynchronous requests.
122 * Use waitForStateChange() if you need to wait for completion.
123 */
124
129 virtual Result requestStart() = 0;
130
135 virtual Result requestPause() = 0;
136
141 virtual Result requestFlush() = 0;
142
147 virtual Result requestStop() = 0;
148
154 virtual StreamState getState() = 0;
155
183 virtual Result waitForStateChange(StreamState inputState,
184 StreamState *nextState,
185 int64_t timeoutNanoseconds) = 0;
186
206 virtual ResultWithValue<int32_t> setBufferSizeInFrames(int32_t /* requestedFrames */) {
207 return Result::ErrorUnimplemented;
208 }
209
223 return ResultWithValue<int32_t>(Result::ErrorUnimplemented);
224 }
225
229 virtual bool isXRunCountSupported() const = 0;
230
237 return mFramesPerBurst;
238 }
239
248
256
264
272
301 return ResultWithValue<double>(Result::ErrorUnimplemented);
302 }
303
325 virtual Result getTimestamp(clockid_t /* clockId */,
326 int64_t* /* framePosition */,
327 int64_t* /* timeNanoseconds */) {
328 return Result::ErrorUnimplemented;
329 }
330
352
353 // ============== I/O ===========================
366 virtual ResultWithValue<int32_t> write(const void* /* buffer */,
367 int32_t /* numFrames */,
368 int64_t /* timeoutNanoseconds */ ) {
369 return ResultWithValue<int32_t>(Result::ErrorUnimplemented);
370 }
371
384 virtual ResultWithValue<int32_t> read(void* /* buffer */,
385 int32_t /* numFrames */,
386 int64_t /* timeoutNanoseconds */) {
387 return ResultWithValue<int32_t>(Result::ErrorUnimplemented);
388 }
389
395 virtual AudioApi getAudioApi() const = 0;
396
402 bool usesAAudio() const {
403 return getAudioApi() == AudioApi::AAudio;
404 }
405
414 virtual void *getUnderlyingStream() const {
415 return nullptr;
416 }
417
422 virtual void updateFramesWritten() = 0;
423
428 virtual void updateFramesRead() = 0;
429
430 /*
431 * Swap old callback for new callback.
432 * This not atomic.
433 * This should only be used internally.
434 * @param dataCallback
435 * @return previous dataCallback
436 */
440 return previousCallback;
441 }
442
443 /*
444 * Swap old callback for new callback.
445 * This not atomic.
446 * This should only be used internally.
447 * @param errorCallback
448 * @return previous errorCallback
449 */
453 return previousCallback;
454 }
455
460
477
481 virtual oboe::Result getLastErrorCallbackResult() const {
482 return mErrorCallbackResult;
483 }
484
485
486 int32_t getDelayBeforeCloseMillis() const {
487 return mDelayBeforeCloseMillis;
488 }
489
507
532 mPerformanceHintEnabled = enabled;
533 }
534
542 return mPerformanceHintEnabled;
543 }
544
571 virtual oboe::Result reportWorkload(int32_t appWorkload) {
572 return oboe::Result::ErrorUnimplemented;
573 }
574
575protected:
576
587 return mErrorCallbackCalled.exchange(true);
588 }
589
596 virtual Result waitForStateTransition(StreamState startingState,
597 StreamState endingState,
599
607 virtual DataCallbackResult onDefaultCallback(void* /* audioData */, int /* numFrames */) {
608 return DataCallbackResult::Stop;
609 }
610
619 DataCallbackResult fireDataCallback(void *audioData, int numFrames);
620
625 return mDataCallbackEnabled;
626 }
627
633 mDataCallbackEnabled = enabled;
634 }
635
641
646 if (mDelayBeforeCloseMillis > 0) {
647 usleep(mDelayBeforeCloseMillis * 1000);
648 }
649 }
650
655
660 virtual void endPerformanceHintInCallback(int32_t /*numFrames*/) {}
661
665 virtual void closePerformanceHint() {}
666
667 /*
668 * Set a weak_ptr to this stream from the shared_ptr so that we can
669 * later use a shared_ptr in the error callback.
670 */
671 void setWeakThis(std::shared_ptr<oboe::AudioStream> &sharedStream) {
672 mWeakThis = sharedStream;
673 }
674
675 /*
676 * Make a shared_ptr that will prevent this stream from being deleted.
677 */
678 std::shared_ptr<oboe::AudioStream> lockWeakThis() {
679 return mWeakThis.lock();
680 }
681
682 std::weak_ptr<AudioStream> mWeakThis; // weak pointer to this object
683
690 std::atomic<int64_t> mFramesWritten{};
691
698 std::atomic<int64_t> mFramesRead{};
699
700 std::mutex mLock; // for synchronizing start/stop/close
701
702 oboe::Result mErrorCallbackResult = oboe::Result::OK;
703
708 int32_t mFramesPerBurst = kUnspecified;
709
710 // Time to sleep in order to prevent a race condition with a callback after a close().
711 // Two milliseconds may be enough but 10 msec is even safer.
712 static constexpr int kMinDelayBeforeCloseMillis = 10;
713 int32_t mDelayBeforeCloseMillis = kMinDelayBeforeCloseMillis;
714
715private:
716
717 // Log the scheduler if it changes.
718 void checkScheduler();
719 int mPreviousScheduler = -1;
720
721 std::atomic<bool> mDataCallbackEnabled{false};
722 std::atomic<bool> mErrorCallbackCalled{false};
723
724 std::atomic<bool> mPerformanceHintEnabled{false}; // set only by app
725};
726
732 void operator()(AudioStream *audioStream) {
733 if (audioStream) {
734 audioStream->close();
735 }
736 delete audioStream;
737 }
738 };
739} // namespace oboe
740
741#endif /* OBOE_STREAM_H_ */
Definition AudioStreamBase.h:30
int32_t mChannelCount
Definition AudioStreamBase.h:250
AudioStreamDataCallback * mDataCallback
Definition AudioStreamBase.h:240
AudioStreamErrorCallback * mErrorCallback
Definition AudioStreamBase.h:244
Definition AudioStreamBuilder.h:34
Definition AudioStreamCallback.h:34
Definition AudioStreamCallback.h:102
Definition AudioStream.h:42
int32_t mFramesPerBurst
Definition AudioStream.h:708
virtual Result pause(int64_t timeoutNanoseconds=kDefaultTimeoutNanos)
std::atomic< int64_t > mFramesRead
Definition AudioStream.h:698
virtual Result waitForStateChange(StreamState inputState, StreamState *nextState, int64_t timeoutNanoseconds)=0
virtual DataCallbackResult onDefaultCallback(void *, int)
Definition AudioStream.h:607
void setDataCallbackEnabled(bool enabled)
Definition AudioStream.h:632
bool usesAAudio() const
Definition AudioStream.h:402
virtual AudioApi getAudioApi() const =0
int32_t getFramesPerBurst() const
Definition AudioStream.h:236
virtual Result flush(int64_t timeoutNanoseconds=kDefaultTimeoutNanos)
virtual Result requestStart()=0
virtual ResultWithValue< double > calculateLatencyMillis()
Definition AudioStream.h:300
virtual ResultWithValue< int32_t > read(void *, int32_t, int64_t)
Definition AudioStream.h:384
virtual bool isXRunCountSupported() const =0
int32_t getBytesPerSample() const
virtual ResultWithValue< int32_t > getXRunCount()
Definition AudioStream.h:222
virtual void updateFramesRead()=0
virtual StreamState getState()=0
virtual Result release()
Definition AudioStream.h:88
virtual void closePerformanceHint()
Definition AudioStream.h:665
int32_t getBytesPerFrame() const
Definition AudioStream.h:247
void sleepBeforeClose()
Definition AudioStream.h:645
virtual void updateFramesWritten()=0
virtual Result open()
Definition AudioStream.h:65
virtual void beginPerformanceHintInCallback()
Definition AudioStream.h:654
virtual Result requestFlush()=0
virtual void endPerformanceHintInCallback(int32_t)
Definition AudioStream.h:660
virtual Result requestPause()=0
virtual Result requestStop()=0
std::atomic< int64_t > mFramesWritten
Definition AudioStream.h:690
virtual Result waitForStateTransition(StreamState startingState, StreamState endingState, int64_t timeoutNanoseconds)
virtual void * getUnderlyingStream() const
Definition AudioStream.h:414
AudioStream(const AudioStreamBuilder &builder)
virtual oboe::Result getLastErrorCallbackResult() const
Definition AudioStream.h:481
virtual Result close()
virtual oboe::Result reportWorkload(int32_t appWorkload)
Definition AudioStream.h:571
bool isPerformanceHintEnabled()
Definition AudioStream.h:541
bool wasErrorCallbackCalled()
Definition AudioStream.h:586
ResultWithValue< int32_t > getAvailableFrames()
virtual ResultWithValue< int32_t > setBufferSizeInFrames(int32_t)
Definition AudioStream.h:206
virtual int64_t getFramesWritten()
DataCallbackResult fireDataCallback(void *audioData, int numFrames)
ResultWithValue< int32_t > waitForAvailableFrames(int32_t numFrames, int64_t timeoutNanoseconds)
virtual Result getTimestamp(clockid_t, int64_t *, int64_t *)
Definition AudioStream.h:325
virtual ResultWithValue< int32_t > write(const void *, int32_t, int64_t)
Definition AudioStream.h:366
void setPerformanceHintEnabled(bool enabled)
Definition AudioStream.h:531
bool isDataCallbackEnabled()
Definition AudioStream.h:624
virtual Result stop(int64_t timeoutNanoseconds=kDefaultTimeoutNanos)
virtual int64_t getFramesRead()
void calculateDefaultDelayBeforeCloseMillis()
virtual Result start(int64_t timeoutNanoseconds=kDefaultTimeoutNanos)
void setDelayBeforeCloseMillis(int32_t delayBeforeCloseMillis)
Definition AudioStream.h:504
virtual ResultWithValue< FrameTimestamp > getTimestamp(clockid_t)
Definition ResultWithValue.h:47
Definition AudioStream.h:731