1 /*
2 * Copyright (C) 2020 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 #include <binding/AAudioBinderAdapter.h>
18 #include <media/AidlConversionUtil.h>
19 #include <utility/AAudioUtilities.h>
20
21 namespace aaudio {
22
23 using android::aidl_utils::statusTFromBinderStatus;
24 using android::binder::Status;
25
AAudioBinderAdapter(IAAudioService * delegate,int32_t serviceLifetimeId)26 AAudioBinderAdapter::AAudioBinderAdapter(IAAudioService* delegate,
27 int32_t serviceLifetimeId)
28 : mDelegate(delegate), mServiceLifetimeId(serviceLifetimeId) {}
29
registerClient(const android::sp<IAAudioClient> & client)30 void AAudioBinderAdapter::registerClient(const android::sp<IAAudioClient>& client) {
31 mDelegate->registerClient(client);
32 }
33
openStream(const AAudioStreamRequest & request,AAudioStreamConfiguration & config)34 AAudioHandleInfo AAudioBinderAdapter::openStream(const AAudioStreamRequest& request,
35 AAudioStreamConfiguration& config) {
36 aaudio_handle_t result;
37 StreamParameters params;
38 Status status = mDelegate->openStream(request.parcelable(),
39 ¶ms,
40 &result);
41 if (!status.isOk()) {
42 result = AAudioConvert_androidToAAudioResult(statusTFromBinderStatus(status));
43 }
44 config = params;
45 return {mServiceLifetimeId, result};
46 }
47
closeStream(const AAudioHandleInfo & streamHandleInfo)48 aaudio_result_t AAudioBinderAdapter::closeStream(const AAudioHandleInfo& streamHandleInfo) {
49 if (streamHandleInfo.getServiceLifetimeId() != mServiceLifetimeId) {
50 return AAUDIO_ERROR_DISCONNECTED;
51 }
52 aaudio_result_t result;
53 Status status = mDelegate->closeStream(streamHandleInfo.getHandle(), &result);
54 if (!status.isOk()) {
55 result = AAudioConvert_androidToAAudioResult(statusTFromBinderStatus(status));
56 }
57 return result;
58 }
59
getStreamDescription(const AAudioHandleInfo & streamHandleInfo,AudioEndpointParcelable & endpointOut)60 aaudio_result_t AAudioBinderAdapter::getStreamDescription(const AAudioHandleInfo& streamHandleInfo,
61 AudioEndpointParcelable& endpointOut) {
62 if (streamHandleInfo.getServiceLifetimeId() != mServiceLifetimeId) {
63 return AAUDIO_ERROR_DISCONNECTED;
64 }
65 aaudio_result_t result;
66 Endpoint endpoint;
67 Status status = mDelegate->getStreamDescription(streamHandleInfo.getHandle(),
68 &endpoint,
69 &result);
70 if (!status.isOk()) {
71 result = AAudioConvert_androidToAAudioResult(statusTFromBinderStatus(status));
72 }
73 endpointOut = std::move(endpoint);
74 return result;
75 }
76
startStream(const AAudioHandleInfo & streamHandleInfo)77 aaudio_result_t AAudioBinderAdapter::startStream(const AAudioHandleInfo& streamHandleInfo) {
78 if (streamHandleInfo.getServiceLifetimeId() != mServiceLifetimeId) {
79 return AAUDIO_ERROR_DISCONNECTED;
80 }
81 aaudio_result_t result;
82 Status status = mDelegate->startStream(streamHandleInfo.getHandle(), &result);
83 if (!status.isOk()) {
84 result = AAudioConvert_androidToAAudioResult(statusTFromBinderStatus(status));
85 }
86 return result;
87 }
88
pauseStream(const AAudioHandleInfo & streamHandleInfo)89 aaudio_result_t AAudioBinderAdapter::pauseStream(const AAudioHandleInfo& streamHandleInfo) {
90 if (streamHandleInfo.getServiceLifetimeId() != mServiceLifetimeId) {
91 return AAUDIO_ERROR_DISCONNECTED;
92 }
93 aaudio_result_t result;
94 Status status = mDelegate->pauseStream(streamHandleInfo.getHandle(), &result);
95 if (!status.isOk()) {
96 result = AAudioConvert_androidToAAudioResult(statusTFromBinderStatus(status));
97 }
98 return result;
99 }
100
stopStream(const AAudioHandleInfo & streamHandleInfo)101 aaudio_result_t AAudioBinderAdapter::stopStream(const AAudioHandleInfo& streamHandleInfo) {
102 if (streamHandleInfo.getServiceLifetimeId() != mServiceLifetimeId) {
103 return AAUDIO_ERROR_DISCONNECTED;
104 }
105 aaudio_result_t result;
106 Status status = mDelegate->stopStream(streamHandleInfo.getHandle(), &result);
107 if (!status.isOk()) {
108 result = AAudioConvert_androidToAAudioResult(statusTFromBinderStatus(status));
109 }
110 return result;
111 }
112
flushStream(const AAudioHandleInfo & streamHandleInfo)113 aaudio_result_t AAudioBinderAdapter::flushStream(const AAudioHandleInfo& streamHandleInfo) {
114 if (streamHandleInfo.getServiceLifetimeId() != mServiceLifetimeId) {
115 return AAUDIO_ERROR_DISCONNECTED;
116 }
117 aaudio_result_t result;
118 Status status = mDelegate->flushStream(streamHandleInfo.getHandle(), &result);
119 if (!status.isOk()) {
120 result = AAudioConvert_androidToAAudioResult(statusTFromBinderStatus(status));
121 }
122 return result;
123 }
124
registerAudioThread(const AAudioHandleInfo & streamHandleInfo,pid_t clientThreadId,int64_t periodNanoseconds)125 aaudio_result_t AAudioBinderAdapter::registerAudioThread(const AAudioHandleInfo& streamHandleInfo,
126 pid_t clientThreadId,
127 int64_t periodNanoseconds) {
128 if (streamHandleInfo.getServiceLifetimeId() != mServiceLifetimeId) {
129 return AAUDIO_ERROR_DISCONNECTED;
130 }
131 aaudio_result_t result;
132 Status status = mDelegate->registerAudioThread(
133 streamHandleInfo.getHandle(), clientThreadId, periodNanoseconds, &result);
134 if (!status.isOk()) {
135 result = AAudioConvert_androidToAAudioResult(statusTFromBinderStatus(status));
136 }
137 return result;
138 }
139
unregisterAudioThread(const AAudioHandleInfo & streamHandleInfo,pid_t clientThreadId)140 aaudio_result_t AAudioBinderAdapter::unregisterAudioThread(const AAudioHandleInfo& streamHandleInfo,
141 pid_t clientThreadId) {
142 if (streamHandleInfo.getServiceLifetimeId() != mServiceLifetimeId) {
143 return AAUDIO_ERROR_DISCONNECTED;
144 }
145 aaudio_result_t result;
146 Status status = mDelegate->unregisterAudioThread(
147 streamHandleInfo.getHandle(), clientThreadId, &result);
148 if (!status.isOk()) {
149 result = AAudioConvert_androidToAAudioResult(statusTFromBinderStatus(status));
150 }
151 return result;
152 }
153
exitStandby(const AAudioHandleInfo & streamHandleInfo,AudioEndpointParcelable & endpointOut)154 aaudio_result_t AAudioBinderAdapter::exitStandby(const AAudioHandleInfo& streamHandleInfo,
155 AudioEndpointParcelable &endpointOut) {
156 if (streamHandleInfo.getServiceLifetimeId() != mServiceLifetimeId) {
157 return AAUDIO_ERROR_DISCONNECTED;
158 }
159 aaudio_result_t result;
160 Endpoint endpoint;
161 Status status = mDelegate->exitStandby(streamHandleInfo.getHandle(), &endpoint, &result);
162 if (!status.isOk()) {
163 result = AAudioConvert_androidToAAudioResult(statusTFromBinderStatus(status));
164 }
165 endpointOut = std::move(endpoint);
166 return result;
167 }
168
169 } // namespace aaudio
170