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