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