blob: f4875118865e8db48ec6fc0efa3572b82243c817 [file] [log] [blame]
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301/*
Aalique Grahame22e49102018-12-18 14:23:57 -08002* Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of The Linux Foundation nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
Aalique Grahame22e49102018-12-18 14:23:57 -080029#define LOG_TAG "a2dp_offload"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053030/*#define LOG_NDEBUG 0*/
31#define LOG_NDDEBUG 0
32#include <errno.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080033#include <log/log.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053034#include <dlfcn.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080035#include <pthread.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
Manish Dewangan6a252632017-12-04 17:27:44 +053039#include "audio_extn.h"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053040#include <stdlib.h>
41#include <cutils/str_parms.h>
42#include <hardware/audio.h>
43#include <hardware/hardware.h>
44#include <cutils/properties.h>
45
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046#ifdef DYNAMIC_LOG_ENABLED
47#include <log_xml_parser.h>
48#define LOG_MASK HAL_MOD_FILE_A2DP
49#include <log_utils.h>
50#endif
51
Naresh Tanniru9d027a62015-03-13 01:32:10 +053052#define AUDIO_PARAMETER_A2DP_STARTED "A2dpStarted"
Naresh Tanniru66cf06c2019-03-20 19:30:37 +053053#define BT_IPC_SOURCE_LIB_NAME "btaudio_offload_if.so"
Florian Pfister1a84f312018-07-19 14:38:18 +020054#define BT_IPC_SINK_LIB_NAME "libbthost_if_sink.so"
55#define MEDIA_FMT_NONE 0
56#define MEDIA_FMT_AAC 0x00010DA6
57#define MEDIA_FMT_APTX 0x000131ff
58#define MEDIA_FMT_APTX_HD 0x00013200
Surendar Karka2febd452018-12-13 17:56:43 +053059#define MEDIA_FMT_APTX_AD 0x00013204
Florian Pfister1a84f312018-07-19 14:38:18 +020060#define MEDIA_FMT_SBC 0x00010BF2
61#define MEDIA_FMT_CELT 0x00013221
62#define MEDIA_FMT_LDAC 0x00013224
63#define MEDIA_FMT_MP3 0x00010BE9
64#define MEDIA_FMT_APTX_ADAPTIVE 0x00013204
Zhou Song12c29502019-03-16 10:37:18 +080065#define MEDIA_FMT_APTX_AD_SPEECH 0x00013208
Naresh Tanniru9d027a62015-03-13 01:32:10 +053066#define MEDIA_FMT_AAC_AOT_LC 2
67#define MEDIA_FMT_AAC_AOT_SBR 5
68#define MEDIA_FMT_AAC_AOT_PS 29
Naresh Tanniru9d027a62015-03-13 01:32:10 +053069#define PCM_CHANNEL_L 1
70#define PCM_CHANNEL_R 2
71#define PCM_CHANNEL_C 3
72#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
73#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
74#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
75#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
76#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
77#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
Surendar Karka2febd452018-12-13 17:56:43 +053078#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
Zhou Song8fccbb62019-03-20 01:08:19 +080079#define MIXER_ENC_APTX_AD_CONFIG_BLOCK "SLIM_7_RX APTX_AD Enc Cfg"
Surendar Karka2febd452018-12-13 17:56:43 +053080#define MIXER_SOURCE_DEC_CONFIG_BLOCK "SLIM_7_TX Decoder Config"
81#define MIXER_SINK_DEC_CONFIG_BLOCK "SLIM_9_TX Decoder Config"
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +053082#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
Florian Pfister1a84f312018-07-19 14:38:18 +020083#define MIXER_DEC_BIT_FORMAT "AFE Output Bit Format"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053084#define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080085#define MIXER_SAMPLE_RATE_RX "BT SampleRate RX"
Surendar Karka2febd452018-12-13 17:56:43 +053086#define MIXER_SOURCE_SAMPLE_RATE_TX "BT SampleRate TX"
Samyak Jainf69e9ef2018-06-12 12:26:37 +053087#define MIXER_SAMPLE_RATE_DEFAULT "BT SampleRate"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053088#define MIXER_AFE_IN_CHANNELS "AFE Input Channels"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080089#define MIXER_ABR_TX_FEEDBACK_PATH "A2DP_SLIM7_UL_HL Switch"
Zhou Song12c29502019-03-16 10:37:18 +080090#define MIXER_ABR_RX_FEEDBACK_PATH "SCO_SLIM7_DL_HL Switch"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080091#define MIXER_SET_FEEDBACK_CHANNEL "BT set feedback channel"
Surendar Karka2febd452018-12-13 17:56:43 +053092#define MIXER_SINK_SAMPLE_RATE "BT_TX SampleRate"
Florian Pfister1a84f312018-07-19 14:38:18 +020093#define MIXER_AFE_SINK_CHANNELS "AFE Output Channels"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053094#define MIXER_ENC_FMT_SBC "SBC"
95#define MIXER_ENC_FMT_AAC "AAC"
96#define MIXER_ENC_FMT_APTX "APTX"
Manisha Agarwala51768b2018-11-01 16:30:52 +053097#define MIXER_FMT_TWS_CHANNEL_MODE "TWS Channel Mode"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053098#define MIXER_ENC_FMT_APTXHD "APTXHD"
Aalique Grahame22e49102018-12-18 14:23:57 -080099#define MIXER_END_FMT_LDAC "LDAC"
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530100#define MIXER_ENC_FMT_NONE "NONE"
yidongh0515e042017-07-06 15:00:34 +0800101#define ENCODER_LATENCY_SBC 10
102#define ENCODER_LATENCY_APTX 40
103#define ENCODER_LATENCY_APTX_HD 20
104#define ENCODER_LATENCY_AAC 70
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530105//To Do: Fine Tune Encoder CELT/LDAC latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530106#define ENCODER_LATENCY_CELT 40
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530107#define ENCODER_LATENCY_LDAC 40
Aalique Grahame22e49102018-12-18 14:23:57 -0800108#define ENCODER_LATENCY_PCM 50
yidongh0515e042017-07-06 15:00:34 +0800109#define DEFAULT_SINK_LATENCY_SBC 140
110#define DEFAULT_SINK_LATENCY_APTX 160
111#define DEFAULT_SINK_LATENCY_APTX_HD 180
112#define DEFAULT_SINK_LATENCY_AAC 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530113//To Do: Fine Tune Default CELT/LDAC Latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530114#define DEFAULT_SINK_LATENCY_CELT 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530115#define DEFAULT_SINK_LATENCY_LDAC 180
Aalique Grahame22e49102018-12-18 14:23:57 -0800116#define DEFAULT_SINK_LATENCY_PCM 140
117
118#define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported"
119#define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled"
120#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
121
122// Default encoder bit width
123#define DEFAULT_ENCODER_BIT_FORMAT 16
124
125// Default encoder latency
126#define DEFAULT_ENCODER_LATENCY 200
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530127
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800128// Slimbus Tx sample rate for ABR feedback channel
129#define ABR_TX_SAMPLE_RATE "KHZ_8"
130
Zhou Song12c29502019-03-16 10:37:18 +0800131// Slimbus Tx sample rate for APTX AD SPEECH
132#define SPEECH_TX_SAMPLE_RATE "KHZ_96"
133
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800134// Purpose ID for Inter Module Communication (IMC) in AFE
135#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
136
137// Maximum quality levels for ABR
138#define MAX_ABR_QUALITY_LEVELS 5
139
140// Instance identifier for A2DP
141#define MAX_INSTANCE_ID (UINT32_MAX / 2)
142
Zhou Song12c29502019-03-16 10:37:18 +0800143// Instance identifier for SWB
144#define APTX_AD_SPEECH_INSTANCE_ID 37
145
146#define SAMPLING_RATE_96K 96000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530147#define SAMPLING_RATE_48K 48000
148#define SAMPLING_RATE_441K 44100
Zhou Song12c29502019-03-16 10:37:18 +0800149#define SAMPLING_RATE_32K 32000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530150#define CH_STEREO 2
151#define CH_MONO 1
Florian Pfister1a84f312018-07-19 14:38:18 +0200152#define SOURCE 0
153#define SINK 1
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800154#define UNINITIALIZED -1
155
156#ifdef __LP64__
157#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
158#else
159#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
160#endif
161
162static void *vndk_fwk_lib_handle = NULL;
163static int is_running_with_enhanced_fwk = UNINITIALIZED;
164
165typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
166static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
Florian Pfister1a84f312018-07-19 14:38:18 +0200167
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530168/*
169 * Below enum values are extended from audio_base.h to
Florian Pfister1a84f312018-07-19 14:38:18 +0200170 * to keep encoder and decoder type local to bthost_ipc
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530171 * and audio_hal as these are intended only for handshake
172 * between IPC lib and Audio HAL.
173 */
174typedef enum {
Florian Pfister1a84f312018-07-19 14:38:18 +0200175 CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
176 CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
177 CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
178 CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
179 CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530180#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +0200181 CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530182#endif
Florian Pfister1a84f312018-07-19 14:38:18 +0200183 CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
184 CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
Surendar Karka2febd452018-12-13 17:56:43 +0530185 CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
Zhou Song12c29502019-03-16 10:37:18 +0800186 CODEC_TYPE_APTX_AD_SPEECH = 637534208u, //0x26000000UL
Aalique Grahame22e49102018-12-18 14:23:57 -0800187 CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u
Florian Pfister1a84f312018-07-19 14:38:18 +0200188}codec_t;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530189
Sharad Sangle95d451b2018-06-19 12:24:20 +0530190/*
191 * enums which describes the APTX Adaptive
192 * channel mode, these values are used by encoder
193 */
194 typedef enum {
195 APTX_AD_CHANNEL_UNCHANGED = -1,
196 APTX_AD_CHANNEL_JOINT_STEREO = 0, // default
197 APTX_AD_CHANNEL_MONO = 1,
198 APTX_AD_CHANNEL_DUAL_MONO = 2,
199 APTX_AD_CHANNEL_STEREO_TWS = 4,
200 APTX_AD_CHANNEL_EARBUD = 8,
201} enc_aptx_ad_channel_mode;
202
203/*
204 * enums which describes the APTX Adaptive
205 * sampling frequency, these values are used
206 * by encoder
207 */
208typedef enum {
209 APTX_AD_SR_UNCHANGED = 0x0,
210 APTX_AD_48 = 0x1, // 48 KHz default
211 APTX_AD_44_1 = 0x2, // 44.1kHz
212} enc_aptx_ad_s_rate;
213
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530214typedef void (*bt_audio_pre_init_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200215typedef int (*audio_source_open_t)(void);
216typedef int (*audio_source_close_t)(void);
217typedef int (*audio_source_start_t)(void);
218typedef int (*audio_source_stop_t)(void);
219typedef int (*audio_source_suspend_t)(void);
220typedef void (*audio_source_handoff_triggered_t)(void);
221typedef void (*clear_source_a2dpsuspend_flag_t)(void);
222typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
223 uint8_t *num_dev, codec_t *codec_type);
224typedef int (*audio_source_check_a2dp_ready_t)(void);
225typedef int (*audio_is_source_scrambling_enabled_t)(void);
Manisha Agarwala51768b2018-11-01 16:30:52 +0530226typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200227typedef int (*audio_sink_start_t)(void);
228typedef int (*audio_sink_stop_t)(void);
229typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
230typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
231typedef int (*audio_sink_check_a2dp_ready_t)(void);
232typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530233
234enum A2DP_STATE {
235 A2DP_STATE_CONNECTED,
236 A2DP_STATE_STARTED,
237 A2DP_STATE_STOPPED,
238 A2DP_STATE_DISCONNECTED,
239};
240
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800241typedef enum {
242 IMC_TRANSMIT,
243 IMC_RECEIVE,
244} imc_direction_t;
245
246typedef enum {
247 IMC_DISABLE,
248 IMC_ENABLE,
249} imc_status_t;
250
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700251typedef enum {
Zhou Song12c29502019-03-16 10:37:18 +0800252 SWAP_DISABLE,
253 SWAP_ENABLE,
254} swap_status_t;
255
256typedef enum {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700257 MTU_SIZE,
258 PEAK_BIT_RATE,
259} frame_control_type_t;
260
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800261// --- external function dependency ---
262fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
263fp_check_a2dp_restore_t fp_check_a2dp_restore;
264
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800265/* PCM config for ABR Feedback hostless front end */
266static struct pcm_config pcm_config_abr = {
267 .channels = 1,
268 .rate = 8000,
269 .period_size = 240,
270 .period_count = 2,
271 .format = PCM_FORMAT_S16_LE,
272 .start_threshold = 0,
273 .stop_threshold = INT_MAX,
274 .avail_min = 0,
275};
276
277/* Adaptive bitrate config for A2DP codecs */
278struct a2dp_abr_config {
279 /* Flag to denote whether Adaptive bitrate is enabled for codec */
280 bool is_abr_enabled;
281 /* Flag to denote whether front end has been opened for ABR */
282 bool abr_started;
283 /* ABR Tx path pcm handle */
284 struct pcm *abr_tx_handle;
Zhou Song12c29502019-03-16 10:37:18 +0800285 /* ABR Rx path pcm handle */
286 struct pcm *abr_rx_handle;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800287 /* ABR Inter Module Communication (IMC) instance ID */
288 uint32_t imc_instance;
289};
290
291static uint32_t instance_id = MAX_INSTANCE_ID;
292
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530293/* structure used to update a2dp state machine
294 * to communicate IPC library
295 * to store DSP encoder configuration information
296 */
297struct a2dp_data {
298 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200299 void *bt_lib_source_handle;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530300 bt_audio_pre_init_t bt_audio_pre_init;
Florian Pfister1a84f312018-07-19 14:38:18 +0200301 audio_source_open_t audio_source_open;
302 audio_source_close_t audio_source_close;
303 audio_source_start_t audio_source_start;
304 audio_source_stop_t audio_source_stop;
305 audio_source_suspend_t audio_source_suspend;
306 audio_source_handoff_triggered_t audio_source_handoff_triggered;
307 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
308 audio_get_enc_config_t audio_get_enc_config;
309 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530310 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200311 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
312 enum A2DP_STATE bt_state_source;
313 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530314 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530315 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200316 bool a2dp_source_started;
317 bool a2dp_source_suspended;
318 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530319 bool is_a2dp_offload_supported;
320 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700321 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530322 /* Mono Mode support for TWS+ */
323 bool is_tws_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530324 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800325 /* Adaptive bitrate config for A2DP codecs */
326 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200327
328 void *bt_lib_sink_handle;
329 audio_sink_start_t audio_sink_start;
330 audio_sink_stop_t audio_sink_stop;
331 audio_get_dec_config_t audio_get_dec_config;
332 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
333 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
334 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
335 enum A2DP_STATE bt_state_sink;
336 codec_t bt_decoder_format;
337 uint32_t dec_sampling_rate;
338 uint32_t dec_channels;
339 bool a2dp_sink_started;
340 int a2dp_sink_total_active_session_requests;
Zhou Song12c29502019-03-16 10:37:18 +0800341 bool swb_configured;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530342};
343
344struct a2dp_data a2dp;
345
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800346/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
347 * Structures sent to configure DSP for ABR are defined below.
348 * This data helps DSP configure feedback path (BTSoC to LPASS)
349 * for link quality levels and mapping quality levels to codec
350 * specific bitrate.
351 */
352
353/* Key value pair for link quality level to bitrate mapping. */
354struct bit_rate_level_map_t {
355 uint32_t link_quality_level;
356 uint32_t bitrate;
357};
358
359/* Link quality level to bitrate mapping info sent to DSP. */
360struct quality_level_to_bitrate_info {
361 /* Number of quality levels being mapped.
362 * This will be equal to the size of mapping table.
363 */
364 uint32_t num_levels;
365 /* Quality level to bitrate mapping table */
366 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
367};
368
369/* Structure to set up Inter Module Communication (IMC) between
370 * AFE Decoder and Encoder.
371 */
372struct imc_dec_enc_info {
373 /* Decoder to encoder communication direction.
374 * Transmit = 0 / Receive = 1
375 */
376 uint32_t direction;
377 /* Enable / disable IMC between decoder and encoder */
378 uint32_t enable;
379 /* Purpose of IMC being set up between decoder and encoder.
380 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
381 * is the default value to be sent as purpose.
382 */
383 uint32_t purpose;
384 /* Unique communication instance ID.
385 * purpose and comm_instance together form the actual key
386 * used in IMC registration, which must be the same for
387 * encoder and decoder for which IMC is being set up.
388 */
389 uint32_t comm_instance;
390};
391
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700392/* Structure to control frame size of AAC encoded frames. */
393struct aac_frame_size_control_t {
394 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
395 uint32_t ctl_type;
396 /* Control value
397 * MTU_SIZE: MTU size in bytes
398 * PEAK_BIT_RATE: Peak bitrate in bits per second.
399 */
400 uint32_t ctl_value;
401};
402
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800403/* Structure used for ABR config of AFE encoder and decoder. */
404struct abr_enc_cfg_t {
405 /* Link quality level to bitrate mapping info sent to DSP. */
406 struct quality_level_to_bitrate_info mapping_info;
407 /* Information to set up IMC between decoder and encoder */
408 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700409 /* Flag to indicate whether ABR is enabled */
410 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800411} __attribute__ ((packed));
412
413/* Structure to send configuration for decoder introduced
414 * on AFE Tx path for ABR link quality feedback to BT encoder.
415 */
416struct abr_dec_cfg_t {
417 /* Decoder media format */
418 uint32_t dec_format;
419 /* Information to set up IMC between decoder and encoder */
420 struct imc_dec_enc_info imc_info;
421} __attribute__ ((packed));
422
Zhou Song12c29502019-03-16 10:37:18 +0800423struct aptx_ad_speech_mode_cfg_t
424{
425 uint32_t mode;
426 uint32_t swapping;
427} __attribute__ ((packed));
428
429/* Structure for SWB voice dec config */
430struct aptx_ad_speech_dec_cfg_t {
431 struct abr_dec_cfg_t abr_cfg;
432 struct aptx_ad_speech_mode_cfg_t speech_mode;
433} __attribute__ ((packed));
434
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530435/* START of DSP configurable structures
436 * These values should match with DSP interface defintion
437 */
438
439/* AAC encoder configuration structure. */
440typedef struct aac_enc_cfg_t aac_enc_cfg_t;
441
442/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
443 * supported aac_fmt_flag are ADTS/RAW
444 * supported channel_cfg are Native mode, Mono , Stereo
445 */
446struct aac_enc_cfg_t {
447 uint32_t enc_format;
448 uint32_t bit_rate;
449 uint32_t enc_mode;
450 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530451 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530452 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530453} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530454
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700455struct aac_enc_cfg_v2_t {
456 struct aac_enc_cfg_t aac_enc_cfg;
457 struct aac_frame_size_control_t frame_ctl;
458} __attribute__ ((packed));
459
Surendar Karkabbb3c822018-11-12 13:00:38 +0530460typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
461struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200462 uint16_t aac_fmt_flag; /* LATM*/
463 uint16_t audio_object_type; /* LC */
464 uint16_t channels; /* Stereo */
465 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
466 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
467 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530468} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200469
Surendar Karkabbb3c822018-11-12 13:00:38 +0530470typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
471struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200472 uint16_t channels; /* Mono, Stereo */
473 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
474 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530475} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200476
477/* AAC decoder configuration structure. */
478typedef struct aac_dec_cfg_t aac_dec_cfg_t;
479struct aac_dec_cfg_t {
480 uint32_t dec_format;
481 audio_aac_decoder_config_t data;
482} __attribute__ ((packed));
483
484/* SBC decoder configuration structure. */
485typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
486struct sbc_dec_cfg_t {
487 uint32_t dec_format;
488 audio_sbc_decoder_config_t data;
489} __attribute__ ((packed));
490
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530491/* SBC encoder configuration structure. */
492typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
493
494/* supported num_subbands are 4/8
495 * supported blk_len are 4, 8, 12, 16
496 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
497 * supported alloc_method are LOUNDNESS/SNR
498 * supported bit_rate for mono channel is max 320kbps
499 * supported bit rate for stereo channel is max 512 kbps
500 */
501struct sbc_enc_cfg_t{
502 uint32_t enc_format;
503 uint32_t num_subbands;
504 uint32_t blk_len;
505 uint32_t channel_mode;
506 uint32_t alloc_method;
507 uint32_t bit_rate;
508 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530509} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530510
511
512/* supported num_channels are Mono/Stereo
513 * supported channel_mapping for mono is CHANNEL_C
514 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
515 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700516 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530517struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530518{
519 uint32_t enc_format;
520 uint32_t sample_rate;
521 uint16_t num_channels;
522 uint16_t reserved;
523 uint8_t channel_mapping[8];
524 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530525} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530526
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530527struct celt_specific_enc_cfg_t
528{
529 uint32_t bit_rate;
530 uint16_t frame_size;
531 uint16_t complexity;
532 uint16_t prediction_mode;
533 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530534} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530535
536struct celt_enc_cfg_t
537{
538 struct custom_enc_cfg_t custom_cfg;
539 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530540} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700541
542/* sync_mode introduced with APTX V2 libraries
543 * sync mode: 0x0 = stereo sync mode
544 * 0x01 = dual mono sync mode
545 * 0x02 = dual mono with no sync on either L or R codewords
546 */
547struct aptx_v2_enc_cfg_ext_t
548{
549 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530550} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700551
552/* APTX struct for combining custom enc and V2 fields */
553struct aptx_enc_cfg_t
554{
555 struct custom_enc_cfg_t custom_cfg;
556 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530557} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700558
Sharad Sangle95d451b2018-06-19 12:24:20 +0530559/* APTX AD structure */
560struct aptx_ad_enc_cfg_ext_t
561{
562 uint32_t sampling_freq;
563 uint32_t mtu;
564 uint32_t channel_mode;
565 uint32_t min_sink_modeA;
566 uint32_t max_sink_modeA;
567 uint32_t min_sink_modeB;
568 uint32_t max_sink_modeB;
569 uint32_t min_sink_modeC;
570 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530571 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530572} __attribute__ ((packed));
573
574struct aptx_ad_enc_cfg_t
575{
576 struct custom_enc_cfg_t custom_cfg;
577 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
578 struct abr_enc_cfg_t abr_cfg;
579} __attribute__ ((packed));
580
Zhou Song8fccbb62019-03-20 01:08:19 +0800581struct aptx_ad_enc_cfg_ext_r2_t
582{
583 uint32_t sampling_freq;
584 uint32_t mtu;
585 uint32_t channel_mode;
586 uint32_t min_sink_modeA;
587 uint32_t max_sink_modeA;
588 uint32_t min_sink_modeB;
589 uint32_t max_sink_modeB;
590 uint32_t min_sink_modeC;
591 uint32_t max_sink_modeC;
592 uint32_t mode;
593 uint32_t input_mode;
594 uint32_t fade_duration;
595 uint8_t sink_cap[11];
596} __attribute__ ((packed));
597
598struct aptx_ad_enc_cfg_r2_t
599{
600 struct custom_enc_cfg_t custom_cfg;
601 struct aptx_ad_enc_cfg_ext_r2_t aptx_ad_cfg;
602 struct abr_enc_cfg_t abr_cfg;
603} __attribute__ ((packed));
604
Zhou Song12c29502019-03-16 10:37:18 +0800605/* APTX AD SPEECH structure */
606struct aptx_ad_speech_enc_cfg_t
607{
608 struct custom_enc_cfg_t custom_cfg;
609 /* Information to set up IMC between decoder and encoder */
610 struct imc_dec_enc_info imc_info;
611 struct aptx_ad_speech_mode_cfg_t speech_mode;
612} __attribute__ ((packed));
613
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530614struct ldac_specific_enc_cfg_t
615{
616 uint32_t bit_rate;
617 uint16_t channel_mode;
618 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530619} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530620
621struct ldac_enc_cfg_t
622{
623 struct custom_enc_cfg_t custom_cfg;
624 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800625 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530626} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530627
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530628/* In LE BT source code uses system/audio.h for below
629 * structure definition. To avoid multiple definition
630 * compilation error for audiohal in LE , masking structure
631 * definition under "LINUX_ENABLED" which is defined only
632 * in LE
633 */
634#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530635/* TODO: Define the following structures only for O using PLATFORM_VERSION */
636/* Information about BT SBC encoder configuration
637 * This data is used between audio HAL module and
638 * BT IPC library to configure DSP encoder
639 */
640typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530641 uint32_t subband; /* 4, 8 */
642 uint32_t blk_len; /* 4, 8, 12, 16 */
643 uint16_t sampling_rate; /*44.1khz,48khz*/
644 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
645 uint8_t alloc; /*0(Loudness),1(SNR)*/
646 uint8_t min_bitpool; /* 2 */
647 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
648 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530649 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530650} audio_sbc_encoder_config;
651
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530652/* Information about BT APTX encoder configuration
653 * This data is used between audio HAL module and
654 * BT IPC library to configure DSP encoder
655 */
656typedef struct {
657 uint16_t sampling_rate;
658 uint8_t channels;
659 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530660 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700661} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530662
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700663typedef struct {
Zhou Song8fccbb62019-03-20 01:08:19 +0800664 uint32_t sampling_rate;
665 uint32_t mtu;
666 int32_t channel_mode;
667 uint32_t min_sink_modeA;
668 uint32_t max_sink_modeA;
669 uint32_t min_sink_modeB;
670 uint32_t max_sink_modeB;
671 uint32_t min_sink_modeC;
672 uint32_t max_sink_modeC;
673 uint32_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530674 uint8_t TTP_modeA_low;
675 uint8_t TTP_modeA_high;
676 uint8_t TTP_modeB_low;
677 uint8_t TTP_modeB_high;
Zhou Song8fccbb62019-03-20 01:08:19 +0800678 uint8_t TTP_TWS_low;
679 uint8_t TTP_TWS_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530680 uint32_t bits_per_sample;
Zhou Song8fccbb62019-03-20 01:08:19 +0800681 uint32_t input_mode;
682 uint32_t fade_duration;
683 uint8_t sink_cap[11];
Sharad Sangle95d451b2018-06-19 12:24:20 +0530684} audio_aptx_ad_config;
685
686typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700687 uint16_t sampling_rate;
688 uint8_t channels;
689 uint32_t bitrate;
690 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530691 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700692} audio_aptx_dual_mono_config;
693
694typedef union {
695 audio_aptx_default_config *default_cfg;
696 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530697 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700698} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530699
700/* Information about BT AAC encoder configuration
701 * This data is used between audio HAL module and
702 * BT IPC library to configure DSP encoder
703 */
704typedef struct {
705 uint32_t enc_mode; /* LC, SBR, PS */
706 uint16_t format_flag; /* RAW, ADTS */
707 uint16_t channels; /* 1-Mono, 2-Stereo */
708 uint32_t sampling_rate;
709 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530710 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530711} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530712#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700713
714typedef struct {
715 audio_aac_encoder_config audio_aac_enc_cfg;
716 struct aac_frame_size_control_t frame_ctl;
717} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530718
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530719/* Information about BT CELT encoder configuration
720 * This data is used between audio HAL module and
721 * BT IPC library to configure DSP encoder
722 */
723typedef struct {
724 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
725 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
726 uint16_t frame_size; /* 64-128-256-512, 512 */
727 uint16_t complexity; /* 0-10, 1 */
728 uint16_t prediction_mode; /* 0-1-2, 0 */
729 uint16_t vbr_flag; /* 0-1, 0*/
730 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530731 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530732} audio_celt_encoder_config;
733
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530734/* Information about BT LDAC encoder configuration
735 * This data is used between audio HAL module and
736 * BT IPC library to configure DSP encoder
737 */
738typedef struct {
739 uint32_t sampling_rate; /*44100,48000,88200,96000*/
740 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
741 uint16_t channel_mode; /* 0, 4, 2, 1*/
742 uint16_t mtu; /*679*/
Aniket Kumar Latad0196282019-05-09 14:24:17 -0700743 uint32_t bits_per_sample;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800744 bool is_abr_enabled;
745 struct quality_level_to_bitrate_info level_to_bitrate_map;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530746} audio_ldac_encoder_config;
747
Surendar Karkabbb3c822018-11-12 13:00:38 +0530748/* Information about BT AAC decoder configuration
749 * This data is used between audio HAL module and
750 * BT IPC library to configure DSP decoder
751 */
752typedef struct {
753 uint16_t aac_fmt_flag; /* LATM*/
754 uint16_t audio_object_type; /* LC */
755 uint16_t channels; /* Stereo */
756 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
757 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
758 44.1k, 48k, 64k, 88.2k, 96k */
759} audio_aac_dec_config_t;
760
761/* Information about BT SBC decoder configuration
762 * This data is used between audio HAL module and
763 * BT IPC library to configure DSP decoder
764 */
765typedef struct {
766 uint16_t channels; /* Mono, Stereo */
767 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
768 44.1k, 48k, 64k, 88.2k, 96k */
769}audio_sbc_dec_config_t;
770
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530771/*********** END of DSP configurable structures ********************/
772
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530773static void update_offload_codec_capabilities()
774{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530775
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530776 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800777 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
778 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
779
780 ALOGD("%s: A2DP offload supported = %d",__func__,
781 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530782}
783
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800784static int stop_abr()
785{
786 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800787 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800788 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800789 int ret = 0;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800790
791 /* This function can be used if !abr_started for clean up */
792 ALOGV("%s: enter", __func__);
793
794 // Close hostless front end
795 if (a2dp.abr_config.abr_tx_handle != NULL) {
796 pcm_close(a2dp.abr_config.abr_tx_handle);
797 a2dp.abr_config.abr_tx_handle = NULL;
798 }
Zhou Song12c29502019-03-16 10:37:18 +0800799 if (a2dp.abr_config.abr_rx_handle != NULL) {
800 pcm_close(a2dp.abr_config.abr_rx_handle);
801 a2dp.abr_config.abr_rx_handle = NULL;
802 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800803 a2dp.abr_config.abr_started = false;
804 a2dp.abr_config.imc_instance = 0;
805
806 // Reset BT driver mixer control for ABR usecase
807 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
808 MIXER_SET_FEEDBACK_CHANNEL);
809 if (!ctl_set_bt_feedback_channel) {
810 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800811 ret = -ENOSYS;
812 } else if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800813 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800814 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800815 }
816
817 // Reset ABR Tx feedback path
818 ALOGV("%s: Disable ABR Tx feedback path", __func__);
819 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
820 MIXER_ABR_TX_FEEDBACK_PATH);
821 if (!ctl_abr_tx_path) {
822 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800823 ret = -ENOSYS;
824 } else if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800825 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800826 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800827 }
828
Zhou Song12c29502019-03-16 10:37:18 +0800829 // Reset ABR Rx feedback path
830 ALOGV("%s: Disable ABR Rx feedback path", __func__);
831 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
832 MIXER_ABR_RX_FEEDBACK_PATH);
833 if (!ctl_abr_rx_path) {
834 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
835 ret = -ENOSYS;
836 } else if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 0) != 0) {
837 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
838 ret = -ENOSYS;
839 }
840
841 return ret;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800842}
843
844static int start_abr()
845{
846 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800847 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800848 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
849 int abr_device_id;
850 int ret = 0;
851
852 if (!a2dp.abr_config.is_abr_enabled) {
853 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
854 return -ENOSYS;
855 }
856
857 if (a2dp.abr_config.abr_started) {
858 ALOGI("%s: ABR has already started", __func__);
859 return ret;
860 }
861
862 // Enable Slimbus 7 Tx feedback path
863 ALOGV("%s: Enable ABR Tx feedback path", __func__);
864 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
865 MIXER_ABR_TX_FEEDBACK_PATH);
866 if (!ctl_abr_tx_path) {
867 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
868 return -ENOSYS;
869 }
870 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
871 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
872 return -ENOSYS;
873 }
874
875 // Notify ABR usecase information to BT driver to distinguish
876 // between SCO and feedback usecase
877 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
878 MIXER_SET_FEEDBACK_CHANNEL);
879 if (!ctl_set_bt_feedback_channel) {
880 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800881 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800882 }
883 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
884 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800885 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800886 }
887
888 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800889 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800890 PCM_CAPTURE);
891 if (!a2dp.abr_config.abr_tx_handle) {
892 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
893 abr_device_id, PCM_IN,
894 &pcm_config_abr);
Zhou Song12c29502019-03-16 10:37:18 +0800895 if (a2dp.abr_config.abr_tx_handle == NULL) {
896 ALOGE("%s: Can't open abr tx device", __func__);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800897 goto fail;
Zhou Song12c29502019-03-16 10:37:18 +0800898 }
899 if (!(pcm_is_ready(a2dp.abr_config.abr_tx_handle) &&
900 !pcm_start(a2dp.abr_config.abr_tx_handle))) {
901 ALOGE("%s: tx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
902 goto fail;
903 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800904 }
Zhou Song12c29502019-03-16 10:37:18 +0800905
906 // Enable Slimbus 7 Rx feedback path for HD Voice use case
907 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH) {
908 ALOGV("%s: Enable ABR Rx feedback path", __func__);
909 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
910 MIXER_ABR_RX_FEEDBACK_PATH);
911 if (!ctl_abr_rx_path) {
912 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
913 goto fail;
914 }
915 if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 1) != 0) {
916 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
917 goto fail;
918 }
919
920 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
921 ALOGE("%s: Failed to set BT usecase", __func__);
922 goto fail;
923 }
924
925 // Open hostless front end and prepare ABR Rx path
926 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
927 PCM_PLAYBACK);
928 if (!a2dp.abr_config.abr_rx_handle) {
929 a2dp.abr_config.abr_rx_handle = pcm_open(a2dp.adev->snd_card,
930 abr_device_id, PCM_OUT,
931 &pcm_config_abr);
932 if (a2dp.abr_config.abr_rx_handle == NULL) {
933 ALOGE("%s: Can't open abr rx device", __func__);
934 goto fail;
935 }
936 if (!(pcm_is_ready(a2dp.abr_config.abr_rx_handle) &&
937 !pcm_start(a2dp.abr_config.abr_rx_handle))) {
938 ALOGE("%s: rx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_rx_handle));
939 goto fail;
940 }
941 }
942 }
943
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800944 a2dp.abr_config.abr_started = true;
945
946 return ret;
947
948fail:
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800949 stop_abr();
950 return -ENOSYS;
951}
952
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800953static int check_if_enhanced_fwk() {
954
955 int is_enhanced_fwk = 1;
956 //dlopen lib
957 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
958 if (vndk_fwk_lib_handle != NULL) {
959 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
960 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
961 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
962 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
963 __func__);
964 is_enhanced_fwk = 1;
965 } else {
966 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
967 }
968 }
969 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
970 return is_enhanced_fwk;
971}
972
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530973static void open_a2dp_source() {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530974 int ret = 0;
975
Florian Pfister1a84f312018-07-19 14:38:18 +0200976 ALOGD(" Open A2DP source start ");
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530977
978 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
979 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
980 ALOGD("calling BT stream open");
981 ret = a2dp.audio_source_open();
982 if(ret != 0) {
983 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Srinu Jellada99a592019-01-25 16:50:52 +0530984 }
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530985 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530986 } else {
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530987 ALOGD("Called a2dp open with improper state %d", a2dp.bt_state_source);
988 }
989 } else {
990 ALOGE("a2dp handle is not identified, Ignoring open request");
991 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
992 }
993}
994/* API to open BT IPC library to start IPC communication for BT Source*/
995static void a2dp_source_init()
996{
997 ALOGD("a2dp_source_init START");
998 if (a2dp.bt_lib_source_handle == NULL) {
999 ALOGD("Requesting for BT lib handle");
1000 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
1001 if (a2dp.bt_lib_source_handle == NULL) {
1002 ALOGE("%s: dlopen failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
1003 return;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301004 }
1005 }
1006
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301007 a2dp.bt_audio_pre_init = (bt_audio_pre_init_t)
1008 dlsym(a2dp.bt_lib_source_handle, "bt_audio_pre_init");
Srinu Jellada99a592019-01-25 16:50:52 +05301009 a2dp.audio_source_open = (audio_source_open_t)
1010 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
1011 a2dp.audio_source_start = (audio_source_start_t)
1012 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
1013 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
1014 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
1015 a2dp.audio_source_suspend = (audio_source_suspend_t)
1016 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
1017 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
1018 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
1019 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1020 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
1021 a2dp.audio_source_stop = (audio_source_stop_t)
1022 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
1023 a2dp.audio_source_close = (audio_source_close_t)
1024 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
1025 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
1026 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
1027 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
1028 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
1029 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1030 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1031 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1032 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1033
Srinu Jella793f5302019-07-19 15:53:55 +05301034 if (a2dp.bt_lib_source_handle && a2dp.bt_audio_pre_init) {
1035 ALOGD("calling BT module preinit");
1036 // fwk related check's will be done in the BT layer
1037 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001038 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001039}
1040
1041/* API to open BT IPC library to start IPC communication for BT Sink*/
1042static void open_a2dp_sink()
1043{
1044 ALOGD(" Open A2DP input start ");
1045 if (a2dp.bt_lib_sink_handle == NULL){
1046 ALOGD(" Requesting for BT lib handle");
1047 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1048
1049 if (a2dp.bt_lib_sink_handle == NULL) {
1050 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1051 } else {
1052 a2dp.audio_sink_start = (audio_sink_start_t)
1053 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1054 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1055 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1056 a2dp.audio_sink_stop = (audio_sink_stop_t)
1057 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1058 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1059 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1060 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1061 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1062 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301063 }
1064}
1065
1066static int close_a2dp_output()
1067{
1068 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001069
1070 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1071 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301072 return -ENOSYS;
1073 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001074
1075 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1076 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001077 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001078 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301079 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001080 a2dp.a2dp_source_started = false;
1081 a2dp.a2dp_source_total_active_session_requests = 0;
1082 a2dp.a2dp_source_suspended = false;
1083 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301084 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301085 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001086 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001087 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1088 stop_abr();
1089 a2dp.abr_config.is_abr_enabled = false;
1090 a2dp.abr_config.abr_started = false;
1091 a2dp.abr_config.imc_instance = 0;
1092 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001093 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301094 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001095
1096 return 0;
1097}
1098
1099static int close_a2dp_input()
1100{
1101 ALOGV("%s\n",__func__);
1102
1103 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1104 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1105 return -ENOSYS;
1106 }
1107
1108 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1109 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001110 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001111 ALOGE("failed close a2dp sink control path from BT library");
1112 }
1113 a2dp.a2dp_sink_started = false;
1114 a2dp.a2dp_sink_total_active_session_requests = 0;
1115 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1116 a2dp.dec_sampling_rate = 48000;
1117 a2dp.dec_channels = 2;
1118 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301119
1120 return 0;
1121}
1122
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301123static void a2dp_check_and_set_scrambler()
1124{
1125 bool scrambler_mode = false;
1126 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001127 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1128 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301129
1130 if (scrambler_mode) {
1131 //enable scrambler in dsp
1132 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1133 MIXER_SCRAMBLER_MODE);
1134 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001135 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301136 return;
1137 } else {
1138 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1139 ALOGE("%s: Could not set scrambler mode", __func__);
1140 return;
1141 }
1142 }
1143 }
1144}
1145
Florian Pfister1a84f312018-07-19 14:38:18 +02001146static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301147{
Florian Pfister1a84f312018-07-19 14:38:18 +02001148 char *rate_str = NULL, *channels = NULL;
1149 uint32_t sampling_rate;
1150 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1151 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301152
Florian Pfister1a84f312018-07-19 14:38:18 +02001153 if (direction == SINK) {
1154 sampling_rate = a2dp.dec_sampling_rate;
1155 } else {
1156 sampling_rate = a2dp.enc_sampling_rate;
1157 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001158 /*
1159 * For LDAC encoder and AAC decoder open slimbus port at
1160 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1161 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1162 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001163 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301164 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001165 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1166 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301167 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1168 sampling_rate = sampling_rate *2;
1169 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001170
Aalique Grahame22e49102018-12-18 14:23:57 -08001171 // No need to configure backend for PCM format.
1172 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1173 return 0;
1174 }
1175
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301176 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301177 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301178 case 44100:
1179 rate_str = "KHZ_44P1";
1180 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301181 case 88200:
1182 rate_str = "KHZ_88P2";
1183 break;
1184 case 96000:
1185 rate_str = "KHZ_96";
1186 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001187 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301188 default:
1189 rate_str = "KHZ_48";
1190 break;
1191 }
1192
Florian Pfister1a84f312018-07-19 14:38:18 +02001193 if (direction == SINK) {
1194 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1195 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301196 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001197 } else {
1198 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1199 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301200 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001201 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301202 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001203
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301204 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1205 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301206 is_configured = false;
1207 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301208 }
1209
Surendar Karka2febd452018-12-13 17:56:43 +05301210 if (direction == SOURCE) {
1211 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001212 if (a2dp.abr_config.is_abr_enabled) {
1213 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH)
1214 rate_str = SPEECH_TX_SAMPLE_RATE;
1215 else
1216 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301217
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301218 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1219 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1220 MIXER_SOURCE_SAMPLE_RATE_TX);
1221 if (!ctl_sample_rate) {
1222 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1223 is_configured = false;
1224 goto fail;
1225 }
1226
1227 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1228 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1229 is_configured = false;
1230 goto fail;
1231 }
Surendar Karka2febd452018-12-13 17:56:43 +05301232 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301233 }
1234 } else {
1235 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1236 MIXER_SAMPLE_RATE_TX is not supported */
1237 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1238 MIXER_SAMPLE_RATE_DEFAULT);
1239 if (!ctl_sample_rate) {
1240 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301241 is_configured = false;
1242 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301243 }
1244
1245 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1246 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001247 is_configured = false;
1248 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301249 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301250 }
1251
Florian Pfister1a84f312018-07-19 14:38:18 +02001252 if (direction == SINK) {
1253 switch (a2dp.dec_channels) {
1254 case 1:
1255 channels = "One";
1256 break;
1257 case 2:
1258 default:
1259 channels = "Two";
1260 break;
1261 }
1262
Ramu Gottipati02809682018-12-19 16:46:12 +05301263 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001264 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1265 MIXER_AFE_SINK_CHANNELS);
1266 } else {
1267 //Configure AFE enc channels
1268 switch (a2dp.enc_channels) {
1269 case 1:
1270 channels = "One";
1271 break;
1272 case 2:
1273 default:
1274 channels = "Two";
1275 break;
1276 }
1277
Ramu Gottipati02809682018-12-19 16:46:12 +05301278 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001279 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1280 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301281 }
1282
Florian Pfister1a84f312018-07-19 14:38:18 +02001283 if (!ctrl_channels) {
1284 ALOGE(" ERROR AFE channels mixer control not identified");
1285 } else {
1286 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301287 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001288 is_configured = false;
1289 goto fail;
1290 }
1291 }
1292 is_configured = true;
1293fail:
1294 return is_configured;
1295}
1296
Surendar Karkabbb3c822018-11-12 13:00:38 +05301297bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001298{
1299 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1300 struct aac_dec_cfg_t aac_dsp_cfg;
1301 bool is_configured = false;
1302 int ret = 0;
1303
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001304 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001305 return false;
1306
Surendar Karka2febd452018-12-13 17:56:43 +05301307 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001308 if (!ctl_dec_data) {
1309 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1310 is_configured = false;
1311 goto fail;
1312 }
1313
1314 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1315 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1316 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1317 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1318 switch(aac_bt_cfg->audio_object_type) {
1319 case 0:
1320 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301321 break;
1322 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001323 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1324 break;
1325 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301326 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001327 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301328 break;
1329 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001330 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1331 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1332 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1333 sizeof(struct aac_dec_cfg_t));
1334 if (ret != 0) {
1335 ALOGE("%s: failed to set AAC decoder config", __func__);
1336 is_configured = false;
1337 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001338 }
1339
Florian Pfister1a84f312018-07-19 14:38:18 +02001340 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1341 MIXER_DEC_BIT_FORMAT);
1342 if (!ctrl_bit_format) {
1343 ALOGE(" ERROR Dec bit format mixer control not identified");
1344 is_configured = false;
1345 goto fail;
1346 }
1347 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1348 if (ret != 0) {
1349 ALOGE("%s: Failed to set bit format to decoder", __func__);
1350 is_configured = false;
1351 goto fail;
1352 }
1353
1354 is_configured = true;
1355 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1356 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1357 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1358 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1359 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1360fail:
1361 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301362}
1363
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301364static int a2dp_set_bit_format(uint32_t enc_bit_format)
1365{
1366 const char *bit_format = NULL;
1367 struct mixer_ctl *ctrl_bit_format = NULL;
1368
1369 // Configure AFE Input Bit Format
1370 switch (enc_bit_format) {
1371 case 32:
1372 bit_format = "S32_LE";
1373 break;
1374 case 24:
1375 bit_format = "S24_LE";
1376 break;
1377 case 16:
1378 default:
1379 bit_format = "S16_LE";
1380 break;
1381 }
1382
1383 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1384 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1385 MIXER_ENC_BIT_FORMAT);
1386 if (!ctrl_bit_format) {
1387 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1388 return -ENOSYS;
1389 }
1390 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1391 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1392 return -ENOSYS;
1393 }
1394 return 0;
1395}
1396
Surendar Karka2febd452018-12-13 17:56:43 +05301397static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301398{
Surendar Karka2febd452018-12-13 17:56:43 +05301399 const char *rate_str = "KHZ_8", *channels = "Zero";
1400 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1401 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301402
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001403 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301404 if (direction == SINK) {
1405 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1406 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1407 MIXER_SINK_SAMPLE_RATE);
1408 } else {
1409 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1410 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1411 MIXER_SAMPLE_RATE_RX);
1412 }
1413 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301414
Surendar Karka2febd452018-12-13 17:56:43 +05301415 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1416 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301417 return -ENOSYS;
1418 }
Zhou Song12c29502019-03-16 10:37:18 +08001419 if (a2dp.abr_config.is_abr_enabled) {
1420 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1421 MIXER_SOURCE_SAMPLE_RATE_TX);
1422 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301423 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1424 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001425 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301426
Zhou Song12c29502019-03-16 10:37:18 +08001427 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1428 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1429 return -ENOSYS;
1430 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301431 }
1432 } else {
1433
Surendar Karka2febd452018-12-13 17:56:43 +05301434 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301435 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301436 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301437 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1438 return -ENOSYS;
1439 }
1440
Surendar Karka2febd452018-12-13 17:56:43 +05301441 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301442 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1443 return -ENOSYS;
1444 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001445 }
1446
1447 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301448 if (direction == SINK) {
1449 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1450 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1451 MIXER_AFE_SINK_CHANNELS);
1452 } else {
1453 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1454 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1455 MIXER_AFE_IN_CHANNELS);
1456 }
1457 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001458 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1459 return -ENOSYS;
1460 }
Surendar Karka2febd452018-12-13 17:56:43 +05301461 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001462 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1463 return -ENOSYS;
1464 }
1465
1466 return 0;
1467}
1468
1469/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301470static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001471{
1472 struct mixer_ctl *ctl_dec_data = NULL;
1473 struct abr_dec_cfg_t dec_cfg;
1474 int ret = 0;
1475
1476 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301477 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001478 if (!ctl_dec_data) {
1479 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1480 return false;
1481 }
1482 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1483 dec_cfg.dec_format = dec_format;
1484 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1485 dec_cfg.imc_info.enable = IMC_ENABLE;
1486 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1487 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1488
1489 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1490 sizeof(dec_cfg));
1491 if (ret != 0) {
1492 ALOGE("%s: Failed to set decoder config", __func__);
1493 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301494 }
Surendar Karka2febd452018-12-13 17:56:43 +05301495 }
1496
1497 return true;
1498}
1499
Surendar Karkabbb3c822018-11-12 13:00:38 +05301500bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001501{
1502 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1503 struct sbc_dec_cfg_t sbc_dsp_cfg;
1504 bool is_configured = false;
1505 int ret = 0;
1506
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001507 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001508 goto fail;
1509
Surendar Karka2febd452018-12-13 17:56:43 +05301510 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001511 if (!ctl_dec_data) {
1512 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1513 is_configured = false;
1514 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301515 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001516
Florian Pfister1a84f312018-07-19 14:38:18 +02001517 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1518 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1519 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1520 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1521 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1522 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301523
Florian Pfister1a84f312018-07-19 14:38:18 +02001524 if (ret != 0) {
1525 ALOGE("%s: failed to set SBC decoder config", __func__);
1526 is_configured = false;
1527 goto fail;
1528 }
1529
1530 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1531 MIXER_DEC_BIT_FORMAT);
1532 if (!ctrl_bit_format) {
1533 ALOGE(" ERROR Dec bit format mixer control not identified");
1534 is_configured = false;
1535 goto fail;
1536 }
1537 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1538 if (ret != 0) {
1539 ALOGE("%s: Failed to set bit format to decoder", __func__);
1540 is_configured = false;
1541 goto fail;
1542 }
1543
1544 is_configured = true;
1545 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1546 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1547 a2dp.dec_channels = 1;
1548 else
1549 a2dp.dec_channels = 2;
1550 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1551 ALOGV("Successfully updated SBC dec format");
1552fail:
1553 return is_configured;
1554}
1555
Florian Pfister1a84f312018-07-19 14:38:18 +02001556/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301557static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301558{
Florian Pfister1a84f312018-07-19 14:38:18 +02001559 void *codec_info = NULL;
1560 codec_t codec_type = CODEC_TYPE_INVALID;
1561 bool is_configured = false;
1562 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301563
Florian Pfister1a84f312018-07-19 14:38:18 +02001564 if (!a2dp.audio_get_dec_config) {
1565 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1566 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301567 }
1568
Surendar Karka2febd452018-12-13 17:56:43 +05301569 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001570 if (!ctl_dec_data) {
1571 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1572 is_configured = false;
1573 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301574 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001575 codec_info = a2dp.audio_get_dec_config(&codec_type);
1576 switch(codec_type) {
1577 case CODEC_TYPE_SBC:
1578 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301579 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001580 break;
1581 case CODEC_TYPE_AAC:
1582 ALOGD(" AAC decoder supported BT device");
1583 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301584 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001585 break;
1586 default:
1587 ALOGD(" Received Unsupported decoder format");
1588 is_configured = false;
1589 break;
1590 }
1591 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301592}
1593
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301594/* API to configure SBC DSP encoder */
1595bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1596{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301597 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301598 struct sbc_enc_cfg_t sbc_dsp_cfg;
1599 bool is_configured = false;
1600 int ret = 0;
1601
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001602 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301603 return false;
1604
1605 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1606 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001607 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301608 is_configured = false;
1609 goto fail;
1610 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301611 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001612 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301613 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1614 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1615 switch(sbc_bt_cfg->channels) {
1616 case 0:
1617 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1618 break;
1619 case 1:
1620 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1621 break;
1622 case 3:
1623 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1624 break;
1625 case 2:
1626 default:
1627 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1628 break;
1629 }
1630 if (sbc_bt_cfg->alloc)
1631 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1632 else
1633 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1634 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1635 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1636 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1637 sizeof(struct sbc_enc_cfg_t));
1638 if (ret != 0) {
1639 ALOGE("%s: failed to set SBC encoder config", __func__);
1640 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301641 goto fail;
1642 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301643 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301644 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301645 is_configured = false;
1646 goto fail;
1647 }
1648 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001649 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301650 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301651
1652 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1653 a2dp.enc_channels = 1;
1654 else
1655 a2dp.enc_channels = 2;
1656
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301657 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1658 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301659fail:
1660 return is_configured;
1661}
1662
Manish Dewangan6a252632017-12-04 17:27:44 +05301663#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301664static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1665 audio_aptx_encoder_config *aptx_bt_cfg)
1666{
1667 int ret = 0;
1668
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001669 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301670 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1671 aptx_dsp_cfg, aptx_bt_cfg);
1672 return -EINVAL;
1673 }
1674
1675 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301676 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301677
1678
1679 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1680 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1681 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1682 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1683 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1684 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1685 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1686 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1687 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301688 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301689 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1690 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1691 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1692 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1693
1694
1695 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1696 case APTX_AD_CHANNEL_UNCHANGED:
1697 case APTX_AD_CHANNEL_JOINT_STEREO:
1698 case APTX_AD_CHANNEL_DUAL_MONO:
1699 case APTX_AD_CHANNEL_STEREO_TWS:
1700 case APTX_AD_CHANNEL_EARBUD:
1701 default:
1702 a2dp.enc_channels = CH_STEREO;
1703 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1704 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1705 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1706 break;
1707 case APTX_AD_CHANNEL_MONO:
1708 a2dp.enc_channels = CH_MONO;
1709 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1710 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1711 break;
1712 }
1713 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1714 case APTX_AD_SR_UNCHANGED:
1715 case APTX_AD_48:
1716 default:
1717 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1718 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1719 break;
1720 case APTX_AD_44_1:
1721 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1722 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
1723 break;
1724 }
1725 ALOGV("Successfully updated APTX AD enc format with \
1726 samplingrate: %d channels:%d",
1727 aptx_dsp_cfg->custom_cfg.sample_rate,
1728 aptx_dsp_cfg->custom_cfg.num_channels);
1729
1730 return ret;
1731}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301732
Zhou Song8fccbb62019-03-20 01:08:19 +08001733static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
1734 audio_aptx_encoder_config *aptx_bt_cfg)
1735{
1736 int ret = 0;
1737
1738 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Mingshu Pangaa429f72019-06-10 17:25:00 +08001739 ALOGE("Invalid param, aptx_dsp_cfg %pK aptx_bt_cfg %pK",
Zhou Song8fccbb62019-03-20 01:08:19 +08001740 aptx_dsp_cfg, aptx_bt_cfg);
1741 return -EINVAL;
1742 }
1743
1744 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
1745 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
1746
1747
1748 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1749 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1750 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1751 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1752 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1753 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1754 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1755 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1756 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
1757 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
1758 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
1759 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
1760 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
1761 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
1762 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1763 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1764 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1765 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1766
1767
1768 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1769 case APTX_AD_CHANNEL_UNCHANGED:
1770 case APTX_AD_CHANNEL_JOINT_STEREO:
1771 case APTX_AD_CHANNEL_DUAL_MONO:
1772 case APTX_AD_CHANNEL_STEREO_TWS:
1773 case APTX_AD_CHANNEL_EARBUD:
1774 default:
1775 a2dp.enc_channels = CH_STEREO;
1776 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1777 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1778 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1779 break;
1780 case APTX_AD_CHANNEL_MONO:
1781 a2dp.enc_channels = CH_MONO;
1782 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1783 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1784 break;
1785 }
1786 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1787 case APTX_AD_SR_UNCHANGED:
1788 case APTX_AD_48:
1789 default:
1790 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1791 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1792 break;
1793 case APTX_AD_44_1:
1794 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1795 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
1796 break;
1797 }
1798 ALOGV("Successfully updated APTX AD enc format with \
1799 samplingrate: %d channels:%d",
1800 aptx_dsp_cfg->custom_cfg.sample_rate,
1801 aptx_dsp_cfg->custom_cfg.num_channels);
1802
1803 return ret;
1804}
1805
Manisha Agarwala51768b2018-11-01 16:30:52 +05301806static void audio_a2dp_update_tws_channel_mode()
1807{
1808 char* channel_mode;
1809 struct mixer_ctl *ctl_channel_mode;
1810 if (a2dp.is_tws_mono_mode_on)
1811 channel_mode = "One";
1812 else
1813 channel_mode = "Two";
1814 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1815 if (!ctl_channel_mode) {
1816 ALOGE("failed to get tws mixer ctl");
1817 return;
1818 }
1819 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1820 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1821 return;
1822 }
1823}
1824
Manish Dewangan6a252632017-12-04 17:27:44 +05301825static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
1826 audio_aptx_encoder_config *aptx_bt_cfg)
1827{
1828 int ret = 0;
1829
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001830 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301831 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1832 aptx_dsp_cfg, aptx_bt_cfg);
1833 return -EINVAL;
1834 }
1835
1836 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001837 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301838
1839 if (!a2dp.is_aptx_dual_mono_supported) {
1840 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1841 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1842 } else {
1843 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1844 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1845 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1846 }
1847
1848 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
1849 case 1:
1850 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1851 break;
1852 case 2:
1853 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05301854 if (!a2dp.is_tws_mono_mode_on) {
1855 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1856 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1857 }
1858 else {
1859 a2dp.is_tws_mono_mode_on = true;
1860 ALOGD("Update tws for mono_mode_on: %d",a2dp.is_tws_mono_mode_on);
Manisha Agarwala51768b2018-11-01 16:30:52 +05301861 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301862 break;
1863 }
1864 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
1865 if (!a2dp.is_aptx_dual_mono_supported) {
1866 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1867 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
1868 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
1869 aptx_dsp_cfg->custom_cfg.num_channels);
1870 } else {
1871 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1872 ALOGV("Successfully updated APTX dual mono enc format with \
1873 samplingrate: %d channels:%d syncmode %d",
1874 aptx_dsp_cfg->custom_cfg.sample_rate,
1875 aptx_dsp_cfg->custom_cfg.num_channels,
1876 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
1877 }
1878 return ret;
1879}
1880#else
1881static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
1882 audio_aptx_encoder_config *aptx_bt_cfg)
1883{
1884 int ret = 0;
1885
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001886 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301887 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1888 aptx_dsp_cfg, aptx_bt_cfg);
1889 return -EINVAL;
1890 }
1891
1892 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001893 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301894 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
1895 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
1896 switch(aptx_dsp_cfg->num_channels) {
1897 case 1:
1898 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
1899 break;
1900 case 2:
1901 default:
1902 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
1903 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
1904 break;
1905 }
1906
1907 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
1908 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
1909
1910 return ret;
1911}
1912#endif
1913
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301914/* API to configure APTX DSP encoder */
1915bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1916{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301917 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08001918 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001919 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301920 bool is_configured = false;
1921 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08001922 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301923
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001924 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301925 return false;
1926
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001927 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1928 if (!ctl_enc_data) {
1929 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1930 return false;
1931 }
1932
Manish Dewangan6a252632017-12-04 17:27:44 +05301933#ifndef LINUX_ENABLED
1934 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301935 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08001936 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001937 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001938 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
1939 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001940 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08001941 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001942 else
Zhou Song8fccbb62019-03-20 01:08:19 +08001943 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001944 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001945 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001946
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001947 if (ret) {
1948 is_configured = false;
1949 goto fail;
1950 }
1951
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001952 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001953 if (aptx_ad_ctl)
1954 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001955 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08001956 else
1957 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001958 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001959 } else {
1960 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001961 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05301962 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301963#else
1964 struct custom_enc_cfg_t aptx_dsp_cfg;
1965 mixer_size = sizeof(struct custom_enc_cfg_t);
1966 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05301967 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05301968 if (ret) {
1969 is_configured = false;
1970 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301971 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001972 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1973 mixer_size);
1974#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301975 if (ret != 0) {
1976 ALOGE("%s: Failed to set APTX encoder config", __func__);
1977 is_configured = false;
1978 goto fail;
1979 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301980#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001981 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301982 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001983 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301984 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
1985 else
1986 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301987#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301988 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301989 is_configured = false;
1990 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301991 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301992 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301993 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05301994 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301995 else
Surendar Karka2febd452018-12-13 17:56:43 +05301996 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301997fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05301998 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001999 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05302000 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302001 return is_configured;
2002}
2003
2004/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302005 */
Manish Dewangan6a252632017-12-04 17:27:44 +05302006#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002007bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302008#else
2009bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2010#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302011{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302012 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302013 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302014 bool is_configured = false;
2015 int ret = 0;
2016
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002017 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302018 return false;
2019
2020 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2021 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002022 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302023 is_configured = false;
2024 goto fail;
2025 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302026
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302027 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002028 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302029 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2030 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2031 switch(aptx_dsp_cfg.num_channels) {
2032 case 1:
2033 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2034 break;
2035 case 2:
2036 default:
2037 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2038 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2039 break;
2040 }
2041 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302042 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302043 if (ret != 0) {
2044 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2045 is_configured = false;
2046 goto fail;
2047 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302048 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302049 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302050 is_configured = false;
2051 goto fail;
2052 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302053 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002054 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302055 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302056 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302057 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2058 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302059fail:
2060 return is_configured;
2061}
2062
2063/* API to configure AAC DSP encoder */
2064bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2065{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302066 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302067 struct aac_enc_cfg_t aac_dsp_cfg;
2068 bool is_configured = false;
2069 int ret = 0;
2070
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002071 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302072 return false;
2073
2074 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2075 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002076 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302077 is_configured = false;
2078 goto fail;
2079 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302080 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002081 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302082 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302083 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002084 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302085 case 0:
2086 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2087 break;
2088 case 2:
2089 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2090 break;
2091 case 1:
2092 default:
2093 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2094 break;
2095 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302096 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302097 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002098
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302099 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2100 sizeof(struct aac_enc_cfg_t));
2101 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002102 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302103 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302104 goto fail;
2105 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302106 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302107 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302108 is_configured = false;
2109 goto fail;
2110 }
2111 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002112 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302113 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002114 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002115 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2116 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2117fail:
2118 return is_configured;
2119}
2120
2121bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2122{
2123 struct mixer_ctl *ctl_enc_data = NULL;
2124 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2125 bool is_configured = false;
2126 int ret = 0;
2127
2128 if (aac_bt_cfg == NULL)
2129 return false;
2130
2131 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2132 if (!ctl_enc_data) {
2133 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2134 is_configured = false;
2135 goto fail;
2136 }
2137 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302138 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002139 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2140 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2141 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2142 case 0:
2143 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2144 break;
2145 case 2:
2146 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2147 break;
2148 case 1:
2149 default:
2150 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2151 break;
2152 }
2153 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2154 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2155 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2156 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2157
2158 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2159 sizeof(struct aac_enc_cfg_v2_t));
2160 if (ret != 0) {
2161 ALOGE("%s: Failed to set AAC encoder config", __func__);
2162 is_configured = false;
2163 goto fail;
2164 }
2165 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2166 if (ret != 0) {
2167 is_configured = false;
2168 goto fail;
2169 }
2170 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302171 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002172 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2173 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2174 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2175 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302176fail:
2177 return is_configured;
2178}
2179
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302180bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2181{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302182 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302183 struct celt_enc_cfg_t celt_dsp_cfg;
2184 bool is_configured = false;
2185 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002186 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302187 return false;
2188
2189 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2190 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002191 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302192 is_configured = false;
2193 goto fail;
2194 }
2195 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2196
Florian Pfister1a84f312018-07-19 14:38:18 +02002197 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302198 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2199 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2200 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2201 case 1:
2202 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2203 break;
2204 case 2:
2205 default:
2206 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2207 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2208 break;
2209 }
2210
2211 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2212
2213 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2214 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2215 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2216 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2217 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2218
2219 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2220 sizeof(struct celt_enc_cfg_t));
2221 if (ret != 0) {
2222 ALOGE("%s: Failed to set CELT encoder config", __func__);
2223 is_configured = false;
2224 goto fail;
2225 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302226 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302227 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302228 is_configured = false;
2229 goto fail;
2230 }
2231 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002232 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302233 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302234 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302235 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2236 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2237fail:
2238 return is_configured;
2239}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302240
2241bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2242{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302243 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302244 struct ldac_enc_cfg_t ldac_dsp_cfg;
2245 bool is_configured = false;
2246 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002247 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302248 return false;
2249
2250 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2251 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002252 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302253 is_configured = false;
2254 goto fail;
2255 }
2256 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2257
Florian Pfister1a84f312018-07-19 14:38:18 +02002258 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302259 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2260 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2261 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2262 case 4:
2263 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2264 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2265 break;
2266 case 2:
2267 case 1:
2268 default:
2269 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2270 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2271 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2272 break;
2273 }
2274
2275 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2276 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2277 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002278 if (ldac_bt_cfg->is_abr_enabled) {
2279 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2280 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2281 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2282 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2283 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002284 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002285 }
2286
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302287 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2288 sizeof(struct ldac_enc_cfg_t));
2289 if (ret != 0) {
2290 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2291 is_configured = false;
2292 goto fail;
2293 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302294 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302295 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302296 is_configured = false;
2297 goto fail;
2298 }
2299 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002300 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302301 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2302 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002303 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302304 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2305 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2306fail:
2307 return is_configured;
2308}
2309
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302310bool configure_a2dp_encoder_format()
2311{
2312 void *codec_info = NULL;
2313 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002314 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302315 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002316 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302317
Florian Pfister1a84f312018-07-19 14:38:18 +02002318 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302319 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2320 return false;
2321 }
2322 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002323 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302324 &codec_type);
2325
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002326 // ABR disabled by default for all codecs
2327 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302328 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002329
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302330 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002331 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302332 ALOGD(" Received SBC encoder supported BT device");
2333 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002334 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302335 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002336 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302337 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302338#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002339 a2dp.is_aptx_dual_mono_supported = false;
2340 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302341#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302342 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002343 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302344 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002345 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302346 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302347#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302348 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002349 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302350#else
2351 is_configured =
2352 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2353#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002354 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302355#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002356 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002357 ALOGD(" Received APTX dual mono encoder supported BT device");
2358 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302359 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2360 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002361 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2362 is_configured =
2363 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302364 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302365#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002366 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302367 ALOGD(" Received AAC encoder supported BT device");
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002368 bool is_aac_frame_ctl_enabled =
2369 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
2370 is_configured = is_aac_frame_ctl_enabled ?
2371 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2372 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302373 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002374 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302375 ALOGD(" Received CELT encoder supported BT device");
2376 is_configured =
2377 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2378 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002379 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302380 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002381 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2382 instance_id = MAX_INSTANCE_ID;
2383 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302384 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002385 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302386 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302387 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302388#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302389 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302390 ALOGD(" Received APTX AD encoder supported BT device");
2391 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2392 instance_id = MAX_INSTANCE_ID;
2393 a2dp.abr_config.imc_instance = instance_id--;
2394 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2395 a2dp.is_aptx_adaptive = true;
2396 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2397 is_configured =
2398 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302399 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302400 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302401#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002402 case CODEC_TYPE_PCM:
2403 ALOGD("Received PCM format for BT device");
2404 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2405 is_configured = true;
2406 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302407 default:
2408 ALOGD(" Received Unsupported encoder formar");
2409 is_configured = false;
2410 break;
2411 }
2412 return is_configured;
2413}
2414
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002415int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302416{
2417 int ret = 0;
2418
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002419 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302420
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002421 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002422 && a2dp.audio_get_enc_config)) {
2423 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302424 return -ENOSYS;
2425 }
2426
Zhou Song12c29502019-03-16 10:37:18 +08002427 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302428 //session will be restarted after suspend completion
2429 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302430 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302431 }
2432
Florian Pfister1a84f312018-07-19 14:38:18 +02002433 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302434 ALOGD("calling BT module stream start");
2435 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002436 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302437 ALOGE("BT controller start return = %d",ret);
2438 if (ret != 0 ) {
2439 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002440 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302441 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002442 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002443 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302444 ret = 0;
2445 ALOGD("Start playback successful to BT library");
2446 } else {
2447 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002448 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302449 ret = -ETIMEDOUT;
2450 }
2451 }
2452 }
2453
Florian Pfister1a84f312018-07-19 14:38:18 +02002454 if (a2dp.a2dp_source_started) {
2455 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302456 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302457 audio_a2dp_update_tws_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002458 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002459 if (a2dp.abr_config.is_abr_enabled)
2460 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302461 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302462
2463 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002464 a2dp.a2dp_source_total_active_session_requests);
2465 return ret;
2466}
2467
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002468uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002469{
2470 uint32_t latency = 0;
2471
2472 switch(a2dp.bt_decoder_format) {
2473 case CODEC_TYPE_SBC:
2474 latency = DEFAULT_SINK_LATENCY_SBC;
2475 break;
2476 case CODEC_TYPE_AAC:
2477 latency = DEFAULT_SINK_LATENCY_AAC;
2478 break;
2479 default:
2480 latency = 200;
2481 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2482 break;
2483 }
2484 return (uint64_t)latency;
2485}
2486
2487bool a2dp_send_sink_setup_complete(void) {
2488 uint64_t system_latency = 0;
2489 bool is_complete = false;
2490
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002491 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002492
2493 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2494 is_complete = true;
2495 }
2496 return is_complete;
2497}
2498
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002499bool a2dp_sink_is_ready()
2500{
2501 bool ret = false;
2502
2503 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2504 (a2dp.is_a2dp_offload_supported) &&
2505 (a2dp.audio_sink_check_a2dp_ready))
2506 ret = a2dp.audio_sink_check_a2dp_ready();
2507 return ret;
2508}
2509
2510int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002511{
2512 int ret = 0;
2513
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002514 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002515
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002516 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002517 && a2dp.audio_get_dec_config)) {
2518 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2519 return -ENOSYS;
2520 }
2521
2522 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2523 ALOGD("calling BT module stream start");
2524 /* This call indicates BT IPC lib to start capture */
2525 ret = a2dp.audio_sink_start();
2526 ALOGE("BT controller start capture return = %d",ret);
2527 if (ret != 0 ) {
2528 ALOGE("BT controller start capture failed");
2529 a2dp.a2dp_sink_started = false;
2530 } else {
2531
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002532 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002533 ALOGD("Wait for capture ready not successful");
2534 ret = -ETIMEDOUT;
2535 }
2536
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002537 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002538 a2dp.a2dp_sink_started = true;
2539 ret = 0;
2540 ALOGD("Start capture successful to BT library");
2541 } else {
2542 ALOGD(" unable to configure DSP decoder");
2543 a2dp.a2dp_sink_started = false;
2544 ret = -ETIMEDOUT;
2545 }
2546
2547 if (!a2dp_send_sink_setup_complete()) {
2548 ALOGD("sink_setup_complete not successful");
2549 ret = -ETIMEDOUT;
2550 }
2551 }
2552 }
2553
2554 if (a2dp.a2dp_sink_started) {
2555 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002556 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002557 }
2558 }
2559
2560 ALOGD("start A2DP sink total active sessions :%d",
2561 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302562 return ret;
2563}
2564
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302565static void reset_a2dp_enc_config_params()
2566{
2567 int ret =0;
2568
Aalique Grahame22e49102018-12-18 14:23:57 -08002569 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302570 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302571 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302572
2573 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
2574 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
2575 MIXER_ENC_CONFIG_BLOCK);
2576 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002577 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302578 } else {
2579 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
2580 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002581 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302582 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002583
2584 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2585
Manisha Agarwala51768b2018-11-01 16:30:52 +05302586 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2587
2588 if (!ctl_channel_mode) {
2589 ALOGE("failed to get tws mixer ctl");
2590 } else {
2591 channel_mode = "Two";
2592 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2593 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2594 }
2595 a2dp.is_tws_mono_mode_on = false;
2596 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302597}
2598
Surendar Karka2febd452018-12-13 17:56:43 +05302599static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002600{
2601 struct mixer_ctl *ctl_dec_data = NULL;
2602 struct abr_dec_cfg_t dummy_reset_cfg;
2603 int ret = 0;
2604
Preetam Singh Ranawat25780e42019-07-31 18:15:57 +05302605 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
2606 if (!ctl_dec_data) {
2607 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
2608 return -EINVAL;
2609 }
2610 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
2611 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
2612 sizeof(dummy_reset_cfg));
2613 if (ret != 0) {
2614 ALOGE("%s: Failed to set dummy decoder config", __func__);
2615 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002616 }
2617
2618 return ret;
2619}
2620
Surendar Karka2febd452018-12-13 17:56:43 +05302621static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02002622{
2623 int ret =0;
2624
2625 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
2626 struct aac_dec_cfg_t dummy_reset_config;
2627
2628 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
2629 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05302630 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02002631 if (!ctl_dec_config) {
2632 ALOGE(" ERROR a2dp decoder format mixer control not identified");
2633 } else {
2634 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
2635 sizeof(struct aac_dec_cfg_t));
2636 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
2637 }
2638 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
2639 MIXER_DEC_BIT_FORMAT);
2640 if (!ctrl_bit_format) {
2641 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
2642 } else {
2643 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
2644 if (ret != 0) {
2645 ALOGE("%s: Failed to set bit format to decoder", __func__);
2646 }
2647 }
2648}
2649
Zhou Song12c29502019-03-16 10:37:18 +08002650static void reset_codec_config()
2651{
2652 reset_a2dp_enc_config_params();
2653 reset_a2dp_source_dec_config_params();
2654 a2dp_reset_backend_cfg(SOURCE);
2655 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
2656 stop_abr();
2657 a2dp.abr_config.is_abr_enabled = false;
2658}
2659
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002660int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302661{
2662 int ret =0;
2663
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002664 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002665 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002666 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302667 return -ENOSYS;
2668 }
2669
Florian Pfister1a84f312018-07-19 14:38:18 +02002670 if (a2dp.a2dp_source_total_active_session_requests > 0)
2671 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08002672 else
2673 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302674
Florian Pfister1a84f312018-07-19 14:38:18 +02002675 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302676 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02002677 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302678 if (ret < 0)
2679 ALOGE("stop stream to BT IPC lib failed");
2680 else
2681 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08002682 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
2683 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05302684 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302685 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002686 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002687 a2dp.a2dp_source_started = false;
2688 ALOGD("Stop A2DP playback, total active sessions :%d",
2689 a2dp.a2dp_source_total_active_session_requests);
2690 return 0;
2691}
2692
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002693int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002694{
2695 int ret =0;
2696
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002697 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002698 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002699 ALOGE("a2dp handle is not identified, Ignoring stop request");
2700 return -ENOSYS;
2701 }
2702
2703 if (a2dp.a2dp_sink_total_active_session_requests > 0)
2704 a2dp.a2dp_sink_total_active_session_requests--;
2705
2706 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2707 ALOGV("calling BT module stream stop");
2708 ret = a2dp.audio_sink_stop();
2709 if (ret < 0)
2710 ALOGE("stop stream to BT IPC lib failed");
2711 else
2712 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05302713 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002714 a2dp_reset_backend_cfg(SINK);
2715 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002716 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002717 a2dp.a2dp_source_started = false;
2718 ALOGD("Stop A2DP capture, total active sessions :%d",
2719 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302720 return 0;
2721}
2722
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002723int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302724{
Aalique Grahame22e49102018-12-18 14:23:57 -08002725 int ret = 0, val, status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302726 char value[32]={0};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302727 struct audio_usecase *uc_info;
2728 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302729
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002730 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002731 ALOGV("no supported encoders identified,ignoring a2dp setparam");
2732 status = -EINVAL;
2733 goto param_handled;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302734 }
2735
2736 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
2737 sizeof(value));
Zhou Song681350a2017-10-19 16:28:42 +08002738 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302739 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002740 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002741 ALOGV("Received device connect request for A2DP source");
2742 open_a2dp_source();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302743 }
2744 goto param_handled;
2745 }
2746
2747 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
2748 sizeof(value));
2749
Zhou Song681350a2017-10-19 16:28:42 +08002750 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302751 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002752 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002753 ALOGV("Received source device dis- connect request");
Samyak Jain4828f4c2018-08-24 16:31:06 +05302754 close_a2dp_output();
kunleiz4a1fad62018-02-08 18:00:16 +08002755 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302756 reset_a2dp_source_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002757 a2dp_reset_backend_cfg(SOURCE);
2758 } else if (audio_is_a2dp_in_device(val)) {
2759 ALOGV("Received sink device dis- connect request");
2760 close_a2dp_input();
Surendar Karka2febd452018-12-13 17:56:43 +05302761 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002762 a2dp_reset_backend_cfg(SINK);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302763 }
2764 goto param_handled;
2765 }
Surendar Karka2febd452018-12-13 17:56:43 +05302766#ifndef LINUX_ENABLED
Manisha Agarwala51768b2018-11-01 16:30:52 +05302767 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
2768 if (ret>=0) {
2769 ALOGD("Setting tws channel mode to %s",value);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002770 if (!(strncmp(value,"mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302771 a2dp.is_tws_mono_mode_on = true;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002772 else if (!(strncmp(value,"dual-mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302773 a2dp.is_tws_mono_mode_on = false;
2774 audio_a2dp_update_tws_channel_mode();
2775 goto param_handled;
2776 }
Surendar Karka2febd452018-12-13 17:56:43 +05302777#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302778 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
2779 if (ret >= 0) {
Surendar Karka2febd452018-12-13 17:56:43 +05302780 if (a2dp.bt_lib_source_handle) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302781 if ((!strncmp(value,"true",sizeof(value)))) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002782 if (a2dp.a2dp_source_suspended) {
2783 ALOGD("%s: A2DP is already suspended", __func__);
2784 goto param_handled;
2785 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302786 ALOGD("Setting a2dp to suspend state");
Florian Pfister1a84f312018-07-19 14:38:18 +02002787 a2dp.a2dp_source_suspended = true;
Surendar Karka2febd452018-12-13 17:56:43 +05302788 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
yidongh7203cca2018-09-19 16:12:25 +08002789 goto param_handled;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302790 list_for_each(node, &a2dp.adev->usecase_list) {
2791 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302792 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08002793 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302794 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002795 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302796 pthread_mutex_lock(&a2dp.adev->lock);
2797 }
2798 }
Zhou Song12c29502019-03-16 10:37:18 +08002799 if (!a2dp.swb_configured)
2800 reset_codec_config();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002801 if (a2dp.audio_source_suspend)
Florian Pfister1a84f312018-07-19 14:38:18 +02002802 a2dp.audio_source_suspend();
2803 } else if (a2dp.a2dp_source_suspended == true) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302804 ALOGD("Resetting a2dp suspend state");
Zhou Song10617ed2017-05-26 13:28:48 +08002805 struct audio_usecase *uc_info;
2806 struct listnode *node;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002807 if (a2dp.clear_source_a2dpsuspend_flag)
Florian Pfister1a84f312018-07-19 14:38:18 +02002808 a2dp.clear_source_a2dpsuspend_flag();
2809 a2dp.a2dp_source_suspended = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302810 /*
2811 * It is possible that before suspend,a2dp sessions can be active
2812 * for example during music + voice activation concurrency
2813 * a2dp suspend will be called & BT will change to sco mode
2814 * though music is paused as a part of voice activation
2815 * compress session close happens only after pause timeout(10secs)
2816 * so if resume request comes before pause timeout as a2dp session
2817 * is already active IPC start will not be called from APM/audio_hw
2818 * Fix is to call a2dp start for IPC library post suspend
2819 * based on number of active session count
2820 */
Florian Pfister1a84f312018-07-19 14:38:18 +02002821 if (a2dp.a2dp_source_total_active_session_requests > 0) {
Naresh Tanniru649871a2016-11-04 18:08:32 +05302822 ALOGD(" Calling IPC lib start post suspend state");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002823 if (a2dp.audio_source_start) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002824 ret = a2dp.audio_source_start();
Naresh Tanniru649871a2016-11-04 18:08:32 +05302825 if (ret != 0) {
2826 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002827 a2dp.a2dp_source_started = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302828 }
2829 }
2830 }
Zhou Song10617ed2017-05-26 13:28:48 +08002831 list_for_each(node, &a2dp.adev->usecase_list) {
2832 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302833 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Zhou Songc66eb7e2017-08-08 18:29:07 +08002834 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302835 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002836 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302837 pthread_mutex_lock(&a2dp.adev->lock);
2838 }
Zhou Song10617ed2017-05-26 13:28:48 +08002839 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302840 }
2841 }
2842 goto param_handled;
2843 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002844
2845 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
2846 sizeof(value));
2847 if (ret >= 0) {
2848 if (a2dp.is_a2dp_offload_supported &&
2849 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2850 *reconfig = true;
2851 }
2852 goto param_handled;
2853 }
2854
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302855param_handled:
2856 ALOGV("end of a2dp setparam");
Aalique Grahame22e49102018-12-18 14:23:57 -08002857 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302858}
2859
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002860void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302861{
2862 a2dp.is_handoff_in_progress = is_on;
2863}
2864
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002865bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302866{
2867 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05302868 // Or if a2dp device is selected but earlier start failed ( as a2dp
2869 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02002870 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302871}
2872
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002873void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302874{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302875 *sample_rate = a2dp.enc_sampling_rate;
2876}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302877
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002878void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02002879{
2880 *sample_rate = a2dp.dec_sampling_rate;
2881}
2882
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002883bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302884{
2885 bool ret = false;
2886
Florian Pfister1a84f312018-07-19 14:38:18 +02002887 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302888 return ret;
2889
Florian Pfister1a84f312018-07-19 14:38:18 +02002890 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08002891 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002892 (a2dp.audio_source_check_a2dp_ready))
2893 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302894 return ret;
2895}
2896
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002897bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02002898{
2899 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302900}
2901
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002902void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08002903 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302904{
2905 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02002906 a2dp.bt_lib_source_handle = NULL;
2907 a2dp.a2dp_source_started = false;
2908 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
2909 a2dp.a2dp_source_total_active_session_requests = 0;
2910 a2dp.a2dp_source_suspended = false;
2911 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302912 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302913 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002914 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302915 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002916 a2dp.abr_config.is_abr_enabled = false;
2917 a2dp.abr_config.abr_started = false;
2918 a2dp.abr_config.imc_instance = 0;
2919 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08002920 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302921 a2dp.is_tws_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05302922 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08002923 a2dp.swb_configured = false;
2924
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002925 // init function pointers
2926 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08002927 init_config.fp_platform_get_pcm_device_id;
2928 fp_check_a2dp_restore = init_config.fp_check_a2dp_restore;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002929
kunleiz5a127262017-09-08 14:47:48 +08002930 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302931 reset_a2dp_source_dec_config_params();
2932 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002933
2934 a2dp.bt_lib_sink_handle = NULL;
2935 a2dp.a2dp_sink_started = false;
2936 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
2937 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002938
2939 if (is_running_with_enhanced_fwk == UNINITIALIZED)
2940 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
2941 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08002942 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02002943
2944 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302945 update_offload_codec_capabilities();
2946}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002947
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002948uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002949{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002950 uint32_t latency = 0;
2951 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302952 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
2953 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002954 char value[PROPERTY_VALUE_MAX];
2955
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002956 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08002957 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002958 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302959 if (sscanf(value, "%d/%d/%d/%d/%d%d",
2960 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002961 ALOGI("Failed to parse avsync offset params from '%s'.", value);
2962 avsync_runtime_prop = 0;
2963 }
2964 }
2965
yidongh0515e042017-07-06 15:00:34 +08002966 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02002967 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2968 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08002969 }
2970
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07002971 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002972 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08002973 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
2974 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002975 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002976 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08002977 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
2978 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002979 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002980 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08002981 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
2982 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002983 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002984 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08002985 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
2986 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002987 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002988 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302989 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
2990 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
2991 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002992 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302993 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
2994 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
2995 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05302996 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05302997 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05302998 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08002999 case CODEC_TYPE_PCM:
3000 latency = ENCODER_LATENCY_PCM;
3001 latency += DEFAULT_SINK_LATENCY_PCM;
3002 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003003 default:
3004 latency = 200;
3005 break;
3006 }
3007 return latency;
3008}
Aalique Grahame22e49102018-12-18 14:23:57 -08003009
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003010int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003011 struct str_parms *reply)
3012{
3013 int ret, val = 0;
3014 char value[32]={0};
3015
3016 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3017 value, sizeof(value));
3018 if (ret >= 0) {
3019 val = a2dp.is_a2dp_offload_supported;
3020 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3021 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3022 }
3023
3024 return 0;
3025}
Zhou Song12c29502019-03-16 10:37:18 +08003026
3027
3028bool configure_aptx_ad_speech_enc_fmt() {
3029 struct mixer_ctl *ctl_enc_data = NULL;
3030 int mixer_size = 0;
3031 int ret = 0;
3032 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3033
3034 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3035 if (!ctl_enc_data) {
3036 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3037 return false;
3038 }
3039
3040 /* Initialize dsp configuration params */
3041 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3042 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3043 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3044 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3045 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3046 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3047 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3048 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3049 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3050 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3051 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3052
3053 /* Configure AFE DSP configuration */
3054 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3055 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3056 mixer_size);
3057 if (ret != 0) {
3058 ALOGE("%s: Failed to set SWB encoder config", __func__);
3059 return false;
3060 }
3061
3062 /* Configure AFE Input Bit Format as PCM_16 */
3063 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3064 if (ret != 0) {
3065 ALOGE("%s: Failed to set SWB bit format", __func__);
3066 return false;
3067 }
3068
3069 return true;
3070}
3071
3072bool configure_aptx_ad_speech_dec_fmt()
3073{
3074 struct mixer_ctl *ctl_dec_data = NULL;
3075 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3076 int ret = 0;
3077
3078 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3079 if (!ctl_dec_data) {
3080 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3081 return false;
3082 }
3083 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3084 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3085 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3086 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3087 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3088 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3089 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3090 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3091
3092 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3093 sizeof(dec_cfg));
3094 if (ret != 0) {
3095 ALOGE("%s: Failed to set decoder config", __func__);
3096 return false;
3097 }
3098 return true;
3099}
3100
3101int sco_start_configuration()
3102{
3103 ALOGD("sco_start_configuration start");
3104
3105 if (!a2dp.swb_configured) {
3106 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3107 /* Configure AFE codec*/
3108 if (configure_aptx_ad_speech_enc_fmt() &&
3109 configure_aptx_ad_speech_dec_fmt()) {
3110 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3111 } else {
3112 ALOGE("%s: failed to send SCO configuration", __func__);
3113 return -ETIMEDOUT;
3114 }
3115 /* Configure backend*/
3116 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3117 a2dp.enc_channels = CH_MONO;
3118 a2dp.abr_config.is_abr_enabled = true;
3119 a2dp_set_backend_cfg(SOURCE);
3120 /* Start abr*/
3121 start_abr();
3122 a2dp.swb_configured = true;
3123 }
3124 return 0;
3125}
3126
3127void sco_reset_configuration()
3128{
Zhou Songd6d71752019-05-21 18:08:51 +08003129 if (a2dp.swb_configured) {
3130 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003131
Zhou Songd6d71752019-05-21 18:08:51 +08003132 reset_codec_config();
3133 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3134 a2dp.swb_configured = false;
3135 }
Zhou Song12c29502019-03-16 10:37:18 +08003136}