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