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> |
| 25 | |
| 26 | #include <cutils/log.h> |
| 27 | #include <cutils/properties.h> |
| 28 | #include <cutils/str_parms.h> |
| 29 | |
| 30 | #include <hardware/audio.h> |
| 31 | #include <hardware/hardware.h> |
| 32 | |
| 33 | #include <system/audio.h> |
| 34 | |
| 35 | #include <tinyalsa/asoundlib.h> |
| 36 | |
| 37 | #include <audio_utils/resampler.h> |
| 38 | |
| 39 | #include "audio_route.h" |
| 40 | |
| 41 | #define PCM_CARD 0 |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 42 | #define PCM_CARD_SPDIF 1 |
| 43 | #define PCM_TOTAL 2 |
| 44 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 45 | #define PCM_DEVICE 0 |
| 46 | |
| 47 | struct pcm_config pcm_config = { |
| 48 | .channels = 2, |
| 49 | .rate = 44100, |
Glenn Kasten | b936627 | 2012-07-18 14:06:12 -0700 | [diff] [blame^] | 50 | .period_size = 512, |
| 51 | .period_count = 2, |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 52 | .format = PCM_FORMAT_S16_LE, |
| 53 | }; |
| 54 | |
| 55 | struct audio_device { |
| 56 | struct audio_hw_device hw_device; |
| 57 | |
| 58 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
| 59 | unsigned int devices; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 60 | bool mic_mute; |
| 61 | struct audio_route *ar; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 62 | audio_source_t input_source; |
| 63 | int cur_route_id; /* current route ID: combination of input source |
| 64 | * and output device IDs */ |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | struct stream_out { |
| 68 | struct audio_stream_out stream; |
| 69 | |
| 70 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 71 | struct pcm *pcm[PCM_TOTAL]; |
| 72 | bool standby; /* true if all PCMs are inactive */ |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 73 | unsigned int device; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 74 | |
| 75 | struct audio_device *dev; |
| 76 | }; |
| 77 | |
| 78 | struct stream_in { |
| 79 | struct audio_stream_in stream; |
| 80 | |
| 81 | pthread_mutex_t lock; /* see note below on mutex acquisition order */ |
| 82 | struct pcm *pcm; |
| 83 | bool standby; |
| 84 | |
| 85 | unsigned int requested_rate; |
| 86 | struct resampler_itfe *resampler; |
| 87 | struct resampler_buffer_provider buf_provider; |
| 88 | int16_t *buffer; |
| 89 | size_t frames_in; |
| 90 | int read_status; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 91 | audio_source_t input_source; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 92 | |
| 93 | struct audio_device *dev; |
| 94 | }; |
| 95 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 96 | enum { |
| 97 | OUT_DEVICE_SPEAKER, |
| 98 | OUT_DEVICE_HEADSET, |
| 99 | OUT_DEVICE_HEADPHONES, |
| 100 | OUT_DEVICE_BT_SCO, |
| 101 | OUT_DEVICE_SPEAKER_AND_HEADSET, |
| 102 | OUT_DEVICE_TAB_SIZE, /* number of rows in route_configs[][] */ |
| 103 | OUT_DEVICE_NONE, |
| 104 | OUT_DEVICE_INVALID, |
| 105 | OUT_DEVICE_CNT |
| 106 | }; |
| 107 | |
| 108 | enum { |
| 109 | IN_SOURCE_MIC, |
| 110 | IN_SOURCE_CAMCORDER, |
| 111 | IN_SOURCE_VOICE_RECOGNITION, |
| 112 | IN_SOURCE_VOICE_COMMUNICATION, |
| 113 | IN_SOURCE_TAB_SIZE, /* number of lines in route_configs[][] */ |
| 114 | IN_SOURCE_NONE, |
| 115 | IN_SOURCE_INVALID, |
| 116 | IN_SOURCE_CNT |
| 117 | }; |
| 118 | |
| 119 | int get_output_device_id(unsigned int device) |
| 120 | { |
| 121 | device &= AUDIO_DEVICE_OUT_ALL; |
| 122 | |
| 123 | if (device == 0) |
| 124 | return OUT_DEVICE_NONE; |
| 125 | |
| 126 | if (popcount(device) == 2) { |
| 127 | if ((device == (AUDIO_DEVICE_OUT_SPEAKER | |
| 128 | AUDIO_DEVICE_OUT_WIRED_HEADSET)) || |
| 129 | (device == (AUDIO_DEVICE_OUT_SPEAKER | |
| 130 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE))) |
| 131 | return OUT_DEVICE_SPEAKER_AND_HEADSET; |
| 132 | else |
| 133 | return OUT_DEVICE_INVALID; |
| 134 | } |
| 135 | |
| 136 | if (popcount(device) != 1) |
| 137 | return OUT_DEVICE_INVALID; |
| 138 | |
| 139 | switch (device) { |
| 140 | case AUDIO_DEVICE_OUT_SPEAKER: |
| 141 | return OUT_DEVICE_SPEAKER; |
| 142 | case AUDIO_DEVICE_OUT_WIRED_HEADSET: |
| 143 | return OUT_DEVICE_HEADSET; |
| 144 | case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: |
| 145 | return OUT_DEVICE_HEADPHONES; |
| 146 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO: |
| 147 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET: |
| 148 | case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT: |
| 149 | return OUT_DEVICE_BT_SCO; |
| 150 | default: |
| 151 | return OUT_DEVICE_INVALID; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | int get_input_source_id(audio_source_t source) |
| 156 | { |
| 157 | switch (source) { |
| 158 | case AUDIO_SOURCE_DEFAULT: |
| 159 | return IN_SOURCE_NONE; |
| 160 | case AUDIO_SOURCE_MIC: |
| 161 | return IN_SOURCE_MIC; |
| 162 | case AUDIO_SOURCE_CAMCORDER: |
| 163 | return IN_SOURCE_CAMCORDER; |
| 164 | case AUDIO_SOURCE_VOICE_RECOGNITION: |
| 165 | return IN_SOURCE_VOICE_RECOGNITION; |
| 166 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
| 167 | return IN_SOURCE_VOICE_COMMUNICATION; |
| 168 | default: |
| 169 | return IN_SOURCE_INVALID; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | struct route_config { |
| 174 | const char * const output_route; |
| 175 | const char * const input_route; |
| 176 | /* TODO add other properties here: es305 presets... */ |
| 177 | }; |
| 178 | |
| 179 | const struct route_config media_speaker = { |
| 180 | "media-speaker", |
| 181 | "media-main-mic" |
| 182 | }; |
| 183 | |
| 184 | const struct route_config media_headphones = { |
| 185 | "media-headphones", |
| 186 | "media-main-mic" |
| 187 | }; |
| 188 | |
| 189 | const struct route_config media_headset = { |
| 190 | "media-headphones", |
| 191 | "media-headset-mic" |
| 192 | }; |
| 193 | |
| 194 | const struct route_config camcorder_speaker = { |
| 195 | "media-speaker", |
| 196 | "media-second-mic" |
| 197 | }; |
| 198 | |
| 199 | const struct route_config camcorder_headphones = { |
| 200 | "media-headphones", |
| 201 | "media-second-mic" |
| 202 | }; |
| 203 | |
| 204 | const struct route_config voice_rec_speaker = { |
| 205 | "voice-rec-speaker", |
| 206 | "voice-rec-main-mic" |
| 207 | }; |
| 208 | |
| 209 | const struct route_config voice_rec_headphones = { |
| 210 | "voice-rec-headphones", |
| 211 | "voice-rec-main-mic" |
| 212 | }; |
| 213 | |
| 214 | const struct route_config voice_rec_headset = { |
| 215 | "voice-rec-headphones", |
| 216 | "voice-rec-headset-mic" |
| 217 | }; |
| 218 | |
| 219 | const struct route_config communication_speaker = { |
| 220 | "communication-speaker", |
| 221 | "communication-main-mic" |
| 222 | }; |
| 223 | |
| 224 | const struct route_config communication_headphones = { |
| 225 | "communication-headphones", |
| 226 | "communication-main-mic" |
| 227 | }; |
| 228 | |
| 229 | const struct route_config communication_headset = { |
| 230 | "communication-headphones", |
| 231 | "communication-headset-mic" |
| 232 | }; |
| 233 | |
| 234 | const struct route_config speaker_and_headphones = { |
| 235 | "speaker-and-headphones", |
| 236 | "main-mic" |
| 237 | }; |
| 238 | |
| 239 | const struct route_config bluetooth_sco = { |
| 240 | "bt-sco-headset", |
| 241 | "bt-sco-mic" |
| 242 | }; |
| 243 | |
| 244 | const struct route_config * const route_configs[IN_SOURCE_TAB_SIZE] |
| 245 | [OUT_DEVICE_TAB_SIZE] = { |
| 246 | { /* IN_SOURCE_MIC */ |
| 247 | &media_speaker, /* OUT_DEVICE_SPEAKER */ |
| 248 | &media_headset, /* OUT_DEVICE_HEADSET */ |
| 249 | &media_headphones, /* OUT_DEVICE_HEADPHONES */ |
| 250 | &bluetooth_sco, /* OUT_DEVICE_BT_SCO */ |
| 251 | &speaker_and_headphones /* OUT_DEVICE_SPEAKER_AND_HEADSET */ |
| 252 | }, |
| 253 | { /* IN_SOURCE_CAMCORDER */ |
| 254 | &camcorder_speaker, /* OUT_DEVICE_SPEAKER */ |
| 255 | &camcorder_headphones, /* OUT_DEVICE_HEADSET */ |
| 256 | &camcorder_headphones, /* OUT_DEVICE_HEADPHONES */ |
| 257 | &bluetooth_sco, /* OUT_DEVICE_BT_SCO */ |
| 258 | &speaker_and_headphones /* OUT_DEVICE_SPEAKER_AND_HEADSET */ |
| 259 | }, |
| 260 | { /* IN_SOURCE_VOICE_RECOGNITION */ |
| 261 | &voice_rec_speaker, /* OUT_DEVICE_SPEAKER */ |
| 262 | &voice_rec_headset, /* OUT_DEVICE_HEADSET */ |
| 263 | &voice_rec_headphones, /* OUT_DEVICE_HEADPHONES */ |
| 264 | &bluetooth_sco, /* OUT_DEVICE_BT_SCO */ |
| 265 | &speaker_and_headphones /* OUT_DEVICE_SPEAKER_AND_HEADSET */ |
| 266 | }, |
| 267 | { /* IN_SOURCE_VOICE_COMMUNICATION */ |
| 268 | &communication_speaker, /* OUT_DEVICE_SPEAKER */ |
| 269 | &communication_headset, /* OUT_DEVICE_HEADSET */ |
| 270 | &communication_headphones, /* OUT_DEVICE_HEADPHONES */ |
| 271 | &bluetooth_sco, /* OUT_DEVICE_BT_SCO */ |
| 272 | &speaker_and_headphones /* OUT_DEVICE_SPEAKER_AND_HEADSET */ |
| 273 | } |
| 274 | }; |
| 275 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 276 | /** |
| 277 | * NOTE: when multiple mutexes have to be acquired, always respect the |
| 278 | * following order: hw device > in stream > out stream |
| 279 | */ |
| 280 | |
| 281 | /* Helper functions */ |
| 282 | |
| 283 | static void select_devices(struct audio_device *adev) |
| 284 | { |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 285 | int output_device_id = get_output_device_id(adev->devices); |
| 286 | int input_source_id = get_input_source_id(adev->input_source); |
Simon Wilson | c4006be | 2012-08-17 11:23:38 -0700 | [diff] [blame] | 287 | const char *output_route = NULL; |
| 288 | const char *input_route = NULL; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 289 | int new_route_id; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 290 | |
Simon Wilson | c4006be | 2012-08-17 11:23:38 -0700 | [diff] [blame] | 291 | reset_mixer_state(adev->ar); |
| 292 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 293 | if (output_device_id == OUT_DEVICE_INVALID || |
| 294 | input_source_id == IN_SOURCE_INVALID) { |
| 295 | ALOGV("select_devices() invalid device %#x or source %d", |
| 296 | adev->devices, adev->input_source); |
| 297 | return; |
| 298 | } |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 299 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 300 | new_route_id = (1 << (input_source_id + OUT_DEVICE_CNT)) + (1 << output_device_id); |
| 301 | if (new_route_id == adev->cur_route_id) |
| 302 | return; |
| 303 | adev->cur_route_id = new_route_id; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 304 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 305 | if (input_source_id != IN_SOURCE_NONE) { |
| 306 | if (output_device_id != OUT_DEVICE_NONE) { |
| 307 | input_route = |
| 308 | route_configs[input_source_id][output_device_id]->input_route; |
| 309 | output_route = |
| 310 | route_configs[input_source_id][output_device_id]->output_route; |
| 311 | } else { |
| 312 | input_route = |
| 313 | route_configs[input_source_id][OUT_DEVICE_SPEAKER]->input_route; |
| 314 | } |
| 315 | } else { |
| 316 | if (output_device_id != OUT_DEVICE_NONE) { |
| 317 | output_route = |
| 318 | route_configs[IN_SOURCE_MIC][output_device_id]->output_route; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | ALOGV("select_devices() devices %#x input src %d output route %s input route %s", |
| 323 | adev->devices, adev->input_source, |
Simon Wilson | c4006be | 2012-08-17 11:23:38 -0700 | [diff] [blame] | 324 | output_route ? output_route : "none", |
| 325 | input_route ? input_route : "none"); |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 326 | |
Simon Wilson | c4006be | 2012-08-17 11:23:38 -0700 | [diff] [blame] | 327 | if (output_route) |
| 328 | audio_route_apply_path(adev->ar, output_route); |
| 329 | if (input_route) |
| 330 | audio_route_apply_path(adev->ar, input_route); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 331 | |
| 332 | update_mixer_state(adev->ar); |
| 333 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* must be called with hw device and output stream mutexes locked */ |
| 337 | static int start_output_stream(struct stream_out *out) |
| 338 | { |
| 339 | struct audio_device *adev = out->dev; |
| 340 | |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 341 | if (out->device & (AUDIO_DEVICE_OUT_SPEAKER | |
| 342 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 343 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
| 344 | AUDIO_DEVICE_OUT_AUX_DIGITAL)) { |
| 345 | out->pcm[PCM_CARD] = pcm_open(PCM_CARD, PCM_DEVICE, |
| 346 | PCM_OUT, &pcm_config); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 347 | |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 348 | if (out->pcm[PCM_CARD] && !pcm_is_ready(out->pcm[PCM_CARD])) { |
| 349 | ALOGE("pcm_open(PCM_CARD) failed: %s", |
| 350 | pcm_get_error(out->pcm[PCM_CARD])); |
| 351 | pcm_close(out->pcm[PCM_CARD]); |
| 352 | return -ENOMEM; |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | if (out->device & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) { |
| 357 | out->pcm[PCM_CARD_SPDIF] = pcm_open(PCM_CARD_SPDIF, PCM_DEVICE, |
| 358 | PCM_OUT, &pcm_config); |
| 359 | |
| 360 | if (out->pcm[PCM_CARD_SPDIF] && |
| 361 | !pcm_is_ready(out->pcm[PCM_CARD_SPDIF])) { |
| 362 | ALOGE("pcm_open(PCM_CARD_SPDIF) failed: %s", |
| 363 | pcm_get_error(out->pcm[PCM_CARD_SPDIF])); |
| 364 | pcm_close(out->pcm[PCM_CARD_SPDIF]); |
| 365 | return -ENOMEM; |
| 366 | } |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 369 | adev->devices &= ~AUDIO_DEVICE_OUT_ALL; |
| 370 | adev->devices |= out->device; |
| 371 | select_devices(adev); |
| 372 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | /* must be called with hw device and input stream mutexes locked */ |
| 377 | static int start_input_stream(struct stream_in *in) |
| 378 | { |
| 379 | struct audio_device *adev = in->dev; |
| 380 | |
| 381 | in->pcm = pcm_open(PCM_CARD, PCM_DEVICE, PCM_IN, &pcm_config); |
| 382 | |
| 383 | if (in->pcm && !pcm_is_ready(in->pcm)) { |
| 384 | ALOGE("pcm_open() failed: %s", pcm_get_error(in->pcm)); |
| 385 | pcm_close(in->pcm); |
| 386 | return -ENOMEM; |
| 387 | } |
| 388 | |
| 389 | /* if no supported sample rate is available, use the resampler */ |
| 390 | if (in->resampler) |
| 391 | in->resampler->reset(in->resampler); |
| 392 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 393 | adev->input_source = in->input_source; |
| 394 | select_devices(adev); |
| 395 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 396 | return 0; |
| 397 | } |
| 398 | |
| 399 | static size_t get_input_buffer_size(unsigned int sample_rate, |
| 400 | audio_format_t format, |
| 401 | unsigned int channel_count) |
| 402 | { |
| 403 | size_t size; |
| 404 | |
| 405 | /* |
| 406 | * take resampling into account and return the closest majoring |
| 407 | * multiple of 16 frames, as audioflinger expects audio buffers to |
| 408 | * be a multiple of 16 frames |
| 409 | */ |
| 410 | size = (pcm_config.period_size * sample_rate) / pcm_config.rate; |
| 411 | size = ((size + 15) / 16) * 16; |
| 412 | |
| 413 | return size * channel_count * audio_bytes_per_sample(format); |
| 414 | } |
| 415 | |
| 416 | static int get_next_buffer(struct resampler_buffer_provider *buffer_provider, |
| 417 | struct resampler_buffer* buffer) |
| 418 | { |
| 419 | struct stream_in *in; |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 420 | size_t i; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 421 | |
| 422 | if (buffer_provider == NULL || buffer == NULL) |
| 423 | return -EINVAL; |
| 424 | |
| 425 | in = (struct stream_in *)((char *)buffer_provider - |
| 426 | offsetof(struct stream_in, buf_provider)); |
| 427 | |
| 428 | if (in->pcm == NULL) { |
| 429 | buffer->raw = NULL; |
| 430 | buffer->frame_count = 0; |
| 431 | in->read_status = -ENODEV; |
| 432 | return -ENODEV; |
| 433 | } |
| 434 | |
| 435 | if (in->frames_in == 0) { |
| 436 | in->read_status = pcm_read(in->pcm, |
| 437 | (void*)in->buffer, |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 438 | pcm_frames_to_bytes(in->pcm, pcm_config.period_size)); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 439 | if (in->read_status != 0) { |
| 440 | ALOGE("get_next_buffer() pcm_read error %d", in->read_status); |
| 441 | buffer->raw = NULL; |
| 442 | buffer->frame_count = 0; |
| 443 | return in->read_status; |
| 444 | } |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 445 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 446 | in->frames_in = pcm_config.period_size; |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 447 | |
| 448 | /* Do stereo to mono conversion in place by discarding right channel */ |
| 449 | for (i = 1; i < in->frames_in; i++) |
| 450 | in->buffer[i] = in->buffer[i * 2]; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | buffer->frame_count = (buffer->frame_count > in->frames_in) ? |
| 454 | in->frames_in : buffer->frame_count; |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 455 | buffer->i16 = in->buffer + (pcm_config.period_size - in->frames_in); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 456 | |
| 457 | return in->read_status; |
| 458 | |
| 459 | } |
| 460 | |
| 461 | static void release_buffer(struct resampler_buffer_provider *buffer_provider, |
| 462 | struct resampler_buffer* buffer) |
| 463 | { |
| 464 | struct stream_in *in; |
| 465 | |
| 466 | if (buffer_provider == NULL || buffer == NULL) |
| 467 | return; |
| 468 | |
| 469 | in = (struct stream_in *)((char *)buffer_provider - |
| 470 | offsetof(struct stream_in, buf_provider)); |
| 471 | |
| 472 | in->frames_in -= buffer->frame_count; |
| 473 | } |
| 474 | |
| 475 | /* read_frames() reads frames from kernel driver, down samples to capture rate |
| 476 | * if necessary and output the number of frames requested to the buffer specified */ |
| 477 | static ssize_t read_frames(struct stream_in *in, void *buffer, ssize_t frames) |
| 478 | { |
| 479 | ssize_t frames_wr = 0; |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 480 | size_t frame_size = audio_stream_frame_size(&in->stream.common); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 481 | |
| 482 | while (frames_wr < frames) { |
| 483 | size_t frames_rd = frames - frames_wr; |
| 484 | if (in->resampler != NULL) { |
| 485 | in->resampler->resample_from_provider(in->resampler, |
| 486 | (int16_t *)((char *)buffer + |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 487 | frames_wr * frame_size), |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 488 | &frames_rd); |
| 489 | } else { |
| 490 | struct resampler_buffer buf = { |
| 491 | { raw : NULL, }, |
| 492 | frame_count : frames_rd, |
| 493 | }; |
| 494 | get_next_buffer(&in->buf_provider, &buf); |
| 495 | if (buf.raw != NULL) { |
| 496 | memcpy((char *)buffer + |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 497 | frames_wr * frame_size, |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 498 | buf.raw, |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 499 | buf.frame_count * frame_size); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 500 | frames_rd = buf.frame_count; |
| 501 | } |
| 502 | release_buffer(&in->buf_provider, &buf); |
| 503 | } |
| 504 | /* in->read_status is updated by getNextBuffer() also called by |
| 505 | * in->resampler->resample_from_provider() */ |
| 506 | if (in->read_status != 0) |
| 507 | return in->read_status; |
| 508 | |
| 509 | frames_wr += frames_rd; |
| 510 | } |
| 511 | return frames_wr; |
| 512 | } |
| 513 | |
| 514 | /* API functions */ |
| 515 | |
| 516 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 517 | { |
| 518 | return pcm_config.rate; |
| 519 | } |
| 520 | |
| 521 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 522 | { |
| 523 | return -ENOSYS; |
| 524 | } |
| 525 | |
| 526 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 527 | { |
| 528 | return pcm_config.period_size * |
| 529 | audio_stream_frame_size((struct audio_stream *)stream); |
| 530 | } |
| 531 | |
Glenn Kasten | ca1414b | 2012-06-25 10:38:47 -0700 | [diff] [blame] | 532 | 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] | 533 | { |
| 534 | return AUDIO_CHANNEL_OUT_STEREO; |
| 535 | } |
| 536 | |
| 537 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 538 | { |
| 539 | return AUDIO_FORMAT_PCM_16_BIT; |
| 540 | } |
| 541 | |
| 542 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 543 | { |
| 544 | return -ENOSYS; |
| 545 | } |
| 546 | |
Simon Wilson | 459d5bd | 2012-08-21 12:18:07 -0700 | [diff] [blame] | 547 | static int do_out_standby(struct stream_out *out) |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 548 | { |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 549 | int i; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 550 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 551 | if (!out->standby) { |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 552 | for (i = 0; i < PCM_TOTAL; i++) { |
| 553 | if (out->pcm[i]) { |
| 554 | pcm_close(out->pcm[i]); |
| 555 | out->pcm[i] = NULL; |
| 556 | } |
| 557 | } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 558 | |
| 559 | out->dev->devices &= ~AUDIO_DEVICE_OUT_ALL; |
| 560 | select_devices(out->dev); |
| 561 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 562 | out->standby = true; |
| 563 | } |
| 564 | |
Simon Wilson | 459d5bd | 2012-08-21 12:18:07 -0700 | [diff] [blame] | 565 | return 0; |
| 566 | } |
| 567 | |
| 568 | static int out_standby(struct audio_stream *stream) |
| 569 | { |
| 570 | struct stream_out *out = (struct stream_out *)stream; |
| 571 | int ret; |
| 572 | |
| 573 | pthread_mutex_lock(&out->dev->lock); |
| 574 | pthread_mutex_lock(&out->lock); |
| 575 | |
| 576 | ret = do_out_standby(out); |
| 577 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 578 | pthread_mutex_unlock(&out->lock); |
| 579 | pthread_mutex_unlock(&out->dev->lock); |
| 580 | |
Simon Wilson | 459d5bd | 2012-08-21 12:18:07 -0700 | [diff] [blame] | 581 | return ret; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | static int out_dump(const struct audio_stream *stream, int fd) |
| 585 | { |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 590 | { |
| 591 | struct stream_out *out = (struct stream_out *)stream; |
| 592 | struct audio_device *adev = out->dev; |
| 593 | struct str_parms *parms; |
| 594 | char value[32]; |
| 595 | int ret; |
| 596 | unsigned int val; |
| 597 | |
| 598 | parms = str_parms_create_str(kvpairs); |
| 599 | |
| 600 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, |
| 601 | value, sizeof(value)); |
| 602 | pthread_mutex_lock(&adev->lock); |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 603 | pthread_mutex_lock(&out->lock); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 604 | if (ret >= 0) { |
| 605 | val = atoi(value); |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 606 | if ((out->device != val) && (val != 0)) { |
Simon Wilson | 459d5bd | 2012-08-21 12:18:07 -0700 | [diff] [blame] | 607 | /* Force standby if moving to/from SPDIF or if the output |
| 608 | * device changes when in SPDIF mode */ |
| 609 | if (((val & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) ^ |
| 610 | (adev->devices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) || |
| 611 | (adev->devices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) { |
| 612 | do_out_standby(out); |
| 613 | } |
| 614 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 615 | out->device = val; |
| 616 | if (!out->standby) { |
| 617 | adev->devices &= ~AUDIO_DEVICE_OUT_ALL; |
| 618 | adev->devices |= out->device; |
| 619 | select_devices(adev); |
| 620 | } |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 621 | } |
| 622 | } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 623 | pthread_mutex_unlock(&out->lock); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 624 | pthread_mutex_unlock(&adev->lock); |
| 625 | |
| 626 | str_parms_destroy(parms); |
| 627 | return ret; |
| 628 | } |
| 629 | |
| 630 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 631 | { |
| 632 | return strdup(""); |
| 633 | } |
| 634 | |
| 635 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 636 | { |
| 637 | return (pcm_config.period_size * pcm_config.period_count * 1000) / |
| 638 | pcm_config.rate; |
| 639 | } |
| 640 | |
| 641 | static int out_set_volume(struct audio_stream_out *stream, float left, |
| 642 | float right) |
| 643 | { |
| 644 | return -ENOSYS; |
| 645 | } |
| 646 | |
| 647 | static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, |
| 648 | size_t bytes) |
| 649 | { |
| 650 | int ret; |
| 651 | struct stream_out *out = (struct stream_out *)stream; |
| 652 | struct audio_device *adev = out->dev; |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 653 | int i; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 654 | |
| 655 | /* |
| 656 | * acquiring hw device mutex systematically is useful if a low |
| 657 | * priority thread is waiting on the output stream mutex - e.g. |
| 658 | * executing out_set_parameters() while holding the hw device |
| 659 | * mutex |
| 660 | */ |
| 661 | pthread_mutex_lock(&adev->lock); |
| 662 | pthread_mutex_lock(&out->lock); |
| 663 | if (out->standby) { |
| 664 | ret = start_output_stream(out); |
| 665 | if (ret != 0) { |
| 666 | pthread_mutex_unlock(&adev->lock); |
| 667 | goto exit; |
| 668 | } |
| 669 | out->standby = false; |
| 670 | } |
| 671 | pthread_mutex_unlock(&adev->lock); |
| 672 | |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 673 | /* Write to all active PCMs */ |
| 674 | for (i = 0; i < PCM_TOTAL; i++) |
| 675 | if (out->pcm[i]) |
| 676 | pcm_write(out->pcm[i], (void *)buffer, bytes); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 677 | |
| 678 | exit: |
| 679 | pthread_mutex_unlock(&out->lock); |
| 680 | |
| 681 | if (ret != 0) { |
| 682 | usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) / |
| 683 | out_get_sample_rate(&stream->common)); |
| 684 | } |
| 685 | |
| 686 | return bytes; |
| 687 | } |
| 688 | |
| 689 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 690 | uint32_t *dsp_frames) |
| 691 | { |
| 692 | return -EINVAL; |
| 693 | } |
| 694 | |
| 695 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 696 | { |
| 697 | return 0; |
| 698 | } |
| 699 | |
| 700 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 701 | { |
| 702 | return 0; |
| 703 | } |
| 704 | |
| 705 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream, |
| 706 | int64_t *timestamp) |
| 707 | { |
| 708 | return -EINVAL; |
| 709 | } |
| 710 | |
| 711 | /** audio_stream_in implementation **/ |
| 712 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 713 | { |
| 714 | struct stream_in *in = (struct stream_in *)stream; |
| 715 | |
| 716 | return in->requested_rate; |
| 717 | } |
| 718 | |
| 719 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 720 | { |
| 721 | return 0; |
| 722 | } |
| 723 | |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 724 | static audio_channel_mask_t in_get_channels(const struct audio_stream *stream) |
| 725 | { |
| 726 | return AUDIO_CHANNEL_IN_MONO; |
| 727 | } |
| 728 | |
| 729 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 730 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
| 731 | { |
| 732 | struct stream_in *in = (struct stream_in *)stream; |
| 733 | |
| 734 | return get_input_buffer_size(in->requested_rate, |
| 735 | AUDIO_FORMAT_PCM_16_BIT, |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 736 | popcount(in_get_channels(stream))); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 740 | { |
| 741 | return AUDIO_FORMAT_PCM_16_BIT; |
| 742 | } |
| 743 | |
| 744 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 745 | { |
| 746 | return -ENOSYS; |
| 747 | } |
| 748 | |
| 749 | static int in_standby(struct audio_stream *stream) |
| 750 | { |
| 751 | struct stream_in *in = (struct stream_in *)stream; |
| 752 | |
| 753 | pthread_mutex_lock(&in->dev->lock); |
| 754 | pthread_mutex_lock(&in->lock); |
| 755 | |
| 756 | if (!in->standby) { |
| 757 | pcm_close(in->pcm); |
| 758 | in->pcm = NULL; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 759 | in->dev->input_source = AUDIO_SOURCE_DEFAULT; |
| 760 | select_devices(in->dev); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 761 | in->standby = true; |
| 762 | } |
| 763 | |
| 764 | pthread_mutex_unlock(&in->lock); |
| 765 | pthread_mutex_unlock(&in->dev->lock); |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | static int in_dump(const struct audio_stream *stream, int fd) |
| 771 | { |
| 772 | return 0; |
| 773 | } |
| 774 | |
| 775 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 776 | { |
| 777 | struct stream_in *in = (struct stream_in *)stream; |
| 778 | struct audio_device *adev = in->dev; |
| 779 | struct str_parms *parms; |
| 780 | char value[32]; |
| 781 | int ret; |
| 782 | unsigned int val; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 783 | bool apply_now = false; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 784 | |
| 785 | parms = str_parms_create_str(kvpairs); |
| 786 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 787 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_INPUT_SOURCE, |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 788 | value, sizeof(value)); |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 789 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 790 | pthread_mutex_lock(&adev->lock); |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 791 | pthread_mutex_lock(&in->lock); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 792 | if (ret >= 0) { |
| 793 | val = atoi(value); |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 794 | /* no audio source uses val == 0 */ |
| 795 | if ((in->input_source != val) && (val != 0)) { |
| 796 | in->input_source = val; |
| 797 | apply_now = !in->standby; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 798 | } |
| 799 | } |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 800 | |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 801 | if (apply_now) { |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 802 | adev->input_source = in->input_source; |
| 803 | select_devices(adev); |
| 804 | } |
| 805 | |
| 806 | pthread_mutex_unlock(&in->lock); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 807 | pthread_mutex_unlock(&adev->lock); |
| 808 | |
| 809 | str_parms_destroy(parms); |
| 810 | return ret; |
| 811 | } |
| 812 | |
| 813 | static char * in_get_parameters(const struct audio_stream *stream, |
| 814 | const char *keys) |
| 815 | { |
| 816 | return strdup(""); |
| 817 | } |
| 818 | |
| 819 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 820 | { |
| 821 | return 0; |
| 822 | } |
| 823 | |
| 824 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, |
| 825 | size_t bytes) |
| 826 | { |
| 827 | int ret = 0; |
| 828 | struct stream_in *in = (struct stream_in *)stream; |
| 829 | struct audio_device *adev = in->dev; |
| 830 | size_t frames_rq = bytes / audio_stream_frame_size(&stream->common); |
| 831 | |
| 832 | /* |
| 833 | * acquiring hw device mutex systematically is useful if a low |
| 834 | * priority thread is waiting on the input stream mutex - e.g. |
| 835 | * executing in_set_parameters() while holding the hw device |
| 836 | * mutex |
| 837 | */ |
| 838 | pthread_mutex_lock(&adev->lock); |
| 839 | pthread_mutex_lock(&in->lock); |
| 840 | if (in->standby) { |
| 841 | ret = start_input_stream(in); |
| 842 | if (ret == 0) |
| 843 | in->standby = 0; |
| 844 | } |
| 845 | pthread_mutex_unlock(&adev->lock); |
| 846 | |
| 847 | if (ret < 0) |
| 848 | goto exit; |
| 849 | |
| 850 | /*if (in->num_preprocessors != 0) |
| 851 | ret = process_frames(in, buffer, frames_rq); |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 852 | else */ |
| 853 | ret = read_frames(in, buffer, frames_rq); |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 854 | |
| 855 | if (ret > 0) |
| 856 | ret = 0; |
| 857 | |
| 858 | /* |
| 859 | * Instead of writing zeroes here, we could trust the hardware |
| 860 | * to always provide zeroes when muted. |
| 861 | */ |
| 862 | if (ret == 0 && adev->mic_mute) |
| 863 | memset(buffer, 0, bytes); |
| 864 | |
| 865 | exit: |
| 866 | if (ret < 0) |
| 867 | usleep(bytes * 1000000 / audio_stream_frame_size(&stream->common) / |
| 868 | in_get_sample_rate(&stream->common)); |
| 869 | |
| 870 | pthread_mutex_unlock(&in->lock); |
| 871 | return bytes; |
| 872 | } |
| 873 | |
| 874 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 875 | { |
| 876 | return 0; |
| 877 | } |
| 878 | |
| 879 | static int in_add_audio_effect(const struct audio_stream *stream, |
| 880 | effect_handle_t effect) |
| 881 | { |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | static int in_remove_audio_effect(const struct audio_stream *stream, |
| 886 | effect_handle_t effect) |
| 887 | { |
| 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | |
| 892 | static int adev_open_output_stream(struct audio_hw_device *dev, |
| 893 | audio_io_handle_t handle, |
| 894 | audio_devices_t devices, |
| 895 | audio_output_flags_t flags, |
| 896 | struct audio_config *config, |
| 897 | struct audio_stream_out **stream_out) |
| 898 | { |
| 899 | struct audio_device *adev = (struct audio_device *)dev; |
| 900 | struct stream_out *out; |
| 901 | int ret; |
| 902 | |
| 903 | out = (struct stream_out *)calloc(1, sizeof(struct stream_out)); |
| 904 | if (!out) |
| 905 | return -ENOMEM; |
| 906 | |
| 907 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 908 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 909 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 910 | out->stream.common.get_channels = out_get_channels; |
| 911 | out->stream.common.get_format = out_get_format; |
| 912 | out->stream.common.set_format = out_set_format; |
| 913 | out->stream.common.standby = out_standby; |
| 914 | out->stream.common.dump = out_dump; |
| 915 | out->stream.common.set_parameters = out_set_parameters; |
| 916 | out->stream.common.get_parameters = out_get_parameters; |
| 917 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 918 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 919 | out->stream.get_latency = out_get_latency; |
| 920 | out->stream.set_volume = out_set_volume; |
| 921 | out->stream.write = out_write; |
| 922 | out->stream.get_render_position = out_get_render_position; |
| 923 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
| 924 | |
| 925 | out->dev = adev; |
| 926 | |
| 927 | config->format = out_get_format(&out->stream.common); |
| 928 | config->channel_mask = out_get_channels(&out->stream.common); |
| 929 | config->sample_rate = out_get_sample_rate(&out->stream.common); |
| 930 | |
| 931 | out->standby = true; |
| 932 | |
| 933 | *stream_out = &out->stream; |
| 934 | return 0; |
| 935 | |
| 936 | err_open: |
| 937 | free(out); |
| 938 | *stream_out = NULL; |
| 939 | return ret; |
| 940 | } |
| 941 | |
| 942 | static void adev_close_output_stream(struct audio_hw_device *dev, |
| 943 | struct audio_stream_out *stream) |
| 944 | { |
| 945 | out_standby(&stream->common); |
| 946 | free(stream); |
| 947 | } |
| 948 | |
| 949 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 950 | { |
| 951 | return 0; |
| 952 | } |
| 953 | |
| 954 | static char * adev_get_parameters(const struct audio_hw_device *dev, |
| 955 | const char *keys) |
| 956 | { |
| 957 | return strdup(""); |
| 958 | } |
| 959 | |
| 960 | static int adev_init_check(const struct audio_hw_device *dev) |
| 961 | { |
| 962 | return 0; |
| 963 | } |
| 964 | |
| 965 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 966 | { |
| 967 | return -ENOSYS; |
| 968 | } |
| 969 | |
| 970 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 971 | { |
| 972 | return -ENOSYS; |
| 973 | } |
| 974 | |
| 975 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 976 | { |
| 977 | return 0; |
| 978 | } |
| 979 | |
| 980 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 981 | { |
| 982 | struct audio_device *adev = (struct audio_device *)dev; |
| 983 | |
| 984 | adev->mic_mute = state; |
| 985 | |
| 986 | return 0; |
| 987 | } |
| 988 | |
| 989 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 990 | { |
| 991 | struct audio_device *adev = (struct audio_device *)dev; |
| 992 | |
| 993 | *state = adev->mic_mute; |
| 994 | |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, |
| 999 | const struct audio_config *config) |
| 1000 | { |
| 1001 | |
| 1002 | return get_input_buffer_size(config->sample_rate, config->format, |
| 1003 | popcount(config->channel_mask)); |
| 1004 | } |
| 1005 | |
| 1006 | static int adev_open_input_stream(struct audio_hw_device *dev, |
| 1007 | audio_io_handle_t handle, |
| 1008 | audio_devices_t devices, |
| 1009 | struct audio_config *config, |
| 1010 | struct audio_stream_in **stream_in) |
| 1011 | { |
| 1012 | struct audio_device *adev = (struct audio_device *)dev; |
| 1013 | struct stream_in *in; |
| 1014 | int ret; |
| 1015 | |
| 1016 | *stream_in = NULL; |
| 1017 | |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 1018 | /* Respond with a request for mono if a different format is given. */ |
| 1019 | if (config->channel_mask != AUDIO_CHANNEL_IN_MONO) { |
| 1020 | config->channel_mask = AUDIO_CHANNEL_IN_MONO; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1021 | return -EINVAL; |
| 1022 | } |
| 1023 | |
| 1024 | in = (struct stream_in *)calloc(1, sizeof(struct stream_in)); |
| 1025 | if (!in) |
| 1026 | return -ENOMEM; |
| 1027 | |
| 1028 | in->stream.common.get_sample_rate = in_get_sample_rate; |
| 1029 | in->stream.common.set_sample_rate = in_set_sample_rate; |
| 1030 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 1031 | in->stream.common.get_channels = in_get_channels; |
| 1032 | in->stream.common.get_format = in_get_format; |
| 1033 | in->stream.common.set_format = in_set_format; |
| 1034 | in->stream.common.standby = in_standby; |
| 1035 | in->stream.common.dump = in_dump; |
| 1036 | in->stream.common.set_parameters = in_set_parameters; |
| 1037 | in->stream.common.get_parameters = in_get_parameters; |
| 1038 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 1039 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 1040 | in->stream.set_gain = in_set_gain; |
| 1041 | in->stream.read = in_read; |
| 1042 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
| 1043 | |
| 1044 | in->dev = adev; |
| 1045 | in->standby = true; |
| 1046 | in->requested_rate = config->sample_rate; |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 1047 | in->input_source = AUDIO_SOURCE_DEFAULT; |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1048 | |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 1049 | in->buffer = malloc(pcm_config.period_size * pcm_config.channels |
| 1050 | * audio_stream_frame_size(&in->stream.common)); |
| 1051 | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1052 | if (!in->buffer) { |
| 1053 | ret = -ENOMEM; |
| 1054 | goto err_malloc; |
| 1055 | } |
| 1056 | |
| 1057 | if (in->requested_rate != pcm_config.rate) { |
| 1058 | in->buf_provider.get_next_buffer = get_next_buffer; |
| 1059 | in->buf_provider.release_buffer = release_buffer; |
| 1060 | |
| 1061 | ret = create_resampler(pcm_config.rate, |
| 1062 | in->requested_rate, |
Eric Laurent | d7abdd0 | 2012-07-27 14:54:41 -0700 | [diff] [blame] | 1063 | 1, |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1064 | RESAMPLER_QUALITY_DEFAULT, |
| 1065 | &in->buf_provider, |
| 1066 | &in->resampler); |
| 1067 | if (ret != 0) { |
| 1068 | ret = -EINVAL; |
| 1069 | goto err_resampler; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | *stream_in = &in->stream; |
| 1074 | return 0; |
| 1075 | |
| 1076 | err_resampler: |
| 1077 | free(in->buffer); |
| 1078 | err_malloc: |
| 1079 | free(in); |
| 1080 | return ret; |
| 1081 | } |
| 1082 | |
| 1083 | static void adev_close_input_stream(struct audio_hw_device *dev, |
| 1084 | struct audio_stream_in *stream) |
| 1085 | { |
| 1086 | struct stream_in *in = (struct stream_in *)stream; |
| 1087 | |
| 1088 | in_standby(&stream->common); |
| 1089 | if (in->resampler) { |
| 1090 | release_resampler(in->resampler); |
| 1091 | in->resampler = NULL; |
| 1092 | } |
| 1093 | free(in->buffer); |
| 1094 | free(stream); |
| 1095 | } |
| 1096 | |
| 1097 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 1098 | { |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | static int adev_close(hw_device_t *device) |
| 1103 | { |
| 1104 | struct audio_device *adev = (struct audio_device *)device; |
| 1105 | |
| 1106 | audio_route_free(adev->ar); |
| 1107 | |
| 1108 | free(device); |
| 1109 | return 0; |
| 1110 | } |
| 1111 | |
| 1112 | static uint32_t adev_get_supported_devices(const struct audio_hw_device *dev) |
| 1113 | { |
| 1114 | return (/* OUT */ |
| 1115 | AUDIO_DEVICE_OUT_SPEAKER | |
| 1116 | AUDIO_DEVICE_OUT_WIRED_HEADSET | |
| 1117 | AUDIO_DEVICE_OUT_WIRED_HEADPHONE | |
Simon Wilson | 56d84e2 | 2012-08-17 13:55:27 -0700 | [diff] [blame] | 1118 | AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET | |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1119 | /*AUDIO_DEVICE_OUT_ALL_SCO |*/ |
| 1120 | AUDIO_DEVICE_OUT_DEFAULT | |
| 1121 | /* IN */ |
| 1122 | AUDIO_DEVICE_IN_BUILTIN_MIC | |
| 1123 | AUDIO_DEVICE_IN_WIRED_HEADSET | |
| 1124 | AUDIO_DEVICE_IN_BACK_MIC | |
| 1125 | /*AUDIO_DEVICE_IN_ALL_SCO |*/ |
| 1126 | AUDIO_DEVICE_IN_DEFAULT); |
| 1127 | } |
| 1128 | |
| 1129 | static int adev_open(const hw_module_t* module, const char* name, |
| 1130 | hw_device_t** device) |
| 1131 | { |
| 1132 | struct audio_device *adev; |
| 1133 | int ret; |
| 1134 | |
| 1135 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 1136 | return -EINVAL; |
| 1137 | |
| 1138 | adev = calloc(1, sizeof(struct audio_device)); |
| 1139 | if (!adev) |
| 1140 | return -ENOMEM; |
| 1141 | |
| 1142 | adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; |
| 1143 | adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_1_0; |
| 1144 | adev->hw_device.common.module = (struct hw_module_t *) module; |
| 1145 | adev->hw_device.common.close = adev_close; |
| 1146 | |
| 1147 | adev->hw_device.get_supported_devices = adev_get_supported_devices; |
| 1148 | adev->hw_device.init_check = adev_init_check; |
| 1149 | adev->hw_device.set_voice_volume = adev_set_voice_volume; |
| 1150 | adev->hw_device.set_master_volume = adev_set_master_volume; |
| 1151 | adev->hw_device.set_mode = adev_set_mode; |
| 1152 | adev->hw_device.set_mic_mute = adev_set_mic_mute; |
| 1153 | adev->hw_device.get_mic_mute = adev_get_mic_mute; |
| 1154 | adev->hw_device.set_parameters = adev_set_parameters; |
| 1155 | adev->hw_device.get_parameters = adev_get_parameters; |
| 1156 | adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size; |
| 1157 | adev->hw_device.open_output_stream = adev_open_output_stream; |
| 1158 | adev->hw_device.close_output_stream = adev_close_output_stream; |
| 1159 | adev->hw_device.open_input_stream = adev_open_input_stream; |
| 1160 | adev->hw_device.close_input_stream = adev_close_input_stream; |
| 1161 | adev->hw_device.dump = adev_dump; |
| 1162 | |
| 1163 | adev->ar = audio_route_init(); |
Eric Laurent | 8753203 | 2012-07-16 13:53:20 -0700 | [diff] [blame] | 1164 | adev->input_source = AUDIO_SOURCE_DEFAULT; |
| 1165 | /* adev->cur_route_id initial value is 0 and such that first device |
| 1166 | * selection is always applied by select_devices() */ |
Simon Wilson | 15f60a8 | 2012-04-24 20:56:32 -0700 | [diff] [blame] | 1167 | |
| 1168 | *device = &adev->hw_device.common; |
| 1169 | |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
| 1173 | static struct hw_module_methods_t hal_module_methods = { |
| 1174 | .open = adev_open, |
| 1175 | }; |
| 1176 | |
| 1177 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 1178 | .common = { |
| 1179 | .tag = HARDWARE_MODULE_TAG, |
| 1180 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 1181 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 1182 | .id = AUDIO_HARDWARE_MODULE_ID, |
| 1183 | .name = "Manta audio HW HAL", |
| 1184 | .author = "The Android Open Source Project", |
| 1185 | .methods = &hal_module_methods, |
| 1186 | }, |
| 1187 | }; |