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