blob: 723ef631aae5dd9e101dc6d7e81a93e976d12d11 [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();
57 aaudio_sharing_mode_t sharingMode = configurationInput.getSharingMode();
58 ALOGE("AAudioService::openStream(): sharingMode = %d", sharingMode);
59
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
80 if (serviceStream == nullptr) {
81 ALOGD("AAudioService::openStream(), sharingMode = AAUDIO_SHARING_MODE_SHARED");
82 serviceStream = new AAudioServiceStreamShared(*this);
83 result = serviceStream->open(request, configurationOutput);
84 configurationOutput.setSharingMode(AAUDIO_SHARING_MODE_SHARED);
85 }
86
87 if (result != AAUDIO_OK) {
88 delete serviceStream;
89 ALOGE("AAudioService::openStream(): failed, return %d", result);
Phil Burk5ed503c2017-02-01 09:38:15 -080090 return result;
91 } else {
Phil Burk3316d5e2017-02-15 11:23:01 -080092 aaudio_handle_t handle = mHandleTracker.put(AAUDIO_HANDLE_TYPE_STREAM, serviceStream);
Phil Burk5ed503c2017-02-01 09:38:15 -080093 ALOGD("AAudioService::openStream(): handle = 0x%08X", handle);
94 if (handle < 0) {
Phil Burkc0c70e32017-02-09 13:18:38 -080095 ALOGE("AAudioService::openStream(): handle table full");
Phil Burk5ed503c2017-02-01 09:38:15 -080096 delete serviceStream;
97 }
98 return handle;
99 }
100}
101
102aaudio_result_t AAudioService::closeStream(aaudio_handle_t streamHandle) {
103 AAudioServiceStreamBase *serviceStream = (AAudioServiceStreamBase *)
104 mHandleTracker.remove(AAUDIO_HANDLE_TYPE_STREAM,
105 streamHandle);
106 ALOGD("AAudioService.closeStream(0x%08X)", streamHandle);
107 if (serviceStream != nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800108 serviceStream->close();
Phil Burk5ed503c2017-02-01 09:38:15 -0800109 delete serviceStream;
110 return AAUDIO_OK;
111 }
112 return AAUDIO_ERROR_INVALID_HANDLE;
113}
114
115AAudioServiceStreamBase *AAudioService::convertHandleToServiceStream(
116 aaudio_handle_t streamHandle) const {
117 return (AAudioServiceStreamBase *) mHandleTracker.get(AAUDIO_HANDLE_TYPE_STREAM,
118 (aaudio_handle_t)streamHandle);
119}
120
121aaudio_result_t AAudioService::getStreamDescription(
122 aaudio_handle_t streamHandle,
123 aaudio::AudioEndpointParcelable &parcelable) {
124 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800125 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800126 ALOGE("AAudioService::getStreamDescription(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800127 return AAUDIO_ERROR_INVALID_HANDLE;
128 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800129 ALOGD("AAudioService::getStreamDescription(), handle = 0x%08x", streamHandle);
130 aaudio_result_t result = serviceStream->getDescription(parcelable);
131 ALOGD("AAudioService::getStreamDescription(), result = %d", result);
132 // parcelable.dump();
133 return result;
Phil Burk5ed503c2017-02-01 09:38:15 -0800134}
135
136aaudio_result_t AAudioService::startStream(aaudio_handle_t streamHandle) {
137 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800138 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800139 ALOGE("AAudioService::startStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800140 return AAUDIO_ERROR_INVALID_HANDLE;
141 }
142 aaudio_result_t result = serviceStream->start();
Phil Burkc0c70e32017-02-09 13:18:38 -0800143 ALOGD("AAudioService::startStream(), serviceStream->start() returned %d", result);
Phil Burk5ed503c2017-02-01 09:38:15 -0800144 return result;
145}
146
147aaudio_result_t AAudioService::pauseStream(aaudio_handle_t streamHandle) {
148 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800149 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800150 ALOGE("AAudioService::pauseStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800151 return AAUDIO_ERROR_INVALID_HANDLE;
152 }
153 aaudio_result_t result = serviceStream->pause();
154 return result;
155}
156
157aaudio_result_t AAudioService::flushStream(aaudio_handle_t streamHandle) {
158 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800159 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800160 ALOGE("AAudioService::flushStream(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800161 return AAUDIO_ERROR_INVALID_HANDLE;
162 }
163 return serviceStream->flush();
164}
165
166aaudio_result_t AAudioService::registerAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800167 pid_t clientProcessId,
Phil Burk5ed503c2017-02-01 09:38:15 -0800168 pid_t clientThreadId,
Phil Burk3316d5e2017-02-15 11:23:01 -0800169 int64_t periodNanoseconds) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800170 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
171 ALOGD("AAudioService::registerAudioThread(), serviceStream = %p", serviceStream);
172 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800173 ALOGE("AAudioService::registerAudioThread(), illegal stream handle = 0x%0x", streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800174 return AAUDIO_ERROR_INVALID_HANDLE;
175 }
176 if (serviceStream->getRegisteredThread() != AAudioServiceStreamBase::ILLEGAL_THREAD_ID) {
177 ALOGE("AAudioService::registerAudioThread(), thread already registered");
Phil Burkc0c70e32017-02-09 13:18:38 -0800178 return AAUDIO_ERROR_INVALID_STATE;
Phil Burk5ed503c2017-02-01 09:38:15 -0800179 }
180 serviceStream->setRegisteredThread(clientThreadId);
Phil Burkc0c70e32017-02-09 13:18:38 -0800181 int err = android::requestPriority(clientProcessId, clientThreadId,
182 DEFAULT_AUDIO_PRIORITY, true /* isForApp */);
Phil Burk5ed503c2017-02-01 09:38:15 -0800183 if (err != 0){
Phil Burkc0c70e32017-02-09 13:18:38 -0800184 ALOGE("AAudioService::registerAudioThread() failed, errno = %d, priority = %d",
185 errno, DEFAULT_AUDIO_PRIORITY);
Phil Burk5ed503c2017-02-01 09:38:15 -0800186 return AAUDIO_ERROR_INTERNAL;
187 } else {
188 return AAUDIO_OK;
189 }
190}
191
192aaudio_result_t AAudioService::unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -0800193 pid_t clientProcessId,
194 pid_t clientThreadId) {
Phil Burk5ed503c2017-02-01 09:38:15 -0800195 AAudioServiceStreamBase *serviceStream = convertHandleToServiceStream(streamHandle);
196 ALOGI("AAudioService::unregisterAudioThread(), serviceStream = %p", serviceStream);
197 if (serviceStream == nullptr) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800198 ALOGE("AAudioService::unregisterAudioThread(), illegal stream handle = 0x%0x",
199 streamHandle);
Phil Burk5ed503c2017-02-01 09:38:15 -0800200 return AAUDIO_ERROR_INVALID_HANDLE;
201 }
202 if (serviceStream->getRegisteredThread() != clientThreadId) {
203 ALOGE("AAudioService::unregisterAudioThread(), wrong thread");
204 return AAUDIO_ERROR_ILLEGAL_ARGUMENT;
205 }
206 serviceStream->setRegisteredThread(0);
207 return AAUDIO_OK;
208}