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