| /* |
| * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| * Not a Contribution. |
| * |
| * Copyright (C) 2013 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 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 <cutils/log.h> |
| #include <cutils/str_parms.h> |
| #include <cutils/properties.h> |
| #include <cutils/atomic.h> |
| |
| #include <hardware/audio_effect.h> |
| #include <audio_effects/effect_aec.h> |
| #include <audio_effects/effect_ns.h> |
| #include "audio_hw.h" |
| #include "platform_api.h" |
| #include <platform.h> |
| |
| struct pcm_config pcm_config_audio_capture = { |
| .channels = 2, |
| .period_count = AUDIO_CAPTURE_PERIOD_COUNT, |
| .format = PCM_FORMAT_S16_LE, |
| }; |
| |
| static struct audio_device *adev = NULL; |
| static pthread_mutex_t adev_init_lock; |
| static unsigned int audio_device_ref_count; |
| |
| static int set_voice_volume_l(struct audio_device *adev, float volume); |
| |
| int enable_audio_route(struct audio_device *adev, |
| struct audio_usecase *usecase, |
| bool update_mixer) |
| { |
| snd_device_t snd_device; |
| char mixer_path[MIXER_PATH_MAX_LENGTH]; |
| |
| 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; |
| |
| strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path)); |
| platform_add_backend_name(mixer_path, snd_device); |
| ALOGV("%s: apply mixer path: %s", __func__, mixer_path); |
| audio_route_apply_path(adev->audio_route, mixer_path); |
| if (update_mixer) |
| audio_route_update_mixer(adev->audio_route); |
| |
| ALOGV("%s: exit", __func__); |
| return 0; |
| } |
| |
| int disable_audio_route(struct audio_device *adev, |
| struct audio_usecase *usecase, |
| bool update_mixer) |
| { |
| snd_device_t snd_device; |
| char mixer_path[MIXER_PATH_MAX_LENGTH]; |
| |
| 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; |
| strlcpy(mixer_path, use_case_table[usecase->id], sizeof(mixer_path)); |
| platform_add_backend_name(mixer_path, snd_device); |
| ALOGV("%s: reset mixer path: %s", __func__, mixer_path); |
| audio_route_reset_path(adev->audio_route, mixer_path); |
| if (update_mixer) |
| audio_route_update_mixer(adev->audio_route); |
| |
| ALOGV("%s: exit", __func__); |
| return 0; |
| } |
| |
| int enable_snd_device(struct audio_device *adev, |
| snd_device_t snd_device, |
| bool update_mixer) |
| { |
| char device_name[DEVICE_NAME_MAX_SIZE] = {0}; |
| |
| if (snd_device < SND_DEVICE_MIN || |
| snd_device >= SND_DEVICE_MAX) { |
| ALOGE("%s: Invalid sound device %d", __func__, snd_device); |
| return -EINVAL; |
| } |
| |
| adev->snd_dev_ref_cnt[snd_device]++; |
| |
| if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0 ) { |
| ALOGE("%s: Invalid sound device returned", __func__); |
| return -EINVAL; |
| } |
| if (adev->snd_dev_ref_cnt[snd_device] > 1) { |
| ALOGV("%s: snd_device(%d: %s) is already active", |
| __func__, snd_device, device_name); |
| return 0; |
| } |
| |
| { |
| ALOGV("%s: snd_device(%d: %s)", __func__, |
| snd_device, device_name); |
| if (platform_send_audio_calibration(adev->platform, snd_device) < 0) { |
| adev->snd_dev_ref_cnt[snd_device]--; |
| return -EINVAL; |
| } |
| audio_route_apply_path(adev->audio_route, device_name); |
| } |
| if (update_mixer) |
| audio_route_update_mixer(adev->audio_route); |
| |
| return 0; |
| } |
| |
| int disable_snd_device(struct audio_device *adev, |
| snd_device_t snd_device, |
| bool update_mixer) |
| { |
| char device_name[DEVICE_NAME_MAX_SIZE] = {0}; |
| |
| 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; |
| } |
| |
| adev->snd_dev_ref_cnt[snd_device]--; |
| |
| if(platform_get_snd_device_name_extn(adev->platform, snd_device, device_name) < 0) { |
| ALOGE("%s: Invalid sound device returned", __func__); |
| return -EINVAL; |
| } |
| |
| if (adev->snd_dev_ref_cnt[snd_device] == 0) { |
| ALOGV("%s: snd_device(%d: %s)", __func__, |
| snd_device, device_name); |
| audio_route_reset_path(adev->audio_route, device_name); |
| |
| if (update_mixer) |
| audio_route_update_mixer(adev->audio_route); |
| } |
| |
| return 0; |
| } |
| |
| static void check_usecases_codec_backend(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; |
| |
| /* |
| * 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_PLAYBACK && |
| usecase != uc_info && |
| usecase->out_snd_device != snd_device && |
| usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) { |
| 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, false); |
| switch_device[usecase->id] = true; |
| num_uc_to_switch++; |
| } |
| } |
| |
| if (num_uc_to_switch) { |
| /* Make sure all the streams are de-routed before disabling the device */ |
| audio_route_update_mixer(adev->audio_route); |
| |
| 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, false); |
| } |
| } |
| |
| 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, false); |
| } |
| } |
| /* Make sure new snd device is enabled before re-routing the streams */ |
| audio_route_update_mixer(adev->audio_route); |
| |
| /* 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 out_snd_device only before enabling the audio route */ |
| if (switch_device[usecase->id] ) { |
| usecase->out_snd_device = snd_device; |
| enable_audio_route(adev, usecase, false); |
| } |
| } |
| |
| audio_route_update_mixer(adev->audio_route); |
| } |
| } |
| |
| 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; |
| |
| /* |
| * 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_CAPTURE && |
| usecase != uc_info && |
| usecase->in_snd_device != snd_device) { |
| 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, false); |
| switch_device[usecase->id] = true; |
| num_uc_to_switch++; |
| } |
| } |
| |
| if (num_uc_to_switch) { |
| /* Make sure all the streams are de-routed before disabling the device */ |
| audio_route_update_mixer(adev->audio_route); |
| |
| 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, false); |
| enable_snd_device(adev, snd_device, false); |
| } |
| } |
| |
| /* Make sure new snd device is enabled before re-routing the streams */ |
| audio_route_update_mixer(adev->audio_route); |
| |
| /* 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, false); |
| } |
| } |
| |
| audio_route_update_mixer(adev->audio_route); |
| } |
| } |
| |
| static int disable_all_usecases_of_type(struct audio_device *adev, |
| usecase_type_t usecase_type, |
| bool update_mixer) |
| { |
| struct audio_usecase *usecase; |
| struct listnode *node; |
| int ret = 0; |
| |
| list_for_each(node, &adev->usecase_list) { |
| usecase = node_to_item(node, struct audio_usecase, list); |
| if (usecase->type == usecase_type) { |
| ALOGV("%s: usecase id %d", __func__, usecase->id); |
| ret = disable_audio_route(adev, usecase, update_mixer); |
| if (ret) { |
| ALOGE("%s: Failed to disable usecase id %d", |
| __func__, usecase->id); |
| } |
| } |
| } |
| |
| return ret; |
| } |
| |
| static int enable_all_usecases_of_type(struct audio_device *adev, |
| usecase_type_t usecase_type, |
| bool update_mixer) |
| { |
| struct audio_usecase *usecase; |
| struct listnode *node; |
| int ret = 0; |
| |
| list_for_each(node, &adev->usecase_list) { |
| usecase = node_to_item(node, struct audio_usecase, list); |
| if (usecase->type == usecase_type) { |
| ALOGV("%s: usecase id %d", __func__, usecase->id); |
| ret = enable_audio_route(adev, usecase, update_mixer); |
| if (ret) { |
| ALOGE("%s: Failed to enable usecase id %d", |
| __func__, usecase->id); |
| } |
| } |
| } |
| |
| return ret; |
| } |
| |
| 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; |
| } |
| |
| 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 *voip_usecase = NULL; |
| struct listnode *node; |
| int status = 0; |
| |
| 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 == VOIP_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_usecases_codec_backend() is called below. |
| */ |
| if (usecase->type == PCM_PLAYBACK) { |
| usecase->devices = usecase->stream.out->devices; |
| in_snd_device = SND_DEVICE_NONE; |
| if (out_snd_device == SND_DEVICE_NONE) { |
| out_snd_device = platform_get_output_snd_device(adev->platform, |
| usecase->stream.out->devices); |
| if (usecase->stream.out == adev->primary_output && |
| adev->active_input && |
| adev->active_input->source == AUDIO_SOURCE_VOICE_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) { |
| if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
| adev->primary_output && !adev->primary_output->standby) { |
| in_snd_device = platform_get_input_snd_device(adev->platform, |
| adev->primary_output->devices); |
| } else { |
| in_snd_device = platform_get_input_snd_device(adev->platform, |
| AUDIO_DEVICE_NONE); |
| } |
| } |
| } |
| } |
| |
| if (out_snd_device == usecase->out_snd_device && |
| in_snd_device == usecase->in_snd_device) { |
| return 0; |
| } |
| |
| ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__, |
| out_snd_device, platform_get_snd_device_name(out_snd_device), |
| in_snd_device, platform_get_snd_device_name(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->type == VOIP_CALL) { |
| status = platform_switch_voice_call_device_pre(adev->platform); |
| disable_all_usecases_of_type(adev, VOICE_CALL, true); |
| } |
| |
| /* Disable current sound devices */ |
| if (usecase->out_snd_device != SND_DEVICE_NONE) { |
| disable_audio_route(adev, usecase, true); |
| disable_snd_device(adev, usecase->out_snd_device, false); |
| } |
| |
| if (usecase->in_snd_device != SND_DEVICE_NONE) { |
| disable_audio_route(adev, usecase, true); |
| disable_snd_device(adev, usecase->in_snd_device, false); |
| } |
| |
| /* Enable new sound devices */ |
| if (out_snd_device != SND_DEVICE_NONE) { |
| if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) |
| check_usecases_codec_backend(adev, usecase, out_snd_device); |
| enable_snd_device(adev, out_snd_device, false); |
| } |
| |
| if (in_snd_device != SND_DEVICE_NONE) { |
| check_and_route_capture_usecases(adev, usecase, in_snd_device); |
| enable_snd_device(adev, in_snd_device, false); |
| } |
| |
| if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) |
| status = platform_switch_voice_call_device_post(adev->platform, |
| out_snd_device, |
| in_snd_device); |
| |
| audio_route_update_mixer(adev->audio_route); |
| |
| usecase->in_snd_device = in_snd_device; |
| usecase->out_snd_device = out_snd_device; |
| |
| if (usecase->type == VOICE_CALL || usecase->type == VOIP_CALL) |
| enable_all_usecases_of_type(adev, usecase->type, true); |
| else |
| enable_audio_route(adev, usecase, true); |
| |
| /* 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); |
| |
| 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; |
| |
| adev->active_input = NULL; |
| |
| ALOGV("%s: enter: usecase(%d: %s)", __func__, |
| in->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; |
| } |
| |
| /* 1. Disable stream specific mixer controls */ |
| disable_audio_route(adev, uc_info, true); |
| |
| /* 2. Disable the tx device */ |
| disable_snd_device(adev, uc_info->in_snd_device, true); |
| |
| 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; |
| |
| in->usecase = platform_update_usecase_from_source(in->source,in->usecase); |
| ALOGV("%s: enter: 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); |
| select_devices(adev, in->usecase); |
| |
| ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", |
| __func__, SOUND_CARD, in->pcm_device_id, in->config.channels); |
| in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id, |
| PCM_IN, &in->config); |
| if (in->pcm && !pcm_is_ready(in->pcm)) { |
| ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); |
| pcm_close(in->pcm); |
| in->pcm = NULL; |
| ret = -EIO; |
| goto error_open; |
| } |
| ALOGV("%s: exit", __func__); |
| return ret; |
| |
| error_open: |
| stop_input_stream(in); |
| |
| error_config: |
| adev->active_input = NULL; |
| ALOGD("%s: exit: status(%d)", __func__, ret); |
| |
| return ret; |
| } |
| |
| static int check_input_parameters(uint32_t sample_rate, |
| audio_format_t format, |
| int channel_count) |
| { |
| int ret = 0; |
| |
| if ((format != AUDIO_FORMAT_PCM_16_BIT)) ret = -EINVAL; |
| |
| switch (channel_count) { |
| case 1: |
| case 2: |
| case 6: |
| break; |
| default: |
| ret = -EINVAL; |
| } |
| |
| switch (sample_rate) { |
| case 8000: |
| case 11025: |
| case 12000: |
| case 16000: |
| case 22050: |
| case 24000: |
| case 32000: |
| case 44100: |
| case 48000: |
| break; |
| default: |
| ret = -EINVAL; |
| } |
| |
| return ret; |
| } |
| |
| static size_t get_input_buffer_size(uint32_t sample_rate, |
| audio_format_t format, |
| int channel_count) |
| { |
| size_t size = 0; |
| |
| if (check_input_parameters(sample_rate, format, channel_count) != 0) |
| return 0; |
| |
| size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000; |
| /* ToDo: should use frame_size computed based on the format and |
| channel_count here. */ |
| size *= sizeof(short) * channel_count; |
| |
| /* make sure the size is multiple of 64 */ |
| size += 0x3f; |
| size &= ~0x3f; |
| |
| return size; |
| } |
| |
| /** audio_stream_in implementation **/ |
| static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| { |
| struct stream_in *in = (struct stream_in *)stream; |
| |
| return in->config.rate; |
| } |
| |
| static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| { |
| return -ENOSYS; |
| } |
| |
| static size_t in_get_buffer_size(const struct audio_stream *stream) |
| { |
| struct stream_in *in = (struct stream_in *)stream; |
| |
| return in->config.period_size * audio_stream_frame_size(stream); |
| } |
| |
| static uint32_t in_get_channels(const struct audio_stream *stream) |
| { |
| struct stream_in *in = (struct stream_in *)stream; |
| |
| return in->channel_mask; |
| } |
| |
| static audio_format_t in_get_format(const struct audio_stream *stream) |
| { |
| struct stream_in *in = (struct stream_in *)stream; |
| |
| return in->format; |
| } |
| |
| static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| { |
| return -ENOSYS; |
| } |
| |
| static int in_standby(struct audio_stream *stream) |
| { |
| struct stream_in *in = (struct stream_in *)stream; |
| struct audio_device *adev = in->dev; |
| int status = 0; |
| ALOGV("%s: enter", __func__); |
| |
| if (in->usecase == USECASE_COMPRESS_VOIP_CALL) { |
| /* Ignore standby in case of voip call because the voip input |
| * stream is closed in adev_close_input_stream() |
| */ |
| ALOGV("%s: Ignore Standby in VOIP call", __func__); |
| return status; |
| } |
| |
| pthread_mutex_lock(&in->lock); |
| if (!in->standby) { |
| in->standby = true; |
| if (in->pcm) { |
| pcm_close(in->pcm); |
| in->pcm = NULL; |
| } |
| pthread_mutex_lock(&adev->lock); |
| status = stop_input_stream(in); |
| pthread_mutex_unlock(&adev->lock); |
| } |
| pthread_mutex_unlock(&in->lock); |
| ALOGV("%s: exit: status(%d)", __func__, status); |
| return status; |
| } |
| |
| static int in_dump(const struct audio_stream *stream, int fd) |
| { |
| return 0; |
| } |
| |
| static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| { |
| struct stream_in *in = (struct stream_in *)stream; |
| struct audio_device *adev = in->dev; |
| struct str_parms *parms; |
| char *str; |
| char value[32]; |
| int ret, val = 0; |
| |
| ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs); |
| parms = str_parms_create_str(kvpairs); |
| |
| ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); |
| |
| pthread_mutex_lock(&in->lock); |
| pthread_mutex_lock(&adev->lock); |
| if (ret >= 0) { |
| val = atoi(value); |
| /* no audio source uses val == 0 */ |
| if ((in->source != val) && (val != 0)) { |
| in->source = val; |
| } |
| } |
| |
| ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
| if (ret >= 0) { |
| val = atoi(value); |
| if ((in->device != val) && (val != 0)) { |
| in->device = val; |
| /* If recording is in progress, change the tx device to new device */ |
| if (!in->standby) |
| ret = select_devices(adev, in->usecase); |
| } |
| } |
| |
| pthread_mutex_unlock(&adev->lock); |
| pthread_mutex_unlock(&in->lock); |
| |
| str_parms_destroy(parms); |
| ALOGV("%s: exit: status(%d)", __func__, ret); |
| return ret; |
| } |
| |
| static char* in_get_parameters(const struct audio_stream *stream, |
| const char *keys) |
| { |
| struct stream_in *in = (struct stream_in *)stream; |
| struct str_parms *query = str_parms_create_str(keys); |
| char *str; |
| char value[256]; |
| struct str_parms *reply = str_parms_create(); |
| ALOGV("%s: enter: keys - %s", __func__, keys); |
| |
| str = str_parms_to_str(reply); |
| str_parms_destroy(query); |
| str_parms_destroy(reply); |
| |
| ALOGV("%s: exit: returns - %s", __func__, str); |
| return str; |
| } |
| |
| static int in_set_gain(struct audio_stream_in *stream, float gain) |
| { |
| return 0; |
| } |
| |
| static ssize_t in_read(struct audio_stream_in *stream, void *buffer, |
| size_t bytes) |
| { |
| struct stream_in *in = (struct stream_in *)stream; |
| struct audio_device *adev = in->dev; |
| int i, ret = -1; |
| |
| pthread_mutex_lock(&in->lock); |
| if (in->standby) { |
| pthread_mutex_lock(&adev->lock); |
| ret = start_input_stream(in); |
| pthread_mutex_unlock(&adev->lock); |
| if (ret != 0) { |
| goto exit; |
| } |
| in->standby = 0; |
| } |
| |
| if (in->pcm) { |
| ret = pcm_read(in->pcm, buffer, bytes); |
| } |
| |
| exit: |
| pthread_mutex_unlock(&in->lock); |
| |
| if (ret != 0) { |
| in_standby(&in->stream.common); |
| ALOGV("%s: read failed - sleeping for buffer duration", __func__); |
| usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) / |
| in_get_sample_rate(&in->stream.common)); |
| } |
| return bytes; |
| } |
| |
| static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| { |
| return 0; |
| } |
| |
| static int add_remove_audio_effect(const struct audio_stream *stream, |
| effect_handle_t effect, |
| bool enable) |
| { |
| struct stream_in *in = (struct stream_in *)stream; |
| int status = 0; |
| |
| return 0; |
| } |
| |
| static int in_add_audio_effect(const struct audio_stream *stream, |
| effect_handle_t effect) |
| { |
| ALOGV("%s: effect %p", __func__, effect); |
| return add_remove_audio_effect(stream, effect, true); |
| } |
| |
| static int in_remove_audio_effect(const struct audio_stream *stream, |
| effect_handle_t effect) |
| { |
| ALOGV("%s: effect %p", __func__, effect); |
| return add_remove_audio_effect(stream, effect, false); |
| } |
| |
| static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| { |
| struct audio_device *adev = (struct audio_device *)dev; |
| struct str_parms *parms; |
| char *str; |
| char value[32]; |
| int val; |
| int ret; |
| |
| ALOGD("%s: enter: %s", __func__, kvpairs); |
| |
| pthread_mutex_lock(&adev->lock); |
| parms = str_parms_create_str(kvpairs); |
| |
| platform_set_parameters(adev->platform, parms); |
| |
| ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); |
| if (ret >= 0) { |
| /* When set to false, HAL should disable EC and NS |
| * But it is currently not supported. |
| */ |
| if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| adev->bluetooth_nrec = true; |
| else |
| adev->bluetooth_nrec = false; |
| } |
| |
| ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); |
| if (ret >= 0) { |
| if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| adev->screen_off = false; |
| else |
| adev->screen_off = true; |
| } |
| |
| ret = str_parms_get_int(parms, "rotation", &val); |
| if (ret >= 0) { |
| bool reverse_speakers = false; |
| switch(val) { |
| // FIXME: note that the code below assumes that the speakers are in the correct placement |
| // relative to the user when the device is rotated 90deg from its default rotation. This |
| // assumption is device-specific, not platform-specific like this code. |
| case 270: |
| reverse_speakers = true; |
| break; |
| case 0: |
| case 90: |
| case 180: |
| break; |
| default: |
| ALOGE("%s: unexpected rotation of %d", __func__, val); |
| } |
| if (adev->speaker_lr_swap != reverse_speakers) { |
| adev->speaker_lr_swap = reverse_speakers; |
| // only update the selected device if there is active pcm playback |
| 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 == PCM_PLAYBACK) { |
| select_devices(adev, usecase->id); |
| break; |
| } |
| } |
| } |
| } |
| |
| str_parms_destroy(parms); |
| |
| pthread_mutex_unlock(&adev->lock); |
| ALOGV("%s: exit with code(%d)", __func__, ret); |
| return ret; |
| } |
| |
| static char* adev_get_parameters(const struct audio_hw_device *dev, |
| const char *keys) |
| { |
| struct audio_device *adev = (struct audio_device *)dev; |
| struct str_parms *reply = str_parms_create(); |
| struct str_parms *query = str_parms_create_str(keys); |
| char *str; |
| |
| pthread_mutex_lock(&adev->lock); |
| |
| platform_get_parameters(adev->platform, query, reply); |
| str = str_parms_to_str(reply); |
| str_parms_destroy(query); |
| str_parms_destroy(reply); |
| |
| pthread_mutex_unlock(&adev->lock); |
| ALOGV("%s: exit: returns - %s", __func__, str); |
| return str; |
| } |
| |
| static int adev_init_check(const struct audio_hw_device *dev) |
| { |
| return 0; |
| } |
| |
| static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| { |
| int ret = 0; |
| return ret; |
| } |
| |
| static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| { |
| return -ENOSYS; |
| } |
| |
| static int adev_get_master_volume(struct audio_hw_device *dev, |
| float *volume) |
| { |
| return -ENOSYS; |
| } |
| |
| static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) |
| { |
| return -ENOSYS; |
| } |
| |
| static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) |
| { |
| return -ENOSYS; |
| } |
| |
| static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| { |
| struct audio_device *adev = (struct audio_device *)dev; |
| pthread_mutex_lock(&adev->lock); |
| if (adev->mode != mode) { |
| ALOGD("%s mode %d\n", __func__, mode); |
| adev->mode = mode; |
| } |
| pthread_mutex_unlock(&adev->lock); |
| return 0; |
| } |
| |
| static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| { |
| int ret = 0; |
| |
| return ret; |
| } |
| |
| static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| { |
| return 0; |
| } |
| |
| static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, |
| const struct audio_config *config) |
| { |
| int channel_count = popcount(config->channel_mask); |
| |
| return get_input_buffer_size(config->sample_rate, config->format, channel_count); |
| } |
| |
| static int adev_open_input_stream(struct audio_hw_device *dev, |
| audio_io_handle_t handle, |
| audio_devices_t devices, |
| struct audio_config *config, |
| struct audio_stream_in **stream_in) |
| { |
| struct audio_device *adev = (struct audio_device *)dev; |
| struct stream_in *in; |
| int ret = 0, buffer_size, frame_size; |
| int channel_count = popcount(config->channel_mask); |
| |
| ALOGV("%s: enter", __func__); |
| *stream_in = NULL; |
| if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0) |
| return -EINVAL; |
| |
| in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); |
| |
| in->stream.common.get_sample_rate = in_get_sample_rate; |
| in->stream.common.set_sample_rate = in_set_sample_rate; |
| in->stream.common.get_buffer_size = in_get_buffer_size; |
| in->stream.common.get_channels = in_get_channels; |
| in->stream.common.get_format = in_get_format; |
| in->stream.common.set_format = in_set_format; |
| in->stream.common.standby = in_standby; |
| in->stream.common.dump = in_dump; |
| in->stream.common.set_parameters = in_set_parameters; |
| in->stream.common.get_parameters = in_get_parameters; |
| in->stream.common.add_audio_effect = in_add_audio_effect; |
| in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| in->stream.set_gain = in_set_gain; |
| in->stream.read = in_read; |
| in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| |
| in->device = devices; |
| in->source = AUDIO_SOURCE_DEFAULT; |
| in->dev = adev; |
| in->standby = 1; |
| in->channel_mask = config->channel_mask; |
| |
| /* Update config params with the requested sample rate and channels */ |
| in->usecase = USECASE_AUDIO_RECORD; |
| in->config = pcm_config_audio_capture; |
| in->config.rate = config->sample_rate; |
| in->format = config->format; |
| |
| { |
| in->config.channels = channel_count; |
| frame_size = audio_stream_frame_size((struct audio_stream *)in); |
| buffer_size = get_input_buffer_size(config->sample_rate, |
| config->format, |
| channel_count); |
| in->config.period_size = buffer_size / frame_size; |
| } |
| |
| *stream_in = &in->stream; |
| ALOGV("%s: exit", __func__); |
| return ret; |
| |
| err_open: |
| free(in); |
| *stream_in = NULL; |
| return ret; |
| } |
| |
| static void adev_close_input_stream(struct audio_hw_device *dev, |
| struct audio_stream_in *stream) |
| { |
| int ret; |
| struct stream_in *in = (struct stream_in *)stream; |
| ALOGV("%s", __func__); |
| |
| in_standby(&stream->common); |
| |
| free(stream); |
| |
| return; |
| } |
| |
| static int adev_dump(const audio_hw_device_t *device, int fd) |
| { |
| return 0; |
| } |
| |
| static int adev_close(hw_device_t *device) |
| { |
| struct audio_device *adev = (struct audio_device *)device; |
| |
| if (!adev) |
| return 0; |
| |
| pthread_mutex_lock(&adev_init_lock); |
| |
| if ((--audio_device_ref_count) == 0) { |
| audio_route_free(adev->audio_route); |
| free(adev->snd_dev_ref_cnt); |
| platform_deinit(adev->platform); |
| free(device); |
| adev = NULL; |
| } |
| pthread_mutex_unlock(&adev_init_lock); |
| return 0; |
| } |
| |
| static int adev_open(const hw_module_t *module, const char *name, |
| hw_device_t **device) |
| { |
| int i, ret; |
| |
| ALOGD("%s: enter", __func__); |
| if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; |
| |
| pthread_mutex_lock(&adev_init_lock); |
| if (audio_device_ref_count != 0){ |
| *device = &adev->device.common; |
| audio_device_ref_count++; |
| ALOGD("%s: returning existing instance of adev", __func__); |
| ALOGD("%s: exit", __func__); |
| pthread_mutex_unlock(&adev_init_lock); |
| return 0; |
| } |
| |
| adev = calloc(1, sizeof(struct audio_device)); |
| |
| adev->device.common.tag = HARDWARE_DEVICE_TAG; |
| adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
| adev->device.common.module = (struct hw_module_t *)module; |
| adev->device.common.close = adev_close; |
| |
| adev->device.init_check = adev_init_check; |
| adev->device.set_voice_volume = adev_set_voice_volume; |
| adev->device.set_master_volume = adev_set_master_volume; |
| adev->device.get_master_volume = adev_get_master_volume; |
| adev->device.set_master_mute = adev_set_master_mute; |
| adev->device.get_master_mute = adev_get_master_mute; |
| adev->device.set_mode = adev_set_mode; |
| adev->device.set_mic_mute = adev_set_mic_mute; |
| adev->device.get_mic_mute = adev_get_mic_mute; |
| adev->device.set_parameters = adev_set_parameters; |
| adev->device.get_parameters = adev_get_parameters; |
| adev->device.get_input_buffer_size = adev_get_input_buffer_size; |
| adev->device.open_output_stream = adev_open_output_stream; |
| adev->device.close_output_stream = adev_close_output_stream; |
| adev->device.open_input_stream = adev_open_input_stream; |
| adev->device.close_input_stream = adev_close_input_stream; |
| adev->device.dump = adev_dump; |
| |
| /* Set the default route before the PCM stream is opened */ |
| adev->mode = AUDIO_MODE_NORMAL; |
| adev->active_input = NULL; |
| adev->primary_output = NULL; |
| adev->out_device = AUDIO_DEVICE_NONE; |
| adev->bluetooth_nrec = true; |
| adev->acdb_settings = TTY_MODE_OFF; |
| /* adev->cur_hdmi_channels = 0; by calloc() */ |
| adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int)); |
| list_init(&adev->usecase_list); |
| |
| /* Loads platform specific libraries dynamically */ |
| adev->platform = platform_init(adev); |
| if (!adev->platform) { |
| free(adev->snd_dev_ref_cnt); |
| free(adev); |
| ALOGE("%s: Failed to init platform data, aborting.", __func__); |
| *device = NULL; |
| return -EINVAL; |
| } |
| |
| if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) { |
| adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW); |
| if (adev->visualizer_lib == NULL) { |
| ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH); |
| } else { |
| ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH); |
| adev->visualizer_start_output = |
| (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib, |
| "visualizer_hal_start_output"); |
| adev->visualizer_stop_output = |
| (int (*)(audio_io_handle_t))dlsym(adev->visualizer_lib, |
| "visualizer_hal_stop_output"); |
| } |
| } |
| *device = &adev->device.common; |
| |
| audio_device_ref_count++; |
| pthread_mutex_unlock(&adev_init_lock); |
| |
| ALOGV("%s: exit", __func__); |
| return 0; |
| } |
| |
| static struct hw_module_methods_t hal_module_methods = { |
| .open = adev_open, |
| }; |
| |
| struct audio_module HAL_MODULE_INFO_SYM = { |
| .common = { |
| .tag = HARDWARE_MODULE_TAG, |
| .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| .hal_api_version = HARDWARE_HAL_API_VERSION, |
| .id = AUDIO_HARDWARE_MODULE_ID, |
| .name = "MPQ Audio HAL", |
| .author = "The Linux Foundation", |
| .methods = &hal_module_methods, |
| }, |
| }; |