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/Parcel.h>
24 #include <binder/Parcelable.h>
25 #include <utility/AAudioUtilities.h>
26
27 #include "binding/AAudioServiceDefinitions.h"
28 #include "binding/RingBufferParcelable.h"
29 #include "binding/AudioEndpointParcelable.h"
30
31 using android::NO_ERROR;
32 using android::status_t;
33 using android::Parcel;
34 using android::Parcelable;
35
36 using namespace aaudio;
37
38 /**
39 * Container for information about the message queues plus
40 * general stream information needed by AAudio clients.
41 * It contains no addresses, just sizes, offsets and file descriptors for
42 * shared memory that can be passed through Binder.
43 */
AudioEndpointParcelable()44 AudioEndpointParcelable::AudioEndpointParcelable() {}
45
~AudioEndpointParcelable()46 AudioEndpointParcelable::~AudioEndpointParcelable() {}
47
48 /**
49 * Add the file descriptor to the table.
50 * @return index in table or negative error
51 */
addFileDescriptor(int fd,int32_t sizeInBytes)52 int32_t AudioEndpointParcelable::addFileDescriptor(int fd, int32_t sizeInBytes) {
53 if (mNumSharedMemories >= MAX_SHARED_MEMORIES) {
54 return AAUDIO_ERROR_OUT_OF_RANGE;
55 }
56 int32_t index = mNumSharedMemories++;
57 mSharedMemories[index].setup(fd, sizeInBytes);
58 return index;
59 }
60
61 /**
62 * The read and write must be symmetric.
63 */
writeToParcel(Parcel * parcel) const64 status_t AudioEndpointParcelable::writeToParcel(Parcel* parcel) const {
65 parcel->writeInt32(mNumSharedMemories);
66 for (int i = 0; i < mNumSharedMemories; i++) {
67 mSharedMemories[i].writeToParcel(parcel);
68 }
69 mUpMessageQueueParcelable.writeToParcel(parcel);
70 mDownMessageQueueParcelable.writeToParcel(parcel);
71 mUpDataQueueParcelable.writeToParcel(parcel);
72 mDownDataQueueParcelable.writeToParcel(parcel);
73 return NO_ERROR; // TODO check for errors above
74 }
75
readFromParcel(const Parcel * parcel)76 status_t AudioEndpointParcelable::readFromParcel(const Parcel* parcel) {
77 parcel->readInt32(&mNumSharedMemories);
78 for (int i = 0; i < mNumSharedMemories; i++) {
79 mSharedMemories[i].readFromParcel(parcel);
80 }
81 mUpMessageQueueParcelable.readFromParcel(parcel);
82 mDownMessageQueueParcelable.readFromParcel(parcel);
83 mUpDataQueueParcelable.readFromParcel(parcel);
84 mDownDataQueueParcelable.readFromParcel(parcel);
85 return NO_ERROR; // TODO check for errors above
86 }
87
resolve(EndpointDescriptor * descriptor)88 aaudio_result_t AudioEndpointParcelable::resolve(EndpointDescriptor *descriptor) {
89 aaudio_result_t result = mUpMessageQueueParcelable.resolve(mSharedMemories,
90 &descriptor->upMessageQueueDescriptor);
91 if (result != AAUDIO_OK) return result;
92 result = mDownMessageQueueParcelable.resolve(mSharedMemories,
93 &descriptor->downMessageQueueDescriptor);
94 if (result != AAUDIO_OK) return result;
95
96 result = mDownDataQueueParcelable.resolve(mSharedMemories,
97 &descriptor->dataQueueDescriptor);
98 return result;
99 }
100
close()101 aaudio_result_t AudioEndpointParcelable::close() {
102 int err = 0;
103 for (int i = 0; i < mNumSharedMemories; i++) {
104 int lastErr = mSharedMemories[i].close();
105 if (lastErr < 0) err = lastErr;
106 }
107 return AAudioConvert_androidToAAudioResult(err);
108 }
109
validate()110 aaudio_result_t AudioEndpointParcelable::validate() {
111 aaudio_result_t result;
112 if (mNumSharedMemories < 0 || mNumSharedMemories >= MAX_SHARED_MEMORIES) {
113 ALOGE("AudioEndpointParcelable invalid mNumSharedMemories = %d", mNumSharedMemories);
114 return AAUDIO_ERROR_INTERNAL;
115 }
116 for (int i = 0; i < mNumSharedMemories; i++) {
117 result = mSharedMemories[i].validate();
118 if (result != AAUDIO_OK) {
119 ALOGE("AudioEndpointParcelable invalid mSharedMemories[%d] = %d", i, result);
120 return result;
121 }
122 }
123 if ((result = mUpMessageQueueParcelable.validate()) != AAUDIO_OK) {
124 ALOGE("AudioEndpointParcelable invalid mUpMessageQueueParcelable = %d", result);
125 return result;
126 }
127 if ((result = mDownMessageQueueParcelable.validate()) != AAUDIO_OK) {
128 ALOGE("AudioEndpointParcelable invalid mDownMessageQueueParcelable = %d", result);
129 return result;
130 }
131 if ((result = mUpDataQueueParcelable.validate()) != AAUDIO_OK) {
132 ALOGE("AudioEndpointParcelable invalid mUpDataQueueParcelable = %d", result);
133 return result;
134 }
135 if ((result = mDownDataQueueParcelable.validate()) != AAUDIO_OK) {
136 ALOGE("AudioEndpointParcelable invalid mDownDataQueueParcelable = %d", result);
137 return result;
138 }
139 return AAUDIO_OK;
140 }
141
dump()142 void AudioEndpointParcelable::dump() {
143 ALOGD("AudioEndpointParcelable ======================================= BEGIN");
144 ALOGD("AudioEndpointParcelable mNumSharedMemories = %d", mNumSharedMemories);
145 for (int i = 0; i < mNumSharedMemories; i++) {
146 mSharedMemories[i].dump();
147 }
148 ALOGD("AudioEndpointParcelable mUpMessageQueueParcelable =========");
149 mUpMessageQueueParcelable.dump();
150 ALOGD("AudioEndpointParcelable mDownMessageQueueParcelable =======");
151 mDownMessageQueueParcelable.dump();
152 ALOGD("AudioEndpointParcelable mUpDataQueueParcelable ============");
153 mUpDataQueueParcelable.dump();
154 ALOGD("AudioEndpointParcelable mDownDataQueueParcelable ==========");
155 mDownDataQueueParcelable.dump();
156 ALOGD("AudioEndpointParcelable ======================================= END");
157 }
158
159