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() = default;
58
67 virtual Result open() {
68 return Result::OK; // Called by subclasses. Might do more in the future.
69 }
70
74 virtual Result close();
75
80 virtual Result start(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
81
86 virtual Result pause(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
87
92 virtual Result flush(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
93
98 virtual Result stop(int64_t timeoutNanoseconds = kDefaultTimeoutNanos);
99
100 /* Asynchronous requests.
101 * Use waitForStateChange() if you need to wait for completion.
102 */
103
108 virtual Result requestStart() = 0;
109
114 virtual Result requestPause() = 0;
115
120 virtual Result requestFlush() = 0;
121
126 virtual Result requestStop() = 0;
127
133 virtual StreamState getState() = 0;
134
163 StreamState *nextState,
164 int64_t timeoutNanoseconds) = 0;
165
178 virtual ResultWithValue<int32_t> setBufferSizeInFrames(int32_t /* requestedFrames */) {
179 return Result::ErrorUnimplemented;
180 }
181
195 return ResultWithValue<int32_t>(Result::ErrorUnimplemented);
196 }
197
201 virtual bool isXRunCountSupported() const = 0;
202
208 int32_t getFramesPerBurst() const {
209 return mFramesPerBurst;
210 }
211
219 int32_t getBytesPerFrame() const { return mChannelCount * getBytesPerSample(); }
220
227 int32_t getBytesPerSample() const;
228
235 virtual int64_t getFramesWritten();
236
243 virtual int64_t getFramesRead();
244
273 return ResultWithValue<double>(Result::ErrorUnimplemented);
274 }
275
297 virtual Result getTimestamp(clockid_t /* clockId */,
298 int64_t* /* framePosition */,
299 int64_t* /* timeNanoseconds */) {
300 return Result::ErrorUnimplemented;
301 }
302
323 virtual ResultWithValue<FrameTimestamp> getTimestamp(clockid_t /* clockId */);
324
325 // ============== I/O ===========================
338 virtual ResultWithValue<int32_t> write(const void* /* buffer */,
339 int32_t /* numFrames */,
340 int64_t /* timeoutNanoseconds */ ) {
341 return ResultWithValue<int32_t>(Result::ErrorUnimplemented);
342 }
343
356 virtual ResultWithValue<int32_t> read(void* /* buffer */,
357 int32_t /* numFrames */,
358 int64_t /* timeoutNanoseconds */) {
359 return ResultWithValue<int32_t>(Result::ErrorUnimplemented);
360 }
361
367 virtual AudioApi getAudioApi() const = 0;
368
374 bool usesAAudio() const {
375 return getAudioApi() == AudioApi::AAudio;
376 }
377
386 virtual void *getUnderlyingStream() const {
387 return nullptr;
388 }
389
394 virtual void updateFramesWritten() = 0;
395
400 virtual void updateFramesRead() = 0;
401
402 /*
403 * Swap old callback for new callback.
404 * This not atomic.
405 * This should only be used internally.
406 * @param dataCallback
407 * @return previous dataCallback
408 */
409 AudioStreamDataCallback *swapDataCallback(AudioStreamDataCallback *dataCallback) {
410 AudioStreamDataCallback *previousCallback = mDataCallback;
411 mDataCallback = dataCallback;
412 return previousCallback;
413 }
414
415 /*
416 * Swap old callback for new callback.
417 * This not atomic.
418 * This should only be used internally.
419 * @param errorCallback
420 * @return previous errorCallback
421 */
422 AudioStreamErrorCallback *swapErrorCallback(AudioStreamErrorCallback *errorCallback) {
423 AudioStreamErrorCallback *previousCallback = mErrorCallback;
424 mErrorCallback = errorCallback;
425 return previousCallback;
426 }
427
432
448 int64_t timeoutNanoseconds);
449
454 return mErrorCallbackResult;
455 }
456
457
458 int32_t getDelayBeforeCloseMillis() const {
459 return mDelayBeforeCloseMillis;
460 }
461
476 void setDelayBeforeCloseMillis(int32_t delayBeforeCloseMillis) {
477 mDelayBeforeCloseMillis = delayBeforeCloseMillis;
478 }
479
480protected:
481
492 return mErrorCallbackCalled.exchange(true);
493 }
494
502 StreamState endingState,
503 int64_t timeoutNanoseconds);
504
512 virtual DataCallbackResult onDefaultCallback(void* /* audioData */, int /* numFrames */) {
513 return DataCallbackResult::Stop;
514 }
515
524 DataCallbackResult fireDataCallback(void *audioData, int numFrames);
525
530 return mDataCallbackEnabled;
531 }
532
537 void setDataCallbackEnabled(bool enabled) {
538 mDataCallbackEnabled = enabled;
539 }
540
546
551 if (mDelayBeforeCloseMillis > 0) {
552 usleep(mDelayBeforeCloseMillis * 1000);
553 }
554 }
555
556 /*
557 * Set a weak_ptr to this stream from the shared_ptr so that we can
558 * later use a shared_ptr in the error callback.
559 */
560 void setWeakThis(std::shared_ptr<oboe::AudioStream> &sharedStream) {
561 mWeakThis = sharedStream;
562 }
563
564 /*
565 * Make a shared_ptr that will prevent this stream from being deleted.
566 */
567 std::shared_ptr<oboe::AudioStream> lockWeakThis() {
568 return mWeakThis.lock();
569 }
570
571 std::weak_ptr<AudioStream> mWeakThis; // weak pointer to this object
572
579 std::atomic<int64_t> mFramesWritten{};
580
587 std::atomic<int64_t> mFramesRead{};
588
589 std::mutex mLock; // for synchronizing start/stop/close
590
591 oboe::Result mErrorCallbackResult = oboe::Result::OK;
592
598
599 // Time to sleep in order to prevent a race condition with a callback after a close().
600 // Two milliseconds may be enough but 10 msec is even safer.
601 static constexpr int kMinDelayBeforeCloseMillis = 10;
602 int32_t mDelayBeforeCloseMillis = kMinDelayBeforeCloseMillis;
603
604private:
605
606 // Log the scheduler if it changes.
607 void checkScheduler();
608 int mPreviousScheduler = -1;
609
610 std::atomic<bool> mDataCallbackEnabled{false};
611 std::atomic<bool> mErrorCallbackCalled{false};
612};
613
619 void operator()(AudioStream *audioStream) {
620 if (audioStream) {
621 audioStream->close();
622 }
623 delete audioStream;
624 }
625 };
626} // namespace oboe
627
628#endif /* OBOE_STREAM_H_ */
Definition: AudioStreamBase.h:30
int32_t mChannelCount
Definition: AudioStreamBase.h:200
AudioStreamDataCallback * mDataCallback
Definition: AudioStreamBase.h:190
AudioStreamErrorCallback * mErrorCallback
Definition: AudioStreamBase.h:194
Definition: AudioStreamBuilder.h:34
Definition: AudioStreamCallback.h:34
Definition: AudioStreamCallback.h:101
Definition: AudioStream.h:44
int32_t mFramesPerBurst
Definition: AudioStream.h:597
virtual Result pause(int64_t timeoutNanoseconds=kDefaultTimeoutNanos)
std::atomic< int64_t > mFramesRead
Definition: AudioStream.h:587
virtual Result waitForStateChange(StreamState inputState, StreamState *nextState, int64_t timeoutNanoseconds)=0
virtual DataCallbackResult onDefaultCallback(void *, int)
Definition: AudioStream.h:512
void setDataCallbackEnabled(bool enabled)
Definition: AudioStream.h:537
bool usesAAudio() const
Definition: AudioStream.h:374
virtual AudioApi getAudioApi() const =0
int32_t getFramesPerBurst() const
Definition: AudioStream.h:208
virtual Result flush(int64_t timeoutNanoseconds=kDefaultTimeoutNanos)
virtual Result requestStart()=0
virtual ResultWithValue< double > calculateLatencyMillis()
Definition: AudioStream.h:272
virtual ResultWithValue< int32_t > read(void *, int32_t, int64_t)
Definition: AudioStream.h:356
virtual bool isXRunCountSupported() const =0
int32_t getBytesPerSample() const
virtual ResultWithValue< int32_t > getXRunCount()
Definition: AudioStream.h:194
virtual void updateFramesRead()=0
virtual StreamState getState()=0
int32_t getBytesPerFrame() const
Definition: AudioStream.h:219
void sleepBeforeClose()
Definition: AudioStream.h:550
virtual void updateFramesWritten()=0
virtual Result open()
Definition: AudioStream.h:67
virtual Result requestFlush()=0
virtual Result requestPause()=0
virtual Result requestStop()=0
std::atomic< int64_t > mFramesWritten
Definition: AudioStream.h:579
virtual Result waitForStateTransition(StreamState startingState, StreamState endingState, int64_t timeoutNanoseconds)
virtual void * getUnderlyingStream() const
Definition: AudioStream.h:386
AudioStream(const AudioStreamBuilder &builder)
virtual oboe::Result getLastErrorCallbackResult() const
Definition: AudioStream.h:453
virtual Result close()
bool wasErrorCallbackCalled()
Definition: AudioStream.h:491
ResultWithValue< int32_t > getAvailableFrames()
virtual ResultWithValue< int32_t > setBufferSizeInFrames(int32_t)
Definition: AudioStream.h:178
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:297
virtual ResultWithValue< int32_t > write(const void *, int32_t, int64_t)
Definition: AudioStream.h:338
bool isDataCallbackEnabled()
Definition: AudioStream.h:529
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:476
virtual ResultWithValue< FrameTimestamp > getTimestamp(clockid_t)
Definition: ResultWithValue.h:47
Definition: AudioStream.h:31
Result
Definition: Definitions.h:157
constexpr int64_t kNanosPerMillisecond
Definition: Definitions.h:42
StreamState
Definition: Definitions.h:57
AudioApi
Definition: Definitions.h:239
constexpr int64_t kDefaultTimeoutNanos
Definition: AudioStream.h:39
constexpr int32_t kUnspecified
Definition: Definitions.h:31
DataCallbackResult
Definition: Definitions.h:145
Definition: AudioStream.h:618