blob: 0b69bf6d32c40a1c39ac978a3e8ad0f1bda777b2 [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
Philip P. Moltmannbda45752020-07-17 16:41:18 -070025#include <android/media/permission/Identity.h>
Phil Burka4eb0d82017-04-12 15:44:06 -070026#include <aaudio/AAudio.h>
Philip P. Moltmannbda45752020-07-17 16:41:18 -070027#include <media/AidlConversion.h>
Andy Hungab7ef302018-05-15 19:35:29 -070028#include <mediautils/ServiceUtilities.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080029#include <utils/String16.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080030
Phil Burkc0c70e32017-02-09 13:18:38 -080031#include "binding/AAudioServiceMessage.h"
Phil Burk11e8d332017-05-24 09:59:02 -070032#include "AAudioClientTracker.h"
Andy Hung47c5e532017-06-26 18:28:00 -070033#include "AAudioEndpointManager.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080034#include "AAudioService.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080035#include "AAudioServiceStreamMMAP.h"
36#include "AAudioServiceStreamShared.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080037
38using namespace android;
39using namespace aaudio;
40
Phil Burk91692942017-06-30 12:23:05 -070041#define MAX_STREAMS_PER_PROCESS 8
Nate Myrenae7d9e62021-03-15 14:21:43 -070042#define AIDL_RETURN(x) { *_aidl_return = (x); return Status::ok(); }
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070043
Philip P. Moltmannbda45752020-07-17 16:41:18 -070044#define VALUE_OR_RETURN_ILLEGAL_ARG_STATUS(x) \
45 ({ auto _tmp = (x); \
46 if (!_tmp.ok()) AIDL_RETURN(AAUDIO_ERROR_ILLEGAL_ARGUMENT); \
47 std::move(_tmp.value()); })
Phil Burk91692942017-06-30 12:23:05 -070048
Phil Burk523b3042017-09-13 13:03:08 -070049using android::AAudioService;
Philip P. Moltmannbda45752020-07-17 16:41:18 -070050using android::media::permission::Identity;
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070051using binder::Status;
Phil Burk5ed503c2017-02-01 09:38:15 -080052
53android::AAudioService::AAudioService()
Ytai Ben-Tsvi734e3502020-08-24 14:57:36 -070054 : BnAAudioService(),
55 mAdapter(this) {
Philip P. Moltmannbda45752020-07-17 16:41:18 -070056 // TODO consider using geteuid()
57 // TODO b/182392769: use identity util
58 mAudioClient.identity.uid = VALUE_OR_FATAL(legacy2aidl_uid_t_int32_t(getuid()));
59 mAudioClient.identity.pid = VALUE_OR_FATAL(legacy2aidl_pid_t_int32_t(getpid()));
60 mAudioClient.identity.packageName = std::nullopt;
61 mAudioClient.identity.attributionTag = std::nullopt;
Phil Burk11e8d332017-05-24 09:59:02 -070062 AAudioClientTracker::getInstance().setAAudioService(this);
Phil Burk5ed503c2017-02-01 09:38:15 -080063}
64
Andy Hung47c5e532017-06-26 18:28:00 -070065status_t AAudioService::dump(int fd, const Vector<String16>& args) {
66 std::string result;
67
68 if (!dumpAllowed()) {
69 std::stringstream ss;
Andy Hung6357b5f2018-10-22 19:47:04 -070070 ss << "Permission Denial: can't dump AAudioService from pid="
Andy Hung47c5e532017-06-26 18:28:00 -070071 << IPCThreadState::self()->getCallingPid() << ", uid="
72 << IPCThreadState::self()->getCallingUid() << "\n";
73 result = ss.str();
74 ALOGW("%s", result.c_str());
75 } else {
Phil Burk523b3042017-09-13 13:03:08 -070076 result = "------------ AAudio Service ------------\n"
77 + mStreamTracker.dump()
Phil Burk4501b352017-06-29 18:12:36 -070078 + AAudioClientTracker::getInstance().dump()
79 + AAudioEndpointManager::getInstance().dump();
Andy Hung47c5e532017-06-26 18:28:00 -070080 }
81 (void)write(fd, result.c_str(), result.size());
82 return NO_ERROR;
83}
84
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070085Status AAudioService::registerClient(const sp<IAAudioClient> &client) {
Phil Burk11e8d332017-05-24 09:59:02 -070086 pid_t pid = IPCThreadState::self()->getCallingPid();
87 AAudioClientTracker::getInstance().registerClient(pid, client);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070088 return Status::ok();
Phil Burk11e8d332017-05-24 09:59:02 -070089}
90
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070091Status
92AAudioService::openStream(const StreamRequest &_request, StreamParameters* _paramsOut,
93 int32_t *_aidl_return) {
94 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
Phil Burk2ebf6622019-04-17 11:10:25 -070095
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070096 // Create wrapper objects for simple usage of the parcelables.
97 const AAudioStreamRequest request(_request);
98 AAudioStreamConfiguration paramsOut;
99
Phil Burk6e463ce2020-04-13 10:20:20 -0700100 // A lock in is used to order the opening of endpoints when an
101 // EXCLUSIVE endpoint is stolen. We want the order to be:
102 // 1) Thread A opens exclusive MMAP endpoint
103 // 2) Thread B wants to open an exclusive MMAP endpoint so it steals the one from A
104 // under this lock.
105 // 3) Thread B opens a shared MMAP endpoint.
106 // 4) Thread A can then get the lock and also open a shared stream.
107 // Without the lock. Thread A might sneak in and reallocate an exclusive stream
108 // before B can open the shared stream.
109 std::unique_lock<std::recursive_mutex> lock(mOpenLock);
110
Phil Burkc0c70e32017-02-09 13:18:38 -0800111 aaudio_result_t result = AAUDIO_OK;
Phil Burk11e8d332017-05-24 09:59:02 -0700112 sp<AAudioServiceStreamBase> serviceStream;
Phil Burkc0c70e32017-02-09 13:18:38 -0800113 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -0700114 bool sharingModeMatchRequired = request.isSharingModeMatchRequired();
Phil Burkc0c70e32017-02-09 13:18:38 -0800115 aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode();
Phil Burkc0c70e32017-02-09 13:18:38 -0800116
Phil Burk91692942017-06-30 12:23:05 -0700117 // Enforce limit on client processes.
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700118 Identity callingIdentity = request.getIdentity();
Nate Myrenae7d9e62021-03-15 14:21:43 -0700119 pid_t pid = IPCThreadState::self()->getCallingPid();
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700120 callingIdentity.pid = VALUE_OR_RETURN_ILLEGAL_ARG_STATUS(
Nate Myrenae7d9e62021-03-15 14:21:43 -0700121 legacy2aidl_pid_t_int32_t(pid));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700122 callingIdentity.uid = VALUE_OR_RETURN_ILLEGAL_ARG_STATUS(
123 legacy2aidl_uid_t_int32_t(IPCThreadState::self()->getCallingUid()));
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700124 if (callingIdentity.pid != mAudioClient.identity.pid) {
Phil Burk91692942017-06-30 12:23:05 -0700125 int32_t count = AAudioClientTracker::getInstance().getStreamCount(pid);
126 if (count >= MAX_STREAMS_PER_PROCESS) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700127 ALOGE("openStream(): exceeded max streams per process %d >= %d",
Phil Burk91692942017-06-30 12:23:05 -0700128 count, MAX_STREAMS_PER_PROCESS);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700129 AIDL_RETURN(AAUDIO_ERROR_UNAVAILABLE);
Phil Burk91692942017-06-30 12:23:05 -0700130 }
131 }
132
Phil Burkc0c70e32017-02-09 13:18:38 -0800133 if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700134 ALOGE("openStream(): unrecognized sharing mode = %d", sharingMode);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700135 AIDL_RETURN(AAUDIO_ERROR_ILLEGAL_ARGUMENT);
Phil Burkc0c70e32017-02-09 13:18:38 -0800136 }
137
Phil Burk836f9df2020-05-29 13:20:28 -0700138 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700139 && AAudioClientTracker::getInstance().isExclusiveEnabled(pid)) {
Eric Laurenta54f1282017-07-01 19:39:32 -0700140 // only trust audioserver for in service indication
141 bool inService = false;
Phil Burk2ebf6622019-04-17 11:10:25 -0700142 if (isCallerInService()) {
Eric Laurenta54f1282017-07-01 19:39:32 -0700143 inService = request.isInService();
144 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700145 serviceStream = new AAudioServiceStreamMMAP(*this, inService);
146 result = serviceStream->open(request);
Phil Burkc0c70e32017-02-09 13:18:38 -0800147 if (result != AAUDIO_OK) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700148 // Clear it so we can possibly fall back to using a shared stream.
Phil Burkfbf031e2017-10-12 15:58:31 -0700149 ALOGW("openStream(), could not open in EXCLUSIVE mode");
Phil Burk11e8d332017-05-24 09:59:02 -0700150 serviceStream.clear();
Phil Burkc0c70e32017-02-09 13:18:38 -0800151 }
152 }
153
Phil Burka3901e92018-10-08 13:54:38 -0700154 // Try SHARED if SHARED requested or if EXCLUSIVE failed.
Phil Burk15f97c92018-09-04 14:06:27 -0700155 if (sharingMode == AAUDIO_SHARING_MODE_SHARED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800156 serviceStream = new AAudioServiceStreamShared(*this);
Phil Burk39f02dd2017-08-04 09:13:31 -0700157 result = serviceStream->open(request);
Phil Burk15f97c92018-09-04 14:06:27 -0700158 } else if (serviceStream.get() == nullptr && !sharingModeMatchRequired) {
159 aaudio::AAudioStreamRequest modifiedRequest = request;
160 // Overwrite the original EXCLUSIVE mode with SHARED.
161 modifiedRequest.getConfiguration().setSharingMode(AAUDIO_SHARING_MODE_SHARED);
162 serviceStream = new AAudioServiceStreamShared(*this);
163 result = serviceStream->open(modifiedRequest);
Phil Burkc0c70e32017-02-09 13:18:38 -0800164 }
165
166 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700167 serviceStream.clear();
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700168 AIDL_RETURN(result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800169 } else {
Phil Burk523b3042017-09-13 13:03:08 -0700170 aaudio_handle_t handle = mStreamTracker.addStreamForHandle(serviceStream.get());
Phil Burk523b3042017-09-13 13:03:08 -0700171 serviceStream->setHandle(handle);
Phil Burk523b3042017-09-13 13:03:08 -0700172 AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700173 paramsOut.copyFrom(*serviceStream);
174 *_paramsOut = std::move(paramsOut).parcelable();
Phil Burka9876702020-04-20 18:16:15 -0700175 // Log open in MediaMetrics after we have the handle because we need the handle to
176 // create the metrics ID.
177 serviceStream->logOpen(handle);
Phil Burk6e463ce2020-04-13 10:20:20 -0700178 ALOGV("%s(): return handle = 0x%08X", __func__, handle);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700179 AIDL_RETURN(handle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800180 }
181}
182
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700183Status AAudioService::closeStream(int32_t streamHandle, int32_t *_aidl_return) {
184 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
185
Phil Burk98d6d922017-07-06 11:52:45 -0700186 // Check permission and ownership first.
187 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700188 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700189 ALOGE("closeStream(0x%0x), illegal stream handle", streamHandle);
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700190 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
Phil Burk91692942017-06-30 12:23:05 -0700191 }
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700192 AIDL_RETURN(closeStream(serviceStream));
193}
194
195Status AAudioService::getStreamDescription(int32_t streamHandle, Endpoint* endpoint,
196 int32_t *_aidl_return) {
197 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
198
199 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
200 if (serviceStream.get() == nullptr) {
201 ALOGE("getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
202 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
203 }
204 AudioEndpointParcelable endpointParcelable;
205 aaudio_result_t result = serviceStream->getDescription(endpointParcelable);
206 if (result == AAUDIO_OK) {
207 *endpoint = std::move(endpointParcelable).parcelable();
208 }
209 AIDL_RETURN(result);
210}
211
212Status AAudioService::startStream(int32_t streamHandle, int32_t *_aidl_return) {
213 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
214
215 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
216 if (serviceStream.get() == nullptr) {
217 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
218 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
219 }
220 AIDL_RETURN(serviceStream->start());
221}
222
223Status AAudioService::pauseStream(int32_t streamHandle, int32_t *_aidl_return) {
224 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
225
226 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
227 if (serviceStream.get() == nullptr) {
228 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
229 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
230 }
231 AIDL_RETURN(serviceStream->pause());
232}
233
234Status AAudioService::stopStream(int32_t streamHandle, int32_t *_aidl_return) {
235 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
236
237 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
238 if (serviceStream.get() == nullptr) {
239 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
240 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
241 }
242 AIDL_RETURN(serviceStream->stop());
243}
244
245Status AAudioService::flushStream(int32_t streamHandle, int32_t *_aidl_return) {
246 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
247
248 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
249 if (serviceStream.get() == nullptr) {
250 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
251 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
252 }
253 AIDL_RETURN(serviceStream->flush());
254}
255
256Status AAudioService::registerAudioThread(int32_t streamHandle, int32_t clientThreadId, int64_t periodNanoseconds,
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 int32_t priority = isCallerInService()
266 ? kRealTimeAudioPriorityService : kRealTimeAudioPriorityClient;
267 AIDL_RETURN(serviceStream->registerAudioThread(clientThreadId, priority));
268}
269
270Status AAudioService::unregisterAudioThread(int32_t streamHandle, int32_t clientThreadId,
271 int32_t *_aidl_return) {
272 static_assert(std::is_same_v<aaudio_result_t, std::decay_t<typeof(*_aidl_return)>>);
273
274 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
275 if (serviceStream.get() == nullptr) {
276 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
277 AIDL_RETURN(AAUDIO_ERROR_INVALID_HANDLE);
278 }
279 AIDL_RETURN(serviceStream->unregisterAudioThread(clientThreadId));
280}
281
282bool AAudioService::isCallerInService() {
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700283 pid_t clientPid = VALUE_OR_FATAL(aidl2legacy_int32_t_pid_t(mAudioClient.identity.pid));
284 uid_t clientUid = VALUE_OR_FATAL(aidl2legacy_int32_t_uid_t(mAudioClient.identity.uid));
285 return clientPid == IPCThreadState::self()->getCallingPid() &&
286 clientUid == IPCThreadState::self()->getCallingUid();
Phil Burk6e463ce2020-04-13 10:20:20 -0700287}
Phil Burk91692942017-06-30 12:23:05 -0700288
Phil Burk6e463ce2020-04-13 10:20:20 -0700289aaudio_result_t AAudioService::closeStream(sp<AAudioServiceStreamBase> serviceStream) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700290 // This is protected by a lock in AAudioClientTracker.
291 // It is safe to unregister the same stream twice.
Phil Burk94862522017-09-13 21:31:36 -0700292 pid_t pid = serviceStream->getOwnerProcessId();
293 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk7ebbc112020-05-13 15:55:17 -0700294 // This is protected by a lock in mStreamTracker.
295 // It is safe to remove the same stream twice.
296 mStreamTracker.removeStreamByHandle(serviceStream->getHandle());
Phil Burk5ed503c2017-02-01 09:38:15 -0800297
Phil Burk7ebbc112020-05-13 15:55:17 -0700298 return serviceStream->close();
Phil Burk94862522017-09-13 21:31:36 -0700299}
Phil Burk523b3042017-09-13 13:03:08 -0700300
301sp<AAudioServiceStreamBase> AAudioService::convertHandleToServiceStream(
302 aaudio_handle_t streamHandle) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700303 sp<AAudioServiceStreamBase> serviceStream = mStreamTracker.getStreamByHandle(
Phil Burk2fe718b2018-05-14 12:28:32 -0700304 streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700305 if (serviceStream.get() != nullptr) {
Phil Burk2ac035f2017-06-23 14:51:14 -0700306 // Only allow owner or the aaudio service to access the stream.
307 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
308 const uid_t ownerUserId = serviceStream->getOwnerUserId();
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700309 const uid_t clientUid = VALUE_OR_FATAL(
310 aidl2legacy_int32_t_uid_t(mAudioClient.identity.uid));
Phil Burk2ac035f2017-06-23 14:51:14 -0700311 bool callerOwnsIt = callingUserId == ownerUserId;
Philip P. Moltmannbda45752020-07-17 16:41:18 -0700312 bool serverCalling = callingUserId == clientUid;
313 bool serverOwnsIt = ownerUserId == clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700314 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
315 if (!allowed) {
316 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
317 callingUserId, streamHandle, ownerUserId);
Phil Burk94862522017-09-13 21:31:36 -0700318 serviceStream.clear();
Phil Burk2ac035f2017-06-23 14:51:14 -0700319 }
320 }
321 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800322}
323
Eric Laurenta54f1282017-07-01 19:39:32 -0700324aaudio_result_t AAudioService::startClient(aaudio_handle_t streamHandle,
jiabind1f1cb62020-03-24 11:57:57 -0700325 const android::AudioClient& client,
326 const audio_attributes_t *attr,
327 audio_port_handle_t *clientHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700328 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
329 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700330 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700331 return AAUDIO_ERROR_INVALID_HANDLE;
332 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700333 return serviceStream->startClient(client, attr, clientHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700334}
335
336aaudio_result_t AAudioService::stopClient(aaudio_handle_t streamHandle,
Phil Burkbbd52862018-04-13 11:37:42 -0700337 audio_port_handle_t portHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700338 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
339 if (serviceStream.get() == nullptr) {
Phil Burk7ebbc112020-05-13 15:55:17 -0700340 ALOGW("%s(), invalid streamHandle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700341 return AAUDIO_ERROR_INVALID_HANDLE;
342 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700343 return serviceStream->stopClient(portHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700344}
345
346// This is only called internally when AudioFlinger wants to tear down a stream.
347// So we do not have to check permissions.
348aaudio_result_t AAudioService::disconnectStreamByPortHandle(audio_port_handle_t portHandle) {
349 ALOGD("%s(%d) called", __func__, portHandle);
350 sp<AAudioServiceStreamBase> serviceStream =
Phil Burk7ebbc112020-05-13 15:55:17 -0700351 mStreamTracker.findStreamByPortHandle(portHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700352 if (serviceStream.get() == nullptr) {
353 ALOGE("%s(), could not find stream with portHandle = %d", __func__, portHandle);
354 return AAUDIO_ERROR_INVALID_HANDLE;
355 }
Phil Burk7ebbc112020-05-13 15:55:17 -0700356 // This is protected by a lock and will just return if already stopped.
Phil Burkbbd52862018-04-13 11:37:42 -0700357 aaudio_result_t result = serviceStream->stop();
358 serviceStream->disconnect();
Phil Burk7ebbc112020-05-13 15:55:17 -0700359 return result;
Eric Laurenta54f1282017-07-01 19:39:32 -0700360}