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