blob: 5a0953c38f2557417ae3c4873284803a09c5c726 [file] [log] [blame]
Amit Pundir4e375822019-04-18 16:46:10 +05301/*
Amit Pundire6732bb2020-09-28 12:43:59 +05302 * Copyright (C) 2016 The Android Open Source Project
Amit Pundir4e375822019-04-18 16:46:10 +05303 *
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 Pundir456949d2020-02-18 22:44:16 +053015 *
Amit Pundire6732bb2020-09-28 12:43:59 +053016 * Copied as it is from device/amlogic/generic/hal/audio/
Amit Pundir4e375822019-04-18 16:46:10 +053017 */
18
Amit Pundire6732bb2020-09-28 12:43:59 +053019#define LOG_TAG "audio_hw_yukawa"
20//#define LOG_NDEBUG 0
Amit Pundir4e375822019-04-18 16:46:10 +053021
22#include <errno.h>
Amit Pundir456949d2020-02-18 22:44:16 +053023#include <inttypes.h>
Amit Pundire6732bb2020-09-28 12:43:59 +053024#include <malloc.h>
Amit Pundir4e375822019-04-18 16:46:10 +053025#include <pthread.h>
26#include <stdint.h>
Amit Pundir4e375822019-04-18 16:46:10 +053027#include <stdlib.h>
Amit Pundir456949d2020-02-18 22:44:16 +053028#include <sys/time.h>
Amit Pundir4e375822019-04-18 16:46:10 +053029#include <unistd.h>
30
31#include <log/log.h>
32#include <cutils/str_parms.h>
Amit Pundire6732bb2020-09-28 12:43:59 +053033#include <cutils/properties.h>
Amit Pundir4e375822019-04-18 16:46:10 +053034
35#include <hardware/hardware.h>
36#include <system/audio.h>
37#include <hardware/audio.h>
Amit Pundire6732bb2020-09-28 12:43:59 +053038
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 Pundir4e375822019-04-18 16:46:10 +053047#include <tinyalsa/asoundlib.h>
Amit Pundir456949d2020-02-18 22:44:16 +053048
Amit Pundire6732bb2020-09-28 12:43:59 +053049#include <sys/ioctl.h>
Amit Pundir4e375822019-04-18 16:46:10 +053050
Amit Pundire6732bb2020-09-28 12:43:59 +053051#include "audio_aec.h"
52#include "audio_hw.h"
Amit Pundir4e375822019-04-18 16:46:10 +053053
Amit Pundire6732bb2020-09-28 12:43:59 +053054static int adev_get_mic_mute(const struct audio_hw_device* dev, bool* state);
55static int adev_get_microphones(const struct audio_hw_device* dev,
56 struct audio_microphone_characteristic_t* mic_array,
57 size_t* mic_count);
58static size_t out_get_buffer_size(const struct audio_stream* stream);
Amit Pundir4e375822019-04-18 16:46:10 +053059
Alden DSouzae44e0672021-02-11 17:07:01 -080060static 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 Pundire6732bb2020-09-28 12:43:59 +053070static int get_audio_output_port(audio_devices_t devices) {
71 /* Only HDMI out for now #FIXME */
72 return PORT_HDMI;
Amit Pundir4e375822019-04-18 16:46:10 +053073}
74
Amit Pundire6732bb2020-09-28 12:43:59 +053075static void timestamp_adjust(struct timespec* ts, ssize_t frames, uint32_t sampling_rate) {
76 /* This function assumes the adjustment (in nsec) is less than the max value of long,
77 * which for 32-bit long this is 2^31 * 1e-9 seconds, slightly over 2 seconds.
78 * For 64-bit long it is 9e+9 seconds. */
79 long adj_nsec = (frames / (float) sampling_rate) * 1E9L;
80 ts->tv_nsec += adj_nsec;
81 while (ts->tv_nsec > 1E9L) {
82 ts->tv_sec++;
83 ts->tv_nsec -= 1E9L;
Amit Pundir456949d2020-02-18 22:44:16 +053084 }
Amit Pundire6732bb2020-09-28 12:43:59 +053085 if (ts->tv_nsec < 0) {
86 ts->tv_sec--;
87 ts->tv_nsec += 1E9L;
88 }
Amit Pundir456949d2020-02-18 22:44:16 +053089}
90
Amit Pundire6732bb2020-09-28 12:43:59 +053091/* Helper function to get PCM hardware timestamp.
92 * Only the field 'timestamp' of argument 'ts' is updated. */
93static int get_pcm_timestamp(struct pcm* pcm, uint32_t sample_rate, struct aec_info* info,
94 bool isOutput) {
95 int ret = 0;
96 if (pcm_get_htimestamp(pcm, &info->available, &info->timestamp) < 0) {
97 ALOGE("Error getting PCM timestamp!");
98 info->timestamp.tv_sec = 0;
99 info->timestamp.tv_nsec = 0;
Amit Pundir456949d2020-02-18 22:44:16 +0530100 return -EINVAL;
101 }
Amit Pundire6732bb2020-09-28 12:43:59 +0530102 ssize_t frames;
103 if (isOutput) {
104 frames = pcm_get_buffer_size(pcm) - info->available;
105 } else {
106 frames = -info->available; /* rewind timestamp */
107 }
108 timestamp_adjust(&info->timestamp, frames, sample_rate);
109 return ret;
Amit Pundir456949d2020-02-18 22:44:16 +0530110}
111
Amit Pundire6732bb2020-09-28 12:43:59 +0530112static int read_filter_from_file(const char* filename, int16_t* filter, int max_length) {
113 FILE* fp = fopen(filename, "r");
114 if (fp == NULL) {
115 ALOGI("%s: File %s not found.", __func__, filename);
116 return 0;
117 }
118 int num_taps = 0;
119 char* line = NULL;
120 size_t len = 0;
121 while (!feof(fp)) {
122 size_t size = getline(&line, &len, fp);
123 if ((line[0] == '#') || (size < 2)) {
124 continue;
125 }
126 int n = sscanf(line, "%" SCNd16 "\n", &filter[num_taps++]);
127 if (n < 1) {
128 ALOGE("Could not find coefficient %d! Exiting...", num_taps - 1);
129 return 0;
130 }
131 ALOGV("Coeff %d : %" PRId16, num_taps, filter[num_taps - 1]);
132 if (num_taps == max_length) {
133 ALOGI("%s: max tap length %d reached.", __func__, max_length);
Amit Pundir456949d2020-02-18 22:44:16 +0530134 break;
135 }
Amit Pundir456949d2020-02-18 22:44:16 +0530136 }
Amit Pundire6732bb2020-09-28 12:43:59 +0530137 free(line);
138 fclose(fp);
139 return num_taps;
Amit Pundir456949d2020-02-18 22:44:16 +0530140}
141
Amit Pundire6732bb2020-09-28 12:43:59 +0530142static void out_set_eq(struct alsa_stream_out* out) {
143 out->speaker_eq = NULL;
144 int16_t* speaker_eq_coeffs = (int16_t*)calloc(SPEAKER_MAX_EQ_LENGTH, sizeof(int16_t));
145 if (speaker_eq_coeffs == NULL) {
146 ALOGE("%s: Failed to allocate speaker EQ", __func__);
147 return;
148 }
149 int num_taps = read_filter_from_file(SPEAKER_EQ_FILE, speaker_eq_coeffs, SPEAKER_MAX_EQ_LENGTH);
150 if (num_taps == 0) {
151 ALOGI("%s: Empty filter file or 0 taps set.", __func__);
152 free(speaker_eq_coeffs);
153 return;
154 }
155 out->speaker_eq = fir_init(
156 out->config.channels, FIR_SINGLE_FILTER, num_taps,
157 out_get_buffer_size(&out->stream.common) / out->config.channels / sizeof(int16_t),
158 speaker_eq_coeffs);
159 free(speaker_eq_coeffs);
160}
Amit Pundir456949d2020-02-18 22:44:16 +0530161
Amit Pundire6732bb2020-09-28 12:43:59 +0530162/* must be called with hw device and output stream mutexes locked */
163static int start_output_stream(struct alsa_stream_out *out)
164{
165 struct alsa_audio_device *adev = out->dev;
166
167 /* default to low power: will be corrected in out_write if necessary before first write to
168 * tinyalsa.
169 */
170 out->write_threshold = PLAYBACK_PERIOD_COUNT * PLAYBACK_PERIOD_SIZE;
171 out->config.start_threshold = PLAYBACK_PERIOD_START_THRESHOLD * PLAYBACK_PERIOD_SIZE;
172 out->config.avail_min = PLAYBACK_PERIOD_SIZE;
173 out->unavailable = true;
174 unsigned int pcm_retry_count = PCM_OPEN_RETRIES;
175 int out_port = get_audio_output_port(out->devices);
176
177 while (1) {
178 out->pcm = pcm_open(CARD_OUT, out_port, PCM_OUT | PCM_MONOTONIC, &out->config);
179 if ((out->pcm != NULL) && pcm_is_ready(out->pcm)) {
Amit Pundir456949d2020-02-18 22:44:16 +0530180 break;
Amit Pundire6732bb2020-09-28 12:43:59 +0530181 } else {
182 ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm));
183 if (out->pcm != NULL) {
184 pcm_close(out->pcm);
185 out->pcm = NULL;
186 }
187 if (--pcm_retry_count == 0) {
188 ALOGE("Failed to open pcm_out after %d tries", PCM_OPEN_RETRIES);
189 return -ENODEV;
190 }
191 usleep(PCM_OPEN_WAIT_TIME_MS * 1000);
Amit Pundir456949d2020-02-18 22:44:16 +0530192 }
Amit Pundir456949d2020-02-18 22:44:16 +0530193 }
Amit Pundire6732bb2020-09-28 12:43:59 +0530194 out->unavailable = false;
195 adev->active_output = out;
196 return 0;
Amit Pundir456949d2020-02-18 22:44:16 +0530197}
198
Amit Pundir4e375822019-04-18 16:46:10 +0530199static uint32_t out_get_sample_rate(const struct audio_stream *stream)
200{
Amit Pundire6732bb2020-09-28 12:43:59 +0530201 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
202 return out->config.rate;
Amit Pundir4e375822019-04-18 16:46:10 +0530203}
204
205static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
206{
Amit Pundire6732bb2020-09-28 12:43:59 +0530207 ALOGV("out_set_sample_rate: %d", 0);
Amit Pundir4e375822019-04-18 16:46:10 +0530208 return -ENOSYS;
209}
210
211static size_t out_get_buffer_size(const struct audio_stream *stream)
212{
Amit Pundire6732bb2020-09-28 12:43:59 +0530213 ALOGV("out_get_buffer_size: %d", 4096);
Amit Pundir4e375822019-04-18 16:46:10 +0530214
Amit Pundire6732bb2020-09-28 12:43:59 +0530215 /* return the closest majoring multiple of 16 frames, as
216 * audioflinger expects audio buffers to be a multiple of 16 frames */
217 size_t size = PLAYBACK_PERIOD_SIZE;
218 size = ((size + 15) / 16) * 16;
219 return size * audio_stream_out_frame_size((struct audio_stream_out *)stream);
Amit Pundir4e375822019-04-18 16:46:10 +0530220}
221
222static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
223{
Amit Pundire6732bb2020-09-28 12:43:59 +0530224 ALOGV("out_get_channels");
225 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
226 return audio_channel_out_mask_from_count(out->config.channels);
Amit Pundir4e375822019-04-18 16:46:10 +0530227}
228
229static audio_format_t out_get_format(const struct audio_stream *stream)
230{
Amit Pundire6732bb2020-09-28 12:43:59 +0530231 ALOGV("out_get_format");
232 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
233 return audio_format_from_pcm_format(out->config.format);
Amit Pundir4e375822019-04-18 16:46:10 +0530234}
235
236static int out_set_format(struct audio_stream *stream, audio_format_t format)
237{
Amit Pundire6732bb2020-09-28 12:43:59 +0530238 ALOGV("out_set_format: %d",format);
Amit Pundir4e375822019-04-18 16:46:10 +0530239 return -ENOSYS;
240}
241
Amit Pundire6732bb2020-09-28 12:43:59 +0530242static int do_output_standby(struct alsa_stream_out *out)
243{
244 struct alsa_audio_device *adev = out->dev;
245
246 fir_reset(out->speaker_eq);
247
248 if (!out->standby) {
249 pcm_close(out->pcm);
250 out->pcm = NULL;
251 adev->active_output = NULL;
252 out->standby = 1;
253 }
254 aec_set_spk_running(adev->aec, false);
255 return 0;
256}
257
258static int out_standby(struct audio_stream *stream)
259{
260 ALOGV("out_standby");
261 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
262 int status;
263
264 pthread_mutex_lock(&out->dev->lock);
265 pthread_mutex_lock(&out->lock);
266 status = do_output_standby(out);
267 pthread_mutex_unlock(&out->lock);
268 pthread_mutex_unlock(&out->dev->lock);
269 return status;
270}
271
Amit Pundir4e375822019-04-18 16:46:10 +0530272static int out_dump(const struct audio_stream *stream, int fd)
273{
Amit Pundire6732bb2020-09-28 12:43:59 +0530274 ALOGV("out_dump");
Amit Pundir4e375822019-04-18 16:46:10 +0530275 return 0;
276}
277
278static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
279{
Amit Pundire6732bb2020-09-28 12:43:59 +0530280 ALOGV("out_set_parameters");
281 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
282 struct alsa_audio_device *adev = out->dev;
Amit Pundir4e375822019-04-18 16:46:10 +0530283 struct str_parms *parms;
284 char value[32];
Amit Pundire6732bb2020-09-28 12:43:59 +0530285 int ret, val = 0;
Amit Pundir4e375822019-04-18 16:46:10 +0530286
287 parms = str_parms_create_str(kvpairs);
288
Amit Pundire6732bb2020-09-28 12:43:59 +0530289 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
290 if (ret >= 0) {
291 val = atoi(value);
292 pthread_mutex_lock(&adev->lock);
293 pthread_mutex_lock(&out->lock);
294 if (((out->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) {
295 out->devices &= ~AUDIO_DEVICE_OUT_ALL;
296 out->devices |= val;
297 }
298 pthread_mutex_unlock(&out->lock);
299 pthread_mutex_unlock(&adev->lock);
Amit Pundir4e375822019-04-18 16:46:10 +0530300 }
301
302 str_parms_destroy(parms);
Amit Pundire6732bb2020-09-28 12:43:59 +0530303 return 0;
Amit Pundir4e375822019-04-18 16:46:10 +0530304}
305
306static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
307{
Amit Pundire6732bb2020-09-28 12:43:59 +0530308 ALOGV("out_get_parameters");
309 return strdup("");
Amit Pundir4e375822019-04-18 16:46:10 +0530310}
311
312static uint32_t out_get_latency(const struct audio_stream_out *stream)
313{
Amit Pundire6732bb2020-09-28 12:43:59 +0530314 ALOGV("out_get_latency");
315 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
316 return (PLAYBACK_PERIOD_SIZE * PLAYBACK_PERIOD_COUNT * 1000) / out->config.rate;
Amit Pundir4e375822019-04-18 16:46:10 +0530317}
318
319static int out_set_volume(struct audio_stream_out *stream, float left,
Amit Pundire6732bb2020-09-28 12:43:59 +0530320 float right)
Amit Pundir4e375822019-04-18 16:46:10 +0530321{
Amit Pundire6732bb2020-09-28 12:43:59 +0530322 ALOGV("out_set_volume: Left:%f Right:%f", left, right);
Amit Pundir456949d2020-02-18 22:44:16 +0530323 return -ENOSYS;
Amit Pundir4e375822019-04-18 16:46:10 +0530324}
325
Amit Pundire6732bb2020-09-28 12:43:59 +0530326static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
327 size_t bytes)
Amit Pundir4e375822019-04-18 16:46:10 +0530328{
Amit Pundire6732bb2020-09-28 12:43:59 +0530329 int ret;
330 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
331 struct alsa_audio_device *adev = out->dev;
332 size_t frame_size = audio_stream_out_frame_size(stream);
333 size_t out_frames = bytes / frame_size;
Amit Pundir456949d2020-02-18 22:44:16 +0530334
Amit Pundire6732bb2020-09-28 12:43:59 +0530335 ALOGV("%s: devices: %d, bytes %zu", __func__, out->devices, bytes);
Amit Pundir456949d2020-02-18 22:44:16 +0530336
Amit Pundire6732bb2020-09-28 12:43:59 +0530337 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
338 * on the output stream mutex - e.g. executing select_mode() while holding the hw device
339 * mutex
340 */
341 pthread_mutex_lock(&adev->lock);
Amit Pundir456949d2020-02-18 22:44:16 +0530342 pthread_mutex_lock(&out->lock);
Amit Pundir456949d2020-02-18 22:44:16 +0530343 if (out->standby) {
Amit Pundire6732bb2020-09-28 12:43:59 +0530344 ret = start_output_stream(out);
345 if (ret != 0) {
346 pthread_mutex_unlock(&adev->lock);
347 goto exit;
348 }
349 out->standby = 0;
350 aec_set_spk_running(adev->aec, true);
Amit Pundir456949d2020-02-18 22:44:16 +0530351 }
352
Amit Pundire6732bb2020-09-28 12:43:59 +0530353 pthread_mutex_unlock(&adev->lock);
Amit Pundir456949d2020-02-18 22:44:16 +0530354
Amit Pundire6732bb2020-09-28 12:43:59 +0530355 if (out->speaker_eq != NULL) {
356 fir_process_interleaved(out->speaker_eq, (int16_t*)buffer, (int16_t*)buffer, out_frames);
Amit Pundir456949d2020-02-18 22:44:16 +0530357 }
Amit Pundir456949d2020-02-18 22:44:16 +0530358
Amit Pundire6732bb2020-09-28 12:43:59 +0530359 ret = pcm_write(out->pcm, buffer, out_frames * frame_size);
360 if (ret == 0) {
361 out->frames_written += out_frames;
362
363 struct aec_info info;
364 get_pcm_timestamp(out->pcm, out->config.rate, &info, true /*isOutput*/);
365 out->timestamp = info.timestamp;
366 info.bytes = out_frames * frame_size;
367 int aec_ret = write_to_reference_fifo(adev->aec, (void *)buffer, &info);
368 if (aec_ret) {
369 ALOGE("AEC: Write to speaker loopback FIFO failed!");
370 }
371 }
372
373exit:
Amit Pundir4e375822019-04-18 16:46:10 +0530374 pthread_mutex_unlock(&out->lock);
375
Amit Pundire6732bb2020-09-28 12:43:59 +0530376 if (ret != 0) {
377 usleep((int64_t)bytes * 1000000 / audio_stream_out_frame_size(stream) /
378 out_get_sample_rate(&stream->common));
Amit Pundir4e375822019-04-18 16:46:10 +0530379 }
380
Amit Pundir456949d2020-02-18 22:44:16 +0530381 return bytes;
Amit Pundir4e375822019-04-18 16:46:10 +0530382}
383
Amit Pundire6732bb2020-09-28 12:43:59 +0530384static int out_get_render_position(const struct audio_stream_out *stream,
385 uint32_t *dsp_frames)
386{
387 ALOGV("out_get_render_position: dsp_frames: %p", dsp_frames);
388 return -ENOSYS;
389}
390
Amit Pundir4e375822019-04-18 16:46:10 +0530391static int out_get_presentation_position(const struct audio_stream_out *stream,
392 uint64_t *frames, struct timespec *timestamp)
393{
Amit Pundir456949d2020-02-18 22:44:16 +0530394 if (stream == NULL || frames == NULL || timestamp == NULL) {
395 return -EINVAL;
396 }
Amit Pundire6732bb2020-09-28 12:43:59 +0530397 struct alsa_stream_out* out = (struct alsa_stream_out*)stream;
Amit Pundir4e375822019-04-18 16:46:10 +0530398
Amit Pundire6732bb2020-09-28 12:43:59 +0530399 *frames = out->frames_written;
400 *timestamp = out->timestamp;
401 ALOGV("%s: frames: %" PRIu64 ", timestamp (nsec): %" PRIu64, __func__, *frames,
402 audio_utils_ns_from_timespec(timestamp));
Amit Pundir4e375822019-04-18 16:46:10 +0530403
Amit Pundir456949d2020-02-18 22:44:16 +0530404 return 0;
Amit Pundir4e375822019-04-18 16:46:10 +0530405}
406
407
408static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
409{
Amit Pundire6732bb2020-09-28 12:43:59 +0530410 ALOGV("out_add_audio_effect: %p", effect);
Amit Pundir4e375822019-04-18 16:46:10 +0530411 return 0;
412}
413
414static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
415{
Amit Pundire6732bb2020-09-28 12:43:59 +0530416 ALOGV("out_remove_audio_effect: %p", effect);
Amit Pundir4e375822019-04-18 16:46:10 +0530417 return 0;
418}
419
420static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
Amit Pundire6732bb2020-09-28 12:43:59 +0530421 int64_t *timestamp)
Amit Pundir4e375822019-04-18 16:46:10 +0530422{
Amit Pundire6732bb2020-09-28 12:43:59 +0530423 *timestamp = 0;
424 ALOGV("out_get_next_write_timestamp: %ld", (long int)(*timestamp));
Amit Pundir456949d2020-02-18 22:44:16 +0530425 return -ENOSYS;
Amit Pundir4e375822019-04-18 16:46:10 +0530426}
427
Amit Pundire6732bb2020-09-28 12:43:59 +0530428/** audio_stream_in implementation **/
429
430/* must be called with hw device and input stream mutexes locked */
431static int start_input_stream(struct alsa_stream_in *in)
432{
433 struct alsa_audio_device *adev = in->dev;
434 in->unavailable = true;
435 unsigned int pcm_retry_count = PCM_OPEN_RETRIES;
436
437 while (1) {
438 in->pcm = pcm_open(CARD_IN, PORT_BUILTIN_MIC, PCM_IN | PCM_MONOTONIC, &in->config);
439 if ((in->pcm != NULL) && pcm_is_ready(in->pcm)) {
440 break;
441 } else {
442 ALOGE("cannot open pcm_in driver: %s", pcm_get_error(in->pcm));
443 if (in->pcm != NULL) {
444 pcm_close(in->pcm);
445 in->pcm = NULL;
446 }
447 if (--pcm_retry_count == 0) {
448 ALOGE("Failed to open pcm_in after %d tries", PCM_OPEN_RETRIES);
449 return -ENODEV;
450 }
451 usleep(PCM_OPEN_WAIT_TIME_MS * 1000);
452 }
453 }
454 in->unavailable = false;
455 adev->active_input = in;
456 return 0;
457}
458
459static void get_mic_characteristics(struct audio_microphone_characteristic_t* mic_data,
460 size_t* mic_count) {
461 *mic_count = 1;
462 memset(mic_data, 0, sizeof(struct audio_microphone_characteristic_t));
463 strlcpy(mic_data->device_id, "builtin_mic", AUDIO_MICROPHONE_ID_MAX_LEN - 1);
464 strlcpy(mic_data->address, "top", AUDIO_DEVICE_MAX_ADDRESS_LEN - 1);
465 memset(mic_data->channel_mapping, AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED,
466 sizeof(mic_data->channel_mapping));
467 mic_data->device = AUDIO_DEVICE_IN_BUILTIN_MIC;
468 mic_data->sensitivity = -37.0;
469 mic_data->max_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
470 mic_data->min_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
471 mic_data->orientation.x = 0.0f;
472 mic_data->orientation.y = 0.0f;
473 mic_data->orientation.z = 0.0f;
474 mic_data->geometric_location.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
475 mic_data->geometric_location.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
476 mic_data->geometric_location.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
477}
478
Amit Pundir4e375822019-04-18 16:46:10 +0530479static uint32_t in_get_sample_rate(const struct audio_stream *stream)
480{
Amit Pundire6732bb2020-09-28 12:43:59 +0530481 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
482 return in->config.rate;
Amit Pundir4e375822019-04-18 16:46:10 +0530483}
484
485static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
486{
Amit Pundire6732bb2020-09-28 12:43:59 +0530487 ALOGV("in_set_sample_rate: %d", rate);
Amit Pundir4e375822019-04-18 16:46:10 +0530488 return -ENOSYS;
489}
490
Amit Pundire6732bb2020-09-28 12:43:59 +0530491static size_t get_input_buffer_size(size_t frames, audio_format_t format,
492 audio_channel_mask_t channel_mask) {
493 /* return the closest majoring multiple of 16 frames, as
494 * audioflinger expects audio buffers to be a multiple of 16 frames */
495 frames = ((frames + 15) / 16) * 16;
496 size_t bytes_per_frame = audio_channel_count_from_in_mask(channel_mask) *
497 audio_bytes_per_sample(format);
498 size_t buffer_size = frames * bytes_per_frame;
499 return buffer_size;
Amit Pundir4e375822019-04-18 16:46:10 +0530500}
501
502static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
503{
Amit Pundire6732bb2020-09-28 12:43:59 +0530504 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
505 ALOGV("in_get_channels: %d", in->config.channels);
506 return audio_channel_in_mask_from_count(in->config.channels);
Amit Pundir4e375822019-04-18 16:46:10 +0530507}
508
509static audio_format_t in_get_format(const struct audio_stream *stream)
510{
Amit Pundire6732bb2020-09-28 12:43:59 +0530511 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
512 ALOGV("in_get_format: %d", in->config.format);
513 return audio_format_from_pcm_format(in->config.format);
Amit Pundir4e375822019-04-18 16:46:10 +0530514}
515
516static int in_set_format(struct audio_stream *stream, audio_format_t format)
517{
518 return -ENOSYS;
519}
520
Amit Pundire6732bb2020-09-28 12:43:59 +0530521static size_t in_get_buffer_size(const struct audio_stream *stream)
Amit Pundir4e375822019-04-18 16:46:10 +0530522{
Amit Pundire6732bb2020-09-28 12:43:59 +0530523 struct alsa_stream_in* in = (struct alsa_stream_in*)stream;
524 size_t frames = CAPTURE_PERIOD_SIZE;
525 if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) {
526 frames = CAPTURE_PERIOD_SIZE * PLAYBACK_CODEC_SAMPLING_RATE / CAPTURE_CODEC_SAMPLING_RATE;
527 }
528
529 size_t buffer_size =
530 get_input_buffer_size(frames, stream->get_format(stream), stream->get_channels(stream));
531 ALOGV("in_get_buffer_size: %zu", buffer_size);
532 return buffer_size;
533}
534
535static int in_get_active_microphones(const struct audio_stream_in* stream,
536 struct audio_microphone_characteristic_t* mic_array,
537 size_t* mic_count) {
538 ALOGV("in_get_active_microphones");
539 if ((mic_array == NULL) || (mic_count == NULL)) {
540 return -EINVAL;
541 }
542 struct alsa_stream_in* in = (struct alsa_stream_in*)stream;
543 struct audio_hw_device* dev = (struct audio_hw_device*)in->dev;
544 bool mic_muted = false;
545 adev_get_mic_mute(dev, &mic_muted);
546 if ((in->source == AUDIO_SOURCE_ECHO_REFERENCE) || mic_muted) {
547 *mic_count = 0;
548 return 0;
549 }
550 adev_get_microphones(dev, mic_array, mic_count);
551 return 0;
552}
553
554static int do_input_standby(struct alsa_stream_in *in)
555{
556 struct alsa_audio_device *adev = in->dev;
557
558 if (!in->standby) {
559 pcm_close(in->pcm);
560 in->pcm = NULL;
561 adev->active_input = NULL;
562 in->standby = true;
563 }
564 return 0;
565}
566
567static int in_standby(struct audio_stream *stream)
568{
569 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
570 int status;
Amit Pundir456949d2020-02-18 22:44:16 +0530571
572 pthread_mutex_lock(&in->lock);
Amit Pundire6732bb2020-09-28 12:43:59 +0530573 pthread_mutex_lock(&in->dev->lock);
574 status = do_input_standby(in);
575 pthread_mutex_unlock(&in->dev->lock);
Amit Pundir456949d2020-02-18 22:44:16 +0530576 pthread_mutex_unlock(&in->lock);
Amit Pundire6732bb2020-09-28 12:43:59 +0530577 return status;
578}
579
580static int in_dump(const struct audio_stream *stream, int fd)
581{
582 struct alsa_stream_in* in = (struct alsa_stream_in*)stream;
583 if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) {
584 return 0;
585 }
586
587 struct audio_microphone_characteristic_t mic_array[AUDIO_MICROPHONE_MAX_COUNT];
588 size_t mic_count;
589
590 get_mic_characteristics(mic_array, &mic_count);
591
592 dprintf(fd, " Microphone count: %zd\n", mic_count);
593 size_t idx;
594 for (idx = 0; idx < mic_count; idx++) {
595 dprintf(fd, " Microphone: %zd\n", idx);
596 dprintf(fd, " Address: %s\n", mic_array[idx].address);
597 dprintf(fd, " Device: %d\n", mic_array[idx].device);
598 dprintf(fd, " Sensitivity (dB): %.2f\n", mic_array[idx].sensitivity);
599 }
600
Amit Pundir4e375822019-04-18 16:46:10 +0530601 return 0;
602}
603
604static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
605{
Amit Pundire6732bb2020-09-28 12:43:59 +0530606 return 0;
Amit Pundir4e375822019-04-18 16:46:10 +0530607}
608
609static char * in_get_parameters(const struct audio_stream *stream,
Amit Pundire6732bb2020-09-28 12:43:59 +0530610 const char *keys)
Amit Pundir4e375822019-04-18 16:46:10 +0530611{
Amit Pundire6732bb2020-09-28 12:43:59 +0530612 return strdup("");
Amit Pundir4e375822019-04-18 16:46:10 +0530613}
614
615static int in_set_gain(struct audio_stream_in *stream, float gain)
616{
617 return 0;
618}
619
Amit Pundir456949d2020-02-18 22:44:16 +0530620static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
Amit Pundire6732bb2020-09-28 12:43:59 +0530621 size_t bytes)
Amit Pundir456949d2020-02-18 22:44:16 +0530622{
Amit Pundire6732bb2020-09-28 12:43:59 +0530623 int ret;
624 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
625 struct alsa_audio_device *adev = in->dev;
626 size_t frame_size = audio_stream_in_frame_size(stream);
627 size_t in_frames = bytes / frame_size;
Amit Pundir456949d2020-02-18 22:44:16 +0530628
Amit Pundire6732bb2020-09-28 12:43:59 +0530629 ALOGV("in_read: stream: %d, bytes %zu", in->source, bytes);
Amit Pundir456949d2020-02-18 22:44:16 +0530630
Amit Pundire6732bb2020-09-28 12:43:59 +0530631 /* Special handling for Echo Reference: simply get the reference from FIFO.
632 * The format and sample rate should be specified by arguments to adev_open_input_stream. */
633 if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) {
634 struct aec_info info;
635 info.bytes = bytes;
Amit Pundir456949d2020-02-18 22:44:16 +0530636
Amit Pundire6732bb2020-09-28 12:43:59 +0530637 const uint64_t time_increment_nsec = (uint64_t)bytes * NANOS_PER_SECOND /
638 audio_stream_in_frame_size(stream) /
639 in_get_sample_rate(&stream->common);
640 if (!aec_get_spk_running(adev->aec)) {
641 if (in->timestamp_nsec == 0) {
642 struct timespec now;
643 clock_gettime(CLOCK_MONOTONIC, &now);
644 const uint64_t timestamp_nsec = audio_utils_ns_from_timespec(&now);
645 in->timestamp_nsec = timestamp_nsec;
646 } else {
647 in->timestamp_nsec += time_increment_nsec;
648 }
649 memset(buffer, 0, bytes);
650 const uint64_t time_increment_usec = time_increment_nsec / 1000;
651 usleep(time_increment_usec);
652 } else {
653 int ref_ret = get_reference_samples(adev->aec, buffer, &info);
654 if ((ref_ret) || (info.timestamp_usec == 0)) {
655 memset(buffer, 0, bytes);
656 in->timestamp_nsec += time_increment_nsec;
657 } else {
658 in->timestamp_nsec = 1000 * info.timestamp_usec;
Amit Pundir456949d2020-02-18 22:44:16 +0530659 }
660 }
Amit Pundire6732bb2020-09-28 12:43:59 +0530661 in->frames_read += in_frames;
Amit Pundir456949d2020-02-18 22:44:16 +0530662
Amit Pundire6732bb2020-09-28 12:43:59 +0530663#if DEBUG_AEC
664 FILE* fp_ref = fopen("/data/local/traces/aec_ref.pcm", "a+");
665 if (fp_ref) {
666 fwrite((char*)buffer, 1, bytes, fp_ref);
667 fclose(fp_ref);
668 } else {
669 ALOGE("AEC debug: Could not open file aec_ref.pcm!");
Amit Pundir456949d2020-02-18 22:44:16 +0530670 }
Amit Pundire6732bb2020-09-28 12:43:59 +0530671 FILE* fp_ref_ts = fopen("/data/local/traces/aec_ref_timestamps.txt", "a+");
672 if (fp_ref_ts) {
673 fprintf(fp_ref_ts, "%" PRIu64 "\n", in->timestamp_nsec);
674 fclose(fp_ref_ts);
675 } else {
676 ALOGE("AEC debug: Could not open file aec_ref_timestamps.txt!");
677 }
678#endif
679 return info.bytes;
680 }
681
682 /* Microphone input stream read */
683
684 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
685 * on the input stream mutex - e.g. executing select_mode() while holding the hw device
686 * mutex
687 */
688 pthread_mutex_lock(&in->lock);
689 pthread_mutex_lock(&adev->lock);
690 if (in->standby) {
691 ret = start_input_stream(in);
692 if (ret != 0) {
693 pthread_mutex_unlock(&adev->lock);
694 ALOGE("start_input_stream failed with code %d", ret);
695 goto exit;
696 }
697 in->standby = false;
698 }
699
700 pthread_mutex_unlock(&adev->lock);
701
702 ret = pcm_read(in->pcm, buffer, in_frames * frame_size);
703 struct aec_info info;
704 get_pcm_timestamp(in->pcm, in->config.rate, &info, false /*isOutput*/);
705 if (ret == 0) {
706 in->frames_read += in_frames;
707 in->timestamp_nsec = audio_utils_ns_from_timespec(&info.timestamp);
708 }
709 else {
710 ALOGE("pcm_read failed with code %d", ret);
Amit Pundir456949d2020-02-18 22:44:16 +0530711 }
712
713exit:
Amit Pundir456949d2020-02-18 22:44:16 +0530714 pthread_mutex_unlock(&in->lock);
715
Amit Pundire6732bb2020-09-28 12:43:59 +0530716 bool mic_muted = false;
717 adev_get_mic_mute((struct audio_hw_device*)adev, &mic_muted);
718 if (mic_muted) {
719 memset(buffer, 0, bytes);
720 }
721
722 if (ret != 0) {
723 usleep((int64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
724 in_get_sample_rate(&stream->common));
725 } else {
726 /* Process AEC if available */
727 /* TODO move to a separate thread */
728 if (!mic_muted) {
729 info.bytes = bytes;
730 int aec_ret = process_aec(adev->aec, buffer, &info);
731 if (aec_ret) {
732 ALOGE("process_aec returned error code %d", aec_ret);
733 }
734 }
735 }
736
737#if DEBUG_AEC && !defined(AEC_HAL)
738 FILE* fp_in = fopen("/data/local/traces/aec_in.pcm", "a+");
739 if (fp_in) {
740 fwrite((char*)buffer, 1, bytes, fp_in);
741 fclose(fp_in);
742 } else {
743 ALOGE("AEC debug: Could not open file aec_in.pcm!");
744 }
745 FILE* fp_mic_ts = fopen("/data/local/traces/aec_in_timestamps.txt", "a+");
746 if (fp_mic_ts) {
747 fprintf(fp_mic_ts, "%" PRIu64 "\n", in->timestamp_nsec);
748 fclose(fp_mic_ts);
749 } else {
750 ALOGE("AEC debug: Could not open file aec_in_timestamps.txt!");
751 }
752#endif
753
Amit Pundir4e375822019-04-18 16:46:10 +0530754 return bytes;
755}
756
Amit Pundire6732bb2020-09-28 12:43:59 +0530757static int in_get_capture_position(const struct audio_stream_in* stream, int64_t* frames,
758 int64_t* time) {
759 if (stream == NULL || frames == NULL || time == NULL) {
760 return -EINVAL;
761 }
762 struct alsa_stream_in* in = (struct alsa_stream_in*)stream;
763
764 *frames = in->frames_read;
765 *time = in->timestamp_nsec;
766 ALOGV("%s: source: %d, timestamp (nsec): %" PRIu64, __func__, in->source, *time);
767
768 return 0;
769}
770
Amit Pundir4e375822019-04-18 16:46:10 +0530771static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
772{
773 return 0;
774}
775
776static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
777{
778 return 0;
779}
780
781static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
782{
783 return 0;
784}
785
786static int adev_open_output_stream(struct audio_hw_device *dev,
Amit Pundire6732bb2020-09-28 12:43:59 +0530787 audio_io_handle_t handle,
788 audio_devices_t devices,
789 audio_output_flags_t flags,
790 struct audio_config *config,
791 struct audio_stream_out **stream_out,
792 const char *address __unused)
Amit Pundir4e375822019-04-18 16:46:10 +0530793{
Amit Pundire6732bb2020-09-28 12:43:59 +0530794 ALOGV("adev_open_output_stream...");
795
796 struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
Amit Pundire6732bb2020-09-28 12:43:59 +0530797 int out_port = get_audio_output_port(devices);
Alden DSouzae44e0672021-02-11 17:07:01 -0800798 struct pcm_params* params = pcm_params_get(CARD_OUT, out_port, PCM_OUT);
799 if (!params) {
Amit Pundire6732bb2020-09-28 12:43:59 +0530800 return -ENOSYS;
Alden DSouzae44e0672021-02-11 17:07:01 -0800801 }
Amit Pundir456949d2020-02-18 22:44:16 +0530802
Alden DSouzae44e0672021-02-11 17:07:01 -0800803 struct alsa_stream_out* out =
804 (struct alsa_stream_out*)calloc(1, sizeof(struct alsa_stream_out));
805 if (!out) {
Amit Pundir4e375822019-04-18 16:46:10 +0530806 return -ENOMEM;
Alden DSouzae44e0672021-02-11 17:07:01 -0800807 }
Amit Pundir4e375822019-04-18 16:46:10 +0530808
809 out->stream.common.get_sample_rate = out_get_sample_rate;
810 out->stream.common.set_sample_rate = out_set_sample_rate;
811 out->stream.common.get_buffer_size = out_get_buffer_size;
812 out->stream.common.get_channels = out_get_channels;
813 out->stream.common.get_format = out_get_format;
814 out->stream.common.set_format = out_set_format;
815 out->stream.common.standby = out_standby;
816 out->stream.common.dump = out_dump;
817 out->stream.common.set_parameters = out_set_parameters;
818 out->stream.common.get_parameters = out_get_parameters;
819 out->stream.common.add_audio_effect = out_add_audio_effect;
820 out->stream.common.remove_audio_effect = out_remove_audio_effect;
821 out->stream.get_latency = out_get_latency;
822 out->stream.set_volume = out_set_volume;
823 out->stream.write = out_write;
824 out->stream.get_render_position = out_get_render_position;
Amit Pundir456949d2020-02-18 22:44:16 +0530825 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Amit Pundire6732bb2020-09-28 12:43:59 +0530826 out->stream.get_presentation_position = out_get_presentation_position;
Amit Pundir4e375822019-04-18 16:46:10 +0530827
Amit Pundire6732bb2020-09-28 12:43:59 +0530828 out->config.channels = CHANNEL_STEREO;
829 out->config.rate = PLAYBACK_CODEC_SAMPLING_RATE;
830 out->config.format = PCM_FORMAT_S16_LE;
831 out->config.period_size = PLAYBACK_PERIOD_SIZE;
832 out->config.period_count = PLAYBACK_PERIOD_COUNT;
Amit Pundir4e375822019-04-18 16:46:10 +0530833
Amit Pundire6732bb2020-09-28 12:43:59 +0530834 if (out->config.rate != config->sample_rate ||
835 audio_channel_count_from_out_mask(config->channel_mask) != CHANNEL_STEREO ||
836 out->config.format != pcm_format_from_audio_format(config->format) ) {
837 config->sample_rate = out->config.rate;
838 config->format = audio_format_from_pcm_format(out->config.format);
839 config->channel_mask = audio_channel_out_mask_from_count(CHANNEL_STEREO);
Alden DSouzae44e0672021-02-11 17:07:01 -0800840 goto error_1;
Amit Pundir4e375822019-04-18 16:46:10 +0530841 }
842
Amit Pundire6732bb2020-09-28 12:43:59 +0530843 ALOGI("adev_open_output_stream selects channels=%d rate=%d format=%d, devices=%d",
844 out->config.channels, out->config.rate, out->config.format, devices);
845
846 out->dev = ladev;
847 out->standby = 1;
848 out->unavailable = false;
849 out->devices = devices;
850
851 config->format = out_get_format(&out->stream.common);
852 config->channel_mask = out_get_channels(&out->stream.common);
853 config->sample_rate = out_get_sample_rate(&out->stream.common);
Amit Pundir4e375822019-04-18 16:46:10 +0530854
Amit Pundire6732bb2020-09-28 12:43:59 +0530855 out->speaker_eq = NULL;
856 if (out_port == PORT_INTERNAL_SPEAKER) {
857 out_set_eq(out);
858 if (out->speaker_eq == NULL) {
859 ALOGE("%s: Failed to initialize speaker EQ", __func__);
860 }
861 }
862
Alden DSouzae44e0672021-02-11 17:07:01 -0800863 int aec_ret = init_aec_reference_config(ladev->aec, out);
864 if (aec_ret) {
865 ALOGE("AEC: Speaker config init failed!");
866 goto error_2;
Amit Pundire6732bb2020-09-28 12:43:59 +0530867 }
Amit Pundir4e375822019-04-18 16:46:10 +0530868
Alden DSouzae44e0672021-02-11 17:07:01 -0800869 *stream_out = &out->stream;
870 return 0;
871
872error_2:
873 fir_release(out->speaker_eq);
874error_1:
875 free(out);
876 return -EINVAL;
Amit Pundir4e375822019-04-18 16:46:10 +0530877}
878
879static void adev_close_output_stream(struct audio_hw_device *dev,
Amit Pundire6732bb2020-09-28 12:43:59 +0530880 struct audio_stream_out *stream)
Amit Pundir4e375822019-04-18 16:46:10 +0530881{
Amit Pundire6732bb2020-09-28 12:43:59 +0530882 ALOGV("adev_close_output_stream...");
883 struct alsa_audio_device *adev = (struct alsa_audio_device *)dev;
884 destroy_aec_reference_config(adev->aec);
885 struct alsa_stream_out* out = (struct alsa_stream_out*)stream;
886 fir_release(out->speaker_eq);
Amit Pundir4e375822019-04-18 16:46:10 +0530887 free(stream);
888}
889
890static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
891{
Amit Pundire6732bb2020-09-28 12:43:59 +0530892 ALOGV("adev_set_parameters");
893 return -ENOSYS;
Amit Pundir4e375822019-04-18 16:46:10 +0530894}
895
896static char * adev_get_parameters(const struct audio_hw_device *dev,
Amit Pundire6732bb2020-09-28 12:43:59 +0530897 const char *keys)
Amit Pundir4e375822019-04-18 16:46:10 +0530898{
Amit Pundire6732bb2020-09-28 12:43:59 +0530899 ALOGV("adev_get_parameters");
Amit Pundir4e375822019-04-18 16:46:10 +0530900 return strdup("");
901}
902
Amit Pundire6732bb2020-09-28 12:43:59 +0530903static int adev_get_microphones(const struct audio_hw_device* dev,
904 struct audio_microphone_characteristic_t* mic_array,
905 size_t* mic_count) {
906 ALOGV("adev_get_microphones");
907 if ((mic_array == NULL) || (mic_count == NULL)) {
908 return -EINVAL;
909 }
910 get_mic_characteristics(mic_array, mic_count);
911 return 0;
912}
913
Amit Pundir4e375822019-04-18 16:46:10 +0530914static int adev_init_check(const struct audio_hw_device *dev)
915{
Amit Pundire6732bb2020-09-28 12:43:59 +0530916 ALOGV("adev_init_check");
Amit Pundir4e375822019-04-18 16:46:10 +0530917 return 0;
918}
919
920static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
921{
Amit Pundire6732bb2020-09-28 12:43:59 +0530922 ALOGV("adev_set_voice_volume: %f", volume);
923 return -ENOSYS;
Amit Pundir4e375822019-04-18 16:46:10 +0530924}
925
926static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
927{
Amit Pundire6732bb2020-09-28 12:43:59 +0530928 ALOGV("adev_set_master_volume: %f", volume);
Amit Pundir4e375822019-04-18 16:46:10 +0530929 return -ENOSYS;
930}
931
932static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
933{
Amit Pundire6732bb2020-09-28 12:43:59 +0530934 ALOGV("adev_get_master_volume: %f", *volume);
Amit Pundir4e375822019-04-18 16:46:10 +0530935 return -ENOSYS;
936}
937
938static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
939{
Amit Pundire6732bb2020-09-28 12:43:59 +0530940 ALOGV("adev_set_master_mute: %d", muted);
Amit Pundir4e375822019-04-18 16:46:10 +0530941 return -ENOSYS;
942}
943
944static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
945{
Amit Pundire6732bb2020-09-28 12:43:59 +0530946 ALOGV("adev_get_master_mute: %d", *muted);
Amit Pundir4e375822019-04-18 16:46:10 +0530947 return -ENOSYS;
948}
949
950static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
951{
Amit Pundire6732bb2020-09-28 12:43:59 +0530952 ALOGV("adev_set_mode: %d", mode);
Amit Pundir4e375822019-04-18 16:46:10 +0530953 return 0;
954}
955
956static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
957{
Amit Pundire6732bb2020-09-28 12:43:59 +0530958 ALOGV("adev_set_mic_mute: %d",state);
959 struct alsa_audio_device *adev = (struct alsa_audio_device *)dev;
Amit Pundir456949d2020-02-18 22:44:16 +0530960 pthread_mutex_lock(&adev->lock);
961 adev->mic_mute = state;
962 pthread_mutex_unlock(&adev->lock);
963 return 0;
Amit Pundir4e375822019-04-18 16:46:10 +0530964}
965
966static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
967{
Amit Pundire6732bb2020-09-28 12:43:59 +0530968 ALOGV("adev_get_mic_mute");
969 struct alsa_audio_device *adev = (struct alsa_audio_device *)dev;
Amit Pundir456949d2020-02-18 22:44:16 +0530970 pthread_mutex_lock(&adev->lock);
971 *state = adev->mic_mute;
972 pthread_mutex_unlock(&adev->lock);
973 return 0;
Amit Pundir4e375822019-04-18 16:46:10 +0530974}
975
976static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
Amit Pundire6732bb2020-09-28 12:43:59 +0530977 const struct audio_config *config)
Amit Pundir4e375822019-04-18 16:46:10 +0530978{
Amit Pundire6732bb2020-09-28 12:43:59 +0530979 size_t buffer_size =
980 get_input_buffer_size(CAPTURE_PERIOD_SIZE, config->format, config->channel_mask);
981 ALOGV("adev_get_input_buffer_size: %zu", buffer_size);
982 return buffer_size;
Amit Pundir4e375822019-04-18 16:46:10 +0530983}
984
Amit Pundire6732bb2020-09-28 12:43:59 +0530985static int adev_open_input_stream(struct audio_hw_device* dev, audio_io_handle_t handle,
986 audio_devices_t devices, struct audio_config* config,
987 struct audio_stream_in** stream_in,
988 audio_input_flags_t flags __unused, const char* address __unused,
989 audio_source_t source) {
990 ALOGV("adev_open_input_stream...");
Amit Pundir456949d2020-02-18 22:44:16 +0530991
Amit Pundire6732bb2020-09-28 12:43:59 +0530992 struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
Amit Pundir456949d2020-02-18 22:44:16 +0530993
Alden DSouzae44e0672021-02-11 17:07:01 -0800994 struct pcm_params* params = pcm_params_get(CARD_IN, PORT_BUILTIN_MIC, PCM_IN);
995 if (!params) {
Amit Pundire6732bb2020-09-28 12:43:59 +0530996 return -ENOSYS;
Alden DSouzae44e0672021-02-11 17:07:01 -0800997 }
Amit Pundire6732bb2020-09-28 12:43:59 +0530998
Alden DSouzae44e0672021-02-11 17:07:01 -0800999 struct alsa_stream_in* in = (struct alsa_stream_in*)calloc(1, sizeof(struct alsa_stream_in));
1000 if (!in) {
Amit Pundire6732bb2020-09-28 12:43:59 +05301001 return -ENOMEM;
Alden DSouzae44e0672021-02-11 17:07:01 -08001002 }
Amit Pundir4e375822019-04-18 16:46:10 +05301003
1004 in->stream.common.get_sample_rate = in_get_sample_rate;
Amit Pundire6732bb2020-09-28 12:43:59 +05301005 in->stream.common.set_sample_rate = in_set_sample_rate;
Amit Pundir4e375822019-04-18 16:46:10 +05301006 in->stream.common.get_buffer_size = in_get_buffer_size;
1007 in->stream.common.get_channels = in_get_channels;
1008 in->stream.common.get_format = in_get_format;
Amit Pundire6732bb2020-09-28 12:43:59 +05301009 in->stream.common.set_format = in_set_format;
Amit Pundir4e375822019-04-18 16:46:10 +05301010 in->stream.common.standby = in_standby;
1011 in->stream.common.dump = in_dump;
1012 in->stream.common.set_parameters = in_set_parameters;
1013 in->stream.common.get_parameters = in_get_parameters;
Amit Pundire6732bb2020-09-28 12:43:59 +05301014 in->stream.common.add_audio_effect = in_add_audio_effect;
1015 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1016 in->stream.set_gain = in_set_gain;
Amit Pundir4e375822019-04-18 16:46:10 +05301017 in->stream.read = in_read;
Amit Pundire6732bb2020-09-28 12:43:59 +05301018 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Amit Pundir456949d2020-02-18 22:44:16 +05301019 in->stream.get_capture_position = in_get_capture_position;
1020 in->stream.get_active_microphones = in_get_active_microphones;
1021
Amit Pundire6732bb2020-09-28 12:43:59 +05301022 in->config.channels = CHANNEL_STEREO;
1023 if (source == AUDIO_SOURCE_ECHO_REFERENCE) {
1024 in->config.rate = PLAYBACK_CODEC_SAMPLING_RATE;
1025 } else {
1026 in->config.rate = CAPTURE_CODEC_SAMPLING_RATE;
1027 }
1028 in->config.format = PCM_FORMAT_S32_LE;
1029 in->config.period_size = CAPTURE_PERIOD_SIZE;
1030 in->config.period_count = CAPTURE_PERIOD_COUNT;
Amit Pundir456949d2020-02-18 22:44:16 +05301031
Amit Pundire6732bb2020-09-28 12:43:59 +05301032 if (in->config.rate != config->sample_rate ||
1033 audio_channel_count_from_in_mask(config->channel_mask) != CHANNEL_STEREO ||
1034 in->config.format != pcm_format_from_audio_format(config->format) ) {
Alden DSouzae44e0672021-02-11 17:07:01 -08001035 config->format = in_get_format(&in->stream.common);
1036 config->channel_mask = in_get_channels(&in->stream.common);
1037 config->sample_rate = in_get_sample_rate(&in->stream.common);
1038 goto error_1;
Amit Pundire6732bb2020-09-28 12:43:59 +05301039 }
Amit Pundir456949d2020-02-18 22:44:16 +05301040
Amit Pundire6732bb2020-09-28 12:43:59 +05301041 ALOGI("adev_open_input_stream selects channels=%d rate=%d format=%d source=%d",
1042 in->config.channels, in->config.rate, in->config.format, source);
1043
1044 in->dev = ladev;
Amit Pundir456949d2020-02-18 22:44:16 +05301045 in->standby = true;
Amit Pundire6732bb2020-09-28 12:43:59 +05301046 in->unavailable = false;
1047 in->source = source;
1048 in->devices = devices;
Amit Pundir456949d2020-02-18 22:44:16 +05301049
Alden DSouzae44e0672021-02-11 17:07:01 -08001050 if (is_aec_input(in)) {
Amit Pundire6732bb2020-09-28 12:43:59 +05301051 int aec_ret = init_aec_mic_config(ladev->aec, in);
1052 if (aec_ret) {
1053 ALOGE("AEC: Mic config init failed!");
Alden DSouzae44e0672021-02-11 17:07:01 -08001054 goto error_1;
Amit Pundir456949d2020-02-18 22:44:16 +05301055 }
Amit Pundire6732bb2020-09-28 12:43:59 +05301056 }
1057
Amit Pundire6732bb2020-09-28 12:43:59 +05301058#if DEBUG_AEC
1059 remove("/data/local/traces/aec_ref.pcm");
1060 remove("/data/local/traces/aec_in.pcm");
1061 remove("/data/local/traces/aec_ref_timestamps.txt");
1062 remove("/data/local/traces/aec_in_timestamps.txt");
1063#endif
Alden DSouzae44e0672021-02-11 17:07:01 -08001064
1065 *stream_in = &in->stream;
1066 return 0;
1067
1068error_1:
1069 free(in);
1070 return -EINVAL;
Amit Pundire6732bb2020-09-28 12:43:59 +05301071}
Amit Pundir456949d2020-02-18 22:44:16 +05301072
Amit Pundire6732bb2020-09-28 12:43:59 +05301073static void adev_close_input_stream(struct audio_hw_device *dev,
1074 struct audio_stream_in *stream)
1075{
1076 ALOGV("adev_close_input_stream...");
Alden DSouzae44e0672021-02-11 17:07:01 -08001077 struct alsa_stream_in* in = (struct alsa_stream_in*)stream;
1078 if (is_aec_input(in)) {
1079 destroy_aec_mic_config(in->dev->aec);
1080 }
Amit Pundire6732bb2020-09-28 12:43:59 +05301081 free(stream);
1082 return;
1083}
Amit Pundir456949d2020-02-18 22:44:16 +05301084
Amit Pundire6732bb2020-09-28 12:43:59 +05301085static int adev_dump(const audio_hw_device_t *device, int fd)
1086{
1087 ALOGV("adev_dump");
Amit Pundir4e375822019-04-18 16:46:10 +05301088 return 0;
1089}
1090
Amit Pundire6732bb2020-09-28 12:43:59 +05301091static int adev_close(hw_device_t *device)
Amit Pundir456949d2020-02-18 22:44:16 +05301092{
Amit Pundire6732bb2020-09-28 12:43:59 +05301093 ALOGV("adev_close");
Amit Pundir456949d2020-02-18 22:44:16 +05301094
Amit Pundire6732bb2020-09-28 12:43:59 +05301095 struct alsa_audio_device *adev = (struct alsa_audio_device *)device;
1096 release_aec(adev->aec);
Alden DSouzae44e0672021-02-11 17:07:01 -08001097 audio_route_free(adev->audio_route);
1098 mixer_close(adev->mixer);
Amit Pundire6732bb2020-09-28 12:43:59 +05301099 free(device);
1100 return 0;
Amit Pundir456949d2020-02-18 22:44:16 +05301101}
1102
Amit Pundir4e375822019-04-18 16:46:10 +05301103static int adev_open(const hw_module_t* module, const char* name,
Amit Pundire6732bb2020-09-28 12:43:59 +05301104 hw_device_t** device)
Amit Pundir4e375822019-04-18 16:46:10 +05301105{
Amit Pundire6732bb2020-09-28 12:43:59 +05301106 ALOGV("adev_open: %s", name);
Amit Pundir4e375822019-04-18 16:46:10 +05301107
Alden DSouzae44e0672021-02-11 17:07:01 -08001108 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) {
Amit Pundir4e375822019-04-18 16:46:10 +05301109 return -EINVAL;
Alden DSouzae44e0672021-02-11 17:07:01 -08001110 }
Amit Pundir4e375822019-04-18 16:46:10 +05301111
Alden DSouzae44e0672021-02-11 17:07:01 -08001112 struct alsa_audio_device* adev = calloc(1, sizeof(struct alsa_audio_device));
1113 if (!adev) {
Amit Pundire6732bb2020-09-28 12:43:59 +05301114 return -ENOMEM;
Alden DSouzae44e0672021-02-11 17:07:01 -08001115 }
Amit Pundir4e375822019-04-18 16:46:10 +05301116
Amit Pundire6732bb2020-09-28 12:43:59 +05301117 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
1118 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
1119 adev->hw_device.common.module = (struct hw_module_t *) module;
1120 adev->hw_device.common.close = adev_close;
1121 adev->hw_device.init_check = adev_init_check;
1122 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1123 adev->hw_device.set_master_volume = adev_set_master_volume;
1124 adev->hw_device.get_master_volume = adev_get_master_volume;
1125 adev->hw_device.set_master_mute = adev_set_master_mute;
1126 adev->hw_device.get_master_mute = adev_get_master_mute;
1127 adev->hw_device.set_mode = adev_set_mode;
1128 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1129 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1130 adev->hw_device.set_parameters = adev_set_parameters;
1131 adev->hw_device.get_parameters = adev_get_parameters;
1132 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1133 adev->hw_device.open_output_stream = adev_open_output_stream;
1134 adev->hw_device.close_output_stream = adev_close_output_stream;
1135 adev->hw_device.open_input_stream = adev_open_input_stream;
1136 adev->hw_device.close_input_stream = adev_close_input_stream;
1137 adev->hw_device.dump = adev_dump;
1138 adev->hw_device.get_microphones = adev_get_microphones;
Amit Pundir4e375822019-04-18 16:46:10 +05301139
Amit Pundire6732bb2020-09-28 12:43:59 +05301140 *device = &adev->hw_device.common;
Amit Pundir4e375822019-04-18 16:46:10 +05301141
Amit Pundire6732bb2020-09-28 12:43:59 +05301142 adev->mixer = mixer_open(CARD_OUT);
Amit Pundire6732bb2020-09-28 12:43:59 +05301143 if (!adev->mixer) {
1144 ALOGE("Unable to open the mixer, aborting.");
Alden DSouzae44e0672021-02-11 17:07:01 -08001145 goto error_1;
Amit Pundir456949d2020-02-18 22:44:16 +05301146 }
1147
Amit Pundire6732bb2020-09-28 12:43:59 +05301148 adev->audio_route = audio_route_init(CARD_OUT, MIXER_XML_PATH);
1149 if (!adev->audio_route) {
1150 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
Alden DSouzae44e0672021-02-11 17:07:01 -08001151 goto error_2;
Amit Pundire6732bb2020-09-28 12:43:59 +05301152 }
Amit Pundir456949d2020-02-18 22:44:16 +05301153
Amit Pundire6732bb2020-09-28 12:43:59 +05301154 pthread_mutex_lock(&adev->lock);
1155 if (init_aec(CAPTURE_CODEC_SAMPLING_RATE, NUM_AEC_REFERENCE_CHANNELS,
1156 CHANNEL_STEREO, &adev->aec)) {
1157 pthread_mutex_unlock(&adev->lock);
Alden DSouzae44e0672021-02-11 17:07:01 -08001158 goto error_3;
Amit Pundire6732bb2020-09-28 12:43:59 +05301159 }
1160 pthread_mutex_unlock(&adev->lock);
1161
Amit Pundir4e375822019-04-18 16:46:10 +05301162 return 0;
Alden DSouzae44e0672021-02-11 17:07:01 -08001163
1164error_3:
1165 audio_route_free(adev->audio_route);
1166error_2:
1167 mixer_close(adev->mixer);
1168error_1:
1169 free(adev);
1170 return -EINVAL;
Amit Pundir4e375822019-04-18 16:46:10 +05301171}
1172
1173static struct hw_module_methods_t hal_module_methods = {
1174 .open = adev_open,
1175};
1176
1177struct audio_module HAL_MODULE_INFO_SYM = {
1178 .common = {
1179 .tag = HARDWARE_MODULE_TAG,
1180 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1181 .hal_api_version = HARDWARE_HAL_API_VERSION,
1182 .id = AUDIO_HARDWARE_MODULE_ID,
Amit Pundire6732bb2020-09-28 12:43:59 +05301183 .name = "Yukawa audio HW HAL",
Amit Pundir4e375822019-04-18 16:46:10 +05301184 .author = "The Android Open Source Project",
1185 .methods = &hal_module_methods,
1186 },
1187};