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