blob: 4a0f96407c6ba9bbe5d5ca1af394e4b936b6ad96 [file] [log] [blame]
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301/*
sriram kumarcd549b02021-02-12 14:27:17 +05302* Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of The Linux Foundation nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
Aalique Grahame22e49102018-12-18 14:23:57 -080029#define LOG_TAG "a2dp_offload"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053030/*#define LOG_NDEBUG 0*/
31#define LOG_NDDEBUG 0
32#include <errno.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080033#include <log/log.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053034#include <dlfcn.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080035#include <pthread.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
Manish Dewangan6a252632017-12-04 17:27:44 +053039#include "audio_extn.h"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053040#include <stdlib.h>
41#include <cutils/str_parms.h>
42#include <hardware/audio.h>
43#include <hardware/hardware.h>
44#include <cutils/properties.h>
45
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046#ifdef DYNAMIC_LOG_ENABLED
47#include <log_xml_parser.h>
48#define LOG_MASK HAL_MOD_FILE_A2DP
49#include <log_utils.h>
50#endif
51
Naresh Tanniru66cf06c2019-03-20 19:30:37 +053052#define BT_IPC_SOURCE_LIB_NAME "btaudio_offload_if.so"
Florian Pfister1a84f312018-07-19 14:38:18 +020053#define BT_IPC_SINK_LIB_NAME "libbthost_if_sink.so"
54#define MEDIA_FMT_NONE 0
55#define MEDIA_FMT_AAC 0x00010DA6
56#define MEDIA_FMT_APTX 0x000131ff
57#define MEDIA_FMT_APTX_HD 0x00013200
Surendar Karka2febd452018-12-13 17:56:43 +053058#define MEDIA_FMT_APTX_AD 0x00013204
Florian Pfister1a84f312018-07-19 14:38:18 +020059#define MEDIA_FMT_SBC 0x00010BF2
60#define MEDIA_FMT_CELT 0x00013221
61#define MEDIA_FMT_LDAC 0x00013224
62#define MEDIA_FMT_MP3 0x00010BE9
63#define MEDIA_FMT_APTX_ADAPTIVE 0x00013204
Zhou Song12c29502019-03-16 10:37:18 +080064#define MEDIA_FMT_APTX_AD_SPEECH 0x00013208
Manisha Agarwald45632b2019-10-17 18:14:28 +080065#define MEDIA_FMT_LC3 0x0001337E
Naresh Tanniru9d027a62015-03-13 01:32:10 +053066#define MEDIA_FMT_AAC_AOT_LC 2
67#define MEDIA_FMT_AAC_AOT_SBR 5
68#define MEDIA_FMT_AAC_AOT_PS 29
Naresh Tanniru9d027a62015-03-13 01:32:10 +053069#define PCM_CHANNEL_L 1
70#define PCM_CHANNEL_R 2
71#define PCM_CHANNEL_C 3
72#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
73#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
74#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
75#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
76#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
77#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
Surendar Karka2febd452018-12-13 17:56:43 +053078#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
Zhou Song8fccbb62019-03-20 01:08:19 +080079#define MIXER_ENC_APTX_AD_CONFIG_BLOCK "SLIM_7_RX APTX_AD Enc Cfg"
Surendar Karka2febd452018-12-13 17:56:43 +053080#define MIXER_SOURCE_DEC_CONFIG_BLOCK "SLIM_7_TX Decoder Config"
81#define MIXER_SINK_DEC_CONFIG_BLOCK "SLIM_9_TX Decoder Config"
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +053082#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
Florian Pfister1a84f312018-07-19 14:38:18 +020083#define MIXER_DEC_BIT_FORMAT "AFE Output Bit Format"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053084#define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080085#define MIXER_SAMPLE_RATE_RX "BT SampleRate RX"
Surendar Karka2febd452018-12-13 17:56:43 +053086#define MIXER_SOURCE_SAMPLE_RATE_TX "BT SampleRate TX"
Samyak Jainf69e9ef2018-06-12 12:26:37 +053087#define MIXER_SAMPLE_RATE_DEFAULT "BT SampleRate"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053088#define MIXER_AFE_IN_CHANNELS "AFE Input Channels"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080089#define MIXER_ABR_TX_FEEDBACK_PATH "A2DP_SLIM7_UL_HL Switch"
Zhou Song12c29502019-03-16 10:37:18 +080090#define MIXER_ABR_RX_FEEDBACK_PATH "SCO_SLIM7_DL_HL Switch"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080091#define MIXER_SET_FEEDBACK_CHANNEL "BT set feedback channel"
Surendar Karka2febd452018-12-13 17:56:43 +053092#define MIXER_SINK_SAMPLE_RATE "BT_TX SampleRate"
Florian Pfister1a84f312018-07-19 14:38:18 +020093#define MIXER_AFE_SINK_CHANNELS "AFE Output Channels"
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +053094#define MIXER_AFE_SINK_CHANNELS_SLIM7 "AFE Output Channels SLIM7"
Manisha Agarwala51768b2018-11-01 16:30:52 +053095#define MIXER_FMT_TWS_CHANNEL_MODE "TWS Channel Mode"
Manisha Agarwald45632b2019-10-17 18:14:28 +080096#define MIXER_FMT_LC3_CHANNEL_MODE "LC3 Channel Mode"
yidongh0515e042017-07-06 15:00:34 +080097#define ENCODER_LATENCY_SBC 10
98#define ENCODER_LATENCY_APTX 40
99#define ENCODER_LATENCY_APTX_HD 20
100#define ENCODER_LATENCY_AAC 70
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530101//To Do: Fine Tune Encoder CELT/LDAC latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530102#define ENCODER_LATENCY_CELT 40
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530103#define ENCODER_LATENCY_LDAC 40
Aalique Grahame22e49102018-12-18 14:23:57 -0800104#define ENCODER_LATENCY_PCM 50
yidongh0515e042017-07-06 15:00:34 +0800105#define DEFAULT_SINK_LATENCY_SBC 140
106#define DEFAULT_SINK_LATENCY_APTX 160
107#define DEFAULT_SINK_LATENCY_APTX_HD 180
108#define DEFAULT_SINK_LATENCY_AAC 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530109//To Do: Fine Tune Default CELT/LDAC Latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530110#define DEFAULT_SINK_LATENCY_CELT 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530111#define DEFAULT_SINK_LATENCY_LDAC 180
Aalique Grahame22e49102018-12-18 14:23:57 -0800112#define DEFAULT_SINK_LATENCY_PCM 140
113
Manisha Agarwald45632b2019-10-17 18:14:28 +0800114// decoder structure is considered.
115#define BAP_UNICAST 1
116// decoder structure is ignored.
117#define BAP_BROADCAST 2
118// decoder structure is treated as second toAirConfig.
119#define BAP_BA_SIMULCAST 3
120
Aalique Grahame22e49102018-12-18 14:23:57 -0800121#define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported"
122#define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled"
123#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
124
125// Default encoder bit width
126#define DEFAULT_ENCODER_BIT_FORMAT 16
127
Manisha Agarwald45632b2019-10-17 18:14:28 +0800128// PCM_24 encoder bit width
129#define ENCODER_BIT_FORMAT_PCM_24 24
130
Aalique Grahame22e49102018-12-18 14:23:57 -0800131// Default encoder latency
132#define DEFAULT_ENCODER_LATENCY 200
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530133
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800134// Slimbus Tx sample rate for ABR feedback channel
135#define ABR_TX_SAMPLE_RATE "KHZ_8"
136
Zhou Song12c29502019-03-16 10:37:18 +0800137// Slimbus Tx sample rate for APTX AD SPEECH
138#define SPEECH_TX_SAMPLE_RATE "KHZ_96"
139
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800140// Purpose ID for Inter Module Communication (IMC) in AFE
141#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
142
143// Maximum quality levels for ABR
144#define MAX_ABR_QUALITY_LEVELS 5
145
146// Instance identifier for A2DP
147#define MAX_INSTANCE_ID (UINT32_MAX / 2)
148
Zhou Song12c29502019-03-16 10:37:18 +0800149// Instance identifier for SWB
150#define APTX_AD_SPEECH_INSTANCE_ID 37
151
152#define SAMPLING_RATE_96K 96000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530153#define SAMPLING_RATE_48K 48000
154#define SAMPLING_RATE_441K 44100
Zhou Song12c29502019-03-16 10:37:18 +0800155#define SAMPLING_RATE_32K 32000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530156#define CH_STEREO 2
157#define CH_MONO 1
Florian Pfister1a84f312018-07-19 14:38:18 +0200158#define SOURCE 0
159#define SINK 1
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800160#define UNINITIALIZED -1
161
162#ifdef __LP64__
163#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
164#else
165#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
166#endif
167
Manisha Agarwald45632b2019-10-17 18:14:28 +0800168#define AUDIO_LOCATION_MAX 28
169
170uint32_t audio_location_map_array[] = { AUDIO_LOCATION_FRONT_LEFT, AUDIO_LOCATION_FRONT_RIGHT, AUDIO_LOCATION_FRONT_CENTER, AUDIO_LOCATION_LOW_FREQUENCY,
171 AUDIO_LOCATION_BACK_LEFT, AUDIO_LOCATION_BACK_RIGHT, AUDIO_LOCATION_FRONT_LEFT_OF_CENTER,
172 AUDIO_LOCATION_FRONT_RIGHT_OF_CENTER, AUDIO_LOCATION_BACK_CENTER, AUDIO_LOCATION_LOW_FREQUENCY_2,
173 AUDIO_LOCATION_SIDE_LEFT, AUDIO_LOCATION_SIDE_RIGHT, AUDIO_LOCATION_TOP_FRONT_LEFT, AUDIO_LOCATION_TOP_FRONT_RIGHT,
174 AUDIO_LOCATION_TOP_FRONT_CENTER, AUDIO_LOCATION_TOP_CENTER, AUDIO_LOCATION_TOP_BACK_LEFT, AUDIO_LOCATION_TOP_BACK_RIGHT,
175 AUDIO_LOCATION_TOP_SIDE_LEFT, AUDIO_LOCATION_TOP_SIDE_RIGHT, AUDIO_LOCATION_TOP_BACK_CENTER,
176 AUDIO_LOCATION_BOTTOM_FRONT_CENTER, AUDIO_LOCATION_BOTTOM_FRONT_LEFT, AUDIO_LOCATION_BOTTOM_FRONT_RIGHT,
177 AUDIO_LOCATION_FRONT_LEFT_WIDE, AUDIO_LOCATION_FRONT_RIGHT_WIDE, AUDIO_LOCATION_LEFT_SURROUND,
178 AUDIO_LOCATION_RIGHT_SURROUND };
179
180int channel_map_array[] = { PCM_CHANNEL_L, PCM_CHANNEL_R, PCM_CHANNEL_C, PCM_CHANNEL_LFE, PCM_CHANNEL_LB, PCM_CHANNEL_RB, PCM_CHANNEL_FLC,
181 PCM_CHANNEL_FRC, PCM_CHANNEL_CB, PCM_CHANNEL_RS, PCM_CHANNEL_SL, PCM_CHANNEL_SR, PCM_CHANNEL_TFL,
182 PCM_CHANNEL_TFR, PCM_CHANNEL_TFC, PCM_CHANNEL_TC, PCM_CHANNEL_TBL, PCM_CHANNEL_TBR, PCM_CHANNEL_TSL,
183 PCM_CHANNEL_TSR, PCM_CHANNEL_TBC, PCM_CHANNEL_BFC, PCM_CHANNEL_BFL, PCM_CHANNEL_BFR, PCM_CHANNEL_LW,
184 PCM_CHANNEL_RW, PCM_CHANNEL_LS, PCM_CHANNEL_RS };
185
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800186static void *vndk_fwk_lib_handle = NULL;
187static int is_running_with_enhanced_fwk = UNINITIALIZED;
188
189typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
190static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
Florian Pfister1a84f312018-07-19 14:38:18 +0200191
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530192/*
193 * Below enum values are extended from audio_base.h to
Florian Pfister1a84f312018-07-19 14:38:18 +0200194 * to keep encoder and decoder type local to bthost_ipc
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530195 * and audio_hal as these are intended only for handshake
196 * between IPC lib and Audio HAL.
197 */
198typedef enum {
Florian Pfister1a84f312018-07-19 14:38:18 +0200199 CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
200 CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
201 CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
202 CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
203 CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530204#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +0200205 CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530206#endif
Florian Pfister1a84f312018-07-19 14:38:18 +0200207 CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
208 CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
Surendar Karka2febd452018-12-13 17:56:43 +0530209 CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
Zhou Song12c29502019-03-16 10:37:18 +0800210 CODEC_TYPE_APTX_AD_SPEECH = 637534208u, //0x26000000UL
Manisha Agarwald45632b2019-10-17 18:14:28 +0800211 CODEC_TYPE_LC3 = 721420288u, //0x2B000000UL
Aalique Grahame22e49102018-12-18 14:23:57 -0800212 CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u
Florian Pfister1a84f312018-07-19 14:38:18 +0200213}codec_t;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530214
Sharad Sangle95d451b2018-06-19 12:24:20 +0530215/*
216 * enums which describes the APTX Adaptive
217 * channel mode, these values are used by encoder
218 */
219 typedef enum {
220 APTX_AD_CHANNEL_UNCHANGED = -1,
221 APTX_AD_CHANNEL_JOINT_STEREO = 0, // default
222 APTX_AD_CHANNEL_MONO = 1,
223 APTX_AD_CHANNEL_DUAL_MONO = 2,
224 APTX_AD_CHANNEL_STEREO_TWS = 4,
225 APTX_AD_CHANNEL_EARBUD = 8,
226} enc_aptx_ad_channel_mode;
227
228/*
229 * enums which describes the APTX Adaptive
230 * sampling frequency, these values are used
231 * by encoder
232 */
233typedef enum {
234 APTX_AD_SR_UNCHANGED = 0x0,
235 APTX_AD_48 = 0x1, // 48 KHz default
236 APTX_AD_44_1 = 0x2, // 44.1kHz
Zhou Song30e8cea2019-10-22 23:39:25 +0800237 APTX_AD_96 = 0x3, // 96KHz
Sharad Sangle95d451b2018-06-19 12:24:20 +0530238} enc_aptx_ad_s_rate;
239
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530240typedef void (*bt_audio_pre_init_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200241typedef int (*audio_source_open_t)(void);
242typedef int (*audio_source_close_t)(void);
243typedef int (*audio_source_start_t)(void);
244typedef int (*audio_source_stop_t)(void);
245typedef int (*audio_source_suspend_t)(void);
246typedef void (*audio_source_handoff_triggered_t)(void);
247typedef void (*clear_source_a2dpsuspend_flag_t)(void);
248typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
249 uint8_t *num_dev, codec_t *codec_type);
250typedef int (*audio_source_check_a2dp_ready_t)(void);
251typedef int (*audio_is_source_scrambling_enabled_t)(void);
Manisha Agarwala51768b2018-11-01 16:30:52 +0530252typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200253typedef int (*audio_sink_start_t)(void);
254typedef int (*audio_sink_stop_t)(void);
255typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
256typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
257typedef int (*audio_sink_check_a2dp_ready_t)(void);
258typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530259
260enum A2DP_STATE {
261 A2DP_STATE_CONNECTED,
262 A2DP_STATE_STARTED,
263 A2DP_STATE_STOPPED,
264 A2DP_STATE_DISCONNECTED,
265};
266
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800267typedef enum {
268 IMC_TRANSMIT,
269 IMC_RECEIVE,
270} imc_direction_t;
271
272typedef enum {
273 IMC_DISABLE,
274 IMC_ENABLE,
275} imc_status_t;
276
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700277typedef enum {
Zhou Song12c29502019-03-16 10:37:18 +0800278 SWAP_DISABLE,
279 SWAP_ENABLE,
280} swap_status_t;
281
282typedef enum {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700283 MTU_SIZE,
284 PEAK_BIT_RATE,
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530285 BIT_RATE_MODE,
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700286} frame_control_type_t;
287
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800288// --- external function dependency ---
289fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
Weiyin Jiang280ea742020-09-08 20:28:22 +0800290fp_check_a2dp_restore_t fp_check_a2dp_restore_l;
Gautam Manamfbb3ebc2020-10-08 18:06:45 +0530291fp_platform_switch_voice_call_device_post_t fp_platform_switch_voice_call_device_post;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800292
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800293/* PCM config for ABR Feedback hostless front end */
294static struct pcm_config pcm_config_abr = {
295 .channels = 1,
296 .rate = 8000,
297 .period_size = 240,
298 .period_count = 2,
299 .format = PCM_FORMAT_S16_LE,
300 .start_threshold = 0,
301 .stop_threshold = INT_MAX,
302 .avail_min = 0,
303};
304
305/* Adaptive bitrate config for A2DP codecs */
306struct a2dp_abr_config {
307 /* Flag to denote whether Adaptive bitrate is enabled for codec */
308 bool is_abr_enabled;
309 /* Flag to denote whether front end has been opened for ABR */
310 bool abr_started;
311 /* ABR Tx path pcm handle */
312 struct pcm *abr_tx_handle;
Zhou Song12c29502019-03-16 10:37:18 +0800313 /* ABR Rx path pcm handle */
314 struct pcm *abr_rx_handle;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800315 /* ABR Inter Module Communication (IMC) instance ID */
316 uint32_t imc_instance;
317};
318
319static uint32_t instance_id = MAX_INSTANCE_ID;
320
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530321/* structure used to update a2dp state machine
322 * to communicate IPC library
323 * to store DSP encoder configuration information
324 */
325struct a2dp_data {
326 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200327 void *bt_lib_source_handle;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530328 bt_audio_pre_init_t bt_audio_pre_init;
Florian Pfister1a84f312018-07-19 14:38:18 +0200329 audio_source_open_t audio_source_open;
330 audio_source_close_t audio_source_close;
331 audio_source_start_t audio_source_start;
332 audio_source_stop_t audio_source_stop;
333 audio_source_suspend_t audio_source_suspend;
334 audio_source_handoff_triggered_t audio_source_handoff_triggered;
335 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
336 audio_get_enc_config_t audio_get_enc_config;
337 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530338 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200339 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
340 enum A2DP_STATE bt_state_source;
341 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530342 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530343 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200344 bool a2dp_source_started;
345 bool a2dp_source_suspended;
346 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530347 bool is_a2dp_offload_supported;
348 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700349 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530350 /* Mono Mode support for TWS+ */
351 bool is_tws_mono_mode_on;
Manisha Agarwald45632b2019-10-17 18:14:28 +0800352 /* Mono Mode support for LC3 */
353 bool is_lc3_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530354 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800355 /* Adaptive bitrate config for A2DP codecs */
356 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200357
358 void *bt_lib_sink_handle;
359 audio_sink_start_t audio_sink_start;
360 audio_sink_stop_t audio_sink_stop;
361 audio_get_dec_config_t audio_get_dec_config;
362 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
363 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
364 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
365 enum A2DP_STATE bt_state_sink;
366 codec_t bt_decoder_format;
367 uint32_t dec_sampling_rate;
368 uint32_t dec_channels;
369 bool a2dp_sink_started;
370 int a2dp_sink_total_active_session_requests;
Zhou Song12c29502019-03-16 10:37:18 +0800371 bool swb_configured;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530372};
373
374struct a2dp_data a2dp;
375
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800376/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
377 * Structures sent to configure DSP for ABR are defined below.
378 * This data helps DSP configure feedback path (BTSoC to LPASS)
379 * for link quality levels and mapping quality levels to codec
380 * specific bitrate.
381 */
382
383/* Key value pair for link quality level to bitrate mapping. */
384struct bit_rate_level_map_t {
385 uint32_t link_quality_level;
386 uint32_t bitrate;
387};
388
389/* Link quality level to bitrate mapping info sent to DSP. */
390struct quality_level_to_bitrate_info {
391 /* Number of quality levels being mapped.
392 * This will be equal to the size of mapping table.
393 */
394 uint32_t num_levels;
395 /* Quality level to bitrate mapping table */
396 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
397};
398
399/* Structure to set up Inter Module Communication (IMC) between
400 * AFE Decoder and Encoder.
401 */
402struct imc_dec_enc_info {
403 /* Decoder to encoder communication direction.
404 * Transmit = 0 / Receive = 1
405 */
406 uint32_t direction;
407 /* Enable / disable IMC between decoder and encoder */
408 uint32_t enable;
409 /* Purpose of IMC being set up between decoder and encoder.
410 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
411 * is the default value to be sent as purpose.
412 */
413 uint32_t purpose;
414 /* Unique communication instance ID.
415 * purpose and comm_instance together form the actual key
416 * used in IMC registration, which must be the same for
417 * encoder and decoder for which IMC is being set up.
418 */
419 uint32_t comm_instance;
420};
421
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700422/* Structure to control frame size of AAC encoded frames. */
423struct aac_frame_size_control_t {
424 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
425 uint32_t ctl_type;
426 /* Control value
427 * MTU_SIZE: MTU size in bytes
428 * PEAK_BIT_RATE: Peak bitrate in bits per second.
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530429 * BIT_RATE_MODE: Variable bitrate
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700430 */
431 uint32_t ctl_value;
432};
433
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800434/* Structure used for ABR config of AFE encoder and decoder. */
435struct abr_enc_cfg_t {
436 /* Link quality level to bitrate mapping info sent to DSP. */
437 struct quality_level_to_bitrate_info mapping_info;
438 /* Information to set up IMC between decoder and encoder */
439 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700440 /* Flag to indicate whether ABR is enabled */
441 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800442} __attribute__ ((packed));
443
444/* Structure to send configuration for decoder introduced
445 * on AFE Tx path for ABR link quality feedback to BT encoder.
446 */
447struct abr_dec_cfg_t {
448 /* Decoder media format */
449 uint32_t dec_format;
450 /* Information to set up IMC between decoder and encoder */
451 struct imc_dec_enc_info imc_info;
452} __attribute__ ((packed));
453
Zhou Song12c29502019-03-16 10:37:18 +0800454struct aptx_ad_speech_mode_cfg_t
455{
456 uint32_t mode;
457 uint32_t swapping;
458} __attribute__ ((packed));
459
460/* Structure for SWB voice dec config */
461struct aptx_ad_speech_dec_cfg_t {
462 struct abr_dec_cfg_t abr_cfg;
463 struct aptx_ad_speech_mode_cfg_t speech_mode;
464} __attribute__ ((packed));
465
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530466/* START of DSP configurable structures
467 * These values should match with DSP interface defintion
468 */
469
470/* AAC encoder configuration structure. */
471typedef struct aac_enc_cfg_t aac_enc_cfg_t;
472
473/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
474 * supported aac_fmt_flag are ADTS/RAW
475 * supported channel_cfg are Native mode, Mono , Stereo
476 */
477struct aac_enc_cfg_t {
478 uint32_t enc_format;
479 uint32_t bit_rate;
480 uint32_t enc_mode;
481 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530482 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530483 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530484} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530485
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700486struct aac_enc_cfg_v2_t {
487 struct aac_enc_cfg_t aac_enc_cfg;
488 struct aac_frame_size_control_t frame_ctl;
489} __attribute__ ((packed));
490
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530491struct aac_enc_cfg_v3_t {
492 struct aac_enc_cfg_t aac_enc_cfg;
493 struct aac_frame_size_control_t frame_ctl;
494 struct aac_frame_size_control_t aac_key_value_ctl;
495} __attribute__ ((packed));
496
Surendar Karkabbb3c822018-11-12 13:00:38 +0530497typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
498struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200499 uint16_t aac_fmt_flag; /* LATM*/
500 uint16_t audio_object_type; /* LC */
501 uint16_t channels; /* Stereo */
502 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
503 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
504 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530505} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200506
Surendar Karkabbb3c822018-11-12 13:00:38 +0530507typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
508struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200509 uint16_t channels; /* Mono, Stereo */
510 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
511 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530512} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200513
514/* AAC decoder configuration structure. */
515typedef struct aac_dec_cfg_t aac_dec_cfg_t;
516struct aac_dec_cfg_t {
517 uint32_t dec_format;
518 audio_aac_decoder_config_t data;
519} __attribute__ ((packed));
520
521/* SBC decoder configuration structure. */
522typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
523struct sbc_dec_cfg_t {
524 uint32_t dec_format;
525 audio_sbc_decoder_config_t data;
526} __attribute__ ((packed));
527
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530528/* SBC encoder configuration structure. */
529typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
530
531/* supported num_subbands are 4/8
532 * supported blk_len are 4, 8, 12, 16
533 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
534 * supported alloc_method are LOUNDNESS/SNR
535 * supported bit_rate for mono channel is max 320kbps
536 * supported bit rate for stereo channel is max 512 kbps
537 */
538struct sbc_enc_cfg_t{
539 uint32_t enc_format;
540 uint32_t num_subbands;
541 uint32_t blk_len;
542 uint32_t channel_mode;
543 uint32_t alloc_method;
544 uint32_t bit_rate;
545 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530546} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530547
548
549/* supported num_channels are Mono/Stereo
550 * supported channel_mapping for mono is CHANNEL_C
551 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
552 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700553 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530554struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530555{
556 uint32_t enc_format;
557 uint32_t sample_rate;
558 uint16_t num_channels;
559 uint16_t reserved;
560 uint8_t channel_mapping[8];
561 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530562} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530563
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530564struct celt_specific_enc_cfg_t
565{
566 uint32_t bit_rate;
567 uint16_t frame_size;
568 uint16_t complexity;
569 uint16_t prediction_mode;
570 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530571} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530572
573struct celt_enc_cfg_t
574{
575 struct custom_enc_cfg_t custom_cfg;
576 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530577} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700578
579/* sync_mode introduced with APTX V2 libraries
580 * sync mode: 0x0 = stereo sync mode
581 * 0x01 = dual mono sync mode
582 * 0x02 = dual mono with no sync on either L or R codewords
583 */
584struct aptx_v2_enc_cfg_ext_t
585{
586 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530587} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700588
589/* APTX struct for combining custom enc and V2 fields */
590struct aptx_enc_cfg_t
591{
592 struct custom_enc_cfg_t custom_cfg;
593 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530594} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700595
Sharad Sangle95d451b2018-06-19 12:24:20 +0530596/* APTX AD structure */
597struct aptx_ad_enc_cfg_ext_t
598{
599 uint32_t sampling_freq;
600 uint32_t mtu;
601 uint32_t channel_mode;
602 uint32_t min_sink_modeA;
603 uint32_t max_sink_modeA;
604 uint32_t min_sink_modeB;
605 uint32_t max_sink_modeB;
606 uint32_t min_sink_modeC;
607 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530608 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530609} __attribute__ ((packed));
610
611struct aptx_ad_enc_cfg_t
612{
613 struct custom_enc_cfg_t custom_cfg;
614 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
615 struct abr_enc_cfg_t abr_cfg;
616} __attribute__ ((packed));
617
Zhou Song8fccbb62019-03-20 01:08:19 +0800618struct aptx_ad_enc_cfg_ext_r2_t
619{
620 uint32_t sampling_freq;
621 uint32_t mtu;
622 uint32_t channel_mode;
623 uint32_t min_sink_modeA;
624 uint32_t max_sink_modeA;
625 uint32_t min_sink_modeB;
626 uint32_t max_sink_modeB;
627 uint32_t min_sink_modeC;
628 uint32_t max_sink_modeC;
629 uint32_t mode;
630 uint32_t input_mode;
631 uint32_t fade_duration;
632 uint8_t sink_cap[11];
633} __attribute__ ((packed));
634
635struct aptx_ad_enc_cfg_r2_t
636{
637 struct custom_enc_cfg_t custom_cfg;
638 struct aptx_ad_enc_cfg_ext_r2_t aptx_ad_cfg;
639 struct abr_enc_cfg_t abr_cfg;
640} __attribute__ ((packed));
641
Zhou Song12c29502019-03-16 10:37:18 +0800642/* APTX AD SPEECH structure */
643struct aptx_ad_speech_enc_cfg_t
644{
645 struct custom_enc_cfg_t custom_cfg;
646 /* Information to set up IMC between decoder and encoder */
647 struct imc_dec_enc_info imc_info;
648 struct aptx_ad_speech_mode_cfg_t speech_mode;
649} __attribute__ ((packed));
650
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530651struct ldac_specific_enc_cfg_t
652{
653 uint32_t bit_rate;
654 uint16_t channel_mode;
655 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530656} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530657
658struct ldac_enc_cfg_t
659{
660 struct custom_enc_cfg_t custom_cfg;
661 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800662 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530663} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530664
Manisha Agarwald45632b2019-10-17 18:14:28 +0800665/* LC3 structure */
666struct lc3_stream_info_t
667{
668 uint32_t stream_id;
669 uint32_t direction;
670 uint32_t channel_mask_lsw;
671 uint32_t channel_mask_msw;
672} __attribute__ ((packed));
673
674struct lc3_stream_map_info_t
675{
676 uint32_t stream_map_size;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +0530677 struct lc3_stream_info_t streamMap[2];
Manisha Agarwald45632b2019-10-17 18:14:28 +0800678} __attribute__ ((packed));
679
680struct lc3_stream_map_ext_t
681{
682 uint32_t audio_location;
683 uint8_t stream_id;
684 uint8_t direction;
685} __attribute__ ((packed));
686
687struct lc3_config_ext_t
688{
689 uint32_t api_version;
690 uint32_t sampling_freq;
691 uint32_t max_octets_per_frame;
692 uint32_t frame_duration;//7.5msec, 10msec
693 uint32_t bit_depth;
694 uint32_t num_blocks;
695 uint8_t default_q_level;
696 uint8_t vendor_specific[16]; // this indicates LC3/LC3Q. 0 => LC3 1.0 , 1 => LC3 1.1
697 uint32_t mode;
698} __attribute__ ((packed));
699
700struct lc3_dec_cfg_ext_t
701{
702 struct lc3_config_ext_t fromAirConfig;
703 uint32_t decoder_output_channel;
704 uint32_t stream_map_size;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +0530705 struct lc3_stream_map_ext_t streamMapIn[2];
Manisha Agarwald45632b2019-10-17 18:14:28 +0800706} __attribute__ ((packed));
707
708struct lc3_enc_cfg_ext_t
709{
710 struct lc3_config_ext_t toAirConfig;
711 uint32_t stream_map_size;
712 struct lc3_stream_map_ext_t streamMapOut[16];
713} __attribute__ ((packed));
714
715struct lc3_dec_codec_cfg_t
716{
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +0530717 struct lc3_dec_cfg_ext_t from_Air_cfg;
Manisha Agarwald45632b2019-10-17 18:14:28 +0800718 struct lc3_stream_map_info_t streamMapToAir;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +0530719 struct lc3_stream_map_info_t streamMapFromAir;
Manisha Agarwald45632b2019-10-17 18:14:28 +0800720} __attribute__ ((packed));
721
722struct lc3_dec_cfg_t
723{
724 struct abr_dec_cfg_t abr_cfg;
725 struct lc3_dec_codec_cfg_t dec_codec;
726} __attribute__ ((packed));
727
728struct lc3_enc_codec_cfg_t
729{
730 struct lc3_enc_cfg_ext_t to_Air_cfg;
731 struct lc3_stream_map_info_t streamMapToAir;
732} __attribute__ ((packed));
733
734struct lc3_enc_cfg_t
735{
736 uint32_t enc_format;
737 struct imc_dec_enc_info imc_info;
738 struct lc3_enc_codec_cfg_t enc_codec;
739} __attribute__ ((packed));
740
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530741/* In LE BT source code uses system/audio.h for below
742 * structure definition. To avoid multiple definition
743 * compilation error for audiohal in LE , masking structure
744 * definition under "LINUX_ENABLED" which is defined only
745 * in LE
746 */
747#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530748/* TODO: Define the following structures only for O using PLATFORM_VERSION */
749/* Information about BT SBC encoder configuration
750 * This data is used between audio HAL module and
751 * BT IPC library to configure DSP encoder
752 */
753typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530754 uint32_t subband; /* 4, 8 */
755 uint32_t blk_len; /* 4, 8, 12, 16 */
756 uint16_t sampling_rate; /*44.1khz,48khz*/
757 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
758 uint8_t alloc; /*0(Loudness),1(SNR)*/
759 uint8_t min_bitpool; /* 2 */
760 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
761 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530762 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530763} audio_sbc_encoder_config;
764
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530765/* Information about BT APTX encoder configuration
766 * This data is used between audio HAL module and
767 * BT IPC library to configure DSP encoder
768 */
769typedef struct {
770 uint16_t sampling_rate;
771 uint8_t channels;
772 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530773 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700774} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530775
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700776typedef struct {
Zhou Song8fccbb62019-03-20 01:08:19 +0800777 uint32_t sampling_rate;
778 uint32_t mtu;
779 int32_t channel_mode;
780 uint32_t min_sink_modeA;
781 uint32_t max_sink_modeA;
782 uint32_t min_sink_modeB;
783 uint32_t max_sink_modeB;
784 uint32_t min_sink_modeC;
785 uint32_t max_sink_modeC;
786 uint32_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530787 uint8_t TTP_modeA_low;
788 uint8_t TTP_modeA_high;
789 uint8_t TTP_modeB_low;
790 uint8_t TTP_modeB_high;
Zhou Song8fccbb62019-03-20 01:08:19 +0800791 uint8_t TTP_TWS_low;
792 uint8_t TTP_TWS_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530793 uint32_t bits_per_sample;
Zhou Song8fccbb62019-03-20 01:08:19 +0800794 uint32_t input_mode;
795 uint32_t fade_duration;
796 uint8_t sink_cap[11];
Sharad Sangle95d451b2018-06-19 12:24:20 +0530797} audio_aptx_ad_config;
798
799typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700800 uint16_t sampling_rate;
801 uint8_t channels;
802 uint32_t bitrate;
803 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530804 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700805} audio_aptx_dual_mono_config;
806
807typedef union {
808 audio_aptx_default_config *default_cfg;
809 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530810 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700811} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530812
813/* Information about BT AAC encoder configuration
814 * This data is used between audio HAL module and
815 * BT IPC library to configure DSP encoder
816 */
817typedef struct {
818 uint32_t enc_mode; /* LC, SBR, PS */
819 uint16_t format_flag; /* RAW, ADTS */
820 uint16_t channels; /* 1-Mono, 2-Stereo */
821 uint32_t sampling_rate;
822 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530823 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530824} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530825#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700826
827typedef struct {
828 audio_aac_encoder_config audio_aac_enc_cfg;
829 struct aac_frame_size_control_t frame_ctl;
830} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530831
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530832typedef struct {
833 audio_aac_encoder_config audio_aac_enc_cfg;
834 struct aac_frame_size_control_t frame_ctl;
835 uint8_t size_control_struct;
836 struct aac_frame_size_control_t* frame_ptr_ctl;
837} audio_aac_encoder_config_v3;
838
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530839/* Information about BT CELT encoder configuration
840 * This data is used between audio HAL module and
841 * BT IPC library to configure DSP encoder
842 */
843typedef struct {
844 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
845 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
846 uint16_t frame_size; /* 64-128-256-512, 512 */
847 uint16_t complexity; /* 0-10, 1 */
848 uint16_t prediction_mode; /* 0-1-2, 0 */
849 uint16_t vbr_flag; /* 0-1, 0*/
850 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530851 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530852} audio_celt_encoder_config;
853
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530854/* Information about BT LDAC encoder configuration
855 * This data is used between audio HAL module and
856 * BT IPC library to configure DSP encoder
857 */
858typedef struct {
859 uint32_t sampling_rate; /*44100,48000,88200,96000*/
860 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
861 uint16_t channel_mode; /* 0, 4, 2, 1*/
862 uint16_t mtu; /*679*/
Aniket Kumar Latad0196282019-05-09 14:24:17 -0700863 uint32_t bits_per_sample;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800864 bool is_abr_enabled;
865 struct quality_level_to_bitrate_info level_to_bitrate_map;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530866} audio_ldac_encoder_config;
867
Surendar Karkabbb3c822018-11-12 13:00:38 +0530868/* Information about BT AAC decoder configuration
869 * This data is used between audio HAL module and
870 * BT IPC library to configure DSP decoder
871 */
872typedef struct {
873 uint16_t aac_fmt_flag; /* LATM*/
874 uint16_t audio_object_type; /* LC */
875 uint16_t channels; /* Stereo */
876 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
877 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
878 44.1k, 48k, 64k, 88.2k, 96k */
879} audio_aac_dec_config_t;
880
881/* Information about BT SBC decoder configuration
882 * This data is used between audio HAL module and
883 * BT IPC library to configure DSP decoder
884 */
885typedef struct {
886 uint16_t channels; /* Mono, Stereo */
887 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
888 44.1k, 48k, 64k, 88.2k, 96k */
889}audio_sbc_dec_config_t;
890
Manisha Agarwald45632b2019-10-17 18:14:28 +0800891/* Information about BT LC3 encoder configuration
892 * This data is used between audio HAL module and
893 * BT IPC library to configure DSP encoder
894 */
895typedef struct {
896 uint32_t api_version;
897 uint32_t sampling_freq;
898 uint32_t max_octets_per_frame;
899 uint32_t frame_duration;//7.5msec, 10msec
900 uint32_t bit_depth;
901 uint32_t num_blocks;
902 uint8_t default_q_level;
903 uint8_t vendor_specific[16]; // this indicates LC3/LC3Q. 0 => LC3 1.0 , 1 => LC3 1.1
904 uint32_t mode;
905} lc3_config_t;
906
907typedef struct {
908 uint32_t audio_location;
909 uint8_t stream_id;
910 uint8_t direction;
911} lc3_stream_map_t;
912
913typedef struct {
914 lc3_config_t toAirConfig;
915 uint8_t stream_map_size;
916 lc3_stream_map_t* streamMapOut;
917} lc3_encoder_config_t;
918
919typedef struct {
920 lc3_config_t fromAirConfig;
921 uint32_t decoder_output_channel;
922 uint8_t stream_map_size;
923 lc3_stream_map_t* streamMapIn;
924} lc3_decoder_config_t;
925
926typedef struct {
927 lc3_encoder_config_t enc_config;
928 lc3_decoder_config_t dec_config;
929} audio_lc3_codec_config_t;
930
931
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530932/*********** END of DSP configurable structures ********************/
933
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530934static void update_offload_codec_capabilities()
935{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530936
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530937 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800938 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
939 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
940
941 ALOGD("%s: A2DP offload supported = %d",__func__,
942 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530943}
944
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800945static int stop_abr()
946{
947 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800948 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800949 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800950 int ret = 0;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800951
952 /* This function can be used if !abr_started for clean up */
953 ALOGV("%s: enter", __func__);
954
955 // Close hostless front end
956 if (a2dp.abr_config.abr_tx_handle != NULL) {
957 pcm_close(a2dp.abr_config.abr_tx_handle);
958 a2dp.abr_config.abr_tx_handle = NULL;
959 }
Zhou Song12c29502019-03-16 10:37:18 +0800960 if (a2dp.abr_config.abr_rx_handle != NULL) {
961 pcm_close(a2dp.abr_config.abr_rx_handle);
962 a2dp.abr_config.abr_rx_handle = NULL;
963 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800964 a2dp.abr_config.abr_started = false;
965 a2dp.abr_config.imc_instance = 0;
966
967 // Reset BT driver mixer control for ABR usecase
968 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
969 MIXER_SET_FEEDBACK_CHANNEL);
970 if (!ctl_set_bt_feedback_channel) {
971 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800972 ret = -ENOSYS;
973 } else if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800974 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800975 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800976 }
977
978 // Reset ABR Tx feedback path
979 ALOGV("%s: Disable ABR Tx feedback path", __func__);
980 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
981 MIXER_ABR_TX_FEEDBACK_PATH);
982 if (!ctl_abr_tx_path) {
983 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800984 ret = -ENOSYS;
985 } else if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800986 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800987 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800988 }
989
Zhou Song12c29502019-03-16 10:37:18 +0800990 // Reset ABR Rx feedback path
991 ALOGV("%s: Disable ABR Rx feedback path", __func__);
992 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
993 MIXER_ABR_RX_FEEDBACK_PATH);
994 if (!ctl_abr_rx_path) {
995 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
996 ret = -ENOSYS;
997 } else if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 0) != 0) {
998 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
999 ret = -ENOSYS;
1000 }
1001
1002 return ret;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001003}
1004
1005static int start_abr()
1006{
1007 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001008 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001009 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
1010 int abr_device_id;
1011 int ret = 0;
1012
1013 if (!a2dp.abr_config.is_abr_enabled) {
1014 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
1015 return -ENOSYS;
1016 }
1017
1018 if (a2dp.abr_config.abr_started) {
1019 ALOGI("%s: ABR has already started", __func__);
1020 return ret;
1021 }
1022
1023 // Enable Slimbus 7 Tx feedback path
1024 ALOGV("%s: Enable ABR Tx feedback path", __func__);
1025 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
1026 MIXER_ABR_TX_FEEDBACK_PATH);
1027 if (!ctl_abr_tx_path) {
1028 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
1029 return -ENOSYS;
1030 }
1031 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
1032 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
1033 return -ENOSYS;
1034 }
1035
1036 // Notify ABR usecase information to BT driver to distinguish
1037 // between SCO and feedback usecase
1038 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
1039 MIXER_SET_FEEDBACK_CHANNEL);
1040 if (!ctl_set_bt_feedback_channel) {
1041 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +08001042 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001043 }
1044 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
1045 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +08001046 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001047 }
1048
1049 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001050 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001051 PCM_CAPTURE);
1052 if (!a2dp.abr_config.abr_tx_handle) {
1053 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
1054 abr_device_id, PCM_IN,
1055 &pcm_config_abr);
Zhou Song12c29502019-03-16 10:37:18 +08001056 if (a2dp.abr_config.abr_tx_handle == NULL) {
1057 ALOGE("%s: Can't open abr tx device", __func__);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001058 goto fail;
Zhou Song12c29502019-03-16 10:37:18 +08001059 }
1060 if (!(pcm_is_ready(a2dp.abr_config.abr_tx_handle) &&
1061 !pcm_start(a2dp.abr_config.abr_tx_handle))) {
1062 ALOGE("%s: tx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
1063 goto fail;
1064 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001065 }
Zhou Song12c29502019-03-16 10:37:18 +08001066
1067 // Enable Slimbus 7 Rx feedback path for HD Voice use case
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301068 if ((a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH)
1069 || ((a2dp.a2dp_sink_started == true) && (a2dp.bt_decoder_format == CODEC_TYPE_LC3))) {
Zhou Song12c29502019-03-16 10:37:18 +08001070 ALOGV("%s: Enable ABR Rx feedback path", __func__);
1071 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
1072 MIXER_ABR_RX_FEEDBACK_PATH);
1073 if (!ctl_abr_rx_path) {
1074 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
1075 goto fail;
1076 }
1077 if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 1) != 0) {
1078 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
1079 goto fail;
1080 }
1081
1082 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
1083 ALOGE("%s: Failed to set BT usecase", __func__);
1084 goto fail;
1085 }
1086
1087 // Open hostless front end and prepare ABR Rx path
1088 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
1089 PCM_PLAYBACK);
1090 if (!a2dp.abr_config.abr_rx_handle) {
1091 a2dp.abr_config.abr_rx_handle = pcm_open(a2dp.adev->snd_card,
1092 abr_device_id, PCM_OUT,
1093 &pcm_config_abr);
1094 if (a2dp.abr_config.abr_rx_handle == NULL) {
1095 ALOGE("%s: Can't open abr rx device", __func__);
1096 goto fail;
1097 }
1098 if (!(pcm_is_ready(a2dp.abr_config.abr_rx_handle) &&
1099 !pcm_start(a2dp.abr_config.abr_rx_handle))) {
1100 ALOGE("%s: rx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_rx_handle));
1101 goto fail;
1102 }
1103 }
1104 }
1105
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001106 a2dp.abr_config.abr_started = true;
1107
1108 return ret;
1109
1110fail:
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001111 stop_abr();
1112 return -ENOSYS;
1113}
1114
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001115static int check_if_enhanced_fwk() {
1116
1117 int is_enhanced_fwk = 1;
1118 //dlopen lib
1119 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
1120 if (vndk_fwk_lib_handle != NULL) {
1121 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
1122 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
1123 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
1124 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
1125 __func__);
1126 is_enhanced_fwk = 1;
1127 } else {
1128 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
1129 }
1130 }
1131 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
1132 return is_enhanced_fwk;
1133}
1134
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301135static void open_a2dp_source() {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301136 int ret = 0;
1137
Florian Pfister1a84f312018-07-19 14:38:18 +02001138 ALOGD(" Open A2DP source start ");
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301139
1140 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
1141 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
1142 ALOGD("calling BT stream open");
1143 ret = a2dp.audio_source_open();
1144 if(ret != 0) {
1145 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Srinu Jellada99a592019-01-25 16:50:52 +05301146 }
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301147 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301148 } else {
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301149 ALOGD("Called a2dp open with improper state %d", a2dp.bt_state_source);
1150 }
1151 } else {
1152 ALOGE("a2dp handle is not identified, Ignoring open request");
1153 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
1154 }
1155}
1156/* API to open BT IPC library to start IPC communication for BT Source*/
1157static void a2dp_source_init()
1158{
1159 ALOGD("a2dp_source_init START");
1160 if (a2dp.bt_lib_source_handle == NULL) {
1161 ALOGD("Requesting for BT lib handle");
1162 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
1163 if (a2dp.bt_lib_source_handle == NULL) {
1164 ALOGE("%s: dlopen failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
1165 return;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301166 }
1167 }
1168
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301169 a2dp.bt_audio_pre_init = (bt_audio_pre_init_t)
1170 dlsym(a2dp.bt_lib_source_handle, "bt_audio_pre_init");
Srinu Jellada99a592019-01-25 16:50:52 +05301171 a2dp.audio_source_open = (audio_source_open_t)
1172 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
1173 a2dp.audio_source_start = (audio_source_start_t)
1174 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
sriram kumarcd549b02021-02-12 14:27:17 +05301175 if (a2dp.audio_source_start == NULL) {
1176 a2dp.audio_source_start = (audio_source_start_t)
1177 dlsym(a2dp.bt_lib_source_handle, "audio_stream_start");
1178 }
Srinu Jellada99a592019-01-25 16:50:52 +05301179 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
1180 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
1181 a2dp.audio_source_suspend = (audio_source_suspend_t)
1182 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
sriram kumarcd549b02021-02-12 14:27:17 +05301183 if (a2dp.audio_source_suspend == NULL) {
1184 a2dp.audio_source_suspend = (audio_source_suspend_t)
1185 dlsym(a2dp.bt_lib_source_handle, "audio_stream_suspend");
1186 }
Srinu Jellada99a592019-01-25 16:50:52 +05301187 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
1188 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
1189 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1190 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
sriram kumarcd549b02021-02-12 14:27:17 +05301191 if (a2dp.clear_source_a2dpsuspend_flag == NULL) {
1192 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1193 dlsym(a2dp.bt_lib_source_handle, "clear_a2dp_suspend_flag");
1194 }
Srinu Jellada99a592019-01-25 16:50:52 +05301195 a2dp.audio_source_stop = (audio_source_stop_t)
1196 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
sriram kumarcd549b02021-02-12 14:27:17 +05301197 if (a2dp.audio_source_stop == NULL) {
1198 a2dp.audio_source_stop = (audio_source_stop_t)
1199 dlsym(a2dp.bt_lib_source_handle, "audio_stream_stop");
1200 }
Srinu Jellada99a592019-01-25 16:50:52 +05301201 a2dp.audio_source_close = (audio_source_close_t)
1202 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
1203 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
1204 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
1205 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
1206 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
1207 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1208 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1209 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1210 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1211
Srinu Jella793f5302019-07-19 15:53:55 +05301212 if (a2dp.bt_lib_source_handle && a2dp.bt_audio_pre_init) {
1213 ALOGD("calling BT module preinit");
1214 // fwk related check's will be done in the BT layer
1215 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001216 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001217}
1218
1219/* API to open BT IPC library to start IPC communication for BT Sink*/
1220static void open_a2dp_sink()
1221{
1222 ALOGD(" Open A2DP input start ");
1223 if (a2dp.bt_lib_sink_handle == NULL){
1224 ALOGD(" Requesting for BT lib handle");
1225 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1226
1227 if (a2dp.bt_lib_sink_handle == NULL) {
1228 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1229 } else {
1230 a2dp.audio_sink_start = (audio_sink_start_t)
1231 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1232 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1233 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1234 a2dp.audio_sink_stop = (audio_sink_stop_t)
1235 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1236 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1237 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1238 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1239 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1240 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301241 }
1242}
1243
1244static int close_a2dp_output()
1245{
1246 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001247
1248 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1249 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301250 return -ENOSYS;
1251 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001252
1253 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1254 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001255 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001256 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301257 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001258 a2dp.a2dp_source_started = false;
1259 a2dp.a2dp_source_total_active_session_requests = 0;
1260 a2dp.a2dp_source_suspended = false;
1261 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301262 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301263 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001264 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001265 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1266 stop_abr();
1267 a2dp.abr_config.is_abr_enabled = false;
1268 a2dp.abr_config.abr_started = false;
1269 a2dp.abr_config.imc_instance = 0;
1270 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001271 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301272 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001273
1274 return 0;
1275}
1276
1277static int close_a2dp_input()
1278{
1279 ALOGV("%s\n",__func__);
1280
1281 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1282 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1283 return -ENOSYS;
1284 }
1285
1286 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1287 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001288 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001289 ALOGE("failed close a2dp sink control path from BT library");
1290 }
1291 a2dp.a2dp_sink_started = false;
1292 a2dp.a2dp_sink_total_active_session_requests = 0;
1293 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1294 a2dp.dec_sampling_rate = 48000;
1295 a2dp.dec_channels = 2;
1296 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301297
1298 return 0;
1299}
1300
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301301static void a2dp_check_and_set_scrambler()
1302{
1303 bool scrambler_mode = false;
1304 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001305 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1306 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301307
1308 if (scrambler_mode) {
1309 //enable scrambler in dsp
1310 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1311 MIXER_SCRAMBLER_MODE);
1312 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001313 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301314 return;
1315 } else {
1316 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1317 ALOGE("%s: Could not set scrambler mode", __func__);
1318 return;
1319 }
1320 }
1321 }
1322}
1323
Florian Pfister1a84f312018-07-19 14:38:18 +02001324static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301325{
Florian Pfister1a84f312018-07-19 14:38:18 +02001326 char *rate_str = NULL, *channels = NULL;
1327 uint32_t sampling_rate;
1328 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1329 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301330
Florian Pfister1a84f312018-07-19 14:38:18 +02001331 if (direction == SINK) {
1332 sampling_rate = a2dp.dec_sampling_rate;
1333 } else {
1334 sampling_rate = a2dp.enc_sampling_rate;
1335 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001336 /*
1337 * For LDAC encoder and AAC decoder open slimbus port at
1338 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1339 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1340 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001341 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301342 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001343 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1344 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301345 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1346 sampling_rate = sampling_rate *2;
1347 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001348
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301349 if (a2dp.bt_encoder_format == CODEC_TYPE_LC3 ||
1350 a2dp.bt_decoder_format == CODEC_TYPE_LC3)
Manisha Agarwald45632b2019-10-17 18:14:28 +08001351 sampling_rate = SAMPLING_RATE_96K;
1352
Aalique Grahame22e49102018-12-18 14:23:57 -08001353 // No need to configure backend for PCM format.
1354 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1355 return 0;
1356 }
1357
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301358 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301359 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301360 case 44100:
1361 rate_str = "KHZ_44P1";
1362 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301363 case 88200:
1364 rate_str = "KHZ_88P2";
1365 break;
1366 case 96000:
1367 rate_str = "KHZ_96";
1368 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001369 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301370 default:
1371 rate_str = "KHZ_48";
1372 break;
1373 }
1374
Florian Pfister1a84f312018-07-19 14:38:18 +02001375 if (direction == SINK) {
1376 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301377 if (a2dp.bt_decoder_format == CODEC_TYPE_LC3)
1378 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1379 MIXER_SOURCE_SAMPLE_RATE_TX);
1380 else
1381 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1382 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001383 } else {
1384 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1385 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301386 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001387 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301388 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001389
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301390 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1391 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301392 is_configured = false;
1393 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301394 }
1395
Surendar Karka2febd452018-12-13 17:56:43 +05301396 if (direction == SOURCE) {
1397 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001398 if (a2dp.abr_config.is_abr_enabled) {
Manisha Agarwald45632b2019-10-17 18:14:28 +08001399 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH ||
1400 a2dp.bt_encoder_format == CODEC_TYPE_LC3)
Zhou Song12c29502019-03-16 10:37:18 +08001401 rate_str = SPEECH_TX_SAMPLE_RATE;
1402 else
1403 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301404
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301405 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1406 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1407 MIXER_SOURCE_SAMPLE_RATE_TX);
1408 if (!ctl_sample_rate) {
1409 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1410 is_configured = false;
1411 goto fail;
1412 }
1413
1414 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1415 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1416 is_configured = false;
1417 goto fail;
1418 }
Surendar Karka2febd452018-12-13 17:56:43 +05301419 }
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301420 } else {
1421 /* LC3 needs to set RX sample rate as well */
1422 if (a2dp.bt_decoder_format == CODEC_TYPE_LC3) {
1423 ALOGD("%s: set rx backend sample rate =%s", __func__, rate_str);
1424 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1425 MIXER_SAMPLE_RATE_RX);
1426 if (!ctl_sample_rate) {
1427 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1428 is_configured = false;
1429 goto fail;
1430 }
1431
1432 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1433 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1434 is_configured = false;
1435 goto fail;
1436 }
1437 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301438 }
1439 } else {
1440 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1441 MIXER_SAMPLE_RATE_TX is not supported */
1442 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1443 MIXER_SAMPLE_RATE_DEFAULT);
1444 if (!ctl_sample_rate) {
1445 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301446 is_configured = false;
1447 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301448 }
1449
1450 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1451 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001452 is_configured = false;
1453 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301454 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301455 }
1456
Florian Pfister1a84f312018-07-19 14:38:18 +02001457 if (direction == SINK) {
1458 switch (a2dp.dec_channels) {
1459 case 1:
1460 channels = "One";
1461 break;
1462 case 2:
1463 default:
1464 channels = "Two";
1465 break;
1466 }
1467
Ramu Gottipati02809682018-12-19 16:46:12 +05301468 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301469 if (a2dp.bt_decoder_format == CODEC_TYPE_LC3)
1470 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1471 MIXER_AFE_SINK_CHANNELS_SLIM7);
1472 else
1473 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1474 MIXER_AFE_SINK_CHANNELS);
Florian Pfister1a84f312018-07-19 14:38:18 +02001475 } else {
1476 //Configure AFE enc channels
1477 switch (a2dp.enc_channels) {
1478 case 1:
1479 channels = "One";
1480 break;
1481 case 2:
1482 default:
1483 channels = "Two";
1484 break;
1485 }
1486
Ramu Gottipati02809682018-12-19 16:46:12 +05301487 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001488 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1489 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301490 }
1491
Florian Pfister1a84f312018-07-19 14:38:18 +02001492 if (!ctrl_channels) {
1493 ALOGE(" ERROR AFE channels mixer control not identified");
1494 } else {
1495 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301496 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001497 is_configured = false;
1498 goto fail;
1499 }
1500 }
1501 is_configured = true;
1502fail:
1503 return is_configured;
1504}
1505
Aniket Kumar Lata990de552019-07-11 14:20:23 -07001506bool a2dp_set_source_backend_cfg()
1507{
1508 if (a2dp.a2dp_source_started && !a2dp.a2dp_source_suspended)
1509 return a2dp_set_backend_cfg(SOURCE);
1510
1511 return false;
1512}
1513
Surendar Karkabbb3c822018-11-12 13:00:38 +05301514bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001515{
1516 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1517 struct aac_dec_cfg_t aac_dsp_cfg;
1518 bool is_configured = false;
1519 int ret = 0;
1520
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001521 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001522 return false;
1523
Surendar Karka2febd452018-12-13 17:56:43 +05301524 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001525 if (!ctl_dec_data) {
1526 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1527 is_configured = false;
1528 goto fail;
1529 }
1530
1531 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1532 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1533 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1534 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1535 switch(aac_bt_cfg->audio_object_type) {
1536 case 0:
1537 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301538 break;
1539 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001540 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1541 break;
1542 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301543 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001544 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301545 break;
1546 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001547 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1548 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1549 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1550 sizeof(struct aac_dec_cfg_t));
1551 if (ret != 0) {
1552 ALOGE("%s: failed to set AAC decoder config", __func__);
1553 is_configured = false;
1554 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001555 }
1556
Florian Pfister1a84f312018-07-19 14:38:18 +02001557 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1558 MIXER_DEC_BIT_FORMAT);
1559 if (!ctrl_bit_format) {
1560 ALOGE(" ERROR Dec bit format mixer control not identified");
1561 is_configured = false;
1562 goto fail;
1563 }
1564 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1565 if (ret != 0) {
1566 ALOGE("%s: Failed to set bit format to decoder", __func__);
1567 is_configured = false;
1568 goto fail;
1569 }
1570
1571 is_configured = true;
1572 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1573 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1574 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1575 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1576 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1577fail:
1578 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301579}
1580
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301581static int a2dp_set_bit_format(uint32_t enc_bit_format)
1582{
1583 const char *bit_format = NULL;
1584 struct mixer_ctl *ctrl_bit_format = NULL;
1585
1586 // Configure AFE Input Bit Format
1587 switch (enc_bit_format) {
1588 case 32:
1589 bit_format = "S32_LE";
1590 break;
1591 case 24:
1592 bit_format = "S24_LE";
1593 break;
1594 case 16:
1595 default:
1596 bit_format = "S16_LE";
1597 break;
1598 }
1599
1600 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1601 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1602 MIXER_ENC_BIT_FORMAT);
1603 if (!ctrl_bit_format) {
1604 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1605 return -ENOSYS;
1606 }
1607 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1608 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1609 return -ENOSYS;
1610 }
1611 return 0;
1612}
1613
Surendar Karka2febd452018-12-13 17:56:43 +05301614static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301615{
Surendar Karka2febd452018-12-13 17:56:43 +05301616 const char *rate_str = "KHZ_8", *channels = "Zero";
1617 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1618 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301619
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001620 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301621 if (direction == SINK) {
1622 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1623 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1624 MIXER_SINK_SAMPLE_RATE);
1625 } else {
1626 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1627 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1628 MIXER_SAMPLE_RATE_RX);
1629 }
1630 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301631
Surendar Karka2febd452018-12-13 17:56:43 +05301632 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1633 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301634 return -ENOSYS;
1635 }
Zhou Song12c29502019-03-16 10:37:18 +08001636 if (a2dp.abr_config.is_abr_enabled) {
1637 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1638 MIXER_SOURCE_SAMPLE_RATE_TX);
1639 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301640 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1641 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001642 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301643
Zhou Song12c29502019-03-16 10:37:18 +08001644 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1645 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1646 return -ENOSYS;
1647 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301648 }
1649 } else {
1650
Surendar Karka2febd452018-12-13 17:56:43 +05301651 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301652 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301653 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301654 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1655 return -ENOSYS;
1656 }
1657
Surendar Karka2febd452018-12-13 17:56:43 +05301658 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301659 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1660 return -ENOSYS;
1661 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001662 }
1663
1664 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301665 if (direction == SINK) {
1666 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1667 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1668 MIXER_AFE_SINK_CHANNELS);
1669 } else {
1670 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1671 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1672 MIXER_AFE_IN_CHANNELS);
1673 }
1674 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001675 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1676 return -ENOSYS;
1677 }
Surendar Karka2febd452018-12-13 17:56:43 +05301678 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001679 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1680 return -ENOSYS;
1681 }
1682
1683 return 0;
1684}
1685
1686/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301687static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001688{
1689 struct mixer_ctl *ctl_dec_data = NULL;
1690 struct abr_dec_cfg_t dec_cfg;
1691 int ret = 0;
1692
1693 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301694 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001695 if (!ctl_dec_data) {
1696 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1697 return false;
1698 }
1699 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1700 dec_cfg.dec_format = dec_format;
1701 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1702 dec_cfg.imc_info.enable = IMC_ENABLE;
1703 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1704 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1705
1706 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1707 sizeof(dec_cfg));
1708 if (ret != 0) {
1709 ALOGE("%s: Failed to set decoder config", __func__);
1710 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301711 }
Surendar Karka2febd452018-12-13 17:56:43 +05301712 }
1713
1714 return true;
1715}
1716
Surendar Karkabbb3c822018-11-12 13:00:38 +05301717bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001718{
1719 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1720 struct sbc_dec_cfg_t sbc_dsp_cfg;
1721 bool is_configured = false;
1722 int ret = 0;
1723
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001724 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001725 goto fail;
1726
Surendar Karka2febd452018-12-13 17:56:43 +05301727 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001728 if (!ctl_dec_data) {
1729 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1730 is_configured = false;
1731 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301732 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001733
Florian Pfister1a84f312018-07-19 14:38:18 +02001734 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1735 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1736 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1737 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1738 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1739 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301740
Florian Pfister1a84f312018-07-19 14:38:18 +02001741 if (ret != 0) {
1742 ALOGE("%s: failed to set SBC decoder config", __func__);
1743 is_configured = false;
1744 goto fail;
1745 }
1746
1747 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1748 MIXER_DEC_BIT_FORMAT);
1749 if (!ctrl_bit_format) {
1750 ALOGE(" ERROR Dec bit format mixer control not identified");
1751 is_configured = false;
1752 goto fail;
1753 }
1754 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1755 if (ret != 0) {
1756 ALOGE("%s: Failed to set bit format to decoder", __func__);
1757 is_configured = false;
1758 goto fail;
1759 }
1760
1761 is_configured = true;
1762 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1763 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1764 a2dp.dec_channels = 1;
1765 else
1766 a2dp.dec_channels = 2;
1767 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1768 ALOGV("Successfully updated SBC dec format");
1769fail:
1770 return is_configured;
1771}
1772
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301773uint64_t convert_channel_map(uint32_t audio_location)
1774{
1775 int i;
1776 uint64_t channel_mask = (uint64_t) 0x00000000;
1777
1778 if (!audio_location) {
1779 channel_mask |= 1ULL << PCM_CHANNEL_C;
1780 return channel_mask;
1781 }
1782
1783 for (i = 0; i < AUDIO_LOCATION_MAX; i++) {
1784 if (audio_location & audio_location_map_array[i])
1785 channel_mask |= 1ULL << channel_map_array[i];
1786 }
1787
1788 return channel_mask;
1789}
1790
1791bool configure_lc3_enc_format(audio_lc3_codec_config_t *lc3_bt_cfg) {
1792 struct mixer_ctl *ctl_enc_data = NULL;
1793 int mixer_size = 0;
1794 int ret = 0;
1795 int i;
1796 bool is_configured = false;
1797 struct lc3_enc_cfg_t lc3_dsp_cfg;
1798 uint64_t channel_mask;
1799
1800 if (lc3_bt_cfg == NULL)
1801 return false;
1802
1803 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1804 if (!ctl_enc_data) {
1805 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
1806 return false;
1807 }
1808
1809 /* Initialize dsp configuration params */
1810 memset(&lc3_dsp_cfg, 0x0, sizeof(struct lc3_enc_cfg_t));
1811
1812 lc3_dsp_cfg.enc_format = MEDIA_FMT_LC3;
1813 //encoder structure
1814
1815 if (lc3_bt_cfg->enc_config.stream_map_size != 0) {
1816 if (!lc3_bt_cfg->enc_config.streamMapOut[0].audio_location)
1817 a2dp.enc_channels = CH_MONO;
1818 else
1819 a2dp.enc_channels = CH_STEREO;
1820 }
1821
1822 lc3_dsp_cfg.imc_info.direction = IMC_RECEIVE;
1823 lc3_dsp_cfg.imc_info.enable = IMC_ENABLE;
1824 lc3_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1825 lc3_dsp_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1826 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.api_version = lc3_bt_cfg->enc_config.toAirConfig.api_version;
1827 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.sampling_freq = lc3_bt_cfg->enc_config.toAirConfig.sampling_freq;
1828 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.max_octets_per_frame = lc3_bt_cfg->enc_config.toAirConfig.max_octets_per_frame;
1829 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.frame_duration = lc3_bt_cfg->enc_config.toAirConfig.frame_duration;
1830 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.bit_depth = lc3_bt_cfg->enc_config.toAirConfig.bit_depth;
1831 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.num_blocks = lc3_bt_cfg->enc_config.toAirConfig.num_blocks;
1832 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.default_q_level = lc3_bt_cfg->enc_config.toAirConfig.default_q_level;
1833 for (i = 0; i < 16; i++)
1834 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.vendor_specific[i] = lc3_bt_cfg->enc_config.toAirConfig.vendor_specific[i];
1835 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.mode = lc3_bt_cfg->enc_config.toAirConfig.mode;
1836 lc3_dsp_cfg.enc_codec.to_Air_cfg.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
1837 lc3_dsp_cfg.enc_codec.streamMapToAir.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
1838
1839 for (i = 0; i < lc3_dsp_cfg.enc_codec.to_Air_cfg.stream_map_size; i++) {
1840 // for encoder stream map info
1841 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].audio_location = lc3_bt_cfg->enc_config.streamMapOut[i].audio_location;
1842 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
1843 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
1844 // for packetizer stream map info
1845 channel_mask = convert_channel_map(lc3_bt_cfg->enc_config.streamMapOut[i].audio_location);
1846 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
1847 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
1848 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].channel_mask_lsw = channel_mask & 0x00000000FFFFFFFF;
1849 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].channel_mask_msw = (channel_mask & 0xFFFFFFFF00000000) >> 32;
1850 }
1851
1852 /* Configure AFE DSP configuration */
1853 mixer_size = sizeof(struct lc3_enc_cfg_t);
1854 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&lc3_dsp_cfg,
1855 mixer_size);
1856 if (ret != 0) {
1857 ALOGE("%s: Failed to set lc3 encoder config", __func__);
1858 is_configured = false;
1859 goto fail;
1860 }
1861
1862 ret = a2dp_set_bit_format(ENCODER_BIT_FORMAT_PCM_24);
1863 if (ret != 0) {
1864 ALOGE("%s: Failed to set lc3 bit format", __func__);
1865 is_configured = false;
1866 goto fail;
1867 }
1868
1869 is_configured = true;
1870 a2dp.bt_encoder_format = CODEC_TYPE_LC3;
1871
1872fail:
1873 return is_configured;
1874}
1875
1876bool configure_lc3_dec_format(audio_lc3_codec_config_t *lc3_bt_cfg)
1877{
1878 struct mixer_ctl *ctl_dec_data = NULL;
1879 struct lc3_dec_cfg_t lc3_dsp_cfg;
1880 uint64_t channel_mask;
1881 bool is_configured = false;
1882 int ret = 0;
1883 int i;
1884
1885 if (lc3_bt_cfg == NULL)
1886 return false;
1887
1888 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
1889 if (!ctl_dec_data) {
1890 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1891 return false;
1892 }
1893
1894 memset(&lc3_dsp_cfg, 0x0, sizeof(struct lc3_dec_cfg_t));
1895
1896 lc3_dsp_cfg.abr_cfg.dec_format = MEDIA_FMT_LC3;
1897 lc3_dsp_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
1898 lc3_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
1899 lc3_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1900
1901 lc3_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1902
1903 /* To check whether decoder configuration is present or not*/
1904 if (lc3_bt_cfg->dec_config.stream_map_size != 0) {
1905 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.api_version = lc3_bt_cfg->dec_config.fromAirConfig.api_version;
1906 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.sampling_freq = lc3_bt_cfg->dec_config.fromAirConfig.sampling_freq;
1907 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.max_octets_per_frame = lc3_bt_cfg->dec_config.fromAirConfig.max_octets_per_frame;
1908 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.frame_duration = lc3_bt_cfg->dec_config.fromAirConfig.frame_duration;
1909 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.bit_depth = lc3_bt_cfg->dec_config.fromAirConfig.bit_depth;
1910 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.num_blocks = lc3_bt_cfg->dec_config.fromAirConfig.num_blocks;
1911 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.default_q_level = lc3_bt_cfg->dec_config.fromAirConfig.default_q_level;
1912 for (i = 0; i < 16; i++)
1913 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.vendor_specific[i] = lc3_bt_cfg->dec_config.fromAirConfig.vendor_specific[i];
1914 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.mode = lc3_bt_cfg->dec_config.fromAirConfig.mode;
1915 lc3_dsp_cfg.dec_codec.from_Air_cfg.decoder_output_channel = lc3_bt_cfg->dec_config.decoder_output_channel;
1916 lc3_dsp_cfg.dec_codec.from_Air_cfg.stream_map_size = lc3_bt_cfg->dec_config.stream_map_size;
1917 lc3_dsp_cfg.dec_codec.streamMapFromAir.stream_map_size = lc3_bt_cfg->dec_config.stream_map_size;
1918
1919 //for depacketizer both fromAir and toAir streamMap needs to be sent.
1920 for (i = 0; i < lc3_dsp_cfg.dec_codec.from_Air_cfg.stream_map_size; i++) {
1921 // for decoder stream map info
1922 lc3_dsp_cfg.dec_codec.from_Air_cfg.streamMapIn[i].audio_location = lc3_bt_cfg->dec_config.streamMapIn[i].audio_location;
1923 lc3_dsp_cfg.dec_codec.from_Air_cfg.streamMapIn[i].stream_id = lc3_bt_cfg->dec_config.streamMapIn[i].stream_id;
1924 lc3_dsp_cfg.dec_codec.from_Air_cfg.streamMapIn[i].direction = lc3_bt_cfg->dec_config.streamMapIn[i].direction;
1925 // for depacketizer stream map fromAir info
1926 channel_mask = convert_channel_map(lc3_bt_cfg->dec_config.streamMapIn[i].audio_location);
1927 lc3_dsp_cfg.dec_codec.streamMapFromAir.streamMap[i].stream_id = lc3_bt_cfg->dec_config.streamMapIn[i].stream_id;
1928 lc3_dsp_cfg.dec_codec.streamMapFromAir.streamMap[i].direction = lc3_bt_cfg->dec_config.streamMapIn[i].direction;
1929 lc3_dsp_cfg.dec_codec.streamMapFromAir.streamMap[i].channel_mask_lsw = channel_mask & 0x00000000FFFFFFFF;
1930 lc3_dsp_cfg.dec_codec.streamMapFromAir.streamMap[i].channel_mask_msw = (channel_mask & 0xFFFFFFFF00000000) >> 32;
1931 }
1932 }
1933
1934 lc3_dsp_cfg.dec_codec.streamMapToAir.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
1935
1936 for (i = 0; i < lc3_bt_cfg->enc_config.stream_map_size; i++) {
1937 // for depacketizer stream map toAir info
1938 channel_mask = convert_channel_map(lc3_bt_cfg->enc_config.streamMapOut[i].audio_location);
1939 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
1940 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
1941 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].channel_mask_lsw = channel_mask & 0x00000000FFFFFFFF;
1942 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].channel_mask_msw = (channel_mask & 0xFFFFFFFF00000000) >> 32;
1943 }
1944
1945 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&lc3_dsp_cfg,
1946 sizeof(struct lc3_dec_cfg_t));
1947 if (ret != 0) {
1948 ALOGE("%s: failed to set LC3 decoder config", __func__);
1949 is_configured = false;
1950 goto fail;
1951 }
1952
1953 is_configured = true;
1954 a2dp.bt_decoder_format = CODEC_TYPE_LC3;
1955 a2dp.dec_channels = CH_STEREO;
1956
1957fail:
1958 return is_configured;
1959}
1960
Florian Pfister1a84f312018-07-19 14:38:18 +02001961/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301962static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301963{
Florian Pfister1a84f312018-07-19 14:38:18 +02001964 void *codec_info = NULL;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301965 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02001966 codec_t codec_type = CODEC_TYPE_INVALID;
1967 bool is_configured = false;
1968 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301969
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301970 if (a2dp.audio_get_dec_config) {
1971 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
1972 if (!ctl_dec_data) {
1973 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1974 is_configured = false;
1975 return false;
1976 }
1977 codec_info = a2dp.audio_get_dec_config(&codec_type);
1978 } else if (a2dp.audio_get_enc_config) {
1979 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev, &codec_type);
1980 } else {
Florian Pfister1a84f312018-07-19 14:38:18 +02001981 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1982 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301983 }
1984
Florian Pfister1a84f312018-07-19 14:38:18 +02001985 switch(codec_type) {
1986 case CODEC_TYPE_SBC:
1987 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301988 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001989 break;
1990 case CODEC_TYPE_AAC:
1991 ALOGD(" AAC decoder supported BT device");
1992 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301993 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001994 break;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301995 case CODEC_TYPE_LC3:
1996 ALOGD(" LC3 decoder supported BT device");
1997 is_configured =
1998 (configure_lc3_enc_format((audio_lc3_codec_config_t *)codec_info) &&
1999 configure_lc3_dec_format((audio_lc3_codec_config_t *)codec_info));
2000 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002001 default:
2002 ALOGD(" Received Unsupported decoder format");
2003 is_configured = false;
2004 break;
2005 }
2006 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302007}
2008
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302009/* API to configure SBC DSP encoder */
2010bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
2011{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302012 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302013 struct sbc_enc_cfg_t sbc_dsp_cfg;
2014 bool is_configured = false;
2015 int ret = 0;
2016
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002017 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302018 return false;
2019
2020 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2021 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002022 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302023 is_configured = false;
2024 goto fail;
2025 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302026 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002027 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302028 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
2029 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
2030 switch(sbc_bt_cfg->channels) {
2031 case 0:
2032 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
2033 break;
2034 case 1:
2035 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
2036 break;
2037 case 3:
2038 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
2039 break;
2040 case 2:
2041 default:
2042 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
2043 break;
2044 }
2045 if (sbc_bt_cfg->alloc)
2046 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
2047 else
2048 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
2049 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
2050 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
2051 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
2052 sizeof(struct sbc_enc_cfg_t));
2053 if (ret != 0) {
2054 ALOGE("%s: failed to set SBC encoder config", __func__);
2055 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302056 goto fail;
2057 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302058 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302059 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302060 is_configured = false;
2061 goto fail;
2062 }
2063 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002064 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302065 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302066
2067 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
2068 a2dp.enc_channels = 1;
2069 else
2070 a2dp.enc_channels = 2;
2071
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302072 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
2073 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302074fail:
2075 return is_configured;
2076}
2077
Manish Dewangan6a252632017-12-04 17:27:44 +05302078#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05302079static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
2080 audio_aptx_encoder_config *aptx_bt_cfg)
2081{
2082 int ret = 0;
2083
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002084 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05302085 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
2086 aptx_dsp_cfg, aptx_bt_cfg);
2087 return -EINVAL;
2088 }
2089
2090 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05302091 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302092
2093
2094 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
2095 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
2096 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
2097 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
2098 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
2099 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
2100 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
2101 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
2102 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05302103 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302104 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
2105 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
2106 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2107 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
2108
2109
2110 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
2111 case APTX_AD_CHANNEL_UNCHANGED:
2112 case APTX_AD_CHANNEL_JOINT_STEREO:
2113 case APTX_AD_CHANNEL_DUAL_MONO:
2114 case APTX_AD_CHANNEL_STEREO_TWS:
2115 case APTX_AD_CHANNEL_EARBUD:
2116 default:
2117 a2dp.enc_channels = CH_STEREO;
2118 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
2119 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2120 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2121 break;
2122 case APTX_AD_CHANNEL_MONO:
2123 a2dp.enc_channels = CH_MONO;
2124 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
2125 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08002126 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302127 }
2128 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
2129 case APTX_AD_SR_UNCHANGED:
2130 case APTX_AD_48:
2131 default:
2132 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
2133 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
2134 break;
2135 case APTX_AD_44_1:
2136 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
2137 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08002138 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302139 }
2140 ALOGV("Successfully updated APTX AD enc format with \
2141 samplingrate: %d channels:%d",
2142 aptx_dsp_cfg->custom_cfg.sample_rate,
2143 aptx_dsp_cfg->custom_cfg.num_channels);
2144
2145 return ret;
2146}
Manisha Agarwala51768b2018-11-01 16:30:52 +05302147
Zhou Song8fccbb62019-03-20 01:08:19 +08002148static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
2149 audio_aptx_encoder_config *aptx_bt_cfg)
2150{
2151 int ret = 0;
2152
2153 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Mingshu Pangaa429f72019-06-10 17:25:00 +08002154 ALOGE("Invalid param, aptx_dsp_cfg %pK aptx_bt_cfg %pK",
Zhou Song8fccbb62019-03-20 01:08:19 +08002155 aptx_dsp_cfg, aptx_bt_cfg);
2156 return -EINVAL;
2157 }
2158
2159 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
2160 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
2161
2162
2163 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
2164 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
2165 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
2166 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
2167 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
2168 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
2169 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
2170 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
2171 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
2172 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
2173 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
2174 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
2175 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
2176 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
2177 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
2178 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
2179 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2180 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
2181
2182
2183 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
2184 case APTX_AD_CHANNEL_UNCHANGED:
2185 case APTX_AD_CHANNEL_JOINT_STEREO:
2186 case APTX_AD_CHANNEL_DUAL_MONO:
2187 case APTX_AD_CHANNEL_STEREO_TWS:
2188 case APTX_AD_CHANNEL_EARBUD:
2189 default:
2190 a2dp.enc_channels = CH_STEREO;
2191 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
2192 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2193 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2194 break;
2195 case APTX_AD_CHANNEL_MONO:
2196 a2dp.enc_channels = CH_MONO;
2197 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
2198 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08002199 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08002200 }
2201 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
2202 case APTX_AD_SR_UNCHANGED:
2203 case APTX_AD_48:
2204 default:
2205 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
2206 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
2207 break;
2208 case APTX_AD_44_1:
2209 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
2210 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08002211 break;
2212 case APTX_AD_96:
2213 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
2214 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_96K;
2215 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08002216 }
2217 ALOGV("Successfully updated APTX AD enc format with \
2218 samplingrate: %d channels:%d",
2219 aptx_dsp_cfg->custom_cfg.sample_rate,
2220 aptx_dsp_cfg->custom_cfg.num_channels);
2221
2222 return ret;
2223}
2224
Manisha Agarwala51768b2018-11-01 16:30:52 +05302225static void audio_a2dp_update_tws_channel_mode()
2226{
2227 char* channel_mode;
2228 struct mixer_ctl *ctl_channel_mode;
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05302229
2230 ALOGD("Update tws for mono_mode on=%d",a2dp.is_tws_mono_mode_on);
2231
Manisha Agarwala51768b2018-11-01 16:30:52 +05302232 if (a2dp.is_tws_mono_mode_on)
2233 channel_mode = "One";
2234 else
2235 channel_mode = "Two";
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05302236
Manisha Agarwala51768b2018-11-01 16:30:52 +05302237 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2238 if (!ctl_channel_mode) {
2239 ALOGE("failed to get tws mixer ctl");
2240 return;
2241 }
2242 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2243 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2244 return;
2245 }
2246}
2247
Manisha Agarwald45632b2019-10-17 18:14:28 +08002248static void audio_a2dp_update_lc3_channel_mode()
2249{
2250 char* channel_mode;
2251 struct mixer_ctl *ctl_channel_mode;
2252
2253 ALOGD("Update lc3 for mono_mode on=%d",a2dp.is_lc3_mono_mode_on);
2254
2255 if (a2dp.is_lc3_mono_mode_on)
2256 channel_mode = "One";
2257 else
2258 channel_mode = "Two";
2259
2260 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_LC3_CHANNEL_MODE);
2261 if (!ctl_channel_mode) {
2262 ALOGE("failed to get lc3 mixer ctl");
2263 return;
2264 }
2265 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2266 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2267 return;
2268 }
2269}
2270
Manish Dewangan6a252632017-12-04 17:27:44 +05302271static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
2272 audio_aptx_encoder_config *aptx_bt_cfg)
2273{
2274 int ret = 0;
2275
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002276 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05302277 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
2278 aptx_dsp_cfg, aptx_bt_cfg);
2279 return -EINVAL;
2280 }
2281
2282 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002283 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05302284
2285 if (!a2dp.is_aptx_dual_mono_supported) {
2286 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
2287 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
2288 } else {
2289 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
2290 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
2291 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
2292 }
2293
2294 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
2295 case 1:
2296 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2297 break;
2298 case 2:
2299 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05302300 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2301 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
Manish Dewangan6a252632017-12-04 17:27:44 +05302302 break;
2303 }
2304 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
2305 if (!a2dp.is_aptx_dual_mono_supported) {
2306 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
2307 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
2308 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
2309 aptx_dsp_cfg->custom_cfg.num_channels);
2310 } else {
2311 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
2312 ALOGV("Successfully updated APTX dual mono enc format with \
2313 samplingrate: %d channels:%d syncmode %d",
2314 aptx_dsp_cfg->custom_cfg.sample_rate,
2315 aptx_dsp_cfg->custom_cfg.num_channels,
2316 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
2317 }
2318 return ret;
2319}
2320#else
2321static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
2322 audio_aptx_encoder_config *aptx_bt_cfg)
2323{
2324 int ret = 0;
2325
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002326 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05302327 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
2328 aptx_dsp_cfg, aptx_bt_cfg);
2329 return -EINVAL;
2330 }
2331
2332 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002333 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05302334 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
2335 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
2336 switch(aptx_dsp_cfg->num_channels) {
2337 case 1:
2338 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
2339 break;
2340 case 2:
2341 default:
2342 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
2343 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
2344 break;
2345 }
2346
2347 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
2348 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
2349
2350 return ret;
2351}
2352#endif
2353
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302354/* API to configure APTX DSP encoder */
2355bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2356{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302357 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08002358 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08002359 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302360 bool is_configured = false;
2361 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08002362 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05302363
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002364 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05302365 return false;
2366
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002367 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2368 if (!ctl_enc_data) {
2369 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2370 return false;
2371 }
2372
Manish Dewangan6a252632017-12-04 17:27:44 +05302373#ifndef LINUX_ENABLED
2374 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302375 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08002376 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002377 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08002378 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
2379 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002380 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08002381 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002382 else
Zhou Song8fccbb62019-03-20 01:08:19 +08002383 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002384 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002385 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002386
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002387 if (ret) {
2388 is_configured = false;
2389 goto fail;
2390 }
2391
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002392 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08002393 if (aptx_ad_ctl)
2394 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002395 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08002396 else
2397 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002398 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002399 } else {
2400 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002401 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302402 }
Manish Dewangan6a252632017-12-04 17:27:44 +05302403#else
2404 struct custom_enc_cfg_t aptx_dsp_cfg;
2405 mixer_size = sizeof(struct custom_enc_cfg_t);
2406 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05302407 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05302408 if (ret) {
2409 is_configured = false;
2410 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302411 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002412 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
2413 mixer_size);
2414#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302415 if (ret != 0) {
2416 ALOGE("%s: Failed to set APTX encoder config", __func__);
2417 is_configured = false;
2418 goto fail;
2419 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302420#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002421 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302422 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002423 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302424 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
2425 else
2426 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302427#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302428 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302429 is_configured = false;
2430 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302431 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302432 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302433 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05302434 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302435 else
Surendar Karka2febd452018-12-13 17:56:43 +05302436 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302437fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05302438 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002439 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05302440 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302441 return is_configured;
2442}
2443
2444/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302445 */
Manish Dewangan6a252632017-12-04 17:27:44 +05302446#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002447bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302448#else
2449bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2450#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302451{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302452 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302453 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302454 bool is_configured = false;
2455 int ret = 0;
2456
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002457 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302458 return false;
2459
2460 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2461 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002462 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302463 is_configured = false;
2464 goto fail;
2465 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302466
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302467 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002468 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302469 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2470 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2471 switch(aptx_dsp_cfg.num_channels) {
2472 case 1:
2473 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2474 break;
2475 case 2:
2476 default:
2477 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2478 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2479 break;
2480 }
2481 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302482 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302483 if (ret != 0) {
2484 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2485 is_configured = false;
2486 goto fail;
2487 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302488 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302489 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302490 is_configured = false;
2491 goto fail;
2492 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302493 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002494 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302495 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302496 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302497 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2498 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302499fail:
2500 return is_configured;
2501}
2502
2503/* API to configure AAC DSP encoder */
2504bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2505{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302506 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302507 struct aac_enc_cfg_t aac_dsp_cfg;
2508 bool is_configured = false;
2509 int ret = 0;
2510
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002511 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302512 return false;
2513
2514 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2515 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002516 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302517 is_configured = false;
2518 goto fail;
2519 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302520 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002521 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302522 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302523 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002524 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302525 case 0:
2526 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2527 break;
2528 case 2:
2529 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2530 break;
2531 case 1:
2532 default:
2533 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2534 break;
2535 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302536 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302537 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002538
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302539 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2540 sizeof(struct aac_enc_cfg_t));
2541 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002542 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302543 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302544 goto fail;
2545 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302546 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302547 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302548 is_configured = false;
2549 goto fail;
2550 }
2551 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002552 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302553 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002554 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002555 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2556 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2557fail:
2558 return is_configured;
2559}
2560
2561bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2562{
2563 struct mixer_ctl *ctl_enc_data = NULL;
2564 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2565 bool is_configured = false;
2566 int ret = 0;
2567
2568 if (aac_bt_cfg == NULL)
2569 return false;
2570
2571 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2572 if (!ctl_enc_data) {
2573 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2574 is_configured = false;
2575 goto fail;
2576 }
2577 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302578 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002579 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2580 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2581 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2582 case 0:
2583 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2584 break;
2585 case 2:
2586 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2587 break;
2588 case 1:
2589 default:
2590 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2591 break;
2592 }
2593 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2594 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2595 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2596 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2597
2598 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2599 sizeof(struct aac_enc_cfg_v2_t));
2600 if (ret != 0) {
2601 ALOGE("%s: Failed to set AAC encoder config", __func__);
2602 is_configured = false;
2603 goto fail;
2604 }
2605 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2606 if (ret != 0) {
2607 is_configured = false;
2608 goto fail;
2609 }
2610 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302611 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002612 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2613 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2614 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2615 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302616fail:
2617 return is_configured;
2618}
2619
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302620bool configure_aac_enc_format_v3(audio_aac_encoder_config_v3 *aac_bt_cfg)
2621{
2622 struct mixer_ctl *ctl_enc_data = NULL;
2623 struct aac_enc_cfg_v3_t aac_dsp_cfg;
2624 struct aac_frame_size_control_t* frame_vbr_ctl = NULL;
2625 bool is_configured = false;
2626 int ret = 0;
2627
2628 if (aac_bt_cfg == NULL)
2629 return false;
2630
2631 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2632 if (!ctl_enc_data) {
2633 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2634 is_configured = false;
2635 goto fail;
2636 }
2637 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v3_t));
2638 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
2639 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2640 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2641 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2642 case 0:
2643 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2644 break;
2645 case 2:
2646 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2647 break;
2648 case 1:
2649 default:
2650 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2651 break;
2652 }
2653 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2654 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2655 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2656 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2657 frame_vbr_ctl = aac_bt_cfg->frame_ptr_ctl;
2658
2659 if (frame_vbr_ctl != NULL) {
2660 aac_dsp_cfg.aac_key_value_ctl.ctl_type = frame_vbr_ctl->ctl_type;
2661 aac_dsp_cfg.aac_key_value_ctl.ctl_value = frame_vbr_ctl->ctl_value;
2662 } else {
2663 ALOGE("%s: VBR cannot be enabled, fall back to default",__func__);
2664 aac_dsp_cfg.aac_key_value_ctl.ctl_type = 0;
2665 aac_dsp_cfg.aac_key_value_ctl.ctl_value = 0;
2666 }
2667
2668 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2669 sizeof(struct aac_enc_cfg_v3_t));
2670 if (ret != 0) {
2671 ALOGE("%s: Failed to set AAC encoder config", __func__);
2672 is_configured = false;
2673 goto fail;
2674 }
2675 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2676 if (ret != 0) {
2677 is_configured = false;
2678 goto fail;
2679 }
2680 is_configured = true;
2681 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
2682 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2683 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2684 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2685 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
2686fail:
2687 return is_configured;
2688}
2689
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302690bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2691{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302692 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302693 struct celt_enc_cfg_t celt_dsp_cfg;
2694 bool is_configured = false;
2695 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002696 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302697 return false;
2698
2699 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2700 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002701 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302702 is_configured = false;
2703 goto fail;
2704 }
2705 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2706
Florian Pfister1a84f312018-07-19 14:38:18 +02002707 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302708 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2709 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2710 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2711 case 1:
2712 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2713 break;
2714 case 2:
2715 default:
2716 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2717 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2718 break;
2719 }
2720
2721 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2722
2723 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2724 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2725 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2726 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2727 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2728
2729 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2730 sizeof(struct celt_enc_cfg_t));
2731 if (ret != 0) {
2732 ALOGE("%s: Failed to set CELT encoder config", __func__);
2733 is_configured = false;
2734 goto fail;
2735 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302736 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302737 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302738 is_configured = false;
2739 goto fail;
2740 }
2741 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002742 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302743 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302744 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302745 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2746 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2747fail:
2748 return is_configured;
2749}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302750
2751bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2752{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302753 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302754 struct ldac_enc_cfg_t ldac_dsp_cfg;
2755 bool is_configured = false;
2756 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002757 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302758 return false;
2759
2760 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2761 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002762 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302763 is_configured = false;
2764 goto fail;
2765 }
2766 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2767
Florian Pfister1a84f312018-07-19 14:38:18 +02002768 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302769 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2770 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2771 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2772 case 4:
2773 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2774 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2775 break;
2776 case 2:
2777 case 1:
2778 default:
2779 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2780 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2781 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2782 break;
2783 }
2784
2785 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2786 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2787 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002788 if (ldac_bt_cfg->is_abr_enabled) {
2789 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2790 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2791 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2792 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2793 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002794 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002795 }
2796
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302797 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2798 sizeof(struct ldac_enc_cfg_t));
2799 if (ret != 0) {
2800 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2801 is_configured = false;
2802 goto fail;
2803 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302804 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302805 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302806 is_configured = false;
2807 goto fail;
2808 }
2809 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002810 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302811 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2812 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002813 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302814 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2815 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2816fail:
2817 return is_configured;
2818}
2819
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302820bool configure_a2dp_encoder_format()
2821{
2822 void *codec_info = NULL;
2823 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002824 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302825 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002826 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302827
Florian Pfister1a84f312018-07-19 14:38:18 +02002828 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302829 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2830 return false;
2831 }
2832 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002833 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302834 &codec_type);
2835
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002836 // ABR disabled by default for all codecs
2837 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302838 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002839
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302840 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002841 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302842 ALOGD(" Received SBC encoder supported BT device");
2843 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002844 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302845 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002846 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302847 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302848#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002849 a2dp.is_aptx_dual_mono_supported = false;
2850 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302851#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302852 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002853 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302854 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002855 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302856 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302857#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302858 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002859 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302860#else
2861 is_configured =
2862 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2863#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002864 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302865#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002866 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002867 ALOGD(" Received APTX dual mono encoder supported BT device");
2868 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302869 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2870 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002871 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2872 is_configured =
2873 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302874 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302875#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002876 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302877 ALOGD(" Received AAC encoder supported BT device");
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302878 bool is_aac_vbr_enabled =
2879 property_get_bool("persist.vendor.bt.aac_vbr_frm_ctl.enabled", false);
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002880 bool is_aac_frame_ctl_enabled =
2881 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302882 if (is_aac_vbr_enabled)
2883 is_configured = configure_aac_enc_format_v3((audio_aac_encoder_config_v3 *) codec_info);
2884 else
2885 is_configured = is_aac_frame_ctl_enabled ?
2886 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2887 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302888 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002889 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302890 ALOGD(" Received CELT encoder supported BT device");
2891 is_configured =
2892 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2893 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002894 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302895 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002896 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2897 instance_id = MAX_INSTANCE_ID;
2898 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302899 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002900 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302901 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302902 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302903#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302904 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302905 ALOGD(" Received APTX AD encoder supported BT device");
2906 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2907 instance_id = MAX_INSTANCE_ID;
2908 a2dp.abr_config.imc_instance = instance_id--;
2909 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2910 a2dp.is_aptx_adaptive = true;
2911 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2912 is_configured =
2913 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302914 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302915 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302916#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002917 case CODEC_TYPE_PCM:
2918 ALOGD("Received PCM format for BT device");
2919 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2920 is_configured = true;
2921 break;
Manisha Agarwald45632b2019-10-17 18:14:28 +08002922 case CODEC_TYPE_LC3:
2923 ALOGD("Received LC3 encoder supported BT device");
2924 a2dp.bt_encoder_format = CODEC_TYPE_LC3;
2925 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2926 instance_id = MAX_INSTANCE_ID;
2927 a2dp.abr_config.imc_instance = instance_id--;
2928 a2dp.abr_config.is_abr_enabled = true;
2929 is_configured =
2930 (configure_lc3_enc_format((audio_lc3_codec_config_t *)codec_info) &&
2931 configure_lc3_dec_format((audio_lc3_codec_config_t *)codec_info));
2932 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302933 default:
2934 ALOGD(" Received Unsupported encoder formar");
2935 is_configured = false;
2936 break;
2937 }
2938 return is_configured;
2939}
2940
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002941int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302942{
2943 int ret = 0;
2944
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002945 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302946
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002947 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002948 && a2dp.audio_get_enc_config)) {
2949 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302950 return -ENOSYS;
2951 }
2952
Zhou Song12c29502019-03-16 10:37:18 +08002953 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302954 //session will be restarted after suspend completion
2955 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302956 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302957 }
2958
Florian Pfister1a84f312018-07-19 14:38:18 +02002959 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302960 ALOGD("calling BT module stream start");
2961 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002962 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302963 ALOGE("BT controller start return = %d",ret);
2964 if (ret != 0 ) {
2965 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002966 a2dp.a2dp_source_started = false;
Revathi Uddarajub26e3932020-06-10 14:51:02 +05302967 ret = -ETIMEDOUT;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302968 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002969 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002970 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302971 ret = 0;
2972 ALOGD("Start playback successful to BT library");
2973 } else {
2974 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002975 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302976 ret = -ETIMEDOUT;
2977 }
2978 }
2979 }
2980
Florian Pfister1a84f312018-07-19 14:38:18 +02002981 if (a2dp.a2dp_source_started) {
2982 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302983 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302984 audio_a2dp_update_tws_channel_mode();
Manisha Agarwald45632b2019-10-17 18:14:28 +08002985 audio_a2dp_update_lc3_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002986 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002987 if (a2dp.abr_config.is_abr_enabled)
2988 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302989 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302990
2991 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002992 a2dp.a2dp_source_total_active_session_requests);
2993 return ret;
2994}
2995
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002996uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002997{
2998 uint32_t latency = 0;
2999
3000 switch(a2dp.bt_decoder_format) {
3001 case CODEC_TYPE_SBC:
3002 latency = DEFAULT_SINK_LATENCY_SBC;
3003 break;
3004 case CODEC_TYPE_AAC:
3005 latency = DEFAULT_SINK_LATENCY_AAC;
3006 break;
3007 default:
3008 latency = 200;
3009 ALOGD("No valid decoder defined, setting latency to %dms", latency);
3010 break;
3011 }
3012 return (uint64_t)latency;
3013}
3014
3015bool a2dp_send_sink_setup_complete(void) {
3016 uint64_t system_latency = 0;
3017 bool is_complete = false;
3018
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003019 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02003020
3021 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
3022 is_complete = true;
3023 }
3024 return is_complete;
3025}
3026
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003027bool a2dp_sink_is_ready()
3028{
3029 bool ret = false;
3030
3031 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
3032 (a2dp.is_a2dp_offload_supported) &&
3033 (a2dp.audio_sink_check_a2dp_ready))
3034 ret = a2dp.audio_sink_check_a2dp_ready();
3035 return ret;
3036}
3037
3038int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02003039{
3040 int ret = 0;
3041
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003042 ALOGD("a2dp_start_capture start");
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303043 /* checking for sink lib for mobile platform not available then using source lib */
3044 if ((!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
3045 && a2dp.audio_get_dec_config)) && (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
3046 && a2dp.audio_get_enc_config))) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003047 ALOGE("a2dp handle is not identified, Ignoring start capture request");
3048 return -ENOSYS;
3049 }
3050
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303051 if (a2dp.bt_lib_sink_handle && !a2dp.a2dp_sink_started
Zhou Songf7a2fdf2021-05-07 09:14:21 +08003052 && !a2dp.a2dp_sink_total_active_session_requests && a2dp.audio_sink_start) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003053 ALOGD("calling BT module stream start");
3054 /* This call indicates BT IPC lib to start capture */
3055 ret = a2dp.audio_sink_start();
3056 ALOGE("BT controller start capture return = %d",ret);
3057 if (ret != 0 ) {
3058 ALOGE("BT controller start capture failed");
3059 a2dp.a2dp_sink_started = false;
3060 } else {
3061
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003062 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003063 ALOGD("Wait for capture ready not successful");
3064 ret = -ETIMEDOUT;
3065 }
3066
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003067 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003068 a2dp.a2dp_sink_started = true;
3069 ret = 0;
3070 ALOGD("Start capture successful to BT library");
3071 } else {
3072 ALOGD(" unable to configure DSP decoder");
3073 a2dp.a2dp_sink_started = false;
3074 ret = -ETIMEDOUT;
3075 }
3076
3077 if (!a2dp_send_sink_setup_complete()) {
3078 ALOGD("sink_setup_complete not successful");
3079 ret = -ETIMEDOUT;
3080 }
3081 }
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303082 } else if ((a2dp.bt_lib_source_handle) && (configure_a2dp_sink_decoder_format())) {
3083 if (a2dp.audio_source_start) {
3084 ret = a2dp.audio_source_start();
3085 if (ret == 0) {
3086 a2dp.a2dp_sink_started = true;
3087 ALOGD("Start capture successful to BT library");
3088 } else {
3089 ALOGE("BT controller start failed");
3090 }
3091 }
3092 } else {
3093 ALOGD(" unable to configure DSP decoder");
3094 a2dp.a2dp_sink_started = false;
3095 ret = -ETIMEDOUT;
Florian Pfister1a84f312018-07-19 14:38:18 +02003096 }
3097
3098 if (a2dp.a2dp_sink_started) {
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303099 if (a2dp_set_backend_cfg(SINK) == true)
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003100 a2dp.a2dp_sink_total_active_session_requests++;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303101 /* Start abr for LC3 decoder*/
3102 if (a2dp.bt_decoder_format == CODEC_TYPE_LC3) {
3103 a2dp.abr_config.is_abr_enabled = true;
3104 start_abr();
Florian Pfister1a84f312018-07-19 14:38:18 +02003105 }
3106 }
3107
3108 ALOGD("start A2DP sink total active sessions :%d",
3109 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303110 return ret;
3111}
3112
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303113static void reset_a2dp_enc_config_params()
3114{
3115 int ret =0;
3116
Aalique Grahame22e49102018-12-18 14:23:57 -08003117 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303118 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05303119 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303120
3121 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
3122 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
3123 MIXER_ENC_CONFIG_BLOCK);
3124 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003125 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303126 } else {
3127 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
3128 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02003129 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303130 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003131
3132 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3133
Manisha Agarwala51768b2018-11-01 16:30:52 +05303134 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
3135
3136 if (!ctl_channel_mode) {
3137 ALOGE("failed to get tws mixer ctl");
3138 } else {
3139 channel_mode = "Two";
3140 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
3141 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
3142 }
3143 a2dp.is_tws_mono_mode_on = false;
3144 }
Manisha Agarwald45632b2019-10-17 18:14:28 +08003145
3146 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_LC3_CHANNEL_MODE);
3147
3148 if (!ctl_channel_mode) {
3149 ALOGE("failed to get lc3 mixer ctl");
3150 } else {
3151 channel_mode = "Two";
3152 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0)
3153 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
3154 a2dp.is_lc3_mono_mode_on = false;
3155 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303156}
3157
Surendar Karka2febd452018-12-13 17:56:43 +05303158static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07003159{
3160 struct mixer_ctl *ctl_dec_data = NULL;
3161 struct abr_dec_cfg_t dummy_reset_cfg;
3162 int ret = 0;
3163
Zhou Songf7a2fdf2021-05-07 09:14:21 +08003164 if (a2dp.bt_decoder_format == CODEC_TYPE_LC3)
3165 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
3166
Preetam Singh Ranawat25780e42019-07-31 18:15:57 +05303167 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3168 if (!ctl_dec_data) {
3169 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
3170 return -EINVAL;
3171 }
3172 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
3173 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
3174 sizeof(dummy_reset_cfg));
3175 if (ret != 0) {
3176 ALOGE("%s: Failed to set dummy decoder config", __func__);
3177 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07003178 }
3179
3180 return ret;
3181}
3182
Surendar Karka2febd452018-12-13 17:56:43 +05303183static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02003184{
3185 int ret =0;
3186
3187 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
3188 struct aac_dec_cfg_t dummy_reset_config;
3189
3190 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
3191 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05303192 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02003193 if (!ctl_dec_config) {
3194 ALOGE(" ERROR a2dp decoder format mixer control not identified");
3195 } else {
3196 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
3197 sizeof(struct aac_dec_cfg_t));
3198 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
3199 }
3200 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
3201 MIXER_DEC_BIT_FORMAT);
3202 if (!ctrl_bit_format) {
3203 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
3204 } else {
3205 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
3206 if (ret != 0) {
3207 ALOGE("%s: Failed to set bit format to decoder", __func__);
3208 }
3209 }
3210}
3211
Zhou Song12c29502019-03-16 10:37:18 +08003212static void reset_codec_config()
3213{
3214 reset_a2dp_enc_config_params();
3215 reset_a2dp_source_dec_config_params();
3216 a2dp_reset_backend_cfg(SOURCE);
3217 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
3218 stop_abr();
3219 a2dp.abr_config.is_abr_enabled = false;
3220}
3221
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003222int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303223{
3224 int ret =0;
3225
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003226 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003227 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003228 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303229 return -ENOSYS;
3230 }
3231
Florian Pfister1a84f312018-07-19 14:38:18 +02003232 if (a2dp.a2dp_source_total_active_session_requests > 0)
3233 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08003234 else
3235 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303236
Florian Pfister1a84f312018-07-19 14:38:18 +02003237 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303238 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02003239 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303240 if (ret < 0)
3241 ALOGE("stop stream to BT IPC lib failed");
3242 else
3243 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08003244 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
3245 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05303246 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303247 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003248 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02003249 a2dp.a2dp_source_started = false;
3250 ALOGD("Stop A2DP playback, total active sessions :%d",
3251 a2dp.a2dp_source_total_active_session_requests);
3252 return 0;
3253}
3254
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003255int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02003256{
3257 int ret =0;
3258
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003259 ALOGV("a2dp_stop_capture start");
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303260 if ((!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop))
3261 && (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop))) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003262 ALOGE("a2dp handle is not identified, Ignoring stop request");
3263 return -ENOSYS;
3264 }
3265
3266 if (a2dp.a2dp_sink_total_active_session_requests > 0)
3267 a2dp.a2dp_sink_total_active_session_requests--;
3268
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303269 if (a2dp.bt_lib_sink_handle && a2dp.a2dp_sink_started
Zhou Songf7a2fdf2021-05-07 09:14:21 +08003270 && !a2dp.a2dp_sink_total_active_session_requests && a2dp.audio_sink_stop) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003271 ALOGV("calling BT module stream stop");
3272 ret = a2dp.audio_sink_stop();
3273 if (ret < 0)
3274 ALOGE("stop stream to BT IPC lib failed");
3275 else
3276 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05303277 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02003278 a2dp_reset_backend_cfg(SINK);
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303279 } else if (a2dp.bt_lib_source_handle) {
3280 ret = a2dp.audio_source_stop();
3281 if (ret < 0)
3282 ALOGE("stop stream to BT IPC lib failed");
3283 else
3284 ALOGV("stop steam to BT IPC lib successful");
Zhou Songf7a2fdf2021-05-07 09:14:21 +08003285 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303286 reset_codec_config();
3287 a2dp.a2dp_sink_started = false;
Florian Pfister1a84f312018-07-19 14:38:18 +02003288 }
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303289
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003290 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02003291 a2dp.a2dp_source_started = false;
3292 ALOGD("Stop A2DP capture, total active sessions :%d",
3293 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303294 return 0;
3295}
3296
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003297int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303298{
Weiyin Jiang280ea742020-09-08 20:28:22 +08003299 int ret = 0, val, status = 0;
3300 char value[32] = {0};
3301 struct audio_usecase *uc_info;
3302 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303303
Weiyin Jiang280ea742020-09-08 20:28:22 +08003304 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003305 ALOGV("no supported encoders identified,ignoring a2dp setparam");
3306 status = -EINVAL;
3307 goto param_handled;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003308 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303309
Weiyin Jiang280ea742020-09-08 20:28:22 +08003310 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303311 sizeof(value));
Weiyin Jiang280ea742020-09-08 20:28:22 +08003312 if (ret >= 0) {
3313 val = atoi(value);
3314 if (audio_is_a2dp_out_device(val)) {
3315 ALOGV("Received device connect request for A2DP source");
3316 open_a2dp_source();
3317 }
3318 goto param_handled;
3319 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303320
Weiyin Jiang280ea742020-09-08 20:28:22 +08003321 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
3322 sizeof(value));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303323
Weiyin Jiang280ea742020-09-08 20:28:22 +08003324 if (ret >= 0) {
3325 val = atoi(value);
3326 if (audio_is_a2dp_out_device(val)) {
3327 ALOGV("Received source device dis- connect request");
3328 close_a2dp_output();
3329 reset_a2dp_enc_config_params();
3330 reset_a2dp_source_dec_config_params();
3331 a2dp_reset_backend_cfg(SOURCE);
3332 } else if (audio_is_a2dp_in_device(val)) {
3333 ALOGV("Received sink device dis- connect request");
3334 close_a2dp_input();
3335 reset_a2dp_sink_dec_config_params();
3336 a2dp_reset_backend_cfg(SINK);
3337 }
3338 goto param_handled;
3339 }
Surendar Karka2febd452018-12-13 17:56:43 +05303340#ifndef LINUX_ENABLED
Weiyin Jiang280ea742020-09-08 20:28:22 +08003341 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
3342 if (ret >= 0) {
3343 ALOGD("Setting tws channel mode to %s",value);
3344 if (!(strncmp(value, "mono", strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05303345 a2dp.is_tws_mono_mode_on = true;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003346 else if (!(strncmp(value, "dual-mono", strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05303347 a2dp.is_tws_mono_mode_on = false;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003348 audio_a2dp_update_tws_channel_mode();
3349 goto param_handled;
3350 }
Manisha Agarwald45632b2019-10-17 18:14:28 +08003351
3352 ret = str_parms_get_str(parms, "LEAMono", value, sizeof(value));
3353 if (ret>=0) {
3354 ALOGD("Setting LC3 channel mode to %s",value);
3355 if (!(strncmp(value,"true",strlen(value))))
3356 a2dp.is_lc3_mono_mode_on = true;
3357 else
3358 a2dp.is_lc3_mono_mode_on = false;
3359 audio_a2dp_update_lc3_channel_mode();
3360 goto param_handled;
3361 }
Surendar Karka2febd452018-12-13 17:56:43 +05303362#endif
Weiyin Jiang280ea742020-09-08 20:28:22 +08003363 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
3364 if (ret >= 0) {
3365 if (a2dp.bt_lib_source_handle == NULL)
3366 goto param_handled;
3367
3368 if ((!strncmp(value, "true", sizeof(value)))) {
3369 if (a2dp.a2dp_source_suspended) {
3370 ALOGD("%s: A2DP is already suspended", __func__);
3371 goto param_handled;
3372 }
3373 ALOGD("Setting a2dp to suspend state");
3374 a2dp.a2dp_source_suspended = true;
3375 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
3376 goto param_handled;
3377 list_for_each(node, &a2dp.adev->usecase_list) {
3378 uc_info = node_to_item(node, struct audio_usecase, list);
3379 if (uc_info->type == PCM_PLAYBACK &&
3380 (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
3381 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
3382 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)) {
3383 fp_check_a2dp_restore_l(a2dp.adev, uc_info->stream.out, false);
Aalique Grahame22e49102018-12-18 14:23:57 -08003384 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08003385 }
3386 if (!a2dp.swb_configured)
3387 reset_codec_config();
3388 if (a2dp.audio_source_suspend)
3389 a2dp.audio_source_suspend();
3390 } else if (a2dp.a2dp_source_suspended == true) {
3391 ALOGD("Resetting a2dp suspend state");
3392 struct audio_usecase *uc_info;
3393 struct listnode *node;
3394 if (a2dp.clear_source_a2dpsuspend_flag)
3395 a2dp.clear_source_a2dpsuspend_flag();
3396 a2dp.a2dp_source_suspended = false;
3397 /*
3398 * It is possible that before suspend,a2dp sessions can be active
3399 * for example during music + voice activation concurrency
3400 * a2dp suspend will be called & BT will change to sco mode
3401 * though music is paused as a part of voice activation
3402 * compress session close happens only after pause timeout(10secs)
3403 * so if resume request comes before pause timeout as a2dp session
3404 * is already active IPC start will not be called from APM/audio_hw
3405 * Fix is to call a2dp start for IPC library post suspend
3406 * based on number of active session count
3407 */
3408 if (a2dp.a2dp_source_total_active_session_requests > 0) {
3409 ALOGD(" Calling IPC lib start post suspend state");
3410 if (a2dp.audio_source_start) {
3411 ret = a2dp.audio_source_start();
3412 if (ret != 0) {
3413 ALOGE("BT controller start failed");
3414 a2dp.a2dp_source_started = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303415 }
3416 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08003417 }
3418 list_for_each(node, &a2dp.adev->usecase_list) {
3419 uc_info = node_to_item(node, struct audio_usecase, list);
3420 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
3421 is_a2dp_out_device_type(&uc_info->stream.out->device_list)) {
3422 fp_check_a2dp_restore_l(a2dp.adev, uc_info->stream.out, true);
Zhou Song10617ed2017-05-26 13:28:48 +08003423 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303424 }
3425 }
3426 goto param_handled;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003427 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003428
Weiyin Jiang280ea742020-09-08 20:28:22 +08003429 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
3430 sizeof(value));
3431 if (ret >= 0) {
3432 if (a2dp.is_a2dp_offload_supported &&
3433 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
3434 *reconfig = true;
3435 }
3436 goto param_handled;
3437 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003438
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303439param_handled:
Weiyin Jiang280ea742020-09-08 20:28:22 +08003440 ALOGV("end of a2dp setparam");
3441 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303442}
3443
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003444void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05303445{
3446 a2dp.is_handoff_in_progress = is_on;
3447}
3448
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003449bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303450{
3451 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05303452 // Or if a2dp device is selected but earlier start failed ( as a2dp
3453 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02003454 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303455}
3456
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003457void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303458{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303459 *sample_rate = a2dp.enc_sampling_rate;
3460}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303461
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003462void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02003463{
3464 *sample_rate = a2dp.dec_sampling_rate;
3465}
3466
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003467bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303468{
3469 bool ret = false;
3470
Florian Pfister1a84f312018-07-19 14:38:18 +02003471 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303472 return ret;
3473
Florian Pfister1a84f312018-07-19 14:38:18 +02003474 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08003475 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02003476 (a2dp.audio_source_check_a2dp_ready))
3477 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303478 return ret;
3479}
3480
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003481bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02003482{
3483 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303484}
3485
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003486void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08003487 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303488{
3489 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02003490 a2dp.bt_lib_source_handle = NULL;
3491 a2dp.a2dp_source_started = false;
3492 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
3493 a2dp.a2dp_source_total_active_session_requests = 0;
3494 a2dp.a2dp_source_suspended = false;
3495 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303496 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303497 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07003498 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05303499 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08003500 a2dp.abr_config.is_abr_enabled = false;
3501 a2dp.abr_config.abr_started = false;
3502 a2dp.abr_config.imc_instance = 0;
3503 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08003504 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05303505 a2dp.is_tws_mono_mode_on = false;
Manisha Agarwald45632b2019-10-17 18:14:28 +08003506 a2dp.is_lc3_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05303507 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08003508 a2dp.swb_configured = false;
3509
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003510 // init function pointers
3511 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08003512 init_config.fp_platform_get_pcm_device_id;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003513 fp_check_a2dp_restore_l = init_config.fp_check_a2dp_restore_l;
Gautam Manamfbb3ebc2020-10-08 18:06:45 +05303514 fp_platform_switch_voice_call_device_post =
3515 init_config.fp_platform_switch_voice_call_device_post;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003516
kunleiz5a127262017-09-08 14:47:48 +08003517 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05303518 reset_a2dp_source_dec_config_params();
3519 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02003520
3521 a2dp.bt_lib_sink_handle = NULL;
3522 a2dp.a2dp_sink_started = false;
3523 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
3524 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003525
3526 if (is_running_with_enhanced_fwk == UNINITIALIZED)
3527 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
3528 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08003529 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02003530
3531 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303532 update_offload_codec_capabilities();
3533}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003534
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003535uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003536{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003537 uint32_t latency = 0;
3538 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303539 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
3540 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003541 char value[PROPERTY_VALUE_MAX];
3542
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003543 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08003544 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003545 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303546 if (sscanf(value, "%d/%d/%d/%d/%d%d",
3547 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003548 ALOGI("Failed to parse avsync offset params from '%s'.", value);
3549 avsync_runtime_prop = 0;
3550 }
3551 }
3552
yidongh0515e042017-07-06 15:00:34 +08003553 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02003554 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
3555 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08003556 }
3557
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07003558 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003559 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08003560 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
3561 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003562 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003563 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08003564 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
3565 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003566 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003567 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08003568 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
3569 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003570 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003571 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08003572 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
3573 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003574 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003575 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05303576 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
3577 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
3578 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003579 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303580 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
3581 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
3582 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05303583 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05303584 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05303585 break;
Manisha Agarwald45632b2019-10-17 18:14:28 +08003586 case CODEC_TYPE_LC3:
3587 latency = slatency;
3588 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08003589 case CODEC_TYPE_PCM:
3590 latency = ENCODER_LATENCY_PCM;
3591 latency += DEFAULT_SINK_LATENCY_PCM;
3592 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003593 default:
3594 latency = 200;
3595 break;
3596 }
3597 return latency;
3598}
Aalique Grahame22e49102018-12-18 14:23:57 -08003599
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003600int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003601 struct str_parms *reply)
3602{
3603 int ret, val = 0;
3604 char value[32]={0};
3605
3606 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3607 value, sizeof(value));
3608 if (ret >= 0) {
3609 val = a2dp.is_a2dp_offload_supported;
3610 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3611 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3612 }
3613
3614 return 0;
3615}
Zhou Song12c29502019-03-16 10:37:18 +08003616
3617
3618bool configure_aptx_ad_speech_enc_fmt() {
3619 struct mixer_ctl *ctl_enc_data = NULL;
3620 int mixer_size = 0;
3621 int ret = 0;
3622 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3623
3624 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3625 if (!ctl_enc_data) {
3626 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3627 return false;
3628 }
3629
3630 /* Initialize dsp configuration params */
3631 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3632 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3633 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3634 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3635 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3636 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3637 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3638 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3639 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3640 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3641 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3642
3643 /* Configure AFE DSP configuration */
3644 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3645 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3646 mixer_size);
3647 if (ret != 0) {
3648 ALOGE("%s: Failed to set SWB encoder config", __func__);
3649 return false;
3650 }
3651
3652 /* Configure AFE Input Bit Format as PCM_16 */
3653 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3654 if (ret != 0) {
3655 ALOGE("%s: Failed to set SWB bit format", __func__);
3656 return false;
3657 }
3658
3659 return true;
3660}
3661
3662bool configure_aptx_ad_speech_dec_fmt()
3663{
3664 struct mixer_ctl *ctl_dec_data = NULL;
3665 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3666 int ret = 0;
3667
3668 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3669 if (!ctl_dec_data) {
3670 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3671 return false;
3672 }
3673 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3674 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3675 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3676 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3677 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3678 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3679 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3680 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3681
3682 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3683 sizeof(dec_cfg));
3684 if (ret != 0) {
3685 ALOGE("%s: Failed to set decoder config", __func__);
3686 return false;
3687 }
3688 return true;
3689}
3690
3691int sco_start_configuration()
3692{
3693 ALOGD("sco_start_configuration start");
3694
3695 if (!a2dp.swb_configured) {
Gautam Manamfbb3ebc2020-10-08 18:06:45 +05303696 /*Before starting sco config, we must ensure to set correct acdb id
3697 because sco cofiguration will trigger port open which needs acdb_id*/
3698 fp_platform_switch_voice_call_device_post(a2dp.adev->platform,
3699 SND_DEVICE_OUT_BT_SCO_SWB,
3700 SND_DEVICE_IN_BT_SCO_MIC_SWB);
3701
Zhou Song12c29502019-03-16 10:37:18 +08003702 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3703 /* Configure AFE codec*/
3704 if (configure_aptx_ad_speech_enc_fmt() &&
3705 configure_aptx_ad_speech_dec_fmt()) {
3706 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3707 } else {
3708 ALOGE("%s: failed to send SCO configuration", __func__);
3709 return -ETIMEDOUT;
3710 }
3711 /* Configure backend*/
3712 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3713 a2dp.enc_channels = CH_MONO;
3714 a2dp.abr_config.is_abr_enabled = true;
3715 a2dp_set_backend_cfg(SOURCE);
3716 /* Start abr*/
3717 start_abr();
3718 a2dp.swb_configured = true;
3719 }
3720 return 0;
3721}
3722
3723void sco_reset_configuration()
3724{
Zhou Songd6d71752019-05-21 18:08:51 +08003725 if (a2dp.swb_configured) {
3726 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003727
Zhou Songd6d71752019-05-21 18:08:51 +08003728 reset_codec_config();
3729 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3730 a2dp.swb_configured = false;
3731 }
Zhou Song12c29502019-03-16 10:37:18 +08003732}