blob: af8c67b3a3bfe8468b9c5529794484a3066059a4 [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 Burk5ed503c2017-02-01 09:38:15 -0800147 return handle;
148 }
149}
150
Phil Burk94862522017-09-13 21:31:36 -0700151// If a close request is pending then close the stream
152bool AAudioService::releaseStream(const sp<AAudioServiceStreamBase> &serviceStream) {
153 bool closed = false;
Phil Burk2fe718b2018-05-14 12:28:32 -0700154 // decrementAndRemoveStreamByHandle() uses a lock so that if there are two simultaneous closes
155 // then only one will get the pointer and do the close.
156 sp<AAudioServiceStreamBase> foundStream = mStreamTracker.decrementAndRemoveStreamByHandle(
157 serviceStream->getHandle());
158 if (foundStream.get() != nullptr) {
159 foundStream->close();
160 pid_t pid = foundStream->getOwnerProcessId();
161 AAudioClientTracker::getInstance().unregisterClientStream(pid, foundStream);
Phil Burk94862522017-09-13 21:31:36 -0700162 closed = true;
163 }
164 return closed;
165}
166
167aaudio_result_t AAudioService::checkForPendingClose(
168 const sp<AAudioServiceStreamBase> &serviceStream,
169 aaudio_result_t defaultResult) {
170 return releaseStream(serviceStream) ? AAUDIO_ERROR_INVALID_STATE : defaultResult;
171}
172
Phil Burk5ed503c2017-02-01 09:38:15 -0800173aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
Phil Burk98d6d922017-07-06 11:52:45 -0700174 // Check permission and ownership first.
175 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700176 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700177 ALOGE("closeStream(0x%0x), illegal stream handle", streamHandle);
Phil Burk91692942017-06-30 12:23:05 -0700178 return AAUDIO_ERROR_INVALID_HANDLE;
179 }
180
Phil Burk94862522017-09-13 21:31:36 -0700181 pid_t pid = serviceStream->getOwnerProcessId();
182 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800183
Phil Burk2fe718b2018-05-14 12:28:32 -0700184 serviceStream->markCloseNeeded();
Phil Burk94862522017-09-13 21:31:36 -0700185 (void) releaseStream(serviceStream);
186 return AAUDIO_OK;
187}
Phil Burk523b3042017-09-13 13:03:08 -0700188
189sp<AAudioServiceStreamBase> AAudioService::convertHandleToServiceStream(
190 aaudio_handle_t streamHandle) {
Phil Burk2fe718b2018-05-14 12:28:32 -0700191 sp<AAudioServiceStreamBase> serviceStream = mStreamTracker.getStreamByHandleAndIncrement(
192 streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700193 if (serviceStream.get() != nullptr) {
Phil Burk2ac035f2017-06-23 14:51:14 -0700194 // Only allow owner or the aaudio service to access the stream.
195 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
196 const uid_t ownerUserId = serviceStream->getOwnerUserId();
197 bool callerOwnsIt = callingUserId == ownerUserId;
Eric Laurenta54f1282017-07-01 19:39:32 -0700198 bool serverCalling = callingUserId == mAudioClient.clientUid;
199 bool serverOwnsIt = ownerUserId == mAudioClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700200 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
201 if (!allowed) {
202 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
203 callingUserId, streamHandle, ownerUserId);
Phil Burk2fe718b2018-05-14 12:28:32 -0700204 // We incremented the reference count so we must check if it needs to be closed.
205 checkForPendingClose(serviceStream, AAUDIO_OK);
Phil Burk94862522017-09-13 21:31:36 -0700206 serviceStream.clear();
Phil Burk2ac035f2017-06-23 14:51:14 -0700207 }
208 }
209 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800210}
211
212aaudio_result_t AAudioService::getStreamDescription(
213 aaudio_handle_t streamHandle,
214 aaudio::AudioEndpointParcelable &parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700215 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
216 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700217 ALOGE("getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800218 return AAUDIO_ERROR_INVALID_HANDLE;
219 }
Phil Burk523b3042017-09-13 13:03:08 -0700220
Phil Burkc0c70e32017-02-09 13:18:38 -0800221 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800222 // parcelable.dump();
Phil Burk94862522017-09-13 21:31:36 -0700223 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800224}
225
226aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700227 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
228 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700229 ALOGE("startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800230 return AAUDIO_ERROR_INVALID_HANDLE;
231 }
Eric Laurenta54f1282017-07-01 19:39:32 -0700232
Phil Burk94862522017-09-13 21:31:36 -0700233 aaudio_result_t result = serviceStream->start();
234 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800235}
236
237aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700238 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
239 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700240 ALOGE("pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800241 return AAUDIO_ERROR_INVALID_HANDLE;
242 }
243 aaudio_result_t result = serviceStream->pause();
Phil Burk94862522017-09-13 21:31:36 -0700244 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800245}
246
Phil Burk71f35bb2017-04-13 16:05:07 -0700247aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700248 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
249 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700250 ALOGE("stopStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700251 return AAUDIO_ERROR_INVALID_HANDLE;
252 }
253 aaudio_result_t result = serviceStream->stop();
Phil Burk94862522017-09-13 21:31:36 -0700254 return checkForPendingClose(serviceStream, result);
Phil Burk71f35bb2017-04-13 16:05:07 -0700255}
256
Phil Burk5ed503c2017-02-01 09:38:15 -0800257aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700258 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
259 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700260 ALOGE("flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800261 return AAUDIO_ERROR_INVALID_HANDLE;
262 }
Phil Burk94862522017-09-13 21:31:36 -0700263 aaudio_result_t result = serviceStream->flush();
264 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800265}
266
267aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burk11e8d332017-05-24 09:59:02 -0700268 pid_t clientThreadId,
269 int64_t periodNanoseconds) {
Phil Burk94862522017-09-13 21:31:36 -0700270 aaudio_result_t result = AAUDIO_OK;
Phil Burk523b3042017-09-13 13:03:08 -0700271 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
272 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700273 ALOGE("registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800274 return AAUDIO_ERROR_INVALID_HANDLE;
275 }
276 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
Phil Burk94862522017-09-13 21:31:36 -0700277 ALOGE("AAudioService::registerAudioThread(), thread already registered");
278 result = AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800279 } else {
Phil Burk94862522017-09-13 21:31:36 -0700280 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
Phil Burk2ebf6622019-04-17 11:10:25 -0700281 int32_t priority = isCallerInService()
282 ? kRealTimeAudioPriorityService : kRealTimeAudioPriorityClient;
Phil Burk94862522017-09-13 21:31:36 -0700283 serviceStream->setRegisteredThread(clientThreadId);
284 int err = android::requestPriority(ownerPid, clientThreadId,
Phil Burk2ebf6622019-04-17 11:10:25 -0700285 priority, true /* isForApp */);
Phil Burk94862522017-09-13 21:31:36 -0700286 if (err != 0) {
287 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
Phil Burk2ebf6622019-04-17 11:10:25 -0700288 clientThreadId, errno, priority);
Phil Burk94862522017-09-13 21:31:36 -0700289 result = AAUDIO_ERROR_INTERNAL;
290 }
Phil Burk5ed503c2017-02-01 09:38:15 -0800291 }
Phil Burk94862522017-09-13 21:31:36 -0700292 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800293}
294
295aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800296 pid_t clientThreadId) {
Phil Burk94862522017-09-13 21:31:36 -0700297 aaudio_result_t result = AAUDIO_OK;
Phil Burk523b3042017-09-13 13:03:08 -0700298 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
299 if (serviceStream.get() == nullptr) {
Phil Burkbbd52862018-04-13 11:37:42 -0700300 ALOGE("%s(), illegal stream handle = 0x%0x", __func__, streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800301 return AAUDIO_ERROR_INVALID_HANDLE;
302 }
303 if (serviceStream->getRegisteredThread() != clientThreadId) {
Phil Burkbbd52862018-04-13 11:37:42 -0700304 ALOGE("%s(), wrong thread", __func__);
Phil Burk94862522017-09-13 21:31:36 -0700305 result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
306 } else {
307 serviceStream->setRegisteredThread(0);
Phil Burk5ed503c2017-02-01 09:38:15 -0800308 }
Phil Burk94862522017-09-13 21:31:36 -0700309 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800310}
Eric Laurenta54f1282017-07-01 19:39:32 -0700311
312aaudio_result_t AAudioService::startClient(aaudio_handle_t streamHandle,
313 const android::AudioClient& client,
314 audio_port_handle_t *clientHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700315 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
316 if (serviceStream.get() == nullptr) {
Phil Burkbbd52862018-04-13 11:37:42 -0700317 ALOGE("%s(), illegal stream handle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700318 return AAUDIO_ERROR_INVALID_HANDLE;
319 }
Phil Burk94862522017-09-13 21:31:36 -0700320 aaudio_result_t result = serviceStream->startClient(client, clientHandle);
321 return checkForPendingClose(serviceStream, result);
Eric Laurenta54f1282017-07-01 19:39:32 -0700322}
323
324aaudio_result_t AAudioService::stopClient(aaudio_handle_t streamHandle,
Phil Burkbbd52862018-04-13 11:37:42 -0700325 audio_port_handle_t portHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700326 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
327 if (serviceStream.get() == nullptr) {
Phil Burkbbd52862018-04-13 11:37:42 -0700328 ALOGE("%s(), illegal stream handle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700329 return AAUDIO_ERROR_INVALID_HANDLE;
330 }
Phil Burkbbd52862018-04-13 11:37:42 -0700331 aaudio_result_t result = serviceStream->stopClient(portHandle);
332 return checkForPendingClose(serviceStream, result);
333}
334
335// This is only called internally when AudioFlinger wants to tear down a stream.
336// So we do not have to check permissions.
337aaudio_result_t AAudioService::disconnectStreamByPortHandle(audio_port_handle_t portHandle) {
338 ALOGD("%s(%d) called", __func__, portHandle);
339 sp<AAudioServiceStreamBase> serviceStream =
Phil Burk2fe718b2018-05-14 12:28:32 -0700340 mStreamTracker.findStreamByPortHandleAndIncrement(portHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700341 if (serviceStream.get() == nullptr) {
342 ALOGE("%s(), could not find stream with portHandle = %d", __func__, portHandle);
343 return AAUDIO_ERROR_INVALID_HANDLE;
344 }
Phil Burkbbd52862018-04-13 11:37:42 -0700345 aaudio_result_t result = serviceStream->stop();
346 serviceStream->disconnect();
Phil Burk94862522017-09-13 21:31:36 -0700347 return checkForPendingClose(serviceStream, result);
Eric Laurenta54f1282017-07-01 19:39:32 -0700348}