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 "RingBufferParcelable"
18 //#define LOG_NDEBUG 0
19 #include <utils/Log.h>
20
21 #include <stdint.h>
22
23 #include <binder/Parcelable.h>
24 #include <utility/AAudioUtilities.h>
25
26 #include "binding/AAudioServiceDefinitions.h"
27 #include "binding/SharedRegionParcelable.h"
28 #include "binding/RingBufferParcelable.h"
29
30 using namespace aaudio;
31
RingBufferParcelable(const RingBuffer & parcelable)32 RingBufferParcelable::RingBufferParcelable(const RingBuffer& parcelable)
33 : mReadCounterParcelable(std::move(parcelable.readCounterParcelable)),
34 mWriteCounterParcelable(std::move(parcelable.writeCounterParcelable)),
35 mDataParcelable(std::move(parcelable.dataParcelable)),
36 mBytesPerFrame(parcelable.bytesPerFrame),
37 mFramesPerBurst(parcelable.framesPerBurst),
38 mCapacityInFrames(parcelable.capacityInFrames),
39 mFlags(static_cast<RingbufferFlags>(parcelable.flags)) {
40 static_assert(sizeof(mFlags) == sizeof(parcelable.flags));
41 }
42
parcelable() const43 RingBuffer RingBufferParcelable::parcelable() const {
44 RingBuffer result;
45 result.readCounterParcelable = std::move(mReadCounterParcelable).parcelable();
46 result.writeCounterParcelable = std::move(mWriteCounterParcelable).parcelable();
47 result.dataParcelable = std::move(mDataParcelable).parcelable();
48 result.bytesPerFrame = mBytesPerFrame;
49 result.framesPerBurst = mFramesPerBurst;
50 result.capacityInFrames = mCapacityInFrames;
51 static_assert(sizeof(mFlags) == sizeof(result.flags));
52 result.flags = static_cast<int32_t>(mFlags);
53 return result;
54 }
55
56 // TODO This assumes that all three use the same SharedMemoryParcelable
setupMemory(int32_t sharedMemoryIndex,int32_t dataMemoryOffset,int32_t dataSizeInBytes,int32_t readCounterOffset,int32_t writeCounterOffset,int32_t counterSizeBytes)57 void RingBufferParcelable::setupMemory(int32_t sharedMemoryIndex,
58 int32_t dataMemoryOffset,
59 int32_t dataSizeInBytes,
60 int32_t readCounterOffset,
61 int32_t writeCounterOffset,
62 int32_t counterSizeBytes) {
63 mReadCounterParcelable.setup(sharedMemoryIndex, readCounterOffset, counterSizeBytes);
64 mWriteCounterParcelable.setup(sharedMemoryIndex, writeCounterOffset, counterSizeBytes);
65 mDataParcelable.setup(sharedMemoryIndex, dataMemoryOffset, dataSizeInBytes);
66 }
67
setupMemory(int32_t sharedMemoryIndex,int32_t dataMemoryOffset,int32_t dataSizeInBytes)68 void RingBufferParcelable::setupMemory(int32_t sharedMemoryIndex,
69 int32_t dataMemoryOffset,
70 int32_t dataSizeInBytes) {
71 mReadCounterParcelable.setup(sharedMemoryIndex, 0, 0);
72 mWriteCounterParcelable.setup(sharedMemoryIndex, 0, 0);
73 mDataParcelable.setup(sharedMemoryIndex, dataMemoryOffset, dataSizeInBytes);
74 }
75
getBytesPerFrame()76 int32_t RingBufferParcelable::getBytesPerFrame() {
77 return mBytesPerFrame;
78 }
79
setBytesPerFrame(int32_t bytesPerFrame)80 void RingBufferParcelable::setBytesPerFrame(int32_t bytesPerFrame) {
81 mBytesPerFrame = bytesPerFrame;
82 }
83
getFramesPerBurst()84 int32_t RingBufferParcelable::getFramesPerBurst() {
85 return mFramesPerBurst;
86 }
87
setFramesPerBurst(int32_t framesPerBurst)88 void RingBufferParcelable::setFramesPerBurst(int32_t framesPerBurst) {
89 mFramesPerBurst = framesPerBurst;
90 }
91
getCapacityInFrames()92 int32_t RingBufferParcelable::getCapacityInFrames() {
93 return mCapacityInFrames;
94 }
95
setCapacityInFrames(int32_t capacityInFrames)96 void RingBufferParcelable::setCapacityInFrames(int32_t capacityInFrames) {
97 mCapacityInFrames = capacityInFrames;
98 }
99
resolve(SharedMemoryParcelable * memoryParcels,RingBufferDescriptor * descriptor)100 aaudio_result_t RingBufferParcelable::resolve(SharedMemoryParcelable *memoryParcels, RingBufferDescriptor *descriptor) {
101 aaudio_result_t result;
102
103 result = mReadCounterParcelable.resolve(memoryParcels,
104 (void **) &descriptor->readCounterAddress);
105 if (result != AAUDIO_OK) {
106 return result;
107 }
108
109 result = mWriteCounterParcelable.resolve(memoryParcels,
110 (void **) &descriptor->writeCounterAddress);
111 if (result != AAUDIO_OK) {
112 return result;
113 }
114
115 result = mDataParcelable.resolve(memoryParcels, (void **) &descriptor->dataAddress);
116 if (result != AAUDIO_OK) {
117 return result;
118 }
119
120 descriptor->bytesPerFrame = mBytesPerFrame;
121 descriptor->framesPerBurst = mFramesPerBurst;
122 descriptor->capacityInFrames = mCapacityInFrames;
123 descriptor->flags = mFlags;
124 return AAUDIO_OK;
125 }
126
validate() const127 aaudio_result_t RingBufferParcelable::validate() const {
128 if (mCapacityInFrames < 0 || mCapacityInFrames >= 32 * 1024) {
129 ALOGE("invalid mCapacityInFrames = %d", mCapacityInFrames);
130 return AAUDIO_ERROR_INTERNAL;
131 }
132 if (mBytesPerFrame < 0 || mBytesPerFrame >= 256) {
133 ALOGE("invalid mBytesPerFrame = %d", mBytesPerFrame);
134 return AAUDIO_ERROR_INTERNAL;
135 }
136 if (mFramesPerBurst < 0 || mFramesPerBurst >= 16 * 1024) {
137 ALOGE("invalid mFramesPerBurst = %d", mFramesPerBurst);
138 return AAUDIO_ERROR_INTERNAL;
139 }
140 return AAUDIO_OK;
141 }
142
143
dump()144 void RingBufferParcelable::dump() {
145 ALOGD("mCapacityInFrames = %d ---------", mCapacityInFrames);
146 if (mCapacityInFrames > 0) {
147 ALOGD("mBytesPerFrame = %d", mBytesPerFrame);
148 ALOGD("mFramesPerBurst = %d", mFramesPerBurst);
149 ALOGD("mFlags = %u", mFlags);
150 mReadCounterParcelable.dump();
151 mWriteCounterParcelable.dump();
152 mDataParcelable.dump();
153 }
154 }
155