1 /*
2 * Copyright 2019 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 #define LOG_TAG "BTAudioHearingAidHIDL"
18
19 #include "hidl/hearing_aid_software_encoding_hidl.h"
20
21 #include <bluetooth/log.h>
22
23 #include "audio_hearing_aid_hw/include/audio_hearing_aid_hw.h"
24 #include "client_interface_hidl.h"
25 #include "os/log.h"
26 #include "osi/include/properties.h"
27
28 namespace fmt {
29 template <>
30 struct formatter<audio_usage_t> : enum_formatter<audio_usage_t> {};
31 template <>
32 struct formatter<audio_content_type_t> : enum_formatter<audio_content_type_t> {
33 };
34 } // namespace fmt
35
36 namespace {
37
38 using ::android::hardware::bluetooth::audio::V2_0::CodecType;
39 using ::bluetooth::audio::hidl::AudioConfiguration;
40 using ::bluetooth::audio::hidl::BitsPerSample;
41 using ::bluetooth::audio::hidl::BluetoothAudioCtrlAck;
42 using ::bluetooth::audio::hidl::ChannelMode;
43 using ::bluetooth::audio::hidl::PcmParameters;
44 using ::bluetooth::audio::hidl::SampleRate;
45 using ::bluetooth::audio::hidl::SessionType;
46 using ::bluetooth::audio::hidl::SessionType_2_1;
47 using ::bluetooth::audio::hidl::hearing_aid::StreamCallbacks;
48
49 using namespace bluetooth;
50
51 // Transport implementation for Hearing Aids
52 class HearingAidTransport
53 : public bluetooth::audio::hidl::IBluetoothSinkTransportInstance {
54 public:
HearingAidTransport(StreamCallbacks stream_cb)55 HearingAidTransport(StreamCallbacks stream_cb)
56 : IBluetoothSinkTransportInstance(
57 SessionType::HEARING_AID_SOFTWARE_ENCODING_DATAPATH,
58 (AudioConfiguration){}),
59 stream_cb_(std::move(stream_cb)),
60 remote_delay_report_ms_(0),
61 total_bytes_read_(0),
62 data_position_({}){};
63
StartRequest()64 BluetoothAudioCtrlAck StartRequest() override {
65 log::info("");
66 if (stream_cb_.on_resume_(true)) {
67 return BluetoothAudioCtrlAck::SUCCESS_FINISHED;
68 }
69 return BluetoothAudioCtrlAck::FAILURE;
70 }
71
SuspendRequest()72 BluetoothAudioCtrlAck SuspendRequest() override {
73 log::info("");
74 if (stream_cb_.on_suspend_()) {
75 uint8_t p_buf[AUDIO_STREAM_OUTPUT_BUFFER_SZ * 2];
76 ::bluetooth::audio::hidl::hearing_aid::read(p_buf, sizeof(p_buf));
77 return BluetoothAudioCtrlAck::SUCCESS_FINISHED;
78 } else {
79 return BluetoothAudioCtrlAck::FAILURE;
80 }
81 }
82
StopRequest()83 void StopRequest() override {
84 log::info("");
85 if (stream_cb_.on_suspend_()) {
86 // flush
87 uint8_t p_buf[AUDIO_STREAM_OUTPUT_BUFFER_SZ * 2];
88 ::bluetooth::audio::hidl::hearing_aid::read(p_buf, sizeof(p_buf));
89 }
90 }
91
GetPresentationPosition(uint64_t * remote_delay_report_ns,uint64_t * total_bytes_read,timespec * data_position)92 bool GetPresentationPosition(uint64_t* remote_delay_report_ns,
93 uint64_t* total_bytes_read,
94 timespec* data_position) override {
95 log::verbose("data={} byte(s), timestamp={}.{}s, delay report={} msec.",
96 total_bytes_read_, data_position_.tv_sec,
97 data_position_.tv_nsec, remote_delay_report_ms_);
98 if (remote_delay_report_ns != nullptr) {
99 *remote_delay_report_ns = remote_delay_report_ms_ * 1000000u;
100 }
101 if (total_bytes_read != nullptr) *total_bytes_read = total_bytes_read_;
102 if (data_position != nullptr) *data_position = data_position_;
103
104 return true;
105 }
106
MetadataChanged(const source_metadata_t & source_metadata)107 void MetadataChanged(const source_metadata_t& source_metadata) override {
108 auto track_count = source_metadata.track_count;
109 auto tracks = source_metadata.tracks;
110 log::info("{} track(s) received", track_count);
111 while (track_count) {
112 log::verbose("usage={}, content_type={}, gain={}", tracks->usage,
113 tracks->content_type, tracks->gain);
114 --track_count;
115 ++tracks;
116 }
117 }
118
ResetPresentationPosition()119 void ResetPresentationPosition() override {
120 log::verbose("called.");
121 remote_delay_report_ms_ = 0;
122 total_bytes_read_ = 0;
123 data_position_ = {};
124 }
125
LogBytesRead(size_t bytes_read)126 void LogBytesRead(size_t bytes_read) override {
127 if (bytes_read) {
128 total_bytes_read_ += bytes_read;
129 clock_gettime(CLOCK_MONOTONIC, &data_position_);
130 }
131 }
132
SetRemoteDelay(uint16_t delay_report_ms)133 void SetRemoteDelay(uint16_t delay_report_ms) {
134 log::info("delay_report={} msec", delay_report_ms);
135 remote_delay_report_ms_ = delay_report_ms;
136 }
137
138 private:
139 StreamCallbacks stream_cb_;
140 uint16_t remote_delay_report_ms_;
141 uint64_t total_bytes_read_;
142 timespec data_position_;
143 };
144
HearingAidGetSelectedHalPcmConfig(PcmParameters * hal_pcm_config)145 bool HearingAidGetSelectedHalPcmConfig(PcmParameters* hal_pcm_config) {
146 if (hal_pcm_config == nullptr) return false;
147 // TODO: we only support one config for now!
148 hal_pcm_config->sampleRate = SampleRate::RATE_16000;
149 hal_pcm_config->bitsPerSample = BitsPerSample::BITS_16;
150 hal_pcm_config->channelMode = ChannelMode::STEREO;
151 return true;
152 }
153
154 // Sink instance of Hearing Aids to provide call-in APIs for Bluetooth Audio Hal
155 HearingAidTransport* hearing_aid_sink = nullptr;
156 // Common interface to call-out into Bluetooth Audio Hal
157 bluetooth::audio::hidl::BluetoothAudioSinkClientInterface*
158 hearing_aid_hal_clientinterface = nullptr;
159 bool btaudio_hearing_aid_disabled = false;
160 bool is_configured = false;
161
162 // Save the value if the remote reports its delay before hearing_aid_sink is
163 // initialized
164 uint16_t remote_delay_ms = 0;
165
is_hal_2_0_force_disabled()166 bool is_hal_2_0_force_disabled() {
167 if (!is_configured) {
168 btaudio_hearing_aid_disabled =
169 osi_property_get_bool(BLUETOOTH_AUDIO_HAL_PROP_DISABLED, false);
170 is_configured = true;
171 }
172 return btaudio_hearing_aid_disabled;
173 }
174
175 } // namespace
176
177 namespace bluetooth {
178 namespace audio {
179 namespace hidl {
180 namespace hearing_aid {
181
is_hal_2_0_enabled()182 bool is_hal_2_0_enabled() { return hearing_aid_hal_clientinterface != nullptr; }
183
init(StreamCallbacks stream_cb,bluetooth::common::MessageLoopThread * message_loop)184 bool init(StreamCallbacks stream_cb,
185 bluetooth::common::MessageLoopThread* message_loop) {
186 log::info("");
187
188 if (is_hal_2_0_force_disabled()) {
189 log::error("BluetoothAudio HAL is disabled");
190 return false;
191 }
192
193 hearing_aid_sink = new HearingAidTransport(std::move(stream_cb));
194 hearing_aid_hal_clientinterface =
195 new bluetooth::audio::hidl::BluetoothAudioSinkClientInterface(
196 hearing_aid_sink, message_loop);
197 if (!hearing_aid_hal_clientinterface->IsValid()) {
198 log::warn("BluetoothAudio HAL for Hearing Aid is invalid?!");
199 delete hearing_aid_hal_clientinterface;
200 hearing_aid_hal_clientinterface = nullptr;
201 delete hearing_aid_sink;
202 hearing_aid_sink = nullptr;
203 return false;
204 }
205
206 if (remote_delay_ms != 0) {
207 log::info("restore DELAY {} ms", remote_delay_ms);
208 hearing_aid_sink->SetRemoteDelay(remote_delay_ms);
209 remote_delay_ms = 0;
210 }
211
212 return true;
213 }
214
cleanup()215 void cleanup() {
216 log::info("");
217 if (!is_hal_2_0_enabled()) return;
218 end_session();
219 delete hearing_aid_hal_clientinterface;
220 hearing_aid_hal_clientinterface = nullptr;
221 delete hearing_aid_sink;
222 hearing_aid_sink = nullptr;
223 remote_delay_ms = 0;
224 }
225
start_session()226 void start_session() {
227 log::info("");
228 if (!is_hal_2_0_enabled()) return;
229 AudioConfiguration audio_config;
230 PcmParameters pcm_config{};
231 if (!HearingAidGetSelectedHalPcmConfig(&pcm_config)) {
232 log::error("cannot get PCM config");
233 return;
234 }
235 audio_config.pcmConfig(pcm_config);
236 if (!hearing_aid_hal_clientinterface->UpdateAudioConfig(audio_config)) {
237 log::error("cannot update audio config to HAL");
238 return;
239 }
240 hearing_aid_hal_clientinterface->StartSession();
241 }
242
end_session()243 void end_session() {
244 log::info("");
245 if (!is_hal_2_0_enabled()) return;
246 hearing_aid_hal_clientinterface->EndSession();
247 }
248
read(uint8_t * p_buf,uint32_t len)249 size_t read(uint8_t* p_buf, uint32_t len) {
250 if (!is_hal_2_0_enabled()) return 0;
251 return hearing_aid_hal_clientinterface->ReadAudioData(p_buf, len);
252 }
253
254 // Update Hearing Aids delay report to BluetoothAudio HAL
set_remote_delay(uint16_t delay_report_ms)255 void set_remote_delay(uint16_t delay_report_ms) {
256 if (!is_hal_2_0_enabled()) {
257 log::info("not ready for DelayReport {} ms", delay_report_ms);
258 remote_delay_ms = delay_report_ms;
259 return;
260 }
261 log::info("delay_report_ms={} ms", delay_report_ms);
262 hearing_aid_sink->SetRemoteDelay(delay_report_ms);
263 }
264
265 } // namespace hearing_aid
266 } // namespace hidl
267 } // namespace audio
268 } // namespace bluetooth
269