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