blob: cfcfb11125dd61b4c35644e10e37d5aa374a80ce [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() {
Phil Burk2ac035f2017-06-23 14:51:14 -070053 mCachedProcessId = getpid();
54 mCachedUserId = getuid(); // TODO consider using geteuid()
Phil Burk11e8d332017-05-24 09:59:02 -070055 AAudioClientTracker::getInstance().setAAudioService(this);
Phil Burk5ed503c2017-02-01 09:38:15 -080056}
57
58AAudioService::~AAudioService() {
59}
60
Andy Hung47c5e532017-06-26 18:28:00 -070061status_t AAudioService::dump(int fd, const Vector<String16>& args) {
62 std::string result;
63
64 if (!dumpAllowed()) {
65 std::stringstream ss;
66 ss << "Permission denial: can't dump AAudioService from pid="
67 << IPCThreadState::self()->getCallingPid() << ", uid="
68 << IPCThreadState::self()->getCallingUid() << "\n";
69 result = ss.str();
70 ALOGW("%s", result.c_str());
71 } else {
Phil Burk4501b352017-06-29 18:12:36 -070072 result = mHandleTracker.dump()
73 + AAudioClientTracker::getInstance().dump()
74 + AAudioEndpointManager::getInstance().dump();
Andy Hung47c5e532017-06-26 18:28:00 -070075 }
76 (void)write(fd, result.c_str(), result.size());
77 return NO_ERROR;
78}
79
Phil Burk11e8d332017-05-24 09:59:02 -070080void AAudioService::registerClient(const sp<IAAudioClient>& client) {
81 pid_t pid = IPCThreadState::self()->getCallingPid();
82 AAudioClientTracker::getInstance().registerClient(pid, client);
83}
84
Phil Burkc0c70e32017-02-09 13:18:38 -080085aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request,
86 aaudio::AAudioStreamConfiguration &configurationOutput) {
87 aaudio_result_t result = AAUDIO_OK;
Phil Burk11e8d332017-05-24 09:59:02 -070088 sp<AAudioServiceStreamBase> serviceStream;
Phil Burkc0c70e32017-02-09 13:18:38 -080089 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -070090 bool sharingModeMatchRequired = request.isSharingModeMatchRequired();
Phil Burkc0c70e32017-02-09 13:18:38 -080091 aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode();
Phil Burkc0c70e32017-02-09 13:18:38 -080092
Phil Burk91692942017-06-30 12:23:05 -070093 // Enforce limit on client processes.
94 pid_t pid = request.getProcessId();
95 if (pid != mCachedProcessId) {
96 int32_t count = AAudioClientTracker::getInstance().getStreamCount(pid);
97 if (count >= MAX_STREAMS_PER_PROCESS) {
98 ALOGE("AAudioService::openStream(): exceeded max streams per process %d >= %d",
99 count, MAX_STREAMS_PER_PROCESS);
100 return AAUDIO_ERROR_UNAVAILABLE;
101 }
102 }
103
Phil Burkc0c70e32017-02-09 13:18:38 -0800104 if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) {
105 ALOGE("AAudioService::openStream(): unrecognized sharing mode = %d", sharingMode);
106 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
107 }
108
109 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800110 serviceStream = new AAudioServiceStreamMMAP();
111 result = serviceStream->open(request, configurationOutput);
112 if (result != AAUDIO_OK) {
113 // fall back to using a shared stream
Phil Burk11e8d332017-05-24 09:59:02 -0700114 ALOGW("AAudioService::openStream(), could not open in EXCLUSIVE mode");
115 serviceStream.clear();
Phil Burkc0c70e32017-02-09 13:18:38 -0800116 } else {
117 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
118 }
119 }
120
121 // if SHARED requested or if EXCLUSIVE failed
Phil Burk71f35bb2017-04-13 16:05:07 -0700122 if (sharingMode == AAUDIO_SHARING_MODE_SHARED
123 || (serviceStream == nullptr && !sharingModeMatchRequired)) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800124 serviceStream = new AAudioServiceStreamShared(*this);
125 result = serviceStream->open(request, configurationOutput);
126 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED);
127 }
128
129 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700130 serviceStream.clear();
131 ALOGE("AAudioService::openStream(): failed, return %d = %s",
132 result, AAudio_convertResultToText(result));
Phil Burk5ed503c2017-02-01 09:38:15 -0800133 return result;
134 } else {
Phil Burk2ac035f2017-06-23 14:51:14 -0700135 const uid_t ownerUserId = request.getUserId(); // only set by service, not by client
136 serviceStream->setOwnerUserId(ownerUserId);
Phil Burk11e8d332017-05-24 09:59:02 -0700137 aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream.get());
Phil Burk5ed503c2017-02-01 09:38:15 -0800138 if (handle < 0) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800139 ALOGE("AAudioService::openStream(): handle table full");
Phil Burk11e8d332017-05-24 09:59:02 -0700140 serviceStream.clear();
141 } else {
142 ALOGD("AAudioService::openStream(): handle = 0x%08X", handle);
143 serviceStream->setHandle(handle);
144 pid_t pid = request.getProcessId();
Phil Burkb63320a2017-06-30 10:28:20 -0700145 serviceStream->setOwnerProcessId(pid);
Phil Burk11e8d332017-05-24 09:59:02 -0700146 AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800147 }
148 return handle;
149 }
150}
151
152aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
Phil Burk91692942017-06-30 12:23:05 -0700153 // Check permission first.
154 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
155 if (serviceStream == nullptr) {
156 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
157 return AAUDIO_ERROR_INVALID_HANDLE;
158 }
159
160 serviceStream = (AAudioServiceStreamBase *)
Phil Burk5ed503c2017-02-01 09:38:15 -0800161 mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM,
162 streamHandle);
Phil Burk11e8d332017-05-24 09:59:02 -0700163 ALOGD("AAudioService.closeStream(0x%08X)", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800164 if (serviceStream != nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800165 serviceStream->close();
Phil Burkb63320a2017-06-30 10:28:20 -0700166 pid_t pid = serviceStream->getOwnerProcessId();
Phil Burk11e8d332017-05-24 09:59:02 -0700167 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800168 return AAUDIO_OK;
169 }
170 return AAUDIO_ERROR_INVALID_HANDLE;
171}
172
173AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream(
174 aaudio_handle_t streamHandle) const {
Phil Burk2ac035f2017-06-23 14:51:14 -0700175 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
176 mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, (aaudio_handle_t)streamHandle);
177 if (serviceStream != nullptr) {
178 // Only allow owner or the aaudio service to access the stream.
179 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
180 const uid_t ownerUserId = serviceStream->getOwnerUserId();
181 bool callerOwnsIt = callingUserId == ownerUserId;
182 bool serverCalling = callingUserId == mCachedUserId;
183 bool serverOwnsIt = ownerUserId == mCachedUserId;
184 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
185 if (!allowed) {
186 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
187 callingUserId, streamHandle, ownerUserId);
188 serviceStream = nullptr;
189 }
190 }
191 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800192}
193
194aaudio_result_t AAudioService::getStreamDescription(
195 aaudio_handle_t streamHandle,
196 aaudio::AudioEndpointParcelable &parcelable) {
197 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800198 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800199 ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800200 return AAUDIO_ERROR_INVALID_HANDLE;
201 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800202 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800203 // parcelable.dump();
204 return result;
Phil Burk5ed503c2017-02-01 09:38:15 -0800205}
206
207aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
208 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800209 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800210 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800211 return AAUDIO_ERROR_INVALID_HANDLE;
212 }
213 aaudio_result_t result = serviceStream->start();
214 return result;
215}
216
217aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
218 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800219 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800220 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800221 return AAUDIO_ERROR_INVALID_HANDLE;
222 }
223 aaudio_result_t result = serviceStream->pause();
224 return result;
225}
226
Phil Burk71f35bb2017-04-13 16:05:07 -0700227aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
228 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
229 if (serviceStream == nullptr) {
230 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
231 return AAUDIO_ERROR_INVALID_HANDLE;
232 }
233 aaudio_result_t result = serviceStream->stop();
234 return result;
235}
236
Phil Burk5ed503c2017-02-01 09:38:15 -0800237aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
238 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800239 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800240 ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800241 return AAUDIO_ERROR_INVALID_HANDLE;
242 }
243 return serviceStream->flush();
244}
245
246aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burk11e8d332017-05-24 09:59:02 -0700247 pid_t clientThreadId,
248 int64_t periodNanoseconds) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800249 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800250 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800251 ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800252 return AAUDIO_ERROR_INVALID_HANDLE;
253 }
254 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
255 ALOGE("AAudioService::registerAudioThread(), thread already registered");
Phil Burkc0c70e32017-02-09 13:18:38 -0800256 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800257 }
Phil Burk2ac035f2017-06-23 14:51:14 -0700258
259 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
Phil Burk5ed503c2017-02-01 09:38:15 -0800260 serviceStream->setRegisteredThread(clientThreadId);
Phil Burk2ac035f2017-06-23 14:51:14 -0700261 int err = android::requestPriority(ownerPid, clientThreadId,
Phil Burkc0c70e32017-02-09 13:18:38 -0800262 DEFAULT_AUDIO_PRIORITY, true /* isForApp */);
Phil Burk5ed503c2017-02-01 09:38:15 -0800263 if (err != 0){
Phil Burk2ac035f2017-06-23 14:51:14 -0700264 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
265 clientThreadId, errno, DEFAULT_AUDIO_PRIORITY);
Phil Burk5ed503c2017-02-01 09:38:15 -0800266 return AAUDIO_ERROR_INTERNAL;
267 } else {
268 return AAUDIO_OK;
269 }
270}
271
272aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800273 pid_t clientThreadId) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800274 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800275 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800276 ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x",
277 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800278 return AAUDIO_ERROR_INVALID_HANDLE;
279 }
280 if (serviceStream->getRegisteredThread() != clientThreadId) {
281 ALOGE("AAudioService::unregisterAudioThread(), wrong thread");
282 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
283 }
284 serviceStream->setRegisteredThread(0);
285 return AAUDIO_OK;
286}