blob: 91f7885f68703b65e63efd7fc55d80b77b4331ce [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 {
72 result = mHandleTracker.dump() + AAudioEndpointManager::getInstance().dump();
73 }
74 (void)write(fd, result.c_str(), result.size());
75 return NO_ERROR;
76}
77
Phil Burk11e8d332017-05-24 09:59:02 -070078void AAudioService::registerClient(const sp<IAAudioClient>& client) {
79 pid_t pid = IPCThreadState::self()->getCallingPid();
80 AAudioClientTracker::getInstance().registerClient(pid, client);
81}
82
Phil Burkc0c70e32017-02-09 13:18:38 -080083aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request,
84 aaudio::AAudioStreamConfiguration &configurationOutput) {
85 aaudio_result_t result = AAUDIO_OK;
Phil Burk11e8d332017-05-24 09:59:02 -070086 sp<AAudioServiceStreamBase> serviceStream;
Phil Burkc0c70e32017-02-09 13:18:38 -080087 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -070088 bool sharingModeMatchRequired = request.isSharingModeMatchRequired();
Phil Burkc0c70e32017-02-09 13:18:38 -080089 aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode();
Phil Burkc0c70e32017-02-09 13:18:38 -080090
Phil Burk91692942017-06-30 12:23:05 -070091 // Enforce limit on client processes.
92 pid_t pid = request.getProcessId();
93 if (pid != mCachedProcessId) {
94 int32_t count = AAudioClientTracker::getInstance().getStreamCount(pid);
95 if (count >= MAX_STREAMS_PER_PROCESS) {
96 ALOGE("AAudioService::openStream(): exceeded max streams per process %d >= %d",
97 count, MAX_STREAMS_PER_PROCESS);
98 return AAUDIO_ERROR_UNAVAILABLE;
99 }
100 }
101
Phil Burkc0c70e32017-02-09 13:18:38 -0800102 if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) {
103 ALOGE("AAudioService::openStream(): unrecognized sharing mode = %d", sharingMode);
104 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
105 }
106
107 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800108 serviceStream = new AAudioServiceStreamMMAP();
109 result = serviceStream->open(request, configurationOutput);
110 if (result != AAUDIO_OK) {
111 // fall back to using a shared stream
Phil Burk11e8d332017-05-24 09:59:02 -0700112 ALOGW("AAudioService::openStream(), could not open in EXCLUSIVE mode");
113 serviceStream.clear();
Phil Burkc0c70e32017-02-09 13:18:38 -0800114 } else {
115 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
116 }
117 }
118
119 // if SHARED requested or if EXCLUSIVE failed
Phil Burk71f35bb2017-04-13 16:05:07 -0700120 if (sharingMode == AAUDIO_SHARING_MODE_SHARED
121 || (serviceStream == nullptr && !sharingModeMatchRequired)) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800122 serviceStream = new AAudioServiceStreamShared(*this);
123 result = serviceStream->open(request, configurationOutput);
124 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED);
125 }
126
127 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700128 serviceStream.clear();
129 ALOGE("AAudioService::openStream(): failed, return %d = %s",
130 result, AAudio_convertResultToText(result));
Phil Burk5ed503c2017-02-01 09:38:15 -0800131 return result;
132 } else {
Phil Burk2ac035f2017-06-23 14:51:14 -0700133 const uid_t ownerUserId = request.getUserId(); // only set by service, not by client
134 serviceStream->setOwnerUserId(ownerUserId);
Phil Burk11e8d332017-05-24 09:59:02 -0700135 aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream.get());
Phil Burk5ed503c2017-02-01 09:38:15 -0800136 if (handle < 0) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800137 ALOGE("AAudioService::openStream(): handle table full");
Phil Burk11e8d332017-05-24 09:59:02 -0700138 serviceStream.clear();
139 } else {
140 ALOGD("AAudioService::openStream(): handle = 0x%08X", handle);
141 serviceStream->setHandle(handle);
142 pid_t pid = request.getProcessId();
Phil Burkb63320a2017-06-30 10:28:20 -0700143 serviceStream->setOwnerProcessId(pid);
Phil Burk11e8d332017-05-24 09:59:02 -0700144 AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800145 }
146 return handle;
147 }
148}
149
150aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
Phil Burk91692942017-06-30 12:23:05 -0700151 // Check permission first.
152 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
153 if (serviceStream == nullptr) {
154 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
155 return AAUDIO_ERROR_INVALID_HANDLE;
156 }
157
158 serviceStream = (AAudioServiceStreamBase *)
Phil Burk5ed503c2017-02-01 09:38:15 -0800159 mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM,
160 streamHandle);
Phil Burk11e8d332017-05-24 09:59:02 -0700161 ALOGD("AAudioService.closeStream(0x%08X)", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800162 if (serviceStream != nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800163 serviceStream->close();
Phil Burkb63320a2017-06-30 10:28:20 -0700164 pid_t pid = serviceStream->getOwnerProcessId();
Phil Burk11e8d332017-05-24 09:59:02 -0700165 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800166 return AAUDIO_OK;
167 }
168 return AAUDIO_ERROR_INVALID_HANDLE;
169}
170
171AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream(
172 aaudio_handle_t streamHandle) const {
Phil Burk2ac035f2017-06-23 14:51:14 -0700173 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
174 mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, (aaudio_handle_t)streamHandle);
175 if (serviceStream != nullptr) {
176 // Only allow owner or the aaudio service to access the stream.
177 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
178 const uid_t ownerUserId = serviceStream->getOwnerUserId();
179 bool callerOwnsIt = callingUserId == ownerUserId;
180 bool serverCalling = callingUserId == mCachedUserId;
181 bool serverOwnsIt = ownerUserId == mCachedUserId;
182 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
183 if (!allowed) {
184 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
185 callingUserId, streamHandle, ownerUserId);
186 serviceStream = nullptr;
187 }
188 }
189 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800190}
191
192aaudio_result_t AAudioService::getStreamDescription(
193 aaudio_handle_t streamHandle,
194 aaudio::AudioEndpointParcelable &parcelable) {
195 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800196 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800197 ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800198 return AAUDIO_ERROR_INVALID_HANDLE;
199 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800200 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800201 // parcelable.dump();
202 return result;
Phil Burk5ed503c2017-02-01 09:38:15 -0800203}
204
205aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
206 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800207 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800208 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800209 return AAUDIO_ERROR_INVALID_HANDLE;
210 }
211 aaudio_result_t result = serviceStream->start();
212 return result;
213}
214
215aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
216 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800217 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800218 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800219 return AAUDIO_ERROR_INVALID_HANDLE;
220 }
221 aaudio_result_t result = serviceStream->pause();
222 return result;
223}
224
Phil Burk71f35bb2017-04-13 16:05:07 -0700225aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
226 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
227 if (serviceStream == nullptr) {
228 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
229 return AAUDIO_ERROR_INVALID_HANDLE;
230 }
231 aaudio_result_t result = serviceStream->stop();
232 return result;
233}
234
Phil Burk5ed503c2017-02-01 09:38:15 -0800235aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
236 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800237 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800238 ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800239 return AAUDIO_ERROR_INVALID_HANDLE;
240 }
241 return serviceStream->flush();
242}
243
244aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burk11e8d332017-05-24 09:59:02 -0700245 pid_t clientThreadId,
246 int64_t periodNanoseconds) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800247 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800248 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800249 ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800250 return AAUDIO_ERROR_INVALID_HANDLE;
251 }
252 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
253 ALOGE("AAudioService::registerAudioThread(), thread already registered");
Phil Burkc0c70e32017-02-09 13:18:38 -0800254 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800255 }
Phil Burk2ac035f2017-06-23 14:51:14 -0700256
257 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
Phil Burk5ed503c2017-02-01 09:38:15 -0800258 serviceStream->setRegisteredThread(clientThreadId);
Phil Burk2ac035f2017-06-23 14:51:14 -0700259 int err = android::requestPriority(ownerPid, clientThreadId,
Phil Burkc0c70e32017-02-09 13:18:38 -0800260 DEFAULT_AUDIO_PRIORITY, true /* isForApp */);
Phil Burk5ed503c2017-02-01 09:38:15 -0800261 if (err != 0){
Phil Burk2ac035f2017-06-23 14:51:14 -0700262 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
263 clientThreadId, errno, DEFAULT_AUDIO_PRIORITY);
Phil Burk5ed503c2017-02-01 09:38:15 -0800264 return AAUDIO_ERROR_INTERNAL;
265 } else {
266 return AAUDIO_OK;
267 }
268}
269
270aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800271 pid_t clientThreadId) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800272 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800273 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800274 ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x",
275 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800276 return AAUDIO_ERROR_INVALID_HANDLE;
277 }
278 if (serviceStream->getRegisteredThread() != clientThreadId) {
279 ALOGE("AAudioService::unregisterAudioThread(), wrong thread");
280 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
281 }
282 serviceStream->setRegisteredThread(0);
283 return AAUDIO_OK;
284}