1 /*
2  * Copyright (C) 2020 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 #pragma once
18 #include <memory>
19 #include <chrono>
20 #include <utility>
21 #include <condition_variable>
22 #include <mutex>
23 #include <stdint.h>
24 
25 namespace android {
26 namespace hardware {
27 namespace audio {
28 namespace V6_0 {
29 namespace implementation {
30 
31 // A one-producer-one-consumer ring buffer.
32 struct RingBuffer {
33     typedef std::chrono::time_point<std::chrono::high_resolution_clock> Timepoint;
34 
35     RingBuffer(size_t capacity);
36 
capacityRingBuffer37     size_t capacity() const { return mCapacity; }
38     size_t availableToProduce() const;
39     size_t availableToConsume() const;
40 
41     struct ContiniousChunk {
42         void *data;
43         size_t size;
44         std::unique_lock<std::mutex> lock;
45     };
46 
47     struct ContiniousLockedChunk : public ContiniousChunk{
48         std::unique_lock<std::mutex> lock;
49     };
50 
51     size_t makeRoomForProduce(size_t atLeast);
52 
53     bool waitForProduceAvailable(Timepoint blockUntil) const;
54 
55     // `getProduceChunk` is a non-blocking function which returns a pointer
56     // (`result.data`) inside RingBuffer's buffer, `result.size` is the
57     // size of the continious chunk (can be smaller than availableToProduce()).
58     ContiniousChunk getProduceChunk() const;
59 
60     // Tries to move the `produce` cursor by `size`, returns the actual size
61     // moved.
62     size_t produce(size_t size);
63 
64     // Tried to write `size` bytes into the internal buffer from `data`,
65     // returns the actual size written.
66     size_t produce(const void *data, size_t size);
67 
68     bool waitForConsumeAvailable(Timepoint blockUntil) const;
69 
70     // `getConsumeChunk` is a non-blocking function which a pointer
71     // (`result.data`) inside RingBuffer's buffer, `result.size` is the
72     //  size of the continious chunk (can be smaller than availableToConsume()).
73     // IMPORTANT: do not call long running functions while have an instance of
74     //            ContiniousLockedChunk, it might block the producer because
75     //            it need to drop the stale audio data to replace it with
76     //            more recent one.
77     // NOTE: ContiniousLockedChunk holds a lock inside, it might deadlock
78     //       if you calling other RingBuffer's functions that assume it is
79     //       unlocked, in this case you want to introduce a function like
80     //       `consume` below.
81     ContiniousLockedChunk getConsumeChunk() const;
82 
83     // Tries to move the `consume` cursor by `size`, returns the actual size
84     // moved.
85     size_t consume(const ContiniousLockedChunk&, size_t size);
86 
87 private:
88     std::unique_ptr<uint8_t[]> mBuffer;
89     mutable std::condition_variable mProduceAvailable;
90     mutable std::condition_variable mConsumeAvailable;
91     mutable std::mutex mMutex;
92     const int mCapacity;
93     int mAvailableToConsume = 0;
94     int mProducePos = 0;
95     int mConsumePos = 0;
96 };
97 
98 }  // namespace implementation
99 }  // namespace V6_0
100 }  // namespace audio
101 }  // namespace hardware
102 }  // namespace android
103