blob: 4ccd2f657b99b08e08406a2a3da3d9a4875c22b4 [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 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 Burk98d6d922017-07-06 11:52:45 -0700153 // Check permission and ownership first.
154 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk91692942017-06-30 12:23:05 -0700155 if (serviceStream == nullptr) {
156 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
157 return AAUDIO_ERROR_INVALID_HANDLE;
158 }
159
Phil Burk98d6d922017-07-06 11:52:45 -0700160 ALOGD("AAudioService.closeStream(0x%08X)", streamHandle);
161 // Remove handle from tracker so that we cannot look up the raw address any more.
Phil Burk91692942017-06-30 12:23:05 -0700162 serviceStream = (AAudioServiceStreamBase *)
Phil Burk5ed503c2017-02-01 09:38:15 -0800163 mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM,
164 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800165 if (serviceStream != nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800166 serviceStream->close();
Phil Burkb63320a2017-06-30 10:28:20 -0700167 pid_t pid = serviceStream->getOwnerProcessId();
Phil Burk11e8d332017-05-24 09:59:02 -0700168 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800169 return AAUDIO_OK;
170 }
171 return AAUDIO_ERROR_INVALID_HANDLE;
172}
173
174AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream(
175 aaudio_handle_t streamHandle) const {
Phil Burk2ac035f2017-06-23 14:51:14 -0700176 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
177 mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, (aaudio_handle_t)streamHandle);
178 if (serviceStream != nullptr) {
179 // Only allow owner or the aaudio service to access the stream.
180 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
181 const uid_t ownerUserId = serviceStream->getOwnerUserId();
182 bool callerOwnsIt = callingUserId == ownerUserId;
183 bool serverCalling = callingUserId == mCachedUserId;
184 bool serverOwnsIt = ownerUserId == mCachedUserId;
185 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
186 if (!allowed) {
187 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
188 callingUserId, streamHandle, ownerUserId);
189 serviceStream = nullptr;
190 }
191 }
192 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800193}
194
195aaudio_result_t AAudioService::getStreamDescription(
196 aaudio_handle_t streamHandle,
197 aaudio::AudioEndpointParcelable &parcelable) {
198 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800199 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800200 ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800201 return AAUDIO_ERROR_INVALID_HANDLE;
202 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800203 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800204 // parcelable.dump();
205 return result;
Phil Burk5ed503c2017-02-01 09:38:15 -0800206}
207
208aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
209 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800210 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800211 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800212 return AAUDIO_ERROR_INVALID_HANDLE;
213 }
214 aaudio_result_t result = serviceStream->start();
215 return result;
216}
217
218aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
219 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800220 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800221 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800222 return AAUDIO_ERROR_INVALID_HANDLE;
223 }
224 aaudio_result_t result = serviceStream->pause();
225 return result;
226}
227
Phil Burk71f35bb2017-04-13 16:05:07 -0700228aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
229 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
230 if (serviceStream == nullptr) {
231 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
232 return AAUDIO_ERROR_INVALID_HANDLE;
233 }
234 aaudio_result_t result = serviceStream->stop();
235 return result;
236}
237
Phil Burk5ed503c2017-02-01 09:38:15 -0800238aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
239 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800240 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800241 ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800242 return AAUDIO_ERROR_INVALID_HANDLE;
243 }
244 return serviceStream->flush();
245}
246
247aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burk11e8d332017-05-24 09:59:02 -0700248 pid_t clientThreadId,
249 int64_t periodNanoseconds) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800250 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800251 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800252 ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800253 return AAUDIO_ERROR_INVALID_HANDLE;
254 }
255 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
256 ALOGE("AAudioService::registerAudioThread(), thread already registered");
Phil Burkc0c70e32017-02-09 13:18:38 -0800257 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800258 }
Phil Burk2ac035f2017-06-23 14:51:14 -0700259
260 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
Phil Burk5ed503c2017-02-01 09:38:15 -0800261 serviceStream->setRegisteredThread(clientThreadId);
Phil Burk2ac035f2017-06-23 14:51:14 -0700262 int err = android::requestPriority(ownerPid, clientThreadId,
Phil Burkc0c70e32017-02-09 13:18:38 -0800263 DEFAULT_AUDIO_PRIORITY, true /* isForApp */);
Phil Burk5ed503c2017-02-01 09:38:15 -0800264 if (err != 0){
Phil Burk2ac035f2017-06-23 14:51:14 -0700265 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
266 clientThreadId, errno, DEFAULT_AUDIO_PRIORITY);
Phil Burk5ed503c2017-02-01 09:38:15 -0800267 return AAUDIO_ERROR_INTERNAL;
268 } else {
269 return AAUDIO_OK;
270 }
271}
272
273aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800274 pid_t clientThreadId) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800275 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800276 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800277 ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x",
278 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800279 return AAUDIO_ERROR_INVALID_HANDLE;
280 }
281 if (serviceStream->getRegisteredThread() != clientThreadId) {
282 ALOGE("AAudioService::unregisterAudioThread(), wrong thread");
283 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
284 }
285 serviceStream->setRegisteredThread(0);
286 return AAUDIO_OK;
287}