blob: 3718bee33a822323e62e45107f7bf2229e136f3f [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
43typedef enum
44{
Phil Burkc0c70e32017-02-09 13:18:38 -080045 AAUDIO_HANDLE_TYPE_STREAM
Phil Burk5ed503c2017-02-01 09:38:15 -080046} aaudio_service_handle_type_t;
Phil Burkc0c70e32017-02-09 13:18:38 -080047static_assert(AAUDIO_HANDLE_TYPE_STREAM < HANDLE_TRACKER_MAX_TYPES, "Too many handle types.");
Phil Burk5ed503c2017-02-01 09:38:15 -080048
49android::AAudioService::AAudioService()
50 : BnAAudioService() {
Phil Burk2ac035f2017-06-23 14:51:14 -070051 mCachedProcessId = getpid();
52 mCachedUserId = getuid(); // TODO consider using geteuid()
Phil Burk11e8d332017-05-24 09:59:02 -070053 AAudioClientTracker::getInstance().setAAudioService(this);
Phil Burk5ed503c2017-02-01 09:38:15 -080054}
55
56AAudioService::~AAudioService() {
57}
58
Andy Hung47c5e532017-06-26 18:28:00 -070059status_t AAudioService::dump(int fd, const Vector<String16>& args) {
60 std::string result;
61
62 if (!dumpAllowed()) {
63 std::stringstream ss;
64 ss << "Permission denial: can't dump AAudioService from pid="
65 << IPCThreadState::self()->getCallingPid() << ", uid="
66 << IPCThreadState::self()->getCallingUid() << "\n";
67 result = ss.str();
68 ALOGW("%s", result.c_str());
69 } else {
70 result = mHandleTracker.dump() + AAudioEndpointManager::getInstance().dump();
71 }
72 (void)write(fd, result.c_str(), result.size());
73 return NO_ERROR;
74}
75
Phil Burk11e8d332017-05-24 09:59:02 -070076void AAudioService::registerClient(const sp<IAAudioClient>& client) {
77 pid_t pid = IPCThreadState::self()->getCallingPid();
78 AAudioClientTracker::getInstance().registerClient(pid, client);
79}
80
Phil Burkc0c70e32017-02-09 13:18:38 -080081aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request,
82 aaudio::AAudioStreamConfiguration &configurationOutput) {
83 aaudio_result_t result = AAUDIO_OK;
Phil Burk11e8d332017-05-24 09:59:02 -070084 sp<AAudioServiceStreamBase> serviceStream;
Phil Burkc0c70e32017-02-09 13:18:38 -080085 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -070086 bool sharingModeMatchRequired = request.isSharingModeMatchRequired();
Phil Burkc0c70e32017-02-09 13:18:38 -080087 aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode();
Phil Burkc0c70e32017-02-09 13:18:38 -080088
89 if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) {
90 ALOGE("AAudioService::openStream(): unrecognized sharing mode = %d", sharingMode);
91 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
92 }
93
94 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) {
Phil Burkc0c70e32017-02-09 13:18:38 -080095 serviceStream = new AAudioServiceStreamMMAP();
96 result = serviceStream->open(request, configurationOutput);
97 if (result != AAUDIO_OK) {
98 // fall back to using a shared stream
Phil Burk11e8d332017-05-24 09:59:02 -070099 ALOGW("AAudioService::openStream(), could not open in EXCLUSIVE mode");
100 serviceStream.clear();
Phil Burkc0c70e32017-02-09 13:18:38 -0800101 } else {
102 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
103 }
104 }
105
106 // if SHARED requested or if EXCLUSIVE failed
Phil Burk71f35bb2017-04-13 16:05:07 -0700107 if (sharingMode == AAUDIO_SHARING_MODE_SHARED
108 || (serviceStream == nullptr && !sharingModeMatchRequired)) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800109 serviceStream = new AAudioServiceStreamShared(*this);
110 result = serviceStream->open(request, configurationOutput);
111 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED);
112 }
113
114 if (result != AAUDIO_OK) {
Phil Burk11e8d332017-05-24 09:59:02 -0700115 serviceStream.clear();
116 ALOGE("AAudioService::openStream(): failed, return %d = %s",
117 result, AAudio_convertResultToText(result));
Phil Burk5ed503c2017-02-01 09:38:15 -0800118 return result;
119 } else {
Phil Burk2ac035f2017-06-23 14:51:14 -0700120 const uid_t ownerUserId = request.getUserId(); // only set by service, not by client
121 serviceStream->setOwnerUserId(ownerUserId);
Phil Burk11e8d332017-05-24 09:59:02 -0700122 aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream.get());
Phil Burk5ed503c2017-02-01 09:38:15 -0800123 if (handle < 0) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800124 ALOGE("AAudioService::openStream(): handle table full");
Phil Burk11e8d332017-05-24 09:59:02 -0700125 serviceStream.clear();
126 } else {
127 ALOGD("AAudioService::openStream(): handle = 0x%08X", handle);
128 serviceStream->setHandle(handle);
129 pid_t pid = request.getProcessId();
130 AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800131 }
132 return handle;
133 }
134}
135
136aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
137 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
138 mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM,
139 streamHandle);
Phil Burk11e8d332017-05-24 09:59:02 -0700140 ALOGD("AAudioService.closeStream(0x%08X)", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800141 if (serviceStream != nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800142 serviceStream->close();
Phil Burk11e8d332017-05-24 09:59:02 -0700143 pid_t pid = IPCThreadState::self()->getCallingPid();
144 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800145 return AAUDIO_OK;
146 }
147 return AAUDIO_ERROR_INVALID_HANDLE;
148}
149
150AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream(
151 aaudio_handle_t streamHandle) const {
Phil Burk2ac035f2017-06-23 14:51:14 -0700152 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
153 mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, (aaudio_handle_t)streamHandle);
154 if (serviceStream != nullptr) {
155 // Only allow owner or the aaudio service to access the stream.
156 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
157 const uid_t ownerUserId = serviceStream->getOwnerUserId();
158 bool callerOwnsIt = callingUserId == ownerUserId;
159 bool serverCalling = callingUserId == mCachedUserId;
160 bool serverOwnsIt = ownerUserId == mCachedUserId;
161 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
162 if (!allowed) {
163 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
164 callingUserId, streamHandle, ownerUserId);
165 serviceStream = nullptr;
166 }
167 }
168 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800169}
170
171aaudio_result_t AAudioService::getStreamDescription(
172 aaudio_handle_t streamHandle,
173 aaudio::AudioEndpointParcelable &parcelable) {
174 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800175 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800176 ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800177 return AAUDIO_ERROR_INVALID_HANDLE;
178 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800179 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800180 // parcelable.dump();
181 return result;
Phil Burk5ed503c2017-02-01 09:38:15 -0800182}
183
184aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
185 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800186 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800187 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800188 return AAUDIO_ERROR_INVALID_HANDLE;
189 }
190 aaudio_result_t result = serviceStream->start();
191 return result;
192}
193
194aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
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::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800198 return AAUDIO_ERROR_INVALID_HANDLE;
199 }
200 aaudio_result_t result = serviceStream->pause();
201 return result;
202}
203
Phil Burk71f35bb2017-04-13 16:05:07 -0700204aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
205 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
206 if (serviceStream == nullptr) {
207 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
208 return AAUDIO_ERROR_INVALID_HANDLE;
209 }
210 aaudio_result_t result = serviceStream->stop();
211 return result;
212}
213
Phil Burk5ed503c2017-02-01 09:38:15 -0800214aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
215 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800216 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800217 ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800218 return AAUDIO_ERROR_INVALID_HANDLE;
219 }
220 return serviceStream->flush();
221}
222
223aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burk11e8d332017-05-24 09:59:02 -0700224 pid_t clientThreadId,
225 int64_t periodNanoseconds) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800226 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800227 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800228 ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800229 return AAUDIO_ERROR_INVALID_HANDLE;
230 }
231 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
232 ALOGE("AAudioService::registerAudioThread(), thread already registered");
Phil Burkc0c70e32017-02-09 13:18:38 -0800233 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800234 }
Phil Burk2ac035f2017-06-23 14:51:14 -0700235
236 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
Phil Burk5ed503c2017-02-01 09:38:15 -0800237 serviceStream->setRegisteredThread(clientThreadId);
Phil Burk2ac035f2017-06-23 14:51:14 -0700238 int err = android::requestPriority(ownerPid, clientThreadId,
Phil Burkc0c70e32017-02-09 13:18:38 -0800239 DEFAULT_AUDIO_PRIORITY, true /* isForApp */);
Phil Burk5ed503c2017-02-01 09:38:15 -0800240 if (err != 0){
Phil Burk2ac035f2017-06-23 14:51:14 -0700241 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
242 clientThreadId, errno, DEFAULT_AUDIO_PRIORITY);
Phil Burk5ed503c2017-02-01 09:38:15 -0800243 return AAUDIO_ERROR_INTERNAL;
244 } else {
245 return AAUDIO_OK;
246 }
247}
248
249aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800250 pid_t clientThreadId) {
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::unregisterAudioThread(), illegal stream handle = 0x%0x",
254 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800255 return AAUDIO_ERROR_INVALID_HANDLE;
256 }
257 if (serviceStream->getRegisteredThread() != clientThreadId) {
258 ALOGE("AAudioService::unregisterAudioThread(), wrong thread");
259 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
260 }
261 serviceStream->setRegisteredThread(0);
262 return AAUDIO_OK;
263}