blob: 5adc0e2f89fc98a2bcf8f402aa4e900074cf11cb [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_;
95};
96
97class BluetoothAudioSession {
98 public:
99 BluetoothAudioSession(const SessionType& session_type);
100
101 /***
102 * The function helps to check if this session is ready or not
103 * @return: true if the Bluetooth stack has started the specified session
104 ***/
105 bool IsSessionReady();
106
107 /***
108 * The report function is used to report that the Bluetooth stack has started
109 * this session without any failure, and will invoke session_changed_cb_ to
110 * notify those registered bluetooth_audio outputs
111 ***/
112 void OnSessionStarted(const std::shared_ptr<IBluetoothAudioPort> stack_iface,
113 const DataMQDesc* mq_desc,
114 const AudioConfiguration& audio_config);
115
116 /***
117 * The report function is used to report that the Bluetooth stack has ended
118 * the session, and will invoke session_changed_cb_ to notify registered
119 * bluetooth_audio outputs
120 ***/
121 void OnSessionEnded();
122
123 /***
124 * The report function is used to report that the Bluetooth stack has notified
125 * the result of startStream or suspendStream, and will invoke
126 * control_result_cb_ to notify registered bluetooth_audio outputs
127 ***/
128 void ReportControlStatus(bool start_resp, BluetoothAudioStatus status);
129
130 /***
131 * The control function helps the bluetooth_audio module to register
132 * PortStatusCallbacks
133 * @return: cookie - the assigned number to this bluetooth_audio output
134 ***/
135 uint16_t RegisterStatusCback(const PortStatusCallbacks& cbacks);
136
137 /***
138 * The control function helps the bluetooth_audio module to unregister
139 * PortStatusCallbacks
140 * @param: cookie - indicates which bluetooth_audio output is
141 ***/
142 void UnregisterStatusCback(uint16_t cookie);
143
144 /***
145 * The control function is for the bluetooth_audio module to get the current
146 * AudioConfiguration
147 ***/
Josh Wu75462aa2022-01-21 21:51:21 -0800148 const AudioConfiguration GetAudioConfig();
Josh Wu20bac522021-12-29 23:52:39 -0800149
150 /***
151 * The report function is used to report that the Bluetooth stack has notified
152 * the audio configuration changed, and will invoke
153 * audio_configuration_changed_cb_ to notify registered bluetooth_audio
154 * outputs
155 ***/
156 void ReportAudioConfigChanged(const AudioConfiguration& audio_config);
157
158 /***
159 * Those control functions are for the bluetooth_audio module to start,
160 * suspend, stop stream, to check position, and to update metadata.
161 ***/
162 bool StartStream();
163 bool SuspendStream();
164 void StopStream();
165 bool GetPresentationPosition(PresentationPosition& presentation_position);
166 void UpdateSourceMetadata(const struct source_metadata& source_metadata);
167 void UpdateSinkMetadata(const struct sink_metadata& sink_metadata);
Chen Chena4c4c612022-02-07 18:01:05 -0800168 void SetLatencyMode(LatencyMode latency_mode);
169 void SetCodecType(CodecType codec_type);
Josh Wu20bac522021-12-29 23:52:39 -0800170
171 // The control function writes stream to FMQ
172 size_t OutWritePcmData(const void* buffer, size_t bytes);
173 // The control function read stream from FMQ
174 size_t InReadPcmData(void* buffer, size_t bytes);
175
176 // Return if IBluetoothAudioProviderFactory implementation existed
177 static bool IsAidlAvailable();
178
Josh Wu20bac522021-12-29 23:52:39 -0800179 private:
180 // using recursive_mutex to allow hwbinder to re-enter again.
181 std::recursive_mutex mutex_;
182 SessionType session_type_;
183
184 // audio control path to use for both software and offloading
185 std::shared_ptr<IBluetoothAudioPort> stack_iface_;
186 // audio data path (FMQ) for software encoding
187 std::unique_ptr<DataMQ> data_mq_;
188 // audio data configuration for both software and offloading
189 std::unique_ptr<AudioConfiguration> audio_config_;
190
191 // saving those registered bluetooth_audio's callbacks
192 std::unordered_map<uint16_t, std::shared_ptr<struct PortStatusCallbacks>>
193 observers_;
194
195 bool UpdateDataPath(const DataMQDesc* mq_desc);
196 bool UpdateAudioConfig(const AudioConfiguration& audio_config);
197 // invoking the registered session_changed_cb_
198 void ReportSessionStatus();
199
200 static inline std::atomic<bool> is_aidl_checked = false;
201 static inline std::atomic<bool> is_aidl_available = false;
202 static inline const std::string kDefaultAudioProviderFactoryInterface =
203 std::string() + IBluetoothAudioProviderFactory::descriptor + "/default";
204};
205
206class BluetoothAudioSessionInstance {
207 public:
208 // The API is to fetch the specified session of A2DP / Hearing Aid
209 static std::shared_ptr<BluetoothAudioSession> GetSessionInstance(
210 const SessionType& session_type);
211
212 private:
213 static std::mutex mutex_;
214 static std::unordered_map<SessionType, std::shared_ptr<BluetoothAudioSession>>
215 sessions_map_;
216};
217
218} // namespace audio
219} // namespace bluetooth
220} // namespace hardware
221} // namespace android
222} // namespace aidl