Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1 | /* |
Apoorv Raghuvanshi | 9eaf94e | 2013-10-04 16:13:44 -0700 | [diff] [blame] | 2 | * Copyright (c) 2013, The Linux Foundation. All rights reserved. |
| 3 | * Not a Contribution. |
| 4 | * |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 5 | * Copyright (C) 2013 The Android Open Source Project |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | * you may not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
| 18 | */ |
| 19 | |
| 20 | #define LOG_TAG "audio_hw_primary" |
| 21 | /*#define LOG_NDEBUG 0*/ |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 22 | #define LOG_NDDEBUG 0 |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 23 | |
| 24 | #include <errno.h> |
| 25 | #include <pthread.h> |
| 26 | #include <stdint.h> |
| 27 | #include <sys/time.h> |
| 28 | #include <stdlib.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 29 | #include <math.h> |
| 30 | |
| 31 | #include <cutils/log.h> |
| 32 | #include <cutils/str_parms.h> |
| 33 | #include <cutils/properties.h> |
| 34 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 35 | #include <hardware/audio_effect.h> |
| 36 | #include <audio_effects/effect_aec.h> |
| 37 | #include <audio_effects/effect_ns.h> |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 38 | #include "audio_hw.h" |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 39 | #include "platform_api.h" |
| 40 | #include <platform.h> |
Apoorv Raghuvanshi | 9eaf94e | 2013-10-04 16:13:44 -0700 | [diff] [blame] | 41 | #include "audio_extn.h" |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 42 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 43 | struct pcm_config pcm_config_deep_buffer = { |
| 44 | .channels = 2, |
| 45 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 46 | .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, |
| 47 | .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, |
| 48 | .format = PCM_FORMAT_S16_LE, |
| 49 | .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, |
| 50 | .stop_threshold = INT_MAX, |
| 51 | .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, |
| 52 | }; |
| 53 | |
| 54 | struct pcm_config pcm_config_low_latency = { |
| 55 | .channels = 2, |
| 56 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, |
| 57 | .period_size = LOW_LATENCY_OUTPUT_PERIOD_SIZE, |
| 58 | .period_count = LOW_LATENCY_OUTPUT_PERIOD_COUNT, |
| 59 | .format = PCM_FORMAT_S16_LE, |
| 60 | .start_threshold = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 61 | .stop_threshold = INT_MAX, |
| 62 | .avail_min = LOW_LATENCY_OUTPUT_PERIOD_SIZE / 4, |
| 63 | }; |
| 64 | |
| 65 | struct pcm_config pcm_config_hdmi_multi = { |
| 66 | .channels = HDMI_MULTI_DEFAULT_CHANNEL_COUNT, /* changed when the stream is opened */ |
| 67 | .rate = DEFAULT_OUTPUT_SAMPLING_RATE, /* changed when the stream is opened */ |
| 68 | .period_size = HDMI_MULTI_PERIOD_SIZE, |
| 69 | .period_count = HDMI_MULTI_PERIOD_COUNT, |
| 70 | .format = PCM_FORMAT_S16_LE, |
| 71 | .start_threshold = 0, |
| 72 | .stop_threshold = INT_MAX, |
| 73 | .avail_min = 0, |
| 74 | }; |
| 75 | |
| 76 | struct pcm_config pcm_config_audio_capture = { |
| 77 | .channels = 2, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 78 | .period_count = AUDIO_CAPTURE_PERIOD_COUNT, |
| 79 | .format = PCM_FORMAT_S16_LE, |
| 80 | }; |
| 81 | |
| 82 | struct pcm_config pcm_config_voice_call = { |
| 83 | .channels = 1, |
| 84 | .rate = 8000, |
| 85 | .period_size = 160, |
| 86 | .period_count = 2, |
| 87 | .format = PCM_FORMAT_S16_LE, |
| 88 | }; |
| 89 | |
| 90 | static const char * const use_case_table[AUDIO_USECASE_MAX] = { |
| 91 | [USECASE_AUDIO_PLAYBACK_DEEP_BUFFER] = "deep-buffer-playback", |
| 92 | [USECASE_AUDIO_PLAYBACK_LOW_LATENCY] = "low-latency-playback", |
| 93 | [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "multi-channel-playback", |
| 94 | [USECASE_AUDIO_RECORD] = "audio-record", |
| 95 | [USECASE_AUDIO_RECORD_LOW_LATENCY] = "low-latency-record", |
| 96 | [USECASE_VOICE_CALL] = "voice-call", |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 97 | [USECASE_AUDIO_PLAYBACK_FM] = "play-fm", |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 98 | }; |
| 99 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 100 | |
| 101 | #define STRING_TO_ENUM(string) { #string, string } |
| 102 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 103 | struct string_to_enum { |
| 104 | const char *name; |
| 105 | uint32_t value; |
| 106 | }; |
| 107 | |
| 108 | static const struct string_to_enum out_channels_name_to_enum_table[] = { |
| 109 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), |
| 110 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), |
| 111 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), |
| 112 | }; |
| 113 | |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 114 | static struct audio_device *adev = NULL; |
| 115 | static pthread_mutex_t adev_init_lock; |
| 116 | static bool is_adev_initialised = false; |
Ravi Kumar Alamanda | f996704 | 2013-02-14 19:35:14 -0800 | [diff] [blame] | 117 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 118 | static int enable_audio_route(struct audio_device *adev, |
| 119 | struct audio_usecase *usecase, |
| 120 | bool update_mixer) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 121 | { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 122 | snd_device_t snd_device; |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 123 | char mixer_path[MIXER_PATH_MAX_LENGTH]; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 124 | |
| 125 | if (usecase == NULL) |
| 126 | return -EINVAL; |
| 127 | |
| 128 | ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); |
| 129 | |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 130 | if (usecase->type == PCM_CAPTURE) |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 131 | snd_device = usecase->in_snd_device; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 132 | else |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 133 | snd_device = usecase->out_snd_device; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 134 | |
| 135 | strcpy(mixer_path, use_case_table[usecase->id]); |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 136 | platform_add_backend_name(mixer_path, snd_device); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 137 | ALOGV("%s: apply mixer path: %s", __func__, mixer_path); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 138 | audio_route_apply_path(adev->audio_route, mixer_path); |
| 139 | if (update_mixer) |
| 140 | audio_route_update_mixer(adev->audio_route); |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 141 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 142 | ALOGV("%s: exit", __func__); |
| 143 | return 0; |
| 144 | } |
| 145 | |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 146 | int disable_audio_route(struct audio_device *adev, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 147 | struct audio_usecase *usecase, |
| 148 | bool update_mixer) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 149 | { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 150 | snd_device_t snd_device; |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 151 | char mixer_path[MIXER_PATH_MAX_LENGTH]; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 152 | |
| 153 | if (usecase == NULL) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | ALOGV("%s: enter: usecase(%d)", __func__, usecase->id); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 157 | if (usecase->type == PCM_CAPTURE) |
| 158 | snd_device = usecase->in_snd_device; |
| 159 | else |
| 160 | snd_device = usecase->out_snd_device; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 161 | strcpy(mixer_path, use_case_table[usecase->id]); |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 162 | platform_add_backend_name(mixer_path, snd_device); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 163 | ALOGV("%s: reset mixer path: %s", __func__, mixer_path); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 164 | audio_route_reset_path(adev->audio_route, mixer_path); |
| 165 | if (update_mixer) |
| 166 | audio_route_update_mixer(adev->audio_route); |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 167 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 168 | ALOGV("%s: exit", __func__); |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static int enable_snd_device(struct audio_device *adev, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 173 | snd_device_t snd_device, |
| 174 | bool update_mixer) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 175 | { |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 176 | if (snd_device < SND_DEVICE_MIN || |
| 177 | snd_device >= SND_DEVICE_MAX) { |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 178 | ALOGE("%s: Invalid sound device %d", __func__, snd_device); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 179 | return -EINVAL; |
| 180 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 181 | |
| 182 | adev->snd_dev_ref_cnt[snd_device]++; |
| 183 | if (adev->snd_dev_ref_cnt[snd_device] > 1) { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 184 | ALOGV("%s: snd_device(%d: %s) is already active", |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 185 | __func__, snd_device, platform_get_snd_device_name(snd_device)); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 189 | if (platform_send_audio_calibration(adev->platform, snd_device) < 0) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 190 | adev->snd_dev_ref_cnt[snd_device]--; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 191 | return -EINVAL; |
| 192 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 193 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 194 | ALOGV("%s: snd_device(%d: %s)", __func__, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 195 | snd_device, platform_get_snd_device_name(snd_device)); |
| 196 | audio_route_apply_path(adev->audio_route, platform_get_snd_device_name(snd_device)); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 197 | if (update_mixer) |
| 198 | audio_route_update_mixer(adev->audio_route); |
| 199 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 200 | return 0; |
| 201 | } |
| 202 | |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 203 | int disable_snd_device(struct audio_device *adev, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 204 | snd_device_t snd_device, |
| 205 | bool update_mixer) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 206 | { |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 207 | if (snd_device < SND_DEVICE_MIN || |
| 208 | snd_device >= SND_DEVICE_MAX) { |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 209 | ALOGE("%s: Invalid sound device %d", __func__, snd_device); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 210 | return -EINVAL; |
| 211 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 212 | if (adev->snd_dev_ref_cnt[snd_device] <= 0) { |
| 213 | ALOGE("%s: device ref cnt is already 0", __func__); |
| 214 | return -EINVAL; |
| 215 | } |
| 216 | adev->snd_dev_ref_cnt[snd_device]--; |
| 217 | if (adev->snd_dev_ref_cnt[snd_device] == 0) { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 218 | ALOGV("%s: snd_device(%d: %s)", __func__, |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 219 | snd_device, platform_get_snd_device_name(snd_device)); |
| 220 | audio_route_reset_path(adev->audio_route, platform_get_snd_device_name(snd_device)); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 221 | if (update_mixer) |
| 222 | audio_route_update_mixer(adev->audio_route); |
| 223 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 224 | return 0; |
| 225 | } |
| 226 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 227 | static void check_usecases_codec_backend(struct audio_device *adev, |
| 228 | struct audio_usecase *uc_info, |
| 229 | snd_device_t snd_device) |
| 230 | { |
| 231 | struct listnode *node; |
| 232 | struct audio_usecase *usecase; |
| 233 | bool switch_device[AUDIO_USECASE_MAX]; |
| 234 | int i, num_uc_to_switch = 0; |
| 235 | |
| 236 | /* |
| 237 | * This function is to make sure that all the usecases that are active on |
| 238 | * the hardware codec backend are always routed to any one device that is |
| 239 | * handled by the hardware codec. |
| 240 | * For example, if low-latency and deep-buffer usecases are currently active |
| 241 | * on speaker and out_set_parameters(headset) is received on low-latency |
| 242 | * output, then we have to make sure deep-buffer is also switched to headset, |
| 243 | * because of the limitation that both the devices cannot be enabled |
| 244 | * at the same time as they share the same backend. |
| 245 | */ |
| 246 | /* Disable all the usecases on the shared backend other than the |
| 247 | specified usecase */ |
| 248 | for (i = 0; i < AUDIO_USECASE_MAX; i++) |
| 249 | switch_device[i] = false; |
| 250 | |
| 251 | list_for_each(node, &adev->usecase_list) { |
| 252 | usecase = node_to_item(node, struct audio_usecase, list); |
| 253 | if (usecase->type != PCM_CAPTURE && |
| 254 | usecase != uc_info && |
| 255 | usecase->out_snd_device != snd_device && |
| 256 | usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) { |
| 257 | ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", |
| 258 | __func__, use_case_table[usecase->id], |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 259 | platform_get_snd_device_name(usecase->out_snd_device)); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 260 | disable_audio_route(adev, usecase, false); |
| 261 | switch_device[usecase->id] = true; |
| 262 | num_uc_to_switch++; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (num_uc_to_switch) { |
| 267 | /* Make sure all the streams are de-routed before disabling the device */ |
| 268 | audio_route_update_mixer(adev->audio_route); |
| 269 | |
| 270 | list_for_each(node, &adev->usecase_list) { |
| 271 | usecase = node_to_item(node, struct audio_usecase, list); |
| 272 | if (switch_device[usecase->id]) { |
| 273 | disable_snd_device(adev, usecase->out_snd_device, false); |
| 274 | enable_snd_device(adev, snd_device, false); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* Make sure new snd device is enabled before re-routing the streams */ |
| 279 | audio_route_update_mixer(adev->audio_route); |
| 280 | |
| 281 | /* Re-route all the usecases on the shared backend other than the |
| 282 | specified usecase to new snd devices */ |
| 283 | list_for_each(node, &adev->usecase_list) { |
| 284 | usecase = node_to_item(node, struct audio_usecase, list); |
| 285 | /* Update the out_snd_device only before enabling the audio route */ |
| 286 | if (switch_device[usecase->id] ) { |
| 287 | usecase->out_snd_device = snd_device; |
| 288 | enable_audio_route(adev, usecase, false); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | audio_route_update_mixer(adev->audio_route); |
| 293 | } |
| 294 | } |
| 295 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 296 | static void check_and_route_capture_usecases(struct audio_device *adev, |
| 297 | struct audio_usecase *uc_info, |
| 298 | snd_device_t snd_device) |
| 299 | { |
| 300 | struct listnode *node; |
| 301 | struct audio_usecase *usecase; |
| 302 | bool switch_device[AUDIO_USECASE_MAX]; |
| 303 | int i, num_uc_to_switch = 0; |
| 304 | |
| 305 | /* |
| 306 | * This function is to make sure that all the active capture usecases |
| 307 | * are always routed to the same input sound device. |
| 308 | * For example, if audio-record and voice-call usecases are currently |
| 309 | * active on speaker(rx) and speaker-mic (tx) and out_set_parameters(earpiece) |
| 310 | * is received for voice call then we have to make sure that audio-record |
| 311 | * usecase is also switched to earpiece i.e. voice-dmic-ef, |
| 312 | * because of the limitation that two devices cannot be enabled |
| 313 | * at the same time if they share the same backend. |
| 314 | */ |
| 315 | for (i = 0; i < AUDIO_USECASE_MAX; i++) |
| 316 | switch_device[i] = false; |
| 317 | |
| 318 | list_for_each(node, &adev->usecase_list) { |
| 319 | usecase = node_to_item(node, struct audio_usecase, list); |
| 320 | if (usecase->type != PCM_PLAYBACK && |
| 321 | usecase != uc_info && |
| 322 | usecase->in_snd_device != snd_device) { |
| 323 | ALOGV("%s: Usecase (%s) is active on (%s) - disabling ..", |
| 324 | __func__, use_case_table[usecase->id], |
Devin Kim | 1e5f353 | 2013-08-09 07:48:29 -0700 | [diff] [blame] | 325 | platform_get_snd_device_name(usecase->in_snd_device)); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 326 | disable_audio_route(adev, usecase, false); |
| 327 | switch_device[usecase->id] = true; |
| 328 | num_uc_to_switch++; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | if (num_uc_to_switch) { |
| 333 | /* Make sure all the streams are de-routed before disabling the device */ |
| 334 | audio_route_update_mixer(adev->audio_route); |
| 335 | |
| 336 | list_for_each(node, &adev->usecase_list) { |
| 337 | usecase = node_to_item(node, struct audio_usecase, list); |
| 338 | if (switch_device[usecase->id]) { |
| 339 | disable_snd_device(adev, usecase->in_snd_device, false); |
| 340 | enable_snd_device(adev, snd_device, false); |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | /* Make sure new snd device is enabled before re-routing the streams */ |
| 345 | audio_route_update_mixer(adev->audio_route); |
| 346 | |
| 347 | /* Re-route all the usecases on the shared backend other than the |
| 348 | specified usecase to new snd devices */ |
| 349 | list_for_each(node, &adev->usecase_list) { |
| 350 | usecase = node_to_item(node, struct audio_usecase, list); |
| 351 | /* Update the in_snd_device only before enabling the audio route */ |
| 352 | if (switch_device[usecase->id] ) { |
| 353 | usecase->in_snd_device = snd_device; |
| 354 | enable_audio_route(adev, usecase, false); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | audio_route_update_mixer(adev->audio_route); |
| 359 | } |
| 360 | } |
| 361 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 362 | |
| 363 | /* must be called with hw device mutex locked */ |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 364 | static int read_hdmi_channel_masks(struct stream_out *out) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 365 | { |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 366 | int ret = 0; |
Haynes Mathew George | 47cd4cb | 2013-07-19 11:58:50 -0700 | [diff] [blame] | 367 | int channels = platform_edid_get_max_channels(out->dev->platform); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 368 | |
| 369 | switch (channels) { |
| 370 | /* |
| 371 | * Do not handle stereo output in Multi-channel cases |
| 372 | * Stereo case is handled in normal playback path |
| 373 | */ |
| 374 | case 6: |
| 375 | ALOGV("%s: HDMI supports 5.1", __func__); |
| 376 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; |
| 377 | break; |
| 378 | case 8: |
| 379 | ALOGV("%s: HDMI supports 5.1 and 7.1 channels", __func__); |
| 380 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; |
| 381 | out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1; |
| 382 | break; |
| 383 | default: |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 384 | ALOGE("HDMI does not support multi channel playback"); |
| 385 | ret = -ENOSYS; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 386 | break; |
| 387 | } |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 388 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 391 | struct audio_usecase *get_usecase_from_list(struct audio_device *adev, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 392 | audio_usecase_t uc_id) |
| 393 | { |
| 394 | struct audio_usecase *usecase; |
| 395 | struct listnode *node; |
| 396 | |
| 397 | list_for_each(node, &adev->usecase_list) { |
| 398 | usecase = node_to_item(node, struct audio_usecase, list); |
| 399 | if (usecase->id == uc_id) |
| 400 | return usecase; |
| 401 | } |
| 402 | return NULL; |
| 403 | } |
| 404 | |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 405 | int select_devices(struct audio_device *adev, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 406 | audio_usecase_t uc_id) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 407 | { |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 408 | snd_device_t out_snd_device = SND_DEVICE_NONE; |
| 409 | snd_device_t in_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 410 | struct audio_usecase *usecase = NULL; |
| 411 | struct audio_usecase *vc_usecase = NULL; |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 412 | struct listnode *node; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 413 | int status = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 414 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 415 | usecase = get_usecase_from_list(adev, uc_id); |
| 416 | if (usecase == NULL) { |
| 417 | ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id); |
| 418 | return -EINVAL; |
| 419 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 420 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 421 | if (usecase->type == VOICE_CALL) { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 422 | out_snd_device = platform_get_output_snd_device(adev->platform, |
| 423 | usecase->stream.out->devices); |
| 424 | 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] | 425 | usecase->devices = usecase->stream.out->devices; |
| 426 | } else { |
| 427 | /* |
| 428 | * If the voice call is active, use the sound devices of voice call usecase |
| 429 | * so that it would not result any device switch. All the usecases will |
| 430 | * be switched to new device when select_devices() is called for voice call |
| 431 | * usecase. This is to avoid switching devices for voice call when |
| 432 | * check_usecases_codec_backend() is called below. |
| 433 | */ |
| 434 | if (adev->in_call) { |
| 435 | vc_usecase = get_usecase_from_list(adev, USECASE_VOICE_CALL); |
| 436 | if (vc_usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) { |
| 437 | in_snd_device = vc_usecase->in_snd_device; |
| 438 | out_snd_device = vc_usecase->out_snd_device; |
| 439 | } |
| 440 | } |
| 441 | if (usecase->type == PCM_PLAYBACK) { |
| 442 | usecase->devices = usecase->stream.out->devices; |
| 443 | in_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 444 | if (out_snd_device == SND_DEVICE_NONE) { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 445 | out_snd_device = platform_get_output_snd_device(adev->platform, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 446 | usecase->stream.out->devices); |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 447 | if (usecase->stream.out == adev->primary_output && |
| 448 | adev->active_input && |
| 449 | adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) { |
| 450 | select_devices(adev, adev->active_input->usecase); |
| 451 | } |
| 452 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 453 | } else if (usecase->type == PCM_CAPTURE) { |
| 454 | usecase->devices = usecase->stream.in->device; |
| 455 | out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 456 | if (in_snd_device == SND_DEVICE_NONE) { |
| 457 | if (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
| 458 | adev->primary_output && !adev->primary_output->standby) { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 459 | in_snd_device = platform_get_input_snd_device(adev->platform, |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 460 | adev->primary_output->devices); |
| 461 | } else { |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 462 | in_snd_device = platform_get_input_snd_device(adev->platform, |
| 463 | AUDIO_DEVICE_NONE); |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 464 | } |
| 465 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
| 469 | if (out_snd_device == usecase->out_snd_device && |
| 470 | in_snd_device == usecase->in_snd_device) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 471 | return 0; |
| 472 | } |
| 473 | |
sangwoo | bc67724 | 2013-08-08 16:53:43 +0900 | [diff] [blame] | 474 | 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] | 475 | out_snd_device, platform_get_snd_device_name(out_snd_device), |
| 476 | in_snd_device, platform_get_snd_device_name(in_snd_device)); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 477 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 478 | /* |
| 479 | * Limitation: While in call, to do a device switch we need to disable |
| 480 | * and enable both RX and TX devices though one of them is same as current |
| 481 | * device. |
| 482 | */ |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 483 | if (usecase->type == VOICE_CALL) { |
| 484 | status = platform_switch_voice_call_device_pre(adev->platform); |
Ravi Kumar Alamanda | 610e8cc | 2013-02-12 01:42:38 -0800 | [diff] [blame] | 485 | } |
| 486 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 487 | /* Disable current sound devices */ |
| 488 | if (usecase->out_snd_device != SND_DEVICE_NONE) { |
| 489 | disable_audio_route(adev, usecase, true); |
| 490 | disable_snd_device(adev, usecase->out_snd_device, false); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 491 | } |
| 492 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 493 | if (usecase->in_snd_device != SND_DEVICE_NONE) { |
| 494 | disable_audio_route(adev, usecase, true); |
| 495 | disable_snd_device(adev, usecase->in_snd_device, false); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 496 | } |
| 497 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 498 | /* Enable new sound devices */ |
| 499 | if (out_snd_device != SND_DEVICE_NONE) { |
| 500 | if (usecase->devices & AUDIO_DEVICE_OUT_ALL_CODEC_BACKEND) |
| 501 | check_usecases_codec_backend(adev, usecase, out_snd_device); |
| 502 | enable_snd_device(adev, out_snd_device, false); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 503 | } |
| 504 | |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 505 | if (in_snd_device != SND_DEVICE_NONE) { |
| 506 | check_and_route_capture_usecases(adev, usecase, in_snd_device); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 507 | enable_snd_device(adev, in_snd_device, false); |
Ravi Kumar Alamanda | c4ba743 | 2013-06-05 14:11:39 -0700 | [diff] [blame] | 508 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 509 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 510 | if (usecase->type == VOICE_CALL) |
| 511 | status = platform_switch_voice_call_device_post(adev->platform, |
| 512 | out_snd_device, |
| 513 | in_snd_device); |
Ravi Kumar Alamanda | 610e8cc | 2013-02-12 01:42:38 -0800 | [diff] [blame] | 514 | |
sangwoo | 170731f | 2013-06-08 15:36:36 +0900 | [diff] [blame] | 515 | audio_route_update_mixer(adev->audio_route); |
| 516 | |
| 517 | usecase->in_snd_device = in_snd_device; |
| 518 | usecase->out_snd_device = out_snd_device; |
| 519 | |
| 520 | enable_audio_route(adev, usecase, true); |
| 521 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 522 | return status; |
| 523 | } |
| 524 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 525 | static int stop_input_stream(struct stream_in *in) |
| 526 | { |
| 527 | int i, ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 528 | struct audio_usecase *uc_info; |
| 529 | struct audio_device *adev = in->dev; |
| 530 | |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 531 | adev->active_input = NULL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 532 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 533 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 534 | in->usecase, use_case_table[in->usecase]); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 535 | uc_info = get_usecase_from_list(adev, in->usecase); |
| 536 | if (uc_info == NULL) { |
| 537 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 538 | __func__, in->usecase); |
| 539 | return -EINVAL; |
| 540 | } |
| 541 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 542 | /* 1. Disable stream specific mixer controls */ |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 543 | disable_audio_route(adev, uc_info, true); |
| 544 | |
| 545 | /* 2. Disable the tx device */ |
| 546 | disable_snd_device(adev, uc_info->in_snd_device, true); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 547 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 548 | list_remove(&uc_info->list); |
| 549 | free(uc_info); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 550 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 551 | ALOGV("%s: exit: status(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 552 | return ret; |
| 553 | } |
| 554 | |
| 555 | int start_input_stream(struct stream_in *in) |
| 556 | { |
| 557 | /* 1. Enable output device and stream routing controls */ |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 558 | int ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 559 | struct audio_usecase *uc_info; |
| 560 | struct audio_device *adev = in->dev; |
| 561 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 562 | ALOGV("%s: enter: usecase(%d)", __func__, in->usecase); |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 563 | 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] | 564 | if (in->pcm_device_id < 0) { |
| 565 | ALOGE("%s: Could not find PCM device id for the usecase(%d)", |
| 566 | __func__, in->usecase); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 567 | ret = -EINVAL; |
| 568 | goto error_config; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 569 | } |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 570 | |
| 571 | adev->active_input = in; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 572 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 573 | uc_info->id = in->usecase; |
| 574 | uc_info->type = PCM_CAPTURE; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 575 | uc_info->stream.in = in; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 576 | uc_info->devices = in->device; |
| 577 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 578 | uc_info->out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 579 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 580 | list_add_tail(&adev->usecase_list, &uc_info->list); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 581 | select_devices(adev, in->usecase); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 582 | |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 583 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d", |
| 584 | __func__, SOUND_CARD, in->pcm_device_id, in->config.channels); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 585 | in->pcm = pcm_open(SOUND_CARD, in->pcm_device_id, |
| 586 | PCM_IN, &in->config); |
| 587 | if (in->pcm && !pcm_is_ready(in->pcm)) { |
| 588 | ALOGE("%s: %s", __func__, pcm_get_error(in->pcm)); |
| 589 | pcm_close(in->pcm); |
| 590 | in->pcm = NULL; |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 591 | ret = -EIO; |
| 592 | goto error_open; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 593 | } |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 594 | ALOGV("%s: exit", __func__); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 595 | return ret; |
| 596 | |
| 597 | error_open: |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 598 | stop_input_stream(in); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 599 | |
| 600 | error_config: |
| 601 | adev->active_input = NULL; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 602 | ALOGD("%s: exit: status(%d)", __func__, ret); |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 603 | |
| 604 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | static int stop_output_stream(struct stream_out *out) |
| 608 | { |
| 609 | int i, ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 610 | struct audio_usecase *uc_info; |
| 611 | struct audio_device *adev = out->dev; |
| 612 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 613 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 614 | out->usecase, use_case_table[out->usecase]); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 615 | uc_info = get_usecase_from_list(adev, out->usecase); |
| 616 | if (uc_info == NULL) { |
| 617 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 618 | __func__, out->usecase); |
| 619 | return -EINVAL; |
| 620 | } |
| 621 | |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 622 | /* 1. Get and set stream specific mixer controls */ |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 623 | disable_audio_route(adev, uc_info, true); |
| 624 | |
| 625 | /* 2. Disable the rx device */ |
| 626 | disable_snd_device(adev, uc_info->out_snd_device, true); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 627 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 628 | list_remove(&uc_info->list); |
| 629 | free(uc_info); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 630 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 631 | ALOGV("%s: exit: status(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 632 | return ret; |
| 633 | } |
| 634 | |
| 635 | int start_output_stream(struct stream_out *out) |
| 636 | { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 637 | int ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 638 | struct audio_usecase *uc_info; |
| 639 | struct audio_device *adev = out->dev; |
| 640 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 641 | ALOGV("%s: enter: usecase(%d: %s) devices(%#x)", |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 642 | __func__, out->usecase, use_case_table[out->usecase], out->devices); |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 643 | 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] | 644 | if (out->pcm_device_id < 0) { |
| 645 | ALOGE("%s: Invalid PCM device id(%d) for the usecase(%d)", |
| 646 | __func__, out->pcm_device_id, out->usecase); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 647 | ret = -EINVAL; |
| 648 | goto error_config; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 652 | uc_info->id = out->usecase; |
| 653 | uc_info->type = PCM_PLAYBACK; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 654 | uc_info->stream.out = out; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 655 | uc_info->devices = out->devices; |
| 656 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 657 | uc_info->out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 658 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 659 | list_add_tail(&adev->usecase_list, &uc_info->list); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 660 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 661 | select_devices(adev, out->usecase); |
| 662 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 663 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)", |
| 664 | __func__, 0, out->pcm_device_id); |
| 665 | out->pcm = pcm_open(SOUND_CARD, out->pcm_device_id, |
| 666 | PCM_OUT, &out->config); |
| 667 | if (out->pcm && !pcm_is_ready(out->pcm)) { |
| 668 | ALOGE("%s: %s", __func__, pcm_get_error(out->pcm)); |
| 669 | pcm_close(out->pcm); |
| 670 | out->pcm = NULL; |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 671 | ret = -EIO; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 672 | goto error_pcm_open; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 673 | } |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 674 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 675 | return 0; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 676 | error_pcm_open: |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 677 | stop_output_stream(out); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 678 | error_config: |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 679 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | static int stop_voice_call(struct audio_device *adev) |
| 683 | { |
| 684 | int i, ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 685 | struct audio_usecase *uc_info; |
| 686 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 687 | ALOGV("%s: enter", __func__); |
Ravi Kumar Alamanda | 8b9c5c8 | 2013-02-20 16:59:34 -0800 | [diff] [blame] | 688 | adev->in_call = false; |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 689 | |
| 690 | ret = platform_stop_voice_call(adev->platform); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 691 | |
| 692 | /* 1. Close the PCM devices */ |
| 693 | if (adev->voice_call_rx) { |
| 694 | pcm_close(adev->voice_call_rx); |
| 695 | adev->voice_call_rx = NULL; |
| 696 | } |
| 697 | if (adev->voice_call_tx) { |
| 698 | pcm_close(adev->voice_call_tx); |
| 699 | adev->voice_call_tx = NULL; |
| 700 | } |
| 701 | |
| 702 | uc_info = get_usecase_from_list(adev, USECASE_VOICE_CALL); |
| 703 | if (uc_info == NULL) { |
| 704 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 705 | __func__, USECASE_VOICE_CALL); |
| 706 | return -EINVAL; |
| 707 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 708 | |
| 709 | /* 2. Get and set stream specific mixer controls */ |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 710 | disable_audio_route(adev, uc_info, true); |
| 711 | |
| 712 | /* 3. Disable the rx and tx devices */ |
| 713 | disable_snd_device(adev, uc_info->out_snd_device, false); |
| 714 | disable_snd_device(adev, uc_info->in_snd_device, true); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 715 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 716 | list_remove(&uc_info->list); |
| 717 | free(uc_info); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 718 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 719 | ALOGV("%s: exit: status(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 720 | return ret; |
| 721 | } |
| 722 | |
| 723 | static int start_voice_call(struct audio_device *adev) |
| 724 | { |
| 725 | int i, ret = 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 726 | struct audio_usecase *uc_info; |
| 727 | int pcm_dev_rx_id, pcm_dev_tx_id; |
| 728 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 729 | ALOGV("%s: enter", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 730 | |
| 731 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 732 | uc_info->id = USECASE_VOICE_CALL; |
| 733 | uc_info->type = VOICE_CALL; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 734 | uc_info->stream.out = adev->primary_output; |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 735 | uc_info->devices = adev->primary_output->devices; |
| 736 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 737 | uc_info->out_snd_device = SND_DEVICE_NONE; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 738 | |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 739 | list_add_tail(&adev->usecase_list, &uc_info->list); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 740 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 741 | select_devices(adev, USECASE_VOICE_CALL); |
| 742 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 743 | pcm_dev_rx_id = platform_get_pcm_device_id(uc_info->id, PCM_PLAYBACK); |
| 744 | 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] | 745 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 746 | if (pcm_dev_rx_id < 0 || pcm_dev_tx_id < 0) { |
| 747 | ALOGE("%s: Invalid PCM devices (rx: %d tx: %d) for the usecase(%d)", |
| 748 | __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] | 749 | ret = -EIO; |
| 750 | goto error_start_voice; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 751 | } |
| 752 | |
Ravi Kumar Alamanda | 8b9c5c8 | 2013-02-20 16:59:34 -0800 | [diff] [blame] | 753 | 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] | 754 | __func__, SOUND_CARD, pcm_dev_rx_id); |
| 755 | adev->voice_call_rx = pcm_open(SOUND_CARD, |
| 756 | pcm_dev_rx_id, |
| 757 | PCM_OUT, &pcm_config_voice_call); |
| 758 | if (adev->voice_call_rx && !pcm_is_ready(adev->voice_call_rx)) { |
| 759 | ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_rx)); |
Ravi Kumar Alamanda | 8b9c5c8 | 2013-02-20 16:59:34 -0800 | [diff] [blame] | 760 | ret = -EIO; |
| 761 | goto error_start_voice; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 762 | } |
| 763 | |
Ravi Kumar Alamanda | 8b9c5c8 | 2013-02-20 16:59:34 -0800 | [diff] [blame] | 764 | 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] | 765 | __func__, SOUND_CARD, pcm_dev_tx_id); |
| 766 | adev->voice_call_tx = pcm_open(SOUND_CARD, |
| 767 | pcm_dev_tx_id, |
| 768 | PCM_IN, &pcm_config_voice_call); |
| 769 | if (adev->voice_call_tx && !pcm_is_ready(adev->voice_call_tx)) { |
| 770 | ALOGE("%s: %s", __func__, pcm_get_error(adev->voice_call_tx)); |
Ravi Kumar Alamanda | 8b9c5c8 | 2013-02-20 16:59:34 -0800 | [diff] [blame] | 771 | ret = -EIO; |
| 772 | goto error_start_voice; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 773 | } |
| 774 | pcm_start(adev->voice_call_rx); |
| 775 | pcm_start(adev->voice_call_tx); |
| 776 | |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 777 | ret = platform_start_voice_call(adev->platform); |
| 778 | if (ret < 0) { |
| 779 | ALOGE("%s: platform_start_voice_call error %d\n", __func__, ret); |
| 780 | goto error_start_voice; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | adev->in_call = true; |
Ravi Kumar Alamanda | 8b9c5c8 | 2013-02-20 16:59:34 -0800 | [diff] [blame] | 784 | return 0; |
| 785 | |
| 786 | error_start_voice: |
| 787 | stop_voice_call(adev); |
| 788 | |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 789 | ALOGD("%s: exit: status(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 790 | return ret; |
| 791 | } |
| 792 | |
| 793 | static int check_input_parameters(uint32_t sample_rate, |
| 794 | audio_format_t format, |
| 795 | int channel_count) |
| 796 | { |
| 797 | if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL; |
| 798 | |
| 799 | if ((channel_count < 1) || (channel_count > 2)) return -EINVAL; |
| 800 | |
| 801 | switch (sample_rate) { |
| 802 | case 8000: |
| 803 | case 11025: |
| 804 | case 12000: |
| 805 | case 16000: |
| 806 | case 22050: |
| 807 | case 24000: |
| 808 | case 32000: |
| 809 | case 44100: |
| 810 | case 48000: |
| 811 | break; |
| 812 | default: |
| 813 | return -EINVAL; |
| 814 | } |
| 815 | |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | static size_t get_input_buffer_size(uint32_t sample_rate, |
| 820 | audio_format_t format, |
| 821 | int channel_count) |
| 822 | { |
| 823 | size_t size = 0; |
| 824 | |
Ravi Kumar Alamanda | 33d3306 | 2013-06-11 14:40:01 -0700 | [diff] [blame] | 825 | if (check_input_parameters(sample_rate, format, channel_count) != 0) |
| 826 | return 0; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 827 | |
Ravi Kumar Alamanda | 33d3306 | 2013-06-11 14:40:01 -0700 | [diff] [blame] | 828 | size = (sample_rate * AUDIO_CAPTURE_PERIOD_DURATION_MSEC) / 1000; |
| 829 | /* ToDo: should use frame_size computed based on the format and |
| 830 | channel_count here. */ |
| 831 | size *= sizeof(short) * channel_count; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 832 | |
Ravi Kumar Alamanda | 33d3306 | 2013-06-11 14:40:01 -0700 | [diff] [blame] | 833 | /* make sure the size is multiple of 64 */ |
| 834 | size += 0x3f; |
| 835 | size &= ~0x3f; |
| 836 | |
| 837 | return size; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 841 | { |
| 842 | struct stream_out *out = (struct stream_out *)stream; |
| 843 | |
| 844 | return out->config.rate; |
| 845 | } |
| 846 | |
| 847 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 848 | { |
| 849 | return -ENOSYS; |
| 850 | } |
| 851 | |
| 852 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 853 | { |
| 854 | struct stream_out *out = (struct stream_out *)stream; |
| 855 | |
| 856 | return out->config.period_size * audio_stream_frame_size(stream); |
| 857 | } |
| 858 | |
| 859 | static uint32_t out_get_channels(const struct audio_stream *stream) |
| 860 | { |
| 861 | struct stream_out *out = (struct stream_out *)stream; |
| 862 | |
| 863 | return out->channel_mask; |
| 864 | } |
| 865 | |
| 866 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 867 | { |
| 868 | return AUDIO_FORMAT_PCM_16_BIT; |
| 869 | } |
| 870 | |
| 871 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 872 | { |
| 873 | return -ENOSYS; |
| 874 | } |
| 875 | |
| 876 | static int out_standby(struct audio_stream *stream) |
| 877 | { |
| 878 | struct stream_out *out = (struct stream_out *)stream; |
| 879 | struct audio_device *adev = out->dev; |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 880 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 881 | out->usecase, use_case_table[out->usecase]); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 882 | pthread_mutex_lock(&out->lock); |
| 883 | |
| 884 | if (!out->standby) { |
| 885 | out->standby = true; |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 886 | if (out->pcm) { |
| 887 | pcm_close(out->pcm); |
| 888 | out->pcm = NULL; |
| 889 | } |
| 890 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 891 | stop_output_stream(out); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 892 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 893 | } |
| 894 | pthread_mutex_unlock(&out->lock); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 895 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | static int out_dump(const struct audio_stream *stream, int fd) |
| 900 | { |
| 901 | return 0; |
| 902 | } |
| 903 | |
| 904 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 905 | { |
| 906 | struct stream_out *out = (struct stream_out *)stream; |
| 907 | struct audio_device *adev = out->dev; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 908 | struct audio_usecase *usecase; |
| 909 | struct listnode *node; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 910 | struct str_parms *parms; |
| 911 | char value[32]; |
| 912 | int ret, val = 0; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 913 | bool select_new_device = false; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 914 | |
sangwoo | bc67724 | 2013-08-08 16:53:43 +0900 | [diff] [blame] | 915 | ALOGD("%s: enter: usecase(%d: %s) kvpairs: %s", |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 916 | __func__, out->usecase, use_case_table[out->usecase], kvpairs); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 917 | parms = str_parms_create_str(kvpairs); |
| 918 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
| 919 | if (ret >= 0) { |
| 920 | val = atoi(value); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 921 | pthread_mutex_lock(&out->lock); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 922 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 923 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 924 | /* |
| 925 | * When HDMI cable is unplugged the music playback is paused and |
| 926 | * the policy manager sends routing=0. But the audioflinger |
| 927 | * continues to write data until standby time (3sec). |
| 928 | * As the HDMI core is turned off, the write gets blocked. |
| 929 | * Avoid this by routing audio to speaker until standby. |
| 930 | */ |
| 931 | if (out->devices == AUDIO_DEVICE_OUT_AUX_DIGITAL && |
| 932 | val == AUDIO_DEVICE_NONE) { |
| 933 | val = AUDIO_DEVICE_OUT_SPEAKER; |
| 934 | } |
| 935 | |
| 936 | /* |
| 937 | * select_devices() call below switches all the usecases on the same |
| 938 | * backend to the new device. Refer to check_usecases_codec_backend() in |
| 939 | * the select_devices(). But how do we undo this? |
| 940 | * |
| 941 | * For example, music playback is active on headset (deep-buffer usecase) |
| 942 | * and if we go to ringtones and select a ringtone, low-latency usecase |
| 943 | * will be started on headset+speaker. As we can't enable headset+speaker |
| 944 | * and headset devices at the same time, select_devices() switches the music |
| 945 | * playback to headset+speaker while starting low-lateny usecase for ringtone. |
| 946 | * So when the ringtone playback is completed, how do we undo the same? |
| 947 | * |
| 948 | * We are relying on the out_set_parameters() call on deep-buffer output, |
| 949 | * once the ringtone playback is ended. |
| 950 | * NOTE: We should not check if the current devices are same as new devices. |
| 951 | * Because select_devices() must be called to switch back the music |
| 952 | * playback to headset. |
| 953 | */ |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 954 | if (val != 0) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 955 | out->devices = val; |
| 956 | |
| 957 | if (!out->standby) |
| 958 | select_devices(adev, out->usecase); |
| 959 | |
| 960 | if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call && |
| 961 | (out == adev->primary_output)) { |
| 962 | start_voice_call(adev); |
| 963 | } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call && |
| 964 | (out == adev->primary_output)) { |
| 965 | select_devices(adev, USECASE_VOICE_CALL); |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
Ravi Kumar Alamanda | 60caf20 | 2013-10-02 09:56:18 -0700 | [diff] [blame] | 969 | if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call && |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 970 | (out == adev->primary_output)) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 971 | stop_voice_call(adev); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 972 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 973 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 974 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 975 | pthread_mutex_unlock(&out->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 976 | } |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 977 | |
| 978 | if (out == adev->primary_output) { |
| 979 | pthread_mutex_lock(&adev->lock); |
| 980 | audio_extn_set_parameters(adev, parms); |
| 981 | pthread_mutex_unlock(&adev->lock); |
| 982 | } |
| 983 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 984 | str_parms_destroy(parms); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 985 | ALOGV("%s: exit: code(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 986 | return ret; |
| 987 | } |
| 988 | |
| 989 | static char* out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 990 | { |
| 991 | struct stream_out *out = (struct stream_out *)stream; |
| 992 | struct str_parms *query = str_parms_create_str(keys); |
| 993 | char *str; |
| 994 | char value[256]; |
| 995 | struct str_parms *reply = str_parms_create(); |
| 996 | size_t i, j; |
| 997 | int ret; |
| 998 | bool first = true; |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 999 | ALOGV("%s: enter: keys - %s", __func__, keys); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1000 | ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value)); |
| 1001 | if (ret >= 0) { |
| 1002 | value[0] = '\0'; |
| 1003 | i = 0; |
| 1004 | while (out->supported_channel_masks[i] != 0) { |
| 1005 | for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) { |
| 1006 | if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) { |
| 1007 | if (!first) { |
| 1008 | strcat(value, "|"); |
| 1009 | } |
| 1010 | strcat(value, out_channels_name_to_enum_table[j].name); |
| 1011 | first = false; |
| 1012 | break; |
| 1013 | } |
| 1014 | } |
| 1015 | i++; |
| 1016 | } |
| 1017 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); |
| 1018 | str = str_parms_to_str(reply); |
| 1019 | } else { |
| 1020 | str = strdup(keys); |
| 1021 | } |
| 1022 | str_parms_destroy(query); |
| 1023 | str_parms_destroy(reply); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1024 | ALOGV("%s: exit: returns - %s", __func__, str); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1025 | return str; |
| 1026 | } |
| 1027 | |
| 1028 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 1029 | { |
| 1030 | struct stream_out *out = (struct stream_out *)stream; |
| 1031 | |
| 1032 | return (out->config.period_count * out->config.period_size * 1000) / (out->config.rate); |
| 1033 | } |
| 1034 | |
| 1035 | static int out_set_volume(struct audio_stream_out *stream, float left, |
| 1036 | float right) |
| 1037 | { |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 1038 | struct stream_out *out = (struct stream_out *)stream; |
| 1039 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) { |
| 1040 | /* only take left channel into account: the API is for stereo anyway */ |
| 1041 | out->muted = (left == 0.0f); |
| 1042 | return 0; |
| 1043 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1044 | return -ENOSYS; |
| 1045 | } |
| 1046 | |
| 1047 | static ssize_t out_write(struct audio_stream_out *stream, const void *buffer, |
| 1048 | size_t bytes) |
| 1049 | { |
| 1050 | struct stream_out *out = (struct stream_out *)stream; |
| 1051 | struct audio_device *adev = out->dev; |
| 1052 | int i, ret = -1; |
| 1053 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1054 | pthread_mutex_lock(&out->lock); |
| 1055 | if (out->standby) { |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1056 | out->standby = false; |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1057 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1058 | ret = start_output_stream(out); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1059 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1060 | if (ret != 0) { |
Ravi Kumar Alamanda | 59d296d | 2013-05-02 11:25:27 -0700 | [diff] [blame] | 1061 | out->standby = true; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1062 | goto exit; |
| 1063 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1064 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1065 | |
| 1066 | if (out->pcm) { |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 1067 | if (out->muted) |
| 1068 | memset((void *)buffer, 0, bytes); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1069 | //ALOGV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes); |
| 1070 | ret = pcm_write(out->pcm, (void *)buffer, bytes); |
| 1071 | } |
| 1072 | |
| 1073 | exit: |
| 1074 | pthread_mutex_unlock(&out->lock); |
| 1075 | |
| 1076 | if (ret != 0) { |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1077 | if (out->pcm) |
| 1078 | 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] | 1079 | out_standby(&out->stream.common); |
| 1080 | usleep(bytes * 1000000 / audio_stream_frame_size(&out->stream.common) / |
| 1081 | out_get_sample_rate(&out->stream.common)); |
| 1082 | } |
| 1083 | return bytes; |
| 1084 | } |
| 1085 | |
| 1086 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 1087 | uint32_t *dsp_frames) |
| 1088 | { |
| 1089 | return -EINVAL; |
| 1090 | } |
| 1091 | |
| 1092 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1093 | { |
| 1094 | return 0; |
| 1095 | } |
| 1096 | |
| 1097 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1098 | { |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream, |
| 1103 | int64_t *timestamp) |
| 1104 | { |
| 1105 | return -EINVAL; |
| 1106 | } |
| 1107 | |
| 1108 | /** audio_stream_in implementation **/ |
| 1109 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 1110 | { |
| 1111 | struct stream_in *in = (struct stream_in *)stream; |
| 1112 | |
| 1113 | return in->config.rate; |
| 1114 | } |
| 1115 | |
| 1116 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 1117 | { |
| 1118 | return -ENOSYS; |
| 1119 | } |
| 1120 | |
| 1121 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 1122 | { |
| 1123 | struct stream_in *in = (struct stream_in *)stream; |
| 1124 | |
| 1125 | return in->config.period_size * audio_stream_frame_size(stream); |
| 1126 | } |
| 1127 | |
| 1128 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 1129 | { |
| 1130 | struct stream_in *in = (struct stream_in *)stream; |
| 1131 | |
| 1132 | return in->channel_mask; |
| 1133 | } |
| 1134 | |
| 1135 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 1136 | { |
| 1137 | return AUDIO_FORMAT_PCM_16_BIT; |
| 1138 | } |
| 1139 | |
| 1140 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 1141 | { |
| 1142 | return -ENOSYS; |
| 1143 | } |
| 1144 | |
| 1145 | static int in_standby(struct audio_stream *stream) |
| 1146 | { |
| 1147 | struct stream_in *in = (struct stream_in *)stream; |
| 1148 | struct audio_device *adev = in->dev; |
| 1149 | int status = 0; |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1150 | ALOGV("%s: enter", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1151 | pthread_mutex_lock(&in->lock); |
| 1152 | if (!in->standby) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1153 | in->standby = true; |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1154 | if (in->pcm) { |
| 1155 | pcm_close(in->pcm); |
| 1156 | in->pcm = NULL; |
| 1157 | } |
| 1158 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1159 | status = stop_input_stream(in); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1160 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1161 | } |
| 1162 | pthread_mutex_unlock(&in->lock); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1163 | ALOGV("%s: exit: status(%d)", __func__, status); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1164 | return status; |
| 1165 | } |
| 1166 | |
| 1167 | static int in_dump(const struct audio_stream *stream, int fd) |
| 1168 | { |
| 1169 | return 0; |
| 1170 | } |
| 1171 | |
| 1172 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 1173 | { |
| 1174 | struct stream_in *in = (struct stream_in *)stream; |
| 1175 | struct audio_device *adev = in->dev; |
| 1176 | struct str_parms *parms; |
| 1177 | char *str; |
| 1178 | char value[32]; |
| 1179 | int ret, val = 0; |
| 1180 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1181 | ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1182 | parms = str_parms_create_str(kvpairs); |
| 1183 | |
| 1184 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); |
| 1185 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1186 | pthread_mutex_lock(&in->lock); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1187 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1188 | if (ret >= 0) { |
| 1189 | val = atoi(value); |
| 1190 | /* no audio source uses val == 0 */ |
| 1191 | if ((in->source != val) && (val != 0)) { |
| 1192 | in->source = val; |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
| 1197 | if (ret >= 0) { |
| 1198 | val = atoi(value); |
| 1199 | if ((in->device != val) && (val != 0)) { |
| 1200 | in->device = val; |
| 1201 | /* 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] | 1202 | if (!in->standby) |
| 1203 | ret = select_devices(adev, in->usecase); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1204 | } |
| 1205 | } |
| 1206 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1207 | pthread_mutex_unlock(&adev->lock); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1208 | pthread_mutex_unlock(&in->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1209 | |
| 1210 | str_parms_destroy(parms); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1211 | ALOGV("%s: exit: status(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1212 | return ret; |
| 1213 | } |
| 1214 | |
| 1215 | static char* in_get_parameters(const struct audio_stream *stream, |
| 1216 | const char *keys) |
| 1217 | { |
| 1218 | return strdup(""); |
| 1219 | } |
| 1220 | |
| 1221 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 1222 | { |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
| 1226 | static ssize_t in_read(struct audio_stream_in *stream, void *buffer, |
| 1227 | size_t bytes) |
| 1228 | { |
| 1229 | struct stream_in *in = (struct stream_in *)stream; |
| 1230 | struct audio_device *adev = in->dev; |
| 1231 | int i, ret = -1; |
| 1232 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1233 | pthread_mutex_lock(&in->lock); |
| 1234 | if (in->standby) { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1235 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1236 | ret = start_input_stream(in); |
Eric Laurent | 150dbfe | 2013-02-27 14:31:02 -0800 | [diff] [blame] | 1237 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1238 | if (ret != 0) { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1239 | goto exit; |
| 1240 | } |
| 1241 | in->standby = 0; |
| 1242 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1243 | |
| 1244 | if (in->pcm) { |
| 1245 | ret = pcm_read(in->pcm, buffer, bytes); |
| 1246 | } |
| 1247 | |
| 1248 | /* |
| 1249 | * Instead of writing zeroes here, we could trust the hardware |
| 1250 | * to always provide zeroes when muted. |
| 1251 | */ |
| 1252 | if (ret == 0 && adev->mic_mute) |
| 1253 | memset(buffer, 0, bytes); |
| 1254 | |
| 1255 | exit: |
| 1256 | pthread_mutex_unlock(&in->lock); |
| 1257 | |
| 1258 | if (ret != 0) { |
| 1259 | in_standby(&in->stream.common); |
| 1260 | ALOGV("%s: read failed - sleeping for buffer duration", __func__); |
| 1261 | usleep(bytes * 1000000 / audio_stream_frame_size(&in->stream.common) / |
| 1262 | in_get_sample_rate(&in->stream.common)); |
| 1263 | } |
| 1264 | return bytes; |
| 1265 | } |
| 1266 | |
| 1267 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 1268 | { |
| 1269 | return 0; |
| 1270 | } |
| 1271 | |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 1272 | static int add_remove_audio_effect(const struct audio_stream *stream, |
| 1273 | effect_handle_t effect, |
| 1274 | bool enable) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1275 | { |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 1276 | struct stream_in *in = (struct stream_in *)stream; |
| 1277 | int status = 0; |
| 1278 | effect_descriptor_t desc; |
| 1279 | |
| 1280 | status = (*effect)->get_descriptor(effect, &desc); |
| 1281 | if (status != 0) |
| 1282 | return status; |
| 1283 | |
| 1284 | pthread_mutex_lock(&in->lock); |
| 1285 | pthread_mutex_lock(&in->dev->lock); |
| 1286 | if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) && |
| 1287 | in->enable_aec != enable && |
| 1288 | (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) { |
| 1289 | in->enable_aec = enable; |
| 1290 | if (!in->standby) |
| 1291 | select_devices(in->dev, in->usecase); |
| 1292 | } |
| 1293 | pthread_mutex_unlock(&in->dev->lock); |
| 1294 | pthread_mutex_unlock(&in->lock); |
| 1295 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1296 | return 0; |
| 1297 | } |
| 1298 | |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 1299 | static int in_add_audio_effect(const struct audio_stream *stream, |
| 1300 | effect_handle_t effect) |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1301 | { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1302 | ALOGV("%s: effect %p", __func__, effect); |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 1303 | return add_remove_audio_effect(stream, effect, true); |
| 1304 | } |
| 1305 | |
| 1306 | static int in_remove_audio_effect(const struct audio_stream *stream, |
| 1307 | effect_handle_t effect) |
| 1308 | { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1309 | ALOGV("%s: effect %p", __func__, effect); |
Ravi Kumar Alamanda | f70ffb4 | 2013-04-16 15:55:53 -0700 | [diff] [blame] | 1310 | return add_remove_audio_effect(stream, effect, false); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | static int adev_open_output_stream(struct audio_hw_device *dev, |
| 1314 | audio_io_handle_t handle, |
| 1315 | audio_devices_t devices, |
| 1316 | audio_output_flags_t flags, |
| 1317 | struct audio_config *config, |
| 1318 | struct audio_stream_out **stream_out) |
| 1319 | { |
| 1320 | struct audio_device *adev = (struct audio_device *)dev; |
| 1321 | struct stream_out *out; |
| 1322 | int i, ret; |
| 1323 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1324 | 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] | 1325 | __func__, config->sample_rate, config->channel_mask, devices, flags); |
| 1326 | *stream_out = NULL; |
| 1327 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
| 1328 | |
| 1329 | if (devices == AUDIO_DEVICE_NONE) |
| 1330 | devices = AUDIO_DEVICE_OUT_SPEAKER; |
| 1331 | |
| 1332 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO; |
| 1333 | out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1334 | out->flags = flags; |
| 1335 | out->devices = devices; |
Haynes Mathew George | 47cd4cb | 2013-07-19 11:58:50 -0700 | [diff] [blame] | 1336 | out->dev = adev; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1337 | |
| 1338 | /* Init use case and pcm_config */ |
| 1339 | if (out->flags & AUDIO_OUTPUT_FLAG_DIRECT && |
| 1340 | out->devices & AUDIO_DEVICE_OUT_AUX_DIGITAL) { |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1341 | pthread_mutex_lock(&adev->lock); |
| 1342 | ret = read_hdmi_channel_masks(out); |
| 1343 | pthread_mutex_unlock(&adev->lock); |
| 1344 | if (ret != 0) { |
| 1345 | /* If HDMI does not support multi channel playback, set the default */ |
| 1346 | out->config.channels = popcount(out->channel_mask); |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1347 | platform_set_hdmi_channels(adev->platform, out->config.channels); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1348 | goto error_open; |
| 1349 | } |
| 1350 | |
| 1351 | if (config->sample_rate == 0) |
| 1352 | config->sample_rate = DEFAULT_OUTPUT_SAMPLING_RATE; |
| 1353 | if (config->channel_mask == 0) |
| 1354 | config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1; |
| 1355 | |
| 1356 | out->channel_mask = config->channel_mask; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1357 | out->usecase = USECASE_AUDIO_PLAYBACK_MULTI_CH; |
| 1358 | out->config = pcm_config_hdmi_multi; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1359 | out->config.rate = config->sample_rate; |
| 1360 | out->config.channels = popcount(out->channel_mask); |
| 1361 | out->config.period_size = HDMI_MULTI_PERIOD_BYTES / (out->config.channels * 2); |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1362 | platform_set_hdmi_channels(adev->platform, out->config.channels); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1363 | } else if (out->flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { |
| 1364 | out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER; |
| 1365 | out->config = pcm_config_deep_buffer; |
| 1366 | } else { |
| 1367 | out->usecase = USECASE_AUDIO_PLAYBACK_LOW_LATENCY; |
| 1368 | out->config = pcm_config_low_latency; |
| 1369 | } |
| 1370 | |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1371 | if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 1372 | if(adev->primary_output == NULL) |
| 1373 | adev->primary_output = out; |
| 1374 | else { |
| 1375 | ALOGE("%s: Primary output is already opened", __func__); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1376 | ret = -EEXIST; |
| 1377 | goto error_open; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1378 | } |
| 1379 | } |
| 1380 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1381 | /* Check if this usecase is already existing */ |
| 1382 | pthread_mutex_lock(&adev->lock); |
| 1383 | if (get_usecase_from_list(adev, out->usecase) != NULL) { |
| 1384 | ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1385 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1386 | ret = -EEXIST; |
| 1387 | goto error_open; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1388 | } |
| 1389 | pthread_mutex_unlock(&adev->lock); |
| 1390 | |
| 1391 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 1392 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 1393 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 1394 | out->stream.common.get_channels = out_get_channels; |
| 1395 | out->stream.common.get_format = out_get_format; |
| 1396 | out->stream.common.set_format = out_set_format; |
| 1397 | out->stream.common.standby = out_standby; |
| 1398 | out->stream.common.dump = out_dump; |
| 1399 | out->stream.common.set_parameters = out_set_parameters; |
| 1400 | out->stream.common.get_parameters = out_get_parameters; |
| 1401 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 1402 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 1403 | out->stream.get_latency = out_get_latency; |
| 1404 | out->stream.set_volume = out_set_volume; |
| 1405 | out->stream.write = out_write; |
| 1406 | out->stream.get_render_position = out_get_render_position; |
| 1407 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
| 1408 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1409 | out->standby = 1; |
Eric Laurent | a9024de | 2013-04-04 09:19:12 -0700 | [diff] [blame] | 1410 | /* out->muted = false; by calloc() */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1411 | |
| 1412 | config->format = out->stream.common.get_format(&out->stream.common); |
| 1413 | config->channel_mask = out->stream.common.get_channels(&out->stream.common); |
| 1414 | config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common); |
| 1415 | |
| 1416 | *stream_out = &out->stream; |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1417 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1418 | return 0; |
Ravi Kumar Alamanda | b199506 | 2013-03-21 23:18:20 -0700 | [diff] [blame] | 1419 | |
| 1420 | error_open: |
| 1421 | free(out); |
| 1422 | *stream_out = NULL; |
| 1423 | ALOGD("%s: exit: ret %d", __func__, ret); |
| 1424 | return ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | static void adev_close_output_stream(struct audio_hw_device *dev, |
| 1428 | struct audio_stream_out *stream) |
| 1429 | { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1430 | ALOGV("%s: enter", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1431 | out_standby(&stream->common); |
| 1432 | free(stream); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1433 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 1437 | { |
| 1438 | struct audio_device *adev = (struct audio_device *)dev; |
| 1439 | struct str_parms *parms; |
| 1440 | char *str; |
| 1441 | char value[32]; |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 1442 | int val; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1443 | int ret; |
| 1444 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1445 | ALOGV("%s: enter: %s", __func__, kvpairs); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1446 | |
| 1447 | parms = str_parms_create_str(kvpairs); |
| 1448 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value)); |
| 1449 | if (ret >= 0) { |
| 1450 | int tty_mode; |
| 1451 | |
| 1452 | if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0) |
| 1453 | tty_mode = TTY_MODE_OFF; |
| 1454 | else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0) |
| 1455 | tty_mode = TTY_MODE_VCO; |
| 1456 | else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0) |
| 1457 | tty_mode = TTY_MODE_HCO; |
| 1458 | else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0) |
| 1459 | tty_mode = TTY_MODE_FULL; |
| 1460 | else |
| 1461 | return -EINVAL; |
| 1462 | |
| 1463 | pthread_mutex_lock(&adev->lock); |
| 1464 | if (tty_mode != adev->tty_mode) { |
| 1465 | adev->tty_mode = tty_mode; |
Ravi Kumar Alamanda | f996704 | 2013-02-14 19:35:14 -0800 | [diff] [blame] | 1466 | adev->acdb_settings = (adev->acdb_settings & TTY_MODE_CLEAR) | tty_mode; |
| 1467 | if (adev->in_call) |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1468 | select_devices(adev, USECASE_VOICE_CALL); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1469 | } |
| 1470 | pthread_mutex_unlock(&adev->lock); |
| 1471 | } |
| 1472 | |
| 1473 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); |
| 1474 | if (ret >= 0) { |
| 1475 | /* When set to false, HAL should disable EC and NS |
| 1476 | * But it is currently not supported. |
| 1477 | */ |
| 1478 | if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| 1479 | adev->bluetooth_nrec = true; |
| 1480 | else |
| 1481 | adev->bluetooth_nrec = false; |
| 1482 | } |
| 1483 | |
| 1484 | ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); |
| 1485 | if (ret >= 0) { |
| 1486 | if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| 1487 | adev->screen_off = false; |
| 1488 | else |
| 1489 | adev->screen_off = true; |
| 1490 | } |
| 1491 | |
Jean-Michel Trivi | c56336b | 2013-05-24 16:55:17 -0700 | [diff] [blame] | 1492 | ret = str_parms_get_int(parms, "rotation", &val); |
| 1493 | if (ret >= 0) { |
| 1494 | bool reverse_speakers = false; |
| 1495 | switch(val) { |
| 1496 | // FIXME: note that the code below assumes that the speakers are in the correct placement |
| 1497 | // relative to the user when the device is rotated 90deg from its default rotation. This |
| 1498 | // assumption is device-specific, not platform-specific like this code. |
| 1499 | case 270: |
| 1500 | reverse_speakers = true; |
| 1501 | break; |
| 1502 | case 0: |
| 1503 | case 90: |
| 1504 | case 180: |
| 1505 | break; |
| 1506 | default: |
| 1507 | ALOGE("%s: unexpected rotation of %d", __func__, val); |
| 1508 | } |
| 1509 | pthread_mutex_lock(&adev->lock); |
| 1510 | if (adev->speaker_lr_swap != reverse_speakers) { |
| 1511 | adev->speaker_lr_swap = reverse_speakers; |
| 1512 | // only update the selected device if there is active pcm playback |
| 1513 | struct audio_usecase *usecase; |
| 1514 | struct listnode *node; |
| 1515 | list_for_each(node, &adev->usecase_list) { |
| 1516 | usecase = node_to_item(node, struct audio_usecase, list); |
| 1517 | if (usecase->type == PCM_PLAYBACK) { |
| 1518 | select_devices(adev, usecase->id); |
| 1519 | break; |
| 1520 | } |
| 1521 | } |
| 1522 | } |
| 1523 | pthread_mutex_unlock(&adev->lock); |
| 1524 | } |
| 1525 | |
Apoorv Raghuvanshi | 9eaf94e | 2013-10-04 16:13:44 -0700 | [diff] [blame] | 1526 | audio_extn_set_parameters(adev, parms); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1527 | str_parms_destroy(parms); |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1528 | ALOGV("%s: exit with code(%d)", __func__, ret); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1529 | return ret; |
| 1530 | } |
| 1531 | |
| 1532 | static char* adev_get_parameters(const struct audio_hw_device *dev, |
| 1533 | const char *keys) |
| 1534 | { |
Apoorv Raghuvanshi | 9eaf94e | 2013-10-04 16:13:44 -0700 | [diff] [blame] | 1535 | return audio_extn_get_parameters(dev, keys); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | static int adev_init_check(const struct audio_hw_device *dev) |
| 1539 | { |
| 1540 | return 0; |
| 1541 | } |
| 1542 | |
| 1543 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 1544 | { |
| 1545 | struct audio_device *adev = (struct audio_device *)dev; |
| 1546 | int vol, err = 0; |
| 1547 | |
| 1548 | pthread_mutex_lock(&adev->lock); |
| 1549 | adev->voice_volume = volume; |
| 1550 | if (adev->mode == AUDIO_MODE_IN_CALL) { |
| 1551 | if (volume < 0.0) { |
| 1552 | volume = 0.0; |
| 1553 | } else if (volume > 1.0) { |
| 1554 | volume = 1.0; |
| 1555 | } |
| 1556 | |
| 1557 | vol = lrint(volume * 100.0); |
| 1558 | |
| 1559 | // Voice volume levels from android are mapped to driver volume levels as follows. |
| 1560 | // 0 -> 5, 20 -> 4, 40 ->3, 60 -> 2, 80 -> 1, 100 -> 0 |
| 1561 | // So adjust the volume to get the correct volume index in driver |
| 1562 | vol = 100 - vol; |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1563 | |
| 1564 | err = platform_set_voice_volume(adev->platform, vol); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1565 | } |
| 1566 | pthread_mutex_unlock(&adev->lock); |
| 1567 | return err; |
| 1568 | } |
| 1569 | |
| 1570 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 1571 | { |
| 1572 | return -ENOSYS; |
| 1573 | } |
| 1574 | |
| 1575 | static int adev_get_master_volume(struct audio_hw_device *dev, |
| 1576 | float *volume) |
| 1577 | { |
| 1578 | return -ENOSYS; |
| 1579 | } |
| 1580 | |
| 1581 | static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) |
| 1582 | { |
| 1583 | return -ENOSYS; |
| 1584 | } |
| 1585 | |
| 1586 | static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) |
| 1587 | { |
| 1588 | return -ENOSYS; |
| 1589 | } |
| 1590 | |
| 1591 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 1592 | { |
| 1593 | struct audio_device *adev = (struct audio_device *)dev; |
| 1594 | |
| 1595 | pthread_mutex_lock(&adev->lock); |
| 1596 | if (adev->mode != mode) { |
| 1597 | adev->mode = mode; |
| 1598 | } |
| 1599 | pthread_mutex_unlock(&adev->lock); |
| 1600 | return 0; |
| 1601 | } |
| 1602 | |
| 1603 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 1604 | { |
| 1605 | struct audio_device *adev = (struct audio_device *)dev; |
| 1606 | int err = 0; |
| 1607 | |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1608 | pthread_mutex_lock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1609 | adev->mic_mute = state; |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1610 | |
| 1611 | err = platform_set_mic_mute(adev->platform, state); |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1612 | pthread_mutex_unlock(&adev->lock); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1613 | return err; |
| 1614 | } |
| 1615 | |
| 1616 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 1617 | { |
| 1618 | struct audio_device *adev = (struct audio_device *)dev; |
| 1619 | |
| 1620 | *state = adev->mic_mute; |
| 1621 | |
| 1622 | return 0; |
| 1623 | } |
| 1624 | |
| 1625 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, |
| 1626 | const struct audio_config *config) |
| 1627 | { |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1628 | int channel_count = popcount(config->channel_mask); |
| 1629 | |
| 1630 | return get_input_buffer_size(config->sample_rate, config->format, channel_count); |
| 1631 | } |
| 1632 | |
| 1633 | static int adev_open_input_stream(struct audio_hw_device *dev, |
| 1634 | audio_io_handle_t handle, |
| 1635 | audio_devices_t devices, |
| 1636 | struct audio_config *config, |
| 1637 | struct audio_stream_in **stream_in) |
| 1638 | { |
| 1639 | struct audio_device *adev = (struct audio_device *)dev; |
| 1640 | struct stream_in *in; |
| 1641 | int ret, buffer_size, frame_size; |
| 1642 | int channel_count = popcount(config->channel_mask); |
| 1643 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1644 | ALOGV("%s: enter", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1645 | *stream_in = NULL; |
| 1646 | if (check_input_parameters(config->sample_rate, config->format, channel_count) != 0) |
| 1647 | return -EINVAL; |
| 1648 | |
| 1649 | in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); |
| 1650 | |
| 1651 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 1652 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 1653 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 1654 | in->stream.common.get_channels = in_get_channels; |
| 1655 | in->stream.common.get_format = in_get_format; |
| 1656 | in->stream.common.set_format = in_set_format; |
| 1657 | in->stream.common.standby = in_standby; |
| 1658 | in->stream.common.dump = in_dump; |
| 1659 | in->stream.common.set_parameters = in_set_parameters; |
| 1660 | in->stream.common.get_parameters = in_get_parameters; |
| 1661 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 1662 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 1663 | in->stream.set_gain = in_set_gain; |
| 1664 | in->stream.read = in_read; |
| 1665 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| 1666 | |
| 1667 | in->device = devices; |
| 1668 | in->source = AUDIO_SOURCE_DEFAULT; |
| 1669 | in->dev = adev; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1670 | in->standby = 1; |
| 1671 | in->channel_mask = config->channel_mask; |
| 1672 | |
| 1673 | /* Update config params with the requested sample rate and channels */ |
| 1674 | in->usecase = USECASE_AUDIO_RECORD; |
| 1675 | in->config = pcm_config_audio_capture; |
| 1676 | in->config.channels = channel_count; |
| 1677 | in->config.rate = config->sample_rate; |
| 1678 | |
| 1679 | frame_size = audio_stream_frame_size((struct audio_stream *)in); |
| 1680 | buffer_size = get_input_buffer_size(config->sample_rate, |
| 1681 | config->format, |
| 1682 | channel_count); |
| 1683 | in->config.period_size = buffer_size / frame_size; |
| 1684 | |
| 1685 | *stream_in = &in->stream; |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1686 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1687 | return 0; |
| 1688 | |
| 1689 | err_open: |
| 1690 | free(in); |
| 1691 | *stream_in = NULL; |
| 1692 | return ret; |
| 1693 | } |
| 1694 | |
| 1695 | static void adev_close_input_stream(struct audio_hw_device *dev, |
| 1696 | struct audio_stream_in *stream) |
| 1697 | { |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1698 | ALOGV("%s", __func__); |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 1699 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1700 | in_standby(&stream->common); |
| 1701 | free(stream); |
| 1702 | |
| 1703 | return; |
| 1704 | } |
| 1705 | |
| 1706 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 1707 | { |
| 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | static int adev_close(hw_device_t *device) |
| 1712 | { |
| 1713 | struct audio_device *adev = (struct audio_device *)device; |
| 1714 | audio_route_free(adev->audio_route); |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1715 | free(adev->snd_dev_ref_cnt); |
| 1716 | platform_deinit(adev->platform); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1717 | free(device); |
| 1718 | return 0; |
| 1719 | } |
| 1720 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1721 | static int adev_open(const hw_module_t *module, const char *name, |
| 1722 | hw_device_t **device) |
| 1723 | { |
Ravi Kumar Alamanda | 71c84b7 | 2013-03-10 23:50:28 -0700 | [diff] [blame] | 1724 | int i, ret; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1725 | |
Ravi Kumar Alamanda | 75d924d | 2013-02-20 21:30:08 -0800 | [diff] [blame] | 1726 | ALOGD("%s: enter", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1727 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; |
| 1728 | |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 1729 | pthread_mutex_lock(&adev_init_lock); |
| 1730 | if (is_adev_initialised == true){ |
| 1731 | *device = &adev->device.common; |
| 1732 | ALOGD("%s: returning existing instance of adev", __func__); |
| 1733 | ALOGD("%s: exit", __func__); |
| 1734 | pthread_mutex_unlock(&adev_init_lock); |
| 1735 | return 0; |
| 1736 | } |
| 1737 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1738 | adev = calloc(1, sizeof(struct audio_device)); |
| 1739 | |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1740 | adev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 1741 | adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
| 1742 | adev->device.common.module = (struct hw_module_t *)module; |
| 1743 | adev->device.common.close = adev_close; |
| 1744 | |
| 1745 | adev->device.init_check = adev_init_check; |
| 1746 | adev->device.set_voice_volume = adev_set_voice_volume; |
| 1747 | adev->device.set_master_volume = adev_set_master_volume; |
| 1748 | adev->device.get_master_volume = adev_get_master_volume; |
| 1749 | adev->device.set_master_mute = adev_set_master_mute; |
| 1750 | adev->device.get_master_mute = adev_get_master_mute; |
| 1751 | adev->device.set_mode = adev_set_mode; |
| 1752 | adev->device.set_mic_mute = adev_set_mic_mute; |
| 1753 | adev->device.get_mic_mute = adev_get_mic_mute; |
| 1754 | adev->device.set_parameters = adev_set_parameters; |
| 1755 | adev->device.get_parameters = adev_get_parameters; |
| 1756 | adev->device.get_input_buffer_size = adev_get_input_buffer_size; |
| 1757 | adev->device.open_output_stream = adev_open_output_stream; |
| 1758 | adev->device.close_output_stream = adev_close_output_stream; |
| 1759 | adev->device.open_input_stream = adev_open_input_stream; |
| 1760 | adev->device.close_input_stream = adev_close_input_stream; |
| 1761 | adev->device.dump = adev_dump; |
| 1762 | |
| 1763 | /* Set the default route before the PCM stream is opened */ |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1764 | adev->mode = AUDIO_MODE_NORMAL; |
Eric Laurent | c840063 | 2013-02-14 19:04:54 -0800 | [diff] [blame] | 1765 | adev->active_input = NULL; |
Ravi Kumar Alamanda | 096c87f | 2013-02-28 20:54:57 -0800 | [diff] [blame] | 1766 | adev->primary_output = NULL; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1767 | adev->out_device = AUDIO_DEVICE_NONE; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1768 | adev->voice_call_rx = NULL; |
| 1769 | adev->voice_call_tx = NULL; |
| 1770 | adev->voice_volume = 1.0f; |
| 1771 | adev->tty_mode = TTY_MODE_OFF; |
| 1772 | adev->bluetooth_nrec = true; |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1773 | adev->in_call = false; |
Ravi Kumar Alamanda | f996704 | 2013-02-14 19:35:14 -0800 | [diff] [blame] | 1774 | adev->acdb_settings = TTY_MODE_OFF; |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1775 | adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int)); |
Ravi Kumar Alamanda | 3b1816c | 2013-02-27 23:01:21 -0800 | [diff] [blame] | 1776 | list_init(&adev->usecase_list); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1777 | |
| 1778 | /* Loads platform specific libraries dynamically */ |
Eric Laurent | b23d528 | 2013-05-14 15:27:20 -0700 | [diff] [blame] | 1779 | adev->platform = platform_init(adev); |
| 1780 | if (!adev->platform) { |
| 1781 | free(adev->snd_dev_ref_cnt); |
| 1782 | free(adev); |
| 1783 | ALOGE("%s: Failed to init platform data, aborting.", __func__); |
| 1784 | *device = NULL; |
| 1785 | return -EINVAL; |
| 1786 | } |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1787 | *device = &adev->device.common; |
| 1788 | |
Apoorv Raghuvanshi | 6e26284 | 2013-10-06 14:39:35 -0700 | [diff] [blame] | 1789 | /* update init flag*/ |
| 1790 | is_adev_initialised = true; |
| 1791 | pthread_mutex_unlock(&adev_init_lock); |
| 1792 | |
Eric Laurent | 994a693 | 2013-07-17 11:51:42 -0700 | [diff] [blame] | 1793 | ALOGV("%s: exit", __func__); |
Ravi Kumar Alamanda | 2dfba2b | 2013-01-17 16:50:22 -0800 | [diff] [blame] | 1794 | return 0; |
| 1795 | } |
| 1796 | |
| 1797 | static struct hw_module_methods_t hal_module_methods = { |
| 1798 | .open = adev_open, |
| 1799 | }; |
| 1800 | |
| 1801 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 1802 | .common = { |
| 1803 | .tag = HARDWARE_MODULE_TAG, |
| 1804 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 1805 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 1806 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 1807 | .name = "QCOM Audio HAL", |
| 1808 | .author = "Code Aurora Forum", |
| 1809 | .methods = &hal_module_methods, |
| 1810 | }, |
| 1811 | }; |