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