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 UpdateAudioConfigToHal(const ::bluetooth::le_audio::offload_config& config) = 0; 100 virtual void SuspendedForReconfiguration() = 0; 101 virtual void ReconfigurationComplete() = 0; 102 }; 103 104 public: 105 class Sink : public IClientInterfaceEndpoint { 106 public: is_broadcaster_(is_broadcaster)107 Sink(bool is_broadcaster = false) : is_broadcaster_(is_broadcaster) {} 108 virtual ~Sink() = default; 109 110 void Cleanup() override; 111 void SetPcmParameters(const PcmParameters& params) override; 112 void SetRemoteDelay(uint16_t delay_report_ms) override; 113 void StartSession() override; 114 void StopSession() override; 115 void ConfirmStreamingRequest() override; 116 void CancelStreamingRequest() override; 117 void UpdateAudioConfigToHal(const ::bluetooth::le_audio::offload_config& config) override; 118 void UpdateBroadcastAudioConfigToHal( 119 const ::bluetooth::le_audio::broadcast_offload_config& config); 120 void SuspendedForReconfiguration() override; 121 void ReconfigurationComplete() override; 122 // Read the stream of bytes sinked to us by the upper layers 123 size_t Read(uint8_t* p_buf, uint32_t len); IsBroadcaster()124 bool IsBroadcaster() { return is_broadcaster_; } 125 std::optional<::bluetooth::le_audio::broadcaster::BroadcastConfiguration> GetBroadcastConfig( 126 const std::vector<std::pair<::bluetooth::le_audio::types::LeAudioContextType, uint8_t>>& 127 subgroup_quality, 128 const std::optional<std::vector<::bluetooth::le_audio::types::acs_ac_record>>& pacs) 129 const; 130 std::optional<::bluetooth::le_audio::set_configurations::AudioSetConfiguration> 131 GetUnicastConfig(const ::bluetooth::le_audio::CodecManager::UnicastConfigurationRequirements& 132 requirements) const; 133 134 private: 135 bool is_broadcaster_ = false; 136 }; 137 class Source : public IClientInterfaceEndpoint { 138 public: 139 virtual ~Source() = default; 140 141 void Cleanup() override; 142 void SetPcmParameters(const PcmParameters& params) override; 143 void SetRemoteDelay(uint16_t delay_report_ms) override; 144 void StartSession() override; 145 void StopSession() override; 146 void ConfirmStreamingRequest() override; 147 void CancelStreamingRequest() override; 148 void UpdateAudioConfigToHal(const ::bluetooth::le_audio::offload_config& config) override; 149 void SuspendedForReconfiguration() override; 150 void ReconfigurationComplete() override; 151 // Source the given stream of bytes to be sinked into the upper layers 152 size_t Write(const uint8_t* p_buf, uint32_t len); 153 }; 154 155 // Get LE Audio sink client interface if it's not previously acquired and not 156 // yet released. 157 Sink* GetSink(StreamCallbacks stream_cb, bluetooth::common::MessageLoopThread* message_loop, 158 bool is_broadcasting_session_type); 159 // This should be called before trying to get unicast sink interface 160 bool IsUnicastSinkAcquired(); 161 // This should be called before trying to get broadcast sink interface 162 bool IsBroadcastSinkAcquired(); 163 // Release sink interface if belongs to LE audio client interface 164 bool ReleaseSink(Sink* sink); 165 166 // Get LE Audio source client interface if it's not previously acquired and 167 // not yet released. 168 Source* GetSource(StreamCallbacks stream_cb, bluetooth::common::MessageLoopThread* message_loop); 169 // This should be called before trying to get source interface 170 bool IsSourceAcquired(); 171 // Release source interface if belongs to LE audio client interface 172 bool ReleaseSource(Source* source); 173 174 // Sets Dynamic Spatial Audio modes supported by the remote device 175 void SetAllowedDsaModes(DsaModes dsa_modes); 176 177 // Get interface, if previously not initialized - it'll initialize 178 // singleton. 179 static LeAudioClientInterface* Get(); 180 181 private: 182 static LeAudioClientInterface* interface; 183 Sink* unicast_sink_ = nullptr; 184 Sink* broadcast_sink_ = nullptr; 185 Source* source_ = nullptr; 186 }; 187 188 } // namespace le_audio 189 } // namespace audio 190 } // namespace bluetooth 191