blob: 0d58df69ea0fad6d87122d2b2497e02602a74912 [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 Tanniru9d027a62015-03-13 01:32:10 +053052#define AUDIO_PARAMETER_A2DP_STARTED "A2dpStarted"
Naresh Tanniru66cf06c2019-03-20 19:30:37 +053053#define BT_IPC_SOURCE_LIB_NAME "btaudio_offload_if.so"
Florian Pfister1a84f312018-07-19 14:38:18 +020054#define BT_IPC_SINK_LIB_NAME "libbthost_if_sink.so"
55#define MEDIA_FMT_NONE 0
56#define MEDIA_FMT_AAC 0x00010DA6
57#define MEDIA_FMT_APTX 0x000131ff
58#define MEDIA_FMT_APTX_HD 0x00013200
Surendar Karka2febd452018-12-13 17:56:43 +053059#define MEDIA_FMT_APTX_AD 0x00013204
Florian Pfister1a84f312018-07-19 14:38:18 +020060#define MEDIA_FMT_SBC 0x00010BF2
61#define MEDIA_FMT_CELT 0x00013221
62#define MEDIA_FMT_LDAC 0x00013224
63#define MEDIA_FMT_MP3 0x00010BE9
64#define MEDIA_FMT_APTX_ADAPTIVE 0x00013204
Zhou Song12c29502019-03-16 10:37:18 +080065#define MEDIA_FMT_APTX_AD_SPEECH 0x00013208
Naresh Tanniru9d027a62015-03-13 01:32:10 +053066#define MEDIA_FMT_AAC_AOT_LC 2
67#define MEDIA_FMT_AAC_AOT_SBR 5
68#define MEDIA_FMT_AAC_AOT_PS 29
Naresh Tanniru9d027a62015-03-13 01:32:10 +053069#define PCM_CHANNEL_L 1
70#define PCM_CHANNEL_R 2
71#define PCM_CHANNEL_C 3
72#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
73#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
74#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
75#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
76#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
77#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
Surendar Karka2febd452018-12-13 17:56:43 +053078#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
79#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"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053093#define MIXER_ENC_FMT_SBC "SBC"
94#define MIXER_ENC_FMT_AAC "AAC"
95#define MIXER_ENC_FMT_APTX "APTX"
Manisha Agarwala51768b2018-11-01 16:30:52 +053096#define MIXER_FMT_TWS_CHANNEL_MODE "TWS Channel Mode"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053097#define MIXER_ENC_FMT_APTXHD "APTXHD"
Aalique Grahame22e49102018-12-18 14:23:57 -080098#define MIXER_END_FMT_LDAC "LDAC"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053099#define MIXER_ENC_FMT_NONE "NONE"
yidongh0515e042017-07-06 15:00:34 +0800100#define ENCODER_LATENCY_SBC 10
101#define ENCODER_LATENCY_APTX 40
102#define ENCODER_LATENCY_APTX_HD 20
103#define ENCODER_LATENCY_AAC 70
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530104//To Do: Fine Tune Encoder CELT/LDAC latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530105#define ENCODER_LATENCY_CELT 40
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530106#define ENCODER_LATENCY_LDAC 40
Aalique Grahame22e49102018-12-18 14:23:57 -0800107#define ENCODER_LATENCY_PCM 50
yidongh0515e042017-07-06 15:00:34 +0800108#define DEFAULT_SINK_LATENCY_SBC 140
109#define DEFAULT_SINK_LATENCY_APTX 160
110#define DEFAULT_SINK_LATENCY_APTX_HD 180
111#define DEFAULT_SINK_LATENCY_AAC 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530112//To Do: Fine Tune Default CELT/LDAC Latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530113#define DEFAULT_SINK_LATENCY_CELT 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530114#define DEFAULT_SINK_LATENCY_LDAC 180
Aalique Grahame22e49102018-12-18 14:23:57 -0800115#define DEFAULT_SINK_LATENCY_PCM 140
116
117#define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported"
118#define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled"
119#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
120
121// Default encoder bit width
122#define DEFAULT_ENCODER_BIT_FORMAT 16
123
124// Default encoder latency
125#define DEFAULT_ENCODER_LATENCY 200
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530126
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800127// Slimbus Tx sample rate for ABR feedback channel
128#define ABR_TX_SAMPLE_RATE "KHZ_8"
129
Zhou Song12c29502019-03-16 10:37:18 +0800130// Slimbus Tx sample rate for APTX AD SPEECH
131#define SPEECH_TX_SAMPLE_RATE "KHZ_96"
132
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800133// Purpose ID for Inter Module Communication (IMC) in AFE
134#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
135
136// Maximum quality levels for ABR
137#define MAX_ABR_QUALITY_LEVELS 5
138
139// Instance identifier for A2DP
140#define MAX_INSTANCE_ID (UINT32_MAX / 2)
141
Zhou Song12c29502019-03-16 10:37:18 +0800142// Instance identifier for SWB
143#define APTX_AD_SPEECH_INSTANCE_ID 37
144
145#define SAMPLING_RATE_96K 96000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530146#define SAMPLING_RATE_48K 48000
147#define SAMPLING_RATE_441K 44100
Zhou Song12c29502019-03-16 10:37:18 +0800148#define SAMPLING_RATE_32K 32000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530149#define CH_STEREO 2
150#define CH_MONO 1
Florian Pfister1a84f312018-07-19 14:38:18 +0200151#define SOURCE 0
152#define SINK 1
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800153#define UNINITIALIZED -1
154
155#ifdef __LP64__
156#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
157#else
158#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
159#endif
160
161static void *vndk_fwk_lib_handle = NULL;
162static int is_running_with_enhanced_fwk = UNINITIALIZED;
163
164typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
165static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
Florian Pfister1a84f312018-07-19 14:38:18 +0200166
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530167/*
168 * Below enum values are extended from audio_base.h to
Florian Pfister1a84f312018-07-19 14:38:18 +0200169 * to keep encoder and decoder type local to bthost_ipc
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530170 * and audio_hal as these are intended only for handshake
171 * between IPC lib and Audio HAL.
172 */
173typedef enum {
Florian Pfister1a84f312018-07-19 14:38:18 +0200174 CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
175 CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
176 CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
177 CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
178 CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530179#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +0200180 CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530181#endif
Florian Pfister1a84f312018-07-19 14:38:18 +0200182 CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
183 CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
Surendar Karka2febd452018-12-13 17:56:43 +0530184 CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
Zhou Song12c29502019-03-16 10:37:18 +0800185 CODEC_TYPE_APTX_AD_SPEECH = 637534208u, //0x26000000UL
Aalique Grahame22e49102018-12-18 14:23:57 -0800186 CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u
Florian Pfister1a84f312018-07-19 14:38:18 +0200187}codec_t;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530188
Sharad Sangle95d451b2018-06-19 12:24:20 +0530189/*
190 * enums which describes the APTX Adaptive
191 * channel mode, these values are used by encoder
192 */
193 typedef enum {
194 APTX_AD_CHANNEL_UNCHANGED = -1,
195 APTX_AD_CHANNEL_JOINT_STEREO = 0, // default
196 APTX_AD_CHANNEL_MONO = 1,
197 APTX_AD_CHANNEL_DUAL_MONO = 2,
198 APTX_AD_CHANNEL_STEREO_TWS = 4,
199 APTX_AD_CHANNEL_EARBUD = 8,
200} enc_aptx_ad_channel_mode;
201
202/*
203 * enums which describes the APTX Adaptive
204 * sampling frequency, these values are used
205 * by encoder
206 */
207typedef enum {
208 APTX_AD_SR_UNCHANGED = 0x0,
209 APTX_AD_48 = 0x1, // 48 KHz default
210 APTX_AD_44_1 = 0x2, // 44.1kHz
211} enc_aptx_ad_s_rate;
212
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530213typedef void (*bt_audio_pre_init_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200214typedef int (*audio_source_open_t)(void);
215typedef int (*audio_source_close_t)(void);
216typedef int (*audio_source_start_t)(void);
217typedef int (*audio_source_stop_t)(void);
218typedef int (*audio_source_suspend_t)(void);
219typedef void (*audio_source_handoff_triggered_t)(void);
220typedef void (*clear_source_a2dpsuspend_flag_t)(void);
221typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
222 uint8_t *num_dev, codec_t *codec_type);
223typedef int (*audio_source_check_a2dp_ready_t)(void);
224typedef int (*audio_is_source_scrambling_enabled_t)(void);
Manisha Agarwala51768b2018-11-01 16:30:52 +0530225typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200226typedef int (*audio_sink_start_t)(void);
227typedef int (*audio_sink_stop_t)(void);
228typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
229typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
230typedef int (*audio_sink_check_a2dp_ready_t)(void);
231typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530232
233enum A2DP_STATE {
234 A2DP_STATE_CONNECTED,
235 A2DP_STATE_STARTED,
236 A2DP_STATE_STOPPED,
237 A2DP_STATE_DISCONNECTED,
238};
239
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800240typedef enum {
241 IMC_TRANSMIT,
242 IMC_RECEIVE,
243} imc_direction_t;
244
245typedef enum {
246 IMC_DISABLE,
247 IMC_ENABLE,
248} imc_status_t;
249
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700250typedef enum {
Zhou Song12c29502019-03-16 10:37:18 +0800251 SWAP_DISABLE,
252 SWAP_ENABLE,
253} swap_status_t;
254
255typedef enum {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700256 MTU_SIZE,
257 PEAK_BIT_RATE,
258} frame_control_type_t;
259
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800260// --- external function dependency ---
261fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
262fp_check_a2dp_restore_t fp_check_a2dp_restore;
263
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800264/* PCM config for ABR Feedback hostless front end */
265static struct pcm_config pcm_config_abr = {
266 .channels = 1,
267 .rate = 8000,
268 .period_size = 240,
269 .period_count = 2,
270 .format = PCM_FORMAT_S16_LE,
271 .start_threshold = 0,
272 .stop_threshold = INT_MAX,
273 .avail_min = 0,
274};
275
276/* Adaptive bitrate config for A2DP codecs */
277struct a2dp_abr_config {
278 /* Flag to denote whether Adaptive bitrate is enabled for codec */
279 bool is_abr_enabled;
280 /* Flag to denote whether front end has been opened for ABR */
281 bool abr_started;
282 /* ABR Tx path pcm handle */
283 struct pcm *abr_tx_handle;
Zhou Song12c29502019-03-16 10:37:18 +0800284 /* ABR Rx path pcm handle */
285 struct pcm *abr_rx_handle;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800286 /* ABR Inter Module Communication (IMC) instance ID */
287 uint32_t imc_instance;
288};
289
290static uint32_t instance_id = MAX_INSTANCE_ID;
291
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530292/* structure used to update a2dp state machine
293 * to communicate IPC library
294 * to store DSP encoder configuration information
295 */
296struct a2dp_data {
297 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200298 void *bt_lib_source_handle;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530299 bt_audio_pre_init_t bt_audio_pre_init;
Florian Pfister1a84f312018-07-19 14:38:18 +0200300 audio_source_open_t audio_source_open;
301 audio_source_close_t audio_source_close;
302 audio_source_start_t audio_source_start;
303 audio_source_stop_t audio_source_stop;
304 audio_source_suspend_t audio_source_suspend;
305 audio_source_handoff_triggered_t audio_source_handoff_triggered;
306 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
307 audio_get_enc_config_t audio_get_enc_config;
308 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530309 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200310 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
311 enum A2DP_STATE bt_state_source;
312 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530313 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530314 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200315 bool a2dp_source_started;
316 bool a2dp_source_suspended;
317 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530318 bool is_a2dp_offload_supported;
319 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700320 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530321 /* Mono Mode support for TWS+ */
322 bool is_tws_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530323 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800324 /* Adaptive bitrate config for A2DP codecs */
325 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200326
327 void *bt_lib_sink_handle;
328 audio_sink_start_t audio_sink_start;
329 audio_sink_stop_t audio_sink_stop;
330 audio_get_dec_config_t audio_get_dec_config;
331 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
332 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
333 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
334 enum A2DP_STATE bt_state_sink;
335 codec_t bt_decoder_format;
336 uint32_t dec_sampling_rate;
337 uint32_t dec_channels;
338 bool a2dp_sink_started;
339 int a2dp_sink_total_active_session_requests;
Zhou Song12c29502019-03-16 10:37:18 +0800340 bool swb_configured;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530341};
342
343struct a2dp_data a2dp;
344
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800345/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
346 * Structures sent to configure DSP for ABR are defined below.
347 * This data helps DSP configure feedback path (BTSoC to LPASS)
348 * for link quality levels and mapping quality levels to codec
349 * specific bitrate.
350 */
351
352/* Key value pair for link quality level to bitrate mapping. */
353struct bit_rate_level_map_t {
354 uint32_t link_quality_level;
355 uint32_t bitrate;
356};
357
358/* Link quality level to bitrate mapping info sent to DSP. */
359struct quality_level_to_bitrate_info {
360 /* Number of quality levels being mapped.
361 * This will be equal to the size of mapping table.
362 */
363 uint32_t num_levels;
364 /* Quality level to bitrate mapping table */
365 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
366};
367
368/* Structure to set up Inter Module Communication (IMC) between
369 * AFE Decoder and Encoder.
370 */
371struct imc_dec_enc_info {
372 /* Decoder to encoder communication direction.
373 * Transmit = 0 / Receive = 1
374 */
375 uint32_t direction;
376 /* Enable / disable IMC between decoder and encoder */
377 uint32_t enable;
378 /* Purpose of IMC being set up between decoder and encoder.
379 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
380 * is the default value to be sent as purpose.
381 */
382 uint32_t purpose;
383 /* Unique communication instance ID.
384 * purpose and comm_instance together form the actual key
385 * used in IMC registration, which must be the same for
386 * encoder and decoder for which IMC is being set up.
387 */
388 uint32_t comm_instance;
389};
390
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700391/* Structure to control frame size of AAC encoded frames. */
392struct aac_frame_size_control_t {
393 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
394 uint32_t ctl_type;
395 /* Control value
396 * MTU_SIZE: MTU size in bytes
397 * PEAK_BIT_RATE: Peak bitrate in bits per second.
398 */
399 uint32_t ctl_value;
400};
401
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800402/* Structure used for ABR config of AFE encoder and decoder. */
403struct abr_enc_cfg_t {
404 /* Link quality level to bitrate mapping info sent to DSP. */
405 struct quality_level_to_bitrate_info mapping_info;
406 /* Information to set up IMC between decoder and encoder */
407 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700408 /* Flag to indicate whether ABR is enabled */
409 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800410} __attribute__ ((packed));
411
412/* Structure to send configuration for decoder introduced
413 * on AFE Tx path for ABR link quality feedback to BT encoder.
414 */
415struct abr_dec_cfg_t {
416 /* Decoder media format */
417 uint32_t dec_format;
418 /* Information to set up IMC between decoder and encoder */
419 struct imc_dec_enc_info imc_info;
420} __attribute__ ((packed));
421
Zhou Song12c29502019-03-16 10:37:18 +0800422struct aptx_ad_speech_mode_cfg_t
423{
424 uint32_t mode;
425 uint32_t swapping;
426} __attribute__ ((packed));
427
428/* Structure for SWB voice dec config */
429struct aptx_ad_speech_dec_cfg_t {
430 struct abr_dec_cfg_t abr_cfg;
431 struct aptx_ad_speech_mode_cfg_t speech_mode;
432} __attribute__ ((packed));
433
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530434/* START of DSP configurable structures
435 * These values should match with DSP interface defintion
436 */
437
438/* AAC encoder configuration structure. */
439typedef struct aac_enc_cfg_t aac_enc_cfg_t;
440
441/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
442 * supported aac_fmt_flag are ADTS/RAW
443 * supported channel_cfg are Native mode, Mono , Stereo
444 */
445struct aac_enc_cfg_t {
446 uint32_t enc_format;
447 uint32_t bit_rate;
448 uint32_t enc_mode;
449 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530450 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530451 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530452} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530453
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700454struct aac_enc_cfg_v2_t {
455 struct aac_enc_cfg_t aac_enc_cfg;
456 struct aac_frame_size_control_t frame_ctl;
457} __attribute__ ((packed));
458
Surendar Karkabbb3c822018-11-12 13:00:38 +0530459typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
460struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200461 uint16_t aac_fmt_flag; /* LATM*/
462 uint16_t audio_object_type; /* LC */
463 uint16_t channels; /* Stereo */
464 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
465 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
466 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530467} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200468
Surendar Karkabbb3c822018-11-12 13:00:38 +0530469typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
470struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200471 uint16_t channels; /* Mono, Stereo */
472 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
473 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530474} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200475
476/* AAC decoder configuration structure. */
477typedef struct aac_dec_cfg_t aac_dec_cfg_t;
478struct aac_dec_cfg_t {
479 uint32_t dec_format;
480 audio_aac_decoder_config_t data;
481} __attribute__ ((packed));
482
483/* SBC decoder configuration structure. */
484typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
485struct sbc_dec_cfg_t {
486 uint32_t dec_format;
487 audio_sbc_decoder_config_t data;
488} __attribute__ ((packed));
489
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530490/* SBC encoder configuration structure. */
491typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
492
493/* supported num_subbands are 4/8
494 * supported blk_len are 4, 8, 12, 16
495 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
496 * supported alloc_method are LOUNDNESS/SNR
497 * supported bit_rate for mono channel is max 320kbps
498 * supported bit rate for stereo channel is max 512 kbps
499 */
500struct sbc_enc_cfg_t{
501 uint32_t enc_format;
502 uint32_t num_subbands;
503 uint32_t blk_len;
504 uint32_t channel_mode;
505 uint32_t alloc_method;
506 uint32_t bit_rate;
507 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530508} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530509
510
511/* supported num_channels are Mono/Stereo
512 * supported channel_mapping for mono is CHANNEL_C
513 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
514 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700515 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530516struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530517{
518 uint32_t enc_format;
519 uint32_t sample_rate;
520 uint16_t num_channels;
521 uint16_t reserved;
522 uint8_t channel_mapping[8];
523 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530524} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530525
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530526struct celt_specific_enc_cfg_t
527{
528 uint32_t bit_rate;
529 uint16_t frame_size;
530 uint16_t complexity;
531 uint16_t prediction_mode;
532 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530533} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530534
535struct celt_enc_cfg_t
536{
537 struct custom_enc_cfg_t custom_cfg;
538 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530539} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700540
541/* sync_mode introduced with APTX V2 libraries
542 * sync mode: 0x0 = stereo sync mode
543 * 0x01 = dual mono sync mode
544 * 0x02 = dual mono with no sync on either L or R codewords
545 */
546struct aptx_v2_enc_cfg_ext_t
547{
548 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530549} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700550
551/* APTX struct for combining custom enc and V2 fields */
552struct aptx_enc_cfg_t
553{
554 struct custom_enc_cfg_t custom_cfg;
555 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530556} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700557
Sharad Sangle95d451b2018-06-19 12:24:20 +0530558/* APTX AD structure */
559struct aptx_ad_enc_cfg_ext_t
560{
561 uint32_t sampling_freq;
562 uint32_t mtu;
563 uint32_t channel_mode;
564 uint32_t min_sink_modeA;
565 uint32_t max_sink_modeA;
566 uint32_t min_sink_modeB;
567 uint32_t max_sink_modeB;
568 uint32_t min_sink_modeC;
569 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530570 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530571} __attribute__ ((packed));
572
573struct aptx_ad_enc_cfg_t
574{
575 struct custom_enc_cfg_t custom_cfg;
576 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
577 struct abr_enc_cfg_t abr_cfg;
578} __attribute__ ((packed));
579
Zhou Song12c29502019-03-16 10:37:18 +0800580/* APTX AD SPEECH structure */
581struct aptx_ad_speech_enc_cfg_t
582{
583 struct custom_enc_cfg_t custom_cfg;
584 /* Information to set up IMC between decoder and encoder */
585 struct imc_dec_enc_info imc_info;
586 struct aptx_ad_speech_mode_cfg_t speech_mode;
587} __attribute__ ((packed));
588
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530589struct ldac_specific_enc_cfg_t
590{
591 uint32_t bit_rate;
592 uint16_t channel_mode;
593 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530594} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530595
596struct ldac_enc_cfg_t
597{
598 struct custom_enc_cfg_t custom_cfg;
599 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800600 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530601} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530602
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530603/* In LE BT source code uses system/audio.h for below
604 * structure definition. To avoid multiple definition
605 * compilation error for audiohal in LE , masking structure
606 * definition under "LINUX_ENABLED" which is defined only
607 * in LE
608 */
609#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530610/* TODO: Define the following structures only for O using PLATFORM_VERSION */
611/* Information about BT SBC encoder configuration
612 * This data is used between audio HAL module and
613 * BT IPC library to configure DSP encoder
614 */
615typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530616 uint32_t subband; /* 4, 8 */
617 uint32_t blk_len; /* 4, 8, 12, 16 */
618 uint16_t sampling_rate; /*44.1khz,48khz*/
619 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
620 uint8_t alloc; /*0(Loudness),1(SNR)*/
621 uint8_t min_bitpool; /* 2 */
622 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
623 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530624 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530625} audio_sbc_encoder_config;
626
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530627/* Information about BT APTX encoder configuration
628 * This data is used between audio HAL module and
629 * BT IPC library to configure DSP encoder
630 */
631typedef struct {
632 uint16_t sampling_rate;
633 uint8_t channels;
634 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530635 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700636} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530637
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700638typedef struct {
Sharad Sangle95d451b2018-06-19 12:24:20 +0530639 uint8_t sampling_rate;
640 uint8_t channel_mode;
641 uint16_t mtu;
642 uint8_t min_sink_modeA;
643 uint8_t max_sink_modeA;
644 uint8_t min_sink_modeB;
645 uint8_t max_sink_modeB;
646 uint8_t min_sink_modeC;
647 uint8_t max_sink_modeC;
648 uint8_t TTP_modeA_low;
649 uint8_t TTP_modeA_high;
650 uint8_t TTP_modeB_low;
651 uint8_t TTP_modeB_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530652 uint32_t bits_per_sample;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530653 uint16_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530654} audio_aptx_ad_config;
655
656typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700657 uint16_t sampling_rate;
658 uint8_t channels;
659 uint32_t bitrate;
660 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530661 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700662} audio_aptx_dual_mono_config;
663
664typedef union {
665 audio_aptx_default_config *default_cfg;
666 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530667 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700668} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530669
670/* Information about BT AAC encoder configuration
671 * This data is used between audio HAL module and
672 * BT IPC library to configure DSP encoder
673 */
674typedef struct {
675 uint32_t enc_mode; /* LC, SBR, PS */
676 uint16_t format_flag; /* RAW, ADTS */
677 uint16_t channels; /* 1-Mono, 2-Stereo */
678 uint32_t sampling_rate;
679 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530680 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530681} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530682#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700683
684typedef struct {
685 audio_aac_encoder_config audio_aac_enc_cfg;
686 struct aac_frame_size_control_t frame_ctl;
687} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530688
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530689/* Information about BT CELT encoder configuration
690 * This data is used between audio HAL module and
691 * BT IPC library to configure DSP encoder
692 */
693typedef struct {
694 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
695 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
696 uint16_t frame_size; /* 64-128-256-512, 512 */
697 uint16_t complexity; /* 0-10, 1 */
698 uint16_t prediction_mode; /* 0-1-2, 0 */
699 uint16_t vbr_flag; /* 0-1, 0*/
700 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530701 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530702} audio_celt_encoder_config;
703
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530704/* Information about BT LDAC encoder configuration
705 * This data is used between audio HAL module and
706 * BT IPC library to configure DSP encoder
707 */
708typedef struct {
709 uint32_t sampling_rate; /*44100,48000,88200,96000*/
710 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
711 uint16_t channel_mode; /* 0, 4, 2, 1*/
712 uint16_t mtu; /*679*/
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800713 bool is_abr_enabled;
714 struct quality_level_to_bitrate_info level_to_bitrate_map;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530715 uint32_t bits_per_sample;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530716} audio_ldac_encoder_config;
717
Surendar Karkabbb3c822018-11-12 13:00:38 +0530718/* Information about BT AAC decoder configuration
719 * This data is used between audio HAL module and
720 * BT IPC library to configure DSP decoder
721 */
722typedef struct {
723 uint16_t aac_fmt_flag; /* LATM*/
724 uint16_t audio_object_type; /* LC */
725 uint16_t channels; /* Stereo */
726 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
727 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
728 44.1k, 48k, 64k, 88.2k, 96k */
729} audio_aac_dec_config_t;
730
731/* Information about BT SBC decoder configuration
732 * This data is used between audio HAL module and
733 * BT IPC library to configure DSP decoder
734 */
735typedef struct {
736 uint16_t channels; /* Mono, Stereo */
737 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
738 44.1k, 48k, 64k, 88.2k, 96k */
739}audio_sbc_dec_config_t;
740
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530741/*********** END of DSP configurable structures ********************/
742
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530743static void update_offload_codec_capabilities()
744{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530745
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530746 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800747 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
748 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
749
750 ALOGD("%s: A2DP offload supported = %d",__func__,
751 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530752}
753
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800754static int stop_abr()
755{
756 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800757 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800758 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800759 int ret = 0;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800760
761 /* This function can be used if !abr_started for clean up */
762 ALOGV("%s: enter", __func__);
763
764 // Close hostless front end
765 if (a2dp.abr_config.abr_tx_handle != NULL) {
766 pcm_close(a2dp.abr_config.abr_tx_handle);
767 a2dp.abr_config.abr_tx_handle = NULL;
768 }
Zhou Song12c29502019-03-16 10:37:18 +0800769 if (a2dp.abr_config.abr_rx_handle != NULL) {
770 pcm_close(a2dp.abr_config.abr_rx_handle);
771 a2dp.abr_config.abr_rx_handle = NULL;
772 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800773 a2dp.abr_config.abr_started = false;
774 a2dp.abr_config.imc_instance = 0;
775
776 // Reset BT driver mixer control for ABR usecase
777 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
778 MIXER_SET_FEEDBACK_CHANNEL);
779 if (!ctl_set_bt_feedback_channel) {
780 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800781 ret = -ENOSYS;
782 } else if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800783 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800784 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800785 }
786
787 // Reset ABR Tx feedback path
788 ALOGV("%s: Disable ABR Tx feedback path", __func__);
789 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
790 MIXER_ABR_TX_FEEDBACK_PATH);
791 if (!ctl_abr_tx_path) {
792 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800793 ret = -ENOSYS;
794 } else if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800795 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800796 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800797 }
798
Zhou Song12c29502019-03-16 10:37:18 +0800799 // Reset ABR Rx feedback path
800 ALOGV("%s: Disable ABR Rx feedback path", __func__);
801 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
802 MIXER_ABR_RX_FEEDBACK_PATH);
803 if (!ctl_abr_rx_path) {
804 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
805 ret = -ENOSYS;
806 } else if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 0) != 0) {
807 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
808 ret = -ENOSYS;
809 }
810
811 return ret;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800812}
813
814static int start_abr()
815{
816 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800817 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800818 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
819 int abr_device_id;
820 int ret = 0;
821
822 if (!a2dp.abr_config.is_abr_enabled) {
823 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
824 return -ENOSYS;
825 }
826
827 if (a2dp.abr_config.abr_started) {
828 ALOGI("%s: ABR has already started", __func__);
829 return ret;
830 }
831
832 // Enable Slimbus 7 Tx feedback path
833 ALOGV("%s: Enable ABR Tx feedback path", __func__);
834 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
835 MIXER_ABR_TX_FEEDBACK_PATH);
836 if (!ctl_abr_tx_path) {
837 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
838 return -ENOSYS;
839 }
840 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
841 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
842 return -ENOSYS;
843 }
844
845 // Notify ABR usecase information to BT driver to distinguish
846 // between SCO and feedback usecase
847 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
848 MIXER_SET_FEEDBACK_CHANNEL);
849 if (!ctl_set_bt_feedback_channel) {
850 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800851 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800852 }
853 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
854 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800855 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800856 }
857
858 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800859 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800860 PCM_CAPTURE);
861 if (!a2dp.abr_config.abr_tx_handle) {
862 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
863 abr_device_id, PCM_IN,
864 &pcm_config_abr);
Zhou Song12c29502019-03-16 10:37:18 +0800865 if (a2dp.abr_config.abr_tx_handle == NULL) {
866 ALOGE("%s: Can't open abr tx device", __func__);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800867 goto fail;
Zhou Song12c29502019-03-16 10:37:18 +0800868 }
869 if (!(pcm_is_ready(a2dp.abr_config.abr_tx_handle) &&
870 !pcm_start(a2dp.abr_config.abr_tx_handle))) {
871 ALOGE("%s: tx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
872 goto fail;
873 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800874 }
Zhou Song12c29502019-03-16 10:37:18 +0800875
876 // Enable Slimbus 7 Rx feedback path for HD Voice use case
877 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH) {
878 ALOGV("%s: Enable ABR Rx feedback path", __func__);
879 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
880 MIXER_ABR_RX_FEEDBACK_PATH);
881 if (!ctl_abr_rx_path) {
882 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
883 goto fail;
884 }
885 if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 1) != 0) {
886 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
887 goto fail;
888 }
889
890 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
891 ALOGE("%s: Failed to set BT usecase", __func__);
892 goto fail;
893 }
894
895 // Open hostless front end and prepare ABR Rx path
896 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
897 PCM_PLAYBACK);
898 if (!a2dp.abr_config.abr_rx_handle) {
899 a2dp.abr_config.abr_rx_handle = pcm_open(a2dp.adev->snd_card,
900 abr_device_id, PCM_OUT,
901 &pcm_config_abr);
902 if (a2dp.abr_config.abr_rx_handle == NULL) {
903 ALOGE("%s: Can't open abr rx device", __func__);
904 goto fail;
905 }
906 if (!(pcm_is_ready(a2dp.abr_config.abr_rx_handle) &&
907 !pcm_start(a2dp.abr_config.abr_rx_handle))) {
908 ALOGE("%s: rx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_rx_handle));
909 goto fail;
910 }
911 }
912 }
913
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800914 a2dp.abr_config.abr_started = true;
915
916 return ret;
917
918fail:
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800919 stop_abr();
920 return -ENOSYS;
921}
922
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800923static int check_if_enhanced_fwk() {
924
925 int is_enhanced_fwk = 1;
926 //dlopen lib
927 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
928 if (vndk_fwk_lib_handle != NULL) {
929 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
930 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
931 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
932 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
933 __func__);
934 is_enhanced_fwk = 1;
935 } else {
936 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
937 }
938 }
939 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
940 return is_enhanced_fwk;
941}
942
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530943static void open_a2dp_source() {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530944 int ret = 0;
945
Florian Pfister1a84f312018-07-19 14:38:18 +0200946 ALOGD(" Open A2DP source start ");
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530947
948 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
949 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
950 ALOGD("calling BT stream open");
951 ret = a2dp.audio_source_open();
952 if(ret != 0) {
953 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Srinu Jellada99a592019-01-25 16:50:52 +0530954 }
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530955 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530956 } else {
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530957 ALOGD("Called a2dp open with improper state %d", a2dp.bt_state_source);
958 }
959 } else {
960 ALOGE("a2dp handle is not identified, Ignoring open request");
961 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
962 }
963}
964/* API to open BT IPC library to start IPC communication for BT Source*/
965static void a2dp_source_init()
966{
967 ALOGD("a2dp_source_init START");
968 if (a2dp.bt_lib_source_handle == NULL) {
969 ALOGD("Requesting for BT lib handle");
970 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
971 if (a2dp.bt_lib_source_handle == NULL) {
972 ALOGE("%s: dlopen failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
973 return;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530974 }
975 }
976
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530977 a2dp.bt_audio_pre_init = (bt_audio_pre_init_t)
978 dlsym(a2dp.bt_lib_source_handle, "bt_audio_pre_init");
Srinu Jellada99a592019-01-25 16:50:52 +0530979 a2dp.audio_source_open = (audio_source_open_t)
980 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
981 a2dp.audio_source_start = (audio_source_start_t)
982 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
983 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
984 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
985 a2dp.audio_source_suspend = (audio_source_suspend_t)
986 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
987 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
988 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
989 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
990 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
991 a2dp.audio_source_stop = (audio_source_stop_t)
992 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
993 a2dp.audio_source_close = (audio_source_close_t)
994 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
995 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
996 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
997 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
998 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
999 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1000 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1001 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1002 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1003
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301004 if (is_running_with_enhanced_fwk == UNINITIALIZED)
1005 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
1006 if (a2dp.bt_lib_source_handle && is_running_with_enhanced_fwk
1007 && a2dp.bt_audio_pre_init) {
1008 ALOGD("calling BT module preinit");
1009 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001010 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001011}
1012
1013/* API to open BT IPC library to start IPC communication for BT Sink*/
1014static void open_a2dp_sink()
1015{
1016 ALOGD(" Open A2DP input start ");
1017 if (a2dp.bt_lib_sink_handle == NULL){
1018 ALOGD(" Requesting for BT lib handle");
1019 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1020
1021 if (a2dp.bt_lib_sink_handle == NULL) {
1022 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1023 } else {
1024 a2dp.audio_sink_start = (audio_sink_start_t)
1025 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1026 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1027 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1028 a2dp.audio_sink_stop = (audio_sink_stop_t)
1029 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1030 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1031 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1032 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1033 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1034 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301035 }
1036}
1037
1038static int close_a2dp_output()
1039{
1040 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001041
1042 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1043 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301044 return -ENOSYS;
1045 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001046
1047 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1048 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001049 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001050 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301051 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001052 a2dp.a2dp_source_started = false;
1053 a2dp.a2dp_source_total_active_session_requests = 0;
1054 a2dp.a2dp_source_suspended = false;
1055 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301056 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301057 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001058 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001059 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1060 stop_abr();
1061 a2dp.abr_config.is_abr_enabled = false;
1062 a2dp.abr_config.abr_started = false;
1063 a2dp.abr_config.imc_instance = 0;
1064 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001065 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301066 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001067
1068 return 0;
1069}
1070
1071static int close_a2dp_input()
1072{
1073 ALOGV("%s\n",__func__);
1074
1075 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1076 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1077 return -ENOSYS;
1078 }
1079
1080 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1081 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001082 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001083 ALOGE("failed close a2dp sink control path from BT library");
1084 }
1085 a2dp.a2dp_sink_started = false;
1086 a2dp.a2dp_sink_total_active_session_requests = 0;
1087 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1088 a2dp.dec_sampling_rate = 48000;
1089 a2dp.dec_channels = 2;
1090 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301091
1092 return 0;
1093}
1094
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301095static void a2dp_check_and_set_scrambler()
1096{
1097 bool scrambler_mode = false;
1098 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001099 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1100 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301101
1102 if (scrambler_mode) {
1103 //enable scrambler in dsp
1104 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1105 MIXER_SCRAMBLER_MODE);
1106 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001107 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301108 return;
1109 } else {
1110 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1111 ALOGE("%s: Could not set scrambler mode", __func__);
1112 return;
1113 }
1114 }
1115 }
1116}
1117
Florian Pfister1a84f312018-07-19 14:38:18 +02001118static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301119{
Florian Pfister1a84f312018-07-19 14:38:18 +02001120 char *rate_str = NULL, *channels = NULL;
1121 uint32_t sampling_rate;
1122 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1123 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301124
Florian Pfister1a84f312018-07-19 14:38:18 +02001125 if (direction == SINK) {
1126 sampling_rate = a2dp.dec_sampling_rate;
1127 } else {
1128 sampling_rate = a2dp.enc_sampling_rate;
1129 }
1130 //For LDAC encoder and AAC decoder open slimbus port at
1131 //96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1132 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301133 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Florian Pfister1a84f312018-07-19 14:38:18 +02001134 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301135 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1136 sampling_rate = sampling_rate *2;
1137 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001138
Aalique Grahame22e49102018-12-18 14:23:57 -08001139 // No need to configure backend for PCM format.
1140 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1141 return 0;
1142 }
1143
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301144 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301145 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301146 case 44100:
1147 rate_str = "KHZ_44P1";
1148 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301149 case 88200:
1150 rate_str = "KHZ_88P2";
1151 break;
1152 case 96000:
1153 rate_str = "KHZ_96";
1154 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001155 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301156 default:
1157 rate_str = "KHZ_48";
1158 break;
1159 }
1160
Florian Pfister1a84f312018-07-19 14:38:18 +02001161 if (direction == SINK) {
1162 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1163 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301164 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001165 } else {
1166 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1167 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301168 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001169 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301170 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001171
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301172 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1173 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301174 is_configured = false;
1175 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301176 }
1177
Surendar Karka2febd452018-12-13 17:56:43 +05301178 if (direction == SOURCE) {
1179 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001180 if (a2dp.abr_config.is_abr_enabled) {
1181 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH)
1182 rate_str = SPEECH_TX_SAMPLE_RATE;
1183 else
1184 rate_str = ABR_TX_SAMPLE_RATE;
1185 }
Surendar Karka2febd452018-12-13 17:56:43 +05301186 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1187 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1188 MIXER_SOURCE_SAMPLE_RATE_TX);
1189 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301190 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301191 is_configured = false;
1192 goto fail;
1193 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301194
Surendar Karka2febd452018-12-13 17:56:43 +05301195 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1196 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1197 is_configured = false;
1198 goto fail;
1199 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301200 }
1201 } else {
1202 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1203 MIXER_SAMPLE_RATE_TX is not supported */
1204 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1205 MIXER_SAMPLE_RATE_DEFAULT);
1206 if (!ctl_sample_rate) {
1207 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301208 is_configured = false;
1209 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301210 }
1211
1212 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1213 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001214 is_configured = false;
1215 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301216 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301217 }
1218
Florian Pfister1a84f312018-07-19 14:38:18 +02001219 if (direction == SINK) {
1220 switch (a2dp.dec_channels) {
1221 case 1:
1222 channels = "One";
1223 break;
1224 case 2:
1225 default:
1226 channels = "Two";
1227 break;
1228 }
1229
Ramu Gottipati02809682018-12-19 16:46:12 +05301230 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001231 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1232 MIXER_AFE_SINK_CHANNELS);
1233 } else {
1234 //Configure AFE enc channels
1235 switch (a2dp.enc_channels) {
1236 case 1:
1237 channels = "One";
1238 break;
1239 case 2:
1240 default:
1241 channels = "Two";
1242 break;
1243 }
1244
Ramu Gottipati02809682018-12-19 16:46:12 +05301245 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001246 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1247 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301248 }
1249
Florian Pfister1a84f312018-07-19 14:38:18 +02001250 if (!ctrl_channels) {
1251 ALOGE(" ERROR AFE channels mixer control not identified");
1252 } else {
1253 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301254 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001255 is_configured = false;
1256 goto fail;
1257 }
1258 }
1259 is_configured = true;
1260fail:
1261 return is_configured;
1262}
1263
Surendar Karkabbb3c822018-11-12 13:00:38 +05301264bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001265{
1266 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1267 struct aac_dec_cfg_t aac_dsp_cfg;
1268 bool is_configured = false;
1269 int ret = 0;
1270
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001271 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001272 return false;
1273
Surendar Karka2febd452018-12-13 17:56:43 +05301274 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001275 if (!ctl_dec_data) {
1276 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1277 is_configured = false;
1278 goto fail;
1279 }
1280
1281 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1282 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1283 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1284 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1285 switch(aac_bt_cfg->audio_object_type) {
1286 case 0:
1287 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301288 break;
1289 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001290 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1291 break;
1292 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301293 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001294 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301295 break;
1296 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001297 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1298 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1299 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1300 sizeof(struct aac_dec_cfg_t));
1301 if (ret != 0) {
1302 ALOGE("%s: failed to set AAC decoder config", __func__);
1303 is_configured = false;
1304 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001305 }
1306
Florian Pfister1a84f312018-07-19 14:38:18 +02001307 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1308 MIXER_DEC_BIT_FORMAT);
1309 if (!ctrl_bit_format) {
1310 ALOGE(" ERROR Dec bit format mixer control not identified");
1311 is_configured = false;
1312 goto fail;
1313 }
1314 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1315 if (ret != 0) {
1316 ALOGE("%s: Failed to set bit format to decoder", __func__);
1317 is_configured = false;
1318 goto fail;
1319 }
1320
1321 is_configured = true;
1322 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1323 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1324 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1325 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1326 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1327fail:
1328 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301329}
1330
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301331static int a2dp_set_bit_format(uint32_t enc_bit_format)
1332{
1333 const char *bit_format = NULL;
1334 struct mixer_ctl *ctrl_bit_format = NULL;
1335
1336 // Configure AFE Input Bit Format
1337 switch (enc_bit_format) {
1338 case 32:
1339 bit_format = "S32_LE";
1340 break;
1341 case 24:
1342 bit_format = "S24_LE";
1343 break;
1344 case 16:
1345 default:
1346 bit_format = "S16_LE";
1347 break;
1348 }
1349
1350 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1351 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1352 MIXER_ENC_BIT_FORMAT);
1353 if (!ctrl_bit_format) {
1354 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1355 return -ENOSYS;
1356 }
1357 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1358 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1359 return -ENOSYS;
1360 }
1361 return 0;
1362}
1363
Surendar Karka2febd452018-12-13 17:56:43 +05301364static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301365{
Surendar Karka2febd452018-12-13 17:56:43 +05301366 const char *rate_str = "KHZ_8", *channels = "Zero";
1367 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1368 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301369
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001370 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301371 if (direction == SINK) {
1372 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1373 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1374 MIXER_SINK_SAMPLE_RATE);
1375 } else {
1376 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1377 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1378 MIXER_SAMPLE_RATE_RX);
1379 }
1380 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301381
Surendar Karka2febd452018-12-13 17:56:43 +05301382 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1383 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301384 return -ENOSYS;
1385 }
Zhou Song12c29502019-03-16 10:37:18 +08001386 if (a2dp.abr_config.is_abr_enabled) {
1387 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1388 MIXER_SOURCE_SAMPLE_RATE_TX);
1389 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301390 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1391 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001392 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301393
Zhou Song12c29502019-03-16 10:37:18 +08001394 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1395 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1396 return -ENOSYS;
1397 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301398 }
1399 } else {
1400
Surendar Karka2febd452018-12-13 17:56:43 +05301401 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301402 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301403 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301404 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1405 return -ENOSYS;
1406 }
1407
Surendar Karka2febd452018-12-13 17:56:43 +05301408 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301409 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1410 return -ENOSYS;
1411 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001412 }
1413
1414 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301415 if (direction == SINK) {
1416 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1417 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1418 MIXER_AFE_SINK_CHANNELS);
1419 } else {
1420 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1421 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1422 MIXER_AFE_IN_CHANNELS);
1423 }
1424 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001425 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1426 return -ENOSYS;
1427 }
Surendar Karka2febd452018-12-13 17:56:43 +05301428 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001429 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1430 return -ENOSYS;
1431 }
1432
1433 return 0;
1434}
1435
1436/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301437static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001438{
1439 struct mixer_ctl *ctl_dec_data = NULL;
1440 struct abr_dec_cfg_t dec_cfg;
1441 int ret = 0;
1442
1443 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301444 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001445 if (!ctl_dec_data) {
1446 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1447 return false;
1448 }
1449 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1450 dec_cfg.dec_format = dec_format;
1451 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1452 dec_cfg.imc_info.enable = IMC_ENABLE;
1453 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1454 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1455
1456 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1457 sizeof(dec_cfg));
1458 if (ret != 0) {
1459 ALOGE("%s: Failed to set decoder config", __func__);
1460 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301461 }
Surendar Karka2febd452018-12-13 17:56:43 +05301462 }
1463
1464 return true;
1465}
1466
Surendar Karkabbb3c822018-11-12 13:00:38 +05301467bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001468{
1469 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1470 struct sbc_dec_cfg_t sbc_dsp_cfg;
1471 bool is_configured = false;
1472 int ret = 0;
1473
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001474 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001475 goto fail;
1476
Surendar Karka2febd452018-12-13 17:56:43 +05301477 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001478 if (!ctl_dec_data) {
1479 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1480 is_configured = false;
1481 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301482 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001483
Florian Pfister1a84f312018-07-19 14:38:18 +02001484 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1485 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1486 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1487 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1488 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1489 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301490
Florian Pfister1a84f312018-07-19 14:38:18 +02001491 if (ret != 0) {
1492 ALOGE("%s: failed to set SBC decoder config", __func__);
1493 is_configured = false;
1494 goto fail;
1495 }
1496
1497 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1498 MIXER_DEC_BIT_FORMAT);
1499 if (!ctrl_bit_format) {
1500 ALOGE(" ERROR Dec bit format mixer control not identified");
1501 is_configured = false;
1502 goto fail;
1503 }
1504 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1505 if (ret != 0) {
1506 ALOGE("%s: Failed to set bit format to decoder", __func__);
1507 is_configured = false;
1508 goto fail;
1509 }
1510
1511 is_configured = true;
1512 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1513 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1514 a2dp.dec_channels = 1;
1515 else
1516 a2dp.dec_channels = 2;
1517 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1518 ALOGV("Successfully updated SBC dec format");
1519fail:
1520 return is_configured;
1521}
1522
Florian Pfister1a84f312018-07-19 14:38:18 +02001523/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301524static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301525{
Florian Pfister1a84f312018-07-19 14:38:18 +02001526 void *codec_info = NULL;
1527 codec_t codec_type = CODEC_TYPE_INVALID;
1528 bool is_configured = false;
1529 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301530
Florian Pfister1a84f312018-07-19 14:38:18 +02001531 if (!a2dp.audio_get_dec_config) {
1532 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1533 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301534 }
1535
Surendar Karka2febd452018-12-13 17:56:43 +05301536 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001537 if (!ctl_dec_data) {
1538 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1539 is_configured = false;
1540 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301541 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001542 codec_info = a2dp.audio_get_dec_config(&codec_type);
1543 switch(codec_type) {
1544 case CODEC_TYPE_SBC:
1545 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301546 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001547 break;
1548 case CODEC_TYPE_AAC:
1549 ALOGD(" AAC decoder supported BT device");
1550 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301551 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001552 break;
1553 default:
1554 ALOGD(" Received Unsupported decoder format");
1555 is_configured = false;
1556 break;
1557 }
1558 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301559}
1560
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301561/* API to configure SBC DSP encoder */
1562bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1563{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301564 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301565 struct sbc_enc_cfg_t sbc_dsp_cfg;
1566 bool is_configured = false;
1567 int ret = 0;
1568
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001569 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301570 return false;
1571
1572 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1573 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001574 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301575 is_configured = false;
1576 goto fail;
1577 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301578 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001579 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301580 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1581 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1582 switch(sbc_bt_cfg->channels) {
1583 case 0:
1584 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1585 break;
1586 case 1:
1587 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1588 break;
1589 case 3:
1590 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1591 break;
1592 case 2:
1593 default:
1594 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1595 break;
1596 }
1597 if (sbc_bt_cfg->alloc)
1598 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1599 else
1600 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1601 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1602 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1603 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1604 sizeof(struct sbc_enc_cfg_t));
1605 if (ret != 0) {
1606 ALOGE("%s: failed to set SBC encoder config", __func__);
1607 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301608 goto fail;
1609 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301610 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301611 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301612 is_configured = false;
1613 goto fail;
1614 }
1615 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001616 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301617 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301618
1619 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1620 a2dp.enc_channels = 1;
1621 else
1622 a2dp.enc_channels = 2;
1623
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301624 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1625 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301626fail:
1627 return is_configured;
1628}
1629
Manish Dewangan6a252632017-12-04 17:27:44 +05301630#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301631static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1632 audio_aptx_encoder_config *aptx_bt_cfg)
1633{
1634 int ret = 0;
1635
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001636 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301637 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1638 aptx_dsp_cfg, aptx_bt_cfg);
1639 return -EINVAL;
1640 }
1641
1642 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301643 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301644
1645
1646 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1647 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1648 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1649 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1650 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1651 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1652 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1653 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1654 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301655 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301656 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1657 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1658 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1659 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1660
1661
1662 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1663 case APTX_AD_CHANNEL_UNCHANGED:
1664 case APTX_AD_CHANNEL_JOINT_STEREO:
1665 case APTX_AD_CHANNEL_DUAL_MONO:
1666 case APTX_AD_CHANNEL_STEREO_TWS:
1667 case APTX_AD_CHANNEL_EARBUD:
1668 default:
1669 a2dp.enc_channels = CH_STEREO;
1670 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1671 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1672 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1673 break;
1674 case APTX_AD_CHANNEL_MONO:
1675 a2dp.enc_channels = CH_MONO;
1676 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1677 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1678 break;
1679 }
1680 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1681 case APTX_AD_SR_UNCHANGED:
1682 case APTX_AD_48:
1683 default:
1684 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1685 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1686 break;
1687 case APTX_AD_44_1:
1688 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1689 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
1690 break;
1691 }
1692 ALOGV("Successfully updated APTX AD enc format with \
1693 samplingrate: %d channels:%d",
1694 aptx_dsp_cfg->custom_cfg.sample_rate,
1695 aptx_dsp_cfg->custom_cfg.num_channels);
1696
1697 return ret;
1698}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301699
1700static void audio_a2dp_update_tws_channel_mode()
1701{
1702 char* channel_mode;
1703 struct mixer_ctl *ctl_channel_mode;
1704 if (a2dp.is_tws_mono_mode_on)
1705 channel_mode = "One";
1706 else
1707 channel_mode = "Two";
1708 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1709 if (!ctl_channel_mode) {
1710 ALOGE("failed to get tws mixer ctl");
1711 return;
1712 }
1713 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1714 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1715 return;
1716 }
1717}
1718
Manish Dewangan6a252632017-12-04 17:27:44 +05301719static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
1720 audio_aptx_encoder_config *aptx_bt_cfg)
1721{
1722 int ret = 0;
1723
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001724 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301725 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1726 aptx_dsp_cfg, aptx_bt_cfg);
1727 return -EINVAL;
1728 }
1729
1730 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001731 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301732
1733 if (!a2dp.is_aptx_dual_mono_supported) {
1734 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1735 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1736 } else {
1737 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1738 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1739 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1740 }
1741
1742 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
1743 case 1:
1744 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1745 break;
1746 case 2:
1747 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05301748 if (!a2dp.is_tws_mono_mode_on) {
1749 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1750 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1751 }
1752 else {
1753 a2dp.is_tws_mono_mode_on = true;
1754 ALOGD("Update tws for mono_mode_on: %d",a2dp.is_tws_mono_mode_on);
Manisha Agarwala51768b2018-11-01 16:30:52 +05301755 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301756 break;
1757 }
1758 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
1759 if (!a2dp.is_aptx_dual_mono_supported) {
1760 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1761 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
1762 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
1763 aptx_dsp_cfg->custom_cfg.num_channels);
1764 } else {
1765 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1766 ALOGV("Successfully updated APTX dual mono enc format with \
1767 samplingrate: %d channels:%d syncmode %d",
1768 aptx_dsp_cfg->custom_cfg.sample_rate,
1769 aptx_dsp_cfg->custom_cfg.num_channels,
1770 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
1771 }
1772 return ret;
1773}
1774#else
1775static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
1776 audio_aptx_encoder_config *aptx_bt_cfg)
1777{
1778 int ret = 0;
1779
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001780 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301781 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1782 aptx_dsp_cfg, aptx_bt_cfg);
1783 return -EINVAL;
1784 }
1785
1786 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001787 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301788 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
1789 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
1790 switch(aptx_dsp_cfg->num_channels) {
1791 case 1:
1792 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
1793 break;
1794 case 2:
1795 default:
1796 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
1797 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
1798 break;
1799 }
1800
1801 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
1802 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
1803
1804 return ret;
1805}
1806#endif
1807
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301808/* API to configure APTX DSP encoder */
1809bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1810{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301811 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001812 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301813 bool is_configured = false;
1814 int ret = 0;
Manish Dewangan6a252632017-12-04 17:27:44 +05301815 int sample_rate_backup;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301816
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001817 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301818 return false;
1819
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001820 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1821 if (!ctl_enc_data) {
1822 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1823 return false;
1824 }
1825
Manish Dewangan6a252632017-12-04 17:27:44 +05301826#ifndef LINUX_ENABLED
1827 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301828 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001829 if (a2dp.is_aptx_adaptive) {
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001830 mixer_size = sizeof(struct aptx_ad_enc_cfg_t);
1831 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
1832 sample_rate_backup = aptx_ad_dsp_cfg.custom_cfg.sample_rate;
1833 } else {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301834 mixer_size = sizeof(struct aptx_enc_cfg_t);
1835 sample_rate_backup = aptx_bt_cfg->default_cfg->sampling_rate;
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001836 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
1837 }
1838 if (ret) {
1839 is_configured = false;
1840 goto fail;
1841 }
1842
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001843 if (a2dp.is_aptx_adaptive) {
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001844 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
1845 mixer_size);
1846 } else {
1847 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1848 mixer_size);
Sharad Sangle95d451b2018-06-19 12:24:20 +05301849 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301850#else
1851 struct custom_enc_cfg_t aptx_dsp_cfg;
1852 mixer_size = sizeof(struct custom_enc_cfg_t);
1853 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05301854 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05301855 if (ret) {
1856 is_configured = false;
1857 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301858 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001859 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1860 mixer_size);
1861#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301862 if (ret != 0) {
1863 ALOGE("%s: Failed to set APTX encoder config", __func__);
1864 is_configured = false;
1865 goto fail;
1866 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301867#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001868 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301869 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001870 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301871 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
1872 else
1873 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301874#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301875 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301876 is_configured = false;
1877 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301878 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301879 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301880 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05301881 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301882 else
Surendar Karka2febd452018-12-13 17:56:43 +05301883 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301884fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05301885 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001886 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05301887 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301888 return is_configured;
1889}
1890
1891/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301892 */
Manish Dewangan6a252632017-12-04 17:27:44 +05301893#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07001894bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05301895#else
1896bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1897#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301898{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301899 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301900 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301901 bool is_configured = false;
1902 int ret = 0;
1903
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001904 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301905 return false;
1906
1907 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1908 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001909 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301910 is_configured = false;
1911 goto fail;
1912 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301913
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301914 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001915 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301916 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
1917 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
1918 switch(aptx_dsp_cfg.num_channels) {
1919 case 1:
1920 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1921 break;
1922 case 2:
1923 default:
1924 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1925 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1926 break;
1927 }
1928 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301929 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301930 if (ret != 0) {
1931 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
1932 is_configured = false;
1933 goto fail;
1934 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301935 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301936 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301937 is_configured = false;
1938 goto fail;
1939 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301940 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001941 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301942 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301943 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301944 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
1945 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301946fail:
1947 return is_configured;
1948}
1949
1950/* API to configure AAC DSP encoder */
1951bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
1952{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301953 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301954 struct aac_enc_cfg_t aac_dsp_cfg;
1955 bool is_configured = false;
1956 int ret = 0;
1957
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001958 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301959 return false;
1960
1961 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1962 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001963 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301964 is_configured = false;
1965 goto fail;
1966 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301967 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001968 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301969 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05301970 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001971 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301972 case 0:
1973 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
1974 break;
1975 case 2:
1976 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
1977 break;
1978 case 1:
1979 default:
1980 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
1981 break;
1982 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05301983 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301984 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001985
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301986 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
1987 sizeof(struct aac_enc_cfg_t));
1988 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001989 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301990 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301991 goto fail;
1992 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301993 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301994 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301995 is_configured = false;
1996 goto fail;
1997 }
1998 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001999 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302000 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002001 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002002 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2003 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2004fail:
2005 return is_configured;
2006}
2007
2008bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2009{
2010 struct mixer_ctl *ctl_enc_data = NULL;
2011 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2012 bool is_configured = false;
2013 int ret = 0;
2014
2015 if (aac_bt_cfg == NULL)
2016 return false;
2017
2018 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2019 if (!ctl_enc_data) {
2020 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2021 is_configured = false;
2022 goto fail;
2023 }
2024 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302025 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002026 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2027 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2028 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2029 case 0:
2030 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2031 break;
2032 case 2:
2033 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2034 break;
2035 case 1:
2036 default:
2037 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2038 break;
2039 }
2040 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2041 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2042 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2043 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2044
2045 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2046 sizeof(struct aac_enc_cfg_v2_t));
2047 if (ret != 0) {
2048 ALOGE("%s: Failed to set AAC encoder config", __func__);
2049 is_configured = false;
2050 goto fail;
2051 }
2052 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2053 if (ret != 0) {
2054 is_configured = false;
2055 goto fail;
2056 }
2057 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302058 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002059 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2060 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2061 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2062 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302063fail:
2064 return is_configured;
2065}
2066
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302067bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2068{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302069 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302070 struct celt_enc_cfg_t celt_dsp_cfg;
2071 bool is_configured = false;
2072 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002073 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302074 return false;
2075
2076 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2077 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002078 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302079 is_configured = false;
2080 goto fail;
2081 }
2082 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2083
Florian Pfister1a84f312018-07-19 14:38:18 +02002084 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302085 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2086 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2087 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2088 case 1:
2089 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2090 break;
2091 case 2:
2092 default:
2093 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2094 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2095 break;
2096 }
2097
2098 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2099
2100 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2101 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2102 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2103 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2104 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2105
2106 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2107 sizeof(struct celt_enc_cfg_t));
2108 if (ret != 0) {
2109 ALOGE("%s: Failed to set CELT encoder config", __func__);
2110 is_configured = false;
2111 goto fail;
2112 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302113 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302114 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302115 is_configured = false;
2116 goto fail;
2117 }
2118 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002119 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302120 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302121 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302122 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2123 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2124fail:
2125 return is_configured;
2126}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302127
2128bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2129{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302130 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302131 struct ldac_enc_cfg_t ldac_dsp_cfg;
2132 bool is_configured = false;
2133 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002134 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302135 return false;
2136
2137 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2138 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002139 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302140 is_configured = false;
2141 goto fail;
2142 }
2143 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2144
Florian Pfister1a84f312018-07-19 14:38:18 +02002145 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302146 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2147 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2148 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2149 case 4:
2150 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2151 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2152 break;
2153 case 2:
2154 case 1:
2155 default:
2156 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2157 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2158 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2159 break;
2160 }
2161
2162 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2163 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2164 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002165 if (ldac_bt_cfg->is_abr_enabled) {
2166 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2167 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2168 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2169 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2170 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002171 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002172 }
2173
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302174 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2175 sizeof(struct ldac_enc_cfg_t));
2176 if (ret != 0) {
2177 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2178 is_configured = false;
2179 goto fail;
2180 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302181 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302182 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302183 is_configured = false;
2184 goto fail;
2185 }
2186 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002187 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302188 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2189 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002190 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302191 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2192 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2193fail:
2194 return is_configured;
2195}
2196
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302197bool configure_a2dp_encoder_format()
2198{
2199 void *codec_info = NULL;
2200 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002201 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302202 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002203 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302204
Florian Pfister1a84f312018-07-19 14:38:18 +02002205 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302206 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2207 return false;
2208 }
2209 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002210 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302211 &codec_type);
2212
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002213 // ABR disabled by default for all codecs
2214 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302215 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002216
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302217 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002218 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302219 ALOGD(" Received SBC encoder supported BT device");
2220 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002221 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302222 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002223 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302224 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302225#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002226 a2dp.is_aptx_dual_mono_supported = false;
2227 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302228#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302229 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002230 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302231 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002232 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302233 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302234#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302235 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002236 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302237#else
2238 is_configured =
2239 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2240#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002241 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302242#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002243 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002244 ALOGD(" Received APTX dual mono encoder supported BT device");
2245 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302246 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2247 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002248 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2249 is_configured =
2250 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302251 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302252#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002253 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302254 ALOGD(" Received AAC encoder supported BT device");
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002255 bool is_aac_frame_ctl_enabled =
2256 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
2257 is_configured = is_aac_frame_ctl_enabled ?
2258 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2259 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302260 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002261 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302262 ALOGD(" Received CELT encoder supported BT device");
2263 is_configured =
2264 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2265 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002266 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302267 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002268 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2269 instance_id = MAX_INSTANCE_ID;
2270 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302271 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002272 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302273 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302274 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302275#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302276 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302277 ALOGD(" Received APTX AD encoder supported BT device");
2278 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2279 instance_id = MAX_INSTANCE_ID;
2280 a2dp.abr_config.imc_instance = instance_id--;
2281 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2282 a2dp.is_aptx_adaptive = true;
2283 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2284 is_configured =
2285 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302286 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302287 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302288#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002289 case CODEC_TYPE_PCM:
2290 ALOGD("Received PCM format for BT device");
2291 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2292 is_configured = true;
2293 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302294 default:
2295 ALOGD(" Received Unsupported encoder formar");
2296 is_configured = false;
2297 break;
2298 }
2299 return is_configured;
2300}
2301
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002302int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302303{
2304 int ret = 0;
2305
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002306 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302307
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002308 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002309 && a2dp.audio_get_enc_config)) {
2310 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302311 return -ENOSYS;
2312 }
2313
Zhou Song12c29502019-03-16 10:37:18 +08002314 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302315 //session will be restarted after suspend completion
2316 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302317 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302318 }
2319
Florian Pfister1a84f312018-07-19 14:38:18 +02002320 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302321 ALOGD("calling BT module stream start");
2322 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002323 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302324 ALOGE("BT controller start return = %d",ret);
2325 if (ret != 0 ) {
2326 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002327 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302328 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002329 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002330 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302331 ret = 0;
2332 ALOGD("Start playback successful to BT library");
2333 } else {
2334 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002335 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302336 ret = -ETIMEDOUT;
2337 }
2338 }
2339 }
2340
Florian Pfister1a84f312018-07-19 14:38:18 +02002341 if (a2dp.a2dp_source_started) {
2342 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302343 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302344 audio_a2dp_update_tws_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002345 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002346 if (a2dp.abr_config.is_abr_enabled)
2347 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302348 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302349
2350 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002351 a2dp.a2dp_source_total_active_session_requests);
2352 return ret;
2353}
2354
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002355uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002356{
2357 uint32_t latency = 0;
2358
2359 switch(a2dp.bt_decoder_format) {
2360 case CODEC_TYPE_SBC:
2361 latency = DEFAULT_SINK_LATENCY_SBC;
2362 break;
2363 case CODEC_TYPE_AAC:
2364 latency = DEFAULT_SINK_LATENCY_AAC;
2365 break;
2366 default:
2367 latency = 200;
2368 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2369 break;
2370 }
2371 return (uint64_t)latency;
2372}
2373
2374bool a2dp_send_sink_setup_complete(void) {
2375 uint64_t system_latency = 0;
2376 bool is_complete = false;
2377
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002378 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002379
2380 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2381 is_complete = true;
2382 }
2383 return is_complete;
2384}
2385
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002386bool a2dp_sink_is_ready()
2387{
2388 bool ret = false;
2389
2390 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2391 (a2dp.is_a2dp_offload_supported) &&
2392 (a2dp.audio_sink_check_a2dp_ready))
2393 ret = a2dp.audio_sink_check_a2dp_ready();
2394 return ret;
2395}
2396
2397int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002398{
2399 int ret = 0;
2400
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002401 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002402
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002403 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002404 && a2dp.audio_get_dec_config)) {
2405 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2406 return -ENOSYS;
2407 }
2408
2409 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2410 ALOGD("calling BT module stream start");
2411 /* This call indicates BT IPC lib to start capture */
2412 ret = a2dp.audio_sink_start();
2413 ALOGE("BT controller start capture return = %d",ret);
2414 if (ret != 0 ) {
2415 ALOGE("BT controller start capture failed");
2416 a2dp.a2dp_sink_started = false;
2417 } else {
2418
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002419 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002420 ALOGD("Wait for capture ready not successful");
2421 ret = -ETIMEDOUT;
2422 }
2423
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002424 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002425 a2dp.a2dp_sink_started = true;
2426 ret = 0;
2427 ALOGD("Start capture successful to BT library");
2428 } else {
2429 ALOGD(" unable to configure DSP decoder");
2430 a2dp.a2dp_sink_started = false;
2431 ret = -ETIMEDOUT;
2432 }
2433
2434 if (!a2dp_send_sink_setup_complete()) {
2435 ALOGD("sink_setup_complete not successful");
2436 ret = -ETIMEDOUT;
2437 }
2438 }
2439 }
2440
2441 if (a2dp.a2dp_sink_started) {
2442 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002443 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002444 }
2445 }
2446
2447 ALOGD("start A2DP sink total active sessions :%d",
2448 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302449 return ret;
2450}
2451
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302452static void reset_a2dp_enc_config_params()
2453{
2454 int ret =0;
2455
Aalique Grahame22e49102018-12-18 14:23:57 -08002456 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302457 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302458 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302459
2460 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
2461 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
2462 MIXER_ENC_CONFIG_BLOCK);
2463 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002464 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302465 } else {
2466 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
2467 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002468 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302469 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002470
2471 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2472
Manisha Agarwala51768b2018-11-01 16:30:52 +05302473 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2474
2475 if (!ctl_channel_mode) {
2476 ALOGE("failed to get tws mixer ctl");
2477 } else {
2478 channel_mode = "Two";
2479 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2480 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2481 }
2482 a2dp.is_tws_mono_mode_on = false;
2483 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302484}
2485
Surendar Karka2febd452018-12-13 17:56:43 +05302486static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002487{
2488 struct mixer_ctl *ctl_dec_data = NULL;
2489 struct abr_dec_cfg_t dummy_reset_cfg;
2490 int ret = 0;
2491
2492 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05302493 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002494 if (!ctl_dec_data) {
2495 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
2496 return -EINVAL;
2497 }
2498 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
2499 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
2500 sizeof(dummy_reset_cfg));
2501 if (ret != 0) {
2502 ALOGE("%s: Failed to set dummy decoder config", __func__);
2503 return ret;
2504 }
2505 }
2506
2507 return ret;
2508}
2509
Surendar Karka2febd452018-12-13 17:56:43 +05302510static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02002511{
2512 int ret =0;
2513
2514 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
2515 struct aac_dec_cfg_t dummy_reset_config;
2516
2517 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
2518 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05302519 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02002520 if (!ctl_dec_config) {
2521 ALOGE(" ERROR a2dp decoder format mixer control not identified");
2522 } else {
2523 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
2524 sizeof(struct aac_dec_cfg_t));
2525 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
2526 }
2527 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
2528 MIXER_DEC_BIT_FORMAT);
2529 if (!ctrl_bit_format) {
2530 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
2531 } else {
2532 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
2533 if (ret != 0) {
2534 ALOGE("%s: Failed to set bit format to decoder", __func__);
2535 }
2536 }
2537}
2538
Zhou Song12c29502019-03-16 10:37:18 +08002539static void reset_codec_config()
2540{
2541 reset_a2dp_enc_config_params();
2542 reset_a2dp_source_dec_config_params();
2543 a2dp_reset_backend_cfg(SOURCE);
2544 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
2545 stop_abr();
2546 a2dp.abr_config.is_abr_enabled = false;
2547}
2548
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002549int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302550{
2551 int ret =0;
2552
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002553 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002554 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002555 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302556 return -ENOSYS;
2557 }
2558
Florian Pfister1a84f312018-07-19 14:38:18 +02002559 if (a2dp.a2dp_source_total_active_session_requests > 0)
2560 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08002561 else
2562 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302563
Florian Pfister1a84f312018-07-19 14:38:18 +02002564 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302565 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02002566 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302567 if (ret < 0)
2568 ALOGE("stop stream to BT IPC lib failed");
2569 else
2570 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08002571 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
2572 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05302573 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302574 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002575 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002576 a2dp.a2dp_source_started = false;
2577 ALOGD("Stop A2DP playback, total active sessions :%d",
2578 a2dp.a2dp_source_total_active_session_requests);
2579 return 0;
2580}
2581
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002582int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002583{
2584 int ret =0;
2585
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002586 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002587 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002588 ALOGE("a2dp handle is not identified, Ignoring stop request");
2589 return -ENOSYS;
2590 }
2591
2592 if (a2dp.a2dp_sink_total_active_session_requests > 0)
2593 a2dp.a2dp_sink_total_active_session_requests--;
2594
2595 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2596 ALOGV("calling BT module stream stop");
2597 ret = a2dp.audio_sink_stop();
2598 if (ret < 0)
2599 ALOGE("stop stream to BT IPC lib failed");
2600 else
2601 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05302602 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002603 a2dp_reset_backend_cfg(SINK);
2604 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002605 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002606 a2dp.a2dp_source_started = false;
2607 ALOGD("Stop A2DP capture, total active sessions :%d",
2608 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302609 return 0;
2610}
2611
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002612int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302613{
Aalique Grahame22e49102018-12-18 14:23:57 -08002614 int ret = 0, val, status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302615 char value[32]={0};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302616 struct audio_usecase *uc_info;
2617 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302618
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002619 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002620 ALOGV("no supported encoders identified,ignoring a2dp setparam");
2621 status = -EINVAL;
2622 goto param_handled;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302623 }
2624
2625 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
2626 sizeof(value));
Zhou Song681350a2017-10-19 16:28:42 +08002627 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302628 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002629 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002630 ALOGV("Received device connect request for A2DP source");
2631 open_a2dp_source();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302632 }
2633 goto param_handled;
2634 }
2635
2636 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
2637 sizeof(value));
2638
Zhou Song681350a2017-10-19 16:28:42 +08002639 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302640 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002641 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002642 ALOGV("Received source device dis- connect request");
Samyak Jain4828f4c2018-08-24 16:31:06 +05302643 close_a2dp_output();
kunleiz4a1fad62018-02-08 18:00:16 +08002644 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302645 reset_a2dp_source_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002646 a2dp_reset_backend_cfg(SOURCE);
2647 } else if (audio_is_a2dp_in_device(val)) {
2648 ALOGV("Received sink device dis- connect request");
2649 close_a2dp_input();
Surendar Karka2febd452018-12-13 17:56:43 +05302650 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002651 a2dp_reset_backend_cfg(SINK);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302652 }
2653 goto param_handled;
2654 }
Surendar Karka2febd452018-12-13 17:56:43 +05302655#ifndef LINUX_ENABLED
Manisha Agarwala51768b2018-11-01 16:30:52 +05302656 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
2657 if (ret>=0) {
2658 ALOGD("Setting tws channel mode to %s",value);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002659 if (!(strncmp(value,"mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302660 a2dp.is_tws_mono_mode_on = true;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002661 else if (!(strncmp(value,"dual-mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302662 a2dp.is_tws_mono_mode_on = false;
2663 audio_a2dp_update_tws_channel_mode();
2664 goto param_handled;
2665 }
Surendar Karka2febd452018-12-13 17:56:43 +05302666#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302667 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
2668 if (ret >= 0) {
Surendar Karka2febd452018-12-13 17:56:43 +05302669 if (a2dp.bt_lib_source_handle) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302670 if ((!strncmp(value,"true",sizeof(value)))) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002671 if (a2dp.a2dp_source_suspended) {
2672 ALOGD("%s: A2DP is already suspended", __func__);
2673 goto param_handled;
2674 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302675 ALOGD("Setting a2dp to suspend state");
Florian Pfister1a84f312018-07-19 14:38:18 +02002676 a2dp.a2dp_source_suspended = true;
Surendar Karka2febd452018-12-13 17:56:43 +05302677 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
yidongh7203cca2018-09-19 16:12:25 +08002678 goto param_handled;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302679 list_for_each(node, &a2dp.adev->usecase_list) {
2680 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302681 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08002682 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302683 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002684 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302685 pthread_mutex_lock(&a2dp.adev->lock);
2686 }
2687 }
Zhou Song12c29502019-03-16 10:37:18 +08002688 if (!a2dp.swb_configured)
2689 reset_codec_config();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002690 if (a2dp.audio_source_suspend)
Florian Pfister1a84f312018-07-19 14:38:18 +02002691 a2dp.audio_source_suspend();
2692 } else if (a2dp.a2dp_source_suspended == true) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302693 ALOGD("Resetting a2dp suspend state");
Zhou Song10617ed2017-05-26 13:28:48 +08002694 struct audio_usecase *uc_info;
2695 struct listnode *node;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002696 if (a2dp.clear_source_a2dpsuspend_flag)
Florian Pfister1a84f312018-07-19 14:38:18 +02002697 a2dp.clear_source_a2dpsuspend_flag();
2698 a2dp.a2dp_source_suspended = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302699 /*
2700 * It is possible that before suspend,a2dp sessions can be active
2701 * for example during music + voice activation concurrency
2702 * a2dp suspend will be called & BT will change to sco mode
2703 * though music is paused as a part of voice activation
2704 * compress session close happens only after pause timeout(10secs)
2705 * so if resume request comes before pause timeout as a2dp session
2706 * is already active IPC start will not be called from APM/audio_hw
2707 * Fix is to call a2dp start for IPC library post suspend
2708 * based on number of active session count
2709 */
Florian Pfister1a84f312018-07-19 14:38:18 +02002710 if (a2dp.a2dp_source_total_active_session_requests > 0) {
Naresh Tanniru649871a2016-11-04 18:08:32 +05302711 ALOGD(" Calling IPC lib start post suspend state");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002712 if (a2dp.audio_source_start) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002713 ret = a2dp.audio_source_start();
Naresh Tanniru649871a2016-11-04 18:08:32 +05302714 if (ret != 0) {
2715 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002716 a2dp.a2dp_source_started = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302717 }
2718 }
2719 }
Zhou Song10617ed2017-05-26 13:28:48 +08002720 list_for_each(node, &a2dp.adev->usecase_list) {
2721 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302722 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08002723 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302724 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002725 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302726 pthread_mutex_lock(&a2dp.adev->lock);
2727 }
Zhou Song10617ed2017-05-26 13:28:48 +08002728 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302729 }
2730 }
2731 goto param_handled;
2732 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002733
2734 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
2735 sizeof(value));
2736 if (ret >= 0) {
2737 if (a2dp.is_a2dp_offload_supported &&
2738 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2739 *reconfig = true;
2740 }
2741 goto param_handled;
2742 }
2743
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302744param_handled:
2745 ALOGV("end of a2dp setparam");
Aalique Grahame22e49102018-12-18 14:23:57 -08002746 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302747}
2748
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002749void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302750{
2751 a2dp.is_handoff_in_progress = is_on;
2752}
2753
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002754bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302755{
2756 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05302757 // Or if a2dp device is selected but earlier start failed ( as a2dp
2758 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02002759 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302760}
2761
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002762void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302763{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302764 *sample_rate = a2dp.enc_sampling_rate;
2765}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302766
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002767void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02002768{
2769 *sample_rate = a2dp.dec_sampling_rate;
2770}
2771
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002772bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302773{
2774 bool ret = false;
2775
Florian Pfister1a84f312018-07-19 14:38:18 +02002776 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302777 return ret;
2778
Florian Pfister1a84f312018-07-19 14:38:18 +02002779 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08002780 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002781 (a2dp.audio_source_check_a2dp_ready))
2782 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302783 return ret;
2784}
2785
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002786bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02002787{
2788 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302789}
2790
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002791void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08002792 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302793{
2794 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02002795 a2dp.bt_lib_source_handle = NULL;
2796 a2dp.a2dp_source_started = false;
2797 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
2798 a2dp.a2dp_source_total_active_session_requests = 0;
2799 a2dp.a2dp_source_suspended = false;
2800 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302801 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302802 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002803 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302804 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002805 a2dp.abr_config.is_abr_enabled = false;
2806 a2dp.abr_config.abr_started = false;
2807 a2dp.abr_config.imc_instance = 0;
2808 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08002809 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302810 a2dp.is_tws_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05302811 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08002812 a2dp.swb_configured = false;
2813
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002814 // init function pointers
2815 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08002816 init_config.fp_platform_get_pcm_device_id;
2817 fp_check_a2dp_restore = init_config.fp_check_a2dp_restore;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002818
kunleiz5a127262017-09-08 14:47:48 +08002819 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302820 reset_a2dp_source_dec_config_params();
2821 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002822
2823 a2dp.bt_lib_sink_handle = NULL;
2824 a2dp.a2dp_sink_started = false;
2825 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
2826 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002827
2828 if (is_running_with_enhanced_fwk == UNINITIALIZED)
2829 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
2830 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08002831 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02002832
2833 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302834 update_offload_codec_capabilities();
2835}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002836
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002837uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002838{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002839 uint32_t latency = 0;
2840 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302841 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
2842 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002843 char value[PROPERTY_VALUE_MAX];
2844
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002845 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08002846 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002847 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302848 if (sscanf(value, "%d/%d/%d/%d/%d%d",
2849 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002850 ALOGI("Failed to parse avsync offset params from '%s'.", value);
2851 avsync_runtime_prop = 0;
2852 }
2853 }
2854
yidongh0515e042017-07-06 15:00:34 +08002855 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02002856 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2857 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08002858 }
2859
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07002860 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002861 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08002862 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
2863 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002864 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002865 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08002866 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
2867 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002868 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002869 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08002870 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
2871 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002872 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002873 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08002874 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
2875 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002876 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002877 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302878 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
2879 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
2880 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002881 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302882 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
2883 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
2884 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05302885 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05302886 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05302887 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08002888 case CODEC_TYPE_PCM:
2889 latency = ENCODER_LATENCY_PCM;
2890 latency += DEFAULT_SINK_LATENCY_PCM;
2891 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002892 default:
2893 latency = 200;
2894 break;
2895 }
2896 return latency;
2897}
Aalique Grahame22e49102018-12-18 14:23:57 -08002898
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002899int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08002900 struct str_parms *reply)
2901{
2902 int ret, val = 0;
2903 char value[32]={0};
2904
2905 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
2906 value, sizeof(value));
2907 if (ret >= 0) {
2908 val = a2dp.is_a2dp_offload_supported;
2909 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
2910 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
2911 }
2912
2913 return 0;
2914}
Zhou Song12c29502019-03-16 10:37:18 +08002915
2916
2917bool configure_aptx_ad_speech_enc_fmt() {
2918 struct mixer_ctl *ctl_enc_data = NULL;
2919 int mixer_size = 0;
2920 int ret = 0;
2921 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
2922
2923 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2924 if (!ctl_enc_data) {
2925 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2926 return false;
2927 }
2928
2929 /* Initialize dsp configuration params */
2930 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
2931 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
2932 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
2933 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
2934 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2935 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
2936 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
2937 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2938 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
2939 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
2940 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
2941
2942 /* Configure AFE DSP configuration */
2943 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
2944 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
2945 mixer_size);
2946 if (ret != 0) {
2947 ALOGE("%s: Failed to set SWB encoder config", __func__);
2948 return false;
2949 }
2950
2951 /* Configure AFE Input Bit Format as PCM_16 */
2952 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2953 if (ret != 0) {
2954 ALOGE("%s: Failed to set SWB bit format", __func__);
2955 return false;
2956 }
2957
2958 return true;
2959}
2960
2961bool configure_aptx_ad_speech_dec_fmt()
2962{
2963 struct mixer_ctl *ctl_dec_data = NULL;
2964 struct aptx_ad_speech_dec_cfg_t dec_cfg;
2965 int ret = 0;
2966
2967 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
2968 if (!ctl_dec_data) {
2969 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
2970 return false;
2971 }
2972 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
2973 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
2974 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
2975 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2976 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2977 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
2978 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
2979 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
2980
2981 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
2982 sizeof(dec_cfg));
2983 if (ret != 0) {
2984 ALOGE("%s: Failed to set decoder config", __func__);
2985 return false;
2986 }
2987 return true;
2988}
2989
2990int sco_start_configuration()
2991{
2992 ALOGD("sco_start_configuration start");
2993
2994 if (!a2dp.swb_configured) {
2995 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
2996 /* Configure AFE codec*/
2997 if (configure_aptx_ad_speech_enc_fmt() &&
2998 configure_aptx_ad_speech_dec_fmt()) {
2999 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3000 } else {
3001 ALOGE("%s: failed to send SCO configuration", __func__);
3002 return -ETIMEDOUT;
3003 }
3004 /* Configure backend*/
3005 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3006 a2dp.enc_channels = CH_MONO;
3007 a2dp.abr_config.is_abr_enabled = true;
3008 a2dp_set_backend_cfg(SOURCE);
3009 /* Start abr*/
3010 start_abr();
3011 a2dp.swb_configured = true;
3012 }
3013 return 0;
3014}
3015
3016void sco_reset_configuration()
3017{
3018 ALOGD("sco_reset_configuration start");
3019
3020 reset_codec_config();
3021 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3022 a2dp.swb_configured = false;
3023}