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