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