Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1 | /* |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2 | * Copyright (C) 2013-2014 The Android Open Source Project |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "audio_hw_primary" |
| 18 | /*#define LOG_NDEBUG 0*/ |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 19 | /*#define VERY_VERY_VERBOSE_LOGGING*/ |
| 20 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 21 | #define ALOGVV ALOGV |
| 22 | #else |
| 23 | #define ALOGVV(a...) do { } while(0) |
| 24 | #endif |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 25 | |
| 26 | #include <errno.h> |
| 27 | #include <pthread.h> |
| 28 | #include <stdint.h> |
| 29 | #include <sys/time.h> |
| 30 | #include <stdlib.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 31 | #include <math.h> |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 32 | #include <dlfcn.h> |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 33 | #include <sys/resource.h> |
| 34 | #include <sys/prctl.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 35 | |
| 36 | #include <cutils/log.h> |
| 37 | #include <cutils/str_parms.h> |
| 38 | #include <cutils/properties.h> |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 39 | #include <cutils/atomic.h> |
| 40 | #include <cutils/sched_policy.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 41 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 42 | #include <hardware/audio_effect.h> |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 43 | #include <hardware/audio_alsaops.h> |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 44 | #include <system/thread_defs.h> |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 45 | #include <audio_effects/effect_aec.h> |
| 46 | #include <audio_effects/effect_ns.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 47 | #include "audio_hw.h" |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 48 | #include "audio_extn.h" |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 49 | #include "platform_api.h" |
| 50 | #include <platform.h> |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 51 | #include "voice_extn.h" |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 52 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 53 | #include "sound/compress_params.h" |
| 54 | |
Marco Nelissen | 32093f5 | 2015-04-08 15:14:02 -0700 | [diff] [blame] | 55 | #define COMPRESS_OFFLOAD_FRAGMENT_SIZE (256 * 1024) |
Marco Nelissen | 94c33a0 | 2015-05-12 09:11:34 -0700 | [diff] [blame] | 56 | // 2 buffers causes problems with high bitrate files |
| 57 | #define COMPRESS_OFFLOAD_NUM_FRAGMENTS 3 |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 58 | /* ToDo: Check and update a proper value in msec */ |
| 59 | #define COMPRESS_OFFLOAD_PLAYBACK_LATENCY 96 |
| 60 | #define COMPRESS_PLAYBACK_VOLUME_MAX 0x2000 |
| 61 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 62 | #define PROXY_OPEN_RETRY_COUNT 100 |
| 63 | #define PROXY_OPEN_WAIT_TIME 20 |
| 64 | |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 65 | static unsigned int configured_low_latency_capture_period_size = |
| 66 | LOW_LATENCY_CAPTURE_PERIOD_SIZE; |
| 67 | |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 68 | /* This constant enables extended precision handling. |
| 69 | * TODO The flag is off until more testing is done. |
| 70 | */ |
| 71 | static const bool k_enable_extended_precision = false; |
| 72 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 73 | struct pcm_config pcm_config_deep_buffer = { |
| 74 | .channels = 2, |
| 75 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 76 | .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, |
| 77 | .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, |
| 78 | .format = PCM_FORMAT_S16_LE, |
| 79 | .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, |
| 80 | .stop_threshold = INT_MAX, |
| 81 | .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, |
| 82 | }; |
| 83 | |
| 84 | struct pcm_config pcm_config_low_latency = { |
| 85 | .channels = 2, |
| 86 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 87 | .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE, |
| 88 | .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT, |
| 89 | .format = PCM_FORMAT_S16_LE, |
| 90 | .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 91 | .stop_threshold = INT_MAX, |
| 92 | .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 93 | }; |
| 94 | |
| 95 | struct pcm_config pcm_config_hdmi_multi = { |
| 96 | .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ |
| 97 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */ |
| 98 | .period_size = HDMI_MULTI_PERIOD_SIZE, |
| 99 | .period_count = HDMI_MULTI_PERIOD_COUNT, |
| 100 | .format = PCM_FORMAT_S16_LE, |
| 101 | .start_threshold = 0, |
| 102 | .stop_threshold = INT_MAX, |
| 103 | .avail_min = 0, |
| 104 | }; |
| 105 | |
| 106 | struct pcm_config pcm_config_audio_capture = { |
| 107 | .channels = 2, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 108 | .period_count = AUDIO_CAPTURE_PERIOD_COUNT, |
| 109 | .format = PCM_FORMAT_S16_LE, |
Eric Laurent | e2d2d1d | 2015-07-06 17:54:15 -0700 | [diff] [blame] | 110 | .stop_threshold = INT_MAX, |
| 111 | .avail_min = 0, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 114 | #define AFE_PROXY_CHANNEL_COUNT 2 |
| 115 | #define AFE_PROXY_SAMPLING_RATE 48000 |
| 116 | |
| 117 | #define AFE_PROXY_PLAYBACK_PERIOD_SIZE 768 |
| 118 | #define AFE_PROXY_PLAYBACK_PERIOD_COUNT 4 |
| 119 | |
| 120 | struct pcm_config pcm_config_afe_proxy_playback = { |
| 121 | .channels = AFE_PROXY_CHANNEL_COUNT, |
| 122 | .rate = AFE_PROXY_SAMPLING_RATE, |
| 123 | .period_size = AFE_PROXY_PLAYBACK_PERIOD_SIZE, |
| 124 | .period_count = AFE_PROXY_PLAYBACK_PERIOD_COUNT, |
| 125 | .format = PCM_FORMAT_S16_LE, |
| 126 | .start_threshold = AFE_PROXY_PLAYBACK_PERIOD_SIZE, |
| 127 | .stop_threshold = INT_MAX, |
| 128 | .avail_min = AFE_PROXY_PLAYBACK_PERIOD_SIZE, |
| 129 | }; |
| 130 | |
| 131 | #define AFE_PROXY_RECORD_PERIOD_SIZE 768 |
| 132 | #define AFE_PROXY_RECORD_PERIOD_COUNT 4 |
| 133 | |
| 134 | struct pcm_config pcm_config_afe_proxy_record = { |
| 135 | .channels = AFE_PROXY_CHANNEL_COUNT, |
| 136 | .rate = AFE_PROXY_SAMPLING_RATE, |
| 137 | .period_size = AFE_PROXY_RECORD_PERIOD_SIZE, |
| 138 | .period_count = AFE_PROXY_RECORD_PERIOD_COUNT, |
| 139 | .format = PCM_FORMAT_S16_LE, |
| 140 | .start_threshold = AFE_PROXY_RECORD_PERIOD_SIZE, |
| 141 | .stop_threshold = INT_MAX, |
| 142 | .avail_min = AFE_PROXY_RECORD_PERIOD_SIZE, |
| 143 | }; |
| 144 | |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 145 | const char * const use_case_table[AUDIO_USECASE_MAX] = { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 146 | [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback", |
| 147 | [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback", |
| 148 | [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 149 | [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback", |
Ravi Kumar Alamanda | f78a4d9 | 2015-04-24 15:18:23 -0700 | [diff] [blame] | 150 | [USECASE_AUDIO_PLAYBACK_TTS] = "audio-tts-playback", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 151 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 152 | [USECASE_AUDIO_RECORD] = "audio-record", |
| 153 | [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 154 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 155 | [USECASE_AUDIO_HFP_SCO] = "hfp-sco", |
| 156 | [USECASE_AUDIO_HFP_SCO_WB] = "hfp-sco-wb", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 157 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 158 | [USECASE_VOICE_CALL] = "voice-call", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 159 | [USECASE_VOICE2_CALL] = "voice2-call", |
| 160 | [USECASE_VOLTE_CALL] = "volte-call", |
| 161 | [USECASE_QCHAT_CALL] = "qchat-call", |
| 162 | [USECASE_VOWLAN_CALL] = "vowlan-call", |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 163 | |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 164 | [USECASE_AUDIO_SPKR_CALIB_RX] = "spkr-rx-calib", |
| 165 | [USECASE_AUDIO_SPKR_CALIB_TX] = "spkr-vi-record", |
| 166 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 167 | [USECASE_AUDIO_PLAYBACK_AFE_PROXY] = "afe-proxy-playback", |
| 168 | [USECASE_AUDIO_RECORD_AFE_PROXY] = "afe-proxy-record", |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 171 | |
| 172 | #define STRING_TO_ENUM(string) { #string, string } |
| 173 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 174 | struct string_to_enum { |
| 175 | const char *name; |
| 176 | uint32_t value; |
| 177 | }; |
| 178 | |
| 179 | static const struct string_to_enum out_channels_name_to_enum_table[] = { |
| 180 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), |
| 181 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), |
| 182 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), |
| 183 | }; |
| 184 | |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 185 | static int set_voice_volume_l(struct audio_device *adev, float volume); |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 186 | static struct audio_device *adev = NULL; |
| 187 | static pthread_mutex_t adev_init_lock; |
| 188 | static unsigned int audio_device_ref_count; |
| 189 | |
| 190 | __attribute__ ((visibility ("default"))) |
| 191 | bool audio_hw_send_gain_dep_calibration(int level) { |
| 192 | bool ret_val = false; |
| 193 | ALOGV("%s: enter ... ", __func__); |
| 194 | |
| 195 | pthread_mutex_lock(&adev_init_lock); |
| 196 | |
| 197 | if (adev != NULL && adev->platform != NULL) { |
| 198 | pthread_mutex_lock(&adev->lock); |
| 199 | ret_val = platform_send_gain_dep_cal(adev->platform, level); |
| 200 | pthread_mutex_unlock(&adev->lock); |
| 201 | } else { |
| 202 | ALOGE("%s: %s is NULL", __func__, adev == NULL ? "adev" : "adev->platform"); |
| 203 | } |
| 204 | |
| 205 | pthread_mutex_unlock(&adev_init_lock); |
| 206 | |
| 207 | ALOGV("%s: exit with ret_val %d ", __func__, ret_val); |
| 208 | return ret_val; |
| 209 | } |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 210 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 211 | static bool is_supported_format(audio_format_t format) |
| 212 | { |
Eric Laurent | 8251ac8 | 2014-07-23 11:00:25 -0700 | [diff] [blame] | 213 | switch (format) { |
| 214 | case AUDIO_FORMAT_MP3: |
| 215 | case AUDIO_FORMAT_AAC_LC: |
| 216 | case AUDIO_FORMAT_AAC_HE_V1: |
| 217 | case AUDIO_FORMAT_AAC_HE_V2: |
| 218 | return true; |
| 219 | default: |
| 220 | break; |
| 221 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 222 | return false; |
| 223 | } |
| 224 | |
| 225 | static int get_snd_codec_id(audio_format_t format) |
| 226 | { |
| 227 | int id = 0; |
| 228 | |
Eric Laurent | 8251ac8 | 2014-07-23 11:00:25 -0700 | [diff] [blame] | 229 | switch (format & AUDIO_FORMAT_MAIN_MASK) { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 230 | case AUDIO_FORMAT_MP3: |
| 231 | id = SND_AUDIOCODEC_MP3; |
| 232 | break; |
| 233 | case AUDIO_FORMAT_AAC: |
| 234 | id = SND_AUDIOCODEC_AAC; |
| 235 | break; |
| 236 | default: |
| 237 | ALOGE("%s: Unsupported audio format", __func__); |
| 238 | } |
| 239 | |
| 240 | return id; |
| 241 | } |
Ravi Kumar Alamanda | f996704 | 2013-02-14 19:35:14 -0800 | [diff] [blame] | 242 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 243 | int enable_audio_route(struct audio_device *adev, |
| 244 | struct audio_usecase *usecase) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 245 | { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 246 | snd_device_t snd_device; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 247 | char mixer_path[50]; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 248 | |
| 249 | if (usecase == NULL) |
| 250 | return -EINVAL; |
| 251 | |
| 252 | ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); |
| 253 | |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 254 | if (usecase->type == PCM_CAPTURE) |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 255 | snd_device = usecase->in_snd_device; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 256 | else |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 257 | snd_device = usecase->out_snd_device; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 258 | |
| 259 | strcpy(mixer_path, use_case_table[usecase->id]); |
Ravi Kumar Alamanda | 299760a | 2013-11-01 17:29:09 -0500 | [diff] [blame] | 260 | platform_add_backend_name(adev->platform, mixer_path, snd_device); |
Ravi Kumar Alamanda | 6402646 | 2014-09-15 00:08:58 -0700 | [diff] [blame] | 261 | ALOGD("%s: apply and update mixer path: %s", __func__, mixer_path); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 262 | audio_route_apply_and_update_path(adev->audio_route, mixer_path); |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 263 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 264 | ALOGV("%s: exit", __func__); |
| 265 | return 0; |
| 266 | } |
| 267 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 268 | int disable_audio_route(struct audio_device *adev, |
| 269 | struct audio_usecase *usecase) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 270 | { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 271 | snd_device_t snd_device; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 272 | char mixer_path[50]; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 273 | |
| 274 | if (usecase == NULL) |
| 275 | return -EINVAL; |
| 276 | |
| 277 | ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 278 | if (usecase->type == PCM_CAPTURE) |
| 279 | snd_device = usecase->in_snd_device; |
| 280 | else |
| 281 | snd_device = usecase->out_snd_device; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 282 | strcpy(mixer_path, use_case_table[usecase->id]); |
Ravi Kumar Alamanda | 299760a | 2013-11-01 17:29:09 -0500 | [diff] [blame] | 283 | platform_add_backend_name(adev->platform, mixer_path, snd_device); |
Ravi Kumar Alamanda | 6402646 | 2014-09-15 00:08:58 -0700 | [diff] [blame] | 284 | ALOGD("%s: reset and update mixer path: %s", __func__, mixer_path); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 285 | audio_route_reset_and_update_path(adev->audio_route, mixer_path); |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 286 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 287 | ALOGV("%s: exit", __func__); |
| 288 | return 0; |
| 289 | } |
| 290 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 291 | int enable_snd_device(struct audio_device *adev, |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 292 | snd_device_t snd_device) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 293 | { |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 294 | int i, num_devices = 0; |
| 295 | snd_device_t new_snd_devices[2]; |
| 296 | |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 297 | if (snd_device < SND_DEVICE_MIN || |
| 298 | snd_device >= SND_DEVICE_MAX) { |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 299 | ALOGE("%s: Invalid sound device %d", __func__, snd_device); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 300 | return -EINVAL; |
| 301 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 302 | |
| 303 | adev->snd_dev_ref_cnt[snd_device]++; |
| 304 | if (adev->snd_dev_ref_cnt[snd_device] > 1) { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 305 | ALOGV("%s: snd_device(%d: %s) is already active", |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 306 | __func__, snd_device, platform_get_snd_device_name(snd_device)); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 310 | /* due to the possibility of calibration overwrite between listen |
| 311 | and audio, notify sound trigger hal before audio calibration is sent */ |
| 312 | audio_extn_sound_trigger_update_device_status(snd_device, |
| 313 | ST_EVENT_SND_DEVICE_BUSY); |
| 314 | |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 315 | if (audio_extn_spkr_prot_is_enabled()) |
| 316 | audio_extn_spkr_prot_calib_cancel(adev); |
| 317 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 318 | if (platform_send_audio_calibration(adev->platform, snd_device) < 0) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 319 | adev->snd_dev_ref_cnt[snd_device]--; |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 320 | audio_extn_sound_trigger_update_device_status(snd_device, |
| 321 | ST_EVENT_SND_DEVICE_FREE); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 322 | return -EINVAL; |
| 323 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 324 | |
zhaoyang yin | 4211fad | 2015-06-04 21:13:25 +0800 | [diff] [blame] | 325 | audio_extn_dsm_feedback_enable(adev, snd_device, true); |
| 326 | |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 327 | if ((snd_device == SND_DEVICE_OUT_SPEAKER || |
| 328 | snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) && |
| 329 | audio_extn_spkr_prot_is_enabled()) { |
| 330 | if (audio_extn_spkr_prot_get_acdb_id(snd_device) < 0) { |
| 331 | adev->snd_dev_ref_cnt[snd_device]--; |
| 332 | return -EINVAL; |
| 333 | } |
| 334 | if (audio_extn_spkr_prot_start_processing(snd_device)) { |
| 335 | ALOGE("%s: spkr_start_processing failed", __func__); |
| 336 | return -EINVAL; |
| 337 | } |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 338 | } else if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices)) { |
| 339 | for (i = 0; i < num_devices; i++) { |
| 340 | enable_snd_device(adev, new_snd_devices[i]); |
| 341 | } |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 342 | } else { |
| 343 | const char * dev_path = platform_get_snd_device_name(snd_device); |
| 344 | ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path); |
| 345 | audio_route_apply_and_update_path(adev->audio_route, dev_path); |
| 346 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 347 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 348 | return 0; |
| 349 | } |
| 350 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 351 | int disable_snd_device(struct audio_device *adev, |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 352 | snd_device_t snd_device) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 353 | { |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 354 | int i, num_devices = 0; |
| 355 | snd_device_t new_snd_devices[2]; |
| 356 | |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 357 | if (snd_device < SND_DEVICE_MIN || |
| 358 | snd_device >= SND_DEVICE_MAX) { |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 359 | ALOGE("%s: Invalid sound device %d", __func__, snd_device); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 360 | return -EINVAL; |
| 361 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 362 | if (adev->snd_dev_ref_cnt[snd_device] <= 0) { |
| 363 | ALOGE("%s: device ref cnt is already 0", __func__); |
| 364 | return -EINVAL; |
| 365 | } |
| 366 | adev->snd_dev_ref_cnt[snd_device]--; |
| 367 | if (adev->snd_dev_ref_cnt[snd_device] == 0) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 368 | const char * dev_path = platform_get_snd_device_name(snd_device); |
Ravi Kumar Alamanda | 6402646 | 2014-09-15 00:08:58 -0700 | [diff] [blame] | 369 | ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path); |
zhaoyang yin | 4211fad | 2015-06-04 21:13:25 +0800 | [diff] [blame] | 370 | |
| 371 | audio_extn_dsm_feedback_enable(adev, snd_device, false); |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 372 | if ((snd_device == SND_DEVICE_OUT_SPEAKER || |
| 373 | snd_device == SND_DEVICE_OUT_VOICE_SPEAKER) && |
| 374 | audio_extn_spkr_prot_is_enabled()) { |
| 375 | audio_extn_spkr_prot_stop_processing(snd_device); |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 376 | } else if (platform_can_split_snd_device(snd_device, &num_devices, new_snd_devices)) { |
| 377 | for (i = 0; i < num_devices; i++) { |
| 378 | disable_snd_device(adev, new_snd_devices[i]); |
| 379 | } |
Ravi Kumar Alamanda | 6386300 | 2015-04-22 11:15:25 -0700 | [diff] [blame] | 380 | } else { |
| 381 | audio_route_reset_and_update_path(adev->audio_route, dev_path); |
| 382 | } |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 383 | audio_extn_sound_trigger_update_device_status(snd_device, |
| 384 | ST_EVENT_SND_DEVICE_FREE); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 385 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 386 | return 0; |
| 387 | } |
| 388 | |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 389 | static void check_and_route_playback_usecases(struct audio_device *adev, |
| 390 | struct audio_usecase *uc_info, |
| 391 | snd_device_t snd_device) |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 392 | { |
| 393 | struct listnode *node; |
| 394 | struct audio_usecase *usecase; |
| 395 | bool switch_device[AUDIO_USECASE_MAX]; |
| 396 | int i, num_uc_to_switch = 0; |
| 397 | |
| 398 | /* |
| 399 | * This function is to make sure that all the usecases that are active on |
| 400 | * the hardware codec backend are always routed to any one device that is |
| 401 | * handled by the hardware codec. |
| 402 | * For example, if low-latency and deep-buffer usecases are currently active |
| 403 | * on speaker and out_set_parameters(headset) is received on low-latency |
| 404 | * output, then we have to make sure deep-buffer is also switched to headset, |
| 405 | * because of the limitation that both the devices cannot be enabled |
| 406 | * at the same time as they share the same backend. |
| 407 | */ |
| 408 | /* Disable all the usecases on the shared backend other than the |
| 409 | specified usecase */ |
| 410 | for (i = 0; i < AUDIO_USECASE_MAX; i++) |
| 411 | switch_device[i] = false; |
| 412 | |
| 413 | list_for_each(node, &adev->usecase_list) { |
| 414 | usecase = node_to_item(node, struct audio_usecase, list); |
| 415 | if (usecase->type != PCM_CAPTURE && |
| 416 | usecase != uc_info && |
| 417 | usecase->out_snd_device != snd_device && |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 418 | usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND && |
| 419 | platform_check_backends_match(snd_device, usecase->out_snd_device)) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 420 | ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", |
| 421 | __func__, use_case_table[usecase->id], |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 422 | platform_get_snd_device_name(usecase->out_snd_device)); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 423 | disable_audio_route(adev, usecase); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 424 | switch_device[usecase->id] = true; |
| 425 | num_uc_to_switch++; |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | if (num_uc_to_switch) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 430 | list_for_each(node, &adev->usecase_list) { |
| 431 | usecase = node_to_item(node, struct audio_usecase, list); |
| 432 | if (switch_device[usecase->id]) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 433 | disable_snd_device(adev, usecase->out_snd_device); |
sangwon.jeon | 866d5ff | 2013-10-17 21:42:50 +0900 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
| 437 | list_for_each(node, &adev->usecase_list) { |
| 438 | usecase = node_to_item(node, struct audio_usecase, list); |
| 439 | if (switch_device[usecase->id]) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 440 | enable_snd_device(adev, snd_device); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 444 | /* Re-route all the usecases on the shared backend other than the |
| 445 | specified usecase to new snd devices */ |
| 446 | list_for_each(node, &adev->usecase_list) { |
| 447 | usecase = node_to_item(node, struct audio_usecase, list); |
| 448 | /* Update the out_snd_device only before enabling the audio route */ |
| 449 | if (switch_device[usecase->id] ) { |
| 450 | usecase->out_snd_device = snd_device; |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 451 | enable_audio_route(adev, usecase); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 452 | } |
| 453 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 457 | static void check_and_route_capture_usecases(struct audio_device *adev, |
| 458 | struct audio_usecase *uc_info, |
| 459 | snd_device_t snd_device) |
| 460 | { |
| 461 | struct listnode *node; |
| 462 | struct audio_usecase *usecase; |
| 463 | bool switch_device[AUDIO_USECASE_MAX]; |
| 464 | int i, num_uc_to_switch = 0; |
| 465 | |
| 466 | /* |
| 467 | * This function is to make sure that all the active capture usecases |
| 468 | * are always routed to the same input sound device. |
| 469 | * For example, if audio-record and voice-call usecases are currently |
| 470 | * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece) |
| 471 | * is received for voice call then we have to make sure that audio-record |
| 472 | * usecase is also switched to earpiece i.e. voice-dmic-ef, |
| 473 | * because of the limitation that two devices cannot be enabled |
| 474 | * at the same time if they share the same backend. |
| 475 | */ |
| 476 | for (i = 0; i < AUDIO_USECASE_MAX; i++) |
| 477 | switch_device[i] = false; |
| 478 | |
| 479 | list_for_each(node, &adev->usecase_list) { |
| 480 | usecase = node_to_item(node, struct audio_usecase, list); |
| 481 | if (usecase->type != PCM_PLAYBACK && |
| 482 | usecase != uc_info && |
Anish Kumar | ff86471 | 2015-06-03 13:35:11 -0700 | [diff] [blame] | 483 | usecase->in_snd_device != snd_device && |
| 484 | (usecase->id != USECASE_AUDIO_SPKR_CALIB_TX)) { |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 485 | ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", |
| 486 | __func__, use_case_table[usecase->id], |
Devin Kim | 1e5f353 | 2013-08-09 07:48:29 -0700 | [diff] [blame] | 487 | platform_get_snd_device_name(usecase->in_snd_device)); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 488 | disable_audio_route(adev, usecase); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 489 | switch_device[usecase->id] = true; |
| 490 | num_uc_to_switch++; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | if (num_uc_to_switch) { |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 495 | list_for_each(node, &adev->usecase_list) { |
| 496 | usecase = node_to_item(node, struct audio_usecase, list); |
| 497 | if (switch_device[usecase->id]) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 498 | disable_snd_device(adev, usecase->in_snd_device); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
| 502 | list_for_each(node, &adev->usecase_list) { |
| 503 | usecase = node_to_item(node, struct audio_usecase, list); |
| 504 | if (switch_device[usecase->id]) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 505 | enable_snd_device(adev, snd_device); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 506 | } |
| 507 | } |
| 508 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 509 | /* Re-route all the usecases on the shared backend other than the |
| 510 | specified usecase to new snd devices */ |
| 511 | list_for_each(node, &adev->usecase_list) { |
| 512 | usecase = node_to_item(node, struct audio_usecase, list); |
| 513 | /* Update the in_snd_device only before enabling the audio route */ |
| 514 | if (switch_device[usecase->id] ) { |
| 515 | usecase->in_snd_device = snd_device; |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 516 | enable_audio_route(adev, usecase); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 517 | } |
| 518 | } |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 522 | /* must be called with hw device mutex locked */ |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 523 | static int read_hdmi_channel_masks(struct stream_out *out) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 524 | { |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 525 | int ret = 0; |
Haynes Mathew George | 47cd4cb | 2013-07-19 11:58:50 -0700 | [diff] [blame] | 526 | int channels = platform_edid_get_max_channels(out->dev->platform); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 527 | |
| 528 | switch (channels) { |
| 529 | /* |
| 530 | * Do not handle stereo output in Multi-channel cases |
| 531 | * Stereo case is handled in normal playback path |
| 532 | */ |
| 533 | case 6: |
| 534 | ALOGV("%s: HDMI supports 5.1", __func__); |
| 535 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; |
| 536 | break; |
| 537 | case 8: |
| 538 | ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__); |
| 539 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; |
| 540 | out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1; |
| 541 | break; |
| 542 | default: |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 543 | ALOGE("HDMI does not support multi channel playback"); |
| 544 | ret = -ENOSYS; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 545 | break; |
| 546 | } |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 547 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Ravi Kumar Alamanda | a237ecc | 2014-07-24 17:27:05 -0700 | [diff] [blame] | 550 | static audio_usecase_t get_voice_usecase_id_from_list(struct audio_device *adev) |
| 551 | { |
| 552 | struct audio_usecase *usecase; |
| 553 | struct listnode *node; |
| 554 | |
| 555 | list_for_each(node, &adev->usecase_list) { |
| 556 | usecase = node_to_item(node, struct audio_usecase, list); |
| 557 | if (usecase->type == VOICE_CALL) { |
| 558 | ALOGV("%s: usecase id %d", __func__, usecase->id); |
| 559 | return usecase->id; |
| 560 | } |
| 561 | } |
| 562 | return USECASE_INVALID; |
| 563 | } |
| 564 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 565 | struct audio_usecase *get_usecase_from_list(struct audio_device *adev, |
| 566 | audio_usecase_t uc_id) |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 567 | { |
| 568 | struct audio_usecase *usecase; |
| 569 | struct listnode *node; |
| 570 | |
| 571 | list_for_each(node, &adev->usecase_list) { |
| 572 | usecase = node_to_item(node, struct audio_usecase, list); |
| 573 | if (usecase->id == uc_id) |
| 574 | return usecase; |
| 575 | } |
| 576 | return NULL; |
| 577 | } |
| 578 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 579 | int select_devices(struct audio_device *adev, |
| 580 | audio_usecase_t uc_id) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 581 | { |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 582 | snd_device_t out_snd_device = SND_DEVICE_NONE; |
| 583 | snd_device_t in_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 584 | struct audio_usecase *usecase = NULL; |
| 585 | struct audio_usecase *vc_usecase = NULL; |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 586 | struct audio_usecase *hfp_usecase = NULL; |
| 587 | audio_usecase_t hfp_ucid; |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 588 | struct listnode *node; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 589 | int status = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 590 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 591 | usecase = get_usecase_from_list(adev, uc_id); |
| 592 | if (usecase == NULL) { |
| 593 | ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id); |
| 594 | return -EINVAL; |
| 595 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 596 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 597 | if ((usecase->type == VOICE_CALL) || |
| 598 | (usecase->type == PCM_HFP_CALL)) { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 599 | out_snd_device = platform_get_output_snd_device(adev->platform, |
| 600 | usecase->stream.out->devices); |
| 601 | in_snd_device = platform_get_input_snd_device(adev->platform, usecase->stream.out->devices); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 602 | usecase->devices = usecase->stream.out->devices; |
| 603 | } else { |
| 604 | /* |
| 605 | * If the voice call is active, use the sound devices of voice call usecase |
| 606 | * so that it would not result any device switch. All the usecases will |
| 607 | * be switched to new device when select_devices() is called for voice call |
| 608 | * usecase. This is to avoid switching devices for voice call when |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 609 | * check_and_route_playback_usecases() is called below. |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 610 | */ |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 611 | if (voice_is_in_call(adev)) { |
Ravi Kumar Alamanda | a237ecc | 2014-07-24 17:27:05 -0700 | [diff] [blame] | 612 | vc_usecase = get_usecase_from_list(adev, |
| 613 | get_voice_usecase_id_from_list(adev)); |
| 614 | if ((vc_usecase != NULL) && |
| 615 | ((vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) || |
| 616 | (usecase->devices == AUDIO_DEVICE_IN_VOICE_CALL))) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 617 | in_snd_device = vc_usecase->in_snd_device; |
| 618 | out_snd_device = vc_usecase->out_snd_device; |
| 619 | } |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 620 | } else if (audio_extn_hfp_is_active(adev)) { |
| 621 | hfp_ucid = audio_extn_hfp_get_usecase(); |
| 622 | hfp_usecase = get_usecase_from_list(adev, hfp_ucid); |
| 623 | if (hfp_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) { |
| 624 | in_snd_device = hfp_usecase->in_snd_device; |
| 625 | out_snd_device = hfp_usecase->out_snd_device; |
| 626 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 627 | } |
| 628 | if (usecase->type == PCM_PLAYBACK) { |
| 629 | usecase->devices = usecase->stream.out->devices; |
| 630 | in_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 631 | if (out_snd_device == SND_DEVICE_NONE) { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 632 | out_snd_device = platform_get_output_snd_device(adev->platform, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 633 | usecase->stream.out->devices); |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 634 | if (usecase->stream.out == adev->primary_output && |
| 635 | adev->active_input && |
Ravi Kumar Alamanda | f282901 | 2014-11-12 16:16:10 -0800 | [diff] [blame] | 636 | adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
| 637 | out_snd_device != usecase->out_snd_device) { |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 638 | select_devices(adev, adev->active_input->usecase); |
| 639 | } |
| 640 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 641 | } else if (usecase->type == PCM_CAPTURE) { |
| 642 | usecase->devices = usecase->stream.in->device; |
| 643 | out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 644 | if (in_snd_device == SND_DEVICE_NONE) { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 645 | audio_devices_t out_device = AUDIO_DEVICE_NONE; |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 646 | if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
| 647 | adev->primary_output && !adev->primary_output->standby) { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 648 | out_device = adev->primary_output->devices; |
Ravi Kumar Alamanda | f282901 | 2014-11-12 16:16:10 -0800 | [diff] [blame] | 649 | platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE); |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 650 | } else if (usecase->id == USECASE_AUDIO_RECORD_AFE_PROXY) { |
| 651 | out_device = AUDIO_DEVICE_OUT_TELEPHONY_TX; |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 652 | } |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 653 | in_snd_device = platform_get_input_snd_device(adev->platform, out_device); |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 654 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
| 658 | if (out_snd_device == usecase->out_snd_device && |
| 659 | in_snd_device == usecase->in_snd_device) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 660 | return 0; |
| 661 | } |
| 662 | |
sangwoo | bc67724 | 2013-08-08 16:53:43 +0900 | [diff] [blame] | 663 | ALOGD("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 664 | out_snd_device, platform_get_snd_device_name(out_snd_device), |
| 665 | in_snd_device, platform_get_snd_device_name(in_snd_device)); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 666 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 667 | /* |
| 668 | * Limitation: While in call, to do a device switch we need to disable |
| 669 | * and enable both RX and TX devices though one of them is same as current |
| 670 | * device. |
| 671 | */ |
Ravi Kumar Alamanda | 36886fc | 2014-09-29 13:41:51 -0700 | [diff] [blame] | 672 | if ((usecase->type == VOICE_CALL) && |
| 673 | (usecase->in_snd_device != SND_DEVICE_NONE) && |
| 674 | (usecase->out_snd_device != SND_DEVICE_NONE)) { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 675 | status = platform_switch_voice_call_device_pre(adev->platform); |
Ravi Kumar Alamanda | 610e8cc | 2013-02-12 01:42:38 -0800 | [diff] [blame] | 676 | } |
| 677 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 678 | /* Disable current sound devices */ |
| 679 | if (usecase->out_snd_device != SND_DEVICE_NONE) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 680 | disable_audio_route(adev, usecase); |
| 681 | disable_snd_device(adev, usecase->out_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 682 | } |
| 683 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 684 | if (usecase->in_snd_device != SND_DEVICE_NONE) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 685 | disable_audio_route(adev, usecase); |
| 686 | disable_snd_device(adev, usecase->in_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 687 | } |
| 688 | |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 689 | /* Applicable only on the targets that has external modem. |
| 690 | * New device information should be sent to modem before enabling |
| 691 | * the devices to reduce in-call device switch time. |
| 692 | */ |
Ravi Kumar Alamanda | 36886fc | 2014-09-29 13:41:51 -0700 | [diff] [blame] | 693 | if ((usecase->type == VOICE_CALL) && |
| 694 | (usecase->in_snd_device != SND_DEVICE_NONE) && |
| 695 | (usecase->out_snd_device != SND_DEVICE_NONE)) { |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 696 | status = platform_switch_voice_call_enable_device_config(adev->platform, |
| 697 | out_snd_device, |
| 698 | in_snd_device); |
Ravi Kumar Alamanda | 36886fc | 2014-09-29 13:41:51 -0700 | [diff] [blame] | 699 | } |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 700 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 701 | /* Enable new sound devices */ |
| 702 | if (out_snd_device != SND_DEVICE_NONE) { |
| 703 | if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 704 | check_and_route_playback_usecases(adev, usecase, out_snd_device); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 705 | enable_snd_device(adev, out_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 706 | } |
| 707 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 708 | if (in_snd_device != SND_DEVICE_NONE) { |
| 709 | check_and_route_capture_usecases(adev, usecase, in_snd_device); |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 710 | enable_snd_device(adev, in_snd_device); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 711 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 712 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 713 | if (usecase->type == VOICE_CALL) |
| 714 | status = platform_switch_voice_call_device_post(adev->platform, |
| 715 | out_snd_device, |
| 716 | in_snd_device); |
Ravi Kumar Alamanda | 610e8cc | 2013-02-12 01:42:38 -0800 | [diff] [blame] | 717 | |
sangwoo | 170731f | 2013-06-08 15:36:36 +0900 | [diff] [blame] | 718 | usecase->in_snd_device = in_snd_device; |
| 719 | usecase->out_snd_device = out_snd_device; |
| 720 | |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 721 | enable_audio_route(adev, usecase); |
sangwoo | 170731f | 2013-06-08 15:36:36 +0900 | [diff] [blame] | 722 | |
Ravi Kumar Alamanda | 83281a9 | 2014-05-19 18:14:57 -0700 | [diff] [blame] | 723 | /* Applicable only on the targets that has external modem. |
| 724 | * Enable device command should be sent to modem only after |
| 725 | * enabling voice call mixer controls |
| 726 | */ |
| 727 | if (usecase->type == VOICE_CALL) |
| 728 | status = platform_switch_voice_call_usecase_route_post(adev->platform, |
| 729 | out_snd_device, |
| 730 | in_snd_device); |
| 731 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 732 | return status; |
| 733 | } |
| 734 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 735 | static int stop_input_stream(struct stream_in *in) |
| 736 | { |
| 737 | int i, ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 738 | struct audio_usecase *uc_info; |
| 739 | struct audio_device *adev = in->dev; |
| 740 | |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 741 | adev->active_input = NULL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 742 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 743 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 744 | in->usecase, use_case_table[in->usecase]); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 745 | uc_info = get_usecase_from_list(adev, in->usecase); |
| 746 | if (uc_info == NULL) { |
| 747 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 748 | __func__, in->usecase); |
| 749 | return -EINVAL; |
| 750 | } |
| 751 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 752 | /* 1. Disable stream specific mixer controls */ |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 753 | disable_audio_route(adev, uc_info); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 754 | |
| 755 | /* 2. Disable the tx device */ |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 756 | disable_snd_device(adev, uc_info->in_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 757 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 758 | list_remove(&uc_info->list); |
| 759 | free(uc_info); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 760 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 761 | ALOGV("%s: exit: status(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 762 | return ret; |
| 763 | } |
| 764 | |
| 765 | int start_input_stream(struct stream_in *in) |
| 766 | { |
| 767 | /* 1. Enable output device and stream routing controls */ |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 768 | int ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 769 | struct audio_usecase *uc_info; |
| 770 | struct audio_device *adev = in->dev; |
| 771 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 772 | ALOGV("%s: enter: usecase(%d)", __func__, in->usecase); |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 773 | in->pcm_device_id = platform_get_pcm_device_id(in->usecase, PCM_CAPTURE); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 774 | if (in->pcm_device_id < 0) { |
| 775 | ALOGE("%s: Could not find PCM device id for the usecase(%d)", |
| 776 | __func__, in->usecase); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 777 | ret = -EINVAL; |
| 778 | goto error_config; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 779 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 780 | |
| 781 | adev->active_input = in; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 782 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 783 | uc_info->id = in->usecase; |
| 784 | uc_info->type = PCM_CAPTURE; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 785 | uc_info->stream.in = in; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 786 | uc_info->devices = in->device; |
| 787 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 788 | uc_info->out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 789 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 790 | list_add_tail(&adev->usecase_list, &uc_info->list); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 791 | select_devices(adev, in->usecase); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 792 | |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 793 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 794 | __func__, adev->snd_card, in->pcm_device_id, in->config.channels); |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 795 | |
| 796 | unsigned int flags = PCM_IN; |
| 797 | unsigned int pcm_open_retry_count = 0; |
| 798 | |
| 799 | if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) { |
| 800 | flags |= PCM_MMAP | PCM_NOIRQ; |
| 801 | pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 802 | } |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 803 | |
| 804 | while (1) { |
| 805 | in->pcm = pcm_open(adev->snd_card, in->pcm_device_id, |
| 806 | flags, &in->config); |
| 807 | if (in->pcm == NULL || !pcm_is_ready(in->pcm)) { |
| 808 | ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); |
| 809 | if (in->pcm != NULL) { |
| 810 | pcm_close(in->pcm); |
| 811 | in->pcm = NULL; |
| 812 | } |
| 813 | if (pcm_open_retry_count-- == 0) { |
| 814 | ret = -EIO; |
| 815 | goto error_open; |
| 816 | } |
| 817 | usleep(PROXY_OPEN_WAIT_TIME * 1000); |
| 818 | continue; |
| 819 | } |
| 820 | break; |
| 821 | } |
| 822 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 823 | ALOGV("%s: exit", __func__); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 824 | return ret; |
| 825 | |
| 826 | error_open: |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 827 | stop_input_stream(in); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 828 | |
| 829 | error_config: |
| 830 | adev->active_input = NULL; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 831 | ALOGD("%s: exit: status(%d)", __func__, ret); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 832 | |
| 833 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 834 | } |
| 835 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 836 | /* must be called with out->lock locked */ |
| 837 | static int send_offload_cmd_l(struct stream_out* out, int command) |
| 838 | { |
| 839 | struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd)); |
| 840 | |
| 841 | ALOGVV("%s %d", __func__, command); |
| 842 | |
| 843 | cmd->cmd = command; |
| 844 | list_add_tail(&out->offload_cmd_list, &cmd->node); |
| 845 | pthread_cond_signal(&out->offload_cond); |
| 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | /* must be called iwth out->lock locked */ |
| 850 | static void stop_compressed_output_l(struct stream_out *out) |
| 851 | { |
| 852 | out->offload_state = OFFLOAD_STATE_IDLE; |
| 853 | out->playback_started = 0; |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 854 | out->send_new_metadata = 1; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 855 | if (out->compr != NULL) { |
| 856 | compress_stop(out->compr); |
| 857 | while (out->offload_thread_blocked) { |
| 858 | pthread_cond_wait(&out->cond, &out->lock); |
| 859 | } |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | static void *offload_thread_loop(void *context) |
| 864 | { |
| 865 | struct stream_out *out = (struct stream_out *) context; |
| 866 | struct listnode *item; |
| 867 | |
| 868 | out->offload_state = OFFLOAD_STATE_IDLE; |
| 869 | out->playback_started = 0; |
| 870 | |
| 871 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); |
| 872 | set_sched_policy(0, SP_FOREGROUND); |
| 873 | prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0); |
| 874 | |
| 875 | ALOGV("%s", __func__); |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 876 | pthread_mutex_lock(&out->lock); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 877 | for (;;) { |
| 878 | struct offload_cmd *cmd = NULL; |
| 879 | stream_callback_event_t event; |
| 880 | bool send_callback = false; |
| 881 | |
| 882 | ALOGVV("%s offload_cmd_list %d out->offload_state %d", |
| 883 | __func__, list_empty(&out->offload_cmd_list), |
| 884 | out->offload_state); |
| 885 | if (list_empty(&out->offload_cmd_list)) { |
| 886 | ALOGV("%s SLEEPING", __func__); |
| 887 | pthread_cond_wait(&out->offload_cond, &out->lock); |
| 888 | ALOGV("%s RUNNING", __func__); |
| 889 | continue; |
| 890 | } |
| 891 | |
| 892 | item = list_head(&out->offload_cmd_list); |
| 893 | cmd = node_to_item(item, struct offload_cmd, node); |
| 894 | list_remove(item); |
| 895 | |
| 896 | ALOGVV("%s STATE %d CMD %d out->compr %p", |
| 897 | __func__, out->offload_state, cmd->cmd, out->compr); |
| 898 | |
| 899 | if (cmd->cmd == OFFLOAD_CMD_EXIT) { |
| 900 | free(cmd); |
| 901 | break; |
| 902 | } |
| 903 | |
| 904 | if (out->compr == NULL) { |
| 905 | ALOGE("%s: Compress handle is NULL", __func__); |
| 906 | pthread_cond_signal(&out->cond); |
| 907 | continue; |
| 908 | } |
| 909 | out->offload_thread_blocked = true; |
| 910 | pthread_mutex_unlock(&out->lock); |
| 911 | send_callback = false; |
| 912 | switch(cmd->cmd) { |
| 913 | case OFFLOAD_CMD_WAIT_FOR_BUFFER: |
| 914 | compress_wait(out->compr, -1); |
| 915 | send_callback = true; |
| 916 | event = STREAM_CBK_EVENT_WRITE_READY; |
| 917 | break; |
| 918 | case OFFLOAD_CMD_PARTIAL_DRAIN: |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 919 | compress_next_track(out->compr); |
| 920 | compress_partial_drain(out->compr); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 921 | send_callback = true; |
| 922 | event = STREAM_CBK_EVENT_DRAIN_READY; |
Ravi Kumar Alamanda | cc4f6bf | 2014-12-02 19:21:51 -0800 | [diff] [blame] | 923 | /* Resend the metadata for next iteration */ |
| 924 | out->send_new_metadata = 1; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 925 | break; |
| 926 | case OFFLOAD_CMD_DRAIN: |
| 927 | compress_drain(out->compr); |
| 928 | send_callback = true; |
| 929 | event = STREAM_CBK_EVENT_DRAIN_READY; |
| 930 | break; |
| 931 | default: |
| 932 | ALOGE("%s unknown command received: %d", __func__, cmd->cmd); |
| 933 | break; |
| 934 | } |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 935 | pthread_mutex_lock(&out->lock); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 936 | out->offload_thread_blocked = false; |
| 937 | pthread_cond_signal(&out->cond); |
Eric Laurent | 6e89524 | 2013-09-05 16:10:57 -0700 | [diff] [blame] | 938 | if (send_callback) { |
Ravi Kumar Alamanda | cc4f6bf | 2014-12-02 19:21:51 -0800 | [diff] [blame] | 939 | ALOGVV("%s: sending offload_callback event %d", __func__, event); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 940 | out->offload_callback(event, NULL, out->offload_cookie); |
Eric Laurent | 6e89524 | 2013-09-05 16:10:57 -0700 | [diff] [blame] | 941 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 942 | free(cmd); |
| 943 | } |
| 944 | |
| 945 | pthread_cond_signal(&out->cond); |
| 946 | while (!list_empty(&out->offload_cmd_list)) { |
| 947 | item = list_head(&out->offload_cmd_list); |
| 948 | list_remove(item); |
| 949 | free(node_to_item(item, struct offload_cmd, node)); |
| 950 | } |
| 951 | pthread_mutex_unlock(&out->lock); |
| 952 | |
| 953 | return NULL; |
| 954 | } |
| 955 | |
| 956 | static int create_offload_callback_thread(struct stream_out *out) |
| 957 | { |
| 958 | pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL); |
| 959 | list_init(&out->offload_cmd_list); |
| 960 | pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL, |
| 961 | offload_thread_loop, out); |
| 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | static int destroy_offload_callback_thread(struct stream_out *out) |
| 966 | { |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 967 | pthread_mutex_lock(&out->lock); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 968 | stop_compressed_output_l(out); |
| 969 | send_offload_cmd_l(out, OFFLOAD_CMD_EXIT); |
| 970 | |
| 971 | pthread_mutex_unlock(&out->lock); |
| 972 | pthread_join(out->offload_thread, (void **) NULL); |
| 973 | pthread_cond_destroy(&out->offload_cond); |
| 974 | |
| 975 | return 0; |
| 976 | } |
| 977 | |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 978 | static bool allow_hdmi_channel_config(struct audio_device *adev) |
| 979 | { |
| 980 | struct listnode *node; |
| 981 | struct audio_usecase *usecase; |
| 982 | bool ret = true; |
| 983 | |
| 984 | list_for_each(node, &adev->usecase_list) { |
| 985 | usecase = node_to_item(node, struct audio_usecase, list); |
| 986 | if (usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { |
| 987 | /* |
| 988 | * If voice call is already existing, do not proceed further to avoid |
| 989 | * disabling/enabling both RX and TX devices, CSD calls, etc. |
| 990 | * Once the voice call done, the HDMI channels can be configured to |
| 991 | * max channels of remaining use cases. |
| 992 | */ |
| 993 | if (usecase->id == USECASE_VOICE_CALL) { |
| 994 | ALOGD("%s: voice call is active, no change in HDMI channels", |
| 995 | __func__); |
| 996 | ret = false; |
| 997 | break; |
| 998 | } else if (usecase->id == USECASE_AUDIO_PLAYBACK_MULTI_CH) { |
| 999 | ALOGD("%s: multi channel playback is active, " |
| 1000 | "no change in HDMI channels", __func__); |
| 1001 | ret = false; |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | return ret; |
| 1007 | } |
| 1008 | |
| 1009 | static int check_and_set_hdmi_channels(struct audio_device *adev, |
| 1010 | unsigned int channels) |
| 1011 | { |
| 1012 | struct listnode *node; |
| 1013 | struct audio_usecase *usecase; |
| 1014 | |
| 1015 | /* Check if change in HDMI channel config is allowed */ |
| 1016 | if (!allow_hdmi_channel_config(adev)) |
| 1017 | return 0; |
| 1018 | |
| 1019 | if (channels == adev->cur_hdmi_channels) { |
| 1020 | ALOGD("%s: Requested channels are same as current", __func__); |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | platform_set_hdmi_channels(adev->platform, channels); |
| 1025 | adev->cur_hdmi_channels = channels; |
| 1026 | |
| 1027 | /* |
| 1028 | * Deroute all the playback streams routed to HDMI so that |
| 1029 | * the back end is deactivated. Note that backend will not |
| 1030 | * be deactivated if any one stream is connected to it. |
| 1031 | */ |
| 1032 | list_for_each(node, &adev->usecase_list) { |
| 1033 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1034 | if (usecase->type == PCM_PLAYBACK && |
| 1035 | usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1036 | disable_audio_route(adev, usecase); |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | /* |
| 1041 | * Enable all the streams disabled above. Now the HDMI backend |
| 1042 | * will be activated with new channel configuration |
| 1043 | */ |
| 1044 | list_for_each(node, &adev->usecase_list) { |
| 1045 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1046 | if (usecase->type == PCM_PLAYBACK && |
| 1047 | usecase->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1048 | enable_audio_route(adev, usecase); |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 1049 | } |
| 1050 | } |
| 1051 | |
| 1052 | return 0; |
| 1053 | } |
| 1054 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1055 | static int stop_output_stream(struct stream_out *out) |
| 1056 | { |
| 1057 | int i, ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1058 | struct audio_usecase *uc_info; |
| 1059 | struct audio_device *adev = out->dev; |
| 1060 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1061 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1062 | out->usecase, use_case_table[out->usecase]); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1063 | uc_info = get_usecase_from_list(adev, out->usecase); |
| 1064 | if (uc_info == NULL) { |
| 1065 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 1066 | __func__, out->usecase); |
| 1067 | return -EINVAL; |
| 1068 | } |
| 1069 | |
Haynes Mathew George | 41f8665 | 2014-06-17 14:22:15 -0700 | [diff] [blame] | 1070 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 1071 | if (adev->visualizer_stop_output != NULL) |
| 1072 | adev->visualizer_stop_output(out->handle, out->pcm_device_id); |
| 1073 | if (adev->offload_effects_stop_output != NULL) |
| 1074 | adev->offload_effects_stop_output(out->handle, out->pcm_device_id); |
| 1075 | } |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 1076 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1077 | /* 1. Get and set stream specific mixer controls */ |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1078 | disable_audio_route(adev, uc_info); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1079 | |
| 1080 | /* 2. Disable the rx device */ |
Ravi Kumar Alamanda | c38e452 | 2014-04-14 11:46:35 -0700 | [diff] [blame] | 1081 | disable_snd_device(adev, uc_info->out_snd_device); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1082 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 1083 | list_remove(&uc_info->list); |
| 1084 | free(uc_info); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1085 | |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 1086 | audio_extn_extspk_update(adev->extspk); |
| 1087 | |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 1088 | /* Must be called after removing the usecase from list */ |
| 1089 | if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) |
| 1090 | check_and_set_hdmi_channels(adev, DEFAULT_HDMI_OUT_CHANNELS); |
| 1091 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1092 | ALOGV("%s: exit: status(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1093 | return ret; |
| 1094 | } |
| 1095 | |
| 1096 | int start_output_stream(struct stream_out *out) |
| 1097 | { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1098 | int ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1099 | struct audio_usecase *uc_info; |
| 1100 | struct audio_device *adev = out->dev; |
| 1101 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1102 | ALOGV("%s: enter: usecase(%d: %s) devices(%#x)", |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1103 | __func__, out->usecase, use_case_table[out->usecase], out->devices); |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1104 | out->pcm_device_id = platform_get_pcm_device_id(out->usecase, PCM_PLAYBACK); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1105 | if (out->pcm_device_id < 0) { |
| 1106 | ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", |
| 1107 | __func__, out->pcm_device_id, out->usecase); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 1108 | ret = -EINVAL; |
| 1109 | goto error_config; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 1113 | uc_info->id = out->usecase; |
| 1114 | uc_info->type = PCM_PLAYBACK; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1115 | uc_info->stream.out = out; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1116 | uc_info->devices = out->devices; |
| 1117 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 1118 | uc_info->out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1119 | |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 1120 | /* This must be called before adding this usecase to the list */ |
| 1121 | if (out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) |
| 1122 | check_and_set_hdmi_channels(adev, out->config.channels); |
| 1123 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 1124 | list_add_tail(&adev->usecase_list, &uc_info->list); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1125 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1126 | select_devices(adev, out->usecase); |
| 1127 | |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 1128 | audio_extn_extspk_update(adev->extspk); |
| 1129 | |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 1130 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d) format(%#x)", |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 1131 | __func__, adev->snd_card, out->pcm_device_id, out->config.format); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1132 | if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 1133 | unsigned int flags = PCM_OUT; |
| 1134 | unsigned int pcm_open_retry_count = 0; |
| 1135 | if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) { |
| 1136 | flags |= PCM_MMAP | PCM_NOIRQ; |
| 1137 | pcm_open_retry_count = PROXY_OPEN_RETRY_COUNT; |
| 1138 | } else |
| 1139 | flags |= PCM_MONOTONIC; |
| 1140 | |
| 1141 | while (1) { |
| 1142 | out->pcm = pcm_open(adev->snd_card, out->pcm_device_id, |
| 1143 | flags, &out->config); |
| 1144 | if (out->pcm == NULL || !pcm_is_ready(out->pcm)) { |
| 1145 | ALOGE("%s: %s", __func__, pcm_get_error(out->pcm)); |
| 1146 | if (out->pcm != NULL) { |
| 1147 | pcm_close(out->pcm); |
| 1148 | out->pcm = NULL; |
| 1149 | } |
| 1150 | if (pcm_open_retry_count-- == 0) { |
| 1151 | ret = -EIO; |
| 1152 | goto error_open; |
| 1153 | } |
| 1154 | usleep(PROXY_OPEN_WAIT_TIME * 1000); |
| 1155 | continue; |
| 1156 | } |
| 1157 | break; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1158 | } |
| 1159 | } else { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1160 | out->pcm = NULL; |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 1161 | out->compr = compress_open(adev->snd_card, out->pcm_device_id, |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1162 | COMPRESS_IN, &out->compr_config); |
| 1163 | if (out->compr && !is_compress_ready(out->compr)) { |
| 1164 | ALOGE("%s: %s", __func__, compress_get_error(out->compr)); |
| 1165 | compress_close(out->compr); |
| 1166 | out->compr = NULL; |
| 1167 | ret = -EIO; |
| 1168 | goto error_open; |
| 1169 | } |
| 1170 | if (out->offload_callback) |
| 1171 | compress_nonblock(out->compr, out->non_blocking); |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 1172 | |
| 1173 | if (adev->visualizer_start_output != NULL) |
Haynes Mathew George | 41f8665 | 2014-06-17 14:22:15 -0700 | [diff] [blame] | 1174 | adev->visualizer_start_output(out->handle, out->pcm_device_id); |
| 1175 | if (adev->offload_effects_start_output != NULL) |
| 1176 | adev->offload_effects_start_output(out->handle, out->pcm_device_id); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1177 | } |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1178 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1179 | return 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1180 | error_open: |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1181 | stop_output_stream(out); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 1182 | error_config: |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 1183 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1184 | } |
| 1185 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1186 | static int check_input_parameters(uint32_t sample_rate, |
| 1187 | audio_format_t format, |
| 1188 | int channel_count) |
| 1189 | { |
| 1190 | if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL; |
| 1191 | |
| 1192 | if ((channel_count < 1) || (channel_count > 2)) return -EINVAL; |
| 1193 | |
| 1194 | switch (sample_rate) { |
| 1195 | case 8000: |
| 1196 | case 11025: |
| 1197 | case 12000: |
| 1198 | case 16000: |
| 1199 | case 22050: |
| 1200 | case 24000: |
| 1201 | case 32000: |
| 1202 | case 44100: |
| 1203 | case 48000: |
| 1204 | break; |
| 1205 | default: |
| 1206 | return -EINVAL; |
| 1207 | } |
| 1208 | |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
| 1212 | static size_t get_input_buffer_size(uint32_t sample_rate, |
| 1213 | audio_format_t format, |
Glenn Kasten | 68e79ce | 2014-07-15 10:56:59 -0700 | [diff] [blame] | 1214 | int channel_count, |
| 1215 | bool is_low_latency) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1216 | { |
| 1217 | size_t size = 0; |
| 1218 | |
Ravi Kumar Alamanda | 33d3306 | 2013-06-11 14:40:01 -0700 | [diff] [blame] | 1219 | if (check_input_parameters(sample_rate, format, channel_count) != 0) |
| 1220 | return 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1221 | |
Ravi Kumar Alamanda | 33d3306 | 2013-06-11 14:40:01 -0700 | [diff] [blame] | 1222 | size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000; |
Glenn Kasten | 68e79ce | 2014-07-15 10:56:59 -0700 | [diff] [blame] | 1223 | if (is_low_latency) |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 1224 | size = configured_low_latency_capture_period_size; |
Ravi Kumar Alamanda | 33d3306 | 2013-06-11 14:40:01 -0700 | [diff] [blame] | 1225 | /* ToDo: should use frame_size computed based on the format and |
| 1226 | channel_count here. */ |
| 1227 | size *= sizeof(short) * channel_count; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1228 | |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 1229 | /* make sure the size is multiple of 32 bytes |
| 1230 | * At 48 kHz mono 16-bit PCM: |
| 1231 | * 5.000 ms = 240 frames = 15*16*1*2 = 480, a whole multiple of 32 (15) |
| 1232 | * 3.333 ms = 160 frames = 10*16*1*2 = 320, a whole multiple of 32 (10) |
| 1233 | */ |
| 1234 | size += 0x1f; |
| 1235 | size &= ~0x1f; |
Ravi Kumar Alamanda | 33d3306 | 2013-06-11 14:40:01 -0700 | [diff] [blame] | 1236 | |
| 1237 | return size; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 1241 | { |
| 1242 | struct stream_out *out = (struct stream_out *)stream; |
| 1243 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1244 | return out->sample_rate; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1245 | } |
| 1246 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 1247 | static int out_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1248 | { |
| 1249 | return -ENOSYS; |
| 1250 | } |
| 1251 | |
| 1252 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 1253 | { |
| 1254 | struct stream_out *out = (struct stream_out *)stream; |
| 1255 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1256 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 1257 | return out->compr_config.fragment_size; |
| 1258 | } |
Eric Laurent | fdf296a | 2014-07-03 16:41:51 -0700 | [diff] [blame] | 1259 | return out->config.period_size * |
| 1260 | audio_stream_out_frame_size((const struct audio_stream_out *)stream); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | static uint32_t out_get_channels(const struct audio_stream *stream) |
| 1264 | { |
| 1265 | struct stream_out *out = (struct stream_out *)stream; |
| 1266 | |
| 1267 | return out->channel_mask; |
| 1268 | } |
| 1269 | |
| 1270 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 1271 | { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1272 | struct stream_out *out = (struct stream_out *)stream; |
| 1273 | |
| 1274 | return out->format; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1275 | } |
| 1276 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 1277 | static int out_set_format(struct audio_stream *stream __unused, audio_format_t format __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1278 | { |
| 1279 | return -ENOSYS; |
| 1280 | } |
| 1281 | |
| 1282 | static int out_standby(struct audio_stream *stream) |
| 1283 | { |
| 1284 | struct stream_out *out = (struct stream_out *)stream; |
| 1285 | struct audio_device *adev = out->dev; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1286 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1287 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1288 | out->usecase, use_case_table[out->usecase]); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1289 | |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1290 | pthread_mutex_lock(&out->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1291 | if (!out->standby) { |
Ravi Kumar Alamanda | 8bba9e9 | 2013-11-11 21:09:07 -0800 | [diff] [blame] | 1292 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1293 | out->standby = true; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1294 | if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 1295 | if (out->pcm) { |
| 1296 | pcm_close(out->pcm); |
| 1297 | out->pcm = NULL; |
| 1298 | } |
| 1299 | } else { |
| 1300 | stop_compressed_output_l(out); |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 1301 | out->gapless_mdata.encoder_delay = 0; |
| 1302 | out->gapless_mdata.encoder_padding = 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1303 | if (out->compr != NULL) { |
| 1304 | compress_close(out->compr); |
| 1305 | out->compr = NULL; |
| 1306 | } |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1307 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1308 | stop_output_stream(out); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1309 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1310 | } |
| 1311 | pthread_mutex_unlock(&out->lock); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1312 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1313 | return 0; |
| 1314 | } |
| 1315 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 1316 | static int out_dump(const struct audio_stream *stream __unused, int fd __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1317 | { |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 1321 | static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms) |
| 1322 | { |
| 1323 | int ret = 0; |
| 1324 | char value[32]; |
| 1325 | struct compr_gapless_mdata tmp_mdata; |
| 1326 | |
| 1327 | if (!out || !parms) { |
| 1328 | return -EINVAL; |
| 1329 | } |
| 1330 | |
| 1331 | ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value)); |
| 1332 | if (ret >= 0) { |
| 1333 | tmp_mdata.encoder_delay = atoi(value); //whats a good limit check? |
| 1334 | } else { |
| 1335 | return -EINVAL; |
| 1336 | } |
| 1337 | |
| 1338 | ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value)); |
| 1339 | if (ret >= 0) { |
| 1340 | tmp_mdata.encoder_padding = atoi(value); |
| 1341 | } else { |
| 1342 | return -EINVAL; |
| 1343 | } |
| 1344 | |
| 1345 | out->gapless_mdata = tmp_mdata; |
| 1346 | out->send_new_metadata = 1; |
| 1347 | ALOGV("%s new encoder delay %u and padding %u", __func__, |
| 1348 | out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding); |
| 1349 | |
| 1350 | return 0; |
| 1351 | } |
| 1352 | |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 1353 | static bool output_drives_call(struct audio_device *adev, struct stream_out *out) |
| 1354 | { |
| 1355 | return out == adev->primary_output || out == adev->voice_tx_output; |
| 1356 | } |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 1357 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1358 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 1359 | { |
| 1360 | struct stream_out *out = (struct stream_out *)stream; |
| 1361 | struct audio_device *adev = out->dev; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1362 | struct audio_usecase *usecase; |
| 1363 | struct listnode *node; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1364 | struct str_parms *parms; |
| 1365 | char value[32]; |
| 1366 | int ret, val = 0; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1367 | bool select_new_device = false; |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 1368 | int status = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1369 | |
sangwoo | bc67724 | 2013-08-08 16:53:43 +0900 | [diff] [blame] | 1370 | ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s", |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1371 | __func__, out->usecase, use_case_table[out->usecase], kvpairs); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1372 | parms = str_parms_create_str(kvpairs); |
| 1373 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
| 1374 | if (ret >= 0) { |
| 1375 | val = atoi(value); |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1376 | pthread_mutex_lock(&out->lock); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1377 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1378 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1379 | /* |
| 1380 | * When HDMI cable is unplugged the music playback is paused and |
| 1381 | * the policy manager sends routing=0. But the audioflinger |
| 1382 | * continues to write data until standby time (3sec). |
| 1383 | * As the HDMI core is turned off, the write gets blocked. |
| 1384 | * Avoid this by routing audio to speaker until standby. |
| 1385 | */ |
| 1386 | if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL && |
| 1387 | val == AUDIO_DEVICE_NONE) { |
| 1388 | val = AUDIO_DEVICE_OUT_SPEAKER; |
| 1389 | } |
| 1390 | |
| 1391 | /* |
| 1392 | * select_devices() call below switches all the usecases on the same |
Ravi Kumar Alamanda | b7ea4f5 | 2015-06-08 16:44:05 -0700 | [diff] [blame] | 1393 | * backend to the new device. Refer to check_and_route_playback_usecases() in |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1394 | * the select_devices(). But how do we undo this? |
| 1395 | * |
| 1396 | * For example, music playback is active on headset (deep-buffer usecase) |
| 1397 | * and if we go to ringtones and select a ringtone, low-latency usecase |
| 1398 | * will be started on headset+speaker. As we can't enable headset+speaker |
| 1399 | * and headset devices at the same time, select_devices() switches the music |
| 1400 | * playback to headset+speaker while starting low-lateny usecase for ringtone. |
| 1401 | * So when the ringtone playback is completed, how do we undo the same? |
| 1402 | * |
| 1403 | * We are relying on the out_set_parameters() call on deep-buffer output, |
| 1404 | * once the ringtone playback is ended. |
| 1405 | * NOTE: We should not check if the current devices are same as new devices. |
| 1406 | * Because select_devices() must be called to switch back the music |
| 1407 | * playback to headset. |
| 1408 | */ |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1409 | if (val != 0) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1410 | out->devices = val; |
| 1411 | |
| 1412 | if (!out->standby) |
| 1413 | select_devices(adev, out->usecase); |
| 1414 | |
Eric Laurent | a765719 | 2014-10-09 21:09:33 -0700 | [diff] [blame] | 1415 | if (output_drives_call(adev, out)) { |
| 1416 | if (!voice_is_in_call(adev)) { |
| 1417 | if (adev->mode == AUDIO_MODE_IN_CALL) { |
| 1418 | adev->current_call_output = out; |
| 1419 | ret = voice_start_call(adev); |
| 1420 | } |
| 1421 | } else { |
| 1422 | adev->current_call_output = out; |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 1423 | voice_update_devices_for_all_voice_usecases(adev); |
Eric Laurent | a765719 | 2014-10-09 21:09:33 -0700 | [diff] [blame] | 1424 | } |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1425 | } |
| 1426 | } |
| 1427 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1428 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1429 | pthread_mutex_unlock(&out->lock); |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 1430 | |
| 1431 | /*handles device and call state changes*/ |
| 1432 | audio_extn_extspk_update(adev->extspk); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1433 | } |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 1434 | |
| 1435 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 1436 | parse_compress_metadata(out, parms); |
| 1437 | } |
| 1438 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1439 | str_parms_destroy(parms); |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 1440 | ALOGV("%s: exit: code(%d)", __func__, status); |
| 1441 | return status; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | static char* out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 1445 | { |
| 1446 | struct stream_out *out = (struct stream_out *)stream; |
| 1447 | struct str_parms *query = str_parms_create_str(keys); |
| 1448 | char *str; |
| 1449 | char value[256]; |
| 1450 | struct str_parms *reply = str_parms_create(); |
| 1451 | size_t i, j; |
| 1452 | int ret; |
| 1453 | bool first = true; |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1454 | ALOGV("%s: enter: keys - %s", __func__, keys); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1455 | ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value)); |
| 1456 | if (ret >= 0) { |
| 1457 | value[0] = '\0'; |
| 1458 | i = 0; |
| 1459 | while (out->supported_channel_masks[i] != 0) { |
| 1460 | for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) { |
| 1461 | if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) { |
| 1462 | if (!first) { |
| 1463 | strcat(value, "|"); |
| 1464 | } |
| 1465 | strcat(value, out_channels_name_to_enum_table[j].name); |
| 1466 | first = false; |
| 1467 | break; |
| 1468 | } |
| 1469 | } |
| 1470 | i++; |
| 1471 | } |
| 1472 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); |
| 1473 | str = str_parms_to_str(reply); |
| 1474 | } else { |
| 1475 | str = strdup(keys); |
| 1476 | } |
| 1477 | str_parms_destroy(query); |
| 1478 | str_parms_destroy(reply); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1479 | ALOGV("%s: exit: returns - %s", __func__, str); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1480 | return str; |
| 1481 | } |
| 1482 | |
| 1483 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 1484 | { |
| 1485 | struct stream_out *out = (struct stream_out *)stream; |
| 1486 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1487 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) |
| 1488 | return COMPRESS_OFFLOAD_PLAYBACK_LATENCY; |
| 1489 | |
| 1490 | return (out->config.period_count * out->config.period_size * 1000) / |
| 1491 | (out->config.rate); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | static int out_set_volume(struct audio_stream_out *stream, float left, |
| 1495 | float right) |
| 1496 | { |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 1497 | struct stream_out *out = (struct stream_out *)stream; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1498 | int volume[2]; |
| 1499 | |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 1500 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) { |
| 1501 | /* only take left channel into account: the API is for stereo anyway */ |
| 1502 | out->muted = (left == 0.0f); |
| 1503 | return 0; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1504 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 1505 | const char *mixer_ctl_name = "Compress Playback Volume"; |
| 1506 | struct audio_device *adev = out->dev; |
| 1507 | struct mixer_ctl *ctl; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1508 | ctl = mixer_get_ctl_by_name(adev->mixer, mixer_ctl_name); |
| 1509 | if (!ctl) { |
Haynes Mathew George | 20bcfa8 | 2014-06-25 13:37:03 -0700 | [diff] [blame] | 1510 | /* try with the control based on device id */ |
| 1511 | int pcm_device_id = platform_get_pcm_device_id(out->usecase, |
| 1512 | PCM_PLAYBACK); |
| 1513 | char ctl_name[128] = {0}; |
| 1514 | snprintf(ctl_name, sizeof(ctl_name), |
| 1515 | "Compress Playback %d Volume", pcm_device_id); |
| 1516 | ctl = mixer_get_ctl_by_name(adev->mixer, ctl_name); |
| 1517 | if (!ctl) { |
| 1518 | ALOGE("%s: Could not get volume ctl mixer cmd", __func__); |
| 1519 | return -EINVAL; |
| 1520 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1521 | } |
| 1522 | volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX); |
| 1523 | volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX); |
| 1524 | mixer_ctl_set_array(ctl, volume, sizeof(volume)/sizeof(volume[0])); |
| 1525 | return 0; |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 1526 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1527 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1528 | return -ENOSYS; |
| 1529 | } |
| 1530 | |
Uday Kishore Pasupuleti | cec8ad8 | 2015-04-15 10:34:06 -0700 | [diff] [blame] | 1531 | #ifdef NO_AUDIO_OUT |
| 1532 | static ssize_t out_write_for_no_output(struct audio_stream_out *stream, |
| 1533 | const void *buffer, size_t bytes) |
| 1534 | { |
| 1535 | struct stream_out *out = (struct stream_out *)stream; |
| 1536 | |
| 1537 | /* No Output device supported other than BT for playback. |
| 1538 | * Sleep for the amount of buffer duration |
| 1539 | */ |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1540 | pthread_mutex_lock(&out->lock); |
Uday Kishore Pasupuleti | cec8ad8 | 2015-04-15 10:34:06 -0700 | [diff] [blame] | 1541 | usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) / |
| 1542 | out_get_sample_rate(&out->stream.common)); |
| 1543 | pthread_mutex_unlock(&out->lock); |
| 1544 | return bytes; |
| 1545 | } |
| 1546 | #endif |
| 1547 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1548 | static ssize_t out_write(struct audio_stream_out *stream, const void *buffer, |
| 1549 | size_t bytes) |
| 1550 | { |
| 1551 | struct stream_out *out = (struct stream_out *)stream; |
| 1552 | struct audio_device *adev = out->dev; |
Eric Laurent | 6e89524 | 2013-09-05 16:10:57 -0700 | [diff] [blame] | 1553 | ssize_t ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1554 | |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1555 | pthread_mutex_lock(&out->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1556 | if (out->standby) { |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1557 | out->standby = false; |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1558 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1559 | ret = start_output_stream(out); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1560 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1561 | /* ToDo: If use case is compress offload should return 0 */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1562 | if (ret != 0) { |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1563 | out->standby = true; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1564 | goto exit; |
| 1565 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1566 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1567 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1568 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 1569 | ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes); |
| 1570 | if (out->send_new_metadata) { |
| 1571 | ALOGVV("send new gapless metadata"); |
| 1572 | compress_set_gapless_metadata(out->compr, &out->gapless_mdata); |
| 1573 | out->send_new_metadata = 0; |
| 1574 | } |
| 1575 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1576 | ret = compress_write(out->compr, buffer, bytes); |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 1577 | ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret); |
Eric Laurent | 6e89524 | 2013-09-05 16:10:57 -0700 | [diff] [blame] | 1578 | if (ret >= 0 && ret < (ssize_t)bytes) { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1579 | send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER); |
| 1580 | } |
| 1581 | if (!out->playback_started) { |
| 1582 | compress_start(out->compr); |
| 1583 | out->playback_started = 1; |
| 1584 | out->offload_state = OFFLOAD_STATE_PLAYING; |
| 1585 | } |
| 1586 | pthread_mutex_unlock(&out->lock); |
| 1587 | return ret; |
| 1588 | } else { |
| 1589 | if (out->pcm) { |
| 1590 | if (out->muted) |
| 1591 | memset((void *)buffer, 0, bytes); |
| 1592 | ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes); |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 1593 | if (out->usecase == USECASE_AUDIO_PLAYBACK_AFE_PROXY) { |
| 1594 | ret = pcm_mmap_write(out->pcm, (void *)buffer, bytes); |
| 1595 | } |
| 1596 | else |
| 1597 | ret = pcm_write(out->pcm, (void *)buffer, bytes); |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 1598 | if (ret == 0) |
| 1599 | out->written += bytes / (out->config.channels * sizeof(short)); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1600 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1601 | } |
| 1602 | |
| 1603 | exit: |
| 1604 | pthread_mutex_unlock(&out->lock); |
| 1605 | |
| 1606 | if (ret != 0) { |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1607 | if (out->pcm) |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 1608 | ALOGE("%s: error %zu - %s", __func__, ret, pcm_get_error(out->pcm)); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1609 | out_standby(&out->stream.common); |
Eric Laurent | fdf296a | 2014-07-03 16:41:51 -0700 | [diff] [blame] | 1610 | usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) / |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1611 | out_get_sample_rate(&out->stream.common)); |
| 1612 | } |
| 1613 | return bytes; |
| 1614 | } |
| 1615 | |
| 1616 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 1617 | uint32_t *dsp_frames) |
| 1618 | { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1619 | struct stream_out *out = (struct stream_out *)stream; |
| 1620 | *dsp_frames = 0; |
| 1621 | if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) { |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1622 | pthread_mutex_lock(&out->lock); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1623 | if (out->compr != NULL) { |
| 1624 | compress_get_tstamp(out->compr, (unsigned long *)dsp_frames, |
| 1625 | &out->sample_rate); |
| 1626 | ALOGVV("%s rendered frames %d sample_rate %d", |
| 1627 | __func__, *dsp_frames, out->sample_rate); |
| 1628 | } |
| 1629 | pthread_mutex_unlock(&out->lock); |
| 1630 | return 0; |
| 1631 | } else |
| 1632 | return -EINVAL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1633 | } |
| 1634 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 1635 | static int out_add_audio_effect(const struct audio_stream *stream __unused, |
| 1636 | effect_handle_t effect __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1637 | { |
| 1638 | return 0; |
| 1639 | } |
| 1640 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 1641 | static int out_remove_audio_effect(const struct audio_stream *stream __unused, |
| 1642 | effect_handle_t effect __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1643 | { |
| 1644 | return 0; |
| 1645 | } |
| 1646 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 1647 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream __unused, |
| 1648 | int64_t *timestamp __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1649 | { |
| 1650 | return -EINVAL; |
| 1651 | } |
| 1652 | |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 1653 | static int out_get_presentation_position(const struct audio_stream_out *stream, |
| 1654 | uint64_t *frames, struct timespec *timestamp) |
| 1655 | { |
| 1656 | struct stream_out *out = (struct stream_out *)stream; |
| 1657 | int ret = -1; |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 1658 | unsigned long dsp_frames; |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 1659 | |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1660 | pthread_mutex_lock(&out->lock); |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 1661 | |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 1662 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 1663 | if (out->compr != NULL) { |
| 1664 | compress_get_tstamp(out->compr, &dsp_frames, |
| 1665 | &out->sample_rate); |
| 1666 | ALOGVV("%s rendered frames %ld sample_rate %d", |
| 1667 | __func__, dsp_frames, out->sample_rate); |
| 1668 | *frames = dsp_frames; |
| 1669 | ret = 0; |
| 1670 | /* this is the best we can do */ |
| 1671 | clock_gettime(CLOCK_MONOTONIC, timestamp); |
| 1672 | } |
| 1673 | } else { |
| 1674 | if (out->pcm) { |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 1675 | unsigned int avail; |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 1676 | if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) { |
| 1677 | size_t kernel_buffer_size = out->config.period_size * out->config.period_count; |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 1678 | int64_t signed_frames = out->written - kernel_buffer_size + avail; |
Haynes Mathew George | 7ff216f | 2013-09-11 19:51:41 -0700 | [diff] [blame] | 1679 | // This adjustment accounts for buffering after app processor. |
| 1680 | // It is based on estimated DSP latency per use case, rather than exact. |
| 1681 | signed_frames -= |
| 1682 | (platform_render_latency(out->usecase) * out->sample_rate / 1000000LL); |
| 1683 | |
Eric Laurent | 949a089 | 2013-09-20 09:20:13 -0700 | [diff] [blame] | 1684 | // It would be unusual for this value to be negative, but check just in case ... |
| 1685 | if (signed_frames >= 0) { |
| 1686 | *frames = signed_frames; |
| 1687 | ret = 0; |
| 1688 | } |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 1689 | } |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | pthread_mutex_unlock(&out->lock); |
| 1694 | |
| 1695 | return ret; |
| 1696 | } |
| 1697 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1698 | static int out_set_callback(struct audio_stream_out *stream, |
| 1699 | stream_callback_t callback, void *cookie) |
| 1700 | { |
| 1701 | struct stream_out *out = (struct stream_out *)stream; |
| 1702 | |
| 1703 | ALOGV("%s", __func__); |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1704 | pthread_mutex_lock(&out->lock); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1705 | out->offload_callback = callback; |
| 1706 | out->offload_cookie = cookie; |
| 1707 | pthread_mutex_unlock(&out->lock); |
| 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | static int out_pause(struct audio_stream_out* stream) |
| 1712 | { |
| 1713 | struct stream_out *out = (struct stream_out *)stream; |
| 1714 | int status = -ENOSYS; |
| 1715 | ALOGV("%s", __func__); |
| 1716 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1717 | pthread_mutex_lock(&out->lock); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1718 | if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) { |
| 1719 | status = compress_pause(out->compr); |
| 1720 | out->offload_state = OFFLOAD_STATE_PAUSED; |
| 1721 | } |
| 1722 | pthread_mutex_unlock(&out->lock); |
| 1723 | } |
| 1724 | return status; |
| 1725 | } |
| 1726 | |
| 1727 | static int out_resume(struct audio_stream_out* stream) |
| 1728 | { |
| 1729 | struct stream_out *out = (struct stream_out *)stream; |
| 1730 | int status = -ENOSYS; |
| 1731 | ALOGV("%s", __func__); |
| 1732 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 1733 | status = 0; |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1734 | pthread_mutex_lock(&out->lock); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1735 | if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) { |
| 1736 | status = compress_resume(out->compr); |
| 1737 | out->offload_state = OFFLOAD_STATE_PLAYING; |
| 1738 | } |
| 1739 | pthread_mutex_unlock(&out->lock); |
| 1740 | } |
| 1741 | return status; |
| 1742 | } |
| 1743 | |
| 1744 | static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type ) |
| 1745 | { |
| 1746 | struct stream_out *out = (struct stream_out *)stream; |
| 1747 | int status = -ENOSYS; |
| 1748 | ALOGV("%s", __func__); |
| 1749 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1750 | pthread_mutex_lock(&out->lock); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1751 | if (type == AUDIO_DRAIN_EARLY_NOTIFY) |
| 1752 | status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN); |
| 1753 | else |
| 1754 | status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN); |
| 1755 | pthread_mutex_unlock(&out->lock); |
| 1756 | } |
| 1757 | return status; |
| 1758 | } |
| 1759 | |
| 1760 | static int out_flush(struct audio_stream_out* stream) |
| 1761 | { |
| 1762 | struct stream_out *out = (struct stream_out *)stream; |
| 1763 | ALOGV("%s", __func__); |
| 1764 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1765 | pthread_mutex_lock(&out->lock); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 1766 | stop_compressed_output_l(out); |
| 1767 | pthread_mutex_unlock(&out->lock); |
| 1768 | return 0; |
| 1769 | } |
| 1770 | return -ENOSYS; |
| 1771 | } |
| 1772 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1773 | /** audio_stream_in implementation **/ |
| 1774 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 1775 | { |
| 1776 | struct stream_in *in = (struct stream_in *)stream; |
| 1777 | |
| 1778 | return in->config.rate; |
| 1779 | } |
| 1780 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 1781 | static int in_set_sample_rate(struct audio_stream *stream __unused, uint32_t rate __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1782 | { |
| 1783 | return -ENOSYS; |
| 1784 | } |
| 1785 | |
| 1786 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 1787 | { |
| 1788 | struct stream_in *in = (struct stream_in *)stream; |
| 1789 | |
Eric Laurent | fdf296a | 2014-07-03 16:41:51 -0700 | [diff] [blame] | 1790 | return in->config.period_size * |
| 1791 | audio_stream_in_frame_size((const struct audio_stream_in *)stream); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1792 | } |
| 1793 | |
| 1794 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 1795 | { |
| 1796 | struct stream_in *in = (struct stream_in *)stream; |
| 1797 | |
| 1798 | return in->channel_mask; |
| 1799 | } |
| 1800 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 1801 | static audio_format_t in_get_format(const struct audio_stream *stream __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1802 | { |
| 1803 | return AUDIO_FORMAT_PCM_16_BIT; |
| 1804 | } |
| 1805 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 1806 | static int in_set_format(struct audio_stream *stream __unused, audio_format_t format __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1807 | { |
| 1808 | return -ENOSYS; |
| 1809 | } |
| 1810 | |
| 1811 | static int in_standby(struct audio_stream *stream) |
| 1812 | { |
| 1813 | struct stream_in *in = (struct stream_in *)stream; |
| 1814 | struct audio_device *adev = in->dev; |
| 1815 | int status = 0; |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1816 | ALOGV("%s: enter", __func__); |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1817 | pthread_mutex_lock(&in->lock); |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 1818 | |
| 1819 | if (!in->standby && in->is_st_session) { |
| 1820 | ALOGD("%s: sound trigger pcm stop lab", __func__); |
| 1821 | audio_extn_sound_trigger_stop_lab(in); |
| 1822 | in->standby = true; |
| 1823 | } |
| 1824 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1825 | if (!in->standby) { |
Ravi Kumar Alamanda | 8bba9e9 | 2013-11-11 21:09:07 -0800 | [diff] [blame] | 1826 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1827 | in->standby = true; |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1828 | if (in->pcm) { |
| 1829 | pcm_close(in->pcm); |
| 1830 | in->pcm = NULL; |
| 1831 | } |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 1832 | adev->enable_voicerx = false; |
| 1833 | platform_set_echo_reference(adev, false, AUDIO_DEVICE_NONE ); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1834 | status = stop_input_stream(in); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1835 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1836 | } |
| 1837 | pthread_mutex_unlock(&in->lock); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1838 | ALOGV("%s: exit: status(%d)", __func__, status); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1839 | return status; |
| 1840 | } |
| 1841 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 1842 | static int in_dump(const struct audio_stream *stream __unused, int fd __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1843 | { |
| 1844 | return 0; |
| 1845 | } |
| 1846 | |
| 1847 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 1848 | { |
| 1849 | struct stream_in *in = (struct stream_in *)stream; |
| 1850 | struct audio_device *adev = in->dev; |
| 1851 | struct str_parms *parms; |
| 1852 | char *str; |
| 1853 | char value[32]; |
| 1854 | int ret, val = 0; |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 1855 | int status = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1856 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1857 | ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1858 | parms = str_parms_create_str(kvpairs); |
| 1859 | |
| 1860 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); |
| 1861 | |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1862 | pthread_mutex_lock(&in->lock); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1863 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1864 | if (ret >= 0) { |
| 1865 | val = atoi(value); |
| 1866 | /* no audio source uses val == 0 */ |
| 1867 | if ((in->source != val) && (val != 0)) { |
| 1868 | in->source = val; |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 1873 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1874 | if (ret >= 0) { |
| 1875 | val = atoi(value); |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 1876 | if (((int)in->device != val) && (val != 0)) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1877 | in->device = val; |
| 1878 | /* If recording is in progress, change the tx device to new device */ |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1879 | if (!in->standby) |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 1880 | status = select_devices(adev, in->usecase); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1881 | } |
| 1882 | } |
| 1883 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1884 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1885 | pthread_mutex_unlock(&in->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1886 | |
| 1887 | str_parms_destroy(parms); |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 1888 | ALOGV("%s: exit: status(%d)", __func__, status); |
| 1889 | return status; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1890 | } |
| 1891 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 1892 | static char* in_get_parameters(const struct audio_stream *stream __unused, |
| 1893 | const char *keys __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1894 | { |
| 1895 | return strdup(""); |
| 1896 | } |
| 1897 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 1898 | static int in_set_gain(struct audio_stream_in *stream __unused, float gain __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1899 | { |
| 1900 | return 0; |
| 1901 | } |
| 1902 | |
| 1903 | static ssize_t in_read(struct audio_stream_in *stream, void *buffer, |
| 1904 | size_t bytes) |
| 1905 | { |
| 1906 | struct stream_in *in = (struct stream_in *)stream; |
| 1907 | struct audio_device *adev = in->dev; |
| 1908 | int i, ret = -1; |
| 1909 | |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1910 | pthread_mutex_lock(&in->lock); |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 1911 | if (in->is_st_session) { |
| 1912 | ALOGVV(" %s: reading on st session bytes=%d", __func__, bytes); |
| 1913 | /* Read from sound trigger HAL */ |
| 1914 | audio_extn_sound_trigger_read(in, buffer, bytes); |
| 1915 | pthread_mutex_unlock(&in->lock); |
| 1916 | return bytes; |
| 1917 | } |
| 1918 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1919 | if (in->standby) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1920 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1921 | ret = start_input_stream(in); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1922 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1923 | if (ret != 0) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1924 | goto exit; |
| 1925 | } |
| 1926 | in->standby = 0; |
| 1927 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1928 | |
| 1929 | if (in->pcm) { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 1930 | if (in->usecase == USECASE_AUDIO_RECORD_AFE_PROXY) { |
| 1931 | ret = pcm_mmap_read(in->pcm, buffer, bytes); |
| 1932 | } else |
| 1933 | ret = pcm_read(in->pcm, buffer, bytes); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1934 | } |
| 1935 | |
| 1936 | /* |
| 1937 | * Instead of writing zeroes here, we could trust the hardware |
| 1938 | * to always provide zeroes when muted. |
Eric Laurent | 7b2b5ab | 2014-09-14 12:29:59 -0700 | [diff] [blame] | 1939 | * No need to acquire adev->lock to read mic_muted here as we don't change its state. |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1940 | */ |
Eric Laurent | 7b2b5ab | 2014-09-14 12:29:59 -0700 | [diff] [blame] | 1941 | if (ret == 0 && adev->mic_muted && in->usecase != USECASE_AUDIO_RECORD_AFE_PROXY) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1942 | memset(buffer, 0, bytes); |
| 1943 | |
| 1944 | exit: |
| 1945 | pthread_mutex_unlock(&in->lock); |
| 1946 | |
| 1947 | if (ret != 0) { |
| 1948 | in_standby(&in->stream.common); |
| 1949 | ALOGV("%s: read failed - sleeping for buffer duration", __func__); |
Eric Laurent | fdf296a | 2014-07-03 16:41:51 -0700 | [diff] [blame] | 1950 | usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) / |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1951 | in_get_sample_rate(&in->stream.common)); |
| 1952 | } |
| 1953 | return bytes; |
| 1954 | } |
| 1955 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 1956 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1957 | { |
| 1958 | return 0; |
| 1959 | } |
| 1960 | |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 1961 | static int add_remove_audio_effect(const struct audio_stream *stream, |
| 1962 | effect_handle_t effect, |
| 1963 | bool enable) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1964 | { |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 1965 | struct stream_in *in = (struct stream_in *)stream; |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 1966 | struct audio_device *adev = in->dev; |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 1967 | int status = 0; |
| 1968 | effect_descriptor_t desc; |
| 1969 | |
| 1970 | status = (*effect)->get_descriptor(effect, &desc); |
| 1971 | if (status != 0) |
| 1972 | return status; |
| 1973 | |
Eric Laurent | 6010f70 | 2015-06-25 23:39:33 +0000 | [diff] [blame] | 1974 | pthread_mutex_lock(&in->lock); |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 1975 | pthread_mutex_lock(&in->dev->lock); |
| 1976 | if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) && |
| 1977 | in->enable_aec != enable && |
| 1978 | (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) { |
| 1979 | in->enable_aec = enable; |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 1980 | if (!enable) |
| 1981 | platform_set_echo_reference(in->dev, enable, AUDIO_DEVICE_NONE); |
| 1982 | adev->enable_voicerx = enable; |
| 1983 | struct audio_usecase *usecase; |
| 1984 | struct listnode *node; |
| 1985 | list_for_each(node, &adev->usecase_list) { |
| 1986 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1987 | if (usecase->type == PCM_PLAYBACK) { |
| 1988 | select_devices(adev, usecase->id); |
| 1989 | break; |
| 1990 | } |
| 1991 | } |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 1992 | if (!in->standby) |
| 1993 | select_devices(in->dev, in->usecase); |
| 1994 | } |
Ravi Kumar Alamanda | 3ad4e1b | 2014-06-03 00:08:15 -0700 | [diff] [blame] | 1995 | if (in->enable_ns != enable && |
| 1996 | (memcmp(&desc.type, FX_IID_NS, sizeof(effect_uuid_t)) == 0)) { |
| 1997 | in->enable_ns = enable; |
| 1998 | if (!in->standby) |
| 1999 | select_devices(in->dev, in->usecase); |
| 2000 | } |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 2001 | pthread_mutex_unlock(&in->dev->lock); |
| 2002 | pthread_mutex_unlock(&in->lock); |
| 2003 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2004 | return 0; |
| 2005 | } |
| 2006 | |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 2007 | static int in_add_audio_effect(const struct audio_stream *stream, |
| 2008 | effect_handle_t effect) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2009 | { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2010 | ALOGV("%s: effect %p", __func__, effect); |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 2011 | return add_remove_audio_effect(stream, effect, true); |
| 2012 | } |
| 2013 | |
| 2014 | static int in_remove_audio_effect(const struct audio_stream *stream, |
| 2015 | effect_handle_t effect) |
| 2016 | { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2017 | ALOGV("%s: effect %p", __func__, effect); |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 2018 | return add_remove_audio_effect(stream, effect, false); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2019 | } |
| 2020 | |
| 2021 | static int adev_open_output_stream(struct audio_hw_device *dev, |
| 2022 | audio_io_handle_t handle, |
| 2023 | audio_devices_t devices, |
| 2024 | audio_output_flags_t flags, |
| 2025 | struct audio_config *config, |
Eric Laurent | 91975a6 | 2014-07-27 17:15:50 -0700 | [diff] [blame] | 2026 | struct audio_stream_out **stream_out, |
| 2027 | const char *address __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2028 | { |
| 2029 | struct audio_device *adev = (struct audio_device *)dev; |
| 2030 | struct stream_out *out; |
| 2031 | int i, ret; |
| 2032 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2033 | ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)", |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2034 | __func__, config->sample_rate, config->channel_mask, devices, flags); |
| 2035 | *stream_out = NULL; |
| 2036 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
| 2037 | |
| 2038 | if (devices == AUDIO_DEVICE_NONE) |
| 2039 | devices = AUDIO_DEVICE_OUT_SPEAKER; |
| 2040 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2041 | out->flags = flags; |
| 2042 | out->devices = devices; |
Haynes Mathew George | 47cd4cb | 2013-07-19 11:58:50 -0700 | [diff] [blame] | 2043 | out->dev = adev; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2044 | out->format = config->format; |
| 2045 | out->sample_rate = config->sample_rate; |
| 2046 | out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 2047 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO; |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 2048 | out->handle = handle; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2049 | |
| 2050 | /* Init use case and pcm_config */ |
| 2051 | if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT && |
Eric Laurent | 7f24504 | 2013-09-30 19:22:50 -0700 | [diff] [blame] | 2052 | !(out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) && |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2053 | out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 2054 | pthread_mutex_lock(&adev->lock); |
| 2055 | ret = read_hdmi_channel_masks(out); |
| 2056 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 2057 | if (ret != 0) |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 2058 | goto error_open; |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 2059 | |
| 2060 | if (config->sample_rate == 0) |
| 2061 | config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 2062 | if (config->channel_mask == 0) |
| 2063 | config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1; |
| 2064 | |
| 2065 | out->channel_mask = config->channel_mask; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2066 | out->sample_rate = config->sample_rate; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2067 | out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH; |
| 2068 | out->config = pcm_config_hdmi_multi; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2069 | out->config.rate = config->sample_rate; |
Eric Laurent | 0de8d1f | 2014-07-01 20:34:45 -0700 | [diff] [blame] | 2070 | out->config.channels = audio_channel_count_from_out_mask(out->channel_mask); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2071 | out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2072 | } else if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 2073 | if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version || |
| 2074 | config->offload_info.size != AUDIO_INFO_INITIALIZER.size) { |
| 2075 | ALOGE("%s: Unsupported Offload information", __func__); |
| 2076 | ret = -EINVAL; |
| 2077 | goto error_open; |
| 2078 | } |
| 2079 | if (!is_supported_format(config->offload_info.format)) { |
| 2080 | ALOGE("%s: Unsupported audio format", __func__); |
| 2081 | ret = -EINVAL; |
| 2082 | goto error_open; |
| 2083 | } |
| 2084 | |
| 2085 | out->compr_config.codec = (struct snd_codec *) |
| 2086 | calloc(1, sizeof(struct snd_codec)); |
| 2087 | |
| 2088 | out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD; |
| 2089 | if (config->offload_info.channel_mask) |
| 2090 | out->channel_mask = config->offload_info.channel_mask; |
| 2091 | else if (config->channel_mask) |
| 2092 | out->channel_mask = config->channel_mask; |
| 2093 | out->format = config->offload_info.format; |
| 2094 | out->sample_rate = config->offload_info.sample_rate; |
| 2095 | |
| 2096 | out->stream.set_callback = out_set_callback; |
| 2097 | out->stream.pause = out_pause; |
| 2098 | out->stream.resume = out_resume; |
| 2099 | out->stream.drain = out_drain; |
| 2100 | out->stream.flush = out_flush; |
| 2101 | |
| 2102 | out->compr_config.codec->id = |
| 2103 | get_snd_codec_id(config->offload_info.format); |
| 2104 | out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE; |
| 2105 | out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS; |
Eric Laurent | 1ccf397 | 2014-10-23 14:42:59 -0700 | [diff] [blame] | 2106 | out->compr_config.codec->sample_rate = config->offload_info.sample_rate; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2107 | out->compr_config.codec->bit_rate = |
| 2108 | config->offload_info.bit_rate; |
| 2109 | out->compr_config.codec->ch_in = |
Eric Laurent | 0de8d1f | 2014-07-01 20:34:45 -0700 | [diff] [blame] | 2110 | audio_channel_count_from_out_mask(config->channel_mask); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2111 | out->compr_config.codec->ch_out = out->compr_config.codec->ch_in; |
| 2112 | |
| 2113 | if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) |
| 2114 | out->non_blocking = 1; |
Haynes Mathew George | 352f27b | 2013-07-26 00:00:15 -0700 | [diff] [blame] | 2115 | |
| 2116 | out->send_new_metadata = 1; |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2117 | create_offload_callback_thread(out); |
| 2118 | ALOGV("%s: offloaded output offload_info version %04x bit rate %d", |
| 2119 | __func__, config->offload_info.version, |
| 2120 | config->offload_info.bit_rate); |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2121 | } else if (out->devices == AUDIO_DEVICE_OUT_TELEPHONY_TX) { |
| 2122 | if (config->sample_rate == 0) |
| 2123 | config->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 2124 | if (config->sample_rate != 48000 && config->sample_rate != 16000 && |
| 2125 | config->sample_rate != 8000) { |
| 2126 | config->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 2127 | ret = -EINVAL; |
| 2128 | goto error_open; |
| 2129 | } |
| 2130 | out->sample_rate = config->sample_rate; |
| 2131 | out->config.rate = config->sample_rate; |
| 2132 | if (config->format == AUDIO_FORMAT_DEFAULT) |
| 2133 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 2134 | if (config->format != AUDIO_FORMAT_PCM_16_BIT) { |
| 2135 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 2136 | ret = -EINVAL; |
| 2137 | goto error_open; |
| 2138 | } |
| 2139 | out->format = config->format; |
| 2140 | out->usecase = USECASE_AUDIO_PLAYBACK_AFE_PROXY; |
| 2141 | out->config = pcm_config_afe_proxy_playback; |
| 2142 | adev->voice_tx_output = out; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2143 | } else { |
Andy Hung | 6fcba9c | 2014-03-18 11:53:32 -0700 | [diff] [blame] | 2144 | if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { |
| 2145 | out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER; |
| 2146 | out->config = pcm_config_deep_buffer; |
Ravi Kumar Alamanda | f78a4d9 | 2015-04-24 15:18:23 -0700 | [diff] [blame] | 2147 | } else if (out->flags & AUDIO_OUTPUT_FLAG_TTS) { |
| 2148 | out->usecase = USECASE_AUDIO_PLAYBACK_TTS; |
| 2149 | out->config = pcm_config_deep_buffer; |
Andy Hung | 6fcba9c | 2014-03-18 11:53:32 -0700 | [diff] [blame] | 2150 | } else { |
| 2151 | out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY; |
| 2152 | out->config = pcm_config_low_latency; |
| 2153 | } |
| 2154 | if (config->format != audio_format_from_pcm_format(out->config.format)) { |
| 2155 | if (k_enable_extended_precision |
| 2156 | && pcm_params_format_test(adev->use_case_table[out->usecase], |
| 2157 | pcm_format_from_audio_format(config->format))) { |
| 2158 | out->config.format = pcm_format_from_audio_format(config->format); |
| 2159 | /* out->format already set to config->format */ |
| 2160 | } else { |
| 2161 | /* deny the externally proposed config format |
| 2162 | * and use the one specified in audio_hw layer configuration. |
| 2163 | * Note: out->format is returned by out->stream.common.get_format() |
| 2164 | * and is used to set config->format in the code several lines below. |
| 2165 | */ |
| 2166 | out->format = audio_format_from_pcm_format(out->config.format); |
| 2167 | } |
| 2168 | } |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2169 | out->sample_rate = out->config.rate; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2170 | } |
Andy Hung | 6fcba9c | 2014-03-18 11:53:32 -0700 | [diff] [blame] | 2171 | ALOGV("%s: Usecase(%s) config->format %#x out->config.format %#x\n", |
| 2172 | __func__, use_case_table[out->usecase], config->format, out->config.format); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2173 | |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 2174 | if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2175 | if (adev->primary_output == NULL) |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 2176 | adev->primary_output = out; |
| 2177 | else { |
| 2178 | ALOGE("%s: Primary output is already opened", __func__); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 2179 | ret = -EEXIST; |
| 2180 | goto error_open; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 2181 | } |
| 2182 | } |
| 2183 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2184 | /* Check if this usecase is already existing */ |
| 2185 | pthread_mutex_lock(&adev->lock); |
| 2186 | if (get_usecase_from_list(adev, out->usecase) != NULL) { |
| 2187 | ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2188 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 2189 | ret = -EEXIST; |
| 2190 | goto error_open; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2191 | } |
| 2192 | pthread_mutex_unlock(&adev->lock); |
| 2193 | |
| 2194 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 2195 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 2196 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 2197 | out->stream.common.get_channels = out_get_channels; |
| 2198 | out->stream.common.get_format = out_get_format; |
| 2199 | out->stream.common.set_format = out_set_format; |
| 2200 | out->stream.common.standby = out_standby; |
| 2201 | out->stream.common.dump = out_dump; |
| 2202 | out->stream.common.set_parameters = out_set_parameters; |
| 2203 | out->stream.common.get_parameters = out_get_parameters; |
| 2204 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 2205 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 2206 | out->stream.get_latency = out_get_latency; |
| 2207 | out->stream.set_volume = out_set_volume; |
Uday Kishore Pasupuleti | cec8ad8 | 2015-04-15 10:34:06 -0700 | [diff] [blame] | 2208 | #ifdef NO_AUDIO_OUT |
| 2209 | out->stream.write = out_write_for_no_output; |
| 2210 | #else |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2211 | out->stream.write = out_write; |
Uday Kishore Pasupuleti | cec8ad8 | 2015-04-15 10:34:06 -0700 | [diff] [blame] | 2212 | #endif |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2213 | out->stream.get_render_position = out_get_render_position; |
| 2214 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 2215 | out->stream.get_presentation_position = out_get_presentation_position; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2216 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2217 | out->standby = 1; |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 2218 | /* out->muted = false; by calloc() */ |
Glenn Kasten | 2ccd7ba | 2013-09-10 09:04:31 -0700 | [diff] [blame] | 2219 | /* out->written = 0; by calloc() */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2220 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2221 | pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL); |
| 2222 | pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL); |
| 2223 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2224 | config->format = out->stream.common.get_format(&out->stream.common); |
| 2225 | config->channel_mask = out->stream.common.get_channels(&out->stream.common); |
| 2226 | config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common); |
| 2227 | |
| 2228 | *stream_out = &out->stream; |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2229 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2230 | return 0; |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 2231 | |
| 2232 | error_open: |
| 2233 | free(out); |
| 2234 | *stream_out = NULL; |
| 2235 | ALOGD("%s: exit: ret %d", __func__, ret); |
| 2236 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2237 | } |
| 2238 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2239 | static void adev_close_output_stream(struct audio_hw_device *dev __unused, |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2240 | struct audio_stream_out *stream) |
| 2241 | { |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2242 | struct stream_out *out = (struct stream_out *)stream; |
| 2243 | struct audio_device *adev = out->dev; |
| 2244 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2245 | ALOGV("%s: enter", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2246 | out_standby(&stream->common); |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2247 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2248 | destroy_offload_callback_thread(out); |
| 2249 | |
| 2250 | if (out->compr_config.codec != NULL) |
| 2251 | free(out->compr_config.codec); |
| 2252 | } |
Ravi Kumar Alamanda | a4fc902 | 2014-10-08 18:57:46 -0700 | [diff] [blame] | 2253 | |
| 2254 | if (adev->voice_tx_output == out) |
| 2255 | adev->voice_tx_output = NULL; |
| 2256 | |
Ravi Kumar Alamanda | 4e02e55 | 2013-07-17 15:22:04 -0700 | [diff] [blame] | 2257 | pthread_cond_destroy(&out->cond); |
| 2258 | pthread_mutex_destroy(&out->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2259 | free(stream); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2260 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 2264 | { |
| 2265 | struct audio_device *adev = (struct audio_device *)dev; |
| 2266 | struct str_parms *parms; |
| 2267 | char *str; |
| 2268 | char value[32]; |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 2269 | int val; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2270 | int ret; |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 2271 | int status = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2272 | |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2273 | ALOGD("%s: enter: %s", __func__, kvpairs); |
| 2274 | |
| 2275 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2276 | |
| 2277 | parms = str_parms_create_str(kvpairs); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2278 | status = voice_set_parameters(adev, parms); |
| 2279 | if (status != 0) { |
| 2280 | goto done; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); |
| 2284 | if (ret >= 0) { |
Ravi Kumar Alamanda | e258e68 | 2015-06-25 13:32:42 -0700 | [diff] [blame] | 2285 | /* When set to false, HAL should disable EC and NS */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2286 | if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| 2287 | adev->bluetooth_nrec = true; |
| 2288 | else |
| 2289 | adev->bluetooth_nrec = false; |
| 2290 | } |
| 2291 | |
| 2292 | ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); |
| 2293 | if (ret >= 0) { |
| 2294 | if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| 2295 | adev->screen_off = false; |
| 2296 | else |
| 2297 | adev->screen_off = true; |
| 2298 | } |
| 2299 | |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 2300 | ret = str_parms_get_int(parms, "rotation", &val); |
| 2301 | if (ret >= 0) { |
| 2302 | bool reverse_speakers = false; |
| 2303 | switch(val) { |
| 2304 | // FIXME: note that the code below assumes that the speakers are in the correct placement |
| 2305 | // relative to the user when the device is rotated 90deg from its default rotation. This |
| 2306 | // assumption is device-specific, not platform-specific like this code. |
| 2307 | case 270: |
| 2308 | reverse_speakers = true; |
| 2309 | break; |
| 2310 | case 0: |
| 2311 | case 90: |
| 2312 | case 180: |
| 2313 | break; |
| 2314 | default: |
| 2315 | ALOGE("%s: unexpected rotation of %d", __func__, val); |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 2316 | status = -EINVAL; |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 2317 | } |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 2318 | if (status == 0) { |
Ravi Kumar Alamanda | 1f60cf8 | 2015-04-23 19:45:17 -0700 | [diff] [blame] | 2319 | platform_swap_lr_channels(adev, reverse_speakers); |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 2320 | } |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 2321 | } |
| 2322 | |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 2323 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_SCO_WB, value, sizeof(value)); |
| 2324 | if (ret >= 0) { |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 2325 | adev->bt_wb_speech_enabled = !strcmp(value, AUDIO_PARAMETER_VALUE_ON); |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 2326 | } |
| 2327 | |
Ravi Kumar Alamanda | 8e6e98f | 2013-11-05 15:57:39 -0800 | [diff] [blame] | 2328 | audio_extn_hfp_set_parameters(adev, parms); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2329 | done: |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2330 | str_parms_destroy(parms); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2331 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 03f0943 | 2014-03-25 18:09:11 -0700 | [diff] [blame] | 2332 | ALOGV("%s: exit with code(%d)", __func__, status); |
| 2333 | return status; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | static char* adev_get_parameters(const struct audio_hw_device *dev, |
| 2337 | const char *keys) |
| 2338 | { |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2339 | struct audio_device *adev = (struct audio_device *)dev; |
| 2340 | struct str_parms *reply = str_parms_create(); |
| 2341 | struct str_parms *query = str_parms_create_str(keys); |
| 2342 | char *str; |
| 2343 | |
| 2344 | pthread_mutex_lock(&adev->lock); |
| 2345 | |
| 2346 | voice_get_parameters(adev, query, reply); |
| 2347 | str = str_parms_to_str(reply); |
| 2348 | str_parms_destroy(query); |
| 2349 | str_parms_destroy(reply); |
| 2350 | |
| 2351 | pthread_mutex_unlock(&adev->lock); |
| 2352 | ALOGV("%s: exit: returns - %s", __func__, str); |
| 2353 | return str; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2354 | } |
| 2355 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2356 | static int adev_init_check(const struct audio_hw_device *dev __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2357 | { |
| 2358 | return 0; |
| 2359 | } |
| 2360 | |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 2361 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 2362 | { |
| 2363 | int ret; |
| 2364 | struct audio_device *adev = (struct audio_device *)dev; |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2365 | |
Eric Laurent | 4cc4ce1 | 2014-09-10 13:21:01 -0500 | [diff] [blame] | 2366 | audio_extn_extspk_set_voice_vol(adev->extspk, volume); |
| 2367 | |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 2368 | pthread_mutex_lock(&adev->lock); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2369 | ret = voice_set_volume(adev, volume); |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 2370 | pthread_mutex_unlock(&adev->lock); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2371 | |
Haynes Mathew George | 5191a85 | 2013-09-11 14:19:36 -0700 | [diff] [blame] | 2372 | return ret; |
| 2373 | } |
| 2374 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2375 | static int adev_set_master_volume(struct audio_hw_device *dev __unused, float volume __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2376 | { |
| 2377 | return -ENOSYS; |
| 2378 | } |
| 2379 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2380 | static int adev_get_master_volume(struct audio_hw_device *dev __unused, |
| 2381 | float *volume __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2382 | { |
| 2383 | return -ENOSYS; |
| 2384 | } |
| 2385 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2386 | static int adev_set_master_mute(struct audio_hw_device *dev __unused, bool muted __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2387 | { |
| 2388 | return -ENOSYS; |
| 2389 | } |
| 2390 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2391 | static int adev_get_master_mute(struct audio_hw_device *dev __unused, bool *muted __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2392 | { |
| 2393 | return -ENOSYS; |
| 2394 | } |
| 2395 | |
| 2396 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 2397 | { |
| 2398 | struct audio_device *adev = (struct audio_device *)dev; |
| 2399 | |
| 2400 | pthread_mutex_lock(&adev->lock); |
| 2401 | if (adev->mode != mode) { |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2402 | ALOGD("%s: mode %d\n", __func__, mode); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2403 | adev->mode = mode; |
Ravi Kumar Alamanda | 36886fc | 2014-09-29 13:41:51 -0700 | [diff] [blame] | 2404 | if ((mode == AUDIO_MODE_NORMAL || mode == AUDIO_MODE_IN_COMMUNICATION) && |
| 2405 | voice_is_in_call(adev)) { |
| 2406 | voice_stop_call(adev); |
| 2407 | adev->current_call_output = NULL; |
| 2408 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2409 | } |
| 2410 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 2411 | |
| 2412 | audio_extn_extspk_set_mode(adev->extspk, mode); |
| 2413 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2414 | return 0; |
| 2415 | } |
| 2416 | |
| 2417 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 2418 | { |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2419 | int ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2420 | struct audio_device *adev = (struct audio_device *)dev; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2421 | |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2422 | ALOGD("%s: state %d\n", __func__, state); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2423 | pthread_mutex_lock(&adev->lock); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2424 | ret = voice_set_mic_mute(adev, state); |
Eric Laurent | 7b2b5ab | 2014-09-14 12:29:59 -0700 | [diff] [blame] | 2425 | adev->mic_muted = state; |
Eric Laurent | a609e8e | 2014-06-18 02:15:17 +0000 | [diff] [blame] | 2426 | pthread_mutex_unlock(&adev->lock); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2427 | |
| 2428 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2429 | } |
| 2430 | |
| 2431 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 2432 | { |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2433 | *state = voice_get_mic_mute((struct audio_device *)dev); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2434 | return 0; |
| 2435 | } |
| 2436 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2437 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev __unused, |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2438 | const struct audio_config *config) |
| 2439 | { |
Eric Laurent | 0de8d1f | 2014-07-01 20:34:45 -0700 | [diff] [blame] | 2440 | int channel_count = audio_channel_count_from_in_mask(config->channel_mask); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2441 | |
Glenn Kasten | 68e79ce | 2014-07-15 10:56:59 -0700 | [diff] [blame] | 2442 | return get_input_buffer_size(config->sample_rate, config->format, channel_count, |
| 2443 | false /* is_low_latency: since we don't know, be conservative */); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | static int adev_open_input_stream(struct audio_hw_device *dev, |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 2447 | audio_io_handle_t handle, |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2448 | audio_devices_t devices, |
| 2449 | struct audio_config *config, |
Glenn Kasten | 68e79ce | 2014-07-15 10:56:59 -0700 | [diff] [blame] | 2450 | struct audio_stream_in **stream_in, |
Eric Laurent | 91975a6 | 2014-07-27 17:15:50 -0700 | [diff] [blame] | 2451 | audio_input_flags_t flags, |
| 2452 | const char *address __unused, |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 2453 | audio_source_t source ) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2454 | { |
| 2455 | struct audio_device *adev = (struct audio_device *)dev; |
| 2456 | struct stream_in *in; |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2457 | int ret = 0, buffer_size, frame_size; |
Eric Laurent | 0de8d1f | 2014-07-01 20:34:45 -0700 | [diff] [blame] | 2458 | int channel_count = audio_channel_count_from_in_mask(config->channel_mask); |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2459 | bool is_low_latency = false; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2460 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2461 | ALOGV("%s: enter", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2462 | *stream_in = NULL; |
| 2463 | if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0) |
| 2464 | return -EINVAL; |
| 2465 | |
| 2466 | in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); |
| 2467 | |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2468 | pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL); |
| 2469 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2470 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 2471 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 2472 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 2473 | in->stream.common.get_channels = in_get_channels; |
| 2474 | in->stream.common.get_format = in_get_format; |
| 2475 | in->stream.common.set_format = in_set_format; |
| 2476 | in->stream.common.standby = in_standby; |
| 2477 | in->stream.common.dump = in_dump; |
| 2478 | in->stream.common.set_parameters = in_set_parameters; |
| 2479 | in->stream.common.get_parameters = in_get_parameters; |
| 2480 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 2481 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 2482 | in->stream.set_gain = in_set_gain; |
| 2483 | in->stream.read = in_read; |
| 2484 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| 2485 | |
| 2486 | in->device = devices; |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 2487 | in->source = source; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2488 | in->dev = adev; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2489 | in->standby = 1; |
| 2490 | in->channel_mask = config->channel_mask; |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 2491 | in->capture_handle = handle; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2492 | |
| 2493 | /* Update config params with the requested sample rate and channels */ |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2494 | if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) { |
| 2495 | if (config->sample_rate == 0) |
| 2496 | config->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 2497 | if (config->sample_rate != 48000 && config->sample_rate != 16000 && |
| 2498 | config->sample_rate != 8000) { |
| 2499 | config->sample_rate = AFE_PROXY_SAMPLING_RATE; |
| 2500 | ret = -EINVAL; |
| 2501 | goto err_open; |
| 2502 | } |
| 2503 | if (config->format == AUDIO_FORMAT_DEFAULT) |
| 2504 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 2505 | if (config->format != AUDIO_FORMAT_PCM_16_BIT) { |
| 2506 | config->format = AUDIO_FORMAT_PCM_16_BIT; |
| 2507 | ret = -EINVAL; |
| 2508 | goto err_open; |
| 2509 | } |
| 2510 | |
| 2511 | in->usecase = USECASE_AUDIO_RECORD_AFE_PROXY; |
| 2512 | in->config = pcm_config_afe_proxy_record; |
| 2513 | } else { |
| 2514 | in->usecase = USECASE_AUDIO_RECORD; |
| 2515 | if (config->sample_rate == LOW_LATENCY_CAPTURE_SAMPLE_RATE && |
| 2516 | (flags & AUDIO_INPUT_FLAG_FAST) != 0) { |
| 2517 | is_low_latency = true; |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 2518 | #if LOW_LATENCY_CAPTURE_USE_CASE |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2519 | in->usecase = USECASE_AUDIO_RECORD_LOW_LATENCY; |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 2520 | #endif |
Ravi Kumar Alamanda | 99c752d | 2014-08-20 17:55:26 -0700 | [diff] [blame] | 2521 | } |
| 2522 | in->config = pcm_config_audio_capture; |
| 2523 | |
| 2524 | frame_size = audio_stream_in_frame_size(&in->stream); |
| 2525 | buffer_size = get_input_buffer_size(config->sample_rate, |
| 2526 | config->format, |
| 2527 | channel_count, |
| 2528 | is_low_latency); |
| 2529 | in->config.period_size = buffer_size / frame_size; |
Glenn Kasten | 68e79ce | 2014-07-15 10:56:59 -0700 | [diff] [blame] | 2530 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2531 | in->config.channels = channel_count; |
| 2532 | in->config.rate = config->sample_rate; |
| 2533 | |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 2534 | /* This stream could be for sound trigger lab, |
| 2535 | get sound trigger pcm if present */ |
| 2536 | audio_extn_sound_trigger_check_and_get_session(in); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2537 | |
| 2538 | *stream_in = &in->stream; |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2539 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2540 | return 0; |
| 2541 | |
| 2542 | err_open: |
| 2543 | free(in); |
| 2544 | *stream_in = NULL; |
| 2545 | return ret; |
| 2546 | } |
| 2547 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2548 | static void adev_close_input_stream(struct audio_hw_device *dev __unused, |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2549 | struct audio_stream_in *stream) |
| 2550 | { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2551 | ALOGV("%s", __func__); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 2552 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2553 | in_standby(&stream->common); |
| 2554 | free(stream); |
| 2555 | |
| 2556 | return; |
| 2557 | } |
| 2558 | |
Haynes Mathew George | cc9649b | 2014-06-10 15:08:39 -0700 | [diff] [blame] | 2559 | static int adev_dump(const audio_hw_device_t *device __unused, int fd __unused) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2560 | { |
| 2561 | return 0; |
| 2562 | } |
| 2563 | |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 2564 | /* verifies input and output devices and their capabilities. |
| 2565 | * |
| 2566 | * This verification is required when enabling extended bit-depth or |
| 2567 | * sampling rates, as not all qcom products support it. |
| 2568 | * |
| 2569 | * Suitable for calling only on initialization such as adev_open(). |
| 2570 | * It fills the audio_device use_case_table[] array. |
| 2571 | * |
| 2572 | * Has a side-effect that it needs to configure audio routing / devices |
| 2573 | * in order to power up the devices and read the device parameters. |
| 2574 | * It does not acquire any hw device lock. Should restore the devices |
| 2575 | * back to "normal state" upon completion. |
| 2576 | */ |
| 2577 | static int adev_verify_devices(struct audio_device *adev) |
| 2578 | { |
| 2579 | /* enumeration is a bit difficult because one really wants to pull |
| 2580 | * the use_case, device id, etc from the hidden pcm_device_table[]. |
| 2581 | * In this case there are the following use cases and device ids. |
| 2582 | * |
| 2583 | * [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = {0, 0}, |
| 2584 | * [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = {15, 15}, |
| 2585 | * [USECASE_AUDIO_PLAYBACK_MULTI_CH] = {1, 1}, |
| 2586 | * [USECASE_AUDIO_PLAYBACK_OFFLOAD] = {9, 9}, |
| 2587 | * [USECASE_AUDIO_RECORD] = {0, 0}, |
| 2588 | * [USECASE_AUDIO_RECORD_LOW_LATENCY] = {15, 15}, |
| 2589 | * [USECASE_VOICE_CALL] = {2, 2}, |
| 2590 | * |
| 2591 | * USECASE_AUDIO_PLAYBACK_OFFLOAD, USECASE_AUDIO_PLAYBACK_MULTI_CH omitted. |
| 2592 | * USECASE_VOICE_CALL omitted, but possible for either input or output. |
| 2593 | */ |
| 2594 | |
| 2595 | /* should be the usecases enabled in adev_open_input_stream() */ |
| 2596 | static const int test_in_usecases[] = { |
| 2597 | USECASE_AUDIO_RECORD, |
| 2598 | USECASE_AUDIO_RECORD_LOW_LATENCY, /* does not appear to be used */ |
| 2599 | }; |
| 2600 | /* should be the usecases enabled in adev_open_output_stream()*/ |
| 2601 | static const int test_out_usecases[] = { |
| 2602 | USECASE_AUDIO_PLAYBACK_DEEP_BUFFER, |
| 2603 | USECASE_AUDIO_PLAYBACK_LOW_LATENCY, |
| 2604 | }; |
| 2605 | static const usecase_type_t usecase_type_by_dir[] = { |
| 2606 | PCM_PLAYBACK, |
| 2607 | PCM_CAPTURE, |
| 2608 | }; |
| 2609 | static const unsigned flags_by_dir[] = { |
| 2610 | PCM_OUT, |
| 2611 | PCM_IN, |
| 2612 | }; |
| 2613 | |
| 2614 | size_t i; |
| 2615 | unsigned dir; |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2616 | const unsigned card_id = adev->snd_card; |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 2617 | char info[512]; /* for possible debug info */ |
| 2618 | |
| 2619 | for (dir = 0; dir < 2; ++dir) { |
| 2620 | const usecase_type_t usecase_type = usecase_type_by_dir[dir]; |
| 2621 | const unsigned flags_dir = flags_by_dir[dir]; |
| 2622 | const size_t testsize = |
| 2623 | dir ? ARRAY_SIZE(test_in_usecases) : ARRAY_SIZE(test_out_usecases); |
| 2624 | const int *testcases = |
| 2625 | dir ? test_in_usecases : test_out_usecases; |
| 2626 | const audio_devices_t audio_device = |
| 2627 | dir ? AUDIO_DEVICE_IN_BUILTIN_MIC : AUDIO_DEVICE_OUT_SPEAKER; |
| 2628 | |
| 2629 | for (i = 0; i < testsize; ++i) { |
| 2630 | const audio_usecase_t audio_usecase = testcases[i]; |
| 2631 | int device_id; |
| 2632 | snd_device_t snd_device; |
| 2633 | struct pcm_params **pparams; |
| 2634 | struct stream_out out; |
| 2635 | struct stream_in in; |
| 2636 | struct audio_usecase uc_info; |
| 2637 | int retval; |
| 2638 | |
| 2639 | pparams = &adev->use_case_table[audio_usecase]; |
| 2640 | pcm_params_free(*pparams); /* can accept null input */ |
| 2641 | *pparams = NULL; |
| 2642 | |
| 2643 | /* find the device ID for the use case (signed, for error) */ |
| 2644 | device_id = platform_get_pcm_device_id(audio_usecase, usecase_type); |
| 2645 | if (device_id < 0) |
| 2646 | continue; |
| 2647 | |
| 2648 | /* prepare structures for device probing */ |
| 2649 | memset(&uc_info, 0, sizeof(uc_info)); |
| 2650 | uc_info.id = audio_usecase; |
| 2651 | uc_info.type = usecase_type; |
| 2652 | if (dir) { |
| 2653 | adev->active_input = ∈ |
| 2654 | memset(&in, 0, sizeof(in)); |
| 2655 | in.device = audio_device; |
| 2656 | in.source = AUDIO_SOURCE_VOICE_COMMUNICATION; |
| 2657 | uc_info.stream.in = ∈ |
| 2658 | } else { |
| 2659 | adev->active_input = NULL; |
| 2660 | } |
| 2661 | memset(&out, 0, sizeof(out)); |
| 2662 | out.devices = audio_device; /* only field needed in select_devices */ |
| 2663 | uc_info.stream.out = &out; |
| 2664 | uc_info.devices = audio_device; |
| 2665 | uc_info.in_snd_device = SND_DEVICE_NONE; |
| 2666 | uc_info.out_snd_device = SND_DEVICE_NONE; |
| 2667 | list_add_tail(&adev->usecase_list, &uc_info.list); |
| 2668 | |
| 2669 | /* select device - similar to start_(in/out)put_stream() */ |
| 2670 | retval = select_devices(adev, audio_usecase); |
| 2671 | if (retval >= 0) { |
| 2672 | *pparams = pcm_params_get(card_id, device_id, flags_dir); |
| 2673 | #if LOG_NDEBUG == 0 |
| 2674 | if (*pparams) { |
| 2675 | ALOGV("%s: (%s) card %d device %d", __func__, |
| 2676 | dir ? "input" : "output", card_id, device_id); |
| 2677 | pcm_params_to_string(*pparams, info, ARRAY_SIZE(info)); |
| 2678 | ALOGV(info); /* print parameters */ |
| 2679 | } else { |
| 2680 | ALOGV("%s: cannot locate card %d device %d", __func__, card_id, device_id); |
| 2681 | } |
| 2682 | #endif |
| 2683 | } |
| 2684 | |
| 2685 | /* deselect device - similar to stop_(in/out)put_stream() */ |
| 2686 | /* 1. Get and set stream specific mixer controls */ |
Glenn Kasten | e713730 | 2014-04-28 15:12:18 -0700 | [diff] [blame] | 2687 | retval = disable_audio_route(adev, &uc_info); |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 2688 | /* 2. Disable the rx device */ |
| 2689 | retval = disable_snd_device(adev, |
Glenn Kasten | e713730 | 2014-04-28 15:12:18 -0700 | [diff] [blame] | 2690 | dir ? uc_info.in_snd_device : uc_info.out_snd_device); |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 2691 | list_remove(&uc_info.list); |
| 2692 | } |
| 2693 | } |
| 2694 | adev->active_input = NULL; /* restore adev state */ |
| 2695 | return 0; |
| 2696 | } |
| 2697 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2698 | static int adev_close(hw_device_t *device) |
| 2699 | { |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 2700 | size_t i; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2701 | struct audio_device *adev = (struct audio_device *)device; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 2702 | |
| 2703 | if (!adev) |
| 2704 | return 0; |
| 2705 | |
| 2706 | pthread_mutex_lock(&adev_init_lock); |
| 2707 | |
| 2708 | if ((--audio_device_ref_count) == 0) { |
| 2709 | audio_route_free(adev->audio_route); |
| 2710 | free(adev->snd_dev_ref_cnt); |
| 2711 | platform_deinit(adev->platform); |
| 2712 | audio_extn_extspk_deinit(adev->extspk); |
| 2713 | audio_extn_sound_trigger_deinit(adev); |
| 2714 | for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) { |
| 2715 | pcm_params_free(adev->use_case_table[i]); |
| 2716 | } |
| 2717 | free(device); |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 2718 | } |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 2719 | |
| 2720 | pthread_mutex_unlock(&adev_init_lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2721 | return 0; |
| 2722 | } |
| 2723 | |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 2724 | /* This returns 1 if the input parameter looks at all plausible as a low latency period size, |
| 2725 | * or 0 otherwise. A return value of 1 doesn't mean the value is guaranteed to work, |
| 2726 | * just that it _might_ work. |
| 2727 | */ |
| 2728 | static int period_size_is_plausible_for_low_latency(int period_size) |
| 2729 | { |
| 2730 | switch (period_size) { |
Glenn Kasten | 5b5d04e | 2015-04-09 09:43:29 -0700 | [diff] [blame] | 2731 | case 48: |
| 2732 | case 96: |
| 2733 | case 144: |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 2734 | case 160: |
Glenn Kasten | 5b5d04e | 2015-04-09 09:43:29 -0700 | [diff] [blame] | 2735 | case 192: |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 2736 | case 240: |
| 2737 | case 320: |
| 2738 | case 480: |
| 2739 | return 1; |
| 2740 | default: |
| 2741 | return 0; |
| 2742 | } |
| 2743 | } |
| 2744 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2745 | static int adev_open(const hw_module_t *module, const char *name, |
| 2746 | hw_device_t **device) |
| 2747 | { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 2748 | int i, ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2749 | |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 2750 | ALOGD("%s: enter", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2751 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 2752 | pthread_mutex_lock(&adev_init_lock); |
| 2753 | if (audio_device_ref_count != 0) { |
| 2754 | *device = &adev->device.common; |
| 2755 | audio_device_ref_count++; |
| 2756 | ALOGV("%s: returning existing instance of adev", __func__); |
| 2757 | ALOGV("%s: exit", __func__); |
| 2758 | pthread_mutex_unlock(&adev_init_lock); |
| 2759 | return 0; |
| 2760 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2761 | adev = calloc(1, sizeof(struct audio_device)); |
| 2762 | |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2763 | pthread_mutex_init(&adev->lock, (const pthread_mutexattr_t *) NULL); |
| 2764 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2765 | adev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 2766 | adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
| 2767 | adev->device.common.module = (struct hw_module_t *)module; |
| 2768 | adev->device.common.close = adev_close; |
| 2769 | |
| 2770 | adev->device.init_check = adev_init_check; |
| 2771 | adev->device.set_voice_volume = adev_set_voice_volume; |
| 2772 | adev->device.set_master_volume = adev_set_master_volume; |
| 2773 | adev->device.get_master_volume = adev_get_master_volume; |
| 2774 | adev->device.set_master_mute = adev_set_master_mute; |
| 2775 | adev->device.get_master_mute = adev_get_master_mute; |
| 2776 | adev->device.set_mode = adev_set_mode; |
| 2777 | adev->device.set_mic_mute = adev_set_mic_mute; |
| 2778 | adev->device.get_mic_mute = adev_get_mic_mute; |
| 2779 | adev->device.set_parameters = adev_set_parameters; |
| 2780 | adev->device.get_parameters = adev_get_parameters; |
| 2781 | adev->device.get_input_buffer_size = adev_get_input_buffer_size; |
| 2782 | adev->device.open_output_stream = adev_open_output_stream; |
| 2783 | adev->device.close_output_stream = adev_close_output_stream; |
| 2784 | adev->device.open_input_stream = adev_open_input_stream; |
| 2785 | adev->device.close_input_stream = adev_close_input_stream; |
| 2786 | adev->device.dump = adev_dump; |
| 2787 | |
| 2788 | /* Set the default route before the PCM stream is opened */ |
| 2789 | pthread_mutex_lock(&adev->lock); |
| 2790 | adev->mode = AUDIO_MODE_NORMAL; |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 2791 | adev->active_input = NULL; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 2792 | adev->primary_output = NULL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2793 | adev->bluetooth_nrec = true; |
Ravi Kumar Alamanda | f996704 | 2013-02-14 19:35:14 -0800 | [diff] [blame] | 2794 | adev->acdb_settings = TTY_MODE_OFF; |
Eric Laurent | 07eeafd | 2013-10-06 12:52:49 -0700 | [diff] [blame] | 2795 | /* adev->cur_hdmi_channels = 0; by calloc() */ |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 2796 | adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int)); |
Vineeta Srivastava | 4b89e37 | 2014-06-19 14:21:42 -0700 | [diff] [blame] | 2797 | voice_init(adev); |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 2798 | list_init(&adev->usecase_list); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2799 | pthread_mutex_unlock(&adev->lock); |
| 2800 | |
| 2801 | /* Loads platform specific libraries dynamically */ |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 2802 | adev->platform = platform_init(adev); |
| 2803 | if (!adev->platform) { |
| 2804 | free(adev->snd_dev_ref_cnt); |
| 2805 | free(adev); |
| 2806 | ALOGE("%s: Failed to init platform data, aborting.", __func__); |
| 2807 | *device = NULL; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 2808 | pthread_mutex_unlock(&adev_init_lock); |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 2809 | return -EINVAL; |
| 2810 | } |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 2811 | |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 2812 | adev->extspk = audio_extn_extspk_init(adev); |
Ravi Kumar Alamanda | a417cc5 | 2015-05-01 16:41:56 -0700 | [diff] [blame] | 2813 | audio_extn_sound_trigger_init(adev); |
Eric Laurent | 0499d4f | 2014-08-25 22:39:29 -0500 | [diff] [blame] | 2814 | |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 2815 | if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) { |
| 2816 | adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW); |
| 2817 | if (adev->visualizer_lib == NULL) { |
| 2818 | ALOGE("%s: DLOPEN failed for %s", __func__, VISUALIZER_LIBRARY_PATH); |
| 2819 | } else { |
| 2820 | ALOGV("%s: DLOPEN successful for %s", __func__, VISUALIZER_LIBRARY_PATH); |
| 2821 | adev->visualizer_start_output = |
Haynes Mathew George | 41f8665 | 2014-06-17 14:22:15 -0700 | [diff] [blame] | 2822 | (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib, |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 2823 | "visualizer_hal_start_output"); |
| 2824 | adev->visualizer_stop_output = |
Haynes Mathew George | 41f8665 | 2014-06-17 14:22:15 -0700 | [diff] [blame] | 2825 | (int (*)(audio_io_handle_t, int))dlsym(adev->visualizer_lib, |
Eric Laurent | c4aef75 | 2013-09-12 17:45:53 -0700 | [diff] [blame] | 2826 | "visualizer_hal_stop_output"); |
| 2827 | } |
| 2828 | } |
| 2829 | |
Haynes Mathew George | 41f8665 | 2014-06-17 14:22:15 -0700 | [diff] [blame] | 2830 | if (access(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, R_OK) == 0) { |
| 2831 | adev->offload_effects_lib = dlopen(OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH, RTLD_NOW); |
| 2832 | if (adev->offload_effects_lib == NULL) { |
| 2833 | ALOGE("%s: DLOPEN failed for %s", __func__, |
| 2834 | OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH); |
| 2835 | } else { |
| 2836 | ALOGV("%s: DLOPEN successful for %s", __func__, |
| 2837 | OFFLOAD_EFFECTS_BUNDLE_LIBRARY_PATH); |
| 2838 | adev->offload_effects_start_output = |
| 2839 | (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib, |
| 2840 | "offload_effects_bundle_hal_start_output"); |
| 2841 | adev->offload_effects_stop_output = |
| 2842 | (int (*)(audio_io_handle_t, int))dlsym(adev->offload_effects_lib, |
| 2843 | "offload_effects_bundle_hal_stop_output"); |
| 2844 | } |
| 2845 | } |
| 2846 | |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 2847 | adev->bt_wb_speech_enabled = false; |
Eric Laurent | cefbbac | 2014-09-04 13:54:10 -0500 | [diff] [blame] | 2848 | adev->enable_voicerx = false; |
Ravi Kumar Alamanda | 9f30654 | 2014-04-02 15:11:49 -0700 | [diff] [blame] | 2849 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2850 | *device = &adev->device.common; |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 2851 | |
Andy Hung | 31aca91 | 2014-03-20 17:14:59 -0700 | [diff] [blame] | 2852 | if (k_enable_extended_precision) |
| 2853 | adev_verify_devices(adev); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2854 | |
Glenn Kasten | 4f99339 | 2014-05-14 07:30:48 -0700 | [diff] [blame] | 2855 | char value[PROPERTY_VALUE_MAX]; |
| 2856 | int trial; |
| 2857 | if (property_get("audio_hal.period_size", value, NULL) > 0) { |
| 2858 | trial = atoi(value); |
| 2859 | if (period_size_is_plausible_for_low_latency(trial)) { |
| 2860 | pcm_config_low_latency.period_size = trial; |
| 2861 | pcm_config_low_latency.start_threshold = trial / 4; |
| 2862 | pcm_config_low_latency.avail_min = trial / 4; |
| 2863 | configured_low_latency_capture_period_size = trial; |
| 2864 | } |
| 2865 | } |
| 2866 | if (property_get("audio_hal.in_period_size", value, NULL) > 0) { |
| 2867 | trial = atoi(value); |
| 2868 | if (period_size_is_plausible_for_low_latency(trial)) { |
| 2869 | configured_low_latency_capture_period_size = trial; |
| 2870 | } |
| 2871 | } |
| 2872 | |
vivek mehta | 1a9b7c0 | 2015-06-25 11:49:38 -0700 | [diff] [blame] | 2873 | audio_device_ref_count++; |
| 2874 | pthread_mutex_unlock(&adev_init_lock); |
| 2875 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 2876 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 2877 | return 0; |
| 2878 | } |
| 2879 | |
| 2880 | static struct hw_module_methods_t hal_module_methods = { |
| 2881 | .open = adev_open, |
| 2882 | }; |
| 2883 | |
| 2884 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 2885 | .common = { |
| 2886 | .tag = HARDWARE_MODULE_TAG, |
| 2887 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 2888 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 2889 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 2890 | .name = "QCOM Audio HAL", |
| 2891 | .author = "Code Aurora Forum", |
| 2892 | .methods = &hal_module_methods, |
| 2893 | }, |
| 2894 | }; |