blob: 6e390cc16a345ff8ac889ff1e70f9460a51e0063 [file] [log] [blame]
Josh Wu20bac522021-12-29 23:52:39 -08001/*
2 * Copyright (C) 2022 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#pragma once
18
19#include <aidl/android/hardware/audio/common/SinkMetadata.h>
20#include <aidl/android/hardware/audio/common/SourceMetadata.h>
21#include <aidl/android/hardware/bluetooth/audio/IBluetoothAudioProvider.h>
22#include <aidl/android/hardware/bluetooth/audio/IBluetoothAudioProviderFactory.h>
Chen Chena4c4c612022-02-07 18:01:05 -080023#include <aidl/android/hardware/bluetooth/audio/LatencyMode.h>
Josh Wu20bac522021-12-29 23:52:39 -080024#include <aidl/android/hardware/bluetooth/audio/SessionType.h>
25#include <fmq/AidlMessageQueue.h>
26#include <hardware/audio.h>
27
28#include <mutex>
29#include <unordered_map>
30
31namespace aidl {
32namespace android {
33namespace hardware {
34namespace bluetooth {
35namespace audio {
36
37using ::aidl::android::hardware::common::fmq::MQDescriptor;
38using ::aidl::android::hardware::common::fmq::SynchronizedReadWrite;
39using ::android::AidlMessageQueue;
40
41using ::aidl::android::hardware::audio::common::SinkMetadata;
42using ::aidl::android::hardware::audio::common::SourceMetadata;
43
44using MQDataType = int8_t;
45using MQDataMode = SynchronizedReadWrite;
46using DataMQ = AidlMessageQueue<MQDataType, MQDataMode>;
47using DataMQDesc =
48 ::aidl::android::hardware::common::fmq::MQDescriptor<MQDataType,
49 MQDataMode>;
50
51static constexpr uint16_t kObserversCookieSize = 0x0010; // 0x0000 ~ 0x000f
52static constexpr uint16_t kObserversCookieUndefined =
53 (static_cast<uint16_t>(SessionType::UNKNOWN) << 8 & 0xff00);
54inline SessionType ObserversCookieGetSessionType(uint16_t cookie) {
55 return static_cast<SessionType>(cookie >> 8 & 0x00ff);
56}
57inline uint16_t ObserversCookieGetInitValue(SessionType session_type) {
58 return (static_cast<uint16_t>(session_type) << 8 & 0xff00);
59}
60inline uint16_t ObserversCookieGetUpperBound(SessionType session_type) {
61 return (static_cast<uint16_t>(session_type) << 8 & 0xff00) +
62 kObserversCookieSize;
63}
64
65/***
66 * This presents the callbacks of started / suspended and session changed,
67 * and the bluetooth_audio module uses to receive the status notification
68 ***/
69struct PortStatusCallbacks {
70 /***
71 * control_result_cb_ - when the Bluetooth stack reports results of
72 * streamStarted or streamSuspended, the BluetoothAudioProvider will invoke
73 * this callback to report to the bluetooth_audio module.
74 * @param: cookie - indicates which bluetooth_audio output should handle
75 * @param: start_resp - this report is for startStream or not
76 * @param: status - the result of startStream
77 ***/
78 std::function<void(uint16_t cookie, bool start_resp,
79 BluetoothAudioStatus status)>
80 control_result_cb_;
81 /***
82 * session_changed_cb_ - when the Bluetooth stack start / end session, the
83 * BluetoothAudioProvider will invoke this callback to notify to the
84 * bluetooth_audio module.
85 * @param: cookie - indicates which bluetooth_audio output should handle
86 ***/
87 std::function<void(uint16_t cookie)> session_changed_cb_;
88 /***
89 * audio_configuration_changed_cb_ - when the Bluetooth stack change the audio
90 * configuration, the BluetoothAudioProvider will invoke this callback to
91 * notify to the bluetooth_audio module.
92 * @param: cookie - indicates which bluetooth_audio output should handle
93 ***/
94 std::function<void(uint16_t cookie)> audio_configuration_changed_cb_;
Chen Chen81f38e52022-02-09 13:27:35 -080095 /***
96 * low_latency_mode_allowed_cb_ - when the Bluetooth stack low latency mode
97 * allowed or disallowed, the BluetoothAudioProvider will invoke
98 * this callback to report to the bluetooth_audio module.
99 * @param: cookie - indicates which bluetooth_audio output should handle
100 * @param: allowed - indicates if low latency mode is allowed
101 ***/
102 std::function<void(uint16_t cookie, bool allowed)>
103 low_latency_mode_allowed_cb_;
Josh Wu20bac522021-12-29 23:52:39 -0800104};
105
106class BluetoothAudioSession {
107 public:
108 BluetoothAudioSession(const SessionType& session_type);
109
110 /***
111 * The function helps to check if this session is ready or not
112 * @return: true if the Bluetooth stack has started the specified session
113 ***/
114 bool IsSessionReady();
115
116 /***
117 * The report function is used to report that the Bluetooth stack has started
118 * this session without any failure, and will invoke session_changed_cb_ to
119 * notify those registered bluetooth_audio outputs
120 ***/
121 void OnSessionStarted(const std::shared_ptr<IBluetoothAudioPort> stack_iface,
122 const DataMQDesc* mq_desc,
123 const AudioConfiguration& audio_config);
124
125 /***
126 * The report function is used to report that the Bluetooth stack has ended
127 * the session, and will invoke session_changed_cb_ to notify registered
128 * bluetooth_audio outputs
129 ***/
130 void OnSessionEnded();
131
132 /***
133 * The report function is used to report that the Bluetooth stack has notified
134 * the result of startStream or suspendStream, and will invoke
135 * control_result_cb_ to notify registered bluetooth_audio outputs
136 ***/
137 void ReportControlStatus(bool start_resp, BluetoothAudioStatus status);
138
139 /***
140 * The control function helps the bluetooth_audio module to register
141 * PortStatusCallbacks
142 * @return: cookie - the assigned number to this bluetooth_audio output
143 ***/
144 uint16_t RegisterStatusCback(const PortStatusCallbacks& cbacks);
145
146 /***
147 * The control function helps the bluetooth_audio module to unregister
148 * PortStatusCallbacks
149 * @param: cookie - indicates which bluetooth_audio output is
150 ***/
151 void UnregisterStatusCback(uint16_t cookie);
152
153 /***
154 * The control function is for the bluetooth_audio module to get the current
155 * AudioConfiguration
156 ***/
Josh Wu75462aa2022-01-21 21:51:21 -0800157 const AudioConfiguration GetAudioConfig();
Josh Wu20bac522021-12-29 23:52:39 -0800158
159 /***
160 * The report function is used to report that the Bluetooth stack has notified
161 * the audio configuration changed, and will invoke
162 * audio_configuration_changed_cb_ to notify registered bluetooth_audio
163 * outputs
164 ***/
165 void ReportAudioConfigChanged(const AudioConfiguration& audio_config);
166
167 /***
Chen Chen81f38e52022-02-09 13:27:35 -0800168 * The report function is used to report that the Bluetooth stack has notified
169 * the low latency mode allowed changed, and will invoke
170 * low_latency_mode_allowed_changed_cb to notify registered bluetooth_audio
171 * outputs
172 ***/
173 void ReportLowLatencyModeAllowedChanged(bool allowed);
174 /***
Josh Wu20bac522021-12-29 23:52:39 -0800175 * Those control functions are for the bluetooth_audio module to start,
176 * suspend, stop stream, to check position, and to update metadata.
177 ***/
178 bool StartStream();
179 bool SuspendStream();
180 void StopStream();
181 bool GetPresentationPosition(PresentationPosition& presentation_position);
182 void UpdateSourceMetadata(const struct source_metadata& source_metadata);
183 void UpdateSinkMetadata(const struct sink_metadata& sink_metadata);
Chen Chena4c4c612022-02-07 18:01:05 -0800184 void SetLatencyMode(LatencyMode latency_mode);
Josh Wu20bac522021-12-29 23:52:39 -0800185
186 // The control function writes stream to FMQ
187 size_t OutWritePcmData(const void* buffer, size_t bytes);
188 // The control function read stream from FMQ
189 size_t InReadPcmData(void* buffer, size_t bytes);
190
191 // Return if IBluetoothAudioProviderFactory implementation existed
192 static bool IsAidlAvailable();
193
Josh Wu20bac522021-12-29 23:52:39 -0800194 private:
195 // using recursive_mutex to allow hwbinder to re-enter again.
196 std::recursive_mutex mutex_;
197 SessionType session_type_;
198
199 // audio control path to use for both software and offloading
200 std::shared_ptr<IBluetoothAudioPort> stack_iface_;
201 // audio data path (FMQ) for software encoding
202 std::unique_ptr<DataMQ> data_mq_;
203 // audio data configuration for both software and offloading
204 std::unique_ptr<AudioConfiguration> audio_config_;
205
206 // saving those registered bluetooth_audio's callbacks
207 std::unordered_map<uint16_t, std::shared_ptr<struct PortStatusCallbacks>>
208 observers_;
209
210 bool UpdateDataPath(const DataMQDesc* mq_desc);
211 bool UpdateAudioConfig(const AudioConfiguration& audio_config);
212 // invoking the registered session_changed_cb_
213 void ReportSessionStatus();
214
215 static inline std::atomic<bool> is_aidl_checked = false;
216 static inline std::atomic<bool> is_aidl_available = false;
217 static inline const std::string kDefaultAudioProviderFactoryInterface =
218 std::string() + IBluetoothAudioProviderFactory::descriptor + "/default";
219};
220
221class BluetoothAudioSessionInstance {
222 public:
223 // The API is to fetch the specified session of A2DP / Hearing Aid
224 static std::shared_ptr<BluetoothAudioSession> GetSessionInstance(
225 const SessionType& session_type);
226
227 private:
228 static std::mutex mutex_;
229 static std::unordered_map<SessionType, std::shared_ptr<BluetoothAudioSession>>
230 sessions_map_;
231};
232
233} // namespace audio
234} // namespace bluetooth
235} // namespace hardware
236} // namespace android
237} // namespace aidl