blob: e0aebf0dd451dd86797106cc7637ffec59cdc205 [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,
253} frame_control_type_t;
254
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800255// --- external function dependency ---
256fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
257fp_check_a2dp_restore_t fp_check_a2dp_restore;
258
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800259/* PCM config for ABR Feedback hostless front end */
260static struct pcm_config pcm_config_abr = {
261 .channels = 1,
262 .rate = 8000,
263 .period_size = 240,
264 .period_count = 2,
265 .format = PCM_FORMAT_S16_LE,
266 .start_threshold = 0,
267 .stop_threshold = INT_MAX,
268 .avail_min = 0,
269};
270
271/* Adaptive bitrate config for A2DP codecs */
272struct a2dp_abr_config {
273 /* Flag to denote whether Adaptive bitrate is enabled for codec */
274 bool is_abr_enabled;
275 /* Flag to denote whether front end has been opened for ABR */
276 bool abr_started;
277 /* ABR Tx path pcm handle */
278 struct pcm *abr_tx_handle;
Zhou Song12c29502019-03-16 10:37:18 +0800279 /* ABR Rx path pcm handle */
280 struct pcm *abr_rx_handle;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800281 /* ABR Inter Module Communication (IMC) instance ID */
282 uint32_t imc_instance;
283};
284
285static uint32_t instance_id = MAX_INSTANCE_ID;
286
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530287/* structure used to update a2dp state machine
288 * to communicate IPC library
289 * to store DSP encoder configuration information
290 */
291struct a2dp_data {
292 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200293 void *bt_lib_source_handle;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530294 bt_audio_pre_init_t bt_audio_pre_init;
Florian Pfister1a84f312018-07-19 14:38:18 +0200295 audio_source_open_t audio_source_open;
296 audio_source_close_t audio_source_close;
297 audio_source_start_t audio_source_start;
298 audio_source_stop_t audio_source_stop;
299 audio_source_suspend_t audio_source_suspend;
300 audio_source_handoff_triggered_t audio_source_handoff_triggered;
301 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
302 audio_get_enc_config_t audio_get_enc_config;
303 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530304 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200305 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
306 enum A2DP_STATE bt_state_source;
307 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530308 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530309 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200310 bool a2dp_source_started;
311 bool a2dp_source_suspended;
312 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530313 bool is_a2dp_offload_supported;
314 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700315 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530316 /* Mono Mode support for TWS+ */
317 bool is_tws_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530318 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800319 /* Adaptive bitrate config for A2DP codecs */
320 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200321
322 void *bt_lib_sink_handle;
323 audio_sink_start_t audio_sink_start;
324 audio_sink_stop_t audio_sink_stop;
325 audio_get_dec_config_t audio_get_dec_config;
326 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
327 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
328 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
329 enum A2DP_STATE bt_state_sink;
330 codec_t bt_decoder_format;
331 uint32_t dec_sampling_rate;
332 uint32_t dec_channels;
333 bool a2dp_sink_started;
334 int a2dp_sink_total_active_session_requests;
Zhou Song12c29502019-03-16 10:37:18 +0800335 bool swb_configured;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530336};
337
338struct a2dp_data a2dp;
339
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800340/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
341 * Structures sent to configure DSP for ABR are defined below.
342 * This data helps DSP configure feedback path (BTSoC to LPASS)
343 * for link quality levels and mapping quality levels to codec
344 * specific bitrate.
345 */
346
347/* Key value pair for link quality level to bitrate mapping. */
348struct bit_rate_level_map_t {
349 uint32_t link_quality_level;
350 uint32_t bitrate;
351};
352
353/* Link quality level to bitrate mapping info sent to DSP. */
354struct quality_level_to_bitrate_info {
355 /* Number of quality levels being mapped.
356 * This will be equal to the size of mapping table.
357 */
358 uint32_t num_levels;
359 /* Quality level to bitrate mapping table */
360 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
361};
362
363/* Structure to set up Inter Module Communication (IMC) between
364 * AFE Decoder and Encoder.
365 */
366struct imc_dec_enc_info {
367 /* Decoder to encoder communication direction.
368 * Transmit = 0 / Receive = 1
369 */
370 uint32_t direction;
371 /* Enable / disable IMC between decoder and encoder */
372 uint32_t enable;
373 /* Purpose of IMC being set up between decoder and encoder.
374 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
375 * is the default value to be sent as purpose.
376 */
377 uint32_t purpose;
378 /* Unique communication instance ID.
379 * purpose and comm_instance together form the actual key
380 * used in IMC registration, which must be the same for
381 * encoder and decoder for which IMC is being set up.
382 */
383 uint32_t comm_instance;
384};
385
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700386/* Structure to control frame size of AAC encoded frames. */
387struct aac_frame_size_control_t {
388 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
389 uint32_t ctl_type;
390 /* Control value
391 * MTU_SIZE: MTU size in bytes
392 * PEAK_BIT_RATE: Peak bitrate in bits per second.
393 */
394 uint32_t ctl_value;
395};
396
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800397/* Structure used for ABR config of AFE encoder and decoder. */
398struct abr_enc_cfg_t {
399 /* Link quality level to bitrate mapping info sent to DSP. */
400 struct quality_level_to_bitrate_info mapping_info;
401 /* Information to set up IMC between decoder and encoder */
402 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700403 /* Flag to indicate whether ABR is enabled */
404 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800405} __attribute__ ((packed));
406
407/* Structure to send configuration for decoder introduced
408 * on AFE Tx path for ABR link quality feedback to BT encoder.
409 */
410struct abr_dec_cfg_t {
411 /* Decoder media format */
412 uint32_t dec_format;
413 /* Information to set up IMC between decoder and encoder */
414 struct imc_dec_enc_info imc_info;
415} __attribute__ ((packed));
416
Zhou Song12c29502019-03-16 10:37:18 +0800417struct aptx_ad_speech_mode_cfg_t
418{
419 uint32_t mode;
420 uint32_t swapping;
421} __attribute__ ((packed));
422
423/* Structure for SWB voice dec config */
424struct aptx_ad_speech_dec_cfg_t {
425 struct abr_dec_cfg_t abr_cfg;
426 struct aptx_ad_speech_mode_cfg_t speech_mode;
427} __attribute__ ((packed));
428
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530429/* START of DSP configurable structures
430 * These values should match with DSP interface defintion
431 */
432
433/* AAC encoder configuration structure. */
434typedef struct aac_enc_cfg_t aac_enc_cfg_t;
435
436/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
437 * supported aac_fmt_flag are ADTS/RAW
438 * supported channel_cfg are Native mode, Mono , Stereo
439 */
440struct aac_enc_cfg_t {
441 uint32_t enc_format;
442 uint32_t bit_rate;
443 uint32_t enc_mode;
444 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530445 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530446 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530447} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530448
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700449struct aac_enc_cfg_v2_t {
450 struct aac_enc_cfg_t aac_enc_cfg;
451 struct aac_frame_size_control_t frame_ctl;
452} __attribute__ ((packed));
453
Surendar Karkabbb3c822018-11-12 13:00:38 +0530454typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
455struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200456 uint16_t aac_fmt_flag; /* LATM*/
457 uint16_t audio_object_type; /* LC */
458 uint16_t channels; /* Stereo */
459 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
460 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
461 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530462} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200463
Surendar Karkabbb3c822018-11-12 13:00:38 +0530464typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
465struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200466 uint16_t channels; /* Mono, Stereo */
467 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
468 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530469} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200470
471/* AAC decoder configuration structure. */
472typedef struct aac_dec_cfg_t aac_dec_cfg_t;
473struct aac_dec_cfg_t {
474 uint32_t dec_format;
475 audio_aac_decoder_config_t data;
476} __attribute__ ((packed));
477
478/* SBC decoder configuration structure. */
479typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
480struct sbc_dec_cfg_t {
481 uint32_t dec_format;
482 audio_sbc_decoder_config_t data;
483} __attribute__ ((packed));
484
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530485/* SBC encoder configuration structure. */
486typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
487
488/* supported num_subbands are 4/8
489 * supported blk_len are 4, 8, 12, 16
490 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
491 * supported alloc_method are LOUNDNESS/SNR
492 * supported bit_rate for mono channel is max 320kbps
493 * supported bit rate for stereo channel is max 512 kbps
494 */
495struct sbc_enc_cfg_t{
496 uint32_t enc_format;
497 uint32_t num_subbands;
498 uint32_t blk_len;
499 uint32_t channel_mode;
500 uint32_t alloc_method;
501 uint32_t bit_rate;
502 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530503} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530504
505
506/* supported num_channels are Mono/Stereo
507 * supported channel_mapping for mono is CHANNEL_C
508 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
509 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700510 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530511struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530512{
513 uint32_t enc_format;
514 uint32_t sample_rate;
515 uint16_t num_channels;
516 uint16_t reserved;
517 uint8_t channel_mapping[8];
518 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530519} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530520
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530521struct celt_specific_enc_cfg_t
522{
523 uint32_t bit_rate;
524 uint16_t frame_size;
525 uint16_t complexity;
526 uint16_t prediction_mode;
527 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530528} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530529
530struct celt_enc_cfg_t
531{
532 struct custom_enc_cfg_t custom_cfg;
533 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530534} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700535
536/* sync_mode introduced with APTX V2 libraries
537 * sync mode: 0x0 = stereo sync mode
538 * 0x01 = dual mono sync mode
539 * 0x02 = dual mono with no sync on either L or R codewords
540 */
541struct aptx_v2_enc_cfg_ext_t
542{
543 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530544} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700545
546/* APTX struct for combining custom enc and V2 fields */
547struct aptx_enc_cfg_t
548{
549 struct custom_enc_cfg_t custom_cfg;
550 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530551} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700552
Sharad Sangle95d451b2018-06-19 12:24:20 +0530553/* APTX AD structure */
554struct aptx_ad_enc_cfg_ext_t
555{
556 uint32_t sampling_freq;
557 uint32_t mtu;
558 uint32_t channel_mode;
559 uint32_t min_sink_modeA;
560 uint32_t max_sink_modeA;
561 uint32_t min_sink_modeB;
562 uint32_t max_sink_modeB;
563 uint32_t min_sink_modeC;
564 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530565 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530566} __attribute__ ((packed));
567
568struct aptx_ad_enc_cfg_t
569{
570 struct custom_enc_cfg_t custom_cfg;
571 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
572 struct abr_enc_cfg_t abr_cfg;
573} __attribute__ ((packed));
574
Zhou Song8fccbb62019-03-20 01:08:19 +0800575struct aptx_ad_enc_cfg_ext_r2_t
576{
577 uint32_t sampling_freq;
578 uint32_t mtu;
579 uint32_t channel_mode;
580 uint32_t min_sink_modeA;
581 uint32_t max_sink_modeA;
582 uint32_t min_sink_modeB;
583 uint32_t max_sink_modeB;
584 uint32_t min_sink_modeC;
585 uint32_t max_sink_modeC;
586 uint32_t mode;
587 uint32_t input_mode;
588 uint32_t fade_duration;
589 uint8_t sink_cap[11];
590} __attribute__ ((packed));
591
592struct aptx_ad_enc_cfg_r2_t
593{
594 struct custom_enc_cfg_t custom_cfg;
595 struct aptx_ad_enc_cfg_ext_r2_t aptx_ad_cfg;
596 struct abr_enc_cfg_t abr_cfg;
597} __attribute__ ((packed));
598
Zhou Song12c29502019-03-16 10:37:18 +0800599/* APTX AD SPEECH structure */
600struct aptx_ad_speech_enc_cfg_t
601{
602 struct custom_enc_cfg_t custom_cfg;
603 /* Information to set up IMC between decoder and encoder */
604 struct imc_dec_enc_info imc_info;
605 struct aptx_ad_speech_mode_cfg_t speech_mode;
606} __attribute__ ((packed));
607
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530608struct ldac_specific_enc_cfg_t
609{
610 uint32_t bit_rate;
611 uint16_t channel_mode;
612 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530613} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530614
615struct ldac_enc_cfg_t
616{
617 struct custom_enc_cfg_t custom_cfg;
618 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800619 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530620} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530621
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530622/* In LE BT source code uses system/audio.h for below
623 * structure definition. To avoid multiple definition
624 * compilation error for audiohal in LE , masking structure
625 * definition under "LINUX_ENABLED" which is defined only
626 * in LE
627 */
628#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530629/* TODO: Define the following structures only for O using PLATFORM_VERSION */
630/* Information about BT SBC encoder configuration
631 * This data is used between audio HAL module and
632 * BT IPC library to configure DSP encoder
633 */
634typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530635 uint32_t subband; /* 4, 8 */
636 uint32_t blk_len; /* 4, 8, 12, 16 */
637 uint16_t sampling_rate; /*44.1khz,48khz*/
638 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
639 uint8_t alloc; /*0(Loudness),1(SNR)*/
640 uint8_t min_bitpool; /* 2 */
641 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
642 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530643 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530644} audio_sbc_encoder_config;
645
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530646/* Information about BT APTX encoder configuration
647 * This data is used between audio HAL module and
648 * BT IPC library to configure DSP encoder
649 */
650typedef struct {
651 uint16_t sampling_rate;
652 uint8_t channels;
653 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530654 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700655} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530656
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700657typedef struct {
Zhou Song8fccbb62019-03-20 01:08:19 +0800658 uint32_t sampling_rate;
659 uint32_t mtu;
660 int32_t channel_mode;
661 uint32_t min_sink_modeA;
662 uint32_t max_sink_modeA;
663 uint32_t min_sink_modeB;
664 uint32_t max_sink_modeB;
665 uint32_t min_sink_modeC;
666 uint32_t max_sink_modeC;
667 uint32_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530668 uint8_t TTP_modeA_low;
669 uint8_t TTP_modeA_high;
670 uint8_t TTP_modeB_low;
671 uint8_t TTP_modeB_high;
Zhou Song8fccbb62019-03-20 01:08:19 +0800672 uint8_t TTP_TWS_low;
673 uint8_t TTP_TWS_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530674 uint32_t bits_per_sample;
Zhou Song8fccbb62019-03-20 01:08:19 +0800675 uint32_t input_mode;
676 uint32_t fade_duration;
677 uint8_t sink_cap[11];
Sharad Sangle95d451b2018-06-19 12:24:20 +0530678} audio_aptx_ad_config;
679
680typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700681 uint16_t sampling_rate;
682 uint8_t channels;
683 uint32_t bitrate;
684 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530685 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700686} audio_aptx_dual_mono_config;
687
688typedef union {
689 audio_aptx_default_config *default_cfg;
690 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530691 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700692} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530693
694/* Information about BT AAC encoder configuration
695 * This data is used between audio HAL module and
696 * BT IPC library to configure DSP encoder
697 */
698typedef struct {
699 uint32_t enc_mode; /* LC, SBR, PS */
700 uint16_t format_flag; /* RAW, ADTS */
701 uint16_t channels; /* 1-Mono, 2-Stereo */
702 uint32_t sampling_rate;
703 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530704 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530705} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530706#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700707
708typedef struct {
709 audio_aac_encoder_config audio_aac_enc_cfg;
710 struct aac_frame_size_control_t frame_ctl;
711} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530712
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530713/* Information about BT CELT encoder configuration
714 * This data is used between audio HAL module and
715 * BT IPC library to configure DSP encoder
716 */
717typedef struct {
718 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
719 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
720 uint16_t frame_size; /* 64-128-256-512, 512 */
721 uint16_t complexity; /* 0-10, 1 */
722 uint16_t prediction_mode; /* 0-1-2, 0 */
723 uint16_t vbr_flag; /* 0-1, 0*/
724 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530725 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530726} audio_celt_encoder_config;
727
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530728/* Information about BT LDAC 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; /*44100,48000,88200,96000*/
734 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
735 uint16_t channel_mode; /* 0, 4, 2, 1*/
736 uint16_t mtu; /*679*/
Aniket Kumar Latad0196282019-05-09 14:24:17 -0700737 uint32_t bits_per_sample;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800738 bool is_abr_enabled;
739 struct quality_level_to_bitrate_info level_to_bitrate_map;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530740} audio_ldac_encoder_config;
741
Surendar Karkabbb3c822018-11-12 13:00:38 +0530742/* Information about BT AAC decoder configuration
743 * This data is used between audio HAL module and
744 * BT IPC library to configure DSP decoder
745 */
746typedef struct {
747 uint16_t aac_fmt_flag; /* LATM*/
748 uint16_t audio_object_type; /* LC */
749 uint16_t channels; /* Stereo */
750 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
751 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
752 44.1k, 48k, 64k, 88.2k, 96k */
753} audio_aac_dec_config_t;
754
755/* Information about BT SBC decoder configuration
756 * This data is used between audio HAL module and
757 * BT IPC library to configure DSP decoder
758 */
759typedef struct {
760 uint16_t channels; /* Mono, Stereo */
761 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
762 44.1k, 48k, 64k, 88.2k, 96k */
763}audio_sbc_dec_config_t;
764
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530765/*********** END of DSP configurable structures ********************/
766
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530767static void update_offload_codec_capabilities()
768{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530769
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530770 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800771 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
772 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
773
774 ALOGD("%s: A2DP offload supported = %d",__func__,
775 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530776}
777
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800778static int stop_abr()
779{
780 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800781 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800782 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800783 int ret = 0;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800784
785 /* This function can be used if !abr_started for clean up */
786 ALOGV("%s: enter", __func__);
787
788 // Close hostless front end
789 if (a2dp.abr_config.abr_tx_handle != NULL) {
790 pcm_close(a2dp.abr_config.abr_tx_handle);
791 a2dp.abr_config.abr_tx_handle = NULL;
792 }
Zhou Song12c29502019-03-16 10:37:18 +0800793 if (a2dp.abr_config.abr_rx_handle != NULL) {
794 pcm_close(a2dp.abr_config.abr_rx_handle);
795 a2dp.abr_config.abr_rx_handle = NULL;
796 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800797 a2dp.abr_config.abr_started = false;
798 a2dp.abr_config.imc_instance = 0;
799
800 // Reset BT driver mixer control for ABR usecase
801 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
802 MIXER_SET_FEEDBACK_CHANNEL);
803 if (!ctl_set_bt_feedback_channel) {
804 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800805 ret = -ENOSYS;
806 } else if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800807 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800808 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800809 }
810
811 // Reset ABR Tx feedback path
812 ALOGV("%s: Disable ABR Tx feedback path", __func__);
813 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
814 MIXER_ABR_TX_FEEDBACK_PATH);
815 if (!ctl_abr_tx_path) {
816 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800817 ret = -ENOSYS;
818 } else if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800819 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800820 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800821 }
822
Zhou Song12c29502019-03-16 10:37:18 +0800823 // Reset ABR Rx feedback path
824 ALOGV("%s: Disable ABR Rx feedback path", __func__);
825 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
826 MIXER_ABR_RX_FEEDBACK_PATH);
827 if (!ctl_abr_rx_path) {
828 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
829 ret = -ENOSYS;
830 } else if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 0) != 0) {
831 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
832 ret = -ENOSYS;
833 }
834
835 return ret;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800836}
837
838static int start_abr()
839{
840 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800841 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800842 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
843 int abr_device_id;
844 int ret = 0;
845
846 if (!a2dp.abr_config.is_abr_enabled) {
847 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
848 return -ENOSYS;
849 }
850
851 if (a2dp.abr_config.abr_started) {
852 ALOGI("%s: ABR has already started", __func__);
853 return ret;
854 }
855
856 // Enable Slimbus 7 Tx feedback path
857 ALOGV("%s: Enable ABR Tx feedback path", __func__);
858 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
859 MIXER_ABR_TX_FEEDBACK_PATH);
860 if (!ctl_abr_tx_path) {
861 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
862 return -ENOSYS;
863 }
864 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
865 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
866 return -ENOSYS;
867 }
868
869 // Notify ABR usecase information to BT driver to distinguish
870 // between SCO and feedback usecase
871 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
872 MIXER_SET_FEEDBACK_CHANNEL);
873 if (!ctl_set_bt_feedback_channel) {
874 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800875 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800876 }
877 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
878 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800879 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800880 }
881
882 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800883 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800884 PCM_CAPTURE);
885 if (!a2dp.abr_config.abr_tx_handle) {
886 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
887 abr_device_id, PCM_IN,
888 &pcm_config_abr);
Zhou Song12c29502019-03-16 10:37:18 +0800889 if (a2dp.abr_config.abr_tx_handle == NULL) {
890 ALOGE("%s: Can't open abr tx device", __func__);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800891 goto fail;
Zhou Song12c29502019-03-16 10:37:18 +0800892 }
893 if (!(pcm_is_ready(a2dp.abr_config.abr_tx_handle) &&
894 !pcm_start(a2dp.abr_config.abr_tx_handle))) {
895 ALOGE("%s: tx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
896 goto fail;
897 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800898 }
Zhou Song12c29502019-03-16 10:37:18 +0800899
900 // Enable Slimbus 7 Rx feedback path for HD Voice use case
901 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH) {
902 ALOGV("%s: Enable ABR Rx feedback path", __func__);
903 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
904 MIXER_ABR_RX_FEEDBACK_PATH);
905 if (!ctl_abr_rx_path) {
906 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
907 goto fail;
908 }
909 if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 1) != 0) {
910 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
911 goto fail;
912 }
913
914 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
915 ALOGE("%s: Failed to set BT usecase", __func__);
916 goto fail;
917 }
918
919 // Open hostless front end and prepare ABR Rx path
920 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
921 PCM_PLAYBACK);
922 if (!a2dp.abr_config.abr_rx_handle) {
923 a2dp.abr_config.abr_rx_handle = pcm_open(a2dp.adev->snd_card,
924 abr_device_id, PCM_OUT,
925 &pcm_config_abr);
926 if (a2dp.abr_config.abr_rx_handle == NULL) {
927 ALOGE("%s: Can't open abr rx device", __func__);
928 goto fail;
929 }
930 if (!(pcm_is_ready(a2dp.abr_config.abr_rx_handle) &&
931 !pcm_start(a2dp.abr_config.abr_rx_handle))) {
932 ALOGE("%s: rx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_rx_handle));
933 goto fail;
934 }
935 }
936 }
937
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800938 a2dp.abr_config.abr_started = true;
939
940 return ret;
941
942fail:
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800943 stop_abr();
944 return -ENOSYS;
945}
946
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800947static int check_if_enhanced_fwk() {
948
949 int is_enhanced_fwk = 1;
950 //dlopen lib
951 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
952 if (vndk_fwk_lib_handle != NULL) {
953 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
954 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
955 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
956 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
957 __func__);
958 is_enhanced_fwk = 1;
959 } else {
960 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
961 }
962 }
963 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
964 return is_enhanced_fwk;
965}
966
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530967static void open_a2dp_source() {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530968 int ret = 0;
969
Florian Pfister1a84f312018-07-19 14:38:18 +0200970 ALOGD(" Open A2DP source start ");
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530971
972 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
973 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
974 ALOGD("calling BT stream open");
975 ret = a2dp.audio_source_open();
976 if(ret != 0) {
977 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Srinu Jellada99a592019-01-25 16:50:52 +0530978 }
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530979 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530980 } else {
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530981 ALOGD("Called a2dp open with improper state %d", a2dp.bt_state_source);
982 }
983 } else {
984 ALOGE("a2dp handle is not identified, Ignoring open request");
985 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
986 }
987}
988/* API to open BT IPC library to start IPC communication for BT Source*/
989static void a2dp_source_init()
990{
991 ALOGD("a2dp_source_init START");
992 if (a2dp.bt_lib_source_handle == NULL) {
993 ALOGD("Requesting for BT lib handle");
994 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
995 if (a2dp.bt_lib_source_handle == NULL) {
996 ALOGE("%s: dlopen failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
997 return;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530998 }
999 }
1000
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301001 a2dp.bt_audio_pre_init = (bt_audio_pre_init_t)
1002 dlsym(a2dp.bt_lib_source_handle, "bt_audio_pre_init");
Srinu Jellada99a592019-01-25 16:50:52 +05301003 a2dp.audio_source_open = (audio_source_open_t)
1004 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
1005 a2dp.audio_source_start = (audio_source_start_t)
1006 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
1007 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
1008 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
1009 a2dp.audio_source_suspend = (audio_source_suspend_t)
1010 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
1011 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
1012 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
1013 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1014 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
1015 a2dp.audio_source_stop = (audio_source_stop_t)
1016 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
1017 a2dp.audio_source_close = (audio_source_close_t)
1018 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
1019 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
1020 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
1021 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
1022 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
1023 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1024 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1025 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1026 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1027
Srinu Jella793f5302019-07-19 15:53:55 +05301028 if (a2dp.bt_lib_source_handle && a2dp.bt_audio_pre_init) {
1029 ALOGD("calling BT module preinit");
1030 // fwk related check's will be done in the BT layer
1031 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001032 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001033}
1034
1035/* API to open BT IPC library to start IPC communication for BT Sink*/
1036static void open_a2dp_sink()
1037{
1038 ALOGD(" Open A2DP input start ");
1039 if (a2dp.bt_lib_sink_handle == NULL){
1040 ALOGD(" Requesting for BT lib handle");
1041 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1042
1043 if (a2dp.bt_lib_sink_handle == NULL) {
1044 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1045 } else {
1046 a2dp.audio_sink_start = (audio_sink_start_t)
1047 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1048 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1049 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1050 a2dp.audio_sink_stop = (audio_sink_stop_t)
1051 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1052 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1053 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1054 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1055 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1056 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301057 }
1058}
1059
1060static int close_a2dp_output()
1061{
1062 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001063
1064 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1065 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301066 return -ENOSYS;
1067 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001068
1069 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1070 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001071 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001072 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301073 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001074 a2dp.a2dp_source_started = false;
1075 a2dp.a2dp_source_total_active_session_requests = 0;
1076 a2dp.a2dp_source_suspended = false;
1077 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301078 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301079 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001080 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001081 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1082 stop_abr();
1083 a2dp.abr_config.is_abr_enabled = false;
1084 a2dp.abr_config.abr_started = false;
1085 a2dp.abr_config.imc_instance = 0;
1086 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001087 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301088 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001089
1090 return 0;
1091}
1092
1093static int close_a2dp_input()
1094{
1095 ALOGV("%s\n",__func__);
1096
1097 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1098 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1099 return -ENOSYS;
1100 }
1101
1102 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1103 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001104 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001105 ALOGE("failed close a2dp sink control path from BT library");
1106 }
1107 a2dp.a2dp_sink_started = false;
1108 a2dp.a2dp_sink_total_active_session_requests = 0;
1109 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1110 a2dp.dec_sampling_rate = 48000;
1111 a2dp.dec_channels = 2;
1112 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301113
1114 return 0;
1115}
1116
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301117static void a2dp_check_and_set_scrambler()
1118{
1119 bool scrambler_mode = false;
1120 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001121 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1122 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301123
1124 if (scrambler_mode) {
1125 //enable scrambler in dsp
1126 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1127 MIXER_SCRAMBLER_MODE);
1128 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001129 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301130 return;
1131 } else {
1132 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1133 ALOGE("%s: Could not set scrambler mode", __func__);
1134 return;
1135 }
1136 }
1137 }
1138}
1139
Florian Pfister1a84f312018-07-19 14:38:18 +02001140static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301141{
Florian Pfister1a84f312018-07-19 14:38:18 +02001142 char *rate_str = NULL, *channels = NULL;
1143 uint32_t sampling_rate;
1144 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1145 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301146
Florian Pfister1a84f312018-07-19 14:38:18 +02001147 if (direction == SINK) {
1148 sampling_rate = a2dp.dec_sampling_rate;
1149 } else {
1150 sampling_rate = a2dp.enc_sampling_rate;
1151 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001152 /*
1153 * For LDAC encoder and AAC decoder open slimbus port at
1154 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1155 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1156 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001157 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301158 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001159 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1160 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301161 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1162 sampling_rate = sampling_rate *2;
1163 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001164
Aalique Grahame22e49102018-12-18 14:23:57 -08001165 // No need to configure backend for PCM format.
1166 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1167 return 0;
1168 }
1169
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301170 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301171 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301172 case 44100:
1173 rate_str = "KHZ_44P1";
1174 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301175 case 88200:
1176 rate_str = "KHZ_88P2";
1177 break;
1178 case 96000:
1179 rate_str = "KHZ_96";
1180 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001181 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301182 default:
1183 rate_str = "KHZ_48";
1184 break;
1185 }
1186
Florian Pfister1a84f312018-07-19 14:38:18 +02001187 if (direction == SINK) {
1188 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1189 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301190 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001191 } else {
1192 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1193 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301194 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001195 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301196 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001197
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301198 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1199 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301200 is_configured = false;
1201 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301202 }
1203
Surendar Karka2febd452018-12-13 17:56:43 +05301204 if (direction == SOURCE) {
1205 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001206 if (a2dp.abr_config.is_abr_enabled) {
1207 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH)
1208 rate_str = SPEECH_TX_SAMPLE_RATE;
1209 else
1210 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301211
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301212 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1213 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1214 MIXER_SOURCE_SAMPLE_RATE_TX);
1215 if (!ctl_sample_rate) {
1216 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1217 is_configured = false;
1218 goto fail;
1219 }
1220
1221 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1222 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1223 is_configured = false;
1224 goto fail;
1225 }
Surendar Karka2febd452018-12-13 17:56:43 +05301226 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301227 }
1228 } else {
1229 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1230 MIXER_SAMPLE_RATE_TX is not supported */
1231 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1232 MIXER_SAMPLE_RATE_DEFAULT);
1233 if (!ctl_sample_rate) {
1234 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301235 is_configured = false;
1236 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301237 }
1238
1239 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1240 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001241 is_configured = false;
1242 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301243 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301244 }
1245
Florian Pfister1a84f312018-07-19 14:38:18 +02001246 if (direction == SINK) {
1247 switch (a2dp.dec_channels) {
1248 case 1:
1249 channels = "One";
1250 break;
1251 case 2:
1252 default:
1253 channels = "Two";
1254 break;
1255 }
1256
Ramu Gottipati02809682018-12-19 16:46:12 +05301257 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001258 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1259 MIXER_AFE_SINK_CHANNELS);
1260 } else {
1261 //Configure AFE enc channels
1262 switch (a2dp.enc_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 enc 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_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301275 }
1276
Florian Pfister1a84f312018-07-19 14:38:18 +02001277 if (!ctrl_channels) {
1278 ALOGE(" ERROR AFE channels mixer control not identified");
1279 } else {
1280 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301281 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001282 is_configured = false;
1283 goto fail;
1284 }
1285 }
1286 is_configured = true;
1287fail:
1288 return is_configured;
1289}
1290
Surendar Karkabbb3c822018-11-12 13:00:38 +05301291bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001292{
1293 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1294 struct aac_dec_cfg_t aac_dsp_cfg;
1295 bool is_configured = false;
1296 int ret = 0;
1297
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001298 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001299 return false;
1300
Surendar Karka2febd452018-12-13 17:56:43 +05301301 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001302 if (!ctl_dec_data) {
1303 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1304 is_configured = false;
1305 goto fail;
1306 }
1307
1308 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1309 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1310 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1311 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1312 switch(aac_bt_cfg->audio_object_type) {
1313 case 0:
1314 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301315 break;
1316 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001317 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1318 break;
1319 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301320 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001321 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301322 break;
1323 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001324 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1325 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1326 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1327 sizeof(struct aac_dec_cfg_t));
1328 if (ret != 0) {
1329 ALOGE("%s: failed to set AAC decoder config", __func__);
1330 is_configured = false;
1331 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001332 }
1333
Florian Pfister1a84f312018-07-19 14:38:18 +02001334 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1335 MIXER_DEC_BIT_FORMAT);
1336 if (!ctrl_bit_format) {
1337 ALOGE(" ERROR Dec bit format mixer control not identified");
1338 is_configured = false;
1339 goto fail;
1340 }
1341 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1342 if (ret != 0) {
1343 ALOGE("%s: Failed to set bit format to decoder", __func__);
1344 is_configured = false;
1345 goto fail;
1346 }
1347
1348 is_configured = true;
1349 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1350 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1351 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1352 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1353 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1354fail:
1355 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301356}
1357
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301358static int a2dp_set_bit_format(uint32_t enc_bit_format)
1359{
1360 const char *bit_format = NULL;
1361 struct mixer_ctl *ctrl_bit_format = NULL;
1362
1363 // Configure AFE Input Bit Format
1364 switch (enc_bit_format) {
1365 case 32:
1366 bit_format = "S32_LE";
1367 break;
1368 case 24:
1369 bit_format = "S24_LE";
1370 break;
1371 case 16:
1372 default:
1373 bit_format = "S16_LE";
1374 break;
1375 }
1376
1377 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1378 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1379 MIXER_ENC_BIT_FORMAT);
1380 if (!ctrl_bit_format) {
1381 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1382 return -ENOSYS;
1383 }
1384 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1385 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1386 return -ENOSYS;
1387 }
1388 return 0;
1389}
1390
Surendar Karka2febd452018-12-13 17:56:43 +05301391static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301392{
Surendar Karka2febd452018-12-13 17:56:43 +05301393 const char *rate_str = "KHZ_8", *channels = "Zero";
1394 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1395 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301396
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001397 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301398 if (direction == SINK) {
1399 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1400 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1401 MIXER_SINK_SAMPLE_RATE);
1402 } else {
1403 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1404 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1405 MIXER_SAMPLE_RATE_RX);
1406 }
1407 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301408
Surendar Karka2febd452018-12-13 17:56:43 +05301409 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1410 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301411 return -ENOSYS;
1412 }
Zhou Song12c29502019-03-16 10:37:18 +08001413 if (a2dp.abr_config.is_abr_enabled) {
1414 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1415 MIXER_SOURCE_SAMPLE_RATE_TX);
1416 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301417 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1418 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001419 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301420
Zhou Song12c29502019-03-16 10:37:18 +08001421 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1422 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1423 return -ENOSYS;
1424 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301425 }
1426 } else {
1427
Surendar Karka2febd452018-12-13 17:56:43 +05301428 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301429 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301430 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301431 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1432 return -ENOSYS;
1433 }
1434
Surendar Karka2febd452018-12-13 17:56:43 +05301435 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301436 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1437 return -ENOSYS;
1438 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001439 }
1440
1441 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301442 if (direction == SINK) {
1443 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1444 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1445 MIXER_AFE_SINK_CHANNELS);
1446 } else {
1447 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1448 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1449 MIXER_AFE_IN_CHANNELS);
1450 }
1451 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001452 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1453 return -ENOSYS;
1454 }
Surendar Karka2febd452018-12-13 17:56:43 +05301455 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001456 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1457 return -ENOSYS;
1458 }
1459
1460 return 0;
1461}
1462
1463/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301464static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001465{
1466 struct mixer_ctl *ctl_dec_data = NULL;
1467 struct abr_dec_cfg_t dec_cfg;
1468 int ret = 0;
1469
1470 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301471 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001472 if (!ctl_dec_data) {
1473 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1474 return false;
1475 }
1476 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1477 dec_cfg.dec_format = dec_format;
1478 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1479 dec_cfg.imc_info.enable = IMC_ENABLE;
1480 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1481 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1482
1483 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1484 sizeof(dec_cfg));
1485 if (ret != 0) {
1486 ALOGE("%s: Failed to set decoder config", __func__);
1487 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301488 }
Surendar Karka2febd452018-12-13 17:56:43 +05301489 }
1490
1491 return true;
1492}
1493
Surendar Karkabbb3c822018-11-12 13:00:38 +05301494bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001495{
1496 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1497 struct sbc_dec_cfg_t sbc_dsp_cfg;
1498 bool is_configured = false;
1499 int ret = 0;
1500
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001501 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001502 goto fail;
1503
Surendar Karka2febd452018-12-13 17:56:43 +05301504 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001505 if (!ctl_dec_data) {
1506 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1507 is_configured = false;
1508 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301509 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001510
Florian Pfister1a84f312018-07-19 14:38:18 +02001511 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1512 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1513 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1514 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1515 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1516 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301517
Florian Pfister1a84f312018-07-19 14:38:18 +02001518 if (ret != 0) {
1519 ALOGE("%s: failed to set SBC decoder config", __func__);
1520 is_configured = false;
1521 goto fail;
1522 }
1523
1524 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1525 MIXER_DEC_BIT_FORMAT);
1526 if (!ctrl_bit_format) {
1527 ALOGE(" ERROR Dec bit format mixer control not identified");
1528 is_configured = false;
1529 goto fail;
1530 }
1531 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1532 if (ret != 0) {
1533 ALOGE("%s: Failed to set bit format to decoder", __func__);
1534 is_configured = false;
1535 goto fail;
1536 }
1537
1538 is_configured = true;
1539 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1540 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1541 a2dp.dec_channels = 1;
1542 else
1543 a2dp.dec_channels = 2;
1544 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1545 ALOGV("Successfully updated SBC dec format");
1546fail:
1547 return is_configured;
1548}
1549
Florian Pfister1a84f312018-07-19 14:38:18 +02001550/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301551static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301552{
Florian Pfister1a84f312018-07-19 14:38:18 +02001553 void *codec_info = NULL;
1554 codec_t codec_type = CODEC_TYPE_INVALID;
1555 bool is_configured = false;
1556 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301557
Florian Pfister1a84f312018-07-19 14:38:18 +02001558 if (!a2dp.audio_get_dec_config) {
1559 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1560 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301561 }
1562
Surendar Karka2febd452018-12-13 17:56:43 +05301563 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001564 if (!ctl_dec_data) {
1565 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1566 is_configured = false;
1567 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301568 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001569 codec_info = a2dp.audio_get_dec_config(&codec_type);
1570 switch(codec_type) {
1571 case CODEC_TYPE_SBC:
1572 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301573 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001574 break;
1575 case CODEC_TYPE_AAC:
1576 ALOGD(" AAC decoder supported BT device");
1577 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301578 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001579 break;
1580 default:
1581 ALOGD(" Received Unsupported decoder format");
1582 is_configured = false;
1583 break;
1584 }
1585 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301586}
1587
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301588/* API to configure SBC DSP encoder */
1589bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1590{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301591 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301592 struct sbc_enc_cfg_t sbc_dsp_cfg;
1593 bool is_configured = false;
1594 int ret = 0;
1595
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001596 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301597 return false;
1598
1599 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1600 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001601 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301602 is_configured = false;
1603 goto fail;
1604 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301605 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001606 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301607 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1608 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1609 switch(sbc_bt_cfg->channels) {
1610 case 0:
1611 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1612 break;
1613 case 1:
1614 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1615 break;
1616 case 3:
1617 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1618 break;
1619 case 2:
1620 default:
1621 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1622 break;
1623 }
1624 if (sbc_bt_cfg->alloc)
1625 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1626 else
1627 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1628 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1629 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1630 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1631 sizeof(struct sbc_enc_cfg_t));
1632 if (ret != 0) {
1633 ALOGE("%s: failed to set SBC encoder config", __func__);
1634 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301635 goto fail;
1636 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301637 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301638 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301639 is_configured = false;
1640 goto fail;
1641 }
1642 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001643 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301644 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301645
1646 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1647 a2dp.enc_channels = 1;
1648 else
1649 a2dp.enc_channels = 2;
1650
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301651 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1652 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301653fail:
1654 return is_configured;
1655}
1656
Manish Dewangan6a252632017-12-04 17:27:44 +05301657#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301658static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1659 audio_aptx_encoder_config *aptx_bt_cfg)
1660{
1661 int ret = 0;
1662
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001663 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301664 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1665 aptx_dsp_cfg, aptx_bt_cfg);
1666 return -EINVAL;
1667 }
1668
1669 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301670 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301671
1672
1673 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1674 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1675 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1676 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1677 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1678 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1679 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1680 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1681 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301682 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301683 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1684 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1685 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1686 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1687
1688
1689 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1690 case APTX_AD_CHANNEL_UNCHANGED:
1691 case APTX_AD_CHANNEL_JOINT_STEREO:
1692 case APTX_AD_CHANNEL_DUAL_MONO:
1693 case APTX_AD_CHANNEL_STEREO_TWS:
1694 case APTX_AD_CHANNEL_EARBUD:
1695 default:
1696 a2dp.enc_channels = CH_STEREO;
1697 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1698 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1699 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1700 break;
1701 case APTX_AD_CHANNEL_MONO:
1702 a2dp.enc_channels = CH_MONO;
1703 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1704 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001705 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301706 }
1707 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1708 case APTX_AD_SR_UNCHANGED:
1709 case APTX_AD_48:
1710 default:
1711 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1712 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1713 break;
1714 case APTX_AD_44_1:
1715 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1716 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001717 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301718 }
1719 ALOGV("Successfully updated APTX AD enc format with \
1720 samplingrate: %d channels:%d",
1721 aptx_dsp_cfg->custom_cfg.sample_rate,
1722 aptx_dsp_cfg->custom_cfg.num_channels);
1723
1724 return ret;
1725}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301726
Zhou Song8fccbb62019-03-20 01:08:19 +08001727static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
1728 audio_aptx_encoder_config *aptx_bt_cfg)
1729{
1730 int ret = 0;
1731
1732 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Mingshu Pangaa429f72019-06-10 17:25:00 +08001733 ALOGE("Invalid param, aptx_dsp_cfg %pK aptx_bt_cfg %pK",
Zhou Song8fccbb62019-03-20 01:08:19 +08001734 aptx_dsp_cfg, aptx_bt_cfg);
1735 return -EINVAL;
1736 }
1737
1738 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
1739 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
1740
1741
1742 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1743 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1744 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1745 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1746 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1747 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1748 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1749 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1750 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
1751 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
1752 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
1753 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
1754 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
1755 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
1756 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1757 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1758 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1759 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1760
1761
1762 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1763 case APTX_AD_CHANNEL_UNCHANGED:
1764 case APTX_AD_CHANNEL_JOINT_STEREO:
1765 case APTX_AD_CHANNEL_DUAL_MONO:
1766 case APTX_AD_CHANNEL_STEREO_TWS:
1767 case APTX_AD_CHANNEL_EARBUD:
1768 default:
1769 a2dp.enc_channels = CH_STEREO;
1770 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1771 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1772 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1773 break;
1774 case APTX_AD_CHANNEL_MONO:
1775 a2dp.enc_channels = CH_MONO;
1776 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1777 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001778 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001779 }
1780 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1781 case APTX_AD_SR_UNCHANGED:
1782 case APTX_AD_48:
1783 default:
1784 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1785 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1786 break;
1787 case APTX_AD_44_1:
1788 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1789 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001790 break;
1791 case APTX_AD_96:
1792 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
1793 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_96K;
1794 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001795 }
1796 ALOGV("Successfully updated APTX AD enc format with \
1797 samplingrate: %d channels:%d",
1798 aptx_dsp_cfg->custom_cfg.sample_rate,
1799 aptx_dsp_cfg->custom_cfg.num_channels);
1800
1801 return ret;
1802}
1803
Manisha Agarwala51768b2018-11-01 16:30:52 +05301804static void audio_a2dp_update_tws_channel_mode()
1805{
1806 char* channel_mode;
1807 struct mixer_ctl *ctl_channel_mode;
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301808
1809 ALOGD("Update tws for mono_mode on=%d",a2dp.is_tws_mono_mode_on);
1810
Manisha Agarwala51768b2018-11-01 16:30:52 +05301811 if (a2dp.is_tws_mono_mode_on)
1812 channel_mode = "One";
1813 else
1814 channel_mode = "Two";
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301815
Manisha Agarwala51768b2018-11-01 16:30:52 +05301816 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1817 if (!ctl_channel_mode) {
1818 ALOGE("failed to get tws mixer ctl");
1819 return;
1820 }
1821 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1822 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1823 return;
1824 }
1825}
1826
Manish Dewangan6a252632017-12-04 17:27:44 +05301827static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
1828 audio_aptx_encoder_config *aptx_bt_cfg)
1829{
1830 int ret = 0;
1831
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001832 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301833 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1834 aptx_dsp_cfg, aptx_bt_cfg);
1835 return -EINVAL;
1836 }
1837
1838 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001839 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301840
1841 if (!a2dp.is_aptx_dual_mono_supported) {
1842 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1843 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1844 } else {
1845 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1846 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1847 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1848 }
1849
1850 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
1851 case 1:
1852 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1853 break;
1854 case 2:
1855 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05301856 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1857 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
Manish Dewangan6a252632017-12-04 17:27:44 +05301858 break;
1859 }
1860 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
1861 if (!a2dp.is_aptx_dual_mono_supported) {
1862 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1863 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
1864 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
1865 aptx_dsp_cfg->custom_cfg.num_channels);
1866 } else {
1867 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1868 ALOGV("Successfully updated APTX dual mono enc format with \
1869 samplingrate: %d channels:%d syncmode %d",
1870 aptx_dsp_cfg->custom_cfg.sample_rate,
1871 aptx_dsp_cfg->custom_cfg.num_channels,
1872 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
1873 }
1874 return ret;
1875}
1876#else
1877static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
1878 audio_aptx_encoder_config *aptx_bt_cfg)
1879{
1880 int ret = 0;
1881
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001882 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301883 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1884 aptx_dsp_cfg, aptx_bt_cfg);
1885 return -EINVAL;
1886 }
1887
1888 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001889 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301890 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
1891 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
1892 switch(aptx_dsp_cfg->num_channels) {
1893 case 1:
1894 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
1895 break;
1896 case 2:
1897 default:
1898 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
1899 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
1900 break;
1901 }
1902
1903 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
1904 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
1905
1906 return ret;
1907}
1908#endif
1909
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301910/* API to configure APTX DSP encoder */
1911bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1912{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301913 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08001914 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001915 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301916 bool is_configured = false;
1917 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08001918 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301919
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001920 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301921 return false;
1922
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001923 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1924 if (!ctl_enc_data) {
1925 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1926 return false;
1927 }
1928
Manish Dewangan6a252632017-12-04 17:27:44 +05301929#ifndef LINUX_ENABLED
1930 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301931 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08001932 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001933 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001934 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
1935 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001936 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08001937 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001938 else
Zhou Song8fccbb62019-03-20 01:08:19 +08001939 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001940 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001941 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001942
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001943 if (ret) {
1944 is_configured = false;
1945 goto fail;
1946 }
1947
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001948 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001949 if (aptx_ad_ctl)
1950 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001951 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08001952 else
1953 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001954 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001955 } else {
1956 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001957 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05301958 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301959#else
1960 struct custom_enc_cfg_t aptx_dsp_cfg;
1961 mixer_size = sizeof(struct custom_enc_cfg_t);
1962 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05301963 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05301964 if (ret) {
1965 is_configured = false;
1966 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301967 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001968 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1969 mixer_size);
1970#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301971 if (ret != 0) {
1972 ALOGE("%s: Failed to set APTX encoder config", __func__);
1973 is_configured = false;
1974 goto fail;
1975 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301976#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001977 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301978 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001979 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301980 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
1981 else
1982 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301983#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301984 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301985 is_configured = false;
1986 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301987 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301988 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301989 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05301990 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301991 else
Surendar Karka2febd452018-12-13 17:56:43 +05301992 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301993fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05301994 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001995 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05301996 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301997 return is_configured;
1998}
1999
2000/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302001 */
Manish Dewangan6a252632017-12-04 17:27:44 +05302002#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002003bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302004#else
2005bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2006#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302007{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302008 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302009 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302010 bool is_configured = false;
2011 int ret = 0;
2012
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002013 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302014 return false;
2015
2016 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2017 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002018 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302019 is_configured = false;
2020 goto fail;
2021 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302022
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302023 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002024 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302025 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2026 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2027 switch(aptx_dsp_cfg.num_channels) {
2028 case 1:
2029 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2030 break;
2031 case 2:
2032 default:
2033 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2034 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2035 break;
2036 }
2037 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302038 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302039 if (ret != 0) {
2040 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2041 is_configured = false;
2042 goto fail;
2043 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302044 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302045 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302046 is_configured = false;
2047 goto fail;
2048 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302049 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002050 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302051 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302052 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302053 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2054 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302055fail:
2056 return is_configured;
2057}
2058
2059/* API to configure AAC DSP encoder */
2060bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2061{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302062 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302063 struct aac_enc_cfg_t aac_dsp_cfg;
2064 bool is_configured = false;
2065 int ret = 0;
2066
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002067 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302068 return false;
2069
2070 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2071 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002072 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302073 is_configured = false;
2074 goto fail;
2075 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302076 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002077 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302078 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302079 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002080 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302081 case 0:
2082 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2083 break;
2084 case 2:
2085 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2086 break;
2087 case 1:
2088 default:
2089 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2090 break;
2091 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302092 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302093 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002094
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302095 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2096 sizeof(struct aac_enc_cfg_t));
2097 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002098 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302099 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302100 goto fail;
2101 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302102 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302103 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302104 is_configured = false;
2105 goto fail;
2106 }
2107 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002108 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302109 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002110 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002111 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2112 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2113fail:
2114 return is_configured;
2115}
2116
2117bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2118{
2119 struct mixer_ctl *ctl_enc_data = NULL;
2120 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2121 bool is_configured = false;
2122 int ret = 0;
2123
2124 if (aac_bt_cfg == NULL)
2125 return false;
2126
2127 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2128 if (!ctl_enc_data) {
2129 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2130 is_configured = false;
2131 goto fail;
2132 }
2133 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302134 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002135 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2136 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2137 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2138 case 0:
2139 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2140 break;
2141 case 2:
2142 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2143 break;
2144 case 1:
2145 default:
2146 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2147 break;
2148 }
2149 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2150 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2151 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2152 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2153
2154 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2155 sizeof(struct aac_enc_cfg_v2_t));
2156 if (ret != 0) {
2157 ALOGE("%s: Failed to set AAC encoder config", __func__);
2158 is_configured = false;
2159 goto fail;
2160 }
2161 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2162 if (ret != 0) {
2163 is_configured = false;
2164 goto fail;
2165 }
2166 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302167 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002168 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2169 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2170 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2171 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302172fail:
2173 return is_configured;
2174}
2175
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302176bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2177{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302178 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302179 struct celt_enc_cfg_t celt_dsp_cfg;
2180 bool is_configured = false;
2181 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002182 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302183 return false;
2184
2185 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2186 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002187 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302188 is_configured = false;
2189 goto fail;
2190 }
2191 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2192
Florian Pfister1a84f312018-07-19 14:38:18 +02002193 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302194 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2195 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2196 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2197 case 1:
2198 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2199 break;
2200 case 2:
2201 default:
2202 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2203 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2204 break;
2205 }
2206
2207 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2208
2209 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2210 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2211 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2212 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2213 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2214
2215 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2216 sizeof(struct celt_enc_cfg_t));
2217 if (ret != 0) {
2218 ALOGE("%s: Failed to set CELT encoder config", __func__);
2219 is_configured = false;
2220 goto fail;
2221 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302222 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302223 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302224 is_configured = false;
2225 goto fail;
2226 }
2227 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002228 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302229 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302230 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302231 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2232 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2233fail:
2234 return is_configured;
2235}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302236
2237bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2238{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302239 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302240 struct ldac_enc_cfg_t ldac_dsp_cfg;
2241 bool is_configured = false;
2242 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002243 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302244 return false;
2245
2246 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2247 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002248 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302249 is_configured = false;
2250 goto fail;
2251 }
2252 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2253
Florian Pfister1a84f312018-07-19 14:38:18 +02002254 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302255 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2256 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2257 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2258 case 4:
2259 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2260 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2261 break;
2262 case 2:
2263 case 1:
2264 default:
2265 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2266 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2267 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2268 break;
2269 }
2270
2271 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2272 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2273 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002274 if (ldac_bt_cfg->is_abr_enabled) {
2275 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2276 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2277 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2278 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2279 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002280 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002281 }
2282
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302283 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2284 sizeof(struct ldac_enc_cfg_t));
2285 if (ret != 0) {
2286 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2287 is_configured = false;
2288 goto fail;
2289 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302290 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302291 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302292 is_configured = false;
2293 goto fail;
2294 }
2295 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002296 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302297 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2298 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002299 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302300 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2301 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2302fail:
2303 return is_configured;
2304}
2305
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302306bool configure_a2dp_encoder_format()
2307{
2308 void *codec_info = NULL;
2309 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002310 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302311 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002312 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302313
Florian Pfister1a84f312018-07-19 14:38:18 +02002314 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302315 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2316 return false;
2317 }
2318 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002319 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302320 &codec_type);
2321
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002322 // ABR disabled by default for all codecs
2323 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302324 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002325
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302326 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002327 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302328 ALOGD(" Received SBC encoder supported BT device");
2329 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002330 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302331 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002332 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302333 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302334#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002335 a2dp.is_aptx_dual_mono_supported = false;
2336 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302337#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302338 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002339 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302340 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002341 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302342 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302343#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302344 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002345 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302346#else
2347 is_configured =
2348 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2349#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002350 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302351#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002352 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002353 ALOGD(" Received APTX dual mono encoder supported BT device");
2354 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302355 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2356 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002357 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2358 is_configured =
2359 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302360 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302361#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002362 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302363 ALOGD(" Received AAC encoder supported BT device");
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002364 bool is_aac_frame_ctl_enabled =
2365 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
2366 is_configured = is_aac_frame_ctl_enabled ?
2367 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2368 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302369 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002370 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302371 ALOGD(" Received CELT encoder supported BT device");
2372 is_configured =
2373 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2374 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002375 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302376 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002377 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2378 instance_id = MAX_INSTANCE_ID;
2379 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302380 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002381 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302382 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302383 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302384#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302385 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302386 ALOGD(" Received APTX AD encoder supported BT device");
2387 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2388 instance_id = MAX_INSTANCE_ID;
2389 a2dp.abr_config.imc_instance = instance_id--;
2390 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2391 a2dp.is_aptx_adaptive = true;
2392 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2393 is_configured =
2394 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302395 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302396 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302397#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002398 case CODEC_TYPE_PCM:
2399 ALOGD("Received PCM format for BT device");
2400 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2401 is_configured = true;
2402 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302403 default:
2404 ALOGD(" Received Unsupported encoder formar");
2405 is_configured = false;
2406 break;
2407 }
2408 return is_configured;
2409}
2410
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002411int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302412{
2413 int ret = 0;
2414
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002415 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302416
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002417 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002418 && a2dp.audio_get_enc_config)) {
2419 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302420 return -ENOSYS;
2421 }
2422
Zhou Song12c29502019-03-16 10:37:18 +08002423 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302424 //session will be restarted after suspend completion
2425 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302426 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302427 }
2428
Florian Pfister1a84f312018-07-19 14:38:18 +02002429 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302430 ALOGD("calling BT module stream start");
2431 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002432 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302433 ALOGE("BT controller start return = %d",ret);
2434 if (ret != 0 ) {
2435 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002436 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302437 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002438 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002439 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302440 ret = 0;
2441 ALOGD("Start playback successful to BT library");
2442 } else {
2443 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002444 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302445 ret = -ETIMEDOUT;
2446 }
2447 }
2448 }
2449
Florian Pfister1a84f312018-07-19 14:38:18 +02002450 if (a2dp.a2dp_source_started) {
2451 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302452 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302453 audio_a2dp_update_tws_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002454 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002455 if (a2dp.abr_config.is_abr_enabled)
2456 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302457 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302458
2459 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002460 a2dp.a2dp_source_total_active_session_requests);
2461 return ret;
2462}
2463
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002464uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002465{
2466 uint32_t latency = 0;
2467
2468 switch(a2dp.bt_decoder_format) {
2469 case CODEC_TYPE_SBC:
2470 latency = DEFAULT_SINK_LATENCY_SBC;
2471 break;
2472 case CODEC_TYPE_AAC:
2473 latency = DEFAULT_SINK_LATENCY_AAC;
2474 break;
2475 default:
2476 latency = 200;
2477 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2478 break;
2479 }
2480 return (uint64_t)latency;
2481}
2482
2483bool a2dp_send_sink_setup_complete(void) {
2484 uint64_t system_latency = 0;
2485 bool is_complete = false;
2486
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002487 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002488
2489 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2490 is_complete = true;
2491 }
2492 return is_complete;
2493}
2494
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002495bool a2dp_sink_is_ready()
2496{
2497 bool ret = false;
2498
2499 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2500 (a2dp.is_a2dp_offload_supported) &&
2501 (a2dp.audio_sink_check_a2dp_ready))
2502 ret = a2dp.audio_sink_check_a2dp_ready();
2503 return ret;
2504}
2505
2506int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002507{
2508 int ret = 0;
2509
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002510 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002511
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002512 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002513 && a2dp.audio_get_dec_config)) {
2514 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2515 return -ENOSYS;
2516 }
2517
2518 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2519 ALOGD("calling BT module stream start");
2520 /* This call indicates BT IPC lib to start capture */
2521 ret = a2dp.audio_sink_start();
2522 ALOGE("BT controller start capture return = %d",ret);
2523 if (ret != 0 ) {
2524 ALOGE("BT controller start capture failed");
2525 a2dp.a2dp_sink_started = false;
2526 } else {
2527
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002528 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002529 ALOGD("Wait for capture ready not successful");
2530 ret = -ETIMEDOUT;
2531 }
2532
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002533 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002534 a2dp.a2dp_sink_started = true;
2535 ret = 0;
2536 ALOGD("Start capture successful to BT library");
2537 } else {
2538 ALOGD(" unable to configure DSP decoder");
2539 a2dp.a2dp_sink_started = false;
2540 ret = -ETIMEDOUT;
2541 }
2542
2543 if (!a2dp_send_sink_setup_complete()) {
2544 ALOGD("sink_setup_complete not successful");
2545 ret = -ETIMEDOUT;
2546 }
2547 }
2548 }
2549
2550 if (a2dp.a2dp_sink_started) {
2551 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002552 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002553 }
2554 }
2555
2556 ALOGD("start A2DP sink total active sessions :%d",
2557 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302558 return ret;
2559}
2560
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302561static void reset_a2dp_enc_config_params()
2562{
2563 int ret =0;
2564
Aalique Grahame22e49102018-12-18 14:23:57 -08002565 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302566 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302567 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302568
2569 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
2570 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
2571 MIXER_ENC_CONFIG_BLOCK);
2572 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002573 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302574 } else {
2575 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
2576 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002577 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302578 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002579
2580 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2581
Manisha Agarwala51768b2018-11-01 16:30:52 +05302582 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2583
2584 if (!ctl_channel_mode) {
2585 ALOGE("failed to get tws mixer ctl");
2586 } else {
2587 channel_mode = "Two";
2588 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2589 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2590 }
2591 a2dp.is_tws_mono_mode_on = false;
2592 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302593}
2594
Surendar Karka2febd452018-12-13 17:56:43 +05302595static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002596{
2597 struct mixer_ctl *ctl_dec_data = NULL;
2598 struct abr_dec_cfg_t dummy_reset_cfg;
2599 int ret = 0;
2600
Preetam Singh Ranawat25780e42019-07-31 18:15:57 +05302601 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
2602 if (!ctl_dec_data) {
2603 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
2604 return -EINVAL;
2605 }
2606 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
2607 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
2608 sizeof(dummy_reset_cfg));
2609 if (ret != 0) {
2610 ALOGE("%s: Failed to set dummy decoder config", __func__);
2611 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002612 }
2613
2614 return ret;
2615}
2616
Surendar Karka2febd452018-12-13 17:56:43 +05302617static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02002618{
2619 int ret =0;
2620
2621 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
2622 struct aac_dec_cfg_t dummy_reset_config;
2623
2624 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
2625 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05302626 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02002627 if (!ctl_dec_config) {
2628 ALOGE(" ERROR a2dp decoder format mixer control not identified");
2629 } else {
2630 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
2631 sizeof(struct aac_dec_cfg_t));
2632 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
2633 }
2634 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
2635 MIXER_DEC_BIT_FORMAT);
2636 if (!ctrl_bit_format) {
2637 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
2638 } else {
2639 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
2640 if (ret != 0) {
2641 ALOGE("%s: Failed to set bit format to decoder", __func__);
2642 }
2643 }
2644}
2645
Zhou Song12c29502019-03-16 10:37:18 +08002646static void reset_codec_config()
2647{
2648 reset_a2dp_enc_config_params();
2649 reset_a2dp_source_dec_config_params();
2650 a2dp_reset_backend_cfg(SOURCE);
2651 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
2652 stop_abr();
2653 a2dp.abr_config.is_abr_enabled = false;
2654}
2655
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002656int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302657{
2658 int ret =0;
2659
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002660 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002661 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002662 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302663 return -ENOSYS;
2664 }
2665
Florian Pfister1a84f312018-07-19 14:38:18 +02002666 if (a2dp.a2dp_source_total_active_session_requests > 0)
2667 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08002668 else
2669 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302670
Florian Pfister1a84f312018-07-19 14:38:18 +02002671 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302672 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02002673 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302674 if (ret < 0)
2675 ALOGE("stop stream to BT IPC lib failed");
2676 else
2677 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08002678 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
2679 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05302680 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302681 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002682 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002683 a2dp.a2dp_source_started = false;
2684 ALOGD("Stop A2DP playback, total active sessions :%d",
2685 a2dp.a2dp_source_total_active_session_requests);
2686 return 0;
2687}
2688
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002689int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002690{
2691 int ret =0;
2692
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002693 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002694 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002695 ALOGE("a2dp handle is not identified, Ignoring stop request");
2696 return -ENOSYS;
2697 }
2698
2699 if (a2dp.a2dp_sink_total_active_session_requests > 0)
2700 a2dp.a2dp_sink_total_active_session_requests--;
2701
2702 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2703 ALOGV("calling BT module stream stop");
2704 ret = a2dp.audio_sink_stop();
2705 if (ret < 0)
2706 ALOGE("stop stream to BT IPC lib failed");
2707 else
2708 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05302709 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002710 a2dp_reset_backend_cfg(SINK);
2711 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002712 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002713 a2dp.a2dp_source_started = false;
2714 ALOGD("Stop A2DP capture, total active sessions :%d",
2715 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302716 return 0;
2717}
2718
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002719int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302720{
Aalique Grahame22e49102018-12-18 14:23:57 -08002721 int ret = 0, val, status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302722 char value[32]={0};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302723 struct audio_usecase *uc_info;
2724 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302725
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002726 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002727 ALOGV("no supported encoders identified,ignoring a2dp setparam");
2728 status = -EINVAL;
2729 goto param_handled;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302730 }
2731
2732 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
2733 sizeof(value));
Zhou Song681350a2017-10-19 16:28:42 +08002734 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302735 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002736 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002737 ALOGV("Received device connect request for A2DP source");
2738 open_a2dp_source();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302739 }
2740 goto param_handled;
2741 }
2742
2743 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
2744 sizeof(value));
2745
Zhou Song681350a2017-10-19 16:28:42 +08002746 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302747 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002748 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002749 ALOGV("Received source device dis- connect request");
Samyak Jain4828f4c2018-08-24 16:31:06 +05302750 close_a2dp_output();
kunleiz4a1fad62018-02-08 18:00:16 +08002751 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302752 reset_a2dp_source_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002753 a2dp_reset_backend_cfg(SOURCE);
2754 } else if (audio_is_a2dp_in_device(val)) {
2755 ALOGV("Received sink device dis- connect request");
2756 close_a2dp_input();
Surendar Karka2febd452018-12-13 17:56:43 +05302757 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002758 a2dp_reset_backend_cfg(SINK);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302759 }
2760 goto param_handled;
2761 }
Surendar Karka2febd452018-12-13 17:56:43 +05302762#ifndef LINUX_ENABLED
Manisha Agarwala51768b2018-11-01 16:30:52 +05302763 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
2764 if (ret>=0) {
2765 ALOGD("Setting tws channel mode to %s",value);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002766 if (!(strncmp(value,"mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302767 a2dp.is_tws_mono_mode_on = true;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002768 else if (!(strncmp(value,"dual-mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302769 a2dp.is_tws_mono_mode_on = false;
2770 audio_a2dp_update_tws_channel_mode();
2771 goto param_handled;
2772 }
Surendar Karka2febd452018-12-13 17:56:43 +05302773#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302774 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
2775 if (ret >= 0) {
Surendar Karka2febd452018-12-13 17:56:43 +05302776 if (a2dp.bt_lib_source_handle) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302777 if ((!strncmp(value,"true",sizeof(value)))) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002778 if (a2dp.a2dp_source_suspended) {
2779 ALOGD("%s: A2DP is already suspended", __func__);
2780 goto param_handled;
2781 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302782 ALOGD("Setting a2dp to suspend state");
Florian Pfister1a84f312018-07-19 14:38:18 +02002783 a2dp.a2dp_source_suspended = true;
Surendar Karka2febd452018-12-13 17:56:43 +05302784 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
yidongh7203cca2018-09-19 16:12:25 +08002785 goto param_handled;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302786 list_for_each(node, &a2dp.adev->usecase_list) {
2787 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302788 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002789 is_a2dp_out_device_type(&uc_info->stream.out->device_list)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302790 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002791 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302792 pthread_mutex_lock(&a2dp.adev->lock);
2793 }
2794 }
Zhou Song12c29502019-03-16 10:37:18 +08002795 if (!a2dp.swb_configured)
2796 reset_codec_config();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002797 if (a2dp.audio_source_suspend)
Florian Pfister1a84f312018-07-19 14:38:18 +02002798 a2dp.audio_source_suspend();
2799 } else if (a2dp.a2dp_source_suspended == true) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302800 ALOGD("Resetting a2dp suspend state");
Zhou Song10617ed2017-05-26 13:28:48 +08002801 struct audio_usecase *uc_info;
2802 struct listnode *node;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002803 if (a2dp.clear_source_a2dpsuspend_flag)
Florian Pfister1a84f312018-07-19 14:38:18 +02002804 a2dp.clear_source_a2dpsuspend_flag();
2805 a2dp.a2dp_source_suspended = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302806 /*
2807 * It is possible that before suspend,a2dp sessions can be active
2808 * for example during music + voice activation concurrency
2809 * a2dp suspend will be called & BT will change to sco mode
2810 * though music is paused as a part of voice activation
2811 * compress session close happens only after pause timeout(10secs)
2812 * so if resume request comes before pause timeout as a2dp session
2813 * is already active IPC start will not be called from APM/audio_hw
2814 * Fix is to call a2dp start for IPC library post suspend
2815 * based on number of active session count
2816 */
Florian Pfister1a84f312018-07-19 14:38:18 +02002817 if (a2dp.a2dp_source_total_active_session_requests > 0) {
Naresh Tanniru649871a2016-11-04 18:08:32 +05302818 ALOGD(" Calling IPC lib start post suspend state");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002819 if (a2dp.audio_source_start) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002820 ret = a2dp.audio_source_start();
Naresh Tanniru649871a2016-11-04 18:08:32 +05302821 if (ret != 0) {
2822 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002823 a2dp.a2dp_source_started = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302824 }
2825 }
2826 }
Zhou Song10617ed2017-05-26 13:28:48 +08002827 list_for_each(node, &a2dp.adev->usecase_list) {
2828 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302829 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002830 is_a2dp_out_device_type(&uc_info->stream.out->device_list)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302831 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002832 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302833 pthread_mutex_lock(&a2dp.adev->lock);
2834 }
Zhou Song10617ed2017-05-26 13:28:48 +08002835 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302836 }
2837 }
2838 goto param_handled;
2839 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002840
2841 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
2842 sizeof(value));
2843 if (ret >= 0) {
2844 if (a2dp.is_a2dp_offload_supported &&
2845 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2846 *reconfig = true;
2847 }
2848 goto param_handled;
2849 }
2850
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302851param_handled:
2852 ALOGV("end of a2dp setparam");
Aalique Grahame22e49102018-12-18 14:23:57 -08002853 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302854}
2855
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002856void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302857{
2858 a2dp.is_handoff_in_progress = is_on;
2859}
2860
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002861bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302862{
2863 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05302864 // Or if a2dp device is selected but earlier start failed ( as a2dp
2865 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02002866 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302867}
2868
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002869void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302870{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302871 *sample_rate = a2dp.enc_sampling_rate;
2872}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302873
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002874void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02002875{
2876 *sample_rate = a2dp.dec_sampling_rate;
2877}
2878
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002879bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302880{
2881 bool ret = false;
2882
Florian Pfister1a84f312018-07-19 14:38:18 +02002883 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302884 return ret;
2885
Florian Pfister1a84f312018-07-19 14:38:18 +02002886 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08002887 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002888 (a2dp.audio_source_check_a2dp_ready))
2889 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302890 return ret;
2891}
2892
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002893bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02002894{
2895 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302896}
2897
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002898void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08002899 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302900{
2901 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02002902 a2dp.bt_lib_source_handle = NULL;
2903 a2dp.a2dp_source_started = false;
2904 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
2905 a2dp.a2dp_source_total_active_session_requests = 0;
2906 a2dp.a2dp_source_suspended = false;
2907 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302908 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302909 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002910 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302911 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002912 a2dp.abr_config.is_abr_enabled = false;
2913 a2dp.abr_config.abr_started = false;
2914 a2dp.abr_config.imc_instance = 0;
2915 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08002916 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302917 a2dp.is_tws_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05302918 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08002919 a2dp.swb_configured = false;
2920
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002921 // init function pointers
2922 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08002923 init_config.fp_platform_get_pcm_device_id;
2924 fp_check_a2dp_restore = init_config.fp_check_a2dp_restore;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002925
kunleiz5a127262017-09-08 14:47:48 +08002926 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302927 reset_a2dp_source_dec_config_params();
2928 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002929
2930 a2dp.bt_lib_sink_handle = NULL;
2931 a2dp.a2dp_sink_started = false;
2932 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
2933 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002934
2935 if (is_running_with_enhanced_fwk == UNINITIALIZED)
2936 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
2937 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08002938 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02002939
2940 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302941 update_offload_codec_capabilities();
2942}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002943
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002944uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002945{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002946 uint32_t latency = 0;
2947 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302948 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
2949 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002950 char value[PROPERTY_VALUE_MAX];
2951
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002952 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08002953 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002954 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302955 if (sscanf(value, "%d/%d/%d/%d/%d%d",
2956 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002957 ALOGI("Failed to parse avsync offset params from '%s'.", value);
2958 avsync_runtime_prop = 0;
2959 }
2960 }
2961
yidongh0515e042017-07-06 15:00:34 +08002962 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02002963 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2964 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08002965 }
2966
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07002967 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002968 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08002969 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
2970 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002971 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002972 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08002973 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
2974 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002975 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002976 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08002977 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
2978 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002979 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002980 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08002981 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
2982 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002983 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002984 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302985 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
2986 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
2987 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002988 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302989 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
2990 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
2991 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05302992 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05302993 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05302994 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08002995 case CODEC_TYPE_PCM:
2996 latency = ENCODER_LATENCY_PCM;
2997 latency += DEFAULT_SINK_LATENCY_PCM;
2998 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002999 default:
3000 latency = 200;
3001 break;
3002 }
3003 return latency;
3004}
Aalique Grahame22e49102018-12-18 14:23:57 -08003005
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003006int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003007 struct str_parms *reply)
3008{
3009 int ret, val = 0;
3010 char value[32]={0};
3011
3012 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3013 value, sizeof(value));
3014 if (ret >= 0) {
3015 val = a2dp.is_a2dp_offload_supported;
3016 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3017 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3018 }
3019
3020 return 0;
3021}
Zhou Song12c29502019-03-16 10:37:18 +08003022
3023
3024bool configure_aptx_ad_speech_enc_fmt() {
3025 struct mixer_ctl *ctl_enc_data = NULL;
3026 int mixer_size = 0;
3027 int ret = 0;
3028 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3029
3030 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3031 if (!ctl_enc_data) {
3032 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3033 return false;
3034 }
3035
3036 /* Initialize dsp configuration params */
3037 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3038 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3039 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3040 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3041 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3042 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3043 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3044 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3045 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3046 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3047 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3048
3049 /* Configure AFE DSP configuration */
3050 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3051 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3052 mixer_size);
3053 if (ret != 0) {
3054 ALOGE("%s: Failed to set SWB encoder config", __func__);
3055 return false;
3056 }
3057
3058 /* Configure AFE Input Bit Format as PCM_16 */
3059 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3060 if (ret != 0) {
3061 ALOGE("%s: Failed to set SWB bit format", __func__);
3062 return false;
3063 }
3064
3065 return true;
3066}
3067
3068bool configure_aptx_ad_speech_dec_fmt()
3069{
3070 struct mixer_ctl *ctl_dec_data = NULL;
3071 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3072 int ret = 0;
3073
3074 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3075 if (!ctl_dec_data) {
3076 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3077 return false;
3078 }
3079 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3080 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3081 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3082 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3083 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3084 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3085 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3086 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3087
3088 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3089 sizeof(dec_cfg));
3090 if (ret != 0) {
3091 ALOGE("%s: Failed to set decoder config", __func__);
3092 return false;
3093 }
3094 return true;
3095}
3096
3097int sco_start_configuration()
3098{
3099 ALOGD("sco_start_configuration start");
3100
3101 if (!a2dp.swb_configured) {
3102 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3103 /* Configure AFE codec*/
3104 if (configure_aptx_ad_speech_enc_fmt() &&
3105 configure_aptx_ad_speech_dec_fmt()) {
3106 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3107 } else {
3108 ALOGE("%s: failed to send SCO configuration", __func__);
3109 return -ETIMEDOUT;
3110 }
3111 /* Configure backend*/
3112 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3113 a2dp.enc_channels = CH_MONO;
3114 a2dp.abr_config.is_abr_enabled = true;
3115 a2dp_set_backend_cfg(SOURCE);
3116 /* Start abr*/
3117 start_abr();
3118 a2dp.swb_configured = true;
3119 }
3120 return 0;
3121}
3122
3123void sco_reset_configuration()
3124{
Zhou Songd6d71752019-05-21 18:08:51 +08003125 if (a2dp.swb_configured) {
3126 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003127
Zhou Songd6d71752019-05-21 18:08:51 +08003128 reset_codec_config();
3129 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3130 a2dp.swb_configured = false;
3131 }
Zhou Song12c29502019-03-16 10:37:18 +08003132}