blob: e1f902e188d0ec4b8463fc22276ade636e9dc72a [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>
Srinu Jellada99a592019-01-25 16:50:52 +053036#include <vndfwk-detect.h>
Naresh Tanniru9d027a62015-03-13 01:32:10 +053037#include "audio_hw.h"
38#include "platform.h"
39#include "platform_api.h"
Manish Dewangan6a252632017-12-04 17:27:44 +053040#include "audio_extn.h"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053041#include <stdlib.h>
42#include <cutils/str_parms.h>
43#include <hardware/audio.h>
44#include <hardware/hardware.h>
45#include <cutils/properties.h>
46
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053047#ifdef DYNAMIC_LOG_ENABLED
48#include <log_xml_parser.h>
49#define LOG_MASK HAL_MOD_FILE_A2DP
50#include <log_utils.h>
51#endif
52
Naresh Tanniru9d027a62015-03-13 01:32:10 +053053#define AUDIO_PARAMETER_A2DP_STARTED "A2dpStarted"
Florian Pfister1a84f312018-07-19 14:38:18 +020054#define BT_IPC_SOURCE_LIB_NAME "libbthost_if.so"
Srinu Jellada99a592019-01-25 16:50:52 +053055#define BT_IPC_SOURCE_LIB_NAME_QTI "libbthost_if_qti.so"
Florian Pfister1a84f312018-07-19 14:38:18 +020056#define BT_IPC_SINK_LIB_NAME "libbthost_if_sink.so"
57#define MEDIA_FMT_NONE 0
58#define MEDIA_FMT_AAC 0x00010DA6
59#define MEDIA_FMT_APTX 0x000131ff
60#define MEDIA_FMT_APTX_HD 0x00013200
Surendar Karka2febd452018-12-13 17:56:43 +053061#define MEDIA_FMT_APTX_AD 0x00013204
Florian Pfister1a84f312018-07-19 14:38:18 +020062#define MEDIA_FMT_SBC 0x00010BF2
63#define MEDIA_FMT_CELT 0x00013221
64#define MEDIA_FMT_LDAC 0x00013224
65#define MEDIA_FMT_MP3 0x00010BE9
66#define MEDIA_FMT_APTX_ADAPTIVE 0x00013204
Naresh Tanniru9d027a62015-03-13 01:32:10 +053067#define MEDIA_FMT_AAC_AOT_LC 2
68#define MEDIA_FMT_AAC_AOT_SBR 5
69#define MEDIA_FMT_AAC_AOT_PS 29
Naresh Tanniru9d027a62015-03-13 01:32:10 +053070#define PCM_CHANNEL_L 1
71#define PCM_CHANNEL_R 2
72#define PCM_CHANNEL_C 3
73#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
74#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
75#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
76#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
77#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
78#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
Surendar Karka2febd452018-12-13 17:56:43 +053079#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
80#define MIXER_SOURCE_DEC_CONFIG_BLOCK "SLIM_7_TX Decoder Config"
81#define MIXER_SINK_DEC_CONFIG_BLOCK "SLIM_9_TX Decoder Config"
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +053082#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
Florian Pfister1a84f312018-07-19 14:38:18 +020083#define MIXER_DEC_BIT_FORMAT "AFE Output Bit Format"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053084#define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080085#define MIXER_SAMPLE_RATE_RX "BT SampleRate RX"
Surendar Karka2febd452018-12-13 17:56:43 +053086#define MIXER_SOURCE_SAMPLE_RATE_TX "BT SampleRate TX"
Samyak Jainf69e9ef2018-06-12 12:26:37 +053087#define MIXER_SAMPLE_RATE_DEFAULT "BT SampleRate"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053088#define MIXER_AFE_IN_CHANNELS "AFE Input Channels"
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -080089#define MIXER_ABR_TX_FEEDBACK_PATH "A2DP_SLIM7_UL_HL Switch"
90#define MIXER_SET_FEEDBACK_CHANNEL "BT set feedback channel"
Surendar Karka2febd452018-12-13 17:56:43 +053091#define MIXER_SINK_SAMPLE_RATE "BT_TX SampleRate"
Florian Pfister1a84f312018-07-19 14:38:18 +020092#define MIXER_AFE_SINK_CHANNELS "AFE Output Channels"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053093#define MIXER_ENC_FMT_SBC "SBC"
94#define MIXER_ENC_FMT_AAC "AAC"
95#define MIXER_ENC_FMT_APTX "APTX"
Manisha Agarwala51768b2018-11-01 16:30:52 +053096#define MIXER_FMT_TWS_CHANNEL_MODE "TWS Channel Mode"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053097#define MIXER_ENC_FMT_APTXHD "APTXHD"
Aalique Grahame22e49102018-12-18 14:23:57 -080098#define MIXER_END_FMT_LDAC "LDAC"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053099#define MIXER_ENC_FMT_NONE "NONE"
yidongh0515e042017-07-06 15:00:34 +0800100#define ENCODER_LATENCY_SBC 10
101#define ENCODER_LATENCY_APTX 40
102#define ENCODER_LATENCY_APTX_HD 20
103#define ENCODER_LATENCY_AAC 70
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530104//To Do: Fine Tune Encoder CELT/LDAC latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530105#define ENCODER_LATENCY_CELT 40
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530106#define ENCODER_LATENCY_LDAC 40
Aalique Grahame22e49102018-12-18 14:23:57 -0800107#define ENCODER_LATENCY_PCM 50
yidongh0515e042017-07-06 15:00:34 +0800108#define DEFAULT_SINK_LATENCY_SBC 140
109#define DEFAULT_SINK_LATENCY_APTX 160
110#define DEFAULT_SINK_LATENCY_APTX_HD 180
111#define DEFAULT_SINK_LATENCY_AAC 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530112//To Do: Fine Tune Default CELT/LDAC Latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530113#define DEFAULT_SINK_LATENCY_CELT 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530114#define DEFAULT_SINK_LATENCY_LDAC 180
Aalique Grahame22e49102018-12-18 14:23:57 -0800115#define DEFAULT_SINK_LATENCY_PCM 140
116
117#define SYSPROP_A2DP_OFFLOAD_SUPPORTED "ro.bluetooth.a2dp_offload.supported"
118#define SYSPROP_A2DP_OFFLOAD_DISABLED "persist.bluetooth.a2dp_offload.disabled"
119#define SYSPROP_A2DP_CODEC_LATENCIES "vendor.audio.a2dp.codec.latency"
120
121// Default encoder bit width
122#define DEFAULT_ENCODER_BIT_FORMAT 16
123
124// Default encoder latency
125#define DEFAULT_ENCODER_LATENCY 200
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530126
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800127// Slimbus Tx sample rate for ABR feedback channel
128#define ABR_TX_SAMPLE_RATE "KHZ_8"
129
130// Purpose ID for Inter Module Communication (IMC) in AFE
131#define IMC_PURPOSE_ID_BT_INFO 0x000132E2
132
133// Maximum quality levels for ABR
134#define MAX_ABR_QUALITY_LEVELS 5
135
136// Instance identifier for A2DP
137#define MAX_INSTANCE_ID (UINT32_MAX / 2)
138
Sharad Sangle95d451b2018-06-19 12:24:20 +0530139#define SAMPLING_RATE_48K 48000
140#define SAMPLING_RATE_441K 44100
141#define CH_STEREO 2
142#define CH_MONO 1
Florian Pfister1a84f312018-07-19 14:38:18 +0200143#define SOURCE 0
144#define SINK 1
145
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530146/*
147 * Below enum values are extended from audio_base.h to
Florian Pfister1a84f312018-07-19 14:38:18 +0200148 * to keep encoder and decoder type local to bthost_ipc
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530149 * and audio_hal as these are intended only for handshake
150 * between IPC lib and Audio HAL.
151 */
152typedef enum {
Florian Pfister1a84f312018-07-19 14:38:18 +0200153 CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
154 CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
155 CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
156 CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
157 CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530158#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +0200159 CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530160#endif
Florian Pfister1a84f312018-07-19 14:38:18 +0200161 CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
162 CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
Surendar Karka2febd452018-12-13 17:56:43 +0530163 CODEC_TYPE_APTX_AD = 620756992u, // 0x25000000UL
Aalique Grahame22e49102018-12-18 14:23:57 -0800164 CODEC_TYPE_PCM = AUDIO_FORMAT_PCM_16_BIT, // 0x1u
Florian Pfister1a84f312018-07-19 14:38:18 +0200165}codec_t;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530166
Sharad Sangle95d451b2018-06-19 12:24:20 +0530167/*
168 * enums which describes the APTX Adaptive
169 * channel mode, these values are used by encoder
170 */
171 typedef enum {
172 APTX_AD_CHANNEL_UNCHANGED = -1,
173 APTX_AD_CHANNEL_JOINT_STEREO = 0, // default
174 APTX_AD_CHANNEL_MONO = 1,
175 APTX_AD_CHANNEL_DUAL_MONO = 2,
176 APTX_AD_CHANNEL_STEREO_TWS = 4,
177 APTX_AD_CHANNEL_EARBUD = 8,
178} enc_aptx_ad_channel_mode;
179
180/*
181 * enums which describes the APTX Adaptive
182 * sampling frequency, these values are used
183 * by encoder
184 */
185typedef enum {
186 APTX_AD_SR_UNCHANGED = 0x0,
187 APTX_AD_48 = 0x1, // 48 KHz default
188 APTX_AD_44_1 = 0x2, // 44.1kHz
189} enc_aptx_ad_s_rate;
190
Florian Pfister1a84f312018-07-19 14:38:18 +0200191typedef int (*audio_source_open_t)(void);
192typedef int (*audio_source_close_t)(void);
193typedef int (*audio_source_start_t)(void);
194typedef int (*audio_source_stop_t)(void);
195typedef int (*audio_source_suspend_t)(void);
196typedef void (*audio_source_handoff_triggered_t)(void);
197typedef void (*clear_source_a2dpsuspend_flag_t)(void);
198typedef void * (*audio_get_enc_config_t)(uint8_t *multicast_status,
199 uint8_t *num_dev, codec_t *codec_type);
200typedef int (*audio_source_check_a2dp_ready_t)(void);
201typedef int (*audio_is_source_scrambling_enabled_t)(void);
Manisha Agarwala51768b2018-11-01 16:30:52 +0530202typedef bool (*audio_is_tws_mono_mode_enable_t)(void);
Florian Pfister1a84f312018-07-19 14:38:18 +0200203typedef int (*audio_sink_start_t)(void);
204typedef int (*audio_sink_stop_t)(void);
205typedef void * (*audio_get_dec_config_t)(codec_t *codec_type);
206typedef void * (*audio_sink_session_setup_complete_t)(uint64_t system_latency);
207typedef int (*audio_sink_check_a2dp_ready_t)(void);
208typedef uint16_t (*audio_sink_get_a2dp_latency_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530209
210enum A2DP_STATE {
211 A2DP_STATE_CONNECTED,
212 A2DP_STATE_STARTED,
213 A2DP_STATE_STOPPED,
214 A2DP_STATE_DISCONNECTED,
215};
216
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800217typedef enum {
218 IMC_TRANSMIT,
219 IMC_RECEIVE,
220} imc_direction_t;
221
222typedef enum {
223 IMC_DISABLE,
224 IMC_ENABLE,
225} imc_status_t;
226
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700227typedef enum {
228 MTU_SIZE,
229 PEAK_BIT_RATE,
230} frame_control_type_t;
231
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800232// --- external function dependency ---
233fp_platform_get_pcm_device_id_t fp_platform_get_pcm_device_id;
234fp_check_a2dp_restore_t fp_check_a2dp_restore;
235
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800236/* PCM config for ABR Feedback hostless front end */
237static struct pcm_config pcm_config_abr = {
238 .channels = 1,
239 .rate = 8000,
240 .period_size = 240,
241 .period_count = 2,
242 .format = PCM_FORMAT_S16_LE,
243 .start_threshold = 0,
244 .stop_threshold = INT_MAX,
245 .avail_min = 0,
246};
247
248/* Adaptive bitrate config for A2DP codecs */
249struct a2dp_abr_config {
250 /* Flag to denote whether Adaptive bitrate is enabled for codec */
251 bool is_abr_enabled;
252 /* Flag to denote whether front end has been opened for ABR */
253 bool abr_started;
254 /* ABR Tx path pcm handle */
255 struct pcm *abr_tx_handle;
256 /* ABR Inter Module Communication (IMC) instance ID */
257 uint32_t imc_instance;
258};
259
260static uint32_t instance_id = MAX_INSTANCE_ID;
261
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530262/* structure used to update a2dp state machine
263 * to communicate IPC library
264 * to store DSP encoder configuration information
265 */
266struct a2dp_data {
267 struct audio_device *adev;
Florian Pfister1a84f312018-07-19 14:38:18 +0200268 void *bt_lib_source_handle;
269 audio_source_open_t audio_source_open;
270 audio_source_close_t audio_source_close;
271 audio_source_start_t audio_source_start;
272 audio_source_stop_t audio_source_stop;
273 audio_source_suspend_t audio_source_suspend;
274 audio_source_handoff_triggered_t audio_source_handoff_triggered;
275 clear_source_a2dpsuspend_flag_t clear_source_a2dpsuspend_flag;
276 audio_get_enc_config_t audio_get_enc_config;
277 audio_source_check_a2dp_ready_t audio_source_check_a2dp_ready;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530278 audio_is_tws_mono_mode_enable_t audio_is_tws_mono_mode_enable;
Florian Pfister1a84f312018-07-19 14:38:18 +0200279 audio_is_source_scrambling_enabled_t audio_is_source_scrambling_enabled;
280 enum A2DP_STATE bt_state_source;
281 codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530282 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530283 uint32_t enc_channels;
Florian Pfister1a84f312018-07-19 14:38:18 +0200284 bool a2dp_source_started;
285 bool a2dp_source_suspended;
286 int a2dp_source_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530287 bool is_a2dp_offload_supported;
288 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700289 bool is_aptx_dual_mono_supported;
Manisha Agarwala51768b2018-11-01 16:30:52 +0530290 /* Mono Mode support for TWS+ */
291 bool is_tws_mono_mode_on;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530292 bool is_aptx_adaptive;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800293 /* Adaptive bitrate config for A2DP codecs */
294 struct a2dp_abr_config abr_config;
Florian Pfister1a84f312018-07-19 14:38:18 +0200295
296 void *bt_lib_sink_handle;
297 audio_sink_start_t audio_sink_start;
298 audio_sink_stop_t audio_sink_stop;
299 audio_get_dec_config_t audio_get_dec_config;
300 audio_sink_session_setup_complete_t audio_sink_session_setup_complete;
301 audio_sink_check_a2dp_ready_t audio_sink_check_a2dp_ready;
302 audio_sink_get_a2dp_latency_t audio_sink_get_a2dp_latency;
303 enum A2DP_STATE bt_state_sink;
304 codec_t bt_decoder_format;
305 uint32_t dec_sampling_rate;
306 uint32_t dec_channels;
307 bool a2dp_sink_started;
308 int a2dp_sink_total_active_session_requests;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530309};
310
311struct a2dp_data a2dp;
312
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800313/* Adaptive bitrate (ABR) is supported by certain Bluetooth codecs.
314 * Structures sent to configure DSP for ABR are defined below.
315 * This data helps DSP configure feedback path (BTSoC to LPASS)
316 * for link quality levels and mapping quality levels to codec
317 * specific bitrate.
318 */
319
320/* Key value pair for link quality level to bitrate mapping. */
321struct bit_rate_level_map_t {
322 uint32_t link_quality_level;
323 uint32_t bitrate;
324};
325
326/* Link quality level to bitrate mapping info sent to DSP. */
327struct quality_level_to_bitrate_info {
328 /* Number of quality levels being mapped.
329 * This will be equal to the size of mapping table.
330 */
331 uint32_t num_levels;
332 /* Quality level to bitrate mapping table */
333 struct bit_rate_level_map_t bit_rate_level_map[MAX_ABR_QUALITY_LEVELS];
334};
335
336/* Structure to set up Inter Module Communication (IMC) between
337 * AFE Decoder and Encoder.
338 */
339struct imc_dec_enc_info {
340 /* Decoder to encoder communication direction.
341 * Transmit = 0 / Receive = 1
342 */
343 uint32_t direction;
344 /* Enable / disable IMC between decoder and encoder */
345 uint32_t enable;
346 /* Purpose of IMC being set up between decoder and encoder.
347 * IMC_PURPOSE_ID_BT_INFO defined for link quality feedback
348 * is the default value to be sent as purpose.
349 */
350 uint32_t purpose;
351 /* Unique communication instance ID.
352 * purpose and comm_instance together form the actual key
353 * used in IMC registration, which must be the same for
354 * encoder and decoder for which IMC is being set up.
355 */
356 uint32_t comm_instance;
357};
358
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700359/* Structure to control frame size of AAC encoded frames. */
360struct aac_frame_size_control_t {
361 /* Type of frame size control: MTU_SIZE / PEAK_BIT_RATE*/
362 uint32_t ctl_type;
363 /* Control value
364 * MTU_SIZE: MTU size in bytes
365 * PEAK_BIT_RATE: Peak bitrate in bits per second.
366 */
367 uint32_t ctl_value;
368};
369
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800370/* Structure used for ABR config of AFE encoder and decoder. */
371struct abr_enc_cfg_t {
372 /* Link quality level to bitrate mapping info sent to DSP. */
373 struct quality_level_to_bitrate_info mapping_info;
374 /* Information to set up IMC between decoder and encoder */
375 struct imc_dec_enc_info imc_info;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -0700376 /* Flag to indicate whether ABR is enabled */
377 bool is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800378} __attribute__ ((packed));
379
380/* Structure to send configuration for decoder introduced
381 * on AFE Tx path for ABR link quality feedback to BT encoder.
382 */
383struct abr_dec_cfg_t {
384 /* Decoder media format */
385 uint32_t dec_format;
386 /* Information to set up IMC between decoder and encoder */
387 struct imc_dec_enc_info imc_info;
388} __attribute__ ((packed));
389
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530390/* START of DSP configurable structures
391 * These values should match with DSP interface defintion
392 */
393
394/* AAC encoder configuration structure. */
395typedef struct aac_enc_cfg_t aac_enc_cfg_t;
396
397/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
398 * supported aac_fmt_flag are ADTS/RAW
399 * supported channel_cfg are Native mode, Mono , Stereo
400 */
401struct aac_enc_cfg_t {
402 uint32_t enc_format;
403 uint32_t bit_rate;
404 uint32_t enc_mode;
405 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530406 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530407 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530408} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530409
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700410struct aac_enc_cfg_v2_t {
411 struct aac_enc_cfg_t aac_enc_cfg;
412 struct aac_frame_size_control_t frame_ctl;
413} __attribute__ ((packed));
414
Surendar Karkabbb3c822018-11-12 13:00:38 +0530415typedef struct audio_aac_decoder_config_t audio_aac_decoder_config_t;
416struct audio_aac_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200417 uint16_t aac_fmt_flag; /* LATM*/
418 uint16_t audio_object_type; /* LC */
419 uint16_t channels; /* Stereo */
420 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
421 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
422 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530423} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200424
Surendar Karkabbb3c822018-11-12 13:00:38 +0530425typedef struct audio_sbc_decoder_config_t audio_sbc_decoder_config_t;
426struct audio_sbc_decoder_config_t {
Florian Pfister1a84f312018-07-19 14:38:18 +0200427 uint16_t channels; /* Mono, Stereo */
428 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
429 44.1k, 48k, 64k, 88.2k, 96k */
Surendar Karkabbb3c822018-11-12 13:00:38 +0530430} __attribute__ ((packed));
Florian Pfister1a84f312018-07-19 14:38:18 +0200431
432/* AAC decoder configuration structure. */
433typedef struct aac_dec_cfg_t aac_dec_cfg_t;
434struct aac_dec_cfg_t {
435 uint32_t dec_format;
436 audio_aac_decoder_config_t data;
437} __attribute__ ((packed));
438
439/* SBC decoder configuration structure. */
440typedef struct sbc_dec_cfg_t sbc_dec_cfg_t;
441struct sbc_dec_cfg_t {
442 uint32_t dec_format;
443 audio_sbc_decoder_config_t data;
444} __attribute__ ((packed));
445
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530446/* SBC encoder configuration structure. */
447typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
448
449/* supported num_subbands are 4/8
450 * supported blk_len are 4, 8, 12, 16
451 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
452 * supported alloc_method are LOUNDNESS/SNR
453 * supported bit_rate for mono channel is max 320kbps
454 * supported bit rate for stereo channel is max 512 kbps
455 */
456struct sbc_enc_cfg_t{
457 uint32_t enc_format;
458 uint32_t num_subbands;
459 uint32_t blk_len;
460 uint32_t channel_mode;
461 uint32_t alloc_method;
462 uint32_t bit_rate;
463 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530464} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530465
466
467/* supported num_channels are Mono/Stereo
468 * supported channel_mapping for mono is CHANNEL_C
469 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
470 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700471 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530472struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530473{
474 uint32_t enc_format;
475 uint32_t sample_rate;
476 uint16_t num_channels;
477 uint16_t reserved;
478 uint8_t channel_mapping[8];
479 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530480} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530481
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530482struct celt_specific_enc_cfg_t
483{
484 uint32_t bit_rate;
485 uint16_t frame_size;
486 uint16_t complexity;
487 uint16_t prediction_mode;
488 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530489} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530490
491struct celt_enc_cfg_t
492{
493 struct custom_enc_cfg_t custom_cfg;
494 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530495} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700496
497/* sync_mode introduced with APTX V2 libraries
498 * sync mode: 0x0 = stereo sync mode
499 * 0x01 = dual mono sync mode
500 * 0x02 = dual mono with no sync on either L or R codewords
501 */
502struct aptx_v2_enc_cfg_ext_t
503{
504 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530505} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700506
507/* APTX struct for combining custom enc and V2 fields */
508struct aptx_enc_cfg_t
509{
510 struct custom_enc_cfg_t custom_cfg;
511 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530512} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700513
Sharad Sangle95d451b2018-06-19 12:24:20 +0530514/* APTX AD structure */
515struct aptx_ad_enc_cfg_ext_t
516{
517 uint32_t sampling_freq;
518 uint32_t mtu;
519 uint32_t channel_mode;
520 uint32_t min_sink_modeA;
521 uint32_t max_sink_modeA;
522 uint32_t min_sink_modeB;
523 uint32_t max_sink_modeB;
524 uint32_t min_sink_modeC;
525 uint32_t max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530526 uint32_t mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530527} __attribute__ ((packed));
528
529struct aptx_ad_enc_cfg_t
530{
531 struct custom_enc_cfg_t custom_cfg;
532 struct aptx_ad_enc_cfg_ext_t aptx_ad_cfg;
533 struct abr_enc_cfg_t abr_cfg;
534} __attribute__ ((packed));
535
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530536struct ldac_specific_enc_cfg_t
537{
538 uint32_t bit_rate;
539 uint16_t channel_mode;
540 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530541} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530542
543struct ldac_enc_cfg_t
544{
545 struct custom_enc_cfg_t custom_cfg;
546 struct ldac_specific_enc_cfg_t ldac_cfg;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800547 struct abr_enc_cfg_t abr_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530548} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530549
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530550/* In LE BT source code uses system/audio.h for below
551 * structure definition. To avoid multiple definition
552 * compilation error for audiohal in LE , masking structure
553 * definition under "LINUX_ENABLED" which is defined only
554 * in LE
555 */
556#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530557/* TODO: Define the following structures only for O using PLATFORM_VERSION */
558/* Information about BT SBC encoder configuration
559 * This data is used between audio HAL module and
560 * BT IPC library to configure DSP encoder
561 */
562typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530563 uint32_t subband; /* 4, 8 */
564 uint32_t blk_len; /* 4, 8, 12, 16 */
565 uint16_t sampling_rate; /*44.1khz,48khz*/
566 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
567 uint8_t alloc; /*0(Loudness),1(SNR)*/
568 uint8_t min_bitpool; /* 2 */
569 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
570 uint32_t bitrate; /* 320kbps to 512kbps */
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530571 uint32_t bits_per_sample;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530572} audio_sbc_encoder_config;
573
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530574/* Information about BT APTX encoder configuration
575 * This data is used between audio HAL module and
576 * BT IPC library to configure DSP encoder
577 */
578typedef struct {
579 uint16_t sampling_rate;
580 uint8_t channels;
581 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530582 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700583} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530584
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700585typedef struct {
Sharad Sangle95d451b2018-06-19 12:24:20 +0530586 uint8_t sampling_rate;
587 uint8_t channel_mode;
588 uint16_t mtu;
589 uint8_t min_sink_modeA;
590 uint8_t max_sink_modeA;
591 uint8_t min_sink_modeB;
592 uint8_t max_sink_modeB;
593 uint8_t min_sink_modeC;
594 uint8_t max_sink_modeC;
595 uint8_t TTP_modeA_low;
596 uint8_t TTP_modeA_high;
597 uint8_t TTP_modeB_low;
598 uint8_t TTP_modeB_high;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530599 uint32_t bits_per_sample;
Sharad Sanglee378afe2018-09-03 20:04:17 +0530600 uint16_t encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530601} audio_aptx_ad_config;
602
603typedef struct {
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700604 uint16_t sampling_rate;
605 uint8_t channels;
606 uint32_t bitrate;
607 uint32_t sync_mode;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530608 uint32_t bits_per_sample;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700609} audio_aptx_dual_mono_config;
610
611typedef union {
612 audio_aptx_default_config *default_cfg;
613 audio_aptx_dual_mono_config *dual_mono_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +0530614 audio_aptx_ad_config *ad_cfg;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700615} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530616
617/* Information about BT AAC encoder configuration
618 * This data is used between audio HAL module and
619 * BT IPC library to configure DSP encoder
620 */
621typedef struct {
622 uint32_t enc_mode; /* LC, SBR, PS */
623 uint16_t format_flag; /* RAW, ADTS */
624 uint16_t channels; /* 1-Mono, 2-Stereo */
625 uint32_t sampling_rate;
626 uint32_t bitrate;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530627 uint32_t bits_per_sample;
Aalique Grahame22e49102018-12-18 14:23:57 -0800628 struct aac_frame_size_control_t frame_ctl;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530629} audio_aac_encoder_config;
Ramu Gottipati08d82e72018-12-17 11:52:14 +0530630#endif
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -0700631
632typedef struct {
633 audio_aac_encoder_config audio_aac_enc_cfg;
634 struct aac_frame_size_control_t frame_ctl;
635} audio_aac_encoder_config_v2;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530636
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530637/* Information about BT CELT encoder configuration
638 * This data is used between audio HAL module and
639 * BT IPC library to configure DSP encoder
640 */
641typedef struct {
642 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
643 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
644 uint16_t frame_size; /* 64-128-256-512, 512 */
645 uint16_t complexity; /* 0-10, 1 */
646 uint16_t prediction_mode; /* 0-1-2, 0 */
647 uint16_t vbr_flag; /* 0-1, 0*/
648 uint32_t bitrate; /*32000 - 1536000, 139500*/
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530649 uint32_t bits_per_sample;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530650} audio_celt_encoder_config;
651
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530652/* Information about BT LDAC encoder configuration
653 * This data is used between audio HAL module and
654 * BT IPC library to configure DSP encoder
655 */
656typedef struct {
657 uint32_t sampling_rate; /*44100,48000,88200,96000*/
658 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
659 uint16_t channel_mode; /* 0, 4, 2, 1*/
660 uint16_t mtu; /*679*/
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800661 bool is_abr_enabled;
662 struct quality_level_to_bitrate_info level_to_bitrate_map;
Samyak Jain2cddc0e2018-07-18 15:22:27 +0530663 uint32_t bits_per_sample;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530664} audio_ldac_encoder_config;
665
Surendar Karkabbb3c822018-11-12 13:00:38 +0530666/* Information about BT AAC decoder configuration
667 * This data is used between audio HAL module and
668 * BT IPC library to configure DSP decoder
669 */
670typedef struct {
671 uint16_t aac_fmt_flag; /* LATM*/
672 uint16_t audio_object_type; /* LC */
673 uint16_t channels; /* Stereo */
674 uint16_t total_size_of_pce_bits; /* 0 - only for channel conf PCE */
675 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
676 44.1k, 48k, 64k, 88.2k, 96k */
677} audio_aac_dec_config_t;
678
679/* Information about BT SBC decoder configuration
680 * This data is used between audio HAL module and
681 * BT IPC library to configure DSP decoder
682 */
683typedef struct {
684 uint16_t channels; /* Mono, Stereo */
685 uint32_t sampling_rate; /* 8k, 11.025k, 12k, 16k, 22.05k, 24k, 32k,
686 44.1k, 48k, 64k, 88.2k, 96k */
687}audio_sbc_dec_config_t;
688
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530689/*********** END of DSP configurable structures ********************/
690
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530691static void update_offload_codec_capabilities()
692{
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530693
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530694 a2dp.is_a2dp_offload_supported =
Aalique Grahame22e49102018-12-18 14:23:57 -0800695 property_get_bool(SYSPROP_A2DP_OFFLOAD_SUPPORTED, false) &&
696 !property_get_bool(SYSPROP_A2DP_OFFLOAD_DISABLED, false);
697
698 ALOGD("%s: A2DP offload supported = %d",__func__,
699 a2dp.is_a2dp_offload_supported);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530700}
701
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800702static int stop_abr()
703{
704 struct mixer_ctl *ctl_abr_tx_path = NULL;
705 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
706
707 /* This function can be used if !abr_started for clean up */
708 ALOGV("%s: enter", __func__);
709
710 // Close hostless front end
711 if (a2dp.abr_config.abr_tx_handle != NULL) {
712 pcm_close(a2dp.abr_config.abr_tx_handle);
713 a2dp.abr_config.abr_tx_handle = NULL;
714 }
715 a2dp.abr_config.abr_started = false;
716 a2dp.abr_config.imc_instance = 0;
717
718 // Reset BT driver mixer control for ABR usecase
719 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
720 MIXER_SET_FEEDBACK_CHANNEL);
721 if (!ctl_set_bt_feedback_channel) {
722 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
723 return -ENOSYS;
724 }
725 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 0) != 0) {
726 ALOGE("%s: Failed to set BT usecase", __func__);
727 return -ENOSYS;
728 }
729
730 // Reset ABR Tx feedback path
731 ALOGV("%s: Disable ABR Tx feedback path", __func__);
732 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
733 MIXER_ABR_TX_FEEDBACK_PATH);
734 if (!ctl_abr_tx_path) {
735 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
736 return -ENOSYS;
737 }
738 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 0) != 0) {
739 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
740 return -ENOSYS;
741 }
742
743 return 0;
744}
745
746static int start_abr()
747{
748 struct mixer_ctl *ctl_abr_tx_path = NULL;
749 struct mixer_ctl *ctl_set_bt_feedback_channel = NULL;
750 int abr_device_id;
751 int ret = 0;
752
753 if (!a2dp.abr_config.is_abr_enabled) {
754 ALOGE("%s: Cannot start if ABR is not enabled", __func__);
755 return -ENOSYS;
756 }
757
758 if (a2dp.abr_config.abr_started) {
759 ALOGI("%s: ABR has already started", __func__);
760 return ret;
761 }
762
763 // Enable Slimbus 7 Tx feedback path
764 ALOGV("%s: Enable ABR Tx feedback path", __func__);
765 ctl_abr_tx_path = mixer_get_ctl_by_name(a2dp.adev->mixer,
766 MIXER_ABR_TX_FEEDBACK_PATH);
767 if (!ctl_abr_tx_path) {
768 ALOGE("%s: ERROR ABR Tx feedback path mixer control not identifed", __func__);
769 return -ENOSYS;
770 }
771 if (mixer_ctl_set_value(ctl_abr_tx_path, 0, 1) != 0) {
772 ALOGE("%s: Failed to set ABR Tx feedback path", __func__);
773 return -ENOSYS;
774 }
775
776 // Notify ABR usecase information to BT driver to distinguish
777 // between SCO and feedback usecase
778 ctl_set_bt_feedback_channel = mixer_get_ctl_by_name(a2dp.adev->mixer,
779 MIXER_SET_FEEDBACK_CHANNEL);
780 if (!ctl_set_bt_feedback_channel) {
781 ALOGE("%s: ERROR Set usecase mixer control not identifed", __func__);
782 return -ENOSYS;
783 }
784 if (mixer_ctl_set_value(ctl_set_bt_feedback_channel, 0, 1) != 0) {
785 ALOGE("%s: Failed to set BT usecase", __func__);
786 return -ENOSYS;
787 }
788
789 // Open hostless front end and prepare ABR Tx path
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800790 abr_device_id = fp_platform_get_pcm_device_id(USECASE_AUDIO_A2DP_ABR_FEEDBACK,
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800791 PCM_CAPTURE);
792 if (!a2dp.abr_config.abr_tx_handle) {
793 a2dp.abr_config.abr_tx_handle = pcm_open(a2dp.adev->snd_card,
794 abr_device_id, PCM_IN,
795 &pcm_config_abr);
796 if (a2dp.abr_config.abr_tx_handle == NULL ||
797 !pcm_is_ready(a2dp.abr_config.abr_tx_handle))
798 goto fail;
799 }
800 ret = pcm_start(a2dp.abr_config.abr_tx_handle);
801 if (ret < 0)
802 goto fail;
803 a2dp.abr_config.abr_started = true;
804
805 return ret;
806
807fail:
808 ALOGE("%s: %s", __func__, pcm_get_error(a2dp.abr_config.abr_tx_handle));
809 stop_abr();
810 return -ENOSYS;
811}
812
Florian Pfister1a84f312018-07-19 14:38:18 +0200813/* API to open BT IPC library to start IPC communication for BT Source*/
814static void open_a2dp_source()
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530815{
816 int ret = 0;
817
Florian Pfister1a84f312018-07-19 14:38:18 +0200818 ALOGD(" Open A2DP source start ");
Srinu Jellada99a592019-01-25 16:50:52 +0530819 if (a2dp.bt_lib_source_handle == NULL) {
820 if(!isRunningWithVendorEnhancedFramework()) {
821 ALOGD(" Requesting for BT lib handle");
822 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME, RTLD_NOW);
823 if (a2dp.bt_lib_source_handle == NULL) {
824 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME);
825 ret = -ENOSYS;
826 goto init_fail;
827 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530828 } else {
Srinu Jellada99a592019-01-25 16:50:52 +0530829 ALOGD(" Requesting for BT QTI lib handle");
830 a2dp.bt_lib_source_handle = dlopen(BT_IPC_SOURCE_LIB_NAME_QTI, RTLD_NOW);
831 if (a2dp.bt_lib_source_handle == NULL) {
832 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SOURCE_LIB_NAME_QTI);
833 ret = -ENOSYS;
834 goto init_fail;
835 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530836 }
837 }
838
Srinu Jellada99a592019-01-25 16:50:52 +0530839 a2dp.audio_source_open = (audio_source_open_t)
840 dlsym(a2dp.bt_lib_source_handle, "audio_stream_open");
841 a2dp.audio_source_start = (audio_source_start_t)
842 dlsym(a2dp.bt_lib_source_handle, "audio_start_stream");
843 a2dp.audio_get_enc_config = (audio_get_enc_config_t)
844 dlsym(a2dp.bt_lib_source_handle, "audio_get_codec_config");
845 a2dp.audio_source_suspend = (audio_source_suspend_t)
846 dlsym(a2dp.bt_lib_source_handle, "audio_suspend_stream");
847 a2dp.audio_source_handoff_triggered = (audio_source_handoff_triggered_t)
848 dlsym(a2dp.bt_lib_source_handle, "audio_handoff_triggered");
849 a2dp.clear_source_a2dpsuspend_flag = (clear_source_a2dpsuspend_flag_t)
850 dlsym(a2dp.bt_lib_source_handle, "clear_a2dpsuspend_flag");
851 a2dp.audio_source_stop = (audio_source_stop_t)
852 dlsym(a2dp.bt_lib_source_handle, "audio_stop_stream");
853 a2dp.audio_source_close = (audio_source_close_t)
854 dlsym(a2dp.bt_lib_source_handle, "audio_stream_close");
855 a2dp.audio_source_check_a2dp_ready = (audio_source_check_a2dp_ready_t)
856 dlsym(a2dp.bt_lib_source_handle,"audio_check_a2dp_ready");
857 a2dp.audio_sink_get_a2dp_latency = (audio_sink_get_a2dp_latency_t)
858 dlsym(a2dp.bt_lib_source_handle,"audio_sink_get_a2dp_latency");
859 a2dp.audio_is_source_scrambling_enabled = (audio_is_source_scrambling_enabled_t)
860 dlsym(a2dp.bt_lib_source_handle,"audio_is_scrambling_enabled");
861 a2dp.audio_is_tws_mono_mode_enable = (audio_is_tws_mono_mode_enable_t)
862 dlsym(a2dp.bt_lib_source_handle,"isTwsMonomodeEnable");
863
Florian Pfister1a84f312018-07-19 14:38:18 +0200864 if (a2dp.bt_lib_source_handle && a2dp.audio_source_open) {
865 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530866 ALOGD("calling BT stream open");
Florian Pfister1a84f312018-07-19 14:38:18 +0200867 ret = a2dp.audio_source_open();
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530868 if(ret != 0) {
Florian Pfister1a84f312018-07-19 14:38:18 +0200869 ALOGE("Failed to open source stream for a2dp: status %d", ret);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530870 goto init_fail;
871 }
Florian Pfister1a84f312018-07-19 14:38:18 +0200872 a2dp.bt_state_source = A2DP_STATE_CONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530873 } else {
Florian Pfister1a84f312018-07-19 14:38:18 +0200874 ALOGD("Called a2dp open with improper state, Ignoring request state %d", a2dp.bt_state_source);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530875 }
876 } else {
877 ALOGE("a2dp handle is not identified, Ignoring open request");
Florian Pfister1a84f312018-07-19 14:38:18 +0200878 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530879 goto init_fail;
880 }
881
882init_fail:
Florian Pfister1a84f312018-07-19 14:38:18 +0200883 if(ret != 0 && (a2dp.bt_lib_source_handle != NULL)) {
884 dlclose(a2dp.bt_lib_source_handle);
885 a2dp.bt_lib_source_handle = NULL;
886 }
887}
888
889/* API to open BT IPC library to start IPC communication for BT Sink*/
890static void open_a2dp_sink()
891{
892 ALOGD(" Open A2DP input start ");
893 if (a2dp.bt_lib_sink_handle == NULL){
894 ALOGD(" Requesting for BT lib handle");
895 a2dp.bt_lib_sink_handle = dlopen(BT_IPC_SINK_LIB_NAME, RTLD_NOW);
896
897 if (a2dp.bt_lib_sink_handle == NULL) {
898 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_SINK_LIB_NAME);
899 } else {
900 a2dp.audio_sink_start = (audio_sink_start_t)
901 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_start_capture");
902 a2dp.audio_get_dec_config = (audio_get_dec_config_t)
903 dlsym(a2dp.bt_lib_sink_handle, "audio_get_decoder_config");
904 a2dp.audio_sink_stop = (audio_sink_stop_t)
905 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_stop_capture");
906 a2dp.audio_sink_check_a2dp_ready = (audio_sink_check_a2dp_ready_t)
907 dlsym(a2dp.bt_lib_sink_handle,"audio_sink_check_a2dp_ready");
908 a2dp.audio_sink_session_setup_complete = (audio_sink_session_setup_complete_t)
909 dlsym(a2dp.bt_lib_sink_handle, "audio_sink_session_setup_complete");
910 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530911 }
912}
913
914static int close_a2dp_output()
915{
916 ALOGV("%s\n",__func__);
Florian Pfister1a84f312018-07-19 14:38:18 +0200917
918 if (!(a2dp.bt_lib_source_handle && a2dp.audio_source_close)) {
919 ALOGE("a2dp source handle is not identified, Ignoring close request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530920 return -ENOSYS;
921 }
Florian Pfister1a84f312018-07-19 14:38:18 +0200922
923 if (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
924 ALOGD("calling BT source stream close");
925 if(a2dp.audio_source_close() == false)
926 ALOGE("failed close a2dp source control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530927 }
Florian Pfister1a84f312018-07-19 14:38:18 +0200928 a2dp.a2dp_source_started = false;
929 a2dp.a2dp_source_total_active_session_requests = 0;
930 a2dp.a2dp_source_suspended = false;
931 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530932 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530933 a2dp.enc_channels = 2;
Florian Pfister1a84f312018-07-19 14:38:18 +0200934 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -0800935 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
936 stop_abr();
937 a2dp.abr_config.is_abr_enabled = false;
938 a2dp.abr_config.abr_started = false;
939 a2dp.abr_config.imc_instance = 0;
940 a2dp.abr_config.abr_tx_handle = NULL;
Surendar Karka2febd452018-12-13 17:56:43 +0530941 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
Florian Pfister1a84f312018-07-19 14:38:18 +0200942
943 return 0;
944}
945
946static int close_a2dp_input()
947{
948 ALOGV("%s\n",__func__);
949
950 if (!(a2dp.bt_lib_sink_handle && a2dp.audio_source_close)) {
951 ALOGE("a2dp sink handle is not identified, Ignoring close request");
952 return -ENOSYS;
953 }
954
955 if (a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) {
956 ALOGD("calling BT sink stream close");
957 if(a2dp.audio_source_close() == false)
958 ALOGE("failed close a2dp sink control path from BT library");
959 }
960 a2dp.a2dp_sink_started = false;
961 a2dp.a2dp_sink_total_active_session_requests = 0;
962 a2dp.bt_decoder_format = CODEC_TYPE_INVALID;
963 a2dp.dec_sampling_rate = 48000;
964 a2dp.dec_channels = 2;
965 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530966
967 return 0;
968}
969
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530970static void a2dp_check_and_set_scrambler()
971{
972 bool scrambler_mode = false;
973 struct mixer_ctl *ctrl_scrambler_mode = NULL;
Florian Pfister1a84f312018-07-19 14:38:18 +0200974 if (a2dp.audio_is_source_scrambling_enabled && (a2dp.bt_state_source != A2DP_STATE_DISCONNECTED))
975 scrambler_mode = a2dp.audio_is_source_scrambling_enabled();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530976
977 if (scrambler_mode) {
978 //enable scrambler in dsp
979 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
980 MIXER_SCRAMBLER_MODE);
981 if (!ctrl_scrambler_mode) {
Florian Pfister1a84f312018-07-19 14:38:18 +0200982 ALOGE(" ERROR scrambler mode mixer control not identified");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530983 return;
984 } else {
985 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
986 ALOGE("%s: Could not set scrambler mode", __func__);
987 return;
988 }
989 }
990 }
991}
992
Florian Pfister1a84f312018-07-19 14:38:18 +0200993static bool a2dp_set_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530994{
Florian Pfister1a84f312018-07-19 14:38:18 +0200995 char *rate_str = NULL, *channels = NULL;
996 uint32_t sampling_rate;
997 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_channels = NULL;
998 bool is_configured = false;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530999
Florian Pfister1a84f312018-07-19 14:38:18 +02001000 if (direction == SINK) {
1001 sampling_rate = a2dp.dec_sampling_rate;
1002 } else {
1003 sampling_rate = a2dp.enc_sampling_rate;
1004 }
1005 //For LDAC encoder and AAC decoder open slimbus port at
1006 //96Khz for 48Khz input and 88.2Khz for 44.1Khz input.
1007 if (((a2dp.bt_encoder_format == CODEC_TYPE_LDAC) ||
Surendar Karkabbb3c822018-11-12 13:00:38 +05301008 (a2dp.bt_decoder_format == CODEC_TYPE_SBC) ||
Florian Pfister1a84f312018-07-19 14:38:18 +02001009 (a2dp.bt_decoder_format == AUDIO_FORMAT_AAC)) &&
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301010 (sampling_rate == 48000 || sampling_rate == 44100 )) {
1011 sampling_rate = sampling_rate *2;
1012 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001013
Aalique Grahame22e49102018-12-18 14:23:57 -08001014 // No need to configure backend for PCM format.
1015 if (a2dp.bt_encoder_format == CODEC_TYPE_PCM) {
1016 return 0;
1017 }
1018
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301019 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301020 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301021 case 44100:
1022 rate_str = "KHZ_44P1";
1023 break;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301024 case 88200:
1025 rate_str = "KHZ_88P2";
1026 break;
1027 case 96000:
1028 rate_str = "KHZ_96";
1029 break;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001030 case 48000:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301031 default:
1032 rate_str = "KHZ_48";
1033 break;
1034 }
1035
Florian Pfister1a84f312018-07-19 14:38:18 +02001036 if (direction == SINK) {
1037 ALOGD("%s: set sink backend sample rate =%s", __func__, rate_str);
1038 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301039 MIXER_SINK_SAMPLE_RATE);
Florian Pfister1a84f312018-07-19 14:38:18 +02001040 } else {
1041 ALOGD("%s: set source backend sample rate =%s", __func__, rate_str);
1042 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301043 MIXER_SAMPLE_RATE_RX);
Florian Pfister1a84f312018-07-19 14:38:18 +02001044 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301045 if (ctl_sample_rate) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001046
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301047 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1048 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Surendar Karka2febd452018-12-13 17:56:43 +05301049 is_configured = false;
1050 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301051 }
1052
Surendar Karka2febd452018-12-13 17:56:43 +05301053 if (direction == SOURCE) {
1054 /* Set Tx backend sample rate */
1055 if (a2dp.abr_config.is_abr_enabled)
1056 rate_str = ABR_TX_SAMPLE_RATE;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001057
Surendar Karka2febd452018-12-13 17:56:43 +05301058 ALOGD("%s: set backend tx sample rate = %s", __func__, rate_str);
1059 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1060 MIXER_SOURCE_SAMPLE_RATE_TX);
1061 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301062 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301063 is_configured = false;
1064 goto fail;
1065 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301066
Surendar Karka2febd452018-12-13 17:56:43 +05301067 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1068 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
1069 is_configured = false;
1070 goto fail;
1071 }
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301072 }
1073 } else {
1074 /* Fallback to legacy approch if MIXER_SAMPLE_RATE_RX and
1075 MIXER_SAMPLE_RATE_TX is not supported */
1076 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1077 MIXER_SAMPLE_RATE_DEFAULT);
1078 if (!ctl_sample_rate) {
1079 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
Surendar Karka2febd452018-12-13 17:56:43 +05301080 is_configured = false;
1081 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301082 }
1083
1084 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1085 ALOGE("%s: Failed to set backend sample rate = %s", __func__, rate_str);
Florian Pfister1a84f312018-07-19 14:38:18 +02001086 is_configured = false;
1087 goto fail;
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301088 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301089 }
1090
Florian Pfister1a84f312018-07-19 14:38:18 +02001091 if (direction == SINK) {
1092 switch (a2dp.dec_channels) {
1093 case 1:
1094 channels = "One";
1095 break;
1096 case 2:
1097 default:
1098 channels = "Two";
1099 break;
1100 }
1101
Ramu Gottipati02809682018-12-19 16:46:12 +05301102 ALOGD("%s: set afe dec channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001103 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1104 MIXER_AFE_SINK_CHANNELS);
1105 } else {
1106 //Configure AFE enc channels
1107 switch (a2dp.enc_channels) {
1108 case 1:
1109 channels = "One";
1110 break;
1111 case 2:
1112 default:
1113 channels = "Two";
1114 break;
1115 }
1116
Ramu Gottipati02809682018-12-19 16:46:12 +05301117 ALOGD("%s: set afe enc channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001118 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1119 MIXER_AFE_IN_CHANNELS);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301120 }
1121
Florian Pfister1a84f312018-07-19 14:38:18 +02001122 if (!ctrl_channels) {
1123 ALOGE(" ERROR AFE channels mixer control not identified");
1124 } else {
1125 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Ramu Gottipati02809682018-12-19 16:46:12 +05301126 ALOGE("%s: Failed to set AFE channels =%s", __func__, channels);
Florian Pfister1a84f312018-07-19 14:38:18 +02001127 is_configured = false;
1128 goto fail;
1129 }
1130 }
1131 is_configured = true;
1132fail:
1133 return is_configured;
1134}
1135
Surendar Karkabbb3c822018-11-12 13:00:38 +05301136bool configure_aac_dec_format(audio_aac_dec_config_t *aac_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001137{
1138 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1139 struct aac_dec_cfg_t aac_dsp_cfg;
1140 bool is_configured = false;
1141 int ret = 0;
1142
1143 if(aac_bt_cfg == NULL)
1144 return false;
1145
Surendar Karka2febd452018-12-13 17:56:43 +05301146 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001147 if (!ctl_dec_data) {
1148 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1149 is_configured = false;
1150 goto fail;
1151 }
1152
1153 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_dec_cfg_t));
1154 aac_dsp_cfg.dec_format = MEDIA_FMT_AAC;
1155 aac_dsp_cfg.data.aac_fmt_flag = aac_bt_cfg->aac_fmt_flag;
1156 aac_dsp_cfg.data.channels = aac_bt_cfg->channels;
1157 switch(aac_bt_cfg->audio_object_type) {
1158 case 0:
1159 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_LC;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301160 break;
1161 case 2:
Florian Pfister1a84f312018-07-19 14:38:18 +02001162 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_PS;
1163 break;
1164 case 1:
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301165 default:
Florian Pfister1a84f312018-07-19 14:38:18 +02001166 aac_dsp_cfg.data.audio_object_type = MEDIA_FMT_AAC_AOT_SBR;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301167 break;
1168 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001169 aac_dsp_cfg.data.total_size_of_pce_bits = aac_bt_cfg->total_size_of_pce_bits;
1170 aac_dsp_cfg.data.sampling_rate = aac_bt_cfg->sampling_rate;
1171 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&aac_dsp_cfg,
1172 sizeof(struct aac_dec_cfg_t));
1173 if (ret != 0) {
1174 ALOGE("%s: failed to set AAC decoder config", __func__);
1175 is_configured = false;
1176 goto fail;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001177 }
1178
Florian Pfister1a84f312018-07-19 14:38:18 +02001179 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1180 MIXER_DEC_BIT_FORMAT);
1181 if (!ctrl_bit_format) {
1182 ALOGE(" ERROR Dec bit format mixer control not identified");
1183 is_configured = false;
1184 goto fail;
1185 }
1186 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1187 if (ret != 0) {
1188 ALOGE("%s: Failed to set bit format to decoder", __func__);
1189 is_configured = false;
1190 goto fail;
1191 }
1192
1193 is_configured = true;
1194 a2dp.bt_decoder_format = CODEC_TYPE_AAC;
1195 a2dp.dec_channels = aac_dsp_cfg.data.channels;
1196 a2dp.dec_sampling_rate = aac_dsp_cfg.data.sampling_rate;
1197 ALOGV("Successfully updated AAC dec format with sampling_rate: %d channels:%d",
1198 aac_dsp_cfg.data.sampling_rate, aac_dsp_cfg.data.channels);
1199fail:
1200 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301201}
1202
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301203static int a2dp_set_bit_format(uint32_t enc_bit_format)
1204{
1205 const char *bit_format = NULL;
1206 struct mixer_ctl *ctrl_bit_format = NULL;
1207
1208 // Configure AFE Input Bit Format
1209 switch (enc_bit_format) {
1210 case 32:
1211 bit_format = "S32_LE";
1212 break;
1213 case 24:
1214 bit_format = "S24_LE";
1215 break;
1216 case 16:
1217 default:
1218 bit_format = "S16_LE";
1219 break;
1220 }
1221
1222 ALOGD("%s: set AFE input bit format = %d", __func__, enc_bit_format);
1223 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1224 MIXER_ENC_BIT_FORMAT);
1225 if (!ctrl_bit_format) {
1226 ALOGE("%s: ERROR AFE input bit format mixer control not identifed", __func__);
1227 return -ENOSYS;
1228 }
1229 if (mixer_ctl_set_enum_by_string(ctrl_bit_format, bit_format) != 0) {
1230 ALOGE("%s: Failed to set AFE input bit format = %d", __func__, enc_bit_format);
1231 return -ENOSYS;
1232 }
1233 return 0;
1234}
1235
Surendar Karka2febd452018-12-13 17:56:43 +05301236static int a2dp_reset_backend_cfg(uint8_t direction)
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301237{
Surendar Karka2febd452018-12-13 17:56:43 +05301238 const char *rate_str = "KHZ_8", *channels = "Zero";
1239 struct mixer_ctl *ctl_sample_rate = NULL, *ctl_sample_rate_tx = NULL;
1240 struct mixer_ctl *ctrl_channels = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301241
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001242 // Reset backend sampling rate
Surendar Karka2febd452018-12-13 17:56:43 +05301243 if (direction == SINK) {
1244 ALOGD("%s: reset sink backend sample rate =%s", __func__, rate_str);
1245 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1246 MIXER_SINK_SAMPLE_RATE);
1247 } else {
1248 ALOGD("%s: reset source backend sample rate =%s", __func__, rate_str);
1249 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
1250 MIXER_SAMPLE_RATE_RX);
1251 }
1252 if (ctl_sample_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301253
Surendar Karka2febd452018-12-13 17:56:43 +05301254 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
1255 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301256 return -ENOSYS;
1257 }
1258
1259 ctl_sample_rate_tx = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05301260 MIXER_SOURCE_SAMPLE_RATE_TX);
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301261 if (!ctl_sample_rate_tx) {
1262 ALOGE("%s: ERROR Tx backend sample rate mixer control not identifed", __func__);
1263 return -ENOSYS;
1264 }
1265
1266 if (mixer_ctl_set_enum_by_string(ctl_sample_rate_tx, rate_str) != 0) {
1267 ALOGE("%s: Failed to reset Tx backend sample rate = %s", __func__, rate_str);
1268 return -ENOSYS;
1269 }
1270 } else {
1271
Surendar Karka2febd452018-12-13 17:56:43 +05301272 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301273 MIXER_SAMPLE_RATE_DEFAULT);
Surendar Karka2febd452018-12-13 17:56:43 +05301274 if (!ctl_sample_rate) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301275 ALOGE("%s: ERROR backend sample rate mixer control not identifed", __func__);
1276 return -ENOSYS;
1277 }
1278
Surendar Karka2febd452018-12-13 17:56:43 +05301279 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
Samyak Jainf69e9ef2018-06-12 12:26:37 +05301280 ALOGE("%s: Failed to reset backend sample rate = %s", __func__, rate_str);
1281 return -ENOSYS;
1282 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001283 }
1284
1285 // Reset AFE input channels
Surendar Karka2febd452018-12-13 17:56:43 +05301286 if (direction == SINK) {
1287 ALOGD("%s: reset afe sink channels =%s", __func__, channels);
1288 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1289 MIXER_AFE_SINK_CHANNELS);
1290 } else {
1291 ALOGD("%s: reset afe source channels =%s", __func__, channels);
1292 ctrl_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
1293 MIXER_AFE_IN_CHANNELS);
1294 }
1295 if (!ctrl_channels) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001296 ALOGE("%s: ERROR AFE input channels mixer control not identifed", __func__);
1297 return -ENOSYS;
1298 }
Surendar Karka2febd452018-12-13 17:56:43 +05301299 if (mixer_ctl_set_enum_by_string(ctrl_channels, channels) != 0) {
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001300 ALOGE("%s: Failed to reset AFE in channels = %d", __func__, a2dp.enc_channels);
1301 return -ENOSYS;
1302 }
1303
1304 return 0;
1305}
1306
1307/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301308static bool configure_a2dp_source_decoder_format(int dec_format)
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001309{
1310 struct mixer_ctl *ctl_dec_data = NULL;
1311 struct abr_dec_cfg_t dec_cfg;
1312 int ret = 0;
1313
1314 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05301315 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001316 if (!ctl_dec_data) {
1317 ALOGE("%s: ERROR A2DP codec config data mixer control not identifed", __func__);
1318 return false;
1319 }
1320 memset(&dec_cfg, 0x0, sizeof(dec_cfg));
1321 dec_cfg.dec_format = dec_format;
1322 dec_cfg.imc_info.direction = IMC_TRANSMIT;
1323 dec_cfg.imc_info.enable = IMC_ENABLE;
1324 dec_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1325 dec_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1326
1327 ret = mixer_ctl_set_array(ctl_dec_data, &dec_cfg,
1328 sizeof(dec_cfg));
1329 if (ret != 0) {
1330 ALOGE("%s: Failed to set decoder config", __func__);
1331 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301332 }
Surendar Karka2febd452018-12-13 17:56:43 +05301333 }
1334
1335 return true;
1336}
1337
Surendar Karkabbb3c822018-11-12 13:00:38 +05301338bool configure_sbc_dec_format(audio_sbc_dec_config_t *sbc_bt_cfg)
Florian Pfister1a84f312018-07-19 14:38:18 +02001339{
1340 struct mixer_ctl *ctl_dec_data = NULL, *ctrl_bit_format = NULL;
1341 struct sbc_dec_cfg_t sbc_dsp_cfg;
1342 bool is_configured = false;
1343 int ret = 0;
1344
1345 if(sbc_bt_cfg == NULL)
1346 goto fail;
1347
Surendar Karka2febd452018-12-13 17:56:43 +05301348 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001349 if (!ctl_dec_data) {
1350 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1351 is_configured = false;
1352 goto fail;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301353 }
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001354
Florian Pfister1a84f312018-07-19 14:38:18 +02001355 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_dec_cfg_t));
1356 sbc_dsp_cfg.dec_format = MEDIA_FMT_SBC;
1357 sbc_dsp_cfg.data.channels = sbc_bt_cfg->channels;
1358 sbc_dsp_cfg.data.sampling_rate = sbc_bt_cfg->sampling_rate;
1359 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&sbc_dsp_cfg,
1360 sizeof(struct sbc_dec_cfg_t));
Surendar Karkabbb3c822018-11-12 13:00:38 +05301361
Florian Pfister1a84f312018-07-19 14:38:18 +02001362 if (ret != 0) {
1363 ALOGE("%s: failed to set SBC decoder config", __func__);
1364 is_configured = false;
1365 goto fail;
1366 }
1367
1368 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1369 MIXER_DEC_BIT_FORMAT);
1370 if (!ctrl_bit_format) {
1371 ALOGE(" ERROR Dec bit format mixer control not identified");
1372 is_configured = false;
1373 goto fail;
1374 }
1375 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1376 if (ret != 0) {
1377 ALOGE("%s: Failed to set bit format to decoder", __func__);
1378 is_configured = false;
1379 goto fail;
1380 }
1381
1382 is_configured = true;
1383 a2dp.bt_decoder_format = CODEC_TYPE_SBC;
1384 if (sbc_dsp_cfg.data.channels == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1385 a2dp.dec_channels = 1;
1386 else
1387 a2dp.dec_channels = 2;
1388 a2dp.dec_sampling_rate = sbc_dsp_cfg.data.sampling_rate;
1389 ALOGV("Successfully updated SBC dec format");
1390fail:
1391 return is_configured;
1392}
1393
Florian Pfister1a84f312018-07-19 14:38:18 +02001394/* API to configure AFE decoder in DSP */
Surendar Karka2febd452018-12-13 17:56:43 +05301395static bool configure_a2dp_sink_decoder_format()
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301396{
Florian Pfister1a84f312018-07-19 14:38:18 +02001397 void *codec_info = NULL;
1398 codec_t codec_type = CODEC_TYPE_INVALID;
1399 bool is_configured = false;
1400 struct mixer_ctl *ctl_dec_data = NULL;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301401
Florian Pfister1a84f312018-07-19 14:38:18 +02001402 if (!a2dp.audio_get_dec_config) {
1403 ALOGE(" a2dp handle is not identified, ignoring a2dp decoder config");
1404 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301405 }
1406
Surendar Karka2febd452018-12-13 17:56:43 +05301407 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02001408 if (!ctl_dec_data) {
1409 ALOGE(" ERROR a2dp decoder CONFIG data mixer control not identified");
1410 is_configured = false;
1411 return false;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301412 }
Florian Pfister1a84f312018-07-19 14:38:18 +02001413 codec_info = a2dp.audio_get_dec_config(&codec_type);
1414 switch(codec_type) {
1415 case CODEC_TYPE_SBC:
1416 ALOGD(" SBC decoder supported BT device");
Surendar Karkabbb3c822018-11-12 13:00:38 +05301417 is_configured = configure_sbc_dec_format((audio_sbc_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001418 break;
1419 case CODEC_TYPE_AAC:
1420 ALOGD(" AAC decoder supported BT device");
1421 is_configured =
Surendar Karkabbb3c822018-11-12 13:00:38 +05301422 configure_aac_dec_format((audio_aac_dec_config_t *)codec_info);
Florian Pfister1a84f312018-07-19 14:38:18 +02001423 break;
1424 default:
1425 ALOGD(" Received Unsupported decoder format");
1426 is_configured = false;
1427 break;
1428 }
1429 return is_configured;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301430}
1431
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301432/* API to configure SBC DSP encoder */
1433bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
1434{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301435 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301436 struct sbc_enc_cfg_t sbc_dsp_cfg;
1437 bool is_configured = false;
1438 int ret = 0;
1439
1440 if(sbc_bt_cfg == NULL)
1441 return false;
1442
1443 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1444 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001445 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301446 is_configured = false;
1447 goto fail;
1448 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301449 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001450 sbc_dsp_cfg.enc_format = MEDIA_FMT_SBC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301451 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
1452 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
1453 switch(sbc_bt_cfg->channels) {
1454 case 0:
1455 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
1456 break;
1457 case 1:
1458 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
1459 break;
1460 case 3:
1461 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
1462 break;
1463 case 2:
1464 default:
1465 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
1466 break;
1467 }
1468 if (sbc_bt_cfg->alloc)
1469 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
1470 else
1471 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
1472 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
1473 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
1474 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
1475 sizeof(struct sbc_enc_cfg_t));
1476 if (ret != 0) {
1477 ALOGE("%s: failed to set SBC encoder config", __func__);
1478 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301479 goto fail;
1480 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301481 ret = a2dp_set_bit_format(sbc_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301482 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301483 is_configured = false;
1484 goto fail;
1485 }
1486 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001487 a2dp.bt_encoder_format = CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301488 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301489
1490 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
1491 a2dp.enc_channels = 1;
1492 else
1493 a2dp.enc_channels = 2;
1494
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301495 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
1496 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301497fail:
1498 return is_configured;
1499}
1500
Manish Dewangan6a252632017-12-04 17:27:44 +05301501#ifndef LINUX_ENABLED
Sharad Sangle95d451b2018-06-19 12:24:20 +05301502static int update_aptx_ad_dsp_config(struct aptx_ad_enc_cfg_t *aptx_dsp_cfg,
1503 audio_aptx_encoder_config *aptx_bt_cfg)
1504{
1505 int ret = 0;
1506
1507 if(aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
1508 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1509 aptx_dsp_cfg, aptx_bt_cfg);
1510 return -EINVAL;
1511 }
1512
1513 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_ad_enc_cfg_t));
Ramu Gottipati02809682018-12-19 16:46:12 +05301514 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301515
1516
1517 aptx_dsp_cfg->aptx_ad_cfg.sampling_freq = aptx_bt_cfg->ad_cfg->sampling_rate;
1518 aptx_dsp_cfg->aptx_ad_cfg.mtu = aptx_bt_cfg->ad_cfg->mtu;
1519 aptx_dsp_cfg->aptx_ad_cfg.channel_mode = aptx_bt_cfg->ad_cfg->channel_mode;
1520 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeA = aptx_bt_cfg->ad_cfg->min_sink_modeA;
1521 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeA = aptx_bt_cfg->ad_cfg->max_sink_modeA;
1522 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeB = aptx_bt_cfg->ad_cfg->min_sink_modeB;
1523 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeB = aptx_bt_cfg->ad_cfg->max_sink_modeB;
1524 aptx_dsp_cfg->aptx_ad_cfg.min_sink_modeC = aptx_bt_cfg->ad_cfg->min_sink_modeC;
1525 aptx_dsp_cfg->aptx_ad_cfg.max_sink_modeC = aptx_bt_cfg->ad_cfg->max_sink_modeC;
Sharad Sanglee378afe2018-09-03 20:04:17 +05301526 aptx_dsp_cfg->aptx_ad_cfg.mode = aptx_bt_cfg->ad_cfg->encoder_mode;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301527 aptx_dsp_cfg->abr_cfg.imc_info.direction = IMC_RECEIVE;
1528 aptx_dsp_cfg->abr_cfg.imc_info.enable = IMC_ENABLE;
1529 aptx_dsp_cfg->abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
1530 aptx_dsp_cfg->abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
1531
1532
1533 switch(aptx_dsp_cfg->aptx_ad_cfg.channel_mode) {
1534 case APTX_AD_CHANNEL_UNCHANGED:
1535 case APTX_AD_CHANNEL_JOINT_STEREO:
1536 case APTX_AD_CHANNEL_DUAL_MONO:
1537 case APTX_AD_CHANNEL_STEREO_TWS:
1538 case APTX_AD_CHANNEL_EARBUD:
1539 default:
1540 a2dp.enc_channels = CH_STEREO;
1541 aptx_dsp_cfg->custom_cfg.num_channels = CH_STEREO;
1542 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1543 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1544 break;
1545 case APTX_AD_CHANNEL_MONO:
1546 a2dp.enc_channels = CH_MONO;
1547 aptx_dsp_cfg->custom_cfg.num_channels = CH_MONO;
1548 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1549 break;
1550 }
1551 switch(aptx_dsp_cfg->aptx_ad_cfg.sampling_freq) {
1552 case APTX_AD_SR_UNCHANGED:
1553 case APTX_AD_48:
1554 default:
1555 a2dp.enc_sampling_rate = SAMPLING_RATE_48K;
1556 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_48K;
1557 break;
1558 case APTX_AD_44_1:
1559 a2dp.enc_sampling_rate = SAMPLING_RATE_441K;
1560 aptx_dsp_cfg->custom_cfg.sample_rate = SAMPLING_RATE_441K;
1561 break;
1562 }
1563 ALOGV("Successfully updated APTX AD enc format with \
1564 samplingrate: %d channels:%d",
1565 aptx_dsp_cfg->custom_cfg.sample_rate,
1566 aptx_dsp_cfg->custom_cfg.num_channels);
1567
1568 return ret;
1569}
Manisha Agarwala51768b2018-11-01 16:30:52 +05301570
1571static void audio_a2dp_update_tws_channel_mode()
1572{
1573 char* channel_mode;
1574 struct mixer_ctl *ctl_channel_mode;
1575 if (a2dp.is_tws_mono_mode_on)
1576 channel_mode = "One";
1577 else
1578 channel_mode = "Two";
1579 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
1580 if (!ctl_channel_mode) {
1581 ALOGE("failed to get tws mixer ctl");
1582 return;
1583 }
1584 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
1585 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
1586 return;
1587 }
1588}
1589
Manish Dewangan6a252632017-12-04 17:27:44 +05301590static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
1591 audio_aptx_encoder_config *aptx_bt_cfg)
1592{
1593 int ret = 0;
1594
1595 if(aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
1596 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1597 aptx_dsp_cfg, aptx_bt_cfg);
1598 return -EINVAL;
1599 }
1600
1601 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001602 aptx_dsp_cfg->custom_cfg.enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301603
1604 if (!a2dp.is_aptx_dual_mono_supported) {
1605 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1606 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
1607 } else {
1608 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1609 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
1610 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
1611 }
1612
1613 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
1614 case 1:
1615 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1616 break;
1617 case 2:
1618 default:
Manisha Agarwala51768b2018-11-01 16:30:52 +05301619 if (!a2dp.is_tws_mono_mode_on) {
1620 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1621 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1622 }
1623 else {
1624 a2dp.is_tws_mono_mode_on = true;
1625 ALOGD("Update tws for mono_mode_on: %d",a2dp.is_tws_mono_mode_on);
Manisha Agarwala51768b2018-11-01 16:30:52 +05301626 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301627 break;
1628 }
1629 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
1630 if (!a2dp.is_aptx_dual_mono_supported) {
1631 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
1632 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
1633 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
1634 aptx_dsp_cfg->custom_cfg.num_channels);
1635 } else {
1636 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
1637 ALOGV("Successfully updated APTX dual mono enc format with \
1638 samplingrate: %d channels:%d syncmode %d",
1639 aptx_dsp_cfg->custom_cfg.sample_rate,
1640 aptx_dsp_cfg->custom_cfg.num_channels,
1641 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
1642 }
1643 return ret;
1644}
1645#else
1646static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
1647 audio_aptx_encoder_config *aptx_bt_cfg)
1648{
1649 int ret = 0;
1650
1651 if(aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
1652 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
1653 aptx_dsp_cfg, aptx_bt_cfg);
1654 return -EINVAL;
1655 }
1656
1657 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001658 aptx_dsp_cfg->enc_format = MEDIA_FMT_APTX;
Manish Dewangan6a252632017-12-04 17:27:44 +05301659 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
1660 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
1661 switch(aptx_dsp_cfg->num_channels) {
1662 case 1:
1663 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
1664 break;
1665 case 2:
1666 default:
1667 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
1668 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
1669 break;
1670 }
1671
1672 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
1673 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
1674
1675 return ret;
1676}
1677#endif
1678
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301679/* API to configure APTX DSP encoder */
1680bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1681{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301682 struct mixer_ctl *ctl_enc_data = NULL;
Manish Dewangan6a252632017-12-04 17:27:44 +05301683 int mixer_size;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301684 bool is_configured = false;
1685 int ret = 0;
Manish Dewangan6a252632017-12-04 17:27:44 +05301686 int sample_rate_backup;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05301687
1688 if(aptx_bt_cfg == NULL)
1689 return false;
1690
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001691 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1692 if (!ctl_enc_data) {
1693 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1694 return false;
1695 }
1696
Manish Dewangan6a252632017-12-04 17:27:44 +05301697#ifndef LINUX_ENABLED
1698 struct aptx_enc_cfg_t aptx_dsp_cfg;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301699 struct aptx_ad_enc_cfg_t aptx_ad_dsp_cfg;
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001700 if(a2dp.is_aptx_adaptive) {
1701 mixer_size = sizeof(struct aptx_ad_enc_cfg_t);
1702 ret = update_aptx_ad_dsp_config(&aptx_ad_dsp_cfg, aptx_bt_cfg);
1703 sample_rate_backup = aptx_ad_dsp_cfg.custom_cfg.sample_rate;
1704 } else {
Sharad Sangle95d451b2018-06-19 12:24:20 +05301705 mixer_size = sizeof(struct aptx_enc_cfg_t);
1706 sample_rate_backup = aptx_bt_cfg->default_cfg->sampling_rate;
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001707 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
1708 }
1709 if (ret) {
1710 is_configured = false;
1711 goto fail;
1712 }
1713
1714 if(a2dp.is_aptx_adaptive) {
1715 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_ad_dsp_cfg,
1716 mixer_size);
1717 } else {
1718 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1719 mixer_size);
Sharad Sangle95d451b2018-06-19 12:24:20 +05301720 }
Manish Dewangan6a252632017-12-04 17:27:44 +05301721#else
1722 struct custom_enc_cfg_t aptx_dsp_cfg;
1723 mixer_size = sizeof(struct custom_enc_cfg_t);
1724 sample_rate_backup = aptx_bt_cfg->sampling_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +05301725 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
Manish Dewangan6a252632017-12-04 17:27:44 +05301726 if (ret) {
1727 is_configured = false;
1728 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301729 }
Weiyin Jiang20d3fa62018-08-01 18:06:27 +08001730 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
1731 mixer_size);
1732#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301733 if (ret != 0) {
1734 ALOGE("%s: Failed to set APTX encoder config", __func__);
1735 is_configured = false;
1736 goto fail;
1737 }
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301738#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301739 if(a2dp.is_aptx_adaptive)
1740 ret = a2dp_set_bit_format(aptx_bt_cfg->ad_cfg->bits_per_sample);
1741 else if(a2dp.is_aptx_dual_mono_supported)
1742 ret = a2dp_set_bit_format(aptx_bt_cfg->dual_mono_cfg->bits_per_sample);
1743 else
1744 ret = a2dp_set_bit_format(aptx_bt_cfg->default_cfg->bits_per_sample);
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05301745#endif
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301746 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301747 is_configured = false;
1748 goto fail;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301749 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301750 is_configured = true;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301751 if (a2dp.is_aptx_adaptive)
Surendar Karka2febd452018-12-13 17:56:43 +05301752 a2dp.bt_encoder_format = CODEC_TYPE_APTX_AD;
Sharad Sangle95d451b2018-06-19 12:24:20 +05301753 else
Surendar Karka2febd452018-12-13 17:56:43 +05301754 a2dp.bt_encoder_format = CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301755fail:
Manish Dewangan6a252632017-12-04 17:27:44 +05301756 /*restore sample rate */
1757 if(!is_configured)
1758 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301759 return is_configured;
1760}
1761
1762/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301763 */
Manish Dewangan6a252632017-12-04 17:27:44 +05301764#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07001765bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +05301766#else
1767bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
1768#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301769{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301770 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301771 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301772 bool is_configured = false;
1773 int ret = 0;
1774
1775 if(aptx_bt_cfg == NULL)
1776 return false;
1777
1778 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1779 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001780 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301781 is_configured = false;
1782 goto fail;
1783 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301784
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301785 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001786 aptx_dsp_cfg.enc_format = MEDIA_FMT_APTX_HD;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301787 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
1788 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
1789 switch(aptx_dsp_cfg.num_channels) {
1790 case 1:
1791 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1792 break;
1793 case 2:
1794 default:
1795 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1796 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1797 break;
1798 }
1799 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301800 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301801 if (ret != 0) {
1802 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
1803 is_configured = false;
1804 goto fail;
1805 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301806 ret = a2dp_set_bit_format(aptx_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301807 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301808 is_configured = false;
1809 goto fail;
1810 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301811 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001812 a2dp.bt_encoder_format = CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301813 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301814 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301815 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
1816 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301817fail:
1818 return is_configured;
1819}
1820
1821/* API to configure AAC DSP encoder */
1822bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
1823{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301824 struct mixer_ctl *ctl_enc_data = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301825 struct aac_enc_cfg_t aac_dsp_cfg;
1826 bool is_configured = false;
1827 int ret = 0;
1828
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001829 if (aac_bt_cfg == NULL)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301830 return false;
1831
1832 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1833 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001834 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301835 is_configured = false;
1836 goto fail;
1837 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301838 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02001839 aac_dsp_cfg.enc_format = MEDIA_FMT_AAC;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301840 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05301841 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001842 switch (aac_bt_cfg->enc_mode) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301843 case 0:
1844 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
1845 break;
1846 case 2:
1847 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
1848 break;
1849 case 1:
1850 default:
1851 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
1852 break;
1853 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +05301854 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301855 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001856
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301857 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
1858 sizeof(struct aac_enc_cfg_t));
1859 if (ret != 0) {
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001860 ALOGE("%s: Failed to set AAC encoder config", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301861 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301862 goto fail;
1863 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301864 ret = a2dp_set_bit_format(aac_bt_cfg->bits_per_sample);
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301865 if (ret != 0) {
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301866 is_configured = false;
1867 goto fail;
1868 }
1869 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001870 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301871 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08001872 a2dp.enc_channels = aac_bt_cfg->channels;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001873 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
1874 __func__, aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
1875fail:
1876 return is_configured;
1877}
1878
1879bool configure_aac_enc_format_v2(audio_aac_encoder_config_v2 *aac_bt_cfg)
1880{
1881 struct mixer_ctl *ctl_enc_data = NULL;
1882 struct aac_enc_cfg_v2_t aac_dsp_cfg;
1883 bool is_configured = false;
1884 int ret = 0;
1885
1886 if (aac_bt_cfg == NULL)
1887 return false;
1888
1889 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1890 if (!ctl_enc_data) {
1891 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1892 is_configured = false;
1893 goto fail;
1894 }
1895 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_v2_t));
Ramu Gottipati08d82e72018-12-17 11:52:14 +05301896 aac_dsp_cfg.aac_enc_cfg.enc_format = MEDIA_FMT_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001897 aac_dsp_cfg.aac_enc_cfg.bit_rate = aac_bt_cfg->audio_aac_enc_cfg.bitrate;
1898 aac_dsp_cfg.aac_enc_cfg.sample_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
1899 switch (aac_bt_cfg->audio_aac_enc_cfg.enc_mode) {
1900 case 0:
1901 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
1902 break;
1903 case 2:
1904 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
1905 break;
1906 case 1:
1907 default:
1908 aac_dsp_cfg.aac_enc_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
1909 break;
1910 }
1911 aac_dsp_cfg.aac_enc_cfg.aac_fmt_flag = aac_bt_cfg->audio_aac_enc_cfg.format_flag;
1912 aac_dsp_cfg.aac_enc_cfg.channel_cfg = aac_bt_cfg->audio_aac_enc_cfg.channels;
1913 aac_dsp_cfg.frame_ctl.ctl_type = aac_bt_cfg->frame_ctl.ctl_type;
1914 aac_dsp_cfg.frame_ctl.ctl_value = aac_bt_cfg->frame_ctl.ctl_value;
1915
1916 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
1917 sizeof(struct aac_enc_cfg_v2_t));
1918 if (ret != 0) {
1919 ALOGE("%s: Failed to set AAC encoder config", __func__);
1920 is_configured = false;
1921 goto fail;
1922 }
1923 ret = a2dp_set_bit_format(aac_bt_cfg->audio_aac_enc_cfg.bits_per_sample);
1924 if (ret != 0) {
1925 is_configured = false;
1926 goto fail;
1927 }
1928 is_configured = true;
Ramu Gottipati08d82e72018-12-17 11:52:14 +05301929 a2dp.bt_encoder_format = CODEC_TYPE_AAC;
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07001930 a2dp.enc_sampling_rate = aac_bt_cfg->audio_aac_enc_cfg.sampling_rate;
1931 a2dp.enc_channels = aac_bt_cfg->audio_aac_enc_cfg.channels;
1932 ALOGV("%s: Successfully updated AAC enc format with sampling rate: %d channels:%d",
1933 __func__, aac_dsp_cfg.aac_enc_cfg.sample_rate, aac_dsp_cfg.aac_enc_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301934fail:
1935 return is_configured;
1936}
1937
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301938bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
1939{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301940 struct mixer_ctl *ctl_enc_data = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301941 struct celt_enc_cfg_t celt_dsp_cfg;
1942 bool is_configured = false;
1943 int ret = 0;
1944 if(celt_bt_cfg == NULL)
1945 return false;
1946
1947 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1948 if (!ctl_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02001949 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301950 is_configured = false;
1951 goto fail;
1952 }
1953 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
1954
Florian Pfister1a84f312018-07-19 14:38:18 +02001955 celt_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301956 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
1957 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
1958 switch(celt_dsp_cfg.custom_cfg.num_channels) {
1959 case 1:
1960 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1961 break;
1962 case 2:
1963 default:
1964 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1965 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1966 break;
1967 }
1968
1969 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
1970
1971 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
1972 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
1973 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
1974 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
1975 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
1976
1977 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
1978 sizeof(struct celt_enc_cfg_t));
1979 if (ret != 0) {
1980 ALOGE("%s: Failed to set CELT encoder config", __func__);
1981 is_configured = false;
1982 goto fail;
1983 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05301984 ret = a2dp_set_bit_format(celt_bt_cfg->bits_per_sample);
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301985 if (ret != 0) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301986 is_configured = false;
1987 goto fail;
1988 }
1989 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02001990 a2dp.bt_encoder_format = CODEC_TYPE_CELT;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301991 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301992 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301993 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
1994 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
1995fail:
1996 return is_configured;
1997}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301998
1999bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
2000{
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302001 struct mixer_ctl *ldac_enc_data = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302002 struct ldac_enc_cfg_t ldac_dsp_cfg;
2003 bool is_configured = false;
2004 int ret = 0;
2005 if(ldac_bt_cfg == NULL)
2006 return false;
2007
2008 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
2009 if (!ldac_enc_data) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002010 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identified");
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302011 is_configured = false;
2012 goto fail;
2013 }
2014 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
2015
Florian Pfister1a84f312018-07-19 14:38:18 +02002016 ldac_dsp_cfg.custom_cfg.enc_format = MEDIA_FMT_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302017 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
2018 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
2019 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
2020 case 4:
2021 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
2022 ldac_dsp_cfg.custom_cfg.num_channels = 1;
2023 break;
2024 case 2:
2025 case 1:
2026 default:
2027 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
2028 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
2029 ldac_dsp_cfg.custom_cfg.num_channels = 2;
2030 break;
2031 }
2032
2033 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
2034 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
2035 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002036 if (ldac_bt_cfg->is_abr_enabled) {
2037 ldac_dsp_cfg.abr_cfg.mapping_info = ldac_bt_cfg->level_to_bitrate_map;
2038 ldac_dsp_cfg.abr_cfg.imc_info.direction = IMC_RECEIVE;
2039 ldac_dsp_cfg.abr_cfg.imc_info.enable = IMC_ENABLE;
2040 ldac_dsp_cfg.abr_cfg.imc_info.purpose = IMC_PURPOSE_ID_BT_INFO;
2041 ldac_dsp_cfg.abr_cfg.imc_info.comm_instance = a2dp.abr_config.imc_instance;
Aniket Kumar Lata8c884eb2018-08-06 15:30:50 -07002042 ldac_dsp_cfg.abr_cfg.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002043 }
2044
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302045 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
2046 sizeof(struct ldac_enc_cfg_t));
2047 if (ret != 0) {
2048 ALOGE("%s: Failed to set LDAC encoder config", __func__);
2049 is_configured = false;
2050 goto fail;
2051 }
Samyak Jain2cddc0e2018-07-18 15:22:27 +05302052 ret = a2dp_set_bit_format(ldac_bt_cfg->bits_per_sample);
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302053 if (ret != 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302054 is_configured = false;
2055 goto fail;
2056 }
2057 is_configured = true;
Florian Pfister1a84f312018-07-19 14:38:18 +02002058 a2dp.bt_encoder_format = CODEC_TYPE_LDAC;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302059 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
2060 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002061 a2dp.abr_config.is_abr_enabled = ldac_bt_cfg->is_abr_enabled;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302062 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
2063 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
2064fail:
2065 return is_configured;
2066}
2067
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302068bool configure_a2dp_encoder_format()
2069{
2070 void *codec_info = NULL;
2071 uint8_t multi_cast = 0, num_dev = 1;
Florian Pfister1a84f312018-07-19 14:38:18 +02002072 codec_t codec_type = CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302073 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002074 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302075
Florian Pfister1a84f312018-07-19 14:38:18 +02002076 if (!a2dp.audio_get_enc_config) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302077 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
2078 return false;
2079 }
2080 ALOGD("configure_a2dp_encoder_format start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002081 codec_info = a2dp.audio_get_enc_config(&multi_cast, &num_dev,
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302082 &codec_type);
2083
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002084 // ABR disabled by default for all codecs
2085 a2dp.abr_config.is_abr_enabled = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302086 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002087
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302088 switch(codec_type) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002089 case CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302090 ALOGD(" Received SBC encoder supported BT device");
2091 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002092 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302093 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002094 case CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302095 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302096#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002097 a2dp.is_aptx_dual_mono_supported = false;
2098 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05302099#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302100 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002101 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302102 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002103 case CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302104 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05302105#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302106 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002107 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05302108#else
2109 is_configured =
2110 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
2111#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002112 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302113#ifndef LINUX_ENABLED
Florian Pfister1a84f312018-07-19 14:38:18 +02002114 case CODEC_TYPE_APTX_DUAL_MONO:
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002115 ALOGD(" Received APTX dual mono encoder supported BT device");
2116 a2dp.is_aptx_dual_mono_supported = true;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302117 if (a2dp.audio_is_tws_mono_mode_enable != NULL)
2118 a2dp.is_tws_mono_mode_on = a2dp.audio_is_tws_mono_mode_enable();
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002119 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
2120 is_configured =
2121 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302122 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05302123#endif
Florian Pfister1a84f312018-07-19 14:38:18 +02002124 case CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302125 ALOGD(" Received AAC encoder supported BT device");
Preetam Singh Ranawat3d78f522018-06-20 14:41:26 -07002126 bool is_aac_frame_ctl_enabled =
2127 property_get_bool("persist.vendor.bt.aac_frm_ctl.enabled", false);
2128 is_configured = is_aac_frame_ctl_enabled ?
2129 configure_aac_enc_format_v2((audio_aac_encoder_config_v2 *) codec_info) :
2130 configure_aac_enc_format((audio_aac_encoder_config *) codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302131 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002132 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302133 ALOGD(" Received CELT encoder supported BT device");
2134 is_configured =
2135 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
2136 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002137 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302138 ALOGD(" Received LDAC encoder supported BT device");
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002139 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2140 instance_id = MAX_INSTANCE_ID;
2141 a2dp.abr_config.imc_instance = instance_id--;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302142 is_configured =
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002143 (configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info) &&
Surendar Karka2febd452018-12-13 17:56:43 +05302144 configure_a2dp_source_decoder_format(CODEC_TYPE_LDAC));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302145 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302146#ifndef LINUX_ENABLED //Temporarily disabled for LE, need to take care while doing VT FR
Surendar Karka2febd452018-12-13 17:56:43 +05302147 case CODEC_TYPE_APTX_AD:
Sharad Sangle95d451b2018-06-19 12:24:20 +05302148 ALOGD(" Received APTX AD encoder supported BT device");
2149 if (!instance_id || instance_id > MAX_INSTANCE_ID)
2150 instance_id = MAX_INSTANCE_ID;
2151 a2dp.abr_config.imc_instance = instance_id--;
2152 a2dp.abr_config.is_abr_enabled = true; // for APTX Adaptive ABR is Always on
2153 a2dp.is_aptx_adaptive = true;
2154 aptx_encoder_cfg.ad_cfg = (audio_aptx_ad_config *)codec_info;
2155 is_configured =
2156 (configure_aptx_enc_format(&aptx_encoder_cfg) &&
Ramu Gottipati02809682018-12-19 16:46:12 +05302157 configure_a2dp_source_decoder_format(MEDIA_FMT_APTX_AD));
Sharad Sangle95d451b2018-06-19 12:24:20 +05302158 break;
Ramu Gottipati3e0d4c32018-11-05 15:57:28 +05302159#endif
Aalique Grahame22e49102018-12-18 14:23:57 -08002160 case CODEC_TYPE_PCM:
2161 ALOGD("Received PCM format for BT device");
2162 a2dp.bt_encoder_format = CODEC_TYPE_PCM;
2163 is_configured = true;
2164 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302165 default:
2166 ALOGD(" Received Unsupported encoder formar");
2167 is_configured = false;
2168 break;
2169 }
2170 return is_configured;
2171}
2172
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002173int a2dp_start_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302174{
2175 int ret = 0;
2176
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002177 ALOGD("a2dp_start_playback start");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302178
Florian Pfister1a84f312018-07-19 14:38:18 +02002179 if(!(a2dp.bt_lib_source_handle && a2dp.audio_source_start
2180 && a2dp.audio_get_enc_config)) {
2181 ALOGE("a2dp handle is not identified, Ignoring start playback request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302182 return -ENOSYS;
2183 }
2184
Florian Pfister1a84f312018-07-19 14:38:18 +02002185 if(a2dp.a2dp_source_suspended == true) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302186 //session will be restarted after suspend completion
2187 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302188 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302189 }
2190
Florian Pfister1a84f312018-07-19 14:38:18 +02002191 if (!a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302192 ALOGD("calling BT module stream start");
2193 /* This call indicates BT IPC lib to start playback */
Florian Pfister1a84f312018-07-19 14:38:18 +02002194 ret = a2dp.audio_source_start();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302195 ALOGE("BT controller start return = %d",ret);
2196 if (ret != 0 ) {
2197 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002198 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302199 } else {
2200 if(configure_a2dp_encoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002201 a2dp.a2dp_source_started = true;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302202 ret = 0;
2203 ALOGD("Start playback successful to BT library");
2204 } else {
2205 ALOGD(" unable to configure DSP encoder");
Florian Pfister1a84f312018-07-19 14:38:18 +02002206 a2dp.a2dp_source_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302207 ret = -ETIMEDOUT;
2208 }
2209 }
2210 }
2211
Florian Pfister1a84f312018-07-19 14:38:18 +02002212 if (a2dp.a2dp_source_started) {
2213 a2dp.a2dp_source_total_active_session_requests++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302214 a2dp_check_and_set_scrambler();
Manisha Agarwal02a0b7f2019-02-06 19:24:46 +05302215 audio_a2dp_update_tws_channel_mode();
Florian Pfister1a84f312018-07-19 14:38:18 +02002216 a2dp_set_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002217 if (a2dp.abr_config.is_abr_enabled)
2218 start_abr();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05302219 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302220
2221 ALOGD("start A2DP playback total active sessions :%d",
Florian Pfister1a84f312018-07-19 14:38:18 +02002222 a2dp.a2dp_source_total_active_session_requests);
2223 return ret;
2224}
2225
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002226uint64_t a2dp_get_decoder_latency()
Florian Pfister1a84f312018-07-19 14:38:18 +02002227{
2228 uint32_t latency = 0;
2229
2230 switch(a2dp.bt_decoder_format) {
2231 case CODEC_TYPE_SBC:
2232 latency = DEFAULT_SINK_LATENCY_SBC;
2233 break;
2234 case CODEC_TYPE_AAC:
2235 latency = DEFAULT_SINK_LATENCY_AAC;
2236 break;
2237 default:
2238 latency = 200;
2239 ALOGD("No valid decoder defined, setting latency to %dms", latency);
2240 break;
2241 }
2242 return (uint64_t)latency;
2243}
2244
2245bool a2dp_send_sink_setup_complete(void) {
2246 uint64_t system_latency = 0;
2247 bool is_complete = false;
2248
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002249 system_latency = a2dp_get_decoder_latency();
Florian Pfister1a84f312018-07-19 14:38:18 +02002250
2251 if (a2dp.audio_sink_session_setup_complete(system_latency) == 0) {
2252 is_complete = true;
2253 }
2254 return is_complete;
2255}
2256
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002257bool a2dp_sink_is_ready()
2258{
2259 bool ret = false;
2260
2261 if ((a2dp.bt_state_sink != A2DP_STATE_DISCONNECTED) &&
2262 (a2dp.is_a2dp_offload_supported) &&
2263 (a2dp.audio_sink_check_a2dp_ready))
2264 ret = a2dp.audio_sink_check_a2dp_ready();
2265 return ret;
2266}
2267
2268int a2dp_start_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002269{
2270 int ret = 0;
2271
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002272 ALOGD("a2dp_start_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002273
2274 if(!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_start
2275 && a2dp.audio_get_dec_config)) {
2276 ALOGE("a2dp handle is not identified, Ignoring start capture request");
2277 return -ENOSYS;
2278 }
2279
2280 if (!a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2281 ALOGD("calling BT module stream start");
2282 /* This call indicates BT IPC lib to start capture */
2283 ret = a2dp.audio_sink_start();
2284 ALOGE("BT controller start capture return = %d",ret);
2285 if (ret != 0 ) {
2286 ALOGE("BT controller start capture failed");
2287 a2dp.a2dp_sink_started = false;
2288 } else {
2289
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002290 if(!a2dp_sink_is_ready()) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002291 ALOGD("Wait for capture ready not successful");
2292 ret = -ETIMEDOUT;
2293 }
2294
Surendar Karka2febd452018-12-13 17:56:43 +05302295 if(configure_a2dp_sink_decoder_format() == true) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002296 a2dp.a2dp_sink_started = true;
2297 ret = 0;
2298 ALOGD("Start capture successful to BT library");
2299 } else {
2300 ALOGD(" unable to configure DSP decoder");
2301 a2dp.a2dp_sink_started = false;
2302 ret = -ETIMEDOUT;
2303 }
2304
2305 if (!a2dp_send_sink_setup_complete()) {
2306 ALOGD("sink_setup_complete not successful");
2307 ret = -ETIMEDOUT;
2308 }
2309 }
2310 }
2311
2312 if (a2dp.a2dp_sink_started) {
2313 if (a2dp_set_backend_cfg(SINK) == true) {
2314 a2dp.a2dp_sink_total_active_session_requests++;
2315 }
2316 }
2317
2318 ALOGD("start A2DP sink total active sessions :%d",
2319 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302320 return ret;
2321}
2322
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302323static void reset_a2dp_enc_config_params()
2324{
2325 int ret =0;
2326
Aalique Grahame22e49102018-12-18 14:23:57 -08002327 struct mixer_ctl *ctl_enc_config, *ctl_channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302328 struct sbc_enc_cfg_t dummy_reset_config;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302329 char* channel_mode;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302330
2331 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
2332 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
2333 MIXER_ENC_CONFIG_BLOCK);
2334 if (!ctl_enc_config) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002335 ALOGE(" ERROR a2dp encoder format mixer control not identified");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302336 } else {
2337 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
2338 sizeof(struct sbc_enc_cfg_t));
Florian Pfister1a84f312018-07-19 14:38:18 +02002339 a2dp.bt_encoder_format = MEDIA_FMT_NONE;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302340 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002341
2342 a2dp_set_bit_format(DEFAULT_ENCODER_BIT_FORMAT);
2343
Manisha Agarwala51768b2018-11-01 16:30:52 +05302344 ctl_channel_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,MIXER_FMT_TWS_CHANNEL_MODE);
2345
2346 if (!ctl_channel_mode) {
2347 ALOGE("failed to get tws mixer ctl");
2348 } else {
2349 channel_mode = "Two";
2350 if (mixer_ctl_set_enum_by_string(ctl_channel_mode, channel_mode) != 0) {
2351 ALOGE("%s: Failed to set the channel mode = %s", __func__, channel_mode);
2352 }
2353 a2dp.is_tws_mono_mode_on = false;
2354 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302355}
2356
Surendar Karka2febd452018-12-13 17:56:43 +05302357static int reset_a2dp_source_dec_config_params()
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002358{
2359 struct mixer_ctl *ctl_dec_data = NULL;
2360 struct abr_dec_cfg_t dummy_reset_cfg;
2361 int ret = 0;
2362
2363 if (a2dp.abr_config.is_abr_enabled) {
Surendar Karka2febd452018-12-13 17:56:43 +05302364 ctl_dec_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_SOURCE_DEC_CONFIG_BLOCK);
Aniket Kumar Latae1220c32018-05-29 14:55:47 -07002365 if (!ctl_dec_data) {
2366 ALOGE("%s: ERROR A2DP decoder config mixer control not identifed", __func__);
2367 return -EINVAL;
2368 }
2369 memset(&dummy_reset_cfg, 0x0, sizeof(dummy_reset_cfg));
2370 ret = mixer_ctl_set_array(ctl_dec_data, (void *)&dummy_reset_cfg,
2371 sizeof(dummy_reset_cfg));
2372 if (ret != 0) {
2373 ALOGE("%s: Failed to set dummy decoder config", __func__);
2374 return ret;
2375 }
2376 }
2377
2378 return ret;
2379}
2380
Surendar Karka2febd452018-12-13 17:56:43 +05302381static void reset_a2dp_sink_dec_config_params()
Florian Pfister1a84f312018-07-19 14:38:18 +02002382{
2383 int ret =0;
2384
2385 struct mixer_ctl *ctl_dec_config, *ctrl_bit_format;
2386 struct aac_dec_cfg_t dummy_reset_config;
2387
2388 memset(&dummy_reset_config, 0x0, sizeof(struct aac_dec_cfg_t));
2389 ctl_dec_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
Surendar Karka2febd452018-12-13 17:56:43 +05302390 MIXER_SINK_DEC_CONFIG_BLOCK);
Florian Pfister1a84f312018-07-19 14:38:18 +02002391 if (!ctl_dec_config) {
2392 ALOGE(" ERROR a2dp decoder format mixer control not identified");
2393 } else {
2394 ret = mixer_ctl_set_array(ctl_dec_config, (void *)&dummy_reset_config,
2395 sizeof(struct aac_dec_cfg_t));
2396 a2dp.bt_decoder_format = MEDIA_FMT_NONE;
2397 }
2398 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
2399 MIXER_DEC_BIT_FORMAT);
2400 if (!ctrl_bit_format) {
2401 ALOGE(" ERROR bit format CONFIG data mixer control not identified");
2402 } else {
2403 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
2404 if (ret != 0) {
2405 ALOGE("%s: Failed to set bit format to decoder", __func__);
2406 }
2407 }
2408}
2409
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002410int a2dp_stop_playback()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302411{
2412 int ret =0;
2413
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002414 ALOGV("a2dp_stop_playback start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002415 if(!(a2dp.bt_lib_source_handle && a2dp.audio_source_stop)) {
2416 ALOGE("a2dp handle is not identified, Ignoring stop request");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302417 return -ENOSYS;
2418 }
2419
Florian Pfister1a84f312018-07-19 14:38:18 +02002420 if (a2dp.a2dp_source_total_active_session_requests > 0)
2421 a2dp.a2dp_source_total_active_session_requests--;
Aalique Grahame22e49102018-12-18 14:23:57 -08002422 else
2423 ALOGE("%s: No active playback session requests on A2DP", __func__);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302424
Florian Pfister1a84f312018-07-19 14:38:18 +02002425 if ( a2dp.a2dp_source_started && !a2dp.a2dp_source_total_active_session_requests) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302426 ALOGV("calling BT module stream stop");
Florian Pfister1a84f312018-07-19 14:38:18 +02002427 ret = a2dp.audio_source_stop();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302428 if (ret < 0)
2429 ALOGE("stop stream to BT IPC lib failed");
2430 else
2431 ALOGV("stop steam to BT IPC lib successful");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302432 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302433 reset_a2dp_source_dec_config_params();
2434 a2dp_reset_backend_cfg(SOURCE);
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002435 if (a2dp.abr_config.is_abr_enabled && a2dp.abr_config.abr_started)
2436 stop_abr();
2437 a2dp.abr_config.is_abr_enabled = false;
Surendar Karka2febd452018-12-13 17:56:43 +05302438 a2dp.a2dp_source_started = false;
Florian Pfister1a84f312018-07-19 14:38:18 +02002439 a2dp_reset_backend_cfg(SOURCE);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302440 }
Florian Pfister1a84f312018-07-19 14:38:18 +02002441 if(!a2dp.a2dp_source_total_active_session_requests)
2442 a2dp.a2dp_source_started = false;
2443 ALOGD("Stop A2DP playback, total active sessions :%d",
2444 a2dp.a2dp_source_total_active_session_requests);
2445 return 0;
2446}
2447
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002448int a2dp_stop_capture()
Florian Pfister1a84f312018-07-19 14:38:18 +02002449{
2450 int ret =0;
2451
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002452 ALOGV("a2dp_stop_capture start");
Florian Pfister1a84f312018-07-19 14:38:18 +02002453 if(!(a2dp.bt_lib_sink_handle && a2dp.audio_sink_stop)) {
2454 ALOGE("a2dp handle is not identified, Ignoring stop request");
2455 return -ENOSYS;
2456 }
2457
2458 if (a2dp.a2dp_sink_total_active_session_requests > 0)
2459 a2dp.a2dp_sink_total_active_session_requests--;
2460
2461 if ( a2dp.a2dp_sink_started && !a2dp.a2dp_sink_total_active_session_requests) {
2462 ALOGV("calling BT module stream stop");
2463 ret = a2dp.audio_sink_stop();
2464 if (ret < 0)
2465 ALOGE("stop stream to BT IPC lib failed");
2466 else
2467 ALOGV("stop steam to BT IPC lib successful");
Surendar Karka2febd452018-12-13 17:56:43 +05302468 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002469 a2dp_reset_backend_cfg(SINK);
2470 }
2471 if(!a2dp.a2dp_sink_total_active_session_requests)
2472 a2dp.a2dp_source_started = false;
2473 ALOGD("Stop A2DP capture, total active sessions :%d",
2474 a2dp.a2dp_sink_total_active_session_requests);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302475 return 0;
2476}
2477
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002478int a2dp_set_parameters(struct str_parms *parms, bool *reconfig)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302479{
Aalique Grahame22e49102018-12-18 14:23:57 -08002480 int ret = 0, val, status = 0;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302481 char value[32]={0};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302482 struct audio_usecase *uc_info;
2483 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302484
2485 if(a2dp.is_a2dp_offload_supported == false) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002486 ALOGV("no supported encoders identified,ignoring a2dp setparam");
2487 status = -EINVAL;
2488 goto param_handled;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302489 }
2490
2491 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
2492 sizeof(value));
Zhou Song681350a2017-10-19 16:28:42 +08002493 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302494 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002495 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002496 ALOGV("Received device connect request for A2DP source");
2497 open_a2dp_source();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302498 }
2499 goto param_handled;
2500 }
2501
2502 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
2503 sizeof(value));
2504
Zhou Song681350a2017-10-19 16:28:42 +08002505 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302506 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08002507 if (audio_is_a2dp_out_device(val)) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002508 ALOGV("Received source device dis- connect request");
Samyak Jain4828f4c2018-08-24 16:31:06 +05302509 close_a2dp_output();
kunleiz4a1fad62018-02-08 18:00:16 +08002510 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302511 reset_a2dp_source_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002512 a2dp_reset_backend_cfg(SOURCE);
2513 } else if (audio_is_a2dp_in_device(val)) {
2514 ALOGV("Received sink device dis- connect request");
2515 close_a2dp_input();
Surendar Karka2febd452018-12-13 17:56:43 +05302516 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002517 a2dp_reset_backend_cfg(SINK);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302518 }
2519 goto param_handled;
2520 }
Surendar Karka2febd452018-12-13 17:56:43 +05302521#ifndef LINUX_ENABLED
Manisha Agarwala51768b2018-11-01 16:30:52 +05302522 ret = str_parms_get_str(parms, "TwsChannelConfig", value, sizeof(value));
2523 if (ret>=0) {
2524 ALOGD("Setting tws channel mode to %s",value);
2525 if(!(strncmp(value,"mono",strlen(value))))
2526 a2dp.is_tws_mono_mode_on = true;
2527 else if(!(strncmp(value,"dual-mono",strlen(value))))
2528 a2dp.is_tws_mono_mode_on = false;
2529 audio_a2dp_update_tws_channel_mode();
2530 goto param_handled;
2531 }
Surendar Karka2febd452018-12-13 17:56:43 +05302532#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302533 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
2534 if (ret >= 0) {
Surendar Karka2febd452018-12-13 17:56:43 +05302535 if (a2dp.bt_lib_source_handle) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302536 if ((!strncmp(value,"true",sizeof(value)))) {
Aalique Grahame22e49102018-12-18 14:23:57 -08002537 if (a2dp.a2dp_source_suspended) {
2538 ALOGD("%s: A2DP is already suspended", __func__);
2539 goto param_handled;
2540 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302541 ALOGD("Setting a2dp to suspend state");
Florian Pfister1a84f312018-07-19 14:38:18 +02002542 a2dp.a2dp_source_suspended = true;
Surendar Karka2febd452018-12-13 17:56:43 +05302543 if (a2dp.bt_state_source == A2DP_STATE_DISCONNECTED)
yidongh7203cca2018-09-19 16:12:25 +08002544 goto param_handled;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302545 list_for_each(node, &a2dp.adev->usecase_list) {
2546 uc_info = node_to_item(node, struct audio_usecase, list);
Zhou Songc66eb7e2017-08-08 18:29:07 +08002547 if (uc_info->type == PCM_PLAYBACK &&
2548 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302549 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002550 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302551 pthread_mutex_lock(&a2dp.adev->lock);
2552 }
2553 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05302554 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302555 reset_a2dp_source_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002556 if(a2dp.audio_source_suspend)
2557 a2dp.audio_source_suspend();
2558 } else if (a2dp.a2dp_source_suspended == true) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302559 ALOGD("Resetting a2dp suspend state");
Zhou Song10617ed2017-05-26 13:28:48 +08002560 struct audio_usecase *uc_info;
2561 struct listnode *node;
Florian Pfister1a84f312018-07-19 14:38:18 +02002562 if(a2dp.clear_source_a2dpsuspend_flag)
2563 a2dp.clear_source_a2dpsuspend_flag();
2564 a2dp.a2dp_source_suspended = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302565 /*
2566 * It is possible that before suspend,a2dp sessions can be active
2567 * for example during music + voice activation concurrency
2568 * a2dp suspend will be called & BT will change to sco mode
2569 * though music is paused as a part of voice activation
2570 * compress session close happens only after pause timeout(10secs)
2571 * so if resume request comes before pause timeout as a2dp session
2572 * is already active IPC start will not be called from APM/audio_hw
2573 * Fix is to call a2dp start for IPC library post suspend
2574 * based on number of active session count
2575 */
Florian Pfister1a84f312018-07-19 14:38:18 +02002576 if (a2dp.a2dp_source_total_active_session_requests > 0) {
Naresh Tanniru649871a2016-11-04 18:08:32 +05302577 ALOGD(" Calling IPC lib start post suspend state");
Florian Pfister1a84f312018-07-19 14:38:18 +02002578 if(a2dp.audio_source_start) {
2579 ret = a2dp.audio_source_start();
Naresh Tanniru649871a2016-11-04 18:08:32 +05302580 if (ret != 0) {
2581 ALOGE("BT controller start failed");
Florian Pfister1a84f312018-07-19 14:38:18 +02002582 a2dp.a2dp_source_started = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05302583 }
2584 }
2585 }
Zhou Song10617ed2017-05-26 13:28:48 +08002586 list_for_each(node, &a2dp.adev->usecase_list) {
2587 uc_info = node_to_item(node, struct audio_usecase, list);
Zhou Songc66eb7e2017-08-08 18:29:07 +08002588 if (uc_info->type == PCM_PLAYBACK &&
2589 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302590 pthread_mutex_unlock(&a2dp.adev->lock);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002591 fp_check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302592 pthread_mutex_lock(&a2dp.adev->lock);
2593 }
Zhou Song10617ed2017-05-26 13:28:48 +08002594 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302595 }
2596 }
2597 goto param_handled;
2598 }
Aalique Grahame22e49102018-12-18 14:23:57 -08002599
2600 ret = str_parms_get_str(parms, AUDIO_PARAMETER_RECONFIG_A2DP, value,
2601 sizeof(value));
2602 if (ret >= 0) {
2603 if (a2dp.is_a2dp_offload_supported &&
2604 a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2605 *reconfig = true;
2606 }
2607 goto param_handled;
2608 }
2609
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302610param_handled:
2611 ALOGV("end of a2dp setparam");
Aalique Grahame22e49102018-12-18 14:23:57 -08002612 return status;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302613}
2614
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002615void a2dp_set_handoff_mode(bool is_on)
Naresh Tannirucd2353e2016-08-19 00:37:25 +05302616{
2617 a2dp.is_handoff_in_progress = is_on;
2618}
2619
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002620bool a2dp_is_force_device_switch()
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302621{
2622 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05302623 // Or if a2dp device is selected but earlier start failed ( as a2dp
2624 // was suspended, force retry.
Florian Pfister1a84f312018-07-19 14:38:18 +02002625 return a2dp.is_handoff_in_progress || !a2dp.a2dp_source_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302626}
2627
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002628void a2dp_get_enc_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302629{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302630 *sample_rate = a2dp.enc_sampling_rate;
2631}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302632
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002633void a2dp_get_dec_sample_rate(int *sample_rate)
Florian Pfister1a84f312018-07-19 14:38:18 +02002634{
2635 *sample_rate = a2dp.dec_sampling_rate;
2636}
2637
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002638bool a2dp_source_is_ready()
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302639{
2640 bool ret = false;
2641
Florian Pfister1a84f312018-07-19 14:38:18 +02002642 if (a2dp.a2dp_source_suspended)
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302643 return ret;
2644
Florian Pfister1a84f312018-07-19 14:38:18 +02002645 if ((a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) &&
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08002646 (a2dp.is_a2dp_offload_supported) &&
Florian Pfister1a84f312018-07-19 14:38:18 +02002647 (a2dp.audio_source_check_a2dp_ready))
2648 ret = a2dp.audio_source_check_a2dp_ready();
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05302649 return ret;
2650}
2651
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002652bool a2dp_source_is_suspended()
Florian Pfister1a84f312018-07-19 14:38:18 +02002653{
2654 return a2dp.a2dp_source_suspended;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05302655}
2656
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002657void a2dp_init(void *adev,
Aalique Grahame6e763712019-01-31 16:18:17 -08002658 a2dp_offload_init_config_t init_config)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302659{
2660 a2dp.adev = (struct audio_device*)adev;
Florian Pfister1a84f312018-07-19 14:38:18 +02002661 a2dp.bt_lib_source_handle = NULL;
2662 a2dp.a2dp_source_started = false;
2663 a2dp.bt_state_source = A2DP_STATE_DISCONNECTED;
2664 a2dp.a2dp_source_total_active_session_requests = 0;
2665 a2dp.a2dp_source_suspended = false;
2666 a2dp.bt_encoder_format = CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05302667 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302668 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07002669 a2dp.is_aptx_dual_mono_supported = false;
Sharad Sangle95d451b2018-06-19 12:24:20 +05302670 a2dp.is_aptx_adaptive = false;
Aniket Kumar Lata7fd86e12018-02-20 19:26:10 -08002671 a2dp.abr_config.is_abr_enabled = false;
2672 a2dp.abr_config.abr_started = false;
2673 a2dp.abr_config.imc_instance = 0;
2674 a2dp.abr_config.abr_tx_handle = NULL;
Manisha Agarwala51768b2018-11-01 16:30:52 +05302675 a2dp.is_tws_mono_mode_on = false;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002676
2677 // init function pointers
2678 fp_platform_get_pcm_device_id =
Aalique Grahame6e763712019-01-31 16:18:17 -08002679 init_config.fp_platform_get_pcm_device_id;
2680 fp_check_a2dp_restore = init_config.fp_check_a2dp_restore;
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002681
kunleiz5a127262017-09-08 14:47:48 +08002682 reset_a2dp_enc_config_params();
Surendar Karka2febd452018-12-13 17:56:43 +05302683 reset_a2dp_source_dec_config_params();
2684 reset_a2dp_sink_dec_config_params();
Florian Pfister1a84f312018-07-19 14:38:18 +02002685
2686 a2dp.bt_lib_sink_handle = NULL;
2687 a2dp.a2dp_sink_started = false;
2688 a2dp.bt_state_sink = A2DP_STATE_DISCONNECTED;
2689 a2dp.a2dp_sink_total_active_session_requests = 0;
Aalique Grahame6e763712019-01-31 16:18:17 -08002690 if (isRunningWithVendorEnhancedFramework())
2691 open_a2dp_sink();
Florian Pfister1a84f312018-07-19 14:38:18 +02002692
2693 a2dp.is_a2dp_offload_supported = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05302694 update_offload_codec_capabilities();
2695}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002696
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002697uint32_t a2dp_get_encoder_latency()
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002698{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002699 uint32_t latency = 0;
2700 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302701 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
2702 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002703 char value[PROPERTY_VALUE_MAX];
2704
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002705 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aalique Grahame22e49102018-12-18 14:23:57 -08002706 avsync_runtime_prop = property_get(SYSPROP_A2DP_CODEC_LATENCIES, value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002707 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302708 if (sscanf(value, "%d/%d/%d/%d/%d%d",
2709 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002710 ALOGI("Failed to parse avsync offset params from '%s'.", value);
2711 avsync_runtime_prop = 0;
2712 }
2713 }
2714
yidongh0515e042017-07-06 15:00:34 +08002715 uint32_t slatency = 0;
Florian Pfister1a84f312018-07-19 14:38:18 +02002716 if (a2dp.audio_sink_get_a2dp_latency && a2dp.bt_state_source != A2DP_STATE_DISCONNECTED) {
2717 slatency = a2dp.audio_sink_get_a2dp_latency();
yidongh0515e042017-07-06 15:00:34 +08002718 }
2719
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07002720 switch(a2dp.bt_encoder_format) {
Florian Pfister1a84f312018-07-19 14:38:18 +02002721 case CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08002722 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
2723 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002724 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002725 case CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08002726 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
2727 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002728 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002729 case CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08002730 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
2731 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002732 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002733 case CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08002734 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
2735 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002736 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002737 case CODEC_TYPE_CELT:
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05302738 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
2739 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
2740 break;
Florian Pfister1a84f312018-07-19 14:38:18 +02002741 case CODEC_TYPE_LDAC:
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05302742 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
2743 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
2744 break;
Ramu Gottipatib729cf82018-12-20 15:36:46 +05302745 case CODEC_TYPE_APTX_AD: // for aptx adaptive the latency depends on the mode (HQ/LL) and
Sharad Sanglee378afe2018-09-03 20:04:17 +05302746 latency = slatency; // BT IPC will take care of accomodating the mode factor and return latency
Preetam Singh Ranawat79c514e2018-12-16 18:49:34 +05302747 break;
Aalique Grahame22e49102018-12-18 14:23:57 -08002748 case CODEC_TYPE_PCM:
2749 latency = ENCODER_LATENCY_PCM;
2750 latency += DEFAULT_SINK_LATENCY_PCM;
2751 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08002752 default:
2753 latency = 200;
2754 break;
2755 }
2756 return latency;
2757}
Aalique Grahame22e49102018-12-18 14:23:57 -08002758
Arun Mirpurib1bec9c2019-01-29 16:42:45 -08002759int a2dp_get_parameters(struct str_parms *query,
Aalique Grahame22e49102018-12-18 14:23:57 -08002760 struct str_parms *reply)
2761{
2762 int ret, val = 0;
2763 char value[32]={0};
2764
2765 ret = str_parms_get_str(query, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED,
2766 value, sizeof(value));
2767 if (ret >= 0) {
2768 val = a2dp.is_a2dp_offload_supported;
2769 str_parms_add_int(reply, AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED, val);
2770 ALOGV("%s: called ... isReconfigA2dpSupported %d", __func__, val);
2771 }
2772
2773 return 0;
2774}