Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
Christopher N. Hesse | 2f6f858 | 2017-01-28 12:46:15 +0100 | [diff] [blame] | 3 | * Copyright (C) 2017 Christopher N. Hesse <raymanfx@gmail.com> |
Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "audio_hw_primary" |
| 19 | /*#define LOG_NDEBUG 0*/ |
| 20 | /*#define VERY_VERY_VERBOSE_LOGGING*/ |
| 21 | #ifdef VERY_VERY_VERBOSE_LOGGING |
| 22 | #define ALOGVV ALOGV |
| 23 | #else |
| 24 | #define ALOGVV(a...) do { } while(0) |
| 25 | #endif |
| 26 | |
| 27 | #define _GNU_SOURCE |
| 28 | #include <errno.h> |
| 29 | #include <pthread.h> |
| 30 | #include <stdint.h> |
| 31 | #include <sys/time.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <math.h> |
| 34 | #include <dlfcn.h> |
| 35 | #include <sys/resource.h> |
| 36 | #include <sys/prctl.h> |
| 37 | |
| 38 | #include <cutils/log.h> |
| 39 | #include <cutils/str_parms.h> |
| 40 | #include <cutils/atomic.h> |
| 41 | #include <cutils/sched_policy.h> |
| 42 | #include <cutils/properties.h> |
| 43 | |
| 44 | #include <hardware/audio_effect.h> |
| 45 | #include <system/thread_defs.h> |
| 46 | #include <audio_effects/effect_aec.h> |
| 47 | #include <audio_effects/effect_ns.h> |
| 48 | #include "audio_hw.h" |
| 49 | |
| 50 | #include "sound/compress_params.h" |
| 51 | |
| 52 | #define MIXER_CTL_COMPRESS_PLAYBACK_VOLUME "Compress Playback Volume" |
Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 53 | |
| 54 | /* TODO: the following PCM device profiles could be read from a config file */ |
| 55 | static struct pcm_device_profile pcm_device_playback = { |
| 56 | .config = { |
| 57 | .channels = PLAYBACK_DEFAULT_CHANNEL_COUNT, |
| 58 | .rate = PLAYBACK_DEFAULT_SAMPLING_RATE, |
| 59 | .period_size = PLAYBACK_PERIOD_SIZE, |
| 60 | .period_count = PLAYBACK_PERIOD_COUNT, |
| 61 | .format = PCM_FORMAT_S16_LE, |
| 62 | .start_threshold = PLAYBACK_START_THRESHOLD(PLAYBACK_PERIOD_SIZE, PLAYBACK_PERIOD_COUNT), |
| 63 | .stop_threshold = PLAYBACK_STOP_THRESHOLD(PLAYBACK_PERIOD_SIZE, PLAYBACK_PERIOD_COUNT), |
| 64 | .silence_threshold = 0, |
| 65 | .silence_size = UINT_MAX, |
| 66 | .avail_min = PLAYBACK_AVAILABLE_MIN, |
| 67 | }, |
| 68 | .card = SOUND_CARD, |
| 69 | .id = 9, |
| 70 | .type = PCM_PLAYBACK, |
| 71 | .devices = AUDIO_DEVICE_OUT_WIRED_HEADSET|AUDIO_DEVICE_OUT_WIRED_HEADPHONE| |
| 72 | AUDIO_DEVICE_OUT_SPEAKER, |
| 73 | }; |
| 74 | |
| 75 | static struct pcm_device_profile pcm_device_capture = { |
| 76 | .config = { |
| 77 | .channels = CAPTURE_DEFAULT_CHANNEL_COUNT, |
| 78 | .rate = CAPTURE_DEFAULT_SAMPLING_RATE, |
| 79 | .period_size = CAPTURE_PERIOD_SIZE, |
| 80 | .period_count = CAPTURE_PERIOD_COUNT, |
| 81 | .format = PCM_FORMAT_S16_LE, |
| 82 | .start_threshold = CAPTURE_START_THRESHOLD, |
| 83 | .stop_threshold = 0, |
| 84 | .silence_threshold = 0, |
| 85 | .avail_min = 0, |
| 86 | }, |
| 87 | .card = SOUND_CARD, |
| 88 | .id = 0, |
| 89 | .type = PCM_CAPTURE, |
| 90 | .devices = AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_WIRED_HEADSET|AUDIO_DEVICE_IN_BACK_MIC, |
| 91 | }; |
| 92 | |
| 93 | static struct pcm_device_profile pcm_device_capture_low_latency = { |
| 94 | .config = { |
| 95 | .channels = CAPTURE_DEFAULT_CHANNEL_COUNT, |
| 96 | .rate = CAPTURE_DEFAULT_SAMPLING_RATE, |
| 97 | .period_size = CAPTURE_PERIOD_SIZE_LOW_LATENCY, |
| 98 | .period_count = CAPTURE_PERIOD_COUNT_LOW_LATENCY, |
| 99 | .format = PCM_FORMAT_S16_LE, |
| 100 | .start_threshold = CAPTURE_START_THRESHOLD, |
| 101 | .stop_threshold = 0, |
| 102 | .silence_threshold = 0, |
| 103 | .avail_min = 0, |
| 104 | }, |
| 105 | .card = SOUND_CARD, |
| 106 | .id = 0, |
| 107 | .type = PCM_CAPTURE_LOW_LATENCY, |
| 108 | .devices = AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_WIRED_HEADSET|AUDIO_DEVICE_IN_BACK_MIC, |
| 109 | }; |
| 110 | |
| 111 | static struct pcm_device_profile pcm_device_capture_loopback_aec = { |
| 112 | .config = { |
| 113 | .channels = CAPTURE_DEFAULT_CHANNEL_COUNT, |
| 114 | .rate = CAPTURE_DEFAULT_SAMPLING_RATE, |
| 115 | .period_size = CAPTURE_PERIOD_SIZE, |
| 116 | .period_count = CAPTURE_PERIOD_COUNT, |
| 117 | .format = PCM_FORMAT_S16_LE, |
| 118 | .start_threshold = CAPTURE_START_THRESHOLD, |
| 119 | .stop_threshold = 0, |
| 120 | .silence_threshold = 0, |
| 121 | .avail_min = 0, |
| 122 | }, |
| 123 | .card = SOUND_CARD, |
| 124 | .id = 1, |
| 125 | .type = PCM_CAPTURE, |
| 126 | .devices = SND_DEVICE_IN_LOOPBACK_AEC, |
| 127 | }; |
| 128 | |
| 129 | static struct pcm_device_profile pcm_device_playback_sco = { |
| 130 | .config = { |
| 131 | .channels = SCO_DEFAULT_CHANNEL_COUNT, |
| 132 | .rate = SCO_DEFAULT_SAMPLING_RATE, |
| 133 | .period_size = SCO_PERIOD_SIZE, |
| 134 | .period_count = SCO_PERIOD_COUNT, |
| 135 | .format = PCM_FORMAT_S16_LE, |
| 136 | .start_threshold = SCO_START_THRESHOLD, |
| 137 | .stop_threshold = SCO_STOP_THRESHOLD, |
| 138 | .silence_threshold = 0, |
| 139 | .avail_min = SCO_AVAILABLE_MIN, |
| 140 | }, |
| 141 | .card = SOUND_CARD, |
| 142 | .id = 2, |
| 143 | .type = PCM_PLAYBACK, |
| 144 | .devices = |
| 145 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO|AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET| |
| 146 | AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, |
| 147 | }; |
| 148 | |
| 149 | static struct pcm_device_profile pcm_device_capture_sco = { |
| 150 | .config = { |
| 151 | .channels = SCO_DEFAULT_CHANNEL_COUNT, |
| 152 | .rate = SCO_DEFAULT_SAMPLING_RATE, |
| 153 | .period_size = SCO_PERIOD_SIZE, |
| 154 | .period_count = SCO_PERIOD_COUNT, |
| 155 | .format = PCM_FORMAT_S16_LE, |
| 156 | .start_threshold = CAPTURE_START_THRESHOLD, |
| 157 | .stop_threshold = 0, |
| 158 | .silence_threshold = 0, |
| 159 | .avail_min = 0, |
| 160 | }, |
| 161 | .card = SOUND_CARD, |
| 162 | .id = 2, |
| 163 | .type = PCM_CAPTURE, |
| 164 | .devices = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, |
| 165 | }; |
| 166 | |
| 167 | static struct pcm_device_profile pcm_device_hotword_streaming = { |
| 168 | .config = { |
| 169 | .channels = 1, |
| 170 | .rate = 16000, |
| 171 | .period_size = CAPTURE_PERIOD_SIZE, |
| 172 | .period_count = CAPTURE_PERIOD_COUNT, |
| 173 | .format = PCM_FORMAT_S16_LE, |
| 174 | .start_threshold = CAPTURE_START_THRESHOLD, |
| 175 | .stop_threshold = 0, |
| 176 | .silence_threshold = 0, |
| 177 | .avail_min = 0, |
| 178 | }, |
| 179 | .card = SOUND_CARD, |
| 180 | .id = 0, |
| 181 | .type = PCM_HOTWORD_STREAMING, |
| 182 | .devices = AUDIO_DEVICE_IN_BUILTIN_MIC|AUDIO_DEVICE_IN_WIRED_HEADSET|AUDIO_DEVICE_IN_BACK_MIC |
| 183 | }; |
| 184 | |
| 185 | static struct pcm_device_profile * const pcm_devices[] = { |
| 186 | &pcm_device_playback, |
| 187 | &pcm_device_capture, |
| 188 | &pcm_device_capture_low_latency, |
| 189 | &pcm_device_playback_sco, |
| 190 | &pcm_device_capture_sco, |
| 191 | &pcm_device_capture_loopback_aec, |
| 192 | &pcm_device_hotword_streaming, |
| 193 | NULL, |
| 194 | }; |
| 195 | |
| 196 | static const char * const use_case_table[AUDIO_USECASE_MAX] = { |
| 197 | [USECASE_AUDIO_PLAYBACK] = "playback", |
| 198 | [USECASE_AUDIO_PLAYBACK_MULTI_CH] = "playback multi-channel", |
| 199 | [USECASE_AUDIO_PLAYBACK_OFFLOAD] = "compress-offload-playback", |
| 200 | [USECASE_AUDIO_CAPTURE] = "capture", |
| 201 | [USECASE_AUDIO_CAPTURE_HOTWORD] = "capture-hotword", |
| 202 | [USECASE_VOICE_CALL] = "voice-call", |
| 203 | }; |
| 204 | |
| 205 | |
| 206 | #define STRING_TO_ENUM(string) { #string, string } |
| 207 | |
| 208 | static unsigned int audio_device_ref_count; |
| 209 | |
| 210 | static struct pcm_config pcm_config_deep_buffer = { |
| 211 | .channels = 2, |
| 212 | .rate = DEEP_BUFFER_OUTPUT_SAMPLING_RATE, |
| 213 | .period_size = DEEP_BUFFER_OUTPUT_PERIOD_SIZE, |
| 214 | .period_count = DEEP_BUFFER_OUTPUT_PERIOD_COUNT, |
| 215 | .format = PCM_FORMAT_S16_LE, |
| 216 | .start_threshold = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, |
| 217 | .stop_threshold = INT_MAX, |
| 218 | .avail_min = DEEP_BUFFER_OUTPUT_PERIOD_SIZE / 4, |
| 219 | }; |
| 220 | |
| 221 | struct string_to_enum { |
| 222 | const char *name; |
| 223 | uint32_t value; |
| 224 | }; |
| 225 | |
| 226 | static const struct string_to_enum out_channels_name_to_enum_table[] = { |
| 227 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), |
| 228 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), |
| 229 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), |
| 230 | }; |
| 231 | |
Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 232 | static bool is_supported_format(audio_format_t format) |
| 233 | { |
| 234 | if (format == AUDIO_FORMAT_MP3 || |
| 235 | ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_AAC)) |
| 236 | return true; |
| 237 | |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | static int get_snd_codec_id(audio_format_t format) |
| 242 | { |
| 243 | int id = 0; |
| 244 | |
| 245 | switch (format & AUDIO_FORMAT_MAIN_MASK) { |
| 246 | case AUDIO_FORMAT_MP3: |
| 247 | id = SND_AUDIOCODEC_MP3; |
| 248 | break; |
| 249 | case AUDIO_FORMAT_AAC: |
| 250 | id = SND_AUDIOCODEC_AAC; |
| 251 | break; |
| 252 | default: |
| 253 | ALOGE("%s: Unsupported audio format", __func__); |
| 254 | } |
| 255 | |
| 256 | return id; |
| 257 | } |
| 258 | |
| 259 | /* Array to store sound devices */ |
| 260 | static const char * const device_table[SND_DEVICE_MAX] = { |
| 261 | [SND_DEVICE_NONE] = "none", |
| 262 | /* Playback sound devices */ |
| 263 | [SND_DEVICE_OUT_HANDSET] = "handset", |
| 264 | [SND_DEVICE_OUT_SPEAKER] = "speaker", |
| 265 | [SND_DEVICE_OUT_HEADPHONES] = "headphones", |
| 266 | [SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES] = "speaker-and-headphones", |
| 267 | [SND_DEVICE_OUT_VOICE_HANDSET] = "voice-handset", |
| 268 | [SND_DEVICE_OUT_VOICE_SPEAKER] = "voice-speaker", |
| 269 | [SND_DEVICE_OUT_VOICE_HEADPHONES] = "voice-headphones", |
| 270 | [SND_DEVICE_OUT_HDMI] = "hdmi", |
| 271 | [SND_DEVICE_OUT_SPEAKER_AND_HDMI] = "speaker-and-hdmi", |
| 272 | [SND_DEVICE_OUT_BT_SCO] = "bt-sco-headset", |
| 273 | [SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES] = "voice-tty-full-headphones", |
| 274 | [SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES] = "voice-tty-vco-headphones", |
| 275 | [SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET] = "voice-tty-hco-handset", |
| 276 | |
| 277 | /* Capture sound devices */ |
| 278 | [SND_DEVICE_IN_HANDSET_MIC] = "handset-mic", |
| 279 | [SND_DEVICE_IN_SPEAKER_MIC] = "speaker-mic", |
| 280 | [SND_DEVICE_IN_HEADSET_MIC] = "headset-mic", |
| 281 | [SND_DEVICE_IN_HANDSET_MIC_AEC] = "handset-mic", |
| 282 | [SND_DEVICE_IN_SPEAKER_MIC_AEC] = "voice-speaker-mic", |
| 283 | [SND_DEVICE_IN_HEADSET_MIC_AEC] = "headset-mic", |
| 284 | [SND_DEVICE_IN_VOICE_SPEAKER_MIC] = "voice-speaker-mic", |
| 285 | [SND_DEVICE_IN_VOICE_HEADSET_MIC] = "voice-headset-mic", |
| 286 | [SND_DEVICE_IN_HDMI_MIC] = "hdmi-mic", |
| 287 | [SND_DEVICE_IN_BT_SCO_MIC] = "bt-sco-mic", |
| 288 | [SND_DEVICE_IN_CAMCORDER_MIC] = "camcorder-mic", |
| 289 | [SND_DEVICE_IN_VOICE_DMIC_1] = "voice-dmic-1", |
| 290 | [SND_DEVICE_IN_VOICE_SPEAKER_DMIC_1] = "voice-speaker-dmic-1", |
| 291 | [SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC] = "voice-tty-full-headset-mic", |
| 292 | [SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC] = "voice-tty-vco-handset-mic", |
| 293 | [SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC] = "voice-tty-hco-headset-mic", |
| 294 | [SND_DEVICE_IN_VOICE_REC_HEADSET_MIC] = "voice-rec-headset-mic", |
| 295 | [SND_DEVICE_IN_VOICE_REC_MIC] = "voice-rec-mic", |
| 296 | [SND_DEVICE_IN_VOICE_REC_DMIC_1] = "voice-rec-dmic-1", |
| 297 | [SND_DEVICE_IN_VOICE_REC_DMIC_NS_1] = "voice-rec-dmic-ns-1", |
| 298 | [SND_DEVICE_IN_LOOPBACK_AEC] = "loopback-aec", |
| 299 | }; |
| 300 | |
| 301 | static struct mixer_card *adev_get_mixer_for_card(struct audio_device *adev, int card) |
| 302 | { |
| 303 | struct mixer_card *mixer_card; |
| 304 | struct listnode *node; |
| 305 | |
| 306 | list_for_each(node, &adev->mixer_list) { |
| 307 | mixer_card = node_to_item(node, struct mixer_card, adev_list_node); |
| 308 | if (mixer_card->card == card) |
| 309 | return mixer_card; |
| 310 | } |
| 311 | return NULL; |
| 312 | } |
| 313 | |
| 314 | static struct mixer_card *uc_get_mixer_for_card(struct audio_usecase *usecase, int card) |
| 315 | { |
| 316 | struct mixer_card *mixer_card; |
| 317 | struct listnode *node; |
| 318 | |
| 319 | list_for_each(node, &usecase->mixer_list) { |
| 320 | mixer_card = node_to_item(node, struct mixer_card, uc_list_node[usecase->id]); |
| 321 | if (mixer_card->card == card) |
| 322 | return mixer_card; |
| 323 | } |
| 324 | return NULL; |
| 325 | } |
| 326 | |
| 327 | static void free_mixer_list(struct audio_device *adev) |
| 328 | { |
| 329 | struct mixer_card *mixer_card; |
| 330 | struct listnode *node; |
| 331 | struct listnode *next; |
| 332 | |
| 333 | list_for_each_safe(node, next, &adev->mixer_list) { |
| 334 | mixer_card = node_to_item(node, struct mixer_card, adev_list_node); |
| 335 | list_remove(node); |
| 336 | audio_route_free(mixer_card->audio_route); |
| 337 | free(mixer_card); |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | static int mixer_init(struct audio_device *adev) |
| 342 | { |
| 343 | int i; |
| 344 | int card; |
| 345 | int retry_num; |
| 346 | struct mixer *mixer; |
| 347 | struct audio_route *audio_route; |
| 348 | char mixer_path[PATH_MAX]; |
| 349 | struct mixer_card *mixer_card; |
| 350 | struct listnode *node; |
| 351 | |
| 352 | list_init(&adev->mixer_list); |
| 353 | |
| 354 | for (i = 0; pcm_devices[i] != NULL; i++) { |
| 355 | card = pcm_devices[i]->card; |
| 356 | if (adev_get_mixer_for_card(adev, card) == NULL) { |
| 357 | retry_num = 0; |
| 358 | do { |
| 359 | mixer = mixer_open(card); |
| 360 | if (mixer == NULL) { |
| 361 | if (++retry_num > RETRY_NUMBER) { |
| 362 | ALOGE("%s unable to open the mixer for--card %d, aborting.", |
| 363 | __func__, card); |
| 364 | goto error; |
| 365 | } |
| 366 | usleep(RETRY_US); |
| 367 | } |
| 368 | } while (mixer == NULL); |
| 369 | |
| 370 | sprintf(mixer_path, "/system/etc/mixer_paths_%d.xml", card); |
| 371 | audio_route = audio_route_init(card, mixer_path); |
| 372 | if (!audio_route) { |
| 373 | ALOGE("%s: Failed to init audio route controls for card %d, aborting.", |
| 374 | __func__, card); |
| 375 | goto error; |
| 376 | } |
| 377 | mixer_card = calloc(1, sizeof(struct mixer_card)); |
| 378 | mixer_card->card = card; |
| 379 | mixer_card->mixer = mixer; |
| 380 | mixer_card->audio_route = audio_route; |
| 381 | list_add_tail(&adev->mixer_list, &mixer_card->adev_list_node); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | return 0; |
| 386 | |
| 387 | error: |
| 388 | free_mixer_list(adev); |
| 389 | return -ENODEV; |
| 390 | } |
| 391 | |
| 392 | static const char *get_snd_device_name(snd_device_t snd_device) |
| 393 | { |
| 394 | const char *name = NULL; |
| 395 | |
| 396 | if (snd_device >= SND_DEVICE_MIN && snd_device < SND_DEVICE_MAX) |
| 397 | name = device_table[snd_device]; |
| 398 | |
| 399 | ALOGE_IF(name == NULL, "%s: invalid snd device %d", __func__, snd_device); |
| 400 | |
| 401 | return name; |
| 402 | } |
| 403 | |
| 404 | static const char *get_snd_device_display_name(snd_device_t snd_device) |
| 405 | { |
| 406 | const char *name = get_snd_device_name(snd_device); |
| 407 | |
| 408 | if (name == NULL) |
| 409 | name = "SND DEVICE NOT FOUND"; |
| 410 | |
| 411 | return name; |
| 412 | } |
| 413 | |
| 414 | static struct pcm_device_profile *get_pcm_device(usecase_type_t uc_type, audio_devices_t devices) |
| 415 | { |
| 416 | int i; |
| 417 | |
| 418 | devices &= ~AUDIO_DEVICE_BIT_IN; |
| 419 | for (i = 0; pcm_devices[i] != NULL; i++) { |
| 420 | if ((pcm_devices[i]->type == uc_type) && |
| 421 | (devices & pcm_devices[i]->devices)) |
| 422 | break; |
| 423 | } |
| 424 | return pcm_devices[i]; |
| 425 | } |
| 426 | |
| 427 | static struct audio_usecase *get_usecase_from_id(struct audio_device *adev, |
| 428 | audio_usecase_t uc_id) |
| 429 | { |
| 430 | struct audio_usecase *usecase; |
| 431 | struct listnode *node; |
| 432 | |
| 433 | list_for_each(node, &adev->usecase_list) { |
| 434 | usecase = node_to_item(node, struct audio_usecase, adev_list_node); |
| 435 | if (usecase->id == uc_id) |
| 436 | return usecase; |
| 437 | } |
| 438 | return NULL; |
| 439 | } |
| 440 | |
| 441 | static struct audio_usecase *get_usecase_from_type(struct audio_device *adev, |
| 442 | usecase_type_t type) |
| 443 | { |
| 444 | struct audio_usecase *usecase; |
| 445 | struct listnode *node; |
| 446 | |
| 447 | list_for_each(node, &adev->usecase_list) { |
| 448 | usecase = node_to_item(node, struct audio_usecase, adev_list_node); |
| 449 | if (usecase->type & type) |
| 450 | return usecase; |
| 451 | } |
| 452 | return NULL; |
| 453 | } |
| 454 | |
| 455 | /* always called with adev lock held */ |
| 456 | static int set_voice_volume_l(struct audio_device *adev, float volume) |
| 457 | { |
| 458 | int err = 0; |
| 459 | (void)volume; |
| 460 | |
| 461 | if (adev->mode == AUDIO_MODE_IN_CALL) { |
| 462 | /* TODO */ |
| 463 | } |
| 464 | return err; |
| 465 | } |
| 466 | |
| 467 | |
| 468 | static snd_device_t get_output_snd_device(struct audio_device *adev, audio_devices_t devices) |
| 469 | { |
| 470 | |
| 471 | audio_mode_t mode = adev->mode; |
| 472 | snd_device_t snd_device = SND_DEVICE_NONE; |
| 473 | |
| 474 | ALOGV("%s: enter: output devices(%#x), mode(%d)", __func__, devices, mode); |
| 475 | if (devices == AUDIO_DEVICE_NONE || |
| 476 | devices & AUDIO_DEVICE_BIT_IN) { |
| 477 | ALOGV("%s: Invalid output devices (%#x)", __func__, devices); |
| 478 | goto exit; |
| 479 | } |
| 480 | |
| 481 | if (mode == AUDIO_MODE_IN_CALL) { |
| 482 | if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE || |
| 483 | devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) { |
| 484 | if (adev->tty_mode == TTY_MODE_FULL) |
| 485 | snd_device = SND_DEVICE_OUT_VOICE_TTY_FULL_HEADPHONES; |
| 486 | else if (adev->tty_mode == TTY_MODE_VCO) |
| 487 | snd_device = SND_DEVICE_OUT_VOICE_TTY_VCO_HEADPHONES; |
| 488 | else if (adev->tty_mode == TTY_MODE_HCO) |
| 489 | snd_device = SND_DEVICE_OUT_VOICE_TTY_HCO_HANDSET; |
| 490 | else |
| 491 | snd_device = SND_DEVICE_OUT_VOICE_HEADPHONES; |
| 492 | } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) { |
| 493 | snd_device = SND_DEVICE_OUT_BT_SCO; |
| 494 | } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) { |
| 495 | snd_device = SND_DEVICE_OUT_VOICE_SPEAKER; |
| 496 | } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) { |
| 497 | snd_device = SND_DEVICE_OUT_HANDSET; |
| 498 | } |
| 499 | if (snd_device != SND_DEVICE_NONE) { |
| 500 | goto exit; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | if (popcount(devices) == 2) { |
| 505 | if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
| 506 | AUDIO_DEVICE_OUT_SPEAKER)) { |
| 507 | snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES; |
| 508 | } else if (devices == (AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 509 | AUDIO_DEVICE_OUT_SPEAKER)) { |
| 510 | snd_device = SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES; |
| 511 | } else { |
| 512 | ALOGE("%s: Invalid combo device(%#x)", __func__, devices); |
| 513 | goto exit; |
| 514 | } |
| 515 | if (snd_device != SND_DEVICE_NONE) { |
| 516 | goto exit; |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | if (popcount(devices) != 1) { |
| 521 | ALOGE("%s: Invalid output devices(%#x)", __func__, devices); |
| 522 | goto exit; |
| 523 | } |
| 524 | |
| 525 | if (devices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE || |
| 526 | devices & AUDIO_DEVICE_OUT_WIRED_HEADSET) { |
| 527 | snd_device = SND_DEVICE_OUT_HEADPHONES; |
| 528 | } else if (devices & AUDIO_DEVICE_OUT_SPEAKER) { |
| 529 | snd_device = SND_DEVICE_OUT_SPEAKER; |
| 530 | } else if (devices & AUDIO_DEVICE_OUT_ALL_SCO) { |
| 531 | snd_device = SND_DEVICE_OUT_BT_SCO; |
| 532 | } else if (devices & AUDIO_DEVICE_OUT_EARPIECE) { |
| 533 | snd_device = SND_DEVICE_OUT_HANDSET; |
| 534 | } else { |
| 535 | ALOGE("%s: Unknown device(s) %#x", __func__, devices); |
| 536 | } |
| 537 | exit: |
| 538 | ALOGV("%s: exit: snd_device(%s)", __func__, device_table[snd_device]); |
| 539 | return snd_device; |
| 540 | } |
| 541 | |
| 542 | static snd_device_t get_input_snd_device(struct audio_device *adev, audio_devices_t out_device) |
| 543 | { |
| 544 | audio_source_t source; |
| 545 | audio_mode_t mode = adev->mode; |
| 546 | audio_devices_t in_device; |
| 547 | audio_channel_mask_t channel_mask; |
| 548 | snd_device_t snd_device = SND_DEVICE_NONE; |
| 549 | struct stream_in *active_input = NULL; |
| 550 | struct audio_usecase *usecase; |
| 551 | |
| 552 | usecase = get_usecase_from_type(adev, PCM_CAPTURE|VOICE_CALL); |
| 553 | if (usecase != NULL) { |
| 554 | active_input = (struct stream_in *)usecase->stream; |
| 555 | } |
| 556 | source = (active_input == NULL) ? |
| 557 | AUDIO_SOURCE_DEFAULT : active_input->source; |
| 558 | |
| 559 | in_device = ((active_input == NULL) ? |
| 560 | AUDIO_DEVICE_NONE : active_input->devices) |
| 561 | & ~AUDIO_DEVICE_BIT_IN; |
| 562 | channel_mask = (active_input == NULL) ? |
| 563 | AUDIO_CHANNEL_IN_MONO : active_input->main_channels; |
| 564 | |
| 565 | ALOGV("%s: enter: out_device(%#x) in_device(%#x)", |
| 566 | __func__, out_device, in_device); |
| 567 | if (mode == AUDIO_MODE_IN_CALL) { |
| 568 | if (out_device == AUDIO_DEVICE_NONE) { |
| 569 | ALOGE("%s: No output device set for voice call", __func__); |
| 570 | goto exit; |
| 571 | } |
| 572 | if (adev->tty_mode != TTY_MODE_OFF) { |
| 573 | if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE || |
| 574 | out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) { |
| 575 | switch (adev->tty_mode) { |
| 576 | case TTY_MODE_FULL: |
| 577 | snd_device = SND_DEVICE_IN_VOICE_TTY_FULL_HEADSET_MIC; |
| 578 | break; |
| 579 | case TTY_MODE_VCO: |
| 580 | snd_device = SND_DEVICE_IN_VOICE_TTY_VCO_HANDSET_MIC; |
| 581 | break; |
| 582 | case TTY_MODE_HCO: |
| 583 | snd_device = SND_DEVICE_IN_VOICE_TTY_HCO_HEADSET_MIC; |
| 584 | break; |
| 585 | default: |
| 586 | ALOGE("%s: Invalid TTY mode (%#x)", __func__, adev->tty_mode); |
| 587 | } |
| 588 | goto exit; |
| 589 | } |
| 590 | } |
| 591 | if (out_device & AUDIO_DEVICE_OUT_EARPIECE || |
| 592 | out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) { |
| 593 | snd_device = SND_DEVICE_IN_HANDSET_MIC; |
| 594 | } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) { |
| 595 | snd_device = SND_DEVICE_IN_VOICE_HEADSET_MIC; |
| 596 | } else if (out_device & AUDIO_DEVICE_OUT_ALL_SCO) { |
| 597 | snd_device = SND_DEVICE_IN_BT_SCO_MIC ; |
| 598 | } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) { |
| 599 | snd_device = SND_DEVICE_IN_VOICE_SPEAKER_MIC; |
| 600 | } |
| 601 | } else if (source == AUDIO_SOURCE_CAMCORDER) { |
| 602 | if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC || |
| 603 | in_device & AUDIO_DEVICE_IN_BACK_MIC) { |
| 604 | snd_device = SND_DEVICE_IN_CAMCORDER_MIC; |
| 605 | } |
| 606 | } else if (source == AUDIO_SOURCE_VOICE_RECOGNITION) { |
| 607 | if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 608 | if (adev->dualmic_config == DUALMIC_CONFIG_1) { |
| 609 | if (channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK) |
| 610 | snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_1; |
| 611 | else if (adev->ns_in_voice_rec) |
| 612 | snd_device = SND_DEVICE_IN_VOICE_REC_DMIC_NS_1; |
| 613 | } |
| 614 | |
| 615 | if (snd_device == SND_DEVICE_NONE) { |
| 616 | snd_device = SND_DEVICE_IN_VOICE_REC_MIC; |
| 617 | } |
| 618 | } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 619 | snd_device = SND_DEVICE_IN_VOICE_REC_HEADSET_MIC; |
| 620 | } |
| 621 | } else if (source == AUDIO_SOURCE_VOICE_COMMUNICATION || source == AUDIO_SOURCE_MIC) { |
| 622 | if (out_device & AUDIO_DEVICE_OUT_SPEAKER) |
| 623 | in_device = AUDIO_DEVICE_IN_BACK_MIC; |
| 624 | if (active_input) { |
| 625 | if (active_input->enable_aec) { |
| 626 | if (in_device & AUDIO_DEVICE_IN_BACK_MIC) { |
| 627 | snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC; |
| 628 | } else if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 629 | if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) { |
| 630 | snd_device = SND_DEVICE_IN_SPEAKER_MIC_AEC; |
| 631 | } else { |
| 632 | snd_device = SND_DEVICE_IN_HANDSET_MIC_AEC; |
| 633 | } |
| 634 | } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 635 | snd_device = SND_DEVICE_IN_HEADSET_MIC_AEC; |
| 636 | } |
| 637 | } |
| 638 | /* TODO: set echo reference */ |
| 639 | } |
| 640 | } else if (source == AUDIO_SOURCE_DEFAULT) { |
| 641 | goto exit; |
| 642 | } |
| 643 | |
| 644 | |
| 645 | if (snd_device != SND_DEVICE_NONE) { |
| 646 | goto exit; |
| 647 | } |
| 648 | |
| 649 | if (in_device != AUDIO_DEVICE_NONE && |
| 650 | !(in_device & AUDIO_DEVICE_IN_VOICE_CALL) && |
| 651 | !(in_device & AUDIO_DEVICE_IN_COMMUNICATION)) { |
| 652 | if (in_device & AUDIO_DEVICE_IN_BUILTIN_MIC) { |
| 653 | snd_device = SND_DEVICE_IN_HANDSET_MIC; |
| 654 | } else if (in_device & AUDIO_DEVICE_IN_BACK_MIC) { |
| 655 | snd_device = SND_DEVICE_IN_SPEAKER_MIC; |
| 656 | } else if (in_device & AUDIO_DEVICE_IN_WIRED_HEADSET) { |
| 657 | snd_device = SND_DEVICE_IN_HEADSET_MIC; |
| 658 | } else if (in_device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) { |
| 659 | snd_device = SND_DEVICE_IN_BT_SCO_MIC ; |
| 660 | } else if (in_device & AUDIO_DEVICE_IN_AUX_DIGITAL) { |
| 661 | snd_device = SND_DEVICE_IN_HDMI_MIC; |
| 662 | } else { |
| 663 | ALOGE("%s: Unknown input device(s) %#x", __func__, in_device); |
| 664 | ALOGW("%s: Using default handset-mic", __func__); |
| 665 | snd_device = SND_DEVICE_IN_HANDSET_MIC; |
| 666 | } |
| 667 | } else { |
| 668 | if (out_device & AUDIO_DEVICE_OUT_EARPIECE) { |
| 669 | snd_device = SND_DEVICE_IN_HANDSET_MIC; |
| 670 | } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADSET) { |
| 671 | snd_device = SND_DEVICE_IN_HEADSET_MIC; |
| 672 | } else if (out_device & AUDIO_DEVICE_OUT_SPEAKER) { |
| 673 | snd_device = SND_DEVICE_IN_SPEAKER_MIC; |
| 674 | } else if (out_device & AUDIO_DEVICE_OUT_WIRED_HEADPHONE) { |
| 675 | snd_device = SND_DEVICE_IN_HANDSET_MIC; |
| 676 | } else if (out_device & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET) { |
| 677 | snd_device = SND_DEVICE_IN_BT_SCO_MIC; |
| 678 | } else { |
| 679 | ALOGE("%s: Unknown output device(s) %#x", __func__, out_device); |
| 680 | ALOGW("%s: Using default handset-mic", __func__); |
| 681 | snd_device = SND_DEVICE_IN_HANDSET_MIC; |
| 682 | } |
| 683 | } |
| 684 | exit: |
| 685 | ALOGV("%s: exit: in_snd_device(%s)", __func__, device_table[snd_device]); |
| 686 | return snd_device; |
| 687 | } |
| 688 | |
| 689 | static int set_hdmi_channels(struct audio_device *adev, int channel_count) |
| 690 | { |
| 691 | struct mixer_ctl *ctl; |
| 692 | const char *mixer_ctl_name = ""; |
| 693 | (void)adev; |
| 694 | (void)channel_count; |
| 695 | /* TODO */ |
| 696 | |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | static int edid_get_max_channels(struct audio_device *adev) |
| 701 | { |
| 702 | int max_channels = 2; |
| 703 | struct mixer_ctl *ctl; |
| 704 | (void)adev; |
| 705 | |
| 706 | /* TODO */ |
| 707 | return max_channels; |
| 708 | } |
| 709 | |
| 710 | /* Delay in Us */ |
| 711 | static int64_t render_latency(audio_usecase_t usecase) |
| 712 | { |
| 713 | (void)usecase; |
| 714 | /* TODO */ |
| 715 | return 0; |
| 716 | } |
| 717 | |
| 718 | static int enable_snd_device(struct audio_device *adev, |
| 719 | struct audio_usecase *uc_info, |
| 720 | snd_device_t snd_device, |
| 721 | bool update_mixer) |
| 722 | { |
| 723 | struct mixer_card *mixer_card; |
| 724 | struct listnode *node; |
| 725 | const char *snd_device_name = get_snd_device_name(snd_device); |
| 726 | |
| 727 | if (snd_device_name == NULL) |
| 728 | return -EINVAL; |
| 729 | |
| 730 | if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES) { |
| 731 | ALOGV("Request to enable combo device: enable individual devices\n"); |
| 732 | enable_snd_device(adev, uc_info, SND_DEVICE_OUT_SPEAKER, update_mixer); |
| 733 | enable_snd_device(adev, uc_info, SND_DEVICE_OUT_HEADPHONES, update_mixer); |
| 734 | return 0; |
| 735 | } |
| 736 | adev->snd_dev_ref_cnt[snd_device]++; |
| 737 | if (adev->snd_dev_ref_cnt[snd_device] > 1) { |
| 738 | ALOGV("%s: snd_device(%d: %s) is already active", |
| 739 | __func__, snd_device, snd_device_name); |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | ALOGV("%s: snd_device(%d: %s)", __func__, |
| 744 | snd_device, snd_device_name); |
| 745 | |
| 746 | list_for_each(node, &uc_info->mixer_list) { |
| 747 | mixer_card = node_to_item(node, struct mixer_card, uc_list_node[uc_info->id]); |
| 748 | audio_route_apply_path(mixer_card->audio_route, snd_device_name); |
| 749 | if (update_mixer) |
| 750 | audio_route_update_mixer(mixer_card->audio_route); |
| 751 | } |
| 752 | |
| 753 | return 0; |
| 754 | } |
| 755 | |
| 756 | static int disable_snd_device(struct audio_device *adev, |
| 757 | struct audio_usecase *uc_info, |
| 758 | snd_device_t snd_device, |
| 759 | bool update_mixer) |
| 760 | { |
| 761 | struct mixer_card *mixer_card; |
| 762 | struct listnode *node; |
| 763 | const char *snd_device_name = get_snd_device_name(snd_device); |
| 764 | |
| 765 | if (snd_device_name == NULL) |
| 766 | return -EINVAL; |
| 767 | |
| 768 | if (snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES) { |
| 769 | ALOGV("Request to disable combo device: disable individual devices\n"); |
| 770 | disable_snd_device(adev, uc_info, SND_DEVICE_OUT_SPEAKER, update_mixer); |
| 771 | disable_snd_device(adev, uc_info, SND_DEVICE_OUT_HEADPHONES, update_mixer); |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | if (adev->snd_dev_ref_cnt[snd_device] <= 0) { |
| 776 | ALOGE("%s: device ref cnt is already 0", __func__); |
| 777 | return -EINVAL; |
| 778 | } |
| 779 | adev->snd_dev_ref_cnt[snd_device]--; |
| 780 | if (adev->snd_dev_ref_cnt[snd_device] == 0) { |
| 781 | ALOGV("%s: snd_device(%d: %s)", __func__, |
| 782 | snd_device, snd_device_name); |
| 783 | list_for_each(node, &uc_info->mixer_list) { |
| 784 | mixer_card = node_to_item(node, struct mixer_card, uc_list_node[uc_info->id]); |
| 785 | audio_route_reset_path(mixer_card->audio_route, snd_device_name); |
| 786 | if (update_mixer) |
| 787 | audio_route_update_mixer(mixer_card->audio_route); |
| 788 | } |
| 789 | } |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | static int select_devices(struct audio_device *adev, |
| 794 | audio_usecase_t uc_id) |
| 795 | { |
| 796 | snd_device_t out_snd_device = SND_DEVICE_NONE; |
| 797 | snd_device_t in_snd_device = SND_DEVICE_NONE; |
| 798 | struct audio_usecase *usecase = NULL; |
| 799 | struct audio_usecase *vc_usecase = NULL; |
| 800 | struct listnode *node; |
| 801 | struct stream_in *active_input = NULL; |
| 802 | struct stream_out *active_out; |
| 803 | struct mixer_card *mixer_card; |
| 804 | |
| 805 | ALOGV("%s: usecase(%d)", __func__, uc_id); |
| 806 | |
| 807 | if (uc_id == USECASE_AUDIO_CAPTURE_HOTWORD) |
| 808 | return 0; |
| 809 | |
| 810 | usecase = get_usecase_from_type(adev, PCM_CAPTURE|VOICE_CALL); |
| 811 | if (usecase != NULL) { |
| 812 | active_input = (struct stream_in *)usecase->stream; |
| 813 | } |
| 814 | |
| 815 | usecase = get_usecase_from_id(adev, uc_id); |
| 816 | if (usecase == NULL) { |
| 817 | ALOGE("%s: Could not find the usecase(%d)", __func__, uc_id); |
| 818 | return -EINVAL; |
| 819 | } |
| 820 | active_out = (struct stream_out *)usecase->stream; |
| 821 | |
| 822 | if (usecase->type == VOICE_CALL) { |
| 823 | out_snd_device = get_output_snd_device(adev, active_out->devices); |
| 824 | in_snd_device = get_input_snd_device(adev, active_out->devices); |
| 825 | usecase->devices = active_out->devices; |
| 826 | } else { |
| 827 | /* |
| 828 | * If the voice call is active, use the sound devices of voice call usecase |
| 829 | * so that it would not result any device switch. All the usecases will |
| 830 | * be switched to new device when select_devices() is called for voice call |
| 831 | * usecase. |
| 832 | */ |
| 833 | if (adev->in_call) { |
| 834 | vc_usecase = get_usecase_from_id(adev, USECASE_VOICE_CALL); |
| 835 | if (usecase == NULL) { |
| 836 | ALOGE("%s: Could not find the voice call usecase", __func__); |
| 837 | } else { |
| 838 | in_snd_device = vc_usecase->in_snd_device; |
| 839 | out_snd_device = vc_usecase->out_snd_device; |
| 840 | } |
| 841 | } |
| 842 | if (usecase->type == PCM_PLAYBACK) { |
| 843 | usecase->devices = active_out->devices; |
| 844 | in_snd_device = SND_DEVICE_NONE; |
| 845 | if (out_snd_device == SND_DEVICE_NONE) { |
| 846 | out_snd_device = get_output_snd_device(adev, active_out->devices); |
| 847 | if (active_out == adev->primary_output && |
| 848 | active_input && |
| 849 | active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION) { |
| 850 | select_devices(adev, active_input->usecase); |
| 851 | } |
| 852 | } |
| 853 | } else if (usecase->type == PCM_CAPTURE) { |
| 854 | usecase->devices = ((struct stream_in *)usecase->stream)->devices; |
| 855 | out_snd_device = SND_DEVICE_NONE; |
| 856 | if (in_snd_device == SND_DEVICE_NONE) { |
| 857 | if (active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION && |
| 858 | adev->primary_output && !adev->primary_output->standby) { |
| 859 | in_snd_device = get_input_snd_device(adev, adev->primary_output->devices); |
| 860 | } else { |
| 861 | in_snd_device = get_input_snd_device(adev, AUDIO_DEVICE_NONE); |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | if (out_snd_device == usecase->out_snd_device && |
| 868 | in_snd_device == usecase->in_snd_device) { |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | ALOGV("%s: out_snd_device(%d: %s) in_snd_device(%d: %s)", __func__, |
| 873 | out_snd_device, get_snd_device_display_name(out_snd_device), |
| 874 | in_snd_device, get_snd_device_display_name(in_snd_device)); |
| 875 | |
| 876 | |
| 877 | /* Disable current sound devices */ |
| 878 | if (usecase->out_snd_device != SND_DEVICE_NONE) { |
Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 879 | disable_snd_device(adev, usecase, usecase->out_snd_device, false); |
Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 880 | } |
| 881 | |
| 882 | if (usecase->in_snd_device != SND_DEVICE_NONE) { |
| 883 | disable_snd_device(adev, usecase, usecase->in_snd_device, false); |
| 884 | } |
| 885 | |
| 886 | /* Enable new sound devices */ |
| 887 | if (out_snd_device != SND_DEVICE_NONE) { |
| 888 | enable_snd_device(adev, usecase, out_snd_device, false); |
| 889 | } |
| 890 | |
| 891 | if (in_snd_device != SND_DEVICE_NONE) { |
| 892 | enable_snd_device(adev, usecase, in_snd_device, false); |
| 893 | } |
| 894 | |
| 895 | list_for_each(node, &usecase->mixer_list) { |
| 896 | mixer_card = node_to_item(node, struct mixer_card, uc_list_node[usecase->id]); |
| 897 | audio_route_update_mixer(mixer_card->audio_route); |
| 898 | } |
| 899 | |
| 900 | usecase->in_snd_device = in_snd_device; |
| 901 | usecase->out_snd_device = out_snd_device; |
| 902 | |
Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 903 | return 0; |
| 904 | } |
| 905 | |
| 906 | |
| 907 | static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames); |
| 908 | static int do_in_standby_l(struct stream_in *in); |
| 909 | |
| 910 | #ifdef PREPROCESSING_ENABLED |
| 911 | static void get_capture_reference_delay(struct stream_in *in, |
| 912 | size_t frames __unused, |
| 913 | struct echo_reference_buffer *buffer) |
| 914 | { |
| 915 | ALOGVV("%s: enter:)", __func__); |
| 916 | |
| 917 | /* read frames available in kernel driver buffer */ |
| 918 | unsigned int kernel_frames; |
| 919 | struct timespec tstamp; |
| 920 | long buf_delay; |
| 921 | long kernel_delay; |
| 922 | long delay_ns; |
| 923 | struct pcm_device *ref_device; |
| 924 | long rsmp_delay = 0; |
| 925 | |
| 926 | ref_device = node_to_item(list_tail(&in->pcm_dev_list), |
| 927 | struct pcm_device, stream_list_node); |
| 928 | |
| 929 | if (pcm_get_htimestamp(ref_device->pcm, &kernel_frames, &tstamp) < 0) { |
| 930 | buffer->time_stamp.tv_sec = 0; |
| 931 | buffer->time_stamp.tv_nsec = 0; |
| 932 | buffer->delay_ns = 0; |
| 933 | ALOGW("read get_capture_reference_delay(): pcm_htimestamp error"); |
| 934 | return; |
| 935 | } |
| 936 | |
| 937 | /* adjust render time stamp with delay added by current driver buffer. |
| 938 | * Add the duration of current frame as we want the render time of the last |
| 939 | * sample being written. */ |
| 940 | |
| 941 | kernel_delay = (long)(((int64_t)kernel_frames * 1000000000) / ref_device->pcm_profile->config.rate); |
| 942 | |
| 943 | buffer->time_stamp = tstamp; |
| 944 | buffer->delay_ns = kernel_delay; |
| 945 | |
| 946 | ALOGVV("get_capture_reference_delay_time_stamp Secs: [%10ld], nSecs: [%9ld], kernel_frames: [%5d]," |
| 947 | " delay_ns: [%d] , frames:[%zd]", |
| 948 | buffer->time_stamp.tv_sec , buffer->time_stamp.tv_nsec, kernel_frames, buffer->delay_ns, frames); |
| 949 | } |
| 950 | |
| 951 | static void get_capture_delay(struct stream_in *in, |
| 952 | size_t frames __unused, |
| 953 | struct echo_reference_buffer *buffer) |
| 954 | { |
| 955 | ALOGVV("%s: enter:)", __func__); |
| 956 | /* read frames available in kernel driver buffer */ |
| 957 | unsigned int kernel_frames; |
| 958 | struct timespec tstamp; |
| 959 | long buf_delay; |
| 960 | long rsmp_delay; |
| 961 | long kernel_delay; |
| 962 | long delay_ns; |
| 963 | struct pcm_device *pcm_device; |
| 964 | |
| 965 | pcm_device = node_to_item(list_head(&in->pcm_dev_list), |
| 966 | struct pcm_device, stream_list_node); |
| 967 | |
| 968 | if (pcm_get_htimestamp(pcm_device->pcm, &kernel_frames, &tstamp) < 0) { |
| 969 | buffer->time_stamp.tv_sec = 0; |
| 970 | buffer->time_stamp.tv_nsec = 0; |
| 971 | buffer->delay_ns = 0; |
| 972 | ALOGW("read get_capture_delay(): pcm_htimestamp error"); |
| 973 | return; |
| 974 | } |
| 975 | |
| 976 | /* read frames available in audio HAL input buffer |
| 977 | * add number of frames being read as we want the capture time of first sample |
| 978 | * in current buffer */ |
| 979 | /* frames in in->read_buf are at driver sampling rate while frames in in->proc_buf are |
| 980 | * at requested sampling rate */ |
| 981 | buf_delay = (long)(((int64_t)(in->read_buf_frames) * 1000000000) / in->config.rate + |
| 982 | ((int64_t)(in->proc_buf_frames) * 1000000000) / in->requested_rate ); |
| 983 | |
| 984 | /* add delay introduced by resampler */ |
| 985 | rsmp_delay = 0; |
| 986 | if (in->resampler) { |
| 987 | rsmp_delay = in->resampler->delay_ns(in->resampler); |
| 988 | } |
| 989 | |
| 990 | kernel_delay = (long)(((int64_t)kernel_frames * 1000000000) / in->config.rate); |
| 991 | |
| 992 | delay_ns = kernel_delay + buf_delay + rsmp_delay; |
| 993 | |
| 994 | buffer->time_stamp = tstamp; |
| 995 | buffer->delay_ns = delay_ns; |
| 996 | ALOGVV("get_capture_delay_time_stamp Secs: [%10ld], nSecs: [%9ld], kernel_frames:[%5d]," |
| 997 | " delay_ns: [%d], kernel_delay:[%ld], buf_delay:[%ld], rsmp_delay:[%ld], " |
| 998 | "in->read_buf_frames:[%zd], in->proc_buf_frames:[%zd], frames:[%zd]", |
| 999 | buffer->time_stamp.tv_sec , buffer->time_stamp.tv_nsec, kernel_frames, |
| 1000 | buffer->delay_ns, kernel_delay, buf_delay, rsmp_delay, |
| 1001 | in->read_buf_frames, in->proc_buf_frames, frames); |
| 1002 | } |
| 1003 | |
| 1004 | static int32_t update_echo_reference(struct stream_in *in, size_t frames) |
| 1005 | { |
| 1006 | ALOGVV("%s: enter:), in->config.channels(%d)", __func__,in->config.channels); |
| 1007 | struct echo_reference_buffer b; |
| 1008 | b.delay_ns = 0; |
| 1009 | struct pcm_device *pcm_device; |
| 1010 | |
| 1011 | pcm_device = node_to_item(list_head(&in->pcm_dev_list), |
| 1012 | struct pcm_device, stream_list_node); |
| 1013 | |
| 1014 | ALOGVV("update_echo_reference, in->config.channels(%d), frames = [%zd], in->ref_buf_frames = [%zd], " |
| 1015 | "b.frame_count = [%zd]", |
| 1016 | in->config.channels, frames, in->ref_buf_frames, frames - in->ref_buf_frames); |
| 1017 | if (in->ref_buf_frames < frames) { |
| 1018 | if (in->ref_buf_size < frames) { |
| 1019 | in->ref_buf_size = frames; |
| 1020 | in->ref_buf = (int16_t *)realloc(in->ref_buf, pcm_frames_to_bytes(pcm_device->pcm, frames)); |
| 1021 | ALOG_ASSERT((in->ref_buf != NULL), |
| 1022 | "update_echo_reference() failed to reallocate ref_buf"); |
| 1023 | ALOGVV("update_echo_reference(): ref_buf %p extended to %d bytes", |
| 1024 | in->ref_buf, pcm_frames_to_bytes(pcm_device->pcm, frames)); |
| 1025 | } |
| 1026 | b.frame_count = frames - in->ref_buf_frames; |
| 1027 | b.raw = (void *)(in->ref_buf + in->ref_buf_frames * in->config.channels); |
| 1028 | |
| 1029 | get_capture_delay(in, frames, &b); |
| 1030 | |
| 1031 | if (in->echo_reference->read(in->echo_reference, &b) == 0) |
| 1032 | { |
| 1033 | in->ref_buf_frames += b.frame_count; |
| 1034 | ALOGVV("update_echo_reference(): in->ref_buf_frames:[%zd], " |
| 1035 | "in->ref_buf_size:[%zd], frames:[%zd], b.frame_count:[%zd]", |
| 1036 | in->ref_buf_frames, in->ref_buf_size, frames, b.frame_count); |
| 1037 | } |
| 1038 | } else |
| 1039 | ALOGW("update_echo_reference(): NOT enough frames to read ref buffer"); |
| 1040 | return b.delay_ns; |
| 1041 | } |
| 1042 | |
| 1043 | static int set_preprocessor_param(effect_handle_t handle, |
| 1044 | effect_param_t *param) |
| 1045 | { |
| 1046 | uint32_t size = sizeof(int); |
| 1047 | uint32_t psize = ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) + |
| 1048 | param->vsize; |
| 1049 | |
| 1050 | int status = (*handle)->command(handle, |
| 1051 | EFFECT_CMD_SET_PARAM, |
| 1052 | sizeof (effect_param_t) + psize, |
| 1053 | param, |
| 1054 | &size, |
| 1055 | ¶m->status); |
| 1056 | if (status == 0) |
| 1057 | status = param->status; |
| 1058 | |
| 1059 | return status; |
| 1060 | } |
| 1061 | |
| 1062 | static int set_preprocessor_echo_delay(effect_handle_t handle, |
| 1063 | int32_t delay_us) |
| 1064 | { |
| 1065 | struct { |
| 1066 | effect_param_t param; |
| 1067 | uint32_t data_0; |
| 1068 | int32_t data_1; |
| 1069 | } buf; |
| 1070 | memset(&buf, 0, sizeof(buf)); |
| 1071 | |
| 1072 | buf.param.psize = sizeof(uint32_t); |
| 1073 | buf.param.vsize = sizeof(uint32_t); |
| 1074 | buf.data_0 = AEC_PARAM_ECHO_DELAY; |
| 1075 | buf.data_1 = delay_us; |
| 1076 | |
| 1077 | return set_preprocessor_param(handle, &buf.param); |
| 1078 | } |
| 1079 | |
| 1080 | static void push_echo_reference(struct stream_in *in, size_t frames) |
| 1081 | { |
| 1082 | ALOGVV("%s: enter:)", __func__); |
| 1083 | /* read frames from echo reference buffer and update echo delay |
| 1084 | * in->ref_buf_frames is updated with frames available in in->ref_buf */ |
| 1085 | |
| 1086 | int32_t delay_us = update_echo_reference(in, frames)/1000; |
| 1087 | int32_t size_in_bytes = 0; |
| 1088 | int i; |
| 1089 | audio_buffer_t buf; |
| 1090 | |
| 1091 | if (in->ref_buf_frames < frames) |
| 1092 | frames = in->ref_buf_frames; |
| 1093 | |
| 1094 | buf.frameCount = frames; |
| 1095 | buf.raw = in->ref_buf; |
| 1096 | |
| 1097 | for (i = 0; i < in->num_preprocessors; i++) { |
| 1098 | if ((*in->preprocessors[i].effect_itfe)->process_reverse == NULL) |
| 1099 | continue; |
| 1100 | ALOGVV("%s: effect_itfe)->process_reverse() BEGIN i=(%d) ", __func__, i); |
| 1101 | (*in->preprocessors[i].effect_itfe)->process_reverse(in->preprocessors[i].effect_itfe, |
| 1102 | &buf, |
| 1103 | NULL); |
| 1104 | ALOGVV("%s: effect_itfe)->process_reverse() END i=(%d) ", __func__, i); |
| 1105 | set_preprocessor_echo_delay(in->preprocessors[i].effect_itfe, delay_us); |
| 1106 | } |
| 1107 | |
| 1108 | in->ref_buf_frames -= buf.frameCount; |
| 1109 | ALOGVV("%s: in->ref_buf_frames(%zd), in->config.channels(%d) ", |
| 1110 | __func__, in->ref_buf_frames, in->config.channels); |
| 1111 | if (in->ref_buf_frames) { |
| 1112 | memcpy(in->ref_buf, |
| 1113 | in->ref_buf + buf.frameCount * in->config.channels, |
| 1114 | in->ref_buf_frames * in->config.channels * sizeof(int16_t)); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | static void put_echo_reference(struct audio_device *adev, |
| 1119 | struct echo_reference_itfe *reference) |
| 1120 | { |
| 1121 | ALOGV("%s: enter:)", __func__); |
| 1122 | int32_t prev_generation = adev->echo_reference_generation; |
| 1123 | struct stream_out *out = adev->primary_output; |
| 1124 | |
| 1125 | if (adev->echo_reference != NULL && |
| 1126 | reference == adev->echo_reference) { |
| 1127 | /* echo reference is taken from the low latency output stream used |
| 1128 | * for voice use cases */ |
| 1129 | adev->echo_reference = NULL; |
| 1130 | android_atomic_inc(&adev->echo_reference_generation); |
| 1131 | if (out != NULL && out->usecase == USECASE_AUDIO_PLAYBACK) { |
| 1132 | // if the primary output is in standby or did not pick the echo reference yet |
| 1133 | // we can safely get rid of it here. |
| 1134 | // otherwise, out_write() or out_standby() will detect the change in echo reference |
| 1135 | // generation and release the echo reference owned by the stream. |
| 1136 | if ((out->echo_reference_generation != prev_generation) || out->standby) |
| 1137 | release_echo_reference(reference); |
| 1138 | } else { |
| 1139 | release_echo_reference(reference); |
| 1140 | } |
| 1141 | ALOGV("release_echo_reference"); |
| 1142 | } |
| 1143 | } |
| 1144 | |
| 1145 | static struct echo_reference_itfe *get_echo_reference(struct audio_device *adev, |
| 1146 | audio_format_t format __unused, |
| 1147 | uint32_t channel_count, |
| 1148 | uint32_t sampling_rate) |
| 1149 | { |
| 1150 | ALOGV("%s: enter:)", __func__); |
| 1151 | put_echo_reference(adev, adev->echo_reference); |
| 1152 | /* echo reference is taken from the low latency output stream used |
| 1153 | * for voice use cases */ |
| 1154 | if (adev->primary_output!= NULL && adev->primary_output->usecase == USECASE_AUDIO_PLAYBACK && |
| 1155 | !adev->primary_output->standby) { |
| 1156 | struct audio_stream *stream = |
| 1157 | &adev->primary_output->stream.common; |
| 1158 | uint32_t wr_channel_count = audio_channel_count_from_out_mask(stream->get_channels(stream)); |
| 1159 | uint32_t wr_sampling_rate = stream->get_sample_rate(stream); |
| 1160 | ALOGV("Calling create_echo_reference"); |
| 1161 | int status = create_echo_reference(AUDIO_FORMAT_PCM_16_BIT, |
| 1162 | channel_count, |
| 1163 | sampling_rate, |
| 1164 | AUDIO_FORMAT_PCM_16_BIT, |
| 1165 | wr_channel_count, |
| 1166 | wr_sampling_rate, |
| 1167 | &adev->echo_reference); |
| 1168 | if (status == 0) |
| 1169 | android_atomic_inc(&adev->echo_reference_generation); |
| 1170 | } |
| 1171 | return adev->echo_reference; |
| 1172 | } |
| 1173 | |
| 1174 | #ifdef HW_AEC_LOOPBACK |
| 1175 | static int get_hw_echo_reference(struct stream_in *in) |
| 1176 | { |
| 1177 | struct pcm_device_profile *ref_pcm_profile; |
| 1178 | struct pcm_device *ref_device; |
| 1179 | struct audio_device *adev = in->dev; |
| 1180 | |
| 1181 | in->hw_echo_reference = false; |
| 1182 | |
| 1183 | if (adev->primary_output!= NULL && |
| 1184 | !adev->primary_output->standby && |
| 1185 | adev->primary_output->usecase == USECASE_AUDIO_PLAYBACK && |
| 1186 | adev->primary_output->devices == AUDIO_DEVICE_OUT_SPEAKER) { |
| 1187 | struct audio_stream *stream = &adev->primary_output->stream.common; |
| 1188 | |
| 1189 | // TODO: currently there is no low latency mode for aec reference. |
| 1190 | ref_pcm_profile = get_pcm_device(PCM_CAPTURE, pcm_device_capture_loopback_aec.devices); |
| 1191 | if (ref_pcm_profile == NULL) { |
| 1192 | ALOGE("%s: Could not find PCM device id for the usecase(%d)", |
| 1193 | __func__, pcm_device_capture_loopback_aec.devices); |
| 1194 | return -EINVAL; |
| 1195 | } |
| 1196 | |
| 1197 | ref_device = (struct pcm_device *)calloc(1, sizeof(struct pcm_device)); |
| 1198 | ref_device->pcm_profile = ref_pcm_profile; |
| 1199 | |
| 1200 | ALOGV("%s: ref_device rate:%d, ch:%d", __func__, ref_pcm_profile->config.rate, ref_pcm_profile->config.channels); |
| 1201 | ref_device->pcm = pcm_open(ref_device->pcm_profile->card, ref_device->pcm_profile->id, PCM_IN | PCM_MONOTONIC, &ref_device->pcm_profile->config); |
| 1202 | |
| 1203 | if (ref_device->pcm && !pcm_is_ready(ref_device->pcm)) { |
| 1204 | ALOGE("%s: %s", __func__, pcm_get_error(ref_device->pcm)); |
| 1205 | pcm_close(ref_device->pcm); |
| 1206 | ref_device->pcm = NULL; |
| 1207 | return -EIO; |
| 1208 | } |
| 1209 | list_add_tail(&in->pcm_dev_list, &ref_device->stream_list_node); |
| 1210 | |
| 1211 | in->hw_echo_reference = true; |
| 1212 | |
| 1213 | ALOGV("%s: hw_echo_reference is true", __func__); |
| 1214 | } |
| 1215 | |
| 1216 | return 0; |
| 1217 | } |
| 1218 | #endif |
| 1219 | |
| 1220 | static int get_playback_delay(struct stream_out *out, |
| 1221 | size_t frames, |
| 1222 | struct echo_reference_buffer *buffer) |
| 1223 | { |
| 1224 | unsigned int kernel_frames; |
| 1225 | int status; |
| 1226 | int primary_pcm = 0; |
| 1227 | struct pcm_device *pcm_device; |
| 1228 | |
| 1229 | pcm_device = node_to_item(list_head(&out->pcm_dev_list), |
| 1230 | struct pcm_device, stream_list_node); |
| 1231 | |
| 1232 | status = pcm_get_htimestamp(pcm_device->pcm, &kernel_frames, &buffer->time_stamp); |
| 1233 | if (status < 0) { |
| 1234 | buffer->time_stamp.tv_sec = 0; |
| 1235 | buffer->time_stamp.tv_nsec = 0; |
| 1236 | buffer->delay_ns = 0; |
| 1237 | ALOGV("get_playback_delay(): pcm_get_htimestamp error," |
| 1238 | "setting playbackTimestamp to 0"); |
| 1239 | return status; |
| 1240 | } |
| 1241 | |
| 1242 | kernel_frames = pcm_get_buffer_size(pcm_device->pcm) - kernel_frames; |
| 1243 | |
| 1244 | /* adjust render time stamp with delay added by current driver buffer. |
| 1245 | * Add the duration of current frame as we want the render time of the last |
| 1246 | * sample being written. */ |
| 1247 | buffer->delay_ns = (long)(((int64_t)(kernel_frames + frames)* 1000000000)/ |
| 1248 | out->config.rate); |
| 1249 | ALOGVV("get_playback_delay_time_stamp Secs: [%10ld], nSecs: [%9ld], kernel_frames: [%5u], delay_ns: [%d],", |
| 1250 | buffer->time_stamp.tv_sec, buffer->time_stamp.tv_nsec, kernel_frames, buffer->delay_ns); |
| 1251 | |
| 1252 | return 0; |
| 1253 | } |
| 1254 | |
| 1255 | #define GET_COMMAND_STATUS(status, fct_status, cmd_status) \ |
| 1256 | do { \ |
| 1257 | if (fct_status != 0) \ |
| 1258 | status = fct_status; \ |
| 1259 | else if (cmd_status != 0) \ |
| 1260 | status = cmd_status; \ |
| 1261 | } while(0) |
| 1262 | |
| 1263 | static int in_configure_reverse(struct stream_in *in) |
| 1264 | { |
| 1265 | int32_t cmd_status; |
| 1266 | uint32_t size = sizeof(int); |
| 1267 | effect_config_t config; |
| 1268 | int32_t status = 0; |
| 1269 | int32_t fct_status = 0; |
| 1270 | int i; |
| 1271 | ALOGV("%s: enter: in->num_preprocessors(%d)", __func__, in->num_preprocessors); |
| 1272 | if (in->num_preprocessors > 0) { |
| 1273 | config.inputCfg.channels = in->main_channels; |
| 1274 | config.outputCfg.channels = in->main_channels; |
| 1275 | config.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1276 | config.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; |
| 1277 | config.inputCfg.samplingRate = in->requested_rate; |
| 1278 | config.outputCfg.samplingRate = in->requested_rate; |
| 1279 | config.inputCfg.mask = |
| 1280 | ( EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT ); |
| 1281 | config.outputCfg.mask = |
| 1282 | ( EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT ); |
| 1283 | |
| 1284 | for (i = 0; i < in->num_preprocessors; i++) |
| 1285 | { |
| 1286 | if ((*in->preprocessors[i].effect_itfe)->process_reverse == NULL) |
| 1287 | continue; |
| 1288 | fct_status = (*(in->preprocessors[i].effect_itfe))->command( |
| 1289 | in->preprocessors[i].effect_itfe, |
| 1290 | EFFECT_CMD_SET_CONFIG_REVERSE, |
| 1291 | sizeof(effect_config_t), |
| 1292 | &config, |
| 1293 | &size, |
| 1294 | &cmd_status); |
| 1295 | ALOGV("%s: calling EFFECT_CMD_SET_CONFIG_REVERSE",__func__); |
| 1296 | GET_COMMAND_STATUS(status, fct_status, cmd_status); |
| 1297 | } |
| 1298 | } |
| 1299 | return status; |
| 1300 | } |
| 1301 | |
| 1302 | #define MAX_NUM_CHANNEL_CONFIGS 10 |
| 1303 | |
| 1304 | static void in_read_audio_effect_channel_configs(struct stream_in *in __unused, |
| 1305 | struct effect_info_s *effect_info) |
| 1306 | { |
| 1307 | /* size and format of the cmd are defined in hardware/audio_effect.h */ |
| 1308 | effect_handle_t effect = effect_info->effect_itfe; |
| 1309 | uint32_t cmd_size = 2 * sizeof(uint32_t); |
| 1310 | uint32_t cmd[] = { EFFECT_FEATURE_AUX_CHANNELS, MAX_NUM_CHANNEL_CONFIGS }; |
| 1311 | /* reply = status + number of configs (n) + n x channel_config_t */ |
| 1312 | uint32_t reply_size = |
| 1313 | 2 * sizeof(uint32_t) + (MAX_NUM_CHANNEL_CONFIGS * sizeof(channel_config_t)); |
| 1314 | int32_t reply[reply_size]; |
| 1315 | int32_t cmd_status; |
| 1316 | |
| 1317 | ALOG_ASSERT((effect_info->num_channel_configs == 0), |
| 1318 | "in_read_audio_effect_channel_configs() num_channel_configs not cleared"); |
| 1319 | ALOG_ASSERT((effect_info->channel_configs == NULL), |
| 1320 | "in_read_audio_effect_channel_configs() channel_configs not cleared"); |
| 1321 | |
| 1322 | /* if this command is not supported, then the effect is supposed to return -EINVAL. |
| 1323 | * This error will be interpreted as if the effect supports the main_channels but does not |
| 1324 | * support any aux_channels */ |
| 1325 | cmd_status = (*effect)->command(effect, |
| 1326 | EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS, |
| 1327 | cmd_size, |
| 1328 | (void*)&cmd, |
| 1329 | &reply_size, |
| 1330 | (void*)&reply); |
| 1331 | |
| 1332 | if (cmd_status != 0) { |
| 1333 | ALOGV("in_read_audio_effect_channel_configs(): " |
| 1334 | "fx->command returned %d", cmd_status); |
| 1335 | return; |
| 1336 | } |
| 1337 | |
| 1338 | if (reply[0] != 0) { |
| 1339 | ALOGW("in_read_audio_effect_channel_configs(): " |
| 1340 | "command EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS error %d num configs %d", |
| 1341 | reply[0], (reply[0] == -ENOMEM) ? reply[1] : MAX_NUM_CHANNEL_CONFIGS); |
| 1342 | return; |
| 1343 | } |
| 1344 | |
| 1345 | /* the feature is not supported */ |
| 1346 | ALOGV("in_read_audio_effect_channel_configs()(): " |
| 1347 | "Feature supported and adding %d channel configs to the list", reply[1]); |
| 1348 | effect_info->num_channel_configs = reply[1]; |
| 1349 | effect_info->channel_configs = |
| 1350 | (channel_config_t *) malloc(sizeof(channel_config_t) * reply[1]); /* n x configs */ |
| 1351 | memcpy(effect_info->channel_configs, (reply + 2), sizeof(channel_config_t) * reply[1]); |
| 1352 | } |
| 1353 | |
| 1354 | |
| 1355 | #define NUM_IN_AUX_CNL_CONFIGS 2 |
| 1356 | static const channel_config_t in_aux_cnl_configs[NUM_IN_AUX_CNL_CONFIGS] = { |
| 1357 | { AUDIO_CHANNEL_IN_FRONT , AUDIO_CHANNEL_IN_BACK}, |
| 1358 | { AUDIO_CHANNEL_IN_STEREO , AUDIO_CHANNEL_IN_RIGHT} |
| 1359 | }; |
| 1360 | static uint32_t in_get_aux_channels(struct stream_in *in) |
| 1361 | { |
| 1362 | int i; |
| 1363 | channel_config_t new_chcfg = {0, 0}; |
| 1364 | |
| 1365 | if (in->num_preprocessors == 0) |
| 1366 | return 0; |
| 1367 | |
| 1368 | /* do not enable dual mic configurations when capturing from other microphones than |
| 1369 | * main or sub */ |
| 1370 | if (!(in->devices & (AUDIO_DEVICE_IN_BUILTIN_MIC | AUDIO_DEVICE_IN_BACK_MIC))) |
| 1371 | return 0; |
| 1372 | |
| 1373 | /* retain most complex aux channels configuration compatible with requested main channels and |
| 1374 | * supported by audio driver and all pre processors */ |
| 1375 | for (i = 0; i < NUM_IN_AUX_CNL_CONFIGS; i++) { |
| 1376 | const channel_config_t *cur_chcfg = &in_aux_cnl_configs[i]; |
| 1377 | if (cur_chcfg->main_channels == in->main_channels) { |
| 1378 | size_t match_cnt; |
| 1379 | size_t idx_preproc; |
| 1380 | for (idx_preproc = 0, match_cnt = 0; |
| 1381 | /* no need to continue if at least one preprocessor doesn't match */ |
| 1382 | idx_preproc < (size_t)in->num_preprocessors && match_cnt == idx_preproc; |
| 1383 | idx_preproc++) { |
| 1384 | struct effect_info_s *effect_info = &in->preprocessors[idx_preproc]; |
| 1385 | size_t idx_chcfg; |
| 1386 | |
| 1387 | for (idx_chcfg = 0; idx_chcfg < effect_info->num_channel_configs; idx_chcfg++) { |
| 1388 | if (memcmp(effect_info->channel_configs + idx_chcfg, |
| 1389 | cur_chcfg, |
| 1390 | sizeof(channel_config_t)) == 0) { |
| 1391 | match_cnt++; |
| 1392 | break; |
| 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | /* if all preprocessors match, we have a candidate */ |
| 1397 | if (match_cnt == (size_t)in->num_preprocessors) { |
| 1398 | /* retain most complex aux channels configuration */ |
| 1399 | if (audio_channel_count_from_in_mask(cur_chcfg->aux_channels) > audio_channel_count_from_in_mask(new_chcfg.aux_channels)) { |
| 1400 | new_chcfg = *cur_chcfg; |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | } |
| 1405 | |
| 1406 | ALOGV("in_get_aux_channels(): return %04x", new_chcfg.aux_channels); |
| 1407 | |
| 1408 | return new_chcfg.aux_channels; |
| 1409 | } |
| 1410 | |
| 1411 | static int in_configure_effect_channels(effect_handle_t effect, |
| 1412 | channel_config_t *channel_config) |
| 1413 | { |
| 1414 | int status = 0; |
| 1415 | int fct_status; |
| 1416 | int32_t cmd_status; |
| 1417 | uint32_t reply_size; |
| 1418 | effect_config_t config; |
| 1419 | uint32_t cmd[(sizeof(uint32_t) + sizeof(channel_config_t) - 1) / sizeof(uint32_t) + 1]; |
| 1420 | |
| 1421 | ALOGV("in_configure_effect_channels(): configure effect with channels: [%04x][%04x]", |
| 1422 | channel_config->main_channels, |
| 1423 | channel_config->aux_channels); |
| 1424 | |
| 1425 | config.inputCfg.mask = EFFECT_CONFIG_CHANNELS; |
| 1426 | config.outputCfg.mask = EFFECT_CONFIG_CHANNELS; |
| 1427 | reply_size = sizeof(effect_config_t); |
| 1428 | fct_status = (*effect)->command(effect, |
| 1429 | EFFECT_CMD_GET_CONFIG, |
| 1430 | 0, |
| 1431 | NULL, |
| 1432 | &reply_size, |
| 1433 | &config); |
| 1434 | if (fct_status != 0) { |
| 1435 | ALOGE("in_configure_effect_channels(): EFFECT_CMD_GET_CONFIG failed"); |
| 1436 | return fct_status; |
| 1437 | } |
| 1438 | |
| 1439 | config.inputCfg.channels = channel_config->main_channels | channel_config->aux_channels; |
| 1440 | config.outputCfg.channels = config.inputCfg.channels; |
| 1441 | reply_size = sizeof(uint32_t); |
| 1442 | fct_status = (*effect)->command(effect, |
| 1443 | EFFECT_CMD_SET_CONFIG, |
| 1444 | sizeof(effect_config_t), |
| 1445 | &config, |
| 1446 | &reply_size, |
| 1447 | &cmd_status); |
| 1448 | GET_COMMAND_STATUS(status, fct_status, cmd_status); |
| 1449 | |
| 1450 | cmd[0] = EFFECT_FEATURE_AUX_CHANNELS; |
| 1451 | memcpy(cmd + 1, channel_config, sizeof(channel_config_t)); |
| 1452 | reply_size = sizeof(uint32_t); |
| 1453 | fct_status = (*effect)->command(effect, |
| 1454 | EFFECT_CMD_SET_FEATURE_CONFIG, |
| 1455 | sizeof(cmd), //sizeof(uint32_t) + sizeof(channel_config_t), |
| 1456 | cmd, |
| 1457 | &reply_size, |
| 1458 | &cmd_status); |
| 1459 | GET_COMMAND_STATUS(status, fct_status, cmd_status); |
| 1460 | |
| 1461 | /* some implementations need to be re-enabled after a config change */ |
| 1462 | reply_size = sizeof(uint32_t); |
| 1463 | fct_status = (*effect)->command(effect, |
| 1464 | EFFECT_CMD_ENABLE, |
| 1465 | 0, |
| 1466 | NULL, |
| 1467 | &reply_size, |
| 1468 | &cmd_status); |
| 1469 | GET_COMMAND_STATUS(status, fct_status, cmd_status); |
| 1470 | |
| 1471 | return status; |
| 1472 | } |
| 1473 | |
| 1474 | static int in_reconfigure_channels(struct stream_in *in, |
| 1475 | effect_handle_t effect, |
| 1476 | channel_config_t *channel_config, |
| 1477 | bool config_changed) { |
| 1478 | |
| 1479 | int status = 0; |
| 1480 | |
| 1481 | ALOGV("in_reconfigure_channels(): config_changed %d effect %p", |
| 1482 | config_changed, effect); |
| 1483 | |
| 1484 | /* if config changed, reconfigure all previously added effects */ |
| 1485 | if (config_changed) { |
| 1486 | int i; |
| 1487 | ALOGV("%s: config_changed (%d)", __func__, config_changed); |
| 1488 | for (i = 0; i < in->num_preprocessors; i++) |
| 1489 | { |
| 1490 | int cur_status = in_configure_effect_channels(in->preprocessors[i].effect_itfe, |
| 1491 | channel_config); |
| 1492 | ALOGV("%s: in_configure_effect_channels i=(%d), [main_channel,aux_channel]=[%d|%d], status=%d", |
| 1493 | __func__, i, channel_config->main_channels, channel_config->aux_channels, cur_status); |
| 1494 | if (cur_status != 0) { |
| 1495 | ALOGV("in_reconfigure_channels(): error %d configuring effect " |
| 1496 | "%d with channels: [%04x][%04x]", |
| 1497 | cur_status, |
| 1498 | i, |
| 1499 | channel_config->main_channels, |
| 1500 | channel_config->aux_channels); |
| 1501 | status = cur_status; |
| 1502 | } |
| 1503 | } |
| 1504 | } else if (effect != NULL && channel_config->aux_channels) { |
| 1505 | /* if aux channels config did not change but aux channels are present, |
| 1506 | * we still need to configure the effect being added */ |
| 1507 | status = in_configure_effect_channels(effect, channel_config); |
| 1508 | } |
| 1509 | return status; |
| 1510 | } |
| 1511 | |
| 1512 | static void in_update_aux_channels(struct stream_in *in, |
| 1513 | effect_handle_t effect) |
| 1514 | { |
| 1515 | uint32_t aux_channels; |
| 1516 | channel_config_t channel_config; |
| 1517 | int status; |
| 1518 | |
| 1519 | aux_channels = in_get_aux_channels(in); |
| 1520 | |
| 1521 | channel_config.main_channels = in->main_channels; |
| 1522 | channel_config.aux_channels = aux_channels; |
| 1523 | status = in_reconfigure_channels(in, |
| 1524 | effect, |
| 1525 | &channel_config, |
| 1526 | (aux_channels != in->aux_channels)); |
| 1527 | |
| 1528 | if (status != 0) { |
| 1529 | ALOGV("in_update_aux_channels(): in_reconfigure_channels error %d", status); |
| 1530 | /* resetting aux channels configuration */ |
| 1531 | aux_channels = 0; |
| 1532 | channel_config.aux_channels = 0; |
| 1533 | in_reconfigure_channels(in, effect, &channel_config, true); |
| 1534 | } |
| 1535 | ALOGV("%s: aux_channels=%d, in->aux_channels_changed=%d", __func__, aux_channels, in->aux_channels_changed); |
| 1536 | if (in->aux_channels != aux_channels) { |
| 1537 | in->aux_channels_changed = true; |
| 1538 | in->aux_channels = aux_channels; |
| 1539 | do_in_standby_l(in); |
| 1540 | } |
| 1541 | } |
| 1542 | #endif |
| 1543 | |
| 1544 | /* This function reads PCM data and: |
| 1545 | * - resample if needed |
| 1546 | * - process if pre-processors are attached |
| 1547 | * - discard unwanted channels |
| 1548 | */ |
| 1549 | static ssize_t read_and_process_frames(struct stream_in *in, void* buffer, ssize_t frames) |
| 1550 | { |
| 1551 | ssize_t frames_wr = 0; |
| 1552 | audio_buffer_t in_buf; |
| 1553 | audio_buffer_t out_buf; |
| 1554 | size_t src_channels = in->config.channels; |
| 1555 | size_t dst_channels = audio_channel_count_from_in_mask(in->main_channels); |
| 1556 | int i; |
| 1557 | void *proc_buf_out; |
| 1558 | struct pcm_device *pcm_device; |
| 1559 | bool has_additional_channels = (dst_channels != src_channels) ? true : false; |
| 1560 | #ifdef PREPROCESSING_ENABLED |
| 1561 | bool has_processing = (in->num_preprocessors != 0) ? true : false; |
| 1562 | #endif |
| 1563 | |
| 1564 | /* Additional channels might be added on top of main_channels: |
| 1565 | * - aux_channels (by processing effects) |
| 1566 | * - extra channels due to HW limitations |
| 1567 | * In case of additional channels, we cannot work inplace |
| 1568 | */ |
| 1569 | if (has_additional_channels) |
| 1570 | proc_buf_out = in->proc_buf_out; |
| 1571 | else |
| 1572 | proc_buf_out = buffer; |
| 1573 | |
| 1574 | if (list_empty(&in->pcm_dev_list)) { |
| 1575 | ALOGE("%s: pcm device list empty", __func__); |
| 1576 | return -EINVAL; |
| 1577 | } |
| 1578 | |
| 1579 | pcm_device = node_to_item(list_head(&in->pcm_dev_list), |
| 1580 | struct pcm_device, stream_list_node); |
| 1581 | |
| 1582 | #ifdef PREPROCESSING_ENABLED |
| 1583 | if (has_processing) { |
| 1584 | /* since all the processing below is done in frames and using the config.channels |
| 1585 | * as the number of channels, no changes is required in case aux_channels are present */ |
| 1586 | while (frames_wr < frames) { |
| 1587 | /* first reload enough frames at the end of process input buffer */ |
| 1588 | if (in->proc_buf_frames < (size_t)frames) { |
| 1589 | ssize_t frames_rd; |
| 1590 | if (in->proc_buf_size < (size_t)frames) { |
| 1591 | size_t size_in_bytes = pcm_frames_to_bytes(pcm_device->pcm, frames); |
| 1592 | in->proc_buf_size = (size_t)frames; |
| 1593 | in->proc_buf_in = (int16_t *)realloc(in->proc_buf_in, size_in_bytes); |
| 1594 | ALOG_ASSERT((in->proc_buf_in != NULL), |
| 1595 | "process_frames() failed to reallocate proc_buf_in"); |
| 1596 | if (has_additional_channels) { |
| 1597 | in->proc_buf_out = (int16_t *)realloc(in->proc_buf_out, size_in_bytes); |
| 1598 | ALOG_ASSERT((in->proc_buf_out != NULL), |
| 1599 | "process_frames() failed to reallocate proc_buf_out"); |
| 1600 | proc_buf_out = in->proc_buf_out; |
| 1601 | } |
| 1602 | } |
| 1603 | frames_rd = read_frames(in, |
| 1604 | in->proc_buf_in + |
| 1605 | in->proc_buf_frames * in->config.channels, |
| 1606 | frames - in->proc_buf_frames); |
| 1607 | if (frames_rd < 0) { |
| 1608 | /* Return error code */ |
| 1609 | frames_wr = frames_rd; |
| 1610 | break; |
| 1611 | } |
| 1612 | in->proc_buf_frames += frames_rd; |
| 1613 | } |
| 1614 | |
| 1615 | if (in->echo_reference != NULL) { |
| 1616 | push_echo_reference(in, in->proc_buf_frames); |
| 1617 | } |
| 1618 | |
| 1619 | /* in_buf.frameCount and out_buf.frameCount indicate respectively |
| 1620 | * the maximum number of frames to be consumed and produced by process() */ |
| 1621 | in_buf.frameCount = in->proc_buf_frames; |
| 1622 | in_buf.s16 = in->proc_buf_in; |
| 1623 | out_buf.frameCount = frames - frames_wr; |
| 1624 | out_buf.s16 = (int16_t *)proc_buf_out + frames_wr * in->config.channels; |
| 1625 | |
| 1626 | /* FIXME: this works because of current pre processing library implementation that |
| 1627 | * does the actual process only when the last enabled effect process is called. |
| 1628 | * The generic solution is to have an output buffer for each effect and pass it as |
| 1629 | * input to the next. |
| 1630 | */ |
| 1631 | for (i = 0; i < in->num_preprocessors; i++) { |
| 1632 | (*in->preprocessors[i].effect_itfe)->process(in->preprocessors[i].effect_itfe, |
| 1633 | &in_buf, |
| 1634 | &out_buf); |
| 1635 | } |
| 1636 | |
| 1637 | /* process() has updated the number of frames consumed and produced in |
| 1638 | * in_buf.frameCount and out_buf.frameCount respectively |
| 1639 | * move remaining frames to the beginning of in->proc_buf_in */ |
| 1640 | in->proc_buf_frames -= in_buf.frameCount; |
| 1641 | |
| 1642 | if (in->proc_buf_frames) { |
| 1643 | memcpy(in->proc_buf_in, |
| 1644 | in->proc_buf_in + in_buf.frameCount * in->config.channels, |
| 1645 | in->proc_buf_frames * in->config.channels * sizeof(int16_t)); |
| 1646 | } |
| 1647 | |
| 1648 | /* if not enough frames were passed to process(), read more and retry. */ |
| 1649 | if (out_buf.frameCount == 0) { |
| 1650 | ALOGW("No frames produced by preproc"); |
| 1651 | continue; |
| 1652 | } |
| 1653 | |
| 1654 | if ((frames_wr + (ssize_t)out_buf.frameCount) <= frames) { |
| 1655 | frames_wr += out_buf.frameCount; |
| 1656 | } else { |
| 1657 | /* The effect does not comply to the API. In theory, we should never end up here! */ |
| 1658 | ALOGE("preprocessing produced too many frames: %d + %zd > %d !", |
| 1659 | (unsigned int)frames_wr, out_buf.frameCount, (unsigned int)frames); |
| 1660 | frames_wr = frames; |
| 1661 | } |
| 1662 | } |
| 1663 | } |
| 1664 | else |
| 1665 | #endif //PREPROCESSING_ENABLED |
| 1666 | { |
| 1667 | /* No processing effects attached */ |
| 1668 | if (has_additional_channels) { |
| 1669 | /* With additional channels, we cannot use original buffer */ |
| 1670 | if (in->proc_buf_size < (size_t)frames) { |
| 1671 | size_t size_in_bytes = pcm_frames_to_bytes(pcm_device->pcm, frames); |
| 1672 | in->proc_buf_size = (size_t)frames; |
| 1673 | in->proc_buf_out = (int16_t *)realloc(in->proc_buf_out, size_in_bytes); |
| 1674 | ALOG_ASSERT((in->proc_buf_out != NULL), |
| 1675 | "process_frames() failed to reallocate proc_buf_out"); |
| 1676 | proc_buf_out = in->proc_buf_out; |
| 1677 | } |
| 1678 | } |
| 1679 | frames_wr = read_frames(in, proc_buf_out, frames); |
| 1680 | } |
| 1681 | |
| 1682 | /* Remove all additional channels that have been added on top of main_channels: |
| 1683 | * - aux_channels |
| 1684 | * - extra channels from HW due to HW limitations |
| 1685 | * Assumption is made that the channels are interleaved and that the main |
| 1686 | * channels are first. */ |
| 1687 | |
| 1688 | if (has_additional_channels) |
| 1689 | { |
| 1690 | int16_t* src_buffer = (int16_t *)proc_buf_out; |
| 1691 | int16_t* dst_buffer = (int16_t *)buffer; |
| 1692 | |
| 1693 | if (dst_channels == 1) { |
| 1694 | for (i = frames_wr; i > 0; i--) |
| 1695 | { |
| 1696 | *dst_buffer++ = *src_buffer; |
| 1697 | src_buffer += src_channels; |
| 1698 | } |
| 1699 | } else { |
| 1700 | for (i = frames_wr; i > 0; i--) |
| 1701 | { |
| 1702 | memcpy(dst_buffer, src_buffer, dst_channels*sizeof(int16_t)); |
| 1703 | dst_buffer += dst_channels; |
| 1704 | src_buffer += src_channels; |
| 1705 | } |
| 1706 | } |
| 1707 | } |
| 1708 | |
| 1709 | return frames_wr; |
| 1710 | } |
| 1711 | |
| 1712 | static int get_next_buffer(struct resampler_buffer_provider *buffer_provider, |
| 1713 | struct resampler_buffer* buffer) |
| 1714 | { |
| 1715 | struct stream_in *in; |
| 1716 | struct pcm_device *pcm_device; |
| 1717 | |
| 1718 | if (buffer_provider == NULL || buffer == NULL) |
| 1719 | return -EINVAL; |
| 1720 | |
| 1721 | in = (struct stream_in *)((char *)buffer_provider - |
| 1722 | offsetof(struct stream_in, buf_provider)); |
| 1723 | |
| 1724 | if (list_empty(&in->pcm_dev_list)) { |
| 1725 | buffer->raw = NULL; |
| 1726 | buffer->frame_count = 0; |
| 1727 | in->read_status = -ENODEV; |
| 1728 | return -ENODEV; |
| 1729 | } |
| 1730 | |
| 1731 | pcm_device = node_to_item(list_head(&in->pcm_dev_list), |
| 1732 | struct pcm_device, stream_list_node); |
| 1733 | |
| 1734 | if (in->read_buf_frames == 0) { |
| 1735 | size_t size_in_bytes = pcm_frames_to_bytes(pcm_device->pcm, in->config.period_size); |
| 1736 | if (in->read_buf_size < in->config.period_size) { |
| 1737 | in->read_buf_size = in->config.period_size; |
| 1738 | in->read_buf = (int16_t *) realloc(in->read_buf, size_in_bytes); |
| 1739 | ALOG_ASSERT((in->read_buf != NULL), |
| 1740 | "get_next_buffer() failed to reallocate read_buf"); |
| 1741 | } |
| 1742 | |
| 1743 | in->read_status = pcm_read(pcm_device->pcm, (void*)in->read_buf, size_in_bytes); |
| 1744 | |
| 1745 | if (in->read_status != 0) { |
| 1746 | ALOGE("get_next_buffer() pcm_read error %d", in->read_status); |
| 1747 | buffer->raw = NULL; |
| 1748 | buffer->frame_count = 0; |
| 1749 | return in->read_status; |
| 1750 | } |
| 1751 | in->read_buf_frames = in->config.period_size; |
| 1752 | |
| 1753 | #ifdef PREPROCESSING_ENABLED |
| 1754 | #ifdef HW_AEC_LOOPBACK |
| 1755 | if (in->hw_echo_reference) { |
| 1756 | struct pcm_device *temp_device = NULL; |
| 1757 | struct pcm_device *ref_device = NULL; |
| 1758 | struct listnode *node = NULL; |
| 1759 | struct echo_reference_buffer b; |
| 1760 | size_t size_hw_ref_bytes; |
| 1761 | size_t size_hw_ref_frames; |
| 1762 | int read_status = 0; |
| 1763 | |
| 1764 | ref_device = node_to_item(list_tail(&in->pcm_dev_list), |
| 1765 | struct pcm_device, stream_list_node); |
| 1766 | list_for_each(node, &in->pcm_dev_list) { |
| 1767 | temp_device = node_to_item(node, struct pcm_device, stream_list_node); |
| 1768 | if (temp_device->pcm_profile->id == 1) { |
| 1769 | ref_device = temp_device; |
| 1770 | break; |
| 1771 | } |
| 1772 | } |
| 1773 | if (ref_device) { |
| 1774 | size_hw_ref_bytes = pcm_frames_to_bytes(ref_device->pcm, ref_device->pcm_profile->config.period_size); |
| 1775 | size_hw_ref_frames = ref_device->pcm_profile->config.period_size; |
| 1776 | if (in->hw_ref_buf_size < size_hw_ref_frames) { |
| 1777 | in->hw_ref_buf_size = size_hw_ref_frames; |
| 1778 | in->hw_ref_buf = (int16_t *) realloc(in->hw_ref_buf, size_hw_ref_bytes); |
| 1779 | ALOG_ASSERT((in->hw_ref_buf != NULL), |
| 1780 | "get_next_buffer() failed to reallocate hw_ref_buf"); |
| 1781 | ALOGV("get_next_buffer(): hw_ref_buf %p extended to %zd bytes", |
| 1782 | in->hw_ref_buf, size_hw_ref_bytes); |
| 1783 | } |
| 1784 | |
| 1785 | read_status = pcm_read(ref_device->pcm, (void*)in->hw_ref_buf, size_hw_ref_bytes); |
| 1786 | if (read_status != 0) { |
| 1787 | ALOGE("process_frames() pcm_read error for HW reference %d", read_status); |
| 1788 | b.raw = NULL; |
| 1789 | b.frame_count = 0; |
| 1790 | } |
| 1791 | else { |
| 1792 | get_capture_reference_delay(in, size_hw_ref_frames, &b); |
| 1793 | b.raw = (void *)in->hw_ref_buf; |
| 1794 | b.frame_count = size_hw_ref_frames; |
| 1795 | if (b.delay_ns != 0) |
| 1796 | b.delay_ns = -b.delay_ns; // as this is capture delay, it needs to be subtracted from the microphone delay |
| 1797 | in->echo_reference->write(in->echo_reference, &b); |
| 1798 | } |
| 1799 | } |
| 1800 | } |
| 1801 | #endif // HW_AEC_LOOPBACK |
| 1802 | #endif // PREPROCESSING_ENABLED |
| 1803 | } |
| 1804 | |
| 1805 | buffer->frame_count = (buffer->frame_count > in->read_buf_frames) ? |
| 1806 | in->read_buf_frames : buffer->frame_count; |
| 1807 | buffer->i16 = in->read_buf + (in->config.period_size - in->read_buf_frames) * |
| 1808 | in->config.channels; |
| 1809 | return in->read_status; |
| 1810 | } |
| 1811 | |
| 1812 | static void release_buffer(struct resampler_buffer_provider *buffer_provider, |
| 1813 | struct resampler_buffer* buffer) |
| 1814 | { |
| 1815 | struct stream_in *in; |
| 1816 | |
| 1817 | if (buffer_provider == NULL || buffer == NULL) |
| 1818 | return; |
| 1819 | |
| 1820 | in = (struct stream_in *)((char *)buffer_provider - |
| 1821 | offsetof(struct stream_in, buf_provider)); |
| 1822 | |
| 1823 | in->read_buf_frames -= buffer->frame_count; |
| 1824 | } |
| 1825 | |
| 1826 | /* read_frames() reads frames from kernel driver, down samples to capture rate |
| 1827 | * if necessary and output the number of frames requested to the buffer specified */ |
| 1828 | static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames) |
| 1829 | { |
| 1830 | ssize_t frames_wr = 0; |
| 1831 | |
| 1832 | struct pcm_device *pcm_device; |
| 1833 | |
| 1834 | if (list_empty(&in->pcm_dev_list)) { |
| 1835 | ALOGE("%s: pcm device list empty", __func__); |
| 1836 | return -EINVAL; |
| 1837 | } |
| 1838 | |
| 1839 | pcm_device = node_to_item(list_head(&in->pcm_dev_list), |
| 1840 | struct pcm_device, stream_list_node); |
| 1841 | |
| 1842 | while (frames_wr < frames) { |
| 1843 | size_t frames_rd = frames - frames_wr; |
| 1844 | ALOGVV("%s: frames_rd: %zd, frames_wr: %zd, in->config.channels: %d", |
| 1845 | __func__,frames_rd,frames_wr,in->config.channels); |
| 1846 | if (in->resampler != NULL) { |
| 1847 | in->resampler->resample_from_provider(in->resampler, |
| 1848 | (int16_t *)((char *)buffer + |
| 1849 | pcm_frames_to_bytes(pcm_device->pcm, frames_wr)), |
| 1850 | &frames_rd); |
| 1851 | } else { |
| 1852 | struct resampler_buffer buf = { |
| 1853 | { raw : NULL, }, |
| 1854 | frame_count : frames_rd, |
| 1855 | }; |
| 1856 | get_next_buffer(&in->buf_provider, &buf); |
| 1857 | if (buf.raw != NULL) { |
| 1858 | memcpy((char *)buffer + |
| 1859 | pcm_frames_to_bytes(pcm_device->pcm, frames_wr), |
| 1860 | buf.raw, |
| 1861 | pcm_frames_to_bytes(pcm_device->pcm, buf.frame_count)); |
| 1862 | frames_rd = buf.frame_count; |
| 1863 | } |
| 1864 | release_buffer(&in->buf_provider, &buf); |
| 1865 | } |
| 1866 | /* in->read_status is updated by getNextBuffer() also called by |
| 1867 | * in->resampler->resample_from_provider() */ |
| 1868 | if (in->read_status != 0) |
| 1869 | return in->read_status; |
| 1870 | |
| 1871 | frames_wr += frames_rd; |
| 1872 | } |
| 1873 | return frames_wr; |
| 1874 | } |
| 1875 | |
| 1876 | static int in_release_pcm_devices(struct stream_in *in) |
| 1877 | { |
| 1878 | struct pcm_device *pcm_device; |
| 1879 | struct listnode *node; |
| 1880 | struct listnode *next; |
| 1881 | |
| 1882 | list_for_each_safe(node, next, &in->pcm_dev_list) { |
| 1883 | pcm_device = node_to_item(node, struct pcm_device, stream_list_node); |
| 1884 | list_remove(node); |
| 1885 | free(pcm_device); |
| 1886 | } |
| 1887 | |
| 1888 | return 0; |
| 1889 | } |
| 1890 | |
| 1891 | static int stop_input_stream(struct stream_in *in) |
| 1892 | { |
| 1893 | struct audio_usecase *uc_info; |
| 1894 | struct audio_device *adev = in->dev; |
| 1895 | |
| 1896 | adev->active_input = NULL; |
| 1897 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
| 1898 | in->usecase, use_case_table[in->usecase]); |
| 1899 | uc_info = get_usecase_from_id(adev, in->usecase); |
| 1900 | if (uc_info == NULL) { |
| 1901 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 1902 | __func__, in->usecase); |
| 1903 | return -EINVAL; |
| 1904 | } |
| 1905 | |
| 1906 | /* Disable the tx device */ |
| 1907 | disable_snd_device(adev, uc_info, uc_info->in_snd_device, true); |
| 1908 | |
| 1909 | list_remove(&uc_info->adev_list_node); |
| 1910 | free(uc_info); |
| 1911 | |
| 1912 | if (list_empty(&in->pcm_dev_list)) { |
| 1913 | ALOGE("%s: pcm device list empty", __func__); |
| 1914 | return -EINVAL; |
| 1915 | } |
| 1916 | |
| 1917 | in_release_pcm_devices(in); |
| 1918 | list_init(&in->pcm_dev_list); |
| 1919 | |
| 1920 | #ifdef HW_AEC_LOOPBACK |
| 1921 | if (in->hw_echo_reference) |
| 1922 | { |
| 1923 | in->hw_echo_reference = false; |
| 1924 | } |
| 1925 | #endif |
| 1926 | |
| 1927 | ALOGV("%s: exit", __func__); |
| 1928 | return 0; |
| 1929 | } |
| 1930 | |
| 1931 | static int start_input_stream(struct stream_in *in) |
| 1932 | { |
| 1933 | /* Enable output device and stream routing controls */ |
| 1934 | int ret = 0; |
| 1935 | bool recreate_resampler = false; |
| 1936 | struct audio_usecase *uc_info; |
| 1937 | struct audio_device *adev = in->dev; |
| 1938 | struct pcm_device_profile *pcm_profile; |
| 1939 | struct pcm_device *pcm_device; |
| 1940 | |
| 1941 | ALOGV("%s: enter: usecase(%d)", __func__, in->usecase); |
| 1942 | adev->active_input = in; |
| 1943 | pcm_profile = get_pcm_device(in->usecase_type, in->devices); |
| 1944 | if (pcm_profile == NULL) { |
| 1945 | ALOGE("%s: Could not find PCM device id for the usecase(%d)", |
| 1946 | __func__, in->usecase); |
| 1947 | ret = -EINVAL; |
| 1948 | goto error_config; |
| 1949 | } |
| 1950 | |
| 1951 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 1952 | uc_info->id = in->usecase; |
| 1953 | uc_info->type = PCM_CAPTURE; |
| 1954 | uc_info->stream = (struct audio_stream *)in; |
| 1955 | uc_info->devices = in->devices; |
| 1956 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 1957 | uc_info->out_snd_device = SND_DEVICE_NONE; |
| 1958 | |
| 1959 | pcm_device = (struct pcm_device *)calloc(1, sizeof(struct pcm_device)); |
| 1960 | pcm_device->pcm_profile = pcm_profile; |
| 1961 | list_init(&in->pcm_dev_list); |
| 1962 | list_add_tail(&in->pcm_dev_list, &pcm_device->stream_list_node); |
| 1963 | |
| 1964 | list_init(&uc_info->mixer_list); |
| 1965 | list_add_tail(&uc_info->mixer_list, |
| 1966 | &adev_get_mixer_for_card(adev, |
| 1967 | pcm_device->pcm_profile->card)->uc_list_node[uc_info->id]); |
| 1968 | |
| 1969 | list_add_tail(&adev->usecase_list, &uc_info->adev_list_node); |
| 1970 | |
| 1971 | select_devices(adev, in->usecase); |
| 1972 | |
| 1973 | /* Config should be updated as profile can be changed between different calls |
| 1974 | * to this function: |
| 1975 | * - Trigger resampler creation |
| 1976 | * - Config needs to be updated */ |
| 1977 | if (in->config.rate != pcm_profile->config.rate) { |
| 1978 | recreate_resampler = true; |
| 1979 | } |
| 1980 | in->config = pcm_profile->config; |
| 1981 | |
| 1982 | #ifdef PREPROCESSING_ENABLED |
| 1983 | if (in->aux_channels_changed) { |
| 1984 | in->config.channels = audio_channel_count_from_in_mask(in->main_channels | in->aux_channels); |
| 1985 | recreate_resampler = true; |
| 1986 | } |
| 1987 | #endif |
| 1988 | |
| 1989 | if (in->requested_rate != in->config.rate) { |
| 1990 | recreate_resampler = true; |
| 1991 | } |
| 1992 | |
| 1993 | if (recreate_resampler) { |
| 1994 | if (in->resampler) { |
| 1995 | release_resampler(in->resampler); |
| 1996 | in->resampler = NULL; |
| 1997 | } |
| 1998 | in->buf_provider.get_next_buffer = get_next_buffer; |
| 1999 | in->buf_provider.release_buffer = release_buffer; |
| 2000 | ret = create_resampler(in->config.rate, |
| 2001 | in->requested_rate, |
| 2002 | in->config.channels, |
| 2003 | RESAMPLER_QUALITY_DEFAULT, |
| 2004 | &in->buf_provider, |
| 2005 | &in->resampler); |
| 2006 | } |
| 2007 | |
| 2008 | #ifdef PREPROCESSING_ENABLED |
| 2009 | if (in->enable_aec && in->echo_reference == NULL) { |
| 2010 | in->echo_reference = get_echo_reference(adev, |
| 2011 | AUDIO_FORMAT_PCM_16_BIT, |
| 2012 | audio_channel_count_from_in_mask(in->main_channels), |
| 2013 | in->requested_rate |
| 2014 | ); |
| 2015 | } |
| 2016 | |
| 2017 | #ifdef HW_AEC_LOOPBACK |
| 2018 | if (in->enable_aec) { |
| 2019 | ret = get_hw_echo_reference(in); |
| 2020 | if (ret!=0) |
| 2021 | goto error_open; |
| 2022 | |
| 2023 | /* force ref buffer reallocation */ |
| 2024 | in->hw_ref_buf_size = 0; |
| 2025 | } |
| 2026 | #endif |
| 2027 | #endif |
| 2028 | |
| 2029 | /* Open the PCM device. |
| 2030 | * The HW is limited to support only the default pcm_profile settings. |
| 2031 | * As such a change in aux_channels will not have an effect. |
| 2032 | */ |
| 2033 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d), channels %d, smp rate %d format %d, \ |
| 2034 | period_size %d", __func__, pcm_device->pcm_profile->card, pcm_device->pcm_profile->id, |
| 2035 | pcm_device->pcm_profile->config.channels,pcm_device->pcm_profile->config.rate, |
| 2036 | pcm_device->pcm_profile->config.format, pcm_device->pcm_profile->config.period_size); |
| 2037 | |
| 2038 | if (pcm_profile->type == PCM_HOTWORD_STREAMING) { |
| 2039 | if (!adev->sound_trigger_open_for_streaming) { |
| 2040 | ALOGE("%s: No handle to sound trigger HAL", __func__); |
| 2041 | ret = -EIO; |
| 2042 | goto error_open; |
| 2043 | } |
| 2044 | pcm_device->pcm = NULL; |
| 2045 | pcm_device->sound_trigger_handle = adev->sound_trigger_open_for_streaming(); |
| 2046 | if (pcm_device->sound_trigger_handle <= 0) { |
| 2047 | ALOGE("%s: Failed to open DSP for streaming", __func__); |
| 2048 | ret = -EIO; |
| 2049 | goto error_open; |
| 2050 | } |
| 2051 | ALOGV("Opened DSP successfully"); |
| 2052 | } else { |
| 2053 | pcm_device->sound_trigger_handle = 0; |
| 2054 | pcm_device->pcm = pcm_open(pcm_device->pcm_profile->card, pcm_device->pcm_profile->id, |
| 2055 | PCM_IN | PCM_MONOTONIC, &pcm_device->pcm_profile->config); |
| 2056 | |
| 2057 | if (pcm_device->pcm && !pcm_is_ready(pcm_device->pcm)) { |
| 2058 | ALOGE("%s: %s", __func__, pcm_get_error(pcm_device->pcm)); |
| 2059 | pcm_close(pcm_device->pcm); |
| 2060 | pcm_device->pcm = NULL; |
| 2061 | ret = -EIO; |
| 2062 | goto error_open; |
| 2063 | } |
| 2064 | } |
| 2065 | |
| 2066 | /* force read and proc buffer reallocation in case of frame size or |
| 2067 | * channel count change */ |
| 2068 | in->proc_buf_frames = 0; |
| 2069 | in->proc_buf_size = 0; |
| 2070 | in->read_buf_size = 0; |
| 2071 | in->read_buf_frames = 0; |
| 2072 | |
| 2073 | /* if no supported sample rate is available, use the resampler */ |
| 2074 | if (in->resampler) { |
| 2075 | in->resampler->reset(in->resampler); |
| 2076 | } |
| 2077 | |
| 2078 | ALOGV("%s: exit", __func__); |
| 2079 | return ret; |
| 2080 | |
| 2081 | error_open: |
| 2082 | if (in->resampler) { |
| 2083 | release_resampler(in->resampler); |
| 2084 | in->resampler = NULL; |
| 2085 | } |
| 2086 | stop_input_stream(in); |
| 2087 | |
| 2088 | error_config: |
| 2089 | ALOGV("%s: exit: status(%d)", __func__, ret); |
| 2090 | adev->active_input = NULL; |
| 2091 | return ret; |
| 2092 | } |
| 2093 | |
| 2094 | void lock_input_stream(struct stream_in *in) |
| 2095 | { |
| 2096 | pthread_mutex_lock(&in->pre_lock); |
| 2097 | pthread_mutex_lock(&in->lock); |
| 2098 | pthread_mutex_unlock(&in->pre_lock); |
| 2099 | } |
| 2100 | |
| 2101 | void lock_output_stream(struct stream_out *out) |
| 2102 | { |
| 2103 | pthread_mutex_lock(&out->pre_lock); |
| 2104 | pthread_mutex_lock(&out->lock); |
| 2105 | pthread_mutex_unlock(&out->pre_lock); |
| 2106 | } |
| 2107 | |
| 2108 | |
| 2109 | /* must be called with out->lock locked */ |
| 2110 | static int send_offload_cmd_l(struct stream_out* out, int command) |
| 2111 | { |
| 2112 | struct offload_cmd *cmd = (struct offload_cmd *)calloc(1, sizeof(struct offload_cmd)); |
| 2113 | |
| 2114 | ALOGVV("%s %d", __func__, command); |
| 2115 | |
| 2116 | cmd->cmd = command; |
| 2117 | list_add_tail(&out->offload_cmd_list, &cmd->node); |
| 2118 | pthread_cond_signal(&out->offload_cond); |
| 2119 | return 0; |
| 2120 | } |
| 2121 | |
| 2122 | /* must be called iwth out->lock locked */ |
| 2123 | static void stop_compressed_output_l(struct stream_out *out) |
| 2124 | { |
| 2125 | out->send_new_metadata = 1; |
| 2126 | if (out->compr != NULL) { |
| 2127 | compress_stop(out->compr); |
| 2128 | while (out->offload_thread_blocked) { |
| 2129 | pthread_cond_wait(&out->cond, &out->lock); |
| 2130 | } |
| 2131 | } |
| 2132 | } |
| 2133 | |
| 2134 | static void *offload_thread_loop(void *context) |
| 2135 | { |
| 2136 | struct stream_out *out = (struct stream_out *) context; |
| 2137 | struct listnode *item; |
| 2138 | |
| 2139 | setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO); |
| 2140 | set_sched_policy(0, SP_FOREGROUND); |
| 2141 | prctl(PR_SET_NAME, (unsigned long)"Offload Callback", 0, 0, 0); |
| 2142 | |
| 2143 | ALOGV("%s", __func__); |
| 2144 | lock_output_stream(out); |
| 2145 | for (;;) { |
| 2146 | struct offload_cmd *cmd = NULL; |
| 2147 | stream_callback_event_t event; |
| 2148 | bool send_callback = false; |
| 2149 | |
| 2150 | ALOGVV("%s offload_cmd_list %d out->offload_state %d", |
| 2151 | __func__, list_empty(&out->offload_cmd_list), |
| 2152 | out->offload_state); |
| 2153 | if (list_empty(&out->offload_cmd_list)) { |
| 2154 | ALOGV("%s SLEEPING", __func__); |
| 2155 | pthread_cond_wait(&out->offload_cond, &out->lock); |
| 2156 | ALOGV("%s RUNNING", __func__); |
| 2157 | continue; |
| 2158 | } |
| 2159 | |
| 2160 | item = list_head(&out->offload_cmd_list); |
| 2161 | cmd = node_to_item(item, struct offload_cmd, node); |
| 2162 | list_remove(item); |
| 2163 | |
| 2164 | ALOGVV("%s STATE %d CMD %d out->compr %p", |
| 2165 | __func__, out->offload_state, cmd->cmd, out->compr); |
| 2166 | |
| 2167 | if (cmd->cmd == OFFLOAD_CMD_EXIT) { |
| 2168 | free(cmd); |
| 2169 | break; |
| 2170 | } |
| 2171 | |
| 2172 | if (out->compr == NULL) { |
| 2173 | ALOGE("%s: Compress handle is NULL", __func__); |
| 2174 | pthread_cond_signal(&out->cond); |
| 2175 | continue; |
| 2176 | } |
| 2177 | out->offload_thread_blocked = true; |
| 2178 | pthread_mutex_unlock(&out->lock); |
| 2179 | send_callback = false; |
| 2180 | switch(cmd->cmd) { |
| 2181 | case OFFLOAD_CMD_WAIT_FOR_BUFFER: |
| 2182 | compress_wait(out->compr, -1); |
| 2183 | send_callback = true; |
| 2184 | event = STREAM_CBK_EVENT_WRITE_READY; |
| 2185 | break; |
| 2186 | case OFFLOAD_CMD_PARTIAL_DRAIN: |
| 2187 | compress_next_track(out->compr); |
| 2188 | compress_partial_drain(out->compr); |
| 2189 | send_callback = true; |
| 2190 | event = STREAM_CBK_EVENT_DRAIN_READY; |
| 2191 | break; |
| 2192 | case OFFLOAD_CMD_DRAIN: |
| 2193 | compress_drain(out->compr); |
| 2194 | send_callback = true; |
| 2195 | event = STREAM_CBK_EVENT_DRAIN_READY; |
| 2196 | break; |
| 2197 | default: |
| 2198 | ALOGE("%s unknown command received: %d", __func__, cmd->cmd); |
| 2199 | break; |
| 2200 | } |
| 2201 | lock_output_stream(out); |
| 2202 | out->offload_thread_blocked = false; |
| 2203 | pthread_cond_signal(&out->cond); |
| 2204 | if (send_callback) { |
| 2205 | out->offload_callback(event, NULL, out->offload_cookie); |
| 2206 | } |
| 2207 | free(cmd); |
| 2208 | } |
| 2209 | |
| 2210 | pthread_cond_signal(&out->cond); |
| 2211 | while (!list_empty(&out->offload_cmd_list)) { |
| 2212 | item = list_head(&out->offload_cmd_list); |
| 2213 | list_remove(item); |
| 2214 | free(node_to_item(item, struct offload_cmd, node)); |
| 2215 | } |
| 2216 | pthread_mutex_unlock(&out->lock); |
| 2217 | |
| 2218 | return NULL; |
| 2219 | } |
| 2220 | |
| 2221 | static int create_offload_callback_thread(struct stream_out *out) |
| 2222 | { |
| 2223 | pthread_cond_init(&out->offload_cond, (const pthread_condattr_t *) NULL); |
| 2224 | list_init(&out->offload_cmd_list); |
| 2225 | pthread_create(&out->offload_thread, (const pthread_attr_t *) NULL, |
| 2226 | offload_thread_loop, out); |
| 2227 | return 0; |
| 2228 | } |
| 2229 | |
| 2230 | static int destroy_offload_callback_thread(struct stream_out *out) |
| 2231 | { |
| 2232 | lock_output_stream(out); |
| 2233 | send_offload_cmd_l(out, OFFLOAD_CMD_EXIT); |
| 2234 | |
| 2235 | pthread_mutex_unlock(&out->lock); |
| 2236 | pthread_join(out->offload_thread, (void **) NULL); |
| 2237 | pthread_cond_destroy(&out->offload_cond); |
| 2238 | |
| 2239 | return 0; |
| 2240 | } |
| 2241 | |
| 2242 | static int uc_release_pcm_devices(struct audio_usecase *usecase) |
| 2243 | { |
| 2244 | struct stream_out *out = (struct stream_out *)usecase->stream; |
| 2245 | struct pcm_device *pcm_device; |
| 2246 | struct listnode *node; |
| 2247 | struct listnode *next; |
| 2248 | |
| 2249 | list_for_each_safe(node, next, &out->pcm_dev_list) { |
| 2250 | pcm_device = node_to_item(node, struct pcm_device, stream_list_node); |
| 2251 | list_remove(node); |
| 2252 | free(pcm_device); |
| 2253 | } |
| 2254 | list_init(&usecase->mixer_list); |
| 2255 | |
| 2256 | return 0; |
| 2257 | } |
| 2258 | |
| 2259 | static int uc_select_pcm_devices(struct audio_usecase *usecase) |
| 2260 | |
| 2261 | { |
| 2262 | struct stream_out *out = (struct stream_out *)usecase->stream; |
| 2263 | struct pcm_device *pcm_device; |
| 2264 | struct pcm_device_profile *pcm_profile; |
| 2265 | struct mixer_card *mixer_card; |
| 2266 | audio_devices_t devices = usecase->devices; |
| 2267 | |
| 2268 | list_init(&usecase->mixer_list); |
| 2269 | list_init(&out->pcm_dev_list); |
| 2270 | |
| 2271 | while ((pcm_profile = get_pcm_device(usecase->type, devices)) != NULL) { |
| 2272 | pcm_device = calloc(1, sizeof(struct pcm_device)); |
| 2273 | pcm_device->pcm_profile = pcm_profile; |
| 2274 | list_add_tail(&out->pcm_dev_list, &pcm_device->stream_list_node); |
| 2275 | mixer_card = uc_get_mixer_for_card(usecase, pcm_profile->card); |
| 2276 | if (mixer_card == NULL) { |
| 2277 | mixer_card = adev_get_mixer_for_card(out->dev, pcm_profile->card); |
| 2278 | list_add_tail(&usecase->mixer_list, &mixer_card->uc_list_node[usecase->id]); |
| 2279 | } |
| 2280 | devices &= ~pcm_profile->devices; |
| 2281 | } |
| 2282 | |
| 2283 | return 0; |
| 2284 | } |
| 2285 | |
| 2286 | static int out_close_pcm_devices(struct stream_out *out) |
| 2287 | { |
| 2288 | struct pcm_device *pcm_device; |
| 2289 | struct listnode *node; |
| 2290 | struct audio_device *adev = out->dev; |
| 2291 | |
| 2292 | list_for_each(node, &out->pcm_dev_list) { |
| 2293 | pcm_device = node_to_item(node, struct pcm_device, stream_list_node); |
| 2294 | if (pcm_device->sound_trigger_handle > 0) { |
| 2295 | adev->sound_trigger_close_for_streaming(pcm_device->sound_trigger_handle); |
| 2296 | pcm_device->sound_trigger_handle = 0; |
| 2297 | } |
| 2298 | if (pcm_device->pcm) { |
| 2299 | pcm_close(pcm_device->pcm); |
| 2300 | pcm_device->pcm = NULL; |
| 2301 | } |
| 2302 | if (pcm_device->resampler) { |
| 2303 | release_resampler(pcm_device->resampler); |
| 2304 | pcm_device->resampler = NULL; |
| 2305 | } |
| 2306 | if (pcm_device->res_buffer) { |
| 2307 | free(pcm_device->res_buffer); |
| 2308 | pcm_device->res_buffer = NULL; |
| 2309 | } |
| 2310 | } |
| 2311 | |
| 2312 | return 0; |
| 2313 | } |
| 2314 | |
| 2315 | static int out_open_pcm_devices(struct stream_out *out) |
| 2316 | { |
| 2317 | struct pcm_device *pcm_device; |
| 2318 | struct listnode *node; |
| 2319 | int ret = 0; |
| 2320 | |
| 2321 | list_for_each(node, &out->pcm_dev_list) { |
| 2322 | pcm_device = node_to_item(node, struct pcm_device, stream_list_node); |
| 2323 | ALOGV("%s: Opening PCM device card_id(%d) device_id(%d)", |
| 2324 | __func__, pcm_device->pcm_profile->card, pcm_device->pcm_profile->id); |
| 2325 | |
| 2326 | pcm_device->pcm = pcm_open(pcm_device->pcm_profile->card, pcm_device->pcm_profile->id, |
| 2327 | PCM_OUT | PCM_MONOTONIC, &pcm_device->pcm_profile->config); |
| 2328 | |
| 2329 | if (pcm_device->pcm && !pcm_is_ready(pcm_device->pcm)) { |
| 2330 | ALOGE("%s: %s", __func__, pcm_get_error(pcm_device->pcm)); |
| 2331 | pcm_device->pcm = NULL; |
| 2332 | ret = -EIO; |
| 2333 | goto error_open; |
| 2334 | } |
| 2335 | /* |
| 2336 | * If the stream rate differs from the PCM rate, we need to |
| 2337 | * create a resampler. |
| 2338 | */ |
| 2339 | if (out->sample_rate != pcm_device->pcm_profile->config.rate) { |
| 2340 | ALOGV("%s: create_resampler(), pcm_device_card(%d), pcm_device_id(%d), \ |
| 2341 | out_rate(%d), device_rate(%d)",__func__, |
| 2342 | pcm_device->pcm_profile->card, pcm_device->pcm_profile->id, |
| 2343 | out->sample_rate, pcm_device->pcm_profile->config.rate); |
| 2344 | ret = create_resampler(out->sample_rate, |
| 2345 | pcm_device->pcm_profile->config.rate, |
| 2346 | audio_channel_count_from_out_mask(out->channel_mask), |
| 2347 | RESAMPLER_QUALITY_DEFAULT, |
| 2348 | NULL, |
| 2349 | &pcm_device->resampler); |
| 2350 | pcm_device->res_byte_count = 0; |
| 2351 | pcm_device->res_buffer = NULL; |
| 2352 | } |
| 2353 | } |
| 2354 | return ret; |
| 2355 | |
| 2356 | error_open: |
| 2357 | out_close_pcm_devices(out); |
| 2358 | return ret; |
| 2359 | } |
| 2360 | |
| 2361 | static int disable_output_path_l(struct stream_out *out) |
| 2362 | { |
| 2363 | struct audio_device *adev = out->dev; |
| 2364 | struct audio_usecase *uc_info; |
| 2365 | |
| 2366 | uc_info = get_usecase_from_id(adev, out->usecase); |
| 2367 | if (uc_info == NULL) { |
| 2368 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 2369 | __func__, out->usecase); |
| 2370 | return -EINVAL; |
| 2371 | } |
| 2372 | disable_snd_device(adev, uc_info, uc_info->out_snd_device, true); |
| 2373 | uc_release_pcm_devices(uc_info); |
| 2374 | list_remove(&uc_info->adev_list_node); |
| 2375 | free(uc_info); |
| 2376 | |
| 2377 | return 0; |
| 2378 | } |
| 2379 | |
| 2380 | static void enable_output_path_l(struct stream_out *out) |
| 2381 | { |
| 2382 | struct audio_device *adev = out->dev; |
| 2383 | struct audio_usecase *uc_info; |
| 2384 | |
| 2385 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 2386 | uc_info->id = out->usecase; |
| 2387 | uc_info->type = PCM_PLAYBACK; |
| 2388 | uc_info->stream = (struct audio_stream *)out; |
| 2389 | uc_info->devices = out->devices; |
| 2390 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 2391 | uc_info->out_snd_device = SND_DEVICE_NONE; |
| 2392 | uc_select_pcm_devices(uc_info); |
| 2393 | |
| 2394 | list_add_tail(&adev->usecase_list, &uc_info->adev_list_node); |
| 2395 | select_devices(adev, out->usecase); |
| 2396 | } |
| 2397 | |
| 2398 | static int stop_output_stream(struct stream_out *out) |
| 2399 | { |
| 2400 | int ret = 0; |
| 2401 | struct audio_device *adev = out->dev; |
| 2402 | bool do_disable = true; |
| 2403 | |
| 2404 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
| 2405 | out->usecase, use_case_table[out->usecase]); |
| 2406 | |
| 2407 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD && |
| 2408 | adev->offload_fx_stop_output != NULL) { |
| 2409 | adev->offload_fx_stop_output(out->handle); |
| 2410 | |
| 2411 | if (out->offload_state == OFFLOAD_STATE_PAUSED || |
| 2412 | out->offload_state == OFFLOAD_STATE_PAUSED_FLUSHED) |
| 2413 | do_disable = false; |
| 2414 | out->offload_state = OFFLOAD_STATE_IDLE; |
| 2415 | } |
| 2416 | if (do_disable) |
| 2417 | ret = disable_output_path_l(out); |
| 2418 | |
| 2419 | ALOGV("%s: exit: status(%d)", __func__, ret); |
| 2420 | return ret; |
| 2421 | } |
| 2422 | |
| 2423 | static int start_output_stream(struct stream_out *out) |
| 2424 | { |
| 2425 | int ret = 0; |
| 2426 | struct audio_device *adev = out->dev; |
| 2427 | |
| 2428 | ALOGV("%s: enter: usecase(%d: %s) devices(%#x) channels(%d)", |
| 2429 | __func__, out->usecase, use_case_table[out->usecase], out->devices, out->config.channels); |
| 2430 | |
| 2431 | enable_output_path_l(out); |
| 2432 | |
| 2433 | if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2434 | out->compr = NULL; |
| 2435 | ret = out_open_pcm_devices(out); |
| 2436 | if (ret != 0) |
| 2437 | goto error_open; |
| 2438 | #ifdef PREPROCESSING_ENABLED |
| 2439 | out->echo_reference = NULL; |
| 2440 | out->echo_reference_generation = adev->echo_reference_generation; |
| 2441 | if (adev->echo_reference != NULL) |
| 2442 | out->echo_reference = adev->echo_reference; |
| 2443 | #endif |
| 2444 | } else { |
| 2445 | out->compr = compress_open(COMPRESS_CARD, COMPRESS_DEVICE, |
| 2446 | COMPRESS_IN, &out->compr_config); |
| 2447 | if (out->compr && !is_compress_ready(out->compr)) { |
| 2448 | ALOGE("%s: %s", __func__, compress_get_error(out->compr)); |
| 2449 | compress_close(out->compr); |
| 2450 | out->compr = NULL; |
| 2451 | ret = -EIO; |
| 2452 | goto error_open; |
| 2453 | } |
| 2454 | if (out->offload_callback) |
| 2455 | compress_nonblock(out->compr, out->non_blocking); |
| 2456 | |
| 2457 | if (adev->offload_fx_start_output != NULL) |
| 2458 | adev->offload_fx_start_output(out->handle); |
| 2459 | } |
| 2460 | ALOGV("%s: exit", __func__); |
| 2461 | return 0; |
| 2462 | error_open: |
| 2463 | stop_output_stream(out); |
| 2464 | error_config: |
| 2465 | return ret; |
| 2466 | } |
| 2467 | |
| 2468 | static int stop_voice_call(struct audio_device *adev) |
| 2469 | { |
| 2470 | struct audio_usecase *uc_info; |
| 2471 | |
| 2472 | ALOGV("%s: enter", __func__); |
| 2473 | adev->in_call = false; |
| 2474 | |
| 2475 | /* TODO: implement voice call stop */ |
| 2476 | |
| 2477 | uc_info = get_usecase_from_id(adev, USECASE_VOICE_CALL); |
| 2478 | if (uc_info == NULL) { |
| 2479 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 2480 | __func__, USECASE_VOICE_CALL); |
| 2481 | return -EINVAL; |
| 2482 | } |
| 2483 | |
| 2484 | disable_snd_device(adev, uc_info, uc_info->out_snd_device, false); |
| 2485 | disable_snd_device(adev, uc_info, uc_info->in_snd_device, true); |
| 2486 | |
| 2487 | uc_release_pcm_devices(uc_info); |
| 2488 | list_remove(&uc_info->adev_list_node); |
| 2489 | free(uc_info); |
| 2490 | |
| 2491 | ALOGV("%s: exit", __func__); |
| 2492 | return 0; |
| 2493 | } |
| 2494 | |
| 2495 | /* always called with adev lock held */ |
| 2496 | static int start_voice_call(struct audio_device *adev) |
| 2497 | { |
| 2498 | struct audio_usecase *uc_info; |
| 2499 | |
| 2500 | ALOGV("%s: enter", __func__); |
| 2501 | |
| 2502 | uc_info = (struct audio_usecase *)calloc(1, sizeof(struct audio_usecase)); |
| 2503 | uc_info->id = USECASE_VOICE_CALL; |
| 2504 | uc_info->type = VOICE_CALL; |
| 2505 | uc_info->stream = (struct audio_stream *)adev->primary_output; |
| 2506 | uc_info->devices = adev->primary_output->devices; |
| 2507 | uc_info->in_snd_device = SND_DEVICE_NONE; |
| 2508 | uc_info->out_snd_device = SND_DEVICE_NONE; |
| 2509 | |
| 2510 | uc_select_pcm_devices(uc_info); |
| 2511 | |
| 2512 | list_add_tail(&adev->usecase_list, &uc_info->adev_list_node); |
| 2513 | |
| 2514 | select_devices(adev, USECASE_VOICE_CALL); |
| 2515 | |
| 2516 | |
| 2517 | /* TODO: implement voice call start */ |
| 2518 | |
| 2519 | /* set cached volume */ |
| 2520 | set_voice_volume_l(adev, adev->voice_volume); |
| 2521 | |
| 2522 | adev->in_call = true; |
| 2523 | ALOGV("%s: exit", __func__); |
| 2524 | return 0; |
| 2525 | } |
| 2526 | |
| 2527 | static int check_input_parameters(uint32_t sample_rate, |
| 2528 | audio_format_t format, |
| 2529 | int channel_count) |
| 2530 | { |
| 2531 | if (format != AUDIO_FORMAT_PCM_16_BIT) return -EINVAL; |
| 2532 | |
| 2533 | if ((channel_count < 1) || (channel_count > 2)) return -EINVAL; |
| 2534 | |
| 2535 | switch (sample_rate) { |
| 2536 | case 8000: |
| 2537 | case 11025: |
| 2538 | case 12000: |
| 2539 | case 16000: |
| 2540 | case 22050: |
| 2541 | case 24000: |
| 2542 | case 32000: |
| 2543 | case 44100: |
| 2544 | case 48000: |
| 2545 | break; |
| 2546 | default: |
| 2547 | return -EINVAL; |
| 2548 | } |
| 2549 | |
| 2550 | return 0; |
| 2551 | } |
| 2552 | |
| 2553 | static size_t get_input_buffer_size(uint32_t sample_rate, |
| 2554 | audio_format_t format, |
| 2555 | int channel_count, |
| 2556 | usecase_type_t usecase_type, |
| 2557 | audio_devices_t devices) |
| 2558 | { |
| 2559 | size_t size = 0; |
| 2560 | struct pcm_device_profile *pcm_profile; |
| 2561 | |
| 2562 | if (check_input_parameters(sample_rate, format, channel_count) != 0) |
| 2563 | return 0; |
| 2564 | |
| 2565 | pcm_profile = get_pcm_device(usecase_type, devices); |
| 2566 | if (pcm_profile == NULL) |
| 2567 | return 0; |
| 2568 | |
| 2569 | /* |
| 2570 | * take resampling into account and return the closest majoring |
| 2571 | * multiple of 16 frames, as audioflinger expects audio buffers to |
| 2572 | * be a multiple of 16 frames |
| 2573 | */ |
| 2574 | size = (pcm_profile->config.period_size * sample_rate) / pcm_profile->config.rate; |
| 2575 | size = ((size + 15) / 16) * 16; |
| 2576 | |
| 2577 | return (size * channel_count * audio_bytes_per_sample(format)); |
| 2578 | |
| 2579 | } |
| 2580 | |
| 2581 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 2582 | { |
| 2583 | struct stream_out *out = (struct stream_out *)stream; |
| 2584 | |
| 2585 | return out->sample_rate; |
| 2586 | } |
| 2587 | |
| 2588 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 2589 | { |
| 2590 | (void)stream; |
| 2591 | (void)rate; |
| 2592 | return -ENOSYS; |
| 2593 | } |
| 2594 | |
| 2595 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 2596 | { |
| 2597 | struct stream_out *out = (struct stream_out *)stream; |
| 2598 | |
| 2599 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2600 | return out->compr_config.fragment_size; |
| 2601 | } |
| 2602 | |
| 2603 | return out->config.period_size * |
| 2604 | audio_stream_out_frame_size((const struct audio_stream_out *)stream); |
| 2605 | } |
| 2606 | |
| 2607 | static uint32_t out_get_channels(const struct audio_stream *stream) |
| 2608 | { |
| 2609 | struct stream_out *out = (struct stream_out *)stream; |
| 2610 | |
| 2611 | return out->channel_mask; |
| 2612 | } |
| 2613 | |
| 2614 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 2615 | { |
| 2616 | struct stream_out *out = (struct stream_out *)stream; |
| 2617 | |
| 2618 | return out->format; |
| 2619 | } |
| 2620 | |
| 2621 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 2622 | { |
| 2623 | (void)stream; |
| 2624 | (void)format; |
| 2625 | return -ENOSYS; |
| 2626 | } |
| 2627 | |
| 2628 | static int do_out_standby_l(struct stream_out *out) |
| 2629 | { |
| 2630 | struct audio_device *adev = out->dev; |
| 2631 | int status = 0; |
| 2632 | |
| 2633 | out->standby = true; |
| 2634 | if (out->usecase != USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2635 | out_close_pcm_devices(out); |
| 2636 | #ifdef PREPROCESSING_ENABLED |
| 2637 | /* stop writing to echo reference */ |
| 2638 | if (out->echo_reference != NULL) { |
| 2639 | out->echo_reference->write(out->echo_reference, NULL); |
| 2640 | if (out->echo_reference_generation != adev->echo_reference_generation) { |
| 2641 | ALOGV("%s: release_echo_reference %p", __func__, out->echo_reference); |
| 2642 | release_echo_reference(out->echo_reference); |
| 2643 | out->echo_reference_generation = adev->echo_reference_generation; |
| 2644 | } |
| 2645 | out->echo_reference = NULL; |
| 2646 | } |
| 2647 | #endif |
| 2648 | } else { |
| 2649 | stop_compressed_output_l(out); |
| 2650 | out->gapless_mdata.encoder_delay = 0; |
| 2651 | out->gapless_mdata.encoder_padding = 0; |
| 2652 | if (out->compr != NULL) { |
| 2653 | compress_close(out->compr); |
| 2654 | out->compr = NULL; |
| 2655 | } |
| 2656 | } |
| 2657 | status = stop_output_stream(out); |
| 2658 | |
| 2659 | return status; |
| 2660 | } |
| 2661 | |
| 2662 | static int out_standby(struct audio_stream *stream) |
| 2663 | { |
| 2664 | struct stream_out *out = (struct stream_out *)stream; |
| 2665 | struct audio_device *adev = out->dev; |
| 2666 | |
| 2667 | ALOGV("%s: enter: usecase(%d: %s)", __func__, |
| 2668 | out->usecase, use_case_table[out->usecase]); |
| 2669 | lock_output_stream(out); |
| 2670 | if (!out->standby) { |
| 2671 | pthread_mutex_lock(&adev->lock); |
| 2672 | do_out_standby_l(out); |
| 2673 | pthread_mutex_unlock(&adev->lock); |
| 2674 | } |
| 2675 | pthread_mutex_unlock(&out->lock); |
| 2676 | ALOGV("%s: exit", __func__); |
| 2677 | return 0; |
| 2678 | } |
| 2679 | |
| 2680 | static int out_dump(const struct audio_stream *stream, int fd) |
| 2681 | { |
| 2682 | (void)stream; |
| 2683 | (void)fd; |
| 2684 | |
| 2685 | return 0; |
| 2686 | } |
| 2687 | |
| 2688 | static int parse_compress_metadata(struct stream_out *out, struct str_parms *parms) |
| 2689 | { |
| 2690 | int ret = 0; |
| 2691 | char value[32]; |
| 2692 | struct compr_gapless_mdata tmp_mdata; |
| 2693 | |
| 2694 | if (!out || !parms) { |
| 2695 | return -EINVAL; |
| 2696 | } |
| 2697 | |
| 2698 | ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES, value, sizeof(value)); |
| 2699 | if (ret >= 0) { |
| 2700 | tmp_mdata.encoder_delay = atoi(value); /* what is a good limit check? */ |
| 2701 | } else { |
| 2702 | return -EINVAL; |
| 2703 | } |
| 2704 | |
| 2705 | ret = str_parms_get_str(parms, AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES, value, sizeof(value)); |
| 2706 | if (ret >= 0) { |
| 2707 | tmp_mdata.encoder_padding = atoi(value); |
| 2708 | } else { |
| 2709 | return -EINVAL; |
| 2710 | } |
| 2711 | |
| 2712 | out->gapless_mdata = tmp_mdata; |
| 2713 | out->send_new_metadata = 1; |
| 2714 | ALOGV("%s new encoder delay %u and padding %u", __func__, |
| 2715 | out->gapless_mdata.encoder_delay, out->gapless_mdata.encoder_padding); |
| 2716 | |
| 2717 | return 0; |
| 2718 | } |
| 2719 | |
| 2720 | |
| 2721 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 2722 | { |
| 2723 | struct stream_out *out = (struct stream_out *)stream; |
| 2724 | struct audio_device *adev = out->dev; |
| 2725 | struct audio_usecase *usecase; |
| 2726 | struct listnode *node; |
| 2727 | struct str_parms *parms; |
| 2728 | char value[32]; |
| 2729 | int ret, val = 0; |
| 2730 | struct audio_usecase *uc_info; |
| 2731 | bool do_standby = false; |
| 2732 | struct pcm_device *pcm_device; |
| 2733 | struct pcm_device_profile *pcm_profile; |
| 2734 | #ifdef PREPROCESSING_ENABLED |
| 2735 | struct stream_in *in = NULL; /* if non-NULL, then force input to standby */ |
| 2736 | #endif |
| 2737 | |
| 2738 | ALOGV("%s: enter: usecase(%d: %s) kvpairs: %s out->devices(%d) adev->mode(%d)", |
| 2739 | __func__, out->usecase, use_case_table[out->usecase], kvpairs, out->devices, adev->mode); |
| 2740 | parms = str_parms_create_str(kvpairs); |
| 2741 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
| 2742 | if (ret >= 0) { |
| 2743 | val = atoi(value); |
| 2744 | pthread_mutex_lock(&adev->lock_inputs); |
| 2745 | lock_output_stream(out); |
| 2746 | pthread_mutex_lock(&adev->lock); |
| 2747 | #ifdef PREPROCESSING_ENABLED |
| 2748 | if (((int)out->devices != val) && (val != 0) && (!out->standby) && |
| 2749 | (out->usecase == USECASE_AUDIO_PLAYBACK)) { |
| 2750 | /* reset active input: |
| 2751 | * - to attach the echo reference |
| 2752 | * - because a change in output device may change mic settings */ |
| 2753 | if (adev->active_input && (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION || |
| 2754 | adev->active_input->source == AUDIO_SOURCE_MIC)) { |
| 2755 | in = adev->active_input; |
| 2756 | } |
| 2757 | } |
| 2758 | #endif |
| 2759 | if (val != 0) { |
| 2760 | out->devices = val; |
| 2761 | |
| 2762 | if (!out->standby) { |
| 2763 | uc_info = get_usecase_from_id(adev, out->usecase); |
| 2764 | if (uc_info == NULL) { |
| 2765 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 2766 | __func__, out->usecase); |
| 2767 | } else { |
| 2768 | list_for_each(node, &out->pcm_dev_list) { |
| 2769 | pcm_device = node_to_item(node, struct pcm_device, stream_list_node); |
| 2770 | if ((pcm_device->pcm_profile->devices & val) == 0) |
| 2771 | do_standby = true; |
| 2772 | val &= ~pcm_device->pcm_profile->devices; |
| 2773 | } |
| 2774 | if (val != 0) |
| 2775 | do_standby = true; |
| 2776 | } |
| 2777 | if (do_standby) |
| 2778 | do_out_standby_l(out); |
| 2779 | else { |
| 2780 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2781 | uc_info = get_usecase_from_id(adev, USECASE_AUDIO_PLAYBACK); |
| 2782 | if (uc_info == NULL) { |
| 2783 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 2784 | __func__, USECASE_AUDIO_PLAYBACK); |
| 2785 | } |
| 2786 | if (uc_info != NULL && uc_info->out_snd_device == SND_DEVICE_OUT_SPEAKER_AND_HEADPHONES) { |
| 2787 | ALOGV("Out_set_param: spk+headset enabled\n"); |
| 2788 | disable_snd_device(adev, uc_info, SND_DEVICE_OUT_SPEAKER, true); |
| 2789 | uc_info->out_snd_device = SND_DEVICE_OUT_HEADPHONES; |
| 2790 | } |
| 2791 | } |
| 2792 | select_devices(adev, out->usecase); |
| 2793 | } |
| 2794 | } |
| 2795 | |
| 2796 | if ((adev->mode == AUDIO_MODE_IN_CALL) && !adev->in_call && |
| 2797 | (out == adev->primary_output)) { |
| 2798 | start_voice_call(adev); |
| 2799 | } else if ((adev->mode == AUDIO_MODE_IN_CALL) && adev->in_call && |
| 2800 | (out == adev->primary_output)) { |
| 2801 | select_devices(adev, USECASE_VOICE_CALL); |
| 2802 | } |
| 2803 | } |
| 2804 | |
| 2805 | if ((adev->mode == AUDIO_MODE_NORMAL) && adev->in_call && |
| 2806 | (out == adev->primary_output)) { |
| 2807 | stop_voice_call(adev); |
| 2808 | } |
| 2809 | pthread_mutex_unlock(&adev->lock); |
| 2810 | pthread_mutex_unlock(&out->lock); |
| 2811 | #ifdef PREPROCESSING_ENABLED |
| 2812 | if (in) { |
| 2813 | /* The lock on adev->lock_inputs prevents input stream from being closed */ |
| 2814 | lock_input_stream(in); |
| 2815 | pthread_mutex_lock(&adev->lock); |
| 2816 | LOG_ALWAYS_FATAL_IF(in != adev->active_input); |
| 2817 | do_in_standby_l(in); |
| 2818 | pthread_mutex_unlock(&adev->lock); |
| 2819 | pthread_mutex_unlock(&in->lock); |
| 2820 | } |
| 2821 | #endif |
| 2822 | pthread_mutex_unlock(&adev->lock_inputs); |
| 2823 | } |
| 2824 | |
| 2825 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2826 | parse_compress_metadata(out, parms); |
| 2827 | } |
| 2828 | |
| 2829 | str_parms_destroy(parms); |
| 2830 | |
| 2831 | if (ret > 0) |
| 2832 | ret = 0; |
| 2833 | ALOGV("%s: exit: code(%d)", __func__, ret); |
| 2834 | return ret; |
| 2835 | } |
| 2836 | |
| 2837 | static char* out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 2838 | { |
| 2839 | struct stream_out *out = (struct stream_out *)stream; |
| 2840 | struct str_parms *query = str_parms_create_str(keys); |
| 2841 | char *str; |
| 2842 | char value[256]; |
| 2843 | struct str_parms *reply = str_parms_create(); |
| 2844 | size_t i, j; |
| 2845 | int ret; |
| 2846 | bool first = true; |
| 2847 | ALOGV("%s: enter: keys - %s", __func__, keys); |
| 2848 | ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value)); |
| 2849 | if (ret >= 0) { |
| 2850 | value[0] = '\0'; |
| 2851 | i = 0; |
| 2852 | while (out->supported_channel_masks[i] != 0) { |
| 2853 | for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) { |
| 2854 | if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) { |
| 2855 | if (!first) { |
| 2856 | strcat(value, "|"); |
| 2857 | } |
| 2858 | strcat(value, out_channels_name_to_enum_table[j].name); |
| 2859 | first = false; |
| 2860 | break; |
| 2861 | } |
| 2862 | } |
| 2863 | i++; |
| 2864 | } |
| 2865 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); |
| 2866 | str = str_parms_to_str(reply); |
| 2867 | } else { |
| 2868 | str = strdup(keys); |
| 2869 | } |
| 2870 | str_parms_destroy(query); |
| 2871 | str_parms_destroy(reply); |
| 2872 | ALOGV("%s: exit: returns - %s", __func__, str); |
| 2873 | return str; |
| 2874 | } |
| 2875 | |
| 2876 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 2877 | { |
| 2878 | struct stream_out *out = (struct stream_out *)stream; |
| 2879 | |
| 2880 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) |
| 2881 | return COMPRESS_OFFLOAD_PLAYBACK_LATENCY; |
| 2882 | |
| 2883 | return (out->config.period_count * out->config.period_size * 1000) / |
| 2884 | (out->config.rate); |
| 2885 | } |
| 2886 | |
| 2887 | static int out_set_volume(struct audio_stream_out *stream, float left, |
| 2888 | float right) |
| 2889 | { |
| 2890 | struct stream_out *out = (struct stream_out *)stream; |
| 2891 | struct audio_device *adev = out->dev; |
| 2892 | int offload_volume[2];//For stereo |
| 2893 | |
| 2894 | if (out->usecase == USECASE_AUDIO_PLAYBACK_MULTI_CH) { |
| 2895 | /* only take left channel into account: the API is for stereo anyway */ |
| 2896 | out->muted = (left == 0.0f); |
| 2897 | return 0; |
| 2898 | } else if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 2899 | struct mixer_ctl *ctl; |
| 2900 | struct mixer *mixer = NULL; |
| 2901 | |
| 2902 | offload_volume[0] = (int)(left * COMPRESS_PLAYBACK_VOLUME_MAX); |
| 2903 | offload_volume[1] = (int)(right * COMPRESS_PLAYBACK_VOLUME_MAX); |
| 2904 | |
| 2905 | mixer = mixer_open(MIXER_CARD); |
| 2906 | if (!mixer) { |
| 2907 | ALOGE("%s unable to open the mixer for card %d, aborting.", |
| 2908 | __func__, MIXER_CARD); |
| 2909 | return -EINVAL; |
| 2910 | } |
| 2911 | ctl = mixer_get_ctl_by_name(mixer, MIXER_CTL_COMPRESS_PLAYBACK_VOLUME); |
| 2912 | if (!ctl) { |
| 2913 | ALOGE("%s: Could not get ctl for mixer cmd - %s", |
| 2914 | __func__, MIXER_CTL_COMPRESS_PLAYBACK_VOLUME); |
| 2915 | mixer_close(mixer); |
| 2916 | return -EINVAL; |
| 2917 | } |
| 2918 | ALOGV("out_set_volume set offload volume (%f, %f)", left, right); |
| 2919 | mixer_ctl_set_array(ctl, offload_volume, |
| 2920 | sizeof(offload_volume)/sizeof(offload_volume[0])); |
| 2921 | mixer_close(mixer); |
| 2922 | return 0; |
| 2923 | } |
| 2924 | |
| 2925 | return -ENOSYS; |
| 2926 | } |
| 2927 | |
Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 2928 | static int fast_set_affinity(pid_t tid) { |
| 2929 | cpu_set_t cpu_set; |
| 2930 | int cpu_num; |
| 2931 | const char *irq_procfs = "/proc/asound/irq_affinity"; |
| 2932 | FILE *fp; |
| 2933 | |
| 2934 | if ((fp = fopen(irq_procfs, "r")) == NULL) { |
| 2935 | ALOGW("Procfs node %s not found", irq_procfs); |
| 2936 | return -1; |
| 2937 | } |
| 2938 | |
| 2939 | if (fscanf(fp, "%d", &cpu_num) != 1) { |
| 2940 | ALOGW("Couldn't read CPU id from procfs node %s", irq_procfs); |
| 2941 | fclose(fp); |
| 2942 | return -1; |
| 2943 | } |
| 2944 | fclose(fp); |
| 2945 | |
| 2946 | CPU_ZERO(&cpu_set); |
| 2947 | CPU_SET(cpu_num, &cpu_set); |
| 2948 | return sched_setaffinity(tid, sizeof(cpu_set), &cpu_set); |
| 2949 | } |
| 2950 | |
| 2951 | static ssize_t out_write(struct audio_stream_out *stream, const void *buffer, |
| 2952 | size_t bytes) |
| 2953 | { |
| 2954 | struct stream_out *out = (struct stream_out *)stream; |
| 2955 | struct audio_device *adev = out->dev; |
| 2956 | ssize_t ret = 0; |
| 2957 | struct pcm_device *pcm_device; |
| 2958 | struct listnode *node; |
| 2959 | size_t frame_size = audio_stream_out_frame_size(stream); |
| 2960 | size_t frames_wr = 0, frames_rq = 0; |
| 2961 | unsigned char *data = NULL; |
| 2962 | struct pcm_config config; |
| 2963 | #ifdef PREPROCESSING_ENABLED |
| 2964 | size_t in_frames = bytes / frame_size; |
| 2965 | size_t out_frames = in_frames; |
| 2966 | struct stream_in *in = NULL; |
| 2967 | #endif |
| 2968 | pid_t tid; |
| 2969 | int err; |
| 2970 | |
| 2971 | lock_output_stream(out); |
| 2972 | |
| 2973 | if (out->usecase == USECASE_AUDIO_PLAYBACK && !out->is_fastmixer_affinity_set) { |
| 2974 | tid = gettid(); |
| 2975 | err = fast_set_affinity(tid); |
| 2976 | if (err < 0) { |
| 2977 | ALOGW("Couldn't set affinity for tid %d; error %d", tid, err); |
| 2978 | } |
| 2979 | out->is_fastmixer_affinity_set = true; |
| 2980 | } |
| 2981 | |
| 2982 | if (out->standby) { |
| 2983 | #ifdef PREPROCESSING_ENABLED |
| 2984 | pthread_mutex_unlock(&out->lock); |
| 2985 | /* Prevent input stream from being closed */ |
| 2986 | pthread_mutex_lock(&adev->lock_inputs); |
| 2987 | lock_output_stream(out); |
| 2988 | if (!out->standby) { |
| 2989 | pthread_mutex_unlock(&adev->lock_inputs); |
| 2990 | goto false_alarm; |
| 2991 | } |
| 2992 | #endif |
| 2993 | pthread_mutex_lock(&adev->lock); |
| 2994 | ret = start_output_stream(out); |
| 2995 | /* ToDo: If use case is compress offload should return 0 */ |
| 2996 | if (ret != 0) { |
| 2997 | pthread_mutex_unlock(&adev->lock); |
| 2998 | #ifdef PREPROCESSING_ENABLED |
| 2999 | pthread_mutex_unlock(&adev->lock_inputs); |
| 3000 | #endif |
| 3001 | goto exit; |
| 3002 | } |
| 3003 | out->standby = false; |
| 3004 | |
| 3005 | #ifdef PREPROCESSING_ENABLED |
| 3006 | /* A change in output device may change the microphone selection */ |
| 3007 | if (adev->active_input && |
| 3008 | (adev->active_input->source == AUDIO_SOURCE_VOICE_COMMUNICATION || |
| 3009 | adev->active_input->source == AUDIO_SOURCE_MIC)) { |
| 3010 | in = adev->active_input; |
| 3011 | ALOGV("%s: enter:) force_input_standby true", __func__); |
| 3012 | } |
| 3013 | #endif |
| 3014 | pthread_mutex_unlock(&adev->lock); |
| 3015 | #ifdef PREPROCESSING_ENABLED |
| 3016 | if (!in) { |
| 3017 | /* Leave mutex locked iff in != NULL */ |
| 3018 | pthread_mutex_unlock(&adev->lock_inputs); |
| 3019 | } |
| 3020 | #endif |
| 3021 | } |
| 3022 | false_alarm: |
| 3023 | |
| 3024 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 3025 | ALOGVV("%s: writing buffer (%d bytes) to compress device", __func__, bytes); |
| 3026 | |
| 3027 | if (out->offload_state == OFFLOAD_STATE_PAUSED_FLUSHED) { |
| 3028 | ALOGV("start offload write from pause state"); |
| 3029 | pthread_mutex_lock(&adev->lock); |
| 3030 | enable_output_path_l(out); |
| 3031 | pthread_mutex_unlock(&adev->lock); |
| 3032 | } |
| 3033 | |
| 3034 | if (out->send_new_metadata) { |
| 3035 | ALOGVV("send new gapless metadata"); |
| 3036 | compress_set_gapless_metadata(out->compr, &out->gapless_mdata); |
| 3037 | out->send_new_metadata = 0; |
| 3038 | } |
| 3039 | |
| 3040 | ret = compress_write(out->compr, buffer, bytes); |
| 3041 | ALOGVV("%s: writing buffer (%d bytes) to compress device returned %d", __func__, bytes, ret); |
| 3042 | if (ret >= 0 && ret < (ssize_t)bytes) { |
| 3043 | send_offload_cmd_l(out, OFFLOAD_CMD_WAIT_FOR_BUFFER); |
| 3044 | } |
| 3045 | if (out->offload_state != OFFLOAD_STATE_PLAYING) { |
| 3046 | compress_start(out->compr); |
| 3047 | out->offload_state = OFFLOAD_STATE_PLAYING; |
| 3048 | } |
| 3049 | pthread_mutex_unlock(&out->lock); |
| 3050 | #ifdef PREPROCESSING_ENABLED |
| 3051 | if (in) { |
| 3052 | /* This mutex was left locked iff in != NULL */ |
| 3053 | pthread_mutex_unlock(&adev->lock_inputs); |
| 3054 | } |
| 3055 | #endif |
| 3056 | return ret; |
| 3057 | } else { |
| 3058 | #ifdef PREPROCESSING_ENABLED |
| 3059 | if (android_atomic_acquire_load(&adev->echo_reference_generation) |
| 3060 | != out->echo_reference_generation) { |
| 3061 | pthread_mutex_lock(&adev->lock); |
| 3062 | if (out->echo_reference != NULL) { |
| 3063 | ALOGV("%s: release_echo_reference %p", __func__, out->echo_reference); |
| 3064 | release_echo_reference(out->echo_reference); |
| 3065 | } |
| 3066 | // note that adev->echo_reference_generation here can be different from the one |
| 3067 | // tested above but it doesn't matter as we now have the adev mutex and it is consistent |
| 3068 | // with what has been set by get_echo_reference() or put_echo_reference() |
| 3069 | out->echo_reference_generation = adev->echo_reference_generation; |
| 3070 | out->echo_reference = adev->echo_reference; |
| 3071 | ALOGV("%s: update echo reference generation %d", __func__, |
| 3072 | out->echo_reference_generation); |
| 3073 | pthread_mutex_unlock(&adev->lock); |
| 3074 | } |
| 3075 | #endif |
| 3076 | |
| 3077 | if (out->muted) |
| 3078 | memset((void *)buffer, 0, bytes); |
| 3079 | list_for_each(node, &out->pcm_dev_list) { |
| 3080 | pcm_device = node_to_item(node, struct pcm_device, stream_list_node); |
| 3081 | if (pcm_device->resampler) { |
| 3082 | if (bytes * pcm_device->pcm_profile->config.rate / out->sample_rate + frame_size |
| 3083 | > pcm_device->res_byte_count) { |
| 3084 | pcm_device->res_byte_count = |
| 3085 | bytes * pcm_device->pcm_profile->config.rate / out->sample_rate + frame_size; |
| 3086 | pcm_device->res_buffer = |
| 3087 | realloc(pcm_device->res_buffer, pcm_device->res_byte_count); |
| 3088 | ALOGV("%s: resampler res_byte_count = %zu", __func__, |
| 3089 | pcm_device->res_byte_count); |
| 3090 | } |
| 3091 | frames_rq = bytes / frame_size; |
| 3092 | frames_wr = pcm_device->res_byte_count / frame_size; |
| 3093 | ALOGVV("%s: resampler request frames = %d frame_size = %d", |
| 3094 | __func__, frames_rq, frame_size); |
| 3095 | pcm_device->resampler->resample_from_input(pcm_device->resampler, |
| 3096 | (int16_t *)buffer, &frames_rq, (int16_t *)pcm_device->res_buffer, &frames_wr); |
| 3097 | ALOGVV("%s: resampler output frames_= %d", __func__, frames_wr); |
| 3098 | } |
| 3099 | if (pcm_device->pcm) { |
| 3100 | #ifdef PREPROCESSING_ENABLED |
| 3101 | if (out->echo_reference != NULL && pcm_device->pcm_profile->devices != SND_DEVICE_OUT_SPEAKER) { |
| 3102 | struct echo_reference_buffer b; |
| 3103 | b.raw = (void *)buffer; |
| 3104 | b.frame_count = in_frames; |
| 3105 | |
| 3106 | get_playback_delay(out, out_frames, &b); |
| 3107 | out->echo_reference->write(out->echo_reference, &b); |
| 3108 | } |
| 3109 | #endif |
Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 3110 | ALOGVV("%s: writing buffer (%d bytes) to pcm device", __func__, bytes); |
| 3111 | if (pcm_device->resampler && pcm_device->res_buffer) |
| 3112 | pcm_device->status = |
| 3113 | pcm_write(pcm_device->pcm, (void *)pcm_device->res_buffer, |
| 3114 | frames_wr * frame_size); |
| 3115 | else |
| 3116 | pcm_device->status = pcm_write(pcm_device->pcm, (void *)buffer, bytes); |
| 3117 | if (pcm_device->status != 0) |
| 3118 | ret = pcm_device->status; |
| 3119 | } |
| 3120 | } |
| 3121 | if (ret == 0) |
| 3122 | out->written += bytes / (out->config.channels * sizeof(short)); |
| 3123 | } |
| 3124 | |
| 3125 | exit: |
| 3126 | pthread_mutex_unlock(&out->lock); |
| 3127 | |
| 3128 | if (ret != 0) { |
| 3129 | list_for_each(node, &out->pcm_dev_list) { |
| 3130 | pcm_device = node_to_item(node, struct pcm_device, stream_list_node); |
| 3131 | if (pcm_device->pcm && pcm_device->status != 0) |
| 3132 | ALOGE("%s: error %zd - %s", __func__, ret, pcm_get_error(pcm_device->pcm)); |
| 3133 | } |
| 3134 | out_standby(&out->stream.common); |
| 3135 | usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) / |
| 3136 | out_get_sample_rate(&out->stream.common)); |
| 3137 | } |
| 3138 | |
| 3139 | #ifdef PREPROCESSING_ENABLED |
| 3140 | if (in) { |
| 3141 | /* The lock on adev->lock_inputs prevents input stream from being closed */ |
| 3142 | lock_input_stream(in); |
| 3143 | pthread_mutex_lock(&adev->lock); |
| 3144 | LOG_ALWAYS_FATAL_IF(in != adev->active_input); |
| 3145 | do_in_standby_l(in); |
| 3146 | pthread_mutex_unlock(&adev->lock); |
| 3147 | pthread_mutex_unlock(&in->lock); |
| 3148 | /* This mutex was left locked iff in != NULL */ |
| 3149 | pthread_mutex_unlock(&adev->lock_inputs); |
| 3150 | } |
| 3151 | #endif |
| 3152 | |
| 3153 | return bytes; |
| 3154 | } |
| 3155 | |
| 3156 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 3157 | uint32_t *dsp_frames) |
| 3158 | { |
| 3159 | struct stream_out *out = (struct stream_out *)stream; |
| 3160 | *dsp_frames = 0; |
| 3161 | if ((out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) && (dsp_frames != NULL)) { |
| 3162 | lock_output_stream(out); |
| 3163 | if (out->compr != NULL) { |
| 3164 | compress_get_tstamp(out->compr, (unsigned long *)dsp_frames, |
| 3165 | &out->sample_rate); |
| 3166 | ALOGVV("%s rendered frames %d sample_rate %d", |
| 3167 | __func__, *dsp_frames, out->sample_rate); |
| 3168 | } |
| 3169 | pthread_mutex_unlock(&out->lock); |
| 3170 | return 0; |
| 3171 | } else |
| 3172 | return -EINVAL; |
| 3173 | } |
| 3174 | |
| 3175 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 3176 | { |
| 3177 | (void)stream; |
| 3178 | (void)effect; |
| 3179 | return 0; |
| 3180 | } |
| 3181 | |
| 3182 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 3183 | { |
| 3184 | (void)stream; |
| 3185 | (void)effect; |
| 3186 | return 0; |
| 3187 | } |
| 3188 | |
| 3189 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream, |
| 3190 | int64_t *timestamp) |
| 3191 | { |
| 3192 | (void)stream; |
| 3193 | (void)timestamp; |
| 3194 | return -EINVAL; |
| 3195 | } |
| 3196 | |
| 3197 | static int out_get_presentation_position(const struct audio_stream_out *stream, |
| 3198 | uint64_t *frames, struct timespec *timestamp) |
| 3199 | { |
| 3200 | struct stream_out *out = (struct stream_out *)stream; |
| 3201 | int ret = -1; |
| 3202 | unsigned long dsp_frames; |
| 3203 | |
| 3204 | lock_output_stream(out); |
| 3205 | |
| 3206 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 3207 | if (out->compr != NULL) { |
| 3208 | compress_get_tstamp(out->compr, &dsp_frames, |
| 3209 | &out->sample_rate); |
| 3210 | ALOGVV("%s rendered frames %ld sample_rate %d", |
| 3211 | __func__, dsp_frames, out->sample_rate); |
| 3212 | *frames = dsp_frames; |
| 3213 | ret = 0; |
| 3214 | /* this is the best we can do */ |
| 3215 | clock_gettime(CLOCK_MONOTONIC, timestamp); |
| 3216 | } |
| 3217 | } else { |
| 3218 | /* FIXME: which device to read from? */ |
| 3219 | if (!list_empty(&out->pcm_dev_list)) { |
| 3220 | unsigned int avail; |
| 3221 | struct pcm_device *pcm_device = node_to_item(list_head(&out->pcm_dev_list), |
| 3222 | struct pcm_device, stream_list_node); |
| 3223 | |
| 3224 | if (pcm_get_htimestamp(pcm_device->pcm, &avail, timestamp) == 0) { |
| 3225 | size_t kernel_buffer_size = out->config.period_size * out->config.period_count; |
| 3226 | int64_t signed_frames = out->written - kernel_buffer_size + avail; |
| 3227 | /* This adjustment accounts for buffering after app processor. |
| 3228 | It is based on estimated DSP latency per use case, rather than exact. */ |
| 3229 | signed_frames -= |
| 3230 | (render_latency(out->usecase) * out->sample_rate / 1000000LL); |
| 3231 | |
| 3232 | /* It would be unusual for this value to be negative, but check just in case ... */ |
| 3233 | if (signed_frames >= 0) { |
| 3234 | *frames = signed_frames; |
| 3235 | ret = 0; |
| 3236 | } |
| 3237 | } |
| 3238 | } |
| 3239 | } |
| 3240 | |
| 3241 | pthread_mutex_unlock(&out->lock); |
| 3242 | |
| 3243 | return ret; |
| 3244 | } |
| 3245 | |
| 3246 | static int out_set_callback(struct audio_stream_out *stream, |
| 3247 | stream_callback_t callback, void *cookie) |
| 3248 | { |
| 3249 | struct stream_out *out = (struct stream_out *)stream; |
| 3250 | |
| 3251 | ALOGV("%s", __func__); |
| 3252 | lock_output_stream(out); |
| 3253 | out->offload_callback = callback; |
| 3254 | out->offload_cookie = cookie; |
| 3255 | pthread_mutex_unlock(&out->lock); |
| 3256 | return 0; |
| 3257 | } |
| 3258 | |
| 3259 | static int out_pause(struct audio_stream_out* stream) |
| 3260 | { |
| 3261 | struct stream_out *out = (struct stream_out *)stream; |
| 3262 | int status = -ENOSYS; |
| 3263 | ALOGV("%s", __func__); |
| 3264 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 3265 | lock_output_stream(out); |
| 3266 | if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PLAYING) { |
| 3267 | status = compress_pause(out->compr); |
| 3268 | out->offload_state = OFFLOAD_STATE_PAUSED; |
| 3269 | pthread_mutex_lock(&out->dev->lock); |
| 3270 | status = disable_output_path_l(out); |
| 3271 | pthread_mutex_unlock(&out->dev->lock); |
| 3272 | } |
| 3273 | pthread_mutex_unlock(&out->lock); |
| 3274 | } |
| 3275 | return status; |
| 3276 | } |
| 3277 | |
| 3278 | static int out_resume(struct audio_stream_out* stream) |
| 3279 | { |
| 3280 | struct stream_out *out = (struct stream_out *)stream; |
| 3281 | int status = -ENOSYS; |
| 3282 | ALOGV("%s", __func__); |
| 3283 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 3284 | status = 0; |
| 3285 | lock_output_stream(out); |
| 3286 | if (out->compr != NULL && out->offload_state == OFFLOAD_STATE_PAUSED) { |
| 3287 | pthread_mutex_lock(&out->dev->lock); |
| 3288 | enable_output_path_l(out); |
| 3289 | pthread_mutex_unlock(&out->dev->lock); |
| 3290 | status = compress_resume(out->compr); |
| 3291 | out->offload_state = OFFLOAD_STATE_PLAYING; |
| 3292 | } |
| 3293 | pthread_mutex_unlock(&out->lock); |
| 3294 | } |
| 3295 | return status; |
| 3296 | } |
| 3297 | |
| 3298 | static int out_drain(struct audio_stream_out* stream, audio_drain_type_t type ) |
| 3299 | { |
| 3300 | struct stream_out *out = (struct stream_out *)stream; |
| 3301 | int status = -ENOSYS; |
| 3302 | ALOGV("%s", __func__); |
| 3303 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 3304 | lock_output_stream(out); |
| 3305 | if (type == AUDIO_DRAIN_EARLY_NOTIFY) |
| 3306 | status = send_offload_cmd_l(out, OFFLOAD_CMD_PARTIAL_DRAIN); |
| 3307 | else |
| 3308 | status = send_offload_cmd_l(out, OFFLOAD_CMD_DRAIN); |
| 3309 | pthread_mutex_unlock(&out->lock); |
| 3310 | } |
| 3311 | return status; |
| 3312 | } |
| 3313 | |
| 3314 | static int out_flush(struct audio_stream_out* stream) |
| 3315 | { |
| 3316 | struct stream_out *out = (struct stream_out *)stream; |
| 3317 | ALOGV("%s", __func__); |
| 3318 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 3319 | lock_output_stream(out); |
| 3320 | if (out->offload_state == OFFLOAD_STATE_PLAYING) { |
| 3321 | ALOGE("out_flush() called in wrong state %d", out->offload_state); |
| 3322 | pthread_mutex_unlock(&out->lock); |
| 3323 | return -ENOSYS; |
| 3324 | } |
| 3325 | if (out->offload_state == OFFLOAD_STATE_PAUSED) { |
| 3326 | stop_compressed_output_l(out); |
| 3327 | out->offload_state = OFFLOAD_STATE_PAUSED_FLUSHED; |
| 3328 | } |
| 3329 | pthread_mutex_unlock(&out->lock); |
| 3330 | return 0; |
| 3331 | } |
| 3332 | return -ENOSYS; |
| 3333 | } |
| 3334 | |
| 3335 | /** audio_stream_in implementation **/ |
| 3336 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 3337 | { |
| 3338 | struct stream_in *in = (struct stream_in *)stream; |
| 3339 | |
| 3340 | return in->requested_rate; |
| 3341 | } |
| 3342 | |
| 3343 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 3344 | { |
| 3345 | (void)stream; |
| 3346 | (void)rate; |
| 3347 | return -ENOSYS; |
| 3348 | } |
| 3349 | |
| 3350 | static uint32_t in_get_channels(const struct audio_stream *stream) |
| 3351 | { |
| 3352 | struct stream_in *in = (struct stream_in *)stream; |
| 3353 | |
| 3354 | return in->main_channels; |
| 3355 | } |
| 3356 | |
| 3357 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 3358 | { |
| 3359 | (void)stream; |
| 3360 | return AUDIO_FORMAT_PCM_16_BIT; |
| 3361 | } |
| 3362 | |
| 3363 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 3364 | { |
| 3365 | (void)stream; |
| 3366 | (void)format; |
| 3367 | |
| 3368 | return -ENOSYS; |
| 3369 | } |
| 3370 | |
| 3371 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 3372 | { |
| 3373 | struct stream_in *in = (struct stream_in *)stream; |
| 3374 | |
| 3375 | return get_input_buffer_size(in->requested_rate, |
| 3376 | in_get_format(stream), |
| 3377 | audio_channel_count_from_in_mask(in->main_channels), |
| 3378 | in->usecase_type, |
| 3379 | in->devices); |
| 3380 | } |
| 3381 | |
| 3382 | static int in_close_pcm_devices(struct stream_in *in) |
| 3383 | { |
| 3384 | struct pcm_device *pcm_device; |
| 3385 | struct listnode *node; |
| 3386 | struct audio_device *adev = in->dev; |
| 3387 | |
| 3388 | list_for_each(node, &in->pcm_dev_list) { |
| 3389 | pcm_device = node_to_item(node, struct pcm_device, stream_list_node); |
| 3390 | if (pcm_device) { |
| 3391 | if (pcm_device->pcm) |
| 3392 | pcm_close(pcm_device->pcm); |
| 3393 | pcm_device->pcm = NULL; |
| 3394 | if (pcm_device->sound_trigger_handle > 0) |
| 3395 | adev->sound_trigger_close_for_streaming(pcm_device->sound_trigger_handle); |
| 3396 | pcm_device->sound_trigger_handle = 0; |
| 3397 | } |
| 3398 | } |
| 3399 | return 0; |
| 3400 | } |
| 3401 | |
| 3402 | |
| 3403 | /* must be called with stream and hw device mutex locked */ |
| 3404 | static int do_in_standby_l(struct stream_in *in) |
| 3405 | { |
| 3406 | int status = 0; |
| 3407 | |
| 3408 | #ifdef PREPROCESSING_ENABLED |
| 3409 | struct audio_device *adev = in->dev; |
| 3410 | #endif |
| 3411 | if (!in->standby) { |
| 3412 | |
| 3413 | in_close_pcm_devices(in); |
| 3414 | |
| 3415 | #ifdef PREPROCESSING_ENABLED |
| 3416 | if (in->echo_reference != NULL) { |
| 3417 | /* stop reading from echo reference */ |
| 3418 | in->echo_reference->read(in->echo_reference, NULL); |
| 3419 | put_echo_reference(adev, in->echo_reference); |
| 3420 | in->echo_reference = NULL; |
| 3421 | } |
| 3422 | #ifdef HW_AEC_LOOPBACK |
| 3423 | if (in->hw_echo_reference) |
| 3424 | { |
| 3425 | if (in->hw_ref_buf) { |
| 3426 | free(in->hw_ref_buf); |
| 3427 | in->hw_ref_buf = NULL; |
| 3428 | } |
| 3429 | } |
| 3430 | #endif // HW_AEC_LOOPBACK |
| 3431 | #endif // PREPROCESSING_ENABLED |
| 3432 | |
| 3433 | status = stop_input_stream(in); |
| 3434 | |
| 3435 | if (in->read_buf) { |
| 3436 | free(in->read_buf); |
| 3437 | in->read_buf = NULL; |
| 3438 | } |
| 3439 | |
| 3440 | in->standby = 1; |
| 3441 | } |
| 3442 | return 0; |
| 3443 | } |
| 3444 | |
| 3445 | // called with adev->lock_inputs locked |
| 3446 | static int in_standby_l(struct stream_in *in) |
| 3447 | { |
| 3448 | struct audio_device *adev = in->dev; |
| 3449 | int status = 0; |
| 3450 | lock_input_stream(in); |
| 3451 | if (!in->standby) { |
| 3452 | pthread_mutex_lock(&adev->lock); |
| 3453 | status = do_in_standby_l(in); |
| 3454 | pthread_mutex_unlock(&adev->lock); |
| 3455 | } |
| 3456 | pthread_mutex_unlock(&in->lock); |
| 3457 | return status; |
| 3458 | } |
| 3459 | |
| 3460 | static int in_standby(struct audio_stream *stream) |
| 3461 | { |
| 3462 | struct stream_in *in = (struct stream_in *)stream; |
| 3463 | struct audio_device *adev = in->dev; |
| 3464 | int status; |
| 3465 | ALOGV("%s: enter", __func__); |
| 3466 | pthread_mutex_lock(&adev->lock_inputs); |
| 3467 | status = in_standby_l(in); |
| 3468 | pthread_mutex_unlock(&adev->lock_inputs); |
| 3469 | ALOGV("%s: exit: status(%d)", __func__, status); |
| 3470 | return status; |
| 3471 | } |
| 3472 | |
| 3473 | static int in_dump(const struct audio_stream *stream, int fd) |
| 3474 | { |
| 3475 | (void)stream; |
| 3476 | (void)fd; |
| 3477 | |
| 3478 | return 0; |
| 3479 | } |
| 3480 | |
| 3481 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 3482 | { |
| 3483 | struct stream_in *in = (struct stream_in *)stream; |
| 3484 | struct audio_device *adev = in->dev; |
| 3485 | struct str_parms *parms; |
| 3486 | char *str; |
| 3487 | char value[32]; |
| 3488 | int ret, val = 0; |
| 3489 | struct audio_usecase *uc_info; |
| 3490 | bool do_standby = false; |
| 3491 | struct listnode *node; |
| 3492 | struct pcm_device *pcm_device; |
| 3493 | struct pcm_device_profile *pcm_profile; |
| 3494 | |
| 3495 | ALOGV("%s: enter: kvpairs=%s", __func__, kvpairs); |
| 3496 | parms = str_parms_create_str(kvpairs); |
| 3497 | |
| 3498 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, value, sizeof(value)); |
| 3499 | |
| 3500 | pthread_mutex_lock(&adev->lock_inputs); |
| 3501 | lock_input_stream(in); |
| 3502 | pthread_mutex_lock(&adev->lock); |
| 3503 | if (ret >= 0) { |
| 3504 | val = atoi(value); |
| 3505 | /* no audio source uses val == 0 */ |
| 3506 | if (((int)in->source != val) && (val != 0)) { |
| 3507 | in->source = val; |
| 3508 | } |
| 3509 | } |
| 3510 | |
| 3511 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
| 3512 | if (ret >= 0) { |
| 3513 | val = atoi(value); |
| 3514 | if (((int)in->devices != val) && (val != 0)) { |
| 3515 | in->devices = val; |
| 3516 | /* If recording is in progress, change the tx device to new device */ |
| 3517 | if (!in->standby) { |
| 3518 | uc_info = get_usecase_from_id(adev, in->usecase); |
| 3519 | if (uc_info == NULL) { |
| 3520 | ALOGE("%s: Could not find the usecase (%d) in the list", |
| 3521 | __func__, in->usecase); |
| 3522 | } else { |
| 3523 | if (list_empty(&in->pcm_dev_list)) |
| 3524 | ALOGE("%s: pcm device list empty", __func__); |
| 3525 | else { |
| 3526 | pcm_device = node_to_item(list_head(&in->pcm_dev_list), |
| 3527 | struct pcm_device, stream_list_node); |
| 3528 | if ((pcm_device->pcm_profile->devices & val & ~AUDIO_DEVICE_BIT_IN) == 0) { |
| 3529 | do_standby = true; |
| 3530 | } |
| 3531 | } |
| 3532 | } |
| 3533 | if (do_standby) { |
| 3534 | ret = do_in_standby_l(in); |
| 3535 | } else |
| 3536 | ret = select_devices(adev, in->usecase); |
| 3537 | } |
| 3538 | } |
| 3539 | } |
| 3540 | pthread_mutex_unlock(&adev->lock); |
| 3541 | pthread_mutex_unlock(&in->lock); |
| 3542 | pthread_mutex_unlock(&adev->lock_inputs); |
| 3543 | str_parms_destroy(parms); |
| 3544 | |
| 3545 | if (ret > 0) |
| 3546 | ret = 0; |
| 3547 | |
| 3548 | ALOGV("%s: exit: status(%d)", __func__, ret); |
| 3549 | return ret; |
| 3550 | } |
| 3551 | |
| 3552 | static char* in_get_parameters(const struct audio_stream *stream, |
| 3553 | const char *keys) |
| 3554 | { |
| 3555 | (void)stream; |
| 3556 | (void)keys; |
| 3557 | |
| 3558 | return strdup(""); |
| 3559 | } |
| 3560 | |
| 3561 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 3562 | { |
| 3563 | (void)stream; |
| 3564 | (void)gain; |
| 3565 | |
| 3566 | return 0; |
| 3567 | } |
| 3568 | |
| 3569 | static ssize_t read_bytes_from_dsp(struct stream_in *in, void* buffer, |
| 3570 | size_t bytes) |
| 3571 | { |
| 3572 | struct pcm_device *pcm_device; |
| 3573 | struct audio_device *adev = in->dev; |
| 3574 | |
| 3575 | pcm_device = node_to_item(list_head(&in->pcm_dev_list), |
| 3576 | struct pcm_device, stream_list_node); |
| 3577 | |
| 3578 | if (pcm_device->sound_trigger_handle > 0) |
| 3579 | return adev->sound_trigger_read_samples(pcm_device->sound_trigger_handle, buffer, bytes); |
| 3580 | else |
| 3581 | return 0; |
| 3582 | } |
| 3583 | |
| 3584 | static ssize_t in_read(struct audio_stream_in *stream, void *buffer, |
| 3585 | size_t bytes) |
| 3586 | { |
| 3587 | struct stream_in *in = (struct stream_in *)stream; |
| 3588 | struct audio_device *adev = in->dev; |
| 3589 | ssize_t frames = -1; |
| 3590 | int ret = -1; |
| 3591 | int read_and_process_successful = false; |
| 3592 | |
| 3593 | size_t frames_rq = bytes / audio_stream_in_frame_size(stream); |
| 3594 | pid_t tid; |
| 3595 | int err; |
| 3596 | |
| 3597 | /* no need to acquire adev->lock_inputs because API contract prevents a close */ |
| 3598 | lock_input_stream(in); |
| 3599 | |
| 3600 | if (in->usecase == USECASE_AUDIO_CAPTURE && !in->is_fastcapture_affinity_set) { |
| 3601 | tid = gettid(); |
| 3602 | err = fast_set_affinity(tid); |
| 3603 | if (err < 0) { |
| 3604 | ALOGW("Couldn't set affinity for tid %d; error %d", tid, err); |
| 3605 | } |
| 3606 | in->is_fastcapture_affinity_set = true; |
| 3607 | } |
| 3608 | |
| 3609 | if (in->standby) { |
| 3610 | pthread_mutex_unlock(&in->lock); |
| 3611 | pthread_mutex_lock(&adev->lock_inputs); |
| 3612 | lock_input_stream(in); |
| 3613 | if (!in->standby) { |
| 3614 | pthread_mutex_unlock(&adev->lock_inputs); |
| 3615 | goto false_alarm; |
| 3616 | } |
| 3617 | pthread_mutex_lock(&adev->lock); |
| 3618 | ret = start_input_stream(in); |
| 3619 | pthread_mutex_unlock(&adev->lock); |
| 3620 | pthread_mutex_unlock(&adev->lock_inputs); |
| 3621 | if (ret != 0) { |
| 3622 | goto exit; |
| 3623 | } |
| 3624 | in->standby = 0; |
| 3625 | } |
| 3626 | false_alarm: |
| 3627 | |
| 3628 | if (!list_empty(&in->pcm_dev_list)) { |
| 3629 | if (in->usecase == USECASE_AUDIO_CAPTURE_HOTWORD) { |
| 3630 | bytes = read_bytes_from_dsp(in, buffer, bytes); |
| 3631 | if (bytes > 0) |
| 3632 | read_and_process_successful = true; |
| 3633 | } else { |
| 3634 | /* |
| 3635 | * Read PCM and: |
| 3636 | * - resample if needed |
| 3637 | * - process if pre-processors are attached |
| 3638 | * - discard unwanted channels |
| 3639 | */ |
| 3640 | frames = read_and_process_frames(in, buffer, frames_rq); |
| 3641 | if (frames >= 0) |
| 3642 | read_and_process_successful = true; |
| 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | /* |
| 3647 | * Instead of writing zeroes here, we could trust the hardware |
| 3648 | * to always provide zeroes when muted. |
| 3649 | */ |
| 3650 | if (read_and_process_successful == true && adev->mic_mute) |
| 3651 | memset(buffer, 0, bytes); |
| 3652 | |
| 3653 | exit: |
| 3654 | pthread_mutex_unlock(&in->lock); |
| 3655 | |
| 3656 | if (read_and_process_successful == false) { |
| 3657 | in_standby(&in->stream.common); |
| 3658 | ALOGV("%s: read failed - sleeping for buffer duration", __func__); |
| 3659 | usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) / |
| 3660 | in->requested_rate); |
| 3661 | } |
| 3662 | return bytes; |
| 3663 | } |
| 3664 | |
| 3665 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 3666 | { |
| 3667 | (void)stream; |
| 3668 | |
| 3669 | return 0; |
| 3670 | } |
| 3671 | |
| 3672 | static int add_remove_audio_effect(const struct audio_stream *stream, |
| 3673 | effect_handle_t effect, |
| 3674 | bool enable) |
| 3675 | { |
| 3676 | struct stream_in *in = (struct stream_in *)stream; |
| 3677 | struct audio_device *adev = in->dev; |
| 3678 | int status = 0; |
| 3679 | effect_descriptor_t desc; |
| 3680 | #ifdef PREPROCESSING_ENABLED |
| 3681 | int i; |
| 3682 | #endif |
| 3683 | status = (*effect)->get_descriptor(effect, &desc); |
| 3684 | if (status != 0) |
| 3685 | return status; |
| 3686 | |
| 3687 | ALOGI("add_remove_audio_effect(), effect type: %08x, enable: %d ", desc.type.timeLow, enable); |
| 3688 | |
| 3689 | pthread_mutex_lock(&adev->lock_inputs); |
| 3690 | lock_input_stream(in); |
| 3691 | pthread_mutex_lock(&in->dev->lock); |
| 3692 | #ifndef PREPROCESSING_ENABLED |
| 3693 | if ((in->source == AUDIO_SOURCE_VOICE_COMMUNICATION) && |
| 3694 | in->enable_aec != enable && |
| 3695 | (memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0)) { |
| 3696 | in->enable_aec = enable; |
| 3697 | if (!in->standby) |
| 3698 | select_devices(in->dev, in->usecase); |
| 3699 | } |
| 3700 | #else |
| 3701 | if ( (in->num_preprocessors > MAX_PREPROCESSORS) && (enable == true) ) { |
| 3702 | status = -ENOSYS; |
| 3703 | goto exit; |
| 3704 | } |
| 3705 | if ( enable == true ) { |
| 3706 | in->preprocessors[in->num_preprocessors].effect_itfe = effect; |
| 3707 | /* add the supported channel of the effect in the channel_configs */ |
| 3708 | in_read_audio_effect_channel_configs(in, &in->preprocessors[in->num_preprocessors]); |
| 3709 | in->num_preprocessors ++; |
| 3710 | /* check compatibility between main channel supported and possible auxiliary channels */ |
| 3711 | in_update_aux_channels(in, effect);//wesley crash |
| 3712 | in->aux_channels_changed = true; |
| 3713 | } else { |
| 3714 | /* if ( enable == false ) */ |
| 3715 | if (in->num_preprocessors <= 0) { |
| 3716 | status = -ENOSYS; |
| 3717 | goto exit; |
| 3718 | } |
| 3719 | status = -EINVAL; |
| 3720 | for (i=0; i < in->num_preprocessors; i++) { |
| 3721 | if (status == 0) { /* status == 0 means an effect was removed from a previous slot */ |
| 3722 | in->preprocessors[i - 1].effect_itfe = in->preprocessors[i].effect_itfe; |
| 3723 | in->preprocessors[i - 1].channel_configs = in->preprocessors[i].channel_configs; |
| 3724 | in->preprocessors[i - 1].num_channel_configs = |
| 3725 | in->preprocessors[i].num_channel_configs; |
| 3726 | ALOGV("add_remove_audio_effect moving fx from %d to %d", i, i-1); |
| 3727 | continue; |
| 3728 | } |
| 3729 | if ( in->preprocessors[i].effect_itfe == effect ) { |
| 3730 | ALOGV("add_remove_audio_effect found fx at index %d", i); |
| 3731 | free(in->preprocessors[i].channel_configs); |
| 3732 | status = 0; |
| 3733 | } |
| 3734 | } |
| 3735 | if (status != 0) |
| 3736 | goto exit; |
| 3737 | in->num_preprocessors--; |
| 3738 | /* if we remove one effect, at least the last proproc should be reset */ |
| 3739 | in->preprocessors[in->num_preprocessors].num_channel_configs = 0; |
| 3740 | in->preprocessors[in->num_preprocessors].effect_itfe = NULL; |
| 3741 | in->preprocessors[in->num_preprocessors].channel_configs = NULL; |
| 3742 | in->aux_channels_changed = false; |
| 3743 | ALOGV("%s: enable(%d), in->aux_channels_changed(%d)", __func__, enable, in->aux_channels_changed); |
| 3744 | } |
| 3745 | ALOGI("%s: num_preprocessors = %d", __func__, in->num_preprocessors); |
| 3746 | |
| 3747 | if ( memcmp(&desc.type, FX_IID_AEC, sizeof(effect_uuid_t)) == 0) { |
| 3748 | in->enable_aec = enable; |
| 3749 | ALOGV("add_remove_audio_effect(), FX_IID_AEC, enable: %d", enable); |
| 3750 | if (!in->standby) { |
| 3751 | select_devices(in->dev, in->usecase); |
| 3752 | do_in_standby_l(in); |
| 3753 | } |
| 3754 | if (in->enable_aec == true) { |
| 3755 | in_configure_reverse(in); |
| 3756 | } |
| 3757 | } |
| 3758 | exit: |
| 3759 | #endif |
| 3760 | ALOGW_IF(status != 0, "add_remove_audio_effect() error %d", status); |
| 3761 | pthread_mutex_unlock(&in->dev->lock); |
| 3762 | pthread_mutex_unlock(&in->lock); |
| 3763 | pthread_mutex_unlock(&adev->lock_inputs); |
| 3764 | return status; |
| 3765 | } |
| 3766 | |
| 3767 | static int in_add_audio_effect(const struct audio_stream *stream, |
| 3768 | effect_handle_t effect) |
| 3769 | { |
| 3770 | ALOGV("%s: effect %p", __func__, effect); |
| 3771 | return add_remove_audio_effect(stream, effect, true); |
| 3772 | } |
| 3773 | |
| 3774 | static int in_remove_audio_effect(const struct audio_stream *stream, |
| 3775 | effect_handle_t effect) |
| 3776 | { |
| 3777 | ALOGV("%s: effect %p", __func__, effect); |
| 3778 | return add_remove_audio_effect(stream, effect, false); |
| 3779 | } |
| 3780 | |
| 3781 | static int adev_open_output_stream(struct audio_hw_device *dev, |
| 3782 | audio_io_handle_t handle, |
| 3783 | audio_devices_t devices, |
| 3784 | audio_output_flags_t flags, |
| 3785 | struct audio_config *config, |
| 3786 | struct audio_stream_out **stream_out, |
| 3787 | const char *address __unused) |
| 3788 | { |
| 3789 | struct audio_device *adev = (struct audio_device *)dev; |
| 3790 | struct stream_out *out; |
| 3791 | int i, ret; |
| 3792 | struct pcm_device_profile *pcm_profile; |
| 3793 | |
| 3794 | ALOGV("%s: enter: sample_rate(%d) channel_mask(%#x) devices(%#x) flags(%#x)", |
| 3795 | __func__, config->sample_rate, config->channel_mask, devices, flags); |
| 3796 | *stream_out = NULL; |
| 3797 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
| 3798 | |
| 3799 | if (devices == AUDIO_DEVICE_NONE) |
| 3800 | devices = AUDIO_DEVICE_OUT_SPEAKER; |
| 3801 | |
| 3802 | out->flags = flags; |
| 3803 | out->devices = devices; |
| 3804 | out->dev = adev; |
| 3805 | out->format = config->format; |
| 3806 | out->sample_rate = config->sample_rate; |
| 3807 | out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 3808 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO; |
| 3809 | out->handle = handle; |
| 3810 | |
| 3811 | pcm_profile = get_pcm_device(PCM_PLAYBACK, devices); |
| 3812 | if (pcm_profile == NULL) { |
| 3813 | ret = -EINVAL; |
| 3814 | goto error_open; |
| 3815 | } |
| 3816 | out->config = pcm_profile->config; |
| 3817 | |
| 3818 | /* Init use case and pcm_config */ |
| 3819 | if (out->flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) { |
| 3820 | if (config->offload_info.version != AUDIO_INFO_INITIALIZER.version || |
| 3821 | config->offload_info.size != AUDIO_INFO_INITIALIZER.size) { |
| 3822 | ALOGE("%s: Unsupported Offload information", __func__); |
| 3823 | ret = -EINVAL; |
| 3824 | goto error_open; |
| 3825 | } |
| 3826 | if (!is_supported_format(config->offload_info.format)) { |
| 3827 | ALOGE("%s: Unsupported audio format", __func__); |
| 3828 | ret = -EINVAL; |
| 3829 | goto error_open; |
| 3830 | } |
| 3831 | |
| 3832 | out->compr_config.codec = (struct snd_codec *) |
| 3833 | calloc(1, sizeof(struct snd_codec)); |
| 3834 | |
| 3835 | out->usecase = USECASE_AUDIO_PLAYBACK_OFFLOAD; |
| 3836 | if (config->offload_info.channel_mask) |
| 3837 | out->channel_mask = config->offload_info.channel_mask; |
| 3838 | else if (config->channel_mask) |
| 3839 | out->channel_mask = config->channel_mask; |
| 3840 | out->format = config->offload_info.format; |
| 3841 | out->sample_rate = config->offload_info.sample_rate; |
| 3842 | |
| 3843 | out->stream.set_callback = out_set_callback; |
| 3844 | out->stream.pause = out_pause; |
| 3845 | out->stream.resume = out_resume; |
| 3846 | out->stream.drain = out_drain; |
| 3847 | out->stream.flush = out_flush; |
| 3848 | |
| 3849 | out->compr_config.codec->id = |
| 3850 | get_snd_codec_id(config->offload_info.format); |
| 3851 | out->compr_config.fragment_size = COMPRESS_OFFLOAD_FRAGMENT_SIZE; |
| 3852 | out->compr_config.fragments = COMPRESS_OFFLOAD_NUM_FRAGMENTS; |
| 3853 | out->compr_config.codec->sample_rate = config->offload_info.sample_rate; |
| 3854 | out->compr_config.codec->bit_rate = |
| 3855 | config->offload_info.bit_rate; |
| 3856 | out->compr_config.codec->ch_in = |
| 3857 | audio_channel_count_from_out_mask(config->channel_mask); |
| 3858 | out->compr_config.codec->ch_out = out->compr_config.codec->ch_in; |
| 3859 | |
| 3860 | if (flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) |
| 3861 | out->non_blocking = 1; |
| 3862 | |
| 3863 | out->send_new_metadata = 1; |
| 3864 | create_offload_callback_thread(out); |
| 3865 | out->offload_state = OFFLOAD_STATE_IDLE; |
| 3866 | |
| 3867 | ALOGV("%s: offloaded output offload_info version %04x bit rate %d", |
| 3868 | __func__, config->offload_info.version, |
| 3869 | config->offload_info.bit_rate); |
| 3870 | } else if (out->flags & (AUDIO_OUTPUT_FLAG_DEEP_BUFFER)) { |
| 3871 | out->usecase = USECASE_AUDIO_PLAYBACK_DEEP_BUFFER; |
| 3872 | out->config = pcm_config_deep_buffer; |
| 3873 | out->sample_rate = out->config.rate; |
| 3874 | ALOGV("%s: use AUDIO_PLAYBACK_DEEP_BUFFER",__func__); |
| 3875 | } else { |
| 3876 | out->usecase = USECASE_AUDIO_PLAYBACK; |
| 3877 | out->sample_rate = out->config.rate; |
| 3878 | } |
| 3879 | |
| 3880 | if (flags & AUDIO_OUTPUT_FLAG_PRIMARY) { |
| 3881 | if (adev->primary_output == NULL) |
| 3882 | adev->primary_output = out; |
| 3883 | else { |
| 3884 | ALOGE("%s: Primary output is already opened", __func__); |
| 3885 | ret = -EEXIST; |
| 3886 | goto error_open; |
| 3887 | } |
| 3888 | } |
| 3889 | |
| 3890 | /* Check if this usecase is already existing */ |
| 3891 | pthread_mutex_lock(&adev->lock); |
| 3892 | if (get_usecase_from_id(adev, out->usecase) != NULL) { |
| 3893 | ALOGE("%s: Usecase (%d) is already present", __func__, out->usecase); |
| 3894 | pthread_mutex_unlock(&adev->lock); |
| 3895 | ret = -EEXIST; |
| 3896 | goto error_open; |
| 3897 | } |
| 3898 | pthread_mutex_unlock(&adev->lock); |
| 3899 | |
| 3900 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 3901 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 3902 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 3903 | out->stream.common.get_channels = out_get_channels; |
| 3904 | out->stream.common.get_format = out_get_format; |
| 3905 | out->stream.common.set_format = out_set_format; |
| 3906 | out->stream.common.standby = out_standby; |
| 3907 | out->stream.common.dump = out_dump; |
| 3908 | out->stream.common.set_parameters = out_set_parameters; |
| 3909 | out->stream.common.get_parameters = out_get_parameters; |
| 3910 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 3911 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 3912 | out->stream.get_latency = out_get_latency; |
| 3913 | out->stream.set_volume = out_set_volume; |
| 3914 | out->stream.write = out_write; |
| 3915 | out->stream.get_render_position = out_get_render_position; |
| 3916 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
| 3917 | out->stream.get_presentation_position = out_get_presentation_position; |
| 3918 | |
| 3919 | out->standby = 1; |
| 3920 | /* out->muted = false; by calloc() */ |
| 3921 | /* out->written = 0; by calloc() */ |
| 3922 | |
| 3923 | pthread_mutex_init(&out->lock, (const pthread_mutexattr_t *) NULL); |
| 3924 | pthread_mutex_init(&out->pre_lock, (const pthread_mutexattr_t *) NULL); |
| 3925 | pthread_cond_init(&out->cond, (const pthread_condattr_t *) NULL); |
| 3926 | |
| 3927 | config->format = out->stream.common.get_format(&out->stream.common); |
| 3928 | config->channel_mask = out->stream.common.get_channels(&out->stream.common); |
| 3929 | config->sample_rate = out->stream.common.get_sample_rate(&out->stream.common); |
| 3930 | |
| 3931 | out->is_fastmixer_affinity_set = false; |
| 3932 | |
| 3933 | *stream_out = &out->stream; |
| 3934 | ALOGV("%s: exit", __func__); |
| 3935 | return 0; |
| 3936 | |
| 3937 | error_open: |
| 3938 | free(out); |
| 3939 | *stream_out = NULL; |
| 3940 | ALOGV("%s: exit: ret %d", __func__, ret); |
| 3941 | return ret; |
| 3942 | } |
| 3943 | |
| 3944 | static void adev_close_output_stream(struct audio_hw_device *dev, |
| 3945 | struct audio_stream_out *stream) |
| 3946 | { |
| 3947 | struct stream_out *out = (struct stream_out *)stream; |
| 3948 | struct audio_device *adev = out->dev; |
| 3949 | (void)dev; |
| 3950 | |
| 3951 | ALOGV("%s: enter", __func__); |
| 3952 | out_standby(&stream->common); |
| 3953 | if (out->usecase == USECASE_AUDIO_PLAYBACK_OFFLOAD) { |
| 3954 | destroy_offload_callback_thread(out); |
| 3955 | |
| 3956 | if (out->compr_config.codec != NULL) |
| 3957 | free(out->compr_config.codec); |
| 3958 | } |
| 3959 | pthread_cond_destroy(&out->cond); |
| 3960 | pthread_mutex_destroy(&out->lock); |
| 3961 | free(stream); |
| 3962 | ALOGV("%s: exit", __func__); |
| 3963 | } |
| 3964 | |
| 3965 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 3966 | { |
| 3967 | struct audio_device *adev = (struct audio_device *)dev; |
| 3968 | struct str_parms *parms; |
| 3969 | char *str; |
| 3970 | char value[32]; |
| 3971 | int val; |
| 3972 | int ret; |
| 3973 | |
| 3974 | ALOGV("%s: enter: %s", __func__, kvpairs); |
| 3975 | |
| 3976 | parms = str_parms_create_str(kvpairs); |
| 3977 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_TTY_MODE, value, sizeof(value)); |
| 3978 | if (ret >= 0) { |
| 3979 | int tty_mode; |
| 3980 | |
| 3981 | if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_OFF) == 0) |
| 3982 | tty_mode = TTY_MODE_OFF; |
| 3983 | else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_VCO) == 0) |
| 3984 | tty_mode = TTY_MODE_VCO; |
| 3985 | else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_HCO) == 0) |
| 3986 | tty_mode = TTY_MODE_HCO; |
| 3987 | else if (strcmp(value, AUDIO_PARAMETER_VALUE_TTY_FULL) == 0) |
| 3988 | tty_mode = TTY_MODE_FULL; |
| 3989 | else |
| 3990 | return -EINVAL; |
| 3991 | |
| 3992 | pthread_mutex_lock(&adev->lock); |
| 3993 | if (tty_mode != adev->tty_mode) { |
| 3994 | adev->tty_mode = tty_mode; |
| 3995 | if (adev->in_call) |
| 3996 | select_devices(adev, USECASE_VOICE_CALL); |
| 3997 | } |
| 3998 | pthread_mutex_unlock(&adev->lock); |
| 3999 | } |
| 4000 | |
| 4001 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_KEY_BT_NREC, value, sizeof(value)); |
| 4002 | if (ret >= 0) { |
| 4003 | /* When set to false, HAL should disable EC and NS |
| 4004 | * But it is currently not supported. |
| 4005 | */ |
| 4006 | if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| 4007 | adev->bluetooth_nrec = true; |
| 4008 | else |
| 4009 | adev->bluetooth_nrec = false; |
| 4010 | } |
| 4011 | |
| 4012 | ret = str_parms_get_str(parms, "screen_state", value, sizeof(value)); |
| 4013 | if (ret >= 0) { |
| 4014 | if (strcmp(value, AUDIO_PARAMETER_VALUE_ON) == 0) |
| 4015 | adev->screen_off = false; |
| 4016 | else |
| 4017 | adev->screen_off = true; |
| 4018 | } |
| 4019 | |
| 4020 | ret = str_parms_get_int(parms, "rotation", &val); |
| 4021 | if (ret >= 0) { |
| 4022 | bool reverse_speakers = false; |
| 4023 | switch(val) { |
| 4024 | /* FIXME: note that the code below assumes that the speakers are in the correct placement |
| 4025 | relative to the user when the device is rotated 90deg from its default rotation. This |
| 4026 | assumption is device-specific, not platform-specific like this code. */ |
| 4027 | case 270: |
| 4028 | reverse_speakers = true; |
| 4029 | break; |
| 4030 | case 0: |
| 4031 | case 90: |
| 4032 | case 180: |
| 4033 | break; |
| 4034 | default: |
| 4035 | ALOGE("%s: unexpected rotation of %d", __func__, val); |
| 4036 | } |
| 4037 | pthread_mutex_lock(&adev->lock); |
| 4038 | if (adev->speaker_lr_swap != reverse_speakers) { |
| 4039 | adev->speaker_lr_swap = reverse_speakers; |
| 4040 | /* only update the selected device if there is active pcm playback */ |
| 4041 | struct audio_usecase *usecase; |
| 4042 | struct listnode *node; |
| 4043 | list_for_each(node, &adev->usecase_list) { |
| 4044 | usecase = node_to_item(node, struct audio_usecase, adev_list_node); |
| 4045 | if (usecase->type == PCM_PLAYBACK) { |
| 4046 | select_devices(adev, usecase->id); |
Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 4047 | break; |
| 4048 | } |
| 4049 | } |
| 4050 | } |
| 4051 | pthread_mutex_unlock(&adev->lock); |
| 4052 | } |
| 4053 | |
| 4054 | str_parms_destroy(parms); |
| 4055 | |
| 4056 | if (ret > 0) |
| 4057 | ret = 0; |
| 4058 | |
| 4059 | ALOGV("%s: exit with code(%d)", __func__, ret); |
| 4060 | return ret; |
| 4061 | } |
| 4062 | |
| 4063 | static char* adev_get_parameters(const struct audio_hw_device *dev, |
| 4064 | const char *keys) |
| 4065 | { |
| 4066 | (void)dev; |
| 4067 | (void)keys; |
| 4068 | |
| 4069 | return strdup(""); |
| 4070 | } |
| 4071 | |
| 4072 | static int adev_init_check(const struct audio_hw_device *dev) |
| 4073 | { |
| 4074 | (void)dev; |
| 4075 | |
| 4076 | return 0; |
| 4077 | } |
| 4078 | |
| 4079 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 4080 | { |
| 4081 | int ret = 0; |
| 4082 | struct audio_device *adev = (struct audio_device *)dev; |
| 4083 | pthread_mutex_lock(&adev->lock); |
| 4084 | /* cache volume */ |
| 4085 | adev->voice_volume = volume; |
| 4086 | ret = set_voice_volume_l(adev, adev->voice_volume); |
| 4087 | pthread_mutex_unlock(&adev->lock); |
| 4088 | return ret; |
| 4089 | } |
| 4090 | |
| 4091 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 4092 | { |
| 4093 | (void)dev; |
| 4094 | (void)volume; |
| 4095 | |
| 4096 | return -ENOSYS; |
| 4097 | } |
| 4098 | |
| 4099 | static int adev_get_master_volume(struct audio_hw_device *dev, |
| 4100 | float *volume) |
| 4101 | { |
| 4102 | (void)dev; |
| 4103 | (void)volume; |
| 4104 | |
| 4105 | return -ENOSYS; |
| 4106 | } |
| 4107 | |
| 4108 | static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) |
| 4109 | { |
| 4110 | (void)dev; |
| 4111 | (void)muted; |
| 4112 | |
| 4113 | return -ENOSYS; |
| 4114 | } |
| 4115 | |
| 4116 | static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) |
| 4117 | { |
| 4118 | (void)dev; |
| 4119 | (void)muted; |
| 4120 | |
| 4121 | return -ENOSYS; |
| 4122 | } |
| 4123 | |
| 4124 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 4125 | { |
| 4126 | struct audio_device *adev = (struct audio_device *)dev; |
| 4127 | |
| 4128 | pthread_mutex_lock(&adev->lock); |
| 4129 | if (adev->mode != mode) { |
| 4130 | ALOGI("%s mode = %d", __func__, mode); |
| 4131 | adev->mode = mode; |
Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 4132 | } |
| 4133 | pthread_mutex_unlock(&adev->lock); |
| 4134 | return 0; |
| 4135 | } |
| 4136 | |
| 4137 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 4138 | { |
| 4139 | struct audio_device *adev = (struct audio_device *)dev; |
| 4140 | int err = 0; |
| 4141 | |
| 4142 | pthread_mutex_lock(&adev->lock); |
| 4143 | adev->mic_mute = state; |
| 4144 | |
| 4145 | if (adev->mode == AUDIO_MODE_IN_CALL) { |
| 4146 | /* TODO */ |
| 4147 | } |
| 4148 | |
| 4149 | pthread_mutex_unlock(&adev->lock); |
| 4150 | return err; |
| 4151 | } |
| 4152 | |
| 4153 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 4154 | { |
| 4155 | struct audio_device *adev = (struct audio_device *)dev; |
| 4156 | |
| 4157 | *state = adev->mic_mute; |
| 4158 | |
| 4159 | return 0; |
| 4160 | } |
| 4161 | |
| 4162 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, |
| 4163 | const struct audio_config *config) |
| 4164 | { |
| 4165 | (void)dev; |
| 4166 | |
| 4167 | /* NOTE: we default to built in mic which may cause a mismatch between what we |
| 4168 | * report here and the actual buffer size |
| 4169 | */ |
| 4170 | return get_input_buffer_size(config->sample_rate, |
| 4171 | config->format, |
| 4172 | audio_channel_count_from_in_mask(config->channel_mask), |
| 4173 | PCM_CAPTURE /* usecase_type */, |
| 4174 | AUDIO_DEVICE_IN_BUILTIN_MIC); |
| 4175 | } |
| 4176 | |
| 4177 | static int adev_open_input_stream(struct audio_hw_device *dev, |
| 4178 | audio_io_handle_t handle __unused, |
| 4179 | audio_devices_t devices, |
| 4180 | struct audio_config *config, |
| 4181 | struct audio_stream_in **stream_in, |
| 4182 | audio_input_flags_t flags, |
| 4183 | const char *address __unused, |
| 4184 | audio_source_t source) |
| 4185 | { |
| 4186 | struct audio_device *adev = (struct audio_device *)dev; |
| 4187 | struct stream_in *in; |
| 4188 | struct pcm_device_profile *pcm_profile; |
| 4189 | |
| 4190 | ALOGV("%s: enter", __func__); |
| 4191 | |
| 4192 | *stream_in = NULL; |
| 4193 | if (check_input_parameters(config->sample_rate, config->format, |
| 4194 | audio_channel_count_from_in_mask(config->channel_mask)) != 0) |
| 4195 | return -EINVAL; |
| 4196 | |
| 4197 | usecase_type_t usecase_type = source == AUDIO_SOURCE_HOTWORD ? |
| 4198 | PCM_HOTWORD_STREAMING : flags & AUDIO_INPUT_FLAG_FAST ? |
| 4199 | PCM_CAPTURE_LOW_LATENCY : PCM_CAPTURE; |
| 4200 | pcm_profile = get_pcm_device(usecase_type, devices); |
| 4201 | if (pcm_profile == NULL && usecase_type == PCM_CAPTURE_LOW_LATENCY) { |
| 4202 | // a low latency profile may not exist for that device, fall back |
| 4203 | // to regular capture. the MixerThread automatically changes |
| 4204 | // to non-fast capture based on the buffer size. |
| 4205 | flags &= ~AUDIO_INPUT_FLAG_FAST; |
| 4206 | usecase_type = PCM_CAPTURE; |
| 4207 | pcm_profile = get_pcm_device(usecase_type, devices); |
| 4208 | } |
| 4209 | if (pcm_profile == NULL) |
| 4210 | return -EINVAL; |
| 4211 | |
| 4212 | in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); |
| 4213 | |
| 4214 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 4215 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 4216 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 4217 | in->stream.common.get_channels = in_get_channels; |
| 4218 | in->stream.common.get_format = in_get_format; |
| 4219 | in->stream.common.set_format = in_set_format; |
| 4220 | in->stream.common.standby = in_standby; |
| 4221 | in->stream.common.dump = in_dump; |
| 4222 | in->stream.common.set_parameters = in_set_parameters; |
| 4223 | in->stream.common.get_parameters = in_get_parameters; |
| 4224 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 4225 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 4226 | in->stream.set_gain = in_set_gain; |
| 4227 | in->stream.read = in_read; |
| 4228 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| 4229 | |
| 4230 | in->devices = devices; |
| 4231 | in->source = source; |
| 4232 | in->dev = adev; |
| 4233 | in->standby = 1; |
| 4234 | in->main_channels = config->channel_mask; |
| 4235 | in->requested_rate = config->sample_rate; |
| 4236 | if (config->sample_rate != CAPTURE_DEFAULT_SAMPLING_RATE) |
| 4237 | flags = flags & ~AUDIO_INPUT_FLAG_FAST; |
| 4238 | in->input_flags = flags; |
| 4239 | /* HW codec is limited to default channels. No need to update with |
| 4240 | * requested channels */ |
| 4241 | in->config = pcm_profile->config; |
| 4242 | |
| 4243 | /* Update config params with the requested sample rate and channels */ |
| 4244 | if (source == AUDIO_SOURCE_HOTWORD) { |
| 4245 | in->usecase = USECASE_AUDIO_CAPTURE_HOTWORD; |
| 4246 | } else { |
| 4247 | in->usecase = USECASE_AUDIO_CAPTURE; |
| 4248 | } |
| 4249 | in->usecase_type = usecase_type; |
| 4250 | |
| 4251 | pthread_mutex_init(&in->lock, (const pthread_mutexattr_t *) NULL); |
| 4252 | pthread_mutex_init(&in->pre_lock, (const pthread_mutexattr_t *) NULL); |
| 4253 | |
| 4254 | in->is_fastcapture_affinity_set = false; |
| 4255 | |
| 4256 | *stream_in = &in->stream; |
| 4257 | ALOGV("%s: exit", __func__); |
| 4258 | return 0; |
| 4259 | } |
| 4260 | |
| 4261 | static void adev_close_input_stream(struct audio_hw_device *dev, |
| 4262 | struct audio_stream_in *stream) |
| 4263 | { |
| 4264 | struct audio_device *adev = (struct audio_device *)dev; |
| 4265 | struct stream_in *in = (struct stream_in*)stream; |
| 4266 | ALOGV("%s", __func__); |
| 4267 | |
| 4268 | /* prevent concurrent out_set_parameters, or out_write from standby */ |
| 4269 | pthread_mutex_lock(&adev->lock_inputs); |
| 4270 | |
| 4271 | #ifdef PREPROCESSING_ENABLED |
| 4272 | int i; |
| 4273 | |
| 4274 | for (i=0; i<in->num_preprocessors; i++) { |
| 4275 | free(in->preprocessors[i].channel_configs); |
| 4276 | } |
| 4277 | |
| 4278 | if (in->read_buf) { |
| 4279 | free(in->read_buf); |
| 4280 | in->read_buf = NULL; |
| 4281 | } |
| 4282 | |
| 4283 | if (in->proc_buf_in) { |
| 4284 | free(in->proc_buf_in); |
| 4285 | in->proc_buf_in = NULL; |
| 4286 | } |
| 4287 | |
| 4288 | if (in->proc_buf_out) { |
| 4289 | free(in->proc_buf_out); |
| 4290 | in->proc_buf_out = NULL; |
| 4291 | } |
| 4292 | |
| 4293 | if (in->ref_buf) { |
| 4294 | free(in->ref_buf); |
| 4295 | in->ref_buf = NULL; |
| 4296 | } |
| 4297 | |
| 4298 | if (in->resampler) { |
| 4299 | release_resampler(in->resampler); |
| 4300 | in->resampler = NULL; |
| 4301 | } |
| 4302 | #endif |
| 4303 | |
| 4304 | in_standby_l(in); |
| 4305 | free(stream); |
| 4306 | |
| 4307 | pthread_mutex_unlock(&adev->lock_inputs); |
| 4308 | |
| 4309 | return; |
| 4310 | } |
| 4311 | |
| 4312 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 4313 | { |
| 4314 | (void)device; |
| 4315 | (void)fd; |
| 4316 | |
| 4317 | return 0; |
| 4318 | } |
| 4319 | |
| 4320 | static int adev_close(hw_device_t *device) |
| 4321 | { |
| 4322 | struct audio_device *adev = (struct audio_device *)device; |
| 4323 | audio_device_ref_count--; |
| 4324 | free(adev->snd_dev_ref_cnt); |
| 4325 | free_mixer_list(adev); |
| 4326 | free(device); |
| 4327 | return 0; |
| 4328 | } |
| 4329 | |
Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 4330 | /* This returns true if the input parameter looks at all plausible as a low latency period size, |
| 4331 | * or false otherwise. A return value of true doesn't mean the value is guaranteed to work, |
| 4332 | * just that it _might_ work. |
| 4333 | */ |
| 4334 | static bool period_size_is_plausible_for_low_latency(int period_size) |
| 4335 | { |
| 4336 | switch (period_size) { |
| 4337 | case 64: |
| 4338 | case 96: |
| 4339 | case 128: |
| 4340 | case 192: |
| 4341 | case 256: |
| 4342 | return true; |
| 4343 | default: |
| 4344 | return false; |
| 4345 | } |
| 4346 | } |
| 4347 | |
| 4348 | static int adev_open(const hw_module_t *module, const char *name, |
| 4349 | hw_device_t **device) |
| 4350 | { |
| 4351 | struct audio_device *adev; |
| 4352 | int retry_count = 0; |
| 4353 | |
| 4354 | ALOGV("%s: enter", __func__); |
| 4355 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) return -EINVAL; |
| 4356 | |
| 4357 | adev = calloc(1, sizeof(struct audio_device)); |
| 4358 | |
| 4359 | adev->device.common.tag = HARDWARE_DEVICE_TAG; |
| 4360 | adev->device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
| 4361 | adev->device.common.module = (struct hw_module_t *)module; |
| 4362 | adev->device.common.close = adev_close; |
| 4363 | |
| 4364 | adev->device.init_check = adev_init_check; |
| 4365 | adev->device.set_voice_volume = adev_set_voice_volume; |
| 4366 | adev->device.set_master_volume = adev_set_master_volume; |
| 4367 | adev->device.get_master_volume = adev_get_master_volume; |
| 4368 | adev->device.set_master_mute = adev_set_master_mute; |
| 4369 | adev->device.get_master_mute = adev_get_master_mute; |
| 4370 | adev->device.set_mode = adev_set_mode; |
| 4371 | adev->device.set_mic_mute = adev_set_mic_mute; |
| 4372 | adev->device.get_mic_mute = adev_get_mic_mute; |
| 4373 | adev->device.set_parameters = adev_set_parameters; |
| 4374 | adev->device.get_parameters = adev_get_parameters; |
| 4375 | adev->device.get_input_buffer_size = adev_get_input_buffer_size; |
| 4376 | adev->device.open_output_stream = adev_open_output_stream; |
| 4377 | adev->device.close_output_stream = adev_close_output_stream; |
| 4378 | adev->device.open_input_stream = adev_open_input_stream; |
| 4379 | adev->device.close_input_stream = adev_close_input_stream; |
| 4380 | adev->device.dump = adev_dump; |
| 4381 | |
| 4382 | /* Set the default route before the PCM stream is opened */ |
| 4383 | adev->mode = AUDIO_MODE_NORMAL; |
| 4384 | adev->active_input = NULL; |
| 4385 | adev->primary_output = NULL; |
| 4386 | adev->voice_volume = 1.0f; |
| 4387 | adev->tty_mode = TTY_MODE_OFF; |
| 4388 | adev->bluetooth_nrec = true; |
| 4389 | adev->in_call = false; |
| 4390 | /* adev->cur_hdmi_channels = 0; by calloc() */ |
| 4391 | adev->snd_dev_ref_cnt = calloc(SND_DEVICE_MAX, sizeof(int)); |
| 4392 | |
| 4393 | adev->dualmic_config = DUALMIC_CONFIG_NONE; |
| 4394 | adev->ns_in_voice_rec = false; |
| 4395 | |
| 4396 | list_init(&adev->usecase_list); |
| 4397 | |
| 4398 | if (mixer_init(adev) != 0) { |
| 4399 | free(adev->snd_dev_ref_cnt); |
| 4400 | free(adev); |
| 4401 | ALOGE("%s: Failed to init, aborting.", __func__); |
| 4402 | *device = NULL; |
| 4403 | return -EINVAL; |
| 4404 | } |
| 4405 | |
| 4406 | if (access(OFFLOAD_FX_LIBRARY_PATH, R_OK) == 0) { |
| 4407 | adev->offload_fx_lib = dlopen(OFFLOAD_FX_LIBRARY_PATH, RTLD_NOW); |
| 4408 | if (adev->offload_fx_lib == NULL) { |
| 4409 | ALOGE("%s: DLOPEN failed for %s", __func__, OFFLOAD_FX_LIBRARY_PATH); |
| 4410 | } else { |
| 4411 | ALOGV("%s: DLOPEN successful for %s", __func__, OFFLOAD_FX_LIBRARY_PATH); |
| 4412 | adev->offload_fx_start_output = |
| 4413 | (int (*)(audio_io_handle_t))dlsym(adev->offload_fx_lib, |
| 4414 | "visualizer_hal_start_output"); |
| 4415 | adev->offload_fx_stop_output = |
| 4416 | (int (*)(audio_io_handle_t))dlsym(adev->offload_fx_lib, |
| 4417 | "visualizer_hal_stop_output"); |
| 4418 | } |
| 4419 | } |
| 4420 | |
Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 4421 | if (access(SOUND_TRIGGER_HAL_LIBRARY_PATH, R_OK) == 0) { |
| 4422 | adev->sound_trigger_lib = dlopen(SOUND_TRIGGER_HAL_LIBRARY_PATH, RTLD_NOW); |
| 4423 | if (adev->sound_trigger_lib == NULL) { |
| 4424 | ALOGE("%s: DLOPEN failed for %s", __func__, SOUND_TRIGGER_HAL_LIBRARY_PATH); |
| 4425 | } else { |
| 4426 | ALOGV("%s: DLOPEN successful for %s", __func__, SOUND_TRIGGER_HAL_LIBRARY_PATH); |
| 4427 | adev->sound_trigger_open_for_streaming = |
| 4428 | (int (*)(void))dlsym(adev->sound_trigger_lib, |
| 4429 | "sound_trigger_open_for_streaming"); |
| 4430 | adev->sound_trigger_read_samples = |
| 4431 | (size_t (*)(int, void *, size_t))dlsym(adev->sound_trigger_lib, |
| 4432 | "sound_trigger_read_samples"); |
| 4433 | adev->sound_trigger_close_for_streaming = |
| 4434 | (int (*)(int))dlsym(adev->sound_trigger_lib, |
| 4435 | "sound_trigger_close_for_streaming"); |
| 4436 | if (!adev->sound_trigger_open_for_streaming || |
| 4437 | !adev->sound_trigger_read_samples || |
| 4438 | !adev->sound_trigger_close_for_streaming) { |
| 4439 | |
| 4440 | ALOGE("%s: Error grabbing functions in %s", __func__, SOUND_TRIGGER_HAL_LIBRARY_PATH); |
| 4441 | adev->sound_trigger_open_for_streaming = 0; |
| 4442 | adev->sound_trigger_read_samples = 0; |
| 4443 | adev->sound_trigger_close_for_streaming = 0; |
| 4444 | } |
| 4445 | } |
| 4446 | } |
| 4447 | |
| 4448 | |
| 4449 | *device = &adev->device.common; |
| 4450 | |
Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 4451 | audio_device_ref_count++; |
| 4452 | |
| 4453 | char value[PROPERTY_VALUE_MAX]; |
| 4454 | if (property_get("audio_hal.period_size", value, NULL) > 0) { |
| 4455 | int trial = atoi(value); |
| 4456 | if (period_size_is_plausible_for_low_latency(trial)) { |
| 4457 | |
| 4458 | pcm_device_playback.config.period_size = trial; |
| 4459 | pcm_device_playback.config.start_threshold = |
| 4460 | PLAYBACK_START_THRESHOLD(trial, PLAYBACK_PERIOD_COUNT); |
| 4461 | pcm_device_playback.config.stop_threshold = |
| 4462 | PLAYBACK_STOP_THRESHOLD(trial, PLAYBACK_PERIOD_COUNT); |
| 4463 | |
| 4464 | pcm_device_capture_low_latency.config.period_size = trial; |
| 4465 | } |
| 4466 | } |
| 4467 | |
| 4468 | ALOGV("%s: exit", __func__); |
| 4469 | return 0; |
| 4470 | } |
| 4471 | |
| 4472 | static struct hw_module_methods_t hal_module_methods = { |
| 4473 | .open = adev_open, |
| 4474 | }; |
| 4475 | |
| 4476 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 4477 | .common = { |
| 4478 | .tag = HARDWARE_MODULE_TAG, |
| 4479 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 4480 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 4481 | .id = AUDIO_HARDWARE_MODULE_ID, |
Christopher N. Hesse | c8502b9 | 2017-01-28 14:02:15 +0100 | [diff] [blame] | 4482 | .name = "Samsung Audio HAL", |
| 4483 | .author = "The LineageOS Project", |
Christopher N. Hesse | 297a636 | 2017-01-28 12:40:45 +0100 | [diff] [blame] | 4484 | .methods = &hal_module_methods, |
| 4485 | }, |
| 4486 | }; |