blob: 751345639df7c1742a63f3b2e04bb00c83691cb9 [file] [log] [blame]
Amit Pundirc738b812019-06-19 16:13:49 +05301/*
Amit Pundirfec74f62020-09-28 12:43:59 +05302 * Copyright (C) 2016 The Android Open Source Project
Amit Pundirc738b812019-06-19 16:13:49 +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 Pundir648e1d02020-02-18 22:44:16 +053015 *
Amit Pundirfec74f62020-09-28 12:43:59 +053016 * Copied as it is from device/amlogic/generic/hal/audio/
Amit Pundirc738b812019-06-19 16:13:49 +053017 */
18
19#define LOG_TAG "audio_hw_generic"
Amit Pundirfec74f62020-09-28 12:43:59 +053020//#define LOG_NDEBUG 0
Amit Pundirc738b812019-06-19 16:13:49 +053021
22#include <errno.h>
Amit Pundir648e1d02020-02-18 22:44:16 +053023#include <inttypes.h>
Amit Pundirfec74f62020-09-28 12:43:59 +053024#include <malloc.h>
Amit Pundirc738b812019-06-19 16:13:49 +053025#include <pthread.h>
26#include <stdint.h>
Amit Pundirc738b812019-06-19 16:13:49 +053027#include <stdlib.h>
Amit Pundir648e1d02020-02-18 22:44:16 +053028#include <sys/time.h>
Amit Pundirc738b812019-06-19 16:13:49 +053029#include <unistd.h>
30
31#include <log/log.h>
32#include <cutils/str_parms.h>
Amit Pundirfec74f62020-09-28 12:43:59 +053033#include <cutils/properties.h>
Amit Pundirc738b812019-06-19 16:13:49 +053034
35#include <hardware/hardware.h>
36#include <system/audio.h>
37#include <hardware/audio.h>
Amit Pundirfec74f62020-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 Pundirc738b812019-06-19 16:13:49 +053047#include <tinyalsa/asoundlib.h>
Amit Pundir648e1d02020-02-18 22:44:16 +053048
Amit Pundirfec74f62020-09-28 12:43:59 +053049#include <sys/ioctl.h>
Amit Pundirc738b812019-06-19 16:13:49 +053050
Amit Pundirfec74f62020-09-28 12:43:59 +053051#include "audio_aec.h"
52#include "audio_hw.h"
Amit Pundirc738b812019-06-19 16:13:49 +053053
Amit Pundirfec74f62020-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 Pundirc738b812019-06-19 16:13:49 +053059
Alden DSouza0f4cb5f2021-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 Pundirfec74f62020-09-28 12:43:59 +053070static int get_audio_output_port(audio_devices_t devices) {
71 /* Default to internal speaker */
72 int port = PORT_INTERNAL_SPEAKER;
73 return port;
Amit Pundirc738b812019-06-19 16:13:49 +053074}
75
Amit Pundirfec74f62020-09-28 12:43:59 +053076static void timestamp_adjust(struct timespec* ts, ssize_t frames, uint32_t sampling_rate) {
77 /* This function assumes the adjustment (in nsec) is less than the max value of long,
78 * which for 32-bit long this is 2^31 * 1e-9 seconds, slightly over 2 seconds.
79 * For 64-bit long it is 9e+9 seconds. */
80 long adj_nsec = (frames / (float) sampling_rate) * 1E9L;
81 ts->tv_nsec += adj_nsec;
82 while (ts->tv_nsec > 1E9L) {
83 ts->tv_sec++;
84 ts->tv_nsec -= 1E9L;
Amit Pundir648e1d02020-02-18 22:44:16 +053085 }
Amit Pundirfec74f62020-09-28 12:43:59 +053086 if (ts->tv_nsec < 0) {
87 ts->tv_sec--;
88 ts->tv_nsec += 1E9L;
89 }
Amit Pundir648e1d02020-02-18 22:44:16 +053090}
91
Amit Pundirfec74f62020-09-28 12:43:59 +053092/* Helper function to get PCM hardware timestamp.
93 * Only the field 'timestamp' of argument 'ts' is updated. */
94static int get_pcm_timestamp(struct pcm* pcm, uint32_t sample_rate, struct aec_info* info,
95 bool isOutput) {
96 int ret = 0;
97 if (pcm_get_htimestamp(pcm, &info->available, &info->timestamp) < 0) {
98 ALOGE("Error getting PCM timestamp!");
99 info->timestamp.tv_sec = 0;
100 info->timestamp.tv_nsec = 0;
Amit Pundir648e1d02020-02-18 22:44:16 +0530101 return -EINVAL;
102 }
Amit Pundirfec74f62020-09-28 12:43:59 +0530103 ssize_t frames;
104 if (isOutput) {
105 frames = pcm_get_buffer_size(pcm) - info->available;
106 } else {
107 frames = -info->available; /* rewind timestamp */
108 }
109 timestamp_adjust(&info->timestamp, frames, sample_rate);
110 return ret;
Amit Pundir648e1d02020-02-18 22:44:16 +0530111}
112
Amit Pundirfec74f62020-09-28 12:43:59 +0530113static int read_filter_from_file(const char* filename, int16_t* filter, int max_length) {
114 FILE* fp = fopen(filename, "r");
115 if (fp == NULL) {
116 ALOGI("%s: File %s not found.", __func__, filename);
117 return 0;
118 }
119 int num_taps = 0;
120 char* line = NULL;
121 size_t len = 0;
122 while (!feof(fp)) {
123 size_t size = getline(&line, &len, fp);
124 if ((line[0] == '#') || (size < 2)) {
125 continue;
126 }
127 int n = sscanf(line, "%" SCNd16 "\n", &filter[num_taps++]);
128 if (n < 1) {
129 ALOGE("Could not find coefficient %d! Exiting...", num_taps - 1);
130 return 0;
131 }
132 ALOGV("Coeff %d : %" PRId16, num_taps, filter[num_taps - 1]);
133 if (num_taps == max_length) {
134 ALOGI("%s: max tap length %d reached.", __func__, max_length);
Amit Pundir648e1d02020-02-18 22:44:16 +0530135 break;
136 }
Amit Pundir648e1d02020-02-18 22:44:16 +0530137 }
Amit Pundirfec74f62020-09-28 12:43:59 +0530138 free(line);
139 fclose(fp);
140 return num_taps;
Amit Pundir648e1d02020-02-18 22:44:16 +0530141}
142
Amit Pundirfec74f62020-09-28 12:43:59 +0530143static void out_set_eq(struct alsa_stream_out* out) {
144 out->speaker_eq = NULL;
145 int16_t* speaker_eq_coeffs = (int16_t*)calloc(SPEAKER_MAX_EQ_LENGTH, sizeof(int16_t));
146 if (speaker_eq_coeffs == NULL) {
147 ALOGE("%s: Failed to allocate speaker EQ", __func__);
148 return;
149 }
150 int num_taps = read_filter_from_file(SPEAKER_EQ_FILE, speaker_eq_coeffs, SPEAKER_MAX_EQ_LENGTH);
151 if (num_taps == 0) {
152 ALOGI("%s: Empty filter file or 0 taps set.", __func__);
153 free(speaker_eq_coeffs);
154 return;
155 }
156 out->speaker_eq = fir_init(
157 out->config.channels, FIR_SINGLE_FILTER, num_taps,
158 out_get_buffer_size(&out->stream.common) / out->config.channels / sizeof(int16_t),
159 speaker_eq_coeffs);
160 free(speaker_eq_coeffs);
161}
Amit Pundir648e1d02020-02-18 22:44:16 +0530162
Amit Pundirfec74f62020-09-28 12:43:59 +0530163/* must be called with hw device and output stream mutexes locked */
164static int start_output_stream(struct alsa_stream_out *out)
165{
166 struct alsa_audio_device *adev = out->dev;
167
168 /* default to low power: will be corrected in out_write if necessary before first write to
169 * tinyalsa.
170 */
171 out->write_threshold = PLAYBACK_PERIOD_COUNT * PLAYBACK_PERIOD_SIZE;
172 out->config.start_threshold = PLAYBACK_PERIOD_START_THRESHOLD * PLAYBACK_PERIOD_SIZE;
173 out->config.avail_min = PLAYBACK_PERIOD_SIZE;
174 out->unavailable = true;
175 unsigned int pcm_retry_count = PCM_OPEN_RETRIES;
176 int out_port = get_audio_output_port(out->devices);
177
178 while (1) {
179 out->pcm = pcm_open(CARD_OUT, out_port, PCM_OUT | PCM_MONOTONIC, &out->config);
180 if ((out->pcm != NULL) && pcm_is_ready(out->pcm)) {
Amit Pundir648e1d02020-02-18 22:44:16 +0530181 break;
Amit Pundirfec74f62020-09-28 12:43:59 +0530182 } else {
183 ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm));
184 if (out->pcm != NULL) {
185 pcm_close(out->pcm);
186 out->pcm = NULL;
187 }
188 if (--pcm_retry_count == 0) {
189 ALOGE("Failed to open pcm_out after %d tries", PCM_OPEN_RETRIES);
190 return -ENODEV;
191 }
192 usleep(PCM_OPEN_WAIT_TIME_MS * 1000);
Amit Pundir648e1d02020-02-18 22:44:16 +0530193 }
Amit Pundir648e1d02020-02-18 22:44:16 +0530194 }
Amit Pundirfec74f62020-09-28 12:43:59 +0530195 out->unavailable = false;
196 adev->active_output = out;
197 return 0;
Amit Pundir648e1d02020-02-18 22:44:16 +0530198}
199
Amit Pundirc738b812019-06-19 16:13:49 +0530200static uint32_t out_get_sample_rate(const struct audio_stream *stream)
201{
Amit Pundirfec74f62020-09-28 12:43:59 +0530202 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
203 return out->config.rate;
Amit Pundirc738b812019-06-19 16:13:49 +0530204}
205
206static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
207{
Amit Pundirfec74f62020-09-28 12:43:59 +0530208 ALOGV("out_set_sample_rate: %d", 0);
Amit Pundirc738b812019-06-19 16:13:49 +0530209 return -ENOSYS;
210}
211
212static size_t out_get_buffer_size(const struct audio_stream *stream)
213{
Amit Pundirfec74f62020-09-28 12:43:59 +0530214 ALOGV("out_get_buffer_size: %d", 4096);
Amit Pundirc738b812019-06-19 16:13:49 +0530215
Amit Pundirfec74f62020-09-28 12:43:59 +0530216 /* return the closest majoring multiple of 16 frames, as
217 * audioflinger expects audio buffers to be a multiple of 16 frames */
218 size_t size = PLAYBACK_PERIOD_SIZE;
219 size = ((size + 15) / 16) * 16;
220 return size * audio_stream_out_frame_size((struct audio_stream_out *)stream);
Amit Pundirc738b812019-06-19 16:13:49 +0530221}
222
223static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
224{
Amit Pundirfec74f62020-09-28 12:43:59 +0530225 ALOGV("out_get_channels");
226 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
227 return audio_channel_out_mask_from_count(out->config.channels);
Amit Pundirc738b812019-06-19 16:13:49 +0530228}
229
230static audio_format_t out_get_format(const struct audio_stream *stream)
231{
Amit Pundirfec74f62020-09-28 12:43:59 +0530232 ALOGV("out_get_format");
233 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
234 return audio_format_from_pcm_format(out->config.format);
Amit Pundirc738b812019-06-19 16:13:49 +0530235}
236
237static int out_set_format(struct audio_stream *stream, audio_format_t format)
238{
Amit Pundirfec74f62020-09-28 12:43:59 +0530239 ALOGV("out_set_format: %d",format);
Amit Pundirc738b812019-06-19 16:13:49 +0530240 return -ENOSYS;
241}
242
Amit Pundirfec74f62020-09-28 12:43:59 +0530243static int do_output_standby(struct alsa_stream_out *out)
244{
245 struct alsa_audio_device *adev = out->dev;
246
247 fir_reset(out->speaker_eq);
248
249 if (!out->standby) {
250 pcm_close(out->pcm);
251 out->pcm = NULL;
252 adev->active_output = NULL;
253 out->standby = 1;
254 }
255 aec_set_spk_running(adev->aec, false);
256 return 0;
257}
258
259static int out_standby(struct audio_stream *stream)
260{
261 ALOGV("out_standby");
262 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
263 int status;
264
265 pthread_mutex_lock(&out->dev->lock);
266 pthread_mutex_lock(&out->lock);
267 status = do_output_standby(out);
268 pthread_mutex_unlock(&out->lock);
269 pthread_mutex_unlock(&out->dev->lock);
270 return status;
271}
272
Amit Pundirc738b812019-06-19 16:13:49 +0530273static int out_dump(const struct audio_stream *stream, int fd)
274{
Amit Pundirfec74f62020-09-28 12:43:59 +0530275 ALOGV("out_dump");
Amit Pundirc738b812019-06-19 16:13:49 +0530276 return 0;
277}
278
279static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
280{
Amit Pundirfec74f62020-09-28 12:43:59 +0530281 ALOGV("out_set_parameters");
282 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
283 struct alsa_audio_device *adev = out->dev;
Amit Pundirc738b812019-06-19 16:13:49 +0530284 struct str_parms *parms;
285 char value[32];
Amit Pundirfec74f62020-09-28 12:43:59 +0530286 int ret, val = 0;
Amit Pundirc738b812019-06-19 16:13:49 +0530287
288 parms = str_parms_create_str(kvpairs);
289
Amit Pundirfec74f62020-09-28 12:43:59 +0530290 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
291 if (ret >= 0) {
292 val = atoi(value);
293 pthread_mutex_lock(&adev->lock);
294 pthread_mutex_lock(&out->lock);
295 if (((out->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) {
296 out->devices &= ~AUDIO_DEVICE_OUT_ALL;
297 out->devices |= val;
298 }
299 pthread_mutex_unlock(&out->lock);
300 pthread_mutex_unlock(&adev->lock);
Amit Pundirc738b812019-06-19 16:13:49 +0530301 }
302
303 str_parms_destroy(parms);
Amit Pundirfec74f62020-09-28 12:43:59 +0530304 return 0;
Amit Pundirc738b812019-06-19 16:13:49 +0530305}
306
307static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
308{
Amit Pundirfec74f62020-09-28 12:43:59 +0530309 ALOGV("out_get_parameters");
310 return strdup("");
Amit Pundirc738b812019-06-19 16:13:49 +0530311}
312
313static uint32_t out_get_latency(const struct audio_stream_out *stream)
314{
Amit Pundirfec74f62020-09-28 12:43:59 +0530315 ALOGV("out_get_latency");
316 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
317 return (PLAYBACK_PERIOD_SIZE * PLAYBACK_PERIOD_COUNT * 1000) / out->config.rate;
Amit Pundirc738b812019-06-19 16:13:49 +0530318}
319
320static int out_set_volume(struct audio_stream_out *stream, float left,
Amit Pundirfec74f62020-09-28 12:43:59 +0530321 float right)
Amit Pundirc738b812019-06-19 16:13:49 +0530322{
Amit Pundirfec74f62020-09-28 12:43:59 +0530323 ALOGV("out_set_volume: Left:%f Right:%f", left, right);
Amit Pundir648e1d02020-02-18 22:44:16 +0530324 return -ENOSYS;
Amit Pundirc738b812019-06-19 16:13:49 +0530325}
326
Amit Pundirfec74f62020-09-28 12:43:59 +0530327static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
328 size_t bytes)
Amit Pundirc738b812019-06-19 16:13:49 +0530329{
Amit Pundirfec74f62020-09-28 12:43:59 +0530330 int ret;
331 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
332 struct alsa_audio_device *adev = out->dev;
333 size_t frame_size = audio_stream_out_frame_size(stream);
334 size_t out_frames = bytes / frame_size;
Amit Pundir648e1d02020-02-18 22:44:16 +0530335
Amit Pundirfec74f62020-09-28 12:43:59 +0530336 ALOGV("%s: devices: %d, bytes %zu", __func__, out->devices, bytes);
Amit Pundir648e1d02020-02-18 22:44:16 +0530337
Amit Pundirfec74f62020-09-28 12:43:59 +0530338 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
339 * on the output stream mutex - e.g. executing select_mode() while holding the hw device
340 * mutex
341 */
342 pthread_mutex_lock(&adev->lock);
Amit Pundir648e1d02020-02-18 22:44:16 +0530343 pthread_mutex_lock(&out->lock);
Amit Pundir648e1d02020-02-18 22:44:16 +0530344 if (out->standby) {
Amit Pundirfec74f62020-09-28 12:43:59 +0530345 ret = start_output_stream(out);
346 if (ret != 0) {
347 pthread_mutex_unlock(&adev->lock);
348 goto exit;
349 }
350 out->standby = 0;
351 aec_set_spk_running(adev->aec, true);
Amit Pundir648e1d02020-02-18 22:44:16 +0530352 }
353
Amit Pundirfec74f62020-09-28 12:43:59 +0530354 pthread_mutex_unlock(&adev->lock);
Amit Pundir648e1d02020-02-18 22:44:16 +0530355
Amit Pundirfec74f62020-09-28 12:43:59 +0530356 if (out->speaker_eq != NULL) {
357 fir_process_interleaved(out->speaker_eq, (int16_t*)buffer, (int16_t*)buffer, out_frames);
Amit Pundir648e1d02020-02-18 22:44:16 +0530358 }
Amit Pundir648e1d02020-02-18 22:44:16 +0530359
Amit Pundirfec74f62020-09-28 12:43:59 +0530360 ret = pcm_write(out->pcm, buffer, out_frames * frame_size);
361 if (ret == 0) {
362 out->frames_written += out_frames;
363
364 struct aec_info info;
365 get_pcm_timestamp(out->pcm, out->config.rate, &info, true /*isOutput*/);
366 out->timestamp = info.timestamp;
367 info.bytes = out_frames * frame_size;
368 int aec_ret = write_to_reference_fifo(adev->aec, (void *)buffer, &info);
369 if (aec_ret) {
370 ALOGE("AEC: Write to speaker loopback FIFO failed!");
371 }
372 }
373
374exit:
Amit Pundirc738b812019-06-19 16:13:49 +0530375 pthread_mutex_unlock(&out->lock);
376
Amit Pundirfec74f62020-09-28 12:43:59 +0530377 if (ret != 0) {
378 usleep((int64_t)bytes * 1000000 / audio_stream_out_frame_size(stream) /
379 out_get_sample_rate(&stream->common));
Amit Pundirc738b812019-06-19 16:13:49 +0530380 }
381
Amit Pundir648e1d02020-02-18 22:44:16 +0530382 return bytes;
Amit Pundirc738b812019-06-19 16:13:49 +0530383}
384
Amit Pundirfec74f62020-09-28 12:43:59 +0530385static int out_get_render_position(const struct audio_stream_out *stream,
386 uint32_t *dsp_frames)
387{
388 ALOGV("out_get_render_position: dsp_frames: %p", dsp_frames);
389 return -ENOSYS;
390}
391
Amit Pundirc738b812019-06-19 16:13:49 +0530392static int out_get_presentation_position(const struct audio_stream_out *stream,
393 uint64_t *frames, struct timespec *timestamp)
394{
Amit Pundir648e1d02020-02-18 22:44:16 +0530395 if (stream == NULL || frames == NULL || timestamp == NULL) {
396 return -EINVAL;
397 }
Amit Pundirfec74f62020-09-28 12:43:59 +0530398 struct alsa_stream_out* out = (struct alsa_stream_out*)stream;
Amit Pundirc738b812019-06-19 16:13:49 +0530399
Amit Pundirfec74f62020-09-28 12:43:59 +0530400 *frames = out->frames_written;
401 *timestamp = out->timestamp;
402 ALOGV("%s: frames: %" PRIu64 ", timestamp (nsec): %" PRIu64, __func__, *frames,
403 audio_utils_ns_from_timespec(timestamp));
Amit Pundirc738b812019-06-19 16:13:49 +0530404
Amit Pundir648e1d02020-02-18 22:44:16 +0530405 return 0;
Amit Pundirc738b812019-06-19 16:13:49 +0530406}
407
408
409static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
410{
Amit Pundirfec74f62020-09-28 12:43:59 +0530411 ALOGV("out_add_audio_effect: %p", effect);
Amit Pundirc738b812019-06-19 16:13:49 +0530412 return 0;
413}
414
415static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
416{
Amit Pundirfec74f62020-09-28 12:43:59 +0530417 ALOGV("out_remove_audio_effect: %p", effect);
Amit Pundirc738b812019-06-19 16:13:49 +0530418 return 0;
419}
420
421static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
Amit Pundirfec74f62020-09-28 12:43:59 +0530422 int64_t *timestamp)
Amit Pundirc738b812019-06-19 16:13:49 +0530423{
Amit Pundirfec74f62020-09-28 12:43:59 +0530424 *timestamp = 0;
425 ALOGV("out_get_next_write_timestamp: %ld", (long int)(*timestamp));
Amit Pundir648e1d02020-02-18 22:44:16 +0530426 return -ENOSYS;
Amit Pundirc738b812019-06-19 16:13:49 +0530427}
428
Amit Pundirfec74f62020-09-28 12:43:59 +0530429/** audio_stream_in implementation **/
430
431/* must be called with hw device and input stream mutexes locked */
432static int start_input_stream(struct alsa_stream_in *in)
433{
434 struct alsa_audio_device *adev = in->dev;
435 in->unavailable = true;
436 unsigned int pcm_retry_count = PCM_OPEN_RETRIES;
437
438 while (1) {
439 in->pcm = pcm_open(CARD_IN, PORT_BUILTIN_MIC, PCM_IN | PCM_MONOTONIC, &in->config);
440 if ((in->pcm != NULL) && pcm_is_ready(in->pcm)) {
441 break;
442 } else {
443 ALOGE("cannot open pcm_in driver: %s", pcm_get_error(in->pcm));
444 if (in->pcm != NULL) {
445 pcm_close(in->pcm);
446 in->pcm = NULL;
447 }
448 if (--pcm_retry_count == 0) {
449 ALOGE("Failed to open pcm_in after %d tries", PCM_OPEN_RETRIES);
450 return -ENODEV;
451 }
452 usleep(PCM_OPEN_WAIT_TIME_MS * 1000);
453 }
454 }
455 in->unavailable = false;
456 adev->active_input = in;
457 return 0;
458}
459
460static void get_mic_characteristics(struct audio_microphone_characteristic_t* mic_data,
461 size_t* mic_count) {
462 *mic_count = 1;
463 memset(mic_data, 0, sizeof(struct audio_microphone_characteristic_t));
464 strlcpy(mic_data->device_id, "builtin_mic", AUDIO_MICROPHONE_ID_MAX_LEN - 1);
465 strlcpy(mic_data->address, "top", AUDIO_DEVICE_MAX_ADDRESS_LEN - 1);
466 memset(mic_data->channel_mapping, AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED,
467 sizeof(mic_data->channel_mapping));
468 mic_data->device = AUDIO_DEVICE_IN_BUILTIN_MIC;
469 mic_data->sensitivity = -37.0;
470 mic_data->max_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
471 mic_data->min_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
472 mic_data->orientation.x = 0.0f;
473 mic_data->orientation.y = 0.0f;
474 mic_data->orientation.z = 0.0f;
475 mic_data->geometric_location.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
476 mic_data->geometric_location.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
477 mic_data->geometric_location.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
478}
479
Amit Pundirc738b812019-06-19 16:13:49 +0530480static uint32_t in_get_sample_rate(const struct audio_stream *stream)
481{
Amit Pundirfec74f62020-09-28 12:43:59 +0530482 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
483 return in->config.rate;
Amit Pundirc738b812019-06-19 16:13:49 +0530484}
485
486static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
487{
Amit Pundirfec74f62020-09-28 12:43:59 +0530488 ALOGV("in_set_sample_rate: %d", rate);
Amit Pundirc738b812019-06-19 16:13:49 +0530489 return -ENOSYS;
490}
491
Amit Pundirfec74f62020-09-28 12:43:59 +0530492static size_t get_input_buffer_size(size_t frames, audio_format_t format,
493 audio_channel_mask_t channel_mask) {
494 /* return the closest majoring multiple of 16 frames, as
495 * audioflinger expects audio buffers to be a multiple of 16 frames */
496 frames = ((frames + 15) / 16) * 16;
497 size_t bytes_per_frame = audio_channel_count_from_in_mask(channel_mask) *
498 audio_bytes_per_sample(format);
499 size_t buffer_size = frames * bytes_per_frame;
500 return buffer_size;
Amit Pundirc738b812019-06-19 16:13:49 +0530501}
502
503static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
504{
Amit Pundirfec74f62020-09-28 12:43:59 +0530505 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
506 ALOGV("in_get_channels: %d", in->config.channels);
507 return audio_channel_in_mask_from_count(in->config.channels);
Amit Pundirc738b812019-06-19 16:13:49 +0530508}
509
510static audio_format_t in_get_format(const struct audio_stream *stream)
511{
Amit Pundirfec74f62020-09-28 12:43:59 +0530512 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
513 ALOGV("in_get_format: %d", in->config.format);
514 return audio_format_from_pcm_format(in->config.format);
Amit Pundirc738b812019-06-19 16:13:49 +0530515}
516
517static int in_set_format(struct audio_stream *stream, audio_format_t format)
518{
519 return -ENOSYS;
520}
521
Amit Pundirfec74f62020-09-28 12:43:59 +0530522static size_t in_get_buffer_size(const struct audio_stream *stream)
Amit Pundirc738b812019-06-19 16:13:49 +0530523{
Amit Pundirfec74f62020-09-28 12:43:59 +0530524 struct alsa_stream_in* in = (struct alsa_stream_in*)stream;
525 size_t frames = CAPTURE_PERIOD_SIZE;
526 if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) {
527 frames = CAPTURE_PERIOD_SIZE * PLAYBACK_CODEC_SAMPLING_RATE / CAPTURE_CODEC_SAMPLING_RATE;
528 }
529
530 size_t buffer_size =
531 get_input_buffer_size(frames, stream->get_format(stream), stream->get_channels(stream));
532 ALOGV("in_get_buffer_size: %zu", buffer_size);
533 return buffer_size;
534}
535
536static int in_get_active_microphones(const struct audio_stream_in* stream,
537 struct audio_microphone_characteristic_t* mic_array,
538 size_t* mic_count) {
539 ALOGV("in_get_active_microphones");
540 if ((mic_array == NULL) || (mic_count == NULL)) {
541 return -EINVAL;
542 }
543 struct alsa_stream_in* in = (struct alsa_stream_in*)stream;
544 struct audio_hw_device* dev = (struct audio_hw_device*)in->dev;
545 bool mic_muted = false;
546 adev_get_mic_mute(dev, &mic_muted);
547 if ((in->source == AUDIO_SOURCE_ECHO_REFERENCE) || mic_muted) {
548 *mic_count = 0;
549 return 0;
550 }
551 adev_get_microphones(dev, mic_array, mic_count);
552 return 0;
553}
554
555static int do_input_standby(struct alsa_stream_in *in)
556{
557 struct alsa_audio_device *adev = in->dev;
558
559 if (!in->standby) {
560 pcm_close(in->pcm);
561 in->pcm = NULL;
562 adev->active_input = NULL;
563 in->standby = true;
564 }
565 return 0;
566}
567
568static int in_standby(struct audio_stream *stream)
569{
570 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
571 int status;
Amit Pundir648e1d02020-02-18 22:44:16 +0530572
573 pthread_mutex_lock(&in->lock);
Amit Pundirfec74f62020-09-28 12:43:59 +0530574 pthread_mutex_lock(&in->dev->lock);
575 status = do_input_standby(in);
576 pthread_mutex_unlock(&in->dev->lock);
Amit Pundir648e1d02020-02-18 22:44:16 +0530577 pthread_mutex_unlock(&in->lock);
Amit Pundirfec74f62020-09-28 12:43:59 +0530578 return status;
579}
580
581static int in_dump(const struct audio_stream *stream, int fd)
582{
583 struct alsa_stream_in* in = (struct alsa_stream_in*)stream;
584 if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) {
585 return 0;
586 }
587
588 struct audio_microphone_characteristic_t mic_array[AUDIO_MICROPHONE_MAX_COUNT];
589 size_t mic_count;
590
591 get_mic_characteristics(mic_array, &mic_count);
592
593 dprintf(fd, " Microphone count: %zd\n", mic_count);
594 size_t idx;
595 for (idx = 0; idx < mic_count; idx++) {
596 dprintf(fd, " Microphone: %zd\n", idx);
597 dprintf(fd, " Address: %s\n", mic_array[idx].address);
598 dprintf(fd, " Device: %d\n", mic_array[idx].device);
599 dprintf(fd, " Sensitivity (dB): %.2f\n", mic_array[idx].sensitivity);
600 }
601
Amit Pundirc738b812019-06-19 16:13:49 +0530602 return 0;
603}
604
605static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
606{
Amit Pundirfec74f62020-09-28 12:43:59 +0530607 return 0;
Amit Pundirc738b812019-06-19 16:13:49 +0530608}
609
610static char * in_get_parameters(const struct audio_stream *stream,
Amit Pundirfec74f62020-09-28 12:43:59 +0530611 const char *keys)
Amit Pundirc738b812019-06-19 16:13:49 +0530612{
Amit Pundirfec74f62020-09-28 12:43:59 +0530613 return strdup("");
Amit Pundirc738b812019-06-19 16:13:49 +0530614}
615
616static int in_set_gain(struct audio_stream_in *stream, float gain)
617{
618 return 0;
619}
620
Amit Pundir648e1d02020-02-18 22:44:16 +0530621static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
Amit Pundirfec74f62020-09-28 12:43:59 +0530622 size_t bytes)
Amit Pundir648e1d02020-02-18 22:44:16 +0530623{
Amit Pundirfec74f62020-09-28 12:43:59 +0530624 int ret;
625 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
626 struct alsa_audio_device *adev = in->dev;
627 size_t frame_size = audio_stream_in_frame_size(stream);
628 size_t in_frames = bytes / frame_size;
Amit Pundir648e1d02020-02-18 22:44:16 +0530629
Amit Pundirfec74f62020-09-28 12:43:59 +0530630 ALOGV("in_read: stream: %d, bytes %zu", in->source, bytes);
Amit Pundir648e1d02020-02-18 22:44:16 +0530631
Amit Pundirfec74f62020-09-28 12:43:59 +0530632 /* Special handling for Echo Reference: simply get the reference from FIFO.
633 * The format and sample rate should be specified by arguments to adev_open_input_stream. */
634 if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) {
635 struct aec_info info;
636 info.bytes = bytes;
Amit Pundir648e1d02020-02-18 22:44:16 +0530637
Amit Pundirfec74f62020-09-28 12:43:59 +0530638 const uint64_t time_increment_nsec = (uint64_t)bytes * NANOS_PER_SECOND /
639 audio_stream_in_frame_size(stream) /
640 in_get_sample_rate(&stream->common);
641 if (!aec_get_spk_running(adev->aec)) {
642 if (in->timestamp_nsec == 0) {
643 struct timespec now;
644 clock_gettime(CLOCK_MONOTONIC, &now);
645 const uint64_t timestamp_nsec = audio_utils_ns_from_timespec(&now);
646 in->timestamp_nsec = timestamp_nsec;
647 } else {
648 in->timestamp_nsec += time_increment_nsec;
649 }
650 memset(buffer, 0, bytes);
651 const uint64_t time_increment_usec = time_increment_nsec / 1000;
652 usleep(time_increment_usec);
653 } else {
654 int ref_ret = get_reference_samples(adev->aec, buffer, &info);
655 if ((ref_ret) || (info.timestamp_usec == 0)) {
656 memset(buffer, 0, bytes);
657 in->timestamp_nsec += time_increment_nsec;
658 } else {
659 in->timestamp_nsec = 1000 * info.timestamp_usec;
Amit Pundir648e1d02020-02-18 22:44:16 +0530660 }
661 }
Amit Pundirfec74f62020-09-28 12:43:59 +0530662 in->frames_read += in_frames;
Amit Pundir648e1d02020-02-18 22:44:16 +0530663
Amit Pundirfec74f62020-09-28 12:43:59 +0530664#if DEBUG_AEC
665 FILE* fp_ref = fopen("/data/local/traces/aec_ref.pcm", "a+");
666 if (fp_ref) {
667 fwrite((char*)buffer, 1, bytes, fp_ref);
668 fclose(fp_ref);
669 } else {
670 ALOGE("AEC debug: Could not open file aec_ref.pcm!");
Amit Pundir648e1d02020-02-18 22:44:16 +0530671 }
Amit Pundirfec74f62020-09-28 12:43:59 +0530672 FILE* fp_ref_ts = fopen("/data/local/traces/aec_ref_timestamps.txt", "a+");
673 if (fp_ref_ts) {
674 fprintf(fp_ref_ts, "%" PRIu64 "\n", in->timestamp_nsec);
675 fclose(fp_ref_ts);
676 } else {
677 ALOGE("AEC debug: Could not open file aec_ref_timestamps.txt!");
678 }
679#endif
680 return info.bytes;
681 }
682
683 /* Microphone input stream read */
684
685 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
686 * on the input stream mutex - e.g. executing select_mode() while holding the hw device
687 * mutex
688 */
689 pthread_mutex_lock(&in->lock);
690 pthread_mutex_lock(&adev->lock);
691 if (in->standby) {
692 ret = start_input_stream(in);
693 if (ret != 0) {
694 pthread_mutex_unlock(&adev->lock);
695 ALOGE("start_input_stream failed with code %d", ret);
696 goto exit;
697 }
698 in->standby = false;
699 }
700
701 pthread_mutex_unlock(&adev->lock);
702
703 ret = pcm_read(in->pcm, buffer, in_frames * frame_size);
704 struct aec_info info;
705 get_pcm_timestamp(in->pcm, in->config.rate, &info, false /*isOutput*/);
706 if (ret == 0) {
707 in->frames_read += in_frames;
708 in->timestamp_nsec = audio_utils_ns_from_timespec(&info.timestamp);
709 }
710 else {
711 ALOGE("pcm_read failed with code %d", ret);
Amit Pundir648e1d02020-02-18 22:44:16 +0530712 }
713
714exit:
Amit Pundir648e1d02020-02-18 22:44:16 +0530715 pthread_mutex_unlock(&in->lock);
716
Amit Pundirfec74f62020-09-28 12:43:59 +0530717 bool mic_muted = false;
718 adev_get_mic_mute((struct audio_hw_device*)adev, &mic_muted);
719 if (mic_muted) {
720 memset(buffer, 0, bytes);
721 }
722
723 if (ret != 0) {
724 usleep((int64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
725 in_get_sample_rate(&stream->common));
726 } else {
727 /* Process AEC if available */
728 /* TODO move to a separate thread */
729 if (!mic_muted) {
730 info.bytes = bytes;
731 int aec_ret = process_aec(adev->aec, buffer, &info);
732 if (aec_ret) {
733 ALOGE("process_aec returned error code %d", aec_ret);
734 }
735 }
736 }
737
738#if DEBUG_AEC && !defined(AEC_HAL)
739 FILE* fp_in = fopen("/data/local/traces/aec_in.pcm", "a+");
740 if (fp_in) {
741 fwrite((char*)buffer, 1, bytes, fp_in);
742 fclose(fp_in);
743 } else {
744 ALOGE("AEC debug: Could not open file aec_in.pcm!");
745 }
746 FILE* fp_mic_ts = fopen("/data/local/traces/aec_in_timestamps.txt", "a+");
747 if (fp_mic_ts) {
748 fprintf(fp_mic_ts, "%" PRIu64 "\n", in->timestamp_nsec);
749 fclose(fp_mic_ts);
750 } else {
751 ALOGE("AEC debug: Could not open file aec_in_timestamps.txt!");
752 }
753#endif
754
Amit Pundirc738b812019-06-19 16:13:49 +0530755 return bytes;
756}
757
Amit Pundirfec74f62020-09-28 12:43:59 +0530758static int in_get_capture_position(const struct audio_stream_in* stream, int64_t* frames,
759 int64_t* time) {
760 if (stream == NULL || frames == NULL || time == NULL) {
761 return -EINVAL;
762 }
763 struct alsa_stream_in* in = (struct alsa_stream_in*)stream;
764
765 *frames = in->frames_read;
766 *time = in->timestamp_nsec;
767 ALOGV("%s: source: %d, timestamp (nsec): %" PRIu64, __func__, in->source, *time);
768
769 return 0;
770}
771
Amit Pundirc738b812019-06-19 16:13:49 +0530772static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
773{
774 return 0;
775}
776
777static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
778{
779 return 0;
780}
781
782static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
783{
784 return 0;
785}
786
787static int adev_open_output_stream(struct audio_hw_device *dev,
Amit Pundirfec74f62020-09-28 12:43:59 +0530788 audio_io_handle_t handle,
789 audio_devices_t devices,
790 audio_output_flags_t flags,
791 struct audio_config *config,
792 struct audio_stream_out **stream_out,
793 const char *address __unused)
Amit Pundirc738b812019-06-19 16:13:49 +0530794{
Amit Pundirfec74f62020-09-28 12:43:59 +0530795 ALOGV("adev_open_output_stream...");
796
797 struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
Amit Pundirfec74f62020-09-28 12:43:59 +0530798 int out_port = get_audio_output_port(devices);
Alden DSouza0f4cb5f2021-02-11 17:07:01 -0800799 struct pcm_params* params = pcm_params_get(CARD_OUT, out_port, PCM_OUT);
800 if (!params) {
Amit Pundirfec74f62020-09-28 12:43:59 +0530801 return -ENOSYS;
Alden DSouza0f4cb5f2021-02-11 17:07:01 -0800802 }
Amit Pundir648e1d02020-02-18 22:44:16 +0530803
Alden DSouza0f4cb5f2021-02-11 17:07:01 -0800804 struct alsa_stream_out* out =
805 (struct alsa_stream_out*)calloc(1, sizeof(struct alsa_stream_out));
806 if (!out) {
Amit Pundirc738b812019-06-19 16:13:49 +0530807 return -ENOMEM;
Alden DSouza0f4cb5f2021-02-11 17:07:01 -0800808 }
Amit Pundirc738b812019-06-19 16:13:49 +0530809
810 out->stream.common.get_sample_rate = out_get_sample_rate;
811 out->stream.common.set_sample_rate = out_set_sample_rate;
812 out->stream.common.get_buffer_size = out_get_buffer_size;
813 out->stream.common.get_channels = out_get_channels;
814 out->stream.common.get_format = out_get_format;
815 out->stream.common.set_format = out_set_format;
816 out->stream.common.standby = out_standby;
817 out->stream.common.dump = out_dump;
818 out->stream.common.set_parameters = out_set_parameters;
819 out->stream.common.get_parameters = out_get_parameters;
820 out->stream.common.add_audio_effect = out_add_audio_effect;
821 out->stream.common.remove_audio_effect = out_remove_audio_effect;
822 out->stream.get_latency = out_get_latency;
823 out->stream.set_volume = out_set_volume;
824 out->stream.write = out_write;
825 out->stream.get_render_position = out_get_render_position;
Amit Pundir648e1d02020-02-18 22:44:16 +0530826 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Amit Pundirfec74f62020-09-28 12:43:59 +0530827 out->stream.get_presentation_position = out_get_presentation_position;
Amit Pundirc738b812019-06-19 16:13:49 +0530828
Amit Pundirfec74f62020-09-28 12:43:59 +0530829 out->config.channels = CHANNEL_STEREO;
830 out->config.rate = PLAYBACK_CODEC_SAMPLING_RATE;
831 out->config.format = PCM_FORMAT_S16_LE;
832 out->config.period_size = PLAYBACK_PERIOD_SIZE;
833 out->config.period_count = PLAYBACK_PERIOD_COUNT;
Amit Pundirc738b812019-06-19 16:13:49 +0530834
Amit Pundirfec74f62020-09-28 12:43:59 +0530835 if (out->config.rate != config->sample_rate ||
836 audio_channel_count_from_out_mask(config->channel_mask) != CHANNEL_STEREO ||
837 out->config.format != pcm_format_from_audio_format(config->format) ) {
838 config->sample_rate = out->config.rate;
839 config->format = audio_format_from_pcm_format(out->config.format);
840 config->channel_mask = audio_channel_out_mask_from_count(CHANNEL_STEREO);
Alden DSouza0f4cb5f2021-02-11 17:07:01 -0800841 goto error_1;
Amit Pundirc738b812019-06-19 16:13:49 +0530842 }
843
Amit Pundirfec74f62020-09-28 12:43:59 +0530844 ALOGI("adev_open_output_stream selects channels=%d rate=%d format=%d, devices=%d",
845 out->config.channels, out->config.rate, out->config.format, devices);
846
847 out->dev = ladev;
848 out->standby = 1;
849 out->unavailable = false;
850 out->devices = devices;
851
852 config->format = out_get_format(&out->stream.common);
853 config->channel_mask = out_get_channels(&out->stream.common);
854 config->sample_rate = out_get_sample_rate(&out->stream.common);
Amit Pundirc738b812019-06-19 16:13:49 +0530855
Amit Pundirfec74f62020-09-28 12:43:59 +0530856 out->speaker_eq = NULL;
857 if (out_port == PORT_INTERNAL_SPEAKER) {
858 out_set_eq(out);
859 if (out->speaker_eq == NULL) {
860 ALOGE("%s: Failed to initialize speaker EQ", __func__);
861 }
862 }
863
Alden DSouza0f4cb5f2021-02-11 17:07:01 -0800864 int aec_ret = init_aec_reference_config(ladev->aec, out);
865 if (aec_ret) {
866 ALOGE("AEC: Speaker config init failed!");
867 goto error_2;
Amit Pundirfec74f62020-09-28 12:43:59 +0530868 }
Amit Pundirc738b812019-06-19 16:13:49 +0530869
Alden DSouza0f4cb5f2021-02-11 17:07:01 -0800870 *stream_out = &out->stream;
871 return 0;
872
873error_2:
874 fir_release(out->speaker_eq);
875error_1:
876 free(out);
877 return -EINVAL;
Amit Pundirc738b812019-06-19 16:13:49 +0530878}
879
880static void adev_close_output_stream(struct audio_hw_device *dev,
Amit Pundirfec74f62020-09-28 12:43:59 +0530881 struct audio_stream_out *stream)
Amit Pundirc738b812019-06-19 16:13:49 +0530882{
Amit Pundirfec74f62020-09-28 12:43:59 +0530883 ALOGV("adev_close_output_stream...");
884 struct alsa_audio_device *adev = (struct alsa_audio_device *)dev;
885 destroy_aec_reference_config(adev->aec);
886 struct alsa_stream_out* out = (struct alsa_stream_out*)stream;
887 fir_release(out->speaker_eq);
Amit Pundirc738b812019-06-19 16:13:49 +0530888 free(stream);
889}
890
891static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
892{
Amit Pundirfec74f62020-09-28 12:43:59 +0530893 ALOGV("adev_set_parameters");
894 return -ENOSYS;
Amit Pundirc738b812019-06-19 16:13:49 +0530895}
896
897static char * adev_get_parameters(const struct audio_hw_device *dev,
Amit Pundirfec74f62020-09-28 12:43:59 +0530898 const char *keys)
Amit Pundirc738b812019-06-19 16:13:49 +0530899{
Amit Pundirfec74f62020-09-28 12:43:59 +0530900 ALOGV("adev_get_parameters");
Amit Pundirc738b812019-06-19 16:13:49 +0530901 return strdup("");
902}
903
Amit Pundirfec74f62020-09-28 12:43:59 +0530904static int adev_get_microphones(const struct audio_hw_device* dev,
905 struct audio_microphone_characteristic_t* mic_array,
906 size_t* mic_count) {
907 ALOGV("adev_get_microphones");
908 if ((mic_array == NULL) || (mic_count == NULL)) {
909 return -EINVAL;
910 }
911 get_mic_characteristics(mic_array, mic_count);
912 return 0;
913}
914
Amit Pundirc738b812019-06-19 16:13:49 +0530915static int adev_init_check(const struct audio_hw_device *dev)
916{
Amit Pundirfec74f62020-09-28 12:43:59 +0530917 ALOGV("adev_init_check");
Amit Pundirc738b812019-06-19 16:13:49 +0530918 return 0;
919}
920
921static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
922{
Amit Pundirfec74f62020-09-28 12:43:59 +0530923 ALOGV("adev_set_voice_volume: %f", volume);
924 return -ENOSYS;
Amit Pundirc738b812019-06-19 16:13:49 +0530925}
926
927static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
928{
Amit Pundirfec74f62020-09-28 12:43:59 +0530929 ALOGV("adev_set_master_volume: %f", volume);
Amit Pundirc738b812019-06-19 16:13:49 +0530930 return -ENOSYS;
931}
932
933static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
934{
Amit Pundirfec74f62020-09-28 12:43:59 +0530935 ALOGV("adev_get_master_volume: %f", *volume);
Amit Pundirc738b812019-06-19 16:13:49 +0530936 return -ENOSYS;
937}
938
939static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
940{
Amit Pundirfec74f62020-09-28 12:43:59 +0530941 ALOGV("adev_set_master_mute: %d", muted);
Amit Pundirc738b812019-06-19 16:13:49 +0530942 return -ENOSYS;
943}
944
945static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
946{
Amit Pundirfec74f62020-09-28 12:43:59 +0530947 ALOGV("adev_get_master_mute: %d", *muted);
Amit Pundirc738b812019-06-19 16:13:49 +0530948 return -ENOSYS;
949}
950
951static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
952{
Amit Pundirfec74f62020-09-28 12:43:59 +0530953 ALOGV("adev_set_mode: %d", mode);
Amit Pundirc738b812019-06-19 16:13:49 +0530954 return 0;
955}
956
957static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
958{
Amit Pundirfec74f62020-09-28 12:43:59 +0530959 ALOGV("adev_set_mic_mute: %d",state);
960 struct alsa_audio_device *adev = (struct alsa_audio_device *)dev;
Amit Pundir648e1d02020-02-18 22:44:16 +0530961 pthread_mutex_lock(&adev->lock);
962 adev->mic_mute = state;
963 pthread_mutex_unlock(&adev->lock);
964 return 0;
Amit Pundirc738b812019-06-19 16:13:49 +0530965}
966
967static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
968{
Amit Pundirfec74f62020-09-28 12:43:59 +0530969 ALOGV("adev_get_mic_mute");
970 struct alsa_audio_device *adev = (struct alsa_audio_device *)dev;
Amit Pundir648e1d02020-02-18 22:44:16 +0530971 pthread_mutex_lock(&adev->lock);
972 *state = adev->mic_mute;
973 pthread_mutex_unlock(&adev->lock);
974 return 0;
Amit Pundirc738b812019-06-19 16:13:49 +0530975}
976
977static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
Amit Pundirfec74f62020-09-28 12:43:59 +0530978 const struct audio_config *config)
Amit Pundirc738b812019-06-19 16:13:49 +0530979{
Amit Pundirfec74f62020-09-28 12:43:59 +0530980 size_t buffer_size =
981 get_input_buffer_size(CAPTURE_PERIOD_SIZE, config->format, config->channel_mask);
982 ALOGV("adev_get_input_buffer_size: %zu", buffer_size);
983 return buffer_size;
Amit Pundirc738b812019-06-19 16:13:49 +0530984}
985
Amit Pundirfec74f62020-09-28 12:43:59 +0530986static int adev_open_input_stream(struct audio_hw_device* dev, audio_io_handle_t handle,
987 audio_devices_t devices, struct audio_config* config,
988 struct audio_stream_in** stream_in,
989 audio_input_flags_t flags __unused, const char* address __unused,
990 audio_source_t source) {
991 ALOGV("adev_open_input_stream...");
Amit Pundir648e1d02020-02-18 22:44:16 +0530992
Amit Pundirfec74f62020-09-28 12:43:59 +0530993 struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
Amit Pundir648e1d02020-02-18 22:44:16 +0530994
Alden DSouza0f4cb5f2021-02-11 17:07:01 -0800995 struct pcm_params* params = pcm_params_get(CARD_IN, PORT_BUILTIN_MIC, PCM_IN);
996 if (!params) {
Amit Pundirfec74f62020-09-28 12:43:59 +0530997 return -ENOSYS;
Alden DSouza0f4cb5f2021-02-11 17:07:01 -0800998 }
Amit Pundirfec74f62020-09-28 12:43:59 +0530999
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001000 struct alsa_stream_in* in = (struct alsa_stream_in*)calloc(1, sizeof(struct alsa_stream_in));
1001 if (!in) {
Amit Pundirfec74f62020-09-28 12:43:59 +05301002 return -ENOMEM;
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001003 }
Amit Pundirc738b812019-06-19 16:13:49 +05301004
1005 in->stream.common.get_sample_rate = in_get_sample_rate;
Amit Pundirfec74f62020-09-28 12:43:59 +05301006 in->stream.common.set_sample_rate = in_set_sample_rate;
Amit Pundirc738b812019-06-19 16:13:49 +05301007 in->stream.common.get_buffer_size = in_get_buffer_size;
1008 in->stream.common.get_channels = in_get_channels;
1009 in->stream.common.get_format = in_get_format;
Amit Pundirfec74f62020-09-28 12:43:59 +05301010 in->stream.common.set_format = in_set_format;
Amit Pundirc738b812019-06-19 16:13:49 +05301011 in->stream.common.standby = in_standby;
1012 in->stream.common.dump = in_dump;
1013 in->stream.common.set_parameters = in_set_parameters;
1014 in->stream.common.get_parameters = in_get_parameters;
Amit Pundirfec74f62020-09-28 12:43:59 +05301015 in->stream.common.add_audio_effect = in_add_audio_effect;
1016 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1017 in->stream.set_gain = in_set_gain;
Amit Pundirc738b812019-06-19 16:13:49 +05301018 in->stream.read = in_read;
Amit Pundirfec74f62020-09-28 12:43:59 +05301019 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Amit Pundir648e1d02020-02-18 22:44:16 +05301020 in->stream.get_capture_position = in_get_capture_position;
1021 in->stream.get_active_microphones = in_get_active_microphones;
1022
Amit Pundirfec74f62020-09-28 12:43:59 +05301023 in->config.channels = CHANNEL_STEREO;
1024 if (source == AUDIO_SOURCE_ECHO_REFERENCE) {
1025 in->config.rate = PLAYBACK_CODEC_SAMPLING_RATE;
1026 } else {
1027 in->config.rate = CAPTURE_CODEC_SAMPLING_RATE;
1028 }
1029 in->config.format = PCM_FORMAT_S32_LE;
1030 in->config.period_size = CAPTURE_PERIOD_SIZE;
1031 in->config.period_count = CAPTURE_PERIOD_COUNT;
Amit Pundir648e1d02020-02-18 22:44:16 +05301032
Amit Pundirfec74f62020-09-28 12:43:59 +05301033 if (in->config.rate != config->sample_rate ||
1034 audio_channel_count_from_in_mask(config->channel_mask) != CHANNEL_STEREO ||
1035 in->config.format != pcm_format_from_audio_format(config->format) ) {
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001036 config->format = in_get_format(&in->stream.common);
1037 config->channel_mask = in_get_channels(&in->stream.common);
1038 config->sample_rate = in_get_sample_rate(&in->stream.common);
1039 goto error_1;
Amit Pundirfec74f62020-09-28 12:43:59 +05301040 }
Amit Pundir648e1d02020-02-18 22:44:16 +05301041
Amit Pundirfec74f62020-09-28 12:43:59 +05301042 ALOGI("adev_open_input_stream selects channels=%d rate=%d format=%d source=%d",
1043 in->config.channels, in->config.rate, in->config.format, source);
1044
1045 in->dev = ladev;
Amit Pundir648e1d02020-02-18 22:44:16 +05301046 in->standby = true;
Amit Pundirfec74f62020-09-28 12:43:59 +05301047 in->unavailable = false;
1048 in->source = source;
1049 in->devices = devices;
Amit Pundir648e1d02020-02-18 22:44:16 +05301050
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001051 if (is_aec_input(in)) {
Amit Pundirfec74f62020-09-28 12:43:59 +05301052 int aec_ret = init_aec_mic_config(ladev->aec, in);
1053 if (aec_ret) {
1054 ALOGE("AEC: Mic config init failed!");
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001055 goto error_1;
Amit Pundir648e1d02020-02-18 22:44:16 +05301056 }
Amit Pundirfec74f62020-09-28 12:43:59 +05301057 }
1058
Amit Pundirfec74f62020-09-28 12:43:59 +05301059#if DEBUG_AEC
1060 remove("/data/local/traces/aec_ref.pcm");
1061 remove("/data/local/traces/aec_in.pcm");
1062 remove("/data/local/traces/aec_ref_timestamps.txt");
1063 remove("/data/local/traces/aec_in_timestamps.txt");
1064#endif
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001065
1066 *stream_in = &in->stream;
1067 return 0;
1068
1069error_1:
1070 free(in);
1071 return -EINVAL;
Amit Pundirfec74f62020-09-28 12:43:59 +05301072}
Amit Pundir648e1d02020-02-18 22:44:16 +05301073
Amit Pundirfec74f62020-09-28 12:43:59 +05301074static void adev_close_input_stream(struct audio_hw_device *dev,
1075 struct audio_stream_in *stream)
1076{
1077 ALOGV("adev_close_input_stream...");
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001078 struct alsa_stream_in* in = (struct alsa_stream_in*)stream;
1079 if (is_aec_input(in)) {
1080 destroy_aec_mic_config(in->dev->aec);
1081 }
Amit Pundirfec74f62020-09-28 12:43:59 +05301082 free(stream);
1083 return;
1084}
Amit Pundir648e1d02020-02-18 22:44:16 +05301085
Amit Pundirfec74f62020-09-28 12:43:59 +05301086static int adev_dump(const audio_hw_device_t *device, int fd)
1087{
1088 ALOGV("adev_dump");
Amit Pundirc738b812019-06-19 16:13:49 +05301089 return 0;
1090}
1091
Amit Pundirfec74f62020-09-28 12:43:59 +05301092static int adev_close(hw_device_t *device)
Amit Pundir648e1d02020-02-18 22:44:16 +05301093{
Amit Pundirfec74f62020-09-28 12:43:59 +05301094 ALOGV("adev_close");
Amit Pundir648e1d02020-02-18 22:44:16 +05301095
Amit Pundirfec74f62020-09-28 12:43:59 +05301096 struct alsa_audio_device *adev = (struct alsa_audio_device *)device;
1097 release_aec(adev->aec);
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001098 audio_route_free(adev->audio_route);
1099 mixer_close(adev->mixer);
Amit Pundirfec74f62020-09-28 12:43:59 +05301100 free(device);
1101 return 0;
Amit Pundir648e1d02020-02-18 22:44:16 +05301102}
1103
Amit Pundirc738b812019-06-19 16:13:49 +05301104static int adev_open(const hw_module_t* module, const char* name,
Amit Pundirfec74f62020-09-28 12:43:59 +05301105 hw_device_t** device)
Amit Pundirc738b812019-06-19 16:13:49 +05301106{
Amit Pundirfec74f62020-09-28 12:43:59 +05301107 ALOGV("adev_open: %s", name);
Amit Pundirc738b812019-06-19 16:13:49 +05301108
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001109 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) {
Amit Pundirc738b812019-06-19 16:13:49 +05301110 return -EINVAL;
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001111 }
Amit Pundirc738b812019-06-19 16:13:49 +05301112
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001113 struct alsa_audio_device* adev = calloc(1, sizeof(struct alsa_audio_device));
1114 if (!adev) {
Amit Pundirfec74f62020-09-28 12:43:59 +05301115 return -ENOMEM;
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001116 }
Amit Pundirc738b812019-06-19 16:13:49 +05301117
Amit Pundirfec74f62020-09-28 12:43:59 +05301118 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
1119 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
1120 adev->hw_device.common.module = (struct hw_module_t *) module;
1121 adev->hw_device.common.close = adev_close;
1122 adev->hw_device.init_check = adev_init_check;
1123 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1124 adev->hw_device.set_master_volume = adev_set_master_volume;
1125 adev->hw_device.get_master_volume = adev_get_master_volume;
1126 adev->hw_device.set_master_mute = adev_set_master_mute;
1127 adev->hw_device.get_master_mute = adev_get_master_mute;
1128 adev->hw_device.set_mode = adev_set_mode;
1129 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1130 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1131 adev->hw_device.set_parameters = adev_set_parameters;
1132 adev->hw_device.get_parameters = adev_get_parameters;
1133 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1134 adev->hw_device.open_output_stream = adev_open_output_stream;
1135 adev->hw_device.close_output_stream = adev_close_output_stream;
1136 adev->hw_device.open_input_stream = adev_open_input_stream;
1137 adev->hw_device.close_input_stream = adev_close_input_stream;
1138 adev->hw_device.dump = adev_dump;
1139 adev->hw_device.get_microphones = adev_get_microphones;
Amit Pundirc738b812019-06-19 16:13:49 +05301140
Amit Pundirfec74f62020-09-28 12:43:59 +05301141 *device = &adev->hw_device.common;
Amit Pundirc738b812019-06-19 16:13:49 +05301142
Amit Pundirfec74f62020-09-28 12:43:59 +05301143 adev->mixer = mixer_open(CARD_OUT);
Amit Pundirfec74f62020-09-28 12:43:59 +05301144 if (!adev->mixer) {
1145 ALOGE("Unable to open the mixer, aborting.");
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001146 goto error_1;
Amit Pundir648e1d02020-02-18 22:44:16 +05301147 }
1148
Amit Pundirfec74f62020-09-28 12:43:59 +05301149 adev->audio_route = audio_route_init(CARD_OUT, MIXER_XML_PATH);
1150 if (!adev->audio_route) {
1151 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001152 goto error_2;
Amit Pundirfec74f62020-09-28 12:43:59 +05301153 }
Amit Pundir648e1d02020-02-18 22:44:16 +05301154
Amit Pundirfec74f62020-09-28 12:43:59 +05301155 pthread_mutex_lock(&adev->lock);
1156 if (init_aec(CAPTURE_CODEC_SAMPLING_RATE, NUM_AEC_REFERENCE_CHANNELS,
1157 CHANNEL_STEREO, &adev->aec)) {
1158 pthread_mutex_unlock(&adev->lock);
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001159 goto error_3;
Amit Pundirfec74f62020-09-28 12:43:59 +05301160 }
1161 pthread_mutex_unlock(&adev->lock);
1162
Amit Pundirc738b812019-06-19 16:13:49 +05301163 return 0;
Alden DSouza0f4cb5f2021-02-11 17:07:01 -08001164
1165error_3:
1166 audio_route_free(adev->audio_route);
1167error_2:
1168 mixer_close(adev->mixer);
1169error_1:
1170 free(adev);
1171 return -EINVAL;
Amit Pundirc738b812019-06-19 16:13:49 +05301172}
1173
1174static struct hw_module_methods_t hal_module_methods = {
1175 .open = adev_open,
1176};
1177
1178struct audio_module HAL_MODULE_INFO_SYM = {
1179 .common = {
1180 .tag = HARDWARE_MODULE_TAG,
1181 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1182 .hal_api_version = HARDWARE_HAL_API_VERSION,
1183 .id = AUDIO_HARDWARE_MODULE_ID,
Amit Pundir648e1d02020-02-18 22:44:16 +05301184 .name = "Generic audio HW HAL",
Amit Pundirc738b812019-06-19 16:13:49 +05301185 .author = "The Android Open Source Project",
1186 .methods = &hal_module_methods,
1187 },
1188};