blob: b4c919a2a4b63bde8a8de7ca0f54a61582f4182d [file] [log] [blame]
Don Turner3bf32ae2017-11-27 13:25:05 +00001/*
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 Burk34217a72018-02-07 20:37:17 -080020#include <cstring>
21#include <assert.h>
Don Turner3bf32ae2017-11-27 13:25:05 +000022#include "common/OboeDebug.h"
Don Turner379e8e52017-11-29 15:49:29 +000023#include "oboe/AudioStream.h"
24#include "oboe/AudioStreamCallback.h"
Don Turner3bf32ae2017-11-27 13:25:05 +000025#include "fifo/FifoBuffer.h"
26
27namespace oboe {
28
29// A stream that contains a FIFO buffer.
Phil Burk34217a72018-02-07 20:37:17 -080030// This is used to implement blocking reads and writes.
Don Turner379e8e52017-11-29 15:49:29 +000031class AudioStreamBuffered : public AudioStream {
Don Turner3bf32ae2017-11-27 13:25:05 +000032public:
33
Don Turner379e8e52017-11-29 15:49:29 +000034 AudioStreamBuffered();
35 explicit AudioStreamBuffered(const AudioStreamBuilder &builder);
Don Turner3bf32ae2017-11-27 13:25:05 +000036
Phil Burka5d66372018-02-09 14:38:23 -080037 void allocateFifo();
38
Don Turner3bf32ae2017-11-27 13:25:05 +000039
Don Turner75241ba2018-05-07 20:03:40 -070040 ResultWithValue<int32_t> write(const void *buffer,
Don Turner3bf32ae2017-11-27 13:25:05 +000041 int32_t numFrames,
42 int64_t timeoutNanoseconds) override;
43
Don Turner75241ba2018-05-07 20:03:40 -070044 ResultWithValue<int32_t> read(void *buffer,
Phil Burk34217a72018-02-07 20:37:17 -080045 int32_t numFrames,
46 int64_t timeoutNanoseconds) override;
47
Don Turner75241ba2018-05-07 20:03:40 -070048 ResultWithValue<int32_t> setBufferSizeInFrames(int32_t requestedFrames) override;
Don Turner3bf32ae2017-11-27 13:25:05 +000049
50 int32_t getBufferSizeInFrames() const override;
51
52 int32_t getBufferCapacityInFrames() const override;
53
Phil Burka5d66372018-02-09 14:38:23 -080054 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 Turner3bf32ae2017-11-27 13:25:05 +000062protected:
63
Phil Burka5d66372018-02-09 14:38:23 -080064 DataCallbackResult onDefaultCallback(void *audioData, int numFrames) override;
Don Turner3bf32ae2017-11-27 13:25:05 +000065
Phil Burka5d66372018-02-09 14:38:23 -080066 // If there is no callback then we need a FIFO between the App and OpenSL ES.
67 bool usingFIFO() const { return getCallback() == nullptr; }
Don Turner3bf32ae2017-11-27 13:25:05 +000068
Phil Burkf2a70532018-05-09 18:36:21 -070069 virtual Result updateServiceFrameCounter() { return Result::OK; };
70
Don Turner3bf32ae2017-11-27 13:25:05 +000071private:
72
Phil Burka5d66372018-02-09 14:38:23 -080073
74 int64_t predictNextCallbackTime();
75
76 void markCallbackTime(int numFrames);
77
78 // Read or write to the FIFO.
Don Turner75241ba2018-05-07 20:03:40 -070079 ResultWithValue<int32_t> transfer(void *buffer, int32_t numFrames, int64_t timeoutNanoseconds);
Phil Burka5d66372018-02-09 14:38:23 -080080
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 Turner3bf32ae2017-11-27 13:25:05 +000090};
91
92} // namespace oboe
93
94#endif //OBOE_STREAM_BUFFERED_H