blob: ecbcb7eb36322988c85c1b8b6bcdbdc83b052671 [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) {
Phil Burk6e463ce2020-04-13 10:20:20 -070088 // A lock in is used to order the opening of endpoints when an
89 // EXCLUSIVE endpoint is stolen. We want the order to be:
90 // 1) Thread A opens exclusive MMAP endpoint
91 // 2) Thread B wants to open an exclusive MMAP endpoint so it steals the one from A
92 // under this lock.
93 // 3) Thread B opens a shared MMAP endpoint.
94 // 4) Thread A can then get the lock and also open a shared stream.
95 // Without the lock. Thread A might sneak in and reallocate an exclusive stream
96 // before B can open the shared stream.
97 std::unique_lock<std::recursive_mutex> lock(mOpenLock);
98
Phil Burkc0c70e32017-02-09 13:18:38 -080099 aaudio_result_t result = AAUDIO_OK;
Phil Burk11e8d332017-05-24 09:59:02 -0700100 sp<AAudioServiceStreamBase> serviceStream;
Phil Burkc0c70e32017-02-09 13:18:38 -0800101 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -0700102 bool sharingModeMatchRequired = request.isSharingModeMatchRequired();
Phil Burkc0c70e32017-02-09 13:18:38 -0800103 aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode();
Phil Burkc0c70e32017-02-09 13:18:38 -0800104
Phil Burk91692942017-06-30 12:23:05 -0700105 // Enforce limit on client processes.
106 pid_t pid = request.getProcessId();
Eric Laurenta54f1282017-07-01 19:39:32 -0700107 if (pid != mAudioClient.clientPid) {
Phil Burk91692942017-06-30 12:23:05 -0700108 int32_t count = AAudioClientTracker::getInstance().getStreamCount(pid);
109 if (count >= MAX_STREAMS_PER_PROCESS) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700110 ALOGE("openStream(): exceeded max streams per process %d >= %d",
Phil Burk91692942017-06-30 12:23:05 -0700111 count, MAX_STREAMS_PER_PROCESS);
112 return AAUDIO_ERROR_UNAVAILABLE;
113 }
114 }
115
Phil Burkc0c70e32017-02-09 13:18:38 -0800116 if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700117 ALOGE("openStream(): unrecognized sharing mode = %d", sharingMode);
Phil Burkc0c70e32017-02-09 13:18:38 -0800118 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
119 }
120
121 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) {
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
Phil Burk94862522017-09-13 21:31:36 -0700165// If a close request is pending then close the stream
166bool AAudioService::releaseStream(const sp<AAudioServiceStreamBase> &serviceStream) {
167 bool closed = false;
Phil Burk2fe718b2018-05-14 12:28:32 -0700168 // decrementAndRemoveStreamByHandle() uses a lock so that if there are two simultaneous closes
169 // then only one will get the pointer and do the close.
170 sp<AAudioServiceStreamBase> foundStream = mStreamTracker.decrementAndRemoveStreamByHandle(
171 serviceStream->getHandle());
172 if (foundStream.get() != nullptr) {
173 foundStream->close();
174 pid_t pid = foundStream->getOwnerProcessId();
175 AAudioClientTracker::getInstance().unregisterClientStream(pid, foundStream);
Phil Burk94862522017-09-13 21:31:36 -0700176 closed = true;
177 }
178 return closed;
179}
180
181aaudio_result_t AAudioService::checkForPendingClose(
182 const sp<AAudioServiceStreamBase> &serviceStream,
183 aaudio_result_t defaultResult) {
184 return releaseStream(serviceStream) ? AAUDIO_ERROR_INVALID_STATE : defaultResult;
185}
186
Phil Burk5ed503c2017-02-01 09:38:15 -0800187aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
Phil Burk98d6d922017-07-06 11:52:45 -0700188 // Check permission and ownership first.
189 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700190 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700191 ALOGE("closeStream(0x%0x), illegal stream handle", streamHandle);
Phil Burk91692942017-06-30 12:23:05 -0700192 return AAUDIO_ERROR_INVALID_HANDLE;
193 }
Phil Burk6e463ce2020-04-13 10:20:20 -0700194 return closeStream(serviceStream);
195}
Phil Burk91692942017-06-30 12:23:05 -0700196
Phil Burk6e463ce2020-04-13 10:20:20 -0700197aaudio_result_t AAudioService::closeStream(sp<AAudioServiceStreamBase> serviceStream) {
Phil Burk94862522017-09-13 21:31:36 -0700198 pid_t pid = serviceStream->getOwnerProcessId();
199 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800200
Phil Burk2fe718b2018-05-14 12:28:32 -0700201 serviceStream->markCloseNeeded();
Phil Burk94862522017-09-13 21:31:36 -0700202 (void) releaseStream(serviceStream);
203 return AAUDIO_OK;
204}
Phil Burk523b3042017-09-13 13:03:08 -0700205
206sp<AAudioServiceStreamBase> AAudioService::convertHandleToServiceStream(
207 aaudio_handle_t streamHandle) {
Phil Burk2fe718b2018-05-14 12:28:32 -0700208 sp<AAudioServiceStreamBase> serviceStream = mStreamTracker.getStreamByHandleAndIncrement(
209 streamHandle);
Phil Burk523b3042017-09-13 13:03:08 -0700210 if (serviceStream.get() != nullptr) {
Phil Burk2ac035f2017-06-23 14:51:14 -0700211 // Only allow owner or the aaudio service to access the stream.
212 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
213 const uid_t ownerUserId = serviceStream->getOwnerUserId();
214 bool callerOwnsIt = callingUserId == ownerUserId;
Eric Laurenta54f1282017-07-01 19:39:32 -0700215 bool serverCalling = callingUserId == mAudioClient.clientUid;
216 bool serverOwnsIt = ownerUserId == mAudioClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700217 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
218 if (!allowed) {
219 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
220 callingUserId, streamHandle, ownerUserId);
Phil Burk2fe718b2018-05-14 12:28:32 -0700221 // We incremented the reference count so we must check if it needs to be closed.
222 checkForPendingClose(serviceStream, AAUDIO_OK);
Phil Burk94862522017-09-13 21:31:36 -0700223 serviceStream.clear();
Phil Burk2ac035f2017-06-23 14:51:14 -0700224 }
225 }
226 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800227}
228
229aaudio_result_t AAudioService::getStreamDescription(
230 aaudio_handle_t streamHandle,
231 aaudio::AudioEndpointParcelable &parcelable) {
Phil Burk523b3042017-09-13 13:03:08 -0700232 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
233 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700234 ALOGE("getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800235 return AAUDIO_ERROR_INVALID_HANDLE;
236 }
Phil Burk523b3042017-09-13 13:03:08 -0700237
Phil Burkc0c70e32017-02-09 13:18:38 -0800238 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800239 // parcelable.dump();
Phil Burk94862522017-09-13 21:31:36 -0700240 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800241}
242
243aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700244 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
245 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700246 ALOGE("startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800247 return AAUDIO_ERROR_INVALID_HANDLE;
248 }
Eric Laurenta54f1282017-07-01 19:39:32 -0700249
Phil Burk94862522017-09-13 21:31:36 -0700250 aaudio_result_t result = serviceStream->start();
251 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800252}
253
254aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700255 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
256 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700257 ALOGE("pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800258 return AAUDIO_ERROR_INVALID_HANDLE;
259 }
260 aaudio_result_t result = serviceStream->pause();
Phil Burk94862522017-09-13 21:31:36 -0700261 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800262}
263
Phil Burk71f35bb2017-04-13 16:05:07 -0700264aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700265 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
266 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700267 ALOGE("stopStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk71f35bb2017-04-13 16:05:07 -0700268 return AAUDIO_ERROR_INVALID_HANDLE;
269 }
270 aaudio_result_t result = serviceStream->stop();
Phil Burk94862522017-09-13 21:31:36 -0700271 return checkForPendingClose(serviceStream, result);
Phil Burk71f35bb2017-04-13 16:05:07 -0700272}
273
Phil Burk5ed503c2017-02-01 09:38:15 -0800274aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700275 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
276 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700277 ALOGE("flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800278 return AAUDIO_ERROR_INVALID_HANDLE;
279 }
Phil Burk94862522017-09-13 21:31:36 -0700280 aaudio_result_t result = serviceStream->flush();
281 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800282}
283
284aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burk11e8d332017-05-24 09:59:02 -0700285 pid_t clientThreadId,
286 int64_t periodNanoseconds) {
Phil Burk94862522017-09-13 21:31:36 -0700287 aaudio_result_t result = AAUDIO_OK;
Phil Burk523b3042017-09-13 13:03:08 -0700288 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
289 if (serviceStream.get() == nullptr) {
Phil Burkfbf031e2017-10-12 15:58:31 -0700290 ALOGE("registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800291 return AAUDIO_ERROR_INVALID_HANDLE;
292 }
293 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
Phil Burk94862522017-09-13 21:31:36 -0700294 ALOGE("AAudioService::registerAudioThread(), thread already registered");
295 result = AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800296 } else {
Phil Burk94862522017-09-13 21:31:36 -0700297 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
Phil Burk2ebf6622019-04-17 11:10:25 -0700298 int32_t priority = isCallerInService()
299 ? kRealTimeAudioPriorityService : kRealTimeAudioPriorityClient;
Phil Burk94862522017-09-13 21:31:36 -0700300 serviceStream->setRegisteredThread(clientThreadId);
301 int err = android::requestPriority(ownerPid, clientThreadId,
Phil Burk2ebf6622019-04-17 11:10:25 -0700302 priority, true /* isForApp */);
Phil Burk94862522017-09-13 21:31:36 -0700303 if (err != 0) {
304 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
Phil Burk2ebf6622019-04-17 11:10:25 -0700305 clientThreadId, errno, priority);
Phil Burk94862522017-09-13 21:31:36 -0700306 result = AAUDIO_ERROR_INTERNAL;
307 }
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}
311
312aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800313 pid_t clientThreadId) {
Phil Burk94862522017-09-13 21:31:36 -0700314 aaudio_result_t result = AAUDIO_OK;
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);
Phil Burk5ed503c2017-02-01 09:38:15 -0800318 return AAUDIO_ERROR_INVALID_HANDLE;
319 }
320 if (serviceStream->getRegisteredThread() != clientThreadId) {
Phil Burkbbd52862018-04-13 11:37:42 -0700321 ALOGE("%s(), wrong thread", __func__);
Phil Burk94862522017-09-13 21:31:36 -0700322 result = AAUDIO_ERROR_ILLEGAL_ARGUMENT;
323 } else {
324 serviceStream->setRegisteredThread(0);
Phil Burk5ed503c2017-02-01 09:38:15 -0800325 }
Phil Burk94862522017-09-13 21:31:36 -0700326 return checkForPendingClose(serviceStream, result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800327}
Eric Laurenta54f1282017-07-01 19:39:32 -0700328
329aaudio_result_t AAudioService::startClient(aaudio_handle_t streamHandle,
jiabind1f1cb62020-03-24 11:57:57 -0700330 const android::AudioClient& client,
331 const audio_attributes_t *attr,
332 audio_port_handle_t *clientHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700333 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
334 if (serviceStream.get() == nullptr) {
Phil Burkbbd52862018-04-13 11:37:42 -0700335 ALOGE("%s(), illegal stream handle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700336 return AAUDIO_ERROR_INVALID_HANDLE;
337 }
jiabind1f1cb62020-03-24 11:57:57 -0700338 aaudio_result_t result = serviceStream->startClient(client, attr, clientHandle);
Phil Burk94862522017-09-13 21:31:36 -0700339 return checkForPendingClose(serviceStream, result);
Eric Laurenta54f1282017-07-01 19:39:32 -0700340}
341
342aaudio_result_t AAudioService::stopClient(aaudio_handle_t streamHandle,
Phil Burkbbd52862018-04-13 11:37:42 -0700343 audio_port_handle_t portHandle) {
Phil Burk523b3042017-09-13 13:03:08 -0700344 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
345 if (serviceStream.get() == nullptr) {
Phil Burkbbd52862018-04-13 11:37:42 -0700346 ALOGE("%s(), illegal stream handle = 0x%0x", __func__, streamHandle);
Eric Laurenta54f1282017-07-01 19:39:32 -0700347 return AAUDIO_ERROR_INVALID_HANDLE;
348 }
Phil Burkbbd52862018-04-13 11:37:42 -0700349 aaudio_result_t result = serviceStream->stopClient(portHandle);
350 return checkForPendingClose(serviceStream, result);
351}
352
353// This is only called internally when AudioFlinger wants to tear down a stream.
354// So we do not have to check permissions.
355aaudio_result_t AAudioService::disconnectStreamByPortHandle(audio_port_handle_t portHandle) {
356 ALOGD("%s(%d) called", __func__, portHandle);
357 sp<AAudioServiceStreamBase> serviceStream =
Phil Burk2fe718b2018-05-14 12:28:32 -0700358 mStreamTracker.findStreamByPortHandleAndIncrement(portHandle);
Phil Burkbbd52862018-04-13 11:37:42 -0700359 if (serviceStream.get() == nullptr) {
360 ALOGE("%s(), could not find stream with portHandle = %d", __func__, portHandle);
361 return AAUDIO_ERROR_INVALID_HANDLE;
362 }
Phil Burkbbd52862018-04-13 11:37:42 -0700363 aaudio_result_t result = serviceStream->stop();
364 serviceStream->disconnect();
Phil Burk94862522017-09-13 21:31:36 -0700365 return checkForPendingClose(serviceStream, result);
Eric Laurenta54f1282017-07-01 19:39:32 -0700366}