blob: cac74538b38e15b34611f1b190d324b9174a15b7 [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>
Phil Burkc0c70e32017-02-09 13:18:38 -080026#include <mediautils/SchedulingPolicyService.h>
Andy Hungab7ef302018-05-15 19:35:29 -070027#include <mediautils/ServiceUtilities.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080028#include <utils/String16.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080029
Phil Burkc0c70e32017-02-09 13:18:38 -080030#include "binding/AAudioServiceMessage.h"
Phil Burk11e8d332017-05-24 09:59:02 -070031#include "AAudioClientTracker.h"
Andy Hung47c5e532017-06-26 18:28:00 -070032#include "AAudioEndpointManager.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080033#include "AAudioService.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080034#include "AAudioServiceStreamMMAP.h"
35#include "AAudioServiceStreamShared.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080036#include "binding/IAAudioService.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
42
Phil Burk523b3042017-09-13 13:03:08 -070043using android::AAudioService;
Phil Burk5ed503c2017-02-01 09:38:15 -080044
45android::AAudioService::AAudioService()
46 : BnAAudioService() {
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
53AAudioService::~AAudioService() {
54}
55
Andy Hung47c5e532017-06-26 18:28:00 -070056status_t AAudioService::dump(int fd, const Vector<String16>& args) {
57 std::string result;
58
59 if (!dumpAllowed()) {
60 std::stringstream ss;
Andy Hung6357b5f2018-10-22 19:47:04 -070061 ss << "Permission Denial: can't dump AAudioService from pid="
Andy Hung47c5e532017-06-26 18:28:00 -070062 << IPCThreadState::self()->getCallingPid() << ", uid="
63 << IPCThreadState::self()->getCallingUid() << "\n";
64 result = ss.str();
65 ALOGW("%s", result.c_str());
66 } else {
Phil Burk523b3042017-09-13 13:03:08 -070067 result = "------------ AAudio Service ------------\n"
68 + mStreamTracker.dump()
Phil Burk4501b352017-06-29 18:12:36 -070069 + AAudioClientTracker::getInstance().dump()
70 + AAudioEndpointManager::getInstance().dump();
Andy Hung47c5e532017-06-26 18:28:00 -070071 }
72 (void)write(fd, result.c_str(), result.size());
73 return NO_ERROR;
74}
75
Phil Burk11e8d332017-05-24 09:59:02 -070076void AAudioService::registerClient(const sp<IAAudioClient>& client) {
77 pid_t pid = IPCThreadState::self()->getCallingPid();
78 AAudioClientTracker::getInstance().registerClient(pid, client);
79}
80
Phil Burk2ebf6622019-04-17 11:10:25 -070081bool AAudioService::isCallerInService() {
82 return mAudioClient.clientPid == IPCThreadState::self()->getCallingPid() &&
83 mAudioClient.clientUid == IPCThreadState::self()->getCallingUid();
84}
85
Phil Burkc0c70e32017-02-09 13:18:38 -080086aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request,
87 aaudio::AAudioStreamConfiguration &configurationOutput) {
88 aaudio_result_t result = AAUDIO_OK;
Phil Burk11e8d332017-05-24 09:59:02 -070089 sp<AAudioServiceStreamBase> serviceStream;
Phil Burkc0c70e32017-02-09 13:18:38 -080090 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -070091 bool sharingModeMatchRequired = request.isSharingModeMatchRequired();
Phil Burkc0c70e32017-02-09 13:18:38 -080092 aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode();
Phil Burkc0c70e32017-02-09 13:18:38 -080093
Phil Burk91692942017-06-30 12:23:05 -070094 // Enforce limit on client processes.
95 pid_t pid = request.getProcessId();
Eric Laurenta54f1282017-07-01 19:39:32 -070096 if (pid != mAudioClient.clientPid) {
Phil Burk91692942017-06-30 12:23:05 -070097 int32_t count = AAudioClientTracker::getInstance().getStreamCount(pid);
98 if (count >= MAX_STREAMS_PER_PROCESS) {
Phil Burkfbf031e2017-10-12 15:58:31 -070099 ALOGE("openStream(): exceeded max streams per process %d >= %d",
Phil Burk91692942017-06-30 12:23:05 -0700100 count, MAX_STREAMS_PER_PROCESS);
101 return AAUDIO_ERROR_UNAVAILABLE;
102 }
103 }
104
Phil Burkc0c70e32017-02-09 13:18:38 -0800105 if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700106 ALOGE("openStream(): unrecognized sharing mode = %d", sharingMode);
Phil Burkc0c70e32017-02-09 13:18:38 -0800107 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
108 }
109
110 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) {
Eric Laurenta54f1282017-07-01 19:39:32 -0700111 // only trust audioserver for in service indication
112 bool inService = false;
Phil Burk2ebf6622019-04-17 11:10:25 -0700113 if (isCallerInService()) {
Eric Laurenta54f1282017-07-01 19:39:32 -0700114 inService = request.isInService();
115 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700116 serviceStream = new AAudioServiceStreamMMAP(*this, inService);
117 result = serviceStream->open(request);
Phil Burkc0c70e32017-02-09 13:18:38 -0800118 if (result != AAUDIO_OK) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700119 // Clear it so we can possibly fall back to using a shared stream.
Phil Burkfbf031e2017-10-12 15:58:31 -0700120 ALOGW("openStream(), could not open in EXCLUSIVE mode");
Phil Burk11e8d332017-05-24 09:59:02 -0700121 serviceStream.clear();
Phil Burkc0c70e32017-02-09 13:18:38 -0800122 }
123 }
124
Phil Burka3901e92018-10-08 13:54:38 -0700125 // Try SHARED if SHARED requested or if EXCLUSIVE failed.
Phil Burk15f97c92018-09-04 14:06:27 -0700126 if (sharingMode == AAUDIO_SHARING_MODE_SHARED) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800127 serviceStream = new AAudioServiceStreamShared(*this);
Phil Burk39f02dd2017-08-04 09:13:31 -0700128 result = serviceStream->open(request);
Phil Burk15f97c92018-09-04 14:06:27 -0700129 } else if (serviceStream.get() == nullptr && !sharingModeMatchRequired) {
130 aaudio::AAudioStreamRequest modifiedRequest = request;
131 // Overwrite the original EXCLUSIVE mode with SHARED.
132 modifiedRequest.getConfiguration().setSharingMode(AAUDIO_SHARING_MODE_SHARED);
133 serviceStream = new AAudioServiceStreamShared(*this);
134 result = serviceStream->open(modifiedRequest);
Phil Burkc0c70e32017-02-09 13:18:38 -0800135 }
136
137 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700138 serviceStream.clear();
Phil Burk5ed503c2017-02-01 09:38:15 -0800139 return result;
140 } else {
Phil Burk523b3042017-09-13 13:03:08 -0700141 aaudio_handle_t handle = mStreamTracker.addStreamForHandle(serviceStream.get());
Phil Burk7ba46552019-04-15 08:58:08 -0700142 ALOGV("openStream(): handle = 0x%08X", handle);
Phil Burk523b3042017-09-13 13:03:08 -0700143 serviceStream->setHandle(handle);
144 pid_t pid = request.getProcessId();
145 AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream);
146 configurationOutput.copyFrom(*serviceStream);
Phil Burka9876702020-04-20 18:16:15 -0700147 // Log open in MediaMetrics after we have the handle because we need the handle to
148 // create the metrics ID.
149 serviceStream->logOpen(handle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800150 return handle;
151 }
152}
153
Phil Burk94862522017-09-13 21:31:36 -0700154// If a close request is pending then close the stream
155bool AAudioService::releaseStream(const sp<AAudioServiceStreamBase> &serviceStream) {
156 bool closed = false;
Phil Burk2fe718b2018-05-14 12:28:32 -0700157 // decrementAndRemoveStreamByHandle() uses a lock so that if there are two simultaneous closes
158 // then only one will get the pointer and do the close.
159 sp<AAudioServiceStreamBase> foundStream = mStreamTracker.decrementAndRemoveStreamByHandle(
160 serviceStream->getHandle());
161 if (foundStream.get() != nullptr) {
162 foundStream->close();
163 pid_t pid = foundStream->getOwnerProcessId();
164 AAudioClientTracker::getInstance().unregisterClientStream(pid, foundStream);
Phil Burk94862522017-09-13 21:31:36 -0700165 closed = true;
166 }
167 return closed;
168}
169
170aaudio_result_t AAudioService::checkForPendingClose(
171 const sp<AAudioServiceStreamBase> &serviceStream,
172 aaudio_result_t defaultResult) {
173 return releaseStream(serviceStream) ? AAUDIO_ERROR_INVALID_STATE : defaultResult;
174}
175
Phil Burk5ed503c2017-02-01 09:38:15 -0800176aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
Phil Burk98d6d922017-07-06 11:52:45 -0700177 // Check permission and ownership first.
178 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700179 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700180 ALOGE("closeStream(0x%0x), illegal stream handle", streamHandle);
Phil Burk91692942017-06-30 12:23:05 -0700181 return AAUDIO_ERROR_INVALID_HANDLE;
182 }
183
Phil Burk94862522017-09-13 21:31:36 -0700184 pid_t pid = serviceStream->getOwnerProcessId();
185 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800186
Phil Burk2fe718b2018-05-14 12:28:32 -0700187 serviceStream->markCloseNeeded();
Phil Burk94862522017-09-13 21:31:36 -0700188 (void) releaseStream(serviceStream);
189 return AAUDIO_OK;
190}
Phil Burk523b3042017-09-13 13:03:08 -0700191
192sp<AAudioServiceStreamBase> AAudioService::convertHandleToServiceStream(
193 aaudio_handle_t streamHandle) {
Phil Burk2fe718b2018-05-14 12:28:32 -0700194 sp<AAudioServiceStreamBase> serviceStream = mStreamTracker.getStreamByHandleAndIncrement(
195 streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700196 if (serviceStream.get() != nullptr) {
Phil Burk2ac035f2017-06-23 14:51:14 -0700197 // Only allow owner or the aaudio service to access the stream.
198 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
199 const uid_t ownerUserId = serviceStream->getOwnerUserId();
200 bool callerOwnsIt = callingUserId == ownerUserId;
Eric Laurenta54f1282017-07-01 19:39:32 -0700201 bool serverCalling = callingUserId == mAudioClient.clientUid;
202 bool serverOwnsIt = ownerUserId == mAudioClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700203 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
204 if (!allowed) {
205 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
206 callingUserId, streamHandle, ownerUserId);
Phil Burk2fe718b2018-05-14 12:28:32 -0700207 // We incremented the reference count so we must check if it needs to be closed.
208 checkForPendingClose(serviceStream, AAUDIO_OK);
Phil Burk94862522017-09-13 21:31:36 -0700209 serviceStream.clear();
Phil Burk2ac035f2017-06-23 14:51:14 -0700210 }
211 }
212 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800213}
214
215aaudio_result_t AAudioService::getStreamDescription(
216 aaudio_handle_t streamHandle,
217 aaudio::AudioEndpointParcelable &parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700218 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
219 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700220 ALOGE("getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800221 return AAUDIO_ERROR_INVALID_HANDLE;
222 }
Phil Burk523b3042017-09-13 13:03:08 -0700223
Phil Burkc0c70e32017-02-09 13:18:38 -0800224 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800225 // parcelable.dump();
Phil Burk94862522017-09-13 21:31:36 -0700226 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800227}
228
229aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700230 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
231 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700232 ALOGE("startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800233 return AAUDIO_ERROR_INVALID_HANDLE;
234 }
Eric Laurenta54f1282017-07-01 19:39:32 -0700235
Phil Burk94862522017-09-13 21:31:36 -0700236 aaudio_result_t result = serviceStream->start();
237 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800238}
239
240aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700241 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
242 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700243 ALOGE("pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800244 return AAUDIO_ERROR_INVALID_HANDLE;
245 }
246 aaudio_result_t result = serviceStream->pause();
Phil Burk94862522017-09-13 21:31:36 -0700247 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800248}
249
Phil Burk71f35bb2017-04-13 16:05:07 -0700250aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700251 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
252 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700253 ALOGE("stopStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700254 return AAUDIO_ERROR_INVALID_HANDLE;
255 }
256 aaudio_result_t result = serviceStream->stop();
Phil Burk94862522017-09-13 21:31:36 -0700257 return checkForPendingClose(serviceStream, result);
Phil Burk71f35bb2017-04-13 16:05:07 -0700258}
259
Phil Burk5ed503c2017-02-01 09:38:15 -0800260aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700261 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
262 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700263 ALOGE("flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800264 return AAUDIO_ERROR_INVALID_HANDLE;
265 }
Phil Burk94862522017-09-13 21:31:36 -0700266 aaudio_result_t result = serviceStream->flush();
267 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800268}
269
270aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burk11e8d332017-05-24 09:59:02 -0700271 pid_t clientThreadId,
272 int64_t periodNanoseconds) {
Phil Burk94862522017-09-13 21:31:36 -0700273 aaudio_result_t result = AAUDIO_OK;
Phil Burk523b3042017-09-13 13:03:08 -0700274 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
275 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700276 ALOGE("registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800277 return AAUDIO_ERROR_INVALID_HANDLE;
278 }
279 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
Phil Burk94862522017-09-13 21:31:36 -0700280 ALOGE("AAudioService::registerAudioThread(), thread already registered");
281 result = AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800282 } else {
Phil Burk94862522017-09-13 21:31:36 -0700283 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
Phil Burk2ebf6622019-04-17 11:10:25 -0700284 int32_t priority = isCallerInService()
285 ? kRealTimeAudioPriorityService : kRealTimeAudioPriorityClient;
Phil Burk94862522017-09-13 21:31:36 -0700286 serviceStream->setRegisteredThread(clientThreadId);
287 int err = android::requestPriority(ownerPid, clientThreadId,
Phil Burk2ebf6622019-04-17 11:10:25 -0700288 priority, true /* isForApp */);
Phil Burk94862522017-09-13 21:31:36 -0700289 if (err != 0) {
290 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
Phil Burk2ebf6622019-04-17 11:10:25 -0700291 clientThreadId, errno, priority);
Phil Burk94862522017-09-13 21:31:36 -0700292 result = AAUDIO_ERROR_INTERNAL;
293 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800294 }
Phil Burk94862522017-09-13 21:31:36 -0700295 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800296}
297
298aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800299 pid_t clientThreadId) {
Phil Burk94862522017-09-13 21:31:36 -0700300 aaudio_result_t result = AAUDIO_OK;
Phil Burk523b3042017-09-13 13:03:08 -0700301 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
302 if (serviceStream.get() == nullptr) {
Phil Burkbbd52862018-04-13 11:37:42 -0700303 ALOGE("%s(), illegal stream handle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800304 return AAUDIO_ERROR_INVALID_HANDLE;
305 }
306 if (serviceStream->getRegisteredThread() != clientThreadId) {
Phil Burkbbd52862018-04-13 11:37:42 -0700307 ALOGE("%s(), wrong thread", __func__);
Phil Burk94862522017-09-13 21:31:36 -0700308 result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
309 } else {
310 serviceStream->setRegisteredThread(0);
Phil Burk5ed503c2017-02-01 09:38:15 -0800311 }
Phil Burk94862522017-09-13 21:31:36 -0700312 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800313}
Eric Laurenta54f1282017-07-01 19:39:32 -0700314
315aaudio_result_t AAudioService::startClient(aaudio_handle_t streamHandle,
jiabind1f1cb62020-03-24 11:57:57 -0700316 const android::AudioClient& client,
317 const audio_attributes_t *attr,
318 audio_port_handle_t *clientHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700319 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
320 if (serviceStream.get() == nullptr) {
Phil Burkbbd52862018-04-13 11:37:42 -0700321 ALOGE("%s(), illegal stream handle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700322 return AAUDIO_ERROR_INVALID_HANDLE;
323 }
jiabind1f1cb62020-03-24 11:57:57 -0700324 aaudio_result_t result = serviceStream->startClient(client, attr, clientHandle);
Phil Burk94862522017-09-13 21:31:36 -0700325 return checkForPendingClose(serviceStream, result);
Eric Laurenta54f1282017-07-01 19:39:32 -0700326}
327
328aaudio_result_t AAudioService::stopClient(aaudio_handle_t streamHandle,
Phil Burkbbd52862018-04-13 11:37:42 -0700329 audio_port_handle_t portHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700330 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
331 if (serviceStream.get() == nullptr) {
Phil Burkbbd52862018-04-13 11:37:42 -0700332 ALOGE("%s(), illegal stream handle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700333 return AAUDIO_ERROR_INVALID_HANDLE;
334 }
Phil Burkbbd52862018-04-13 11:37:42 -0700335 aaudio_result_t result = serviceStream->stopClient(portHandle);
336 return checkForPendingClose(serviceStream, result);
337}
338
339// This is only called internally when AudioFlinger wants to tear down a stream.
340// So we do not have to check permissions.
341aaudio_result_t AAudioService::disconnectStreamByPortHandle(audio_port_handle_t portHandle) {
342 ALOGD("%s(%d) called", __func__, portHandle);
343 sp<AAudioServiceStreamBase> serviceStream =
Phil Burk2fe718b2018-05-14 12:28:32 -0700344 mStreamTracker.findStreamByPortHandleAndIncrement(portHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700345 if (serviceStream.get() == nullptr) {
346 ALOGE("%s(), could not find stream with portHandle = %d", __func__, portHandle);
347 return AAUDIO_ERROR_INVALID_HANDLE;
348 }
Phil Burkbbd52862018-04-13 11:37:42 -0700349 aaudio_result_t result = serviceStream->stop();
350 serviceStream->disconnect();
Phil Burk94862522017-09-13 21:31:36 -0700351 return checkForPendingClose(serviceStream, result);
Eric Laurenta54f1282017-07-01 19:39:32 -0700352}