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