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