blob: 3f1bba3225fa254831b5d22242af242b382582e2 [file] [log] [blame]
Phil Burkc0c70e32017-02-09 13:18:38 -08001/*
2 * Copyright (C) 2017 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
18#define LOG_TAG "AAudio"
19//#define LOG_NDEBUG 0
20#include <utils/Log.h>
21
22#include <binder/IServiceManager.h>
23#include <utils/Mutex.h>
24#include <utils/RefBase.h>
25
26#include <aaudio/AAudio.h>
27
28#include "AudioEndpointParcelable.h"
29#include "binding/AAudioStreamRequest.h"
30#include "binding/AAudioStreamConfiguration.h"
31#include "binding/IAAudioService.h"
32#include "binding/AAudioServiceMessage.h"
33
34#include "AAudioBinderClient.h"
35#include "AAudioServiceInterface.h"
36
37using android::String16;
38using android::IServiceManager;
39using android::defaultServiceManager;
40using android::interface_cast;
41using android::IAAudioService;
42using android::Mutex;
43using android::sp;
44
45using namespace aaudio;
46
47static android::Mutex gServiceLock;
48static sp<IAAudioService> gAAudioService;
49
50// TODO Share code with other service clients.
51// Helper function to get access to the "AAudioService" service.
52// This code was modeled after frameworks/av/media/libaudioclient/AudioSystem.cpp
53static const sp<IAAudioService> getAAudioService() {
54 sp<IBinder> binder;
55 Mutex::Autolock _l(gServiceLock);
56 if (gAAudioService == 0) {
57 sp<IServiceManager> sm = defaultServiceManager();
58 // Try several times to get the service.
59 int retries = 4;
60 do {
61 binder = sm->getService(String16(AAUDIO_SERVICE_NAME)); // This will wait a while.
62 if (binder != 0) {
63 break;
64 }
65 } while (retries-- > 0);
66
67 if (binder != 0) {
68 // TODO Add linkToDeath() like in frameworks/av/media/libaudioclient/AudioSystem.cpp
69 // TODO Create a DeathRecipient that disconnects all active streams.
70 gAAudioService = interface_cast<IAAudioService>(binder);
71 } else {
72 ALOGE("AudioStreamInternal could not get %s", AAUDIO_SERVICE_NAME);
73 }
74 }
75 return gAAudioService;
76}
77
Phil Burk71f35bb2017-04-13 16:05:07 -070078static void dropAAudioService() {
79 Mutex::Autolock _l(gServiceLock);
80 gAAudioService.clear(); // force a reconnect
81}
Phil Burkc0c70e32017-02-09 13:18:38 -080082
83AAudioBinderClient::AAudioBinderClient()
84 : AAudioServiceInterface() {}
85
86AAudioBinderClient::~AAudioBinderClient() {}
87
88/**
89* @param request info needed to create the stream
90* @param configuration contains information about the created stream
91* @return handle to the stream or a negative error
92*/
93aaudio_handle_t AAudioBinderClient::openStream(const AAudioStreamRequest &request,
94 AAudioStreamConfiguration &configurationOutput) {
Phil Burk71f35bb2017-04-13 16:05:07 -070095 aaudio_handle_t stream;
96 for (int i = 0; i < 2; i++) {
97 const sp<IAAudioService> &service = getAAudioService();
98 if (service == 0) {
99 return AAUDIO_ERROR_NO_SERVICE;
100 }
Phil Burkc0c70e32017-02-09 13:18:38 -0800101
Phil Burk71f35bb2017-04-13 16:05:07 -0700102 stream = service->openStream(request, configurationOutput);
103
104 if (stream == AAUDIO_ERROR_NO_SERVICE) {
105 ALOGE("AAudioBinderClient: lost connection to AAudioService.");
106 dropAAudioService(); // force a reconnect
107 } else {
108 break;
109 }
110 }
111 return stream;
Phil Burkc0c70e32017-02-09 13:18:38 -0800112}
113
114aaudio_result_t AAudioBinderClient::closeStream(aaudio_handle_t streamHandle) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800115 const sp<IAAudioService> &service = getAAudioService();
116 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
117 return service->closeStream(streamHandle);
118}
119
120/* Get an immutable description of the in-memory queues
121* used to communicate with the underlying HAL or Service.
122*/
123aaudio_result_t AAudioBinderClient::getStreamDescription(aaudio_handle_t streamHandle,
124 AudioEndpointParcelable &parcelable) {
Phil Burkc0c70e32017-02-09 13:18:38 -0800125 const sp<IAAudioService> &service = getAAudioService();
126 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
127 return service->getStreamDescription(streamHandle, parcelable);
128}
129
Phil Burkc0c70e32017-02-09 13:18:38 -0800130aaudio_result_t AAudioBinderClient::startStream(aaudio_handle_t streamHandle) {
131 const sp<IAAudioService> &service = getAAudioService();
132 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
133 return service->startStream(streamHandle);
134}
135
Phil Burkc0c70e32017-02-09 13:18:38 -0800136aaudio_result_t AAudioBinderClient::pauseStream(aaudio_handle_t streamHandle) {
137 const sp<IAAudioService> &service = getAAudioService();
138 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
Phil Burk71f35bb2017-04-13 16:05:07 -0700139 return service->pauseStream(streamHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800140}
141
Phil Burk71f35bb2017-04-13 16:05:07 -0700142aaudio_result_t AAudioBinderClient::stopStream(aaudio_handle_t streamHandle) {
143 const sp<IAAudioService> &service = getAAudioService();
144 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
145 return service->stopStream(streamHandle);
146}
147
Phil Burkc0c70e32017-02-09 13:18:38 -0800148aaudio_result_t AAudioBinderClient::flushStream(aaudio_handle_t streamHandle) {
149 const sp<IAAudioService> &service = getAAudioService();
150 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
Phil Burk71f35bb2017-04-13 16:05:07 -0700151 return service->flushStream(streamHandle);
Phil Burkc0c70e32017-02-09 13:18:38 -0800152}
153
154/**
155* Manage the specified thread as a low latency audio thread.
156*/
157aaudio_result_t AAudioBinderClient::registerAudioThread(aaudio_handle_t streamHandle,
158 pid_t clientProcessId,
159 pid_t clientThreadId,
160 int64_t periodNanoseconds) {
161 const sp<IAAudioService> &service = getAAudioService();
162 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
163 return service->registerAudioThread(streamHandle,
164 clientProcessId,
165 clientThreadId,
166 periodNanoseconds);
167}
168
169aaudio_result_t AAudioBinderClient::unregisterAudioThread(aaudio_handle_t streamHandle,
170 pid_t clientProcessId,
171 pid_t clientThreadId) {
172 const sp<IAAudioService> &service = getAAudioService();
173 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
174 return service->unregisterAudioThread(streamHandle,
175 clientProcessId,
176 clientThreadId);
177}