blob: 4a37bf12b8a5516fff700e5c7c7b06c53b3ecb23 [file] [log] [blame]
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301/*
Aalique Grahame22e49102018-12-18 14:23:57 -08002* Copyright (c) 2015-2019, 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 Tanniru9d027a62015-03-13 01:32:10 +053052#define AUDIO_PARAMETER_A2DP_STARTED "A2dpStarted"
Naresh Tanniru66cf06c2019-03-20 19:30:37 +053053#define BT_IPC_SOURCE_LIB_NAME "btaudio_offload_if.so"
Florian Pfister1a84f312018-07-19 14:38:18 +020054#define BT_IPC_SINK_LIB_NAME "libbthost_if_sink.so"
55#define MEDIA_FMT_NONE 0
56#define MEDIA_FMT_AAC 0x00010DA6
57#define MEDIA_FMT_APTX 0x000131ff
58#define MEDIA_FMT_APTX_HD 0x00013200
Surendar Karka2febd452018-12-13 17:56:43 +053059#define MEDIA_FMT_APTX_AD 0x00013204
Florian Pfister1a84f312018-07-19 14:38:18 +020060#define MEDIA_FMT_SBC 0x00010BF2
61#define MEDIA_FMT_CELT 0x00013221
62#define MEDIA_FMT_LDAC 0x00013224
63#define MEDIA_FMT_MP3 0x00010BE9
64#define MEDIA_FMT_APTX_ADAPTIVE 0x00013204
Zhou Song12c29502019-03-16 10:37:18 +080065#define MEDIA_FMT_APTX_AD_SPEECH 0x00013208
Naresh Tanniru9d027a62015-03-13 01:32:10 +053066#define MEDIA_FMT_AAC_AOT_LC 2
67#define MEDIA_FMT_AAC_AOT_SBR 5
68#define MEDIA_FMT_AAC_AOT_PS 29
Naresh Tanniru9d027a62015-03-13 01:32:10 +053069#define PCM_CHANNEL_L 1
70#define PCM_CHANNEL_R 2
71#define PCM_CHANNEL_C 3
72#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
73#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
74#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
75#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
76#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
77#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
Surendar Karka2febd452018-12-13 17:56:43 +053078#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
Zhou Song8fccbb62019-03-20 01:08:19 +080079#define MIXER_ENC_APTX_AD_CONFIG_BLOCK "SLIM_7_RX APTX_AD Enc Cfg"
Surendar Karka2febd452018-12-13 17:56:43 +053080#define MIXER_SOURCE_DEC_CONFIG_BLOCK "SLIM_7_TX Decoder Config"
81#define MIXER_SINK_DEC_CONFIG_BLOCK "SLIM_9_TX Decoder Config"
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +053082#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
Florian Pfister1a84f312018-07-19 14:38:18 +020083#define MIXER_DEC_BIT_FORMAT "AFE Output Bit Format"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053084#define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080085#define MIXER_SAMPLE_RATE_RX "BT SampleRate RX"
Surendar Karka2febd452018-12-13 17:56:43 +053086#define MIXER_SOURCE_SAMPLE_RATE_TX "BT SampleRate TX"
Samyak Jainf69e9ef2018-06-12 12:26:37 +053087#define MIXER_SAMPLE_RATE_DEFAULT "BT SampleRate"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053088#define MIXER_AFE_IN_CHANNELS "AFE Input Channels"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080089#define MIXER_ABR_TX_FEEDBACK_PATH "A2DP_SLIM7_UL_HL Switch"
Zhou Song12c29502019-03-16 10:37:18 +080090#define MIXER_ABR_RX_FEEDBACK_PATH "SCO_SLIM7_DL_HL Switch"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080091#define MIXER_SET_FEEDBACK_CHANNEL "BT set feedback channel"
Surendar Karka2febd452018-12-13 17:56:43 +053092#define MIXER_SINK_SAMPLE_RATE "BT_TX SampleRate"
Florian Pfister1a84f312018-07-19 14:38:18 +020093#define MIXER_AFE_SINK_CHANNELS "AFE Output Channels"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053094#define MIXER_ENC_FMT_SBC "SBC"
95#define MIXER_ENC_FMT_AAC "AAC"
96#define MIXER_ENC_FMT_APTX "APTX"
Manisha Agarwala51768b2018-11-01 16:30:52 +053097#define MIXER_FMT_TWS_CHANNEL_MODE "TWS Channel Mode"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053098#define MIXER_ENC_FMT_APTXHD "APTXHD"
Aalique Grahame22e49102018-12-18 14:23:57 -080099#define MIXER_END_FMT_LDAC "LDAC"
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530100#define MIXER_ENC_FMT_NONE "NONE"
yidongh0515e042017-07-06 15:00:34 +0800101#define ENCODER_LATENCY_SBC 10
102#define ENCODER_LATENCY_APTX 40
103#define ENCODER_LATENCY_APTX_HD 20
104#define ENCODER_LATENCY_AAC 70
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530105//To Do: Fine Tune Encoder CELT/LDAC latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530106#define ENCODER_LATENCY_CELT 40
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530107#define ENCODER_LATENCY_LDAC 40
Aalique Grahame22e49102018-12-18 14:23:57 -0800108#define ENCODER_LATENCY_PCM 50
yidongh0515e042017-07-06 15:00:34 +0800109#define DEFAULT_SINK_LATENCY_SBC 140
110#define DEFAULT_SINK_LATENCY_APTX 160
111#define DEFAULT_SINK_LATENCY_APTX_HD 180
112#define DEFAULT_SINK_LATENCY_AAC 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530113//To Do: Fine Tune Default CELT/LDAC Latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530114#define DEFAULT_SINK_LATENCY_CELT 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530115#define DEFAULT_SINK_LATENCY_LDAC 180
Aalique Grahame22e49102018-12-18 14:23:57 -0800116#define DEFAULT_SINK_LATENCY_PCM 140
117
118#define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported"
119#define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled"
120#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
121
122// Default encoder bit width
123#define DEFAULT_ENCODER_BIT_FORMAT 16
124
125// Default encoder latency
126#define DEFAULT_ENCODER_LATENCY 200
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530127
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800128// Slimbus Tx sample rate for ABR feedback channel
129#define ABR_TX_SAMPLE_RATE "KHZ_8"
130
Zhou Song12c29502019-03-16 10:37:18 +0800131// Slimbus Tx sample rate for APTX AD SPEECH
132#define SPEECH_TX_SAMPLE_RATE "KHZ_96"
133
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800134// Purpose ID for Inter Module Communication (IMC) in AFE
135#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
136
137// Maximum quality levels for ABR
138#define MAX_ABR_QUALITY_LEVELS 5
139
140// Instance identifier for A2DP
141#define MAX_INSTANCE_ID (UINT32_MAX / 2)
142
Zhou Song12c29502019-03-16 10:37:18 +0800143// Instance identifier for SWB
144#define APTX_AD_SPEECH_INSTANCE_ID 37
145
146#define SAMPLING_RATE_96K 96000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530147#define SAMPLING_RATE_48K 48000
148#define SAMPLING_RATE_441K 44100
Zhou Song12c29502019-03-16 10:37:18 +0800149#define SAMPLING_RATE_32K 32000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530150#define CH_STEREO 2
151#define CH_MONO 1
Florian Pfister1a84f312018-07-19 14:38:18 +0200152#define SOURCE 0
153#define SINK 1
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800154#define UNINITIALIZED -1
155
156#ifdef __LP64__
157#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
158#else
159#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
160#endif
161
162static void *vndk_fwk_lib_handle = NULL;
163static int is_running_with_enhanced_fwk = UNINITIALIZED;
164
165typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
166static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
Florian Pfister1a84f312018-07-19 14:38:18 +0200167
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530168/*
169 * Below enum values are extended from audio_base.h to
Florian Pfister1a84f312018-07-19 14:38:18 +0200170 * to keep encoder and decoder type local to bthost_ipc
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530171 * and audio_hal as these are intended only for handshake
172 * between IPC lib and Audio HAL.
173 */
174typedef enum {
Florian Pfister1a84f312018-07-19 14:38:18 +0200175 CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
176 CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
177 CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
178 CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
179 CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530180#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +0200181 CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530182#endif
Florian Pfister1a84f312018-07-19 14:38:18 +0200183 CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
184 CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
Surendar Karka2febd452018-12-13 17:56:43 +0530185 CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
Zhou Song12c29502019-03-16 10:37:18 +0800186 CODEC_TYPE_APTX_AD_SPEECH = 637534208u, //0x26000000UL
Aalique Grahame22e49102018-12-18 14:23:57 -0800187 CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u
Florian Pfister1a84f312018-07-19 14:38:18 +0200188}codec_t;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530189
Sharad Sangle95d451b2018-06-19 12:24:20 +0530190/*
191 * enums which describes the APTX Adaptive
192 * channel mode, these values are used by encoder
193 */
194 typedef enum {
195 APTX_AD_CHANNEL_UNCHANGED = -1,
196 APTX_AD_CHANNEL_JOINT_STEREO = 0, // default
197 APTX_AD_CHANNEL_MONO = 1,
198 APTX_AD_CHANNEL_DUAL_MONO = 2,
199 APTX_AD_CHANNEL_STEREO_TWS = 4,
200 APTX_AD_CHANNEL_EARBUD = 8,
201} enc_aptx_ad_channel_mode;
202
203/*
204 * enums which describes the APTX Adaptive
205 * sampling frequency, these values are used
206 * by encoder
207 */
208typedef enum {
209 APTX_AD_SR_UNCHANGED = 0x0,
210 APTX_AD_48 = 0x1, // 48 KHz default
211 APTX_AD_44_1 = 0x2, // 44.1kHz
Zhou Song5e541dc2019-10-22 23:39:25 +0800212 APTX_AD_96 = 0x3, // 96KHz
Sharad Sangle95d451b2018-06-19 12:24:20 +0530213} enc_aptx_ad_s_rate;
214
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530215typedef void (*bt_audio_pre_init_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200216typedef int (*audio_source_open_t)(void);
217typedef int (*audio_source_close_t)(void);
218typedef int (*audio_source_start_t)(void);
219typedef int (*audio_source_stop_t)(void);
220typedef int (*audio_source_suspend_t)(void);
221typedef void (*audio_source_handoff_triggered_t)(void);
222typedef void (*clear_source_a2dpsuspend_flag_t)(void);
223typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
224 uint8_t *num_dev, codec_t *codec_type);
225typedef int (*audio_source_check_a2dp_ready_t)(void);
226typedef int (*audio_is_source_scrambling_enabled_t)(void);
Manisha Agarwala51768b2018-11-01 16:30:52 +0530227typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200228typedef int (*audio_sink_start_t)(void);
229typedef int (*audio_sink_stop_t)(void);
230typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
231typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
232typedef int (*audio_sink_check_a2dp_ready_t)(void);
233typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530234
235enum A2DP_STATE {
236 A2DP_STATE_CONNECTED,
237 A2DP_STATE_STARTED,
238 A2DP_STATE_STOPPED,
239 A2DP_STATE_DISCONNECTED,
240};
241
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800242typedef enum {
243 IMC_TRANSMIT,
244 IMC_RECEIVE,
245} imc_direction_t;
246
247typedef enum {
248 IMC_DISABLE,
249 IMC_ENABLE,
250} imc_status_t;
251
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700252typedef enum {
Zhou Song12c29502019-03-16 10:37:18 +0800253 SWAP_DISABLE,
254 SWAP_ENABLE,
255} swap_status_t;
256
257typedef enum {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700258 MTU_SIZE,
259 PEAK_BIT_RATE,
260} frame_control_type_t;
261
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800262// --- external function dependency ---
263fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
264fp_check_a2dp_restore_t fp_check_a2dp_restore;
265
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800266/* PCM config for ABR Feedback hostless front end */
267static struct pcm_config pcm_config_abr = {
268 .channels = 1,
269 .rate = 8000,
270 .period_size = 240,
271 .period_count = 2,
272 .format = PCM_FORMAT_S16_LE,
273 .start_threshold = 0,
274 .stop_threshold = INT_MAX,
275 .avail_min = 0,
276};
277
278/* Adaptive bitrate config for A2DP codecs */
279struct a2dp_abr_config {
280 /* Flag to denote whether Adaptive bitrate is enabled for codec */
281 bool is_abr_enabled;
282 /* Flag to denote whether front end has been opened for ABR */
283 bool abr_started;
284 /* ABR Tx path pcm handle */
285 struct pcm *abr_tx_handle;
Zhou Song12c29502019-03-16 10:37:18 +0800286 /* ABR Rx path pcm handle */
287 struct pcm *abr_rx_handle;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800288 /* ABR Inter Module Communication (IMC) instance ID */
289 uint32_t imc_instance;
290};
291
292static uint32_t instance_id = MAX_INSTANCE_ID;
293
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530294/* structure used to update a2dp state machine
295 * to communicate IPC library
296 * to store DSP encoder configuration information
297 */
298struct a2dp_data {
299 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200300 void *bt_lib_source_handle;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530301 bt_audio_pre_init_t bt_audio_pre_init;
Florian Pfister1a84f312018-07-19 14:38:18 +0200302 audio_source_open_t audio_source_open;
303 audio_source_close_t audio_source_close;
304 audio_source_start_t audio_source_start;
305 audio_source_stop_t audio_source_stop;
306 audio_source_suspend_t audio_source_suspend;
307 audio_source_handoff_triggered_t audio_source_handoff_triggered;
308 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
309 audio_get_enc_config_t audio_get_enc_config;
310 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530311 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200312 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
313 enum A2DP_STATE bt_state_source;
314 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530315 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530316 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200317 bool a2dp_source_started;
318 bool a2dp_source_suspended;
319 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530320 bool is_a2dp_offload_supported;
321 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700322 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530323 /* Mono Mode support for TWS+ */
324 bool is_tws_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530325 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800326 /* Adaptive bitrate config for A2DP codecs */
327 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200328
329 void *bt_lib_sink_handle;
330 audio_sink_start_t audio_sink_start;
331 audio_sink_stop_t audio_sink_stop;
332 audio_get_dec_config_t audio_get_dec_config;
333 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
334 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
335 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
336 enum A2DP_STATE bt_state_sink;
337 codec_t bt_decoder_format;
338 uint32_t dec_sampling_rate;
339 uint32_t dec_channels;
340 bool a2dp_sink_started;
341 int a2dp_sink_total_active_session_requests;
Zhou Song12c29502019-03-16 10:37:18 +0800342 bool swb_configured;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530343};
344
345struct a2dp_data a2dp;
346
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800347/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
348 * Structures sent to configure DSP for ABR are defined below.
349 * This data helps DSP configure feedback path (BTSoC to LPASS)
350 * for link quality levels and mapping quality levels to codec
351 * specific bitrate.
352 */
353
354/* Key value pair for link quality level to bitrate mapping. */
355struct bit_rate_level_map_t {
356 uint32_t link_quality_level;
357 uint32_t bitrate;
358};
359
360/* Link quality level to bitrate mapping info sent to DSP. */
361struct quality_level_to_bitrate_info {
362 /* Number of quality levels being mapped.
363 * This will be equal to the size of mapping table.
364 */
365 uint32_t num_levels;
366 /* Quality level to bitrate mapping table */
367 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
368};
369
370/* Structure to set up Inter Module Communication (IMC) between
371 * AFE Decoder and Encoder.
372 */
373struct imc_dec_enc_info {
374 /* Decoder to encoder communication direction.
375 * Transmit = 0 / Receive = 1
376 */
377 uint32_t direction;
378 /* Enable / disable IMC between decoder and encoder */
379 uint32_t enable;
380 /* Purpose of IMC being set up between decoder and encoder.
381 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
382 * is the default value to be sent as purpose.
383 */
384 uint32_t purpose;
385 /* Unique communication instance ID.
386 * purpose and comm_instance together form the actual key
387 * used in IMC registration, which must be the same for
388 * encoder and decoder for which IMC is being set up.
389 */
390 uint32_t comm_instance;
391};
392
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700393/* Structure to control frame size of AAC encoded frames. */
394struct aac_frame_size_control_t {
395 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
396 uint32_t ctl_type;
397 /* Control value
398 * MTU_SIZE: MTU size in bytes
399 * PEAK_BIT_RATE: Peak bitrate in bits per second.
400 */
401 uint32_t ctl_value;
402};
403
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800404/* Structure used for ABR config of AFE encoder and decoder. */
405struct abr_enc_cfg_t {
406 /* Link quality level to bitrate mapping info sent to DSP. */
407 struct quality_level_to_bitrate_info mapping_info;
408 /* Information to set up IMC between decoder and encoder */
409 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700410 /* Flag to indicate whether ABR is enabled */
411 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800412} __attribute__ ((packed));
413
414/* Structure to send configuration for decoder introduced
415 * on AFE Tx path for ABR link quality feedback to BT encoder.
416 */
417struct abr_dec_cfg_t {
418 /* Decoder media format */
419 uint32_t dec_format;
420 /* Information to set up IMC between decoder and encoder */
421 struct imc_dec_enc_info imc_info;
422} __attribute__ ((packed));
423
Zhou Song12c29502019-03-16 10:37:18 +0800424struct aptx_ad_speech_mode_cfg_t
425{
426 uint32_t mode;
427 uint32_t swapping;
428} __attribute__ ((packed));
429
430/* Structure for SWB voice dec config */
431struct aptx_ad_speech_dec_cfg_t {
432 struct abr_dec_cfg_t abr_cfg;
433 struct aptx_ad_speech_mode_cfg_t speech_mode;
434} __attribute__ ((packed));
435
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530436/* START of DSP configurable structures
437 * These values should match with DSP interface defintion
438 */
439
440/* AAC encoder configuration structure. */
441typedef struct aac_enc_cfg_t aac_enc_cfg_t;
442
443/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
444 * supported aac_fmt_flag are ADTS/RAW
445 * supported channel_cfg are Native mode, Mono , Stereo
446 */
447struct aac_enc_cfg_t {
448 uint32_t enc_format;
449 uint32_t bit_rate;
450 uint32_t enc_mode;
451 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530452 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530453 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530454} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530455
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700456struct aac_enc_cfg_v2_t {
457 struct aac_enc_cfg_t aac_enc_cfg;
458 struct aac_frame_size_control_t frame_ctl;
459} __attribute__ ((packed));
460
Surendar Karkabbb3c822018-11-12 13:00:38 +0530461typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
462struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200463 uint16_t aac_fmt_flag; /* LATM*/
464 uint16_t audio_object_type; /* LC */
465 uint16_t channels; /* Stereo */
466 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
467 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
468 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530469} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200470
Surendar Karkabbb3c822018-11-12 13:00:38 +0530471typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
472struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200473 uint16_t channels; /* Mono, Stereo */
474 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
475 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530476} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200477
478/* AAC decoder configuration structure. */
479typedef struct aac_dec_cfg_t aac_dec_cfg_t;
480struct aac_dec_cfg_t {
481 uint32_t dec_format;
482 audio_aac_decoder_config_t data;
483} __attribute__ ((packed));
484
485/* SBC decoder configuration structure. */
486typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
487struct sbc_dec_cfg_t {
488 uint32_t dec_format;
489 audio_sbc_decoder_config_t data;
490} __attribute__ ((packed));
491
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530492/* SBC encoder configuration structure. */
493typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
494
495/* supported num_subbands are 4/8
496 * supported blk_len are 4, 8, 12, 16
497 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
498 * supported alloc_method are LOUNDNESS/SNR
499 * supported bit_rate for mono channel is max 320kbps
500 * supported bit rate for stereo channel is max 512 kbps
501 */
502struct sbc_enc_cfg_t{
503 uint32_t enc_format;
504 uint32_t num_subbands;
505 uint32_t blk_len;
506 uint32_t channel_mode;
507 uint32_t alloc_method;
508 uint32_t bit_rate;
509 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530510} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530511
512
513/* supported num_channels are Mono/Stereo
514 * supported channel_mapping for mono is CHANNEL_C
515 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
516 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700517 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530518struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530519{
520 uint32_t enc_format;
521 uint32_t sample_rate;
522 uint16_t num_channels;
523 uint16_t reserved;
524 uint8_t channel_mapping[8];
525 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530526} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530527
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530528struct celt_specific_enc_cfg_t
529{
530 uint32_t bit_rate;
531 uint16_t frame_size;
532 uint16_t complexity;
533 uint16_t prediction_mode;
534 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530535} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530536
537struct celt_enc_cfg_t
538{
539 struct custom_enc_cfg_t custom_cfg;
540 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530541} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700542
543/* sync_mode introduced with APTX V2 libraries
544 * sync mode: 0x0 = stereo sync mode
545 * 0x01 = dual mono sync mode
546 * 0x02 = dual mono with no sync on either L or R codewords
547 */
548struct aptx_v2_enc_cfg_ext_t
549{
550 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530551} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700552
553/* APTX struct for combining custom enc and V2 fields */
554struct aptx_enc_cfg_t
555{
556 struct custom_enc_cfg_t custom_cfg;
557 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530558} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700559
Sharad Sangle95d451b2018-06-19 12:24:20 +0530560/* APTX AD structure */
561struct aptx_ad_enc_cfg_ext_t
562{
563 uint32_t sampling_freq;
564 uint32_t mtu;
565 uint32_t channel_mode;
566 uint32_t min_sink_modeA;
567 uint32_t max_sink_modeA;
568 uint32_t min_sink_modeB;
569 uint32_t max_sink_modeB;
570 uint32_t min_sink_modeC;
571 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530572 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530573} __attribute__ ((packed));
574
575struct aptx_ad_enc_cfg_t
576{
577 struct custom_enc_cfg_t custom_cfg;
578 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
579 struct abr_enc_cfg_t abr_cfg;
580} __attribute__ ((packed));
581
Zhou Song8fccbb62019-03-20 01:08:19 +0800582struct aptx_ad_enc_cfg_ext_r2_t
583{
584 uint32_t sampling_freq;
585 uint32_t mtu;
586 uint32_t channel_mode;
587 uint32_t min_sink_modeA;
588 uint32_t max_sink_modeA;
589 uint32_t min_sink_modeB;
590 uint32_t max_sink_modeB;
591 uint32_t min_sink_modeC;
592 uint32_t max_sink_modeC;
593 uint32_t mode;
594 uint32_t input_mode;
595 uint32_t fade_duration;
596 uint8_t sink_cap[11];
597} __attribute__ ((packed));
598
599struct aptx_ad_enc_cfg_r2_t
600{
601 struct custom_enc_cfg_t custom_cfg;
602 struct aptx_ad_enc_cfg_ext_r2_t aptx_ad_cfg;
603 struct abr_enc_cfg_t abr_cfg;
604} __attribute__ ((packed));
605
Zhou Song12c29502019-03-16 10:37:18 +0800606/* APTX AD SPEECH structure */
607struct aptx_ad_speech_enc_cfg_t
608{
609 struct custom_enc_cfg_t custom_cfg;
610 /* Information to set up IMC between decoder and encoder */
611 struct imc_dec_enc_info imc_info;
612 struct aptx_ad_speech_mode_cfg_t speech_mode;
613} __attribute__ ((packed));
614
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530615struct ldac_specific_enc_cfg_t
616{
617 uint32_t bit_rate;
618 uint16_t channel_mode;
619 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530620} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530621
622struct ldac_enc_cfg_t
623{
624 struct custom_enc_cfg_t custom_cfg;
625 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800626 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530627} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530628
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530629/* In LE BT source code uses system/audio.h for below
630 * structure definition. To avoid multiple definition
631 * compilation error for audiohal in LE , masking structure
632 * definition under "LINUX_ENABLED" which is defined only
633 * in LE
634 */
635#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530636/* TODO: Define the following structures only for O using PLATFORM_VERSION */
637/* Information about BT SBC encoder configuration
638 * This data is used between audio HAL module and
639 * BT IPC library to configure DSP encoder
640 */
641typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530642 uint32_t subband; /* 4, 8 */
643 uint32_t blk_len; /* 4, 8, 12, 16 */
644 uint16_t sampling_rate; /*44.1khz,48khz*/
645 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
646 uint8_t alloc; /*0(Loudness),1(SNR)*/
647 uint8_t min_bitpool; /* 2 */
648 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
649 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530650 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530651} audio_sbc_encoder_config;
652
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530653/* Information about BT APTX encoder configuration
654 * This data is used between audio HAL module and
655 * BT IPC library to configure DSP encoder
656 */
657typedef struct {
658 uint16_t sampling_rate;
659 uint8_t channels;
660 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530661 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700662} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530663
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700664typedef struct {
Zhou Song8fccbb62019-03-20 01:08:19 +0800665 uint32_t sampling_rate;
666 uint32_t mtu;
667 int32_t channel_mode;
668 uint32_t min_sink_modeA;
669 uint32_t max_sink_modeA;
670 uint32_t min_sink_modeB;
671 uint32_t max_sink_modeB;
672 uint32_t min_sink_modeC;
673 uint32_t max_sink_modeC;
674 uint32_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530675 uint8_t TTP_modeA_low;
676 uint8_t TTP_modeA_high;
677 uint8_t TTP_modeB_low;
678 uint8_t TTP_modeB_high;
Zhou Song8fccbb62019-03-20 01:08:19 +0800679 uint8_t TTP_TWS_low;
680 uint8_t TTP_TWS_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530681 uint32_t bits_per_sample;
Zhou Song8fccbb62019-03-20 01:08:19 +0800682 uint32_t input_mode;
683 uint32_t fade_duration;
684 uint8_t sink_cap[11];
Sharad Sangle95d451b2018-06-19 12:24:20 +0530685} audio_aptx_ad_config;
686
687typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700688 uint16_t sampling_rate;
689 uint8_t channels;
690 uint32_t bitrate;
691 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530692 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700693} audio_aptx_dual_mono_config;
694
695typedef union {
696 audio_aptx_default_config *default_cfg;
697 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530698 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700699} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530700
701/* Information about BT AAC encoder configuration
702 * This data is used between audio HAL module and
703 * BT IPC library to configure DSP encoder
704 */
705typedef struct {
706 uint32_t enc_mode; /* LC, SBR, PS */
707 uint16_t format_flag; /* RAW, ADTS */
708 uint16_t channels; /* 1-Mono, 2-Stereo */
709 uint32_t sampling_rate;
710 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530711 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530712} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530713#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700714
715typedef struct {
716 audio_aac_encoder_config audio_aac_enc_cfg;
717 struct aac_frame_size_control_t frame_ctl;
718} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530719
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530720/* Information about BT CELT encoder configuration
721 * This data is used between audio HAL module and
722 * BT IPC library to configure DSP encoder
723 */
724typedef struct {
725 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
726 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
727 uint16_t frame_size; /* 64-128-256-512, 512 */
728 uint16_t complexity; /* 0-10, 1 */
729 uint16_t prediction_mode; /* 0-1-2, 0 */
730 uint16_t vbr_flag; /* 0-1, 0*/
731 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530732 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530733} audio_celt_encoder_config;
734
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530735/* Information about BT LDAC encoder configuration
736 * This data is used between audio HAL module and
737 * BT IPC library to configure DSP encoder
738 */
739typedef struct {
740 uint32_t sampling_rate; /*44100,48000,88200,96000*/
741 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
742 uint16_t channel_mode; /* 0, 4, 2, 1*/
743 uint16_t mtu; /*679*/
Aniket Kumar Latad0196282019-05-09 14:24:17 -0700744 uint32_t bits_per_sample;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800745 bool is_abr_enabled;
746 struct quality_level_to_bitrate_info level_to_bitrate_map;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530747} audio_ldac_encoder_config;
748
Surendar Karkabbb3c822018-11-12 13:00:38 +0530749/* Information about BT AAC decoder configuration
750 * This data is used between audio HAL module and
751 * BT IPC library to configure DSP decoder
752 */
753typedef struct {
754 uint16_t aac_fmt_flag; /* LATM*/
755 uint16_t audio_object_type; /* LC */
756 uint16_t channels; /* Stereo */
757 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
758 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
759 44.1k, 48k, 64k, 88.2k, 96k */
760} audio_aac_dec_config_t;
761
762/* Information about BT SBC decoder configuration
763 * This data is used between audio HAL module and
764 * BT IPC library to configure DSP decoder
765 */
766typedef struct {
767 uint16_t channels; /* Mono, Stereo */
768 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
769 44.1k, 48k, 64k, 88.2k, 96k */
770}audio_sbc_dec_config_t;
771
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530772/*********** END of DSP configurable structures ********************/
773
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530774static void update_offload_codec_capabilities()
775{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530776
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530777 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800778 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
779 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
780
781 ALOGD("%s: A2DP offload supported = %d",__func__,
782 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530783}
784
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800785static int stop_abr()
786{
787 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800788 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800789 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800790 int ret = 0;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800791
792 /* This function can be used if !abr_started for clean up */
793 ALOGV("%s: enter", __func__);
794
795 // Close hostless front end
796 if (a2dp.abr_config.abr_tx_handle != NULL) {
797 pcm_close(a2dp.abr_config.abr_tx_handle);
798 a2dp.abr_config.abr_tx_handle = NULL;
799 }
Zhou Song12c29502019-03-16 10:37:18 +0800800 if (a2dp.abr_config.abr_rx_handle != NULL) {
801 pcm_close(a2dp.abr_config.abr_rx_handle);
802 a2dp.abr_config.abr_rx_handle = NULL;
803 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800804 a2dp.abr_config.abr_started = false;
805 a2dp.abr_config.imc_instance = 0;
806
807 // Reset BT driver mixer control for ABR usecase
808 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
809 MIXER_SET_FEEDBACK_CHANNEL);
810 if (!ctl_set_bt_feedback_channel) {
811 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800812 ret = -ENOSYS;
813 } else if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800814 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800815 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800816 }
817
818 // Reset ABR Tx feedback path
819 ALOGV("%s: Disable ABR Tx feedback path", __func__);
820 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
821 MIXER_ABR_TX_FEEDBACK_PATH);
822 if (!ctl_abr_tx_path) {
823 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800824 ret = -ENOSYS;
825 } else if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800826 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800827 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800828 }
829
Zhou Song12c29502019-03-16 10:37:18 +0800830 // Reset ABR Rx feedback path
831 ALOGV("%s: Disable ABR Rx feedback path", __func__);
832 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
833 MIXER_ABR_RX_FEEDBACK_PATH);
834 if (!ctl_abr_rx_path) {
835 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
836 ret = -ENOSYS;
837 } else if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 0) != 0) {
838 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
839 ret = -ENOSYS;
840 }
841
842 return ret;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800843}
844
845static int start_abr()
846{
847 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800848 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800849 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
850 int abr_device_id;
851 int ret = 0;
852
853 if (!a2dp.abr_config.is_abr_enabled) {
854 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
855 return -ENOSYS;
856 }
857
858 if (a2dp.abr_config.abr_started) {
859 ALOGI("%s: ABR has already started", __func__);
860 return ret;
861 }
862
863 // Enable Slimbus 7 Tx feedback path
864 ALOGV("%s: Enable ABR Tx feedback path", __func__);
865 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
866 MIXER_ABR_TX_FEEDBACK_PATH);
867 if (!ctl_abr_tx_path) {
868 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
869 return -ENOSYS;
870 }
871 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
872 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
873 return -ENOSYS;
874 }
875
876 // Notify ABR usecase information to BT driver to distinguish
877 // between SCO and feedback usecase
878 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
879 MIXER_SET_FEEDBACK_CHANNEL);
880 if (!ctl_set_bt_feedback_channel) {
881 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800882 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800883 }
884 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
885 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800886 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800887 }
888
889 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800890 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800891 PCM_CAPTURE);
892 if (!a2dp.abr_config.abr_tx_handle) {
893 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
894 abr_device_id, PCM_IN,
895 &pcm_config_abr);
Zhou Song12c29502019-03-16 10:37:18 +0800896 if (a2dp.abr_config.abr_tx_handle == NULL) {
897 ALOGE("%s: Can't open abr tx device", __func__);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800898 goto fail;
Zhou Song12c29502019-03-16 10:37:18 +0800899 }
900 if (!(pcm_is_ready(a2dp.abr_config.abr_tx_handle) &&
901 !pcm_start(a2dp.abr_config.abr_tx_handle))) {
902 ALOGE("%s: tx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
903 goto fail;
904 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800905 }
Zhou Song12c29502019-03-16 10:37:18 +0800906
907 // Enable Slimbus 7 Rx feedback path for HD Voice use case
908 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH) {
909 ALOGV("%s: Enable ABR Rx feedback path", __func__);
910 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
911 MIXER_ABR_RX_FEEDBACK_PATH);
912 if (!ctl_abr_rx_path) {
913 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
914 goto fail;
915 }
916 if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 1) != 0) {
917 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
918 goto fail;
919 }
920
921 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
922 ALOGE("%s: Failed to set BT usecase", __func__);
923 goto fail;
924 }
925
926 // Open hostless front end and prepare ABR Rx path
927 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
928 PCM_PLAYBACK);
929 if (!a2dp.abr_config.abr_rx_handle) {
930 a2dp.abr_config.abr_rx_handle = pcm_open(a2dp.adev->snd_card,
931 abr_device_id, PCM_OUT,
932 &pcm_config_abr);
933 if (a2dp.abr_config.abr_rx_handle == NULL) {
934 ALOGE("%s: Can't open abr rx device", __func__);
935 goto fail;
936 }
937 if (!(pcm_is_ready(a2dp.abr_config.abr_rx_handle) &&
938 !pcm_start(a2dp.abr_config.abr_rx_handle))) {
939 ALOGE("%s: rx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_rx_handle));
940 goto fail;
941 }
942 }
943 }
944
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800945 a2dp.abr_config.abr_started = true;
946
947 return ret;
948
949fail:
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800950 stop_abr();
951 return -ENOSYS;
952}
953
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800954static int check_if_enhanced_fwk() {
955
956 int is_enhanced_fwk = 1;
957 //dlopen lib
958 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
959 if (vndk_fwk_lib_handle != NULL) {
960 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
961 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
962 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
963 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
964 __func__);
965 is_enhanced_fwk = 1;
966 } else {
967 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
968 }
969 }
970 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
971 return is_enhanced_fwk;
972}
973
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530974static void open_a2dp_source() {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530975 int ret = 0;
976
Florian Pfister1a84f312018-07-19 14:38:18 +0200977 ALOGD(" Open A2DP source start ");
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530978
979 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
980 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
981 ALOGD("calling BT stream open");
982 ret = a2dp.audio_source_open();
983 if(ret != 0) {
984 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Srinu Jellada99a592019-01-25 16:50:52 +0530985 }
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530986 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530987 } else {
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530988 ALOGD("Called a2dp open with improper state %d", a2dp.bt_state_source);
989 }
990 } else {
991 ALOGE("a2dp handle is not identified, Ignoring open request");
992 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
993 }
994}
995/* API to open BT IPC library to start IPC communication for BT Source*/
996static void a2dp_source_init()
997{
998 ALOGD("a2dp_source_init START");
999 if (a2dp.bt_lib_source_handle == NULL) {
1000 ALOGD("Requesting for BT lib handle");
1001 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
1002 if (a2dp.bt_lib_source_handle == NULL) {
1003 ALOGE("%s: dlopen failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
1004 return;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301005 }
1006 }
1007
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301008 a2dp.bt_audio_pre_init = (bt_audio_pre_init_t)
1009 dlsym(a2dp.bt_lib_source_handle, "bt_audio_pre_init");
Srinu Jellada99a592019-01-25 16:50:52 +05301010 a2dp.audio_source_open = (audio_source_open_t)
1011 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
1012 a2dp.audio_source_start = (audio_source_start_t)
1013 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
1014 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
1015 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
1016 a2dp.audio_source_suspend = (audio_source_suspend_t)
1017 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
1018 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
1019 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
1020 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1021 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
1022 a2dp.audio_source_stop = (audio_source_stop_t)
1023 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
1024 a2dp.audio_source_close = (audio_source_close_t)
1025 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
1026 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
1027 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
1028 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
1029 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
1030 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1031 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1032 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1033 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1034
Srinu Jella425521d2019-07-19 15:53:55 +05301035 if (a2dp.bt_lib_source_handle && a2dp.bt_audio_pre_init) {
1036 ALOGD("calling BT module preinit");
1037 // fwk related check's will be done in the BT layer
1038 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001039 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001040}
1041
1042/* API to open BT IPC library to start IPC communication for BT Sink*/
1043static void open_a2dp_sink()
1044{
1045 ALOGD(" Open A2DP input start ");
1046 if (a2dp.bt_lib_sink_handle == NULL){
1047 ALOGD(" Requesting for BT lib handle");
1048 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1049
1050 if (a2dp.bt_lib_sink_handle == NULL) {
1051 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1052 } else {
1053 a2dp.audio_sink_start = (audio_sink_start_t)
1054 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1055 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1056 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1057 a2dp.audio_sink_stop = (audio_sink_stop_t)
1058 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1059 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1060 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1061 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1062 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1063 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301064 }
1065}
1066
1067static int close_a2dp_output()
1068{
1069 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001070
1071 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1072 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301073 return -ENOSYS;
1074 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001075
1076 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1077 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001078 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001079 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301080 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001081 a2dp.a2dp_source_started = false;
1082 a2dp.a2dp_source_total_active_session_requests = 0;
1083 a2dp.a2dp_source_suspended = false;
1084 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301085 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301086 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001087 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001088 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1089 stop_abr();
1090 a2dp.abr_config.is_abr_enabled = false;
1091 a2dp.abr_config.abr_started = false;
1092 a2dp.abr_config.imc_instance = 0;
1093 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001094 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301095 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001096
1097 return 0;
1098}
1099
1100static int close_a2dp_input()
1101{
1102 ALOGV("%s\n",__func__);
1103
1104 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1105 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1106 return -ENOSYS;
1107 }
1108
1109 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1110 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001111 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001112 ALOGE("failed close a2dp sink control path from BT library");
1113 }
1114 a2dp.a2dp_sink_started = false;
1115 a2dp.a2dp_sink_total_active_session_requests = 0;
1116 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1117 a2dp.dec_sampling_rate = 48000;
1118 a2dp.dec_channels = 2;
1119 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301120
1121 return 0;
1122}
1123
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301124static void a2dp_check_and_set_scrambler()
1125{
1126 bool scrambler_mode = false;
1127 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001128 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1129 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301130
1131 if (scrambler_mode) {
1132 //enable scrambler in dsp
1133 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1134 MIXER_SCRAMBLER_MODE);
1135 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001136 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301137 return;
1138 } else {
1139 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1140 ALOGE("%s: Could not set scrambler mode", __func__);
1141 return;
1142 }
1143 }
1144 }
1145}
1146
Florian Pfister1a84f312018-07-19 14:38:18 +02001147static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301148{
Florian Pfister1a84f312018-07-19 14:38:18 +02001149 char *rate_str = NULL, *channels = NULL;
1150 uint32_t sampling_rate;
1151 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1152 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301153
Florian Pfister1a84f312018-07-19 14:38:18 +02001154 if (direction == SINK) {
1155 sampling_rate = a2dp.dec_sampling_rate;
1156 } else {
1157 sampling_rate = a2dp.enc_sampling_rate;
1158 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001159 /*
1160 * For LDAC encoder and AAC decoder open slimbus port at
1161 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1162 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1163 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001164 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301165 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001166 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1167 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301168 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1169 sampling_rate = sampling_rate *2;
1170 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001171
Aalique Grahame22e49102018-12-18 14:23:57 -08001172 // No need to configure backend for PCM format.
1173 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1174 return 0;
1175 }
1176
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301177 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301178 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301179 case 44100:
1180 rate_str = "KHZ_44P1";
1181 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301182 case 88200:
1183 rate_str = "KHZ_88P2";
1184 break;
1185 case 96000:
1186 rate_str = "KHZ_96";
1187 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001188 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301189 default:
1190 rate_str = "KHZ_48";
1191 break;
1192 }
1193
Florian Pfister1a84f312018-07-19 14:38:18 +02001194 if (direction == SINK) {
1195 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1196 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301197 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001198 } else {
1199 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1200 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301201 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001202 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301203 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001204
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301205 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1206 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301207 is_configured = false;
1208 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301209 }
1210
Surendar Karka2febd452018-12-13 17:56:43 +05301211 if (direction == SOURCE) {
1212 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001213 if (a2dp.abr_config.is_abr_enabled) {
1214 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH)
1215 rate_str = SPEECH_TX_SAMPLE_RATE;
1216 else
1217 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301218
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301219 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1220 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1221 MIXER_SOURCE_SAMPLE_RATE_TX);
1222 if (!ctl_sample_rate) {
1223 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1224 is_configured = false;
1225 goto fail;
1226 }
1227
1228 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1229 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1230 is_configured = false;
1231 goto fail;
1232 }
Surendar Karka2febd452018-12-13 17:56:43 +05301233 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301234 }
1235 } else {
1236 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1237 MIXER_SAMPLE_RATE_TX is not supported */
1238 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1239 MIXER_SAMPLE_RATE_DEFAULT);
1240 if (!ctl_sample_rate) {
1241 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301242 is_configured = false;
1243 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301244 }
1245
1246 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1247 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001248 is_configured = false;
1249 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301250 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301251 }
1252
Florian Pfister1a84f312018-07-19 14:38:18 +02001253 if (direction == SINK) {
1254 switch (a2dp.dec_channels) {
1255 case 1:
1256 channels = "One";
1257 break;
1258 case 2:
1259 default:
1260 channels = "Two";
1261 break;
1262 }
1263
Ramu Gottipati02809682018-12-19 16:46:12 +05301264 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001265 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1266 MIXER_AFE_SINK_CHANNELS);
1267 } else {
1268 //Configure AFE enc channels
1269 switch (a2dp.enc_channels) {
1270 case 1:
1271 channels = "One";
1272 break;
1273 case 2:
1274 default:
1275 channels = "Two";
1276 break;
1277 }
1278
Ramu Gottipati02809682018-12-19 16:46:12 +05301279 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001280 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1281 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301282 }
1283
Florian Pfister1a84f312018-07-19 14:38:18 +02001284 if (!ctrl_channels) {
1285 ALOGE(" ERROR AFE channels mixer control not identified");
1286 } else {
1287 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301288 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001289 is_configured = false;
1290 goto fail;
1291 }
1292 }
1293 is_configured = true;
1294fail:
1295 return is_configured;
1296}
1297
Surendar Karkabbb3c822018-11-12 13:00:38 +05301298bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001299{
1300 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1301 struct aac_dec_cfg_t aac_dsp_cfg;
1302 bool is_configured = false;
1303 int ret = 0;
1304
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001305 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001306 return false;
1307
Surendar Karka2febd452018-12-13 17:56:43 +05301308 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001309 if (!ctl_dec_data) {
1310 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1311 is_configured = false;
1312 goto fail;
1313 }
1314
1315 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1316 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1317 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1318 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1319 switch(aac_bt_cfg->audio_object_type) {
1320 case 0:
1321 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301322 break;
1323 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001324 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1325 break;
1326 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301327 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001328 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301329 break;
1330 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001331 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1332 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1333 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1334 sizeof(struct aac_dec_cfg_t));
1335 if (ret != 0) {
1336 ALOGE("%s: failed to set AAC decoder config", __func__);
1337 is_configured = false;
1338 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001339 }
1340
Florian Pfister1a84f312018-07-19 14:38:18 +02001341 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1342 MIXER_DEC_BIT_FORMAT);
1343 if (!ctrl_bit_format) {
1344 ALOGE(" ERROR Dec bit format mixer control not identified");
1345 is_configured = false;
1346 goto fail;
1347 }
1348 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1349 if (ret != 0) {
1350 ALOGE("%s: Failed to set bit format to decoder", __func__);
1351 is_configured = false;
1352 goto fail;
1353 }
1354
1355 is_configured = true;
1356 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1357 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1358 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1359 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1360 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1361fail:
1362 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301363}
1364
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301365static int a2dp_set_bit_format(uint32_t enc_bit_format)
1366{
1367 const char *bit_format = NULL;
1368 struct mixer_ctl *ctrl_bit_format = NULL;
1369
1370 // Configure AFE Input Bit Format
1371 switch (enc_bit_format) {
1372 case 32:
1373 bit_format = "S32_LE";
1374 break;
1375 case 24:
1376 bit_format = "S24_LE";
1377 break;
1378 case 16:
1379 default:
1380 bit_format = "S16_LE";
1381 break;
1382 }
1383
1384 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1385 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1386 MIXER_ENC_BIT_FORMAT);
1387 if (!ctrl_bit_format) {
1388 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1389 return -ENOSYS;
1390 }
1391 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1392 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1393 return -ENOSYS;
1394 }
1395 return 0;
1396}
1397
Surendar Karka2febd452018-12-13 17:56:43 +05301398static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301399{
Surendar Karka2febd452018-12-13 17:56:43 +05301400 const char *rate_str = "KHZ_8", *channels = "Zero";
1401 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1402 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301403
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001404 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301405 if (direction == SINK) {
1406 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1407 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1408 MIXER_SINK_SAMPLE_RATE);
1409 } else {
1410 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1411 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1412 MIXER_SAMPLE_RATE_RX);
1413 }
1414 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301415
Surendar Karka2febd452018-12-13 17:56:43 +05301416 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1417 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301418 return -ENOSYS;
1419 }
Zhou Song12c29502019-03-16 10:37:18 +08001420 if (a2dp.abr_config.is_abr_enabled) {
1421 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1422 MIXER_SOURCE_SAMPLE_RATE_TX);
1423 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301424 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1425 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001426 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301427
Zhou Song12c29502019-03-16 10:37:18 +08001428 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1429 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1430 return -ENOSYS;
1431 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301432 }
1433 } else {
1434
Surendar Karka2febd452018-12-13 17:56:43 +05301435 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301436 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301437 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301438 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1439 return -ENOSYS;
1440 }
1441
Surendar Karka2febd452018-12-13 17:56:43 +05301442 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301443 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1444 return -ENOSYS;
1445 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001446 }
1447
1448 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301449 if (direction == SINK) {
1450 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1451 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1452 MIXER_AFE_SINK_CHANNELS);
1453 } else {
1454 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1455 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1456 MIXER_AFE_IN_CHANNELS);
1457 }
1458 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001459 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1460 return -ENOSYS;
1461 }
Surendar Karka2febd452018-12-13 17:56:43 +05301462 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001463 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1464 return -ENOSYS;
1465 }
1466
1467 return 0;
1468}
1469
1470/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301471static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001472{
1473 struct mixer_ctl *ctl_dec_data = NULL;
1474 struct abr_dec_cfg_t dec_cfg;
1475 int ret = 0;
1476
1477 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301478 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001479 if (!ctl_dec_data) {
1480 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1481 return false;
1482 }
1483 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1484 dec_cfg.dec_format = dec_format;
1485 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1486 dec_cfg.imc_info.enable = IMC_ENABLE;
1487 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1488 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1489
1490 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1491 sizeof(dec_cfg));
1492 if (ret != 0) {
1493 ALOGE("%s: Failed to set decoder config", __func__);
1494 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301495 }
Surendar Karka2febd452018-12-13 17:56:43 +05301496 }
1497
1498 return true;
1499}
1500
Surendar Karkabbb3c822018-11-12 13:00:38 +05301501bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001502{
1503 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1504 struct sbc_dec_cfg_t sbc_dsp_cfg;
1505 bool is_configured = false;
1506 int ret = 0;
1507
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001508 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001509 goto fail;
1510
Surendar Karka2febd452018-12-13 17:56:43 +05301511 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001512 if (!ctl_dec_data) {
1513 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1514 is_configured = false;
1515 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301516 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001517
Florian Pfister1a84f312018-07-19 14:38:18 +02001518 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1519 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1520 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1521 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1522 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1523 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301524
Florian Pfister1a84f312018-07-19 14:38:18 +02001525 if (ret != 0) {
1526 ALOGE("%s: failed to set SBC decoder config", __func__);
1527 is_configured = false;
1528 goto fail;
1529 }
1530
1531 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1532 MIXER_DEC_BIT_FORMAT);
1533 if (!ctrl_bit_format) {
1534 ALOGE(" ERROR Dec bit format mixer control not identified");
1535 is_configured = false;
1536 goto fail;
1537 }
1538 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1539 if (ret != 0) {
1540 ALOGE("%s: Failed to set bit format to decoder", __func__);
1541 is_configured = false;
1542 goto fail;
1543 }
1544
1545 is_configured = true;
1546 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1547 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1548 a2dp.dec_channels = 1;
1549 else
1550 a2dp.dec_channels = 2;
1551 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1552 ALOGV("Successfully updated SBC dec format");
1553fail:
1554 return is_configured;
1555}
1556
Florian Pfister1a84f312018-07-19 14:38:18 +02001557/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301558static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301559{
Florian Pfister1a84f312018-07-19 14:38:18 +02001560 void *codec_info = NULL;
1561 codec_t codec_type = CODEC_TYPE_INVALID;
1562 bool is_configured = false;
1563 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301564
Florian Pfister1a84f312018-07-19 14:38:18 +02001565 if (!a2dp.audio_get_dec_config) {
1566 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1567 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301568 }
1569
Surendar Karka2febd452018-12-13 17:56:43 +05301570 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001571 if (!ctl_dec_data) {
1572 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1573 is_configured = false;
1574 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301575 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001576 codec_info = a2dp.audio_get_dec_config(&codec_type);
1577 switch(codec_type) {
1578 case CODEC_TYPE_SBC:
1579 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301580 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001581 break;
1582 case CODEC_TYPE_AAC:
1583 ALOGD(" AAC decoder supported BT device");
1584 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301585 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001586 break;
1587 default:
1588 ALOGD(" Received Unsupported decoder format");
1589 is_configured = false;
1590 break;
1591 }
1592 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301593}
1594
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301595/* API to configure SBC DSP encoder */
1596bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1597{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301598 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301599 struct sbc_enc_cfg_t sbc_dsp_cfg;
1600 bool is_configured = false;
1601 int ret = 0;
1602
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001603 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301604 return false;
1605
1606 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1607 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001608 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301609 is_configured = false;
1610 goto fail;
1611 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301612 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001613 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301614 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1615 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1616 switch(sbc_bt_cfg->channels) {
1617 case 0:
1618 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1619 break;
1620 case 1:
1621 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1622 break;
1623 case 3:
1624 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1625 break;
1626 case 2:
1627 default:
1628 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1629 break;
1630 }
1631 if (sbc_bt_cfg->alloc)
1632 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1633 else
1634 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1635 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1636 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1637 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1638 sizeof(struct sbc_enc_cfg_t));
1639 if (ret != 0) {
1640 ALOGE("%s: failed to set SBC encoder config", __func__);
1641 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301642 goto fail;
1643 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301644 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301645 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301646 is_configured = false;
1647 goto fail;
1648 }
1649 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001650 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301651 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301652
1653 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1654 a2dp.enc_channels = 1;
1655 else
1656 a2dp.enc_channels = 2;
1657
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301658 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1659 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301660fail:
1661 return is_configured;
1662}
1663
Manish Dewangan6a252632017-12-04 17:27:44 +05301664#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301665static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1666 audio_aptx_encoder_config *aptx_bt_cfg)
1667{
1668 int ret = 0;
1669
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001670 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301671 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1672 aptx_dsp_cfg, aptx_bt_cfg);
1673 return -EINVAL;
1674 }
1675
1676 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301677 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301678
1679
1680 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1681 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1682 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1683 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1684 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1685 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1686 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1687 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1688 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301689 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301690 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1691 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1692 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1693 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1694
1695
1696 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1697 case APTX_AD_CHANNEL_UNCHANGED:
1698 case APTX_AD_CHANNEL_JOINT_STEREO:
1699 case APTX_AD_CHANNEL_DUAL_MONO:
1700 case APTX_AD_CHANNEL_STEREO_TWS:
1701 case APTX_AD_CHANNEL_EARBUD:
1702 default:
1703 a2dp.enc_channels = CH_STEREO;
1704 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1705 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1706 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1707 break;
1708 case APTX_AD_CHANNEL_MONO:
1709 a2dp.enc_channels = CH_MONO;
1710 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1711 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song5e541dc2019-10-22 23:39:25 +08001712 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301713 }
1714 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1715 case APTX_AD_SR_UNCHANGED:
1716 case APTX_AD_48:
1717 default:
1718 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1719 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1720 break;
1721 case APTX_AD_44_1:
1722 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1723 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song5e541dc2019-10-22 23:39:25 +08001724 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301725 }
1726 ALOGV("Successfully updated APTX AD enc format with \
1727 samplingrate: %d channels:%d",
1728 aptx_dsp_cfg->custom_cfg.sample_rate,
1729 aptx_dsp_cfg->custom_cfg.num_channels);
1730
1731 return ret;
1732}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301733
Zhou Song8fccbb62019-03-20 01:08:19 +08001734static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
1735 audio_aptx_encoder_config *aptx_bt_cfg)
1736{
1737 int ret = 0;
1738
1739 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Mingshu Pangaa429f72019-06-10 17:25:00 +08001740 ALOGE("Invalid param, aptx_dsp_cfg %pK aptx_bt_cfg %pK",
Zhou Song8fccbb62019-03-20 01:08:19 +08001741 aptx_dsp_cfg, aptx_bt_cfg);
1742 return -EINVAL;
1743 }
1744
1745 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
1746 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
1747
1748
1749 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1750 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1751 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1752 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1753 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1754 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1755 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1756 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1757 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
1758 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
1759 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
1760 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
1761 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
1762 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
1763 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1764 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1765 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1766 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1767
1768
1769 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1770 case APTX_AD_CHANNEL_UNCHANGED:
1771 case APTX_AD_CHANNEL_JOINT_STEREO:
1772 case APTX_AD_CHANNEL_DUAL_MONO:
1773 case APTX_AD_CHANNEL_STEREO_TWS:
1774 case APTX_AD_CHANNEL_EARBUD:
1775 default:
1776 a2dp.enc_channels = CH_STEREO;
1777 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1778 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1779 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1780 break;
1781 case APTX_AD_CHANNEL_MONO:
1782 a2dp.enc_channels = CH_MONO;
1783 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1784 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song5e541dc2019-10-22 23:39:25 +08001785 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001786 }
1787 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1788 case APTX_AD_SR_UNCHANGED:
1789 case APTX_AD_48:
1790 default:
1791 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1792 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1793 break;
1794 case APTX_AD_44_1:
1795 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1796 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song5e541dc2019-10-22 23:39:25 +08001797 break;
1798 case APTX_AD_96:
1799 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
1800 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_96K;
1801 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001802 }
1803 ALOGV("Successfully updated APTX AD enc format with \
1804 samplingrate: %d channels:%d",
1805 aptx_dsp_cfg->custom_cfg.sample_rate,
1806 aptx_dsp_cfg->custom_cfg.num_channels);
1807
1808 return ret;
1809}
1810
Manisha Agarwala51768b2018-11-01 16:30:52 +05301811static void audio_a2dp_update_tws_channel_mode()
1812{
1813 char* channel_mode;
1814 struct mixer_ctl *ctl_channel_mode;
Manisha Agarwal6e691c02019-09-20 14:03:57 +05301815
1816 ALOGD("Update tws for mono_mode on=%d",a2dp.is_tws_mono_mode_on);
1817
Manisha Agarwala51768b2018-11-01 16:30:52 +05301818 if (a2dp.is_tws_mono_mode_on)
1819 channel_mode = "One";
1820 else
1821 channel_mode = "Two";
Manisha Agarwal6e691c02019-09-20 14:03:57 +05301822
Manisha Agarwala51768b2018-11-01 16:30:52 +05301823 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1824 if (!ctl_channel_mode) {
1825 ALOGE("failed to get tws mixer ctl");
1826 return;
1827 }
1828 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1829 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1830 return;
1831 }
1832}
1833
Manish Dewangan6a252632017-12-04 17:27:44 +05301834static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
1835 audio_aptx_encoder_config *aptx_bt_cfg)
1836{
1837 int ret = 0;
1838
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001839 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301840 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1841 aptx_dsp_cfg, aptx_bt_cfg);
1842 return -EINVAL;
1843 }
1844
1845 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001846 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301847
1848 if (!a2dp.is_aptx_dual_mono_supported) {
1849 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1850 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1851 } else {
1852 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1853 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1854 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1855 }
1856
1857 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
1858 case 1:
1859 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1860 break;
1861 case 2:
1862 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05301863 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1864 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
Manish Dewangan6a252632017-12-04 17:27:44 +05301865 break;
1866 }
1867 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
1868 if (!a2dp.is_aptx_dual_mono_supported) {
1869 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1870 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
1871 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
1872 aptx_dsp_cfg->custom_cfg.num_channels);
1873 } else {
1874 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1875 ALOGV("Successfully updated APTX dual mono enc format with \
1876 samplingrate: %d channels:%d syncmode %d",
1877 aptx_dsp_cfg->custom_cfg.sample_rate,
1878 aptx_dsp_cfg->custom_cfg.num_channels,
1879 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
1880 }
1881 return ret;
1882}
1883#else
1884static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
1885 audio_aptx_encoder_config *aptx_bt_cfg)
1886{
1887 int ret = 0;
1888
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001889 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301890 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1891 aptx_dsp_cfg, aptx_bt_cfg);
1892 return -EINVAL;
1893 }
1894
1895 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001896 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301897 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
1898 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
1899 switch(aptx_dsp_cfg->num_channels) {
1900 case 1:
1901 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
1902 break;
1903 case 2:
1904 default:
1905 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
1906 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
1907 break;
1908 }
1909
1910 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
1911 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
1912
1913 return ret;
1914}
1915#endif
1916
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301917/* API to configure APTX DSP encoder */
1918bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1919{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301920 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08001921 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001922 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301923 bool is_configured = false;
1924 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08001925 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301926
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001927 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301928 return false;
1929
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001930 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1931 if (!ctl_enc_data) {
1932 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1933 return false;
1934 }
1935
Manish Dewangan6a252632017-12-04 17:27:44 +05301936#ifndef LINUX_ENABLED
1937 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301938 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08001939 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001940 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001941 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
1942 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001943 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08001944 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001945 else
Zhou Song8fccbb62019-03-20 01:08:19 +08001946 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001947 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001948 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001949
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001950 if (ret) {
1951 is_configured = false;
1952 goto fail;
1953 }
1954
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001955 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001956 if (aptx_ad_ctl)
1957 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001958 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08001959 else
1960 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001961 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001962 } else {
1963 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001964 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05301965 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301966#else
1967 struct custom_enc_cfg_t aptx_dsp_cfg;
1968 mixer_size = sizeof(struct custom_enc_cfg_t);
1969 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05301970 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05301971 if (ret) {
1972 is_configured = false;
1973 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301974 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001975 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1976 mixer_size);
1977#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301978 if (ret != 0) {
1979 ALOGE("%s: Failed to set APTX encoder config", __func__);
1980 is_configured = false;
1981 goto fail;
1982 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301983#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001984 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301985 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001986 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301987 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
1988 else
1989 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301990#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301991 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301992 is_configured = false;
1993 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301994 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301995 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301996 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05301997 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301998 else
Surendar Karka2febd452018-12-13 17:56:43 +05301999 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302000fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05302001 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002002 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05302003 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302004 return is_configured;
2005}
2006
2007/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302008 */
Manish Dewangan6a252632017-12-04 17:27:44 +05302009#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002010bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302011#else
2012bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2013#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302014{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302015 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302016 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302017 bool is_configured = false;
2018 int ret = 0;
2019
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002020 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302021 return false;
2022
2023 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2024 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002025 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302026 is_configured = false;
2027 goto fail;
2028 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302029
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302030 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002031 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302032 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2033 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2034 switch(aptx_dsp_cfg.num_channels) {
2035 case 1:
2036 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2037 break;
2038 case 2:
2039 default:
2040 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2041 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2042 break;
2043 }
2044 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302045 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302046 if (ret != 0) {
2047 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2048 is_configured = false;
2049 goto fail;
2050 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302051 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302052 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302053 is_configured = false;
2054 goto fail;
2055 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302056 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002057 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302058 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302059 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302060 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2061 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302062fail:
2063 return is_configured;
2064}
2065
2066/* API to configure AAC DSP encoder */
2067bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2068{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302069 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302070 struct aac_enc_cfg_t aac_dsp_cfg;
2071 bool is_configured = false;
2072 int ret = 0;
2073
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002074 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302075 return false;
2076
2077 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2078 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002079 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302080 is_configured = false;
2081 goto fail;
2082 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302083 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002084 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302085 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302086 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002087 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302088 case 0:
2089 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2090 break;
2091 case 2:
2092 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2093 break;
2094 case 1:
2095 default:
2096 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2097 break;
2098 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302099 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302100 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002101
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302102 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2103 sizeof(struct aac_enc_cfg_t));
2104 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002105 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302106 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302107 goto fail;
2108 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302109 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302110 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302111 is_configured = false;
2112 goto fail;
2113 }
2114 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002115 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302116 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002117 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002118 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2119 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2120fail:
2121 return is_configured;
2122}
2123
2124bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2125{
2126 struct mixer_ctl *ctl_enc_data = NULL;
2127 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2128 bool is_configured = false;
2129 int ret = 0;
2130
2131 if (aac_bt_cfg == NULL)
2132 return false;
2133
2134 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2135 if (!ctl_enc_data) {
2136 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2137 is_configured = false;
2138 goto fail;
2139 }
2140 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302141 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002142 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2143 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2144 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2145 case 0:
2146 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2147 break;
2148 case 2:
2149 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2150 break;
2151 case 1:
2152 default:
2153 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2154 break;
2155 }
2156 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2157 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2158 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2159 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2160
2161 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2162 sizeof(struct aac_enc_cfg_v2_t));
2163 if (ret != 0) {
2164 ALOGE("%s: Failed to set AAC encoder config", __func__);
2165 is_configured = false;
2166 goto fail;
2167 }
2168 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2169 if (ret != 0) {
2170 is_configured = false;
2171 goto fail;
2172 }
2173 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302174 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002175 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2176 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2177 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2178 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302179fail:
2180 return is_configured;
2181}
2182
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302183bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2184{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302185 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302186 struct celt_enc_cfg_t celt_dsp_cfg;
2187 bool is_configured = false;
2188 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002189 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302190 return false;
2191
2192 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2193 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002194 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302195 is_configured = false;
2196 goto fail;
2197 }
2198 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2199
Florian Pfister1a84f312018-07-19 14:38:18 +02002200 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302201 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2202 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2203 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2204 case 1:
2205 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2206 break;
2207 case 2:
2208 default:
2209 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2210 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2211 break;
2212 }
2213
2214 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2215
2216 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2217 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2218 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2219 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2220 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2221
2222 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2223 sizeof(struct celt_enc_cfg_t));
2224 if (ret != 0) {
2225 ALOGE("%s: Failed to set CELT encoder config", __func__);
2226 is_configured = false;
2227 goto fail;
2228 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302229 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302230 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302231 is_configured = false;
2232 goto fail;
2233 }
2234 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002235 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302236 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302237 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302238 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2239 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2240fail:
2241 return is_configured;
2242}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302243
2244bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2245{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302246 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302247 struct ldac_enc_cfg_t ldac_dsp_cfg;
2248 bool is_configured = false;
2249 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002250 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302251 return false;
2252
2253 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2254 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002255 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302256 is_configured = false;
2257 goto fail;
2258 }
2259 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2260
Florian Pfister1a84f312018-07-19 14:38:18 +02002261 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302262 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2263 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2264 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2265 case 4:
2266 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2267 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2268 break;
2269 case 2:
2270 case 1:
2271 default:
2272 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2273 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2274 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2275 break;
2276 }
2277
2278 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2279 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2280 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002281 if (ldac_bt_cfg->is_abr_enabled) {
2282 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2283 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2284 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2285 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2286 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002287 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002288 }
2289
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302290 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2291 sizeof(struct ldac_enc_cfg_t));
2292 if (ret != 0) {
2293 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2294 is_configured = false;
2295 goto fail;
2296 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302297 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302298 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302299 is_configured = false;
2300 goto fail;
2301 }
2302 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002303 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302304 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2305 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002306 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302307 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2308 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2309fail:
2310 return is_configured;
2311}
2312
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302313bool configure_a2dp_encoder_format()
2314{
2315 void *codec_info = NULL;
2316 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002317 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302318 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002319 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302320
Florian Pfister1a84f312018-07-19 14:38:18 +02002321 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302322 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2323 return false;
2324 }
2325 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002326 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302327 &codec_type);
2328
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002329 // ABR disabled by default for all codecs
2330 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302331 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002332
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302333 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002334 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302335 ALOGD(" Received SBC encoder supported BT device");
2336 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002337 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302338 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002339 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302340 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302341#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002342 a2dp.is_aptx_dual_mono_supported = false;
2343 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302344#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302345 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002346 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302347 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002348 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302349 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302350#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302351 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002352 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302353#else
2354 is_configured =
2355 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2356#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002357 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302358#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002359 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002360 ALOGD(" Received APTX dual mono encoder supported BT device");
2361 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302362 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2363 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002364 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2365 is_configured =
2366 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302367 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302368#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002369 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302370 ALOGD(" Received AAC encoder supported BT device");
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002371 bool is_aac_frame_ctl_enabled =
2372 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
2373 is_configured = is_aac_frame_ctl_enabled ?
2374 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2375 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302376 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002377 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302378 ALOGD(" Received CELT encoder supported BT device");
2379 is_configured =
2380 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2381 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002382 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302383 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002384 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2385 instance_id = MAX_INSTANCE_ID;
2386 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302387 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002388 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302389 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302390 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302391#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302392 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302393 ALOGD(" Received APTX AD encoder supported BT device");
2394 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2395 instance_id = MAX_INSTANCE_ID;
2396 a2dp.abr_config.imc_instance = instance_id--;
2397 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2398 a2dp.is_aptx_adaptive = true;
2399 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2400 is_configured =
2401 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302402 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302403 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302404#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002405 case CODEC_TYPE_PCM:
2406 ALOGD("Received PCM format for BT device");
2407 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2408 is_configured = true;
2409 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302410 default:
2411 ALOGD(" Received Unsupported encoder formar");
2412 is_configured = false;
2413 break;
2414 }
2415 return is_configured;
2416}
2417
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002418int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302419{
2420 int ret = 0;
2421
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002422 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302423
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002424 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002425 && a2dp.audio_get_enc_config)) {
2426 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302427 return -ENOSYS;
2428 }
2429
Zhou Song12c29502019-03-16 10:37:18 +08002430 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302431 //session will be restarted after suspend completion
2432 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302433 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302434 }
2435
Florian Pfister1a84f312018-07-19 14:38:18 +02002436 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302437 ALOGD("calling BT module stream start");
2438 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002439 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302440 ALOGE("BT controller start return = %d",ret);
2441 if (ret != 0 ) {
2442 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002443 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302444 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002445 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002446 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302447 ret = 0;
2448 ALOGD("Start playback successful to BT library");
2449 } else {
2450 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002451 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302452 ret = -ETIMEDOUT;
2453 }
2454 }
2455 }
2456
Florian Pfister1a84f312018-07-19 14:38:18 +02002457 if (a2dp.a2dp_source_started) {
2458 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302459 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302460 audio_a2dp_update_tws_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002461 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002462 if (a2dp.abr_config.is_abr_enabled)
2463 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302464 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302465
2466 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002467 a2dp.a2dp_source_total_active_session_requests);
2468 return ret;
2469}
2470
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002471uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002472{
2473 uint32_t latency = 0;
2474
2475 switch(a2dp.bt_decoder_format) {
2476 case CODEC_TYPE_SBC:
2477 latency = DEFAULT_SINK_LATENCY_SBC;
2478 break;
2479 case CODEC_TYPE_AAC:
2480 latency = DEFAULT_SINK_LATENCY_AAC;
2481 break;
2482 default:
2483 latency = 200;
2484 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2485 break;
2486 }
2487 return (uint64_t)latency;
2488}
2489
2490bool a2dp_send_sink_setup_complete(void) {
2491 uint64_t system_latency = 0;
2492 bool is_complete = false;
2493
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002494 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002495
2496 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2497 is_complete = true;
2498 }
2499 return is_complete;
2500}
2501
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002502bool a2dp_sink_is_ready()
2503{
2504 bool ret = false;
2505
2506 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2507 (a2dp.is_a2dp_offload_supported) &&
2508 (a2dp.audio_sink_check_a2dp_ready))
2509 ret = a2dp.audio_sink_check_a2dp_ready();
2510 return ret;
2511}
2512
2513int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002514{
2515 int ret = 0;
2516
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002517 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002518
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002519 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002520 && a2dp.audio_get_dec_config)) {
2521 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2522 return -ENOSYS;
2523 }
2524
2525 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2526 ALOGD("calling BT module stream start");
2527 /* This call indicates BT IPC lib to start capture */
2528 ret = a2dp.audio_sink_start();
2529 ALOGE("BT controller start capture return = %d",ret);
2530 if (ret != 0 ) {
2531 ALOGE("BT controller start capture failed");
2532 a2dp.a2dp_sink_started = false;
2533 } else {
2534
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002535 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002536 ALOGD("Wait for capture ready not successful");
2537 ret = -ETIMEDOUT;
2538 }
2539
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002540 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002541 a2dp.a2dp_sink_started = true;
2542 ret = 0;
2543 ALOGD("Start capture successful to BT library");
2544 } else {
2545 ALOGD(" unable to configure DSP decoder");
2546 a2dp.a2dp_sink_started = false;
2547 ret = -ETIMEDOUT;
2548 }
2549
2550 if (!a2dp_send_sink_setup_complete()) {
2551 ALOGD("sink_setup_complete not successful");
2552 ret = -ETIMEDOUT;
2553 }
2554 }
2555 }
2556
2557 if (a2dp.a2dp_sink_started) {
2558 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002559 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002560 }
2561 }
2562
2563 ALOGD("start A2DP sink total active sessions :%d",
2564 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302565 return ret;
2566}
2567
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302568static void reset_a2dp_enc_config_params()
2569{
2570 int ret =0;
2571
Aalique Grahame22e49102018-12-18 14:23:57 -08002572 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302573 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302574 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302575
2576 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
2577 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
2578 MIXER_ENC_CONFIG_BLOCK);
2579 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002580 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302581 } else {
2582 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
2583 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002584 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302585 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002586
2587 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2588
Manisha Agarwala51768b2018-11-01 16:30:52 +05302589 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2590
2591 if (!ctl_channel_mode) {
2592 ALOGE("failed to get tws mixer ctl");
2593 } else {
2594 channel_mode = "Two";
2595 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2596 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2597 }
2598 a2dp.is_tws_mono_mode_on = false;
2599 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302600}
2601
Surendar Karka2febd452018-12-13 17:56:43 +05302602static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002603{
2604 struct mixer_ctl *ctl_dec_data = NULL;
2605 struct abr_dec_cfg_t dummy_reset_cfg;
2606 int ret = 0;
2607
Preetam Singh Ranawat6459c142019-07-31 18:15:57 +05302608 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
2609 if (!ctl_dec_data) {
2610 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
2611 return -EINVAL;
2612 }
2613 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
2614 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
2615 sizeof(dummy_reset_cfg));
2616 if (ret != 0) {
2617 ALOGE("%s: Failed to set dummy decoder config", __func__);
2618 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002619 }
2620
2621 return ret;
2622}
2623
Surendar Karka2febd452018-12-13 17:56:43 +05302624static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02002625{
2626 int ret =0;
2627
2628 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
2629 struct aac_dec_cfg_t dummy_reset_config;
2630
2631 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
2632 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05302633 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02002634 if (!ctl_dec_config) {
2635 ALOGE(" ERROR a2dp decoder format mixer control not identified");
2636 } else {
2637 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
2638 sizeof(struct aac_dec_cfg_t));
2639 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
2640 }
2641 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
2642 MIXER_DEC_BIT_FORMAT);
2643 if (!ctrl_bit_format) {
2644 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
2645 } else {
2646 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
2647 if (ret != 0) {
2648 ALOGE("%s: Failed to set bit format to decoder", __func__);
2649 }
2650 }
2651}
2652
Zhou Song12c29502019-03-16 10:37:18 +08002653static void reset_codec_config()
2654{
2655 reset_a2dp_enc_config_params();
2656 reset_a2dp_source_dec_config_params();
2657 a2dp_reset_backend_cfg(SOURCE);
2658 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
2659 stop_abr();
2660 a2dp.abr_config.is_abr_enabled = false;
2661}
2662
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002663int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302664{
2665 int ret =0;
2666
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002667 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002668 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002669 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302670 return -ENOSYS;
2671 }
2672
Florian Pfister1a84f312018-07-19 14:38:18 +02002673 if (a2dp.a2dp_source_total_active_session_requests > 0)
2674 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08002675 else
2676 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302677
Florian Pfister1a84f312018-07-19 14:38:18 +02002678 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302679 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02002680 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302681 if (ret < 0)
2682 ALOGE("stop stream to BT IPC lib failed");
2683 else
2684 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08002685 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
2686 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05302687 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302688 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002689 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002690 a2dp.a2dp_source_started = false;
2691 ALOGD("Stop A2DP playback, total active sessions :%d",
2692 a2dp.a2dp_source_total_active_session_requests);
2693 return 0;
2694}
2695
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002696int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002697{
2698 int ret =0;
2699
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002700 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002701 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002702 ALOGE("a2dp handle is not identified, Ignoring stop request");
2703 return -ENOSYS;
2704 }
2705
2706 if (a2dp.a2dp_sink_total_active_session_requests > 0)
2707 a2dp.a2dp_sink_total_active_session_requests--;
2708
2709 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2710 ALOGV("calling BT module stream stop");
2711 ret = a2dp.audio_sink_stop();
2712 if (ret < 0)
2713 ALOGE("stop stream to BT IPC lib failed");
2714 else
2715 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05302716 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002717 a2dp_reset_backend_cfg(SINK);
2718 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002719 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002720 a2dp.a2dp_source_started = false;
2721 ALOGD("Stop A2DP capture, total active sessions :%d",
2722 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302723 return 0;
2724}
2725
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002726int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302727{
Aalique Grahame22e49102018-12-18 14:23:57 -08002728 int ret = 0, val, status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302729 char value[32]={0};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302730 struct audio_usecase *uc_info;
2731 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302732
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002733 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002734 ALOGV("no supported encoders identified,ignoring a2dp setparam");
2735 status = -EINVAL;
2736 goto param_handled;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302737 }
2738
2739 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
2740 sizeof(value));
Zhou Song681350a2017-10-19 16:28:42 +08002741 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302742 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002743 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002744 ALOGV("Received device connect request for A2DP source");
2745 open_a2dp_source();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302746 }
2747 goto param_handled;
2748 }
2749
2750 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
2751 sizeof(value));
2752
Zhou Song681350a2017-10-19 16:28:42 +08002753 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302754 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002755 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002756 ALOGV("Received source device dis- connect request");
Samyak Jain4828f4c2018-08-24 16:31:06 +05302757 close_a2dp_output();
kunleiz4a1fad62018-02-08 18:00:16 +08002758 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302759 reset_a2dp_source_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002760 a2dp_reset_backend_cfg(SOURCE);
2761 } else if (audio_is_a2dp_in_device(val)) {
2762 ALOGV("Received sink device dis- connect request");
2763 close_a2dp_input();
Surendar Karka2febd452018-12-13 17:56:43 +05302764 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002765 a2dp_reset_backend_cfg(SINK);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302766 }
2767 goto param_handled;
2768 }
Surendar Karka2febd452018-12-13 17:56:43 +05302769#ifndef LINUX_ENABLED
Manisha Agarwala51768b2018-11-01 16:30:52 +05302770 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
2771 if (ret>=0) {
2772 ALOGD("Setting tws channel mode to %s",value);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002773 if (!(strncmp(value,"mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302774 a2dp.is_tws_mono_mode_on = true;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002775 else if (!(strncmp(value,"dual-mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302776 a2dp.is_tws_mono_mode_on = false;
2777 audio_a2dp_update_tws_channel_mode();
2778 goto param_handled;
2779 }
Surendar Karka2febd452018-12-13 17:56:43 +05302780#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302781 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
2782 if (ret >= 0) {
Surendar Karka2febd452018-12-13 17:56:43 +05302783 if (a2dp.bt_lib_source_handle) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302784 if ((!strncmp(value,"true",sizeof(value)))) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002785 if (a2dp.a2dp_source_suspended) {
2786 ALOGD("%s: A2DP is already suspended", __func__);
2787 goto param_handled;
2788 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302789 ALOGD("Setting a2dp to suspend state");
Florian Pfister1a84f312018-07-19 14:38:18 +02002790 a2dp.a2dp_source_suspended = true;
Surendar Karka2febd452018-12-13 17:56:43 +05302791 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
yidongh7203cca2018-09-19 16:12:25 +08002792 goto param_handled;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302793 list_for_each(node, &a2dp.adev->usecase_list) {
2794 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302795 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08002796 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302797 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002798 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302799 pthread_mutex_lock(&a2dp.adev->lock);
2800 }
2801 }
Zhou Song12c29502019-03-16 10:37:18 +08002802 if (!a2dp.swb_configured)
2803 reset_codec_config();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002804 if (a2dp.audio_source_suspend)
Florian Pfister1a84f312018-07-19 14:38:18 +02002805 a2dp.audio_source_suspend();
2806 } else if (a2dp.a2dp_source_suspended == true) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302807 ALOGD("Resetting a2dp suspend state");
Zhou Song10617ed2017-05-26 13:28:48 +08002808 struct audio_usecase *uc_info;
2809 struct listnode *node;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002810 if (a2dp.clear_source_a2dpsuspend_flag)
Florian Pfister1a84f312018-07-19 14:38:18 +02002811 a2dp.clear_source_a2dpsuspend_flag();
2812 a2dp.a2dp_source_suspended = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302813 /*
2814 * It is possible that before suspend,a2dp sessions can be active
2815 * for example during music + voice activation concurrency
2816 * a2dp suspend will be called & BT will change to sco mode
2817 * though music is paused as a part of voice activation
2818 * compress session close happens only after pause timeout(10secs)
2819 * so if resume request comes before pause timeout as a2dp session
2820 * is already active IPC start will not be called from APM/audio_hw
2821 * Fix is to call a2dp start for IPC library post suspend
2822 * based on number of active session count
2823 */
Florian Pfister1a84f312018-07-19 14:38:18 +02002824 if (a2dp.a2dp_source_total_active_session_requests > 0) {
Naresh Tanniru649871a2016-11-04 18:08:32 +05302825 ALOGD(" Calling IPC lib start post suspend state");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002826 if (a2dp.audio_source_start) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002827 ret = a2dp.audio_source_start();
Naresh Tanniru649871a2016-11-04 18:08:32 +05302828 if (ret != 0) {
2829 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002830 a2dp.a2dp_source_started = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302831 }
2832 }
2833 }
Zhou Song10617ed2017-05-26 13:28:48 +08002834 list_for_each(node, &a2dp.adev->usecase_list) {
2835 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302836 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08002837 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302838 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002839 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302840 pthread_mutex_lock(&a2dp.adev->lock);
2841 }
Zhou Song10617ed2017-05-26 13:28:48 +08002842 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302843 }
2844 }
2845 goto param_handled;
2846 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002847
2848 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
2849 sizeof(value));
2850 if (ret >= 0) {
2851 if (a2dp.is_a2dp_offload_supported &&
2852 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2853 *reconfig = true;
2854 }
2855 goto param_handled;
2856 }
2857
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302858param_handled:
2859 ALOGV("end of a2dp setparam");
Aalique Grahame22e49102018-12-18 14:23:57 -08002860 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302861}
2862
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002863void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302864{
2865 a2dp.is_handoff_in_progress = is_on;
2866}
2867
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002868bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302869{
2870 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05302871 // Or if a2dp device is selected but earlier start failed ( as a2dp
2872 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02002873 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302874}
2875
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002876void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302877{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302878 *sample_rate = a2dp.enc_sampling_rate;
2879}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302880
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002881void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02002882{
2883 *sample_rate = a2dp.dec_sampling_rate;
2884}
2885
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002886bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302887{
2888 bool ret = false;
2889
Florian Pfister1a84f312018-07-19 14:38:18 +02002890 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302891 return ret;
2892
Florian Pfister1a84f312018-07-19 14:38:18 +02002893 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08002894 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002895 (a2dp.audio_source_check_a2dp_ready))
2896 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302897 return ret;
2898}
2899
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002900bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02002901{
2902 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302903}
2904
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002905void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08002906 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302907{
2908 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02002909 a2dp.bt_lib_source_handle = NULL;
2910 a2dp.a2dp_source_started = false;
2911 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
2912 a2dp.a2dp_source_total_active_session_requests = 0;
2913 a2dp.a2dp_source_suspended = false;
2914 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302915 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302916 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002917 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302918 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002919 a2dp.abr_config.is_abr_enabled = false;
2920 a2dp.abr_config.abr_started = false;
2921 a2dp.abr_config.imc_instance = 0;
2922 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08002923 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302924 a2dp.is_tws_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05302925 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08002926 a2dp.swb_configured = false;
2927
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002928 // init function pointers
2929 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08002930 init_config.fp_platform_get_pcm_device_id;
2931 fp_check_a2dp_restore = init_config.fp_check_a2dp_restore;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002932
kunleiz5a127262017-09-08 14:47:48 +08002933 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302934 reset_a2dp_source_dec_config_params();
2935 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002936
2937 a2dp.bt_lib_sink_handle = NULL;
2938 a2dp.a2dp_sink_started = false;
2939 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
2940 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002941
2942 if (is_running_with_enhanced_fwk == UNINITIALIZED)
2943 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
2944 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08002945 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02002946
2947 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302948 update_offload_codec_capabilities();
2949}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002950
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002951uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002952{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002953 uint32_t latency = 0;
2954 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302955 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
2956 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002957 char value[PROPERTY_VALUE_MAX];
2958
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002959 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08002960 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002961 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302962 if (sscanf(value, "%d/%d/%d/%d/%d%d",
2963 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002964 ALOGI("Failed to parse avsync offset params from '%s'.", value);
2965 avsync_runtime_prop = 0;
2966 }
2967 }
2968
yidongh0515e042017-07-06 15:00:34 +08002969 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02002970 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2971 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08002972 }
2973
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07002974 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002975 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08002976 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
2977 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002978 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002979 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08002980 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
2981 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002982 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002983 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08002984 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
2985 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002986 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002987 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08002988 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
2989 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002990 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002991 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302992 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
2993 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
2994 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002995 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302996 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
2997 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
2998 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05302999 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05303000 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05303001 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08003002 case CODEC_TYPE_PCM:
3003 latency = ENCODER_LATENCY_PCM;
3004 latency += DEFAULT_SINK_LATENCY_PCM;
3005 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003006 default:
3007 latency = 200;
3008 break;
3009 }
3010 return latency;
3011}
Aalique Grahame22e49102018-12-18 14:23:57 -08003012
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003013int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003014 struct str_parms *reply)
3015{
3016 int ret, val = 0;
3017 char value[32]={0};
3018
3019 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3020 value, sizeof(value));
3021 if (ret >= 0) {
3022 val = a2dp.is_a2dp_offload_supported;
3023 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3024 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3025 }
3026
3027 return 0;
3028}
Zhou Song12c29502019-03-16 10:37:18 +08003029
3030
3031bool configure_aptx_ad_speech_enc_fmt() {
3032 struct mixer_ctl *ctl_enc_data = NULL;
3033 int mixer_size = 0;
3034 int ret = 0;
3035 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3036
3037 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3038 if (!ctl_enc_data) {
3039 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3040 return false;
3041 }
3042
3043 /* Initialize dsp configuration params */
3044 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3045 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3046 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3047 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3048 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3049 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3050 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3051 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3052 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3053 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3054 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3055
3056 /* Configure AFE DSP configuration */
3057 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3058 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3059 mixer_size);
3060 if (ret != 0) {
3061 ALOGE("%s: Failed to set SWB encoder config", __func__);
3062 return false;
3063 }
3064
3065 /* Configure AFE Input Bit Format as PCM_16 */
3066 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3067 if (ret != 0) {
3068 ALOGE("%s: Failed to set SWB bit format", __func__);
3069 return false;
3070 }
3071
3072 return true;
3073}
3074
3075bool configure_aptx_ad_speech_dec_fmt()
3076{
3077 struct mixer_ctl *ctl_dec_data = NULL;
3078 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3079 int ret = 0;
3080
3081 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3082 if (!ctl_dec_data) {
3083 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3084 return false;
3085 }
3086 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3087 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3088 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3089 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3090 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3091 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3092 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3093 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3094
3095 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3096 sizeof(dec_cfg));
3097 if (ret != 0) {
3098 ALOGE("%s: Failed to set decoder config", __func__);
3099 return false;
3100 }
3101 return true;
3102}
3103
3104int sco_start_configuration()
3105{
3106 ALOGD("sco_start_configuration start");
3107
3108 if (!a2dp.swb_configured) {
3109 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3110 /* Configure AFE codec*/
3111 if (configure_aptx_ad_speech_enc_fmt() &&
3112 configure_aptx_ad_speech_dec_fmt()) {
3113 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3114 } else {
3115 ALOGE("%s: failed to send SCO configuration", __func__);
3116 return -ETIMEDOUT;
3117 }
3118 /* Configure backend*/
3119 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3120 a2dp.enc_channels = CH_MONO;
3121 a2dp.abr_config.is_abr_enabled = true;
3122 a2dp_set_backend_cfg(SOURCE);
3123 /* Start abr*/
3124 start_abr();
3125 a2dp.swb_configured = true;
3126 }
3127 return 0;
3128}
3129
3130void sco_reset_configuration()
3131{
Zhou Songd6d71752019-05-21 18:08:51 +08003132 if (a2dp.swb_configured) {
3133 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003134
Zhou Songd6d71752019-05-21 18:08:51 +08003135 reset_codec_config();
3136 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3137 a2dp.swb_configured = false;
3138 }
Zhou Song12c29502019-03-16 10:37:18 +08003139}