blob: b665adb6e93589575b8663f2e5369ca700b040ef [file] [log] [blame]
Naresh Tanniru9d027a62015-03-13 01:32:10 +05301/*
Naresh Tannirued694c82017-02-07 17:01:28 +05302* Copyright (c) 2015-2017, 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"
38#include <stdlib.h>
39#include <cutils/str_parms.h>
40#include <hardware/audio.h>
41#include <hardware/hardware.h>
42#include <cutils/properties.h>
43
44#ifdef SPLIT_A2DP_ENABLED
45#define AUDIO_PARAMETER_A2DP_STARTED "A2dpStarted"
46#define BT_IPC_LIB_NAME "libbthost_if.so"
47#define ENC_MEDIA_FMT_NONE 0
48#define ENC_MEDIA_FMT_AAC 0x00010DA6
49#define ENC_MEDIA_FMT_APTX 0x000131ff
50#define ENC_MEDIA_FMT_APTX_HD 0x00013200
51#define ENC_MEDIA_FMT_SBC 0x00010BF2
52#define MEDIA_FMT_AAC_AOT_LC 2
53#define MEDIA_FMT_AAC_AOT_SBR 5
54#define MEDIA_FMT_AAC_AOT_PS 29
Naresh Tanniru9d027a62015-03-13 01:32:10 +053055#define PCM_CHANNEL_L 1
56#define PCM_CHANNEL_R 2
57#define PCM_CHANNEL_C 3
58#define MEDIA_FMT_SBC_CHANNEL_MODE_MONO 1
59#define MEDIA_FMT_SBC_CHANNEL_MODE_STEREO 2
60#define MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO 8
61#define MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO 9
62#define MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS 0
63#define MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR 1
64#define MIXER_ENC_CONFIG_BLOCK "SLIM_7_RX Encoder Config"
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +053065#define MIXER_ENC_BIT_FORMAT "AFE Input Bit Format"
Naresh Tanniru9d027a62015-03-13 01:32:10 +053066#define MIXER_ENC_FMT_SBC "SBC"
67#define MIXER_ENC_FMT_AAC "AAC"
68#define MIXER_ENC_FMT_APTX "APTX"
69#define MIXER_ENC_FMT_APTXHD "APTXHD"
70#define MIXER_ENC_FMT_NONE "NONE"
71
72
73typedef int (*audio_stream_open_t)(void);
74typedef int (*audio_stream_close_t)(void);
75typedef int (*audio_start_stream_t)(void);
76typedef int (*audio_stop_stream_t)(void);
77typedef int (*audio_suspend_stream_t)(void);
78typedef void (*audio_handoff_triggered_t)(void);
79typedef void (*clear_a2dpsuspend_flag_t)(void);
80typedef void * (*audio_get_codec_config_t)(uint8_t *multicast_status,uint8_t *num_dev,
81 audio_format_t *codec_type);
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +053082typedef int (*audio_check_a2dp_ready_t)(void);
Naresh Tanniru9d027a62015-03-13 01:32:10 +053083
84enum A2DP_STATE {
85 A2DP_STATE_CONNECTED,
86 A2DP_STATE_STARTED,
87 A2DP_STATE_STOPPED,
88 A2DP_STATE_DISCONNECTED,
89};
90
91/* structure used to update a2dp state machine
92 * to communicate IPC library
93 * to store DSP encoder configuration information
94 */
95struct a2dp_data {
96 struct audio_device *adev;
97 void *bt_lib_handle;
98 audio_stream_open_t audio_stream_open;
99 audio_stream_close_t audio_stream_close;
100 audio_start_stream_t audio_start_stream;
101 audio_stop_stream_t audio_stop_stream;
102 audio_suspend_stream_t audio_suspend_stream;
103 audio_handoff_triggered_t audio_handoff_triggered;
104 clear_a2dpsuspend_flag_t clear_a2dpsuspend_flag;
105 audio_get_codec_config_t audio_get_codec_config;
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530106 audio_check_a2dp_ready_t audio_check_a2dp_ready;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530107 enum A2DP_STATE bt_state;
108 audio_format_t bt_encoder_format;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530109 uint32_t enc_sampling_rate;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530110 bool a2dp_started;
111 bool a2dp_suspended;
112 int a2dp_total_active_session_request;
113 bool is_a2dp_offload_supported;
114 bool is_handoff_in_progress;
115};
116
117struct a2dp_data a2dp;
118
119/* START of DSP configurable structures
120 * These values should match with DSP interface defintion
121 */
122
123/* AAC encoder configuration structure. */
124typedef struct aac_enc_cfg_t aac_enc_cfg_t;
125
126/* supported enc_mode are AAC_LC, AAC_SBR, AAC_PS
127 * supported aac_fmt_flag are ADTS/RAW
128 * supported channel_cfg are Native mode, Mono , Stereo
129 */
130struct aac_enc_cfg_t {
131 uint32_t enc_format;
132 uint32_t bit_rate;
133 uint32_t enc_mode;
134 uint16_t aac_fmt_flag;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530135 uint16_t channel_cfg;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530136 uint32_t sample_rate;
137} ;
138
139/* SBC encoder configuration structure. */
140typedef struct sbc_enc_cfg_t sbc_enc_cfg_t;
141
142/* supported num_subbands are 4/8
143 * supported blk_len are 4, 8, 12, 16
144 * supported channel_mode are MONO, STEREO, DUAL_MONO, JOINT_STEREO
145 * supported alloc_method are LOUNDNESS/SNR
146 * supported bit_rate for mono channel is max 320kbps
147 * supported bit rate for stereo channel is max 512 kbps
148 */
149struct sbc_enc_cfg_t{
150 uint32_t enc_format;
151 uint32_t num_subbands;
152 uint32_t blk_len;
153 uint32_t channel_mode;
154 uint32_t alloc_method;
155 uint32_t bit_rate;
156 uint32_t sample_rate;
157};
158
159
160/* supported num_channels are Mono/Stereo
161 * supported channel_mapping for mono is CHANNEL_C
162 * supported channel mapping for stereo is CHANNEL_L and CHANNEL_R
163 * custom size and reserved are not used(for future enhancement)
164 */
165struct custom_enc_cfg_aptx_t
166{
167 uint32_t enc_format;
168 uint32_t sample_rate;
169 uint16_t num_channels;
170 uint16_t reserved;
171 uint8_t channel_mapping[8];
172 uint32_t custom_size;
173};
174
Satya Krishna Pindiprolif7d65712017-04-26 14:24:53 +0530175/* TODO: Define the following structures only for O using PLATFORM_VERSION */
176/* Information about BT SBC encoder configuration
177 * This data is used between audio HAL module and
178 * BT IPC library to configure DSP encoder
179 */
180typedef struct {
181 uint32_t subband; /* 4, 8 */
182 uint32_t blk_len; /* 4, 8, 12, 16 */
183 uint16_t sampling_rate; /*44.1khz,48khz*/
184 uint8_t channels; /*0(Mono),1(Dual_mono),2(Stereo),3(JS)*/
185 uint8_t alloc; /*0(Loudness),1(SNR)*/
186 uint8_t min_bitpool; /* 2 */
187 uint8_t max_bitpool; /*53(44.1khz),51 (48khz) */
188 uint32_t bitrate; /* 320kbps to 512kbps */
189} audio_sbc_encoder_config;
190
191
192/* Information about BT APTX encoder configuration
193 * This data is used between audio HAL module and
194 * BT IPC library to configure DSP encoder
195 */
196typedef struct {
197 uint16_t sampling_rate;
198 uint8_t channels;
199 uint32_t bitrate;
200} audio_aptx_encoder_config;
201
202
203/* Information about BT AAC encoder configuration
204 * This data is used between audio HAL module and
205 * BT IPC library to configure DSP encoder
206 */
207typedef struct {
208 uint32_t enc_mode; /* LC, SBR, PS */
209 uint16_t format_flag; /* RAW, ADTS */
210 uint16_t channels; /* 1-Mono, 2-Stereo */
211 uint32_t sampling_rate;
212 uint32_t bitrate;
213} audio_aac_encoder_config;
214
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530215/*********** END of DSP configurable structures ********************/
216
217/* API to identify DSP encoder captabilities */
218static void a2dp_offload_codec_cap_parser(char *value)
219{
Naresh Tannirucd2353e2016-08-19 00:37:25 +0530220 char *tok = NULL,*saveptr;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530221
Naresh Tannirucd2353e2016-08-19 00:37:25 +0530222 tok = strtok_r(value, "-", &saveptr);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530223 while (tok != NULL) {
224 if (strcmp(tok, "sbc") == 0) {
225 ALOGD("%s: SBC offload supported\n",__func__);
226 a2dp.is_a2dp_offload_supported = true;
227 break;
228 } else if (strcmp(tok, "aptx") == 0) {
229 ALOGD("%s: aptx offload supported\n",__func__);
230 a2dp.is_a2dp_offload_supported = true;
231 break;
Naresh Tannirued694c82017-02-07 17:01:28 +0530232 } else if (strcmp(tok, "aptxhd") == 0) {
233 ALOGD("%s: aptx HD offload supported\n",__func__);
234 a2dp.is_a2dp_offload_supported = true;
235 break;
236 } else if (strcmp(tok, "aac") == 0) {
237 ALOGD("%s: aac offload supported\n",__func__);
238 a2dp.is_a2dp_offload_supported = true;
239 break;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530240 }
Naresh Tannirucd2353e2016-08-19 00:37:25 +0530241 tok = strtok_r(NULL, "-", &saveptr);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530242 };
243}
244
245static void update_offload_codec_capabilities()
246{
247 char value[PROPERTY_VALUE_MAX] = {'\0'};
248
249 property_get("persist.bt.a2dp_offload_cap", value, "false");
250 ALOGD("get_offload_codec_capabilities = %s",value);
251 a2dp.is_a2dp_offload_supported =
252 property_get_bool("persist.bt.a2dp_offload_cap", false);
253 if (strcmp(value, "false") != 0)
254 a2dp_offload_codec_cap_parser(value);
255 ALOGD("%s: codec cap = %s",__func__,value);
256}
257
258/* API to open BT IPC library to start IPC communication */
259static void open_a2dp_output()
260{
261 int ret = 0;
262
263 ALOGD(" Open A2DP output start ");
264 if (a2dp.bt_lib_handle == NULL){
265 ALOGD(" Requesting for BT lib handle");
266 a2dp.bt_lib_handle = dlopen(BT_IPC_LIB_NAME, RTLD_NOW);
267
268 if (a2dp.bt_lib_handle == NULL) {
269 ALOGE("%s: DLOPEN failed for %s", __func__, BT_IPC_LIB_NAME);
270 ret = -ENOSYS;
271 goto init_fail;
272 } else {
273 a2dp.audio_stream_open = (audio_stream_open_t)
274 dlsym(a2dp.bt_lib_handle, "audio_stream_open");
275 a2dp.audio_start_stream = (audio_start_stream_t)
276 dlsym(a2dp.bt_lib_handle, "audio_start_stream");
277 a2dp.audio_get_codec_config = (audio_get_codec_config_t)
278 dlsym(a2dp.bt_lib_handle, "audio_get_codec_config");
279 a2dp.audio_suspend_stream = (audio_suspend_stream_t)
280 dlsym(a2dp.bt_lib_handle, "audio_suspend_stream");
281 a2dp.audio_handoff_triggered = (audio_handoff_triggered_t)
282 dlsym(a2dp.bt_lib_handle, "audio_handoff_triggered");
283 a2dp.clear_a2dpsuspend_flag = (clear_a2dpsuspend_flag_t)
284 dlsym(a2dp.bt_lib_handle, "clear_a2dpsuspend_flag");
285 a2dp.audio_stop_stream = (audio_stop_stream_t)
286 dlsym(a2dp.bt_lib_handle, "audio_stop_stream");
287 a2dp.audio_stream_close = (audio_stream_close_t)
288 dlsym(a2dp.bt_lib_handle, "audio_stream_close");
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530289 a2dp.audio_check_a2dp_ready = (audio_check_a2dp_ready_t)
290 dlsym(a2dp.bt_lib_handle,"audio_check_a2dp_ready");
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530291 }
292 }
293
294 if (a2dp.bt_lib_handle && a2dp.audio_stream_open) {
295 if (a2dp.bt_state == A2DP_STATE_DISCONNECTED) {
296 ALOGD("calling BT stream open");
297 ret = a2dp.audio_stream_open();
298 if(ret != 0) {
299 ALOGE("Failed to open output stream for a2dp: status %d", ret);
300 goto init_fail;
301 }
302 a2dp.bt_state = A2DP_STATE_CONNECTED;
303 } else {
304 ALOGD("Called a2dp open with improper state, Ignoring request state %d", a2dp.bt_state);
305 }
306 } else {
307 ALOGE("a2dp handle is not identified, Ignoring open request");
308 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
309 goto init_fail;
310 }
311
312init_fail:
313 if(ret != 0 && (a2dp.bt_lib_handle != NULL)) {
314 dlclose(a2dp.bt_lib_handle);
315 a2dp.bt_lib_handle = NULL;
316 }
317}
318
319static int close_a2dp_output()
320{
321 ALOGV("%s\n",__func__);
322 if (!(a2dp.bt_lib_handle && a2dp.audio_stream_close)) {
323 ALOGE("a2dp handle is not identified, Ignoring close request");
324 return -ENOSYS;
325 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530326 if (a2dp.bt_state != A2DP_STATE_DISCONNECTED) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530327 ALOGD("calling BT stream close");
328 if(a2dp.audio_stream_close() == false)
329 ALOGE("failed close a2dp control path from BT library");
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530330 }
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530331 a2dp.a2dp_started = false;
332 a2dp.a2dp_total_active_session_request = 0;
333 a2dp.a2dp_suspended = false;
334 a2dp.bt_encoder_format = AUDIO_FORMAT_INVALID;
335 a2dp.enc_sampling_rate = 48000;
336 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530337
338 return 0;
339}
340
341/* API to configure SBC DSP encoder */
342bool configure_sbc_enc_format(audio_sbc_encoder_config *sbc_bt_cfg)
343{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530344 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530345 struct sbc_enc_cfg_t sbc_dsp_cfg;
346 bool is_configured = false;
347 int ret = 0;
348
349 if(sbc_bt_cfg == NULL)
350 return false;
351
352 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
353 if (!ctl_enc_data) {
354 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
355 is_configured = false;
356 goto fail;
357 }
358 a2dp.bt_encoder_format = AUDIO_FORMAT_SBC;
359 memset(&sbc_dsp_cfg, 0x0, sizeof(struct sbc_enc_cfg_t));
360 sbc_dsp_cfg.enc_format = ENC_MEDIA_FMT_SBC;
361 sbc_dsp_cfg.num_subbands = sbc_bt_cfg->subband;
362 sbc_dsp_cfg.blk_len = sbc_bt_cfg->blk_len;
363 switch(sbc_bt_cfg->channels) {
364 case 0:
365 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_MONO;
366 break;
367 case 1:
368 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_DUAL_MONO;
369 break;
370 case 3:
371 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_JOINT_STEREO;
372 break;
373 case 2:
374 default:
375 sbc_dsp_cfg.channel_mode = MEDIA_FMT_SBC_CHANNEL_MODE_STEREO;
376 break;
377 }
378 if (sbc_bt_cfg->alloc)
379 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_LOUDNESS;
380 else
381 sbc_dsp_cfg.alloc_method = MEDIA_FMT_SBC_ALLOCATION_METHOD_SNR;
382 sbc_dsp_cfg.bit_rate = sbc_bt_cfg->bitrate;
383 sbc_dsp_cfg.sample_rate = sbc_bt_cfg->sampling_rate;
384 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&sbc_dsp_cfg,
385 sizeof(struct sbc_enc_cfg_t));
386 if (ret != 0) {
387 ALOGE("%s: failed to set SBC encoder config", __func__);
388 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530389 goto fail;
390 }
391 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
392 MIXER_ENC_BIT_FORMAT);
393 if (!ctrl_bit_format) {
394 ALOGE(" ERROR bit format CONFIG data mixer control not identifed");
395 is_configured = false;
396 goto fail;
397 }
398 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
399 if (ret != 0) {
400 ALOGE("%s: Failed to set bit format to encoder", __func__);
401 is_configured = false;
402 goto fail;
403 }
404 is_configured = true;
405 a2dp.enc_sampling_rate = sbc_bt_cfg->sampling_rate;
406 ALOGV("Successfully updated SBC enc format with samplingrate: %d channelmode:%d",
407 sbc_dsp_cfg.sample_rate, sbc_dsp_cfg.channel_mode);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530408fail:
409 return is_configured;
410}
411
412/* API to configure APTX DSP encoder */
413bool configure_aptx_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
414{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530415 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530416 struct custom_enc_cfg_aptx_t aptx_dsp_cfg;
417 bool is_configured = false;
418 int ret = 0;
419
420 if(aptx_bt_cfg == NULL)
421 return false;
422
423 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
424 if (!ctl_enc_data) {
425 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
426 is_configured = false;
427 goto fail;
428 }
429 a2dp.bt_encoder_format = AUDIO_FORMAT_APTX;
430 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_aptx_t));
431 aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX;
432 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
433 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
434 switch(aptx_dsp_cfg.num_channels) {
435 case 1:
436 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
437 break;
438 case 2:
439 default:
440 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
441 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
442 break;
443 }
444 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
445 sizeof(struct custom_enc_cfg_aptx_t));
446 if (ret != 0) {
447 ALOGE("%s: Failed to set APTX encoder config", __func__);
448 is_configured = false;
449 goto fail;
450 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530451 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
452 MIXER_ENC_BIT_FORMAT);
453 if (!ctrl_bit_format) {
454 ALOGE("ERROR bit format CONFIG data mixer control not identifed");
455 is_configured = false;
456 goto fail;
457 } else {
458 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
459 if (ret != 0) {
460 ALOGE("%s: Failed to set bit format to encoder", __func__);
461 is_configured = false;
462 goto fail;
463 }
464 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530465 is_configured = true;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530466 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
467 ALOGV("Successfully updated APTX enc format with samplingrate: %d channels:%d",
468 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530469fail:
470 return is_configured;
471}
472
473/* API to configure APTX HD DSP encoder
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530474 */
475bool configure_aptx_hd_enc_format(audio_aptx_encoder_config *aptx_bt_cfg)
476{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530477 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530478 struct custom_enc_cfg_aptx_t aptx_dsp_cfg;
479 bool is_configured = false;
480 int ret = 0;
481
482 if(aptx_bt_cfg == NULL)
483 return false;
484
485 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
486 if (!ctl_enc_data) {
487 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
488 is_configured = false;
489 goto fail;
490 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530491
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530492 a2dp.bt_encoder_format = AUDIO_FORMAT_APTX_HD;
493 memset(&aptx_dsp_cfg, 0x0, sizeof(struct custom_enc_cfg_aptx_t));
494 aptx_dsp_cfg.enc_format = ENC_MEDIA_FMT_APTX_HD;
495 aptx_dsp_cfg.sample_rate = aptx_bt_cfg->sampling_rate;
496 aptx_dsp_cfg.num_channels = aptx_bt_cfg->channels;
497 switch(aptx_dsp_cfg.num_channels) {
498 case 1:
499 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_C;
500 break;
501 case 2:
502 default:
503 aptx_dsp_cfg.channel_mapping[0] = PCM_CHANNEL_L;
504 aptx_dsp_cfg.channel_mapping[1] = PCM_CHANNEL_R;
505 break;
506 }
507 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aptx_dsp_cfg,
508 sizeof(struct custom_enc_cfg_aptx_t));
509 if (ret != 0) {
510 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
511 is_configured = false;
512 goto fail;
513 }
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530514 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_BIT_FORMAT);
515 if (!ctrl_bit_format) {
516 ALOGE(" ERROR bit format CONFIG data mixer control not identifed");
517 is_configured = false;
518 goto fail;
519 }
520 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S24_LE");
521 if (ret != 0) {
522 ALOGE("%s: Failed to set APTX HD encoder config", __func__);
523 is_configured = false;
524 goto fail;
525 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530526 is_configured = true;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530527 a2dp.enc_sampling_rate = aptx_bt_cfg->sampling_rate;
528 ALOGV("Successfully updated APTX HD encformat with samplingrate: %d channels:%d",
529 aptx_dsp_cfg.sample_rate, aptx_dsp_cfg.num_channels);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530530fail:
531 return is_configured;
532}
533
534/* API to configure AAC DSP encoder */
535bool configure_aac_enc_format(audio_aac_encoder_config *aac_bt_cfg)
536{
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530537 struct mixer_ctl *ctl_enc_data = NULL, *ctrl_bit_format = NULL;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530538 struct aac_enc_cfg_t aac_dsp_cfg;
539 bool is_configured = false;
540 int ret = 0;
541
542 if(aac_bt_cfg == NULL)
543 return false;
544
545 ctl_enc_data = mixer_get_ctl_by_name(a2dp.adev->mixer, MIXER_ENC_CONFIG_BLOCK);
546 if (!ctl_enc_data) {
547 ALOGE(" ERROR a2dp encoder CONFIG data mixer control not identifed");
548 is_configured = false;
549 goto fail;
550 }
551 a2dp.bt_encoder_format = AUDIO_FORMAT_AAC;
552 memset(&aac_dsp_cfg, 0x0, sizeof(struct aac_enc_cfg_t));
553 aac_dsp_cfg.enc_format = ENC_MEDIA_FMT_AAC;
554 aac_dsp_cfg.bit_rate = aac_bt_cfg->bitrate;
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530555 aac_dsp_cfg.sample_rate = aac_bt_cfg->sampling_rate;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530556 switch(aac_bt_cfg->enc_mode) {
557 case 0:
558 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_LC;
559 break;
560 case 2:
561 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_PS;
562 break;
563 case 1:
564 default:
565 aac_dsp_cfg.enc_mode = MEDIA_FMT_AAC_AOT_SBR;
566 break;
567 }
Naresh Tannirua42d0bd2016-09-21 15:30:46 +0530568 aac_dsp_cfg.aac_fmt_flag = aac_bt_cfg->format_flag;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530569 aac_dsp_cfg.channel_cfg = aac_bt_cfg->channels;
570 ret = mixer_ctl_set_array(ctl_enc_data, (void *)&aac_dsp_cfg,
571 sizeof(struct aac_enc_cfg_t));
572 if (ret != 0) {
573 ALOGE("%s: failed to set SBC encoder config", __func__);
574 is_configured = false;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530575 goto fail;
576 }
577 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
578 MIXER_ENC_BIT_FORMAT);
579 if (!ctrl_bit_format) {
580 is_configured = false;
581 ALOGE(" ERROR bit format CONFIG data mixer control not identifed");
582 goto fail;
583 }
584 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
585 if (ret != 0) {
586 ALOGE("%s: Failed to set bit format to encoder", __func__);
587 is_configured = false;
588 goto fail;
589 }
590 is_configured = true;
591 a2dp.enc_sampling_rate = aac_bt_cfg->sampling_rate;
592 ALOGV("Successfully updated AAC enc format with samplingrate: %d channels:%d",
593 aac_dsp_cfg.sample_rate, aac_dsp_cfg.channel_cfg);
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530594fail:
595 return is_configured;
596}
597
598bool configure_a2dp_encoder_format()
599{
600 void *codec_info = NULL;
601 uint8_t multi_cast = 0, num_dev = 1;
602 audio_format_t codec_type = AUDIO_FORMAT_INVALID;
603 bool is_configured = false;
604
605 if (!a2dp.audio_get_codec_config) {
606 ALOGE(" a2dp handle is not identified, ignoring a2dp encoder config");
607 return false;
608 }
609 ALOGD("configure_a2dp_encoder_format start");
610 codec_info = a2dp.audio_get_codec_config(&multi_cast, &num_dev,
611 &codec_type);
612
613 switch(codec_type) {
614 case AUDIO_FORMAT_SBC:
615 ALOGD(" Received SBC encoder supported BT device");
616 is_configured =
617 configure_sbc_enc_format((audio_sbc_encoder_config *)codec_info);
618 break;
619 case AUDIO_FORMAT_APTX:
620 ALOGD(" Received APTX encoder supported BT device");
621 is_configured =
622 configure_aptx_enc_format((audio_aptx_encoder_config *)codec_info);
623 break;
624 case AUDIO_FORMAT_APTX_HD:
625 ALOGD(" Received APTX HD encoder supported BT device");
626 is_configured =
627 configure_aptx_hd_enc_format((audio_aptx_encoder_config *)codec_info);
628 break;
629 case AUDIO_FORMAT_AAC:
630 ALOGD(" Received AAC encoder supported BT device");
631 is_configured =
632 configure_aac_enc_format((audio_aac_encoder_config *)codec_info);
633 break;
634 default:
635 ALOGD(" Received Unsupported encoder formar");
636 is_configured = false;
637 break;
638 }
639 return is_configured;
640}
641
642int audio_extn_a2dp_start_playback()
643{
644 int ret = 0;
645
646 ALOGD("audio_extn_a2dp_start_playback start");
647
648 if(!(a2dp.bt_lib_handle && a2dp.audio_start_stream
649 && a2dp.audio_get_codec_config)) {
650 ALOGE("a2dp handle is not identified, Ignoring start request");
651 return -ENOSYS;
652 }
653
654 if(a2dp.a2dp_suspended == true) {
655 //session will be restarted after suspend completion
656 ALOGD("a2dp start requested during suspend state");
Naresh Tannirucd2353e2016-08-19 00:37:25 +0530657 return -ENOSYS;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530658 }
659
660 if (!a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
661 ALOGD("calling BT module stream start");
662 /* This call indicates BT IPC lib to start playback */
663 ret = a2dp.audio_start_stream();
664 ALOGE("BT controller start return = %d",ret);
665 if (ret != 0 ) {
666 ALOGE("BT controller start failed");
667 a2dp.a2dp_started = false;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530668 } else {
669 if(configure_a2dp_encoder_format() == true) {
670 a2dp.a2dp_started = true;
671 ret = 0;
672 ALOGD("Start playback successful to BT library");
673 } else {
674 ALOGD(" unable to configure DSP encoder");
675 a2dp.a2dp_started = false;
676 ret = -ETIMEDOUT;
677 }
678 }
679 }
680
681 if (a2dp.a2dp_started)
682 a2dp.a2dp_total_active_session_request++;
683
684 ALOGD("start A2DP playback total active sessions :%d",
685 a2dp.a2dp_total_active_session_request);
686 return ret;
687}
688
Naresh Tanniru03f9dd52016-10-19 18:46:22 +0530689static void reset_a2dp_enc_config_params()
690{
691 int ret =0;
692
693 struct mixer_ctl *ctl_enc_config, *ctrl_bit_format;
694 struct sbc_enc_cfg_t dummy_reset_config;
695
696 memset(&dummy_reset_config, 0x0, sizeof(struct sbc_enc_cfg_t));
697 ctl_enc_config = mixer_get_ctl_by_name(a2dp.adev->mixer,
698 MIXER_ENC_CONFIG_BLOCK);
699 if (!ctl_enc_config) {
700 ALOGE(" ERROR a2dp encoder format mixer control not identifed");
701 } else {
702 ret = mixer_ctl_set_array(ctl_enc_config, (void *)&dummy_reset_config,
703 sizeof(struct sbc_enc_cfg_t));
704 a2dp.bt_encoder_format = ENC_MEDIA_FMT_NONE;
705 }
706 ctrl_bit_format = mixer_get_ctl_by_name(a2dp.adev->mixer,
707 MIXER_ENC_BIT_FORMAT);
708 if (!ctrl_bit_format) {
709 ALOGE(" ERROR bit format CONFIG data mixer control not identifed");
710 } else {
711 ret = mixer_ctl_set_enum_by_string(ctrl_bit_format, "S16_LE");
712 if (ret != 0) {
713 ALOGE("%s: Failed to set bit format to encoder", __func__);
714 }
715 }
716}
717
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530718int audio_extn_a2dp_stop_playback()
719{
720 int ret =0;
721
722 ALOGV("audio_extn_a2dp_stop_playback start");
723 if(!(a2dp.bt_lib_handle && a2dp.audio_stop_stream)) {
724 ALOGE("a2dp handle is not identified, Ignoring start request");
725 return -ENOSYS;
726 }
727
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530728 if (a2dp.a2dp_total_active_session_request > 0)
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530729 a2dp.a2dp_total_active_session_request--;
730
731 if ( a2dp.a2dp_started && !a2dp.a2dp_total_active_session_request) {
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530732 ALOGV("calling BT module stream stop");
733 ret = a2dp.audio_stop_stream();
734 if (ret < 0)
735 ALOGE("stop stream to BT IPC lib failed");
736 else
737 ALOGV("stop steam to BT IPC lib successful");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +0530738 reset_a2dp_enc_config_params();
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530739 }
740 if(!a2dp.a2dp_total_active_session_request)
741 a2dp.a2dp_started = false;
742 ALOGD("Stop A2DP playback total active sessions :%d",
743 a2dp.a2dp_total_active_session_request);
744 return 0;
745}
746
747void audio_extn_a2dp_set_parameters(struct str_parms *parms)
748{
749 int ret, val;
750 char value[32]={0};
751
752 if(a2dp.is_a2dp_offload_supported == false) {
753 ALOGV("no supported encoders identified,ignoring a2dp setparam");
754 return;
755 }
756
757 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_CONNECT, value,
758 sizeof(value));
759 if( ret >= 0) {
760 val = atoi(value);
761 if (val & AUDIO_DEVICE_OUT_ALL_A2DP) {
762 ALOGV("Received device connect request for A2DP");
763 open_a2dp_output();
764 }
765 goto param_handled;
766 }
767
768 ret = str_parms_get_str(parms, AUDIO_PARAMETER_DEVICE_DISCONNECT, value,
769 sizeof(value));
770
771 if( ret >= 0) {
772 val = atoi(value);
773 if (val & AUDIO_DEVICE_OUT_ALL_A2DP) {
774 ALOGV("Received device dis- connect request");
Naresh Tanniru03f9dd52016-10-19 18:46:22 +0530775 reset_a2dp_enc_config_params();
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530776 close_a2dp_output();
777 }
778 goto param_handled;
779 }
780
781 ret = str_parms_get_str(parms, "A2dpSuspended", value, sizeof(value));
782 if (ret >= 0) {
783 if (a2dp.bt_lib_handle && (a2dp.bt_state != A2DP_STATE_DISCONNECTED) ) {
784 if ((!strncmp(value,"true",sizeof(value)))) {
785 ALOGD("Setting a2dp to suspend state");
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530786 a2dp.a2dp_suspended = true;
Naresh Tanniru03f9dd52016-10-19 18:46:22 +0530787 reset_a2dp_enc_config_params();
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530788 if(a2dp.audio_suspend_stream)
789 a2dp.audio_suspend_stream();
790 } else if (a2dp.a2dp_suspended == true) {
791 ALOGD("Resetting a2dp suspend state");
792 if(a2dp.clear_a2dpsuspend_flag)
793 a2dp.clear_a2dpsuspend_flag();
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530794 a2dp.a2dp_suspended = false;
Naresh Tanniru649871a2016-11-04 18:08:32 +0530795 /*
796 * It is possible that before suspend,a2dp sessions can be active
797 * for example during music + voice activation concurrency
798 * a2dp suspend will be called & BT will change to sco mode
799 * though music is paused as a part of voice activation
800 * compress session close happens only after pause timeout(10secs)
801 * so if resume request comes before pause timeout as a2dp session
802 * is already active IPC start will not be called from APM/audio_hw
803 * Fix is to call a2dp start for IPC library post suspend
804 * based on number of active session count
805 */
806 if (a2dp.a2dp_total_active_session_request > 0) {
807 ALOGD(" Calling IPC lib start post suspend state");
808 if(a2dp.audio_start_stream) {
809 ret = a2dp.audio_start_stream();
810 if (ret != 0) {
811 ALOGE("BT controller start failed");
812 a2dp.a2dp_started = false;
813 }
814 }
815 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530816 }
817 }
818 goto param_handled;
819 }
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530820param_handled:
821 ALOGV("end of a2dp setparam");
822}
823
Naresh Tannirucd2353e2016-08-19 00:37:25 +0530824void audio_extn_a2dp_set_handoff_mode(bool is_on)
825{
826 a2dp.is_handoff_in_progress = is_on;
827}
828
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530829bool audio_extn_a2dp_is_force_device_switch()
830{
831 //During encoder reconfiguration mode, force a2dp device switch
Ashish Jainc597d102016-12-12 10:31:34 +0530832 // Or if a2dp device is selected but earlier start failed ( as a2dp
833 // was suspended, force retry.
834 return a2dp.is_handoff_in_progress || !a2dp.a2dp_started;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530835}
836
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530837void audio_extn_a2dp_get_apptype_params(uint32_t *sample_rate,
838 uint32_t *bit_width)
839{
840 if(a2dp.bt_encoder_format == AUDIO_FORMAT_APTX_HD)
841 *bit_width = 24;
842 else
843 *bit_width = 16;
844 *sample_rate = a2dp.enc_sampling_rate;
845}
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530846
847bool audio_extn_a2dp_is_ready()
848{
849 bool ret = false;
850
Aniket Kumar Lata901bcb82017-03-10 15:42:46 -0800851 if ((a2dp.bt_state != A2DP_STATE_DISCONNECTED) &&
852 (a2dp.is_a2dp_offload_supported) &&
Preetam Singh Ranawata1849ba2017-02-06 14:10:11 +0530853 (a2dp.audio_check_a2dp_ready))
854 ret = a2dp.audio_check_a2dp_ready();
855 return ret;
856}
857
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530858void audio_extn_a2dp_init (void *adev)
859{
860 a2dp.adev = (struct audio_device*)adev;
861 a2dp.bt_lib_handle = NULL;
862 a2dp.a2dp_started = false;
863 a2dp.bt_state = A2DP_STATE_DISCONNECTED;
864 a2dp.a2dp_total_active_session_request = 0;
865 a2dp.a2dp_suspended = false;
866 a2dp.bt_encoder_format = AUDIO_FORMAT_INVALID;
Naresh Tanniruf5ba8d02016-09-29 18:06:37 +0530867 a2dp.enc_sampling_rate = 48000;
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530868 a2dp.is_a2dp_offload_supported = false;
869 a2dp.is_handoff_in_progress = false;
870 update_offload_codec_capabilities();
871}
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -0800872
873uint32_t audio_extn_a2dp_get_encoder_latency()
874{
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -0800875 uint32_t latency = 0;
876 int avsync_runtime_prop = 0;
877 int sbc_offset = 0, aptx_offset = 0, aptxhd_offset = 0, aac_offset = 0;
878 char value[PROPERTY_VALUE_MAX];
879
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -0800880 memset(value, '\0', sizeof(char)*PROPERTY_VALUE_MAX);
881 avsync_runtime_prop = property_get("audio.a2dp.codec.latency", value, NULL);
882 if (avsync_runtime_prop > 0) {
883 if (sscanf(value, "%d/%d/%d/%d",
884 &sbc_offset, &aptx_offset, &aptxhd_offset, &aac_offset) != 4) {
885 ALOGI("Failed to parse avsync offset params from '%s'.", value);
886 avsync_runtime_prop = 0;
887 }
888 }
889
Aniket Kumar Latafaaffde2017-03-22 19:18:15 -0700890 switch(a2dp.bt_encoder_format) {
Aniket Kumar Latad5972fa2017-02-08 13:53:48 -0800891 case AUDIO_FORMAT_SBC:
892 latency = (avsync_runtime_prop > 0) ? sbc_offset : 150;
893 break;
894 case AUDIO_FORMAT_APTX:
895 latency = (avsync_runtime_prop > 0) ? aptx_offset : 200;
896 break;
897 case AUDIO_FORMAT_APTX_HD:
898 latency = (avsync_runtime_prop > 0) ? aptxhd_offset : 200;
899 break;
900 case AUDIO_FORMAT_AAC:
901 latency = (avsync_runtime_prop > 0) ? aac_offset : 250;
902 break;
903 default:
904 latency = 200;
905 break;
906 }
907 return latency;
908}
Naresh Tanniru9d027a62015-03-13 01:32:10 +0530909#endif // SPLIT_A2DP_ENABLED