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