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 "bta/le_audio/le_audio_types.h"
21 #include "client_interface_hidl.h"
22 
23 namespace bluetooth {
24 namespace audio {
25 namespace hidl {
26 namespace le_audio {
27 
28 using ::android::hardware::bluetooth::audio::V2_1::PcmParameters;
29 using ::bluetooth::audio::hidl::BluetoothAudioCtrlAck;
30 using ::bluetooth::le_audio::set_configurations::AudioSetConfiguration;
31 using ::bluetooth::le_audio::set_configurations::CodecConfigSetting;
32 
33 using ::bluetooth::audio::le_audio::StartRequestState;
34 
35 constexpr uint8_t kChannelNumberMono = 1;
36 constexpr uint8_t kChannelNumberStereo = 2;
37 
38 constexpr uint32_t kSampleRate48000 = 48000;
39 constexpr uint32_t kSampleRate44100 = 44100;
40 constexpr uint32_t kSampleRate32000 = 32000;
41 constexpr uint32_t kSampleRate24000 = 24000;
42 constexpr uint32_t kSampleRate16000 = 16000;
43 constexpr uint32_t kSampleRate8000 = 8000;
44 
45 constexpr uint8_t kBitsPerSample16 = 16;
46 constexpr uint8_t kBitsPerSample24 = 24;
47 constexpr uint8_t kBitsPerSample32 = 32;
48 
49 using ::bluetooth::audio::le_audio::StreamCallbacks;
50 
51 void flush_sink();
52 void flush_source();
53 
54 bool is_source_hal_enabled();
55 bool is_sink_hal_enabled();
56 
57 class LeAudioTransport {
58  public:
59   LeAudioTransport(void (*flush)(void), StreamCallbacks stream_cb,
60                    PcmParameters pcm_config);
61 
62   BluetoothAudioCtrlAck StartRequest();
63   BluetoothAudioCtrlAck StartRequestV2();
64 
65   BluetoothAudioCtrlAck SuspendRequest();
66 
67   void StopRequest();
68 
69   bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
70                                uint64_t* total_bytes_processed,
71                                timespec* data_position);
72 
73   void MetadataChanged(const source_metadata_t& source_metadata);
74 
75   void ResetPresentationPosition();
76 
77   void LogBytesProcessed(size_t bytes_processed);
78 
79   void SetRemoteDelay(uint16_t delay_report_ms);
80 
81   const PcmParameters& LeAudioGetSelectedHalPcmConfig();
82 
83   void LeAudioSetSelectedHalPcmConfig(uint32_t sample_rate_hz, uint8_t bit_rate,
84                                       uint8_t channels_count,
85                                       uint32_t data_interval);
86 
87   bool IsRequestCompletedAfterUpdate(
88       const std::function<
89           std::pair<StartRequestState, bool>(StartRequestState)>& lambda);
90 
91   StartRequestState GetStartRequestState(void);
92   void ClearStartRequestState(void);
93   void SetStartRequestState(StartRequestState state);
94 
95  private:
96   void (*flush_)(void);
97   StreamCallbacks stream_cb_;
98   uint16_t remote_delay_report_ms_;
99   uint64_t total_bytes_processed_;
100   timespec data_position_;
101   PcmParameters pcm_config_;
102   mutable std::mutex start_request_state_mutex_;
103   std::atomic<StartRequestState> start_request_state_;
104 };
105 
106 // Sink transport implementation for Le Audio
107 class LeAudioSinkTransport
108     : public ::bluetooth::audio::hidl::IBluetoothSinkTransportInstance {
109  public:
110   LeAudioSinkTransport(SessionType_2_1 session_type, StreamCallbacks stream_cb);
111 
112   ~LeAudioSinkTransport();
113 
114   BluetoothAudioCtrlAck StartRequest() override;
115   BluetoothAudioCtrlAck StartRequestV2();
116 
117   BluetoothAudioCtrlAck SuspendRequest() override;
118 
119   void StopRequest() override;
120 
121   bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
122                                uint64_t* total_bytes_read,
123                                timespec* data_position) override;
124 
125   void MetadataChanged(const source_metadata_t& source_metadata) override;
126 
127   void ResetPresentationPosition() override;
128 
129   void LogBytesRead(size_t bytes_read) override;
130 
131   void SetRemoteDelay(uint16_t delay_report_ms);
132 
133   const PcmParameters& LeAudioGetSelectedHalPcmConfig();
134 
135   void LeAudioSetSelectedHalPcmConfig(uint32_t sample_rate_hz, uint8_t bit_rate,
136                                       uint8_t channels_count,
137                                       uint32_t data_interval);
138 
139   bool IsRequestCompletedAfterUpdate(
140       const std::function<
141           std::pair<StartRequestState, bool>(StartRequestState)>& lambda);
142 
143   StartRequestState GetStartRequestState(void);
144   void ClearStartRequestState(void);
145   void SetStartRequestState(StartRequestState state);
146 
147   static inline LeAudioSinkTransport* instance = nullptr;
148   static inline BluetoothAudioSinkClientInterface* interface = nullptr;
149 
150  private:
151   LeAudioTransport* transport_;
152 };
153 
154 class LeAudioSourceTransport
155     : public ::bluetooth::audio::hidl::IBluetoothSourceTransportInstance {
156  public:
157   LeAudioSourceTransport(SessionType_2_1 session_type,
158                          StreamCallbacks stream_cb);
159 
160   ~LeAudioSourceTransport();
161 
162   BluetoothAudioCtrlAck StartRequest() override;
163 
164   BluetoothAudioCtrlAck SuspendRequest() override;
165 
166   void StopRequest() override;
167 
168   bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
169                                uint64_t* total_bytes_written,
170                                timespec* data_position) override;
171 
172   void MetadataChanged(const source_metadata_t& source_metadata) override;
173 
174   void ResetPresentationPosition() override;
175 
176   void LogBytesWritten(size_t bytes_written) override;
177 
178   void SetRemoteDelay(uint16_t delay_report_ms);
179 
180   const PcmParameters& LeAudioGetSelectedHalPcmConfig();
181 
182   void LeAudioSetSelectedHalPcmConfig(uint32_t sample_rate_hz, uint8_t bit_rate,
183                                       uint8_t channels_count,
184                                       uint32_t data_interval);
185 
186   bool IsRequestCompletedAfterUpdate(
187       const std::function<
188           std::pair<StartRequestState, bool>(StartRequestState)>& lambda);
189   StartRequestState GetStartRequestState(void);
190   void ClearStartRequestState(void);
191   void SetStartRequestState(StartRequestState state);
192 
193   static inline LeAudioSourceTransport* instance = nullptr;
194   static inline BluetoothAudioSourceClientInterface* interface = nullptr;
195 
196  private:
197   LeAudioTransport* transport_;
198 };
199 
200 }  // namespace le_audio
201 }  // namespace hidl
202 }  // namespace audio
203 }  // namespace bluetooth
204