blob: 8e04bf8c83770f6e7997a3bffc27eb700dc76d58 [file] [log] [blame]
/*
* Copyright (C) 2013-2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "audio_hw_primary"
#define ATRACE_TAG ATRACE_TAG_AUDIO
/*#define LOG_NDEBUG 0*/
/*#define VERY_VERY_VERBOSE_LOGGING*/
#ifdef VERY_VERY_VERBOSE_LOGGING
#define ALOGVV ALOGV
#else
#define ALOGVV(a...) do { } while(0)
#endif
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <sys/time.h>
#include <stdlib.h>
#include <math.h>
#include <dlfcn.h>
#include <sys/resource.h>
#include <sys/prctl.h>
#include <limits.h>
#include <log/log.h>
#include <cutils/trace.h>
#include <cutils/str_parms.h>
#include <cutils/properties.h>
#include <cutils/atomic.h>
#include <cutils/sched_policy.h>
#include <hardware/audio_effect.h>
#include <hardware/audio_alsaops.h>
#include <system/thread_defs.h>
#include <tinyalsa/asoundlib.h>
#include <audio_effects/effect_aec.h>
#include <audio_effects/effect_ns.h>
#include <audio_utils/clock.h>
#include "audio_hw.h"
#include "audio_extn.h"
#include "audio_perf.h"
#include "platform_api.h"
#include <platform.h>
#include "voice_extn.h"
#include "sound/compress_params.h"
#include "audio_extn/tfa_98xx.h"
#include "audio_extn/maxxaudio.h"
/* COMPRESS_OFFLOAD_FRAGMENT_SIZE must be more than 8KB and a multiple of 32KB if more than 32KB.
* COMPRESS_OFFLOAD_FRAGMENT_SIZE * COMPRESS_OFFLOAD_NUM_FRAGMENTS must be less than 8MB. */
#define COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024)
// 2 buffers causes problems with high bitrate files
#define COMPRESS_OFFLOAD_NUM_FRAGMENTS 3
/* ToDo: Check and update a proper value in msec */
#define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96
/* treat as unsigned Q1.13 */
#define APP_TYPE_GAIN_DEFAULT 0x2000
#define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000
/* treat as unsigned Q1.13 */
#define VOIP_PLAYBACK_VOLUME_MAX 0x2000
#define RECORD_GAIN_MIN 0.0f
#define RECORD_GAIN_MAX 1.0f
#define RECORD_VOLUME_CTL_MAX 0x2000
#define PROXY_OPEN_RETRY_COUNT 100
#define PROXY_OPEN_WAIT_TIME 20
#define MIN_CHANNEL_COUNT 1
#define DEFAULT_CHANNEL_COUNT 2
#ifndef MAX_TARGET_SPECIFIC_CHANNEL_CNT
#define MAX_CHANNEL_COUNT 1
#else
#define MAX_CHANNEL_COUNT atoi(XSTR(MAX_TARGET_SPECIFIC_CHANNEL_CNT))
#define XSTR(x) STR(x)
#define STR(x) #x
#endif
#define MAX_HIFI_CHANNEL_COUNT 8
#define ULL_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
static unsigned int configured_low_latency_capture_period_size =
LOW_LATENCY_CAPTURE_PERIOD_SIZE;
#define MMAP_PERIOD_SIZE (DEFAULT_OUTPUT_SAMPLING_RATE/1000)
#define MMAP_PERIOD_COUNT_MIN 32
#define MMAP_PERIOD_COUNT_MAX 512
#define MMAP_PERIOD_COUNT_DEFAULT (MMAP_PERIOD_COUNT_MAX)
/* This constant enables extended precision handling.
* TODO The flag is off until more testing is done.
*/
static const bool k_enable_extended_precision = false;
struct pcm_config pcm_config_deep_buffer = {
.channels = DEFAULT_CHANNEL_COUNT,
.rate = DEFAULT_OUTPUT_SAMPLING_RATE,
.period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE,
.period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
.format = PCM_FORMAT_S16_LE,
.start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
.stop_threshold = INT_MAX,
.avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4,
};
struct pcm_config pcm_config_low_latency = {
.channels = DEFAULT_CHANNEL_COUNT,
.rate = DEFAULT_OUTPUT_SAMPLING_RATE,
.period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE,
.period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT,
.format = PCM_FORMAT_S16_LE,
.start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
.stop_threshold = INT_MAX,
.avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4,
};
static int af_period_multiplier = 4;
struct pcm_config pcm_config_rt = {
.channels = DEFAULT_CHANNEL_COUNT,
.rate = DEFAULT_OUTPUT_SAMPLING_RATE,
.period_size = ULL_PERIOD_SIZE, //1 ms
.period_count = 512, //=> buffer size is 512ms
.format = PCM_FORMAT_S16_LE,
.start_threshold = ULL_PERIOD_SIZE*8, //8ms
.stop_threshold = INT_MAX,
.silence_threshold = 0,
.silence_size = 0,
.avail_min = ULL_PERIOD_SIZE, //1 ms
};
struct pcm_config pcm_config_hdmi_multi = {
.channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
.rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
.period_size = HDMI_MULTI_PERIOD_SIZE,
.period_count = HDMI_MULTI_PERIOD_COUNT,
.format = PCM_FORMAT_S16_LE,
.start_threshold = 0,
.stop_threshold = INT_MAX,
.avail_min = 0,
};
struct pcm_config pcm_config_mmap_playback = {
.channels = DEFAULT_CHANNEL_COUNT,
.rate = DEFAULT_OUTPUT_SAMPLING_RATE,
.period_size = MMAP_PERIOD_SIZE,
.period_count = MMAP_PERIOD_COUNT_DEFAULT,
.format = PCM_FORMAT_S16_LE,
.start_threshold = MMAP_PERIOD_SIZE*8,
.stop_threshold = INT32_MAX,
.silence_threshold = 0,
.silence_size = 0,
.avail_min = MMAP_PERIOD_SIZE, //1 ms
};
struct pcm_config pcm_config_hifi = {
.channels = DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */
.rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */
.period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, /* change #define */
.period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT,
.format = PCM_FORMAT_S24_3LE,
.start_threshold = 0,
.stop_threshold = INT_MAX,
.avail_min = 0,
};
struct pcm_config pcm_config_audio_capture = {
.channels = DEFAULT_CHANNEL_COUNT,
.period_count = AUDIO_CAPTURE_PERIOD_COUNT,
.format = PCM_FORMAT_S16_LE,
.stop_threshold = INT_MAX,
.avail_min = 0,
};
struct pcm_config pcm_config_audio_capture_rt = {
.channels = DEFAULT_CHANNEL_COUNT,
.rate = DEFAULT_OUTPUT_SAMPLING_RATE,
.period_size = ULL_PERIOD_SIZE,
.period_count = 512,
.format = PCM_FORMAT_S16_LE,
.start_threshold = 0,
.stop_threshold = INT_MAX,
.silence_threshold = 0,
.silence_size = 0,
.avail_min = ULL_PERIOD_SIZE, //1 ms
};
struct pcm_config pcm_config_mmap_capture = {
.channels = DEFAULT_CHANNEL_COUNT,
.rate = DEFAULT_OUTPUT_SAMPLING_RATE,
.period_size = MMAP_PERIOD_SIZE,
.period_count = MMAP_PERIOD_COUNT_DEFAULT,
.format = PCM_FORMAT_S16_LE,
.start_threshold = 0,
.stop_threshold = INT_MAX,
.silence_threshold = 0,
.silence_size = 0,
.avail_min = MMAP_PERIOD_SIZE, //1 ms
};
struct pcm_config pcm_config_voip = {
.channels = 1,
.period_count = 2,
.format = PCM_FORMAT_S16_LE,
.stop_threshold = INT_MAX,
.avail_min = 0,
};
#define AFE_PROXY_CHANNEL_COUNT 2
#define AFE_PROXY_SAMPLING_RATE 48000
#define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768
#define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4
struct pcm_config pcm_config_afe_proxy_playback = {
.channels = AFE_PROXY_CHANNEL_COUNT,
.rate = AFE_PROXY_SAMPLING_RATE,
.period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
.period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT,
.format = PCM_FORMAT_S16_LE,
.start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
.stop_threshold = INT_MAX,
.avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE,
};
#define AFE_PROXY_RECORD_PERIOD_SIZE 768
#define AFE_PROXY_RECORD_PERIOD_COUNT 4
struct pcm_config pcm_config_afe_proxy_record = {
.channels = AFE_PROXY_CHANNEL_COUNT,
.rate = AFE_PROXY_SAMPLING_RATE,
.period_size = AFE_PROXY_RECORD_PERIOD_SIZE,
.period_count = AFE_PROXY_RECORD_PERIOD_COUNT,
.format = PCM_FORMAT_S16_LE,
.start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE,
.stop_threshold = AFE_PROXY_RECORD_PERIOD_SIZE * AFE_PROXY_RECORD_PERIOD_COUNT,
.avail_min = AFE_PROXY_RECORD_PERIOD_SIZE,
};
const char * const use_case_table[AUDIO_USECASE_MAX] = {
[USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback",
[USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback",
[USECASE_AUDIO_PLAYBACK_HIFI] = "hifi-playback",
[USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback",
[USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback",
[USECASE_AUDIO_PLAYBACK_ULL] = "audio-ull-playback",
[USECASE_AUDIO_PLAYBACK_MMAP] = "mmap-playback",
[USECASE_AUDIO_RECORD] = "audio-record",
[USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record",
[USECASE_AUDIO_RECORD_MMAP] = "mmap-record",
[USECASE_AUDIO_RECORD_HIFI] = "hifi-record",
[USECASE_AUDIO_HFP_SCO] = "hfp-sco",
[USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb",
[USECASE_VOICE_CALL] = "voice-call",
[USECASE_VOICE2_CALL] = "voice2-call",
[USECASE_VOLTE_CALL] = "volte-call",
[USECASE_QCHAT_CALL] = "qchat-call",
[USECASE_VOWLAN_CALL] = "vowlan-call",
[USECASE_VOICEMMODE1_CALL] = "voicemmode1-call",
[USECASE_VOICEMMODE2_CALL] = "voicemmode2-call",
[USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib",
[USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record",
[USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback",
[USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record",
[USECASE_INCALL_REC_UPLINK] = "incall-rec-uplink",
[USECASE_INCALL_REC_DOWNLINK] = "incall-rec-downlink",
[USECASE_INCALL_REC_UPLINK_AND_DOWNLINK] = "incall-rec-uplink-and-downlink",
[USECASE_AUDIO_PLAYBACK_VOIP] = "audio-playback-voip",
[USECASE_AUDIO_RECORD_VOIP] = "audio-record-voip",
[USECASE_INCALL_MUSIC_UPLINK] = "incall-music-uplink",
[USECASE_AUDIO_A2DP_ABR_FEEDBACK] = "a2dp-abr-feedback",
};
#define STRING_TO_ENUM(string) { #string, string }
struct string_to_enum {
const char *name;
uint32_t value;
};
static const struct string_to_enum channels_name_to_enum_table[] = {
STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_1),
STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_2),
STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_3),
STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_4),
STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_5),
STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_6),
STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_7),
STRING_TO_ENUM(AUDIO_CHANNEL_INDEX_MASK_8),
};
static int set_voice_volume_l(struct audio_device *adev, float volume);
static struct audio_device *adev = NULL;
static pthread_mutex_t adev_init_lock = PTHREAD_MUTEX_INITIALIZER;
static unsigned int audio_device_ref_count;
//cache last MBDRC cal step level
static int last_known_cal_step = -1 ;
static int check_a2dp_restore_l(struct audio_device *adev, struct stream_out *out, bool restore);
static int set_compr_volume(struct audio_stream_out *stream, float left, float right);
static bool may_use_noirq_mode(struct audio_device *adev, audio_usecase_t uc_id,
int flags __unused)
{
int dir = 0;
switch (uc_id) {
case USECASE_AUDIO_RECORD_LOW_LATENCY:
dir = 1;
case USECASE_AUDIO_PLAYBACK_ULL:
break;
default:
return false;
}
int dev_id = platform_get_pcm_device_id(uc_id, dir == 0 ?
PCM_PLAYBACK : PCM_CAPTURE);
if (adev->adm_is_noirq_avail)
return adev->adm_is_noirq_avail(adev->adm_data,
adev->snd_card, dev_id, dir);
return false;
}
static void register_out_stream(struct stream_out *out)
{
struct audio_device *adev = out->dev;
if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD)
return;
if (!adev->adm_register_output_stream)
return;
adev->adm_register_output_stream(adev->adm_data,
out->handle,
out->flags);
if (!adev->adm_set_config)
return;
if (out->realtime) {
adev->adm_set_config(adev->adm_data,
out->handle,
out->pcm, &out->config);
}
}
static void register_in_stream(struct stream_in *in)
{
struct audio_device *adev = in->dev;
if (!adev->adm_register_input_stream)
return;
adev->adm_register_input_stream(adev->adm_data,
in->capture_handle,
in->flags);
if (!adev->adm_set_config)
return;
if (in->realtime) {
adev->adm_set_config(adev->adm_data,
in->capture_handle,
in->pcm,
&in->config);
}
}
static void request_out_focus(struct stream_out *out, long ns)
{
struct audio_device *adev = out->dev;
if (adev->adm_request_focus_v2) {
adev->adm_request_focus_v2(adev->adm_data, out->handle, ns);
} else if (adev->adm_request_focus) {
adev->adm_request_focus(adev->adm_data, out->handle);
}
}
static void request_in_focus(struct stream_in *in, long ns)
{
struct audio_device *adev = in->dev;
if (adev->adm_request_focus_v2) {
adev->adm_request_focus_v2(adev->adm_data, in->capture_handle, ns);
} else if (adev->adm_request_focus) {
adev->adm_request_focus(adev->adm_data, in->capture_handle);
}
}
static void release_out_focus(struct stream_out *out, long ns __unused)
{
struct audio_device *adev = out->dev;
if (adev->adm_abandon_focus)
adev->adm_abandon_focus(adev->adm_data, out->handle);
}
static void release_in_focus(struct stream_in *in, long ns __unused)
{
struct audio_device *adev = in->dev;
if (adev->adm_abandon_focus)
adev->adm_abandon_focus(adev->adm_data, in->capture_handle);
}
static int parse_snd_card_status(struct str_parms * parms, int * card,
card_status_t * status)
{
char value[32]={0};
char state[32]={0};
int ret = str_parms_get_str(parms, "SND_CARD_STATUS", value, sizeof(value));
if (ret < 0)
return -1;
// sscanf should be okay as value is of max length 32.
// same as sizeof state.
if (sscanf(value, "%d,%s", card, state) < 2)
return -1;
*status = !strcmp(state, "ONLINE") ? CARD_STATUS_ONLINE :
CARD_STATUS_OFFLINE;
return 0;
}
// always call with adev lock held
void send_gain_dep_calibration_l() {
if (last_known_cal_step >= 0)
platform_send_gain_dep_cal(adev->platform, last_known_cal_step);
}
__attribute__ ((visibility ("default")))
bool audio_hw_send_gain_dep_calibration(int level) {
bool ret_val = false;
ALOGV("%s: enter ... ", __func__);
pthread_mutex_lock(&adev_init_lock);
if (adev != NULL && adev->platform != NULL) {
pthread_mutex_lock(&adev->lock);
last_known_cal_step = level;
send_gain_dep_calibration_l();
pthread_mutex_unlock(&adev->lock);
} else {
ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform");
}
pthread_mutex_unlock(&adev_init_lock);
ALOGV("%s: exit with ret_val %d ", __func__, ret_val);
return ret_val;
}
#ifdef MAXXAUDIO_QDSP_ENABLED
bool audio_hw_send_ma_parameter(int stream_type, float vol, bool active)
{
bool ret = false;
ALOGV("%s: enter ...", __func__);
pthread_mutex_lock(&adev_init_lock);
if (adev != NULL && adev->platform != NULL) {
pthread_mutex_lock(&adev->lock);
ret = audio_extn_ma_set_state(adev, stream_type, vol, active);
pthread_mutex_unlock(&adev->lock);
}
pthread_mutex_unlock(&adev_init_lock);
ALOGV("%s: exit with ret %d", __func__, ret);
return ret;
}
#else
#define audio_hw_send_ma_parameter(stream_type, vol, active) (0)
#endif
__attribute__ ((visibility ("default")))
int audio_hw_get_gain_level_mapping(struct amp_db_and_gain_table *mapping_tbl,
int table_size) {
int ret_val = 0;
ALOGV("%s: enter ... ", __func__);
pthread_mutex_lock(&adev_init_lock);
if (adev == NULL) {
ALOGW("%s: adev is NULL .... ", __func__);
goto done;
}
pthread_mutex_lock(&adev->lock);
ret_val = platform_get_gain_level_mapping(mapping_tbl, table_size);
pthread_mutex_unlock(&adev->lock);
done:
pthread_mutex_unlock(&adev_init_lock);
ALOGV("%s: exit ... ", __func__);
return ret_val;
}
static bool is_supported_format(audio_format_t format)
{
switch (format) {
case AUDIO_FORMAT_MP3:
case AUDIO_FORMAT_AAC_LC:
case AUDIO_FORMAT_AAC_HE_V1:
case AUDIO_FORMAT_AAC_HE_V2:
return true;
default:
break;
}
return false;
}
static bool is_supported_24bits_audiosource(audio_source_t source)
{
switch (source) {
case AUDIO_SOURCE_UNPROCESSED:
#ifdef ENABLED_24BITS_CAMCORDER
case AUDIO_SOURCE_CAMCORDER:
#endif
return true;
default:
break;
}
return false;
}
static inline bool is_mmap_usecase(audio_usecase_t uc_id)
{
return (uc_id == USECASE_AUDIO_RECORD_AFE_PROXY) ||
(uc_id == USECASE_AUDIO_PLAYBACK_AFE_PROXY);
}
static int get_snd_codec_id(audio_format_t format)
{
int id = 0;
switch (format & AUDIO_FORMAT_MAIN_MASK) {
case AUDIO_FORMAT_MP3:
id = SND_AUDIOCODEC_MP3;
break;
case AUDIO_FORMAT_AAC:
id = SND_AUDIOCODEC_AAC;
break;
default:
ALOGE("%s: Unsupported audio format", __func__);
}
return id;
}
static int audio_ssr_status(struct audio_device *adev)
{
int ret = 0;
struct mixer_ctl *ctl;
const char *mixer_ctl_name = "Audio SSR Status";
ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name);
ret = mixer_ctl_get_value(ctl, 0);
ALOGD("%s: value: %d", __func__, ret);
return ret;
}
static void stream_app_type_cfg_init(struct stream_app_type_cfg *cfg)
{
cfg->gain[0] = cfg->gain[1] = APP_TYPE_GAIN_DEFAULT;
}
static bool is_btsco_device(snd_device_t out_snd_device, snd_device_t in_snd_device)
{
return out_snd_device == SND_DEVICE_OUT_BT_SCO ||
out_snd_device == SND_DEVICE_OUT_BT_SCO_WB ||
in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB_NREC ||
in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_WB ||
in_snd_device == SND_DEVICE_IN_BT_SCO_MIC_NREC ||
in_snd_device == SND_DEVICE_IN_BT_SCO_MIC;
}
static bool is_a2dp_device(snd_device_t out_snd_device)
{
return out_snd_device == SND_DEVICE_OUT_BT_A2DP;
}
int enable_audio_route(struct audio_device *adev,
struct audio_usecase *usecase)
{
snd_device_t snd_device;
char mixer_path[50];
if (usecase == NULL)
return -EINVAL;
ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
if (usecase->type == PCM_CAPTURE)
snd_device = usecase->in_snd_device;
else
snd_device = usecase->out_snd_device;
audio_extn_utils_send_app_type_cfg(adev, usecase);
audio_extn_utils_send_audio_calibration(adev, usecase);
strcpy(mixer_path, use_case_table[usecase->id]);
platform_add_backend_name(adev->platform, mixer_path, snd_device);
audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_BUSY);
ALOGD("%s: usecase(%d) apply and update mixer path: %s", __func__, usecase->id, mixer_path);
audio_route_apply_and_update_path(adev->audio_route, mixer_path);
ALOGV("%s: exit", __func__);
return 0;
}
int disable_audio_route(struct audio_device *adev,
struct audio_usecase *usecase)
{
snd_device_t snd_device;
char mixer_path[50];
if (usecase == NULL)
return -EINVAL;
ALOGV("%s: enter: usecase(%d)", __func__, usecase->id);
if (usecase->type == PCM_CAPTURE)
snd_device = usecase->in_snd_device;
else
snd_device = usecase->out_snd_device;
strcpy(mixer_path, use_case_table[usecase->id]);
platform_add_backend_name(adev->platform, mixer_path, snd_device);
ALOGD("%s: usecase(%d) reset and update mixer path: %s", __func__, usecase->id, mixer_path);
audio_route_reset_and_update_path(adev->audio_route, mixer_path);
audio_extn_sound_trigger_update_stream_status(usecase, ST_EVENT_STREAM_FREE);
ALOGV("%s: exit", __func__);
return 0;
}
int enable_snd_device(struct audio_device *adev,
snd_device_t snd_device)
{
int i, num_devices = 0;
snd_device_t new_snd_devices[2];
int ret_val = -EINVAL;
if (snd_device < SND_DEVICE_MIN ||
snd_device >= SND_DEVICE_MAX) {
ALOGE("%s: Invalid sound device %d", __func__, snd_device);
goto on_error;
}
platform_send_audio_calibration(adev->platform, snd_device);
if (adev->snd_dev_ref_cnt[snd_device] >= 1) {
ALOGV("%s: snd_device(%d: %s) is already active",
__func__, snd_device, platform_get_snd_device_name(snd_device));
goto on_success;
}
/* due to the possibility of calibration overwrite between listen
and audio, notify sound trigger hal before audio calibration is sent */
audio_extn_sound_trigger_update_device_status(snd_device,
ST_EVENT_SND_DEVICE_BUSY);
if (audio_extn_spkr_prot_is_enabled())
audio_extn_spkr_prot_calib_cancel(adev);
audio_extn_dsm_feedback_enable(adev, snd_device, true);
if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
snd_device == SND_DEVICE_OUT_SPEAKER_SAFE ||
snd_device == SND_DEVICE_OUT_SPEAKER_REVERSE ||
snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
audio_extn_spkr_prot_is_enabled()) {
if (platform_get_snd_device_acdb_id(snd_device) < 0) {
goto on_error;
}
if (audio_extn_spkr_prot_start_processing(snd_device)) {
ALOGE("%s: spkr_start_processing failed", __func__);
goto on_error;
}
} else if (platform_can_split_snd_device(snd_device,
&num_devices,
new_snd_devices) == 0) {
for (i = 0; i < num_devices; i++) {
enable_snd_device(adev, new_snd_devices[i]);
}
platform_set_speaker_gain_in_combo(adev, snd_device, true);
} else {
char device_name[DEVICE_NAME_MAX_SIZE] = {0};
if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
ALOGE(" %s: Invalid sound device returned", __func__);
goto on_error;
}
ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
if (is_a2dp_device(snd_device) &&
(audio_extn_a2dp_start_playback() < 0)) {
ALOGE("%s: failed to configure A2DP control path", __func__);
goto on_error;
}
audio_route_apply_and_update_path(adev->audio_route, device_name);
}
on_success:
adev->snd_dev_ref_cnt[snd_device]++;
ret_val = 0;
on_error:
return ret_val;
}
int disable_snd_device(struct audio_device *adev,
snd_device_t snd_device)
{
int i, num_devices = 0;
snd_device_t new_snd_devices[2];
if (snd_device < SND_DEVICE_MIN ||
snd_device >= SND_DEVICE_MAX) {
ALOGE("%s: Invalid sound device %d", __func__, snd_device);
return -EINVAL;
}
if (adev->snd_dev_ref_cnt[snd_device] <= 0) {
ALOGE("%s: device ref cnt is already 0", __func__);
return -EINVAL;
}
audio_extn_tfa_98xx_disable_speaker(snd_device);
adev->snd_dev_ref_cnt[snd_device]--;
if (adev->snd_dev_ref_cnt[snd_device] == 0) {
audio_extn_dsm_feedback_enable(adev, snd_device, false);
if (is_a2dp_device(snd_device))
audio_extn_a2dp_stop_playback();
if ((snd_device == SND_DEVICE_OUT_SPEAKER ||
snd_device == SND_DEVICE_OUT_SPEAKER_SAFE ||
snd_device == SND_DEVICE_OUT_SPEAKER_REVERSE ||
snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) &&
audio_extn_spkr_prot_is_enabled()) {
audio_extn_spkr_prot_stop_processing(snd_device);
// FIXME b/65363602: bullhead is the only Nexus with audio_extn_spkr_prot_is_enabled()
// and does not use speaker swap. As this code causes a problem with device enable ref
// counting we remove it for now.
// when speaker device is disabled, reset swap.
// will be renabled on usecase start
// platform_set_swap_channels(adev, false);
} else if (platform_can_split_snd_device(snd_device,
&num_devices,
new_snd_devices) == 0) {
for (i = 0; i < num_devices; i++) {
disable_snd_device(adev, new_snd_devices[i]);
}
platform_set_speaker_gain_in_combo(adev, snd_device, false);
} else {
char device_name[DEVICE_NAME_MAX_SIZE] = {0};
if (platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) {
ALOGE(" %s: Invalid sound device returned", __func__);
return -EINVAL;
}
ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, device_name);
audio_route_reset_and_update_path(adev->audio_route, device_name);
}
audio_extn_sound_trigger_update_device_status(snd_device,
ST_EVENT_SND_DEVICE_FREE);
}
return 0;
}
/*
legend:
uc - existing usecase
new_uc - new usecase
d1, d11, d2 - SND_DEVICE enums
a1, a2 - corresponding ANDROID device enums
B, B1, B2 - backend strings
case 1
uc->dev d1 (a1) B1
new_uc->dev d1 (a1), d2 (a2) B1, B2
resolution: disable and enable uc->dev on d1
case 2
uc->dev d1 (a1) B1
new_uc->dev d11 (a1) B1
resolution: need to switch uc since d1 and d11 are related
(e.g. speaker and voice-speaker)
use ANDROID_DEVICE_OUT enums to match devices since SND_DEVICE enums may vary
case 3
uc->dev d1 (a1) B1
new_uc->dev d2 (a2) B2
resolution: no need to switch uc
case 4
uc->dev d1 (a1) B
new_uc->dev d2 (a2) B
resolution: disable enable uc-dev on d2 since backends match
we cannot enable two streams on two different devices if they
share the same backend. e.g. if offload is on speaker device using
QUAD_MI2S backend and a low-latency stream is started on voice-handset
using the same backend, offload must also be switched to voice-handset.
case 5
uc->dev d1 (a1) B
new_uc->dev d1 (a1), d2 (a2) B
resolution: disable enable uc-dev on d2 since backends match
we cannot enable two streams on two different devices if they
share the same backend.
case 6
uc->dev d1 a1 B1
new_uc->dev d2 a1 B2
resolution: no need to switch
case 7
uc->dev d1 (a1), d2 (a2) B1, B2
new_uc->dev d1 B1
resolution: no need to switch
*/
static snd_device_t derive_playback_snd_device(struct audio_usecase *uc,
struct audio_usecase *new_uc,
snd_device_t new_snd_device)
{
audio_devices_t a1 = uc->stream.out->devices;
audio_devices_t a2 = new_uc->stream.out->devices;
snd_device_t d1 = uc->out_snd_device;
snd_device_t d2 = new_snd_device;
// Treat as a special case when a1 and a2 are not disjoint
if ((a1 != a2) && (a1 & a2)) {
snd_device_t d3[2];
int num_devices = 0;
int ret = platform_can_split_snd_device(popcount(a1) > 1 ? d1 : d2,
&num_devices,
d3);
if (ret < 0) {
if (ret != -ENOSYS) {
ALOGW("%s failed to split snd_device %d",
__func__,
popcount(a1) > 1 ? d1 : d2);
}
goto end;
}
// NB: case 7 is hypothetical and isn't a practical usecase yet.
// But if it does happen, we need to give priority to d2 if
// the combo devices active on the existing usecase share a backend.
// This is because we cannot have a usecase active on a combo device
// and a new usecase requests one device in this combo pair.
if (platform_check_backends_match(d3[0], d3[1])) {
return d2; // case 5
} else {
return d1; // case 1
}
} else {
if (platform_check_backends_match(d1, d2)) {
return d2; // case 2, 4
} else {
return d1; // case 6, 3
}
}
end:
return d2; // return whatever was calculated before.
}
static void check_and_route_playback_usecases(struct audio_device *adev,
struct audio_usecase *uc_info,
snd_device_t snd_device)
{
struct listnode *node;
struct audio_usecase *usecase;
bool switch_device[AUDIO_USECASE_MAX];
int i, num_uc_to_switch = 0;
bool force_routing = platform_check_and_set_playback_backend_cfg(adev,
uc_info,
snd_device);
/* For a2dp device reconfigure all active sessions
* with new AFE encoder format based on a2dp state
*/
if ((SND_DEVICE_OUT_BT_A2DP == snd_device ||
SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP == snd_device ||
SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP == snd_device) &&
audio_extn_a2dp_is_force_device_switch()) {
force_routing = true;
}
/*
* This function is to make sure that all the usecases that are active on
* the hardware codec backend are always routed to any one device that is
* handled by the hardware codec.
* For example, if low-latency and deep-buffer usecases are currently active
* on speaker and out_set_parameters(headset) is received on low-latency
* output, then we have to make sure deep-buffer is also switched to headset,
* because of the limitation that both the devices cannot be enabled
* at the same time as they share the same backend.
*/
/* Disable all the usecases on the shared backend other than the
specified usecase */
for (i = 0; i < AUDIO_USECASE_MAX; i++)
switch_device[i] = false;
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
if (usecase->type == PCM_CAPTURE || usecase == uc_info)
continue;
if (force_routing ||
(usecase->out_snd_device != snd_device &&
(usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND ||
usecase->devices & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) &&
platform_check_backends_match(snd_device, usecase->out_snd_device))) {
ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
__func__, use_case_table[usecase->id],
platform_get_snd_device_name(usecase->out_snd_device));
disable_audio_route(adev, usecase);
switch_device[usecase->id] = true;
num_uc_to_switch++;
}
}
if (num_uc_to_switch) {
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
if (switch_device[usecase->id]) {
disable_snd_device(adev, usecase->out_snd_device);
}
}
snd_device_t d_device;
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
if (switch_device[usecase->id]) {
d_device = derive_playback_snd_device(usecase, uc_info,
snd_device);
enable_snd_device(adev, d_device);
/* Update the out_snd_device before enabling the audio route */
usecase->out_snd_device = d_device;
}
}
/* Re-route all the usecases on the shared backend other than the
specified usecase to new snd devices */
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
if (switch_device[usecase->id] ) {
enable_audio_route(adev, usecase);
}
}
}
}
static void check_and_route_capture_usecases(struct audio_device *adev,
struct audio_usecase *uc_info,
snd_device_t snd_device)
{
struct listnode *node;
struct audio_usecase *usecase;
bool switch_device[AUDIO_USECASE_MAX];
int i, num_uc_to_switch = 0;
platform_check_and_set_capture_backend_cfg(adev, uc_info, snd_device);
/*
* This function is to make sure that all the active capture usecases
* are always routed to the same input sound device.
* For example, if audio-record and voice-call usecases are currently
* active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece)
* is received for voice call then we have to make sure that audio-record
* usecase is also switched to earpiece i.e. voice-dmic-ef,
* because of the limitation that two devices cannot be enabled
* at the same time if they share the same backend.
*/
for (i = 0; i < AUDIO_USECASE_MAX; i++)
switch_device[i] = false;
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
if (usecase->type != PCM_PLAYBACK &&
usecase != uc_info &&
usecase->in_snd_device != snd_device &&
(usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) {
ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..",
__func__, use_case_table[usecase->id],
platform_get_snd_device_name(usecase->in_snd_device));
disable_audio_route(adev, usecase);
switch_device[usecase->id] = true;
num_uc_to_switch++;
}
}
if (num_uc_to_switch) {
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
if (switch_device[usecase->id]) {
disable_snd_device(adev, usecase->in_snd_device);
}
}
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
if (switch_device[usecase->id]) {
enable_snd_device(adev, snd_device);
}
}
/* Re-route all the usecases on the shared backend other than the
specified usecase to new snd devices */
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
/* Update the in_snd_device only before enabling the audio route */
if (switch_device[usecase->id] ) {
usecase->in_snd_device = snd_device;
enable_audio_route(adev, usecase);
}
}
}
}
/* must be called with hw device mutex locked */
static int read_hdmi_channel_masks(struct stream_out *out)
{
int ret = 0;
int channels = platform_edid_get_max_channels(out->dev->platform);
switch (channels) {
/*
* Do not handle stereo output in Multi-channel cases
* Stereo case is handled in normal playback path
*/
case 6:
ALOGV("%s: HDMI supports 5.1", __func__);
out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
break;
case 8:
ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__);
out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1;
out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1;
break;
default:
ALOGE("HDMI does not support multi channel playback");
ret = -ENOSYS;
break;
}
return ret;
}
static ssize_t read_usb_sup_sample_rates(bool is_playback,
uint32_t *supported_sample_rates,
uint32_t max_rates)
{
ssize_t count = audio_extn_usb_sup_sample_rates(is_playback,
supported_sample_rates,
max_rates);
#if !LOG_NDEBUG
for (ssize_t i=0; i<count; i++) {
ALOGV("%s %s %d", __func__, is_playback ? "P" : "C",
supported_sample_rates[i]);
}
#endif
return count;
}
static int read_usb_sup_channel_masks(bool is_playback,
audio_channel_mask_t *supported_channel_masks,
uint32_t max_masks)
{
int channels = audio_extn_usb_get_max_channels(is_playback);
int channel_count;
uint32_t num_masks = 0;
if (channels > MAX_HIFI_CHANNEL_COUNT) {
channels = MAX_HIFI_CHANNEL_COUNT;
}
if (is_playback) {
// start from 2 channels as framework currently doesn't support mono.
// TODO: consider only supporting channel index masks beyond stereo here.
for (channel_count = FCC_2;
channel_count <= channels && num_masks < max_masks;
++channel_count) {
supported_channel_masks[num_masks++] = audio_channel_out_mask_from_count(channel_count);
}
for (channel_count = FCC_2;
channel_count <= channels && num_masks < max_masks;
++channel_count) {
supported_channel_masks[num_masks++] =
audio_channel_mask_for_index_assignment_from_count(channel_count);
}
} else {
// For capture we report all supported channel masks from 1 channel up.
channel_count = MIN_CHANNEL_COUNT;
// audio_channel_in_mask_from_count() does the right conversion to either positional or
// indexed mask
for ( ; channel_count <= channels && num_masks < max_masks; channel_count++) {
supported_channel_masks[num_masks++] =
audio_channel_in_mask_from_count(channel_count);
}
}
#ifdef NDEBUG
for (size_t i = 0; i < num_masks; ++i) {
ALOGV("%s: %s supported ch %d supported_channel_masks[%zu] %08x num_masks %d", __func__,
is_playback ? "P" : "C", channels, i, supported_channel_masks[i], num_masks);
}
#endif
return num_masks;
}
static int read_usb_sup_formats(bool is_playback __unused,
audio_format_t *supported_formats,
uint32_t max_formats __unused)
{
int bitwidth = audio_extn_usb_get_max_bit_width(is_playback);
switch (bitwidth) {
case 24:
// XXX : usb.c returns 24 for s24 and s24_le?
supported_formats[0] = AUDIO_FORMAT_PCM_24_BIT_PACKED;
break;
case 32:
supported_formats[0] = AUDIO_FORMAT_PCM_32_BIT;
break;
case 16:
default :
supported_formats[0] = AUDIO_FORMAT_PCM_16_BIT;
break;
}
ALOGV("%s: %s supported format %d", __func__,
is_playback ? "P" : "C", bitwidth);
return 1;
}
static int read_usb_sup_params_and_compare(bool is_playback,
audio_format_t *format,
audio_format_t *supported_formats,
uint32_t max_formats,
audio_channel_mask_t *mask,
audio_channel_mask_t *supported_channel_masks,
uint32_t max_masks,
uint32_t *rate,
uint32_t *supported_sample_rates,
uint32_t max_rates) {
int ret = 0;
int num_formats;
int num_masks;
int num_rates;
int i;
num_formats = read_usb_sup_formats(is_playback, supported_formats,
max_formats);
num_masks = read_usb_sup_channel_masks(is_playback, supported_channel_masks,
max_masks);
num_rates = read_usb_sup_sample_rates(is_playback,
supported_sample_rates, max_rates);
#define LUT(table, len, what, dflt) \
for (i=0; i<len && (table[i] != what); i++); \
if (i==len) { ret |= (what == dflt ? 0 : -1); what=table[0]; }
LUT(supported_formats, num_formats, *format, AUDIO_FORMAT_DEFAULT);
LUT(supported_channel_masks, num_masks, *mask, AUDIO_CHANNEL_NONE);
LUT(supported_sample_rates, num_rates, *rate, 0);
#undef LUT
return ret < 0 ? -EINVAL : 0; // HACK TBD
}
static bool is_usb_ready(struct audio_device *adev, bool is_playback)
{
// Check if usb is ready.
// The usb device may have been removed quickly after insertion and hence
// no longer available. This will show up as empty channel masks, or rates.
pthread_mutex_lock(&adev->lock);
uint32_t supported_sample_rate;
// we consider usb ready if we can fetch at least one sample rate.
const bool ready = read_usb_sup_sample_rates(
is_playback, &supported_sample_rate, 1 /* max_rates */) > 0;
pthread_mutex_unlock(&adev->lock);
return ready;
}
static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev)
{
struct audio_usecase *usecase;
struct listnode *node;
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
if (usecase->type == VOICE_CALL) {
ALOGV("%s: usecase id %d", __func__, usecase->id);
return usecase->id;
}
}
return USECASE_INVALID;
}
struct audio_usecase *get_usecase_from_list(struct audio_device *adev,
audio_usecase_t uc_id)
{
struct audio_usecase *usecase;
struct listnode *node;
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
if (usecase->id == uc_id)
return usecase;
}
return NULL;
}
static bool force_device_switch(struct audio_usecase *usecase)
{
if (usecase->stream.out == NULL) {
ALOGE("%s: stream.out is NULL", __func__);
return false;
}
// Force all A2DP output devices to reconfigure for proper AFE encode format
// Also handle a case where in earlier A2DP start failed as A2DP stream was
// in suspended state, hence try to trigger a retry when we again get a routing request.
if ((usecase->stream.out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
audio_extn_a2dp_is_force_device_switch()) {
ALOGD("%s: Force A2DP device switch to update new encoder config", __func__);
return true;
}
return false;
}
int select_devices(struct audio_device *adev,
audio_usecase_t uc_id)
{
snd_device_t out_snd_device = SND_DEVICE_NONE;
snd_device_t in_snd_device = SND_DEVICE_NONE;
struct audio_usecase *usecase = NULL;
struct audio_usecase *vc_usecase = NULL;
struct audio_usecase *hfp_usecase = NULL;
audio_usecase_t hfp_ucid;
struct listnode *node;
int status = 0;
struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
USECASE_AUDIO_PLAYBACK_VOIP);
usecase = get_usecase_from_list(adev, uc_id);
if (usecase == NULL) {
ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id);
return -EINVAL;
}
if ((usecase->type == VOICE_CALL) ||
(usecase->type == PCM_HFP_CALL)) {
out_snd_device = platform_get_output_snd_device(adev->platform,
usecase->stream.out->devices);
in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices);
usecase->devices = usecase->stream.out->devices;
} else {
/*
* If the voice call is active, use the sound devices of voice call usecase
* so that it would not result any device switch. All the usecases will
* be switched to new device when select_devices() is called for voice call
* usecase. This is to avoid switching devices for voice call when
* check_and_route_playback_usecases() is called below.
*/
if (voice_is_in_call(adev)) {
vc_usecase = get_usecase_from_list(adev,
get_voice_usecase_id_from_list(adev));
if ((vc_usecase != NULL) &&
((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
(usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) {
in_snd_device = vc_usecase->in_snd_device;
out_snd_device = vc_usecase->out_snd_device;
}
} else if (audio_extn_hfp_is_active(adev)) {
hfp_ucid = audio_extn_hfp_get_usecase();
hfp_usecase = get_usecase_from_list(adev, hfp_ucid);
if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) {
in_snd_device = hfp_usecase->in_snd_device;
out_snd_device = hfp_usecase->out_snd_device;
}
}
if (usecase->type == PCM_PLAYBACK) {
usecase->devices = usecase->stream.out->devices;
in_snd_device = SND_DEVICE_NONE;
if (out_snd_device == SND_DEVICE_NONE) {
struct stream_out *voip_out = adev->primary_output;
out_snd_device = platform_get_output_snd_device(adev->platform,
usecase->stream.out->devices);
if (voip_usecase)
voip_out = voip_usecase->stream.out;
if (usecase->stream.out == voip_out &&
adev->active_input &&
(adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
adev->mode == AUDIO_MODE_IN_COMMUNICATION)) {
select_devices(adev, adev->active_input->usecase);
}
}
} else if (usecase->type == PCM_CAPTURE) {
usecase->devices = usecase->stream.in->device;
out_snd_device = SND_DEVICE_NONE;
if (in_snd_device == SND_DEVICE_NONE) {
audio_devices_t out_device = AUDIO_DEVICE_NONE;
if (adev->active_input &&
(adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION ||
adev->mode == AUDIO_MODE_IN_COMMUNICATION)) {
struct audio_usecase *voip_usecase = get_usecase_from_list(adev,
USECASE_AUDIO_PLAYBACK_VOIP);
platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE);
if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) {
out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX;
} else if (voip_usecase) {
out_device = voip_usecase->stream.out->devices;
} else if (adev->primary_output) {
out_device = adev->primary_output->devices;
}
}
in_snd_device = platform_get_input_snd_device(adev->platform, out_device);
}
}
}
if (out_snd_device == usecase->out_snd_device &&
in_snd_device == usecase->in_snd_device) {
if (!force_device_switch(usecase))
return 0;
}
if (is_a2dp_device(out_snd_device) && !audio_extn_a2dp_is_ready()) {
ALOGD("SCO/A2DP is selected but they are not connected/ready hence dont route");
return 0;
}
if ((out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_BT_A2DP ||
out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP) &&
(!audio_extn_a2dp_is_ready())) {
ALOGW("%s: A2DP profile is not ready, routing to speaker only", __func__);
if (out_snd_device == SND_DEVICE_OUT_SPEAKER_SAFE_AND_BT_A2DP)
out_snd_device = SND_DEVICE_OUT_SPEAKER_SAFE;
else
out_snd_device = SND_DEVICE_OUT_SPEAKER;
}
if (usecase->id == USECASE_INCALL_MUSIC_UPLINK) {
out_snd_device = SND_DEVICE_OUT_VOICE_MUSIC_TX;
}
if (out_snd_device != SND_DEVICE_NONE &&
out_snd_device != adev->last_logged_snd_device[uc_id][0]) {
ALOGD("%s: changing use case %s output device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
__func__,
use_case_table[uc_id],
adev->last_logged_snd_device[uc_id][0],
platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][0]),
adev->last_logged_snd_device[uc_id][0] != SND_DEVICE_NONE ?
platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][0]) :
-1,
out_snd_device,
platform_get_snd_device_name(out_snd_device),
platform_get_snd_device_acdb_id(out_snd_device));
adev->last_logged_snd_device[uc_id][0] = out_snd_device;
}
if (in_snd_device != SND_DEVICE_NONE &&
in_snd_device != adev->last_logged_snd_device[uc_id][1]) {
ALOGD("%s: changing use case %s input device from(%d: %s, acdb %d) to (%d: %s, acdb %d)",
__func__,
use_case_table[uc_id],
adev->last_logged_snd_device[uc_id][1],
platform_get_snd_device_name(adev->last_logged_snd_device[uc_id][1]),
adev->last_logged_snd_device[uc_id][1] != SND_DEVICE_NONE ?
platform_get_snd_device_acdb_id(adev->last_logged_snd_device[uc_id][1]) :
-1,
in_snd_device,
platform_get_snd_device_name(in_snd_device),
platform_get_snd_device_acdb_id(in_snd_device));
adev->last_logged_snd_device[uc_id][1] = in_snd_device;
}
/*
* Limitation: While in call, to do a device switch we need to disable
* and enable both RX and TX devices though one of them is same as current
* device.
*/
if ((usecase->type == VOICE_CALL) &&
(usecase->in_snd_device != SND_DEVICE_NONE) &&
(usecase->out_snd_device != SND_DEVICE_NONE)) {
status = platform_switch_voice_call_device_pre(adev->platform);
/* Disable sidetone only if voice call already exists */
if (voice_is_call_state_active(adev))
voice_set_sidetone(adev, usecase->out_snd_device, false);
}
/* Disable current sound devices */
if (usecase->out_snd_device != SND_DEVICE_NONE) {
disable_audio_route(adev, usecase);
disable_snd_device(adev, usecase->out_snd_device);
}
if (usecase->in_snd_device != SND_DEVICE_NONE) {
disable_audio_route(adev, usecase);
disable_snd_device(adev, usecase->in_snd_device);
}
/* Applicable only on the targets that has external modem.
* New device information should be sent to modem before enabling
* the devices to reduce in-call device switch time.
*/
if ((usecase->type == VOICE_CALL) &&
(usecase->in_snd_device != SND_DEVICE_NONE) &&
(usecase->out_snd_device != SND_DEVICE_NONE)) {
status = platform_switch_voice_call_enable_device_config(adev->platform,
out_snd_device,
in_snd_device);
}
/* Enable new sound devices */
if (out_snd_device != SND_DEVICE_NONE) {
if ((usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) ||
(usecase->devices & (AUDIO_DEVICE_OUT_USB_DEVICE|AUDIO_DEVICE_OUT_USB_HEADSET)) ||
(usecase->devices & AUDIO_DEVICE_OUT_ALL_A2DP))
check_and_route_playback_usecases(adev, usecase, out_snd_device);
enable_snd_device(adev, out_snd_device);
}
if (in_snd_device != SND_DEVICE_NONE) {
check_and_route_capture_usecases(adev, usecase, in_snd_device);
enable_snd_device(adev, in_snd_device);
}
if (usecase->type == VOICE_CALL)
status = platform_switch_voice_call_device_post(adev->platform,
out_snd_device,
in_snd_device);
usecase->in_snd_device = in_snd_device;
usecase->out_snd_device = out_snd_device;
audio_extn_tfa_98xx_set_mode();
enable_audio_route(adev, usecase);
audio_extn_ma_set_device(usecase);
/* Applicable only on the targets that has external modem.
* Enable device command should be sent to modem only after
* enabling voice call mixer controls
*/
if (usecase->type == VOICE_CALL) {
status = platform_switch_voice_call_usecase_route_post(adev->platform,
out_snd_device,
in_snd_device);
/* Enable sidetone only if voice call already exists */
if (voice_is_call_state_active(adev))
voice_set_sidetone(adev, out_snd_device, true);
}
if (usecase == voip_usecase) {
struct stream_out *voip_out = voip_usecase->stream.out;
audio_extn_utils_send_app_type_gain(adev,
voip_out->app_type_cfg.app_type,
&voip_out->app_type_cfg.gain[0]);
}
return status;
}
static int stop_input_stream(struct stream_in *in)
{
int i, ret = 0;
struct audio_usecase *uc_info;
struct audio_device *adev = in->dev;
ALOGV("%s: enter: usecase(%d: %s)", __func__,
in->usecase, use_case_table[in->usecase]);
if (adev->active_input) {
if (adev->active_input->usecase == in->usecase) {
adev->active_input = NULL;
} else {
ALOGW("%s adev->active_input->usecase %s, v/s in->usecase %s",
__func__,
use_case_table[adev->active_input->usecase],
use_case_table[in->usecase]);
}
}
uc_info = get_usecase_from_list(adev, in->usecase);
if (uc_info == NULL) {
ALOGE("%s: Could not find the usecase (%d) in the list",
__func__, in->usecase);
return -EINVAL;
}
/* Close in-call recording streams */
voice_check_and_stop_incall_rec_usecase(adev, in);
/* 1. Disable stream specific mixer controls */
disable_audio_route(adev, uc_info);
/* 2. Disable the tx device */
disable_snd_device(adev, uc_info->in_snd_device);
list_remove(&uc_info->list);
free(uc_info);
ALOGV("%s: exit: status(%d)", __func__, ret);
return ret;
}
int start_input_stream(struct stream_in *in)
{
/* 1. Enable output device and stream routing controls */
int ret = 0;
struct audio_usecase *uc_info;
struct audio_device *adev = in->dev;
ALOGV("%s: enter: usecase(%d)", __func__, in->usecase);
if (audio_extn_tfa_98xx_is_supported() && !audio_ssr_status(adev))
return -EIO;
if (in->card_status == CARD_STATUS_OFFLINE ||
adev->card_status == CARD_STATUS_OFFLINE) {
ALOGW("in->card_status or adev->card_status offline, try again");
ret = -EAGAIN;
goto error_config;
}
/* Check if source matches incall recording usecase criteria */
ret = voice_check_and_set_incall_rec_usecase(adev, in);
if (ret)
goto error_config;
else
ALOGV("%s: usecase(%d)", __func__, in->usecase);
in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE);
if (in->pcm_device_id < 0) {
ALOGE("%s: Could not find PCM device id for the usecase(%d)",
__func__, in->usecase);
ret = -EINVAL;
goto error_config;
}
adev->active_input = in;
uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
uc_info->id = in->usecase;
uc_info->type = PCM_CAPTURE;
uc_info->stream.in = in;
uc_info->devices = in->device;
uc_info->in_snd_device = SND_DEVICE_NONE;
uc_info->out_snd_device = SND_DEVICE_NONE;
list_add_tail(&adev->usecase_list, &uc_info->list);
audio_streaming_hint_start();
audio_extn_perf_lock_acquire();
select_devices(adev, in->usecase);
if (in->usecase == USECASE_AUDIO_RECORD_MMAP) {
if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
ALOGE("%s: pcm stream not ready", __func__);
goto error_open;
}
ret = pcm_start(in->pcm);
if (ret < 0) {
ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
goto error_open;
}
} else {
unsigned int flags = PCM_IN | PCM_MONOTONIC;
unsigned int pcm_open_retry_count = 0;
if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) {
flags |= PCM_MMAP | PCM_NOIRQ;
pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
} else if (in->realtime) {
flags |= PCM_MMAP | PCM_NOIRQ;
}
ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d",
__func__, adev->snd_card, in->pcm_device_id, in->config.channels);
while (1) {
in->pcm = pcm_open(adev->snd_card, in->pcm_device_id,
flags, &in->config);
if (in->pcm == NULL || !pcm_is_ready(in->pcm)) {
ALOGE("%s: %s", __func__, pcm_get_error(in->pcm));
if (in->pcm != NULL) {
pcm_close(in->pcm);
in->pcm = NULL;
}
if (pcm_open_retry_count-- == 0) {
ret = -EIO;
goto error_open;
}
usleep(PROXY_OPEN_WAIT_TIME * 1000);
continue;
}
break;
}
ALOGV("%s: pcm_prepare", __func__);
ret = pcm_prepare(in->pcm);
if (ret < 0) {
ALOGE("%s: pcm_prepare returned %d", __func__, ret);
pcm_close(in->pcm);
in->pcm = NULL;
goto error_open;
}
if (in->realtime) {
ret = pcm_start(in->pcm);
if (ret < 0) {
ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
pcm_close(in->pcm);
in->pcm = NULL;
goto error_open;
}
}
}
register_in_stream(in);
audio_streaming_hint_end();
audio_extn_perf_lock_release();
ALOGV("%s: exit", __func__);
return 0;
error_open:
stop_input_stream(in);
audio_streaming_hint_end();
audio_extn_perf_lock_release();
error_config:
adev->active_input = NULL;
ALOGW("%s: exit: status(%d)", __func__, ret);
return ret;
}
void lock_input_stream(struct stream_in *in)
{
pthread_mutex_lock(&in->pre_lock);
pthread_mutex_lock(&in->lock);
pthread_mutex_unlock(&in->pre_lock);
}
void lock_output_stream(struct stream_out *out)
{
pthread_mutex_lock(&out->pre_lock);
pthread_mutex_lock(&out->lock);
pthread_mutex_unlock(&out->pre_lock);
}
/* must be called with out->lock locked */
static int send_offload_cmd_l(struct stream_out* out, int command)
{
struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd));
ALOGVV("%s %d", __func__, command);
cmd->cmd = command;
list_add_tail(&out->offload_cmd_list, &cmd->node);
pthread_cond_signal(&out->offload_cond);
return 0;
}
/* must be called iwth out->lock locked */
static void stop_compressed_output_l(struct stream_out *out)
{
out->offload_state = OFFLOAD_STATE_IDLE;
out->playback_started = 0;
out->send_new_metadata = 1;
if (out->compr != NULL) {
compress_stop(out->compr);
while (out->offload_thread_blocked) {
pthread_cond_wait(&out->cond, &out->lock);
}
}
}
static void *offload_thread_loop(void *context)
{
struct stream_out *out = (struct stream_out *) context;
struct listnode *item;
out->offload_state = OFFLOAD_STATE_IDLE;
out->playback_started = 0;
setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
set_sched_policy(0, SP_FOREGROUND);
prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0);
ALOGV("%s", __func__);
lock_output_stream(out);
for (;;) {
struct offload_cmd *cmd = NULL;
stream_callback_event_t event;
bool send_callback = false;
ALOGVV("%s offload_cmd_list %d out->offload_state %d",
__func__, list_empty(&out->offload_cmd_list),
out->offload_state);
if (list_empty(&out->offload_cmd_list)) {
ALOGV("%s SLEEPING", __func__);
pthread_cond_wait(&out->offload_cond, &out->lock);
ALOGV("%s RUNNING", __func__);
continue;
}
item = list_head(&out->offload_cmd_list);
cmd = node_to_item(item, struct offload_cmd, node);
list_remove(item);
ALOGVV("%s STATE %d CMD %d out->compr %p",
__func__, out->offload_state, cmd->cmd, out->compr);
if (cmd->cmd == OFFLOAD_CMD_EXIT) {
free(cmd);
break;
}
if (out->compr == NULL) {
ALOGE("%s: Compress handle is NULL", __func__);
free(cmd);
pthread_cond_signal(&out->cond);
continue;
}
out->offload_thread_blocked = true;
pthread_mutex_unlock(&out->lock);
send_callback = false;
switch (cmd->cmd) {
case OFFLOAD_CMD_WAIT_FOR_BUFFER:
compress_wait(out->compr, -1);
send_callback = true;
event = STREAM_CBK_EVENT_WRITE_READY;
break;
case OFFLOAD_CMD_PARTIAL_DRAIN:
compress_next_track(out->compr);
compress_partial_drain(out->compr);
send_callback = true;
event = STREAM_CBK_EVENT_DRAIN_READY;
/* Resend the metadata for next iteration */
out->send_new_metadata = 1;
break;
case OFFLOAD_CMD_DRAIN:
compress_drain(out->compr);
send_callback = true;
event = STREAM_CBK_EVENT_DRAIN_READY;
break;
case OFFLOAD_CMD_ERROR:
send_callback = true;
event = STREAM_CBK_EVENT_ERROR;
break;
default:
ALOGE("%s unknown command received: %d", __func__, cmd->cmd);
break;
}
lock_output_stream(out);
out->offload_thread_blocked = false;
pthread_cond_signal(&out->cond);
if (send_callback) {
ALOGVV("%s: sending offload_callback event %d", __func__, event);
out->offload_callback(event, NULL, out->offload_cookie);
}
free(cmd);
}
pthread_cond_signal(&out->cond);
while (!list_empty(&out->offload_cmd_list)) {
item = list_head(&out->offload_cmd_list);
list_remove(item);
free(node_to_item(item, struct offload_cmd, node));
}
pthread_mutex_unlock(&out->lock);
return NULL;
}
static int create_offload_callback_thread(struct stream_out *out)
{
pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL);
list_init(&out->offload_cmd_list);
pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL,
offload_thread_loop, out);
return 0;
}
static int destroy_offload_callback_thread(struct stream_out *out)
{
lock_output_stream(out);
stop_compressed_output_l(out);
send_offload_cmd_l(out, OFFLOAD_CMD_EXIT);
pthread_mutex_unlock(&out->lock);
pthread_join(out->offload_thread, (void **) NULL);
pthread_cond_destroy(&out->offload_cond);
return 0;
}
static bool allow_hdmi_channel_config(struct audio_device *adev)
{
struct listnode *node;
struct audio_usecase *usecase;
bool ret = true;
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
/*
* If voice call is already existing, do not proceed further to avoid
* disabling/enabling both RX and TX devices, CSD calls, etc.
* Once the voice call done, the HDMI channels can be configured to
* max channels of remaining use cases.
*/
if (usecase->id == USECASE_VOICE_CALL) {
ALOGV("%s: voice call is active, no change in HDMI channels",
__func__);
ret = false;
break;
} else if (usecase->id == USECASE_AUDIO_PLAYBACK_HIFI) {
ALOGV("%s: hifi playback is active, "
"no change in HDMI channels", __func__);
ret = false;
break;
}
}
}
return ret;
}
static int check_and_set_hdmi_channels(struct audio_device *adev,
unsigned int channels)
{
struct listnode *node;
struct audio_usecase *usecase;
/* Check if change in HDMI channel config is allowed */
if (!allow_hdmi_channel_config(adev))
return 0;
if (channels == adev->cur_hdmi_channels) {
ALOGV("%s: Requested channels are same as current", __func__);
return 0;
}
platform_set_hdmi_channels(adev->platform, channels);
adev->cur_hdmi_channels = channels;
/*
* Deroute all the playback streams routed to HDMI so that
* the back end is deactivated. Note that backend will not
* be deactivated if any one stream is connected to it.
*/
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
if (usecase->type == PCM_PLAYBACK &&
usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
disable_audio_route(adev, usecase);
}
}
/*
* Enable all the streams disabled above. Now the HDMI backend
* will be activated with new channel configuration
*/
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
if (usecase->type == PCM_PLAYBACK &&
usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) {
enable_audio_route(adev, usecase);
}
}
return 0;
}
static int check_and_set_usb_service_interval(struct audio_device *adev,
struct audio_usecase *uc_info,
bool min)
{
struct listnode *node;
struct audio_usecase *usecase;
bool switch_usecases = false;
bool reconfig = false;
if ((uc_info->id != USECASE_AUDIO_PLAYBACK_MMAP) &&
(uc_info->id != USECASE_AUDIO_PLAYBACK_ULL))
return -1;
/* set if the valid usecase do not already exist */
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
if (usecase->type == PCM_PLAYBACK &&
(audio_is_usb_out_device(usecase->devices & AUDIO_DEVICE_OUT_ALL_USB))) {
switch (usecase->id) {
case USECASE_AUDIO_PLAYBACK_MMAP:
case USECASE_AUDIO_PLAYBACK_ULL:
// cannot reconfig while mmap/ull is present.
return -1;
default:
switch_usecases = true;
break;
}
}
if (switch_usecases)
break;
}
/*
* client can try to set service interval in start_output_stream
* to min or to 0 (i.e reset) in stop_output_stream .
*/
unsigned long service_interval =
audio_extn_usb_find_service_interval(min, true /*playback*/);
int ret = platform_set_usb_service_interval(adev->platform,
true /*playback*/,
service_interval,
&reconfig);
/* no change or not supported or no active usecases */
if (ret || !reconfig || !switch_usecases)
return -1;
return 0;
#undef VALID_USECASE
}
static int stop_output_stream(struct stream_out *out)
{
int i, ret = 0;
struct audio_usecase *uc_info;
struct audio_device *adev = out->dev;
bool has_voip_usecase =
get_usecase_from_list(adev, USECASE_AUDIO_PLAYBACK_VOIP) != NULL;
ALOGV("%s: enter: usecase(%d: %s)", __func__,
out->usecase, use_case_table[out->usecase]);
uc_info = get_usecase_from_list(adev, out->usecase);
if (uc_info == NULL) {
ALOGE("%s: Could not find the usecase (%d) in the list",
__func__, out->usecase);
return -EINVAL;
}
if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
if (adev->visualizer_stop_output != NULL)
adev->visualizer_stop_output(out->handle, out->pcm_device_id);
if (adev->offload_effects_stop_output != NULL)
adev->offload_effects_stop_output(out->handle, out->pcm_device_id);
} else if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
audio_low_latency_hint_end();
}
/* 1. Get and set stream specific mixer controls */
disable_audio_route(adev, uc_info);
/* 2. Disable the rx device */
disable_snd_device(adev, uc_info->out_snd_device);
list_remove(&uc_info->list);
audio_extn_extspk_update(adev->extspk);
/* Must be called after removing the usecase from list */
if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS);
else if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
ret = check_and_set_usb_service_interval(adev, uc_info, false /*min*/);
if (ret == 0) {
/* default service interval was successfully updated,
reopen USB backend with new service interval */
check_and_route_playback_usecases(adev, uc_info, uc_info->out_snd_device);
}
ret = 0;
}
if (has_voip_usecase ||
out->devices & AUDIO_DEVICE_OUT_SPEAKER_SAFE) {
struct listnode *node;
struct audio_usecase *usecase;
list_for_each(node, &adev->usecase_list) {
usecase = node_to_item(node, struct audio_usecase, list);
if (usecase->type == PCM_CAPTURE || usecase == uc_info)
continue;
ALOGD("%s: select_devices at usecase(%d: %s) after removing the usecase(%d: %s)",
__func__, usecase->id, use_case_table[usecase->id],
out->usecase, use_case_table[out->usecase]);
select_devices(adev, usecase->id);
}
}
free(uc_info);
ALOGV("%s: exit: status(%d)", __func__, ret);
return ret;
}
int start_output_stream(struct stream_out *out)
{
int ret = 0;
struct audio_usecase *uc_info;
struct audio_device *adev = out->dev;
bool a2dp_combo = false;
ALOGV("%s: enter: usecase(%d: %s) devices(%#x)",
__func__, out->usecase, use_case_table[out->usecase], out->devices);
if (out->card_status == CARD_STATUS_OFFLINE ||
adev->card_status == CARD_STATUS_OFFLINE) {
ALOGW("out->card_status or adev->card_status offline, try again");
ret = -EAGAIN;
goto error_config;
}
if (out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) {
if (!audio_extn_a2dp_is_ready()) {
if (out->devices & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
a2dp_combo = true;
} else {
if (!(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
ALOGE("%s: A2DP profile is not ready, return error", __func__);
ret = -EAGAIN;
goto error_config;
}
}
}
}
out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK);
if (out->pcm_device_id < 0) {
ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)",
__func__, out->pcm_device_id, out->usecase);
ret = -EINVAL;
goto error_config;
}
uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase));
uc_info->id = out->usecase;
uc_info->type = PCM_PLAYBACK;
uc_info->stream.out = out;
uc_info->devices = out->devices;
uc_info->in_snd_device = SND_DEVICE_NONE;
uc_info->out_snd_device = SND_DEVICE_NONE;
/* This must be called before adding this usecase to the list */
if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL)
check_and_set_hdmi_channels(adev, out->config.channels);
else if (audio_is_usb_out_device(out->devices & AUDIO_DEVICE_OUT_ALL_USB)) {
check_and_set_usb_service_interval(adev, uc_info, true /*min*/);
/* USB backend is not reopened immediately.
This is eventually done as part of select_devices */
}
list_add_tail(&adev->usecase_list, &uc_info->list);
audio_streaming_hint_start();
audio_extn_perf_lock_acquire();
if ((out->devices & AUDIO_DEVICE_OUT_ALL_A2DP) &&
(!audio_extn_a2dp_is_ready())) {
if (!a2dp_combo) {
check_a2dp_restore_l(adev, out, false);
} else {
audio_devices_t dev = out->devices;
if (dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE)
out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
else
out->devices = AUDIO_DEVICE_OUT_SPEAKER;
select_devices(adev, out->usecase);
out->devices = dev;
}
} else {
select_devices(adev, out->usecase);
}
audio_extn_extspk_update(adev->extspk);
ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)",
__func__, adev->snd_card, out->pcm_device_id, out->config.format);
if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
out->pcm = NULL;
out->compr = compress_open(adev->snd_card, out->pcm_device_id,
COMPRESS_IN, &out->compr_config);
if (out->compr && !is_compress_ready(out->compr)) {
ALOGE("%s: %s", __func__, compress_get_error(out->compr));
compress_close(out->compr);
out->compr = NULL;
ret = -EIO;
goto error_open;
}
if (out->offload_callback)
compress_nonblock(out->compr, out->non_blocking);
if (adev->visualizer_start_output != NULL) {
int capture_device_id =
platform_get_pcm_device_id(USECASE_AUDIO_RECORD_AFE_PROXY,
PCM_CAPTURE);
adev->visualizer_start_output(out->handle, out->pcm_device_id,
adev->snd_card, capture_device_id);
}
if (adev->offload_effects_start_output != NULL)
adev->offload_effects_start_output(out->handle, out->pcm_device_id);
} else if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
ALOGE("%s: pcm stream not ready", __func__);
goto error_open;
}
ret = pcm_start(out->pcm);
if (ret < 0) {
ALOGE("%s: MMAP pcm_start failed ret %d", __func__, ret);
goto error_open;
}
} else {
unsigned int flags = PCM_OUT | PCM_MONOTONIC;
unsigned int pcm_open_retry_count = 0;
if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) {
flags |= PCM_MMAP | PCM_NOIRQ;
pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT;
} else if (out->realtime) {
flags |= PCM_MMAP | PCM_NOIRQ;
}
while (1) {
out->pcm = pcm_open(adev->snd_card, out->pcm_device_id,
flags, &out->config);
if (out->pcm == NULL || !pcm_is_ready(out->pcm)) {
ALOGE("%s: %s", __func__, pcm_get_error(out->pcm));
if (out->pcm != NULL) {
pcm_close(out->pcm);
out->pcm = NULL;
}
if (pcm_open_retry_count-- == 0) {
ret = -EIO;
goto error_open;
}
usleep(PROXY_OPEN_WAIT_TIME * 1000);
continue;
}
break;
}
ALOGV("%s: pcm_prepare", __func__);
if (pcm_is_ready(out->pcm)) {
ret = pcm_prepare(out->pcm);
if (ret < 0) {
ALOGE("%s: pcm_prepare returned %d", __func__, ret);
pcm_close(out->pcm);
out->pcm = NULL;
goto error_open;
}
}
if (out->realtime) {
ret = pcm_start(out->pcm);
if (ret < 0) {
ALOGE("%s: RT pcm_start failed ret %d", __func__, ret);
pcm_close(out->pcm);
out->pcm = NULL;
goto error_open;
}
}
}
register_out_stream(out);
audio_streaming_hint_end();
audio_extn_perf_lock_release();
audio_extn_tfa_98xx_enable_speaker();
if (out->usecase == USECASE_AUDIO_PLAYBACK_ULL ||
out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
audio_low_latency_hint_start();
}
// consider a scenario where on pause lower layers are tear down.
// so on resume, swap mixer control need to be sent only when
// backend is active, hence rather than sending from enable device
// sending it from start of streamtream
platform_set_swap_channels(adev, true);
ALOGV("%s: exit", __func__);
return 0;
error_open:
audio_streaming_hint_end();
audio_extn_perf_lock_release();
stop_output_stream(out);
error_config:
return ret;
}
static int check_input_parameters(uint32_t sample_rate,
audio_format_t format,
int channel_count, bool is_usb_hifi)
{
if ((format != AUDIO_FORMAT_PCM_16_BIT) &&
(format != AUDIO_FORMAT_PCM_8_24_BIT) &&
(format != AUDIO_FORMAT_PCM_24_BIT_PACKED) &&
!(is_usb_hifi && (format == AUDIO_FORMAT_PCM_32_BIT))) {
ALOGE("%s: unsupported AUDIO FORMAT (%d) ", __func__, format);
return -EINVAL;
}
int max_channel_count = is_usb_hifi ? MAX_HIFI_CHANNEL_COUNT : MAX_CHANNEL_COUNT;
if ((channel_count < MIN_CHANNEL_COUNT) || (channel_count > max_channel_count)) {
ALOGE("%s: unsupported channel count (%d) passed Min / Max (%d / %d)", __func__,
channel_count, MIN_CHANNEL_COUNT, max_channel_count);
return -EINVAL;
}
switch (sample_rate) {
case 8000:
case 11025:
case 12000:
case 16000:
case 22050:
case 24000:
case 32000:
case 44100:
case 48000:
case 96000:
break;
default:
ALOGE("%s: unsupported (%d) samplerate passed ", __func__, sample_rate);
return -EINVAL;
}
return 0;
}
/** Add a value in a list if not already present.
* @return true if value was successfully inserted or already present,
* false if the list is full and does not contain the value.
*/
static bool register_uint(uint32_t value, uint32_t* list, size_t list_length) {
for (size_t i = 0; i < list_length; i++) {
if (list[i] == value) return true; // value is already present
if (list[i] == 0) { // no values in this slot
list[i] = value;
return true; // value inserted
}
}
return false; // could not insert value
}
/** Add channel_mask in supported_channel_masks if not already present.
* @return true if channel_mask was successfully inserted or already present,
* false if supported_channel_masks is full and does not contain channel_mask.
*/
static void register_channel_mask(audio_channel_mask_t channel_mask,
audio_channel_mask_t supported_channel_masks[static MAX_SUPPORTED_CHANNEL_MASKS]) {
ALOGE_IF(!register_uint(channel_mask, supported_channel_masks, MAX_SUPPORTED_CHANNEL_MASKS),
"%s: stream can not declare supporting its channel_mask %x", __func__, channel_mask);
}
/** Add format in supported_formats if not already present.
* @return true if format was successfully inserted or already present,
* false if supported_formats is full and does not contain format.
*/
static void register_format(audio_format_t format,
audio_format_t supported_formats[static MAX_SUPPORTED_FORMATS]) {
ALOGE_IF(!register_uint(format, supported_formats, MAX_SUPPORTED_FORMATS),
"%s: stream can not declare supporting its format %x", __func__, format);
}
/** Add sample_rate in supported_sample_rates if not already present.
* @return true if sample_rate was successfully inserted or already present,
* false if supported_sample_rates is full and does not contain sample_rate.
*/
static void register_sample_rate(uint32_t sample_rate,
uint32_t supported_sample_rates[static MAX_SUPPORTED_SAMPLE_RATES]) {
ALOGE_IF(!register_uint(sample_rate, supported_sample_rates, MAX_SUPPORTED_SAMPLE_RATES),
"%s: stream can not declare supporting its sample rate %x", __func__, sample_rate);
}
static size_t get_stream_buffer_size(size_t duration_ms,
uint32_t sample_rate,
audio_format_t format,
int channel_count,
bool is_low_latency)
{
size_t size = 0;
size = (sample_rate * duration_ms) / 1000;
if (is_low_latency)
size = configured_low_latency_capture_period_size;
size *= channel_count * audio_bytes_per_sample(format);
/* make sure the size is multiple of 32 bytes
* At 48 kHz mono 16-bit PCM:
* 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15)
* 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10)
*/
size += 0x1f;
size &= ~0x1f;
return size;
}
static uint32_t out_get_sample_rate(const struct audio_stream *stream)
{
struct stream_out *out = (struct stream_out *)stream;
return out->sample_rate;
}
static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused)
{
return -ENOSYS;
}
static size_t out_get_buffer_size(const struct audio_stream *stream)
{
struct stream_out *out = (struct stream_out *)stream;
if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
return out->compr_config.fragment_size;
}
return out->config.period_size * out->af_period_multiplier *
audio_stream_out_frame_size((const struct audio_stream_out *)stream);
}
static uint32_t out_get_channels(const struct audio_stream *stream)
{
struct stream_out *out = (struct stream_out *)stream;
return out->channel_mask;
}
static audio_format_t out_get_format(const struct audio_stream *stream)
{
struct stream_out *out = (struct stream_out *)stream;
return out->format;
}
static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused)
{
return -ENOSYS;
}
/* must be called with out->lock locked */
static int out_standby_l(struct audio_stream *stream)
{
struct stream_out *out = (struct stream_out *)stream;
struct audio_device *adev = out->dev;
bool do_stop = true;
if (!out->standby) {
if (adev->adm_deregister_stream)
adev->adm_deregister_stream(adev->adm_data, out->handle);
pthread_mutex_lock(&adev->lock);
out->standby = true;
if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) {
if (out->pcm) {
pcm_close(out->pcm);
out->pcm = NULL;
}
if (out->usecase == USECASE_AUDIO_PLAYBACK_MMAP) {
do_stop = out->playback_started;
out->playback_started = false;
}
} else {
stop_compressed_output_l(out);
out->gapless_mdata.encoder_delay = 0;
out->gapless_mdata.encoder_padding = 0;
if (out->compr != NULL) {
compress_close(out->compr);
out->compr = NULL;
}
}
if (do_stop) {
stop_output_stream(out);
}
pthread_mutex_unlock(&adev->lock);
}
return 0;
}
static int out_standby(struct audio_stream *stream)
{
struct stream_out *out = (struct stream_out *)stream;
ALOGV("%s: enter: usecase(%d: %s)", __func__,
out->usecase, use_case_table[out->usecase]);
lock_output_stream(out);
out_standby_l(stream);
pthread_mutex_unlock(&out->lock);
ALOGV("%s: exit", __func__);
return 0;
}
static int out_on_error(struct audio_stream *stream)
{
struct stream_out *out = (struct stream_out *)stream;
struct audio_device *adev = out->dev;
bool do_standby = false;
lock_output_stream(out);
if (!out->standby) {
if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) {
stop_compressed_output_l(out);
send_offload_cmd_l(out, OFFLOAD_CMD_ERROR);
} else
do_standby = true;
}
pthread_mutex_unlock(&out->lock);
if (do_standby)
return out_standby(&out->stream.common);
return 0;
}
static int out_dump(const struct audio_stream *stream, int fd)
{
struct stream_out *out = (struct stream_out *)stream;
// We try to get the lock for consistency,
// but it isn't necessary for these variables.
// If we're not in standby, we may be blocked on a write.
const bool locked = (pthread_mutex_trylock(&out->lock) == 0);
dprintf(fd, " Standby: %s\n", out->standby ? "yes" : "no");
dprintf(fd, " Frames written: %lld\n", (long long)out->written);
if (locked) {
pthread_mutex_unlock(&out->lock);
}
// dump error info
(void)error_log_dump(
out->error_log, fd, " " /* prefix */, 0 /* lines */, 0 /* limit_ns */);
return 0;
}
static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms)
{
int ret = 0;
char value[32];
struct compr_gapless_mdata tmp_mdata;
if (!out || !parms) {
return -EINVAL;
}
ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value));
if (ret >= 0) {
tmp_mdata.encoder_delay = atoi(value); //whats a good limit check?
} else {
return -EINVAL;
}
ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value));
if (ret >= 0) {
tmp_mdata.encoder_padding = atoi(value);
} else {
return -EINVAL;
}
out->gapless_mdata = tmp_mdata;
out->send_new_metadata = 1;
ALOGV("%s new encoder delay %u and padding %u", __func__,
out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding);
return 0;
}
static bool output_drives_call(struct audio_device *adev, struct stream_out *out)
{
return out == adev->primary_output || out == adev->voice_tx_output;
}
static int get_alive_usb_card(struct str_parms* parms) {
int card;
if ((str_parms_get_int(parms, "card", &card) >= 0) &&
!audio_extn_usb_alive(card)) {
return card;
}
return -ENODEV;
}
static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
{
struct stream_out *out = (struct stream_out *)stream;
struct audio_device *adev = out->dev;
struct audio_usecase *usecase;
struct listnode *node;
struct str_parms *parms;
char value[32];
int ret, val = 0;
bool select_new_device = false;
int status = 0;
bool bypass_a2dp = false;
ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s",
__func__, out->usecase, use_case_table[out->usecase], kvpairs);
parms = str_parms_create_str(kvpairs);
ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
if (ret >= 0) {
val = atoi(value);
lock_output_stream(out);
// The usb driver needs to be closed after usb device disconnection
// otherwise audio is no longer played on the new usb devices.
// By forcing the stream in standby, the usb stack refcount drops to 0
// and the driver is closed.
if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD && val == AUDIO_DEVICE_NONE &&
audio_is_usb_out_device(out->devices)) {
ALOGD("%s() putting the usb device in standby after disconnection", __func__);
out_standby_l(&out->stream.common);
}
pthread_mutex_lock(&adev->lock);
/*
* When HDMI cable is unplugged the music playback is paused and
* the policy manager sends routing=0. But the audioflinger
* continues to write data until standby time (3sec).
* As the HDMI core is turned off, the write gets blocked.
* Avoid this by routing audio to speaker until standby.
*/
if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL &&
val == AUDIO_DEVICE_NONE) {
val = AUDIO_DEVICE_OUT_SPEAKER;
}
/*
* When A2DP is disconnected the
* music playback is paused and the policy manager sends routing=0
* But the audioflingercontinues to write data until standby time
* (3sec). As BT is turned off, the write gets blocked.
* Avoid this by routing audio to speaker until standby.
*/
if ((out->devices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP) &&
(val == AUDIO_DEVICE_NONE) &&
!audio_extn_a2dp_is_ready()) {
val = AUDIO_DEVICE_OUT_SPEAKER;
}
/* To avoid a2dp to sco overlapping / BT device improper state
* check with BT lib about a2dp streaming support before routing
*/
if (val & AUDIO_DEVICE_OUT_ALL_A2DP) {
if (!audio_extn_a2dp_is_ready()) {
if (val & (AUDIO_DEVICE_OUT_SPEAKER | AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
//combo usecase just by pass a2dp
ALOGW("%s: A2DP profile is not ready,routing to speaker only", __func__);
bypass_a2dp = true;
} else {
ALOGE("%s: A2DP profile is not ready,ignoring routing request", __func__);
/* update device to a2dp and don't route as BT returned error
* However it is still possible a2dp routing called because
* of current active device disconnection (like wired headset)
*/
out->devices = val;
pthread_mutex_unlock(&out->lock);
pthread_mutex_unlock(&adev->lock);
status = -ENOSYS;
goto routing_fail;
}
}
}
audio_devices_t new_dev = val;
// Workaround: If routing to an non existing usb device, fail gracefully
// The routing request will otherwise block during 10 second
int card;
if (audio_is_usb_out_device(new_dev) &&
(card = get_alive_usb_card(parms)) >= 0) {
ALOGW("out_set_parameters() ignoring rerouting to non existing USB card %d", card);
pthread_mutex_unlock(&adev->lock);
pthread_mutex_unlock(&out->lock);
status = -ENOSYS;
goto routing_fail;
}
/*
* select_devices() call below switches all the usecases on the same
* backend to the new device. Refer to check_and_route_playback_usecases() in
* the select_devices(). But how do we undo this?
*
* For example, music playback is active on headset (deep-buffer usecase)
* and if we go to ringtones and select a ringtone, low-latency usecase
* will be started on headset+speaker. As we can't enable headset+speaker
* and headset devices at the same time, select_devices() switches the music
* playback to headset+speaker while starting low-lateny usecase for ringtone.
* So when the ringtone playback is completed, how do we undo the same?
*
* We are relying on the out_set_parameters() call on deep-buffer output,
* once the ringtone playback is ended.
* NOTE: We should not check if the current devices are same as new devices.
* Because select_devices() must be called to switch back the music
* playback to headset.
*/
if (new_dev != AUDIO_DEVICE_NONE) {
bool same_dev = out->devices == new_dev;
out->devices = new_dev;
if (output_drives_call(adev, out)) {
if (!voice_is_call_state_active(adev)) {
if (adev->mode == AUDIO_MODE_IN_CALL) {
adev->current_call_output = out;
ret = voice_start_call(adev);
}
} else {
adev->current_call_output = out;
voice_update_devices_for_all_voice_usecases(adev);
}
}
if (!out->standby) {
if (!same_dev) {
ALOGV("update routing change");
// inform adm before actual routing to prevent glitches.
if (adev->adm_on_routing_change) {
adev->adm_on_routing_change(adev->adm_data,
out->handle);
}
}
if (!bypass_a2dp) {
select_devices(adev, out->usecase);
} else {
if (new_dev & AUDIO_DEVICE_OUT_SPEAKER_SAFE)
out->devices = AUDIO_DEVICE_OUT_SPEAKER_SAFE;
else
out->devices = AUDIO_DEVICE_OUT_SPEAKER;
select_devices(adev, out->usecase);
out->devices = new_dev;
}
audio_extn_tfa_98xx_update();
// on device switch force swap, lower functions will make sure
// to check if swap is allowed or not.
if (!same_dev)
platform_set_swap_channels(adev, true);
if ((out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) &&
out->a2dp_compress_mute &&
(!(out->devices &