blob: 6db993255f5e3f067127d0f9ae3d795a48f6ac68 [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"
Zhou Song8fccbb62019-03-20 01:08:19 +080079#define MIXER_ENC_APTX_AD_CONFIG_BLOCK "SLIM_7_RX APTX_AD Enc Cfg"
Surendar Karka2febd452018-12-13 17:56:43 +053080#define MIXER_SOURCE_DEC_CONFIG_BLOCK "SLIM_7_TX Decoder Config"
81#define MIXER_SINK_DEC_CONFIG_BLOCK "SLIM_9_TX Decoder Config"
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +053082#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
Florian Pfister1a84f312018-07-19 14:38:18 +020083#define MIXER_DEC_BIT_FORMAT "AFE Output Bit Format"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053084#define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080085#define MIXER_SAMPLE_RATE_RX "BT SampleRate RX"
Surendar Karka2febd452018-12-13 17:56:43 +053086#define MIXER_SOURCE_SAMPLE_RATE_TX "BT SampleRate TX"
Samyak Jainf69e9ef2018-06-12 12:26:37 +053087#define MIXER_SAMPLE_RATE_DEFAULT "BT SampleRate"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053088#define MIXER_AFE_IN_CHANNELS "AFE Input Channels"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080089#define MIXER_ABR_TX_FEEDBACK_PATH "A2DP_SLIM7_UL_HL Switch"
Zhou Song12c29502019-03-16 10:37:18 +080090#define MIXER_ABR_RX_FEEDBACK_PATH "SCO_SLIM7_DL_HL Switch"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080091#define MIXER_SET_FEEDBACK_CHANNEL "BT set feedback channel"
Surendar Karka2febd452018-12-13 17:56:43 +053092#define MIXER_SINK_SAMPLE_RATE "BT_TX SampleRate"
Florian Pfister1a84f312018-07-19 14:38:18 +020093#define MIXER_AFE_SINK_CHANNELS "AFE Output Channels"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053094#define MIXER_ENC_FMT_SBC "SBC"
95#define MIXER_ENC_FMT_AAC "AAC"
96#define MIXER_ENC_FMT_APTX "APTX"
Manisha Agarwala51768b2018-11-01 16:30:52 +053097#define MIXER_FMT_TWS_CHANNEL_MODE "TWS Channel Mode"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053098#define MIXER_ENC_FMT_APTXHD "APTXHD"
Aalique Grahame22e49102018-12-18 14:23:57 -080099#define MIXER_END_FMT_LDAC "LDAC"
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530100#define MIXER_ENC_FMT_NONE "NONE"
yidongh0515e042017-07-06 15:00:34 +0800101#define ENCODER_LATENCY_SBC 10
102#define ENCODER_LATENCY_APTX 40
103#define ENCODER_LATENCY_APTX_HD 20
104#define ENCODER_LATENCY_AAC 70
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530105//To Do: Fine Tune Encoder CELT/LDAC latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530106#define ENCODER_LATENCY_CELT 40
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530107#define ENCODER_LATENCY_LDAC 40
Aalique Grahame22e49102018-12-18 14:23:57 -0800108#define ENCODER_LATENCY_PCM 50
yidongh0515e042017-07-06 15:00:34 +0800109#define DEFAULT_SINK_LATENCY_SBC 140
110#define DEFAULT_SINK_LATENCY_APTX 160
111#define DEFAULT_SINK_LATENCY_APTX_HD 180
112#define DEFAULT_SINK_LATENCY_AAC 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530113//To Do: Fine Tune Default CELT/LDAC Latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530114#define DEFAULT_SINK_LATENCY_CELT 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530115#define DEFAULT_SINK_LATENCY_LDAC 180
Aalique Grahame22e49102018-12-18 14:23:57 -0800116#define DEFAULT_SINK_LATENCY_PCM 140
117
118#define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported"
119#define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled"
120#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
121
122// Default encoder bit width
123#define DEFAULT_ENCODER_BIT_FORMAT 16
124
125// Default encoder latency
126#define DEFAULT_ENCODER_LATENCY 200
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530127
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800128// Slimbus Tx sample rate for ABR feedback channel
129#define ABR_TX_SAMPLE_RATE "KHZ_8"
130
Zhou Song12c29502019-03-16 10:37:18 +0800131// Slimbus Tx sample rate for APTX AD SPEECH
132#define SPEECH_TX_SAMPLE_RATE "KHZ_96"
133
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800134// Purpose ID for Inter Module Communication (IMC) in AFE
135#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
136
137// Maximum quality levels for ABR
138#define MAX_ABR_QUALITY_LEVELS 5
139
140// Instance identifier for A2DP
141#define MAX_INSTANCE_ID (UINT32_MAX / 2)
142
Zhou Song12c29502019-03-16 10:37:18 +0800143// Instance identifier for SWB
144#define APTX_AD_SPEECH_INSTANCE_ID 37
145
146#define SAMPLING_RATE_96K 96000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530147#define SAMPLING_RATE_48K 48000
148#define SAMPLING_RATE_441K 44100
Zhou Song12c29502019-03-16 10:37:18 +0800149#define SAMPLING_RATE_32K 32000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530150#define CH_STEREO 2
151#define CH_MONO 1
Florian Pfister1a84f312018-07-19 14:38:18 +0200152#define SOURCE 0
153#define SINK 1
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800154#define UNINITIALIZED -1
155
156#ifdef __LP64__
157#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
158#else
159#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
160#endif
161
162static void *vndk_fwk_lib_handle = NULL;
163static int is_running_with_enhanced_fwk = UNINITIALIZED;
164
165typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
166static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
Florian Pfister1a84f312018-07-19 14:38:18 +0200167
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530168/*
169 * Below enum values are extended from audio_base.h to
Florian Pfister1a84f312018-07-19 14:38:18 +0200170 * to keep encoder and decoder type local to bthost_ipc
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530171 * and audio_hal as these are intended only for handshake
172 * between IPC lib and Audio HAL.
173 */
174typedef enum {
Florian Pfister1a84f312018-07-19 14:38:18 +0200175 CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
176 CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
177 CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
178 CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
179 CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530180#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +0200181 CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530182#endif
Florian Pfister1a84f312018-07-19 14:38:18 +0200183 CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
184 CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
Surendar Karka2febd452018-12-13 17:56:43 +0530185 CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
Zhou Song12c29502019-03-16 10:37:18 +0800186 CODEC_TYPE_APTX_AD_SPEECH = 637534208u, //0x26000000UL
Aalique Grahame22e49102018-12-18 14:23:57 -0800187 CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u
Florian Pfister1a84f312018-07-19 14:38:18 +0200188}codec_t;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530189
Sharad Sangle95d451b2018-06-19 12:24:20 +0530190/*
191 * enums which describes the APTX Adaptive
192 * channel mode, these values are used by encoder
193 */
194 typedef enum {
195 APTX_AD_CHANNEL_UNCHANGED = -1,
196 APTX_AD_CHANNEL_JOINT_STEREO = 0, // default
197 APTX_AD_CHANNEL_MONO = 1,
198 APTX_AD_CHANNEL_DUAL_MONO = 2,
199 APTX_AD_CHANNEL_STEREO_TWS = 4,
200 APTX_AD_CHANNEL_EARBUD = 8,
201} enc_aptx_ad_channel_mode;
202
203/*
204 * enums which describes the APTX Adaptive
205 * sampling frequency, these values are used
206 * by encoder
207 */
208typedef enum {
209 APTX_AD_SR_UNCHANGED = 0x0,
210 APTX_AD_48 = 0x1, // 48 KHz default
211 APTX_AD_44_1 = 0x2, // 44.1kHz
212} enc_aptx_ad_s_rate;
213
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530214typedef void (*bt_audio_pre_init_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200215typedef int (*audio_source_open_t)(void);
216typedef int (*audio_source_close_t)(void);
217typedef int (*audio_source_start_t)(void);
218typedef int (*audio_source_stop_t)(void);
219typedef int (*audio_source_suspend_t)(void);
220typedef void (*audio_source_handoff_triggered_t)(void);
221typedef void (*clear_source_a2dpsuspend_flag_t)(void);
222typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
223 uint8_t *num_dev, codec_t *codec_type);
224typedef int (*audio_source_check_a2dp_ready_t)(void);
225typedef int (*audio_is_source_scrambling_enabled_t)(void);
Manisha Agarwala51768b2018-11-01 16:30:52 +0530226typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200227typedef int (*audio_sink_start_t)(void);
228typedef int (*audio_sink_stop_t)(void);
229typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
230typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
231typedef int (*audio_sink_check_a2dp_ready_t)(void);
232typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530233
234enum A2DP_STATE {
235 A2DP_STATE_CONNECTED,
236 A2DP_STATE_STARTED,
237 A2DP_STATE_STOPPED,
238 A2DP_STATE_DISCONNECTED,
239};
240
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800241typedef enum {
242 IMC_TRANSMIT,
243 IMC_RECEIVE,
244} imc_direction_t;
245
246typedef enum {
247 IMC_DISABLE,
248 IMC_ENABLE,
249} imc_status_t;
250
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700251typedef enum {
Zhou Song12c29502019-03-16 10:37:18 +0800252 SWAP_DISABLE,
253 SWAP_ENABLE,
254} swap_status_t;
255
256typedef enum {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700257 MTU_SIZE,
258 PEAK_BIT_RATE,
259} frame_control_type_t;
260
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800261// --- external function dependency ---
262fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
263fp_check_a2dp_restore_t fp_check_a2dp_restore;
264
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800265/* PCM config for ABR Feedback hostless front end */
266static struct pcm_config pcm_config_abr = {
267 .channels = 1,
268 .rate = 8000,
269 .period_size = 240,
270 .period_count = 2,
271 .format = PCM_FORMAT_S16_LE,
272 .start_threshold = 0,
273 .stop_threshold = INT_MAX,
274 .avail_min = 0,
275};
276
277/* Adaptive bitrate config for A2DP codecs */
278struct a2dp_abr_config {
279 /* Flag to denote whether Adaptive bitrate is enabled for codec */
280 bool is_abr_enabled;
281 /* Flag to denote whether front end has been opened for ABR */
282 bool abr_started;
283 /* ABR Tx path pcm handle */
284 struct pcm *abr_tx_handle;
Zhou Song12c29502019-03-16 10:37:18 +0800285 /* ABR Rx path pcm handle */
286 struct pcm *abr_rx_handle;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800287 /* ABR Inter Module Communication (IMC) instance ID */
288 uint32_t imc_instance;
289};
290
291static uint32_t instance_id = MAX_INSTANCE_ID;
292
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530293/* structure used to update a2dp state machine
294 * to communicate IPC library
295 * to store DSP encoder configuration information
296 */
297struct a2dp_data {
298 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200299 void *bt_lib_source_handle;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530300 bt_audio_pre_init_t bt_audio_pre_init;
Florian Pfister1a84f312018-07-19 14:38:18 +0200301 audio_source_open_t audio_source_open;
302 audio_source_close_t audio_source_close;
303 audio_source_start_t audio_source_start;
304 audio_source_stop_t audio_source_stop;
305 audio_source_suspend_t audio_source_suspend;
306 audio_source_handoff_triggered_t audio_source_handoff_triggered;
307 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
308 audio_get_enc_config_t audio_get_enc_config;
309 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530310 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200311 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
312 enum A2DP_STATE bt_state_source;
313 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530314 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530315 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200316 bool a2dp_source_started;
317 bool a2dp_source_suspended;
318 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530319 bool is_a2dp_offload_supported;
320 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700321 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530322 /* Mono Mode support for TWS+ */
323 bool is_tws_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530324 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800325 /* Adaptive bitrate config for A2DP codecs */
326 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200327
328 void *bt_lib_sink_handle;
329 audio_sink_start_t audio_sink_start;
330 audio_sink_stop_t audio_sink_stop;
331 audio_get_dec_config_t audio_get_dec_config;
332 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
333 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
334 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
335 enum A2DP_STATE bt_state_sink;
336 codec_t bt_decoder_format;
337 uint32_t dec_sampling_rate;
338 uint32_t dec_channels;
339 bool a2dp_sink_started;
340 int a2dp_sink_total_active_session_requests;
Zhou Song12c29502019-03-16 10:37:18 +0800341 bool swb_configured;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530342};
343
344struct a2dp_data a2dp;
345
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800346/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
347 * Structures sent to configure DSP for ABR are defined below.
348 * This data helps DSP configure feedback path (BTSoC to LPASS)
349 * for link quality levels and mapping quality levels to codec
350 * specific bitrate.
351 */
352
353/* Key value pair for link quality level to bitrate mapping. */
354struct bit_rate_level_map_t {
355 uint32_t link_quality_level;
356 uint32_t bitrate;
357};
358
359/* Link quality level to bitrate mapping info sent to DSP. */
360struct quality_level_to_bitrate_info {
361 /* Number of quality levels being mapped.
362 * This will be equal to the size of mapping table.
363 */
364 uint32_t num_levels;
365 /* Quality level to bitrate mapping table */
366 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
367};
368
369/* Structure to set up Inter Module Communication (IMC) between
370 * AFE Decoder and Encoder.
371 */
372struct imc_dec_enc_info {
373 /* Decoder to encoder communication direction.
374 * Transmit = 0 / Receive = 1
375 */
376 uint32_t direction;
377 /* Enable / disable IMC between decoder and encoder */
378 uint32_t enable;
379 /* Purpose of IMC being set up between decoder and encoder.
380 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
381 * is the default value to be sent as purpose.
382 */
383 uint32_t purpose;
384 /* Unique communication instance ID.
385 * purpose and comm_instance together form the actual key
386 * used in IMC registration, which must be the same for
387 * encoder and decoder for which IMC is being set up.
388 */
389 uint32_t comm_instance;
390};
391
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700392/* Structure to control frame size of AAC encoded frames. */
393struct aac_frame_size_control_t {
394 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
395 uint32_t ctl_type;
396 /* Control value
397 * MTU_SIZE: MTU size in bytes
398 * PEAK_BIT_RATE: Peak bitrate in bits per second.
399 */
400 uint32_t ctl_value;
401};
402
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800403/* Structure used for ABR config of AFE encoder and decoder. */
404struct abr_enc_cfg_t {
405 /* Link quality level to bitrate mapping info sent to DSP. */
406 struct quality_level_to_bitrate_info mapping_info;
407 /* Information to set up IMC between decoder and encoder */
408 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700409 /* Flag to indicate whether ABR is enabled */
410 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800411} __attribute__ ((packed));
412
413/* Structure to send configuration for decoder introduced
414 * on AFE Tx path for ABR link quality feedback to BT encoder.
415 */
416struct abr_dec_cfg_t {
417 /* Decoder media format */
418 uint32_t dec_format;
419 /* Information to set up IMC between decoder and encoder */
420 struct imc_dec_enc_info imc_info;
421} __attribute__ ((packed));
422
Zhou Song12c29502019-03-16 10:37:18 +0800423struct aptx_ad_speech_mode_cfg_t
424{
425 uint32_t mode;
426 uint32_t swapping;
427} __attribute__ ((packed));
428
429/* Structure for SWB voice dec config */
430struct aptx_ad_speech_dec_cfg_t {
431 struct abr_dec_cfg_t abr_cfg;
432 struct aptx_ad_speech_mode_cfg_t speech_mode;
433} __attribute__ ((packed));
434
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530435/* START of DSP configurable structures
436 * These values should match with DSP interface defintion
437 */
438
439/* AAC encoder configuration structure. */
440typedef struct aac_enc_cfg_t aac_enc_cfg_t;
441
442/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
443 * supported aac_fmt_flag are ADTS/RAW
444 * supported channel_cfg are Native mode, Mono , Stereo
445 */
446struct aac_enc_cfg_t {
447 uint32_t enc_format;
448 uint32_t bit_rate;
449 uint32_t enc_mode;
450 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530451 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530452 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530453} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530454
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700455struct aac_enc_cfg_v2_t {
456 struct aac_enc_cfg_t aac_enc_cfg;
457 struct aac_frame_size_control_t frame_ctl;
458} __attribute__ ((packed));
459
Surendar Karkabbb3c822018-11-12 13:00:38 +0530460typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
461struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200462 uint16_t aac_fmt_flag; /* LATM*/
463 uint16_t audio_object_type; /* LC */
464 uint16_t channels; /* Stereo */
465 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
466 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
467 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530468} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200469
Surendar Karkabbb3c822018-11-12 13:00:38 +0530470typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
471struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200472 uint16_t channels; /* Mono, Stereo */
473 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
474 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530475} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200476
477/* AAC decoder configuration structure. */
478typedef struct aac_dec_cfg_t aac_dec_cfg_t;
479struct aac_dec_cfg_t {
480 uint32_t dec_format;
481 audio_aac_decoder_config_t data;
482} __attribute__ ((packed));
483
484/* SBC decoder configuration structure. */
485typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
486struct sbc_dec_cfg_t {
487 uint32_t dec_format;
488 audio_sbc_decoder_config_t data;
489} __attribute__ ((packed));
490
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530491/* SBC encoder configuration structure. */
492typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
493
494/* supported num_subbands are 4/8
495 * supported blk_len are 4, 8, 12, 16
496 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
497 * supported alloc_method are LOUNDNESS/SNR
498 * supported bit_rate for mono channel is max 320kbps
499 * supported bit rate for stereo channel is max 512 kbps
500 */
501struct sbc_enc_cfg_t{
502 uint32_t enc_format;
503 uint32_t num_subbands;
504 uint32_t blk_len;
505 uint32_t channel_mode;
506 uint32_t alloc_method;
507 uint32_t bit_rate;
508 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530509} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530510
511
512/* supported num_channels are Mono/Stereo
513 * supported channel_mapping for mono is CHANNEL_C
514 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
515 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700516 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530517struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530518{
519 uint32_t enc_format;
520 uint32_t sample_rate;
521 uint16_t num_channels;
522 uint16_t reserved;
523 uint8_t channel_mapping[8];
524 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530525} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530526
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530527struct celt_specific_enc_cfg_t
528{
529 uint32_t bit_rate;
530 uint16_t frame_size;
531 uint16_t complexity;
532 uint16_t prediction_mode;
533 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530534} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530535
536struct celt_enc_cfg_t
537{
538 struct custom_enc_cfg_t custom_cfg;
539 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530540} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700541
542/* sync_mode introduced with APTX V2 libraries
543 * sync mode: 0x0 = stereo sync mode
544 * 0x01 = dual mono sync mode
545 * 0x02 = dual mono with no sync on either L or R codewords
546 */
547struct aptx_v2_enc_cfg_ext_t
548{
549 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530550} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700551
552/* APTX struct for combining custom enc and V2 fields */
553struct aptx_enc_cfg_t
554{
555 struct custom_enc_cfg_t custom_cfg;
556 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530557} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700558
Sharad Sangle95d451b2018-06-19 12:24:20 +0530559/* APTX AD structure */
560struct aptx_ad_enc_cfg_ext_t
561{
562 uint32_t sampling_freq;
563 uint32_t mtu;
564 uint32_t channel_mode;
565 uint32_t min_sink_modeA;
566 uint32_t max_sink_modeA;
567 uint32_t min_sink_modeB;
568 uint32_t max_sink_modeB;
569 uint32_t min_sink_modeC;
570 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530571 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530572} __attribute__ ((packed));
573
574struct aptx_ad_enc_cfg_t
575{
576 struct custom_enc_cfg_t custom_cfg;
577 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
578 struct abr_enc_cfg_t abr_cfg;
579} __attribute__ ((packed));
580
Zhou Song8fccbb62019-03-20 01:08:19 +0800581struct aptx_ad_enc_cfg_ext_r2_t
582{
583 uint32_t sampling_freq;
584 uint32_t mtu;
585 uint32_t channel_mode;
586 uint32_t min_sink_modeA;
587 uint32_t max_sink_modeA;
588 uint32_t min_sink_modeB;
589 uint32_t max_sink_modeB;
590 uint32_t min_sink_modeC;
591 uint32_t max_sink_modeC;
592 uint32_t mode;
593 uint32_t input_mode;
594 uint32_t fade_duration;
595 uint8_t sink_cap[11];
596} __attribute__ ((packed));
597
598struct aptx_ad_enc_cfg_r2_t
599{
600 struct custom_enc_cfg_t custom_cfg;
601 struct aptx_ad_enc_cfg_ext_r2_t aptx_ad_cfg;
602 struct abr_enc_cfg_t abr_cfg;
603} __attribute__ ((packed));
604
Zhou Song12c29502019-03-16 10:37:18 +0800605/* APTX AD SPEECH structure */
606struct aptx_ad_speech_enc_cfg_t
607{
608 struct custom_enc_cfg_t custom_cfg;
609 /* Information to set up IMC between decoder and encoder */
610 struct imc_dec_enc_info imc_info;
611 struct aptx_ad_speech_mode_cfg_t speech_mode;
612} __attribute__ ((packed));
613
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530614struct ldac_specific_enc_cfg_t
615{
616 uint32_t bit_rate;
617 uint16_t channel_mode;
618 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530619} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530620
621struct ldac_enc_cfg_t
622{
623 struct custom_enc_cfg_t custom_cfg;
624 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800625 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530626} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530627
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530628/* In LE BT source code uses system/audio.h for below
629 * structure definition. To avoid multiple definition
630 * compilation error for audiohal in LE , masking structure
631 * definition under "LINUX_ENABLED" which is defined only
632 * in LE
633 */
634#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530635/* TODO: Define the following structures only for O using PLATFORM_VERSION */
636/* Information about BT SBC encoder configuration
637 * This data is used between audio HAL module and
638 * BT IPC library to configure DSP encoder
639 */
640typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530641 uint32_t subband; /* 4, 8 */
642 uint32_t blk_len; /* 4, 8, 12, 16 */
643 uint16_t sampling_rate; /*44.1khz,48khz*/
644 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
645 uint8_t alloc; /*0(Loudness),1(SNR)*/
646 uint8_t min_bitpool; /* 2 */
647 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
648 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530649 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530650} audio_sbc_encoder_config;
651
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530652/* Information about BT APTX encoder configuration
653 * This data is used between audio HAL module and
654 * BT IPC library to configure DSP encoder
655 */
656typedef struct {
657 uint16_t sampling_rate;
658 uint8_t channels;
659 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530660 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700661} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530662
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700663typedef struct {
Zhou Song8fccbb62019-03-20 01:08:19 +0800664 uint32_t sampling_rate;
665 uint32_t mtu;
666 int32_t channel_mode;
667 uint32_t min_sink_modeA;
668 uint32_t max_sink_modeA;
669 uint32_t min_sink_modeB;
670 uint32_t max_sink_modeB;
671 uint32_t min_sink_modeC;
672 uint32_t max_sink_modeC;
673 uint32_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530674 uint8_t TTP_modeA_low;
675 uint8_t TTP_modeA_high;
676 uint8_t TTP_modeB_low;
677 uint8_t TTP_modeB_high;
Zhou Song8fccbb62019-03-20 01:08:19 +0800678 uint8_t TTP_TWS_low;
679 uint8_t TTP_TWS_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530680 uint32_t bits_per_sample;
Zhou Song8fccbb62019-03-20 01:08:19 +0800681 uint32_t input_mode;
682 uint32_t fade_duration;
683 uint8_t sink_cap[11];
Sharad Sangle95d451b2018-06-19 12:24:20 +0530684} audio_aptx_ad_config;
685
686typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700687 uint16_t sampling_rate;
688 uint8_t channels;
689 uint32_t bitrate;
690 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530691 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700692} audio_aptx_dual_mono_config;
693
694typedef union {
695 audio_aptx_default_config *default_cfg;
696 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530697 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700698} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530699
700/* Information about BT AAC encoder configuration
701 * This data is used between audio HAL module and
702 * BT IPC library to configure DSP encoder
703 */
704typedef struct {
705 uint32_t enc_mode; /* LC, SBR, PS */
706 uint16_t format_flag; /* RAW, ADTS */
707 uint16_t channels; /* 1-Mono, 2-Stereo */
708 uint32_t sampling_rate;
709 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530710 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530711} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530712#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700713
714typedef struct {
715 audio_aac_encoder_config audio_aac_enc_cfg;
716 struct aac_frame_size_control_t frame_ctl;
717} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530718
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530719/* Information about BT CELT encoder configuration
720 * This data is used between audio HAL module and
721 * BT IPC library to configure DSP encoder
722 */
723typedef struct {
724 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
725 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
726 uint16_t frame_size; /* 64-128-256-512, 512 */
727 uint16_t complexity; /* 0-10, 1 */
728 uint16_t prediction_mode; /* 0-1-2, 0 */
729 uint16_t vbr_flag; /* 0-1, 0*/
730 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530731 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530732} audio_celt_encoder_config;
733
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530734/* Information about BT LDAC encoder configuration
735 * This data is used between audio HAL module and
736 * BT IPC library to configure DSP encoder
737 */
738typedef struct {
739 uint32_t sampling_rate; /*44100,48000,88200,96000*/
740 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
741 uint16_t channel_mode; /* 0, 4, 2, 1*/
742 uint16_t mtu; /*679*/
Aniket Kumar Latad0196282019-05-09 14:24:17 -0700743 uint32_t bits_per_sample;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800744 bool is_abr_enabled;
745 struct quality_level_to_bitrate_info level_to_bitrate_map;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530746} audio_ldac_encoder_config;
747
Surendar Karkabbb3c822018-11-12 13:00:38 +0530748/* Information about BT AAC decoder configuration
749 * This data is used between audio HAL module and
750 * BT IPC library to configure DSP decoder
751 */
752typedef struct {
753 uint16_t aac_fmt_flag; /* LATM*/
754 uint16_t audio_object_type; /* LC */
755 uint16_t channels; /* Stereo */
756 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
757 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
758 44.1k, 48k, 64k, 88.2k, 96k */
759} audio_aac_dec_config_t;
760
761/* Information about BT SBC decoder configuration
762 * This data is used between audio HAL module and
763 * BT IPC library to configure DSP decoder
764 */
765typedef struct {
766 uint16_t channels; /* Mono, Stereo */
767 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
768 44.1k, 48k, 64k, 88.2k, 96k */
769}audio_sbc_dec_config_t;
770
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530771/*********** END of DSP configurable structures ********************/
772
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530773static void update_offload_codec_capabilities()
774{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530775
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530776 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800777 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
778 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
779
780 ALOGD("%s: A2DP offload supported = %d",__func__,
781 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530782}
783
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800784static int stop_abr()
785{
786 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800787 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800788 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800789 int ret = 0;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800790
791 /* This function can be used if !abr_started for clean up */
792 ALOGV("%s: enter", __func__);
793
794 // Close hostless front end
795 if (a2dp.abr_config.abr_tx_handle != NULL) {
796 pcm_close(a2dp.abr_config.abr_tx_handle);
797 a2dp.abr_config.abr_tx_handle = NULL;
798 }
Zhou Song12c29502019-03-16 10:37:18 +0800799 if (a2dp.abr_config.abr_rx_handle != NULL) {
800 pcm_close(a2dp.abr_config.abr_rx_handle);
801 a2dp.abr_config.abr_rx_handle = NULL;
802 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800803 a2dp.abr_config.abr_started = false;
804 a2dp.abr_config.imc_instance = 0;
805
806 // Reset BT driver mixer control for ABR usecase
807 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
808 MIXER_SET_FEEDBACK_CHANNEL);
809 if (!ctl_set_bt_feedback_channel) {
810 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800811 ret = -ENOSYS;
812 } else if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800813 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800814 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800815 }
816
817 // Reset ABR Tx feedback path
818 ALOGV("%s: Disable ABR Tx feedback path", __func__);
819 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
820 MIXER_ABR_TX_FEEDBACK_PATH);
821 if (!ctl_abr_tx_path) {
822 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800823 ret = -ENOSYS;
824 } else if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800825 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800826 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800827 }
828
Zhou Song12c29502019-03-16 10:37:18 +0800829 // Reset ABR Rx feedback path
830 ALOGV("%s: Disable ABR Rx feedback path", __func__);
831 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
832 MIXER_ABR_RX_FEEDBACK_PATH);
833 if (!ctl_abr_rx_path) {
834 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
835 ret = -ENOSYS;
836 } else if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 0) != 0) {
837 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
838 ret = -ENOSYS;
839 }
840
841 return ret;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800842}
843
844static int start_abr()
845{
846 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800847 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800848 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
849 int abr_device_id;
850 int ret = 0;
851
852 if (!a2dp.abr_config.is_abr_enabled) {
853 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
854 return -ENOSYS;
855 }
856
857 if (a2dp.abr_config.abr_started) {
858 ALOGI("%s: ABR has already started", __func__);
859 return ret;
860 }
861
862 // Enable Slimbus 7 Tx feedback path
863 ALOGV("%s: Enable ABR Tx feedback path", __func__);
864 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
865 MIXER_ABR_TX_FEEDBACK_PATH);
866 if (!ctl_abr_tx_path) {
867 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
868 return -ENOSYS;
869 }
870 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
871 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
872 return -ENOSYS;
873 }
874
875 // Notify ABR usecase information to BT driver to distinguish
876 // between SCO and feedback usecase
877 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
878 MIXER_SET_FEEDBACK_CHANNEL);
879 if (!ctl_set_bt_feedback_channel) {
880 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800881 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800882 }
883 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
884 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800885 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800886 }
887
888 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800889 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800890 PCM_CAPTURE);
891 if (!a2dp.abr_config.abr_tx_handle) {
892 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
893 abr_device_id, PCM_IN,
894 &pcm_config_abr);
Zhou Song12c29502019-03-16 10:37:18 +0800895 if (a2dp.abr_config.abr_tx_handle == NULL) {
896 ALOGE("%s: Can't open abr tx device", __func__);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800897 goto fail;
Zhou Song12c29502019-03-16 10:37:18 +0800898 }
899 if (!(pcm_is_ready(a2dp.abr_config.abr_tx_handle) &&
900 !pcm_start(a2dp.abr_config.abr_tx_handle))) {
901 ALOGE("%s: tx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
902 goto fail;
903 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800904 }
Zhou Song12c29502019-03-16 10:37:18 +0800905
906 // Enable Slimbus 7 Rx feedback path for HD Voice use case
907 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH) {
908 ALOGV("%s: Enable ABR Rx feedback path", __func__);
909 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
910 MIXER_ABR_RX_FEEDBACK_PATH);
911 if (!ctl_abr_rx_path) {
912 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
913 goto fail;
914 }
915 if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 1) != 0) {
916 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
917 goto fail;
918 }
919
920 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
921 ALOGE("%s: Failed to set BT usecase", __func__);
922 goto fail;
923 }
924
925 // Open hostless front end and prepare ABR Rx path
926 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
927 PCM_PLAYBACK);
928 if (!a2dp.abr_config.abr_rx_handle) {
929 a2dp.abr_config.abr_rx_handle = pcm_open(a2dp.adev->snd_card,
930 abr_device_id, PCM_OUT,
931 &pcm_config_abr);
932 if (a2dp.abr_config.abr_rx_handle == NULL) {
933 ALOGE("%s: Can't open abr rx device", __func__);
934 goto fail;
935 }
936 if (!(pcm_is_ready(a2dp.abr_config.abr_rx_handle) &&
937 !pcm_start(a2dp.abr_config.abr_rx_handle))) {
938 ALOGE("%s: rx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_rx_handle));
939 goto fail;
940 }
941 }
942 }
943
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800944 a2dp.abr_config.abr_started = true;
945
946 return ret;
947
948fail:
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800949 stop_abr();
950 return -ENOSYS;
951}
952
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800953static int check_if_enhanced_fwk() {
954
955 int is_enhanced_fwk = 1;
956 //dlopen lib
957 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
958 if (vndk_fwk_lib_handle != NULL) {
959 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
960 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
961 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
962 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
963 __func__);
964 is_enhanced_fwk = 1;
965 } else {
966 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
967 }
968 }
969 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
970 return is_enhanced_fwk;
971}
972
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530973static void open_a2dp_source() {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530974 int ret = 0;
975
Florian Pfister1a84f312018-07-19 14:38:18 +0200976 ALOGD(" Open A2DP source start ");
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530977
978 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
979 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
980 ALOGD("calling BT stream open");
981 ret = a2dp.audio_source_open();
982 if(ret != 0) {
983 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Srinu Jellada99a592019-01-25 16:50:52 +0530984 }
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530985 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530986 } else {
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530987 ALOGD("Called a2dp open with improper state %d", a2dp.bt_state_source);
988 }
989 } else {
990 ALOGE("a2dp handle is not identified, Ignoring open request");
991 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
992 }
993}
994/* API to open BT IPC library to start IPC communication for BT Source*/
995static void a2dp_source_init()
996{
997 ALOGD("a2dp_source_init START");
998 if (a2dp.bt_lib_source_handle == NULL) {
999 ALOGD("Requesting for BT lib handle");
1000 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
1001 if (a2dp.bt_lib_source_handle == NULL) {
1002 ALOGE("%s: dlopen failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
1003 return;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301004 }
1005 }
1006
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301007 a2dp.bt_audio_pre_init = (bt_audio_pre_init_t)
1008 dlsym(a2dp.bt_lib_source_handle, "bt_audio_pre_init");
Srinu Jellada99a592019-01-25 16:50:52 +05301009 a2dp.audio_source_open = (audio_source_open_t)
1010 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
1011 a2dp.audio_source_start = (audio_source_start_t)
1012 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
1013 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
1014 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
1015 a2dp.audio_source_suspend = (audio_source_suspend_t)
1016 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
1017 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
1018 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
1019 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1020 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
1021 a2dp.audio_source_stop = (audio_source_stop_t)
1022 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
1023 a2dp.audio_source_close = (audio_source_close_t)
1024 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
1025 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
1026 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
1027 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
1028 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
1029 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1030 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1031 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1032 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1033
Srinu Jella793f5302019-07-19 15:53:55 +05301034 if (a2dp.bt_lib_source_handle && a2dp.bt_audio_pre_init) {
1035 ALOGD("calling BT module preinit");
1036 // fwk related check's will be done in the BT layer
1037 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001038 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001039}
1040
1041/* API to open BT IPC library to start IPC communication for BT Sink*/
1042static void open_a2dp_sink()
1043{
1044 ALOGD(" Open A2DP input start ");
1045 if (a2dp.bt_lib_sink_handle == NULL){
1046 ALOGD(" Requesting for BT lib handle");
1047 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1048
1049 if (a2dp.bt_lib_sink_handle == NULL) {
1050 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1051 } else {
1052 a2dp.audio_sink_start = (audio_sink_start_t)
1053 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1054 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1055 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1056 a2dp.audio_sink_stop = (audio_sink_stop_t)
1057 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1058 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1059 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1060 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1061 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1062 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301063 }
1064}
1065
1066static int close_a2dp_output()
1067{
1068 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001069
1070 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1071 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301072 return -ENOSYS;
1073 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001074
1075 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1076 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001077 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001078 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301079 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001080 a2dp.a2dp_source_started = false;
1081 a2dp.a2dp_source_total_active_session_requests = 0;
1082 a2dp.a2dp_source_suspended = false;
1083 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301084 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301085 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001086 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001087 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1088 stop_abr();
1089 a2dp.abr_config.is_abr_enabled = false;
1090 a2dp.abr_config.abr_started = false;
1091 a2dp.abr_config.imc_instance = 0;
1092 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001093 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301094 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001095
1096 return 0;
1097}
1098
1099static int close_a2dp_input()
1100{
1101 ALOGV("%s\n",__func__);
1102
1103 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1104 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1105 return -ENOSYS;
1106 }
1107
1108 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1109 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001110 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001111 ALOGE("failed close a2dp sink control path from BT library");
1112 }
1113 a2dp.a2dp_sink_started = false;
1114 a2dp.a2dp_sink_total_active_session_requests = 0;
1115 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1116 a2dp.dec_sampling_rate = 48000;
1117 a2dp.dec_channels = 2;
1118 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301119
1120 return 0;
1121}
1122
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301123static void a2dp_check_and_set_scrambler()
1124{
1125 bool scrambler_mode = false;
1126 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001127 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1128 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301129
1130 if (scrambler_mode) {
1131 //enable scrambler in dsp
1132 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1133 MIXER_SCRAMBLER_MODE);
1134 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001135 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301136 return;
1137 } else {
1138 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1139 ALOGE("%s: Could not set scrambler mode", __func__);
1140 return;
1141 }
1142 }
1143 }
1144}
1145
Florian Pfister1a84f312018-07-19 14:38:18 +02001146static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301147{
Florian Pfister1a84f312018-07-19 14:38:18 +02001148 char *rate_str = NULL, *channels = NULL;
1149 uint32_t sampling_rate;
1150 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1151 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301152
Florian Pfister1a84f312018-07-19 14:38:18 +02001153 if (direction == SINK) {
1154 sampling_rate = a2dp.dec_sampling_rate;
1155 } else {
1156 sampling_rate = a2dp.enc_sampling_rate;
1157 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001158 /*
1159 * For LDAC encoder and AAC decoder open slimbus port at
1160 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1161 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1162 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001163 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301164 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001165 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1166 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301167 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1168 sampling_rate = sampling_rate *2;
1169 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001170
Aalique Grahame22e49102018-12-18 14:23:57 -08001171 // No need to configure backend for PCM format.
1172 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1173 return 0;
1174 }
1175
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301176 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301177 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301178 case 44100:
1179 rate_str = "KHZ_44P1";
1180 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301181 case 88200:
1182 rate_str = "KHZ_88P2";
1183 break;
1184 case 96000:
1185 rate_str = "KHZ_96";
1186 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001187 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301188 default:
1189 rate_str = "KHZ_48";
1190 break;
1191 }
1192
Florian Pfister1a84f312018-07-19 14:38:18 +02001193 if (direction == SINK) {
1194 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1195 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301196 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001197 } else {
1198 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1199 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301200 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001201 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301202 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001203
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301204 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1205 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301206 is_configured = false;
1207 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301208 }
1209
Surendar Karka2febd452018-12-13 17:56:43 +05301210 if (direction == SOURCE) {
1211 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001212 if (a2dp.abr_config.is_abr_enabled) {
1213 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH)
1214 rate_str = SPEECH_TX_SAMPLE_RATE;
1215 else
1216 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301217
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301218 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1219 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1220 MIXER_SOURCE_SAMPLE_RATE_TX);
1221 if (!ctl_sample_rate) {
1222 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1223 is_configured = false;
1224 goto fail;
1225 }
1226
1227 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1228 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1229 is_configured = false;
1230 goto fail;
1231 }
Surendar Karka2febd452018-12-13 17:56:43 +05301232 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301233 }
1234 } else {
1235 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1236 MIXER_SAMPLE_RATE_TX is not supported */
1237 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1238 MIXER_SAMPLE_RATE_DEFAULT);
1239 if (!ctl_sample_rate) {
1240 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301241 is_configured = false;
1242 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301243 }
1244
1245 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1246 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001247 is_configured = false;
1248 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301249 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301250 }
1251
Florian Pfister1a84f312018-07-19 14:38:18 +02001252 if (direction == SINK) {
1253 switch (a2dp.dec_channels) {
1254 case 1:
1255 channels = "One";
1256 break;
1257 case 2:
1258 default:
1259 channels = "Two";
1260 break;
1261 }
1262
Ramu Gottipati02809682018-12-19 16:46:12 +05301263 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001264 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1265 MIXER_AFE_SINK_CHANNELS);
1266 } else {
1267 //Configure AFE enc channels
1268 switch (a2dp.enc_channels) {
1269 case 1:
1270 channels = "One";
1271 break;
1272 case 2:
1273 default:
1274 channels = "Two";
1275 break;
1276 }
1277
Ramu Gottipati02809682018-12-19 16:46:12 +05301278 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001279 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1280 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301281 }
1282
Florian Pfister1a84f312018-07-19 14:38:18 +02001283 if (!ctrl_channels) {
1284 ALOGE(" ERROR AFE channels mixer control not identified");
1285 } else {
1286 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301287 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001288 is_configured = false;
1289 goto fail;
1290 }
1291 }
1292 is_configured = true;
1293fail:
1294 return is_configured;
1295}
1296
Surendar Karkabbb3c822018-11-12 13:00:38 +05301297bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001298{
1299 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1300 struct aac_dec_cfg_t aac_dsp_cfg;
1301 bool is_configured = false;
1302 int ret = 0;
1303
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001304 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001305 return false;
1306
Surendar Karka2febd452018-12-13 17:56:43 +05301307 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001308 if (!ctl_dec_data) {
1309 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1310 is_configured = false;
1311 goto fail;
1312 }
1313
1314 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1315 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1316 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1317 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1318 switch(aac_bt_cfg->audio_object_type) {
1319 case 0:
1320 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301321 break;
1322 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001323 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1324 break;
1325 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301326 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001327 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301328 break;
1329 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001330 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1331 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1332 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1333 sizeof(struct aac_dec_cfg_t));
1334 if (ret != 0) {
1335 ALOGE("%s: failed to set AAC decoder config", __func__);
1336 is_configured = false;
1337 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001338 }
1339
Florian Pfister1a84f312018-07-19 14:38:18 +02001340 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1341 MIXER_DEC_BIT_FORMAT);
1342 if (!ctrl_bit_format) {
1343 ALOGE(" ERROR Dec bit format mixer control not identified");
1344 is_configured = false;
1345 goto fail;
1346 }
1347 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1348 if (ret != 0) {
1349 ALOGE("%s: Failed to set bit format to decoder", __func__);
1350 is_configured = false;
1351 goto fail;
1352 }
1353
1354 is_configured = true;
1355 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1356 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1357 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1358 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1359 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1360fail:
1361 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301362}
1363
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301364static int a2dp_set_bit_format(uint32_t enc_bit_format)
1365{
1366 const char *bit_format = NULL;
1367 struct mixer_ctl *ctrl_bit_format = NULL;
1368
1369 // Configure AFE Input Bit Format
1370 switch (enc_bit_format) {
1371 case 32:
1372 bit_format = "S32_LE";
1373 break;
1374 case 24:
1375 bit_format = "S24_LE";
1376 break;
1377 case 16:
1378 default:
1379 bit_format = "S16_LE";
1380 break;
1381 }
1382
1383 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1384 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1385 MIXER_ENC_BIT_FORMAT);
1386 if (!ctrl_bit_format) {
1387 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1388 return -ENOSYS;
1389 }
1390 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1391 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1392 return -ENOSYS;
1393 }
1394 return 0;
1395}
1396
Surendar Karka2febd452018-12-13 17:56:43 +05301397static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301398{
Surendar Karka2febd452018-12-13 17:56:43 +05301399 const char *rate_str = "KHZ_8", *channels = "Zero";
1400 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1401 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301402
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001403 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301404 if (direction == SINK) {
1405 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1406 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1407 MIXER_SINK_SAMPLE_RATE);
1408 } else {
1409 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1410 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1411 MIXER_SAMPLE_RATE_RX);
1412 }
1413 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301414
Surendar Karka2febd452018-12-13 17:56:43 +05301415 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1416 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301417 return -ENOSYS;
1418 }
Zhou Song12c29502019-03-16 10:37:18 +08001419 if (a2dp.abr_config.is_abr_enabled) {
1420 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1421 MIXER_SOURCE_SAMPLE_RATE_TX);
1422 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301423 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1424 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001425 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301426
Zhou Song12c29502019-03-16 10:37:18 +08001427 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1428 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1429 return -ENOSYS;
1430 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301431 }
1432 } else {
1433
Surendar Karka2febd452018-12-13 17:56:43 +05301434 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301435 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301436 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301437 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1438 return -ENOSYS;
1439 }
1440
Surendar Karka2febd452018-12-13 17:56:43 +05301441 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301442 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1443 return -ENOSYS;
1444 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001445 }
1446
1447 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301448 if (direction == SINK) {
1449 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1450 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1451 MIXER_AFE_SINK_CHANNELS);
1452 } else {
1453 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1454 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1455 MIXER_AFE_IN_CHANNELS);
1456 }
1457 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001458 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1459 return -ENOSYS;
1460 }
Surendar Karka2febd452018-12-13 17:56:43 +05301461 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001462 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1463 return -ENOSYS;
1464 }
1465
1466 return 0;
1467}
1468
1469/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301470static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001471{
1472 struct mixer_ctl *ctl_dec_data = NULL;
1473 struct abr_dec_cfg_t dec_cfg;
1474 int ret = 0;
1475
1476 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301477 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001478 if (!ctl_dec_data) {
1479 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1480 return false;
1481 }
1482 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1483 dec_cfg.dec_format = dec_format;
1484 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1485 dec_cfg.imc_info.enable = IMC_ENABLE;
1486 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1487 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1488
1489 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1490 sizeof(dec_cfg));
1491 if (ret != 0) {
1492 ALOGE("%s: Failed to set decoder config", __func__);
1493 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301494 }
Surendar Karka2febd452018-12-13 17:56:43 +05301495 }
1496
1497 return true;
1498}
1499
Surendar Karkabbb3c822018-11-12 13:00:38 +05301500bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001501{
1502 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1503 struct sbc_dec_cfg_t sbc_dsp_cfg;
1504 bool is_configured = false;
1505 int ret = 0;
1506
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001507 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001508 goto fail;
1509
Surendar Karka2febd452018-12-13 17:56:43 +05301510 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001511 if (!ctl_dec_data) {
1512 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1513 is_configured = false;
1514 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301515 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001516
Florian Pfister1a84f312018-07-19 14:38:18 +02001517 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1518 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1519 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1520 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1521 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1522 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301523
Florian Pfister1a84f312018-07-19 14:38:18 +02001524 if (ret != 0) {
1525 ALOGE("%s: failed to set SBC decoder config", __func__);
1526 is_configured = false;
1527 goto fail;
1528 }
1529
1530 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1531 MIXER_DEC_BIT_FORMAT);
1532 if (!ctrl_bit_format) {
1533 ALOGE(" ERROR Dec bit format mixer control not identified");
1534 is_configured = false;
1535 goto fail;
1536 }
1537 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1538 if (ret != 0) {
1539 ALOGE("%s: Failed to set bit format to decoder", __func__);
1540 is_configured = false;
1541 goto fail;
1542 }
1543
1544 is_configured = true;
1545 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1546 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1547 a2dp.dec_channels = 1;
1548 else
1549 a2dp.dec_channels = 2;
1550 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1551 ALOGV("Successfully updated SBC dec format");
1552fail:
1553 return is_configured;
1554}
1555
Florian Pfister1a84f312018-07-19 14:38:18 +02001556/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301557static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301558{
Florian Pfister1a84f312018-07-19 14:38:18 +02001559 void *codec_info = NULL;
1560 codec_t codec_type = CODEC_TYPE_INVALID;
1561 bool is_configured = false;
1562 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301563
Florian Pfister1a84f312018-07-19 14:38:18 +02001564 if (!a2dp.audio_get_dec_config) {
1565 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1566 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301567 }
1568
Surendar Karka2febd452018-12-13 17:56:43 +05301569 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001570 if (!ctl_dec_data) {
1571 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1572 is_configured = false;
1573 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301574 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001575 codec_info = a2dp.audio_get_dec_config(&codec_type);
1576 switch(codec_type) {
1577 case CODEC_TYPE_SBC:
1578 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301579 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001580 break;
1581 case CODEC_TYPE_AAC:
1582 ALOGD(" AAC decoder supported BT device");
1583 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301584 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001585 break;
1586 default:
1587 ALOGD(" Received Unsupported decoder format");
1588 is_configured = false;
1589 break;
1590 }
1591 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301592}
1593
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301594/* API to configure SBC DSP encoder */
1595bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1596{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301597 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301598 struct sbc_enc_cfg_t sbc_dsp_cfg;
1599 bool is_configured = false;
1600 int ret = 0;
1601
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001602 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301603 return false;
1604
1605 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1606 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001607 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301608 is_configured = false;
1609 goto fail;
1610 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301611 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001612 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301613 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1614 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1615 switch(sbc_bt_cfg->channels) {
1616 case 0:
1617 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1618 break;
1619 case 1:
1620 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1621 break;
1622 case 3:
1623 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1624 break;
1625 case 2:
1626 default:
1627 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1628 break;
1629 }
1630 if (sbc_bt_cfg->alloc)
1631 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1632 else
1633 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1634 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1635 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1636 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1637 sizeof(struct sbc_enc_cfg_t));
1638 if (ret != 0) {
1639 ALOGE("%s: failed to set SBC encoder config", __func__);
1640 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301641 goto fail;
1642 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301643 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301644 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301645 is_configured = false;
1646 goto fail;
1647 }
1648 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001649 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301650 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301651
1652 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1653 a2dp.enc_channels = 1;
1654 else
1655 a2dp.enc_channels = 2;
1656
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301657 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1658 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301659fail:
1660 return is_configured;
1661}
1662
Manish Dewangan6a252632017-12-04 17:27:44 +05301663#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301664static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1665 audio_aptx_encoder_config *aptx_bt_cfg)
1666{
1667 int ret = 0;
1668
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001669 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301670 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1671 aptx_dsp_cfg, aptx_bt_cfg);
1672 return -EINVAL;
1673 }
1674
1675 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301676 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301677
1678
1679 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1680 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1681 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1682 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1683 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1684 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1685 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1686 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1687 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301688 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301689 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1690 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1691 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1692 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1693
1694
1695 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1696 case APTX_AD_CHANNEL_UNCHANGED:
1697 case APTX_AD_CHANNEL_JOINT_STEREO:
1698 case APTX_AD_CHANNEL_DUAL_MONO:
1699 case APTX_AD_CHANNEL_STEREO_TWS:
1700 case APTX_AD_CHANNEL_EARBUD:
1701 default:
1702 a2dp.enc_channels = CH_STEREO;
1703 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1704 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1705 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1706 break;
1707 case APTX_AD_CHANNEL_MONO:
1708 a2dp.enc_channels = CH_MONO;
1709 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1710 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1711 break;
1712 }
1713 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1714 case APTX_AD_SR_UNCHANGED:
1715 case APTX_AD_48:
1716 default:
1717 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1718 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1719 break;
1720 case APTX_AD_44_1:
1721 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1722 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
1723 break;
1724 }
1725 ALOGV("Successfully updated APTX AD enc format with \
1726 samplingrate: %d channels:%d",
1727 aptx_dsp_cfg->custom_cfg.sample_rate,
1728 aptx_dsp_cfg->custom_cfg.num_channels);
1729
1730 return ret;
1731}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301732
Zhou Song8fccbb62019-03-20 01:08:19 +08001733static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
1734 audio_aptx_encoder_config *aptx_bt_cfg)
1735{
1736 int ret = 0;
1737
1738 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Mingshu Pangaa429f72019-06-10 17:25:00 +08001739 ALOGE("Invalid param, aptx_dsp_cfg %pK aptx_bt_cfg %pK",
Zhou Song8fccbb62019-03-20 01:08:19 +08001740 aptx_dsp_cfg, aptx_bt_cfg);
1741 return -EINVAL;
1742 }
1743
1744 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
1745 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
1746
1747
1748 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1749 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1750 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1751 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1752 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1753 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1754 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1755 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1756 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
1757 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
1758 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
1759 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
1760 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
1761 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
1762 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1763 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1764 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1765 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1766
1767
1768 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1769 case APTX_AD_CHANNEL_UNCHANGED:
1770 case APTX_AD_CHANNEL_JOINT_STEREO:
1771 case APTX_AD_CHANNEL_DUAL_MONO:
1772 case APTX_AD_CHANNEL_STEREO_TWS:
1773 case APTX_AD_CHANNEL_EARBUD:
1774 default:
1775 a2dp.enc_channels = CH_STEREO;
1776 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1777 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1778 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1779 break;
1780 case APTX_AD_CHANNEL_MONO:
1781 a2dp.enc_channels = CH_MONO;
1782 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1783 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1784 break;
1785 }
1786 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1787 case APTX_AD_SR_UNCHANGED:
1788 case APTX_AD_48:
1789 default:
1790 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1791 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1792 break;
1793 case APTX_AD_44_1:
1794 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1795 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
1796 break;
1797 }
1798 ALOGV("Successfully updated APTX AD enc format with \
1799 samplingrate: %d channels:%d",
1800 aptx_dsp_cfg->custom_cfg.sample_rate,
1801 aptx_dsp_cfg->custom_cfg.num_channels);
1802
1803 return ret;
1804}
1805
Manisha Agarwala51768b2018-11-01 16:30:52 +05301806static void audio_a2dp_update_tws_channel_mode()
1807{
1808 char* channel_mode;
1809 struct mixer_ctl *ctl_channel_mode;
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301810
1811 ALOGD("Update tws for mono_mode on=%d",a2dp.is_tws_mono_mode_on);
1812
Manisha Agarwala51768b2018-11-01 16:30:52 +05301813 if (a2dp.is_tws_mono_mode_on)
1814 channel_mode = "One";
1815 else
1816 channel_mode = "Two";
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301817
Manisha Agarwala51768b2018-11-01 16:30:52 +05301818 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1819 if (!ctl_channel_mode) {
1820 ALOGE("failed to get tws mixer ctl");
1821 return;
1822 }
1823 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1824 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1825 return;
1826 }
1827}
1828
Manish Dewangan6a252632017-12-04 17:27:44 +05301829static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
1830 audio_aptx_encoder_config *aptx_bt_cfg)
1831{
1832 int ret = 0;
1833
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001834 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301835 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1836 aptx_dsp_cfg, aptx_bt_cfg);
1837 return -EINVAL;
1838 }
1839
1840 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001841 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301842
1843 if (!a2dp.is_aptx_dual_mono_supported) {
1844 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1845 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1846 } else {
1847 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1848 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1849 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1850 }
1851
1852 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
1853 case 1:
1854 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1855 break;
1856 case 2:
1857 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05301858 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1859 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
Manish Dewangan6a252632017-12-04 17:27:44 +05301860 break;
1861 }
1862 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
1863 if (!a2dp.is_aptx_dual_mono_supported) {
1864 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1865 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
1866 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
1867 aptx_dsp_cfg->custom_cfg.num_channels);
1868 } else {
1869 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1870 ALOGV("Successfully updated APTX dual mono enc format with \
1871 samplingrate: %d channels:%d syncmode %d",
1872 aptx_dsp_cfg->custom_cfg.sample_rate,
1873 aptx_dsp_cfg->custom_cfg.num_channels,
1874 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
1875 }
1876 return ret;
1877}
1878#else
1879static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
1880 audio_aptx_encoder_config *aptx_bt_cfg)
1881{
1882 int ret = 0;
1883
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001884 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301885 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1886 aptx_dsp_cfg, aptx_bt_cfg);
1887 return -EINVAL;
1888 }
1889
1890 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001891 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301892 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
1893 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
1894 switch(aptx_dsp_cfg->num_channels) {
1895 case 1:
1896 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
1897 break;
1898 case 2:
1899 default:
1900 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
1901 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
1902 break;
1903 }
1904
1905 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
1906 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
1907
1908 return ret;
1909}
1910#endif
1911
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301912/* API to configure APTX DSP encoder */
1913bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1914{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301915 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08001916 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001917 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301918 bool is_configured = false;
1919 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08001920 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301921
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001922 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301923 return false;
1924
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001925 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1926 if (!ctl_enc_data) {
1927 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1928 return false;
1929 }
1930
Manish Dewangan6a252632017-12-04 17:27:44 +05301931#ifndef LINUX_ENABLED
1932 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301933 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08001934 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001935 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001936 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
1937 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001938 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08001939 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001940 else
Zhou Song8fccbb62019-03-20 01:08:19 +08001941 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001942 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001943 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001944
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001945 if (ret) {
1946 is_configured = false;
1947 goto fail;
1948 }
1949
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001950 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001951 if (aptx_ad_ctl)
1952 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001953 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08001954 else
1955 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001956 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001957 } else {
1958 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001959 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05301960 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301961#else
1962 struct custom_enc_cfg_t aptx_dsp_cfg;
1963 mixer_size = sizeof(struct custom_enc_cfg_t);
1964 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05301965 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05301966 if (ret) {
1967 is_configured = false;
1968 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301969 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001970 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1971 mixer_size);
1972#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301973 if (ret != 0) {
1974 ALOGE("%s: Failed to set APTX encoder config", __func__);
1975 is_configured = false;
1976 goto fail;
1977 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301978#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001979 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301980 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001981 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301982 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
1983 else
1984 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301985#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301986 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301987 is_configured = false;
1988 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301989 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301990 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301991 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05301992 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301993 else
Surendar Karka2febd452018-12-13 17:56:43 +05301994 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301995fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05301996 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001997 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05301998 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301999 return is_configured;
2000}
2001
2002/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302003 */
Manish Dewangan6a252632017-12-04 17:27:44 +05302004#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002005bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302006#else
2007bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2008#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302009{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302010 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302011 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302012 bool is_configured = false;
2013 int ret = 0;
2014
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002015 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302016 return false;
2017
2018 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2019 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002020 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302021 is_configured = false;
2022 goto fail;
2023 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302024
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302025 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002026 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302027 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2028 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2029 switch(aptx_dsp_cfg.num_channels) {
2030 case 1:
2031 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2032 break;
2033 case 2:
2034 default:
2035 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2036 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2037 break;
2038 }
2039 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302040 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302041 if (ret != 0) {
2042 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2043 is_configured = false;
2044 goto fail;
2045 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302046 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302047 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302048 is_configured = false;
2049 goto fail;
2050 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302051 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002052 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302053 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302054 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302055 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2056 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302057fail:
2058 return is_configured;
2059}
2060
2061/* API to configure AAC DSP encoder */
2062bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2063{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302064 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302065 struct aac_enc_cfg_t aac_dsp_cfg;
2066 bool is_configured = false;
2067 int ret = 0;
2068
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002069 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302070 return false;
2071
2072 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2073 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002074 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302075 is_configured = false;
2076 goto fail;
2077 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302078 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002079 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302080 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302081 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002082 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302083 case 0:
2084 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2085 break;
2086 case 2:
2087 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2088 break;
2089 case 1:
2090 default:
2091 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2092 break;
2093 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302094 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302095 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002096
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302097 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2098 sizeof(struct aac_enc_cfg_t));
2099 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002100 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302101 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302102 goto fail;
2103 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302104 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302105 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302106 is_configured = false;
2107 goto fail;
2108 }
2109 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002110 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302111 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002112 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002113 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2114 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2115fail:
2116 return is_configured;
2117}
2118
2119bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2120{
2121 struct mixer_ctl *ctl_enc_data = NULL;
2122 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2123 bool is_configured = false;
2124 int ret = 0;
2125
2126 if (aac_bt_cfg == NULL)
2127 return false;
2128
2129 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2130 if (!ctl_enc_data) {
2131 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2132 is_configured = false;
2133 goto fail;
2134 }
2135 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302136 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002137 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2138 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2139 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2140 case 0:
2141 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2142 break;
2143 case 2:
2144 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2145 break;
2146 case 1:
2147 default:
2148 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2149 break;
2150 }
2151 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2152 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2153 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2154 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2155
2156 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2157 sizeof(struct aac_enc_cfg_v2_t));
2158 if (ret != 0) {
2159 ALOGE("%s: Failed to set AAC encoder config", __func__);
2160 is_configured = false;
2161 goto fail;
2162 }
2163 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2164 if (ret != 0) {
2165 is_configured = false;
2166 goto fail;
2167 }
2168 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302169 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002170 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2171 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2172 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2173 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302174fail:
2175 return is_configured;
2176}
2177
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302178bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2179{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302180 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302181 struct celt_enc_cfg_t celt_dsp_cfg;
2182 bool is_configured = false;
2183 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002184 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302185 return false;
2186
2187 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2188 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002189 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302190 is_configured = false;
2191 goto fail;
2192 }
2193 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2194
Florian Pfister1a84f312018-07-19 14:38:18 +02002195 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302196 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2197 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2198 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2199 case 1:
2200 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2201 break;
2202 case 2:
2203 default:
2204 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2205 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2206 break;
2207 }
2208
2209 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2210
2211 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2212 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2213 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2214 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2215 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2216
2217 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2218 sizeof(struct celt_enc_cfg_t));
2219 if (ret != 0) {
2220 ALOGE("%s: Failed to set CELT encoder config", __func__);
2221 is_configured = false;
2222 goto fail;
2223 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302224 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302225 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302226 is_configured = false;
2227 goto fail;
2228 }
2229 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002230 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302231 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302232 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302233 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2234 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2235fail:
2236 return is_configured;
2237}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302238
2239bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2240{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302241 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302242 struct ldac_enc_cfg_t ldac_dsp_cfg;
2243 bool is_configured = false;
2244 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002245 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302246 return false;
2247
2248 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2249 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002250 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302251 is_configured = false;
2252 goto fail;
2253 }
2254 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2255
Florian Pfister1a84f312018-07-19 14:38:18 +02002256 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302257 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2258 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2259 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2260 case 4:
2261 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2262 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2263 break;
2264 case 2:
2265 case 1:
2266 default:
2267 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2268 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2269 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2270 break;
2271 }
2272
2273 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2274 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2275 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002276 if (ldac_bt_cfg->is_abr_enabled) {
2277 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2278 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2279 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2280 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2281 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002282 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002283 }
2284
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302285 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2286 sizeof(struct ldac_enc_cfg_t));
2287 if (ret != 0) {
2288 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2289 is_configured = false;
2290 goto fail;
2291 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302292 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302293 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302294 is_configured = false;
2295 goto fail;
2296 }
2297 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002298 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302299 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2300 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002301 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302302 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2303 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2304fail:
2305 return is_configured;
2306}
2307
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302308bool configure_a2dp_encoder_format()
2309{
2310 void *codec_info = NULL;
2311 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002312 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302313 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002314 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302315
Florian Pfister1a84f312018-07-19 14:38:18 +02002316 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302317 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2318 return false;
2319 }
2320 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002321 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302322 &codec_type);
2323
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002324 // ABR disabled by default for all codecs
2325 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302326 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002327
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302328 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002329 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302330 ALOGD(" Received SBC encoder supported BT device");
2331 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002332 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302333 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002334 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302335 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302336#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002337 a2dp.is_aptx_dual_mono_supported = false;
2338 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302339#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302340 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002341 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302342 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002343 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302344 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302345#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302346 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002347 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302348#else
2349 is_configured =
2350 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2351#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002352 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302353#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002354 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002355 ALOGD(" Received APTX dual mono encoder supported BT device");
2356 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302357 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2358 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002359 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2360 is_configured =
2361 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302362 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302363#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002364 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302365 ALOGD(" Received AAC encoder supported BT device");
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002366 bool is_aac_frame_ctl_enabled =
2367 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
2368 is_configured = is_aac_frame_ctl_enabled ?
2369 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2370 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302371 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002372 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302373 ALOGD(" Received CELT encoder supported BT device");
2374 is_configured =
2375 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2376 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002377 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302378 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002379 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2380 instance_id = MAX_INSTANCE_ID;
2381 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302382 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002383 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302384 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302385 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302386#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302387 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302388 ALOGD(" Received APTX AD encoder supported BT device");
2389 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2390 instance_id = MAX_INSTANCE_ID;
2391 a2dp.abr_config.imc_instance = instance_id--;
2392 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2393 a2dp.is_aptx_adaptive = true;
2394 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2395 is_configured =
2396 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302397 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302398 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302399#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002400 case CODEC_TYPE_PCM:
2401 ALOGD("Received PCM format for BT device");
2402 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2403 is_configured = true;
2404 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302405 default:
2406 ALOGD(" Received Unsupported encoder formar");
2407 is_configured = false;
2408 break;
2409 }
2410 return is_configured;
2411}
2412
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002413int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302414{
2415 int ret = 0;
2416
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002417 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302418
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002419 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002420 && a2dp.audio_get_enc_config)) {
2421 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302422 return -ENOSYS;
2423 }
2424
Zhou Song12c29502019-03-16 10:37:18 +08002425 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302426 //session will be restarted after suspend completion
2427 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302428 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302429 }
2430
Florian Pfister1a84f312018-07-19 14:38:18 +02002431 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302432 ALOGD("calling BT module stream start");
2433 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002434 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302435 ALOGE("BT controller start return = %d",ret);
2436 if (ret != 0 ) {
2437 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002438 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302439 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002440 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002441 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302442 ret = 0;
2443 ALOGD("Start playback successful to BT library");
2444 } else {
2445 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002446 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302447 ret = -ETIMEDOUT;
2448 }
2449 }
2450 }
2451
Florian Pfister1a84f312018-07-19 14:38:18 +02002452 if (a2dp.a2dp_source_started) {
2453 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302454 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302455 audio_a2dp_update_tws_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002456 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002457 if (a2dp.abr_config.is_abr_enabled)
2458 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302459 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302460
2461 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002462 a2dp.a2dp_source_total_active_session_requests);
2463 return ret;
2464}
2465
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002466uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002467{
2468 uint32_t latency = 0;
2469
2470 switch(a2dp.bt_decoder_format) {
2471 case CODEC_TYPE_SBC:
2472 latency = DEFAULT_SINK_LATENCY_SBC;
2473 break;
2474 case CODEC_TYPE_AAC:
2475 latency = DEFAULT_SINK_LATENCY_AAC;
2476 break;
2477 default:
2478 latency = 200;
2479 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2480 break;
2481 }
2482 return (uint64_t)latency;
2483}
2484
2485bool a2dp_send_sink_setup_complete(void) {
2486 uint64_t system_latency = 0;
2487 bool is_complete = false;
2488
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002489 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002490
2491 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2492 is_complete = true;
2493 }
2494 return is_complete;
2495}
2496
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002497bool a2dp_sink_is_ready()
2498{
2499 bool ret = false;
2500
2501 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2502 (a2dp.is_a2dp_offload_supported) &&
2503 (a2dp.audio_sink_check_a2dp_ready))
2504 ret = a2dp.audio_sink_check_a2dp_ready();
2505 return ret;
2506}
2507
2508int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002509{
2510 int ret = 0;
2511
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002512 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002513
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002514 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002515 && a2dp.audio_get_dec_config)) {
2516 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2517 return -ENOSYS;
2518 }
2519
2520 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2521 ALOGD("calling BT module stream start");
2522 /* This call indicates BT IPC lib to start capture */
2523 ret = a2dp.audio_sink_start();
2524 ALOGE("BT controller start capture return = %d",ret);
2525 if (ret != 0 ) {
2526 ALOGE("BT controller start capture failed");
2527 a2dp.a2dp_sink_started = false;
2528 } else {
2529
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002530 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002531 ALOGD("Wait for capture ready not successful");
2532 ret = -ETIMEDOUT;
2533 }
2534
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002535 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002536 a2dp.a2dp_sink_started = true;
2537 ret = 0;
2538 ALOGD("Start capture successful to BT library");
2539 } else {
2540 ALOGD(" unable to configure DSP decoder");
2541 a2dp.a2dp_sink_started = false;
2542 ret = -ETIMEDOUT;
2543 }
2544
2545 if (!a2dp_send_sink_setup_complete()) {
2546 ALOGD("sink_setup_complete not successful");
2547 ret = -ETIMEDOUT;
2548 }
2549 }
2550 }
2551
2552 if (a2dp.a2dp_sink_started) {
2553 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002554 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002555 }
2556 }
2557
2558 ALOGD("start A2DP sink total active sessions :%d",
2559 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302560 return ret;
2561}
2562
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302563static void reset_a2dp_enc_config_params()
2564{
2565 int ret =0;
2566
Aalique Grahame22e49102018-12-18 14:23:57 -08002567 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302568 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302569 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302570
2571 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
2572 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
2573 MIXER_ENC_CONFIG_BLOCK);
2574 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002575 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302576 } else {
2577 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
2578 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002579 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302580 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002581
2582 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2583
Manisha Agarwala51768b2018-11-01 16:30:52 +05302584 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2585
2586 if (!ctl_channel_mode) {
2587 ALOGE("failed to get tws mixer ctl");
2588 } else {
2589 channel_mode = "Two";
2590 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2591 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2592 }
2593 a2dp.is_tws_mono_mode_on = false;
2594 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302595}
2596
Surendar Karka2febd452018-12-13 17:56:43 +05302597static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002598{
2599 struct mixer_ctl *ctl_dec_data = NULL;
2600 struct abr_dec_cfg_t dummy_reset_cfg;
2601 int ret = 0;
2602
Preetam Singh Ranawat25780e42019-07-31 18:15:57 +05302603 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
2604 if (!ctl_dec_data) {
2605 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
2606 return -EINVAL;
2607 }
2608 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
2609 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
2610 sizeof(dummy_reset_cfg));
2611 if (ret != 0) {
2612 ALOGE("%s: Failed to set dummy decoder config", __func__);
2613 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002614 }
2615
2616 return ret;
2617}
2618
Surendar Karka2febd452018-12-13 17:56:43 +05302619static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02002620{
2621 int ret =0;
2622
2623 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
2624 struct aac_dec_cfg_t dummy_reset_config;
2625
2626 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
2627 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05302628 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02002629 if (!ctl_dec_config) {
2630 ALOGE(" ERROR a2dp decoder format mixer control not identified");
2631 } else {
2632 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
2633 sizeof(struct aac_dec_cfg_t));
2634 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
2635 }
2636 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
2637 MIXER_DEC_BIT_FORMAT);
2638 if (!ctrl_bit_format) {
2639 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
2640 } else {
2641 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
2642 if (ret != 0) {
2643 ALOGE("%s: Failed to set bit format to decoder", __func__);
2644 }
2645 }
2646}
2647
Zhou Song12c29502019-03-16 10:37:18 +08002648static void reset_codec_config()
2649{
2650 reset_a2dp_enc_config_params();
2651 reset_a2dp_source_dec_config_params();
2652 a2dp_reset_backend_cfg(SOURCE);
2653 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
2654 stop_abr();
2655 a2dp.abr_config.is_abr_enabled = false;
2656}
2657
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002658int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302659{
2660 int ret =0;
2661
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002662 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002663 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002664 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302665 return -ENOSYS;
2666 }
2667
Florian Pfister1a84f312018-07-19 14:38:18 +02002668 if (a2dp.a2dp_source_total_active_session_requests > 0)
2669 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08002670 else
2671 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302672
Florian Pfister1a84f312018-07-19 14:38:18 +02002673 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302674 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02002675 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302676 if (ret < 0)
2677 ALOGE("stop stream to BT IPC lib failed");
2678 else
2679 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08002680 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
2681 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05302682 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302683 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002684 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002685 a2dp.a2dp_source_started = false;
2686 ALOGD("Stop A2DP playback, total active sessions :%d",
2687 a2dp.a2dp_source_total_active_session_requests);
2688 return 0;
2689}
2690
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002691int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002692{
2693 int ret =0;
2694
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002695 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002696 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002697 ALOGE("a2dp handle is not identified, Ignoring stop request");
2698 return -ENOSYS;
2699 }
2700
2701 if (a2dp.a2dp_sink_total_active_session_requests > 0)
2702 a2dp.a2dp_sink_total_active_session_requests--;
2703
2704 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2705 ALOGV("calling BT module stream stop");
2706 ret = a2dp.audio_sink_stop();
2707 if (ret < 0)
2708 ALOGE("stop stream to BT IPC lib failed");
2709 else
2710 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05302711 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002712 a2dp_reset_backend_cfg(SINK);
2713 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002714 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002715 a2dp.a2dp_source_started = false;
2716 ALOGD("Stop A2DP capture, total active sessions :%d",
2717 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302718 return 0;
2719}
2720
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002721int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302722{
Aalique Grahame22e49102018-12-18 14:23:57 -08002723 int ret = 0, val, status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302724 char value[32]={0};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302725 struct audio_usecase *uc_info;
2726 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302727
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002728 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002729 ALOGV("no supported encoders identified,ignoring a2dp setparam");
2730 status = -EINVAL;
2731 goto param_handled;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302732 }
2733
2734 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
2735 sizeof(value));
Zhou Song681350a2017-10-19 16:28:42 +08002736 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302737 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002738 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002739 ALOGV("Received device connect request for A2DP source");
2740 open_a2dp_source();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302741 }
2742 goto param_handled;
2743 }
2744
2745 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
2746 sizeof(value));
2747
Zhou Song681350a2017-10-19 16:28:42 +08002748 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302749 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002750 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002751 ALOGV("Received source device dis- connect request");
Samyak Jain4828f4c2018-08-24 16:31:06 +05302752 close_a2dp_output();
kunleiz4a1fad62018-02-08 18:00:16 +08002753 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302754 reset_a2dp_source_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002755 a2dp_reset_backend_cfg(SOURCE);
2756 } else if (audio_is_a2dp_in_device(val)) {
2757 ALOGV("Received sink device dis- connect request");
2758 close_a2dp_input();
Surendar Karka2febd452018-12-13 17:56:43 +05302759 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002760 a2dp_reset_backend_cfg(SINK);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302761 }
2762 goto param_handled;
2763 }
Surendar Karka2febd452018-12-13 17:56:43 +05302764#ifndef LINUX_ENABLED
Manisha Agarwala51768b2018-11-01 16:30:52 +05302765 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
2766 if (ret>=0) {
2767 ALOGD("Setting tws channel mode to %s",value);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002768 if (!(strncmp(value,"mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302769 a2dp.is_tws_mono_mode_on = true;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002770 else if (!(strncmp(value,"dual-mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302771 a2dp.is_tws_mono_mode_on = false;
2772 audio_a2dp_update_tws_channel_mode();
2773 goto param_handled;
2774 }
Surendar Karka2febd452018-12-13 17:56:43 +05302775#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302776 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
2777 if (ret >= 0) {
Surendar Karka2febd452018-12-13 17:56:43 +05302778 if (a2dp.bt_lib_source_handle) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302779 if ((!strncmp(value,"true",sizeof(value)))) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002780 if (a2dp.a2dp_source_suspended) {
2781 ALOGD("%s: A2DP is already suspended", __func__);
2782 goto param_handled;
2783 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302784 ALOGD("Setting a2dp to suspend state");
Florian Pfister1a84f312018-07-19 14:38:18 +02002785 a2dp.a2dp_source_suspended = true;
Surendar Karka2febd452018-12-13 17:56:43 +05302786 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
yidongh7203cca2018-09-19 16:12:25 +08002787 goto param_handled;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302788 list_for_each(node, &a2dp.adev->usecase_list) {
2789 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302790 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08002791 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302792 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002793 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302794 pthread_mutex_lock(&a2dp.adev->lock);
2795 }
2796 }
Zhou Song12c29502019-03-16 10:37:18 +08002797 if (!a2dp.swb_configured)
2798 reset_codec_config();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002799 if (a2dp.audio_source_suspend)
Florian Pfister1a84f312018-07-19 14:38:18 +02002800 a2dp.audio_source_suspend();
2801 } else if (a2dp.a2dp_source_suspended == true) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302802 ALOGD("Resetting a2dp suspend state");
Zhou Song10617ed2017-05-26 13:28:48 +08002803 struct audio_usecase *uc_info;
2804 struct listnode *node;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002805 if (a2dp.clear_source_a2dpsuspend_flag)
Florian Pfister1a84f312018-07-19 14:38:18 +02002806 a2dp.clear_source_a2dpsuspend_flag();
2807 a2dp.a2dp_source_suspended = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302808 /*
2809 * It is possible that before suspend,a2dp sessions can be active
2810 * for example during music + voice activation concurrency
2811 * a2dp suspend will be called & BT will change to sco mode
2812 * though music is paused as a part of voice activation
2813 * compress session close happens only after pause timeout(10secs)
2814 * so if resume request comes before pause timeout as a2dp session
2815 * is already active IPC start will not be called from APM/audio_hw
2816 * Fix is to call a2dp start for IPC library post suspend
2817 * based on number of active session count
2818 */
Florian Pfister1a84f312018-07-19 14:38:18 +02002819 if (a2dp.a2dp_source_total_active_session_requests > 0) {
Naresh Tanniru649871a2016-11-04 18:08:32 +05302820 ALOGD(" Calling IPC lib start post suspend state");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002821 if (a2dp.audio_source_start) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002822 ret = a2dp.audio_source_start();
Naresh Tanniru649871a2016-11-04 18:08:32 +05302823 if (ret != 0) {
2824 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002825 a2dp.a2dp_source_started = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302826 }
2827 }
2828 }
Zhou Song10617ed2017-05-26 13:28:48 +08002829 list_for_each(node, &a2dp.adev->usecase_list) {
2830 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302831 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08002832 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302833 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002834 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302835 pthread_mutex_lock(&a2dp.adev->lock);
2836 }
Zhou Song10617ed2017-05-26 13:28:48 +08002837 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302838 }
2839 }
2840 goto param_handled;
2841 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002842
2843 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
2844 sizeof(value));
2845 if (ret >= 0) {
2846 if (a2dp.is_a2dp_offload_supported &&
2847 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2848 *reconfig = true;
2849 }
2850 goto param_handled;
2851 }
2852
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302853param_handled:
2854 ALOGV("end of a2dp setparam");
Aalique Grahame22e49102018-12-18 14:23:57 -08002855 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302856}
2857
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002858void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302859{
2860 a2dp.is_handoff_in_progress = is_on;
2861}
2862
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002863bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302864{
2865 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05302866 // Or if a2dp device is selected but earlier start failed ( as a2dp
2867 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02002868 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302869}
2870
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002871void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302872{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302873 *sample_rate = a2dp.enc_sampling_rate;
2874}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302875
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002876void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02002877{
2878 *sample_rate = a2dp.dec_sampling_rate;
2879}
2880
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002881bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302882{
2883 bool ret = false;
2884
Florian Pfister1a84f312018-07-19 14:38:18 +02002885 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302886 return ret;
2887
Florian Pfister1a84f312018-07-19 14:38:18 +02002888 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08002889 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002890 (a2dp.audio_source_check_a2dp_ready))
2891 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302892 return ret;
2893}
2894
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002895bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02002896{
2897 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302898}
2899
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002900void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08002901 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302902{
2903 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02002904 a2dp.bt_lib_source_handle = NULL;
2905 a2dp.a2dp_source_started = false;
2906 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
2907 a2dp.a2dp_source_total_active_session_requests = 0;
2908 a2dp.a2dp_source_suspended = false;
2909 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302910 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302911 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002912 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302913 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002914 a2dp.abr_config.is_abr_enabled = false;
2915 a2dp.abr_config.abr_started = false;
2916 a2dp.abr_config.imc_instance = 0;
2917 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08002918 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302919 a2dp.is_tws_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05302920 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08002921 a2dp.swb_configured = false;
2922
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002923 // init function pointers
2924 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08002925 init_config.fp_platform_get_pcm_device_id;
2926 fp_check_a2dp_restore = init_config.fp_check_a2dp_restore;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002927
kunleiz5a127262017-09-08 14:47:48 +08002928 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302929 reset_a2dp_source_dec_config_params();
2930 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002931
2932 a2dp.bt_lib_sink_handle = NULL;
2933 a2dp.a2dp_sink_started = false;
2934 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
2935 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002936
2937 if (is_running_with_enhanced_fwk == UNINITIALIZED)
2938 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
2939 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08002940 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02002941
2942 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302943 update_offload_codec_capabilities();
2944}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002945
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002946uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002947{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002948 uint32_t latency = 0;
2949 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302950 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
2951 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002952 char value[PROPERTY_VALUE_MAX];
2953
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002954 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08002955 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002956 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302957 if (sscanf(value, "%d/%d/%d/%d/%d%d",
2958 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002959 ALOGI("Failed to parse avsync offset params from '%s'.", value);
2960 avsync_runtime_prop = 0;
2961 }
2962 }
2963
yidongh0515e042017-07-06 15:00:34 +08002964 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02002965 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2966 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08002967 }
2968
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07002969 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002970 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08002971 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
2972 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002973 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002974 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08002975 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
2976 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002977 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002978 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08002979 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
2980 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002981 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002982 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08002983 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
2984 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002985 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002986 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302987 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
2988 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
2989 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002990 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302991 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
2992 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
2993 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05302994 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05302995 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05302996 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08002997 case CODEC_TYPE_PCM:
2998 latency = ENCODER_LATENCY_PCM;
2999 latency += DEFAULT_SINK_LATENCY_PCM;
3000 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003001 default:
3002 latency = 200;
3003 break;
3004 }
3005 return latency;
3006}
Aalique Grahame22e49102018-12-18 14:23:57 -08003007
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003008int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003009 struct str_parms *reply)
3010{
3011 int ret, val = 0;
3012 char value[32]={0};
3013
3014 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3015 value, sizeof(value));
3016 if (ret >= 0) {
3017 val = a2dp.is_a2dp_offload_supported;
3018 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3019 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3020 }
3021
3022 return 0;
3023}
Zhou Song12c29502019-03-16 10:37:18 +08003024
3025
3026bool configure_aptx_ad_speech_enc_fmt() {
3027 struct mixer_ctl *ctl_enc_data = NULL;
3028 int mixer_size = 0;
3029 int ret = 0;
3030 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3031
3032 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3033 if (!ctl_enc_data) {
3034 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3035 return false;
3036 }
3037
3038 /* Initialize dsp configuration params */
3039 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3040 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3041 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3042 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3043 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3044 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3045 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3046 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3047 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3048 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3049 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3050
3051 /* Configure AFE DSP configuration */
3052 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3053 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3054 mixer_size);
3055 if (ret != 0) {
3056 ALOGE("%s: Failed to set SWB encoder config", __func__);
3057 return false;
3058 }
3059
3060 /* Configure AFE Input Bit Format as PCM_16 */
3061 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3062 if (ret != 0) {
3063 ALOGE("%s: Failed to set SWB bit format", __func__);
3064 return false;
3065 }
3066
3067 return true;
3068}
3069
3070bool configure_aptx_ad_speech_dec_fmt()
3071{
3072 struct mixer_ctl *ctl_dec_data = NULL;
3073 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3074 int ret = 0;
3075
3076 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3077 if (!ctl_dec_data) {
3078 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3079 return false;
3080 }
3081 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3082 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3083 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3084 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3085 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3086 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3087 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3088 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3089
3090 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3091 sizeof(dec_cfg));
3092 if (ret != 0) {
3093 ALOGE("%s: Failed to set decoder config", __func__);
3094 return false;
3095 }
3096 return true;
3097}
3098
3099int sco_start_configuration()
3100{
3101 ALOGD("sco_start_configuration start");
3102
3103 if (!a2dp.swb_configured) {
3104 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3105 /* Configure AFE codec*/
3106 if (configure_aptx_ad_speech_enc_fmt() &&
3107 configure_aptx_ad_speech_dec_fmt()) {
3108 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3109 } else {
3110 ALOGE("%s: failed to send SCO configuration", __func__);
3111 return -ETIMEDOUT;
3112 }
3113 /* Configure backend*/
3114 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3115 a2dp.enc_channels = CH_MONO;
3116 a2dp.abr_config.is_abr_enabled = true;
3117 a2dp_set_backend_cfg(SOURCE);
3118 /* Start abr*/
3119 start_abr();
3120 a2dp.swb_configured = true;
3121 }
3122 return 0;
3123}
3124
3125void sco_reset_configuration()
3126{
Zhou Songd6d71752019-05-21 18:08:51 +08003127 if (a2dp.swb_configured) {
3128 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003129
Zhou Songd6d71752019-05-21 18:08:51 +08003130 reset_codec_config();
3131 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3132 a2dp.swb_configured = false;
3133 }
Zhou Song12c29502019-03-16 10:37:18 +08003134}