blob: 429a2e3705934a4052e4f788262cb44d1a41ce36 [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
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301034 if (is_running_with_enhanced_fwk == UNINITIALIZED)
1035 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
1036 if (a2dp.bt_lib_source_handle && is_running_with_enhanced_fwk
1037 && a2dp.bt_audio_pre_init) {
1038 ALOGD("calling BT module preinit");
1039 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001040 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001041}
1042
1043/* API to open BT IPC library to start IPC communication for BT Sink*/
1044static void open_a2dp_sink()
1045{
1046 ALOGD(" Open A2DP input start ");
1047 if (a2dp.bt_lib_sink_handle == NULL){
1048 ALOGD(" Requesting for BT lib handle");
1049 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1050
1051 if (a2dp.bt_lib_sink_handle == NULL) {
1052 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1053 } else {
1054 a2dp.audio_sink_start = (audio_sink_start_t)
1055 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1056 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1057 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1058 a2dp.audio_sink_stop = (audio_sink_stop_t)
1059 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1060 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1061 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1062 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1063 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1064 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301065 }
1066}
1067
1068static int close_a2dp_output()
1069{
1070 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001071
1072 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1073 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301074 return -ENOSYS;
1075 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001076
1077 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1078 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001079 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001080 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301081 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001082 a2dp.a2dp_source_started = false;
1083 a2dp.a2dp_source_total_active_session_requests = 0;
1084 a2dp.a2dp_source_suspended = false;
1085 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301086 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301087 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001088 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001089 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1090 stop_abr();
1091 a2dp.abr_config.is_abr_enabled = false;
1092 a2dp.abr_config.abr_started = false;
1093 a2dp.abr_config.imc_instance = 0;
1094 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001095 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301096 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001097
1098 return 0;
1099}
1100
1101static int close_a2dp_input()
1102{
1103 ALOGV("%s\n",__func__);
1104
1105 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1106 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1107 return -ENOSYS;
1108 }
1109
1110 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1111 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001112 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001113 ALOGE("failed close a2dp sink control path from BT library");
1114 }
1115 a2dp.a2dp_sink_started = false;
1116 a2dp.a2dp_sink_total_active_session_requests = 0;
1117 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1118 a2dp.dec_sampling_rate = 48000;
1119 a2dp.dec_channels = 2;
1120 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301121
1122 return 0;
1123}
1124
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301125static void a2dp_check_and_set_scrambler()
1126{
1127 bool scrambler_mode = false;
1128 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001129 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1130 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301131
1132 if (scrambler_mode) {
1133 //enable scrambler in dsp
1134 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1135 MIXER_SCRAMBLER_MODE);
1136 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001137 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301138 return;
1139 } else {
1140 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1141 ALOGE("%s: Could not set scrambler mode", __func__);
1142 return;
1143 }
1144 }
1145 }
1146}
1147
Florian Pfister1a84f312018-07-19 14:38:18 +02001148static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301149{
Florian Pfister1a84f312018-07-19 14:38:18 +02001150 char *rate_str = NULL, *channels = NULL;
1151 uint32_t sampling_rate;
1152 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1153 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301154
Florian Pfister1a84f312018-07-19 14:38:18 +02001155 if (direction == SINK) {
1156 sampling_rate = a2dp.dec_sampling_rate;
1157 } else {
1158 sampling_rate = a2dp.enc_sampling_rate;
1159 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001160 /*
1161 * For LDAC encoder and AAC decoder open slimbus port at
1162 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1163 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1164 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001165 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301166 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001167 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1168 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301169 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1170 sampling_rate = sampling_rate *2;
1171 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001172
Aalique Grahame22e49102018-12-18 14:23:57 -08001173 // No need to configure backend for PCM format.
1174 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1175 return 0;
1176 }
1177
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301178 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301179 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301180 case 44100:
1181 rate_str = "KHZ_44P1";
1182 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301183 case 88200:
1184 rate_str = "KHZ_88P2";
1185 break;
1186 case 96000:
1187 rate_str = "KHZ_96";
1188 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001189 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301190 default:
1191 rate_str = "KHZ_48";
1192 break;
1193 }
1194
Florian Pfister1a84f312018-07-19 14:38:18 +02001195 if (direction == SINK) {
1196 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1197 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301198 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001199 } else {
1200 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1201 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301202 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001203 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301204 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001205
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301206 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1207 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301208 is_configured = false;
1209 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301210 }
1211
Surendar Karka2febd452018-12-13 17:56:43 +05301212 if (direction == SOURCE) {
1213 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001214 if (a2dp.abr_config.is_abr_enabled) {
1215 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH)
1216 rate_str = SPEECH_TX_SAMPLE_RATE;
1217 else
1218 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301219
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301220 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1221 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1222 MIXER_SOURCE_SAMPLE_RATE_TX);
1223 if (!ctl_sample_rate) {
1224 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1225 is_configured = false;
1226 goto fail;
1227 }
1228
1229 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1230 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1231 is_configured = false;
1232 goto fail;
1233 }
Surendar Karka2febd452018-12-13 17:56:43 +05301234 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301235 }
1236 } else {
1237 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1238 MIXER_SAMPLE_RATE_TX is not supported */
1239 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1240 MIXER_SAMPLE_RATE_DEFAULT);
1241 if (!ctl_sample_rate) {
1242 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301243 is_configured = false;
1244 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301245 }
1246
1247 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1248 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001249 is_configured = false;
1250 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301251 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301252 }
1253
Florian Pfister1a84f312018-07-19 14:38:18 +02001254 if (direction == SINK) {
1255 switch (a2dp.dec_channels) {
1256 case 1:
1257 channels = "One";
1258 break;
1259 case 2:
1260 default:
1261 channels = "Two";
1262 break;
1263 }
1264
Ramu Gottipati02809682018-12-19 16:46:12 +05301265 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001266 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1267 MIXER_AFE_SINK_CHANNELS);
1268 } else {
1269 //Configure AFE enc channels
1270 switch (a2dp.enc_channels) {
1271 case 1:
1272 channels = "One";
1273 break;
1274 case 2:
1275 default:
1276 channels = "Two";
1277 break;
1278 }
1279
Ramu Gottipati02809682018-12-19 16:46:12 +05301280 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001281 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1282 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301283 }
1284
Florian Pfister1a84f312018-07-19 14:38:18 +02001285 if (!ctrl_channels) {
1286 ALOGE(" ERROR AFE channels mixer control not identified");
1287 } else {
1288 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301289 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001290 is_configured = false;
1291 goto fail;
1292 }
1293 }
1294 is_configured = true;
1295fail:
1296 return is_configured;
1297}
1298
Surendar Karkabbb3c822018-11-12 13:00:38 +05301299bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001300{
1301 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1302 struct aac_dec_cfg_t aac_dsp_cfg;
1303 bool is_configured = false;
1304 int ret = 0;
1305
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001306 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001307 return false;
1308
Surendar Karka2febd452018-12-13 17:56:43 +05301309 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001310 if (!ctl_dec_data) {
1311 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1312 is_configured = false;
1313 goto fail;
1314 }
1315
1316 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1317 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1318 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1319 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1320 switch(aac_bt_cfg->audio_object_type) {
1321 case 0:
1322 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301323 break;
1324 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001325 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1326 break;
1327 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301328 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001329 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301330 break;
1331 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001332 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1333 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1334 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1335 sizeof(struct aac_dec_cfg_t));
1336 if (ret != 0) {
1337 ALOGE("%s: failed to set AAC decoder config", __func__);
1338 is_configured = false;
1339 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001340 }
1341
Florian Pfister1a84f312018-07-19 14:38:18 +02001342 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1343 MIXER_DEC_BIT_FORMAT);
1344 if (!ctrl_bit_format) {
1345 ALOGE(" ERROR Dec bit format mixer control not identified");
1346 is_configured = false;
1347 goto fail;
1348 }
1349 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1350 if (ret != 0) {
1351 ALOGE("%s: Failed to set bit format to decoder", __func__);
1352 is_configured = false;
1353 goto fail;
1354 }
1355
1356 is_configured = true;
1357 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1358 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1359 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1360 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1361 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1362fail:
1363 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301364}
1365
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301366static int a2dp_set_bit_format(uint32_t enc_bit_format)
1367{
1368 const char *bit_format = NULL;
1369 struct mixer_ctl *ctrl_bit_format = NULL;
1370
1371 // Configure AFE Input Bit Format
1372 switch (enc_bit_format) {
1373 case 32:
1374 bit_format = "S32_LE";
1375 break;
1376 case 24:
1377 bit_format = "S24_LE";
1378 break;
1379 case 16:
1380 default:
1381 bit_format = "S16_LE";
1382 break;
1383 }
1384
1385 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1386 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1387 MIXER_ENC_BIT_FORMAT);
1388 if (!ctrl_bit_format) {
1389 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1390 return -ENOSYS;
1391 }
1392 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1393 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1394 return -ENOSYS;
1395 }
1396 return 0;
1397}
1398
Surendar Karka2febd452018-12-13 17:56:43 +05301399static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301400{
Surendar Karka2febd452018-12-13 17:56:43 +05301401 const char *rate_str = "KHZ_8", *channels = "Zero";
1402 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1403 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301404
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001405 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301406 if (direction == SINK) {
1407 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1408 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1409 MIXER_SINK_SAMPLE_RATE);
1410 } else {
1411 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1412 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1413 MIXER_SAMPLE_RATE_RX);
1414 }
1415 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301416
Surendar Karka2febd452018-12-13 17:56:43 +05301417 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1418 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301419 return -ENOSYS;
1420 }
Zhou Song12c29502019-03-16 10:37:18 +08001421 if (a2dp.abr_config.is_abr_enabled) {
1422 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1423 MIXER_SOURCE_SAMPLE_RATE_TX);
1424 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301425 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1426 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001427 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301428
Zhou Song12c29502019-03-16 10:37:18 +08001429 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1430 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1431 return -ENOSYS;
1432 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301433 }
1434 } else {
1435
Surendar Karka2febd452018-12-13 17:56:43 +05301436 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301437 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301438 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301439 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1440 return -ENOSYS;
1441 }
1442
Surendar Karka2febd452018-12-13 17:56:43 +05301443 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301444 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1445 return -ENOSYS;
1446 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001447 }
1448
1449 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301450 if (direction == SINK) {
1451 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1452 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1453 MIXER_AFE_SINK_CHANNELS);
1454 } else {
1455 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1456 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1457 MIXER_AFE_IN_CHANNELS);
1458 }
1459 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001460 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1461 return -ENOSYS;
1462 }
Surendar Karka2febd452018-12-13 17:56:43 +05301463 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001464 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1465 return -ENOSYS;
1466 }
1467
1468 return 0;
1469}
1470
1471/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301472static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001473{
1474 struct mixer_ctl *ctl_dec_data = NULL;
1475 struct abr_dec_cfg_t dec_cfg;
1476 int ret = 0;
1477
1478 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301479 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001480 if (!ctl_dec_data) {
1481 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1482 return false;
1483 }
1484 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1485 dec_cfg.dec_format = dec_format;
1486 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1487 dec_cfg.imc_info.enable = IMC_ENABLE;
1488 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1489 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1490
1491 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1492 sizeof(dec_cfg));
1493 if (ret != 0) {
1494 ALOGE("%s: Failed to set decoder config", __func__);
1495 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301496 }
Surendar Karka2febd452018-12-13 17:56:43 +05301497 }
1498
1499 return true;
1500}
1501
Surendar Karkabbb3c822018-11-12 13:00:38 +05301502bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001503{
1504 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1505 struct sbc_dec_cfg_t sbc_dsp_cfg;
1506 bool is_configured = false;
1507 int ret = 0;
1508
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001509 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001510 goto fail;
1511
Surendar Karka2febd452018-12-13 17:56:43 +05301512 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001513 if (!ctl_dec_data) {
1514 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1515 is_configured = false;
1516 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301517 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001518
Florian Pfister1a84f312018-07-19 14:38:18 +02001519 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1520 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1521 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1522 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1523 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1524 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301525
Florian Pfister1a84f312018-07-19 14:38:18 +02001526 if (ret != 0) {
1527 ALOGE("%s: failed to set SBC decoder config", __func__);
1528 is_configured = false;
1529 goto fail;
1530 }
1531
1532 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1533 MIXER_DEC_BIT_FORMAT);
1534 if (!ctrl_bit_format) {
1535 ALOGE(" ERROR Dec bit format mixer control not identified");
1536 is_configured = false;
1537 goto fail;
1538 }
1539 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1540 if (ret != 0) {
1541 ALOGE("%s: Failed to set bit format to decoder", __func__);
1542 is_configured = false;
1543 goto fail;
1544 }
1545
1546 is_configured = true;
1547 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1548 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1549 a2dp.dec_channels = 1;
1550 else
1551 a2dp.dec_channels = 2;
1552 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1553 ALOGV("Successfully updated SBC dec format");
1554fail:
1555 return is_configured;
1556}
1557
Florian Pfister1a84f312018-07-19 14:38:18 +02001558/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301559static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301560{
Florian Pfister1a84f312018-07-19 14:38:18 +02001561 void *codec_info = NULL;
1562 codec_t codec_type = CODEC_TYPE_INVALID;
1563 bool is_configured = false;
1564 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301565
Florian Pfister1a84f312018-07-19 14:38:18 +02001566 if (!a2dp.audio_get_dec_config) {
1567 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1568 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301569 }
1570
Surendar Karka2febd452018-12-13 17:56:43 +05301571 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001572 if (!ctl_dec_data) {
1573 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1574 is_configured = false;
1575 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301576 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001577 codec_info = a2dp.audio_get_dec_config(&codec_type);
1578 switch(codec_type) {
1579 case CODEC_TYPE_SBC:
1580 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301581 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001582 break;
1583 case CODEC_TYPE_AAC:
1584 ALOGD(" AAC decoder supported BT device");
1585 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301586 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001587 break;
1588 default:
1589 ALOGD(" Received Unsupported decoder format");
1590 is_configured = false;
1591 break;
1592 }
1593 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301594}
1595
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301596/* API to configure SBC DSP encoder */
1597bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1598{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301599 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301600 struct sbc_enc_cfg_t sbc_dsp_cfg;
1601 bool is_configured = false;
1602 int ret = 0;
1603
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001604 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301605 return false;
1606
1607 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1608 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001609 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301610 is_configured = false;
1611 goto fail;
1612 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301613 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001614 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301615 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1616 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1617 switch(sbc_bt_cfg->channels) {
1618 case 0:
1619 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1620 break;
1621 case 1:
1622 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1623 break;
1624 case 3:
1625 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1626 break;
1627 case 2:
1628 default:
1629 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1630 break;
1631 }
1632 if (sbc_bt_cfg->alloc)
1633 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1634 else
1635 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1636 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1637 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1638 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1639 sizeof(struct sbc_enc_cfg_t));
1640 if (ret != 0) {
1641 ALOGE("%s: failed to set SBC encoder config", __func__);
1642 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301643 goto fail;
1644 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301645 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301646 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301647 is_configured = false;
1648 goto fail;
1649 }
1650 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001651 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301652 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301653
1654 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1655 a2dp.enc_channels = 1;
1656 else
1657 a2dp.enc_channels = 2;
1658
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301659 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1660 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301661fail:
1662 return is_configured;
1663}
1664
Manish Dewangan6a252632017-12-04 17:27:44 +05301665#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301666static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1667 audio_aptx_encoder_config *aptx_bt_cfg)
1668{
1669 int ret = 0;
1670
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001671 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301672 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1673 aptx_dsp_cfg, aptx_bt_cfg);
1674 return -EINVAL;
1675 }
1676
1677 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301678 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301679
1680
1681 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1682 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1683 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1684 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1685 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1686 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1687 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1688 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1689 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301690 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301691 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1692 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1693 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1694 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1695
1696
1697 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1698 case APTX_AD_CHANNEL_UNCHANGED:
1699 case APTX_AD_CHANNEL_JOINT_STEREO:
1700 case APTX_AD_CHANNEL_DUAL_MONO:
1701 case APTX_AD_CHANNEL_STEREO_TWS:
1702 case APTX_AD_CHANNEL_EARBUD:
1703 default:
1704 a2dp.enc_channels = CH_STEREO;
1705 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1706 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1707 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1708 break;
1709 case APTX_AD_CHANNEL_MONO:
1710 a2dp.enc_channels = CH_MONO;
1711 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1712 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1713 break;
1714 }
1715 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1716 case APTX_AD_SR_UNCHANGED:
1717 case APTX_AD_48:
1718 default:
1719 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1720 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1721 break;
1722 case APTX_AD_44_1:
1723 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1724 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
1725 break;
1726 }
1727 ALOGV("Successfully updated APTX AD enc format with \
1728 samplingrate: %d channels:%d",
1729 aptx_dsp_cfg->custom_cfg.sample_rate,
1730 aptx_dsp_cfg->custom_cfg.num_channels);
1731
1732 return ret;
1733}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301734
Zhou Song8fccbb62019-03-20 01:08:19 +08001735static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
1736 audio_aptx_encoder_config *aptx_bt_cfg)
1737{
1738 int ret = 0;
1739
1740 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
1741 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1742 aptx_dsp_cfg, aptx_bt_cfg);
1743 return -EINVAL;
1744 }
1745
1746 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
1747 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
1748
1749
1750 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1751 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1752 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1753 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1754 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1755 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1756 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1757 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1758 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
1759 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
1760 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
1761 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
1762 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
1763 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
1764 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1765 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1766 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1767 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1768
1769
1770 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1771 case APTX_AD_CHANNEL_UNCHANGED:
1772 case APTX_AD_CHANNEL_JOINT_STEREO:
1773 case APTX_AD_CHANNEL_DUAL_MONO:
1774 case APTX_AD_CHANNEL_STEREO_TWS:
1775 case APTX_AD_CHANNEL_EARBUD:
1776 default:
1777 a2dp.enc_channels = CH_STEREO;
1778 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1779 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1780 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1781 break;
1782 case APTX_AD_CHANNEL_MONO:
1783 a2dp.enc_channels = CH_MONO;
1784 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1785 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1786 break;
1787 }
1788 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1789 case APTX_AD_SR_UNCHANGED:
1790 case APTX_AD_48:
1791 default:
1792 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1793 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1794 break;
1795 case APTX_AD_44_1:
1796 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1797 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
1798 break;
1799 }
1800 ALOGV("Successfully updated APTX AD enc format with \
1801 samplingrate: %d channels:%d",
1802 aptx_dsp_cfg->custom_cfg.sample_rate,
1803 aptx_dsp_cfg->custom_cfg.num_channels);
1804
1805 return ret;
1806}
1807
Manisha Agarwala51768b2018-11-01 16:30:52 +05301808static void audio_a2dp_update_tws_channel_mode()
1809{
1810 char* channel_mode;
1811 struct mixer_ctl *ctl_channel_mode;
1812 if (a2dp.is_tws_mono_mode_on)
1813 channel_mode = "One";
1814 else
1815 channel_mode = "Two";
1816 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1817 if (!ctl_channel_mode) {
1818 ALOGE("failed to get tws mixer ctl");
1819 return;
1820 }
1821 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1822 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1823 return;
1824 }
1825}
1826
Manish Dewangan6a252632017-12-04 17:27:44 +05301827static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
1828 audio_aptx_encoder_config *aptx_bt_cfg)
1829{
1830 int ret = 0;
1831
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001832 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301833 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1834 aptx_dsp_cfg, aptx_bt_cfg);
1835 return -EINVAL;
1836 }
1837
1838 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001839 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301840
1841 if (!a2dp.is_aptx_dual_mono_supported) {
1842 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1843 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1844 } else {
1845 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1846 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1847 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1848 }
1849
1850 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
1851 case 1:
1852 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1853 break;
1854 case 2:
1855 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05301856 if (!a2dp.is_tws_mono_mode_on) {
1857 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1858 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1859 }
1860 else {
1861 a2dp.is_tws_mono_mode_on = true;
1862 ALOGD("Update tws for mono_mode_on: %d",a2dp.is_tws_mono_mode_on);
Manisha Agarwala51768b2018-11-01 16:30:52 +05301863 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301864 break;
1865 }
1866 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
1867 if (!a2dp.is_aptx_dual_mono_supported) {
1868 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1869 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
1870 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
1871 aptx_dsp_cfg->custom_cfg.num_channels);
1872 } else {
1873 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1874 ALOGV("Successfully updated APTX dual mono enc format with \
1875 samplingrate: %d channels:%d syncmode %d",
1876 aptx_dsp_cfg->custom_cfg.sample_rate,
1877 aptx_dsp_cfg->custom_cfg.num_channels,
1878 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
1879 }
1880 return ret;
1881}
1882#else
1883static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
1884 audio_aptx_encoder_config *aptx_bt_cfg)
1885{
1886 int ret = 0;
1887
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001888 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301889 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1890 aptx_dsp_cfg, aptx_bt_cfg);
1891 return -EINVAL;
1892 }
1893
1894 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001895 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301896 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
1897 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
1898 switch(aptx_dsp_cfg->num_channels) {
1899 case 1:
1900 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
1901 break;
1902 case 2:
1903 default:
1904 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
1905 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
1906 break;
1907 }
1908
1909 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
1910 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
1911
1912 return ret;
1913}
1914#endif
1915
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301916/* API to configure APTX DSP encoder */
1917bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1918{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301919 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08001920 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001921 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301922 bool is_configured = false;
1923 int ret = 0;
Manish Dewangan6a252632017-12-04 17:27:44 +05301924 int sample_rate_backup;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301925
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001926 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301927 return false;
1928
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001929 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1930 if (!ctl_enc_data) {
1931 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1932 return false;
1933 }
1934
Manish Dewangan6a252632017-12-04 17:27:44 +05301935#ifndef LINUX_ENABLED
1936 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301937 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08001938 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001939 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001940 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
1941 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
1942 if (aptx_ad_ctl) {
1943 mixer_size = sizeof(struct aptx_ad_enc_cfg_r2_t);
1944 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
1945 } else {
1946 mixer_size = sizeof(struct aptx_ad_enc_cfg_t);
1947 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
1948 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001949 sample_rate_backup = aptx_ad_dsp_cfg.custom_cfg.sample_rate;
1950 } else {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301951 mixer_size = sizeof(struct aptx_enc_cfg_t);
1952 sample_rate_backup = aptx_bt_cfg->default_cfg->sampling_rate;
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001953 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
1954 }
1955 if (ret) {
1956 is_configured = false;
1957 goto fail;
1958 }
1959
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001960 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001961 if (aptx_ad_ctl)
1962 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
1963 mixer_size);
1964 else
1965 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001966 mixer_size);
1967 } else {
1968 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1969 mixer_size);
Sharad Sangle95d451b2018-06-19 12:24:20 +05301970 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301971#else
1972 struct custom_enc_cfg_t aptx_dsp_cfg;
1973 mixer_size = sizeof(struct custom_enc_cfg_t);
1974 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05301975 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05301976 if (ret) {
1977 is_configured = false;
1978 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301979 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001980 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1981 mixer_size);
1982#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301983 if (ret != 0) {
1984 ALOGE("%s: Failed to set APTX encoder config", __func__);
1985 is_configured = false;
1986 goto fail;
1987 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301988#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001989 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301990 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001991 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301992 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
1993 else
1994 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301995#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301996 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301997 is_configured = false;
1998 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301999 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302000 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302001 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05302002 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302003 else
Surendar Karka2febd452018-12-13 17:56:43 +05302004 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302005fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05302006 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002007 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05302008 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302009 return is_configured;
2010}
2011
2012/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302013 */
Manish Dewangan6a252632017-12-04 17:27:44 +05302014#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002015bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302016#else
2017bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2018#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302019{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302020 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302021 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302022 bool is_configured = false;
2023 int ret = 0;
2024
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002025 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302026 return false;
2027
2028 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2029 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002030 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302031 is_configured = false;
2032 goto fail;
2033 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302034
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302035 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002036 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302037 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2038 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2039 switch(aptx_dsp_cfg.num_channels) {
2040 case 1:
2041 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2042 break;
2043 case 2:
2044 default:
2045 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2046 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2047 break;
2048 }
2049 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302050 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302051 if (ret != 0) {
2052 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2053 is_configured = false;
2054 goto fail;
2055 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302056 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302057 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302058 is_configured = false;
2059 goto fail;
2060 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302061 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002062 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302063 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302064 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302065 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2066 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302067fail:
2068 return is_configured;
2069}
2070
2071/* API to configure AAC DSP encoder */
2072bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2073{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302074 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302075 struct aac_enc_cfg_t aac_dsp_cfg;
2076 bool is_configured = false;
2077 int ret = 0;
2078
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002079 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302080 return false;
2081
2082 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2083 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002084 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302085 is_configured = false;
2086 goto fail;
2087 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302088 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002089 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302090 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302091 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002092 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302093 case 0:
2094 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2095 break;
2096 case 2:
2097 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2098 break;
2099 case 1:
2100 default:
2101 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2102 break;
2103 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302104 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302105 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002106
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302107 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2108 sizeof(struct aac_enc_cfg_t));
2109 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002110 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302111 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302112 goto fail;
2113 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302114 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302115 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302116 is_configured = false;
2117 goto fail;
2118 }
2119 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002120 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302121 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002122 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002123 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2124 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2125fail:
2126 return is_configured;
2127}
2128
2129bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2130{
2131 struct mixer_ctl *ctl_enc_data = NULL;
2132 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2133 bool is_configured = false;
2134 int ret = 0;
2135
2136 if (aac_bt_cfg == NULL)
2137 return false;
2138
2139 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2140 if (!ctl_enc_data) {
2141 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2142 is_configured = false;
2143 goto fail;
2144 }
2145 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302146 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002147 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2148 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2149 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2150 case 0:
2151 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2152 break;
2153 case 2:
2154 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2155 break;
2156 case 1:
2157 default:
2158 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2159 break;
2160 }
2161 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2162 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2163 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2164 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2165
2166 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2167 sizeof(struct aac_enc_cfg_v2_t));
2168 if (ret != 0) {
2169 ALOGE("%s: Failed to set AAC encoder config", __func__);
2170 is_configured = false;
2171 goto fail;
2172 }
2173 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2174 if (ret != 0) {
2175 is_configured = false;
2176 goto fail;
2177 }
2178 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302179 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002180 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2181 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2182 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2183 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302184fail:
2185 return is_configured;
2186}
2187
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302188bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2189{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302190 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302191 struct celt_enc_cfg_t celt_dsp_cfg;
2192 bool is_configured = false;
2193 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002194 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302195 return false;
2196
2197 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2198 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002199 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302200 is_configured = false;
2201 goto fail;
2202 }
2203 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2204
Florian Pfister1a84f312018-07-19 14:38:18 +02002205 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302206 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2207 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2208 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2209 case 1:
2210 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2211 break;
2212 case 2:
2213 default:
2214 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2215 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2216 break;
2217 }
2218
2219 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2220
2221 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2222 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2223 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2224 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2225 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2226
2227 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2228 sizeof(struct celt_enc_cfg_t));
2229 if (ret != 0) {
2230 ALOGE("%s: Failed to set CELT encoder config", __func__);
2231 is_configured = false;
2232 goto fail;
2233 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302234 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302235 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302236 is_configured = false;
2237 goto fail;
2238 }
2239 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002240 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302241 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302242 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302243 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2244 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2245fail:
2246 return is_configured;
2247}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302248
2249bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2250{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302251 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302252 struct ldac_enc_cfg_t ldac_dsp_cfg;
2253 bool is_configured = false;
2254 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002255 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302256 return false;
2257
2258 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2259 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002260 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302261 is_configured = false;
2262 goto fail;
2263 }
2264 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2265
Florian Pfister1a84f312018-07-19 14:38:18 +02002266 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302267 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2268 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2269 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2270 case 4:
2271 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2272 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2273 break;
2274 case 2:
2275 case 1:
2276 default:
2277 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2278 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2279 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2280 break;
2281 }
2282
2283 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2284 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2285 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002286 if (ldac_bt_cfg->is_abr_enabled) {
2287 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2288 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2289 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2290 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2291 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002292 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002293 }
2294
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302295 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2296 sizeof(struct ldac_enc_cfg_t));
2297 if (ret != 0) {
2298 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2299 is_configured = false;
2300 goto fail;
2301 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302302 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302303 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302304 is_configured = false;
2305 goto fail;
2306 }
2307 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002308 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302309 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2310 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002311 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302312 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2313 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2314fail:
2315 return is_configured;
2316}
2317
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302318bool configure_a2dp_encoder_format()
2319{
2320 void *codec_info = NULL;
2321 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002322 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302323 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002324 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302325
Florian Pfister1a84f312018-07-19 14:38:18 +02002326 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302327 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2328 return false;
2329 }
2330 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002331 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302332 &codec_type);
2333
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002334 // ABR disabled by default for all codecs
2335 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302336 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002337
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302338 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002339 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302340 ALOGD(" Received SBC encoder supported BT device");
2341 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002342 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302343 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002344 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302345 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302346#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002347 a2dp.is_aptx_dual_mono_supported = false;
2348 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302349#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302350 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002351 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302352 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002353 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302354 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302355#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302356 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002357 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302358#else
2359 is_configured =
2360 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2361#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002362 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302363#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002364 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002365 ALOGD(" Received APTX dual mono encoder supported BT device");
2366 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302367 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2368 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002369 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2370 is_configured =
2371 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302372 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302373#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002374 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302375 ALOGD(" Received AAC encoder supported BT device");
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002376 bool is_aac_frame_ctl_enabled =
2377 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
2378 is_configured = is_aac_frame_ctl_enabled ?
2379 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2380 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302381 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002382 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302383 ALOGD(" Received CELT encoder supported BT device");
2384 is_configured =
2385 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2386 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002387 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302388 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002389 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2390 instance_id = MAX_INSTANCE_ID;
2391 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302392 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002393 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302394 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302395 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302396#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302397 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302398 ALOGD(" Received APTX AD encoder supported BT device");
2399 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2400 instance_id = MAX_INSTANCE_ID;
2401 a2dp.abr_config.imc_instance = instance_id--;
2402 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2403 a2dp.is_aptx_adaptive = true;
2404 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2405 is_configured =
2406 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302407 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302408 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302409#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002410 case CODEC_TYPE_PCM:
2411 ALOGD("Received PCM format for BT device");
2412 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2413 is_configured = true;
2414 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302415 default:
2416 ALOGD(" Received Unsupported encoder formar");
2417 is_configured = false;
2418 break;
2419 }
2420 return is_configured;
2421}
2422
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002423int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302424{
2425 int ret = 0;
2426
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002427 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302428
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002429 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002430 && a2dp.audio_get_enc_config)) {
2431 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302432 return -ENOSYS;
2433 }
2434
Zhou Song12c29502019-03-16 10:37:18 +08002435 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302436 //session will be restarted after suspend completion
2437 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302438 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302439 }
2440
Florian Pfister1a84f312018-07-19 14:38:18 +02002441 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302442 ALOGD("calling BT module stream start");
2443 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002444 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302445 ALOGE("BT controller start return = %d",ret);
2446 if (ret != 0 ) {
2447 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002448 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302449 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002450 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002451 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302452 ret = 0;
2453 ALOGD("Start playback successful to BT library");
2454 } else {
2455 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002456 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302457 ret = -ETIMEDOUT;
2458 }
2459 }
2460 }
2461
Florian Pfister1a84f312018-07-19 14:38:18 +02002462 if (a2dp.a2dp_source_started) {
2463 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302464 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302465 audio_a2dp_update_tws_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002466 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002467 if (a2dp.abr_config.is_abr_enabled)
2468 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302469 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302470
2471 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002472 a2dp.a2dp_source_total_active_session_requests);
2473 return ret;
2474}
2475
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002476uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002477{
2478 uint32_t latency = 0;
2479
2480 switch(a2dp.bt_decoder_format) {
2481 case CODEC_TYPE_SBC:
2482 latency = DEFAULT_SINK_LATENCY_SBC;
2483 break;
2484 case CODEC_TYPE_AAC:
2485 latency = DEFAULT_SINK_LATENCY_AAC;
2486 break;
2487 default:
2488 latency = 200;
2489 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2490 break;
2491 }
2492 return (uint64_t)latency;
2493}
2494
2495bool a2dp_send_sink_setup_complete(void) {
2496 uint64_t system_latency = 0;
2497 bool is_complete = false;
2498
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002499 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002500
2501 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2502 is_complete = true;
2503 }
2504 return is_complete;
2505}
2506
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002507bool a2dp_sink_is_ready()
2508{
2509 bool ret = false;
2510
2511 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2512 (a2dp.is_a2dp_offload_supported) &&
2513 (a2dp.audio_sink_check_a2dp_ready))
2514 ret = a2dp.audio_sink_check_a2dp_ready();
2515 return ret;
2516}
2517
2518int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002519{
2520 int ret = 0;
2521
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002522 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002523
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002524 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002525 && a2dp.audio_get_dec_config)) {
2526 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2527 return -ENOSYS;
2528 }
2529
2530 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2531 ALOGD("calling BT module stream start");
2532 /* This call indicates BT IPC lib to start capture */
2533 ret = a2dp.audio_sink_start();
2534 ALOGE("BT controller start capture return = %d",ret);
2535 if (ret != 0 ) {
2536 ALOGE("BT controller start capture failed");
2537 a2dp.a2dp_sink_started = false;
2538 } else {
2539
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002540 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002541 ALOGD("Wait for capture ready not successful");
2542 ret = -ETIMEDOUT;
2543 }
2544
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002545 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002546 a2dp.a2dp_sink_started = true;
2547 ret = 0;
2548 ALOGD("Start capture successful to BT library");
2549 } else {
2550 ALOGD(" unable to configure DSP decoder");
2551 a2dp.a2dp_sink_started = false;
2552 ret = -ETIMEDOUT;
2553 }
2554
2555 if (!a2dp_send_sink_setup_complete()) {
2556 ALOGD("sink_setup_complete not successful");
2557 ret = -ETIMEDOUT;
2558 }
2559 }
2560 }
2561
2562 if (a2dp.a2dp_sink_started) {
2563 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002564 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002565 }
2566 }
2567
2568 ALOGD("start A2DP sink total active sessions :%d",
2569 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302570 return ret;
2571}
2572
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302573static void reset_a2dp_enc_config_params()
2574{
2575 int ret =0;
2576
Aalique Grahame22e49102018-12-18 14:23:57 -08002577 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302578 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302579 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302580
2581 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
2582 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
2583 MIXER_ENC_CONFIG_BLOCK);
2584 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002585 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302586 } else {
2587 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
2588 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002589 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302590 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002591
2592 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2593
Manisha Agarwala51768b2018-11-01 16:30:52 +05302594 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2595
2596 if (!ctl_channel_mode) {
2597 ALOGE("failed to get tws mixer ctl");
2598 } else {
2599 channel_mode = "Two";
2600 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2601 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2602 }
2603 a2dp.is_tws_mono_mode_on = false;
2604 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302605}
2606
Surendar Karka2febd452018-12-13 17:56:43 +05302607static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002608{
2609 struct mixer_ctl *ctl_dec_data = NULL;
2610 struct abr_dec_cfg_t dummy_reset_cfg;
2611 int ret = 0;
2612
2613 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05302614 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002615 if (!ctl_dec_data) {
2616 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
2617 return -EINVAL;
2618 }
2619 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
2620 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
2621 sizeof(dummy_reset_cfg));
2622 if (ret != 0) {
2623 ALOGE("%s: Failed to set dummy decoder config", __func__);
2624 return ret;
2625 }
2626 }
2627
2628 return ret;
2629}
2630
Surendar Karka2febd452018-12-13 17:56:43 +05302631static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02002632{
2633 int ret =0;
2634
2635 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
2636 struct aac_dec_cfg_t dummy_reset_config;
2637
2638 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
2639 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05302640 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02002641 if (!ctl_dec_config) {
2642 ALOGE(" ERROR a2dp decoder format mixer control not identified");
2643 } else {
2644 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
2645 sizeof(struct aac_dec_cfg_t));
2646 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
2647 }
2648 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
2649 MIXER_DEC_BIT_FORMAT);
2650 if (!ctrl_bit_format) {
2651 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
2652 } else {
2653 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
2654 if (ret != 0) {
2655 ALOGE("%s: Failed to set bit format to decoder", __func__);
2656 }
2657 }
2658}
2659
Zhou Song12c29502019-03-16 10:37:18 +08002660static void reset_codec_config()
2661{
2662 reset_a2dp_enc_config_params();
2663 reset_a2dp_source_dec_config_params();
2664 a2dp_reset_backend_cfg(SOURCE);
2665 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
2666 stop_abr();
2667 a2dp.abr_config.is_abr_enabled = false;
2668}
2669
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002670int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302671{
2672 int ret =0;
2673
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002674 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002675 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002676 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302677 return -ENOSYS;
2678 }
2679
Florian Pfister1a84f312018-07-19 14:38:18 +02002680 if (a2dp.a2dp_source_total_active_session_requests > 0)
2681 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08002682 else
2683 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302684
Florian Pfister1a84f312018-07-19 14:38:18 +02002685 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302686 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02002687 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302688 if (ret < 0)
2689 ALOGE("stop stream to BT IPC lib failed");
2690 else
2691 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08002692 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
2693 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05302694 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302695 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002696 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002697 a2dp.a2dp_source_started = false;
2698 ALOGD("Stop A2DP playback, total active sessions :%d",
2699 a2dp.a2dp_source_total_active_session_requests);
2700 return 0;
2701}
2702
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002703int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002704{
2705 int ret =0;
2706
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002707 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002708 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002709 ALOGE("a2dp handle is not identified, Ignoring stop request");
2710 return -ENOSYS;
2711 }
2712
2713 if (a2dp.a2dp_sink_total_active_session_requests > 0)
2714 a2dp.a2dp_sink_total_active_session_requests--;
2715
2716 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2717 ALOGV("calling BT module stream stop");
2718 ret = a2dp.audio_sink_stop();
2719 if (ret < 0)
2720 ALOGE("stop stream to BT IPC lib failed");
2721 else
2722 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05302723 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002724 a2dp_reset_backend_cfg(SINK);
2725 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002726 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002727 a2dp.a2dp_source_started = false;
2728 ALOGD("Stop A2DP capture, total active sessions :%d",
2729 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302730 return 0;
2731}
2732
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002733int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302734{
Aalique Grahame22e49102018-12-18 14:23:57 -08002735 int ret = 0, val, status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302736 char value[32]={0};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302737 struct audio_usecase *uc_info;
2738 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302739
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002740 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002741 ALOGV("no supported encoders identified,ignoring a2dp setparam");
2742 status = -EINVAL;
2743 goto param_handled;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302744 }
2745
2746 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
2747 sizeof(value));
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 device connect request for A2DP source");
2752 open_a2dp_source();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302753 }
2754 goto param_handled;
2755 }
2756
2757 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
2758 sizeof(value));
2759
Zhou Song681350a2017-10-19 16:28:42 +08002760 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302761 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002762 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002763 ALOGV("Received source device dis- connect request");
Samyak Jain4828f4c2018-08-24 16:31:06 +05302764 close_a2dp_output();
kunleiz4a1fad62018-02-08 18:00:16 +08002765 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302766 reset_a2dp_source_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002767 a2dp_reset_backend_cfg(SOURCE);
2768 } else if (audio_is_a2dp_in_device(val)) {
2769 ALOGV("Received sink device dis- connect request");
2770 close_a2dp_input();
Surendar Karka2febd452018-12-13 17:56:43 +05302771 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002772 a2dp_reset_backend_cfg(SINK);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302773 }
2774 goto param_handled;
2775 }
Surendar Karka2febd452018-12-13 17:56:43 +05302776#ifndef LINUX_ENABLED
Manisha Agarwala51768b2018-11-01 16:30:52 +05302777 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
2778 if (ret>=0) {
2779 ALOGD("Setting tws channel mode to %s",value);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002780 if (!(strncmp(value,"mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302781 a2dp.is_tws_mono_mode_on = true;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002782 else if (!(strncmp(value,"dual-mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302783 a2dp.is_tws_mono_mode_on = false;
2784 audio_a2dp_update_tws_channel_mode();
2785 goto param_handled;
2786 }
Surendar Karka2febd452018-12-13 17:56:43 +05302787#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302788 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
2789 if (ret >= 0) {
Surendar Karka2febd452018-12-13 17:56:43 +05302790 if (a2dp.bt_lib_source_handle) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302791 if ((!strncmp(value,"true",sizeof(value)))) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002792 if (a2dp.a2dp_source_suspended) {
2793 ALOGD("%s: A2DP is already suspended", __func__);
2794 goto param_handled;
2795 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302796 ALOGD("Setting a2dp to suspend state");
Florian Pfister1a84f312018-07-19 14:38:18 +02002797 a2dp.a2dp_source_suspended = true;
Surendar Karka2febd452018-12-13 17:56:43 +05302798 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
yidongh7203cca2018-09-19 16:12:25 +08002799 goto param_handled;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302800 list_for_each(node, &a2dp.adev->usecase_list) {
2801 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302802 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08002803 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302804 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002805 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302806 pthread_mutex_lock(&a2dp.adev->lock);
2807 }
2808 }
Zhou Song12c29502019-03-16 10:37:18 +08002809 if (!a2dp.swb_configured)
2810 reset_codec_config();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002811 if (a2dp.audio_source_suspend)
Florian Pfister1a84f312018-07-19 14:38:18 +02002812 a2dp.audio_source_suspend();
2813 } else if (a2dp.a2dp_source_suspended == true) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302814 ALOGD("Resetting a2dp suspend state");
Zhou Song10617ed2017-05-26 13:28:48 +08002815 struct audio_usecase *uc_info;
2816 struct listnode *node;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002817 if (a2dp.clear_source_a2dpsuspend_flag)
Florian Pfister1a84f312018-07-19 14:38:18 +02002818 a2dp.clear_source_a2dpsuspend_flag();
2819 a2dp.a2dp_source_suspended = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302820 /*
2821 * It is possible that before suspend,a2dp sessions can be active
2822 * for example during music + voice activation concurrency
2823 * a2dp suspend will be called & BT will change to sco mode
2824 * though music is paused as a part of voice activation
2825 * compress session close happens only after pause timeout(10secs)
2826 * so if resume request comes before pause timeout as a2dp session
2827 * is already active IPC start will not be called from APM/audio_hw
2828 * Fix is to call a2dp start for IPC library post suspend
2829 * based on number of active session count
2830 */
Florian Pfister1a84f312018-07-19 14:38:18 +02002831 if (a2dp.a2dp_source_total_active_session_requests > 0) {
Naresh Tanniru649871a2016-11-04 18:08:32 +05302832 ALOGD(" Calling IPC lib start post suspend state");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002833 if (a2dp.audio_source_start) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002834 ret = a2dp.audio_source_start();
Naresh Tanniru649871a2016-11-04 18:08:32 +05302835 if (ret != 0) {
2836 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002837 a2dp.a2dp_source_started = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302838 }
2839 }
2840 }
Zhou Song10617ed2017-05-26 13:28:48 +08002841 list_for_each(node, &a2dp.adev->usecase_list) {
2842 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302843 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08002844 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302845 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002846 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302847 pthread_mutex_lock(&a2dp.adev->lock);
2848 }
Zhou Song10617ed2017-05-26 13:28:48 +08002849 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302850 }
2851 }
2852 goto param_handled;
2853 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002854
2855 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
2856 sizeof(value));
2857 if (ret >= 0) {
2858 if (a2dp.is_a2dp_offload_supported &&
2859 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2860 *reconfig = true;
2861 }
2862 goto param_handled;
2863 }
2864
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302865param_handled:
2866 ALOGV("end of a2dp setparam");
Aalique Grahame22e49102018-12-18 14:23:57 -08002867 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302868}
2869
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002870void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302871{
2872 a2dp.is_handoff_in_progress = is_on;
2873}
2874
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002875bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302876{
2877 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05302878 // Or if a2dp device is selected but earlier start failed ( as a2dp
2879 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02002880 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302881}
2882
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002883void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302884{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302885 *sample_rate = a2dp.enc_sampling_rate;
2886}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302887
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002888void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02002889{
2890 *sample_rate = a2dp.dec_sampling_rate;
2891}
2892
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002893bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302894{
2895 bool ret = false;
2896
Florian Pfister1a84f312018-07-19 14:38:18 +02002897 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302898 return ret;
2899
Florian Pfister1a84f312018-07-19 14:38:18 +02002900 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08002901 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002902 (a2dp.audio_source_check_a2dp_ready))
2903 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302904 return ret;
2905}
2906
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002907bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02002908{
2909 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302910}
2911
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002912void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08002913 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302914{
2915 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02002916 a2dp.bt_lib_source_handle = NULL;
2917 a2dp.a2dp_source_started = false;
2918 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
2919 a2dp.a2dp_source_total_active_session_requests = 0;
2920 a2dp.a2dp_source_suspended = false;
2921 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302922 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302923 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002924 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302925 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002926 a2dp.abr_config.is_abr_enabled = false;
2927 a2dp.abr_config.abr_started = false;
2928 a2dp.abr_config.imc_instance = 0;
2929 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08002930 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302931 a2dp.is_tws_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05302932 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08002933 a2dp.swb_configured = false;
2934
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002935 // init function pointers
2936 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08002937 init_config.fp_platform_get_pcm_device_id;
2938 fp_check_a2dp_restore = init_config.fp_check_a2dp_restore;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002939
kunleiz5a127262017-09-08 14:47:48 +08002940 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302941 reset_a2dp_source_dec_config_params();
2942 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002943
2944 a2dp.bt_lib_sink_handle = NULL;
2945 a2dp.a2dp_sink_started = false;
2946 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
2947 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002948
2949 if (is_running_with_enhanced_fwk == UNINITIALIZED)
2950 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
2951 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08002952 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02002953
2954 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302955 update_offload_codec_capabilities();
2956}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002957
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002958uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002959{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002960 uint32_t latency = 0;
2961 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302962 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
2963 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002964 char value[PROPERTY_VALUE_MAX];
2965
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002966 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08002967 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002968 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302969 if (sscanf(value, "%d/%d/%d/%d/%d%d",
2970 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002971 ALOGI("Failed to parse avsync offset params from '%s'.", value);
2972 avsync_runtime_prop = 0;
2973 }
2974 }
2975
yidongh0515e042017-07-06 15:00:34 +08002976 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02002977 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2978 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08002979 }
2980
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07002981 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002982 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08002983 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
2984 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002985 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002986 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08002987 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
2988 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002989 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002990 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08002991 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
2992 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002993 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002994 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08002995 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
2996 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002997 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002998 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302999 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
3000 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
3001 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003002 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303003 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
3004 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
3005 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05303006 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05303007 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05303008 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08003009 case CODEC_TYPE_PCM:
3010 latency = ENCODER_LATENCY_PCM;
3011 latency += DEFAULT_SINK_LATENCY_PCM;
3012 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003013 default:
3014 latency = 200;
3015 break;
3016 }
3017 return latency;
3018}
Aalique Grahame22e49102018-12-18 14:23:57 -08003019
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003020int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003021 struct str_parms *reply)
3022{
3023 int ret, val = 0;
3024 char value[32]={0};
3025
3026 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3027 value, sizeof(value));
3028 if (ret >= 0) {
3029 val = a2dp.is_a2dp_offload_supported;
3030 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3031 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3032 }
3033
3034 return 0;
3035}
Zhou Song12c29502019-03-16 10:37:18 +08003036
3037
3038bool configure_aptx_ad_speech_enc_fmt() {
3039 struct mixer_ctl *ctl_enc_data = NULL;
3040 int mixer_size = 0;
3041 int ret = 0;
3042 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3043
3044 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3045 if (!ctl_enc_data) {
3046 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3047 return false;
3048 }
3049
3050 /* Initialize dsp configuration params */
3051 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3052 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3053 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3054 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3055 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3056 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3057 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3058 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3059 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3060 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3061 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3062
3063 /* Configure AFE DSP configuration */
3064 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3065 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3066 mixer_size);
3067 if (ret != 0) {
3068 ALOGE("%s: Failed to set SWB encoder config", __func__);
3069 return false;
3070 }
3071
3072 /* Configure AFE Input Bit Format as PCM_16 */
3073 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3074 if (ret != 0) {
3075 ALOGE("%s: Failed to set SWB bit format", __func__);
3076 return false;
3077 }
3078
3079 return true;
3080}
3081
3082bool configure_aptx_ad_speech_dec_fmt()
3083{
3084 struct mixer_ctl *ctl_dec_data = NULL;
3085 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3086 int ret = 0;
3087
3088 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3089 if (!ctl_dec_data) {
3090 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3091 return false;
3092 }
3093 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3094 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3095 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3096 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3097 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3098 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3099 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3100 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3101
3102 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3103 sizeof(dec_cfg));
3104 if (ret != 0) {
3105 ALOGE("%s: Failed to set decoder config", __func__);
3106 return false;
3107 }
3108 return true;
3109}
3110
3111int sco_start_configuration()
3112{
3113 ALOGD("sco_start_configuration start");
3114
3115 if (!a2dp.swb_configured) {
3116 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3117 /* Configure AFE codec*/
3118 if (configure_aptx_ad_speech_enc_fmt() &&
3119 configure_aptx_ad_speech_dec_fmt()) {
3120 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3121 } else {
3122 ALOGE("%s: failed to send SCO configuration", __func__);
3123 return -ETIMEDOUT;
3124 }
3125 /* Configure backend*/
3126 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3127 a2dp.enc_channels = CH_MONO;
3128 a2dp.abr_config.is_abr_enabled = true;
3129 a2dp_set_backend_cfg(SOURCE);
3130 /* Start abr*/
3131 start_abr();
3132 a2dp.swb_configured = true;
3133 }
3134 return 0;
3135}
3136
3137void sco_reset_configuration()
3138{
Zhou Songd6d71752019-05-21 18:08:51 +08003139 if (a2dp.swb_configured) {
3140 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003141
Zhou Songd6d71752019-05-21 18:08:51 +08003142 reset_codec_config();
3143 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3144 a2dp.swb_configured = false;
3145 }
Zhou Song12c29502019-03-16 10:37:18 +08003146}