blob: 3992719280e2c5c5361d2eb743fb22326441ba49 [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 Laurentcb4dae22017-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 Laurentcb4dae22017-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 Laurentcb4dae22017-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 }
117 serviceStream = new AAudioServiceStreamMMAP(mAudioClient, inService);
Phil Burkc0c70e32017-02-09 13:18:38 -0800118 result = serviceStream->open(request, configurationOutput);
119 if (result != AAUDIO_OK) {
120 // 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 } else {
124 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
125 }
126 }
127
128 // if SHARED requested or if EXCLUSIVE failed
Phil Burk71f35bb2017-04-13 16:05:07 -0700129 if (sharingMode == AAUDIO_SHARING_MODE_SHARED
130 || (serviceStream == nullptr && !sharingModeMatchRequired)) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800131 serviceStream = new AAudioServiceStreamShared(*this);
132 result = serviceStream->open(request, configurationOutput);
133 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED);
134 }
135
136 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700137 serviceStream.clear();
138 ALOGE("AAudioService::openStream(): failed, return %d = %s",
139 result, AAudio_convertResultToText(result));
Phil Burk5ed503c2017-02-01 09:38:15 -0800140 return result;
141 } else {
Phil Burk11e8d332017-05-24 09:59:02 -0700142 aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream.get());
Phil Burk5ed503c2017-02-01 09:38:15 -0800143 if (handle < 0) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800144 ALOGE("AAudioService::openStream(): handle table full");
Phil Burk5a26e662017-07-07 12:44:48 -0700145 serviceStream->close();
Phil Burk11e8d332017-05-24 09:59:02 -0700146 serviceStream.clear();
147 } else {
148 ALOGD("AAudioService::openStream(): handle = 0x%08X", handle);
149 serviceStream->setHandle(handle);
150 pid_t pid = request.getProcessId();
151 AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800152 }
153 return handle;
154 }
155}
156
157aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
Phil Burk98d6d922017-07-06 11:52:45 -0700158 // Check permission and ownership first.
159 sp<AAudioServiceStreamBase> serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk91692942017-06-30 12:23:05 -0700160 if (serviceStream == nullptr) {
161 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
162 return AAUDIO_ERROR_INVALID_HANDLE;
163 }
164
Phil Burk98d6d922017-07-06 11:52:45 -0700165 ALOGD("AAudioService.closeStream(0x%08X)", streamHandle);
166 // Remove handle from tracker so that we cannot look up the raw address any more.
Phil Burk91692942017-06-30 12:23:05 -0700167 serviceStream = (AAudioServiceStreamBase *)
Phil Burk5ed503c2017-02-01 09:38:15 -0800168 mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM,
169 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800170 if (serviceStream != nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800171 serviceStream->close();
Phil Burkb63320a2017-06-30 10:28:20 -0700172 pid_t pid = serviceStream->getOwnerProcessId();
Phil Burk11e8d332017-05-24 09:59:02 -0700173 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800174 return AAUDIO_OK;
175 }
176 return AAUDIO_ERROR_INVALID_HANDLE;
177}
178
179AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream(
180 aaudio_handle_t streamHandle) const {
Phil Burk2ac035f2017-06-23 14:51:14 -0700181 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
182 mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, (aaudio_handle_t)streamHandle);
183 if (serviceStream != nullptr) {
184 // Only allow owner or the aaudio service to access the stream.
185 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
186 const uid_t ownerUserId = serviceStream->getOwnerUserId();
187 bool callerOwnsIt = callingUserId == ownerUserId;
Eric Laurentcb4dae22017-07-01 19:39:32 -0700188 bool serverCalling = callingUserId == mAudioClient.clientUid;
189 bool serverOwnsIt = ownerUserId == mAudioClient.clientUid;
Phil Burk2ac035f2017-06-23 14:51:14 -0700190 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
191 if (!allowed) {
192 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
193 callingUserId, streamHandle, ownerUserId);
194 serviceStream = nullptr;
195 }
196 }
197 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800198}
199
200aaudio_result_t AAudioService::getStreamDescription(
201 aaudio_handle_t streamHandle,
202 aaudio::AudioEndpointParcelable &parcelable) {
203 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800204 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800205 ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800206 return AAUDIO_ERROR_INVALID_HANDLE;
207 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800208 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800209 // parcelable.dump();
210 return result;
Phil Burk5ed503c2017-02-01 09:38:15 -0800211}
212
213aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
214 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800215 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800216 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800217 return AAUDIO_ERROR_INVALID_HANDLE;
218 }
Eric Laurentcb4dae22017-07-01 19:39:32 -0700219
Phil Burk5ed503c2017-02-01 09:38:15 -0800220 aaudio_result_t result = serviceStream->start();
221 return result;
222}
223
224aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
225 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800226 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800227 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800228 return AAUDIO_ERROR_INVALID_HANDLE;
229 }
230 aaudio_result_t result = serviceStream->pause();
231 return result;
232}
233
Phil Burk71f35bb2017-04-13 16:05:07 -0700234aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
235 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
236 if (serviceStream == nullptr) {
237 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
238 return AAUDIO_ERROR_INVALID_HANDLE;
239 }
240 aaudio_result_t result = serviceStream->stop();
241 return result;
242}
243
Phil Burk5ed503c2017-02-01 09:38:15 -0800244aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
245 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800246 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800247 ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800248 return AAUDIO_ERROR_INVALID_HANDLE;
249 }
250 return serviceStream->flush();
251}
252
253aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burk11e8d332017-05-24 09:59:02 -0700254 pid_t clientThreadId,
255 int64_t periodNanoseconds) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800256 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800257 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800258 ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800259 return AAUDIO_ERROR_INVALID_HANDLE;
260 }
261 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
262 ALOGE("AAudioService::registerAudioThread(), thread already registered");
Phil Burkc0c70e32017-02-09 13:18:38 -0800263 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800264 }
Phil Burk2ac035f2017-06-23 14:51:14 -0700265
266 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
Phil Burk5ed503c2017-02-01 09:38:15 -0800267 serviceStream->setRegisteredThread(clientThreadId);
Phil Burk2ac035f2017-06-23 14:51:14 -0700268 int err = android::requestPriority(ownerPid, clientThreadId,
Phil Burkc0c70e32017-02-09 13:18:38 -0800269 DEFAULT_AUDIO_PRIORITY, true /* isForApp */);
Phil Burk5ed503c2017-02-01 09:38:15 -0800270 if (err != 0){
Phil Burk2ac035f2017-06-23 14:51:14 -0700271 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
272 clientThreadId, errno, DEFAULT_AUDIO_PRIORITY);
Phil Burk5ed503c2017-02-01 09:38:15 -0800273 return AAUDIO_ERROR_INTERNAL;
274 } else {
275 return AAUDIO_OK;
276 }
277}
278
279aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800280 pid_t clientThreadId) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800281 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800282 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800283 ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x",
284 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800285 return AAUDIO_ERROR_INVALID_HANDLE;
286 }
287 if (serviceStream->getRegisteredThread() != clientThreadId) {
288 ALOGE("AAudioService::unregisterAudioThread(), wrong thread");
289 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
290 }
291 serviceStream->setRegisteredThread(0);
292 return AAUDIO_OK;
293}
Eric Laurentcb4dae22017-07-01 19:39:32 -0700294
295aaudio_result_t AAudioService::startClient(aaudio_handle_t streamHandle,
296 const android::AudioClient& client,
297 audio_port_handle_t *clientHandle) {
298 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
299 if (serviceStream == nullptr) {
300 ALOGE("AAudioService::startClient(), illegal stream handle = 0x%0x",
301 streamHandle);
302 return AAUDIO_ERROR_INVALID_HANDLE;
303 }
304 return serviceStream->startClient(client, clientHandle);
305}
306
307aaudio_result_t AAudioService::stopClient(aaudio_handle_t streamHandle,
308 audio_port_handle_t clientHandle) {
309 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
310 if (serviceStream == nullptr) {
311 ALOGE("AAudioService::stopClient(), illegal stream handle = 0x%0x",
312 streamHandle);
313 return AAUDIO_ERROR_INVALID_HANDLE;
314 }
315 return serviceStream->stopClient(clientHandle);
316}