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