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