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