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