Phil Burk | 204a163 | 2017-01-03 17:23:43 -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 | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 17 | #define LOG_TAG "RingBufferParcelable" |
Phil Burk | c0c70e3 | 2017-02-09 13:18:38 -0800 | [diff] [blame] | 18 | //#define LOG_NDEBUG 0 |
| 19 | #include <utils/Log.h> |
| 20 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 21 | #include <stdint.h> |
| 22 | |
| 23 | #include <binder/Parcelable.h> |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 24 | #include <utility/AAudioUtilities.h> |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 25 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 26 | #include "binding/AAudioServiceDefinitions.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 27 | #include "binding/SharedRegionParcelable.h" |
| 28 | #include "binding/RingBufferParcelable.h" |
| 29 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 30 | using namespace aaudio; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 31 | |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 32 | RingBufferParcelable::RingBufferParcelable(const RingBuffer& parcelable) |
jiabin | d5bd06a | 2021-04-27 22:04:08 +0000 | [diff] [blame] | 33 | : mReadCounterParcelable(parcelable.readCounterParcelable), |
| 34 | mWriteCounterParcelable(parcelable.writeCounterParcelable), |
| 35 | mDataParcelable(parcelable.dataParcelable), |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 36 | mBytesPerFrame(parcelable.bytesPerFrame), |
| 37 | mFramesPerBurst(parcelable.framesPerBurst), |
| 38 | mCapacityInFrames(parcelable.capacityInFrames), |
| 39 | mFlags(static_cast<RingbufferFlags>(parcelable.flags)) { |
| 40 | static_assert(sizeof(mFlags) == sizeof(parcelable.flags)); |
| 41 | } |
| 42 | |
| 43 | RingBuffer RingBufferParcelable::parcelable() const { |
| 44 | RingBuffer result; |
jiabin | d5bd06a | 2021-04-27 22:04:08 +0000 | [diff] [blame] | 45 | result.readCounterParcelable = mReadCounterParcelable.parcelable(); |
| 46 | result.writeCounterParcelable = mWriteCounterParcelable.parcelable(); |
| 47 | result.dataParcelable = mDataParcelable.parcelable(); |
Ytai Ben-Tsvi | c5f4587 | 2020-08-18 10:39:44 -0700 | [diff] [blame] | 48 | result.bytesPerFrame = mBytesPerFrame; |
| 49 | result.framesPerBurst = mFramesPerBurst; |
| 50 | result.capacityInFrames = mCapacityInFrames; |
| 51 | static_assert(sizeof(mFlags) == sizeof(result.flags)); |
| 52 | result.flags = static_cast<int32_t>(mFlags); |
| 53 | return result; |
| 54 | } |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 55 | |
| 56 | // TODO This assumes that all three use the same SharedMemoryParcelable |
| 57 | void RingBufferParcelable::setupMemory(int32_t sharedMemoryIndex, |
| 58 | int32_t dataMemoryOffset, |
| 59 | int32_t dataSizeInBytes, |
| 60 | int32_t readCounterOffset, |
| 61 | int32_t writeCounterOffset, |
| 62 | int32_t counterSizeBytes) { |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 63 | mReadCounterParcelable.setup({sharedMemoryIndex, readCounterOffset, counterSizeBytes}); |
| 64 | mWriteCounterParcelable.setup({sharedMemoryIndex, writeCounterOffset, counterSizeBytes}); |
| 65 | mDataParcelable.setup({sharedMemoryIndex, dataMemoryOffset, dataSizeInBytes}); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void RingBufferParcelable::setupMemory(int32_t sharedMemoryIndex, |
| 69 | int32_t dataMemoryOffset, |
| 70 | int32_t dataSizeInBytes) { |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 71 | mReadCounterParcelable.setup({sharedMemoryIndex, 0, 0}); |
| 72 | mWriteCounterParcelable.setup({sharedMemoryIndex, 0, 0}); |
| 73 | mDataParcelable.setup({sharedMemoryIndex, dataMemoryOffset, dataSizeInBytes}); |
| 74 | } |
| 75 | |
| 76 | void RingBufferParcelable::setupMemory( |
| 77 | const SharedRegionParcelable::MemoryInfoTuple& dataMemoryInfo, |
| 78 | const SharedRegionParcelable::MemoryInfoTuple& readCounterInfo, |
| 79 | const SharedRegionParcelable::MemoryInfoTuple& writeCounterInfo) { |
| 80 | mReadCounterParcelable.setup(readCounterInfo); |
| 81 | mWriteCounterParcelable.setup(writeCounterInfo); |
| 82 | mDataParcelable.setup(dataMemoryInfo); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 83 | } |
| 84 | |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 85 | int32_t RingBufferParcelable::getBytesPerFrame() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 86 | return mBytesPerFrame; |
| 87 | } |
| 88 | |
| 89 | void RingBufferParcelable::setBytesPerFrame(int32_t bytesPerFrame) { |
| 90 | mBytesPerFrame = bytesPerFrame; |
| 91 | } |
| 92 | |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 93 | int32_t RingBufferParcelable::getFramesPerBurst() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 94 | return mFramesPerBurst; |
| 95 | } |
| 96 | |
| 97 | void RingBufferParcelable::setFramesPerBurst(int32_t framesPerBurst) { |
| 98 | mFramesPerBurst = framesPerBurst; |
| 99 | } |
| 100 | |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 101 | int32_t RingBufferParcelable::getCapacityInFrames() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 102 | return mCapacityInFrames; |
| 103 | } |
| 104 | |
| 105 | void RingBufferParcelable::setCapacityInFrames(int32_t capacityInFrames) { |
| 106 | mCapacityInFrames = capacityInFrames; |
| 107 | } |
| 108 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 109 | aaudio_result_t RingBufferParcelable::resolve(SharedMemoryParcelable *memoryParcels, RingBufferDescriptor *descriptor) { |
| 110 | aaudio_result_t result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 111 | |
| 112 | result = mReadCounterParcelable.resolve(memoryParcels, |
| 113 | (void **) &descriptor->readCounterAddress); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 114 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 115 | return result; |
| 116 | } |
| 117 | |
| 118 | result = mWriteCounterParcelable.resolve(memoryParcels, |
| 119 | (void **) &descriptor->writeCounterAddress); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 120 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 121 | return result; |
| 122 | } |
| 123 | |
| 124 | result = mDataParcelable.resolve(memoryParcels, (void **) &descriptor->dataAddress); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 125 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 126 | return result; |
| 127 | } |
| 128 | |
| 129 | descriptor->bytesPerFrame = mBytesPerFrame; |
| 130 | descriptor->framesPerBurst = mFramesPerBurst; |
| 131 | descriptor->capacityInFrames = mCapacityInFrames; |
| 132 | descriptor->flags = mFlags; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 133 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 134 | } |
| 135 | |
jiabin | fc791ee | 2023-02-15 19:43:40 +0000 | [diff] [blame] | 136 | void RingBufferParcelable::updateMemory(const RingBufferParcelable& parcelable, |
| 137 | const std::map<int32_t, int32_t>& memoryIndexMap) { |
| 138 | setupMemory(parcelable.mDataParcelable.getMemoryInfo(&memoryIndexMap), |
| 139 | parcelable.mReadCounterParcelable.getMemoryInfo(&memoryIndexMap), |
| 140 | parcelable.mWriteCounterParcelable.getMemoryInfo(&memoryIndexMap)); |
jiabin | f7f0615 | 2021-11-22 18:10:14 +0000 | [diff] [blame] | 141 | setBytesPerFrame(parcelable.getBytesPerFrame()); |
| 142 | setFramesPerBurst(parcelable.getFramesPerBurst()); |
| 143 | setCapacityInFrames(parcelable.getCapacityInFrames()); |
| 144 | } |
| 145 | |
Phil Burk | a5891f4 | 2018-03-12 17:21:11 -0700 | [diff] [blame] | 146 | aaudio_result_t RingBufferParcelable::validate() const { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 147 | if (mCapacityInFrames < 0 || mCapacityInFrames >= 32 * 1024) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 148 | ALOGE("invalid mCapacityInFrames = %d", mCapacityInFrames); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 149 | return AAUDIO_ERROR_INTERNAL; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 150 | } |
| 151 | if (mBytesPerFrame < 0 || mBytesPerFrame >= 256) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 152 | ALOGE("invalid mBytesPerFrame = %d", mBytesPerFrame); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 153 | return AAUDIO_ERROR_INTERNAL; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 154 | } |
Phil Burk | 5204d31 | 2017-05-04 17:16:13 -0700 | [diff] [blame] | 155 | if (mFramesPerBurst < 0 || mFramesPerBurst >= 16 * 1024) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 156 | ALOGE("invalid mFramesPerBurst = %d", mFramesPerBurst); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 157 | return AAUDIO_ERROR_INTERNAL; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 158 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 159 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 162 | void RingBufferParcelable::dump() { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 163 | ALOGD("mCapacityInFrames = %d ---------", mCapacityInFrames); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 164 | if (mCapacityInFrames > 0) { |
Phil Burk | fbf031e | 2017-10-12 15:58:31 -0700 | [diff] [blame] | 165 | ALOGD("mBytesPerFrame = %d", mBytesPerFrame); |
| 166 | ALOGD("mFramesPerBurst = %d", mFramesPerBurst); |
| 167 | ALOGD("mFlags = %u", mFlags); |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 168 | mReadCounterParcelable.dump(); |
| 169 | mWriteCounterParcelable.dump(); |
| 170 | mDataParcelable.dump(); |
| 171 | } |
| 172 | } |