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 | |
| 17 | #include <stdint.h> |
| 18 | |
| 19 | #include <binder/Parcelable.h> |
| 20 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 21 | #include "binding/AAudioServiceDefinitions.h" |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 22 | #include "binding/SharedRegionParcelable.h" |
| 23 | #include "binding/RingBufferParcelable.h" |
| 24 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 25 | using namespace aaudio; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 26 | |
| 27 | RingBufferParcelable::RingBufferParcelable() {} |
| 28 | RingBufferParcelable::~RingBufferParcelable() {} |
| 29 | |
| 30 | // TODO This assumes that all three use the same SharedMemoryParcelable |
| 31 | void RingBufferParcelable::setupMemory(int32_t sharedMemoryIndex, |
| 32 | int32_t dataMemoryOffset, |
| 33 | int32_t dataSizeInBytes, |
| 34 | int32_t readCounterOffset, |
| 35 | int32_t writeCounterOffset, |
| 36 | int32_t counterSizeBytes) { |
| 37 | mReadCounterParcelable.setup(sharedMemoryIndex, readCounterOffset, counterSizeBytes); |
| 38 | mWriteCounterParcelable.setup(sharedMemoryIndex, writeCounterOffset, counterSizeBytes); |
| 39 | mDataParcelable.setup(sharedMemoryIndex, dataMemoryOffset, dataSizeInBytes); |
| 40 | } |
| 41 | |
| 42 | void RingBufferParcelable::setupMemory(int32_t sharedMemoryIndex, |
| 43 | int32_t dataMemoryOffset, |
| 44 | int32_t dataSizeInBytes) { |
| 45 | mReadCounterParcelable.setup(sharedMemoryIndex, 0, 0); |
| 46 | mWriteCounterParcelable.setup(sharedMemoryIndex, 0, 0); |
| 47 | mDataParcelable.setup(sharedMemoryIndex, dataMemoryOffset, dataSizeInBytes); |
| 48 | } |
| 49 | |
| 50 | int32_t RingBufferParcelable::getBytesPerFrame() { |
| 51 | return mBytesPerFrame; |
| 52 | } |
| 53 | |
| 54 | void RingBufferParcelable::setBytesPerFrame(int32_t bytesPerFrame) { |
| 55 | mBytesPerFrame = bytesPerFrame; |
| 56 | } |
| 57 | |
| 58 | int32_t RingBufferParcelable::getFramesPerBurst() { |
| 59 | return mFramesPerBurst; |
| 60 | } |
| 61 | |
| 62 | void RingBufferParcelable::setFramesPerBurst(int32_t framesPerBurst) { |
| 63 | mFramesPerBurst = framesPerBurst; |
| 64 | } |
| 65 | |
| 66 | int32_t RingBufferParcelable::getCapacityInFrames() { |
| 67 | return mCapacityInFrames; |
| 68 | } |
| 69 | |
| 70 | void RingBufferParcelable::setCapacityInFrames(int32_t capacityInFrames) { |
| 71 | mCapacityInFrames = capacityInFrames; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * The read and write must be symmetric. |
| 76 | */ |
| 77 | status_t RingBufferParcelable::writeToParcel(Parcel* parcel) const { |
| 78 | parcel->writeInt32(mCapacityInFrames); |
| 79 | if (mCapacityInFrames > 0) { |
| 80 | parcel->writeInt32(mBytesPerFrame); |
| 81 | parcel->writeInt32(mFramesPerBurst); |
| 82 | parcel->writeInt32(mFlags); |
| 83 | mReadCounterParcelable.writeToParcel(parcel); |
| 84 | mWriteCounterParcelable.writeToParcel(parcel); |
| 85 | mDataParcelable.writeToParcel(parcel); |
| 86 | } |
| 87 | return NO_ERROR; // TODO check for errors above |
| 88 | } |
| 89 | |
| 90 | status_t RingBufferParcelable::readFromParcel(const Parcel* parcel) { |
| 91 | parcel->readInt32(&mCapacityInFrames); |
| 92 | if (mCapacityInFrames > 0) { |
| 93 | parcel->readInt32(&mBytesPerFrame); |
| 94 | parcel->readInt32(&mFramesPerBurst); |
| 95 | parcel->readInt32((int32_t *)&mFlags); |
| 96 | mReadCounterParcelable.readFromParcel(parcel); |
| 97 | mWriteCounterParcelable.readFromParcel(parcel); |
| 98 | mDataParcelable.readFromParcel(parcel); |
| 99 | } |
| 100 | return NO_ERROR; // TODO check for errors above |
| 101 | } |
| 102 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 103 | aaudio_result_t RingBufferParcelable::resolve(SharedMemoryParcelable *memoryParcels, RingBufferDescriptor *descriptor) { |
| 104 | aaudio_result_t result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 105 | |
| 106 | result = mReadCounterParcelable.resolve(memoryParcels, |
| 107 | (void **) &descriptor->readCounterAddress); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 108 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 109 | return result; |
| 110 | } |
| 111 | |
| 112 | result = mWriteCounterParcelable.resolve(memoryParcels, |
| 113 | (void **) &descriptor->writeCounterAddress); |
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 = mDataParcelable.resolve(memoryParcels, (void **) &descriptor->dataAddress); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 119 | if (result != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 120 | return result; |
| 121 | } |
| 122 | |
| 123 | descriptor->bytesPerFrame = mBytesPerFrame; |
| 124 | descriptor->framesPerBurst = mFramesPerBurst; |
| 125 | descriptor->capacityInFrames = mCapacityInFrames; |
| 126 | descriptor->flags = mFlags; |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 127 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 128 | } |
| 129 | |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 130 | aaudio_result_t RingBufferParcelable::validate() { |
| 131 | aaudio_result_t result; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 132 | if (mCapacityInFrames < 0 || mCapacityInFrames >= 32 * 1024) { |
| 133 | ALOGE("RingBufferParcelable invalid mCapacityInFrames = %d", mCapacityInFrames); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 134 | return AAUDIO_ERROR_INTERNAL; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 135 | } |
| 136 | if (mBytesPerFrame < 0 || mBytesPerFrame >= 256) { |
| 137 | ALOGE("RingBufferParcelable invalid mBytesPerFrame = %d", mBytesPerFrame); |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 138 | return AAUDIO_ERROR_INTERNAL; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 139 | } |
| 140 | if (mFramesPerBurst < 0 || mFramesPerBurst >= 1024) { |
| 141 | ALOGE("RingBufferParcelable invalid mFramesPerBurst = %d", mFramesPerBurst); |
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 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 144 | if ((result = mReadCounterParcelable.validate()) != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 145 | ALOGE("RingBufferParcelable invalid mReadCounterParcelable = %d", result); |
| 146 | return result; |
| 147 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 148 | if ((result = mWriteCounterParcelable.validate()) != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 149 | ALOGE("RingBufferParcelable invalid mWriteCounterParcelable = %d", result); |
| 150 | return result; |
| 151 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 152 | if ((result = mDataParcelable.validate()) != AAUDIO_OK) { |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 153 | ALOGE("RingBufferParcelable invalid mDataParcelable = %d", result); |
| 154 | return result; |
| 155 | } |
Phil Burk | 5ed503c | 2017-02-01 09:38:15 -0800 | [diff] [blame] | 156 | return AAUDIO_OK; |
Phil Burk | 204a163 | 2017-01-03 17:23:43 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | |
| 160 | void RingBufferParcelable::dump() { |
| 161 | ALOGD("RingBufferParcelable mCapacityInFrames = %d ---------", mCapacityInFrames); |
| 162 | if (mCapacityInFrames > 0) { |
| 163 | ALOGD("RingBufferParcelable mBytesPerFrame = %d", mBytesPerFrame); |
| 164 | ALOGD("RingBufferParcelable mFramesPerBurst = %d", mFramesPerBurst); |
| 165 | ALOGD("RingBufferParcelable mFlags = %u", mFlags); |
| 166 | mReadCounterParcelable.dump(); |
| 167 | mWriteCounterParcelable.dump(); |
| 168 | mDataParcelable.dump(); |
| 169 | } |
| 170 | } |