blob: 4996b3f50700f08700ca83f24144edb39baa6712 [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 Burkfbf031e2017-10-12 15:58:31 -070017#define LOG_TAG "RingBufferParcelable"
Phil Burkc0c70e32017-02-09 13:18:38 -080018//#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>
Phil Burka5891f42018-03-12 17:21:11 -070024#include <utility/AAudioUtilities.h>
Phil Burk204a1632017-01-03 17:23:43 -080025
Phil Burk5ed503c2017-02-01 09:38:15 -080026#include "binding/AAudioServiceDefinitions.h"
Phil Burk204a1632017-01-03 17:23:43 -080027#include "binding/SharedRegionParcelable.h"
28#include "binding/RingBufferParcelable.h"
29
Phil Burk5ed503c2017-02-01 09:38:15 -080030using namespace aaudio;
Phil Burk204a1632017-01-03 17:23:43 -080031
32RingBufferParcelable::RingBufferParcelable() {}
33RingBufferParcelable::~RingBufferParcelable() {}
34
35// TODO This assumes that all three use the same SharedMemoryParcelable
36void 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
47void 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
55int32_t RingBufferParcelable::getBytesPerFrame() {
56 return mBytesPerFrame;
57}
58
59void RingBufferParcelable::setBytesPerFrame(int32_t bytesPerFrame) {
60 mBytesPerFrame = bytesPerFrame;
61}
62
63int32_t RingBufferParcelable::getFramesPerBurst() {
64 return mFramesPerBurst;
65}
66
67void RingBufferParcelable::setFramesPerBurst(int32_t framesPerBurst) {
68 mFramesPerBurst = framesPerBurst;
69}
70
71int32_t RingBufferParcelable::getCapacityInFrames() {
72 return mCapacityInFrames;
73}
74
75void RingBufferParcelable::setCapacityInFrames(int32_t capacityInFrames) {
76 mCapacityInFrames = capacityInFrames;
77}
78
79/**
80 * The read and write must be symmetric.
81 */
82status_t RingBufferParcelable::writeToParcel(Parcel* parcel) const {
Phil Burka5891f42018-03-12 17:21:11 -070083 status_t status = AAudioConvert_aaudioToAndroidStatus(validate());
84 if (status != NO_ERROR) goto error;
85
86 status = parcel->writeInt32(mCapacityInFrames);
Phil Burk5204d312017-05-04 17:16:13 -070087 if (status != NO_ERROR) goto error;
Phil Burk204a1632017-01-03 17:23:43 -080088 if (mCapacityInFrames > 0) {
Phil Burk5204d312017-05-04 17:16:13 -070089 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 Burk204a1632017-01-03 17:23:43 -0800101 }
Phil Burk5204d312017-05-04 17:16:13 -0700102 return NO_ERROR;
103error:
Phil Burka5891f42018-03-12 17:21:11 -0700104 ALOGE("%s returning %d", __func__, status);
Phil Burk5204d312017-05-04 17:16:13 -0700105 return status;
Phil Burk204a1632017-01-03 17:23:43 -0800106}
107
108status_t RingBufferParcelable::readFromParcel(const Parcel* parcel) {
Phil Burk5204d312017-05-04 17:16:13 -0700109 status_t status = parcel->readInt32(&mCapacityInFrames);
110 if (status != NO_ERROR) goto error;
Phil Burk204a1632017-01-03 17:23:43 -0800111 if (mCapacityInFrames > 0) {
Phil Burk5204d312017-05-04 17:16:13 -0700112 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 Burk204a1632017-01-03 17:23:43 -0800124 }
Phil Burka5891f42018-03-12 17:21:11 -0700125 return AAudioConvert_aaudioToAndroidStatus(validate());
Phil Burk5204d312017-05-04 17:16:13 -0700126error:
Phil Burka5891f42018-03-12 17:21:11 -0700127 ALOGE("%s returning %d", __func__, status);
Phil Burk5204d312017-05-04 17:16:13 -0700128 return status;
Phil Burk204a1632017-01-03 17:23:43 -0800129}
130
Phil Burk5ed503c2017-02-01 09:38:15 -0800131aaudio_result_t RingBufferParcelable::resolve(SharedMemoryParcelable *memoryParcels, RingBufferDescriptor *descriptor) {
132 aaudio_result_t result;
Phil Burk204a1632017-01-03 17:23:43 -0800133
134 result = mReadCounterParcelable.resolve(memoryParcels,
135 (void **) &descriptor->readCounterAddress);
Phil Burk5ed503c2017-02-01 09:38:15 -0800136 if (result != AAUDIO_OK) {
Phil Burk204a1632017-01-03 17:23:43 -0800137 return result;
138 }
139
140 result = mWriteCounterParcelable.resolve(memoryParcels,
141 (void **) &descriptor->writeCounterAddress);
Phil Burk5ed503c2017-02-01 09:38:15 -0800142 if (result != AAUDIO_OK) {
Phil Burk204a1632017-01-03 17:23:43 -0800143 return result;
144 }
145
146 result = mDataParcelable.resolve(memoryParcels, (void **) &descriptor->dataAddress);
Phil Burk5ed503c2017-02-01 09:38:15 -0800147 if (result != AAUDIO_OK) {
Phil Burk204a1632017-01-03 17:23:43 -0800148 return result;
149 }
150
151 descriptor->bytesPerFrame = mBytesPerFrame;
152 descriptor->framesPerBurst = mFramesPerBurst;
153 descriptor->capacityInFrames = mCapacityInFrames;
154 descriptor->flags = mFlags;
Phil Burk5ed503c2017-02-01 09:38:15 -0800155 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800156}
157
Phil Burka5891f42018-03-12 17:21:11 -0700158aaudio_result_t RingBufferParcelable::validate() const {
Phil Burk204a1632017-01-03 17:23:43 -0800159 if (mCapacityInFrames < 0 || mCapacityInFrames >= 32 * 1024) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700160 ALOGE("invalid mCapacityInFrames = %d", mCapacityInFrames);
Phil Burk5ed503c2017-02-01 09:38:15 -0800161 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -0800162 }
163 if (mBytesPerFrame < 0 || mBytesPerFrame >= 256) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700164 ALOGE("invalid mBytesPerFrame = %d", mBytesPerFrame);
Phil Burk5ed503c2017-02-01 09:38:15 -0800165 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -0800166 }
Phil Burk5204d312017-05-04 17:16:13 -0700167 if (mFramesPerBurst < 0 || mFramesPerBurst >= 16 * 1024) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700168 ALOGE("invalid mFramesPerBurst = %d", mFramesPerBurst);
Phil Burk5ed503c2017-02-01 09:38:15 -0800169 return AAUDIO_ERROR_INTERNAL;
Phil Burk204a1632017-01-03 17:23:43 -0800170 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800171 return AAUDIO_OK;
Phil Burk204a1632017-01-03 17:23:43 -0800172}
173
174
175void RingBufferParcelable::dump() {
Phil Burkfbf031e2017-10-12 15:58:31 -0700176 ALOGD("mCapacityInFrames = %d ---------", mCapacityInFrames);
Phil Burk204a1632017-01-03 17:23:43 -0800177 if (mCapacityInFrames > 0) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700178 ALOGD("mBytesPerFrame = %d", mBytesPerFrame);
179 ALOGD("mFramesPerBurst = %d", mFramesPerBurst);
180 ALOGD("mFlags = %u", mFlags);
Phil Burk204a1632017-01-03 17:23:43 -0800181 mReadCounterParcelable.dump();
182 mWriteCounterParcelable.dump();
183 mDataParcelable.dump();
184 }
185}