blob: 5b3489511fd1d3771c20d769d1e580b8e6aea82c [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
Andy Hung47c5e532017-06-26 18:28:00 -070021#include <sstream>
Phil Burkc0c70e32017-02-09 13:18:38 -080022//#include <time.h>
23//#include <pthread.h>
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>
27#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"
35#include "AAudioServiceStreamMMAP.h"
36#include "binding/IAAudioService.h"
Andy Hung47c5e532017-06-26 18:28:00 -070037#include "ServiceUtilities.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080038#include "utility/HandleTracker.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080039
40using namespace android;
41using namespace aaudio;
42
Phil Burk91692942017-06-30 12:23:05 -070043#define MAX_STREAMS_PER_PROCESS 8
44
Phil Burk5ed503c2017-02-01 09:38:15 -080045typedef enum
46{
Phil Burkc0c70e32017-02-09 13:18:38 -080047 AAUDIO_HANDLE_TYPE_STREAM
Phil Burk5ed503c2017-02-01 09:38:15 -080048} aaudio_service_handle_type_t;
Phil Burkc0c70e32017-02-09 13:18:38 -080049static_assert(AAUDIO_HANDLE_TYPE_STREAM < HANDLE_TRACKER_MAX_TYPES, "Too many handle types.");
Phil Burk5ed503c2017-02-01 09:38:15 -080050
51android::AAudioService::AAudioService()
52 : BnAAudioService() {
Eric Laurenta54f1282017-07-01 19:39:32 -070053 mAudioClient.clientUid = getuid(); // TODO consider using geteuid()
54 mAudioClient.clientPid = getpid();
55 mAudioClient.packageName = String16("");
Phil Burk11e8d332017-05-24 09:59:02 -070056 AAudioClientTracker::getInstance().setAAudioService(this);
Phil Burk5ed503c2017-02-01 09:38:15 -080057}
58
59AAudioService::~AAudioService() {
60}
61
Andy Hung47c5e532017-06-26 18:28:00 -070062status_t AAudioService::dump(int fd, const Vector<String16>& args) {
63 std::string result;
64
65 if (!dumpAllowed()) {
66 std::stringstream ss;
67 ss << "Permission denial: can't dump AAudioService from pid="
68 << IPCThreadState::self()->getCallingPid() << ", uid="
69 << IPCThreadState::self()->getCallingUid() << "\n";
70 result = ss.str();
71 ALOGW("%s", result.c_str());
72 } else {
Phil Burk4501b352017-06-29 18:12:36 -070073 result = mHandleTracker.dump()
74 + AAudioClientTracker::getInstance().dump()
75 + AAudioEndpointManager::getInstance().dump();
Andy Hung47c5e532017-06-26 18:28:00 -070076 }
77 (void)write(fd, result.c_str(), result.size());
78 return NO_ERROR;
79}
80
Phil Burk11e8d332017-05-24 09:59:02 -070081void AAudioService::registerClient(const sp<IAAudioClient>& client) {
82 pid_t pid = IPCThreadState::self()->getCallingPid();
83 AAudioClientTracker::getInstance().registerClient(pid, client);
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) {
99 ALOGE("AAudioService::openStream(): exceeded max streams per process %d >= %d",
100 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) {
106 ALOGE("AAudioService::openStream(): unrecognized sharing mode = %d", sharingMode);
107 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;
113 if (mAudioClient.clientPid == IPCThreadState::self()->getCallingPid() &&
114 mAudioClient.clientUid == IPCThreadState::self()->getCallingUid()) {
115 inService = request.isInService();
116 }
Phil Burk39f02dd2017-08-04 09:13:31 -0700117 serviceStream = new AAudioServiceStreamMMAP(*this, inService);
118 result = serviceStream->open(request);
Phil Burkc0c70e32017-02-09 13:18:38 -0800119 if (result != AAUDIO_OK) {
Phil Burk39f02dd2017-08-04 09:13:31 -0700120 // Clear it so we can possibly fall back to using a shared stream.
Phil Burk11e8d332017-05-24 09:59:02 -0700121 ALOGW("AAudioService::openStream(), could not open in EXCLUSIVE mode");
122 serviceStream.clear();
Phil Burkc0c70e32017-02-09 13:18:38 -0800123 }
124 }
125
126 // if SHARED requested or if EXCLUSIVE failed
Phil Burk71f35bb2017-04-13 16:05:07 -0700127 if (sharingMode == AAUDIO_SHARING_MODE_SHARED
128 || (serviceStream == nullptr && !sharingModeMatchRequired)) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800129 serviceStream = new AAudioServiceStreamShared(*this);
Phil Burk39f02dd2017-08-04 09:13:31 -0700130 result = serviceStream->open(request);
Phil Burkc0c70e32017-02-09 13:18:38 -0800131 }
132
133 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700134 serviceStream.clear();
135 ALOGE("AAudioService::openStream(): failed, return %d = %s",
136 result, AAudio_convertResultToText(result));
Phil Burk5ed503c2017-02-01 09:38:15 -0800137 return result;
138 } else {
Phil Burk11e8d332017-05-24 09:59:02 -0700139 aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream.get());
Phil Burk5ed503c2017-02-01 09:38:15 -0800140 if (handle < 0) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800141 ALOGE("AAudioService::openStream(): handle table full");
Phil Burk5a26e662017-07-07 12:44:48 -0700142 serviceStream->close();
Phil Burk11e8d332017-05-24 09:59:02 -0700143 serviceStream.clear();
144 } else {
145 ALOGD("AAudioService::openStream(): handle = 0x%08X", handle);
146 serviceStream->setHandle(handle);
147 pid_t pid = request.getProcessId();
148 AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream);
Phil Burk39f02dd2017-08-04 09:13:31 -0700149 configurationOutput.copyFrom(*serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800150 }
151 return handle;
152 }
153}
154
155aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
Phil Burk98d6d922017-07-06 11:52:45 -0700156 // Check permission and ownership first.
157 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk91692942017-06-30 12:23:05 -0700158 if (serviceStream == nullptr) {
159 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
160 return AAUDIO_ERROR_INVALID_HANDLE;
161 }
162
Phil Burk98d6d922017-07-06 11:52:45 -0700163 ALOGD("AAudioService.closeStream(0x%08X)", streamHandle);
164 // Remove handle from tracker so that we cannot look up the raw address any more.
Phil Burk91692942017-06-30 12:23:05 -0700165 serviceStream = (AAudioServiceStreamBase *)
Phil Burk5ed503c2017-02-01 09:38:15 -0800166 mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM,
167 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800168 if (serviceStream != nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800169 serviceStream->close();
Phil Burkb63320a2017-06-30 10:28:20 -0700170 pid_t pid = serviceStream->getOwnerProcessId();
Phil Burk11e8d332017-05-24 09:59:02 -0700171 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800172 return AAUDIO_OK;
173 }
174 return AAUDIO_ERROR_INVALID_HANDLE;
175}
176
177AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream(
178 aaudio_handle_t streamHandle) const {
Phil Burk2ac035f2017-06-23 14:51:14 -0700179 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
180 mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, (aaudio_handle_t)streamHandle);
181 if (serviceStream != nullptr) {
182 // Only allow owner or the aaudio service to access the stream.
183 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
184 const uid_t ownerUserId = serviceStream->getOwnerUserId();
185 bool callerOwnsIt = callingUserId == ownerUserId;
Eric Laurenta54f1282017-07-01 19:39:32 -0700186 bool serverCalling = callingUserId == mAudioClient.clientUid;
187 bool serverOwnsIt = ownerUserId == mAudioClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700188 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
189 if (!allowed) {
190 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
191 callingUserId, streamHandle, ownerUserId);
192 serviceStream = nullptr;
193 }
194 }
195 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800196}
197
198aaudio_result_t AAudioService::getStreamDescription(
199 aaudio_handle_t streamHandle,
200 aaudio::AudioEndpointParcelable &parcelable) {
201 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800202 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800203 ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800204 return AAUDIO_ERROR_INVALID_HANDLE;
205 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800206 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800207 // parcelable.dump();
208 return result;
Phil Burk5ed503c2017-02-01 09:38:15 -0800209}
210
211aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
212 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800213 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800214 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800215 return AAUDIO_ERROR_INVALID_HANDLE;
216 }
Eric Laurenta54f1282017-07-01 19:39:32 -0700217
Phil Burkbcc36742017-08-31 17:24:51 -0700218 return serviceStream->start();
Phil Burk5ed503c2017-02-01 09:38:15 -0800219}
220
221aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
222 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800223 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800224 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800225 return AAUDIO_ERROR_INVALID_HANDLE;
226 }
227 aaudio_result_t result = serviceStream->pause();
228 return result;
229}
230
Phil Burk71f35bb2017-04-13 16:05:07 -0700231aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
232 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
233 if (serviceStream == nullptr) {
234 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
235 return AAUDIO_ERROR_INVALID_HANDLE;
236 }
237 aaudio_result_t result = serviceStream->stop();
238 return result;
239}
240
Phil Burk5ed503c2017-02-01 09:38:15 -0800241aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
242 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800243 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800244 ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800245 return AAUDIO_ERROR_INVALID_HANDLE;
246 }
247 return serviceStream->flush();
248}
249
250aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burk11e8d332017-05-24 09:59:02 -0700251 pid_t clientThreadId,
252 int64_t periodNanoseconds) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800253 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800254 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800255 ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800256 return AAUDIO_ERROR_INVALID_HANDLE;
257 }
258 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
259 ALOGE("AAudioService::registerAudioThread(), thread already registered");
Phil Burkc0c70e32017-02-09 13:18:38 -0800260 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800261 }
Phil Burk2ac035f2017-06-23 14:51:14 -0700262
263 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
Phil Burk5ed503c2017-02-01 09:38:15 -0800264 serviceStream->setRegisteredThread(clientThreadId);
Phil Burk2ac035f2017-06-23 14:51:14 -0700265 int err = android::requestPriority(ownerPid, clientThreadId,
Phil Burkc0c70e32017-02-09 13:18:38 -0800266 DEFAULT_AUDIO_PRIORITY, true /* isForApp */);
Phil Burk5ed503c2017-02-01 09:38:15 -0800267 if (err != 0){
Phil Burk2ac035f2017-06-23 14:51:14 -0700268 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
269 clientThreadId, errno, DEFAULT_AUDIO_PRIORITY);
Phil Burk5ed503c2017-02-01 09:38:15 -0800270 return AAUDIO_ERROR_INTERNAL;
271 } else {
272 return AAUDIO_OK;
273 }
274}
275
276aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800277 pid_t clientThreadId) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800278 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800279 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800280 ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x",
281 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800282 return AAUDIO_ERROR_INVALID_HANDLE;
283 }
284 if (serviceStream->getRegisteredThread() != clientThreadId) {
285 ALOGE("AAudioService::unregisterAudioThread(), wrong thread");
286 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
287 }
288 serviceStream->setRegisteredThread(0);
289 return AAUDIO_OK;
290}
Eric Laurenta54f1282017-07-01 19:39:32 -0700291
292aaudio_result_t AAudioService::startClient(aaudio_handle_t streamHandle,
293 const android::AudioClient& client,
294 audio_port_handle_t *clientHandle) {
295 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
296 if (serviceStream == nullptr) {
297 ALOGE("AAudioService::startClient(), illegal stream handle = 0x%0x",
298 streamHandle);
299 return AAUDIO_ERROR_INVALID_HANDLE;
300 }
301 return serviceStream->startClient(client, clientHandle);
302}
303
304aaudio_result_t AAudioService::stopClient(aaudio_handle_t streamHandle,
305 audio_port_handle_t clientHandle) {
306 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
307 if (serviceStream == nullptr) {
308 ALOGE("AAudioService::stopClient(), illegal stream handle = 0x%0x",
309 streamHandle);
310 return AAUDIO_ERROR_INVALID_HANDLE;
311 }
312 return serviceStream->stopClient(clientHandle);
313}