blob: 035252340d7cee27154eb76fe06bf75136ee7ba1 [file] [log] [blame]
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301/*
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002* Copyright (c) 2015-2020, The Linux Foundation. All rights reserved.
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of The Linux Foundation nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
Aalique Grahame22e49102018-12-18 14:23:57 -080029#define LOG_TAG "a2dp_offload"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053030/*#define LOG_NDEBUG 0*/
31#define LOG_NDDEBUG 0
32#include <errno.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080033#include <log/log.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053034#include <dlfcn.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080035#include <pthread.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
Manish Dewangan6a252632017-12-04 17:27:44 +053039#include "audio_extn.h"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053040#include <stdlib.h>
41#include <cutils/str_parms.h>
42#include <hardware/audio.h>
43#include <hardware/hardware.h>
44#include <cutils/properties.h>
45
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046#ifdef DYNAMIC_LOG_ENABLED
47#include <log_xml_parser.h>
48#define LOG_MASK HAL_MOD_FILE_A2DP
49#include <log_utils.h>
50#endif
51
Naresh Tanniru66cf06c2019-03-20 19:30:37 +053052#define BT_IPC_SOURCE_LIB_NAME "btaudio_offload_if.so"
Florian Pfister1a84f312018-07-19 14:38:18 +020053#define BT_IPC_SINK_LIB_NAME "libbthost_if_sink.so"
54#define MEDIA_FMT_NONE 0
55#define MEDIA_FMT_AAC 0x00010DA6
56#define MEDIA_FMT_APTX 0x000131ff
57#define MEDIA_FMT_APTX_HD 0x00013200
Surendar Karka2febd452018-12-13 17:56:43 +053058#define MEDIA_FMT_APTX_AD 0x00013204
Florian Pfister1a84f312018-07-19 14:38:18 +020059#define MEDIA_FMT_SBC 0x00010BF2
60#define MEDIA_FMT_CELT 0x00013221
61#define MEDIA_FMT_LDAC 0x00013224
62#define MEDIA_FMT_MP3 0x00010BE9
63#define MEDIA_FMT_APTX_ADAPTIVE 0x00013204
Zhou Song12c29502019-03-16 10:37:18 +080064#define MEDIA_FMT_APTX_AD_SPEECH 0x00013208
Naresh Tanniru9d027a62015-03-13 01:32:10 +053065#define MEDIA_FMT_AAC_AOT_LC 2
66#define MEDIA_FMT_AAC_AOT_SBR 5
67#define MEDIA_FMT_AAC_AOT_PS 29
Naresh Tanniru9d027a62015-03-13 01:32:10 +053068#define PCM_CHANNEL_L 1
69#define PCM_CHANNEL_R 2
70#define PCM_CHANNEL_C 3
71#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
72#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
73#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
74#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
75#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
76#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
Surendar Karka2febd452018-12-13 17:56:43 +053077#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
Zhou Song8fccbb62019-03-20 01:08:19 +080078#define MIXER_ENC_APTX_AD_CONFIG_BLOCK "SLIM_7_RX APTX_AD Enc Cfg"
Surendar Karka2febd452018-12-13 17:56:43 +053079#define MIXER_SOURCE_DEC_CONFIG_BLOCK "SLIM_7_TX Decoder Config"
80#define MIXER_SINK_DEC_CONFIG_BLOCK "SLIM_9_TX Decoder Config"
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +053081#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
Florian Pfister1a84f312018-07-19 14:38:18 +020082#define MIXER_DEC_BIT_FORMAT "AFE Output Bit Format"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053083#define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080084#define MIXER_SAMPLE_RATE_RX "BT SampleRate RX"
Surendar Karka2febd452018-12-13 17:56:43 +053085#define MIXER_SOURCE_SAMPLE_RATE_TX "BT SampleRate TX"
Samyak Jainf69e9ef2018-06-12 12:26:37 +053086#define MIXER_SAMPLE_RATE_DEFAULT "BT SampleRate"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053087#define MIXER_AFE_IN_CHANNELS "AFE Input Channels"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080088#define MIXER_ABR_TX_FEEDBACK_PATH "A2DP_SLIM7_UL_HL Switch"
Zhou Song12c29502019-03-16 10:37:18 +080089#define MIXER_ABR_RX_FEEDBACK_PATH "SCO_SLIM7_DL_HL Switch"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080090#define MIXER_SET_FEEDBACK_CHANNEL "BT set feedback channel"
Surendar Karka2febd452018-12-13 17:56:43 +053091#define MIXER_SINK_SAMPLE_RATE "BT_TX SampleRate"
Florian Pfister1a84f312018-07-19 14:38:18 +020092#define MIXER_AFE_SINK_CHANNELS "AFE Output Channels"
Manisha Agarwala51768b2018-11-01 16:30:52 +053093#define MIXER_FMT_TWS_CHANNEL_MODE "TWS Channel Mode"
yidongh0515e042017-07-06 15:00:34 +080094#define ENCODER_LATENCY_SBC 10
95#define ENCODER_LATENCY_APTX 40
96#define ENCODER_LATENCY_APTX_HD 20
97#define ENCODER_LATENCY_AAC 70
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +053098//To Do: Fine Tune Encoder CELT/LDAC latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +053099#define ENCODER_LATENCY_CELT 40
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530100#define ENCODER_LATENCY_LDAC 40
Aalique Grahame22e49102018-12-18 14:23:57 -0800101#define ENCODER_LATENCY_PCM 50
yidongh0515e042017-07-06 15:00:34 +0800102#define DEFAULT_SINK_LATENCY_SBC 140
103#define DEFAULT_SINK_LATENCY_APTX 160
104#define DEFAULT_SINK_LATENCY_APTX_HD 180
105#define DEFAULT_SINK_LATENCY_AAC 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530106//To Do: Fine Tune Default CELT/LDAC Latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530107#define DEFAULT_SINK_LATENCY_CELT 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530108#define DEFAULT_SINK_LATENCY_LDAC 180
Aalique Grahame22e49102018-12-18 14:23:57 -0800109#define DEFAULT_SINK_LATENCY_PCM 140
110
111#define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported"
112#define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled"
113#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
114
115// Default encoder bit width
116#define DEFAULT_ENCODER_BIT_FORMAT 16
117
118// Default encoder latency
119#define DEFAULT_ENCODER_LATENCY 200
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530120
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800121// Slimbus Tx sample rate for ABR feedback channel
122#define ABR_TX_SAMPLE_RATE "KHZ_8"
123
Zhou Song12c29502019-03-16 10:37:18 +0800124// Slimbus Tx sample rate for APTX AD SPEECH
125#define SPEECH_TX_SAMPLE_RATE "KHZ_96"
126
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800127// Purpose ID for Inter Module Communication (IMC) in AFE
128#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
129
130// Maximum quality levels for ABR
131#define MAX_ABR_QUALITY_LEVELS 5
132
133// Instance identifier for A2DP
134#define MAX_INSTANCE_ID (UINT32_MAX / 2)
135
Zhou Song12c29502019-03-16 10:37:18 +0800136// Instance identifier for SWB
137#define APTX_AD_SPEECH_INSTANCE_ID 37
138
139#define SAMPLING_RATE_96K 96000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530140#define SAMPLING_RATE_48K 48000
141#define SAMPLING_RATE_441K 44100
Zhou Song12c29502019-03-16 10:37:18 +0800142#define SAMPLING_RATE_32K 32000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530143#define CH_STEREO 2
144#define CH_MONO 1
Florian Pfister1a84f312018-07-19 14:38:18 +0200145#define SOURCE 0
146#define SINK 1
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800147#define UNINITIALIZED -1
148
149#ifdef __LP64__
150#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
151#else
152#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
153#endif
154
155static void *vndk_fwk_lib_handle = NULL;
156static int is_running_with_enhanced_fwk = UNINITIALIZED;
157
158typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
159static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
Florian Pfister1a84f312018-07-19 14:38:18 +0200160
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530161/*
162 * Below enum values are extended from audio_base.h to
Florian Pfister1a84f312018-07-19 14:38:18 +0200163 * to keep encoder and decoder type local to bthost_ipc
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530164 * and audio_hal as these are intended only for handshake
165 * between IPC lib and Audio HAL.
166 */
167typedef enum {
Florian Pfister1a84f312018-07-19 14:38:18 +0200168 CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
169 CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
170 CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
171 CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
172 CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530173#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +0200174 CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530175#endif
Florian Pfister1a84f312018-07-19 14:38:18 +0200176 CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
177 CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
Surendar Karka2febd452018-12-13 17:56:43 +0530178 CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
Zhou Song12c29502019-03-16 10:37:18 +0800179 CODEC_TYPE_APTX_AD_SPEECH = 637534208u, //0x26000000UL
Aalique Grahame22e49102018-12-18 14:23:57 -0800180 CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u
Florian Pfister1a84f312018-07-19 14:38:18 +0200181}codec_t;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530182
Sharad Sangle95d451b2018-06-19 12:24:20 +0530183/*
184 * enums which describes the APTX Adaptive
185 * channel mode, these values are used by encoder
186 */
187 typedef enum {
188 APTX_AD_CHANNEL_UNCHANGED = -1,
189 APTX_AD_CHANNEL_JOINT_STEREO = 0, // default
190 APTX_AD_CHANNEL_MONO = 1,
191 APTX_AD_CHANNEL_DUAL_MONO = 2,
192 APTX_AD_CHANNEL_STEREO_TWS = 4,
193 APTX_AD_CHANNEL_EARBUD = 8,
194} enc_aptx_ad_channel_mode;
195
196/*
197 * enums which describes the APTX Adaptive
198 * sampling frequency, these values are used
199 * by encoder
200 */
201typedef enum {
202 APTX_AD_SR_UNCHANGED = 0x0,
203 APTX_AD_48 = 0x1, // 48 KHz default
204 APTX_AD_44_1 = 0x2, // 44.1kHz
Zhou Song30e8cea2019-10-22 23:39:25 +0800205 APTX_AD_96 = 0x3, // 96KHz
Sharad Sangle95d451b2018-06-19 12:24:20 +0530206} enc_aptx_ad_s_rate;
207
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530208typedef void (*bt_audio_pre_init_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200209typedef int (*audio_source_open_t)(void);
210typedef int (*audio_source_close_t)(void);
211typedef int (*audio_source_start_t)(void);
212typedef int (*audio_source_stop_t)(void);
213typedef int (*audio_source_suspend_t)(void);
214typedef void (*audio_source_handoff_triggered_t)(void);
215typedef void (*clear_source_a2dpsuspend_flag_t)(void);
216typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
217 uint8_t *num_dev, codec_t *codec_type);
218typedef int (*audio_source_check_a2dp_ready_t)(void);
219typedef int (*audio_is_source_scrambling_enabled_t)(void);
Manisha Agarwala51768b2018-11-01 16:30:52 +0530220typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200221typedef int (*audio_sink_start_t)(void);
222typedef int (*audio_sink_stop_t)(void);
223typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
224typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
225typedef int (*audio_sink_check_a2dp_ready_t)(void);
226typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530227
228enum A2DP_STATE {
229 A2DP_STATE_CONNECTED,
230 A2DP_STATE_STARTED,
231 A2DP_STATE_STOPPED,
232 A2DP_STATE_DISCONNECTED,
233};
234
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800235typedef enum {
236 IMC_TRANSMIT,
237 IMC_RECEIVE,
238} imc_direction_t;
239
240typedef enum {
241 IMC_DISABLE,
242 IMC_ENABLE,
243} imc_status_t;
244
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700245typedef enum {
Zhou Song12c29502019-03-16 10:37:18 +0800246 SWAP_DISABLE,
247 SWAP_ENABLE,
248} swap_status_t;
249
250typedef enum {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700251 MTU_SIZE,
252 PEAK_BIT_RATE,
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530253 BIT_RATE_MODE,
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700254} frame_control_type_t;
255
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800256// --- external function dependency ---
257fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
Weiyin Jiang280ea742020-09-08 20:28:22 +0800258fp_check_a2dp_restore_t fp_check_a2dp_restore_l;
Gautam Manamfbb3ebc2020-10-08 18:06:45 +0530259fp_platform_switch_voice_call_device_post_t fp_platform_switch_voice_call_device_post;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800260
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800261/* PCM config for ABR Feedback hostless front end */
262static struct pcm_config pcm_config_abr = {
263 .channels = 1,
264 .rate = 8000,
265 .period_size = 240,
266 .period_count = 2,
267 .format = PCM_FORMAT_S16_LE,
268 .start_threshold = 0,
269 .stop_threshold = INT_MAX,
270 .avail_min = 0,
271};
272
273/* Adaptive bitrate config for A2DP codecs */
274struct a2dp_abr_config {
275 /* Flag to denote whether Adaptive bitrate is enabled for codec */
276 bool is_abr_enabled;
277 /* Flag to denote whether front end has been opened for ABR */
278 bool abr_started;
279 /* ABR Tx path pcm handle */
280 struct pcm *abr_tx_handle;
Zhou Song12c29502019-03-16 10:37:18 +0800281 /* ABR Rx path pcm handle */
282 struct pcm *abr_rx_handle;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800283 /* ABR Inter Module Communication (IMC) instance ID */
284 uint32_t imc_instance;
285};
286
287static uint32_t instance_id = MAX_INSTANCE_ID;
288
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530289/* structure used to update a2dp state machine
290 * to communicate IPC library
291 * to store DSP encoder configuration information
292 */
293struct a2dp_data {
294 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200295 void *bt_lib_source_handle;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530296 bt_audio_pre_init_t bt_audio_pre_init;
Florian Pfister1a84f312018-07-19 14:38:18 +0200297 audio_source_open_t audio_source_open;
298 audio_source_close_t audio_source_close;
299 audio_source_start_t audio_source_start;
300 audio_source_stop_t audio_source_stop;
301 audio_source_suspend_t audio_source_suspend;
302 audio_source_handoff_triggered_t audio_source_handoff_triggered;
303 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
304 audio_get_enc_config_t audio_get_enc_config;
305 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530306 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200307 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
308 enum A2DP_STATE bt_state_source;
309 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530310 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530311 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200312 bool a2dp_source_started;
313 bool a2dp_source_suspended;
314 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530315 bool is_a2dp_offload_supported;
316 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700317 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530318 /* Mono Mode support for TWS+ */
319 bool is_tws_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530320 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800321 /* Adaptive bitrate config for A2DP codecs */
322 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200323
324 void *bt_lib_sink_handle;
325 audio_sink_start_t audio_sink_start;
326 audio_sink_stop_t audio_sink_stop;
327 audio_get_dec_config_t audio_get_dec_config;
328 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
329 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
330 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
331 enum A2DP_STATE bt_state_sink;
332 codec_t bt_decoder_format;
333 uint32_t dec_sampling_rate;
334 uint32_t dec_channels;
335 bool a2dp_sink_started;
336 int a2dp_sink_total_active_session_requests;
Zhou Song12c29502019-03-16 10:37:18 +0800337 bool swb_configured;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530338};
339
340struct a2dp_data a2dp;
341
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800342/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
343 * Structures sent to configure DSP for ABR are defined below.
344 * This data helps DSP configure feedback path (BTSoC to LPASS)
345 * for link quality levels and mapping quality levels to codec
346 * specific bitrate.
347 */
348
349/* Key value pair for link quality level to bitrate mapping. */
350struct bit_rate_level_map_t {
351 uint32_t link_quality_level;
352 uint32_t bitrate;
353};
354
355/* Link quality level to bitrate mapping info sent to DSP. */
356struct quality_level_to_bitrate_info {
357 /* Number of quality levels being mapped.
358 * This will be equal to the size of mapping table.
359 */
360 uint32_t num_levels;
361 /* Quality level to bitrate mapping table */
362 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
363};
364
365/* Structure to set up Inter Module Communication (IMC) between
366 * AFE Decoder and Encoder.
367 */
368struct imc_dec_enc_info {
369 /* Decoder to encoder communication direction.
370 * Transmit = 0 / Receive = 1
371 */
372 uint32_t direction;
373 /* Enable / disable IMC between decoder and encoder */
374 uint32_t enable;
375 /* Purpose of IMC being set up between decoder and encoder.
376 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
377 * is the default value to be sent as purpose.
378 */
379 uint32_t purpose;
380 /* Unique communication instance ID.
381 * purpose and comm_instance together form the actual key
382 * used in IMC registration, which must be the same for
383 * encoder and decoder for which IMC is being set up.
384 */
385 uint32_t comm_instance;
386};
387
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700388/* Structure to control frame size of AAC encoded frames. */
389struct aac_frame_size_control_t {
390 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
391 uint32_t ctl_type;
392 /* Control value
393 * MTU_SIZE: MTU size in bytes
394 * PEAK_BIT_RATE: Peak bitrate in bits per second.
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530395 * BIT_RATE_MODE: Variable bitrate
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700396 */
397 uint32_t ctl_value;
398};
399
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800400/* Structure used for ABR config of AFE encoder and decoder. */
401struct abr_enc_cfg_t {
402 /* Link quality level to bitrate mapping info sent to DSP. */
403 struct quality_level_to_bitrate_info mapping_info;
404 /* Information to set up IMC between decoder and encoder */
405 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700406 /* Flag to indicate whether ABR is enabled */
407 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800408} __attribute__ ((packed));
409
410/* Structure to send configuration for decoder introduced
411 * on AFE Tx path for ABR link quality feedback to BT encoder.
412 */
413struct abr_dec_cfg_t {
414 /* Decoder media format */
415 uint32_t dec_format;
416 /* Information to set up IMC between decoder and encoder */
417 struct imc_dec_enc_info imc_info;
418} __attribute__ ((packed));
419
Zhou Song12c29502019-03-16 10:37:18 +0800420struct aptx_ad_speech_mode_cfg_t
421{
422 uint32_t mode;
423 uint32_t swapping;
424} __attribute__ ((packed));
425
426/* Structure for SWB voice dec config */
427struct aptx_ad_speech_dec_cfg_t {
428 struct abr_dec_cfg_t abr_cfg;
429 struct aptx_ad_speech_mode_cfg_t speech_mode;
430} __attribute__ ((packed));
431
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530432/* START of DSP configurable structures
433 * These values should match with DSP interface defintion
434 */
435
436/* AAC encoder configuration structure. */
437typedef struct aac_enc_cfg_t aac_enc_cfg_t;
438
439/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
440 * supported aac_fmt_flag are ADTS/RAW
441 * supported channel_cfg are Native mode, Mono , Stereo
442 */
443struct aac_enc_cfg_t {
444 uint32_t enc_format;
445 uint32_t bit_rate;
446 uint32_t enc_mode;
447 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530448 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530449 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530450} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530451
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700452struct aac_enc_cfg_v2_t {
453 struct aac_enc_cfg_t aac_enc_cfg;
454 struct aac_frame_size_control_t frame_ctl;
455} __attribute__ ((packed));
456
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530457struct aac_enc_cfg_v3_t {
458 struct aac_enc_cfg_t aac_enc_cfg;
459 struct aac_frame_size_control_t frame_ctl;
460 struct aac_frame_size_control_t aac_key_value_ctl;
461} __attribute__ ((packed));
462
Surendar Karkabbb3c822018-11-12 13:00:38 +0530463typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
464struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200465 uint16_t aac_fmt_flag; /* LATM*/
466 uint16_t audio_object_type; /* LC */
467 uint16_t channels; /* Stereo */
468 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
469 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
470 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530471} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200472
Surendar Karkabbb3c822018-11-12 13:00:38 +0530473typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
474struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200475 uint16_t channels; /* Mono, Stereo */
476 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
477 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530478} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200479
480/* AAC decoder configuration structure. */
481typedef struct aac_dec_cfg_t aac_dec_cfg_t;
482struct aac_dec_cfg_t {
483 uint32_t dec_format;
484 audio_aac_decoder_config_t data;
485} __attribute__ ((packed));
486
487/* SBC decoder configuration structure. */
488typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
489struct sbc_dec_cfg_t {
490 uint32_t dec_format;
491 audio_sbc_decoder_config_t data;
492} __attribute__ ((packed));
493
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530494/* SBC encoder configuration structure. */
495typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
496
497/* supported num_subbands are 4/8
498 * supported blk_len are 4, 8, 12, 16
499 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
500 * supported alloc_method are LOUNDNESS/SNR
501 * supported bit_rate for mono channel is max 320kbps
502 * supported bit rate for stereo channel is max 512 kbps
503 */
504struct sbc_enc_cfg_t{
505 uint32_t enc_format;
506 uint32_t num_subbands;
507 uint32_t blk_len;
508 uint32_t channel_mode;
509 uint32_t alloc_method;
510 uint32_t bit_rate;
511 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530512} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530513
514
515/* supported num_channels are Mono/Stereo
516 * supported channel_mapping for mono is CHANNEL_C
517 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
518 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700519 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530520struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530521{
522 uint32_t enc_format;
523 uint32_t sample_rate;
524 uint16_t num_channels;
525 uint16_t reserved;
526 uint8_t channel_mapping[8];
527 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530528} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530529
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530530struct celt_specific_enc_cfg_t
531{
532 uint32_t bit_rate;
533 uint16_t frame_size;
534 uint16_t complexity;
535 uint16_t prediction_mode;
536 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530537} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530538
539struct celt_enc_cfg_t
540{
541 struct custom_enc_cfg_t custom_cfg;
542 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530543} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700544
545/* sync_mode introduced with APTX V2 libraries
546 * sync mode: 0x0 = stereo sync mode
547 * 0x01 = dual mono sync mode
548 * 0x02 = dual mono with no sync on either L or R codewords
549 */
550struct aptx_v2_enc_cfg_ext_t
551{
552 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530553} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700554
555/* APTX struct for combining custom enc and V2 fields */
556struct aptx_enc_cfg_t
557{
558 struct custom_enc_cfg_t custom_cfg;
559 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530560} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700561
Sharad Sangle95d451b2018-06-19 12:24:20 +0530562/* APTX AD structure */
563struct aptx_ad_enc_cfg_ext_t
564{
565 uint32_t sampling_freq;
566 uint32_t mtu;
567 uint32_t channel_mode;
568 uint32_t min_sink_modeA;
569 uint32_t max_sink_modeA;
570 uint32_t min_sink_modeB;
571 uint32_t max_sink_modeB;
572 uint32_t min_sink_modeC;
573 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530574 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530575} __attribute__ ((packed));
576
577struct aptx_ad_enc_cfg_t
578{
579 struct custom_enc_cfg_t custom_cfg;
580 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
581 struct abr_enc_cfg_t abr_cfg;
582} __attribute__ ((packed));
583
Zhou Song8fccbb62019-03-20 01:08:19 +0800584struct aptx_ad_enc_cfg_ext_r2_t
585{
586 uint32_t sampling_freq;
587 uint32_t mtu;
588 uint32_t channel_mode;
589 uint32_t min_sink_modeA;
590 uint32_t max_sink_modeA;
591 uint32_t min_sink_modeB;
592 uint32_t max_sink_modeB;
593 uint32_t min_sink_modeC;
594 uint32_t max_sink_modeC;
595 uint32_t mode;
596 uint32_t input_mode;
597 uint32_t fade_duration;
598 uint8_t sink_cap[11];
599} __attribute__ ((packed));
600
601struct aptx_ad_enc_cfg_r2_t
602{
603 struct custom_enc_cfg_t custom_cfg;
604 struct aptx_ad_enc_cfg_ext_r2_t aptx_ad_cfg;
605 struct abr_enc_cfg_t abr_cfg;
606} __attribute__ ((packed));
607
Zhou Song12c29502019-03-16 10:37:18 +0800608/* APTX AD SPEECH structure */
609struct aptx_ad_speech_enc_cfg_t
610{
611 struct custom_enc_cfg_t custom_cfg;
612 /* Information to set up IMC between decoder and encoder */
613 struct imc_dec_enc_info imc_info;
614 struct aptx_ad_speech_mode_cfg_t speech_mode;
615} __attribute__ ((packed));
616
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530617struct ldac_specific_enc_cfg_t
618{
619 uint32_t bit_rate;
620 uint16_t channel_mode;
621 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530622} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530623
624struct ldac_enc_cfg_t
625{
626 struct custom_enc_cfg_t custom_cfg;
627 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800628 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530629} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530630
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530631/* In LE BT source code uses system/audio.h for below
632 * structure definition. To avoid multiple definition
633 * compilation error for audiohal in LE , masking structure
634 * definition under "LINUX_ENABLED" which is defined only
635 * in LE
636 */
637#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530638/* TODO: Define the following structures only for O using PLATFORM_VERSION */
639/* Information about BT SBC encoder configuration
640 * This data is used between audio HAL module and
641 * BT IPC library to configure DSP encoder
642 */
643typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530644 uint32_t subband; /* 4, 8 */
645 uint32_t blk_len; /* 4, 8, 12, 16 */
646 uint16_t sampling_rate; /*44.1khz,48khz*/
647 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
648 uint8_t alloc; /*0(Loudness),1(SNR)*/
649 uint8_t min_bitpool; /* 2 */
650 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
651 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530652 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530653} audio_sbc_encoder_config;
654
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530655/* Information about BT APTX encoder configuration
656 * This data is used between audio HAL module and
657 * BT IPC library to configure DSP encoder
658 */
659typedef struct {
660 uint16_t sampling_rate;
661 uint8_t channels;
662 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530663 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700664} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530665
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700666typedef struct {
Zhou Song8fccbb62019-03-20 01:08:19 +0800667 uint32_t sampling_rate;
668 uint32_t mtu;
669 int32_t channel_mode;
670 uint32_t min_sink_modeA;
671 uint32_t max_sink_modeA;
672 uint32_t min_sink_modeB;
673 uint32_t max_sink_modeB;
674 uint32_t min_sink_modeC;
675 uint32_t max_sink_modeC;
676 uint32_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530677 uint8_t TTP_modeA_low;
678 uint8_t TTP_modeA_high;
679 uint8_t TTP_modeB_low;
680 uint8_t TTP_modeB_high;
Zhou Song8fccbb62019-03-20 01:08:19 +0800681 uint8_t TTP_TWS_low;
682 uint8_t TTP_TWS_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530683 uint32_t bits_per_sample;
Zhou Song8fccbb62019-03-20 01:08:19 +0800684 uint32_t input_mode;
685 uint32_t fade_duration;
686 uint8_t sink_cap[11];
Sharad Sangle95d451b2018-06-19 12:24:20 +0530687} audio_aptx_ad_config;
688
689typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700690 uint16_t sampling_rate;
691 uint8_t channels;
692 uint32_t bitrate;
693 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530694 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700695} audio_aptx_dual_mono_config;
696
697typedef union {
698 audio_aptx_default_config *default_cfg;
699 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530700 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700701} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530702
703/* Information about BT AAC encoder configuration
704 * This data is used between audio HAL module and
705 * BT IPC library to configure DSP encoder
706 */
707typedef struct {
708 uint32_t enc_mode; /* LC, SBR, PS */
709 uint16_t format_flag; /* RAW, ADTS */
710 uint16_t channels; /* 1-Mono, 2-Stereo */
711 uint32_t sampling_rate;
712 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530713 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530714} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530715#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700716
717typedef struct {
718 audio_aac_encoder_config audio_aac_enc_cfg;
719 struct aac_frame_size_control_t frame_ctl;
720} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530721
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530722typedef struct {
723 audio_aac_encoder_config audio_aac_enc_cfg;
724 struct aac_frame_size_control_t frame_ctl;
725 uint8_t size_control_struct;
726 struct aac_frame_size_control_t* frame_ptr_ctl;
727} audio_aac_encoder_config_v3;
728
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530729/* Information about BT CELT encoder configuration
730 * This data is used between audio HAL module and
731 * BT IPC library to configure DSP encoder
732 */
733typedef struct {
734 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
735 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
736 uint16_t frame_size; /* 64-128-256-512, 512 */
737 uint16_t complexity; /* 0-10, 1 */
738 uint16_t prediction_mode; /* 0-1-2, 0 */
739 uint16_t vbr_flag; /* 0-1, 0*/
740 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530741 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530742} audio_celt_encoder_config;
743
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530744/* Information about BT LDAC encoder configuration
745 * This data is used between audio HAL module and
746 * BT IPC library to configure DSP encoder
747 */
748typedef struct {
749 uint32_t sampling_rate; /*44100,48000,88200,96000*/
750 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
751 uint16_t channel_mode; /* 0, 4, 2, 1*/
752 uint16_t mtu; /*679*/
Aniket Kumar Latad0196282019-05-09 14:24:17 -0700753 uint32_t bits_per_sample;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800754 bool is_abr_enabled;
755 struct quality_level_to_bitrate_info level_to_bitrate_map;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530756} audio_ldac_encoder_config;
757
Surendar Karkabbb3c822018-11-12 13:00:38 +0530758/* Information about BT AAC decoder configuration
759 * This data is used between audio HAL module and
760 * BT IPC library to configure DSP decoder
761 */
762typedef struct {
763 uint16_t aac_fmt_flag; /* LATM*/
764 uint16_t audio_object_type; /* LC */
765 uint16_t channels; /* Stereo */
766 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
767 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
768 44.1k, 48k, 64k, 88.2k, 96k */
769} audio_aac_dec_config_t;
770
771/* Information about BT SBC decoder configuration
772 * This data is used between audio HAL module and
773 * BT IPC library to configure DSP decoder
774 */
775typedef struct {
776 uint16_t channels; /* Mono, Stereo */
777 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
778 44.1k, 48k, 64k, 88.2k, 96k */
779}audio_sbc_dec_config_t;
780
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530781/*********** END of DSP configurable structures ********************/
782
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530783static void update_offload_codec_capabilities()
784{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530785
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530786 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800787 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
788 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
789
790 ALOGD("%s: A2DP offload supported = %d",__func__,
791 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530792}
793
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800794static int stop_abr()
795{
796 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800797 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800798 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800799 int ret = 0;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800800
801 /* This function can be used if !abr_started for clean up */
802 ALOGV("%s: enter", __func__);
803
804 // Close hostless front end
805 if (a2dp.abr_config.abr_tx_handle != NULL) {
806 pcm_close(a2dp.abr_config.abr_tx_handle);
807 a2dp.abr_config.abr_tx_handle = NULL;
808 }
Zhou Song12c29502019-03-16 10:37:18 +0800809 if (a2dp.abr_config.abr_rx_handle != NULL) {
810 pcm_close(a2dp.abr_config.abr_rx_handle);
811 a2dp.abr_config.abr_rx_handle = NULL;
812 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800813 a2dp.abr_config.abr_started = false;
814 a2dp.abr_config.imc_instance = 0;
815
816 // Reset BT driver mixer control for ABR usecase
817 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
818 MIXER_SET_FEEDBACK_CHANNEL);
819 if (!ctl_set_bt_feedback_channel) {
820 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800821 ret = -ENOSYS;
822 } else if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800823 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800824 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800825 }
826
827 // Reset ABR Tx feedback path
828 ALOGV("%s: Disable ABR Tx feedback path", __func__);
829 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
830 MIXER_ABR_TX_FEEDBACK_PATH);
831 if (!ctl_abr_tx_path) {
832 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800833 ret = -ENOSYS;
834 } else if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800835 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800836 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800837 }
838
Zhou Song12c29502019-03-16 10:37:18 +0800839 // Reset ABR Rx feedback path
840 ALOGV("%s: Disable ABR Rx feedback path", __func__);
841 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
842 MIXER_ABR_RX_FEEDBACK_PATH);
843 if (!ctl_abr_rx_path) {
844 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
845 ret = -ENOSYS;
846 } else if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 0) != 0) {
847 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
848 ret = -ENOSYS;
849 }
850
851 return ret;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800852}
853
854static int start_abr()
855{
856 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800857 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800858 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
859 int abr_device_id;
860 int ret = 0;
861
862 if (!a2dp.abr_config.is_abr_enabled) {
863 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
864 return -ENOSYS;
865 }
866
867 if (a2dp.abr_config.abr_started) {
868 ALOGI("%s: ABR has already started", __func__);
869 return ret;
870 }
871
872 // Enable Slimbus 7 Tx feedback path
873 ALOGV("%s: Enable ABR Tx feedback path", __func__);
874 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
875 MIXER_ABR_TX_FEEDBACK_PATH);
876 if (!ctl_abr_tx_path) {
877 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
878 return -ENOSYS;
879 }
880 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
881 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
882 return -ENOSYS;
883 }
884
885 // Notify ABR usecase information to BT driver to distinguish
886 // between SCO and feedback usecase
887 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
888 MIXER_SET_FEEDBACK_CHANNEL);
889 if (!ctl_set_bt_feedback_channel) {
890 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800891 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800892 }
893 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
894 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800895 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800896 }
897
898 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800899 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800900 PCM_CAPTURE);
901 if (!a2dp.abr_config.abr_tx_handle) {
902 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
903 abr_device_id, PCM_IN,
904 &pcm_config_abr);
Zhou Song12c29502019-03-16 10:37:18 +0800905 if (a2dp.abr_config.abr_tx_handle == NULL) {
906 ALOGE("%s: Can't open abr tx device", __func__);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800907 goto fail;
Zhou Song12c29502019-03-16 10:37:18 +0800908 }
909 if (!(pcm_is_ready(a2dp.abr_config.abr_tx_handle) &&
910 !pcm_start(a2dp.abr_config.abr_tx_handle))) {
911 ALOGE("%s: tx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
912 goto fail;
913 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800914 }
Zhou Song12c29502019-03-16 10:37:18 +0800915
916 // Enable Slimbus 7 Rx feedback path for HD Voice use case
917 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH) {
918 ALOGV("%s: Enable ABR Rx feedback path", __func__);
919 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
920 MIXER_ABR_RX_FEEDBACK_PATH);
921 if (!ctl_abr_rx_path) {
922 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
923 goto fail;
924 }
925 if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 1) != 0) {
926 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
927 goto fail;
928 }
929
930 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
931 ALOGE("%s: Failed to set BT usecase", __func__);
932 goto fail;
933 }
934
935 // Open hostless front end and prepare ABR Rx path
936 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
937 PCM_PLAYBACK);
938 if (!a2dp.abr_config.abr_rx_handle) {
939 a2dp.abr_config.abr_rx_handle = pcm_open(a2dp.adev->snd_card,
940 abr_device_id, PCM_OUT,
941 &pcm_config_abr);
942 if (a2dp.abr_config.abr_rx_handle == NULL) {
943 ALOGE("%s: Can't open abr rx device", __func__);
944 goto fail;
945 }
946 if (!(pcm_is_ready(a2dp.abr_config.abr_rx_handle) &&
947 !pcm_start(a2dp.abr_config.abr_rx_handle))) {
948 ALOGE("%s: rx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_rx_handle));
949 goto fail;
950 }
951 }
952 }
953
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800954 a2dp.abr_config.abr_started = true;
955
956 return ret;
957
958fail:
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800959 stop_abr();
960 return -ENOSYS;
961}
962
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800963static int check_if_enhanced_fwk() {
964
965 int is_enhanced_fwk = 1;
966 //dlopen lib
967 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
968 if (vndk_fwk_lib_handle != NULL) {
969 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
970 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
971 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
972 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
973 __func__);
974 is_enhanced_fwk = 1;
975 } else {
976 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
977 }
978 }
979 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
980 return is_enhanced_fwk;
981}
982
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530983static void open_a2dp_source() {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530984 int ret = 0;
985
Florian Pfister1a84f312018-07-19 14:38:18 +0200986 ALOGD(" Open A2DP source start ");
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530987
988 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
989 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
990 ALOGD("calling BT stream open");
991 ret = a2dp.audio_source_open();
992 if(ret != 0) {
993 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Srinu Jellada99a592019-01-25 16:50:52 +0530994 }
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530995 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530996 } else {
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530997 ALOGD("Called a2dp open with improper state %d", a2dp.bt_state_source);
998 }
999 } else {
1000 ALOGE("a2dp handle is not identified, Ignoring open request");
1001 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
1002 }
1003}
1004/* API to open BT IPC library to start IPC communication for BT Source*/
1005static void a2dp_source_init()
1006{
1007 ALOGD("a2dp_source_init START");
1008 if (a2dp.bt_lib_source_handle == NULL) {
1009 ALOGD("Requesting for BT lib handle");
1010 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
1011 if (a2dp.bt_lib_source_handle == NULL) {
1012 ALOGE("%s: dlopen failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
1013 return;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301014 }
1015 }
1016
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301017 a2dp.bt_audio_pre_init = (bt_audio_pre_init_t)
1018 dlsym(a2dp.bt_lib_source_handle, "bt_audio_pre_init");
Srinu Jellada99a592019-01-25 16:50:52 +05301019 a2dp.audio_source_open = (audio_source_open_t)
1020 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
1021 a2dp.audio_source_start = (audio_source_start_t)
1022 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
1023 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
1024 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
1025 a2dp.audio_source_suspend = (audio_source_suspend_t)
1026 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
1027 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
1028 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
1029 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1030 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
1031 a2dp.audio_source_stop = (audio_source_stop_t)
1032 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
1033 a2dp.audio_source_close = (audio_source_close_t)
1034 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
1035 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
1036 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
1037 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
1038 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
1039 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1040 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1041 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1042 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1043
Srinu Jella793f5302019-07-19 15:53:55 +05301044 if (a2dp.bt_lib_source_handle && a2dp.bt_audio_pre_init) {
1045 ALOGD("calling BT module preinit");
1046 // fwk related check's will be done in the BT layer
1047 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001048 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001049}
1050
1051/* API to open BT IPC library to start IPC communication for BT Sink*/
1052static void open_a2dp_sink()
1053{
1054 ALOGD(" Open A2DP input start ");
1055 if (a2dp.bt_lib_sink_handle == NULL){
1056 ALOGD(" Requesting for BT lib handle");
1057 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1058
1059 if (a2dp.bt_lib_sink_handle == NULL) {
1060 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1061 } else {
1062 a2dp.audio_sink_start = (audio_sink_start_t)
1063 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1064 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1065 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1066 a2dp.audio_sink_stop = (audio_sink_stop_t)
1067 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1068 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1069 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1070 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1071 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1072 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301073 }
1074}
1075
1076static int close_a2dp_output()
1077{
1078 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001079
1080 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1081 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301082 return -ENOSYS;
1083 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001084
1085 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1086 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001087 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001088 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301089 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001090 a2dp.a2dp_source_started = false;
1091 a2dp.a2dp_source_total_active_session_requests = 0;
1092 a2dp.a2dp_source_suspended = false;
1093 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301094 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301095 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001096 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001097 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1098 stop_abr();
1099 a2dp.abr_config.is_abr_enabled = false;
1100 a2dp.abr_config.abr_started = false;
1101 a2dp.abr_config.imc_instance = 0;
1102 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001103 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301104 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001105
1106 return 0;
1107}
1108
1109static int close_a2dp_input()
1110{
1111 ALOGV("%s\n",__func__);
1112
1113 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1114 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1115 return -ENOSYS;
1116 }
1117
1118 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1119 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001120 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001121 ALOGE("failed close a2dp sink control path from BT library");
1122 }
1123 a2dp.a2dp_sink_started = false;
1124 a2dp.a2dp_sink_total_active_session_requests = 0;
1125 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1126 a2dp.dec_sampling_rate = 48000;
1127 a2dp.dec_channels = 2;
1128 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301129
1130 return 0;
1131}
1132
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301133static void a2dp_check_and_set_scrambler()
1134{
1135 bool scrambler_mode = false;
1136 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001137 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1138 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301139
1140 if (scrambler_mode) {
1141 //enable scrambler in dsp
1142 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1143 MIXER_SCRAMBLER_MODE);
1144 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001145 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301146 return;
1147 } else {
1148 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1149 ALOGE("%s: Could not set scrambler mode", __func__);
1150 return;
1151 }
1152 }
1153 }
1154}
1155
Florian Pfister1a84f312018-07-19 14:38:18 +02001156static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301157{
Florian Pfister1a84f312018-07-19 14:38:18 +02001158 char *rate_str = NULL, *channels = NULL;
1159 uint32_t sampling_rate;
1160 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1161 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301162
Florian Pfister1a84f312018-07-19 14:38:18 +02001163 if (direction == SINK) {
1164 sampling_rate = a2dp.dec_sampling_rate;
1165 } else {
1166 sampling_rate = a2dp.enc_sampling_rate;
1167 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001168 /*
1169 * For LDAC encoder and AAC decoder open slimbus port at
1170 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1171 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1172 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001173 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301174 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001175 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1176 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301177 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1178 sampling_rate = sampling_rate *2;
1179 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001180
Aalique Grahame22e49102018-12-18 14:23:57 -08001181 // No need to configure backend for PCM format.
1182 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1183 return 0;
1184 }
1185
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301186 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301187 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301188 case 44100:
1189 rate_str = "KHZ_44P1";
1190 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301191 case 88200:
1192 rate_str = "KHZ_88P2";
1193 break;
1194 case 96000:
1195 rate_str = "KHZ_96";
1196 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001197 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301198 default:
1199 rate_str = "KHZ_48";
1200 break;
1201 }
1202
Florian Pfister1a84f312018-07-19 14:38:18 +02001203 if (direction == SINK) {
1204 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1205 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301206 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001207 } else {
1208 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1209 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301210 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001211 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301212 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001213
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301214 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1215 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301216 is_configured = false;
1217 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301218 }
1219
Surendar Karka2febd452018-12-13 17:56:43 +05301220 if (direction == SOURCE) {
1221 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001222 if (a2dp.abr_config.is_abr_enabled) {
1223 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH)
1224 rate_str = SPEECH_TX_SAMPLE_RATE;
1225 else
1226 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301227
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301228 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1229 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1230 MIXER_SOURCE_SAMPLE_RATE_TX);
1231 if (!ctl_sample_rate) {
1232 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1233 is_configured = false;
1234 goto fail;
1235 }
1236
1237 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1238 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1239 is_configured = false;
1240 goto fail;
1241 }
Surendar Karka2febd452018-12-13 17:56:43 +05301242 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301243 }
1244 } else {
1245 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1246 MIXER_SAMPLE_RATE_TX is not supported */
1247 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1248 MIXER_SAMPLE_RATE_DEFAULT);
1249 if (!ctl_sample_rate) {
1250 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301251 is_configured = false;
1252 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301253 }
1254
1255 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1256 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001257 is_configured = false;
1258 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301259 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301260 }
1261
Florian Pfister1a84f312018-07-19 14:38:18 +02001262 if (direction == SINK) {
1263 switch (a2dp.dec_channels) {
1264 case 1:
1265 channels = "One";
1266 break;
1267 case 2:
1268 default:
1269 channels = "Two";
1270 break;
1271 }
1272
Ramu Gottipati02809682018-12-19 16:46:12 +05301273 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001274 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1275 MIXER_AFE_SINK_CHANNELS);
1276 } else {
1277 //Configure AFE enc channels
1278 switch (a2dp.enc_channels) {
1279 case 1:
1280 channels = "One";
1281 break;
1282 case 2:
1283 default:
1284 channels = "Two";
1285 break;
1286 }
1287
Ramu Gottipati02809682018-12-19 16:46:12 +05301288 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001289 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1290 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301291 }
1292
Florian Pfister1a84f312018-07-19 14:38:18 +02001293 if (!ctrl_channels) {
1294 ALOGE(" ERROR AFE channels mixer control not identified");
1295 } else {
1296 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301297 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001298 is_configured = false;
1299 goto fail;
1300 }
1301 }
1302 is_configured = true;
1303fail:
1304 return is_configured;
1305}
1306
Aniket Kumar Lata990de552019-07-11 14:20:23 -07001307bool a2dp_set_source_backend_cfg()
1308{
1309 if (a2dp.a2dp_source_started && !a2dp.a2dp_source_suspended)
1310 return a2dp_set_backend_cfg(SOURCE);
1311
1312 return false;
1313}
1314
Surendar Karkabbb3c822018-11-12 13:00:38 +05301315bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001316{
1317 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1318 struct aac_dec_cfg_t aac_dsp_cfg;
1319 bool is_configured = false;
1320 int ret = 0;
1321
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001322 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001323 return false;
1324
Surendar Karka2febd452018-12-13 17:56:43 +05301325 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001326 if (!ctl_dec_data) {
1327 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1328 is_configured = false;
1329 goto fail;
1330 }
1331
1332 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1333 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1334 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1335 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1336 switch(aac_bt_cfg->audio_object_type) {
1337 case 0:
1338 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301339 break;
1340 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001341 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1342 break;
1343 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301344 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001345 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301346 break;
1347 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001348 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1349 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1350 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1351 sizeof(struct aac_dec_cfg_t));
1352 if (ret != 0) {
1353 ALOGE("%s: failed to set AAC decoder config", __func__);
1354 is_configured = false;
1355 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001356 }
1357
Florian Pfister1a84f312018-07-19 14:38:18 +02001358 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1359 MIXER_DEC_BIT_FORMAT);
1360 if (!ctrl_bit_format) {
1361 ALOGE(" ERROR Dec bit format mixer control not identified");
1362 is_configured = false;
1363 goto fail;
1364 }
1365 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1366 if (ret != 0) {
1367 ALOGE("%s: Failed to set bit format to decoder", __func__);
1368 is_configured = false;
1369 goto fail;
1370 }
1371
1372 is_configured = true;
1373 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1374 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1375 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1376 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1377 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1378fail:
1379 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301380}
1381
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301382static int a2dp_set_bit_format(uint32_t enc_bit_format)
1383{
1384 const char *bit_format = NULL;
1385 struct mixer_ctl *ctrl_bit_format = NULL;
1386
1387 // Configure AFE Input Bit Format
1388 switch (enc_bit_format) {
1389 case 32:
1390 bit_format = "S32_LE";
1391 break;
1392 case 24:
1393 bit_format = "S24_LE";
1394 break;
1395 case 16:
1396 default:
1397 bit_format = "S16_LE";
1398 break;
1399 }
1400
1401 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1402 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1403 MIXER_ENC_BIT_FORMAT);
1404 if (!ctrl_bit_format) {
1405 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1406 return -ENOSYS;
1407 }
1408 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1409 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1410 return -ENOSYS;
1411 }
1412 return 0;
1413}
1414
Surendar Karka2febd452018-12-13 17:56:43 +05301415static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301416{
Surendar Karka2febd452018-12-13 17:56:43 +05301417 const char *rate_str = "KHZ_8", *channels = "Zero";
1418 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1419 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301420
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001421 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301422 if (direction == SINK) {
1423 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1424 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1425 MIXER_SINK_SAMPLE_RATE);
1426 } else {
1427 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1428 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1429 MIXER_SAMPLE_RATE_RX);
1430 }
1431 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301432
Surendar Karka2febd452018-12-13 17:56:43 +05301433 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1434 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301435 return -ENOSYS;
1436 }
Zhou Song12c29502019-03-16 10:37:18 +08001437 if (a2dp.abr_config.is_abr_enabled) {
1438 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1439 MIXER_SOURCE_SAMPLE_RATE_TX);
1440 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301441 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1442 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001443 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301444
Zhou Song12c29502019-03-16 10:37:18 +08001445 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1446 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1447 return -ENOSYS;
1448 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301449 }
1450 } else {
1451
Surendar Karka2febd452018-12-13 17:56:43 +05301452 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301453 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301454 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301455 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1456 return -ENOSYS;
1457 }
1458
Surendar Karka2febd452018-12-13 17:56:43 +05301459 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301460 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1461 return -ENOSYS;
1462 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001463 }
1464
1465 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301466 if (direction == SINK) {
1467 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1468 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1469 MIXER_AFE_SINK_CHANNELS);
1470 } else {
1471 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1472 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1473 MIXER_AFE_IN_CHANNELS);
1474 }
1475 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001476 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1477 return -ENOSYS;
1478 }
Surendar Karka2febd452018-12-13 17:56:43 +05301479 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001480 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1481 return -ENOSYS;
1482 }
1483
1484 return 0;
1485}
1486
1487/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301488static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001489{
1490 struct mixer_ctl *ctl_dec_data = NULL;
1491 struct abr_dec_cfg_t dec_cfg;
1492 int ret = 0;
1493
1494 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301495 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001496 if (!ctl_dec_data) {
1497 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1498 return false;
1499 }
1500 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1501 dec_cfg.dec_format = dec_format;
1502 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1503 dec_cfg.imc_info.enable = IMC_ENABLE;
1504 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1505 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1506
1507 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1508 sizeof(dec_cfg));
1509 if (ret != 0) {
1510 ALOGE("%s: Failed to set decoder config", __func__);
1511 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301512 }
Surendar Karka2febd452018-12-13 17:56:43 +05301513 }
1514
1515 return true;
1516}
1517
Surendar Karkabbb3c822018-11-12 13:00:38 +05301518bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001519{
1520 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1521 struct sbc_dec_cfg_t sbc_dsp_cfg;
1522 bool is_configured = false;
1523 int ret = 0;
1524
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001525 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001526 goto fail;
1527
Surendar Karka2febd452018-12-13 17:56:43 +05301528 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001529 if (!ctl_dec_data) {
1530 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1531 is_configured = false;
1532 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301533 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001534
Florian Pfister1a84f312018-07-19 14:38:18 +02001535 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1536 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1537 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1538 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1539 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1540 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301541
Florian Pfister1a84f312018-07-19 14:38:18 +02001542 if (ret != 0) {
1543 ALOGE("%s: failed to set SBC decoder config", __func__);
1544 is_configured = false;
1545 goto fail;
1546 }
1547
1548 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1549 MIXER_DEC_BIT_FORMAT);
1550 if (!ctrl_bit_format) {
1551 ALOGE(" ERROR Dec bit format mixer control not identified");
1552 is_configured = false;
1553 goto fail;
1554 }
1555 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1556 if (ret != 0) {
1557 ALOGE("%s: Failed to set bit format to decoder", __func__);
1558 is_configured = false;
1559 goto fail;
1560 }
1561
1562 is_configured = true;
1563 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1564 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1565 a2dp.dec_channels = 1;
1566 else
1567 a2dp.dec_channels = 2;
1568 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1569 ALOGV("Successfully updated SBC dec format");
1570fail:
1571 return is_configured;
1572}
1573
Florian Pfister1a84f312018-07-19 14:38:18 +02001574/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301575static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301576{
Florian Pfister1a84f312018-07-19 14:38:18 +02001577 void *codec_info = NULL;
1578 codec_t codec_type = CODEC_TYPE_INVALID;
1579 bool is_configured = false;
1580 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301581
Florian Pfister1a84f312018-07-19 14:38:18 +02001582 if (!a2dp.audio_get_dec_config) {
1583 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1584 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301585 }
1586
Surendar Karka2febd452018-12-13 17:56:43 +05301587 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001588 if (!ctl_dec_data) {
1589 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1590 is_configured = false;
1591 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301592 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001593 codec_info = a2dp.audio_get_dec_config(&codec_type);
1594 switch(codec_type) {
1595 case CODEC_TYPE_SBC:
1596 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301597 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001598 break;
1599 case CODEC_TYPE_AAC:
1600 ALOGD(" AAC decoder supported BT device");
1601 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301602 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001603 break;
1604 default:
1605 ALOGD(" Received Unsupported decoder format");
1606 is_configured = false;
1607 break;
1608 }
1609 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301610}
1611
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301612/* API to configure SBC DSP encoder */
1613bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1614{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301615 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301616 struct sbc_enc_cfg_t sbc_dsp_cfg;
1617 bool is_configured = false;
1618 int ret = 0;
1619
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001620 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301621 return false;
1622
1623 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1624 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001625 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301626 is_configured = false;
1627 goto fail;
1628 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301629 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001630 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301631 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1632 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1633 switch(sbc_bt_cfg->channels) {
1634 case 0:
1635 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1636 break;
1637 case 1:
1638 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1639 break;
1640 case 3:
1641 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1642 break;
1643 case 2:
1644 default:
1645 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1646 break;
1647 }
1648 if (sbc_bt_cfg->alloc)
1649 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1650 else
1651 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1652 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1653 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1654 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1655 sizeof(struct sbc_enc_cfg_t));
1656 if (ret != 0) {
1657 ALOGE("%s: failed to set SBC encoder config", __func__);
1658 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301659 goto fail;
1660 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301661 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301662 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301663 is_configured = false;
1664 goto fail;
1665 }
1666 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001667 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301668 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301669
1670 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1671 a2dp.enc_channels = 1;
1672 else
1673 a2dp.enc_channels = 2;
1674
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301675 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1676 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301677fail:
1678 return is_configured;
1679}
1680
Manish Dewangan6a252632017-12-04 17:27:44 +05301681#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301682static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1683 audio_aptx_encoder_config *aptx_bt_cfg)
1684{
1685 int ret = 0;
1686
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001687 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301688 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1689 aptx_dsp_cfg, aptx_bt_cfg);
1690 return -EINVAL;
1691 }
1692
1693 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301694 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301695
1696
1697 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1698 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1699 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1700 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1701 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1702 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1703 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1704 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1705 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301706 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301707 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1708 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1709 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1710 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1711
1712
1713 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1714 case APTX_AD_CHANNEL_UNCHANGED:
1715 case APTX_AD_CHANNEL_JOINT_STEREO:
1716 case APTX_AD_CHANNEL_DUAL_MONO:
1717 case APTX_AD_CHANNEL_STEREO_TWS:
1718 case APTX_AD_CHANNEL_EARBUD:
1719 default:
1720 a2dp.enc_channels = CH_STEREO;
1721 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1722 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1723 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1724 break;
1725 case APTX_AD_CHANNEL_MONO:
1726 a2dp.enc_channels = CH_MONO;
1727 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1728 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001729 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301730 }
1731 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1732 case APTX_AD_SR_UNCHANGED:
1733 case APTX_AD_48:
1734 default:
1735 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1736 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1737 break;
1738 case APTX_AD_44_1:
1739 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1740 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001741 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301742 }
1743 ALOGV("Successfully updated APTX AD enc format with \
1744 samplingrate: %d channels:%d",
1745 aptx_dsp_cfg->custom_cfg.sample_rate,
1746 aptx_dsp_cfg->custom_cfg.num_channels);
1747
1748 return ret;
1749}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301750
Zhou Song8fccbb62019-03-20 01:08:19 +08001751static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
1752 audio_aptx_encoder_config *aptx_bt_cfg)
1753{
1754 int ret = 0;
1755
1756 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Mingshu Pangaa429f72019-06-10 17:25:00 +08001757 ALOGE("Invalid param, aptx_dsp_cfg %pK aptx_bt_cfg %pK",
Zhou Song8fccbb62019-03-20 01:08:19 +08001758 aptx_dsp_cfg, aptx_bt_cfg);
1759 return -EINVAL;
1760 }
1761
1762 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
1763 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
1764
1765
1766 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1767 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1768 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1769 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1770 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1771 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1772 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1773 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1774 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
1775 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
1776 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
1777 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
1778 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
1779 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
1780 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1781 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1782 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1783 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1784
1785
1786 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1787 case APTX_AD_CHANNEL_UNCHANGED:
1788 case APTX_AD_CHANNEL_JOINT_STEREO:
1789 case APTX_AD_CHANNEL_DUAL_MONO:
1790 case APTX_AD_CHANNEL_STEREO_TWS:
1791 case APTX_AD_CHANNEL_EARBUD:
1792 default:
1793 a2dp.enc_channels = CH_STEREO;
1794 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1795 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1796 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1797 break;
1798 case APTX_AD_CHANNEL_MONO:
1799 a2dp.enc_channels = CH_MONO;
1800 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1801 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001802 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001803 }
1804 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1805 case APTX_AD_SR_UNCHANGED:
1806 case APTX_AD_48:
1807 default:
1808 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1809 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1810 break;
1811 case APTX_AD_44_1:
1812 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1813 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001814 break;
1815 case APTX_AD_96:
1816 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
1817 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_96K;
1818 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001819 }
1820 ALOGV("Successfully updated APTX AD enc format with \
1821 samplingrate: %d channels:%d",
1822 aptx_dsp_cfg->custom_cfg.sample_rate,
1823 aptx_dsp_cfg->custom_cfg.num_channels);
1824
1825 return ret;
1826}
1827
Manisha Agarwala51768b2018-11-01 16:30:52 +05301828static void audio_a2dp_update_tws_channel_mode()
1829{
1830 char* channel_mode;
1831 struct mixer_ctl *ctl_channel_mode;
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301832
1833 ALOGD("Update tws for mono_mode on=%d",a2dp.is_tws_mono_mode_on);
1834
Manisha Agarwala51768b2018-11-01 16:30:52 +05301835 if (a2dp.is_tws_mono_mode_on)
1836 channel_mode = "One";
1837 else
1838 channel_mode = "Two";
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301839
Manisha Agarwala51768b2018-11-01 16:30:52 +05301840 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1841 if (!ctl_channel_mode) {
1842 ALOGE("failed to get tws mixer ctl");
1843 return;
1844 }
1845 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1846 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1847 return;
1848 }
1849}
1850
Manish Dewangan6a252632017-12-04 17:27:44 +05301851static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
1852 audio_aptx_encoder_config *aptx_bt_cfg)
1853{
1854 int ret = 0;
1855
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001856 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301857 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1858 aptx_dsp_cfg, aptx_bt_cfg);
1859 return -EINVAL;
1860 }
1861
1862 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001863 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301864
1865 if (!a2dp.is_aptx_dual_mono_supported) {
1866 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1867 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1868 } else {
1869 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1870 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1871 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1872 }
1873
1874 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
1875 case 1:
1876 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1877 break;
1878 case 2:
1879 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05301880 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1881 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
Manish Dewangan6a252632017-12-04 17:27:44 +05301882 break;
1883 }
1884 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
1885 if (!a2dp.is_aptx_dual_mono_supported) {
1886 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1887 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
1888 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
1889 aptx_dsp_cfg->custom_cfg.num_channels);
1890 } else {
1891 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1892 ALOGV("Successfully updated APTX dual mono enc format with \
1893 samplingrate: %d channels:%d syncmode %d",
1894 aptx_dsp_cfg->custom_cfg.sample_rate,
1895 aptx_dsp_cfg->custom_cfg.num_channels,
1896 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
1897 }
1898 return ret;
1899}
1900#else
1901static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
1902 audio_aptx_encoder_config *aptx_bt_cfg)
1903{
1904 int ret = 0;
1905
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001906 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301907 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1908 aptx_dsp_cfg, aptx_bt_cfg);
1909 return -EINVAL;
1910 }
1911
1912 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001913 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301914 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
1915 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
1916 switch(aptx_dsp_cfg->num_channels) {
1917 case 1:
1918 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
1919 break;
1920 case 2:
1921 default:
1922 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
1923 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
1924 break;
1925 }
1926
1927 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
1928 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
1929
1930 return ret;
1931}
1932#endif
1933
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301934/* API to configure APTX DSP encoder */
1935bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1936{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301937 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08001938 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001939 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301940 bool is_configured = false;
1941 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08001942 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301943
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001944 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301945 return false;
1946
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001947 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1948 if (!ctl_enc_data) {
1949 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1950 return false;
1951 }
1952
Manish Dewangan6a252632017-12-04 17:27:44 +05301953#ifndef LINUX_ENABLED
1954 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301955 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08001956 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001957 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001958 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
1959 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001960 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08001961 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001962 else
Zhou Song8fccbb62019-03-20 01:08:19 +08001963 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001964 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001965 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001966
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001967 if (ret) {
1968 is_configured = false;
1969 goto fail;
1970 }
1971
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001972 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001973 if (aptx_ad_ctl)
1974 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001975 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08001976 else
1977 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001978 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001979 } else {
1980 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001981 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05301982 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301983#else
1984 struct custom_enc_cfg_t aptx_dsp_cfg;
1985 mixer_size = sizeof(struct custom_enc_cfg_t);
1986 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05301987 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05301988 if (ret) {
1989 is_configured = false;
1990 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301991 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001992 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1993 mixer_size);
1994#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301995 if (ret != 0) {
1996 ALOGE("%s: Failed to set APTX encoder config", __func__);
1997 is_configured = false;
1998 goto fail;
1999 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302000#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002001 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302002 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002003 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302004 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
2005 else
2006 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302007#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302008 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302009 is_configured = false;
2010 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302011 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302012 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302013 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05302014 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302015 else
Surendar Karka2febd452018-12-13 17:56:43 +05302016 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302017fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05302018 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002019 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05302020 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302021 return is_configured;
2022}
2023
2024/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302025 */
Manish Dewangan6a252632017-12-04 17:27:44 +05302026#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002027bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302028#else
2029bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2030#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302031{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302032 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302033 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302034 bool is_configured = false;
2035 int ret = 0;
2036
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002037 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302038 return false;
2039
2040 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2041 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002042 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302043 is_configured = false;
2044 goto fail;
2045 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302046
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302047 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002048 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302049 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2050 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2051 switch(aptx_dsp_cfg.num_channels) {
2052 case 1:
2053 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2054 break;
2055 case 2:
2056 default:
2057 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2058 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2059 break;
2060 }
2061 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302062 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302063 if (ret != 0) {
2064 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2065 is_configured = false;
2066 goto fail;
2067 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302068 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302069 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302070 is_configured = false;
2071 goto fail;
2072 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302073 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002074 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302075 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302076 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302077 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2078 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302079fail:
2080 return is_configured;
2081}
2082
2083/* API to configure AAC DSP encoder */
2084bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2085{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302086 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302087 struct aac_enc_cfg_t aac_dsp_cfg;
2088 bool is_configured = false;
2089 int ret = 0;
2090
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002091 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302092 return false;
2093
2094 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2095 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002096 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302097 is_configured = false;
2098 goto fail;
2099 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302100 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002101 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302102 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302103 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002104 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302105 case 0:
2106 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2107 break;
2108 case 2:
2109 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2110 break;
2111 case 1:
2112 default:
2113 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2114 break;
2115 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302116 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302117 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002118
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302119 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2120 sizeof(struct aac_enc_cfg_t));
2121 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002122 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302123 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302124 goto fail;
2125 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302126 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302127 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302128 is_configured = false;
2129 goto fail;
2130 }
2131 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002132 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302133 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002134 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002135 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2136 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2137fail:
2138 return is_configured;
2139}
2140
2141bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2142{
2143 struct mixer_ctl *ctl_enc_data = NULL;
2144 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2145 bool is_configured = false;
2146 int ret = 0;
2147
2148 if (aac_bt_cfg == NULL)
2149 return false;
2150
2151 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2152 if (!ctl_enc_data) {
2153 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2154 is_configured = false;
2155 goto fail;
2156 }
2157 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302158 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002159 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2160 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2161 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2162 case 0:
2163 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2164 break;
2165 case 2:
2166 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2167 break;
2168 case 1:
2169 default:
2170 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2171 break;
2172 }
2173 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2174 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2175 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2176 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2177
2178 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2179 sizeof(struct aac_enc_cfg_v2_t));
2180 if (ret != 0) {
2181 ALOGE("%s: Failed to set AAC encoder config", __func__);
2182 is_configured = false;
2183 goto fail;
2184 }
2185 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2186 if (ret != 0) {
2187 is_configured = false;
2188 goto fail;
2189 }
2190 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302191 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002192 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2193 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2194 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2195 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302196fail:
2197 return is_configured;
2198}
2199
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302200bool configure_aac_enc_format_v3(audio_aac_encoder_config_v3 *aac_bt_cfg)
2201{
2202 struct mixer_ctl *ctl_enc_data = NULL;
2203 struct aac_enc_cfg_v3_t aac_dsp_cfg;
2204 struct aac_frame_size_control_t* frame_vbr_ctl = NULL;
2205 bool is_configured = false;
2206 int ret = 0;
2207
2208 if (aac_bt_cfg == NULL)
2209 return false;
2210
2211 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2212 if (!ctl_enc_data) {
2213 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2214 is_configured = false;
2215 goto fail;
2216 }
2217 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v3_t));
2218 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
2219 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2220 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2221 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2222 case 0:
2223 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2224 break;
2225 case 2:
2226 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2227 break;
2228 case 1:
2229 default:
2230 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2231 break;
2232 }
2233 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2234 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2235 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2236 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2237 frame_vbr_ctl = aac_bt_cfg->frame_ptr_ctl;
2238
2239 if (frame_vbr_ctl != NULL) {
2240 aac_dsp_cfg.aac_key_value_ctl.ctl_type = frame_vbr_ctl->ctl_type;
2241 aac_dsp_cfg.aac_key_value_ctl.ctl_value = frame_vbr_ctl->ctl_value;
2242 } else {
2243 ALOGE("%s: VBR cannot be enabled, fall back to default",__func__);
2244 aac_dsp_cfg.aac_key_value_ctl.ctl_type = 0;
2245 aac_dsp_cfg.aac_key_value_ctl.ctl_value = 0;
2246 }
2247
2248 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2249 sizeof(struct aac_enc_cfg_v3_t));
2250 if (ret != 0) {
2251 ALOGE("%s: Failed to set AAC encoder config", __func__);
2252 is_configured = false;
2253 goto fail;
2254 }
2255 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2256 if (ret != 0) {
2257 is_configured = false;
2258 goto fail;
2259 }
2260 is_configured = true;
2261 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
2262 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2263 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2264 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2265 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
2266fail:
2267 return is_configured;
2268}
2269
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302270bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2271{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302272 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302273 struct celt_enc_cfg_t celt_dsp_cfg;
2274 bool is_configured = false;
2275 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002276 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302277 return false;
2278
2279 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2280 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002281 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302282 is_configured = false;
2283 goto fail;
2284 }
2285 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2286
Florian Pfister1a84f312018-07-19 14:38:18 +02002287 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302288 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2289 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2290 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2291 case 1:
2292 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2293 break;
2294 case 2:
2295 default:
2296 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2297 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2298 break;
2299 }
2300
2301 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2302
2303 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2304 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2305 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2306 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2307 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2308
2309 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2310 sizeof(struct celt_enc_cfg_t));
2311 if (ret != 0) {
2312 ALOGE("%s: Failed to set CELT encoder config", __func__);
2313 is_configured = false;
2314 goto fail;
2315 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302316 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302317 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302318 is_configured = false;
2319 goto fail;
2320 }
2321 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002322 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302323 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302324 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302325 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2326 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2327fail:
2328 return is_configured;
2329}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302330
2331bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2332{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302333 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302334 struct ldac_enc_cfg_t ldac_dsp_cfg;
2335 bool is_configured = false;
2336 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002337 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302338 return false;
2339
2340 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2341 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002342 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302343 is_configured = false;
2344 goto fail;
2345 }
2346 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2347
Florian Pfister1a84f312018-07-19 14:38:18 +02002348 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302349 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2350 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2351 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2352 case 4:
2353 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2354 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2355 break;
2356 case 2:
2357 case 1:
2358 default:
2359 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2360 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2361 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2362 break;
2363 }
2364
2365 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2366 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2367 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002368 if (ldac_bt_cfg->is_abr_enabled) {
2369 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2370 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2371 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2372 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2373 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002374 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002375 }
2376
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302377 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2378 sizeof(struct ldac_enc_cfg_t));
2379 if (ret != 0) {
2380 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2381 is_configured = false;
2382 goto fail;
2383 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302384 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302385 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302386 is_configured = false;
2387 goto fail;
2388 }
2389 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002390 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302391 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2392 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002393 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302394 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2395 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2396fail:
2397 return is_configured;
2398}
2399
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302400bool configure_a2dp_encoder_format()
2401{
2402 void *codec_info = NULL;
2403 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002404 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302405 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002406 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302407
Florian Pfister1a84f312018-07-19 14:38:18 +02002408 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302409 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2410 return false;
2411 }
2412 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002413 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302414 &codec_type);
2415
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002416 // ABR disabled by default for all codecs
2417 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302418 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002419
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302420 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002421 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302422 ALOGD(" Received SBC encoder supported BT device");
2423 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002424 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302425 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002426 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302427 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302428#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002429 a2dp.is_aptx_dual_mono_supported = false;
2430 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302431#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302432 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002433 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302434 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002435 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302436 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302437#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302438 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002439 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302440#else
2441 is_configured =
2442 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2443#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002444 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302445#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002446 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002447 ALOGD(" Received APTX dual mono encoder supported BT device");
2448 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302449 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2450 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002451 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2452 is_configured =
2453 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302454 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302455#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002456 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302457 ALOGD(" Received AAC encoder supported BT device");
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302458 bool is_aac_vbr_enabled =
2459 property_get_bool("persist.vendor.bt.aac_vbr_frm_ctl.enabled", false);
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002460 bool is_aac_frame_ctl_enabled =
2461 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302462 if (is_aac_vbr_enabled)
2463 is_configured = configure_aac_enc_format_v3((audio_aac_encoder_config_v3 *) codec_info);
2464 else
2465 is_configured = is_aac_frame_ctl_enabled ?
2466 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2467 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302468 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002469 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302470 ALOGD(" Received CELT encoder supported BT device");
2471 is_configured =
2472 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2473 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002474 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302475 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002476 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2477 instance_id = MAX_INSTANCE_ID;
2478 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302479 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002480 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302481 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302482 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302483#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302484 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302485 ALOGD(" Received APTX AD encoder supported BT device");
2486 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2487 instance_id = MAX_INSTANCE_ID;
2488 a2dp.abr_config.imc_instance = instance_id--;
2489 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2490 a2dp.is_aptx_adaptive = true;
2491 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2492 is_configured =
2493 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302494 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302495 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302496#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002497 case CODEC_TYPE_PCM:
2498 ALOGD("Received PCM format for BT device");
2499 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2500 is_configured = true;
2501 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302502 default:
2503 ALOGD(" Received Unsupported encoder formar");
2504 is_configured = false;
2505 break;
2506 }
2507 return is_configured;
2508}
2509
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002510int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302511{
2512 int ret = 0;
2513
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002514 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302515
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002516 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002517 && a2dp.audio_get_enc_config)) {
2518 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302519 return -ENOSYS;
2520 }
2521
Zhou Song12c29502019-03-16 10:37:18 +08002522 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302523 //session will be restarted after suspend completion
2524 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302525 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302526 }
2527
Florian Pfister1a84f312018-07-19 14:38:18 +02002528 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302529 ALOGD("calling BT module stream start");
2530 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002531 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302532 ALOGE("BT controller start return = %d",ret);
2533 if (ret != 0 ) {
2534 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002535 a2dp.a2dp_source_started = false;
Revathi Uddarajub26e3932020-06-10 14:51:02 +05302536 ret = -ETIMEDOUT;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302537 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002538 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002539 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302540 ret = 0;
2541 ALOGD("Start playback successful to BT library");
2542 } else {
2543 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002544 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302545 ret = -ETIMEDOUT;
2546 }
2547 }
2548 }
2549
Florian Pfister1a84f312018-07-19 14:38:18 +02002550 if (a2dp.a2dp_source_started) {
2551 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302552 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302553 audio_a2dp_update_tws_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002554 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002555 if (a2dp.abr_config.is_abr_enabled)
2556 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302557 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302558
2559 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002560 a2dp.a2dp_source_total_active_session_requests);
2561 return ret;
2562}
2563
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002564uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002565{
2566 uint32_t latency = 0;
2567
2568 switch(a2dp.bt_decoder_format) {
2569 case CODEC_TYPE_SBC:
2570 latency = DEFAULT_SINK_LATENCY_SBC;
2571 break;
2572 case CODEC_TYPE_AAC:
2573 latency = DEFAULT_SINK_LATENCY_AAC;
2574 break;
2575 default:
2576 latency = 200;
2577 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2578 break;
2579 }
2580 return (uint64_t)latency;
2581}
2582
2583bool a2dp_send_sink_setup_complete(void) {
2584 uint64_t system_latency = 0;
2585 bool is_complete = false;
2586
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002587 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002588
2589 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2590 is_complete = true;
2591 }
2592 return is_complete;
2593}
2594
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002595bool a2dp_sink_is_ready()
2596{
2597 bool ret = false;
2598
2599 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2600 (a2dp.is_a2dp_offload_supported) &&
2601 (a2dp.audio_sink_check_a2dp_ready))
2602 ret = a2dp.audio_sink_check_a2dp_ready();
2603 return ret;
2604}
2605
2606int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002607{
2608 int ret = 0;
2609
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002610 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002611
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002612 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002613 && a2dp.audio_get_dec_config)) {
2614 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2615 return -ENOSYS;
2616 }
2617
2618 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2619 ALOGD("calling BT module stream start");
2620 /* This call indicates BT IPC lib to start capture */
2621 ret = a2dp.audio_sink_start();
2622 ALOGE("BT controller start capture return = %d",ret);
2623 if (ret != 0 ) {
2624 ALOGE("BT controller start capture failed");
2625 a2dp.a2dp_sink_started = false;
2626 } else {
2627
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002628 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002629 ALOGD("Wait for capture ready not successful");
2630 ret = -ETIMEDOUT;
2631 }
2632
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002633 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002634 a2dp.a2dp_sink_started = true;
2635 ret = 0;
2636 ALOGD("Start capture successful to BT library");
2637 } else {
2638 ALOGD(" unable to configure DSP decoder");
2639 a2dp.a2dp_sink_started = false;
2640 ret = -ETIMEDOUT;
2641 }
2642
2643 if (!a2dp_send_sink_setup_complete()) {
2644 ALOGD("sink_setup_complete not successful");
2645 ret = -ETIMEDOUT;
2646 }
2647 }
2648 }
2649
2650 if (a2dp.a2dp_sink_started) {
2651 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002652 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002653 }
2654 }
2655
2656 ALOGD("start A2DP sink total active sessions :%d",
2657 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302658 return ret;
2659}
2660
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302661static void reset_a2dp_enc_config_params()
2662{
2663 int ret =0;
2664
Aalique Grahame22e49102018-12-18 14:23:57 -08002665 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302666 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302667 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302668
2669 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
2670 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
2671 MIXER_ENC_CONFIG_BLOCK);
2672 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002673 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302674 } else {
2675 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
2676 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002677 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302678 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002679
2680 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2681
Manisha Agarwala51768b2018-11-01 16:30:52 +05302682 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2683
2684 if (!ctl_channel_mode) {
2685 ALOGE("failed to get tws mixer ctl");
2686 } else {
2687 channel_mode = "Two";
2688 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2689 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2690 }
2691 a2dp.is_tws_mono_mode_on = false;
2692 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302693}
2694
Surendar Karka2febd452018-12-13 17:56:43 +05302695static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002696{
2697 struct mixer_ctl *ctl_dec_data = NULL;
2698 struct abr_dec_cfg_t dummy_reset_cfg;
2699 int ret = 0;
2700
Preetam Singh Ranawat25780e42019-07-31 18:15:57 +05302701 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
2702 if (!ctl_dec_data) {
2703 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
2704 return -EINVAL;
2705 }
2706 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
2707 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
2708 sizeof(dummy_reset_cfg));
2709 if (ret != 0) {
2710 ALOGE("%s: Failed to set dummy decoder config", __func__);
2711 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002712 }
2713
2714 return ret;
2715}
2716
Surendar Karka2febd452018-12-13 17:56:43 +05302717static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02002718{
2719 int ret =0;
2720
2721 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
2722 struct aac_dec_cfg_t dummy_reset_config;
2723
2724 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
2725 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05302726 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02002727 if (!ctl_dec_config) {
2728 ALOGE(" ERROR a2dp decoder format mixer control not identified");
2729 } else {
2730 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
2731 sizeof(struct aac_dec_cfg_t));
2732 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
2733 }
2734 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
2735 MIXER_DEC_BIT_FORMAT);
2736 if (!ctrl_bit_format) {
2737 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
2738 } else {
2739 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
2740 if (ret != 0) {
2741 ALOGE("%s: Failed to set bit format to decoder", __func__);
2742 }
2743 }
2744}
2745
Zhou Song12c29502019-03-16 10:37:18 +08002746static void reset_codec_config()
2747{
2748 reset_a2dp_enc_config_params();
2749 reset_a2dp_source_dec_config_params();
2750 a2dp_reset_backend_cfg(SOURCE);
2751 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
2752 stop_abr();
2753 a2dp.abr_config.is_abr_enabled = false;
2754}
2755
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002756int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302757{
2758 int ret =0;
2759
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002760 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002761 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002762 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302763 return -ENOSYS;
2764 }
2765
Florian Pfister1a84f312018-07-19 14:38:18 +02002766 if (a2dp.a2dp_source_total_active_session_requests > 0)
2767 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08002768 else
2769 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302770
Florian Pfister1a84f312018-07-19 14:38:18 +02002771 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302772 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02002773 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302774 if (ret < 0)
2775 ALOGE("stop stream to BT IPC lib failed");
2776 else
2777 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08002778 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
2779 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05302780 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302781 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002782 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002783 a2dp.a2dp_source_started = false;
2784 ALOGD("Stop A2DP playback, total active sessions :%d",
2785 a2dp.a2dp_source_total_active_session_requests);
2786 return 0;
2787}
2788
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002789int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002790{
2791 int ret =0;
2792
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002793 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002794 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002795 ALOGE("a2dp handle is not identified, Ignoring stop request");
2796 return -ENOSYS;
2797 }
2798
2799 if (a2dp.a2dp_sink_total_active_session_requests > 0)
2800 a2dp.a2dp_sink_total_active_session_requests--;
2801
2802 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2803 ALOGV("calling BT module stream stop");
2804 ret = a2dp.audio_sink_stop();
2805 if (ret < 0)
2806 ALOGE("stop stream to BT IPC lib failed");
2807 else
2808 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05302809 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002810 a2dp_reset_backend_cfg(SINK);
2811 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002812 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002813 a2dp.a2dp_source_started = false;
2814 ALOGD("Stop A2DP capture, total active sessions :%d",
2815 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302816 return 0;
2817}
2818
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002819int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302820{
Weiyin Jiang280ea742020-09-08 20:28:22 +08002821 int ret = 0, val, status = 0;
2822 char value[32] = {0};
2823 struct audio_usecase *uc_info;
2824 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302825
Weiyin Jiang280ea742020-09-08 20:28:22 +08002826 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002827 ALOGV("no supported encoders identified,ignoring a2dp setparam");
2828 status = -EINVAL;
2829 goto param_handled;
Weiyin Jiang280ea742020-09-08 20:28:22 +08002830 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302831
Weiyin Jiang280ea742020-09-08 20:28:22 +08002832 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302833 sizeof(value));
Weiyin Jiang280ea742020-09-08 20:28:22 +08002834 if (ret >= 0) {
2835 val = atoi(value);
2836 if (audio_is_a2dp_out_device(val)) {
2837 ALOGV("Received device connect request for A2DP source");
2838 open_a2dp_source();
2839 }
2840 goto param_handled;
2841 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302842
Weiyin Jiang280ea742020-09-08 20:28:22 +08002843 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
2844 sizeof(value));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302845
Weiyin Jiang280ea742020-09-08 20:28:22 +08002846 if (ret >= 0) {
2847 val = atoi(value);
2848 if (audio_is_a2dp_out_device(val)) {
2849 ALOGV("Received source device dis- connect request");
2850 close_a2dp_output();
2851 reset_a2dp_enc_config_params();
2852 reset_a2dp_source_dec_config_params();
2853 a2dp_reset_backend_cfg(SOURCE);
2854 } else if (audio_is_a2dp_in_device(val)) {
2855 ALOGV("Received sink device dis- connect request");
2856 close_a2dp_input();
2857 reset_a2dp_sink_dec_config_params();
2858 a2dp_reset_backend_cfg(SINK);
2859 }
2860 goto param_handled;
2861 }
Surendar Karka2febd452018-12-13 17:56:43 +05302862#ifndef LINUX_ENABLED
Weiyin Jiang280ea742020-09-08 20:28:22 +08002863 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
2864 if (ret >= 0) {
2865 ALOGD("Setting tws channel mode to %s",value);
2866 if (!(strncmp(value, "mono", strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302867 a2dp.is_tws_mono_mode_on = true;
Weiyin Jiang280ea742020-09-08 20:28:22 +08002868 else if (!(strncmp(value, "dual-mono", strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302869 a2dp.is_tws_mono_mode_on = false;
Weiyin Jiang280ea742020-09-08 20:28:22 +08002870 audio_a2dp_update_tws_channel_mode();
2871 goto param_handled;
2872 }
Surendar Karka2febd452018-12-13 17:56:43 +05302873#endif
Weiyin Jiang280ea742020-09-08 20:28:22 +08002874 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
2875 if (ret >= 0) {
2876 if (a2dp.bt_lib_source_handle == NULL)
2877 goto param_handled;
2878
2879 if ((!strncmp(value, "true", sizeof(value)))) {
2880 if (a2dp.a2dp_source_suspended) {
2881 ALOGD("%s: A2DP is already suspended", __func__);
2882 goto param_handled;
2883 }
2884 ALOGD("Setting a2dp to suspend state");
2885 a2dp.a2dp_source_suspended = true;
2886 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
2887 goto param_handled;
2888 list_for_each(node, &a2dp.adev->usecase_list) {
2889 uc_info = node_to_item(node, struct audio_usecase, list);
2890 if (uc_info->type == PCM_PLAYBACK &&
2891 (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
2892 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
2893 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)) {
2894 fp_check_a2dp_restore_l(a2dp.adev, uc_info->stream.out, false);
Aalique Grahame22e49102018-12-18 14:23:57 -08002895 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08002896 }
2897 if (!a2dp.swb_configured)
2898 reset_codec_config();
2899 if (a2dp.audio_source_suspend)
2900 a2dp.audio_source_suspend();
2901 } else if (a2dp.a2dp_source_suspended == true) {
2902 ALOGD("Resetting a2dp suspend state");
2903 struct audio_usecase *uc_info;
2904 struct listnode *node;
2905 if (a2dp.clear_source_a2dpsuspend_flag)
2906 a2dp.clear_source_a2dpsuspend_flag();
2907 a2dp.a2dp_source_suspended = false;
2908 /*
2909 * It is possible that before suspend,a2dp sessions can be active
2910 * for example during music + voice activation concurrency
2911 * a2dp suspend will be called & BT will change to sco mode
2912 * though music is paused as a part of voice activation
2913 * compress session close happens only after pause timeout(10secs)
2914 * so if resume request comes before pause timeout as a2dp session
2915 * is already active IPC start will not be called from APM/audio_hw
2916 * Fix is to call a2dp start for IPC library post suspend
2917 * based on number of active session count
2918 */
2919 if (a2dp.a2dp_source_total_active_session_requests > 0) {
2920 ALOGD(" Calling IPC lib start post suspend state");
2921 if (a2dp.audio_source_start) {
2922 ret = a2dp.audio_source_start();
2923 if (ret != 0) {
2924 ALOGE("BT controller start failed");
2925 a2dp.a2dp_source_started = false;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302926 }
2927 }
Weiyin Jiang280ea742020-09-08 20:28:22 +08002928 }
2929 list_for_each(node, &a2dp.adev->usecase_list) {
2930 uc_info = node_to_item(node, struct audio_usecase, list);
2931 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
2932 is_a2dp_out_device_type(&uc_info->stream.out->device_list)) {
2933 fp_check_a2dp_restore_l(a2dp.adev, uc_info->stream.out, true);
Zhou Song10617ed2017-05-26 13:28:48 +08002934 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302935 }
2936 }
2937 goto param_handled;
Weiyin Jiang280ea742020-09-08 20:28:22 +08002938 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002939
Weiyin Jiang280ea742020-09-08 20:28:22 +08002940 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
2941 sizeof(value));
2942 if (ret >= 0) {
2943 if (a2dp.is_a2dp_offload_supported &&
2944 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2945 *reconfig = true;
2946 }
2947 goto param_handled;
2948 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002949
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302950param_handled:
Weiyin Jiang280ea742020-09-08 20:28:22 +08002951 ALOGV("end of a2dp setparam");
2952 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302953}
2954
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002955void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302956{
2957 a2dp.is_handoff_in_progress = is_on;
2958}
2959
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002960bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302961{
2962 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05302963 // Or if a2dp device is selected but earlier start failed ( as a2dp
2964 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02002965 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302966}
2967
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002968void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302969{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302970 *sample_rate = a2dp.enc_sampling_rate;
2971}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302972
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002973void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02002974{
2975 *sample_rate = a2dp.dec_sampling_rate;
2976}
2977
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002978bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302979{
2980 bool ret = false;
2981
Florian Pfister1a84f312018-07-19 14:38:18 +02002982 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302983 return ret;
2984
Florian Pfister1a84f312018-07-19 14:38:18 +02002985 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08002986 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002987 (a2dp.audio_source_check_a2dp_ready))
2988 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302989 return ret;
2990}
2991
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002992bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02002993{
2994 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302995}
2996
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002997void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08002998 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302999{
3000 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02003001 a2dp.bt_lib_source_handle = NULL;
3002 a2dp.a2dp_source_started = false;
3003 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
3004 a2dp.a2dp_source_total_active_session_requests = 0;
3005 a2dp.a2dp_source_suspended = false;
3006 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303007 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303008 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07003009 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05303010 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08003011 a2dp.abr_config.is_abr_enabled = false;
3012 a2dp.abr_config.abr_started = false;
3013 a2dp.abr_config.imc_instance = 0;
3014 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08003015 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05303016 a2dp.is_tws_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05303017 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08003018 a2dp.swb_configured = false;
3019
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003020 // init function pointers
3021 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08003022 init_config.fp_platform_get_pcm_device_id;
Weiyin Jiang280ea742020-09-08 20:28:22 +08003023 fp_check_a2dp_restore_l = init_config.fp_check_a2dp_restore_l;
Gautam Manamfbb3ebc2020-10-08 18:06:45 +05303024 fp_platform_switch_voice_call_device_post =
3025 init_config.fp_platform_switch_voice_call_device_post;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003026
kunleiz5a127262017-09-08 14:47:48 +08003027 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05303028 reset_a2dp_source_dec_config_params();
3029 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02003030
3031 a2dp.bt_lib_sink_handle = NULL;
3032 a2dp.a2dp_sink_started = false;
3033 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
3034 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003035
3036 if (is_running_with_enhanced_fwk == UNINITIALIZED)
3037 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
3038 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08003039 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02003040
3041 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303042 update_offload_codec_capabilities();
3043}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003044
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003045uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003046{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003047 uint32_t latency = 0;
3048 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303049 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
3050 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003051 char value[PROPERTY_VALUE_MAX];
3052
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003053 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08003054 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003055 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303056 if (sscanf(value, "%d/%d/%d/%d/%d%d",
3057 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003058 ALOGI("Failed to parse avsync offset params from '%s'.", value);
3059 avsync_runtime_prop = 0;
3060 }
3061 }
3062
yidongh0515e042017-07-06 15:00:34 +08003063 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02003064 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
3065 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08003066 }
3067
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07003068 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003069 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08003070 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
3071 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003072 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003073 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08003074 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
3075 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003076 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003077 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08003078 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
3079 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003080 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003081 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08003082 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
3083 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003084 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003085 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05303086 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
3087 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
3088 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003089 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303090 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
3091 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
3092 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05303093 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05303094 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05303095 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08003096 case CODEC_TYPE_PCM:
3097 latency = ENCODER_LATENCY_PCM;
3098 latency += DEFAULT_SINK_LATENCY_PCM;
3099 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003100 default:
3101 latency = 200;
3102 break;
3103 }
3104 return latency;
3105}
Aalique Grahame22e49102018-12-18 14:23:57 -08003106
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003107int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003108 struct str_parms *reply)
3109{
3110 int ret, val = 0;
3111 char value[32]={0};
3112
3113 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3114 value, sizeof(value));
3115 if (ret >= 0) {
3116 val = a2dp.is_a2dp_offload_supported;
3117 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3118 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3119 }
3120
3121 return 0;
3122}
Zhou Song12c29502019-03-16 10:37:18 +08003123
3124
3125bool configure_aptx_ad_speech_enc_fmt() {
3126 struct mixer_ctl *ctl_enc_data = NULL;
3127 int mixer_size = 0;
3128 int ret = 0;
3129 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3130
3131 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3132 if (!ctl_enc_data) {
3133 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3134 return false;
3135 }
3136
3137 /* Initialize dsp configuration params */
3138 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3139 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3140 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3141 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3142 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3143 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3144 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3145 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3146 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3147 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3148 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3149
3150 /* Configure AFE DSP configuration */
3151 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3152 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3153 mixer_size);
3154 if (ret != 0) {
3155 ALOGE("%s: Failed to set SWB encoder config", __func__);
3156 return false;
3157 }
3158
3159 /* Configure AFE Input Bit Format as PCM_16 */
3160 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3161 if (ret != 0) {
3162 ALOGE("%s: Failed to set SWB bit format", __func__);
3163 return false;
3164 }
3165
3166 return true;
3167}
3168
3169bool configure_aptx_ad_speech_dec_fmt()
3170{
3171 struct mixer_ctl *ctl_dec_data = NULL;
3172 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3173 int ret = 0;
3174
3175 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3176 if (!ctl_dec_data) {
3177 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3178 return false;
3179 }
3180 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3181 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3182 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3183 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3184 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3185 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3186 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3187 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3188
3189 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3190 sizeof(dec_cfg));
3191 if (ret != 0) {
3192 ALOGE("%s: Failed to set decoder config", __func__);
3193 return false;
3194 }
3195 return true;
3196}
3197
3198int sco_start_configuration()
3199{
3200 ALOGD("sco_start_configuration start");
3201
3202 if (!a2dp.swb_configured) {
Gautam Manamfbb3ebc2020-10-08 18:06:45 +05303203 /*Before starting sco config, we must ensure to set correct acdb id
3204 because sco cofiguration will trigger port open which needs acdb_id*/
3205 fp_platform_switch_voice_call_device_post(a2dp.adev->platform,
3206 SND_DEVICE_OUT_BT_SCO_SWB,
3207 SND_DEVICE_IN_BT_SCO_MIC_SWB);
3208
Zhou Song12c29502019-03-16 10:37:18 +08003209 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3210 /* Configure AFE codec*/
3211 if (configure_aptx_ad_speech_enc_fmt() &&
3212 configure_aptx_ad_speech_dec_fmt()) {
3213 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3214 } else {
3215 ALOGE("%s: failed to send SCO configuration", __func__);
3216 return -ETIMEDOUT;
3217 }
3218 /* Configure backend*/
3219 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3220 a2dp.enc_channels = CH_MONO;
3221 a2dp.abr_config.is_abr_enabled = true;
3222 a2dp_set_backend_cfg(SOURCE);
3223 /* Start abr*/
3224 start_abr();
3225 a2dp.swb_configured = true;
3226 }
3227 return 0;
3228}
3229
3230void sco_reset_configuration()
3231{
Zhou Songd6d71752019-05-21 18:08:51 +08003232 if (a2dp.swb_configured) {
3233 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003234
Zhou Songd6d71752019-05-21 18:08:51 +08003235 reset_codec_config();
3236 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3237 a2dp.swb_configured = false;
3238 }
Zhou Song12c29502019-03-16 10:37:18 +08003239}