blob: 105c88d91fc787c7205df20ddff1ee5f847ec044 [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;
258fp_check_a2dp_restore_t fp_check_a2dp_restore;
259
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800260/* PCM config for ABR Feedback hostless front end */
261static struct pcm_config pcm_config_abr = {
262 .channels = 1,
263 .rate = 8000,
264 .period_size = 240,
265 .period_count = 2,
266 .format = PCM_FORMAT_S16_LE,
267 .start_threshold = 0,
268 .stop_threshold = INT_MAX,
269 .avail_min = 0,
270};
271
272/* Adaptive bitrate config for A2DP codecs */
273struct a2dp_abr_config {
274 /* Flag to denote whether Adaptive bitrate is enabled for codec */
275 bool is_abr_enabled;
276 /* Flag to denote whether front end has been opened for ABR */
277 bool abr_started;
278 /* ABR Tx path pcm handle */
279 struct pcm *abr_tx_handle;
Zhou Song12c29502019-03-16 10:37:18 +0800280 /* ABR Rx path pcm handle */
281 struct pcm *abr_rx_handle;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800282 /* ABR Inter Module Communication (IMC) instance ID */
283 uint32_t imc_instance;
284};
285
286static uint32_t instance_id = MAX_INSTANCE_ID;
287
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530288/* structure used to update a2dp state machine
289 * to communicate IPC library
290 * to store DSP encoder configuration information
291 */
292struct a2dp_data {
293 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200294 void *bt_lib_source_handle;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530295 bt_audio_pre_init_t bt_audio_pre_init;
Florian Pfister1a84f312018-07-19 14:38:18 +0200296 audio_source_open_t audio_source_open;
297 audio_source_close_t audio_source_close;
298 audio_source_start_t audio_source_start;
299 audio_source_stop_t audio_source_stop;
300 audio_source_suspend_t audio_source_suspend;
301 audio_source_handoff_triggered_t audio_source_handoff_triggered;
302 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
303 audio_get_enc_config_t audio_get_enc_config;
304 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530305 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200306 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
307 enum A2DP_STATE bt_state_source;
308 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530309 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530310 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200311 bool a2dp_source_started;
312 bool a2dp_source_suspended;
313 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530314 bool is_a2dp_offload_supported;
315 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700316 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530317 /* Mono Mode support for TWS+ */
318 bool is_tws_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530319 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800320 /* Adaptive bitrate config for A2DP codecs */
321 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200322
323 void *bt_lib_sink_handle;
324 audio_sink_start_t audio_sink_start;
325 audio_sink_stop_t audio_sink_stop;
326 audio_get_dec_config_t audio_get_dec_config;
327 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
328 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
329 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
330 enum A2DP_STATE bt_state_sink;
331 codec_t bt_decoder_format;
332 uint32_t dec_sampling_rate;
333 uint32_t dec_channels;
334 bool a2dp_sink_started;
335 int a2dp_sink_total_active_session_requests;
Zhou Song12c29502019-03-16 10:37:18 +0800336 bool swb_configured;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530337};
338
339struct a2dp_data a2dp;
340
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800341/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
342 * Structures sent to configure DSP for ABR are defined below.
343 * This data helps DSP configure feedback path (BTSoC to LPASS)
344 * for link quality levels and mapping quality levels to codec
345 * specific bitrate.
346 */
347
348/* Key value pair for link quality level to bitrate mapping. */
349struct bit_rate_level_map_t {
350 uint32_t link_quality_level;
351 uint32_t bitrate;
352};
353
354/* Link quality level to bitrate mapping info sent to DSP. */
355struct quality_level_to_bitrate_info {
356 /* Number of quality levels being mapped.
357 * This will be equal to the size of mapping table.
358 */
359 uint32_t num_levels;
360 /* Quality level to bitrate mapping table */
361 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
362};
363
364/* Structure to set up Inter Module Communication (IMC) between
365 * AFE Decoder and Encoder.
366 */
367struct imc_dec_enc_info {
368 /* Decoder to encoder communication direction.
369 * Transmit = 0 / Receive = 1
370 */
371 uint32_t direction;
372 /* Enable / disable IMC between decoder and encoder */
373 uint32_t enable;
374 /* Purpose of IMC being set up between decoder and encoder.
375 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
376 * is the default value to be sent as purpose.
377 */
378 uint32_t purpose;
379 /* Unique communication instance ID.
380 * purpose and comm_instance together form the actual key
381 * used in IMC registration, which must be the same for
382 * encoder and decoder for which IMC is being set up.
383 */
384 uint32_t comm_instance;
385};
386
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700387/* Structure to control frame size of AAC encoded frames. */
388struct aac_frame_size_control_t {
389 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
390 uint32_t ctl_type;
391 /* Control value
392 * MTU_SIZE: MTU size in bytes
393 * PEAK_BIT_RATE: Peak bitrate in bits per second.
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530394 * BIT_RATE_MODE: Variable bitrate
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700395 */
396 uint32_t ctl_value;
397};
398
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800399/* Structure used for ABR config of AFE encoder and decoder. */
400struct abr_enc_cfg_t {
401 /* Link quality level to bitrate mapping info sent to DSP. */
402 struct quality_level_to_bitrate_info mapping_info;
403 /* Information to set up IMC between decoder and encoder */
404 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700405 /* Flag to indicate whether ABR is enabled */
406 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800407} __attribute__ ((packed));
408
409/* Structure to send configuration for decoder introduced
410 * on AFE Tx path for ABR link quality feedback to BT encoder.
411 */
412struct abr_dec_cfg_t {
413 /* Decoder media format */
414 uint32_t dec_format;
415 /* Information to set up IMC between decoder and encoder */
416 struct imc_dec_enc_info imc_info;
417} __attribute__ ((packed));
418
Zhou Song12c29502019-03-16 10:37:18 +0800419struct aptx_ad_speech_mode_cfg_t
420{
421 uint32_t mode;
422 uint32_t swapping;
423} __attribute__ ((packed));
424
425/* Structure for SWB voice dec config */
426struct aptx_ad_speech_dec_cfg_t {
427 struct abr_dec_cfg_t abr_cfg;
428 struct aptx_ad_speech_mode_cfg_t speech_mode;
429} __attribute__ ((packed));
430
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530431/* START of DSP configurable structures
432 * These values should match with DSP interface defintion
433 */
434
435/* AAC encoder configuration structure. */
436typedef struct aac_enc_cfg_t aac_enc_cfg_t;
437
438/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
439 * supported aac_fmt_flag are ADTS/RAW
440 * supported channel_cfg are Native mode, Mono , Stereo
441 */
442struct aac_enc_cfg_t {
443 uint32_t enc_format;
444 uint32_t bit_rate;
445 uint32_t enc_mode;
446 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530447 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530448 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530449} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530450
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700451struct aac_enc_cfg_v2_t {
452 struct aac_enc_cfg_t aac_enc_cfg;
453 struct aac_frame_size_control_t frame_ctl;
454} __attribute__ ((packed));
455
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530456struct aac_enc_cfg_v3_t {
457 struct aac_enc_cfg_t aac_enc_cfg;
458 struct aac_frame_size_control_t frame_ctl;
459 struct aac_frame_size_control_t aac_key_value_ctl;
460} __attribute__ ((packed));
461
Surendar Karkabbb3c822018-11-12 13:00:38 +0530462typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
463struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200464 uint16_t aac_fmt_flag; /* LATM*/
465 uint16_t audio_object_type; /* LC */
466 uint16_t channels; /* Stereo */
467 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
468 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
469 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530470} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200471
Surendar Karkabbb3c822018-11-12 13:00:38 +0530472typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
473struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200474 uint16_t channels; /* Mono, Stereo */
475 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
476 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530477} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200478
479/* AAC decoder configuration structure. */
480typedef struct aac_dec_cfg_t aac_dec_cfg_t;
481struct aac_dec_cfg_t {
482 uint32_t dec_format;
483 audio_aac_decoder_config_t data;
484} __attribute__ ((packed));
485
486/* SBC decoder configuration structure. */
487typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
488struct sbc_dec_cfg_t {
489 uint32_t dec_format;
490 audio_sbc_decoder_config_t data;
491} __attribute__ ((packed));
492
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530493/* SBC encoder configuration structure. */
494typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
495
496/* supported num_subbands are 4/8
497 * supported blk_len are 4, 8, 12, 16
498 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
499 * supported alloc_method are LOUNDNESS/SNR
500 * supported bit_rate for mono channel is max 320kbps
501 * supported bit rate for stereo channel is max 512 kbps
502 */
503struct sbc_enc_cfg_t{
504 uint32_t enc_format;
505 uint32_t num_subbands;
506 uint32_t blk_len;
507 uint32_t channel_mode;
508 uint32_t alloc_method;
509 uint32_t bit_rate;
510 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530511} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530512
513
514/* supported num_channels are Mono/Stereo
515 * supported channel_mapping for mono is CHANNEL_C
516 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
517 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700518 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530519struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530520{
521 uint32_t enc_format;
522 uint32_t sample_rate;
523 uint16_t num_channels;
524 uint16_t reserved;
525 uint8_t channel_mapping[8];
526 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530527} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530528
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530529struct celt_specific_enc_cfg_t
530{
531 uint32_t bit_rate;
532 uint16_t frame_size;
533 uint16_t complexity;
534 uint16_t prediction_mode;
535 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530536} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530537
538struct celt_enc_cfg_t
539{
540 struct custom_enc_cfg_t custom_cfg;
541 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530542} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700543
544/* sync_mode introduced with APTX V2 libraries
545 * sync mode: 0x0 = stereo sync mode
546 * 0x01 = dual mono sync mode
547 * 0x02 = dual mono with no sync on either L or R codewords
548 */
549struct aptx_v2_enc_cfg_ext_t
550{
551 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530552} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700553
554/* APTX struct for combining custom enc and V2 fields */
555struct aptx_enc_cfg_t
556{
557 struct custom_enc_cfg_t custom_cfg;
558 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530559} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700560
Sharad Sangle95d451b2018-06-19 12:24:20 +0530561/* APTX AD structure */
562struct aptx_ad_enc_cfg_ext_t
563{
564 uint32_t sampling_freq;
565 uint32_t mtu;
566 uint32_t channel_mode;
567 uint32_t min_sink_modeA;
568 uint32_t max_sink_modeA;
569 uint32_t min_sink_modeB;
570 uint32_t max_sink_modeB;
571 uint32_t min_sink_modeC;
572 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530573 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530574} __attribute__ ((packed));
575
576struct aptx_ad_enc_cfg_t
577{
578 struct custom_enc_cfg_t custom_cfg;
579 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
580 struct abr_enc_cfg_t abr_cfg;
581} __attribute__ ((packed));
582
Zhou Song8fccbb62019-03-20 01:08:19 +0800583struct aptx_ad_enc_cfg_ext_r2_t
584{
585 uint32_t sampling_freq;
586 uint32_t mtu;
587 uint32_t channel_mode;
588 uint32_t min_sink_modeA;
589 uint32_t max_sink_modeA;
590 uint32_t min_sink_modeB;
591 uint32_t max_sink_modeB;
592 uint32_t min_sink_modeC;
593 uint32_t max_sink_modeC;
594 uint32_t mode;
595 uint32_t input_mode;
596 uint32_t fade_duration;
597 uint8_t sink_cap[11];
598} __attribute__ ((packed));
599
600struct aptx_ad_enc_cfg_r2_t
601{
602 struct custom_enc_cfg_t custom_cfg;
603 struct aptx_ad_enc_cfg_ext_r2_t aptx_ad_cfg;
604 struct abr_enc_cfg_t abr_cfg;
605} __attribute__ ((packed));
606
Zhou Song12c29502019-03-16 10:37:18 +0800607/* APTX AD SPEECH structure */
608struct aptx_ad_speech_enc_cfg_t
609{
610 struct custom_enc_cfg_t custom_cfg;
611 /* Information to set up IMC between decoder and encoder */
612 struct imc_dec_enc_info imc_info;
613 struct aptx_ad_speech_mode_cfg_t speech_mode;
614} __attribute__ ((packed));
615
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530616struct ldac_specific_enc_cfg_t
617{
618 uint32_t bit_rate;
619 uint16_t channel_mode;
620 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530621} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530622
623struct ldac_enc_cfg_t
624{
625 struct custom_enc_cfg_t custom_cfg;
626 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800627 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530628} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530629
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530630/* In LE BT source code uses system/audio.h for below
631 * structure definition. To avoid multiple definition
632 * compilation error for audiohal in LE , masking structure
633 * definition under "LINUX_ENABLED" which is defined only
634 * in LE
635 */
636#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530637/* TODO: Define the following structures only for O using PLATFORM_VERSION */
638/* Information about BT SBC encoder configuration
639 * This data is used between audio HAL module and
640 * BT IPC library to configure DSP encoder
641 */
642typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530643 uint32_t subband; /* 4, 8 */
644 uint32_t blk_len; /* 4, 8, 12, 16 */
645 uint16_t sampling_rate; /*44.1khz,48khz*/
646 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
647 uint8_t alloc; /*0(Loudness),1(SNR)*/
648 uint8_t min_bitpool; /* 2 */
649 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
650 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530651 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530652} audio_sbc_encoder_config;
653
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530654/* Information about BT APTX encoder configuration
655 * This data is used between audio HAL module and
656 * BT IPC library to configure DSP encoder
657 */
658typedef struct {
659 uint16_t sampling_rate;
660 uint8_t channels;
661 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530662 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700663} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530664
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700665typedef struct {
Zhou Song8fccbb62019-03-20 01:08:19 +0800666 uint32_t sampling_rate;
667 uint32_t mtu;
668 int32_t channel_mode;
669 uint32_t min_sink_modeA;
670 uint32_t max_sink_modeA;
671 uint32_t min_sink_modeB;
672 uint32_t max_sink_modeB;
673 uint32_t min_sink_modeC;
674 uint32_t max_sink_modeC;
675 uint32_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530676 uint8_t TTP_modeA_low;
677 uint8_t TTP_modeA_high;
678 uint8_t TTP_modeB_low;
679 uint8_t TTP_modeB_high;
Zhou Song8fccbb62019-03-20 01:08:19 +0800680 uint8_t TTP_TWS_low;
681 uint8_t TTP_TWS_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530682 uint32_t bits_per_sample;
Zhou Song8fccbb62019-03-20 01:08:19 +0800683 uint32_t input_mode;
684 uint32_t fade_duration;
685 uint8_t sink_cap[11];
Sharad Sangle95d451b2018-06-19 12:24:20 +0530686} audio_aptx_ad_config;
687
688typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700689 uint16_t sampling_rate;
690 uint8_t channels;
691 uint32_t bitrate;
692 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530693 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700694} audio_aptx_dual_mono_config;
695
696typedef union {
697 audio_aptx_default_config *default_cfg;
698 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530699 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700700} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530701
702/* Information about BT AAC encoder configuration
703 * This data is used between audio HAL module and
704 * BT IPC library to configure DSP encoder
705 */
706typedef struct {
707 uint32_t enc_mode; /* LC, SBR, PS */
708 uint16_t format_flag; /* RAW, ADTS */
709 uint16_t channels; /* 1-Mono, 2-Stereo */
710 uint32_t sampling_rate;
711 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530712 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530713} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530714#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700715
716typedef struct {
717 audio_aac_encoder_config audio_aac_enc_cfg;
718 struct aac_frame_size_control_t frame_ctl;
719} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530720
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530721typedef struct {
722 audio_aac_encoder_config audio_aac_enc_cfg;
723 struct aac_frame_size_control_t frame_ctl;
724 uint8_t size_control_struct;
725 struct aac_frame_size_control_t* frame_ptr_ctl;
726} audio_aac_encoder_config_v3;
727
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530728/* Information about BT CELT encoder configuration
729 * This data is used between audio HAL module and
730 * BT IPC library to configure DSP encoder
731 */
732typedef struct {
733 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
734 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
735 uint16_t frame_size; /* 64-128-256-512, 512 */
736 uint16_t complexity; /* 0-10, 1 */
737 uint16_t prediction_mode; /* 0-1-2, 0 */
738 uint16_t vbr_flag; /* 0-1, 0*/
739 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530740 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530741} audio_celt_encoder_config;
742
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530743/* Information about BT LDAC encoder configuration
744 * This data is used between audio HAL module and
745 * BT IPC library to configure DSP encoder
746 */
747typedef struct {
748 uint32_t sampling_rate; /*44100,48000,88200,96000*/
749 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
750 uint16_t channel_mode; /* 0, 4, 2, 1*/
751 uint16_t mtu; /*679*/
Aniket Kumar Latad0196282019-05-09 14:24:17 -0700752 uint32_t bits_per_sample;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800753 bool is_abr_enabled;
754 struct quality_level_to_bitrate_info level_to_bitrate_map;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530755} audio_ldac_encoder_config;
756
Surendar Karkabbb3c822018-11-12 13:00:38 +0530757/* Information about BT AAC decoder configuration
758 * This data is used between audio HAL module and
759 * BT IPC library to configure DSP decoder
760 */
761typedef struct {
762 uint16_t aac_fmt_flag; /* LATM*/
763 uint16_t audio_object_type; /* LC */
764 uint16_t channels; /* Stereo */
765 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
766 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
767 44.1k, 48k, 64k, 88.2k, 96k */
768} audio_aac_dec_config_t;
769
770/* Information about BT SBC decoder configuration
771 * This data is used between audio HAL module and
772 * BT IPC library to configure DSP decoder
773 */
774typedef struct {
775 uint16_t channels; /* Mono, Stereo */
776 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
777 44.1k, 48k, 64k, 88.2k, 96k */
778}audio_sbc_dec_config_t;
779
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530780/*********** END of DSP configurable structures ********************/
781
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530782static void update_offload_codec_capabilities()
783{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530784
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530785 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800786 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
787 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
788
789 ALOGD("%s: A2DP offload supported = %d",__func__,
790 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530791}
792
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800793static int stop_abr()
794{
795 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800796 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800797 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800798 int ret = 0;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800799
800 /* This function can be used if !abr_started for clean up */
801 ALOGV("%s: enter", __func__);
802
803 // Close hostless front end
804 if (a2dp.abr_config.abr_tx_handle != NULL) {
805 pcm_close(a2dp.abr_config.abr_tx_handle);
806 a2dp.abr_config.abr_tx_handle = NULL;
807 }
Zhou Song12c29502019-03-16 10:37:18 +0800808 if (a2dp.abr_config.abr_rx_handle != NULL) {
809 pcm_close(a2dp.abr_config.abr_rx_handle);
810 a2dp.abr_config.abr_rx_handle = NULL;
811 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800812 a2dp.abr_config.abr_started = false;
813 a2dp.abr_config.imc_instance = 0;
814
815 // Reset BT driver mixer control for ABR usecase
816 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
817 MIXER_SET_FEEDBACK_CHANNEL);
818 if (!ctl_set_bt_feedback_channel) {
819 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800820 ret = -ENOSYS;
821 } else if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800822 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800823 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800824 }
825
826 // Reset ABR Tx feedback path
827 ALOGV("%s: Disable ABR Tx feedback path", __func__);
828 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
829 MIXER_ABR_TX_FEEDBACK_PATH);
830 if (!ctl_abr_tx_path) {
831 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800832 ret = -ENOSYS;
833 } else if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800834 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800835 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800836 }
837
Zhou Song12c29502019-03-16 10:37:18 +0800838 // Reset ABR Rx feedback path
839 ALOGV("%s: Disable ABR Rx feedback path", __func__);
840 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
841 MIXER_ABR_RX_FEEDBACK_PATH);
842 if (!ctl_abr_rx_path) {
843 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
844 ret = -ENOSYS;
845 } else if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 0) != 0) {
846 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
847 ret = -ENOSYS;
848 }
849
850 return ret;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800851}
852
853static int start_abr()
854{
855 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800856 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800857 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
858 int abr_device_id;
859 int ret = 0;
860
861 if (!a2dp.abr_config.is_abr_enabled) {
862 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
863 return -ENOSYS;
864 }
865
866 if (a2dp.abr_config.abr_started) {
867 ALOGI("%s: ABR has already started", __func__);
868 return ret;
869 }
870
871 // Enable Slimbus 7 Tx feedback path
872 ALOGV("%s: Enable ABR Tx feedback path", __func__);
873 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
874 MIXER_ABR_TX_FEEDBACK_PATH);
875 if (!ctl_abr_tx_path) {
876 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
877 return -ENOSYS;
878 }
879 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
880 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
881 return -ENOSYS;
882 }
883
884 // Notify ABR usecase information to BT driver to distinguish
885 // between SCO and feedback usecase
886 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
887 MIXER_SET_FEEDBACK_CHANNEL);
888 if (!ctl_set_bt_feedback_channel) {
889 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800890 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800891 }
892 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
893 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800894 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800895 }
896
897 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800898 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800899 PCM_CAPTURE);
900 if (!a2dp.abr_config.abr_tx_handle) {
901 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
902 abr_device_id, PCM_IN,
903 &pcm_config_abr);
Zhou Song12c29502019-03-16 10:37:18 +0800904 if (a2dp.abr_config.abr_tx_handle == NULL) {
905 ALOGE("%s: Can't open abr tx device", __func__);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800906 goto fail;
Zhou Song12c29502019-03-16 10:37:18 +0800907 }
908 if (!(pcm_is_ready(a2dp.abr_config.abr_tx_handle) &&
909 !pcm_start(a2dp.abr_config.abr_tx_handle))) {
910 ALOGE("%s: tx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
911 goto fail;
912 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800913 }
Zhou Song12c29502019-03-16 10:37:18 +0800914
915 // Enable Slimbus 7 Rx feedback path for HD Voice use case
916 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH) {
917 ALOGV("%s: Enable ABR Rx feedback path", __func__);
918 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
919 MIXER_ABR_RX_FEEDBACK_PATH);
920 if (!ctl_abr_rx_path) {
921 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
922 goto fail;
923 }
924 if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 1) != 0) {
925 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
926 goto fail;
927 }
928
929 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
930 ALOGE("%s: Failed to set BT usecase", __func__);
931 goto fail;
932 }
933
934 // Open hostless front end and prepare ABR Rx path
935 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
936 PCM_PLAYBACK);
937 if (!a2dp.abr_config.abr_rx_handle) {
938 a2dp.abr_config.abr_rx_handle = pcm_open(a2dp.adev->snd_card,
939 abr_device_id, PCM_OUT,
940 &pcm_config_abr);
941 if (a2dp.abr_config.abr_rx_handle == NULL) {
942 ALOGE("%s: Can't open abr rx device", __func__);
943 goto fail;
944 }
945 if (!(pcm_is_ready(a2dp.abr_config.abr_rx_handle) &&
946 !pcm_start(a2dp.abr_config.abr_rx_handle))) {
947 ALOGE("%s: rx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_rx_handle));
948 goto fail;
949 }
950 }
951 }
952
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800953 a2dp.abr_config.abr_started = true;
954
955 return ret;
956
957fail:
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800958 stop_abr();
959 return -ENOSYS;
960}
961
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800962static int check_if_enhanced_fwk() {
963
964 int is_enhanced_fwk = 1;
965 //dlopen lib
966 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
967 if (vndk_fwk_lib_handle != NULL) {
968 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
969 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
970 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
971 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
972 __func__);
973 is_enhanced_fwk = 1;
974 } else {
975 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
976 }
977 }
978 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
979 return is_enhanced_fwk;
980}
981
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530982static void open_a2dp_source() {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530983 int ret = 0;
984
Florian Pfister1a84f312018-07-19 14:38:18 +0200985 ALOGD(" Open A2DP source start ");
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530986
987 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
988 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
989 ALOGD("calling BT stream open");
990 ret = a2dp.audio_source_open();
991 if(ret != 0) {
992 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Srinu Jellada99a592019-01-25 16:50:52 +0530993 }
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530994 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530995 } else {
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530996 ALOGD("Called a2dp open with improper state %d", a2dp.bt_state_source);
997 }
998 } else {
999 ALOGE("a2dp handle is not identified, Ignoring open request");
1000 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
1001 }
1002}
1003/* API to open BT IPC library to start IPC communication for BT Source*/
1004static void a2dp_source_init()
1005{
1006 ALOGD("a2dp_source_init START");
1007 if (a2dp.bt_lib_source_handle == NULL) {
1008 ALOGD("Requesting for BT lib handle");
1009 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
1010 if (a2dp.bt_lib_source_handle == NULL) {
1011 ALOGE("%s: dlopen failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
1012 return;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301013 }
1014 }
1015
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301016 a2dp.bt_audio_pre_init = (bt_audio_pre_init_t)
1017 dlsym(a2dp.bt_lib_source_handle, "bt_audio_pre_init");
Srinu Jellada99a592019-01-25 16:50:52 +05301018 a2dp.audio_source_open = (audio_source_open_t)
1019 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
1020 a2dp.audio_source_start = (audio_source_start_t)
1021 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
1022 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
1023 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
1024 a2dp.audio_source_suspend = (audio_source_suspend_t)
1025 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
1026 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
1027 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
1028 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1029 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
1030 a2dp.audio_source_stop = (audio_source_stop_t)
1031 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
1032 a2dp.audio_source_close = (audio_source_close_t)
1033 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
1034 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
1035 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
1036 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
1037 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
1038 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1039 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1040 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1041 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1042
Srinu Jella793f5302019-07-19 15:53:55 +05301043 if (a2dp.bt_lib_source_handle && a2dp.bt_audio_pre_init) {
1044 ALOGD("calling BT module preinit");
1045 // fwk related check's will be done in the BT layer
1046 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001047 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001048}
1049
1050/* API to open BT IPC library to start IPC communication for BT Sink*/
1051static void open_a2dp_sink()
1052{
1053 ALOGD(" Open A2DP input start ");
1054 if (a2dp.bt_lib_sink_handle == NULL){
1055 ALOGD(" Requesting for BT lib handle");
1056 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1057
1058 if (a2dp.bt_lib_sink_handle == NULL) {
1059 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1060 } else {
1061 a2dp.audio_sink_start = (audio_sink_start_t)
1062 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1063 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1064 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1065 a2dp.audio_sink_stop = (audio_sink_stop_t)
1066 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1067 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1068 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1069 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1070 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1071 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301072 }
1073}
1074
1075static int close_a2dp_output()
1076{
1077 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001078
1079 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1080 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301081 return -ENOSYS;
1082 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001083
1084 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1085 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001086 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001087 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301088 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001089 a2dp.a2dp_source_started = false;
1090 a2dp.a2dp_source_total_active_session_requests = 0;
1091 a2dp.a2dp_source_suspended = false;
1092 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301093 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301094 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001095 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001096 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1097 stop_abr();
1098 a2dp.abr_config.is_abr_enabled = false;
1099 a2dp.abr_config.abr_started = false;
1100 a2dp.abr_config.imc_instance = 0;
1101 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001102 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301103 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001104
1105 return 0;
1106}
1107
1108static int close_a2dp_input()
1109{
1110 ALOGV("%s\n",__func__);
1111
1112 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1113 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1114 return -ENOSYS;
1115 }
1116
1117 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1118 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001119 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001120 ALOGE("failed close a2dp sink control path from BT library");
1121 }
1122 a2dp.a2dp_sink_started = false;
1123 a2dp.a2dp_sink_total_active_session_requests = 0;
1124 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1125 a2dp.dec_sampling_rate = 48000;
1126 a2dp.dec_channels = 2;
1127 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301128
1129 return 0;
1130}
1131
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301132static void a2dp_check_and_set_scrambler()
1133{
1134 bool scrambler_mode = false;
1135 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001136 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1137 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301138
1139 if (scrambler_mode) {
1140 //enable scrambler in dsp
1141 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1142 MIXER_SCRAMBLER_MODE);
1143 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001144 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301145 return;
1146 } else {
1147 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1148 ALOGE("%s: Could not set scrambler mode", __func__);
1149 return;
1150 }
1151 }
1152 }
1153}
1154
Florian Pfister1a84f312018-07-19 14:38:18 +02001155static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301156{
Florian Pfister1a84f312018-07-19 14:38:18 +02001157 char *rate_str = NULL, *channels = NULL;
1158 uint32_t sampling_rate;
1159 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1160 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301161
Florian Pfister1a84f312018-07-19 14:38:18 +02001162 if (direction == SINK) {
1163 sampling_rate = a2dp.dec_sampling_rate;
1164 } else {
1165 sampling_rate = a2dp.enc_sampling_rate;
1166 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001167 /*
1168 * For LDAC encoder and AAC decoder open slimbus port at
1169 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1170 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1171 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001172 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301173 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001174 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1175 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301176 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1177 sampling_rate = sampling_rate *2;
1178 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001179
Aalique Grahame22e49102018-12-18 14:23:57 -08001180 // No need to configure backend for PCM format.
1181 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1182 return 0;
1183 }
1184
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301185 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301186 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301187 case 44100:
1188 rate_str = "KHZ_44P1";
1189 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301190 case 88200:
1191 rate_str = "KHZ_88P2";
1192 break;
1193 case 96000:
1194 rate_str = "KHZ_96";
1195 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001196 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301197 default:
1198 rate_str = "KHZ_48";
1199 break;
1200 }
1201
Florian Pfister1a84f312018-07-19 14:38:18 +02001202 if (direction == SINK) {
1203 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1204 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301205 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001206 } else {
1207 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1208 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301209 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001210 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301211 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001212
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301213 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1214 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301215 is_configured = false;
1216 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301217 }
1218
Surendar Karka2febd452018-12-13 17:56:43 +05301219 if (direction == SOURCE) {
1220 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001221 if (a2dp.abr_config.is_abr_enabled) {
1222 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH)
1223 rate_str = SPEECH_TX_SAMPLE_RATE;
1224 else
1225 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301226
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301227 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1228 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1229 MIXER_SOURCE_SAMPLE_RATE_TX);
1230 if (!ctl_sample_rate) {
1231 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1232 is_configured = false;
1233 goto fail;
1234 }
1235
1236 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1237 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1238 is_configured = false;
1239 goto fail;
1240 }
Surendar Karka2febd452018-12-13 17:56:43 +05301241 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301242 }
1243 } else {
1244 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1245 MIXER_SAMPLE_RATE_TX is not supported */
1246 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1247 MIXER_SAMPLE_RATE_DEFAULT);
1248 if (!ctl_sample_rate) {
1249 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301250 is_configured = false;
1251 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301252 }
1253
1254 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1255 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001256 is_configured = false;
1257 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301258 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301259 }
1260
Florian Pfister1a84f312018-07-19 14:38:18 +02001261 if (direction == SINK) {
1262 switch (a2dp.dec_channels) {
1263 case 1:
1264 channels = "One";
1265 break;
1266 case 2:
1267 default:
1268 channels = "Two";
1269 break;
1270 }
1271
Ramu Gottipati02809682018-12-19 16:46:12 +05301272 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001273 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1274 MIXER_AFE_SINK_CHANNELS);
1275 } else {
1276 //Configure AFE enc channels
1277 switch (a2dp.enc_channels) {
1278 case 1:
1279 channels = "One";
1280 break;
1281 case 2:
1282 default:
1283 channels = "Two";
1284 break;
1285 }
1286
Ramu Gottipati02809682018-12-19 16:46:12 +05301287 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001288 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1289 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301290 }
1291
Florian Pfister1a84f312018-07-19 14:38:18 +02001292 if (!ctrl_channels) {
1293 ALOGE(" ERROR AFE channels mixer control not identified");
1294 } else {
1295 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301296 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001297 is_configured = false;
1298 goto fail;
1299 }
1300 }
1301 is_configured = true;
1302fail:
1303 return is_configured;
1304}
1305
Aniket Kumar Lata990de552019-07-11 14:20:23 -07001306bool a2dp_set_source_backend_cfg()
1307{
1308 if (a2dp.a2dp_source_started && !a2dp.a2dp_source_suspended)
1309 return a2dp_set_backend_cfg(SOURCE);
1310
1311 return false;
1312}
1313
Surendar Karkabbb3c822018-11-12 13:00:38 +05301314bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001315{
1316 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1317 struct aac_dec_cfg_t aac_dsp_cfg;
1318 bool is_configured = false;
1319 int ret = 0;
1320
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001321 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001322 return false;
1323
Surendar Karka2febd452018-12-13 17:56:43 +05301324 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001325 if (!ctl_dec_data) {
1326 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1327 is_configured = false;
1328 goto fail;
1329 }
1330
1331 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1332 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1333 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1334 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1335 switch(aac_bt_cfg->audio_object_type) {
1336 case 0:
1337 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301338 break;
1339 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001340 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1341 break;
1342 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301343 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001344 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301345 break;
1346 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001347 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1348 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1349 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1350 sizeof(struct aac_dec_cfg_t));
1351 if (ret != 0) {
1352 ALOGE("%s: failed to set AAC decoder config", __func__);
1353 is_configured = false;
1354 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001355 }
1356
Florian Pfister1a84f312018-07-19 14:38:18 +02001357 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1358 MIXER_DEC_BIT_FORMAT);
1359 if (!ctrl_bit_format) {
1360 ALOGE(" ERROR Dec bit format mixer control not identified");
1361 is_configured = false;
1362 goto fail;
1363 }
1364 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1365 if (ret != 0) {
1366 ALOGE("%s: Failed to set bit format to decoder", __func__);
1367 is_configured = false;
1368 goto fail;
1369 }
1370
1371 is_configured = true;
1372 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1373 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1374 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1375 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1376 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1377fail:
1378 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301379}
1380
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301381static int a2dp_set_bit_format(uint32_t enc_bit_format)
1382{
1383 const char *bit_format = NULL;
1384 struct mixer_ctl *ctrl_bit_format = NULL;
1385
1386 // Configure AFE Input Bit Format
1387 switch (enc_bit_format) {
1388 case 32:
1389 bit_format = "S32_LE";
1390 break;
1391 case 24:
1392 bit_format = "S24_LE";
1393 break;
1394 case 16:
1395 default:
1396 bit_format = "S16_LE";
1397 break;
1398 }
1399
1400 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1401 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1402 MIXER_ENC_BIT_FORMAT);
1403 if (!ctrl_bit_format) {
1404 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1405 return -ENOSYS;
1406 }
1407 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1408 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1409 return -ENOSYS;
1410 }
1411 return 0;
1412}
1413
Surendar Karka2febd452018-12-13 17:56:43 +05301414static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301415{
Surendar Karka2febd452018-12-13 17:56:43 +05301416 const char *rate_str = "KHZ_8", *channels = "Zero";
1417 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1418 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301419
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001420 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301421 if (direction == SINK) {
1422 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1423 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1424 MIXER_SINK_SAMPLE_RATE);
1425 } else {
1426 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1427 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1428 MIXER_SAMPLE_RATE_RX);
1429 }
1430 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301431
Surendar Karka2febd452018-12-13 17:56:43 +05301432 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1433 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301434 return -ENOSYS;
1435 }
Zhou Song12c29502019-03-16 10:37:18 +08001436 if (a2dp.abr_config.is_abr_enabled) {
1437 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1438 MIXER_SOURCE_SAMPLE_RATE_TX);
1439 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301440 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1441 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001442 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301443
Zhou Song12c29502019-03-16 10:37:18 +08001444 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1445 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1446 return -ENOSYS;
1447 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301448 }
1449 } else {
1450
Surendar Karka2febd452018-12-13 17:56:43 +05301451 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301452 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301453 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301454 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1455 return -ENOSYS;
1456 }
1457
Surendar Karka2febd452018-12-13 17:56:43 +05301458 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301459 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1460 return -ENOSYS;
1461 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001462 }
1463
1464 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301465 if (direction == SINK) {
1466 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1467 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1468 MIXER_AFE_SINK_CHANNELS);
1469 } else {
1470 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1471 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1472 MIXER_AFE_IN_CHANNELS);
1473 }
1474 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001475 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1476 return -ENOSYS;
1477 }
Surendar Karka2febd452018-12-13 17:56:43 +05301478 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001479 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1480 return -ENOSYS;
1481 }
1482
1483 return 0;
1484}
1485
1486/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301487static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001488{
1489 struct mixer_ctl *ctl_dec_data = NULL;
1490 struct abr_dec_cfg_t dec_cfg;
1491 int ret = 0;
1492
1493 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301494 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001495 if (!ctl_dec_data) {
1496 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1497 return false;
1498 }
1499 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1500 dec_cfg.dec_format = dec_format;
1501 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1502 dec_cfg.imc_info.enable = IMC_ENABLE;
1503 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1504 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1505
1506 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1507 sizeof(dec_cfg));
1508 if (ret != 0) {
1509 ALOGE("%s: Failed to set decoder config", __func__);
1510 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301511 }
Surendar Karka2febd452018-12-13 17:56:43 +05301512 }
1513
1514 return true;
1515}
1516
Surendar Karkabbb3c822018-11-12 13:00:38 +05301517bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001518{
1519 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1520 struct sbc_dec_cfg_t sbc_dsp_cfg;
1521 bool is_configured = false;
1522 int ret = 0;
1523
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001524 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001525 goto fail;
1526
Surendar Karka2febd452018-12-13 17:56:43 +05301527 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001528 if (!ctl_dec_data) {
1529 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1530 is_configured = false;
1531 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301532 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001533
Florian Pfister1a84f312018-07-19 14:38:18 +02001534 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1535 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1536 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1537 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1538 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1539 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301540
Florian Pfister1a84f312018-07-19 14:38:18 +02001541 if (ret != 0) {
1542 ALOGE("%s: failed to set SBC decoder config", __func__);
1543 is_configured = false;
1544 goto fail;
1545 }
1546
1547 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1548 MIXER_DEC_BIT_FORMAT);
1549 if (!ctrl_bit_format) {
1550 ALOGE(" ERROR Dec bit format mixer control not identified");
1551 is_configured = false;
1552 goto fail;
1553 }
1554 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1555 if (ret != 0) {
1556 ALOGE("%s: Failed to set bit format to decoder", __func__);
1557 is_configured = false;
1558 goto fail;
1559 }
1560
1561 is_configured = true;
1562 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1563 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1564 a2dp.dec_channels = 1;
1565 else
1566 a2dp.dec_channels = 2;
1567 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1568 ALOGV("Successfully updated SBC dec format");
1569fail:
1570 return is_configured;
1571}
1572
Florian Pfister1a84f312018-07-19 14:38:18 +02001573/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301574static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301575{
Florian Pfister1a84f312018-07-19 14:38:18 +02001576 void *codec_info = NULL;
1577 codec_t codec_type = CODEC_TYPE_INVALID;
1578 bool is_configured = false;
1579 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301580
Florian Pfister1a84f312018-07-19 14:38:18 +02001581 if (!a2dp.audio_get_dec_config) {
1582 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1583 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301584 }
1585
Surendar Karka2febd452018-12-13 17:56:43 +05301586 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001587 if (!ctl_dec_data) {
1588 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1589 is_configured = false;
1590 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301591 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001592 codec_info = a2dp.audio_get_dec_config(&codec_type);
1593 switch(codec_type) {
1594 case CODEC_TYPE_SBC:
1595 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301596 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001597 break;
1598 case CODEC_TYPE_AAC:
1599 ALOGD(" AAC decoder supported BT device");
1600 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301601 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001602 break;
1603 default:
1604 ALOGD(" Received Unsupported decoder format");
1605 is_configured = false;
1606 break;
1607 }
1608 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301609}
1610
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301611/* API to configure SBC DSP encoder */
1612bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1613{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301614 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301615 struct sbc_enc_cfg_t sbc_dsp_cfg;
1616 bool is_configured = false;
1617 int ret = 0;
1618
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001619 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301620 return false;
1621
1622 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1623 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001624 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301625 is_configured = false;
1626 goto fail;
1627 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301628 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001629 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301630 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1631 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1632 switch(sbc_bt_cfg->channels) {
1633 case 0:
1634 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1635 break;
1636 case 1:
1637 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1638 break;
1639 case 3:
1640 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1641 break;
1642 case 2:
1643 default:
1644 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1645 break;
1646 }
1647 if (sbc_bt_cfg->alloc)
1648 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1649 else
1650 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1651 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1652 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1653 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1654 sizeof(struct sbc_enc_cfg_t));
1655 if (ret != 0) {
1656 ALOGE("%s: failed to set SBC encoder config", __func__);
1657 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301658 goto fail;
1659 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301660 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301661 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301662 is_configured = false;
1663 goto fail;
1664 }
1665 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001666 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301667 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301668
1669 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1670 a2dp.enc_channels = 1;
1671 else
1672 a2dp.enc_channels = 2;
1673
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301674 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1675 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301676fail:
1677 return is_configured;
1678}
1679
Manish Dewangan6a252632017-12-04 17:27:44 +05301680#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301681static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1682 audio_aptx_encoder_config *aptx_bt_cfg)
1683{
1684 int ret = 0;
1685
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001686 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301687 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1688 aptx_dsp_cfg, aptx_bt_cfg);
1689 return -EINVAL;
1690 }
1691
1692 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301693 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301694
1695
1696 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1697 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1698 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1699 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1700 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1701 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1702 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1703 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1704 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301705 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301706 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1707 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1708 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1709 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1710
1711
1712 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1713 case APTX_AD_CHANNEL_UNCHANGED:
1714 case APTX_AD_CHANNEL_JOINT_STEREO:
1715 case APTX_AD_CHANNEL_DUAL_MONO:
1716 case APTX_AD_CHANNEL_STEREO_TWS:
1717 case APTX_AD_CHANNEL_EARBUD:
1718 default:
1719 a2dp.enc_channels = CH_STEREO;
1720 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1721 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1722 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1723 break;
1724 case APTX_AD_CHANNEL_MONO:
1725 a2dp.enc_channels = CH_MONO;
1726 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1727 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001728 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301729 }
1730 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1731 case APTX_AD_SR_UNCHANGED:
1732 case APTX_AD_48:
1733 default:
1734 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1735 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1736 break;
1737 case APTX_AD_44_1:
1738 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1739 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001740 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301741 }
1742 ALOGV("Successfully updated APTX AD enc format with \
1743 samplingrate: %d channels:%d",
1744 aptx_dsp_cfg->custom_cfg.sample_rate,
1745 aptx_dsp_cfg->custom_cfg.num_channels);
1746
1747 return ret;
1748}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301749
Zhou Song8fccbb62019-03-20 01:08:19 +08001750static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
1751 audio_aptx_encoder_config *aptx_bt_cfg)
1752{
1753 int ret = 0;
1754
1755 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Mingshu Pangaa429f72019-06-10 17:25:00 +08001756 ALOGE("Invalid param, aptx_dsp_cfg %pK aptx_bt_cfg %pK",
Zhou Song8fccbb62019-03-20 01:08:19 +08001757 aptx_dsp_cfg, aptx_bt_cfg);
1758 return -EINVAL;
1759 }
1760
1761 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
1762 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
1763
1764
1765 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1766 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1767 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1768 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1769 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1770 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1771 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1772 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1773 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
1774 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
1775 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
1776 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
1777 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
1778 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
1779 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1780 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1781 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1782 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1783
1784
1785 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1786 case APTX_AD_CHANNEL_UNCHANGED:
1787 case APTX_AD_CHANNEL_JOINT_STEREO:
1788 case APTX_AD_CHANNEL_DUAL_MONO:
1789 case APTX_AD_CHANNEL_STEREO_TWS:
1790 case APTX_AD_CHANNEL_EARBUD:
1791 default:
1792 a2dp.enc_channels = CH_STEREO;
1793 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1794 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1795 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1796 break;
1797 case APTX_AD_CHANNEL_MONO:
1798 a2dp.enc_channels = CH_MONO;
1799 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1800 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001801 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001802 }
1803 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1804 case APTX_AD_SR_UNCHANGED:
1805 case APTX_AD_48:
1806 default:
1807 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1808 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1809 break;
1810 case APTX_AD_44_1:
1811 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1812 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001813 break;
1814 case APTX_AD_96:
1815 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
1816 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_96K;
1817 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001818 }
1819 ALOGV("Successfully updated APTX AD enc format with \
1820 samplingrate: %d channels:%d",
1821 aptx_dsp_cfg->custom_cfg.sample_rate,
1822 aptx_dsp_cfg->custom_cfg.num_channels);
1823
1824 return ret;
1825}
1826
Manisha Agarwala51768b2018-11-01 16:30:52 +05301827static void audio_a2dp_update_tws_channel_mode()
1828{
1829 char* channel_mode;
1830 struct mixer_ctl *ctl_channel_mode;
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301831
1832 ALOGD("Update tws for mono_mode on=%d",a2dp.is_tws_mono_mode_on);
1833
Manisha Agarwala51768b2018-11-01 16:30:52 +05301834 if (a2dp.is_tws_mono_mode_on)
1835 channel_mode = "One";
1836 else
1837 channel_mode = "Two";
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301838
Manisha Agarwala51768b2018-11-01 16:30:52 +05301839 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1840 if (!ctl_channel_mode) {
1841 ALOGE("failed to get tws mixer ctl");
1842 return;
1843 }
1844 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1845 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1846 return;
1847 }
1848}
1849
Manish Dewangan6a252632017-12-04 17:27:44 +05301850static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
1851 audio_aptx_encoder_config *aptx_bt_cfg)
1852{
1853 int ret = 0;
1854
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001855 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301856 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1857 aptx_dsp_cfg, aptx_bt_cfg);
1858 return -EINVAL;
1859 }
1860
1861 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001862 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301863
1864 if (!a2dp.is_aptx_dual_mono_supported) {
1865 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1866 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1867 } else {
1868 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1869 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1870 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1871 }
1872
1873 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
1874 case 1:
1875 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1876 break;
1877 case 2:
1878 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05301879 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1880 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
Manish Dewangan6a252632017-12-04 17:27:44 +05301881 break;
1882 }
1883 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
1884 if (!a2dp.is_aptx_dual_mono_supported) {
1885 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1886 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
1887 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
1888 aptx_dsp_cfg->custom_cfg.num_channels);
1889 } else {
1890 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1891 ALOGV("Successfully updated APTX dual mono enc format with \
1892 samplingrate: %d channels:%d syncmode %d",
1893 aptx_dsp_cfg->custom_cfg.sample_rate,
1894 aptx_dsp_cfg->custom_cfg.num_channels,
1895 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
1896 }
1897 return ret;
1898}
1899#else
1900static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
1901 audio_aptx_encoder_config *aptx_bt_cfg)
1902{
1903 int ret = 0;
1904
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001905 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301906 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1907 aptx_dsp_cfg, aptx_bt_cfg);
1908 return -EINVAL;
1909 }
1910
1911 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001912 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301913 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
1914 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
1915 switch(aptx_dsp_cfg->num_channels) {
1916 case 1:
1917 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
1918 break;
1919 case 2:
1920 default:
1921 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
1922 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
1923 break;
1924 }
1925
1926 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
1927 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
1928
1929 return ret;
1930}
1931#endif
1932
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301933/* API to configure APTX DSP encoder */
1934bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1935{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301936 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08001937 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001938 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301939 bool is_configured = false;
1940 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08001941 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301942
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001943 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301944 return false;
1945
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001946 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1947 if (!ctl_enc_data) {
1948 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1949 return false;
1950 }
1951
Manish Dewangan6a252632017-12-04 17:27:44 +05301952#ifndef LINUX_ENABLED
1953 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301954 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08001955 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001956 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001957 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
1958 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001959 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08001960 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001961 else
Zhou Song8fccbb62019-03-20 01:08:19 +08001962 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001963 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001964 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001965
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001966 if (ret) {
1967 is_configured = false;
1968 goto fail;
1969 }
1970
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001971 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001972 if (aptx_ad_ctl)
1973 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001974 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08001975 else
1976 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001977 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001978 } else {
1979 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001980 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05301981 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301982#else
1983 struct custom_enc_cfg_t aptx_dsp_cfg;
1984 mixer_size = sizeof(struct custom_enc_cfg_t);
1985 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05301986 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05301987 if (ret) {
1988 is_configured = false;
1989 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301990 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001991 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1992 mixer_size);
1993#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301994 if (ret != 0) {
1995 ALOGE("%s: Failed to set APTX encoder config", __func__);
1996 is_configured = false;
1997 goto fail;
1998 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301999#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002000 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302001 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002002 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302003 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
2004 else
2005 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302006#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302007 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302008 is_configured = false;
2009 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302010 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302011 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302012 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05302013 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302014 else
Surendar Karka2febd452018-12-13 17:56:43 +05302015 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302016fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05302017 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002018 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05302019 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302020 return is_configured;
2021}
2022
2023/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302024 */
Manish Dewangan6a252632017-12-04 17:27:44 +05302025#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002026bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302027#else
2028bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2029#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302030{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302031 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302032 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302033 bool is_configured = false;
2034 int ret = 0;
2035
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002036 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302037 return false;
2038
2039 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2040 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002041 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302042 is_configured = false;
2043 goto fail;
2044 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302045
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302046 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002047 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302048 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2049 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2050 switch(aptx_dsp_cfg.num_channels) {
2051 case 1:
2052 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2053 break;
2054 case 2:
2055 default:
2056 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2057 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2058 break;
2059 }
2060 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302061 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302062 if (ret != 0) {
2063 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2064 is_configured = false;
2065 goto fail;
2066 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302067 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302068 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302069 is_configured = false;
2070 goto fail;
2071 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302072 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002073 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302074 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302075 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302076 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2077 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302078fail:
2079 return is_configured;
2080}
2081
2082/* API to configure AAC DSP encoder */
2083bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2084{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302085 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302086 struct aac_enc_cfg_t aac_dsp_cfg;
2087 bool is_configured = false;
2088 int ret = 0;
2089
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002090 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302091 return false;
2092
2093 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2094 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002095 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302096 is_configured = false;
2097 goto fail;
2098 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302099 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002100 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302101 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302102 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002103 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302104 case 0:
2105 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2106 break;
2107 case 2:
2108 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2109 break;
2110 case 1:
2111 default:
2112 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2113 break;
2114 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302115 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302116 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002117
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302118 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2119 sizeof(struct aac_enc_cfg_t));
2120 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002121 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302122 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302123 goto fail;
2124 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302125 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302126 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302127 is_configured = false;
2128 goto fail;
2129 }
2130 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002131 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302132 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002133 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002134 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2135 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2136fail:
2137 return is_configured;
2138}
2139
2140bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2141{
2142 struct mixer_ctl *ctl_enc_data = NULL;
2143 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2144 bool is_configured = false;
2145 int ret = 0;
2146
2147 if (aac_bt_cfg == NULL)
2148 return false;
2149
2150 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2151 if (!ctl_enc_data) {
2152 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2153 is_configured = false;
2154 goto fail;
2155 }
2156 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302157 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002158 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2159 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2160 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2161 case 0:
2162 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2163 break;
2164 case 2:
2165 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2166 break;
2167 case 1:
2168 default:
2169 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2170 break;
2171 }
2172 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2173 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2174 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2175 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2176
2177 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2178 sizeof(struct aac_enc_cfg_v2_t));
2179 if (ret != 0) {
2180 ALOGE("%s: Failed to set AAC encoder config", __func__);
2181 is_configured = false;
2182 goto fail;
2183 }
2184 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2185 if (ret != 0) {
2186 is_configured = false;
2187 goto fail;
2188 }
2189 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302190 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002191 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2192 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2193 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2194 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302195fail:
2196 return is_configured;
2197}
2198
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302199bool configure_aac_enc_format_v3(audio_aac_encoder_config_v3 *aac_bt_cfg)
2200{
2201 struct mixer_ctl *ctl_enc_data = NULL;
2202 struct aac_enc_cfg_v3_t aac_dsp_cfg;
2203 struct aac_frame_size_control_t* frame_vbr_ctl = NULL;
2204 bool is_configured = false;
2205 int ret = 0;
2206
2207 if (aac_bt_cfg == NULL)
2208 return false;
2209
2210 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2211 if (!ctl_enc_data) {
2212 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2213 is_configured = false;
2214 goto fail;
2215 }
2216 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v3_t));
2217 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
2218 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2219 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2220 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2221 case 0:
2222 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2223 break;
2224 case 2:
2225 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2226 break;
2227 case 1:
2228 default:
2229 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2230 break;
2231 }
2232 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2233 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2234 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2235 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2236 frame_vbr_ctl = aac_bt_cfg->frame_ptr_ctl;
2237
2238 if (frame_vbr_ctl != NULL) {
2239 aac_dsp_cfg.aac_key_value_ctl.ctl_type = frame_vbr_ctl->ctl_type;
2240 aac_dsp_cfg.aac_key_value_ctl.ctl_value = frame_vbr_ctl->ctl_value;
2241 } else {
2242 ALOGE("%s: VBR cannot be enabled, fall back to default",__func__);
2243 aac_dsp_cfg.aac_key_value_ctl.ctl_type = 0;
2244 aac_dsp_cfg.aac_key_value_ctl.ctl_value = 0;
2245 }
2246
2247 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2248 sizeof(struct aac_enc_cfg_v3_t));
2249 if (ret != 0) {
2250 ALOGE("%s: Failed to set AAC encoder config", __func__);
2251 is_configured = false;
2252 goto fail;
2253 }
2254 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2255 if (ret != 0) {
2256 is_configured = false;
2257 goto fail;
2258 }
2259 is_configured = true;
2260 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
2261 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2262 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2263 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2264 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
2265fail:
2266 return is_configured;
2267}
2268
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302269bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2270{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302271 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302272 struct celt_enc_cfg_t celt_dsp_cfg;
2273 bool is_configured = false;
2274 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002275 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302276 return false;
2277
2278 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2279 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002280 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302281 is_configured = false;
2282 goto fail;
2283 }
2284 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2285
Florian Pfister1a84f312018-07-19 14:38:18 +02002286 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302287 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2288 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2289 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2290 case 1:
2291 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2292 break;
2293 case 2:
2294 default:
2295 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2296 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2297 break;
2298 }
2299
2300 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2301
2302 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2303 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2304 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2305 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2306 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2307
2308 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2309 sizeof(struct celt_enc_cfg_t));
2310 if (ret != 0) {
2311 ALOGE("%s: Failed to set CELT encoder config", __func__);
2312 is_configured = false;
2313 goto fail;
2314 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302315 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302316 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302317 is_configured = false;
2318 goto fail;
2319 }
2320 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002321 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302322 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302323 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302324 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2325 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2326fail:
2327 return is_configured;
2328}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302329
2330bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2331{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302332 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302333 struct ldac_enc_cfg_t ldac_dsp_cfg;
2334 bool is_configured = false;
2335 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002336 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302337 return false;
2338
2339 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2340 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002341 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302342 is_configured = false;
2343 goto fail;
2344 }
2345 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2346
Florian Pfister1a84f312018-07-19 14:38:18 +02002347 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302348 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2349 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2350 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2351 case 4:
2352 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2353 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2354 break;
2355 case 2:
2356 case 1:
2357 default:
2358 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2359 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2360 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2361 break;
2362 }
2363
2364 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2365 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2366 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002367 if (ldac_bt_cfg->is_abr_enabled) {
2368 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2369 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2370 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2371 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2372 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002373 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002374 }
2375
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302376 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2377 sizeof(struct ldac_enc_cfg_t));
2378 if (ret != 0) {
2379 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2380 is_configured = false;
2381 goto fail;
2382 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302383 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302384 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302385 is_configured = false;
2386 goto fail;
2387 }
2388 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002389 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302390 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2391 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002392 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302393 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2394 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2395fail:
2396 return is_configured;
2397}
2398
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302399bool configure_a2dp_encoder_format()
2400{
2401 void *codec_info = NULL;
2402 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002403 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302404 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002405 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302406
Florian Pfister1a84f312018-07-19 14:38:18 +02002407 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302408 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2409 return false;
2410 }
2411 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002412 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302413 &codec_type);
2414
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002415 // ABR disabled by default for all codecs
2416 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302417 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002418
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302419 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002420 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302421 ALOGD(" Received SBC encoder supported BT device");
2422 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002423 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302424 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002425 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302426 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302427#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002428 a2dp.is_aptx_dual_mono_supported = false;
2429 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302430#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302431 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002432 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302433 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002434 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302435 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302436#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302437 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002438 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302439#else
2440 is_configured =
2441 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2442#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002443 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302444#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002445 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002446 ALOGD(" Received APTX dual mono encoder supported BT device");
2447 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302448 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2449 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002450 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2451 is_configured =
2452 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302453 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302454#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002455 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302456 ALOGD(" Received AAC encoder supported BT device");
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302457 bool is_aac_vbr_enabled =
2458 property_get_bool("persist.vendor.bt.aac_vbr_frm_ctl.enabled", false);
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002459 bool is_aac_frame_ctl_enabled =
2460 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302461 if (is_aac_vbr_enabled)
2462 is_configured = configure_aac_enc_format_v3((audio_aac_encoder_config_v3 *) codec_info);
2463 else
2464 is_configured = is_aac_frame_ctl_enabled ?
2465 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2466 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302467 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002468 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302469 ALOGD(" Received CELT encoder supported BT device");
2470 is_configured =
2471 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2472 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002473 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302474 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002475 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2476 instance_id = MAX_INSTANCE_ID;
2477 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302478 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002479 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302480 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302481 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302482#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302483 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302484 ALOGD(" Received APTX AD encoder supported BT device");
2485 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2486 instance_id = MAX_INSTANCE_ID;
2487 a2dp.abr_config.imc_instance = instance_id--;
2488 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2489 a2dp.is_aptx_adaptive = true;
2490 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2491 is_configured =
2492 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302493 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302494 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302495#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002496 case CODEC_TYPE_PCM:
2497 ALOGD("Received PCM format for BT device");
2498 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2499 is_configured = true;
2500 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302501 default:
2502 ALOGD(" Received Unsupported encoder formar");
2503 is_configured = false;
2504 break;
2505 }
2506 return is_configured;
2507}
2508
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002509int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302510{
2511 int ret = 0;
2512
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002513 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302514
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002515 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002516 && a2dp.audio_get_enc_config)) {
2517 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302518 return -ENOSYS;
2519 }
2520
Zhou Song12c29502019-03-16 10:37:18 +08002521 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302522 //session will be restarted after suspend completion
2523 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302524 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302525 }
2526
Florian Pfister1a84f312018-07-19 14:38:18 +02002527 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302528 ALOGD("calling BT module stream start");
2529 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002530 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302531 ALOGE("BT controller start return = %d",ret);
2532 if (ret != 0 ) {
2533 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002534 a2dp.a2dp_source_started = false;
Revathi Uddarajub26e3932020-06-10 14:51:02 +05302535 ret = -ETIMEDOUT;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302536 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002537 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002538 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302539 ret = 0;
2540 ALOGD("Start playback successful to BT library");
2541 } else {
2542 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002543 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302544 ret = -ETIMEDOUT;
2545 }
2546 }
2547 }
2548
Florian Pfister1a84f312018-07-19 14:38:18 +02002549 if (a2dp.a2dp_source_started) {
2550 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302551 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302552 audio_a2dp_update_tws_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002553 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002554 if (a2dp.abr_config.is_abr_enabled)
2555 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302556 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302557
2558 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002559 a2dp.a2dp_source_total_active_session_requests);
2560 return ret;
2561}
2562
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002563uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002564{
2565 uint32_t latency = 0;
2566
2567 switch(a2dp.bt_decoder_format) {
2568 case CODEC_TYPE_SBC:
2569 latency = DEFAULT_SINK_LATENCY_SBC;
2570 break;
2571 case CODEC_TYPE_AAC:
2572 latency = DEFAULT_SINK_LATENCY_AAC;
2573 break;
2574 default:
2575 latency = 200;
2576 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2577 break;
2578 }
2579 return (uint64_t)latency;
2580}
2581
2582bool a2dp_send_sink_setup_complete(void) {
2583 uint64_t system_latency = 0;
2584 bool is_complete = false;
2585
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002586 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002587
2588 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2589 is_complete = true;
2590 }
2591 return is_complete;
2592}
2593
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002594bool a2dp_sink_is_ready()
2595{
2596 bool ret = false;
2597
2598 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2599 (a2dp.is_a2dp_offload_supported) &&
2600 (a2dp.audio_sink_check_a2dp_ready))
2601 ret = a2dp.audio_sink_check_a2dp_ready();
2602 return ret;
2603}
2604
2605int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002606{
2607 int ret = 0;
2608
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002609 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002610
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002611 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002612 && a2dp.audio_get_dec_config)) {
2613 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2614 return -ENOSYS;
2615 }
2616
2617 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2618 ALOGD("calling BT module stream start");
2619 /* This call indicates BT IPC lib to start capture */
2620 ret = a2dp.audio_sink_start();
2621 ALOGE("BT controller start capture return = %d",ret);
2622 if (ret != 0 ) {
2623 ALOGE("BT controller start capture failed");
2624 a2dp.a2dp_sink_started = false;
2625 } else {
2626
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002627 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002628 ALOGD("Wait for capture ready not successful");
2629 ret = -ETIMEDOUT;
2630 }
2631
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002632 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002633 a2dp.a2dp_sink_started = true;
2634 ret = 0;
2635 ALOGD("Start capture successful to BT library");
2636 } else {
2637 ALOGD(" unable to configure DSP decoder");
2638 a2dp.a2dp_sink_started = false;
2639 ret = -ETIMEDOUT;
2640 }
2641
2642 if (!a2dp_send_sink_setup_complete()) {
2643 ALOGD("sink_setup_complete not successful");
2644 ret = -ETIMEDOUT;
2645 }
2646 }
2647 }
2648
2649 if (a2dp.a2dp_sink_started) {
2650 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002651 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002652 }
2653 }
2654
2655 ALOGD("start A2DP sink total active sessions :%d",
2656 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302657 return ret;
2658}
2659
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302660static void reset_a2dp_enc_config_params()
2661{
2662 int ret =0;
2663
Aalique Grahame22e49102018-12-18 14:23:57 -08002664 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302665 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302666 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302667
2668 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
2669 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
2670 MIXER_ENC_CONFIG_BLOCK);
2671 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002672 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302673 } else {
2674 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
2675 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002676 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302677 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002678
2679 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2680
Manisha Agarwala51768b2018-11-01 16:30:52 +05302681 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2682
2683 if (!ctl_channel_mode) {
2684 ALOGE("failed to get tws mixer ctl");
2685 } else {
2686 channel_mode = "Two";
2687 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2688 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2689 }
2690 a2dp.is_tws_mono_mode_on = false;
2691 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302692}
2693
Surendar Karka2febd452018-12-13 17:56:43 +05302694static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002695{
2696 struct mixer_ctl *ctl_dec_data = NULL;
2697 struct abr_dec_cfg_t dummy_reset_cfg;
2698 int ret = 0;
2699
Preetam Singh Ranawat25780e42019-07-31 18:15:57 +05302700 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
2701 if (!ctl_dec_data) {
2702 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
2703 return -EINVAL;
2704 }
2705 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
2706 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
2707 sizeof(dummy_reset_cfg));
2708 if (ret != 0) {
2709 ALOGE("%s: Failed to set dummy decoder config", __func__);
2710 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002711 }
2712
2713 return ret;
2714}
2715
Surendar Karka2febd452018-12-13 17:56:43 +05302716static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02002717{
2718 int ret =0;
2719
2720 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
2721 struct aac_dec_cfg_t dummy_reset_config;
2722
2723 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
2724 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05302725 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02002726 if (!ctl_dec_config) {
2727 ALOGE(" ERROR a2dp decoder format mixer control not identified");
2728 } else {
2729 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
2730 sizeof(struct aac_dec_cfg_t));
2731 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
2732 }
2733 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
2734 MIXER_DEC_BIT_FORMAT);
2735 if (!ctrl_bit_format) {
2736 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
2737 } else {
2738 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
2739 if (ret != 0) {
2740 ALOGE("%s: Failed to set bit format to decoder", __func__);
2741 }
2742 }
2743}
2744
Zhou Song12c29502019-03-16 10:37:18 +08002745static void reset_codec_config()
2746{
2747 reset_a2dp_enc_config_params();
2748 reset_a2dp_source_dec_config_params();
2749 a2dp_reset_backend_cfg(SOURCE);
2750 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
2751 stop_abr();
2752 a2dp.abr_config.is_abr_enabled = false;
2753}
2754
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002755int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302756{
2757 int ret =0;
2758
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002759 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002760 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002761 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302762 return -ENOSYS;
2763 }
2764
Florian Pfister1a84f312018-07-19 14:38:18 +02002765 if (a2dp.a2dp_source_total_active_session_requests > 0)
2766 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08002767 else
2768 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302769
Florian Pfister1a84f312018-07-19 14:38:18 +02002770 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302771 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02002772 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302773 if (ret < 0)
2774 ALOGE("stop stream to BT IPC lib failed");
2775 else
2776 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08002777 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
2778 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05302779 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302780 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002781 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002782 a2dp.a2dp_source_started = false;
2783 ALOGD("Stop A2DP playback, total active sessions :%d",
2784 a2dp.a2dp_source_total_active_session_requests);
2785 return 0;
2786}
2787
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002788int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002789{
2790 int ret =0;
2791
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002792 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002793 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002794 ALOGE("a2dp handle is not identified, Ignoring stop request");
2795 return -ENOSYS;
2796 }
2797
2798 if (a2dp.a2dp_sink_total_active_session_requests > 0)
2799 a2dp.a2dp_sink_total_active_session_requests--;
2800
2801 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2802 ALOGV("calling BT module stream stop");
2803 ret = a2dp.audio_sink_stop();
2804 if (ret < 0)
2805 ALOGE("stop stream to BT IPC lib failed");
2806 else
2807 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05302808 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002809 a2dp_reset_backend_cfg(SINK);
2810 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002811 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002812 a2dp.a2dp_source_started = false;
2813 ALOGD("Stop A2DP capture, total active sessions :%d",
2814 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302815 return 0;
2816}
2817
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002818int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302819{
Aalique Grahame22e49102018-12-18 14:23:57 -08002820 int ret = 0, val, status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302821 char value[32]={0};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302822 struct audio_usecase *uc_info;
2823 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302824
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002825 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002826 ALOGV("no supported encoders identified,ignoring a2dp setparam");
2827 status = -EINVAL;
2828 goto param_handled;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302829 }
2830
2831 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
2832 sizeof(value));
Zhou Song681350a2017-10-19 16:28:42 +08002833 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302834 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002835 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002836 ALOGV("Received device connect request for A2DP source");
2837 open_a2dp_source();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302838 }
2839 goto param_handled;
2840 }
2841
2842 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
2843 sizeof(value));
2844
Zhou Song681350a2017-10-19 16:28:42 +08002845 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302846 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002847 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002848 ALOGV("Received source device dis- connect request");
Samyak Jain4828f4c2018-08-24 16:31:06 +05302849 close_a2dp_output();
kunleiz4a1fad62018-02-08 18:00:16 +08002850 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302851 reset_a2dp_source_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002852 a2dp_reset_backend_cfg(SOURCE);
2853 } else if (audio_is_a2dp_in_device(val)) {
2854 ALOGV("Received sink device dis- connect request");
2855 close_a2dp_input();
Surendar Karka2febd452018-12-13 17:56:43 +05302856 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002857 a2dp_reset_backend_cfg(SINK);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302858 }
2859 goto param_handled;
2860 }
Surendar Karka2febd452018-12-13 17:56:43 +05302861#ifndef LINUX_ENABLED
Manisha Agarwala51768b2018-11-01 16:30:52 +05302862 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
2863 if (ret>=0) {
2864 ALOGD("Setting tws channel mode to %s",value);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002865 if (!(strncmp(value,"mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302866 a2dp.is_tws_mono_mode_on = true;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002867 else if (!(strncmp(value,"dual-mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302868 a2dp.is_tws_mono_mode_on = false;
2869 audio_a2dp_update_tws_channel_mode();
2870 goto param_handled;
2871 }
Surendar Karka2febd452018-12-13 17:56:43 +05302872#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302873 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
2874 if (ret >= 0) {
Surendar Karka2febd452018-12-13 17:56:43 +05302875 if (a2dp.bt_lib_source_handle) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302876 if ((!strncmp(value,"true",sizeof(value)))) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002877 if (a2dp.a2dp_source_suspended) {
2878 ALOGD("%s: A2DP is already suspended", __func__);
2879 goto param_handled;
2880 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302881 ALOGD("Setting a2dp to suspend state");
Florian Pfister1a84f312018-07-19 14:38:18 +02002882 a2dp.a2dp_source_suspended = true;
Surendar Karka2febd452018-12-13 17:56:43 +05302883 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
yidongh7203cca2018-09-19 16:12:25 +08002884 goto param_handled;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302885 list_for_each(node, &a2dp.adev->usecase_list) {
2886 uc_info = node_to_item(node, struct audio_usecase, list);
Zhou Song47ea2f02019-11-20 00:16:18 +08002887 if (uc_info->type == PCM_PLAYBACK &&
2888 (uc_info->out_snd_device == SND_DEVICE_OUT_BT_A2DP ||
2889 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
2890 uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302891 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002892 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302893 pthread_mutex_lock(&a2dp.adev->lock);
2894 }
2895 }
Zhou Song12c29502019-03-16 10:37:18 +08002896 if (!a2dp.swb_configured)
2897 reset_codec_config();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002898 if (a2dp.audio_source_suspend)
Florian Pfister1a84f312018-07-19 14:38:18 +02002899 a2dp.audio_source_suspend();
2900 } else if (a2dp.a2dp_source_suspended == true) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302901 ALOGD("Resetting a2dp suspend state");
Zhou Song10617ed2017-05-26 13:28:48 +08002902 struct audio_usecase *uc_info;
2903 struct listnode *node;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002904 if (a2dp.clear_source_a2dpsuspend_flag)
Florian Pfister1a84f312018-07-19 14:38:18 +02002905 a2dp.clear_source_a2dpsuspend_flag();
2906 a2dp.a2dp_source_suspended = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302907 /*
2908 * It is possible that before suspend,a2dp sessions can be active
2909 * for example during music + voice activation concurrency
2910 * a2dp suspend will be called & BT will change to sco mode
2911 * though music is paused as a part of voice activation
2912 * compress session close happens only after pause timeout(10secs)
2913 * so if resume request comes before pause timeout as a2dp session
2914 * is already active IPC start will not be called from APM/audio_hw
2915 * Fix is to call a2dp start for IPC library post suspend
2916 * based on number of active session count
2917 */
Florian Pfister1a84f312018-07-19 14:38:18 +02002918 if (a2dp.a2dp_source_total_active_session_requests > 0) {
Naresh Tanniru649871a2016-11-04 18:08:32 +05302919 ALOGD(" Calling IPC lib start post suspend state");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002920 if (a2dp.audio_source_start) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002921 ret = a2dp.audio_source_start();
Naresh Tanniru649871a2016-11-04 18:08:32 +05302922 if (ret != 0) {
2923 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002924 a2dp.a2dp_source_started = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302925 }
2926 }
2927 }
Zhou Song10617ed2017-05-26 13:28:48 +08002928 list_for_each(node, &a2dp.adev->usecase_list) {
2929 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302930 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002931 is_a2dp_out_device_type(&uc_info->stream.out->device_list)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302932 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002933 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302934 pthread_mutex_lock(&a2dp.adev->lock);
2935 }
Zhou Song10617ed2017-05-26 13:28:48 +08002936 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302937 }
2938 }
2939 goto param_handled;
2940 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002941
2942 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
2943 sizeof(value));
2944 if (ret >= 0) {
2945 if (a2dp.is_a2dp_offload_supported &&
2946 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2947 *reconfig = true;
2948 }
2949 goto param_handled;
2950 }
2951
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302952param_handled:
2953 ALOGV("end of a2dp setparam");
Aalique Grahame22e49102018-12-18 14:23:57 -08002954 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302955}
2956
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002957void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302958{
2959 a2dp.is_handoff_in_progress = is_on;
2960}
2961
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002962bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302963{
2964 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05302965 // Or if a2dp device is selected but earlier start failed ( as a2dp
2966 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02002967 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302968}
2969
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002970void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302971{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302972 *sample_rate = a2dp.enc_sampling_rate;
2973}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302974
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002975void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02002976{
2977 *sample_rate = a2dp.dec_sampling_rate;
2978}
2979
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002980bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302981{
2982 bool ret = false;
2983
Florian Pfister1a84f312018-07-19 14:38:18 +02002984 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302985 return ret;
2986
Florian Pfister1a84f312018-07-19 14:38:18 +02002987 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08002988 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002989 (a2dp.audio_source_check_a2dp_ready))
2990 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302991 return ret;
2992}
2993
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002994bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02002995{
2996 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302997}
2998
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002999void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08003000 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303001{
3002 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02003003 a2dp.bt_lib_source_handle = NULL;
3004 a2dp.a2dp_source_started = false;
3005 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
3006 a2dp.a2dp_source_total_active_session_requests = 0;
3007 a2dp.a2dp_source_suspended = false;
3008 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05303009 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303010 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07003011 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05303012 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08003013 a2dp.abr_config.is_abr_enabled = false;
3014 a2dp.abr_config.abr_started = false;
3015 a2dp.abr_config.imc_instance = 0;
3016 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08003017 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05303018 a2dp.is_tws_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05303019 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08003020 a2dp.swb_configured = false;
3021
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003022 // init function pointers
3023 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08003024 init_config.fp_platform_get_pcm_device_id;
3025 fp_check_a2dp_restore = init_config.fp_check_a2dp_restore;
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) {
3203 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3204 /* Configure AFE codec*/
3205 if (configure_aptx_ad_speech_enc_fmt() &&
3206 configure_aptx_ad_speech_dec_fmt()) {
3207 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3208 } else {
3209 ALOGE("%s: failed to send SCO configuration", __func__);
3210 return -ETIMEDOUT;
3211 }
3212 /* Configure backend*/
3213 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3214 a2dp.enc_channels = CH_MONO;
3215 a2dp.abr_config.is_abr_enabled = true;
3216 a2dp_set_backend_cfg(SOURCE);
3217 /* Start abr*/
3218 start_abr();
3219 a2dp.swb_configured = true;
3220 }
3221 return 0;
3222}
3223
3224void sco_reset_configuration()
3225{
Zhou Songd6d71752019-05-21 18:08:51 +08003226 if (a2dp.swb_configured) {
3227 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003228
Zhou Songd6d71752019-05-21 18:08:51 +08003229 reset_codec_config();
3230 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3231 a2dp.swb_configured = false;
3232 }
Zhou Song12c29502019-03-16 10:37:18 +08003233}