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