1 /*
2  * Copyright 2024 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 <functional>
20 
21 #include "audio_hal_interface/le_audio_software.h"
22 #include "audio_hal_interface/le_audio_software_host.h"
23 #include "bta/le_audio/le_audio_types.h"
24 
25 namespace bluetooth {
26 namespace audio {
27 namespace host {
28 namespace le_audio {
29 
30 using ::bluetooth::le_audio::set_configurations::AudioSetConfiguration;
31 using ::bluetooth::le_audio::set_configurations::CodecConfigSetting;
32 
33 using ::bluetooth::audio::le_audio::btle_stream_started_status;
34 using ::bluetooth::audio::le_audio::LeAudioClientInterface;
35 using ::bluetooth::audio::le_audio::StartRequestState;
36 using ::bluetooth::audio::le_audio::StreamCallbacks;
37 
38 typedef LeAudioClientInterface::PcmParameters PcmParameters;
39 
40 class LeAudioTransport {
41  public:
42   LeAudioTransport(std::function<void()> flush, StreamCallbacks stream_cb,
43                    PcmParameters pcm_config);
44 
45   bool StartRequest();
46 
47   bool SuspendRequest();
48 
49   void StopRequest();
50 
51   bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
52                                uint64_t* total_bytes_processed,
53                                timespec* data_position);
54 
55   void SourceMetadataChanged(const source_metadata_v7_t& source_metadata);
56 
57   void SinkMetadataChanged(const sink_metadata_v7_t& sink_metadata);
58 
59   void ResetPresentationPosition();
60 
61   void LogBytesProcessed(size_t bytes_processed);
62 
63   void SetRemoteDelay(uint16_t delay_report_ms);
64 
65   const PcmParameters& LeAudioGetSelectedHalPcmConfig();
66 
67   void LeAudioSetSelectedHalPcmConfig(uint32_t sample_rate_hz, uint8_t bit_rate,
68                                       uint8_t channels_count,
69                                       uint32_t data_interval);
70 
71   StartRequestState GetStartRequestState(void);
72   void ClearStartRequestState(void);
73   void SetStartRequestState(StartRequestState state);
74 
75  private:
76   std::function<void()> flush_;
77   StreamCallbacks stream_cb_;
78   uint16_t remote_delay_report_ms_;
79   uint64_t total_bytes_processed_;
80   timespec data_position_;
81   PcmParameters pcm_config_;
82   std::atomic<StartRequestState> start_request_state_;
83 };
84 
85 // Sink transport implementation for Le Audio
86 class LeAudioSinkTransport {
87  public:
88   LeAudioSinkTransport(StreamCallbacks stream_cb);
89 
90   ~LeAudioSinkTransport();
91 
92   bool StartRequest();
93 
94   bool SuspendRequest();
95 
96   void StopRequest();
97 
98   bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
99                                uint64_t* total_bytes_read,
100                                timespec* data_position);
101 
102   void SourceMetadataChanged(const source_metadata_v7_t& source_metadata);
103 
104   void SinkMetadataChanged(const sink_metadata_v7_t& sink_metadata);
105 
106   void ResetPresentationPosition();
107 
108   void LogBytesRead(size_t bytes_read);
109 
110   void SetRemoteDelay(uint16_t delay_report_ms);
111 
112   const PcmParameters& LeAudioGetSelectedHalPcmConfig();
113 
114   void LeAudioSetSelectedHalPcmConfig(uint32_t sample_rate_hz, uint8_t bit_rate,
115                                       uint8_t channels_count,
116                                       uint32_t data_interval);
117 
118   StartRequestState GetStartRequestState(void);
119   void ClearStartRequestState(void);
120   void SetStartRequestState(StartRequestState state);
121 
122   static inline LeAudioSinkTransport* instance = nullptr;
123   static inline btle_stream_started_status stream_started =
124       btle_stream_started_status::IDLE;
125 
126  private:
127   LeAudioTransport* transport_;
128 };
129 
130 class LeAudioSourceTransport {
131  public:
132   LeAudioSourceTransport(StreamCallbacks stream_cb);
133 
134   ~LeAudioSourceTransport();
135 
136   bool StartRequest();
137 
138   bool SuspendRequest();
139 
140   void StopRequest();
141 
142   bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
143                                uint64_t* total_bytes_written,
144                                timespec* data_position);
145 
146   void SourceMetadataChanged(const source_metadata_v7_t& source_metadata);
147 
148   void SinkMetadataChanged(const sink_metadata_v7_t& sink_metadata);
149 
150   void ResetPresentationPosition();
151 
152   void LogBytesWritten(size_t bytes_written);
153 
154   void SetRemoteDelay(uint16_t delay_report_ms);
155 
156   const PcmParameters& LeAudioGetSelectedHalPcmConfig();
157 
158   void LeAudioSetSelectedHalPcmConfig(uint32_t sample_rate_hz, uint8_t bit_rate,
159                                       uint8_t channels_count,
160                                       uint32_t data_interval);
161 
162   StartRequestState GetStartRequestState(void);
163   void ClearStartRequestState(void);
164   void SetStartRequestState(StartRequestState state);
165 
166   static inline LeAudioSourceTransport* instance = nullptr;
167   static inline btle_stream_started_status stream_started =
168       btle_stream_started_status::IDLE;
169 
170  private:
171   LeAudioTransport* transport_;
172 };
173 
174 }  // namespace le_audio
175 }  // namespace host
176 }  // namespace audio
177 }  // namespace bluetooth
178