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