1 /******************************************************************************
2 *
3 * Copyright 2018 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 /* Implements hal for bluedroid ha audio device */
20
21 #define LOG_TAG "bt_hearing_aid_hw"
22
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <inttypes.h>
26 #include <stdint.h>
27 #include <sys/errno.h>
28 #include <sys/socket.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <sys/un.h>
32 #include <unistd.h>
33
34 #include <mutex>
35
36 #include <hardware/audio.h>
37 #include <hardware/hardware.h>
38 #include <system/audio.h>
39
40 #include "osi/include/hash_map_utils.h"
41 #include "osi/include/log.h"
42 #include "osi/include/osi.h"
43 #include "osi/include/socket_utils/sockets.h"
44
45 #include "audio_hearing_aid_hw/include/audio_hearing_aid_hw.h"
46
47 /*****************************************************************************
48 * Constants & Macros
49 *****************************************************************************/
50
51 #define CTRL_CHAN_RETRY_COUNT 3
52 #define USEC_PER_SEC 1000000L
53 #define SOCK_SEND_TIMEOUT_MS 2000 /* Timeout for sending */
54 #define SOCK_RECV_TIMEOUT_MS 5000 /* Timeout for receiving */
55
56 // set WRITE_POLL_MS to 0 for blocking sockets, nonzero for polled non-blocking
57 // sockets
58 #define WRITE_POLL_MS 20
59
60 #define FNLOG() LOG_VERBOSE("%s", __func__);
61 #define DEBUG(fmt, ...) LOG_VERBOSE("%s: " fmt, __func__, ##__VA_ARGS__)
62 #define INFO(fmt, ...) LOG_INFO("%s: " fmt, __func__, ##__VA_ARGS__)
63 #define WARN(fmt, ...) LOG_WARN("%s: " fmt, __func__, ##__VA_ARGS__)
64 #define ERROR(fmt, ...) LOG_ERROR("%s: " fmt, __func__, ##__VA_ARGS__)
65
66 #define ASSERTC(cond, msg, val) \
67 if (!(cond)) { \
68 ERROR("### ASSERT : %s line %d %s (%d) ###", __FILE__, __LINE__, msg, \
69 val); \
70 }
71
72 #define CASE_RETURN_STR(const) \
73 case const: \
74 return #const;
75
audio_ha_hw_dump_ctrl_event(tHEARING_AID_CTRL_CMD event)76 static const char* audio_ha_hw_dump_ctrl_event(tHEARING_AID_CTRL_CMD event) {
77 switch (event) {
78 CASE_RETURN_STR(HEARING_AID_CTRL_CMD_NONE)
79 CASE_RETURN_STR(HEARING_AID_CTRL_CMD_CHECK_READY)
80 CASE_RETURN_STR(HEARING_AID_CTRL_CMD_START)
81 CASE_RETURN_STR(HEARING_AID_CTRL_CMD_STOP)
82 CASE_RETURN_STR(HEARING_AID_CTRL_CMD_SUSPEND)
83 CASE_RETURN_STR(HEARING_AID_CTRL_GET_INPUT_AUDIO_CONFIG)
84 CASE_RETURN_STR(HEARING_AID_CTRL_GET_OUTPUT_AUDIO_CONFIG)
85 CASE_RETURN_STR(HEARING_AID_CTRL_SET_OUTPUT_AUDIO_CONFIG)
86 CASE_RETURN_STR(HEARING_AID_CTRL_CMD_OFFLOAD_START)
87 default:
88 break;
89 }
90
91 return "UNKNOWN HEARING_AID_CTRL_CMD";
92 }
93
94 /*****************************************************************************
95 * Local type definitions
96 *****************************************************************************/
97
98 typedef enum {
99 AUDIO_HA_STATE_STARTING,
100 AUDIO_HA_STATE_STARTED,
101 AUDIO_HA_STATE_STOPPING,
102 AUDIO_HA_STATE_STOPPED,
103 /* need explicit set param call to resume (suspend=false) */
104 AUDIO_HA_STATE_SUSPENDED,
105 AUDIO_HA_STATE_STANDBY /* allows write to autoresume */
106 } ha_state_t;
107
108 struct ha_stream_in;
109 struct ha_stream_out;
110
111 struct ha_audio_device {
112 // Important: device must be first as an audio_hw_device* may be cast to
113 // ha_audio_device* when the type is implicitly known.
114 struct audio_hw_device device;
115 std::recursive_mutex* mutex; // See note below on mutex acquisition order.
116 struct ha_stream_in* input;
117 struct ha_stream_out* output;
118 };
119
120 struct ha_config {
121 uint32_t rate;
122 uint32_t channel_mask;
123 bool is_stereo_to_mono; // True if fetching Stereo and mixing into Mono
124 int format;
125 };
126
127 /* move ctrl_fd outside output stream and keep open until HAL unloaded ? */
128
129 struct ha_stream_common {
130 std::recursive_mutex* mutex; // See note below on mutex acquisition order.
131 int ctrl_fd;
132 int audio_fd;
133 size_t buffer_sz;
134 struct ha_config cfg;
135 ha_state_t state;
136 };
137
138 struct ha_stream_out {
139 struct audio_stream_out stream;
140 struct ha_stream_common common;
141 uint64_t frames_presented; // frames written, never reset
142 uint64_t frames_rendered; // frames written, reset on standby
143 };
144
145 struct ha_stream_in {
146 struct audio_stream_in stream;
147 struct ha_stream_common common;
148 };
149
150 /*
151 * Mutex acquisition order:
152 *
153 * The ha_audio_device (adev) mutex must be acquired before
154 * the ha_stream_common (out or in) mutex.
155 *
156 * This may differ from other audio HALs.
157 */
158
159 /*****************************************************************************
160 * Static variables
161 *****************************************************************************/
162
163 /*****************************************************************************
164 * Static functions
165 *****************************************************************************/
166
167 static size_t out_get_buffer_size(const struct audio_stream* stream);
168
169 /*****************************************************************************
170 * Externs
171 *****************************************************************************/
172
173 /*****************************************************************************
174 * Functions
175 *****************************************************************************/
176 static void ha_open_ctrl_path(struct ha_stream_common* common);
177
178 /*****************************************************************************
179 * Miscellaneous helper functions
180 *****************************************************************************/
181
182 /* logs timestamp with microsec precision
183 pprev is optional in case a dedicated diff is required */
ts_log(UNUSED_ATTR const char * tag,UNUSED_ATTR int val,struct timespec * pprev_opt)184 static void ts_log(UNUSED_ATTR const char* tag, UNUSED_ATTR int val,
185 struct timespec* pprev_opt) {
186 struct timespec now;
187 static struct timespec prev = {0, 0};
188 unsigned long long now_us;
189 unsigned long long diff_us;
190
191 clock_gettime(CLOCK_MONOTONIC, &now);
192
193 now_us = now.tv_sec * USEC_PER_SEC + now.tv_nsec / 1000;
194
195 if (pprev_opt) {
196 diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC +
197 (now.tv_nsec - prev.tv_nsec) / 1000;
198 *pprev_opt = now;
199 DEBUG("[%s] ts %08lld, *diff %08lld, val %d", tag, now_us, diff_us, val);
200 } else {
201 diff_us = (now.tv_sec - prev.tv_sec) * USEC_PER_SEC +
202 (now.tv_nsec - prev.tv_nsec) / 1000;
203 prev = now;
204 DEBUG("[%s] ts %08lld, diff %08lld, val %d", tag, now_us, diff_us, val);
205 }
206 }
207
calc_audiotime_usec(struct ha_config cfg,int bytes)208 static int calc_audiotime_usec(struct ha_config cfg, int bytes) {
209 int chan_count = audio_channel_count_from_out_mask(cfg.channel_mask);
210 int bytes_per_sample;
211
212 switch (cfg.format) {
213 case AUDIO_FORMAT_PCM_8_BIT:
214 bytes_per_sample = 1;
215 break;
216 case AUDIO_FORMAT_PCM_16_BIT:
217 bytes_per_sample = 2;
218 break;
219 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
220 bytes_per_sample = 3;
221 break;
222 case AUDIO_FORMAT_PCM_8_24_BIT:
223 bytes_per_sample = 4;
224 break;
225 case AUDIO_FORMAT_PCM_32_BIT:
226 bytes_per_sample = 4;
227 break;
228 default:
229 ASSERTC(false, "unsupported sample format", cfg.format);
230 bytes_per_sample = 2;
231 break;
232 }
233
234 return (
235 int)(((int64_t)bytes * (USEC_PER_SEC / (chan_count * bytes_per_sample))) /
236 cfg.rate);
237 }
238
239 /*****************************************************************************
240 *
241 * bluedroid stack adaptation
242 *
243 ****************************************************************************/
244
skt_connect(const char * path,size_t buffer_sz)245 static int skt_connect(const char* path, size_t buffer_sz) {
246 int ret;
247 int skt_fd;
248 int len;
249
250 INFO("connect to %s (sz %zu)", path, buffer_sz);
251
252 skt_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
253
254 if (osi_socket_local_client_connect(
255 skt_fd, path, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM) < 0) {
256 ERROR("failed to connect (%s)", strerror(errno));
257 close(skt_fd);
258 return -1;
259 }
260
261 len = buffer_sz;
262 ret =
263 setsockopt(skt_fd, SOL_SOCKET, SO_SNDBUF, (char*)&len, (int)sizeof(len));
264 if (ret < 0) ERROR("setsockopt failed (%s)", strerror(errno));
265
266 ret =
267 setsockopt(skt_fd, SOL_SOCKET, SO_RCVBUF, (char*)&len, (int)sizeof(len));
268 if (ret < 0) ERROR("setsockopt failed (%s)", strerror(errno));
269
270 /* Socket send/receive timeout value */
271 struct timeval tv;
272 tv.tv_sec = SOCK_SEND_TIMEOUT_MS / 1000;
273 tv.tv_usec = (SOCK_SEND_TIMEOUT_MS % 1000) * 1000;
274
275 ret = setsockopt(skt_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
276 if (ret < 0) ERROR("setsockopt failed (%s)", strerror(errno));
277
278 tv.tv_sec = SOCK_RECV_TIMEOUT_MS / 1000;
279 tv.tv_usec = (SOCK_RECV_TIMEOUT_MS % 1000) * 1000;
280
281 ret = setsockopt(skt_fd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
282 if (ret < 0) ERROR("setsockopt failed (%s)", strerror(errno));
283
284 INFO("connected to stack fd = %d", skt_fd);
285
286 return skt_fd;
287 }
288
skt_read(int fd,void * p,size_t len)289 static int skt_read(int fd, void* p, size_t len) {
290 ssize_t read;
291
292 FNLOG();
293
294 ts_log("skt_read recv", len, NULL);
295
296 OSI_NO_INTR(read = recv(fd, p, len, MSG_NOSIGNAL));
297 if (read == -1) ERROR("read failed with errno=%d\n", errno);
298
299 return (int)read;
300 }
301
skt_write(int fd,const void * p,size_t len)302 static int skt_write(int fd, const void* p, size_t len) {
303 ssize_t sent;
304 FNLOG();
305
306 ts_log("skt_write", len, NULL);
307
308 if (WRITE_POLL_MS == 0) {
309 // do not poll, use blocking send
310 OSI_NO_INTR(sent = send(fd, p, len, MSG_NOSIGNAL));
311 if (sent == -1) ERROR("write failed with error(%s)", strerror(errno));
312
313 return (int)sent;
314 }
315
316 // use non-blocking send, poll
317 int ms_timeout = SOCK_SEND_TIMEOUT_MS;
318 size_t count = 0;
319 while (count < len) {
320 OSI_NO_INTR(sent = send(fd, p, len - count, MSG_NOSIGNAL | MSG_DONTWAIT));
321 if (sent == -1) {
322 if (errno != EAGAIN && errno != EWOULDBLOCK) {
323 ERROR("write failed with error(%s)", strerror(errno));
324 return -1;
325 }
326 if (ms_timeout >= WRITE_POLL_MS) {
327 usleep(WRITE_POLL_MS * 1000);
328 ms_timeout -= WRITE_POLL_MS;
329 continue;
330 }
331 WARN("write timeout exceeded, sent %zu bytes", count);
332 return -1;
333 }
334 count += sent;
335 p = (const uint8_t*)p + sent;
336 }
337 return (int)count;
338 }
339
skt_disconnect(int fd)340 static int skt_disconnect(int fd) {
341 INFO("fd %d", fd);
342
343 if (fd != AUDIO_SKT_DISCONNECTED) {
344 shutdown(fd, SHUT_RDWR);
345 close(fd);
346 }
347 return 0;
348 }
349
350 /*****************************************************************************
351 *
352 * AUDIO CONTROL PATH
353 *
354 ****************************************************************************/
355
ha_ctrl_receive(struct ha_stream_common * common,void * buffer,size_t length)356 static int ha_ctrl_receive(struct ha_stream_common* common, void* buffer,
357 size_t length) {
358 ssize_t ret;
359 int i;
360
361 for (i = 0;; i++) {
362 OSI_NO_INTR(ret = recv(common->ctrl_fd, buffer, length, MSG_NOSIGNAL));
363 if (ret > 0) {
364 break;
365 }
366 if (ret == 0) {
367 ERROR("receive control data failed: peer closed");
368 break;
369 }
370 if (errno != EWOULDBLOCK && errno != EAGAIN) {
371 ERROR("receive control data failed: error(%s)", strerror(errno));
372 break;
373 }
374 if (i == (CTRL_CHAN_RETRY_COUNT - 1)) {
375 ERROR("receive control data failed: max retry count");
376 break;
377 }
378 INFO("receive control data failed (%s), retrying", strerror(errno));
379 }
380 if (ret <= 0) {
381 skt_disconnect(common->ctrl_fd);
382 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
383 }
384 return ret;
385 }
386
387 // Sends control info for stream |common|. The data to send is stored in
388 // |buffer| and has size |length|.
389 // On success, returns the number of octets sent, otherwise -1.
ha_ctrl_send(struct ha_stream_common * common,const void * buffer,size_t length)390 static int ha_ctrl_send(struct ha_stream_common* common, const void* buffer,
391 size_t length) {
392 ssize_t sent;
393 size_t remaining = length;
394 int i;
395
396 if (length == 0) return 0; // Nothing to do
397
398 for (i = 0;; i++) {
399 OSI_NO_INTR(sent = send(common->ctrl_fd, buffer, remaining, MSG_NOSIGNAL));
400 if (sent == static_cast<ssize_t>(remaining)) {
401 remaining = 0;
402 break;
403 }
404 if (sent > 0) {
405 buffer = (static_cast<const char*>(buffer) + sent);
406 remaining -= sent;
407 continue;
408 }
409 if (sent < 0) {
410 if (errno != EWOULDBLOCK && errno != EAGAIN) {
411 ERROR("send control data failed: error(%s)", strerror(errno));
412 break;
413 }
414 INFO("send control data failed (%s), retrying", strerror(errno));
415 }
416 if (i >= (CTRL_CHAN_RETRY_COUNT - 1)) {
417 ERROR("send control data failed: max retry count");
418 break;
419 }
420 }
421 if (remaining > 0) {
422 skt_disconnect(common->ctrl_fd);
423 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
424 return -1;
425 }
426 return length;
427 }
428
ha_command(struct ha_stream_common * common,tHEARING_AID_CTRL_CMD cmd)429 static int ha_command(struct ha_stream_common* common,
430 tHEARING_AID_CTRL_CMD cmd) {
431 char ack;
432
433 DEBUG("HEARING_AID COMMAND %s", audio_ha_hw_dump_ctrl_event(cmd));
434
435 if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED) {
436 INFO("starting up or recovering from previous error");
437 ha_open_ctrl_path(common);
438 if (common->ctrl_fd == AUDIO_SKT_DISCONNECTED) {
439 ERROR("failure to open ctrl path");
440 return -1;
441 }
442 }
443
444 /* send command */
445 ssize_t sent;
446 OSI_NO_INTR(sent = send(common->ctrl_fd, &cmd, 1, MSG_NOSIGNAL));
447 if (sent == -1) {
448 ERROR("cmd failed (%s)", strerror(errno));
449 skt_disconnect(common->ctrl_fd);
450 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
451 return -1;
452 }
453
454 /* wait for ack byte */
455 if (ha_ctrl_receive(common, &ack, 1) < 0) {
456 ERROR("HEARING_AID COMMAND %s: no ACK", audio_ha_hw_dump_ctrl_event(cmd));
457 return -1;
458 }
459
460 DEBUG("HEARING_AID COMMAND %s DONE STATUS %d",
461 audio_ha_hw_dump_ctrl_event(cmd), ack);
462
463 if (ack == HEARING_AID_CTRL_ACK_INCALL_FAILURE) return ack;
464 if (ack != HEARING_AID_CTRL_ACK_SUCCESS) {
465 ERROR("HEARING_AID COMMAND %s error %d", audio_ha_hw_dump_ctrl_event(cmd),
466 ack);
467 return -1;
468 }
469
470 return 0;
471 }
472
check_ha_ready(struct ha_stream_common * common)473 static int check_ha_ready(struct ha_stream_common* common) {
474 if (ha_command(common, HEARING_AID_CTRL_CMD_CHECK_READY) < 0) {
475 ERROR("check ha ready failed");
476 return -1;
477 }
478 return 0;
479 }
480
ha_read_input_audio_config(struct ha_stream_common * common)481 static int ha_read_input_audio_config(struct ha_stream_common* common) {
482 tHA_SAMPLE_RATE sample_rate;
483 tHA_CHANNEL_COUNT channel_count;
484
485 if (ha_command(common, HEARING_AID_CTRL_GET_INPUT_AUDIO_CONFIG) < 0) {
486 ERROR("get ha input audio config failed");
487 return -1;
488 }
489
490 if (ha_ctrl_receive(common, &sample_rate, sizeof(tHA_SAMPLE_RATE)) < 0)
491 return -1;
492 if (ha_ctrl_receive(common, &channel_count, sizeof(tHA_CHANNEL_COUNT)) < 0) {
493 return -1;
494 }
495
496 switch (sample_rate) {
497 case 16000:
498 case 24000:
499 case 44100:
500 case 48000:
501 common->cfg.rate = sample_rate;
502 break;
503 default:
504 ERROR("Invalid sample rate: %" PRIu32, sample_rate);
505 return -1;
506 }
507
508 switch (channel_count) {
509 case 1:
510 common->cfg.channel_mask = AUDIO_CHANNEL_IN_MONO;
511 break;
512 case 2:
513 common->cfg.channel_mask = AUDIO_CHANNEL_IN_STEREO;
514 break;
515 default:
516 ERROR("Invalid channel count: %" PRIu32, channel_count);
517 return -1;
518 }
519
520 // TODO: For now input audio format is always hard-coded as PCM 16-bit
521 common->cfg.format = AUDIO_FORMAT_PCM_16_BIT;
522
523 INFO("got input audio config %d %d", common->cfg.format, common->cfg.rate);
524
525 return 0;
526 }
527
ha_read_output_audio_config(struct ha_stream_common * common,btav_a2dp_codec_config_t * codec_config,btav_a2dp_codec_config_t * codec_capability,bool update_stream_config)528 static int ha_read_output_audio_config(
529 struct ha_stream_common* common, btav_a2dp_codec_config_t* codec_config,
530 btav_a2dp_codec_config_t* codec_capability, bool update_stream_config) {
531 struct ha_config stream_config;
532
533 if (ha_command(common, HEARING_AID_CTRL_GET_OUTPUT_AUDIO_CONFIG) < 0) {
534 ERROR("get ha output audio config failed");
535 return -1;
536 }
537
538 // Receive the current codec config
539 if (ha_ctrl_receive(common, &codec_config->sample_rate,
540 sizeof(btav_a2dp_codec_sample_rate_t)) < 0) {
541 return -1;
542 }
543 if (ha_ctrl_receive(common, &codec_config->bits_per_sample,
544 sizeof(btav_a2dp_codec_bits_per_sample_t)) < 0) {
545 return -1;
546 }
547 if (ha_ctrl_receive(common, &codec_config->channel_mode,
548 sizeof(btav_a2dp_codec_channel_mode_t)) < 0) {
549 return -1;
550 }
551
552 // Receive the current codec capability
553 if (ha_ctrl_receive(common, &codec_capability->sample_rate,
554 sizeof(btav_a2dp_codec_sample_rate_t)) < 0) {
555 return -1;
556 }
557 if (ha_ctrl_receive(common, &codec_capability->bits_per_sample,
558 sizeof(btav_a2dp_codec_bits_per_sample_t)) < 0) {
559 return -1;
560 }
561 if (ha_ctrl_receive(common, &codec_capability->channel_mode,
562 sizeof(btav_a2dp_codec_channel_mode_t)) < 0) {
563 return -1;
564 }
565
566 // Check the codec config sample rate
567 switch (codec_config->sample_rate) {
568 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
569 stream_config.rate = 44100;
570 break;
571 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
572 stream_config.rate = 48000;
573 break;
574 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
575 stream_config.rate = 88200;
576 break;
577 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
578 stream_config.rate = 96000;
579 break;
580 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
581 stream_config.rate = 176400;
582 break;
583 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
584 stream_config.rate = 192000;
585 break;
586 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
587 stream_config.rate = 16000;
588 break;
589 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
590 stream_config.rate = 24000;
591 break;
592 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
593 default:
594 ERROR("Invalid sample rate: 0x%x", codec_config->sample_rate);
595 return -1;
596 }
597
598 // Check the codec config bits per sample
599 switch (codec_config->bits_per_sample) {
600 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
601 stream_config.format = AUDIO_FORMAT_PCM_16_BIT;
602 break;
603 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
604 stream_config.format = AUDIO_FORMAT_PCM_24_BIT_PACKED;
605 break;
606 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
607 stream_config.format = AUDIO_FORMAT_PCM_32_BIT;
608 break;
609 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
610 default:
611 ERROR("Invalid bits per sample: 0x%x", codec_config->bits_per_sample);
612 return -1;
613 }
614
615 // Check the codec config channel mode
616 switch (codec_config->channel_mode) {
617 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
618 stream_config.channel_mask = AUDIO_CHANNEL_OUT_MONO;
619 stream_config.is_stereo_to_mono = true;
620 break;
621 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
622 stream_config.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
623 stream_config.is_stereo_to_mono = false;
624 break;
625 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
626 default:
627 ERROR("Invalid channel mode: 0x%x", codec_config->channel_mode);
628 return -1;
629 }
630 if (stream_config.is_stereo_to_mono) {
631 stream_config.channel_mask = AUDIO_CHANNEL_OUT_STEREO;
632 }
633
634 // Update the output stream configuration
635 if (update_stream_config) {
636 common->cfg.rate = stream_config.rate;
637 common->cfg.channel_mask = stream_config.channel_mask;
638 common->cfg.is_stereo_to_mono = stream_config.is_stereo_to_mono;
639 common->cfg.format = stream_config.format;
640 common->buffer_sz = audio_ha_hw_stream_compute_buffer_size(
641 codec_config->sample_rate, codec_config->bits_per_sample,
642 codec_config->channel_mode);
643 if (common->cfg.is_stereo_to_mono) {
644 // We need to fetch twice as much data from the Audio framework
645 common->buffer_sz *= 2;
646 }
647 }
648
649 INFO(
650 "got output codec config (update_stream_config=%s): "
651 "sample_rate=0x%x bits_per_sample=0x%x channel_mode=0x%x",
652 update_stream_config ? "true" : "false", codec_config->sample_rate,
653 codec_config->bits_per_sample, codec_config->channel_mode);
654
655 INFO(
656 "got output codec capability: sample_rate=0x%x bits_per_sample=0x%x "
657 "channel_mode=0x%x",
658 codec_capability->sample_rate, codec_capability->bits_per_sample,
659 codec_capability->channel_mode);
660
661 return 0;
662 }
663
ha_write_output_audio_config(struct ha_stream_common * common)664 static int ha_write_output_audio_config(struct ha_stream_common* common) {
665 btav_a2dp_codec_config_t codec_config;
666
667 if (ha_command(common, HEARING_AID_CTRL_SET_OUTPUT_AUDIO_CONFIG) < 0) {
668 ERROR("set ha output audio config failed");
669 return -1;
670 }
671
672 codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_NONE;
673 codec_config.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE;
674 codec_config.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_NONE;
675
676 switch (common->cfg.rate) {
677 case 44100:
678 codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_44100;
679 break;
680 case 48000:
681 codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_48000;
682 break;
683 case 88200:
684 codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_88200;
685 break;
686 case 96000:
687 codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_96000;
688 break;
689 case 176400:
690 codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_176400;
691 break;
692 case 192000:
693 codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_192000;
694 break;
695 case 16000:
696 codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_16000;
697 break;
698 case 24000:
699 codec_config.sample_rate = BTAV_A2DP_CODEC_SAMPLE_RATE_24000;
700 break;
701 default:
702 ERROR("Invalid sample rate: %" PRIu32, common->cfg.rate);
703 return -1;
704 }
705
706 switch (common->cfg.format) {
707 case AUDIO_FORMAT_PCM_16_BIT:
708 codec_config.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16;
709 break;
710 case AUDIO_FORMAT_PCM_24_BIT_PACKED:
711 codec_config.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24;
712 break;
713 case AUDIO_FORMAT_PCM_32_BIT:
714 codec_config.bits_per_sample = BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32;
715 break;
716 case AUDIO_FORMAT_PCM_8_24_BIT:
717 // All 24-bit audio is expected in AUDIO_FORMAT_PCM_24_BIT_PACKED format
718 FALLTHROUGH_INTENDED; /* FALLTHROUGH */
719 default:
720 ERROR("Invalid audio format: 0x%x", common->cfg.format);
721 return -1;
722 }
723
724 switch (common->cfg.channel_mask) {
725 case AUDIO_CHANNEL_OUT_MONO:
726 codec_config.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
727 break;
728 case AUDIO_CHANNEL_OUT_STEREO:
729 if (common->cfg.is_stereo_to_mono) {
730 codec_config.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_MONO;
731 } else {
732 codec_config.channel_mode = BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO;
733 }
734 break;
735 default:
736 ERROR("Invalid channel mask: 0x%x", common->cfg.channel_mask);
737 return -1;
738 }
739
740 // Send the current codec config that has been selected by us
741 if (ha_ctrl_send(common, &codec_config.sample_rate,
742 sizeof(btav_a2dp_codec_sample_rate_t)) < 0)
743 return -1;
744 if (ha_ctrl_send(common, &codec_config.bits_per_sample,
745 sizeof(btav_a2dp_codec_bits_per_sample_t)) < 0) {
746 return -1;
747 }
748 if (ha_ctrl_send(common, &codec_config.channel_mode,
749 sizeof(btav_a2dp_codec_channel_mode_t)) < 0) {
750 return -1;
751 }
752
753 INFO(
754 "sent output codec config: sample_rate=0x%x bits_per_sample=0x%x "
755 "channel_mode=0x%x",
756 codec_config.sample_rate, codec_config.bits_per_sample,
757 codec_config.channel_mode);
758
759 return 0;
760 }
761
ha_open_ctrl_path(struct ha_stream_common * common)762 static void ha_open_ctrl_path(struct ha_stream_common* common) {
763 int i;
764
765 if (common->ctrl_fd != AUDIO_SKT_DISCONNECTED) return; // already connected
766
767 /* retry logic to catch any timing variations on control channel */
768 for (i = 0; i < CTRL_CHAN_RETRY_COUNT; i++) {
769 /* connect control channel if not already connected */
770 if ((common->ctrl_fd = skt_connect(
771 HEARING_AID_CTRL_PATH, AUDIO_STREAM_CONTROL_OUTPUT_BUFFER_SZ)) >=
772 0) {
773 /* success, now check if stack is ready */
774 if (check_ha_ready(common) == 0) break;
775
776 ERROR("error : ha not ready, wait 250 ms and retry");
777 usleep(250000);
778 skt_disconnect(common->ctrl_fd);
779 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
780 }
781
782 /* ctrl channel not ready, wait a bit */
783 usleep(250000);
784 }
785 }
786
787 /*****************************************************************************
788 *
789 * AUDIO DATA PATH
790 *
791 ****************************************************************************/
792
ha_stream_common_init(struct ha_stream_common * common)793 static void ha_stream_common_init(struct ha_stream_common* common) {
794 FNLOG();
795
796 common->mutex = new std::recursive_mutex;
797
798 common->ctrl_fd = AUDIO_SKT_DISCONNECTED;
799 common->audio_fd = AUDIO_SKT_DISCONNECTED;
800 common->state = AUDIO_HA_STATE_STOPPED;
801
802 /* manages max capacity of socket pipe */
803 common->buffer_sz = AUDIO_STREAM_OUTPUT_BUFFER_SZ;
804 }
805
ha_stream_common_destroy(struct ha_stream_common * common)806 static void ha_stream_common_destroy(struct ha_stream_common* common) {
807 FNLOG();
808
809 delete common->mutex;
810 common->mutex = NULL;
811 }
812
start_audio_datapath(struct ha_stream_common * common)813 static int start_audio_datapath(struct ha_stream_common* common) {
814 INFO("state %d", common->state);
815
816 int oldstate = common->state;
817 common->state = AUDIO_HA_STATE_STARTING;
818
819 int ha_status = ha_command(common, HEARING_AID_CTRL_CMD_START);
820 if (ha_status < 0) {
821 ERROR("Audiopath start failed (status %d)", ha_status);
822 goto error;
823 } else if (ha_status == HEARING_AID_CTRL_ACK_INCALL_FAILURE) {
824 ERROR("Audiopath start failed - in call, move to suspended");
825 goto error;
826 }
827
828 /* connect socket if not yet connected */
829 if (common->audio_fd == AUDIO_SKT_DISCONNECTED) {
830 common->audio_fd = skt_connect(HEARING_AID_DATA_PATH, common->buffer_sz);
831 if (common->audio_fd < 0) {
832 ERROR("Audiopath start failed - error opening data socket");
833 goto error;
834 }
835 }
836 common->state = (ha_state_t)AUDIO_HA_STATE_STARTED;
837 return 0;
838
839 error:
840 common->state = (ha_state_t)oldstate;
841 return -1;
842 }
843
stop_audio_datapath(struct ha_stream_common * common)844 static int stop_audio_datapath(struct ha_stream_common* common) {
845 int oldstate = common->state;
846
847 INFO("state %d", common->state);
848
849 /* prevent any stray output writes from autostarting the stream
850 while stopping audiopath */
851 common->state = AUDIO_HA_STATE_STOPPING;
852
853 if (ha_command(common, HEARING_AID_CTRL_CMD_STOP) < 0) {
854 ERROR("audiopath stop failed");
855 common->state = (ha_state_t)oldstate;
856 return -1;
857 }
858
859 common->state = (ha_state_t)AUDIO_HA_STATE_STOPPED;
860
861 /* disconnect audio path */
862 skt_disconnect(common->audio_fd);
863 common->audio_fd = AUDIO_SKT_DISCONNECTED;
864
865 return 0;
866 }
867
suspend_audio_datapath(struct ha_stream_common * common,bool standby)868 static int suspend_audio_datapath(struct ha_stream_common* common,
869 bool standby) {
870 INFO("state %d", common->state);
871
872 if (common->state == AUDIO_HA_STATE_STOPPING) return -1;
873
874 if (ha_command(common, HEARING_AID_CTRL_CMD_SUSPEND) < 0) return -1;
875
876 if (standby)
877 common->state = AUDIO_HA_STATE_STANDBY;
878 else
879 common->state = AUDIO_HA_STATE_SUSPENDED;
880
881 /* disconnect audio path */
882 skt_disconnect(common->audio_fd);
883
884 common->audio_fd = AUDIO_SKT_DISCONNECTED;
885
886 return 0;
887 }
888
889 /*****************************************************************************
890 *
891 * audio output callbacks
892 *
893 ****************************************************************************/
894
out_write(struct audio_stream_out * stream,const void * buffer,size_t bytes)895 static ssize_t out_write(struct audio_stream_out* stream, const void* buffer,
896 size_t bytes) {
897 struct ha_stream_out* out = (struct ha_stream_out*)stream;
898 int sent = -1;
899 size_t write_bytes = bytes;
900
901 DEBUG("write %zu bytes (fd %d)", bytes, out->common.audio_fd);
902
903 std::unique_lock<std::recursive_mutex> lock(*out->common.mutex);
904 if (out->common.state == AUDIO_HA_STATE_SUSPENDED ||
905 out->common.state == AUDIO_HA_STATE_STOPPING) {
906 DEBUG("stream suspended or closing");
907 goto finish;
908 }
909
910 /* only allow autostarting if we are in stopped or standby */
911 if ((out->common.state == AUDIO_HA_STATE_STOPPED) ||
912 (out->common.state == AUDIO_HA_STATE_STANDBY)) {
913 if (start_audio_datapath(&out->common) < 0) {
914 goto finish;
915 }
916 } else if (out->common.state != AUDIO_HA_STATE_STARTED) {
917 ERROR("stream not in stopped or standby");
918 goto finish;
919 }
920
921 // Mix the stereo into mono if necessary
922 if (out->common.cfg.is_stereo_to_mono) {
923 const size_t frames = bytes / audio_stream_out_frame_size(stream);
924 int16_t* src = (int16_t*)buffer;
925 int16_t* dst = (int16_t*)buffer;
926 for (size_t i = 0; i < frames; i++, dst++, src += 2) {
927 *dst = (int16_t)(((int32_t)src[0] + (int32_t)src[1]) >> 1);
928 }
929 write_bytes /= 2;
930 DEBUG("stereo-to-mono mixing: write %zu bytes (fd %d)", write_bytes,
931 out->common.audio_fd);
932 }
933
934 lock.unlock();
935 sent = skt_write(out->common.audio_fd, buffer, write_bytes);
936 lock.lock();
937
938 if (sent == -1) {
939 skt_disconnect(out->common.audio_fd);
940 out->common.audio_fd = AUDIO_SKT_DISCONNECTED;
941 if ((out->common.state != AUDIO_HA_STATE_SUSPENDED) &&
942 (out->common.state != AUDIO_HA_STATE_STOPPING)) {
943 out->common.state = AUDIO_HA_STATE_STOPPED;
944 } else {
945 ERROR("write failed : stream suspended, avoid resetting state");
946 }
947 goto finish;
948 }
949
950 finish:;
951 const size_t frames = bytes / audio_stream_out_frame_size(stream);
952 out->frames_rendered += frames;
953 out->frames_presented += frames;
954 lock.unlock();
955
956 // If send didn't work out, sleep to emulate write delay.
957 if (sent == -1) {
958 const int us_delay = calc_audiotime_usec(out->common.cfg, bytes);
959 DEBUG("emulate ha write delay (%d us)", us_delay);
960 usleep(us_delay);
961 }
962 return bytes;
963 }
964
out_get_sample_rate(const struct audio_stream * stream)965 static uint32_t out_get_sample_rate(const struct audio_stream* stream) {
966 struct ha_stream_out* out = (struct ha_stream_out*)stream;
967
968 DEBUG("rate %" PRIu32, out->common.cfg.rate);
969
970 return out->common.cfg.rate;
971 }
972
out_set_sample_rate(struct audio_stream * stream,uint32_t rate)973 static int out_set_sample_rate(struct audio_stream* stream, uint32_t rate) {
974 struct ha_stream_out* out = (struct ha_stream_out*)stream;
975
976 DEBUG("out_set_sample_rate : %" PRIu32, rate);
977
978 out->common.cfg.rate = rate;
979
980 return 0;
981 }
982
out_get_buffer_size(const struct audio_stream * stream)983 static size_t out_get_buffer_size(const struct audio_stream* stream) {
984 struct ha_stream_out* out = (struct ha_stream_out*)stream;
985 // period_size is the AudioFlinger mixer buffer size.
986 const size_t period_size =
987 out->common.buffer_sz / AUDIO_STREAM_OUTPUT_BUFFER_PERIODS;
988
989 DEBUG("socket buffer size: %zu period size: %zu", out->common.buffer_sz,
990 period_size);
991
992 return period_size;
993 }
994
audio_ha_hw_stream_compute_buffer_size(btav_a2dp_codec_sample_rate_t codec_sample_rate,btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample,btav_a2dp_codec_channel_mode_t codec_channel_mode)995 size_t audio_ha_hw_stream_compute_buffer_size(
996 btav_a2dp_codec_sample_rate_t codec_sample_rate,
997 btav_a2dp_codec_bits_per_sample_t codec_bits_per_sample,
998 btav_a2dp_codec_channel_mode_t codec_channel_mode) {
999 size_t buffer_sz = AUDIO_STREAM_OUTPUT_BUFFER_SZ; // Default value
1000 const uint64_t time_period_ms = 20; // Conservative 20ms
1001 uint32_t sample_rate;
1002 uint32_t bits_per_sample;
1003 uint32_t number_of_channels;
1004
1005 // Check the codec config sample rate
1006 switch (codec_sample_rate) {
1007 case BTAV_A2DP_CODEC_SAMPLE_RATE_44100:
1008 sample_rate = 44100;
1009 break;
1010 case BTAV_A2DP_CODEC_SAMPLE_RATE_48000:
1011 sample_rate = 48000;
1012 break;
1013 case BTAV_A2DP_CODEC_SAMPLE_RATE_88200:
1014 sample_rate = 88200;
1015 break;
1016 case BTAV_A2DP_CODEC_SAMPLE_RATE_96000:
1017 sample_rate = 96000;
1018 break;
1019 case BTAV_A2DP_CODEC_SAMPLE_RATE_176400:
1020 sample_rate = 176400;
1021 break;
1022 case BTAV_A2DP_CODEC_SAMPLE_RATE_192000:
1023 sample_rate = 192000;
1024 break;
1025 case BTAV_A2DP_CODEC_SAMPLE_RATE_16000:
1026 sample_rate = 16000;
1027 break;
1028 case BTAV_A2DP_CODEC_SAMPLE_RATE_24000:
1029 sample_rate = 24000;
1030 break;
1031 case BTAV_A2DP_CODEC_SAMPLE_RATE_NONE:
1032 default:
1033 ERROR("Invalid sample rate: 0x%x", codec_sample_rate);
1034 return buffer_sz;
1035 }
1036
1037 // Check the codec config bits per sample
1038 switch (codec_bits_per_sample) {
1039 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16:
1040 bits_per_sample = 16;
1041 break;
1042 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24:
1043 bits_per_sample = 24;
1044 break;
1045 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32:
1046 bits_per_sample = 32;
1047 break;
1048 case BTAV_A2DP_CODEC_BITS_PER_SAMPLE_NONE:
1049 default:
1050 ERROR("Invalid bits per sample: 0x%x", codec_bits_per_sample);
1051 return buffer_sz;
1052 }
1053
1054 // Check the codec config channel mode
1055 switch (codec_channel_mode) {
1056 case BTAV_A2DP_CODEC_CHANNEL_MODE_MONO:
1057 number_of_channels = 1;
1058 break;
1059 case BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO:
1060 number_of_channels = 2;
1061 break;
1062 case BTAV_A2DP_CODEC_CHANNEL_MODE_NONE:
1063 default:
1064 ERROR("Invalid channel mode: 0x%x", codec_channel_mode);
1065 return buffer_sz;
1066 }
1067
1068 //
1069 // The buffer size is computed by using the following formula:
1070 //
1071 // AUDIO_STREAM_OUTPUT_BUFFER_SIZE =
1072 // (TIME_PERIOD_MS * AUDIO_STREAM_OUTPUT_BUFFER_PERIODS *
1073 // SAMPLE_RATE_HZ * NUMBER_OF_CHANNELS * (BITS_PER_SAMPLE / 8)) / 1000
1074 //
1075 // AUDIO_STREAM_OUTPUT_BUFFER_PERIODS controls how the socket buffer is
1076 // divided for AudioFlinger data delivery. The AudioFlinger mixer delivers
1077 // data in chunks of
1078 // (AUDIO_STREAM_OUTPUT_BUFFER_SIZE / AUDIO_STREAM_OUTPUT_BUFFER_PERIODS) .
1079 // If the number of periods is 2, the socket buffer represents "double
1080 // buffering" of the AudioFlinger mixer buffer.
1081 //
1082 // Furthermore, the AudioFlinger expects the buffer size to be a multiple
1083 // of 16 frames.
1084 const size_t divisor = (AUDIO_STREAM_OUTPUT_BUFFER_PERIODS * 16 *
1085 number_of_channels * bits_per_sample) /
1086 8;
1087
1088 buffer_sz = (time_period_ms * AUDIO_STREAM_OUTPUT_BUFFER_PERIODS *
1089 sample_rate * number_of_channels * (bits_per_sample / 8)) /
1090 1000;
1091
1092 // Adjust the buffer size so it can be divided by the divisor
1093 const size_t remainder = buffer_sz % divisor;
1094 if (remainder != 0) {
1095 buffer_sz += divisor - remainder;
1096 }
1097
1098 return buffer_sz;
1099 }
1100
out_get_channels(const struct audio_stream * stream)1101 static audio_channel_mask_t out_get_channels(
1102 const struct audio_stream* stream) {
1103 struct ha_stream_out* out = (struct ha_stream_out*)stream;
1104
1105 DEBUG("channels 0x%" PRIx32, out->common.cfg.channel_mask);
1106
1107 return (audio_channel_mask_t)out->common.cfg.channel_mask;
1108 }
1109
out_get_format(const struct audio_stream * stream)1110 static audio_format_t out_get_format(const struct audio_stream* stream) {
1111 struct ha_stream_out* out = (struct ha_stream_out*)stream;
1112 DEBUG("format 0x%x", out->common.cfg.format);
1113 return (audio_format_t)out->common.cfg.format;
1114 }
1115
out_set_format(UNUSED_ATTR struct audio_stream * stream,UNUSED_ATTR audio_format_t format)1116 static int out_set_format(UNUSED_ATTR struct audio_stream* stream,
1117 UNUSED_ATTR audio_format_t format) {
1118 DEBUG("setting format not yet supported (0x%x)", format);
1119 return -ENOSYS;
1120 }
1121
out_standby(struct audio_stream * stream)1122 static int out_standby(struct audio_stream* stream) {
1123 struct ha_stream_out* out = (struct ha_stream_out*)stream;
1124 int retVal = 0;
1125
1126 FNLOG();
1127
1128 std::lock_guard<std::recursive_mutex> lock(*out->common.mutex);
1129 // Do nothing in SUSPENDED state.
1130 if (out->common.state != AUDIO_HA_STATE_SUSPENDED)
1131 retVal = suspend_audio_datapath(&out->common, true);
1132 out->frames_rendered = 0; // rendered is reset, presented is not
1133
1134 return retVal;
1135 }
1136
out_dump(UNUSED_ATTR const struct audio_stream * stream,UNUSED_ATTR int fd)1137 static int out_dump(UNUSED_ATTR const struct audio_stream* stream,
1138 UNUSED_ATTR int fd) {
1139 FNLOG();
1140 return 0;
1141 }
1142
out_set_parameters(struct audio_stream * stream,const char * kvpairs)1143 static int out_set_parameters(struct audio_stream* stream,
1144 const char* kvpairs) {
1145 struct ha_stream_out* out = (struct ha_stream_out*)stream;
1146
1147 INFO("state %d kvpairs %s", out->common.state, kvpairs);
1148
1149 std::unordered_map<std::string, std::string> params =
1150 hash_map_utils_new_from_string_params(kvpairs);
1151 int status = 0;
1152
1153 if (params.empty()) return status;
1154
1155 std::lock_guard<std::recursive_mutex> lock(*out->common.mutex);
1156
1157 /* dump params */
1158 hash_map_utils_dump_string_keys_string_values(params);
1159
1160 if (params["closing"].compare("true") == 0) {
1161 DEBUG("stream closing, disallow any writes");
1162 out->common.state = AUDIO_HA_STATE_STOPPING;
1163 }
1164
1165 if (params["HearingAidSuspended"].compare("true") == 0) {
1166 if (out->common.state == AUDIO_HA_STATE_STARTED)
1167 status = suspend_audio_datapath(&out->common, false);
1168 } else {
1169 /* Do not start the streaming automatically. If the phone was streaming
1170 * prior to being suspended, the next out_write shall trigger the
1171 * AVDTP start procedure */
1172 if (out->common.state == AUDIO_HA_STATE_SUSPENDED)
1173 out->common.state = AUDIO_HA_STATE_STANDBY;
1174 /* Irrespective of the state, return 0 */
1175 }
1176
1177 return status;
1178 }
1179
out_get_parameters(const struct audio_stream * stream,const char * keys)1180 static char* out_get_parameters(const struct audio_stream* stream,
1181 const char* keys) {
1182 FNLOG();
1183
1184 btav_a2dp_codec_config_t codec_config;
1185 btav_a2dp_codec_config_t codec_capability;
1186
1187 struct ha_stream_out* out = (struct ha_stream_out*)stream;
1188
1189 std::unordered_map<std::string, std::string> params =
1190 hash_map_utils_new_from_string_params(keys);
1191 std::unordered_map<std::string, std::string> return_params;
1192
1193 if (params.empty()) return strdup("");
1194
1195 std::lock_guard<std::recursive_mutex> lock(*out->common.mutex);
1196
1197 if (ha_read_output_audio_config(&out->common, &codec_config,
1198 &codec_capability,
1199 false /* update_stream_config */) < 0) {
1200 ERROR("ha_read_output_audio_config failed");
1201 goto done;
1202 }
1203
1204 // Add the format
1205 if (params.find(AUDIO_PARAMETER_STREAM_SUP_FORMATS) != params.end()) {
1206 std::string param;
1207 if (codec_capability.bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_16) {
1208 if (!param.empty()) param += "|";
1209 param += "AUDIO_FORMAT_PCM_16_BIT";
1210 }
1211 if (codec_capability.bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_24) {
1212 if (!param.empty()) param += "|";
1213 param += "AUDIO_FORMAT_PCM_24_BIT_PACKED";
1214 }
1215 if (codec_capability.bits_per_sample & BTAV_A2DP_CODEC_BITS_PER_SAMPLE_32) {
1216 if (!param.empty()) param += "|";
1217 param += "AUDIO_FORMAT_PCM_32_BIT";
1218 }
1219 if (param.empty()) {
1220 ERROR("Invalid codec capability bits_per_sample=0x%x",
1221 codec_capability.bits_per_sample);
1222 goto done;
1223 } else {
1224 return_params[AUDIO_PARAMETER_STREAM_SUP_FORMATS] = param;
1225 }
1226 }
1227
1228 // Add the sample rate
1229 if (params.find(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES) != params.end()) {
1230 std::string param;
1231 if (codec_capability.sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_44100) {
1232 if (!param.empty()) param += "|";
1233 param += "44100";
1234 }
1235 if (codec_capability.sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_48000) {
1236 if (!param.empty()) param += "|";
1237 param += "48000";
1238 }
1239 if (codec_capability.sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_88200) {
1240 if (!param.empty()) param += "|";
1241 param += "88200";
1242 }
1243 if (codec_capability.sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_96000) {
1244 if (!param.empty()) param += "|";
1245 param += "96000";
1246 }
1247 if (codec_capability.sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_176400) {
1248 if (!param.empty()) param += "|";
1249 param += "176400";
1250 }
1251 if (codec_capability.sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_192000) {
1252 if (!param.empty()) param += "|";
1253 param += "192000";
1254 }
1255 if (codec_capability.sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_16000) {
1256 if (!param.empty()) param += "|";
1257 param += "16000";
1258 }
1259 if (codec_capability.sample_rate & BTAV_A2DP_CODEC_SAMPLE_RATE_24000) {
1260 if (!param.empty()) param += "|";
1261 param += "24000";
1262 }
1263 if (param.empty()) {
1264 ERROR("Invalid codec capability sample_rate=0x%x",
1265 codec_capability.sample_rate);
1266 goto done;
1267 } else {
1268 return_params[AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES] = param;
1269 }
1270 }
1271
1272 // Add the channel mask
1273 if (params.find(AUDIO_PARAMETER_STREAM_SUP_CHANNELS) != params.end()) {
1274 std::string param;
1275 if (codec_capability.channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_MONO) {
1276 if (!param.empty()) param += "|";
1277 param += "AUDIO_CHANNEL_OUT_MONO";
1278 }
1279 if (codec_capability.channel_mode & BTAV_A2DP_CODEC_CHANNEL_MODE_STEREO) {
1280 if (!param.empty()) param += "|";
1281 param += "AUDIO_CHANNEL_OUT_STEREO";
1282 }
1283 if (param.empty()) {
1284 ERROR("Invalid codec capability channel_mode=0x%x",
1285 codec_capability.channel_mode);
1286 goto done;
1287 } else {
1288 return_params[AUDIO_PARAMETER_STREAM_SUP_CHANNELS] = param;
1289 }
1290 }
1291
1292 done:
1293 std::string result;
1294 for (const auto& ptr : return_params) {
1295 result += ptr.first + "=" + ptr.second + ";";
1296 }
1297
1298 INFO("get parameters result = %s", result.c_str());
1299
1300 return strdup(result.c_str());
1301 }
1302
out_get_latency(const struct audio_stream_out * stream)1303 static uint32_t out_get_latency(const struct audio_stream_out* stream) {
1304 int latency_us;
1305
1306 struct ha_stream_out* out = (struct ha_stream_out*)stream;
1307
1308 FNLOG();
1309
1310 latency_us =
1311 ((out->common.buffer_sz * 1000) /
1312 audio_stream_out_frame_size(&out->stream) / out->common.cfg.rate) *
1313 1000;
1314
1315 return (latency_us / 1000) + 200;
1316 }
1317
out_set_volume(UNUSED_ATTR struct audio_stream_out * stream,UNUSED_ATTR float left,UNUSED_ATTR float right)1318 static int out_set_volume(UNUSED_ATTR struct audio_stream_out* stream,
1319 UNUSED_ATTR float left, UNUSED_ATTR float right) {
1320 FNLOG();
1321
1322 /* volume controlled in audioflinger mixer (digital) */
1323
1324 return -ENOSYS;
1325 }
1326
out_get_presentation_position(const struct audio_stream_out * stream,uint64_t * frames,struct timespec * timestamp)1327 static int out_get_presentation_position(const struct audio_stream_out* stream,
1328 uint64_t* frames,
1329 struct timespec* timestamp) {
1330 struct ha_stream_out* out = (struct ha_stream_out*)stream;
1331
1332 FNLOG();
1333 if (stream == NULL || frames == NULL || timestamp == NULL) return -EINVAL;
1334
1335 int ret = -EWOULDBLOCK;
1336 std::lock_guard<std::recursive_mutex> lock(*out->common.mutex);
1337 uint64_t latency_frames =
1338 (uint64_t)out_get_latency(stream) * out->common.cfg.rate / 1000;
1339 if (out->frames_presented >= latency_frames) {
1340 *frames = out->frames_presented - latency_frames;
1341 clock_gettime(CLOCK_MONOTONIC,
1342 timestamp); // could also be associated with out_write().
1343 ret = 0;
1344 }
1345 return ret;
1346 }
1347
out_get_render_position(const struct audio_stream_out * stream,uint32_t * dsp_frames)1348 static int out_get_render_position(const struct audio_stream_out* stream,
1349 uint32_t* dsp_frames) {
1350 struct ha_stream_out* out = (struct ha_stream_out*)stream;
1351
1352 FNLOG();
1353 if (stream == NULL || dsp_frames == NULL) return -EINVAL;
1354
1355 std::lock_guard<std::recursive_mutex> lock(*out->common.mutex);
1356 uint64_t latency_frames =
1357 (uint64_t)out_get_latency(stream) * out->common.cfg.rate / 1000;
1358 if (out->frames_rendered >= latency_frames) {
1359 *dsp_frames = (uint32_t)(out->frames_rendered - latency_frames);
1360 } else {
1361 *dsp_frames = 0;
1362 }
1363 return 0;
1364 }
1365
out_add_audio_effect(UNUSED_ATTR const struct audio_stream * stream,UNUSED_ATTR effect_handle_t effect)1366 static int out_add_audio_effect(UNUSED_ATTR const struct audio_stream* stream,
1367 UNUSED_ATTR effect_handle_t effect) {
1368 FNLOG();
1369 return 0;
1370 }
1371
out_remove_audio_effect(UNUSED_ATTR const struct audio_stream * stream,UNUSED_ATTR effect_handle_t effect)1372 static int out_remove_audio_effect(
1373 UNUSED_ATTR const struct audio_stream* stream,
1374 UNUSED_ATTR effect_handle_t effect) {
1375 FNLOG();
1376 return 0;
1377 }
1378
1379 /*
1380 * AUDIO INPUT STREAM
1381 */
1382
in_get_sample_rate(const struct audio_stream * stream)1383 static uint32_t in_get_sample_rate(const struct audio_stream* stream) {
1384 struct ha_stream_in* in = (struct ha_stream_in*)stream;
1385
1386 FNLOG();
1387 return in->common.cfg.rate;
1388 }
1389
in_set_sample_rate(struct audio_stream * stream,uint32_t rate)1390 static int in_set_sample_rate(struct audio_stream* stream, uint32_t rate) {
1391 struct ha_stream_in* in = (struct ha_stream_in*)stream;
1392
1393 FNLOG();
1394
1395 if (in->common.cfg.rate > 0 && in->common.cfg.rate == rate)
1396 return 0;
1397 else
1398 return -1;
1399 }
1400
in_get_buffer_size(UNUSED_ATTR const struct audio_stream * stream)1401 static size_t in_get_buffer_size(
1402 UNUSED_ATTR const struct audio_stream* stream) {
1403 FNLOG();
1404 return 320;
1405 }
1406
in_get_channels(const struct audio_stream * stream)1407 static audio_channel_mask_t in_get_channels(const struct audio_stream* stream) {
1408 struct ha_stream_in* in = (struct ha_stream_in*)stream;
1409
1410 FNLOG();
1411 return (audio_channel_mask_t)in->common.cfg.channel_mask;
1412 }
1413
in_get_format(UNUSED_ATTR const struct audio_stream * stream)1414 static audio_format_t in_get_format(
1415 UNUSED_ATTR const struct audio_stream* stream) {
1416 FNLOG();
1417 return AUDIO_FORMAT_PCM_16_BIT;
1418 }
1419
in_set_format(UNUSED_ATTR struct audio_stream * stream,UNUSED_ATTR audio_format_t format)1420 static int in_set_format(UNUSED_ATTR struct audio_stream* stream,
1421 UNUSED_ATTR audio_format_t format) {
1422 FNLOG();
1423 if (format == AUDIO_FORMAT_PCM_16_BIT)
1424 return 0;
1425 else
1426 return -1;
1427 }
1428
in_standby(UNUSED_ATTR struct audio_stream * stream)1429 static int in_standby(UNUSED_ATTR struct audio_stream* stream) {
1430 FNLOG();
1431 return 0;
1432 }
1433
in_dump(UNUSED_ATTR const struct audio_stream * stream,UNUSED_ATTR int fd)1434 static int in_dump(UNUSED_ATTR const struct audio_stream* stream,
1435 UNUSED_ATTR int fd) {
1436 FNLOG();
1437 return 0;
1438 }
1439
in_set_parameters(UNUSED_ATTR struct audio_stream * stream,UNUSED_ATTR const char * kvpairs)1440 static int in_set_parameters(UNUSED_ATTR struct audio_stream* stream,
1441 UNUSED_ATTR const char* kvpairs) {
1442 FNLOG();
1443 return 0;
1444 }
1445
in_get_parameters(UNUSED_ATTR const struct audio_stream * stream,UNUSED_ATTR const char * keys)1446 static char* in_get_parameters(UNUSED_ATTR const struct audio_stream* stream,
1447 UNUSED_ATTR const char* keys) {
1448 FNLOG();
1449 return strdup("");
1450 }
1451
in_set_gain(UNUSED_ATTR struct audio_stream_in * stream,UNUSED_ATTR float gain)1452 static int in_set_gain(UNUSED_ATTR struct audio_stream_in* stream,
1453 UNUSED_ATTR float gain) {
1454 FNLOG();
1455 return 0;
1456 }
1457
in_read(struct audio_stream_in * stream,void * buffer,size_t bytes)1458 static ssize_t in_read(struct audio_stream_in* stream, void* buffer,
1459 size_t bytes) {
1460 struct ha_stream_in* in = (struct ha_stream_in*)stream;
1461 int read;
1462 int us_delay;
1463
1464 DEBUG("read %zu bytes, state: %d", bytes, in->common.state);
1465
1466 std::unique_lock<std::recursive_mutex> lock(*in->common.mutex);
1467 if (in->common.state == AUDIO_HA_STATE_SUSPENDED ||
1468 in->common.state == AUDIO_HA_STATE_STOPPING) {
1469 DEBUG("stream suspended");
1470 goto error;
1471 }
1472
1473 /* only allow autostarting if we are in stopped or standby */
1474 if ((in->common.state == AUDIO_HA_STATE_STOPPED) ||
1475 (in->common.state == AUDIO_HA_STATE_STANDBY)) {
1476 if (start_audio_datapath(&in->common) < 0) {
1477 goto error;
1478 }
1479 } else if (in->common.state != AUDIO_HA_STATE_STARTED) {
1480 ERROR("stream not in stopped or standby");
1481 goto error;
1482 }
1483
1484 lock.unlock();
1485 read = skt_read(in->common.audio_fd, buffer, bytes);
1486 lock.lock();
1487 if (read == -1) {
1488 skt_disconnect(in->common.audio_fd);
1489 in->common.audio_fd = AUDIO_SKT_DISCONNECTED;
1490 if ((in->common.state != AUDIO_HA_STATE_SUSPENDED) &&
1491 (in->common.state != AUDIO_HA_STATE_STOPPING)) {
1492 in->common.state = AUDIO_HA_STATE_STOPPED;
1493 } else {
1494 ERROR("read failed : stream suspended, avoid resetting state");
1495 }
1496 goto error;
1497 } else if (read == 0) {
1498 DEBUG("read time out - return zeros");
1499 memset(buffer, 0, bytes);
1500 read = bytes;
1501 }
1502 lock.unlock();
1503
1504 DEBUG("read %d bytes out of %zu bytes", read, bytes);
1505 return read;
1506
1507 error:
1508 memset(buffer, 0, bytes);
1509 us_delay = calc_audiotime_usec(in->common.cfg, bytes);
1510 DEBUG("emulate ha read delay (%d us)", us_delay);
1511
1512 usleep(us_delay);
1513 return bytes;
1514 }
1515
in_get_input_frames_lost(UNUSED_ATTR struct audio_stream_in * stream)1516 static uint32_t in_get_input_frames_lost(
1517 UNUSED_ATTR struct audio_stream_in* stream) {
1518 FNLOG();
1519 return 0;
1520 }
1521
in_add_audio_effect(UNUSED_ATTR const struct audio_stream * stream,UNUSED_ATTR effect_handle_t effect)1522 static int in_add_audio_effect(UNUSED_ATTR const struct audio_stream* stream,
1523 UNUSED_ATTR effect_handle_t effect) {
1524 FNLOG();
1525 return 0;
1526 }
1527
in_remove_audio_effect(UNUSED_ATTR const struct audio_stream * stream,UNUSED_ATTR effect_handle_t effect)1528 static int in_remove_audio_effect(UNUSED_ATTR const struct audio_stream* stream,
1529 UNUSED_ATTR effect_handle_t effect) {
1530 FNLOG();
1531
1532 return 0;
1533 }
1534
adev_open_output_stream(struct audio_hw_device * dev,UNUSED_ATTR audio_io_handle_t handle,UNUSED_ATTR audio_devices_t devices,UNUSED_ATTR audio_output_flags_t flags,struct audio_config * config,struct audio_stream_out ** stream_out,UNUSED_ATTR const char * address)1535 static int adev_open_output_stream(struct audio_hw_device* dev,
1536 UNUSED_ATTR audio_io_handle_t handle,
1537 UNUSED_ATTR audio_devices_t devices,
1538 UNUSED_ATTR audio_output_flags_t flags,
1539 struct audio_config* config,
1540 struct audio_stream_out** stream_out,
1541 UNUSED_ATTR const char* address)
1542
1543 {
1544 struct ha_audio_device* ha_dev = (struct ha_audio_device*)dev;
1545 struct ha_stream_out* out;
1546 int ret = 0;
1547
1548 INFO("opening output");
1549 // protect against adev->output and stream_out from being inconsistent
1550 std::lock_guard<std::recursive_mutex> lock(*ha_dev->mutex);
1551 out = (struct ha_stream_out*)calloc(1, sizeof(struct ha_stream_out));
1552
1553 if (!out) return -ENOMEM;
1554
1555 out->stream.common.get_sample_rate = out_get_sample_rate;
1556 out->stream.common.set_sample_rate = out_set_sample_rate;
1557 out->stream.common.get_buffer_size = out_get_buffer_size;
1558 out->stream.common.get_channels = out_get_channels;
1559 out->stream.common.get_format = out_get_format;
1560 out->stream.common.set_format = out_set_format;
1561 out->stream.common.standby = out_standby;
1562 out->stream.common.dump = out_dump;
1563 out->stream.common.set_parameters = out_set_parameters;
1564 out->stream.common.get_parameters = out_get_parameters;
1565 out->stream.common.add_audio_effect = out_add_audio_effect;
1566 out->stream.common.remove_audio_effect = out_remove_audio_effect;
1567 out->stream.get_latency = out_get_latency;
1568 out->stream.set_volume = out_set_volume;
1569 out->stream.write = out_write;
1570 out->stream.get_render_position = out_get_render_position;
1571 out->stream.get_presentation_position = out_get_presentation_position;
1572
1573 /* initialize ha specifics */
1574 ha_stream_common_init(&out->common);
1575
1576 // Make sure we always have the feeding parameters configured
1577 btav_a2dp_codec_config_t codec_config;
1578 btav_a2dp_codec_config_t codec_capability;
1579 if (ha_read_output_audio_config(&out->common, &codec_config,
1580 &codec_capability,
1581 true /* update_stream_config */) < 0) {
1582 ERROR("ha_read_output_audio_config failed");
1583 ret = -1;
1584 goto err_open;
1585 }
1586 // ha_read_output_audio_config() opens the socket control path (or fails)
1587
1588 /* set output config values */
1589 if (config != nullptr) {
1590 // Try to use the config parameters and send it to the remote side
1591 // TODO: Shall we use out_set_format() and similar?
1592 if (config->format != 0) out->common.cfg.format = config->format;
1593 if (config->sample_rate != 0) out->common.cfg.rate = config->sample_rate;
1594 if (config->channel_mask != 0)
1595 out->common.cfg.channel_mask = config->channel_mask;
1596 if ((out->common.cfg.format != 0) || (out->common.cfg.rate != 0) ||
1597 (out->common.cfg.channel_mask != 0)) {
1598 if (ha_write_output_audio_config(&out->common) < 0) {
1599 ERROR("ha_write_output_audio_config failed");
1600 ret = -1;
1601 goto err_open;
1602 }
1603 // Read again and make sure we use the same parameters as the remote side
1604 if (ha_read_output_audio_config(&out->common, &codec_config,
1605 &codec_capability,
1606 true /* update_stream_config */) < 0) {
1607 ERROR("ha_read_output_audio_config failed");
1608 ret = -1;
1609 goto err_open;
1610 }
1611 }
1612 config->format = out_get_format((const struct audio_stream*)&out->stream);
1613 config->sample_rate =
1614 out_get_sample_rate((const struct audio_stream*)&out->stream);
1615 config->channel_mask =
1616 out_get_channels((const struct audio_stream*)&out->stream);
1617
1618 INFO(
1619 "Output stream config: format=0x%x sample_rate=%d channel_mask=0x%x "
1620 "buffer_sz=%zu",
1621 config->format, config->sample_rate, config->channel_mask,
1622 out->common.buffer_sz);
1623 }
1624 *stream_out = &out->stream;
1625 ha_dev->output = out;
1626
1627 DEBUG("success");
1628 /* Delay to ensure Headset is in proper state when START is initiated from
1629 * DUT immediately after the connection due to ongoing music playback. */
1630 usleep(250000);
1631 return 0;
1632
1633 err_open:
1634 ha_stream_common_destroy(&out->common);
1635 free(out);
1636 *stream_out = NULL;
1637 ha_dev->output = NULL;
1638 ERROR("failed");
1639 return ret;
1640 }
1641
adev_close_output_stream(struct audio_hw_device * dev,struct audio_stream_out * stream)1642 static void adev_close_output_stream(struct audio_hw_device* dev,
1643 struct audio_stream_out* stream) {
1644 struct ha_audio_device* ha_dev = (struct ha_audio_device*)dev;
1645 struct ha_stream_out* out = (struct ha_stream_out*)stream;
1646
1647 // prevent interference with adev_set_parameters.
1648 std::lock_guard<std::recursive_mutex> lock(*ha_dev->mutex);
1649 {
1650 std::lock_guard<std::recursive_mutex> lock(*out->common.mutex);
1651 const ha_state_t state = out->common.state;
1652 INFO("closing output (state %d)", (int)state);
1653 if ((state == AUDIO_HA_STATE_STARTED) ||
1654 (state == AUDIO_HA_STATE_STOPPING)) {
1655 stop_audio_datapath(&out->common);
1656 }
1657
1658 skt_disconnect(out->common.ctrl_fd);
1659 out->common.ctrl_fd = AUDIO_SKT_DISCONNECTED;
1660 }
1661
1662 ha_stream_common_destroy(&out->common);
1663 free(stream);
1664 ha_dev->output = NULL;
1665
1666 DEBUG("done");
1667 }
1668
adev_set_parameters(struct audio_hw_device * dev,const char * kvpairs)1669 static int adev_set_parameters(struct audio_hw_device* dev,
1670 const char* kvpairs) {
1671 struct ha_audio_device* ha_dev = (struct ha_audio_device*)dev;
1672 int retval = 0;
1673
1674 // prevent interference with adev_close_output_stream
1675 std::lock_guard<std::recursive_mutex> lock(*ha_dev->mutex);
1676 struct ha_stream_out* out = ha_dev->output;
1677
1678 if (out == NULL) return retval;
1679
1680 INFO("state %d", out->common.state);
1681
1682 retval =
1683 out->stream.common.set_parameters((struct audio_stream*)out, kvpairs);
1684
1685 return retval;
1686 }
1687
adev_get_parameters(UNUSED_ATTR const struct audio_hw_device * dev,const char * keys)1688 static char* adev_get_parameters(UNUSED_ATTR const struct audio_hw_device* dev,
1689 const char* keys) {
1690 FNLOG();
1691
1692 std::unordered_map<std::string, std::string> params =
1693 hash_map_utils_new_from_string_params(keys);
1694 hash_map_utils_dump_string_keys_string_values(params);
1695
1696 return strdup("");
1697 }
1698
adev_init_check(UNUSED_ATTR const struct audio_hw_device * dev)1699 static int adev_init_check(UNUSED_ATTR const struct audio_hw_device* dev) {
1700 FNLOG();
1701
1702 return 0;
1703 }
1704
adev_set_voice_volume(UNUSED_ATTR struct audio_hw_device * dev,UNUSED_ATTR float volume)1705 static int adev_set_voice_volume(UNUSED_ATTR struct audio_hw_device* dev,
1706 UNUSED_ATTR float volume) {
1707 FNLOG();
1708
1709 return -ENOSYS;
1710 }
1711
adev_set_master_volume(UNUSED_ATTR struct audio_hw_device * dev,UNUSED_ATTR float volume)1712 static int adev_set_master_volume(UNUSED_ATTR struct audio_hw_device* dev,
1713 UNUSED_ATTR float volume) {
1714 FNLOG();
1715
1716 return -ENOSYS;
1717 }
1718
adev_set_mode(UNUSED_ATTR struct audio_hw_device * dev,UNUSED_ATTR audio_mode_t mode)1719 static int adev_set_mode(UNUSED_ATTR struct audio_hw_device* dev,
1720 UNUSED_ATTR audio_mode_t mode) {
1721 FNLOG();
1722
1723 return 0;
1724 }
1725
adev_set_mic_mute(UNUSED_ATTR struct audio_hw_device * dev,UNUSED_ATTR bool state)1726 static int adev_set_mic_mute(UNUSED_ATTR struct audio_hw_device* dev,
1727 UNUSED_ATTR bool state) {
1728 FNLOG();
1729
1730 return -ENOSYS;
1731 }
1732
adev_get_mic_mute(UNUSED_ATTR const struct audio_hw_device * dev,UNUSED_ATTR bool * state)1733 static int adev_get_mic_mute(UNUSED_ATTR const struct audio_hw_device* dev,
1734 UNUSED_ATTR bool* state) {
1735 FNLOG();
1736
1737 return -ENOSYS;
1738 }
1739
adev_get_input_buffer_size(UNUSED_ATTR const struct audio_hw_device * dev,UNUSED_ATTR const struct audio_config * config)1740 static size_t adev_get_input_buffer_size(
1741 UNUSED_ATTR const struct audio_hw_device* dev,
1742 UNUSED_ATTR const struct audio_config* config) {
1743 FNLOG();
1744
1745 return 320;
1746 }
1747
adev_open_input_stream(struct audio_hw_device * dev,UNUSED_ATTR audio_io_handle_t handle,UNUSED_ATTR audio_devices_t devices,UNUSED_ATTR struct audio_config * config,struct audio_stream_in ** stream_in,UNUSED_ATTR audio_input_flags_t flags,UNUSED_ATTR const char * address,UNUSED_ATTR audio_source_t source)1748 static int adev_open_input_stream(struct audio_hw_device* dev,
1749 UNUSED_ATTR audio_io_handle_t handle,
1750 UNUSED_ATTR audio_devices_t devices,
1751 UNUSED_ATTR struct audio_config* config,
1752 struct audio_stream_in** stream_in,
1753 UNUSED_ATTR audio_input_flags_t flags,
1754 UNUSED_ATTR const char* address,
1755 UNUSED_ATTR audio_source_t source) {
1756 struct ha_audio_device* ha_dev = (struct ha_audio_device*)dev;
1757 struct ha_stream_in* in;
1758 int ret;
1759
1760 FNLOG();
1761
1762 // protect against adev->input and stream_in from being inconsistent
1763 std::lock_guard<std::recursive_mutex> lock(*ha_dev->mutex);
1764 in = (struct ha_stream_in*)calloc(1, sizeof(struct ha_stream_in));
1765
1766 if (!in) return -ENOMEM;
1767
1768 in->stream.common.get_sample_rate = in_get_sample_rate;
1769 in->stream.common.set_sample_rate = in_set_sample_rate;
1770 in->stream.common.get_buffer_size = in_get_buffer_size;
1771 in->stream.common.get_channels = in_get_channels;
1772 in->stream.common.get_format = in_get_format;
1773 in->stream.common.set_format = in_set_format;
1774 in->stream.common.standby = in_standby;
1775 in->stream.common.dump = in_dump;
1776 in->stream.common.set_parameters = in_set_parameters;
1777 in->stream.common.get_parameters = in_get_parameters;
1778 in->stream.common.add_audio_effect = in_add_audio_effect;
1779 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1780 in->stream.set_gain = in_set_gain;
1781 in->stream.read = in_read;
1782 in->stream.get_input_frames_lost = in_get_input_frames_lost;
1783
1784 /* initialize ha specifics */
1785 ha_stream_common_init(&in->common);
1786
1787 *stream_in = &in->stream;
1788 ha_dev->input = in;
1789
1790 if (ha_read_input_audio_config(&in->common) < 0) {
1791 ERROR("ha_read_input_audio_config failed (%s)", strerror(errno));
1792 ret = -1;
1793 goto err_open;
1794 }
1795 // ha_read_input_audio_config() opens socket control path (or fails)
1796
1797 DEBUG("success");
1798 return 0;
1799
1800 err_open:
1801 ha_stream_common_destroy(&in->common);
1802 free(in);
1803 *stream_in = NULL;
1804 ha_dev->input = NULL;
1805 ERROR("failed");
1806 return ret;
1807 }
1808
adev_close_input_stream(struct audio_hw_device * dev,struct audio_stream_in * stream)1809 static void adev_close_input_stream(struct audio_hw_device* dev,
1810 struct audio_stream_in* stream) {
1811 struct ha_audio_device* ha_dev = (struct ha_audio_device*)dev;
1812 struct ha_stream_in* in = (struct ha_stream_in*)stream;
1813
1814 std::lock_guard<std::recursive_mutex> lock(*ha_dev->mutex);
1815 {
1816 std::lock_guard<std::recursive_mutex> lock(*in->common.mutex);
1817 const ha_state_t state = in->common.state;
1818 INFO("closing input (state %d)", (int)state);
1819
1820 if ((state == AUDIO_HA_STATE_STARTED) || (state == AUDIO_HA_STATE_STOPPING))
1821 stop_audio_datapath(&in->common);
1822
1823 skt_disconnect(in->common.ctrl_fd);
1824 in->common.ctrl_fd = AUDIO_SKT_DISCONNECTED;
1825 }
1826 ha_stream_common_destroy(&in->common);
1827 free(stream);
1828 ha_dev->input = NULL;
1829
1830 DEBUG("done");
1831 }
1832
adev_dump(UNUSED_ATTR const audio_hw_device_t * device,UNUSED_ATTR int fd)1833 static int adev_dump(UNUSED_ATTR const audio_hw_device_t* device,
1834 UNUSED_ATTR int fd) {
1835 FNLOG();
1836
1837 return 0;
1838 }
1839
adev_close(hw_device_t * device)1840 static int adev_close(hw_device_t* device) {
1841 struct ha_audio_device* ha_dev = (struct ha_audio_device*)device;
1842 FNLOG();
1843
1844 delete ha_dev->mutex;
1845 ha_dev->mutex = nullptr;
1846 free(device);
1847 return 0;
1848 }
1849
adev_open(const hw_module_t * module,const char * name,hw_device_t ** device)1850 static int adev_open(const hw_module_t* module, const char* name,
1851 hw_device_t** device) {
1852 struct ha_audio_device* adev;
1853
1854 INFO(" adev_open in ha_hw module");
1855 FNLOG();
1856
1857 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) {
1858 ERROR("interface %s not matching [%s]", name, AUDIO_HARDWARE_INTERFACE);
1859 return -EINVAL;
1860 }
1861
1862 adev = (struct ha_audio_device*)calloc(1, sizeof(struct ha_audio_device));
1863
1864 if (!adev) return -ENOMEM;
1865
1866 adev->mutex = new std::recursive_mutex;
1867
1868 adev->device.common.tag = HARDWARE_DEVICE_TAG;
1869 adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
1870 adev->device.common.module = (struct hw_module_t*)module;
1871 adev->device.common.close = adev_close;
1872
1873 adev->device.init_check = adev_init_check;
1874 adev->device.set_voice_volume = adev_set_voice_volume;
1875 adev->device.set_master_volume = adev_set_master_volume;
1876 adev->device.set_mode = adev_set_mode;
1877 adev->device.set_mic_mute = adev_set_mic_mute;
1878 adev->device.get_mic_mute = adev_get_mic_mute;
1879 adev->device.set_parameters = adev_set_parameters;
1880 adev->device.get_parameters = adev_get_parameters;
1881 adev->device.get_input_buffer_size = adev_get_input_buffer_size;
1882 adev->device.open_output_stream = adev_open_output_stream;
1883 adev->device.close_output_stream = adev_close_output_stream;
1884 adev->device.open_input_stream = adev_open_input_stream;
1885 adev->device.close_input_stream = adev_close_input_stream;
1886 adev->device.dump = adev_dump;
1887
1888 adev->output = NULL;
1889
1890 *device = &adev->device.common;
1891
1892 return 0;
1893 }
1894
1895 static struct hw_module_methods_t hal_module_methods = {
1896 .open = adev_open,
1897 };
1898
1899 __attribute__((
1900 visibility("default"))) struct audio_module HAL_MODULE_INFO_SYM = {
1901 .common =
1902 {
1903 .tag = HARDWARE_MODULE_TAG,
1904 .version_major = 1,
1905 .version_minor = 0,
1906 .id = AUDIO_HARDWARE_MODULE_ID,
1907 .name = "Hearing Aid Audio HW HAL",
1908 .author = "The Android Open Source Project",
1909 .methods = &hal_module_methods,
1910 },
1911 };
1912