• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "AudioEndpointParcelable"
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::base::unique_fd;
32 using android::status_t;
33 
34 using namespace aaudio;
35 
AudioEndpointParcelable(Endpoint && parcelable)36 AudioEndpointParcelable::AudioEndpointParcelable(Endpoint&& parcelable)
37         : mUpMessageQueueParcelable(parcelable.upMessageQueueParcelable),
38           mDownMessageQueueParcelable(parcelable.downMessageQueueParcelable),
39           mUpDataQueueParcelable(parcelable.upDataQueueParcelable),
40           mDownDataQueueParcelable(parcelable.downDataQueueParcelable),
41           mNumSharedMemories(parcelable.sharedMemories.size()) {
42     for (size_t i = 0; i < parcelable.sharedMemories.size() && i < MAX_SHARED_MEMORIES; ++i) {
43         // Re-construct.
44         mSharedMemories[i].~SharedMemoryParcelable();
45         new(&mSharedMemories[i]) SharedMemoryParcelable(std::move(parcelable.sharedMemories[i]));
46     }
47 }
48 
operator =(Endpoint && parcelable)49 AudioEndpointParcelable& AudioEndpointParcelable::operator=(Endpoint&& parcelable) {
50     this->~AudioEndpointParcelable();
51     new(this) AudioEndpointParcelable(std::move(parcelable));
52     return *this;
53 }
54 
parcelable()55 Endpoint AudioEndpointParcelable::parcelable()&& {
56     Endpoint result;
57     result.upMessageQueueParcelable = mUpMessageQueueParcelable.parcelable();
58     result.downMessageQueueParcelable = mDownMessageQueueParcelable.parcelable();
59     result.upDataQueueParcelable = mUpDataQueueParcelable.parcelable();
60     result.downDataQueueParcelable = mDownDataQueueParcelable.parcelable();
61     result.sharedMemories.reserve(std::min(mNumSharedMemories, MAX_SHARED_MEMORIES));
62     for (size_t i = 0; i < mNumSharedMemories && i < MAX_SHARED_MEMORIES; ++i) {
63         result.sharedMemories.emplace_back(std::move(mSharedMemories[i]).parcelable());
64     }
65     return result;
66 }
67 
68 /**
69  * Add the file descriptor to the table.
70  * @return index in table or negative error
71  */
addFileDescriptor(const unique_fd & fd,int32_t sizeInBytes)72 int32_t AudioEndpointParcelable::addFileDescriptor(const unique_fd& fd,
73                                                    int32_t sizeInBytes) {
74     if (mNumSharedMemories >= MAX_SHARED_MEMORIES) {
75         return AAUDIO_ERROR_OUT_OF_RANGE;
76     }
77     int32_t index = mNumSharedMemories++;
78     mSharedMemories[index].setup(fd, sizeInBytes);
79     return index;
80 }
81 
closeDataFileDescriptor()82 void AudioEndpointParcelable::closeDataFileDescriptor() {
83     const int32_t curDataMemoryIndex = mDownDataQueueParcelable.getSharedMemoryIndex();
84     mSharedMemories[curDataMemoryIndex].closeAndReleaseFd();
85 }
86 
updateDataFileDescriptor(AudioEndpointParcelable * endpointParcelable)87 void AudioEndpointParcelable::updateDataFileDescriptor(
88         AudioEndpointParcelable* endpointParcelable) {
89     const int32_t curDataMemoryIndex = mDownDataQueueParcelable.getSharedMemoryIndex();
90     const int32_t newDataMemoryIndex =
91             endpointParcelable->mDownDataQueueParcelable.getSharedMemoryIndex();
92     mSharedMemories[curDataMemoryIndex].close();
93     mSharedMemories[curDataMemoryIndex].setup(
94             endpointParcelable->mSharedMemories[newDataMemoryIndex]);
95     mDownDataQueueParcelable.updateMemory(endpointParcelable->mDownDataQueueParcelable);
96 }
97 
resolve(EndpointDescriptor * descriptor)98 aaudio_result_t AudioEndpointParcelable::resolve(EndpointDescriptor *descriptor) {
99     aaudio_result_t result = mUpMessageQueueParcelable.resolve(mSharedMemories,
100                                                            &descriptor->upMessageQueueDescriptor);
101     if (result != AAUDIO_OK) return result;
102     result = mDownMessageQueueParcelable.resolve(mSharedMemories,
103                                         &descriptor->downMessageQueueDescriptor);
104     if (result != AAUDIO_OK) return result;
105 
106     result = mDownDataQueueParcelable.resolve(mSharedMemories,
107                                               &descriptor->dataQueueDescriptor);
108     return result;
109 }
110 
resolveDataQueue(RingBufferDescriptor * descriptor)111 aaudio_result_t AudioEndpointParcelable::resolveDataQueue(RingBufferDescriptor *descriptor) {
112     return mDownDataQueueParcelable.resolve(mSharedMemories, descriptor);
113 }
114 
close()115 aaudio_result_t AudioEndpointParcelable::close() {
116     int err = 0;
117     for (int i = 0; i < mNumSharedMemories; i++) {
118         int lastErr = mSharedMemories[i].close();
119         if (lastErr < 0) err = lastErr;
120     }
121     return AAudioConvert_androidToAAudioResult(err);
122 }
123 
validate() const124 aaudio_result_t AudioEndpointParcelable::validate() const {
125     if (mNumSharedMemories < 0 || mNumSharedMemories >= MAX_SHARED_MEMORIES) {
126         ALOGE("invalid mNumSharedMemories = %d", mNumSharedMemories);
127         return AAUDIO_ERROR_INTERNAL;
128     }
129     return AAUDIO_OK;
130 }
131 
dump()132 void AudioEndpointParcelable::dump() {
133     ALOGD("======================================= BEGIN");
134     ALOGD("mNumSharedMemories = %d", mNumSharedMemories);
135     for (int i = 0; i < mNumSharedMemories; i++) {
136         mSharedMemories[i].dump();
137     }
138     ALOGD("mUpMessageQueueParcelable =========");
139     mUpMessageQueueParcelable.dump();
140     ALOGD("mDownMessageQueueParcelable =======");
141     mDownMessageQueueParcelable.dump();
142     ALOGD("mUpDataQueueParcelable ============");
143     mUpDataQueueParcelable.dump();
144     ALOGD("mDownDataQueueParcelable ==========");
145     mDownDataQueueParcelable.dump();
146     ALOGD("======================================= END");
147 }
148 
149