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