blob: 6b74b2171dea38eff74caeaa4c4c41490fd3d66a [file] [log] [blame]
Phil Burk204a1632017-01-03 17:23:43 -08001/*
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 Burkc0c70e32017-02-09 13:18:38 -080017#define LOG_TAG "AAudio"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burk204a1632017-01-03 17:23:43 -080021#include <stdint.h>
22
23#include <binder/Parcelable.h>
24
Phil Burk5ed503c2017-02-01 09:38:15 -080025#include "binding/AAudioServiceDefinitions.h"
Phil Burk204a1632017-01-03 17:23:43 -080026#include "binding/SharedRegionParcelable.h"
27#include "binding/RingBufferParcelable.h"
28
Phil Burk5ed503c2017-02-01 09:38:15 -080029using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080030
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 {
Phil Burk5204d312017-05-04 17:16:13 -070082 status_t status = parcel->writeInt32(mCapacityInFrames);
83 if (status != NO_ERROR) goto error;
Phil Burk204a1632017-01-03 17:23:43 -080084 if (mCapacityInFrames > 0) {
Phil Burk5204d312017-05-04 17:16:13 -070085 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 Burk204a1632017-01-03 17:23:43 -080097 }
Phil Burk5204d312017-05-04 17:16:13 -070098 return NO_ERROR;
99error:
100 ALOGE("RingBufferParcelable::writeToParcel() error = %d", status);
101 return status;
Phil Burk204a1632017-01-03 17:23:43 -0800102}
103
104status_t RingBufferParcelable::readFromParcel(const Parcel* parcel) {
Phil Burk5204d312017-05-04 17:16:13 -0700105 status_t status = parcel->readInt32(&mCapacityInFrames);
106 if (status != NO_ERROR) goto error;
Phil Burk204a1632017-01-03 17:23:43 -0800107 if (mCapacityInFrames > 0) {
Phil Burk5204d312017-05-04 17:16:13 -0700108 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 Burk204a1632017-01-03 17:23:43 -0800120 }
Phil Burk5204d312017-05-04 17:16:13 -0700121 return NO_ERROR;
122error:
123 ALOGE("RingBufferParcelable::readFromParcel() error = %d", status);
124 return status;
Phil Burk204a1632017-01-03 17:23:43 -0800125}
126
Phil Burk5ed503c2017-02-01 09:38:15 -0800127aaudio_result_t RingBufferParcelable::resolve(SharedMemoryParcelable *memoryParcels, RingBufferDescriptor *descriptor) {
128 aaudio_result_t result;
Phil Burk204a1632017-01-03 17:23:43 -0800129
130 result = mReadCounterParcelable.resolve(memoryParcels,
131 (void **) &descriptor->readCounterAddress);
Phil Burk5ed503c2017-02-01 09:38:15 -0800132 if (result != AAUDIO_OK) {
Phil Burk204a1632017-01-03 17:23:43 -0800133 return result;
134 }
135
136 result = mWriteCounterParcelable.resolve(memoryParcels,
137 (void **) &descriptor->writeCounterAddress);
Phil Burk5ed503c2017-02-01 09:38:15 -0800138 if (result != AAUDIO_OK) {
Phil Burk204a1632017-01-03 17:23:43 -0800139 return result;
140 }
141
142 result = mDataParcelable.resolve(memoryParcels, (void **) &descriptor->dataAddress);
Phil Burk5ed503c2017-02-01 09:38:15 -0800143 if (result != AAUDIO_OK) {
Phil Burk204a1632017-01-03 17:23:43 -0800144 return result;
145 }
146
147 descriptor->bytesPerFrame = mBytesPerFrame;
148 descriptor->framesPerBurst = mFramesPerBurst;
149 descriptor->capacityInFrames = mCapacityInFrames;
150 descriptor->flags = mFlags;
Phil Burk5ed503c2017-02-01 09:38:15 -0800151 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800152}
153
Phil Burk5ed503c2017-02-01 09:38:15 -0800154aaudio_result_t RingBufferParcelable::validate() {
155 aaudio_result_t result;
Phil Burk204a1632017-01-03 17:23:43 -0800156 if (mCapacityInFrames < 0 || mCapacityInFrames >= 32 * 1024) {
157 ALOGE("RingBufferParcelable invalid mCapacityInFrames = %d", mCapacityInFrames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800158 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -0800159 }
160 if (mBytesPerFrame < 0 || mBytesPerFrame >= 256) {
161 ALOGE("RingBufferParcelable invalid mBytesPerFrame = %d", mBytesPerFrame);
Phil Burk5ed503c2017-02-01 09:38:15 -0800162 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -0800163 }
Phil Burk5204d312017-05-04 17:16:13 -0700164 if (mFramesPerBurst < 0 || mFramesPerBurst >= 16 * 1024) {
Phil Burk204a1632017-01-03 17:23:43 -0800165 ALOGE("RingBufferParcelable invalid mFramesPerBurst = %d", mFramesPerBurst);
Phil Burk5ed503c2017-02-01 09:38:15 -0800166 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -0800167 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800168 if ((result = mReadCounterParcelable.validate()) != AAUDIO_OK) {
Phil Burk204a1632017-01-03 17:23:43 -0800169 ALOGE("RingBufferParcelable invalid mReadCounterParcelable = %d", result);
170 return result;
171 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800172 if ((result = mWriteCounterParcelable.validate()) != AAUDIO_OK) {
Phil Burk204a1632017-01-03 17:23:43 -0800173 ALOGE("RingBufferParcelable invalid mWriteCounterParcelable = %d", result);
174 return result;
175 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800176 if ((result = mDataParcelable.validate()) != AAUDIO_OK) {
Phil Burk204a1632017-01-03 17:23:43 -0800177 ALOGE("RingBufferParcelable invalid mDataParcelable = %d", result);
178 return result;
179 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800180 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800181}
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}