blob: 8315c409ebeeca449122c9097dc4b2bc9ad191c9 [file] [log] [blame]
Phil Burk7f6b40d2017-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
78
79AAudioBinderClient::AAudioBinderClient()
80 : AAudioServiceInterface() {}
81
82AAudioBinderClient::~AAudioBinderClient() {}
83
84/**
85* @param request info needed to create the stream
86* @param configuration contains information about the created stream
87* @return handle to the stream or a negative error
88*/
89aaudio_handle_t AAudioBinderClient::openStream(const AAudioStreamRequest &request,
90 AAudioStreamConfiguration &configurationOutput) {
91
92 const sp<IAAudioService> &service = getAAudioService();
93 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
94 return service->openStream(request, configurationOutput);
95}
96
97aaudio_result_t AAudioBinderClient::closeStream(aaudio_handle_t streamHandle) {
98
99 const sp<IAAudioService> &service = getAAudioService();
100 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
101 return service->closeStream(streamHandle);
102}
103
104/* Get an immutable description of the in-memory queues
105* used to communicate with the underlying HAL or Service.
106*/
107aaudio_result_t AAudioBinderClient::getStreamDescription(aaudio_handle_t streamHandle,
108 AudioEndpointParcelable &parcelable) {
109
110 const sp<IAAudioService> &service = getAAudioService();
111 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
112 return service->getStreamDescription(streamHandle, parcelable);
113}
114
115/**
116* Start the flow of data.
117*/
118aaudio_result_t AAudioBinderClient::startStream(aaudio_handle_t streamHandle) {
119 const sp<IAAudioService> &service = getAAudioService();
120 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
121 return service->startStream(streamHandle);
122}
123
124/**
125* Stop the flow of data such that start() can resume without loss of data.
126*/
127aaudio_result_t AAudioBinderClient::pauseStream(aaudio_handle_t streamHandle) {
128 const sp<IAAudioService> &service = getAAudioService();
129 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
130 return service->startStream(streamHandle);
131}
132
133/**
134* Discard any data held by the underlying HAL or Service.
135*/
136aaudio_result_t AAudioBinderClient::flushStream(aaudio_handle_t streamHandle) {
137 const sp<IAAudioService> &service = getAAudioService();
138 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
139 return service->startStream(streamHandle);
140}
141
142/**
143* Manage the specified thread as a low latency audio thread.
144*/
145aaudio_result_t AAudioBinderClient::registerAudioThread(aaudio_handle_t streamHandle,
146 pid_t clientProcessId,
147 pid_t clientThreadId,
148 int64_t periodNanoseconds) {
149 const sp<IAAudioService> &service = getAAudioService();
150 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
151 return service->registerAudioThread(streamHandle,
152 clientProcessId,
153 clientThreadId,
154 periodNanoseconds);
155}
156
157aaudio_result_t AAudioBinderClient::unregisterAudioThread(aaudio_handle_t streamHandle,
158 pid_t clientProcessId,
159 pid_t clientThreadId) {
160 const sp<IAAudioService> &service = getAAudioService();
161 if (service == 0) return AAUDIO_ERROR_NO_SERVICE;
162 return service->unregisterAudioThread(streamHandle,
163 clientProcessId,
164 clientThreadId);
165}
166
167