blob: 3b4acd823423d59fccdca2925c88f5f69bf080ba [file] [log] [blame]
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301/*
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +05302* Copyright (c) 2015-2018, 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*/
29#define LOG_TAG "split_a2dp"
30/*#define LOG_NDEBUG 0*/
31#define LOG_NDDEBUG 0
32#include <errno.h>
33#include <cutils/log.h>
34#include <dlfcn.h>
35#include "audio_hw.h"
36#include "platform.h"
37#include "platform_api.h"
Manish Dewangan6a252632017-12-04 17:27:44 +053038#include "audio_extn.h"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053039#include <stdlib.h>
40#include <cutils/str_parms.h>
41#include <hardware/audio.h>
42#include <hardware/hardware.h>
43#include <cutils/properties.h>
44
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053045#ifdef DYNAMIC_LOG_ENABLED
46#include <log_xml_parser.h>
47#define LOG_MASK HAL_MOD_FILE_A2DP
48#include <log_utils.h>
49#endif
50
Naresh Tanniru9d027a62015-03-13 01:32:10 +053051#ifdef SPLIT_A2DP_ENABLED
52#define AUDIO_PARAMETER_A2DP_STARTED "A2dpStarted"
53#define BT_IPC_LIB_NAME "libbthost_if.so"
54#define ENC_MEDIA_FMT_NONE 0
55#define ENC_MEDIA_FMT_AAC 0x00010DA6
56#define ENC_MEDIA_FMT_APTX 0x000131ff
57#define ENC_MEDIA_FMT_APTX_HD 0x00013200
58#define ENC_MEDIA_FMT_SBC 0x00010BF2
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +053059#define ENC_MEDIA_FMT_CELT 0x00013221
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +053060#define ENC_MEDIA_FMT_LDAC 0x00013224
Naresh Tanniru9d027a62015-03-13 01:32:10 +053061#define MEDIA_FMT_AAC_AOT_LC 2
62#define MEDIA_FMT_AAC_AOT_SBR 5
63#define MEDIA_FMT_AAC_AOT_PS 29
Naresh Tanniru9d027a62015-03-13 01:32:10 +053064#define PCM_CHANNEL_L 1
65#define PCM_CHANNEL_R 2
66#define PCM_CHANNEL_C 3
67#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
68#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
69#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
70#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
71#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
72#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
73#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +053074#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +053075#define MIXER_SCRAMBLER_MODE "AFE Scrambler Mode"
76#define MIXER_SAMPLE_RATE "BT SampleRate"
77#define MIXER_AFE_IN_CHANNELS "AFE Input Channels"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053078#define MIXER_ENC_FMT_SBC "SBC"
79#define MIXER_ENC_FMT_AAC "AAC"
80#define MIXER_ENC_FMT_APTX "APTX"
81#define MIXER_ENC_FMT_APTXHD "APTXHD"
82#define MIXER_ENC_FMT_NONE "NONE"
yidongh0515e042017-07-06 15:00:34 +080083#define ENCODER_LATENCY_SBC 10
84#define ENCODER_LATENCY_APTX 40
85#define ENCODER_LATENCY_APTX_HD 20
86#define ENCODER_LATENCY_AAC 70
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +053087//To Do: Fine Tune Encoder CELT/LDAC latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +053088#define ENCODER_LATENCY_CELT 40
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +053089#define ENCODER_LATENCY_LDAC 40
yidongh0515e042017-07-06 15:00:34 +080090#define DEFAULT_SINK_LATENCY_SBC 140
91#define DEFAULT_SINK_LATENCY_APTX 160
92#define DEFAULT_SINK_LATENCY_APTX_HD 180
93#define DEFAULT_SINK_LATENCY_AAC 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +053094//To Do: Fine Tune Default CELT/LDAC Latency.
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +053095#define DEFAULT_SINK_LATENCY_CELT 180
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +053096#define DEFAULT_SINK_LATENCY_LDAC 180
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +053097
98/*
99 * Below enum values are extended from audio_base.h to
100 * to keep encoder codec type local to bthost_ipc
101 * and audio_hal as these are intended only for handshake
102 * between IPC lib and Audio HAL.
103 */
104typedef enum {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530105 ENC_CODEC_TYPE_INVALID = AUDIO_FORMAT_INVALID, // 0xFFFFFFFFUL
106 ENC_CODEC_TYPE_AAC = AUDIO_FORMAT_AAC, // 0x04000000UL
107 ENC_CODEC_TYPE_SBC = AUDIO_FORMAT_SBC, // 0x1F000000UL
108 ENC_CODEC_TYPE_APTX = AUDIO_FORMAT_APTX, // 0x20000000UL
109 ENC_CODEC_TYPE_APTX_HD = AUDIO_FORMAT_APTX_HD, // 0x21000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530110#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700111 ENC_CODEC_TYPE_APTX_DUAL_MONO = 570425344u, // 0x22000000UL
Manish Dewangan6a252632017-12-04 17:27:44 +0530112#endif
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530113 ENC_CODEC_TYPE_LDAC = AUDIO_FORMAT_LDAC, // 0x23000000UL
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530114 ENC_CODEC_TYPE_CELT = 603979776u, // 0x24000000UL
115}enc_codec_t;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530116
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530117typedef int (*audio_stream_open_t)(void);
118typedef int (*audio_stream_close_t)(void);
119typedef int (*audio_start_stream_t)(void);
120typedef int (*audio_stop_stream_t)(void);
121typedef int (*audio_suspend_stream_t)(void);
122typedef void (*audio_handoff_triggered_t)(void);
123typedef void (*clear_a2dpsuspend_flag_t)(void);
124typedef void * (*audio_get_codec_config_t)(uint8_t *multicast_status,uint8_t *num_dev,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530125 enc_codec_t *codec_type);
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530126typedef int (*audio_check_a2dp_ready_t)(void);
yidongh0515e042017-07-06 15:00:34 +0800127typedef uint16_t (*audio_get_a2dp_sink_latency_t)(void);
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530128typedef int (*audio_is_scrambling_enabled_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530129
130enum A2DP_STATE {
131 A2DP_STATE_CONNECTED,
132 A2DP_STATE_STARTED,
133 A2DP_STATE_STOPPED,
134 A2DP_STATE_DISCONNECTED,
135};
136
137/* structure used to update a2dp state machine
138 * to communicate IPC library
139 * to store DSP encoder configuration information
140 */
141struct a2dp_data {
142 struct audio_device *adev;
143 void *bt_lib_handle;
144 audio_stream_open_t audio_stream_open;
145 audio_stream_close_t audio_stream_close;
146 audio_start_stream_t audio_start_stream;
147 audio_stop_stream_t audio_stop_stream;
148 audio_suspend_stream_t audio_suspend_stream;
149 audio_handoff_triggered_t audio_handoff_triggered;
150 clear_a2dpsuspend_flag_t clear_a2dpsuspend_flag;
151 audio_get_codec_config_t audio_get_codec_config;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530152 audio_check_a2dp_ready_t audio_check_a2dp_ready;
yidongh0515e042017-07-06 15:00:34 +0800153 audio_get_a2dp_sink_latency_t audio_get_a2dp_sink_latency;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530154 audio_is_scrambling_enabled_t audio_is_scrambling_enabled;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530155 enum A2DP_STATE bt_state;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530156 enc_codec_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530157 uint32_t enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530158 uint32_t enc_channels;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530159 bool a2dp_started;
160 bool a2dp_suspended;
161 int a2dp_total_active_session_request;
162 bool is_a2dp_offload_supported;
163 bool is_handoff_in_progress;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700164 bool is_aptx_dual_mono_supported;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530165};
166
167struct a2dp_data a2dp;
168
169/* START of DSP configurable structures
170 * These values should match with DSP interface defintion
171 */
172
173/* AAC encoder configuration structure. */
174typedef struct aac_enc_cfg_t aac_enc_cfg_t;
175
176/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
177 * supported aac_fmt_flag are ADTS/RAW
178 * supported channel_cfg are Native mode, Mono , Stereo
179 */
180struct aac_enc_cfg_t {
181 uint32_t enc_format;
182 uint32_t bit_rate;
183 uint32_t enc_mode;
184 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530185 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530186 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530187} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530188
189/* SBC encoder configuration structure. */
190typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
191
192/* supported num_subbands are 4/8
193 * supported blk_len are 4, 8, 12, 16
194 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
195 * supported alloc_method are LOUNDNESS/SNR
196 * supported bit_rate for mono channel is max 320kbps
197 * supported bit rate for stereo channel is max 512 kbps
198 */
199struct sbc_enc_cfg_t{
200 uint32_t enc_format;
201 uint32_t num_subbands;
202 uint32_t blk_len;
203 uint32_t channel_mode;
204 uint32_t alloc_method;
205 uint32_t bit_rate;
206 uint32_t sample_rate;
Manish Dewangan6a252632017-12-04 17:27:44 +0530207} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530208
209
210/* supported num_channels are Mono/Stereo
211 * supported channel_mapping for mono is CHANNEL_C
212 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
213 * custom size and reserved are not used(for future enhancement)
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700214 */
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530215struct custom_enc_cfg_t
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530216{
217 uint32_t enc_format;
218 uint32_t sample_rate;
219 uint16_t num_channels;
220 uint16_t reserved;
221 uint8_t channel_mapping[8];
222 uint32_t custom_size;
Manish Dewangan6a252632017-12-04 17:27:44 +0530223} __attribute__ ((packed));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530224
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530225struct celt_specific_enc_cfg_t
226{
227 uint32_t bit_rate;
228 uint16_t frame_size;
229 uint16_t complexity;
230 uint16_t prediction_mode;
231 uint16_t vbr_flag;
Manish Dewangan6a252632017-12-04 17:27:44 +0530232} __attribute__ ((packed));
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530233
234struct celt_enc_cfg_t
235{
236 struct custom_enc_cfg_t custom_cfg;
237 struct celt_specific_enc_cfg_t celt_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530238} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700239
240/* sync_mode introduced with APTX V2 libraries
241 * sync mode: 0x0 = stereo sync mode
242 * 0x01 = dual mono sync mode
243 * 0x02 = dual mono with no sync on either L or R codewords
244 */
245struct aptx_v2_enc_cfg_ext_t
246{
247 uint32_t sync_mode;
Manish Dewangan6a252632017-12-04 17:27:44 +0530248} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700249
250/* APTX struct for combining custom enc and V2 fields */
251struct aptx_enc_cfg_t
252{
253 struct custom_enc_cfg_t custom_cfg;
254 struct aptx_v2_enc_cfg_ext_t aptx_v2_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530255} __attribute__ ((packed));
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700256
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530257struct ldac_specific_enc_cfg_t
258{
259 uint32_t bit_rate;
260 uint16_t channel_mode;
261 uint16_t mtu;
Manish Dewangan6a252632017-12-04 17:27:44 +0530262} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530263
264struct ldac_enc_cfg_t
265{
266 struct custom_enc_cfg_t custom_cfg;
267 struct ldac_specific_enc_cfg_t ldac_cfg;
Manish Dewangan6a252632017-12-04 17:27:44 +0530268} __attribute__ ((packed));
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530269
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530270/* In LE BT source code uses system/audio.h for below
271 * structure definition. To avoid multiple definition
272 * compilation error for audiohal in LE , masking structure
273 * definition under "LINUX_ENABLED" which is defined only
274 * in LE
275 */
276#ifndef LINUX_ENABLED
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530277/* TODO: Define the following structures only for O using PLATFORM_VERSION */
278/* Information about BT SBC encoder configuration
279 * This data is used between audio HAL module and
280 * BT IPC library to configure DSP encoder
281 */
282typedef struct {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530283 uint32_t subband; /* 4, 8 */
284 uint32_t blk_len; /* 4, 8, 12, 16 */
285 uint16_t sampling_rate; /*44.1khz,48khz*/
286 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
287 uint8_t alloc; /*0(Loudness),1(SNR)*/
288 uint8_t min_bitpool; /* 2 */
289 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
290 uint32_t bitrate; /* 320kbps to 512kbps */
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530291} audio_sbc_encoder_config;
292
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530293/* Information about BT APTX encoder configuration
294 * This data is used between audio HAL module and
295 * BT IPC library to configure DSP encoder
296 */
297typedef struct {
298 uint16_t sampling_rate;
299 uint8_t channels;
300 uint32_t bitrate;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700301} audio_aptx_default_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530302
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700303typedef struct {
304 uint16_t sampling_rate;
305 uint8_t channels;
306 uint32_t bitrate;
307 uint32_t sync_mode;
308} audio_aptx_dual_mono_config;
309
310typedef union {
311 audio_aptx_default_config *default_cfg;
312 audio_aptx_dual_mono_config *dual_mono_cfg;
313} audio_aptx_encoder_config;
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530314
315/* Information about BT AAC encoder configuration
316 * This data is used between audio HAL module and
317 * BT IPC library to configure DSP encoder
318 */
319typedef struct {
320 uint32_t enc_mode; /* LC, SBR, PS */
321 uint16_t format_flag; /* RAW, ADTS */
322 uint16_t channels; /* 1-Mono, 2-Stereo */
323 uint32_t sampling_rate;
324 uint32_t bitrate;
325} audio_aac_encoder_config;
Sachin Mohan Gadag1657c052017-09-13 16:00:27 +0530326#endif
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530327
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530328/* Information about BT CELT encoder configuration
329 * This data is used between audio HAL module and
330 * BT IPC library to configure DSP encoder
331 */
332typedef struct {
333 uint32_t sampling_rate; /* 32000 - 48000, 48000 */
334 uint16_t channels; /* 1-Mono, 2-Stereo, 2*/
335 uint16_t frame_size; /* 64-128-256-512, 512 */
336 uint16_t complexity; /* 0-10, 1 */
337 uint16_t prediction_mode; /* 0-1-2, 0 */
338 uint16_t vbr_flag; /* 0-1, 0*/
339 uint32_t bitrate; /*32000 - 1536000, 139500*/
340} audio_celt_encoder_config;
341
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530342/* Information about BT LDAC encoder configuration
343 * This data is used between audio HAL module and
344 * BT IPC library to configure DSP encoder
345 */
346typedef struct {
347 uint32_t sampling_rate; /*44100,48000,88200,96000*/
348 uint32_t bit_rate; /*303000,606000,909000(in bits per second)*/
349 uint16_t channel_mode; /* 0, 4, 2, 1*/
350 uint16_t mtu; /*679*/
351} audio_ldac_encoder_config;
352
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530353/*********** END of DSP configurable structures ********************/
354
355/* API to identify DSP encoder captabilities */
356static void a2dp_offload_codec_cap_parser(char *value)
357{
Naresh Tannirucd2353e2016-08-19 00:37:25 +0530358 char *tok = NULL,*saveptr;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530359
Naresh Tannirucd2353e2016-08-19 00:37:25 +0530360 tok = strtok_r(value, "-", &saveptr);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530361 while (tok != NULL) {
362 if (strcmp(tok, "sbc") == 0) {
363 ALOGD("%s: SBC offload supported\n",__func__);
364 a2dp.is_a2dp_offload_supported = true;
365 break;
366 } else if (strcmp(tok, "aptx") == 0) {
367 ALOGD("%s: aptx offload supported\n",__func__);
368 a2dp.is_a2dp_offload_supported = true;
369 break;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700370 } else if (strcmp(tok, "aptxtws") == 0) {
371 ALOGD("%s: aptx dual mono offload supported\n",__func__);
372 a2dp.is_a2dp_offload_supported = true;
373 break;
Naresh Tannirued694c82017-02-07 17:01:28 +0530374 } else if (strcmp(tok, "aptxhd") == 0) {
375 ALOGD("%s: aptx HD offload supported\n",__func__);
376 a2dp.is_a2dp_offload_supported = true;
377 break;
378 } else if (strcmp(tok, "aac") == 0) {
379 ALOGD("%s: aac offload supported\n",__func__);
380 a2dp.is_a2dp_offload_supported = true;
381 break;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530382 } else if (strcmp(tok, "celt") == 0) {
383 ALOGD("%s: celt offload supported\n",__func__);
384 a2dp.is_a2dp_offload_supported = true;
385 break;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530386 } else if (strcmp(tok, "ldac") == 0) {
387 ALOGD("%s: ldac offload supported\n",__func__);
388 a2dp.is_a2dp_offload_supported = true;
389 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530390 }
Naresh Tannirucd2353e2016-08-19 00:37:25 +0530391 tok = strtok_r(NULL, "-", &saveptr);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530392 };
393}
394
395static void update_offload_codec_capabilities()
396{
397 char value[PROPERTY_VALUE_MAX] = {'\0'};
398
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700399 property_get("persist.vendor.bt.a2dp_offload_cap", value, "false");
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530400 ALOGD("get_offload_codec_capabilities = %s",value);
401 a2dp.is_a2dp_offload_supported =
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700402 property_get_bool("persist.vendor.bt.a2dp_offload_cap", false);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530403 if (strcmp(value, "false") != 0)
404 a2dp_offload_codec_cap_parser(value);
405 ALOGD("%s: codec cap = %s",__func__,value);
406}
407
408/* API to open BT IPC library to start IPC communication */
409static void open_a2dp_output()
410{
411 int ret = 0;
412
413 ALOGD(" Open A2DP output start ");
414 if (a2dp.bt_lib_handle == NULL){
415 ALOGD(" Requesting for BT lib handle");
416 a2dp.bt_lib_handle = dlopen(BT_IPC_LIB_NAME, RTLD_NOW);
417
418 if (a2dp.bt_lib_handle == NULL) {
419 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_LIB_NAME);
420 ret = -ENOSYS;
421 goto init_fail;
422 } else {
423 a2dp.audio_stream_open = (audio_stream_open_t)
424 dlsym(a2dp.bt_lib_handle, "audio_stream_open");
425 a2dp.audio_start_stream = (audio_start_stream_t)
426 dlsym(a2dp.bt_lib_handle, "audio_start_stream");
427 a2dp.audio_get_codec_config = (audio_get_codec_config_t)
428 dlsym(a2dp.bt_lib_handle, "audio_get_codec_config");
429 a2dp.audio_suspend_stream = (audio_suspend_stream_t)
430 dlsym(a2dp.bt_lib_handle, "audio_suspend_stream");
431 a2dp.audio_handoff_triggered = (audio_handoff_triggered_t)
432 dlsym(a2dp.bt_lib_handle, "audio_handoff_triggered");
433 a2dp.clear_a2dpsuspend_flag = (clear_a2dpsuspend_flag_t)
434 dlsym(a2dp.bt_lib_handle, "clear_a2dpsuspend_flag");
435 a2dp.audio_stop_stream = (audio_stop_stream_t)
436 dlsym(a2dp.bt_lib_handle, "audio_stop_stream");
437 a2dp.audio_stream_close = (audio_stream_close_t)
438 dlsym(a2dp.bt_lib_handle, "audio_stream_close");
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530439 a2dp.audio_check_a2dp_ready = (audio_check_a2dp_ready_t)
440 dlsym(a2dp.bt_lib_handle,"audio_check_a2dp_ready");
yidongh0515e042017-07-06 15:00:34 +0800441 a2dp.audio_get_a2dp_sink_latency = (audio_get_a2dp_sink_latency_t)
442 dlsym(a2dp.bt_lib_handle,"audio_get_a2dp_sink_latency");
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530443 a2dp.audio_is_scrambling_enabled = (audio_is_scrambling_enabled_t)
444 dlsym(a2dp.bt_lib_handle,"audio_is_scrambling_enabled");
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530445 }
446 }
447
448 if (a2dp.bt_lib_handle && a2dp.audio_stream_open) {
449 if (a2dp.bt_state == A2DP_STATE_DISCONNECTED) {
450 ALOGD("calling BT stream open");
451 ret = a2dp.audio_stream_open();
452 if(ret != 0) {
453 ALOGE("Failed to open output stream for a2dp: status %d", ret);
454 goto init_fail;
455 }
456 a2dp.bt_state = A2DP_STATE_CONNECTED;
457 } else {
458 ALOGD("Called a2dp open with improper state, Ignoring request state %d", a2dp.bt_state);
459 }
460 } else {
461 ALOGE("a2dp handle is not identified, Ignoring open request");
462 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
463 goto init_fail;
464 }
465
466init_fail:
467 if(ret != 0 && (a2dp.bt_lib_handle != NULL)) {
468 dlclose(a2dp.bt_lib_handle);
469 a2dp.bt_lib_handle = NULL;
470 }
471}
472
473static int close_a2dp_output()
474{
475 ALOGV("%s\n",__func__);
476 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_close)) {
477 ALOGE("a2dp handle is not identified, Ignoring close request");
478 return -ENOSYS;
479 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530480 if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530481 ALOGD("calling BT stream close");
482 if(a2dp.audio_stream_close() == false)
483 ALOGE("failed close a2dp control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530484 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530485 a2dp.a2dp_started = false;
486 a2dp.a2dp_total_active_session_request = 0;
487 a2dp.a2dp_suspended = false;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530488 a2dp.bt_encoder_format = ENC_CODEC_TYPE_INVALID;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530489 a2dp.enc_sampling_rate = 48000;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530490 a2dp.enc_channels = 2;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530491 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530492
493 return 0;
494}
495
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530496static void a2dp_check_and_set_scrambler()
497{
498 bool scrambler_mode = false;
499 struct mixer_ctl *ctrl_scrambler_mode = NULL;
500 if (a2dp.audio_is_scrambling_enabled && (a2dp.bt_state != A2DP_STATE_DISCONNECTED))
501 scrambler_mode = a2dp.audio_is_scrambling_enabled();
502
503 if (scrambler_mode) {
504 //enable scrambler in dsp
505 ctrl_scrambler_mode = mixer_get_ctl_by_name(a2dp.adev->mixer,
506 MIXER_SCRAMBLER_MODE);
507 if (!ctrl_scrambler_mode) {
508 ALOGE(" ERROR scrambler mode mixer control not identifed");
509 return;
510 } else {
511 if (mixer_ctl_set_value(ctrl_scrambler_mode, 0, true) != 0) {
512 ALOGE("%s: Could not set scrambler mode", __func__);
513 return;
514 }
515 }
516 }
517}
518
519static void a2dp_set_backend_cfg()
520{
521 char *rate_str = NULL, *in_channels = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530522 uint32_t sampling_rate = a2dp.enc_sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530523 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530524
525 //For LDAC encoder open slimbus port at 96Khz for 48Khz input
526 //and 88.2Khz for 44.1Khz input.
527 if ((a2dp.bt_encoder_format == ENC_CODEC_TYPE_LDAC) &&
528 (sampling_rate == 48000 || sampling_rate == 44100 )) {
529 sampling_rate = sampling_rate *2;
530 }
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530531 //Configure backend sampling rate
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +0530532 switch (sampling_rate) {
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530533 case 44100:
534 rate_str = "KHZ_44P1";
535 break;
536 case 48000:
537 rate_str = "KHZ_48";
538 break;
539 case 88200:
540 rate_str = "KHZ_88P2";
541 break;
542 case 96000:
543 rate_str = "KHZ_96";
544 break;
545 default:
546 rate_str = "KHZ_48";
547 break;
548 }
549
550 ALOGD("%s: set backend sample rate =%s", __func__, rate_str);
551 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
552 MIXER_SAMPLE_RATE);
553 if (!ctl_sample_rate) {
554 ALOGE(" ERROR backend sample rate mixer control not identifed");
555 return;
556 } else {
557 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
558 ALOGE("%s: Failed to set backend sample rate =%s", __func__, rate_str);
559 return;
560 }
561 }
562
563 //Configure AFE input channels
564 switch (a2dp.enc_channels) {
565 case 1:
566 in_channels = "One";
567 break;
568 case 2:
569 default:
570 in_channels = "Two";
571 break;
572 }
573
574 ALOGD("%s: set afe input channels =%d", __func__, a2dp.enc_channels);
575 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
576 MIXER_AFE_IN_CHANNELS);
577 if (!ctrl_in_channels) {
578 ALOGE(" ERROR AFE input channels mixer control not identifed");
579 return;
580 } else {
581 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
582 ALOGE("%s: Failed to set AFE in channels =%d", __func__, a2dp.enc_channels);
583 return;
584 }
585 }
586}
587
588static void a2dp_reset_backend_cfg()
589{
590 char *rate_str = "KHZ_8", *in_channels = "Zero";
591 struct mixer_ctl *ctl_sample_rate = NULL, *ctrl_in_channels = NULL;
592
593 //reset backend sampling rate
594 ALOGD("%s: reset backend sample rate =%s", __func__, rate_str);
595 ctl_sample_rate = mixer_get_ctl_by_name(a2dp.adev->mixer,
596 MIXER_SAMPLE_RATE);
597 if (!ctl_sample_rate) {
598 ALOGE(" ERROR backend sample rate mixer control not identifed");
599 return;
600 } else {
601 if (mixer_ctl_set_enum_by_string(ctl_sample_rate, rate_str) != 0) {
602 ALOGE("%s: Failed to reset backend sample rate =%s", __func__, rate_str);
603 return;
604 }
605 }
606
607 //reset AFE input channels
608 ALOGD("%s: reset afe input channels =%s", __func__, in_channels);
609 ctrl_in_channels = mixer_get_ctl_by_name(a2dp.adev->mixer,
610 MIXER_AFE_IN_CHANNELS);
611 if (!ctrl_in_channels) {
612 ALOGE(" ERROR AFE input channels mixer control not identifed");
613 return;
614 } else {
615 if (mixer_ctl_set_enum_by_string(ctrl_in_channels, in_channels) != 0) {
616 ALOGE("%s: Failed to reset AFE in channels =%d", __func__, a2dp.enc_channels);
617 return;
618 }
619 }
620}
621
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530622/* API to configure SBC DSP encoder */
623bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
624{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530625 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530626 struct sbc_enc_cfg_t sbc_dsp_cfg;
627 bool is_configured = false;
628 int ret = 0;
629
630 if(sbc_bt_cfg == NULL)
631 return false;
632
633 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
634 if (!ctl_enc_data) {
635 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
636 is_configured = false;
637 goto fail;
638 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530639 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
640 sbc_dsp_cfg.enc_format = ENC_MEDIA_FMT_SBC;
641 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
642 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
643 switch(sbc_bt_cfg->channels) {
644 case 0:
645 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
646 break;
647 case 1:
648 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
649 break;
650 case 3:
651 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
652 break;
653 case 2:
654 default:
655 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
656 break;
657 }
658 if (sbc_bt_cfg->alloc)
659 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
660 else
661 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
662 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
663 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
664 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
665 sizeof(struct sbc_enc_cfg_t));
666 if (ret != 0) {
667 ALOGE("%s: failed to set SBC encoder config", __func__);
668 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530669 goto fail;
670 }
671 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
672 MIXER_ENC_BIT_FORMAT);
673 if (!ctrl_bit_format) {
674 ALOGE(" ERROR bit format CONFIG data mixer control not identifed");
675 is_configured = false;
676 goto fail;
677 }
678 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
679 if (ret != 0) {
680 ALOGE("%s: Failed to set bit format to encoder", __func__);
681 is_configured = false;
682 goto fail;
683 }
684 is_configured = true;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530685 a2dp.bt_encoder_format = ENC_CODEC_TYPE_SBC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530686 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530687
688 if (sbc_dsp_cfg.channel_mode == MEDIA_FMT_SBC_CHANNEL_MODE_MONO)
689 a2dp.enc_channels = 1;
690 else
691 a2dp.enc_channels = 2;
692
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530693 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
694 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530695fail:
696 return is_configured;
697}
698
Manish Dewangan6a252632017-12-04 17:27:44 +0530699#ifndef LINUX_ENABLED
700static int update_aptx_dsp_config_v2(struct aptx_enc_cfg_t *aptx_dsp_cfg,
701 audio_aptx_encoder_config *aptx_bt_cfg)
702{
703 int ret = 0;
704
705 if(aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
706 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
707 aptx_dsp_cfg, aptx_bt_cfg);
708 return -EINVAL;
709 }
710
711 memset(aptx_dsp_cfg, 0x0, sizeof(struct aptx_enc_cfg_t));
712 aptx_dsp_cfg->custom_cfg.enc_format = ENC_MEDIA_FMT_APTX;
713
714 if (!a2dp.is_aptx_dual_mono_supported) {
715 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->default_cfg->sampling_rate;
716 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->default_cfg->channels;
717 } else {
718 aptx_dsp_cfg->custom_cfg.sample_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
719 aptx_dsp_cfg->custom_cfg.num_channels = aptx_bt_cfg->dual_mono_cfg->channels;
720 aptx_dsp_cfg->aptx_v2_cfg.sync_mode = aptx_bt_cfg->dual_mono_cfg->sync_mode;
721 }
722
723 switch(aptx_dsp_cfg->custom_cfg.num_channels) {
724 case 1:
725 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
726 break;
727 case 2:
728 default:
729 aptx_dsp_cfg->custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
730 aptx_dsp_cfg->custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
731 break;
732 }
733 a2dp.enc_channels = aptx_dsp_cfg->custom_cfg.num_channels;
734 if (!a2dp.is_aptx_dual_mono_supported) {
735 a2dp.enc_sampling_rate = aptx_bt_cfg->default_cfg->sampling_rate;
736 ALOGV("Successfully updated APTX enc format with samplingrate: %d \
737 channels:%d", aptx_dsp_cfg->custom_cfg.sample_rate,
738 aptx_dsp_cfg->custom_cfg.num_channels);
739 } else {
740 a2dp.enc_sampling_rate = aptx_bt_cfg->dual_mono_cfg->sampling_rate;
741 ALOGV("Successfully updated APTX dual mono enc format with \
742 samplingrate: %d channels:%d syncmode %d",
743 aptx_dsp_cfg->custom_cfg.sample_rate,
744 aptx_dsp_cfg->custom_cfg.num_channels,
745 aptx_dsp_cfg->aptx_v2_cfg.sync_mode);
746 }
747 return ret;
748}
749#else
750static int update_aptx_dsp_config_v1(struct custom_enc_cfg_t *aptx_dsp_cfg,
751 audio_aptx_encoder_config *aptx_bt_cfg)
752{
753 int ret = 0;
754
755 if(aptx_dsp_cfg == NULL || aptx_bt_cfg == NULL) {
756 ALOGE("Invalid param, aptx_dsp_cfg %p aptx_bt_cfg %p",
757 aptx_dsp_cfg, aptx_bt_cfg);
758 return -EINVAL;
759 }
760
761 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
762 aptx_dsp_cfg->enc_format = ENC_MEDIA_FMT_APTX;
763 aptx_dsp_cfg->sample_rate = aptx_bt_cfg->sampling_rate;
764 aptx_dsp_cfg->num_channels = aptx_bt_cfg->channels;
765 switch(aptx_dsp_cfg->num_channels) {
766 case 1:
767 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_C;
768 break;
769 case 2:
770 default:
771 aptx_dsp_cfg->channel_mapping[0] = PCM_CHANNEL_L;
772 aptx_dsp_cfg->channel_mapping[1] = PCM_CHANNEL_R;
773 break;
774 }
775
776 ALOGV("Updated APTX enc format with samplingrate: %d channels:%d",
777 aptx_dsp_cfg->sample_rate, aptx_dsp_cfg->num_channels);
778
779 return ret;
780}
781#endif
782
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530783/* API to configure APTX DSP encoder */
784bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
785{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530786 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
Manish Dewangan6a252632017-12-04 17:27:44 +0530787 int mixer_size;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530788 bool is_configured = false;
789 int ret = 0;
Manish Dewangan6a252632017-12-04 17:27:44 +0530790 int sample_rate_backup;
Preetam Singh Ranawat109bb3c2018-01-30 12:12:02 +0530791
792 if(aptx_bt_cfg == NULL)
793 return false;
794
Manish Dewangan6a252632017-12-04 17:27:44 +0530795#ifndef LINUX_ENABLED
796 struct aptx_enc_cfg_t aptx_dsp_cfg;
797 mixer_size = sizeof(struct aptx_enc_cfg_t);
798 sample_rate_backup = aptx_bt_cfg->default_cfg->sampling_rate;
799#else
800 struct custom_enc_cfg_t aptx_dsp_cfg;
801 mixer_size = sizeof(struct custom_enc_cfg_t);
802 sample_rate_backup = aptx_bt_cfg->sampling_rate;
803#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530804
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530805 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
806 if (!ctl_enc_data) {
807 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
808 is_configured = false;
809 goto fail;
810 }
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700811
Manish Dewangan6a252632017-12-04 17:27:44 +0530812#ifndef LINUX_ENABLED
813 ret = update_aptx_dsp_config_v2(&aptx_dsp_cfg, aptx_bt_cfg);
814#else
815 ret = update_aptx_dsp_config_v1(&aptx_dsp_cfg, aptx_bt_cfg);
816#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700817
Manish Dewangan6a252632017-12-04 17:27:44 +0530818 if (ret) {
819 is_configured = false;
820 goto fail;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530821 }
822 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Manish Dewangan6a252632017-12-04 17:27:44 +0530823 mixer_size);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530824 if (ret != 0) {
825 ALOGE("%s: Failed to set APTX encoder config", __func__);
826 is_configured = false;
827 goto fail;
828 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530829 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
830 MIXER_ENC_BIT_FORMAT);
831 if (!ctrl_bit_format) {
832 ALOGE("ERROR bit format CONFIG data mixer control not identifed");
833 is_configured = false;
834 goto fail;
835 } else {
836 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
837 if (ret != 0) {
838 ALOGE("%s: Failed to set bit format to encoder", __func__);
839 is_configured = false;
840 goto fail;
841 }
842 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530843 is_configured = true;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530844 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530845fail:
Manish Dewangan6a252632017-12-04 17:27:44 +0530846 /*restore sample rate */
847 if(!is_configured)
848 a2dp.enc_sampling_rate = sample_rate_backup;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530849 return is_configured;
850}
851
852/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530853 */
Manish Dewangan6a252632017-12-04 17:27:44 +0530854#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -0700855bool configure_aptx_hd_enc_format(audio_aptx_default_config *aptx_bt_cfg)
Manish Dewangan6a252632017-12-04 17:27:44 +0530856#else
857bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
858#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530859{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530860 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530861 struct custom_enc_cfg_t aptx_dsp_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530862 bool is_configured = false;
863 int ret = 0;
864
865 if(aptx_bt_cfg == NULL)
866 return false;
867
868 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
869 if (!ctl_enc_data) {
870 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
871 is_configured = false;
872 goto fail;
873 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530874
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530875 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530876 aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX_HD;
877 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
878 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
879 switch(aptx_dsp_cfg.num_channels) {
880 case 1:
881 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
882 break;
883 case 2:
884 default:
885 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
886 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
887 break;
888 }
889 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530890 sizeof(struct custom_enc_cfg_t));
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530891 if (ret != 0) {
892 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
893 is_configured = false;
894 goto fail;
895 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530896 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_BIT_FORMAT);
897 if (!ctrl_bit_format) {
898 ALOGE(" ERROR bit format CONFIG data mixer control not identifed");
899 is_configured = false;
900 goto fail;
901 }
902 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S24_LE");
903 if (ret != 0) {
904 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
905 is_configured = false;
906 goto fail;
907 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530908 is_configured = true;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530909 a2dp.bt_encoder_format = ENC_CODEC_TYPE_APTX_HD;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530910 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530911 a2dp.enc_channels = aptx_bt_cfg->channels;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530912 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
913 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530914fail:
915 return is_configured;
916}
917
918/* API to configure AAC DSP encoder */
919bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
920{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530921 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530922 struct aac_enc_cfg_t aac_dsp_cfg;
923 bool is_configured = false;
924 int ret = 0;
925
926 if(aac_bt_cfg == NULL)
927 return false;
928
929 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
930 if (!ctl_enc_data) {
931 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
932 is_configured = false;
933 goto fail;
934 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530935 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
936 aac_dsp_cfg.enc_format = ENC_MEDIA_FMT_AAC;
937 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530938 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530939 switch(aac_bt_cfg->enc_mode) {
940 case 0:
941 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
942 break;
943 case 2:
944 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
945 break;
946 case 1:
947 default:
948 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
949 break;
950 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530951 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530952 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
953 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
954 sizeof(struct aac_enc_cfg_t));
955 if (ret != 0) {
956 ALOGE("%s: failed to set SBC encoder config", __func__);
957 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530958 goto fail;
959 }
960 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
961 MIXER_ENC_BIT_FORMAT);
962 if (!ctrl_bit_format) {
963 is_configured = false;
964 ALOGE(" ERROR bit format CONFIG data mixer control not identifed");
965 goto fail;
966 }
967 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
968 if (ret != 0) {
969 ALOGE("%s: Failed to set bit format to encoder", __func__);
970 is_configured = false;
971 goto fail;
972 }
973 is_configured = true;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530974 a2dp.bt_encoder_format = ENC_CODEC_TYPE_AAC;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530975 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +0530976 a2dp.enc_channels = aac_bt_cfg->channels;;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530977 ALOGV("Successfully updated AAC enc format with samplingrate: %d channels:%d",
978 aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530979fail:
980 return is_configured;
981}
982
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +0530983bool configure_celt_enc_format(audio_celt_encoder_config *celt_bt_cfg)
984{
985 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
986 struct celt_enc_cfg_t celt_dsp_cfg;
987 bool is_configured = false;
988 int ret = 0;
989 if(celt_bt_cfg == NULL)
990 return false;
991
992 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
993 if (!ctl_enc_data) {
994 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
995 is_configured = false;
996 goto fail;
997 }
998 memset(&celt_dsp_cfg, 0x0, sizeof(struct celt_enc_cfg_t));
999
1000 celt_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_CELT;
1001 celt_dsp_cfg.custom_cfg.sample_rate = celt_bt_cfg->sampling_rate;
1002 celt_dsp_cfg.custom_cfg.num_channels = celt_bt_cfg->channels;
1003 switch(celt_dsp_cfg.custom_cfg.num_channels) {
1004 case 1:
1005 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1006 break;
1007 case 2:
1008 default:
1009 celt_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1010 celt_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1011 break;
1012 }
1013
1014 celt_dsp_cfg.custom_cfg.custom_size = sizeof(struct celt_enc_cfg_t);
1015
1016 celt_dsp_cfg.celt_cfg.frame_size = celt_bt_cfg->frame_size;
1017 celt_dsp_cfg.celt_cfg.complexity = celt_bt_cfg->complexity;
1018 celt_dsp_cfg.celt_cfg.prediction_mode = celt_bt_cfg->prediction_mode;
1019 celt_dsp_cfg.celt_cfg.vbr_flag = celt_bt_cfg->vbr_flag;
1020 celt_dsp_cfg.celt_cfg.bit_rate = celt_bt_cfg->bitrate;
1021
1022 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&celt_dsp_cfg,
1023 sizeof(struct celt_enc_cfg_t));
1024 if (ret != 0) {
1025 ALOGE("%s: Failed to set CELT encoder config", __func__);
1026 is_configured = false;
1027 goto fail;
1028 }
1029 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_BIT_FORMAT);
1030 if (!ctrl_bit_format) {
1031 ALOGE(" ERROR bit format CONFIG data mixer control not identifed");
1032 is_configured = false;
1033 goto fail;
1034 }
1035 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1036 if (ret != 0) {
1037 ALOGE("%s: Failed to set bit format to encoder", __func__);
1038 is_configured = false;
1039 goto fail;
1040 }
1041 is_configured = true;
1042 a2dp.bt_encoder_format = ENC_CODEC_TYPE_CELT;
1043 a2dp.enc_sampling_rate = celt_bt_cfg->sampling_rate;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301044 a2dp.enc_channels = celt_bt_cfg->channels;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301045 ALOGV("Successfully updated CELT encformat with samplingrate: %d channels:%d",
1046 celt_dsp_cfg.custom_cfg.sample_rate, celt_dsp_cfg.custom_cfg.num_channels);
1047fail:
1048 return is_configured;
1049}
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301050
1051bool configure_ldac_enc_format(audio_ldac_encoder_config *ldac_bt_cfg)
1052{
1053 struct mixer_ctl *ldac_enc_data = NULL, *ctrl_bit_format = NULL;
1054 struct ldac_enc_cfg_t ldac_dsp_cfg;
1055 bool is_configured = false;
1056 int ret = 0;
1057 if(ldac_bt_cfg == NULL)
1058 return false;
1059
1060 ldac_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
1061 if (!ldac_enc_data) {
1062 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
1063 is_configured = false;
1064 goto fail;
1065 }
1066 memset(&ldac_dsp_cfg, 0x0, sizeof(struct ldac_enc_cfg_t));
1067
1068 ldac_dsp_cfg.custom_cfg.enc_format = ENC_MEDIA_FMT_LDAC;
1069 ldac_dsp_cfg.custom_cfg.sample_rate = ldac_bt_cfg->sampling_rate;
1070 ldac_dsp_cfg.ldac_cfg.channel_mode = ldac_bt_cfg->channel_mode;
1071 switch(ldac_dsp_cfg.ldac_cfg.channel_mode) {
1072 case 4:
1073 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_C;
1074 ldac_dsp_cfg.custom_cfg.num_channels = 1;
1075 break;
1076 case 2:
1077 case 1:
1078 default:
1079 ldac_dsp_cfg.custom_cfg.channel_mapping[0] = PCM_CHANNEL_L;
1080 ldac_dsp_cfg.custom_cfg.channel_mapping[1] = PCM_CHANNEL_R;
1081 ldac_dsp_cfg.custom_cfg.num_channels = 2;
1082 break;
1083 }
1084
1085 ldac_dsp_cfg.custom_cfg.custom_size = sizeof(struct ldac_enc_cfg_t);
1086 ldac_dsp_cfg.ldac_cfg.mtu = ldac_bt_cfg->mtu;
1087 ldac_dsp_cfg.ldac_cfg.bit_rate = ldac_bt_cfg->bit_rate;
1088 ret = mixer_ctl_set_array(ldac_enc_data, (void *)&ldac_dsp_cfg,
1089 sizeof(struct ldac_enc_cfg_t));
1090 if (ret != 0) {
1091 ALOGE("%s: Failed to set LDAC encoder config", __func__);
1092 is_configured = false;
1093 goto fail;
1094 }
1095 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_BIT_FORMAT);
1096 if (!ctrl_bit_format) {
1097 ALOGE(" ERROR bit format CONFIG data mixer control not identifed");
1098 is_configured = false;
1099 goto fail;
1100 }
1101 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1102 if (ret != 0) {
1103 ALOGE("%s: Failed to set bit format to encoder", __func__);
1104 is_configured = false;
1105 goto fail;
1106 }
1107 is_configured = true;
1108 a2dp.bt_encoder_format = ENC_CODEC_TYPE_LDAC;
1109 a2dp.enc_sampling_rate = ldac_bt_cfg->sampling_rate;
1110 a2dp.enc_channels = ldac_dsp_cfg.custom_cfg.num_channels;
1111 ALOGV("Successfully updated LDAC encformat with samplingrate: %d channels:%d",
1112 ldac_dsp_cfg.custom_cfg.sample_rate, ldac_dsp_cfg.custom_cfg.num_channels);
1113fail:
1114 return is_configured;
1115}
1116
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301117bool configure_a2dp_encoder_format()
1118{
1119 void *codec_info = NULL;
1120 uint8_t multi_cast = 0, num_dev = 1;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301121 enc_codec_t codec_type = ENC_CODEC_TYPE_INVALID;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301122 bool is_configured = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07001123 audio_aptx_encoder_config aptx_encoder_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301124
1125 if (!a2dp.audio_get_codec_config) {
1126 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
1127 return false;
1128 }
1129 ALOGD("configure_a2dp_encoder_format start");
1130 codec_info = a2dp.audio_get_codec_config(&multi_cast, &num_dev,
1131 &codec_type);
1132
1133 switch(codec_type) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301134 case ENC_CODEC_TYPE_SBC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301135 ALOGD(" Received SBC encoder supported BT device");
1136 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07001137 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301138 break;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301139 case ENC_CODEC_TYPE_APTX:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301140 ALOGD(" Received APTX encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05301141#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07001142 a2dp.is_aptx_dual_mono_supported = false;
1143 aptx_encoder_cfg.default_cfg = (audio_aptx_default_config *)codec_info;
Manish Dewangan6a252632017-12-04 17:27:44 +05301144#endif
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301145 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07001146 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301147 break;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301148 case ENC_CODEC_TYPE_APTX_HD:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301149 ALOGD(" Received APTX HD encoder supported BT device");
Manish Dewangan6a252632017-12-04 17:27:44 +05301150#ifndef LINUX_ENABLED
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301151 is_configured =
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07001152 configure_aptx_hd_enc_format((audio_aptx_default_config *)codec_info);
Manish Dewangan6a252632017-12-04 17:27:44 +05301153#else
1154 is_configured =
1155 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
1156#endif
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07001157 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05301158#ifndef LINUX_ENABLED
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07001159 case ENC_CODEC_TYPE_APTX_DUAL_MONO:
1160 ALOGD(" Received APTX dual mono encoder supported BT device");
1161 a2dp.is_aptx_dual_mono_supported = true;
1162 aptx_encoder_cfg.dual_mono_cfg = (audio_aptx_dual_mono_config *)codec_info;
1163 is_configured =
1164 configure_aptx_enc_format(&aptx_encoder_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301165 break;
Manish Dewangan6a252632017-12-04 17:27:44 +05301166#endif
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301167 case ENC_CODEC_TYPE_AAC:
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301168 ALOGD(" Received AAC encoder supported BT device");
1169 is_configured =
1170 configure_aac_enc_format((audio_aac_encoder_config *)codec_info);
1171 break;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301172 case ENC_CODEC_TYPE_CELT:
1173 ALOGD(" Received CELT encoder supported BT device");
1174 is_configured =
1175 configure_celt_enc_format((audio_celt_encoder_config *)codec_info);
1176 break;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301177 case ENC_CODEC_TYPE_LDAC:
1178 ALOGD(" Received LDAC encoder supported BT device");
1179 is_configured =
1180 configure_ldac_enc_format((audio_ldac_encoder_config *)codec_info);
1181 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301182 default:
1183 ALOGD(" Received Unsupported encoder formar");
1184 is_configured = false;
1185 break;
1186 }
1187 return is_configured;
1188}
1189
1190int audio_extn_a2dp_start_playback()
1191{
1192 int ret = 0;
1193
1194 ALOGD("audio_extn_a2dp_start_playback start");
1195
1196 if(!(a2dp.bt_lib_handle && a2dp.audio_start_stream
1197 && a2dp.audio_get_codec_config)) {
1198 ALOGE("a2dp handle is not identified, Ignoring start request");
1199 return -ENOSYS;
1200 }
1201
1202 if(a2dp.a2dp_suspended == true) {
1203 //session will be restarted after suspend completion
1204 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301205 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301206 }
1207
1208 if (!a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
1209 ALOGD("calling BT module stream start");
1210 /* This call indicates BT IPC lib to start playback */
1211 ret = a2dp.audio_start_stream();
1212 ALOGE("BT controller start return = %d",ret);
1213 if (ret != 0 ) {
1214 ALOGE("BT controller start failed");
1215 a2dp.a2dp_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301216 } else {
1217 if(configure_a2dp_encoder_format() == true) {
1218 a2dp.a2dp_started = true;
1219 ret = 0;
1220 ALOGD("Start playback successful to BT library");
1221 } else {
1222 ALOGD(" unable to configure DSP encoder");
1223 a2dp.a2dp_started = false;
1224 ret = -ETIMEDOUT;
1225 }
1226 }
1227 }
1228
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301229 if (a2dp.a2dp_started) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301230 a2dp.a2dp_total_active_session_request++;
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301231 a2dp_check_and_set_scrambler();
1232 a2dp_set_backend_cfg();
1233 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301234
1235 ALOGD("start A2DP playback total active sessions :%d",
1236 a2dp.a2dp_total_active_session_request);
1237 return ret;
1238}
1239
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05301240static void reset_a2dp_enc_config_params()
1241{
1242 int ret =0;
1243
1244 struct mixer_ctl *ctl_enc_config, *ctrl_bit_format;
1245 struct sbc_enc_cfg_t dummy_reset_config;
1246
1247 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
1248 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
1249 MIXER_ENC_CONFIG_BLOCK);
1250 if (!ctl_enc_config) {
1251 ALOGE(" ERROR a2dp encoder format mixer control not identifed");
1252 } else {
1253 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
1254 sizeof(struct sbc_enc_cfg_t));
1255 a2dp.bt_encoder_format = ENC_MEDIA_FMT_NONE;
1256 }
1257 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
1258 MIXER_ENC_BIT_FORMAT);
1259 if (!ctrl_bit_format) {
1260 ALOGE(" ERROR bit format CONFIG data mixer control not identifed");
1261 } else {
1262 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
1263 if (ret != 0) {
1264 ALOGE("%s: Failed to set bit format to encoder", __func__);
1265 }
1266 }
1267}
1268
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301269int audio_extn_a2dp_stop_playback()
1270{
1271 int ret =0;
1272
1273 ALOGV("audio_extn_a2dp_stop_playback start");
1274 if(!(a2dp.bt_lib_handle && a2dp.audio_stop_stream)) {
1275 ALOGE("a2dp handle is not identified, Ignoring start request");
1276 return -ENOSYS;
1277 }
1278
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301279 if (a2dp.a2dp_total_active_session_request > 0)
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301280 a2dp.a2dp_total_active_session_request--;
1281
1282 if ( a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301283 ALOGV("calling BT module stream stop");
1284 ret = a2dp.audio_stop_stream();
1285 if (ret < 0)
1286 ALOGE("stop stream to BT IPC lib failed");
1287 else
1288 ALOGV("stop steam to BT IPC lib successful");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05301289 reset_a2dp_enc_config_params();
Preetam Singh Ranawatc2bef792017-11-22 10:59:15 +05301290 a2dp_reset_backend_cfg();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301291 }
1292 if(!a2dp.a2dp_total_active_session_request)
1293 a2dp.a2dp_started = false;
1294 ALOGD("Stop A2DP playback total active sessions :%d",
1295 a2dp.a2dp_total_active_session_request);
1296 return 0;
1297}
1298
1299void audio_extn_a2dp_set_parameters(struct str_parms *parms)
1300{
1301 int ret, val;
1302 char value[32]={0};
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05301303 struct audio_usecase *uc_info;
1304 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301305
1306 if(a2dp.is_a2dp_offload_supported == false) {
1307 ALOGV("no supported encoders identified,ignoring a2dp setparam");
1308 return;
1309 }
1310
1311 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
1312 sizeof(value));
Zhou Song681350a2017-10-19 16:28:42 +08001313 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301314 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08001315 if (audio_is_a2dp_out_device(val)) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301316 ALOGV("Received device connect request for A2DP");
1317 open_a2dp_output();
1318 }
1319 goto param_handled;
1320 }
1321
1322 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
1323 sizeof(value));
1324
Zhou Song681350a2017-10-19 16:28:42 +08001325 if (ret >= 0) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301326 val = atoi(value);
Zhou Song681350a2017-10-19 16:28:42 +08001327 if (audio_is_a2dp_out_device(val)) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301328 ALOGV("Received device dis- connect request");
1329 close_a2dp_output();
kunleiz4a1fad62018-02-08 18:00:16 +08001330 reset_a2dp_enc_config_params();
1331 a2dp_reset_backend_cfg();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301332 }
1333 goto param_handled;
1334 }
1335
1336 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
1337 if (ret >= 0) {
1338 if (a2dp.bt_lib_handle && (a2dp.bt_state != A2DP_STATE_DISCONNECTED) ) {
1339 if ((!strncmp(value,"true",sizeof(value)))) {
1340 ALOGD("Setting a2dp to suspend state");
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301341 a2dp.a2dp_suspended = true;
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05301342 list_for_each(node, &a2dp.adev->usecase_list) {
1343 uc_info = node_to_item(node, struct audio_usecase, list);
Zhou Songc66eb7e2017-08-08 18:29:07 +08001344 if (uc_info->type == PCM_PLAYBACK &&
1345 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05301346 pthread_mutex_unlock(&a2dp.adev->lock);
1347 check_a2dp_restore(a2dp.adev, uc_info->stream.out, false);
1348 pthread_mutex_lock(&a2dp.adev->lock);
1349 }
1350 }
Naresh Tanniru03f9dd52016-10-19 18:46:22 +05301351 reset_a2dp_enc_config_params();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301352 if(a2dp.audio_suspend_stream)
1353 a2dp.audio_suspend_stream();
1354 } else if (a2dp.a2dp_suspended == true) {
1355 ALOGD("Resetting a2dp suspend state");
Zhou Song10617ed2017-05-26 13:28:48 +08001356 struct audio_usecase *uc_info;
1357 struct listnode *node;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301358 if(a2dp.clear_a2dpsuspend_flag)
1359 a2dp.clear_a2dpsuspend_flag();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301360 a2dp.a2dp_suspended = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +05301361 /*
1362 * It is possible that before suspend,a2dp sessions can be active
1363 * for example during music + voice activation concurrency
1364 * a2dp suspend will be called & BT will change to sco mode
1365 * though music is paused as a part of voice activation
1366 * compress session close happens only after pause timeout(10secs)
1367 * so if resume request comes before pause timeout as a2dp session
1368 * is already active IPC start will not be called from APM/audio_hw
1369 * Fix is to call a2dp start for IPC library post suspend
1370 * based on number of active session count
1371 */
1372 if (a2dp.a2dp_total_active_session_request > 0) {
1373 ALOGD(" Calling IPC lib start post suspend state");
1374 if(a2dp.audio_start_stream) {
1375 ret = a2dp.audio_start_stream();
1376 if (ret != 0) {
1377 ALOGE("BT controller start failed");
1378 a2dp.a2dp_started = false;
1379 }
1380 }
1381 }
Zhou Song10617ed2017-05-26 13:28:48 +08001382 list_for_each(node, &a2dp.adev->usecase_list) {
1383 uc_info = node_to_item(node, struct audio_usecase, list);
Zhou Songc66eb7e2017-08-08 18:29:07 +08001384 if (uc_info->type == PCM_PLAYBACK &&
1385 (uc_info->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP)) {
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05301386 pthread_mutex_unlock(&a2dp.adev->lock);
1387 check_a2dp_restore(a2dp.adev, uc_info->stream.out, true);
1388 pthread_mutex_lock(&a2dp.adev->lock);
1389 }
Zhou Song10617ed2017-05-26 13:28:48 +08001390 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301391 }
1392 }
1393 goto param_handled;
1394 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301395param_handled:
1396 ALOGV("end of a2dp setparam");
1397}
1398
Naresh Tannirucd2353e2016-08-19 00:37:25 +05301399void audio_extn_a2dp_set_handoff_mode(bool is_on)
1400{
1401 a2dp.is_handoff_in_progress = is_on;
1402}
1403
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301404bool audio_extn_a2dp_is_force_device_switch()
1405{
1406 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +05301407 // Or if a2dp device is selected but earlier start failed ( as a2dp
1408 // was suspended, force retry.
1409 return a2dp.is_handoff_in_progress || !a2dp.a2dp_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301410}
1411
Preetam Singh Ranawatb7475ba2017-02-24 18:17:06 +05301412void audio_extn_a2dp_get_sample_rate(int *sample_rate)
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301413{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301414 *sample_rate = a2dp.enc_sampling_rate;
1415}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301416
1417bool audio_extn_a2dp_is_ready()
1418{
1419 bool ret = false;
1420
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05301421 if (a2dp.a2dp_suspended)
1422 return ret;
1423
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -08001424 if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) &&
1425 (a2dp.is_a2dp_offload_supported) &&
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +05301426 (a2dp.audio_check_a2dp_ready))
1427 ret = a2dp.audio_check_a2dp_ready();
1428 return ret;
1429}
1430
Chaithanya Krishna Bacharaju69d2e4c2017-05-26 18:22:46 +05301431bool audio_extn_a2dp_is_suspended()
1432{
1433 return a2dp.a2dp_suspended;
1434}
1435
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301436void audio_extn_a2dp_init (void *adev)
1437{
1438 a2dp.adev = (struct audio_device*)adev;
1439 a2dp.bt_lib_handle = NULL;
1440 a2dp.a2dp_started = false;
1441 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
1442 a2dp.a2dp_total_active_session_request = 0;
1443 a2dp.a2dp_suspended = false;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301444 a2dp.bt_encoder_format = ENC_CODEC_TYPE_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +05301445 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301446 a2dp.is_a2dp_offload_supported = false;
1447 a2dp.is_handoff_in_progress = false;
Aniket Kumar Lata53e54b12017-08-24 17:45:38 -07001448 a2dp.is_aptx_dual_mono_supported = false;
kunleiz5a127262017-09-08 14:47:48 +08001449 reset_a2dp_enc_config_params();
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301450 update_offload_codec_capabilities();
1451}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08001452
1453uint32_t audio_extn_a2dp_get_encoder_latency()
1454{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08001455 uint32_t latency = 0;
1456 int avsync_runtime_prop = 0;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301457 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0,
1458 aac_offset = 0, celt_offset = 0, ldac_offset = 0;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08001459 char value[PROPERTY_VALUE_MAX];
1460
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08001461 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -07001462 avsync_runtime_prop = property_get("vendor.audio.a2dp.codec.latency", value, NULL);
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08001463 if (avsync_runtime_prop > 0) {
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301464 if (sscanf(value, "%d/%d/%d/%d/%d%d",
1465 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset, &celt_offset, &ldac_offset) != 6) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08001466 ALOGI("Failed to parse avsync offset params from '%s'.", value);
1467 avsync_runtime_prop = 0;
1468 }
1469 }
1470
yidongh0515e042017-07-06 15:00:34 +08001471 uint32_t slatency = 0;
1472 if (a2dp.audio_get_a2dp_sink_latency && a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
1473 slatency = a2dp.audio_get_a2dp_sink_latency();
1474 }
1475
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -07001476 switch(a2dp.bt_encoder_format) {
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301477 case ENC_CODEC_TYPE_SBC:
yidongh0515e042017-07-06 15:00:34 +08001478 latency = (avsync_runtime_prop > 0) ? sbc_offset : ENCODER_LATENCY_SBC;
1479 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_SBC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08001480 break;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301481 case ENC_CODEC_TYPE_APTX:
yidongh0515e042017-07-06 15:00:34 +08001482 latency = (avsync_runtime_prop > 0) ? aptx_offset : ENCODER_LATENCY_APTX;
1483 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08001484 break;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301485 case ENC_CODEC_TYPE_APTX_HD:
yidongh0515e042017-07-06 15:00:34 +08001486 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : ENCODER_LATENCY_APTX_HD;
1487 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_APTX_HD : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08001488 break;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301489 case ENC_CODEC_TYPE_AAC:
yidongh0515e042017-07-06 15:00:34 +08001490 latency = (avsync_runtime_prop > 0) ? aac_offset : ENCODER_LATENCY_AAC;
1491 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_AAC : slatency;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08001492 break;
Preetam Singh Ranawat0d645ea2017-08-07 18:12:07 +05301493 case ENC_CODEC_TYPE_CELT:
1494 latency = (avsync_runtime_prop > 0) ? celt_offset : ENCODER_LATENCY_CELT;
1495 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_CELT : slatency;
1496 break;
Preetam Singh Ranawatd058a3d2017-10-25 17:31:37 +05301497 case ENC_CODEC_TYPE_LDAC:
1498 latency = (avsync_runtime_prop > 0) ? ldac_offset : ENCODER_LATENCY_LDAC;
1499 latency += (slatency <= 0) ? DEFAULT_SINK_LATENCY_LDAC : slatency;
1500 break;
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -08001501 default:
1502 latency = 200;
1503 break;
1504 }
1505 return latency;
1506}
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301507#endif // SPLIT_A2DP_ENABLED