blob: 671fe59335492b5f0396b3793284614fe6d959ad [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 }
751 a2dp_common_init();
752 a2dp.enc_sampling_rate = 0;
753 a2dp.enc_channels = 0;
754
755 return 0;
756}
757
758static int a2dp_check_and_set_scrambler()
759{
760 bool scrambler_mode = false;
761 struct mixer_ctl *ctrl_scrambler_mode = NULL;
762 int ret = 0;
763 if (a2dp.audio_is_scrambling_enabled && (a2dp.bt_state != A2DP_STATE_DISCONNECTED))
764 scrambler_mode = a2dp.audio_is_scrambling_enabled();
765
766 // Scrambling needs to be enabled in DSP if scrambler mode is set
767 // disable scrambling not required
768 if (scrambler_mode) {
769 // enable scrambler in dsp
770 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
771 MIXER_SCRAMBLER_MODE);
772 if (!ctrl_scrambler_mode) {
773 ALOGE("%s: ERROR scrambler mode mixer control not identifed", __func__);
774 return -ENOSYS;
775 } else {
776 ret = mixer_ctl_set_value(ctrl_scrambler_mode, 0, true);
777 if (ret != 0) {
778 ALOGE("%s: Could not set scrambler mode", __func__);
779 return ret;
780 }
781 }
782 }
783 return 0;
784}
785
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700786static int a2dp_set_backend_cfg()
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800787{
788 const char *rate_str = NULL, *in_channels = NULL;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700789 uint32_t sampling_rate_rx = a2dp.enc_sampling_rate;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800790 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL;
791
792 // For LDAC encoder open slimbus port at 96Khz for 48Khz input
793 // and 88.2Khz for 44.1Khz input.
794 if ((a2dp.bt_encoder_format == ENC_CODEC_TYPE_LDAC) &&
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700795 (sampling_rate_rx == 48000 || sampling_rate_rx == 44100 )) {
796 sampling_rate_rx *= 2;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800797 }
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +0530798 // No need to configure backend for PCM format.
799 if (a2dp.bt_encoder_format == ENC_CODEC_TYPE_PCM) {
800 return 0;
801 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700802 // Set Rx backend sample rate
803 switch (sampling_rate_rx) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800804 case 44100:
805 rate_str = "KHZ_44P1";
806 break;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800807 case 88200:
808 rate_str = "KHZ_88P2";
809 break;
810 case 96000:
811 rate_str = "KHZ_96";
812 break;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700813 case 48000:
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800814 default:
815 rate_str = "KHZ_48";
816 break;
817 }
818
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700819 ALOGV("%s: set backend rx sample rate = %s", __func__, rate_str);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800820 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700821 MIXER_SAMPLE_RATE_RX);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800822 if (!ctl_sample_rate) {
823 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700824 return -ENOSYS;
825 }
826 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
827 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
828 return -ENOSYS;
829 }
830
831 // Set Tx backend sample rate
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -0700832 if (a2dp.abr_config.is_abr_enabled) {
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700833 rate_str = ABR_TX_SAMPLE_RATE;
834
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -0700835 ALOGV("%s: set backend tx sample rate = %s", __func__, rate_str);
836 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
837 MIXER_SAMPLE_RATE_TX);
838 if (!ctl_sample_rate) {
839 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
840 return -ENOSYS;
841 }
842 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
843 ALOGE("%s: Failed to set backend sample rate = %s",
844 __func__, rate_str);
845 return -ENOSYS;
846 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800847 }
848
849 // Configure AFE input channels
850 switch (a2dp.enc_channels) {
851 case 1:
852 in_channels = "One";
853 break;
854 case 2:
855 default:
856 in_channels = "Two";
857 break;
858 }
859
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700860 ALOGV("%s: set AFE input channels = %d", __func__, a2dp.enc_channels);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800861 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
862 MIXER_AFE_IN_CHANNELS);
863 if (!ctrl_in_channels) {
864 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700865 return -ENOSYS;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800866 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700867 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
868 ALOGE("%s: Failed to set AFE in channels = %d", __func__, a2dp.enc_channels);
869 return -ENOSYS;
870 }
871
872 return 0;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800873}
874
875static int a2dp_set_bit_format(uint32_t enc_bit_format)
876{
877 const char *bit_format = NULL;
878 struct mixer_ctl *ctrl_bit_format = NULL;
879
880 // Configure AFE Input Bit Format
881 switch (enc_bit_format) {
882 case 32:
883 bit_format = "S32_LE";
884 break;
885 case 24:
886 bit_format = "S24_LE";
887 break;
888 case 16:
889 default:
890 bit_format = "S16_LE";
891 break;
892 }
893
894 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
895 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
896 MIXER_ENC_BIT_FORMAT);
897 if (!ctrl_bit_format) {
898 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
899 return -ENOSYS;
900 }
901 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
902 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
903 return -ENOSYS;
904 }
905 return 0;
906}
907
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700908static int a2dp_reset_backend_cfg()
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800909{
910 const char *rate_str = "KHZ_8", *in_channels = "Zero";
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700911 struct mixer_ctl *ctl_sample_rate_rx = NULL, *ctl_sample_rate_tx = NULL;
912 struct mixer_ctl *ctrl_in_channels = NULL;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800913
914 // Reset backend sampling rate
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700915 ALOGV("%s: reset backend sample rate = %s", __func__, rate_str);
916 ctl_sample_rate_rx = mixer_get_ctl_by_name(a2dp.adev->mixer,
917 MIXER_SAMPLE_RATE_RX);
918 if (!ctl_sample_rate_rx) {
919 ALOGE("%s: ERROR Rx backend sample rate mixer control not identifed", __func__);
920 return -ENOSYS;
921 }
922 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_rx, rate_str) != 0) {
923 ALOGE("%s: Failed to reset Rx backend sample rate = %s", __func__, rate_str);
924 return -ENOSYS;
925 }
926
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -0700927 if (a2dp.abr_config.is_abr_enabled) {
928 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
929 MIXER_SAMPLE_RATE_TX);
930 if (!ctl_sample_rate_tx) {
931 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
932 return -ENOSYS;
933 }
934 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
935 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
936 return -ENOSYS;
937 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800938 }
939
940 // Reset AFE input channels
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700941 ALOGV("%s: reset AFE input channels = %s", __func__, in_channels);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800942 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
943 MIXER_AFE_IN_CHANNELS);
944 if (!ctrl_in_channels) {
945 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700946 return -ENOSYS;
947 }
948 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
949 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
950 return -ENOSYS;
951 }
952
953 return 0;
954}
955
956/* API to configure AFE decoder in DSP */
957static bool configure_a2dp_decoder_format(int dec_format)
958{
959 struct mixer_ctl *ctl_dec_data = NULL;
960 struct abr_dec_cfg_t dec_cfg;
961 int ret = 0;
962
963 if (a2dp.abr_config.is_abr_enabled) {
964 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_DEC_CONFIG_BLOCK);
965 if (!ctl_dec_data) {
966 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
967 return false;
968 }
969 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
970 dec_cfg.dec_format = dec_format;
971 dec_cfg.imc_info.direction = IMC_TRANSMIT;
972 dec_cfg.imc_info.enable = IMC_ENABLE;
973 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
974 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
975
976 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dec_cfg,
977 sizeof(dec_cfg));
978 if (ret != 0) {
979 ALOGE("%s: Failed to set decoder config", __func__);
980 return false;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800981 }
982 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700983
984 return true;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800985}
986
987/* API to configure SBC DSP encoder */
988static bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
989{
990 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
991 struct sbc_enc_cfg_t sbc_dsp_cfg;
992 bool is_configured = false;
993 int ret = 0;
994
995 if (sbc_bt_cfg == NULL) {
996 ALOGE("%s: Failed to get SBC encoder config from BT", __func__);
997 return false;
998 }
999
1000 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1001 if (!ctl_enc_data) {
1002 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1003 is_configured = false;
1004 goto exit;
1005 }
1006 memset(&sbc_dsp_cfg, 0x0, sizeof(sbc_dsp_cfg));
1007 sbc_dsp_cfg.enc_format = ENC_MEDIA_FMT_SBC;
1008 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1009 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1010 switch (sbc_bt_cfg->channels) {
1011 case 0:
1012 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1013 break;
1014 case 1:
1015 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1016 break;
1017 case 3:
1018 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1019 break;
1020 case 2:
1021 default:
1022 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1023 break;
1024 }
1025 if (sbc_bt_cfg->alloc)
1026 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1027 else
1028 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1029 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1030 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1031 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1032 sizeof(sbc_dsp_cfg));
1033 if (ret != 0) {
1034 ALOGE("%s: failed to set SBC encoder config", __func__);
1035 is_configured = false;
1036 goto exit;
1037 }
1038 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
1039 if (ret != 0) {
1040 is_configured = false;
1041 goto exit;
1042 }
1043 is_configured = true;
1044 a2dp.bt_encoder_format = ENC_CODEC_TYPE_SBC;
1045 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
1046
1047 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1048 a2dp.enc_channels = 1;
1049 else
1050 a2dp.enc_channels = 2;
1051
1052 ALOGV("%s: Successfully updated SBC enc format with sampling rate: %d channel mode:%d",
1053 __func__, sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
1054exit:
1055 return is_configured;
1056}
1057
1058/* API to configure APTX DSP encoder */
1059static bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1060{
1061 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1062 int mixer_size;
1063 bool is_configured = false;
1064 int ret = 0;
1065 struct aptx_enc_cfg_t aptx_dsp_cfg;
1066 mixer_size = sizeof(aptx_dsp_cfg);
1067
1068 if (aptx_bt_cfg == NULL) {
1069 ALOGE("%s: Failed to get APTX encoder config from BT", __func__);
1070 return false;
1071 }
1072
1073 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1074 if (!ctl_enc_data) {
1075 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1076 is_configured = false;
1077 goto exit;
1078 }
1079
1080 memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg));
1081 aptx_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_APTX;
1082
1083 if (!a2dp.is_aptx_dual_mono_supported) {
1084 aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1085 aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1086 } else {
1087 aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1088 aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1089 aptx_dsp_cfg.aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1090 }
1091
1092 switch (aptx_dsp_cfg.custom_cfg.num_channels) {
1093 case 1:
1094 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1095 break;
1096 case 2:
1097 default:
1098 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1099 aptx_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1100 break;
1101 }
1102 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1103 mixer_size);
1104 if (ret != 0) {
1105 ALOGE("%s: Failed to set APTX encoder config", __func__);
1106 is_configured = false;
1107 goto exit;
1108 }
1109 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
1110 if (ret != 0) {
1111 is_configured = false;
1112 goto exit;
1113 }
1114 is_configured = true;
1115 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX;
1116 a2dp.enc_channels = aptx_dsp_cfg.custom_cfg.num_channels;
1117 if (!a2dp.is_aptx_dual_mono_supported) {
1118 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1119 ALOGV("%s: Successfully updated APTX enc format with sampling rate: %d \
1120 channels:%d", __func__, aptx_dsp_cfg.custom_cfg.sample_rate,
1121 aptx_dsp_cfg.custom_cfg.num_channels);
1122 } else {
1123 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1124 ALOGV("%s: Successfully updated APTX dual mono enc format with \
1125 sampling rate: %d channels:%d sync mode %d", __func__,
1126 aptx_dsp_cfg.custom_cfg.sample_rate,
1127 aptx_dsp_cfg.custom_cfg.num_channels,
1128 aptx_dsp_cfg.aptx_v2_cfg.sync_mode);
1129 }
1130
1131exit:
1132 return is_configured;
1133}
1134
1135/* API to configure APTX HD DSP encoder
1136 */
1137static bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
1138{
1139 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1140 struct custom_enc_cfg_t aptx_dsp_cfg;
1141 bool is_configured = false;
1142 int ret = 0;
1143
1144 if (aptx_bt_cfg == NULL) {
1145 ALOGE("%s: Failed to get APTX HD encoder config from BT", __func__);
1146 return false;
1147 }
1148
1149 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1150 if (!ctl_enc_data) {
1151 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1152 is_configured = false;
1153 goto exit;
1154 }
1155
1156 memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg));
1157 aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX_HD;
1158 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
1159 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
1160 switch (aptx_dsp_cfg.num_channels) {
1161 case 1:
1162 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1163 break;
1164 case 2:
1165 default:
1166 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1167 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1168 break;
1169 }
1170 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1171 sizeof(aptx_dsp_cfg));
1172 if (ret != 0) {
1173 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
1174 is_configured = false;
1175 goto exit;
1176 }
1177 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
1178 if (ret != 0) {
1179 is_configured = false;
1180 goto exit;
1181 }
1182 is_configured = true;
1183 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX_HD;
1184 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
1185 a2dp.enc_channels = aptx_bt_cfg->channels;
1186 ALOGV("%s: Successfully updated APTX HD encformat with sampling rate: %d channels:%d",
1187 __func__, aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
1188exit:
1189 return is_configured;
1190}
1191
1192/* API to configure AAC DSP encoder */
1193static bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
1194{
1195 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1196 struct aac_enc_cfg_t aac_dsp_cfg;
1197 bool is_configured = false;
1198 int ret = 0;
1199
1200 if (aac_bt_cfg == NULL) {
1201 ALOGE("%s: Failed to get AAC encoder config from BT", __func__);
1202 return false;
1203 }
1204
1205 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1206 if (!ctl_enc_data) {
1207 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1208 is_configured = false;
1209 goto exit;
1210 }
1211 memset(&aac_dsp_cfg, 0x0, sizeof(aac_dsp_cfg));
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001212 aac_dsp_cfg.aac_cfg.enc_format = ENC_MEDIA_FMT_AAC;
1213 aac_dsp_cfg.aac_cfg.bit_rate = aac_bt_cfg->bitrate;
1214 aac_dsp_cfg.aac_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001215 switch (aac_bt_cfg->enc_mode) {
1216 case 0:
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001217 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001218 break;
1219 case 2:
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001220 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001221 break;
1222 case 1:
1223 default:
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001224 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001225 break;
1226 }
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001227 aac_dsp_cfg.aac_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
1228 aac_dsp_cfg.aac_cfg.channel_cfg = aac_bt_cfg->channels;
1229 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
1230 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001231 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
1232 sizeof(aac_dsp_cfg));
1233 if (ret != 0) {
1234 ALOGE("%s: failed to set AAC encoder config", __func__);
1235 is_configured = false;
1236 goto exit;
1237 }
1238 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
1239 if (ret != 0) {
1240 is_configured = false;
1241 goto exit;
1242 }
1243 is_configured = true;
1244 a2dp.bt_encoder_format = ENC_CODEC_TYPE_AAC;
1245 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001246 a2dp.enc_channels = aac_bt_cfg->channels;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001247 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001248 __func__, aac_dsp_cfg.aac_cfg.sample_rate, aac_dsp_cfg.aac_cfg.channel_cfg);
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001249exit:
1250 return is_configured;
1251}
1252
1253static bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
1254{
1255 struct mixer_ctl *ldac_enc_data = NULL, *ctrl_bit_format = NULL;
1256 struct ldac_enc_cfg_t ldac_dsp_cfg;
1257 bool is_configured = false;
1258 int ret = 0;
1259
1260 if (ldac_bt_cfg == NULL) {
1261 ALOGE("%s: Failed to get LDAC encoder config from BT", __func__);
1262 return false;
1263 }
1264
1265 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1266 if (!ldac_enc_data) {
1267 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1268 is_configured = false;
1269 goto exit;
1270 }
1271 memset(&ldac_dsp_cfg, 0x0, sizeof(ldac_dsp_cfg));
1272
1273 ldac_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_LDAC;
1274 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
1275 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
1276 switch (ldac_dsp_cfg.ldac_cfg.channel_mode) {
1277 case 4:
1278 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1279 ldac_dsp_cfg.custom_cfg.num_channels = 1;
1280 break;
1281 case 2:
1282 case 1:
1283 default:
1284 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1285 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1286 ldac_dsp_cfg.custom_cfg.num_channels = 2;
1287 break;
1288 }
1289
1290 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(ldac_dsp_cfg);
1291 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
1292 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001293 if (ldac_bt_cfg->is_abr_enabled) {
1294 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
1295 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
1296 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
1297 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1298 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata76ff4802018-08-06 15:30:50 -07001299 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001300 }
1301
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001302 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
1303 sizeof(ldac_dsp_cfg));
1304 if (ret != 0) {
1305 ALOGE("%s: Failed to set LDAC encoder config", __func__);
1306 is_configured = false;
1307 goto exit;
1308 }
1309 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
1310 if (ret != 0) {
1311 is_configured = false;
1312 goto exit;
1313 }
1314 is_configured = true;
1315 a2dp.bt_encoder_format = ENC_CODEC_TYPE_LDAC;
1316 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
1317 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001318 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001319 ALOGV("%s: Successfully updated LDAC encformat with sampling rate: %d channels:%d",
1320 __func__, ldac_dsp_cfg.custom_cfg.sample_rate,
1321 ldac_dsp_cfg.custom_cfg.num_channels);
1322exit:
1323 return is_configured;
1324}
1325
1326bool configure_a2dp_encoder_format()
1327{
1328 void *codec_info = NULL;
1329 uint8_t multi_cast = 0, num_dev = 1;
1330 enc_codec_t codec_type = ENC_CODEC_TYPE_INVALID;
1331 bool is_configured = false;
1332 audio_aptx_encoder_config aptx_encoder_cfg;
1333
1334 if (!a2dp.audio_get_codec_config) {
1335 ALOGE("%s: A2DP handle is not identified, ignoring A2DP encoder config", __func__);
1336 return false;
1337 }
1338 ALOGD("%s: start", __func__);
1339 codec_info = a2dp.audio_get_codec_config(&multi_cast, &num_dev,
1340 &codec_type);
1341
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001342 // ABR disabled by default for all codecs
1343 a2dp.abr_config.is_abr_enabled = false;
1344
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001345 switch (codec_type) {
1346 case ENC_CODEC_TYPE_SBC:
1347 ALOGD("%s: Received SBC encoder supported Bluetooth device", __func__);
1348 is_configured =
1349 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
1350 break;
1351 case ENC_CODEC_TYPE_APTX:
1352 ALOGD("%s: Received APTX encoder supported Bluetooth device", __func__);
1353 a2dp.is_aptx_dual_mono_supported = false;
1354 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
1355 is_configured =
1356 configure_aptx_enc_format(&aptx_encoder_cfg);
1357 break;
1358 case ENC_CODEC_TYPE_APTX_HD:
1359 ALOGD("%s: Received APTX HD encoder supported Bluetooth device", __func__);
1360 is_configured =
1361 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
1362 break;
1363 case ENC_CODEC_TYPE_AAC:
1364 ALOGD("%s: Received AAC encoder supported Bluetooth device", __func__);
1365 is_configured =
1366 configure_aac_enc_format((audio_aac_encoder_config *)codec_info);
1367 break;
1368 case ENC_CODEC_TYPE_LDAC:
1369 ALOGD("%s: Received LDAC encoder supported Bluetooth device", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001370 if (!instance_id || instance_id > MAX_INSTANCE_ID)
1371 instance_id = MAX_INSTANCE_ID;
1372 a2dp.abr_config.imc_instance = instance_id--;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001373 is_configured =
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001374 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
1375 configure_a2dp_decoder_format(ENC_CODEC_TYPE_LDAC));
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001376 break;
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +05301377 case ENC_CODEC_TYPE_PCM:
1378 ALOGD("Received PCM format for BT device");
1379 a2dp.bt_encoder_format = ENC_CODEC_TYPE_PCM;
1380 is_configured = true;
1381 break;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001382 default:
1383 ALOGD("%s: Received unsupported encoder format", __func__);
1384 is_configured = false;
1385 break;
1386 }
1387 return is_configured;
1388}
1389
1390int audio_extn_a2dp_start_playback()
1391{
1392 int ret = 0;
1393
1394 ALOGD("%s: start", __func__);
1395
1396 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_start
1397 && a2dp.audio_get_codec_config)) {
1398 ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__);
1399 return -ENOSYS;
1400 }
1401
1402 if (a2dp.a2dp_suspended) {
1403 // session will be restarted after suspend completion
1404 ALOGD("%s: A2DP start requested during suspend state", __func__);
1405 return -ENOSYS;
1406 }
1407
1408 if (!a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
1409 ALOGD("%s: calling Bluetooth module stream start", __func__);
1410 /* This call indicates Bluetooth IPC lib to start playback */
1411 ret = a2dp.audio_stream_start();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001412 if (ret != 0 ) {
1413 ALOGE("%s: Bluetooth controller start failed", __func__);
1414 a2dp.a2dp_started = false;
1415 } else {
1416 if (configure_a2dp_encoder_format() == true) {
1417 a2dp.a2dp_started = true;
1418 ret = 0;
1419 ALOGD("%s: Start playback successful to Bluetooth IPC library", __func__);
1420 } else {
1421 ALOGD("%s: unable to configure DSP encoder", __func__);
1422 a2dp.a2dp_started = false;
1423 ret = -ETIMEDOUT;
1424 }
1425 }
1426 }
1427
1428 if (a2dp.a2dp_started) {
1429 a2dp.a2dp_total_active_session_request++;
1430 a2dp_check_and_set_scrambler();
1431 a2dp_set_backend_cfg();
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001432 if (a2dp.abr_config.is_abr_enabled)
1433 start_abr();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001434 }
1435
1436 ALOGD("%s: start A2DP playback total active sessions :%d", __func__,
1437 a2dp.a2dp_total_active_session_request);
1438 return ret;
1439}
1440
1441static int reset_a2dp_enc_config_params()
1442{
1443 int ret = 0;
1444
1445 struct mixer_ctl *ctl_enc_config, *ctrl_bit_format;
1446 struct sbc_enc_cfg_t dummy_reset_config;
1447
1448 memset(&dummy_reset_config, 0x0, sizeof(dummy_reset_config));
1449 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
1450 MIXER_ENC_CONFIG_BLOCK);
1451 if (!ctl_enc_config) {
1452 ALOGE("%s: ERROR A2DP encoder format mixer control not identifed", __func__);
1453 } else {
1454 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
1455 sizeof(dummy_reset_config));
1456 a2dp.bt_encoder_format = ENC_MEDIA_FMT_NONE;
1457 }
1458
1459 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
1460
1461 return ret;
1462}
1463
Aniket Kumar Lataa85e6992018-05-22 13:56:10 -07001464static int reset_a2dp_dec_config_params()
1465{
1466 struct mixer_ctl *ctl_dec_data = NULL;
1467 struct abr_dec_cfg_t dummy_reset_cfg;
1468 int ret = 0;
1469
1470 if (a2dp.abr_config.is_abr_enabled) {
1471 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_DEC_CONFIG_BLOCK);
1472 if (!ctl_dec_data) {
1473 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
1474 return -EINVAL;
1475 }
1476 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
1477 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
1478 sizeof(dummy_reset_cfg));
1479 if (ret != 0) {
1480 ALOGE("%s: Failed to set dummy decoder config", __func__);
1481 return ret;
1482 }
1483 }
1484
1485 return ret;
1486}
1487
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001488static void reset_a2dp_config() {
1489 reset_a2dp_enc_config_params();
1490 reset_a2dp_dec_config_params();
1491 a2dp_reset_backend_cfg();
1492 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1493 stop_abr();
1494 a2dp.abr_config.is_abr_enabled = false;
1495}
1496
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001497int audio_extn_a2dp_stop_playback()
1498{
1499 int ret = 0;
1500
1501 ALOGV("%s: stop", __func__);
1502 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_stop)) {
1503 ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__);
1504 return -ENOSYS;
1505 }
1506
1507 if (a2dp.a2dp_total_active_session_request > 0)
1508 a2dp.a2dp_total_active_session_request--;
1509 else
1510 ALOGE("%s: No active playback session requests on A2DP", __func__);
1511
1512 if (a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
1513 ALOGV("%s: calling Bluetooth module stream stop", __func__);
1514 ret = a2dp.audio_stream_stop();
1515 if (ret < 0)
1516 ALOGE("%s: stop stream to Bluetooth IPC lib failed", __func__);
1517 else
1518 ALOGV("%s: stop steam to Bluetooth IPC lib successful", __func__);
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001519 if (!a2dp.a2dp_suspended)
1520 reset_a2dp_config();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001521 a2dp.a2dp_started = false;
1522 }
1523 ALOGD("%s: Stop A2DP playback total active sessions :%d", __func__,
1524 a2dp.a2dp_total_active_session_request);
1525 return 0;
1526}
1527
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001528int audio_extn_a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001529{
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001530 int ret = 0, val;
Kevin Rocard702c1572018-07-11 20:52:03 -07001531 int status = 0;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001532 char value[32] = {0};
1533 struct audio_usecase *uc_info;
1534 struct listnode *node;
1535
Petri Gynther2479b0b2018-04-17 18:38:47 -07001536 if (a2dp.is_a2dp_offload_enabled == false) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001537 ALOGV("%s: No supported encoders identified,ignoring A2DP setparam", __func__);
Kevin Rocard702c1572018-07-11 20:52:03 -07001538 status = -EINVAL;
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001539 goto param_handled;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001540 }
1541
1542 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
1543 sizeof(value));
1544 if (ret >= 0) {
1545 val = atoi(value);
1546 if (audio_is_a2dp_out_device(val)) {
1547 ALOGV("%s: Received device connect request for A2DP", __func__);
1548 open_a2dp_output();
1549 }
1550 goto param_handled;
1551 }
1552
1553 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
1554 sizeof(value));
1555
1556 if (ret >= 0) {
1557 val = atoi(value);
1558 if (audio_is_a2dp_out_device(val)) {
1559 ALOGV("%s: Received device disconnect request", __func__);
Aniket Kumar Lata30fe8682018-08-20 18:48:19 -07001560 reset_a2dp_config();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001561 close_a2dp_output();
1562 }
1563 goto param_handled;
1564 }
1565
1566 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
1567 if (ret >= 0) {
1568 if (a2dp.bt_lib_handle && (a2dp.bt_state != A2DP_STATE_DISCONNECTED)) {
Jack He0e071562018-05-11 14:39:36 -07001569 if (strncmp(value, "true", sizeof(value)) == 0) {
1570 if (a2dp.a2dp_suspended) {
1571 ALOGD("%s: A2DP is already suspended", __func__);
1572 goto param_handled;
1573 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001574 ALOGD("%s: Setting A2DP to suspend state", __func__);
1575 a2dp.a2dp_suspended = true;
1576 list_for_each(node, &a2dp.adev->usecase_list) {
1577 uc_info = node_to_item(node, struct audio_usecase, list);
1578 if (uc_info->type == PCM_PLAYBACK &&
1579 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
1580 pthread_mutex_unlock(&a2dp.adev->lock);
1581 check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
1582 pthread_mutex_lock(&a2dp.adev->lock);
1583 }
1584 }
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001585 reset_a2dp_config();
Jack He0e071562018-05-11 14:39:36 -07001586 if (a2dp.audio_stream_suspend) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001587 a2dp.audio_stream_suspend();
Jack He0e071562018-05-11 14:39:36 -07001588 }
1589 } else {
1590 if (!a2dp.a2dp_suspended) {
1591 ALOGD("%s: A2DP is already unsuspended", __func__);
1592 goto param_handled;
1593 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001594 ALOGD("%s: Resetting A2DP suspend state", __func__);
1595 struct audio_usecase *uc_info;
1596 struct listnode *node;
Jack He0e071562018-05-11 14:39:36 -07001597 if (a2dp.clear_a2dp_suspend_flag) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001598 a2dp.clear_a2dp_suspend_flag();
Jack He0e071562018-05-11 14:39:36 -07001599 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001600 a2dp.a2dp_suspended = false;
1601 /*
1602 * It is possible that before suspend, A2DP sessions can be active.
1603 * For example, during music + voice activation concurrency,
1604 * A2DP suspend will be called & Bluetooth will change to SCO mode.
1605 * Though music is paused as a part of voice activation,
1606 * compress session close happens only after pause timeout(10 secs).
1607 * So, if resume request comes before pause timeout, as A2DP session
1608 * is already active, IPC start will not be called from APM/audio_hw.
1609 * Fix this by calling A2DP start for IPC library post suspend
1610 * based on number of active session count.
1611 */
1612 if (a2dp.a2dp_total_active_session_request > 0) {
1613 ALOGD("%s: Calling Bluetooth IPC lib start post suspend state", __func__);
1614 if (a2dp.audio_stream_start) {
Kevin Rocard702c1572018-07-11 20:52:03 -07001615 status = a2dp.audio_stream_start();
1616 if (status != 0) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001617 ALOGE("%s: Bluetooth controller start failed", __func__);
1618 a2dp.a2dp_started = false;
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001619 } else {
1620 if (!configure_a2dp_encoder_format()) {
1621 ALOGE("%s: Encoder params configuration failed post suspend", __func__);
1622 a2dp.a2dp_started = false;
Kevin Rocard702c1572018-07-11 20:52:03 -07001623 status = -ETIMEDOUT;
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001624 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001625 }
1626 }
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001627 if (a2dp.a2dp_started) {
1628 a2dp_set_backend_cfg();
1629 if (a2dp.abr_config.is_abr_enabled)
1630 start_abr();
1631 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001632 }
1633 list_for_each(node, &a2dp.adev->usecase_list) {
1634 uc_info = node_to_item(node, struct audio_usecase, list);
1635 if (uc_info->type == PCM_PLAYBACK &&
1636 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
1637 pthread_mutex_unlock(&a2dp.adev->lock);
1638 check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
1639 pthread_mutex_lock(&a2dp.adev->lock);
1640 }
1641 }
1642 }
1643 }
1644 goto param_handled;
1645 }
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001646
1647 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
1648 sizeof(value));
1649 if (ret >= 0) {
1650 if (a2dp.is_a2dp_offload_enabled &&
1651 a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
1652 *reconfig = true;
1653 }
1654 goto param_handled;
1655 }
1656
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001657param_handled:
1658 ALOGV("%s: end of A2DP setparam", __func__);
Kevin Rocard702c1572018-07-11 20:52:03 -07001659 return status;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001660}
1661
1662void audio_extn_a2dp_set_handoff_mode(bool is_on)
1663{
1664 a2dp.is_handoff_in_progress = is_on;
1665}
1666
1667bool audio_extn_a2dp_is_force_device_switch()
1668{
1669 // During encoder reconfiguration mode, force A2DP device switch
1670 // Or if A2DP device is selected but earlier start failed as A2DP
1671 // was suspended, force retry.
1672 return a2dp.is_handoff_in_progress || !a2dp.a2dp_started;
1673}
1674
1675void audio_extn_a2dp_get_sample_rate(int *sample_rate)
1676{
1677 *sample_rate = a2dp.enc_sampling_rate;
1678}
1679
1680bool audio_extn_a2dp_is_ready()
1681{
1682 bool ret = false;
1683
1684 if (a2dp.a2dp_suspended)
1685 goto exit;
1686
1687 if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) &&
Petri Gynther2479b0b2018-04-17 18:38:47 -07001688 (a2dp.is_a2dp_offload_enabled) &&
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001689 (a2dp.audio_check_a2dp_ready))
1690 ret = a2dp.audio_check_a2dp_ready();
1691
1692exit:
1693 return ret;
1694}
1695
1696bool audio_extn_a2dp_is_suspended()
1697{
1698 return a2dp.a2dp_suspended;
1699}
1700
1701void audio_extn_a2dp_init(void *adev)
1702{
1703 a2dp.adev = (struct audio_device*)adev;
1704 a2dp.bt_lib_handle = NULL;
1705 a2dp_common_init();
1706 a2dp.enc_sampling_rate = 48000;
Petri Gynther2479b0b2018-04-17 18:38:47 -07001707 a2dp.is_a2dp_offload_enabled = false;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001708 a2dp.is_handoff_in_progress = false;
1709 a2dp.is_aptx_dual_mono_supported = false;
1710 reset_a2dp_enc_config_params();
Aniket Kumar Lataa85e6992018-05-22 13:56:10 -07001711 reset_a2dp_dec_config_params();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001712 update_offload_codec_support();
1713}
1714
1715uint32_t audio_extn_a2dp_get_encoder_latency()
1716{
1717 uint32_t latency = 0;
1718 int avsync_runtime_prop = 0;
1719 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
1720 aac_offset = 0, ldac_offset = 0;
1721 char value[PROPERTY_VALUE_MAX];
1722
1723 memset(value, '\0', sizeof(char) * PROPERTY_VALUE_MAX);
1724 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
1725 if (avsync_runtime_prop > 0) {
1726 if (sscanf(value, "%d/%d/%d/%d/%d",
1727 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset,
1728 &ldac_offset) != 5) {
1729 ALOGI("%s: Failed to parse avsync offset params from '%s'.", __func__, value);
1730 avsync_runtime_prop = 0;
1731 }
1732 }
1733
1734 switch (a2dp.bt_encoder_format) {
1735 case ENC_CODEC_TYPE_SBC:
1736 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
1737 latency += DEFAULT_SINK_LATENCY_SBC;
1738 break;
1739 case ENC_CODEC_TYPE_APTX:
1740 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
1741 latency += DEFAULT_SINK_LATENCY_APTX;
1742 break;
1743 case ENC_CODEC_TYPE_APTX_HD:
1744 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
1745 latency += DEFAULT_SINK_LATENCY_APTX_HD;
1746 break;
1747 case ENC_CODEC_TYPE_AAC:
1748 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
1749 latency += DEFAULT_SINK_LATENCY_AAC;
1750 break;
1751 case ENC_CODEC_TYPE_LDAC:
1752 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
1753 latency += DEFAULT_SINK_LATENCY_LDAC;
1754 break;
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +05301755 case ENC_CODEC_TYPE_PCM:
1756 latency = ENCODER_LATENCY_PCM;
1757 latency += DEFAULT_SINK_LATENCY_PCM;
1758 break;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001759 default:
1760 latency = DEFAULT_ENCODER_LATENCY;
1761 break;
1762 }
1763 return latency;
1764}
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001765
1766int audio_extn_a2dp_get_parameters(struct str_parms *query,
1767 struct str_parms *reply)
1768{
1769 int ret, val = 0;
1770 char value[32]={0};
1771
1772 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
1773 value, sizeof(value));
1774 if (ret >= 0) {
1775 val = a2dp.is_a2dp_offload_enabled;
1776 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
1777 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
1778 }
1779
1780 return 0;
1781}
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001782#endif // A2DP_OFFLOAD_ENABLED