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, uint64_t* total_bytes_processed,
52                                timespec* data_position);
53 
54   void SourceMetadataChanged(const source_metadata_v7_t& source_metadata);
55 
56   void SinkMetadataChanged(const sink_metadata_v7_t& sink_metadata);
57 
58   void ResetPresentationPosition();
59 
60   void LogBytesProcessed(size_t bytes_processed);
61 
62   void SetRemoteDelay(uint16_t delay_report_ms);
63 
64   const PcmParameters& LeAudioGetSelectedHalPcmConfig();
65 
66   void LeAudioSetSelectedHalPcmConfig(uint32_t sample_rate_hz, uint8_t bit_rate,
67                                       uint8_t channels_count, uint32_t data_interval);
68 
69   StartRequestState GetStartRequestState(void);
70   void ClearStartRequestState(void);
71   void SetStartRequestState(StartRequestState state);
72 
73 private:
74   std::function<void()> flush_;
75   StreamCallbacks stream_cb_;
76   uint16_t remote_delay_report_ms_;
77   uint64_t total_bytes_processed_;
78   timespec data_position_;
79   PcmParameters pcm_config_;
80   std::atomic<StartRequestState> start_request_state_;
81 };
82 
83 // Sink transport implementation for Le Audio
84 class LeAudioSinkTransport {
85 public:
86   LeAudioSinkTransport(StreamCallbacks stream_cb);
87 
88   ~LeAudioSinkTransport();
89 
90   bool StartRequest();
91 
92   bool SuspendRequest();
93 
94   void StopRequest();
95 
96   bool GetPresentationPosition(uint64_t* remote_delay_report_ns, uint64_t* total_bytes_read,
97                                timespec* data_position);
98 
99   void SourceMetadataChanged(const source_metadata_v7_t& source_metadata);
100 
101   void SinkMetadataChanged(const sink_metadata_v7_t& sink_metadata);
102 
103   void ResetPresentationPosition();
104 
105   void LogBytesRead(size_t bytes_read);
106 
107   void SetRemoteDelay(uint16_t delay_report_ms);
108 
109   const PcmParameters& LeAudioGetSelectedHalPcmConfig();
110 
111   void LeAudioSetSelectedHalPcmConfig(uint32_t sample_rate_hz, uint8_t bit_rate,
112                                       uint8_t channels_count, uint32_t data_interval);
113 
114   StartRequestState GetStartRequestState(void);
115   void ClearStartRequestState(void);
116   void SetStartRequestState(StartRequestState state);
117 
118   static inline LeAudioSinkTransport* instance = nullptr;
119   static inline btle_stream_started_status stream_started = btle_stream_started_status::IDLE;
120 
121 private:
122   LeAudioTransport* transport_;
123 };
124 
125 class LeAudioSourceTransport {
126 public:
127   LeAudioSourceTransport(StreamCallbacks stream_cb);
128 
129   ~LeAudioSourceTransport();
130 
131   bool StartRequest();
132 
133   bool SuspendRequest();
134 
135   void StopRequest();
136 
137   bool GetPresentationPosition(uint64_t* remote_delay_report_ns, uint64_t* total_bytes_written,
138                                timespec* data_position);
139 
140   void SourceMetadataChanged(const source_metadata_v7_t& source_metadata);
141 
142   void SinkMetadataChanged(const sink_metadata_v7_t& sink_metadata);
143 
144   void ResetPresentationPosition();
145 
146   void LogBytesWritten(size_t bytes_written);
147 
148   void SetRemoteDelay(uint16_t delay_report_ms);
149 
150   const PcmParameters& LeAudioGetSelectedHalPcmConfig();
151 
152   void LeAudioSetSelectedHalPcmConfig(uint32_t sample_rate_hz, uint8_t bit_rate,
153                                       uint8_t channels_count, uint32_t data_interval);
154 
155   StartRequestState GetStartRequestState(void);
156   void ClearStartRequestState(void);
157   void SetStartRequestState(StartRequestState state);
158 
159   static inline LeAudioSourceTransport* instance = nullptr;
160   static inline btle_stream_started_status stream_started = btle_stream_started_status::IDLE;
161 
162 private:
163   LeAudioTransport* transport_;
164 };
165 
166 }  // namespace le_audio
167 }  // namespace host
168 }  // namespace audio
169 }  // namespace bluetooth
170