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 #ifdef TARGET_FLOSS
20 #include <audio_hal_interface/audio_linux.h>
21 #else
22 #include <hardware/audio.h>
23 #endif
24 
25 #include <com_android_bluetooth_flags.h>
26 
27 #include <functional>
28 #include <optional>
29 
30 #include "bta/le_audio/codec_manager.h"
31 #include "bta/le_audio/le_audio_types.h"
32 #include "common/message_loop_thread.h"
33 
34 namespace bluetooth {
35 namespace audio {
36 namespace le_audio {
37 
38 using ::bluetooth::le_audio::DsaMode;
39 using ::bluetooth::le_audio::DsaModes;
40 
41 enum class StartRequestState {
42   IDLE = 0x00,
43   PENDING_BEFORE_RESUME,
44   PENDING_AFTER_RESUME,
45   CONFIRMED,
46   CANCELED,
47 };
48 
49 constexpr uint8_t kChannelNumberMono = 1;
50 constexpr uint8_t kChannelNumberStereo = 2;
51 
52 constexpr uint32_t kSampleRate48000 = 48000;
53 constexpr uint32_t kSampleRate44100 = 44100;
54 constexpr uint32_t kSampleRate32000 = 32000;
55 constexpr uint32_t kSampleRate24000 = 24000;
56 constexpr uint32_t kSampleRate16000 = 16000;
57 constexpr uint32_t kSampleRate8000 = 8000;
58 
59 constexpr uint8_t kBitsPerSample16 = 16;
60 constexpr uint8_t kBitsPerSample24 = 24;
61 constexpr uint8_t kBitsPerSample32 = 32;
62 
63 struct StreamCallbacks {
64   std::function<bool(bool start_media_task)> on_resume_;
65   std::function<bool(void)> on_suspend_;
66   std::function<bool(const source_metadata_v7_t&, DsaMode)> on_metadata_update_;
67   std::function<bool(const sink_metadata_v7_t&)> on_sink_metadata_update_;
68 };
69 
70 struct OffloadCapabilities {
71   std::vector<bluetooth::le_audio::set_configurations::AudioSetConfiguration>
72       unicast_offload_capabilities;
73   std::vector<bluetooth::le_audio::set_configurations::AudioSetConfiguration>
74       broadcast_offload_capabilities;
75 };
76 
77 OffloadCapabilities get_offload_capabilities();
78 
79 class LeAudioClientInterface {
80  public:
81   struct PcmParameters {
82     uint32_t data_interval_us;
83     uint32_t sample_rate;
84     uint8_t bits_per_sample;
85     uint8_t channels_count;
86   };
87 
88  private:
89   class IClientInterfaceEndpoint {
90    public:
91     virtual ~IClientInterfaceEndpoint() = default;
92     virtual void Cleanup() = 0;
93     virtual void SetPcmParameters(const PcmParameters& params) = 0;
94     virtual void SetRemoteDelay(uint16_t delay_report_ms) = 0;
95     virtual void StartSession() = 0;
96     virtual void StopSession() = 0;
97     virtual void ConfirmStreamingRequest() = 0;
98     virtual void CancelStreamingRequest() = 0;
99     virtual void ConfirmStreamingRequestV2() = 0;
100     virtual void CancelStreamingRequestV2() = 0;
101     virtual void UpdateAudioConfigToHal(
102         const ::bluetooth::le_audio::offload_config& config) = 0;
103     virtual void SuspendedForReconfiguration() = 0;
104     virtual void ReconfigurationComplete() = 0;
105   };
106 
107  public:
108   class Sink : public IClientInterfaceEndpoint {
109    public:
is_broadcaster_(is_broadcaster)110     Sink(bool is_broadcaster = false) : is_broadcaster_(is_broadcaster){};
111     virtual ~Sink() = default;
112 
113     void Cleanup() override;
114     void SetPcmParameters(const PcmParameters& params) override;
115     void SetRemoteDelay(uint16_t delay_report_ms) override;
116     void StartSession() override;
117     void StopSession() override;
118     void ConfirmStreamingRequest() override;
119     void CancelStreamingRequest() override;
120     void ConfirmStreamingRequestV2() override;
121     void CancelStreamingRequestV2() override;
122     void UpdateAudioConfigToHal(
123         const ::bluetooth::le_audio::offload_config& config) override;
124     void UpdateBroadcastAudioConfigToHal(
125         const ::bluetooth::le_audio::broadcast_offload_config& config);
126     void SuspendedForReconfiguration() override;
127     void ReconfigurationComplete() override;
128     // Read the stream of bytes sinked to us by the upper layers
129     size_t Read(uint8_t* p_buf, uint32_t len);
IsBroadcaster()130     bool IsBroadcaster() { return is_broadcaster_; }
131     std::optional<::bluetooth::le_audio::broadcaster::BroadcastConfiguration>
132     GetBroadcastConfig(
133         const std::vector<std::pair<
134             ::bluetooth::le_audio::types::LeAudioContextType, uint8_t>>&
135             subgroup_quality,
136         const std::optional<
137             std::vector<::bluetooth::le_audio::types::acs_ac_record>>& pacs)
138         const;
139     std::optional<
140         ::bluetooth::le_audio::set_configurations::AudioSetConfiguration>
141     GetUnicastConfig(const ::bluetooth::le_audio::CodecManager::
142                          UnicastConfigurationRequirements& requirements) const;
143 
144    private:
145     bool is_broadcaster_ = false;
146   };
147   class Source : public IClientInterfaceEndpoint {
148    public:
149     virtual ~Source() = default;
150 
151     void Cleanup() override;
152     void SetPcmParameters(const PcmParameters& params) override;
153     void SetRemoteDelay(uint16_t delay_report_ms) override;
154     void StartSession() override;
155     void StopSession() override;
156     void ConfirmStreamingRequest() override;
157     void CancelStreamingRequest() override;
158     void ConfirmStreamingRequestV2() override;
159     void CancelStreamingRequestV2() override;
160     void UpdateAudioConfigToHal(
161         const ::bluetooth::le_audio::offload_config& config) override;
162     void SuspendedForReconfiguration() override;
163     void ReconfigurationComplete() override;
164     // Source the given stream of bytes to be sinked into the upper layers
165     size_t Write(const uint8_t* p_buf, uint32_t len);
166   };
167 
168   // Get LE Audio sink client interface if it's not previously acquired and not
169   // yet released.
170   Sink* GetSink(StreamCallbacks stream_cb,
171                 bluetooth::common::MessageLoopThread* message_loop,
172                 bool is_broadcasting_session_type);
173   // This should be called before trying to get unicast sink interface
174   bool IsUnicastSinkAcquired();
175   // This should be called before trying to get broadcast sink interface
176   bool IsBroadcastSinkAcquired();
177   // Release sink interface if belongs to LE audio client interface
178   bool ReleaseSink(Sink* sink);
179 
180   // Get LE Audio source client interface if it's not previously acquired and
181   // not yet released.
182   Source* GetSource(StreamCallbacks stream_cb,
183                     bluetooth::common::MessageLoopThread* message_loop);
184   // This should be called before trying to get source interface
185   bool IsSourceAcquired();
186   // Release source interface if belongs to LE audio client interface
187   bool ReleaseSource(Source* source);
188 
189   // Sets Dynamic Spatial Audio modes supported by the remote device
190   void SetAllowedDsaModes(DsaModes dsa_modes);
191 
192   // Get interface, if previously not initialized - it'll initialize
193   // singleton.
194   static LeAudioClientInterface* Get();
195 
196  private:
197   static LeAudioClientInterface* interface;
198   Sink* unicast_sink_ = nullptr;
199   Sink* broadcast_sink_ = nullptr;
200   Source* source_ = nullptr;
201 };
202 
203 }  // namespace le_audio
204 }  // namespace audio
205 }  // namespace bluetooth
206