blob: 816d5ab697278a0093c81b42a7c07c07e76fba4b [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) {
66 ALOGD("AAudioService::openStream(), sharingMode = AAUDIO_SHARING_MODE_EXCLUSIVE");
67 serviceStream = new AAudioServiceStreamMMAP();
68 result = serviceStream->open(request, configurationOutput);
69 if (result != AAUDIO_OK) {
70 // fall back to using a shared stream
71 ALOGD("AAudioService::openStream(), EXCLUSIVE mode failed");
72 delete serviceStream;
73 serviceStream = nullptr;
74 } else {
75 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_EXCLUSIVE);
76 }
77 }
78
79 // if SHARED requested or if EXCLUSIVE failed
Phil Burk71f35bb2017-04-13 16:05:07 -070080 if (sharingMode == AAUDIO_SHARING_MODE_SHARED
81 || (serviceStream == nullptr && !sharingModeMatchRequired)) {
82 ALOGD("AAudioService::openStream(), try AAUDIO_SHARING_MODE_SHARED");
Phil Burkc0c70e32017-02-09 13:18:38 -080083 serviceStream = new AAudioServiceStreamShared(*this);
84 result = serviceStream->open(request, configurationOutput);
85 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED);
86 }
87
88 if (result != AAUDIO_OK) {
89 delete serviceStream;
90 ALOGE("AAudioService::openStream(): failed, return %d", result);
Phil Burk5ed503c2017-02-01 09:38:15 -080091 return result;
92 } else {
Phil Burk3316d5e2017-02-15 11:23:01 -080093 aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -080094 ALOGD("AAudioService::openStream(): handle = 0x%08X", handle);
95 if (handle < 0) {
Phil Burkc0c70e32017-02-09 13:18:38 -080096 ALOGE("AAudioService::openStream(): handle table full");
Phil Burk5ed503c2017-02-01 09:38:15 -080097 delete serviceStream;
98 }
99 return handle;
100 }
101}
102
103aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
104 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
105 mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM,
106 streamHandle);
107 ALOGD("AAudioService.closeStream(0x%08X)", streamHandle);
108 if (serviceStream != nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800109 serviceStream->close();
Phil Burk5ed503c2017-02-01 09:38:15 -0800110 delete serviceStream;
111 return AAUDIO_OK;
112 }
113 return AAUDIO_ERROR_INVALID_HANDLE;
114}
115
116AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream(
117 aaudio_handle_t streamHandle) const {
118 return (AAudioServiceStreamBase *) mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM,
119 (aaudio_handle_t)streamHandle);
120}
121
122aaudio_result_t AAudioService::getStreamDescription(
123 aaudio_handle_t streamHandle,
124 aaudio::AudioEndpointParcelable &parcelable) {
125 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800126 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800127 ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800128 return AAUDIO_ERROR_INVALID_HANDLE;
129 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800130 aaudio_result_t result = serviceStream->getDescription(parcelable);
Phil Burkc0c70e32017-02-09 13:18:38 -0800131 // parcelable.dump();
132 return result;
Phil Burk5ed503c2017-02-01 09:38:15 -0800133}
134
135aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
136 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800137 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800138 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800139 return AAUDIO_ERROR_INVALID_HANDLE;
140 }
141 aaudio_result_t result = serviceStream->start();
142 return result;
143}
144
145aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
146 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800147 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800148 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800149 return AAUDIO_ERROR_INVALID_HANDLE;
150 }
151 aaudio_result_t result = serviceStream->pause();
152 return result;
153}
154
Phil Burk71f35bb2017-04-13 16:05:07 -0700155aaudio_result_t AAudioService::stopStream(aaudio_handle_t streamHandle) {
156 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
157 if (serviceStream == nullptr) {
158 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
159 return AAUDIO_ERROR_INVALID_HANDLE;
160 }
161 aaudio_result_t result = serviceStream->stop();
162 return result;
163}
164
Phil Burk5ed503c2017-02-01 09:38:15 -0800165aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
166 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800167 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800168 ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800169 return AAUDIO_ERROR_INVALID_HANDLE;
170 }
171 return serviceStream->flush();
172}
173
174aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800175 pid_t clientProcessId,
Phil Burk5ed503c2017-02-01 09:38:15 -0800176 pid_t clientThreadId,
Phil Burk3316d5e2017-02-15 11:23:01 -0800177 int64_t periodNanoseconds) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800178 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800179 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800180 ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800181 return AAUDIO_ERROR_INVALID_HANDLE;
182 }
183 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
184 ALOGE("AAudioService::registerAudioThread(), thread already registered");
Phil Burkc0c70e32017-02-09 13:18:38 -0800185 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800186 }
187 serviceStream->setRegisteredThread(clientThreadId);
Phil Burkc0c70e32017-02-09 13:18:38 -0800188 int err = android::requestPriority(clientProcessId, clientThreadId,
189 DEFAULT_AUDIO_PRIORITY, true /* isForApp */);
Phil Burk5ed503c2017-02-01 09:38:15 -0800190 if (err != 0){
Phil Burkc0c70e32017-02-09 13:18:38 -0800191 ALOGE("AAudioService::registerAudioThread() failed, errno = %d, priority = %d",
192 errno, DEFAULT_AUDIO_PRIORITY);
Phil Burk5ed503c2017-02-01 09:38:15 -0800193 return AAUDIO_ERROR_INTERNAL;
194 } else {
195 return AAUDIO_OK;
196 }
197}
198
199aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800200 pid_t clientProcessId,
201 pid_t clientThreadId) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800202 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800203 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800204 ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x",
205 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800206 return AAUDIO_ERROR_INVALID_HANDLE;
207 }
208 if (serviceStream->getRegisteredThread() != clientThreadId) {
209 ALOGE("AAudioService::unregisterAudioThread(), wrong thread");
210 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
211 }
212 serviceStream->setRegisteredThread(0);
213 return AAUDIO_OK;
214}