blob: 00e581f6bcbdf65c07e77d3f8b809527ea5e1ac7 [file] [log] [blame]
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301/*
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002* Copyright (c) 2015-2020, The Linux Foundation. All rights reserved.
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of The Linux Foundation nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
Aalique Grahame22e49102018-12-18 14:23:57 -080029#define LOG_TAG "a2dp_offload"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053030/*#define LOG_NDEBUG 0*/
31#define LOG_NDDEBUG 0
32#include <errno.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080033#include <log/log.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053034#include <dlfcn.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080035#include <pthread.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
Manish Dewangan6a252632017-12-04 17:27:44 +053039#include "audio_extn.h"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053040#include <stdlib.h>
41#include <cutils/str_parms.h>
42#include <hardware/audio.h>
43#include <hardware/hardware.h>
44#include <cutils/properties.h>
45
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046#ifdef DYNAMIC_LOG_ENABLED
47#include <log_xml_parser.h>
48#define LOG_MASK HAL_MOD_FILE_A2DP
49#include <log_utils.h>
50#endif
51
Naresh Tanniru66cf06c2019-03-20 19:30:37 +053052#define BT_IPC_SOURCE_LIB_NAME "btaudio_offload_if.so"
Florian Pfister1a84f312018-07-19 14:38:18 +020053#define BT_IPC_SINK_LIB_NAME "libbthost_if_sink.so"
54#define MEDIA_FMT_NONE 0
55#define MEDIA_FMT_AAC 0x00010DA6
56#define MEDIA_FMT_APTX 0x000131ff
57#define MEDIA_FMT_APTX_HD 0x00013200
Surendar Karka2febd452018-12-13 17:56:43 +053058#define MEDIA_FMT_APTX_AD 0x00013204
Florian Pfister1a84f312018-07-19 14:38:18 +020059#define MEDIA_FMT_SBC 0x00010BF2
60#define MEDIA_FMT_CELT 0x00013221
61#define MEDIA_FMT_LDAC 0x00013224
62#define MEDIA_FMT_MP3 0x00010BE9
63#define MEDIA_FMT_APTX_ADAPTIVE 0x00013204
Zhou Song12c29502019-03-16 10:37:18 +080064#define MEDIA_FMT_APTX_AD_SPEECH 0x00013208
Naresh Tanniru9d027a62015-03-13 01:32:10 +053065#define MEDIA_FMT_AAC_AOT_LC 2
66#define MEDIA_FMT_AAC_AOT_SBR 5
67#define MEDIA_FMT_AAC_AOT_PS 29
Naresh Tanniru9d027a62015-03-13 01:32:10 +053068#define PCM_CHANNEL_L 1
69#define PCM_CHANNEL_R 2
70#define PCM_CHANNEL_C 3
71#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
72#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
73#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
74#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
75#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
76#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
Surendar Karka2febd452018-12-13 17:56:43 +053077#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
Zhou Song8fccbb62019-03-20 01:08:19 +080078#define MIXER_ENC_APTX_AD_CONFIG_BLOCK "SLIM_7_RX APTX_AD Enc Cfg"
Surendar Karka2febd452018-12-13 17:56:43 +053079#define MIXER_SOURCE_DEC_CONFIG_BLOCK "SLIM_7_TX Decoder Config"
80#define MIXER_SINK_DEC_CONFIG_BLOCK "SLIM_9_TX Decoder Config"
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +053081#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
Florian Pfister1a84f312018-07-19 14:38:18 +020082#define MIXER_DEC_BIT_FORMAT "AFE Output Bit Format"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053083#define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080084#define MIXER_SAMPLE_RATE_RX "BT SampleRate RX"
Surendar Karka2febd452018-12-13 17:56:43 +053085#define MIXER_SOURCE_SAMPLE_RATE_TX "BT SampleRate TX"
Samyak Jainf69e9ef2018-06-12 12:26:37 +053086#define MIXER_SAMPLE_RATE_DEFAULT "BT SampleRate"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053087#define MIXER_AFE_IN_CHANNELS "AFE Input Channels"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080088#define MIXER_ABR_TX_FEEDBACK_PATH "A2DP_SLIM7_UL_HL Switch"
Zhou Song12c29502019-03-16 10:37:18 +080089#define MIXER_ABR_RX_FEEDBACK_PATH "SCO_SLIM7_DL_HL Switch"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080090#define MIXER_SET_FEEDBACK_CHANNEL "BT set feedback channel"
Surendar Karka2febd452018-12-13 17:56:43 +053091#define MIXER_SINK_SAMPLE_RATE "BT_TX SampleRate"
Florian Pfister1a84f312018-07-19 14:38:18 +020092#define MIXER_AFE_SINK_CHANNELS "AFE Output Channels"
Manisha Agarwala51768b2018-11-01 16:30:52 +053093#define MIXER_FMT_TWS_CHANNEL_MODE "TWS Channel Mode"
yidongh0515e042017-07-06 15:00:34 +080094#define ENCODER_LATENCY_SBC 10
95#define ENCODER_LATENCY_APTX 40
96#define ENCODER_LATENCY_APTX_HD 20
97#define ENCODER_LATENCY_AAC 70
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +053098//To Do: Fine Tune Encoder CELT/LDAC latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +053099#define ENCODER_LATENCY_CELT 40
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530100#define ENCODER_LATENCY_LDAC 40
Aalique Grahame22e49102018-12-18 14:23:57 -0800101#define ENCODER_LATENCY_PCM 50
yidongh0515e042017-07-06 15:00:34 +0800102#define DEFAULT_SINK_LATENCY_SBC 140
103#define DEFAULT_SINK_LATENCY_APTX 160
104#define DEFAULT_SINK_LATENCY_APTX_HD 180
105#define DEFAULT_SINK_LATENCY_AAC 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530106//To Do: Fine Tune Default CELT/LDAC Latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530107#define DEFAULT_SINK_LATENCY_CELT 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530108#define DEFAULT_SINK_LATENCY_LDAC 180
Aalique Grahame22e49102018-12-18 14:23:57 -0800109#define DEFAULT_SINK_LATENCY_PCM 140
110
111#define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported"
112#define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled"
113#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
114
115// Default encoder bit width
116#define DEFAULT_ENCODER_BIT_FORMAT 16
117
118// Default encoder latency
119#define DEFAULT_ENCODER_LATENCY 200
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530120
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800121// Slimbus Tx sample rate for ABR feedback channel
122#define ABR_TX_SAMPLE_RATE "KHZ_8"
123
Zhou Song12c29502019-03-16 10:37:18 +0800124// Slimbus Tx sample rate for APTX AD SPEECH
125#define SPEECH_TX_SAMPLE_RATE "KHZ_96"
126
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800127// Purpose ID for Inter Module Communication (IMC) in AFE
128#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
129
130// Maximum quality levels for ABR
131#define MAX_ABR_QUALITY_LEVELS 5
132
133// Instance identifier for A2DP
134#define MAX_INSTANCE_ID (UINT32_MAX / 2)
135
Zhou Song12c29502019-03-16 10:37:18 +0800136// Instance identifier for SWB
137#define APTX_AD_SPEECH_INSTANCE_ID 37
138
139#define SAMPLING_RATE_96K 96000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530140#define SAMPLING_RATE_48K 48000
141#define SAMPLING_RATE_441K 44100
Zhou Song12c29502019-03-16 10:37:18 +0800142#define SAMPLING_RATE_32K 32000
Sharad Sangle95d451b2018-06-19 12:24:20 +0530143#define CH_STEREO 2
144#define CH_MONO 1
Florian Pfister1a84f312018-07-19 14:38:18 +0200145#define SOURCE 0
146#define SINK 1
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800147#define UNINITIALIZED -1
148
149#ifdef __LP64__
150#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
151#else
152#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
153#endif
154
155static void *vndk_fwk_lib_handle = NULL;
156static int is_running_with_enhanced_fwk = UNINITIALIZED;
157
158typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
159static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
Florian Pfister1a84f312018-07-19 14:38:18 +0200160
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530161/*
162 * Below enum values are extended from audio_base.h to
Florian Pfister1a84f312018-07-19 14:38:18 +0200163 * to keep encoder and decoder type local to bthost_ipc
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530164 * and audio_hal as these are intended only for handshake
165 * between IPC lib and Audio HAL.
166 */
167typedef enum {
Florian Pfister1a84f312018-07-19 14:38:18 +0200168 CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
169 CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
170 CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
171 CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
172 CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530173#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +0200174 CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530175#endif
Florian Pfister1a84f312018-07-19 14:38:18 +0200176 CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
177 CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
Surendar Karka2febd452018-12-13 17:56:43 +0530178 CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
Zhou Song12c29502019-03-16 10:37:18 +0800179 CODEC_TYPE_APTX_AD_SPEECH = 637534208u, //0x26000000UL
Aalique Grahame22e49102018-12-18 14:23:57 -0800180 CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u
Florian Pfister1a84f312018-07-19 14:38:18 +0200181}codec_t;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530182
Sharad Sangle95d451b2018-06-19 12:24:20 +0530183/*
184 * enums which describes the APTX Adaptive
185 * channel mode, these values are used by encoder
186 */
187 typedef enum {
188 APTX_AD_CHANNEL_UNCHANGED = -1,
189 APTX_AD_CHANNEL_JOINT_STEREO = 0, // default
190 APTX_AD_CHANNEL_MONO = 1,
191 APTX_AD_CHANNEL_DUAL_MONO = 2,
192 APTX_AD_CHANNEL_STEREO_TWS = 4,
193 APTX_AD_CHANNEL_EARBUD = 8,
194} enc_aptx_ad_channel_mode;
195
196/*
197 * enums which describes the APTX Adaptive
198 * sampling frequency, these values are used
199 * by encoder
200 */
201typedef enum {
202 APTX_AD_SR_UNCHANGED = 0x0,
203 APTX_AD_48 = 0x1, // 48 KHz default
204 APTX_AD_44_1 = 0x2, // 44.1kHz
Zhou Song30e8cea2019-10-22 23:39:25 +0800205 APTX_AD_96 = 0x3, // 96KHz
Sharad Sangle95d451b2018-06-19 12:24:20 +0530206} enc_aptx_ad_s_rate;
207
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530208typedef void (*bt_audio_pre_init_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200209typedef int (*audio_source_open_t)(void);
210typedef int (*audio_source_close_t)(void);
211typedef int (*audio_source_start_t)(void);
212typedef int (*audio_source_stop_t)(void);
213typedef int (*audio_source_suspend_t)(void);
214typedef void (*audio_source_handoff_triggered_t)(void);
215typedef void (*clear_source_a2dpsuspend_flag_t)(void);
216typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
217 uint8_t *num_dev, codec_t *codec_type);
218typedef int (*audio_source_check_a2dp_ready_t)(void);
219typedef int (*audio_is_source_scrambling_enabled_t)(void);
Manisha Agarwala51768b2018-11-01 16:30:52 +0530220typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200221typedef int (*audio_sink_start_t)(void);
222typedef int (*audio_sink_stop_t)(void);
223typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
224typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
225typedef int (*audio_sink_check_a2dp_ready_t)(void);
226typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530227
228enum A2DP_STATE {
229 A2DP_STATE_CONNECTED,
230 A2DP_STATE_STARTED,
231 A2DP_STATE_STOPPED,
232 A2DP_STATE_DISCONNECTED,
233};
234
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800235typedef enum {
236 IMC_TRANSMIT,
237 IMC_RECEIVE,
238} imc_direction_t;
239
240typedef enum {
241 IMC_DISABLE,
242 IMC_ENABLE,
243} imc_status_t;
244
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700245typedef enum {
Zhou Song12c29502019-03-16 10:37:18 +0800246 SWAP_DISABLE,
247 SWAP_ENABLE,
248} swap_status_t;
249
250typedef enum {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700251 MTU_SIZE,
252 PEAK_BIT_RATE,
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530253 BIT_RATE_MODE,
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700254} frame_control_type_t;
255
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800256// --- external function dependency ---
257fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
258fp_check_a2dp_restore_t fp_check_a2dp_restore;
259
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800260/* PCM config for ABR Feedback hostless front end */
261static struct pcm_config pcm_config_abr = {
262 .channels = 1,
263 .rate = 8000,
264 .period_size = 240,
265 .period_count = 2,
266 .format = PCM_FORMAT_S16_LE,
267 .start_threshold = 0,
268 .stop_threshold = INT_MAX,
269 .avail_min = 0,
270};
271
272/* Adaptive bitrate config for A2DP codecs */
273struct a2dp_abr_config {
274 /* Flag to denote whether Adaptive bitrate is enabled for codec */
275 bool is_abr_enabled;
276 /* Flag to denote whether front end has been opened for ABR */
277 bool abr_started;
278 /* ABR Tx path pcm handle */
279 struct pcm *abr_tx_handle;
Zhou Song12c29502019-03-16 10:37:18 +0800280 /* ABR Rx path pcm handle */
281 struct pcm *abr_rx_handle;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800282 /* ABR Inter Module Communication (IMC) instance ID */
283 uint32_t imc_instance;
284};
285
286static uint32_t instance_id = MAX_INSTANCE_ID;
287
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530288/* structure used to update a2dp state machine
289 * to communicate IPC library
290 * to store DSP encoder configuration information
291 */
292struct a2dp_data {
293 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200294 void *bt_lib_source_handle;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530295 bt_audio_pre_init_t bt_audio_pre_init;
Florian Pfister1a84f312018-07-19 14:38:18 +0200296 audio_source_open_t audio_source_open;
297 audio_source_close_t audio_source_close;
298 audio_source_start_t audio_source_start;
299 audio_source_stop_t audio_source_stop;
300 audio_source_suspend_t audio_source_suspend;
301 audio_source_handoff_triggered_t audio_source_handoff_triggered;
302 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
303 audio_get_enc_config_t audio_get_enc_config;
304 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530305 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200306 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
307 enum A2DP_STATE bt_state_source;
308 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530309 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530310 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200311 bool a2dp_source_started;
312 bool a2dp_source_suspended;
313 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530314 bool is_a2dp_offload_supported;
315 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700316 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530317 /* Mono Mode support for TWS+ */
318 bool is_tws_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530319 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800320 /* Adaptive bitrate config for A2DP codecs */
321 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200322
323 void *bt_lib_sink_handle;
324 audio_sink_start_t audio_sink_start;
325 audio_sink_stop_t audio_sink_stop;
326 audio_get_dec_config_t audio_get_dec_config;
327 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
328 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
329 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
330 enum A2DP_STATE bt_state_sink;
331 codec_t bt_decoder_format;
332 uint32_t dec_sampling_rate;
333 uint32_t dec_channels;
334 bool a2dp_sink_started;
335 int a2dp_sink_total_active_session_requests;
Zhou Song12c29502019-03-16 10:37:18 +0800336 bool swb_configured;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530337};
338
339struct a2dp_data a2dp;
340
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800341/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
342 * Structures sent to configure DSP for ABR are defined below.
343 * This data helps DSP configure feedback path (BTSoC to LPASS)
344 * for link quality levels and mapping quality levels to codec
345 * specific bitrate.
346 */
347
348/* Key value pair for link quality level to bitrate mapping. */
349struct bit_rate_level_map_t {
350 uint32_t link_quality_level;
351 uint32_t bitrate;
352};
353
354/* Link quality level to bitrate mapping info sent to DSP. */
355struct quality_level_to_bitrate_info {
356 /* Number of quality levels being mapped.
357 * This will be equal to the size of mapping table.
358 */
359 uint32_t num_levels;
360 /* Quality level to bitrate mapping table */
361 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
362};
363
364/* Structure to set up Inter Module Communication (IMC) between
365 * AFE Decoder and Encoder.
366 */
367struct imc_dec_enc_info {
368 /* Decoder to encoder communication direction.
369 * Transmit = 0 / Receive = 1
370 */
371 uint32_t direction;
372 /* Enable / disable IMC between decoder and encoder */
373 uint32_t enable;
374 /* Purpose of IMC being set up between decoder and encoder.
375 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
376 * is the default value to be sent as purpose.
377 */
378 uint32_t purpose;
379 /* Unique communication instance ID.
380 * purpose and comm_instance together form the actual key
381 * used in IMC registration, which must be the same for
382 * encoder and decoder for which IMC is being set up.
383 */
384 uint32_t comm_instance;
385};
386
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700387/* Structure to control frame size of AAC encoded frames. */
388struct aac_frame_size_control_t {
389 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
390 uint32_t ctl_type;
391 /* Control value
392 * MTU_SIZE: MTU size in bytes
393 * PEAK_BIT_RATE: Peak bitrate in bits per second.
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530394 * BIT_RATE_MODE: Variable bitrate
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700395 */
396 uint32_t ctl_value;
397};
398
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800399/* Structure used for ABR config of AFE encoder and decoder. */
400struct abr_enc_cfg_t {
401 /* Link quality level to bitrate mapping info sent to DSP. */
402 struct quality_level_to_bitrate_info mapping_info;
403 /* Information to set up IMC between decoder and encoder */
404 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700405 /* Flag to indicate whether ABR is enabled */
406 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800407} __attribute__ ((packed));
408
409/* Structure to send configuration for decoder introduced
410 * on AFE Tx path for ABR link quality feedback to BT encoder.
411 */
412struct abr_dec_cfg_t {
413 /* Decoder media format */
414 uint32_t dec_format;
415 /* Information to set up IMC between decoder and encoder */
416 struct imc_dec_enc_info imc_info;
417} __attribute__ ((packed));
418
Zhou Song12c29502019-03-16 10:37:18 +0800419struct aptx_ad_speech_mode_cfg_t
420{
421 uint32_t mode;
422 uint32_t swapping;
423} __attribute__ ((packed));
424
425/* Structure for SWB voice dec config */
426struct aptx_ad_speech_dec_cfg_t {
427 struct abr_dec_cfg_t abr_cfg;
428 struct aptx_ad_speech_mode_cfg_t speech_mode;
429} __attribute__ ((packed));
430
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530431/* START of DSP configurable structures
432 * These values should match with DSP interface defintion
433 */
434
435/* AAC encoder configuration structure. */
436typedef struct aac_enc_cfg_t aac_enc_cfg_t;
437
438/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
439 * supported aac_fmt_flag are ADTS/RAW
440 * supported channel_cfg are Native mode, Mono , Stereo
441 */
442struct aac_enc_cfg_t {
443 uint32_t enc_format;
444 uint32_t bit_rate;
445 uint32_t enc_mode;
446 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530447 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530448 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530449} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530450
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700451struct aac_enc_cfg_v2_t {
452 struct aac_enc_cfg_t aac_enc_cfg;
453 struct aac_frame_size_control_t frame_ctl;
454} __attribute__ ((packed));
455
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530456struct aac_enc_cfg_v3_t {
457 struct aac_enc_cfg_t aac_enc_cfg;
458 struct aac_frame_size_control_t frame_ctl;
459 struct aac_frame_size_control_t aac_key_value_ctl;
460} __attribute__ ((packed));
461
Surendar Karkabbb3c822018-11-12 13:00:38 +0530462typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
463struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200464 uint16_t aac_fmt_flag; /* LATM*/
465 uint16_t audio_object_type; /* LC */
466 uint16_t channels; /* Stereo */
467 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
468 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
469 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530470} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200471
Surendar Karkabbb3c822018-11-12 13:00:38 +0530472typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
473struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200474 uint16_t channels; /* Mono, Stereo */
475 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
476 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530477} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200478
479/* AAC decoder configuration structure. */
480typedef struct aac_dec_cfg_t aac_dec_cfg_t;
481struct aac_dec_cfg_t {
482 uint32_t dec_format;
483 audio_aac_decoder_config_t data;
484} __attribute__ ((packed));
485
486/* SBC decoder configuration structure. */
487typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
488struct sbc_dec_cfg_t {
489 uint32_t dec_format;
490 audio_sbc_decoder_config_t data;
491} __attribute__ ((packed));
492
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530493/* SBC encoder configuration structure. */
494typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
495
496/* supported num_subbands are 4/8
497 * supported blk_len are 4, 8, 12, 16
498 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
499 * supported alloc_method are LOUNDNESS/SNR
500 * supported bit_rate for mono channel is max 320kbps
501 * supported bit rate for stereo channel is max 512 kbps
502 */
503struct sbc_enc_cfg_t{
504 uint32_t enc_format;
505 uint32_t num_subbands;
506 uint32_t blk_len;
507 uint32_t channel_mode;
508 uint32_t alloc_method;
509 uint32_t bit_rate;
510 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530511} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530512
513
514/* supported num_channels are Mono/Stereo
515 * supported channel_mapping for mono is CHANNEL_C
516 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
517 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700518 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530519struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530520{
521 uint32_t enc_format;
522 uint32_t sample_rate;
523 uint16_t num_channels;
524 uint16_t reserved;
525 uint8_t channel_mapping[8];
526 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530527} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530528
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530529struct celt_specific_enc_cfg_t
530{
531 uint32_t bit_rate;
532 uint16_t frame_size;
533 uint16_t complexity;
534 uint16_t prediction_mode;
535 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530536} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530537
538struct celt_enc_cfg_t
539{
540 struct custom_enc_cfg_t custom_cfg;
541 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530542} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700543
544/* sync_mode introduced with APTX V2 libraries
545 * sync mode: 0x0 = stereo sync mode
546 * 0x01 = dual mono sync mode
547 * 0x02 = dual mono with no sync on either L or R codewords
548 */
549struct aptx_v2_enc_cfg_ext_t
550{
551 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530552} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700553
554/* APTX struct for combining custom enc and V2 fields */
555struct aptx_enc_cfg_t
556{
557 struct custom_enc_cfg_t custom_cfg;
558 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530559} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700560
Sharad Sangle95d451b2018-06-19 12:24:20 +0530561/* APTX AD structure */
562struct aptx_ad_enc_cfg_ext_t
563{
564 uint32_t sampling_freq;
565 uint32_t mtu;
566 uint32_t channel_mode;
567 uint32_t min_sink_modeA;
568 uint32_t max_sink_modeA;
569 uint32_t min_sink_modeB;
570 uint32_t max_sink_modeB;
571 uint32_t min_sink_modeC;
572 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530573 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530574} __attribute__ ((packed));
575
576struct aptx_ad_enc_cfg_t
577{
578 struct custom_enc_cfg_t custom_cfg;
579 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
580 struct abr_enc_cfg_t abr_cfg;
581} __attribute__ ((packed));
582
Zhou Song8fccbb62019-03-20 01:08:19 +0800583struct aptx_ad_enc_cfg_ext_r2_t
584{
585 uint32_t sampling_freq;
586 uint32_t mtu;
587 uint32_t channel_mode;
588 uint32_t min_sink_modeA;
589 uint32_t max_sink_modeA;
590 uint32_t min_sink_modeB;
591 uint32_t max_sink_modeB;
592 uint32_t min_sink_modeC;
593 uint32_t max_sink_modeC;
594 uint32_t mode;
595 uint32_t input_mode;
596 uint32_t fade_duration;
597 uint8_t sink_cap[11];
598} __attribute__ ((packed));
599
600struct aptx_ad_enc_cfg_r2_t
601{
602 struct custom_enc_cfg_t custom_cfg;
603 struct aptx_ad_enc_cfg_ext_r2_t aptx_ad_cfg;
604 struct abr_enc_cfg_t abr_cfg;
605} __attribute__ ((packed));
606
Zhou Song12c29502019-03-16 10:37:18 +0800607/* APTX AD SPEECH structure */
608struct aptx_ad_speech_enc_cfg_t
609{
610 struct custom_enc_cfg_t custom_cfg;
611 /* Information to set up IMC between decoder and encoder */
612 struct imc_dec_enc_info imc_info;
613 struct aptx_ad_speech_mode_cfg_t speech_mode;
614} __attribute__ ((packed));
615
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530616struct ldac_specific_enc_cfg_t
617{
618 uint32_t bit_rate;
619 uint16_t channel_mode;
620 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530621} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530622
623struct ldac_enc_cfg_t
624{
625 struct custom_enc_cfg_t custom_cfg;
626 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800627 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530628} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530629
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530630/* In LE BT source code uses system/audio.h for below
631 * structure definition. To avoid multiple definition
632 * compilation error for audiohal in LE , masking structure
633 * definition under "LINUX_ENABLED" which is defined only
634 * in LE
635 */
636#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530637/* TODO: Define the following structures only for O using PLATFORM_VERSION */
638/* Information about BT SBC encoder configuration
639 * This data is used between audio HAL module and
640 * BT IPC library to configure DSP encoder
641 */
642typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530643 uint32_t subband; /* 4, 8 */
644 uint32_t blk_len; /* 4, 8, 12, 16 */
645 uint16_t sampling_rate; /*44.1khz,48khz*/
646 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
647 uint8_t alloc; /*0(Loudness),1(SNR)*/
648 uint8_t min_bitpool; /* 2 */
649 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
650 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530651 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530652} audio_sbc_encoder_config;
653
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530654/* Information about BT APTX encoder configuration
655 * This data is used between audio HAL module and
656 * BT IPC library to configure DSP encoder
657 */
658typedef struct {
659 uint16_t sampling_rate;
660 uint8_t channels;
661 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530662 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700663} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530664
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700665typedef struct {
Zhou Song8fccbb62019-03-20 01:08:19 +0800666 uint32_t sampling_rate;
667 uint32_t mtu;
668 int32_t channel_mode;
669 uint32_t min_sink_modeA;
670 uint32_t max_sink_modeA;
671 uint32_t min_sink_modeB;
672 uint32_t max_sink_modeB;
673 uint32_t min_sink_modeC;
674 uint32_t max_sink_modeC;
675 uint32_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530676 uint8_t TTP_modeA_low;
677 uint8_t TTP_modeA_high;
678 uint8_t TTP_modeB_low;
679 uint8_t TTP_modeB_high;
Zhou Song8fccbb62019-03-20 01:08:19 +0800680 uint8_t TTP_TWS_low;
681 uint8_t TTP_TWS_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530682 uint32_t bits_per_sample;
Zhou Song8fccbb62019-03-20 01:08:19 +0800683 uint32_t input_mode;
684 uint32_t fade_duration;
685 uint8_t sink_cap[11];
Sharad Sangle95d451b2018-06-19 12:24:20 +0530686} audio_aptx_ad_config;
687
688typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700689 uint16_t sampling_rate;
690 uint8_t channels;
691 uint32_t bitrate;
692 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530693 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700694} audio_aptx_dual_mono_config;
695
696typedef union {
697 audio_aptx_default_config *default_cfg;
698 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530699 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700700} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530701
702/* Information about BT AAC encoder configuration
703 * This data is used between audio HAL module and
704 * BT IPC library to configure DSP encoder
705 */
706typedef struct {
707 uint32_t enc_mode; /* LC, SBR, PS */
708 uint16_t format_flag; /* RAW, ADTS */
709 uint16_t channels; /* 1-Mono, 2-Stereo */
710 uint32_t sampling_rate;
711 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530712 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530713} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530714#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700715
716typedef struct {
717 audio_aac_encoder_config audio_aac_enc_cfg;
718 struct aac_frame_size_control_t frame_ctl;
719} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530720
Manisha Agarwalabb536a2020-01-30 16:12:44 +0530721typedef struct {
722 audio_aac_encoder_config audio_aac_enc_cfg;
723 struct aac_frame_size_control_t frame_ctl;
724 uint8_t size_control_struct;
725 struct aac_frame_size_control_t* frame_ptr_ctl;
726} audio_aac_encoder_config_v3;
727
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530728/* Information about BT CELT encoder configuration
729 * This data is used between audio HAL module and
730 * BT IPC library to configure DSP encoder
731 */
732typedef struct {
733 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
734 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
735 uint16_t frame_size; /* 64-128-256-512, 512 */
736 uint16_t complexity; /* 0-10, 1 */
737 uint16_t prediction_mode; /* 0-1-2, 0 */
738 uint16_t vbr_flag; /* 0-1, 0*/
739 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530740 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530741} audio_celt_encoder_config;
742
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530743/* Information about BT LDAC encoder configuration
744 * This data is used between audio HAL module and
745 * BT IPC library to configure DSP encoder
746 */
747typedef struct {
748 uint32_t sampling_rate; /*44100,48000,88200,96000*/
749 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
750 uint16_t channel_mode; /* 0, 4, 2, 1*/
751 uint16_t mtu; /*679*/
Aniket Kumar Latad0196282019-05-09 14:24:17 -0700752 uint32_t bits_per_sample;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800753 bool is_abr_enabled;
754 struct quality_level_to_bitrate_info level_to_bitrate_map;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530755} audio_ldac_encoder_config;
756
Surendar Karkabbb3c822018-11-12 13:00:38 +0530757/* Information about BT AAC decoder configuration
758 * This data is used between audio HAL module and
759 * BT IPC library to configure DSP decoder
760 */
761typedef struct {
762 uint16_t aac_fmt_flag; /* LATM*/
763 uint16_t audio_object_type; /* LC */
764 uint16_t channels; /* Stereo */
765 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
766 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
767 44.1k, 48k, 64k, 88.2k, 96k */
768} audio_aac_dec_config_t;
769
770/* Information about BT SBC decoder configuration
771 * This data is used between audio HAL module and
772 * BT IPC library to configure DSP decoder
773 */
774typedef struct {
775 uint16_t channels; /* Mono, Stereo */
776 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
777 44.1k, 48k, 64k, 88.2k, 96k */
778}audio_sbc_dec_config_t;
779
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530780/*********** END of DSP configurable structures ********************/
781
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530782static void update_offload_codec_capabilities()
783{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530784
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530785 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800786 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
787 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
788
789 ALOGD("%s: A2DP offload supported = %d",__func__,
790 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530791}
792
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800793static int stop_abr()
794{
795 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800796 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800797 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800798 int ret = 0;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800799
800 /* This function can be used if !abr_started for clean up */
801 ALOGV("%s: enter", __func__);
802
803 // Close hostless front end
804 if (a2dp.abr_config.abr_tx_handle != NULL) {
805 pcm_close(a2dp.abr_config.abr_tx_handle);
806 a2dp.abr_config.abr_tx_handle = NULL;
807 }
Zhou Song12c29502019-03-16 10:37:18 +0800808 if (a2dp.abr_config.abr_rx_handle != NULL) {
809 pcm_close(a2dp.abr_config.abr_rx_handle);
810 a2dp.abr_config.abr_rx_handle = NULL;
811 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800812 a2dp.abr_config.abr_started = false;
813 a2dp.abr_config.imc_instance = 0;
814
815 // Reset BT driver mixer control for ABR usecase
816 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
817 MIXER_SET_FEEDBACK_CHANNEL);
818 if (!ctl_set_bt_feedback_channel) {
819 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800820 ret = -ENOSYS;
821 } else if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800822 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800823 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800824 }
825
826 // Reset ABR Tx feedback path
827 ALOGV("%s: Disable ABR Tx feedback path", __func__);
828 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
829 MIXER_ABR_TX_FEEDBACK_PATH);
830 if (!ctl_abr_tx_path) {
831 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800832 ret = -ENOSYS;
833 } else if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800834 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800835 ret = -ENOSYS;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800836 }
837
Zhou Song12c29502019-03-16 10:37:18 +0800838 // Reset ABR Rx feedback path
839 ALOGV("%s: Disable ABR Rx feedback path", __func__);
840 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
841 MIXER_ABR_RX_FEEDBACK_PATH);
842 if (!ctl_abr_rx_path) {
843 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
844 ret = -ENOSYS;
845 } else if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 0) != 0) {
846 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
847 ret = -ENOSYS;
848 }
849
850 return ret;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800851}
852
853static int start_abr()
854{
855 struct mixer_ctl *ctl_abr_tx_path = NULL;
Zhou Song12c29502019-03-16 10:37:18 +0800856 struct mixer_ctl *ctl_abr_rx_path = NULL;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800857 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
858 int abr_device_id;
859 int ret = 0;
860
861 if (!a2dp.abr_config.is_abr_enabled) {
862 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
863 return -ENOSYS;
864 }
865
866 if (a2dp.abr_config.abr_started) {
867 ALOGI("%s: ABR has already started", __func__);
868 return ret;
869 }
870
871 // Enable Slimbus 7 Tx feedback path
872 ALOGV("%s: Enable ABR Tx feedback path", __func__);
873 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
874 MIXER_ABR_TX_FEEDBACK_PATH);
875 if (!ctl_abr_tx_path) {
876 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
877 return -ENOSYS;
878 }
879 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
880 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
881 return -ENOSYS;
882 }
883
884 // Notify ABR usecase information to BT driver to distinguish
885 // between SCO and feedback usecase
886 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
887 MIXER_SET_FEEDBACK_CHANNEL);
888 if (!ctl_set_bt_feedback_channel) {
889 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800890 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800891 }
892 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
893 ALOGE("%s: Failed to set BT usecase", __func__);
Zhou Song12c29502019-03-16 10:37:18 +0800894 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800895 }
896
897 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800898 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800899 PCM_CAPTURE);
900 if (!a2dp.abr_config.abr_tx_handle) {
901 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
902 abr_device_id, PCM_IN,
903 &pcm_config_abr);
Zhou Song12c29502019-03-16 10:37:18 +0800904 if (a2dp.abr_config.abr_tx_handle == NULL) {
905 ALOGE("%s: Can't open abr tx device", __func__);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800906 goto fail;
Zhou Song12c29502019-03-16 10:37:18 +0800907 }
908 if (!(pcm_is_ready(a2dp.abr_config.abr_tx_handle) &&
909 !pcm_start(a2dp.abr_config.abr_tx_handle))) {
910 ALOGE("%s: tx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
911 goto fail;
912 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800913 }
Zhou Song12c29502019-03-16 10:37:18 +0800914
915 // Enable Slimbus 7 Rx feedback path for HD Voice use case
916 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH) {
917 ALOGV("%s: Enable ABR Rx feedback path", __func__);
918 ctl_abr_rx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
919 MIXER_ABR_RX_FEEDBACK_PATH);
920 if (!ctl_abr_rx_path) {
921 ALOGE("%s: ERROR ABR Rx feedback path mixer control not identifed", __func__);
922 goto fail;
923 }
924 if (mixer_ctl_set_value(ctl_abr_rx_path, 0, 1) != 0) {
925 ALOGE("%s: Failed to set ABR Rx feedback path", __func__);
926 goto fail;
927 }
928
929 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
930 ALOGE("%s: Failed to set BT usecase", __func__);
931 goto fail;
932 }
933
934 // Open hostless front end and prepare ABR Rx path
935 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
936 PCM_PLAYBACK);
937 if (!a2dp.abr_config.abr_rx_handle) {
938 a2dp.abr_config.abr_rx_handle = pcm_open(a2dp.adev->snd_card,
939 abr_device_id, PCM_OUT,
940 &pcm_config_abr);
941 if (a2dp.abr_config.abr_rx_handle == NULL) {
942 ALOGE("%s: Can't open abr rx device", __func__);
943 goto fail;
944 }
945 if (!(pcm_is_ready(a2dp.abr_config.abr_rx_handle) &&
946 !pcm_start(a2dp.abr_config.abr_rx_handle))) {
947 ALOGE("%s: rx: %s", __func__, pcm_get_error(a2dp.abr_config.abr_rx_handle));
948 goto fail;
949 }
950 }
951 }
952
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800953 a2dp.abr_config.abr_started = true;
954
955 return ret;
956
957fail:
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800958 stop_abr();
959 return -ENOSYS;
960}
961
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800962static int check_if_enhanced_fwk() {
963
964 int is_enhanced_fwk = 1;
965 //dlopen lib
966 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
967 if (vndk_fwk_lib_handle != NULL) {
968 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
969 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
970 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
971 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
972 __func__);
973 is_enhanced_fwk = 1;
974 } else {
975 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
976 }
977 }
978 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
979 return is_enhanced_fwk;
980}
981
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530982static void open_a2dp_source() {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530983 int ret = 0;
984
Florian Pfister1a84f312018-07-19 14:38:18 +0200985 ALOGD(" Open A2DP source start ");
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530986
987 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
988 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
989 ALOGD("calling BT stream open");
990 ret = a2dp.audio_source_open();
991 if(ret != 0) {
992 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Srinu Jellada99a592019-01-25 16:50:52 +0530993 }
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530994 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530995 } else {
Naresh Tanniru66cf06c2019-03-20 19:30:37 +0530996 ALOGD("Called a2dp open with improper state %d", a2dp.bt_state_source);
997 }
998 } else {
999 ALOGE("a2dp handle is not identified, Ignoring open request");
1000 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
1001 }
1002}
1003/* API to open BT IPC library to start IPC communication for BT Source*/
1004static void a2dp_source_init()
1005{
1006 ALOGD("a2dp_source_init START");
1007 if (a2dp.bt_lib_source_handle == NULL) {
1008 ALOGD("Requesting for BT lib handle");
1009 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
1010 if (a2dp.bt_lib_source_handle == NULL) {
1011 ALOGE("%s: dlopen failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
1012 return;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301013 }
1014 }
1015
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05301016 a2dp.bt_audio_pre_init = (bt_audio_pre_init_t)
1017 dlsym(a2dp.bt_lib_source_handle, "bt_audio_pre_init");
Srinu Jellada99a592019-01-25 16:50:52 +05301018 a2dp.audio_source_open = (audio_source_open_t)
1019 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
1020 a2dp.audio_source_start = (audio_source_start_t)
1021 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
1022 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
1023 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
1024 a2dp.audio_source_suspend = (audio_source_suspend_t)
1025 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
1026 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
1027 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
1028 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
1029 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
1030 a2dp.audio_source_stop = (audio_source_stop_t)
1031 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
1032 a2dp.audio_source_close = (audio_source_close_t)
1033 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
1034 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
1035 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
1036 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
1037 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
1038 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
1039 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
1040 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
1041 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
1042
Srinu Jella793f5302019-07-19 15:53:55 +05301043 if (a2dp.bt_lib_source_handle && a2dp.bt_audio_pre_init) {
1044 ALOGD("calling BT module preinit");
1045 // fwk related check's will be done in the BT layer
1046 a2dp.bt_audio_pre_init();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001047 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001048}
1049
1050/* API to open BT IPC library to start IPC communication for BT Sink*/
1051static void open_a2dp_sink()
1052{
1053 ALOGD(" Open A2DP input start ");
1054 if (a2dp.bt_lib_sink_handle == NULL){
1055 ALOGD(" Requesting for BT lib handle");
1056 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
1057
1058 if (a2dp.bt_lib_sink_handle == NULL) {
1059 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
1060 } else {
1061 a2dp.audio_sink_start = (audio_sink_start_t)
1062 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
1063 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
1064 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
1065 a2dp.audio_sink_stop = (audio_sink_stop_t)
1066 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
1067 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
1068 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
1069 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
1070 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
1071 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301072 }
1073}
1074
1075static int close_a2dp_output()
1076{
1077 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +02001078
1079 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
1080 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301081 return -ENOSYS;
1082 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001083
1084 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
1085 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001086 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001087 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301088 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001089 a2dp.a2dp_source_started = false;
1090 a2dp.a2dp_source_total_active_session_requests = 0;
1091 a2dp.a2dp_source_suspended = false;
1092 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301093 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301094 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +02001095 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001096 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
1097 stop_abr();
1098 a2dp.abr_config.is_abr_enabled = false;
1099 a2dp.abr_config.abr_started = false;
1100 a2dp.abr_config.imc_instance = 0;
1101 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001102 a2dp.abr_config.abr_rx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +05301103 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +02001104
1105 return 0;
1106}
1107
1108static int close_a2dp_input()
1109{
1110 ALOGV("%s\n",__func__);
1111
1112 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
1113 ALOGE("a2dp sink handle is not identified, Ignoring close request");
1114 return -ENOSYS;
1115 }
1116
1117 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
1118 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001119 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +02001120 ALOGE("failed close a2dp sink control path from BT library");
1121 }
1122 a2dp.a2dp_sink_started = false;
1123 a2dp.a2dp_sink_total_active_session_requests = 0;
1124 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1125 a2dp.dec_sampling_rate = 48000;
1126 a2dp.dec_channels = 2;
1127 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301128
1129 return 0;
1130}
1131
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301132static void a2dp_check_and_set_scrambler()
1133{
1134 bool scrambler_mode = false;
1135 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001136 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1137 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301138
1139 if (scrambler_mode) {
1140 //enable scrambler in dsp
1141 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1142 MIXER_SCRAMBLER_MODE);
1143 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001144 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301145 return;
1146 } else {
1147 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1148 ALOGE("%s: Could not set scrambler mode", __func__);
1149 return;
1150 }
1151 }
1152 }
1153}
1154
Florian Pfister1a84f312018-07-19 14:38:18 +02001155static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301156{
Florian Pfister1a84f312018-07-19 14:38:18 +02001157 char *rate_str = NULL, *channels = NULL;
1158 uint32_t sampling_rate;
1159 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1160 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301161
Florian Pfister1a84f312018-07-19 14:38:18 +02001162 if (direction == SINK) {
1163 sampling_rate = a2dp.dec_sampling_rate;
1164 } else {
1165 sampling_rate = a2dp.enc_sampling_rate;
1166 }
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001167 /*
1168 * For LDAC encoder and AAC decoder open slimbus port at
1169 * 96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1170 * For APTX AD encoder, open slimbus port at 96Khz for 48Khz input.
1171 */
Florian Pfister1a84f312018-07-19 14:38:18 +02001172 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301173 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Zhou Song7d7ca8b2019-05-23 22:38:25 +08001174 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC) ||
1175 (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301176 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1177 sampling_rate = sampling_rate *2;
1178 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001179
Aalique Grahame22e49102018-12-18 14:23:57 -08001180 // No need to configure backend for PCM format.
1181 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1182 return 0;
1183 }
1184
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301185 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301186 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301187 case 44100:
1188 rate_str = "KHZ_44P1";
1189 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301190 case 88200:
1191 rate_str = "KHZ_88P2";
1192 break;
1193 case 96000:
1194 rate_str = "KHZ_96";
1195 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001196 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301197 default:
1198 rate_str = "KHZ_48";
1199 break;
1200 }
1201
Florian Pfister1a84f312018-07-19 14:38:18 +02001202 if (direction == SINK) {
1203 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1204 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301205 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001206 } else {
1207 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1208 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301209 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001210 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301211 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001212
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301213 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1214 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301215 is_configured = false;
1216 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301217 }
1218
Surendar Karka2febd452018-12-13 17:56:43 +05301219 if (direction == SOURCE) {
1220 /* Set Tx backend sample rate */
Zhou Song12c29502019-03-16 10:37:18 +08001221 if (a2dp.abr_config.is_abr_enabled) {
1222 if (a2dp.bt_encoder_format == CODEC_TYPE_APTX_AD_SPEECH)
1223 rate_str = SPEECH_TX_SAMPLE_RATE;
1224 else
1225 rate_str = ABR_TX_SAMPLE_RATE;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301226
Preetam Singh Ranawat225e13c2019-05-16 17:31:02 +05301227 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1228 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1229 MIXER_SOURCE_SAMPLE_RATE_TX);
1230 if (!ctl_sample_rate) {
1231 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1232 is_configured = false;
1233 goto fail;
1234 }
1235
1236 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1237 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1238 is_configured = false;
1239 goto fail;
1240 }
Surendar Karka2febd452018-12-13 17:56:43 +05301241 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301242 }
1243 } else {
1244 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1245 MIXER_SAMPLE_RATE_TX is not supported */
1246 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1247 MIXER_SAMPLE_RATE_DEFAULT);
1248 if (!ctl_sample_rate) {
1249 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301250 is_configured = false;
1251 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301252 }
1253
1254 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1255 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001256 is_configured = false;
1257 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301258 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301259 }
1260
Florian Pfister1a84f312018-07-19 14:38:18 +02001261 if (direction == SINK) {
1262 switch (a2dp.dec_channels) {
1263 case 1:
1264 channels = "One";
1265 break;
1266 case 2:
1267 default:
1268 channels = "Two";
1269 break;
1270 }
1271
Ramu Gottipati02809682018-12-19 16:46:12 +05301272 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001273 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1274 MIXER_AFE_SINK_CHANNELS);
1275 } else {
1276 //Configure AFE enc channels
1277 switch (a2dp.enc_channels) {
1278 case 1:
1279 channels = "One";
1280 break;
1281 case 2:
1282 default:
1283 channels = "Two";
1284 break;
1285 }
1286
Ramu Gottipati02809682018-12-19 16:46:12 +05301287 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001288 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1289 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301290 }
1291
Florian Pfister1a84f312018-07-19 14:38:18 +02001292 if (!ctrl_channels) {
1293 ALOGE(" ERROR AFE channels mixer control not identified");
1294 } else {
1295 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301296 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001297 is_configured = false;
1298 goto fail;
1299 }
1300 }
1301 is_configured = true;
1302fail:
1303 return is_configured;
1304}
1305
Surendar Karkabbb3c822018-11-12 13:00:38 +05301306bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001307{
1308 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1309 struct aac_dec_cfg_t aac_dsp_cfg;
1310 bool is_configured = false;
1311 int ret = 0;
1312
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001313 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001314 return false;
1315
Surendar Karka2febd452018-12-13 17:56:43 +05301316 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001317 if (!ctl_dec_data) {
1318 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1319 is_configured = false;
1320 goto fail;
1321 }
1322
1323 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1324 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1325 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1326 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1327 switch(aac_bt_cfg->audio_object_type) {
1328 case 0:
1329 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301330 break;
1331 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001332 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1333 break;
1334 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301335 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001336 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301337 break;
1338 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001339 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1340 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1341 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1342 sizeof(struct aac_dec_cfg_t));
1343 if (ret != 0) {
1344 ALOGE("%s: failed to set AAC decoder config", __func__);
1345 is_configured = false;
1346 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001347 }
1348
Florian Pfister1a84f312018-07-19 14:38:18 +02001349 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1350 MIXER_DEC_BIT_FORMAT);
1351 if (!ctrl_bit_format) {
1352 ALOGE(" ERROR Dec bit format mixer control not identified");
1353 is_configured = false;
1354 goto fail;
1355 }
1356 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1357 if (ret != 0) {
1358 ALOGE("%s: Failed to set bit format to decoder", __func__);
1359 is_configured = false;
1360 goto fail;
1361 }
1362
1363 is_configured = true;
1364 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1365 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1366 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1367 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1368 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1369fail:
1370 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301371}
1372
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301373static int a2dp_set_bit_format(uint32_t enc_bit_format)
1374{
1375 const char *bit_format = NULL;
1376 struct mixer_ctl *ctrl_bit_format = NULL;
1377
1378 // Configure AFE Input Bit Format
1379 switch (enc_bit_format) {
1380 case 32:
1381 bit_format = "S32_LE";
1382 break;
1383 case 24:
1384 bit_format = "S24_LE";
1385 break;
1386 case 16:
1387 default:
1388 bit_format = "S16_LE";
1389 break;
1390 }
1391
1392 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1393 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1394 MIXER_ENC_BIT_FORMAT);
1395 if (!ctrl_bit_format) {
1396 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1397 return -ENOSYS;
1398 }
1399 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1400 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1401 return -ENOSYS;
1402 }
1403 return 0;
1404}
1405
Surendar Karka2febd452018-12-13 17:56:43 +05301406static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301407{
Surendar Karka2febd452018-12-13 17:56:43 +05301408 const char *rate_str = "KHZ_8", *channels = "Zero";
1409 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1410 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301411
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001412 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301413 if (direction == SINK) {
1414 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1415 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1416 MIXER_SINK_SAMPLE_RATE);
1417 } else {
1418 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1419 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1420 MIXER_SAMPLE_RATE_RX);
1421 }
1422 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301423
Surendar Karka2febd452018-12-13 17:56:43 +05301424 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1425 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301426 return -ENOSYS;
1427 }
Zhou Song12c29502019-03-16 10:37:18 +08001428 if (a2dp.abr_config.is_abr_enabled) {
1429 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
1430 MIXER_SOURCE_SAMPLE_RATE_TX);
1431 if (!ctl_sample_rate_tx) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301432 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1433 return -ENOSYS;
Zhou Song12c29502019-03-16 10:37:18 +08001434 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301435
Zhou Song12c29502019-03-16 10:37:18 +08001436 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1437 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1438 return -ENOSYS;
1439 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301440 }
1441 } else {
1442
Surendar Karka2febd452018-12-13 17:56:43 +05301443 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301444 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301445 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301446 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1447 return -ENOSYS;
1448 }
1449
Surendar Karka2febd452018-12-13 17:56:43 +05301450 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301451 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1452 return -ENOSYS;
1453 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001454 }
1455
1456 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301457 if (direction == SINK) {
1458 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1459 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1460 MIXER_AFE_SINK_CHANNELS);
1461 } else {
1462 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1463 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1464 MIXER_AFE_IN_CHANNELS);
1465 }
1466 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001467 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1468 return -ENOSYS;
1469 }
Surendar Karka2febd452018-12-13 17:56:43 +05301470 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001471 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1472 return -ENOSYS;
1473 }
1474
1475 return 0;
1476}
1477
1478/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301479static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001480{
1481 struct mixer_ctl *ctl_dec_data = NULL;
1482 struct abr_dec_cfg_t dec_cfg;
1483 int ret = 0;
1484
1485 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301486 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001487 if (!ctl_dec_data) {
1488 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1489 return false;
1490 }
1491 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1492 dec_cfg.dec_format = dec_format;
1493 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1494 dec_cfg.imc_info.enable = IMC_ENABLE;
1495 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1496 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1497
1498 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1499 sizeof(dec_cfg));
1500 if (ret != 0) {
1501 ALOGE("%s: Failed to set decoder config", __func__);
1502 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301503 }
Surendar Karka2febd452018-12-13 17:56:43 +05301504 }
1505
1506 return true;
1507}
1508
Surendar Karkabbb3c822018-11-12 13:00:38 +05301509bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001510{
1511 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1512 struct sbc_dec_cfg_t sbc_dsp_cfg;
1513 bool is_configured = false;
1514 int ret = 0;
1515
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001516 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001517 goto fail;
1518
Surendar Karka2febd452018-12-13 17:56:43 +05301519 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001520 if (!ctl_dec_data) {
1521 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1522 is_configured = false;
1523 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301524 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001525
Florian Pfister1a84f312018-07-19 14:38:18 +02001526 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1527 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1528 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1529 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1530 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1531 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301532
Florian Pfister1a84f312018-07-19 14:38:18 +02001533 if (ret != 0) {
1534 ALOGE("%s: failed to set SBC decoder config", __func__);
1535 is_configured = false;
1536 goto fail;
1537 }
1538
1539 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1540 MIXER_DEC_BIT_FORMAT);
1541 if (!ctrl_bit_format) {
1542 ALOGE(" ERROR Dec bit format mixer control not identified");
1543 is_configured = false;
1544 goto fail;
1545 }
1546 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1547 if (ret != 0) {
1548 ALOGE("%s: Failed to set bit format to decoder", __func__);
1549 is_configured = false;
1550 goto fail;
1551 }
1552
1553 is_configured = true;
1554 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1555 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1556 a2dp.dec_channels = 1;
1557 else
1558 a2dp.dec_channels = 2;
1559 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1560 ALOGV("Successfully updated SBC dec format");
1561fail:
1562 return is_configured;
1563}
1564
Florian Pfister1a84f312018-07-19 14:38:18 +02001565/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301566static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301567{
Florian Pfister1a84f312018-07-19 14:38:18 +02001568 void *codec_info = NULL;
1569 codec_t codec_type = CODEC_TYPE_INVALID;
1570 bool is_configured = false;
1571 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301572
Florian Pfister1a84f312018-07-19 14:38:18 +02001573 if (!a2dp.audio_get_dec_config) {
1574 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1575 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301576 }
1577
Surendar Karka2febd452018-12-13 17:56:43 +05301578 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001579 if (!ctl_dec_data) {
1580 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1581 is_configured = false;
1582 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301583 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001584 codec_info = a2dp.audio_get_dec_config(&codec_type);
1585 switch(codec_type) {
1586 case CODEC_TYPE_SBC:
1587 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301588 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001589 break;
1590 case CODEC_TYPE_AAC:
1591 ALOGD(" AAC decoder supported BT device");
1592 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301593 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001594 break;
1595 default:
1596 ALOGD(" Received Unsupported decoder format");
1597 is_configured = false;
1598 break;
1599 }
1600 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301601}
1602
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301603/* API to configure SBC DSP encoder */
1604bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1605{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301606 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301607 struct sbc_enc_cfg_t sbc_dsp_cfg;
1608 bool is_configured = false;
1609 int ret = 0;
1610
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001611 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301612 return false;
1613
1614 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1615 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001616 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301617 is_configured = false;
1618 goto fail;
1619 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301620 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001621 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301622 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1623 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1624 switch(sbc_bt_cfg->channels) {
1625 case 0:
1626 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1627 break;
1628 case 1:
1629 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1630 break;
1631 case 3:
1632 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1633 break;
1634 case 2:
1635 default:
1636 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1637 break;
1638 }
1639 if (sbc_bt_cfg->alloc)
1640 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1641 else
1642 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1643 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1644 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1645 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1646 sizeof(struct sbc_enc_cfg_t));
1647 if (ret != 0) {
1648 ALOGE("%s: failed to set SBC encoder config", __func__);
1649 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301650 goto fail;
1651 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301652 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301653 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301654 is_configured = false;
1655 goto fail;
1656 }
1657 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001658 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301659 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301660
1661 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1662 a2dp.enc_channels = 1;
1663 else
1664 a2dp.enc_channels = 2;
1665
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301666 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1667 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301668fail:
1669 return is_configured;
1670}
1671
Manish Dewangan6a252632017-12-04 17:27:44 +05301672#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301673static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1674 audio_aptx_encoder_config *aptx_bt_cfg)
1675{
1676 int ret = 0;
1677
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001678 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301679 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1680 aptx_dsp_cfg, aptx_bt_cfg);
1681 return -EINVAL;
1682 }
1683
1684 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301685 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301686
1687
1688 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1689 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1690 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1691 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1692 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1693 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1694 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1695 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1696 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301697 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301698 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1699 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1700 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1701 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1702
1703
1704 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1705 case APTX_AD_CHANNEL_UNCHANGED:
1706 case APTX_AD_CHANNEL_JOINT_STEREO:
1707 case APTX_AD_CHANNEL_DUAL_MONO:
1708 case APTX_AD_CHANNEL_STEREO_TWS:
1709 case APTX_AD_CHANNEL_EARBUD:
1710 default:
1711 a2dp.enc_channels = CH_STEREO;
1712 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1713 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1714 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1715 break;
1716 case APTX_AD_CHANNEL_MONO:
1717 a2dp.enc_channels = CH_MONO;
1718 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1719 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001720 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301721 }
1722 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1723 case APTX_AD_SR_UNCHANGED:
1724 case APTX_AD_48:
1725 default:
1726 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1727 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1728 break;
1729 case APTX_AD_44_1:
1730 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1731 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001732 break;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301733 }
1734 ALOGV("Successfully updated APTX AD enc format with \
1735 samplingrate: %d channels:%d",
1736 aptx_dsp_cfg->custom_cfg.sample_rate,
1737 aptx_dsp_cfg->custom_cfg.num_channels);
1738
1739 return ret;
1740}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301741
Zhou Song8fccbb62019-03-20 01:08:19 +08001742static int update_aptx_ad_dsp_config_r2(struct aptx_ad_enc_cfg_r2_t *aptx_dsp_cfg,
1743 audio_aptx_encoder_config *aptx_bt_cfg)
1744{
1745 int ret = 0;
1746
1747 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Mingshu Pangaa429f72019-06-10 17:25:00 +08001748 ALOGE("Invalid param, aptx_dsp_cfg %pK aptx_bt_cfg %pK",
Zhou Song8fccbb62019-03-20 01:08:19 +08001749 aptx_dsp_cfg, aptx_bt_cfg);
1750 return -EINVAL;
1751 }
1752
1753 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_r2_t));
1754 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
1755
1756
1757 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1758 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1759 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1760 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1761 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1762 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1763 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1764 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1765 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
1766 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
1767 aptx_dsp_cfg->aptx_ad_cfg.input_mode = aptx_bt_cfg->ad_cfg->input_mode;
1768 aptx_dsp_cfg->aptx_ad_cfg.fade_duration = aptx_bt_cfg->ad_cfg->fade_duration;
1769 for (int i = 0; i < sizeof(aptx_dsp_cfg->aptx_ad_cfg.sink_cap); i ++)
1770 aptx_dsp_cfg->aptx_ad_cfg.sink_cap[i] = aptx_bt_cfg->ad_cfg->sink_cap[i];
1771 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1772 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1773 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1774 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1775
1776
1777 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1778 case APTX_AD_CHANNEL_UNCHANGED:
1779 case APTX_AD_CHANNEL_JOINT_STEREO:
1780 case APTX_AD_CHANNEL_DUAL_MONO:
1781 case APTX_AD_CHANNEL_STEREO_TWS:
1782 case APTX_AD_CHANNEL_EARBUD:
1783 default:
1784 a2dp.enc_channels = CH_STEREO;
1785 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1786 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1787 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1788 break;
1789 case APTX_AD_CHANNEL_MONO:
1790 a2dp.enc_channels = CH_MONO;
1791 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1792 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
Zhou Song30e8cea2019-10-22 23:39:25 +08001793 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001794 }
1795 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1796 case APTX_AD_SR_UNCHANGED:
1797 case APTX_AD_48:
1798 default:
1799 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1800 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1801 break;
1802 case APTX_AD_44_1:
1803 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1804 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
Zhou Song30e8cea2019-10-22 23:39:25 +08001805 break;
1806 case APTX_AD_96:
1807 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
1808 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_96K;
1809 break;
Zhou Song8fccbb62019-03-20 01:08:19 +08001810 }
1811 ALOGV("Successfully updated APTX AD enc format with \
1812 samplingrate: %d channels:%d",
1813 aptx_dsp_cfg->custom_cfg.sample_rate,
1814 aptx_dsp_cfg->custom_cfg.num_channels);
1815
1816 return ret;
1817}
1818
Manisha Agarwala51768b2018-11-01 16:30:52 +05301819static void audio_a2dp_update_tws_channel_mode()
1820{
1821 char* channel_mode;
1822 struct mixer_ctl *ctl_channel_mode;
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301823
1824 ALOGD("Update tws for mono_mode on=%d",a2dp.is_tws_mono_mode_on);
1825
Manisha Agarwala51768b2018-11-01 16:30:52 +05301826 if (a2dp.is_tws_mono_mode_on)
1827 channel_mode = "One";
1828 else
1829 channel_mode = "Two";
Manisha Agarwal5bb881e2019-09-20 14:03:57 +05301830
Manisha Agarwala51768b2018-11-01 16:30:52 +05301831 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1832 if (!ctl_channel_mode) {
1833 ALOGE("failed to get tws mixer ctl");
1834 return;
1835 }
1836 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1837 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1838 return;
1839 }
1840}
1841
Manish Dewangan6a252632017-12-04 17:27:44 +05301842static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
1843 audio_aptx_encoder_config *aptx_bt_cfg)
1844{
1845 int ret = 0;
1846
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001847 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301848 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1849 aptx_dsp_cfg, aptx_bt_cfg);
1850 return -EINVAL;
1851 }
1852
1853 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001854 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301855
1856 if (!a2dp.is_aptx_dual_mono_supported) {
1857 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1858 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1859 } else {
1860 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1861 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1862 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1863 }
1864
1865 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
1866 case 1:
1867 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1868 break;
1869 case 2:
1870 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05301871 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1872 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
Manish Dewangan6a252632017-12-04 17:27:44 +05301873 break;
1874 }
1875 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
1876 if (!a2dp.is_aptx_dual_mono_supported) {
1877 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1878 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
1879 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
1880 aptx_dsp_cfg->custom_cfg.num_channels);
1881 } else {
1882 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1883 ALOGV("Successfully updated APTX dual mono enc format with \
1884 samplingrate: %d channels:%d syncmode %d",
1885 aptx_dsp_cfg->custom_cfg.sample_rate,
1886 aptx_dsp_cfg->custom_cfg.num_channels,
1887 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
1888 }
1889 return ret;
1890}
1891#else
1892static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
1893 audio_aptx_encoder_config *aptx_bt_cfg)
1894{
1895 int ret = 0;
1896
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001897 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301898 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1899 aptx_dsp_cfg, aptx_bt_cfg);
1900 return -EINVAL;
1901 }
1902
1903 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001904 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301905 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
1906 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
1907 switch(aptx_dsp_cfg->num_channels) {
1908 case 1:
1909 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
1910 break;
1911 case 2:
1912 default:
1913 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
1914 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
1915 break;
1916 }
1917
1918 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
1919 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
1920
1921 return ret;
1922}
1923#endif
1924
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301925/* API to configure APTX DSP encoder */
1926bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1927{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301928 struct mixer_ctl *ctl_enc_data = NULL;
Zhou Song8fccbb62019-03-20 01:08:19 +08001929 struct mixer_ctl *aptx_ad_ctl = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08001930 int mixer_size = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301931 bool is_configured = false;
1932 int ret = 0;
Mingshu Pangaa429f72019-06-10 17:25:00 +08001933 int sample_rate_backup = SAMPLING_RATE_48K;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301934
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001935 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301936 return false;
1937
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001938 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1939 if (!ctl_enc_data) {
1940 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1941 return false;
1942 }
1943
Manish Dewangan6a252632017-12-04 17:27:44 +05301944#ifndef LINUX_ENABLED
1945 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301946 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Zhou Song8fccbb62019-03-20 01:08:19 +08001947 struct aptx_ad_enc_cfg_r2_t aptx_ad_dsp_cfg_r2;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001948 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001949 aptx_ad_ctl = mixer_get_ctl_by_name(a2dp.adev->mixer,
1950 MIXER_ENC_APTX_AD_CONFIG_BLOCK);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001951 if (aptx_ad_ctl)
Zhou Song8fccbb62019-03-20 01:08:19 +08001952 ret = update_aptx_ad_dsp_config_r2(&aptx_ad_dsp_cfg_r2, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001953 else
Zhou Song8fccbb62019-03-20 01:08:19 +08001954 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001955 } else
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001956 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
Mingshu Pangaa429f72019-06-10 17:25:00 +08001957
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001958 if (ret) {
1959 is_configured = false;
1960 goto fail;
1961 }
1962
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001963 if (a2dp.is_aptx_adaptive) {
Zhou Song8fccbb62019-03-20 01:08:19 +08001964 if (aptx_ad_ctl)
1965 ret = mixer_ctl_set_array(aptx_ad_ctl, (void *)&aptx_ad_dsp_cfg_r2,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001966 sizeof(struct aptx_ad_enc_cfg_r2_t));
Zhou Song8fccbb62019-03-20 01:08:19 +08001967 else
1968 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001969 sizeof(struct aptx_ad_enc_cfg_t));
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001970 } else {
1971 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Mingshu Pangaa429f72019-06-10 17:25:00 +08001972 sizeof(struct aptx_enc_cfg_t));
Sharad Sangle95d451b2018-06-19 12:24:20 +05301973 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301974#else
1975 struct custom_enc_cfg_t aptx_dsp_cfg;
1976 mixer_size = sizeof(struct custom_enc_cfg_t);
1977 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05301978 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05301979 if (ret) {
1980 is_configured = false;
1981 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301982 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001983 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1984 mixer_size);
1985#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301986 if (ret != 0) {
1987 ALOGE("%s: Failed to set APTX encoder config", __func__);
1988 is_configured = false;
1989 goto fail;
1990 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301991#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001992 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301993 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001994 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301995 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
1996 else
1997 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301998#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301999 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302000 is_configured = false;
2001 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302002 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302003 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302004 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05302005 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302006 else
Surendar Karka2febd452018-12-13 17:56:43 +05302007 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302008fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05302009 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002010 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05302011 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302012 return is_configured;
2013}
2014
2015/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302016 */
Manish Dewangan6a252632017-12-04 17:27:44 +05302017#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002018bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05302019#else
2020bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
2021#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302022{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302023 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302024 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302025 bool is_configured = false;
2026 int ret = 0;
2027
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002028 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302029 return false;
2030
2031 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2032 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002033 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302034 is_configured = false;
2035 goto fail;
2036 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302037
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302038 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002039 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302040 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
2041 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
2042 switch(aptx_dsp_cfg.num_channels) {
2043 case 1:
2044 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2045 break;
2046 case 2:
2047 default:
2048 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2049 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2050 break;
2051 }
2052 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302053 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302054 if (ret != 0) {
2055 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
2056 is_configured = false;
2057 goto fail;
2058 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302059 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302060 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302061 is_configured = false;
2062 goto fail;
2063 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302064 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002065 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302066 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302067 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302068 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
2069 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302070fail:
2071 return is_configured;
2072}
2073
2074/* API to configure AAC DSP encoder */
2075bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
2076{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302077 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302078 struct aac_enc_cfg_t aac_dsp_cfg;
2079 bool is_configured = false;
2080 int ret = 0;
2081
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002082 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302083 return false;
2084
2085 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2086 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002087 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302088 is_configured = false;
2089 goto fail;
2090 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302091 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002092 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302093 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302094 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002095 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302096 case 0:
2097 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2098 break;
2099 case 2:
2100 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2101 break;
2102 case 1:
2103 default:
2104 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2105 break;
2106 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05302107 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302108 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002109
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302110 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2111 sizeof(struct aac_enc_cfg_t));
2112 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002113 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302114 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302115 goto fail;
2116 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302117 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302118 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302119 is_configured = false;
2120 goto fail;
2121 }
2122 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002123 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302124 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002125 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002126 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2127 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
2128fail:
2129 return is_configured;
2130}
2131
2132bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
2133{
2134 struct mixer_ctl *ctl_enc_data = NULL;
2135 struct aac_enc_cfg_v2_t aac_dsp_cfg;
2136 bool is_configured = false;
2137 int ret = 0;
2138
2139 if (aac_bt_cfg == NULL)
2140 return false;
2141
2142 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2143 if (!ctl_enc_data) {
2144 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2145 is_configured = false;
2146 goto fail;
2147 }
2148 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302149 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002150 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2151 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2152 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2153 case 0:
2154 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2155 break;
2156 case 2:
2157 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2158 break;
2159 case 1:
2160 default:
2161 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2162 break;
2163 }
2164 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2165 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2166 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2167 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2168
2169 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2170 sizeof(struct aac_enc_cfg_v2_t));
2171 if (ret != 0) {
2172 ALOGE("%s: Failed to set AAC encoder config", __func__);
2173 is_configured = false;
2174 goto fail;
2175 }
2176 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2177 if (ret != 0) {
2178 is_configured = false;
2179 goto fail;
2180 }
2181 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05302182 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002183 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2184 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2185 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2186 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302187fail:
2188 return is_configured;
2189}
2190
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302191bool configure_aac_enc_format_v3(audio_aac_encoder_config_v3 *aac_bt_cfg)
2192{
2193 struct mixer_ctl *ctl_enc_data = NULL;
2194 struct aac_enc_cfg_v3_t aac_dsp_cfg;
2195 struct aac_frame_size_control_t* frame_vbr_ctl = NULL;
2196 bool is_configured = false;
2197 int ret = 0;
2198
2199 if (aac_bt_cfg == NULL)
2200 return false;
2201
2202 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2203 if (!ctl_enc_data) {
2204 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
2205 is_configured = false;
2206 goto fail;
2207 }
2208 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v3_t));
2209 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
2210 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
2211 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2212 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
2213 case 0:
2214 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
2215 break;
2216 case 2:
2217 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
2218 break;
2219 case 1:
2220 default:
2221 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
2222 break;
2223 }
2224 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
2225 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
2226 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
2227 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
2228 frame_vbr_ctl = aac_bt_cfg->frame_ptr_ctl;
2229
2230 if (frame_vbr_ctl != NULL) {
2231 aac_dsp_cfg.aac_key_value_ctl.ctl_type = frame_vbr_ctl->ctl_type;
2232 aac_dsp_cfg.aac_key_value_ctl.ctl_value = frame_vbr_ctl->ctl_value;
2233 } else {
2234 ALOGE("%s: VBR cannot be enabled, fall back to default",__func__);
2235 aac_dsp_cfg.aac_key_value_ctl.ctl_type = 0;
2236 aac_dsp_cfg.aac_key_value_ctl.ctl_value = 0;
2237 }
2238
2239 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
2240 sizeof(struct aac_enc_cfg_v3_t));
2241 if (ret != 0) {
2242 ALOGE("%s: Failed to set AAC encoder config", __func__);
2243 is_configured = false;
2244 goto fail;
2245 }
2246 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
2247 if (ret != 0) {
2248 is_configured = false;
2249 goto fail;
2250 }
2251 is_configured = true;
2252 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
2253 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
2254 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
2255 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
2256 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
2257fail:
2258 return is_configured;
2259}
2260
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302261bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
2262{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302263 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302264 struct celt_enc_cfg_t celt_dsp_cfg;
2265 bool is_configured = false;
2266 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002267 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302268 return false;
2269
2270 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2271 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002272 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302273 is_configured = false;
2274 goto fail;
2275 }
2276 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
2277
Florian Pfister1a84f312018-07-19 14:38:18 +02002278 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302279 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
2280 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
2281 switch(celt_dsp_cfg.custom_cfg.num_channels) {
2282 case 1:
2283 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2284 break;
2285 case 2:
2286 default:
2287 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2288 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2289 break;
2290 }
2291
2292 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2293
2294 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2295 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2296 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2297 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2298 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2299
2300 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2301 sizeof(struct celt_enc_cfg_t));
2302 if (ret != 0) {
2303 ALOGE("%s: Failed to set CELT encoder config", __func__);
2304 is_configured = false;
2305 goto fail;
2306 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302307 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302308 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302309 is_configured = false;
2310 goto fail;
2311 }
2312 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002313 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302314 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302315 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302316 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2317 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2318fail:
2319 return is_configured;
2320}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302321
2322bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2323{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302324 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302325 struct ldac_enc_cfg_t ldac_dsp_cfg;
2326 bool is_configured = false;
2327 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002328 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302329 return false;
2330
2331 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2332 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002333 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302334 is_configured = false;
2335 goto fail;
2336 }
2337 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2338
Florian Pfister1a84f312018-07-19 14:38:18 +02002339 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302340 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2341 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2342 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2343 case 4:
2344 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2345 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2346 break;
2347 case 2:
2348 case 1:
2349 default:
2350 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2351 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2352 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2353 break;
2354 }
2355
2356 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2357 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2358 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002359 if (ldac_bt_cfg->is_abr_enabled) {
2360 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2361 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2362 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2363 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2364 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002365 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002366 }
2367
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302368 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2369 sizeof(struct ldac_enc_cfg_t));
2370 if (ret != 0) {
2371 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2372 is_configured = false;
2373 goto fail;
2374 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302375 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302376 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302377 is_configured = false;
2378 goto fail;
2379 }
2380 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002381 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302382 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2383 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002384 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302385 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2386 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2387fail:
2388 return is_configured;
2389}
2390
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302391bool configure_a2dp_encoder_format()
2392{
2393 void *codec_info = NULL;
2394 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002395 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302396 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002397 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302398
Florian Pfister1a84f312018-07-19 14:38:18 +02002399 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302400 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2401 return false;
2402 }
2403 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002404 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302405 &codec_type);
2406
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002407 // ABR disabled by default for all codecs
2408 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302409 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002410
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302411 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002412 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302413 ALOGD(" Received SBC encoder supported BT device");
2414 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002415 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302416 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002417 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302418 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302419#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002420 a2dp.is_aptx_dual_mono_supported = false;
2421 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302422#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302423 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002424 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302425 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002426 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302427 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302428#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302429 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002430 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302431#else
2432 is_configured =
2433 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2434#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002435 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302436#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002437 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002438 ALOGD(" Received APTX dual mono encoder supported BT device");
2439 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302440 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2441 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002442 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2443 is_configured =
2444 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302445 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302446#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002447 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302448 ALOGD(" Received AAC encoder supported BT device");
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302449 bool is_aac_vbr_enabled =
2450 property_get_bool("persist.vendor.bt.aac_vbr_frm_ctl.enabled", false);
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002451 bool is_aac_frame_ctl_enabled =
2452 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
Manisha Agarwalabb536a2020-01-30 16:12:44 +05302453 if (is_aac_vbr_enabled)
2454 is_configured = configure_aac_enc_format_v3((audio_aac_encoder_config_v3 *) codec_info);
2455 else
2456 is_configured = is_aac_frame_ctl_enabled ?
2457 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2458 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302459 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002460 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302461 ALOGD(" Received CELT encoder supported BT device");
2462 is_configured =
2463 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2464 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002465 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302466 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002467 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2468 instance_id = MAX_INSTANCE_ID;
2469 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302470 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002471 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302472 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302473 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302474#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302475 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302476 ALOGD(" Received APTX AD encoder supported BT device");
2477 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2478 instance_id = MAX_INSTANCE_ID;
2479 a2dp.abr_config.imc_instance = instance_id--;
2480 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2481 a2dp.is_aptx_adaptive = true;
2482 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2483 is_configured =
2484 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302485 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302486 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302487#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002488 case CODEC_TYPE_PCM:
2489 ALOGD("Received PCM format for BT device");
2490 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2491 is_configured = true;
2492 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302493 default:
2494 ALOGD(" Received Unsupported encoder formar");
2495 is_configured = false;
2496 break;
2497 }
2498 return is_configured;
2499}
2500
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002501int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302502{
2503 int ret = 0;
2504
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002505 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302506
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002507 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002508 && a2dp.audio_get_enc_config)) {
2509 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302510 return -ENOSYS;
2511 }
2512
Zhou Song12c29502019-03-16 10:37:18 +08002513 if (a2dp.a2dp_source_suspended || a2dp.swb_configured) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302514 //session will be restarted after suspend completion
2515 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302516 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302517 }
2518
Florian Pfister1a84f312018-07-19 14:38:18 +02002519 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302520 ALOGD("calling BT module stream start");
2521 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002522 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302523 ALOGE("BT controller start return = %d",ret);
2524 if (ret != 0 ) {
2525 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002526 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302527 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002528 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002529 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302530 ret = 0;
2531 ALOGD("Start playback successful to BT library");
2532 } else {
2533 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002534 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302535 ret = -ETIMEDOUT;
2536 }
2537 }
2538 }
2539
Florian Pfister1a84f312018-07-19 14:38:18 +02002540 if (a2dp.a2dp_source_started) {
2541 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302542 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302543 audio_a2dp_update_tws_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002544 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002545 if (a2dp.abr_config.is_abr_enabled)
2546 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302547 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302548
2549 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002550 a2dp.a2dp_source_total_active_session_requests);
2551 return ret;
2552}
2553
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002554uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002555{
2556 uint32_t latency = 0;
2557
2558 switch(a2dp.bt_decoder_format) {
2559 case CODEC_TYPE_SBC:
2560 latency = DEFAULT_SINK_LATENCY_SBC;
2561 break;
2562 case CODEC_TYPE_AAC:
2563 latency = DEFAULT_SINK_LATENCY_AAC;
2564 break;
2565 default:
2566 latency = 200;
2567 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2568 break;
2569 }
2570 return (uint64_t)latency;
2571}
2572
2573bool a2dp_send_sink_setup_complete(void) {
2574 uint64_t system_latency = 0;
2575 bool is_complete = false;
2576
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002577 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002578
2579 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2580 is_complete = true;
2581 }
2582 return is_complete;
2583}
2584
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002585bool a2dp_sink_is_ready()
2586{
2587 bool ret = false;
2588
2589 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2590 (a2dp.is_a2dp_offload_supported) &&
2591 (a2dp.audio_sink_check_a2dp_ready))
2592 ret = a2dp.audio_sink_check_a2dp_ready();
2593 return ret;
2594}
2595
2596int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002597{
2598 int ret = 0;
2599
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002600 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002601
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002602 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002603 && a2dp.audio_get_dec_config)) {
2604 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2605 return -ENOSYS;
2606 }
2607
2608 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2609 ALOGD("calling BT module stream start");
2610 /* This call indicates BT IPC lib to start capture */
2611 ret = a2dp.audio_sink_start();
2612 ALOGE("BT controller start capture return = %d",ret);
2613 if (ret != 0 ) {
2614 ALOGE("BT controller start capture failed");
2615 a2dp.a2dp_sink_started = false;
2616 } else {
2617
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002618 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002619 ALOGD("Wait for capture ready not successful");
2620 ret = -ETIMEDOUT;
2621 }
2622
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002623 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002624 a2dp.a2dp_sink_started = true;
2625 ret = 0;
2626 ALOGD("Start capture successful to BT library");
2627 } else {
2628 ALOGD(" unable to configure DSP decoder");
2629 a2dp.a2dp_sink_started = false;
2630 ret = -ETIMEDOUT;
2631 }
2632
2633 if (!a2dp_send_sink_setup_complete()) {
2634 ALOGD("sink_setup_complete not successful");
2635 ret = -ETIMEDOUT;
2636 }
2637 }
2638 }
2639
2640 if (a2dp.a2dp_sink_started) {
2641 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002642 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002643 }
2644 }
2645
2646 ALOGD("start A2DP sink total active sessions :%d",
2647 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302648 return ret;
2649}
2650
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302651static void reset_a2dp_enc_config_params()
2652{
2653 int ret =0;
2654
Aalique Grahame22e49102018-12-18 14:23:57 -08002655 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302656 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302657 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302658
2659 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
2660 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
2661 MIXER_ENC_CONFIG_BLOCK);
2662 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002663 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302664 } else {
2665 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
2666 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002667 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302668 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002669
2670 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2671
Manisha Agarwala51768b2018-11-01 16:30:52 +05302672 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2673
2674 if (!ctl_channel_mode) {
2675 ALOGE("failed to get tws mixer ctl");
2676 } else {
2677 channel_mode = "Two";
2678 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2679 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2680 }
2681 a2dp.is_tws_mono_mode_on = false;
2682 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302683}
2684
Surendar Karka2febd452018-12-13 17:56:43 +05302685static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002686{
2687 struct mixer_ctl *ctl_dec_data = NULL;
2688 struct abr_dec_cfg_t dummy_reset_cfg;
2689 int ret = 0;
2690
Preetam Singh Ranawat25780e42019-07-31 18:15:57 +05302691 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
2692 if (!ctl_dec_data) {
2693 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
2694 return -EINVAL;
2695 }
2696 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
2697 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
2698 sizeof(dummy_reset_cfg));
2699 if (ret != 0) {
2700 ALOGE("%s: Failed to set dummy decoder config", __func__);
2701 return ret;
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002702 }
2703
2704 return ret;
2705}
2706
Surendar Karka2febd452018-12-13 17:56:43 +05302707static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02002708{
2709 int ret =0;
2710
2711 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
2712 struct aac_dec_cfg_t dummy_reset_config;
2713
2714 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
2715 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05302716 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02002717 if (!ctl_dec_config) {
2718 ALOGE(" ERROR a2dp decoder format mixer control not identified");
2719 } else {
2720 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
2721 sizeof(struct aac_dec_cfg_t));
2722 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
2723 }
2724 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
2725 MIXER_DEC_BIT_FORMAT);
2726 if (!ctrl_bit_format) {
2727 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
2728 } else {
2729 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
2730 if (ret != 0) {
2731 ALOGE("%s: Failed to set bit format to decoder", __func__);
2732 }
2733 }
2734}
2735
Zhou Song12c29502019-03-16 10:37:18 +08002736static void reset_codec_config()
2737{
2738 reset_a2dp_enc_config_params();
2739 reset_a2dp_source_dec_config_params();
2740 a2dp_reset_backend_cfg(SOURCE);
2741 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
2742 stop_abr();
2743 a2dp.abr_config.is_abr_enabled = false;
2744}
2745
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002746int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302747{
2748 int ret =0;
2749
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002750 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002751 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002752 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302753 return -ENOSYS;
2754 }
2755
Florian Pfister1a84f312018-07-19 14:38:18 +02002756 if (a2dp.a2dp_source_total_active_session_requests > 0)
2757 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08002758 else
2759 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302760
Florian Pfister1a84f312018-07-19 14:38:18 +02002761 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302762 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02002763 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302764 if (ret < 0)
2765 ALOGE("stop stream to BT IPC lib failed");
2766 else
2767 ALOGV("stop steam to BT IPC lib successful");
Zhou Song12c29502019-03-16 10:37:18 +08002768 if (!a2dp.a2dp_source_suspended && !a2dp.swb_configured)
2769 reset_codec_config();
Surendar Karka2febd452018-12-13 17:56:43 +05302770 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302771 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002772 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002773 a2dp.a2dp_source_started = false;
2774 ALOGD("Stop A2DP playback, total active sessions :%d",
2775 a2dp.a2dp_source_total_active_session_requests);
2776 return 0;
2777}
2778
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002779int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002780{
2781 int ret =0;
2782
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002783 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002784 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002785 ALOGE("a2dp handle is not identified, Ignoring stop request");
2786 return -ENOSYS;
2787 }
2788
2789 if (a2dp.a2dp_sink_total_active_session_requests > 0)
2790 a2dp.a2dp_sink_total_active_session_requests--;
2791
2792 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2793 ALOGV("calling BT module stream stop");
2794 ret = a2dp.audio_sink_stop();
2795 if (ret < 0)
2796 ALOGE("stop stream to BT IPC lib failed");
2797 else
2798 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05302799 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002800 a2dp_reset_backend_cfg(SINK);
2801 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002802 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002803 a2dp.a2dp_source_started = false;
2804 ALOGD("Stop A2DP capture, total active sessions :%d",
2805 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302806 return 0;
2807}
2808
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002809int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302810{
Aalique Grahame22e49102018-12-18 14:23:57 -08002811 int ret = 0, val, status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302812 char value[32]={0};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302813 struct audio_usecase *uc_info;
2814 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302815
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002816 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002817 ALOGV("no supported encoders identified,ignoring a2dp setparam");
2818 status = -EINVAL;
2819 goto param_handled;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302820 }
2821
2822 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
2823 sizeof(value));
Zhou Song681350a2017-10-19 16:28:42 +08002824 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302825 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002826 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002827 ALOGV("Received device connect request for A2DP source");
2828 open_a2dp_source();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302829 }
2830 goto param_handled;
2831 }
2832
2833 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
2834 sizeof(value));
2835
Zhou Song681350a2017-10-19 16:28:42 +08002836 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302837 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002838 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002839 ALOGV("Received source device dis- connect request");
Samyak Jain4828f4c2018-08-24 16:31:06 +05302840 close_a2dp_output();
kunleiz4a1fad62018-02-08 18:00:16 +08002841 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302842 reset_a2dp_source_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002843 a2dp_reset_backend_cfg(SOURCE);
2844 } else if (audio_is_a2dp_in_device(val)) {
2845 ALOGV("Received sink device dis- connect request");
2846 close_a2dp_input();
Surendar Karka2febd452018-12-13 17:56:43 +05302847 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002848 a2dp_reset_backend_cfg(SINK);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302849 }
2850 goto param_handled;
2851 }
Surendar Karka2febd452018-12-13 17:56:43 +05302852#ifndef LINUX_ENABLED
Manisha Agarwala51768b2018-11-01 16:30:52 +05302853 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
2854 if (ret>=0) {
2855 ALOGD("Setting tws channel mode to %s",value);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002856 if (!(strncmp(value,"mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302857 a2dp.is_tws_mono_mode_on = true;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002858 else if (!(strncmp(value,"dual-mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302859 a2dp.is_tws_mono_mode_on = false;
2860 audio_a2dp_update_tws_channel_mode();
2861 goto param_handled;
2862 }
Surendar Karka2febd452018-12-13 17:56:43 +05302863#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302864 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
2865 if (ret >= 0) {
Surendar Karka2febd452018-12-13 17:56:43 +05302866 if (a2dp.bt_lib_source_handle) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302867 if ((!strncmp(value,"true",sizeof(value)))) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002868 if (a2dp.a2dp_source_suspended) {
2869 ALOGD("%s: A2DP is already suspended", __func__);
2870 goto param_handled;
2871 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302872 ALOGD("Setting a2dp to suspend state");
Florian Pfister1a84f312018-07-19 14:38:18 +02002873 a2dp.a2dp_source_suspended = true;
Surendar Karka2febd452018-12-13 17:56:43 +05302874 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
yidongh7203cca2018-09-19 16:12:25 +08002875 goto param_handled;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302876 list_for_each(node, &a2dp.adev->usecase_list) {
2877 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302878 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002879 is_a2dp_out_device_type(&uc_info->stream.out->device_list)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302880 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002881 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302882 pthread_mutex_lock(&a2dp.adev->lock);
2883 }
2884 }
Zhou Song12c29502019-03-16 10:37:18 +08002885 if (!a2dp.swb_configured)
2886 reset_codec_config();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002887 if (a2dp.audio_source_suspend)
Florian Pfister1a84f312018-07-19 14:38:18 +02002888 a2dp.audio_source_suspend();
2889 } else if (a2dp.a2dp_source_suspended == true) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302890 ALOGD("Resetting a2dp suspend state");
Zhou Song10617ed2017-05-26 13:28:48 +08002891 struct audio_usecase *uc_info;
2892 struct listnode *node;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002893 if (a2dp.clear_source_a2dpsuspend_flag)
Florian Pfister1a84f312018-07-19 14:38:18 +02002894 a2dp.clear_source_a2dpsuspend_flag();
2895 a2dp.a2dp_source_suspended = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302896 /*
2897 * It is possible that before suspend,a2dp sessions can be active
2898 * for example during music + voice activation concurrency
2899 * a2dp suspend will be called & BT will change to sco mode
2900 * though music is paused as a part of voice activation
2901 * compress session close happens only after pause timeout(10secs)
2902 * so if resume request comes before pause timeout as a2dp session
2903 * is already active IPC start will not be called from APM/audio_hw
2904 * Fix is to call a2dp start for IPC library post suspend
2905 * based on number of active session count
2906 */
Florian Pfister1a84f312018-07-19 14:38:18 +02002907 if (a2dp.a2dp_source_total_active_session_requests > 0) {
Naresh Tanniru649871a2016-11-04 18:08:32 +05302908 ALOGD(" Calling IPC lib start post suspend state");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002909 if (a2dp.audio_source_start) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002910 ret = a2dp.audio_source_start();
Naresh Tanniru649871a2016-11-04 18:08:32 +05302911 if (ret != 0) {
2912 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002913 a2dp.a2dp_source_started = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302914 }
2915 }
2916 }
Zhou Song10617ed2017-05-26 13:28:48 +08002917 list_for_each(node, &a2dp.adev->usecase_list) {
2918 uc_info = node_to_item(node, struct audio_usecase, list);
Sujin Panicker390724d2019-04-26 10:43:36 +05302919 if (uc_info->stream.out && uc_info->type == PCM_PLAYBACK &&
Aniket Kumar Lata0e6e1e52019-11-14 21:43:55 -08002920 is_a2dp_out_device_type(&uc_info->stream.out->device_list)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302921 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002922 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302923 pthread_mutex_lock(&a2dp.adev->lock);
2924 }
Zhou Song10617ed2017-05-26 13:28:48 +08002925 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302926 }
2927 }
2928 goto param_handled;
2929 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002930
2931 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
2932 sizeof(value));
2933 if (ret >= 0) {
2934 if (a2dp.is_a2dp_offload_supported &&
2935 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2936 *reconfig = true;
2937 }
2938 goto param_handled;
2939 }
2940
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302941param_handled:
2942 ALOGV("end of a2dp setparam");
Aalique Grahame22e49102018-12-18 14:23:57 -08002943 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302944}
2945
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002946void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302947{
2948 a2dp.is_handoff_in_progress = is_on;
2949}
2950
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002951bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302952{
2953 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05302954 // Or if a2dp device is selected but earlier start failed ( as a2dp
2955 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02002956 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302957}
2958
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002959void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302960{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302961 *sample_rate = a2dp.enc_sampling_rate;
2962}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302963
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002964void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02002965{
2966 *sample_rate = a2dp.dec_sampling_rate;
2967}
2968
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002969bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302970{
2971 bool ret = false;
2972
Florian Pfister1a84f312018-07-19 14:38:18 +02002973 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302974 return ret;
2975
Florian Pfister1a84f312018-07-19 14:38:18 +02002976 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08002977 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002978 (a2dp.audio_source_check_a2dp_ready))
2979 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302980 return ret;
2981}
2982
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002983bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02002984{
2985 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302986}
2987
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002988void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08002989 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302990{
2991 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02002992 a2dp.bt_lib_source_handle = NULL;
2993 a2dp.a2dp_source_started = false;
2994 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
2995 a2dp.a2dp_source_total_active_session_requests = 0;
2996 a2dp.a2dp_source_suspended = false;
2997 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302998 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302999 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07003000 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05303001 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08003002 a2dp.abr_config.is_abr_enabled = false;
3003 a2dp.abr_config.abr_started = false;
3004 a2dp.abr_config.imc_instance = 0;
3005 a2dp.abr_config.abr_tx_handle = NULL;
Zhou Song12c29502019-03-16 10:37:18 +08003006 a2dp.abr_config.abr_rx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05303007 a2dp.is_tws_mono_mode_on = false;
Naresh Tanniru66cf06c2019-03-20 19:30:37 +05303008 a2dp_source_init();
Zhou Song12c29502019-03-16 10:37:18 +08003009 a2dp.swb_configured = false;
3010
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003011 // init function pointers
3012 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08003013 init_config.fp_platform_get_pcm_device_id;
3014 fp_check_a2dp_restore = init_config.fp_check_a2dp_restore;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003015
kunleiz5a127262017-09-08 14:47:48 +08003016 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05303017 reset_a2dp_source_dec_config_params();
3018 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02003019
3020 a2dp.bt_lib_sink_handle = NULL;
3021 a2dp.a2dp_sink_started = false;
3022 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
3023 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08003024
3025 if (is_running_with_enhanced_fwk == UNINITIALIZED)
3026 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
3027 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08003028 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02003029
3030 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303031 update_offload_codec_capabilities();
3032}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003033
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003034uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003035{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003036 uint32_t latency = 0;
3037 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303038 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
3039 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003040 char value[PROPERTY_VALUE_MAX];
3041
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003042 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08003043 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003044 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303045 if (sscanf(value, "%d/%d/%d/%d/%d%d",
3046 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003047 ALOGI("Failed to parse avsync offset params from '%s'.", value);
3048 avsync_runtime_prop = 0;
3049 }
3050 }
3051
yidongh0515e042017-07-06 15:00:34 +08003052 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02003053 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
3054 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08003055 }
3056
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07003057 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02003058 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08003059 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
3060 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003061 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003062 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08003063 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
3064 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003065 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003066 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08003067 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
3068 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003069 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003070 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08003071 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
3072 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003073 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003074 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05303075 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
3076 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
3077 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02003078 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05303079 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
3080 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
3081 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05303082 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05303083 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05303084 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08003085 case CODEC_TYPE_PCM:
3086 latency = ENCODER_LATENCY_PCM;
3087 latency += DEFAULT_SINK_LATENCY_PCM;
3088 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08003089 default:
3090 latency = 200;
3091 break;
3092 }
3093 return latency;
3094}
Aalique Grahame22e49102018-12-18 14:23:57 -08003095
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08003096int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08003097 struct str_parms *reply)
3098{
3099 int ret, val = 0;
3100 char value[32]={0};
3101
3102 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
3103 value, sizeof(value));
3104 if (ret >= 0) {
3105 val = a2dp.is_a2dp_offload_supported;
3106 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
3107 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
3108 }
3109
3110 return 0;
3111}
Zhou Song12c29502019-03-16 10:37:18 +08003112
3113
3114bool configure_aptx_ad_speech_enc_fmt() {
3115 struct mixer_ctl *ctl_enc_data = NULL;
3116 int mixer_size = 0;
3117 int ret = 0;
3118 struct aptx_ad_speech_enc_cfg_t aptx_dsp_cfg;
3119
3120 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
3121 if (!ctl_enc_data) {
3122 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
3123 return false;
3124 }
3125
3126 /* Initialize dsp configuration params */
3127 memset(&aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_speech_enc_cfg_t));
3128 aptx_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_APTX_AD_SPEECH;
3129 aptx_dsp_cfg.custom_cfg.sample_rate = SAMPLING_RATE_32K;
3130 aptx_dsp_cfg.custom_cfg.num_channels = CH_MONO;
3131 aptx_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
3132 aptx_dsp_cfg.imc_info.direction = IMC_RECEIVE;
3133 aptx_dsp_cfg.imc_info.enable = IMC_ENABLE;
3134 aptx_dsp_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3135 aptx_dsp_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3136 aptx_dsp_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3137 aptx_dsp_cfg.speech_mode.swapping = SWAP_ENABLE;
3138
3139 /* Configure AFE DSP configuration */
3140 mixer_size = sizeof(struct aptx_ad_speech_enc_cfg_t);
3141 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
3142 mixer_size);
3143 if (ret != 0) {
3144 ALOGE("%s: Failed to set SWB encoder config", __func__);
3145 return false;
3146 }
3147
3148 /* Configure AFE Input Bit Format as PCM_16 */
3149 ret = a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
3150 if (ret != 0) {
3151 ALOGE("%s: Failed to set SWB bit format", __func__);
3152 return false;
3153 }
3154
3155 return true;
3156}
3157
3158bool configure_aptx_ad_speech_dec_fmt()
3159{
3160 struct mixer_ctl *ctl_dec_data = NULL;
3161 struct aptx_ad_speech_dec_cfg_t dec_cfg;
3162 int ret = 0;
3163
3164 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
3165 if (!ctl_dec_data) {
3166 ALOGE("%s: ERROR codec config data mixer control not identifed", __func__);
3167 return false;
3168 }
3169 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
3170 dec_cfg.abr_cfg.dec_format = MEDIA_FMT_APTX_AD_SPEECH;
3171 dec_cfg.abr_cfg.imc_info.direction = IMC_TRANSMIT;
3172 dec_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
3173 dec_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
3174 dec_cfg.abr_cfg.imc_info.comm_instance = APTX_AD_SPEECH_INSTANCE_ID;
3175 dec_cfg.speech_mode.mode = a2dp.adev->swb_speech_mode;
3176 dec_cfg.speech_mode.swapping = SWAP_ENABLE;
3177
3178 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
3179 sizeof(dec_cfg));
3180 if (ret != 0) {
3181 ALOGE("%s: Failed to set decoder config", __func__);
3182 return false;
3183 }
3184 return true;
3185}
3186
3187int sco_start_configuration()
3188{
3189 ALOGD("sco_start_configuration start");
3190
3191 if (!a2dp.swb_configured) {
3192 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD_SPEECH;
3193 /* Configure AFE codec*/
3194 if (configure_aptx_ad_speech_enc_fmt() &&
3195 configure_aptx_ad_speech_dec_fmt()) {
3196 ALOGD("%s: SCO enc/dec configured successfully", __func__);
3197 } else {
3198 ALOGE("%s: failed to send SCO configuration", __func__);
3199 return -ETIMEDOUT;
3200 }
3201 /* Configure backend*/
3202 a2dp.enc_sampling_rate = SAMPLING_RATE_96K;
3203 a2dp.enc_channels = CH_MONO;
3204 a2dp.abr_config.is_abr_enabled = true;
3205 a2dp_set_backend_cfg(SOURCE);
3206 /* Start abr*/
3207 start_abr();
3208 a2dp.swb_configured = true;
3209 }
3210 return 0;
3211}
3212
3213void sco_reset_configuration()
3214{
Zhou Songd6d71752019-05-21 18:08:51 +08003215 if (a2dp.swb_configured) {
3216 ALOGD("sco_reset_configuration start");
Zhou Song12c29502019-03-16 10:37:18 +08003217
Zhou Songd6d71752019-05-21 18:08:51 +08003218 reset_codec_config();
3219 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
3220 a2dp.swb_configured = false;
3221 }
Zhou Song12c29502019-03-16 10:37:18 +08003222}