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