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 <memory>
21 #include <optional>
22 #include <vector>
23 
24 #include "audio_hal_interface/le_audio_software.h"
25 #include "le_audio/codec_manager.h"
26 #include "le_audio/le_audio_types.h"
27 
28 namespace bluetooth::le_audio {
29 /* Represents configuration used to configure the local audio sessions and
30  * the software codecs in case of a software coding sessions.
31  */
32 struct LeAudioCodecConfiguration {
33   static constexpr uint8_t kChannelNumberMono = bluetooth::audio::le_audio::kChannelNumberMono;
34   static constexpr uint8_t kChannelNumberStereo = bluetooth::audio::le_audio::kChannelNumberStereo;
35 
36   static constexpr uint32_t kSampleRate48000 = bluetooth::audio::le_audio::kSampleRate48000;
37   static constexpr uint32_t kSampleRate44100 = bluetooth::audio::le_audio::kSampleRate44100;
38   static constexpr uint32_t kSampleRate32000 = bluetooth::audio::le_audio::kSampleRate32000;
39   static constexpr uint32_t kSampleRate24000 = bluetooth::audio::le_audio::kSampleRate24000;
40   static constexpr uint32_t kSampleRate16000 = bluetooth::audio::le_audio::kSampleRate16000;
41   static constexpr uint32_t kSampleRate8000 = bluetooth::audio::le_audio::kSampleRate8000;
42 
43   static constexpr uint8_t kBitsPerSample16 = bluetooth::audio::le_audio::kBitsPerSample16;
44   static constexpr uint8_t kBitsPerSample24 = bluetooth::audio::le_audio::kBitsPerSample24;
45   static constexpr uint8_t kBitsPerSample32 = bluetooth::audio::le_audio::kBitsPerSample32;
46 
47   static constexpr uint32_t kInterval7500Us = 7500;
48   static constexpr uint32_t kInterval10000Us = 10000;
49 
50   /** number of channels */
51   uint8_t num_channels = 0;
52 
53   /** sampling rate that the codec expects to receive from audio framework */
54   uint32_t sample_rate = 0;
55 
56   /** bits per sample that codec expects to receive from audio framework */
57   uint8_t bits_per_sample = 0;
58 
59   /** Data interval determines how often we send samples to the remote. This
60    * should match how often we grab data from audio source, optionally we can
61    * grab data every 2 or 3 intervals, but this would increase latency.
62    *
63    * Value is provided in us.
64    */
65   uint32_t data_interval_us = 0;
66 
67   bool operator!=(const LeAudioCodecConfiguration& other) {
68     return !((num_channels == other.num_channels) && (sample_rate == other.sample_rate) &&
69              (bits_per_sample == other.bits_per_sample) &&
70              (data_interval_us == other.data_interval_us));
71   }
72 
73   bool operator==(const LeAudioCodecConfiguration& other) const {
74     return (num_channels == other.num_channels) && (sample_rate == other.sample_rate) &&
75            (bits_per_sample == other.bits_per_sample) &&
76            (data_interval_us == other.data_interval_us);
77   }
78 
IsInvalidLeAudioCodecConfiguration79   bool IsInvalid() const {
80     return (num_channels == 0) || (sample_rate == 0) || (bits_per_sample == 0) ||
81            (data_interval_us == 0);
82   }
83 };
84 
85 class LeAudioCommonAudioHalClient {
86 public:
87   virtual ~LeAudioCommonAudioHalClient() = default;
88   virtual std::optional<broadcaster::BroadcastConfiguration> GetBroadcastConfig(
89           const std::vector<std::pair<types::LeAudioContextType, uint8_t>>& subgroup_quality,
90           const std::optional<std::vector<::bluetooth::le_audio::types::acs_ac_record>>& pacs)
91           const = 0;
92   virtual std::optional<::bluetooth::le_audio::set_configurations::AudioSetConfiguration>
93   GetUnicastConfig(const CodecManager::UnicastConfigurationRequirements& requirements) const = 0;
94 };
95 
96 /* Used by the local BLE Audio Sink device to pass the audio data
97  * received from a remote BLE Audio Source to the Audio HAL.
98  */
99 class LeAudioSinkAudioHalClient {
100 public:
101   class Callbacks {
102   public:
103     Callbacks() = default;
104     virtual ~Callbacks() = default;
105     virtual void OnAudioSuspend(void) = 0;
106     virtual void OnAudioResume(void) = 0;
107     virtual void OnAudioMetadataUpdate(
108             const std::vector<struct record_track_metadata_v7> sink_metadata) = 0;
109 
110     base::WeakPtrFactory<Callbacks> weak_factory_{this};
111   };
112 
113   virtual ~LeAudioSinkAudioHalClient() = default;
114   virtual bool Start(const LeAudioCodecConfiguration& codecConfiguration, Callbacks* audioReceiver,
115                      DsaModes dsa_modes = {DsaMode::DISABLED}) = 0;
116   virtual void Stop() = 0;
117   virtual size_t SendData(uint8_t* data, uint16_t size) = 0;
118 
119   virtual void ConfirmStreamingRequest() = 0;
120   virtual void CancelStreamingRequest() = 0;
121 
122   virtual void UpdateRemoteDelay(uint16_t remote_delay_ms) = 0;
123   virtual void UpdateAudioConfigToHal(const ::bluetooth::le_audio::offload_config& config) = 0;
124   virtual void SuspendedForReconfiguration() = 0;
125   virtual void ReconfigurationComplete() = 0;
126 
127   static std::unique_ptr<LeAudioSinkAudioHalClient> AcquireUnicast();
128   static void DebugDump(int fd);
129 
130 protected:
131   LeAudioSinkAudioHalClient() = default;
132 };
133 
134 /* Used by the local BLE Audio Source device to get data from the
135  * Audio HAL, so we could send it over to a remote BLE Audio Sink device.
136  */
137 class LeAudioSourceAudioHalClient : public LeAudioCommonAudioHalClient {
138 public:
139   class Callbacks {
140   public:
141     Callbacks() = default;
142     virtual ~Callbacks() = default;
143     virtual void OnAudioDataReady(const std::vector<uint8_t>& data) = 0;
144     virtual void OnAudioSuspend(void) = 0;
145     virtual void OnAudioResume(void) = 0;
146     virtual void OnAudioMetadataUpdate(
147             const std::vector<struct playback_track_metadata_v7> source_metadata,
148             DsaMode dsa_mode) = 0;
149 
150     base::WeakPtrFactory<Callbacks> weak_factory_{this};
151   };
152 
153   virtual ~LeAudioSourceAudioHalClient() = default;
154   virtual bool Start(const LeAudioCodecConfiguration& codecConfiguration, Callbacks* audioReceiver,
155                      DsaModes dsa_modes = {DsaMode::DISABLED}) = 0;
156   virtual void Stop() = 0;
SendData(uint8_t *,uint16_t)157   virtual size_t SendData(uint8_t* /*data*/, uint16_t /*size*/) { return 0; }
158   virtual void ConfirmStreamingRequest() = 0;
159   virtual void CancelStreamingRequest() = 0;
160   virtual void UpdateRemoteDelay(uint16_t remote_delay_ms) = 0;
161   virtual void UpdateAudioConfigToHal(const ::bluetooth::le_audio::offload_config& config) = 0;
162   virtual void UpdateBroadcastAudioConfigToHal(
163           const ::bluetooth::le_audio::broadcast_offload_config& config) = 0;
164   virtual void SuspendedForReconfiguration() = 0;
165   virtual void ReconfigurationComplete() = 0;
166 
167   static std::unique_ptr<LeAudioSourceAudioHalClient> AcquireUnicast();
168   static std::unique_ptr<LeAudioSourceAudioHalClient> AcquireBroadcast();
169   static void DebugDump(int fd);
170 
171 protected:
172   LeAudioSourceAudioHalClient() = default;
173 };
174 }  // namespace bluetooth::le_audio
175