blob: 22cdb35e48bf6e3bb73a2dce6620be421af80c44 [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()
45 : BnAAudioService() {
Eric Laurenta54f1282017-07-01 19:39:32 -070046 mAudioClient.clientUid = getuid(); // TODO consider using geteuid()
47 mAudioClient.clientPid = getpid();
48 mAudioClient.packageName = String16("");
Phil Burk11e8d332017-05-24 09:59:02 -070049 AAudioClientTracker::getInstance().setAAudioService(this);
Phil Burk5ed503c2017-02-01 09:38:15 -080050}
51
52AAudioService::~AAudioService() {
53}
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
Phil Burk11e8d332017-05-24 09:59:02 -070075void AAudioService::registerClient(const sp<IAAudioClient>& client) {
76 pid_t pid = IPCThreadState::self()->getCallingPid();
77 AAudioClientTracker::getInstance().registerClient(pid, client);
78}
79
Phil Burk2ebf6622019-04-17 11:10:25 -070080bool AAudioService::isCallerInService() {
81 return mAudioClient.clientPid == IPCThreadState::self()->getCallingPid() &&
82 mAudioClient.clientUid == IPCThreadState::self()->getCallingUid();
83}
84
Phil Burkc0c70e32017-02-09 13:18:38 -080085aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request,
86 aaudio::AAudioStreamConfiguration &configurationOutput) {
Phil Burk6e463ce2020-04-13 10:20:20 -070087 // A lock in is used to order the opening of endpoints when an
88 // EXCLUSIVE endpoint is stolen. We want the order to be:
89 // 1) Thread A opens exclusive MMAP endpoint
90 // 2) Thread B wants to open an exclusive MMAP endpoint so it steals the one from A
91 // under this lock.
92 // 3) Thread B opens a shared MMAP endpoint.
93 // 4) Thread A can then get the lock and also open a shared stream.
94 // Without the lock. Thread A might sneak in and reallocate an exclusive stream
95 // before B can open the shared stream.
96 std::unique_lock<std::recursive_mutex> lock(mOpenLock);
97
Phil Burkc0c70e32017-02-09 13:18:38 -080098 aaudio_result_t result = AAUDIO_OK;
Phil Burk11e8d332017-05-24 09:59:02 -070099 sp<AAudioServiceStreamBase> serviceStream;
Phil Burkc0c70e32017-02-09 13:18:38 -0800100 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -0700101 bool sharingModeMatchRequired = request.isSharingModeMatchRequired();
Phil Burkc0c70e32017-02-09 13:18:38 -0800102 aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode();
Phil Burkc0c70e32017-02-09 13:18:38 -0800103
Phil Burk91692942017-06-30 12:23:05 -0700104 // Enforce limit on client processes.
105 pid_t pid = request.getProcessId();
Eric Laurenta54f1282017-07-01 19:39:32 -0700106 if (pid != mAudioClient.clientPid) {
Phil Burk91692942017-06-30 12:23:05 -0700107 int32_t count = AAudioClientTracker::getInstance().getStreamCount(pid);
108 if (count >= MAX_STREAMS_PER_PROCESS) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700109 ALOGE("openStream(): exceeded max streams per process %d >= %d",
Phil Burk91692942017-06-30 12:23:05 -0700110 count, MAX_STREAMS_PER_PROCESS);
111 return AAUDIO_ERROR_UNAVAILABLE;
112 }
113 }
114
Phil Burkc0c70e32017-02-09 13:18:38 -0800115 if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700116 ALOGE("openStream(): unrecognized sharing mode = %d", sharingMode);
Phil Burkc0c70e32017-02-09 13:18:38 -0800117 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
118 }
119
Phil Burk836f9df2020-05-29 13:20:28 -0700120 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE
121 && AAudioClientTracker::getInstance().isExclusiveEnabled(request.getProcessId())) {
Eric Laurenta54f1282017-07-01 19:39:32 -0700122 // only trust audioserver for in service indication
123 bool inService = false;
Phil Burk2ebf6622019-04-17 11:10:25 -0700124 if (isCallerInService()) {
Eric Laurenta54f1282017-07-01 19:39:32 -0700125 inService = request.isInService();
126 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700127 serviceStream = new AAudioServiceStreamMMAP(*this, inService);
128 result = serviceStream->open(request);
Phil Burkc0c70e32017-02-09 13:18:38 -0800129 if (result != AAUDIO_OK) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700130 // Clear it so we can possibly fall back to using a shared stream.
Phil Burkfbf031e2017-10-12 15:58:31 -0700131 ALOGW("openStream(), could not open in EXCLUSIVE mode");
Phil Burk11e8d332017-05-24 09:59:02 -0700132 serviceStream.clear();
Phil Burkc0c70e32017-02-09 13:18:38 -0800133 }
134 }
135
Phil Burka3901e92018-10-08 13:54:38 -0700136 // Try SHARED if SHARED requested or if EXCLUSIVE failed.
Phil Burk15f97c92018-09-04 14:06:27 -0700137 if (sharingMode == AAUDIO_SHARING_MODE_SHARED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800138 serviceStream = new AAudioServiceStreamShared(*this);
Phil Burk39f02dd2017-08-04 09:13:31 -0700139 result = serviceStream->open(request);
Phil Burk15f97c92018-09-04 14:06:27 -0700140 } else if (serviceStream.get() == nullptr && !sharingModeMatchRequired) {
141 aaudio::AAudioStreamRequest modifiedRequest = request;
142 // Overwrite the original EXCLUSIVE mode with SHARED.
143 modifiedRequest.getConfiguration().setSharingMode(AAUDIO_SHARING_MODE_SHARED);
144 serviceStream = new AAudioServiceStreamShared(*this);
145 result = serviceStream->open(modifiedRequest);
Phil Burkc0c70e32017-02-09 13:18:38 -0800146 }
147
148 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700149 serviceStream.clear();
Phil Burk5ed503c2017-02-01 09:38:15 -0800150 return result;
151 } else {
Phil Burk523b3042017-09-13 13:03:08 -0700152 aaudio_handle_t handle = mStreamTracker.addStreamForHandle(serviceStream.get());
Phil Burk523b3042017-09-13 13:03:08 -0700153 serviceStream->setHandle(handle);
154 pid_t pid = request.getProcessId();
155 AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream);
156 configurationOutput.copyFrom(*serviceStream);
Phil Burka9876702020-04-20 18:16:15 -0700157 // Log open in MediaMetrics after we have the handle because we need the handle to
158 // create the metrics ID.
159 serviceStream->logOpen(handle);
Phil Burk6e463ce2020-04-13 10:20:20 -0700160 ALOGV("%s(): return handle = 0x%08X", __func__, handle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800161 return handle;
162 }
163}
164
165aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
Phil Burk98d6d922017-07-06 11:52:45 -0700166 // Check permission and ownership first.
167 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700168 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700169 ALOGE("closeStream(0x%0x), illegal stream handle", streamHandle);
Phil Burk91692942017-06-30 12:23:05 -0700170 return AAUDIO_ERROR_INVALID_HANDLE;
171 }
Phil Burk6e463ce2020-04-13 10:20:20 -0700172 return closeStream(serviceStream);
173}
Phil Burk91692942017-06-30 12:23:05 -0700174
Phil Burk6e463ce2020-04-13 10:20:20 -0700175aaudio_result_t AAudioService::closeStream(sp<AAudioServiceStreamBase> serviceStream) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700176 // This is protected by a lock in AAudioClientTracker.
177 // It is safe to unregister the same stream twice.
Phil Burk94862522017-09-13 21:31:36 -0700178 pid_t pid = serviceStream->getOwnerProcessId();
179 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk7ebbc112020-05-13 15:55:17 -0700180 // This is protected by a lock in mStreamTracker.
181 // It is safe to remove the same stream twice.
182 mStreamTracker.removeStreamByHandle(serviceStream->getHandle());
Phil Burk5ed503c2017-02-01 09:38:15 -0800183
Phil Burk7ebbc112020-05-13 15:55:17 -0700184 return serviceStream->close();
Phil Burk94862522017-09-13 21:31:36 -0700185}
Phil Burk523b3042017-09-13 13:03:08 -0700186
187sp<AAudioServiceStreamBase> AAudioService::convertHandleToServiceStream(
188 aaudio_handle_t streamHandle) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700189 sp<AAudioServiceStreamBase> serviceStream = mStreamTracker.getStreamByHandle(
Phil Burk2fe718b2018-05-14 12:28:32 -0700190 streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700191 if (serviceStream.get() != nullptr) {
Phil Burk2ac035f2017-06-23 14:51:14 -0700192 // Only allow owner or the aaudio service to access the stream.
193 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
194 const uid_t ownerUserId = serviceStream->getOwnerUserId();
195 bool callerOwnsIt = callingUserId == ownerUserId;
Eric Laurenta54f1282017-07-01 19:39:32 -0700196 bool serverCalling = callingUserId == mAudioClient.clientUid;
197 bool serverOwnsIt = ownerUserId == mAudioClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700198 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
199 if (!allowed) {
200 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
201 callingUserId, streamHandle, ownerUserId);
Phil Burk94862522017-09-13 21:31:36 -0700202 serviceStream.clear();
Phil Burk2ac035f2017-06-23 14:51:14 -0700203 }
204 }
205 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800206}
207
208aaudio_result_t AAudioService::getStreamDescription(
209 aaudio_handle_t streamHandle,
210 aaudio::AudioEndpointParcelable &parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700211 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
212 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700213 ALOGE("getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800214 return AAUDIO_ERROR_INVALID_HANDLE;
215 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700216 return serviceStream->getDescription(parcelable);
Phil Burk5ed503c2017-02-01 09:38:15 -0800217}
218
219aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700220 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
221 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700222 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800223 return AAUDIO_ERROR_INVALID_HANDLE;
224 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700225 return serviceStream->start();
Phil Burk5ed503c2017-02-01 09:38:15 -0800226}
227
228aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700229 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
230 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700231 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800232 return AAUDIO_ERROR_INVALID_HANDLE;
233 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700234 return serviceStream->pause();
Phil Burk5ed503c2017-02-01 09:38:15 -0800235}
236
Phil Burk71f35bb2017-04-13 16:05:07 -0700237aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700238 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
239 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700240 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700241 return AAUDIO_ERROR_INVALID_HANDLE;
242 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700243 return serviceStream->stop();
Phil Burk71f35bb2017-04-13 16:05:07 -0700244}
245
Phil Burk5ed503c2017-02-01 09:38:15 -0800246aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700247 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
248 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700249 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800250 return AAUDIO_ERROR_INVALID_HANDLE;
251 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700252 return serviceStream->flush();
Phil Burk5ed503c2017-02-01 09:38:15 -0800253}
254
255aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burk11e8d332017-05-24 09:59:02 -0700256 pid_t clientThreadId,
Phil Burk7ebbc112020-05-13 15:55:17 -0700257 int64_t /* periodNanoseconds */) {
Phil Burk523b3042017-09-13 13:03:08 -0700258 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
259 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700260 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800261 return AAUDIO_ERROR_INVALID_HANDLE;
262 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700263 int32_t priority = isCallerInService()
264 ? kRealTimeAudioPriorityService : kRealTimeAudioPriorityClient;
265 return serviceStream->registerAudioThread(clientThreadId, priority);
Phil Burk5ed503c2017-02-01 09:38:15 -0800266}
267
268aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800269 pid_t clientThreadId) {
Phil Burk523b3042017-09-13 13:03:08 -0700270 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
271 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700272 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800273 return AAUDIO_ERROR_INVALID_HANDLE;
274 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700275 return serviceStream->unregisterAudioThread(clientThreadId);
Phil Burk5ed503c2017-02-01 09:38:15 -0800276}
Eric Laurenta54f1282017-07-01 19:39:32 -0700277
278aaudio_result_t AAudioService::startClient(aaudio_handle_t streamHandle,
jiabind1f1cb62020-03-24 11:57:57 -0700279 const android::AudioClient& client,
280 const audio_attributes_t *attr,
281 audio_port_handle_t *clientHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700282 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
283 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700284 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700285 return AAUDIO_ERROR_INVALID_HANDLE;
286 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700287 return serviceStream->startClient(client, attr, clientHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700288}
289
290aaudio_result_t AAudioService::stopClient(aaudio_handle_t streamHandle,
Phil Burkbbd52862018-04-13 11:37:42 -0700291 audio_port_handle_t portHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700292 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
293 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700294 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700295 return AAUDIO_ERROR_INVALID_HANDLE;
296 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700297 return serviceStream->stopClient(portHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700298}
299
300// This is only called internally when AudioFlinger wants to tear down a stream.
301// So we do not have to check permissions.
302aaudio_result_t AAudioService::disconnectStreamByPortHandle(audio_port_handle_t portHandle) {
303 ALOGD("%s(%d) called", __func__, portHandle);
304 sp<AAudioServiceStreamBase> serviceStream =
Phil Burk7ebbc112020-05-13 15:55:17 -0700305 mStreamTracker.findStreamByPortHandle(portHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700306 if (serviceStream.get() == nullptr) {
307 ALOGE("%s(), could not find stream with portHandle = %d", __func__, portHandle);
308 return AAUDIO_ERROR_INVALID_HANDLE;
309 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700310 // This is protected by a lock and will just return if already stopped.
Phil Burkbbd52862018-04-13 11:37:42 -0700311 aaudio_result_t result = serviceStream->stop();
312 serviceStream->disconnect();
Phil Burk7ebbc112020-05-13 15:55:17 -0700313 return result;
Eric Laurenta54f1282017-07-01 19:39:32 -0700314}