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