Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "audio_hw_primary" |
| 18 | /*#define LOG_NDEBUG 0*/ |
| 19 | |
| 20 | #include <errno.h> |
| 21 | #include <pthread.h> |
| 22 | #include <stdint.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <sys/time.h> |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 25 | #include <fcntl.h> |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 26 | |
| 27 | #include <cutils/log.h> |
| 28 | #include <cutils/properties.h> |
| 29 | #include <cutils/str_parms.h> |
| 30 | |
| 31 | #include <hardware/audio.h> |
| 32 | #include <hardware/hardware.h> |
| 33 | |
Christopher Ferris | f4d123f | 2013-12-04 16:22:55 -0800 | [diff] [blame] | 34 | #include <linux/videodev2.h> |
Simon Wilson | f051bcf | 2012-09-26 14:10:07 -0700 | [diff] [blame] | 35 | #include <videodev2_exynos_media.h> |
| 36 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 37 | #include <system/audio.h> |
| 38 | |
| 39 | #include <tinyalsa/asoundlib.h> |
| 40 | |
| 41 | #include <audio_utils/resampler.h> |
Simon Wilson | 759c6f0 | 2013-01-15 16:38:56 -0800 | [diff] [blame] | 42 | #include <audio_route/audio_route.h> |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 43 | |
Eric Laurent | a989ebf | 2012-10-15 15:14:44 -0700 | [diff] [blame] | 44 | #include <BubbleLevel.h> |
| 45 | |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 46 | #include <eS305VoiceProcessing.h> |
| 47 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 48 | #define PCM_CARD 0 |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 49 | #define PCM_CARD_SPDIF 1 |
| 50 | #define PCM_TOTAL 2 |
| 51 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 52 | #define PCM_DEVICE 0 |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 53 | #define PCM_DEVICE_DEEP 1 |
Simon Wilson | a282d2f | 2012-09-12 16:14:24 -0700 | [diff] [blame] | 54 | #define PCM_DEVICE_VOICE 2 |
| 55 | #define PCM_DEVICE_SCO 3 |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 56 | |
Simon Wilson | 759c6f0 | 2013-01-15 16:38:56 -0800 | [diff] [blame] | 57 | #define MIXER_CARD 0 |
| 58 | |
Eric Laurent | 1a0c0a7 | 2012-08-24 11:29:04 -0700 | [diff] [blame] | 59 | /* duration in ms of volume ramp applied when starting capture to remove plop */ |
| 60 | #define CAPTURE_START_RAMP_MS 100 |
| 61 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 62 | /* default sampling for HDMI multichannel output */ |
| 63 | #define HDMI_MULTI_DEFAULT_SAMPLING_RATE 44100 |
| 64 | /* maximum number of channel mask configurations supported. Currently the primary |
| 65 | * output only supports 1 (stereo) and the multi channel HDMI output 2 (5.1 and 7.1) */ |
| 66 | #define MAX_SUPPORTED_CHANNEL_MASKS 2 |
| 67 | |
| 68 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) |
| 69 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 70 | struct pcm_config pcm_config = { |
| 71 | .channels = 2, |
| 72 | .rate = 44100, |
Glenn Kasten | 22db177 | 2012-09-04 09:26:50 -0700 | [diff] [blame] | 73 | .period_size = 256, |
Glenn Kasten | b936627 | 2012-07-18 14:06:12 -0700 | [diff] [blame] | 74 | .period_count = 2, |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 75 | .format = PCM_FORMAT_S16_LE, |
| 76 | }; |
| 77 | |
Glenn Kasten | 7ecf623 | 2012-09-28 12:01:31 -0700 | [diff] [blame] | 78 | struct pcm_config pcm_config_in = { |
| 79 | .channels = 2, |
| 80 | .rate = 44100, |
Glenn Kasten | 9ee451c | 2014-07-28 15:14:28 -0700 | [diff] [blame] | 81 | .period_size = 1024, |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 82 | .period_count = 2, |
| 83 | .format = PCM_FORMAT_S16_LE, |
| 84 | }; |
| 85 | |
| 86 | struct pcm_config pcm_config_in_low_latency = { |
| 87 | .channels = 2, |
| 88 | .rate = 44100, |
Glenn Kasten | 344070a | 2014-05-12 14:41:05 -0700 | [diff] [blame] | 89 | .period_size = 256, |
Glenn Kasten | 7ecf623 | 2012-09-28 12:01:31 -0700 | [diff] [blame] | 90 | .period_count = 2, |
| 91 | .format = PCM_FORMAT_S16_LE, |
| 92 | }; |
| 93 | |
Simon Wilson | a282d2f | 2012-09-12 16:14:24 -0700 | [diff] [blame] | 94 | struct pcm_config pcm_config_sco = { |
| 95 | .channels = 1, |
| 96 | .rate = 8000, |
| 97 | .period_size = 128, |
| 98 | .period_count = 2, |
| 99 | .format = PCM_FORMAT_S16_LE, |
| 100 | }; |
| 101 | |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 102 | struct pcm_config pcm_config_deep = { |
| 103 | .channels = 2, |
| 104 | .rate = 44100, |
| 105 | /* FIXME This is an arbitrary number, may change. |
| 106 | * Dynamic configuration based on screen on/off is not implemented; |
| 107 | * let's see what power consumption is first to see if necessary. |
| 108 | */ |
| 109 | .period_size = 8192, |
| 110 | .period_count = 2, |
| 111 | .format = PCM_FORMAT_S16_LE, |
| 112 | }; |
| 113 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 114 | struct pcm_config pcm_config_hdmi_multi = { |
| 115 | .channels = 6, /* changed when the stream is opened */ |
| 116 | .rate = HDMI_MULTI_DEFAULT_SAMPLING_RATE, |
| 117 | .period_size = 1024, |
| 118 | .period_count = 4, |
| 119 | .format = PCM_FORMAT_S16_LE, |
| 120 | }; |
| 121 | |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 122 | enum output_type { |
| 123 | OUTPUT_DEEP_BUF, // deep PCM buffers output stream |
| 124 | OUTPUT_LOW_LATENCY, // low latency output stream |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 125 | OUTPUT_HDMI, // HDMI multi channel |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 126 | OUTPUT_TOTAL |
| 127 | }; |
| 128 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 129 | struct audio_device { |
| 130 | struct audio_hw_device hw_device; |
| 131 | |
| 132 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 133 | audio_devices_t out_device; /* "or" of stream_out.device for all active output streams */ |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 134 | audio_devices_t in_device; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 135 | bool mic_mute; |
| 136 | struct audio_route *ar; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 137 | audio_source_t input_source; |
| 138 | int cur_route_id; /* current route ID: combination of input source |
| 139 | * and output device IDs */ |
Simon Wilson | a282d2f | 2012-09-12 16:14:24 -0700 | [diff] [blame] | 140 | struct pcm *pcm_voice_out; |
| 141 | struct pcm *pcm_sco_out; |
| 142 | struct pcm *pcm_voice_in; |
| 143 | struct pcm *pcm_sco_in; |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 144 | int es305_preset; |
Eric Laurent | a989ebf | 2012-10-15 15:14:44 -0700 | [diff] [blame] | 145 | int es305_new_mode; |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 146 | int es305_mode; |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 147 | int hdmi_drv_fd; /* either an fd >= 0 or -1 */ |
Eric Laurent | a989ebf | 2012-10-15 15:14:44 -0700 | [diff] [blame] | 148 | struct bubble_level *bubble_level; |
Eric Laurent | b52afc8 | 2013-01-18 15:12:57 -0800 | [diff] [blame] | 149 | audio_channel_mask_t in_channel_mask; |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 150 | unsigned int sco_on_count; |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 151 | |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 152 | struct stream_out *outputs[OUTPUT_TOTAL]; |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 153 | pthread_mutex_t lock_outputs; /* see note below on mutex acquisition order */ |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | struct stream_out { |
| 157 | struct audio_stream_out stream; |
| 158 | |
| 159 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 160 | struct pcm *pcm[PCM_TOTAL]; |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 161 | struct pcm_config config; |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 162 | unsigned int pcm_device; |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 163 | bool standby; /* true if all PCMs are inactive */ |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 164 | audio_devices_t device; |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 165 | /* FIXME: when HDMI multichannel output is active, other outputs must be disabled as |
| 166 | * HDMI and WM1811 share the same I2S. This means that notifications and other sounds are |
| 167 | * silent when watching a 5.1 movie. */ |
| 168 | bool disabled; |
| 169 | audio_channel_mask_t channel_mask; |
| 170 | /* Array of supported channel mask configurations. +1 so that the last entry is always 0 */ |
| 171 | audio_channel_mask_t supported_channel_masks[MAX_SUPPORTED_CHANNEL_MASKS + 1]; |
Eric Laurent | 3948fda | 2013-04-04 09:24:22 -0700 | [diff] [blame] | 172 | bool muted; |
Glenn Kasten | 21169cb | 2013-08-20 15:36:18 -0700 | [diff] [blame] | 173 | uint64_t written; /* total frames written, not cleared when entering standby */ |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 174 | |
| 175 | struct audio_device *dev; |
| 176 | }; |
| 177 | |
| 178 | struct stream_in { |
| 179 | struct audio_stream_in stream; |
| 180 | |
| 181 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
| 182 | struct pcm *pcm; |
| 183 | bool standby; |
| 184 | |
| 185 | unsigned int requested_rate; |
| 186 | struct resampler_itfe *resampler; |
| 187 | struct resampler_buffer_provider buf_provider; |
| 188 | int16_t *buffer; |
| 189 | size_t frames_in; |
| 190 | int read_status; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 191 | audio_source_t input_source; |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 192 | audio_io_handle_t io_handle; |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 193 | audio_devices_t device; |
Eric Laurent | 1a0c0a7 | 2012-08-24 11:29:04 -0700 | [diff] [blame] | 194 | uint16_t ramp_vol; |
| 195 | uint16_t ramp_step; |
| 196 | size_t ramp_frames; |
Eric Laurent | b52afc8 | 2013-01-18 15:12:57 -0800 | [diff] [blame] | 197 | audio_channel_mask_t channel_mask; |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 198 | audio_input_flags_t flags; |
| 199 | struct pcm_config *config; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 200 | |
| 201 | struct audio_device *dev; |
| 202 | }; |
| 203 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 204 | #define STRING_TO_ENUM(string) { #string, string } |
| 205 | |
| 206 | struct string_to_enum { |
| 207 | const char *name; |
| 208 | uint32_t value; |
| 209 | }; |
| 210 | |
| 211 | const struct string_to_enum out_channels_name_to_enum_table[] = { |
| 212 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO), |
| 213 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1), |
| 214 | STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1), |
| 215 | }; |
| 216 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 217 | enum { |
| 218 | OUT_DEVICE_SPEAKER, |
| 219 | OUT_DEVICE_HEADSET, |
| 220 | OUT_DEVICE_HEADPHONES, |
| 221 | OUT_DEVICE_BT_SCO, |
| 222 | OUT_DEVICE_SPEAKER_AND_HEADSET, |
| 223 | OUT_DEVICE_TAB_SIZE, /* number of rows in route_configs[][] */ |
| 224 | OUT_DEVICE_NONE, |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 225 | OUT_DEVICE_CNT |
| 226 | }; |
| 227 | |
| 228 | enum { |
| 229 | IN_SOURCE_MIC, |
| 230 | IN_SOURCE_CAMCORDER, |
| 231 | IN_SOURCE_VOICE_RECOGNITION, |
| 232 | IN_SOURCE_VOICE_COMMUNICATION, |
| 233 | IN_SOURCE_TAB_SIZE, /* number of lines in route_configs[][] */ |
| 234 | IN_SOURCE_NONE, |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 235 | IN_SOURCE_CNT |
| 236 | }; |
| 237 | |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 238 | enum { |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 239 | ES305_MODE_DEFAULT, |
Eric Laurent | a989ebf | 2012-10-15 15:14:44 -0700 | [diff] [blame] | 240 | ES305_MODE_LEVEL, |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 241 | ES305_NUM_MODES, |
| 242 | }; |
| 243 | |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 244 | int get_output_device_id(audio_devices_t device) |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 245 | { |
Eric Laurent | 6a466d7 | 2012-08-28 12:13:38 -0700 | [diff] [blame] | 246 | if (device == AUDIO_DEVICE_NONE) |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 247 | return OUT_DEVICE_NONE; |
| 248 | |
| 249 | if (popcount(device) == 2) { |
| 250 | if ((device == (AUDIO_DEVICE_OUT_SPEAKER | |
| 251 | AUDIO_DEVICE_OUT_WIRED_HEADSET)) || |
| 252 | (device == (AUDIO_DEVICE_OUT_SPEAKER | |
| 253 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE))) |
| 254 | return OUT_DEVICE_SPEAKER_AND_HEADSET; |
| 255 | else |
Simon Wilson | a6b9412 | 2012-09-12 12:52:24 -0700 | [diff] [blame] | 256 | return OUT_DEVICE_NONE; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | if (popcount(device) != 1) |
Simon Wilson | a6b9412 | 2012-09-12 12:52:24 -0700 | [diff] [blame] | 260 | return OUT_DEVICE_NONE; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 261 | |
| 262 | switch (device) { |
| 263 | case AUDIO_DEVICE_OUT_SPEAKER: |
| 264 | return OUT_DEVICE_SPEAKER; |
| 265 | case AUDIO_DEVICE_OUT_WIRED_HEADSET: |
| 266 | return OUT_DEVICE_HEADSET; |
| 267 | case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: |
| 268 | return OUT_DEVICE_HEADPHONES; |
| 269 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO: |
| 270 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET: |
| 271 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT: |
| 272 | return OUT_DEVICE_BT_SCO; |
| 273 | default: |
Simon Wilson | a6b9412 | 2012-09-12 12:52:24 -0700 | [diff] [blame] | 274 | return OUT_DEVICE_NONE; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 275 | } |
| 276 | } |
| 277 | |
| 278 | int get_input_source_id(audio_source_t source) |
| 279 | { |
| 280 | switch (source) { |
| 281 | case AUDIO_SOURCE_DEFAULT: |
| 282 | return IN_SOURCE_NONE; |
| 283 | case AUDIO_SOURCE_MIC: |
| 284 | return IN_SOURCE_MIC; |
| 285 | case AUDIO_SOURCE_CAMCORDER: |
| 286 | return IN_SOURCE_CAMCORDER; |
| 287 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
| 288 | return IN_SOURCE_VOICE_RECOGNITION; |
| 289 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
| 290 | return IN_SOURCE_VOICE_COMMUNICATION; |
| 291 | default: |
Simon Wilson | a6b9412 | 2012-09-12 12:52:24 -0700 | [diff] [blame] | 292 | return IN_SOURCE_NONE; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | struct route_config { |
| 297 | const char * const output_route; |
| 298 | const char * const input_route; |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 299 | int es305_preset[ES305_NUM_MODES]; // es305 preset for this route. |
| 300 | // -1 means es305 bypass |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 301 | }; |
| 302 | |
| 303 | const struct route_config media_speaker = { |
| 304 | "media-speaker", |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 305 | "media-main-mic", |
| 306 | { ES305_PRESET_OFF, |
| 307 | ES305_PRESET_OFF } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | const struct route_config media_headphones = { |
| 311 | "media-headphones", |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 312 | "media-main-mic", |
| 313 | { ES305_PRESET_OFF, |
| 314 | ES305_PRESET_OFF } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 315 | }; |
| 316 | |
| 317 | const struct route_config media_headset = { |
| 318 | "media-headphones", |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 319 | "media-headset-mic", |
| 320 | { ES305_PRESET_OFF, |
| 321 | ES305_PRESET_OFF } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | const struct route_config camcorder_speaker = { |
| 325 | "media-speaker", |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 326 | "media-second-mic", |
Jean-Michel Trivi | 48277c5 | 2012-10-08 10:22:59 -0700 | [diff] [blame] | 327 | { ES305_PRESET_CAMCORDER, |
| 328 | ES305_PRESET_CAMCORDER } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | const struct route_config camcorder_headphones = { |
| 332 | "media-headphones", |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 333 | "media-second-mic", |
Jean-Michel Trivi | 48277c5 | 2012-10-08 10:22:59 -0700 | [diff] [blame] | 334 | { ES305_PRESET_CAMCORDER, |
| 335 | ES305_PRESET_CAMCORDER } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | const struct route_config voice_rec_speaker = { |
| 339 | "voice-rec-speaker", |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 340 | "voice-rec-main-mic", |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 341 | { ES305_PRESET_ASRA_HANDHELD, |
| 342 | ES305_PRESET_ASRA_DESKTOP } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 343 | }; |
| 344 | |
| 345 | const struct route_config voice_rec_headphones = { |
| 346 | "voice-rec-headphones", |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 347 | "voice-rec-main-mic", |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 348 | { ES305_PRESET_ASRA_HANDHELD, |
| 349 | ES305_PRESET_ASRA_DESKTOP } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 350 | }; |
| 351 | |
| 352 | const struct route_config voice_rec_headset = { |
| 353 | "voice-rec-headphones", |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 354 | "voice-rec-headset-mic", |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 355 | { ES305_PRESET_ASRA_HEADSET, |
| 356 | ES305_PRESET_ASRA_HEADSET } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 357 | }; |
| 358 | |
| 359 | const struct route_config communication_speaker = { |
| 360 | "communication-speaker", |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 361 | "communication-main-mic", |
| 362 | { ES305_PRESET_VOIP_HANDHELD, |
| 363 | ES305_PRESET_VOIP_DESKTOP } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 364 | }; |
| 365 | |
| 366 | const struct route_config communication_headphones = { |
| 367 | "communication-headphones", |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 368 | "communication-main-mic", |
| 369 | { ES305_PRESET_VOIP_HEADPHONES, |
| 370 | ES305_PRESET_VOIP_HP_DESKTOP} |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 371 | }; |
| 372 | |
| 373 | const struct route_config communication_headset = { |
| 374 | "communication-headphones", |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 375 | "communication-headset-mic", |
| 376 | { ES305_PRESET_VOIP_HEADSET, |
| 377 | ES305_PRESET_VOIP_HEADSET } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 378 | }; |
| 379 | |
| 380 | const struct route_config speaker_and_headphones = { |
| 381 | "speaker-and-headphones", |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 382 | "main-mic", |
| 383 | { ES305_PRESET_CURRENT, |
| 384 | ES305_PRESET_CURRENT } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | const struct route_config bluetooth_sco = { |
| 388 | "bt-sco-headset", |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 389 | "bt-sco-mic", |
| 390 | { ES305_PRESET_OFF, |
| 391 | ES305_PRESET_OFF } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 392 | }; |
| 393 | |
| 394 | const struct route_config * const route_configs[IN_SOURCE_TAB_SIZE] |
| 395 | [OUT_DEVICE_TAB_SIZE] = { |
| 396 | { /* IN_SOURCE_MIC */ |
| 397 | &media_speaker, /* OUT_DEVICE_SPEAKER */ |
| 398 | &media_headset, /* OUT_DEVICE_HEADSET */ |
| 399 | &media_headphones, /* OUT_DEVICE_HEADPHONES */ |
| 400 | &bluetooth_sco, /* OUT_DEVICE_BT_SCO */ |
| 401 | &speaker_and_headphones /* OUT_DEVICE_SPEAKER_AND_HEADSET */ |
| 402 | }, |
| 403 | { /* IN_SOURCE_CAMCORDER */ |
| 404 | &camcorder_speaker, /* OUT_DEVICE_SPEAKER */ |
| 405 | &camcorder_headphones, /* OUT_DEVICE_HEADSET */ |
| 406 | &camcorder_headphones, /* OUT_DEVICE_HEADPHONES */ |
| 407 | &bluetooth_sco, /* OUT_DEVICE_BT_SCO */ |
| 408 | &speaker_and_headphones /* OUT_DEVICE_SPEAKER_AND_HEADSET */ |
| 409 | }, |
| 410 | { /* IN_SOURCE_VOICE_RECOGNITION */ |
| 411 | &voice_rec_speaker, /* OUT_DEVICE_SPEAKER */ |
| 412 | &voice_rec_headset, /* OUT_DEVICE_HEADSET */ |
| 413 | &voice_rec_headphones, /* OUT_DEVICE_HEADPHONES */ |
| 414 | &bluetooth_sco, /* OUT_DEVICE_BT_SCO */ |
| 415 | &speaker_and_headphones /* OUT_DEVICE_SPEAKER_AND_HEADSET */ |
| 416 | }, |
| 417 | { /* IN_SOURCE_VOICE_COMMUNICATION */ |
| 418 | &communication_speaker, /* OUT_DEVICE_SPEAKER */ |
| 419 | &communication_headset, /* OUT_DEVICE_HEADSET */ |
| 420 | &communication_headphones, /* OUT_DEVICE_HEADPHONES */ |
| 421 | &bluetooth_sco, /* OUT_DEVICE_BT_SCO */ |
| 422 | &speaker_and_headphones /* OUT_DEVICE_SPEAKER_AND_HEADSET */ |
| 423 | } |
| 424 | }; |
| 425 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 426 | static void do_out_standby(struct stream_out *out); |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 427 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 428 | /** |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 429 | * NOTE: when multiple mutexes have to be acquired, always respect the following order: |
| 430 | * lock_outputs for hw device outputs list only |
| 431 | * in stream |
| 432 | * out stream(s) in enum output_type order |
| 433 | * hw device |
| 434 | * TODO investigate whether we ever actually take both in stream and out stream |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 435 | */ |
| 436 | |
| 437 | /* Helper functions */ |
| 438 | |
Glenn Kasten | 6408391 | 2014-09-05 10:33:42 -0700 | [diff] [blame] | 439 | /* must be called with hw device mutex locked */ |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 440 | static int open_hdmi_driver(struct audio_device *adev) |
Simon Wilson | f051bcf | 2012-09-26 14:10:07 -0700 | [diff] [blame] | 441 | { |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 442 | if (adev->hdmi_drv_fd < 0) { |
| 443 | adev->hdmi_drv_fd = open("/dev/video16", O_RDWR); |
| 444 | if (adev->hdmi_drv_fd < 0) |
| 445 | ALOGE("%s cannot open video16 (%d)", __func__, adev->hdmi_drv_fd); |
| 446 | } |
| 447 | return adev->hdmi_drv_fd; |
| 448 | } |
| 449 | |
| 450 | /* must be called with hw device mutex locked */ |
| 451 | static int enable_hdmi_audio(struct audio_device *adev, int enable) |
| 452 | { |
Simon Wilson | f051bcf | 2012-09-26 14:10:07 -0700 | [diff] [blame] | 453 | int ret; |
| 454 | struct v4l2_control ctrl; |
| 455 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 456 | ret = open_hdmi_driver(adev); |
| 457 | if (ret < 0) |
| 458 | return ret; |
Simon Wilson | f051bcf | 2012-09-26 14:10:07 -0700 | [diff] [blame] | 459 | |
| 460 | ctrl.id = V4L2_CID_TV_ENABLE_HDMI_AUDIO; |
| 461 | ctrl.value = !!enable; |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 462 | ret = ioctl(adev->hdmi_drv_fd, VIDIOC_S_CTRL, &ctrl); |
Simon Wilson | f051bcf | 2012-09-26 14:10:07 -0700 | [diff] [blame] | 463 | |
| 464 | if (ret < 0) |
| 465 | ALOGE("V4L2_CID_TV_ENABLE_HDMI_AUDIO ioctl error (%d)", errno); |
| 466 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 467 | return ret; |
| 468 | } |
| 469 | |
Glenn Kasten | 6408391 | 2014-09-05 10:33:42 -0700 | [diff] [blame] | 470 | /* must be called with hw device mutex locked |
| 471 | * Called from adev_open_output_stream with no stream lock, |
| 472 | * but this is OK because stream is not yet visible |
| 473 | */ |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 474 | static int read_hdmi_channel_masks(struct audio_device *adev, struct stream_out *out) { |
| 475 | int ret; |
| 476 | struct v4l2_control ctrl; |
| 477 | |
| 478 | ret = open_hdmi_driver(adev); |
| 479 | if (ret < 0) |
| 480 | return ret; |
| 481 | |
| 482 | ctrl.id = V4L2_CID_TV_MAX_AUDIO_CHANNELS; |
| 483 | ret = ioctl(adev->hdmi_drv_fd, VIDIOC_G_CTRL, &ctrl); |
| 484 | if (ret < 0) { |
| 485 | ALOGE("V4L2_CID_TV_MAX_AUDIO_CHANNELS ioctl error (%d)", errno); |
| 486 | return ret; |
| 487 | } |
| 488 | |
| 489 | ALOGV("%s ioctl %d got %d max channels", __func__, ret, ctrl.value); |
| 490 | |
| 491 | if (ctrl.value != 6 && ctrl.value != 8) |
| 492 | return -ENOSYS; |
| 493 | |
| 494 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_5POINT1; |
| 495 | if (ctrl.value == 8) |
| 496 | out->supported_channel_masks[1] = AUDIO_CHANNEL_OUT_7POINT1; |
| 497 | |
| 498 | return ret; |
| 499 | } |
| 500 | |
| 501 | /* must be called with hw device mutex locked */ |
| 502 | static int set_hdmi_channels(struct audio_device *adev, int channels) { |
| 503 | int ret; |
| 504 | struct v4l2_control ctrl; |
| 505 | |
| 506 | ret = open_hdmi_driver(adev); |
| 507 | if (ret < 0) |
| 508 | return ret; |
| 509 | |
| 510 | ctrl.id = V4L2_CID_TV_SET_NUM_CHANNELS; |
| 511 | ctrl.value = channels; |
| 512 | ret = ioctl(adev->hdmi_drv_fd, VIDIOC_S_CTRL, &ctrl); |
| 513 | if (ret < 0) |
| 514 | ALOGE("V4L2_CID_TV_SET_NUM_CHANNELS ioctl error (%d)", errno); |
Simon Wilson | f051bcf | 2012-09-26 14:10:07 -0700 | [diff] [blame] | 515 | |
| 516 | return ret; |
| 517 | } |
| 518 | |
Glenn Kasten | 6408391 | 2014-09-05 10:33:42 -0700 | [diff] [blame] | 519 | /* must be called with hw device mutex locked */ |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 520 | static void select_devices(struct audio_device *adev) |
| 521 | { |
Eric Laurent | 6a466d7 | 2012-08-28 12:13:38 -0700 | [diff] [blame] | 522 | int output_device_id = get_output_device_id(adev->out_device); |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 523 | int input_source_id = get_input_source_id(adev->input_source); |
Simon Wilson | c4006be | 2012-08-17 11:23:38 -0700 | [diff] [blame] | 524 | const char *output_route = NULL; |
| 525 | const char *input_route = NULL; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 526 | int new_route_id; |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 527 | int new_es305_preset = -1; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 528 | |
Simon Wilson | 759c6f0 | 2013-01-15 16:38:56 -0800 | [diff] [blame] | 529 | audio_route_reset(adev->ar); |
Simon Wilson | c4006be | 2012-08-17 11:23:38 -0700 | [diff] [blame] | 530 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 531 | enable_hdmi_audio(adev, adev->out_device & AUDIO_DEVICE_OUT_AUX_DIGITAL); |
Simon Wilson | ab5dda6 | 2012-10-01 17:44:38 -0700 | [diff] [blame] | 532 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 533 | new_route_id = (1 << (input_source_id + OUT_DEVICE_CNT)) + (1 << output_device_id); |
Eric Laurent | a989ebf | 2012-10-15 15:14:44 -0700 | [diff] [blame] | 534 | if ((new_route_id == adev->cur_route_id) && (adev->es305_mode == adev->es305_new_mode)) |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 535 | return; |
| 536 | adev->cur_route_id = new_route_id; |
Eric Laurent | a989ebf | 2012-10-15 15:14:44 -0700 | [diff] [blame] | 537 | adev->es305_mode = adev->es305_new_mode; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 538 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 539 | if (input_source_id != IN_SOURCE_NONE) { |
| 540 | if (output_device_id != OUT_DEVICE_NONE) { |
| 541 | input_route = |
| 542 | route_configs[input_source_id][output_device_id]->input_route; |
| 543 | output_route = |
| 544 | route_configs[input_source_id][output_device_id]->output_route; |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 545 | new_es305_preset = |
| 546 | route_configs[input_source_id][output_device_id]->es305_preset[adev->es305_mode]; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 547 | } else { |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 548 | switch (adev->in_device) { |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 549 | case AUDIO_DEVICE_IN_WIRED_HEADSET & ~AUDIO_DEVICE_BIT_IN: |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 550 | output_device_id = OUT_DEVICE_HEADSET; |
| 551 | break; |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 552 | case AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET & ~AUDIO_DEVICE_BIT_IN: |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 553 | output_device_id = OUT_DEVICE_BT_SCO; |
| 554 | break; |
| 555 | default: |
| 556 | output_device_id = OUT_DEVICE_SPEAKER; |
| 557 | break; |
| 558 | } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 559 | input_route = |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 560 | route_configs[input_source_id][output_device_id]->input_route; |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 561 | new_es305_preset = |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 562 | route_configs[input_source_id][output_device_id]->es305_preset[adev->es305_mode]; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 563 | } |
Eric Laurent | b52afc8 | 2013-01-18 15:12:57 -0800 | [diff] [blame] | 564 | // disable noise suppression when capturing front and back mic for voice recognition |
| 565 | if ((adev->input_source == AUDIO_SOURCE_VOICE_RECOGNITION) && |
Jean-Michel Trivi | 3f467de | 2013-06-06 15:26:01 -0700 | [diff] [blame] | 566 | (adev->in_channel_mask == AUDIO_CHANNEL_IN_FRONT_BACK)) |
Eric Laurent | b52afc8 | 2013-01-18 15:12:57 -0800 | [diff] [blame] | 567 | new_es305_preset = -1; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 568 | } else { |
| 569 | if (output_device_id != OUT_DEVICE_NONE) { |
| 570 | output_route = |
| 571 | route_configs[IN_SOURCE_MIC][output_device_id]->output_route; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | ALOGV("select_devices() devices %#x input src %d output route %s input route %s", |
Eric Laurent | 6a466d7 | 2012-08-28 12:13:38 -0700 | [diff] [blame] | 576 | adev->out_device, adev->input_source, |
Simon Wilson | c4006be | 2012-08-17 11:23:38 -0700 | [diff] [blame] | 577 | output_route ? output_route : "none", |
| 578 | input_route ? input_route : "none"); |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 579 | |
Simon Wilson | c4006be | 2012-08-17 11:23:38 -0700 | [diff] [blame] | 580 | if (output_route) |
| 581 | audio_route_apply_path(adev->ar, output_route); |
| 582 | if (input_route) |
| 583 | audio_route_apply_path(adev->ar, input_route); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 584 | |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 585 | if ((new_es305_preset != ES305_PRESET_CURRENT) && |
| 586 | (new_es305_preset != adev->es305_preset)) { |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 587 | ALOGV(" select_devices() changing es305 preset from %d to %d", |
| 588 | adev->es305_preset, new_es305_preset); |
| 589 | if (eS305_UsePreset(new_es305_preset) == 0) { |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 590 | adev->es305_preset = new_es305_preset; |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 591 | } |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Simon Wilson | 759c6f0 | 2013-01-15 16:38:56 -0800 | [diff] [blame] | 594 | audio_route_update_mixer(adev->ar); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 595 | } |
| 596 | |
Glenn Kasten | 6408391 | 2014-09-05 10:33:42 -0700 | [diff] [blame] | 597 | /* must be called with hw device mutex unlocked */ |
Eric Laurent | a989ebf | 2012-10-15 15:14:44 -0700 | [diff] [blame] | 598 | void bubblelevel_callback(bool is_level, void *user_data) |
| 599 | { |
| 600 | struct audio_device *adev = (struct audio_device *)user_data; |
| 601 | int es305_mode; |
| 602 | |
| 603 | if (is_level) |
| 604 | es305_mode = ES305_MODE_LEVEL; |
| 605 | else |
| 606 | es305_mode = ES305_MODE_DEFAULT; |
| 607 | |
| 608 | pthread_mutex_lock(&adev->lock); |
| 609 | if (es305_mode != adev->es305_mode) { |
| 610 | adev->es305_new_mode = es305_mode; |
| 611 | select_devices(adev); |
| 612 | ALOGV("bubblelevel_callback is_level %d es305_mode %d", is_level, es305_mode); |
| 613 | } |
| 614 | pthread_mutex_unlock(&adev->lock); |
| 615 | } |
| 616 | |
Eric Laurent | 429f24d | 2012-11-02 14:37:38 -0700 | [diff] [blame] | 617 | /* must be called with hw device mutex locked */ |
| 618 | bool get_bubblelevel(struct audio_device *adev) |
| 619 | { |
| 620 | if (!adev->bubble_level) { |
| 621 | adev->bubble_level = bubble_level_create(); |
| 622 | if (adev->bubble_level) |
| 623 | adev->bubble_level->set_callback(adev->bubble_level, bubblelevel_callback, adev); |
| 624 | } |
| 625 | return (adev->bubble_level != NULL); |
| 626 | } |
| 627 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 628 | /* must be called with hw device outputs list, all out streams, and hw device mutexes locked */ |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 629 | static void force_non_hdmi_out_standby(struct audio_device *adev) |
| 630 | { |
| 631 | enum output_type type; |
| 632 | struct stream_out *out; |
| 633 | |
| 634 | for (type = 0; type < OUTPUT_TOTAL; ++type) { |
| 635 | out = adev->outputs[type]; |
| 636 | if (type == OUTPUT_HDMI || !out) |
| 637 | continue; |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 638 | /* This will never recurse more than 2 levels deep. */ |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 639 | do_out_standby(out); |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 643 | /* must be called with the hw device mutex locked, OK to hold other mutexes */ |
| 644 | static void start_bt_sco(struct audio_device *adev) { |
| 645 | if (adev->sco_on_count++ > 0) |
| 646 | return; |
| 647 | |
Glenn Kasten | 21169cb | 2013-08-20 15:36:18 -0700 | [diff] [blame] | 648 | adev->pcm_voice_out = pcm_open(PCM_CARD, PCM_DEVICE_VOICE, PCM_OUT | PCM_MONOTONIC, |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 649 | &pcm_config_sco); |
| 650 | if (adev->pcm_voice_out && !pcm_is_ready(adev->pcm_voice_out)) { |
| 651 | ALOGE("pcm_open(VOICE_OUT) failed: %s", pcm_get_error(adev->pcm_voice_out)); |
| 652 | goto err_voice_out; |
| 653 | } |
Glenn Kasten | 21169cb | 2013-08-20 15:36:18 -0700 | [diff] [blame] | 654 | adev->pcm_sco_out = pcm_open(PCM_CARD, PCM_DEVICE_SCO, PCM_OUT | PCM_MONOTONIC, |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 655 | &pcm_config_sco); |
| 656 | if (adev->pcm_sco_out && !pcm_is_ready(adev->pcm_sco_out)) { |
| 657 | ALOGE("pcm_open(SCO_OUT) failed: %s", pcm_get_error(adev->pcm_sco_out)); |
| 658 | goto err_sco_out; |
| 659 | } |
| 660 | adev->pcm_voice_in = pcm_open(PCM_CARD, PCM_DEVICE_VOICE, PCM_IN, |
| 661 | &pcm_config_sco); |
| 662 | if (adev->pcm_voice_in && !pcm_is_ready(adev->pcm_voice_in)) { |
| 663 | ALOGE("pcm_open(VOICE_IN) failed: %s", pcm_get_error(adev->pcm_voice_in)); |
| 664 | goto err_voice_in; |
| 665 | } |
| 666 | adev->pcm_sco_in = pcm_open(PCM_CARD, PCM_DEVICE_SCO, PCM_IN, |
| 667 | &pcm_config_sco); |
| 668 | if (adev->pcm_sco_in && !pcm_is_ready(adev->pcm_sco_in)) { |
| 669 | ALOGE("pcm_open(SCO_IN) failed: %s", pcm_get_error(adev->pcm_sco_in)); |
| 670 | goto err_sco_in; |
| 671 | } |
| 672 | |
| 673 | pcm_start(adev->pcm_voice_out); |
| 674 | pcm_start(adev->pcm_sco_out); |
| 675 | pcm_start(adev->pcm_voice_in); |
| 676 | pcm_start(adev->pcm_sco_in); |
| 677 | |
| 678 | return; |
| 679 | |
| 680 | err_sco_in: |
| 681 | pcm_close(adev->pcm_sco_in); |
| 682 | err_voice_in: |
| 683 | pcm_close(adev->pcm_voice_in); |
| 684 | err_sco_out: |
| 685 | pcm_close(adev->pcm_sco_out); |
| 686 | err_voice_out: |
| 687 | pcm_close(adev->pcm_voice_out); |
| 688 | } |
| 689 | |
| 690 | /* must be called with the hw device mutex locked, OK to hold other mutexes */ |
| 691 | static void stop_bt_sco(struct audio_device *adev) { |
| 692 | if (adev->sco_on_count == 0 || --adev->sco_on_count > 0) |
| 693 | return; |
| 694 | |
| 695 | pcm_stop(adev->pcm_voice_out); |
| 696 | pcm_stop(adev->pcm_sco_out); |
| 697 | pcm_stop(adev->pcm_voice_in); |
| 698 | pcm_stop(adev->pcm_sco_in); |
| 699 | |
| 700 | pcm_close(adev->pcm_voice_out); |
| 701 | pcm_close(adev->pcm_sco_out); |
| 702 | pcm_close(adev->pcm_voice_in); |
| 703 | pcm_close(adev->pcm_sco_in); |
| 704 | } |
| 705 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 706 | /* must be called with hw device outputs list, output stream, and hw device mutexes locked */ |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 707 | static int start_output_stream(struct stream_out *out) |
| 708 | { |
| 709 | struct audio_device *adev = out->dev; |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 710 | int type; |
| 711 | |
| 712 | if (out == adev->outputs[OUTPUT_HDMI]) { |
| 713 | force_non_hdmi_out_standby(adev); |
| 714 | } else if (adev->outputs[OUTPUT_HDMI] && !adev->outputs[OUTPUT_HDMI]->standby) { |
| 715 | out->disabled = true; |
| 716 | return 0; |
| 717 | } |
| 718 | |
| 719 | out->disabled = false; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 720 | |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 721 | if (out->device & (AUDIO_DEVICE_OUT_SPEAKER | |
| 722 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 723 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
Simon Wilson | a282d2f | 2012-09-12 16:14:24 -0700 | [diff] [blame] | 724 | AUDIO_DEVICE_OUT_AUX_DIGITAL | |
| 725 | AUDIO_DEVICE_OUT_ALL_SCO)) { |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 726 | |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 727 | out->pcm[PCM_CARD] = pcm_open(PCM_CARD, out->pcm_device, |
Glenn Kasten | 21169cb | 2013-08-20 15:36:18 -0700 | [diff] [blame] | 728 | PCM_OUT | PCM_MONOTONIC, &out->config); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 729 | |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 730 | if (out->pcm[PCM_CARD] && !pcm_is_ready(out->pcm[PCM_CARD])) { |
| 731 | ALOGE("pcm_open(PCM_CARD) failed: %s", |
| 732 | pcm_get_error(out->pcm[PCM_CARD])); |
| 733 | pcm_close(out->pcm[PCM_CARD]); |
| 734 | return -ENOMEM; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | if (out->device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) { |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 739 | out->pcm[PCM_CARD_SPDIF] = pcm_open(PCM_CARD_SPDIF, out->pcm_device, |
Glenn Kasten | 21169cb | 2013-08-20 15:36:18 -0700 | [diff] [blame] | 740 | PCM_OUT | PCM_MONOTONIC, &out->config); |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 741 | |
| 742 | if (out->pcm[PCM_CARD_SPDIF] && |
| 743 | !pcm_is_ready(out->pcm[PCM_CARD_SPDIF])) { |
| 744 | ALOGE("pcm_open(PCM_CARD_SPDIF) failed: %s", |
| 745 | pcm_get_error(out->pcm[PCM_CARD_SPDIF])); |
| 746 | pcm_close(out->pcm[PCM_CARD_SPDIF]); |
| 747 | return -ENOMEM; |
| 748 | } |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 751 | adev->out_device |= out->device; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 752 | select_devices(adev); |
| 753 | |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 754 | if (out->device & AUDIO_DEVICE_OUT_ALL_SCO) |
| 755 | start_bt_sco(adev); |
| 756 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 757 | if (out->device & AUDIO_DEVICE_OUT_AUX_DIGITAL) |
| 758 | set_hdmi_channels(adev, out->config.channels); |
| 759 | |
Eric Laurent | a989ebf | 2012-10-15 15:14:44 -0700 | [diff] [blame] | 760 | /* anticipate level measurement in case we start capture later */ |
Eric Laurent | 429f24d | 2012-11-02 14:37:38 -0700 | [diff] [blame] | 761 | if (get_bubblelevel(adev)) |
| 762 | adev->bubble_level->poll_once(adev->bubble_level); |
Eric Laurent | a989ebf | 2012-10-15 15:14:44 -0700 | [diff] [blame] | 763 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 764 | return 0; |
| 765 | } |
| 766 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 767 | /* must be called with input stream and hw device mutexes locked */ |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 768 | static int start_input_stream(struct stream_in *in) |
| 769 | { |
| 770 | struct audio_device *adev = in->dev; |
| 771 | |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 772 | in->pcm = pcm_open(PCM_CARD, PCM_DEVICE, PCM_IN, in->config); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 773 | |
| 774 | if (in->pcm && !pcm_is_ready(in->pcm)) { |
| 775 | ALOGE("pcm_open() failed: %s", pcm_get_error(in->pcm)); |
| 776 | pcm_close(in->pcm); |
| 777 | return -ENOMEM; |
| 778 | } |
| 779 | |
| 780 | /* if no supported sample rate is available, use the resampler */ |
| 781 | if (in->resampler) |
| 782 | in->resampler->reset(in->resampler); |
| 783 | |
Eric Laurent | f423182 | 2012-08-22 16:06:38 -0700 | [diff] [blame] | 784 | in->frames_in = 0; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 785 | adev->input_source = in->input_source; |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 786 | adev->in_device = in->device; |
Jean-Michel Trivi | 3f467de | 2013-06-06 15:26:01 -0700 | [diff] [blame] | 787 | adev->in_channel_mask = in->channel_mask; |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 788 | |
| 789 | eS305_SetActiveIoHandle(in->io_handle); |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 790 | select_devices(adev); |
| 791 | |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 792 | if (in->device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) |
| 793 | start_bt_sco(adev); |
| 794 | |
Eric Laurent | 1a0c0a7 | 2012-08-24 11:29:04 -0700 | [diff] [blame] | 795 | /* initialize volume ramp */ |
| 796 | in->ramp_frames = (CAPTURE_START_RAMP_MS * in->requested_rate) / 1000; |
| 797 | in->ramp_step = (uint16_t)(USHRT_MAX / in->ramp_frames); |
| 798 | in->ramp_vol = 0;; |
| 799 | |
Eric Laurent | 429f24d | 2012-11-02 14:37:38 -0700 | [diff] [blame] | 800 | if (get_bubblelevel(adev)) { |
| 801 | adev->bubble_level->set_poll_interval(adev->bubble_level, BL_POLL_INTERVAL_MIN_SEC); |
| 802 | adev->bubble_level->start_polling(adev->bubble_level); |
| 803 | } |
Eric Laurent | a989ebf | 2012-10-15 15:14:44 -0700 | [diff] [blame] | 804 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | static size_t get_input_buffer_size(unsigned int sample_rate, |
| 809 | audio_format_t format, |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 810 | unsigned int channel_count, |
| 811 | bool is_low_latency) |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 812 | { |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 813 | const struct pcm_config *config = is_low_latency ? |
| 814 | &pcm_config_in_low_latency : &pcm_config_in; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 815 | size_t size; |
| 816 | |
| 817 | /* |
| 818 | * take resampling into account and return the closest majoring |
| 819 | * multiple of 16 frames, as audioflinger expects audio buffers to |
| 820 | * be a multiple of 16 frames |
| 821 | */ |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 822 | size = (config->period_size * sample_rate) / config->rate; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 823 | size = ((size + 15) / 16) * 16; |
| 824 | |
| 825 | return size * channel_count * audio_bytes_per_sample(format); |
| 826 | } |
| 827 | |
| 828 | static int get_next_buffer(struct resampler_buffer_provider *buffer_provider, |
| 829 | struct resampler_buffer* buffer) |
| 830 | { |
| 831 | struct stream_in *in; |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 832 | size_t i; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 833 | |
| 834 | if (buffer_provider == NULL || buffer == NULL) |
| 835 | return -EINVAL; |
| 836 | |
| 837 | in = (struct stream_in *)((char *)buffer_provider - |
| 838 | offsetof(struct stream_in, buf_provider)); |
| 839 | |
| 840 | if (in->pcm == NULL) { |
| 841 | buffer->raw = NULL; |
| 842 | buffer->frame_count = 0; |
| 843 | in->read_status = -ENODEV; |
| 844 | return -ENODEV; |
| 845 | } |
| 846 | |
| 847 | if (in->frames_in == 0) { |
| 848 | in->read_status = pcm_read(in->pcm, |
| 849 | (void*)in->buffer, |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 850 | pcm_frames_to_bytes(in->pcm, in->config->period_size)); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 851 | if (in->read_status != 0) { |
| 852 | ALOGE("get_next_buffer() pcm_read error %d", in->read_status); |
| 853 | buffer->raw = NULL; |
| 854 | buffer->frame_count = 0; |
| 855 | return in->read_status; |
| 856 | } |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 857 | |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 858 | in->frames_in = in->config->period_size; |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 859 | |
| 860 | /* Do stereo to mono conversion in place by discarding right channel */ |
Eric Laurent | b52afc8 | 2013-01-18 15:12:57 -0800 | [diff] [blame] | 861 | if (in->channel_mask == AUDIO_CHANNEL_IN_MONO) |
| 862 | for (i = 1; i < in->frames_in; i++) |
| 863 | in->buffer[i] = in->buffer[i * 2]; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | buffer->frame_count = (buffer->frame_count > in->frames_in) ? |
| 867 | in->frames_in : buffer->frame_count; |
Eric Laurent | b52afc8 | 2013-01-18 15:12:57 -0800 | [diff] [blame] | 868 | buffer->i16 = in->buffer + |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 869 | (in->config->period_size - in->frames_in) * |
Eric Laurent | 1e7184b | 2014-07-01 20:29:10 -0700 | [diff] [blame] | 870 | audio_channel_count_from_in_mask(in->channel_mask); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 871 | |
| 872 | return in->read_status; |
| 873 | |
| 874 | } |
| 875 | |
| 876 | static void release_buffer(struct resampler_buffer_provider *buffer_provider, |
| 877 | struct resampler_buffer* buffer) |
| 878 | { |
| 879 | struct stream_in *in; |
| 880 | |
| 881 | if (buffer_provider == NULL || buffer == NULL) |
| 882 | return; |
| 883 | |
| 884 | in = (struct stream_in *)((char *)buffer_provider - |
| 885 | offsetof(struct stream_in, buf_provider)); |
| 886 | |
| 887 | in->frames_in -= buffer->frame_count; |
| 888 | } |
| 889 | |
| 890 | /* read_frames() reads frames from kernel driver, down samples to capture rate |
| 891 | * if necessary and output the number of frames requested to the buffer specified */ |
| 892 | static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames) |
| 893 | { |
| 894 | ssize_t frames_wr = 0; |
Eric Laurent | 84c3373 | 2014-07-03 16:35:46 -0700 | [diff] [blame] | 895 | size_t frame_size = audio_stream_in_frame_size(&in->stream); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 896 | |
| 897 | while (frames_wr < frames) { |
| 898 | size_t frames_rd = frames - frames_wr; |
| 899 | if (in->resampler != NULL) { |
| 900 | in->resampler->resample_from_provider(in->resampler, |
| 901 | (int16_t *)((char *)buffer + |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 902 | frames_wr * frame_size), |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 903 | &frames_rd); |
| 904 | } else { |
| 905 | struct resampler_buffer buf = { |
| 906 | { raw : NULL, }, |
| 907 | frame_count : frames_rd, |
| 908 | }; |
| 909 | get_next_buffer(&in->buf_provider, &buf); |
| 910 | if (buf.raw != NULL) { |
| 911 | memcpy((char *)buffer + |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 912 | frames_wr * frame_size, |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 913 | buf.raw, |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 914 | buf.frame_count * frame_size); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 915 | frames_rd = buf.frame_count; |
| 916 | } |
| 917 | release_buffer(&in->buf_provider, &buf); |
| 918 | } |
| 919 | /* in->read_status is updated by getNextBuffer() also called by |
| 920 | * in->resampler->resample_from_provider() */ |
| 921 | if (in->read_status != 0) |
| 922 | return in->read_status; |
| 923 | |
| 924 | frames_wr += frames_rd; |
| 925 | } |
| 926 | return frames_wr; |
| 927 | } |
| 928 | |
| 929 | /* API functions */ |
| 930 | |
| 931 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 932 | { |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 933 | struct stream_out *out = (struct stream_out *)stream; |
| 934 | |
| 935 | return out->config.rate; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 939 | { |
| 940 | return -ENOSYS; |
| 941 | } |
| 942 | |
| 943 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 944 | { |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 945 | struct stream_out *out = (struct stream_out *)stream; |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 946 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 947 | return out->config.period_size * |
Eric Laurent | 84c3373 | 2014-07-03 16:35:46 -0700 | [diff] [blame] | 948 | audio_stream_out_frame_size((const struct audio_stream_out *)stream); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 949 | } |
| 950 | |
Glenn Kasten | ca1414b | 2012-06-25 10:38:47 -0700 | [diff] [blame] | 951 | static audio_channel_mask_t out_get_channels(const struct audio_stream *stream) |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 952 | { |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 953 | struct stream_out *out = (struct stream_out *)stream; |
| 954 | |
| 955 | return out->channel_mask; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 959 | { |
| 960 | return AUDIO_FORMAT_PCM_16_BIT; |
| 961 | } |
| 962 | |
| 963 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 964 | { |
| 965 | return -ENOSYS; |
| 966 | } |
| 967 | |
Glenn Kasten | 068b84e | 2012-09-26 12:34:51 -0700 | [diff] [blame] | 968 | /* Return the set of output devices associated with active streams |
| 969 | * other than out. Assumes out is non-NULL and out->dev is locked. |
| 970 | */ |
| 971 | static audio_devices_t output_devices(struct stream_out *out) |
| 972 | { |
| 973 | struct audio_device *dev = out->dev; |
| 974 | enum output_type type; |
| 975 | audio_devices_t devices = AUDIO_DEVICE_NONE; |
| 976 | |
| 977 | for (type = 0; type < OUTPUT_TOTAL; ++type) { |
| 978 | struct stream_out *other = dev->outputs[type]; |
| 979 | if (other && (other != out) && !other->standby) { |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 980 | // TODO no longer accurate |
Glenn Kasten | 068b84e | 2012-09-26 12:34:51 -0700 | [diff] [blame] | 981 | /* safe to access other stream without a mutex, |
| 982 | * because we hold the dev lock, |
| 983 | * which prevents the other stream from being closed |
| 984 | */ |
| 985 | devices |= other->device; |
| 986 | } |
| 987 | } |
| 988 | |
| 989 | return devices; |
| 990 | } |
| 991 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 992 | /* must be called with hw device outputs list, all out streams, and hw device mutex locked */ |
| 993 | static void do_out_standby(struct stream_out *out) |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 994 | { |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 995 | struct audio_device *adev = out->dev; |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 996 | int i; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 997 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 998 | if (!out->standby) { |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 999 | for (i = 0; i < PCM_TOTAL; i++) { |
| 1000 | if (out->pcm[i]) { |
| 1001 | pcm_close(out->pcm[i]); |
| 1002 | out->pcm[i] = NULL; |
| 1003 | } |
| 1004 | } |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1005 | out->standby = true; |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 1006 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1007 | if (out == adev->outputs[OUTPUT_HDMI]) { |
| 1008 | /* force standby on low latency output stream so that it can reuse HDMI driver if |
| 1009 | * necessary when restarted */ |
| 1010 | force_non_hdmi_out_standby(adev); |
| 1011 | } |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 1012 | |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1013 | if (out->device & AUDIO_DEVICE_OUT_ALL_SCO) |
| 1014 | stop_bt_sco(adev); |
| 1015 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1016 | /* re-calculate the set of active devices from other streams */ |
| 1017 | adev->out_device = output_devices(out); |
Simon Wilson | 79a2e01 | 2012-10-19 14:20:33 -0700 | [diff] [blame] | 1018 | |
| 1019 | /* Skip resetting the mixer if no output device is active */ |
| 1020 | if (adev->out_device) |
| 1021 | select_devices(adev); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1022 | } |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1023 | } |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1024 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1025 | /* lock outputs list, all output streams, and device */ |
| 1026 | static void lock_all_outputs(struct audio_device *adev) |
| 1027 | { |
| 1028 | enum output_type type; |
| 1029 | pthread_mutex_lock(&adev->lock_outputs); |
| 1030 | for (type = 0; type < OUTPUT_TOTAL; ++type) { |
| 1031 | struct stream_out *out = adev->outputs[type]; |
| 1032 | if (out) |
| 1033 | pthread_mutex_lock(&out->lock); |
| 1034 | } |
| 1035 | pthread_mutex_lock(&adev->lock); |
| 1036 | } |
| 1037 | |
| 1038 | /* unlock device, all output streams (except specified stream), and outputs list */ |
| 1039 | static void unlock_all_outputs(struct audio_device *adev, struct stream_out *except) |
| 1040 | { |
| 1041 | /* unlock order is irrelevant, but for cleanliness we unlock in reverse order */ |
| 1042 | pthread_mutex_unlock(&adev->lock); |
| 1043 | enum output_type type = OUTPUT_TOTAL; |
| 1044 | do { |
| 1045 | struct stream_out *out = adev->outputs[--type]; |
| 1046 | if (out && out != except) |
| 1047 | pthread_mutex_unlock(&out->lock); |
| 1048 | } while (type != (enum output_type) 0); |
| 1049 | pthread_mutex_unlock(&adev->lock_outputs); |
Simon Wilson | 459d5bd | 2012-08-21 12:18:07 -0700 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | static int out_standby(struct audio_stream *stream) |
| 1053 | { |
| 1054 | struct stream_out *out = (struct stream_out *)stream; |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1055 | struct audio_device *adev = out->dev; |
Simon Wilson | 459d5bd | 2012-08-21 12:18:07 -0700 | [diff] [blame] | 1056 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1057 | lock_all_outputs(adev); |
Simon Wilson | 459d5bd | 2012-08-21 12:18:07 -0700 | [diff] [blame] | 1058 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1059 | do_out_standby(out); |
Simon Wilson | 459d5bd | 2012-08-21 12:18:07 -0700 | [diff] [blame] | 1060 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1061 | unlock_all_outputs(adev, NULL); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1062 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1063 | return 0; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | static int out_dump(const struct audio_stream *stream, int fd) |
| 1067 | { |
| 1068 | return 0; |
| 1069 | } |
| 1070 | |
| 1071 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 1072 | { |
| 1073 | struct stream_out *out = (struct stream_out *)stream; |
| 1074 | struct audio_device *adev = out->dev; |
| 1075 | struct str_parms *parms; |
| 1076 | char value[32]; |
| 1077 | int ret; |
| 1078 | unsigned int val; |
| 1079 | |
| 1080 | parms = str_parms_create_str(kvpairs); |
| 1081 | |
| 1082 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, |
| 1083 | value, sizeof(value)); |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1084 | lock_all_outputs(adev); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1085 | if (ret >= 0) { |
| 1086 | val = atoi(value); |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 1087 | if ((out->device != val) && (val != 0)) { |
Simon Wilson | 459d5bd | 2012-08-21 12:18:07 -0700 | [diff] [blame] | 1088 | /* Force standby if moving to/from SPDIF or if the output |
| 1089 | * device changes when in SPDIF mode */ |
| 1090 | if (((val & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) ^ |
Eric Laurent | 6a466d7 | 2012-08-28 12:13:38 -0700 | [diff] [blame] | 1091 | (adev->out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) || |
| 1092 | (adev->out_device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) { |
Simon Wilson | 459d5bd | 2012-08-21 12:18:07 -0700 | [diff] [blame] | 1093 | do_out_standby(out); |
| 1094 | } |
| 1095 | |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1096 | /* force output standby to start or stop SCO pcm stream if needed */ |
Simon Wilson | a282d2f | 2012-09-12 16:14:24 -0700 | [diff] [blame] | 1097 | if ((val & AUDIO_DEVICE_OUT_ALL_SCO) ^ |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1098 | (out->device & AUDIO_DEVICE_OUT_ALL_SCO)) { |
| 1099 | do_out_standby(out); |
Simon Wilson | a282d2f | 2012-09-12 16:14:24 -0700 | [diff] [blame] | 1100 | } |
| 1101 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1102 | if (!out->standby && (out == adev->outputs[OUTPUT_HDMI] || |
| 1103 | !adev->outputs[OUTPUT_HDMI] || |
| 1104 | adev->outputs[OUTPUT_HDMI]->standby)) { |
| 1105 | adev->out_device = output_devices(out) | val; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 1106 | select_devices(adev); |
| 1107 | } |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1108 | out->device = val; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1109 | } |
| 1110 | } |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1111 | unlock_all_outputs(adev, NULL); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1112 | |
| 1113 | str_parms_destroy(parms); |
| 1114 | return ret; |
| 1115 | } |
| 1116 | |
| 1117 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 1118 | { |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1119 | struct stream_out *out = (struct stream_out *)stream; |
| 1120 | struct str_parms *query = str_parms_create_str(keys); |
| 1121 | char *str; |
| 1122 | char value[256]; |
| 1123 | struct str_parms *reply = str_parms_create(); |
| 1124 | size_t i, j; |
| 1125 | int ret; |
| 1126 | bool first = true; |
| 1127 | |
| 1128 | ret = str_parms_get_str(query, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value, sizeof(value)); |
| 1129 | if (ret >= 0) { |
| 1130 | value[0] = '\0'; |
| 1131 | i = 0; |
| 1132 | /* the last entry in supported_channel_masks[] is always 0 */ |
| 1133 | while (out->supported_channel_masks[i] != 0) { |
| 1134 | for (j = 0; j < ARRAY_SIZE(out_channels_name_to_enum_table); j++) { |
| 1135 | if (out_channels_name_to_enum_table[j].value == out->supported_channel_masks[i]) { |
| 1136 | if (!first) { |
| 1137 | strcat(value, "|"); |
| 1138 | } |
| 1139 | strcat(value, out_channels_name_to_enum_table[j].name); |
| 1140 | first = false; |
| 1141 | break; |
| 1142 | } |
| 1143 | } |
| 1144 | i++; |
| 1145 | } |
| 1146 | str_parms_add_str(reply, AUDIO_PARAMETER_STREAM_SUP_CHANNELS, value); |
| 1147 | str = str_parms_to_str(reply); |
| 1148 | } else { |
| 1149 | str = strdup(keys); |
| 1150 | } |
| 1151 | |
| 1152 | str_parms_destroy(query); |
| 1153 | str_parms_destroy(reply); |
| 1154 | return str; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 1158 | { |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1159 | struct stream_out *out = (struct stream_out *)stream; |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 1160 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1161 | return (out->config.period_size * out->config.period_count * 1000) / |
| 1162 | out->config.rate; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | static int out_set_volume(struct audio_stream_out *stream, float left, |
| 1166 | float right) |
| 1167 | { |
Eric Laurent | 3948fda | 2013-04-04 09:24:22 -0700 | [diff] [blame] | 1168 | struct stream_out *out = (struct stream_out *)stream; |
| 1169 | struct audio_device *adev = out->dev; |
| 1170 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1171 | /* The mutex lock is not needed, because the client |
| 1172 | * is not allowed to close the stream concurrently with this API |
| 1173 | * pthread_mutex_lock(&adev->lock_outputs); |
| 1174 | */ |
| 1175 | bool is_HDMI = out == adev->outputs[OUTPUT_HDMI]; |
| 1176 | /* pthread_mutex_unlock(&adev->lock_outputs); */ |
| 1177 | if (is_HDMI) { |
Eric Laurent | 3948fda | 2013-04-04 09:24:22 -0700 | [diff] [blame] | 1178 | /* only take left channel into account: the API is for stereo anyway */ |
| 1179 | out->muted = (left == 0.0f); |
| 1180 | return 0; |
| 1181 | } |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1182 | return -ENOSYS; |
| 1183 | } |
| 1184 | |
| 1185 | static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, |
| 1186 | size_t bytes) |
| 1187 | { |
Glenn Kasten | f2d9802 | 2013-08-30 16:20:49 -0700 | [diff] [blame] | 1188 | int ret = 0; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1189 | struct stream_out *out = (struct stream_out *)stream; |
| 1190 | struct audio_device *adev = out->dev; |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 1191 | int i; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1192 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1193 | /* FIXME This comment is no longer correct |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1194 | * acquiring hw device mutex systematically is useful if a low |
| 1195 | * priority thread is waiting on the output stream mutex - e.g. |
| 1196 | * executing out_set_parameters() while holding the hw device |
| 1197 | * mutex |
| 1198 | */ |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1199 | pthread_mutex_lock(&out->lock); |
| 1200 | if (out->standby) { |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1201 | pthread_mutex_unlock(&out->lock); |
| 1202 | lock_all_outputs(adev); |
| 1203 | if (!out->standby) { |
| 1204 | unlock_all_outputs(adev, out); |
| 1205 | goto false_alarm; |
| 1206 | } |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1207 | ret = start_output_stream(out); |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1208 | if (ret < 0) { |
| 1209 | unlock_all_outputs(adev, NULL); |
| 1210 | goto final_exit; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1211 | } |
| 1212 | out->standby = false; |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1213 | unlock_all_outputs(adev, out); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1214 | } |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1215 | false_alarm: |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1216 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1217 | if (out->disabled) { |
| 1218 | ret = -EPIPE; |
| 1219 | goto exit; |
| 1220 | } |
| 1221 | |
Eric Laurent | 3948fda | 2013-04-04 09:24:22 -0700 | [diff] [blame] | 1222 | if (out->muted) |
| 1223 | memset((void *)buffer, 0, bytes); |
| 1224 | |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 1225 | /* Write to all active PCMs */ |
| 1226 | for (i = 0; i < PCM_TOTAL; i++) |
Glenn Kasten | 21169cb | 2013-08-20 15:36:18 -0700 | [diff] [blame] | 1227 | if (out->pcm[i]) { |
| 1228 | ret = pcm_write(out->pcm[i], (void *)buffer, bytes); |
| 1229 | if (ret != 0) |
| 1230 | break; |
| 1231 | } |
| 1232 | if (ret == 0) |
| 1233 | out->written += bytes / (out->config.channels * sizeof(short)); |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1234 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1235 | exit: |
| 1236 | pthread_mutex_unlock(&out->lock); |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1237 | final_exit: |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1238 | |
| 1239 | if (ret != 0) { |
Eric Laurent | 84c3373 | 2014-07-03 16:35:46 -0700 | [diff] [blame] | 1240 | usleep(bytes * 1000000 / audio_stream_out_frame_size(stream) / |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1241 | out_get_sample_rate(&stream->common)); |
| 1242 | } |
| 1243 | |
| 1244 | return bytes; |
| 1245 | } |
| 1246 | |
| 1247 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 1248 | uint32_t *dsp_frames) |
| 1249 | { |
| 1250 | return -EINVAL; |
| 1251 | } |
| 1252 | |
| 1253 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1254 | { |
| 1255 | return 0; |
| 1256 | } |
| 1257 | |
| 1258 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 1259 | { |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
| 1263 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream, |
| 1264 | int64_t *timestamp) |
| 1265 | { |
| 1266 | return -EINVAL; |
| 1267 | } |
| 1268 | |
Glenn Kasten | 21169cb | 2013-08-20 15:36:18 -0700 | [diff] [blame] | 1269 | static int out_get_presentation_position(const struct audio_stream_out *stream, |
| 1270 | uint64_t *frames, struct timespec *timestamp) |
| 1271 | { |
| 1272 | struct stream_out *out = (struct stream_out *)stream; |
| 1273 | int ret = -1; |
| 1274 | |
| 1275 | pthread_mutex_lock(&out->lock); |
| 1276 | |
| 1277 | int i; |
| 1278 | // There is a question how to implement this correctly when there is more than one PCM stream. |
| 1279 | // We are just interested in the frames pending for playback in the kernel buffer here, |
| 1280 | // not the total played since start. The current behavior should be safe because the |
| 1281 | // cases where both cards are active are marginal. |
| 1282 | for (i = 0; i < PCM_TOTAL; i++) |
| 1283 | if (out->pcm[i]) { |
| 1284 | size_t avail; |
| 1285 | if (pcm_get_htimestamp(out->pcm[i], &avail, timestamp) == 0) { |
| 1286 | size_t kernel_buffer_size = out->config.period_size * out->config.period_count; |
| 1287 | // FIXME This calculation is incorrect if there is buffering after app processor |
| 1288 | int64_t signed_frames = out->written - kernel_buffer_size + avail; |
| 1289 | // It would be unusual for this value to be negative, but check just in case ... |
| 1290 | if (signed_frames >= 0) { |
| 1291 | *frames = signed_frames; |
| 1292 | ret = 0; |
| 1293 | } |
| 1294 | break; |
| 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | pthread_mutex_unlock(&out->lock); |
| 1299 | |
| 1300 | return ret; |
| 1301 | } |
| 1302 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1303 | /** audio_stream_in implementation **/ |
| 1304 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 1305 | { |
| 1306 | struct stream_in *in = (struct stream_in *)stream; |
| 1307 | |
| 1308 | return in->requested_rate; |
| 1309 | } |
| 1310 | |
| 1311 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 1312 | { |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 1316 | static audio_channel_mask_t in_get_channels(const struct audio_stream *stream) |
| 1317 | { |
Eric Laurent | b52afc8 | 2013-01-18 15:12:57 -0800 | [diff] [blame] | 1318 | struct stream_in *in = (struct stream_in *)stream; |
| 1319 | |
| 1320 | return in->channel_mask; |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1324 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 1325 | { |
| 1326 | struct stream_in *in = (struct stream_in *)stream; |
| 1327 | |
| 1328 | return get_input_buffer_size(in->requested_rate, |
| 1329 | AUDIO_FORMAT_PCM_16_BIT, |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 1330 | audio_channel_count_from_in_mask(in_get_channels(stream)), |
| 1331 | (in->flags & AUDIO_INPUT_FLAG_FAST) != 0); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 1335 | { |
| 1336 | return AUDIO_FORMAT_PCM_16_BIT; |
| 1337 | } |
| 1338 | |
| 1339 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 1340 | { |
| 1341 | return -ENOSYS; |
| 1342 | } |
| 1343 | |
Glenn Kasten | 6408391 | 2014-09-05 10:33:42 -0700 | [diff] [blame] | 1344 | /* must be called with in stream and hw device mutex locked */ |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1345 | static void do_in_standby(struct stream_in *in) |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1346 | { |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1347 | struct audio_device *adev = in->dev; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1348 | |
| 1349 | if (!in->standby) { |
| 1350 | pcm_close(in->pcm); |
| 1351 | in->pcm = NULL; |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1352 | |
| 1353 | if (in->device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) |
| 1354 | stop_bt_sco(adev); |
| 1355 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 1356 | in->dev->input_source = AUDIO_SOURCE_DEFAULT; |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 1357 | in->dev->in_device = AUDIO_DEVICE_NONE; |
Jean-Michel Trivi | 3f467de | 2013-06-06 15:26:01 -0700 | [diff] [blame] | 1358 | in->dev->in_channel_mask = 0; |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1359 | select_devices(adev); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1360 | in->standby = true; |
Eric Laurent | a989ebf | 2012-10-15 15:14:44 -0700 | [diff] [blame] | 1361 | |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1362 | if (get_bubblelevel(adev)) |
| 1363 | in->dev->bubble_level->stop_polling(adev->bubble_level); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1366 | eS305_SetActiveIoHandle(ES305_IO_HANDLE_NONE); |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | static int in_standby(struct audio_stream *stream) |
| 1370 | { |
| 1371 | struct stream_in *in = (struct stream_in *)stream; |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1372 | |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1373 | pthread_mutex_lock(&in->lock); |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1374 | pthread_mutex_lock(&in->dev->lock); |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1375 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1376 | do_in_standby(in); |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1377 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1378 | pthread_mutex_unlock(&in->dev->lock); |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1379 | pthread_mutex_unlock(&in->lock); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1380 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1381 | return 0; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | static int in_dump(const struct audio_stream *stream, int fd) |
| 1385 | { |
| 1386 | return 0; |
| 1387 | } |
| 1388 | |
| 1389 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 1390 | { |
| 1391 | struct stream_in *in = (struct stream_in *)stream; |
| 1392 | struct audio_device *adev = in->dev; |
| 1393 | struct str_parms *parms; |
| 1394 | char value[32]; |
| 1395 | int ret; |
| 1396 | unsigned int val; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 1397 | bool apply_now = false; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1398 | |
| 1399 | parms = str_parms_create_str(kvpairs); |
| 1400 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 1401 | pthread_mutex_lock(&in->lock); |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1402 | pthread_mutex_lock(&adev->lock); |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 1403 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, |
| 1404 | value, sizeof(value)); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1405 | if (ret >= 0) { |
| 1406 | val = atoi(value); |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 1407 | /* no audio source uses val == 0 */ |
| 1408 | if ((in->input_source != val) && (val != 0)) { |
| 1409 | in->input_source = val; |
| 1410 | apply_now = !in->standby; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1411 | } |
| 1412 | } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 1413 | |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 1414 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, |
| 1415 | value, sizeof(value)); |
| 1416 | if (ret >= 0) { |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1417 | /* strip AUDIO_DEVICE_BIT_IN to allow bitwise comparisons */ |
| 1418 | val = atoi(value) & ~AUDIO_DEVICE_BIT_IN; |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 1419 | /* no audio device uses val == 0 */ |
| 1420 | if ((in->device != val) && (val != 0)) { |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1421 | /* force output standby to start or stop SCO pcm stream if needed */ |
| 1422 | if ((val & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) ^ |
| 1423 | (in->device & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) { |
| 1424 | do_in_standby(in); |
| 1425 | } |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 1426 | in->device = val; |
| 1427 | apply_now = !in->standby; |
| 1428 | } |
| 1429 | } |
| 1430 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 1431 | if (apply_now) { |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 1432 | adev->input_source = in->input_source; |
Eric Laurent | 42531fa | 2012-10-03 09:06:14 -0700 | [diff] [blame] | 1433 | adev->in_device = in->device; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 1434 | select_devices(adev); |
| 1435 | } |
| 1436 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1437 | pthread_mutex_unlock(&adev->lock); |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1438 | pthread_mutex_unlock(&in->lock); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1439 | |
| 1440 | str_parms_destroy(parms); |
| 1441 | return ret; |
| 1442 | } |
| 1443 | |
| 1444 | static char * in_get_parameters(const struct audio_stream *stream, |
| 1445 | const char *keys) |
| 1446 | { |
| 1447 | return strdup(""); |
| 1448 | } |
| 1449 | |
| 1450 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 1451 | { |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
Eric Laurent | 1a0c0a7 | 2012-08-24 11:29:04 -0700 | [diff] [blame] | 1455 | static void in_apply_ramp(struct stream_in *in, int16_t *buffer, size_t frames) |
| 1456 | { |
| 1457 | size_t i; |
| 1458 | uint16_t vol = in->ramp_vol; |
| 1459 | uint16_t step = in->ramp_step; |
| 1460 | |
| 1461 | frames = (frames < in->ramp_frames) ? frames : in->ramp_frames; |
| 1462 | |
Eric Laurent | b52afc8 | 2013-01-18 15:12:57 -0800 | [diff] [blame] | 1463 | if (in->channel_mask == AUDIO_CHANNEL_IN_MONO) |
| 1464 | for (i = 0; i < frames; i++) |
| 1465 | { |
| 1466 | buffer[i] = (int16_t)((buffer[i] * vol) >> 16); |
| 1467 | vol += step; |
| 1468 | } |
| 1469 | else |
| 1470 | for (i = 0; i < frames; i++) |
| 1471 | { |
| 1472 | buffer[2*i] = (int16_t)((buffer[2*i] * vol) >> 16); |
| 1473 | buffer[2*i + 1] = (int16_t)((buffer[2*i + 1] * vol) >> 16); |
| 1474 | vol += step; |
| 1475 | } |
| 1476 | |
Eric Laurent | 1a0c0a7 | 2012-08-24 11:29:04 -0700 | [diff] [blame] | 1477 | |
| 1478 | in->ramp_vol = vol; |
| 1479 | in->ramp_frames -= frames; |
| 1480 | } |
| 1481 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1482 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, |
| 1483 | size_t bytes) |
| 1484 | { |
| 1485 | int ret = 0; |
| 1486 | struct stream_in *in = (struct stream_in *)stream; |
| 1487 | struct audio_device *adev = in->dev; |
Eric Laurent | 84c3373 | 2014-07-03 16:35:46 -0700 | [diff] [blame] | 1488 | size_t frames_rq = bytes / audio_stream_in_frame_size(stream); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1489 | |
| 1490 | /* |
| 1491 | * acquiring hw device mutex systematically is useful if a low |
| 1492 | * priority thread is waiting on the input stream mutex - e.g. |
| 1493 | * executing in_set_parameters() while holding the hw device |
| 1494 | * mutex |
| 1495 | */ |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1496 | pthread_mutex_lock(&in->lock); |
| 1497 | if (in->standby) { |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1498 | pthread_mutex_lock(&adev->lock); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1499 | ret = start_input_stream(in); |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1500 | pthread_mutex_unlock(&adev->lock); |
| 1501 | if (ret < 0) |
| 1502 | goto exit; |
| 1503 | in->standby = false; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1504 | } |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1505 | |
| 1506 | /*if (in->num_preprocessors != 0) |
| 1507 | ret = process_frames(in, buffer, frames_rq); |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 1508 | else */ |
| 1509 | ret = read_frames(in, buffer, frames_rq); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1510 | |
| 1511 | if (ret > 0) |
| 1512 | ret = 0; |
| 1513 | |
Eric Laurent | 1a0c0a7 | 2012-08-24 11:29:04 -0700 | [diff] [blame] | 1514 | if (in->ramp_frames > 0) |
| 1515 | in_apply_ramp(in, buffer, frames_rq); |
| 1516 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1517 | /* |
| 1518 | * Instead of writing zeroes here, we could trust the hardware |
| 1519 | * to always provide zeroes when muted. |
| 1520 | */ |
| 1521 | if (ret == 0 && adev->mic_mute) |
| 1522 | memset(buffer, 0, bytes); |
| 1523 | |
| 1524 | exit: |
| 1525 | if (ret < 0) |
Eric Laurent | 84c3373 | 2014-07-03 16:35:46 -0700 | [diff] [blame] | 1526 | usleep(bytes * 1000000 / audio_stream_in_frame_size(stream) / |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1527 | in_get_sample_rate(&stream->common)); |
| 1528 | |
| 1529 | pthread_mutex_unlock(&in->lock); |
| 1530 | return bytes; |
| 1531 | } |
| 1532 | |
| 1533 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 1534 | { |
| 1535 | return 0; |
| 1536 | } |
| 1537 | |
| 1538 | static int in_add_audio_effect(const struct audio_stream *stream, |
| 1539 | effect_handle_t effect) |
| 1540 | { |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1541 | struct stream_in *in = (struct stream_in *)stream; |
| 1542 | effect_descriptor_t descr; |
| 1543 | if ((*effect)->get_descriptor(effect, &descr) == 0) { |
| 1544 | |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1545 | pthread_mutex_lock(&in->lock); |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1546 | pthread_mutex_lock(&in->dev->lock); |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1547 | |
| 1548 | eS305_AddEffect(&descr, in->io_handle); |
| 1549 | |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1550 | pthread_mutex_unlock(&in->dev->lock); |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1551 | pthread_mutex_unlock(&in->lock); |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1552 | } |
| 1553 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1554 | return 0; |
| 1555 | } |
| 1556 | |
| 1557 | static int in_remove_audio_effect(const struct audio_stream *stream, |
| 1558 | effect_handle_t effect) |
| 1559 | { |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1560 | struct stream_in *in = (struct stream_in *)stream; |
| 1561 | effect_descriptor_t descr; |
| 1562 | if ((*effect)->get_descriptor(effect, &descr) == 0) { |
| 1563 | |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1564 | pthread_mutex_lock(&in->lock); |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1565 | pthread_mutex_lock(&in->dev->lock); |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1566 | |
| 1567 | eS305_RemoveEffect(&descr, in->io_handle); |
| 1568 | |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1569 | pthread_mutex_unlock(&in->dev->lock); |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1570 | pthread_mutex_unlock(&in->lock); |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1571 | } |
| 1572 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1573 | return 0; |
| 1574 | } |
| 1575 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1576 | static int adev_open_output_stream(struct audio_hw_device *dev, |
| 1577 | audio_io_handle_t handle, |
| 1578 | audio_devices_t devices, |
| 1579 | audio_output_flags_t flags, |
| 1580 | struct audio_config *config, |
Eric Laurent | 2f4b809 | 2014-07-27 17:23:22 -0700 | [diff] [blame] | 1581 | struct audio_stream_out **stream_out, |
| 1582 | const char *address __unused) |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1583 | { |
| 1584 | struct audio_device *adev = (struct audio_device *)dev; |
| 1585 | struct stream_out *out; |
| 1586 | int ret; |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 1587 | enum output_type type; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1588 | |
| 1589 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
| 1590 | if (!out) |
| 1591 | return -ENOMEM; |
| 1592 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1593 | out->supported_channel_masks[0] = AUDIO_CHANNEL_OUT_STEREO; |
| 1594 | out->channel_mask = AUDIO_CHANNEL_OUT_STEREO; |
| 1595 | if (devices == AUDIO_DEVICE_NONE) |
| 1596 | devices = AUDIO_DEVICE_OUT_SPEAKER; |
| 1597 | out->device = devices; |
| 1598 | |
| 1599 | if (flags & AUDIO_OUTPUT_FLAG_DIRECT && |
| 1600 | devices == AUDIO_DEVICE_OUT_AUX_DIGITAL) { |
| 1601 | pthread_mutex_lock(&adev->lock); |
| 1602 | ret = read_hdmi_channel_masks(adev, out); |
| 1603 | pthread_mutex_unlock(&adev->lock); |
| 1604 | if (ret != 0) |
| 1605 | goto err_open; |
| 1606 | if (config->sample_rate == 0) |
| 1607 | config->sample_rate = HDMI_MULTI_DEFAULT_SAMPLING_RATE; |
| 1608 | if (config->channel_mask == 0) |
| 1609 | config->channel_mask = AUDIO_CHANNEL_OUT_5POINT1; |
| 1610 | out->channel_mask = config->channel_mask; |
| 1611 | out->config = pcm_config_hdmi_multi; |
| 1612 | out->config.rate = config->sample_rate; |
Eric Laurent | 1e7184b | 2014-07-01 20:29:10 -0700 | [diff] [blame] | 1613 | out->config.channels = audio_channel_count_from_out_mask(config->channel_mask); |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1614 | out->pcm_device = PCM_DEVICE; |
| 1615 | type = OUTPUT_HDMI; |
| 1616 | } else if (flags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) { |
| 1617 | out->config = pcm_config_deep; |
| 1618 | out->pcm_device = PCM_DEVICE_DEEP; |
| 1619 | type = OUTPUT_DEEP_BUF; |
| 1620 | } else { |
| 1621 | out->config = pcm_config; |
| 1622 | out->pcm_device = PCM_DEVICE; |
| 1623 | type = OUTPUT_LOW_LATENCY; |
| 1624 | } |
| 1625 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1626 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 1627 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 1628 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 1629 | out->stream.common.get_channels = out_get_channels; |
| 1630 | out->stream.common.get_format = out_get_format; |
| 1631 | out->stream.common.set_format = out_set_format; |
| 1632 | out->stream.common.standby = out_standby; |
| 1633 | out->stream.common.dump = out_dump; |
| 1634 | out->stream.common.set_parameters = out_set_parameters; |
| 1635 | out->stream.common.get_parameters = out_get_parameters; |
| 1636 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 1637 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 1638 | out->stream.get_latency = out_get_latency; |
| 1639 | out->stream.set_volume = out_set_volume; |
| 1640 | out->stream.write = out_write; |
| 1641 | out->stream.get_render_position = out_get_render_position; |
| 1642 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
Glenn Kasten | 21169cb | 2013-08-20 15:36:18 -0700 | [diff] [blame] | 1643 | out->stream.get_presentation_position = out_get_presentation_position; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1644 | |
| 1645 | out->dev = adev; |
| 1646 | |
| 1647 | config->format = out_get_format(&out->stream.common); |
| 1648 | config->channel_mask = out_get_channels(&out->stream.common); |
| 1649 | config->sample_rate = out_get_sample_rate(&out->stream.common); |
| 1650 | |
| 1651 | out->standby = true; |
Eric Laurent | 3948fda | 2013-04-04 09:24:22 -0700 | [diff] [blame] | 1652 | /* out->muted = false; by calloc() */ |
Glenn Kasten | 21169cb | 2013-08-20 15:36:18 -0700 | [diff] [blame] | 1653 | /* out->written = 0; by calloc() */ |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1654 | |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1655 | pthread_mutex_lock(&adev->lock_outputs); |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 1656 | if (adev->outputs[type]) { |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1657 | pthread_mutex_unlock(&adev->lock_outputs); |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 1658 | ret = -EBUSY; |
| 1659 | goto err_open; |
| 1660 | } |
| 1661 | adev->outputs[type] = out; |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1662 | pthread_mutex_unlock(&adev->lock_outputs); |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 1663 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1664 | *stream_out = &out->stream; |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1665 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1666 | return 0; |
| 1667 | |
| 1668 | err_open: |
| 1669 | free(out); |
| 1670 | *stream_out = NULL; |
| 1671 | return ret; |
| 1672 | } |
| 1673 | |
| 1674 | static void adev_close_output_stream(struct audio_hw_device *dev, |
| 1675 | struct audio_stream_out *stream) |
| 1676 | { |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 1677 | struct audio_device *adev; |
| 1678 | enum output_type type; |
| 1679 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1680 | out_standby(&stream->common); |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 1681 | adev = (struct audio_device *)dev; |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1682 | pthread_mutex_lock(&adev->lock_outputs); |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 1683 | for (type = 0; type < OUTPUT_TOTAL; ++type) { |
| 1684 | if (adev->outputs[type] == (struct stream_out *) stream) { |
| 1685 | adev->outputs[type] = NULL; |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1686 | break; |
Glenn Kasten | e0aa8f3 | 2012-08-17 09:26:58 -0700 | [diff] [blame] | 1687 | } |
| 1688 | } |
Glenn Kasten | 22f4563 | 2014-09-05 10:34:40 -0700 | [diff] [blame] | 1689 | pthread_mutex_unlock(&adev->lock_outputs); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1690 | free(stream); |
| 1691 | } |
| 1692 | |
| 1693 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 1694 | { |
Eric Laurent | a989ebf | 2012-10-15 15:14:44 -0700 | [diff] [blame] | 1695 | return 0; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
| 1698 | static char * adev_get_parameters(const struct audio_hw_device *dev, |
| 1699 | const char *keys) |
| 1700 | { |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 1701 | struct audio_device *adev = (struct audio_device *)dev; |
| 1702 | struct str_parms *parms = str_parms_create_str(keys); |
| 1703 | char value[32]; |
| 1704 | int ret = str_parms_get_str(parms, "ec_supported", value, sizeof(value)); |
| 1705 | char *str; |
| 1706 | |
| 1707 | str_parms_destroy(parms); |
| 1708 | if (ret >= 0) { |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1709 | parms = str_parms_create_str("ec_supported=yes"); |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 1710 | str = str_parms_to_str(parms); |
| 1711 | str_parms_destroy(parms); |
| 1712 | return str; |
| 1713 | } |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1714 | return strdup(""); |
| 1715 | } |
| 1716 | |
| 1717 | static int adev_init_check(const struct audio_hw_device *dev) |
| 1718 | { |
| 1719 | return 0; |
| 1720 | } |
| 1721 | |
| 1722 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 1723 | { |
| 1724 | return -ENOSYS; |
| 1725 | } |
| 1726 | |
| 1727 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 1728 | { |
| 1729 | return -ENOSYS; |
| 1730 | } |
| 1731 | |
| 1732 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 1733 | { |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
| 1737 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 1738 | { |
| 1739 | struct audio_device *adev = (struct audio_device *)dev; |
| 1740 | |
| 1741 | adev->mic_mute = state; |
| 1742 | |
| 1743 | return 0; |
| 1744 | } |
| 1745 | |
| 1746 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 1747 | { |
| 1748 | struct audio_device *adev = (struct audio_device *)dev; |
| 1749 | |
| 1750 | *state = adev->mic_mute; |
| 1751 | |
| 1752 | return 0; |
| 1753 | } |
| 1754 | |
| 1755 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, |
| 1756 | const struct audio_config *config) |
| 1757 | { |
| 1758 | |
| 1759 | return get_input_buffer_size(config->sample_rate, config->format, |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 1760 | audio_channel_count_from_in_mask(config->channel_mask), |
| 1761 | false /* is_low_latency: since we don't know, be conservative */); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | static int adev_open_input_stream(struct audio_hw_device *dev, |
| 1765 | audio_io_handle_t handle, |
| 1766 | audio_devices_t devices, |
| 1767 | struct audio_config *config, |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 1768 | struct audio_stream_in **stream_in, |
Eric Laurent | 2f4b809 | 2014-07-27 17:23:22 -0700 | [diff] [blame] | 1769 | audio_input_flags_t flags, |
| 1770 | const char *address __unused, |
| 1771 | audio_source_t source __unused) |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1772 | { |
| 1773 | struct audio_device *adev = (struct audio_device *)dev; |
| 1774 | struct stream_in *in; |
| 1775 | int ret; |
| 1776 | |
| 1777 | *stream_in = NULL; |
| 1778 | |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 1779 | /* Respond with a request for mono if a different format is given. */ |
Eric Laurent | b52afc8 | 2013-01-18 15:12:57 -0800 | [diff] [blame] | 1780 | if (config->channel_mask != AUDIO_CHANNEL_IN_MONO && |
| 1781 | config->channel_mask != AUDIO_CHANNEL_IN_FRONT_BACK) { |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 1782 | config->channel_mask = AUDIO_CHANNEL_IN_MONO; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1783 | return -EINVAL; |
| 1784 | } |
| 1785 | |
| 1786 | in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); |
| 1787 | if (!in) |
| 1788 | return -ENOMEM; |
| 1789 | |
| 1790 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 1791 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 1792 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 1793 | in->stream.common.get_channels = in_get_channels; |
| 1794 | in->stream.common.get_format = in_get_format; |
| 1795 | in->stream.common.set_format = in_set_format; |
| 1796 | in->stream.common.standby = in_standby; |
| 1797 | in->stream.common.dump = in_dump; |
| 1798 | in->stream.common.set_parameters = in_set_parameters; |
| 1799 | in->stream.common.get_parameters = in_get_parameters; |
| 1800 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 1801 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 1802 | in->stream.set_gain = in_set_gain; |
| 1803 | in->stream.read = in_read; |
| 1804 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| 1805 | |
| 1806 | in->dev = adev; |
| 1807 | in->standby = true; |
| 1808 | in->requested_rate = config->sample_rate; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 1809 | in->input_source = AUDIO_SOURCE_DEFAULT; |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1810 | /* strip AUDIO_DEVICE_BIT_IN to allow bitwise comparisons */ |
| 1811 | in->device = devices & ~AUDIO_DEVICE_BIT_IN; |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1812 | in->io_handle = handle; |
Eric Laurent | b52afc8 | 2013-01-18 15:12:57 -0800 | [diff] [blame] | 1813 | in->channel_mask = config->channel_mask; |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 1814 | in->flags = flags; |
| 1815 | struct pcm_config *pcm_config = flags & AUDIO_INPUT_FLAG_FAST ? |
| 1816 | &pcm_config_in_low_latency : &pcm_config_in; |
| 1817 | in->config = pcm_config; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1818 | |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 1819 | in->buffer = malloc(pcm_config->period_size * pcm_config->channels |
Eric Laurent | 84c3373 | 2014-07-03 16:35:46 -0700 | [diff] [blame] | 1820 | * audio_stream_in_frame_size(&in->stream)); |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 1821 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1822 | if (!in->buffer) { |
| 1823 | ret = -ENOMEM; |
| 1824 | goto err_malloc; |
| 1825 | } |
| 1826 | |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 1827 | if (in->requested_rate != pcm_config->rate) { |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1828 | in->buf_provider.get_next_buffer = get_next_buffer; |
| 1829 | in->buf_provider.release_buffer = release_buffer; |
| 1830 | |
Glenn Kasten | a83d9a1 | 2014-07-15 10:28:31 -0700 | [diff] [blame] | 1831 | ret = create_resampler(pcm_config->rate, |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1832 | in->requested_rate, |
Eric Laurent | 1e7184b | 2014-07-01 20:29:10 -0700 | [diff] [blame] | 1833 | audio_channel_count_from_in_mask(in->channel_mask), |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1834 | RESAMPLER_QUALITY_DEFAULT, |
| 1835 | &in->buf_provider, |
| 1836 | &in->resampler); |
| 1837 | if (ret != 0) { |
| 1838 | ret = -EINVAL; |
| 1839 | goto err_resampler; |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | *stream_in = &in->stream; |
| 1844 | return 0; |
| 1845 | |
| 1846 | err_resampler: |
| 1847 | free(in->buffer); |
| 1848 | err_malloc: |
| 1849 | free(in); |
| 1850 | return ret; |
| 1851 | } |
| 1852 | |
| 1853 | static void adev_close_input_stream(struct audio_hw_device *dev, |
| 1854 | struct audio_stream_in *stream) |
| 1855 | { |
| 1856 | struct stream_in *in = (struct stream_in *)stream; |
| 1857 | |
| 1858 | in_standby(&stream->common); |
| 1859 | if (in->resampler) { |
| 1860 | release_resampler(in->resampler); |
| 1861 | in->resampler = NULL; |
| 1862 | } |
| 1863 | free(in->buffer); |
| 1864 | free(stream); |
| 1865 | } |
| 1866 | |
| 1867 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 1868 | { |
| 1869 | return 0; |
| 1870 | } |
| 1871 | |
| 1872 | static int adev_close(hw_device_t *device) |
| 1873 | { |
| 1874 | struct audio_device *adev = (struct audio_device *)device; |
| 1875 | |
| 1876 | audio_route_free(adev->ar); |
| 1877 | |
Jean-Michel Trivi | e04f7c9 | 2012-09-30 16:01:41 -0700 | [diff] [blame] | 1878 | eS305_Release(); |
| 1879 | |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1880 | if (adev->hdmi_drv_fd >= 0) |
| 1881 | close(adev->hdmi_drv_fd); |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 1882 | |
Eric Laurent | 429f24d | 2012-11-02 14:37:38 -0700 | [diff] [blame] | 1883 | if (adev->bubble_level) |
| 1884 | bubble_level_release(adev->bubble_level); |
Eric Laurent | a989ebf | 2012-10-15 15:14:44 -0700 | [diff] [blame] | 1885 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1886 | free(device); |
| 1887 | return 0; |
| 1888 | } |
| 1889 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1890 | static int adev_open(const hw_module_t* module, const char* name, |
| 1891 | hw_device_t** device) |
| 1892 | { |
| 1893 | struct audio_device *adev; |
| 1894 | int ret; |
| 1895 | |
| 1896 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 1897 | return -EINVAL; |
| 1898 | |
| 1899 | adev = calloc(1, sizeof(struct audio_device)); |
| 1900 | if (!adev) |
| 1901 | return -ENOMEM; |
| 1902 | |
| 1903 | adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; |
Eric Laurent | 6a466d7 | 2012-08-28 12:13:38 -0700 | [diff] [blame] | 1904 | adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1905 | adev->hw_device.common.module = (struct hw_module_t *) module; |
| 1906 | adev->hw_device.common.close = adev_close; |
| 1907 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1908 | adev->hw_device.init_check = adev_init_check; |
| 1909 | adev->hw_device.set_voice_volume = adev_set_voice_volume; |
| 1910 | adev->hw_device.set_master_volume = adev_set_master_volume; |
| 1911 | adev->hw_device.set_mode = adev_set_mode; |
| 1912 | adev->hw_device.set_mic_mute = adev_set_mic_mute; |
| 1913 | adev->hw_device.get_mic_mute = adev_get_mic_mute; |
| 1914 | adev->hw_device.set_parameters = adev_set_parameters; |
| 1915 | adev->hw_device.get_parameters = adev_get_parameters; |
| 1916 | adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size; |
| 1917 | adev->hw_device.open_output_stream = adev_open_output_stream; |
| 1918 | adev->hw_device.close_output_stream = adev_close_output_stream; |
| 1919 | adev->hw_device.open_input_stream = adev_open_input_stream; |
| 1920 | adev->hw_device.close_input_stream = adev_close_input_stream; |
| 1921 | adev->hw_device.dump = adev_dump; |
| 1922 | |
Simon Wilson | 759c6f0 | 2013-01-15 16:38:56 -0800 | [diff] [blame] | 1923 | adev->ar = audio_route_init(MIXER_CARD, NULL); |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 1924 | adev->input_source = AUDIO_SOURCE_DEFAULT; |
| 1925 | /* adev->cur_route_id initial value is 0 and such that first device |
| 1926 | * selection is always applied by select_devices() */ |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1927 | |
Eric Laurent | 688880c | 2012-09-07 16:46:00 -0700 | [diff] [blame] | 1928 | adev->es305_preset = ES305_PRESET_INIT; |
Eric Laurent | a989ebf | 2012-10-15 15:14:44 -0700 | [diff] [blame] | 1929 | adev->es305_new_mode = ES305_MODE_LEVEL; |
| 1930 | adev->es305_mode = ES305_MODE_LEVEL; |
Eric Laurent | b2c0b4f | 2012-09-21 11:42:48 -0700 | [diff] [blame] | 1931 | adev->hdmi_drv_fd = -1; |
Eric Laurent | 4f1aece | 2013-04-19 17:10:25 -0700 | [diff] [blame] | 1932 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1933 | *device = &adev->hw_device.common; |
| 1934 | |
Glenn Kasten | 6e11d0a | 2014-05-12 14:41:05 -0700 | [diff] [blame] | 1935 | char value[PROPERTY_VALUE_MAX]; |
| 1936 | if (property_get("audio_hal.period_size", value, NULL) > 0) { |
| 1937 | pcm_config.period_size = atoi(value); |
| 1938 | pcm_config_in.period_size = pcm_config.period_size; |
| 1939 | } |
| 1940 | if (property_get("audio_hal.in_period_size", value, NULL) > 0) |
| 1941 | pcm_config_in.period_size = atoi(value); |
| 1942 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1943 | return 0; |
| 1944 | } |
| 1945 | |
| 1946 | static struct hw_module_methods_t hal_module_methods = { |
| 1947 | .open = adev_open, |
| 1948 | }; |
| 1949 | |
| 1950 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 1951 | .common = { |
| 1952 | .tag = HARDWARE_MODULE_TAG, |
| 1953 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 1954 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 1955 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 1956 | .name = "Manta audio HW HAL", |
| 1957 | .author = "The Android Open Source Project", |
| 1958 | .methods = &hal_module_methods, |
| 1959 | }, |
| 1960 | }; |