1 /*
2  * Copyright 2022 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 <bluetooth/log.h>
20 
21 #include <vector>
22 
23 #include "broadcaster/broadcaster_types.h"
24 #include "hardware/bt_le_audio.h"
25 #include "le_audio_types.h"
26 
27 namespace bluetooth::le_audio {
28 
29 class LeAudioSinkAudioHalClient;
30 class LeAudioSourceAudioHalClient;
31 
32 struct stream_map_info {
stream_map_infostream_map_info33   stream_map_info(uint16_t stream_handle, uint32_t audio_channel_allocation,
34                   bool is_stream_active)
35       : stream_handle(stream_handle),
36         audio_channel_allocation(audio_channel_allocation),
37         is_stream_active(is_stream_active) {}
38   uint16_t stream_handle;
39   uint32_t audio_channel_allocation;
40   bool is_stream_active;
41 };
42 
43 struct offload_config {
44   std::vector<stream_map_info> stream_map;
45   uint8_t bits_per_sample;
46   uint32_t sampling_rate;
47   uint32_t frame_duration;
48   uint16_t octets_per_frame;
49   uint8_t blocks_per_sdu;
50   uint16_t peer_delay_ms;
51 };
52 
53 struct broadcast_offload_config {
54   std::vector<std::pair<uint16_t, uint32_t>> stream_map;
55   uint8_t bits_per_sample;
56   uint32_t sampling_rate;
57   uint32_t frame_duration;
58   uint16_t octets_per_frame;
59   uint8_t blocks_per_sdu;
60   uint8_t retransmission_number;
61   uint16_t max_transport_latency;
62 };
63 
64 class CodecManager {
65  public:
66   struct UnicastConfigurationRequirements {
67     ::bluetooth::le_audio::types::LeAudioContextType audio_context_type;
68     std::optional<std::vector<types::acs_ac_record>> sink_pacs;
69     std::optional<std::vector<types::acs_ac_record>> source_pacs;
70 
71     struct DeviceDirectionRequirements {
72       uint8_t target_latency = types::kTargetLatencyUndefined;
73       uint8_t target_Phy = types::kTargetPhyUndefined;
74       types::LeAudioLtvMap params;
75     };
76 
77     std::optional<std::vector<DeviceDirectionRequirements>> sink_requirements;
78     std::optional<std::vector<DeviceDirectionRequirements>> source_requirements;
79   };
80 
81   /* The verifier function checks each possible configuration (from the set of
82    * all possible, supported configuration acquired from
83    * AudioSetConfigurationProvider for the given scenario), to select a single
84    * configuration, matching the current streaming audio group requirements.
85    * Note: Used only with the legacy AudioSetConfigurationProvider.
86    */
87   typedef std::function<const set_configurations::AudioSetConfiguration*(
88       const UnicastConfigurationRequirements& requirements,
89       const set_configurations::AudioSetConfigurations* confs)>
90       UnicastConfigurationVerifier;
91 
92   struct BroadcastConfigurationRequirements {
93     std::vector<
94         std::pair<bluetooth::le_audio::types::LeAudioContextType, uint8_t>>
95         subgroup_quality;
96     std::optional<std::vector<types::acs_ac_record>> sink_pacs;
97   };
98 
99   virtual ~CodecManager() = default;
GetInstance(void)100   static CodecManager* GetInstance(void) {
101     static CodecManager* instance = new CodecManager();
102     return instance;
103   }
104   void Start(const std::vector<bluetooth::le_audio::btle_audio_codec_config_t>&
105                  offloading_preference);
106   void Stop(void);
107   virtual types::CodecLocation GetCodecLocation(void) const;
108   virtual bool IsDualBiDirSwbSupported(void) const;
109   virtual void UpdateCisConfiguration(
110       const std::vector<struct types::cis>& cises,
111       const stream_parameters& stream_params, uint8_t direction);
112   virtual void ClearCisConfiguration(uint8_t direction);
113   virtual bool IsUsingCodecExtensibility() const;
114   virtual bool UpdateActiveUnicastAudioHalClient(
115       LeAudioSourceAudioHalClient* source_unicast_client,
116       LeAudioSinkAudioHalClient* sink_unicast_client, bool is_active);
117   virtual bool UpdateActiveBroadcastAudioHalClient(
118       LeAudioSourceAudioHalClient* source_broadcast_client, bool is_active);
119   virtual void UpdateActiveAudioConfig(
120       const types::BidirectionalPair<stream_parameters>& stream_params,
121       types::BidirectionalPair<uint16_t> delays_ms,
122       std::function<void(const offload_config& config, uint8_t direction)>
123           update_receiver);
124   virtual std::unique_ptr<
125       ::bluetooth::le_audio::set_configurations::AudioSetConfiguration>
126   GetCodecConfig(const UnicastConfigurationRequirements& requirements,
127                  UnicastConfigurationVerifier verifier);
128   virtual bool CheckCodecConfigIsBiDirSwb(
129       const ::bluetooth::le_audio::set_configurations::AudioSetConfiguration&
130           config) const;
131   virtual bool CheckCodecConfigIsDualBiDirSwb(
132       const ::bluetooth::le_audio::set_configurations::AudioSetConfiguration&
133           config) const;
134   virtual std::unique_ptr<broadcaster::BroadcastConfiguration>
135   GetBroadcastConfig(
136       const BroadcastConfigurationRequirements& requirements) const;
137 
138   virtual void UpdateBroadcastConnHandle(
139       const std::vector<uint16_t>& conn_handle,
140       std::function<
141           void(const ::bluetooth::le_audio::broadcast_offload_config& config)>
142           update_receiver);
143   virtual std::vector<bluetooth::le_audio::btle_audio_codec_config_t>
144   GetLocalAudioOutputCodecCapa();
145   virtual std::vector<bluetooth::le_audio::btle_audio_codec_config_t>
146   GetLocalAudioInputCodecCapa();
147 
148  private:
149   CodecManager();
150   struct impl;
151   std::unique_ptr<impl> pimpl_;
152 };
153 
154 std::ostream& operator<<(
155     std::ostream& os,
156     const CodecManager::UnicastConfigurationRequirements& req);
157 }  // namespace bluetooth::le_audio
158 
159 namespace fmt {
160 template <>
161 struct formatter<
162     bluetooth::le_audio::CodecManager::UnicastConfigurationRequirements>
163     : ostream_formatter {};
164 }  // namespace fmt
165