blob: cade8cdba75e9a714051cc39ebeafe176430e673 [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
Naresh Tanniru9d027a62015-03-13 01:32:10 +053065#define MEDIA_FMT_AAC_AOT_LC 2
66#define MEDIA_FMT_AAC_AOT_SBR 5
67#define MEDIA_FMT_AAC_AOT_PS 29
Naresh Tanniru9d027a62015-03-13 01:32:10 +053068#define PCM_CHANNEL_L 1
69#define PCM_CHANNEL_R 2
70#define PCM_CHANNEL_C 3
71#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
72#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
73#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
74#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
75#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
76#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
Surendar Karka2febd452018-12-13 17:56:43 +053077#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
Zhou Song8fccbb62019-03-20 01:08:19 +080078#define MIXER_ENC_APTX_AD_CONFIG_BLOCK "SLIM_7_RX APTX_AD Enc Cfg"
Surendar Karka2febd452018-12-13 17:56:43 +053079#define MIXER_SOURCE_DEC_CONFIG_BLOCK "SLIM_7_TX Decoder Config"
80#define MIXER_SINK_DEC_CONFIG_BLOCK "SLIM_9_TX Decoder Config"
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +053081#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
Florian Pfister1a84f312018-07-19 14:38:18 +020082#define MIXER_DEC_BIT_FORMAT "AFE Output Bit Format"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053083#define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080084#define MIXER_SAMPLE_RATE_RX "BT SampleRate RX"
Surendar Karka2febd452018-12-13 17:56:43 +053085#define MIXER_SOURCE_SAMPLE_RATE_TX "BT SampleRate TX"
Samyak Jainf69e9ef2018-06-12 12:26:37 +053086#define MIXER_SAMPLE_RATE_DEFAULT "BT SampleRate"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053087#define MIXER_AFE_IN_CHANNELS "AFE Input Channels"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080088#define MIXER_ABR_TX_FEEDBACK_PATH "A2DP_SLIM7_UL_HL Switch"
Zhou Song12c29502019-03-16 10:37:18 +080089#define MIXER_ABR_RX_FEEDBACK_PATH "SCO_SLIM7_DL_HL Switch"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080090#define MIXER_SET_FEEDBACK_CHANNEL "BT set feedback channel"
Surendar Karka2febd452018-12-13 17:56:43 +053091#define MIXER_SINK_SAMPLE_RATE "BT_TX SampleRate"
Florian Pfister1a84f312018-07-19 14:38:18 +020092#define MIXER_AFE_SINK_CHANNELS "AFE Output Channels"
Manisha Agarwala51768b2018-11-01 16:30:52 +053093#define MIXER_FMT_TWS_CHANNEL_MODE "TWS Channel Mode"
yidongh0515e042017-07-06 15:00:34 +080094#define ENCODER_LATENCY_SBC 10
95#define ENCODER_LATENCY_APTX 40
96#define ENCODER_LATENCY_APTX_HD 20
97#define ENCODER_LATENCY_AAC 70
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +053098//To Do: Fine Tune Encoder CELT/LDAC latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +053099#define ENCODER_LATENCY_CELT 40
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530100#define ENCODER_LATENCY_LDAC 40
Aalique Grahame22e49102018-12-18 14:23:57 -0800101#define ENCODER_LATENCY_PCM 50
yidongh0515e042017-07-06 15:00:34 +0800102#define DEFAULT_SINK_LATENCY_SBC 140
103#define DEFAULT_SINK_LATENCY_APTX 160
104#define DEFAULT_SINK_LATENCY_APTX_HD 180
105#define DEFAULT_SINK_LATENCY_AAC 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530106//To Do: Fine Tune Default CELT/LDAC Latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530107#define DEFAULT_SINK_LATENCY_CELT 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530108#define DEFAULT_SINK_LATENCY_LDAC 180
Aalique Grahame22e49102018-12-18 14:23:57 -0800109#define DEFAULT_SINK_LATENCY_PCM 140
110
111#define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported"
112#define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled"
113#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
114
115// Default encoder bit width
116#define DEFAULT_ENCODER_BIT_FORMAT 16
117
118// Default encoder latency
119#define DEFAULT_ENCODER_LATENCY 200
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530120
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800121// Slimbus Tx sample rate for ABR feedback channel
122#define ABR_TX_SAMPLE_RATE "KHZ_8"
123
Zhou Song12c29502019-03-16 10:37:18 +0800124// Slimbus Tx sample rate for APTX AD SPEECH
125#define SPEECH_TX_SAMPLE_RATE "KHZ_96"
126
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800127// Purpose ID for Inter Module Communication (IMC) in AFE
128#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
129
130// Maximum quality levels for ABR
131#define MAX_ABR_QUALITY_LEVELS 5
132
133// Instance identifier for A2DP
134#define MAX_INSTANCE_ID (UINT32_MAX / 2)
135
Zhou Song12c29502019-03-16 10:37:18 +0800136// Instance identifier for SWB
137#define APTX_AD_SPEECH_INSTANCE_ID 37
138
139#define SAMPLING_RATE_96K 96000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530140#define SAMPLING_RATE_48K 48000
141#define SAMPLING_RATE_441K 44100
Zhou Song12c29502019-03-16 10:37:18 +0800142#define SAMPLING_RATE_32K 32000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530143#define CH_STEREO 2
144#define CH_MONO 1
Florian Pfister1a84f312018-07-19 14:38:18 +0200145#define SOURCE 0
146#define SINK 1
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800147#define UNINITIALIZED -1
148
149#ifdef __LP64__
150#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
151#else
152#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
153#endif
154
155static void *vndk_fwk_lib_handle = NULL;
156static int is_running_with_enhanced_fwk = UNINITIALIZED;
157
158typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
159static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
Florian Pfister1a84f312018-07-19 14:38:18 +0200160
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530161/*
162 * Below enum values are extended from audio_base.h to
Florian Pfister1a84f312018-07-19 14:38:18 +0200163 * to keep encoder and decoder type local to bthost_ipc
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530164 * and audio_hal as these are intended only for handshake
165 * between IPC lib and Audio HAL.
166 */
167typedef enum {
Florian Pfister1a84f312018-07-19 14:38:18 +0200168 CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
169 CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
170 CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
171 CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
172 CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530173#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +0200174 CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530175#endif
Florian Pfister1a84f312018-07-19 14:38:18 +0200176 CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
177 CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
Surendar Karka2febd452018-12-13 17:56:43 +0530178 CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
Zhou Song12c29502019-03-16 10:37:18 +0800179 CODEC_TYPE_APTX_AD_SPEECH = 637534208u, //0x26000000UL
Aalique Grahame22e49102018-12-18 14:23:57 -0800180 CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u
Florian Pfister1a84f312018-07-19 14:38:18 +0200181}codec_t;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530182
Sharad Sangle95d451b2018-06-19 12:24:20 +0530183/*
184 * enums which describes the APTX Adaptive
185 * channel mode, these values are used by encoder
186 */
187 typedef enum {
188 APTX_AD_CHANNEL_UNCHANGED = -1,
189 APTX_AD_CHANNEL_JOINT_STEREO = 0, // default
190 APTX_AD_CHANNEL_MONO = 1,
191 APTX_AD_CHANNEL_DUAL_MONO = 2,
192 APTX_AD_CHANNEL_STEREO_TWS = 4,
193 APTX_AD_CHANNEL_EARBUD = 8,
194} enc_aptx_ad_channel_mode;
195
196/*
197 * enums which describes the APTX Adaptive
198 * sampling frequency, these values are used
199 * by encoder
200 */
201typedef enum {
202 APTX_AD_SR_UNCHANGED = 0x0,
203 APTX_AD_48 = 0x1, // 48 KHz default
204 APTX_AD_44_1 = 0x2, // 44.1kHz
Zhou Song30e8cea2019-10-22 23:39:25 +0800205 APTX_AD_96 = 0x3, // 96KHz
Sharad Sangle95d451b2018-06-19 12:24:20 +0530206} enc_aptx_ad_s_rate;
207
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530208typedef void (*bt_audio_pre_init_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200209typedef int (*audio_source_open_t)(void);
210typedef int (*audio_source_close_t)(void);
211typedef int (*audio_source_start_t)(void);
212typedef int (*audio_source_stop_t)(void);
213typedef int (*audio_source_suspend_t)(void);
214typedef void (*audio_source_handoff_triggered_t)(void);
215typedef void (*clear_source_a2dpsuspend_flag_t)(void);
216typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
217 uint8_t *num_dev, codec_t *codec_type);
218typedef int (*audio_source_check_a2dp_ready_t)(void);
219typedef int (*audio_is_source_scrambling_enabled_t)(void);
Manisha Agarwala51768b2018-11-01 16:30:52 +0530220typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200221typedef int (*audio_sink_start_t)(void);
222typedef int (*audio_sink_stop_t)(void);
223typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
224typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
225typedef int (*audio_sink_check_a2dp_ready_t)(void);
226typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530227
228enum A2DP_STATE {
229 A2DP_STATE_CONNECTED,
230 A2DP_STATE_STARTED,
231 A2DP_STATE_STOPPED,
232 A2DP_STATE_DISCONNECTED,
233};
234
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800235typedef enum {
236 IMC_TRANSMIT,
237 IMC_RECEIVE,
238} imc_direction_t;
239
240typedef enum {
241 IMC_DISABLE,
242 IMC_ENABLE,
243} imc_status_t;
244
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700245typedef enum {
Zhou Song12c29502019-03-16 10:37:18 +0800246 SWAP_DISABLE,
247 SWAP_ENABLE,
248} swap_status_t;
249
250typedef enum {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700251 MTU_SIZE,
252 PEAK_BIT_RATE,
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530253 BIT_RATE_MODE,
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700254} frame_control_type_t;
255
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800256// --- external function dependency ---
257fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
Weiyin Jiangd5974e62020-09-08 20:28:22 +0800258fp_check_a2dp_restore_t fp_check_a2dp_restore_l;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800259
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800260/* PCM config for ABR Feedback hostless front end */
261static struct pcm_config pcm_config_abr = {
262 .channels = 1,
263 .rate = 8000,
264 .period_size = 240,
265 .period_count = 2,
266 .format = PCM_FORMAT_S16_LE,
267 .start_threshold = 0,
268 .stop_threshold = INT_MAX,
269 .avail_min = 0,
270};
271
272/* Adaptive bitrate config for A2DP codecs */
273struct a2dp_abr_config {
274 /* Flag to denote whether Adaptive bitrate is enabled for codec */
275 bool is_abr_enabled;
276 /* Flag to denote whether front end has been opened for ABR */
277 bool abr_started;
278 /* ABR Tx path pcm handle */
279 struct pcm *abr_tx_handle;
Zhou Song12c29502019-03-16 10:37:18 +0800280 /* ABR Rx path pcm handle */
281 struct pcm *abr_rx_handle;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800282 /* ABR Inter Module Communication (IMC) instance ID */
283 uint32_t imc_instance;
284};
285
286static uint32_t instance_id = MAX_INSTANCE_ID;
287
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530288/* structure used to update a2dp state machine
289 * to communicate IPC library
290 * to store DSP encoder configuration information
291 */
292struct a2dp_data {
293 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200294 void *bt_lib_source_handle;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530295 bt_audio_pre_init_t bt_audio_pre_init;
Florian Pfister1a84f312018-07-19 14:38:18 +0200296 audio_source_open_t audio_source_open;
297 audio_source_close_t audio_source_close;
298 audio_source_start_t audio_source_start;
299 audio_source_stop_t audio_source_stop;
300 audio_source_suspend_t audio_source_suspend;
301 audio_source_handoff_triggered_t audio_source_handoff_triggered;
302 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
303 audio_get_enc_config_t audio_get_enc_config;
304 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530305 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200306 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
307 enum A2DP_STATE bt_state_source;
308 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530309 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530310 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200311 bool a2dp_source_started;
312 bool a2dp_source_suspended;
313 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530314 bool is_a2dp_offload_supported;
315 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700316 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530317 /* Mono Mode support for TWS+ */
318 bool is_tws_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530319 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800320 /* Adaptive bitrate config for A2DP codecs */
321 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200322
323 void *bt_lib_sink_handle;
324 audio_sink_start_t audio_sink_start;
325 audio_sink_stop_t audio_sink_stop;
326 audio_get_dec_config_t audio_get_dec_config;
327 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
328 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
329 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
330 enum A2DP_STATE bt_state_sink;
331 codec_t bt_decoder_format;
332 uint32_t dec_sampling_rate;
333 uint32_t dec_channels;
334 bool a2dp_sink_started;
335 int a2dp_sink_total_active_session_requests;
Zhou Song12c29502019-03-16 10:37:18 +0800336 bool swb_configured;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530337};
338
339struct a2dp_data a2dp;
340
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800341/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
342 * Structures sent to configure DSP for ABR are defined below.
343 * This data helps DSP configure feedback path (BTSoC to LPASS)
344 * for link quality levels and mapping quality levels to codec
345 * specific bitrate.
346 */
347
348/* Key value pair for link quality level to bitrate mapping. */
349struct bit_rate_level_map_t {
350 uint32_t link_quality_level;
351 uint32_t bitrate;
352};
353
354/* Link quality level to bitrate mapping info sent to DSP. */
355struct quality_level_to_bitrate_info {
356 /* Number of quality levels being mapped.
357 * This will be equal to the size of mapping table.
358 */
359 uint32_t num_levels;
360 /* Quality level to bitrate mapping table */
361 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
362};
363
364/* Structure to set up Inter Module Communication (IMC) between
365 * AFE Decoder and Encoder.
366 */
367struct imc_dec_enc_info {
368 /* Decoder to encoder communication direction.
369 * Transmit = 0 / Receive = 1
370 */
371 uint32_t direction;
372 /* Enable / disable IMC between decoder and encoder */
373 uint32_t enable;
374 /* Purpose of IMC being set up between decoder and encoder.
375 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
376 * is the default value to be sent as purpose.
377 */
378 uint32_t purpose;
379 /* Unique communication instance ID.
380 * purpose and comm_instance together form the actual key
381 * used in IMC registration, which must be the same for
382 * encoder and decoder for which IMC is being set up.
383 */
384 uint32_t comm_instance;
385};
386
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700387/* Structure to control frame size of AAC encoded frames. */
388struct aac_frame_size_control_t {
389 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
390 uint32_t ctl_type;
391 /* Control value
392 * MTU_SIZE: MTU size in bytes
393 * PEAK_BIT_RATE: Peak bitrate in bits per second.
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530394 * BIT_RATE_MODE: Variable bitrate
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700395 */
396 uint32_t ctl_value;
397};
398
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800399/* Structure used for ABR config of AFE encoder and decoder. */
400struct abr_enc_cfg_t {
401 /* Link quality level to bitrate mapping info sent to DSP. */
402 struct quality_level_to_bitrate_info mapping_info;
403 /* Information to set up IMC between decoder and encoder */
404 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700405 /* Flag to indicate whether ABR is enabled */
406 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800407} __attribute__ ((packed));
408
409/* Structure to send configuration for decoder introduced
410 * on AFE Tx path for ABR link quality feedback to BT encoder.
411 */
412struct abr_dec_cfg_t {
413 /* Decoder media format */
414 uint32_t dec_format;
415 /* Information to set up IMC between decoder and encoder */
416 struct imc_dec_enc_info imc_info;
417} __attribute__ ((packed));
418
Zhou Song12c29502019-03-16 10:37:18 +0800419struct aptx_ad_speech_mode_cfg_t
420{
421 uint32_t mode;
422 uint32_t swapping;
423} __attribute__ ((packed));
424
425/* Structure for SWB voice dec config */
426struct aptx_ad_speech_dec_cfg_t {
427 struct abr_dec_cfg_t abr_cfg;
428 struct aptx_ad_speech_mode_cfg_t speech_mode;
429} __attribute__ ((packed));
430
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530431/* START of DSP configurable structures
432 * These values should match with DSP interface defintion
433 */
434
435/* AAC encoder configuration structure. */
436typedef struct aac_enc_cfg_t aac_enc_cfg_t;
437
438/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
439 * supported aac_fmt_flag are ADTS/RAW
440 * supported channel_cfg are Native mode, Mono , Stereo
441 */
442struct aac_enc_cfg_t {
443 uint32_t enc_format;
444 uint32_t bit_rate;
445 uint32_t enc_mode;
446 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530447 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530448 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530449} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530450
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700451struct aac_enc_cfg_v2_t {
452 struct aac_enc_cfg_t aac_enc_cfg;
453 struct aac_frame_size_control_t frame_ctl;
454} __attribute__ ((packed));
455
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530456struct aac_enc_cfg_v3_t {
457 struct aac_enc_cfg_t aac_enc_cfg;
458 struct aac_frame_size_control_t frame_ctl;
459 struct aac_frame_size_control_t aac_key_value_ctl;
460} __attribute__ ((packed));
461
Surendar Karkabbb3c822018-11-12 13:00:38 +0530462typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
463struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200464 uint16_t aac_fmt_flag; /* LATM*/
465 uint16_t audio_object_type; /* LC */
466 uint16_t channels; /* Stereo */
467 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
468 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
469 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530470} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200471
Surendar Karkabbb3c822018-11-12 13:00:38 +0530472typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
473struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200474 uint16_t channels; /* Mono, Stereo */
475 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
476 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530477} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200478
479/* AAC decoder configuration structure. */
480typedef struct aac_dec_cfg_t aac_dec_cfg_t;
481struct aac_dec_cfg_t {
482 uint32_t dec_format;
483 audio_aac_decoder_config_t data;
484} __attribute__ ((packed));
485
486/* SBC decoder configuration structure. */
487typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
488struct sbc_dec_cfg_t {
489 uint32_t dec_format;
490 audio_sbc_decoder_config_t data;
491} __attribute__ ((packed));
492
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530493/* SBC encoder configuration structure. */
494typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
495
496/* supported num_subbands are 4/8
497 * supported blk_len are 4, 8, 12, 16
498 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
499 * supported alloc_method are LOUNDNESS/SNR
500 * supported bit_rate for mono channel is max 320kbps
501 * supported bit rate for stereo channel is max 512 kbps
502 */
503struct sbc_enc_cfg_t{
504 uint32_t enc_format;
505 uint32_t num_subbands;
506 uint32_t blk_len;
507 uint32_t channel_mode;
508 uint32_t alloc_method;
509 uint32_t bit_rate;
510 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530511} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530512
513
514/* supported num_channels are Mono/Stereo
515 * supported channel_mapping for mono is CHANNEL_C
516 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
517 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700518 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530519struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530520{
521 uint32_t enc_format;
522 uint32_t sample_rate;
523 uint16_t num_channels;
524 uint16_t reserved;
525 uint8_t channel_mapping[8];
526 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530527} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530528
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530529struct celt_specific_enc_cfg_t
530{
531 uint32_t bit_rate;
532 uint16_t frame_size;
533 uint16_t complexity;
534 uint16_t prediction_mode;
535 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530536} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530537
538struct celt_enc_cfg_t
539{
540 struct custom_enc_cfg_t custom_cfg;
541 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530542} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700543
544/* sync_mode introduced with APTX V2 libraries
545 * sync mode: 0x0 = stereo sync mode
546 * 0x01 = dual mono sync mode
547 * 0x02 = dual mono with no sync on either L or R codewords
548 */
549struct aptx_v2_enc_cfg_ext_t
550{
551 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530552} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700553
554/* APTX struct for combining custom enc and V2 fields */
555struct aptx_enc_cfg_t
556{
557 struct custom_enc_cfg_t custom_cfg;
558 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530559} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700560
Sharad Sangle95d451b2018-06-19 12:24:20 +0530561/* APTX AD structure */
562struct aptx_ad_enc_cfg_ext_t
563{
564 uint32_t sampling_freq;
565 uint32_t mtu;
566 uint32_t channel_mode;
567 uint32_t min_sink_modeA;
568 uint32_t max_sink_modeA;
569 uint32_t min_sink_modeB;
570 uint32_t max_sink_modeB;
571 uint32_t min_sink_modeC;
572 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530573 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530574} __attribute__ ((packed));
575
576struct aptx_ad_enc_cfg_t
577{
578 struct custom_enc_cfg_t custom_cfg;
579 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
580 struct abr_enc_cfg_t abr_cfg;
581} __attribute__ ((packed));
582
Zhou Song8fccbb62019-03-20 01:08:19 +0800583struct aptx_ad_enc_cfg_ext_r2_t
584{
585 uint32_t sampling_freq;
586 uint32_t mtu;
587 uint32_t channel_mode;
588 uint32_t min_sink_modeA;
589 uint32_t max_sink_modeA;
590 uint32_t min_sink_modeB;
591 uint32_t max_sink_modeB;
592 uint32_t min_sink_modeC;
593 uint32_t max_sink_modeC;
594 uint32_t mode;
595 uint32_t input_mode;
596 uint32_t fade_duration;
597 uint8_t sink_cap[11];
598} __attribute__ ((packed));
599
600struct aptx_ad_enc_cfg_r2_t
601{
602 struct custom_enc_cfg_t custom_cfg;
603 struct aptx_ad_enc_cfg_ext_r2_t aptx_ad_cfg;
604 struct abr_enc_cfg_t abr_cfg;
605} __attribute__ ((packed));
606
Zhou Song12c29502019-03-16 10:37:18 +0800607/* APTX AD SPEECH structure */
608struct aptx_ad_speech_enc_cfg_t
609{
610 struct custom_enc_cfg_t custom_cfg;
611 /* Information to set up IMC between decoder and encoder */
612 struct imc_dec_enc_info imc_info;
613 struct aptx_ad_speech_mode_cfg_t speech_mode;
614} __attribute__ ((packed));
615
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530616struct ldac_specific_enc_cfg_t
617{
618 uint32_t bit_rate;
619 uint16_t channel_mode;
620 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530621} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530622
623struct ldac_enc_cfg_t
624{
625 struct custom_enc_cfg_t custom_cfg;
626 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800627 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530628} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530629
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530630/* In LE BT source code uses system/audio.h for below
631 * structure definition. To avoid multiple definition
632 * compilation error for audiohal in LE , masking structure
633 * definition under "LINUX_ENABLED" which is defined only
634 * in LE
635 */
636#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530637/* TODO: Define the following structures only for O using PLATFORM_VERSION */
638/* Information about BT SBC encoder configuration
639 * This data is used between audio HAL module and
640 * BT IPC library to configure DSP encoder
641 */
642typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530643 uint32_t subband; /* 4, 8 */
644 uint32_t blk_len; /* 4, 8, 12, 16 */
645 uint16_t sampling_rate; /*44.1khz,48khz*/
646 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
647 uint8_t alloc; /*0(Loudness),1(SNR)*/
648 uint8_t min_bitpool; /* 2 */
649 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
650 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530651 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530652} audio_sbc_encoder_config;
653
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530654/* Information about BT APTX encoder configuration
655 * This data is used between audio HAL module and
656 * BT IPC library to configure DSP encoder
657 */
658typedef struct {
659 uint16_t sampling_rate;
660 uint8_t channels;
661 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530662 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700663} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530664
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700665typedef struct {
Zhou Song8fccbb62019-03-20 01:08:19 +0800666 uint32_t sampling_rate;
667 uint32_t mtu;
668 int32_t channel_mode;
669 uint32_t min_sink_modeA;
670 uint32_t max_sink_modeA;
671 uint32_t min_sink_modeB;
672 uint32_t max_sink_modeB;
673 uint32_t min_sink_modeC;
674 uint32_t max_sink_modeC;
675 uint32_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530676 uint8_t TTP_modeA_low;
677 uint8_t TTP_modeA_high;
678 uint8_t TTP_modeB_low;
679 uint8_t TTP_modeB_high;
Zhou Song8fccbb62019-03-20 01:08:19 +0800680 uint8_t TTP_TWS_low;
681 uint8_t TTP_TWS_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530682 uint32_t bits_per_sample;
Zhou Song8fccbb62019-03-20 01:08:19 +0800683 uint32_t input_mode;
684 uint32_t fade_duration;
685 uint8_t sink_cap[11];
Sharad Sangle95d451b2018-06-19 12:24:20 +0530686} audio_aptx_ad_config;
687
688typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700689 uint16_t sampling_rate;
690 uint8_t channels;
691 uint32_t bitrate;
692 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530693 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700694} audio_aptx_dual_mono_config;
695
696typedef union {
697 audio_aptx_default_config *default_cfg;
698 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530699 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700700} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530701
702/* Information about BT AAC encoder configuration
703 * This data is used between audio HAL module and
704 * BT IPC library to configure DSP encoder
705 */
706typedef struct {
707 uint32_t enc_mode; /* LC, SBR, PS */
708 uint16_t format_flag; /* RAW, ADTS */
709 uint16_t channels; /* 1-Mono, 2-Stereo */
710 uint32_t sampling_rate;
711 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530712 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530713} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530714#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700715
716typedef struct {
717 audio_aac_encoder_config audio_aac_enc_cfg;
718 struct aac_frame_size_control_t frame_ctl;
719} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530720
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530721typedef struct {
722 audio_aac_encoder_config audio_aac_enc_cfg;
723 struct aac_frame_size_control_t frame_ctl;
724 uint8_t size_control_struct;
725 struct aac_frame_size_control_t* frame_ptr_ctl;
726} audio_aac_encoder_config_v3;
727
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530728/* Information about BT CELT encoder configuration
729 * This data is used between audio HAL module and
730 * BT IPC library to configure DSP encoder
731 */
732typedef struct {
733 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
734 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
735 uint16_t frame_size; /* 64-128-256-512, 512 */
736 uint16_t complexity; /* 0-10, 1 */
737 uint16_t prediction_mode; /* 0-1-2, 0 */
738 uint16_t vbr_flag; /* 0-1, 0*/
739 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530740 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530741} audio_celt_encoder_config;
742
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530743/* Information about BT LDAC encoder configuration
744 * This data is used between audio HAL module and
745 * BT IPC library to configure DSP encoder
746 */
747typedef struct {
748 uint32_t sampling_rate; /*44100,48000,88200,96000*/
749 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
750 uint16_t channel_mode; /* 0, 4, 2, 1*/
751 uint16_t mtu; /*679*/
Aniket Kumar Latad0196282019-05-09 14:24:17 -0700752 uint32_t bits_per_sample;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800753 bool is_abr_enabled;
754 struct quality_level_to_bitrate_info level_to_bitrate_map;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530755} audio_ldac_encoder_config;
756
Surendar Karkabbb3c822018-11-12 13:00:38 +0530757/* Information about BT AAC decoder configuration
758 * This data is used between audio HAL module and
759 * BT IPC library to configure DSP decoder
760 */
761typedef struct {
762 uint16_t aac_fmt_flag; /* LATM*/
763 uint16_t audio_object_type; /* LC */
764 uint16_t channels; /* Stereo */
765 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
766 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
767 44.1k, 48k, 64k, 88.2k, 96k */
768} audio_aac_dec_config_t;
769
770/* Information about BT SBC decoder configuration
771 * This data is used between audio HAL module and
772 * BT IPC library to configure DSP decoder
773 */
774typedef struct {
775 uint16_t channels; /* Mono, Stereo */
776 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
777 44.1k, 48k, 64k, 88.2k, 96k */
778}audio_sbc_dec_config_t;
779
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530780/*********** END of DSP configurable structures ********************/
781
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530782static void update_offload_codec_capabilities()
783{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530784
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530785 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800786 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
787 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
788
789 ALOGD("%s: A2DP offload supported = %d",__func__,
790 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530791}
792
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800793static int stop_abr()
794{
795 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800796 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800797 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800798 int ret = 0;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800799
800 /* This function can be used if !abr_started for clean up */
801 ALOGV("%s: enter", __func__);
802
803 // Close hostless front end
804 if (a2dp.abr_config.abr_tx_handle != NULL) {
805 pcm_close(a2dp.abr_config.abr_tx_handle);
806 a2dp.abr_config.abr_tx_handle = NULL;
807 }
Zhou Song12c29502019-03-16 10:37:18 +0800808 if (a2dp.abr_config.abr_rx_handle != NULL) {
809 pcm_close(a2dp.abr_config.abr_rx_handle);
810 a2dp.abr_config.abr_rx_handle = NULL;
811 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800812 a2dp.abr_config.abr_started = false;
813 a2dp.abr_config.imc_instance = 0;
814
815 // Reset BT driver mixer control for ABR usecase
816 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
817 MIXER_SET_FEEDBACK_CHANNEL);
818 if (!ctl_set_bt_feedback_channel) {
819 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800820 ret = -ENOSYS;
821 } else if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800822 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800823 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800824 }
825
826 // Reset ABR Tx feedback path
827 ALOGV("%s: Disable ABR Tx feedback path", __func__);
828 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
829 MIXER_ABR_TX_FEEDBACK_PATH);
830 if (!ctl_abr_tx_path) {
831 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800832 ret = -ENOSYS;
833 } else if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800834 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800835 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800836 }
837
Zhou Song12c29502019-03-16 10:37:18 +0800838 // Reset ABR Rx feedback path
839 ALOGV("%s: Disable ABR Rx feedback path", __func__);
840 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
841 MIXER_ABR_RX_FEEDBACK_PATH);
842 if (!ctl_abr_rx_path) {
843 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
844 ret = -ENOSYS;
845 } else if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 0) != 0) {
846 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
847 ret = -ENOSYS;
848 }
849
850 return ret;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800851}
852
853static int start_abr()
854{
855 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800856 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800857 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
858 int abr_device_id;
859 int ret = 0;
860
861 if (!a2dp.abr_config.is_abr_enabled) {
862 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
863 return -ENOSYS;
864 }
865
866 if (a2dp.abr_config.abr_started) {
867 ALOGI("%s: ABR has already started", __func__);
868 return ret;
869 }
870
871 // Enable Slimbus 7 Tx feedback path
872 ALOGV("%s: Enable ABR Tx feedback path", __func__);
873 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
874 MIXER_ABR_TX_FEEDBACK_PATH);
875 if (!ctl_abr_tx_path) {
876 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
877 return -ENOSYS;
878 }
879 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
880 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
881 return -ENOSYS;
882 }
883
884 // Notify ABR usecase information to BT driver to distinguish
885 // between SCO and feedback usecase
886 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
887 MIXER_SET_FEEDBACK_CHANNEL);
888 if (!ctl_set_bt_feedback_channel) {
889 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800890 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800891 }
892 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
893 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800894 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800895 }
896
897 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800898 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800899 PCM_CAPTURE);
900 if (!a2dp.abr_config.abr_tx_handle) {
901 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
902 abr_device_id, PCM_IN,
903 &pcm_config_abr);
Zhou Song12c29502019-03-16 10:37:18 +0800904 if (a2dp.abr_config.abr_tx_handle == NULL) {
905 ALOGE("%s: Can't open abr tx device", __func__);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800906 goto fail;
Zhou Song12c29502019-03-16 10:37:18 +0800907 }
908 if (!(pcm_is_ready(a2dp.abr_config.abr_tx_handle) &&
909 !pcm_start(a2dp.abr_config.abr_tx_handle))) {
910 ALOGE("%s: tx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
911 goto fail;
912 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800913 }
Zhou Song12c29502019-03-16 10:37:18 +0800914
915 // Enable Slimbus 7 Rx feedback path for HD Voice use case
916 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH) {
917 ALOGV("%s: Enable ABR Rx feedback path", __func__);
918 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
919 MIXER_ABR_RX_FEEDBACK_PATH);
920 if (!ctl_abr_rx_path) {
921 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
922 goto fail;
923 }
924 if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 1) != 0) {
925 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
926 goto fail;
927 }
928
929 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
930 ALOGE("%s: Failed to set BT usecase", __func__);
931 goto fail;
932 }
933
934 // Open hostless front end and prepare ABR Rx path
935 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
936 PCM_PLAYBACK);
937 if (!a2dp.abr_config.abr_rx_handle) {
938 a2dp.abr_config.abr_rx_handle = pcm_open(a2dp.adev->snd_card,
939 abr_device_id, PCM_OUT,
940 &pcm_config_abr);
941 if (a2dp.abr_config.abr_rx_handle == NULL) {
942 ALOGE("%s: Can't open abr rx device", __func__);
943 goto fail;
944 }
945 if (!(pcm_is_ready(a2dp.abr_config.abr_rx_handle) &&
946 !pcm_start(a2dp.abr_config.abr_rx_handle))) {
947 ALOGE("%s: rx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_rx_handle));
948 goto fail;
949 }
950 }
951 }
952
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800953 a2dp.abr_config.abr_started = true;
954
955 return ret;
956
957fail:
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800958 stop_abr();
959 return -ENOSYS;
960}
961
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800962static int check_if_enhanced_fwk() {
963
964 int is_enhanced_fwk = 1;
965 //dlopen lib
966 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
967 if (vndk_fwk_lib_handle != NULL) {
968 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
969 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
970 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
971 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
972 __func__);
973 is_enhanced_fwk = 1;
974 } else {
975 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
976 }
977 }
978 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
979 return is_enhanced_fwk;
980}
981
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530982static void open_a2dp_source() {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530983 int ret = 0;
984
Florian Pfister1a84f312018-07-19 14:38:18 +0200985 ALOGD(" Open A2DP source start ");
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530986
987 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
988 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
989 ALOGD("calling BT stream open");
990 ret = a2dp.audio_source_open();
991 if(ret != 0) {
992 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Srinu Jellada99a592019-01-25 16:50:52 +0530993 }
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530994 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Lakshman Chaluvaraju6cbe1362021-06-18 14:32:41 +0530995 if (!a2dp.adev->bt_sco_on)
996 a2dp.a2dp_source_suspended = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530997 } else {
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530998 ALOGD("Called a2dp open with improper state %d", a2dp.bt_state_source);
999 }
1000 } else {
1001 ALOGE("a2dp handle is not identified, Ignoring open request");
1002 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
1003 }
1004}
1005/* API to open BT IPC library to start IPC communication for BT Source*/
1006static void a2dp_source_init()
1007{
1008 ALOGD("a2dp_source_init START");
1009 if (a2dp.bt_lib_source_handle == NULL) {
1010 ALOGD("Requesting for BT lib handle");
1011 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
1012 if (a2dp.bt_lib_source_handle == NULL) {
1013 ALOGE("%s: dlopen failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
1014 return;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301015 }
1016 }
1017
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301018 a2dp.bt_audio_pre_init = (bt_audio_pre_init_t)
1019 dlsym(a2dp.bt_lib_source_handle, "bt_audio_pre_init");
Srinu Jellada99a592019-01-25 16:50:52 +05301020 a2dp.audio_source_open = (audio_source_open_t)
1021 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
1022 a2dp.audio_source_start = (audio_source_start_t)
1023 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
1024 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
1025 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
1026 a2dp.audio_source_suspend = (audio_source_suspend_t)
1027 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
1028 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
1029 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
1030 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1031 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
1032 a2dp.audio_source_stop = (audio_source_stop_t)
1033 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
1034 a2dp.audio_source_close = (audio_source_close_t)
1035 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
1036 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
1037 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
1038 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
1039 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
1040 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1041 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1042 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1043 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1044
Srinu Jella793f5302019-07-19 15:53:55 +05301045 if (a2dp.bt_lib_source_handle && a2dp.bt_audio_pre_init) {
1046 ALOGD("calling BT module preinit");
1047 // fwk related check's will be done in the BT layer
1048 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001049 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001050}
1051
1052/* API to open BT IPC library to start IPC communication for BT Sink*/
1053static void open_a2dp_sink()
1054{
1055 ALOGD(" Open A2DP input start ");
1056 if (a2dp.bt_lib_sink_handle == NULL){
1057 ALOGD(" Requesting for BT lib handle");
1058 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1059
1060 if (a2dp.bt_lib_sink_handle == NULL) {
1061 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1062 } else {
1063 a2dp.audio_sink_start = (audio_sink_start_t)
1064 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1065 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1066 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1067 a2dp.audio_sink_stop = (audio_sink_stop_t)
1068 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1069 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1070 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1071 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1072 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1073 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301074 }
1075}
1076
1077static int close_a2dp_output()
1078{
1079 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001080
1081 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1082 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301083 return -ENOSYS;
1084 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001085
1086 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1087 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001088 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001089 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301090 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001091 a2dp.a2dp_source_started = false;
1092 a2dp.a2dp_source_total_active_session_requests = 0;
Lakshman Chaluvaraju6cbe1362021-06-18 14:32:41 +05301093 if (!a2dp.adev->bt_sco_on)
1094 a2dp.a2dp_source_suspended = false;
Florian Pfister1a84f312018-07-19 14:38:18 +02001095 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301096 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301097 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001098 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001099 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1100 stop_abr();
1101 a2dp.abr_config.is_abr_enabled = false;
1102 a2dp.abr_config.abr_started = false;
1103 a2dp.abr_config.imc_instance = 0;
1104 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001105 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301106 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001107
1108 return 0;
1109}
1110
1111static int close_a2dp_input()
1112{
1113 ALOGV("%s\n",__func__);
1114
1115 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1116 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1117 return -ENOSYS;
1118 }
1119
1120 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1121 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001122 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001123 ALOGE("failed close a2dp sink control path from BT library");
1124 }
1125 a2dp.a2dp_sink_started = false;
1126 a2dp.a2dp_sink_total_active_session_requests = 0;
1127 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1128 a2dp.dec_sampling_rate = 48000;
1129 a2dp.dec_channels = 2;
1130 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301131
1132 return 0;
1133}
1134
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301135static void a2dp_check_and_set_scrambler()
1136{
1137 bool scrambler_mode = false;
1138 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001139 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1140 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301141
1142 if (scrambler_mode) {
1143 //enable scrambler in dsp
1144 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1145 MIXER_SCRAMBLER_MODE);
1146 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001147 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301148 return;
1149 } else {
1150 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1151 ALOGE("%s: Could not set scrambler mode", __func__);
1152 return;
1153 }
1154 }
1155 }
1156}
1157
Florian Pfister1a84f312018-07-19 14:38:18 +02001158static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301159{
Florian Pfister1a84f312018-07-19 14:38:18 +02001160 char *rate_str = NULL, *channels = NULL;
1161 uint32_t sampling_rate;
1162 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1163 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301164
Florian Pfister1a84f312018-07-19 14:38:18 +02001165 if (direction == SINK) {
1166 sampling_rate = a2dp.dec_sampling_rate;
1167 } else {
1168 sampling_rate = a2dp.enc_sampling_rate;
1169 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001170 /*
1171 * For LDAC encoder and AAC decoder open slimbus port at
1172 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1173 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1174 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001175 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301176 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001177 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1178 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301179 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1180 sampling_rate = sampling_rate *2;
1181 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001182
Aalique Grahame22e49102018-12-18 14:23:57 -08001183 // No need to configure backend for PCM format.
1184 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1185 return 0;
1186 }
1187
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301188 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301189 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301190 case 44100:
1191 rate_str = "KHZ_44P1";
1192 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301193 case 88200:
1194 rate_str = "KHZ_88P2";
1195 break;
1196 case 96000:
1197 rate_str = "KHZ_96";
1198 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001199 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301200 default:
1201 rate_str = "KHZ_48";
1202 break;
1203 }
1204
Florian Pfister1a84f312018-07-19 14:38:18 +02001205 if (direction == SINK) {
1206 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1207 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301208 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001209 } else {
1210 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1211 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301212 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001213 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301214 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001215
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301216 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1217 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301218 is_configured = false;
1219 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301220 }
1221
Surendar Karka2febd452018-12-13 17:56:43 +05301222 if (direction == SOURCE) {
1223 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001224 if (a2dp.abr_config.is_abr_enabled) {
1225 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH)
1226 rate_str = SPEECH_TX_SAMPLE_RATE;
1227 else
1228 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301229
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301230 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1231 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1232 MIXER_SOURCE_SAMPLE_RATE_TX);
1233 if (!ctl_sample_rate) {
1234 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1235 is_configured = false;
1236 goto fail;
1237 }
1238
1239 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1240 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1241 is_configured = false;
1242 goto fail;
1243 }
Surendar Karka2febd452018-12-13 17:56:43 +05301244 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301245 }
1246 } else {
1247 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1248 MIXER_SAMPLE_RATE_TX is not supported */
1249 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1250 MIXER_SAMPLE_RATE_DEFAULT);
1251 if (!ctl_sample_rate) {
1252 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301253 is_configured = false;
1254 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301255 }
1256
1257 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1258 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001259 is_configured = false;
1260 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301261 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301262 }
1263
Florian Pfister1a84f312018-07-19 14:38:18 +02001264 if (direction == SINK) {
1265 switch (a2dp.dec_channels) {
1266 case 1:
1267 channels = "One";
1268 break;
1269 case 2:
1270 default:
1271 channels = "Two";
1272 break;
1273 }
1274
Ramu Gottipati02809682018-12-19 16:46:12 +05301275 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001276 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1277 MIXER_AFE_SINK_CHANNELS);
1278 } else {
1279 //Configure AFE enc channels
1280 switch (a2dp.enc_channels) {
1281 case 1:
1282 channels = "One";
1283 break;
1284 case 2:
1285 default:
1286 channels = "Two";
1287 break;
1288 }
1289
Ramu Gottipati02809682018-12-19 16:46:12 +05301290 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001291 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1292 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301293 }
1294
Florian Pfister1a84f312018-07-19 14:38:18 +02001295 if (!ctrl_channels) {
1296 ALOGE(" ERROR AFE channels mixer control not identified");
1297 } else {
1298 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301299 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001300 is_configured = false;
1301 goto fail;
1302 }
1303 }
1304 is_configured = true;
1305fail:
1306 return is_configured;
1307}
1308
Aniket Kumar Latabce0be62019-07-11 14:20:23 -07001309bool a2dp_set_source_backend_cfg()
1310{
1311 if (a2dp.a2dp_source_started && !a2dp.a2dp_source_suspended)
1312 return a2dp_set_backend_cfg(SOURCE);
1313
1314 return false;
1315}
1316
Surendar Karkabbb3c822018-11-12 13:00:38 +05301317bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001318{
1319 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1320 struct aac_dec_cfg_t aac_dsp_cfg;
1321 bool is_configured = false;
1322 int ret = 0;
1323
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001324 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001325 return false;
1326
Surendar Karka2febd452018-12-13 17:56:43 +05301327 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001328 if (!ctl_dec_data) {
1329 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1330 is_configured = false;
1331 goto fail;
1332 }
1333
1334 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1335 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1336 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1337 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1338 switch(aac_bt_cfg->audio_object_type) {
1339 case 0:
1340 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301341 break;
1342 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001343 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1344 break;
1345 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301346 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001347 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301348 break;
1349 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001350 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1351 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1352 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1353 sizeof(struct aac_dec_cfg_t));
1354 if (ret != 0) {
1355 ALOGE("%s: failed to set AAC decoder config", __func__);
1356 is_configured = false;
1357 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001358 }
1359
Florian Pfister1a84f312018-07-19 14:38:18 +02001360 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1361 MIXER_DEC_BIT_FORMAT);
1362 if (!ctrl_bit_format) {
1363 ALOGE(" ERROR Dec bit format mixer control not identified");
1364 is_configured = false;
1365 goto fail;
1366 }
1367 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1368 if (ret != 0) {
1369 ALOGE("%s: Failed to set bit format to decoder", __func__);
1370 is_configured = false;
1371 goto fail;
1372 }
1373
1374 is_configured = true;
1375 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1376 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1377 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1378 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1379 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1380fail:
1381 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301382}
1383
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301384static int a2dp_set_bit_format(uint32_t enc_bit_format)
1385{
1386 const char *bit_format = NULL;
1387 struct mixer_ctl *ctrl_bit_format = NULL;
1388
1389 // Configure AFE Input Bit Format
1390 switch (enc_bit_format) {
1391 case 32:
1392 bit_format = "S32_LE";
1393 break;
1394 case 24:
1395 bit_format = "S24_LE";
1396 break;
1397 case 16:
1398 default:
1399 bit_format = "S16_LE";
1400 break;
1401 }
1402
1403 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1404 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1405 MIXER_ENC_BIT_FORMAT);
1406 if (!ctrl_bit_format) {
1407 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1408 return -ENOSYS;
1409 }
1410 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1411 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1412 return -ENOSYS;
1413 }
1414 return 0;
1415}
1416
Surendar Karka2febd452018-12-13 17:56:43 +05301417static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301418{
Surendar Karka2febd452018-12-13 17:56:43 +05301419 const char *rate_str = "KHZ_8", *channels = "Zero";
1420 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1421 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301422
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001423 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301424 if (direction == SINK) {
1425 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1426 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1427 MIXER_SINK_SAMPLE_RATE);
1428 } else {
1429 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1430 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1431 MIXER_SAMPLE_RATE_RX);
1432 }
1433 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301434
Surendar Karka2febd452018-12-13 17:56:43 +05301435 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1436 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301437 return -ENOSYS;
1438 }
Zhou Song12c29502019-03-16 10:37:18 +08001439 if (a2dp.abr_config.is_abr_enabled) {
1440 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1441 MIXER_SOURCE_SAMPLE_RATE_TX);
1442 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301443 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1444 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001445 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301446
Zhou Song12c29502019-03-16 10:37:18 +08001447 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1448 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1449 return -ENOSYS;
1450 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301451 }
1452 } else {
1453
Surendar Karka2febd452018-12-13 17:56:43 +05301454 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301455 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301456 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301457 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1458 return -ENOSYS;
1459 }
1460
Surendar Karka2febd452018-12-13 17:56:43 +05301461 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301462 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1463 return -ENOSYS;
1464 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001465 }
1466
1467 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301468 if (direction == SINK) {
1469 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1470 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1471 MIXER_AFE_SINK_CHANNELS);
1472 } else {
1473 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1474 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1475 MIXER_AFE_IN_CHANNELS);
1476 }
1477 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001478 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1479 return -ENOSYS;
1480 }
Surendar Karka2febd452018-12-13 17:56:43 +05301481 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001482 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1483 return -ENOSYS;
1484 }
1485
1486 return 0;
1487}
1488
1489/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301490static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001491{
1492 struct mixer_ctl *ctl_dec_data = NULL;
1493 struct abr_dec_cfg_t dec_cfg;
1494 int ret = 0;
1495
1496 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301497 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001498 if (!ctl_dec_data) {
1499 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1500 return false;
1501 }
1502 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1503 dec_cfg.dec_format = dec_format;
1504 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1505 dec_cfg.imc_info.enable = IMC_ENABLE;
1506 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1507 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1508
1509 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1510 sizeof(dec_cfg));
1511 if (ret != 0) {
1512 ALOGE("%s: Failed to set decoder config", __func__);
1513 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301514 }
Surendar Karka2febd452018-12-13 17:56:43 +05301515 }
1516
1517 return true;
1518}
1519
Surendar Karkabbb3c822018-11-12 13:00:38 +05301520bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001521{
1522 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1523 struct sbc_dec_cfg_t sbc_dsp_cfg;
1524 bool is_configured = false;
1525 int ret = 0;
1526
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001527 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001528 goto fail;
1529
Surendar Karka2febd452018-12-13 17:56:43 +05301530 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001531 if (!ctl_dec_data) {
1532 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1533 is_configured = false;
1534 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301535 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001536
Florian Pfister1a84f312018-07-19 14:38:18 +02001537 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1538 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1539 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1540 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1541 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1542 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301543
Florian Pfister1a84f312018-07-19 14:38:18 +02001544 if (ret != 0) {
1545 ALOGE("%s: failed to set SBC decoder config", __func__);
1546 is_configured = false;
1547 goto fail;
1548 }
1549
1550 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1551 MIXER_DEC_BIT_FORMAT);
1552 if (!ctrl_bit_format) {
1553 ALOGE(" ERROR Dec bit format mixer control not identified");
1554 is_configured = false;
1555 goto fail;
1556 }
1557 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1558 if (ret != 0) {
1559 ALOGE("%s: Failed to set bit format to decoder", __func__);
1560 is_configured = false;
1561 goto fail;
1562 }
1563
1564 is_configured = true;
1565 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1566 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1567 a2dp.dec_channels = 1;
1568 else
1569 a2dp.dec_channels = 2;
1570 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1571 ALOGV("Successfully updated SBC dec format");
1572fail:
1573 return is_configured;
1574}
1575
Florian Pfister1a84f312018-07-19 14:38:18 +02001576/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301577static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301578{
Florian Pfister1a84f312018-07-19 14:38:18 +02001579 void *codec_info = NULL;
1580 codec_t codec_type = CODEC_TYPE_INVALID;
1581 bool is_configured = false;
1582 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301583
Florian Pfister1a84f312018-07-19 14:38:18 +02001584 if (!a2dp.audio_get_dec_config) {
1585 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1586 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301587 }
1588
Surendar Karka2febd452018-12-13 17:56:43 +05301589 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001590 if (!ctl_dec_data) {
1591 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1592 is_configured = false;
1593 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301594 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001595 codec_info = a2dp.audio_get_dec_config(&codec_type);
1596 switch(codec_type) {
1597 case CODEC_TYPE_SBC:
1598 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301599 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001600 break;
1601 case CODEC_TYPE_AAC:
1602 ALOGD(" AAC decoder supported BT device");
1603 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301604 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001605 break;
1606 default:
1607 ALOGD(" Received Unsupported decoder format");
1608 is_configured = false;
1609 break;
1610 }
1611 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301612}
1613
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301614/* API to configure SBC DSP encoder */
1615bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1616{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301617 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301618 struct sbc_enc_cfg_t sbc_dsp_cfg;
1619 bool is_configured = false;
1620 int ret = 0;
1621
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001622 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301623 return false;
1624
1625 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1626 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001627 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301628 is_configured = false;
1629 goto fail;
1630 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301631 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001632 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301633 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1634 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1635 switch(sbc_bt_cfg->channels) {
1636 case 0:
1637 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1638 break;
1639 case 1:
1640 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1641 break;
1642 case 3:
1643 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1644 break;
1645 case 2:
1646 default:
1647 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1648 break;
1649 }
1650 if (sbc_bt_cfg->alloc)
1651 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1652 else
1653 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1654 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1655 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1656 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1657 sizeof(struct sbc_enc_cfg_t));
1658 if (ret != 0) {
1659 ALOGE("%s: failed to set SBC encoder config", __func__);
1660 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301661 goto fail;
1662 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301663 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301664 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301665 is_configured = false;
1666 goto fail;
1667 }
1668 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001669 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301670 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301671
1672 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1673 a2dp.enc_channels = 1;
1674 else
1675 a2dp.enc_channels = 2;
1676
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301677 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1678 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301679fail:
1680 return is_configured;
1681}
1682
Manish Dewangan6a252632017-12-04 17:27:44 +05301683#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301684static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1685 audio_aptx_encoder_config *aptx_bt_cfg)
1686{
1687 int ret = 0;
1688
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001689 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301690 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1691 aptx_dsp_cfg, aptx_bt_cfg);
1692 return -EINVAL;
1693 }
1694
1695 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301696 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301697
1698
1699 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1700 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1701 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1702 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1703 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1704 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1705 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1706 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1707 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301708 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301709 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1710 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1711 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1712 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1713
1714
1715 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1716 case APTX_AD_CHANNEL_UNCHANGED:
1717 case APTX_AD_CHANNEL_JOINT_STEREO:
1718 case APTX_AD_CHANNEL_DUAL_MONO:
1719 case APTX_AD_CHANNEL_STEREO_TWS:
1720 case APTX_AD_CHANNEL_EARBUD:
1721 default:
1722 a2dp.enc_channels = CH_STEREO;
1723 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1724 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1725 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1726 break;
1727 case APTX_AD_CHANNEL_MONO:
1728 a2dp.enc_channels = CH_MONO;
1729 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1730 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001731 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301732 }
1733 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1734 case APTX_AD_SR_UNCHANGED:
1735 case APTX_AD_48:
1736 default:
1737 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1738 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1739 break;
1740 case APTX_AD_44_1:
1741 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1742 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001743 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301744 }
1745 ALOGV("Successfully updated APTX AD enc format with \
1746 samplingrate: %d channels:%d",
1747 aptx_dsp_cfg->custom_cfg.sample_rate,
1748 aptx_dsp_cfg->custom_cfg.num_channels);
1749
1750 return ret;
1751}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301752
Zhou Song8fccbb62019-03-20 01:08:19 +08001753static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
1754 audio_aptx_encoder_config *aptx_bt_cfg)
1755{
1756 int ret = 0;
1757
1758 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Mingshu Pangaa429f72019-06-10 17:25:00 +08001759 ALOGE("Invalid param, aptx_dsp_cfg %pK aptx_bt_cfg %pK",
Zhou Song8fccbb62019-03-20 01:08:19 +08001760 aptx_dsp_cfg, aptx_bt_cfg);
1761 return -EINVAL;
1762 }
1763
1764 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
1765 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
1766
1767
1768 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1769 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1770 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1771 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1772 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1773 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1774 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1775 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1776 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
1777 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
1778 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
1779 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
1780 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
1781 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
1782 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1783 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1784 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1785 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1786
1787
1788 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1789 case APTX_AD_CHANNEL_UNCHANGED:
1790 case APTX_AD_CHANNEL_JOINT_STEREO:
1791 case APTX_AD_CHANNEL_DUAL_MONO:
1792 case APTX_AD_CHANNEL_STEREO_TWS:
1793 case APTX_AD_CHANNEL_EARBUD:
1794 default:
1795 a2dp.enc_channels = CH_STEREO;
1796 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1797 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1798 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1799 break;
1800 case APTX_AD_CHANNEL_MONO:
1801 a2dp.enc_channels = CH_MONO;
1802 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1803 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001804 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001805 }
1806 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1807 case APTX_AD_SR_UNCHANGED:
1808 case APTX_AD_48:
1809 default:
1810 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1811 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1812 break;
1813 case APTX_AD_44_1:
1814 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1815 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001816 break;
1817 case APTX_AD_96:
1818 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
1819 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_96K;
1820 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001821 }
1822 ALOGV("Successfully updated APTX AD enc format with \
1823 samplingrate: %d channels:%d",
1824 aptx_dsp_cfg->custom_cfg.sample_rate,
1825 aptx_dsp_cfg->custom_cfg.num_channels);
1826
1827 return ret;
1828}
1829
Manisha Agarwala51768b2018-11-01 16:30:52 +05301830static void audio_a2dp_update_tws_channel_mode()
1831{
1832 char* channel_mode;
1833 struct mixer_ctl *ctl_channel_mode;
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301834
1835 ALOGD("Update tws for mono_mode on=%d",a2dp.is_tws_mono_mode_on);
1836
Manisha Agarwala51768b2018-11-01 16:30:52 +05301837 if (a2dp.is_tws_mono_mode_on)
1838 channel_mode = "One";
1839 else
1840 channel_mode = "Two";
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301841
Manisha Agarwala51768b2018-11-01 16:30:52 +05301842 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1843 if (!ctl_channel_mode) {
1844 ALOGE("failed to get tws mixer ctl");
1845 return;
1846 }
1847 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1848 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1849 return;
1850 }
1851}
1852
Manish Dewangan6a252632017-12-04 17:27:44 +05301853static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
1854 audio_aptx_encoder_config *aptx_bt_cfg)
1855{
1856 int ret = 0;
1857
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001858 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301859 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1860 aptx_dsp_cfg, aptx_bt_cfg);
1861 return -EINVAL;
1862 }
1863
1864 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001865 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301866
1867 if (!a2dp.is_aptx_dual_mono_supported) {
1868 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1869 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1870 } else {
1871 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1872 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1873 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1874 }
1875
1876 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
1877 case 1:
1878 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1879 break;
1880 case 2:
1881 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05301882 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1883 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
Manish Dewangan6a252632017-12-04 17:27:44 +05301884 break;
1885 }
1886 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
1887 if (!a2dp.is_aptx_dual_mono_supported) {
1888 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1889 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
1890 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
1891 aptx_dsp_cfg->custom_cfg.num_channels);
1892 } else {
1893 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1894 ALOGV("Successfully updated APTX dual mono enc format with \
1895 samplingrate: %d channels:%d syncmode %d",
1896 aptx_dsp_cfg->custom_cfg.sample_rate,
1897 aptx_dsp_cfg->custom_cfg.num_channels,
1898 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
1899 }
1900 return ret;
1901}
1902#else
1903static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
1904 audio_aptx_encoder_config *aptx_bt_cfg)
1905{
1906 int ret = 0;
1907
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001908 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301909 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1910 aptx_dsp_cfg, aptx_bt_cfg);
1911 return -EINVAL;
1912 }
1913
1914 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001915 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301916 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
1917 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
1918 switch(aptx_dsp_cfg->num_channels) {
1919 case 1:
1920 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
1921 break;
1922 case 2:
1923 default:
1924 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
1925 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
1926 break;
1927 }
1928
1929 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
1930 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
1931
1932 return ret;
1933}
1934#endif
1935
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301936/* API to configure APTX DSP encoder */
1937bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1938{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301939 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08001940 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001941 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301942 bool is_configured = false;
1943 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08001944 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301945
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001946 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301947 return false;
1948
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001949 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1950 if (!ctl_enc_data) {
1951 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1952 return false;
1953 }
1954
Manish Dewangan6a252632017-12-04 17:27:44 +05301955#ifndef LINUX_ENABLED
1956 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301957 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08001958 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001959 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001960 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
1961 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001962 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08001963 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001964 else
Zhou Song8fccbb62019-03-20 01:08:19 +08001965 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001966 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001967 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001968
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001969 if (ret) {
1970 is_configured = false;
1971 goto fail;
1972 }
1973
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001974 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001975 if (aptx_ad_ctl)
1976 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001977 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08001978 else
1979 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001980 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001981 } else {
1982 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001983 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05301984 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301985#else
1986 struct custom_enc_cfg_t aptx_dsp_cfg;
1987 mixer_size = sizeof(struct custom_enc_cfg_t);
1988 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05301989 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05301990 if (ret) {
1991 is_configured = false;
1992 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301993 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001994 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1995 mixer_size);
1996#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301997 if (ret != 0) {
1998 ALOGE("%s: Failed to set APTX encoder config", __func__);
1999 is_configured = false;
2000 goto fail;
2001 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302002#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002003 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302004 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002005 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302006 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
2007 else
2008 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302009#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302010 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302011 is_configured = false;
2012 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302013 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302014 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302015 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05302016 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302017 else
Surendar Karka2febd452018-12-13 17:56:43 +05302018 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302019fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05302020 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002021 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05302022 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302023 return is_configured;
2024}
2025
2026/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302027 */
Manish Dewangan6a252632017-12-04 17:27:44 +05302028#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002029bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302030#else
2031bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2032#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302033{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302034 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302035 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302036 bool is_configured = false;
2037 int ret = 0;
2038
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002039 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302040 return false;
2041
2042 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2043 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002044 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302045 is_configured = false;
2046 goto fail;
2047 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302048
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302049 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002050 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302051 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2052 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2053 switch(aptx_dsp_cfg.num_channels) {
2054 case 1:
2055 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2056 break;
2057 case 2:
2058 default:
2059 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2060 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2061 break;
2062 }
2063 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302064 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302065 if (ret != 0) {
2066 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2067 is_configured = false;
2068 goto fail;
2069 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302070 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302071 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302072 is_configured = false;
2073 goto fail;
2074 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302075 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002076 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302077 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302078 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302079 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2080 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302081fail:
2082 return is_configured;
2083}
2084
2085/* API to configure AAC DSP encoder */
2086bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2087{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302088 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302089 struct aac_enc_cfg_t aac_dsp_cfg;
2090 bool is_configured = false;
2091 int ret = 0;
2092
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002093 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302094 return false;
2095
2096 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2097 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002098 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302099 is_configured = false;
2100 goto fail;
2101 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302102 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002103 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302104 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302105 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002106 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302107 case 0:
2108 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2109 break;
2110 case 2:
2111 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2112 break;
2113 case 1:
2114 default:
2115 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2116 break;
2117 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302118 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302119 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002120
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302121 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2122 sizeof(struct aac_enc_cfg_t));
2123 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002124 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302125 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302126 goto fail;
2127 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302128 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302129 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302130 is_configured = false;
2131 goto fail;
2132 }
2133 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002134 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302135 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002136 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002137 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2138 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2139fail:
2140 return is_configured;
2141}
2142
2143bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2144{
2145 struct mixer_ctl *ctl_enc_data = NULL;
2146 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2147 bool is_configured = false;
2148 int ret = 0;
2149
2150 if (aac_bt_cfg == NULL)
2151 return false;
2152
2153 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2154 if (!ctl_enc_data) {
2155 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2156 is_configured = false;
2157 goto fail;
2158 }
2159 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302160 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002161 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2162 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2163 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2164 case 0:
2165 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2166 break;
2167 case 2:
2168 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2169 break;
2170 case 1:
2171 default:
2172 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2173 break;
2174 }
2175 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2176 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2177 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2178 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2179
2180 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2181 sizeof(struct aac_enc_cfg_v2_t));
2182 if (ret != 0) {
2183 ALOGE("%s: Failed to set AAC encoder config", __func__);
2184 is_configured = false;
2185 goto fail;
2186 }
2187 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2188 if (ret != 0) {
2189 is_configured = false;
2190 goto fail;
2191 }
2192 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302193 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002194 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2195 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2196 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2197 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302198fail:
2199 return is_configured;
2200}
2201
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302202bool configure_aac_enc_format_v3(audio_aac_encoder_config_v3 *aac_bt_cfg)
2203{
2204 struct mixer_ctl *ctl_enc_data = NULL;
2205 struct aac_enc_cfg_v3_t aac_dsp_cfg;
2206 struct aac_frame_size_control_t* frame_vbr_ctl = NULL;
2207 bool is_configured = false;
2208 int ret = 0;
2209
2210 if (aac_bt_cfg == NULL)
2211 return false;
2212
2213 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2214 if (!ctl_enc_data) {
2215 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2216 is_configured = false;
2217 goto fail;
2218 }
2219 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v3_t));
2220 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
2221 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2222 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2223 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2224 case 0:
2225 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2226 break;
2227 case 2:
2228 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2229 break;
2230 case 1:
2231 default:
2232 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2233 break;
2234 }
2235 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2236 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2237 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2238 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2239 frame_vbr_ctl = aac_bt_cfg->frame_ptr_ctl;
2240
2241 if (frame_vbr_ctl != NULL) {
2242 aac_dsp_cfg.aac_key_value_ctl.ctl_type = frame_vbr_ctl->ctl_type;
2243 aac_dsp_cfg.aac_key_value_ctl.ctl_value = frame_vbr_ctl->ctl_value;
2244 } else {
2245 ALOGE("%s: VBR cannot be enabled, fall back to default",__func__);
2246 aac_dsp_cfg.aac_key_value_ctl.ctl_type = 0;
2247 aac_dsp_cfg.aac_key_value_ctl.ctl_value = 0;
2248 }
2249
2250 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2251 sizeof(struct aac_enc_cfg_v3_t));
2252 if (ret != 0) {
2253 ALOGE("%s: Failed to set AAC encoder config", __func__);
2254 is_configured = false;
2255 goto fail;
2256 }
2257 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2258 if (ret != 0) {
2259 is_configured = false;
2260 goto fail;
2261 }
2262 is_configured = true;
2263 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
2264 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2265 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2266 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2267 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
2268fail:
2269 return is_configured;
2270}
2271
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302272bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2273{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302274 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302275 struct celt_enc_cfg_t celt_dsp_cfg;
2276 bool is_configured = false;
2277 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002278 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302279 return false;
2280
2281 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2282 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002283 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302284 is_configured = false;
2285 goto fail;
2286 }
2287 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2288
Florian Pfister1a84f312018-07-19 14:38:18 +02002289 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302290 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2291 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2292 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2293 case 1:
2294 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2295 break;
2296 case 2:
2297 default:
2298 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2299 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2300 break;
2301 }
2302
2303 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2304
2305 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2306 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2307 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2308 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2309 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2310
2311 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2312 sizeof(struct celt_enc_cfg_t));
2313 if (ret != 0) {
2314 ALOGE("%s: Failed to set CELT encoder config", __func__);
2315 is_configured = false;
2316 goto fail;
2317 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302318 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302319 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302320 is_configured = false;
2321 goto fail;
2322 }
2323 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002324 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302325 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302326 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302327 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2328 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2329fail:
2330 return is_configured;
2331}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302332
2333bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2334{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302335 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302336 struct ldac_enc_cfg_t ldac_dsp_cfg;
2337 bool is_configured = false;
2338 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002339 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302340 return false;
2341
2342 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2343 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002344 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302345 is_configured = false;
2346 goto fail;
2347 }
2348 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2349
Florian Pfister1a84f312018-07-19 14:38:18 +02002350 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302351 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2352 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2353 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2354 case 4:
2355 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2356 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2357 break;
2358 case 2:
2359 case 1:
2360 default:
2361 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2362 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2363 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2364 break;
2365 }
2366
2367 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2368 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2369 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002370 if (ldac_bt_cfg->is_abr_enabled) {
2371 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2372 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2373 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2374 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2375 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002376 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002377 }
2378
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302379 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2380 sizeof(struct ldac_enc_cfg_t));
2381 if (ret != 0) {
2382 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2383 is_configured = false;
2384 goto fail;
2385 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302386 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302387 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302388 is_configured = false;
2389 goto fail;
2390 }
2391 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002392 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302393 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2394 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002395 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302396 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2397 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2398fail:
2399 return is_configured;
2400}
2401
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302402bool configure_a2dp_encoder_format()
2403{
2404 void *codec_info = NULL;
2405 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002406 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302407 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002408 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302409
Florian Pfister1a84f312018-07-19 14:38:18 +02002410 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302411 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2412 return false;
2413 }
2414 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002415 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302416 &codec_type);
2417
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002418 // ABR disabled by default for all codecs
2419 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302420 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002421
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302422 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002423 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302424 ALOGD(" Received SBC encoder supported BT device");
2425 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002426 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302427 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002428 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302429 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302430#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002431 a2dp.is_aptx_dual_mono_supported = false;
2432 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302433#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302434 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002435 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302436 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002437 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302438 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302439#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302440 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002441 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302442#else
2443 is_configured =
2444 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2445#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002446 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302447#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002448 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002449 ALOGD(" Received APTX dual mono encoder supported BT device");
2450 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302451 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2452 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002453 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2454 is_configured =
2455 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302456 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302457#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002458 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302459 ALOGD(" Received AAC encoder supported BT device");
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302460 bool is_aac_vbr_enabled =
2461 property_get_bool("persist.vendor.bt.aac_vbr_frm_ctl.enabled", false);
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002462 bool is_aac_frame_ctl_enabled =
2463 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302464 if (is_aac_vbr_enabled)
2465 is_configured = configure_aac_enc_format_v3((audio_aac_encoder_config_v3 *) codec_info);
2466 else
2467 is_configured = is_aac_frame_ctl_enabled ?
2468 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2469 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302470 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002471 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302472 ALOGD(" Received CELT encoder supported BT device");
2473 is_configured =
2474 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2475 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002476 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302477 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002478 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2479 instance_id = MAX_INSTANCE_ID;
2480 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302481 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002482 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302483 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302484 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302485#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302486 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302487 ALOGD(" Received APTX AD encoder supported BT device");
2488 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2489 instance_id = MAX_INSTANCE_ID;
2490 a2dp.abr_config.imc_instance = instance_id--;
2491 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2492 a2dp.is_aptx_adaptive = true;
2493 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2494 is_configured =
2495 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302496 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302497 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302498#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002499 case CODEC_TYPE_PCM:
2500 ALOGD("Received PCM format for BT device");
2501 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2502 is_configured = true;
2503 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302504 default:
2505 ALOGD(" Received Unsupported encoder formar");
2506 is_configured = false;
2507 break;
2508 }
2509 return is_configured;
2510}
2511
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002512int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302513{
2514 int ret = 0;
2515
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002516 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302517
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002518 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002519 && a2dp.audio_get_enc_config)) {
2520 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302521 return -ENOSYS;
2522 }
2523
Zhou Song12c29502019-03-16 10:37:18 +08002524 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302525 //session will be restarted after suspend completion
2526 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302527 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302528 }
2529
Florian Pfister1a84f312018-07-19 14:38:18 +02002530 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302531 ALOGD("calling BT module stream start");
2532 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002533 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302534 ALOGE("BT controller start return = %d",ret);
2535 if (ret != 0 ) {
2536 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002537 a2dp.a2dp_source_started = false;
Revathi Uddarajub26e3932020-06-10 14:51:02 +05302538 ret = -ETIMEDOUT;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302539 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002540 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002541 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302542 ret = 0;
2543 ALOGD("Start playback successful to BT library");
2544 } else {
2545 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002546 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302547 ret = -ETIMEDOUT;
2548 }
2549 }
2550 }
2551
Florian Pfister1a84f312018-07-19 14:38:18 +02002552 if (a2dp.a2dp_source_started) {
2553 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302554 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302555 audio_a2dp_update_tws_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002556 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002557 if (a2dp.abr_config.is_abr_enabled)
2558 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302559 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302560
2561 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002562 a2dp.a2dp_source_total_active_session_requests);
2563 return ret;
2564}
2565
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002566uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002567{
2568 uint32_t latency = 0;
2569
2570 switch(a2dp.bt_decoder_format) {
2571 case CODEC_TYPE_SBC:
2572 latency = DEFAULT_SINK_LATENCY_SBC;
2573 break;
2574 case CODEC_TYPE_AAC:
2575 latency = DEFAULT_SINK_LATENCY_AAC;
2576 break;
2577 default:
2578 latency = 200;
2579 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2580 break;
2581 }
2582 return (uint64_t)latency;
2583}
2584
2585bool a2dp_send_sink_setup_complete(void) {
2586 uint64_t system_latency = 0;
2587 bool is_complete = false;
2588
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002589 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002590
2591 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2592 is_complete = true;
2593 }
2594 return is_complete;
2595}
2596
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002597bool a2dp_sink_is_ready()
2598{
2599 bool ret = false;
2600
2601 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2602 (a2dp.is_a2dp_offload_supported) &&
2603 (a2dp.audio_sink_check_a2dp_ready))
2604 ret = a2dp.audio_sink_check_a2dp_ready();
2605 return ret;
2606}
2607
2608int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002609{
2610 int ret = 0;
2611
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002612 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002613
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002614 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002615 && a2dp.audio_get_dec_config)) {
2616 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2617 return -ENOSYS;
2618 }
2619
2620 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2621 ALOGD("calling BT module stream start");
2622 /* This call indicates BT IPC lib to start capture */
2623 ret = a2dp.audio_sink_start();
2624 ALOGE("BT controller start capture return = %d",ret);
2625 if (ret != 0 ) {
2626 ALOGE("BT controller start capture failed");
2627 a2dp.a2dp_sink_started = false;
2628 } else {
2629
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002630 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002631 ALOGD("Wait for capture ready not successful");
2632 ret = -ETIMEDOUT;
2633 }
2634
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002635 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002636 a2dp.a2dp_sink_started = true;
2637 ret = 0;
2638 ALOGD("Start capture successful to BT library");
2639 } else {
2640 ALOGD(" unable to configure DSP decoder");
2641 a2dp.a2dp_sink_started = false;
2642 ret = -ETIMEDOUT;
2643 }
2644
2645 if (!a2dp_send_sink_setup_complete()) {
2646 ALOGD("sink_setup_complete not successful");
2647 ret = -ETIMEDOUT;
2648 }
2649 }
2650 }
2651
2652 if (a2dp.a2dp_sink_started) {
2653 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002654 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002655 }
2656 }
2657
2658 ALOGD("start A2DP sink total active sessions :%d",
2659 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302660 return ret;
2661}
2662
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302663static void reset_a2dp_enc_config_params()
2664{
2665 int ret =0;
2666
Aalique Grahame22e49102018-12-18 14:23:57 -08002667 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302668 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302669 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302670
2671 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
2672 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
2673 MIXER_ENC_CONFIG_BLOCK);
2674 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002675 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302676 } else {
2677 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
2678 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002679 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302680 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002681
2682 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2683
Manisha Agarwala51768b2018-11-01 16:30:52 +05302684 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2685
2686 if (!ctl_channel_mode) {
2687 ALOGE("failed to get tws mixer ctl");
2688 } else {
2689 channel_mode = "Two";
2690 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2691 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2692 }
2693 a2dp.is_tws_mono_mode_on = false;
2694 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302695}
2696
Surendar Karka2febd452018-12-13 17:56:43 +05302697static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002698{
2699 struct mixer_ctl *ctl_dec_data = NULL;
2700 struct abr_dec_cfg_t dummy_reset_cfg;
2701 int ret = 0;
2702
Preetam Singh Ranawat25780e42019-07-31 18:15:57 +05302703 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
2704 if (!ctl_dec_data) {
2705 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
2706 return -EINVAL;
2707 }
2708 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
2709 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
2710 sizeof(dummy_reset_cfg));
2711 if (ret != 0) {
2712 ALOGE("%s: Failed to set dummy decoder config", __func__);
2713 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002714 }
2715
2716 return ret;
2717}
2718
Surendar Karka2febd452018-12-13 17:56:43 +05302719static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02002720{
2721 int ret =0;
2722
2723 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
2724 struct aac_dec_cfg_t dummy_reset_config;
2725
2726 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
2727 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05302728 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02002729 if (!ctl_dec_config) {
2730 ALOGE(" ERROR a2dp decoder format mixer control not identified");
2731 } else {
2732 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
2733 sizeof(struct aac_dec_cfg_t));
2734 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
2735 }
2736 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
2737 MIXER_DEC_BIT_FORMAT);
2738 if (!ctrl_bit_format) {
2739 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
2740 } else {
2741 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
2742 if (ret != 0) {
2743 ALOGE("%s: Failed to set bit format to decoder", __func__);
2744 }
2745 }
2746}
2747
Zhou Song12c29502019-03-16 10:37:18 +08002748static void reset_codec_config()
2749{
2750 reset_a2dp_enc_config_params();
2751 reset_a2dp_source_dec_config_params();
2752 a2dp_reset_backend_cfg(SOURCE);
2753 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
2754 stop_abr();
2755 a2dp.abr_config.is_abr_enabled = false;
2756}
2757
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002758int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302759{
2760 int ret =0;
2761
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002762 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002763 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002764 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302765 return -ENOSYS;
2766 }
2767
Florian Pfister1a84f312018-07-19 14:38:18 +02002768 if (a2dp.a2dp_source_total_active_session_requests > 0)
2769 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08002770 else
2771 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302772
Florian Pfister1a84f312018-07-19 14:38:18 +02002773 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302774 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02002775 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302776 if (ret < 0)
2777 ALOGE("stop stream to BT IPC lib failed");
2778 else
2779 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08002780 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
2781 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05302782 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302783 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002784 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002785 a2dp.a2dp_source_started = false;
2786 ALOGD("Stop A2DP playback, total active sessions :%d",
2787 a2dp.a2dp_source_total_active_session_requests);
2788 return 0;
2789}
2790
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002791int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002792{
2793 int ret =0;
2794
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002795 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002796 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002797 ALOGE("a2dp handle is not identified, Ignoring stop request");
2798 return -ENOSYS;
2799 }
2800
2801 if (a2dp.a2dp_sink_total_active_session_requests > 0)
2802 a2dp.a2dp_sink_total_active_session_requests--;
2803
2804 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2805 ALOGV("calling BT module stream stop");
2806 ret = a2dp.audio_sink_stop();
2807 if (ret < 0)
2808 ALOGE("stop stream to BT IPC lib failed");
2809 else
2810 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05302811 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002812 a2dp_reset_backend_cfg(SINK);
2813 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002814 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002815 a2dp.a2dp_source_started = false;
2816 ALOGD("Stop A2DP capture, total active sessions :%d",
2817 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302818 return 0;
2819}
2820
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002821int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302822{
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002823 int ret = 0, val, status = 0;
2824 char value[32] = {0};
2825 struct audio_usecase *uc_info;
2826 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302827
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002828 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002829 ALOGV("no supported encoders identified,ignoring a2dp setparam");
2830 status = -EINVAL;
2831 goto param_handled;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002832 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302833
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002834 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302835 sizeof(value));
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002836 if (ret >= 0) {
2837 val = atoi(value);
2838 if (audio_is_a2dp_out_device(val)) {
2839 ALOGV("Received device connect request for A2DP source");
2840 open_a2dp_source();
2841 }
2842 goto param_handled;
2843 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302844
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002845 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
2846 sizeof(value));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302847
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002848 if (ret >= 0) {
2849 val = atoi(value);
2850 if (audio_is_a2dp_out_device(val)) {
2851 ALOGV("Received source device dis- connect request");
2852 close_a2dp_output();
2853 reset_a2dp_enc_config_params();
2854 reset_a2dp_source_dec_config_params();
2855 a2dp_reset_backend_cfg(SOURCE);
2856 } else if (audio_is_a2dp_in_device(val)) {
2857 ALOGV("Received sink device dis- connect request");
2858 close_a2dp_input();
2859 reset_a2dp_sink_dec_config_params();
2860 a2dp_reset_backend_cfg(SINK);
2861 }
2862 goto param_handled;
2863 }
Surendar Karka2febd452018-12-13 17:56:43 +05302864#ifndef LINUX_ENABLED
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002865 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
2866 if (ret >= 0) {
2867 ALOGD("Setting tws channel mode to %s",value);
2868 if (!(strncmp(value, "mono", strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302869 a2dp.is_tws_mono_mode_on = true;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002870 else if (!(strncmp(value, "dual-mono", strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302871 a2dp.is_tws_mono_mode_on = false;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002872 audio_a2dp_update_tws_channel_mode();
2873 goto param_handled;
2874 }
Surendar Karka2febd452018-12-13 17:56:43 +05302875#endif
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002876 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
2877 if (ret >= 0) {
2878 if (a2dp.bt_lib_source_handle == NULL)
2879 goto param_handled;
2880
2881 if ((!strncmp(value, "true", sizeof(value)))) {
2882 if (a2dp.a2dp_source_suspended) {
2883 ALOGD("%s: A2DP is already suspended", __func__);
2884 goto param_handled;
2885 }
2886 ALOGD("Setting a2dp to suspend state");
2887 a2dp.a2dp_source_suspended = true;
2888 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
2889 goto param_handled;
2890 list_for_each(node, &a2dp.adev->usecase_list) {
2891 uc_info = node_to_item(node, struct audio_usecase, list);
2892 if (uc_info->type == PCM_PLAYBACK &&
2893 (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
2894 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
2895 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)) {
2896 fp_check_a2dp_restore_l(a2dp.adev, uc_info->stream.out, false);
Aalique Grahame22e49102018-12-18 14:23:57 -08002897 }
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002898 }
2899 if (!a2dp.swb_configured)
2900 reset_codec_config();
2901 if (a2dp.audio_source_suspend)
2902 a2dp.audio_source_suspend();
2903 } else if (a2dp.a2dp_source_suspended == true) {
2904 ALOGD("Resetting a2dp suspend state");
2905 struct audio_usecase *uc_info;
2906 struct listnode *node;
2907 if (a2dp.clear_source_a2dpsuspend_flag)
2908 a2dp.clear_source_a2dpsuspend_flag();
2909 a2dp.a2dp_source_suspended = false;
2910 /*
2911 * It is possible that before suspend,a2dp sessions can be active
2912 * for example during music + voice activation concurrency
2913 * a2dp suspend will be called & BT will change to sco mode
2914 * though music is paused as a part of voice activation
2915 * compress session close happens only after pause timeout(10secs)
2916 * so if resume request comes before pause timeout as a2dp session
2917 * is already active IPC start will not be called from APM/audio_hw
2918 * Fix is to call a2dp start for IPC library post suspend
2919 * based on number of active session count
2920 */
2921 if (a2dp.a2dp_source_total_active_session_requests > 0) {
2922 ALOGD(" Calling IPC lib start post suspend state");
2923 if (a2dp.audio_source_start) {
2924 ret = a2dp.audio_source_start();
2925 if (ret != 0) {
2926 ALOGE("BT controller start failed");
2927 a2dp.a2dp_source_started = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302928 }
2929 }
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002930 }
2931 list_for_each(node, &a2dp.adev->usecase_list) {
2932 uc_info = node_to_item(node, struct audio_usecase, list);
2933 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
2934 is_a2dp_out_device_type(&uc_info->stream.out->device_list)) {
2935 fp_check_a2dp_restore_l(a2dp.adev, uc_info->stream.out, true);
Zhou Song10617ed2017-05-26 13:28:48 +08002936 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302937 }
2938 }
2939 goto param_handled;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002940 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002941
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002942 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
2943 sizeof(value));
2944 if (ret >= 0) {
2945 if (a2dp.is_a2dp_offload_supported &&
2946 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2947 *reconfig = true;
2948 }
2949 goto param_handled;
2950 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002951
Lakshman Chaluvaraju6cbe1362021-06-18 14:32:41 +05302952 ret = str_parms_get_str(parms, "BT_SCO", value, sizeof(value));
2953 if (ret >= 0) {
2954 if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) {
2955 a2dp.a2dp_source_suspended = true;
2956 }
2957 }
2958
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302959param_handled:
Weiyin Jiangd5974e62020-09-08 20:28:22 +08002960 ALOGV("end of a2dp setparam");
2961 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302962}
2963
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002964void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302965{
2966 a2dp.is_handoff_in_progress = is_on;
2967}
2968
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002969bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302970{
2971 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05302972 // Or if a2dp device is selected but earlier start failed ( as a2dp
2973 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02002974 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302975}
2976
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002977void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302978{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302979 *sample_rate = a2dp.enc_sampling_rate;
2980}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302981
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002982void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02002983{
2984 *sample_rate = a2dp.dec_sampling_rate;
2985}
2986
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002987bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302988{
2989 bool ret = false;
2990
Florian Pfister1a84f312018-07-19 14:38:18 +02002991 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302992 return ret;
2993
Florian Pfister1a84f312018-07-19 14:38:18 +02002994 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08002995 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002996 (a2dp.audio_source_check_a2dp_ready))
2997 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302998 return ret;
2999}
3000
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003001bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02003002{
3003 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05303004}
3005
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003006void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08003007 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303008{
3009 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02003010 a2dp.bt_lib_source_handle = NULL;
3011 a2dp.a2dp_source_started = false;
3012 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
3013 a2dp.a2dp_source_total_active_session_requests = 0;
3014 a2dp.a2dp_source_suspended = false;
3015 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303016 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303017 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07003018 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05303019 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08003020 a2dp.abr_config.is_abr_enabled = false;
3021 a2dp.abr_config.abr_started = false;
3022 a2dp.abr_config.imc_instance = 0;
3023 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08003024 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05303025 a2dp.is_tws_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05303026 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08003027 a2dp.swb_configured = false;
3028
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003029 // init function pointers
3030 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08003031 init_config.fp_platform_get_pcm_device_id;
Weiyin Jiangd5974e62020-09-08 20:28:22 +08003032 fp_check_a2dp_restore_l = init_config.fp_check_a2dp_restore_l;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003033
kunleiz5a127262017-09-08 14:47:48 +08003034 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05303035 reset_a2dp_source_dec_config_params();
3036 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02003037
3038 a2dp.bt_lib_sink_handle = NULL;
3039 a2dp.a2dp_sink_started = false;
3040 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
3041 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003042
3043 if (is_running_with_enhanced_fwk == UNINITIALIZED)
3044 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
3045 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08003046 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02003047
3048 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303049 update_offload_codec_capabilities();
3050}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003051
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003052uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003053{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003054 uint32_t latency = 0;
3055 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303056 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
3057 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003058 char value[PROPERTY_VALUE_MAX];
3059
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003060 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08003061 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003062 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303063 if (sscanf(value, "%d/%d/%d/%d/%d%d",
3064 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003065 ALOGI("Failed to parse avsync offset params from '%s'.", value);
3066 avsync_runtime_prop = 0;
3067 }
3068 }
3069
yidongh0515e042017-07-06 15:00:34 +08003070 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02003071 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
3072 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08003073 }
3074
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07003075 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003076 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08003077 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
3078 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003079 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003080 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08003081 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
3082 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003083 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003084 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08003085 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
3086 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003087 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003088 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08003089 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
3090 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003091 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003092 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05303093 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
3094 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
3095 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003096 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303097 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
3098 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
3099 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05303100 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05303101 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05303102 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08003103 case CODEC_TYPE_PCM:
3104 latency = ENCODER_LATENCY_PCM;
3105 latency += DEFAULT_SINK_LATENCY_PCM;
3106 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003107 default:
3108 latency = 200;
3109 break;
3110 }
3111 return latency;
3112}
Aalique Grahame22e49102018-12-18 14:23:57 -08003113
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003114int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003115 struct str_parms *reply)
3116{
3117 int ret, val = 0;
3118 char value[32]={0};
3119
3120 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3121 value, sizeof(value));
3122 if (ret >= 0) {
3123 val = a2dp.is_a2dp_offload_supported;
3124 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3125 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3126 }
3127
3128 return 0;
3129}
Zhou Song12c29502019-03-16 10:37:18 +08003130
3131
3132bool configure_aptx_ad_speech_enc_fmt() {
3133 struct mixer_ctl *ctl_enc_data = NULL;
3134 int mixer_size = 0;
3135 int ret = 0;
3136 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3137
3138 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3139 if (!ctl_enc_data) {
3140 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3141 return false;
3142 }
3143
3144 /* Initialize dsp configuration params */
3145 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3146 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3147 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3148 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3149 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3150 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3151 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3152 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3153 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3154 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3155 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3156
3157 /* Configure AFE DSP configuration */
3158 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3159 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3160 mixer_size);
3161 if (ret != 0) {
3162 ALOGE("%s: Failed to set SWB encoder config", __func__);
3163 return false;
3164 }
3165
3166 /* Configure AFE Input Bit Format as PCM_16 */
3167 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3168 if (ret != 0) {
3169 ALOGE("%s: Failed to set SWB bit format", __func__);
3170 return false;
3171 }
3172
3173 return true;
3174}
3175
3176bool configure_aptx_ad_speech_dec_fmt()
3177{
3178 struct mixer_ctl *ctl_dec_data = NULL;
3179 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3180 int ret = 0;
3181
3182 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3183 if (!ctl_dec_data) {
3184 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3185 return false;
3186 }
3187 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3188 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3189 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3190 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3191 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3192 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3193 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3194 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3195
3196 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3197 sizeof(dec_cfg));
3198 if (ret != 0) {
3199 ALOGE("%s: Failed to set decoder config", __func__);
3200 return false;
3201 }
3202 return true;
3203}
3204
3205int sco_start_configuration()
3206{
3207 ALOGD("sco_start_configuration start");
3208
3209 if (!a2dp.swb_configured) {
3210 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3211 /* Configure AFE codec*/
3212 if (configure_aptx_ad_speech_enc_fmt() &&
3213 configure_aptx_ad_speech_dec_fmt()) {
3214 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3215 } else {
3216 ALOGE("%s: failed to send SCO configuration", __func__);
3217 return -ETIMEDOUT;
3218 }
3219 /* Configure backend*/
3220 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3221 a2dp.enc_channels = CH_MONO;
3222 a2dp.abr_config.is_abr_enabled = true;
3223 a2dp_set_backend_cfg(SOURCE);
3224 /* Start abr*/
3225 start_abr();
3226 a2dp.swb_configured = true;
3227 }
3228 return 0;
3229}
3230
3231void sco_reset_configuration()
3232{
Zhou Songd6d71752019-05-21 18:08:51 +08003233 if (a2dp.swb_configured) {
3234 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003235
Zhou Songd6d71752019-05-21 18:08:51 +08003236 reset_codec_config();
3237 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3238 a2dp.swb_configured = false;
3239 }
Zhou Song12c29502019-03-16 10:37:18 +08003240}