Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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_BUFFERED_H |
| 18 | #define OBOE_STREAM_BUFFERED_H |
| 19 | |
Phil Burk | 34217a7 | 2018-02-07 20:37:17 -0800 | [diff] [blame] | 20 | #include <cstring> |
| 21 | #include <assert.h> |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 22 | #include "common/OboeDebug.h" |
Don Turner | 379e8e5 | 2017-11-29 15:49:29 +0000 | [diff] [blame] | 23 | #include "oboe/AudioStream.h" |
| 24 | #include "oboe/AudioStreamCallback.h" |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 25 | #include "fifo/FifoBuffer.h" |
| 26 | |
| 27 | namespace oboe { |
| 28 | |
| 29 | // A stream that contains a FIFO buffer. |
Phil Burk | 34217a7 | 2018-02-07 20:37:17 -0800 | [diff] [blame] | 30 | // This is used to implement blocking reads and writes. |
Don Turner | 379e8e5 | 2017-11-29 15:49:29 +0000 | [diff] [blame] | 31 | class AudioStreamBuffered : public AudioStream { |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 32 | public: |
| 33 | |
Don Turner | 379e8e5 | 2017-11-29 15:49:29 +0000 | [diff] [blame] | 34 | AudioStreamBuffered(); |
| 35 | explicit AudioStreamBuffered(const AudioStreamBuilder &builder); |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 36 | |
Phil Burk | a5d6637 | 2018-02-09 14:38:23 -0800 | [diff] [blame] | 37 | void allocateFifo(); |
| 38 | |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 39 | |
Don Turner | 75241ba | 2018-05-07 20:03:40 -0700 | [diff] [blame^] | 40 | ResultWithValue<int32_t> write(const void *buffer, |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 41 | int32_t numFrames, |
| 42 | int64_t timeoutNanoseconds) override; |
| 43 | |
Don Turner | 75241ba | 2018-05-07 20:03:40 -0700 | [diff] [blame^] | 44 | ResultWithValue<int32_t> read(void *buffer, |
Phil Burk | 34217a7 | 2018-02-07 20:37:17 -0800 | [diff] [blame] | 45 | int32_t numFrames, |
| 46 | int64_t timeoutNanoseconds) override; |
| 47 | |
Don Turner | 75241ba | 2018-05-07 20:03:40 -0700 | [diff] [blame^] | 48 | ResultWithValue<int32_t> setBufferSizeInFrames(int32_t requestedFrames) override; |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 49 | |
| 50 | int32_t getBufferSizeInFrames() const override; |
| 51 | |
| 52 | int32_t getBufferCapacityInFrames() const override; |
| 53 | |
Phil Burk | a5d6637 | 2018-02-09 14:38:23 -0800 | [diff] [blame] | 54 | int32_t getXRunCount() const override { |
| 55 | return mXRunCount; |
| 56 | } |
| 57 | |
| 58 | int64_t getFramesWritten() const override; |
| 59 | |
| 60 | int64_t getFramesRead() const override; |
| 61 | |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 62 | protected: |
| 63 | |
Phil Burk | a5d6637 | 2018-02-09 14:38:23 -0800 | [diff] [blame] | 64 | DataCallbackResult onDefaultCallback(void *audioData, int numFrames) override; |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 65 | |
Phil Burk | a5d6637 | 2018-02-09 14:38:23 -0800 | [diff] [blame] | 66 | // If there is no callback then we need a FIFO between the App and OpenSL ES. |
| 67 | bool usingFIFO() const { return getCallback() == nullptr; } |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 68 | |
Phil Burk | f2a7053 | 2018-05-09 18:36:21 -0700 | [diff] [blame] | 69 | virtual Result updateServiceFrameCounter() { return Result::OK; }; |
| 70 | |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 71 | private: |
| 72 | |
Phil Burk | a5d6637 | 2018-02-09 14:38:23 -0800 | [diff] [blame] | 73 | |
| 74 | int64_t predictNextCallbackTime(); |
| 75 | |
| 76 | void markCallbackTime(int numFrames); |
| 77 | |
| 78 | // Read or write to the FIFO. |
Don Turner | 75241ba | 2018-05-07 20:03:40 -0700 | [diff] [blame^] | 79 | ResultWithValue<int32_t> transfer(void *buffer, int32_t numFrames, int64_t timeoutNanoseconds); |
Phil Burk | a5d6637 | 2018-02-09 14:38:23 -0800 | [diff] [blame] | 80 | |
| 81 | void incrementXRunCount() { |
| 82 | mXRunCount++; |
| 83 | } |
| 84 | |
| 85 | std::unique_ptr<FifoBuffer> mFifoBuffer; |
| 86 | |
| 87 | int64_t mBackgroundRanAtNanoseconds = 0; |
| 88 | int32_t mLastBackgroundSize = 0; |
| 89 | int32_t mXRunCount = 0; |
Don Turner | 3bf32ae | 2017-11-27 13:25:05 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | } // namespace oboe |
| 93 | |
| 94 | #endif //OBOE_STREAM_BUFFERED_H |