blob: 82b12d6489f1f4fe4b727aeb711ee0ca72a964df [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
120 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) {
Eric Laurenta54f1282017-07-01 19:39:32 -0700121 // only trust audioserver for in service indication
122 bool inService = false;
Phil Burk2ebf6622019-04-17 11:10:25 -0700123 if (isCallerInService()) {
Eric Laurenta54f1282017-07-01 19:39:32 -0700124 inService = request.isInService();
125 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700126 serviceStream = new AAudioServiceStreamMMAP(*this, inService);
127 result = serviceStream->open(request);
Phil Burkc0c70e32017-02-09 13:18:38 -0800128 if (result != AAUDIO_OK) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700129 // Clear it so we can possibly fall back to using a shared stream.
Phil Burkfbf031e2017-10-12 15:58:31 -0700130 ALOGW("openStream(), could not open in EXCLUSIVE mode");
Phil Burk11e8d332017-05-24 09:59:02 -0700131 serviceStream.clear();
Phil Burkc0c70e32017-02-09 13:18:38 -0800132 }
133 }
134
Phil Burka3901e92018-10-08 13:54:38 -0700135 // Try SHARED if SHARED requested or if EXCLUSIVE failed.
Phil Burk15f97c92018-09-04 14:06:27 -0700136 if (sharingMode == AAUDIO_SHARING_MODE_SHARED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800137 serviceStream = new AAudioServiceStreamShared(*this);
Phil Burk39f02dd2017-08-04 09:13:31 -0700138 result = serviceStream->open(request);
Phil Burk15f97c92018-09-04 14:06:27 -0700139 } else if (serviceStream.get() == nullptr && !sharingModeMatchRequired) {
140 aaudio::AAudioStreamRequest modifiedRequest = request;
141 // Overwrite the original EXCLUSIVE mode with SHARED.
142 modifiedRequest.getConfiguration().setSharingMode(AAUDIO_SHARING_MODE_SHARED);
143 serviceStream = new AAudioServiceStreamShared(*this);
144 result = serviceStream->open(modifiedRequest);
Phil Burkc0c70e32017-02-09 13:18:38 -0800145 }
146
147 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700148 serviceStream.clear();
Phil Burk5ed503c2017-02-01 09:38:15 -0800149 return result;
150 } else {
Phil Burk523b3042017-09-13 13:03:08 -0700151 aaudio_handle_t handle = mStreamTracker.addStreamForHandle(serviceStream.get());
Phil Burk523b3042017-09-13 13:03:08 -0700152 serviceStream->setHandle(handle);
153 pid_t pid = request.getProcessId();
154 AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream);
155 configurationOutput.copyFrom(*serviceStream);
Phil Burka9876702020-04-20 18:16:15 -0700156 // Log open in MediaMetrics after we have the handle because we need the handle to
157 // create the metrics ID.
158 serviceStream->logOpen(handle);
Phil Burk6e463ce2020-04-13 10:20:20 -0700159 ALOGV("%s(): return handle = 0x%08X", __func__, handle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800160 return handle;
161 }
162}
163
164aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
Phil Burk98d6d922017-07-06 11:52:45 -0700165 // Check permission and ownership first.
166 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700167 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700168 ALOGE("closeStream(0x%0x), illegal stream handle", streamHandle);
Phil Burk91692942017-06-30 12:23:05 -0700169 return AAUDIO_ERROR_INVALID_HANDLE;
170 }
Phil Burk6e463ce2020-04-13 10:20:20 -0700171 return closeStream(serviceStream);
172}
Phil Burk91692942017-06-30 12:23:05 -0700173
Phil Burk6e463ce2020-04-13 10:20:20 -0700174aaudio_result_t AAudioService::closeStream(sp<AAudioServiceStreamBase> serviceStream) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700175 // This is protected by a lock in AAudioClientTracker.
176 // It is safe to unregister the same stream twice.
Phil Burk94862522017-09-13 21:31:36 -0700177 pid_t pid = serviceStream->getOwnerProcessId();
178 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk7ebbc112020-05-13 15:55:17 -0700179 // This is protected by a lock in mStreamTracker.
180 // It is safe to remove the same stream twice.
181 mStreamTracker.removeStreamByHandle(serviceStream->getHandle());
Phil Burk5ed503c2017-02-01 09:38:15 -0800182
Phil Burk7ebbc112020-05-13 15:55:17 -0700183 return serviceStream->close();
Phil Burk94862522017-09-13 21:31:36 -0700184}
Phil Burk523b3042017-09-13 13:03:08 -0700185
186sp<AAudioServiceStreamBase> AAudioService::convertHandleToServiceStream(
187 aaudio_handle_t streamHandle) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700188 sp<AAudioServiceStreamBase> serviceStream = mStreamTracker.getStreamByHandle(
Phil Burk2fe718b2018-05-14 12:28:32 -0700189 streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700190 if (serviceStream.get() != nullptr) {
Phil Burk2ac035f2017-06-23 14:51:14 -0700191 // Only allow owner or the aaudio service to access the stream.
192 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
193 const uid_t ownerUserId = serviceStream->getOwnerUserId();
194 bool callerOwnsIt = callingUserId == ownerUserId;
Eric Laurenta54f1282017-07-01 19:39:32 -0700195 bool serverCalling = callingUserId == mAudioClient.clientUid;
196 bool serverOwnsIt = ownerUserId == mAudioClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700197 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
198 if (!allowed) {
199 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
200 callingUserId, streamHandle, ownerUserId);
Phil Burk94862522017-09-13 21:31:36 -0700201 serviceStream.clear();
Phil Burk2ac035f2017-06-23 14:51:14 -0700202 }
203 }
204 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800205}
206
207aaudio_result_t AAudioService::getStreamDescription(
208 aaudio_handle_t streamHandle,
209 aaudio::AudioEndpointParcelable &parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700210 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
211 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700212 ALOGE("getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800213 return AAUDIO_ERROR_INVALID_HANDLE;
214 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700215 return serviceStream->getDescription(parcelable);
Phil Burk5ed503c2017-02-01 09:38:15 -0800216}
217
218aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700219 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
220 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700221 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800222 return AAUDIO_ERROR_INVALID_HANDLE;
223 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700224 return serviceStream->start();
Phil Burk5ed503c2017-02-01 09:38:15 -0800225}
226
227aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700228 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
229 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700230 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800231 return AAUDIO_ERROR_INVALID_HANDLE;
232 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700233 return serviceStream->pause();
Phil Burk5ed503c2017-02-01 09:38:15 -0800234}
235
Phil Burk71f35bb2017-04-13 16:05:07 -0700236aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700237 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
238 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700239 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700240 return AAUDIO_ERROR_INVALID_HANDLE;
241 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700242 return serviceStream->stop();
Phil Burk71f35bb2017-04-13 16:05:07 -0700243}
244
Phil Burk5ed503c2017-02-01 09:38:15 -0800245aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700246 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
247 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700248 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800249 return AAUDIO_ERROR_INVALID_HANDLE;
250 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700251 return serviceStream->flush();
Phil Burk5ed503c2017-02-01 09:38:15 -0800252}
253
254aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burk11e8d332017-05-24 09:59:02 -0700255 pid_t clientThreadId,
Phil Burk7ebbc112020-05-13 15:55:17 -0700256 int64_t /* periodNanoseconds */) {
Phil Burk523b3042017-09-13 13:03:08 -0700257 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
258 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700259 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800260 return AAUDIO_ERROR_INVALID_HANDLE;
261 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700262 int32_t priority = isCallerInService()
263 ? kRealTimeAudioPriorityService : kRealTimeAudioPriorityClient;
264 return serviceStream->registerAudioThread(clientThreadId, priority);
Phil Burk5ed503c2017-02-01 09:38:15 -0800265}
266
267aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800268 pid_t clientThreadId) {
Phil Burk523b3042017-09-13 13:03:08 -0700269 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
270 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700271 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800272 return AAUDIO_ERROR_INVALID_HANDLE;
273 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700274 return serviceStream->unregisterAudioThread(clientThreadId);
Phil Burk5ed503c2017-02-01 09:38:15 -0800275}
Eric Laurenta54f1282017-07-01 19:39:32 -0700276
277aaudio_result_t AAudioService::startClient(aaudio_handle_t streamHandle,
jiabind1f1cb62020-03-24 11:57:57 -0700278 const android::AudioClient& client,
279 const audio_attributes_t *attr,
280 audio_port_handle_t *clientHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700281 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
282 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700283 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700284 return AAUDIO_ERROR_INVALID_HANDLE;
285 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700286 return serviceStream->startClient(client, attr, clientHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700287}
288
289aaudio_result_t AAudioService::stopClient(aaudio_handle_t streamHandle,
Phil Burkbbd52862018-04-13 11:37:42 -0700290 audio_port_handle_t portHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700291 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
292 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700293 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700294 return AAUDIO_ERROR_INVALID_HANDLE;
295 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700296 return serviceStream->stopClient(portHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700297}
298
299// This is only called internally when AudioFlinger wants to tear down a stream.
300// So we do not have to check permissions.
301aaudio_result_t AAudioService::disconnectStreamByPortHandle(audio_port_handle_t portHandle) {
302 ALOGD("%s(%d) called", __func__, portHandle);
303 sp<AAudioServiceStreamBase> serviceStream =
Phil Burk7ebbc112020-05-13 15:55:17 -0700304 mStreamTracker.findStreamByPortHandle(portHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700305 if (serviceStream.get() == nullptr) {
306 ALOGE("%s(), could not find stream with portHandle = %d", __func__, portHandle);
307 return AAUDIO_ERROR_INVALID_HANDLE;
308 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700309 // This is protected by a lock and will just return if already stopped.
Phil Burkbbd52862018-04-13 11:37:42 -0700310 aaudio_result_t result = serviceStream->stop();
311 serviceStream->disconnect();
Phil Burk7ebbc112020-05-13 15:55:17 -0700312 return result;
Eric Laurenta54f1282017-07-01 19:39:32 -0700313}