blob: c3a8d5db71ca014e82bbdc8f092fb468a98c3c9e [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 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700756 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
757 stop_abr();
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800758 a2dp_common_init();
759 a2dp.enc_sampling_rate = 0;
760 a2dp.enc_channels = 0;
761
762 return 0;
763}
764
765static int a2dp_check_and_set_scrambler()
766{
767 bool scrambler_mode = false;
768 struct mixer_ctl *ctrl_scrambler_mode = NULL;
769 int ret = 0;
770 if (a2dp.audio_is_scrambling_enabled && (a2dp.bt_state != A2DP_STATE_DISCONNECTED))
771 scrambler_mode = a2dp.audio_is_scrambling_enabled();
772
773 // Scrambling needs to be enabled in DSP if scrambler mode is set
774 // disable scrambling not required
775 if (scrambler_mode) {
776 // enable scrambler in dsp
777 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
778 MIXER_SCRAMBLER_MODE);
779 if (!ctrl_scrambler_mode) {
780 ALOGE("%s: ERROR scrambler mode mixer control not identifed", __func__);
781 return -ENOSYS;
782 } else {
783 ret = mixer_ctl_set_value(ctrl_scrambler_mode, 0, true);
784 if (ret != 0) {
785 ALOGE("%s: Could not set scrambler mode", __func__);
786 return ret;
787 }
788 }
789 }
790 return 0;
791}
792
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700793static int a2dp_set_backend_cfg()
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800794{
795 const char *rate_str = NULL, *in_channels = NULL;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700796 uint32_t sampling_rate_rx = a2dp.enc_sampling_rate;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800797 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL;
798
799 // For LDAC encoder open slimbus port at 96Khz for 48Khz input
800 // and 88.2Khz for 44.1Khz input.
801 if ((a2dp.bt_encoder_format == ENC_CODEC_TYPE_LDAC) &&
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700802 (sampling_rate_rx == 48000 || sampling_rate_rx == 44100 )) {
803 sampling_rate_rx *= 2;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800804 }
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +0530805 // No need to configure backend for PCM format.
806 if (a2dp.bt_encoder_format == ENC_CODEC_TYPE_PCM) {
807 return 0;
808 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700809 // Set Rx backend sample rate
810 switch (sampling_rate_rx) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800811 case 44100:
812 rate_str = "KHZ_44P1";
813 break;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800814 case 88200:
815 rate_str = "KHZ_88P2";
816 break;
817 case 96000:
818 rate_str = "KHZ_96";
819 break;
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700820 case 48000:
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800821 default:
822 rate_str = "KHZ_48";
823 break;
824 }
825
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700826 ALOGV("%s: set backend rx sample rate = %s", __func__, rate_str);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800827 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700828 MIXER_SAMPLE_RATE_RX);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800829 if (!ctl_sample_rate) {
830 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700831 return -ENOSYS;
832 }
833 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
834 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
835 return -ENOSYS;
836 }
837
838 // Set Tx backend sample rate
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -0700839 if (a2dp.abr_config.is_abr_enabled) {
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700840 rate_str = ABR_TX_SAMPLE_RATE;
841
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -0700842 ALOGV("%s: set backend tx sample rate = %s", __func__, rate_str);
843 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
844 MIXER_SAMPLE_RATE_TX);
845 if (!ctl_sample_rate) {
846 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
847 return -ENOSYS;
848 }
849 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
850 ALOGE("%s: Failed to set backend sample rate = %s",
851 __func__, rate_str);
852 return -ENOSYS;
853 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800854 }
855
856 // Configure AFE input channels
857 switch (a2dp.enc_channels) {
858 case 1:
859 in_channels = "One";
860 break;
861 case 2:
862 default:
863 in_channels = "Two";
864 break;
865 }
866
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700867 ALOGV("%s: set AFE input channels = %d", __func__, a2dp.enc_channels);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800868 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
869 MIXER_AFE_IN_CHANNELS);
870 if (!ctrl_in_channels) {
871 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700872 return -ENOSYS;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800873 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700874 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
875 ALOGE("%s: Failed to set AFE in channels = %d", __func__, a2dp.enc_channels);
876 return -ENOSYS;
877 }
878
879 return 0;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800880}
881
882static int a2dp_set_bit_format(uint32_t enc_bit_format)
883{
884 const char *bit_format = NULL;
885 struct mixer_ctl *ctrl_bit_format = NULL;
886
887 // Configure AFE Input Bit Format
888 switch (enc_bit_format) {
889 case 32:
890 bit_format = "S32_LE";
891 break;
892 case 24:
893 bit_format = "S24_LE";
894 break;
895 case 16:
896 default:
897 bit_format = "S16_LE";
898 break;
899 }
900
901 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
902 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
903 MIXER_ENC_BIT_FORMAT);
904 if (!ctrl_bit_format) {
905 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
906 return -ENOSYS;
907 }
908 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
909 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
910 return -ENOSYS;
911 }
912 return 0;
913}
914
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700915static int a2dp_reset_backend_cfg()
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800916{
917 const char *rate_str = "KHZ_8", *in_channels = "Zero";
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700918 struct mixer_ctl *ctl_sample_rate_rx = NULL, *ctl_sample_rate_tx = NULL;
919 struct mixer_ctl *ctrl_in_channels = NULL;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800920
921 // Reset backend sampling rate
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700922 ALOGV("%s: reset backend sample rate = %s", __func__, rate_str);
923 ctl_sample_rate_rx = mixer_get_ctl_by_name(a2dp.adev->mixer,
924 MIXER_SAMPLE_RATE_RX);
925 if (!ctl_sample_rate_rx) {
926 ALOGE("%s: ERROR Rx backend sample rate mixer control not identifed", __func__);
927 return -ENOSYS;
928 }
929 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_rx, rate_str) != 0) {
930 ALOGE("%s: Failed to reset Rx backend sample rate = %s", __func__, rate_str);
931 return -ENOSYS;
932 }
933
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -0700934 if (a2dp.abr_config.is_abr_enabled) {
935 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
936 MIXER_SAMPLE_RATE_TX);
937 if (!ctl_sample_rate_tx) {
938 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
939 return -ENOSYS;
940 }
941 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
942 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
943 return -ENOSYS;
944 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800945 }
946
947 // Reset AFE input channels
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700948 ALOGV("%s: reset AFE input channels = %s", __func__, in_channels);
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800949 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
950 MIXER_AFE_IN_CHANNELS);
951 if (!ctrl_in_channels) {
952 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700953 return -ENOSYS;
954 }
955 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
956 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
957 return -ENOSYS;
958 }
959
960 return 0;
961}
962
963/* API to configure AFE decoder in DSP */
964static bool configure_a2dp_decoder_format(int dec_format)
965{
966 struct mixer_ctl *ctl_dec_data = NULL;
967 struct abr_dec_cfg_t dec_cfg;
968 int ret = 0;
969
970 if (a2dp.abr_config.is_abr_enabled) {
971 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_DEC_CONFIG_BLOCK);
972 if (!ctl_dec_data) {
973 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
974 return false;
975 }
976 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
977 dec_cfg.dec_format = dec_format;
978 dec_cfg.imc_info.direction = IMC_TRANSMIT;
979 dec_cfg.imc_info.enable = IMC_ENABLE;
980 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
981 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
982
983 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dec_cfg,
984 sizeof(dec_cfg));
985 if (ret != 0) {
986 ALOGE("%s: Failed to set decoder config", __func__);
987 return false;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800988 }
989 }
Aniket Kumar Lata99546312018-03-19 21:38:38 -0700990
991 return true;
Aniket Kumar Lata26483012018-01-31 20:21:42 -0800992}
993
994/* API to configure SBC DSP encoder */
995static bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
996{
997 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
998 struct sbc_enc_cfg_t sbc_dsp_cfg;
999 bool is_configured = false;
1000 int ret = 0;
1001
1002 if (sbc_bt_cfg == NULL) {
1003 ALOGE("%s: Failed to get SBC encoder config from BT", __func__);
1004 return false;
1005 }
1006
1007 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1008 if (!ctl_enc_data) {
1009 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1010 is_configured = false;
1011 goto exit;
1012 }
1013 memset(&sbc_dsp_cfg, 0x0, sizeof(sbc_dsp_cfg));
1014 sbc_dsp_cfg.enc_format = ENC_MEDIA_FMT_SBC;
1015 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1016 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1017 switch (sbc_bt_cfg->channels) {
1018 case 0:
1019 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1020 break;
1021 case 1:
1022 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1023 break;
1024 case 3:
1025 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1026 break;
1027 case 2:
1028 default:
1029 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1030 break;
1031 }
1032 if (sbc_bt_cfg->alloc)
1033 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1034 else
1035 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1036 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1037 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1038 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1039 sizeof(sbc_dsp_cfg));
1040 if (ret != 0) {
1041 ALOGE("%s: failed to set SBC encoder config", __func__);
1042 is_configured = false;
1043 goto exit;
1044 }
1045 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
1046 if (ret != 0) {
1047 is_configured = false;
1048 goto exit;
1049 }
1050 is_configured = true;
1051 a2dp.bt_encoder_format = ENC_CODEC_TYPE_SBC;
1052 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
1053
1054 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1055 a2dp.enc_channels = 1;
1056 else
1057 a2dp.enc_channels = 2;
1058
1059 ALOGV("%s: Successfully updated SBC enc format with sampling rate: %d channel mode:%d",
1060 __func__, sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
1061exit:
1062 return is_configured;
1063}
1064
1065/* API to configure APTX DSP encoder */
1066static bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1067{
1068 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1069 int mixer_size;
1070 bool is_configured = false;
1071 int ret = 0;
1072 struct aptx_enc_cfg_t aptx_dsp_cfg;
1073 mixer_size = sizeof(aptx_dsp_cfg);
1074
1075 if (aptx_bt_cfg == NULL) {
1076 ALOGE("%s: Failed to get APTX encoder config from BT", __func__);
1077 return false;
1078 }
1079
1080 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1081 if (!ctl_enc_data) {
1082 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1083 is_configured = false;
1084 goto exit;
1085 }
1086
1087 memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg));
1088 aptx_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_APTX;
1089
1090 if (!a2dp.is_aptx_dual_mono_supported) {
1091 aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1092 aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1093 } else {
1094 aptx_dsp_cfg.custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1095 aptx_dsp_cfg.custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1096 aptx_dsp_cfg.aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1097 }
1098
1099 switch (aptx_dsp_cfg.custom_cfg.num_channels) {
1100 case 1:
1101 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1102 break;
1103 case 2:
1104 default:
1105 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1106 aptx_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1107 break;
1108 }
1109 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1110 mixer_size);
1111 if (ret != 0) {
1112 ALOGE("%s: Failed to set APTX encoder config", __func__);
1113 is_configured = false;
1114 goto exit;
1115 }
1116 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
1117 if (ret != 0) {
1118 is_configured = false;
1119 goto exit;
1120 }
1121 is_configured = true;
1122 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX;
1123 a2dp.enc_channels = aptx_dsp_cfg.custom_cfg.num_channels;
1124 if (!a2dp.is_aptx_dual_mono_supported) {
1125 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1126 ALOGV("%s: Successfully updated APTX enc format with sampling rate: %d \
1127 channels:%d", __func__, aptx_dsp_cfg.custom_cfg.sample_rate,
1128 aptx_dsp_cfg.custom_cfg.num_channels);
1129 } else {
1130 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1131 ALOGV("%s: Successfully updated APTX dual mono enc format with \
1132 sampling rate: %d channels:%d sync mode %d", __func__,
1133 aptx_dsp_cfg.custom_cfg.sample_rate,
1134 aptx_dsp_cfg.custom_cfg.num_channels,
1135 aptx_dsp_cfg.aptx_v2_cfg.sync_mode);
1136 }
1137
1138exit:
1139 return is_configured;
1140}
1141
1142/* API to configure APTX HD DSP encoder
1143 */
1144static bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
1145{
1146 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1147 struct custom_enc_cfg_t aptx_dsp_cfg;
1148 bool is_configured = false;
1149 int ret = 0;
1150
1151 if (aptx_bt_cfg == NULL) {
1152 ALOGE("%s: Failed to get APTX HD encoder config from BT", __func__);
1153 return false;
1154 }
1155
1156 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1157 if (!ctl_enc_data) {
1158 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1159 is_configured = false;
1160 goto exit;
1161 }
1162
1163 memset(&aptx_dsp_cfg, 0x0, sizeof(aptx_dsp_cfg));
1164 aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX_HD;
1165 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
1166 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
1167 switch (aptx_dsp_cfg.num_channels) {
1168 case 1:
1169 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1170 break;
1171 case 2:
1172 default:
1173 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1174 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1175 break;
1176 }
1177 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1178 sizeof(aptx_dsp_cfg));
1179 if (ret != 0) {
1180 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
1181 is_configured = false;
1182 goto exit;
1183 }
1184 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
1185 if (ret != 0) {
1186 is_configured = false;
1187 goto exit;
1188 }
1189 is_configured = true;
1190 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX_HD;
1191 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
1192 a2dp.enc_channels = aptx_bt_cfg->channels;
1193 ALOGV("%s: Successfully updated APTX HD encformat with sampling rate: %d channels:%d",
1194 __func__, aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
1195exit:
1196 return is_configured;
1197}
1198
1199/* API to configure AAC DSP encoder */
1200static bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
1201{
1202 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
1203 struct aac_enc_cfg_t aac_dsp_cfg;
1204 bool is_configured = false;
1205 int ret = 0;
1206
1207 if (aac_bt_cfg == NULL) {
1208 ALOGE("%s: Failed to get AAC encoder config from BT", __func__);
1209 return false;
1210 }
1211
1212 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1213 if (!ctl_enc_data) {
1214 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1215 is_configured = false;
1216 goto exit;
1217 }
1218 memset(&aac_dsp_cfg, 0x0, sizeof(aac_dsp_cfg));
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001219 aac_dsp_cfg.aac_cfg.enc_format = ENC_MEDIA_FMT_AAC;
1220 aac_dsp_cfg.aac_cfg.bit_rate = aac_bt_cfg->bitrate;
1221 aac_dsp_cfg.aac_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001222 switch (aac_bt_cfg->enc_mode) {
1223 case 0:
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001224 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001225 break;
1226 case 2:
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001227 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001228 break;
1229 case 1:
1230 default:
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001231 aac_dsp_cfg.aac_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001232 break;
1233 }
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001234 aac_dsp_cfg.aac_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
1235 aac_dsp_cfg.aac_cfg.channel_cfg = aac_bt_cfg->channels;
1236 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
1237 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001238 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
1239 sizeof(aac_dsp_cfg));
1240 if (ret != 0) {
1241 ALOGE("%s: failed to set AAC encoder config", __func__);
1242 is_configured = false;
1243 goto exit;
1244 }
1245 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
1246 if (ret != 0) {
1247 is_configured = false;
1248 goto exit;
1249 }
1250 is_configured = true;
1251 a2dp.bt_encoder_format = ENC_CODEC_TYPE_AAC;
1252 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001253 a2dp.enc_channels = aac_bt_cfg->channels;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001254 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
Aniket Kumar Lata5790f3e2018-06-20 14:41:26 -07001255 __func__, aac_dsp_cfg.aac_cfg.sample_rate, aac_dsp_cfg.aac_cfg.channel_cfg);
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001256exit:
1257 return is_configured;
1258}
1259
1260static bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
1261{
1262 struct mixer_ctl *ldac_enc_data = NULL, *ctrl_bit_format = NULL;
1263 struct ldac_enc_cfg_t ldac_dsp_cfg;
1264 bool is_configured = false;
1265 int ret = 0;
1266
1267 if (ldac_bt_cfg == NULL) {
1268 ALOGE("%s: Failed to get LDAC encoder config from BT", __func__);
1269 return false;
1270 }
1271
1272 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1273 if (!ldac_enc_data) {
1274 ALOGE("%s: ERROR A2DP encoder config data mixer control not identifed", __func__);
1275 is_configured = false;
1276 goto exit;
1277 }
1278 memset(&ldac_dsp_cfg, 0x0, sizeof(ldac_dsp_cfg));
1279
1280 ldac_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_LDAC;
1281 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
1282 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
1283 switch (ldac_dsp_cfg.ldac_cfg.channel_mode) {
1284 case 4:
1285 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1286 ldac_dsp_cfg.custom_cfg.num_channels = 1;
1287 break;
1288 case 2:
1289 case 1:
1290 default:
1291 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1292 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1293 ldac_dsp_cfg.custom_cfg.num_channels = 2;
1294 break;
1295 }
1296
1297 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(ldac_dsp_cfg);
1298 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
1299 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001300 if (ldac_bt_cfg->is_abr_enabled) {
1301 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
1302 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
1303 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
1304 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1305 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata76ff4802018-08-06 15:30:50 -07001306 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001307 }
1308
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001309 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
1310 sizeof(ldac_dsp_cfg));
1311 if (ret != 0) {
1312 ALOGE("%s: Failed to set LDAC encoder config", __func__);
1313 is_configured = false;
1314 goto exit;
1315 }
1316 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
1317 if (ret != 0) {
1318 is_configured = false;
1319 goto exit;
1320 }
1321 is_configured = true;
1322 a2dp.bt_encoder_format = ENC_CODEC_TYPE_LDAC;
1323 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
1324 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001325 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001326 ALOGV("%s: Successfully updated LDAC encformat with sampling rate: %d channels:%d",
1327 __func__, ldac_dsp_cfg.custom_cfg.sample_rate,
1328 ldac_dsp_cfg.custom_cfg.num_channels);
1329exit:
1330 return is_configured;
1331}
1332
1333bool configure_a2dp_encoder_format()
1334{
1335 void *codec_info = NULL;
1336 uint8_t multi_cast = 0, num_dev = 1;
1337 enc_codec_t codec_type = ENC_CODEC_TYPE_INVALID;
1338 bool is_configured = false;
1339 audio_aptx_encoder_config aptx_encoder_cfg;
1340
1341 if (!a2dp.audio_get_codec_config) {
1342 ALOGE("%s: A2DP handle is not identified, ignoring A2DP encoder config", __func__);
1343 return false;
1344 }
1345 ALOGD("%s: start", __func__);
1346 codec_info = a2dp.audio_get_codec_config(&multi_cast, &num_dev,
1347 &codec_type);
1348
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001349 // ABR disabled by default for all codecs
1350 a2dp.abr_config.is_abr_enabled = false;
1351
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001352 switch (codec_type) {
1353 case ENC_CODEC_TYPE_SBC:
1354 ALOGD("%s: Received SBC encoder supported Bluetooth device", __func__);
1355 is_configured =
1356 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
1357 break;
1358 case ENC_CODEC_TYPE_APTX:
1359 ALOGD("%s: Received APTX encoder supported Bluetooth device", __func__);
1360 a2dp.is_aptx_dual_mono_supported = false;
1361 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
1362 is_configured =
1363 configure_aptx_enc_format(&aptx_encoder_cfg);
1364 break;
1365 case ENC_CODEC_TYPE_APTX_HD:
1366 ALOGD("%s: Received APTX HD encoder supported Bluetooth device", __func__);
1367 is_configured =
1368 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
1369 break;
1370 case ENC_CODEC_TYPE_AAC:
1371 ALOGD("%s: Received AAC encoder supported Bluetooth device", __func__);
1372 is_configured =
1373 configure_aac_enc_format((audio_aac_encoder_config *)codec_info);
1374 break;
1375 case ENC_CODEC_TYPE_LDAC:
1376 ALOGD("%s: Received LDAC encoder supported Bluetooth device", __func__);
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001377 if (!instance_id || instance_id > MAX_INSTANCE_ID)
1378 instance_id = MAX_INSTANCE_ID;
1379 a2dp.abr_config.imc_instance = instance_id--;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001380 is_configured =
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001381 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
1382 configure_a2dp_decoder_format(ENC_CODEC_TYPE_LDAC));
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001383 break;
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +05301384 case ENC_CODEC_TYPE_PCM:
1385 ALOGD("Received PCM format for BT device");
1386 a2dp.bt_encoder_format = ENC_CODEC_TYPE_PCM;
1387 is_configured = true;
1388 break;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001389 default:
1390 ALOGD("%s: Received unsupported encoder format", __func__);
1391 is_configured = false;
1392 break;
1393 }
1394 return is_configured;
1395}
1396
1397int audio_extn_a2dp_start_playback()
1398{
1399 int ret = 0;
1400
1401 ALOGD("%s: start", __func__);
1402
1403 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_start
1404 && a2dp.audio_get_codec_config)) {
1405 ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__);
1406 return -ENOSYS;
1407 }
1408
1409 if (a2dp.a2dp_suspended) {
1410 // session will be restarted after suspend completion
1411 ALOGD("%s: A2DP start requested during suspend state", __func__);
1412 return -ENOSYS;
1413 }
1414
1415 if (!a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
1416 ALOGD("%s: calling Bluetooth module stream start", __func__);
1417 /* This call indicates Bluetooth IPC lib to start playback */
1418 ret = a2dp.audio_stream_start();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001419 if (ret != 0 ) {
1420 ALOGE("%s: Bluetooth controller start failed", __func__);
1421 a2dp.a2dp_started = false;
1422 } else {
1423 if (configure_a2dp_encoder_format() == true) {
1424 a2dp.a2dp_started = true;
1425 ret = 0;
1426 ALOGD("%s: Start playback successful to Bluetooth IPC library", __func__);
1427 } else {
1428 ALOGD("%s: unable to configure DSP encoder", __func__);
1429 a2dp.a2dp_started = false;
1430 ret = -ETIMEDOUT;
1431 }
1432 }
1433 }
1434
1435 if (a2dp.a2dp_started) {
1436 a2dp.a2dp_total_active_session_request++;
1437 a2dp_check_and_set_scrambler();
1438 a2dp_set_backend_cfg();
Aniket Kumar Lata99546312018-03-19 21:38:38 -07001439 if (a2dp.abr_config.is_abr_enabled)
1440 start_abr();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001441 }
1442
1443 ALOGD("%s: start A2DP playback total active sessions :%d", __func__,
1444 a2dp.a2dp_total_active_session_request);
1445 return ret;
1446}
1447
1448static int reset_a2dp_enc_config_params()
1449{
1450 int ret = 0;
1451
1452 struct mixer_ctl *ctl_enc_config, *ctrl_bit_format;
1453 struct sbc_enc_cfg_t dummy_reset_config;
1454
1455 memset(&dummy_reset_config, 0x0, sizeof(dummy_reset_config));
1456 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
1457 MIXER_ENC_CONFIG_BLOCK);
1458 if (!ctl_enc_config) {
1459 ALOGE("%s: ERROR A2DP encoder format mixer control not identifed", __func__);
1460 } else {
1461 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
1462 sizeof(dummy_reset_config));
1463 a2dp.bt_encoder_format = ENC_MEDIA_FMT_NONE;
1464 }
1465
1466 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
1467
1468 return ret;
1469}
1470
Aniket Kumar Lataa85e6992018-05-22 13:56:10 -07001471static int reset_a2dp_dec_config_params()
1472{
1473 struct mixer_ctl *ctl_dec_data = NULL;
1474 struct abr_dec_cfg_t dummy_reset_cfg;
1475 int ret = 0;
1476
1477 if (a2dp.abr_config.is_abr_enabled) {
1478 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_DEC_CONFIG_BLOCK);
1479 if (!ctl_dec_data) {
1480 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
1481 return -EINVAL;
1482 }
1483 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
1484 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
1485 sizeof(dummy_reset_cfg));
1486 if (ret != 0) {
1487 ALOGE("%s: Failed to set dummy decoder config", __func__);
1488 return ret;
1489 }
1490 }
1491
1492 return ret;
1493}
1494
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001495static void reset_a2dp_config() {
1496 reset_a2dp_enc_config_params();
1497 reset_a2dp_dec_config_params();
1498 a2dp_reset_backend_cfg();
1499 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1500 stop_abr();
1501 a2dp.abr_config.is_abr_enabled = false;
1502}
1503
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001504int audio_extn_a2dp_stop_playback()
1505{
1506 int ret = 0;
1507
1508 ALOGV("%s: stop", __func__);
1509 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_stop)) {
1510 ALOGE("%s: A2DP handle is not identified, Ignoring start request", __func__);
1511 return -ENOSYS;
1512 }
1513
1514 if (a2dp.a2dp_total_active_session_request > 0)
1515 a2dp.a2dp_total_active_session_request--;
1516 else
1517 ALOGE("%s: No active playback session requests on A2DP", __func__);
1518
1519 if (a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
1520 ALOGV("%s: calling Bluetooth module stream stop", __func__);
1521 ret = a2dp.audio_stream_stop();
1522 if (ret < 0)
1523 ALOGE("%s: stop stream to Bluetooth IPC lib failed", __func__);
1524 else
1525 ALOGV("%s: stop steam to Bluetooth IPC lib successful", __func__);
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001526 if (!a2dp.a2dp_suspended)
1527 reset_a2dp_config();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001528 a2dp.a2dp_started = false;
1529 }
1530 ALOGD("%s: Stop A2DP playback total active sessions :%d", __func__,
1531 a2dp.a2dp_total_active_session_request);
1532 return 0;
1533}
1534
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001535int audio_extn_a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001536{
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001537 int ret = 0, val;
Kevin Rocard702c1572018-07-11 20:52:03 -07001538 int status = 0;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001539 char value[32] = {0};
1540 struct audio_usecase *uc_info;
1541 struct listnode *node;
1542
Petri Gynther2479b0b2018-04-17 18:38:47 -07001543 if (a2dp.is_a2dp_offload_enabled == false) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001544 ALOGV("%s: No supported encoders identified,ignoring A2DP setparam", __func__);
Kevin Rocard702c1572018-07-11 20:52:03 -07001545 status = -EINVAL;
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001546 goto param_handled;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001547 }
1548
1549 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
1550 sizeof(value));
1551 if (ret >= 0) {
1552 val = atoi(value);
1553 if (audio_is_a2dp_out_device(val)) {
1554 ALOGV("%s: Received device connect request for A2DP", __func__);
1555 open_a2dp_output();
1556 }
1557 goto param_handled;
1558 }
1559
1560 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
1561 sizeof(value));
1562
1563 if (ret >= 0) {
1564 val = atoi(value);
1565 if (audio_is_a2dp_out_device(val)) {
1566 ALOGV("%s: Received device disconnect request", __func__);
1567 reset_a2dp_enc_config_params();
Aniket Kumar Lataa85e6992018-05-22 13:56:10 -07001568 reset_a2dp_dec_config_params();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001569 close_a2dp_output();
1570 }
1571 goto param_handled;
1572 }
1573
1574 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
1575 if (ret >= 0) {
1576 if (a2dp.bt_lib_handle && (a2dp.bt_state != A2DP_STATE_DISCONNECTED)) {
Jack He0e071562018-05-11 14:39:36 -07001577 if (strncmp(value, "true", sizeof(value)) == 0) {
1578 if (a2dp.a2dp_suspended) {
1579 ALOGD("%s: A2DP is already suspended", __func__);
1580 goto param_handled;
1581 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001582 ALOGD("%s: Setting A2DP to suspend state", __func__);
1583 a2dp.a2dp_suspended = true;
1584 list_for_each(node, &a2dp.adev->usecase_list) {
1585 uc_info = node_to_item(node, struct audio_usecase, list);
1586 if (uc_info->type == PCM_PLAYBACK &&
1587 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
1588 pthread_mutex_unlock(&a2dp.adev->lock);
1589 check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
1590 pthread_mutex_lock(&a2dp.adev->lock);
1591 }
1592 }
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001593 reset_a2dp_config();
Jack He0e071562018-05-11 14:39:36 -07001594 if (a2dp.audio_stream_suspend) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001595 a2dp.audio_stream_suspend();
Jack He0e071562018-05-11 14:39:36 -07001596 }
1597 } else {
1598 if (!a2dp.a2dp_suspended) {
1599 ALOGD("%s: A2DP is already unsuspended", __func__);
1600 goto param_handled;
1601 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001602 ALOGD("%s: Resetting A2DP suspend state", __func__);
1603 struct audio_usecase *uc_info;
1604 struct listnode *node;
Jack He0e071562018-05-11 14:39:36 -07001605 if (a2dp.clear_a2dp_suspend_flag) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001606 a2dp.clear_a2dp_suspend_flag();
Jack He0e071562018-05-11 14:39:36 -07001607 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001608 a2dp.a2dp_suspended = false;
1609 /*
1610 * It is possible that before suspend, A2DP sessions can be active.
1611 * For example, during music + voice activation concurrency,
1612 * A2DP suspend will be called & Bluetooth will change to SCO mode.
1613 * Though music is paused as a part of voice activation,
1614 * compress session close happens only after pause timeout(10 secs).
1615 * So, if resume request comes before pause timeout, as A2DP session
1616 * is already active, IPC start will not be called from APM/audio_hw.
1617 * Fix this by calling A2DP start for IPC library post suspend
1618 * based on number of active session count.
1619 */
1620 if (a2dp.a2dp_total_active_session_request > 0) {
1621 ALOGD("%s: Calling Bluetooth IPC lib start post suspend state", __func__);
1622 if (a2dp.audio_stream_start) {
Kevin Rocard702c1572018-07-11 20:52:03 -07001623 status = a2dp.audio_stream_start();
1624 if (status != 0) {
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001625 ALOGE("%s: Bluetooth controller start failed", __func__);
1626 a2dp.a2dp_started = false;
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001627 } else {
1628 if (!configure_a2dp_encoder_format()) {
1629 ALOGE("%s: Encoder params configuration failed post suspend", __func__);
1630 a2dp.a2dp_started = false;
Kevin Rocard702c1572018-07-11 20:52:03 -07001631 status = -ETIMEDOUT;
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001632 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001633 }
1634 }
Aniket Kumar Lataeedbcac2018-06-21 16:46:50 -07001635 if (a2dp.a2dp_started) {
1636 a2dp_set_backend_cfg();
1637 if (a2dp.abr_config.is_abr_enabled)
1638 start_abr();
1639 }
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001640 }
1641 list_for_each(node, &a2dp.adev->usecase_list) {
1642 uc_info = node_to_item(node, struct audio_usecase, list);
1643 if (uc_info->type == PCM_PLAYBACK &&
1644 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
1645 pthread_mutex_unlock(&a2dp.adev->lock);
1646 check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
1647 pthread_mutex_lock(&a2dp.adev->lock);
1648 }
1649 }
1650 }
1651 }
1652 goto param_handled;
1653 }
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001654
1655 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
1656 sizeof(value));
1657 if (ret >= 0) {
1658 if (a2dp.is_a2dp_offload_enabled &&
1659 a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
1660 *reconfig = true;
1661 }
1662 goto param_handled;
1663 }
1664
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001665param_handled:
1666 ALOGV("%s: end of A2DP setparam", __func__);
Kevin Rocard702c1572018-07-11 20:52:03 -07001667 return status;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001668}
1669
1670void audio_extn_a2dp_set_handoff_mode(bool is_on)
1671{
1672 a2dp.is_handoff_in_progress = is_on;
1673}
1674
1675bool audio_extn_a2dp_is_force_device_switch()
1676{
1677 // During encoder reconfiguration mode, force A2DP device switch
1678 // Or if A2DP device is selected but earlier start failed as A2DP
1679 // was suspended, force retry.
1680 return a2dp.is_handoff_in_progress || !a2dp.a2dp_started;
1681}
1682
1683void audio_extn_a2dp_get_sample_rate(int *sample_rate)
1684{
1685 *sample_rate = a2dp.enc_sampling_rate;
1686}
1687
1688bool audio_extn_a2dp_is_ready()
1689{
1690 bool ret = false;
1691
1692 if (a2dp.a2dp_suspended)
1693 goto exit;
1694
1695 if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) &&
Petri Gynther2479b0b2018-04-17 18:38:47 -07001696 (a2dp.is_a2dp_offload_enabled) &&
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001697 (a2dp.audio_check_a2dp_ready))
1698 ret = a2dp.audio_check_a2dp_ready();
1699
1700exit:
1701 return ret;
1702}
1703
1704bool audio_extn_a2dp_is_suspended()
1705{
1706 return a2dp.a2dp_suspended;
1707}
1708
1709void audio_extn_a2dp_init(void *adev)
1710{
1711 a2dp.adev = (struct audio_device*)adev;
1712 a2dp.bt_lib_handle = NULL;
1713 a2dp_common_init();
1714 a2dp.enc_sampling_rate = 48000;
Petri Gynther2479b0b2018-04-17 18:38:47 -07001715 a2dp.is_a2dp_offload_enabled = false;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001716 a2dp.is_handoff_in_progress = false;
1717 a2dp.is_aptx_dual_mono_supported = false;
1718 reset_a2dp_enc_config_params();
Aniket Kumar Lataa85e6992018-05-22 13:56:10 -07001719 reset_a2dp_dec_config_params();
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001720 update_offload_codec_support();
1721}
1722
1723uint32_t audio_extn_a2dp_get_encoder_latency()
1724{
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001725 uint32_t latency_ms = 0;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001726 int avsync_runtime_prop = 0;
1727 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
1728 aac_offset = 0, ldac_offset = 0;
1729 char value[PROPERTY_VALUE_MAX];
1730
1731 memset(value, '\0', sizeof(char) * PROPERTY_VALUE_MAX);
1732 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
1733 if (avsync_runtime_prop > 0) {
1734 if (sscanf(value, "%d/%d/%d/%d/%d",
1735 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset,
1736 &ldac_offset) != 5) {
1737 ALOGI("%s: Failed to parse avsync offset params from '%s'.", __func__, value);
1738 avsync_runtime_prop = 0;
1739 }
1740 }
1741
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001742 uint32_t slatency_ms = 0;
1743 if (a2dp.audio_get_a2dp_sink_latency && a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
1744 slatency_ms = a2dp.audio_get_a2dp_sink_latency();
1745 }
1746
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001747 switch (a2dp.bt_encoder_format) {
1748 case ENC_CODEC_TYPE_SBC:
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001749 latency_ms = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
1750 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_SBC : slatency_ms;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001751 break;
1752 case ENC_CODEC_TYPE_APTX:
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001753 latency_ms = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
1754 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_APTX : slatency_ms;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001755 break;
1756 case ENC_CODEC_TYPE_APTX_HD:
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001757 latency_ms = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
1758 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency_ms;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001759 break;
1760 case ENC_CODEC_TYPE_AAC:
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001761 latency_ms = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
1762 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_AAC : slatency_ms;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001763 break;
1764 case ENC_CODEC_TYPE_LDAC:
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001765 latency_ms = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
1766 latency_ms += (slatency_ms == 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency_ms;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001767 break;
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +05301768 case ENC_CODEC_TYPE_PCM:
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001769 latency_ms = ENCODER_LATENCY_PCM;
1770 latency_ms += DEFAULT_SINK_LATENCY_PCM;
Bala Kishore Pati3a5da9d2018-05-10 16:41:45 +05301771 break;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001772 default:
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001773 latency_ms = DEFAULT_ENCODER_LATENCY;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001774 break;
1775 }
Aniket Kumar Latada256de2018-08-07 14:32:33 -07001776 return latency_ms;
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001777}
Aniket Kumar Latad6834ef2018-04-06 17:02:48 -07001778
1779int audio_extn_a2dp_get_parameters(struct str_parms *query,
1780 struct str_parms *reply)
1781{
1782 int ret, val = 0;
1783 char value[32]={0};
1784
1785 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
1786 value, sizeof(value));
1787 if (ret >= 0) {
1788 val = a2dp.is_a2dp_offload_enabled;
1789 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
1790 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
1791 }
1792
1793 return 0;
1794}
Aniket Kumar Lata26483012018-01-31 20:21:42 -08001795#endif // A2DP_OFFLOAD_ENABLED