blob: 1a9421b6f741e0963e5bb9afec204af677d1f472 [file] [log] [blame]
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001/*
2 * Copyright (C) 2013-2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "a2dp_offload"
18/*#define LOG_NDEBUG 0*/
19#define LOG_NDDEBUG 0
20
21#include <dlfcn.h>
22#include <errno.h>
Steven Morelanda5352422018-03-27 09:32:08 -070023#include <pthread.h>
Aniket Kumar Lata26483012018-01-31 20:21:42 -080024#include <stdlib.h>
25
26#include <cutils/log.h>
27#include <cutils/str_parms.h>
28#include <cutils/properties.h>
29#include <hardware/audio.h>
30
31#include "audio_hw.h"
32#include "audio_extn.h"
33#include "platform_api.h"
34
35#ifdef A2DP_OFFLOAD_ENABLED
36#define AUDIO_PARAMETER_A2DP_STARTED "A2dpStarted"
37#define BT_IPC_LIB_NAME "libbthost_if.so"
38
39// Media format definitions
40#define ENC_MEDIA_FMT_AAC 0x00010DA6
41#define ENC_MEDIA_FMT_APTX 0x000131ff
42#define ENC_MEDIA_FMT_APTX_HD 0x00013200
43#define ENC_MEDIA_FMT_LDAC 0x00013224
44#define ENC_MEDIA_FMT_SBC 0x00010BF2
45#define ENC_MEDIA_FMT_NONE 0
46#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
47#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
48#define MEDIA_FMT_AAC_AOT_LC 2
49#define MEDIA_FMT_AAC_AOT_SBR 5
50#define MEDIA_FMT_AAC_AOT_PS 29
51#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
52#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
53#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
54#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
55
56// PCM channels
57#define PCM_CHANNEL_L 1
58#define PCM_CHANNEL_R 2
59#define PCM_CHANNEL_C 3
60
61// Mixer controls sent to ALSA
62#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
63#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
64#define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode"
65#define MIXER_SAMPLE_RATE "BT SampleRate"
66#define MIXER_AFE_IN_CHANNELS "AFE Input Channels"
67
68// Encoder format strings
69#define ENC_FMT_AAC "aac"
70#define ENC_FMT_APTX "aptx"
71#define ENC_FMT_APTXHD "aptxhd"
72#define ENC_FMT_LDAC "ldac"
73#define ENC_FMT_SBC "sbc"
74
75// System properties used for A2DP Offload
76#define SYSPROP_A2DP_OFFLOAD_ENABLED "persist.vendor.bluetooth.a2dp_offload.enable"
77#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
78
79// Default encoder bit width
80#define DEFAULT_ENCODER_BIT_FORMAT 16
81
82// Default encoder latency
83#define DEFAULT_ENCODER_LATENCY 200
84
85// Encoder latency offset for codecs supported
86#define ENCODER_LATENCY_AAC 70
87#define ENCODER_LATENCY_APTX 40
88#define ENCODER_LATENCY_APTX_HD 20
89#define ENCODER_LATENCY_LDAC 40
90#define ENCODER_LATENCY_SBC 10
91
92// Default A2DP sink latency offset
93#define DEFAULT_SINK_LATENCY_AAC 180
94#define DEFAULT_SINK_LATENCY_APTX 160
95#define DEFAULT_SINK_LATENCY_APTX_HD 180
96#define DEFAULT_SINK_LATENCY_LDAC 180
97#define DEFAULT_SINK_LATENCY_SBC 140
98
99/*
100 * Below enum values are extended from audio-base.h to
101 * keep encoder codec type local to bthost_ipc
102 * and audio_hal as these are intended only for handshake
103 * between IPC lib and Audio HAL.
104 */
105typedef enum {
106 ENC_CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
107 ENC_CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
108 ENC_CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
109 ENC_CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
110 ENC_CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
111 ENC_CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
112} enc_codec_t;
113
114typedef int (*audio_stream_open_t)(void);
115typedef int (*audio_stream_close_t)(void);
116typedef int (*audio_stream_start_t)(void);
117typedef int (*audio_stream_stop_t)(void);
118typedef int (*audio_stream_suspend_t)(void);
119typedef void (*audio_handoff_triggered_t)(void);
120typedef void (*clear_a2dp_suspend_flag_t)(void);
121typedef void * (*audio_get_codec_config_t)(uint8_t *multicast_status, uint8_t *num_dev,
122 enc_codec_t *codec_type);
123typedef int (*audio_check_a2dp_ready_t)(void);
124typedef int (*audio_is_scrambling_enabled_t)(void);
125
126enum A2DP_STATE {
127 A2DP_STATE_CONNECTED,
128 A2DP_STATE_STARTED,
129 A2DP_STATE_STOPPED,
130 A2DP_STATE_DISCONNECTED,
131};
132
133/* Data structure used to:
134 * - Update the A2DP state machine
135 * - Communicate with the libbthost_if.so IPC library
136 * - Store DSP encoder configuration information
137 */
138struct a2dp_data {
139 /* Audio device handle */
140 struct audio_device *adev;
141 /* Bluetooth IPC library handle */
142 void *bt_lib_handle;
143 /* Open A2DP audio stream. Initialize audio datapath */
144 audio_stream_open_t audio_stream_open;
145 /* Close A2DP audio stream */
146 audio_stream_close_t audio_stream_close;
147 /* Start A2DP audio stream. Start audio datapath */
148 audio_stream_start_t audio_stream_start;
149 /* Stop A2DP audio stream */
150 audio_stream_stop_t audio_stream_stop;
151 /* Suspend A2DP audio stream */
152 audio_stream_suspend_t audio_stream_suspend;
153 /* Notify Bluetooth IPC library of handoff being triggered */
154 audio_handoff_triggered_t audio_handoff_triggered;
155 /* Clear A2DP suspend flag in Bluetooth IPC library */
156 clear_a2dp_suspend_flag_t clear_a2dp_suspend_flag;
157 /* Get codec configuration from Bluetooth stack via
158 * Bluetooth IPC library */
159 audio_get_codec_config_t audio_get_codec_config;
160 /* Check if A2DP is ready */
161 audio_check_a2dp_ready_t audio_check_a2dp_ready;
162 /* Check if scrambling is enabled on BTSoC */
163 audio_is_scrambling_enabled_t audio_is_scrambling_enabled;
164 /* Internal A2DP state identifier */
165 enum A2DP_STATE bt_state;
166 /* A2DP codec type configured */
167 enc_codec_t bt_encoder_format;
168 /* Sampling rate configured with A2DP encoder on DSP */
169 uint32_t enc_sampling_rate;
170 /* Channel configuration of A2DP on DSP */
171 uint32_t enc_channels;
172 /* Flag to denote whether A2DP audio datapath has started */
173 bool a2dp_started;
174 /* Flag to denote whether A2DP audio datapath is suspended */
175 bool a2dp_suspended;
176 /* Number of active sessions on A2DP output */
177 int a2dp_total_active_session_request;
178 /* Flag to denote whether A2DP offload is supported */
179 bool is_a2dp_offload_supported;
180 /* Flag to denote whether codec reconfiguration/soft handoff is in progress */
181 bool is_handoff_in_progress;
182 /* Flag to denote whether APTX Dual Mono encoder is supported */
183 bool is_aptx_dual_mono_supported;
184};
185
186struct a2dp_data a2dp;
187
188/* START of DSP configurable structures
189 * These values should match with DSP interface defintion
190 */
191
192/* AAC encoder configuration structure. */
193typedef struct aac_enc_cfg_t aac_enc_cfg_t;
194
195struct aac_enc_cfg_t {
196 /* Encoder media format for AAC */
197 uint32_t enc_format;
198
199 /* Encoding rate in bits per second */
200 uint32_t bit_rate;
201
202 /* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS */
203 uint32_t enc_mode;
204
205 /* supported aac_fmt_flag are ADTS/RAW */
206 uint16_t aac_fmt_flag;
207
208 /* supported channel_cfg are Native mode, Mono , Stereo */
209 uint16_t channel_cfg;
210
211 /* Number of samples per second */
212 uint32_t sample_rate;
213} __attribute__ ((packed));
214
215/* SBC encoder configuration structure. */
216typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
217
218struct sbc_enc_cfg_t {
219 /* Encoder media format for SBC */
220 uint32_t enc_format;
221
222 /* supported num_subbands are 4/8 */
223 uint32_t num_subbands;
224
225 /* supported blk_len are 4, 8, 12, 16 */
226 uint32_t blk_len;
227
228 /* supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO */
229 uint32_t channel_mode;
230
231 /* supported alloc_method are LOUNDNESS/SNR */
232 uint32_t alloc_method;
233
234 /* supported bit_rate for mono channel is max 320kbps
235 * supported bit rate for stereo channel is max 512 kbps */
236 uint32_t bit_rate;
237
238 /* Number of samples per second */
239 uint32_t sample_rate;
240} __attribute__ ((packed));
241
242struct custom_enc_cfg_t {
243 /* Custom encoder media format */
244 uint32_t enc_format;
245
246 /* Number of samples per second */
247 uint32_t sample_rate;
248
249 /* supported num_channels are Mono/Stereo */
250 uint16_t num_channels;
251
252 /* Reserved for future enhancement */
253 uint16_t reserved;
254
255 /* supported channel_mapping for mono is CHANNEL_C
256 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R */
257 uint8_t channel_mapping[8];
258
259 /* Reserved for future enhancement */
260 uint32_t custom_size;
261} __attribute__ ((packed));
262
263struct aptx_v2_enc_cfg_ext_t {
264/* sync_mode introduced with APTX V2 libraries
265 * sync mode: 0x0 = stereo sync mode
266 * 0x01 = dual mono sync mode
267 * 0x02 = dual mono with no sync on either L or R codewords
268 */
269 uint32_t sync_mode;
270} __attribute__ ((packed));
271
272/* APTX struct for combining custom enc and V2 members */
273struct aptx_enc_cfg_t {
274 struct custom_enc_cfg_t custom_cfg;
275 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
276} __attribute__ ((packed));
277
278struct ldac_specific_enc_cfg_t {
279 /*
280 * This is used to calculate the encoder output
281 * bytes per frame (i.e. bytes per packet).
282 * Bit rate also configures the EQMID.
283 * The min bit rate 303000 bps is calculated for
284 * 44.1 kHz and 88.2 KHz sampling frequencies with
285 * Mobile use Quality.
286 * The max bit rate of 990000 bps is calculated for
287 * 96kHz and 48 KHz with High Quality
288 * @Range(in bits per second)
289 * 303000 for Mobile use Quality
290 * 606000 for standard Quality
291 * 909000 for High Quality
292 */
293 uint32_t bit_rate;
294
295 /*
296 * The channel setting information for LDAC specification
297 * of Bluetooth A2DP which is determined by SRC and SNK
298 * devices in Bluetooth transmission.
299 * @Range:
300 * 0 for native mode
301 * 4 for mono
302 * 2 for dual channel
303 * 1 for stereo
304 */
305 uint16_t channel_mode;
306
307 /*
308 * Maximum Transmission Unit (MTU).
309 * The minimum MTU that a L2CAP implementation for LDAC shall
310 * support is 679 bytes, because LDAC is optimized with 2-DH5
311 * packet as its target.
312 * @Range : 679
313 * @Default: 679 for LDACBT_MTU_2DH5
314 */
315 uint16_t mtu;
316} __attribute__ ((packed));
317
318/* LDAC struct for combining custom enc and standard members */
319struct ldac_enc_cfg_t {
320 struct custom_enc_cfg_t custom_cfg;
321 struct ldac_specific_enc_cfg_t ldac_cfg;
322} __attribute__ ((packed));
323
324/* Information about Bluetooth SBC encoder configuration
325 * This data is used between audio HAL module and
326 * Bluetooth IPC library to configure DSP encoder
327 */
328typedef struct {
329 uint32_t subband; /* 4, 8 */
330 uint32_t blk_len; /* 4, 8, 12, 16 */
331 uint16_t sampling_rate; /* 44.1khz, 48khz */
332 uint8_t channels; /* 0(Mono), 1(Dual_mono), 2(Stereo), 3(JS) */
333 uint8_t alloc; /* 0(Loudness), 1(SNR) */
334 uint8_t min_bitpool; /* 2 */
335 uint8_t max_bitpool; /* 53(44.1khz), 51 (48khz) */
336 uint32_t bitrate; /* 320kbps to 512kbps */
337 uint32_t bits_per_sample; /* 16 bit */
338} audio_sbc_encoder_config;
339
340/* Information about Bluetooth APTX encoder configuration
341 * This data is used between audio HAL module and
342 * Bluetooth IPC library to configure DSP encoder
343 */
344typedef struct {
345 uint16_t sampling_rate;
346 uint8_t channels;
347 uint32_t bitrate;
348 uint32_t bits_per_sample;
349} audio_aptx_default_config;
350
351typedef struct {
352 uint16_t sampling_rate;
353 uint8_t channels;
354 uint32_t bitrate;
355 uint32_t sync_mode;
356} audio_aptx_dual_mono_config;
357
358typedef union {
359 audio_aptx_default_config *default_cfg;
360 audio_aptx_dual_mono_config *dual_mono_cfg;
361} audio_aptx_encoder_config;
362
363/* Information about Bluetooth AAC encoder configuration
364 * This data is used between audio HAL module and
365 * Bluetooth IPC library to configure DSP encoder
366 */
367typedef struct {
368 uint32_t enc_mode; /* LC, SBR, PS */
369 uint16_t format_flag; /* RAW, ADTS */
370 uint16_t channels; /* 1-Mono, 2-Stereo */
371 uint32_t sampling_rate;
372 uint32_t bitrate;
373 uint32_t bits_per_sample;
374} audio_aac_encoder_config;
375
376/* Information about Bluetooth LDAC encoder configuration
377 * This data is used between audio HAL module and
378 * Bluetooth IPC library to configure DSP encoder
379 */
380typedef struct {
381 uint32_t sampling_rate; /* 44100, 48000, 88200, 96000 */
382 uint32_t bit_rate; /* 303000, 606000, 909000 (in bits per second) */
383 uint16_t channel_mode; /* 0, 4, 2, 1 */
384 uint16_t mtu;
385 uint32_t bits_per_sample; /* 16, 24, 32 (bits) */
386} audio_ldac_encoder_config;
387
388/*********** END of DSP configurable structures ********************/
389
390static void a2dp_common_init()
391{
392 a2dp.a2dp_started = false;
393 a2dp.a2dp_total_active_session_request = 0;
394 a2dp.a2dp_suspended = false;
395 a2dp.bt_encoder_format = ENC_CODEC_TYPE_INVALID;
396 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
397}
398
399static void update_offload_codec_support()
400{
401 char value[PROPERTY_VALUE_MAX] = {'\0'};
402
403 a2dp.is_a2dp_offload_supported =
404 property_get_bool(SYSPROP_A2DP_OFFLOAD_ENABLED, false);
405 ALOGD("%s: A2DP offload supported = %s", __func__, value);
406}
407
408/* API to open Bluetooth IPC library to start IPC communication */
409static int open_a2dp_output()
410{
411 int ret = 0;
412
413 ALOGD("%s: Open A2DP output start", __func__);
414
415 if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
416 ALOGD("%s: Called A2DP open with improper state, Ignoring request state %d",
417 __func__, a2dp.bt_state);
418 return -ENOSYS;
419 }
420
421 if (a2dp.bt_lib_handle == NULL) {
422 ALOGD("%s: Requesting for Bluetooth IPC lib handle", __func__);
423 a2dp.bt_lib_handle = dlopen(BT_IPC_LIB_NAME, RTLD_NOW);
424
425 if (a2dp.bt_lib_handle == NULL) {
426 ret = -errno;
427 ALOGE("%s: DLOPEN failed for %s errno %d strerror %s", __func__,
428 BT_IPC_LIB_NAME, errno, strerror(errno));
429 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
430 return ret;
431 } else {
432 a2dp.audio_stream_open = (audio_stream_open_t)
433 dlsym(a2dp.bt_lib_handle, "audio_stream_open");
434 a2dp.audio_stream_start = (audio_stream_start_t)
435 dlsym(a2dp.bt_lib_handle, "audio_stream_start");
436 a2dp.audio_get_codec_config = (audio_get_codec_config_t)
437 dlsym(a2dp.bt_lib_handle, "audio_get_codec_config");
438 a2dp.audio_stream_suspend = (audio_stream_suspend_t)
439 dlsym(a2dp.bt_lib_handle, "audio_stream_suspend");
440 a2dp.audio_handoff_triggered = (audio_handoff_triggered_t)
441 dlsym(a2dp.bt_lib_handle, "audio_handoff_triggered");
442 a2dp.clear_a2dp_suspend_flag = (clear_a2dp_suspend_flag_t)
443 dlsym(a2dp.bt_lib_handle, "clear_a2dp_suspend_flag");
444 a2dp.audio_stream_stop = (audio_stream_stop_t)
445 dlsym(a2dp.bt_lib_handle, "audio_stream_stop");
446 a2dp.audio_stream_close = (audio_stream_close_t)
447 dlsym(a2dp.bt_lib_handle, "audio_stream_close");
448 a2dp.audio_check_a2dp_ready = (audio_check_a2dp_ready_t)
449 dlsym(a2dp.bt_lib_handle,"audio_check_a2dp_ready");
450 a2dp.audio_is_scrambling_enabled = (audio_is_scrambling_enabled_t)
451 dlsym(a2dp.bt_lib_handle,"audio_is_scrambling_enabled");
452 }
453 }
454
455 if (a2dp.bt_lib_handle && a2dp.audio_stream_open) {
456 ALOGD("%s: calling Bluetooth stream open", __func__);
457 ret = a2dp.audio_stream_open();
458 if (ret != 0) {
459 ALOGE("%s: Failed to open output stream for A2DP: status %d", __func__, ret);
460 dlclose(a2dp.bt_lib_handle);
461 a2dp.bt_lib_handle = NULL;
462 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
463 return ret;
464 }
465 a2dp.bt_state = A2DP_STATE_CONNECTED;
466 } else {
467 ALOGE("%s: A2DP handle is not identified, Ignoring open request", __func__);
468 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
469 return -ENOSYS;
470 }
471
472 return ret;
473}
474
475static int close_a2dp_output()
476{
477 ALOGV("%s\n",__func__);
478 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_close)) {
479 ALOGE("%s: A2DP handle is not identified, Ignoring close request", __func__);
480 return -ENOSYS;
481 }
482 if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
483 ALOGD("%s: calling Bluetooth stream close", __func__);
484 if (a2dp.audio_stream_close() == false)
485 ALOGE("%s: failed close A2DP control path from Bluetooth IPC library", __func__);
486 }
487 a2dp_common_init();
488 a2dp.enc_sampling_rate = 0;
489 a2dp.enc_channels = 0;
490
491 return 0;
492}
493
494static int a2dp_check_and_set_scrambler()
495{
496 bool scrambler_mode = false;
497 struct mixer_ctl *ctrl_scrambler_mode = NULL;
498 int ret = 0;
499 if (a2dp.audio_is_scrambling_enabled && (a2dp.bt_state != A2DP_STATE_DISCONNECTED))
500 scrambler_mode = a2dp.audio_is_scrambling_enabled();
501
502 // Scrambling needs to be enabled in DSP if scrambler mode is set
503 // disable scrambling not required
504 if (scrambler_mode) {
505 // enable scrambler in dsp
506 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
507 MIXER_SCRAMBLER_MODE);
508 if (!ctrl_scrambler_mode) {
509 ALOGE("%s: ERROR scrambler mode mixer control not identifed", __func__);
510 return -ENOSYS;
511 } else {
512 ret = mixer_ctl_set_value(ctrl_scrambler_mode, 0, true);
513 if (ret != 0) {
514 ALOGE("%s: Could not set scrambler mode", __func__);
515 return ret;
516 }
517 }
518 }
519 return 0;
520}
521
522static void a2dp_set_backend_cfg()
523{
524 const char *rate_str = NULL, *in_channels = NULL;
525 uint32_t sampling_rate = a2dp.enc_sampling_rate;
526 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL;
527
528 // For LDAC encoder open slimbus port at 96Khz for 48Khz input
529 // and 88.2Khz for 44.1Khz input.
530 if ((a2dp.bt_encoder_format == ENC_CODEC_TYPE_LDAC) &&
531 (sampling_rate == 48000 || sampling_rate == 44100 )) {
532 sampling_rate = sampling_rate * 2;
533 }
534 // Configure backend sampling rate
535 switch (sampling_rate) {
536 case 44100:
537 rate_str = "KHZ_44P1";
538 break;
539 case 48000:
540 rate_str = "KHZ_48";
541 break;
542 case 88200:
543 rate_str = "KHZ_88P2";
544 break;
545 case 96000:
546 rate_str = "KHZ_96";
547 break;
548 default:
549 rate_str = "KHZ_48";
550 break;
551 }
552
553 ALOGD("%s: set backend sample rate =%s", __func__, rate_str);
554 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
555 MIXER_SAMPLE_RATE);
556 if (!ctl_sample_rate) {
557 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
558 return;
559 } else {
560 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
561 ALOGE("%s: Failed to set backend sample rate =%s", __func__, rate_str);
562 return;
563 }
564 }
565
566 // Configure AFE input channels
567 switch (a2dp.enc_channels) {
568 case 1:
569 in_channels = "One";
570 break;
571 case 2:
572 default:
573 in_channels = "Two";
574 break;
575 }
576
577 ALOGD("%s: set AFE input channels =%d", __func__, a2dp.enc_channels);
578 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
579 MIXER_AFE_IN_CHANNELS);
580 if (!ctrl_in_channels) {
581 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
582 return;
583 } else {
584 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
585 ALOGE("%s: Failed to set AFE in channels =%d", __func__, a2dp.enc_channels);
586 return;
587 }
588 }
589}
590
591static int a2dp_set_bit_format(uint32_t enc_bit_format)
592{
593 const char *bit_format = NULL;
594 struct mixer_ctl *ctrl_bit_format = NULL;
595
596 // Configure AFE Input Bit Format
597 switch (enc_bit_format) {
598 case 32:
599 bit_format = "S32_LE";
600 break;
601 case 24:
602 bit_format = "S24_LE";
603 break;
604 case 16:
605 default:
606 bit_format = "S16_LE";
607 break;
608 }
609
610 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
611 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
612 MIXER_ENC_BIT_FORMAT);
613 if (!ctrl_bit_format) {
614 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
615 return -ENOSYS;
616 }
617 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
618 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
619 return -ENOSYS;
620 }
621 return 0;
622}
623
624static void a2dp_reset_backend_cfg()
625{
626 const char *rate_str = "KHZ_8", *in_channels = "Zero";
627 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL;
628
629 // Reset backend sampling rate
630 ALOGD("%s: reset backend sample rate = %s", __func__, rate_str);
631 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
632 MIXER_SAMPLE_RATE);
633 if (!ctl_sample_rate) {
634 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
635 return;
636 } else {
637 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
638 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
639 return;
640 }
641 }
642
643 // Reset AFE input channels
644 ALOGD("%s: reset AFE input channels = %s", __func__, in_channels);
645 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
646 MIXER_AFE_IN_CHANNELS);
647 if (!ctrl_in_channels) {
648 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
649 return;
650 } else {
651 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
652 ALOGE("%s: Failed to reset AFE in channels =%d", __func__, a2dp.enc_channels);
653 return;
654 }
655 }
656}
657
658/* API to configure SBC DSP encoder */
659static bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
660{
661 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
662 struct sbc_enc_cfg_t sbc_dsp_cfg;
663 bool is_configured = false;
664 int ret = 0;
665
666 if (sbc_bt_cfg == NULL) {
667 ALOGE("%s: Failed to get SBC encoder config from BT", __func__);
668 return false;
669 }
670
671 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
672 if (!ctl_enc_data) {
673 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
674 is_configured = false;
675 goto exit;
676 }
677 memset(&sbc_dsp_cfg, 0x0, sizeof(sbc_dsp_cfg));
678 sbc_dsp_cfg.enc_format = ENC_MEDIA_FMT_SBC;
679 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
680 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
681 switch (sbc_bt_cfg->channels) {
682 case 0:
683 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
684 break;
685 case 1:
686 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
687 break;
688 case 3:
689 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
690 break;
691 case 2:
692 default:
693 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
694 break;
695 }
696 if (sbc_bt_cfg->alloc)
697 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
698 else
699 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
700 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
701 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
702 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
703 sizeof(sbc_dsp_cfg));
704 if (ret != 0) {
705 ALOGE("%s: failed to set SBC encoder config", __func__);
706 is_configured = false;
707 goto exit;
708 }
709 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
710 if (ret != 0) {
711 is_configured = false;
712 goto exit;
713 }
714 is_configured = true;
715 a2dp.bt_encoder_format = ENC_CODEC_TYPE_SBC;
716 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
717
718 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
719 a2dp.enc_channels = 1;
720 else
721 a2dp.enc_channels = 2;
722
723 ALOGV("%s: Successfully updated SBC enc format with sampling rate: %d channel mode:%d",
724 __func__, sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
725exit:
726 return is_configured;
727}
728
729/* API to configure APTX DSP encoder */
730static bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
731{
732 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
733 int mixer_size;
734 bool is_configured = false;
735 int ret = 0;
736 struct aptx_enc_cfg_t aptx_dsp_cfg;
737 mixer_size = sizeof(aptx_dsp_cfg);
738
739 if (aptx_bt_cfg == NULL) {
740 ALOGE("%s: Failed to get APTX encoder config from BT", __func__);
741 return false;
742 }
743
744 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
745 if (!ctl_enc_data) {
746 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
747 is_configured = false;
748 goto exit;
749 }
750
751 memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg));
752 aptx_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_APTX;
753
754 if (!a2dp.is_aptx_dual_mono_supported) {
755 aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
756 aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
757 } else {
758 aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
759 aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
760 aptx_dsp_cfg.aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
761 }
762
763 switch (aptx_dsp_cfg.custom_cfg.num_channels) {
764 case 1:
765 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
766 break;
767 case 2:
768 default:
769 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
770 aptx_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
771 break;
772 }
773 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
774 mixer_size);
775 if (ret != 0) {
776 ALOGE("%s: Failed to set APTX encoder config", __func__);
777 is_configured = false;
778 goto exit;
779 }
780 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
781 if (ret != 0) {
782 is_configured = false;
783 goto exit;
784 }
785 is_configured = true;
786 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX;
787 a2dp.enc_channels = aptx_dsp_cfg.custom_cfg.num_channels;
788 if (!a2dp.is_aptx_dual_mono_supported) {
789 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
790 ALOGV("%s: Successfully updated APTX enc format with sampling rate: %d \
791 channels:%d", __func__, aptx_dsp_cfg.custom_cfg.sample_rate,
792 aptx_dsp_cfg.custom_cfg.num_channels);
793 } else {
794 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
795 ALOGV("%s: Successfully updated APTX dual mono enc format with \
796 sampling rate: %d channels:%d sync mode %d", __func__,
797 aptx_dsp_cfg.custom_cfg.sample_rate,
798 aptx_dsp_cfg.custom_cfg.num_channels,
799 aptx_dsp_cfg.aptx_v2_cfg.sync_mode);
800 }
801
802exit:
803 return is_configured;
804}
805
806/* API to configure APTX HD DSP encoder
807 */
808static bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
809{
810 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
811 struct custom_enc_cfg_t aptx_dsp_cfg;
812 bool is_configured = false;
813 int ret = 0;
814
815 if (aptx_bt_cfg == NULL) {
816 ALOGE("%s: Failed to get APTX HD encoder config from BT", __func__);
817 return false;
818 }
819
820 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
821 if (!ctl_enc_data) {
822 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
823 is_configured = false;
824 goto exit;
825 }
826
827 memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg));
828 aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX_HD;
829 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
830 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
831 switch (aptx_dsp_cfg.num_channels) {
832 case 1:
833 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
834 break;
835 case 2:
836 default:
837 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
838 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
839 break;
840 }
841 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
842 sizeof(aptx_dsp_cfg));
843 if (ret != 0) {
844 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
845 is_configured = false;
846 goto exit;
847 }
848 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
849 if (ret != 0) {
850 is_configured = false;
851 goto exit;
852 }
853 is_configured = true;
854 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX_HD;
855 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
856 a2dp.enc_channels = aptx_bt_cfg->channels;
857 ALOGV("%s: Successfully updated APTX HD encformat with sampling rate: %d channels:%d",
858 __func__, aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
859exit:
860 return is_configured;
861}
862
863/* API to configure AAC DSP encoder */
864static bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
865{
866 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
867 struct aac_enc_cfg_t aac_dsp_cfg;
868 bool is_configured = false;
869 int ret = 0;
870
871 if (aac_bt_cfg == NULL) {
872 ALOGE("%s: Failed to get AAC encoder config from BT", __func__);
873 return false;
874 }
875
876 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
877 if (!ctl_enc_data) {
878 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
879 is_configured = false;
880 goto exit;
881 }
882 memset(&aac_dsp_cfg, 0x0, sizeof(aac_dsp_cfg));
883 aac_dsp_cfg.enc_format = ENC_MEDIA_FMT_AAC;
884 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
885 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
886 switch (aac_bt_cfg->enc_mode) {
887 case 0:
888 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
889 break;
890 case 2:
891 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
892 break;
893 case 1:
894 default:
895 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
896 break;
897 }
898 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
899 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
900 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
901 sizeof(aac_dsp_cfg));
902 if (ret != 0) {
903 ALOGE("%s: failed to set AAC encoder config", __func__);
904 is_configured = false;
905 goto exit;
906 }
907 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
908 if (ret != 0) {
909 is_configured = false;
910 goto exit;
911 }
912 is_configured = true;
913 a2dp.bt_encoder_format = ENC_CODEC_TYPE_AAC;
914 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
915 a2dp.enc_channels = aac_bt_cfg->channels;;
916 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
917 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
918exit:
919 return is_configured;
920}
921
922static bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
923{
924 struct mixer_ctl *ldac_enc_data = NULL, *ctrl_bit_format = NULL;
925 struct ldac_enc_cfg_t ldac_dsp_cfg;
926 bool is_configured = false;
927 int ret = 0;
928
929 if (ldac_bt_cfg == NULL) {
930 ALOGE("%s: Failed to get LDAC encoder config from BT", __func__);
931 return false;
932 }
933
934 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
935 if (!ldac_enc_data) {
936 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
937 is_configured = false;
938 goto exit;
939 }
940 memset(&ldac_dsp_cfg, 0x0, sizeof(ldac_dsp_cfg));
941
942 ldac_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_LDAC;
943 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
944 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
945 switch (ldac_dsp_cfg.ldac_cfg.channel_mode) {
946 case 4:
947 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
948 ldac_dsp_cfg.custom_cfg.num_channels = 1;
949 break;
950 case 2:
951 case 1:
952 default:
953 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
954 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
955 ldac_dsp_cfg.custom_cfg.num_channels = 2;
956 break;
957 }
958
959 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(ldac_dsp_cfg);
960 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
961 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
962 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
963 sizeof(ldac_dsp_cfg));
964 if (ret != 0) {
965 ALOGE("%s: Failed to set LDAC encoder config", __func__);
966 is_configured = false;
967 goto exit;
968 }
969 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
970 if (ret != 0) {
971 is_configured = false;
972 goto exit;
973 }
974 is_configured = true;
975 a2dp.bt_encoder_format = ENC_CODEC_TYPE_LDAC;
976 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
977 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
978 ALOGV("%s: Successfully updated LDAC encformat with sampling rate: %d channels:%d",
979 __func__, ldac_dsp_cfg.custom_cfg.sample_rate,
980 ldac_dsp_cfg.custom_cfg.num_channels);
981exit:
982 return is_configured;
983}
984
985bool configure_a2dp_encoder_format()
986{
987 void *codec_info = NULL;
988 uint8_t multi_cast = 0, num_dev = 1;
989 enc_codec_t codec_type = ENC_CODEC_TYPE_INVALID;
990 bool is_configured = false;
991 audio_aptx_encoder_config aptx_encoder_cfg;
992
993 if (!a2dp.audio_get_codec_config) {
994 ALOGE("%s: A2DP handle is not identified, ignoring A2DP encoder config", __func__);
995 return false;
996 }
997 ALOGD("%s: start", __func__);
998 codec_info = a2dp.audio_get_codec_config(&multi_cast, &num_dev,
999 &codec_type);
1000
1001 switch (codec_type) {
1002 case ENC_CODEC_TYPE_SBC:
1003 ALOGD("%s: Received SBC encoder supported Bluetooth device", __func__);
1004 is_configured =
1005 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
1006 break;
1007 case ENC_CODEC_TYPE_APTX:
1008 ALOGD("%s: Received APTX encoder supported Bluetooth device", __func__);
1009 a2dp.is_aptx_dual_mono_supported = false;
1010 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
1011 is_configured =
1012 configure_aptx_enc_format(&aptx_encoder_cfg);
1013 break;
1014 case ENC_CODEC_TYPE_APTX_HD:
1015 ALOGD("%s: Received APTX HD encoder supported Bluetooth device", __func__);
1016 is_configured =
1017 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
1018 break;
1019 case ENC_CODEC_TYPE_AAC:
1020 ALOGD("%s: Received AAC encoder supported Bluetooth device", __func__);
1021 is_configured =
1022 configure_aac_enc_format((audio_aac_encoder_config *)codec_info);
1023 break;
1024 case ENC_CODEC_TYPE_LDAC:
1025 ALOGD("%s: Received LDAC encoder supported Bluetooth device", __func__);
1026 is_configured =
1027 configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info);
1028 break;
1029 default:
1030 ALOGD("%s: Received unsupported encoder format", __func__);
1031 is_configured = false;
1032 break;
1033 }
1034 return is_configured;
1035}
1036
1037int audio_extn_a2dp_start_playback()
1038{
1039 int ret = 0;
1040
1041 ALOGD("%s: start", __func__);
1042
1043 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_start
1044 && a2dp.audio_get_codec_config)) {
1045 ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__);
1046 return -ENOSYS;
1047 }
1048
1049 if (a2dp.a2dp_suspended) {
1050 // session will be restarted after suspend completion
1051 ALOGD("%s: A2DP start requested during suspend state", __func__);
1052 return -ENOSYS;
1053 }
1054
1055 if (!a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
1056 ALOGD("%s: calling Bluetooth module stream start", __func__);
1057 /* This call indicates Bluetooth IPC lib to start playback */
1058 ret = a2dp.audio_stream_start();
1059 ALOGE("%s: Bluetooth controller start return = %d", __func__, ret);
1060 if (ret != 0 ) {
1061 ALOGE("%s: Bluetooth controller start failed", __func__);
1062 a2dp.a2dp_started = false;
1063 } else {
1064 if (configure_a2dp_encoder_format() == true) {
1065 a2dp.a2dp_started = true;
1066 ret = 0;
1067 ALOGD("%s: Start playback successful to Bluetooth IPC library", __func__);
1068 } else {
1069 ALOGD("%s: unable to configure DSP encoder", __func__);
1070 a2dp.a2dp_started = false;
1071 ret = -ETIMEDOUT;
1072 }
1073 }
1074 }
1075
1076 if (a2dp.a2dp_started) {
1077 a2dp.a2dp_total_active_session_request++;
1078 a2dp_check_and_set_scrambler();
1079 a2dp_set_backend_cfg();
1080 }
1081
1082 ALOGD("%s: start A2DP playback total active sessions :%d", __func__,
1083 a2dp.a2dp_total_active_session_request);
1084 return ret;
1085}
1086
1087static int reset_a2dp_enc_config_params()
1088{
1089 int ret = 0;
1090
1091 struct mixer_ctl *ctl_enc_config, *ctrl_bit_format;
1092 struct sbc_enc_cfg_t dummy_reset_config;
1093
1094 memset(&dummy_reset_config, 0x0, sizeof(dummy_reset_config));
1095 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
1096 MIXER_ENC_CONFIG_BLOCK);
1097 if (!ctl_enc_config) {
1098 ALOGE("%s: ERROR A2DP encoder format mixer control not identifed", __func__);
1099 } else {
1100 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
1101 sizeof(dummy_reset_config));
1102 a2dp.bt_encoder_format = ENC_MEDIA_FMT_NONE;
1103 }
1104
1105 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
1106
1107 return ret;
1108}
1109
1110int audio_extn_a2dp_stop_playback()
1111{
1112 int ret = 0;
1113
1114 ALOGV("%s: stop", __func__);
1115 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_stop)) {
1116 ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__);
1117 return -ENOSYS;
1118 }
1119
1120 if (a2dp.a2dp_total_active_session_request > 0)
1121 a2dp.a2dp_total_active_session_request--;
1122 else
1123 ALOGE("%s: No active playback session requests on A2DP", __func__);
1124
1125 if (a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
1126 ALOGV("%s: calling Bluetooth module stream stop", __func__);
1127 ret = a2dp.audio_stream_stop();
1128 if (ret < 0)
1129 ALOGE("%s: stop stream to Bluetooth IPC lib failed", __func__);
1130 else
1131 ALOGV("%s: stop steam to Bluetooth IPC lib successful", __func__);
1132 reset_a2dp_enc_config_params();
1133 a2dp_reset_backend_cfg();
1134 a2dp.a2dp_started = false;
1135 }
1136 ALOGD("%s: Stop A2DP playback total active sessions :%d", __func__,
1137 a2dp.a2dp_total_active_session_request);
1138 return 0;
1139}
1140
1141void audio_extn_a2dp_set_parameters(struct str_parms *parms)
1142{
1143 int ret, val;
1144 char value[32] = {0};
1145 struct audio_usecase *uc_info;
1146 struct listnode *node;
1147
1148 if (a2dp.is_a2dp_offload_supported == false) {
1149 ALOGV("%s: No supported encoders identified,ignoring A2DP setparam", __func__);
1150 return;
1151 }
1152
1153 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
1154 sizeof(value));
1155 if (ret >= 0) {
1156 val = atoi(value);
1157 if (audio_is_a2dp_out_device(val)) {
1158 ALOGV("%s: Received device connect request for A2DP", __func__);
1159 open_a2dp_output();
1160 }
1161 goto param_handled;
1162 }
1163
1164 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
1165 sizeof(value));
1166
1167 if (ret >= 0) {
1168 val = atoi(value);
1169 if (audio_is_a2dp_out_device(val)) {
1170 ALOGV("%s: Received device disconnect request", __func__);
1171 reset_a2dp_enc_config_params();
1172 close_a2dp_output();
1173 }
1174 goto param_handled;
1175 }
1176
1177 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
1178 if (ret >= 0) {
1179 if (a2dp.bt_lib_handle && (a2dp.bt_state != A2DP_STATE_DISCONNECTED)) {
1180 if ((!strncmp(value, "true", sizeof(value))) && !a2dp.a2dp_suspended) {
1181 ALOGD("%s: Setting A2DP to suspend state", __func__);
1182 a2dp.a2dp_suspended = true;
1183 list_for_each(node, &a2dp.adev->usecase_list) {
1184 uc_info = node_to_item(node, struct audio_usecase, list);
1185 if (uc_info->type == PCM_PLAYBACK &&
1186 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
1187 pthread_mutex_unlock(&a2dp.adev->lock);
1188 check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
1189 pthread_mutex_lock(&a2dp.adev->lock);
1190 }
1191 }
1192 reset_a2dp_enc_config_params();
1193 if (a2dp.audio_stream_suspend)
1194 a2dp.audio_stream_suspend();
1195 } else if (a2dp.a2dp_suspended) {
1196 ALOGD("%s: Resetting A2DP suspend state", __func__);
1197 struct audio_usecase *uc_info;
1198 struct listnode *node;
1199 if (a2dp.clear_a2dp_suspend_flag)
1200 a2dp.clear_a2dp_suspend_flag();
1201 a2dp.a2dp_suspended = false;
1202 /*
1203 * It is possible that before suspend, A2DP sessions can be active.
1204 * For example, during music + voice activation concurrency,
1205 * A2DP suspend will be called & Bluetooth will change to SCO mode.
1206 * Though music is paused as a part of voice activation,
1207 * compress session close happens only after pause timeout(10 secs).
1208 * So, if resume request comes before pause timeout, as A2DP session
1209 * is already active, IPC start will not be called from APM/audio_hw.
1210 * Fix this by calling A2DP start for IPC library post suspend
1211 * based on number of active session count.
1212 */
1213 if (a2dp.a2dp_total_active_session_request > 0) {
1214 ALOGD("%s: Calling Bluetooth IPC lib start post suspend state", __func__);
1215 if (a2dp.audio_stream_start) {
1216 ret = a2dp.audio_stream_start();
1217 if (ret != 0) {
1218 ALOGE("%s: Bluetooth controller start failed", __func__);
1219 a2dp.a2dp_started = false;
1220 }
1221 }
1222 }
1223 list_for_each(node, &a2dp.adev->usecase_list) {
1224 uc_info = node_to_item(node, struct audio_usecase, list);
1225 if (uc_info->type == PCM_PLAYBACK &&
1226 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
1227 pthread_mutex_unlock(&a2dp.adev->lock);
1228 check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
1229 pthread_mutex_lock(&a2dp.adev->lock);
1230 }
1231 }
1232 }
1233 }
1234 goto param_handled;
1235 }
1236param_handled:
1237 ALOGV("%s: end of A2DP setparam", __func__);
1238}
1239
1240void audio_extn_a2dp_set_handoff_mode(bool is_on)
1241{
1242 a2dp.is_handoff_in_progress = is_on;
1243}
1244
1245bool audio_extn_a2dp_is_force_device_switch()
1246{
1247 // During encoder reconfiguration mode, force A2DP device switch
1248 // Or if A2DP device is selected but earlier start failed as A2DP
1249 // was suspended, force retry.
1250 return a2dp.is_handoff_in_progress || !a2dp.a2dp_started;
1251}
1252
1253void audio_extn_a2dp_get_sample_rate(int *sample_rate)
1254{
1255 *sample_rate = a2dp.enc_sampling_rate;
1256}
1257
1258bool audio_extn_a2dp_is_ready()
1259{
1260 bool ret = false;
1261
1262 if (a2dp.a2dp_suspended)
1263 goto exit;
1264
1265 if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) &&
1266 (a2dp.is_a2dp_offload_supported) &&
1267 (a2dp.audio_check_a2dp_ready))
1268 ret = a2dp.audio_check_a2dp_ready();
1269
1270exit:
1271 return ret;
1272}
1273
1274bool audio_extn_a2dp_is_suspended()
1275{
1276 return a2dp.a2dp_suspended;
1277}
1278
1279void audio_extn_a2dp_init(void *adev)
1280{
1281 a2dp.adev = (struct audio_device*)adev;
1282 a2dp.bt_lib_handle = NULL;
1283 a2dp_common_init();
1284 a2dp.enc_sampling_rate = 48000;
1285 a2dp.is_a2dp_offload_supported = false;
1286 a2dp.is_handoff_in_progress = false;
1287 a2dp.is_aptx_dual_mono_supported = false;
1288 reset_a2dp_enc_config_params();
1289 update_offload_codec_support();
1290}
1291
1292uint32_t audio_extn_a2dp_get_encoder_latency()
1293{
1294 uint32_t latency = 0;
1295 int avsync_runtime_prop = 0;
1296 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
1297 aac_offset = 0, ldac_offset = 0;
1298 char value[PROPERTY_VALUE_MAX];
1299
1300 memset(value, '\0', sizeof(char) * PROPERTY_VALUE_MAX);
1301 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
1302 if (avsync_runtime_prop > 0) {
1303 if (sscanf(value, "%d/%d/%d/%d/%d",
1304 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset,
1305 &ldac_offset) != 5) {
1306 ALOGI("%s: Failed to parse avsync offset params from '%s'.", __func__, value);
1307 avsync_runtime_prop = 0;
1308 }
1309 }
1310
1311 switch (a2dp.bt_encoder_format) {
1312 case ENC_CODEC_TYPE_SBC:
1313 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
1314 latency += DEFAULT_SINK_LATENCY_SBC;
1315 break;
1316 case ENC_CODEC_TYPE_APTX:
1317 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
1318 latency += DEFAULT_SINK_LATENCY_APTX;
1319 break;
1320 case ENC_CODEC_TYPE_APTX_HD:
1321 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
1322 latency += DEFAULT_SINK_LATENCY_APTX_HD;
1323 break;
1324 case ENC_CODEC_TYPE_AAC:
1325 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
1326 latency += DEFAULT_SINK_LATENCY_AAC;
1327 break;
1328 case ENC_CODEC_TYPE_LDAC:
1329 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
1330 latency += DEFAULT_SINK_LATENCY_LDAC;
1331 break;
1332 default:
1333 latency = DEFAULT_ENCODER_LATENCY;
1334 break;
1335 }
1336 return latency;
1337}
1338#endif // A2DP_OFFLOAD_ENABLED