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