blob: 585341a41b440162138fb39d98e14290cd5dc445 [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();
Phil Burkb63320a2017-06-30 10:28:20 -0700130 serviceStream->setOwnerProcessId(pid);
Phil Burk11e8d332017-05-24 09:59:02 -0700131 AAudioClientTracker::getInstance().registerClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800132 }
133 return handle;
134 }
135}
136
137aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
138 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
139 mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM,
140 streamHandle);
Phil Burk11e8d332017-05-24 09:59:02 -0700141 ALOGD("AAudioService.closeStream(0x%08X)", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800142 if (serviceStream != nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800143 serviceStream->close();
Phil Burkb63320a2017-06-30 10:28:20 -0700144 pid_t pid = serviceStream->getOwnerProcessId();
Phil Burk11e8d332017-05-24 09:59:02 -0700145 AAudioClientTracker::getInstance().unregisterClientStream(pid, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -0800146 return AAUDIO_OK;
147 }
148 return AAUDIO_ERROR_INVALID_HANDLE;
149}
150
151AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream(
152 aaudio_handle_t streamHandle) const {
Phil Burk2ac035f2017-06-23 14:51:14 -0700153 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
154 mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, (aaudio_handle_t)streamHandle);
155 if (serviceStream != nullptr) {
156 // Only allow owner or the aaudio service to access the stream.
157 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
158 const uid_t ownerUserId = serviceStream->getOwnerUserId();
159 bool callerOwnsIt = callingUserId == ownerUserId;
160 bool serverCalling = callingUserId == mCachedUserId;
161 bool serverOwnsIt = ownerUserId == mCachedUserId;
162 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
163 if (!allowed) {
164 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
165 callingUserId, streamHandle, ownerUserId);
166 serviceStream = nullptr;
167 }
168 }
169 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800170}
171
172aaudio_result_t AAudioService::getStreamDescription(
173 aaudio_handle_t streamHandle,
174 aaudio::AudioEndpointParcelable &parcelable) {
175 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800176 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800177 ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800178 return AAUDIO_ERROR_INVALID_HANDLE;
179 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800180 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800181 // parcelable.dump();
182 return result;
Phil Burk5ed503c2017-02-01 09:38:15 -0800183}
184
185aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
186 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800187 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800188 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800189 return AAUDIO_ERROR_INVALID_HANDLE;
190 }
191 aaudio_result_t result = serviceStream->start();
192 return result;
193}
194
195aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
196 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800197 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800198 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800199 return AAUDIO_ERROR_INVALID_HANDLE;
200 }
201 aaudio_result_t result = serviceStream->pause();
202 return result;
203}
204
Phil Burk71f35bb2017-04-13 16:05:07 -0700205aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
206 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
207 if (serviceStream == nullptr) {
208 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
209 return AAUDIO_ERROR_INVALID_HANDLE;
210 }
211 aaudio_result_t result = serviceStream->stop();
212 return result;
213}
214
Phil Burk5ed503c2017-02-01 09:38:15 -0800215aaudio_result_t AAudioService::flushStream(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::flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800219 return AAUDIO_ERROR_INVALID_HANDLE;
220 }
221 return serviceStream->flush();
222}
223
224aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burk11e8d332017-05-24 09:59:02 -0700225 pid_t clientThreadId,
226 int64_t periodNanoseconds) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800227 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800228 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800229 ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800230 return AAUDIO_ERROR_INVALID_HANDLE;
231 }
232 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
233 ALOGE("AAudioService::registerAudioThread(), thread already registered");
Phil Burkc0c70e32017-02-09 13:18:38 -0800234 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800235 }
Phil Burk2ac035f2017-06-23 14:51:14 -0700236
237 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
Phil Burk5ed503c2017-02-01 09:38:15 -0800238 serviceStream->setRegisteredThread(clientThreadId);
Phil Burk2ac035f2017-06-23 14:51:14 -0700239 int err = android::requestPriority(ownerPid, clientThreadId,
Phil Burkc0c70e32017-02-09 13:18:38 -0800240 DEFAULT_AUDIO_PRIORITY, true /* isForApp */);
Phil Burk5ed503c2017-02-01 09:38:15 -0800241 if (err != 0){
Phil Burk2ac035f2017-06-23 14:51:14 -0700242 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
243 clientThreadId, errno, DEFAULT_AUDIO_PRIORITY);
Phil Burk5ed503c2017-02-01 09:38:15 -0800244 return AAUDIO_ERROR_INTERNAL;
245 } else {
246 return AAUDIO_OK;
247 }
248}
249
250aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800251 pid_t clientThreadId) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800252 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800253 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800254 ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x",
255 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800256 return AAUDIO_ERROR_INVALID_HANDLE;
257 }
258 if (serviceStream->getRegisteredThread() != clientThreadId) {
259 ALOGE("AAudioService::unregisterAudioThread(), wrong thread");
260 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
261 }
262 serviceStream->setRegisteredThread(0);
263 return AAUDIO_OK;
264}