blob: 6f278c2495eabb2c12eb9ebcf8de96ba5631d81e [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
Aniket Kumar Lata26483012018-01-31 20:21:42 -080036#define BT_IPC_LIB_NAME "libbthost_if.so"
37
38// Media format definitions
39#define ENC_MEDIA_FMT_AAC 0x00010DA6
40#define ENC_MEDIA_FMT_APTX 0x000131ff
41#define ENC_MEDIA_FMT_APTX_HD 0x00013200
42#define ENC_MEDIA_FMT_LDAC 0x00013224
43#define ENC_MEDIA_FMT_SBC 0x00010BF2
44#define ENC_MEDIA_FMT_NONE 0
45#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
46#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
47#define MEDIA_FMT_AAC_AOT_LC 2
48#define MEDIA_FMT_AAC_AOT_SBR 5
49#define MEDIA_FMT_AAC_AOT_PS 29
50#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
51#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
52#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
53#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
54
55// PCM channels
56#define PCM_CHANNEL_L 1
57#define PCM_CHANNEL_R 2
58#define PCM_CHANNEL_C 3
59
60// Mixer controls sent to ALSA
61#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
Aniket Kumar Lata99546312018-03-19 21:38:38 -070062#define MIXER_DEC_CONFIG_BLOCK "SLIM_7_TX Decoder Config"
Aniket Kumar Lata26483012018-01-31 20:21:42 -080063#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
64#define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode"
Aniket Kumar Lata99546312018-03-19 21:38:38 -070065#define MIXER_SAMPLE_RATE_RX "BT SampleRate RX"
66#define MIXER_SAMPLE_RATE_TX "BT SampleRate TX"
Aniket Kumar Lata26483012018-01-31 20:21:42 -080067#define MIXER_AFE_IN_CHANNELS "AFE Input Channels"
Aniket Kumar Lata99546312018-03-19 21:38:38 -070068#define MIXER_ABR_TX_FEEDBACK_PATH "A2DP_SLIM7_UL_HL Switch"
69#define MIXER_SET_FEEDBACK_CHANNEL "BT set feedback channel"
Aniket Kumar Lata26483012018-01-31 20:21:42 -080070
71// Encoder format strings
72#define ENC_FMT_AAC "aac"
73#define ENC_FMT_APTX "aptx"
74#define ENC_FMT_APTXHD "aptxhd"
75#define ENC_FMT_LDAC "ldac"
76#define ENC_FMT_SBC "sbc"
77
78// System properties used for A2DP Offload
79#define SYSPROP_A2DP_OFFLOAD_ENABLED "persist.vendor.bluetooth.a2dp_offload.enable"
80#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
81
82// Default encoder bit width
83#define DEFAULT_ENCODER_BIT_FORMAT 16
84
85// Default encoder latency
86#define DEFAULT_ENCODER_LATENCY 200
87
88// Encoder latency offset for codecs supported
89#define ENCODER_LATENCY_AAC 70
90#define ENCODER_LATENCY_APTX 40
91#define ENCODER_LATENCY_APTX_HD 20
92#define ENCODER_LATENCY_LDAC 40
93#define ENCODER_LATENCY_SBC 10
94
95// Default A2DP sink latency offset
96#define DEFAULT_SINK_LATENCY_AAC 180
97#define DEFAULT_SINK_LATENCY_APTX 160
98#define DEFAULT_SINK_LATENCY_APTX_HD 180
99#define DEFAULT_SINK_LATENCY_LDAC 180
100#define DEFAULT_SINK_LATENCY_SBC 140
101
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700102// Slimbus Tx sample rate for ABR feedback channel
103#define ABR_TX_SAMPLE_RATE "KHZ_8"
104
105// Purpose ID for Inter Module Communication (IMC) in AFE
106#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
107
108// Maximum quality levels for ABR
109#define MAX_ABR_QUALITY_LEVELS 5
110
111// Instance identifier for A2DP
112#define MAX_INSTANCE_ID (UINT32_MAX / 2)
113
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800114/*
115 * Below enum values are extended from audio-base.h to
116 * keep encoder codec type local to bthost_ipc
117 * and audio_hal as these are intended only for handshake
118 * between IPC lib and Audio HAL.
119 */
120typedef enum {
121 ENC_CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
122 ENC_CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
123 ENC_CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
124 ENC_CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
125 ENC_CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
126 ENC_CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
127} enc_codec_t;
128
129typedef int (*audio_stream_open_t)(void);
130typedef int (*audio_stream_close_t)(void);
131typedef int (*audio_stream_start_t)(void);
132typedef int (*audio_stream_stop_t)(void);
133typedef int (*audio_stream_suspend_t)(void);
134typedef void (*audio_handoff_triggered_t)(void);
135typedef void (*clear_a2dp_suspend_flag_t)(void);
136typedef void * (*audio_get_codec_config_t)(uint8_t *multicast_status, uint8_t *num_dev,
137 enc_codec_t *codec_type);
138typedef int (*audio_check_a2dp_ready_t)(void);
139typedef int (*audio_is_scrambling_enabled_t)(void);
140
141enum A2DP_STATE {
142 A2DP_STATE_CONNECTED,
143 A2DP_STATE_STARTED,
144 A2DP_STATE_STOPPED,
145 A2DP_STATE_DISCONNECTED,
146};
147
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700148typedef enum {
149 IMC_TRANSMIT,
150 IMC_RECEIVE,
151} imc_direction_t;
152
153typedef enum {
154 IMC_DISABLE,
155 IMC_ENABLE,
156} imc_status_t;
157
158/* PCM config for ABR Feedback hostless front end */
159static struct pcm_config pcm_config_abr = {
160 .channels = 1,
161 .rate = 8000,
162 .period_size = 240,
163 .period_count = 2,
164 .format = PCM_FORMAT_S16_LE,
165 .start_threshold = 0,
166 .stop_threshold = INT_MAX,
167 .avail_min = 0,
168};
169
170/* Adaptive bitrate config for A2DP codecs */
171struct a2dp_abr_config {
172 /* Flag to denote whether Adaptive bitrate is enabled for codec */
173 bool is_abr_enabled;
174 /* Flag to denote whether front end has been opened for ABR */
175 bool abr_started;
176 /* ABR Tx path pcm handle */
177 struct pcm *abr_tx_handle;
178 /* ABR Inter Module Communication (IMC) instance ID */
179 uint32_t imc_instance;
180};
181
182static uint32_t instance_id = MAX_INSTANCE_ID;
183
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800184/* Data structure used to:
185 * - Update the A2DP state machine
186 * - Communicate with the libbthost_if.so IPC library
187 * - Store DSP encoder configuration information
188 */
189struct a2dp_data {
190 /* Audio device handle */
191 struct audio_device *adev;
192 /* Bluetooth IPC library handle */
193 void *bt_lib_handle;
194 /* Open A2DP audio stream. Initialize audio datapath */
195 audio_stream_open_t audio_stream_open;
196 /* Close A2DP audio stream */
197 audio_stream_close_t audio_stream_close;
198 /* Start A2DP audio stream. Start audio datapath */
199 audio_stream_start_t audio_stream_start;
200 /* Stop A2DP audio stream */
201 audio_stream_stop_t audio_stream_stop;
202 /* Suspend A2DP audio stream */
203 audio_stream_suspend_t audio_stream_suspend;
204 /* Notify Bluetooth IPC library of handoff being triggered */
205 audio_handoff_triggered_t audio_handoff_triggered;
206 /* Clear A2DP suspend flag in Bluetooth IPC library */
207 clear_a2dp_suspend_flag_t clear_a2dp_suspend_flag;
208 /* Get codec configuration from Bluetooth stack via
209 * Bluetooth IPC library */
210 audio_get_codec_config_t audio_get_codec_config;
211 /* Check if A2DP is ready */
212 audio_check_a2dp_ready_t audio_check_a2dp_ready;
213 /* Check if scrambling is enabled on BTSoC */
214 audio_is_scrambling_enabled_t audio_is_scrambling_enabled;
215 /* Internal A2DP state identifier */
216 enum A2DP_STATE bt_state;
217 /* A2DP codec type configured */
218 enc_codec_t bt_encoder_format;
219 /* Sampling rate configured with A2DP encoder on DSP */
220 uint32_t enc_sampling_rate;
221 /* Channel configuration of A2DP on DSP */
222 uint32_t enc_channels;
223 /* Flag to denote whether A2DP audio datapath has started */
224 bool a2dp_started;
225 /* Flag to denote whether A2DP audio datapath is suspended */
226 bool a2dp_suspended;
227 /* Number of active sessions on A2DP output */
228 int a2dp_total_active_session_request;
229 /* Flag to denote whether A2DP offload is supported */
230 bool is_a2dp_offload_supported;
231 /* Flag to denote whether codec reconfiguration/soft handoff is in progress */
232 bool is_handoff_in_progress;
233 /* Flag to denote whether APTX Dual Mono encoder is supported */
234 bool is_aptx_dual_mono_supported;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700235 /* Adaptive bitrate config for A2DP codecs */
236 struct a2dp_abr_config abr_config;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800237};
238
239struct a2dp_data a2dp;
240
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700241/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
242 * Structures sent to configure DSP for ABR are defined below.
243 * This data helps DSP configure feedback path (BTSoC to LPASS)
244 * for link quality levels and mapping quality levels to codec
245 * specific bitrate.
246 */
247
248/* Key value pair for link quality level to bitrate mapping. */
249struct bit_rate_level_map_t {
250 uint32_t link_quality_level;
251 uint32_t bitrate;
252};
253
254/* Link quality level to bitrate mapping info sent to DSP. */
255struct quality_level_to_bitrate_info {
256 /* Number of quality levels being mapped.
257 * This will be equal to the size of mapping table.
258 */
259 uint32_t num_levels;
260 /* Quality level to bitrate mapping table */
261 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
262};
263
264/* Structure to set up Inter Module Communication (IMC) between
265 * AFE Decoder and Encoder.
266 */
267struct imc_dec_enc_info {
268 /* Decoder to encoder communication direction.
269 * Transmit = 0 / Receive = 1
270 */
271 uint32_t direction;
272 /* Enable / disable IMC between decoder and encoder */
273 uint32_t enable;
274 /* Purpose of IMC being set up between decoder and encoder.
275 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
276 * is the default value to be sent as purpose.
277 */
278 uint32_t purpose;
279 /* Unique communication instance ID.
280 * purpose and comm_instance together form the actual key
281 * used in IMC registration, which must be the same for
282 * encoder and decoder for which IMC is being set up.
283 */
284 uint32_t comm_instance;
285};
286
287/* Structure used for ABR config of AFE encoder and decoder. */
288struct abr_enc_cfg_t {
289 /* Link quality level to bitrate mapping info sent to DSP. */
290 struct quality_level_to_bitrate_info mapping_info;
291 /* Information to set up IMC between decoder and encoder */
292 struct imc_dec_enc_info imc_info;
293} __attribute__ ((packed));
294
295/* Structure to send configuration for decoder introduced
296 * on AFE Tx path for ABR link quality feedback to BT encoder.
297 */
298struct abr_dec_cfg_t {
299 /* Decoder media format */
300 uint32_t dec_format;
301 /* Information to set up IMC between decoder and encoder */
302 struct imc_dec_enc_info imc_info;
303} __attribute__ ((packed));
304
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800305/* START of DSP configurable structures
306 * These values should match with DSP interface defintion
307 */
308
309/* AAC encoder configuration structure. */
310typedef struct aac_enc_cfg_t aac_enc_cfg_t;
311
312struct aac_enc_cfg_t {
313 /* Encoder media format for AAC */
314 uint32_t enc_format;
315
316 /* Encoding rate in bits per second */
317 uint32_t bit_rate;
318
319 /* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS */
320 uint32_t enc_mode;
321
322 /* supported aac_fmt_flag are ADTS/RAW */
323 uint16_t aac_fmt_flag;
324
325 /* supported channel_cfg are Native mode, Mono , Stereo */
326 uint16_t channel_cfg;
327
328 /* Number of samples per second */
329 uint32_t sample_rate;
330} __attribute__ ((packed));
331
332/* SBC encoder configuration structure. */
333typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
334
335struct sbc_enc_cfg_t {
336 /* Encoder media format for SBC */
337 uint32_t enc_format;
338
339 /* supported num_subbands are 4/8 */
340 uint32_t num_subbands;
341
342 /* supported blk_len are 4, 8, 12, 16 */
343 uint32_t blk_len;
344
345 /* supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO */
346 uint32_t channel_mode;
347
348 /* supported alloc_method are LOUNDNESS/SNR */
349 uint32_t alloc_method;
350
351 /* supported bit_rate for mono channel is max 320kbps
352 * supported bit rate for stereo channel is max 512 kbps */
353 uint32_t bit_rate;
354
355 /* Number of samples per second */
356 uint32_t sample_rate;
357} __attribute__ ((packed));
358
359struct custom_enc_cfg_t {
360 /* Custom encoder media format */
361 uint32_t enc_format;
362
363 /* Number of samples per second */
364 uint32_t sample_rate;
365
366 /* supported num_channels are Mono/Stereo */
367 uint16_t num_channels;
368
369 /* Reserved for future enhancement */
370 uint16_t reserved;
371
372 /* supported channel_mapping for mono is CHANNEL_C
373 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R */
374 uint8_t channel_mapping[8];
375
376 /* Reserved for future enhancement */
377 uint32_t custom_size;
378} __attribute__ ((packed));
379
380struct aptx_v2_enc_cfg_ext_t {
381/* sync_mode introduced with APTX V2 libraries
382 * sync mode: 0x0 = stereo sync mode
383 * 0x01 = dual mono sync mode
384 * 0x02 = dual mono with no sync on either L or R codewords
385 */
386 uint32_t sync_mode;
387} __attribute__ ((packed));
388
389/* APTX struct for combining custom enc and V2 members */
390struct aptx_enc_cfg_t {
391 struct custom_enc_cfg_t custom_cfg;
392 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
393} __attribute__ ((packed));
394
395struct ldac_specific_enc_cfg_t {
396 /*
397 * This is used to calculate the encoder output
398 * bytes per frame (i.e. bytes per packet).
399 * Bit rate also configures the EQMID.
400 * The min bit rate 303000 bps is calculated for
401 * 44.1 kHz and 88.2 KHz sampling frequencies with
402 * Mobile use Quality.
403 * The max bit rate of 990000 bps is calculated for
404 * 96kHz and 48 KHz with High Quality
405 * @Range(in bits per second)
406 * 303000 for Mobile use Quality
407 * 606000 for standard Quality
408 * 909000 for High Quality
409 */
410 uint32_t bit_rate;
411
412 /*
413 * The channel setting information for LDAC specification
414 * of Bluetooth A2DP which is determined by SRC and SNK
415 * devices in Bluetooth transmission.
416 * @Range:
417 * 0 for native mode
418 * 4 for mono
419 * 2 for dual channel
420 * 1 for stereo
421 */
422 uint16_t channel_mode;
423
424 /*
425 * Maximum Transmission Unit (MTU).
426 * The minimum MTU that a L2CAP implementation for LDAC shall
427 * support is 679 bytes, because LDAC is optimized with 2-DH5
428 * packet as its target.
429 * @Range : 679
430 * @Default: 679 for LDACBT_MTU_2DH5
431 */
432 uint16_t mtu;
433} __attribute__ ((packed));
434
435/* LDAC struct for combining custom enc and standard members */
436struct ldac_enc_cfg_t {
437 struct custom_enc_cfg_t custom_cfg;
438 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700439 struct abr_enc_cfg_t abr_cfg;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800440} __attribute__ ((packed));
441
442/* Information about Bluetooth SBC encoder configuration
443 * This data is used between audio HAL module and
444 * Bluetooth IPC library to configure DSP encoder
445 */
446typedef struct {
447 uint32_t subband; /* 4, 8 */
448 uint32_t blk_len; /* 4, 8, 12, 16 */
449 uint16_t sampling_rate; /* 44.1khz, 48khz */
450 uint8_t channels; /* 0(Mono), 1(Dual_mono), 2(Stereo), 3(JS) */
451 uint8_t alloc; /* 0(Loudness), 1(SNR) */
452 uint8_t min_bitpool; /* 2 */
453 uint8_t max_bitpool; /* 53(44.1khz), 51 (48khz) */
454 uint32_t bitrate; /* 320kbps to 512kbps */
455 uint32_t bits_per_sample; /* 16 bit */
456} audio_sbc_encoder_config;
457
458/* Information about Bluetooth APTX encoder configuration
459 * This data is used between audio HAL module and
460 * Bluetooth IPC library to configure DSP encoder
461 */
462typedef struct {
463 uint16_t sampling_rate;
464 uint8_t channels;
465 uint32_t bitrate;
466 uint32_t bits_per_sample;
467} audio_aptx_default_config;
468
469typedef struct {
470 uint16_t sampling_rate;
471 uint8_t channels;
472 uint32_t bitrate;
473 uint32_t sync_mode;
474} audio_aptx_dual_mono_config;
475
476typedef union {
477 audio_aptx_default_config *default_cfg;
478 audio_aptx_dual_mono_config *dual_mono_cfg;
479} audio_aptx_encoder_config;
480
481/* Information about Bluetooth AAC encoder configuration
482 * This data is used between audio HAL module and
483 * Bluetooth IPC library to configure DSP encoder
484 */
485typedef struct {
486 uint32_t enc_mode; /* LC, SBR, PS */
487 uint16_t format_flag; /* RAW, ADTS */
488 uint16_t channels; /* 1-Mono, 2-Stereo */
489 uint32_t sampling_rate;
490 uint32_t bitrate;
491 uint32_t bits_per_sample;
492} audio_aac_encoder_config;
493
494/* Information about Bluetooth LDAC encoder configuration
495 * This data is used between audio HAL module and
496 * Bluetooth IPC library to configure DSP encoder
497 */
498typedef struct {
499 uint32_t sampling_rate; /* 44100, 48000, 88200, 96000 */
500 uint32_t bit_rate; /* 303000, 606000, 909000 (in bits per second) */
501 uint16_t channel_mode; /* 0, 4, 2, 1 */
502 uint16_t mtu;
503 uint32_t bits_per_sample; /* 16, 24, 32 (bits) */
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700504 bool is_abr_enabled;
505 struct quality_level_to_bitrate_info level_to_bitrate_map;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800506} audio_ldac_encoder_config;
507
508/*********** END of DSP configurable structures ********************/
509
510static void a2dp_common_init()
511{
512 a2dp.a2dp_started = false;
513 a2dp.a2dp_total_active_session_request = 0;
514 a2dp.a2dp_suspended = false;
515 a2dp.bt_encoder_format = ENC_CODEC_TYPE_INVALID;
516 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700517 a2dp.abr_config.is_abr_enabled = false;
518 a2dp.abr_config.abr_started = false;
519 a2dp.abr_config.imc_instance = 0;
520 a2dp.abr_config.abr_tx_handle = NULL;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800521}
522
523static void update_offload_codec_support()
524{
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800525 a2dp.is_a2dp_offload_supported =
526 property_get_bool(SYSPROP_A2DP_OFFLOAD_ENABLED, false);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700527 ALOGD("%s: A2DP offload supported = %d",
528 __func__, a2dp.is_a2dp_offload_supported);
529}
530
531static int stop_abr()
532{
533 struct mixer_ctl *ctl_abr_tx_path = NULL;
534 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
535
536 /* This function can be used if !abr_started for clean up */
537 ALOGV("%s: enter", __func__);
538
539 // Close hostless front end
540 if (a2dp.abr_config.abr_tx_handle != NULL) {
541 pcm_close(a2dp.abr_config.abr_tx_handle);
542 a2dp.abr_config.abr_tx_handle = NULL;
543 }
544 a2dp.abr_config.abr_started = false;
545 a2dp.abr_config.imc_instance = 0;
546
547 // Reset BT driver mixer control for ABR usecase
548 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
549 MIXER_SET_FEEDBACK_CHANNEL);
550 if (!ctl_set_bt_feedback_channel) {
551 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
552 return -ENOSYS;
553 }
554 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
555 ALOGE("%s: Failed to set BT usecase", __func__);
556 return -ENOSYS;
557 }
558
559 // Reset ABR Tx feedback path
560 ALOGV("%s: Disable ABR Tx feedback path", __func__);
561 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
562 MIXER_ABR_TX_FEEDBACK_PATH);
563 if (!ctl_abr_tx_path) {
564 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
565 return -ENOSYS;
566 }
567 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
568 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
569 return -ENOSYS;
570 }
571
572 return 0;
573}
574
575static int start_abr()
576{
577 struct mixer_ctl *ctl_abr_tx_path = NULL;
578 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
579 int abr_device_id;
580 int ret = 0;
581
582 if (!a2dp.abr_config.is_abr_enabled) {
583 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
584 return -ENOSYS;
585 }
586
587 if (a2dp.abr_config.abr_started) {
588 ALOGI("%s: ABR has already started", __func__);
589 return ret;
590 }
591
592 // Enable Slimbus 7 Tx feedback path
593 ALOGV("%s: Enable ABR Tx feedback path", __func__);
594 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
595 MIXER_ABR_TX_FEEDBACK_PATH);
596 if (!ctl_abr_tx_path) {
597 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
598 return -ENOSYS;
599 }
600 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
601 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
602 return -ENOSYS;
603 }
604
605 // Notify ABR usecase information to BT driver to distinguish
606 // between SCO and feedback usecase
607 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
608 MIXER_SET_FEEDBACK_CHANNEL);
609 if (!ctl_set_bt_feedback_channel) {
610 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
611 return -ENOSYS;
612 }
613 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
614 ALOGE("%s: Failed to set BT usecase", __func__);
615 return -ENOSYS;
616 }
617
618 // Open hostless front end and prepare ABR Tx path
619 abr_device_id = platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
620 PCM_CAPTURE);
621 if (!a2dp.abr_config.abr_tx_handle) {
622 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
623 abr_device_id, PCM_IN,
624 &pcm_config_abr);
625 if (a2dp.abr_config.abr_tx_handle == NULL ||
626 !pcm_is_ready(a2dp.abr_config.abr_tx_handle))
627 goto fail;
628 }
629 ret = pcm_start(a2dp.abr_config.abr_tx_handle);
630 if (ret < 0)
631 goto fail;
632 a2dp.abr_config.abr_started = true;
633
634 return ret;
635
636fail:
637 ALOGE("%s: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
638 stop_abr();
639 return -ENOSYS;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800640}
641
642/* API to open Bluetooth IPC library to start IPC communication */
643static int open_a2dp_output()
644{
645 int ret = 0;
646
647 ALOGD("%s: Open A2DP output start", __func__);
648
649 if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
650 ALOGD("%s: Called A2DP open with improper state, Ignoring request state %d",
651 __func__, a2dp.bt_state);
652 return -ENOSYS;
653 }
654
655 if (a2dp.bt_lib_handle == NULL) {
656 ALOGD("%s: Requesting for Bluetooth IPC lib handle", __func__);
657 a2dp.bt_lib_handle = dlopen(BT_IPC_LIB_NAME, RTLD_NOW);
658
659 if (a2dp.bt_lib_handle == NULL) {
660 ret = -errno;
661 ALOGE("%s: DLOPEN failed for %s errno %d strerror %s", __func__,
662 BT_IPC_LIB_NAME, errno, strerror(errno));
663 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
664 return ret;
665 } else {
666 a2dp.audio_stream_open = (audio_stream_open_t)
667 dlsym(a2dp.bt_lib_handle, "audio_stream_open");
668 a2dp.audio_stream_start = (audio_stream_start_t)
669 dlsym(a2dp.bt_lib_handle, "audio_stream_start");
670 a2dp.audio_get_codec_config = (audio_get_codec_config_t)
671 dlsym(a2dp.bt_lib_handle, "audio_get_codec_config");
672 a2dp.audio_stream_suspend = (audio_stream_suspend_t)
673 dlsym(a2dp.bt_lib_handle, "audio_stream_suspend");
674 a2dp.audio_handoff_triggered = (audio_handoff_triggered_t)
675 dlsym(a2dp.bt_lib_handle, "audio_handoff_triggered");
676 a2dp.clear_a2dp_suspend_flag = (clear_a2dp_suspend_flag_t)
677 dlsym(a2dp.bt_lib_handle, "clear_a2dp_suspend_flag");
678 a2dp.audio_stream_stop = (audio_stream_stop_t)
679 dlsym(a2dp.bt_lib_handle, "audio_stream_stop");
680 a2dp.audio_stream_close = (audio_stream_close_t)
681 dlsym(a2dp.bt_lib_handle, "audio_stream_close");
682 a2dp.audio_check_a2dp_ready = (audio_check_a2dp_ready_t)
683 dlsym(a2dp.bt_lib_handle,"audio_check_a2dp_ready");
684 a2dp.audio_is_scrambling_enabled = (audio_is_scrambling_enabled_t)
685 dlsym(a2dp.bt_lib_handle,"audio_is_scrambling_enabled");
686 }
687 }
688
689 if (a2dp.bt_lib_handle && a2dp.audio_stream_open) {
690 ALOGD("%s: calling Bluetooth stream open", __func__);
691 ret = a2dp.audio_stream_open();
692 if (ret != 0) {
693 ALOGE("%s: Failed to open output stream for A2DP: status %d", __func__, ret);
694 dlclose(a2dp.bt_lib_handle);
695 a2dp.bt_lib_handle = NULL;
696 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
697 return ret;
698 }
699 a2dp.bt_state = A2DP_STATE_CONNECTED;
700 } else {
701 ALOGE("%s: A2DP handle is not identified, Ignoring open request", __func__);
702 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
703 return -ENOSYS;
704 }
705
706 return ret;
707}
708
709static int close_a2dp_output()
710{
711 ALOGV("%s\n",__func__);
712 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_close)) {
713 ALOGE("%s: A2DP handle is not identified, Ignoring close request", __func__);
714 return -ENOSYS;
715 }
716 if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
717 ALOGD("%s: calling Bluetooth stream close", __func__);
718 if (a2dp.audio_stream_close() == false)
719 ALOGE("%s: failed close A2DP control path from Bluetooth IPC library", __func__);
720 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700721 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
722 stop_abr();
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800723 a2dp_common_init();
724 a2dp.enc_sampling_rate = 0;
725 a2dp.enc_channels = 0;
726
727 return 0;
728}
729
730static int a2dp_check_and_set_scrambler()
731{
732 bool scrambler_mode = false;
733 struct mixer_ctl *ctrl_scrambler_mode = NULL;
734 int ret = 0;
735 if (a2dp.audio_is_scrambling_enabled && (a2dp.bt_state != A2DP_STATE_DISCONNECTED))
736 scrambler_mode = a2dp.audio_is_scrambling_enabled();
737
738 // Scrambling needs to be enabled in DSP if scrambler mode is set
739 // disable scrambling not required
740 if (scrambler_mode) {
741 // enable scrambler in dsp
742 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
743 MIXER_SCRAMBLER_MODE);
744 if (!ctrl_scrambler_mode) {
745 ALOGE("%s: ERROR scrambler mode mixer control not identifed", __func__);
746 return -ENOSYS;
747 } else {
748 ret = mixer_ctl_set_value(ctrl_scrambler_mode, 0, true);
749 if (ret != 0) {
750 ALOGE("%s: Could not set scrambler mode", __func__);
751 return ret;
752 }
753 }
754 }
755 return 0;
756}
757
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700758static int a2dp_set_backend_cfg()
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800759{
760 const char *rate_str = NULL, *in_channels = NULL;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700761 uint32_t sampling_rate_rx = a2dp.enc_sampling_rate;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800762 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL;
763
764 // For LDAC encoder open slimbus port at 96Khz for 48Khz input
765 // and 88.2Khz for 44.1Khz input.
766 if ((a2dp.bt_encoder_format == ENC_CODEC_TYPE_LDAC) &&
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700767 (sampling_rate_rx == 48000 || sampling_rate_rx == 44100 )) {
768 sampling_rate_rx *= 2;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800769 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700770
771 // Set Rx backend sample rate
772 switch (sampling_rate_rx) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800773 case 44100:
774 rate_str = "KHZ_44P1";
775 break;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800776 case 88200:
777 rate_str = "KHZ_88P2";
778 break;
779 case 96000:
780 rate_str = "KHZ_96";
781 break;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700782 case 48000:
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800783 default:
784 rate_str = "KHZ_48";
785 break;
786 }
787
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700788 ALOGV("%s: set backend rx sample rate = %s", __func__, rate_str);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800789 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700790 MIXER_SAMPLE_RATE_RX);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800791 if (!ctl_sample_rate) {
792 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700793 return -ENOSYS;
794 }
795 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
796 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
797 return -ENOSYS;
798 }
799
800 // Set Tx backend sample rate
801 if (a2dp.abr_config.is_abr_enabled)
802 rate_str = ABR_TX_SAMPLE_RATE;
803
804 ALOGV("%s: set backend tx sample rate = %s", __func__, rate_str);
805 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
806 MIXER_SAMPLE_RATE_TX);
807 if (!ctl_sample_rate) {
808 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
809 return -ENOSYS;
810 }
811 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
812 ALOGE("%s: Failed to set backend sample rate = %s",
813 __func__, rate_str);
814 return -ENOSYS;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800815 }
816
817 // Configure AFE input channels
818 switch (a2dp.enc_channels) {
819 case 1:
820 in_channels = "One";
821 break;
822 case 2:
823 default:
824 in_channels = "Two";
825 break;
826 }
827
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700828 ALOGV("%s: set AFE input channels = %d", __func__, a2dp.enc_channels);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800829 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
830 MIXER_AFE_IN_CHANNELS);
831 if (!ctrl_in_channels) {
832 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700833 return -ENOSYS;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800834 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700835 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
836 ALOGE("%s: Failed to set AFE in channels = %d", __func__, a2dp.enc_channels);
837 return -ENOSYS;
838 }
839
840 return 0;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800841}
842
843static int a2dp_set_bit_format(uint32_t enc_bit_format)
844{
845 const char *bit_format = NULL;
846 struct mixer_ctl *ctrl_bit_format = NULL;
847
848 // Configure AFE Input Bit Format
849 switch (enc_bit_format) {
850 case 32:
851 bit_format = "S32_LE";
852 break;
853 case 24:
854 bit_format = "S24_LE";
855 break;
856 case 16:
857 default:
858 bit_format = "S16_LE";
859 break;
860 }
861
862 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
863 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
864 MIXER_ENC_BIT_FORMAT);
865 if (!ctrl_bit_format) {
866 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
867 return -ENOSYS;
868 }
869 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
870 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
871 return -ENOSYS;
872 }
873 return 0;
874}
875
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700876static int a2dp_reset_backend_cfg()
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800877{
878 const char *rate_str = "KHZ_8", *in_channels = "Zero";
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700879 struct mixer_ctl *ctl_sample_rate_rx = NULL, *ctl_sample_rate_tx = NULL;
880 struct mixer_ctl *ctrl_in_channels = NULL;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800881
882 // Reset backend sampling rate
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700883 ALOGV("%s: reset backend sample rate = %s", __func__, rate_str);
884 ctl_sample_rate_rx = mixer_get_ctl_by_name(a2dp.adev->mixer,
885 MIXER_SAMPLE_RATE_RX);
886 if (!ctl_sample_rate_rx) {
887 ALOGE("%s: ERROR Rx backend sample rate mixer control not identifed", __func__);
888 return -ENOSYS;
889 }
890 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_rx, rate_str) != 0) {
891 ALOGE("%s: Failed to reset Rx backend sample rate = %s", __func__, rate_str);
892 return -ENOSYS;
893 }
894
895 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
896 MIXER_SAMPLE_RATE_TX);
897 if (!ctl_sample_rate_tx) {
898 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
899 return -ENOSYS;
900 }
901 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
902 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
903 return -ENOSYS;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800904 }
905
906 // Reset AFE input channels
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700907 ALOGV("%s: reset AFE input channels = %s", __func__, in_channels);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800908 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
909 MIXER_AFE_IN_CHANNELS);
910 if (!ctrl_in_channels) {
911 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700912 return -ENOSYS;
913 }
914 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
915 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
916 return -ENOSYS;
917 }
918
919 return 0;
920}
921
922/* API to configure AFE decoder in DSP */
923static bool configure_a2dp_decoder_format(int dec_format)
924{
925 struct mixer_ctl *ctl_dec_data = NULL;
926 struct abr_dec_cfg_t dec_cfg;
927 int ret = 0;
928
929 if (a2dp.abr_config.is_abr_enabled) {
930 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_DEC_CONFIG_BLOCK);
931 if (!ctl_dec_data) {
932 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
933 return false;
934 }
935 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
936 dec_cfg.dec_format = dec_format;
937 dec_cfg.imc_info.direction = IMC_TRANSMIT;
938 dec_cfg.imc_info.enable = IMC_ENABLE;
939 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
940 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
941
942 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dec_cfg,
943 sizeof(dec_cfg));
944 if (ret != 0) {
945 ALOGE("%s: Failed to set decoder config", __func__);
946 return false;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800947 }
948 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700949
950 return true;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800951}
952
953/* API to configure SBC DSP encoder */
954static bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
955{
956 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
957 struct sbc_enc_cfg_t sbc_dsp_cfg;
958 bool is_configured = false;
959 int ret = 0;
960
961 if (sbc_bt_cfg == NULL) {
962 ALOGE("%s: Failed to get SBC encoder config from BT", __func__);
963 return false;
964 }
965
966 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
967 if (!ctl_enc_data) {
968 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
969 is_configured = false;
970 goto exit;
971 }
972 memset(&sbc_dsp_cfg, 0x0, sizeof(sbc_dsp_cfg));
973 sbc_dsp_cfg.enc_format = ENC_MEDIA_FMT_SBC;
974 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
975 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
976 switch (sbc_bt_cfg->channels) {
977 case 0:
978 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
979 break;
980 case 1:
981 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
982 break;
983 case 3:
984 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
985 break;
986 case 2:
987 default:
988 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
989 break;
990 }
991 if (sbc_bt_cfg->alloc)
992 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
993 else
994 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
995 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
996 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
997 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
998 sizeof(sbc_dsp_cfg));
999 if (ret != 0) {
1000 ALOGE("%s: failed to set SBC encoder config", __func__);
1001 is_configured = false;
1002 goto exit;
1003 }
1004 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
1005 if (ret != 0) {
1006 is_configured = false;
1007 goto exit;
1008 }
1009 is_configured = true;
1010 a2dp.bt_encoder_format = ENC_CODEC_TYPE_SBC;
1011 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
1012
1013 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1014 a2dp.enc_channels = 1;
1015 else
1016 a2dp.enc_channels = 2;
1017
1018 ALOGV("%s: Successfully updated SBC enc format with sampling rate: %d channel mode:%d",
1019 __func__, sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
1020exit:
1021 return is_configured;
1022}
1023
1024/* API to configure APTX DSP encoder */
1025static bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1026{
1027 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1028 int mixer_size;
1029 bool is_configured = false;
1030 int ret = 0;
1031 struct aptx_enc_cfg_t aptx_dsp_cfg;
1032 mixer_size = sizeof(aptx_dsp_cfg);
1033
1034 if (aptx_bt_cfg == NULL) {
1035 ALOGE("%s: Failed to get APTX encoder config from BT", __func__);
1036 return false;
1037 }
1038
1039 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1040 if (!ctl_enc_data) {
1041 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1042 is_configured = false;
1043 goto exit;
1044 }
1045
1046 memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg));
1047 aptx_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_APTX;
1048
1049 if (!a2dp.is_aptx_dual_mono_supported) {
1050 aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1051 aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1052 } else {
1053 aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1054 aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1055 aptx_dsp_cfg.aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1056 }
1057
1058 switch (aptx_dsp_cfg.custom_cfg.num_channels) {
1059 case 1:
1060 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1061 break;
1062 case 2:
1063 default:
1064 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1065 aptx_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1066 break;
1067 }
1068 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1069 mixer_size);
1070 if (ret != 0) {
1071 ALOGE("%s: Failed to set APTX encoder config", __func__);
1072 is_configured = false;
1073 goto exit;
1074 }
1075 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
1076 if (ret != 0) {
1077 is_configured = false;
1078 goto exit;
1079 }
1080 is_configured = true;
1081 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX;
1082 a2dp.enc_channels = aptx_dsp_cfg.custom_cfg.num_channels;
1083 if (!a2dp.is_aptx_dual_mono_supported) {
1084 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1085 ALOGV("%s: Successfully updated APTX enc format with sampling rate: %d \
1086 channels:%d", __func__, aptx_dsp_cfg.custom_cfg.sample_rate,
1087 aptx_dsp_cfg.custom_cfg.num_channels);
1088 } else {
1089 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1090 ALOGV("%s: Successfully updated APTX dual mono enc format with \
1091 sampling rate: %d channels:%d sync mode %d", __func__,
1092 aptx_dsp_cfg.custom_cfg.sample_rate,
1093 aptx_dsp_cfg.custom_cfg.num_channels,
1094 aptx_dsp_cfg.aptx_v2_cfg.sync_mode);
1095 }
1096
1097exit:
1098 return is_configured;
1099}
1100
1101/* API to configure APTX HD DSP encoder
1102 */
1103static bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
1104{
1105 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1106 struct custom_enc_cfg_t aptx_dsp_cfg;
1107 bool is_configured = false;
1108 int ret = 0;
1109
1110 if (aptx_bt_cfg == NULL) {
1111 ALOGE("%s: Failed to get APTX HD encoder config from BT", __func__);
1112 return false;
1113 }
1114
1115 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1116 if (!ctl_enc_data) {
1117 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1118 is_configured = false;
1119 goto exit;
1120 }
1121
1122 memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg));
1123 aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX_HD;
1124 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
1125 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
1126 switch (aptx_dsp_cfg.num_channels) {
1127 case 1:
1128 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1129 break;
1130 case 2:
1131 default:
1132 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1133 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1134 break;
1135 }
1136 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1137 sizeof(aptx_dsp_cfg));
1138 if (ret != 0) {
1139 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
1140 is_configured = false;
1141 goto exit;
1142 }
1143 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
1144 if (ret != 0) {
1145 is_configured = false;
1146 goto exit;
1147 }
1148 is_configured = true;
1149 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX_HD;
1150 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
1151 a2dp.enc_channels = aptx_bt_cfg->channels;
1152 ALOGV("%s: Successfully updated APTX HD encformat with sampling rate: %d channels:%d",
1153 __func__, aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
1154exit:
1155 return is_configured;
1156}
1157
1158/* API to configure AAC DSP encoder */
1159static bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
1160{
1161 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1162 struct aac_enc_cfg_t aac_dsp_cfg;
1163 bool is_configured = false;
1164 int ret = 0;
1165
1166 if (aac_bt_cfg == NULL) {
1167 ALOGE("%s: Failed to get AAC encoder config from BT", __func__);
1168 return false;
1169 }
1170
1171 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1172 if (!ctl_enc_data) {
1173 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1174 is_configured = false;
1175 goto exit;
1176 }
1177 memset(&aac_dsp_cfg, 0x0, sizeof(aac_dsp_cfg));
1178 aac_dsp_cfg.enc_format = ENC_MEDIA_FMT_AAC;
1179 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
1180 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
1181 switch (aac_bt_cfg->enc_mode) {
1182 case 0:
1183 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
1184 break;
1185 case 2:
1186 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
1187 break;
1188 case 1:
1189 default:
1190 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
1191 break;
1192 }
1193 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
1194 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
1195 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
1196 sizeof(aac_dsp_cfg));
1197 if (ret != 0) {
1198 ALOGE("%s: failed to set AAC encoder config", __func__);
1199 is_configured = false;
1200 goto exit;
1201 }
1202 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
1203 if (ret != 0) {
1204 is_configured = false;
1205 goto exit;
1206 }
1207 is_configured = true;
1208 a2dp.bt_encoder_format = ENC_CODEC_TYPE_AAC;
1209 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001210 a2dp.enc_channels = aac_bt_cfg->channels;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001211 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
1212 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
1213exit:
1214 return is_configured;
1215}
1216
1217static bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
1218{
1219 struct mixer_ctl *ldac_enc_data = NULL, *ctrl_bit_format = NULL;
1220 struct ldac_enc_cfg_t ldac_dsp_cfg;
1221 bool is_configured = false;
1222 int ret = 0;
1223
1224 if (ldac_bt_cfg == NULL) {
1225 ALOGE("%s: Failed to get LDAC encoder config from BT", __func__);
1226 return false;
1227 }
1228
1229 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1230 if (!ldac_enc_data) {
1231 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1232 is_configured = false;
1233 goto exit;
1234 }
1235 memset(&ldac_dsp_cfg, 0x0, sizeof(ldac_dsp_cfg));
1236
1237 ldac_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_LDAC;
1238 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
1239 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
1240 switch (ldac_dsp_cfg.ldac_cfg.channel_mode) {
1241 case 4:
1242 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1243 ldac_dsp_cfg.custom_cfg.num_channels = 1;
1244 break;
1245 case 2:
1246 case 1:
1247 default:
1248 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1249 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1250 ldac_dsp_cfg.custom_cfg.num_channels = 2;
1251 break;
1252 }
1253
1254 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(ldac_dsp_cfg);
1255 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
1256 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001257 if (ldac_bt_cfg->is_abr_enabled) {
1258 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
1259 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
1260 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
1261 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1262 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1263 }
1264
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001265 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
1266 sizeof(ldac_dsp_cfg));
1267 if (ret != 0) {
1268 ALOGE("%s: Failed to set LDAC encoder config", __func__);
1269 is_configured = false;
1270 goto exit;
1271 }
1272 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
1273 if (ret != 0) {
1274 is_configured = false;
1275 goto exit;
1276 }
1277 is_configured = true;
1278 a2dp.bt_encoder_format = ENC_CODEC_TYPE_LDAC;
1279 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
1280 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001281 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001282 ALOGV("%s: Successfully updated LDAC encformat with sampling rate: %d channels:%d",
1283 __func__, ldac_dsp_cfg.custom_cfg.sample_rate,
1284 ldac_dsp_cfg.custom_cfg.num_channels);
1285exit:
1286 return is_configured;
1287}
1288
1289bool configure_a2dp_encoder_format()
1290{
1291 void *codec_info = NULL;
1292 uint8_t multi_cast = 0, num_dev = 1;
1293 enc_codec_t codec_type = ENC_CODEC_TYPE_INVALID;
1294 bool is_configured = false;
1295 audio_aptx_encoder_config aptx_encoder_cfg;
1296
1297 if (!a2dp.audio_get_codec_config) {
1298 ALOGE("%s: A2DP handle is not identified, ignoring A2DP encoder config", __func__);
1299 return false;
1300 }
1301 ALOGD("%s: start", __func__);
1302 codec_info = a2dp.audio_get_codec_config(&multi_cast, &num_dev,
1303 &codec_type);
1304
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001305 // ABR disabled by default for all codecs
1306 a2dp.abr_config.is_abr_enabled = false;
1307
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001308 switch (codec_type) {
1309 case ENC_CODEC_TYPE_SBC:
1310 ALOGD("%s: Received SBC encoder supported Bluetooth device", __func__);
1311 is_configured =
1312 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
1313 break;
1314 case ENC_CODEC_TYPE_APTX:
1315 ALOGD("%s: Received APTX encoder supported Bluetooth device", __func__);
1316 a2dp.is_aptx_dual_mono_supported = false;
1317 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
1318 is_configured =
1319 configure_aptx_enc_format(&aptx_encoder_cfg);
1320 break;
1321 case ENC_CODEC_TYPE_APTX_HD:
1322 ALOGD("%s: Received APTX HD encoder supported Bluetooth device", __func__);
1323 is_configured =
1324 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
1325 break;
1326 case ENC_CODEC_TYPE_AAC:
1327 ALOGD("%s: Received AAC encoder supported Bluetooth device", __func__);
1328 is_configured =
1329 configure_aac_enc_format((audio_aac_encoder_config *)codec_info);
1330 break;
1331 case ENC_CODEC_TYPE_LDAC:
1332 ALOGD("%s: Received LDAC encoder supported Bluetooth device", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001333 if (!instance_id || instance_id > MAX_INSTANCE_ID)
1334 instance_id = MAX_INSTANCE_ID;
1335 a2dp.abr_config.imc_instance = instance_id--;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001336 is_configured =
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001337 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
1338 configure_a2dp_decoder_format(ENC_CODEC_TYPE_LDAC));
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001339 break;
1340 default:
1341 ALOGD("%s: Received unsupported encoder format", __func__);
1342 is_configured = false;
1343 break;
1344 }
1345 return is_configured;
1346}
1347
1348int audio_extn_a2dp_start_playback()
1349{
1350 int ret = 0;
1351
1352 ALOGD("%s: start", __func__);
1353
1354 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_start
1355 && a2dp.audio_get_codec_config)) {
1356 ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__);
1357 return -ENOSYS;
1358 }
1359
1360 if (a2dp.a2dp_suspended) {
1361 // session will be restarted after suspend completion
1362 ALOGD("%s: A2DP start requested during suspend state", __func__);
1363 return -ENOSYS;
1364 }
1365
1366 if (!a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
1367 ALOGD("%s: calling Bluetooth module stream start", __func__);
1368 /* This call indicates Bluetooth IPC lib to start playback */
1369 ret = a2dp.audio_stream_start();
1370 ALOGE("%s: Bluetooth controller start return = %d", __func__, ret);
1371 if (ret != 0 ) {
1372 ALOGE("%s: Bluetooth controller start failed", __func__);
1373 a2dp.a2dp_started = false;
1374 } else {
1375 if (configure_a2dp_encoder_format() == true) {
1376 a2dp.a2dp_started = true;
1377 ret = 0;
1378 ALOGD("%s: Start playback successful to Bluetooth IPC library", __func__);
1379 } else {
1380 ALOGD("%s: unable to configure DSP encoder", __func__);
1381 a2dp.a2dp_started = false;
1382 ret = -ETIMEDOUT;
1383 }
1384 }
1385 }
1386
1387 if (a2dp.a2dp_started) {
1388 a2dp.a2dp_total_active_session_request++;
1389 a2dp_check_and_set_scrambler();
1390 a2dp_set_backend_cfg();
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001391 if (a2dp.abr_config.is_abr_enabled)
1392 start_abr();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001393 }
1394
1395 ALOGD("%s: start A2DP playback total active sessions :%d", __func__,
1396 a2dp.a2dp_total_active_session_request);
1397 return ret;
1398}
1399
1400static int reset_a2dp_enc_config_params()
1401{
1402 int ret = 0;
1403
1404 struct mixer_ctl *ctl_enc_config, *ctrl_bit_format;
1405 struct sbc_enc_cfg_t dummy_reset_config;
1406
1407 memset(&dummy_reset_config, 0x0, sizeof(dummy_reset_config));
1408 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
1409 MIXER_ENC_CONFIG_BLOCK);
1410 if (!ctl_enc_config) {
1411 ALOGE("%s: ERROR A2DP encoder format mixer control not identifed", __func__);
1412 } else {
1413 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
1414 sizeof(dummy_reset_config));
1415 a2dp.bt_encoder_format = ENC_MEDIA_FMT_NONE;
1416 }
1417
1418 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
1419
1420 return ret;
1421}
1422
1423int audio_extn_a2dp_stop_playback()
1424{
1425 int ret = 0;
1426
1427 ALOGV("%s: stop", __func__);
1428 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_stop)) {
1429 ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__);
1430 return -ENOSYS;
1431 }
1432
1433 if (a2dp.a2dp_total_active_session_request > 0)
1434 a2dp.a2dp_total_active_session_request--;
1435 else
1436 ALOGE("%s: No active playback session requests on A2DP", __func__);
1437
1438 if (a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
1439 ALOGV("%s: calling Bluetooth module stream stop", __func__);
1440 ret = a2dp.audio_stream_stop();
1441 if (ret < 0)
1442 ALOGE("%s: stop stream to Bluetooth IPC lib failed", __func__);
1443 else
1444 ALOGV("%s: stop steam to Bluetooth IPC lib successful", __func__);
1445 reset_a2dp_enc_config_params();
1446 a2dp_reset_backend_cfg();
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001447 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1448 stop_abr();
1449 a2dp.abr_config.is_abr_enabled = false;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001450 a2dp.a2dp_started = false;
1451 }
1452 ALOGD("%s: Stop A2DP playback total active sessions :%d", __func__,
1453 a2dp.a2dp_total_active_session_request);
1454 return 0;
1455}
1456
1457void audio_extn_a2dp_set_parameters(struct str_parms *parms)
1458{
1459 int ret, val;
1460 char value[32] = {0};
1461 struct audio_usecase *uc_info;
1462 struct listnode *node;
1463
1464 if (a2dp.is_a2dp_offload_supported == false) {
1465 ALOGV("%s: No supported encoders identified,ignoring A2DP setparam", __func__);
1466 return;
1467 }
1468
1469 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
1470 sizeof(value));
1471 if (ret >= 0) {
1472 val = atoi(value);
1473 if (audio_is_a2dp_out_device(val)) {
1474 ALOGV("%s: Received device connect request for A2DP", __func__);
1475 open_a2dp_output();
1476 }
1477 goto param_handled;
1478 }
1479
1480 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
1481 sizeof(value));
1482
1483 if (ret >= 0) {
1484 val = atoi(value);
1485 if (audio_is_a2dp_out_device(val)) {
1486 ALOGV("%s: Received device disconnect request", __func__);
1487 reset_a2dp_enc_config_params();
1488 close_a2dp_output();
1489 }
1490 goto param_handled;
1491 }
1492
1493 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
1494 if (ret >= 0) {
1495 if (a2dp.bt_lib_handle && (a2dp.bt_state != A2DP_STATE_DISCONNECTED)) {
1496 if ((!strncmp(value, "true", sizeof(value))) && !a2dp.a2dp_suspended) {
1497 ALOGD("%s: Setting A2DP to suspend state", __func__);
1498 a2dp.a2dp_suspended = true;
1499 list_for_each(node, &a2dp.adev->usecase_list) {
1500 uc_info = node_to_item(node, struct audio_usecase, list);
1501 if (uc_info->type == PCM_PLAYBACK &&
1502 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
1503 pthread_mutex_unlock(&a2dp.adev->lock);
1504 check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
1505 pthread_mutex_lock(&a2dp.adev->lock);
1506 }
1507 }
1508 reset_a2dp_enc_config_params();
1509 if (a2dp.audio_stream_suspend)
1510 a2dp.audio_stream_suspend();
1511 } else if (a2dp.a2dp_suspended) {
1512 ALOGD("%s: Resetting A2DP suspend state", __func__);
1513 struct audio_usecase *uc_info;
1514 struct listnode *node;
1515 if (a2dp.clear_a2dp_suspend_flag)
1516 a2dp.clear_a2dp_suspend_flag();
1517 a2dp.a2dp_suspended = false;
1518 /*
1519 * It is possible that before suspend, A2DP sessions can be active.
1520 * For example, during music + voice activation concurrency,
1521 * A2DP suspend will be called & Bluetooth will change to SCO mode.
1522 * Though music is paused as a part of voice activation,
1523 * compress session close happens only after pause timeout(10 secs).
1524 * So, if resume request comes before pause timeout, as A2DP session
1525 * is already active, IPC start will not be called from APM/audio_hw.
1526 * Fix this by calling A2DP start for IPC library post suspend
1527 * based on number of active session count.
1528 */
1529 if (a2dp.a2dp_total_active_session_request > 0) {
1530 ALOGD("%s: Calling Bluetooth IPC lib start post suspend state", __func__);
1531 if (a2dp.audio_stream_start) {
1532 ret = a2dp.audio_stream_start();
1533 if (ret != 0) {
1534 ALOGE("%s: Bluetooth controller start failed", __func__);
1535 a2dp.a2dp_started = false;
1536 }
1537 }
1538 }
1539 list_for_each(node, &a2dp.adev->usecase_list) {
1540 uc_info = node_to_item(node, struct audio_usecase, list);
1541 if (uc_info->type == PCM_PLAYBACK &&
1542 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
1543 pthread_mutex_unlock(&a2dp.adev->lock);
1544 check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
1545 pthread_mutex_lock(&a2dp.adev->lock);
1546 }
1547 }
1548 }
1549 }
1550 goto param_handled;
1551 }
1552param_handled:
1553 ALOGV("%s: end of A2DP setparam", __func__);
1554}
1555
1556void audio_extn_a2dp_set_handoff_mode(bool is_on)
1557{
1558 a2dp.is_handoff_in_progress = is_on;
1559}
1560
1561bool audio_extn_a2dp_is_force_device_switch()
1562{
1563 // During encoder reconfiguration mode, force A2DP device switch
1564 // Or if A2DP device is selected but earlier start failed as A2DP
1565 // was suspended, force retry.
1566 return a2dp.is_handoff_in_progress || !a2dp.a2dp_started;
1567}
1568
1569void audio_extn_a2dp_get_sample_rate(int *sample_rate)
1570{
1571 *sample_rate = a2dp.enc_sampling_rate;
1572}
1573
1574bool audio_extn_a2dp_is_ready()
1575{
1576 bool ret = false;
1577
1578 if (a2dp.a2dp_suspended)
1579 goto exit;
1580
1581 if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) &&
1582 (a2dp.is_a2dp_offload_supported) &&
1583 (a2dp.audio_check_a2dp_ready))
1584 ret = a2dp.audio_check_a2dp_ready();
1585
1586exit:
1587 return ret;
1588}
1589
1590bool audio_extn_a2dp_is_suspended()
1591{
1592 return a2dp.a2dp_suspended;
1593}
1594
1595void audio_extn_a2dp_init(void *adev)
1596{
1597 a2dp.adev = (struct audio_device*)adev;
1598 a2dp.bt_lib_handle = NULL;
1599 a2dp_common_init();
1600 a2dp.enc_sampling_rate = 48000;
1601 a2dp.is_a2dp_offload_supported = false;
1602 a2dp.is_handoff_in_progress = false;
1603 a2dp.is_aptx_dual_mono_supported = false;
1604 reset_a2dp_enc_config_params();
1605 update_offload_codec_support();
1606}
1607
1608uint32_t audio_extn_a2dp_get_encoder_latency()
1609{
1610 uint32_t latency = 0;
1611 int avsync_runtime_prop = 0;
1612 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
1613 aac_offset = 0, ldac_offset = 0;
1614 char value[PROPERTY_VALUE_MAX];
1615
1616 memset(value, '\0', sizeof(char) * PROPERTY_VALUE_MAX);
1617 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
1618 if (avsync_runtime_prop > 0) {
1619 if (sscanf(value, "%d/%d/%d/%d/%d",
1620 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset,
1621 &ldac_offset) != 5) {
1622 ALOGI("%s: Failed to parse avsync offset params from '%s'.", __func__, value);
1623 avsync_runtime_prop = 0;
1624 }
1625 }
1626
1627 switch (a2dp.bt_encoder_format) {
1628 case ENC_CODEC_TYPE_SBC:
1629 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
1630 latency += DEFAULT_SINK_LATENCY_SBC;
1631 break;
1632 case ENC_CODEC_TYPE_APTX:
1633 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
1634 latency += DEFAULT_SINK_LATENCY_APTX;
1635 break;
1636 case ENC_CODEC_TYPE_APTX_HD:
1637 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
1638 latency += DEFAULT_SINK_LATENCY_APTX_HD;
1639 break;
1640 case ENC_CODEC_TYPE_AAC:
1641 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
1642 latency += DEFAULT_SINK_LATENCY_AAC;
1643 break;
1644 case ENC_CODEC_TYPE_LDAC:
1645 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
1646 latency += DEFAULT_SINK_LATENCY_LDAC;
1647 break;
1648 default:
1649 latency = DEFAULT_ENCODER_LATENCY;
1650 break;
1651 }
1652 return latency;
1653}
1654#endif // A2DP_OFFLOAD_ENABLED