blob: 69e58f6bd78530594c2d4d36d1484a09770d82fe [file] [log] [blame]
Phil Burk5ed503c2017-02-01 09:38:15 -08001/*
2 * Copyright (C) 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 "AAudioService"
18//#define LOG_NDEBUG 0
19#include <utils/Log.h>
20
Phil Burk523b3042017-09-13 13:03:08 -070021#include <iomanip>
22#include <iostream>
Andy Hung47c5e532017-06-26 18:28:00 -070023#include <sstream>
Phil Burk5ed503c2017-02-01 09:38:15 -080024
Phil Burka4eb0d82017-04-12 15:44:06 -070025#include <aaudio/AAudio.h>
Andy Hungab7ef302018-05-15 19:35:29 -070026#include <mediautils/ServiceUtilities.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080027#include <utils/String16.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080028
Phil Burkc0c70e32017-02-09 13:18:38 -080029#include "binding/AAudioServiceMessage.h"
Phil Burk11e8d332017-05-24 09:59:02 -070030#include "AAudioClientTracker.h"
Andy Hung47c5e532017-06-26 18:28:00 -070031#include "AAudioEndpointManager.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080032#include "AAudioService.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080033#include "AAudioServiceStreamMMAP.h"
34#include "AAudioServiceStreamShared.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080035
36using namespace android;
37using namespace aaudio;
38
Phil Burk91692942017-06-30 12:23:05 -070039#define MAX_STREAMS_PER_PROCESS 8
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070040#define AIDL_RETURN(x) *_aidl_return = (x); return Status::ok();
41
Phil Burk91692942017-06-30 12:23:05 -070042
Phil Burk523b3042017-09-13 13:03:08 -070043using android::AAudioService;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070044using binder::Status;
Phil Burk5ed503c2017-02-01 09:38:15 -080045
46android::AAudioService::AAudioService()
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -070047 : BnAAudioService(),
48 mAdapter(this) {
Eric Laurenta54f1282017-07-01 19:39:32 -070049 mAudioClient.clientUid = getuid(); // TODO consider using geteuid()
50 mAudioClient.clientPid = getpid();
51 mAudioClient.packageName = String16("");
Phil Burk11e8d332017-05-24 09:59:02 -070052 AAudioClientTracker::getInstance().setAAudioService(this);
Phil Burk5ed503c2017-02-01 09:38:15 -080053}
54
Andy Hung47c5e532017-06-26 18:28:00 -070055status_t AAudioService::dump(int fd, const Vector<String16>& args) {
56 std::string result;
57
58 if (!dumpAllowed()) {
59 std::stringstream ss;
Andy Hung6357b5f2018-10-22 19:47:04 -070060 ss << "Permission Denial: can't dump AAudioService from pid="
Andy Hung47c5e532017-06-26 18:28:00 -070061 << IPCThreadState::self()->getCallingPid() << ", uid="
62 << IPCThreadState::self()->getCallingUid() << "\n";
63 result = ss.str();
64 ALOGW("%s", result.c_str());
65 } else {
Phil Burk523b3042017-09-13 13:03:08 -070066 result = "------------ AAudio Service ------------\n"
67 + mStreamTracker.dump()
Phil Burk4501b352017-06-29 18:12:36 -070068 + AAudioClientTracker::getInstance().dump()
69 + AAudioEndpointManager::getInstance().dump();
Andy Hung47c5e532017-06-26 18:28:00 -070070 }
71 (void)write(fd, result.c_str(), result.size());
72 return NO_ERROR;
73}
74
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070075Status AAudioService::registerClient(const sp<IAAudioClient> &client) {
Phil Burk11e8d332017-05-24 09:59:02 -070076 pid_t pid = IPCThreadState::self()->getCallingPid();
77 AAudioClientTracker::getInstance().registerClient(pid, client);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070078 return Status::ok();
Phil Burk11e8d332017-05-24 09:59:02 -070079}
80
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070081Status
82AAudioService::openStream(const StreamRequest &_request, StreamParameters* _paramsOut,
83 int32_t *_aidl_return) {
84 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
Phil Burk2ebf6622019-04-17 11:10:25 -070085
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070086 // Create wrapper objects for simple usage of the parcelables.
87 const AAudioStreamRequest request(_request);
88 AAudioStreamConfiguration paramsOut;
89
Phil Burk6e463ce2020-04-13 10:20:20 -070090 // A lock in is used to order the opening of endpoints when an
91 // EXCLUSIVE endpoint is stolen. We want the order to be:
92 // 1) Thread A opens exclusive MMAP endpoint
93 // 2) Thread B wants to open an exclusive MMAP endpoint so it steals the one from A
94 // under this lock.
95 // 3) Thread B opens a shared MMAP endpoint.
96 // 4) Thread A can then get the lock and also open a shared stream.
97 // Without the lock. Thread A might sneak in and reallocate an exclusive stream
98 // before B can open the shared stream.
99 std::unique_lock<std::recursive_mutex> lock(mOpenLock);
100
Phil Burkc0c70e32017-02-09 13:18:38 -0800101 aaudio_result_t result = AAUDIO_OK;
Phil Burk11e8d332017-05-24 09:59:02 -0700102 sp<AAudioServiceStreamBase> serviceStream;
Phil Burkc0c70e32017-02-09 13:18:38 -0800103 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -0700104 bool sharingModeMatchRequired = request.isSharingModeMatchRequired();
Phil Burkc0c70e32017-02-09 13:18:38 -0800105 aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode();
Phil Burkc0c70e32017-02-09 13:18:38 -0800106
Phil Burk91692942017-06-30 12:23:05 -0700107 // Enforce limit on client processes.
108 pid_t pid = request.getProcessId();
Eric Laurenta54f1282017-07-01 19:39:32 -0700109 if (pid != mAudioClient.clientPid) {
Phil Burk91692942017-06-30 12:23:05 -0700110 int32_t count = AAudioClientTracker::getInstance().getStreamCount(pid);
111 if (count >= MAX_STREAMS_PER_PROCESS) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700112 ALOGE("openStream(): exceeded max streams per process %d >= %d",
Phil Burk91692942017-06-30 12:23:05 -0700113 count, MAX_STREAMS_PER_PROCESS);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700114 AIDL_RETURN(AAUDIO_ERROR_UNAVAILABLE);
Phil Burk91692942017-06-30 12:23:05 -0700115 }
116 }
117
Phil Burkc0c70e32017-02-09 13:18:38 -0800118 if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700119 ALOGE("openStream(): unrecognized sharing mode = %d", sharingMode);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700120 AIDL_RETURN(AAUDIO_ERROR_ILLEGAL_ARGUMENT);
Phil Burkc0c70e32017-02-09 13:18:38 -0800121 }
122
Phil Burk836f9df2020-05-29 13:20:28 -0700123 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE
124 && AAudioClientTracker::getInstance().isExclusiveEnabled(request.getProcessId())) {
Eric Laurenta54f1282017-07-01 19:39:32 -0700125 // only trust audioserver for in service indication
126 bool inService = false;
Phil Burk2ebf6622019-04-17 11:10:25 -0700127 if (isCallerInService()) {
Eric Laurenta54f1282017-07-01 19:39:32 -0700128 inService = request.isInService();
129 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700130 serviceStream = new AAudioServiceStreamMMAP(*this, inService);
131 result = serviceStream->open(request);
Phil Burkc0c70e32017-02-09 13:18:38 -0800132 if (result != AAUDIO_OK) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700133 // Clear it so we can possibly fall back to using a shared stream.
Phil Burkfbf031e2017-10-12 15:58:31 -0700134 ALOGW("openStream(), could not open in EXCLUSIVE mode");
Phil Burk11e8d332017-05-24 09:59:02 -0700135 serviceStream.clear();
Phil Burkc0c70e32017-02-09 13:18:38 -0800136 }
137 }
138
Phil Burka3901e92018-10-08 13:54:38 -0700139 // Try SHARED if SHARED requested or if EXCLUSIVE failed.
Phil Burk15f97c92018-09-04 14:06:27 -0700140 if (sharingMode == AAUDIO_SHARING_MODE_SHARED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800141 serviceStream = new AAudioServiceStreamShared(*this);
Phil Burk39f02dd2017-08-04 09:13:31 -0700142 result = serviceStream->open(request);
Phil Burk15f97c92018-09-04 14:06:27 -0700143 } else if (serviceStream.get() == nullptr && !sharingModeMatchRequired) {
144 aaudio::AAudioStreamRequest modifiedRequest = request;
145 // Overwrite the original EXCLUSIVE mode with SHARED.
146 modifiedRequest.getConfiguration().setSharingMode(AAUDIO_SHARING_MODE_SHARED);
147 serviceStream = new AAudioServiceStreamShared(*this);
148 result = serviceStream->open(modifiedRequest);
Phil Burkc0c70e32017-02-09 13:18:38 -0800149 }
150
151 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700152 serviceStream.clear();
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700153 AIDL_RETURN(result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800154 } else {
Phil Burk523b3042017-09-13 13:03:08 -0700155 aaudio_handle_t handle = mStreamTracker.addStreamForHandle(serviceStream.get());
Phil Burk523b3042017-09-13 13:03:08 -0700156 serviceStream->setHandle(handle);
157 pid_t pid = request.getProcessId();
158 AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700159 paramsOut.copyFrom(*serviceStream);
160 *_paramsOut = std::move(paramsOut).parcelable();
Phil Burka9876702020-04-20 18:16:15 -0700161 // Log open in MediaMetrics after we have the handle because we need the handle to
162 // create the metrics ID.
163 serviceStream->logOpen(handle);
Phil Burk6e463ce2020-04-13 10:20:20 -0700164 ALOGV("%s(): return handle = 0x%08X", __func__, handle);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700165 AIDL_RETURN(handle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800166 }
167}
168
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700169Status AAudioService::closeStream(int32_t streamHandle, int32_t *_aidl_return) {
170 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
171
Phil Burk98d6d922017-07-06 11:52:45 -0700172 // Check permission and ownership first.
173 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700174 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700175 ALOGE("closeStream(0x%0x), illegal stream handle", streamHandle);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700176 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
Phil Burk91692942017-06-30 12:23:05 -0700177 }
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700178 AIDL_RETURN(closeStream(serviceStream));
179}
180
181Status AAudioService::getStreamDescription(int32_t streamHandle, Endpoint* endpoint,
182 int32_t *_aidl_return) {
183 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
184
185 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
186 if (serviceStream.get() == nullptr) {
187 ALOGE("getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
188 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
189 }
190 AudioEndpointParcelable endpointParcelable;
191 aaudio_result_t result = serviceStream->getDescription(endpointParcelable);
192 if (result == AAUDIO_OK) {
193 *endpoint = std::move(endpointParcelable).parcelable();
194 }
195 AIDL_RETURN(result);
196}
197
198Status AAudioService::startStream(int32_t streamHandle, int32_t *_aidl_return) {
199 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
200
201 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
202 if (serviceStream.get() == nullptr) {
203 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
204 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
205 }
206 AIDL_RETURN(serviceStream->start());
207}
208
209Status AAudioService::pauseStream(int32_t streamHandle, int32_t *_aidl_return) {
210 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
211
212 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
213 if (serviceStream.get() == nullptr) {
214 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
215 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
216 }
217 AIDL_RETURN(serviceStream->pause());
218}
219
220Status AAudioService::stopStream(int32_t streamHandle, int32_t *_aidl_return) {
221 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
222
223 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
224 if (serviceStream.get() == nullptr) {
225 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
226 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
227 }
228 AIDL_RETURN(serviceStream->stop());
229}
230
231Status AAudioService::flushStream(int32_t streamHandle, int32_t *_aidl_return) {
232 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
233
234 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
235 if (serviceStream.get() == nullptr) {
236 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
237 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
238 }
239 AIDL_RETURN(serviceStream->flush());
240}
241
242Status AAudioService::registerAudioThread(int32_t streamHandle, int32_t clientThreadId, int64_t periodNanoseconds,
243 int32_t *_aidl_return) {
244 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
245
246 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
247 if (serviceStream.get() == nullptr) {
248 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
249 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
250 }
251 int32_t priority = isCallerInService()
252 ? kRealTimeAudioPriorityService : kRealTimeAudioPriorityClient;
253 AIDL_RETURN(serviceStream->registerAudioThread(clientThreadId, priority));
254}
255
256Status AAudioService::unregisterAudioThread(int32_t streamHandle, int32_t clientThreadId,
257 int32_t *_aidl_return) {
258 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
259
260 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
261 if (serviceStream.get() == nullptr) {
262 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
263 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
264 }
265 AIDL_RETURN(serviceStream->unregisterAudioThread(clientThreadId));
266}
267
268bool AAudioService::isCallerInService() {
269 return mAudioClient.clientPid == IPCThreadState::self()->getCallingPid() &&
270 mAudioClient.clientUid == IPCThreadState::self()->getCallingUid();
Phil Burk6e463ce2020-04-13 10:20:20 -0700271}
Phil Burk91692942017-06-30 12:23:05 -0700272
Phil Burk6e463ce2020-04-13 10:20:20 -0700273aaudio_result_t AAudioService::closeStream(sp<AAudioServiceStreamBase> serviceStream) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700274 // This is protected by a lock in AAudioClientTracker.
275 // It is safe to unregister the same stream twice.
Phil Burk94862522017-09-13 21:31:36 -0700276 pid_t pid = serviceStream->getOwnerProcessId();
277 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk7ebbc112020-05-13 15:55:17 -0700278 // This is protected by a lock in mStreamTracker.
279 // It is safe to remove the same stream twice.
280 mStreamTracker.removeStreamByHandle(serviceStream->getHandle());
Phil Burk5ed503c2017-02-01 09:38:15 -0800281
Phil Burk7ebbc112020-05-13 15:55:17 -0700282 return serviceStream->close();
Phil Burk94862522017-09-13 21:31:36 -0700283}
Phil Burk523b3042017-09-13 13:03:08 -0700284
285sp<AAudioServiceStreamBase> AAudioService::convertHandleToServiceStream(
286 aaudio_handle_t streamHandle) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700287 sp<AAudioServiceStreamBase> serviceStream = mStreamTracker.getStreamByHandle(
Phil Burk2fe718b2018-05-14 12:28:32 -0700288 streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700289 if (serviceStream.get() != nullptr) {
Phil Burk2ac035f2017-06-23 14:51:14 -0700290 // Only allow owner or the aaudio service to access the stream.
291 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
292 const uid_t ownerUserId = serviceStream->getOwnerUserId();
293 bool callerOwnsIt = callingUserId == ownerUserId;
Eric Laurenta54f1282017-07-01 19:39:32 -0700294 bool serverCalling = callingUserId == mAudioClient.clientUid;
295 bool serverOwnsIt = ownerUserId == mAudioClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700296 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
297 if (!allowed) {
298 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
299 callingUserId, streamHandle, ownerUserId);
Phil Burk94862522017-09-13 21:31:36 -0700300 serviceStream.clear();
Phil Burk2ac035f2017-06-23 14:51:14 -0700301 }
302 }
303 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800304}
305
Eric Laurenta54f1282017-07-01 19:39:32 -0700306aaudio_result_t AAudioService::startClient(aaudio_handle_t streamHandle,
jiabind1f1cb62020-03-24 11:57:57 -0700307 const android::AudioClient& client,
308 const audio_attributes_t *attr,
309 audio_port_handle_t *clientHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700310 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
311 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700312 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700313 return AAUDIO_ERROR_INVALID_HANDLE;
314 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700315 return serviceStream->startClient(client, attr, clientHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700316}
317
318aaudio_result_t AAudioService::stopClient(aaudio_handle_t streamHandle,
Phil Burkbbd52862018-04-13 11:37:42 -0700319 audio_port_handle_t portHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700320 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
321 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700322 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700323 return AAUDIO_ERROR_INVALID_HANDLE;
324 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700325 return serviceStream->stopClient(portHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700326}
327
328// This is only called internally when AudioFlinger wants to tear down a stream.
329// So we do not have to check permissions.
330aaudio_result_t AAudioService::disconnectStreamByPortHandle(audio_port_handle_t portHandle) {
331 ALOGD("%s(%d) called", __func__, portHandle);
332 sp<AAudioServiceStreamBase> serviceStream =
Phil Burk7ebbc112020-05-13 15:55:17 -0700333 mStreamTracker.findStreamByPortHandle(portHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700334 if (serviceStream.get() == nullptr) {
335 ALOGE("%s(), could not find stream with portHandle = %d", __func__, portHandle);
336 return AAUDIO_ERROR_INVALID_HANDLE;
337 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700338 // This is protected by a lock and will just return if already stopped.
Phil Burkbbd52862018-04-13 11:37:42 -0700339 aaudio_result_t result = serviceStream->stop();
340 serviceStream->disconnect();
Phil Burk7ebbc112020-05-13 15:55:17 -0700341 return result;
Eric Laurenta54f1282017-07-01 19:39:32 -0700342}