blob: 52c7dd39d5f31d6bc9713d4ff3ff8aa435447a6b [file] [log] [blame]
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301/*
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002* Copyright (c) 2015-2020, The Linux Foundation. All rights reserved.
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of The Linux Foundation nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
Aalique Grahame22e49102018-12-18 14:23:57 -080029#define LOG_TAG "a2dp_offload"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053030/*#define LOG_NDEBUG 0*/
31#define LOG_NDDEBUG 0
32#include <errno.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080033#include <log/log.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053034#include <dlfcn.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080035#include <pthread.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
Manish Dewangan6a252632017-12-04 17:27:44 +053039#include "audio_extn.h"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053040#include <stdlib.h>
41#include <cutils/str_parms.h>
42#include <hardware/audio.h>
43#include <hardware/hardware.h>
44#include <cutils/properties.h>
45
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046#ifdef DYNAMIC_LOG_ENABLED
47#include <log_xml_parser.h>
48#define LOG_MASK HAL_MOD_FILE_A2DP
49#include <log_utils.h>
50#endif
51
Naresh Tanniru66cf06c2019-03-20 19:30:37 +053052#define BT_IPC_SOURCE_LIB_NAME "btaudio_offload_if.so"
Florian Pfister1a84f312018-07-19 14:38:18 +020053#define BT_IPC_SINK_LIB_NAME "libbthost_if_sink.so"
54#define MEDIA_FMT_NONE 0
55#define MEDIA_FMT_AAC 0x00010DA6
56#define MEDIA_FMT_APTX 0x000131ff
57#define MEDIA_FMT_APTX_HD 0x00013200
Surendar Karka2febd452018-12-13 17:56:43 +053058#define MEDIA_FMT_APTX_AD 0x00013204
Florian Pfister1a84f312018-07-19 14:38:18 +020059#define MEDIA_FMT_SBC 0x00010BF2
60#define MEDIA_FMT_CELT 0x00013221
61#define MEDIA_FMT_LDAC 0x00013224
62#define MEDIA_FMT_MP3 0x00010BE9
63#define MEDIA_FMT_APTX_ADAPTIVE 0x00013204
Zhou Song12c29502019-03-16 10:37:18 +080064#define MEDIA_FMT_APTX_AD_SPEECH 0x00013208
Manisha Agarwald45632b2019-10-17 18:14:28 +080065#define MEDIA_FMT_LC3 0x0001337E
Naresh Tanniru9d027a62015-03-13 01:32:10 +053066#define MEDIA_FMT_AAC_AOT_LC 2
67#define MEDIA_FMT_AAC_AOT_SBR 5
68#define MEDIA_FMT_AAC_AOT_PS 29
Naresh Tanniru9d027a62015-03-13 01:32:10 +053069#define PCM_CHANNEL_L 1
70#define PCM_CHANNEL_R 2
71#define PCM_CHANNEL_C 3
72#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
73#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
74#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
75#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
76#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
77#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
Surendar Karka2febd452018-12-13 17:56:43 +053078#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
Zhou Song8fccbb62019-03-20 01:08:19 +080079#define MIXER_ENC_APTX_AD_CONFIG_BLOCK "SLIM_7_RX APTX_AD Enc Cfg"
Surendar Karka2febd452018-12-13 17:56:43 +053080#define MIXER_SOURCE_DEC_CONFIG_BLOCK "SLIM_7_TX Decoder Config"
81#define MIXER_SINK_DEC_CONFIG_BLOCK "SLIM_9_TX Decoder Config"
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +053082#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
Florian Pfister1a84f312018-07-19 14:38:18 +020083#define MIXER_DEC_BIT_FORMAT "AFE Output Bit Format"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053084#define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080085#define MIXER_SAMPLE_RATE_RX "BT SampleRate RX"
Surendar Karka2febd452018-12-13 17:56:43 +053086#define MIXER_SOURCE_SAMPLE_RATE_TX "BT SampleRate TX"
Samyak Jainf69e9ef2018-06-12 12:26:37 +053087#define MIXER_SAMPLE_RATE_DEFAULT "BT SampleRate"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053088#define MIXER_AFE_IN_CHANNELS "AFE Input Channels"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080089#define MIXER_ABR_TX_FEEDBACK_PATH "A2DP_SLIM7_UL_HL Switch"
Zhou Song12c29502019-03-16 10:37:18 +080090#define MIXER_ABR_RX_FEEDBACK_PATH "SCO_SLIM7_DL_HL Switch"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080091#define MIXER_SET_FEEDBACK_CHANNEL "BT set feedback channel"
Surendar Karka2febd452018-12-13 17:56:43 +053092#define MIXER_SINK_SAMPLE_RATE "BT_TX SampleRate"
Florian Pfister1a84f312018-07-19 14:38:18 +020093#define MIXER_AFE_SINK_CHANNELS "AFE Output Channels"
Manisha Agarwala51768b2018-11-01 16:30:52 +053094#define MIXER_FMT_TWS_CHANNEL_MODE "TWS Channel Mode"
Manisha Agarwald45632b2019-10-17 18:14:28 +080095#define MIXER_FMT_LC3_CHANNEL_MODE "LC3 Channel Mode"
yidongh0515e042017-07-06 15:00:34 +080096#define ENCODER_LATENCY_SBC 10
97#define ENCODER_LATENCY_APTX 40
98#define ENCODER_LATENCY_APTX_HD 20
99#define ENCODER_LATENCY_AAC 70
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530100//To Do: Fine Tune Encoder CELT/LDAC latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530101#define ENCODER_LATENCY_CELT 40
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530102#define ENCODER_LATENCY_LDAC 40
Aalique Grahame22e49102018-12-18 14:23:57 -0800103#define ENCODER_LATENCY_PCM 50
yidongh0515e042017-07-06 15:00:34 +0800104#define DEFAULT_SINK_LATENCY_SBC 140
105#define DEFAULT_SINK_LATENCY_APTX 160
106#define DEFAULT_SINK_LATENCY_APTX_HD 180
107#define DEFAULT_SINK_LATENCY_AAC 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530108//To Do: Fine Tune Default CELT/LDAC Latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530109#define DEFAULT_SINK_LATENCY_CELT 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530110#define DEFAULT_SINK_LATENCY_LDAC 180
Aalique Grahame22e49102018-12-18 14:23:57 -0800111#define DEFAULT_SINK_LATENCY_PCM 140
112
Manisha Agarwald45632b2019-10-17 18:14:28 +0800113// decoder structure is considered.
114#define BAP_UNICAST 1
115// decoder structure is ignored.
116#define BAP_BROADCAST 2
117// decoder structure is treated as second toAirConfig.
118#define BAP_BA_SIMULCAST 3
119
Aalique Grahame22e49102018-12-18 14:23:57 -0800120#define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported"
121#define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled"
122#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
123
124// Default encoder bit width
125#define DEFAULT_ENCODER_BIT_FORMAT 16
126
Manisha Agarwald45632b2019-10-17 18:14:28 +0800127// PCM_24 encoder bit width
128#define ENCODER_BIT_FORMAT_PCM_24 24
129
Aalique Grahame22e49102018-12-18 14:23:57 -0800130// Default encoder latency
131#define DEFAULT_ENCODER_LATENCY 200
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530132
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800133// Slimbus Tx sample rate for ABR feedback channel
134#define ABR_TX_SAMPLE_RATE "KHZ_8"
135
Zhou Song12c29502019-03-16 10:37:18 +0800136// Slimbus Tx sample rate for APTX AD SPEECH
137#define SPEECH_TX_SAMPLE_RATE "KHZ_96"
138
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800139// Purpose ID for Inter Module Communication (IMC) in AFE
140#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
141
142// Maximum quality levels for ABR
143#define MAX_ABR_QUALITY_LEVELS 5
144
145// Instance identifier for A2DP
146#define MAX_INSTANCE_ID (UINT32_MAX / 2)
147
Zhou Song12c29502019-03-16 10:37:18 +0800148// Instance identifier for SWB
149#define APTX_AD_SPEECH_INSTANCE_ID 37
150
151#define SAMPLING_RATE_96K 96000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530152#define SAMPLING_RATE_48K 48000
153#define SAMPLING_RATE_441K 44100
Zhou Song12c29502019-03-16 10:37:18 +0800154#define SAMPLING_RATE_32K 32000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530155#define CH_STEREO 2
156#define CH_MONO 1
Florian Pfister1a84f312018-07-19 14:38:18 +0200157#define SOURCE 0
158#define SINK 1
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800159#define UNINITIALIZED -1
160
161#ifdef __LP64__
162#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
163#else
164#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
165#endif
166
Manisha Agarwald45632b2019-10-17 18:14:28 +0800167#define AUDIO_LOCATION_MAX 28
168
169uint32_t audio_location_map_array[] = { AUDIO_LOCATION_FRONT_LEFT, AUDIO_LOCATION_FRONT_RIGHT, AUDIO_LOCATION_FRONT_CENTER, AUDIO_LOCATION_LOW_FREQUENCY,
170 AUDIO_LOCATION_BACK_LEFT, AUDIO_LOCATION_BACK_RIGHT, AUDIO_LOCATION_FRONT_LEFT_OF_CENTER,
171 AUDIO_LOCATION_FRONT_RIGHT_OF_CENTER, AUDIO_LOCATION_BACK_CENTER, AUDIO_LOCATION_LOW_FREQUENCY_2,
172 AUDIO_LOCATION_SIDE_LEFT, AUDIO_LOCATION_SIDE_RIGHT, AUDIO_LOCATION_TOP_FRONT_LEFT, AUDIO_LOCATION_TOP_FRONT_RIGHT,
173 AUDIO_LOCATION_TOP_FRONT_CENTER, AUDIO_LOCATION_TOP_CENTER, AUDIO_LOCATION_TOP_BACK_LEFT, AUDIO_LOCATION_TOP_BACK_RIGHT,
174 AUDIO_LOCATION_TOP_SIDE_LEFT, AUDIO_LOCATION_TOP_SIDE_RIGHT, AUDIO_LOCATION_TOP_BACK_CENTER,
175 AUDIO_LOCATION_BOTTOM_FRONT_CENTER, AUDIO_LOCATION_BOTTOM_FRONT_LEFT, AUDIO_LOCATION_BOTTOM_FRONT_RIGHT,
176 AUDIO_LOCATION_FRONT_LEFT_WIDE, AUDIO_LOCATION_FRONT_RIGHT_WIDE, AUDIO_LOCATION_LEFT_SURROUND,
177 AUDIO_LOCATION_RIGHT_SURROUND };
178
179int channel_map_array[] = { PCM_CHANNEL_L, PCM_CHANNEL_R, PCM_CHANNEL_C, PCM_CHANNEL_LFE, PCM_CHANNEL_LB, PCM_CHANNEL_RB, PCM_CHANNEL_FLC,
180 PCM_CHANNEL_FRC, PCM_CHANNEL_CB, PCM_CHANNEL_RS, PCM_CHANNEL_SL, PCM_CHANNEL_SR, PCM_CHANNEL_TFL,
181 PCM_CHANNEL_TFR, PCM_CHANNEL_TFC, PCM_CHANNEL_TC, PCM_CHANNEL_TBL, PCM_CHANNEL_TBR, PCM_CHANNEL_TSL,
182 PCM_CHANNEL_TSR, PCM_CHANNEL_TBC, PCM_CHANNEL_BFC, PCM_CHANNEL_BFL, PCM_CHANNEL_BFR, PCM_CHANNEL_LW,
183 PCM_CHANNEL_RW, PCM_CHANNEL_LS, PCM_CHANNEL_RS };
184
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800185static void *vndk_fwk_lib_handle = NULL;
186static int is_running_with_enhanced_fwk = UNINITIALIZED;
187
188typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
189static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
Florian Pfister1a84f312018-07-19 14:38:18 +0200190
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530191/*
192 * Below enum values are extended from audio_base.h to
Florian Pfister1a84f312018-07-19 14:38:18 +0200193 * to keep encoder and decoder type local to bthost_ipc
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530194 * and audio_hal as these are intended only for handshake
195 * between IPC lib and Audio HAL.
196 */
197typedef enum {
Florian Pfister1a84f312018-07-19 14:38:18 +0200198 CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
199 CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
200 CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
201 CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
202 CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530203#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +0200204 CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530205#endif
Florian Pfister1a84f312018-07-19 14:38:18 +0200206 CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
207 CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
Surendar Karka2febd452018-12-13 17:56:43 +0530208 CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
Zhou Song12c29502019-03-16 10:37:18 +0800209 CODEC_TYPE_APTX_AD_SPEECH = 637534208u, //0x26000000UL
Manisha Agarwald45632b2019-10-17 18:14:28 +0800210 CODEC_TYPE_LC3 = 721420288u, //0x2B000000UL
Aalique Grahame22e49102018-12-18 14:23:57 -0800211 CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u
Florian Pfister1a84f312018-07-19 14:38:18 +0200212}codec_t;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530213
Sharad Sangle95d451b2018-06-19 12:24:20 +0530214/*
215 * enums which describes the APTX Adaptive
216 * channel mode, these values are used by encoder
217 */
218 typedef enum {
219 APTX_AD_CHANNEL_UNCHANGED = -1,
220 APTX_AD_CHANNEL_JOINT_STEREO = 0, // default
221 APTX_AD_CHANNEL_MONO = 1,
222 APTX_AD_CHANNEL_DUAL_MONO = 2,
223 APTX_AD_CHANNEL_STEREO_TWS = 4,
224 APTX_AD_CHANNEL_EARBUD = 8,
225} enc_aptx_ad_channel_mode;
226
227/*
228 * enums which describes the APTX Adaptive
229 * sampling frequency, these values are used
230 * by encoder
231 */
232typedef enum {
233 APTX_AD_SR_UNCHANGED = 0x0,
234 APTX_AD_48 = 0x1, // 48 KHz default
235 APTX_AD_44_1 = 0x2, // 44.1kHz
Zhou Song30e8cea2019-10-22 23:39:25 +0800236 APTX_AD_96 = 0x3, // 96KHz
Sharad Sangle95d451b2018-06-19 12:24:20 +0530237} enc_aptx_ad_s_rate;
238
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530239typedef void (*bt_audio_pre_init_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200240typedef int (*audio_source_open_t)(void);
241typedef int (*audio_source_close_t)(void);
242typedef int (*audio_source_start_t)(void);
243typedef int (*audio_source_stop_t)(void);
244typedef int (*audio_source_suspend_t)(void);
245typedef void (*audio_source_handoff_triggered_t)(void);
246typedef void (*clear_source_a2dpsuspend_flag_t)(void);
247typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
248 uint8_t *num_dev, codec_t *codec_type);
249typedef int (*audio_source_check_a2dp_ready_t)(void);
250typedef int (*audio_is_source_scrambling_enabled_t)(void);
Manisha Agarwala51768b2018-11-01 16:30:52 +0530251typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200252typedef int (*audio_sink_start_t)(void);
253typedef int (*audio_sink_stop_t)(void);
254typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
255typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
256typedef int (*audio_sink_check_a2dp_ready_t)(void);
257typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530258
259enum A2DP_STATE {
260 A2DP_STATE_CONNECTED,
261 A2DP_STATE_STARTED,
262 A2DP_STATE_STOPPED,
263 A2DP_STATE_DISCONNECTED,
264};
265
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800266typedef enum {
267 IMC_TRANSMIT,
268 IMC_RECEIVE,
269} imc_direction_t;
270
271typedef enum {
272 IMC_DISABLE,
273 IMC_ENABLE,
274} imc_status_t;
275
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700276typedef enum {
Zhou Song12c29502019-03-16 10:37:18 +0800277 SWAP_DISABLE,
278 SWAP_ENABLE,
279} swap_status_t;
280
281typedef enum {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700282 MTU_SIZE,
283 PEAK_BIT_RATE,
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530284 BIT_RATE_MODE,
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700285} frame_control_type_t;
286
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800287// --- external function dependency ---
288fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
Weiyin Jiang280ea742020-09-08 20:28:22 +0800289fp_check_a2dp_restore_t fp_check_a2dp_restore_l;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800290
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800291/* PCM config for ABR Feedback hostless front end */
292static struct pcm_config pcm_config_abr = {
293 .channels = 1,
294 .rate = 8000,
295 .period_size = 240,
296 .period_count = 2,
297 .format = PCM_FORMAT_S16_LE,
298 .start_threshold = 0,
299 .stop_threshold = INT_MAX,
300 .avail_min = 0,
301};
302
303/* Adaptive bitrate config for A2DP codecs */
304struct a2dp_abr_config {
305 /* Flag to denote whether Adaptive bitrate is enabled for codec */
306 bool is_abr_enabled;
307 /* Flag to denote whether front end has been opened for ABR */
308 bool abr_started;
309 /* ABR Tx path pcm handle */
310 struct pcm *abr_tx_handle;
Zhou Song12c29502019-03-16 10:37:18 +0800311 /* ABR Rx path pcm handle */
312 struct pcm *abr_rx_handle;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800313 /* ABR Inter Module Communication (IMC) instance ID */
314 uint32_t imc_instance;
315};
316
317static uint32_t instance_id = MAX_INSTANCE_ID;
318
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530319/* structure used to update a2dp state machine
320 * to communicate IPC library
321 * to store DSP encoder configuration information
322 */
323struct a2dp_data {
324 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200325 void *bt_lib_source_handle;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530326 bt_audio_pre_init_t bt_audio_pre_init;
Florian Pfister1a84f312018-07-19 14:38:18 +0200327 audio_source_open_t audio_source_open;
328 audio_source_close_t audio_source_close;
329 audio_source_start_t audio_source_start;
330 audio_source_stop_t audio_source_stop;
331 audio_source_suspend_t audio_source_suspend;
332 audio_source_handoff_triggered_t audio_source_handoff_triggered;
333 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
334 audio_get_enc_config_t audio_get_enc_config;
335 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530336 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200337 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
338 enum A2DP_STATE bt_state_source;
339 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530340 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530341 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200342 bool a2dp_source_started;
343 bool a2dp_source_suspended;
344 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530345 bool is_a2dp_offload_supported;
346 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700347 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530348 /* Mono Mode support for TWS+ */
349 bool is_tws_mono_mode_on;
Manisha Agarwald45632b2019-10-17 18:14:28 +0800350 /* Mono Mode support for LC3 */
351 bool is_lc3_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530352 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800353 /* Adaptive bitrate config for A2DP codecs */
354 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200355
356 void *bt_lib_sink_handle;
357 audio_sink_start_t audio_sink_start;
358 audio_sink_stop_t audio_sink_stop;
359 audio_get_dec_config_t audio_get_dec_config;
360 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
361 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
362 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
363 enum A2DP_STATE bt_state_sink;
364 codec_t bt_decoder_format;
365 uint32_t dec_sampling_rate;
366 uint32_t dec_channels;
367 bool a2dp_sink_started;
368 int a2dp_sink_total_active_session_requests;
Zhou Song12c29502019-03-16 10:37:18 +0800369 bool swb_configured;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530370};
371
372struct a2dp_data a2dp;
373
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800374/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
375 * Structures sent to configure DSP for ABR are defined below.
376 * This data helps DSP configure feedback path (BTSoC to LPASS)
377 * for link quality levels and mapping quality levels to codec
378 * specific bitrate.
379 */
380
381/* Key value pair for link quality level to bitrate mapping. */
382struct bit_rate_level_map_t {
383 uint32_t link_quality_level;
384 uint32_t bitrate;
385};
386
387/* Link quality level to bitrate mapping info sent to DSP. */
388struct quality_level_to_bitrate_info {
389 /* Number of quality levels being mapped.
390 * This will be equal to the size of mapping table.
391 */
392 uint32_t num_levels;
393 /* Quality level to bitrate mapping table */
394 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
395};
396
397/* Structure to set up Inter Module Communication (IMC) between
398 * AFE Decoder and Encoder.
399 */
400struct imc_dec_enc_info {
401 /* Decoder to encoder communication direction.
402 * Transmit = 0 / Receive = 1
403 */
404 uint32_t direction;
405 /* Enable / disable IMC between decoder and encoder */
406 uint32_t enable;
407 /* Purpose of IMC being set up between decoder and encoder.
408 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
409 * is the default value to be sent as purpose.
410 */
411 uint32_t purpose;
412 /* Unique communication instance ID.
413 * purpose and comm_instance together form the actual key
414 * used in IMC registration, which must be the same for
415 * encoder and decoder for which IMC is being set up.
416 */
417 uint32_t comm_instance;
418};
419
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700420/* Structure to control frame size of AAC encoded frames. */
421struct aac_frame_size_control_t {
422 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
423 uint32_t ctl_type;
424 /* Control value
425 * MTU_SIZE: MTU size in bytes
426 * PEAK_BIT_RATE: Peak bitrate in bits per second.
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530427 * BIT_RATE_MODE: Variable bitrate
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700428 */
429 uint32_t ctl_value;
430};
431
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800432/* Structure used for ABR config of AFE encoder and decoder. */
433struct abr_enc_cfg_t {
434 /* Link quality level to bitrate mapping info sent to DSP. */
435 struct quality_level_to_bitrate_info mapping_info;
436 /* Information to set up IMC between decoder and encoder */
437 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700438 /* Flag to indicate whether ABR is enabled */
439 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800440} __attribute__ ((packed));
441
442/* Structure to send configuration for decoder introduced
443 * on AFE Tx path for ABR link quality feedback to BT encoder.
444 */
445struct abr_dec_cfg_t {
446 /* Decoder media format */
447 uint32_t dec_format;
448 /* Information to set up IMC between decoder and encoder */
449 struct imc_dec_enc_info imc_info;
450} __attribute__ ((packed));
451
Zhou Song12c29502019-03-16 10:37:18 +0800452struct aptx_ad_speech_mode_cfg_t
453{
454 uint32_t mode;
455 uint32_t swapping;
456} __attribute__ ((packed));
457
458/* Structure for SWB voice dec config */
459struct aptx_ad_speech_dec_cfg_t {
460 struct abr_dec_cfg_t abr_cfg;
461 struct aptx_ad_speech_mode_cfg_t speech_mode;
462} __attribute__ ((packed));
463
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530464/* START of DSP configurable structures
465 * These values should match with DSP interface defintion
466 */
467
468/* AAC encoder configuration structure. */
469typedef struct aac_enc_cfg_t aac_enc_cfg_t;
470
471/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
472 * supported aac_fmt_flag are ADTS/RAW
473 * supported channel_cfg are Native mode, Mono , Stereo
474 */
475struct aac_enc_cfg_t {
476 uint32_t enc_format;
477 uint32_t bit_rate;
478 uint32_t enc_mode;
479 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530480 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530481 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530482} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530483
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700484struct aac_enc_cfg_v2_t {
485 struct aac_enc_cfg_t aac_enc_cfg;
486 struct aac_frame_size_control_t frame_ctl;
487} __attribute__ ((packed));
488
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530489struct aac_enc_cfg_v3_t {
490 struct aac_enc_cfg_t aac_enc_cfg;
491 struct aac_frame_size_control_t frame_ctl;
492 struct aac_frame_size_control_t aac_key_value_ctl;
493} __attribute__ ((packed));
494
Surendar Karkabbb3c822018-11-12 13:00:38 +0530495typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
496struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200497 uint16_t aac_fmt_flag; /* LATM*/
498 uint16_t audio_object_type; /* LC */
499 uint16_t channels; /* Stereo */
500 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
501 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
502 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530503} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200504
Surendar Karkabbb3c822018-11-12 13:00:38 +0530505typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
506struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200507 uint16_t channels; /* Mono, Stereo */
508 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
509 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530510} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200511
512/* AAC decoder configuration structure. */
513typedef struct aac_dec_cfg_t aac_dec_cfg_t;
514struct aac_dec_cfg_t {
515 uint32_t dec_format;
516 audio_aac_decoder_config_t data;
517} __attribute__ ((packed));
518
519/* SBC decoder configuration structure. */
520typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
521struct sbc_dec_cfg_t {
522 uint32_t dec_format;
523 audio_sbc_decoder_config_t data;
524} __attribute__ ((packed));
525
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530526/* SBC encoder configuration structure. */
527typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
528
529/* supported num_subbands are 4/8
530 * supported blk_len are 4, 8, 12, 16
531 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
532 * supported alloc_method are LOUNDNESS/SNR
533 * supported bit_rate for mono channel is max 320kbps
534 * supported bit rate for stereo channel is max 512 kbps
535 */
536struct sbc_enc_cfg_t{
537 uint32_t enc_format;
538 uint32_t num_subbands;
539 uint32_t blk_len;
540 uint32_t channel_mode;
541 uint32_t alloc_method;
542 uint32_t bit_rate;
543 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530544} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530545
546
547/* supported num_channels are Mono/Stereo
548 * supported channel_mapping for mono is CHANNEL_C
549 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
550 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700551 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530552struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530553{
554 uint32_t enc_format;
555 uint32_t sample_rate;
556 uint16_t num_channels;
557 uint16_t reserved;
558 uint8_t channel_mapping[8];
559 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530560} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530561
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530562struct celt_specific_enc_cfg_t
563{
564 uint32_t bit_rate;
565 uint16_t frame_size;
566 uint16_t complexity;
567 uint16_t prediction_mode;
568 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530569} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530570
571struct celt_enc_cfg_t
572{
573 struct custom_enc_cfg_t custom_cfg;
574 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530575} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700576
577/* sync_mode introduced with APTX V2 libraries
578 * sync mode: 0x0 = stereo sync mode
579 * 0x01 = dual mono sync mode
580 * 0x02 = dual mono with no sync on either L or R codewords
581 */
582struct aptx_v2_enc_cfg_ext_t
583{
584 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530585} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700586
587/* APTX struct for combining custom enc and V2 fields */
588struct aptx_enc_cfg_t
589{
590 struct custom_enc_cfg_t custom_cfg;
591 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530592} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700593
Sharad Sangle95d451b2018-06-19 12:24:20 +0530594/* APTX AD structure */
595struct aptx_ad_enc_cfg_ext_t
596{
597 uint32_t sampling_freq;
598 uint32_t mtu;
599 uint32_t channel_mode;
600 uint32_t min_sink_modeA;
601 uint32_t max_sink_modeA;
602 uint32_t min_sink_modeB;
603 uint32_t max_sink_modeB;
604 uint32_t min_sink_modeC;
605 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530606 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530607} __attribute__ ((packed));
608
609struct aptx_ad_enc_cfg_t
610{
611 struct custom_enc_cfg_t custom_cfg;
612 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
613 struct abr_enc_cfg_t abr_cfg;
614} __attribute__ ((packed));
615
Zhou Song8fccbb62019-03-20 01:08:19 +0800616struct aptx_ad_enc_cfg_ext_r2_t
617{
618 uint32_t sampling_freq;
619 uint32_t mtu;
620 uint32_t channel_mode;
621 uint32_t min_sink_modeA;
622 uint32_t max_sink_modeA;
623 uint32_t min_sink_modeB;
624 uint32_t max_sink_modeB;
625 uint32_t min_sink_modeC;
626 uint32_t max_sink_modeC;
627 uint32_t mode;
628 uint32_t input_mode;
629 uint32_t fade_duration;
630 uint8_t sink_cap[11];
631} __attribute__ ((packed));
632
633struct aptx_ad_enc_cfg_r2_t
634{
635 struct custom_enc_cfg_t custom_cfg;
636 struct aptx_ad_enc_cfg_ext_r2_t aptx_ad_cfg;
637 struct abr_enc_cfg_t abr_cfg;
638} __attribute__ ((packed));
639
Zhou Song12c29502019-03-16 10:37:18 +0800640/* APTX AD SPEECH structure */
641struct aptx_ad_speech_enc_cfg_t
642{
643 struct custom_enc_cfg_t custom_cfg;
644 /* Information to set up IMC between decoder and encoder */
645 struct imc_dec_enc_info imc_info;
646 struct aptx_ad_speech_mode_cfg_t speech_mode;
647} __attribute__ ((packed));
648
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530649struct ldac_specific_enc_cfg_t
650{
651 uint32_t bit_rate;
652 uint16_t channel_mode;
653 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530654} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530655
656struct ldac_enc_cfg_t
657{
658 struct custom_enc_cfg_t custom_cfg;
659 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800660 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530661} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530662
Manisha Agarwald45632b2019-10-17 18:14:28 +0800663/* LC3 structure */
664struct lc3_stream_info_t
665{
666 uint32_t stream_id;
667 uint32_t direction;
668 uint32_t channel_mask_lsw;
669 uint32_t channel_mask_msw;
670} __attribute__ ((packed));
671
672struct lc3_stream_map_info_t
673{
674 uint32_t stream_map_size;
675 struct lc3_stream_info_t streamMap[16];
676} __attribute__ ((packed));
677
678struct lc3_stream_map_ext_t
679{
680 uint32_t audio_location;
681 uint8_t stream_id;
682 uint8_t direction;
683} __attribute__ ((packed));
684
685struct lc3_config_ext_t
686{
687 uint32_t api_version;
688 uint32_t sampling_freq;
689 uint32_t max_octets_per_frame;
690 uint32_t frame_duration;//7.5msec, 10msec
691 uint32_t bit_depth;
692 uint32_t num_blocks;
693 uint8_t default_q_level;
694 uint8_t vendor_specific[16]; // this indicates LC3/LC3Q. 0 => LC3 1.0 , 1 => LC3 1.1
695 uint32_t mode;
696} __attribute__ ((packed));
697
698struct lc3_dec_cfg_ext_t
699{
700 struct lc3_config_ext_t fromAirConfig;
701 uint32_t decoder_output_channel;
702 uint32_t stream_map_size;
703 struct lc3_stream_map_ext_t streamMapIn[16];
704} __attribute__ ((packed));
705
706struct lc3_enc_cfg_ext_t
707{
708 struct lc3_config_ext_t toAirConfig;
709 uint32_t stream_map_size;
710 struct lc3_stream_map_ext_t streamMapOut[16];
711} __attribute__ ((packed));
712
713struct lc3_dec_codec_cfg_t
714{
715 struct lc3_stream_map_info_t streamMapToAir;
716} __attribute__ ((packed));
717
718struct lc3_dec_cfg_t
719{
720 struct abr_dec_cfg_t abr_cfg;
721 struct lc3_dec_codec_cfg_t dec_codec;
722} __attribute__ ((packed));
723
724struct lc3_enc_codec_cfg_t
725{
726 struct lc3_enc_cfg_ext_t to_Air_cfg;
727 struct lc3_stream_map_info_t streamMapToAir;
728} __attribute__ ((packed));
729
730struct lc3_enc_cfg_t
731{
732 uint32_t enc_format;
733 struct imc_dec_enc_info imc_info;
734 struct lc3_enc_codec_cfg_t enc_codec;
735} __attribute__ ((packed));
736
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530737/* In LE BT source code uses system/audio.h for below
738 * structure definition. To avoid multiple definition
739 * compilation error for audiohal in LE , masking structure
740 * definition under "LINUX_ENABLED" which is defined only
741 * in LE
742 */
743#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530744/* TODO: Define the following structures only for O using PLATFORM_VERSION */
745/* Information about BT SBC encoder configuration
746 * This data is used between audio HAL module and
747 * BT IPC library to configure DSP encoder
748 */
749typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530750 uint32_t subband; /* 4, 8 */
751 uint32_t blk_len; /* 4, 8, 12, 16 */
752 uint16_t sampling_rate; /*44.1khz,48khz*/
753 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
754 uint8_t alloc; /*0(Loudness),1(SNR)*/
755 uint8_t min_bitpool; /* 2 */
756 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
757 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530758 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530759} audio_sbc_encoder_config;
760
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530761/* Information about BT APTX encoder configuration
762 * This data is used between audio HAL module and
763 * BT IPC library to configure DSP encoder
764 */
765typedef struct {
766 uint16_t sampling_rate;
767 uint8_t channels;
768 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530769 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700770} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530771
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700772typedef struct {
Zhou Song8fccbb62019-03-20 01:08:19 +0800773 uint32_t sampling_rate;
774 uint32_t mtu;
775 int32_t channel_mode;
776 uint32_t min_sink_modeA;
777 uint32_t max_sink_modeA;
778 uint32_t min_sink_modeB;
779 uint32_t max_sink_modeB;
780 uint32_t min_sink_modeC;
781 uint32_t max_sink_modeC;
782 uint32_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530783 uint8_t TTP_modeA_low;
784 uint8_t TTP_modeA_high;
785 uint8_t TTP_modeB_low;
786 uint8_t TTP_modeB_high;
Zhou Song8fccbb62019-03-20 01:08:19 +0800787 uint8_t TTP_TWS_low;
788 uint8_t TTP_TWS_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530789 uint32_t bits_per_sample;
Zhou Song8fccbb62019-03-20 01:08:19 +0800790 uint32_t input_mode;
791 uint32_t fade_duration;
792 uint8_t sink_cap[11];
Sharad Sangle95d451b2018-06-19 12:24:20 +0530793} audio_aptx_ad_config;
794
795typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700796 uint16_t sampling_rate;
797 uint8_t channels;
798 uint32_t bitrate;
799 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530800 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700801} audio_aptx_dual_mono_config;
802
803typedef union {
804 audio_aptx_default_config *default_cfg;
805 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530806 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700807} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530808
809/* Information about BT AAC encoder configuration
810 * This data is used between audio HAL module and
811 * BT IPC library to configure DSP encoder
812 */
813typedef struct {
814 uint32_t enc_mode; /* LC, SBR, PS */
815 uint16_t format_flag; /* RAW, ADTS */
816 uint16_t channels; /* 1-Mono, 2-Stereo */
817 uint32_t sampling_rate;
818 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530819 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530820} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530821#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700822
823typedef struct {
824 audio_aac_encoder_config audio_aac_enc_cfg;
825 struct aac_frame_size_control_t frame_ctl;
826} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530827
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530828typedef struct {
829 audio_aac_encoder_config audio_aac_enc_cfg;
830 struct aac_frame_size_control_t frame_ctl;
831 uint8_t size_control_struct;
832 struct aac_frame_size_control_t* frame_ptr_ctl;
833} audio_aac_encoder_config_v3;
834
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530835/* Information about BT CELT encoder configuration
836 * This data is used between audio HAL module and
837 * BT IPC library to configure DSP encoder
838 */
839typedef struct {
840 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
841 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
842 uint16_t frame_size; /* 64-128-256-512, 512 */
843 uint16_t complexity; /* 0-10, 1 */
844 uint16_t prediction_mode; /* 0-1-2, 0 */
845 uint16_t vbr_flag; /* 0-1, 0*/
846 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530847 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530848} audio_celt_encoder_config;
849
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530850/* Information about BT LDAC encoder configuration
851 * This data is used between audio HAL module and
852 * BT IPC library to configure DSP encoder
853 */
854typedef struct {
855 uint32_t sampling_rate; /*44100,48000,88200,96000*/
856 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
857 uint16_t channel_mode; /* 0, 4, 2, 1*/
858 uint16_t mtu; /*679*/
Aniket Kumar Latad0196282019-05-09 14:24:17 -0700859 uint32_t bits_per_sample;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800860 bool is_abr_enabled;
861 struct quality_level_to_bitrate_info level_to_bitrate_map;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530862} audio_ldac_encoder_config;
863
Surendar Karkabbb3c822018-11-12 13:00:38 +0530864/* Information about BT AAC decoder configuration
865 * This data is used between audio HAL module and
866 * BT IPC library to configure DSP decoder
867 */
868typedef struct {
869 uint16_t aac_fmt_flag; /* LATM*/
870 uint16_t audio_object_type; /* LC */
871 uint16_t channels; /* Stereo */
872 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
873 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
874 44.1k, 48k, 64k, 88.2k, 96k */
875} audio_aac_dec_config_t;
876
877/* Information about BT SBC decoder configuration
878 * This data is used between audio HAL module and
879 * BT IPC library to configure DSP decoder
880 */
881typedef struct {
882 uint16_t channels; /* Mono, Stereo */
883 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
884 44.1k, 48k, 64k, 88.2k, 96k */
885}audio_sbc_dec_config_t;
886
Manisha Agarwald45632b2019-10-17 18:14:28 +0800887/* Information about BT LC3 encoder configuration
888 * This data is used between audio HAL module and
889 * BT IPC library to configure DSP encoder
890 */
891typedef struct {
892 uint32_t api_version;
893 uint32_t sampling_freq;
894 uint32_t max_octets_per_frame;
895 uint32_t frame_duration;//7.5msec, 10msec
896 uint32_t bit_depth;
897 uint32_t num_blocks;
898 uint8_t default_q_level;
899 uint8_t vendor_specific[16]; // this indicates LC3/LC3Q. 0 => LC3 1.0 , 1 => LC3 1.1
900 uint32_t mode;
901} lc3_config_t;
902
903typedef struct {
904 uint32_t audio_location;
905 uint8_t stream_id;
906 uint8_t direction;
907} lc3_stream_map_t;
908
909typedef struct {
910 lc3_config_t toAirConfig;
911 uint8_t stream_map_size;
912 lc3_stream_map_t* streamMapOut;
913} lc3_encoder_config_t;
914
915typedef struct {
916 lc3_config_t fromAirConfig;
917 uint32_t decoder_output_channel;
918 uint8_t stream_map_size;
919 lc3_stream_map_t* streamMapIn;
920} lc3_decoder_config_t;
921
922typedef struct {
923 lc3_encoder_config_t enc_config;
924 lc3_decoder_config_t dec_config;
925} audio_lc3_codec_config_t;
926
927
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530928/*********** END of DSP configurable structures ********************/
929
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530930static void update_offload_codec_capabilities()
931{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530932
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530933 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800934 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
935 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
936
937 ALOGD("%s: A2DP offload supported = %d",__func__,
938 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530939}
940
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800941static int stop_abr()
942{
943 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800944 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800945 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800946 int ret = 0;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800947
948 /* This function can be used if !abr_started for clean up */
949 ALOGV("%s: enter", __func__);
950
951 // Close hostless front end
952 if (a2dp.abr_config.abr_tx_handle != NULL) {
953 pcm_close(a2dp.abr_config.abr_tx_handle);
954 a2dp.abr_config.abr_tx_handle = NULL;
955 }
Zhou Song12c29502019-03-16 10:37:18 +0800956 if (a2dp.abr_config.abr_rx_handle != NULL) {
957 pcm_close(a2dp.abr_config.abr_rx_handle);
958 a2dp.abr_config.abr_rx_handle = NULL;
959 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800960 a2dp.abr_config.abr_started = false;
961 a2dp.abr_config.imc_instance = 0;
962
963 // Reset BT driver mixer control for ABR usecase
964 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
965 MIXER_SET_FEEDBACK_CHANNEL);
966 if (!ctl_set_bt_feedback_channel) {
967 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800968 ret = -ENOSYS;
969 } else if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800970 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800971 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800972 }
973
974 // Reset ABR Tx feedback path
975 ALOGV("%s: Disable ABR Tx feedback path", __func__);
976 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
977 MIXER_ABR_TX_FEEDBACK_PATH);
978 if (!ctl_abr_tx_path) {
979 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800980 ret = -ENOSYS;
981 } else if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800982 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800983 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800984 }
985
Zhou Song12c29502019-03-16 10:37:18 +0800986 // Reset ABR Rx feedback path
987 ALOGV("%s: Disable ABR Rx feedback path", __func__);
988 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
989 MIXER_ABR_RX_FEEDBACK_PATH);
990 if (!ctl_abr_rx_path) {
991 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
992 ret = -ENOSYS;
993 } else if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 0) != 0) {
994 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
995 ret = -ENOSYS;
996 }
997
998 return ret;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800999}
1000
1001static int start_abr()
1002{
1003 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001004 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001005 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
1006 int abr_device_id;
1007 int ret = 0;
1008
1009 if (!a2dp.abr_config.is_abr_enabled) {
1010 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
1011 return -ENOSYS;
1012 }
1013
1014 if (a2dp.abr_config.abr_started) {
1015 ALOGI("%s: ABR has already started", __func__);
1016 return ret;
1017 }
1018
1019 // Enable Slimbus 7 Tx feedback path
1020 ALOGV("%s: Enable ABR Tx feedback path", __func__);
1021 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
1022 MIXER_ABR_TX_FEEDBACK_PATH);
1023 if (!ctl_abr_tx_path) {
1024 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
1025 return -ENOSYS;
1026 }
1027 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
1028 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
1029 return -ENOSYS;
1030 }
1031
1032 // Notify ABR usecase information to BT driver to distinguish
1033 // between SCO and feedback usecase
1034 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
1035 MIXER_SET_FEEDBACK_CHANNEL);
1036 if (!ctl_set_bt_feedback_channel) {
1037 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +08001038 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001039 }
1040 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
1041 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +08001042 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001043 }
1044
1045 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08001046 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001047 PCM_CAPTURE);
1048 if (!a2dp.abr_config.abr_tx_handle) {
1049 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
1050 abr_device_id, PCM_IN,
1051 &pcm_config_abr);
Zhou Song12c29502019-03-16 10:37:18 +08001052 if (a2dp.abr_config.abr_tx_handle == NULL) {
1053 ALOGE("%s: Can't open abr tx device", __func__);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001054 goto fail;
Zhou Song12c29502019-03-16 10:37:18 +08001055 }
1056 if (!(pcm_is_ready(a2dp.abr_config.abr_tx_handle) &&
1057 !pcm_start(a2dp.abr_config.abr_tx_handle))) {
1058 ALOGE("%s: tx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
1059 goto fail;
1060 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001061 }
Zhou Song12c29502019-03-16 10:37:18 +08001062
1063 // Enable Slimbus 7 Rx feedback path for HD Voice use case
1064 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH) {
1065 ALOGV("%s: Enable ABR Rx feedback path", __func__);
1066 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
1067 MIXER_ABR_RX_FEEDBACK_PATH);
1068 if (!ctl_abr_rx_path) {
1069 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
1070 goto fail;
1071 }
1072 if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 1) != 0) {
1073 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
1074 goto fail;
1075 }
1076
1077 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
1078 ALOGE("%s: Failed to set BT usecase", __func__);
1079 goto fail;
1080 }
1081
1082 // Open hostless front end and prepare ABR Rx path
1083 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
1084 PCM_PLAYBACK);
1085 if (!a2dp.abr_config.abr_rx_handle) {
1086 a2dp.abr_config.abr_rx_handle = pcm_open(a2dp.adev->snd_card,
1087 abr_device_id, PCM_OUT,
1088 &pcm_config_abr);
1089 if (a2dp.abr_config.abr_rx_handle == NULL) {
1090 ALOGE("%s: Can't open abr rx device", __func__);
1091 goto fail;
1092 }
1093 if (!(pcm_is_ready(a2dp.abr_config.abr_rx_handle) &&
1094 !pcm_start(a2dp.abr_config.abr_rx_handle))) {
1095 ALOGE("%s: rx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_rx_handle));
1096 goto fail;
1097 }
1098 }
1099 }
1100
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001101 a2dp.abr_config.abr_started = true;
1102
1103 return ret;
1104
1105fail:
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001106 stop_abr();
1107 return -ENOSYS;
1108}
1109
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001110static int check_if_enhanced_fwk() {
1111
1112 int is_enhanced_fwk = 1;
1113 //dlopen lib
1114 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
1115 if (vndk_fwk_lib_handle != NULL) {
1116 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
1117 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
1118 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
1119 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
1120 __func__);
1121 is_enhanced_fwk = 1;
1122 } else {
1123 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
1124 }
1125 }
1126 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
1127 return is_enhanced_fwk;
1128}
1129
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301130static void open_a2dp_source() {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301131 int ret = 0;
1132
Florian Pfister1a84f312018-07-19 14:38:18 +02001133 ALOGD(" Open A2DP source start ");
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301134
1135 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
1136 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
1137 ALOGD("calling BT stream open");
1138 ret = a2dp.audio_source_open();
1139 if(ret != 0) {
1140 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Srinu Jellada99a592019-01-25 16:50:52 +05301141 }
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301142 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301143 } else {
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301144 ALOGD("Called a2dp open with improper state %d", a2dp.bt_state_source);
1145 }
1146 } else {
1147 ALOGE("a2dp handle is not identified, Ignoring open request");
1148 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
1149 }
1150}
1151/* API to open BT IPC library to start IPC communication for BT Source*/
1152static void a2dp_source_init()
1153{
1154 ALOGD("a2dp_source_init START");
1155 if (a2dp.bt_lib_source_handle == NULL) {
1156 ALOGD("Requesting for BT lib handle");
1157 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
1158 if (a2dp.bt_lib_source_handle == NULL) {
1159 ALOGE("%s: dlopen failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
1160 return;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301161 }
1162 }
1163
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301164 a2dp.bt_audio_pre_init = (bt_audio_pre_init_t)
1165 dlsym(a2dp.bt_lib_source_handle, "bt_audio_pre_init");
Srinu Jellada99a592019-01-25 16:50:52 +05301166 a2dp.audio_source_open = (audio_source_open_t)
1167 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
1168 a2dp.audio_source_start = (audio_source_start_t)
1169 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
1170 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
1171 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
1172 a2dp.audio_source_suspend = (audio_source_suspend_t)
1173 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
1174 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
1175 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
1176 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1177 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
1178 a2dp.audio_source_stop = (audio_source_stop_t)
1179 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
1180 a2dp.audio_source_close = (audio_source_close_t)
1181 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
1182 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
1183 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
1184 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
1185 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
1186 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1187 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1188 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1189 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1190
Srinu Jella793f5302019-07-19 15:53:55 +05301191 if (a2dp.bt_lib_source_handle && a2dp.bt_audio_pre_init) {
1192 ALOGD("calling BT module preinit");
1193 // fwk related check's will be done in the BT layer
1194 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001195 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001196}
1197
1198/* API to open BT IPC library to start IPC communication for BT Sink*/
1199static void open_a2dp_sink()
1200{
1201 ALOGD(" Open A2DP input start ");
1202 if (a2dp.bt_lib_sink_handle == NULL){
1203 ALOGD(" Requesting for BT lib handle");
1204 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1205
1206 if (a2dp.bt_lib_sink_handle == NULL) {
1207 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1208 } else {
1209 a2dp.audio_sink_start = (audio_sink_start_t)
1210 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1211 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1212 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1213 a2dp.audio_sink_stop = (audio_sink_stop_t)
1214 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1215 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1216 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1217 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1218 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1219 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301220 }
1221}
1222
1223static int close_a2dp_output()
1224{
1225 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001226
1227 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1228 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301229 return -ENOSYS;
1230 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001231
1232 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1233 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001234 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001235 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301236 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001237 a2dp.a2dp_source_started = false;
1238 a2dp.a2dp_source_total_active_session_requests = 0;
1239 a2dp.a2dp_source_suspended = false;
1240 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301241 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301242 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001243 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001244 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1245 stop_abr();
1246 a2dp.abr_config.is_abr_enabled = false;
1247 a2dp.abr_config.abr_started = false;
1248 a2dp.abr_config.imc_instance = 0;
1249 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001250 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301251 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001252
1253 return 0;
1254}
1255
1256static int close_a2dp_input()
1257{
1258 ALOGV("%s\n",__func__);
1259
1260 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1261 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1262 return -ENOSYS;
1263 }
1264
1265 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1266 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001267 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001268 ALOGE("failed close a2dp sink control path from BT library");
1269 }
1270 a2dp.a2dp_sink_started = false;
1271 a2dp.a2dp_sink_total_active_session_requests = 0;
1272 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1273 a2dp.dec_sampling_rate = 48000;
1274 a2dp.dec_channels = 2;
1275 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301276
1277 return 0;
1278}
1279
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301280static void a2dp_check_and_set_scrambler()
1281{
1282 bool scrambler_mode = false;
1283 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001284 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1285 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301286
1287 if (scrambler_mode) {
1288 //enable scrambler in dsp
1289 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1290 MIXER_SCRAMBLER_MODE);
1291 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001292 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301293 return;
1294 } else {
1295 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1296 ALOGE("%s: Could not set scrambler mode", __func__);
1297 return;
1298 }
1299 }
1300 }
1301}
1302
Florian Pfister1a84f312018-07-19 14:38:18 +02001303static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301304{
Florian Pfister1a84f312018-07-19 14:38:18 +02001305 char *rate_str = NULL, *channels = NULL;
1306 uint32_t sampling_rate;
1307 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1308 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301309
Florian Pfister1a84f312018-07-19 14:38:18 +02001310 if (direction == SINK) {
1311 sampling_rate = a2dp.dec_sampling_rate;
1312 } else {
1313 sampling_rate = a2dp.enc_sampling_rate;
1314 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001315 /*
1316 * For LDAC encoder and AAC decoder open slimbus port at
1317 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1318 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1319 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001320 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301321 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001322 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1323 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301324 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1325 sampling_rate = sampling_rate *2;
1326 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001327
Manisha Agarwald45632b2019-10-17 18:14:28 +08001328 if (a2dp.bt_encoder_format == CODEC_TYPE_LC3)
1329 sampling_rate = SAMPLING_RATE_96K;
1330
Aalique Grahame22e49102018-12-18 14:23:57 -08001331 // No need to configure backend for PCM format.
1332 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1333 return 0;
1334 }
1335
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301336 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301337 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301338 case 44100:
1339 rate_str = "KHZ_44P1";
1340 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301341 case 88200:
1342 rate_str = "KHZ_88P2";
1343 break;
1344 case 96000:
1345 rate_str = "KHZ_96";
1346 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001347 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301348 default:
1349 rate_str = "KHZ_48";
1350 break;
1351 }
1352
Florian Pfister1a84f312018-07-19 14:38:18 +02001353 if (direction == SINK) {
1354 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1355 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301356 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001357 } else {
1358 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1359 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301360 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001361 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301362 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001363
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301364 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1365 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301366 is_configured = false;
1367 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301368 }
1369
Surendar Karka2febd452018-12-13 17:56:43 +05301370 if (direction == SOURCE) {
1371 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001372 if (a2dp.abr_config.is_abr_enabled) {
Manisha Agarwald45632b2019-10-17 18:14:28 +08001373 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH ||
1374 a2dp.bt_encoder_format == CODEC_TYPE_LC3)
Zhou Song12c29502019-03-16 10:37:18 +08001375 rate_str = SPEECH_TX_SAMPLE_RATE;
1376 else
1377 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301378
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301379 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1380 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1381 MIXER_SOURCE_SAMPLE_RATE_TX);
1382 if (!ctl_sample_rate) {
1383 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1384 is_configured = false;
1385 goto fail;
1386 }
1387
1388 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1389 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1390 is_configured = false;
1391 goto fail;
1392 }
Surendar Karka2febd452018-12-13 17:56:43 +05301393 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301394 }
1395 } else {
1396 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1397 MIXER_SAMPLE_RATE_TX is not supported */
1398 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1399 MIXER_SAMPLE_RATE_DEFAULT);
1400 if (!ctl_sample_rate) {
1401 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301402 is_configured = false;
1403 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301404 }
1405
1406 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1407 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001408 is_configured = false;
1409 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301410 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301411 }
1412
Florian Pfister1a84f312018-07-19 14:38:18 +02001413 if (direction == SINK) {
1414 switch (a2dp.dec_channels) {
1415 case 1:
1416 channels = "One";
1417 break;
1418 case 2:
1419 default:
1420 channels = "Two";
1421 break;
1422 }
1423
Ramu Gottipati02809682018-12-19 16:46:12 +05301424 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001425 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1426 MIXER_AFE_SINK_CHANNELS);
1427 } else {
1428 //Configure AFE enc channels
1429 switch (a2dp.enc_channels) {
1430 case 1:
1431 channels = "One";
1432 break;
1433 case 2:
1434 default:
1435 channels = "Two";
1436 break;
1437 }
1438
Ramu Gottipati02809682018-12-19 16:46:12 +05301439 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001440 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1441 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301442 }
1443
Florian Pfister1a84f312018-07-19 14:38:18 +02001444 if (!ctrl_channels) {
1445 ALOGE(" ERROR AFE channels mixer control not identified");
1446 } else {
1447 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301448 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001449 is_configured = false;
1450 goto fail;
1451 }
1452 }
1453 is_configured = true;
1454fail:
1455 return is_configured;
1456}
1457
Aniket Kumar Lata990de552019-07-11 14:20:23 -07001458bool a2dp_set_source_backend_cfg()
1459{
1460 if (a2dp.a2dp_source_started && !a2dp.a2dp_source_suspended)
1461 return a2dp_set_backend_cfg(SOURCE);
1462
1463 return false;
1464}
1465
Surendar Karkabbb3c822018-11-12 13:00:38 +05301466bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001467{
1468 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1469 struct aac_dec_cfg_t aac_dsp_cfg;
1470 bool is_configured = false;
1471 int ret = 0;
1472
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001473 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001474 return false;
1475
Surendar Karka2febd452018-12-13 17:56:43 +05301476 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001477 if (!ctl_dec_data) {
1478 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1479 is_configured = false;
1480 goto fail;
1481 }
1482
1483 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1484 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1485 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1486 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1487 switch(aac_bt_cfg->audio_object_type) {
1488 case 0:
1489 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301490 break;
1491 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001492 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1493 break;
1494 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301495 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001496 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301497 break;
1498 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001499 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1500 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1501 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1502 sizeof(struct aac_dec_cfg_t));
1503 if (ret != 0) {
1504 ALOGE("%s: failed to set AAC decoder config", __func__);
1505 is_configured = false;
1506 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001507 }
1508
Florian Pfister1a84f312018-07-19 14:38:18 +02001509 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1510 MIXER_DEC_BIT_FORMAT);
1511 if (!ctrl_bit_format) {
1512 ALOGE(" ERROR Dec bit format mixer control not identified");
1513 is_configured = false;
1514 goto fail;
1515 }
1516 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1517 if (ret != 0) {
1518 ALOGE("%s: Failed to set bit format to decoder", __func__);
1519 is_configured = false;
1520 goto fail;
1521 }
1522
1523 is_configured = true;
1524 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1525 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1526 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1527 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1528 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1529fail:
1530 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301531}
1532
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301533static int a2dp_set_bit_format(uint32_t enc_bit_format)
1534{
1535 const char *bit_format = NULL;
1536 struct mixer_ctl *ctrl_bit_format = NULL;
1537
1538 // Configure AFE Input Bit Format
1539 switch (enc_bit_format) {
1540 case 32:
1541 bit_format = "S32_LE";
1542 break;
1543 case 24:
1544 bit_format = "S24_LE";
1545 break;
1546 case 16:
1547 default:
1548 bit_format = "S16_LE";
1549 break;
1550 }
1551
1552 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1553 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1554 MIXER_ENC_BIT_FORMAT);
1555 if (!ctrl_bit_format) {
1556 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1557 return -ENOSYS;
1558 }
1559 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1560 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1561 return -ENOSYS;
1562 }
1563 return 0;
1564}
1565
Surendar Karka2febd452018-12-13 17:56:43 +05301566static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301567{
Surendar Karka2febd452018-12-13 17:56:43 +05301568 const char *rate_str = "KHZ_8", *channels = "Zero";
1569 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1570 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301571
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001572 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301573 if (direction == SINK) {
1574 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1575 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1576 MIXER_SINK_SAMPLE_RATE);
1577 } else {
1578 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1579 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1580 MIXER_SAMPLE_RATE_RX);
1581 }
1582 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301583
Surendar Karka2febd452018-12-13 17:56:43 +05301584 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1585 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301586 return -ENOSYS;
1587 }
Zhou Song12c29502019-03-16 10:37:18 +08001588 if (a2dp.abr_config.is_abr_enabled) {
1589 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1590 MIXER_SOURCE_SAMPLE_RATE_TX);
1591 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301592 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1593 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001594 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301595
Zhou Song12c29502019-03-16 10:37:18 +08001596 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1597 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1598 return -ENOSYS;
1599 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301600 }
1601 } else {
1602
Surendar Karka2febd452018-12-13 17:56:43 +05301603 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301604 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301605 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301606 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1607 return -ENOSYS;
1608 }
1609
Surendar Karka2febd452018-12-13 17:56:43 +05301610 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301611 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1612 return -ENOSYS;
1613 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001614 }
1615
1616 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301617 if (direction == SINK) {
1618 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1619 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1620 MIXER_AFE_SINK_CHANNELS);
1621 } else {
1622 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1623 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1624 MIXER_AFE_IN_CHANNELS);
1625 }
1626 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001627 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1628 return -ENOSYS;
1629 }
Surendar Karka2febd452018-12-13 17:56:43 +05301630 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001631 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1632 return -ENOSYS;
1633 }
1634
1635 return 0;
1636}
1637
1638/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301639static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001640{
1641 struct mixer_ctl *ctl_dec_data = NULL;
1642 struct abr_dec_cfg_t dec_cfg;
1643 int ret = 0;
1644
1645 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301646 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001647 if (!ctl_dec_data) {
1648 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1649 return false;
1650 }
1651 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1652 dec_cfg.dec_format = dec_format;
1653 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1654 dec_cfg.imc_info.enable = IMC_ENABLE;
1655 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1656 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1657
1658 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1659 sizeof(dec_cfg));
1660 if (ret != 0) {
1661 ALOGE("%s: Failed to set decoder config", __func__);
1662 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301663 }
Surendar Karka2febd452018-12-13 17:56:43 +05301664 }
1665
1666 return true;
1667}
1668
Surendar Karkabbb3c822018-11-12 13:00:38 +05301669bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001670{
1671 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1672 struct sbc_dec_cfg_t sbc_dsp_cfg;
1673 bool is_configured = false;
1674 int ret = 0;
1675
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001676 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001677 goto fail;
1678
Surendar Karka2febd452018-12-13 17:56:43 +05301679 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001680 if (!ctl_dec_data) {
1681 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1682 is_configured = false;
1683 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301684 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001685
Florian Pfister1a84f312018-07-19 14:38:18 +02001686 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1687 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1688 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1689 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1690 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1691 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301692
Florian Pfister1a84f312018-07-19 14:38:18 +02001693 if (ret != 0) {
1694 ALOGE("%s: failed to set SBC decoder config", __func__);
1695 is_configured = false;
1696 goto fail;
1697 }
1698
1699 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1700 MIXER_DEC_BIT_FORMAT);
1701 if (!ctrl_bit_format) {
1702 ALOGE(" ERROR Dec bit format mixer control not identified");
1703 is_configured = false;
1704 goto fail;
1705 }
1706 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1707 if (ret != 0) {
1708 ALOGE("%s: Failed to set bit format to decoder", __func__);
1709 is_configured = false;
1710 goto fail;
1711 }
1712
1713 is_configured = true;
1714 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1715 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1716 a2dp.dec_channels = 1;
1717 else
1718 a2dp.dec_channels = 2;
1719 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1720 ALOGV("Successfully updated SBC dec format");
1721fail:
1722 return is_configured;
1723}
1724
Florian Pfister1a84f312018-07-19 14:38:18 +02001725/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301726static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301727{
Florian Pfister1a84f312018-07-19 14:38:18 +02001728 void *codec_info = NULL;
1729 codec_t codec_type = CODEC_TYPE_INVALID;
1730 bool is_configured = false;
1731 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301732
Florian Pfister1a84f312018-07-19 14:38:18 +02001733 if (!a2dp.audio_get_dec_config) {
1734 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1735 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301736 }
1737
Surendar Karka2febd452018-12-13 17:56:43 +05301738 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001739 if (!ctl_dec_data) {
1740 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1741 is_configured = false;
1742 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301743 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001744 codec_info = a2dp.audio_get_dec_config(&codec_type);
1745 switch(codec_type) {
1746 case CODEC_TYPE_SBC:
1747 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301748 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001749 break;
1750 case CODEC_TYPE_AAC:
1751 ALOGD(" AAC decoder supported BT device");
1752 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301753 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001754 break;
1755 default:
1756 ALOGD(" Received Unsupported decoder format");
1757 is_configured = false;
1758 break;
1759 }
1760 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301761}
1762
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301763/* API to configure SBC DSP encoder */
1764bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1765{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301766 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301767 struct sbc_enc_cfg_t sbc_dsp_cfg;
1768 bool is_configured = false;
1769 int ret = 0;
1770
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001771 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301772 return false;
1773
1774 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1775 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001776 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301777 is_configured = false;
1778 goto fail;
1779 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301780 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001781 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301782 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1783 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1784 switch(sbc_bt_cfg->channels) {
1785 case 0:
1786 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1787 break;
1788 case 1:
1789 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1790 break;
1791 case 3:
1792 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1793 break;
1794 case 2:
1795 default:
1796 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1797 break;
1798 }
1799 if (sbc_bt_cfg->alloc)
1800 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1801 else
1802 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1803 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1804 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1805 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1806 sizeof(struct sbc_enc_cfg_t));
1807 if (ret != 0) {
1808 ALOGE("%s: failed to set SBC encoder config", __func__);
1809 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301810 goto fail;
1811 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301812 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301813 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301814 is_configured = false;
1815 goto fail;
1816 }
1817 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001818 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301819 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301820
1821 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1822 a2dp.enc_channels = 1;
1823 else
1824 a2dp.enc_channels = 2;
1825
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301826 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1827 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301828fail:
1829 return is_configured;
1830}
1831
Manish Dewangan6a252632017-12-04 17:27:44 +05301832#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301833static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1834 audio_aptx_encoder_config *aptx_bt_cfg)
1835{
1836 int ret = 0;
1837
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001838 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301839 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1840 aptx_dsp_cfg, aptx_bt_cfg);
1841 return -EINVAL;
1842 }
1843
1844 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301845 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301846
1847
1848 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1849 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1850 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1851 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1852 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1853 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1854 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1855 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1856 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301857 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301858 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1859 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1860 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1861 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1862
1863
1864 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1865 case APTX_AD_CHANNEL_UNCHANGED:
1866 case APTX_AD_CHANNEL_JOINT_STEREO:
1867 case APTX_AD_CHANNEL_DUAL_MONO:
1868 case APTX_AD_CHANNEL_STEREO_TWS:
1869 case APTX_AD_CHANNEL_EARBUD:
1870 default:
1871 a2dp.enc_channels = CH_STEREO;
1872 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1873 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1874 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1875 break;
1876 case APTX_AD_CHANNEL_MONO:
1877 a2dp.enc_channels = CH_MONO;
1878 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1879 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001880 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301881 }
1882 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1883 case APTX_AD_SR_UNCHANGED:
1884 case APTX_AD_48:
1885 default:
1886 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1887 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1888 break;
1889 case APTX_AD_44_1:
1890 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1891 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001892 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301893 }
1894 ALOGV("Successfully updated APTX AD enc format with \
1895 samplingrate: %d channels:%d",
1896 aptx_dsp_cfg->custom_cfg.sample_rate,
1897 aptx_dsp_cfg->custom_cfg.num_channels);
1898
1899 return ret;
1900}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301901
Zhou Song8fccbb62019-03-20 01:08:19 +08001902static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
1903 audio_aptx_encoder_config *aptx_bt_cfg)
1904{
1905 int ret = 0;
1906
1907 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Mingshu Pangaa429f72019-06-10 17:25:00 +08001908 ALOGE("Invalid param, aptx_dsp_cfg %pK aptx_bt_cfg %pK",
Zhou Song8fccbb62019-03-20 01:08:19 +08001909 aptx_dsp_cfg, aptx_bt_cfg);
1910 return -EINVAL;
1911 }
1912
1913 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
1914 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
1915
1916
1917 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1918 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1919 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1920 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1921 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1922 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1923 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1924 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1925 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
1926 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
1927 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
1928 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
1929 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
1930 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
1931 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1932 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1933 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1934 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1935
1936
1937 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1938 case APTX_AD_CHANNEL_UNCHANGED:
1939 case APTX_AD_CHANNEL_JOINT_STEREO:
1940 case APTX_AD_CHANNEL_DUAL_MONO:
1941 case APTX_AD_CHANNEL_STEREO_TWS:
1942 case APTX_AD_CHANNEL_EARBUD:
1943 default:
1944 a2dp.enc_channels = CH_STEREO;
1945 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1946 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1947 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1948 break;
1949 case APTX_AD_CHANNEL_MONO:
1950 a2dp.enc_channels = CH_MONO;
1951 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1952 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001953 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001954 }
1955 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1956 case APTX_AD_SR_UNCHANGED:
1957 case APTX_AD_48:
1958 default:
1959 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1960 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1961 break;
1962 case APTX_AD_44_1:
1963 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1964 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001965 break;
1966 case APTX_AD_96:
1967 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
1968 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_96K;
1969 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001970 }
1971 ALOGV("Successfully updated APTX AD enc format with \
1972 samplingrate: %d channels:%d",
1973 aptx_dsp_cfg->custom_cfg.sample_rate,
1974 aptx_dsp_cfg->custom_cfg.num_channels);
1975
1976 return ret;
1977}
1978
Manisha Agarwala51768b2018-11-01 16:30:52 +05301979static void audio_a2dp_update_tws_channel_mode()
1980{
1981 char* channel_mode;
1982 struct mixer_ctl *ctl_channel_mode;
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301983
1984 ALOGD("Update tws for mono_mode on=%d",a2dp.is_tws_mono_mode_on);
1985
Manisha Agarwala51768b2018-11-01 16:30:52 +05301986 if (a2dp.is_tws_mono_mode_on)
1987 channel_mode = "One";
1988 else
1989 channel_mode = "Two";
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301990
Manisha Agarwala51768b2018-11-01 16:30:52 +05301991 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1992 if (!ctl_channel_mode) {
1993 ALOGE("failed to get tws mixer ctl");
1994 return;
1995 }
1996 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1997 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1998 return;
1999 }
2000}
2001
Manisha Agarwald45632b2019-10-17 18:14:28 +08002002static void audio_a2dp_update_lc3_channel_mode()
2003{
2004 char* channel_mode;
2005 struct mixer_ctl *ctl_channel_mode;
2006
2007 ALOGD("Update lc3 for mono_mode on=%d",a2dp.is_lc3_mono_mode_on);
2008
2009 if (a2dp.is_lc3_mono_mode_on)
2010 channel_mode = "One";
2011 else
2012 channel_mode = "Two";
2013
2014 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_LC3_CHANNEL_MODE);
2015 if (!ctl_channel_mode) {
2016 ALOGE("failed to get lc3 mixer ctl");
2017 return;
2018 }
2019 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2020 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2021 return;
2022 }
2023}
2024
Manish Dewangan6a252632017-12-04 17:27:44 +05302025static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
2026 audio_aptx_encoder_config *aptx_bt_cfg)
2027{
2028 int ret = 0;
2029
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002030 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05302031 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
2032 aptx_dsp_cfg, aptx_bt_cfg);
2033 return -EINVAL;
2034 }
2035
2036 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002037 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05302038
2039 if (!a2dp.is_aptx_dual_mono_supported) {
2040 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
2041 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
2042 } else {
2043 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
2044 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
2045 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
2046 }
2047
2048 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
2049 case 1:
2050 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2051 break;
2052 case 2:
2053 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05302054 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2055 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
Manish Dewangan6a252632017-12-04 17:27:44 +05302056 break;
2057 }
2058 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
2059 if (!a2dp.is_aptx_dual_mono_supported) {
2060 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
2061 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
2062 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
2063 aptx_dsp_cfg->custom_cfg.num_channels);
2064 } else {
2065 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
2066 ALOGV("Successfully updated APTX dual mono enc format with \
2067 samplingrate: %d channels:%d syncmode %d",
2068 aptx_dsp_cfg->custom_cfg.sample_rate,
2069 aptx_dsp_cfg->custom_cfg.num_channels,
2070 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
2071 }
2072 return ret;
2073}
2074#else
2075static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
2076 audio_aptx_encoder_config *aptx_bt_cfg)
2077{
2078 int ret = 0;
2079
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002080 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05302081 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
2082 aptx_dsp_cfg, aptx_bt_cfg);
2083 return -EINVAL;
2084 }
2085
2086 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002087 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05302088 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
2089 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
2090 switch(aptx_dsp_cfg->num_channels) {
2091 case 1:
2092 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
2093 break;
2094 case 2:
2095 default:
2096 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
2097 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
2098 break;
2099 }
2100
2101 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
2102 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
2103
2104 return ret;
2105}
2106#endif
2107
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302108/* API to configure APTX DSP encoder */
2109bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2110{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302111 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08002112 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08002113 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302114 bool is_configured = false;
2115 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08002116 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05302117
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002118 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05302119 return false;
2120
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002121 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2122 if (!ctl_enc_data) {
2123 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2124 return false;
2125 }
2126
Manish Dewangan6a252632017-12-04 17:27:44 +05302127#ifndef LINUX_ENABLED
2128 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302129 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08002130 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002131 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08002132 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
2133 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002134 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08002135 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002136 else
Zhou Song8fccbb62019-03-20 01:08:19 +08002137 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002138 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002139 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08002140
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002141 if (ret) {
2142 is_configured = false;
2143 goto fail;
2144 }
2145
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002146 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08002147 if (aptx_ad_ctl)
2148 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002149 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08002150 else
2151 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002152 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002153 } else {
2154 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08002155 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302156 }
Manish Dewangan6a252632017-12-04 17:27:44 +05302157#else
2158 struct custom_enc_cfg_t aptx_dsp_cfg;
2159 mixer_size = sizeof(struct custom_enc_cfg_t);
2160 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05302161 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05302162 if (ret) {
2163 is_configured = false;
2164 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302165 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08002166 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
2167 mixer_size);
2168#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302169 if (ret != 0) {
2170 ALOGE("%s: Failed to set APTX encoder config", __func__);
2171 is_configured = false;
2172 goto fail;
2173 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302174#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002175 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302176 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002177 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302178 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
2179 else
2180 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302181#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302182 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302183 is_configured = false;
2184 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302185 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302186 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302187 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05302188 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302189 else
Surendar Karka2febd452018-12-13 17:56:43 +05302190 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302191fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05302192 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002193 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05302194 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302195 return is_configured;
2196}
2197
2198/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302199 */
Manish Dewangan6a252632017-12-04 17:27:44 +05302200#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002201bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302202#else
2203bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2204#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302205{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302206 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302207 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302208 bool is_configured = false;
2209 int ret = 0;
2210
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002211 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302212 return false;
2213
2214 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2215 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002216 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302217 is_configured = false;
2218 goto fail;
2219 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302220
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302221 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002222 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302223 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2224 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2225 switch(aptx_dsp_cfg.num_channels) {
2226 case 1:
2227 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2228 break;
2229 case 2:
2230 default:
2231 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2232 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2233 break;
2234 }
2235 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302236 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302237 if (ret != 0) {
2238 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2239 is_configured = false;
2240 goto fail;
2241 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302242 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302243 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302244 is_configured = false;
2245 goto fail;
2246 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302247 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002248 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302249 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302250 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302251 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2252 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302253fail:
2254 return is_configured;
2255}
2256
2257/* API to configure AAC DSP encoder */
2258bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2259{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302260 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302261 struct aac_enc_cfg_t aac_dsp_cfg;
2262 bool is_configured = false;
2263 int ret = 0;
2264
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002265 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302266 return false;
2267
2268 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2269 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002270 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302271 is_configured = false;
2272 goto fail;
2273 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302274 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002275 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302276 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302277 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002278 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302279 case 0:
2280 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2281 break;
2282 case 2:
2283 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2284 break;
2285 case 1:
2286 default:
2287 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2288 break;
2289 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302290 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302291 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002292
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302293 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2294 sizeof(struct aac_enc_cfg_t));
2295 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002296 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302297 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302298 goto fail;
2299 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302300 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302301 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302302 is_configured = false;
2303 goto fail;
2304 }
2305 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002306 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302307 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002308 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002309 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2310 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2311fail:
2312 return is_configured;
2313}
2314
2315bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2316{
2317 struct mixer_ctl *ctl_enc_data = NULL;
2318 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2319 bool is_configured = false;
2320 int ret = 0;
2321
2322 if (aac_bt_cfg == NULL)
2323 return false;
2324
2325 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2326 if (!ctl_enc_data) {
2327 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2328 is_configured = false;
2329 goto fail;
2330 }
2331 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302332 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002333 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2334 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2335 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2336 case 0:
2337 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2338 break;
2339 case 2:
2340 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2341 break;
2342 case 1:
2343 default:
2344 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2345 break;
2346 }
2347 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2348 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2349 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2350 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2351
2352 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2353 sizeof(struct aac_enc_cfg_v2_t));
2354 if (ret != 0) {
2355 ALOGE("%s: Failed to set AAC encoder config", __func__);
2356 is_configured = false;
2357 goto fail;
2358 }
2359 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2360 if (ret != 0) {
2361 is_configured = false;
2362 goto fail;
2363 }
2364 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302365 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002366 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2367 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2368 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2369 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302370fail:
2371 return is_configured;
2372}
2373
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302374bool configure_aac_enc_format_v3(audio_aac_encoder_config_v3 *aac_bt_cfg)
2375{
2376 struct mixer_ctl *ctl_enc_data = NULL;
2377 struct aac_enc_cfg_v3_t aac_dsp_cfg;
2378 struct aac_frame_size_control_t* frame_vbr_ctl = NULL;
2379 bool is_configured = false;
2380 int ret = 0;
2381
2382 if (aac_bt_cfg == NULL)
2383 return false;
2384
2385 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2386 if (!ctl_enc_data) {
2387 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2388 is_configured = false;
2389 goto fail;
2390 }
2391 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v3_t));
2392 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
2393 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2394 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2395 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2396 case 0:
2397 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2398 break;
2399 case 2:
2400 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2401 break;
2402 case 1:
2403 default:
2404 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2405 break;
2406 }
2407 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2408 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2409 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2410 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2411 frame_vbr_ctl = aac_bt_cfg->frame_ptr_ctl;
2412
2413 if (frame_vbr_ctl != NULL) {
2414 aac_dsp_cfg.aac_key_value_ctl.ctl_type = frame_vbr_ctl->ctl_type;
2415 aac_dsp_cfg.aac_key_value_ctl.ctl_value = frame_vbr_ctl->ctl_value;
2416 } else {
2417 ALOGE("%s: VBR cannot be enabled, fall back to default",__func__);
2418 aac_dsp_cfg.aac_key_value_ctl.ctl_type = 0;
2419 aac_dsp_cfg.aac_key_value_ctl.ctl_value = 0;
2420 }
2421
2422 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2423 sizeof(struct aac_enc_cfg_v3_t));
2424 if (ret != 0) {
2425 ALOGE("%s: Failed to set AAC encoder config", __func__);
2426 is_configured = false;
2427 goto fail;
2428 }
2429 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2430 if (ret != 0) {
2431 is_configured = false;
2432 goto fail;
2433 }
2434 is_configured = true;
2435 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
2436 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2437 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2438 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2439 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
2440fail:
2441 return is_configured;
2442}
2443
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302444bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2445{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302446 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302447 struct celt_enc_cfg_t celt_dsp_cfg;
2448 bool is_configured = false;
2449 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002450 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302451 return false;
2452
2453 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2454 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002455 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302456 is_configured = false;
2457 goto fail;
2458 }
2459 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2460
Florian Pfister1a84f312018-07-19 14:38:18 +02002461 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302462 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2463 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2464 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2465 case 1:
2466 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2467 break;
2468 case 2:
2469 default:
2470 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2471 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2472 break;
2473 }
2474
2475 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2476
2477 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2478 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2479 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2480 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2481 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2482
2483 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2484 sizeof(struct celt_enc_cfg_t));
2485 if (ret != 0) {
2486 ALOGE("%s: Failed to set CELT encoder config", __func__);
2487 is_configured = false;
2488 goto fail;
2489 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302490 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302491 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302492 is_configured = false;
2493 goto fail;
2494 }
2495 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002496 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302497 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302498 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302499 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2500 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2501fail:
2502 return is_configured;
2503}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302504
2505bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2506{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302507 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302508 struct ldac_enc_cfg_t ldac_dsp_cfg;
2509 bool is_configured = false;
2510 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002511 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302512 return false;
2513
2514 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2515 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002516 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302517 is_configured = false;
2518 goto fail;
2519 }
2520 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2521
Florian Pfister1a84f312018-07-19 14:38:18 +02002522 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302523 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2524 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2525 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2526 case 4:
2527 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2528 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2529 break;
2530 case 2:
2531 case 1:
2532 default:
2533 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2534 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2535 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2536 break;
2537 }
2538
2539 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2540 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2541 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002542 if (ldac_bt_cfg->is_abr_enabled) {
2543 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2544 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2545 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2546 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2547 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002548 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002549 }
2550
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302551 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2552 sizeof(struct ldac_enc_cfg_t));
2553 if (ret != 0) {
2554 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2555 is_configured = false;
2556 goto fail;
2557 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302558 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302559 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302560 is_configured = false;
2561 goto fail;
2562 }
2563 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002564 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302565 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2566 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002567 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302568 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2569 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2570fail:
2571 return is_configured;
2572}
2573
Manisha Agarwald45632b2019-10-17 18:14:28 +08002574uint64_t convert_channel_map(uint32_t audio_location)
2575{
2576 int i;
2577 uint64_t channel_mask = (uint64_t) 0x00000000;
2578
2579 if (!audio_location) {
2580 channel_mask |= 1ULL << PCM_CHANNEL_C;
2581 return channel_mask;
2582 }
2583
2584 for (i = 0; i < AUDIO_LOCATION_MAX; i++) {
2585 if (audio_location & audio_location_map_array[i])
2586 channel_mask |= 1ULL << channel_map_array[i];
2587 }
2588
2589 return channel_mask;
2590}
2591
2592bool configure_lc3_enc_format(audio_lc3_codec_config_t *lc3_bt_cfg) {
2593 struct mixer_ctl *ctl_enc_data = NULL;
2594 int mixer_size = 0;
2595 int ret = 0;
2596 int i;
2597 bool is_configured = false;
2598 struct lc3_enc_cfg_t lc3_dsp_cfg;
2599 uint64_t channel_mask;
2600
2601 if (lc3_bt_cfg == NULL)
2602 return false;
2603
2604 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2605 if (!ctl_enc_data) {
2606 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
2607 return false;
2608 }
2609
2610 /* Initialize dsp configuration params */
2611 memset(&lc3_dsp_cfg, 0x0, sizeof(struct lc3_enc_cfg_t));
2612
2613 lc3_dsp_cfg.enc_format = MEDIA_FMT_LC3;
2614 //encoder structure
2615
2616 if (lc3_bt_cfg->enc_config.stream_map_size != 0) {
2617 if (!lc3_bt_cfg->enc_config.streamMapOut[0].audio_location)
2618 a2dp.enc_channels = CH_MONO;
2619 else
2620 a2dp.enc_channels = CH_STEREO;
2621 }
2622
2623 lc3_dsp_cfg.imc_info.direction = IMC_RECEIVE;
2624 lc3_dsp_cfg.imc_info.enable = IMC_ENABLE;
2625 lc3_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2626 lc3_dsp_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
2627 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.api_version = lc3_bt_cfg->enc_config.toAirConfig.api_version;
2628 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.sampling_freq = lc3_bt_cfg->enc_config.toAirConfig.sampling_freq;
2629 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.max_octets_per_frame = lc3_bt_cfg->enc_config.toAirConfig.max_octets_per_frame;
2630 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.frame_duration = lc3_bt_cfg->enc_config.toAirConfig.frame_duration;
2631 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.bit_depth = lc3_bt_cfg->enc_config.toAirConfig.bit_depth;
2632 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.num_blocks = lc3_bt_cfg->enc_config.toAirConfig.num_blocks;
2633 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.default_q_level = lc3_bt_cfg->enc_config.toAirConfig.default_q_level;
2634 for (i = 0; i < 16; i++)
2635 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.vendor_specific[i] = lc3_bt_cfg->enc_config.toAirConfig.vendor_specific[i];
2636 lc3_dsp_cfg.enc_codec.to_Air_cfg.toAirConfig.mode = lc3_bt_cfg->enc_config.toAirConfig.mode;
2637 lc3_dsp_cfg.enc_codec.to_Air_cfg.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
2638 lc3_dsp_cfg.enc_codec.streamMapToAir.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
2639
2640 for (i = 0; i < lc3_dsp_cfg.enc_codec.to_Air_cfg.stream_map_size; i++) {
2641 // for encoder stream map info
2642 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].audio_location = lc3_bt_cfg->enc_config.streamMapOut[i].audio_location;
2643 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
2644 lc3_dsp_cfg.enc_codec.to_Air_cfg.streamMapOut[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
2645 // for packetizer stream map info
2646 channel_mask = convert_channel_map(lc3_bt_cfg->enc_config.streamMapOut[i].audio_location);
2647 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
2648 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
2649 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].channel_mask_lsw = channel_mask & 0x00000000FFFFFFFF;
2650 lc3_dsp_cfg.enc_codec.streamMapToAir.streamMap[i].channel_mask_msw = (channel_mask & 0xFFFFFFFF00000000) >> 32;
2651 }
2652
2653 /* Configure AFE DSP configuration */
2654 mixer_size = sizeof(struct lc3_enc_cfg_t);
2655 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&lc3_dsp_cfg,
2656 mixer_size);
2657 if (ret != 0) {
2658 ALOGE("%s: Failed to set lc3 encoder config", __func__);
2659 is_configured = false;
2660 goto fail;
2661 }
2662
2663 ret = a2dp_set_bit_format(ENCODER_BIT_FORMAT_PCM_24);
2664 if (ret != 0) {
2665 ALOGE("%s: Failed to set lc3 bit format", __func__);
2666 is_configured = false;
2667 goto fail;
2668 }
2669
2670 is_configured = true;
2671 a2dp.bt_encoder_format = CODEC_TYPE_LC3;
2672
2673fail:
2674 return is_configured;
2675}
2676
2677bool configure_lc3_dec_format(audio_lc3_codec_config_t *lc3_bt_cfg)
2678{
2679 struct mixer_ctl *ctl_dec_data = NULL;
2680 struct lc3_dec_cfg_t lc3_dsp_cfg;
2681 uint64_t channel_mask;
2682 bool is_configured = false;
2683 int ret = 0;
2684 int i;
2685
2686 if (lc3_bt_cfg == NULL)
2687 return false;
2688
2689 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
2690 if (!ctl_dec_data) {
2691 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
2692 return false;
2693 }
2694
2695 memset(&lc3_dsp_cfg, 0x0, sizeof(struct lc3_dec_cfg_t));
2696
2697 lc3_dsp_cfg.abr_cfg.dec_format = MEDIA_FMT_LC3;
2698 lc3_dsp_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
2699 lc3_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2700 lc3_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2701
2702 lc3_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
2703
2704 //for depacketizer both fromAir and toAir streamMap needs to be sent.
2705 lc3_dsp_cfg.dec_codec.streamMapToAir.stream_map_size = lc3_bt_cfg->enc_config.stream_map_size;
2706
2707 for (i = 0; i < lc3_bt_cfg->enc_config.stream_map_size; i++) {
2708 // for depacketizer stream map info
2709 channel_mask = convert_channel_map(lc3_bt_cfg->enc_config.streamMapOut[i].audio_location);
2710 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].stream_id = lc3_bt_cfg->enc_config.streamMapOut[i].stream_id;
2711 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].direction = lc3_bt_cfg->enc_config.streamMapOut[i].direction;
2712 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].channel_mask_lsw = channel_mask & 0x00000000FFFFFFFF;
2713 lc3_dsp_cfg.dec_codec.streamMapToAir.streamMap[i].channel_mask_msw = (channel_mask & 0xFFFFFFFF00000000) >> 32;
2714 }
2715
2716 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&lc3_dsp_cfg,
2717 sizeof(struct lc3_dec_cfg_t));
2718 if (ret != 0) {
2719 ALOGE("%s: failed to set LC3 decoder config", __func__);
2720 is_configured = false;
2721 goto fail;
2722 }
2723
2724 is_configured = true;
2725 a2dp.bt_decoder_format = CODEC_TYPE_LC3;
2726
2727fail:
2728 return is_configured;
2729}
2730
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302731bool configure_a2dp_encoder_format()
2732{
2733 void *codec_info = NULL;
2734 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002735 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302736 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002737 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302738
Florian Pfister1a84f312018-07-19 14:38:18 +02002739 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302740 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2741 return false;
2742 }
2743 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002744 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302745 &codec_type);
2746
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002747 // ABR disabled by default for all codecs
2748 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302749 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002750
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302751 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002752 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302753 ALOGD(" Received SBC encoder supported BT device");
2754 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002755 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302756 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002757 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302758 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302759#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002760 a2dp.is_aptx_dual_mono_supported = false;
2761 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302762#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302763 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002764 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302765 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002766 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302767 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302768#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302769 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002770 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302771#else
2772 is_configured =
2773 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2774#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002775 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302776#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002777 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002778 ALOGD(" Received APTX dual mono encoder supported BT device");
2779 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302780 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2781 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002782 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2783 is_configured =
2784 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302785 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302786#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002787 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302788 ALOGD(" Received AAC encoder supported BT device");
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302789 bool is_aac_vbr_enabled =
2790 property_get_bool("persist.vendor.bt.aac_vbr_frm_ctl.enabled", false);
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002791 bool is_aac_frame_ctl_enabled =
2792 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302793 if (is_aac_vbr_enabled)
2794 is_configured = configure_aac_enc_format_v3((audio_aac_encoder_config_v3 *) codec_info);
2795 else
2796 is_configured = is_aac_frame_ctl_enabled ?
2797 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2798 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302799 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002800 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302801 ALOGD(" Received CELT encoder supported BT device");
2802 is_configured =
2803 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2804 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002805 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302806 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002807 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2808 instance_id = MAX_INSTANCE_ID;
2809 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302810 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002811 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302812 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302813 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302814#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302815 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302816 ALOGD(" Received APTX AD encoder supported BT device");
2817 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2818 instance_id = MAX_INSTANCE_ID;
2819 a2dp.abr_config.imc_instance = instance_id--;
2820 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2821 a2dp.is_aptx_adaptive = true;
2822 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2823 is_configured =
2824 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302825 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302826 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302827#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002828 case CODEC_TYPE_PCM:
2829 ALOGD("Received PCM format for BT device");
2830 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2831 is_configured = true;
2832 break;
Manisha Agarwald45632b2019-10-17 18:14:28 +08002833 case CODEC_TYPE_LC3:
2834 ALOGD("Received LC3 encoder supported BT device");
2835 a2dp.bt_encoder_format = CODEC_TYPE_LC3;
2836 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2837 instance_id = MAX_INSTANCE_ID;
2838 a2dp.abr_config.imc_instance = instance_id--;
2839 a2dp.abr_config.is_abr_enabled = true;
2840 is_configured =
2841 (configure_lc3_enc_format((audio_lc3_codec_config_t *)codec_info) &&
2842 configure_lc3_dec_format((audio_lc3_codec_config_t *)codec_info));
2843 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302844 default:
2845 ALOGD(" Received Unsupported encoder formar");
2846 is_configured = false;
2847 break;
2848 }
2849 return is_configured;
2850}
2851
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002852int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302853{
2854 int ret = 0;
2855
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002856 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302857
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002858 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002859 && a2dp.audio_get_enc_config)) {
2860 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302861 return -ENOSYS;
2862 }
2863
Zhou Song12c29502019-03-16 10:37:18 +08002864 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302865 //session will be restarted after suspend completion
2866 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302867 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302868 }
2869
Florian Pfister1a84f312018-07-19 14:38:18 +02002870 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302871 ALOGD("calling BT module stream start");
2872 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002873 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302874 ALOGE("BT controller start return = %d",ret);
2875 if (ret != 0 ) {
2876 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002877 a2dp.a2dp_source_started = false;
Revathi Uddarajub26e3932020-06-10 14:51:02 +05302878 ret = -ETIMEDOUT;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302879 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002880 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002881 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302882 ret = 0;
2883 ALOGD("Start playback successful to BT library");
2884 } else {
2885 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002886 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302887 ret = -ETIMEDOUT;
2888 }
2889 }
2890 }
2891
Florian Pfister1a84f312018-07-19 14:38:18 +02002892 if (a2dp.a2dp_source_started) {
2893 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302894 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302895 audio_a2dp_update_tws_channel_mode();
Manisha Agarwald45632b2019-10-17 18:14:28 +08002896 audio_a2dp_update_lc3_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002897 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002898 if (a2dp.abr_config.is_abr_enabled)
2899 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302900 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302901
2902 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002903 a2dp.a2dp_source_total_active_session_requests);
2904 return ret;
2905}
2906
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002907uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002908{
2909 uint32_t latency = 0;
2910
2911 switch(a2dp.bt_decoder_format) {
2912 case CODEC_TYPE_SBC:
2913 latency = DEFAULT_SINK_LATENCY_SBC;
2914 break;
2915 case CODEC_TYPE_AAC:
2916 latency = DEFAULT_SINK_LATENCY_AAC;
2917 break;
2918 default:
2919 latency = 200;
2920 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2921 break;
2922 }
2923 return (uint64_t)latency;
2924}
2925
2926bool a2dp_send_sink_setup_complete(void) {
2927 uint64_t system_latency = 0;
2928 bool is_complete = false;
2929
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002930 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002931
2932 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2933 is_complete = true;
2934 }
2935 return is_complete;
2936}
2937
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002938bool a2dp_sink_is_ready()
2939{
2940 bool ret = false;
2941
2942 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2943 (a2dp.is_a2dp_offload_supported) &&
2944 (a2dp.audio_sink_check_a2dp_ready))
2945 ret = a2dp.audio_sink_check_a2dp_ready();
2946 return ret;
2947}
2948
2949int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002950{
2951 int ret = 0;
2952
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002953 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002954
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002955 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002956 && a2dp.audio_get_dec_config)) {
2957 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2958 return -ENOSYS;
2959 }
2960
2961 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2962 ALOGD("calling BT module stream start");
2963 /* This call indicates BT IPC lib to start capture */
2964 ret = a2dp.audio_sink_start();
2965 ALOGE("BT controller start capture return = %d",ret);
2966 if (ret != 0 ) {
2967 ALOGE("BT controller start capture failed");
2968 a2dp.a2dp_sink_started = false;
2969 } else {
2970
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002971 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002972 ALOGD("Wait for capture ready not successful");
2973 ret = -ETIMEDOUT;
2974 }
2975
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002976 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002977 a2dp.a2dp_sink_started = true;
2978 ret = 0;
2979 ALOGD("Start capture successful to BT library");
2980 } else {
2981 ALOGD(" unable to configure DSP decoder");
2982 a2dp.a2dp_sink_started = false;
2983 ret = -ETIMEDOUT;
2984 }
2985
2986 if (!a2dp_send_sink_setup_complete()) {
2987 ALOGD("sink_setup_complete not successful");
2988 ret = -ETIMEDOUT;
2989 }
2990 }
2991 }
2992
2993 if (a2dp.a2dp_sink_started) {
2994 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002995 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002996 }
2997 }
2998
2999 ALOGD("start A2DP sink total active sessions :%d",
3000 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303001 return ret;
3002}
3003
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303004static void reset_a2dp_enc_config_params()
3005{
3006 int ret =0;
3007
Aalique Grahame22e49102018-12-18 14:23:57 -08003008 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303009 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05303010 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303011
3012 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
3013 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
3014 MIXER_ENC_CONFIG_BLOCK);
3015 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003016 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303017 } else {
3018 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
3019 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02003020 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303021 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003022
3023 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3024
Manisha Agarwala51768b2018-11-01 16:30:52 +05303025 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
3026
3027 if (!ctl_channel_mode) {
3028 ALOGE("failed to get tws mixer ctl");
3029 } else {
3030 channel_mode = "Two";
3031 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
3032 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
3033 }
3034 a2dp.is_tws_mono_mode_on = false;
3035 }
Manisha Agarwald45632b2019-10-17 18:14:28 +08003036
3037 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_LC3_CHANNEL_MODE);
3038
3039 if (!ctl_channel_mode) {
3040 ALOGE("failed to get lc3 mixer ctl");
3041 } else {
3042 channel_mode = "Two";
3043 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0)
3044 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
3045 a2dp.is_lc3_mono_mode_on = false;
3046 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05303047}
3048
Surendar Karka2febd452018-12-13 17:56:43 +05303049static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07003050{
3051 struct mixer_ctl *ctl_dec_data = NULL;
3052 struct abr_dec_cfg_t dummy_reset_cfg;
3053 int ret = 0;
3054
Preetam Singh Ranawat25780e42019-07-31 18:15:57 +05303055 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3056 if (!ctl_dec_data) {
3057 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
3058 return -EINVAL;
3059 }
3060 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
3061 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
3062 sizeof(dummy_reset_cfg));
3063 if (ret != 0) {
3064 ALOGE("%s: Failed to set dummy decoder config", __func__);
3065 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07003066 }
3067
3068 return ret;
3069}
3070
Surendar Karka2febd452018-12-13 17:56:43 +05303071static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02003072{
3073 int ret =0;
3074
3075 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
3076 struct aac_dec_cfg_t dummy_reset_config;
3077
3078 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
3079 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05303080 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02003081 if (!ctl_dec_config) {
3082 ALOGE(" ERROR a2dp decoder format mixer control not identified");
3083 } else {
3084 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
3085 sizeof(struct aac_dec_cfg_t));
3086 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
3087 }
3088 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
3089 MIXER_DEC_BIT_FORMAT);
3090 if (!ctrl_bit_format) {
3091 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
3092 } else {
3093 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
3094 if (ret != 0) {
3095 ALOGE("%s: Failed to set bit format to decoder", __func__);
3096 }
3097 }
3098}
3099
Zhou Song12c29502019-03-16 10:37:18 +08003100static void reset_codec_config()
3101{
3102 reset_a2dp_enc_config_params();
3103 reset_a2dp_source_dec_config_params();
3104 a2dp_reset_backend_cfg(SOURCE);
3105 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
3106 stop_abr();
3107 a2dp.abr_config.is_abr_enabled = false;
3108}
3109
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003110int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303111{
3112 int ret =0;
3113
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003114 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003115 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003116 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303117 return -ENOSYS;
3118 }
3119
Florian Pfister1a84f312018-07-19 14:38:18 +02003120 if (a2dp.a2dp_source_total_active_session_requests > 0)
3121 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08003122 else
3123 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303124
Florian Pfister1a84f312018-07-19 14:38:18 +02003125 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303126 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02003127 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303128 if (ret < 0)
3129 ALOGE("stop stream to BT IPC lib failed");
3130 else
3131 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08003132 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
3133 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05303134 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303135 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003136 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02003137 a2dp.a2dp_source_started = false;
3138 ALOGD("Stop A2DP playback, total active sessions :%d",
3139 a2dp.a2dp_source_total_active_session_requests);
3140 return 0;
3141}
3142
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003143int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02003144{
3145 int ret =0;
3146
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003147 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003148 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003149 ALOGE("a2dp handle is not identified, Ignoring stop request");
3150 return -ENOSYS;
3151 }
3152
3153 if (a2dp.a2dp_sink_total_active_session_requests > 0)
3154 a2dp.a2dp_sink_total_active_session_requests--;
3155
3156 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
3157 ALOGV("calling BT module stream stop");
3158 ret = a2dp.audio_sink_stop();
3159 if (ret < 0)
3160 ALOGE("stop stream to BT IPC lib failed");
3161 else
3162 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05303163 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02003164 a2dp_reset_backend_cfg(SINK);
3165 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003166 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02003167 a2dp.a2dp_source_started = false;
3168 ALOGD("Stop A2DP capture, total active sessions :%d",
3169 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303170 return 0;
3171}
3172
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003173int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303174{
Weiyin Jiang280ea742020-09-08 20:28:22 +08003175 int ret = 0, val, status = 0;
3176 char value[32] = {0};
3177 struct audio_usecase *uc_info;
3178 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303179
Weiyin Jiang280ea742020-09-08 20:28:22 +08003180 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08003181 ALOGV("no supported encoders identified,ignoring a2dp setparam");
3182 status = -EINVAL;
3183 goto param_handled;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003184 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303185
Weiyin Jiang280ea742020-09-08 20:28:22 +08003186 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303187 sizeof(value));
Weiyin Jiang280ea742020-09-08 20:28:22 +08003188 if (ret >= 0) {
3189 val = atoi(value);
3190 if (audio_is_a2dp_out_device(val)) {
3191 ALOGV("Received device connect request for A2DP source");
3192 open_a2dp_source();
3193 }
3194 goto param_handled;
3195 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303196
Weiyin Jiang280ea742020-09-08 20:28:22 +08003197 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
3198 sizeof(value));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303199
Weiyin Jiang280ea742020-09-08 20:28:22 +08003200 if (ret >= 0) {
3201 val = atoi(value);
3202 if (audio_is_a2dp_out_device(val)) {
3203 ALOGV("Received source device dis- connect request");
3204 close_a2dp_output();
3205 reset_a2dp_enc_config_params();
3206 reset_a2dp_source_dec_config_params();
3207 a2dp_reset_backend_cfg(SOURCE);
3208 } else if (audio_is_a2dp_in_device(val)) {
3209 ALOGV("Received sink device dis- connect request");
3210 close_a2dp_input();
3211 reset_a2dp_sink_dec_config_params();
3212 a2dp_reset_backend_cfg(SINK);
3213 }
3214 goto param_handled;
3215 }
Surendar Karka2febd452018-12-13 17:56:43 +05303216#ifndef LINUX_ENABLED
Weiyin Jiang280ea742020-09-08 20:28:22 +08003217 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
3218 if (ret >= 0) {
3219 ALOGD("Setting tws channel mode to %s",value);
3220 if (!(strncmp(value, "mono", strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05303221 a2dp.is_tws_mono_mode_on = true;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003222 else if (!(strncmp(value, "dual-mono", strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05303223 a2dp.is_tws_mono_mode_on = false;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003224 audio_a2dp_update_tws_channel_mode();
3225 goto param_handled;
3226 }
Manisha Agarwald45632b2019-10-17 18:14:28 +08003227
3228 ret = str_parms_get_str(parms, "LEAMono", value, sizeof(value));
3229 if (ret>=0) {
3230 ALOGD("Setting LC3 channel mode to %s",value);
3231 if (!(strncmp(value,"true",strlen(value))))
3232 a2dp.is_lc3_mono_mode_on = true;
3233 else
3234 a2dp.is_lc3_mono_mode_on = false;
3235 audio_a2dp_update_lc3_channel_mode();
3236 goto param_handled;
3237 }
Surendar Karka2febd452018-12-13 17:56:43 +05303238#endif
Weiyin Jiang280ea742020-09-08 20:28:22 +08003239 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
3240 if (ret >= 0) {
3241 if (a2dp.bt_lib_source_handle == NULL)
3242 goto param_handled;
3243
3244 if ((!strncmp(value, "true", sizeof(value)))) {
3245 if (a2dp.a2dp_source_suspended) {
3246 ALOGD("%s: A2DP is already suspended", __func__);
3247 goto param_handled;
3248 }
3249 ALOGD("Setting a2dp to suspend state");
3250 a2dp.a2dp_source_suspended = true;
3251 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
3252 goto param_handled;
3253 list_for_each(node, &a2dp.adev->usecase_list) {
3254 uc_info = node_to_item(node, struct audio_usecase, list);
3255 if (uc_info->type == PCM_PLAYBACK &&
3256 (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
3257 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
3258 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)) {
3259 fp_check_a2dp_restore_l(a2dp.adev, uc_info->stream.out, false);
Aalique Grahame22e49102018-12-18 14:23:57 -08003260 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08003261 }
3262 if (!a2dp.swb_configured)
3263 reset_codec_config();
3264 if (a2dp.audio_source_suspend)
3265 a2dp.audio_source_suspend();
3266 } else if (a2dp.a2dp_source_suspended == true) {
3267 ALOGD("Resetting a2dp suspend state");
3268 struct audio_usecase *uc_info;
3269 struct listnode *node;
3270 if (a2dp.clear_source_a2dpsuspend_flag)
3271 a2dp.clear_source_a2dpsuspend_flag();
3272 a2dp.a2dp_source_suspended = false;
3273 /*
3274 * It is possible that before suspend,a2dp sessions can be active
3275 * for example during music + voice activation concurrency
3276 * a2dp suspend will be called & BT will change to sco mode
3277 * though music is paused as a part of voice activation
3278 * compress session close happens only after pause timeout(10secs)
3279 * so if resume request comes before pause timeout as a2dp session
3280 * is already active IPC start will not be called from APM/audio_hw
3281 * Fix is to call a2dp start for IPC library post suspend
3282 * based on number of active session count
3283 */
3284 if (a2dp.a2dp_source_total_active_session_requests > 0) {
3285 ALOGD(" Calling IPC lib start post suspend state");
3286 if (a2dp.audio_source_start) {
3287 ret = a2dp.audio_source_start();
3288 if (ret != 0) {
3289 ALOGE("BT controller start failed");
3290 a2dp.a2dp_source_started = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303291 }
3292 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08003293 }
3294 list_for_each(node, &a2dp.adev->usecase_list) {
3295 uc_info = node_to_item(node, struct audio_usecase, list);
3296 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
3297 is_a2dp_out_device_type(&uc_info->stream.out->device_list)) {
3298 fp_check_a2dp_restore_l(a2dp.adev, uc_info->stream.out, true);
Zhou Song10617ed2017-05-26 13:28:48 +08003299 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303300 }
3301 }
3302 goto param_handled;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003303 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003304
Weiyin Jiang280ea742020-09-08 20:28:22 +08003305 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
3306 sizeof(value));
3307 if (ret >= 0) {
3308 if (a2dp.is_a2dp_offload_supported &&
3309 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
3310 *reconfig = true;
3311 }
3312 goto param_handled;
3313 }
Aalique Grahame22e49102018-12-18 14:23:57 -08003314
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303315param_handled:
Weiyin Jiang280ea742020-09-08 20:28:22 +08003316 ALOGV("end of a2dp setparam");
3317 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303318}
3319
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003320void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05303321{
3322 a2dp.is_handoff_in_progress = is_on;
3323}
3324
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003325bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303326{
3327 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05303328 // Or if a2dp device is selected but earlier start failed ( as a2dp
3329 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02003330 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303331}
3332
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003333void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303334{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303335 *sample_rate = a2dp.enc_sampling_rate;
3336}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303337
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003338void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02003339{
3340 *sample_rate = a2dp.dec_sampling_rate;
3341}
3342
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003343bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303344{
3345 bool ret = false;
3346
Florian Pfister1a84f312018-07-19 14:38:18 +02003347 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303348 return ret;
3349
Florian Pfister1a84f312018-07-19 14:38:18 +02003350 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08003351 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02003352 (a2dp.audio_source_check_a2dp_ready))
3353 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05303354 return ret;
3355}
3356
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003357bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02003358{
3359 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303360}
3361
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003362void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08003363 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303364{
3365 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02003366 a2dp.bt_lib_source_handle = NULL;
3367 a2dp.a2dp_source_started = false;
3368 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
3369 a2dp.a2dp_source_total_active_session_requests = 0;
3370 a2dp.a2dp_source_suspended = false;
3371 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303372 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303373 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07003374 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05303375 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08003376 a2dp.abr_config.is_abr_enabled = false;
3377 a2dp.abr_config.abr_started = false;
3378 a2dp.abr_config.imc_instance = 0;
3379 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08003380 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05303381 a2dp.is_tws_mono_mode_on = false;
Manisha Agarwald45632b2019-10-17 18:14:28 +08003382 a2dp.is_lc3_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05303383 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08003384 a2dp.swb_configured = false;
3385
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003386 // init function pointers
3387 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08003388 init_config.fp_platform_get_pcm_device_id;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003389 fp_check_a2dp_restore_l = init_config.fp_check_a2dp_restore_l;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003390
kunleiz5a127262017-09-08 14:47:48 +08003391 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05303392 reset_a2dp_source_dec_config_params();
3393 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02003394
3395 a2dp.bt_lib_sink_handle = NULL;
3396 a2dp.a2dp_sink_started = false;
3397 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
3398 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003399
3400 if (is_running_with_enhanced_fwk == UNINITIALIZED)
3401 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
3402 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08003403 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02003404
3405 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303406 update_offload_codec_capabilities();
3407}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003408
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003409uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003410{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003411 uint32_t latency = 0;
3412 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303413 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
3414 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003415 char value[PROPERTY_VALUE_MAX];
3416
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003417 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08003418 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003419 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303420 if (sscanf(value, "%d/%d/%d/%d/%d%d",
3421 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003422 ALOGI("Failed to parse avsync offset params from '%s'.", value);
3423 avsync_runtime_prop = 0;
3424 }
3425 }
3426
yidongh0515e042017-07-06 15:00:34 +08003427 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02003428 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
3429 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08003430 }
3431
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07003432 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003433 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08003434 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
3435 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003436 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003437 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08003438 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
3439 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003440 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003441 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08003442 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
3443 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003444 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003445 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08003446 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
3447 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003448 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003449 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05303450 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
3451 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
3452 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003453 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303454 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
3455 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
3456 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05303457 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05303458 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05303459 break;
Manisha Agarwald45632b2019-10-17 18:14:28 +08003460 case CODEC_TYPE_LC3:
3461 latency = slatency;
3462 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08003463 case CODEC_TYPE_PCM:
3464 latency = ENCODER_LATENCY_PCM;
3465 latency += DEFAULT_SINK_LATENCY_PCM;
3466 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003467 default:
3468 latency = 200;
3469 break;
3470 }
3471 return latency;
3472}
Aalique Grahame22e49102018-12-18 14:23:57 -08003473
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003474int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003475 struct str_parms *reply)
3476{
3477 int ret, val = 0;
3478 char value[32]={0};
3479
3480 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3481 value, sizeof(value));
3482 if (ret >= 0) {
3483 val = a2dp.is_a2dp_offload_supported;
3484 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3485 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3486 }
3487
3488 return 0;
3489}
Zhou Song12c29502019-03-16 10:37:18 +08003490
3491
3492bool configure_aptx_ad_speech_enc_fmt() {
3493 struct mixer_ctl *ctl_enc_data = NULL;
3494 int mixer_size = 0;
3495 int ret = 0;
3496 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3497
3498 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3499 if (!ctl_enc_data) {
3500 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3501 return false;
3502 }
3503
3504 /* Initialize dsp configuration params */
3505 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3506 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3507 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3508 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3509 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3510 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3511 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3512 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3513 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3514 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3515 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3516
3517 /* Configure AFE DSP configuration */
3518 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3519 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3520 mixer_size);
3521 if (ret != 0) {
3522 ALOGE("%s: Failed to set SWB encoder config", __func__);
3523 return false;
3524 }
3525
3526 /* Configure AFE Input Bit Format as PCM_16 */
3527 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3528 if (ret != 0) {
3529 ALOGE("%s: Failed to set SWB bit format", __func__);
3530 return false;
3531 }
3532
3533 return true;
3534}
3535
3536bool configure_aptx_ad_speech_dec_fmt()
3537{
3538 struct mixer_ctl *ctl_dec_data = NULL;
3539 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3540 int ret = 0;
3541
3542 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3543 if (!ctl_dec_data) {
3544 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3545 return false;
3546 }
3547 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3548 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3549 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3550 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3551 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3552 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3553 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3554 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3555
3556 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3557 sizeof(dec_cfg));
3558 if (ret != 0) {
3559 ALOGE("%s: Failed to set decoder config", __func__);
3560 return false;
3561 }
3562 return true;
3563}
3564
3565int sco_start_configuration()
3566{
3567 ALOGD("sco_start_configuration start");
3568
3569 if (!a2dp.swb_configured) {
3570 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3571 /* Configure AFE codec*/
3572 if (configure_aptx_ad_speech_enc_fmt() &&
3573 configure_aptx_ad_speech_dec_fmt()) {
3574 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3575 } else {
3576 ALOGE("%s: failed to send SCO configuration", __func__);
3577 return -ETIMEDOUT;
3578 }
3579 /* Configure backend*/
3580 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3581 a2dp.enc_channels = CH_MONO;
3582 a2dp.abr_config.is_abr_enabled = true;
3583 a2dp_set_backend_cfg(SOURCE);
3584 /* Start abr*/
3585 start_abr();
3586 a2dp.swb_configured = true;
3587 }
3588 return 0;
3589}
3590
3591void sco_reset_configuration()
3592{
Zhou Songd6d71752019-05-21 18:08:51 +08003593 if (a2dp.swb_configured) {
3594 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003595
Zhou Songd6d71752019-05-21 18:08:51 +08003596 reset_codec_config();
3597 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3598 a2dp.swb_configured = false;
3599 }
Zhou Song12c29502019-03-16 10:37:18 +08003600}