blob: 7c2e4db756b7f6cdb1647f8ad3db4385edb6d8cb [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);
Aniket Kumar Latada256de2018-08-07 14:32:33 -0700144typedef uint16_t (*audio_get_a2dp_sink_latency_t)(void);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800145
146enum A2DP_STATE {
147 A2DP_STATE_CONNECTED,
148 A2DP_STATE_STARTED,
149 A2DP_STATE_STOPPED,
150 A2DP_STATE_DISCONNECTED,
151};
152
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700153typedef enum {
154 IMC_TRANSMIT,
155 IMC_RECEIVE,
156} imc_direction_t;
157
158typedef enum {
159 IMC_DISABLE,
160 IMC_ENABLE,
161} imc_status_t;
162
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -0700163typedef enum {
164 MTU_SIZE,
165 PEAK_BIT_RATE,
166} frame_control_type_t;
167
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700168/* PCM config for ABR Feedback hostless front end */
169static struct pcm_config pcm_config_abr = {
170 .channels = 1,
171 .rate = 8000,
172 .period_size = 240,
173 .period_count = 2,
174 .format = PCM_FORMAT_S16_LE,
175 .start_threshold = 0,
176 .stop_threshold = INT_MAX,
177 .avail_min = 0,
178};
179
180/* Adaptive bitrate config for A2DP codecs */
181struct a2dp_abr_config {
182 /* Flag to denote whether Adaptive bitrate is enabled for codec */
183 bool is_abr_enabled;
184 /* Flag to denote whether front end has been opened for ABR */
185 bool abr_started;
186 /* ABR Tx path pcm handle */
187 struct pcm *abr_tx_handle;
188 /* ABR Inter Module Communication (IMC) instance ID */
189 uint32_t imc_instance;
190};
191
192static uint32_t instance_id = MAX_INSTANCE_ID;
193
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800194/* Data structure used to:
195 * - Update the A2DP state machine
196 * - Communicate with the libbthost_if.so IPC library
197 * - Store DSP encoder configuration information
198 */
199struct a2dp_data {
200 /* Audio device handle */
201 struct audio_device *adev;
202 /* Bluetooth IPC library handle */
203 void *bt_lib_handle;
204 /* Open A2DP audio stream. Initialize audio datapath */
205 audio_stream_open_t audio_stream_open;
206 /* Close A2DP audio stream */
207 audio_stream_close_t audio_stream_close;
208 /* Start A2DP audio stream. Start audio datapath */
209 audio_stream_start_t audio_stream_start;
210 /* Stop A2DP audio stream */
211 audio_stream_stop_t audio_stream_stop;
212 /* Suspend A2DP audio stream */
213 audio_stream_suspend_t audio_stream_suspend;
214 /* Notify Bluetooth IPC library of handoff being triggered */
215 audio_handoff_triggered_t audio_handoff_triggered;
216 /* Clear A2DP suspend flag in Bluetooth IPC library */
217 clear_a2dp_suspend_flag_t clear_a2dp_suspend_flag;
218 /* Get codec configuration from Bluetooth stack via
219 * Bluetooth IPC library */
220 audio_get_codec_config_t audio_get_codec_config;
221 /* Check if A2DP is ready */
222 audio_check_a2dp_ready_t audio_check_a2dp_ready;
223 /* Check if scrambling is enabled on BTSoC */
224 audio_is_scrambling_enabled_t audio_is_scrambling_enabled;
Aniket Kumar Latada256de2018-08-07 14:32:33 -0700225 /* Get sink latency from Bluetooth stack */
226 audio_get_a2dp_sink_latency_t audio_get_a2dp_sink_latency;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800227 /* Internal A2DP state identifier */
228 enum A2DP_STATE bt_state;
229 /* A2DP codec type configured */
230 enc_codec_t bt_encoder_format;
231 /* Sampling rate configured with A2DP encoder on DSP */
232 uint32_t enc_sampling_rate;
233 /* Channel configuration of A2DP on DSP */
234 uint32_t enc_channels;
235 /* Flag to denote whether A2DP audio datapath has started */
236 bool a2dp_started;
237 /* Flag to denote whether A2DP audio datapath is suspended */
238 bool a2dp_suspended;
239 /* Number of active sessions on A2DP output */
240 int a2dp_total_active_session_request;
Petri Gynther2479b0b2018-04-17 18:38:47 -0700241 /* Flag to denote whether A2DP offload is enabled */
242 bool is_a2dp_offload_enabled;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800243 /* Flag to denote whether codec reconfiguration/soft handoff is in progress */
244 bool is_handoff_in_progress;
245 /* Flag to denote whether APTX Dual Mono encoder is supported */
246 bool is_aptx_dual_mono_supported;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700247 /* Adaptive bitrate config for A2DP codecs */
248 struct a2dp_abr_config abr_config;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800249};
250
251struct a2dp_data a2dp;
252
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700253/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
254 * Structures sent to configure DSP for ABR are defined below.
255 * This data helps DSP configure feedback path (BTSoC to LPASS)
256 * for link quality levels and mapping quality levels to codec
257 * specific bitrate.
258 */
259
260/* Key value pair for link quality level to bitrate mapping. */
261struct bit_rate_level_map_t {
262 uint32_t link_quality_level;
263 uint32_t bitrate;
264};
265
266/* Link quality level to bitrate mapping info sent to DSP. */
267struct quality_level_to_bitrate_info {
268 /* Number of quality levels being mapped.
269 * This will be equal to the size of mapping table.
270 */
271 uint32_t num_levels;
272 /* Quality level to bitrate mapping table */
273 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
274};
275
276/* Structure to set up Inter Module Communication (IMC) between
277 * AFE Decoder and Encoder.
278 */
279struct imc_dec_enc_info {
280 /* Decoder to encoder communication direction.
281 * Transmit = 0 / Receive = 1
282 */
283 uint32_t direction;
284 /* Enable / disable IMC between decoder and encoder */
285 uint32_t enable;
286 /* Purpose of IMC being set up between decoder and encoder.
287 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
288 * is the default value to be sent as purpose.
289 */
290 uint32_t purpose;
291 /* Unique communication instance ID.
292 * purpose and comm_instance together form the actual key
293 * used in IMC registration, which must be the same for
294 * encoder and decoder for which IMC is being set up.
295 */
296 uint32_t comm_instance;
297};
298
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -0700299/* Structure to control frame size of AAC encoded frames. */
300struct aac_frame_size_control_t {
301 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
302 uint32_t ctl_type;
303 /* Control value
304 * MTU_SIZE: MTU size in bytes
305 * PEAK_BIT_RATE: Peak bitrate in bits per second.
306 */
307 uint32_t ctl_value;
308};
309
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700310/* Structure used for ABR config of AFE encoder and decoder. */
311struct abr_enc_cfg_t {
312 /* Link quality level to bitrate mapping info sent to DSP. */
313 struct quality_level_to_bitrate_info mapping_info;
314 /* Information to set up IMC between decoder and encoder */
315 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata76ff4802018-08-06 15:30:50 -0700316 /* Flag to indicate whether ABR is enabled */
317 bool is_abr_enabled;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700318} __attribute__ ((packed));
319
320/* Structure to send configuration for decoder introduced
321 * on AFE Tx path for ABR link quality feedback to BT encoder.
322 */
323struct abr_dec_cfg_t {
324 /* Decoder media format */
325 uint32_t dec_format;
326 /* Information to set up IMC between decoder and encoder */
327 struct imc_dec_enc_info imc_info;
328} __attribute__ ((packed));
329
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800330/* START of DSP configurable structures
331 * These values should match with DSP interface defintion
332 */
333
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -0700334struct aac_cfg_blk_t {
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800335 /* Encoder media format for AAC */
336 uint32_t enc_format;
337
338 /* Encoding rate in bits per second */
339 uint32_t bit_rate;
340
341 /* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS */
342 uint32_t enc_mode;
343
344 /* supported aac_fmt_flag are ADTS/RAW */
345 uint16_t aac_fmt_flag;
346
347 /* supported channel_cfg are Native mode, Mono , Stereo */
348 uint16_t channel_cfg;
349
350 /* Number of samples per second */
351 uint32_t sample_rate;
352} __attribute__ ((packed));
353
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -0700354/* AAC encoder configuration structure. */
355typedef struct aac_enc_cfg_t aac_enc_cfg_t;
356
357struct aac_enc_cfg_t {
358 struct aac_cfg_blk_t aac_cfg;
359 struct aac_frame_size_control_t frame_ctl;
360} __attribute__ ((packed));
361
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800362/* SBC encoder configuration structure. */
363typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
364
365struct sbc_enc_cfg_t {
366 /* Encoder media format for SBC */
367 uint32_t enc_format;
368
369 /* supported num_subbands are 4/8 */
370 uint32_t num_subbands;
371
372 /* supported blk_len are 4, 8, 12, 16 */
373 uint32_t blk_len;
374
375 /* supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO */
376 uint32_t channel_mode;
377
378 /* supported alloc_method are LOUNDNESS/SNR */
379 uint32_t alloc_method;
380
381 /* supported bit_rate for mono channel is max 320kbps
382 * supported bit rate for stereo channel is max 512 kbps */
383 uint32_t bit_rate;
384
385 /* Number of samples per second */
386 uint32_t sample_rate;
387} __attribute__ ((packed));
388
389struct custom_enc_cfg_t {
390 /* Custom encoder media format */
391 uint32_t enc_format;
392
393 /* Number of samples per second */
394 uint32_t sample_rate;
395
396 /* supported num_channels are Mono/Stereo */
397 uint16_t num_channels;
398
399 /* Reserved for future enhancement */
400 uint16_t reserved;
401
402 /* supported channel_mapping for mono is CHANNEL_C
403 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R */
404 uint8_t channel_mapping[8];
405
406 /* Reserved for future enhancement */
407 uint32_t custom_size;
408} __attribute__ ((packed));
409
410struct aptx_v2_enc_cfg_ext_t {
411/* sync_mode introduced with APTX V2 libraries
412 * sync mode: 0x0 = stereo sync mode
413 * 0x01 = dual mono sync mode
414 * 0x02 = dual mono with no sync on either L or R codewords
415 */
416 uint32_t sync_mode;
417} __attribute__ ((packed));
418
419/* APTX struct for combining custom enc and V2 members */
420struct aptx_enc_cfg_t {
421 struct custom_enc_cfg_t custom_cfg;
422 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
423} __attribute__ ((packed));
424
425struct ldac_specific_enc_cfg_t {
426 /*
427 * This is used to calculate the encoder output
428 * bytes per frame (i.e. bytes per packet).
429 * Bit rate also configures the EQMID.
430 * The min bit rate 303000 bps is calculated for
431 * 44.1 kHz and 88.2 KHz sampling frequencies with
432 * Mobile use Quality.
433 * The max bit rate of 990000 bps is calculated for
434 * 96kHz and 48 KHz with High Quality
435 * @Range(in bits per second)
436 * 303000 for Mobile use Quality
437 * 606000 for standard Quality
438 * 909000 for High Quality
439 */
440 uint32_t bit_rate;
441
442 /*
443 * The channel setting information for LDAC specification
444 * of Bluetooth A2DP which is determined by SRC and SNK
445 * devices in Bluetooth transmission.
446 * @Range:
447 * 0 for native mode
448 * 4 for mono
449 * 2 for dual channel
450 * 1 for stereo
451 */
452 uint16_t channel_mode;
453
454 /*
455 * Maximum Transmission Unit (MTU).
456 * The minimum MTU that a L2CAP implementation for LDAC shall
457 * support is 679 bytes, because LDAC is optimized with 2-DH5
458 * packet as its target.
459 * @Range : 679
460 * @Default: 679 for LDACBT_MTU_2DH5
461 */
462 uint16_t mtu;
463} __attribute__ ((packed));
464
465/* LDAC struct for combining custom enc and standard members */
466struct ldac_enc_cfg_t {
467 struct custom_enc_cfg_t custom_cfg;
468 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700469 struct abr_enc_cfg_t abr_cfg;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800470} __attribute__ ((packed));
471
472/* Information about Bluetooth SBC encoder configuration
473 * This data is used between audio HAL module and
474 * Bluetooth IPC library to configure DSP encoder
475 */
476typedef struct {
477 uint32_t subband; /* 4, 8 */
478 uint32_t blk_len; /* 4, 8, 12, 16 */
479 uint16_t sampling_rate; /* 44.1khz, 48khz */
480 uint8_t channels; /* 0(Mono), 1(Dual_mono), 2(Stereo), 3(JS) */
481 uint8_t alloc; /* 0(Loudness), 1(SNR) */
482 uint8_t min_bitpool; /* 2 */
483 uint8_t max_bitpool; /* 53(44.1khz), 51 (48khz) */
484 uint32_t bitrate; /* 320kbps to 512kbps */
485 uint32_t bits_per_sample; /* 16 bit */
486} audio_sbc_encoder_config;
487
488/* Information about Bluetooth APTX encoder configuration
489 * This data is used between audio HAL module and
490 * Bluetooth IPC library to configure DSP encoder
491 */
492typedef struct {
493 uint16_t sampling_rate;
494 uint8_t channels;
495 uint32_t bitrate;
496 uint32_t bits_per_sample;
497} audio_aptx_default_config;
498
499typedef struct {
500 uint16_t sampling_rate;
501 uint8_t channels;
502 uint32_t bitrate;
503 uint32_t sync_mode;
504} audio_aptx_dual_mono_config;
505
506typedef union {
507 audio_aptx_default_config *default_cfg;
508 audio_aptx_dual_mono_config *dual_mono_cfg;
509} audio_aptx_encoder_config;
510
511/* Information about Bluetooth AAC encoder configuration
512 * This data is used between audio HAL module and
513 * Bluetooth IPC library to configure DSP encoder
514 */
515typedef struct {
516 uint32_t enc_mode; /* LC, SBR, PS */
517 uint16_t format_flag; /* RAW, ADTS */
518 uint16_t channels; /* 1-Mono, 2-Stereo */
519 uint32_t sampling_rate;
520 uint32_t bitrate;
521 uint32_t bits_per_sample;
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -0700522 struct aac_frame_size_control_t frame_ctl;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800523} audio_aac_encoder_config;
524
525/* Information about Bluetooth LDAC encoder configuration
526 * This data is used between audio HAL module and
527 * Bluetooth IPC library to configure DSP encoder
528 */
529typedef struct {
530 uint32_t sampling_rate; /* 44100, 48000, 88200, 96000 */
531 uint32_t bit_rate; /* 303000, 606000, 909000 (in bits per second) */
532 uint16_t channel_mode; /* 0, 4, 2, 1 */
533 uint16_t mtu;
534 uint32_t bits_per_sample; /* 16, 24, 32 (bits) */
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700535 bool is_abr_enabled;
536 struct quality_level_to_bitrate_info level_to_bitrate_map;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800537} audio_ldac_encoder_config;
538
539/*********** END of DSP configurable structures ********************/
540
541static void a2dp_common_init()
542{
543 a2dp.a2dp_started = false;
544 a2dp.a2dp_total_active_session_request = 0;
545 a2dp.a2dp_suspended = false;
546 a2dp.bt_encoder_format = ENC_CODEC_TYPE_INVALID;
547 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700548 a2dp.abr_config.is_abr_enabled = false;
549 a2dp.abr_config.abr_started = false;
550 a2dp.abr_config.imc_instance = 0;
551 a2dp.abr_config.abr_tx_handle = NULL;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800552}
553
554static void update_offload_codec_support()
555{
Petri Gynther2479b0b2018-04-17 18:38:47 -0700556 a2dp.is_a2dp_offload_enabled =
557 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
558 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
559
560 ALOGD("%s: A2DP offload enabled = %d", __func__,
561 a2dp.is_a2dp_offload_enabled);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700562}
563
564static int stop_abr()
565{
566 struct mixer_ctl *ctl_abr_tx_path = NULL;
567 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
568
569 /* This function can be used if !abr_started for clean up */
570 ALOGV("%s: enter", __func__);
571
572 // Close hostless front end
573 if (a2dp.abr_config.abr_tx_handle != NULL) {
574 pcm_close(a2dp.abr_config.abr_tx_handle);
575 a2dp.abr_config.abr_tx_handle = NULL;
576 }
577 a2dp.abr_config.abr_started = false;
578 a2dp.abr_config.imc_instance = 0;
579
580 // Reset BT driver mixer control for ABR usecase
581 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
582 MIXER_SET_FEEDBACK_CHANNEL);
583 if (!ctl_set_bt_feedback_channel) {
584 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
585 return -ENOSYS;
586 }
587 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
588 ALOGE("%s: Failed to set BT usecase", __func__);
589 return -ENOSYS;
590 }
591
592 // Reset ABR Tx feedback path
593 ALOGV("%s: Disable ABR Tx feedback path", __func__);
594 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
595 MIXER_ABR_TX_FEEDBACK_PATH);
596 if (!ctl_abr_tx_path) {
597 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
598 return -ENOSYS;
599 }
600 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
601 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
602 return -ENOSYS;
603 }
604
605 return 0;
606}
607
608static int start_abr()
609{
610 struct mixer_ctl *ctl_abr_tx_path = NULL;
611 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
612 int abr_device_id;
613 int ret = 0;
614
615 if (!a2dp.abr_config.is_abr_enabled) {
616 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
617 return -ENOSYS;
618 }
619
620 if (a2dp.abr_config.abr_started) {
621 ALOGI("%s: ABR has already started", __func__);
622 return ret;
623 }
624
625 // Enable Slimbus 7 Tx feedback path
626 ALOGV("%s: Enable ABR Tx feedback path", __func__);
627 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
628 MIXER_ABR_TX_FEEDBACK_PATH);
629 if (!ctl_abr_tx_path) {
630 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
631 return -ENOSYS;
632 }
633 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
634 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
635 return -ENOSYS;
636 }
637
638 // Notify ABR usecase information to BT driver to distinguish
639 // between SCO and feedback usecase
640 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
641 MIXER_SET_FEEDBACK_CHANNEL);
642 if (!ctl_set_bt_feedback_channel) {
643 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
644 return -ENOSYS;
645 }
646 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
647 ALOGE("%s: Failed to set BT usecase", __func__);
648 return -ENOSYS;
649 }
650
651 // Open hostless front end and prepare ABR Tx path
652 abr_device_id = platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
653 PCM_CAPTURE);
654 if (!a2dp.abr_config.abr_tx_handle) {
655 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
656 abr_device_id, PCM_IN,
657 &pcm_config_abr);
658 if (a2dp.abr_config.abr_tx_handle == NULL ||
659 !pcm_is_ready(a2dp.abr_config.abr_tx_handle))
660 goto fail;
661 }
662 ret = pcm_start(a2dp.abr_config.abr_tx_handle);
663 if (ret < 0)
664 goto fail;
665 a2dp.abr_config.abr_started = true;
666
667 return ret;
668
669fail:
670 ALOGE("%s: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
671 stop_abr();
672 return -ENOSYS;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800673}
674
675/* API to open Bluetooth IPC library to start IPC communication */
676static int open_a2dp_output()
677{
678 int ret = 0;
679
680 ALOGD("%s: Open A2DP output start", __func__);
681
682 if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
683 ALOGD("%s: Called A2DP open with improper state, Ignoring request state %d",
684 __func__, a2dp.bt_state);
685 return -ENOSYS;
686 }
687
688 if (a2dp.bt_lib_handle == NULL) {
689 ALOGD("%s: Requesting for Bluetooth IPC lib handle", __func__);
690 a2dp.bt_lib_handle = dlopen(BT_IPC_LIB_NAME, RTLD_NOW);
691
692 if (a2dp.bt_lib_handle == NULL) {
693 ret = -errno;
694 ALOGE("%s: DLOPEN failed for %s errno %d strerror %s", __func__,
695 BT_IPC_LIB_NAME, errno, strerror(errno));
696 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
697 return ret;
698 } else {
699 a2dp.audio_stream_open = (audio_stream_open_t)
700 dlsym(a2dp.bt_lib_handle, "audio_stream_open");
701 a2dp.audio_stream_start = (audio_stream_start_t)
702 dlsym(a2dp.bt_lib_handle, "audio_stream_start");
703 a2dp.audio_get_codec_config = (audio_get_codec_config_t)
704 dlsym(a2dp.bt_lib_handle, "audio_get_codec_config");
705 a2dp.audio_stream_suspend = (audio_stream_suspend_t)
706 dlsym(a2dp.bt_lib_handle, "audio_stream_suspend");
707 a2dp.audio_handoff_triggered = (audio_handoff_triggered_t)
708 dlsym(a2dp.bt_lib_handle, "audio_handoff_triggered");
709 a2dp.clear_a2dp_suspend_flag = (clear_a2dp_suspend_flag_t)
710 dlsym(a2dp.bt_lib_handle, "clear_a2dp_suspend_flag");
711 a2dp.audio_stream_stop = (audio_stream_stop_t)
712 dlsym(a2dp.bt_lib_handle, "audio_stream_stop");
713 a2dp.audio_stream_close = (audio_stream_close_t)
714 dlsym(a2dp.bt_lib_handle, "audio_stream_close");
715 a2dp.audio_check_a2dp_ready = (audio_check_a2dp_ready_t)
716 dlsym(a2dp.bt_lib_handle,"audio_check_a2dp_ready");
717 a2dp.audio_is_scrambling_enabled = (audio_is_scrambling_enabled_t)
718 dlsym(a2dp.bt_lib_handle,"audio_is_scrambling_enabled");
Aniket Kumar Latada256de2018-08-07 14:32:33 -0700719 a2dp.audio_get_a2dp_sink_latency = (audio_get_a2dp_sink_latency_t)
720 dlsym(a2dp.bt_lib_handle,"audio_get_a2dp_sink_latency");
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800721 }
722 }
723
724 if (a2dp.bt_lib_handle && a2dp.audio_stream_open) {
725 ALOGD("%s: calling Bluetooth stream open", __func__);
726 ret = a2dp.audio_stream_open();
727 if (ret != 0) {
728 ALOGE("%s: Failed to open output stream for A2DP: status %d", __func__, ret);
729 dlclose(a2dp.bt_lib_handle);
730 a2dp.bt_lib_handle = NULL;
731 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
732 return ret;
733 }
734 a2dp.bt_state = A2DP_STATE_CONNECTED;
735 } else {
736 ALOGE("%s: A2DP handle is not identified, Ignoring open request", __func__);
737 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
738 return -ENOSYS;
739 }
740
741 return ret;
742}
743
744static int close_a2dp_output()
745{
746 ALOGV("%s\n",__func__);
747 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_close)) {
748 ALOGE("%s: A2DP handle is not identified, Ignoring close request", __func__);
749 return -ENOSYS;
750 }
751 if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
752 ALOGD("%s: calling Bluetooth stream close", __func__);
753 if (a2dp.audio_stream_close() == false)
754 ALOGE("%s: failed close A2DP control path from Bluetooth IPC library", __func__);
755 }
756 a2dp_common_init();
757 a2dp.enc_sampling_rate = 0;
758 a2dp.enc_channels = 0;
759
760 return 0;
761}
762
763static int a2dp_check_and_set_scrambler()
764{
765 bool scrambler_mode = false;
766 struct mixer_ctl *ctrl_scrambler_mode = NULL;
767 int ret = 0;
768 if (a2dp.audio_is_scrambling_enabled && (a2dp.bt_state != A2DP_STATE_DISCONNECTED))
769 scrambler_mode = a2dp.audio_is_scrambling_enabled();
770
771 // Scrambling needs to be enabled in DSP if scrambler mode is set
772 // disable scrambling not required
773 if (scrambler_mode) {
774 // enable scrambler in dsp
775 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
776 MIXER_SCRAMBLER_MODE);
777 if (!ctrl_scrambler_mode) {
778 ALOGE("%s: ERROR scrambler mode mixer control not identifed", __func__);
779 return -ENOSYS;
780 } else {
781 ret = mixer_ctl_set_value(ctrl_scrambler_mode, 0, true);
782 if (ret != 0) {
783 ALOGE("%s: Could not set scrambler mode", __func__);
784 return ret;
785 }
786 }
787 }
788 return 0;
789}
790
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700791static int a2dp_set_backend_cfg()
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800792{
793 const char *rate_str = NULL, *in_channels = NULL;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700794 uint32_t sampling_rate_rx = a2dp.enc_sampling_rate;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800795 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL;
796
797 // For LDAC encoder open slimbus port at 96Khz for 48Khz input
798 // and 88.2Khz for 44.1Khz input.
799 if ((a2dp.bt_encoder_format == ENC_CODEC_TYPE_LDAC) &&
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700800 (sampling_rate_rx == 48000 || sampling_rate_rx == 44100 )) {
801 sampling_rate_rx *= 2;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800802 }
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +0530803 // No need to configure backend for PCM format.
804 if (a2dp.bt_encoder_format == ENC_CODEC_TYPE_PCM) {
805 return 0;
806 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700807 // Set Rx backend sample rate
808 switch (sampling_rate_rx) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800809 case 44100:
810 rate_str = "KHZ_44P1";
811 break;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800812 case 88200:
813 rate_str = "KHZ_88P2";
814 break;
815 case 96000:
816 rate_str = "KHZ_96";
817 break;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700818 case 48000:
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800819 default:
820 rate_str = "KHZ_48";
821 break;
822 }
823
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700824 ALOGV("%s: set backend rx sample rate = %s", __func__, rate_str);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800825 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700826 MIXER_SAMPLE_RATE_RX);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800827 if (!ctl_sample_rate) {
828 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700829 return -ENOSYS;
830 }
831 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
832 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
833 return -ENOSYS;
834 }
835
836 // Set Tx backend sample rate
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -0700837 if (a2dp.abr_config.is_abr_enabled) {
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700838 rate_str = ABR_TX_SAMPLE_RATE;
839
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -0700840 ALOGV("%s: set backend tx sample rate = %s", __func__, rate_str);
841 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
842 MIXER_SAMPLE_RATE_TX);
843 if (!ctl_sample_rate) {
844 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
845 return -ENOSYS;
846 }
847 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
848 ALOGE("%s: Failed to set backend sample rate = %s",
849 __func__, rate_str);
850 return -ENOSYS;
851 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800852 }
853
854 // Configure AFE input channels
855 switch (a2dp.enc_channels) {
856 case 1:
857 in_channels = "One";
858 break;
859 case 2:
860 default:
861 in_channels = "Two";
862 break;
863 }
864
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700865 ALOGV("%s: set AFE input channels = %d", __func__, a2dp.enc_channels);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800866 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
867 MIXER_AFE_IN_CHANNELS);
868 if (!ctrl_in_channels) {
869 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700870 return -ENOSYS;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800871 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700872 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
873 ALOGE("%s: Failed to set AFE in channels = %d", __func__, a2dp.enc_channels);
874 return -ENOSYS;
875 }
876
877 return 0;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800878}
879
880static int a2dp_set_bit_format(uint32_t enc_bit_format)
881{
882 const char *bit_format = NULL;
883 struct mixer_ctl *ctrl_bit_format = NULL;
884
885 // Configure AFE Input Bit Format
886 switch (enc_bit_format) {
887 case 32:
888 bit_format = "S32_LE";
889 break;
890 case 24:
891 bit_format = "S24_LE";
892 break;
893 case 16:
894 default:
895 bit_format = "S16_LE";
896 break;
897 }
898
899 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
900 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
901 MIXER_ENC_BIT_FORMAT);
902 if (!ctrl_bit_format) {
903 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
904 return -ENOSYS;
905 }
906 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
907 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
908 return -ENOSYS;
909 }
910 return 0;
911}
912
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700913static int a2dp_reset_backend_cfg()
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800914{
915 const char *rate_str = "KHZ_8", *in_channels = "Zero";
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700916 struct mixer_ctl *ctl_sample_rate_rx = NULL, *ctl_sample_rate_tx = NULL;
917 struct mixer_ctl *ctrl_in_channels = NULL;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800918
919 // Reset backend sampling rate
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700920 ALOGV("%s: reset backend sample rate = %s", __func__, rate_str);
921 ctl_sample_rate_rx = mixer_get_ctl_by_name(a2dp.adev->mixer,
922 MIXER_SAMPLE_RATE_RX);
923 if (!ctl_sample_rate_rx) {
924 ALOGE("%s: ERROR Rx backend sample rate mixer control not identifed", __func__);
925 return -ENOSYS;
926 }
927 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_rx, rate_str) != 0) {
928 ALOGE("%s: Failed to reset Rx backend sample rate = %s", __func__, rate_str);
929 return -ENOSYS;
930 }
931
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -0700932 if (a2dp.abr_config.is_abr_enabled) {
933 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
934 MIXER_SAMPLE_RATE_TX);
935 if (!ctl_sample_rate_tx) {
936 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
937 return -ENOSYS;
938 }
939 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
940 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
941 return -ENOSYS;
942 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800943 }
944
945 // Reset AFE input channels
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700946 ALOGV("%s: reset AFE input channels = %s", __func__, in_channels);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800947 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
948 MIXER_AFE_IN_CHANNELS);
949 if (!ctrl_in_channels) {
950 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700951 return -ENOSYS;
952 }
953 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
954 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
955 return -ENOSYS;
956 }
957
958 return 0;
959}
960
961/* API to configure AFE decoder in DSP */
962static bool configure_a2dp_decoder_format(int dec_format)
963{
964 struct mixer_ctl *ctl_dec_data = NULL;
965 struct abr_dec_cfg_t dec_cfg;
966 int ret = 0;
967
968 if (a2dp.abr_config.is_abr_enabled) {
969 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_DEC_CONFIG_BLOCK);
970 if (!ctl_dec_data) {
971 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
972 return false;
973 }
974 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
975 dec_cfg.dec_format = dec_format;
976 dec_cfg.imc_info.direction = IMC_TRANSMIT;
977 dec_cfg.imc_info.enable = IMC_ENABLE;
978 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
979 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
980
981 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dec_cfg,
982 sizeof(dec_cfg));
983 if (ret != 0) {
984 ALOGE("%s: Failed to set decoder config", __func__);
985 return false;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800986 }
987 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700988
989 return true;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800990}
991
992/* API to configure SBC DSP encoder */
993static bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
994{
995 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
996 struct sbc_enc_cfg_t sbc_dsp_cfg;
997 bool is_configured = false;
998 int ret = 0;
999
1000 if (sbc_bt_cfg == NULL) {
1001 ALOGE("%s: Failed to get SBC encoder config from BT", __func__);
1002 return false;
1003 }
1004
1005 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1006 if (!ctl_enc_data) {
1007 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1008 is_configured = false;
1009 goto exit;
1010 }
1011 memset(&sbc_dsp_cfg, 0x0, sizeof(sbc_dsp_cfg));
1012 sbc_dsp_cfg.enc_format = ENC_MEDIA_FMT_SBC;
1013 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1014 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1015 switch (sbc_bt_cfg->channels) {
1016 case 0:
1017 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1018 break;
1019 case 1:
1020 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1021 break;
1022 case 3:
1023 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1024 break;
1025 case 2:
1026 default:
1027 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1028 break;
1029 }
1030 if (sbc_bt_cfg->alloc)
1031 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1032 else
1033 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1034 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1035 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1036 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1037 sizeof(sbc_dsp_cfg));
1038 if (ret != 0) {
1039 ALOGE("%s: failed to set SBC encoder config", __func__);
1040 is_configured = false;
1041 goto exit;
1042 }
1043 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
1044 if (ret != 0) {
1045 is_configured = false;
1046 goto exit;
1047 }
1048 is_configured = true;
1049 a2dp.bt_encoder_format = ENC_CODEC_TYPE_SBC;
1050 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
1051
1052 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1053 a2dp.enc_channels = 1;
1054 else
1055 a2dp.enc_channels = 2;
1056
1057 ALOGV("%s: Successfully updated SBC enc format with sampling rate: %d channel mode:%d",
1058 __func__, sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
1059exit:
1060 return is_configured;
1061}
1062
1063/* API to configure APTX DSP encoder */
1064static bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1065{
1066 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1067 int mixer_size;
1068 bool is_configured = false;
1069 int ret = 0;
1070 struct aptx_enc_cfg_t aptx_dsp_cfg;
1071 mixer_size = sizeof(aptx_dsp_cfg);
1072
1073 if (aptx_bt_cfg == NULL) {
1074 ALOGE("%s: Failed to get APTX encoder config from BT", __func__);
1075 return false;
1076 }
1077
1078 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1079 if (!ctl_enc_data) {
1080 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1081 is_configured = false;
1082 goto exit;
1083 }
1084
1085 memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg));
1086 aptx_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_APTX;
1087
1088 if (!a2dp.is_aptx_dual_mono_supported) {
1089 aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1090 aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1091 } else {
1092 aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1093 aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1094 aptx_dsp_cfg.aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1095 }
1096
1097 switch (aptx_dsp_cfg.custom_cfg.num_channels) {
1098 case 1:
1099 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1100 break;
1101 case 2:
1102 default:
1103 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1104 aptx_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1105 break;
1106 }
1107 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1108 mixer_size);
1109 if (ret != 0) {
1110 ALOGE("%s: Failed to set APTX encoder config", __func__);
1111 is_configured = false;
1112 goto exit;
1113 }
1114 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
1115 if (ret != 0) {
1116 is_configured = false;
1117 goto exit;
1118 }
1119 is_configured = true;
1120 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX;
1121 a2dp.enc_channels = aptx_dsp_cfg.custom_cfg.num_channels;
1122 if (!a2dp.is_aptx_dual_mono_supported) {
1123 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1124 ALOGV("%s: Successfully updated APTX enc format with sampling rate: %d \
1125 channels:%d", __func__, aptx_dsp_cfg.custom_cfg.sample_rate,
1126 aptx_dsp_cfg.custom_cfg.num_channels);
1127 } else {
1128 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1129 ALOGV("%s: Successfully updated APTX dual mono enc format with \
1130 sampling rate: %d channels:%d sync mode %d", __func__,
1131 aptx_dsp_cfg.custom_cfg.sample_rate,
1132 aptx_dsp_cfg.custom_cfg.num_channels,
1133 aptx_dsp_cfg.aptx_v2_cfg.sync_mode);
1134 }
1135
1136exit:
1137 return is_configured;
1138}
1139
1140/* API to configure APTX HD DSP encoder
1141 */
1142static bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
1143{
1144 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1145 struct custom_enc_cfg_t aptx_dsp_cfg;
1146 bool is_configured = false;
1147 int ret = 0;
1148
1149 if (aptx_bt_cfg == NULL) {
1150 ALOGE("%s: Failed to get APTX HD encoder config from BT", __func__);
1151 return false;
1152 }
1153
1154 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1155 if (!ctl_enc_data) {
1156 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1157 is_configured = false;
1158 goto exit;
1159 }
1160
1161 memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg));
1162 aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX_HD;
1163 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
1164 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
1165 switch (aptx_dsp_cfg.num_channels) {
1166 case 1:
1167 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1168 break;
1169 case 2:
1170 default:
1171 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1172 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1173 break;
1174 }
1175 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1176 sizeof(aptx_dsp_cfg));
1177 if (ret != 0) {
1178 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
1179 is_configured = false;
1180 goto exit;
1181 }
1182 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
1183 if (ret != 0) {
1184 is_configured = false;
1185 goto exit;
1186 }
1187 is_configured = true;
1188 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX_HD;
1189 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
1190 a2dp.enc_channels = aptx_bt_cfg->channels;
1191 ALOGV("%s: Successfully updated APTX HD encformat with sampling rate: %d channels:%d",
1192 __func__, aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
1193exit:
1194 return is_configured;
1195}
1196
1197/* API to configure AAC DSP encoder */
1198static bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
1199{
1200 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1201 struct aac_enc_cfg_t aac_dsp_cfg;
1202 bool is_configured = false;
1203 int ret = 0;
1204
1205 if (aac_bt_cfg == NULL) {
1206 ALOGE("%s: Failed to get AAC encoder config from BT", __func__);
1207 return false;
1208 }
1209
1210 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1211 if (!ctl_enc_data) {
1212 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1213 is_configured = false;
1214 goto exit;
1215 }
1216 memset(&aac_dsp_cfg, 0x0, sizeof(aac_dsp_cfg));
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001217 aac_dsp_cfg.aac_cfg.enc_format = ENC_MEDIA_FMT_AAC;
1218 aac_dsp_cfg.aac_cfg.bit_rate = aac_bt_cfg->bitrate;
1219 aac_dsp_cfg.aac_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001220 switch (aac_bt_cfg->enc_mode) {
1221 case 0:
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001222 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001223 break;
1224 case 2:
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001225 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001226 break;
1227 case 1:
1228 default:
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001229 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001230 break;
1231 }
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001232 aac_dsp_cfg.aac_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
1233 aac_dsp_cfg.aac_cfg.channel_cfg = aac_bt_cfg->channels;
1234 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
1235 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001236 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
1237 sizeof(aac_dsp_cfg));
1238 if (ret != 0) {
1239 ALOGE("%s: failed to set AAC encoder config", __func__);
1240 is_configured = false;
1241 goto exit;
1242 }
1243 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
1244 if (ret != 0) {
1245 is_configured = false;
1246 goto exit;
1247 }
1248 is_configured = true;
1249 a2dp.bt_encoder_format = ENC_CODEC_TYPE_AAC;
1250 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001251 a2dp.enc_channels = aac_bt_cfg->channels;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001252 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001253 __func__, aac_dsp_cfg.aac_cfg.sample_rate, aac_dsp_cfg.aac_cfg.channel_cfg);
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001254exit:
1255 return is_configured;
1256}
1257
1258static bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
1259{
1260 struct mixer_ctl *ldac_enc_data = NULL, *ctrl_bit_format = NULL;
1261 struct ldac_enc_cfg_t ldac_dsp_cfg;
1262 bool is_configured = false;
1263 int ret = 0;
1264
1265 if (ldac_bt_cfg == NULL) {
1266 ALOGE("%s: Failed to get LDAC encoder config from BT", __func__);
1267 return false;
1268 }
1269
1270 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1271 if (!ldac_enc_data) {
1272 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1273 is_configured = false;
1274 goto exit;
1275 }
1276 memset(&ldac_dsp_cfg, 0x0, sizeof(ldac_dsp_cfg));
1277
1278 ldac_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_LDAC;
1279 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
1280 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
1281 switch (ldac_dsp_cfg.ldac_cfg.channel_mode) {
1282 case 4:
1283 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1284 ldac_dsp_cfg.custom_cfg.num_channels = 1;
1285 break;
1286 case 2:
1287 case 1:
1288 default:
1289 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1290 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1291 ldac_dsp_cfg.custom_cfg.num_channels = 2;
1292 break;
1293 }
1294
1295 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(ldac_dsp_cfg);
1296 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
1297 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001298 if (ldac_bt_cfg->is_abr_enabled) {
1299 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
1300 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
1301 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
1302 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1303 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata76ff4802018-08-06 15:30:50 -07001304 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001305 }
1306
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001307 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
1308 sizeof(ldac_dsp_cfg));
1309 if (ret != 0) {
1310 ALOGE("%s: Failed to set LDAC encoder config", __func__);
1311 is_configured = false;
1312 goto exit;
1313 }
1314 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
1315 if (ret != 0) {
1316 is_configured = false;
1317 goto exit;
1318 }
1319 is_configured = true;
1320 a2dp.bt_encoder_format = ENC_CODEC_TYPE_LDAC;
1321 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
1322 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001323 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001324 ALOGV("%s: Successfully updated LDAC encformat with sampling rate: %d channels:%d",
1325 __func__, ldac_dsp_cfg.custom_cfg.sample_rate,
1326 ldac_dsp_cfg.custom_cfg.num_channels);
1327exit:
1328 return is_configured;
1329}
1330
1331bool configure_a2dp_encoder_format()
1332{
1333 void *codec_info = NULL;
1334 uint8_t multi_cast = 0, num_dev = 1;
1335 enc_codec_t codec_type = ENC_CODEC_TYPE_INVALID;
1336 bool is_configured = false;
1337 audio_aptx_encoder_config aptx_encoder_cfg;
1338
1339 if (!a2dp.audio_get_codec_config) {
1340 ALOGE("%s: A2DP handle is not identified, ignoring A2DP encoder config", __func__);
1341 return false;
1342 }
1343 ALOGD("%s: start", __func__);
1344 codec_info = a2dp.audio_get_codec_config(&multi_cast, &num_dev,
1345 &codec_type);
1346
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001347 // ABR disabled by default for all codecs
1348 a2dp.abr_config.is_abr_enabled = false;
1349
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001350 switch (codec_type) {
1351 case ENC_CODEC_TYPE_SBC:
1352 ALOGD("%s: Received SBC encoder supported Bluetooth device", __func__);
1353 is_configured =
1354 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
1355 break;
1356 case ENC_CODEC_TYPE_APTX:
1357 ALOGD("%s: Received APTX encoder supported Bluetooth device", __func__);
1358 a2dp.is_aptx_dual_mono_supported = false;
1359 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
1360 is_configured =
1361 configure_aptx_enc_format(&aptx_encoder_cfg);
1362 break;
1363 case ENC_CODEC_TYPE_APTX_HD:
1364 ALOGD("%s: Received APTX HD encoder supported Bluetooth device", __func__);
1365 is_configured =
1366 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
1367 break;
1368 case ENC_CODEC_TYPE_AAC:
1369 ALOGD("%s: Received AAC encoder supported Bluetooth device", __func__);
1370 is_configured =
1371 configure_aac_enc_format((audio_aac_encoder_config *)codec_info);
1372 break;
1373 case ENC_CODEC_TYPE_LDAC:
1374 ALOGD("%s: Received LDAC encoder supported Bluetooth device", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001375 if (!instance_id || instance_id > MAX_INSTANCE_ID)
1376 instance_id = MAX_INSTANCE_ID;
1377 a2dp.abr_config.imc_instance = instance_id--;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001378 is_configured =
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001379 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
1380 configure_a2dp_decoder_format(ENC_CODEC_TYPE_LDAC));
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001381 break;
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +05301382 case ENC_CODEC_TYPE_PCM:
1383 ALOGD("Received PCM format for BT device");
1384 a2dp.bt_encoder_format = ENC_CODEC_TYPE_PCM;
1385 is_configured = true;
1386 break;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001387 default:
1388 ALOGD("%s: Received unsupported encoder format", __func__);
1389 is_configured = false;
1390 break;
1391 }
1392 return is_configured;
1393}
1394
1395int audio_extn_a2dp_start_playback()
1396{
1397 int ret = 0;
1398
1399 ALOGD("%s: start", __func__);
1400
1401 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_start
1402 && a2dp.audio_get_codec_config)) {
1403 ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__);
1404 return -ENOSYS;
1405 }
1406
1407 if (a2dp.a2dp_suspended) {
1408 // session will be restarted after suspend completion
1409 ALOGD("%s: A2DP start requested during suspend state", __func__);
1410 return -ENOSYS;
1411 }
1412
1413 if (!a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
1414 ALOGD("%s: calling Bluetooth module stream start", __func__);
1415 /* This call indicates Bluetooth IPC lib to start playback */
1416 ret = a2dp.audio_stream_start();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001417 if (ret != 0 ) {
1418 ALOGE("%s: Bluetooth controller start failed", __func__);
1419 a2dp.a2dp_started = false;
1420 } else {
1421 if (configure_a2dp_encoder_format() == true) {
1422 a2dp.a2dp_started = true;
1423 ret = 0;
1424 ALOGD("%s: Start playback successful to Bluetooth IPC library", __func__);
1425 } else {
1426 ALOGD("%s: unable to configure DSP encoder", __func__);
1427 a2dp.a2dp_started = false;
1428 ret = -ETIMEDOUT;
1429 }
1430 }
1431 }
1432
1433 if (a2dp.a2dp_started) {
1434 a2dp.a2dp_total_active_session_request++;
1435 a2dp_check_and_set_scrambler();
1436 a2dp_set_backend_cfg();
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001437 if (a2dp.abr_config.is_abr_enabled)
1438 start_abr();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001439 }
1440
1441 ALOGD("%s: start A2DP playback total active sessions :%d", __func__,
1442 a2dp.a2dp_total_active_session_request);
1443 return ret;
1444}
1445
1446static int reset_a2dp_enc_config_params()
1447{
1448 int ret = 0;
1449
1450 struct mixer_ctl *ctl_enc_config, *ctrl_bit_format;
1451 struct sbc_enc_cfg_t dummy_reset_config;
1452
1453 memset(&dummy_reset_config, 0x0, sizeof(dummy_reset_config));
1454 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
1455 MIXER_ENC_CONFIG_BLOCK);
1456 if (!ctl_enc_config) {
1457 ALOGE("%s: ERROR A2DP encoder format mixer control not identifed", __func__);
1458 } else {
1459 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
1460 sizeof(dummy_reset_config));
1461 a2dp.bt_encoder_format = ENC_MEDIA_FMT_NONE;
1462 }
1463
1464 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
1465
1466 return ret;
1467}
1468
Aniket Kumar Lataa85e6992018-05-22 13:56:10 -07001469static int reset_a2dp_dec_config_params()
1470{
1471 struct mixer_ctl *ctl_dec_data = NULL;
1472 struct abr_dec_cfg_t dummy_reset_cfg;
1473 int ret = 0;
1474
1475 if (a2dp.abr_config.is_abr_enabled) {
1476 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_DEC_CONFIG_BLOCK);
1477 if (!ctl_dec_data) {
1478 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
1479 return -EINVAL;
1480 }
1481 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
1482 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
1483 sizeof(dummy_reset_cfg));
1484 if (ret != 0) {
1485 ALOGE("%s: Failed to set dummy decoder config", __func__);
1486 return ret;
1487 }
1488 }
1489
1490 return ret;
1491}
1492
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001493static void reset_a2dp_config() {
1494 reset_a2dp_enc_config_params();
1495 reset_a2dp_dec_config_params();
1496 a2dp_reset_backend_cfg();
1497 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1498 stop_abr();
1499 a2dp.abr_config.is_abr_enabled = false;
1500}
1501
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001502int audio_extn_a2dp_stop_playback()
1503{
1504 int ret = 0;
1505
1506 ALOGV("%s: stop", __func__);
1507 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_stop)) {
1508 ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__);
1509 return -ENOSYS;
1510 }
1511
1512 if (a2dp.a2dp_total_active_session_request > 0)
1513 a2dp.a2dp_total_active_session_request--;
1514 else
1515 ALOGE("%s: No active playback session requests on A2DP", __func__);
1516
1517 if (a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
1518 ALOGV("%s: calling Bluetooth module stream stop", __func__);
1519 ret = a2dp.audio_stream_stop();
1520 if (ret < 0)
1521 ALOGE("%s: stop stream to Bluetooth IPC lib failed", __func__);
1522 else
1523 ALOGV("%s: stop steam to Bluetooth IPC lib successful", __func__);
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001524 if (!a2dp.a2dp_suspended)
1525 reset_a2dp_config();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001526 a2dp.a2dp_started = false;
1527 }
1528 ALOGD("%s: Stop A2DP playback total active sessions :%d", __func__,
1529 a2dp.a2dp_total_active_session_request);
1530 return 0;
1531}
1532
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001533int audio_extn_a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001534{
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001535 int ret = 0, val;
Kevin Rocard702c1572018-07-11 20:52:03 -07001536 int status = 0;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001537 char value[32] = {0};
1538 struct audio_usecase *uc_info;
1539 struct listnode *node;
1540
Petri Gynther2479b0b2018-04-17 18:38:47 -07001541 if (a2dp.is_a2dp_offload_enabled == false) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001542 ALOGV("%s: No supported encoders identified,ignoring A2DP setparam", __func__);
Kevin Rocard702c1572018-07-11 20:52:03 -07001543 status = -EINVAL;
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001544 goto param_handled;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001545 }
1546
1547 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
1548 sizeof(value));
1549 if (ret >= 0) {
1550 val = atoi(value);
1551 if (audio_is_a2dp_out_device(val)) {
1552 ALOGV("%s: Received device connect request for A2DP", __func__);
1553 open_a2dp_output();
1554 }
1555 goto param_handled;
1556 }
1557
1558 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
1559 sizeof(value));
1560
1561 if (ret >= 0) {
1562 val = atoi(value);
1563 if (audio_is_a2dp_out_device(val)) {
1564 ALOGV("%s: Received device disconnect request", __func__);
Aniket Kumar Lata30fe8682018-08-20 18:48:19 -07001565 reset_a2dp_config();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001566 close_a2dp_output();
1567 }
1568 goto param_handled;
1569 }
1570
1571 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
1572 if (ret >= 0) {
1573 if (a2dp.bt_lib_handle && (a2dp.bt_state != A2DP_STATE_DISCONNECTED)) {
Jack He0e071562018-05-11 14:39:36 -07001574 if (strncmp(value, "true", sizeof(value)) == 0) {
1575 if (a2dp.a2dp_suspended) {
1576 ALOGD("%s: A2DP is already suspended", __func__);
1577 goto param_handled;
1578 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001579 ALOGD("%s: Setting A2DP to suspend state", __func__);
1580 a2dp.a2dp_suspended = true;
1581 list_for_each(node, &a2dp.adev->usecase_list) {
1582 uc_info = node_to_item(node, struct audio_usecase, list);
1583 if (uc_info->type == PCM_PLAYBACK &&
1584 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
1585 pthread_mutex_unlock(&a2dp.adev->lock);
1586 check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
1587 pthread_mutex_lock(&a2dp.adev->lock);
1588 }
1589 }
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001590 reset_a2dp_config();
Jack He0e071562018-05-11 14:39:36 -07001591 if (a2dp.audio_stream_suspend) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001592 a2dp.audio_stream_suspend();
Jack He0e071562018-05-11 14:39:36 -07001593 }
1594 } else {
1595 if (!a2dp.a2dp_suspended) {
1596 ALOGD("%s: A2DP is already unsuspended", __func__);
1597 goto param_handled;
1598 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001599 ALOGD("%s: Resetting A2DP suspend state", __func__);
1600 struct audio_usecase *uc_info;
1601 struct listnode *node;
Jack He0e071562018-05-11 14:39:36 -07001602 if (a2dp.clear_a2dp_suspend_flag) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001603 a2dp.clear_a2dp_suspend_flag();
Jack He0e071562018-05-11 14:39:36 -07001604 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001605 a2dp.a2dp_suspended = false;
1606 /*
1607 * It is possible that before suspend, A2DP sessions can be active.
1608 * For example, during music + voice activation concurrency,
1609 * A2DP suspend will be called & Bluetooth will change to SCO mode.
1610 * Though music is paused as a part of voice activation,
1611 * compress session close happens only after pause timeout(10 secs).
1612 * So, if resume request comes before pause timeout, as A2DP session
1613 * is already active, IPC start will not be called from APM/audio_hw.
1614 * Fix this by calling A2DP start for IPC library post suspend
1615 * based on number of active session count.
1616 */
1617 if (a2dp.a2dp_total_active_session_request > 0) {
1618 ALOGD("%s: Calling Bluetooth IPC lib start post suspend state", __func__);
1619 if (a2dp.audio_stream_start) {
Kevin Rocard702c1572018-07-11 20:52:03 -07001620 status = a2dp.audio_stream_start();
1621 if (status != 0) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001622 ALOGE("%s: Bluetooth controller start failed", __func__);
1623 a2dp.a2dp_started = false;
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001624 } else {
1625 if (!configure_a2dp_encoder_format()) {
1626 ALOGE("%s: Encoder params configuration failed post suspend", __func__);
1627 a2dp.a2dp_started = false;
Kevin Rocard702c1572018-07-11 20:52:03 -07001628 status = -ETIMEDOUT;
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001629 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001630 }
1631 }
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001632 if (a2dp.a2dp_started) {
1633 a2dp_set_backend_cfg();
1634 if (a2dp.abr_config.is_abr_enabled)
1635 start_abr();
1636 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001637 }
1638 list_for_each(node, &a2dp.adev->usecase_list) {
1639 uc_info = node_to_item(node, struct audio_usecase, list);
1640 if (uc_info->type == PCM_PLAYBACK &&
1641 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
1642 pthread_mutex_unlock(&a2dp.adev->lock);
1643 check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
1644 pthread_mutex_lock(&a2dp.adev->lock);
1645 }
1646 }
1647 }
1648 }
1649 goto param_handled;
1650 }
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001651
1652 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
1653 sizeof(value));
1654 if (ret >= 0) {
1655 if (a2dp.is_a2dp_offload_enabled &&
1656 a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
1657 *reconfig = true;
1658 }
1659 goto param_handled;
1660 }
1661
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001662param_handled:
1663 ALOGV("%s: end of A2DP setparam", __func__);
Kevin Rocard702c1572018-07-11 20:52:03 -07001664 return status;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001665}
1666
1667void audio_extn_a2dp_set_handoff_mode(bool is_on)
1668{
1669 a2dp.is_handoff_in_progress = is_on;
1670}
1671
1672bool audio_extn_a2dp_is_force_device_switch()
1673{
1674 // During encoder reconfiguration mode, force A2DP device switch
1675 // Or if A2DP device is selected but earlier start failed as A2DP
1676 // was suspended, force retry.
1677 return a2dp.is_handoff_in_progress || !a2dp.a2dp_started;
1678}
1679
1680void audio_extn_a2dp_get_sample_rate(int *sample_rate)
1681{
1682 *sample_rate = a2dp.enc_sampling_rate;
1683}
1684
1685bool audio_extn_a2dp_is_ready()
1686{
1687 bool ret = false;
1688
1689 if (a2dp.a2dp_suspended)
1690 goto exit;
1691
1692 if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) &&
Petri Gynther2479b0b2018-04-17 18:38:47 -07001693 (a2dp.is_a2dp_offload_enabled) &&
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001694 (a2dp.audio_check_a2dp_ready))
1695 ret = a2dp.audio_check_a2dp_ready();
1696
1697exit:
1698 return ret;
1699}
1700
1701bool audio_extn_a2dp_is_suspended()
1702{
1703 return a2dp.a2dp_suspended;
1704}
1705
1706void audio_extn_a2dp_init(void *adev)
1707{
1708 a2dp.adev = (struct audio_device*)adev;
1709 a2dp.bt_lib_handle = NULL;
1710 a2dp_common_init();
1711 a2dp.enc_sampling_rate = 48000;
Petri Gynther2479b0b2018-04-17 18:38:47 -07001712 a2dp.is_a2dp_offload_enabled = false;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001713 a2dp.is_handoff_in_progress = false;
1714 a2dp.is_aptx_dual_mono_supported = false;
1715 reset_a2dp_enc_config_params();
Aniket Kumar Lataa85e6992018-05-22 13:56:10 -07001716 reset_a2dp_dec_config_params();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001717 update_offload_codec_support();
1718}
1719
1720uint32_t audio_extn_a2dp_get_encoder_latency()
1721{
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001722 uint32_t latency_ms = 0;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001723 int avsync_runtime_prop = 0;
1724 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
1725 aac_offset = 0, ldac_offset = 0;
1726 char value[PROPERTY_VALUE_MAX];
1727
1728 memset(value, '\0', sizeof(char) * PROPERTY_VALUE_MAX);
1729 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
1730 if (avsync_runtime_prop > 0) {
1731 if (sscanf(value, "%d/%d/%d/%d/%d",
1732 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset,
1733 &ldac_offset) != 5) {
1734 ALOGI("%s: Failed to parse avsync offset params from '%s'.", __func__, value);
1735 avsync_runtime_prop = 0;
1736 }
1737 }
1738
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001739 uint32_t slatency_ms = 0;
1740 if (a2dp.audio_get_a2dp_sink_latency && a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
1741 slatency_ms = a2dp.audio_get_a2dp_sink_latency();
1742 }
1743
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001744 switch (a2dp.bt_encoder_format) {
1745 case ENC_CODEC_TYPE_SBC:
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001746 latency_ms = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
1747 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_SBC : slatency_ms;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001748 break;
1749 case ENC_CODEC_TYPE_APTX:
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001750 latency_ms = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
1751 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_APTX : slatency_ms;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001752 break;
1753 case ENC_CODEC_TYPE_APTX_HD:
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001754 latency_ms = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
1755 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency_ms;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001756 break;
1757 case ENC_CODEC_TYPE_AAC:
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001758 latency_ms = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
1759 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_AAC : slatency_ms;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001760 break;
1761 case ENC_CODEC_TYPE_LDAC:
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001762 latency_ms = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
1763 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency_ms;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001764 break;
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +05301765 case ENC_CODEC_TYPE_PCM:
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001766 latency_ms = ENCODER_LATENCY_PCM;
1767 latency_ms += DEFAULT_SINK_LATENCY_PCM;
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +05301768 break;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001769 default:
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001770 latency_ms = DEFAULT_ENCODER_LATENCY;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001771 break;
1772 }
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001773 return latency_ms;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001774}
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001775
1776int audio_extn_a2dp_get_parameters(struct str_parms *query,
1777 struct str_parms *reply)
1778{
1779 int ret, val = 0;
1780 char value[32]={0};
1781
1782 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
1783 value, sizeof(value));
1784 if (ret >= 0) {
1785 val = a2dp.is_a2dp_offload_enabled;
1786 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
1787 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
1788 }
1789
1790 return 0;
1791}
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001792#endif // A2DP_OFFLOAD_ENABLED