blob: c9b906508280007e3d46429e3a0976b5ad489fd1 [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
Phil Burkc0c70e32017-02-09 13:18:38 -080021//#include <time.h>
22//#include <pthread.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080023
Phil Burka4eb0d82017-04-12 15:44:06 -070024#include <aaudio/AAudio.h>
Phil Burkc0c70e32017-02-09 13:18:38 -080025#include <mediautils/SchedulingPolicyService.h>
26#include <utils/String16.h>
Phil Burk5ed503c2017-02-01 09:38:15 -080027
Phil Burkc0c70e32017-02-09 13:18:38 -080028#include "binding/AAudioServiceMessage.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080029#include "AAudioService.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080030#include "AAudioServiceStreamMMAP.h"
31#include "AAudioServiceStreamShared.h"
32#include "AAudioServiceStreamMMAP.h"
33#include "binding/IAAudioService.h"
34#include "utility/HandleTracker.h"
Phil Burk5ed503c2017-02-01 09:38:15 -080035
36using namespace android;
37using namespace aaudio;
38
39typedef enum
40{
Phil Burkc0c70e32017-02-09 13:18:38 -080041 AAUDIO_HANDLE_TYPE_STREAM
Phil Burk5ed503c2017-02-01 09:38:15 -080042} aaudio_service_handle_type_t;
Phil Burkc0c70e32017-02-09 13:18:38 -080043static_assert(AAUDIO_HANDLE_TYPE_STREAM < HANDLE_TRACKER_MAX_TYPES, "Too many handle types.");
Phil Burk5ed503c2017-02-01 09:38:15 -080044
45android::AAudioService::AAudioService()
46 : BnAAudioService() {
47}
48
49AAudioService::~AAudioService() {
50}
51
Phil Burkc0c70e32017-02-09 13:18:38 -080052aaudio_handle_t AAudioService::openStream(const aaudio::AAudioStreamRequest &request,
53 aaudio::AAudioStreamConfiguration &configurationOutput) {
54 aaudio_result_t result = AAUDIO_OK;
55 AAudioServiceStreamBase *serviceStream = nullptr;
56 const AAudioStreamConfiguration &configurationInput = request.getConstantConfiguration();
Phil Burk71f35bb2017-04-13 16:05:07 -070057 bool sharingModeMatchRequired = request.isSharingModeMatchRequired();
Phil Burkc0c70e32017-02-09 13:18:38 -080058 aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode();
Phil Burkc0c70e32017-02-09 13:18:38 -080059
60 if (sharingMode != AAUDIO_SHARING_MODE_EXCLUSIVE && sharingMode != AAUDIO_SHARING_MODE_SHARED) {
61 ALOGE("AAudioService::openStream(): unrecognized sharing mode = %d", sharingMode);
62 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
63 }
64
65 if (sharingMode == AAUDIO_SHARING_MODE_EXCLUSIVE) {
Phil Burkc0c70e32017-02-09 13:18:38 -080066 serviceStream = new AAudioServiceStreamMMAP();
67 result = serviceStream->open(request, configurationOutput);
68 if (result != AAUDIO_OK) {
69 // fall back to using a shared stream
70 ALOGD("AAudioService::openStream(), EXCLUSIVE mode failed");
71 delete serviceStream;
72 serviceStream = nullptr;
73 } else {
74 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
75 }
76 }
77
78 // if SHARED requested or if EXCLUSIVE failed
Phil Burk71f35bb2017-04-13 16:05:07 -070079 if (sharingMode == AAUDIO_SHARING_MODE_SHARED
80 || (serviceStream == nullptr && !sharingModeMatchRequired)) {
Phil Burkc0c70e32017-02-09 13:18:38 -080081 serviceStream = new AAudioServiceStreamShared(*this);
82 result = serviceStream->open(request, configurationOutput);
83 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED);
84 }
85
86 if (result != AAUDIO_OK) {
87 delete serviceStream;
88 ALOGE("AAudioService::openStream(): failed, return %d", result);
Phil Burk5ed503c2017-02-01 09:38:15 -080089 return result;
90 } else {
Phil Burk3316d5e2017-02-15 11:23:01 -080091 aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream);
Phil Burkcf5f6d22017-05-26 12:35:07 -070092 ALOGV("AAudioService::openStream(): handle = 0x%08X", handle);
Phil Burk5ed503c2017-02-01 09:38:15 -080093 if (handle < 0) {
Phil Burkc0c70e32017-02-09 13:18:38 -080094 ALOGE("AAudioService::openStream(): handle table full");
Phil Burk5ed503c2017-02-01 09:38:15 -080095 delete serviceStream;
96 }
97 return handle;
98 }
99}
100
101aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
102 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
103 mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM,
104 streamHandle);
Phil Burkcf5f6d22017-05-26 12:35:07 -0700105 ALOGV("AAudioService.closeStream(0x%08X)", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800106 if (serviceStream != nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800107 serviceStream->close();
Phil Burk5ed503c2017-02-01 09:38:15 -0800108 delete serviceStream;
109 return AAUDIO_OK;
110 }
111 return AAUDIO_ERROR_INVALID_HANDLE;
112}
113
114AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream(
115 aaudio_handle_t streamHandle) const {
116 return (AAudioServiceStreamBase *) mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM,
117 (aaudio_handle_t)streamHandle);
118}
119
120aaudio_result_t AAudioService::getStreamDescription(
121 aaudio_handle_t streamHandle,
122 aaudio::AudioEndpointParcelable &parcelable) {
123 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800124 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800125 ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800126 return AAUDIO_ERROR_INVALID_HANDLE;
127 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800128 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800129 // parcelable.dump();
130 return result;
Phil Burk5ed503c2017-02-01 09:38:15 -0800131}
132
133aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
134 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800135 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800136 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800137 return AAUDIO_ERROR_INVALID_HANDLE;
138 }
139 aaudio_result_t result = serviceStream->start();
140 return result;
141}
142
143aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
144 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800145 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800146 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800147 return AAUDIO_ERROR_INVALID_HANDLE;
148 }
149 aaudio_result_t result = serviceStream->pause();
150 return result;
151}
152
Phil Burk71f35bb2017-04-13 16:05:07 -0700153aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
154 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
155 if (serviceStream == nullptr) {
156 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
157 return AAUDIO_ERROR_INVALID_HANDLE;
158 }
159 aaudio_result_t result = serviceStream->stop();
160 return result;
161}
162
Phil Burk5ed503c2017-02-01 09:38:15 -0800163aaudio_result_t AAudioService::flushStream(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::flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800167 return AAUDIO_ERROR_INVALID_HANDLE;
168 }
169 return serviceStream->flush();
170}
171
172aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800173 pid_t clientProcessId,
Phil Burk5ed503c2017-02-01 09:38:15 -0800174 pid_t clientThreadId,
Phil Burk3316d5e2017-02-15 11:23:01 -0800175 int64_t periodNanoseconds) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800176 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800177 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800178 ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800179 return AAUDIO_ERROR_INVALID_HANDLE;
180 }
181 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
182 ALOGE("AAudioService::registerAudioThread(), thread already registered");
Phil Burkc0c70e32017-02-09 13:18:38 -0800183 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800184 }
185 serviceStream->setRegisteredThread(clientThreadId);
Phil Burkc0c70e32017-02-09 13:18:38 -0800186 int err = android::requestPriority(clientProcessId, clientThreadId,
187 DEFAULT_AUDIO_PRIORITY, true /* isForApp */);
Phil Burk5ed503c2017-02-01 09:38:15 -0800188 if (err != 0){
Phil Burkc0c70e32017-02-09 13:18:38 -0800189 ALOGE("AAudioService::registerAudioThread() failed, errno = %d, priority = %d",
190 errno, DEFAULT_AUDIO_PRIORITY);
Phil Burk5ed503c2017-02-01 09:38:15 -0800191 return AAUDIO_ERROR_INTERNAL;
192 } else {
193 return AAUDIO_OK;
194 }
195}
196
197aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800198 pid_t clientProcessId,
199 pid_t clientThreadId) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800200 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800201 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800202 ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x",
203 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800204 return AAUDIO_ERROR_INVALID_HANDLE;
205 }
206 if (serviceStream->getRegisteredThread() != clientThreadId) {
207 ALOGE("AAudioService::unregisterAudioThread(), wrong thread");
208 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
209 }
210 serviceStream->setRegisteredThread(0);
211 return AAUDIO_OK;
212}