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