blob: 6a7b6395682903fa4e40f95b6920274a58273c21 [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
Phil Burk5204d312017-05-04 17:16:13 -070017#ifndef ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H
18#define ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H
Phil Burkc0c70e32017-02-09 13:18:38 -080019
Phil Burk11e8d332017-05-24 09:59:02 -070020#include <utils/RefBase.h>
Phil Burk9b3f8ef2017-05-16 11:37:43 -070021#include <utils/Singleton.h>
22
Phil Burka4eb0d82017-04-12 15:44:06 -070023#include <aaudio/AAudio.h>
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070024#include <binder/IInterface.h>
25
Ytai Ben-Tsvi0412f732020-08-17 14:43:36 -070026#include "aaudio/BnAAudioClient.h"
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070027#include "aaudio/IAAudioService.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080028#include "AAudioServiceInterface.h"
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070029#include "binding/AAudioBinderAdapter.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080030#include "binding/AAudioStreamRequest.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080031#include "binding/AudioEndpointParcelable.h"
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070032#include "core/AAudioStreamParameters.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080033
34/**
Phil Burk11e8d332017-05-24 09:59:02 -070035 * Implements the AAudioServiceInterface by talking to the service through Binder.
Phil Burkc0c70e32017-02-09 13:18:38 -080036 */
37
38namespace aaudio {
39
Phil Burk11e8d332017-05-24 09:59:02 -070040class AAudioBinderClient : public virtual android::RefBase
41 , public AAudioServiceInterface
Phil Burk9b3f8ef2017-05-16 11:37:43 -070042 , public android::Singleton<AAudioBinderClient> {
Phil Burkc0c70e32017-02-09 13:18:38 -080043
44public:
45
46 AAudioBinderClient();
47
48 virtual ~AAudioBinderClient();
49
Ytai Ben-Tsvi0412f732020-08-17 14:43:36 -070050 void registerClient(const android::sp<IAAudioClient>& client __unused) override {}
Phil Burk11e8d332017-05-24 09:59:02 -070051
Phil Burkc0c70e32017-02-09 13:18:38 -080052 /**
53 * @param request info needed to create the stream
54 * @param configuration contains resulting information about the created stream
55 * @return handle to the stream or a negative error
56 */
57 aaudio_handle_t openStream(const AAudioStreamRequest &request,
58 AAudioStreamConfiguration &configurationOutput) override;
59
60 aaudio_result_t closeStream(aaudio_handle_t streamHandle) override;
61
62 /* Get an immutable description of the in-memory queues
63 * used to communicate with the underlying HAL or Service.
64 */
65 aaudio_result_t getStreamDescription(aaudio_handle_t streamHandle,
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -070066 AudioEndpointParcelable &endpointOut) override;
Phil Burkc0c70e32017-02-09 13:18:38 -080067
68 /**
69 * Start the flow of data.
70 * This is asynchronous. When complete, the service will send a STARTED event.
71 */
72 aaudio_result_t startStream(aaudio_handle_t streamHandle) override;
73
74 /**
75 * Stop the flow of data such that start() can resume without loss of data.
76 * This is asynchronous. When complete, the service will send a PAUSED event.
77 */
78 aaudio_result_t pauseStream(aaudio_handle_t streamHandle) override;
79
Phil Burk71f35bb2017-04-13 16:05:07 -070080 aaudio_result_t stopStream(aaudio_handle_t streamHandle) override;
81
Phil Burkc0c70e32017-02-09 13:18:38 -080082 /**
83 * Discard any data held by the underlying HAL or Service.
84 * This is asynchronous. When complete, the service will send a FLUSHED event.
85 */
86 aaudio_result_t flushStream(aaudio_handle_t streamHandle) override;
87
88 /**
89 * Manage the specified thread as a low latency audio thread.
90 * TODO Consider passing this information as part of the startStream() call.
91 */
92 aaudio_result_t registerAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -080093 pid_t clientThreadId,
94 int64_t periodNanoseconds) override;
95
96 aaudio_result_t unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -080097 pid_t clientThreadId) override;
Phil Burk11e8d332017-05-24 09:59:02 -070098
Eric Laurenta54f1282017-07-01 19:39:32 -070099 aaudio_result_t startClient(aaudio_handle_t streamHandle __unused,
jiabind1f1cb62020-03-24 11:57:57 -0700100 const android::AudioClient& client __unused,
101 const audio_attributes_t *attr __unused,
102 audio_port_handle_t *clientHandle __unused) override {
Eric Laurenta54f1282017-07-01 19:39:32 -0700103 return AAUDIO_ERROR_UNAVAILABLE;
104 }
105
106 aaudio_result_t stopClient(aaudio_handle_t streamHandle __unused,
107 audio_port_handle_t clientHandle __unused) override {
108 return AAUDIO_ERROR_UNAVAILABLE;
109 }
110
Phil Burk11e8d332017-05-24 09:59:02 -0700111 void onStreamChange(aaudio_handle_t handle, int32_t opcode, int32_t value) {
112 // TODO This is just a stub so we can have a client Binder to pass to the service.
113 // TODO Implemented in a later CL.
114 ALOGW("onStreamChange called!");
115 }
116
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700117 class AAudioClient : public android::IBinder::DeathRecipient, public BnAAudioClient {
Phil Burk11e8d332017-05-24 09:59:02 -0700118 public:
119 AAudioClient(android::wp<AAudioBinderClient> aaudioBinderClient)
Phil Burk2bc7c182017-08-28 11:45:01 -0700120 : mBinderClient(aaudioBinderClient) {
Phil Burk11e8d332017-05-24 09:59:02 -0700121 }
122
123 // implement DeathRecipient
124 virtual void binderDied(const android::wp<android::IBinder>& who __unused) {
125 android::sp<AAudioBinderClient> client = mBinderClient.promote();
Phil Burk2bc7c182017-08-28 11:45:01 -0700126 if (client.get() != nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -0700127 client->dropAAudioService();
128 }
129 ALOGW("AAudio service binderDied()!");
130 }
131
132 // implement BnAAudioClient
Ytai Ben-Tsvi0412f732020-08-17 14:43:36 -0700133 android::binder::Status onStreamChange(int32_t handle, int32_t opcode, int32_t value) {
134 static_assert(std::is_same_v<aaudio_handle_t, int32_t>);
Phil Burk11e8d332017-05-24 09:59:02 -0700135 android::sp<AAudioBinderClient> client = mBinderClient.promote();
Phil Burk2bc7c182017-08-28 11:45:01 -0700136 if (client.get() != nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -0700137 client->onStreamChange(handle, opcode, value);
138 }
Ytai Ben-Tsvi0412f732020-08-17 14:43:36 -0700139 return android::binder::Status::ok();
Phil Burk11e8d332017-05-24 09:59:02 -0700140 }
141 private:
142 android::wp<AAudioBinderClient> mBinderClient;
143 };
144
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700145 // This adapter is used to convert the binder interface (delegate) to the AudioServiceInterface
146 // conventions (translating between data types and respective parcelables, translating error
147 // codes and calling conventions).
148 // The adapter also owns the underlying service object and is responsible to unlink its death
149 // listener when destroyed.
150 class Adapter : public AAudioBinderAdapter {
151 public:
152 Adapter(const android::sp<IAAudioService>& delegate,
153 const android::sp<AAudioClient>& aaudioClient)
154 : AAudioBinderAdapter(delegate.get()),
155 mDelegate(delegate),
156 mAAudioClient(aaudioClient) {}
Phil Burk11e8d332017-05-24 09:59:02 -0700157
Ytai Ben-Tsvic5f45872020-08-18 10:39:44 -0700158 virtual ~Adapter() {
159 if (mDelegate != nullptr) {
160 android::IInterface::asBinder(mDelegate)->unlinkToDeath(mAAudioClient);
161 }
162 }
163
164 // This should never be called (call is rejected at the AudioBinderClient level).
165 aaudio_result_t startClient(aaudio_handle_t streamHandle __unused,
166 const android::AudioClient& client __unused,
167 const audio_attributes_t* attr __unused,
168 audio_port_handle_t* clientHandle __unused) override {
169 LOG_ALWAYS_FATAL("Shouldn't get here");
170 return AAUDIO_ERROR_UNAVAILABLE;
171 }
172
173 // This should never be called (call is rejected at the AudioBinderClient level).
174 aaudio_result_t stopClient(aaudio_handle_t streamHandle __unused,
175 audio_port_handle_t clientHandle __unused) override {
176 LOG_ALWAYS_FATAL("Shouldn't get here");
177 return AAUDIO_ERROR_UNAVAILABLE;
178 }
179
180 private:
181 android::sp<IAAudioService> mDelegate;
182 android::sp<AAudioClient> mAAudioClient;
183 };
184
185private:
186 android::Mutex mServiceLock;
187 std::shared_ptr<AAudioServiceInterface> mAdapter;
188 android::sp<AAudioClient> mAAudioClient;
189
190 std::shared_ptr<AAudioServiceInterface> getAAudioService();
191
192 void dropAAudioService();
Phil Burk11e8d332017-05-24 09:59:02 -0700193
Phil Burkc0c70e32017-02-09 13:18:38 -0800194};
195
196
197} /* namespace aaudio */
198
Phil Burk5204d312017-05-04 17:16:13 -0700199#endif //ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H