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 ANDROID_AAUDIO_RINGBUFFER_PARCELABLE_H
18 #define ANDROID_AAUDIO_RINGBUFFER_PARCELABLE_H
19 
20 #include <map>
21 #include <stdint.h>
22 
23 #include <aaudio/RingBuffer.h>
24 #include <binder/Parcelable.h>
25 
26 #include "binding/AAudioServiceDefinitions.h"
27 #include "binding/SharedRegionParcelable.h"
28 
29 namespace aaudio {
30 
31 class RingBufferParcelable  {
32 public:
33     RingBufferParcelable() = default;
34 
35     // Construct based on a parcelable representation.
36     explicit RingBufferParcelable(const RingBuffer& parcelable);
37 
38     // TODO This assumes that all three use the same SharedMemoryParcelable
39     void setupMemory(int32_t sharedMemoryIndex,
40                      int32_t dataMemoryOffset,
41                      int32_t dataSizeInBytes,
42                      int32_t readCounterOffset,
43                      int32_t writeCounterOffset,
44                      int32_t counterSizeBytes);
45 
46     void setupMemory(int32_t sharedMemoryIndex,
47                      int32_t dataMemoryOffset,
48                      int32_t dataSizeInBytes);
49 
50     /**
51      * Set up memory for the RingBufferParcelable.
52      *
53      * This function will take three MemoryInfoTuple as parameters to set up memory. The
54      * MemoryInfoTuple contains the shared memory index, offset in the shared memory and size
55      * of the object. This will allow setting up the read counter, write counter and data memory
56      * that are located in different shared memory blocks.
57      *
58      * @param dataMemoryInfo
59      * @param readCounterInfo
60      * @param writeCounterInfo
61      */
62     void setupMemory(const SharedRegionParcelable::MemoryInfoTuple& dataMemoryInfo,
63                      const SharedRegionParcelable::MemoryInfoTuple& readCounterInfo,
64                      const SharedRegionParcelable::MemoryInfoTuple& writeCounterInfo);
65 
66     int32_t getBytesPerFrame() const;
67 
68     void setBytesPerFrame(int32_t bytesPerFrame);
69 
70     int32_t getFramesPerBurst() const;
71 
72     void setFramesPerBurst(int32_t framesPerBurst);
73 
74     int32_t getCapacityInFrames() const;
75 
76     void setCapacityInFrames(int32_t capacityInFrames);
77 
78     bool isFileDescriptorSafe(SharedMemoryParcelable *memoryParcels);
79 
80     aaudio_result_t resolve(SharedMemoryParcelable *memoryParcels, RingBufferDescriptor *descriptor);
81 
82     /**
83      * Update this ring buffer with the given ring buffer.
84      *
85      * @param parcelable the ring buffer to be used to update this ring buffer.
86      * @param memoryIndexMap a map from the shared memory indexes used by the given ring buffer
87      *                       to the shared memory indexes used by this ring buffer.
88      */
89     void updateMemory(const RingBufferParcelable& parcelable,
90                       const std::map<int32_t, int32_t>& memoryIndexMap);
91 
getReadCounterSharedMemoryIndex()92     int32_t getReadCounterSharedMemoryIndex() const {
93         return mReadCounterParcelable.getSharedMemoryIndex();
94     }
getWriteCounterSharedMemoryIndex()95     int32_t getWriteCounterSharedMemoryIndex() const {
96         return mWriteCounterParcelable.getSharedMemoryIndex();
97     }
getDataSharedMemoryIndex()98     int32_t getDataSharedMemoryIndex() const {
99         return mDataParcelable.getSharedMemoryIndex();
100     }
101 
102     void dump();
103 
104     // Extract a parcelable representation of this object.
105     RingBuffer parcelable() const;
106 
107 private:
108     SharedRegionParcelable  mReadCounterParcelable;
109     SharedRegionParcelable  mWriteCounterParcelable;
110     SharedRegionParcelable  mDataParcelable;
111     int32_t                 mBytesPerFrame = 0;     // index is in frames
112     int32_t                 mFramesPerBurst = 0;    // for ISOCHRONOUS queues
113     int32_t                 mCapacityInFrames = 0;  // zero if unused
114     RingbufferFlags         mFlags = RingbufferFlags::NONE;
115 
116     aaudio_result_t validate() const;
117 };
118 
119 } /* namespace aaudio */
120 
121 #endif //ANDROID_AAUDIO_RINGBUFFER_PARCELABLE_H
122