blob: 18d93352edcc11dca042e4489b9b1b70dc6e2db0 [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
Petri Gynther2479b0b2018-04-17 18:38:47 -070079#define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported"
80#define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled"
Aniket Kumar Lata26483012018-01-31 20:21:42 -080081#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
82
83// Default encoder bit width
84#define DEFAULT_ENCODER_BIT_FORMAT 16
85
86// Default encoder latency
87#define DEFAULT_ENCODER_LATENCY 200
88
89// Encoder latency offset for codecs supported
90#define ENCODER_LATENCY_AAC 70
91#define ENCODER_LATENCY_APTX 40
92#define ENCODER_LATENCY_APTX_HD 20
93#define ENCODER_LATENCY_LDAC 40
94#define ENCODER_LATENCY_SBC 10
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +053095#define ENCODER_LATENCY_PCM 50
Aniket Kumar Lata26483012018-01-31 20:21:42 -080096
97// Default A2DP sink latency offset
98#define DEFAULT_SINK_LATENCY_AAC 180
99#define DEFAULT_SINK_LATENCY_APTX 160
100#define DEFAULT_SINK_LATENCY_APTX_HD 180
101#define DEFAULT_SINK_LATENCY_LDAC 180
102#define DEFAULT_SINK_LATENCY_SBC 140
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +0530103#define DEFAULT_SINK_LATENCY_PCM 140
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800104
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700105// Slimbus Tx sample rate for ABR feedback channel
106#define ABR_TX_SAMPLE_RATE "KHZ_8"
107
108// Purpose ID for Inter Module Communication (IMC) in AFE
109#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
110
111// Maximum quality levels for ABR
112#define MAX_ABR_QUALITY_LEVELS 5
113
114// Instance identifier for A2DP
115#define MAX_INSTANCE_ID (UINT32_MAX / 2)
116
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800117/*
118 * Below enum values are extended from audio-base.h to
119 * keep encoder codec type local to bthost_ipc
120 * and audio_hal as these are intended only for handshake
121 * between IPC lib and Audio HAL.
122 */
123typedef enum {
124 ENC_CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
125 ENC_CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
126 ENC_CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
127 ENC_CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
128 ENC_CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
129 ENC_CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +0530130 ENC_CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800131} enc_codec_t;
132
133typedef int (*audio_stream_open_t)(void);
134typedef int (*audio_stream_close_t)(void);
135typedef int (*audio_stream_start_t)(void);
136typedef int (*audio_stream_stop_t)(void);
137typedef int (*audio_stream_suspend_t)(void);
138typedef void (*audio_handoff_triggered_t)(void);
139typedef void (*clear_a2dp_suspend_flag_t)(void);
140typedef void * (*audio_get_codec_config_t)(uint8_t *multicast_status, uint8_t *num_dev,
141 enc_codec_t *codec_type);
142typedef int (*audio_check_a2dp_ready_t)(void);
143typedef int (*audio_is_scrambling_enabled_t)(void);
144
145enum A2DP_STATE {
146 A2DP_STATE_CONNECTED,
147 A2DP_STATE_STARTED,
148 A2DP_STATE_STOPPED,
149 A2DP_STATE_DISCONNECTED,
150};
151
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700152typedef enum {
153 IMC_TRANSMIT,
154 IMC_RECEIVE,
155} imc_direction_t;
156
157typedef enum {
158 IMC_DISABLE,
159 IMC_ENABLE,
160} imc_status_t;
161
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -0700162typedef enum {
163 MTU_SIZE,
164 PEAK_BIT_RATE,
165} frame_control_type_t;
166
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700167/* PCM config for ABR Feedback hostless front end */
168static struct pcm_config pcm_config_abr = {
169 .channels = 1,
170 .rate = 8000,
171 .period_size = 240,
172 .period_count = 2,
173 .format = PCM_FORMAT_S16_LE,
174 .start_threshold = 0,
175 .stop_threshold = INT_MAX,
176 .avail_min = 0,
177};
178
179/* Adaptive bitrate config for A2DP codecs */
180struct a2dp_abr_config {
181 /* Flag to denote whether Adaptive bitrate is enabled for codec */
182 bool is_abr_enabled;
183 /* Flag to denote whether front end has been opened for ABR */
184 bool abr_started;
185 /* ABR Tx path pcm handle */
186 struct pcm *abr_tx_handle;
187 /* ABR Inter Module Communication (IMC) instance ID */
188 uint32_t imc_instance;
189};
190
191static uint32_t instance_id = MAX_INSTANCE_ID;
192
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800193/* Data structure used to:
194 * - Update the A2DP state machine
195 * - Communicate with the libbthost_if.so IPC library
196 * - Store DSP encoder configuration information
197 */
198struct a2dp_data {
199 /* Audio device handle */
200 struct audio_device *adev;
201 /* Bluetooth IPC library handle */
202 void *bt_lib_handle;
203 /* Open A2DP audio stream. Initialize audio datapath */
204 audio_stream_open_t audio_stream_open;
205 /* Close A2DP audio stream */
206 audio_stream_close_t audio_stream_close;
207 /* Start A2DP audio stream. Start audio datapath */
208 audio_stream_start_t audio_stream_start;
209 /* Stop A2DP audio stream */
210 audio_stream_stop_t audio_stream_stop;
211 /* Suspend A2DP audio stream */
212 audio_stream_suspend_t audio_stream_suspend;
213 /* Notify Bluetooth IPC library of handoff being triggered */
214 audio_handoff_triggered_t audio_handoff_triggered;
215 /* Clear A2DP suspend flag in Bluetooth IPC library */
216 clear_a2dp_suspend_flag_t clear_a2dp_suspend_flag;
217 /* Get codec configuration from Bluetooth stack via
218 * Bluetooth IPC library */
219 audio_get_codec_config_t audio_get_codec_config;
220 /* Check if A2DP is ready */
221 audio_check_a2dp_ready_t audio_check_a2dp_ready;
222 /* Check if scrambling is enabled on BTSoC */
223 audio_is_scrambling_enabled_t audio_is_scrambling_enabled;
224 /* Internal A2DP state identifier */
225 enum A2DP_STATE bt_state;
226 /* A2DP codec type configured */
227 enc_codec_t bt_encoder_format;
228 /* Sampling rate configured with A2DP encoder on DSP */
229 uint32_t enc_sampling_rate;
230 /* Channel configuration of A2DP on DSP */
231 uint32_t enc_channels;
232 /* Flag to denote whether A2DP audio datapath has started */
233 bool a2dp_started;
234 /* Flag to denote whether A2DP audio datapath is suspended */
235 bool a2dp_suspended;
236 /* Number of active sessions on A2DP output */
237 int a2dp_total_active_session_request;
Petri Gynther2479b0b2018-04-17 18:38:47 -0700238 /* Flag to denote whether A2DP offload is enabled */
239 bool is_a2dp_offload_enabled;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800240 /* Flag to denote whether codec reconfiguration/soft handoff is in progress */
241 bool is_handoff_in_progress;
242 /* Flag to denote whether APTX Dual Mono encoder is supported */
243 bool is_aptx_dual_mono_supported;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700244 /* Adaptive bitrate config for A2DP codecs */
245 struct a2dp_abr_config abr_config;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800246};
247
248struct a2dp_data a2dp;
249
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700250/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
251 * Structures sent to configure DSP for ABR are defined below.
252 * This data helps DSP configure feedback path (BTSoC to LPASS)
253 * for link quality levels and mapping quality levels to codec
254 * specific bitrate.
255 */
256
257/* Key value pair for link quality level to bitrate mapping. */
258struct bit_rate_level_map_t {
259 uint32_t link_quality_level;
260 uint32_t bitrate;
261};
262
263/* Link quality level to bitrate mapping info sent to DSP. */
264struct quality_level_to_bitrate_info {
265 /* Number of quality levels being mapped.
266 * This will be equal to the size of mapping table.
267 */
268 uint32_t num_levels;
269 /* Quality level to bitrate mapping table */
270 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
271};
272
273/* Structure to set up Inter Module Communication (IMC) between
274 * AFE Decoder and Encoder.
275 */
276struct imc_dec_enc_info {
277 /* Decoder to encoder communication direction.
278 * Transmit = 0 / Receive = 1
279 */
280 uint32_t direction;
281 /* Enable / disable IMC between decoder and encoder */
282 uint32_t enable;
283 /* Purpose of IMC being set up between decoder and encoder.
284 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
285 * is the default value to be sent as purpose.
286 */
287 uint32_t purpose;
288 /* Unique communication instance ID.
289 * purpose and comm_instance together form the actual key
290 * used in IMC registration, which must be the same for
291 * encoder and decoder for which IMC is being set up.
292 */
293 uint32_t comm_instance;
294};
295
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -0700296/* Structure to control frame size of AAC encoded frames. */
297struct aac_frame_size_control_t {
298 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
299 uint32_t ctl_type;
300 /* Control value
301 * MTU_SIZE: MTU size in bytes
302 * PEAK_BIT_RATE: Peak bitrate in bits per second.
303 */
304 uint32_t ctl_value;
305};
306
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700307/* Structure used for ABR config of AFE encoder and decoder. */
308struct abr_enc_cfg_t {
309 /* Link quality level to bitrate mapping info sent to DSP. */
310 struct quality_level_to_bitrate_info mapping_info;
311 /* Information to set up IMC between decoder and encoder */
312 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata76ff4802018-08-06 15:30:50 -0700313 /* Flag to indicate whether ABR is enabled */
314 bool is_abr_enabled;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700315} __attribute__ ((packed));
316
317/* Structure to send configuration for decoder introduced
318 * on AFE Tx path for ABR link quality feedback to BT encoder.
319 */
320struct abr_dec_cfg_t {
321 /* Decoder media format */
322 uint32_t dec_format;
323 /* Information to set up IMC between decoder and encoder */
324 struct imc_dec_enc_info imc_info;
325} __attribute__ ((packed));
326
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800327/* START of DSP configurable structures
328 * These values should match with DSP interface defintion
329 */
330
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -0700331struct aac_cfg_blk_t {
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800332 /* Encoder media format for AAC */
333 uint32_t enc_format;
334
335 /* Encoding rate in bits per second */
336 uint32_t bit_rate;
337
338 /* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS */
339 uint32_t enc_mode;
340
341 /* supported aac_fmt_flag are ADTS/RAW */
342 uint16_t aac_fmt_flag;
343
344 /* supported channel_cfg are Native mode, Mono , Stereo */
345 uint16_t channel_cfg;
346
347 /* Number of samples per second */
348 uint32_t sample_rate;
349} __attribute__ ((packed));
350
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -0700351/* AAC encoder configuration structure. */
352typedef struct aac_enc_cfg_t aac_enc_cfg_t;
353
354struct aac_enc_cfg_t {
355 struct aac_cfg_blk_t aac_cfg;
356 struct aac_frame_size_control_t frame_ctl;
357} __attribute__ ((packed));
358
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800359/* SBC encoder configuration structure. */
360typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
361
362struct sbc_enc_cfg_t {
363 /* Encoder media format for SBC */
364 uint32_t enc_format;
365
366 /* supported num_subbands are 4/8 */
367 uint32_t num_subbands;
368
369 /* supported blk_len are 4, 8, 12, 16 */
370 uint32_t blk_len;
371
372 /* supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO */
373 uint32_t channel_mode;
374
375 /* supported alloc_method are LOUNDNESS/SNR */
376 uint32_t alloc_method;
377
378 /* supported bit_rate for mono channel is max 320kbps
379 * supported bit rate for stereo channel is max 512 kbps */
380 uint32_t bit_rate;
381
382 /* Number of samples per second */
383 uint32_t sample_rate;
384} __attribute__ ((packed));
385
386struct custom_enc_cfg_t {
387 /* Custom encoder media format */
388 uint32_t enc_format;
389
390 /* Number of samples per second */
391 uint32_t sample_rate;
392
393 /* supported num_channels are Mono/Stereo */
394 uint16_t num_channels;
395
396 /* Reserved for future enhancement */
397 uint16_t reserved;
398
399 /* supported channel_mapping for mono is CHANNEL_C
400 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R */
401 uint8_t channel_mapping[8];
402
403 /* Reserved for future enhancement */
404 uint32_t custom_size;
405} __attribute__ ((packed));
406
407struct aptx_v2_enc_cfg_ext_t {
408/* sync_mode introduced with APTX V2 libraries
409 * sync mode: 0x0 = stereo sync mode
410 * 0x01 = dual mono sync mode
411 * 0x02 = dual mono with no sync on either L or R codewords
412 */
413 uint32_t sync_mode;
414} __attribute__ ((packed));
415
416/* APTX struct for combining custom enc and V2 members */
417struct aptx_enc_cfg_t {
418 struct custom_enc_cfg_t custom_cfg;
419 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
420} __attribute__ ((packed));
421
422struct ldac_specific_enc_cfg_t {
423 /*
424 * This is used to calculate the encoder output
425 * bytes per frame (i.e. bytes per packet).
426 * Bit rate also configures the EQMID.
427 * The min bit rate 303000 bps is calculated for
428 * 44.1 kHz and 88.2 KHz sampling frequencies with
429 * Mobile use Quality.
430 * The max bit rate of 990000 bps is calculated for
431 * 96kHz and 48 KHz with High Quality
432 * @Range(in bits per second)
433 * 303000 for Mobile use Quality
434 * 606000 for standard Quality
435 * 909000 for High Quality
436 */
437 uint32_t bit_rate;
438
439 /*
440 * The channel setting information for LDAC specification
441 * of Bluetooth A2DP which is determined by SRC and SNK
442 * devices in Bluetooth transmission.
443 * @Range:
444 * 0 for native mode
445 * 4 for mono
446 * 2 for dual channel
447 * 1 for stereo
448 */
449 uint16_t channel_mode;
450
451 /*
452 * Maximum Transmission Unit (MTU).
453 * The minimum MTU that a L2CAP implementation for LDAC shall
454 * support is 679 bytes, because LDAC is optimized with 2-DH5
455 * packet as its target.
456 * @Range : 679
457 * @Default: 679 for LDACBT_MTU_2DH5
458 */
459 uint16_t mtu;
460} __attribute__ ((packed));
461
462/* LDAC struct for combining custom enc and standard members */
463struct ldac_enc_cfg_t {
464 struct custom_enc_cfg_t custom_cfg;
465 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700466 struct abr_enc_cfg_t abr_cfg;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800467} __attribute__ ((packed));
468
469/* Information about Bluetooth SBC encoder configuration
470 * This data is used between audio HAL module and
471 * Bluetooth IPC library to configure DSP encoder
472 */
473typedef struct {
474 uint32_t subband; /* 4, 8 */
475 uint32_t blk_len; /* 4, 8, 12, 16 */
476 uint16_t sampling_rate; /* 44.1khz, 48khz */
477 uint8_t channels; /* 0(Mono), 1(Dual_mono), 2(Stereo), 3(JS) */
478 uint8_t alloc; /* 0(Loudness), 1(SNR) */
479 uint8_t min_bitpool; /* 2 */
480 uint8_t max_bitpool; /* 53(44.1khz), 51 (48khz) */
481 uint32_t bitrate; /* 320kbps to 512kbps */
482 uint32_t bits_per_sample; /* 16 bit */
483} audio_sbc_encoder_config;
484
485/* Information about Bluetooth APTX encoder configuration
486 * This data is used between audio HAL module and
487 * Bluetooth IPC library to configure DSP encoder
488 */
489typedef struct {
490 uint16_t sampling_rate;
491 uint8_t channels;
492 uint32_t bitrate;
493 uint32_t bits_per_sample;
494} audio_aptx_default_config;
495
496typedef struct {
497 uint16_t sampling_rate;
498 uint8_t channels;
499 uint32_t bitrate;
500 uint32_t sync_mode;
501} audio_aptx_dual_mono_config;
502
503typedef union {
504 audio_aptx_default_config *default_cfg;
505 audio_aptx_dual_mono_config *dual_mono_cfg;
506} audio_aptx_encoder_config;
507
508/* Information about Bluetooth AAC encoder configuration
509 * This data is used between audio HAL module and
510 * Bluetooth IPC library to configure DSP encoder
511 */
512typedef struct {
513 uint32_t enc_mode; /* LC, SBR, PS */
514 uint16_t format_flag; /* RAW, ADTS */
515 uint16_t channels; /* 1-Mono, 2-Stereo */
516 uint32_t sampling_rate;
517 uint32_t bitrate;
518 uint32_t bits_per_sample;
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -0700519 struct aac_frame_size_control_t frame_ctl;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800520} audio_aac_encoder_config;
521
522/* Information about Bluetooth LDAC encoder configuration
523 * This data is used between audio HAL module and
524 * Bluetooth IPC library to configure DSP encoder
525 */
526typedef struct {
527 uint32_t sampling_rate; /* 44100, 48000, 88200, 96000 */
528 uint32_t bit_rate; /* 303000, 606000, 909000 (in bits per second) */
529 uint16_t channel_mode; /* 0, 4, 2, 1 */
530 uint16_t mtu;
531 uint32_t bits_per_sample; /* 16, 24, 32 (bits) */
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700532 bool is_abr_enabled;
533 struct quality_level_to_bitrate_info level_to_bitrate_map;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800534} audio_ldac_encoder_config;
535
536/*********** END of DSP configurable structures ********************/
537
538static void a2dp_common_init()
539{
540 a2dp.a2dp_started = false;
541 a2dp.a2dp_total_active_session_request = 0;
542 a2dp.a2dp_suspended = false;
543 a2dp.bt_encoder_format = ENC_CODEC_TYPE_INVALID;
544 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700545 a2dp.abr_config.is_abr_enabled = false;
546 a2dp.abr_config.abr_started = false;
547 a2dp.abr_config.imc_instance = 0;
548 a2dp.abr_config.abr_tx_handle = NULL;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800549}
550
551static void update_offload_codec_support()
552{
Petri Gynther2479b0b2018-04-17 18:38:47 -0700553 a2dp.is_a2dp_offload_enabled =
554 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
555 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
556
557 ALOGD("%s: A2DP offload enabled = %d", __func__,
558 a2dp.is_a2dp_offload_enabled);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700559}
560
561static int stop_abr()
562{
563 struct mixer_ctl *ctl_abr_tx_path = NULL;
564 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
565
566 /* This function can be used if !abr_started for clean up */
567 ALOGV("%s: enter", __func__);
568
569 // Close hostless front end
570 if (a2dp.abr_config.abr_tx_handle != NULL) {
571 pcm_close(a2dp.abr_config.abr_tx_handle);
572 a2dp.abr_config.abr_tx_handle = NULL;
573 }
574 a2dp.abr_config.abr_started = false;
575 a2dp.abr_config.imc_instance = 0;
576
577 // Reset BT driver mixer control for ABR usecase
578 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
579 MIXER_SET_FEEDBACK_CHANNEL);
580 if (!ctl_set_bt_feedback_channel) {
581 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
582 return -ENOSYS;
583 }
584 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
585 ALOGE("%s: Failed to set BT usecase", __func__);
586 return -ENOSYS;
587 }
588
589 // Reset ABR Tx feedback path
590 ALOGV("%s: Disable ABR Tx feedback path", __func__);
591 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
592 MIXER_ABR_TX_FEEDBACK_PATH);
593 if (!ctl_abr_tx_path) {
594 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
595 return -ENOSYS;
596 }
597 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
598 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
599 return -ENOSYS;
600 }
601
602 return 0;
603}
604
605static int start_abr()
606{
607 struct mixer_ctl *ctl_abr_tx_path = NULL;
608 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
609 int abr_device_id;
610 int ret = 0;
611
612 if (!a2dp.abr_config.is_abr_enabled) {
613 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
614 return -ENOSYS;
615 }
616
617 if (a2dp.abr_config.abr_started) {
618 ALOGI("%s: ABR has already started", __func__);
619 return ret;
620 }
621
622 // Enable Slimbus 7 Tx feedback path
623 ALOGV("%s: Enable ABR Tx feedback path", __func__);
624 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
625 MIXER_ABR_TX_FEEDBACK_PATH);
626 if (!ctl_abr_tx_path) {
627 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
628 return -ENOSYS;
629 }
630 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
631 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
632 return -ENOSYS;
633 }
634
635 // Notify ABR usecase information to BT driver to distinguish
636 // between SCO and feedback usecase
637 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
638 MIXER_SET_FEEDBACK_CHANNEL);
639 if (!ctl_set_bt_feedback_channel) {
640 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
641 return -ENOSYS;
642 }
643 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
644 ALOGE("%s: Failed to set BT usecase", __func__);
645 return -ENOSYS;
646 }
647
648 // Open hostless front end and prepare ABR Tx path
649 abr_device_id = platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
650 PCM_CAPTURE);
651 if (!a2dp.abr_config.abr_tx_handle) {
652 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
653 abr_device_id, PCM_IN,
654 &pcm_config_abr);
655 if (a2dp.abr_config.abr_tx_handle == NULL ||
656 !pcm_is_ready(a2dp.abr_config.abr_tx_handle))
657 goto fail;
658 }
659 ret = pcm_start(a2dp.abr_config.abr_tx_handle);
660 if (ret < 0)
661 goto fail;
662 a2dp.abr_config.abr_started = true;
663
664 return ret;
665
666fail:
667 ALOGE("%s: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
668 stop_abr();
669 return -ENOSYS;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800670}
671
672/* API to open Bluetooth IPC library to start IPC communication */
673static int open_a2dp_output()
674{
675 int ret = 0;
676
677 ALOGD("%s: Open A2DP output start", __func__);
678
679 if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
680 ALOGD("%s: Called A2DP open with improper state, Ignoring request state %d",
681 __func__, a2dp.bt_state);
682 return -ENOSYS;
683 }
684
685 if (a2dp.bt_lib_handle == NULL) {
686 ALOGD("%s: Requesting for Bluetooth IPC lib handle", __func__);
687 a2dp.bt_lib_handle = dlopen(BT_IPC_LIB_NAME, RTLD_NOW);
688
689 if (a2dp.bt_lib_handle == NULL) {
690 ret = -errno;
691 ALOGE("%s: DLOPEN failed for %s errno %d strerror %s", __func__,
692 BT_IPC_LIB_NAME, errno, strerror(errno));
693 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
694 return ret;
695 } else {
696 a2dp.audio_stream_open = (audio_stream_open_t)
697 dlsym(a2dp.bt_lib_handle, "audio_stream_open");
698 a2dp.audio_stream_start = (audio_stream_start_t)
699 dlsym(a2dp.bt_lib_handle, "audio_stream_start");
700 a2dp.audio_get_codec_config = (audio_get_codec_config_t)
701 dlsym(a2dp.bt_lib_handle, "audio_get_codec_config");
702 a2dp.audio_stream_suspend = (audio_stream_suspend_t)
703 dlsym(a2dp.bt_lib_handle, "audio_stream_suspend");
704 a2dp.audio_handoff_triggered = (audio_handoff_triggered_t)
705 dlsym(a2dp.bt_lib_handle, "audio_handoff_triggered");
706 a2dp.clear_a2dp_suspend_flag = (clear_a2dp_suspend_flag_t)
707 dlsym(a2dp.bt_lib_handle, "clear_a2dp_suspend_flag");
708 a2dp.audio_stream_stop = (audio_stream_stop_t)
709 dlsym(a2dp.bt_lib_handle, "audio_stream_stop");
710 a2dp.audio_stream_close = (audio_stream_close_t)
711 dlsym(a2dp.bt_lib_handle, "audio_stream_close");
712 a2dp.audio_check_a2dp_ready = (audio_check_a2dp_ready_t)
713 dlsym(a2dp.bt_lib_handle,"audio_check_a2dp_ready");
714 a2dp.audio_is_scrambling_enabled = (audio_is_scrambling_enabled_t)
715 dlsym(a2dp.bt_lib_handle,"audio_is_scrambling_enabled");
716 }
717 }
718
719 if (a2dp.bt_lib_handle && a2dp.audio_stream_open) {
720 ALOGD("%s: calling Bluetooth stream open", __func__);
721 ret = a2dp.audio_stream_open();
722 if (ret != 0) {
723 ALOGE("%s: Failed to open output stream for A2DP: status %d", __func__, ret);
724 dlclose(a2dp.bt_lib_handle);
725 a2dp.bt_lib_handle = NULL;
726 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
727 return ret;
728 }
729 a2dp.bt_state = A2DP_STATE_CONNECTED;
730 } else {
731 ALOGE("%s: A2DP handle is not identified, Ignoring open request", __func__);
732 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
733 return -ENOSYS;
734 }
735
736 return ret;
737}
738
739static int close_a2dp_output()
740{
741 ALOGV("%s\n",__func__);
742 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_close)) {
743 ALOGE("%s: A2DP handle is not identified, Ignoring close request", __func__);
744 return -ENOSYS;
745 }
746 if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
747 ALOGD("%s: calling Bluetooth stream close", __func__);
748 if (a2dp.audio_stream_close() == false)
749 ALOGE("%s: failed close A2DP control path from Bluetooth IPC library", __func__);
750 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700751 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
752 stop_abr();
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800753 a2dp_common_init();
754 a2dp.enc_sampling_rate = 0;
755 a2dp.enc_channels = 0;
756
757 return 0;
758}
759
760static int a2dp_check_and_set_scrambler()
761{
762 bool scrambler_mode = false;
763 struct mixer_ctl *ctrl_scrambler_mode = NULL;
764 int ret = 0;
765 if (a2dp.audio_is_scrambling_enabled && (a2dp.bt_state != A2DP_STATE_DISCONNECTED))
766 scrambler_mode = a2dp.audio_is_scrambling_enabled();
767
768 // Scrambling needs to be enabled in DSP if scrambler mode is set
769 // disable scrambling not required
770 if (scrambler_mode) {
771 // enable scrambler in dsp
772 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
773 MIXER_SCRAMBLER_MODE);
774 if (!ctrl_scrambler_mode) {
775 ALOGE("%s: ERROR scrambler mode mixer control not identifed", __func__);
776 return -ENOSYS;
777 } else {
778 ret = mixer_ctl_set_value(ctrl_scrambler_mode, 0, true);
779 if (ret != 0) {
780 ALOGE("%s: Could not set scrambler mode", __func__);
781 return ret;
782 }
783 }
784 }
785 return 0;
786}
787
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700788static int a2dp_set_backend_cfg()
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800789{
790 const char *rate_str = NULL, *in_channels = NULL;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700791 uint32_t sampling_rate_rx = a2dp.enc_sampling_rate;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800792 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL;
793
794 // For LDAC encoder open slimbus port at 96Khz for 48Khz input
795 // and 88.2Khz for 44.1Khz input.
796 if ((a2dp.bt_encoder_format == ENC_CODEC_TYPE_LDAC) &&
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700797 (sampling_rate_rx == 48000 || sampling_rate_rx == 44100 )) {
798 sampling_rate_rx *= 2;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800799 }
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +0530800 // No need to configure backend for PCM format.
801 if (a2dp.bt_encoder_format == ENC_CODEC_TYPE_PCM) {
802 return 0;
803 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700804 // Set Rx backend sample rate
805 switch (sampling_rate_rx) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800806 case 44100:
807 rate_str = "KHZ_44P1";
808 break;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800809 case 88200:
810 rate_str = "KHZ_88P2";
811 break;
812 case 96000:
813 rate_str = "KHZ_96";
814 break;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700815 case 48000:
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800816 default:
817 rate_str = "KHZ_48";
818 break;
819 }
820
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700821 ALOGV("%s: set backend rx sample rate = %s", __func__, rate_str);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800822 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700823 MIXER_SAMPLE_RATE_RX);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800824 if (!ctl_sample_rate) {
825 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700826 return -ENOSYS;
827 }
828 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
829 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
830 return -ENOSYS;
831 }
832
833 // Set Tx backend sample rate
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -0700834 if (a2dp.abr_config.is_abr_enabled) {
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700835 rate_str = ABR_TX_SAMPLE_RATE;
836
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -0700837 ALOGV("%s: set backend tx sample rate = %s", __func__, rate_str);
838 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
839 MIXER_SAMPLE_RATE_TX);
840 if (!ctl_sample_rate) {
841 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
842 return -ENOSYS;
843 }
844 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
845 ALOGE("%s: Failed to set backend sample rate = %s",
846 __func__, rate_str);
847 return -ENOSYS;
848 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800849 }
850
851 // Configure AFE input channels
852 switch (a2dp.enc_channels) {
853 case 1:
854 in_channels = "One";
855 break;
856 case 2:
857 default:
858 in_channels = "Two";
859 break;
860 }
861
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700862 ALOGV("%s: set AFE input channels = %d", __func__, a2dp.enc_channels);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800863 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
864 MIXER_AFE_IN_CHANNELS);
865 if (!ctrl_in_channels) {
866 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700867 return -ENOSYS;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800868 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700869 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
870 ALOGE("%s: Failed to set AFE in channels = %d", __func__, a2dp.enc_channels);
871 return -ENOSYS;
872 }
873
874 return 0;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800875}
876
877static int a2dp_set_bit_format(uint32_t enc_bit_format)
878{
879 const char *bit_format = NULL;
880 struct mixer_ctl *ctrl_bit_format = NULL;
881
882 // Configure AFE Input Bit Format
883 switch (enc_bit_format) {
884 case 32:
885 bit_format = "S32_LE";
886 break;
887 case 24:
888 bit_format = "S24_LE";
889 break;
890 case 16:
891 default:
892 bit_format = "S16_LE";
893 break;
894 }
895
896 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
897 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
898 MIXER_ENC_BIT_FORMAT);
899 if (!ctrl_bit_format) {
900 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
901 return -ENOSYS;
902 }
903 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
904 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
905 return -ENOSYS;
906 }
907 return 0;
908}
909
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700910static int a2dp_reset_backend_cfg()
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800911{
912 const char *rate_str = "KHZ_8", *in_channels = "Zero";
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700913 struct mixer_ctl *ctl_sample_rate_rx = NULL, *ctl_sample_rate_tx = NULL;
914 struct mixer_ctl *ctrl_in_channels = NULL;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800915
916 // Reset backend sampling rate
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700917 ALOGV("%s: reset backend sample rate = %s", __func__, rate_str);
918 ctl_sample_rate_rx = mixer_get_ctl_by_name(a2dp.adev->mixer,
919 MIXER_SAMPLE_RATE_RX);
920 if (!ctl_sample_rate_rx) {
921 ALOGE("%s: ERROR Rx backend sample rate mixer control not identifed", __func__);
922 return -ENOSYS;
923 }
924 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_rx, rate_str) != 0) {
925 ALOGE("%s: Failed to reset Rx backend sample rate = %s", __func__, rate_str);
926 return -ENOSYS;
927 }
928
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -0700929 if (a2dp.abr_config.is_abr_enabled) {
930 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
931 MIXER_SAMPLE_RATE_TX);
932 if (!ctl_sample_rate_tx) {
933 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
934 return -ENOSYS;
935 }
936 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
937 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
938 return -ENOSYS;
939 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800940 }
941
942 // Reset AFE input channels
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700943 ALOGV("%s: reset AFE input channels = %s", __func__, in_channels);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800944 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
945 MIXER_AFE_IN_CHANNELS);
946 if (!ctrl_in_channels) {
947 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700948 return -ENOSYS;
949 }
950 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
951 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
952 return -ENOSYS;
953 }
954
955 return 0;
956}
957
958/* API to configure AFE decoder in DSP */
959static bool configure_a2dp_decoder_format(int dec_format)
960{
961 struct mixer_ctl *ctl_dec_data = NULL;
962 struct abr_dec_cfg_t dec_cfg;
963 int ret = 0;
964
965 if (a2dp.abr_config.is_abr_enabled) {
966 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_DEC_CONFIG_BLOCK);
967 if (!ctl_dec_data) {
968 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
969 return false;
970 }
971 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
972 dec_cfg.dec_format = dec_format;
973 dec_cfg.imc_info.direction = IMC_TRANSMIT;
974 dec_cfg.imc_info.enable = IMC_ENABLE;
975 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
976 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
977
978 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dec_cfg,
979 sizeof(dec_cfg));
980 if (ret != 0) {
981 ALOGE("%s: Failed to set decoder config", __func__);
982 return false;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800983 }
984 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700985
986 return true;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800987}
988
989/* API to configure SBC DSP encoder */
990static bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
991{
992 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
993 struct sbc_enc_cfg_t sbc_dsp_cfg;
994 bool is_configured = false;
995 int ret = 0;
996
997 if (sbc_bt_cfg == NULL) {
998 ALOGE("%s: Failed to get SBC encoder config from BT", __func__);
999 return false;
1000 }
1001
1002 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1003 if (!ctl_enc_data) {
1004 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1005 is_configured = false;
1006 goto exit;
1007 }
1008 memset(&sbc_dsp_cfg, 0x0, sizeof(sbc_dsp_cfg));
1009 sbc_dsp_cfg.enc_format = ENC_MEDIA_FMT_SBC;
1010 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1011 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1012 switch (sbc_bt_cfg->channels) {
1013 case 0:
1014 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1015 break;
1016 case 1:
1017 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1018 break;
1019 case 3:
1020 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1021 break;
1022 case 2:
1023 default:
1024 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1025 break;
1026 }
1027 if (sbc_bt_cfg->alloc)
1028 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1029 else
1030 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1031 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1032 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1033 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1034 sizeof(sbc_dsp_cfg));
1035 if (ret != 0) {
1036 ALOGE("%s: failed to set SBC encoder config", __func__);
1037 is_configured = false;
1038 goto exit;
1039 }
1040 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
1041 if (ret != 0) {
1042 is_configured = false;
1043 goto exit;
1044 }
1045 is_configured = true;
1046 a2dp.bt_encoder_format = ENC_CODEC_TYPE_SBC;
1047 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
1048
1049 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1050 a2dp.enc_channels = 1;
1051 else
1052 a2dp.enc_channels = 2;
1053
1054 ALOGV("%s: Successfully updated SBC enc format with sampling rate: %d channel mode:%d",
1055 __func__, sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
1056exit:
1057 return is_configured;
1058}
1059
1060/* API to configure APTX DSP encoder */
1061static bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1062{
1063 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1064 int mixer_size;
1065 bool is_configured = false;
1066 int ret = 0;
1067 struct aptx_enc_cfg_t aptx_dsp_cfg;
1068 mixer_size = sizeof(aptx_dsp_cfg);
1069
1070 if (aptx_bt_cfg == NULL) {
1071 ALOGE("%s: Failed to get APTX encoder config from BT", __func__);
1072 return false;
1073 }
1074
1075 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1076 if (!ctl_enc_data) {
1077 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1078 is_configured = false;
1079 goto exit;
1080 }
1081
1082 memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg));
1083 aptx_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_APTX;
1084
1085 if (!a2dp.is_aptx_dual_mono_supported) {
1086 aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1087 aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1088 } else {
1089 aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1090 aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1091 aptx_dsp_cfg.aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1092 }
1093
1094 switch (aptx_dsp_cfg.custom_cfg.num_channels) {
1095 case 1:
1096 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1097 break;
1098 case 2:
1099 default:
1100 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1101 aptx_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1102 break;
1103 }
1104 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1105 mixer_size);
1106 if (ret != 0) {
1107 ALOGE("%s: Failed to set APTX encoder config", __func__);
1108 is_configured = false;
1109 goto exit;
1110 }
1111 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
1112 if (ret != 0) {
1113 is_configured = false;
1114 goto exit;
1115 }
1116 is_configured = true;
1117 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX;
1118 a2dp.enc_channels = aptx_dsp_cfg.custom_cfg.num_channels;
1119 if (!a2dp.is_aptx_dual_mono_supported) {
1120 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1121 ALOGV("%s: Successfully updated APTX enc format with sampling rate: %d \
1122 channels:%d", __func__, aptx_dsp_cfg.custom_cfg.sample_rate,
1123 aptx_dsp_cfg.custom_cfg.num_channels);
1124 } else {
1125 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1126 ALOGV("%s: Successfully updated APTX dual mono enc format with \
1127 sampling rate: %d channels:%d sync mode %d", __func__,
1128 aptx_dsp_cfg.custom_cfg.sample_rate,
1129 aptx_dsp_cfg.custom_cfg.num_channels,
1130 aptx_dsp_cfg.aptx_v2_cfg.sync_mode);
1131 }
1132
1133exit:
1134 return is_configured;
1135}
1136
1137/* API to configure APTX HD DSP encoder
1138 */
1139static bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
1140{
1141 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1142 struct custom_enc_cfg_t aptx_dsp_cfg;
1143 bool is_configured = false;
1144 int ret = 0;
1145
1146 if (aptx_bt_cfg == NULL) {
1147 ALOGE("%s: Failed to get APTX HD encoder config from BT", __func__);
1148 return false;
1149 }
1150
1151 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1152 if (!ctl_enc_data) {
1153 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1154 is_configured = false;
1155 goto exit;
1156 }
1157
1158 memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg));
1159 aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX_HD;
1160 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
1161 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
1162 switch (aptx_dsp_cfg.num_channels) {
1163 case 1:
1164 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1165 break;
1166 case 2:
1167 default:
1168 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1169 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1170 break;
1171 }
1172 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1173 sizeof(aptx_dsp_cfg));
1174 if (ret != 0) {
1175 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
1176 is_configured = false;
1177 goto exit;
1178 }
1179 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
1180 if (ret != 0) {
1181 is_configured = false;
1182 goto exit;
1183 }
1184 is_configured = true;
1185 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX_HD;
1186 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
1187 a2dp.enc_channels = aptx_bt_cfg->channels;
1188 ALOGV("%s: Successfully updated APTX HD encformat with sampling rate: %d channels:%d",
1189 __func__, aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
1190exit:
1191 return is_configured;
1192}
1193
1194/* API to configure AAC DSP encoder */
1195static bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
1196{
1197 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1198 struct aac_enc_cfg_t aac_dsp_cfg;
1199 bool is_configured = false;
1200 int ret = 0;
1201
1202 if (aac_bt_cfg == NULL) {
1203 ALOGE("%s: Failed to get AAC encoder config from BT", __func__);
1204 return false;
1205 }
1206
1207 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1208 if (!ctl_enc_data) {
1209 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1210 is_configured = false;
1211 goto exit;
1212 }
1213 memset(&aac_dsp_cfg, 0x0, sizeof(aac_dsp_cfg));
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001214 aac_dsp_cfg.aac_cfg.enc_format = ENC_MEDIA_FMT_AAC;
1215 aac_dsp_cfg.aac_cfg.bit_rate = aac_bt_cfg->bitrate;
1216 aac_dsp_cfg.aac_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001217 switch (aac_bt_cfg->enc_mode) {
1218 case 0:
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001219 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001220 break;
1221 case 2:
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001222 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001223 break;
1224 case 1:
1225 default:
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001226 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001227 break;
1228 }
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001229 aac_dsp_cfg.aac_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
1230 aac_dsp_cfg.aac_cfg.channel_cfg = aac_bt_cfg->channels;
1231 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
1232 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001233 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
1234 sizeof(aac_dsp_cfg));
1235 if (ret != 0) {
1236 ALOGE("%s: failed to set AAC encoder config", __func__);
1237 is_configured = false;
1238 goto exit;
1239 }
1240 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
1241 if (ret != 0) {
1242 is_configured = false;
1243 goto exit;
1244 }
1245 is_configured = true;
1246 a2dp.bt_encoder_format = ENC_CODEC_TYPE_AAC;
1247 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001248 a2dp.enc_channels = aac_bt_cfg->channels;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001249 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001250 __func__, aac_dsp_cfg.aac_cfg.sample_rate, aac_dsp_cfg.aac_cfg.channel_cfg);
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001251exit:
1252 return is_configured;
1253}
1254
1255static bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
1256{
1257 struct mixer_ctl *ldac_enc_data = NULL, *ctrl_bit_format = NULL;
1258 struct ldac_enc_cfg_t ldac_dsp_cfg;
1259 bool is_configured = false;
1260 int ret = 0;
1261
1262 if (ldac_bt_cfg == NULL) {
1263 ALOGE("%s: Failed to get LDAC encoder config from BT", __func__);
1264 return false;
1265 }
1266
1267 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1268 if (!ldac_enc_data) {
1269 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1270 is_configured = false;
1271 goto exit;
1272 }
1273 memset(&ldac_dsp_cfg, 0x0, sizeof(ldac_dsp_cfg));
1274
1275 ldac_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_LDAC;
1276 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
1277 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
1278 switch (ldac_dsp_cfg.ldac_cfg.channel_mode) {
1279 case 4:
1280 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1281 ldac_dsp_cfg.custom_cfg.num_channels = 1;
1282 break;
1283 case 2:
1284 case 1:
1285 default:
1286 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1287 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1288 ldac_dsp_cfg.custom_cfg.num_channels = 2;
1289 break;
1290 }
1291
1292 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(ldac_dsp_cfg);
1293 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
1294 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001295 if (ldac_bt_cfg->is_abr_enabled) {
1296 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
1297 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
1298 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
1299 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1300 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata76ff4802018-08-06 15:30:50 -07001301 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001302 }
1303
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001304 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
1305 sizeof(ldac_dsp_cfg));
1306 if (ret != 0) {
1307 ALOGE("%s: Failed to set LDAC encoder config", __func__);
1308 is_configured = false;
1309 goto exit;
1310 }
1311 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
1312 if (ret != 0) {
1313 is_configured = false;
1314 goto exit;
1315 }
1316 is_configured = true;
1317 a2dp.bt_encoder_format = ENC_CODEC_TYPE_LDAC;
1318 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
1319 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001320 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001321 ALOGV("%s: Successfully updated LDAC encformat with sampling rate: %d channels:%d",
1322 __func__, ldac_dsp_cfg.custom_cfg.sample_rate,
1323 ldac_dsp_cfg.custom_cfg.num_channels);
1324exit:
1325 return is_configured;
1326}
1327
1328bool configure_a2dp_encoder_format()
1329{
1330 void *codec_info = NULL;
1331 uint8_t multi_cast = 0, num_dev = 1;
1332 enc_codec_t codec_type = ENC_CODEC_TYPE_INVALID;
1333 bool is_configured = false;
1334 audio_aptx_encoder_config aptx_encoder_cfg;
1335
1336 if (!a2dp.audio_get_codec_config) {
1337 ALOGE("%s: A2DP handle is not identified, ignoring A2DP encoder config", __func__);
1338 return false;
1339 }
1340 ALOGD("%s: start", __func__);
1341 codec_info = a2dp.audio_get_codec_config(&multi_cast, &num_dev,
1342 &codec_type);
1343
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001344 // ABR disabled by default for all codecs
1345 a2dp.abr_config.is_abr_enabled = false;
1346
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001347 switch (codec_type) {
1348 case ENC_CODEC_TYPE_SBC:
1349 ALOGD("%s: Received SBC encoder supported Bluetooth device", __func__);
1350 is_configured =
1351 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
1352 break;
1353 case ENC_CODEC_TYPE_APTX:
1354 ALOGD("%s: Received APTX encoder supported Bluetooth device", __func__);
1355 a2dp.is_aptx_dual_mono_supported = false;
1356 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
1357 is_configured =
1358 configure_aptx_enc_format(&aptx_encoder_cfg);
1359 break;
1360 case ENC_CODEC_TYPE_APTX_HD:
1361 ALOGD("%s: Received APTX HD encoder supported Bluetooth device", __func__);
1362 is_configured =
1363 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
1364 break;
1365 case ENC_CODEC_TYPE_AAC:
1366 ALOGD("%s: Received AAC encoder supported Bluetooth device", __func__);
1367 is_configured =
1368 configure_aac_enc_format((audio_aac_encoder_config *)codec_info);
1369 break;
1370 case ENC_CODEC_TYPE_LDAC:
1371 ALOGD("%s: Received LDAC encoder supported Bluetooth device", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001372 if (!instance_id || instance_id > MAX_INSTANCE_ID)
1373 instance_id = MAX_INSTANCE_ID;
1374 a2dp.abr_config.imc_instance = instance_id--;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001375 is_configured =
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001376 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
1377 configure_a2dp_decoder_format(ENC_CODEC_TYPE_LDAC));
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001378 break;
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +05301379 case ENC_CODEC_TYPE_PCM:
1380 ALOGD("Received PCM format for BT device");
1381 a2dp.bt_encoder_format = ENC_CODEC_TYPE_PCM;
1382 is_configured = true;
1383 break;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001384 default:
1385 ALOGD("%s: Received unsupported encoder format", __func__);
1386 is_configured = false;
1387 break;
1388 }
1389 return is_configured;
1390}
1391
1392int audio_extn_a2dp_start_playback()
1393{
1394 int ret = 0;
1395
1396 ALOGD("%s: start", __func__);
1397
1398 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_start
1399 && a2dp.audio_get_codec_config)) {
1400 ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__);
1401 return -ENOSYS;
1402 }
1403
1404 if (a2dp.a2dp_suspended) {
1405 // session will be restarted after suspend completion
1406 ALOGD("%s: A2DP start requested during suspend state", __func__);
1407 return -ENOSYS;
1408 }
1409
1410 if (!a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
1411 ALOGD("%s: calling Bluetooth module stream start", __func__);
1412 /* This call indicates Bluetooth IPC lib to start playback */
1413 ret = a2dp.audio_stream_start();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001414 if (ret != 0 ) {
1415 ALOGE("%s: Bluetooth controller start failed", __func__);
1416 a2dp.a2dp_started = false;
1417 } else {
1418 if (configure_a2dp_encoder_format() == true) {
1419 a2dp.a2dp_started = true;
1420 ret = 0;
1421 ALOGD("%s: Start playback successful to Bluetooth IPC library", __func__);
1422 } else {
1423 ALOGD("%s: unable to configure DSP encoder", __func__);
1424 a2dp.a2dp_started = false;
1425 ret = -ETIMEDOUT;
1426 }
1427 }
1428 }
1429
1430 if (a2dp.a2dp_started) {
1431 a2dp.a2dp_total_active_session_request++;
1432 a2dp_check_and_set_scrambler();
1433 a2dp_set_backend_cfg();
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001434 if (a2dp.abr_config.is_abr_enabled)
1435 start_abr();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001436 }
1437
1438 ALOGD("%s: start A2DP playback total active sessions :%d", __func__,
1439 a2dp.a2dp_total_active_session_request);
1440 return ret;
1441}
1442
1443static int reset_a2dp_enc_config_params()
1444{
1445 int ret = 0;
1446
1447 struct mixer_ctl *ctl_enc_config, *ctrl_bit_format;
1448 struct sbc_enc_cfg_t dummy_reset_config;
1449
1450 memset(&dummy_reset_config, 0x0, sizeof(dummy_reset_config));
1451 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
1452 MIXER_ENC_CONFIG_BLOCK);
1453 if (!ctl_enc_config) {
1454 ALOGE("%s: ERROR A2DP encoder format mixer control not identifed", __func__);
1455 } else {
1456 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
1457 sizeof(dummy_reset_config));
1458 a2dp.bt_encoder_format = ENC_MEDIA_FMT_NONE;
1459 }
1460
1461 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
1462
1463 return ret;
1464}
1465
Aniket Kumar Lataa85e6992018-05-22 13:56:10 -07001466static int reset_a2dp_dec_config_params()
1467{
1468 struct mixer_ctl *ctl_dec_data = NULL;
1469 struct abr_dec_cfg_t dummy_reset_cfg;
1470 int ret = 0;
1471
1472 if (a2dp.abr_config.is_abr_enabled) {
1473 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_DEC_CONFIG_BLOCK);
1474 if (!ctl_dec_data) {
1475 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
1476 return -EINVAL;
1477 }
1478 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
1479 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
1480 sizeof(dummy_reset_cfg));
1481 if (ret != 0) {
1482 ALOGE("%s: Failed to set dummy decoder config", __func__);
1483 return ret;
1484 }
1485 }
1486
1487 return ret;
1488}
1489
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001490static void reset_a2dp_config() {
1491 reset_a2dp_enc_config_params();
1492 reset_a2dp_dec_config_params();
1493 a2dp_reset_backend_cfg();
1494 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1495 stop_abr();
1496 a2dp.abr_config.is_abr_enabled = false;
1497}
1498
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001499int audio_extn_a2dp_stop_playback()
1500{
1501 int ret = 0;
1502
1503 ALOGV("%s: stop", __func__);
1504 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_stop)) {
1505 ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__);
1506 return -ENOSYS;
1507 }
1508
1509 if (a2dp.a2dp_total_active_session_request > 0)
1510 a2dp.a2dp_total_active_session_request--;
1511 else
1512 ALOGE("%s: No active playback session requests on A2DP", __func__);
1513
1514 if (a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
1515 ALOGV("%s: calling Bluetooth module stream stop", __func__);
1516 ret = a2dp.audio_stream_stop();
1517 if (ret < 0)
1518 ALOGE("%s: stop stream to Bluetooth IPC lib failed", __func__);
1519 else
1520 ALOGV("%s: stop steam to Bluetooth IPC lib successful", __func__);
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001521 if (!a2dp.a2dp_suspended)
1522 reset_a2dp_config();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001523 a2dp.a2dp_started = false;
1524 }
1525 ALOGD("%s: Stop A2DP playback total active sessions :%d", __func__,
1526 a2dp.a2dp_total_active_session_request);
1527 return 0;
1528}
1529
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001530int audio_extn_a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001531{
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001532 int ret = 0, val;
Kevin Rocard702c1572018-07-11 20:52:03 -07001533 int status = 0;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001534 char value[32] = {0};
1535 struct audio_usecase *uc_info;
1536 struct listnode *node;
1537
Petri Gynther2479b0b2018-04-17 18:38:47 -07001538 if (a2dp.is_a2dp_offload_enabled == false) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001539 ALOGV("%s: No supported encoders identified,ignoring A2DP setparam", __func__);
Kevin Rocard702c1572018-07-11 20:52:03 -07001540 status = -EINVAL;
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001541 goto param_handled;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001542 }
1543
1544 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
1545 sizeof(value));
1546 if (ret >= 0) {
1547 val = atoi(value);
1548 if (audio_is_a2dp_out_device(val)) {
1549 ALOGV("%s: Received device connect request for A2DP", __func__);
1550 open_a2dp_output();
1551 }
1552 goto param_handled;
1553 }
1554
1555 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
1556 sizeof(value));
1557
1558 if (ret >= 0) {
1559 val = atoi(value);
1560 if (audio_is_a2dp_out_device(val)) {
1561 ALOGV("%s: Received device disconnect request", __func__);
1562 reset_a2dp_enc_config_params();
Aniket Kumar Lataa85e6992018-05-22 13:56:10 -07001563 reset_a2dp_dec_config_params();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001564 close_a2dp_output();
1565 }
1566 goto param_handled;
1567 }
1568
1569 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
1570 if (ret >= 0) {
1571 if (a2dp.bt_lib_handle && (a2dp.bt_state != A2DP_STATE_DISCONNECTED)) {
Jack He0e071562018-05-11 14:39:36 -07001572 if (strncmp(value, "true", sizeof(value)) == 0) {
1573 if (a2dp.a2dp_suspended) {
1574 ALOGD("%s: A2DP is already suspended", __func__);
1575 goto param_handled;
1576 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001577 ALOGD("%s: Setting A2DP to suspend state", __func__);
1578 a2dp.a2dp_suspended = true;
1579 list_for_each(node, &a2dp.adev->usecase_list) {
1580 uc_info = node_to_item(node, struct audio_usecase, list);
1581 if (uc_info->type == PCM_PLAYBACK &&
1582 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
1583 pthread_mutex_unlock(&a2dp.adev->lock);
1584 check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
1585 pthread_mutex_lock(&a2dp.adev->lock);
1586 }
1587 }
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001588 reset_a2dp_config();
Jack He0e071562018-05-11 14:39:36 -07001589 if (a2dp.audio_stream_suspend) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001590 a2dp.audio_stream_suspend();
Jack He0e071562018-05-11 14:39:36 -07001591 }
1592 } else {
1593 if (!a2dp.a2dp_suspended) {
1594 ALOGD("%s: A2DP is already unsuspended", __func__);
1595 goto param_handled;
1596 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001597 ALOGD("%s: Resetting A2DP suspend state", __func__);
1598 struct audio_usecase *uc_info;
1599 struct listnode *node;
Jack He0e071562018-05-11 14:39:36 -07001600 if (a2dp.clear_a2dp_suspend_flag) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001601 a2dp.clear_a2dp_suspend_flag();
Jack He0e071562018-05-11 14:39:36 -07001602 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001603 a2dp.a2dp_suspended = false;
1604 /*
1605 * It is possible that before suspend, A2DP sessions can be active.
1606 * For example, during music + voice activation concurrency,
1607 * A2DP suspend will be called & Bluetooth will change to SCO mode.
1608 * Though music is paused as a part of voice activation,
1609 * compress session close happens only after pause timeout(10 secs).
1610 * So, if resume request comes before pause timeout, as A2DP session
1611 * is already active, IPC start will not be called from APM/audio_hw.
1612 * Fix this by calling A2DP start for IPC library post suspend
1613 * based on number of active session count.
1614 */
1615 if (a2dp.a2dp_total_active_session_request > 0) {
1616 ALOGD("%s: Calling Bluetooth IPC lib start post suspend state", __func__);
1617 if (a2dp.audio_stream_start) {
Kevin Rocard702c1572018-07-11 20:52:03 -07001618 status = a2dp.audio_stream_start();
1619 if (status != 0) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001620 ALOGE("%s: Bluetooth controller start failed", __func__);
1621 a2dp.a2dp_started = false;
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001622 } else {
1623 if (!configure_a2dp_encoder_format()) {
1624 ALOGE("%s: Encoder params configuration failed post suspend", __func__);
1625 a2dp.a2dp_started = false;
Kevin Rocard702c1572018-07-11 20:52:03 -07001626 status = -ETIMEDOUT;
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001627 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001628 }
1629 }
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001630 if (a2dp.a2dp_started) {
1631 a2dp_set_backend_cfg();
1632 if (a2dp.abr_config.is_abr_enabled)
1633 start_abr();
1634 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001635 }
1636 list_for_each(node, &a2dp.adev->usecase_list) {
1637 uc_info = node_to_item(node, struct audio_usecase, list);
1638 if (uc_info->type == PCM_PLAYBACK &&
1639 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
1640 pthread_mutex_unlock(&a2dp.adev->lock);
1641 check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
1642 pthread_mutex_lock(&a2dp.adev->lock);
1643 }
1644 }
1645 }
1646 }
1647 goto param_handled;
1648 }
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001649
1650 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
1651 sizeof(value));
1652 if (ret >= 0) {
1653 if (a2dp.is_a2dp_offload_enabled &&
1654 a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
1655 *reconfig = true;
1656 }
1657 goto param_handled;
1658 }
1659
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001660param_handled:
1661 ALOGV("%s: end of A2DP setparam", __func__);
Kevin Rocard702c1572018-07-11 20:52:03 -07001662 return status;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001663}
1664
1665void audio_extn_a2dp_set_handoff_mode(bool is_on)
1666{
1667 a2dp.is_handoff_in_progress = is_on;
1668}
1669
1670bool audio_extn_a2dp_is_force_device_switch()
1671{
1672 // During encoder reconfiguration mode, force A2DP device switch
1673 // Or if A2DP device is selected but earlier start failed as A2DP
1674 // was suspended, force retry.
1675 return a2dp.is_handoff_in_progress || !a2dp.a2dp_started;
1676}
1677
1678void audio_extn_a2dp_get_sample_rate(int *sample_rate)
1679{
1680 *sample_rate = a2dp.enc_sampling_rate;
1681}
1682
1683bool audio_extn_a2dp_is_ready()
1684{
1685 bool ret = false;
1686
1687 if (a2dp.a2dp_suspended)
1688 goto exit;
1689
1690 if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) &&
Petri Gynther2479b0b2018-04-17 18:38:47 -07001691 (a2dp.is_a2dp_offload_enabled) &&
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001692 (a2dp.audio_check_a2dp_ready))
1693 ret = a2dp.audio_check_a2dp_ready();
1694
1695exit:
1696 return ret;
1697}
1698
1699bool audio_extn_a2dp_is_suspended()
1700{
1701 return a2dp.a2dp_suspended;
1702}
1703
1704void audio_extn_a2dp_init(void *adev)
1705{
1706 a2dp.adev = (struct audio_device*)adev;
1707 a2dp.bt_lib_handle = NULL;
1708 a2dp_common_init();
1709 a2dp.enc_sampling_rate = 48000;
Petri Gynther2479b0b2018-04-17 18:38:47 -07001710 a2dp.is_a2dp_offload_enabled = false;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001711 a2dp.is_handoff_in_progress = false;
1712 a2dp.is_aptx_dual_mono_supported = false;
1713 reset_a2dp_enc_config_params();
Aniket Kumar Lataa85e6992018-05-22 13:56:10 -07001714 reset_a2dp_dec_config_params();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001715 update_offload_codec_support();
1716}
1717
1718uint32_t audio_extn_a2dp_get_encoder_latency()
1719{
1720 uint32_t latency = 0;
1721 int avsync_runtime_prop = 0;
1722 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
1723 aac_offset = 0, ldac_offset = 0;
1724 char value[PROPERTY_VALUE_MAX];
1725
1726 memset(value, '\0', sizeof(char) * PROPERTY_VALUE_MAX);
1727 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
1728 if (avsync_runtime_prop > 0) {
1729 if (sscanf(value, "%d/%d/%d/%d/%d",
1730 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset,
1731 &ldac_offset) != 5) {
1732 ALOGI("%s: Failed to parse avsync offset params from '%s'.", __func__, value);
1733 avsync_runtime_prop = 0;
1734 }
1735 }
1736
1737 switch (a2dp.bt_encoder_format) {
1738 case ENC_CODEC_TYPE_SBC:
1739 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
1740 latency += DEFAULT_SINK_LATENCY_SBC;
1741 break;
1742 case ENC_CODEC_TYPE_APTX:
1743 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
1744 latency += DEFAULT_SINK_LATENCY_APTX;
1745 break;
1746 case ENC_CODEC_TYPE_APTX_HD:
1747 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
1748 latency += DEFAULT_SINK_LATENCY_APTX_HD;
1749 break;
1750 case ENC_CODEC_TYPE_AAC:
1751 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
1752 latency += DEFAULT_SINK_LATENCY_AAC;
1753 break;
1754 case ENC_CODEC_TYPE_LDAC:
1755 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
1756 latency += DEFAULT_SINK_LATENCY_LDAC;
1757 break;
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +05301758 case ENC_CODEC_TYPE_PCM:
1759 latency = ENCODER_LATENCY_PCM;
1760 latency += DEFAULT_SINK_LATENCY_PCM;
1761 break;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001762 default:
1763 latency = DEFAULT_ENCODER_LATENCY;
1764 break;
1765 }
1766 return latency;
1767}
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001768
1769int audio_extn_a2dp_get_parameters(struct str_parms *query,
1770 struct str_parms *reply)
1771{
1772 int ret, val = 0;
1773 char value[32]={0};
1774
1775 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
1776 value, sizeof(value));
1777 if (ret >= 0) {
1778 val = a2dp.is_a2dp_offload_enabled;
1779 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
1780 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
1781 }
1782
1783 return 0;
1784}
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001785#endif // A2DP_OFFLOAD_ENABLED