blob: 669bb542941fa6ad3e3eccba36ebb1fed3d04434 [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) {
Eric Laurentd51329e2017-06-30 16:06:16 -0700110 serviceStream = new AAudioServiceStreamMMAP(mCachedUserId);
Phil Burkc0c70e32017-02-09 13:18:38 -0800111 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 Burk5a26e662017-07-07 12:44:48 -0700140 serviceStream->close();
Phil Burk11e8d332017-05-24 09:59:02 -0700141 serviceStream.clear();
142 } else {
143 ALOGD("AAudioService::openStream(): handle = 0x%08X", handle);
144 serviceStream->setHandle(handle);
145 pid_t pid = request.getProcessId();
Phil Burkb63320a2017-06-30 10:28:20 -0700146 serviceStream->setOwnerProcessId(pid);
Phil Burk11e8d332017-05-24 09:59:02 -0700147 AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800148 }
149 return handle;
150 }
151}
152
153aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
Phil Burk98d6d922017-07-06 11:52:45 -0700154 // Check permission and ownership first.
155 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk91692942017-06-30 12:23:05 -0700156 if (serviceStream == nullptr) {
157 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
158 return AAUDIO_ERROR_INVALID_HANDLE;
159 }
160
Phil Burk98d6d922017-07-06 11:52:45 -0700161 ALOGD("AAudioService.closeStream(0x%08X)", streamHandle);
162 // Remove handle from tracker so that we cannot look up the raw address any more.
Phil Burk91692942017-06-30 12:23:05 -0700163 serviceStream = (AAudioServiceStreamBase *)
Phil Burk5ed503c2017-02-01 09:38:15 -0800164 mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM,
165 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800166 if (serviceStream != nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800167 serviceStream->close();
Phil Burkb63320a2017-06-30 10:28:20 -0700168 pid_t pid = serviceStream->getOwnerProcessId();
Phil Burk11e8d332017-05-24 09:59:02 -0700169 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800170 return AAUDIO_OK;
171 }
172 return AAUDIO_ERROR_INVALID_HANDLE;
173}
174
175AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream(
176 aaudio_handle_t streamHandle) const {
Phil Burk2ac035f2017-06-23 14:51:14 -0700177 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
178 mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, (aaudio_handle_t)streamHandle);
179 if (serviceStream != nullptr) {
180 // Only allow owner or the aaudio service to access the stream.
181 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
182 const uid_t ownerUserId = serviceStream->getOwnerUserId();
183 bool callerOwnsIt = callingUserId == ownerUserId;
184 bool serverCalling = callingUserId == mCachedUserId;
185 bool serverOwnsIt = ownerUserId == mCachedUserId;
186 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
187 if (!allowed) {
188 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
189 callingUserId, streamHandle, ownerUserId);
190 serviceStream = nullptr;
191 }
192 }
193 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800194}
195
196aaudio_result_t AAudioService::getStreamDescription(
197 aaudio_handle_t streamHandle,
198 aaudio::AudioEndpointParcelable &parcelable) {
199 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800200 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800201 ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800202 return AAUDIO_ERROR_INVALID_HANDLE;
203 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800204 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800205 // parcelable.dump();
206 return result;
Phil Burk5ed503c2017-02-01 09:38:15 -0800207}
208
209aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
210 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800211 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800212 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800213 return AAUDIO_ERROR_INVALID_HANDLE;
214 }
215 aaudio_result_t result = serviceStream->start();
216 return result;
217}
218
219aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
220 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800221 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800222 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800223 return AAUDIO_ERROR_INVALID_HANDLE;
224 }
225 aaudio_result_t result = serviceStream->pause();
226 return result;
227}
228
Phil Burk71f35bb2017-04-13 16:05:07 -0700229aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
230 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
231 if (serviceStream == nullptr) {
232 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
233 return AAUDIO_ERROR_INVALID_HANDLE;
234 }
235 aaudio_result_t result = serviceStream->stop();
236 return result;
237}
238
Phil Burk5ed503c2017-02-01 09:38:15 -0800239aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
240 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800241 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800242 ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800243 return AAUDIO_ERROR_INVALID_HANDLE;
244 }
245 return serviceStream->flush();
246}
247
248aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burk11e8d332017-05-24 09:59:02 -0700249 pid_t clientThreadId,
250 int64_t periodNanoseconds) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800251 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800252 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800253 ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800254 return AAUDIO_ERROR_INVALID_HANDLE;
255 }
256 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
257 ALOGE("AAudioService::registerAudioThread(), thread already registered");
Phil Burkc0c70e32017-02-09 13:18:38 -0800258 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800259 }
Phil Burk2ac035f2017-06-23 14:51:14 -0700260
261 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
Phil Burk5ed503c2017-02-01 09:38:15 -0800262 serviceStream->setRegisteredThread(clientThreadId);
Phil Burk2ac035f2017-06-23 14:51:14 -0700263 int err = android::requestPriority(ownerPid, clientThreadId,
Phil Burkc0c70e32017-02-09 13:18:38 -0800264 DEFAULT_AUDIO_PRIORITY, true /* isForApp */);
Phil Burk5ed503c2017-02-01 09:38:15 -0800265 if (err != 0){
Phil Burk2ac035f2017-06-23 14:51:14 -0700266 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
267 clientThreadId, errno, DEFAULT_AUDIO_PRIORITY);
Phil Burk5ed503c2017-02-01 09:38:15 -0800268 return AAUDIO_ERROR_INTERNAL;
269 } else {
270 return AAUDIO_OK;
271 }
272}
273
274aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800275 pid_t clientThreadId) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800276 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800277 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800278 ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x",
279 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800280 return AAUDIO_ERROR_INVALID_HANDLE;
281 }
282 if (serviceStream->getRegisteredThread() != clientThreadId) {
283 ALOGE("AAudioService::unregisterAudioThread(), wrong thread");
284 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
285 }
286 serviceStream->setRegisteredThread(0);
287 return AAUDIO_OK;
288}