1 /* 2 * Copyright 2020 HIMSA II K/S - www.himsa.com. 3 * Represented by EHIMA - www.ehima.com 4 * Copyright (c) 2022 The Android Open Source Project 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 #pragma once 19 20 #include <future> 21 #include <memory> 22 23 #include "audio_hal_interface/le_audio_software.h" 24 #include "common/repeating_timer.h" 25 26 namespace le_audio { 27 /* Represents configuration of audio codec, as exchanged between le audio and 28 * phone. 29 * It can also be passed to the audio source to configure its parameters. 30 */ 31 struct LeAudioCodecConfiguration { 32 static constexpr uint8_t kChannelNumberMono = 33 bluetooth::audio::le_audio::kChannelNumberMono; 34 static constexpr uint8_t kChannelNumberStereo = 35 bluetooth::audio::le_audio::kChannelNumberStereo; 36 37 static constexpr uint32_t kSampleRate48000 = 38 bluetooth::audio::le_audio::kSampleRate48000; 39 static constexpr uint32_t kSampleRate44100 = 40 bluetooth::audio::le_audio::kSampleRate44100; 41 static constexpr uint32_t kSampleRate32000 = 42 bluetooth::audio::le_audio::kSampleRate32000; 43 static constexpr uint32_t kSampleRate24000 = 44 bluetooth::audio::le_audio::kSampleRate24000; 45 static constexpr uint32_t kSampleRate16000 = 46 bluetooth::audio::le_audio::kSampleRate16000; 47 static constexpr uint32_t kSampleRate8000 = 48 bluetooth::audio::le_audio::kSampleRate8000; 49 50 static constexpr uint8_t kBitsPerSample16 = 51 bluetooth::audio::le_audio::kBitsPerSample16; 52 static constexpr uint8_t kBitsPerSample24 = 53 bluetooth::audio::le_audio::kBitsPerSample24; 54 static constexpr uint8_t kBitsPerSample32 = 55 bluetooth::audio::le_audio::kBitsPerSample32; 56 57 static constexpr uint32_t kInterval7500Us = 7500; 58 static constexpr uint32_t kInterval10000Us = 10000; 59 60 /** number of channels */ 61 uint8_t num_channels; 62 63 /** sampling rate that the codec expects to receive from audio framework */ 64 uint32_t sample_rate; 65 66 /** bits per sample that codec expects to receive from audio framework */ 67 uint8_t bits_per_sample; 68 69 /** Data interval determines how often we send samples to the remote. This 70 * should match how often we grab data from audio source, optionally we can 71 * grab data every 2 or 3 intervals, but this would increase latency. 72 * 73 * Value is provided in us. 74 */ 75 uint32_t data_interval_us; 76 77 bool operator!=(const LeAudioCodecConfiguration& other) { 78 return !((num_channels == other.num_channels) && 79 (sample_rate == other.sample_rate) && 80 (bits_per_sample == other.bits_per_sample) && 81 (data_interval_us == other.data_interval_us)); 82 } 83 84 bool operator==(const LeAudioCodecConfiguration& other) const { 85 return ((num_channels == other.num_channels) && 86 (sample_rate == other.sample_rate) && 87 (bits_per_sample == other.bits_per_sample) && 88 (data_interval_us == other.data_interval_us)); 89 } 90 IsInvalidLeAudioCodecConfiguration91 bool IsInvalid() { 92 return (num_channels == 0) || (sample_rate == 0) || 93 (bits_per_sample == 0) || (data_interval_us == 0); 94 } 95 }; 96 97 /* Used by the local BLE Audio Sink device to pass the audio data 98 * received from a remote BLE Audio Source to the Audio HAL. 99 */ 100 class LeAudioSinkAudioHalClient { 101 public: 102 class Callbacks { 103 public: 104 virtual ~Callbacks() = default; 105 virtual void OnAudioSuspend(std::promise<void> do_suspend_promise) = 0; 106 virtual void OnAudioResume(void) = 0; 107 virtual void OnAudioMetadataUpdate( 108 std::vector<struct record_track_metadata> sink_metadata) = 0; 109 }; 110 111 virtual ~LeAudioSinkAudioHalClient() = default; 112 virtual bool Start(const LeAudioCodecConfiguration& codecConfiguration, 113 Callbacks* audioReceiver) = 0; 114 virtual void Stop() = 0; 115 virtual size_t SendData(uint8_t* data, uint16_t size) = 0; 116 117 virtual void ConfirmStreamingRequest() = 0; 118 virtual void CancelStreamingRequest() = 0; 119 120 virtual void UpdateRemoteDelay(uint16_t remote_delay_ms) = 0; 121 virtual void UpdateAudioConfigToHal( 122 const ::le_audio::offload_config& config) = 0; 123 virtual void SuspendedForReconfiguration() = 0; 124 virtual void ReconfigurationComplete() = 0; 125 126 static std::unique_ptr<LeAudioSinkAudioHalClient> AcquireUnicast(); 127 static void DebugDump(int fd); 128 129 protected: 130 LeAudioSinkAudioHalClient() = default; 131 }; 132 133 /* Used by the local BLE Audio Source device to get data from the 134 * Audio HAL, so we could send it over to a remote BLE Audio Sink device. 135 */ 136 class LeAudioSourceAudioHalClient { 137 public: 138 class Callbacks { 139 public: 140 virtual ~Callbacks() = default; 141 virtual void OnAudioDataReady(const std::vector<uint8_t>& data) = 0; 142 virtual void OnAudioSuspend(std::promise<void> do_suspend_promise) = 0; 143 virtual void OnAudioResume(void) = 0; 144 virtual void OnAudioMetadataUpdate( 145 std::vector<struct playback_track_metadata> source_metadata) = 0; 146 }; 147 148 virtual ~LeAudioSourceAudioHalClient() = default; 149 virtual bool Start(const LeAudioCodecConfiguration& codecConfiguration, 150 Callbacks* audioReceiver) = 0; 151 virtual void Stop() = 0; SendData(uint8_t * data,uint16_t size)152 virtual size_t SendData(uint8_t* data, uint16_t size) { return 0; } 153 virtual void ConfirmStreamingRequest() = 0; 154 virtual void CancelStreamingRequest() = 0; 155 virtual void UpdateRemoteDelay(uint16_t remote_delay_ms) = 0; 156 virtual void UpdateAudioConfigToHal( 157 const ::le_audio::offload_config& config) = 0; 158 virtual void UpdateBroadcastAudioConfigToHal( 159 const ::le_audio::broadcast_offload_config& config) = 0; 160 virtual void SuspendedForReconfiguration() = 0; 161 virtual void ReconfigurationComplete() = 0; 162 163 static std::unique_ptr<LeAudioSourceAudioHalClient> AcquireUnicast(); 164 static std::unique_ptr<LeAudioSourceAudioHalClient> AcquireBroadcast(); 165 static void DebugDump(int fd); 166 167 protected: 168 LeAudioSourceAudioHalClient() = default; 169 }; 170 } // namespace le_audio 171