blob: d8bbdfe5469d842af6da8835b2aad8ee3e046025 [file] [log] [blame]
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301/*
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002* Copyright (c) 2015-2020, 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"
Manisha Agarwala51768b2018-11-01 16:30:52 +053094#define MIXER_FMT_TWS_CHANNEL_MODE "TWS Channel Mode"
Manisha Agarwald45632b2019-10-17 18:14:28 +080095#define MIXER_FMT_LC3_CHANNEL_MODE "LC3 Channel Mode"
yidongh0515e042017-07-06 15:00:34 +080096#define ENCODER_LATENCY_SBC 10
97#define ENCODER_LATENCY_APTX 40
98#define ENCODER_LATENCY_APTX_HD 20
99#define ENCODER_LATENCY_AAC 70
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530100//To Do: Fine Tune Encoder CELT/LDAC latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530101#define ENCODER_LATENCY_CELT 40
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530102#define ENCODER_LATENCY_LDAC 40
Aalique Grahame22e49102018-12-18 14:23:57 -0800103#define ENCODER_LATENCY_PCM 50
yidongh0515e042017-07-06 15:00:34 +0800104#define DEFAULT_SINK_LATENCY_SBC 140
105#define DEFAULT_SINK_LATENCY_APTX 160
106#define DEFAULT_SINK_LATENCY_APTX_HD 180
107#define DEFAULT_SINK_LATENCY_AAC 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530108//To Do: Fine Tune Default CELT/LDAC Latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530109#define DEFAULT_SINK_LATENCY_CELT 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530110#define DEFAULT_SINK_LATENCY_LDAC 180
Aalique Grahame22e49102018-12-18 14:23:57 -0800111#define DEFAULT_SINK_LATENCY_PCM 140
112
Manisha Agarwald45632b2019-10-17 18:14:28 +0800113// decoder structure is considered.
114#define BAP_UNICAST 1
115// decoder structure is ignored.
116#define BAP_BROADCAST 2
117// decoder structure is treated as second toAirConfig.
118#define BAP_BA_SIMULCAST 3
119
Aalique Grahame22e49102018-12-18 14:23:57 -0800120#define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported"
121#define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled"
122#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
123
124// Default encoder bit width
125#define DEFAULT_ENCODER_BIT_FORMAT 16
126
Manisha Agarwald45632b2019-10-17 18:14:28 +0800127// PCM_24 encoder bit width
128#define ENCODER_BIT_FORMAT_PCM_24 24
129
Aalique Grahame22e49102018-12-18 14:23:57 -0800130// Default encoder latency
131#define DEFAULT_ENCODER_LATENCY 200
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530132
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800133// Slimbus Tx sample rate for ABR feedback channel
134#define ABR_TX_SAMPLE_RATE "KHZ_8"
135
Zhou Song12c29502019-03-16 10:37:18 +0800136// Slimbus Tx sample rate for APTX AD SPEECH
137#define SPEECH_TX_SAMPLE_RATE "KHZ_96"
138
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800139// Purpose ID for Inter Module Communication (IMC) in AFE
140#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
141
142// Maximum quality levels for ABR
143#define MAX_ABR_QUALITY_LEVELS 5
144
145// Instance identifier for A2DP
146#define MAX_INSTANCE_ID (UINT32_MAX / 2)
147
Zhou Song12c29502019-03-16 10:37:18 +0800148// Instance identifier for SWB
149#define APTX_AD_SPEECH_INSTANCE_ID 37
150
151#define SAMPLING_RATE_96K 96000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530152#define SAMPLING_RATE_48K 48000
153#define SAMPLING_RATE_441K 44100
Zhou Song12c29502019-03-16 10:37:18 +0800154#define SAMPLING_RATE_32K 32000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530155#define CH_STEREO 2
156#define CH_MONO 1
Florian Pfister1a84f312018-07-19 14:38:18 +0200157#define SOURCE 0
158#define SINK 1
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800159#define UNINITIALIZED -1
160
161#ifdef __LP64__
162#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
163#else
164#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
165#endif
166
Manisha Agarwald45632b2019-10-17 18:14:28 +0800167#define AUDIO_LOCATION_MAX 28
168
169uint32_t audio_location_map_array[] = { AUDIO_LOCATION_FRONT_LEFT, AUDIO_LOCATION_FRONT_RIGHT, AUDIO_LOCATION_FRONT_CENTER, AUDIO_LOCATION_LOW_FREQUENCY,
170 AUDIO_LOCATION_BACK_LEFT, AUDIO_LOCATION_BACK_RIGHT, AUDIO_LOCATION_FRONT_LEFT_OF_CENTER,
171 AUDIO_LOCATION_FRONT_RIGHT_OF_CENTER, AUDIO_LOCATION_BACK_CENTER, AUDIO_LOCATION_LOW_FREQUENCY_2,
172 AUDIO_LOCATION_SIDE_LEFT, AUDIO_LOCATION_SIDE_RIGHT, AUDIO_LOCATION_TOP_FRONT_LEFT, AUDIO_LOCATION_TOP_FRONT_RIGHT,
173 AUDIO_LOCATION_TOP_FRONT_CENTER, AUDIO_LOCATION_TOP_CENTER, AUDIO_LOCATION_TOP_BACK_LEFT, AUDIO_LOCATION_TOP_BACK_RIGHT,
174 AUDIO_LOCATION_TOP_SIDE_LEFT, AUDIO_LOCATION_TOP_SIDE_RIGHT, AUDIO_LOCATION_TOP_BACK_CENTER,
175 AUDIO_LOCATION_BOTTOM_FRONT_CENTER, AUDIO_LOCATION_BOTTOM_FRONT_LEFT, AUDIO_LOCATION_BOTTOM_FRONT_RIGHT,
176 AUDIO_LOCATION_FRONT_LEFT_WIDE, AUDIO_LOCATION_FRONT_RIGHT_WIDE, AUDIO_LOCATION_LEFT_SURROUND,
177 AUDIO_LOCATION_RIGHT_SURROUND };
178
179int channel_map_array[] = { PCM_CHANNEL_L, PCM_CHANNEL_R, PCM_CHANNEL_C, PCM_CHANNEL_LFE, PCM_CHANNEL_LB, PCM_CHANNEL_RB, PCM_CHANNEL_FLC,
180 PCM_CHANNEL_FRC, PCM_CHANNEL_CB, PCM_CHANNEL_RS, PCM_CHANNEL_SL, PCM_CHANNEL_SR, PCM_CHANNEL_TFL,
181 PCM_CHANNEL_TFR, PCM_CHANNEL_TFC, PCM_CHANNEL_TC, PCM_CHANNEL_TBL, PCM_CHANNEL_TBR, PCM_CHANNEL_TSL,
182 PCM_CHANNEL_TSR, PCM_CHANNEL_TBC, PCM_CHANNEL_BFC, PCM_CHANNEL_BFL, PCM_CHANNEL_BFR, PCM_CHANNEL_LW,
183 PCM_CHANNEL_RW, PCM_CHANNEL_LS, PCM_CHANNEL_RS };
184
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800185static void *vndk_fwk_lib_handle = NULL;
186static int is_running_with_enhanced_fwk = UNINITIALIZED;
187
188typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
189static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
Florian Pfister1a84f312018-07-19 14:38:18 +0200190
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530191/*
192 * Below enum values are extended from audio_base.h to
Florian Pfister1a84f312018-07-19 14:38:18 +0200193 * to keep encoder and decoder type local to bthost_ipc
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530194 * and audio_hal as these are intended only for handshake
195 * between IPC lib and Audio HAL.
196 */
197typedef enum {
Florian Pfister1a84f312018-07-19 14:38:18 +0200198 CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
199 CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
200 CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
201 CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
202 CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530203#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +0200204 CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530205#endif
Florian Pfister1a84f312018-07-19 14:38:18 +0200206 CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
207 CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
Surendar Karka2febd452018-12-13 17:56:43 +0530208 CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
Zhou Song12c29502019-03-16 10:37:18 +0800209 CODEC_TYPE_APTX_AD_SPEECH = 637534208u, //0x26000000UL
Manisha Agarwald45632b2019-10-17 18:14:28 +0800210 CODEC_TYPE_LC3 = 721420288u, //0x2B000000UL
Aalique Grahame22e49102018-12-18 14:23:57 -0800211 CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u
Florian Pfister1a84f312018-07-19 14:38:18 +0200212}codec_t;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530213
Sharad Sangle95d451b2018-06-19 12:24:20 +0530214/*
215 * enums which describes the APTX Adaptive
216 * channel mode, these values are used by encoder
217 */
218 typedef enum {
219 APTX_AD_CHANNEL_UNCHANGED = -1,
220 APTX_AD_CHANNEL_JOINT_STEREO = 0, // default
221 APTX_AD_CHANNEL_MONO = 1,
222 APTX_AD_CHANNEL_DUAL_MONO = 2,
223 APTX_AD_CHANNEL_STEREO_TWS = 4,
224 APTX_AD_CHANNEL_EARBUD = 8,
225} enc_aptx_ad_channel_mode;
226
227/*
228 * enums which describes the APTX Adaptive
229 * sampling frequency, these values are used
230 * by encoder
231 */
232typedef enum {
233 APTX_AD_SR_UNCHANGED = 0x0,
234 APTX_AD_48 = 0x1, // 48 KHz default
235 APTX_AD_44_1 = 0x2, // 44.1kHz
Zhou Song30e8cea2019-10-22 23:39:25 +0800236 APTX_AD_96 = 0x3, // 96KHz
Sharad Sangle95d451b2018-06-19 12:24:20 +0530237} enc_aptx_ad_s_rate;
238
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530239typedef void (*bt_audio_pre_init_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200240typedef int (*audio_source_open_t)(void);
241typedef int (*audio_source_close_t)(void);
242typedef int (*audio_source_start_t)(void);
243typedef int (*audio_source_stop_t)(void);
244typedef int (*audio_source_suspend_t)(void);
245typedef void (*audio_source_handoff_triggered_t)(void);
246typedef void (*clear_source_a2dpsuspend_flag_t)(void);
247typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
248 uint8_t *num_dev, codec_t *codec_type);
249typedef int (*audio_source_check_a2dp_ready_t)(void);
250typedef int (*audio_is_source_scrambling_enabled_t)(void);
Manisha Agarwala51768b2018-11-01 16:30:52 +0530251typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200252typedef int (*audio_sink_start_t)(void);
253typedef int (*audio_sink_stop_t)(void);
254typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
255typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
256typedef int (*audio_sink_check_a2dp_ready_t)(void);
257typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530258
259enum A2DP_STATE {
260 A2DP_STATE_CONNECTED,
261 A2DP_STATE_STARTED,
262 A2DP_STATE_STOPPED,
263 A2DP_STATE_DISCONNECTED,
264};
265
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800266typedef enum {
267 IMC_TRANSMIT,
268 IMC_RECEIVE,
269} imc_direction_t;
270
271typedef enum {
272 IMC_DISABLE,
273 IMC_ENABLE,
274} imc_status_t;
275
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700276typedef enum {
Zhou Song12c29502019-03-16 10:37:18 +0800277 SWAP_DISABLE,
278 SWAP_ENABLE,
279} swap_status_t;
280
281typedef enum {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700282 MTU_SIZE,
283 PEAK_BIT_RATE,
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530284 BIT_RATE_MODE,
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700285} frame_control_type_t;
286
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800287// --- external function dependency ---
288fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
Weiyin Jiang280ea742020-09-08 20:28:22 +0800289fp_check_a2dp_restore_t fp_check_a2dp_restore_l;
Gautam Manamfbb3ebc2020-10-08 18:06:45 +0530290fp_platform_switch_voice_call_device_post_t fp_platform_switch_voice_call_device_post;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800291
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800292/* PCM config for ABR Feedback hostless front end */
293static struct pcm_config pcm_config_abr = {
294 .channels = 1,
295 .rate = 8000,
296 .period_size = 240,
297 .period_count = 2,
298 .format = PCM_FORMAT_S16_LE,
299 .start_threshold = 0,
300 .stop_threshold = INT_MAX,
301 .avail_min = 0,
302};
303
304/* Adaptive bitrate config for A2DP codecs */
305struct a2dp_abr_config {
306 /* Flag to denote whether Adaptive bitrate is enabled for codec */
307 bool is_abr_enabled;
308 /* Flag to denote whether front end has been opened for ABR */
309 bool abr_started;
310 /* ABR Tx path pcm handle */
311 struct pcm *abr_tx_handle;
Zhou Song12c29502019-03-16 10:37:18 +0800312 /* ABR Rx path pcm handle */
313 struct pcm *abr_rx_handle;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800314 /* ABR Inter Module Communication (IMC) instance ID */
315 uint32_t imc_instance;
316};
317
318static uint32_t instance_id = MAX_INSTANCE_ID;
319
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530320/* structure used to update a2dp state machine
321 * to communicate IPC library
322 * to store DSP encoder configuration information
323 */
324struct a2dp_data {
325 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200326 void *bt_lib_source_handle;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530327 bt_audio_pre_init_t bt_audio_pre_init;
Florian Pfister1a84f312018-07-19 14:38:18 +0200328 audio_source_open_t audio_source_open;
329 audio_source_close_t audio_source_close;
330 audio_source_start_t audio_source_start;
331 audio_source_stop_t audio_source_stop;
332 audio_source_suspend_t audio_source_suspend;
333 audio_source_handoff_triggered_t audio_source_handoff_triggered;
334 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
335 audio_get_enc_config_t audio_get_enc_config;
336 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530337 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200338 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
339 enum A2DP_STATE bt_state_source;
340 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530341 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530342 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200343 bool a2dp_source_started;
344 bool a2dp_source_suspended;
345 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530346 bool is_a2dp_offload_supported;
347 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700348 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530349 /* Mono Mode support for TWS+ */
350 bool is_tws_mono_mode_on;
Manisha Agarwald45632b2019-10-17 18:14:28 +0800351 /* Mono Mode support for LC3 */
352 bool is_lc3_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530353 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800354 /* Adaptive bitrate config for A2DP codecs */
355 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200356
357 void *bt_lib_sink_handle;
358 audio_sink_start_t audio_sink_start;
359 audio_sink_stop_t audio_sink_stop;
360 audio_get_dec_config_t audio_get_dec_config;
361 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
362 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
363 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
364 enum A2DP_STATE bt_state_sink;
365 codec_t bt_decoder_format;
366 uint32_t dec_sampling_rate;
367 uint32_t dec_channels;
368 bool a2dp_sink_started;
369 int a2dp_sink_total_active_session_requests;
Zhou Song12c29502019-03-16 10:37:18 +0800370 bool swb_configured;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530371};
372
373struct a2dp_data a2dp;
374
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800375/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
376 * Structures sent to configure DSP for ABR are defined below.
377 * This data helps DSP configure feedback path (BTSoC to LPASS)
378 * for link quality levels and mapping quality levels to codec
379 * specific bitrate.
380 */
381
382/* Key value pair for link quality level to bitrate mapping. */
383struct bit_rate_level_map_t {
384 uint32_t link_quality_level;
385 uint32_t bitrate;
386};
387
388/* Link quality level to bitrate mapping info sent to DSP. */
389struct quality_level_to_bitrate_info {
390 /* Number of quality levels being mapped.
391 * This will be equal to the size of mapping table.
392 */
393 uint32_t num_levels;
394 /* Quality level to bitrate mapping table */
395 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
396};
397
398/* Structure to set up Inter Module Communication (IMC) between
399 * AFE Decoder and Encoder.
400 */
401struct imc_dec_enc_info {
402 /* Decoder to encoder communication direction.
403 * Transmit = 0 / Receive = 1
404 */
405 uint32_t direction;
406 /* Enable / disable IMC between decoder and encoder */
407 uint32_t enable;
408 /* Purpose of IMC being set up between decoder and encoder.
409 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
410 * is the default value to be sent as purpose.
411 */
412 uint32_t purpose;
413 /* Unique communication instance ID.
414 * purpose and comm_instance together form the actual key
415 * used in IMC registration, which must be the same for
416 * encoder and decoder for which IMC is being set up.
417 */
418 uint32_t comm_instance;
419};
420
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700421/* Structure to control frame size of AAC encoded frames. */
422struct aac_frame_size_control_t {
423 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
424 uint32_t ctl_type;
425 /* Control value
426 * MTU_SIZE: MTU size in bytes
427 * PEAK_BIT_RATE: Peak bitrate in bits per second.
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530428 * BIT_RATE_MODE: Variable bitrate
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700429 */
430 uint32_t ctl_value;
431};
432
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800433/* Structure used for ABR config of AFE encoder and decoder. */
434struct abr_enc_cfg_t {
435 /* Link quality level to bitrate mapping info sent to DSP. */
436 struct quality_level_to_bitrate_info mapping_info;
437 /* Information to set up IMC between decoder and encoder */
438 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700439 /* Flag to indicate whether ABR is enabled */
440 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800441} __attribute__ ((packed));
442
443/* Structure to send configuration for decoder introduced
444 * on AFE Tx path for ABR link quality feedback to BT encoder.
445 */
446struct abr_dec_cfg_t {
447 /* Decoder media format */
448 uint32_t dec_format;
449 /* Information to set up IMC between decoder and encoder */
450 struct imc_dec_enc_info imc_info;
451} __attribute__ ((packed));
452
Zhou Song12c29502019-03-16 10:37:18 +0800453struct aptx_ad_speech_mode_cfg_t
454{
455 uint32_t mode;
456 uint32_t swapping;
457} __attribute__ ((packed));
458
459/* Structure for SWB voice dec config */
460struct aptx_ad_speech_dec_cfg_t {
461 struct abr_dec_cfg_t abr_cfg;
462 struct aptx_ad_speech_mode_cfg_t speech_mode;
463} __attribute__ ((packed));
464
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530465/* START of DSP configurable structures
466 * These values should match with DSP interface defintion
467 */
468
469/* AAC encoder configuration structure. */
470typedef struct aac_enc_cfg_t aac_enc_cfg_t;
471
472/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
473 * supported aac_fmt_flag are ADTS/RAW
474 * supported channel_cfg are Native mode, Mono , Stereo
475 */
476struct aac_enc_cfg_t {
477 uint32_t enc_format;
478 uint32_t bit_rate;
479 uint32_t enc_mode;
480 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530481 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530482 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530483} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530484
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700485struct aac_enc_cfg_v2_t {
486 struct aac_enc_cfg_t aac_enc_cfg;
487 struct aac_frame_size_control_t frame_ctl;
488} __attribute__ ((packed));
489
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530490struct aac_enc_cfg_v3_t {
491 struct aac_enc_cfg_t aac_enc_cfg;
492 struct aac_frame_size_control_t frame_ctl;
493 struct aac_frame_size_control_t aac_key_value_ctl;
494} __attribute__ ((packed));
495
Surendar Karkabbb3c822018-11-12 13:00:38 +0530496typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
497struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200498 uint16_t aac_fmt_flag; /* LATM*/
499 uint16_t audio_object_type; /* LC */
500 uint16_t channels; /* Stereo */
501 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
502 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
503 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530504} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200505
Surendar Karkabbb3c822018-11-12 13:00:38 +0530506typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
507struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200508 uint16_t channels; /* Mono, Stereo */
509 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
510 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530511} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200512
513/* AAC decoder configuration structure. */
514typedef struct aac_dec_cfg_t aac_dec_cfg_t;
515struct aac_dec_cfg_t {
516 uint32_t dec_format;
517 audio_aac_decoder_config_t data;
518} __attribute__ ((packed));
519
520/* SBC decoder configuration structure. */
521typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
522struct sbc_dec_cfg_t {
523 uint32_t dec_format;
524 audio_sbc_decoder_config_t data;
525} __attribute__ ((packed));
526
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530527/* SBC encoder configuration structure. */
528typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
529
530/* supported num_subbands are 4/8
531 * supported blk_len are 4, 8, 12, 16
532 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
533 * supported alloc_method are LOUNDNESS/SNR
534 * supported bit_rate for mono channel is max 320kbps
535 * supported bit rate for stereo channel is max 512 kbps
536 */
537struct sbc_enc_cfg_t{
538 uint32_t enc_format;
539 uint32_t num_subbands;
540 uint32_t blk_len;
541 uint32_t channel_mode;
542 uint32_t alloc_method;
543 uint32_t bit_rate;
544 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530545} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530546
547
548/* supported num_channels are Mono/Stereo
549 * supported channel_mapping for mono is CHANNEL_C
550 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
551 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700552 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530553struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530554{
555 uint32_t enc_format;
556 uint32_t sample_rate;
557 uint16_t num_channels;
558 uint16_t reserved;
559 uint8_t channel_mapping[8];
560 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530561} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530562
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530563struct celt_specific_enc_cfg_t
564{
565 uint32_t bit_rate;
566 uint16_t frame_size;
567 uint16_t complexity;
568 uint16_t prediction_mode;
569 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530570} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530571
572struct celt_enc_cfg_t
573{
574 struct custom_enc_cfg_t custom_cfg;
575 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530576} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700577
578/* sync_mode introduced with APTX V2 libraries
579 * sync mode: 0x0 = stereo sync mode
580 * 0x01 = dual mono sync mode
581 * 0x02 = dual mono with no sync on either L or R codewords
582 */
583struct aptx_v2_enc_cfg_ext_t
584{
585 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530586} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700587
588/* APTX struct for combining custom enc and V2 fields */
589struct aptx_enc_cfg_t
590{
591 struct custom_enc_cfg_t custom_cfg;
592 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530593} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700594
Sharad Sangle95d451b2018-06-19 12:24:20 +0530595/* APTX AD structure */
596struct aptx_ad_enc_cfg_ext_t
597{
598 uint32_t sampling_freq;
599 uint32_t mtu;
600 uint32_t channel_mode;
601 uint32_t min_sink_modeA;
602 uint32_t max_sink_modeA;
603 uint32_t min_sink_modeB;
604 uint32_t max_sink_modeB;
605 uint32_t min_sink_modeC;
606 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530607 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530608} __attribute__ ((packed));
609
610struct aptx_ad_enc_cfg_t
611{
612 struct custom_enc_cfg_t custom_cfg;
613 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
614 struct abr_enc_cfg_t abr_cfg;
615} __attribute__ ((packed));
616
Zhou Song8fccbb62019-03-20 01:08:19 +0800617struct aptx_ad_enc_cfg_ext_r2_t
618{
619 uint32_t sampling_freq;
620 uint32_t mtu;
621 uint32_t channel_mode;
622 uint32_t min_sink_modeA;
623 uint32_t max_sink_modeA;
624 uint32_t min_sink_modeB;
625 uint32_t max_sink_modeB;
626 uint32_t min_sink_modeC;
627 uint32_t max_sink_modeC;
628 uint32_t mode;
629 uint32_t input_mode;
630 uint32_t fade_duration;
631 uint8_t sink_cap[11];
632} __attribute__ ((packed));
633
634struct aptx_ad_enc_cfg_r2_t
635{
636 struct custom_enc_cfg_t custom_cfg;
637 struct aptx_ad_enc_cfg_ext_r2_t aptx_ad_cfg;
638 struct abr_enc_cfg_t abr_cfg;
639} __attribute__ ((packed));
640
Zhou Song12c29502019-03-16 10:37:18 +0800641/* APTX AD SPEECH structure */
642struct aptx_ad_speech_enc_cfg_t
643{
644 struct custom_enc_cfg_t custom_cfg;
645 /* Information to set up IMC between decoder and encoder */
646 struct imc_dec_enc_info imc_info;
647 struct aptx_ad_speech_mode_cfg_t speech_mode;
648} __attribute__ ((packed));
649
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530650struct ldac_specific_enc_cfg_t
651{
652 uint32_t bit_rate;
653 uint16_t channel_mode;
654 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530655} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530656
657struct ldac_enc_cfg_t
658{
659 struct custom_enc_cfg_t custom_cfg;
660 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800661 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530662} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530663
Manisha Agarwald45632b2019-10-17 18:14:28 +0800664/* LC3 structure */
665struct lc3_stream_info_t
666{
667 uint32_t stream_id;
668 uint32_t direction;
669 uint32_t channel_mask_lsw;
670 uint32_t channel_mask_msw;
671} __attribute__ ((packed));
672
673struct lc3_stream_map_info_t
674{
675 uint32_t stream_map_size;
676 struct lc3_stream_info_t streamMap[16];
677} __attribute__ ((packed));
678
679struct lc3_stream_map_ext_t
680{
681 uint32_t audio_location;
682 uint8_t stream_id;
683 uint8_t direction;
684} __attribute__ ((packed));
685
686struct lc3_config_ext_t
687{
688 uint32_t api_version;
689 uint32_t sampling_freq;
690 uint32_t max_octets_per_frame;
691 uint32_t frame_duration;//7.5msec, 10msec
692 uint32_t bit_depth;
693 uint32_t num_blocks;
694 uint8_t default_q_level;
695 uint8_t vendor_specific[16]; // this indicates LC3/LC3Q. 0 => LC3 1.0 , 1 => LC3 1.1
696 uint32_t mode;
697} __attribute__ ((packed));
698
699struct lc3_dec_cfg_ext_t
700{
701 struct lc3_config_ext_t fromAirConfig;
702 uint32_t decoder_output_channel;
703 uint32_t stream_map_size;
704 struct lc3_stream_map_ext_t streamMapIn[16];
705} __attribute__ ((packed));
706
707struct lc3_enc_cfg_ext_t
708{
709 struct lc3_config_ext_t toAirConfig;
710 uint32_t stream_map_size;
711 struct lc3_stream_map_ext_t streamMapOut[16];
712} __attribute__ ((packed));
713
714struct lc3_dec_codec_cfg_t
715{
716 struct lc3_stream_map_info_t streamMapToAir;
717} __attribute__ ((packed));
718
719struct lc3_dec_cfg_t
720{
721 struct abr_dec_cfg_t abr_cfg;
722 struct lc3_dec_codec_cfg_t dec_codec;
723} __attribute__ ((packed));
724
725struct lc3_enc_codec_cfg_t
726{
727 struct lc3_enc_cfg_ext_t to_Air_cfg;
728 struct lc3_stream_map_info_t streamMapToAir;
729} __attribute__ ((packed));
730
731struct lc3_enc_cfg_t
732{
733 uint32_t enc_format;
734 struct imc_dec_enc_info imc_info;
735 struct lc3_enc_codec_cfg_t enc_codec;
736} __attribute__ ((packed));
737
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530738/* In LE BT source code uses system/audio.h for below
739 * structure definition. To avoid multiple definition
740 * compilation error for audiohal in LE , masking structure
741 * definition under "LINUX_ENABLED" which is defined only
742 * in LE
743 */
744#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530745/* TODO: Define the following structures only for O using PLATFORM_VERSION */
746/* Information about BT SBC encoder configuration
747 * This data is used between audio HAL module and
748 * BT IPC library to configure DSP encoder
749 */
750typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530751 uint32_t subband; /* 4, 8 */
752 uint32_t blk_len; /* 4, 8, 12, 16 */
753 uint16_t sampling_rate; /*44.1khz,48khz*/
754 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
755 uint8_t alloc; /*0(Loudness),1(SNR)*/
756 uint8_t min_bitpool; /* 2 */
757 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
758 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530759 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530760} audio_sbc_encoder_config;
761
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530762/* Information about BT APTX encoder configuration
763 * This data is used between audio HAL module and
764 * BT IPC library to configure DSP encoder
765 */
766typedef struct {
767 uint16_t sampling_rate;
768 uint8_t channels;
769 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530770 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700771} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530772
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700773typedef struct {
Zhou Song8fccbb62019-03-20 01:08:19 +0800774 uint32_t sampling_rate;
775 uint32_t mtu;
776 int32_t channel_mode;
777 uint32_t min_sink_modeA;
778 uint32_t max_sink_modeA;
779 uint32_t min_sink_modeB;
780 uint32_t max_sink_modeB;
781 uint32_t min_sink_modeC;
782 uint32_t max_sink_modeC;
783 uint32_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530784 uint8_t TTP_modeA_low;
785 uint8_t TTP_modeA_high;
786 uint8_t TTP_modeB_low;
787 uint8_t TTP_modeB_high;
Zhou Song8fccbb62019-03-20 01:08:19 +0800788 uint8_t TTP_TWS_low;
789 uint8_t TTP_TWS_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530790 uint32_t bits_per_sample;
Zhou Song8fccbb62019-03-20 01:08:19 +0800791 uint32_t input_mode;
792 uint32_t fade_duration;
793 uint8_t sink_cap[11];
Sharad Sangle95d451b2018-06-19 12:24:20 +0530794} audio_aptx_ad_config;
795
796typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700797 uint16_t sampling_rate;
798 uint8_t channels;
799 uint32_t bitrate;
800 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530801 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700802} audio_aptx_dual_mono_config;
803
804typedef union {
805 audio_aptx_default_config *default_cfg;
806 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530807 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700808} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530809
810/* Information about BT AAC encoder configuration
811 * This data is used between audio HAL module and
812 * BT IPC library to configure DSP encoder
813 */
814typedef struct {
815 uint32_t enc_mode; /* LC, SBR, PS */
816 uint16_t format_flag; /* RAW, ADTS */
817 uint16_t channels; /* 1-Mono, 2-Stereo */
818 uint32_t sampling_rate;
819 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530820 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530821} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530822#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700823
824typedef struct {
825 audio_aac_encoder_config audio_aac_enc_cfg;
826 struct aac_frame_size_control_t frame_ctl;
827} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530828
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530829typedef struct {
830 audio_aac_encoder_config audio_aac_enc_cfg;
831 struct aac_frame_size_control_t frame_ctl;
832 uint8_t size_control_struct;
833 struct aac_frame_size_control_t* frame_ptr_ctl;
834} audio_aac_encoder_config_v3;
835
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530836/* Information about BT CELT encoder configuration
837 * This data is used between audio HAL module and
838 * BT IPC library to configure DSP encoder
839 */
840typedef struct {
841 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
842 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
843 uint16_t frame_size; /* 64-128-256-512, 512 */
844 uint16_t complexity; /* 0-10, 1 */
845 uint16_t prediction_mode; /* 0-1-2, 0 */
846 uint16_t vbr_flag; /* 0-1, 0*/
847 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530848 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530849} audio_celt_encoder_config;
850
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530851/* Information about BT LDAC encoder configuration
852 * This data is used between audio HAL module and
853 * BT IPC library to configure DSP encoder
854 */
855typedef struct {
856 uint32_t sampling_rate; /*44100,48000,88200,96000*/
857 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
858 uint16_t channel_mode; /* 0, 4, 2, 1*/
859 uint16_t mtu; /*679*/
Aniket Kumar Latad0196282019-05-09 14:24:17 -0700860 uint32_t bits_per_sample;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800861 bool is_abr_enabled;
862 struct quality_level_to_bitrate_info level_to_bitrate_map;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530863} audio_ldac_encoder_config;
864
Surendar Karkabbb3c822018-11-12 13:00:38 +0530865/* Information about BT AAC decoder configuration
866 * This data is used between audio HAL module and
867 * BT IPC library to configure DSP decoder
868 */
869typedef struct {
870 uint16_t aac_fmt_flag; /* LATM*/
871 uint16_t audio_object_type; /* LC */
872 uint16_t channels; /* Stereo */
873 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
874 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
875 44.1k, 48k, 64k, 88.2k, 96k */
876} audio_aac_dec_config_t;
877
878/* Information about BT SBC decoder configuration
879 * This data is used between audio HAL module and
880 * BT IPC library to configure DSP decoder
881 */
882typedef struct {
883 uint16_t channels; /* Mono, Stereo */
884 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
885 44.1k, 48k, 64k, 88.2k, 96k */
886}audio_sbc_dec_config_t;
887
Manisha Agarwald45632b2019-10-17 18:14:28 +0800888/* Information about BT LC3 encoder configuration
889 * This data is used between audio HAL module and
890 * BT IPC library to configure DSP encoder
891 */
892typedef struct {
893 uint32_t api_version;
894 uint32_t sampling_freq;
895 uint32_t max_octets_per_frame;
896 uint32_t frame_duration;//7.5msec, 10msec
897 uint32_t bit_depth;
898 uint32_t num_blocks;
899 uint8_t default_q_level;
900 uint8_t vendor_specific[16]; // this indicates LC3/LC3Q. 0 => LC3 1.0 , 1 => LC3 1.1
901 uint32_t mode;
902} lc3_config_t;
903
904typedef struct {
905 uint32_t audio_location;
906 uint8_t stream_id;
907 uint8_t direction;
908} lc3_stream_map_t;
909
910typedef struct {
911 lc3_config_t toAirConfig;
912 uint8_t stream_map_size;
913 lc3_stream_map_t* streamMapOut;
914} lc3_encoder_config_t;
915
916typedef struct {
917 lc3_config_t fromAirConfig;
918 uint32_t decoder_output_channel;
919 uint8_t stream_map_size;
920 lc3_stream_map_t* streamMapIn;
921} lc3_decoder_config_t;
922
923typedef struct {
924 lc3_encoder_config_t enc_config;
925 lc3_decoder_config_t dec_config;
926} audio_lc3_codec_config_t;
927
928
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530929/*********** END of DSP configurable structures ********************/
930
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530931static void update_offload_codec_capabilities()
932{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530933
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530934 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800935 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
936 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
937
938 ALOGD("%s: A2DP offload supported = %d",__func__,
939 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530940}
941
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800942static int stop_abr()
943{
944 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800945 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800946 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800947 int ret = 0;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800948
949 /* This function can be used if !abr_started for clean up */
950 ALOGV("%s: enter", __func__);
951
952 // Close hostless front end
953 if (a2dp.abr_config.abr_tx_handle != NULL) {
954 pcm_close(a2dp.abr_config.abr_tx_handle);
955 a2dp.abr_config.abr_tx_handle = NULL;
956 }
Zhou Song12c29502019-03-16 10:37:18 +0800957 if (a2dp.abr_config.abr_rx_handle != NULL) {
958 pcm_close(a2dp.abr_config.abr_rx_handle);
959 a2dp.abr_config.abr_rx_handle = NULL;
960 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800961 a2dp.abr_config.abr_started = false;
962 a2dp.abr_config.imc_instance = 0;
963
964 // Reset BT driver mixer control for ABR usecase
965 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
966 MIXER_SET_FEEDBACK_CHANNEL);
967 if (!ctl_set_bt_feedback_channel) {
968 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800969 ret = -ENOSYS;
970 } else if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800971 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800972 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800973 }
974
975 // Reset ABR Tx feedback path
976 ALOGV("%s: Disable ABR Tx feedback path", __func__);
977 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
978 MIXER_ABR_TX_FEEDBACK_PATH);
979 if (!ctl_abr_tx_path) {
980 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800981 ret = -ENOSYS;
982 } else if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800983 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800984 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800985 }
986
Zhou Song12c29502019-03-16 10:37:18 +0800987 // Reset ABR Rx feedback path
988 ALOGV("%s: Disable ABR Rx feedback path", __func__);
989 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
990 MIXER_ABR_RX_FEEDBACK_PATH);
991 if (!ctl_abr_rx_path) {
992 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
993 ret = -ENOSYS;
994 } else if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 0) != 0) {
995 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
996 ret = -ENOSYS;
997 }
998
999 return ret;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001000}
1001
1002static int start_abr()
1003{
1004 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001005 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001006 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
1007 int abr_device_id;
1008 int ret = 0;
1009
1010 if (!a2dp.abr_config.is_abr_enabled) {
1011 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
1012 return -ENOSYS;
1013 }
1014
1015 if (a2dp.abr_config.abr_started) {
1016 ALOGI("%s: ABR has already started", __func__);
1017 return ret;
1018 }
1019
1020 // Enable Slimbus 7 Tx feedback path
1021 ALOGV("%s: Enable ABR Tx feedback path", __func__);
1022 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
1023 MIXER_ABR_TX_FEEDBACK_PATH);
1024 if (!ctl_abr_tx_path) {
1025 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
1026 return -ENOSYS;
1027 }
1028 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
1029 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
1030 return -ENOSYS;
1031 }
1032
1033 // Notify ABR usecase information to BT driver to distinguish
1034 // between SCO and feedback usecase
1035 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
1036 MIXER_SET_FEEDBACK_CHANNEL);
1037 if (!ctl_set_bt_feedback_channel) {
1038 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +08001039 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001040 }
1041 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
1042 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +08001043 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001044 }
1045
1046 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001047 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001048 PCM_CAPTURE);
1049 if (!a2dp.abr_config.abr_tx_handle) {
1050 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
1051 abr_device_id, PCM_IN,
1052 &pcm_config_abr);
Zhou Song12c29502019-03-16 10:37:18 +08001053 if (a2dp.abr_config.abr_tx_handle == NULL) {
1054 ALOGE("%s: Can't open abr tx device", __func__);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001055 goto fail;
Zhou Song12c29502019-03-16 10:37:18 +08001056 }
1057 if (!(pcm_is_ready(a2dp.abr_config.abr_tx_handle) &&
1058 !pcm_start(a2dp.abr_config.abr_tx_handle))) {
1059 ALOGE("%s: tx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
1060 goto fail;
1061 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001062 }
Zhou Song12c29502019-03-16 10:37:18 +08001063
1064 // Enable Slimbus 7 Rx feedback path for HD Voice use case
1065 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH) {
1066 ALOGV("%s: Enable ABR Rx feedback path", __func__);
1067 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
1068 MIXER_ABR_RX_FEEDBACK_PATH);
1069 if (!ctl_abr_rx_path) {
1070 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
1071 goto fail;
1072 }
1073 if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 1) != 0) {
1074 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
1075 goto fail;
1076 }
1077
1078 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
1079 ALOGE("%s: Failed to set BT usecase", __func__);
1080 goto fail;
1081 }
1082
1083 // Open hostless front end and prepare ABR Rx path
1084 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
1085 PCM_PLAYBACK);
1086 if (!a2dp.abr_config.abr_rx_handle) {
1087 a2dp.abr_config.abr_rx_handle = pcm_open(a2dp.adev->snd_card,
1088 abr_device_id, PCM_OUT,
1089 &pcm_config_abr);
1090 if (a2dp.abr_config.abr_rx_handle == NULL) {
1091 ALOGE("%s: Can't open abr rx device", __func__);
1092 goto fail;
1093 }
1094 if (!(pcm_is_ready(a2dp.abr_config.abr_rx_handle) &&
1095 !pcm_start(a2dp.abr_config.abr_rx_handle))) {
1096 ALOGE("%s: rx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_rx_handle));
1097 goto fail;
1098 }
1099 }
1100 }
1101
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001102 a2dp.abr_config.abr_started = true;
1103
1104 return ret;
1105
1106fail:
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001107 stop_abr();
1108 return -ENOSYS;
1109}
1110
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001111static int check_if_enhanced_fwk() {
1112
1113 int is_enhanced_fwk = 1;
1114 //dlopen lib
1115 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
1116 if (vndk_fwk_lib_handle != NULL) {
1117 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
1118 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
1119 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
1120 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
1121 __func__);
1122 is_enhanced_fwk = 1;
1123 } else {
1124 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
1125 }
1126 }
1127 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
1128 return is_enhanced_fwk;
1129}
1130
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301131static void open_a2dp_source() {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301132 int ret = 0;
1133
Florian Pfister1a84f312018-07-19 14:38:18 +02001134 ALOGD(" Open A2DP source start ");
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301135
1136 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
1137 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
1138 ALOGD("calling BT stream open");
1139 ret = a2dp.audio_source_open();
1140 if(ret != 0) {
1141 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Srinu Jellada99a592019-01-25 16:50:52 +05301142 }
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301143 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301144 } else {
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301145 ALOGD("Called a2dp open with improper state %d", a2dp.bt_state_source);
1146 }
1147 } else {
1148 ALOGE("a2dp handle is not identified, Ignoring open request");
1149 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
1150 }
1151}
1152/* API to open BT IPC library to start IPC communication for BT Source*/
1153static void a2dp_source_init()
1154{
1155 ALOGD("a2dp_source_init START");
1156 if (a2dp.bt_lib_source_handle == NULL) {
1157 ALOGD("Requesting for BT lib handle");
1158 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
1159 if (a2dp.bt_lib_source_handle == NULL) {
1160 ALOGE("%s: dlopen failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
1161 return;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301162 }
1163 }
1164
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301165 a2dp.bt_audio_pre_init = (bt_audio_pre_init_t)
1166 dlsym(a2dp.bt_lib_source_handle, "bt_audio_pre_init");
Srinu Jellada99a592019-01-25 16:50:52 +05301167 a2dp.audio_source_open = (audio_source_open_t)
1168 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
1169 a2dp.audio_source_start = (audio_source_start_t)
1170 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
1171 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
1172 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
1173 a2dp.audio_source_suspend = (audio_source_suspend_t)
1174 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
1175 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
1176 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
1177 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1178 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
1179 a2dp.audio_source_stop = (audio_source_stop_t)
1180 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
1181 a2dp.audio_source_close = (audio_source_close_t)
1182 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
1183 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
1184 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
1185 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
1186 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
1187 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1188 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1189 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1190 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1191
Srinu Jella793f5302019-07-19 15:53:55 +05301192 if (a2dp.bt_lib_source_handle && a2dp.bt_audio_pre_init) {
1193 ALOGD("calling BT module preinit");
1194 // fwk related check's will be done in the BT layer
1195 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001196 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001197}
1198
1199/* API to open BT IPC library to start IPC communication for BT Sink*/
1200static void open_a2dp_sink()
1201{
1202 ALOGD(" Open A2DP input start ");
1203 if (a2dp.bt_lib_sink_handle == NULL){
1204 ALOGD(" Requesting for BT lib handle");
1205 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1206
1207 if (a2dp.bt_lib_sink_handle == NULL) {
1208 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1209 } else {
1210 a2dp.audio_sink_start = (audio_sink_start_t)
1211 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1212 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1213 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1214 a2dp.audio_sink_stop = (audio_sink_stop_t)
1215 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1216 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1217 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1218 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1219 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1220 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301221 }
1222}
1223
1224static int close_a2dp_output()
1225{
1226 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001227
1228 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1229 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301230 return -ENOSYS;
1231 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001232
1233 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1234 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001235 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001236 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301237 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001238 a2dp.a2dp_source_started = false;
1239 a2dp.a2dp_source_total_active_session_requests = 0;
1240 a2dp.a2dp_source_suspended = false;
1241 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301242 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301243 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001244 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001245 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1246 stop_abr();
1247 a2dp.abr_config.is_abr_enabled = false;
1248 a2dp.abr_config.abr_started = false;
1249 a2dp.abr_config.imc_instance = 0;
1250 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001251 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301252 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001253
1254 return 0;
1255}
1256
1257static int close_a2dp_input()
1258{
1259 ALOGV("%s\n",__func__);
1260
1261 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1262 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1263 return -ENOSYS;
1264 }
1265
1266 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1267 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001268 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001269 ALOGE("failed close a2dp sink control path from BT library");
1270 }
1271 a2dp.a2dp_sink_started = false;
1272 a2dp.a2dp_sink_total_active_session_requests = 0;
1273 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1274 a2dp.dec_sampling_rate = 48000;
1275 a2dp.dec_channels = 2;
1276 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301277
1278 return 0;
1279}
1280
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301281static void a2dp_check_and_set_scrambler()
1282{
1283 bool scrambler_mode = false;
1284 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001285 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1286 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301287
1288 if (scrambler_mode) {
1289 //enable scrambler in dsp
1290 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1291 MIXER_SCRAMBLER_MODE);
1292 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001293 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301294 return;
1295 } else {
1296 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1297 ALOGE("%s: Could not set scrambler mode", __func__);
1298 return;
1299 }
1300 }
1301 }
1302}
1303
Florian Pfister1a84f312018-07-19 14:38:18 +02001304static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301305{
Florian Pfister1a84f312018-07-19 14:38:18 +02001306 char *rate_str = NULL, *channels = NULL;
1307 uint32_t sampling_rate;
1308 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1309 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301310
Florian Pfister1a84f312018-07-19 14:38:18 +02001311 if (direction == SINK) {
1312 sampling_rate = a2dp.dec_sampling_rate;
1313 } else {
1314 sampling_rate = a2dp.enc_sampling_rate;
1315 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001316 /*
1317 * For LDAC encoder and AAC decoder open slimbus port at
1318 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1319 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1320 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001321 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301322 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001323 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1324 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301325 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1326 sampling_rate = sampling_rate *2;
1327 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001328
Manisha Agarwald45632b2019-10-17 18:14:28 +08001329 if (a2dp.bt_encoder_format == CODEC_TYPE_LC3)
1330 sampling_rate = SAMPLING_RATE_96K;
1331
Aalique Grahame22e49102018-12-18 14:23:57 -08001332 // No need to configure backend for PCM format.
1333 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1334 return 0;
1335 }
1336
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301337 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301338 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301339 case 44100:
1340 rate_str = "KHZ_44P1";
1341 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301342 case 88200:
1343 rate_str = "KHZ_88P2";
1344 break;
1345 case 96000:
1346 rate_str = "KHZ_96";
1347 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001348 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301349 default:
1350 rate_str = "KHZ_48";
1351 break;
1352 }
1353
Florian Pfister1a84f312018-07-19 14:38:18 +02001354 if (direction == SINK) {
1355 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1356 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301357 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001358 } else {
1359 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1360 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301361 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001362 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301363 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001364
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301365 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1366 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301367 is_configured = false;
1368 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301369 }
1370
Surendar Karka2febd452018-12-13 17:56:43 +05301371 if (direction == SOURCE) {
1372 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001373 if (a2dp.abr_config.is_abr_enabled) {
Manisha Agarwald45632b2019-10-17 18:14:28 +08001374 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH ||
1375 a2dp.bt_encoder_format == CODEC_TYPE_LC3)
Zhou Song12c29502019-03-16 10:37:18 +08001376 rate_str = SPEECH_TX_SAMPLE_RATE;
1377 else
1378 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301379
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301380 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1381 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1382 MIXER_SOURCE_SAMPLE_RATE_TX);
1383 if (!ctl_sample_rate) {
1384 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1385 is_configured = false;
1386 goto fail;
1387 }
1388
1389 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1390 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1391 is_configured = false;
1392 goto fail;
1393 }
Surendar Karka2febd452018-12-13 17:56:43 +05301394 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301395 }
1396 } else {
1397 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1398 MIXER_SAMPLE_RATE_TX is not supported */
1399 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1400 MIXER_SAMPLE_RATE_DEFAULT);
1401 if (!ctl_sample_rate) {
1402 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301403 is_configured = false;
1404 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301405 }
1406
1407 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1408 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001409 is_configured = false;
1410 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301411 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301412 }
1413
Florian Pfister1a84f312018-07-19 14:38:18 +02001414 if (direction == SINK) {
1415 switch (a2dp.dec_channels) {
1416 case 1:
1417 channels = "One";
1418 break;
1419 case 2:
1420 default:
1421 channels = "Two";
1422 break;
1423 }
1424
Ramu Gottipati02809682018-12-19 16:46:12 +05301425 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001426 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1427 MIXER_AFE_SINK_CHANNELS);
1428 } else {
1429 //Configure AFE enc channels
1430 switch (a2dp.enc_channels) {
1431 case 1:
1432 channels = "One";
1433 break;
1434 case 2:
1435 default:
1436 channels = "Two";
1437 break;
1438 }
1439
Ramu Gottipati02809682018-12-19 16:46:12 +05301440 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001441 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1442 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301443 }
1444
Florian Pfister1a84f312018-07-19 14:38:18 +02001445 if (!ctrl_channels) {
1446 ALOGE(" ERROR AFE channels mixer control not identified");
1447 } else {
1448 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301449 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001450 is_configured = false;
1451 goto fail;
1452 }
1453 }
1454 is_configured = true;
1455fail:
1456 return is_configured;
1457}
1458
Aniket Kumar Lata990de552019-07-11 14:20:23 -07001459bool a2dp_set_source_backend_cfg()
1460{
1461 if (a2dp.a2dp_source_started && !a2dp.a2dp_source_suspended)
1462 return a2dp_set_backend_cfg(SOURCE);
1463
1464 return false;
1465}
1466
Surendar Karkabbb3c822018-11-12 13:00:38 +05301467bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001468{
1469 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1470 struct aac_dec_cfg_t aac_dsp_cfg;
1471 bool is_configured = false;
1472 int ret = 0;
1473
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001474 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001475 return false;
1476
Surendar Karka2febd452018-12-13 17:56:43 +05301477 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001478 if (!ctl_dec_data) {
1479 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1480 is_configured = false;
1481 goto fail;
1482 }
1483
1484 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1485 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1486 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1487 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1488 switch(aac_bt_cfg->audio_object_type) {
1489 case 0:
1490 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301491 break;
1492 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001493 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1494 break;
1495 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301496 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001497 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301498 break;
1499 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001500 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1501 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1502 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1503 sizeof(struct aac_dec_cfg_t));
1504 if (ret != 0) {
1505 ALOGE("%s: failed to set AAC decoder config", __func__);
1506 is_configured = false;
1507 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001508 }
1509
Florian Pfister1a84f312018-07-19 14:38:18 +02001510 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1511 MIXER_DEC_BIT_FORMAT);
1512 if (!ctrl_bit_format) {
1513 ALOGE(" ERROR Dec bit format mixer control not identified");
1514 is_configured = false;
1515 goto fail;
1516 }
1517 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1518 if (ret != 0) {
1519 ALOGE("%s: Failed to set bit format to decoder", __func__);
1520 is_configured = false;
1521 goto fail;
1522 }
1523
1524 is_configured = true;
1525 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1526 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1527 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1528 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1529 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1530fail:
1531 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301532}
1533
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301534static int a2dp_set_bit_format(uint32_t enc_bit_format)
1535{
1536 const char *bit_format = NULL;
1537 struct mixer_ctl *ctrl_bit_format = NULL;
1538
1539 // Configure AFE Input Bit Format
1540 switch (enc_bit_format) {
1541 case 32:
1542 bit_format = "S32_LE";
1543 break;
1544 case 24:
1545 bit_format = "S24_LE";
1546 break;
1547 case 16:
1548 default:
1549 bit_format = "S16_LE";
1550 break;
1551 }
1552
1553 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1554 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1555 MIXER_ENC_BIT_FORMAT);
1556 if (!ctrl_bit_format) {
1557 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1558 return -ENOSYS;
1559 }
1560 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1561 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1562 return -ENOSYS;
1563 }
1564 return 0;
1565}
1566
Surendar Karka2febd452018-12-13 17:56:43 +05301567static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301568{
Surendar Karka2febd452018-12-13 17:56:43 +05301569 const char *rate_str = "KHZ_8", *channels = "Zero";
1570 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1571 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301572
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001573 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301574 if (direction == SINK) {
1575 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1576 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1577 MIXER_SINK_SAMPLE_RATE);
1578 } else {
1579 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1580 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1581 MIXER_SAMPLE_RATE_RX);
1582 }
1583 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301584
Surendar Karka2febd452018-12-13 17:56:43 +05301585 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1586 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301587 return -ENOSYS;
1588 }
Zhou Song12c29502019-03-16 10:37:18 +08001589 if (a2dp.abr_config.is_abr_enabled) {
1590 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1591 MIXER_SOURCE_SAMPLE_RATE_TX);
1592 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301593 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1594 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001595 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301596
Zhou Song12c29502019-03-16 10:37:18 +08001597 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1598 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1599 return -ENOSYS;
1600 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301601 }
1602 } else {
1603
Surendar Karka2febd452018-12-13 17:56:43 +05301604 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301605 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301606 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301607 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1608 return -ENOSYS;
1609 }
1610
Surendar Karka2febd452018-12-13 17:56:43 +05301611 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301612 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1613 return -ENOSYS;
1614 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001615 }
1616
1617 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301618 if (direction == SINK) {
1619 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1620 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1621 MIXER_AFE_SINK_CHANNELS);
1622 } else {
1623 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1624 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1625 MIXER_AFE_IN_CHANNELS);
1626 }
1627 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001628 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1629 return -ENOSYS;
1630 }
Surendar Karka2febd452018-12-13 17:56:43 +05301631 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001632 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1633 return -ENOSYS;
1634 }
1635
1636 return 0;
1637}
1638
1639/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301640static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001641{
1642 struct mixer_ctl *ctl_dec_data = NULL;
1643 struct abr_dec_cfg_t dec_cfg;
1644 int ret = 0;
1645
1646 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301647 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001648 if (!ctl_dec_data) {
1649 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1650 return false;
1651 }
1652 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1653 dec_cfg.dec_format = dec_format;
1654 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1655 dec_cfg.imc_info.enable = IMC_ENABLE;
1656 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1657 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1658
1659 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1660 sizeof(dec_cfg));
1661 if (ret != 0) {
1662 ALOGE("%s: Failed to set decoder config", __func__);
1663 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301664 }
Surendar Karka2febd452018-12-13 17:56:43 +05301665 }
1666
1667 return true;
1668}
1669
Surendar Karkabbb3c822018-11-12 13:00:38 +05301670bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001671{
1672 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1673 struct sbc_dec_cfg_t sbc_dsp_cfg;
1674 bool is_configured = false;
1675 int ret = 0;
1676
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001677 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001678 goto fail;
1679
Surendar Karka2febd452018-12-13 17:56:43 +05301680 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001681 if (!ctl_dec_data) {
1682 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1683 is_configured = false;
1684 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301685 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001686
Florian Pfister1a84f312018-07-19 14:38:18 +02001687 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1688 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1689 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1690 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1691 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1692 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301693
Florian Pfister1a84f312018-07-19 14:38:18 +02001694 if (ret != 0) {
1695 ALOGE("%s: failed to set SBC decoder config", __func__);
1696 is_configured = false;
1697 goto fail;
1698 }
1699
1700 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1701 MIXER_DEC_BIT_FORMAT);
1702 if (!ctrl_bit_format) {
1703 ALOGE(" ERROR Dec bit format mixer control not identified");
1704 is_configured = false;
1705 goto fail;
1706 }
1707 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1708 if (ret != 0) {
1709 ALOGE("%s: Failed to set bit format to decoder", __func__);
1710 is_configured = false;
1711 goto fail;
1712 }
1713
1714 is_configured = true;
1715 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1716 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1717 a2dp.dec_channels = 1;
1718 else
1719 a2dp.dec_channels = 2;
1720 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1721 ALOGV("Successfully updated SBC dec format");
1722fail:
1723 return is_configured;
1724}
1725
Florian Pfister1a84f312018-07-19 14:38:18 +02001726/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301727static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301728{
Florian Pfister1a84f312018-07-19 14:38:18 +02001729 void *codec_info = NULL;
1730 codec_t codec_type = CODEC_TYPE_INVALID;
1731 bool is_configured = false;
1732 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301733
Florian Pfister1a84f312018-07-19 14:38:18 +02001734 if (!a2dp.audio_get_dec_config) {
1735 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1736 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301737 }
1738
Surendar Karka2febd452018-12-13 17:56:43 +05301739 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001740 if (!ctl_dec_data) {
1741 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1742 is_configured = false;
1743 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301744 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001745 codec_info = a2dp.audio_get_dec_config(&codec_type);
1746 switch(codec_type) {
1747 case CODEC_TYPE_SBC:
1748 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301749 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001750 break;
1751 case CODEC_TYPE_AAC:
1752 ALOGD(" AAC decoder supported BT device");
1753 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301754 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001755 break;
1756 default:
1757 ALOGD(" Received Unsupported decoder format");
1758 is_configured = false;
1759 break;
1760 }
1761 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301762}
1763
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301764/* API to configure SBC DSP encoder */
1765bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1766{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301767 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301768 struct sbc_enc_cfg_t sbc_dsp_cfg;
1769 bool is_configured = false;
1770 int ret = 0;
1771
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001772 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301773 return false;
1774
1775 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1776 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001777 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301778 is_configured = false;
1779 goto fail;
1780 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301781 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001782 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301783 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1784 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1785 switch(sbc_bt_cfg->channels) {
1786 case 0:
1787 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1788 break;
1789 case 1:
1790 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1791 break;
1792 case 3:
1793 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1794 break;
1795 case 2:
1796 default:
1797 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1798 break;
1799 }
1800 if (sbc_bt_cfg->alloc)
1801 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1802 else
1803 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1804 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1805 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1806 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1807 sizeof(struct sbc_enc_cfg_t));
1808 if (ret != 0) {
1809 ALOGE("%s: failed to set SBC encoder config", __func__);
1810 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301811 goto fail;
1812 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301813 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301814 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301815 is_configured = false;
1816 goto fail;
1817 }
1818 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001819 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301820 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301821
1822 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1823 a2dp.enc_channels = 1;
1824 else
1825 a2dp.enc_channels = 2;
1826
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301827 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1828 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301829fail:
1830 return is_configured;
1831}
1832
Manish Dewangan6a252632017-12-04 17:27:44 +05301833#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301834static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1835 audio_aptx_encoder_config *aptx_bt_cfg)
1836{
1837 int ret = 0;
1838
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001839 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301840 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1841 aptx_dsp_cfg, aptx_bt_cfg);
1842 return -EINVAL;
1843 }
1844
1845 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301846 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301847
1848
1849 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1850 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1851 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1852 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1853 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1854 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1855 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1856 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1857 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301858 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301859 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1860 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1861 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1862 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1863
1864
1865 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1866 case APTX_AD_CHANNEL_UNCHANGED:
1867 case APTX_AD_CHANNEL_JOINT_STEREO:
1868 case APTX_AD_CHANNEL_DUAL_MONO:
1869 case APTX_AD_CHANNEL_STEREO_TWS:
1870 case APTX_AD_CHANNEL_EARBUD:
1871 default:
1872 a2dp.enc_channels = CH_STEREO;
1873 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1874 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1875 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1876 break;
1877 case APTX_AD_CHANNEL_MONO:
1878 a2dp.enc_channels = CH_MONO;
1879 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1880 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001881 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301882 }
1883 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1884 case APTX_AD_SR_UNCHANGED:
1885 case APTX_AD_48:
1886 default:
1887 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1888 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1889 break;
1890 case APTX_AD_44_1:
1891 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1892 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001893 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301894 }
1895 ALOGV("Successfully updated APTX AD enc format with \
1896 samplingrate: %d channels:%d",
1897 aptx_dsp_cfg->custom_cfg.sample_rate,
1898 aptx_dsp_cfg->custom_cfg.num_channels);
1899
1900 return ret;
1901}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301902
Zhou Song8fccbb62019-03-20 01:08:19 +08001903static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
1904 audio_aptx_encoder_config *aptx_bt_cfg)
1905{
1906 int ret = 0;
1907
1908 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Mingshu Pangaa429f72019-06-10 17:25:00 +08001909 ALOGE("Invalid param, aptx_dsp_cfg %pK aptx_bt_cfg %pK",
Zhou Song8fccbb62019-03-20 01:08:19 +08001910 aptx_dsp_cfg, aptx_bt_cfg);
1911 return -EINVAL;
1912 }
1913
1914 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
1915 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
1916
1917
1918 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1919 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1920 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1921 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1922 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1923 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1924 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1925 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1926 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
1927 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
1928 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
1929 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
1930 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
1931 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
1932 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1933 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1934 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1935 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1936
1937
1938 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1939 case APTX_AD_CHANNEL_UNCHANGED:
1940 case APTX_AD_CHANNEL_JOINT_STEREO:
1941 case APTX_AD_CHANNEL_DUAL_MONO:
1942 case APTX_AD_CHANNEL_STEREO_TWS:
1943 case APTX_AD_CHANNEL_EARBUD:
1944 default:
1945 a2dp.enc_channels = CH_STEREO;
1946 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1947 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1948 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1949 break;
1950 case APTX_AD_CHANNEL_MONO:
1951 a2dp.enc_channels = CH_MONO;
1952 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1953 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001954 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001955 }
1956 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1957 case APTX_AD_SR_UNCHANGED:
1958 case APTX_AD_48:
1959 default:
1960 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1961 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1962 break;
1963 case APTX_AD_44_1:
1964 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1965 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001966 break;
1967 case APTX_AD_96:
1968 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
1969 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_96K;
1970 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001971 }
1972 ALOGV("Successfully updated APTX AD enc format with \
1973 samplingrate: %d channels:%d",
1974 aptx_dsp_cfg->custom_cfg.sample_rate,
1975 aptx_dsp_cfg->custom_cfg.num_channels);
1976
1977 return ret;
1978}
1979
Manisha Agarwala51768b2018-11-01 16:30:52 +05301980static void audio_a2dp_update_tws_channel_mode()
1981{
1982 char* channel_mode;
1983 struct mixer_ctl *ctl_channel_mode;
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301984
1985 ALOGD("Update tws for mono_mode on=%d",a2dp.is_tws_mono_mode_on);
1986
Manisha Agarwala51768b2018-11-01 16:30:52 +05301987 if (a2dp.is_tws_mono_mode_on)
1988 channel_mode = "One";
1989 else
1990 channel_mode = "Two";
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301991
Manisha Agarwala51768b2018-11-01 16:30:52 +05301992 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1993 if (!ctl_channel_mode) {
1994 ALOGE("failed to get tws mixer ctl");
1995 return;
1996 }
1997 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1998 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1999 return;
2000 }
2001}
2002
Manisha Agarwald45632b2019-10-17 18:14:28 +08002003static void audio_a2dp_update_lc3_channel_mode()
2004{
2005 char* channel_mode;
2006 struct mixer_ctl *ctl_channel_mode;
2007
2008 ALOGD("Update lc3 for mono_mode on=%d",a2dp.is_lc3_mono_mode_on);
2009
2010 if (a2dp.is_lc3_mono_mode_on)
2011 channel_mode = "One";
2012 else
2013 channel_mode = "Two";
2014
2015 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_LC3_CHANNEL_MODE);
2016 if (!ctl_channel_mode) {
2017 ALOGE("failed to get lc3 mixer ctl");
2018 return;
2019 }
2020 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2021 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2022 return;
2023 }
2024}
2025
Manish Dewangan6a252632017-12-04 17:27:44 +05302026static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
2027 audio_aptx_encoder_config *aptx_bt_cfg)
2028{
2029 int ret = 0;
2030
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002031 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05302032 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
2033 aptx_dsp_cfg, aptx_bt_cfg);
2034 return -EINVAL;
2035 }
2036
2037 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002038 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05302039
2040 if (!a2dp.is_aptx_dual_mono_supported) {
2041 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
2042 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
2043 } else {
2044 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
2045 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
2046 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
2047 }
2048
2049 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
2050 case 1:
2051 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2052 break;
2053 case 2:
2054 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05302055 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2056 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
Manish Dewangan6a252632017-12-04 17:27:44 +05302057 break;
2058 }
2059 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
2060 if (!a2dp.is_aptx_dual_mono_supported) {
2061 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
2062 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
2063 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
2064 aptx_dsp_cfg->custom_cfg.num_channels);
2065 } else {
2066 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
2067 ALOGV("Successfully updated APTX dual mono enc format with \
2068 samplingrate: %d channels:%d syncmode %d",
2069 aptx_dsp_cfg->custom_cfg.sample_rate,
2070 aptx_dsp_cfg->custom_cfg.num_channels,
2071 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
2072 }
2073 return ret;
2074}
2075#else
2076static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
2077 audio_aptx_encoder_config *aptx_bt_cfg)
2078{
2079 int ret = 0;
2080
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002081 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05302082 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
2083 aptx_dsp_cfg, aptx_bt_cfg);
2084 return -EINVAL;
2085 }
2086
2087 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002088 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05302089 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
2090 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
2091 switch(aptx_dsp_cfg->num_channels) {
2092 case 1:
2093 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
2094 break;
2095 case 2:
2096 default:
2097 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
2098 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
2099 break;
2100 }
2101
2102 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
2103 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
2104
2105 return ret;
2106}
2107#endif
2108
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302109/* API to configure APTX DSP encoder */
2110bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2111{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302112 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08002113 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08002114 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302115 bool is_configured = false;
2116 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08002117 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05302118
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002119 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05302120 return false;
2121
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002122 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2123 if (!ctl_enc_data) {
2124 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2125 return false;
2126 }
2127
Manish Dewangan6a252632017-12-04 17:27:44 +05302128#ifndef LINUX_ENABLED
2129 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302130 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08002131 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002132 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08002133 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
2134 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002135 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08002136 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002137 else
Zhou Song8fccbb62019-03-20 01:08:19 +08002138 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002139 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002140 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002141
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002142 if (ret) {
2143 is_configured = false;
2144 goto fail;
2145 }
2146
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002147 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08002148 if (aptx_ad_ctl)
2149 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002150 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08002151 else
2152 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002153 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002154 } else {
2155 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002156 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302157 }
Manish Dewangan6a252632017-12-04 17:27:44 +05302158#else
2159 struct custom_enc_cfg_t aptx_dsp_cfg;
2160 mixer_size = sizeof(struct custom_enc_cfg_t);
2161 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05302162 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05302163 if (ret) {
2164 is_configured = false;
2165 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302166 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002167 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
2168 mixer_size);
2169#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302170 if (ret != 0) {
2171 ALOGE("%s: Failed to set APTX encoder config", __func__);
2172 is_configured = false;
2173 goto fail;
2174 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302175#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002176 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302177 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002178 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302179 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
2180 else
2181 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302182#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302183 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302184 is_configured = false;
2185 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302186 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302187 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302188 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05302189 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302190 else
Surendar Karka2febd452018-12-13 17:56:43 +05302191 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302192fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05302193 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002194 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05302195 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302196 return is_configured;
2197}
2198
2199/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302200 */
Manish Dewangan6a252632017-12-04 17:27:44 +05302201#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002202bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302203#else
2204bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2205#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302206{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302207 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302208 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302209 bool is_configured = false;
2210 int ret = 0;
2211
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002212 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302213 return false;
2214
2215 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2216 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002217 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302218 is_configured = false;
2219 goto fail;
2220 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302221
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302222 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002223 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302224 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2225 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2226 switch(aptx_dsp_cfg.num_channels) {
2227 case 1:
2228 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2229 break;
2230 case 2:
2231 default:
2232 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2233 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2234 break;
2235 }
2236 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302237 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302238 if (ret != 0) {
2239 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2240 is_configured = false;
2241 goto fail;
2242 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302243 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302244 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302245 is_configured = false;
2246 goto fail;
2247 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302248 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002249 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302250 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302251 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302252 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2253 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302254fail:
2255 return is_configured;
2256}
2257
2258/* API to configure AAC DSP encoder */
2259bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2260{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302261 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302262 struct aac_enc_cfg_t aac_dsp_cfg;
2263 bool is_configured = false;
2264 int ret = 0;
2265
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002266 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302267 return false;
2268
2269 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2270 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002271 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302272 is_configured = false;
2273 goto fail;
2274 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302275 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002276 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302277 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302278 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002279 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302280 case 0:
2281 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2282 break;
2283 case 2:
2284 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2285 break;
2286 case 1:
2287 default:
2288 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2289 break;
2290 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302291 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302292 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002293
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302294 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2295 sizeof(struct aac_enc_cfg_t));
2296 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002297 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302298 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302299 goto fail;
2300 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302301 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302302 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302303 is_configured = false;
2304 goto fail;
2305 }
2306 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002307 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302308 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002309 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002310 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2311 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2312fail:
2313 return is_configured;
2314}
2315
2316bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2317{
2318 struct mixer_ctl *ctl_enc_data = NULL;
2319 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2320 bool is_configured = false;
2321 int ret = 0;
2322
2323 if (aac_bt_cfg == NULL)
2324 return false;
2325
2326 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2327 if (!ctl_enc_data) {
2328 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2329 is_configured = false;
2330 goto fail;
2331 }
2332 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302333 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002334 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2335 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2336 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2337 case 0:
2338 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2339 break;
2340 case 2:
2341 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2342 break;
2343 case 1:
2344 default:
2345 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2346 break;
2347 }
2348 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2349 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2350 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2351 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2352
2353 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2354 sizeof(struct aac_enc_cfg_v2_t));
2355 if (ret != 0) {
2356 ALOGE("%s: Failed to set AAC encoder config", __func__);
2357 is_configured = false;
2358 goto fail;
2359 }
2360 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2361 if (ret != 0) {
2362 is_configured = false;
2363 goto fail;
2364 }
2365 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302366 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002367 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2368 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2369 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2370 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302371fail:
2372 return is_configured;
2373}
2374
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302375bool configure_aac_enc_format_v3(audio_aac_encoder_config_v3 *aac_bt_cfg)
2376{
2377 struct mixer_ctl *ctl_enc_data = NULL;
2378 struct aac_enc_cfg_v3_t aac_dsp_cfg;
2379 struct aac_frame_size_control_t* frame_vbr_ctl = NULL;
2380 bool is_configured = false;
2381 int ret = 0;
2382
2383 if (aac_bt_cfg == NULL)
2384 return false;
2385
2386 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2387 if (!ctl_enc_data) {
2388 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2389 is_configured = false;
2390 goto fail;
2391 }
2392 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v3_t));
2393 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
2394 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2395 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2396 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2397 case 0:
2398 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2399 break;
2400 case 2:
2401 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2402 break;
2403 case 1:
2404 default:
2405 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2406 break;
2407 }
2408 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2409 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2410 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2411 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2412 frame_vbr_ctl = aac_bt_cfg->frame_ptr_ctl;
2413
2414 if (frame_vbr_ctl != NULL) {
2415 aac_dsp_cfg.aac_key_value_ctl.ctl_type = frame_vbr_ctl->ctl_type;
2416 aac_dsp_cfg.aac_key_value_ctl.ctl_value = frame_vbr_ctl->ctl_value;
2417 } else {
2418 ALOGE("%s: VBR cannot be enabled, fall back to default",__func__);
2419 aac_dsp_cfg.aac_key_value_ctl.ctl_type = 0;
2420 aac_dsp_cfg.aac_key_value_ctl.ctl_value = 0;
2421 }
2422
2423 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2424 sizeof(struct aac_enc_cfg_v3_t));
2425 if (ret != 0) {
2426 ALOGE("%s: Failed to set AAC encoder config", __func__);
2427 is_configured = false;
2428 goto fail;
2429 }
2430 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2431 if (ret != 0) {
2432 is_configured = false;
2433 goto fail;
2434 }
2435 is_configured = true;
2436 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
2437 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2438 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2439 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2440 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
2441fail:
2442 return is_configured;
2443}
2444
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302445bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2446{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302447 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302448 struct celt_enc_cfg_t celt_dsp_cfg;
2449 bool is_configured = false;
2450 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002451 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302452 return false;
2453
2454 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2455 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002456 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302457 is_configured = false;
2458 goto fail;
2459 }
2460 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2461
Florian Pfister1a84f312018-07-19 14:38:18 +02002462 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302463 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2464 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2465 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2466 case 1:
2467 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2468 break;
2469 case 2:
2470 default:
2471 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2472 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2473 break;
2474 }
2475
2476 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2477
2478 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2479 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2480 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2481 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2482 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2483
2484 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2485 sizeof(struct celt_enc_cfg_t));
2486 if (ret != 0) {
2487 ALOGE("%s: Failed to set CELT encoder config", __func__);
2488 is_configured = false;
2489 goto fail;
2490 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302491 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302492 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302493 is_configured = false;
2494 goto fail;
2495 }
2496 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002497 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302498 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302499 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302500 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2501 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2502fail:
2503 return is_configured;
2504}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302505
2506bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2507{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302508 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302509 struct ldac_enc_cfg_t ldac_dsp_cfg;
2510 bool is_configured = false;
2511 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002512 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302513 return false;
2514
2515 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2516 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002517 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302518 is_configured = false;
2519 goto fail;
2520 }
2521 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2522
Florian Pfister1a84f312018-07-19 14:38:18 +02002523 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302524 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2525 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2526 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2527 case 4:
2528 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2529 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2530 break;
2531 case 2:
2532 case 1:
2533 default:
2534 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2535 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2536 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2537 break;
2538 }
2539
2540 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2541 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2542 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002543 if (ldac_bt_cfg->is_abr_enabled) {
2544 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2545 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2546 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2547 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2548 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002549 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002550 }
2551
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302552 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2553 sizeof(struct ldac_enc_cfg_t));
2554 if (ret != 0) {
2555 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2556 is_configured = false;
2557 goto fail;
2558 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302559 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302560 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302561 is_configured = false;
2562 goto fail;
2563 }
2564 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002565 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302566 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2567 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002568 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302569 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2570 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2571fail:
2572 return is_configured;
2573}
2574
Manisha Agarwald45632b2019-10-17 18:14:28 +08002575uint64_t convert_channel_map(uint32_t audio_location)
2576{
2577 int i;
2578 uint64_t channel_mask = (uint64_t) 0x00000000;
2579
2580 if (!audio_location) {
2581 channel_mask |= 1ULL << PCM_CHANNEL_C;
2582 return channel_mask;
2583 }
2584
2585 for (i = 0; i < AUDIO_LOCATION_MAX; i++) {
2586 if (audio_location & audio_location_map_array[i])
2587 channel_mask |= 1ULL << channel_map_array[i];
2588 }
2589
2590 return channel_mask;
2591}
2592
2593bool configure_lc3_enc_format(audio_lc3_codec_config_t *lc3_bt_cfg) {
2594 struct mixer_ctl *ctl_enc_data = NULL;
2595 int mixer_size = 0;
2596 int ret = 0;
2597 int i;
2598 bool is_configured = false;
2599 struct lc3_enc_cfg_t lc3_dsp_cfg;
2600 uint64_t channel_mask;
2601
2602 if (lc3_bt_cfg == NULL)
2603 return false;
2604
2605 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2606 if (!ctl_enc_data) {
2607 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
2608 return false;
2609 }
2610
2611 /* Initialize dsp configuration params */
2612 memset(&lc3_dsp_cfg, 0x0, sizeof(struct lc3_enc_cfg_t));
2613
2614 lc3_dsp_cfg.enc_format = MEDIA_FMT_LC3;
2615 //encoder structure
2616
2617 if (lc3_bt_cfg->enc_config.stream_map_size != 0) {
2618 if (!lc3_bt_cfg->enc_config.streamMapOut[0].audio_location)
2619 a2dp.enc_channels = CH_MONO;
2620 else
2621 a2dp.enc_channels = CH_STEREO;
2622 }
2623
2624 lc3_dsp_cfg.imc_info.direction = IMC_RECEIVE;
2625 lc3_dsp_cfg.imc_info.enable = IMC_ENABLE;
2626 lc3_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2627 lc3_dsp_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
2628 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.api_version = lc3_bt_cfg->enc_config.toAirConfig.api_version;
2629 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.sampling_freq = lc3_bt_cfg->enc_config.toAirConfig.sampling_freq;
2630 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.max_octets_per_frame = lc3_bt_cfg->enc_config.toAirConfig.max_octets_per_frame;
2631 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.frame_duration = lc3_bt_cfg->enc_config.toAirConfig.frame_duration;
2632 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.bit_depth = lc3_bt_cfg->enc_config.toAirConfig.bit_depth;
2633 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.num_blocks = lc3_bt_cfg->enc_config.toAirConfig.num_blocks;
2634 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.default_q_level = lc3_bt_cfg->enc_config.toAirConfig.default_q_level;
2635 for (i = 0; i < 16; i++)
2636 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.vendor_specific[i] = lc3_bt_cfg->enc_config.toAirConfig.vendor_specific[i];
2637 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.mode = lc3_bt_cfg->enc_config.toAirConfig.mode;
2638 lc3_dsp_cfg.enc_codec.to_Air_cfg.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
2639 lc3_dsp_cfg.enc_codec.streamMapToAir.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
2640
2641 for (i = 0; i < lc3_dsp_cfg.enc_codec.to_Air_cfg.stream_map_size; i++) {
2642 // for encoder stream map info
2643 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].audio_location = lc3_bt_cfg->enc_config.streamMapOut[i].audio_location;
2644 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
2645 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
2646 // for packetizer stream map info
2647 channel_mask = convert_channel_map(lc3_bt_cfg->enc_config.streamMapOut[i].audio_location);
2648 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
2649 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
2650 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].channel_mask_lsw = channel_mask & 0x00000000FFFFFFFF;
2651 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].channel_mask_msw = (channel_mask & 0xFFFFFFFF00000000) >> 32;
2652 }
2653
2654 /* Configure AFE DSP configuration */
2655 mixer_size = sizeof(struct lc3_enc_cfg_t);
2656 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&lc3_dsp_cfg,
2657 mixer_size);
2658 if (ret != 0) {
2659 ALOGE("%s: Failed to set lc3 encoder config", __func__);
2660 is_configured = false;
2661 goto fail;
2662 }
2663
2664 ret = a2dp_set_bit_format(ENCODER_BIT_FORMAT_PCM_24);
2665 if (ret != 0) {
2666 ALOGE("%s: Failed to set lc3 bit format", __func__);
2667 is_configured = false;
2668 goto fail;
2669 }
2670
2671 is_configured = true;
2672 a2dp.bt_encoder_format = CODEC_TYPE_LC3;
2673
2674fail:
2675 return is_configured;
2676}
2677
2678bool configure_lc3_dec_format(audio_lc3_codec_config_t *lc3_bt_cfg)
2679{
2680 struct mixer_ctl *ctl_dec_data = NULL;
2681 struct lc3_dec_cfg_t lc3_dsp_cfg;
2682 uint64_t channel_mask;
2683 bool is_configured = false;
2684 int ret = 0;
2685 int i;
2686
2687 if (lc3_bt_cfg == NULL)
2688 return false;
2689
2690 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
2691 if (!ctl_dec_data) {
2692 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
2693 return false;
2694 }
2695
2696 memset(&lc3_dsp_cfg, 0x0, sizeof(struct lc3_dec_cfg_t));
2697
2698 lc3_dsp_cfg.abr_cfg.dec_format = MEDIA_FMT_LC3;
2699 lc3_dsp_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
2700 lc3_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2701 lc3_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2702
2703 lc3_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
2704
2705 //for depacketizer both fromAir and toAir streamMap needs to be sent.
2706 lc3_dsp_cfg.dec_codec.streamMapToAir.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
2707
2708 for (i = 0; i < lc3_bt_cfg->enc_config.stream_map_size; i++) {
2709 // for depacketizer stream map info
2710 channel_mask = convert_channel_map(lc3_bt_cfg->enc_config.streamMapOut[i].audio_location);
2711 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
2712 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
2713 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].channel_mask_lsw = channel_mask & 0x00000000FFFFFFFF;
2714 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].channel_mask_msw = (channel_mask & 0xFFFFFFFF00000000) >> 32;
2715 }
2716
2717 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&lc3_dsp_cfg,
2718 sizeof(struct lc3_dec_cfg_t));
2719 if (ret != 0) {
2720 ALOGE("%s: failed to set LC3 decoder config", __func__);
2721 is_configured = false;
2722 goto fail;
2723 }
2724
2725 is_configured = true;
2726 a2dp.bt_decoder_format = CODEC_TYPE_LC3;
2727
2728fail:
2729 return is_configured;
2730}
2731
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302732bool configure_a2dp_encoder_format()
2733{
2734 void *codec_info = NULL;
2735 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002736 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302737 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002738 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302739
Florian Pfister1a84f312018-07-19 14:38:18 +02002740 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302741 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2742 return false;
2743 }
2744 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002745 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302746 &codec_type);
2747
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002748 // ABR disabled by default for all codecs
2749 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302750 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002751
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302752 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002753 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302754 ALOGD(" Received SBC encoder supported BT device");
2755 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002756 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302757 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002758 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302759 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302760#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002761 a2dp.is_aptx_dual_mono_supported = false;
2762 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302763#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302764 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002765 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302766 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002767 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302768 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302769#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302770 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002771 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302772#else
2773 is_configured =
2774 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2775#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002776 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302777#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002778 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002779 ALOGD(" Received APTX dual mono encoder supported BT device");
2780 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302781 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2782 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002783 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2784 is_configured =
2785 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302786 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302787#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002788 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302789 ALOGD(" Received AAC encoder supported BT device");
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302790 bool is_aac_vbr_enabled =
2791 property_get_bool("persist.vendor.bt.aac_vbr_frm_ctl.enabled", false);
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002792 bool is_aac_frame_ctl_enabled =
2793 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302794 if (is_aac_vbr_enabled)
2795 is_configured = configure_aac_enc_format_v3((audio_aac_encoder_config_v3 *) codec_info);
2796 else
2797 is_configured = is_aac_frame_ctl_enabled ?
2798 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2799 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302800 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002801 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302802 ALOGD(" Received CELT encoder supported BT device");
2803 is_configured =
2804 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2805 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002806 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302807 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002808 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2809 instance_id = MAX_INSTANCE_ID;
2810 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302811 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002812 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302813 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302814 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302815#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302816 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302817 ALOGD(" Received APTX AD encoder supported BT device");
2818 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2819 instance_id = MAX_INSTANCE_ID;
2820 a2dp.abr_config.imc_instance = instance_id--;
2821 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2822 a2dp.is_aptx_adaptive = true;
2823 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2824 is_configured =
2825 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302826 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302827 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302828#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002829 case CODEC_TYPE_PCM:
2830 ALOGD("Received PCM format for BT device");
2831 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2832 is_configured = true;
2833 break;
Manisha Agarwald45632b2019-10-17 18:14:28 +08002834 case CODEC_TYPE_LC3:
2835 ALOGD("Received LC3 encoder supported BT device");
2836 a2dp.bt_encoder_format = CODEC_TYPE_LC3;
2837 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2838 instance_id = MAX_INSTANCE_ID;
2839 a2dp.abr_config.imc_instance = instance_id--;
2840 a2dp.abr_config.is_abr_enabled = true;
2841 is_configured =
2842 (configure_lc3_enc_format((audio_lc3_codec_config_t *)codec_info) &&
2843 configure_lc3_dec_format((audio_lc3_codec_config_t *)codec_info));
2844 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302845 default:
2846 ALOGD(" Received Unsupported encoder formar");
2847 is_configured = false;
2848 break;
2849 }
2850 return is_configured;
2851}
2852
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002853int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302854{
2855 int ret = 0;
2856
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002857 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302858
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002859 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002860 && a2dp.audio_get_enc_config)) {
2861 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302862 return -ENOSYS;
2863 }
2864
Zhou Song12c29502019-03-16 10:37:18 +08002865 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302866 //session will be restarted after suspend completion
2867 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302868 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302869 }
2870
Florian Pfister1a84f312018-07-19 14:38:18 +02002871 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302872 ALOGD("calling BT module stream start");
2873 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002874 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302875 ALOGE("BT controller start return = %d",ret);
2876 if (ret != 0 ) {
2877 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002878 a2dp.a2dp_source_started = false;
Revathi Uddarajub26e3932020-06-10 14:51:02 +05302879 ret = -ETIMEDOUT;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302880 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002881 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002882 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302883 ret = 0;
2884 ALOGD("Start playback successful to BT library");
2885 } else {
2886 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002887 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302888 ret = -ETIMEDOUT;
2889 }
2890 }
2891 }
2892
Florian Pfister1a84f312018-07-19 14:38:18 +02002893 if (a2dp.a2dp_source_started) {
2894 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302895 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302896 audio_a2dp_update_tws_channel_mode();
Manisha Agarwald45632b2019-10-17 18:14:28 +08002897 audio_a2dp_update_lc3_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002898 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002899 if (a2dp.abr_config.is_abr_enabled)
2900 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302901 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302902
2903 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002904 a2dp.a2dp_source_total_active_session_requests);
2905 return ret;
2906}
2907
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002908uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002909{
2910 uint32_t latency = 0;
2911
2912 switch(a2dp.bt_decoder_format) {
2913 case CODEC_TYPE_SBC:
2914 latency = DEFAULT_SINK_LATENCY_SBC;
2915 break;
2916 case CODEC_TYPE_AAC:
2917 latency = DEFAULT_SINK_LATENCY_AAC;
2918 break;
2919 default:
2920 latency = 200;
2921 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2922 break;
2923 }
2924 return (uint64_t)latency;
2925}
2926
2927bool a2dp_send_sink_setup_complete(void) {
2928 uint64_t system_latency = 0;
2929 bool is_complete = false;
2930
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002931 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002932
2933 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2934 is_complete = true;
2935 }
2936 return is_complete;
2937}
2938
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002939bool a2dp_sink_is_ready()
2940{
2941 bool ret = false;
2942
2943 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2944 (a2dp.is_a2dp_offload_supported) &&
2945 (a2dp.audio_sink_check_a2dp_ready))
2946 ret = a2dp.audio_sink_check_a2dp_ready();
2947 return ret;
2948}
2949
2950int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002951{
2952 int ret = 0;
2953
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002954 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002955
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002956 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002957 && a2dp.audio_get_dec_config)) {
2958 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2959 return -ENOSYS;
2960 }
2961
2962 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2963 ALOGD("calling BT module stream start");
2964 /* This call indicates BT IPC lib to start capture */
2965 ret = a2dp.audio_sink_start();
2966 ALOGE("BT controller start capture return = %d",ret);
2967 if (ret != 0 ) {
2968 ALOGE("BT controller start capture failed");
2969 a2dp.a2dp_sink_started = false;
2970 } else {
2971
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002972 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002973 ALOGD("Wait for capture ready not successful");
2974 ret = -ETIMEDOUT;
2975 }
2976
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002977 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002978 a2dp.a2dp_sink_started = true;
2979 ret = 0;
2980 ALOGD("Start capture successful to BT library");
2981 } else {
2982 ALOGD(" unable to configure DSP decoder");
2983 a2dp.a2dp_sink_started = false;
2984 ret = -ETIMEDOUT;
2985 }
2986
2987 if (!a2dp_send_sink_setup_complete()) {
2988 ALOGD("sink_setup_complete not successful");
2989 ret = -ETIMEDOUT;
2990 }
2991 }
2992 }
2993
2994 if (a2dp.a2dp_sink_started) {
2995 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002996 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002997 }
2998 }
2999
3000 ALOGD("start A2DP sink total active sessions :%d",
3001 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303002 return ret;
3003}
3004
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303005static void reset_a2dp_enc_config_params()
3006{
3007 int ret =0;
3008
Aalique Grahame22e49102018-12-18 14:23:57 -08003009 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303010 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05303011 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303012
3013 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
3014 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
3015 MIXER_ENC_CONFIG_BLOCK);
3016 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003017 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303018 } else {
3019 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
3020 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02003021 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303022 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003023
3024 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3025
Manisha Agarwala51768b2018-11-01 16:30:52 +05303026 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
3027
3028 if (!ctl_channel_mode) {
3029 ALOGE("failed to get tws mixer ctl");
3030 } else {
3031 channel_mode = "Two";
3032 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
3033 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
3034 }
3035 a2dp.is_tws_mono_mode_on = false;
3036 }
Manisha Agarwald45632b2019-10-17 18:14:28 +08003037
3038 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_LC3_CHANNEL_MODE);
3039
3040 if (!ctl_channel_mode) {
3041 ALOGE("failed to get lc3 mixer ctl");
3042 } else {
3043 channel_mode = "Two";
3044 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0)
3045 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
3046 a2dp.is_lc3_mono_mode_on = false;
3047 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303048}
3049
Surendar Karka2febd452018-12-13 17:56:43 +05303050static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07003051{
3052 struct mixer_ctl *ctl_dec_data = NULL;
3053 struct abr_dec_cfg_t dummy_reset_cfg;
3054 int ret = 0;
3055
Preetam Singh Ranawat25780e42019-07-31 18:15:57 +05303056 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3057 if (!ctl_dec_data) {
3058 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
3059 return -EINVAL;
3060 }
3061 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
3062 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
3063 sizeof(dummy_reset_cfg));
3064 if (ret != 0) {
3065 ALOGE("%s: Failed to set dummy decoder config", __func__);
3066 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07003067 }
3068
3069 return ret;
3070}
3071
Surendar Karka2febd452018-12-13 17:56:43 +05303072static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02003073{
3074 int ret =0;
3075
3076 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
3077 struct aac_dec_cfg_t dummy_reset_config;
3078
3079 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
3080 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05303081 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02003082 if (!ctl_dec_config) {
3083 ALOGE(" ERROR a2dp decoder format mixer control not identified");
3084 } else {
3085 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
3086 sizeof(struct aac_dec_cfg_t));
3087 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
3088 }
3089 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
3090 MIXER_DEC_BIT_FORMAT);
3091 if (!ctrl_bit_format) {
3092 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
3093 } else {
3094 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
3095 if (ret != 0) {
3096 ALOGE("%s: Failed to set bit format to decoder", __func__);
3097 }
3098 }
3099}
3100
Zhou Song12c29502019-03-16 10:37:18 +08003101static void reset_codec_config()
3102{
3103 reset_a2dp_enc_config_params();
3104 reset_a2dp_source_dec_config_params();
3105 a2dp_reset_backend_cfg(SOURCE);
3106 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
3107 stop_abr();
3108 a2dp.abr_config.is_abr_enabled = false;
3109}
3110
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003111int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303112{
3113 int ret =0;
3114
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003115 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003116 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003117 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303118 return -ENOSYS;
3119 }
3120
Florian Pfister1a84f312018-07-19 14:38:18 +02003121 if (a2dp.a2dp_source_total_active_session_requests > 0)
3122 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08003123 else
3124 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303125
Florian Pfister1a84f312018-07-19 14:38:18 +02003126 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303127 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02003128 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303129 if (ret < 0)
3130 ALOGE("stop stream to BT IPC lib failed");
3131 else
3132 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08003133 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
3134 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05303135 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303136 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003137 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02003138 a2dp.a2dp_source_started = false;
3139 ALOGD("Stop A2DP playback, total active sessions :%d",
3140 a2dp.a2dp_source_total_active_session_requests);
3141 return 0;
3142}
3143
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003144int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02003145{
3146 int ret =0;
3147
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003148 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003149 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003150 ALOGE("a2dp handle is not identified, Ignoring stop request");
3151 return -ENOSYS;
3152 }
3153
3154 if (a2dp.a2dp_sink_total_active_session_requests > 0)
3155 a2dp.a2dp_sink_total_active_session_requests--;
3156
3157 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
3158 ALOGV("calling BT module stream stop");
3159 ret = a2dp.audio_sink_stop();
3160 if (ret < 0)
3161 ALOGE("stop stream to BT IPC lib failed");
3162 else
3163 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05303164 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02003165 a2dp_reset_backend_cfg(SINK);
3166 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003167 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02003168 a2dp.a2dp_source_started = false;
3169 ALOGD("Stop A2DP capture, total active sessions :%d",
3170 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303171 return 0;
3172}
3173
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003174int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303175{
Weiyin Jiang280ea742020-09-08 20:28:22 +08003176 int ret = 0, val, status = 0;
3177 char value[32] = {0};
3178 struct audio_usecase *uc_info;
3179 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303180
Weiyin Jiang280ea742020-09-08 20:28:22 +08003181 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003182 ALOGV("no supported encoders identified,ignoring a2dp setparam");
3183 status = -EINVAL;
3184 goto param_handled;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003185 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303186
Weiyin Jiang280ea742020-09-08 20:28:22 +08003187 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303188 sizeof(value));
Weiyin Jiang280ea742020-09-08 20:28:22 +08003189 if (ret >= 0) {
3190 val = atoi(value);
3191 if (audio_is_a2dp_out_device(val)) {
3192 ALOGV("Received device connect request for A2DP source");
3193 open_a2dp_source();
3194 }
3195 goto param_handled;
3196 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303197
Weiyin Jiang280ea742020-09-08 20:28:22 +08003198 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
3199 sizeof(value));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303200
Weiyin Jiang280ea742020-09-08 20:28:22 +08003201 if (ret >= 0) {
3202 val = atoi(value);
3203 if (audio_is_a2dp_out_device(val)) {
3204 ALOGV("Received source device dis- connect request");
3205 close_a2dp_output();
3206 reset_a2dp_enc_config_params();
3207 reset_a2dp_source_dec_config_params();
3208 a2dp_reset_backend_cfg(SOURCE);
3209 } else if (audio_is_a2dp_in_device(val)) {
3210 ALOGV("Received sink device dis- connect request");
3211 close_a2dp_input();
3212 reset_a2dp_sink_dec_config_params();
3213 a2dp_reset_backend_cfg(SINK);
3214 }
3215 goto param_handled;
3216 }
Surendar Karka2febd452018-12-13 17:56:43 +05303217#ifndef LINUX_ENABLED
Weiyin Jiang280ea742020-09-08 20:28:22 +08003218 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
3219 if (ret >= 0) {
3220 ALOGD("Setting tws channel mode to %s",value);
3221 if (!(strncmp(value, "mono", strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05303222 a2dp.is_tws_mono_mode_on = true;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003223 else if (!(strncmp(value, "dual-mono", strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05303224 a2dp.is_tws_mono_mode_on = false;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003225 audio_a2dp_update_tws_channel_mode();
3226 goto param_handled;
3227 }
Manisha Agarwald45632b2019-10-17 18:14:28 +08003228
3229 ret = str_parms_get_str(parms, "LEAMono", value, sizeof(value));
3230 if (ret>=0) {
3231 ALOGD("Setting LC3 channel mode to %s",value);
3232 if (!(strncmp(value,"true",strlen(value))))
3233 a2dp.is_lc3_mono_mode_on = true;
3234 else
3235 a2dp.is_lc3_mono_mode_on = false;
3236 audio_a2dp_update_lc3_channel_mode();
3237 goto param_handled;
3238 }
Surendar Karka2febd452018-12-13 17:56:43 +05303239#endif
Weiyin Jiang280ea742020-09-08 20:28:22 +08003240 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
3241 if (ret >= 0) {
3242 if (a2dp.bt_lib_source_handle == NULL)
3243 goto param_handled;
3244
3245 if ((!strncmp(value, "true", sizeof(value)))) {
3246 if (a2dp.a2dp_source_suspended) {
3247 ALOGD("%s: A2DP is already suspended", __func__);
3248 goto param_handled;
3249 }
3250 ALOGD("Setting a2dp to suspend state");
3251 a2dp.a2dp_source_suspended = true;
3252 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
3253 goto param_handled;
3254 list_for_each(node, &a2dp.adev->usecase_list) {
3255 uc_info = node_to_item(node, struct audio_usecase, list);
3256 if (uc_info->type == PCM_PLAYBACK &&
3257 (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
3258 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
3259 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)) {
3260 fp_check_a2dp_restore_l(a2dp.adev, uc_info->stream.out, false);
Aalique Grahame22e49102018-12-18 14:23:57 -08003261 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08003262 }
3263 if (!a2dp.swb_configured)
3264 reset_codec_config();
3265 if (a2dp.audio_source_suspend)
3266 a2dp.audio_source_suspend();
3267 } else if (a2dp.a2dp_source_suspended == true) {
3268 ALOGD("Resetting a2dp suspend state");
3269 struct audio_usecase *uc_info;
3270 struct listnode *node;
3271 if (a2dp.clear_source_a2dpsuspend_flag)
3272 a2dp.clear_source_a2dpsuspend_flag();
3273 a2dp.a2dp_source_suspended = false;
3274 /*
3275 * It is possible that before suspend,a2dp sessions can be active
3276 * for example during music + voice activation concurrency
3277 * a2dp suspend will be called & BT will change to sco mode
3278 * though music is paused as a part of voice activation
3279 * compress session close happens only after pause timeout(10secs)
3280 * so if resume request comes before pause timeout as a2dp session
3281 * is already active IPC start will not be called from APM/audio_hw
3282 * Fix is to call a2dp start for IPC library post suspend
3283 * based on number of active session count
3284 */
3285 if (a2dp.a2dp_source_total_active_session_requests > 0) {
3286 ALOGD(" Calling IPC lib start post suspend state");
3287 if (a2dp.audio_source_start) {
3288 ret = a2dp.audio_source_start();
3289 if (ret != 0) {
3290 ALOGE("BT controller start failed");
3291 a2dp.a2dp_source_started = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303292 }
3293 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08003294 }
3295 list_for_each(node, &a2dp.adev->usecase_list) {
3296 uc_info = node_to_item(node, struct audio_usecase, list);
3297 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
3298 is_a2dp_out_device_type(&uc_info->stream.out->device_list)) {
3299 fp_check_a2dp_restore_l(a2dp.adev, uc_info->stream.out, true);
Zhou Song10617ed2017-05-26 13:28:48 +08003300 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303301 }
3302 }
3303 goto param_handled;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003304 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003305
Weiyin Jiang280ea742020-09-08 20:28:22 +08003306 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
3307 sizeof(value));
3308 if (ret >= 0) {
3309 if (a2dp.is_a2dp_offload_supported &&
3310 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
3311 *reconfig = true;
3312 }
3313 goto param_handled;
3314 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003315
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303316param_handled:
Weiyin Jiang280ea742020-09-08 20:28:22 +08003317 ALOGV("end of a2dp setparam");
3318 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303319}
3320
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003321void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05303322{
3323 a2dp.is_handoff_in_progress = is_on;
3324}
3325
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003326bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303327{
3328 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05303329 // Or if a2dp device is selected but earlier start failed ( as a2dp
3330 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02003331 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303332}
3333
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003334void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303335{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303336 *sample_rate = a2dp.enc_sampling_rate;
3337}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303338
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003339void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02003340{
3341 *sample_rate = a2dp.dec_sampling_rate;
3342}
3343
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003344bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303345{
3346 bool ret = false;
3347
Florian Pfister1a84f312018-07-19 14:38:18 +02003348 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303349 return ret;
3350
Florian Pfister1a84f312018-07-19 14:38:18 +02003351 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08003352 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02003353 (a2dp.audio_source_check_a2dp_ready))
3354 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303355 return ret;
3356}
3357
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003358bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02003359{
3360 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303361}
3362
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003363void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08003364 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303365{
3366 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02003367 a2dp.bt_lib_source_handle = NULL;
3368 a2dp.a2dp_source_started = false;
3369 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
3370 a2dp.a2dp_source_total_active_session_requests = 0;
3371 a2dp.a2dp_source_suspended = false;
3372 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303373 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303374 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07003375 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05303376 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08003377 a2dp.abr_config.is_abr_enabled = false;
3378 a2dp.abr_config.abr_started = false;
3379 a2dp.abr_config.imc_instance = 0;
3380 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08003381 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05303382 a2dp.is_tws_mono_mode_on = false;
Manisha Agarwald45632b2019-10-17 18:14:28 +08003383 a2dp.is_lc3_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05303384 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08003385 a2dp.swb_configured = false;
3386
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003387 // init function pointers
3388 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08003389 init_config.fp_platform_get_pcm_device_id;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003390 fp_check_a2dp_restore_l = init_config.fp_check_a2dp_restore_l;
Gautam Manamfbb3ebc2020-10-08 18:06:45 +05303391 fp_platform_switch_voice_call_device_post =
3392 init_config.fp_platform_switch_voice_call_device_post;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003393
kunleiz5a127262017-09-08 14:47:48 +08003394 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05303395 reset_a2dp_source_dec_config_params();
3396 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02003397
3398 a2dp.bt_lib_sink_handle = NULL;
3399 a2dp.a2dp_sink_started = false;
3400 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
3401 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003402
3403 if (is_running_with_enhanced_fwk == UNINITIALIZED)
3404 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
3405 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08003406 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02003407
3408 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303409 update_offload_codec_capabilities();
3410}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003411
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003412uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003413{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003414 uint32_t latency = 0;
3415 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303416 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
3417 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003418 char value[PROPERTY_VALUE_MAX];
3419
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003420 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08003421 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003422 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303423 if (sscanf(value, "%d/%d/%d/%d/%d%d",
3424 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003425 ALOGI("Failed to parse avsync offset params from '%s'.", value);
3426 avsync_runtime_prop = 0;
3427 }
3428 }
3429
yidongh0515e042017-07-06 15:00:34 +08003430 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02003431 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
3432 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08003433 }
3434
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07003435 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003436 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08003437 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
3438 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003439 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003440 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08003441 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
3442 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003443 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003444 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08003445 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
3446 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003447 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003448 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08003449 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
3450 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003451 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003452 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05303453 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
3454 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
3455 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003456 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303457 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
3458 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
3459 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05303460 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05303461 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05303462 break;
Manisha Agarwald45632b2019-10-17 18:14:28 +08003463 case CODEC_TYPE_LC3:
3464 latency = slatency;
3465 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08003466 case CODEC_TYPE_PCM:
3467 latency = ENCODER_LATENCY_PCM;
3468 latency += DEFAULT_SINK_LATENCY_PCM;
3469 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003470 default:
3471 latency = 200;
3472 break;
3473 }
3474 return latency;
3475}
Aalique Grahame22e49102018-12-18 14:23:57 -08003476
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003477int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003478 struct str_parms *reply)
3479{
3480 int ret, val = 0;
3481 char value[32]={0};
3482
3483 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3484 value, sizeof(value));
3485 if (ret >= 0) {
3486 val = a2dp.is_a2dp_offload_supported;
3487 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3488 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3489 }
3490
3491 return 0;
3492}
Zhou Song12c29502019-03-16 10:37:18 +08003493
3494
3495bool configure_aptx_ad_speech_enc_fmt() {
3496 struct mixer_ctl *ctl_enc_data = NULL;
3497 int mixer_size = 0;
3498 int ret = 0;
3499 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3500
3501 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3502 if (!ctl_enc_data) {
3503 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3504 return false;
3505 }
3506
3507 /* Initialize dsp configuration params */
3508 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3509 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3510 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3511 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3512 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3513 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3514 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3515 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3516 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3517 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3518 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3519
3520 /* Configure AFE DSP configuration */
3521 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3522 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3523 mixer_size);
3524 if (ret != 0) {
3525 ALOGE("%s: Failed to set SWB encoder config", __func__);
3526 return false;
3527 }
3528
3529 /* Configure AFE Input Bit Format as PCM_16 */
3530 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3531 if (ret != 0) {
3532 ALOGE("%s: Failed to set SWB bit format", __func__);
3533 return false;
3534 }
3535
3536 return true;
3537}
3538
3539bool configure_aptx_ad_speech_dec_fmt()
3540{
3541 struct mixer_ctl *ctl_dec_data = NULL;
3542 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3543 int ret = 0;
3544
3545 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3546 if (!ctl_dec_data) {
3547 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3548 return false;
3549 }
3550 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3551 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3552 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3553 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3554 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3555 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3556 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3557 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3558
3559 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3560 sizeof(dec_cfg));
3561 if (ret != 0) {
3562 ALOGE("%s: Failed to set decoder config", __func__);
3563 return false;
3564 }
3565 return true;
3566}
3567
3568int sco_start_configuration()
3569{
3570 ALOGD("sco_start_configuration start");
3571
3572 if (!a2dp.swb_configured) {
Gautam Manamfbb3ebc2020-10-08 18:06:45 +05303573 /*Before starting sco config, we must ensure to set correct acdb id
3574 because sco cofiguration will trigger port open which needs acdb_id*/
3575 fp_platform_switch_voice_call_device_post(a2dp.adev->platform,
3576 SND_DEVICE_OUT_BT_SCO_SWB,
3577 SND_DEVICE_IN_BT_SCO_MIC_SWB);
3578
Zhou Song12c29502019-03-16 10:37:18 +08003579 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3580 /* Configure AFE codec*/
3581 if (configure_aptx_ad_speech_enc_fmt() &&
3582 configure_aptx_ad_speech_dec_fmt()) {
3583 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3584 } else {
3585 ALOGE("%s: failed to send SCO configuration", __func__);
3586 return -ETIMEDOUT;
3587 }
3588 /* Configure backend*/
3589 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3590 a2dp.enc_channels = CH_MONO;
3591 a2dp.abr_config.is_abr_enabled = true;
3592 a2dp_set_backend_cfg(SOURCE);
3593 /* Start abr*/
3594 start_abr();
3595 a2dp.swb_configured = true;
3596 }
3597 return 0;
3598}
3599
3600void sco_reset_configuration()
3601{
Zhou Songd6d71752019-05-21 18:08:51 +08003602 if (a2dp.swb_configured) {
3603 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003604
Zhou Songd6d71752019-05-21 18:08:51 +08003605 reset_codec_config();
3606 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3607 a2dp.swb_configured = false;
3608 }
Zhou Song12c29502019-03-16 10:37:18 +08003609}