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