blob: 8cb105460762109ac07a46d3baa5642c2aa783ab [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;
1803 if (a2dp.is_tws_mono_mode_on)
1804 channel_mode = "One";
1805 else
1806 channel_mode = "Two";
1807 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1808 if (!ctl_channel_mode) {
1809 ALOGE("failed to get tws mixer ctl");
1810 return;
1811 }
1812 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1813 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1814 return;
1815 }
1816}
1817
Manish Dewangan6a252632017-12-04 17:27:44 +05301818static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
1819 audio_aptx_encoder_config *aptx_bt_cfg)
1820{
1821 int ret = 0;
1822
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001823 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301824 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1825 aptx_dsp_cfg, aptx_bt_cfg);
1826 return -EINVAL;
1827 }
1828
1829 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001830 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301831
1832 if (!a2dp.is_aptx_dual_mono_supported) {
1833 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1834 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1835 } else {
1836 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1837 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1838 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1839 }
1840
1841 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
1842 case 1:
1843 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1844 break;
1845 case 2:
1846 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05301847 if (!a2dp.is_tws_mono_mode_on) {
1848 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1849 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1850 }
1851 else {
1852 a2dp.is_tws_mono_mode_on = true;
1853 ALOGD("Update tws for mono_mode_on: %d",a2dp.is_tws_mono_mode_on);
Manisha Agarwala51768b2018-11-01 16:30:52 +05301854 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301855 break;
1856 }
1857 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
1858 if (!a2dp.is_aptx_dual_mono_supported) {
1859 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1860 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
1861 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
1862 aptx_dsp_cfg->custom_cfg.num_channels);
1863 } else {
1864 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1865 ALOGV("Successfully updated APTX dual mono enc format with \
1866 samplingrate: %d channels:%d syncmode %d",
1867 aptx_dsp_cfg->custom_cfg.sample_rate,
1868 aptx_dsp_cfg->custom_cfg.num_channels,
1869 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
1870 }
1871 return ret;
1872}
1873#else
1874static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
1875 audio_aptx_encoder_config *aptx_bt_cfg)
1876{
1877 int ret = 0;
1878
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001879 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301880 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1881 aptx_dsp_cfg, aptx_bt_cfg);
1882 return -EINVAL;
1883 }
1884
1885 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001886 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301887 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
1888 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
1889 switch(aptx_dsp_cfg->num_channels) {
1890 case 1:
1891 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
1892 break;
1893 case 2:
1894 default:
1895 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
1896 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
1897 break;
1898 }
1899
1900 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
1901 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
1902
1903 return ret;
1904}
1905#endif
1906
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301907/* API to configure APTX DSP encoder */
1908bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1909{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301910 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08001911 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001912 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301913 bool is_configured = false;
1914 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08001915 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301916
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001917 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301918 return false;
1919
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001920 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1921 if (!ctl_enc_data) {
1922 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1923 return false;
1924 }
1925
Manish Dewangan6a252632017-12-04 17:27:44 +05301926#ifndef LINUX_ENABLED
1927 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301928 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08001929 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001930 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001931 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
1932 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001933 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08001934 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001935 else
Zhou Song8fccbb62019-03-20 01:08:19 +08001936 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001937 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001938 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001939
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001940 if (ret) {
1941 is_configured = false;
1942 goto fail;
1943 }
1944
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001945 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001946 if (aptx_ad_ctl)
1947 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001948 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08001949 else
1950 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001951 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001952 } else {
1953 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001954 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05301955 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301956#else
1957 struct custom_enc_cfg_t aptx_dsp_cfg;
1958 mixer_size = sizeof(struct custom_enc_cfg_t);
1959 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05301960 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05301961 if (ret) {
1962 is_configured = false;
1963 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301964 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001965 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1966 mixer_size);
1967#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301968 if (ret != 0) {
1969 ALOGE("%s: Failed to set APTX encoder config", __func__);
1970 is_configured = false;
1971 goto fail;
1972 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301973#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001974 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301975 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001976 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301977 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
1978 else
1979 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301980#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301981 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301982 is_configured = false;
1983 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301984 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301985 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301986 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05301987 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301988 else
Surendar Karka2febd452018-12-13 17:56:43 +05301989 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301990fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05301991 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001992 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05301993 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301994 return is_configured;
1995}
1996
1997/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301998 */
Manish Dewangan6a252632017-12-04 17:27:44 +05301999#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002000bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302001#else
2002bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2003#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302004{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302005 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302006 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302007 bool is_configured = false;
2008 int ret = 0;
2009
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002010 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302011 return false;
2012
2013 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2014 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002015 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302016 is_configured = false;
2017 goto fail;
2018 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302019
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302020 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002021 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302022 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2023 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2024 switch(aptx_dsp_cfg.num_channels) {
2025 case 1:
2026 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2027 break;
2028 case 2:
2029 default:
2030 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2031 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2032 break;
2033 }
2034 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302035 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302036 if (ret != 0) {
2037 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2038 is_configured = false;
2039 goto fail;
2040 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302041 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302042 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302043 is_configured = false;
2044 goto fail;
2045 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302046 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002047 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302048 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302049 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302050 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2051 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302052fail:
2053 return is_configured;
2054}
2055
2056/* API to configure AAC DSP encoder */
2057bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2058{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302059 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302060 struct aac_enc_cfg_t aac_dsp_cfg;
2061 bool is_configured = false;
2062 int ret = 0;
2063
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002064 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302065 return false;
2066
2067 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2068 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002069 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302070 is_configured = false;
2071 goto fail;
2072 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302073 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002074 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302075 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302076 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002077 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302078 case 0:
2079 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2080 break;
2081 case 2:
2082 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2083 break;
2084 case 1:
2085 default:
2086 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2087 break;
2088 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302089 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302090 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002091
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302092 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2093 sizeof(struct aac_enc_cfg_t));
2094 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002095 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302096 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302097 goto fail;
2098 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302099 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302100 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302101 is_configured = false;
2102 goto fail;
2103 }
2104 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002105 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302106 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002107 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002108 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2109 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2110fail:
2111 return is_configured;
2112}
2113
2114bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2115{
2116 struct mixer_ctl *ctl_enc_data = NULL;
2117 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2118 bool is_configured = false;
2119 int ret = 0;
2120
2121 if (aac_bt_cfg == NULL)
2122 return false;
2123
2124 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2125 if (!ctl_enc_data) {
2126 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2127 is_configured = false;
2128 goto fail;
2129 }
2130 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302131 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002132 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2133 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2134 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2135 case 0:
2136 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2137 break;
2138 case 2:
2139 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2140 break;
2141 case 1:
2142 default:
2143 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2144 break;
2145 }
2146 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2147 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2148 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2149 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2150
2151 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2152 sizeof(struct aac_enc_cfg_v2_t));
2153 if (ret != 0) {
2154 ALOGE("%s: Failed to set AAC encoder config", __func__);
2155 is_configured = false;
2156 goto fail;
2157 }
2158 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2159 if (ret != 0) {
2160 is_configured = false;
2161 goto fail;
2162 }
2163 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302164 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002165 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2166 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2167 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2168 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302169fail:
2170 return is_configured;
2171}
2172
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302173bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2174{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302175 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302176 struct celt_enc_cfg_t celt_dsp_cfg;
2177 bool is_configured = false;
2178 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002179 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302180 return false;
2181
2182 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2183 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002184 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302185 is_configured = false;
2186 goto fail;
2187 }
2188 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2189
Florian Pfister1a84f312018-07-19 14:38:18 +02002190 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302191 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2192 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2193 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2194 case 1:
2195 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2196 break;
2197 case 2:
2198 default:
2199 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2200 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2201 break;
2202 }
2203
2204 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2205
2206 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2207 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2208 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2209 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2210 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2211
2212 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2213 sizeof(struct celt_enc_cfg_t));
2214 if (ret != 0) {
2215 ALOGE("%s: Failed to set CELT encoder config", __func__);
2216 is_configured = false;
2217 goto fail;
2218 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302219 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302220 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302221 is_configured = false;
2222 goto fail;
2223 }
2224 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002225 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302226 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302227 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302228 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2229 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2230fail:
2231 return is_configured;
2232}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302233
2234bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2235{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302236 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302237 struct ldac_enc_cfg_t ldac_dsp_cfg;
2238 bool is_configured = false;
2239 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002240 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302241 return false;
2242
2243 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2244 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002245 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302246 is_configured = false;
2247 goto fail;
2248 }
2249 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2250
Florian Pfister1a84f312018-07-19 14:38:18 +02002251 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302252 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2253 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2254 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2255 case 4:
2256 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2257 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2258 break;
2259 case 2:
2260 case 1:
2261 default:
2262 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2263 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2264 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2265 break;
2266 }
2267
2268 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2269 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2270 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002271 if (ldac_bt_cfg->is_abr_enabled) {
2272 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2273 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2274 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2275 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2276 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002277 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002278 }
2279
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302280 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2281 sizeof(struct ldac_enc_cfg_t));
2282 if (ret != 0) {
2283 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2284 is_configured = false;
2285 goto fail;
2286 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302287 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302288 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302289 is_configured = false;
2290 goto fail;
2291 }
2292 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002293 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302294 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2295 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002296 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302297 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2298 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2299fail:
2300 return is_configured;
2301}
2302
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302303bool configure_a2dp_encoder_format()
2304{
2305 void *codec_info = NULL;
2306 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002307 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302308 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002309 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302310
Florian Pfister1a84f312018-07-19 14:38:18 +02002311 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302312 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2313 return false;
2314 }
2315 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002316 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302317 &codec_type);
2318
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002319 // ABR disabled by default for all codecs
2320 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302321 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002322
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302323 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002324 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302325 ALOGD(" Received SBC encoder supported BT device");
2326 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002327 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302328 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002329 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302330 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302331#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002332 a2dp.is_aptx_dual_mono_supported = false;
2333 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302334#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302335 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002336 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302337 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002338 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302339 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302340#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302341 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002342 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302343#else
2344 is_configured =
2345 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2346#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002347 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302348#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002349 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002350 ALOGD(" Received APTX dual mono encoder supported BT device");
2351 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302352 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2353 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002354 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2355 is_configured =
2356 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302357 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302358#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002359 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302360 ALOGD(" Received AAC encoder supported BT device");
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002361 bool is_aac_frame_ctl_enabled =
2362 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
2363 is_configured = is_aac_frame_ctl_enabled ?
2364 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2365 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302366 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002367 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302368 ALOGD(" Received CELT encoder supported BT device");
2369 is_configured =
2370 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2371 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002372 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302373 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002374 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2375 instance_id = MAX_INSTANCE_ID;
2376 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302377 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002378 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302379 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302380 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302381#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302382 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302383 ALOGD(" Received APTX AD encoder supported BT device");
2384 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2385 instance_id = MAX_INSTANCE_ID;
2386 a2dp.abr_config.imc_instance = instance_id--;
2387 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2388 a2dp.is_aptx_adaptive = true;
2389 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2390 is_configured =
2391 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302392 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302393 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302394#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002395 case CODEC_TYPE_PCM:
2396 ALOGD("Received PCM format for BT device");
2397 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2398 is_configured = true;
2399 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302400 default:
2401 ALOGD(" Received Unsupported encoder formar");
2402 is_configured = false;
2403 break;
2404 }
2405 return is_configured;
2406}
2407
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002408int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302409{
2410 int ret = 0;
2411
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002412 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302413
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002414 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002415 && a2dp.audio_get_enc_config)) {
2416 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302417 return -ENOSYS;
2418 }
2419
Zhou Song12c29502019-03-16 10:37:18 +08002420 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302421 //session will be restarted after suspend completion
2422 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302423 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302424 }
2425
Florian Pfister1a84f312018-07-19 14:38:18 +02002426 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302427 ALOGD("calling BT module stream start");
2428 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002429 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302430 ALOGE("BT controller start return = %d",ret);
2431 if (ret != 0 ) {
2432 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002433 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302434 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002435 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002436 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302437 ret = 0;
2438 ALOGD("Start playback successful to BT library");
2439 } else {
2440 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002441 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302442 ret = -ETIMEDOUT;
2443 }
2444 }
2445 }
2446
Florian Pfister1a84f312018-07-19 14:38:18 +02002447 if (a2dp.a2dp_source_started) {
2448 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302449 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302450 audio_a2dp_update_tws_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002451 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002452 if (a2dp.abr_config.is_abr_enabled)
2453 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302454 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302455
2456 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002457 a2dp.a2dp_source_total_active_session_requests);
2458 return ret;
2459}
2460
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002461uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002462{
2463 uint32_t latency = 0;
2464
2465 switch(a2dp.bt_decoder_format) {
2466 case CODEC_TYPE_SBC:
2467 latency = DEFAULT_SINK_LATENCY_SBC;
2468 break;
2469 case CODEC_TYPE_AAC:
2470 latency = DEFAULT_SINK_LATENCY_AAC;
2471 break;
2472 default:
2473 latency = 200;
2474 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2475 break;
2476 }
2477 return (uint64_t)latency;
2478}
2479
2480bool a2dp_send_sink_setup_complete(void) {
2481 uint64_t system_latency = 0;
2482 bool is_complete = false;
2483
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002484 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002485
2486 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2487 is_complete = true;
2488 }
2489 return is_complete;
2490}
2491
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002492bool a2dp_sink_is_ready()
2493{
2494 bool ret = false;
2495
2496 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2497 (a2dp.is_a2dp_offload_supported) &&
2498 (a2dp.audio_sink_check_a2dp_ready))
2499 ret = a2dp.audio_sink_check_a2dp_ready();
2500 return ret;
2501}
2502
2503int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002504{
2505 int ret = 0;
2506
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002507 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002508
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002509 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002510 && a2dp.audio_get_dec_config)) {
2511 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2512 return -ENOSYS;
2513 }
2514
2515 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2516 ALOGD("calling BT module stream start");
2517 /* This call indicates BT IPC lib to start capture */
2518 ret = a2dp.audio_sink_start();
2519 ALOGE("BT controller start capture return = %d",ret);
2520 if (ret != 0 ) {
2521 ALOGE("BT controller start capture failed");
2522 a2dp.a2dp_sink_started = false;
2523 } else {
2524
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002525 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002526 ALOGD("Wait for capture ready not successful");
2527 ret = -ETIMEDOUT;
2528 }
2529
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002530 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002531 a2dp.a2dp_sink_started = true;
2532 ret = 0;
2533 ALOGD("Start capture successful to BT library");
2534 } else {
2535 ALOGD(" unable to configure DSP decoder");
2536 a2dp.a2dp_sink_started = false;
2537 ret = -ETIMEDOUT;
2538 }
2539
2540 if (!a2dp_send_sink_setup_complete()) {
2541 ALOGD("sink_setup_complete not successful");
2542 ret = -ETIMEDOUT;
2543 }
2544 }
2545 }
2546
2547 if (a2dp.a2dp_sink_started) {
2548 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002549 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002550 }
2551 }
2552
2553 ALOGD("start A2DP sink total active sessions :%d",
2554 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302555 return ret;
2556}
2557
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302558static void reset_a2dp_enc_config_params()
2559{
2560 int ret =0;
2561
Aalique Grahame22e49102018-12-18 14:23:57 -08002562 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302563 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302564 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302565
2566 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
2567 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
2568 MIXER_ENC_CONFIG_BLOCK);
2569 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002570 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302571 } else {
2572 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
2573 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002574 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302575 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002576
2577 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2578
Manisha Agarwala51768b2018-11-01 16:30:52 +05302579 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2580
2581 if (!ctl_channel_mode) {
2582 ALOGE("failed to get tws mixer ctl");
2583 } else {
2584 channel_mode = "Two";
2585 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2586 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2587 }
2588 a2dp.is_tws_mono_mode_on = false;
2589 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302590}
2591
Surendar Karka2febd452018-12-13 17:56:43 +05302592static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002593{
2594 struct mixer_ctl *ctl_dec_data = NULL;
2595 struct abr_dec_cfg_t dummy_reset_cfg;
2596 int ret = 0;
2597
Preetam Singh Ranawat25780e42019-07-31 18:15:57 +05302598 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
2599 if (!ctl_dec_data) {
2600 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
2601 return -EINVAL;
2602 }
2603 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
2604 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
2605 sizeof(dummy_reset_cfg));
2606 if (ret != 0) {
2607 ALOGE("%s: Failed to set dummy decoder config", __func__);
2608 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002609 }
2610
2611 return ret;
2612}
2613
Surendar Karka2febd452018-12-13 17:56:43 +05302614static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02002615{
2616 int ret =0;
2617
2618 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
2619 struct aac_dec_cfg_t dummy_reset_config;
2620
2621 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
2622 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05302623 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02002624 if (!ctl_dec_config) {
2625 ALOGE(" ERROR a2dp decoder format mixer control not identified");
2626 } else {
2627 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
2628 sizeof(struct aac_dec_cfg_t));
2629 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
2630 }
2631 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
2632 MIXER_DEC_BIT_FORMAT);
2633 if (!ctrl_bit_format) {
2634 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
2635 } else {
2636 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
2637 if (ret != 0) {
2638 ALOGE("%s: Failed to set bit format to decoder", __func__);
2639 }
2640 }
2641}
2642
Zhou Song12c29502019-03-16 10:37:18 +08002643static void reset_codec_config()
2644{
2645 reset_a2dp_enc_config_params();
2646 reset_a2dp_source_dec_config_params();
2647 a2dp_reset_backend_cfg(SOURCE);
2648 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
2649 stop_abr();
2650 a2dp.abr_config.is_abr_enabled = false;
2651}
2652
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002653int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302654{
2655 int ret =0;
2656
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002657 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002658 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002659 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302660 return -ENOSYS;
2661 }
2662
Florian Pfister1a84f312018-07-19 14:38:18 +02002663 if (a2dp.a2dp_source_total_active_session_requests > 0)
2664 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08002665 else
2666 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302667
Florian Pfister1a84f312018-07-19 14:38:18 +02002668 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302669 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02002670 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302671 if (ret < 0)
2672 ALOGE("stop stream to BT IPC lib failed");
2673 else
2674 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08002675 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
2676 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05302677 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302678 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002679 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002680 a2dp.a2dp_source_started = false;
2681 ALOGD("Stop A2DP playback, total active sessions :%d",
2682 a2dp.a2dp_source_total_active_session_requests);
2683 return 0;
2684}
2685
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002686int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002687{
2688 int ret =0;
2689
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002690 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002691 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002692 ALOGE("a2dp handle is not identified, Ignoring stop request");
2693 return -ENOSYS;
2694 }
2695
2696 if (a2dp.a2dp_sink_total_active_session_requests > 0)
2697 a2dp.a2dp_sink_total_active_session_requests--;
2698
2699 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2700 ALOGV("calling BT module stream stop");
2701 ret = a2dp.audio_sink_stop();
2702 if (ret < 0)
2703 ALOGE("stop stream to BT IPC lib failed");
2704 else
2705 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05302706 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002707 a2dp_reset_backend_cfg(SINK);
2708 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002709 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002710 a2dp.a2dp_source_started = false;
2711 ALOGD("Stop A2DP capture, total active sessions :%d",
2712 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302713 return 0;
2714}
2715
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002716int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302717{
Aalique Grahame22e49102018-12-18 14:23:57 -08002718 int ret = 0, val, status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302719 char value[32]={0};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302720 struct audio_usecase *uc_info;
2721 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302722
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002723 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002724 ALOGV("no supported encoders identified,ignoring a2dp setparam");
2725 status = -EINVAL;
2726 goto param_handled;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302727 }
2728
2729 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
2730 sizeof(value));
Zhou Song681350a2017-10-19 16:28:42 +08002731 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302732 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002733 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002734 ALOGV("Received device connect request for A2DP source");
2735 open_a2dp_source();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302736 }
2737 goto param_handled;
2738 }
2739
2740 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
2741 sizeof(value));
2742
Zhou Song681350a2017-10-19 16:28:42 +08002743 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302744 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002745 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002746 ALOGV("Received source device dis- connect request");
Samyak Jain4828f4c2018-08-24 16:31:06 +05302747 close_a2dp_output();
kunleiz4a1fad62018-02-08 18:00:16 +08002748 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302749 reset_a2dp_source_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002750 a2dp_reset_backend_cfg(SOURCE);
2751 } else if (audio_is_a2dp_in_device(val)) {
2752 ALOGV("Received sink device dis- connect request");
2753 close_a2dp_input();
Surendar Karka2febd452018-12-13 17:56:43 +05302754 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002755 a2dp_reset_backend_cfg(SINK);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302756 }
2757 goto param_handled;
2758 }
Surendar Karka2febd452018-12-13 17:56:43 +05302759#ifndef LINUX_ENABLED
Manisha Agarwala51768b2018-11-01 16:30:52 +05302760 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
2761 if (ret>=0) {
2762 ALOGD("Setting tws channel mode to %s",value);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002763 if (!(strncmp(value,"mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302764 a2dp.is_tws_mono_mode_on = true;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002765 else if (!(strncmp(value,"dual-mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302766 a2dp.is_tws_mono_mode_on = false;
2767 audio_a2dp_update_tws_channel_mode();
2768 goto param_handled;
2769 }
Surendar Karka2febd452018-12-13 17:56:43 +05302770#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302771 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
2772 if (ret >= 0) {
Surendar Karka2febd452018-12-13 17:56:43 +05302773 if (a2dp.bt_lib_source_handle) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302774 if ((!strncmp(value,"true",sizeof(value)))) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002775 if (a2dp.a2dp_source_suspended) {
2776 ALOGD("%s: A2DP is already suspended", __func__);
2777 goto param_handled;
2778 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302779 ALOGD("Setting a2dp to suspend state");
Florian Pfister1a84f312018-07-19 14:38:18 +02002780 a2dp.a2dp_source_suspended = true;
Surendar Karka2febd452018-12-13 17:56:43 +05302781 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
yidongh7203cca2018-09-19 16:12:25 +08002782 goto param_handled;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302783 list_for_each(node, &a2dp.adev->usecase_list) {
2784 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302785 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08002786 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302787 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002788 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302789 pthread_mutex_lock(&a2dp.adev->lock);
2790 }
2791 }
Zhou Song12c29502019-03-16 10:37:18 +08002792 if (!a2dp.swb_configured)
2793 reset_codec_config();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002794 if (a2dp.audio_source_suspend)
Florian Pfister1a84f312018-07-19 14:38:18 +02002795 a2dp.audio_source_suspend();
2796 } else if (a2dp.a2dp_source_suspended == true) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302797 ALOGD("Resetting a2dp suspend state");
Zhou Song10617ed2017-05-26 13:28:48 +08002798 struct audio_usecase *uc_info;
2799 struct listnode *node;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002800 if (a2dp.clear_source_a2dpsuspend_flag)
Florian Pfister1a84f312018-07-19 14:38:18 +02002801 a2dp.clear_source_a2dpsuspend_flag();
2802 a2dp.a2dp_source_suspended = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302803 /*
2804 * It is possible that before suspend,a2dp sessions can be active
2805 * for example during music + voice activation concurrency
2806 * a2dp suspend will be called & BT will change to sco mode
2807 * though music is paused as a part of voice activation
2808 * compress session close happens only after pause timeout(10secs)
2809 * so if resume request comes before pause timeout as a2dp session
2810 * is already active IPC start will not be called from APM/audio_hw
2811 * Fix is to call a2dp start for IPC library post suspend
2812 * based on number of active session count
2813 */
Florian Pfister1a84f312018-07-19 14:38:18 +02002814 if (a2dp.a2dp_source_total_active_session_requests > 0) {
Naresh Tanniru649871a2016-11-04 18:08:32 +05302815 ALOGD(" Calling IPC lib start post suspend state");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002816 if (a2dp.audio_source_start) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002817 ret = a2dp.audio_source_start();
Naresh Tanniru649871a2016-11-04 18:08:32 +05302818 if (ret != 0) {
2819 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002820 a2dp.a2dp_source_started = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302821 }
2822 }
2823 }
Zhou Song10617ed2017-05-26 13:28:48 +08002824 list_for_each(node, &a2dp.adev->usecase_list) {
2825 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302826 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08002827 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302828 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002829 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302830 pthread_mutex_lock(&a2dp.adev->lock);
2831 }
Zhou Song10617ed2017-05-26 13:28:48 +08002832 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302833 }
2834 }
2835 goto param_handled;
2836 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002837
2838 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
2839 sizeof(value));
2840 if (ret >= 0) {
2841 if (a2dp.is_a2dp_offload_supported &&
2842 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2843 *reconfig = true;
2844 }
2845 goto param_handled;
2846 }
2847
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302848param_handled:
2849 ALOGV("end of a2dp setparam");
Aalique Grahame22e49102018-12-18 14:23:57 -08002850 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302851}
2852
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002853void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302854{
2855 a2dp.is_handoff_in_progress = is_on;
2856}
2857
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002858bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302859{
2860 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05302861 // Or if a2dp device is selected but earlier start failed ( as a2dp
2862 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02002863 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302864}
2865
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002866void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302867{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302868 *sample_rate = a2dp.enc_sampling_rate;
2869}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302870
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002871void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02002872{
2873 *sample_rate = a2dp.dec_sampling_rate;
2874}
2875
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002876bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302877{
2878 bool ret = false;
2879
Florian Pfister1a84f312018-07-19 14:38:18 +02002880 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302881 return ret;
2882
Florian Pfister1a84f312018-07-19 14:38:18 +02002883 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08002884 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002885 (a2dp.audio_source_check_a2dp_ready))
2886 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302887 return ret;
2888}
2889
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002890bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02002891{
2892 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302893}
2894
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002895void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08002896 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302897{
2898 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02002899 a2dp.bt_lib_source_handle = NULL;
2900 a2dp.a2dp_source_started = false;
2901 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
2902 a2dp.a2dp_source_total_active_session_requests = 0;
2903 a2dp.a2dp_source_suspended = false;
2904 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302905 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302906 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002907 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302908 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002909 a2dp.abr_config.is_abr_enabled = false;
2910 a2dp.abr_config.abr_started = false;
2911 a2dp.abr_config.imc_instance = 0;
2912 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08002913 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302914 a2dp.is_tws_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05302915 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08002916 a2dp.swb_configured = false;
2917
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002918 // init function pointers
2919 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08002920 init_config.fp_platform_get_pcm_device_id;
2921 fp_check_a2dp_restore = init_config.fp_check_a2dp_restore;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002922
kunleiz5a127262017-09-08 14:47:48 +08002923 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302924 reset_a2dp_source_dec_config_params();
2925 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002926
2927 a2dp.bt_lib_sink_handle = NULL;
2928 a2dp.a2dp_sink_started = false;
2929 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
2930 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002931
2932 if (is_running_with_enhanced_fwk == UNINITIALIZED)
2933 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
2934 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08002935 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02002936
2937 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302938 update_offload_codec_capabilities();
2939}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002940
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002941uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002942{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002943 uint32_t latency = 0;
2944 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302945 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
2946 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002947 char value[PROPERTY_VALUE_MAX];
2948
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002949 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08002950 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002951 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302952 if (sscanf(value, "%d/%d/%d/%d/%d%d",
2953 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002954 ALOGI("Failed to parse avsync offset params from '%s'.", value);
2955 avsync_runtime_prop = 0;
2956 }
2957 }
2958
yidongh0515e042017-07-06 15:00:34 +08002959 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02002960 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2961 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08002962 }
2963
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07002964 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002965 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08002966 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
2967 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002968 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002969 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08002970 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
2971 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002972 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002973 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08002974 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
2975 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002976 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002977 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08002978 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
2979 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002980 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002981 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302982 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
2983 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
2984 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002985 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302986 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
2987 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
2988 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05302989 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05302990 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05302991 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08002992 case CODEC_TYPE_PCM:
2993 latency = ENCODER_LATENCY_PCM;
2994 latency += DEFAULT_SINK_LATENCY_PCM;
2995 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002996 default:
2997 latency = 200;
2998 break;
2999 }
3000 return latency;
3001}
Aalique Grahame22e49102018-12-18 14:23:57 -08003002
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003003int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003004 struct str_parms *reply)
3005{
3006 int ret, val = 0;
3007 char value[32]={0};
3008
3009 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3010 value, sizeof(value));
3011 if (ret >= 0) {
3012 val = a2dp.is_a2dp_offload_supported;
3013 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3014 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3015 }
3016
3017 return 0;
3018}
Zhou Song12c29502019-03-16 10:37:18 +08003019
3020
3021bool configure_aptx_ad_speech_enc_fmt() {
3022 struct mixer_ctl *ctl_enc_data = NULL;
3023 int mixer_size = 0;
3024 int ret = 0;
3025 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3026
3027 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3028 if (!ctl_enc_data) {
3029 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3030 return false;
3031 }
3032
3033 /* Initialize dsp configuration params */
3034 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3035 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3036 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3037 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3038 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3039 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3040 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3041 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3042 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3043 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3044 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3045
3046 /* Configure AFE DSP configuration */
3047 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3048 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3049 mixer_size);
3050 if (ret != 0) {
3051 ALOGE("%s: Failed to set SWB encoder config", __func__);
3052 return false;
3053 }
3054
3055 /* Configure AFE Input Bit Format as PCM_16 */
3056 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3057 if (ret != 0) {
3058 ALOGE("%s: Failed to set SWB bit format", __func__);
3059 return false;
3060 }
3061
3062 return true;
3063}
3064
3065bool configure_aptx_ad_speech_dec_fmt()
3066{
3067 struct mixer_ctl *ctl_dec_data = NULL;
3068 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3069 int ret = 0;
3070
3071 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3072 if (!ctl_dec_data) {
3073 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3074 return false;
3075 }
3076 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3077 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3078 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3079 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3080 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3081 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3082 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3083 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3084
3085 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3086 sizeof(dec_cfg));
3087 if (ret != 0) {
3088 ALOGE("%s: Failed to set decoder config", __func__);
3089 return false;
3090 }
3091 return true;
3092}
3093
3094int sco_start_configuration()
3095{
3096 ALOGD("sco_start_configuration start");
3097
3098 if (!a2dp.swb_configured) {
3099 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3100 /* Configure AFE codec*/
3101 if (configure_aptx_ad_speech_enc_fmt() &&
3102 configure_aptx_ad_speech_dec_fmt()) {
3103 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3104 } else {
3105 ALOGE("%s: failed to send SCO configuration", __func__);
3106 return -ETIMEDOUT;
3107 }
3108 /* Configure backend*/
3109 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3110 a2dp.enc_channels = CH_MONO;
3111 a2dp.abr_config.is_abr_enabled = true;
3112 a2dp_set_backend_cfg(SOURCE);
3113 /* Start abr*/
3114 start_abr();
3115 a2dp.swb_configured = true;
3116 }
3117 return 0;
3118}
3119
3120void sco_reset_configuration()
3121{
Zhou Songd6d71752019-05-21 18:08:51 +08003122 if (a2dp.swb_configured) {
3123 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003124
Zhou Songd6d71752019-05-21 18:08:51 +08003125 reset_codec_config();
3126 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3127 a2dp.swb_configured = false;
3128 }
Zhou Song12c29502019-03-16 10:37:18 +08003129}