blob: 0841127ce3e06282d16b38e290d48a1035da913e [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() {
50}
51
52AAudioService::~AAudioService() {
53}
54
Andy Hung47c5e532017-06-26 18:28:00 -070055status_t AAudioService::dump(int fd, const Vector<String16>& args) {
56 std::string result;
57
58 if (!dumpAllowed()) {
59 std::stringstream ss;
60 ss << "Permission denial: can't dump AAudioService from pid="
61 << IPCThreadState::self()->getCallingPid() << ", uid="
62 << IPCThreadState::self()->getCallingUid() << "\n";
63 result = ss.str();
64 ALOGW("%s", result.c_str());
65 } else {
66 result = mHandleTracker.dump() + AAudioEndpointManager::getInstance().dump();
67 }
68 (void)write(fd, result.c_str(), result.size());
69 return NO_ERROR;
70}
71
Phil Burkc0c70e32017-02-09 13:18:38 -080072aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request,
73 aaudio::AAudioStreamConfiguration &configurationOutput) {
74 aaudio_result_t result = AAUDIO_OK;
75 AAudioServiceStreamBase *serviceStream = nullptr;
76 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -070077 bool sharingModeMatchRequired = request.isSharingModeMatchRequired();
Phil Burkc0c70e32017-02-09 13:18:38 -080078 aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode();
Phil Burkc0c70e32017-02-09 13:18:38 -080079
80 if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) {
81 ALOGE("AAudioService::openStream(): unrecognized sharing mode = %d", sharingMode);
82 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
83 }
84
85 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) {
Phil Burkc0c70e32017-02-09 13:18:38 -080086 serviceStream = new AAudioServiceStreamMMAP();
87 result = serviceStream->open(request, configurationOutput);
88 if (result != AAUDIO_OK) {
89 // fall back to using a shared stream
90 ALOGD("AAudioService::openStream(), EXCLUSIVE mode failed");
91 delete serviceStream;
92 serviceStream = nullptr;
93 } else {
94 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
95 }
96 }
97
98 // if SHARED requested or if EXCLUSIVE failed
Phil Burk71f35bb2017-04-13 16:05:07 -070099 if (sharingMode == AAUDIO_SHARING_MODE_SHARED
100 || (serviceStream == nullptr && !sharingModeMatchRequired)) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800101 serviceStream = new AAudioServiceStreamShared(*this);
102 result = serviceStream->open(request, configurationOutput);
103 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED);
104 }
105
106 if (result != AAUDIO_OK) {
107 delete serviceStream;
108 ALOGE("AAudioService::openStream(): failed, return %d", result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800109 return result;
110 } else {
Phil Burk3316d5e2017-02-15 11:23:01 -0800111 aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream);
Phil Burkec89b2e2017-06-20 15:05:06 -0700112 ALOGD("AAudioService::openStream(): handle = 0x%08X", handle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800113 if (handle < 0) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800114 ALOGE("AAudioService::openStream(): handle table full");
Phil Burk5ed503c2017-02-01 09:38:15 -0800115 delete serviceStream;
116 }
117 return handle;
118 }
119}
120
121aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
122 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
123 mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM,
124 streamHandle);
Phil Burkcf5f6d22017-05-26 12:35:07 -0700125 ALOGV("AAudioService.closeStream(0x%08X)", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800126 if (serviceStream != nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800127 serviceStream->close();
Phil Burk5ed503c2017-02-01 09:38:15 -0800128 delete serviceStream;
129 return AAUDIO_OK;
130 }
131 return AAUDIO_ERROR_INVALID_HANDLE;
132}
133
134AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream(
135 aaudio_handle_t streamHandle) const {
136 return (AAudioServiceStreamBase *) mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM,
137 (aaudio_handle_t)streamHandle);
138}
139
140aaudio_result_t AAudioService::getStreamDescription(
141 aaudio_handle_t streamHandle,
142 aaudio::AudioEndpointParcelable &parcelable) {
143 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800144 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800145 ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800146 return AAUDIO_ERROR_INVALID_HANDLE;
147 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800148 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800149 // parcelable.dump();
150 return result;
Phil Burk5ed503c2017-02-01 09:38:15 -0800151}
152
153aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
154 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800155 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800156 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800157 return AAUDIO_ERROR_INVALID_HANDLE;
158 }
159 aaudio_result_t result = serviceStream->start();
160 return result;
161}
162
163aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
164 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800165 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800166 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800167 return AAUDIO_ERROR_INVALID_HANDLE;
168 }
169 aaudio_result_t result = serviceStream->pause();
170 return result;
171}
172
Phil Burk71f35bb2017-04-13 16:05:07 -0700173aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
174 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
175 if (serviceStream == nullptr) {
176 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
177 return AAUDIO_ERROR_INVALID_HANDLE;
178 }
179 aaudio_result_t result = serviceStream->stop();
180 return result;
181}
182
Phil Burk5ed503c2017-02-01 09:38:15 -0800183aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
184 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800185 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800186 ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800187 return AAUDIO_ERROR_INVALID_HANDLE;
188 }
189 return serviceStream->flush();
190}
191
192aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800193 pid_t clientProcessId,
Phil Burk5ed503c2017-02-01 09:38:15 -0800194 pid_t clientThreadId,
Phil Burk3316d5e2017-02-15 11:23:01 -0800195 int64_t periodNanoseconds) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800196 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::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800199 return AAUDIO_ERROR_INVALID_HANDLE;
200 }
201 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
202 ALOGE("AAudioService::registerAudioThread(), thread already registered");
Phil Burkc0c70e32017-02-09 13:18:38 -0800203 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800204 }
205 serviceStream->setRegisteredThread(clientThreadId);
Phil Burkc0c70e32017-02-09 13:18:38 -0800206 int err = android::requestPriority(clientProcessId, clientThreadId,
207 DEFAULT_AUDIO_PRIORITY, true /* isForApp */);
Phil Burk5ed503c2017-02-01 09:38:15 -0800208 if (err != 0){
Phil Burkc0c70e32017-02-09 13:18:38 -0800209 ALOGE("AAudioService::registerAudioThread() failed, errno = %d, priority = %d",
210 errno, DEFAULT_AUDIO_PRIORITY);
Phil Burk5ed503c2017-02-01 09:38:15 -0800211 return AAUDIO_ERROR_INTERNAL;
212 } else {
213 return AAUDIO_OK;
214 }
215}
216
217aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800218 pid_t clientProcessId,
219 pid_t clientThreadId) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800220 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800221 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800222 ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x",
223 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800224 return AAUDIO_ERROR_INVALID_HANDLE;
225 }
226 if (serviceStream->getRegisteredThread() != clientThreadId) {
227 ALOGE("AAudioService::unregisterAudioThread(), wrong thread");
228 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
229 }
230 serviceStream->setRegisteredThread(0);
231 return AAUDIO_OK;
232}