blob: 34869d425eeb9cab16092845033560d94be7b1ee [file] [log] [blame]
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301/*
Aalique Grahame22e49102018-12-18 14:23:57 -08002* Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of The Linux Foundation nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
Aalique Grahame22e49102018-12-18 14:23:57 -080029#define LOG_TAG "a2dp_offload"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053030/*#define LOG_NDEBUG 0*/
31#define LOG_NDDEBUG 0
32#include <errno.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080033#include <log/log.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053034#include <dlfcn.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080035#include <pthread.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
Manish Dewangan6a252632017-12-04 17:27:44 +053039#include "audio_extn.h"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053040#include <stdlib.h>
41#include <cutils/str_parms.h>
42#include <hardware/audio.h>
43#include <hardware/hardware.h>
44#include <cutils/properties.h>
45
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046#ifdef DYNAMIC_LOG_ENABLED
47#include <log_xml_parser.h>
48#define LOG_MASK HAL_MOD_FILE_A2DP
49#include <log_utils.h>
50#endif
51
Naresh 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
205} enc_aptx_ad_s_rate;
206
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530207typedef void (*bt_audio_pre_init_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200208typedef int (*audio_source_open_t)(void);
209typedef int (*audio_source_close_t)(void);
210typedef int (*audio_source_start_t)(void);
211typedef int (*audio_source_stop_t)(void);
212typedef int (*audio_source_suspend_t)(void);
213typedef void (*audio_source_handoff_triggered_t)(void);
214typedef void (*clear_source_a2dpsuspend_flag_t)(void);
215typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
216 uint8_t *num_dev, codec_t *codec_type);
217typedef int (*audio_source_check_a2dp_ready_t)(void);
218typedef int (*audio_is_source_scrambling_enabled_t)(void);
Manisha Agarwala51768b2018-11-01 16:30:52 +0530219typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200220typedef int (*audio_sink_start_t)(void);
221typedef int (*audio_sink_stop_t)(void);
222typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
223typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
224typedef int (*audio_sink_check_a2dp_ready_t)(void);
225typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530226
227enum A2DP_STATE {
228 A2DP_STATE_CONNECTED,
229 A2DP_STATE_STARTED,
230 A2DP_STATE_STOPPED,
231 A2DP_STATE_DISCONNECTED,
232};
233
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800234typedef enum {
235 IMC_TRANSMIT,
236 IMC_RECEIVE,
237} imc_direction_t;
238
239typedef enum {
240 IMC_DISABLE,
241 IMC_ENABLE,
242} imc_status_t;
243
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700244typedef enum {
Zhou Song12c29502019-03-16 10:37:18 +0800245 SWAP_DISABLE,
246 SWAP_ENABLE,
247} swap_status_t;
248
249typedef enum {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700250 MTU_SIZE,
251 PEAK_BIT_RATE,
252} frame_control_type_t;
253
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800254// --- external function dependency ---
255fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
256fp_check_a2dp_restore_t fp_check_a2dp_restore;
257
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800258/* PCM config for ABR Feedback hostless front end */
259static struct pcm_config pcm_config_abr = {
260 .channels = 1,
261 .rate = 8000,
262 .period_size = 240,
263 .period_count = 2,
264 .format = PCM_FORMAT_S16_LE,
265 .start_threshold = 0,
266 .stop_threshold = INT_MAX,
267 .avail_min = 0,
268};
269
270/* Adaptive bitrate config for A2DP codecs */
271struct a2dp_abr_config {
272 /* Flag to denote whether Adaptive bitrate is enabled for codec */
273 bool is_abr_enabled;
274 /* Flag to denote whether front end has been opened for ABR */
275 bool abr_started;
276 /* ABR Tx path pcm handle */
277 struct pcm *abr_tx_handle;
Zhou Song12c29502019-03-16 10:37:18 +0800278 /* ABR Rx path pcm handle */
279 struct pcm *abr_rx_handle;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800280 /* ABR Inter Module Communication (IMC) instance ID */
281 uint32_t imc_instance;
282};
283
284static uint32_t instance_id = MAX_INSTANCE_ID;
285
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530286/* structure used to update a2dp state machine
287 * to communicate IPC library
288 * to store DSP encoder configuration information
289 */
290struct a2dp_data {
291 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200292 void *bt_lib_source_handle;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530293 bt_audio_pre_init_t bt_audio_pre_init;
Florian Pfister1a84f312018-07-19 14:38:18 +0200294 audio_source_open_t audio_source_open;
295 audio_source_close_t audio_source_close;
296 audio_source_start_t audio_source_start;
297 audio_source_stop_t audio_source_stop;
298 audio_source_suspend_t audio_source_suspend;
299 audio_source_handoff_triggered_t audio_source_handoff_triggered;
300 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
301 audio_get_enc_config_t audio_get_enc_config;
302 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530303 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200304 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
305 enum A2DP_STATE bt_state_source;
306 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530307 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530308 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200309 bool a2dp_source_started;
310 bool a2dp_source_suspended;
311 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530312 bool is_a2dp_offload_supported;
313 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700314 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530315 /* Mono Mode support for TWS+ */
316 bool is_tws_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530317 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800318 /* Adaptive bitrate config for A2DP codecs */
319 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200320
321 void *bt_lib_sink_handle;
322 audio_sink_start_t audio_sink_start;
323 audio_sink_stop_t audio_sink_stop;
324 audio_get_dec_config_t audio_get_dec_config;
325 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
326 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
327 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
328 enum A2DP_STATE bt_state_sink;
329 codec_t bt_decoder_format;
330 uint32_t dec_sampling_rate;
331 uint32_t dec_channels;
332 bool a2dp_sink_started;
333 int a2dp_sink_total_active_session_requests;
Zhou Song12c29502019-03-16 10:37:18 +0800334 bool swb_configured;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530335};
336
337struct a2dp_data a2dp;
338
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800339/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
340 * Structures sent to configure DSP for ABR are defined below.
341 * This data helps DSP configure feedback path (BTSoC to LPASS)
342 * for link quality levels and mapping quality levels to codec
343 * specific bitrate.
344 */
345
346/* Key value pair for link quality level to bitrate mapping. */
347struct bit_rate_level_map_t {
348 uint32_t link_quality_level;
349 uint32_t bitrate;
350};
351
352/* Link quality level to bitrate mapping info sent to DSP. */
353struct quality_level_to_bitrate_info {
354 /* Number of quality levels being mapped.
355 * This will be equal to the size of mapping table.
356 */
357 uint32_t num_levels;
358 /* Quality level to bitrate mapping table */
359 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
360};
361
362/* Structure to set up Inter Module Communication (IMC) between
363 * AFE Decoder and Encoder.
364 */
365struct imc_dec_enc_info {
366 /* Decoder to encoder communication direction.
367 * Transmit = 0 / Receive = 1
368 */
369 uint32_t direction;
370 /* Enable / disable IMC between decoder and encoder */
371 uint32_t enable;
372 /* Purpose of IMC being set up between decoder and encoder.
373 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
374 * is the default value to be sent as purpose.
375 */
376 uint32_t purpose;
377 /* Unique communication instance ID.
378 * purpose and comm_instance together form the actual key
379 * used in IMC registration, which must be the same for
380 * encoder and decoder for which IMC is being set up.
381 */
382 uint32_t comm_instance;
383};
384
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700385/* Structure to control frame size of AAC encoded frames. */
386struct aac_frame_size_control_t {
387 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
388 uint32_t ctl_type;
389 /* Control value
390 * MTU_SIZE: MTU size in bytes
391 * PEAK_BIT_RATE: Peak bitrate in bits per second.
392 */
393 uint32_t ctl_value;
394};
395
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800396/* Structure used for ABR config of AFE encoder and decoder. */
397struct abr_enc_cfg_t {
398 /* Link quality level to bitrate mapping info sent to DSP. */
399 struct quality_level_to_bitrate_info mapping_info;
400 /* Information to set up IMC between decoder and encoder */
401 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700402 /* Flag to indicate whether ABR is enabled */
403 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800404} __attribute__ ((packed));
405
406/* Structure to send configuration for decoder introduced
407 * on AFE Tx path for ABR link quality feedback to BT encoder.
408 */
409struct abr_dec_cfg_t {
410 /* Decoder media format */
411 uint32_t dec_format;
412 /* Information to set up IMC between decoder and encoder */
413 struct imc_dec_enc_info imc_info;
414} __attribute__ ((packed));
415
Zhou Song12c29502019-03-16 10:37:18 +0800416struct aptx_ad_speech_mode_cfg_t
417{
418 uint32_t mode;
419 uint32_t swapping;
420} __attribute__ ((packed));
421
422/* Structure for SWB voice dec config */
423struct aptx_ad_speech_dec_cfg_t {
424 struct abr_dec_cfg_t abr_cfg;
425 struct aptx_ad_speech_mode_cfg_t speech_mode;
426} __attribute__ ((packed));
427
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530428/* START of DSP configurable structures
429 * These values should match with DSP interface defintion
430 */
431
432/* AAC encoder configuration structure. */
433typedef struct aac_enc_cfg_t aac_enc_cfg_t;
434
435/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
436 * supported aac_fmt_flag are ADTS/RAW
437 * supported channel_cfg are Native mode, Mono , Stereo
438 */
439struct aac_enc_cfg_t {
440 uint32_t enc_format;
441 uint32_t bit_rate;
442 uint32_t enc_mode;
443 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530444 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530445 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530446} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530447
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700448struct aac_enc_cfg_v2_t {
449 struct aac_enc_cfg_t aac_enc_cfg;
450 struct aac_frame_size_control_t frame_ctl;
451} __attribute__ ((packed));
452
Surendar Karkabbb3c822018-11-12 13:00:38 +0530453typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
454struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200455 uint16_t aac_fmt_flag; /* LATM*/
456 uint16_t audio_object_type; /* LC */
457 uint16_t channels; /* Stereo */
458 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
459 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
460 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530461} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200462
Surendar Karkabbb3c822018-11-12 13:00:38 +0530463typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
464struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200465 uint16_t channels; /* Mono, Stereo */
466 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
467 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530468} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200469
470/* AAC decoder configuration structure. */
471typedef struct aac_dec_cfg_t aac_dec_cfg_t;
472struct aac_dec_cfg_t {
473 uint32_t dec_format;
474 audio_aac_decoder_config_t data;
475} __attribute__ ((packed));
476
477/* SBC decoder configuration structure. */
478typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
479struct sbc_dec_cfg_t {
480 uint32_t dec_format;
481 audio_sbc_decoder_config_t data;
482} __attribute__ ((packed));
483
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530484/* SBC encoder configuration structure. */
485typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
486
487/* supported num_subbands are 4/8
488 * supported blk_len are 4, 8, 12, 16
489 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
490 * supported alloc_method are LOUNDNESS/SNR
491 * supported bit_rate for mono channel is max 320kbps
492 * supported bit rate for stereo channel is max 512 kbps
493 */
494struct sbc_enc_cfg_t{
495 uint32_t enc_format;
496 uint32_t num_subbands;
497 uint32_t blk_len;
498 uint32_t channel_mode;
499 uint32_t alloc_method;
500 uint32_t bit_rate;
501 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530502} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530503
504
505/* supported num_channels are Mono/Stereo
506 * supported channel_mapping for mono is CHANNEL_C
507 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
508 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700509 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530510struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530511{
512 uint32_t enc_format;
513 uint32_t sample_rate;
514 uint16_t num_channels;
515 uint16_t reserved;
516 uint8_t channel_mapping[8];
517 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530518} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530519
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530520struct celt_specific_enc_cfg_t
521{
522 uint32_t bit_rate;
523 uint16_t frame_size;
524 uint16_t complexity;
525 uint16_t prediction_mode;
526 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530527} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530528
529struct celt_enc_cfg_t
530{
531 struct custom_enc_cfg_t custom_cfg;
532 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530533} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700534
535/* sync_mode introduced with APTX V2 libraries
536 * sync mode: 0x0 = stereo sync mode
537 * 0x01 = dual mono sync mode
538 * 0x02 = dual mono with no sync on either L or R codewords
539 */
540struct aptx_v2_enc_cfg_ext_t
541{
542 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530543} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700544
545/* APTX struct for combining custom enc and V2 fields */
546struct aptx_enc_cfg_t
547{
548 struct custom_enc_cfg_t custom_cfg;
549 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530550} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700551
Sharad Sangle95d451b2018-06-19 12:24:20 +0530552/* APTX AD structure */
553struct aptx_ad_enc_cfg_ext_t
554{
555 uint32_t sampling_freq;
556 uint32_t mtu;
557 uint32_t channel_mode;
558 uint32_t min_sink_modeA;
559 uint32_t max_sink_modeA;
560 uint32_t min_sink_modeB;
561 uint32_t max_sink_modeB;
562 uint32_t min_sink_modeC;
563 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530564 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530565} __attribute__ ((packed));
566
567struct aptx_ad_enc_cfg_t
568{
569 struct custom_enc_cfg_t custom_cfg;
570 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
571 struct abr_enc_cfg_t abr_cfg;
572} __attribute__ ((packed));
573
Zhou Song8fccbb62019-03-20 01:08:19 +0800574struct aptx_ad_enc_cfg_ext_r2_t
575{
576 uint32_t sampling_freq;
577 uint32_t mtu;
578 uint32_t channel_mode;
579 uint32_t min_sink_modeA;
580 uint32_t max_sink_modeA;
581 uint32_t min_sink_modeB;
582 uint32_t max_sink_modeB;
583 uint32_t min_sink_modeC;
584 uint32_t max_sink_modeC;
585 uint32_t mode;
586 uint32_t input_mode;
587 uint32_t fade_duration;
588 uint8_t sink_cap[11];
589} __attribute__ ((packed));
590
591struct aptx_ad_enc_cfg_r2_t
592{
593 struct custom_enc_cfg_t custom_cfg;
594 struct aptx_ad_enc_cfg_ext_r2_t aptx_ad_cfg;
595 struct abr_enc_cfg_t abr_cfg;
596} __attribute__ ((packed));
597
Zhou Song12c29502019-03-16 10:37:18 +0800598/* APTX AD SPEECH structure */
599struct aptx_ad_speech_enc_cfg_t
600{
601 struct custom_enc_cfg_t custom_cfg;
602 /* Information to set up IMC between decoder and encoder */
603 struct imc_dec_enc_info imc_info;
604 struct aptx_ad_speech_mode_cfg_t speech_mode;
605} __attribute__ ((packed));
606
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530607struct ldac_specific_enc_cfg_t
608{
609 uint32_t bit_rate;
610 uint16_t channel_mode;
611 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530612} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530613
614struct ldac_enc_cfg_t
615{
616 struct custom_enc_cfg_t custom_cfg;
617 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800618 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530619} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530620
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530621/* In LE BT source code uses system/audio.h for below
622 * structure definition. To avoid multiple definition
623 * compilation error for audiohal in LE , masking structure
624 * definition under "LINUX_ENABLED" which is defined only
625 * in LE
626 */
627#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530628/* TODO: Define the following structures only for O using PLATFORM_VERSION */
629/* Information about BT SBC encoder configuration
630 * This data is used between audio HAL module and
631 * BT IPC library to configure DSP encoder
632 */
633typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530634 uint32_t subband; /* 4, 8 */
635 uint32_t blk_len; /* 4, 8, 12, 16 */
636 uint16_t sampling_rate; /*44.1khz,48khz*/
637 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
638 uint8_t alloc; /*0(Loudness),1(SNR)*/
639 uint8_t min_bitpool; /* 2 */
640 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
641 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530642 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530643} audio_sbc_encoder_config;
644
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530645/* Information about BT APTX encoder configuration
646 * This data is used between audio HAL module and
647 * BT IPC library to configure DSP encoder
648 */
649typedef struct {
650 uint16_t sampling_rate;
651 uint8_t channels;
652 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530653 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700654} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530655
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700656typedef struct {
Zhou Song8fccbb62019-03-20 01:08:19 +0800657 uint32_t sampling_rate;
658 uint32_t mtu;
659 int32_t channel_mode;
660 uint32_t min_sink_modeA;
661 uint32_t max_sink_modeA;
662 uint32_t min_sink_modeB;
663 uint32_t max_sink_modeB;
664 uint32_t min_sink_modeC;
665 uint32_t max_sink_modeC;
666 uint32_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530667 uint8_t TTP_modeA_low;
668 uint8_t TTP_modeA_high;
669 uint8_t TTP_modeB_low;
670 uint8_t TTP_modeB_high;
Zhou Song8fccbb62019-03-20 01:08:19 +0800671 uint8_t TTP_TWS_low;
672 uint8_t TTP_TWS_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530673 uint32_t bits_per_sample;
Zhou Song8fccbb62019-03-20 01:08:19 +0800674 uint32_t input_mode;
675 uint32_t fade_duration;
676 uint8_t sink_cap[11];
Sharad Sangle95d451b2018-06-19 12:24:20 +0530677} audio_aptx_ad_config;
678
679typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700680 uint16_t sampling_rate;
681 uint8_t channels;
682 uint32_t bitrate;
683 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530684 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700685} audio_aptx_dual_mono_config;
686
687typedef union {
688 audio_aptx_default_config *default_cfg;
689 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530690 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700691} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530692
693/* Information about BT AAC encoder configuration
694 * This data is used between audio HAL module and
695 * BT IPC library to configure DSP encoder
696 */
697typedef struct {
698 uint32_t enc_mode; /* LC, SBR, PS */
699 uint16_t format_flag; /* RAW, ADTS */
700 uint16_t channels; /* 1-Mono, 2-Stereo */
701 uint32_t sampling_rate;
702 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530703 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530704} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530705#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700706
707typedef struct {
708 audio_aac_encoder_config audio_aac_enc_cfg;
709 struct aac_frame_size_control_t frame_ctl;
710} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530711
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530712/* Information about BT CELT encoder configuration
713 * This data is used between audio HAL module and
714 * BT IPC library to configure DSP encoder
715 */
716typedef struct {
717 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
718 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
719 uint16_t frame_size; /* 64-128-256-512, 512 */
720 uint16_t complexity; /* 0-10, 1 */
721 uint16_t prediction_mode; /* 0-1-2, 0 */
722 uint16_t vbr_flag; /* 0-1, 0*/
723 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530724 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530725} audio_celt_encoder_config;
726
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530727/* Information about BT LDAC encoder configuration
728 * This data is used between audio HAL module and
729 * BT IPC library to configure DSP encoder
730 */
731typedef struct {
732 uint32_t sampling_rate; /*44100,48000,88200,96000*/
733 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
734 uint16_t channel_mode; /* 0, 4, 2, 1*/
735 uint16_t mtu; /*679*/
Aniket Kumar Latad0196282019-05-09 14:24:17 -0700736 uint32_t bits_per_sample;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800737 bool is_abr_enabled;
738 struct quality_level_to_bitrate_info level_to_bitrate_map;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530739} audio_ldac_encoder_config;
740
Surendar Karkabbb3c822018-11-12 13:00:38 +0530741/* Information about BT AAC decoder configuration
742 * This data is used between audio HAL module and
743 * BT IPC library to configure DSP decoder
744 */
745typedef struct {
746 uint16_t aac_fmt_flag; /* LATM*/
747 uint16_t audio_object_type; /* LC */
748 uint16_t channels; /* Stereo */
749 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
750 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
751 44.1k, 48k, 64k, 88.2k, 96k */
752} audio_aac_dec_config_t;
753
754/* Information about BT SBC decoder configuration
755 * This data is used between audio HAL module and
756 * BT IPC library to configure DSP decoder
757 */
758typedef struct {
759 uint16_t channels; /* Mono, Stereo */
760 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
761 44.1k, 48k, 64k, 88.2k, 96k */
762}audio_sbc_dec_config_t;
763
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530764/*********** END of DSP configurable structures ********************/
765
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530766static void update_offload_codec_capabilities()
767{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530768
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530769 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800770 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
771 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
772
773 ALOGD("%s: A2DP offload supported = %d",__func__,
774 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530775}
776
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800777static int stop_abr()
778{
779 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800780 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800781 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800782 int ret = 0;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800783
784 /* This function can be used if !abr_started for clean up */
785 ALOGV("%s: enter", __func__);
786
787 // Close hostless front end
788 if (a2dp.abr_config.abr_tx_handle != NULL) {
789 pcm_close(a2dp.abr_config.abr_tx_handle);
790 a2dp.abr_config.abr_tx_handle = NULL;
791 }
Zhou Song12c29502019-03-16 10:37:18 +0800792 if (a2dp.abr_config.abr_rx_handle != NULL) {
793 pcm_close(a2dp.abr_config.abr_rx_handle);
794 a2dp.abr_config.abr_rx_handle = NULL;
795 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800796 a2dp.abr_config.abr_started = false;
797 a2dp.abr_config.imc_instance = 0;
798
799 // Reset BT driver mixer control for ABR usecase
800 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
801 MIXER_SET_FEEDBACK_CHANNEL);
802 if (!ctl_set_bt_feedback_channel) {
803 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800804 ret = -ENOSYS;
805 } else if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800806 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800807 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800808 }
809
810 // Reset ABR Tx feedback path
811 ALOGV("%s: Disable ABR Tx feedback path", __func__);
812 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
813 MIXER_ABR_TX_FEEDBACK_PATH);
814 if (!ctl_abr_tx_path) {
815 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800816 ret = -ENOSYS;
817 } else if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800818 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800819 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800820 }
821
Zhou Song12c29502019-03-16 10:37:18 +0800822 // Reset ABR Rx feedback path
823 ALOGV("%s: Disable ABR Rx feedback path", __func__);
824 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
825 MIXER_ABR_RX_FEEDBACK_PATH);
826 if (!ctl_abr_rx_path) {
827 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
828 ret = -ENOSYS;
829 } else if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 0) != 0) {
830 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
831 ret = -ENOSYS;
832 }
833
834 return ret;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800835}
836
837static int start_abr()
838{
839 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800840 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800841 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
842 int abr_device_id;
843 int ret = 0;
844
845 if (!a2dp.abr_config.is_abr_enabled) {
846 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
847 return -ENOSYS;
848 }
849
850 if (a2dp.abr_config.abr_started) {
851 ALOGI("%s: ABR has already started", __func__);
852 return ret;
853 }
854
855 // Enable Slimbus 7 Tx feedback path
856 ALOGV("%s: Enable ABR Tx feedback path", __func__);
857 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
858 MIXER_ABR_TX_FEEDBACK_PATH);
859 if (!ctl_abr_tx_path) {
860 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
861 return -ENOSYS;
862 }
863 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
864 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
865 return -ENOSYS;
866 }
867
868 // Notify ABR usecase information to BT driver to distinguish
869 // between SCO and feedback usecase
870 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
871 MIXER_SET_FEEDBACK_CHANNEL);
872 if (!ctl_set_bt_feedback_channel) {
873 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800874 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800875 }
876 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
877 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800878 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800879 }
880
881 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800882 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800883 PCM_CAPTURE);
884 if (!a2dp.abr_config.abr_tx_handle) {
885 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
886 abr_device_id, PCM_IN,
887 &pcm_config_abr);
Zhou Song12c29502019-03-16 10:37:18 +0800888 if (a2dp.abr_config.abr_tx_handle == NULL) {
889 ALOGE("%s: Can't open abr tx device", __func__);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800890 goto fail;
Zhou Song12c29502019-03-16 10:37:18 +0800891 }
892 if (!(pcm_is_ready(a2dp.abr_config.abr_tx_handle) &&
893 !pcm_start(a2dp.abr_config.abr_tx_handle))) {
894 ALOGE("%s: tx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
895 goto fail;
896 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800897 }
Zhou Song12c29502019-03-16 10:37:18 +0800898
899 // Enable Slimbus 7 Rx feedback path for HD Voice use case
900 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH) {
901 ALOGV("%s: Enable ABR Rx feedback path", __func__);
902 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
903 MIXER_ABR_RX_FEEDBACK_PATH);
904 if (!ctl_abr_rx_path) {
905 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
906 goto fail;
907 }
908 if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 1) != 0) {
909 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
910 goto fail;
911 }
912
913 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
914 ALOGE("%s: Failed to set BT usecase", __func__);
915 goto fail;
916 }
917
918 // Open hostless front end and prepare ABR Rx path
919 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
920 PCM_PLAYBACK);
921 if (!a2dp.abr_config.abr_rx_handle) {
922 a2dp.abr_config.abr_rx_handle = pcm_open(a2dp.adev->snd_card,
923 abr_device_id, PCM_OUT,
924 &pcm_config_abr);
925 if (a2dp.abr_config.abr_rx_handle == NULL) {
926 ALOGE("%s: Can't open abr rx device", __func__);
927 goto fail;
928 }
929 if (!(pcm_is_ready(a2dp.abr_config.abr_rx_handle) &&
930 !pcm_start(a2dp.abr_config.abr_rx_handle))) {
931 ALOGE("%s: rx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_rx_handle));
932 goto fail;
933 }
934 }
935 }
936
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800937 a2dp.abr_config.abr_started = true;
938
939 return ret;
940
941fail:
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800942 stop_abr();
943 return -ENOSYS;
944}
945
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800946static int check_if_enhanced_fwk() {
947
948 int is_enhanced_fwk = 1;
949 //dlopen lib
950 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
951 if (vndk_fwk_lib_handle != NULL) {
952 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
953 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
954 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
955 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
956 __func__);
957 is_enhanced_fwk = 1;
958 } else {
959 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
960 }
961 }
962 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
963 return is_enhanced_fwk;
964}
965
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530966static void open_a2dp_source() {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530967 int ret = 0;
968
Florian Pfister1a84f312018-07-19 14:38:18 +0200969 ALOGD(" Open A2DP source start ");
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530970
971 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
972 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
973 ALOGD("calling BT stream open");
974 ret = a2dp.audio_source_open();
975 if(ret != 0) {
976 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Srinu Jellada99a592019-01-25 16:50:52 +0530977 }
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530978 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530979 } else {
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530980 ALOGD("Called a2dp open with improper state %d", a2dp.bt_state_source);
981 }
982 } else {
983 ALOGE("a2dp handle is not identified, Ignoring open request");
984 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
985 }
986}
987/* API to open BT IPC library to start IPC communication for BT Source*/
988static void a2dp_source_init()
989{
990 ALOGD("a2dp_source_init START");
991 if (a2dp.bt_lib_source_handle == NULL) {
992 ALOGD("Requesting for BT lib handle");
993 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
994 if (a2dp.bt_lib_source_handle == NULL) {
995 ALOGE("%s: dlopen failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
996 return;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530997 }
998 }
999
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301000 a2dp.bt_audio_pre_init = (bt_audio_pre_init_t)
1001 dlsym(a2dp.bt_lib_source_handle, "bt_audio_pre_init");
Srinu Jellada99a592019-01-25 16:50:52 +05301002 a2dp.audio_source_open = (audio_source_open_t)
1003 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
1004 a2dp.audio_source_start = (audio_source_start_t)
1005 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
1006 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
1007 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
1008 a2dp.audio_source_suspend = (audio_source_suspend_t)
1009 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
1010 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
1011 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
1012 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1013 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
1014 a2dp.audio_source_stop = (audio_source_stop_t)
1015 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
1016 a2dp.audio_source_close = (audio_source_close_t)
1017 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
1018 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
1019 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
1020 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
1021 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
1022 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1023 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1024 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1025 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1026
Srinu Jella793f5302019-07-19 15:53:55 +05301027 if (a2dp.bt_lib_source_handle && a2dp.bt_audio_pre_init) {
1028 ALOGD("calling BT module preinit");
1029 // fwk related check's will be done in the BT layer
1030 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001031 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001032}
1033
1034/* API to open BT IPC library to start IPC communication for BT Sink*/
1035static void open_a2dp_sink()
1036{
1037 ALOGD(" Open A2DP input start ");
1038 if (a2dp.bt_lib_sink_handle == NULL){
1039 ALOGD(" Requesting for BT lib handle");
1040 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1041
1042 if (a2dp.bt_lib_sink_handle == NULL) {
1043 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1044 } else {
1045 a2dp.audio_sink_start = (audio_sink_start_t)
1046 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1047 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1048 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1049 a2dp.audio_sink_stop = (audio_sink_stop_t)
1050 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1051 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1052 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1053 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1054 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1055 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301056 }
1057}
1058
1059static int close_a2dp_output()
1060{
1061 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001062
1063 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1064 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301065 return -ENOSYS;
1066 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001067
1068 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1069 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001070 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001071 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301072 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001073 a2dp.a2dp_source_started = false;
1074 a2dp.a2dp_source_total_active_session_requests = 0;
1075 a2dp.a2dp_source_suspended = false;
1076 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301077 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301078 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001079 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001080 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1081 stop_abr();
1082 a2dp.abr_config.is_abr_enabled = false;
1083 a2dp.abr_config.abr_started = false;
1084 a2dp.abr_config.imc_instance = 0;
1085 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001086 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301087 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001088
1089 return 0;
1090}
1091
1092static int close_a2dp_input()
1093{
1094 ALOGV("%s\n",__func__);
1095
1096 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1097 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1098 return -ENOSYS;
1099 }
1100
1101 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1102 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001103 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001104 ALOGE("failed close a2dp sink control path from BT library");
1105 }
1106 a2dp.a2dp_sink_started = false;
1107 a2dp.a2dp_sink_total_active_session_requests = 0;
1108 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1109 a2dp.dec_sampling_rate = 48000;
1110 a2dp.dec_channels = 2;
1111 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301112
1113 return 0;
1114}
1115
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301116static void a2dp_check_and_set_scrambler()
1117{
1118 bool scrambler_mode = false;
1119 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001120 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1121 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301122
1123 if (scrambler_mode) {
1124 //enable scrambler in dsp
1125 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1126 MIXER_SCRAMBLER_MODE);
1127 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001128 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301129 return;
1130 } else {
1131 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1132 ALOGE("%s: Could not set scrambler mode", __func__);
1133 return;
1134 }
1135 }
1136 }
1137}
1138
Florian Pfister1a84f312018-07-19 14:38:18 +02001139static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301140{
Florian Pfister1a84f312018-07-19 14:38:18 +02001141 char *rate_str = NULL, *channels = NULL;
1142 uint32_t sampling_rate;
1143 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1144 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301145
Florian Pfister1a84f312018-07-19 14:38:18 +02001146 if (direction == SINK) {
1147 sampling_rate = a2dp.dec_sampling_rate;
1148 } else {
1149 sampling_rate = a2dp.enc_sampling_rate;
1150 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001151 /*
1152 * For LDAC encoder and AAC decoder open slimbus port at
1153 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1154 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1155 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001156 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301157 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001158 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1159 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301160 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1161 sampling_rate = sampling_rate *2;
1162 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001163
Aalique Grahame22e49102018-12-18 14:23:57 -08001164 // No need to configure backend for PCM format.
1165 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1166 return 0;
1167 }
1168
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301169 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301170 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301171 case 44100:
1172 rate_str = "KHZ_44P1";
1173 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301174 case 88200:
1175 rate_str = "KHZ_88P2";
1176 break;
1177 case 96000:
1178 rate_str = "KHZ_96";
1179 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001180 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301181 default:
1182 rate_str = "KHZ_48";
1183 break;
1184 }
1185
Florian Pfister1a84f312018-07-19 14:38:18 +02001186 if (direction == SINK) {
1187 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1188 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301189 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001190 } else {
1191 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1192 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301193 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001194 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301195 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001196
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301197 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1198 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301199 is_configured = false;
1200 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301201 }
1202
Surendar Karka2febd452018-12-13 17:56:43 +05301203 if (direction == SOURCE) {
1204 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001205 if (a2dp.abr_config.is_abr_enabled) {
1206 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH)
1207 rate_str = SPEECH_TX_SAMPLE_RATE;
1208 else
1209 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301210
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301211 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1212 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1213 MIXER_SOURCE_SAMPLE_RATE_TX);
1214 if (!ctl_sample_rate) {
1215 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1216 is_configured = false;
1217 goto fail;
1218 }
1219
1220 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1221 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1222 is_configured = false;
1223 goto fail;
1224 }
Surendar Karka2febd452018-12-13 17:56:43 +05301225 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301226 }
1227 } else {
1228 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1229 MIXER_SAMPLE_RATE_TX is not supported */
1230 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1231 MIXER_SAMPLE_RATE_DEFAULT);
1232 if (!ctl_sample_rate) {
1233 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301234 is_configured = false;
1235 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301236 }
1237
1238 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1239 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001240 is_configured = false;
1241 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301242 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301243 }
1244
Florian Pfister1a84f312018-07-19 14:38:18 +02001245 if (direction == SINK) {
1246 switch (a2dp.dec_channels) {
1247 case 1:
1248 channels = "One";
1249 break;
1250 case 2:
1251 default:
1252 channels = "Two";
1253 break;
1254 }
1255
Ramu Gottipati02809682018-12-19 16:46:12 +05301256 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001257 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1258 MIXER_AFE_SINK_CHANNELS);
1259 } else {
1260 //Configure AFE enc channels
1261 switch (a2dp.enc_channels) {
1262 case 1:
1263 channels = "One";
1264 break;
1265 case 2:
1266 default:
1267 channels = "Two";
1268 break;
1269 }
1270
Ramu Gottipati02809682018-12-19 16:46:12 +05301271 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001272 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1273 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301274 }
1275
Florian Pfister1a84f312018-07-19 14:38:18 +02001276 if (!ctrl_channels) {
1277 ALOGE(" ERROR AFE channels mixer control not identified");
1278 } else {
1279 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301280 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001281 is_configured = false;
1282 goto fail;
1283 }
1284 }
1285 is_configured = true;
1286fail:
1287 return is_configured;
1288}
1289
Surendar Karkabbb3c822018-11-12 13:00:38 +05301290bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001291{
1292 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1293 struct aac_dec_cfg_t aac_dsp_cfg;
1294 bool is_configured = false;
1295 int ret = 0;
1296
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001297 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001298 return false;
1299
Surendar Karka2febd452018-12-13 17:56:43 +05301300 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001301 if (!ctl_dec_data) {
1302 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1303 is_configured = false;
1304 goto fail;
1305 }
1306
1307 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1308 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1309 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1310 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1311 switch(aac_bt_cfg->audio_object_type) {
1312 case 0:
1313 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301314 break;
1315 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001316 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1317 break;
1318 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301319 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001320 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301321 break;
1322 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001323 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1324 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1325 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1326 sizeof(struct aac_dec_cfg_t));
1327 if (ret != 0) {
1328 ALOGE("%s: failed to set AAC decoder config", __func__);
1329 is_configured = false;
1330 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001331 }
1332
Florian Pfister1a84f312018-07-19 14:38:18 +02001333 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1334 MIXER_DEC_BIT_FORMAT);
1335 if (!ctrl_bit_format) {
1336 ALOGE(" ERROR Dec bit format mixer control not identified");
1337 is_configured = false;
1338 goto fail;
1339 }
1340 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1341 if (ret != 0) {
1342 ALOGE("%s: Failed to set bit format to decoder", __func__);
1343 is_configured = false;
1344 goto fail;
1345 }
1346
1347 is_configured = true;
1348 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1349 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1350 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1351 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1352 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1353fail:
1354 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301355}
1356
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301357static int a2dp_set_bit_format(uint32_t enc_bit_format)
1358{
1359 const char *bit_format = NULL;
1360 struct mixer_ctl *ctrl_bit_format = NULL;
1361
1362 // Configure AFE Input Bit Format
1363 switch (enc_bit_format) {
1364 case 32:
1365 bit_format = "S32_LE";
1366 break;
1367 case 24:
1368 bit_format = "S24_LE";
1369 break;
1370 case 16:
1371 default:
1372 bit_format = "S16_LE";
1373 break;
1374 }
1375
1376 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1377 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1378 MIXER_ENC_BIT_FORMAT);
1379 if (!ctrl_bit_format) {
1380 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1381 return -ENOSYS;
1382 }
1383 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1384 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1385 return -ENOSYS;
1386 }
1387 return 0;
1388}
1389
Surendar Karka2febd452018-12-13 17:56:43 +05301390static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301391{
Surendar Karka2febd452018-12-13 17:56:43 +05301392 const char *rate_str = "KHZ_8", *channels = "Zero";
1393 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1394 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301395
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001396 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301397 if (direction == SINK) {
1398 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1399 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1400 MIXER_SINK_SAMPLE_RATE);
1401 } else {
1402 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1403 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1404 MIXER_SAMPLE_RATE_RX);
1405 }
1406 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301407
Surendar Karka2febd452018-12-13 17:56:43 +05301408 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1409 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301410 return -ENOSYS;
1411 }
Zhou Song12c29502019-03-16 10:37:18 +08001412 if (a2dp.abr_config.is_abr_enabled) {
1413 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1414 MIXER_SOURCE_SAMPLE_RATE_TX);
1415 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301416 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1417 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001418 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301419
Zhou Song12c29502019-03-16 10:37:18 +08001420 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1421 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1422 return -ENOSYS;
1423 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301424 }
1425 } else {
1426
Surendar Karka2febd452018-12-13 17:56:43 +05301427 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301428 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301429 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301430 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1431 return -ENOSYS;
1432 }
1433
Surendar Karka2febd452018-12-13 17:56:43 +05301434 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301435 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1436 return -ENOSYS;
1437 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001438 }
1439
1440 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301441 if (direction == SINK) {
1442 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1443 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1444 MIXER_AFE_SINK_CHANNELS);
1445 } else {
1446 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1447 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1448 MIXER_AFE_IN_CHANNELS);
1449 }
1450 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001451 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1452 return -ENOSYS;
1453 }
Surendar Karka2febd452018-12-13 17:56:43 +05301454 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001455 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1456 return -ENOSYS;
1457 }
1458
1459 return 0;
1460}
1461
1462/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301463static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001464{
1465 struct mixer_ctl *ctl_dec_data = NULL;
1466 struct abr_dec_cfg_t dec_cfg;
1467 int ret = 0;
1468
1469 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301470 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001471 if (!ctl_dec_data) {
1472 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1473 return false;
1474 }
1475 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1476 dec_cfg.dec_format = dec_format;
1477 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1478 dec_cfg.imc_info.enable = IMC_ENABLE;
1479 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1480 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1481
1482 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1483 sizeof(dec_cfg));
1484 if (ret != 0) {
1485 ALOGE("%s: Failed to set decoder config", __func__);
1486 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301487 }
Surendar Karka2febd452018-12-13 17:56:43 +05301488 }
1489
1490 return true;
1491}
1492
Surendar Karkabbb3c822018-11-12 13:00:38 +05301493bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001494{
1495 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1496 struct sbc_dec_cfg_t sbc_dsp_cfg;
1497 bool is_configured = false;
1498 int ret = 0;
1499
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001500 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001501 goto fail;
1502
Surendar Karka2febd452018-12-13 17:56:43 +05301503 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001504 if (!ctl_dec_data) {
1505 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1506 is_configured = false;
1507 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301508 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001509
Florian Pfister1a84f312018-07-19 14:38:18 +02001510 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1511 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1512 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1513 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1514 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1515 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301516
Florian Pfister1a84f312018-07-19 14:38:18 +02001517 if (ret != 0) {
1518 ALOGE("%s: failed to set SBC decoder config", __func__);
1519 is_configured = false;
1520 goto fail;
1521 }
1522
1523 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1524 MIXER_DEC_BIT_FORMAT);
1525 if (!ctrl_bit_format) {
1526 ALOGE(" ERROR Dec bit format mixer control not identified");
1527 is_configured = false;
1528 goto fail;
1529 }
1530 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1531 if (ret != 0) {
1532 ALOGE("%s: Failed to set bit format to decoder", __func__);
1533 is_configured = false;
1534 goto fail;
1535 }
1536
1537 is_configured = true;
1538 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1539 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1540 a2dp.dec_channels = 1;
1541 else
1542 a2dp.dec_channels = 2;
1543 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1544 ALOGV("Successfully updated SBC dec format");
1545fail:
1546 return is_configured;
1547}
1548
Florian Pfister1a84f312018-07-19 14:38:18 +02001549/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301550static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301551{
Florian Pfister1a84f312018-07-19 14:38:18 +02001552 void *codec_info = NULL;
1553 codec_t codec_type = CODEC_TYPE_INVALID;
1554 bool is_configured = false;
1555 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301556
Florian Pfister1a84f312018-07-19 14:38:18 +02001557 if (!a2dp.audio_get_dec_config) {
1558 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1559 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301560 }
1561
Surendar Karka2febd452018-12-13 17:56:43 +05301562 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001563 if (!ctl_dec_data) {
1564 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1565 is_configured = false;
1566 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301567 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001568 codec_info = a2dp.audio_get_dec_config(&codec_type);
1569 switch(codec_type) {
1570 case CODEC_TYPE_SBC:
1571 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301572 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001573 break;
1574 case CODEC_TYPE_AAC:
1575 ALOGD(" AAC decoder supported BT device");
1576 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301577 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001578 break;
1579 default:
1580 ALOGD(" Received Unsupported decoder format");
1581 is_configured = false;
1582 break;
1583 }
1584 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301585}
1586
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301587/* API to configure SBC DSP encoder */
1588bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1589{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301590 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301591 struct sbc_enc_cfg_t sbc_dsp_cfg;
1592 bool is_configured = false;
1593 int ret = 0;
1594
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001595 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301596 return false;
1597
1598 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1599 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001600 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301601 is_configured = false;
1602 goto fail;
1603 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301604 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001605 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301606 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1607 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1608 switch(sbc_bt_cfg->channels) {
1609 case 0:
1610 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1611 break;
1612 case 1:
1613 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1614 break;
1615 case 3:
1616 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1617 break;
1618 case 2:
1619 default:
1620 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1621 break;
1622 }
1623 if (sbc_bt_cfg->alloc)
1624 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1625 else
1626 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1627 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1628 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1629 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1630 sizeof(struct sbc_enc_cfg_t));
1631 if (ret != 0) {
1632 ALOGE("%s: failed to set SBC encoder config", __func__);
1633 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301634 goto fail;
1635 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301636 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301637 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301638 is_configured = false;
1639 goto fail;
1640 }
1641 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001642 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301643 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301644
1645 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1646 a2dp.enc_channels = 1;
1647 else
1648 a2dp.enc_channels = 2;
1649
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301650 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1651 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301652fail:
1653 return is_configured;
1654}
1655
Manish Dewangan6a252632017-12-04 17:27:44 +05301656#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301657static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1658 audio_aptx_encoder_config *aptx_bt_cfg)
1659{
1660 int ret = 0;
1661
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001662 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301663 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1664 aptx_dsp_cfg, aptx_bt_cfg);
1665 return -EINVAL;
1666 }
1667
1668 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301669 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301670
1671
1672 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1673 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1674 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1675 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1676 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1677 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1678 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1679 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1680 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301681 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301682 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1683 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1684 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1685 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1686
1687
1688 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1689 case APTX_AD_CHANNEL_UNCHANGED:
1690 case APTX_AD_CHANNEL_JOINT_STEREO:
1691 case APTX_AD_CHANNEL_DUAL_MONO:
1692 case APTX_AD_CHANNEL_STEREO_TWS:
1693 case APTX_AD_CHANNEL_EARBUD:
1694 default:
1695 a2dp.enc_channels = CH_STEREO;
1696 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1697 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1698 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1699 break;
1700 case APTX_AD_CHANNEL_MONO:
1701 a2dp.enc_channels = CH_MONO;
1702 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1703 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1704 break;
1705 }
1706 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1707 case APTX_AD_SR_UNCHANGED:
1708 case APTX_AD_48:
1709 default:
1710 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1711 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1712 break;
1713 case APTX_AD_44_1:
1714 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1715 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
1716 break;
1717 }
1718 ALOGV("Successfully updated APTX AD enc format with \
1719 samplingrate: %d channels:%d",
1720 aptx_dsp_cfg->custom_cfg.sample_rate,
1721 aptx_dsp_cfg->custom_cfg.num_channels);
1722
1723 return ret;
1724}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301725
Zhou Song8fccbb62019-03-20 01:08:19 +08001726static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
1727 audio_aptx_encoder_config *aptx_bt_cfg)
1728{
1729 int ret = 0;
1730
1731 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Mingshu Pangaa429f72019-06-10 17:25:00 +08001732 ALOGE("Invalid param, aptx_dsp_cfg %pK aptx_bt_cfg %pK",
Zhou Song8fccbb62019-03-20 01:08:19 +08001733 aptx_dsp_cfg, aptx_bt_cfg);
1734 return -EINVAL;
1735 }
1736
1737 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
1738 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
1739
1740
1741 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1742 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1743 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1744 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1745 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1746 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1747 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1748 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1749 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
1750 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
1751 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
1752 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
1753 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
1754 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
1755 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1756 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1757 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1758 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1759
1760
1761 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1762 case APTX_AD_CHANNEL_UNCHANGED:
1763 case APTX_AD_CHANNEL_JOINT_STEREO:
1764 case APTX_AD_CHANNEL_DUAL_MONO:
1765 case APTX_AD_CHANNEL_STEREO_TWS:
1766 case APTX_AD_CHANNEL_EARBUD:
1767 default:
1768 a2dp.enc_channels = CH_STEREO;
1769 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1770 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1771 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1772 break;
1773 case APTX_AD_CHANNEL_MONO:
1774 a2dp.enc_channels = CH_MONO;
1775 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1776 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1777 break;
1778 }
1779 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1780 case APTX_AD_SR_UNCHANGED:
1781 case APTX_AD_48:
1782 default:
1783 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1784 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1785 break;
1786 case APTX_AD_44_1:
1787 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1788 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
1789 break;
1790 }
1791 ALOGV("Successfully updated APTX AD enc format with \
1792 samplingrate: %d channels:%d",
1793 aptx_dsp_cfg->custom_cfg.sample_rate,
1794 aptx_dsp_cfg->custom_cfg.num_channels);
1795
1796 return ret;
1797}
1798
Manisha Agarwala51768b2018-11-01 16:30:52 +05301799static void audio_a2dp_update_tws_channel_mode()
1800{
1801 char* channel_mode;
1802 struct mixer_ctl *ctl_channel_mode;
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301803
1804 ALOGD("Update tws for mono_mode on=%d",a2dp.is_tws_mono_mode_on);
1805
Manisha Agarwala51768b2018-11-01 16:30:52 +05301806 if (a2dp.is_tws_mono_mode_on)
1807 channel_mode = "One";
1808 else
1809 channel_mode = "Two";
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301810
Manisha Agarwala51768b2018-11-01 16:30:52 +05301811 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1812 if (!ctl_channel_mode) {
1813 ALOGE("failed to get tws mixer ctl");
1814 return;
1815 }
1816 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1817 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1818 return;
1819 }
1820}
1821
Manish Dewangan6a252632017-12-04 17:27:44 +05301822static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
1823 audio_aptx_encoder_config *aptx_bt_cfg)
1824{
1825 int ret = 0;
1826
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001827 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301828 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1829 aptx_dsp_cfg, aptx_bt_cfg);
1830 return -EINVAL;
1831 }
1832
1833 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001834 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301835
1836 if (!a2dp.is_aptx_dual_mono_supported) {
1837 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1838 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1839 } else {
1840 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1841 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1842 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1843 }
1844
1845 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
1846 case 1:
1847 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1848 break;
1849 case 2:
1850 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05301851 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1852 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
Manish Dewangan6a252632017-12-04 17:27:44 +05301853 break;
1854 }
1855 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
1856 if (!a2dp.is_aptx_dual_mono_supported) {
1857 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1858 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
1859 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
1860 aptx_dsp_cfg->custom_cfg.num_channels);
1861 } else {
1862 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1863 ALOGV("Successfully updated APTX dual mono enc format with \
1864 samplingrate: %d channels:%d syncmode %d",
1865 aptx_dsp_cfg->custom_cfg.sample_rate,
1866 aptx_dsp_cfg->custom_cfg.num_channels,
1867 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
1868 }
1869 return ret;
1870}
1871#else
1872static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
1873 audio_aptx_encoder_config *aptx_bt_cfg)
1874{
1875 int ret = 0;
1876
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001877 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301878 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1879 aptx_dsp_cfg, aptx_bt_cfg);
1880 return -EINVAL;
1881 }
1882
1883 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001884 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301885 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
1886 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
1887 switch(aptx_dsp_cfg->num_channels) {
1888 case 1:
1889 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
1890 break;
1891 case 2:
1892 default:
1893 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
1894 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
1895 break;
1896 }
1897
1898 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
1899 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
1900
1901 return ret;
1902}
1903#endif
1904
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301905/* API to configure APTX DSP encoder */
1906bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1907{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301908 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08001909 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001910 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301911 bool is_configured = false;
1912 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08001913 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301914
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001915 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301916 return false;
1917
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001918 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1919 if (!ctl_enc_data) {
1920 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1921 return false;
1922 }
1923
Manish Dewangan6a252632017-12-04 17:27:44 +05301924#ifndef LINUX_ENABLED
1925 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301926 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08001927 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001928 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001929 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
1930 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001931 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08001932 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001933 else
Zhou Song8fccbb62019-03-20 01:08:19 +08001934 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001935 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001936 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001937
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001938 if (ret) {
1939 is_configured = false;
1940 goto fail;
1941 }
1942
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001943 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001944 if (aptx_ad_ctl)
1945 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001946 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08001947 else
1948 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001949 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001950 } else {
1951 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001952 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05301953 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301954#else
1955 struct custom_enc_cfg_t aptx_dsp_cfg;
1956 mixer_size = sizeof(struct custom_enc_cfg_t);
1957 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05301958 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05301959 if (ret) {
1960 is_configured = false;
1961 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301962 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001963 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1964 mixer_size);
1965#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301966 if (ret != 0) {
1967 ALOGE("%s: Failed to set APTX encoder config", __func__);
1968 is_configured = false;
1969 goto fail;
1970 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301971#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001972 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301973 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001974 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301975 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
1976 else
1977 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301978#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301979 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301980 is_configured = false;
1981 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301982 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301983 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301984 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05301985 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301986 else
Surendar Karka2febd452018-12-13 17:56:43 +05301987 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301988fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05301989 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001990 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05301991 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301992 return is_configured;
1993}
1994
1995/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301996 */
Manish Dewangan6a252632017-12-04 17:27:44 +05301997#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07001998bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05301999#else
2000bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2001#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302002{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302003 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302004 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302005 bool is_configured = false;
2006 int ret = 0;
2007
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002008 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302009 return false;
2010
2011 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2012 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002013 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302014 is_configured = false;
2015 goto fail;
2016 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302017
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302018 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002019 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302020 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2021 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2022 switch(aptx_dsp_cfg.num_channels) {
2023 case 1:
2024 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2025 break;
2026 case 2:
2027 default:
2028 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2029 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2030 break;
2031 }
2032 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302033 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302034 if (ret != 0) {
2035 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2036 is_configured = false;
2037 goto fail;
2038 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302039 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302040 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302041 is_configured = false;
2042 goto fail;
2043 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302044 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002045 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302046 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302047 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302048 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2049 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302050fail:
2051 return is_configured;
2052}
2053
2054/* API to configure AAC DSP encoder */
2055bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2056{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302057 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302058 struct aac_enc_cfg_t aac_dsp_cfg;
2059 bool is_configured = false;
2060 int ret = 0;
2061
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002062 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302063 return false;
2064
2065 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2066 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002067 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302068 is_configured = false;
2069 goto fail;
2070 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302071 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002072 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302073 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302074 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002075 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302076 case 0:
2077 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2078 break;
2079 case 2:
2080 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2081 break;
2082 case 1:
2083 default:
2084 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2085 break;
2086 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302087 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302088 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002089
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302090 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2091 sizeof(struct aac_enc_cfg_t));
2092 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002093 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302094 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302095 goto fail;
2096 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302097 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302098 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302099 is_configured = false;
2100 goto fail;
2101 }
2102 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002103 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302104 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002105 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002106 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2107 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2108fail:
2109 return is_configured;
2110}
2111
2112bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2113{
2114 struct mixer_ctl *ctl_enc_data = NULL;
2115 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2116 bool is_configured = false;
2117 int ret = 0;
2118
2119 if (aac_bt_cfg == NULL)
2120 return false;
2121
2122 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2123 if (!ctl_enc_data) {
2124 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2125 is_configured = false;
2126 goto fail;
2127 }
2128 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302129 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002130 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2131 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2132 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2133 case 0:
2134 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2135 break;
2136 case 2:
2137 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2138 break;
2139 case 1:
2140 default:
2141 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2142 break;
2143 }
2144 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2145 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2146 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2147 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2148
2149 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2150 sizeof(struct aac_enc_cfg_v2_t));
2151 if (ret != 0) {
2152 ALOGE("%s: Failed to set AAC encoder config", __func__);
2153 is_configured = false;
2154 goto fail;
2155 }
2156 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2157 if (ret != 0) {
2158 is_configured = false;
2159 goto fail;
2160 }
2161 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302162 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002163 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2164 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2165 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2166 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302167fail:
2168 return is_configured;
2169}
2170
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302171bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2172{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302173 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302174 struct celt_enc_cfg_t celt_dsp_cfg;
2175 bool is_configured = false;
2176 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002177 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302178 return false;
2179
2180 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2181 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002182 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302183 is_configured = false;
2184 goto fail;
2185 }
2186 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2187
Florian Pfister1a84f312018-07-19 14:38:18 +02002188 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302189 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2190 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2191 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2192 case 1:
2193 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2194 break;
2195 case 2:
2196 default:
2197 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2198 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2199 break;
2200 }
2201
2202 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2203
2204 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2205 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2206 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2207 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2208 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2209
2210 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2211 sizeof(struct celt_enc_cfg_t));
2212 if (ret != 0) {
2213 ALOGE("%s: Failed to set CELT encoder config", __func__);
2214 is_configured = false;
2215 goto fail;
2216 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302217 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302218 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302219 is_configured = false;
2220 goto fail;
2221 }
2222 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002223 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302224 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302225 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302226 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2227 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2228fail:
2229 return is_configured;
2230}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302231
2232bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2233{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302234 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302235 struct ldac_enc_cfg_t ldac_dsp_cfg;
2236 bool is_configured = false;
2237 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002238 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302239 return false;
2240
2241 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2242 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002243 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302244 is_configured = false;
2245 goto fail;
2246 }
2247 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2248
Florian Pfister1a84f312018-07-19 14:38:18 +02002249 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302250 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2251 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2252 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2253 case 4:
2254 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2255 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2256 break;
2257 case 2:
2258 case 1:
2259 default:
2260 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2261 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2262 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2263 break;
2264 }
2265
2266 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2267 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2268 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002269 if (ldac_bt_cfg->is_abr_enabled) {
2270 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2271 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2272 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2273 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2274 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002275 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002276 }
2277
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302278 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2279 sizeof(struct ldac_enc_cfg_t));
2280 if (ret != 0) {
2281 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2282 is_configured = false;
2283 goto fail;
2284 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302285 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302286 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302287 is_configured = false;
2288 goto fail;
2289 }
2290 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002291 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302292 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2293 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002294 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302295 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2296 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2297fail:
2298 return is_configured;
2299}
2300
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302301bool configure_a2dp_encoder_format()
2302{
2303 void *codec_info = NULL;
2304 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002305 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302306 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002307 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302308
Florian Pfister1a84f312018-07-19 14:38:18 +02002309 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302310 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2311 return false;
2312 }
2313 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002314 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302315 &codec_type);
2316
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002317 // ABR disabled by default for all codecs
2318 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302319 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002320
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302321 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002322 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302323 ALOGD(" Received SBC encoder supported BT device");
2324 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002325 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302326 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002327 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302328 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302329#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002330 a2dp.is_aptx_dual_mono_supported = false;
2331 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302332#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302333 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002334 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302335 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002336 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302337 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302338#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302339 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002340 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302341#else
2342 is_configured =
2343 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2344#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002345 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302346#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002347 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002348 ALOGD(" Received APTX dual mono encoder supported BT device");
2349 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302350 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2351 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002352 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2353 is_configured =
2354 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302355 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302356#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002357 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302358 ALOGD(" Received AAC encoder supported BT device");
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002359 bool is_aac_frame_ctl_enabled =
2360 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
2361 is_configured = is_aac_frame_ctl_enabled ?
2362 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2363 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302364 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002365 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302366 ALOGD(" Received CELT encoder supported BT device");
2367 is_configured =
2368 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2369 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002370 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302371 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002372 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2373 instance_id = MAX_INSTANCE_ID;
2374 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302375 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002376 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302377 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302378 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302379#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302380 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302381 ALOGD(" Received APTX AD encoder supported BT device");
2382 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2383 instance_id = MAX_INSTANCE_ID;
2384 a2dp.abr_config.imc_instance = instance_id--;
2385 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2386 a2dp.is_aptx_adaptive = true;
2387 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2388 is_configured =
2389 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302390 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302391 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302392#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002393 case CODEC_TYPE_PCM:
2394 ALOGD("Received PCM format for BT device");
2395 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2396 is_configured = true;
2397 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302398 default:
2399 ALOGD(" Received Unsupported encoder formar");
2400 is_configured = false;
2401 break;
2402 }
2403 return is_configured;
2404}
2405
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002406int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302407{
2408 int ret = 0;
2409
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002410 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302411
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002412 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002413 && a2dp.audio_get_enc_config)) {
2414 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302415 return -ENOSYS;
2416 }
2417
Zhou Song12c29502019-03-16 10:37:18 +08002418 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302419 //session will be restarted after suspend completion
2420 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302421 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302422 }
2423
Florian Pfister1a84f312018-07-19 14:38:18 +02002424 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302425 ALOGD("calling BT module stream start");
2426 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002427 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302428 ALOGE("BT controller start return = %d",ret);
2429 if (ret != 0 ) {
2430 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002431 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302432 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002433 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002434 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302435 ret = 0;
2436 ALOGD("Start playback successful to BT library");
2437 } else {
2438 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002439 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302440 ret = -ETIMEDOUT;
2441 }
2442 }
2443 }
2444
Florian Pfister1a84f312018-07-19 14:38:18 +02002445 if (a2dp.a2dp_source_started) {
2446 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302447 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302448 audio_a2dp_update_tws_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002449 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002450 if (a2dp.abr_config.is_abr_enabled)
2451 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302452 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302453
2454 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002455 a2dp.a2dp_source_total_active_session_requests);
2456 return ret;
2457}
2458
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002459uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002460{
2461 uint32_t latency = 0;
2462
2463 switch(a2dp.bt_decoder_format) {
2464 case CODEC_TYPE_SBC:
2465 latency = DEFAULT_SINK_LATENCY_SBC;
2466 break;
2467 case CODEC_TYPE_AAC:
2468 latency = DEFAULT_SINK_LATENCY_AAC;
2469 break;
2470 default:
2471 latency = 200;
2472 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2473 break;
2474 }
2475 return (uint64_t)latency;
2476}
2477
2478bool a2dp_send_sink_setup_complete(void) {
2479 uint64_t system_latency = 0;
2480 bool is_complete = false;
2481
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002482 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002483
2484 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2485 is_complete = true;
2486 }
2487 return is_complete;
2488}
2489
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002490bool a2dp_sink_is_ready()
2491{
2492 bool ret = false;
2493
2494 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2495 (a2dp.is_a2dp_offload_supported) &&
2496 (a2dp.audio_sink_check_a2dp_ready))
2497 ret = a2dp.audio_sink_check_a2dp_ready();
2498 return ret;
2499}
2500
2501int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002502{
2503 int ret = 0;
2504
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002505 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002506
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002507 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002508 && a2dp.audio_get_dec_config)) {
2509 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2510 return -ENOSYS;
2511 }
2512
2513 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2514 ALOGD("calling BT module stream start");
2515 /* This call indicates BT IPC lib to start capture */
2516 ret = a2dp.audio_sink_start();
2517 ALOGE("BT controller start capture return = %d",ret);
2518 if (ret != 0 ) {
2519 ALOGE("BT controller start capture failed");
2520 a2dp.a2dp_sink_started = false;
2521 } else {
2522
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002523 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002524 ALOGD("Wait for capture ready not successful");
2525 ret = -ETIMEDOUT;
2526 }
2527
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002528 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002529 a2dp.a2dp_sink_started = true;
2530 ret = 0;
2531 ALOGD("Start capture successful to BT library");
2532 } else {
2533 ALOGD(" unable to configure DSP decoder");
2534 a2dp.a2dp_sink_started = false;
2535 ret = -ETIMEDOUT;
2536 }
2537
2538 if (!a2dp_send_sink_setup_complete()) {
2539 ALOGD("sink_setup_complete not successful");
2540 ret = -ETIMEDOUT;
2541 }
2542 }
2543 }
2544
2545 if (a2dp.a2dp_sink_started) {
2546 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002547 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002548 }
2549 }
2550
2551 ALOGD("start A2DP sink total active sessions :%d",
2552 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302553 return ret;
2554}
2555
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302556static void reset_a2dp_enc_config_params()
2557{
2558 int ret =0;
2559
Aalique Grahame22e49102018-12-18 14:23:57 -08002560 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302561 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302562 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302563
2564 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
2565 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
2566 MIXER_ENC_CONFIG_BLOCK);
2567 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002568 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302569 } else {
2570 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
2571 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002572 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302573 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002574
2575 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2576
Manisha Agarwala51768b2018-11-01 16:30:52 +05302577 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2578
2579 if (!ctl_channel_mode) {
2580 ALOGE("failed to get tws mixer ctl");
2581 } else {
2582 channel_mode = "Two";
2583 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2584 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2585 }
2586 a2dp.is_tws_mono_mode_on = false;
2587 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302588}
2589
Surendar Karka2febd452018-12-13 17:56:43 +05302590static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002591{
2592 struct mixer_ctl *ctl_dec_data = NULL;
2593 struct abr_dec_cfg_t dummy_reset_cfg;
2594 int ret = 0;
2595
Preetam Singh Ranawat25780e42019-07-31 18:15:57 +05302596 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
2597 if (!ctl_dec_data) {
2598 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
2599 return -EINVAL;
2600 }
2601 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
2602 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
2603 sizeof(dummy_reset_cfg));
2604 if (ret != 0) {
2605 ALOGE("%s: Failed to set dummy decoder config", __func__);
2606 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002607 }
2608
2609 return ret;
2610}
2611
Surendar Karka2febd452018-12-13 17:56:43 +05302612static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02002613{
2614 int ret =0;
2615
2616 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
2617 struct aac_dec_cfg_t dummy_reset_config;
2618
2619 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
2620 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05302621 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02002622 if (!ctl_dec_config) {
2623 ALOGE(" ERROR a2dp decoder format mixer control not identified");
2624 } else {
2625 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
2626 sizeof(struct aac_dec_cfg_t));
2627 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
2628 }
2629 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
2630 MIXER_DEC_BIT_FORMAT);
2631 if (!ctrl_bit_format) {
2632 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
2633 } else {
2634 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
2635 if (ret != 0) {
2636 ALOGE("%s: Failed to set bit format to decoder", __func__);
2637 }
2638 }
2639}
2640
Zhou Song12c29502019-03-16 10:37:18 +08002641static void reset_codec_config()
2642{
2643 reset_a2dp_enc_config_params();
2644 reset_a2dp_source_dec_config_params();
2645 a2dp_reset_backend_cfg(SOURCE);
2646 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
2647 stop_abr();
2648 a2dp.abr_config.is_abr_enabled = false;
2649}
2650
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002651int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302652{
2653 int ret =0;
2654
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002655 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002656 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002657 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302658 return -ENOSYS;
2659 }
2660
Florian Pfister1a84f312018-07-19 14:38:18 +02002661 if (a2dp.a2dp_source_total_active_session_requests > 0)
2662 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08002663 else
2664 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302665
Florian Pfister1a84f312018-07-19 14:38:18 +02002666 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302667 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02002668 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302669 if (ret < 0)
2670 ALOGE("stop stream to BT IPC lib failed");
2671 else
2672 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08002673 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
2674 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05302675 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302676 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002677 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002678 a2dp.a2dp_source_started = false;
2679 ALOGD("Stop A2DP playback, total active sessions :%d",
2680 a2dp.a2dp_source_total_active_session_requests);
2681 return 0;
2682}
2683
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002684int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002685{
2686 int ret =0;
2687
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002688 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002689 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002690 ALOGE("a2dp handle is not identified, Ignoring stop request");
2691 return -ENOSYS;
2692 }
2693
2694 if (a2dp.a2dp_sink_total_active_session_requests > 0)
2695 a2dp.a2dp_sink_total_active_session_requests--;
2696
2697 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2698 ALOGV("calling BT module stream stop");
2699 ret = a2dp.audio_sink_stop();
2700 if (ret < 0)
2701 ALOGE("stop stream to BT IPC lib failed");
2702 else
2703 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05302704 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002705 a2dp_reset_backend_cfg(SINK);
2706 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002707 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002708 a2dp.a2dp_source_started = false;
2709 ALOGD("Stop A2DP capture, total active sessions :%d",
2710 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302711 return 0;
2712}
2713
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002714int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302715{
Aalique Grahame22e49102018-12-18 14:23:57 -08002716 int ret = 0, val, status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302717 char value[32]={0};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302718 struct audio_usecase *uc_info;
2719 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302720
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002721 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002722 ALOGV("no supported encoders identified,ignoring a2dp setparam");
2723 status = -EINVAL;
2724 goto param_handled;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302725 }
2726
2727 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
2728 sizeof(value));
Zhou Song681350a2017-10-19 16:28:42 +08002729 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302730 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002731 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002732 ALOGV("Received device connect request for A2DP source");
2733 open_a2dp_source();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302734 }
2735 goto param_handled;
2736 }
2737
2738 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
2739 sizeof(value));
2740
Zhou Song681350a2017-10-19 16:28:42 +08002741 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302742 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002743 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002744 ALOGV("Received source device dis- connect request");
Samyak Jain4828f4c2018-08-24 16:31:06 +05302745 close_a2dp_output();
kunleiz4a1fad62018-02-08 18:00:16 +08002746 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302747 reset_a2dp_source_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002748 a2dp_reset_backend_cfg(SOURCE);
2749 } else if (audio_is_a2dp_in_device(val)) {
2750 ALOGV("Received sink device dis- connect request");
2751 close_a2dp_input();
Surendar Karka2febd452018-12-13 17:56:43 +05302752 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002753 a2dp_reset_backend_cfg(SINK);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302754 }
2755 goto param_handled;
2756 }
Surendar Karka2febd452018-12-13 17:56:43 +05302757#ifndef LINUX_ENABLED
Manisha Agarwala51768b2018-11-01 16:30:52 +05302758 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
2759 if (ret>=0) {
2760 ALOGD("Setting tws channel mode to %s",value);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002761 if (!(strncmp(value,"mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302762 a2dp.is_tws_mono_mode_on = true;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002763 else if (!(strncmp(value,"dual-mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302764 a2dp.is_tws_mono_mode_on = false;
2765 audio_a2dp_update_tws_channel_mode();
2766 goto param_handled;
2767 }
Surendar Karka2febd452018-12-13 17:56:43 +05302768#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302769 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
2770 if (ret >= 0) {
Surendar Karka2febd452018-12-13 17:56:43 +05302771 if (a2dp.bt_lib_source_handle) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302772 if ((!strncmp(value,"true",sizeof(value)))) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002773 if (a2dp.a2dp_source_suspended) {
2774 ALOGD("%s: A2DP is already suspended", __func__);
2775 goto param_handled;
2776 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302777 ALOGD("Setting a2dp to suspend state");
Florian Pfister1a84f312018-07-19 14:38:18 +02002778 a2dp.a2dp_source_suspended = true;
Surendar Karka2febd452018-12-13 17:56:43 +05302779 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
yidongh7203cca2018-09-19 16:12:25 +08002780 goto param_handled;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302781 list_for_each(node, &a2dp.adev->usecase_list) {
2782 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302783 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08002784 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302785 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002786 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302787 pthread_mutex_lock(&a2dp.adev->lock);
2788 }
2789 }
Zhou Song12c29502019-03-16 10:37:18 +08002790 if (!a2dp.swb_configured)
2791 reset_codec_config();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002792 if (a2dp.audio_source_suspend)
Florian Pfister1a84f312018-07-19 14:38:18 +02002793 a2dp.audio_source_suspend();
2794 } else if (a2dp.a2dp_source_suspended == true) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302795 ALOGD("Resetting a2dp suspend state");
Zhou Song10617ed2017-05-26 13:28:48 +08002796 struct audio_usecase *uc_info;
2797 struct listnode *node;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002798 if (a2dp.clear_source_a2dpsuspend_flag)
Florian Pfister1a84f312018-07-19 14:38:18 +02002799 a2dp.clear_source_a2dpsuspend_flag();
2800 a2dp.a2dp_source_suspended = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302801 /*
2802 * It is possible that before suspend,a2dp sessions can be active
2803 * for example during music + voice activation concurrency
2804 * a2dp suspend will be called & BT will change to sco mode
2805 * though music is paused as a part of voice activation
2806 * compress session close happens only after pause timeout(10secs)
2807 * so if resume request comes before pause timeout as a2dp session
2808 * is already active IPC start will not be called from APM/audio_hw
2809 * Fix is to call a2dp start for IPC library post suspend
2810 * based on number of active session count
2811 */
Florian Pfister1a84f312018-07-19 14:38:18 +02002812 if (a2dp.a2dp_source_total_active_session_requests > 0) {
Naresh Tanniru649871a2016-11-04 18:08:32 +05302813 ALOGD(" Calling IPC lib start post suspend state");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002814 if (a2dp.audio_source_start) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002815 ret = a2dp.audio_source_start();
Naresh Tanniru649871a2016-11-04 18:08:32 +05302816 if (ret != 0) {
2817 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002818 a2dp.a2dp_source_started = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302819 }
2820 }
2821 }
Zhou Song10617ed2017-05-26 13:28:48 +08002822 list_for_each(node, &a2dp.adev->usecase_list) {
2823 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302824 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08002825 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302826 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002827 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302828 pthread_mutex_lock(&a2dp.adev->lock);
2829 }
Zhou Song10617ed2017-05-26 13:28:48 +08002830 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302831 }
2832 }
2833 goto param_handled;
2834 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002835
2836 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
2837 sizeof(value));
2838 if (ret >= 0) {
2839 if (a2dp.is_a2dp_offload_supported &&
2840 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2841 *reconfig = true;
2842 }
2843 goto param_handled;
2844 }
2845
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302846param_handled:
2847 ALOGV("end of a2dp setparam");
Aalique Grahame22e49102018-12-18 14:23:57 -08002848 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302849}
2850
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002851void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302852{
2853 a2dp.is_handoff_in_progress = is_on;
2854}
2855
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002856bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302857{
2858 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05302859 // Or if a2dp device is selected but earlier start failed ( as a2dp
2860 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02002861 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302862}
2863
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002864void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302865{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302866 *sample_rate = a2dp.enc_sampling_rate;
2867}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302868
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002869void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02002870{
2871 *sample_rate = a2dp.dec_sampling_rate;
2872}
2873
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002874bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302875{
2876 bool ret = false;
2877
Florian Pfister1a84f312018-07-19 14:38:18 +02002878 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302879 return ret;
2880
Florian Pfister1a84f312018-07-19 14:38:18 +02002881 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08002882 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002883 (a2dp.audio_source_check_a2dp_ready))
2884 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302885 return ret;
2886}
2887
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002888bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02002889{
2890 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302891}
2892
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002893void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08002894 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302895{
2896 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02002897 a2dp.bt_lib_source_handle = NULL;
2898 a2dp.a2dp_source_started = false;
2899 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
2900 a2dp.a2dp_source_total_active_session_requests = 0;
2901 a2dp.a2dp_source_suspended = false;
2902 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302903 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302904 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002905 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302906 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002907 a2dp.abr_config.is_abr_enabled = false;
2908 a2dp.abr_config.abr_started = false;
2909 a2dp.abr_config.imc_instance = 0;
2910 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08002911 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302912 a2dp.is_tws_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05302913 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08002914 a2dp.swb_configured = false;
2915
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002916 // init function pointers
2917 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08002918 init_config.fp_platform_get_pcm_device_id;
2919 fp_check_a2dp_restore = init_config.fp_check_a2dp_restore;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002920
kunleiz5a127262017-09-08 14:47:48 +08002921 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302922 reset_a2dp_source_dec_config_params();
2923 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002924
2925 a2dp.bt_lib_sink_handle = NULL;
2926 a2dp.a2dp_sink_started = false;
2927 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
2928 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002929
2930 if (is_running_with_enhanced_fwk == UNINITIALIZED)
2931 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
2932 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08002933 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02002934
2935 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302936 update_offload_codec_capabilities();
2937}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002938
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002939uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002940{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002941 uint32_t latency = 0;
2942 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302943 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
2944 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002945 char value[PROPERTY_VALUE_MAX];
2946
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002947 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08002948 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002949 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302950 if (sscanf(value, "%d/%d/%d/%d/%d%d",
2951 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002952 ALOGI("Failed to parse avsync offset params from '%s'.", value);
2953 avsync_runtime_prop = 0;
2954 }
2955 }
2956
yidongh0515e042017-07-06 15:00:34 +08002957 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02002958 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2959 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08002960 }
2961
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07002962 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002963 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08002964 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
2965 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002966 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002967 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08002968 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
2969 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002970 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002971 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08002972 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
2973 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002974 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002975 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08002976 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
2977 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002978 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002979 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302980 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
2981 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
2982 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002983 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302984 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
2985 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
2986 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05302987 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05302988 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05302989 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08002990 case CODEC_TYPE_PCM:
2991 latency = ENCODER_LATENCY_PCM;
2992 latency += DEFAULT_SINK_LATENCY_PCM;
2993 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002994 default:
2995 latency = 200;
2996 break;
2997 }
2998 return latency;
2999}
Aalique Grahame22e49102018-12-18 14:23:57 -08003000
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003001int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003002 struct str_parms *reply)
3003{
3004 int ret, val = 0;
3005 char value[32]={0};
3006
3007 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3008 value, sizeof(value));
3009 if (ret >= 0) {
3010 val = a2dp.is_a2dp_offload_supported;
3011 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3012 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3013 }
3014
3015 return 0;
3016}
Zhou Song12c29502019-03-16 10:37:18 +08003017
3018
3019bool configure_aptx_ad_speech_enc_fmt() {
3020 struct mixer_ctl *ctl_enc_data = NULL;
3021 int mixer_size = 0;
3022 int ret = 0;
3023 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3024
3025 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3026 if (!ctl_enc_data) {
3027 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3028 return false;
3029 }
3030
3031 /* Initialize dsp configuration params */
3032 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3033 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3034 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3035 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3036 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3037 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3038 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3039 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3040 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3041 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3042 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3043
3044 /* Configure AFE DSP configuration */
3045 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3046 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3047 mixer_size);
3048 if (ret != 0) {
3049 ALOGE("%s: Failed to set SWB encoder config", __func__);
3050 return false;
3051 }
3052
3053 /* Configure AFE Input Bit Format as PCM_16 */
3054 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3055 if (ret != 0) {
3056 ALOGE("%s: Failed to set SWB bit format", __func__);
3057 return false;
3058 }
3059
3060 return true;
3061}
3062
3063bool configure_aptx_ad_speech_dec_fmt()
3064{
3065 struct mixer_ctl *ctl_dec_data = NULL;
3066 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3067 int ret = 0;
3068
3069 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3070 if (!ctl_dec_data) {
3071 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3072 return false;
3073 }
3074 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3075 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3076 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3077 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3078 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3079 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3080 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3081 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3082
3083 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3084 sizeof(dec_cfg));
3085 if (ret != 0) {
3086 ALOGE("%s: Failed to set decoder config", __func__);
3087 return false;
3088 }
3089 return true;
3090}
3091
3092int sco_start_configuration()
3093{
3094 ALOGD("sco_start_configuration start");
3095
3096 if (!a2dp.swb_configured) {
3097 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3098 /* Configure AFE codec*/
3099 if (configure_aptx_ad_speech_enc_fmt() &&
3100 configure_aptx_ad_speech_dec_fmt()) {
3101 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3102 } else {
3103 ALOGE("%s: failed to send SCO configuration", __func__);
3104 return -ETIMEDOUT;
3105 }
3106 /* Configure backend*/
3107 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3108 a2dp.enc_channels = CH_MONO;
3109 a2dp.abr_config.is_abr_enabled = true;
3110 a2dp_set_backend_cfg(SOURCE);
3111 /* Start abr*/
3112 start_abr();
3113 a2dp.swb_configured = true;
3114 }
3115 return 0;
3116}
3117
3118void sco_reset_configuration()
3119{
Zhou Songd6d71752019-05-21 18:08:51 +08003120 if (a2dp.swb_configured) {
3121 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003122
Zhou Songd6d71752019-05-21 18:08:51 +08003123 reset_codec_config();
3124 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3125 a2dp.swb_configured = false;
3126 }
Zhou Song12c29502019-03-16 10:37:18 +08003127}