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
31namespace oboe {
32
39constexpr int64_t kDefaultTimeoutNanos = (2000 * kNanosPerMillisecond);
40
45 friend class AudioStreamBuilder; // allow access to setWeakThis() and lockWeakThis()
46public:
47
48 AudioStream() {}
49
55 explicit AudioStream(const AudioStreamBuilder &builder);
56
57 virtual ~AudioStream();
58
67 virtual Result open() {
68 return Result::OK; // Called by subclasses. Might do more in the future.
69 }
70
90 virtual Result release() {
91 return Result::ErrorUnimplemented;
92 }
93
97 virtual Result close();
98
103 virtual Result start(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
104
109 virtual Result pause(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
110
115 virtual Result flush(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
116
121 virtual Result stop(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
122
123 /* Asynchronous requests.
124 * Use waitForStateChange() if you need to wait for completion.
125 */
126
131 virtual Result requestStart() = 0;
132
137 virtual Result requestPause() = 0;
138
143 virtual Result requestFlush() = 0;
144
149 virtual Result requestStop() = 0;
150
156 virtual StreamState getState() = 0;
157
186 StreamState *nextState,
187 int64_t timeoutNanoseconds) = 0;
188
208 virtual ResultWithValue<int32_t> setBufferSizeInFrames(int32_t /* requestedFrames */) {
209 return Result::ErrorUnimplemented;
210 }
211
225 return ResultWithValue<int32_t>(Result::ErrorUnimplemented);
226 }
227
231 virtual bool isXRunCountSupported() const = 0;
232
239 return mFramesPerBurst;
240 }
241
250
258
266
274
303 return ResultWithValue<double>(Result::ErrorUnimplemented);
304 }
305
327 virtual Result getTimestamp(clockid_t /* clockId */,
328 int64_t* /* framePosition */,
329 int64_t* /* timeNanoseconds */) {
330 return Result::ErrorUnimplemented;
331 }
332
354
355 // ============== I/O ===========================
368 virtual ResultWithValue<int32_t> write(const void* /* buffer */,
369 int32_t /* numFrames */,
370 int64_t /* timeoutNanoseconds */ ) {
371 return ResultWithValue<int32_t>(Result::ErrorUnimplemented);
372 }
373
386 virtual ResultWithValue<int32_t> read(void* /* buffer */,
387 int32_t /* numFrames */,
388 int64_t /* timeoutNanoseconds */) {
389 return ResultWithValue<int32_t>(Result::ErrorUnimplemented);
390 }
391
397 virtual AudioApi getAudioApi() const = 0;
398
404 bool usesAAudio() const {
405 return getAudioApi() == AudioApi::AAudio;
406 }
407
416 virtual void *getUnderlyingStream() const {
417 return nullptr;
418 }
419
424 virtual void updateFramesWritten() = 0;
425
430 virtual void updateFramesRead() = 0;
431
432 /*
433 * Swap old callback for new callback.
434 * This not atomic.
435 * This should only be used internally.
436 * @param dataCallback
437 * @return previous dataCallback
438 */
442 return previousCallback;
443 }
444
445 /*
446 * Swap old callback for new callback.
447 * This not atomic.
448 * This should only be used internally.
449 * @param errorCallback
450 * @return previous errorCallback
451 */
455 return previousCallback;
456 }
457
462
479
484 return mErrorCallbackResult;
485 }
486
487
488 int32_t getDelayBeforeCloseMillis() const {
489 return mDelayBeforeCloseMillis;
490 }
491
509
534 mPerformanceHintEnabled = enabled;
535 }
536
544 return mPerformanceHintEnabled;
545 }
546
547protected:
548
559 return mErrorCallbackCalled.exchange(true);
560 }
561
571
579 virtual DataCallbackResult onDefaultCallback(void* /* audioData */, int /* numFrames */) {
580 return DataCallbackResult::Stop;
581 }
582
592
597 return mDataCallbackEnabled;
598 }
599
605 mDataCallbackEnabled = enabled;
606 }
607
613
618 if (mDelayBeforeCloseMillis > 0) {
619 usleep(mDelayBeforeCloseMillis * 1000);
620 }
621 }
622
627
632 virtual void endPerformanceHintInCallback(int32_t /*numFrames*/) {}
633
637 virtual void closePerformanceHint() {}
638
639 /*
640 * Set a weak_ptr to this stream from the shared_ptr so that we can
641 * later use a shared_ptr in the error callback.
642 */
643 void setWeakThis(std::shared_ptr<oboe::AudioStream> &sharedStream) {
644 mWeakThis = sharedStream;
645 }
646
647 /*
648 * Make a shared_ptr that will prevent this stream from being deleted.
649 */
650 std::shared_ptr<oboe::AudioStream> lockWeakThis() {
651 return mWeakThis.lock();
652 }
653
654 std::weak_ptr<AudioStream> mWeakThis; // weak pointer to this object
655
662 std::atomic<int64_t> mFramesWritten{};
663
670 std::atomic<int64_t> mFramesRead{};
671
672 std::mutex mLock; // for synchronizing start/stop/close
673
674 oboe::Result mErrorCallbackResult = oboe::Result::OK;
675
681
682 // Time to sleep in order to prevent a race condition with a callback after a close().
683 // Two milliseconds may be enough but 10 msec is even safer.
684 static constexpr int kMinDelayBeforeCloseMillis = 10;
685 int32_t mDelayBeforeCloseMillis = kMinDelayBeforeCloseMillis;
686
687private:
688
689 // Log the scheduler if it changes.
690 void checkScheduler();
691 int mPreviousScheduler = -1;
692
693 std::atomic<bool> mDataCallbackEnabled{false};
694 std::atomic<bool> mErrorCallbackCalled{false};
695
696 std::atomic<bool> mPerformanceHintEnabled{false}; // set only by app
697};
698
704 void operator()(AudioStream *audioStream) {
705 if (audioStream) {
706 audioStream->close();
707 }
708 delete audioStream;
709 }
710 };
711} // namespace oboe
712
713#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:44
int32_t mFramesPerBurst
Definition AudioStream.h:680
virtual Result pause(int64_t timeoutNanoseconds=kDefaultTimeoutNanos)
std::atomic< int64_t > mFramesRead
Definition AudioStream.h:670
virtual Result waitForStateChange(StreamState inputState, StreamState *nextState, int64_t timeoutNanoseconds)=0
virtual DataCallbackResult onDefaultCallback(void *, int)
Definition AudioStream.h:579
void setDataCallbackEnabled(bool enabled)
Definition AudioStream.h:604
bool usesAAudio() const
Definition AudioStream.h:404
virtual AudioApi getAudioApi() const =0
int32_t getFramesPerBurst() const
Definition AudioStream.h:238
virtual Result flush(int64_t timeoutNanoseconds=kDefaultTimeoutNanos)
virtual Result requestStart()=0
virtual ResultWithValue< double > calculateLatencyMillis()
Definition AudioStream.h:302
virtual ResultWithValue< int32_t > read(void *, int32_t, int64_t)
Definition AudioStream.h:386
virtual bool isXRunCountSupported() const =0
int32_t getBytesPerSample() const
virtual ResultWithValue< int32_t > getXRunCount()
Definition AudioStream.h:224
virtual void updateFramesRead()=0
virtual StreamState getState()=0
virtual Result release()
Definition AudioStream.h:90
virtual void closePerformanceHint()
Definition AudioStream.h:637
int32_t getBytesPerFrame() const
Definition AudioStream.h:249
void sleepBeforeClose()
Definition AudioStream.h:617
virtual void updateFramesWritten()=0
virtual Result open()
Definition AudioStream.h:67
virtual void beginPerformanceHintInCallback()
Definition AudioStream.h:626
virtual Result requestFlush()=0
virtual void endPerformanceHintInCallback(int32_t)
Definition AudioStream.h:632
virtual Result requestPause()=0
virtual Result requestStop()=0
std::atomic< int64_t > mFramesWritten
Definition AudioStream.h:662
virtual Result waitForStateTransition(StreamState startingState, StreamState endingState, int64_t timeoutNanoseconds)
virtual void * getUnderlyingStream() const
Definition AudioStream.h:416
AudioStream(const AudioStreamBuilder &builder)
virtual oboe::Result getLastErrorCallbackResult() const
Definition AudioStream.h:483
virtual Result close()
bool isPerformanceHintEnabled()
Definition AudioStream.h:543
bool wasErrorCallbackCalled()
Definition AudioStream.h:558
ResultWithValue< int32_t > getAvailableFrames()
virtual ResultWithValue< int32_t > setBufferSizeInFrames(int32_t)
Definition AudioStream.h:208
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:327
virtual ResultWithValue< int32_t > write(const void *, int32_t, int64_t)
Definition AudioStream.h:368
void setPerformanceHintEnabled(bool enabled)
Definition AudioStream.h:533
bool isDataCallbackEnabled()
Definition AudioStream.h:596
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:506
virtual ResultWithValue< FrameTimestamp > getTimestamp(clockid_t)
Definition ResultWithValue.h:47
Definition AudioClock.h:24
Result
Definition Definitions.h:172
constexpr int64_t kNanosPerMillisecond
Definition Definitions.h:42
StreamState
Definition Definitions.h:57
AudioApi
Definition Definitions.h:254
constexpr int64_t kDefaultTimeoutNanos
Definition AudioStream.h:39
constexpr int32_t kUnspecified
Definition Definitions.h:31
DataCallbackResult
Definition Definitions.h:160
Definition AudioStream.h:703