blob: 631c6a7afbdc8f1ac3c3bc96a8dbd1d344923fa9 [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 Burkc0c70e32017-02-09 13:18:38 -080035#include "binding/IAAudioService.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080036
37using namespace android;
38using namespace aaudio;
39
Phil Burk91692942017-06-30 12:23:05 -070040#define MAX_STREAMS_PER_PROCESS 8
41
Phil Burk523b3042017-09-13 13:03:08 -070042using android::AAudioService;
Phil Burk5ed503c2017-02-01 09:38:15 -080043
44android::AAudioService::AAudioService()
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -070045 : BnAAudioService(),
46 mAdapter(this) {
Eric Laurenta54f1282017-07-01 19:39:32 -070047 mAudioClient.clientUid = getuid(); // TODO consider using geteuid()
48 mAudioClient.clientPid = getpid();
49 mAudioClient.packageName = String16("");
Phil Burk11e8d332017-05-24 09:59:02 -070050 AAudioClientTracker::getInstance().setAAudioService(this);
Phil Burk5ed503c2017-02-01 09:38:15 -080051}
52
Andy Hung47c5e532017-06-26 18:28:00 -070053status_t AAudioService::dump(int fd, const Vector<String16>& args) {
54 std::string result;
55
56 if (!dumpAllowed()) {
57 std::stringstream ss;
Andy Hung6357b5f2018-10-22 19:47:04 -070058 ss << "Permission Denial: can't dump AAudioService from pid="
Andy Hung47c5e532017-06-26 18:28:00 -070059 << IPCThreadState::self()->getCallingPid() << ", uid="
60 << IPCThreadState::self()->getCallingUid() << "\n";
61 result = ss.str();
62 ALOGW("%s", result.c_str());
63 } else {
Phil Burk523b3042017-09-13 13:03:08 -070064 result = "------------ AAudio Service ------------\n"
65 + mStreamTracker.dump()
Phil Burk4501b352017-06-29 18:12:36 -070066 + AAudioClientTracker::getInstance().dump()
67 + AAudioEndpointManager::getInstance().dump();
Andy Hung47c5e532017-06-26 18:28:00 -070068 }
69 (void)write(fd, result.c_str(), result.size());
70 return NO_ERROR;
71}
72
Phil Burk11e8d332017-05-24 09:59:02 -070073void AAudioService::registerClient(const sp<IAAudioClient>& client) {
74 pid_t pid = IPCThreadState::self()->getCallingPid();
75 AAudioClientTracker::getInstance().registerClient(pid, client);
76}
77
Phil Burk2ebf6622019-04-17 11:10:25 -070078bool AAudioService::isCallerInService() {
79 return mAudioClient.clientPid == IPCThreadState::self()->getCallingPid() &&
80 mAudioClient.clientUid == IPCThreadState::self()->getCallingUid();
81}
82
Phil Burkc0c70e32017-02-09 13:18:38 -080083aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request,
84 aaudio::AAudioStreamConfiguration &configurationOutput) {
Phil Burk6e463ce2020-04-13 10:20:20 -070085 // A lock in is used to order the opening of endpoints when an
86 // EXCLUSIVE endpoint is stolen. We want the order to be:
87 // 1) Thread A opens exclusive MMAP endpoint
88 // 2) Thread B wants to open an exclusive MMAP endpoint so it steals the one from A
89 // under this lock.
90 // 3) Thread B opens a shared MMAP endpoint.
91 // 4) Thread A can then get the lock and also open a shared stream.
92 // Without the lock. Thread A might sneak in and reallocate an exclusive stream
93 // before B can open the shared stream.
94 std::unique_lock<std::recursive_mutex> lock(mOpenLock);
95
Phil Burkc0c70e32017-02-09 13:18:38 -080096 aaudio_result_t result = AAUDIO_OK;
Phil Burk11e8d332017-05-24 09:59:02 -070097 sp<AAudioServiceStreamBase> serviceStream;
Phil Burkc0c70e32017-02-09 13:18:38 -080098 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -070099 bool sharingModeMatchRequired = request.isSharingModeMatchRequired();
Phil Burkc0c70e32017-02-09 13:18:38 -0800100 aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode();
Phil Burkc0c70e32017-02-09 13:18:38 -0800101
Phil Burk91692942017-06-30 12:23:05 -0700102 // Enforce limit on client processes.
103 pid_t pid = request.getProcessId();
Eric Laurenta54f1282017-07-01 19:39:32 -0700104 if (pid != mAudioClient.clientPid) {
Phil Burk91692942017-06-30 12:23:05 -0700105 int32_t count = AAudioClientTracker::getInstance().getStreamCount(pid);
106 if (count >= MAX_STREAMS_PER_PROCESS) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700107 ALOGE("openStream(): exceeded max streams per process %d >= %d",
Phil Burk91692942017-06-30 12:23:05 -0700108 count, MAX_STREAMS_PER_PROCESS);
109 return AAUDIO_ERROR_UNAVAILABLE;
110 }
111 }
112
Phil Burkc0c70e32017-02-09 13:18:38 -0800113 if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700114 ALOGE("openStream(): unrecognized sharing mode = %d", sharingMode);
Phil Burkc0c70e32017-02-09 13:18:38 -0800115 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
116 }
117
Phil Burk836f9df2020-05-29 13:20:28 -0700118 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE
119 && AAudioClientTracker::getInstance().isExclusiveEnabled(request.getProcessId())) {
Eric Laurenta54f1282017-07-01 19:39:32 -0700120 // only trust audioserver for in service indication
121 bool inService = false;
Phil Burk2ebf6622019-04-17 11:10:25 -0700122 if (isCallerInService()) {
Eric Laurenta54f1282017-07-01 19:39:32 -0700123 inService = request.isInService();
124 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700125 serviceStream = new AAudioServiceStreamMMAP(*this, inService);
126 result = serviceStream->open(request);
Phil Burkc0c70e32017-02-09 13:18:38 -0800127 if (result != AAUDIO_OK) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700128 // Clear it so we can possibly fall back to using a shared stream.
Phil Burkfbf031e2017-10-12 15:58:31 -0700129 ALOGW("openStream(), could not open in EXCLUSIVE mode");
Phil Burk11e8d332017-05-24 09:59:02 -0700130 serviceStream.clear();
Phil Burkc0c70e32017-02-09 13:18:38 -0800131 }
132 }
133
Phil Burka3901e92018-10-08 13:54:38 -0700134 // Try SHARED if SHARED requested or if EXCLUSIVE failed.
Phil Burk15f97c92018-09-04 14:06:27 -0700135 if (sharingMode == AAUDIO_SHARING_MODE_SHARED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800136 serviceStream = new AAudioServiceStreamShared(*this);
Phil Burk39f02dd2017-08-04 09:13:31 -0700137 result = serviceStream->open(request);
Phil Burk15f97c92018-09-04 14:06:27 -0700138 } else if (serviceStream.get() == nullptr && !sharingModeMatchRequired) {
139 aaudio::AAudioStreamRequest modifiedRequest = request;
140 // Overwrite the original EXCLUSIVE mode with SHARED.
141 modifiedRequest.getConfiguration().setSharingMode(AAUDIO_SHARING_MODE_SHARED);
142 serviceStream = new AAudioServiceStreamShared(*this);
143 result = serviceStream->open(modifiedRequest);
Phil Burkc0c70e32017-02-09 13:18:38 -0800144 }
145
146 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700147 serviceStream.clear();
Phil Burk5ed503c2017-02-01 09:38:15 -0800148 return result;
149 } else {
Phil Burk523b3042017-09-13 13:03:08 -0700150 aaudio_handle_t handle = mStreamTracker.addStreamForHandle(serviceStream.get());
Phil Burk523b3042017-09-13 13:03:08 -0700151 serviceStream->setHandle(handle);
152 pid_t pid = request.getProcessId();
153 AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream);
154 configurationOutput.copyFrom(*serviceStream);
Phil Burka9876702020-04-20 18:16:15 -0700155 // Log open in MediaMetrics after we have the handle because we need the handle to
156 // create the metrics ID.
157 serviceStream->logOpen(handle);
Phil Burk6e463ce2020-04-13 10:20:20 -0700158 ALOGV("%s(): return handle = 0x%08X", __func__, handle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800159 return handle;
160 }
161}
162
163aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
Phil Burk98d6d922017-07-06 11:52:45 -0700164 // Check permission and ownership first.
165 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700166 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700167 ALOGE("closeStream(0x%0x), illegal stream handle", streamHandle);
Phil Burk91692942017-06-30 12:23:05 -0700168 return AAUDIO_ERROR_INVALID_HANDLE;
169 }
Phil Burk6e463ce2020-04-13 10:20:20 -0700170 return closeStream(serviceStream);
171}
Phil Burk91692942017-06-30 12:23:05 -0700172
Phil Burk6e463ce2020-04-13 10:20:20 -0700173aaudio_result_t AAudioService::closeStream(sp<AAudioServiceStreamBase> serviceStream) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700174 // This is protected by a lock in AAudioClientTracker.
175 // It is safe to unregister the same stream twice.
Phil Burk94862522017-09-13 21:31:36 -0700176 pid_t pid = serviceStream->getOwnerProcessId();
177 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk7ebbc112020-05-13 15:55:17 -0700178 // This is protected by a lock in mStreamTracker.
179 // It is safe to remove the same stream twice.
180 mStreamTracker.removeStreamByHandle(serviceStream->getHandle());
Phil Burk5ed503c2017-02-01 09:38:15 -0800181
Phil Burk7ebbc112020-05-13 15:55:17 -0700182 return serviceStream->close();
Phil Burk94862522017-09-13 21:31:36 -0700183}
Phil Burk523b3042017-09-13 13:03:08 -0700184
185sp<AAudioServiceStreamBase> AAudioService::convertHandleToServiceStream(
186 aaudio_handle_t streamHandle) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700187 sp<AAudioServiceStreamBase> serviceStream = mStreamTracker.getStreamByHandle(
Phil Burk2fe718b2018-05-14 12:28:32 -0700188 streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700189 if (serviceStream.get() != nullptr) {
Phil Burk2ac035f2017-06-23 14:51:14 -0700190 // Only allow owner or the aaudio service to access the stream.
191 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
192 const uid_t ownerUserId = serviceStream->getOwnerUserId();
193 bool callerOwnsIt = callingUserId == ownerUserId;
Eric Laurenta54f1282017-07-01 19:39:32 -0700194 bool serverCalling = callingUserId == mAudioClient.clientUid;
195 bool serverOwnsIt = ownerUserId == mAudioClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700196 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
197 if (!allowed) {
198 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
199 callingUserId, streamHandle, ownerUserId);
Phil Burk94862522017-09-13 21:31:36 -0700200 serviceStream.clear();
Phil Burk2ac035f2017-06-23 14:51:14 -0700201 }
202 }
203 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800204}
205
206aaudio_result_t AAudioService::getStreamDescription(
207 aaudio_handle_t streamHandle,
208 aaudio::AudioEndpointParcelable &parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700209 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
210 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700211 ALOGE("getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800212 return AAUDIO_ERROR_INVALID_HANDLE;
213 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700214 return serviceStream->getDescription(parcelable);
Phil Burk5ed503c2017-02-01 09:38:15 -0800215}
216
217aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700218 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
219 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700220 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800221 return AAUDIO_ERROR_INVALID_HANDLE;
222 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700223 return serviceStream->start();
Phil Burk5ed503c2017-02-01 09:38:15 -0800224}
225
226aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700227 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
228 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700229 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800230 return AAUDIO_ERROR_INVALID_HANDLE;
231 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700232 return serviceStream->pause();
Phil Burk5ed503c2017-02-01 09:38:15 -0800233}
234
Phil Burk71f35bb2017-04-13 16:05:07 -0700235aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700236 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
237 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700238 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700239 return AAUDIO_ERROR_INVALID_HANDLE;
240 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700241 return serviceStream->stop();
Phil Burk71f35bb2017-04-13 16:05:07 -0700242}
243
Phil Burk5ed503c2017-02-01 09:38:15 -0800244aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700245 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
246 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700247 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800248 return AAUDIO_ERROR_INVALID_HANDLE;
249 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700250 return serviceStream->flush();
Phil Burk5ed503c2017-02-01 09:38:15 -0800251}
252
253aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burk11e8d332017-05-24 09:59:02 -0700254 pid_t clientThreadId,
Phil Burk7ebbc112020-05-13 15:55:17 -0700255 int64_t /* periodNanoseconds */) {
Phil Burk523b3042017-09-13 13:03:08 -0700256 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
257 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700258 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800259 return AAUDIO_ERROR_INVALID_HANDLE;
260 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700261 int32_t priority = isCallerInService()
262 ? kRealTimeAudioPriorityService : kRealTimeAudioPriorityClient;
263 return serviceStream->registerAudioThread(clientThreadId, priority);
Phil Burk5ed503c2017-02-01 09:38:15 -0800264}
265
266aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800267 pid_t clientThreadId) {
Phil Burk523b3042017-09-13 13:03:08 -0700268 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
269 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700270 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800271 return AAUDIO_ERROR_INVALID_HANDLE;
272 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700273 return serviceStream->unregisterAudioThread(clientThreadId);
Phil Burk5ed503c2017-02-01 09:38:15 -0800274}
Eric Laurenta54f1282017-07-01 19:39:32 -0700275
276aaudio_result_t AAudioService::startClient(aaudio_handle_t streamHandle,
jiabind1f1cb62020-03-24 11:57:57 -0700277 const android::AudioClient& client,
278 const audio_attributes_t *attr,
279 audio_port_handle_t *clientHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700280 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
281 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700282 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700283 return AAUDIO_ERROR_INVALID_HANDLE;
284 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700285 return serviceStream->startClient(client, attr, clientHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700286}
287
288aaudio_result_t AAudioService::stopClient(aaudio_handle_t streamHandle,
Phil Burkbbd52862018-04-13 11:37:42 -0700289 audio_port_handle_t portHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700290 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
291 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700292 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700293 return AAUDIO_ERROR_INVALID_HANDLE;
294 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700295 return serviceStream->stopClient(portHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700296}
297
298// This is only called internally when AudioFlinger wants to tear down a stream.
299// So we do not have to check permissions.
300aaudio_result_t AAudioService::disconnectStreamByPortHandle(audio_port_handle_t portHandle) {
301 ALOGD("%s(%d) called", __func__, portHandle);
302 sp<AAudioServiceStreamBase> serviceStream =
Phil Burk7ebbc112020-05-13 15:55:17 -0700303 mStreamTracker.findStreamByPortHandle(portHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700304 if (serviceStream.get() == nullptr) {
305 ALOGE("%s(), could not find stream with portHandle = %d", __func__, portHandle);
306 return AAUDIO_ERROR_INVALID_HANDLE;
307 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700308 // This is protected by a lock and will just return if already stopped.
Phil Burkbbd52862018-04-13 11:37:42 -0700309 aaudio_result_t result = serviceStream->stop();
310 serviceStream->disconnect();
Phil Burk7ebbc112020-05-13 15:55:17 -0700311 return result;
Eric Laurenta54f1282017-07-01 19:39:32 -0700312}