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