blob: 90199cfc2e9d68075831586b229da402cfcfe0b8 [file] [log] [blame]
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301/*
Aalique Grahame22e49102018-12-18 14:23:57 -08002* Copyright (c) 2015-2019, The Linux Foundation. All rights reserved.
Naresh Tanniru9d027a62015-03-13 01:32:10 +05303*
4* Redistribution and use in source and binary forms, with or without
5* modification, are permitted provided that the following conditions are
6* met:
7* * Redistributions of source code must retain the above copyright
8* notice, this list of conditions and the following disclaimer.
9* * Redistributions in binary form must reproduce the above
10* copyright notice, this list of conditions and the following
11* disclaimer in the documentation and/or other materials provided
12* with the distribution.
13* * Neither the name of The Linux Foundation nor the names of its
14* contributors may be used to endorse or promote products derived
15* from this software without specific prior written permission.
16*
17* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28*/
Aalique Grahame22e49102018-12-18 14:23:57 -080029#define LOG_TAG "a2dp_offload"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053030/*#define LOG_NDEBUG 0*/
31#define LOG_NDDEBUG 0
32#include <errno.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080033#include <log/log.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053034#include <dlfcn.h>
Aalique Grahame22e49102018-12-18 14:23:57 -080035#include <pthread.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053036#include "audio_hw.h"
37#include "platform.h"
38#include "platform_api.h"
Manish Dewangan6a252632017-12-04 17:27:44 +053039#include "audio_extn.h"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053040#include <stdlib.h>
41#include <cutils/str_parms.h>
42#include <hardware/audio.h>
43#include <hardware/hardware.h>
44#include <cutils/properties.h>
45
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053046#ifdef DYNAMIC_LOG_ENABLED
47#include <log_xml_parser.h>
48#define LOG_MASK HAL_MOD_FILE_A2DP
49#include <log_utils.h>
50#endif
51
Naresh Tanniru9d027a62015-03-13 01:32:10 +053052#define AUDIO_PARAMETER_A2DP_STARTED "A2dpStarted"
Florian Pfister1a84f312018-07-19 14:38:18 +020053#define BT_IPC_SOURCE_LIB_NAME "libbthost_if.so"
Srinu Jellada99a592019-01-25 16:50:52 +053054#define BT_IPC_SOURCE_LIB_NAME_QTI "libbthost_if_qti.so"
Florian Pfister1a84f312018-07-19 14:38:18 +020055#define BT_IPC_SINK_LIB_NAME "libbthost_if_sink.so"
56#define MEDIA_FMT_NONE 0
57#define MEDIA_FMT_AAC 0x00010DA6
58#define MEDIA_FMT_APTX 0x000131ff
59#define MEDIA_FMT_APTX_HD 0x00013200
Surendar Karka2febd452018-12-13 17:56:43 +053060#define MEDIA_FMT_APTX_AD 0x00013204
Florian Pfister1a84f312018-07-19 14:38:18 +020061#define MEDIA_FMT_SBC 0x00010BF2
62#define MEDIA_FMT_CELT 0x00013221
63#define MEDIA_FMT_LDAC 0x00013224
64#define MEDIA_FMT_MP3 0x00010BE9
65#define MEDIA_FMT_APTX_ADAPTIVE 0x00013204
Naresh Tanniru9d027a62015-03-13 01:32:10 +053066#define MEDIA_FMT_AAC_AOT_LC 2
67#define MEDIA_FMT_AAC_AOT_SBR 5
68#define MEDIA_FMT_AAC_AOT_PS 29
Naresh Tanniru9d027a62015-03-13 01:32:10 +053069#define PCM_CHANNEL_L 1
70#define PCM_CHANNEL_R 2
71#define PCM_CHANNEL_C 3
72#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
73#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
74#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
75#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
76#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
77#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
Surendar Karka2febd452018-12-13 17:56:43 +053078#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
79#define MIXER_SOURCE_DEC_CONFIG_BLOCK "SLIM_7_TX Decoder Config"
80#define MIXER_SINK_DEC_CONFIG_BLOCK "SLIM_9_TX Decoder Config"
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +053081#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
Florian Pfister1a84f312018-07-19 14:38:18 +020082#define MIXER_DEC_BIT_FORMAT "AFE Output Bit Format"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053083#define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080084#define MIXER_SAMPLE_RATE_RX "BT SampleRate RX"
Surendar Karka2febd452018-12-13 17:56:43 +053085#define MIXER_SOURCE_SAMPLE_RATE_TX "BT SampleRate TX"
Samyak Jainf69e9ef2018-06-12 12:26:37 +053086#define MIXER_SAMPLE_RATE_DEFAULT "BT SampleRate"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053087#define MIXER_AFE_IN_CHANNELS "AFE Input Channels"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080088#define MIXER_ABR_TX_FEEDBACK_PATH "A2DP_SLIM7_UL_HL Switch"
89#define MIXER_SET_FEEDBACK_CHANNEL "BT set feedback channel"
Surendar Karka2febd452018-12-13 17:56:43 +053090#define MIXER_SINK_SAMPLE_RATE "BT_TX SampleRate"
Florian Pfister1a84f312018-07-19 14:38:18 +020091#define MIXER_AFE_SINK_CHANNELS "AFE Output Channels"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053092#define MIXER_ENC_FMT_SBC "SBC"
93#define MIXER_ENC_FMT_AAC "AAC"
94#define MIXER_ENC_FMT_APTX "APTX"
Manisha Agarwala51768b2018-11-01 16:30:52 +053095#define MIXER_FMT_TWS_CHANNEL_MODE "TWS Channel Mode"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053096#define MIXER_ENC_FMT_APTXHD "APTXHD"
Aalique Grahame22e49102018-12-18 14:23:57 -080097#define MIXER_END_FMT_LDAC "LDAC"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053098#define MIXER_ENC_FMT_NONE "NONE"
yidongh0515e042017-07-06 15:00:34 +080099#define ENCODER_LATENCY_SBC 10
100#define ENCODER_LATENCY_APTX 40
101#define ENCODER_LATENCY_APTX_HD 20
102#define ENCODER_LATENCY_AAC 70
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530103//To Do: Fine Tune Encoder CELT/LDAC latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530104#define ENCODER_LATENCY_CELT 40
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530105#define ENCODER_LATENCY_LDAC 40
Aalique Grahame22e49102018-12-18 14:23:57 -0800106#define ENCODER_LATENCY_PCM 50
yidongh0515e042017-07-06 15:00:34 +0800107#define DEFAULT_SINK_LATENCY_SBC 140
108#define DEFAULT_SINK_LATENCY_APTX 160
109#define DEFAULT_SINK_LATENCY_APTX_HD 180
110#define DEFAULT_SINK_LATENCY_AAC 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530111//To Do: Fine Tune Default CELT/LDAC Latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530112#define DEFAULT_SINK_LATENCY_CELT 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530113#define DEFAULT_SINK_LATENCY_LDAC 180
Aalique Grahame22e49102018-12-18 14:23:57 -0800114#define DEFAULT_SINK_LATENCY_PCM 140
115
116#define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported"
117#define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled"
118#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
119
120// Default encoder bit width
121#define DEFAULT_ENCODER_BIT_FORMAT 16
122
123// Default encoder latency
124#define DEFAULT_ENCODER_LATENCY 200
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530125
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800126// Slimbus Tx sample rate for ABR feedback channel
127#define ABR_TX_SAMPLE_RATE "KHZ_8"
128
129// Purpose ID for Inter Module Communication (IMC) in AFE
130#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
131
132// Maximum quality levels for ABR
133#define MAX_ABR_QUALITY_LEVELS 5
134
135// Instance identifier for A2DP
136#define MAX_INSTANCE_ID (UINT32_MAX / 2)
137
Sharad Sangle95d451b2018-06-19 12:24:20 +0530138#define SAMPLING_RATE_48K 48000
139#define SAMPLING_RATE_441K 44100
140#define CH_STEREO 2
141#define CH_MONO 1
Florian Pfister1a84f312018-07-19 14:38:18 +0200142#define SOURCE 0
143#define SINK 1
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800144#define UNINITIALIZED -1
145
146#ifdef __LP64__
147#define VNDK_FWK_LIB_PATH "/vendor/lib64/libqti_vndfwk_detect.so"
148#else
149#define VNDK_FWK_LIB_PATH "/vendor/lib/libqti_vndfwk_detect.so"
150#endif
151
152static void *vndk_fwk_lib_handle = NULL;
153static int is_running_with_enhanced_fwk = UNINITIALIZED;
154
155typedef int (*vndk_fwk_isVendorEnhancedFwk_t)();
156static vndk_fwk_isVendorEnhancedFwk_t vndk_fwk_isVendorEnhancedFwk;
Florian Pfister1a84f312018-07-19 14:38:18 +0200157
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530158/*
159 * Below enum values are extended from audio_base.h to
Florian Pfister1a84f312018-07-19 14:38:18 +0200160 * to keep encoder and decoder type local to bthost_ipc
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530161 * and audio_hal as these are intended only for handshake
162 * between IPC lib and Audio HAL.
163 */
164typedef enum {
Florian Pfister1a84f312018-07-19 14:38:18 +0200165 CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
166 CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
167 CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
168 CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
169 CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530170#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +0200171 CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530172#endif
Florian Pfister1a84f312018-07-19 14:38:18 +0200173 CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
174 CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
Surendar Karka2febd452018-12-13 17:56:43 +0530175 CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
Aalique Grahame22e49102018-12-18 14:23:57 -0800176 CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u
Florian Pfister1a84f312018-07-19 14:38:18 +0200177}codec_t;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530178
Sharad Sangle95d451b2018-06-19 12:24:20 +0530179/*
180 * enums which describes the APTX Adaptive
181 * channel mode, these values are used by encoder
182 */
183 typedef enum {
184 APTX_AD_CHANNEL_UNCHANGED = -1,
185 APTX_AD_CHANNEL_JOINT_STEREO = 0, // default
186 APTX_AD_CHANNEL_MONO = 1,
187 APTX_AD_CHANNEL_DUAL_MONO = 2,
188 APTX_AD_CHANNEL_STEREO_TWS = 4,
189 APTX_AD_CHANNEL_EARBUD = 8,
190} enc_aptx_ad_channel_mode;
191
192/*
193 * enums which describes the APTX Adaptive
194 * sampling frequency, these values are used
195 * by encoder
196 */
197typedef enum {
198 APTX_AD_SR_UNCHANGED = 0x0,
199 APTX_AD_48 = 0x1, // 48 KHz default
200 APTX_AD_44_1 = 0x2, // 44.1kHz
201} enc_aptx_ad_s_rate;
202
Florian Pfister1a84f312018-07-19 14:38:18 +0200203typedef int (*audio_source_open_t)(void);
204typedef int (*audio_source_close_t)(void);
205typedef int (*audio_source_start_t)(void);
206typedef int (*audio_source_stop_t)(void);
207typedef int (*audio_source_suspend_t)(void);
208typedef void (*audio_source_handoff_triggered_t)(void);
209typedef void (*clear_source_a2dpsuspend_flag_t)(void);
210typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
211 uint8_t *num_dev, codec_t *codec_type);
212typedef int (*audio_source_check_a2dp_ready_t)(void);
213typedef int (*audio_is_source_scrambling_enabled_t)(void);
Manisha Agarwala51768b2018-11-01 16:30:52 +0530214typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200215typedef int (*audio_sink_start_t)(void);
216typedef int (*audio_sink_stop_t)(void);
217typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
218typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
219typedef int (*audio_sink_check_a2dp_ready_t)(void);
220typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530221
222enum A2DP_STATE {
223 A2DP_STATE_CONNECTED,
224 A2DP_STATE_STARTED,
225 A2DP_STATE_STOPPED,
226 A2DP_STATE_DISCONNECTED,
227};
228
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800229typedef enum {
230 IMC_TRANSMIT,
231 IMC_RECEIVE,
232} imc_direction_t;
233
234typedef enum {
235 IMC_DISABLE,
236 IMC_ENABLE,
237} imc_status_t;
238
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700239typedef enum {
240 MTU_SIZE,
241 PEAK_BIT_RATE,
242} frame_control_type_t;
243
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800244// --- external function dependency ---
245fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
246fp_check_a2dp_restore_t fp_check_a2dp_restore;
247
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800248/* PCM config for ABR Feedback hostless front end */
249static struct pcm_config pcm_config_abr = {
250 .channels = 1,
251 .rate = 8000,
252 .period_size = 240,
253 .period_count = 2,
254 .format = PCM_FORMAT_S16_LE,
255 .start_threshold = 0,
256 .stop_threshold = INT_MAX,
257 .avail_min = 0,
258};
259
260/* Adaptive bitrate config for A2DP codecs */
261struct a2dp_abr_config {
262 /* Flag to denote whether Adaptive bitrate is enabled for codec */
263 bool is_abr_enabled;
264 /* Flag to denote whether front end has been opened for ABR */
265 bool abr_started;
266 /* ABR Tx path pcm handle */
267 struct pcm *abr_tx_handle;
268 /* ABR Inter Module Communication (IMC) instance ID */
269 uint32_t imc_instance;
270};
271
272static uint32_t instance_id = MAX_INSTANCE_ID;
273
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530274/* structure used to update a2dp state machine
275 * to communicate IPC library
276 * to store DSP encoder configuration information
277 */
278struct a2dp_data {
279 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200280 void *bt_lib_source_handle;
281 audio_source_open_t audio_source_open;
282 audio_source_close_t audio_source_close;
283 audio_source_start_t audio_source_start;
284 audio_source_stop_t audio_source_stop;
285 audio_source_suspend_t audio_source_suspend;
286 audio_source_handoff_triggered_t audio_source_handoff_triggered;
287 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
288 audio_get_enc_config_t audio_get_enc_config;
289 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530290 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200291 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
292 enum A2DP_STATE bt_state_source;
293 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530294 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530295 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200296 bool a2dp_source_started;
297 bool a2dp_source_suspended;
298 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530299 bool is_a2dp_offload_supported;
300 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700301 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530302 /* Mono Mode support for TWS+ */
303 bool is_tws_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530304 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800305 /* Adaptive bitrate config for A2DP codecs */
306 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200307
308 void *bt_lib_sink_handle;
309 audio_sink_start_t audio_sink_start;
310 audio_sink_stop_t audio_sink_stop;
311 audio_get_dec_config_t audio_get_dec_config;
312 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
313 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
314 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
315 enum A2DP_STATE bt_state_sink;
316 codec_t bt_decoder_format;
317 uint32_t dec_sampling_rate;
318 uint32_t dec_channels;
319 bool a2dp_sink_started;
320 int a2dp_sink_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530321};
322
323struct a2dp_data a2dp;
324
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800325/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
326 * Structures sent to configure DSP for ABR are defined below.
327 * This data helps DSP configure feedback path (BTSoC to LPASS)
328 * for link quality levels and mapping quality levels to codec
329 * specific bitrate.
330 */
331
332/* Key value pair for link quality level to bitrate mapping. */
333struct bit_rate_level_map_t {
334 uint32_t link_quality_level;
335 uint32_t bitrate;
336};
337
338/* Link quality level to bitrate mapping info sent to DSP. */
339struct quality_level_to_bitrate_info {
340 /* Number of quality levels being mapped.
341 * This will be equal to the size of mapping table.
342 */
343 uint32_t num_levels;
344 /* Quality level to bitrate mapping table */
345 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
346};
347
348/* Structure to set up Inter Module Communication (IMC) between
349 * AFE Decoder and Encoder.
350 */
351struct imc_dec_enc_info {
352 /* Decoder to encoder communication direction.
353 * Transmit = 0 / Receive = 1
354 */
355 uint32_t direction;
356 /* Enable / disable IMC between decoder and encoder */
357 uint32_t enable;
358 /* Purpose of IMC being set up between decoder and encoder.
359 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
360 * is the default value to be sent as purpose.
361 */
362 uint32_t purpose;
363 /* Unique communication instance ID.
364 * purpose and comm_instance together form the actual key
365 * used in IMC registration, which must be the same for
366 * encoder and decoder for which IMC is being set up.
367 */
368 uint32_t comm_instance;
369};
370
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700371/* Structure to control frame size of AAC encoded frames. */
372struct aac_frame_size_control_t {
373 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
374 uint32_t ctl_type;
375 /* Control value
376 * MTU_SIZE: MTU size in bytes
377 * PEAK_BIT_RATE: Peak bitrate in bits per second.
378 */
379 uint32_t ctl_value;
380};
381
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800382/* Structure used for ABR config of AFE encoder and decoder. */
383struct abr_enc_cfg_t {
384 /* Link quality level to bitrate mapping info sent to DSP. */
385 struct quality_level_to_bitrate_info mapping_info;
386 /* Information to set up IMC between decoder and encoder */
387 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700388 /* Flag to indicate whether ABR is enabled */
389 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800390} __attribute__ ((packed));
391
392/* Structure to send configuration for decoder introduced
393 * on AFE Tx path for ABR link quality feedback to BT encoder.
394 */
395struct abr_dec_cfg_t {
396 /* Decoder media format */
397 uint32_t dec_format;
398 /* Information to set up IMC between decoder and encoder */
399 struct imc_dec_enc_info imc_info;
400} __attribute__ ((packed));
401
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530402/* START of DSP configurable structures
403 * These values should match with DSP interface defintion
404 */
405
406/* AAC encoder configuration structure. */
407typedef struct aac_enc_cfg_t aac_enc_cfg_t;
408
409/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
410 * supported aac_fmt_flag are ADTS/RAW
411 * supported channel_cfg are Native mode, Mono , Stereo
412 */
413struct aac_enc_cfg_t {
414 uint32_t enc_format;
415 uint32_t bit_rate;
416 uint32_t enc_mode;
417 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530418 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530419 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530420} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530421
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700422struct aac_enc_cfg_v2_t {
423 struct aac_enc_cfg_t aac_enc_cfg;
424 struct aac_frame_size_control_t frame_ctl;
425} __attribute__ ((packed));
426
Surendar Karkabbb3c822018-11-12 13:00:38 +0530427typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
428struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200429 uint16_t aac_fmt_flag; /* LATM*/
430 uint16_t audio_object_type; /* LC */
431 uint16_t channels; /* Stereo */
432 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
433 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
434 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530435} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200436
Surendar Karkabbb3c822018-11-12 13:00:38 +0530437typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
438struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200439 uint16_t channels; /* Mono, Stereo */
440 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
441 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530442} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200443
444/* AAC decoder configuration structure. */
445typedef struct aac_dec_cfg_t aac_dec_cfg_t;
446struct aac_dec_cfg_t {
447 uint32_t dec_format;
448 audio_aac_decoder_config_t data;
449} __attribute__ ((packed));
450
451/* SBC decoder configuration structure. */
452typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
453struct sbc_dec_cfg_t {
454 uint32_t dec_format;
455 audio_sbc_decoder_config_t data;
456} __attribute__ ((packed));
457
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530458/* SBC encoder configuration structure. */
459typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
460
461/* supported num_subbands are 4/8
462 * supported blk_len are 4, 8, 12, 16
463 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
464 * supported alloc_method are LOUNDNESS/SNR
465 * supported bit_rate for mono channel is max 320kbps
466 * supported bit rate for stereo channel is max 512 kbps
467 */
468struct sbc_enc_cfg_t{
469 uint32_t enc_format;
470 uint32_t num_subbands;
471 uint32_t blk_len;
472 uint32_t channel_mode;
473 uint32_t alloc_method;
474 uint32_t bit_rate;
475 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530476} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530477
478
479/* supported num_channels are Mono/Stereo
480 * supported channel_mapping for mono is CHANNEL_C
481 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
482 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700483 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530484struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530485{
486 uint32_t enc_format;
487 uint32_t sample_rate;
488 uint16_t num_channels;
489 uint16_t reserved;
490 uint8_t channel_mapping[8];
491 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530492} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530493
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530494struct celt_specific_enc_cfg_t
495{
496 uint32_t bit_rate;
497 uint16_t frame_size;
498 uint16_t complexity;
499 uint16_t prediction_mode;
500 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530501} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530502
503struct celt_enc_cfg_t
504{
505 struct custom_enc_cfg_t custom_cfg;
506 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530507} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700508
509/* sync_mode introduced with APTX V2 libraries
510 * sync mode: 0x0 = stereo sync mode
511 * 0x01 = dual mono sync mode
512 * 0x02 = dual mono with no sync on either L or R codewords
513 */
514struct aptx_v2_enc_cfg_ext_t
515{
516 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530517} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700518
519/* APTX struct for combining custom enc and V2 fields */
520struct aptx_enc_cfg_t
521{
522 struct custom_enc_cfg_t custom_cfg;
523 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530524} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700525
Sharad Sangle95d451b2018-06-19 12:24:20 +0530526/* APTX AD structure */
527struct aptx_ad_enc_cfg_ext_t
528{
529 uint32_t sampling_freq;
530 uint32_t mtu;
531 uint32_t channel_mode;
532 uint32_t min_sink_modeA;
533 uint32_t max_sink_modeA;
534 uint32_t min_sink_modeB;
535 uint32_t max_sink_modeB;
536 uint32_t min_sink_modeC;
537 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530538 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530539} __attribute__ ((packed));
540
541struct aptx_ad_enc_cfg_t
542{
543 struct custom_enc_cfg_t custom_cfg;
544 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
545 struct abr_enc_cfg_t abr_cfg;
546} __attribute__ ((packed));
547
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530548struct ldac_specific_enc_cfg_t
549{
550 uint32_t bit_rate;
551 uint16_t channel_mode;
552 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530553} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530554
555struct ldac_enc_cfg_t
556{
557 struct custom_enc_cfg_t custom_cfg;
558 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800559 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530560} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530561
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530562/* In LE BT source code uses system/audio.h for below
563 * structure definition. To avoid multiple definition
564 * compilation error for audiohal in LE , masking structure
565 * definition under "LINUX_ENABLED" which is defined only
566 * in LE
567 */
568#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530569/* TODO: Define the following structures only for O using PLATFORM_VERSION */
570/* Information about BT SBC encoder configuration
571 * This data is used between audio HAL module and
572 * BT IPC library to configure DSP encoder
573 */
574typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530575 uint32_t subband; /* 4, 8 */
576 uint32_t blk_len; /* 4, 8, 12, 16 */
577 uint16_t sampling_rate; /*44.1khz,48khz*/
578 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
579 uint8_t alloc; /*0(Loudness),1(SNR)*/
580 uint8_t min_bitpool; /* 2 */
581 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
582 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530583 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530584} audio_sbc_encoder_config;
585
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530586/* Information about BT APTX encoder configuration
587 * This data is used between audio HAL module and
588 * BT IPC library to configure DSP encoder
589 */
590typedef struct {
591 uint16_t sampling_rate;
592 uint8_t channels;
593 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530594 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700595} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530596
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700597typedef struct {
Sharad Sangle95d451b2018-06-19 12:24:20 +0530598 uint8_t sampling_rate;
599 uint8_t channel_mode;
600 uint16_t mtu;
601 uint8_t min_sink_modeA;
602 uint8_t max_sink_modeA;
603 uint8_t min_sink_modeB;
604 uint8_t max_sink_modeB;
605 uint8_t min_sink_modeC;
606 uint8_t max_sink_modeC;
607 uint8_t TTP_modeA_low;
608 uint8_t TTP_modeA_high;
609 uint8_t TTP_modeB_low;
610 uint8_t TTP_modeB_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530611 uint32_t bits_per_sample;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530612 uint16_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530613} audio_aptx_ad_config;
614
615typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700616 uint16_t sampling_rate;
617 uint8_t channels;
618 uint32_t bitrate;
619 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530620 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700621} audio_aptx_dual_mono_config;
622
623typedef union {
624 audio_aptx_default_config *default_cfg;
625 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530626 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700627} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530628
629/* Information about BT AAC encoder configuration
630 * This data is used between audio HAL module and
631 * BT IPC library to configure DSP encoder
632 */
633typedef struct {
634 uint32_t enc_mode; /* LC, SBR, PS */
635 uint16_t format_flag; /* RAW, ADTS */
636 uint16_t channels; /* 1-Mono, 2-Stereo */
637 uint32_t sampling_rate;
638 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530639 uint32_t bits_per_sample;
Aalique Grahame22e49102018-12-18 14:23:57 -0800640 struct aac_frame_size_control_t frame_ctl;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530641} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530642#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700643
644typedef struct {
645 audio_aac_encoder_config audio_aac_enc_cfg;
646 struct aac_frame_size_control_t frame_ctl;
647} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530648
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530649/* Information about BT CELT encoder configuration
650 * This data is used between audio HAL module and
651 * BT IPC library to configure DSP encoder
652 */
653typedef struct {
654 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
655 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
656 uint16_t frame_size; /* 64-128-256-512, 512 */
657 uint16_t complexity; /* 0-10, 1 */
658 uint16_t prediction_mode; /* 0-1-2, 0 */
659 uint16_t vbr_flag; /* 0-1, 0*/
660 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530661 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530662} audio_celt_encoder_config;
663
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530664/* Information about BT LDAC encoder configuration
665 * This data is used between audio HAL module and
666 * BT IPC library to configure DSP encoder
667 */
668typedef struct {
669 uint32_t sampling_rate; /*44100,48000,88200,96000*/
670 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
671 uint16_t channel_mode; /* 0, 4, 2, 1*/
672 uint16_t mtu; /*679*/
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800673 bool is_abr_enabled;
674 struct quality_level_to_bitrate_info level_to_bitrate_map;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530675 uint32_t bits_per_sample;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530676} audio_ldac_encoder_config;
677
Surendar Karkabbb3c822018-11-12 13:00:38 +0530678/* Information about BT AAC decoder configuration
679 * This data is used between audio HAL module and
680 * BT IPC library to configure DSP decoder
681 */
682typedef struct {
683 uint16_t aac_fmt_flag; /* LATM*/
684 uint16_t audio_object_type; /* LC */
685 uint16_t channels; /* Stereo */
686 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
687 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
688 44.1k, 48k, 64k, 88.2k, 96k */
689} audio_aac_dec_config_t;
690
691/* Information about BT SBC decoder configuration
692 * This data is used between audio HAL module and
693 * BT IPC library to configure DSP decoder
694 */
695typedef struct {
696 uint16_t channels; /* Mono, Stereo */
697 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
698 44.1k, 48k, 64k, 88.2k, 96k */
699}audio_sbc_dec_config_t;
700
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530701/*********** END of DSP configurable structures ********************/
702
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530703static void update_offload_codec_capabilities()
704{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530705
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530706 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800707 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
708 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
709
710 ALOGD("%s: A2DP offload supported = %d",__func__,
711 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530712}
713
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800714static int stop_abr()
715{
716 struct mixer_ctl *ctl_abr_tx_path = NULL;
717 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
718
719 /* This function can be used if !abr_started for clean up */
720 ALOGV("%s: enter", __func__);
721
722 // Close hostless front end
723 if (a2dp.abr_config.abr_tx_handle != NULL) {
724 pcm_close(a2dp.abr_config.abr_tx_handle);
725 a2dp.abr_config.abr_tx_handle = NULL;
726 }
727 a2dp.abr_config.abr_started = false;
728 a2dp.abr_config.imc_instance = 0;
729
730 // Reset BT driver mixer control for ABR usecase
731 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
732 MIXER_SET_FEEDBACK_CHANNEL);
733 if (!ctl_set_bt_feedback_channel) {
734 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
735 return -ENOSYS;
736 }
737 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
738 ALOGE("%s: Failed to set BT usecase", __func__);
739 return -ENOSYS;
740 }
741
742 // Reset ABR Tx feedback path
743 ALOGV("%s: Disable ABR Tx feedback path", __func__);
744 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
745 MIXER_ABR_TX_FEEDBACK_PATH);
746 if (!ctl_abr_tx_path) {
747 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
748 return -ENOSYS;
749 }
750 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
751 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
752 return -ENOSYS;
753 }
754
755 return 0;
756}
757
758static int start_abr()
759{
760 struct mixer_ctl *ctl_abr_tx_path = NULL;
761 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
762 int abr_device_id;
763 int ret = 0;
764
765 if (!a2dp.abr_config.is_abr_enabled) {
766 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
767 return -ENOSYS;
768 }
769
770 if (a2dp.abr_config.abr_started) {
771 ALOGI("%s: ABR has already started", __func__);
772 return ret;
773 }
774
775 // Enable Slimbus 7 Tx feedback path
776 ALOGV("%s: Enable ABR Tx feedback path", __func__);
777 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
778 MIXER_ABR_TX_FEEDBACK_PATH);
779 if (!ctl_abr_tx_path) {
780 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
781 return -ENOSYS;
782 }
783 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
784 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
785 return -ENOSYS;
786 }
787
788 // Notify ABR usecase information to BT driver to distinguish
789 // between SCO and feedback usecase
790 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
791 MIXER_SET_FEEDBACK_CHANNEL);
792 if (!ctl_set_bt_feedback_channel) {
793 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
794 return -ENOSYS;
795 }
796 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
797 ALOGE("%s: Failed to set BT usecase", __func__);
798 return -ENOSYS;
799 }
800
801 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800802 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800803 PCM_CAPTURE);
804 if (!a2dp.abr_config.abr_tx_handle) {
805 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
806 abr_device_id, PCM_IN,
807 &pcm_config_abr);
808 if (a2dp.abr_config.abr_tx_handle == NULL ||
809 !pcm_is_ready(a2dp.abr_config.abr_tx_handle))
810 goto fail;
811 }
812 ret = pcm_start(a2dp.abr_config.abr_tx_handle);
813 if (ret < 0)
814 goto fail;
815 a2dp.abr_config.abr_started = true;
816
817 return ret;
818
819fail:
820 ALOGE("%s: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
821 stop_abr();
822 return -ENOSYS;
823}
824
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800825static int check_if_enhanced_fwk() {
826
827 int is_enhanced_fwk = 1;
828 //dlopen lib
829 vndk_fwk_lib_handle = dlopen(VNDK_FWK_LIB_PATH, RTLD_NOW);
830 if (vndk_fwk_lib_handle != NULL) {
831 vndk_fwk_isVendorEnhancedFwk = (vndk_fwk_isVendorEnhancedFwk_t)
832 dlsym(vndk_fwk_lib_handle, "isRunningWithVendorEnhancedFramework");
833 if (vndk_fwk_isVendorEnhancedFwk == NULL) {
834 ALOGW("%s: VNDK_FWK_LIB not found, defaulting to enhanced_fwk configuration",
835 __func__);
836 is_enhanced_fwk = 1;
837 } else {
838 is_enhanced_fwk = vndk_fwk_isVendorEnhancedFwk();
839 }
840 }
841 ALOGV("%s: vndk_fwk_isVendorEnhancedFwk=%d", __func__, is_enhanced_fwk);
842 return is_enhanced_fwk;
843}
844
Florian Pfister1a84f312018-07-19 14:38:18 +0200845/* API to open BT IPC library to start IPC communication for BT Source*/
846static void open_a2dp_source()
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530847{
848 int ret = 0;
849
Florian Pfister1a84f312018-07-19 14:38:18 +0200850 ALOGD(" Open A2DP source start ");
Srinu Jellada99a592019-01-25 16:50:52 +0530851 if (a2dp.bt_lib_source_handle == NULL) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800852 if (is_running_with_enhanced_fwk == UNINITIALIZED)
853 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
854 if (!is_running_with_enhanced_fwk) {
Srinu Jellada99a592019-01-25 16:50:52 +0530855 ALOGD(" Requesting for BT lib handle");
856 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
857 if (a2dp.bt_lib_source_handle == NULL) {
858 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
859 ret = -ENOSYS;
860 goto init_fail;
861 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530862 } else {
Srinu Jellada99a592019-01-25 16:50:52 +0530863 ALOGD(" Requesting for BT QTI lib handle");
864 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME_QTI, RTLD_NOW);
865 if (a2dp.bt_lib_source_handle == NULL) {
866 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME_QTI);
867 ret = -ENOSYS;
868 goto init_fail;
869 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530870 }
871 }
872
Srinu Jellada99a592019-01-25 16:50:52 +0530873 a2dp.audio_source_open = (audio_source_open_t)
874 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
875 a2dp.audio_source_start = (audio_source_start_t)
876 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
877 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
878 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
879 a2dp.audio_source_suspend = (audio_source_suspend_t)
880 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
881 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
882 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
883 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
884 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
885 a2dp.audio_source_stop = (audio_source_stop_t)
886 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
887 a2dp.audio_source_close = (audio_source_close_t)
888 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
889 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
890 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
891 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
892 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
893 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
894 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
895 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
896 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
897
Florian Pfister1a84f312018-07-19 14:38:18 +0200898 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
899 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530900 ALOGD("calling BT stream open");
Florian Pfister1a84f312018-07-19 14:38:18 +0200901 ret = a2dp.audio_source_open();
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800902 if (ret != 0) {
Florian Pfister1a84f312018-07-19 14:38:18 +0200903 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530904 goto init_fail;
905 }
Florian Pfister1a84f312018-07-19 14:38:18 +0200906 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530907 } else {
Florian Pfister1a84f312018-07-19 14:38:18 +0200908 ALOGD("Called a2dp open with improper state, Ignoring request state %d", a2dp.bt_state_source);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530909 }
910 } else {
911 ALOGE("a2dp handle is not identified, Ignoring open request");
Florian Pfister1a84f312018-07-19 14:38:18 +0200912 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530913 goto init_fail;
914 }
915
916init_fail:
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800917 if (ret != 0 && (a2dp.bt_lib_source_handle != NULL)) {
Florian Pfister1a84f312018-07-19 14:38:18 +0200918 dlclose(a2dp.bt_lib_source_handle);
919 a2dp.bt_lib_source_handle = NULL;
920 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800921 if (vndk_fwk_lib_handle != NULL) {
922 dlclose(vndk_fwk_lib_handle);
923 vndk_fwk_lib_handle = NULL;
924 }
Florian Pfister1a84f312018-07-19 14:38:18 +0200925}
926
927/* API to open BT IPC library to start IPC communication for BT Sink*/
928static void open_a2dp_sink()
929{
930 ALOGD(" Open A2DP input start ");
931 if (a2dp.bt_lib_sink_handle == NULL){
932 ALOGD(" Requesting for BT lib handle");
933 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
934
935 if (a2dp.bt_lib_sink_handle == NULL) {
936 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
937 } else {
938 a2dp.audio_sink_start = (audio_sink_start_t)
939 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
940 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
941 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
942 a2dp.audio_sink_stop = (audio_sink_stop_t)
943 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
944 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
945 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
946 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
947 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
948 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530949 }
950}
951
952static int close_a2dp_output()
953{
954 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +0200955
956 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
957 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530958 return -ENOSYS;
959 }
Florian Pfister1a84f312018-07-19 14:38:18 +0200960
961 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
962 ALOGD("calling BT source stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800963 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +0200964 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530965 }
Florian Pfister1a84f312018-07-19 14:38:18 +0200966 a2dp.a2dp_source_started = false;
967 a2dp.a2dp_source_total_active_session_requests = 0;
968 a2dp.a2dp_source_suspended = false;
969 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530970 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530971 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +0200972 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800973 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
974 stop_abr();
975 a2dp.abr_config.is_abr_enabled = false;
976 a2dp.abr_config.abr_started = false;
977 a2dp.abr_config.imc_instance = 0;
978 a2dp.abr_config.abr_tx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +0530979 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +0200980
981 return 0;
982}
983
984static int close_a2dp_input()
985{
986 ALOGV("%s\n",__func__);
987
988 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
989 ALOGE("a2dp sink handle is not identified, Ignoring close request");
990 return -ENOSYS;
991 }
992
993 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
994 ALOGD("calling BT sink stream close");
Arun Mirpuri5dc77802019-02-26 16:32:42 -0800995 if (a2dp.audio_source_close() == false)
Florian Pfister1a84f312018-07-19 14:38:18 +0200996 ALOGE("failed close a2dp sink control path from BT library");
997 }
998 a2dp.a2dp_sink_started = false;
999 a2dp.a2dp_sink_total_active_session_requests = 0;
1000 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
1001 a2dp.dec_sampling_rate = 48000;
1002 a2dp.dec_channels = 2;
1003 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301004
1005 return 0;
1006}
1007
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301008static void a2dp_check_and_set_scrambler()
1009{
1010 bool scrambler_mode = false;
1011 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +02001012 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
1013 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301014
1015 if (scrambler_mode) {
1016 //enable scrambler in dsp
1017 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
1018 MIXER_SCRAMBLER_MODE);
1019 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001020 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301021 return;
1022 } else {
1023 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
1024 ALOGE("%s: Could not set scrambler mode", __func__);
1025 return;
1026 }
1027 }
1028 }
1029}
1030
Florian Pfister1a84f312018-07-19 14:38:18 +02001031static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301032{
Florian Pfister1a84f312018-07-19 14:38:18 +02001033 char *rate_str = NULL, *channels = NULL;
1034 uint32_t sampling_rate;
1035 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
1036 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301037
Florian Pfister1a84f312018-07-19 14:38:18 +02001038 if (direction == SINK) {
1039 sampling_rate = a2dp.dec_sampling_rate;
1040 } else {
1041 sampling_rate = a2dp.enc_sampling_rate;
1042 }
1043 //For LDAC encoder and AAC decoder open slimbus port at
1044 //96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1045 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301046 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Florian Pfister1a84f312018-07-19 14:38:18 +02001047 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301048 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1049 sampling_rate = sampling_rate *2;
1050 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001051
Aalique Grahame22e49102018-12-18 14:23:57 -08001052 // No need to configure backend for PCM format.
1053 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1054 return 0;
1055 }
1056
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301057 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301058 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301059 case 44100:
1060 rate_str = "KHZ_44P1";
1061 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301062 case 88200:
1063 rate_str = "KHZ_88P2";
1064 break;
1065 case 96000:
1066 rate_str = "KHZ_96";
1067 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001068 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301069 default:
1070 rate_str = "KHZ_48";
1071 break;
1072 }
1073
Florian Pfister1a84f312018-07-19 14:38:18 +02001074 if (direction == SINK) {
1075 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1076 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301077 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001078 } else {
1079 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1080 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301081 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001082 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301083 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001084
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301085 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1086 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301087 is_configured = false;
1088 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301089 }
1090
Surendar Karka2febd452018-12-13 17:56:43 +05301091 if (direction == SOURCE) {
1092 /* Set Tx backend sample rate */
1093 if (a2dp.abr_config.is_abr_enabled)
1094 rate_str = ABR_TX_SAMPLE_RATE;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001095
Surendar Karka2febd452018-12-13 17:56:43 +05301096 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1097 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1098 MIXER_SOURCE_SAMPLE_RATE_TX);
1099 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301100 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301101 is_configured = false;
1102 goto fail;
1103 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301104
Surendar Karka2febd452018-12-13 17:56:43 +05301105 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1106 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1107 is_configured = false;
1108 goto fail;
1109 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301110 }
1111 } else {
1112 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1113 MIXER_SAMPLE_RATE_TX is not supported */
1114 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1115 MIXER_SAMPLE_RATE_DEFAULT);
1116 if (!ctl_sample_rate) {
1117 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301118 is_configured = false;
1119 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301120 }
1121
1122 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1123 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001124 is_configured = false;
1125 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301126 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301127 }
1128
Florian Pfister1a84f312018-07-19 14:38:18 +02001129 if (direction == SINK) {
1130 switch (a2dp.dec_channels) {
1131 case 1:
1132 channels = "One";
1133 break;
1134 case 2:
1135 default:
1136 channels = "Two";
1137 break;
1138 }
1139
Ramu Gottipati02809682018-12-19 16:46:12 +05301140 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001141 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1142 MIXER_AFE_SINK_CHANNELS);
1143 } else {
1144 //Configure AFE enc channels
1145 switch (a2dp.enc_channels) {
1146 case 1:
1147 channels = "One";
1148 break;
1149 case 2:
1150 default:
1151 channels = "Two";
1152 break;
1153 }
1154
Ramu Gottipati02809682018-12-19 16:46:12 +05301155 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001156 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1157 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301158 }
1159
Florian Pfister1a84f312018-07-19 14:38:18 +02001160 if (!ctrl_channels) {
1161 ALOGE(" ERROR AFE channels mixer control not identified");
1162 } else {
1163 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301164 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001165 is_configured = false;
1166 goto fail;
1167 }
1168 }
1169 is_configured = true;
1170fail:
1171 return is_configured;
1172}
1173
Surendar Karkabbb3c822018-11-12 13:00:38 +05301174bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001175{
1176 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1177 struct aac_dec_cfg_t aac_dsp_cfg;
1178 bool is_configured = false;
1179 int ret = 0;
1180
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001181 if (aac_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001182 return false;
1183
Surendar Karka2febd452018-12-13 17:56:43 +05301184 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001185 if (!ctl_dec_data) {
1186 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1187 is_configured = false;
1188 goto fail;
1189 }
1190
1191 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1192 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1193 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1194 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1195 switch(aac_bt_cfg->audio_object_type) {
1196 case 0:
1197 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301198 break;
1199 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001200 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1201 break;
1202 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301203 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001204 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301205 break;
1206 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001207 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1208 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1209 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1210 sizeof(struct aac_dec_cfg_t));
1211 if (ret != 0) {
1212 ALOGE("%s: failed to set AAC decoder config", __func__);
1213 is_configured = false;
1214 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001215 }
1216
Florian Pfister1a84f312018-07-19 14:38:18 +02001217 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1218 MIXER_DEC_BIT_FORMAT);
1219 if (!ctrl_bit_format) {
1220 ALOGE(" ERROR Dec bit format mixer control not identified");
1221 is_configured = false;
1222 goto fail;
1223 }
1224 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1225 if (ret != 0) {
1226 ALOGE("%s: Failed to set bit format to decoder", __func__);
1227 is_configured = false;
1228 goto fail;
1229 }
1230
1231 is_configured = true;
1232 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1233 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1234 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1235 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1236 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1237fail:
1238 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301239}
1240
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301241static int a2dp_set_bit_format(uint32_t enc_bit_format)
1242{
1243 const char *bit_format = NULL;
1244 struct mixer_ctl *ctrl_bit_format = NULL;
1245
1246 // Configure AFE Input Bit Format
1247 switch (enc_bit_format) {
1248 case 32:
1249 bit_format = "S32_LE";
1250 break;
1251 case 24:
1252 bit_format = "S24_LE";
1253 break;
1254 case 16:
1255 default:
1256 bit_format = "S16_LE";
1257 break;
1258 }
1259
1260 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1261 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1262 MIXER_ENC_BIT_FORMAT);
1263 if (!ctrl_bit_format) {
1264 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1265 return -ENOSYS;
1266 }
1267 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1268 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1269 return -ENOSYS;
1270 }
1271 return 0;
1272}
1273
Surendar Karka2febd452018-12-13 17:56:43 +05301274static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301275{
Surendar Karka2febd452018-12-13 17:56:43 +05301276 const char *rate_str = "KHZ_8", *channels = "Zero";
1277 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1278 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301279
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001280 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301281 if (direction == SINK) {
1282 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1283 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1284 MIXER_SINK_SAMPLE_RATE);
1285 } else {
1286 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1287 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1288 MIXER_SAMPLE_RATE_RX);
1289 }
1290 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301291
Surendar Karka2febd452018-12-13 17:56:43 +05301292 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1293 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301294 return -ENOSYS;
1295 }
1296
1297 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301298 MIXER_SOURCE_SAMPLE_RATE_TX);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301299 if (!ctl_sample_rate_tx) {
1300 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1301 return -ENOSYS;
1302 }
1303
1304 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1305 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1306 return -ENOSYS;
1307 }
1308 } else {
1309
Surendar Karka2febd452018-12-13 17:56:43 +05301310 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301311 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301312 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301313 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1314 return -ENOSYS;
1315 }
1316
Surendar Karka2febd452018-12-13 17:56:43 +05301317 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301318 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1319 return -ENOSYS;
1320 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001321 }
1322
1323 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301324 if (direction == SINK) {
1325 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1326 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1327 MIXER_AFE_SINK_CHANNELS);
1328 } else {
1329 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1330 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1331 MIXER_AFE_IN_CHANNELS);
1332 }
1333 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001334 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1335 return -ENOSYS;
1336 }
Surendar Karka2febd452018-12-13 17:56:43 +05301337 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001338 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1339 return -ENOSYS;
1340 }
1341
1342 return 0;
1343}
1344
1345/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301346static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001347{
1348 struct mixer_ctl *ctl_dec_data = NULL;
1349 struct abr_dec_cfg_t dec_cfg;
1350 int ret = 0;
1351
1352 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301353 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001354 if (!ctl_dec_data) {
1355 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1356 return false;
1357 }
1358 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1359 dec_cfg.dec_format = dec_format;
1360 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1361 dec_cfg.imc_info.enable = IMC_ENABLE;
1362 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1363 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1364
1365 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1366 sizeof(dec_cfg));
1367 if (ret != 0) {
1368 ALOGE("%s: Failed to set decoder config", __func__);
1369 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301370 }
Surendar Karka2febd452018-12-13 17:56:43 +05301371 }
1372
1373 return true;
1374}
1375
Surendar Karkabbb3c822018-11-12 13:00:38 +05301376bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001377{
1378 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1379 struct sbc_dec_cfg_t sbc_dsp_cfg;
1380 bool is_configured = false;
1381 int ret = 0;
1382
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001383 if (sbc_bt_cfg == NULL)
Florian Pfister1a84f312018-07-19 14:38:18 +02001384 goto fail;
1385
Surendar Karka2febd452018-12-13 17:56:43 +05301386 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001387 if (!ctl_dec_data) {
1388 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1389 is_configured = false;
1390 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301391 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001392
Florian Pfister1a84f312018-07-19 14:38:18 +02001393 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1394 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1395 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1396 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1397 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1398 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301399
Florian Pfister1a84f312018-07-19 14:38:18 +02001400 if (ret != 0) {
1401 ALOGE("%s: failed to set SBC decoder config", __func__);
1402 is_configured = false;
1403 goto fail;
1404 }
1405
1406 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1407 MIXER_DEC_BIT_FORMAT);
1408 if (!ctrl_bit_format) {
1409 ALOGE(" ERROR Dec bit format mixer control not identified");
1410 is_configured = false;
1411 goto fail;
1412 }
1413 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1414 if (ret != 0) {
1415 ALOGE("%s: Failed to set bit format to decoder", __func__);
1416 is_configured = false;
1417 goto fail;
1418 }
1419
1420 is_configured = true;
1421 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1422 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1423 a2dp.dec_channels = 1;
1424 else
1425 a2dp.dec_channels = 2;
1426 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1427 ALOGV("Successfully updated SBC dec format");
1428fail:
1429 return is_configured;
1430}
1431
Florian Pfister1a84f312018-07-19 14:38:18 +02001432/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301433static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301434{
Florian Pfister1a84f312018-07-19 14:38:18 +02001435 void *codec_info = NULL;
1436 codec_t codec_type = CODEC_TYPE_INVALID;
1437 bool is_configured = false;
1438 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301439
Florian Pfister1a84f312018-07-19 14:38:18 +02001440 if (!a2dp.audio_get_dec_config) {
1441 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1442 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301443 }
1444
Surendar Karka2febd452018-12-13 17:56:43 +05301445 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001446 if (!ctl_dec_data) {
1447 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1448 is_configured = false;
1449 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301450 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001451 codec_info = a2dp.audio_get_dec_config(&codec_type);
1452 switch(codec_type) {
1453 case CODEC_TYPE_SBC:
1454 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301455 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001456 break;
1457 case CODEC_TYPE_AAC:
1458 ALOGD(" AAC decoder supported BT device");
1459 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301460 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001461 break;
1462 default:
1463 ALOGD(" Received Unsupported decoder format");
1464 is_configured = false;
1465 break;
1466 }
1467 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301468}
1469
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301470/* API to configure SBC DSP encoder */
1471bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1472{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301473 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301474 struct sbc_enc_cfg_t sbc_dsp_cfg;
1475 bool is_configured = false;
1476 int ret = 0;
1477
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001478 if (sbc_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301479 return false;
1480
1481 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1482 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001483 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301484 is_configured = false;
1485 goto fail;
1486 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301487 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001488 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301489 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1490 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1491 switch(sbc_bt_cfg->channels) {
1492 case 0:
1493 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1494 break;
1495 case 1:
1496 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1497 break;
1498 case 3:
1499 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1500 break;
1501 case 2:
1502 default:
1503 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1504 break;
1505 }
1506 if (sbc_bt_cfg->alloc)
1507 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1508 else
1509 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1510 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1511 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1512 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1513 sizeof(struct sbc_enc_cfg_t));
1514 if (ret != 0) {
1515 ALOGE("%s: failed to set SBC encoder config", __func__);
1516 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301517 goto fail;
1518 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301519 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301520 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301521 is_configured = false;
1522 goto fail;
1523 }
1524 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001525 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301526 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301527
1528 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1529 a2dp.enc_channels = 1;
1530 else
1531 a2dp.enc_channels = 2;
1532
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301533 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1534 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301535fail:
1536 return is_configured;
1537}
1538
Manish Dewangan6a252632017-12-04 17:27:44 +05301539#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301540static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1541 audio_aptx_encoder_config *aptx_bt_cfg)
1542{
1543 int ret = 0;
1544
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001545 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301546 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1547 aptx_dsp_cfg, aptx_bt_cfg);
1548 return -EINVAL;
1549 }
1550
1551 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301552 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301553
1554
1555 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1556 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1557 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1558 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1559 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1560 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1561 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1562 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1563 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301564 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301565 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1566 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1567 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1568 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1569
1570
1571 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1572 case APTX_AD_CHANNEL_UNCHANGED:
1573 case APTX_AD_CHANNEL_JOINT_STEREO:
1574 case APTX_AD_CHANNEL_DUAL_MONO:
1575 case APTX_AD_CHANNEL_STEREO_TWS:
1576 case APTX_AD_CHANNEL_EARBUD:
1577 default:
1578 a2dp.enc_channels = CH_STEREO;
1579 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1580 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1581 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1582 break;
1583 case APTX_AD_CHANNEL_MONO:
1584 a2dp.enc_channels = CH_MONO;
1585 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1586 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1587 break;
1588 }
1589 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1590 case APTX_AD_SR_UNCHANGED:
1591 case APTX_AD_48:
1592 default:
1593 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1594 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1595 break;
1596 case APTX_AD_44_1:
1597 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1598 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
1599 break;
1600 }
1601 ALOGV("Successfully updated APTX AD enc format with \
1602 samplingrate: %d channels:%d",
1603 aptx_dsp_cfg->custom_cfg.sample_rate,
1604 aptx_dsp_cfg->custom_cfg.num_channels);
1605
1606 return ret;
1607}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301608
1609static void audio_a2dp_update_tws_channel_mode()
1610{
1611 char* channel_mode;
1612 struct mixer_ctl *ctl_channel_mode;
1613 if (a2dp.is_tws_mono_mode_on)
1614 channel_mode = "One";
1615 else
1616 channel_mode = "Two";
1617 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1618 if (!ctl_channel_mode) {
1619 ALOGE("failed to get tws mixer ctl");
1620 return;
1621 }
1622 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1623 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1624 return;
1625 }
1626}
1627
Manish Dewangan6a252632017-12-04 17:27:44 +05301628static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
1629 audio_aptx_encoder_config *aptx_bt_cfg)
1630{
1631 int ret = 0;
1632
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001633 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301634 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1635 aptx_dsp_cfg, aptx_bt_cfg);
1636 return -EINVAL;
1637 }
1638
1639 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001640 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301641
1642 if (!a2dp.is_aptx_dual_mono_supported) {
1643 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1644 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1645 } else {
1646 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1647 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1648 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1649 }
1650
1651 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
1652 case 1:
1653 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1654 break;
1655 case 2:
1656 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05301657 if (!a2dp.is_tws_mono_mode_on) {
1658 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1659 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1660 }
1661 else {
1662 a2dp.is_tws_mono_mode_on = true;
1663 ALOGD("Update tws for mono_mode_on: %d",a2dp.is_tws_mono_mode_on);
Manisha Agarwala51768b2018-11-01 16:30:52 +05301664 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301665 break;
1666 }
1667 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
1668 if (!a2dp.is_aptx_dual_mono_supported) {
1669 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1670 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
1671 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
1672 aptx_dsp_cfg->custom_cfg.num_channels);
1673 } else {
1674 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1675 ALOGV("Successfully updated APTX dual mono enc format with \
1676 samplingrate: %d channels:%d syncmode %d",
1677 aptx_dsp_cfg->custom_cfg.sample_rate,
1678 aptx_dsp_cfg->custom_cfg.num_channels,
1679 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
1680 }
1681 return ret;
1682}
1683#else
1684static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
1685 audio_aptx_encoder_config *aptx_bt_cfg)
1686{
1687 int ret = 0;
1688
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001689 if (aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
Manish Dewangan6a252632017-12-04 17:27:44 +05301690 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1691 aptx_dsp_cfg, aptx_bt_cfg);
1692 return -EINVAL;
1693 }
1694
1695 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001696 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301697 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
1698 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
1699 switch(aptx_dsp_cfg->num_channels) {
1700 case 1:
1701 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
1702 break;
1703 case 2:
1704 default:
1705 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
1706 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
1707 break;
1708 }
1709
1710 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
1711 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
1712
1713 return ret;
1714}
1715#endif
1716
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301717/* API to configure APTX DSP encoder */
1718bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1719{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301720 struct mixer_ctl *ctl_enc_data = NULL;
Manish Dewangan6a252632017-12-04 17:27:44 +05301721 int mixer_size;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301722 bool is_configured = false;
1723 int ret = 0;
Manish Dewangan6a252632017-12-04 17:27:44 +05301724 int sample_rate_backup;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301725
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001726 if (aptx_bt_cfg == NULL)
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301727 return false;
1728
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001729 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1730 if (!ctl_enc_data) {
1731 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1732 return false;
1733 }
1734
Manish Dewangan6a252632017-12-04 17:27:44 +05301735#ifndef LINUX_ENABLED
1736 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301737 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001738 if (a2dp.is_aptx_adaptive) {
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001739 mixer_size = sizeof(struct aptx_ad_enc_cfg_t);
1740 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
1741 sample_rate_backup = aptx_ad_dsp_cfg.custom_cfg.sample_rate;
1742 } else {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301743 mixer_size = sizeof(struct aptx_enc_cfg_t);
1744 sample_rate_backup = aptx_bt_cfg->default_cfg->sampling_rate;
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001745 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
1746 }
1747 if (ret) {
1748 is_configured = false;
1749 goto fail;
1750 }
1751
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001752 if (a2dp.is_aptx_adaptive) {
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001753 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
1754 mixer_size);
1755 } else {
1756 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1757 mixer_size);
Sharad Sangle95d451b2018-06-19 12:24:20 +05301758 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301759#else
1760 struct custom_enc_cfg_t aptx_dsp_cfg;
1761 mixer_size = sizeof(struct custom_enc_cfg_t);
1762 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05301763 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05301764 if (ret) {
1765 is_configured = false;
1766 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301767 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001768 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1769 mixer_size);
1770#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301771 if (ret != 0) {
1772 ALOGE("%s: Failed to set APTX encoder config", __func__);
1773 is_configured = false;
1774 goto fail;
1775 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301776#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001777 if (a2dp.is_aptx_adaptive)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301778 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001779 else if (a2dp.is_aptx_dual_mono_supported)
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301780 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
1781 else
1782 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301783#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301784 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301785 is_configured = false;
1786 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301787 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301788 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301789 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05301790 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301791 else
Surendar Karka2febd452018-12-13 17:56:43 +05301792 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301793fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05301794 /*restore sample rate */
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001795 if (!is_configured)
Manish Dewangan6a252632017-12-04 17:27:44 +05301796 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301797 return is_configured;
1798}
1799
1800/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301801 */
Manish Dewangan6a252632017-12-04 17:27:44 +05301802#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07001803bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05301804#else
1805bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1806#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301807{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301808 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301809 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301810 bool is_configured = false;
1811 int ret = 0;
1812
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001813 if (aptx_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301814 return false;
1815
1816 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1817 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001818 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301819 is_configured = false;
1820 goto fail;
1821 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301822
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301823 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001824 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301825 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
1826 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
1827 switch(aptx_dsp_cfg.num_channels) {
1828 case 1:
1829 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1830 break;
1831 case 2:
1832 default:
1833 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1834 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1835 break;
1836 }
1837 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301838 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301839 if (ret != 0) {
1840 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
1841 is_configured = false;
1842 goto fail;
1843 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301844 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301845 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301846 is_configured = false;
1847 goto fail;
1848 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301849 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001850 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301851 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301852 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301853 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
1854 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301855fail:
1856 return is_configured;
1857}
1858
1859/* API to configure AAC DSP encoder */
1860bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
1861{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301862 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301863 struct aac_enc_cfg_t aac_dsp_cfg;
1864 bool is_configured = false;
1865 int ret = 0;
1866
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001867 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301868 return false;
1869
1870 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1871 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001872 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301873 is_configured = false;
1874 goto fail;
1875 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301876 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001877 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301878 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05301879 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001880 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301881 case 0:
1882 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
1883 break;
1884 case 2:
1885 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
1886 break;
1887 case 1:
1888 default:
1889 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
1890 break;
1891 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05301892 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301893 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001894
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301895 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
1896 sizeof(struct aac_enc_cfg_t));
1897 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001898 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301899 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301900 goto fail;
1901 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301902 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301903 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301904 is_configured = false;
1905 goto fail;
1906 }
1907 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001908 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301909 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001910 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001911 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
1912 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
1913fail:
1914 return is_configured;
1915}
1916
1917bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
1918{
1919 struct mixer_ctl *ctl_enc_data = NULL;
1920 struct aac_enc_cfg_v2_t aac_dsp_cfg;
1921 bool is_configured = false;
1922 int ret = 0;
1923
1924 if (aac_bt_cfg == NULL)
1925 return false;
1926
1927 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1928 if (!ctl_enc_data) {
1929 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1930 is_configured = false;
1931 goto fail;
1932 }
1933 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05301934 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001935 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
1936 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
1937 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
1938 case 0:
1939 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
1940 break;
1941 case 2:
1942 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
1943 break;
1944 case 1:
1945 default:
1946 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
1947 break;
1948 }
1949 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
1950 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
1951 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
1952 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
1953
1954 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
1955 sizeof(struct aac_enc_cfg_v2_t));
1956 if (ret != 0) {
1957 ALOGE("%s: Failed to set AAC encoder config", __func__);
1958 is_configured = false;
1959 goto fail;
1960 }
1961 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
1962 if (ret != 0) {
1963 is_configured = false;
1964 goto fail;
1965 }
1966 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05301967 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001968 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
1969 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
1970 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
1971 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301972fail:
1973 return is_configured;
1974}
1975
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301976bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
1977{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301978 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301979 struct celt_enc_cfg_t celt_dsp_cfg;
1980 bool is_configured = false;
1981 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08001982 if (celt_bt_cfg == NULL)
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301983 return false;
1984
1985 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1986 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001987 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301988 is_configured = false;
1989 goto fail;
1990 }
1991 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
1992
Florian Pfister1a84f312018-07-19 14:38:18 +02001993 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301994 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
1995 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
1996 switch(celt_dsp_cfg.custom_cfg.num_channels) {
1997 case 1:
1998 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1999 break;
2000 case 2:
2001 default:
2002 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2003 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2004 break;
2005 }
2006
2007 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
2008
2009 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
2010 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
2011 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
2012 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
2013 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
2014
2015 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
2016 sizeof(struct celt_enc_cfg_t));
2017 if (ret != 0) {
2018 ALOGE("%s: Failed to set CELT encoder config", __func__);
2019 is_configured = false;
2020 goto fail;
2021 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302022 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302023 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302024 is_configured = false;
2025 goto fail;
2026 }
2027 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002028 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302029 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302030 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302031 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
2032 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
2033fail:
2034 return is_configured;
2035}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302036
2037bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2038{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302039 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302040 struct ldac_enc_cfg_t ldac_dsp_cfg;
2041 bool is_configured = false;
2042 int ret = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002043 if (ldac_bt_cfg == NULL)
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302044 return false;
2045
2046 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2047 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002048 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302049 is_configured = false;
2050 goto fail;
2051 }
2052 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2053
Florian Pfister1a84f312018-07-19 14:38:18 +02002054 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302055 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2056 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2057 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2058 case 4:
2059 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2060 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2061 break;
2062 case 2:
2063 case 1:
2064 default:
2065 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2066 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2067 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2068 break;
2069 }
2070
2071 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2072 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2073 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002074 if (ldac_bt_cfg->is_abr_enabled) {
2075 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2076 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2077 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2078 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2079 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002080 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002081 }
2082
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302083 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2084 sizeof(struct ldac_enc_cfg_t));
2085 if (ret != 0) {
2086 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2087 is_configured = false;
2088 goto fail;
2089 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302090 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302091 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302092 is_configured = false;
2093 goto fail;
2094 }
2095 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002096 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302097 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2098 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002099 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302100 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2101 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2102fail:
2103 return is_configured;
2104}
2105
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302106bool configure_a2dp_encoder_format()
2107{
2108 void *codec_info = NULL;
2109 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002110 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302111 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002112 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302113
Florian Pfister1a84f312018-07-19 14:38:18 +02002114 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302115 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2116 return false;
2117 }
2118 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002119 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302120 &codec_type);
2121
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002122 // ABR disabled by default for all codecs
2123 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302124 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002125
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302126 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002127 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302128 ALOGD(" Received SBC encoder supported BT device");
2129 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002130 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302131 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002132 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302133 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302134#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002135 a2dp.is_aptx_dual_mono_supported = false;
2136 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302137#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302138 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002139 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302140 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002141 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302142 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302143#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302144 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002145 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302146#else
2147 is_configured =
2148 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2149#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002150 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302151#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002152 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002153 ALOGD(" Received APTX dual mono encoder supported BT device");
2154 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302155 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2156 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002157 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2158 is_configured =
2159 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302160 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302161#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002162 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302163 ALOGD(" Received AAC encoder supported BT device");
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002164 bool is_aac_frame_ctl_enabled =
2165 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
2166 is_configured = is_aac_frame_ctl_enabled ?
2167 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2168 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302169 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002170 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302171 ALOGD(" Received CELT encoder supported BT device");
2172 is_configured =
2173 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2174 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002175 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302176 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002177 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2178 instance_id = MAX_INSTANCE_ID;
2179 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302180 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002181 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302182 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302183 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302184#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302185 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302186 ALOGD(" Received APTX AD encoder supported BT device");
2187 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2188 instance_id = MAX_INSTANCE_ID;
2189 a2dp.abr_config.imc_instance = instance_id--;
2190 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2191 a2dp.is_aptx_adaptive = true;
2192 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2193 is_configured =
2194 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302195 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302196 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302197#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002198 case CODEC_TYPE_PCM:
2199 ALOGD("Received PCM format for BT device");
2200 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2201 is_configured = true;
2202 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302203 default:
2204 ALOGD(" Received Unsupported encoder formar");
2205 is_configured = false;
2206 break;
2207 }
2208 return is_configured;
2209}
2210
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002211int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302212{
2213 int ret = 0;
2214
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002215 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302216
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002217 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002218 && a2dp.audio_get_enc_config)) {
2219 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302220 return -ENOSYS;
2221 }
2222
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002223 if (a2dp.a2dp_source_suspended == true) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302224 //session will be restarted after suspend completion
2225 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302226 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302227 }
2228
Florian Pfister1a84f312018-07-19 14:38:18 +02002229 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302230 ALOGD("calling BT module stream start");
2231 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002232 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302233 ALOGE("BT controller start return = %d",ret);
2234 if (ret != 0 ) {
2235 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002236 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302237 } else {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002238 if (configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002239 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302240 ret = 0;
2241 ALOGD("Start playback successful to BT library");
2242 } else {
2243 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002244 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302245 ret = -ETIMEDOUT;
2246 }
2247 }
2248 }
2249
Florian Pfister1a84f312018-07-19 14:38:18 +02002250 if (a2dp.a2dp_source_started) {
2251 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302252 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302253 audio_a2dp_update_tws_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002254 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002255 if (a2dp.abr_config.is_abr_enabled)
2256 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302257 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302258
2259 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002260 a2dp.a2dp_source_total_active_session_requests);
2261 return ret;
2262}
2263
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002264uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002265{
2266 uint32_t latency = 0;
2267
2268 switch(a2dp.bt_decoder_format) {
2269 case CODEC_TYPE_SBC:
2270 latency = DEFAULT_SINK_LATENCY_SBC;
2271 break;
2272 case CODEC_TYPE_AAC:
2273 latency = DEFAULT_SINK_LATENCY_AAC;
2274 break;
2275 default:
2276 latency = 200;
2277 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2278 break;
2279 }
2280 return (uint64_t)latency;
2281}
2282
2283bool a2dp_send_sink_setup_complete(void) {
2284 uint64_t system_latency = 0;
2285 bool is_complete = false;
2286
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002287 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002288
2289 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2290 is_complete = true;
2291 }
2292 return is_complete;
2293}
2294
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002295bool a2dp_sink_is_ready()
2296{
2297 bool ret = false;
2298
2299 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2300 (a2dp.is_a2dp_offload_supported) &&
2301 (a2dp.audio_sink_check_a2dp_ready))
2302 ret = a2dp.audio_sink_check_a2dp_ready();
2303 return ret;
2304}
2305
2306int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002307{
2308 int ret = 0;
2309
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002310 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002311
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002312 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
Florian Pfister1a84f312018-07-19 14:38:18 +02002313 && a2dp.audio_get_dec_config)) {
2314 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2315 return -ENOSYS;
2316 }
2317
2318 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2319 ALOGD("calling BT module stream start");
2320 /* This call indicates BT IPC lib to start capture */
2321 ret = a2dp.audio_sink_start();
2322 ALOGE("BT controller start capture return = %d",ret);
2323 if (ret != 0 ) {
2324 ALOGE("BT controller start capture failed");
2325 a2dp.a2dp_sink_started = false;
2326 } else {
2327
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002328 if (!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002329 ALOGD("Wait for capture ready not successful");
2330 ret = -ETIMEDOUT;
2331 }
2332
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002333 if (configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002334 a2dp.a2dp_sink_started = true;
2335 ret = 0;
2336 ALOGD("Start capture successful to BT library");
2337 } else {
2338 ALOGD(" unable to configure DSP decoder");
2339 a2dp.a2dp_sink_started = false;
2340 ret = -ETIMEDOUT;
2341 }
2342
2343 if (!a2dp_send_sink_setup_complete()) {
2344 ALOGD("sink_setup_complete not successful");
2345 ret = -ETIMEDOUT;
2346 }
2347 }
2348 }
2349
2350 if (a2dp.a2dp_sink_started) {
2351 if (a2dp_set_backend_cfg(SINK) == true) {
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002352 a2dp.a2dp_sink_total_active_session_requests++;
Florian Pfister1a84f312018-07-19 14:38:18 +02002353 }
2354 }
2355
2356 ALOGD("start A2DP sink total active sessions :%d",
2357 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302358 return ret;
2359}
2360
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302361static void reset_a2dp_enc_config_params()
2362{
2363 int ret =0;
2364
Aalique Grahame22e49102018-12-18 14:23:57 -08002365 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302366 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302367 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302368
2369 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
2370 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
2371 MIXER_ENC_CONFIG_BLOCK);
2372 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002373 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302374 } else {
2375 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
2376 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002377 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302378 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002379
2380 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2381
Manisha Agarwala51768b2018-11-01 16:30:52 +05302382 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2383
2384 if (!ctl_channel_mode) {
2385 ALOGE("failed to get tws mixer ctl");
2386 } else {
2387 channel_mode = "Two";
2388 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2389 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2390 }
2391 a2dp.is_tws_mono_mode_on = false;
2392 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302393}
2394
Surendar Karka2febd452018-12-13 17:56:43 +05302395static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002396{
2397 struct mixer_ctl *ctl_dec_data = NULL;
2398 struct abr_dec_cfg_t dummy_reset_cfg;
2399 int ret = 0;
2400
2401 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05302402 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002403 if (!ctl_dec_data) {
2404 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
2405 return -EINVAL;
2406 }
2407 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
2408 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
2409 sizeof(dummy_reset_cfg));
2410 if (ret != 0) {
2411 ALOGE("%s: Failed to set dummy decoder config", __func__);
2412 return ret;
2413 }
2414 }
2415
2416 return ret;
2417}
2418
Surendar Karka2febd452018-12-13 17:56:43 +05302419static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02002420{
2421 int ret =0;
2422
2423 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
2424 struct aac_dec_cfg_t dummy_reset_config;
2425
2426 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
2427 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05302428 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02002429 if (!ctl_dec_config) {
2430 ALOGE(" ERROR a2dp decoder format mixer control not identified");
2431 } else {
2432 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
2433 sizeof(struct aac_dec_cfg_t));
2434 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
2435 }
2436 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
2437 MIXER_DEC_BIT_FORMAT);
2438 if (!ctrl_bit_format) {
2439 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
2440 } else {
2441 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
2442 if (ret != 0) {
2443 ALOGE("%s: Failed to set bit format to decoder", __func__);
2444 }
2445 }
2446}
2447
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002448int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302449{
2450 int ret =0;
2451
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002452 ALOGV("a2dp_stop_playback start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002453 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002454 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302455 return -ENOSYS;
2456 }
2457
Florian Pfister1a84f312018-07-19 14:38:18 +02002458 if (a2dp.a2dp_source_total_active_session_requests > 0)
2459 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08002460 else
2461 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302462
Florian Pfister1a84f312018-07-19 14:38:18 +02002463 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302464 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02002465 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302466 if (ret < 0)
2467 ALOGE("stop stream to BT IPC lib failed");
2468 else
2469 ALOGV("stop steam to BT IPC lib successful");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302470 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302471 reset_a2dp_source_dec_config_params();
2472 a2dp_reset_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002473 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
2474 stop_abr();
2475 a2dp.abr_config.is_abr_enabled = false;
Surendar Karka2febd452018-12-13 17:56:43 +05302476 a2dp.a2dp_source_started = false;
Florian Pfister1a84f312018-07-19 14:38:18 +02002477 a2dp_reset_backend_cfg(SOURCE);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302478 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002479 if (!a2dp.a2dp_source_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002480 a2dp.a2dp_source_started = false;
2481 ALOGD("Stop A2DP playback, total active sessions :%d",
2482 a2dp.a2dp_source_total_active_session_requests);
2483 return 0;
2484}
2485
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002486int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002487{
2488 int ret =0;
2489
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002490 ALOGV("a2dp_stop_capture start");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002491 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002492 ALOGE("a2dp handle is not identified, Ignoring stop request");
2493 return -ENOSYS;
2494 }
2495
2496 if (a2dp.a2dp_sink_total_active_session_requests > 0)
2497 a2dp.a2dp_sink_total_active_session_requests--;
2498
2499 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2500 ALOGV("calling BT module stream stop");
2501 ret = a2dp.audio_sink_stop();
2502 if (ret < 0)
2503 ALOGE("stop stream to BT IPC lib failed");
2504 else
2505 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05302506 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002507 a2dp_reset_backend_cfg(SINK);
2508 }
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002509 if (!a2dp.a2dp_sink_total_active_session_requests)
Florian Pfister1a84f312018-07-19 14:38:18 +02002510 a2dp.a2dp_source_started = false;
2511 ALOGD("Stop A2DP capture, total active sessions :%d",
2512 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302513 return 0;
2514}
2515
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002516int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302517{
Aalique Grahame22e49102018-12-18 14:23:57 -08002518 int ret = 0, val, status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302519 char value[32]={0};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302520 struct audio_usecase *uc_info;
2521 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302522
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002523 if (a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002524 ALOGV("no supported encoders identified,ignoring a2dp setparam");
2525 status = -EINVAL;
2526 goto param_handled;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302527 }
2528
2529 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
2530 sizeof(value));
Zhou Song681350a2017-10-19 16:28:42 +08002531 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302532 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002533 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002534 ALOGV("Received device connect request for A2DP source");
2535 open_a2dp_source();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302536 }
2537 goto param_handled;
2538 }
2539
2540 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
2541 sizeof(value));
2542
Zhou Song681350a2017-10-19 16:28:42 +08002543 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302544 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002545 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002546 ALOGV("Received source device dis- connect request");
Samyak Jain4828f4c2018-08-24 16:31:06 +05302547 close_a2dp_output();
kunleiz4a1fad62018-02-08 18:00:16 +08002548 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302549 reset_a2dp_source_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002550 a2dp_reset_backend_cfg(SOURCE);
2551 } else if (audio_is_a2dp_in_device(val)) {
2552 ALOGV("Received sink device dis- connect request");
2553 close_a2dp_input();
Surendar Karka2febd452018-12-13 17:56:43 +05302554 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002555 a2dp_reset_backend_cfg(SINK);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302556 }
2557 goto param_handled;
2558 }
Surendar Karka2febd452018-12-13 17:56:43 +05302559#ifndef LINUX_ENABLED
Manisha Agarwala51768b2018-11-01 16:30:52 +05302560 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
2561 if (ret>=0) {
2562 ALOGD("Setting tws channel mode to %s",value);
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002563 if (!(strncmp(value,"mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302564 a2dp.is_tws_mono_mode_on = true;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002565 else if (!(strncmp(value,"dual-mono",strlen(value))))
Manisha Agarwala51768b2018-11-01 16:30:52 +05302566 a2dp.is_tws_mono_mode_on = false;
2567 audio_a2dp_update_tws_channel_mode();
2568 goto param_handled;
2569 }
Surendar Karka2febd452018-12-13 17:56:43 +05302570#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302571 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
2572 if (ret >= 0) {
Surendar Karka2febd452018-12-13 17:56:43 +05302573 if (a2dp.bt_lib_source_handle) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302574 if ((!strncmp(value,"true",sizeof(value)))) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002575 if (a2dp.a2dp_source_suspended) {
2576 ALOGD("%s: A2DP is already suspended", __func__);
2577 goto param_handled;
2578 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302579 ALOGD("Setting a2dp to suspend state");
Florian Pfister1a84f312018-07-19 14:38:18 +02002580 a2dp.a2dp_source_suspended = true;
Surendar Karka2febd452018-12-13 17:56:43 +05302581 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
yidongh7203cca2018-09-19 16:12:25 +08002582 goto param_handled;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302583 list_for_each(node, &a2dp.adev->usecase_list) {
2584 uc_info = node_to_item(node, struct audio_usecase, list);
Zhou Songc66eb7e2017-08-08 18:29:07 +08002585 if (uc_info->type == PCM_PLAYBACK &&
2586 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302587 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002588 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302589 pthread_mutex_lock(&a2dp.adev->lock);
2590 }
2591 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302592 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302593 reset_a2dp_source_dec_config_params();
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002594 if (a2dp.audio_source_suspend)
Florian Pfister1a84f312018-07-19 14:38:18 +02002595 a2dp.audio_source_suspend();
2596 } else if (a2dp.a2dp_source_suspended == true) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302597 ALOGD("Resetting a2dp suspend state");
Zhou Song10617ed2017-05-26 13:28:48 +08002598 struct audio_usecase *uc_info;
2599 struct listnode *node;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002600 if (a2dp.clear_source_a2dpsuspend_flag)
Florian Pfister1a84f312018-07-19 14:38:18 +02002601 a2dp.clear_source_a2dpsuspend_flag();
2602 a2dp.a2dp_source_suspended = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302603 /*
2604 * It is possible that before suspend,a2dp sessions can be active
2605 * for example during music + voice activation concurrency
2606 * a2dp suspend will be called & BT will change to sco mode
2607 * though music is paused as a part of voice activation
2608 * compress session close happens only after pause timeout(10secs)
2609 * so if resume request comes before pause timeout as a2dp session
2610 * is already active IPC start will not be called from APM/audio_hw
2611 * Fix is to call a2dp start for IPC library post suspend
2612 * based on number of active session count
2613 */
Florian Pfister1a84f312018-07-19 14:38:18 +02002614 if (a2dp.a2dp_source_total_active_session_requests > 0) {
Naresh Tanniru649871a2016-11-04 18:08:32 +05302615 ALOGD(" Calling IPC lib start post suspend state");
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002616 if (a2dp.audio_source_start) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002617 ret = a2dp.audio_source_start();
Naresh Tanniru649871a2016-11-04 18:08:32 +05302618 if (ret != 0) {
2619 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002620 a2dp.a2dp_source_started = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302621 }
2622 }
2623 }
Zhou Song10617ed2017-05-26 13:28:48 +08002624 list_for_each(node, &a2dp.adev->usecase_list) {
2625 uc_info = node_to_item(node, struct audio_usecase, list);
Zhou Songc66eb7e2017-08-08 18:29:07 +08002626 if (uc_info->type == PCM_PLAYBACK &&
2627 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302628 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002629 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302630 pthread_mutex_lock(&a2dp.adev->lock);
2631 }
Zhou Song10617ed2017-05-26 13:28:48 +08002632 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302633 }
2634 }
2635 goto param_handled;
2636 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002637
2638 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
2639 sizeof(value));
2640 if (ret >= 0) {
2641 if (a2dp.is_a2dp_offload_supported &&
2642 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2643 *reconfig = true;
2644 }
2645 goto param_handled;
2646 }
2647
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302648param_handled:
2649 ALOGV("end of a2dp setparam");
Aalique Grahame22e49102018-12-18 14:23:57 -08002650 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302651}
2652
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002653void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302654{
2655 a2dp.is_handoff_in_progress = is_on;
2656}
2657
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002658bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302659{
2660 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05302661 // Or if a2dp device is selected but earlier start failed ( as a2dp
2662 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02002663 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302664}
2665
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002666void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302667{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302668 *sample_rate = a2dp.enc_sampling_rate;
2669}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302670
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002671void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02002672{
2673 *sample_rate = a2dp.dec_sampling_rate;
2674}
2675
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002676bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302677{
2678 bool ret = false;
2679
Florian Pfister1a84f312018-07-19 14:38:18 +02002680 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302681 return ret;
2682
Florian Pfister1a84f312018-07-19 14:38:18 +02002683 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08002684 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002685 (a2dp.audio_source_check_a2dp_ready))
2686 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302687 return ret;
2688}
2689
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002690bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02002691{
2692 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302693}
2694
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002695void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08002696 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302697{
2698 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02002699 a2dp.bt_lib_source_handle = NULL;
2700 a2dp.a2dp_source_started = false;
2701 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
2702 a2dp.a2dp_source_total_active_session_requests = 0;
2703 a2dp.a2dp_source_suspended = false;
2704 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302705 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302706 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002707 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302708 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002709 a2dp.abr_config.is_abr_enabled = false;
2710 a2dp.abr_config.abr_started = false;
2711 a2dp.abr_config.imc_instance = 0;
2712 a2dp.abr_config.abr_tx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302713 a2dp.is_tws_mono_mode_on = false;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002714
2715 // init function pointers
2716 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08002717 init_config.fp_platform_get_pcm_device_id;
2718 fp_check_a2dp_restore = init_config.fp_check_a2dp_restore;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002719
kunleiz5a127262017-09-08 14:47:48 +08002720 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302721 reset_a2dp_source_dec_config_params();
2722 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002723
2724 a2dp.bt_lib_sink_handle = NULL;
2725 a2dp.a2dp_sink_started = false;
2726 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
2727 a2dp.a2dp_sink_total_active_session_requests = 0;
Arun Mirpuri5dc77802019-02-26 16:32:42 -08002728
2729 if (is_running_with_enhanced_fwk == UNINITIALIZED)
2730 is_running_with_enhanced_fwk = check_if_enhanced_fwk();
2731 if (is_running_with_enhanced_fwk)
Aalique Grahame6e763712019-01-31 16:18:17 -08002732 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02002733
2734 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302735 update_offload_codec_capabilities();
2736}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002737
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002738uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002739{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002740 uint32_t latency = 0;
2741 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302742 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
2743 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002744 char value[PROPERTY_VALUE_MAX];
2745
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002746 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08002747 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002748 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302749 if (sscanf(value, "%d/%d/%d/%d/%d%d",
2750 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002751 ALOGI("Failed to parse avsync offset params from '%s'.", value);
2752 avsync_runtime_prop = 0;
2753 }
2754 }
2755
yidongh0515e042017-07-06 15:00:34 +08002756 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02002757 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2758 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08002759 }
2760
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07002761 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002762 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08002763 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
2764 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002765 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002766 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08002767 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
2768 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002769 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002770 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08002771 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
2772 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002773 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002774 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08002775 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
2776 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002777 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002778 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302779 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
2780 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
2781 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002782 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302783 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
2784 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
2785 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05302786 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05302787 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05302788 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08002789 case CODEC_TYPE_PCM:
2790 latency = ENCODER_LATENCY_PCM;
2791 latency += DEFAULT_SINK_LATENCY_PCM;
2792 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002793 default:
2794 latency = 200;
2795 break;
2796 }
2797 return latency;
2798}
Aalique Grahame22e49102018-12-18 14:23:57 -08002799
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002800int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08002801 struct str_parms *reply)
2802{
2803 int ret, val = 0;
2804 char value[32]={0};
2805
2806 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
2807 value, sizeof(value));
2808 if (ret >= 0) {
2809 val = a2dp.is_a2dp_offload_supported;
2810 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
2811 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
2812 }
2813
2814 return 0;
2815}