RingBufferParcelable.cpp revision 5204d315c6c6f53188f8d1414dd1b55b6c90142b
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#define LOG_TAG "AAudio" 18//#define LOG_NDEBUG 0 19#include <utils/Log.h> 20 21#include <stdint.h> 22 23#include <binder/Parcelable.h> 24 25#include "binding/AAudioServiceDefinitions.h" 26#include "binding/SharedRegionParcelable.h" 27#include "binding/RingBufferParcelable.h" 28 29using namespace aaudio; 30 31RingBufferParcelable::RingBufferParcelable() {} 32RingBufferParcelable::~RingBufferParcelable() {} 33 34// TODO This assumes that all three use the same SharedMemoryParcelable 35void 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 46void 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 54int32_t RingBufferParcelable::getBytesPerFrame() { 55 return mBytesPerFrame; 56} 57 58void RingBufferParcelable::setBytesPerFrame(int32_t bytesPerFrame) { 59 mBytesPerFrame = bytesPerFrame; 60} 61 62int32_t RingBufferParcelable::getFramesPerBurst() { 63 return mFramesPerBurst; 64} 65 66void RingBufferParcelable::setFramesPerBurst(int32_t framesPerBurst) { 67 mFramesPerBurst = framesPerBurst; 68} 69 70int32_t RingBufferParcelable::getCapacityInFrames() { 71 return mCapacityInFrames; 72} 73 74void RingBufferParcelable::setCapacityInFrames(int32_t capacityInFrames) { 75 mCapacityInFrames = capacityInFrames; 76} 77 78/** 79 * The read and write must be symmetric. 80 */ 81status_t RingBufferParcelable::writeToParcel(Parcel* parcel) const { 82 status_t status = parcel->writeInt32(mCapacityInFrames); 83 if (status != NO_ERROR) goto error; 84 if (mCapacityInFrames > 0) { 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; 97 } 98 return NO_ERROR; 99error: 100 ALOGE("RingBufferParcelable::writeToParcel() error = %d", status); 101 return status; 102} 103 104status_t RingBufferParcelable::readFromParcel(const Parcel* parcel) { 105 status_t status = parcel->readInt32(&mCapacityInFrames); 106 if (status != NO_ERROR) goto error; 107 if (mCapacityInFrames > 0) { 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; 120 } 121 return NO_ERROR; 122error: 123 ALOGE("RingBufferParcelable::readFromParcel() error = %d", status); 124 return status; 125} 126 127aaudio_result_t RingBufferParcelable::resolve(SharedMemoryParcelable *memoryParcels, RingBufferDescriptor *descriptor) { 128 aaudio_result_t result; 129 130 result = mReadCounterParcelable.resolve(memoryParcels, 131 (void **) &descriptor->readCounterAddress); 132 if (result != AAUDIO_OK) { 133 return result; 134 } 135 136 result = mWriteCounterParcelable.resolve(memoryParcels, 137 (void **) &descriptor->writeCounterAddress); 138 if (result != AAUDIO_OK) { 139 return result; 140 } 141 142 result = mDataParcelable.resolve(memoryParcels, (void **) &descriptor->dataAddress); 143 if (result != AAUDIO_OK) { 144 return result; 145 } 146 147 descriptor->bytesPerFrame = mBytesPerFrame; 148 descriptor->framesPerBurst = mFramesPerBurst; 149 descriptor->capacityInFrames = mCapacityInFrames; 150 descriptor->flags = mFlags; 151 return AAUDIO_OK; 152} 153 154aaudio_result_t RingBufferParcelable::validate() { 155 aaudio_result_t result; 156 if (mCapacityInFrames < 0 || mCapacityInFrames >= 32 * 1024) { 157 ALOGE("RingBufferParcelable invalid mCapacityInFrames = %d", mCapacityInFrames); 158 return AAUDIO_ERROR_INTERNAL; 159 } 160 if (mBytesPerFrame < 0 || mBytesPerFrame >= 256) { 161 ALOGE("RingBufferParcelable invalid mBytesPerFrame = %d", mBytesPerFrame); 162 return AAUDIO_ERROR_INTERNAL; 163 } 164 if (mFramesPerBurst < 0 || mFramesPerBurst >= 16 * 1024) { 165 ALOGE("RingBufferParcelable invalid mFramesPerBurst = %d", mFramesPerBurst); 166 return AAUDIO_ERROR_INTERNAL; 167 } 168 if ((result = mReadCounterParcelable.validate()) != AAUDIO_OK) { 169 ALOGE("RingBufferParcelable invalid mReadCounterParcelable = %d", result); 170 return result; 171 } 172 if ((result = mWriteCounterParcelable.validate()) != AAUDIO_OK) { 173 ALOGE("RingBufferParcelable invalid mWriteCounterParcelable = %d", result); 174 return result; 175 } 176 if ((result = mDataParcelable.validate()) != AAUDIO_OK) { 177 ALOGE("RingBufferParcelable invalid mDataParcelable = %d", result); 178 return result; 179 } 180 return AAUDIO_OK; 181} 182 183 184void 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} 195