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