Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 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 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 17 | #ifndef ANDROID_AAUDIO_RINGBUFFER_PARCELABLE_H |
| 18 | #define ANDROID_AAUDIO_RINGBUFFER_PARCELABLE_H |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 19 | |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame^] | 20 | #include <map> |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 23 | #include <aaudio/RingBuffer.h> |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 24 | #include <binder/Parcelable.h> |
| 25 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 26 | #include "binding/AAudioServiceDefinitions.h" |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 27 | #include "binding/SharedRegionParcelable.h" |
| 28 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 29 | namespace aaudio { |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 30 | |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 31 | class RingBufferParcelable { |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 32 | public: |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 33 | RingBufferParcelable() = default; |
| 34 | |
| 35 | // Construct based on a parcelable representation. |
| 36 | explicit RingBufferParcelable(const RingBuffer& parcelable); |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 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 | |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame^] | 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 | |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 66 | int32_t getBytesPerFrame() const; |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 67 | |
| 68 | void setBytesPerFrame(int32_t bytesPerFrame); |
| 69 | |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 70 | int32_t getFramesPerBurst() const; |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 71 | |
| 72 | void setFramesPerBurst(int32_t framesPerBurst); |
| 73 | |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 74 | int32_t getCapacityInFrames() const; |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 75 | |
| 76 | void setCapacityInFrames(int32_t capacityInFrames); |
| 77 | |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 78 | bool isFileDescriptorSafe(SharedMemoryParcelable *memoryParcels); |
| 79 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 80 | aaudio_result_t resolve(SharedMemoryParcelable *memoryParcels, RingBufferDescriptor *descriptor); |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 81 | |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame^] | 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); |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 91 | |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame^] | 92 | int32_t getReadCounterSharedMemoryIndex() const { |
| 93 | return mReadCounterParcelable.getSharedMemoryIndex(); |
| 94 | } |
| 95 | int32_t getWriteCounterSharedMemoryIndex() const { |
| 96 | return mWriteCounterParcelable.getSharedMemoryIndex(); |
| 97 | } |
| 98 | int32_t getDataSharedMemoryIndex() const { |
| 99 | return mDataParcelable.getSharedMemoryIndex(); |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 102 | void dump(); |
| 103 | |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 104 | // Extract a parcelable representation of this object. |
| 105 | RingBuffer parcelable() const; |
| 106 | |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 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; |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 115 | |
| 116 | aaudio_result_t validate() const; |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 117 | }; |
| 118 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 119 | } /* namespace aaudio */ |
Phil Burk | 828bea5 | 2017-01-03 17:22:09 -0800 | [diff] [blame] | 120 | |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 121 | #endif //ANDROID_AAUDIO_RINGBUFFER_PARCELABLE_H |