Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 1 | /* |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 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. |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 15 | * |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 16 | * Copied as it is from device/amlogic/generic/hal/audio/ |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 17 | */ |
| 18 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 19 | #define LOG_TAG "audio_hw_yukawa" |
| 20 | //#define LOG_NDEBUG 0 |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 21 | |
| 22 | #include <errno.h> |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 23 | #include <inttypes.h> |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 24 | #include <malloc.h> |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 25 | #include <pthread.h> |
| 26 | #include <stdint.h> |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 27 | #include <stdlib.h> |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 28 | #include <sys/time.h> |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 29 | #include <unistd.h> |
| 30 | |
| 31 | #include <log/log.h> |
| 32 | #include <cutils/str_parms.h> |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 33 | #include <cutils/properties.h> |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 34 | |
| 35 | #include <hardware/hardware.h> |
| 36 | #include <system/audio.h> |
| 37 | #include <hardware/audio.h> |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 38 | |
| 39 | #include <audio_effects/effect_aec.h> |
| 40 | #include <audio_route/audio_route.h> |
| 41 | #include <audio_utils/clock.h> |
| 42 | #include <audio_utils/echo_reference.h> |
| 43 | #include <audio_utils/resampler.h> |
| 44 | #include <hardware/audio_alsaops.h> |
| 45 | #include <hardware/audio_effect.h> |
| 46 | #include <sound/asound.h> |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 47 | #include <tinyalsa/asoundlib.h> |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 48 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 49 | #include <sys/ioctl.h> |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 50 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 51 | #include "audio_aec.h" |
| 52 | #include "audio_hw.h" |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 53 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 54 | static int adev_get_mic_mute(const struct audio_hw_device* dev, bool* state); |
| 55 | static int adev_get_microphones(const struct audio_hw_device* dev, |
| 56 | struct audio_microphone_characteristic_t* mic_array, |
| 57 | size_t* mic_count); |
| 58 | static size_t out_get_buffer_size(const struct audio_stream* stream); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 59 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 60 | static int get_audio_output_port(audio_devices_t devices) { |
| 61 | /* Only HDMI out for now #FIXME */ |
| 62 | return PORT_HDMI; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 63 | } |
| 64 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 65 | static void timestamp_adjust(struct timespec* ts, ssize_t frames, uint32_t sampling_rate) { |
| 66 | /* This function assumes the adjustment (in nsec) is less than the max value of long, |
| 67 | * which for 32-bit long this is 2^31 * 1e-9 seconds, slightly over 2 seconds. |
| 68 | * For 64-bit long it is 9e+9 seconds. */ |
| 69 | long adj_nsec = (frames / (float) sampling_rate) * 1E9L; |
| 70 | ts->tv_nsec += adj_nsec; |
| 71 | while (ts->tv_nsec > 1E9L) { |
| 72 | ts->tv_sec++; |
| 73 | ts->tv_nsec -= 1E9L; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 74 | } |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 75 | if (ts->tv_nsec < 0) { |
| 76 | ts->tv_sec--; |
| 77 | ts->tv_nsec += 1E9L; |
| 78 | } |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 79 | } |
| 80 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 81 | /* Helper function to get PCM hardware timestamp. |
| 82 | * Only the field 'timestamp' of argument 'ts' is updated. */ |
| 83 | static int get_pcm_timestamp(struct pcm* pcm, uint32_t sample_rate, struct aec_info* info, |
| 84 | bool isOutput) { |
| 85 | int ret = 0; |
| 86 | if (pcm_get_htimestamp(pcm, &info->available, &info->timestamp) < 0) { |
| 87 | ALOGE("Error getting PCM timestamp!"); |
| 88 | info->timestamp.tv_sec = 0; |
| 89 | info->timestamp.tv_nsec = 0; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 90 | return -EINVAL; |
| 91 | } |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 92 | ssize_t frames; |
| 93 | if (isOutput) { |
| 94 | frames = pcm_get_buffer_size(pcm) - info->available; |
| 95 | } else { |
| 96 | frames = -info->available; /* rewind timestamp */ |
| 97 | } |
| 98 | timestamp_adjust(&info->timestamp, frames, sample_rate); |
| 99 | return ret; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 100 | } |
| 101 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 102 | static int read_filter_from_file(const char* filename, int16_t* filter, int max_length) { |
| 103 | FILE* fp = fopen(filename, "r"); |
| 104 | if (fp == NULL) { |
| 105 | ALOGI("%s: File %s not found.", __func__, filename); |
| 106 | return 0; |
| 107 | } |
| 108 | int num_taps = 0; |
| 109 | char* line = NULL; |
| 110 | size_t len = 0; |
| 111 | while (!feof(fp)) { |
| 112 | size_t size = getline(&line, &len, fp); |
| 113 | if ((line[0] == '#') || (size < 2)) { |
| 114 | continue; |
| 115 | } |
| 116 | int n = sscanf(line, "%" SCNd16 "\n", &filter[num_taps++]); |
| 117 | if (n < 1) { |
| 118 | ALOGE("Could not find coefficient %d! Exiting...", num_taps - 1); |
| 119 | return 0; |
| 120 | } |
| 121 | ALOGV("Coeff %d : %" PRId16, num_taps, filter[num_taps - 1]); |
| 122 | if (num_taps == max_length) { |
| 123 | ALOGI("%s: max tap length %d reached.", __func__, max_length); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 124 | break; |
| 125 | } |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 126 | } |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 127 | free(line); |
| 128 | fclose(fp); |
| 129 | return num_taps; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 130 | } |
| 131 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 132 | static void out_set_eq(struct alsa_stream_out* out) { |
| 133 | out->speaker_eq = NULL; |
| 134 | int16_t* speaker_eq_coeffs = (int16_t*)calloc(SPEAKER_MAX_EQ_LENGTH, sizeof(int16_t)); |
| 135 | if (speaker_eq_coeffs == NULL) { |
| 136 | ALOGE("%s: Failed to allocate speaker EQ", __func__); |
| 137 | return; |
| 138 | } |
| 139 | int num_taps = read_filter_from_file(SPEAKER_EQ_FILE, speaker_eq_coeffs, SPEAKER_MAX_EQ_LENGTH); |
| 140 | if (num_taps == 0) { |
| 141 | ALOGI("%s: Empty filter file or 0 taps set.", __func__); |
| 142 | free(speaker_eq_coeffs); |
| 143 | return; |
| 144 | } |
| 145 | out->speaker_eq = fir_init( |
| 146 | out->config.channels, FIR_SINGLE_FILTER, num_taps, |
| 147 | out_get_buffer_size(&out->stream.common) / out->config.channels / sizeof(int16_t), |
| 148 | speaker_eq_coeffs); |
| 149 | free(speaker_eq_coeffs); |
| 150 | } |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 151 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 152 | /* must be called with hw device and output stream mutexes locked */ |
| 153 | static int start_output_stream(struct alsa_stream_out *out) |
| 154 | { |
| 155 | struct alsa_audio_device *adev = out->dev; |
| 156 | |
| 157 | /* default to low power: will be corrected in out_write if necessary before first write to |
| 158 | * tinyalsa. |
| 159 | */ |
| 160 | out->write_threshold = PLAYBACK_PERIOD_COUNT * PLAYBACK_PERIOD_SIZE; |
| 161 | out->config.start_threshold = PLAYBACK_PERIOD_START_THRESHOLD * PLAYBACK_PERIOD_SIZE; |
| 162 | out->config.avail_min = PLAYBACK_PERIOD_SIZE; |
| 163 | out->unavailable = true; |
| 164 | unsigned int pcm_retry_count = PCM_OPEN_RETRIES; |
| 165 | int out_port = get_audio_output_port(out->devices); |
| 166 | |
| 167 | while (1) { |
| 168 | out->pcm = pcm_open(CARD_OUT, out_port, PCM_OUT | PCM_MONOTONIC, &out->config); |
| 169 | if ((out->pcm != NULL) && pcm_is_ready(out->pcm)) { |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 170 | break; |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 171 | } else { |
| 172 | ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm)); |
| 173 | if (out->pcm != NULL) { |
| 174 | pcm_close(out->pcm); |
| 175 | out->pcm = NULL; |
| 176 | } |
| 177 | if (--pcm_retry_count == 0) { |
| 178 | ALOGE("Failed to open pcm_out after %d tries", PCM_OPEN_RETRIES); |
| 179 | return -ENODEV; |
| 180 | } |
| 181 | usleep(PCM_OPEN_WAIT_TIME_MS * 1000); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 182 | } |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 183 | } |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 184 | out->unavailable = false; |
| 185 | adev->active_output = out; |
| 186 | return 0; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 187 | } |
| 188 | |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 189 | static uint32_t out_get_sample_rate(const struct audio_stream *stream) |
| 190 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 191 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 192 | return out->config.rate; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 196 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 197 | ALOGV("out_set_sample_rate: %d", 0); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 198 | return -ENOSYS; |
| 199 | } |
| 200 | |
| 201 | static size_t out_get_buffer_size(const struct audio_stream *stream) |
| 202 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 203 | ALOGV("out_get_buffer_size: %d", 4096); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 204 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 205 | /* return the closest majoring multiple of 16 frames, as |
| 206 | * audioflinger expects audio buffers to be a multiple of 16 frames */ |
| 207 | size_t size = PLAYBACK_PERIOD_SIZE; |
| 208 | size = ((size + 15) / 16) * 16; |
| 209 | return size * audio_stream_out_frame_size((struct audio_stream_out *)stream); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static audio_channel_mask_t out_get_channels(const struct audio_stream *stream) |
| 213 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 214 | ALOGV("out_get_channels"); |
| 215 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 216 | return audio_channel_out_mask_from_count(out->config.channels); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | static audio_format_t out_get_format(const struct audio_stream *stream) |
| 220 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 221 | ALOGV("out_get_format"); |
| 222 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 223 | return audio_format_from_pcm_format(out->config.format); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static int out_set_format(struct audio_stream *stream, audio_format_t format) |
| 227 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 228 | ALOGV("out_set_format: %d",format); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 229 | return -ENOSYS; |
| 230 | } |
| 231 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 232 | static int do_output_standby(struct alsa_stream_out *out) |
| 233 | { |
| 234 | struct alsa_audio_device *adev = out->dev; |
| 235 | |
| 236 | fir_reset(out->speaker_eq); |
| 237 | |
| 238 | if (!out->standby) { |
| 239 | pcm_close(out->pcm); |
| 240 | out->pcm = NULL; |
| 241 | adev->active_output = NULL; |
| 242 | out->standby = 1; |
| 243 | } |
| 244 | aec_set_spk_running(adev->aec, false); |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | static int out_standby(struct audio_stream *stream) |
| 249 | { |
| 250 | ALOGV("out_standby"); |
| 251 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 252 | int status; |
| 253 | |
| 254 | pthread_mutex_lock(&out->dev->lock); |
| 255 | pthread_mutex_lock(&out->lock); |
| 256 | status = do_output_standby(out); |
| 257 | pthread_mutex_unlock(&out->lock); |
| 258 | pthread_mutex_unlock(&out->dev->lock); |
| 259 | return status; |
| 260 | } |
| 261 | |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 262 | static int out_dump(const struct audio_stream *stream, int fd) |
| 263 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 264 | ALOGV("out_dump"); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 269 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 270 | ALOGV("out_set_parameters"); |
| 271 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 272 | struct alsa_audio_device *adev = out->dev; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 273 | struct str_parms *parms; |
| 274 | char value[32]; |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 275 | int ret, val = 0; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 276 | |
| 277 | parms = str_parms_create_str(kvpairs); |
| 278 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 279 | ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value)); |
| 280 | if (ret >= 0) { |
| 281 | val = atoi(value); |
| 282 | pthread_mutex_lock(&adev->lock); |
| 283 | pthread_mutex_lock(&out->lock); |
| 284 | if (((out->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) { |
| 285 | out->devices &= ~AUDIO_DEVICE_OUT_ALL; |
| 286 | out->devices |= val; |
| 287 | } |
| 288 | pthread_mutex_unlock(&out->lock); |
| 289 | pthread_mutex_unlock(&adev->lock); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | str_parms_destroy(parms); |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 293 | return 0; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static char * out_get_parameters(const struct audio_stream *stream, const char *keys) |
| 297 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 298 | ALOGV("out_get_parameters"); |
| 299 | return strdup(""); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static uint32_t out_get_latency(const struct audio_stream_out *stream) |
| 303 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 304 | ALOGV("out_get_latency"); |
| 305 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 306 | return (PLAYBACK_PERIOD_SIZE * PLAYBACK_PERIOD_COUNT * 1000) / out->config.rate; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | static int out_set_volume(struct audio_stream_out *stream, float left, |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 310 | float right) |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 311 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 312 | ALOGV("out_set_volume: Left:%f Right:%f", left, right); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 313 | return -ENOSYS; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 314 | } |
| 315 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 316 | static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, |
| 317 | size_t bytes) |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 318 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 319 | int ret; |
| 320 | struct alsa_stream_out *out = (struct alsa_stream_out *)stream; |
| 321 | struct alsa_audio_device *adev = out->dev; |
| 322 | size_t frame_size = audio_stream_out_frame_size(stream); |
| 323 | size_t out_frames = bytes / frame_size; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 324 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 325 | ALOGV("%s: devices: %d, bytes %zu", __func__, out->devices, bytes); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 326 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 327 | /* acquiring hw device mutex systematically is useful if a low priority thread is waiting |
| 328 | * on the output stream mutex - e.g. executing select_mode() while holding the hw device |
| 329 | * mutex |
| 330 | */ |
| 331 | pthread_mutex_lock(&adev->lock); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 332 | pthread_mutex_lock(&out->lock); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 333 | if (out->standby) { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 334 | ret = start_output_stream(out); |
| 335 | if (ret != 0) { |
| 336 | pthread_mutex_unlock(&adev->lock); |
| 337 | goto exit; |
| 338 | } |
| 339 | out->standby = 0; |
| 340 | aec_set_spk_running(adev->aec, true); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 341 | } |
| 342 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 343 | pthread_mutex_unlock(&adev->lock); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 344 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 345 | if (out->speaker_eq != NULL) { |
| 346 | fir_process_interleaved(out->speaker_eq, (int16_t*)buffer, (int16_t*)buffer, out_frames); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 347 | } |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 348 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 349 | ret = pcm_write(out->pcm, buffer, out_frames * frame_size); |
| 350 | if (ret == 0) { |
| 351 | out->frames_written += out_frames; |
| 352 | |
| 353 | struct aec_info info; |
| 354 | get_pcm_timestamp(out->pcm, out->config.rate, &info, true /*isOutput*/); |
| 355 | out->timestamp = info.timestamp; |
| 356 | info.bytes = out_frames * frame_size; |
| 357 | int aec_ret = write_to_reference_fifo(adev->aec, (void *)buffer, &info); |
| 358 | if (aec_ret) { |
| 359 | ALOGE("AEC: Write to speaker loopback FIFO failed!"); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | exit: |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 364 | pthread_mutex_unlock(&out->lock); |
| 365 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 366 | if (ret != 0) { |
| 367 | usleep((int64_t)bytes * 1000000 / audio_stream_out_frame_size(stream) / |
| 368 | out_get_sample_rate(&stream->common)); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 369 | } |
| 370 | |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 371 | return bytes; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 372 | } |
| 373 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 374 | static int out_get_render_position(const struct audio_stream_out *stream, |
| 375 | uint32_t *dsp_frames) |
| 376 | { |
| 377 | ALOGV("out_get_render_position: dsp_frames: %p", dsp_frames); |
| 378 | return -ENOSYS; |
| 379 | } |
| 380 | |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 381 | static int out_get_presentation_position(const struct audio_stream_out *stream, |
| 382 | uint64_t *frames, struct timespec *timestamp) |
| 383 | { |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 384 | if (stream == NULL || frames == NULL || timestamp == NULL) { |
| 385 | return -EINVAL; |
| 386 | } |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 387 | struct alsa_stream_out* out = (struct alsa_stream_out*)stream; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 388 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 389 | *frames = out->frames_written; |
| 390 | *timestamp = out->timestamp; |
| 391 | ALOGV("%s: frames: %" PRIu64 ", timestamp (nsec): %" PRIu64, __func__, *frames, |
| 392 | audio_utils_ns_from_timespec(timestamp)); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 393 | |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 394 | return 0; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | |
| 398 | static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 399 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 400 | ALOGV("out_add_audio_effect: %p", effect); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 405 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 406 | ALOGV("out_remove_audio_effect: %p", effect); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | static int out_get_next_write_timestamp(const struct audio_stream_out *stream, |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 411 | int64_t *timestamp) |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 412 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 413 | *timestamp = 0; |
| 414 | ALOGV("out_get_next_write_timestamp: %ld", (long int)(*timestamp)); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 415 | return -ENOSYS; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 416 | } |
| 417 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 418 | /** audio_stream_in implementation **/ |
| 419 | |
| 420 | /* must be called with hw device and input stream mutexes locked */ |
| 421 | static int start_input_stream(struct alsa_stream_in *in) |
| 422 | { |
| 423 | struct alsa_audio_device *adev = in->dev; |
| 424 | in->unavailable = true; |
| 425 | unsigned int pcm_retry_count = PCM_OPEN_RETRIES; |
| 426 | |
| 427 | while (1) { |
| 428 | in->pcm = pcm_open(CARD_IN, PORT_BUILTIN_MIC, PCM_IN | PCM_MONOTONIC, &in->config); |
| 429 | if ((in->pcm != NULL) && pcm_is_ready(in->pcm)) { |
| 430 | break; |
| 431 | } else { |
| 432 | ALOGE("cannot open pcm_in driver: %s", pcm_get_error(in->pcm)); |
| 433 | if (in->pcm != NULL) { |
| 434 | pcm_close(in->pcm); |
| 435 | in->pcm = NULL; |
| 436 | } |
| 437 | if (--pcm_retry_count == 0) { |
| 438 | ALOGE("Failed to open pcm_in after %d tries", PCM_OPEN_RETRIES); |
| 439 | return -ENODEV; |
| 440 | } |
| 441 | usleep(PCM_OPEN_WAIT_TIME_MS * 1000); |
| 442 | } |
| 443 | } |
| 444 | in->unavailable = false; |
| 445 | adev->active_input = in; |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | static void get_mic_characteristics(struct audio_microphone_characteristic_t* mic_data, |
| 450 | size_t* mic_count) { |
| 451 | *mic_count = 1; |
| 452 | memset(mic_data, 0, sizeof(struct audio_microphone_characteristic_t)); |
| 453 | strlcpy(mic_data->device_id, "builtin_mic", AUDIO_MICROPHONE_ID_MAX_LEN - 1); |
| 454 | strlcpy(mic_data->address, "top", AUDIO_DEVICE_MAX_ADDRESS_LEN - 1); |
| 455 | memset(mic_data->channel_mapping, AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED, |
| 456 | sizeof(mic_data->channel_mapping)); |
| 457 | mic_data->device = AUDIO_DEVICE_IN_BUILTIN_MIC; |
| 458 | mic_data->sensitivity = -37.0; |
| 459 | mic_data->max_spl = AUDIO_MICROPHONE_SPL_UNKNOWN; |
| 460 | mic_data->min_spl = AUDIO_MICROPHONE_SPL_UNKNOWN; |
| 461 | mic_data->orientation.x = 0.0f; |
| 462 | mic_data->orientation.y = 0.0f; |
| 463 | mic_data->orientation.z = 0.0f; |
| 464 | mic_data->geometric_location.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN; |
| 465 | mic_data->geometric_location.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN; |
| 466 | mic_data->geometric_location.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN; |
| 467 | } |
| 468 | |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 469 | static uint32_t in_get_sample_rate(const struct audio_stream *stream) |
| 470 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 471 | struct alsa_stream_in *in = (struct alsa_stream_in *)stream; |
| 472 | return in->config.rate; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) |
| 476 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 477 | ALOGV("in_set_sample_rate: %d", rate); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 478 | return -ENOSYS; |
| 479 | } |
| 480 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 481 | static size_t get_input_buffer_size(size_t frames, audio_format_t format, |
| 482 | audio_channel_mask_t channel_mask) { |
| 483 | /* return the closest majoring multiple of 16 frames, as |
| 484 | * audioflinger expects audio buffers to be a multiple of 16 frames */ |
| 485 | frames = ((frames + 15) / 16) * 16; |
| 486 | size_t bytes_per_frame = audio_channel_count_from_in_mask(channel_mask) * |
| 487 | audio_bytes_per_sample(format); |
| 488 | size_t buffer_size = frames * bytes_per_frame; |
| 489 | return buffer_size; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | static audio_channel_mask_t in_get_channels(const struct audio_stream *stream) |
| 493 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 494 | struct alsa_stream_in *in = (struct alsa_stream_in *)stream; |
| 495 | ALOGV("in_get_channels: %d", in->config.channels); |
| 496 | return audio_channel_in_mask_from_count(in->config.channels); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static audio_format_t in_get_format(const struct audio_stream *stream) |
| 500 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 501 | struct alsa_stream_in *in = (struct alsa_stream_in *)stream; |
| 502 | ALOGV("in_get_format: %d", in->config.format); |
| 503 | return audio_format_from_pcm_format(in->config.format); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static int in_set_format(struct audio_stream *stream, audio_format_t format) |
| 507 | { |
| 508 | return -ENOSYS; |
| 509 | } |
| 510 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 511 | static size_t in_get_buffer_size(const struct audio_stream *stream) |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 512 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 513 | struct alsa_stream_in* in = (struct alsa_stream_in*)stream; |
| 514 | size_t frames = CAPTURE_PERIOD_SIZE; |
| 515 | if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) { |
| 516 | frames = CAPTURE_PERIOD_SIZE * PLAYBACK_CODEC_SAMPLING_RATE / CAPTURE_CODEC_SAMPLING_RATE; |
| 517 | } |
| 518 | |
| 519 | size_t buffer_size = |
| 520 | get_input_buffer_size(frames, stream->get_format(stream), stream->get_channels(stream)); |
| 521 | ALOGV("in_get_buffer_size: %zu", buffer_size); |
| 522 | return buffer_size; |
| 523 | } |
| 524 | |
| 525 | static int in_get_active_microphones(const struct audio_stream_in* stream, |
| 526 | struct audio_microphone_characteristic_t* mic_array, |
| 527 | size_t* mic_count) { |
| 528 | ALOGV("in_get_active_microphones"); |
| 529 | if ((mic_array == NULL) || (mic_count == NULL)) { |
| 530 | return -EINVAL; |
| 531 | } |
| 532 | struct alsa_stream_in* in = (struct alsa_stream_in*)stream; |
| 533 | struct audio_hw_device* dev = (struct audio_hw_device*)in->dev; |
| 534 | bool mic_muted = false; |
| 535 | adev_get_mic_mute(dev, &mic_muted); |
| 536 | if ((in->source == AUDIO_SOURCE_ECHO_REFERENCE) || mic_muted) { |
| 537 | *mic_count = 0; |
| 538 | return 0; |
| 539 | } |
| 540 | adev_get_microphones(dev, mic_array, mic_count); |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static int do_input_standby(struct alsa_stream_in *in) |
| 545 | { |
| 546 | struct alsa_audio_device *adev = in->dev; |
| 547 | |
| 548 | if (!in->standby) { |
| 549 | pcm_close(in->pcm); |
| 550 | in->pcm = NULL; |
| 551 | adev->active_input = NULL; |
| 552 | in->standby = true; |
| 553 | } |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | static int in_standby(struct audio_stream *stream) |
| 558 | { |
| 559 | struct alsa_stream_in *in = (struct alsa_stream_in *)stream; |
| 560 | int status; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 561 | |
| 562 | pthread_mutex_lock(&in->lock); |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 563 | pthread_mutex_lock(&in->dev->lock); |
| 564 | status = do_input_standby(in); |
| 565 | pthread_mutex_unlock(&in->dev->lock); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 566 | pthread_mutex_unlock(&in->lock); |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 567 | return status; |
| 568 | } |
| 569 | |
| 570 | static int in_dump(const struct audio_stream *stream, int fd) |
| 571 | { |
| 572 | struct alsa_stream_in* in = (struct alsa_stream_in*)stream; |
| 573 | if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) { |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | struct audio_microphone_characteristic_t mic_array[AUDIO_MICROPHONE_MAX_COUNT]; |
| 578 | size_t mic_count; |
| 579 | |
| 580 | get_mic_characteristics(mic_array, &mic_count); |
| 581 | |
| 582 | dprintf(fd, " Microphone count: %zd\n", mic_count); |
| 583 | size_t idx; |
| 584 | for (idx = 0; idx < mic_count; idx++) { |
| 585 | dprintf(fd, " Microphone: %zd\n", idx); |
| 586 | dprintf(fd, " Address: %s\n", mic_array[idx].address); |
| 587 | dprintf(fd, " Device: %d\n", mic_array[idx].device); |
| 588 | dprintf(fd, " Sensitivity (dB): %.2f\n", mic_array[idx].sensitivity); |
| 589 | } |
| 590 | |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static int in_set_parameters(struct audio_stream *stream, const char *kvpairs) |
| 595 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 596 | return 0; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | static char * in_get_parameters(const struct audio_stream *stream, |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 600 | const char *keys) |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 601 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 602 | return strdup(""); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static int in_set_gain(struct audio_stream_in *stream, float gain) |
| 606 | { |
| 607 | return 0; |
| 608 | } |
| 609 | |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 610 | static ssize_t in_read(struct audio_stream_in *stream, void* buffer, |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 611 | size_t bytes) |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 612 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 613 | int ret; |
| 614 | struct alsa_stream_in *in = (struct alsa_stream_in *)stream; |
| 615 | struct alsa_audio_device *adev = in->dev; |
| 616 | size_t frame_size = audio_stream_in_frame_size(stream); |
| 617 | size_t in_frames = bytes / frame_size; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 618 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 619 | ALOGV("in_read: stream: %d, bytes %zu", in->source, bytes); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 620 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 621 | /* Special handling for Echo Reference: simply get the reference from FIFO. |
| 622 | * The format and sample rate should be specified by arguments to adev_open_input_stream. */ |
| 623 | if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) { |
| 624 | struct aec_info info; |
| 625 | info.bytes = bytes; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 626 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 627 | const uint64_t time_increment_nsec = (uint64_t)bytes * NANOS_PER_SECOND / |
| 628 | audio_stream_in_frame_size(stream) / |
| 629 | in_get_sample_rate(&stream->common); |
| 630 | if (!aec_get_spk_running(adev->aec)) { |
| 631 | if (in->timestamp_nsec == 0) { |
| 632 | struct timespec now; |
| 633 | clock_gettime(CLOCK_MONOTONIC, &now); |
| 634 | const uint64_t timestamp_nsec = audio_utils_ns_from_timespec(&now); |
| 635 | in->timestamp_nsec = timestamp_nsec; |
| 636 | } else { |
| 637 | in->timestamp_nsec += time_increment_nsec; |
| 638 | } |
| 639 | memset(buffer, 0, bytes); |
| 640 | const uint64_t time_increment_usec = time_increment_nsec / 1000; |
| 641 | usleep(time_increment_usec); |
| 642 | } else { |
| 643 | int ref_ret = get_reference_samples(adev->aec, buffer, &info); |
| 644 | if ((ref_ret) || (info.timestamp_usec == 0)) { |
| 645 | memset(buffer, 0, bytes); |
| 646 | in->timestamp_nsec += time_increment_nsec; |
| 647 | } else { |
| 648 | in->timestamp_nsec = 1000 * info.timestamp_usec; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 649 | } |
| 650 | } |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 651 | in->frames_read += in_frames; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 652 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 653 | #if DEBUG_AEC |
| 654 | FILE* fp_ref = fopen("/data/local/traces/aec_ref.pcm", "a+"); |
| 655 | if (fp_ref) { |
| 656 | fwrite((char*)buffer, 1, bytes, fp_ref); |
| 657 | fclose(fp_ref); |
| 658 | } else { |
| 659 | ALOGE("AEC debug: Could not open file aec_ref.pcm!"); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 660 | } |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 661 | FILE* fp_ref_ts = fopen("/data/local/traces/aec_ref_timestamps.txt", "a+"); |
| 662 | if (fp_ref_ts) { |
| 663 | fprintf(fp_ref_ts, "%" PRIu64 "\n", in->timestamp_nsec); |
| 664 | fclose(fp_ref_ts); |
| 665 | } else { |
| 666 | ALOGE("AEC debug: Could not open file aec_ref_timestamps.txt!"); |
| 667 | } |
| 668 | #endif |
| 669 | return info.bytes; |
| 670 | } |
| 671 | |
| 672 | /* Microphone input stream read */ |
| 673 | |
| 674 | /* acquiring hw device mutex systematically is useful if a low priority thread is waiting |
| 675 | * on the input stream mutex - e.g. executing select_mode() while holding the hw device |
| 676 | * mutex |
| 677 | */ |
| 678 | pthread_mutex_lock(&in->lock); |
| 679 | pthread_mutex_lock(&adev->lock); |
| 680 | if (in->standby) { |
| 681 | ret = start_input_stream(in); |
| 682 | if (ret != 0) { |
| 683 | pthread_mutex_unlock(&adev->lock); |
| 684 | ALOGE("start_input_stream failed with code %d", ret); |
| 685 | goto exit; |
| 686 | } |
| 687 | in->standby = false; |
| 688 | } |
| 689 | |
| 690 | pthread_mutex_unlock(&adev->lock); |
| 691 | |
| 692 | ret = pcm_read(in->pcm, buffer, in_frames * frame_size); |
| 693 | struct aec_info info; |
| 694 | get_pcm_timestamp(in->pcm, in->config.rate, &info, false /*isOutput*/); |
| 695 | if (ret == 0) { |
| 696 | in->frames_read += in_frames; |
| 697 | in->timestamp_nsec = audio_utils_ns_from_timespec(&info.timestamp); |
| 698 | } |
| 699 | else { |
| 700 | ALOGE("pcm_read failed with code %d", ret); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | exit: |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 704 | pthread_mutex_unlock(&in->lock); |
| 705 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 706 | bool mic_muted = false; |
| 707 | adev_get_mic_mute((struct audio_hw_device*)adev, &mic_muted); |
| 708 | if (mic_muted) { |
| 709 | memset(buffer, 0, bytes); |
| 710 | } |
| 711 | |
| 712 | if (ret != 0) { |
| 713 | usleep((int64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) / |
| 714 | in_get_sample_rate(&stream->common)); |
| 715 | } else { |
| 716 | /* Process AEC if available */ |
| 717 | /* TODO move to a separate thread */ |
| 718 | if (!mic_muted) { |
| 719 | info.bytes = bytes; |
| 720 | int aec_ret = process_aec(adev->aec, buffer, &info); |
| 721 | if (aec_ret) { |
| 722 | ALOGE("process_aec returned error code %d", aec_ret); |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | #if DEBUG_AEC && !defined(AEC_HAL) |
| 728 | FILE* fp_in = fopen("/data/local/traces/aec_in.pcm", "a+"); |
| 729 | if (fp_in) { |
| 730 | fwrite((char*)buffer, 1, bytes, fp_in); |
| 731 | fclose(fp_in); |
| 732 | } else { |
| 733 | ALOGE("AEC debug: Could not open file aec_in.pcm!"); |
| 734 | } |
| 735 | FILE* fp_mic_ts = fopen("/data/local/traces/aec_in_timestamps.txt", "a+"); |
| 736 | if (fp_mic_ts) { |
| 737 | fprintf(fp_mic_ts, "%" PRIu64 "\n", in->timestamp_nsec); |
| 738 | fclose(fp_mic_ts); |
| 739 | } else { |
| 740 | ALOGE("AEC debug: Could not open file aec_in_timestamps.txt!"); |
| 741 | } |
| 742 | #endif |
| 743 | |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 744 | return bytes; |
| 745 | } |
| 746 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 747 | static int in_get_capture_position(const struct audio_stream_in* stream, int64_t* frames, |
| 748 | int64_t* time) { |
| 749 | if (stream == NULL || frames == NULL || time == NULL) { |
| 750 | return -EINVAL; |
| 751 | } |
| 752 | struct alsa_stream_in* in = (struct alsa_stream_in*)stream; |
| 753 | |
| 754 | *frames = in->frames_read; |
| 755 | *time = in->timestamp_nsec; |
| 756 | ALOGV("%s: source: %d, timestamp (nsec): %" PRIu64, __func__, in->source, *time); |
| 757 | |
| 758 | return 0; |
| 759 | } |
| 760 | |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 761 | static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) |
| 762 | { |
| 763 | return 0; |
| 764 | } |
| 765 | |
| 766 | static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 767 | { |
| 768 | return 0; |
| 769 | } |
| 770 | |
| 771 | static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect) |
| 772 | { |
| 773 | return 0; |
| 774 | } |
| 775 | |
| 776 | static int adev_open_output_stream(struct audio_hw_device *dev, |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 777 | audio_io_handle_t handle, |
| 778 | audio_devices_t devices, |
| 779 | audio_output_flags_t flags, |
| 780 | struct audio_config *config, |
| 781 | struct audio_stream_out **stream_out, |
| 782 | const char *address __unused) |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 783 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 784 | ALOGV("adev_open_output_stream..."); |
| 785 | |
| 786 | struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev; |
| 787 | struct alsa_stream_out *out; |
| 788 | struct pcm_params *params; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 789 | int ret = 0; |
| 790 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 791 | int out_port = get_audio_output_port(devices); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 792 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 793 | params = pcm_params_get(CARD_OUT, out_port, PCM_OUT); |
| 794 | if (!params) |
| 795 | return -ENOSYS; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 796 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 797 | out = (struct alsa_stream_out *)calloc(1, sizeof(struct alsa_stream_out)); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 798 | if (!out) |
| 799 | return -ENOMEM; |
| 800 | |
| 801 | out->stream.common.get_sample_rate = out_get_sample_rate; |
| 802 | out->stream.common.set_sample_rate = out_set_sample_rate; |
| 803 | out->stream.common.get_buffer_size = out_get_buffer_size; |
| 804 | out->stream.common.get_channels = out_get_channels; |
| 805 | out->stream.common.get_format = out_get_format; |
| 806 | out->stream.common.set_format = out_set_format; |
| 807 | out->stream.common.standby = out_standby; |
| 808 | out->stream.common.dump = out_dump; |
| 809 | out->stream.common.set_parameters = out_set_parameters; |
| 810 | out->stream.common.get_parameters = out_get_parameters; |
| 811 | out->stream.common.add_audio_effect = out_add_audio_effect; |
| 812 | out->stream.common.remove_audio_effect = out_remove_audio_effect; |
| 813 | out->stream.get_latency = out_get_latency; |
| 814 | out->stream.set_volume = out_set_volume; |
| 815 | out->stream.write = out_write; |
| 816 | out->stream.get_render_position = out_get_render_position; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 817 | out->stream.get_next_write_timestamp = out_get_next_write_timestamp; |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 818 | out->stream.get_presentation_position = out_get_presentation_position; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 819 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 820 | out->config.channels = CHANNEL_STEREO; |
| 821 | out->config.rate = PLAYBACK_CODEC_SAMPLING_RATE; |
| 822 | out->config.format = PCM_FORMAT_S16_LE; |
| 823 | out->config.period_size = PLAYBACK_PERIOD_SIZE; |
| 824 | out->config.period_count = PLAYBACK_PERIOD_COUNT; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 825 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 826 | if (out->config.rate != config->sample_rate || |
| 827 | audio_channel_count_from_out_mask(config->channel_mask) != CHANNEL_STEREO || |
| 828 | out->config.format != pcm_format_from_audio_format(config->format) ) { |
| 829 | config->sample_rate = out->config.rate; |
| 830 | config->format = audio_format_from_pcm_format(out->config.format); |
| 831 | config->channel_mask = audio_channel_out_mask_from_count(CHANNEL_STEREO); |
| 832 | ret = -EINVAL; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 833 | } |
| 834 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 835 | ALOGI("adev_open_output_stream selects channels=%d rate=%d format=%d, devices=%d", |
| 836 | out->config.channels, out->config.rate, out->config.format, devices); |
| 837 | |
| 838 | out->dev = ladev; |
| 839 | out->standby = 1; |
| 840 | out->unavailable = false; |
| 841 | out->devices = devices; |
| 842 | |
| 843 | config->format = out_get_format(&out->stream.common); |
| 844 | config->channel_mask = out_get_channels(&out->stream.common); |
| 845 | config->sample_rate = out_get_sample_rate(&out->stream.common); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 846 | |
| 847 | *stream_out = &out->stream; |
| 848 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 849 | out->speaker_eq = NULL; |
| 850 | if (out_port == PORT_INTERNAL_SPEAKER) { |
| 851 | out_set_eq(out); |
| 852 | if (out->speaker_eq == NULL) { |
| 853 | ALOGE("%s: Failed to initialize speaker EQ", __func__); |
| 854 | } |
| 855 | } |
| 856 | |
| 857 | /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */ |
| 858 | ret = 0; |
| 859 | |
| 860 | if (ret == 0) { |
| 861 | int aec_ret = init_aec_reference_config(ladev->aec, out); |
| 862 | if (aec_ret) { |
| 863 | ALOGE("AEC: Speaker config init failed!"); |
| 864 | return -EINVAL; |
| 865 | } |
| 866 | } |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 867 | |
| 868 | return ret; |
| 869 | } |
| 870 | |
| 871 | static void adev_close_output_stream(struct audio_hw_device *dev, |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 872 | struct audio_stream_out *stream) |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 873 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 874 | ALOGV("adev_close_output_stream..."); |
| 875 | struct alsa_audio_device *adev = (struct alsa_audio_device *)dev; |
| 876 | destroy_aec_reference_config(adev->aec); |
| 877 | struct alsa_stream_out* out = (struct alsa_stream_out*)stream; |
| 878 | fir_release(out->speaker_eq); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 879 | free(stream); |
| 880 | } |
| 881 | |
| 882 | static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs) |
| 883 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 884 | ALOGV("adev_set_parameters"); |
| 885 | return -ENOSYS; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | static char * adev_get_parameters(const struct audio_hw_device *dev, |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 889 | const char *keys) |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 890 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 891 | ALOGV("adev_get_parameters"); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 892 | return strdup(""); |
| 893 | } |
| 894 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 895 | static int adev_get_microphones(const struct audio_hw_device* dev, |
| 896 | struct audio_microphone_characteristic_t* mic_array, |
| 897 | size_t* mic_count) { |
| 898 | ALOGV("adev_get_microphones"); |
| 899 | if ((mic_array == NULL) || (mic_count == NULL)) { |
| 900 | return -EINVAL; |
| 901 | } |
| 902 | get_mic_characteristics(mic_array, mic_count); |
| 903 | return 0; |
| 904 | } |
| 905 | |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 906 | static int adev_init_check(const struct audio_hw_device *dev) |
| 907 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 908 | ALOGV("adev_init_check"); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 909 | return 0; |
| 910 | } |
| 911 | |
| 912 | static int adev_set_voice_volume(struct audio_hw_device *dev, float volume) |
| 913 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 914 | ALOGV("adev_set_voice_volume: %f", volume); |
| 915 | return -ENOSYS; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | static int adev_set_master_volume(struct audio_hw_device *dev, float volume) |
| 919 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 920 | ALOGV("adev_set_master_volume: %f", volume); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 921 | return -ENOSYS; |
| 922 | } |
| 923 | |
| 924 | static int adev_get_master_volume(struct audio_hw_device *dev, float *volume) |
| 925 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 926 | ALOGV("adev_get_master_volume: %f", *volume); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 927 | return -ENOSYS; |
| 928 | } |
| 929 | |
| 930 | static int adev_set_master_mute(struct audio_hw_device *dev, bool muted) |
| 931 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 932 | ALOGV("adev_set_master_mute: %d", muted); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 933 | return -ENOSYS; |
| 934 | } |
| 935 | |
| 936 | static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted) |
| 937 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 938 | ALOGV("adev_get_master_mute: %d", *muted); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 939 | return -ENOSYS; |
| 940 | } |
| 941 | |
| 942 | static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) |
| 943 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 944 | ALOGV("adev_set_mode: %d", mode); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) |
| 949 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 950 | ALOGV("adev_set_mic_mute: %d",state); |
| 951 | struct alsa_audio_device *adev = (struct alsa_audio_device *)dev; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 952 | pthread_mutex_lock(&adev->lock); |
| 953 | adev->mic_mute = state; |
| 954 | pthread_mutex_unlock(&adev->lock); |
| 955 | return 0; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) |
| 959 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 960 | ALOGV("adev_get_mic_mute"); |
| 961 | struct alsa_audio_device *adev = (struct alsa_audio_device *)dev; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 962 | pthread_mutex_lock(&adev->lock); |
| 963 | *state = adev->mic_mute; |
| 964 | pthread_mutex_unlock(&adev->lock); |
| 965 | return 0; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 969 | const struct audio_config *config) |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 970 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 971 | size_t buffer_size = |
| 972 | get_input_buffer_size(CAPTURE_PERIOD_SIZE, config->format, config->channel_mask); |
| 973 | ALOGV("adev_get_input_buffer_size: %zu", buffer_size); |
| 974 | return buffer_size; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 975 | } |
| 976 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 977 | static int adev_open_input_stream(struct audio_hw_device* dev, audio_io_handle_t handle, |
| 978 | audio_devices_t devices, struct audio_config* config, |
| 979 | struct audio_stream_in** stream_in, |
| 980 | audio_input_flags_t flags __unused, const char* address __unused, |
| 981 | audio_source_t source) { |
| 982 | ALOGV("adev_open_input_stream..."); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 983 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 984 | struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev; |
| 985 | struct alsa_stream_in *in; |
| 986 | struct pcm_params *params; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 987 | int ret = 0; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 988 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 989 | params = pcm_params_get(CARD_IN, PORT_BUILTIN_MIC, PCM_IN); |
| 990 | if (!params) |
| 991 | return -ENOSYS; |
| 992 | |
| 993 | in = (struct alsa_stream_in *)calloc(1, sizeof(struct alsa_stream_in)); |
| 994 | if (!in) |
| 995 | return -ENOMEM; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 996 | |
| 997 | in->stream.common.get_sample_rate = in_get_sample_rate; |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 998 | in->stream.common.set_sample_rate = in_set_sample_rate; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 999 | in->stream.common.get_buffer_size = in_get_buffer_size; |
| 1000 | in->stream.common.get_channels = in_get_channels; |
| 1001 | in->stream.common.get_format = in_get_format; |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1002 | in->stream.common.set_format = in_set_format; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 1003 | in->stream.common.standby = in_standby; |
| 1004 | in->stream.common.dump = in_dump; |
| 1005 | in->stream.common.set_parameters = in_set_parameters; |
| 1006 | in->stream.common.get_parameters = in_get_parameters; |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1007 | in->stream.common.add_audio_effect = in_add_audio_effect; |
| 1008 | in->stream.common.remove_audio_effect = in_remove_audio_effect; |
| 1009 | in->stream.set_gain = in_set_gain; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 1010 | in->stream.read = in_read; |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1011 | in->stream.get_input_frames_lost = in_get_input_frames_lost; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1012 | in->stream.get_capture_position = in_get_capture_position; |
| 1013 | in->stream.get_active_microphones = in_get_active_microphones; |
| 1014 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1015 | in->config.channels = CHANNEL_STEREO; |
| 1016 | if (source == AUDIO_SOURCE_ECHO_REFERENCE) { |
| 1017 | in->config.rate = PLAYBACK_CODEC_SAMPLING_RATE; |
| 1018 | } else { |
| 1019 | in->config.rate = CAPTURE_CODEC_SAMPLING_RATE; |
| 1020 | } |
| 1021 | in->config.format = PCM_FORMAT_S32_LE; |
| 1022 | in->config.period_size = CAPTURE_PERIOD_SIZE; |
| 1023 | in->config.period_count = CAPTURE_PERIOD_COUNT; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1024 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1025 | if (in->config.rate != config->sample_rate || |
| 1026 | audio_channel_count_from_in_mask(config->channel_mask) != CHANNEL_STEREO || |
| 1027 | in->config.format != pcm_format_from_audio_format(config->format) ) { |
| 1028 | ret = -EINVAL; |
| 1029 | } |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1030 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1031 | ALOGI("adev_open_input_stream selects channels=%d rate=%d format=%d source=%d", |
| 1032 | in->config.channels, in->config.rate, in->config.format, source); |
| 1033 | |
| 1034 | in->dev = ladev; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1035 | in->standby = true; |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1036 | in->unavailable = false; |
| 1037 | in->source = source; |
| 1038 | in->devices = devices; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1039 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1040 | config->format = in_get_format(&in->stream.common); |
| 1041 | config->channel_mask = in_get_channels(&in->stream.common); |
| 1042 | config->sample_rate = in_get_sample_rate(&in->stream.common); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1043 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1044 | /* If AEC is in the app, only configure based on ECHO_REFERENCE spec. |
| 1045 | * If AEC is in the HAL, configure using the given mic stream. */ |
| 1046 | bool aecInput = true; |
| 1047 | #if !defined(AEC_HAL) |
| 1048 | aecInput = (in->source == AUDIO_SOURCE_ECHO_REFERENCE); |
| 1049 | #endif |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 1050 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1051 | if ((ret == 0) && aecInput) { |
| 1052 | int aec_ret = init_aec_mic_config(ladev->aec, in); |
| 1053 | if (aec_ret) { |
| 1054 | ALOGE("AEC: Mic config init failed!"); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1055 | return -EINVAL; |
| 1056 | } |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | if (ret) { |
| 1060 | free(in); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1061 | } else { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1062 | *stream_in = &in->stream; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1063 | } |
| 1064 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1065 | #if DEBUG_AEC |
| 1066 | remove("/data/local/traces/aec_ref.pcm"); |
| 1067 | remove("/data/local/traces/aec_in.pcm"); |
| 1068 | remove("/data/local/traces/aec_ref_timestamps.txt"); |
| 1069 | remove("/data/local/traces/aec_in_timestamps.txt"); |
| 1070 | #endif |
| 1071 | return ret; |
| 1072 | } |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1073 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1074 | static void adev_close_input_stream(struct audio_hw_device *dev, |
| 1075 | struct audio_stream_in *stream) |
| 1076 | { |
| 1077 | ALOGV("adev_close_input_stream..."); |
| 1078 | struct alsa_audio_device *adev = (struct alsa_audio_device *)dev; |
| 1079 | destroy_aec_mic_config(adev->aec); |
| 1080 | free(stream); |
| 1081 | return; |
| 1082 | } |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1083 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1084 | static int adev_dump(const audio_hw_device_t *device, int fd) |
| 1085 | { |
| 1086 | ALOGV("adev_dump"); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 1087 | return 0; |
| 1088 | } |
| 1089 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1090 | static int adev_close(hw_device_t *device) |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1091 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1092 | ALOGV("adev_close"); |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1093 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1094 | struct alsa_audio_device *adev = (struct alsa_audio_device *)device; |
| 1095 | release_aec(adev->aec); |
| 1096 | free(device); |
| 1097 | return 0; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1098 | } |
| 1099 | |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 1100 | static int adev_open(const hw_module_t* module, const char* name, |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1101 | hw_device_t** device) |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 1102 | { |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1103 | struct alsa_audio_device *adev; |
| 1104 | |
| 1105 | ALOGV("adev_open: %s", name); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 1106 | |
| 1107 | if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) |
| 1108 | return -EINVAL; |
| 1109 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1110 | adev = calloc(1, sizeof(struct alsa_audio_device)); |
| 1111 | if (!adev) |
| 1112 | return -ENOMEM; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 1113 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1114 | adev->hw_device.common.tag = HARDWARE_DEVICE_TAG; |
| 1115 | adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0; |
| 1116 | adev->hw_device.common.module = (struct hw_module_t *) module; |
| 1117 | adev->hw_device.common.close = adev_close; |
| 1118 | adev->hw_device.init_check = adev_init_check; |
| 1119 | adev->hw_device.set_voice_volume = adev_set_voice_volume; |
| 1120 | adev->hw_device.set_master_volume = adev_set_master_volume; |
| 1121 | adev->hw_device.get_master_volume = adev_get_master_volume; |
| 1122 | adev->hw_device.set_master_mute = adev_set_master_mute; |
| 1123 | adev->hw_device.get_master_mute = adev_get_master_mute; |
| 1124 | adev->hw_device.set_mode = adev_set_mode; |
| 1125 | adev->hw_device.set_mic_mute = adev_set_mic_mute; |
| 1126 | adev->hw_device.get_mic_mute = adev_get_mic_mute; |
| 1127 | adev->hw_device.set_parameters = adev_set_parameters; |
| 1128 | adev->hw_device.get_parameters = adev_get_parameters; |
| 1129 | adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size; |
| 1130 | adev->hw_device.open_output_stream = adev_open_output_stream; |
| 1131 | adev->hw_device.close_output_stream = adev_close_output_stream; |
| 1132 | adev->hw_device.open_input_stream = adev_open_input_stream; |
| 1133 | adev->hw_device.close_input_stream = adev_close_input_stream; |
| 1134 | adev->hw_device.dump = adev_dump; |
| 1135 | adev->hw_device.get_microphones = adev_get_microphones; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 1136 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1137 | *device = &adev->hw_device.common; |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 1138 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1139 | adev->mixer = mixer_open(CARD_OUT); |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 1140 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1141 | if (!adev->mixer) { |
| 1142 | ALOGE("Unable to open the mixer, aborting."); |
| 1143 | return -EINVAL; |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1144 | } |
| 1145 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1146 | adev->audio_route = audio_route_init(CARD_OUT, MIXER_XML_PATH); |
| 1147 | if (!adev->audio_route) { |
| 1148 | ALOGE("%s: Failed to init audio route controls, aborting.", __func__); |
| 1149 | return -EINVAL; |
| 1150 | } |
Amit Pundir | 456949d | 2020-02-18 22:44:16 +0530 | [diff] [blame] | 1151 | |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1152 | pthread_mutex_lock(&adev->lock); |
| 1153 | if (init_aec(CAPTURE_CODEC_SAMPLING_RATE, NUM_AEC_REFERENCE_CHANNELS, |
| 1154 | CHANNEL_STEREO, &adev->aec)) { |
| 1155 | pthread_mutex_unlock(&adev->lock); |
| 1156 | return -EINVAL; |
| 1157 | } |
| 1158 | pthread_mutex_unlock(&adev->lock); |
| 1159 | |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | static struct hw_module_methods_t hal_module_methods = { |
| 1164 | .open = adev_open, |
| 1165 | }; |
| 1166 | |
| 1167 | struct audio_module HAL_MODULE_INFO_SYM = { |
| 1168 | .common = { |
| 1169 | .tag = HARDWARE_MODULE_TAG, |
| 1170 | .module_api_version = AUDIO_MODULE_API_VERSION_0_1, |
| 1171 | .hal_api_version = HARDWARE_HAL_API_VERSION, |
| 1172 | .id = AUDIO_HARDWARE_MODULE_ID, |
Amit Pundir | e6732bb | 2020-09-28 12:43:59 +0530 | [diff] [blame] | 1173 | .name = "Yukawa audio HW HAL", |
Amit Pundir | 4e37582 | 2019-04-18 16:46:10 +0530 | [diff] [blame] | 1174 | .author = "The Android Open Source Project", |
| 1175 | .methods = &hal_module_methods, |
| 1176 | }, |
| 1177 | }; |