blob: b8a696fdbb9b39bee0b915d130ae54559a8b8742 [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"
Zhou Songab94f062021-05-12 13:11:17 +080097#define MIXER_SLIM7_TX_ADM_CHANNEL "SLIM7_TX ADM Channels"
yidongh0515e042017-07-06 15:00:34 +080098#define ENCODER_LATENCY_SBC 10
99#define ENCODER_LATENCY_APTX 40
100#define ENCODER_LATENCY_APTX_HD 20
101#define ENCODER_LATENCY_AAC 70
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530102//To Do: Fine Tune Encoder CELT/LDAC latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530103#define ENCODER_LATENCY_CELT 40
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530104#define ENCODER_LATENCY_LDAC 40
Aalique Grahame22e49102018-12-18 14:23:57 -0800105#define ENCODER_LATENCY_PCM 50
yidongh0515e042017-07-06 15:00:34 +0800106#define DEFAULT_SINK_LATENCY_SBC 140
107#define DEFAULT_SINK_LATENCY_APTX 160
108#define DEFAULT_SINK_LATENCY_APTX_HD 180
109#define DEFAULT_SINK_LATENCY_AAC 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530110//To Do: Fine Tune Default CELT/LDAC Latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530111#define DEFAULT_SINK_LATENCY_CELT 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530112#define DEFAULT_SINK_LATENCY_LDAC 180
Aalique Grahame22e49102018-12-18 14:23:57 -0800113#define DEFAULT_SINK_LATENCY_PCM 140
114
Manisha Agarwald45632b2019-10-17 18:14:28 +0800115// decoder structure is considered.
116#define BAP_UNICAST 1
117// decoder structure is ignored.
118#define BAP_BROADCAST 2
119// decoder structure is treated as second toAirConfig.
120#define BAP_BA_SIMULCAST 3
121
Aalique Grahame22e49102018-12-18 14:23:57 -0800122#define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported"
123#define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled"
124#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
125
126// Default encoder bit width
127#define DEFAULT_ENCODER_BIT_FORMAT 16
128
Manisha Agarwald45632b2019-10-17 18:14:28 +0800129// PCM_24 encoder bit width
130#define ENCODER_BIT_FORMAT_PCM_24 24
131
Aalique Grahame22e49102018-12-18 14:23:57 -0800132// Default encoder latency
133#define DEFAULT_ENCODER_LATENCY 200
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530134
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800135// Slimbus Tx sample rate for ABR feedback channel
136#define ABR_TX_SAMPLE_RATE "KHZ_8"
137
Zhou Song12c29502019-03-16 10:37:18 +0800138// Slimbus Tx sample rate for APTX AD SPEECH
139#define SPEECH_TX_SAMPLE_RATE "KHZ_96"
140
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800141// Purpose ID for Inter Module Communication (IMC) in AFE
142#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
143
144// Maximum quality levels for ABR
145#define MAX_ABR_QUALITY_LEVELS 5
146
147// Instance identifier for A2DP
148#define MAX_INSTANCE_ID (UINT32_MAX / 2)
149
Zhou Song12c29502019-03-16 10:37:18 +0800150// Instance identifier for SWB
151#define APTX_AD_SPEECH_INSTANCE_ID 37
152
153#define SAMPLING_RATE_96K 96000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530154#define SAMPLING_RATE_48K 48000
155#define SAMPLING_RATE_441K 44100
Zhou Song12c29502019-03-16 10:37:18 +0800156#define SAMPLING_RATE_32K 32000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530157#define CH_STEREO 2
158#define CH_MONO 1
Florian Pfister1a84f312018-07-19 14:38:18 +0200159#define SOURCE 0
160#define SINK 1
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800161#define UNINITIALIZED -1
162
163#ifdef __LP64__
164#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
165#else
166#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
167#endif
168
Manisha Agarwald45632b2019-10-17 18:14:28 +0800169#define AUDIO_LOCATION_MAX 28
170
171uint32_t audio_location_map_array[] = { AUDIO_LOCATION_FRONT_LEFT, AUDIO_LOCATION_FRONT_RIGHT, AUDIO_LOCATION_FRONT_CENTER, AUDIO_LOCATION_LOW_FREQUENCY,
172 AUDIO_LOCATION_BACK_LEFT, AUDIO_LOCATION_BACK_RIGHT, AUDIO_LOCATION_FRONT_LEFT_OF_CENTER,
173 AUDIO_LOCATION_FRONT_RIGHT_OF_CENTER, AUDIO_LOCATION_BACK_CENTER, AUDIO_LOCATION_LOW_FREQUENCY_2,
174 AUDIO_LOCATION_SIDE_LEFT, AUDIO_LOCATION_SIDE_RIGHT, AUDIO_LOCATION_TOP_FRONT_LEFT, AUDIO_LOCATION_TOP_FRONT_RIGHT,
175 AUDIO_LOCATION_TOP_FRONT_CENTER, AUDIO_LOCATION_TOP_CENTER, AUDIO_LOCATION_TOP_BACK_LEFT, AUDIO_LOCATION_TOP_BACK_RIGHT,
176 AUDIO_LOCATION_TOP_SIDE_LEFT, AUDIO_LOCATION_TOP_SIDE_RIGHT, AUDIO_LOCATION_TOP_BACK_CENTER,
177 AUDIO_LOCATION_BOTTOM_FRONT_CENTER, AUDIO_LOCATION_BOTTOM_FRONT_LEFT, AUDIO_LOCATION_BOTTOM_FRONT_RIGHT,
178 AUDIO_LOCATION_FRONT_LEFT_WIDE, AUDIO_LOCATION_FRONT_RIGHT_WIDE, AUDIO_LOCATION_LEFT_SURROUND,
179 AUDIO_LOCATION_RIGHT_SURROUND };
180
181int channel_map_array[] = { PCM_CHANNEL_L, PCM_CHANNEL_R, PCM_CHANNEL_C, PCM_CHANNEL_LFE, PCM_CHANNEL_LB, PCM_CHANNEL_RB, PCM_CHANNEL_FLC,
182 PCM_CHANNEL_FRC, PCM_CHANNEL_CB, PCM_CHANNEL_RS, PCM_CHANNEL_SL, PCM_CHANNEL_SR, PCM_CHANNEL_TFL,
183 PCM_CHANNEL_TFR, PCM_CHANNEL_TFC, PCM_CHANNEL_TC, PCM_CHANNEL_TBL, PCM_CHANNEL_TBR, PCM_CHANNEL_TSL,
184 PCM_CHANNEL_TSR, PCM_CHANNEL_TBC, PCM_CHANNEL_BFC, PCM_CHANNEL_BFL, PCM_CHANNEL_BFR, PCM_CHANNEL_LW,
185 PCM_CHANNEL_RW, PCM_CHANNEL_LS, PCM_CHANNEL_RS };
186
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800187static void *vndk_fwk_lib_handle = NULL;
188static int is_running_with_enhanced_fwk = UNINITIALIZED;
189
190typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
191static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
Florian Pfister1a84f312018-07-19 14:38:18 +0200192
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530193/*
194 * Below enum values are extended from audio_base.h to
Florian Pfister1a84f312018-07-19 14:38:18 +0200195 * to keep encoder and decoder type local to bthost_ipc
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530196 * and audio_hal as these are intended only for handshake
197 * between IPC lib and Audio HAL.
198 */
199typedef enum {
Florian Pfister1a84f312018-07-19 14:38:18 +0200200 CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
201 CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
202 CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
203 CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
204 CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530205#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +0200206 CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530207#endif
Florian Pfister1a84f312018-07-19 14:38:18 +0200208 CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
209 CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
Surendar Karka2febd452018-12-13 17:56:43 +0530210 CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
Zhou Song12c29502019-03-16 10:37:18 +0800211 CODEC_TYPE_APTX_AD_SPEECH = 637534208u, //0x26000000UL
Manisha Agarwald45632b2019-10-17 18:14:28 +0800212 CODEC_TYPE_LC3 = 721420288u, //0x2B000000UL
Aalique Grahame22e49102018-12-18 14:23:57 -0800213 CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u
Florian Pfister1a84f312018-07-19 14:38:18 +0200214}codec_t;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530215
Sharad Sangle95d451b2018-06-19 12:24:20 +0530216/*
217 * enums which describes the APTX Adaptive
218 * channel mode, these values are used by encoder
219 */
220 typedef enum {
221 APTX_AD_CHANNEL_UNCHANGED = -1,
222 APTX_AD_CHANNEL_JOINT_STEREO = 0, // default
223 APTX_AD_CHANNEL_MONO = 1,
224 APTX_AD_CHANNEL_DUAL_MONO = 2,
225 APTX_AD_CHANNEL_STEREO_TWS = 4,
226 APTX_AD_CHANNEL_EARBUD = 8,
227} enc_aptx_ad_channel_mode;
228
229/*
230 * enums which describes the APTX Adaptive
231 * sampling frequency, these values are used
232 * by encoder
233 */
234typedef enum {
235 APTX_AD_SR_UNCHANGED = 0x0,
236 APTX_AD_48 = 0x1, // 48 KHz default
237 APTX_AD_44_1 = 0x2, // 44.1kHz
Zhou Song30e8cea2019-10-22 23:39:25 +0800238 APTX_AD_96 = 0x3, // 96KHz
Sharad Sangle95d451b2018-06-19 12:24:20 +0530239} enc_aptx_ad_s_rate;
240
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530241typedef void (*bt_audio_pre_init_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200242typedef int (*audio_source_open_t)(void);
243typedef int (*audio_source_close_t)(void);
244typedef int (*audio_source_start_t)(void);
245typedef int (*audio_source_stop_t)(void);
246typedef int (*audio_source_suspend_t)(void);
247typedef void (*audio_source_handoff_triggered_t)(void);
248typedef void (*clear_source_a2dpsuspend_flag_t)(void);
249typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
250 uint8_t *num_dev, codec_t *codec_type);
251typedef int (*audio_source_check_a2dp_ready_t)(void);
252typedef int (*audio_is_source_scrambling_enabled_t)(void);
Manisha Agarwala51768b2018-11-01 16:30:52 +0530253typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200254typedef int (*audio_sink_start_t)(void);
255typedef int (*audio_sink_stop_t)(void);
256typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
257typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
258typedef int (*audio_sink_check_a2dp_ready_t)(void);
259typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530260
261enum A2DP_STATE {
262 A2DP_STATE_CONNECTED,
263 A2DP_STATE_STARTED,
264 A2DP_STATE_STOPPED,
265 A2DP_STATE_DISCONNECTED,
266};
267
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800268typedef enum {
269 IMC_TRANSMIT,
270 IMC_RECEIVE,
271} imc_direction_t;
272
273typedef enum {
274 IMC_DISABLE,
275 IMC_ENABLE,
276} imc_status_t;
277
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700278typedef enum {
Zhou Song12c29502019-03-16 10:37:18 +0800279 SWAP_DISABLE,
280 SWAP_ENABLE,
281} swap_status_t;
282
283typedef enum {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700284 MTU_SIZE,
285 PEAK_BIT_RATE,
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530286 BIT_RATE_MODE,
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700287} frame_control_type_t;
288
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800289// --- external function dependency ---
290fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
Weiyin Jiang280ea742020-09-08 20:28:22 +0800291fp_check_a2dp_restore_t fp_check_a2dp_restore_l;
Gautam Manamfbb3ebc2020-10-08 18:06:45 +0530292fp_platform_switch_voice_call_device_post_t fp_platform_switch_voice_call_device_post;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800293
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800294/* PCM config for ABR Feedback hostless front end */
295static struct pcm_config pcm_config_abr = {
296 .channels = 1,
297 .rate = 8000,
298 .period_size = 240,
299 .period_count = 2,
300 .format = PCM_FORMAT_S16_LE,
301 .start_threshold = 0,
302 .stop_threshold = INT_MAX,
303 .avail_min = 0,
304};
305
306/* Adaptive bitrate config for A2DP codecs */
307struct a2dp_abr_config {
308 /* Flag to denote whether Adaptive bitrate is enabled for codec */
309 bool is_abr_enabled;
310 /* Flag to denote whether front end has been opened for ABR */
311 bool abr_started;
312 /* ABR Tx path pcm handle */
313 struct pcm *abr_tx_handle;
Zhou Song12c29502019-03-16 10:37:18 +0800314 /* ABR Rx path pcm handle */
315 struct pcm *abr_rx_handle;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800316 /* ABR Inter Module Communication (IMC) instance ID */
317 uint32_t imc_instance;
318};
319
320static uint32_t instance_id = MAX_INSTANCE_ID;
321
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530322/* structure used to update a2dp state machine
323 * to communicate IPC library
324 * to store DSP encoder configuration information
325 */
326struct a2dp_data {
327 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200328 void *bt_lib_source_handle;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530329 bt_audio_pre_init_t bt_audio_pre_init;
Florian Pfister1a84f312018-07-19 14:38:18 +0200330 audio_source_open_t audio_source_open;
331 audio_source_close_t audio_source_close;
332 audio_source_start_t audio_source_start;
333 audio_source_stop_t audio_source_stop;
334 audio_source_suspend_t audio_source_suspend;
335 audio_source_handoff_triggered_t audio_source_handoff_triggered;
336 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
337 audio_get_enc_config_t audio_get_enc_config;
338 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530339 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200340 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
341 enum A2DP_STATE bt_state_source;
342 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530343 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530344 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200345 bool a2dp_source_started;
346 bool a2dp_source_suspended;
347 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530348 bool is_a2dp_offload_supported;
349 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700350 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530351 /* Mono Mode support for TWS+ */
352 bool is_tws_mono_mode_on;
Manisha Agarwald45632b2019-10-17 18:14:28 +0800353 /* Mono Mode support for LC3 */
354 bool is_lc3_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530355 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800356 /* Adaptive bitrate config for A2DP codecs */
357 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200358
359 void *bt_lib_sink_handle;
360 audio_sink_start_t audio_sink_start;
361 audio_sink_stop_t audio_sink_stop;
362 audio_get_dec_config_t audio_get_dec_config;
363 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
364 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
365 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
366 enum A2DP_STATE bt_state_sink;
367 codec_t bt_decoder_format;
368 uint32_t dec_sampling_rate;
369 uint32_t dec_channels;
370 bool a2dp_sink_started;
371 int a2dp_sink_total_active_session_requests;
Zhou Song12c29502019-03-16 10:37:18 +0800372 bool swb_configured;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530373};
374
375struct a2dp_data a2dp;
376
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800377/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
378 * Structures sent to configure DSP for ABR are defined below.
379 * This data helps DSP configure feedback path (BTSoC to LPASS)
380 * for link quality levels and mapping quality levels to codec
381 * specific bitrate.
382 */
383
384/* Key value pair for link quality level to bitrate mapping. */
385struct bit_rate_level_map_t {
386 uint32_t link_quality_level;
387 uint32_t bitrate;
388};
389
390/* Link quality level to bitrate mapping info sent to DSP. */
391struct quality_level_to_bitrate_info {
392 /* Number of quality levels being mapped.
393 * This will be equal to the size of mapping table.
394 */
395 uint32_t num_levels;
396 /* Quality level to bitrate mapping table */
397 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
398};
399
400/* Structure to set up Inter Module Communication (IMC) between
401 * AFE Decoder and Encoder.
402 */
403struct imc_dec_enc_info {
404 /* Decoder to encoder communication direction.
405 * Transmit = 0 / Receive = 1
406 */
407 uint32_t direction;
408 /* Enable / disable IMC between decoder and encoder */
409 uint32_t enable;
410 /* Purpose of IMC being set up between decoder and encoder.
411 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
412 * is the default value to be sent as purpose.
413 */
414 uint32_t purpose;
415 /* Unique communication instance ID.
416 * purpose and comm_instance together form the actual key
417 * used in IMC registration, which must be the same for
418 * encoder and decoder for which IMC is being set up.
419 */
420 uint32_t comm_instance;
421};
422
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700423/* Structure to control frame size of AAC encoded frames. */
424struct aac_frame_size_control_t {
425 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
426 uint32_t ctl_type;
427 /* Control value
428 * MTU_SIZE: MTU size in bytes
429 * PEAK_BIT_RATE: Peak bitrate in bits per second.
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530430 * BIT_RATE_MODE: Variable bitrate
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700431 */
432 uint32_t ctl_value;
433};
434
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800435/* Structure used for ABR config of AFE encoder and decoder. */
436struct abr_enc_cfg_t {
437 /* Link quality level to bitrate mapping info sent to DSP. */
438 struct quality_level_to_bitrate_info mapping_info;
439 /* Information to set up IMC between decoder and encoder */
440 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700441 /* Flag to indicate whether ABR is enabled */
442 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800443} __attribute__ ((packed));
444
445/* Structure to send configuration for decoder introduced
446 * on AFE Tx path for ABR link quality feedback to BT encoder.
447 */
448struct abr_dec_cfg_t {
449 /* Decoder media format */
450 uint32_t dec_format;
451 /* Information to set up IMC between decoder and encoder */
452 struct imc_dec_enc_info imc_info;
453} __attribute__ ((packed));
454
Zhou Song12c29502019-03-16 10:37:18 +0800455struct aptx_ad_speech_mode_cfg_t
456{
457 uint32_t mode;
458 uint32_t swapping;
459} __attribute__ ((packed));
460
461/* Structure for SWB voice dec config */
462struct aptx_ad_speech_dec_cfg_t {
463 struct abr_dec_cfg_t abr_cfg;
464 struct aptx_ad_speech_mode_cfg_t speech_mode;
465} __attribute__ ((packed));
466
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530467/* START of DSP configurable structures
468 * These values should match with DSP interface defintion
469 */
470
471/* AAC encoder configuration structure. */
472typedef struct aac_enc_cfg_t aac_enc_cfg_t;
473
474/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
475 * supported aac_fmt_flag are ADTS/RAW
476 * supported channel_cfg are Native mode, Mono , Stereo
477 */
478struct aac_enc_cfg_t {
479 uint32_t enc_format;
480 uint32_t bit_rate;
481 uint32_t enc_mode;
482 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530483 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530484 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530485} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530486
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700487struct aac_enc_cfg_v2_t {
488 struct aac_enc_cfg_t aac_enc_cfg;
489 struct aac_frame_size_control_t frame_ctl;
490} __attribute__ ((packed));
491
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530492struct aac_enc_cfg_v3_t {
493 struct aac_enc_cfg_t aac_enc_cfg;
494 struct aac_frame_size_control_t frame_ctl;
495 struct aac_frame_size_control_t aac_key_value_ctl;
496} __attribute__ ((packed));
497
Surendar Karkabbb3c822018-11-12 13:00:38 +0530498typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
499struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200500 uint16_t aac_fmt_flag; /* LATM*/
501 uint16_t audio_object_type; /* LC */
502 uint16_t channels; /* Stereo */
503 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
504 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
505 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530506} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200507
Surendar Karkabbb3c822018-11-12 13:00:38 +0530508typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
509struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200510 uint16_t channels; /* Mono, Stereo */
511 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
512 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530513} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200514
515/* AAC decoder configuration structure. */
516typedef struct aac_dec_cfg_t aac_dec_cfg_t;
517struct aac_dec_cfg_t {
518 uint32_t dec_format;
519 audio_aac_decoder_config_t data;
520} __attribute__ ((packed));
521
522/* SBC decoder configuration structure. */
523typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
524struct sbc_dec_cfg_t {
525 uint32_t dec_format;
526 audio_sbc_decoder_config_t data;
527} __attribute__ ((packed));
528
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530529/* SBC encoder configuration structure. */
530typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
531
532/* supported num_subbands are 4/8
533 * supported blk_len are 4, 8, 12, 16
534 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
535 * supported alloc_method are LOUNDNESS/SNR
536 * supported bit_rate for mono channel is max 320kbps
537 * supported bit rate for stereo channel is max 512 kbps
538 */
539struct sbc_enc_cfg_t{
540 uint32_t enc_format;
541 uint32_t num_subbands;
542 uint32_t blk_len;
543 uint32_t channel_mode;
544 uint32_t alloc_method;
545 uint32_t bit_rate;
546 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530547} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530548
549
550/* supported num_channels are Mono/Stereo
551 * supported channel_mapping for mono is CHANNEL_C
552 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
553 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700554 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530555struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530556{
557 uint32_t enc_format;
558 uint32_t sample_rate;
559 uint16_t num_channels;
560 uint16_t reserved;
561 uint8_t channel_mapping[8];
562 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530563} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530564
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530565struct celt_specific_enc_cfg_t
566{
567 uint32_t bit_rate;
568 uint16_t frame_size;
569 uint16_t complexity;
570 uint16_t prediction_mode;
571 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530572} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530573
574struct celt_enc_cfg_t
575{
576 struct custom_enc_cfg_t custom_cfg;
577 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530578} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700579
580/* sync_mode introduced with APTX V2 libraries
581 * sync mode: 0x0 = stereo sync mode
582 * 0x01 = dual mono sync mode
583 * 0x02 = dual mono with no sync on either L or R codewords
584 */
585struct aptx_v2_enc_cfg_ext_t
586{
587 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530588} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700589
590/* APTX struct for combining custom enc and V2 fields */
591struct aptx_enc_cfg_t
592{
593 struct custom_enc_cfg_t custom_cfg;
594 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530595} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700596
Sharad Sangle95d451b2018-06-19 12:24:20 +0530597/* APTX AD structure */
598struct aptx_ad_enc_cfg_ext_t
599{
600 uint32_t sampling_freq;
601 uint32_t mtu;
602 uint32_t channel_mode;
603 uint32_t min_sink_modeA;
604 uint32_t max_sink_modeA;
605 uint32_t min_sink_modeB;
606 uint32_t max_sink_modeB;
607 uint32_t min_sink_modeC;
608 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530609 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530610} __attribute__ ((packed));
611
612struct aptx_ad_enc_cfg_t
613{
614 struct custom_enc_cfg_t custom_cfg;
615 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
616 struct abr_enc_cfg_t abr_cfg;
617} __attribute__ ((packed));
618
Zhou Song8fccbb62019-03-20 01:08:19 +0800619struct aptx_ad_enc_cfg_ext_r2_t
620{
621 uint32_t sampling_freq;
622 uint32_t mtu;
623 uint32_t channel_mode;
624 uint32_t min_sink_modeA;
625 uint32_t max_sink_modeA;
626 uint32_t min_sink_modeB;
627 uint32_t max_sink_modeB;
628 uint32_t min_sink_modeC;
629 uint32_t max_sink_modeC;
630 uint32_t mode;
631 uint32_t input_mode;
632 uint32_t fade_duration;
633 uint8_t sink_cap[11];
634} __attribute__ ((packed));
635
636struct aptx_ad_enc_cfg_r2_t
637{
638 struct custom_enc_cfg_t custom_cfg;
639 struct aptx_ad_enc_cfg_ext_r2_t aptx_ad_cfg;
640 struct abr_enc_cfg_t abr_cfg;
641} __attribute__ ((packed));
642
Zhou Song12c29502019-03-16 10:37:18 +0800643/* APTX AD SPEECH structure */
644struct aptx_ad_speech_enc_cfg_t
645{
646 struct custom_enc_cfg_t custom_cfg;
647 /* Information to set up IMC between decoder and encoder */
648 struct imc_dec_enc_info imc_info;
649 struct aptx_ad_speech_mode_cfg_t speech_mode;
650} __attribute__ ((packed));
651
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530652struct ldac_specific_enc_cfg_t
653{
654 uint32_t bit_rate;
655 uint16_t channel_mode;
656 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530657} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530658
659struct ldac_enc_cfg_t
660{
661 struct custom_enc_cfg_t custom_cfg;
662 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800663 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530664} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530665
Manisha Agarwald45632b2019-10-17 18:14:28 +0800666/* LC3 structure */
667struct lc3_stream_info_t
668{
669 uint32_t stream_id;
670 uint32_t direction;
671 uint32_t channel_mask_lsw;
672 uint32_t channel_mask_msw;
673} __attribute__ ((packed));
674
675struct lc3_stream_map_info_t
676{
677 uint32_t stream_map_size;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +0530678 struct lc3_stream_info_t streamMap[2];
Manisha Agarwald45632b2019-10-17 18:14:28 +0800679} __attribute__ ((packed));
680
681struct lc3_stream_map_ext_t
682{
683 uint32_t audio_location;
684 uint8_t stream_id;
685 uint8_t direction;
686} __attribute__ ((packed));
687
688struct lc3_config_ext_t
689{
690 uint32_t api_version;
691 uint32_t sampling_freq;
692 uint32_t max_octets_per_frame;
693 uint32_t frame_duration;//7.5msec, 10msec
694 uint32_t bit_depth;
695 uint32_t num_blocks;
696 uint8_t default_q_level;
697 uint8_t vendor_specific[16]; // this indicates LC3/LC3Q. 0 => LC3 1.0 , 1 => LC3 1.1
698 uint32_t mode;
699} __attribute__ ((packed));
700
701struct lc3_dec_cfg_ext_t
702{
703 struct lc3_config_ext_t fromAirConfig;
704 uint32_t decoder_output_channel;
705 uint32_t stream_map_size;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +0530706 struct lc3_stream_map_ext_t streamMapIn[2];
Manisha Agarwald45632b2019-10-17 18:14:28 +0800707} __attribute__ ((packed));
708
709struct lc3_enc_cfg_ext_t
710{
711 struct lc3_config_ext_t toAirConfig;
712 uint32_t stream_map_size;
713 struct lc3_stream_map_ext_t streamMapOut[16];
714} __attribute__ ((packed));
715
716struct lc3_dec_codec_cfg_t
717{
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +0530718 struct lc3_dec_cfg_ext_t from_Air_cfg;
Manisha Agarwald45632b2019-10-17 18:14:28 +0800719 struct lc3_stream_map_info_t streamMapToAir;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +0530720 struct lc3_stream_map_info_t streamMapFromAir;
Manisha Agarwald45632b2019-10-17 18:14:28 +0800721} __attribute__ ((packed));
722
723struct lc3_dec_cfg_t
724{
725 struct abr_dec_cfg_t abr_cfg;
726 struct lc3_dec_codec_cfg_t dec_codec;
727} __attribute__ ((packed));
728
729struct lc3_enc_codec_cfg_t
730{
731 struct lc3_enc_cfg_ext_t to_Air_cfg;
732 struct lc3_stream_map_info_t streamMapToAir;
733} __attribute__ ((packed));
734
735struct lc3_enc_cfg_t
736{
737 uint32_t enc_format;
738 struct imc_dec_enc_info imc_info;
739 struct lc3_enc_codec_cfg_t enc_codec;
740} __attribute__ ((packed));
741
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530742/* In LE BT source code uses system/audio.h for below
743 * structure definition. To avoid multiple definition
744 * compilation error for audiohal in LE , masking structure
745 * definition under "LINUX_ENABLED" which is defined only
746 * in LE
747 */
748#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530749/* TODO: Define the following structures only for O using PLATFORM_VERSION */
750/* Information about BT SBC encoder configuration
751 * This data is used between audio HAL module and
752 * BT IPC library to configure DSP encoder
753 */
754typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530755 uint32_t subband; /* 4, 8 */
756 uint32_t blk_len; /* 4, 8, 12, 16 */
757 uint16_t sampling_rate; /*44.1khz,48khz*/
758 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
759 uint8_t alloc; /*0(Loudness),1(SNR)*/
760 uint8_t min_bitpool; /* 2 */
761 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
762 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530763 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530764} audio_sbc_encoder_config;
765
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530766/* Information about BT APTX encoder configuration
767 * This data is used between audio HAL module and
768 * BT IPC library to configure DSP encoder
769 */
770typedef struct {
771 uint16_t sampling_rate;
772 uint8_t channels;
773 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530774 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700775} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530776
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700777typedef struct {
Zhou Song8fccbb62019-03-20 01:08:19 +0800778 uint32_t sampling_rate;
779 uint32_t mtu;
780 int32_t channel_mode;
781 uint32_t min_sink_modeA;
782 uint32_t max_sink_modeA;
783 uint32_t min_sink_modeB;
784 uint32_t max_sink_modeB;
785 uint32_t min_sink_modeC;
786 uint32_t max_sink_modeC;
787 uint32_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530788 uint8_t TTP_modeA_low;
789 uint8_t TTP_modeA_high;
790 uint8_t TTP_modeB_low;
791 uint8_t TTP_modeB_high;
Zhou Song8fccbb62019-03-20 01:08:19 +0800792 uint8_t TTP_TWS_low;
793 uint8_t TTP_TWS_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530794 uint32_t bits_per_sample;
Zhou Song8fccbb62019-03-20 01:08:19 +0800795 uint32_t input_mode;
796 uint32_t fade_duration;
797 uint8_t sink_cap[11];
Sharad Sangle95d451b2018-06-19 12:24:20 +0530798} audio_aptx_ad_config;
799
800typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700801 uint16_t sampling_rate;
802 uint8_t channels;
803 uint32_t bitrate;
804 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530805 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700806} audio_aptx_dual_mono_config;
807
808typedef union {
809 audio_aptx_default_config *default_cfg;
810 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530811 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700812} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530813
814/* Information about BT AAC encoder configuration
815 * This data is used between audio HAL module and
816 * BT IPC library to configure DSP encoder
817 */
818typedef struct {
819 uint32_t enc_mode; /* LC, SBR, PS */
820 uint16_t format_flag; /* RAW, ADTS */
821 uint16_t channels; /* 1-Mono, 2-Stereo */
822 uint32_t sampling_rate;
823 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530824 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530825} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530826#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700827
828typedef struct {
829 audio_aac_encoder_config audio_aac_enc_cfg;
830 struct aac_frame_size_control_t frame_ctl;
831} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530832
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530833typedef struct {
834 audio_aac_encoder_config audio_aac_enc_cfg;
835 struct aac_frame_size_control_t frame_ctl;
836 uint8_t size_control_struct;
837 struct aac_frame_size_control_t* frame_ptr_ctl;
838} audio_aac_encoder_config_v3;
839
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530840/* Information about BT CELT encoder configuration
841 * This data is used between audio HAL module and
842 * BT IPC library to configure DSP encoder
843 */
844typedef struct {
845 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
846 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
847 uint16_t frame_size; /* 64-128-256-512, 512 */
848 uint16_t complexity; /* 0-10, 1 */
849 uint16_t prediction_mode; /* 0-1-2, 0 */
850 uint16_t vbr_flag; /* 0-1, 0*/
851 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530852 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530853} audio_celt_encoder_config;
854
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530855/* Information about BT LDAC encoder configuration
856 * This data is used between audio HAL module and
857 * BT IPC library to configure DSP encoder
858 */
859typedef struct {
860 uint32_t sampling_rate; /*44100,48000,88200,96000*/
861 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
862 uint16_t channel_mode; /* 0, 4, 2, 1*/
863 uint16_t mtu; /*679*/
Aniket Kumar Latad0196282019-05-09 14:24:17 -0700864 uint32_t bits_per_sample;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800865 bool is_abr_enabled;
866 struct quality_level_to_bitrate_info level_to_bitrate_map;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530867} audio_ldac_encoder_config;
868
Surendar Karkabbb3c822018-11-12 13:00:38 +0530869/* Information about BT AAC decoder configuration
870 * This data is used between audio HAL module and
871 * BT IPC library to configure DSP decoder
872 */
873typedef struct {
874 uint16_t aac_fmt_flag; /* LATM*/
875 uint16_t audio_object_type; /* LC */
876 uint16_t channels; /* Stereo */
877 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
878 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
879 44.1k, 48k, 64k, 88.2k, 96k */
880} audio_aac_dec_config_t;
881
882/* Information about BT SBC decoder configuration
883 * This data is used between audio HAL module and
884 * BT IPC library to configure DSP decoder
885 */
886typedef struct {
887 uint16_t channels; /* Mono, Stereo */
888 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
889 44.1k, 48k, 64k, 88.2k, 96k */
890}audio_sbc_dec_config_t;
891
Manisha Agarwald45632b2019-10-17 18:14:28 +0800892/* Information about BT LC3 encoder configuration
893 * This data is used between audio HAL module and
894 * BT IPC library to configure DSP encoder
895 */
896typedef struct {
897 uint32_t api_version;
898 uint32_t sampling_freq;
899 uint32_t max_octets_per_frame;
900 uint32_t frame_duration;//7.5msec, 10msec
901 uint32_t bit_depth;
902 uint32_t num_blocks;
903 uint8_t default_q_level;
904 uint8_t vendor_specific[16]; // this indicates LC3/LC3Q. 0 => LC3 1.0 , 1 => LC3 1.1
905 uint32_t mode;
906} lc3_config_t;
907
908typedef struct {
909 uint32_t audio_location;
910 uint8_t stream_id;
911 uint8_t direction;
912} lc3_stream_map_t;
913
914typedef struct {
915 lc3_config_t toAirConfig;
916 uint8_t stream_map_size;
917 lc3_stream_map_t* streamMapOut;
918} lc3_encoder_config_t;
919
920typedef struct {
921 lc3_config_t fromAirConfig;
922 uint32_t decoder_output_channel;
923 uint8_t stream_map_size;
924 lc3_stream_map_t* streamMapIn;
925} lc3_decoder_config_t;
926
927typedef struct {
928 lc3_encoder_config_t enc_config;
929 lc3_decoder_config_t dec_config;
930} audio_lc3_codec_config_t;
931
932
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530933/*********** END of DSP configurable structures ********************/
934
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530935static void update_offload_codec_capabilities()
936{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530937
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530938 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800939 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
940 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
941
942 ALOGD("%s: A2DP offload supported = %d",__func__,
943 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530944}
945
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800946static int stop_abr()
947{
948 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800949 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800950 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800951 int ret = 0;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800952
953 /* This function can be used if !abr_started for clean up */
954 ALOGV("%s: enter", __func__);
955
956 // Close hostless front end
957 if (a2dp.abr_config.abr_tx_handle != NULL) {
958 pcm_close(a2dp.abr_config.abr_tx_handle);
959 a2dp.abr_config.abr_tx_handle = NULL;
960 }
Zhou Song12c29502019-03-16 10:37:18 +0800961 if (a2dp.abr_config.abr_rx_handle != NULL) {
962 pcm_close(a2dp.abr_config.abr_rx_handle);
963 a2dp.abr_config.abr_rx_handle = NULL;
964 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800965 a2dp.abr_config.abr_started = false;
966 a2dp.abr_config.imc_instance = 0;
967
968 // Reset BT driver mixer control for ABR usecase
969 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
970 MIXER_SET_FEEDBACK_CHANNEL);
971 if (!ctl_set_bt_feedback_channel) {
972 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800973 ret = -ENOSYS;
974 } else if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800975 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800976 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800977 }
978
979 // Reset ABR Tx feedback path
980 ALOGV("%s: Disable ABR Tx feedback path", __func__);
981 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
982 MIXER_ABR_TX_FEEDBACK_PATH);
983 if (!ctl_abr_tx_path) {
984 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800985 ret = -ENOSYS;
986 } else if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800987 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800988 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800989 }
990
Zhou Song12c29502019-03-16 10:37:18 +0800991 // Reset ABR Rx feedback path
992 ALOGV("%s: Disable ABR Rx feedback path", __func__);
993 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
994 MIXER_ABR_RX_FEEDBACK_PATH);
995 if (!ctl_abr_rx_path) {
996 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
997 ret = -ENOSYS;
998 } else if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 0) != 0) {
999 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
1000 ret = -ENOSYS;
1001 }
1002
1003 return ret;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001004}
1005
1006static int start_abr()
1007{
1008 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001009 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001010 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
1011 int abr_device_id;
1012 int ret = 0;
1013
1014 if (!a2dp.abr_config.is_abr_enabled) {
1015 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
1016 return -ENOSYS;
1017 }
1018
1019 if (a2dp.abr_config.abr_started) {
1020 ALOGI("%s: ABR has already started", __func__);
1021 return ret;
1022 }
1023
1024 // Enable Slimbus 7 Tx feedback path
1025 ALOGV("%s: Enable ABR Tx feedback path", __func__);
1026 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
1027 MIXER_ABR_TX_FEEDBACK_PATH);
1028 if (!ctl_abr_tx_path) {
1029 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
1030 return -ENOSYS;
1031 }
1032 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
1033 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
1034 return -ENOSYS;
1035 }
1036
1037 // Notify ABR usecase information to BT driver to distinguish
1038 // between SCO and feedback usecase
1039 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
1040 MIXER_SET_FEEDBACK_CHANNEL);
1041 if (!ctl_set_bt_feedback_channel) {
1042 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +08001043 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001044 }
1045 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
1046 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +08001047 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001048 }
1049
1050 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001051 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001052 PCM_CAPTURE);
1053 if (!a2dp.abr_config.abr_tx_handle) {
1054 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
1055 abr_device_id, PCM_IN,
1056 &pcm_config_abr);
Zhou Song12c29502019-03-16 10:37:18 +08001057 if (a2dp.abr_config.abr_tx_handle == NULL) {
1058 ALOGE("%s: Can't open abr tx device", __func__);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001059 goto fail;
Zhou Song12c29502019-03-16 10:37:18 +08001060 }
1061 if (!(pcm_is_ready(a2dp.abr_config.abr_tx_handle) &&
1062 !pcm_start(a2dp.abr_config.abr_tx_handle))) {
1063 ALOGE("%s: tx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
1064 goto fail;
1065 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001066 }
Zhou Song12c29502019-03-16 10:37:18 +08001067
1068 // Enable Slimbus 7 Rx feedback path for HD Voice use case
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301069 if ((a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH)
1070 || ((a2dp.a2dp_sink_started == true) && (a2dp.bt_decoder_format == CODEC_TYPE_LC3))) {
Zhou Song12c29502019-03-16 10:37:18 +08001071 ALOGV("%s: Enable ABR Rx feedback path", __func__);
1072 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
1073 MIXER_ABR_RX_FEEDBACK_PATH);
1074 if (!ctl_abr_rx_path) {
1075 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
1076 goto fail;
1077 }
1078 if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 1) != 0) {
1079 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
1080 goto fail;
1081 }
1082
1083 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
1084 ALOGE("%s: Failed to set BT usecase", __func__);
1085 goto fail;
1086 }
1087
1088 // Open hostless front end and prepare ABR Rx path
1089 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
1090 PCM_PLAYBACK);
1091 if (!a2dp.abr_config.abr_rx_handle) {
1092 a2dp.abr_config.abr_rx_handle = pcm_open(a2dp.adev->snd_card,
1093 abr_device_id, PCM_OUT,
1094 &pcm_config_abr);
1095 if (a2dp.abr_config.abr_rx_handle == NULL) {
1096 ALOGE("%s: Can't open abr rx device", __func__);
1097 goto fail;
1098 }
1099 if (!(pcm_is_ready(a2dp.abr_config.abr_rx_handle) &&
1100 !pcm_start(a2dp.abr_config.abr_rx_handle))) {
1101 ALOGE("%s: rx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_rx_handle));
1102 goto fail;
1103 }
1104 }
1105 }
1106
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001107 a2dp.abr_config.abr_started = true;
1108
1109 return ret;
1110
1111fail:
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001112 stop_abr();
1113 return -ENOSYS;
1114}
1115
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001116static int check_if_enhanced_fwk() {
1117
1118 int is_enhanced_fwk = 1;
1119 //dlopen lib
1120 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
1121 if (vndk_fwk_lib_handle != NULL) {
1122 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
1123 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
1124 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
1125 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
1126 __func__);
1127 is_enhanced_fwk = 1;
1128 } else {
1129 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
1130 }
1131 }
1132 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
1133 return is_enhanced_fwk;
1134}
1135
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301136static void open_a2dp_source() {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301137 int ret = 0;
1138
Florian Pfister1a84f312018-07-19 14:38:18 +02001139 ALOGD(" Open A2DP source start ");
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301140
1141 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
1142 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
1143 ALOGD("calling BT stream open");
1144 ret = a2dp.audio_source_open();
1145 if(ret != 0) {
1146 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Srinu Jellada99a592019-01-25 16:50:52 +05301147 }
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301148 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301149 } else {
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301150 ALOGD("Called a2dp open with improper state %d", a2dp.bt_state_source);
1151 }
1152 } else {
1153 ALOGE("a2dp handle is not identified, Ignoring open request");
1154 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
1155 }
1156}
1157/* API to open BT IPC library to start IPC communication for BT Source*/
1158static void a2dp_source_init()
1159{
1160 ALOGD("a2dp_source_init START");
1161 if (a2dp.bt_lib_source_handle == NULL) {
1162 ALOGD("Requesting for BT lib handle");
1163 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
1164 if (a2dp.bt_lib_source_handle == NULL) {
1165 ALOGE("%s: dlopen failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
1166 return;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301167 }
1168 }
1169
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301170 a2dp.bt_audio_pre_init = (bt_audio_pre_init_t)
1171 dlsym(a2dp.bt_lib_source_handle, "bt_audio_pre_init");
Srinu Jellada99a592019-01-25 16:50:52 +05301172 a2dp.audio_source_open = (audio_source_open_t)
1173 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
1174 a2dp.audio_source_start = (audio_source_start_t)
1175 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
sriram kumarcd549b02021-02-12 14:27:17 +05301176 if (a2dp.audio_source_start == NULL) {
1177 a2dp.audio_source_start = (audio_source_start_t)
1178 dlsym(a2dp.bt_lib_source_handle, "audio_stream_start");
1179 }
Srinu Jellada99a592019-01-25 16:50:52 +05301180 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
1181 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
1182 a2dp.audio_source_suspend = (audio_source_suspend_t)
1183 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
sriram kumarcd549b02021-02-12 14:27:17 +05301184 if (a2dp.audio_source_suspend == NULL) {
1185 a2dp.audio_source_suspend = (audio_source_suspend_t)
1186 dlsym(a2dp.bt_lib_source_handle, "audio_stream_suspend");
1187 }
Srinu Jellada99a592019-01-25 16:50:52 +05301188 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
1189 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
1190 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1191 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
sriram kumarcd549b02021-02-12 14:27:17 +05301192 if (a2dp.clear_source_a2dpsuspend_flag == NULL) {
1193 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1194 dlsym(a2dp.bt_lib_source_handle, "clear_a2dp_suspend_flag");
1195 }
Srinu Jellada99a592019-01-25 16:50:52 +05301196 a2dp.audio_source_stop = (audio_source_stop_t)
1197 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
sriram kumarcd549b02021-02-12 14:27:17 +05301198 if (a2dp.audio_source_stop == NULL) {
1199 a2dp.audio_source_stop = (audio_source_stop_t)
1200 dlsym(a2dp.bt_lib_source_handle, "audio_stream_stop");
1201 }
Srinu Jellada99a592019-01-25 16:50:52 +05301202 a2dp.audio_source_close = (audio_source_close_t)
1203 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
1204 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
1205 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
1206 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
1207 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
1208 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1209 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1210 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1211 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1212
Srinu Jella793f5302019-07-19 15:53:55 +05301213 if (a2dp.bt_lib_source_handle && a2dp.bt_audio_pre_init) {
1214 ALOGD("calling BT module preinit");
1215 // fwk related check's will be done in the BT layer
1216 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001217 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001218}
1219
1220/* API to open BT IPC library to start IPC communication for BT Sink*/
1221static void open_a2dp_sink()
1222{
1223 ALOGD(" Open A2DP input start ");
1224 if (a2dp.bt_lib_sink_handle == NULL){
1225 ALOGD(" Requesting for BT lib handle");
1226 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1227
1228 if (a2dp.bt_lib_sink_handle == NULL) {
1229 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1230 } else {
1231 a2dp.audio_sink_start = (audio_sink_start_t)
1232 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1233 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1234 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1235 a2dp.audio_sink_stop = (audio_sink_stop_t)
1236 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1237 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1238 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1239 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1240 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1241 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301242 }
1243}
1244
1245static int close_a2dp_output()
1246{
1247 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001248
1249 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1250 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301251 return -ENOSYS;
1252 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001253
1254 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1255 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001256 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001257 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301258 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001259 a2dp.a2dp_source_started = false;
1260 a2dp.a2dp_source_total_active_session_requests = 0;
1261 a2dp.a2dp_source_suspended = false;
1262 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301263 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301264 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001265 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001266 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1267 stop_abr();
1268 a2dp.abr_config.is_abr_enabled = false;
1269 a2dp.abr_config.abr_started = false;
1270 a2dp.abr_config.imc_instance = 0;
1271 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001272 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301273 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001274
1275 return 0;
1276}
1277
1278static int close_a2dp_input()
1279{
1280 ALOGV("%s\n",__func__);
1281
1282 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1283 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1284 return -ENOSYS;
1285 }
1286
1287 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1288 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001289 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001290 ALOGE("failed close a2dp sink control path from BT library");
1291 }
1292 a2dp.a2dp_sink_started = false;
1293 a2dp.a2dp_sink_total_active_session_requests = 0;
1294 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1295 a2dp.dec_sampling_rate = 48000;
1296 a2dp.dec_channels = 2;
1297 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301298
1299 return 0;
1300}
1301
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301302static void a2dp_check_and_set_scrambler()
1303{
1304 bool scrambler_mode = false;
1305 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001306 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1307 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301308
1309 if (scrambler_mode) {
1310 //enable scrambler in dsp
1311 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1312 MIXER_SCRAMBLER_MODE);
1313 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001314 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301315 return;
1316 } else {
1317 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1318 ALOGE("%s: Could not set scrambler mode", __func__);
1319 return;
1320 }
1321 }
1322 }
1323}
1324
Florian Pfister1a84f312018-07-19 14:38:18 +02001325static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301326{
Florian Pfister1a84f312018-07-19 14:38:18 +02001327 char *rate_str = NULL, *channels = NULL;
1328 uint32_t sampling_rate;
1329 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
Zhou Songab94f062021-05-12 13:11:17 +08001330 struct mixer_ctl *adm_ctrl_channels = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001331 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301332
Florian Pfister1a84f312018-07-19 14:38:18 +02001333 if (direction == SINK) {
1334 sampling_rate = a2dp.dec_sampling_rate;
1335 } else {
1336 sampling_rate = a2dp.enc_sampling_rate;
1337 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001338 /*
1339 * For LDAC encoder and AAC decoder open slimbus port at
1340 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1341 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1342 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001343 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301344 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001345 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1346 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301347 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1348 sampling_rate = sampling_rate *2;
1349 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001350
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301351 if (a2dp.bt_encoder_format == CODEC_TYPE_LC3 ||
1352 a2dp.bt_decoder_format == CODEC_TYPE_LC3)
Manisha Agarwald45632b2019-10-17 18:14:28 +08001353 sampling_rate = SAMPLING_RATE_96K;
1354
Aalique Grahame22e49102018-12-18 14:23:57 -08001355 // No need to configure backend for PCM format.
1356 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1357 return 0;
1358 }
1359
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301360 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301361 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301362 case 44100:
1363 rate_str = "KHZ_44P1";
1364 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301365 case 88200:
1366 rate_str = "KHZ_88P2";
1367 break;
1368 case 96000:
1369 rate_str = "KHZ_96";
1370 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001371 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301372 default:
1373 rate_str = "KHZ_48";
1374 break;
1375 }
1376
Florian Pfister1a84f312018-07-19 14:38:18 +02001377 if (direction == SINK) {
1378 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301379 if (a2dp.bt_decoder_format == CODEC_TYPE_LC3)
1380 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1381 MIXER_SOURCE_SAMPLE_RATE_TX);
1382 else
1383 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1384 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001385 } else {
1386 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1387 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301388 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001389 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301390 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001391
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301392 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1393 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301394 is_configured = false;
1395 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301396 }
1397
Surendar Karka2febd452018-12-13 17:56:43 +05301398 if (direction == SOURCE) {
1399 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001400 if (a2dp.abr_config.is_abr_enabled) {
Manisha Agarwald45632b2019-10-17 18:14:28 +08001401 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH ||
1402 a2dp.bt_encoder_format == CODEC_TYPE_LC3)
Zhou Song12c29502019-03-16 10:37:18 +08001403 rate_str = SPEECH_TX_SAMPLE_RATE;
1404 else
1405 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301406
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301407 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1408 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1409 MIXER_SOURCE_SAMPLE_RATE_TX);
1410 if (!ctl_sample_rate) {
1411 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1412 is_configured = false;
1413 goto fail;
1414 }
1415
1416 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1417 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1418 is_configured = false;
1419 goto fail;
1420 }
Surendar Karka2febd452018-12-13 17:56:43 +05301421 }
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301422 } else {
1423 /* LC3 needs to set RX sample rate as well */
1424 if (a2dp.bt_decoder_format == CODEC_TYPE_LC3) {
1425 ALOGD("%s: set rx backend sample rate =%s", __func__, rate_str);
1426 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1427 MIXER_SAMPLE_RATE_RX);
1428 if (!ctl_sample_rate) {
1429 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1430 is_configured = false;
1431 goto fail;
1432 }
1433
1434 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1435 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1436 is_configured = false;
1437 goto fail;
1438 }
1439 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301440 }
1441 } else {
1442 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1443 MIXER_SAMPLE_RATE_TX is not supported */
1444 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1445 MIXER_SAMPLE_RATE_DEFAULT);
1446 if (!ctl_sample_rate) {
1447 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301448 is_configured = false;
1449 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301450 }
1451
1452 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1453 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001454 is_configured = false;
1455 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301456 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301457 }
1458
Florian Pfister1a84f312018-07-19 14:38:18 +02001459 if (direction == SINK) {
1460 switch (a2dp.dec_channels) {
1461 case 1:
1462 channels = "One";
1463 break;
1464 case 2:
1465 default:
1466 channels = "Two";
1467 break;
1468 }
1469
Ramu Gottipati02809682018-12-19 16:46:12 +05301470 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Zhou Songab94f062021-05-12 13:11:17 +08001471 if (a2dp.bt_decoder_format == CODEC_TYPE_LC3) {
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301472 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1473 MIXER_AFE_SINK_CHANNELS_SLIM7);
Zhou Songab94f062021-05-12 13:11:17 +08001474 adm_ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1475 MIXER_SLIM7_TX_ADM_CHANNEL);
1476 } else {
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301477 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1478 MIXER_AFE_SINK_CHANNELS);
Zhou Songab94f062021-05-12 13:11:17 +08001479 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001480 } else {
1481 //Configure AFE enc channels
1482 switch (a2dp.enc_channels) {
1483 case 1:
1484 channels = "One";
1485 break;
1486 case 2:
1487 default:
1488 channels = "Two";
1489 break;
1490 }
1491
Ramu Gottipati02809682018-12-19 16:46:12 +05301492 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001493 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1494 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301495 }
1496
Florian Pfister1a84f312018-07-19 14:38:18 +02001497 if (!ctrl_channels) {
1498 ALOGE(" ERROR AFE channels mixer control not identified");
1499 } else {
1500 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301501 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001502 is_configured = false;
1503 goto fail;
1504 }
1505 }
Zhou Songab94f062021-05-12 13:11:17 +08001506 // need to set adm channel for LC3 decoder
Zhou Song8d7ba052021-05-17 00:40:35 +08001507 if (a2dp.bt_decoder_format == CODEC_TYPE_LC3 &&
1508 direction == SINK) {
Zhou Songab94f062021-05-12 13:11:17 +08001509 if (!adm_ctrl_channels) {
1510 ALOGE(" ERROR ADM channels mixer control not identified");
1511 } else {
1512 if (mixer_ctl_set_enum_by_string(adm_ctrl_channels, channels) != 0) {
1513 ALOGE("%s: Failed to set ADM channels =%s", __func__, channels);
1514 is_configured = false;
1515 goto fail;
1516 }
1517 }
1518 }
1519
Florian Pfister1a84f312018-07-19 14:38:18 +02001520 is_configured = true;
1521fail:
1522 return is_configured;
1523}
1524
Aniket Kumar Lata990de552019-07-11 14:20:23 -07001525bool a2dp_set_source_backend_cfg()
1526{
1527 if (a2dp.a2dp_source_started && !a2dp.a2dp_source_suspended)
1528 return a2dp_set_backend_cfg(SOURCE);
1529
1530 return false;
1531}
1532
Surendar Karkabbb3c822018-11-12 13:00:38 +05301533bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001534{
1535 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1536 struct aac_dec_cfg_t aac_dsp_cfg;
1537 bool is_configured = false;
1538 int ret = 0;
1539
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001540 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001541 return false;
1542
Surendar Karka2febd452018-12-13 17:56:43 +05301543 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001544 if (!ctl_dec_data) {
1545 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1546 is_configured = false;
1547 goto fail;
1548 }
1549
1550 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1551 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1552 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1553 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1554 switch(aac_bt_cfg->audio_object_type) {
1555 case 0:
1556 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301557 break;
1558 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001559 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1560 break;
1561 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301562 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001563 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301564 break;
1565 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001566 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1567 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1568 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1569 sizeof(struct aac_dec_cfg_t));
1570 if (ret != 0) {
1571 ALOGE("%s: failed to set AAC decoder config", __func__);
1572 is_configured = false;
1573 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001574 }
1575
Florian Pfister1a84f312018-07-19 14:38:18 +02001576 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1577 MIXER_DEC_BIT_FORMAT);
1578 if (!ctrl_bit_format) {
1579 ALOGE(" ERROR Dec bit format mixer control not identified");
1580 is_configured = false;
1581 goto fail;
1582 }
1583 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1584 if (ret != 0) {
1585 ALOGE("%s: Failed to set bit format to decoder", __func__);
1586 is_configured = false;
1587 goto fail;
1588 }
1589
1590 is_configured = true;
1591 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1592 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1593 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1594 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1595 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1596fail:
1597 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301598}
1599
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301600static int a2dp_set_bit_format(uint32_t enc_bit_format)
1601{
1602 const char *bit_format = NULL;
1603 struct mixer_ctl *ctrl_bit_format = NULL;
1604
1605 // Configure AFE Input Bit Format
1606 switch (enc_bit_format) {
1607 case 32:
1608 bit_format = "S32_LE";
1609 break;
1610 case 24:
1611 bit_format = "S24_LE";
1612 break;
1613 case 16:
1614 default:
1615 bit_format = "S16_LE";
1616 break;
1617 }
1618
1619 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1620 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1621 MIXER_ENC_BIT_FORMAT);
1622 if (!ctrl_bit_format) {
1623 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1624 return -ENOSYS;
1625 }
1626 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1627 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1628 return -ENOSYS;
1629 }
1630 return 0;
1631}
1632
Surendar Karka2febd452018-12-13 17:56:43 +05301633static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301634{
Surendar Karka2febd452018-12-13 17:56:43 +05301635 const char *rate_str = "KHZ_8", *channels = "Zero";
1636 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
Zhou Songab94f062021-05-12 13:11:17 +08001637 struct mixer_ctl *ctrl_channels = NULL, *adm_ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301638
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001639 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301640 if (direction == SINK) {
1641 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1642 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1643 MIXER_SINK_SAMPLE_RATE);
1644 } else {
1645 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1646 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1647 MIXER_SAMPLE_RATE_RX);
1648 }
1649 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301650
Surendar Karka2febd452018-12-13 17:56:43 +05301651 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1652 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301653 return -ENOSYS;
1654 }
Zhou Song12c29502019-03-16 10:37:18 +08001655 if (a2dp.abr_config.is_abr_enabled) {
1656 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1657 MIXER_SOURCE_SAMPLE_RATE_TX);
1658 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301659 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1660 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001661 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301662
Zhou Song12c29502019-03-16 10:37:18 +08001663 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1664 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1665 return -ENOSYS;
1666 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301667 }
1668 } else {
Surendar Karka2febd452018-12-13 17:56:43 +05301669 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301670 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301671 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301672 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1673 return -ENOSYS;
1674 }
1675
Surendar Karka2febd452018-12-13 17:56:43 +05301676 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301677 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1678 return -ENOSYS;
1679 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001680 }
1681
1682 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301683 if (direction == SINK) {
1684 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1685 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1686 MIXER_AFE_SINK_CHANNELS);
1687 } else {
1688 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1689 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1690 MIXER_AFE_IN_CHANNELS);
1691 }
1692 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001693 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1694 return -ENOSYS;
1695 }
Surendar Karka2febd452018-12-13 17:56:43 +05301696 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001697 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1698 return -ENOSYS;
1699 }
Zhou Songab94f062021-05-12 13:11:17 +08001700 // Reset adm channels for slim7 tx if available
1701 adm_ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1702 MIXER_SLIM7_TX_ADM_CHANNEL);
1703 if (adm_ctrl_channels) {
1704 if (mixer_ctl_set_enum_by_string(adm_ctrl_channels, channels) != 0)
1705 ALOGE("%s: Failed to reset ADM in channels = %d", __func__, a2dp.dec_channels);
1706 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001707
1708 return 0;
1709}
1710
1711/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301712static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001713{
1714 struct mixer_ctl *ctl_dec_data = NULL;
1715 struct abr_dec_cfg_t dec_cfg;
1716 int ret = 0;
1717
1718 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301719 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001720 if (!ctl_dec_data) {
1721 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1722 return false;
1723 }
1724 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1725 dec_cfg.dec_format = dec_format;
1726 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1727 dec_cfg.imc_info.enable = IMC_ENABLE;
1728 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1729 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1730
1731 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1732 sizeof(dec_cfg));
1733 if (ret != 0) {
1734 ALOGE("%s: Failed to set decoder config", __func__);
1735 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301736 }
Surendar Karka2febd452018-12-13 17:56:43 +05301737 }
1738
1739 return true;
1740}
1741
Surendar Karkabbb3c822018-11-12 13:00:38 +05301742bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001743{
1744 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1745 struct sbc_dec_cfg_t sbc_dsp_cfg;
1746 bool is_configured = false;
1747 int ret = 0;
1748
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001749 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001750 goto fail;
1751
Surendar Karka2febd452018-12-13 17:56:43 +05301752 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001753 if (!ctl_dec_data) {
1754 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1755 is_configured = false;
1756 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301757 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001758
Florian Pfister1a84f312018-07-19 14:38:18 +02001759 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1760 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1761 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1762 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1763 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1764 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301765
Florian Pfister1a84f312018-07-19 14:38:18 +02001766 if (ret != 0) {
1767 ALOGE("%s: failed to set SBC decoder config", __func__);
1768 is_configured = false;
1769 goto fail;
1770 }
1771
1772 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1773 MIXER_DEC_BIT_FORMAT);
1774 if (!ctrl_bit_format) {
1775 ALOGE(" ERROR Dec bit format mixer control not identified");
1776 is_configured = false;
1777 goto fail;
1778 }
1779 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1780 if (ret != 0) {
1781 ALOGE("%s: Failed to set bit format to decoder", __func__);
1782 is_configured = false;
1783 goto fail;
1784 }
1785
1786 is_configured = true;
1787 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1788 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1789 a2dp.dec_channels = 1;
1790 else
1791 a2dp.dec_channels = 2;
1792 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1793 ALOGV("Successfully updated SBC dec format");
1794fail:
1795 return is_configured;
1796}
1797
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301798uint64_t convert_channel_map(uint32_t audio_location)
1799{
1800 int i;
1801 uint64_t channel_mask = (uint64_t) 0x00000000;
1802
1803 if (!audio_location) {
1804 channel_mask |= 1ULL << PCM_CHANNEL_C;
1805 return channel_mask;
1806 }
1807
1808 for (i = 0; i < AUDIO_LOCATION_MAX; i++) {
1809 if (audio_location & audio_location_map_array[i])
1810 channel_mask |= 1ULL << channel_map_array[i];
1811 }
1812
1813 return channel_mask;
1814}
1815
1816bool configure_lc3_enc_format(audio_lc3_codec_config_t *lc3_bt_cfg) {
1817 struct mixer_ctl *ctl_enc_data = NULL;
1818 int mixer_size = 0;
1819 int ret = 0;
1820 int i;
1821 bool is_configured = false;
1822 struct lc3_enc_cfg_t lc3_dsp_cfg;
1823 uint64_t channel_mask;
1824
1825 if (lc3_bt_cfg == NULL)
1826 return false;
1827
1828 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1829 if (!ctl_enc_data) {
1830 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
1831 return false;
1832 }
1833
1834 /* Initialize dsp configuration params */
1835 memset(&lc3_dsp_cfg, 0x0, sizeof(struct lc3_enc_cfg_t));
1836
1837 lc3_dsp_cfg.enc_format = MEDIA_FMT_LC3;
1838 //encoder structure
1839
1840 if (lc3_bt_cfg->enc_config.stream_map_size != 0) {
1841 if (!lc3_bt_cfg->enc_config.streamMapOut[0].audio_location)
1842 a2dp.enc_channels = CH_MONO;
1843 else
1844 a2dp.enc_channels = CH_STEREO;
1845 }
1846
1847 lc3_dsp_cfg.imc_info.direction = IMC_RECEIVE;
1848 lc3_dsp_cfg.imc_info.enable = IMC_ENABLE;
1849 lc3_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1850 lc3_dsp_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1851 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.api_version = lc3_bt_cfg->enc_config.toAirConfig.api_version;
1852 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.sampling_freq = lc3_bt_cfg->enc_config.toAirConfig.sampling_freq;
1853 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.max_octets_per_frame = lc3_bt_cfg->enc_config.toAirConfig.max_octets_per_frame;
1854 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.frame_duration = lc3_bt_cfg->enc_config.toAirConfig.frame_duration;
1855 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.bit_depth = lc3_bt_cfg->enc_config.toAirConfig.bit_depth;
1856 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.num_blocks = lc3_bt_cfg->enc_config.toAirConfig.num_blocks;
1857 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.default_q_level = lc3_bt_cfg->enc_config.toAirConfig.default_q_level;
1858 for (i = 0; i < 16; i++)
1859 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.vendor_specific[i] = lc3_bt_cfg->enc_config.toAirConfig.vendor_specific[i];
1860 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.mode = lc3_bt_cfg->enc_config.toAirConfig.mode;
1861 lc3_dsp_cfg.enc_codec.to_Air_cfg.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
1862 lc3_dsp_cfg.enc_codec.streamMapToAir.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
1863
1864 for (i = 0; i < lc3_dsp_cfg.enc_codec.to_Air_cfg.stream_map_size; i++) {
1865 // for encoder stream map info
1866 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].audio_location = lc3_bt_cfg->enc_config.streamMapOut[i].audio_location;
1867 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
1868 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
1869 // for packetizer stream map info
1870 channel_mask = convert_channel_map(lc3_bt_cfg->enc_config.streamMapOut[i].audio_location);
1871 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
1872 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
1873 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].channel_mask_lsw = channel_mask & 0x00000000FFFFFFFF;
1874 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].channel_mask_msw = (channel_mask & 0xFFFFFFFF00000000) >> 32;
1875 }
1876
1877 /* Configure AFE DSP configuration */
1878 mixer_size = sizeof(struct lc3_enc_cfg_t);
1879 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&lc3_dsp_cfg,
1880 mixer_size);
1881 if (ret != 0) {
1882 ALOGE("%s: Failed to set lc3 encoder config", __func__);
1883 is_configured = false;
1884 goto fail;
1885 }
1886
1887 ret = a2dp_set_bit_format(ENCODER_BIT_FORMAT_PCM_24);
1888 if (ret != 0) {
1889 ALOGE("%s: Failed to set lc3 bit format", __func__);
1890 is_configured = false;
1891 goto fail;
1892 }
1893
1894 is_configured = true;
1895 a2dp.bt_encoder_format = CODEC_TYPE_LC3;
1896
1897fail:
1898 return is_configured;
1899}
1900
1901bool configure_lc3_dec_format(audio_lc3_codec_config_t *lc3_bt_cfg)
1902{
1903 struct mixer_ctl *ctl_dec_data = NULL;
1904 struct lc3_dec_cfg_t lc3_dsp_cfg;
1905 uint64_t channel_mask;
1906 bool is_configured = false;
1907 int ret = 0;
1908 int i;
1909
1910 if (lc3_bt_cfg == NULL)
1911 return false;
1912
1913 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
1914 if (!ctl_dec_data) {
1915 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1916 return false;
1917 }
1918
1919 memset(&lc3_dsp_cfg, 0x0, sizeof(struct lc3_dec_cfg_t));
1920
1921 lc3_dsp_cfg.abr_cfg.dec_format = MEDIA_FMT_LC3;
1922 lc3_dsp_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
1923 lc3_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
1924 lc3_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1925
1926 lc3_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1927
1928 /* To check whether decoder configuration is present or not*/
1929 if (lc3_bt_cfg->dec_config.stream_map_size != 0) {
1930 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.api_version = lc3_bt_cfg->dec_config.fromAirConfig.api_version;
1931 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.sampling_freq = lc3_bt_cfg->dec_config.fromAirConfig.sampling_freq;
1932 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.max_octets_per_frame = lc3_bt_cfg->dec_config.fromAirConfig.max_octets_per_frame;
1933 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.frame_duration = lc3_bt_cfg->dec_config.fromAirConfig.frame_duration;
1934 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.bit_depth = lc3_bt_cfg->dec_config.fromAirConfig.bit_depth;
1935 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.num_blocks = lc3_bt_cfg->dec_config.fromAirConfig.num_blocks;
1936 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.default_q_level = lc3_bt_cfg->dec_config.fromAirConfig.default_q_level;
1937 for (i = 0; i < 16; i++)
1938 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.vendor_specific[i] = lc3_bt_cfg->dec_config.fromAirConfig.vendor_specific[i];
1939 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.mode = lc3_bt_cfg->dec_config.fromAirConfig.mode;
1940 lc3_dsp_cfg.dec_codec.from_Air_cfg.decoder_output_channel = lc3_bt_cfg->dec_config.decoder_output_channel;
1941 lc3_dsp_cfg.dec_codec.from_Air_cfg.stream_map_size = lc3_bt_cfg->dec_config.stream_map_size;
1942 lc3_dsp_cfg.dec_codec.streamMapFromAir.stream_map_size = lc3_bt_cfg->dec_config.stream_map_size;
1943
1944 //for depacketizer both fromAir and toAir streamMap needs to be sent.
1945 for (i = 0; i < lc3_dsp_cfg.dec_codec.from_Air_cfg.stream_map_size; i++) {
1946 // for decoder stream map info
1947 lc3_dsp_cfg.dec_codec.from_Air_cfg.streamMapIn[i].audio_location = lc3_bt_cfg->dec_config.streamMapIn[i].audio_location;
1948 lc3_dsp_cfg.dec_codec.from_Air_cfg.streamMapIn[i].stream_id = lc3_bt_cfg->dec_config.streamMapIn[i].stream_id;
1949 lc3_dsp_cfg.dec_codec.from_Air_cfg.streamMapIn[i].direction = lc3_bt_cfg->dec_config.streamMapIn[i].direction;
1950 // for depacketizer stream map fromAir info
1951 channel_mask = convert_channel_map(lc3_bt_cfg->dec_config.streamMapIn[i].audio_location);
1952 lc3_dsp_cfg.dec_codec.streamMapFromAir.streamMap[i].stream_id = lc3_bt_cfg->dec_config.streamMapIn[i].stream_id;
1953 lc3_dsp_cfg.dec_codec.streamMapFromAir.streamMap[i].direction = lc3_bt_cfg->dec_config.streamMapIn[i].direction;
1954 lc3_dsp_cfg.dec_codec.streamMapFromAir.streamMap[i].channel_mask_lsw = channel_mask & 0x00000000FFFFFFFF;
1955 lc3_dsp_cfg.dec_codec.streamMapFromAir.streamMap[i].channel_mask_msw = (channel_mask & 0xFFFFFFFF00000000) >> 32;
1956 }
1957 }
1958
1959 lc3_dsp_cfg.dec_codec.streamMapToAir.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
1960
1961 for (i = 0; i < lc3_bt_cfg->enc_config.stream_map_size; i++) {
1962 // for depacketizer stream map toAir info
1963 channel_mask = convert_channel_map(lc3_bt_cfg->enc_config.streamMapOut[i].audio_location);
1964 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
1965 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
1966 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].channel_mask_lsw = channel_mask & 0x00000000FFFFFFFF;
1967 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].channel_mask_msw = (channel_mask & 0xFFFFFFFF00000000) >> 32;
1968 }
1969
1970 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&lc3_dsp_cfg,
1971 sizeof(struct lc3_dec_cfg_t));
1972 if (ret != 0) {
1973 ALOGE("%s: failed to set LC3 decoder config", __func__);
1974 is_configured = false;
1975 goto fail;
1976 }
1977
1978 is_configured = true;
1979 a2dp.bt_decoder_format = CODEC_TYPE_LC3;
1980 a2dp.dec_channels = CH_STEREO;
Zhou Songab94f062021-05-12 13:11:17 +08001981 a2dp.dec_sampling_rate =
1982 lc3_dsp_cfg.dec_codec.from_Air_cfg.fromAirConfig.sampling_freq;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301983
1984fail:
1985 return is_configured;
1986}
1987
Florian Pfister1a84f312018-07-19 14:38:18 +02001988/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301989static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301990{
Florian Pfister1a84f312018-07-19 14:38:18 +02001991 void *codec_info = NULL;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301992 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02001993 codec_t codec_type = CODEC_TYPE_INVALID;
1994 bool is_configured = false;
1995 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301996
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05301997 if (a2dp.audio_get_dec_config) {
1998 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
1999 if (!ctl_dec_data) {
2000 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
2001 is_configured = false;
2002 return false;
2003 }
2004 codec_info = a2dp.audio_get_dec_config(&codec_type);
2005 } else if (a2dp.audio_get_enc_config) {
2006 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev, &codec_type);
2007 } else {
Florian Pfister1a84f312018-07-19 14:38:18 +02002008 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
2009 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302010 }
2011
Florian Pfister1a84f312018-07-19 14:38:18 +02002012 switch(codec_type) {
2013 case CODEC_TYPE_SBC:
2014 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05302015 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02002016 break;
2017 case CODEC_TYPE_AAC:
2018 ALOGD(" AAC decoder supported BT device");
2019 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05302020 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02002021 break;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05302022 case CODEC_TYPE_LC3:
2023 ALOGD(" LC3 decoder supported BT device");
2024 is_configured =
2025 (configure_lc3_enc_format((audio_lc3_codec_config_t *)codec_info) &&
2026 configure_lc3_dec_format((audio_lc3_codec_config_t *)codec_info));
2027 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002028 default:
2029 ALOGD(" Received Unsupported decoder format");
2030 is_configured = false;
2031 break;
2032 }
2033 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302034}
2035
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302036/* API to configure SBC DSP encoder */
2037bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
2038{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302039 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302040 struct sbc_enc_cfg_t sbc_dsp_cfg;
2041 bool is_configured = false;
2042 int ret = 0;
2043
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002044 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302045 return false;
2046
2047 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2048 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002049 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302050 is_configured = false;
2051 goto fail;
2052 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302053 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002054 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302055 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
2056 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
2057 switch(sbc_bt_cfg->channels) {
2058 case 0:
2059 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
2060 break;
2061 case 1:
2062 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
2063 break;
2064 case 3:
2065 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
2066 break;
2067 case 2:
2068 default:
2069 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
2070 break;
2071 }
2072 if (sbc_bt_cfg->alloc)
2073 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
2074 else
2075 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
2076 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
2077 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
2078 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
2079 sizeof(struct sbc_enc_cfg_t));
2080 if (ret != 0) {
2081 ALOGE("%s: failed to set SBC encoder config", __func__);
2082 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302083 goto fail;
2084 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302085 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302086 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302087 is_configured = false;
2088 goto fail;
2089 }
2090 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002091 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302092 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302093
2094 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
2095 a2dp.enc_channels = 1;
2096 else
2097 a2dp.enc_channels = 2;
2098
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302099 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
2100 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302101fail:
2102 return is_configured;
2103}
2104
Manish Dewangan6a252632017-12-04 17:27:44 +05302105#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05302106static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
2107 audio_aptx_encoder_config *aptx_bt_cfg)
2108{
2109 int ret = 0;
2110
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002111 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05302112 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
2113 aptx_dsp_cfg, aptx_bt_cfg);
2114 return -EINVAL;
2115 }
2116
2117 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05302118 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302119
2120
2121 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
2122 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
2123 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
2124 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
2125 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
2126 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
2127 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
2128 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
2129 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05302130 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302131 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
2132 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
2133 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2134 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
2135
2136
2137 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
2138 case APTX_AD_CHANNEL_UNCHANGED:
2139 case APTX_AD_CHANNEL_JOINT_STEREO:
2140 case APTX_AD_CHANNEL_DUAL_MONO:
2141 case APTX_AD_CHANNEL_STEREO_TWS:
2142 case APTX_AD_CHANNEL_EARBUD:
2143 default:
2144 a2dp.enc_channels = CH_STEREO;
2145 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
2146 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2147 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2148 break;
2149 case APTX_AD_CHANNEL_MONO:
2150 a2dp.enc_channels = CH_MONO;
2151 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
2152 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08002153 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302154 }
2155 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
2156 case APTX_AD_SR_UNCHANGED:
2157 case APTX_AD_48:
2158 default:
2159 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
2160 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
2161 break;
2162 case APTX_AD_44_1:
2163 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
2164 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08002165 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302166 }
2167 ALOGV("Successfully updated APTX AD enc format with \
2168 samplingrate: %d channels:%d",
2169 aptx_dsp_cfg->custom_cfg.sample_rate,
2170 aptx_dsp_cfg->custom_cfg.num_channels);
2171
2172 return ret;
2173}
Manisha Agarwala51768b2018-11-01 16:30:52 +05302174
Zhou Song8fccbb62019-03-20 01:08:19 +08002175static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
2176 audio_aptx_encoder_config *aptx_bt_cfg)
2177{
2178 int ret = 0;
2179
2180 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Mingshu Pangaa429f72019-06-10 17:25:00 +08002181 ALOGE("Invalid param, aptx_dsp_cfg %pK aptx_bt_cfg %pK",
Zhou Song8fccbb62019-03-20 01:08:19 +08002182 aptx_dsp_cfg, aptx_bt_cfg);
2183 return -EINVAL;
2184 }
2185
2186 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
2187 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
2188
2189
2190 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
2191 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
2192 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
2193 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
2194 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
2195 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
2196 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
2197 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
2198 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
2199 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
2200 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
2201 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
2202 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
2203 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
2204 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
2205 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
2206 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2207 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
2208
2209
2210 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
2211 case APTX_AD_CHANNEL_UNCHANGED:
2212 case APTX_AD_CHANNEL_JOINT_STEREO:
2213 case APTX_AD_CHANNEL_DUAL_MONO:
2214 case APTX_AD_CHANNEL_STEREO_TWS:
2215 case APTX_AD_CHANNEL_EARBUD:
2216 default:
2217 a2dp.enc_channels = CH_STEREO;
2218 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
2219 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2220 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2221 break;
2222 case APTX_AD_CHANNEL_MONO:
2223 a2dp.enc_channels = CH_MONO;
2224 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
2225 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08002226 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08002227 }
2228 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
2229 case APTX_AD_SR_UNCHANGED:
2230 case APTX_AD_48:
2231 default:
2232 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
2233 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
2234 break;
2235 case APTX_AD_44_1:
2236 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
2237 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08002238 break;
2239 case APTX_AD_96:
2240 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
2241 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_96K;
2242 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08002243 }
2244 ALOGV("Successfully updated APTX AD enc format with \
2245 samplingrate: %d channels:%d",
2246 aptx_dsp_cfg->custom_cfg.sample_rate,
2247 aptx_dsp_cfg->custom_cfg.num_channels);
2248
2249 return ret;
2250}
2251
Manisha Agarwala51768b2018-11-01 16:30:52 +05302252static void audio_a2dp_update_tws_channel_mode()
2253{
2254 char* channel_mode;
2255 struct mixer_ctl *ctl_channel_mode;
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05302256
2257 ALOGD("Update tws for mono_mode on=%d",a2dp.is_tws_mono_mode_on);
2258
Manisha Agarwala51768b2018-11-01 16:30:52 +05302259 if (a2dp.is_tws_mono_mode_on)
2260 channel_mode = "One";
2261 else
2262 channel_mode = "Two";
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05302263
Manisha Agarwala51768b2018-11-01 16:30:52 +05302264 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2265 if (!ctl_channel_mode) {
2266 ALOGE("failed to get tws mixer ctl");
2267 return;
2268 }
2269 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2270 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2271 return;
2272 }
2273}
2274
Manisha Agarwald45632b2019-10-17 18:14:28 +08002275static void audio_a2dp_update_lc3_channel_mode()
2276{
2277 char* channel_mode;
2278 struct mixer_ctl *ctl_channel_mode;
2279
2280 ALOGD("Update lc3 for mono_mode on=%d",a2dp.is_lc3_mono_mode_on);
2281
2282 if (a2dp.is_lc3_mono_mode_on)
2283 channel_mode = "One";
2284 else
2285 channel_mode = "Two";
2286
2287 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_LC3_CHANNEL_MODE);
2288 if (!ctl_channel_mode) {
2289 ALOGE("failed to get lc3 mixer ctl");
2290 return;
2291 }
2292 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2293 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2294 return;
2295 }
2296}
2297
Manish Dewangan6a252632017-12-04 17:27:44 +05302298static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
2299 audio_aptx_encoder_config *aptx_bt_cfg)
2300{
2301 int ret = 0;
2302
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002303 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05302304 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
2305 aptx_dsp_cfg, aptx_bt_cfg);
2306 return -EINVAL;
2307 }
2308
2309 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002310 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05302311
2312 if (!a2dp.is_aptx_dual_mono_supported) {
2313 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
2314 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
2315 } else {
2316 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
2317 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
2318 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
2319 }
2320
2321 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
2322 case 1:
2323 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2324 break;
2325 case 2:
2326 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05302327 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2328 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
Manish Dewangan6a252632017-12-04 17:27:44 +05302329 break;
2330 }
2331 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
2332 if (!a2dp.is_aptx_dual_mono_supported) {
2333 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
2334 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
2335 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
2336 aptx_dsp_cfg->custom_cfg.num_channels);
2337 } else {
2338 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
2339 ALOGV("Successfully updated APTX dual mono enc format with \
2340 samplingrate: %d channels:%d syncmode %d",
2341 aptx_dsp_cfg->custom_cfg.sample_rate,
2342 aptx_dsp_cfg->custom_cfg.num_channels,
2343 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
2344 }
2345 return ret;
2346}
2347#else
2348static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
2349 audio_aptx_encoder_config *aptx_bt_cfg)
2350{
2351 int ret = 0;
2352
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002353 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05302354 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
2355 aptx_dsp_cfg, aptx_bt_cfg);
2356 return -EINVAL;
2357 }
2358
2359 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002360 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05302361 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
2362 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
2363 switch(aptx_dsp_cfg->num_channels) {
2364 case 1:
2365 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
2366 break;
2367 case 2:
2368 default:
2369 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
2370 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
2371 break;
2372 }
2373
2374 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
2375 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
2376
2377 return ret;
2378}
2379#endif
2380
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302381/* API to configure APTX DSP encoder */
2382bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2383{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302384 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08002385 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08002386 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302387 bool is_configured = false;
2388 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08002389 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05302390
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002391 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05302392 return false;
2393
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002394 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2395 if (!ctl_enc_data) {
2396 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2397 return false;
2398 }
2399
Manish Dewangan6a252632017-12-04 17:27:44 +05302400#ifndef LINUX_ENABLED
2401 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302402 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08002403 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002404 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08002405 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
2406 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002407 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08002408 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002409 else
Zhou Song8fccbb62019-03-20 01:08:19 +08002410 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002411 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002412 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002413
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002414 if (ret) {
2415 is_configured = false;
2416 goto fail;
2417 }
2418
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002419 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08002420 if (aptx_ad_ctl)
2421 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002422 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08002423 else
2424 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002425 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002426 } else {
2427 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002428 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302429 }
Manish Dewangan6a252632017-12-04 17:27:44 +05302430#else
2431 struct custom_enc_cfg_t aptx_dsp_cfg;
2432 mixer_size = sizeof(struct custom_enc_cfg_t);
2433 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05302434 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05302435 if (ret) {
2436 is_configured = false;
2437 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302438 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002439 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
2440 mixer_size);
2441#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302442 if (ret != 0) {
2443 ALOGE("%s: Failed to set APTX encoder config", __func__);
2444 is_configured = false;
2445 goto fail;
2446 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302447#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002448 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302449 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002450 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302451 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
2452 else
2453 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302454#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302455 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302456 is_configured = false;
2457 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302458 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302459 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302460 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05302461 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302462 else
Surendar Karka2febd452018-12-13 17:56:43 +05302463 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302464fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05302465 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002466 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05302467 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302468 return is_configured;
2469}
2470
2471/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302472 */
Manish Dewangan6a252632017-12-04 17:27:44 +05302473#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002474bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302475#else
2476bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2477#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302478{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302479 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302480 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302481 bool is_configured = false;
2482 int ret = 0;
2483
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002484 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302485 return false;
2486
2487 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2488 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002489 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302490 is_configured = false;
2491 goto fail;
2492 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302493
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302494 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002495 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302496 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2497 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2498 switch(aptx_dsp_cfg.num_channels) {
2499 case 1:
2500 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2501 break;
2502 case 2:
2503 default:
2504 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2505 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2506 break;
2507 }
2508 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302509 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302510 if (ret != 0) {
2511 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2512 is_configured = false;
2513 goto fail;
2514 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302515 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302516 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302517 is_configured = false;
2518 goto fail;
2519 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302520 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002521 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302522 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302523 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302524 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2525 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302526fail:
2527 return is_configured;
2528}
2529
2530/* API to configure AAC DSP encoder */
2531bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2532{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302533 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302534 struct aac_enc_cfg_t aac_dsp_cfg;
2535 bool is_configured = false;
2536 int ret = 0;
2537
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002538 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302539 return false;
2540
2541 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2542 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002543 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302544 is_configured = false;
2545 goto fail;
2546 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302547 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002548 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302549 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302550 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002551 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302552 case 0:
2553 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2554 break;
2555 case 2:
2556 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2557 break;
2558 case 1:
2559 default:
2560 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2561 break;
2562 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302563 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302564 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002565
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302566 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2567 sizeof(struct aac_enc_cfg_t));
2568 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002569 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302570 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302571 goto fail;
2572 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302573 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302574 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302575 is_configured = false;
2576 goto fail;
2577 }
2578 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002579 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302580 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002581 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002582 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2583 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2584fail:
2585 return is_configured;
2586}
2587
2588bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2589{
2590 struct mixer_ctl *ctl_enc_data = NULL;
2591 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2592 bool is_configured = false;
2593 int ret = 0;
2594
2595 if (aac_bt_cfg == NULL)
2596 return false;
2597
2598 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2599 if (!ctl_enc_data) {
2600 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2601 is_configured = false;
2602 goto fail;
2603 }
2604 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302605 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002606 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2607 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2608 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2609 case 0:
2610 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2611 break;
2612 case 2:
2613 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2614 break;
2615 case 1:
2616 default:
2617 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2618 break;
2619 }
2620 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2621 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2622 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2623 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2624
2625 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2626 sizeof(struct aac_enc_cfg_v2_t));
2627 if (ret != 0) {
2628 ALOGE("%s: Failed to set AAC encoder config", __func__);
2629 is_configured = false;
2630 goto fail;
2631 }
2632 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2633 if (ret != 0) {
2634 is_configured = false;
2635 goto fail;
2636 }
2637 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302638 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002639 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2640 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2641 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2642 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302643fail:
2644 return is_configured;
2645}
2646
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302647bool configure_aac_enc_format_v3(audio_aac_encoder_config_v3 *aac_bt_cfg)
2648{
2649 struct mixer_ctl *ctl_enc_data = NULL;
2650 struct aac_enc_cfg_v3_t aac_dsp_cfg;
2651 struct aac_frame_size_control_t* frame_vbr_ctl = NULL;
2652 bool is_configured = false;
2653 int ret = 0;
2654
2655 if (aac_bt_cfg == NULL)
2656 return false;
2657
2658 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2659 if (!ctl_enc_data) {
2660 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2661 is_configured = false;
2662 goto fail;
2663 }
2664 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v3_t));
2665 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
2666 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2667 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2668 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2669 case 0:
2670 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2671 break;
2672 case 2:
2673 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2674 break;
2675 case 1:
2676 default:
2677 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2678 break;
2679 }
2680 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2681 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2682 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2683 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2684 frame_vbr_ctl = aac_bt_cfg->frame_ptr_ctl;
2685
2686 if (frame_vbr_ctl != NULL) {
2687 aac_dsp_cfg.aac_key_value_ctl.ctl_type = frame_vbr_ctl->ctl_type;
2688 aac_dsp_cfg.aac_key_value_ctl.ctl_value = frame_vbr_ctl->ctl_value;
2689 } else {
2690 ALOGE("%s: VBR cannot be enabled, fall back to default",__func__);
2691 aac_dsp_cfg.aac_key_value_ctl.ctl_type = 0;
2692 aac_dsp_cfg.aac_key_value_ctl.ctl_value = 0;
2693 }
2694
2695 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2696 sizeof(struct aac_enc_cfg_v3_t));
2697 if (ret != 0) {
2698 ALOGE("%s: Failed to set AAC encoder config", __func__);
2699 is_configured = false;
2700 goto fail;
2701 }
2702 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2703 if (ret != 0) {
2704 is_configured = false;
2705 goto fail;
2706 }
2707 is_configured = true;
2708 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
2709 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2710 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2711 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2712 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
2713fail:
2714 return is_configured;
2715}
2716
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302717bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2718{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302719 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302720 struct celt_enc_cfg_t celt_dsp_cfg;
2721 bool is_configured = false;
2722 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002723 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302724 return false;
2725
2726 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2727 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002728 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302729 is_configured = false;
2730 goto fail;
2731 }
2732 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2733
Florian Pfister1a84f312018-07-19 14:38:18 +02002734 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302735 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2736 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2737 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2738 case 1:
2739 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2740 break;
2741 case 2:
2742 default:
2743 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2744 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2745 break;
2746 }
2747
2748 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2749
2750 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2751 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2752 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2753 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2754 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2755
2756 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2757 sizeof(struct celt_enc_cfg_t));
2758 if (ret != 0) {
2759 ALOGE("%s: Failed to set CELT encoder config", __func__);
2760 is_configured = false;
2761 goto fail;
2762 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302763 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302764 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302765 is_configured = false;
2766 goto fail;
2767 }
2768 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002769 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302770 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302771 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302772 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2773 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2774fail:
2775 return is_configured;
2776}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302777
2778bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2779{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302780 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302781 struct ldac_enc_cfg_t ldac_dsp_cfg;
2782 bool is_configured = false;
2783 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002784 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302785 return false;
2786
2787 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2788 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002789 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302790 is_configured = false;
2791 goto fail;
2792 }
2793 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2794
Florian Pfister1a84f312018-07-19 14:38:18 +02002795 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302796 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2797 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2798 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2799 case 4:
2800 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2801 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2802 break;
2803 case 2:
2804 case 1:
2805 default:
2806 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2807 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2808 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2809 break;
2810 }
2811
2812 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2813 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2814 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002815 if (ldac_bt_cfg->is_abr_enabled) {
2816 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2817 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2818 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2819 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2820 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002821 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002822 }
2823
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302824 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2825 sizeof(struct ldac_enc_cfg_t));
2826 if (ret != 0) {
2827 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2828 is_configured = false;
2829 goto fail;
2830 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302831 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302832 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302833 is_configured = false;
2834 goto fail;
2835 }
2836 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002837 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302838 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2839 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002840 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302841 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2842 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2843fail:
2844 return is_configured;
2845}
2846
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302847bool configure_a2dp_encoder_format()
2848{
2849 void *codec_info = NULL;
2850 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002851 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302852 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002853 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302854
Florian Pfister1a84f312018-07-19 14:38:18 +02002855 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302856 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2857 return false;
2858 }
2859 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002860 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302861 &codec_type);
2862
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002863 // ABR disabled by default for all codecs
2864 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302865 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002866
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302867 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002868 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302869 ALOGD(" Received SBC encoder supported BT device");
2870 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002871 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302872 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002873 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302874 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302875#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002876 a2dp.is_aptx_dual_mono_supported = false;
2877 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302878#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302879 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002880 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302881 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002882 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302883 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302884#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302885 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002886 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302887#else
2888 is_configured =
2889 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2890#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002891 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302892#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002893 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002894 ALOGD(" Received APTX dual mono encoder supported BT device");
2895 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302896 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2897 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002898 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2899 is_configured =
2900 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302901 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302902#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002903 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302904 ALOGD(" Received AAC encoder supported BT device");
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302905 bool is_aac_vbr_enabled =
2906 property_get_bool("persist.vendor.bt.aac_vbr_frm_ctl.enabled", false);
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002907 bool is_aac_frame_ctl_enabled =
2908 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302909 if (is_aac_vbr_enabled)
2910 is_configured = configure_aac_enc_format_v3((audio_aac_encoder_config_v3 *) codec_info);
2911 else
2912 is_configured = is_aac_frame_ctl_enabled ?
2913 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2914 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302915 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002916 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302917 ALOGD(" Received CELT encoder supported BT device");
2918 is_configured =
2919 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2920 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002921 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302922 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002923 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2924 instance_id = MAX_INSTANCE_ID;
2925 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302926 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002927 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302928 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302929 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302930#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302931 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302932 ALOGD(" Received APTX AD encoder supported BT device");
2933 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2934 instance_id = MAX_INSTANCE_ID;
2935 a2dp.abr_config.imc_instance = instance_id--;
2936 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2937 a2dp.is_aptx_adaptive = true;
2938 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2939 is_configured =
2940 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302941 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302942 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302943#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002944 case CODEC_TYPE_PCM:
2945 ALOGD("Received PCM format for BT device");
2946 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2947 is_configured = true;
2948 break;
Manisha Agarwald45632b2019-10-17 18:14:28 +08002949 case CODEC_TYPE_LC3:
2950 ALOGD("Received LC3 encoder supported BT device");
2951 a2dp.bt_encoder_format = CODEC_TYPE_LC3;
2952 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2953 instance_id = MAX_INSTANCE_ID;
2954 a2dp.abr_config.imc_instance = instance_id--;
2955 a2dp.abr_config.is_abr_enabled = true;
2956 is_configured =
2957 (configure_lc3_enc_format((audio_lc3_codec_config_t *)codec_info) &&
2958 configure_lc3_dec_format((audio_lc3_codec_config_t *)codec_info));
2959 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302960 default:
2961 ALOGD(" Received Unsupported encoder formar");
2962 is_configured = false;
2963 break;
2964 }
2965 return is_configured;
2966}
2967
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002968int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302969{
2970 int ret = 0;
2971
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002972 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302973
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002974 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002975 && a2dp.audio_get_enc_config)) {
2976 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302977 return -ENOSYS;
2978 }
2979
Zhou Song12c29502019-03-16 10:37:18 +08002980 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302981 //session will be restarted after suspend completion
2982 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302983 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302984 }
2985
Florian Pfister1a84f312018-07-19 14:38:18 +02002986 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302987 ALOGD("calling BT module stream start");
2988 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002989 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302990 ALOGE("BT controller start return = %d",ret);
2991 if (ret != 0 ) {
2992 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002993 a2dp.a2dp_source_started = false;
Revathi Uddarajub26e3932020-06-10 14:51:02 +05302994 ret = -ETIMEDOUT;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302995 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002996 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002997 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302998 ret = 0;
2999 ALOGD("Start playback successful to BT library");
3000 } else {
3001 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02003002 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303003 ret = -ETIMEDOUT;
3004 }
3005 }
3006 }
3007
Florian Pfister1a84f312018-07-19 14:38:18 +02003008 if (a2dp.a2dp_source_started) {
3009 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05303010 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05303011 audio_a2dp_update_tws_channel_mode();
Manisha Agarwald45632b2019-10-17 18:14:28 +08003012 audio_a2dp_update_lc3_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02003013 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08003014 if (a2dp.abr_config.is_abr_enabled)
3015 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05303016 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303017
3018 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02003019 a2dp.a2dp_source_total_active_session_requests);
3020 return ret;
3021}
3022
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003023uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02003024{
3025 uint32_t latency = 0;
3026
3027 switch(a2dp.bt_decoder_format) {
3028 case CODEC_TYPE_SBC:
3029 latency = DEFAULT_SINK_LATENCY_SBC;
3030 break;
3031 case CODEC_TYPE_AAC:
3032 latency = DEFAULT_SINK_LATENCY_AAC;
3033 break;
3034 default:
3035 latency = 200;
3036 ALOGD("No valid decoder defined, setting latency to %dms", latency);
3037 break;
3038 }
3039 return (uint64_t)latency;
3040}
3041
3042bool a2dp_send_sink_setup_complete(void) {
3043 uint64_t system_latency = 0;
3044 bool is_complete = false;
3045
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003046 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02003047
3048 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
3049 is_complete = true;
3050 }
3051 return is_complete;
3052}
3053
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003054bool a2dp_sink_is_ready()
3055{
3056 bool ret = false;
3057
3058 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
3059 (a2dp.is_a2dp_offload_supported) &&
3060 (a2dp.audio_sink_check_a2dp_ready))
3061 ret = a2dp.audio_sink_check_a2dp_ready();
3062 return ret;
3063}
3064
3065int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02003066{
3067 int ret = 0;
3068
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003069 ALOGD("a2dp_start_capture start");
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303070 /* checking for sink lib for mobile platform not available then using source lib */
3071 if ((!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
3072 && a2dp.audio_get_dec_config)) && (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
3073 && a2dp.audio_get_enc_config))) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003074 ALOGE("a2dp handle is not identified, Ignoring start capture request");
3075 return -ENOSYS;
3076 }
3077
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303078 if (a2dp.bt_lib_sink_handle && !a2dp.a2dp_sink_started
Zhou Songf7a2fdf2021-05-07 09:14:21 +08003079 && !a2dp.a2dp_sink_total_active_session_requests && a2dp.audio_sink_start) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003080 ALOGD("calling BT module stream start");
3081 /* This call indicates BT IPC lib to start capture */
3082 ret = a2dp.audio_sink_start();
3083 ALOGE("BT controller start capture return = %d",ret);
3084 if (ret != 0 ) {
3085 ALOGE("BT controller start capture failed");
3086 a2dp.a2dp_sink_started = false;
3087 } else {
3088
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003089 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003090 ALOGD("Wait for capture ready not successful");
3091 ret = -ETIMEDOUT;
3092 }
3093
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003094 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003095 a2dp.a2dp_sink_started = true;
3096 ret = 0;
3097 ALOGD("Start capture successful to BT library");
3098 } else {
3099 ALOGD(" unable to configure DSP decoder");
3100 a2dp.a2dp_sink_started = false;
3101 ret = -ETIMEDOUT;
3102 }
3103
3104 if (!a2dp_send_sink_setup_complete()) {
3105 ALOGD("sink_setup_complete not successful");
3106 ret = -ETIMEDOUT;
3107 }
3108 }
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303109 } else if ((a2dp.bt_lib_source_handle) && (configure_a2dp_sink_decoder_format())) {
3110 if (a2dp.audio_source_start) {
3111 ret = a2dp.audio_source_start();
3112 if (ret == 0) {
3113 a2dp.a2dp_sink_started = true;
3114 ALOGD("Start capture successful to BT library");
3115 } else {
3116 ALOGE("BT controller start failed");
3117 }
3118 }
3119 } else {
3120 ALOGD(" unable to configure DSP decoder");
3121 a2dp.a2dp_sink_started = false;
3122 ret = -ETIMEDOUT;
Florian Pfister1a84f312018-07-19 14:38:18 +02003123 }
3124
3125 if (a2dp.a2dp_sink_started) {
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303126 if (a2dp_set_backend_cfg(SINK) == true)
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003127 a2dp.a2dp_sink_total_active_session_requests++;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303128 /* Start abr for LC3 decoder*/
3129 if (a2dp.bt_decoder_format == CODEC_TYPE_LC3) {
3130 a2dp.abr_config.is_abr_enabled = true;
Zhou Song8d7ba052021-05-17 00:40:35 +08003131 /*
3132 * Before starting abr, we must ensure to set correct acdb id
3133 * because abr will trigger port open which needs acdb_id
3134 */
3135 fp_platform_switch_voice_call_device_post(a2dp.adev->platform,
3136 SND_DEVICE_OUT_BT_SCO_WB,
3137 SND_DEVICE_IN_BT_SCO_MIC_WB);
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303138 start_abr();
Florian Pfister1a84f312018-07-19 14:38:18 +02003139 }
3140 }
3141
3142 ALOGD("start A2DP sink total active sessions :%d",
3143 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303144 return ret;
3145}
3146
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303147static void reset_a2dp_enc_config_params()
3148{
3149 int ret =0;
3150
Aalique Grahame22e49102018-12-18 14:23:57 -08003151 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303152 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05303153 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303154
3155 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
3156 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
3157 MIXER_ENC_CONFIG_BLOCK);
3158 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003159 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303160 } else {
3161 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
3162 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02003163 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303164 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003165
3166 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3167
Manisha Agarwala51768b2018-11-01 16:30:52 +05303168 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
3169
3170 if (!ctl_channel_mode) {
3171 ALOGE("failed to get tws mixer ctl");
3172 } else {
3173 channel_mode = "Two";
3174 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
3175 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
3176 }
3177 a2dp.is_tws_mono_mode_on = false;
3178 }
Manisha Agarwald45632b2019-10-17 18:14:28 +08003179
3180 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_LC3_CHANNEL_MODE);
3181
3182 if (!ctl_channel_mode) {
3183 ALOGE("failed to get lc3 mixer ctl");
3184 } else {
3185 channel_mode = "Two";
3186 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0)
3187 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
3188 a2dp.is_lc3_mono_mode_on = false;
3189 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303190}
3191
Surendar Karka2febd452018-12-13 17:56:43 +05303192static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07003193{
3194 struct mixer_ctl *ctl_dec_data = NULL;
3195 struct abr_dec_cfg_t dummy_reset_cfg;
3196 int ret = 0;
3197
Zhou Songf7a2fdf2021-05-07 09:14:21 +08003198 if (a2dp.bt_decoder_format == CODEC_TYPE_LC3)
3199 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
3200
Preetam Singh Ranawat25780e42019-07-31 18:15:57 +05303201 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3202 if (!ctl_dec_data) {
3203 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
3204 return -EINVAL;
3205 }
3206 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
3207 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
3208 sizeof(dummy_reset_cfg));
3209 if (ret != 0) {
3210 ALOGE("%s: Failed to set dummy decoder config", __func__);
3211 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07003212 }
3213
3214 return ret;
3215}
3216
Surendar Karka2febd452018-12-13 17:56:43 +05303217static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02003218{
3219 int ret =0;
3220
3221 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
3222 struct aac_dec_cfg_t dummy_reset_config;
3223
3224 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
3225 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05303226 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02003227 if (!ctl_dec_config) {
3228 ALOGE(" ERROR a2dp decoder format mixer control not identified");
3229 } else {
3230 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
3231 sizeof(struct aac_dec_cfg_t));
3232 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
3233 }
3234 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
3235 MIXER_DEC_BIT_FORMAT);
3236 if (!ctrl_bit_format) {
3237 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
3238 } else {
3239 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
3240 if (ret != 0) {
3241 ALOGE("%s: Failed to set bit format to decoder", __func__);
3242 }
3243 }
3244}
3245
Zhou Song12c29502019-03-16 10:37:18 +08003246static void reset_codec_config()
3247{
3248 reset_a2dp_enc_config_params();
3249 reset_a2dp_source_dec_config_params();
3250 a2dp_reset_backend_cfg(SOURCE);
3251 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
3252 stop_abr();
3253 a2dp.abr_config.is_abr_enabled = false;
3254}
3255
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003256int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303257{
3258 int ret =0;
3259
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003260 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003261 if (!(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");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303263 return -ENOSYS;
3264 }
3265
Florian Pfister1a84f312018-07-19 14:38:18 +02003266 if (a2dp.a2dp_source_total_active_session_requests > 0)
3267 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08003268 else
3269 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303270
Florian Pfister1a84f312018-07-19 14:38:18 +02003271 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303272 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02003273 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303274 if (ret < 0)
3275 ALOGE("stop stream to BT IPC lib failed");
3276 else
3277 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08003278 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
3279 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05303280 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303281 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003282 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02003283 a2dp.a2dp_source_started = false;
3284 ALOGD("Stop A2DP playback, total active sessions :%d",
3285 a2dp.a2dp_source_total_active_session_requests);
3286 return 0;
3287}
3288
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003289int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02003290{
3291 int ret =0;
3292
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003293 ALOGV("a2dp_stop_capture start");
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303294 if ((!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop))
3295 && (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop))) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003296 ALOGE("a2dp handle is not identified, Ignoring stop request");
3297 return -ENOSYS;
3298 }
3299
3300 if (a2dp.a2dp_sink_total_active_session_requests > 0)
3301 a2dp.a2dp_sink_total_active_session_requests--;
3302
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303303 if (a2dp.bt_lib_sink_handle && a2dp.a2dp_sink_started
Zhou Songf7a2fdf2021-05-07 09:14:21 +08003304 && !a2dp.a2dp_sink_total_active_session_requests && a2dp.audio_sink_stop) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003305 ALOGV("calling BT module stream stop");
3306 ret = a2dp.audio_sink_stop();
3307 if (ret < 0)
3308 ALOGE("stop stream to BT IPC lib failed");
3309 else
3310 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05303311 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02003312 a2dp_reset_backend_cfg(SINK);
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303313 } else if (a2dp.bt_lib_source_handle) {
3314 ret = a2dp.audio_source_stop();
3315 if (ret < 0)
3316 ALOGE("stop stream to BT IPC lib failed");
3317 else
3318 ALOGV("stop steam to BT IPC lib successful");
Zhou Songf7a2fdf2021-05-07 09:14:21 +08003319 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303320 reset_codec_config();
3321 a2dp.a2dp_sink_started = false;
Florian Pfister1a84f312018-07-19 14:38:18 +02003322 }
Kakanaboina Ramanjaneyulu8cedb092021-03-15 15:55:29 +05303323
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003324 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02003325 a2dp.a2dp_source_started = false;
3326 ALOGD("Stop A2DP capture, total active sessions :%d",
3327 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303328 return 0;
3329}
3330
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003331int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303332{
Weiyin Jiang280ea742020-09-08 20:28:22 +08003333 int ret = 0, val, status = 0;
3334 char value[32] = {0};
3335 struct audio_usecase *uc_info;
3336 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303337
Weiyin Jiang280ea742020-09-08 20:28:22 +08003338 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003339 ALOGV("no supported encoders identified,ignoring a2dp setparam");
3340 status = -EINVAL;
3341 goto param_handled;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003342 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303343
Weiyin Jiang280ea742020-09-08 20:28:22 +08003344 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303345 sizeof(value));
Weiyin Jiang280ea742020-09-08 20:28:22 +08003346 if (ret >= 0) {
3347 val = atoi(value);
3348 if (audio_is_a2dp_out_device(val)) {
3349 ALOGV("Received device connect request for A2DP source");
3350 open_a2dp_source();
3351 }
3352 goto param_handled;
3353 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303354
Weiyin Jiang280ea742020-09-08 20:28:22 +08003355 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
3356 sizeof(value));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303357
Weiyin Jiang280ea742020-09-08 20:28:22 +08003358 if (ret >= 0) {
3359 val = atoi(value);
3360 if (audio_is_a2dp_out_device(val)) {
3361 ALOGV("Received source device dis- connect request");
3362 close_a2dp_output();
3363 reset_a2dp_enc_config_params();
3364 reset_a2dp_source_dec_config_params();
3365 a2dp_reset_backend_cfg(SOURCE);
3366 } else if (audio_is_a2dp_in_device(val)) {
3367 ALOGV("Received sink device dis- connect request");
3368 close_a2dp_input();
3369 reset_a2dp_sink_dec_config_params();
3370 a2dp_reset_backend_cfg(SINK);
3371 }
3372 goto param_handled;
3373 }
Surendar Karka2febd452018-12-13 17:56:43 +05303374#ifndef LINUX_ENABLED
Weiyin Jiang280ea742020-09-08 20:28:22 +08003375 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
3376 if (ret >= 0) {
3377 ALOGD("Setting tws channel mode to %s",value);
3378 if (!(strncmp(value, "mono", strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05303379 a2dp.is_tws_mono_mode_on = true;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003380 else if (!(strncmp(value, "dual-mono", strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05303381 a2dp.is_tws_mono_mode_on = false;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003382 audio_a2dp_update_tws_channel_mode();
3383 goto param_handled;
3384 }
Manisha Agarwald45632b2019-10-17 18:14:28 +08003385
3386 ret = str_parms_get_str(parms, "LEAMono", value, sizeof(value));
3387 if (ret>=0) {
3388 ALOGD("Setting LC3 channel mode to %s",value);
3389 if (!(strncmp(value,"true",strlen(value))))
3390 a2dp.is_lc3_mono_mode_on = true;
3391 else
3392 a2dp.is_lc3_mono_mode_on = false;
3393 audio_a2dp_update_lc3_channel_mode();
3394 goto param_handled;
3395 }
Surendar Karka2febd452018-12-13 17:56:43 +05303396#endif
Weiyin Jiang280ea742020-09-08 20:28:22 +08003397 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
3398 if (ret >= 0) {
3399 if (a2dp.bt_lib_source_handle == NULL)
3400 goto param_handled;
3401
3402 if ((!strncmp(value, "true", sizeof(value)))) {
3403 if (a2dp.a2dp_source_suspended) {
3404 ALOGD("%s: A2DP is already suspended", __func__);
3405 goto param_handled;
3406 }
3407 ALOGD("Setting a2dp to suspend state");
3408 a2dp.a2dp_source_suspended = true;
3409 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
3410 goto param_handled;
3411 list_for_each(node, &a2dp.adev->usecase_list) {
3412 uc_info = node_to_item(node, struct audio_usecase, list);
3413 if (uc_info->type == PCM_PLAYBACK &&
3414 (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
3415 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
3416 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)) {
3417 fp_check_a2dp_restore_l(a2dp.adev, uc_info->stream.out, false);
Aalique Grahame22e49102018-12-18 14:23:57 -08003418 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08003419 }
3420 if (!a2dp.swb_configured)
3421 reset_codec_config();
3422 if (a2dp.audio_source_suspend)
3423 a2dp.audio_source_suspend();
3424 } else if (a2dp.a2dp_source_suspended == true) {
3425 ALOGD("Resetting a2dp suspend state");
3426 struct audio_usecase *uc_info;
3427 struct listnode *node;
3428 if (a2dp.clear_source_a2dpsuspend_flag)
3429 a2dp.clear_source_a2dpsuspend_flag();
3430 a2dp.a2dp_source_suspended = false;
3431 /*
3432 * It is possible that before suspend,a2dp sessions can be active
3433 * for example during music + voice activation concurrency
3434 * a2dp suspend will be called & BT will change to sco mode
3435 * though music is paused as a part of voice activation
3436 * compress session close happens only after pause timeout(10secs)
3437 * so if resume request comes before pause timeout as a2dp session
3438 * is already active IPC start will not be called from APM/audio_hw
3439 * Fix is to call a2dp start for IPC library post suspend
3440 * based on number of active session count
3441 */
3442 if (a2dp.a2dp_source_total_active_session_requests > 0) {
3443 ALOGD(" Calling IPC lib start post suspend state");
3444 if (a2dp.audio_source_start) {
3445 ret = a2dp.audio_source_start();
3446 if (ret != 0) {
3447 ALOGE("BT controller start failed");
3448 a2dp.a2dp_source_started = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303449 }
3450 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08003451 }
3452 list_for_each(node, &a2dp.adev->usecase_list) {
3453 uc_info = node_to_item(node, struct audio_usecase, list);
3454 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
3455 is_a2dp_out_device_type(&uc_info->stream.out->device_list)) {
3456 fp_check_a2dp_restore_l(a2dp.adev, uc_info->stream.out, true);
Zhou Song10617ed2017-05-26 13:28:48 +08003457 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303458 }
3459 }
3460 goto param_handled;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003461 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003462
Weiyin Jiang280ea742020-09-08 20:28:22 +08003463 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
3464 sizeof(value));
3465 if (ret >= 0) {
3466 if (a2dp.is_a2dp_offload_supported &&
3467 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
3468 *reconfig = true;
3469 }
3470 goto param_handled;
3471 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003472
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303473param_handled:
Weiyin Jiang280ea742020-09-08 20:28:22 +08003474 ALOGV("end of a2dp setparam");
3475 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303476}
3477
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003478void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05303479{
3480 a2dp.is_handoff_in_progress = is_on;
3481}
3482
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003483bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303484{
3485 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05303486 // Or if a2dp device is selected but earlier start failed ( as a2dp
3487 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02003488 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303489}
3490
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003491void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303492{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303493 *sample_rate = a2dp.enc_sampling_rate;
3494}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303495
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003496void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02003497{
3498 *sample_rate = a2dp.dec_sampling_rate;
3499}
3500
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003501bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303502{
3503 bool ret = false;
3504
Florian Pfister1a84f312018-07-19 14:38:18 +02003505 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303506 return ret;
3507
Florian Pfister1a84f312018-07-19 14:38:18 +02003508 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08003509 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02003510 (a2dp.audio_source_check_a2dp_ready))
3511 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303512 return ret;
3513}
3514
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003515bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02003516{
3517 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303518}
3519
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003520void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08003521 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303522{
3523 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02003524 a2dp.bt_lib_source_handle = NULL;
3525 a2dp.a2dp_source_started = false;
3526 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
3527 a2dp.a2dp_source_total_active_session_requests = 0;
3528 a2dp.a2dp_source_suspended = false;
3529 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303530 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303531 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07003532 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05303533 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08003534 a2dp.abr_config.is_abr_enabled = false;
3535 a2dp.abr_config.abr_started = false;
3536 a2dp.abr_config.imc_instance = 0;
3537 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08003538 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05303539 a2dp.is_tws_mono_mode_on = false;
Manisha Agarwald45632b2019-10-17 18:14:28 +08003540 a2dp.is_lc3_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05303541 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08003542 a2dp.swb_configured = false;
3543
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003544 // init function pointers
3545 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08003546 init_config.fp_platform_get_pcm_device_id;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003547 fp_check_a2dp_restore_l = init_config.fp_check_a2dp_restore_l;
Gautam Manamfbb3ebc2020-10-08 18:06:45 +05303548 fp_platform_switch_voice_call_device_post =
3549 init_config.fp_platform_switch_voice_call_device_post;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003550
kunleiz5a127262017-09-08 14:47:48 +08003551 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05303552 reset_a2dp_source_dec_config_params();
3553 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02003554
3555 a2dp.bt_lib_sink_handle = NULL;
3556 a2dp.a2dp_sink_started = false;
3557 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
3558 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003559
3560 if (is_running_with_enhanced_fwk == UNINITIALIZED)
3561 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
3562 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08003563 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02003564
3565 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303566 update_offload_codec_capabilities();
3567}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003568
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003569uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003570{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003571 uint32_t latency = 0;
3572 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303573 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
3574 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003575 char value[PROPERTY_VALUE_MAX];
3576
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003577 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08003578 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003579 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303580 if (sscanf(value, "%d/%d/%d/%d/%d%d",
3581 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003582 ALOGI("Failed to parse avsync offset params from '%s'.", value);
3583 avsync_runtime_prop = 0;
3584 }
3585 }
3586
yidongh0515e042017-07-06 15:00:34 +08003587 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02003588 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
3589 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08003590 }
3591
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07003592 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003593 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08003594 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
3595 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003596 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003597 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08003598 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
3599 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003600 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003601 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08003602 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
3603 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003604 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003605 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08003606 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
3607 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003608 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003609 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05303610 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
3611 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
3612 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003613 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303614 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
3615 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
3616 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05303617 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05303618 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05303619 break;
Manisha Agarwald45632b2019-10-17 18:14:28 +08003620 case CODEC_TYPE_LC3:
3621 latency = slatency;
3622 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08003623 case CODEC_TYPE_PCM:
3624 latency = ENCODER_LATENCY_PCM;
3625 latency += DEFAULT_SINK_LATENCY_PCM;
3626 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003627 default:
3628 latency = 200;
3629 break;
3630 }
3631 return latency;
3632}
Aalique Grahame22e49102018-12-18 14:23:57 -08003633
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003634int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003635 struct str_parms *reply)
3636{
3637 int ret, val = 0;
3638 char value[32]={0};
3639
3640 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3641 value, sizeof(value));
3642 if (ret >= 0) {
3643 val = a2dp.is_a2dp_offload_supported;
3644 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3645 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3646 }
3647
3648 return 0;
3649}
Zhou Song12c29502019-03-16 10:37:18 +08003650
3651
3652bool configure_aptx_ad_speech_enc_fmt() {
3653 struct mixer_ctl *ctl_enc_data = NULL;
3654 int mixer_size = 0;
3655 int ret = 0;
3656 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3657
3658 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3659 if (!ctl_enc_data) {
3660 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3661 return false;
3662 }
3663
3664 /* Initialize dsp configuration params */
3665 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3666 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3667 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3668 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3669 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3670 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3671 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3672 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3673 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3674 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3675 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3676
3677 /* Configure AFE DSP configuration */
3678 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3679 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3680 mixer_size);
3681 if (ret != 0) {
3682 ALOGE("%s: Failed to set SWB encoder config", __func__);
3683 return false;
3684 }
3685
3686 /* Configure AFE Input Bit Format as PCM_16 */
3687 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3688 if (ret != 0) {
3689 ALOGE("%s: Failed to set SWB bit format", __func__);
3690 return false;
3691 }
3692
3693 return true;
3694}
3695
3696bool configure_aptx_ad_speech_dec_fmt()
3697{
3698 struct mixer_ctl *ctl_dec_data = NULL;
3699 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3700 int ret = 0;
3701
3702 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3703 if (!ctl_dec_data) {
3704 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3705 return false;
3706 }
3707 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3708 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3709 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3710 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3711 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3712 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3713 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3714 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3715
3716 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3717 sizeof(dec_cfg));
3718 if (ret != 0) {
3719 ALOGE("%s: Failed to set decoder config", __func__);
3720 return false;
3721 }
3722 return true;
3723}
3724
3725int sco_start_configuration()
3726{
3727 ALOGD("sco_start_configuration start");
3728
3729 if (!a2dp.swb_configured) {
Gautam Manamfbb3ebc2020-10-08 18:06:45 +05303730 /*Before starting sco config, we must ensure to set correct acdb id
3731 because sco cofiguration will trigger port open which needs acdb_id*/
3732 fp_platform_switch_voice_call_device_post(a2dp.adev->platform,
3733 SND_DEVICE_OUT_BT_SCO_SWB,
3734 SND_DEVICE_IN_BT_SCO_MIC_SWB);
3735
Zhou Song12c29502019-03-16 10:37:18 +08003736 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3737 /* Configure AFE codec*/
3738 if (configure_aptx_ad_speech_enc_fmt() &&
3739 configure_aptx_ad_speech_dec_fmt()) {
3740 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3741 } else {
3742 ALOGE("%s: failed to send SCO configuration", __func__);
3743 return -ETIMEDOUT;
3744 }
3745 /* Configure backend*/
3746 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3747 a2dp.enc_channels = CH_MONO;
3748 a2dp.abr_config.is_abr_enabled = true;
3749 a2dp_set_backend_cfg(SOURCE);
3750 /* Start abr*/
3751 start_abr();
3752 a2dp.swb_configured = true;
3753 }
3754 return 0;
3755}
3756
3757void sco_reset_configuration()
3758{
Zhou Songd6d71752019-05-21 18:08:51 +08003759 if (a2dp.swb_configured) {
3760 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003761
Zhou Songd6d71752019-05-21 18:08:51 +08003762 reset_codec_config();
3763 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3764 a2dp.swb_configured = false;
3765 }
Zhou Song12c29502019-03-16 10:37:18 +08003766}