blob: a9a621e6d47ab7c9c2a23566e46c46d3c76c4c81 [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-Tsvi0412f732020-08-17 14:43:36 -070024#include "aaudio/BnAAudioClient.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080025#include "AAudioServiceDefinitions.h"
26#include "AAudioServiceInterface.h"
27#include "binding/AAudioStreamRequest.h"
28#include "binding/AAudioStreamConfiguration.h"
29#include "binding/AudioEndpointParcelable.h"
Phil Burk11e8d332017-05-24 09:59:02 -070030#include "binding/IAAudioService.h"
Phil Burkc0c70e32017-02-09 13:18:38 -080031
32/**
Phil Burk11e8d332017-05-24 09:59:02 -070033 * Implements the AAudioServiceInterface by talking to the service through Binder.
Phil Burkc0c70e32017-02-09 13:18:38 -080034 */
35
36namespace aaudio {
37
Phil Burk11e8d332017-05-24 09:59:02 -070038class AAudioBinderClient : public virtual android::RefBase
39 , public AAudioServiceInterface
Phil Burk9b3f8ef2017-05-16 11:37:43 -070040 , public android::Singleton<AAudioBinderClient> {
Phil Burkc0c70e32017-02-09 13:18:38 -080041
42public:
43
44 AAudioBinderClient();
45
46 virtual ~AAudioBinderClient();
47
Phil Burk11e8d332017-05-24 09:59:02 -070048 const android::sp<android::IAAudioService> getAAudioService();
49
50 void dropAAudioService();
51
Ytai Ben-Tsvi0412f732020-08-17 14:43:36 -070052 void registerClient(const android::sp<IAAudioClient>& client __unused) override {}
Phil Burk11e8d332017-05-24 09:59:02 -070053
Phil Burkc0c70e32017-02-09 13:18:38 -080054 /**
55 * @param request info needed to create the stream
56 * @param configuration contains resulting information about the created stream
57 * @return handle to the stream or a negative error
58 */
59 aaudio_handle_t openStream(const AAudioStreamRequest &request,
60 AAudioStreamConfiguration &configurationOutput) override;
61
62 aaudio_result_t closeStream(aaudio_handle_t streamHandle) override;
63
64 /* Get an immutable description of the in-memory queues
65 * used to communicate with the underlying HAL or Service.
66 */
67 aaudio_result_t getStreamDescription(aaudio_handle_t streamHandle,
68 AudioEndpointParcelable &parcelable) override;
69
70 /**
71 * Start the flow of data.
72 * This is asynchronous. When complete, the service will send a STARTED event.
73 */
74 aaudio_result_t startStream(aaudio_handle_t streamHandle) override;
75
76 /**
77 * Stop the flow of data such that start() can resume without loss of data.
78 * This is asynchronous. When complete, the service will send a PAUSED event.
79 */
80 aaudio_result_t pauseStream(aaudio_handle_t streamHandle) override;
81
Phil Burk71f35bb2017-04-13 16:05:07 -070082 aaudio_result_t stopStream(aaudio_handle_t streamHandle) override;
83
Phil Burkc0c70e32017-02-09 13:18:38 -080084 /**
85 * Discard any data held by the underlying HAL or Service.
86 * This is asynchronous. When complete, the service will send a FLUSHED event.
87 */
88 aaudio_result_t flushStream(aaudio_handle_t streamHandle) override;
89
90 /**
91 * Manage the specified thread as a low latency audio thread.
92 * TODO Consider passing this information as part of the startStream() call.
93 */
94 aaudio_result_t registerAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -080095 pid_t clientThreadId,
96 int64_t periodNanoseconds) override;
97
98 aaudio_result_t unregisterAudioThread(aaudio_handle_t streamHandle,
Phil Burkc0c70e32017-02-09 13:18:38 -080099 pid_t clientThreadId) override;
Phil Burk11e8d332017-05-24 09:59:02 -0700100
Eric Laurenta54f1282017-07-01 19:39:32 -0700101 aaudio_result_t startClient(aaudio_handle_t streamHandle __unused,
jiabind1f1cb62020-03-24 11:57:57 -0700102 const android::AudioClient& client __unused,
103 const audio_attributes_t *attr __unused,
104 audio_port_handle_t *clientHandle __unused) override {
Eric Laurenta54f1282017-07-01 19:39:32 -0700105 return AAUDIO_ERROR_UNAVAILABLE;
106 }
107
108 aaudio_result_t stopClient(aaudio_handle_t streamHandle __unused,
109 audio_port_handle_t clientHandle __unused) override {
110 return AAUDIO_ERROR_UNAVAILABLE;
111 }
112
Phil Burk11e8d332017-05-24 09:59:02 -0700113 void onStreamChange(aaudio_handle_t handle, int32_t opcode, int32_t value) {
114 // TODO This is just a stub so we can have a client Binder to pass to the service.
115 // TODO Implemented in a later CL.
116 ALOGW("onStreamChange called!");
117 }
118
Ytai Ben-Tsvi0412f732020-08-17 14:43:36 -0700119 class AAudioClient : public android::IBinder::DeathRecipient , public BnAAudioClient
Phil Burk11e8d332017-05-24 09:59:02 -0700120 {
121 public:
122 AAudioClient(android::wp<AAudioBinderClient> aaudioBinderClient)
Phil Burk2bc7c182017-08-28 11:45:01 -0700123 : mBinderClient(aaudioBinderClient) {
Phil Burk11e8d332017-05-24 09:59:02 -0700124 }
125
126 // implement DeathRecipient
127 virtual void binderDied(const android::wp<android::IBinder>& who __unused) {
128 android::sp<AAudioBinderClient> client = mBinderClient.promote();
Phil Burk2bc7c182017-08-28 11:45:01 -0700129 if (client.get() != nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -0700130 client->dropAAudioService();
131 }
132 ALOGW("AAudio service binderDied()!");
133 }
134
135 // implement BnAAudioClient
Ytai Ben-Tsvi0412f732020-08-17 14:43:36 -0700136 android::binder::Status onStreamChange(int32_t handle, int32_t opcode, int32_t value) {
137 static_assert(std::is_same_v<aaudio_handle_t, int32_t>);
Phil Burk11e8d332017-05-24 09:59:02 -0700138 android::sp<AAudioBinderClient> client = mBinderClient.promote();
Phil Burk2bc7c182017-08-28 11:45:01 -0700139 if (client.get() != nullptr) {
Phil Burk11e8d332017-05-24 09:59:02 -0700140 client->onStreamChange(handle, opcode, value);
141 }
Ytai Ben-Tsvi0412f732020-08-17 14:43:36 -0700142 return android::binder::Status::ok();
Phil Burk11e8d332017-05-24 09:59:02 -0700143 }
144 private:
145 android::wp<AAudioBinderClient> mBinderClient;
146 };
147
Phil Burk2bc7c182017-08-28 11:45:01 -0700148private:
Phil Burk11e8d332017-05-24 09:59:02 -0700149
Phil Burk2bc7c182017-08-28 11:45:01 -0700150 android::Mutex mServiceLock;
Phil Burk11e8d332017-05-24 09:59:02 -0700151 android::sp<android::IAAudioService> mAAudioService;
Phil Burk2bc7c182017-08-28 11:45:01 -0700152 android::sp<AAudioClient> mAAudioClient;
Phil Burk11e8d332017-05-24 09:59:02 -0700153
Phil Burkc0c70e32017-02-09 13:18:38 -0800154};
155
156
157} /* namespace aaudio */
158
Phil Burk5204d312017-05-04 17:16:13 -0700159#endif //ANDROID_AAUDIO_AAUDIO_BINDER_CLIENT_H