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 broadcast_offload_config { 33 std::vector<std::pair<uint16_t, uint32_t>> stream_map; 34 uint8_t bits_per_sample; 35 uint32_t sampling_rate; 36 uint32_t frame_duration; 37 uint16_t octets_per_frame; 38 uint8_t blocks_per_sdu; 39 uint8_t retransmission_number; 40 uint16_t max_transport_latency; 41 }; 42 43 struct ProviderInfo { 44 bool allowAsymmetric = false; 45 bool lowLatency = false; 46 toStringProviderInfo47 inline std::string toString() const { 48 std::ostringstream _aidl_os; 49 _aidl_os << "ProviderInfo{"; 50 _aidl_os << "allowAsymmetric: " << allowAsymmetric; 51 _aidl_os << ", lowLatency: " << lowLatency; 52 _aidl_os << "}"; 53 return _aidl_os.str(); 54 } 55 }; 56 57 class CodecManager { 58 public: 59 enum Flags { 60 NONE = 0x00, 61 LOW_LATENCY, 62 ALLOW_ASYMMETRIC, 63 }; 64 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 Flags flags; 80 }; 81 82 /* The provider function checks each possible configuration (from the set of 83 * all possible, supported configuration acquired from 84 * AudioSetConfigurationProvider for the given scenario), to select a single 85 * configuration, matching the current streaming audio group requirements. 86 * Note: Used only with the legacy AudioSetConfigurationProvider. 87 */ 88 typedef std::function<std::unique_ptr<types::AudioSetConfiguration>( 89 const UnicastConfigurationRequirements& requirements, 90 const types::AudioSetConfigurations* confs)> 91 UnicastConfigurationProvider; 92 93 struct BroadcastConfigurationRequirements { 94 std::vector<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( 105 const std::vector<bluetooth::le_audio::btle_audio_codec_config_t>& offloading_preference); 106 void Stop(void); 107 virtual types::CodecLocation GetCodecLocation(void) const; 108 virtual std::optional<ProviderInfo> GetCodecConfigProviderInfo(void) const; 109 virtual bool IsDualBiDirSwbSupported(void) const; 110 virtual bool UpdateCisConfiguration(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(LeAudioSourceAudioHalClient* source_unicast_client, 115 LeAudioSinkAudioHalClient* sink_unicast_client, 116 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 std::function<void(const stream_config& config, uint8_t direction)> update_receiver, 122 uint8_t directions_to_update = (bluetooth::le_audio::types::kLeAudioDirectionSink | 123 bluetooth::le_audio::types::kLeAudioDirectionSource)); 124 virtual std::unique_ptr<::bluetooth::le_audio::types::AudioSetConfiguration> GetCodecConfig( 125 const UnicastConfigurationRequirements& requirements, 126 UnicastConfigurationProvider provider); 127 virtual bool CheckCodecConfigIsBiDirSwb( 128 const ::bluetooth::le_audio::types::AudioSetConfiguration& config) const; 129 virtual bool CheckCodecConfigIsDualBiDirSwb( 130 const ::bluetooth::le_audio::types::AudioSetConfiguration& config) const; 131 virtual std::unique_ptr<broadcaster::BroadcastConfiguration> GetBroadcastConfig( 132 const BroadcastConfigurationRequirements& requirements) const; 133 134 virtual void UpdateBroadcastConnHandle( 135 const std::vector<uint16_t>& conn_handle, 136 std::function<void(const ::bluetooth::le_audio::broadcast_offload_config& config)> 137 update_receiver); 138 virtual std::vector<bluetooth::le_audio::btle_audio_codec_config_t> 139 GetLocalAudioOutputCodecCapa(); 140 virtual std::vector<bluetooth::le_audio::btle_audio_codec_config_t> GetLocalAudioInputCodecCapa(); 141 142 private: 143 CodecManager(); 144 struct impl; 145 std::unique_ptr<impl> pimpl_; 146 }; 147 148 std::ostream& operator<<(std::ostream& os, 149 const CodecManager::UnicastConfigurationRequirements& req); 150 } // namespace bluetooth::le_audio 151 152 namespace std { 153 template <> 154 struct formatter<bluetooth::le_audio::CodecManager::UnicastConfigurationRequirements> 155 : ostream_formatter {}; 156 } // namespace std 157