blob: 148f53d8c40b144e4bbc7a3e6fb49ef4ea72687e [file] [log] [blame]
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301/*
sriram kumarcd549b02021-02-12 14:27:17 +05302* Copyright (c) 2015-2021, The Linux Foundation. All rights reserved.
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of The Linux Foundation nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
Aalique Grahame22e49102018-12-18 14:23:57 -080029#define LOG_TAG "a2dp_offload"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053030/*#define LOG_NDEBUG 0*/
31#define LOG_NDDEBUG 0
32#include <errno.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080033#include <log/log.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053034#include <dlfcn.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080035#include <pthread.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
Manish Dewangan6a252632017-12-04 17:27:44 +053039#include "audio_extn.h"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053040#include <stdlib.h>
41#include <cutils/str_parms.h>
42#include <hardware/audio.h>
43#include <hardware/hardware.h>
44#include <cutils/properties.h>
45
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046#ifdef DYNAMIC_LOG_ENABLED
47#include <log_xml_parser.h>
48#define LOG_MASK HAL_MOD_FILE_A2DP
49#include <log_utils.h>
50#endif
51
Naresh Tanniru66cf06c2019-03-20 19:30:37 +053052#define BT_IPC_SOURCE_LIB_NAME "btaudio_offload_if.so"
Florian Pfister1a84f312018-07-19 14:38:18 +020053#define BT_IPC_SINK_LIB_NAME "libbthost_if_sink.so"
54#define MEDIA_FMT_NONE 0
55#define MEDIA_FMT_AAC 0x00010DA6
56#define MEDIA_FMT_APTX 0x000131ff
57#define MEDIA_FMT_APTX_HD 0x00013200
Surendar Karka2febd452018-12-13 17:56:43 +053058#define MEDIA_FMT_APTX_AD 0x00013204
Florian Pfister1a84f312018-07-19 14:38:18 +020059#define MEDIA_FMT_SBC 0x00010BF2
60#define MEDIA_FMT_CELT 0x00013221
61#define MEDIA_FMT_LDAC 0x00013224
62#define MEDIA_FMT_MP3 0x00010BE9
63#define MEDIA_FMT_APTX_ADAPTIVE 0x00013204
Zhou Song12c29502019-03-16 10:37:18 +080064#define MEDIA_FMT_APTX_AD_SPEECH 0x00013208
Manisha Agarwald45632b2019-10-17 18:14:28 +080065#define MEDIA_FMT_LC3 0x0001337E
Naresh Tanniru9d027a62015-03-13 01:32:10 +053066#define MEDIA_FMT_AAC_AOT_LC 2
67#define MEDIA_FMT_AAC_AOT_SBR 5
68#define MEDIA_FMT_AAC_AOT_PS 29
Naresh Tanniru9d027a62015-03-13 01:32:10 +053069#define PCM_CHANNEL_L 1
70#define PCM_CHANNEL_R 2
71#define PCM_CHANNEL_C 3
72#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
73#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
74#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
75#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
76#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
77#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
Surendar Karka2febd452018-12-13 17:56:43 +053078#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
Zhou Song8fccbb62019-03-20 01:08:19 +080079#define MIXER_ENC_APTX_AD_CONFIG_BLOCK "SLIM_7_RX APTX_AD Enc Cfg"
Surendar Karka2febd452018-12-13 17:56:43 +053080#define MIXER_SOURCE_DEC_CONFIG_BLOCK "SLIM_7_TX Decoder Config"
81#define MIXER_SINK_DEC_CONFIG_BLOCK "SLIM_9_TX Decoder Config"
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +053082#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
Florian Pfister1a84f312018-07-19 14:38:18 +020083#define MIXER_DEC_BIT_FORMAT "AFE Output Bit Format"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053084#define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080085#define MIXER_SAMPLE_RATE_RX "BT SampleRate RX"
Surendar Karka2febd452018-12-13 17:56:43 +053086#define MIXER_SOURCE_SAMPLE_RATE_TX "BT SampleRate TX"
Samyak Jainf69e9ef2018-06-12 12:26:37 +053087#define MIXER_SAMPLE_RATE_DEFAULT "BT SampleRate"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053088#define MIXER_AFE_IN_CHANNELS "AFE Input Channels"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080089#define MIXER_ABR_TX_FEEDBACK_PATH "A2DP_SLIM7_UL_HL Switch"
Zhou Song12c29502019-03-16 10:37:18 +080090#define MIXER_ABR_RX_FEEDBACK_PATH "SCO_SLIM7_DL_HL Switch"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080091#define MIXER_SET_FEEDBACK_CHANNEL "BT set feedback channel"
Surendar Karka2febd452018-12-13 17:56:43 +053092#define MIXER_SINK_SAMPLE_RATE "BT_TX SampleRate"
Florian Pfister1a84f312018-07-19 14:38:18 +020093#define MIXER_AFE_SINK_CHANNELS "AFE Output Channels"
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");
sriram kumarcd549b02021-02-12 14:27:17 +05301171 if (a2dp.audio_source_start == NULL) {
1172 a2dp.audio_source_start = (audio_source_start_t)
1173 dlsym(a2dp.bt_lib_source_handle, "audio_stream_start");
1174 }
Srinu Jellada99a592019-01-25 16:50:52 +05301175 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
1176 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
1177 a2dp.audio_source_suspend = (audio_source_suspend_t)
1178 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
sriram kumarcd549b02021-02-12 14:27:17 +05301179 if (a2dp.audio_source_suspend == NULL) {
1180 a2dp.audio_source_suspend = (audio_source_suspend_t)
1181 dlsym(a2dp.bt_lib_source_handle, "audio_stream_suspend");
1182 }
Srinu Jellada99a592019-01-25 16:50:52 +05301183 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
1184 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
1185 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1186 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
sriram kumarcd549b02021-02-12 14:27:17 +05301187 if (a2dp.clear_source_a2dpsuspend_flag == NULL) {
1188 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1189 dlsym(a2dp.bt_lib_source_handle, "clear_a2dp_suspend_flag");
1190 }
Srinu Jellada99a592019-01-25 16:50:52 +05301191 a2dp.audio_source_stop = (audio_source_stop_t)
1192 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
sriram kumarcd549b02021-02-12 14:27:17 +05301193 if (a2dp.audio_source_stop == NULL) {
1194 a2dp.audio_source_stop = (audio_source_stop_t)
1195 dlsym(a2dp.bt_lib_source_handle, "audio_stream_stop");
1196 }
Srinu Jellada99a592019-01-25 16:50:52 +05301197 a2dp.audio_source_close = (audio_source_close_t)
1198 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
1199 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
1200 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
1201 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
1202 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
1203 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1204 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1205 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1206 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1207
Srinu Jella793f5302019-07-19 15:53:55 +05301208 if (a2dp.bt_lib_source_handle && a2dp.bt_audio_pre_init) {
1209 ALOGD("calling BT module preinit");
1210 // fwk related check's will be done in the BT layer
1211 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001212 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001213}
1214
1215/* API to open BT IPC library to start IPC communication for BT Sink*/
1216static void open_a2dp_sink()
1217{
1218 ALOGD(" Open A2DP input start ");
1219 if (a2dp.bt_lib_sink_handle == NULL){
1220 ALOGD(" Requesting for BT lib handle");
1221 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1222
1223 if (a2dp.bt_lib_sink_handle == NULL) {
1224 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1225 } else {
1226 a2dp.audio_sink_start = (audio_sink_start_t)
1227 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1228 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1229 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1230 a2dp.audio_sink_stop = (audio_sink_stop_t)
1231 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1232 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1233 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1234 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1235 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1236 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301237 }
1238}
1239
1240static int close_a2dp_output()
1241{
1242 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001243
1244 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1245 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301246 return -ENOSYS;
1247 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001248
1249 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1250 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001251 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001252 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301253 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001254 a2dp.a2dp_source_started = false;
1255 a2dp.a2dp_source_total_active_session_requests = 0;
1256 a2dp.a2dp_source_suspended = false;
1257 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301258 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301259 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001260 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001261 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1262 stop_abr();
1263 a2dp.abr_config.is_abr_enabled = false;
1264 a2dp.abr_config.abr_started = false;
1265 a2dp.abr_config.imc_instance = 0;
1266 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001267 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301268 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001269
1270 return 0;
1271}
1272
1273static int close_a2dp_input()
1274{
1275 ALOGV("%s\n",__func__);
1276
1277 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1278 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1279 return -ENOSYS;
1280 }
1281
1282 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1283 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001284 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001285 ALOGE("failed close a2dp sink control path from BT library");
1286 }
1287 a2dp.a2dp_sink_started = false;
1288 a2dp.a2dp_sink_total_active_session_requests = 0;
1289 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1290 a2dp.dec_sampling_rate = 48000;
1291 a2dp.dec_channels = 2;
1292 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301293
1294 return 0;
1295}
1296
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301297static void a2dp_check_and_set_scrambler()
1298{
1299 bool scrambler_mode = false;
1300 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001301 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1302 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301303
1304 if (scrambler_mode) {
1305 //enable scrambler in dsp
1306 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1307 MIXER_SCRAMBLER_MODE);
1308 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001309 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301310 return;
1311 } else {
1312 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1313 ALOGE("%s: Could not set scrambler mode", __func__);
1314 return;
1315 }
1316 }
1317 }
1318}
1319
Florian Pfister1a84f312018-07-19 14:38:18 +02001320static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301321{
Florian Pfister1a84f312018-07-19 14:38:18 +02001322 char *rate_str = NULL, *channels = NULL;
1323 uint32_t sampling_rate;
1324 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1325 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301326
Florian Pfister1a84f312018-07-19 14:38:18 +02001327 if (direction == SINK) {
1328 sampling_rate = a2dp.dec_sampling_rate;
1329 } else {
1330 sampling_rate = a2dp.enc_sampling_rate;
1331 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001332 /*
1333 * For LDAC encoder and AAC decoder open slimbus port at
1334 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1335 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1336 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001337 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301338 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001339 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1340 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301341 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1342 sampling_rate = sampling_rate *2;
1343 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001344
Manisha Agarwald45632b2019-10-17 18:14:28 +08001345 if (a2dp.bt_encoder_format == CODEC_TYPE_LC3)
1346 sampling_rate = SAMPLING_RATE_96K;
1347
Aalique Grahame22e49102018-12-18 14:23:57 -08001348 // No need to configure backend for PCM format.
1349 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1350 return 0;
1351 }
1352
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301353 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301354 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301355 case 44100:
1356 rate_str = "KHZ_44P1";
1357 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301358 case 88200:
1359 rate_str = "KHZ_88P2";
1360 break;
1361 case 96000:
1362 rate_str = "KHZ_96";
1363 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001364 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301365 default:
1366 rate_str = "KHZ_48";
1367 break;
1368 }
1369
Florian Pfister1a84f312018-07-19 14:38:18 +02001370 if (direction == SINK) {
1371 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1372 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301373 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001374 } else {
1375 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1376 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301377 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001378 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301379 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001380
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301381 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1382 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301383 is_configured = false;
1384 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301385 }
1386
Surendar Karka2febd452018-12-13 17:56:43 +05301387 if (direction == SOURCE) {
1388 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001389 if (a2dp.abr_config.is_abr_enabled) {
Manisha Agarwald45632b2019-10-17 18:14:28 +08001390 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH ||
1391 a2dp.bt_encoder_format == CODEC_TYPE_LC3)
Zhou Song12c29502019-03-16 10:37:18 +08001392 rate_str = SPEECH_TX_SAMPLE_RATE;
1393 else
1394 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301395
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301396 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1397 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1398 MIXER_SOURCE_SAMPLE_RATE_TX);
1399 if (!ctl_sample_rate) {
1400 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1401 is_configured = false;
1402 goto fail;
1403 }
1404
1405 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1406 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1407 is_configured = false;
1408 goto fail;
1409 }
Surendar Karka2febd452018-12-13 17:56:43 +05301410 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301411 }
1412 } else {
1413 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1414 MIXER_SAMPLE_RATE_TX is not supported */
1415 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1416 MIXER_SAMPLE_RATE_DEFAULT);
1417 if (!ctl_sample_rate) {
1418 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301419 is_configured = false;
1420 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301421 }
1422
1423 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1424 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001425 is_configured = false;
1426 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301427 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301428 }
1429
Florian Pfister1a84f312018-07-19 14:38:18 +02001430 if (direction == SINK) {
1431 switch (a2dp.dec_channels) {
1432 case 1:
1433 channels = "One";
1434 break;
1435 case 2:
1436 default:
1437 channels = "Two";
1438 break;
1439 }
1440
Ramu Gottipati02809682018-12-19 16:46:12 +05301441 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001442 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1443 MIXER_AFE_SINK_CHANNELS);
1444 } else {
1445 //Configure AFE enc channels
1446 switch (a2dp.enc_channels) {
1447 case 1:
1448 channels = "One";
1449 break;
1450 case 2:
1451 default:
1452 channels = "Two";
1453 break;
1454 }
1455
Ramu Gottipati02809682018-12-19 16:46:12 +05301456 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001457 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1458 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301459 }
1460
Florian Pfister1a84f312018-07-19 14:38:18 +02001461 if (!ctrl_channels) {
1462 ALOGE(" ERROR AFE channels mixer control not identified");
1463 } else {
1464 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301465 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001466 is_configured = false;
1467 goto fail;
1468 }
1469 }
1470 is_configured = true;
1471fail:
1472 return is_configured;
1473}
1474
Aniket Kumar Lata990de552019-07-11 14:20:23 -07001475bool a2dp_set_source_backend_cfg()
1476{
1477 if (a2dp.a2dp_source_started && !a2dp.a2dp_source_suspended)
1478 return a2dp_set_backend_cfg(SOURCE);
1479
1480 return false;
1481}
1482
Surendar Karkabbb3c822018-11-12 13:00:38 +05301483bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001484{
1485 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1486 struct aac_dec_cfg_t aac_dsp_cfg;
1487 bool is_configured = false;
1488 int ret = 0;
1489
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001490 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001491 return false;
1492
Surendar Karka2febd452018-12-13 17:56:43 +05301493 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001494 if (!ctl_dec_data) {
1495 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1496 is_configured = false;
1497 goto fail;
1498 }
1499
1500 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1501 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1502 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1503 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1504 switch(aac_bt_cfg->audio_object_type) {
1505 case 0:
1506 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301507 break;
1508 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001509 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1510 break;
1511 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301512 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001513 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301514 break;
1515 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001516 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1517 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1518 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1519 sizeof(struct aac_dec_cfg_t));
1520 if (ret != 0) {
1521 ALOGE("%s: failed to set AAC decoder config", __func__);
1522 is_configured = false;
1523 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001524 }
1525
Florian Pfister1a84f312018-07-19 14:38:18 +02001526 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1527 MIXER_DEC_BIT_FORMAT);
1528 if (!ctrl_bit_format) {
1529 ALOGE(" ERROR Dec bit format mixer control not identified");
1530 is_configured = false;
1531 goto fail;
1532 }
1533 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1534 if (ret != 0) {
1535 ALOGE("%s: Failed to set bit format to decoder", __func__);
1536 is_configured = false;
1537 goto fail;
1538 }
1539
1540 is_configured = true;
1541 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1542 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1543 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1544 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1545 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1546fail:
1547 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301548}
1549
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301550static int a2dp_set_bit_format(uint32_t enc_bit_format)
1551{
1552 const char *bit_format = NULL;
1553 struct mixer_ctl *ctrl_bit_format = NULL;
1554
1555 // Configure AFE Input Bit Format
1556 switch (enc_bit_format) {
1557 case 32:
1558 bit_format = "S32_LE";
1559 break;
1560 case 24:
1561 bit_format = "S24_LE";
1562 break;
1563 case 16:
1564 default:
1565 bit_format = "S16_LE";
1566 break;
1567 }
1568
1569 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1570 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1571 MIXER_ENC_BIT_FORMAT);
1572 if (!ctrl_bit_format) {
1573 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1574 return -ENOSYS;
1575 }
1576 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1577 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1578 return -ENOSYS;
1579 }
1580 return 0;
1581}
1582
Surendar Karka2febd452018-12-13 17:56:43 +05301583static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301584{
Surendar Karka2febd452018-12-13 17:56:43 +05301585 const char *rate_str = "KHZ_8", *channels = "Zero";
1586 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1587 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301588
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001589 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301590 if (direction == SINK) {
1591 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1592 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1593 MIXER_SINK_SAMPLE_RATE);
1594 } else {
1595 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1596 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1597 MIXER_SAMPLE_RATE_RX);
1598 }
1599 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301600
Surendar Karka2febd452018-12-13 17:56:43 +05301601 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1602 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301603 return -ENOSYS;
1604 }
Zhou Song12c29502019-03-16 10:37:18 +08001605 if (a2dp.abr_config.is_abr_enabled) {
1606 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1607 MIXER_SOURCE_SAMPLE_RATE_TX);
1608 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301609 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1610 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001611 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301612
Zhou Song12c29502019-03-16 10:37:18 +08001613 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1614 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1615 return -ENOSYS;
1616 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301617 }
1618 } else {
1619
Surendar Karka2febd452018-12-13 17:56:43 +05301620 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301621 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301622 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301623 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1624 return -ENOSYS;
1625 }
1626
Surendar Karka2febd452018-12-13 17:56:43 +05301627 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301628 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1629 return -ENOSYS;
1630 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001631 }
1632
1633 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301634 if (direction == SINK) {
1635 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1636 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1637 MIXER_AFE_SINK_CHANNELS);
1638 } else {
1639 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1640 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1641 MIXER_AFE_IN_CHANNELS);
1642 }
1643 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001644 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1645 return -ENOSYS;
1646 }
Surendar Karka2febd452018-12-13 17:56:43 +05301647 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001648 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1649 return -ENOSYS;
1650 }
1651
1652 return 0;
1653}
1654
1655/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301656static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001657{
1658 struct mixer_ctl *ctl_dec_data = NULL;
1659 struct abr_dec_cfg_t dec_cfg;
1660 int ret = 0;
1661
1662 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301663 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001664 if (!ctl_dec_data) {
1665 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1666 return false;
1667 }
1668 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1669 dec_cfg.dec_format = dec_format;
1670 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1671 dec_cfg.imc_info.enable = IMC_ENABLE;
1672 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1673 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1674
1675 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1676 sizeof(dec_cfg));
1677 if (ret != 0) {
1678 ALOGE("%s: Failed to set decoder config", __func__);
1679 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301680 }
Surendar Karka2febd452018-12-13 17:56:43 +05301681 }
1682
1683 return true;
1684}
1685
Surendar Karkabbb3c822018-11-12 13:00:38 +05301686bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001687{
1688 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1689 struct sbc_dec_cfg_t sbc_dsp_cfg;
1690 bool is_configured = false;
1691 int ret = 0;
1692
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001693 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001694 goto fail;
1695
Surendar Karka2febd452018-12-13 17:56:43 +05301696 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001697 if (!ctl_dec_data) {
1698 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1699 is_configured = false;
1700 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301701 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001702
Florian Pfister1a84f312018-07-19 14:38:18 +02001703 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1704 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1705 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1706 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1707 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1708 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301709
Florian Pfister1a84f312018-07-19 14:38:18 +02001710 if (ret != 0) {
1711 ALOGE("%s: failed to set SBC decoder config", __func__);
1712 is_configured = false;
1713 goto fail;
1714 }
1715
1716 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1717 MIXER_DEC_BIT_FORMAT);
1718 if (!ctrl_bit_format) {
1719 ALOGE(" ERROR Dec bit format mixer control not identified");
1720 is_configured = false;
1721 goto fail;
1722 }
1723 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1724 if (ret != 0) {
1725 ALOGE("%s: Failed to set bit format to decoder", __func__);
1726 is_configured = false;
1727 goto fail;
1728 }
1729
1730 is_configured = true;
1731 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1732 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1733 a2dp.dec_channels = 1;
1734 else
1735 a2dp.dec_channels = 2;
1736 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1737 ALOGV("Successfully updated SBC dec format");
1738fail:
1739 return is_configured;
1740}
1741
Florian Pfister1a84f312018-07-19 14:38:18 +02001742/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301743static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301744{
Florian Pfister1a84f312018-07-19 14:38:18 +02001745 void *codec_info = NULL;
1746 codec_t codec_type = CODEC_TYPE_INVALID;
1747 bool is_configured = false;
1748 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301749
Florian Pfister1a84f312018-07-19 14:38:18 +02001750 if (!a2dp.audio_get_dec_config) {
1751 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1752 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301753 }
1754
Surendar Karka2febd452018-12-13 17:56:43 +05301755 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001756 if (!ctl_dec_data) {
1757 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1758 is_configured = false;
1759 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301760 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001761 codec_info = a2dp.audio_get_dec_config(&codec_type);
1762 switch(codec_type) {
1763 case CODEC_TYPE_SBC:
1764 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301765 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001766 break;
1767 case CODEC_TYPE_AAC:
1768 ALOGD(" AAC decoder supported BT device");
1769 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301770 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001771 break;
1772 default:
1773 ALOGD(" Received Unsupported decoder format");
1774 is_configured = false;
1775 break;
1776 }
1777 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301778}
1779
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301780/* API to configure SBC DSP encoder */
1781bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1782{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301783 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301784 struct sbc_enc_cfg_t sbc_dsp_cfg;
1785 bool is_configured = false;
1786 int ret = 0;
1787
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001788 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301789 return false;
1790
1791 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1792 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001793 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301794 is_configured = false;
1795 goto fail;
1796 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301797 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001798 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301799 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1800 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1801 switch(sbc_bt_cfg->channels) {
1802 case 0:
1803 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1804 break;
1805 case 1:
1806 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1807 break;
1808 case 3:
1809 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1810 break;
1811 case 2:
1812 default:
1813 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1814 break;
1815 }
1816 if (sbc_bt_cfg->alloc)
1817 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1818 else
1819 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1820 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1821 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1822 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1823 sizeof(struct sbc_enc_cfg_t));
1824 if (ret != 0) {
1825 ALOGE("%s: failed to set SBC encoder config", __func__);
1826 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301827 goto fail;
1828 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301829 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301830 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301831 is_configured = false;
1832 goto fail;
1833 }
1834 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001835 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301836 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301837
1838 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1839 a2dp.enc_channels = 1;
1840 else
1841 a2dp.enc_channels = 2;
1842
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301843 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1844 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301845fail:
1846 return is_configured;
1847}
1848
Manish Dewangan6a252632017-12-04 17:27:44 +05301849#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301850static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1851 audio_aptx_encoder_config *aptx_bt_cfg)
1852{
1853 int ret = 0;
1854
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001855 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301856 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1857 aptx_dsp_cfg, aptx_bt_cfg);
1858 return -EINVAL;
1859 }
1860
1861 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301862 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301863
1864
1865 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1866 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1867 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1868 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1869 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1870 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1871 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1872 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1873 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301874 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301875 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1876 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1877 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1878 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1879
1880
1881 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1882 case APTX_AD_CHANNEL_UNCHANGED:
1883 case APTX_AD_CHANNEL_JOINT_STEREO:
1884 case APTX_AD_CHANNEL_DUAL_MONO:
1885 case APTX_AD_CHANNEL_STEREO_TWS:
1886 case APTX_AD_CHANNEL_EARBUD:
1887 default:
1888 a2dp.enc_channels = CH_STEREO;
1889 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1890 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1891 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1892 break;
1893 case APTX_AD_CHANNEL_MONO:
1894 a2dp.enc_channels = CH_MONO;
1895 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1896 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001897 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301898 }
1899 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1900 case APTX_AD_SR_UNCHANGED:
1901 case APTX_AD_48:
1902 default:
1903 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1904 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1905 break;
1906 case APTX_AD_44_1:
1907 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1908 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001909 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301910 }
1911 ALOGV("Successfully updated APTX AD enc format with \
1912 samplingrate: %d channels:%d",
1913 aptx_dsp_cfg->custom_cfg.sample_rate,
1914 aptx_dsp_cfg->custom_cfg.num_channels);
1915
1916 return ret;
1917}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301918
Zhou Song8fccbb62019-03-20 01:08:19 +08001919static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
1920 audio_aptx_encoder_config *aptx_bt_cfg)
1921{
1922 int ret = 0;
1923
1924 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Mingshu Pangaa429f72019-06-10 17:25:00 +08001925 ALOGE("Invalid param, aptx_dsp_cfg %pK aptx_bt_cfg %pK",
Zhou Song8fccbb62019-03-20 01:08:19 +08001926 aptx_dsp_cfg, aptx_bt_cfg);
1927 return -EINVAL;
1928 }
1929
1930 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
1931 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
1932
1933
1934 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1935 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1936 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1937 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1938 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1939 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1940 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1941 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1942 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
1943 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
1944 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
1945 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
1946 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
1947 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
1948 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1949 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1950 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1951 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1952
1953
1954 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1955 case APTX_AD_CHANNEL_UNCHANGED:
1956 case APTX_AD_CHANNEL_JOINT_STEREO:
1957 case APTX_AD_CHANNEL_DUAL_MONO:
1958 case APTX_AD_CHANNEL_STEREO_TWS:
1959 case APTX_AD_CHANNEL_EARBUD:
1960 default:
1961 a2dp.enc_channels = CH_STEREO;
1962 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1963 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1964 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1965 break;
1966 case APTX_AD_CHANNEL_MONO:
1967 a2dp.enc_channels = CH_MONO;
1968 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1969 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001970 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001971 }
1972 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1973 case APTX_AD_SR_UNCHANGED:
1974 case APTX_AD_48:
1975 default:
1976 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1977 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1978 break;
1979 case APTX_AD_44_1:
1980 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1981 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001982 break;
1983 case APTX_AD_96:
1984 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
1985 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_96K;
1986 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001987 }
1988 ALOGV("Successfully updated APTX AD enc format with \
1989 samplingrate: %d channels:%d",
1990 aptx_dsp_cfg->custom_cfg.sample_rate,
1991 aptx_dsp_cfg->custom_cfg.num_channels);
1992
1993 return ret;
1994}
1995
Manisha Agarwala51768b2018-11-01 16:30:52 +05301996static void audio_a2dp_update_tws_channel_mode()
1997{
1998 char* channel_mode;
1999 struct mixer_ctl *ctl_channel_mode;
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05302000
2001 ALOGD("Update tws for mono_mode on=%d",a2dp.is_tws_mono_mode_on);
2002
Manisha Agarwala51768b2018-11-01 16:30:52 +05302003 if (a2dp.is_tws_mono_mode_on)
2004 channel_mode = "One";
2005 else
2006 channel_mode = "Two";
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05302007
Manisha Agarwala51768b2018-11-01 16:30:52 +05302008 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2009 if (!ctl_channel_mode) {
2010 ALOGE("failed to get tws mixer ctl");
2011 return;
2012 }
2013 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2014 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2015 return;
2016 }
2017}
2018
Manisha Agarwald45632b2019-10-17 18:14:28 +08002019static void audio_a2dp_update_lc3_channel_mode()
2020{
2021 char* channel_mode;
2022 struct mixer_ctl *ctl_channel_mode;
2023
2024 ALOGD("Update lc3 for mono_mode on=%d",a2dp.is_lc3_mono_mode_on);
2025
2026 if (a2dp.is_lc3_mono_mode_on)
2027 channel_mode = "One";
2028 else
2029 channel_mode = "Two";
2030
2031 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_LC3_CHANNEL_MODE);
2032 if (!ctl_channel_mode) {
2033 ALOGE("failed to get lc3 mixer ctl");
2034 return;
2035 }
2036 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2037 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2038 return;
2039 }
2040}
2041
Manish Dewangan6a252632017-12-04 17:27:44 +05302042static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
2043 audio_aptx_encoder_config *aptx_bt_cfg)
2044{
2045 int ret = 0;
2046
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002047 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05302048 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
2049 aptx_dsp_cfg, aptx_bt_cfg);
2050 return -EINVAL;
2051 }
2052
2053 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002054 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05302055
2056 if (!a2dp.is_aptx_dual_mono_supported) {
2057 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
2058 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
2059 } else {
2060 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
2061 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
2062 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
2063 }
2064
2065 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
2066 case 1:
2067 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2068 break;
2069 case 2:
2070 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05302071 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2072 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
Manish Dewangan6a252632017-12-04 17:27:44 +05302073 break;
2074 }
2075 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
2076 if (!a2dp.is_aptx_dual_mono_supported) {
2077 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
2078 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
2079 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
2080 aptx_dsp_cfg->custom_cfg.num_channels);
2081 } else {
2082 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
2083 ALOGV("Successfully updated APTX dual mono enc format with \
2084 samplingrate: %d channels:%d syncmode %d",
2085 aptx_dsp_cfg->custom_cfg.sample_rate,
2086 aptx_dsp_cfg->custom_cfg.num_channels,
2087 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
2088 }
2089 return ret;
2090}
2091#else
2092static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
2093 audio_aptx_encoder_config *aptx_bt_cfg)
2094{
2095 int ret = 0;
2096
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002097 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05302098 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
2099 aptx_dsp_cfg, aptx_bt_cfg);
2100 return -EINVAL;
2101 }
2102
2103 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002104 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05302105 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
2106 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
2107 switch(aptx_dsp_cfg->num_channels) {
2108 case 1:
2109 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
2110 break;
2111 case 2:
2112 default:
2113 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
2114 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
2115 break;
2116 }
2117
2118 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
2119 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
2120
2121 return ret;
2122}
2123#endif
2124
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302125/* API to configure APTX DSP encoder */
2126bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2127{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302128 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08002129 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08002130 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302131 bool is_configured = false;
2132 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08002133 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05302134
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002135 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05302136 return false;
2137
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002138 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2139 if (!ctl_enc_data) {
2140 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2141 return false;
2142 }
2143
Manish Dewangan6a252632017-12-04 17:27:44 +05302144#ifndef LINUX_ENABLED
2145 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302146 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08002147 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002148 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08002149 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
2150 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002151 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08002152 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002153 else
Zhou Song8fccbb62019-03-20 01:08:19 +08002154 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002155 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002156 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002157
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002158 if (ret) {
2159 is_configured = false;
2160 goto fail;
2161 }
2162
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002163 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08002164 if (aptx_ad_ctl)
2165 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002166 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08002167 else
2168 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002169 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002170 } else {
2171 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002172 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302173 }
Manish Dewangan6a252632017-12-04 17:27:44 +05302174#else
2175 struct custom_enc_cfg_t aptx_dsp_cfg;
2176 mixer_size = sizeof(struct custom_enc_cfg_t);
2177 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05302178 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05302179 if (ret) {
2180 is_configured = false;
2181 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302182 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002183 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
2184 mixer_size);
2185#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302186 if (ret != 0) {
2187 ALOGE("%s: Failed to set APTX encoder config", __func__);
2188 is_configured = false;
2189 goto fail;
2190 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302191#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002192 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302193 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002194 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302195 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
2196 else
2197 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302198#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302199 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302200 is_configured = false;
2201 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302202 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302203 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302204 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05302205 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302206 else
Surendar Karka2febd452018-12-13 17:56:43 +05302207 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302208fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05302209 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002210 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05302211 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302212 return is_configured;
2213}
2214
2215/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302216 */
Manish Dewangan6a252632017-12-04 17:27:44 +05302217#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002218bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302219#else
2220bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2221#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302222{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302223 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302224 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302225 bool is_configured = false;
2226 int ret = 0;
2227
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002228 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302229 return false;
2230
2231 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2232 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002233 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302234 is_configured = false;
2235 goto fail;
2236 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302237
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302238 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002239 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302240 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2241 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2242 switch(aptx_dsp_cfg.num_channels) {
2243 case 1:
2244 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2245 break;
2246 case 2:
2247 default:
2248 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2249 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2250 break;
2251 }
2252 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302253 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302254 if (ret != 0) {
2255 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2256 is_configured = false;
2257 goto fail;
2258 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302259 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302260 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302261 is_configured = false;
2262 goto fail;
2263 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302264 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002265 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302266 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302267 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302268 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2269 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302270fail:
2271 return is_configured;
2272}
2273
2274/* API to configure AAC DSP encoder */
2275bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2276{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302277 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302278 struct aac_enc_cfg_t aac_dsp_cfg;
2279 bool is_configured = false;
2280 int ret = 0;
2281
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002282 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302283 return false;
2284
2285 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2286 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002287 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302288 is_configured = false;
2289 goto fail;
2290 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302291 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002292 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302293 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302294 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002295 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302296 case 0:
2297 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2298 break;
2299 case 2:
2300 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2301 break;
2302 case 1:
2303 default:
2304 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2305 break;
2306 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302307 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302308 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002309
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302310 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2311 sizeof(struct aac_enc_cfg_t));
2312 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002313 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302314 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302315 goto fail;
2316 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302317 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302318 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302319 is_configured = false;
2320 goto fail;
2321 }
2322 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002323 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302324 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002325 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002326 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2327 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2328fail:
2329 return is_configured;
2330}
2331
2332bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2333{
2334 struct mixer_ctl *ctl_enc_data = NULL;
2335 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2336 bool is_configured = false;
2337 int ret = 0;
2338
2339 if (aac_bt_cfg == NULL)
2340 return false;
2341
2342 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2343 if (!ctl_enc_data) {
2344 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2345 is_configured = false;
2346 goto fail;
2347 }
2348 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302349 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002350 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2351 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2352 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2353 case 0:
2354 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2355 break;
2356 case 2:
2357 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2358 break;
2359 case 1:
2360 default:
2361 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2362 break;
2363 }
2364 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2365 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2366 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2367 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2368
2369 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2370 sizeof(struct aac_enc_cfg_v2_t));
2371 if (ret != 0) {
2372 ALOGE("%s: Failed to set AAC encoder config", __func__);
2373 is_configured = false;
2374 goto fail;
2375 }
2376 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2377 if (ret != 0) {
2378 is_configured = false;
2379 goto fail;
2380 }
2381 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302382 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002383 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2384 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2385 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2386 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302387fail:
2388 return is_configured;
2389}
2390
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302391bool configure_aac_enc_format_v3(audio_aac_encoder_config_v3 *aac_bt_cfg)
2392{
2393 struct mixer_ctl *ctl_enc_data = NULL;
2394 struct aac_enc_cfg_v3_t aac_dsp_cfg;
2395 struct aac_frame_size_control_t* frame_vbr_ctl = NULL;
2396 bool is_configured = false;
2397 int ret = 0;
2398
2399 if (aac_bt_cfg == NULL)
2400 return false;
2401
2402 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2403 if (!ctl_enc_data) {
2404 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2405 is_configured = false;
2406 goto fail;
2407 }
2408 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v3_t));
2409 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
2410 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2411 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2412 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2413 case 0:
2414 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2415 break;
2416 case 2:
2417 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2418 break;
2419 case 1:
2420 default:
2421 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2422 break;
2423 }
2424 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2425 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2426 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2427 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2428 frame_vbr_ctl = aac_bt_cfg->frame_ptr_ctl;
2429
2430 if (frame_vbr_ctl != NULL) {
2431 aac_dsp_cfg.aac_key_value_ctl.ctl_type = frame_vbr_ctl->ctl_type;
2432 aac_dsp_cfg.aac_key_value_ctl.ctl_value = frame_vbr_ctl->ctl_value;
2433 } else {
2434 ALOGE("%s: VBR cannot be enabled, fall back to default",__func__);
2435 aac_dsp_cfg.aac_key_value_ctl.ctl_type = 0;
2436 aac_dsp_cfg.aac_key_value_ctl.ctl_value = 0;
2437 }
2438
2439 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2440 sizeof(struct aac_enc_cfg_v3_t));
2441 if (ret != 0) {
2442 ALOGE("%s: Failed to set AAC encoder config", __func__);
2443 is_configured = false;
2444 goto fail;
2445 }
2446 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2447 if (ret != 0) {
2448 is_configured = false;
2449 goto fail;
2450 }
2451 is_configured = true;
2452 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
2453 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2454 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2455 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2456 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
2457fail:
2458 return is_configured;
2459}
2460
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302461bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2462{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302463 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302464 struct celt_enc_cfg_t celt_dsp_cfg;
2465 bool is_configured = false;
2466 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002467 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302468 return false;
2469
2470 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2471 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002472 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302473 is_configured = false;
2474 goto fail;
2475 }
2476 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2477
Florian Pfister1a84f312018-07-19 14:38:18 +02002478 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302479 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2480 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2481 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2482 case 1:
2483 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2484 break;
2485 case 2:
2486 default:
2487 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2488 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2489 break;
2490 }
2491
2492 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2493
2494 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2495 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2496 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2497 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2498 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2499
2500 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2501 sizeof(struct celt_enc_cfg_t));
2502 if (ret != 0) {
2503 ALOGE("%s: Failed to set CELT encoder config", __func__);
2504 is_configured = false;
2505 goto fail;
2506 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302507 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302508 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302509 is_configured = false;
2510 goto fail;
2511 }
2512 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002513 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302514 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302515 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302516 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2517 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2518fail:
2519 return is_configured;
2520}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302521
2522bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2523{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302524 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302525 struct ldac_enc_cfg_t ldac_dsp_cfg;
2526 bool is_configured = false;
2527 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002528 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302529 return false;
2530
2531 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2532 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002533 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302534 is_configured = false;
2535 goto fail;
2536 }
2537 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2538
Florian Pfister1a84f312018-07-19 14:38:18 +02002539 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302540 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2541 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2542 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2543 case 4:
2544 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2545 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2546 break;
2547 case 2:
2548 case 1:
2549 default:
2550 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2551 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2552 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2553 break;
2554 }
2555
2556 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2557 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2558 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002559 if (ldac_bt_cfg->is_abr_enabled) {
2560 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2561 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2562 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2563 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2564 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002565 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002566 }
2567
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302568 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2569 sizeof(struct ldac_enc_cfg_t));
2570 if (ret != 0) {
2571 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2572 is_configured = false;
2573 goto fail;
2574 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302575 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302576 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302577 is_configured = false;
2578 goto fail;
2579 }
2580 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002581 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302582 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2583 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002584 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302585 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2586 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2587fail:
2588 return is_configured;
2589}
2590
Manisha Agarwald45632b2019-10-17 18:14:28 +08002591uint64_t convert_channel_map(uint32_t audio_location)
2592{
2593 int i;
2594 uint64_t channel_mask = (uint64_t) 0x00000000;
2595
2596 if (!audio_location) {
2597 channel_mask |= 1ULL << PCM_CHANNEL_C;
2598 return channel_mask;
2599 }
2600
2601 for (i = 0; i < AUDIO_LOCATION_MAX; i++) {
2602 if (audio_location & audio_location_map_array[i])
2603 channel_mask |= 1ULL << channel_map_array[i];
2604 }
2605
2606 return channel_mask;
2607}
2608
2609bool configure_lc3_enc_format(audio_lc3_codec_config_t *lc3_bt_cfg) {
2610 struct mixer_ctl *ctl_enc_data = NULL;
2611 int mixer_size = 0;
2612 int ret = 0;
2613 int i;
2614 bool is_configured = false;
2615 struct lc3_enc_cfg_t lc3_dsp_cfg;
2616 uint64_t channel_mask;
2617
2618 if (lc3_bt_cfg == NULL)
2619 return false;
2620
2621 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2622 if (!ctl_enc_data) {
2623 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
2624 return false;
2625 }
2626
2627 /* Initialize dsp configuration params */
2628 memset(&lc3_dsp_cfg, 0x0, sizeof(struct lc3_enc_cfg_t));
2629
2630 lc3_dsp_cfg.enc_format = MEDIA_FMT_LC3;
2631 //encoder structure
2632
2633 if (lc3_bt_cfg->enc_config.stream_map_size != 0) {
2634 if (!lc3_bt_cfg->enc_config.streamMapOut[0].audio_location)
2635 a2dp.enc_channels = CH_MONO;
2636 else
2637 a2dp.enc_channels = CH_STEREO;
2638 }
2639
2640 lc3_dsp_cfg.imc_info.direction = IMC_RECEIVE;
2641 lc3_dsp_cfg.imc_info.enable = IMC_ENABLE;
2642 lc3_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2643 lc3_dsp_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
2644 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.api_version = lc3_bt_cfg->enc_config.toAirConfig.api_version;
2645 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.sampling_freq = lc3_bt_cfg->enc_config.toAirConfig.sampling_freq;
2646 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.max_octets_per_frame = lc3_bt_cfg->enc_config.toAirConfig.max_octets_per_frame;
2647 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.frame_duration = lc3_bt_cfg->enc_config.toAirConfig.frame_duration;
2648 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.bit_depth = lc3_bt_cfg->enc_config.toAirConfig.bit_depth;
2649 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.num_blocks = lc3_bt_cfg->enc_config.toAirConfig.num_blocks;
2650 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.default_q_level = lc3_bt_cfg->enc_config.toAirConfig.default_q_level;
2651 for (i = 0; i < 16; i++)
2652 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.vendor_specific[i] = lc3_bt_cfg->enc_config.toAirConfig.vendor_specific[i];
2653 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.mode = lc3_bt_cfg->enc_config.toAirConfig.mode;
2654 lc3_dsp_cfg.enc_codec.to_Air_cfg.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
2655 lc3_dsp_cfg.enc_codec.streamMapToAir.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
2656
2657 for (i = 0; i < lc3_dsp_cfg.enc_codec.to_Air_cfg.stream_map_size; i++) {
2658 // for encoder stream map info
2659 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].audio_location = lc3_bt_cfg->enc_config.streamMapOut[i].audio_location;
2660 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
2661 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
2662 // for packetizer stream map info
2663 channel_mask = convert_channel_map(lc3_bt_cfg->enc_config.streamMapOut[i].audio_location);
2664 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
2665 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
2666 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].channel_mask_lsw = channel_mask & 0x00000000FFFFFFFF;
2667 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].channel_mask_msw = (channel_mask & 0xFFFFFFFF00000000) >> 32;
2668 }
2669
2670 /* Configure AFE DSP configuration */
2671 mixer_size = sizeof(struct lc3_enc_cfg_t);
2672 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&lc3_dsp_cfg,
2673 mixer_size);
2674 if (ret != 0) {
2675 ALOGE("%s: Failed to set lc3 encoder config", __func__);
2676 is_configured = false;
2677 goto fail;
2678 }
2679
2680 ret = a2dp_set_bit_format(ENCODER_BIT_FORMAT_PCM_24);
2681 if (ret != 0) {
2682 ALOGE("%s: Failed to set lc3 bit format", __func__);
2683 is_configured = false;
2684 goto fail;
2685 }
2686
2687 is_configured = true;
2688 a2dp.bt_encoder_format = CODEC_TYPE_LC3;
2689
2690fail:
2691 return is_configured;
2692}
2693
2694bool configure_lc3_dec_format(audio_lc3_codec_config_t *lc3_bt_cfg)
2695{
2696 struct mixer_ctl *ctl_dec_data = NULL;
2697 struct lc3_dec_cfg_t lc3_dsp_cfg;
2698 uint64_t channel_mask;
2699 bool is_configured = false;
2700 int ret = 0;
2701 int i;
2702
2703 if (lc3_bt_cfg == NULL)
2704 return false;
2705
2706 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
2707 if (!ctl_dec_data) {
2708 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
2709 return false;
2710 }
2711
2712 memset(&lc3_dsp_cfg, 0x0, sizeof(struct lc3_dec_cfg_t));
2713
2714 lc3_dsp_cfg.abr_cfg.dec_format = MEDIA_FMT_LC3;
2715 lc3_dsp_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
2716 lc3_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2717 lc3_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2718
2719 lc3_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
2720
2721 //for depacketizer both fromAir and toAir streamMap needs to be sent.
2722 lc3_dsp_cfg.dec_codec.streamMapToAir.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
2723
2724 for (i = 0; i < lc3_bt_cfg->enc_config.stream_map_size; i++) {
2725 // for depacketizer stream map info
2726 channel_mask = convert_channel_map(lc3_bt_cfg->enc_config.streamMapOut[i].audio_location);
2727 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
2728 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
2729 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].channel_mask_lsw = channel_mask & 0x00000000FFFFFFFF;
2730 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].channel_mask_msw = (channel_mask & 0xFFFFFFFF00000000) >> 32;
2731 }
2732
2733 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&lc3_dsp_cfg,
2734 sizeof(struct lc3_dec_cfg_t));
2735 if (ret != 0) {
2736 ALOGE("%s: failed to set LC3 decoder config", __func__);
2737 is_configured = false;
2738 goto fail;
2739 }
2740
2741 is_configured = true;
2742 a2dp.bt_decoder_format = CODEC_TYPE_LC3;
2743
2744fail:
2745 return is_configured;
2746}
2747
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302748bool configure_a2dp_encoder_format()
2749{
2750 void *codec_info = NULL;
2751 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002752 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302753 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002754 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302755
Florian Pfister1a84f312018-07-19 14:38:18 +02002756 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302757 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2758 return false;
2759 }
2760 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002761 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302762 &codec_type);
2763
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002764 // ABR disabled by default for all codecs
2765 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302766 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002767
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302768 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002769 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302770 ALOGD(" Received SBC encoder supported BT device");
2771 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002772 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302773 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002774 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302775 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302776#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002777 a2dp.is_aptx_dual_mono_supported = false;
2778 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302779#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302780 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002781 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302782 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002783 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302784 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302785#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302786 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002787 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302788#else
2789 is_configured =
2790 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2791#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002792 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302793#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002794 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002795 ALOGD(" Received APTX dual mono encoder supported BT device");
2796 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302797 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2798 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002799 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2800 is_configured =
2801 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302802 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302803#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002804 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302805 ALOGD(" Received AAC encoder supported BT device");
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302806 bool is_aac_vbr_enabled =
2807 property_get_bool("persist.vendor.bt.aac_vbr_frm_ctl.enabled", false);
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002808 bool is_aac_frame_ctl_enabled =
2809 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302810 if (is_aac_vbr_enabled)
2811 is_configured = configure_aac_enc_format_v3((audio_aac_encoder_config_v3 *) codec_info);
2812 else
2813 is_configured = is_aac_frame_ctl_enabled ?
2814 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2815 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302816 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002817 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302818 ALOGD(" Received CELT encoder supported BT device");
2819 is_configured =
2820 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2821 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002822 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302823 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002824 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2825 instance_id = MAX_INSTANCE_ID;
2826 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302827 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002828 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302829 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302830 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302831#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302832 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302833 ALOGD(" Received APTX AD encoder supported BT device");
2834 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2835 instance_id = MAX_INSTANCE_ID;
2836 a2dp.abr_config.imc_instance = instance_id--;
2837 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2838 a2dp.is_aptx_adaptive = true;
2839 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2840 is_configured =
2841 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302842 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302843 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302844#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002845 case CODEC_TYPE_PCM:
2846 ALOGD("Received PCM format for BT device");
2847 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2848 is_configured = true;
2849 break;
Manisha Agarwald45632b2019-10-17 18:14:28 +08002850 case CODEC_TYPE_LC3:
2851 ALOGD("Received LC3 encoder supported BT device");
2852 a2dp.bt_encoder_format = CODEC_TYPE_LC3;
2853 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2854 instance_id = MAX_INSTANCE_ID;
2855 a2dp.abr_config.imc_instance = instance_id--;
2856 a2dp.abr_config.is_abr_enabled = true;
2857 is_configured =
2858 (configure_lc3_enc_format((audio_lc3_codec_config_t *)codec_info) &&
2859 configure_lc3_dec_format((audio_lc3_codec_config_t *)codec_info));
2860 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302861 default:
2862 ALOGD(" Received Unsupported encoder formar");
2863 is_configured = false;
2864 break;
2865 }
2866 return is_configured;
2867}
2868
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002869int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302870{
2871 int ret = 0;
2872
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002873 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302874
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002875 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002876 && a2dp.audio_get_enc_config)) {
2877 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302878 return -ENOSYS;
2879 }
2880
Zhou Song12c29502019-03-16 10:37:18 +08002881 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302882 //session will be restarted after suspend completion
2883 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302884 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302885 }
2886
Florian Pfister1a84f312018-07-19 14:38:18 +02002887 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302888 ALOGD("calling BT module stream start");
2889 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002890 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302891 ALOGE("BT controller start return = %d",ret);
2892 if (ret != 0 ) {
2893 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002894 a2dp.a2dp_source_started = false;
Revathi Uddarajub26e3932020-06-10 14:51:02 +05302895 ret = -ETIMEDOUT;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302896 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002897 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002898 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302899 ret = 0;
2900 ALOGD("Start playback successful to BT library");
2901 } else {
2902 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002903 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302904 ret = -ETIMEDOUT;
2905 }
2906 }
2907 }
2908
Florian Pfister1a84f312018-07-19 14:38:18 +02002909 if (a2dp.a2dp_source_started) {
2910 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302911 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302912 audio_a2dp_update_tws_channel_mode();
Manisha Agarwald45632b2019-10-17 18:14:28 +08002913 audio_a2dp_update_lc3_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002914 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002915 if (a2dp.abr_config.is_abr_enabled)
2916 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302917 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302918
2919 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002920 a2dp.a2dp_source_total_active_session_requests);
2921 return ret;
2922}
2923
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002924uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002925{
2926 uint32_t latency = 0;
2927
2928 switch(a2dp.bt_decoder_format) {
2929 case CODEC_TYPE_SBC:
2930 latency = DEFAULT_SINK_LATENCY_SBC;
2931 break;
2932 case CODEC_TYPE_AAC:
2933 latency = DEFAULT_SINK_LATENCY_AAC;
2934 break;
2935 default:
2936 latency = 200;
2937 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2938 break;
2939 }
2940 return (uint64_t)latency;
2941}
2942
2943bool a2dp_send_sink_setup_complete(void) {
2944 uint64_t system_latency = 0;
2945 bool is_complete = false;
2946
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002947 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002948
2949 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2950 is_complete = true;
2951 }
2952 return is_complete;
2953}
2954
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002955bool a2dp_sink_is_ready()
2956{
2957 bool ret = false;
2958
2959 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2960 (a2dp.is_a2dp_offload_supported) &&
2961 (a2dp.audio_sink_check_a2dp_ready))
2962 ret = a2dp.audio_sink_check_a2dp_ready();
2963 return ret;
2964}
2965
2966int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002967{
2968 int ret = 0;
2969
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002970 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002971
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002972 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002973 && a2dp.audio_get_dec_config)) {
2974 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2975 return -ENOSYS;
2976 }
2977
2978 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2979 ALOGD("calling BT module stream start");
2980 /* This call indicates BT IPC lib to start capture */
2981 ret = a2dp.audio_sink_start();
2982 ALOGE("BT controller start capture return = %d",ret);
2983 if (ret != 0 ) {
2984 ALOGE("BT controller start capture failed");
2985 a2dp.a2dp_sink_started = false;
2986 } else {
2987
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002988 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002989 ALOGD("Wait for capture ready not successful");
2990 ret = -ETIMEDOUT;
2991 }
2992
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002993 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002994 a2dp.a2dp_sink_started = true;
2995 ret = 0;
2996 ALOGD("Start capture successful to BT library");
2997 } else {
2998 ALOGD(" unable to configure DSP decoder");
2999 a2dp.a2dp_sink_started = false;
3000 ret = -ETIMEDOUT;
3001 }
3002
3003 if (!a2dp_send_sink_setup_complete()) {
3004 ALOGD("sink_setup_complete not successful");
3005 ret = -ETIMEDOUT;
3006 }
3007 }
3008 }
3009
3010 if (a2dp.a2dp_sink_started) {
3011 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003012 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02003013 }
3014 }
3015
3016 ALOGD("start A2DP sink total active sessions :%d",
3017 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303018 return ret;
3019}
3020
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303021static void reset_a2dp_enc_config_params()
3022{
3023 int ret =0;
3024
Aalique Grahame22e49102018-12-18 14:23:57 -08003025 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303026 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05303027 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303028
3029 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
3030 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
3031 MIXER_ENC_CONFIG_BLOCK);
3032 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003033 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303034 } else {
3035 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
3036 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02003037 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303038 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003039
3040 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3041
Manisha Agarwala51768b2018-11-01 16:30:52 +05303042 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
3043
3044 if (!ctl_channel_mode) {
3045 ALOGE("failed to get tws mixer ctl");
3046 } else {
3047 channel_mode = "Two";
3048 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
3049 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
3050 }
3051 a2dp.is_tws_mono_mode_on = false;
3052 }
Manisha Agarwald45632b2019-10-17 18:14:28 +08003053
3054 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_LC3_CHANNEL_MODE);
3055
3056 if (!ctl_channel_mode) {
3057 ALOGE("failed to get lc3 mixer ctl");
3058 } else {
3059 channel_mode = "Two";
3060 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0)
3061 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
3062 a2dp.is_lc3_mono_mode_on = false;
3063 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303064}
3065
Surendar Karka2febd452018-12-13 17:56:43 +05303066static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07003067{
3068 struct mixer_ctl *ctl_dec_data = NULL;
3069 struct abr_dec_cfg_t dummy_reset_cfg;
3070 int ret = 0;
3071
Preetam Singh Ranawat25780e42019-07-31 18:15:57 +05303072 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3073 if (!ctl_dec_data) {
3074 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
3075 return -EINVAL;
3076 }
3077 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
3078 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
3079 sizeof(dummy_reset_cfg));
3080 if (ret != 0) {
3081 ALOGE("%s: Failed to set dummy decoder config", __func__);
3082 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07003083 }
3084
3085 return ret;
3086}
3087
Surendar Karka2febd452018-12-13 17:56:43 +05303088static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02003089{
3090 int ret =0;
3091
3092 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
3093 struct aac_dec_cfg_t dummy_reset_config;
3094
3095 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
3096 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05303097 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02003098 if (!ctl_dec_config) {
3099 ALOGE(" ERROR a2dp decoder format mixer control not identified");
3100 } else {
3101 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
3102 sizeof(struct aac_dec_cfg_t));
3103 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
3104 }
3105 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
3106 MIXER_DEC_BIT_FORMAT);
3107 if (!ctrl_bit_format) {
3108 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
3109 } else {
3110 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
3111 if (ret != 0) {
3112 ALOGE("%s: Failed to set bit format to decoder", __func__);
3113 }
3114 }
3115}
3116
Zhou Song12c29502019-03-16 10:37:18 +08003117static void reset_codec_config()
3118{
3119 reset_a2dp_enc_config_params();
3120 reset_a2dp_source_dec_config_params();
3121 a2dp_reset_backend_cfg(SOURCE);
3122 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
3123 stop_abr();
3124 a2dp.abr_config.is_abr_enabled = false;
3125}
3126
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003127int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303128{
3129 int ret =0;
3130
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003131 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003132 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003133 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303134 return -ENOSYS;
3135 }
3136
Florian Pfister1a84f312018-07-19 14:38:18 +02003137 if (a2dp.a2dp_source_total_active_session_requests > 0)
3138 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08003139 else
3140 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303141
Florian Pfister1a84f312018-07-19 14:38:18 +02003142 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303143 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02003144 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303145 if (ret < 0)
3146 ALOGE("stop stream to BT IPC lib failed");
3147 else
3148 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08003149 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
3150 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05303151 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303152 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003153 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02003154 a2dp.a2dp_source_started = false;
3155 ALOGD("Stop A2DP playback, total active sessions :%d",
3156 a2dp.a2dp_source_total_active_session_requests);
3157 return 0;
3158}
3159
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003160int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02003161{
3162 int ret =0;
3163
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003164 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003165 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003166 ALOGE("a2dp handle is not identified, Ignoring stop request");
3167 return -ENOSYS;
3168 }
3169
3170 if (a2dp.a2dp_sink_total_active_session_requests > 0)
3171 a2dp.a2dp_sink_total_active_session_requests--;
3172
3173 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
3174 ALOGV("calling BT module stream stop");
3175 ret = a2dp.audio_sink_stop();
3176 if (ret < 0)
3177 ALOGE("stop stream to BT IPC lib failed");
3178 else
3179 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05303180 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02003181 a2dp_reset_backend_cfg(SINK);
3182 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003183 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02003184 a2dp.a2dp_source_started = false;
3185 ALOGD("Stop A2DP capture, total active sessions :%d",
3186 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303187 return 0;
3188}
3189
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003190int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303191{
Weiyin Jiang280ea742020-09-08 20:28:22 +08003192 int ret = 0, val, status = 0;
3193 char value[32] = {0};
3194 struct audio_usecase *uc_info;
3195 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303196
Weiyin Jiang280ea742020-09-08 20:28:22 +08003197 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003198 ALOGV("no supported encoders identified,ignoring a2dp setparam");
3199 status = -EINVAL;
3200 goto param_handled;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003201 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303202
Weiyin Jiang280ea742020-09-08 20:28:22 +08003203 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303204 sizeof(value));
Weiyin Jiang280ea742020-09-08 20:28:22 +08003205 if (ret >= 0) {
3206 val = atoi(value);
3207 if (audio_is_a2dp_out_device(val)) {
3208 ALOGV("Received device connect request for A2DP source");
3209 open_a2dp_source();
3210 }
3211 goto param_handled;
3212 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303213
Weiyin Jiang280ea742020-09-08 20:28:22 +08003214 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
3215 sizeof(value));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303216
Weiyin Jiang280ea742020-09-08 20:28:22 +08003217 if (ret >= 0) {
3218 val = atoi(value);
3219 if (audio_is_a2dp_out_device(val)) {
3220 ALOGV("Received source device dis- connect request");
3221 close_a2dp_output();
3222 reset_a2dp_enc_config_params();
3223 reset_a2dp_source_dec_config_params();
3224 a2dp_reset_backend_cfg(SOURCE);
3225 } else if (audio_is_a2dp_in_device(val)) {
3226 ALOGV("Received sink device dis- connect request");
3227 close_a2dp_input();
3228 reset_a2dp_sink_dec_config_params();
3229 a2dp_reset_backend_cfg(SINK);
3230 }
3231 goto param_handled;
3232 }
Surendar Karka2febd452018-12-13 17:56:43 +05303233#ifndef LINUX_ENABLED
Weiyin Jiang280ea742020-09-08 20:28:22 +08003234 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
3235 if (ret >= 0) {
3236 ALOGD("Setting tws channel mode to %s",value);
3237 if (!(strncmp(value, "mono", strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05303238 a2dp.is_tws_mono_mode_on = true;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003239 else if (!(strncmp(value, "dual-mono", strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05303240 a2dp.is_tws_mono_mode_on = false;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003241 audio_a2dp_update_tws_channel_mode();
3242 goto param_handled;
3243 }
Manisha Agarwald45632b2019-10-17 18:14:28 +08003244
3245 ret = str_parms_get_str(parms, "LEAMono", value, sizeof(value));
3246 if (ret>=0) {
3247 ALOGD("Setting LC3 channel mode to %s",value);
3248 if (!(strncmp(value,"true",strlen(value))))
3249 a2dp.is_lc3_mono_mode_on = true;
3250 else
3251 a2dp.is_lc3_mono_mode_on = false;
3252 audio_a2dp_update_lc3_channel_mode();
3253 goto param_handled;
3254 }
Surendar Karka2febd452018-12-13 17:56:43 +05303255#endif
Weiyin Jiang280ea742020-09-08 20:28:22 +08003256 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
3257 if (ret >= 0) {
3258 if (a2dp.bt_lib_source_handle == NULL)
3259 goto param_handled;
3260
3261 if ((!strncmp(value, "true", sizeof(value)))) {
3262 if (a2dp.a2dp_source_suspended) {
3263 ALOGD("%s: A2DP is already suspended", __func__);
3264 goto param_handled;
3265 }
3266 ALOGD("Setting a2dp to suspend state");
3267 a2dp.a2dp_source_suspended = true;
3268 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
3269 goto param_handled;
3270 list_for_each(node, &a2dp.adev->usecase_list) {
3271 uc_info = node_to_item(node, struct audio_usecase, list);
3272 if (uc_info->type == PCM_PLAYBACK &&
3273 (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
3274 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
3275 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)) {
3276 fp_check_a2dp_restore_l(a2dp.adev, uc_info->stream.out, false);
Aalique Grahame22e49102018-12-18 14:23:57 -08003277 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08003278 }
3279 if (!a2dp.swb_configured)
3280 reset_codec_config();
3281 if (a2dp.audio_source_suspend)
3282 a2dp.audio_source_suspend();
3283 } else if (a2dp.a2dp_source_suspended == true) {
3284 ALOGD("Resetting a2dp suspend state");
3285 struct audio_usecase *uc_info;
3286 struct listnode *node;
3287 if (a2dp.clear_source_a2dpsuspend_flag)
3288 a2dp.clear_source_a2dpsuspend_flag();
3289 a2dp.a2dp_source_suspended = false;
3290 /*
3291 * It is possible that before suspend,a2dp sessions can be active
3292 * for example during music + voice activation concurrency
3293 * a2dp suspend will be called & BT will change to sco mode
3294 * though music is paused as a part of voice activation
3295 * compress session close happens only after pause timeout(10secs)
3296 * so if resume request comes before pause timeout as a2dp session
3297 * is already active IPC start will not be called from APM/audio_hw
3298 * Fix is to call a2dp start for IPC library post suspend
3299 * based on number of active session count
3300 */
3301 if (a2dp.a2dp_source_total_active_session_requests > 0) {
3302 ALOGD(" Calling IPC lib start post suspend state");
3303 if (a2dp.audio_source_start) {
3304 ret = a2dp.audio_source_start();
3305 if (ret != 0) {
3306 ALOGE("BT controller start failed");
3307 a2dp.a2dp_source_started = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303308 }
3309 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08003310 }
3311 list_for_each(node, &a2dp.adev->usecase_list) {
3312 uc_info = node_to_item(node, struct audio_usecase, list);
3313 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
3314 is_a2dp_out_device_type(&uc_info->stream.out->device_list)) {
3315 fp_check_a2dp_restore_l(a2dp.adev, uc_info->stream.out, true);
Zhou Song10617ed2017-05-26 13:28:48 +08003316 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303317 }
3318 }
3319 goto param_handled;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003320 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003321
Weiyin Jiang280ea742020-09-08 20:28:22 +08003322 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
3323 sizeof(value));
3324 if (ret >= 0) {
3325 if (a2dp.is_a2dp_offload_supported &&
3326 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
3327 *reconfig = true;
3328 }
3329 goto param_handled;
3330 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003331
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303332param_handled:
Weiyin Jiang280ea742020-09-08 20:28:22 +08003333 ALOGV("end of a2dp setparam");
3334 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303335}
3336
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003337void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05303338{
3339 a2dp.is_handoff_in_progress = is_on;
3340}
3341
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003342bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303343{
3344 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05303345 // Or if a2dp device is selected but earlier start failed ( as a2dp
3346 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02003347 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303348}
3349
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003350void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303351{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303352 *sample_rate = a2dp.enc_sampling_rate;
3353}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303354
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003355void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02003356{
3357 *sample_rate = a2dp.dec_sampling_rate;
3358}
3359
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003360bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303361{
3362 bool ret = false;
3363
Florian Pfister1a84f312018-07-19 14:38:18 +02003364 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303365 return ret;
3366
Florian Pfister1a84f312018-07-19 14:38:18 +02003367 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08003368 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02003369 (a2dp.audio_source_check_a2dp_ready))
3370 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303371 return ret;
3372}
3373
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003374bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02003375{
3376 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303377}
3378
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003379void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08003380 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303381{
3382 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02003383 a2dp.bt_lib_source_handle = NULL;
3384 a2dp.a2dp_source_started = false;
3385 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
3386 a2dp.a2dp_source_total_active_session_requests = 0;
3387 a2dp.a2dp_source_suspended = false;
3388 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303389 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303390 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07003391 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05303392 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08003393 a2dp.abr_config.is_abr_enabled = false;
3394 a2dp.abr_config.abr_started = false;
3395 a2dp.abr_config.imc_instance = 0;
3396 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08003397 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05303398 a2dp.is_tws_mono_mode_on = false;
Manisha Agarwald45632b2019-10-17 18:14:28 +08003399 a2dp.is_lc3_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05303400 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08003401 a2dp.swb_configured = false;
3402
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003403 // init function pointers
3404 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08003405 init_config.fp_platform_get_pcm_device_id;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003406 fp_check_a2dp_restore_l = init_config.fp_check_a2dp_restore_l;
Gautam Manamfbb3ebc2020-10-08 18:06:45 +05303407 fp_platform_switch_voice_call_device_post =
3408 init_config.fp_platform_switch_voice_call_device_post;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003409
kunleiz5a127262017-09-08 14:47:48 +08003410 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05303411 reset_a2dp_source_dec_config_params();
3412 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02003413
3414 a2dp.bt_lib_sink_handle = NULL;
3415 a2dp.a2dp_sink_started = false;
3416 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
3417 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003418
3419 if (is_running_with_enhanced_fwk == UNINITIALIZED)
3420 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
3421 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08003422 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02003423
3424 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303425 update_offload_codec_capabilities();
3426}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003427
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003428uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003429{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003430 uint32_t latency = 0;
3431 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303432 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
3433 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003434 char value[PROPERTY_VALUE_MAX];
3435
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003436 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08003437 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003438 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303439 if (sscanf(value, "%d/%d/%d/%d/%d%d",
3440 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003441 ALOGI("Failed to parse avsync offset params from '%s'.", value);
3442 avsync_runtime_prop = 0;
3443 }
3444 }
3445
yidongh0515e042017-07-06 15:00:34 +08003446 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02003447 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
3448 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08003449 }
3450
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07003451 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003452 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08003453 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
3454 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003455 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003456 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08003457 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
3458 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003459 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003460 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08003461 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
3462 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003463 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003464 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08003465 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
3466 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003467 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003468 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05303469 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
3470 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
3471 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003472 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303473 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
3474 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
3475 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05303476 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05303477 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05303478 break;
Manisha Agarwald45632b2019-10-17 18:14:28 +08003479 case CODEC_TYPE_LC3:
3480 latency = slatency;
3481 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08003482 case CODEC_TYPE_PCM:
3483 latency = ENCODER_LATENCY_PCM;
3484 latency += DEFAULT_SINK_LATENCY_PCM;
3485 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003486 default:
3487 latency = 200;
3488 break;
3489 }
3490 return latency;
3491}
Aalique Grahame22e49102018-12-18 14:23:57 -08003492
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003493int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003494 struct str_parms *reply)
3495{
3496 int ret, val = 0;
3497 char value[32]={0};
3498
3499 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3500 value, sizeof(value));
3501 if (ret >= 0) {
3502 val = a2dp.is_a2dp_offload_supported;
3503 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3504 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3505 }
3506
3507 return 0;
3508}
Zhou Song12c29502019-03-16 10:37:18 +08003509
3510
3511bool configure_aptx_ad_speech_enc_fmt() {
3512 struct mixer_ctl *ctl_enc_data = NULL;
3513 int mixer_size = 0;
3514 int ret = 0;
3515 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3516
3517 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3518 if (!ctl_enc_data) {
3519 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3520 return false;
3521 }
3522
3523 /* Initialize dsp configuration params */
3524 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3525 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3526 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3527 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3528 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3529 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3530 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3531 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3532 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3533 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3534 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3535
3536 /* Configure AFE DSP configuration */
3537 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3538 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3539 mixer_size);
3540 if (ret != 0) {
3541 ALOGE("%s: Failed to set SWB encoder config", __func__);
3542 return false;
3543 }
3544
3545 /* Configure AFE Input Bit Format as PCM_16 */
3546 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3547 if (ret != 0) {
3548 ALOGE("%s: Failed to set SWB bit format", __func__);
3549 return false;
3550 }
3551
3552 return true;
3553}
3554
3555bool configure_aptx_ad_speech_dec_fmt()
3556{
3557 struct mixer_ctl *ctl_dec_data = NULL;
3558 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3559 int ret = 0;
3560
3561 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3562 if (!ctl_dec_data) {
3563 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3564 return false;
3565 }
3566 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3567 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3568 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3569 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3570 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3571 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3572 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3573 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3574
3575 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3576 sizeof(dec_cfg));
3577 if (ret != 0) {
3578 ALOGE("%s: Failed to set decoder config", __func__);
3579 return false;
3580 }
3581 return true;
3582}
3583
3584int sco_start_configuration()
3585{
3586 ALOGD("sco_start_configuration start");
3587
3588 if (!a2dp.swb_configured) {
Gautam Manamfbb3ebc2020-10-08 18:06:45 +05303589 /*Before starting sco config, we must ensure to set correct acdb id
3590 because sco cofiguration will trigger port open which needs acdb_id*/
3591 fp_platform_switch_voice_call_device_post(a2dp.adev->platform,
3592 SND_DEVICE_OUT_BT_SCO_SWB,
3593 SND_DEVICE_IN_BT_SCO_MIC_SWB);
3594
Zhou Song12c29502019-03-16 10:37:18 +08003595 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3596 /* Configure AFE codec*/
3597 if (configure_aptx_ad_speech_enc_fmt() &&
3598 configure_aptx_ad_speech_dec_fmt()) {
3599 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3600 } else {
3601 ALOGE("%s: failed to send SCO configuration", __func__);
3602 return -ETIMEDOUT;
3603 }
3604 /* Configure backend*/
3605 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3606 a2dp.enc_channels = CH_MONO;
3607 a2dp.abr_config.is_abr_enabled = true;
3608 a2dp_set_backend_cfg(SOURCE);
3609 /* Start abr*/
3610 start_abr();
3611 a2dp.swb_configured = true;
3612 }
3613 return 0;
3614}
3615
3616void sco_reset_configuration()
3617{
Zhou Songd6d71752019-05-21 18:08:51 +08003618 if (a2dp.swb_configured) {
3619 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003620
Zhou Songd6d71752019-05-21 18:08:51 +08003621 reset_codec_config();
3622 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3623 a2dp.swb_configured = false;
3624 }
Zhou Song12c29502019-03-16 10:37:18 +08003625}