blob: 443f40da658b3deac0b64a6db325b0bfb23571df [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"
Andy Hung47c5e532017-06-26 18:28:00 -070030#include "AAudioEndpointManager.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080031#include "AAudioService.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080032#include "AAudioServiceStreamMMAP.h"
33#include "AAudioServiceStreamShared.h"
34#include "AAudioServiceStreamMMAP.h"
35#include "binding/IAAudioService.h"
Andy Hung47c5e532017-06-26 18:28:00 -070036#include "ServiceUtilities.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080037#include "utility/HandleTracker.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080038
39using namespace android;
40using namespace aaudio;
41
42typedef enum
43{
Phil Burkc0c70e32017-02-09 13:18:38 -080044 AAUDIO_HANDLE_TYPE_STREAM
Phil Burk5ed503c2017-02-01 09:38:15 -080045} aaudio_service_handle_type_t;
Phil Burkc0c70e32017-02-09 13:18:38 -080046static_assert(AAUDIO_HANDLE_TYPE_STREAM < HANDLE_TRACKER_MAX_TYPES, "Too many handle types.");
Phil Burk5ed503c2017-02-01 09:38:15 -080047
48android::AAudioService::AAudioService()
49 : BnAAudioService() {
Phil Burk2ac035f2017-06-23 14:51:14 -070050 mCachedProcessId = getpid();
51 mCachedUserId = getuid(); // TODO consider using geteuid()
Phil Burk5ed503c2017-02-01 09:38:15 -080052}
53
54AAudioService::~AAudioService() {
55}
56
Andy Hung47c5e532017-06-26 18:28:00 -070057status_t AAudioService::dump(int fd, const Vector<String16>& args) {
58 std::string result;
59
60 if (!dumpAllowed()) {
61 std::stringstream ss;
62 ss << "Permission denial: can't dump AAudioService from pid="
63 << IPCThreadState::self()->getCallingPid() << ", uid="
64 << IPCThreadState::self()->getCallingUid() << "\n";
65 result = ss.str();
66 ALOGW("%s", result.c_str());
67 } else {
68 result = mHandleTracker.dump() + AAudioEndpointManager::getInstance().dump();
69 }
70 (void)write(fd, result.c_str(), result.size());
71 return NO_ERROR;
72}
73
Phil Burkc0c70e32017-02-09 13:18:38 -080074aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request,
75 aaudio::AAudioStreamConfiguration &configurationOutput) {
76 aaudio_result_t result = AAUDIO_OK;
77 AAudioServiceStreamBase *serviceStream = nullptr;
78 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -070079 bool sharingModeMatchRequired = request.isSharingModeMatchRequired();
Phil Burkc0c70e32017-02-09 13:18:38 -080080 aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode();
Phil Burkc0c70e32017-02-09 13:18:38 -080081
82 if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) {
83 ALOGE("AAudioService::openStream(): unrecognized sharing mode = %d", sharingMode);
84 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
85 }
86
87 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) {
Phil Burkc0c70e32017-02-09 13:18:38 -080088 serviceStream = new AAudioServiceStreamMMAP();
89 result = serviceStream->open(request, configurationOutput);
90 if (result != AAUDIO_OK) {
91 // fall back to using a shared stream
92 ALOGD("AAudioService::openStream(), EXCLUSIVE mode failed");
93 delete serviceStream;
94 serviceStream = nullptr;
95 } else {
96 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
97 }
98 }
99
100 // if SHARED requested or if EXCLUSIVE failed
Phil Burk71f35bb2017-04-13 16:05:07 -0700101 if (sharingMode == AAUDIO_SHARING_MODE_SHARED
102 || (serviceStream == nullptr && !sharingModeMatchRequired)) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800103 serviceStream = new AAudioServiceStreamShared(*this);
104 result = serviceStream->open(request, configurationOutput);
105 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED);
106 }
107
108 if (result != AAUDIO_OK) {
109 delete serviceStream;
110 ALOGE("AAudioService::openStream(): failed, return %d", result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800111 return result;
112 } else {
Phil Burk2ac035f2017-06-23 14:51:14 -0700113 const uid_t ownerUserId = request.getUserId(); // only set by service, not by client
114 serviceStream->setOwnerUserId(ownerUserId);
Phil Burk3316d5e2017-02-15 11:23:01 -0800115 aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream);
Phil Burk2ac035f2017-06-23 14:51:14 -0700116 ALOGD("AAudioService::openStream(): handle = 0x%08X owned by %d", handle, ownerUserId);
Phil Burk5ed503c2017-02-01 09:38:15 -0800117 if (handle < 0) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800118 ALOGE("AAudioService::openStream(): handle table full");
Phil Burk5ed503c2017-02-01 09:38:15 -0800119 delete serviceStream;
120 }
121 return handle;
122 }
123}
124
125aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
126 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
127 mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM,
128 streamHandle);
Phil Burkcf5f6d22017-05-26 12:35:07 -0700129 ALOGV("AAudioService.closeStream(0x%08X)", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800130 if (serviceStream != nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800131 serviceStream->close();
Phil Burk5ed503c2017-02-01 09:38:15 -0800132 delete serviceStream;
133 return AAUDIO_OK;
134 }
135 return AAUDIO_ERROR_INVALID_HANDLE;
136}
137
138AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream(
139 aaudio_handle_t streamHandle) const {
Phil Burk2ac035f2017-06-23 14:51:14 -0700140 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
141 mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM, (aaudio_handle_t)streamHandle);
142 if (serviceStream != nullptr) {
143 // Only allow owner or the aaudio service to access the stream.
144 const uid_t callingUserId = IPCThreadState::self()->getCallingUid();
145 const uid_t ownerUserId = serviceStream->getOwnerUserId();
146 bool callerOwnsIt = callingUserId == ownerUserId;
147 bool serverCalling = callingUserId == mCachedUserId;
148 bool serverOwnsIt = ownerUserId == mCachedUserId;
149 bool allowed = callerOwnsIt || serverCalling || serverOwnsIt;
150 if (!allowed) {
151 ALOGE("AAudioService: calling uid %d cannot access stream 0x%08X owned by %d",
152 callingUserId, streamHandle, ownerUserId);
153 serviceStream = nullptr;
154 }
155 }
156 return serviceStream;
Phil Burk5ed503c2017-02-01 09:38:15 -0800157}
158
159aaudio_result_t AAudioService::getStreamDescription(
160 aaudio_handle_t streamHandle,
161 aaudio::AudioEndpointParcelable &parcelable) {
162 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800163 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800164 ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800165 return AAUDIO_ERROR_INVALID_HANDLE;
166 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800167 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800168 // parcelable.dump();
169 return result;
Phil Burk5ed503c2017-02-01 09:38:15 -0800170}
171
172aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
173 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800174 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800175 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800176 return AAUDIO_ERROR_INVALID_HANDLE;
177 }
178 aaudio_result_t result = serviceStream->start();
179 return result;
180}
181
182aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
183 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800184 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800185 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800186 return AAUDIO_ERROR_INVALID_HANDLE;
187 }
188 aaudio_result_t result = serviceStream->pause();
189 return result;
190}
191
Phil Burk71f35bb2017-04-13 16:05:07 -0700192aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
193 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
194 if (serviceStream == nullptr) {
195 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
196 return AAUDIO_ERROR_INVALID_HANDLE;
197 }
198 aaudio_result_t result = serviceStream->stop();
199 return result;
200}
201
Phil Burk5ed503c2017-02-01 09:38:15 -0800202aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
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::flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800206 return AAUDIO_ERROR_INVALID_HANDLE;
207 }
208 return serviceStream->flush();
209}
210
211aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
212 pid_t clientThreadId,
Phil Burk3316d5e2017-02-15 11:23:01 -0800213 int64_t periodNanoseconds) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800214 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::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800217 return AAUDIO_ERROR_INVALID_HANDLE;
218 }
219 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
220 ALOGE("AAudioService::registerAudioThread(), thread already registered");
Phil Burkc0c70e32017-02-09 13:18:38 -0800221 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800222 }
Phil Burk2ac035f2017-06-23 14:51:14 -0700223
224 const pid_t ownerPid = IPCThreadState::self()->getCallingPid(); // TODO review
Phil Burk5ed503c2017-02-01 09:38:15 -0800225 serviceStream->setRegisteredThread(clientThreadId);
Phil Burk2ac035f2017-06-23 14:51:14 -0700226 int err = android::requestPriority(ownerPid, clientThreadId,
Phil Burkc0c70e32017-02-09 13:18:38 -0800227 DEFAULT_AUDIO_PRIORITY, true /* isForApp */);
Phil Burk5ed503c2017-02-01 09:38:15 -0800228 if (err != 0){
Phil Burk2ac035f2017-06-23 14:51:14 -0700229 ALOGE("AAudioService::registerAudioThread(%d) failed, errno = %d, priority = %d",
230 clientThreadId, errno, DEFAULT_AUDIO_PRIORITY);
Phil Burk5ed503c2017-02-01 09:38:15 -0800231 return AAUDIO_ERROR_INTERNAL;
232 } else {
233 return AAUDIO_OK;
234 }
235}
236
237aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800238 pid_t clientThreadId) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800239 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::unregisterAudioThread(), illegal stream handle = 0x%0x",
242 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800243 return AAUDIO_ERROR_INVALID_HANDLE;
244 }
245 if (serviceStream->getRegisteredThread() != clientThreadId) {
246 ALOGE("AAudioService::unregisterAudioThread(), wrong thread");
247 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
248 }
249 serviceStream->setRegisteredThread(0);
250 return AAUDIO_OK;
251}