1 /*
2  * Copyright 2021 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 "../le_audio_software.h"
20 #include "audio_aidl_interfaces.h"
21 #include "bta/le_audio/le_audio_types.h"
22 #include "client_interface_aidl.h"
23 
24 namespace bluetooth {
25 namespace audio {
26 namespace aidl {
27 namespace le_audio {
28 
29 using ::aidl::android::hardware::bluetooth::audio::BroadcastCapability;
30 using ::aidl::android::hardware::bluetooth::audio::
31     LeAudioBroadcastConfiguration;
32 using ::aidl::android::hardware::bluetooth::audio::LeAudioConfiguration;
33 using ::aidl::android::hardware::bluetooth::audio::PcmConfiguration;
34 using ::aidl::android::hardware::bluetooth::audio::SessionType;
35 using ::aidl::android::hardware::bluetooth::audio::UnicastCapability;
36 using ::bluetooth::audio::aidl::BluetoothAudioCtrlAck;
37 using ::bluetooth::audio::aidl::LatencyMode;
38 using ::bluetooth::audio::le_audio::StartRequestState;
39 using ::bluetooth::le_audio::DsaMode;
40 using ::bluetooth::le_audio::set_configurations::AudioSetConfiguration;
41 using ::bluetooth::le_audio::set_configurations::CodecConfigSetting;
42 
43 constexpr uint8_t kChannelNumberMono = 1;
44 constexpr uint8_t kChannelNumberStereo = 2;
45 
46 constexpr uint32_t kSampleRate48000 = 48000;
47 constexpr uint32_t kSampleRate44100 = 44100;
48 constexpr uint32_t kSampleRate32000 = 32000;
49 constexpr uint32_t kSampleRate24000 = 24000;
50 constexpr uint32_t kSampleRate16000 = 16000;
51 constexpr uint32_t kSampleRate8000 = 8000;
52 
53 constexpr uint8_t kBitsPerSample16 = 16;
54 constexpr uint8_t kBitsPerSample24 = 24;
55 constexpr uint8_t kBitsPerSample32 = 32;
56 
57 using ::bluetooth::audio::le_audio::StreamCallbacks;
58 
59 void flush_source();
60 bool hal_ucast_capability_to_stack_format(
61     const UnicastCapability& ucast_capability,
62     CodecConfigSetting& stack_capability);
63 AudioConfiguration offload_config_to_hal_audio_config(
64     const ::bluetooth::le_audio::offload_config& offload_config);
65 
66 bluetooth::audio::le_audio::OffloadCapabilities get_offload_capabilities();
67 
68 class LeAudioTransport {
69  public:
70   LeAudioTransport(void (*flush)(void), StreamCallbacks stream_cb,
71                    PcmConfiguration pcm_config);
72   ~LeAudioTransport();
73 
74   BluetoothAudioCtrlAck StartRequest(bool is_low_latency);
75   BluetoothAudioCtrlAck StartRequestV2(bool is_low_latency);
76 
77   BluetoothAudioCtrlAck SuspendRequest();
78 
79   void StopRequest();
80 
81   void SetLatencyMode(LatencyMode latency_mode);
82 
83   bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
84                                uint64_t* total_bytes_processed,
85                                timespec* data_position);
86 
87   void SourceMetadataChanged(const source_metadata_v7_t& source_metadata);
88 
89   void SinkMetadataChanged(const sink_metadata_v7_t& sink_metadata);
90 
91   void ResetPresentationPosition();
92 
93   void LogBytesProcessed(size_t bytes_processed);
94 
95   void SetRemoteDelay(uint16_t delay_report_ms);
96 
97   const PcmConfiguration& LeAudioGetSelectedHalPcmConfig();
98 
99   void LeAudioSetSelectedHalPcmConfig(uint32_t sample_rate_hz, uint8_t bit_rate,
100                                       uint8_t channels_count,
101                                       uint32_t data_interval);
102 
103   void LeAudioSetBroadcastConfig(
104       const ::bluetooth::le_audio::broadcast_offload_config& offload_config);
105 
106   const LeAudioBroadcastConfiguration& LeAudioGetBroadcastConfig();
107 
108   bool IsRequestCompletedAfterUpdate(
109       const std::function<
110           std::pair<StartRequestState, bool>(StartRequestState)>& lambda);
111   StartRequestState GetStartRequestState(void);
112   void ClearStartRequestState(void);
113   void SetStartRequestState(StartRequestState state);
114 
115  private:
116   void (*flush_)(void);
117   StreamCallbacks stream_cb_;
118   uint16_t remote_delay_report_ms_;
119   uint64_t total_bytes_processed_;
120   timespec data_position_;
121   PcmConfiguration pcm_config_;
122   LeAudioBroadcastConfiguration broadcast_config_;
123   mutable std::mutex start_request_state_mutex_;
124   std::atomic<StartRequestState> start_request_state_;
125   DsaMode dsa_mode_;
126   source_metadata_v7_t cached_source_metadata_;
127 };
128 
129 // Sink transport implementation for Le Audio
130 class LeAudioSinkTransport
131     : public ::bluetooth::audio::aidl::IBluetoothSinkTransportInstance {
132  public:
133   LeAudioSinkTransport(SessionType session_type, StreamCallbacks stream_cb);
134 
135   ~LeAudioSinkTransport();
136 
137   BluetoothAudioCtrlAck StartRequest(bool is_low_latency) override;
138   BluetoothAudioCtrlAck StartRequestV2(bool is_low_latency);
139 
140   BluetoothAudioCtrlAck SuspendRequest() override;
141 
142   void StopRequest() override;
143 
144   void SetLatencyMode(LatencyMode latency_mode) override;
145 
146   bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
147                                uint64_t* total_bytes_read,
148                                timespec* data_position) override;
149 
150   void SourceMetadataChanged(
151       const source_metadata_v7_t& source_metadata) override;
152 
153   void SinkMetadataChanged(const sink_metadata_v7_t& sink_metadata) override;
154 
155   void ResetPresentationPosition() override;
156 
157   void LogBytesRead(size_t bytes_read) override;
158 
159   void SetRemoteDelay(uint16_t delay_report_ms);
160 
161   const PcmConfiguration& LeAudioGetSelectedHalPcmConfig();
162 
163   void LeAudioSetSelectedHalPcmConfig(uint32_t sample_rate_hz, uint8_t bit_rate,
164                                       uint8_t channels_count,
165                                       uint32_t data_interval);
166 
167   void LeAudioSetBroadcastConfig(
168       const ::bluetooth::le_audio::broadcast_offload_config& offload_config);
169 
170   const LeAudioBroadcastConfiguration& LeAudioGetBroadcastConfig();
171 
172   bool IsRequestCompletedAfterUpdate(
173       const std::function<
174           std::pair<StartRequestState, bool>(StartRequestState)>& lambda);
175   StartRequestState GetStartRequestState(void);
176   void ClearStartRequestState(void);
177   void SetStartRequestState(StartRequestState state);
178 
179   static inline LeAudioSinkTransport* instance_unicast_ = nullptr;
180   static inline LeAudioSinkTransport* instance_broadcast_ = nullptr;
181 
182   static inline BluetoothAudioSinkClientInterface* interface_unicast_ = nullptr;
183   static inline BluetoothAudioSinkClientInterface* interface_broadcast_ =
184       nullptr;
185 
186  private:
187   LeAudioTransport* transport_;
188 };
189 
190 class LeAudioSourceTransport
191     : public ::bluetooth::audio::aidl::IBluetoothSourceTransportInstance {
192  public:
193   LeAudioSourceTransport(SessionType session_type, StreamCallbacks stream_cb);
194 
195   ~LeAudioSourceTransport();
196 
197   BluetoothAudioCtrlAck StartRequest(bool is_low_latency) override;
198   BluetoothAudioCtrlAck StartRequestV2(bool is_low_latency);
199 
200   BluetoothAudioCtrlAck SuspendRequest() override;
201 
202   void StopRequest() override;
203 
204   void SetLatencyMode(LatencyMode latency_mode) override;
205 
206   bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
207                                uint64_t* total_bytes_written,
208                                timespec* data_position) override;
209 
210   void SourceMetadataChanged(
211       const source_metadata_v7_t& source_metadata) override;
212 
213   void SinkMetadataChanged(const sink_metadata_v7_t& sink_metadata) override;
214 
215   void ResetPresentationPosition() override;
216 
217   void LogBytesWritten(size_t bytes_written) override;
218 
219   void SetRemoteDelay(uint16_t delay_report_ms);
220 
221   const PcmConfiguration& LeAudioGetSelectedHalPcmConfig();
222 
223   void LeAudioSetSelectedHalPcmConfig(uint32_t sample_rate_hz, uint8_t bit_rate,
224                                       uint8_t channels_count,
225                                       uint32_t data_interval);
226 
227   bool IsRequestCompletedAfterUpdate(
228       const std::function<
229           std::pair<StartRequestState, bool>(StartRequestState)>& lambda);
230 
231   StartRequestState GetStartRequestState(void);
232   void ClearStartRequestState(void);
233   void SetStartRequestState(StartRequestState state);
234 
235   static inline LeAudioSourceTransport* instance = nullptr;
236   static inline BluetoothAudioSourceClientInterface* interface = nullptr;
237 
238  private:
239   LeAudioTransport* transport_;
240 };
241 
242 }  // namespace le_audio
243 }  // namespace aidl
244 }  // namespace audio
245 }  // namespace bluetooth
246