blob: 4a16ac15a96b3ea997f25c9e8a6ca5e7b5f4cc38 [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
Amit Pundire6732bb2020-09-28 12:43:59 +053060static int get_audio_output_port(audio_devices_t devices) {
61 /* Only HDMI out for now #FIXME */
62 return PORT_HDMI;
Amit Pundir4e375822019-04-18 16:46:10 +053063}
64
Amit Pundire6732bb2020-09-28 12:43:59 +053065static void timestamp_adjust(struct timespec* ts, ssize_t frames, uint32_t sampling_rate) {
66 /* This function assumes the adjustment (in nsec) is less than the max value of long,
67 * which for 32-bit long this is 2^31 * 1e-9 seconds, slightly over 2 seconds.
68 * For 64-bit long it is 9e+9 seconds. */
69 long adj_nsec = (frames / (float) sampling_rate) * 1E9L;
70 ts->tv_nsec += adj_nsec;
71 while (ts->tv_nsec > 1E9L) {
72 ts->tv_sec++;
73 ts->tv_nsec -= 1E9L;
Amit Pundir456949d2020-02-18 22:44:16 +053074 }
Amit Pundire6732bb2020-09-28 12:43:59 +053075 if (ts->tv_nsec < 0) {
76 ts->tv_sec--;
77 ts->tv_nsec += 1E9L;
78 }
Amit Pundir456949d2020-02-18 22:44:16 +053079}
80
Amit Pundire6732bb2020-09-28 12:43:59 +053081/* Helper function to get PCM hardware timestamp.
82 * Only the field 'timestamp' of argument 'ts' is updated. */
83static int get_pcm_timestamp(struct pcm* pcm, uint32_t sample_rate, struct aec_info* info,
84 bool isOutput) {
85 int ret = 0;
86 if (pcm_get_htimestamp(pcm, &info->available, &info->timestamp) < 0) {
87 ALOGE("Error getting PCM timestamp!");
88 info->timestamp.tv_sec = 0;
89 info->timestamp.tv_nsec = 0;
Amit Pundir456949d2020-02-18 22:44:16 +053090 return -EINVAL;
91 }
Amit Pundire6732bb2020-09-28 12:43:59 +053092 ssize_t frames;
93 if (isOutput) {
94 frames = pcm_get_buffer_size(pcm) - info->available;
95 } else {
96 frames = -info->available; /* rewind timestamp */
97 }
98 timestamp_adjust(&info->timestamp, frames, sample_rate);
99 return ret;
Amit Pundir456949d2020-02-18 22:44:16 +0530100}
101
Amit Pundire6732bb2020-09-28 12:43:59 +0530102static int read_filter_from_file(const char* filename, int16_t* filter, int max_length) {
103 FILE* fp = fopen(filename, "r");
104 if (fp == NULL) {
105 ALOGI("%s: File %s not found.", __func__, filename);
106 return 0;
107 }
108 int num_taps = 0;
109 char* line = NULL;
110 size_t len = 0;
111 while (!feof(fp)) {
112 size_t size = getline(&line, &len, fp);
113 if ((line[0] == '#') || (size < 2)) {
114 continue;
115 }
116 int n = sscanf(line, "%" SCNd16 "\n", &filter[num_taps++]);
117 if (n < 1) {
118 ALOGE("Could not find coefficient %d! Exiting...", num_taps - 1);
119 return 0;
120 }
121 ALOGV("Coeff %d : %" PRId16, num_taps, filter[num_taps - 1]);
122 if (num_taps == max_length) {
123 ALOGI("%s: max tap length %d reached.", __func__, max_length);
Amit Pundir456949d2020-02-18 22:44:16 +0530124 break;
125 }
Amit Pundir456949d2020-02-18 22:44:16 +0530126 }
Amit Pundire6732bb2020-09-28 12:43:59 +0530127 free(line);
128 fclose(fp);
129 return num_taps;
Amit Pundir456949d2020-02-18 22:44:16 +0530130}
131
Amit Pundire6732bb2020-09-28 12:43:59 +0530132static void out_set_eq(struct alsa_stream_out* out) {
133 out->speaker_eq = NULL;
134 int16_t* speaker_eq_coeffs = (int16_t*)calloc(SPEAKER_MAX_EQ_LENGTH, sizeof(int16_t));
135 if (speaker_eq_coeffs == NULL) {
136 ALOGE("%s: Failed to allocate speaker EQ", __func__);
137 return;
138 }
139 int num_taps = read_filter_from_file(SPEAKER_EQ_FILE, speaker_eq_coeffs, SPEAKER_MAX_EQ_LENGTH);
140 if (num_taps == 0) {
141 ALOGI("%s: Empty filter file or 0 taps set.", __func__);
142 free(speaker_eq_coeffs);
143 return;
144 }
145 out->speaker_eq = fir_init(
146 out->config.channels, FIR_SINGLE_FILTER, num_taps,
147 out_get_buffer_size(&out->stream.common) / out->config.channels / sizeof(int16_t),
148 speaker_eq_coeffs);
149 free(speaker_eq_coeffs);
150}
Amit Pundir456949d2020-02-18 22:44:16 +0530151
Amit Pundire6732bb2020-09-28 12:43:59 +0530152/* must be called with hw device and output stream mutexes locked */
153static int start_output_stream(struct alsa_stream_out *out)
154{
155 struct alsa_audio_device *adev = out->dev;
156
157 /* default to low power: will be corrected in out_write if necessary before first write to
158 * tinyalsa.
159 */
160 out->write_threshold = PLAYBACK_PERIOD_COUNT * PLAYBACK_PERIOD_SIZE;
161 out->config.start_threshold = PLAYBACK_PERIOD_START_THRESHOLD * PLAYBACK_PERIOD_SIZE;
162 out->config.avail_min = PLAYBACK_PERIOD_SIZE;
163 out->unavailable = true;
164 unsigned int pcm_retry_count = PCM_OPEN_RETRIES;
165 int out_port = get_audio_output_port(out->devices);
166
167 while (1) {
168 out->pcm = pcm_open(CARD_OUT, out_port, PCM_OUT | PCM_MONOTONIC, &out->config);
169 if ((out->pcm != NULL) && pcm_is_ready(out->pcm)) {
Amit Pundir456949d2020-02-18 22:44:16 +0530170 break;
Amit Pundire6732bb2020-09-28 12:43:59 +0530171 } else {
172 ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm));
173 if (out->pcm != NULL) {
174 pcm_close(out->pcm);
175 out->pcm = NULL;
176 }
177 if (--pcm_retry_count == 0) {
178 ALOGE("Failed to open pcm_out after %d tries", PCM_OPEN_RETRIES);
179 return -ENODEV;
180 }
181 usleep(PCM_OPEN_WAIT_TIME_MS * 1000);
Amit Pundir456949d2020-02-18 22:44:16 +0530182 }
Amit Pundir456949d2020-02-18 22:44:16 +0530183 }
Amit Pundire6732bb2020-09-28 12:43:59 +0530184 out->unavailable = false;
185 adev->active_output = out;
186 return 0;
Amit Pundir456949d2020-02-18 22:44:16 +0530187}
188
Amit Pundir4e375822019-04-18 16:46:10 +0530189static uint32_t out_get_sample_rate(const struct audio_stream *stream)
190{
Amit Pundire6732bb2020-09-28 12:43:59 +0530191 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
192 return out->config.rate;
Amit Pundir4e375822019-04-18 16:46:10 +0530193}
194
195static int out_set_sample_rate(struct audio_stream *stream, uint32_t rate)
196{
Amit Pundire6732bb2020-09-28 12:43:59 +0530197 ALOGV("out_set_sample_rate: %d", 0);
Amit Pundir4e375822019-04-18 16:46:10 +0530198 return -ENOSYS;
199}
200
201static size_t out_get_buffer_size(const struct audio_stream *stream)
202{
Amit Pundire6732bb2020-09-28 12:43:59 +0530203 ALOGV("out_get_buffer_size: %d", 4096);
Amit Pundir4e375822019-04-18 16:46:10 +0530204
Amit Pundire6732bb2020-09-28 12:43:59 +0530205 /* return the closest majoring multiple of 16 frames, as
206 * audioflinger expects audio buffers to be a multiple of 16 frames */
207 size_t size = PLAYBACK_PERIOD_SIZE;
208 size = ((size + 15) / 16) * 16;
209 return size * audio_stream_out_frame_size((struct audio_stream_out *)stream);
Amit Pundir4e375822019-04-18 16:46:10 +0530210}
211
212static audio_channel_mask_t out_get_channels(const struct audio_stream *stream)
213{
Amit Pundire6732bb2020-09-28 12:43:59 +0530214 ALOGV("out_get_channels");
215 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
216 return audio_channel_out_mask_from_count(out->config.channels);
Amit Pundir4e375822019-04-18 16:46:10 +0530217}
218
219static audio_format_t out_get_format(const struct audio_stream *stream)
220{
Amit Pundire6732bb2020-09-28 12:43:59 +0530221 ALOGV("out_get_format");
222 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
223 return audio_format_from_pcm_format(out->config.format);
Amit Pundir4e375822019-04-18 16:46:10 +0530224}
225
226static int out_set_format(struct audio_stream *stream, audio_format_t format)
227{
Amit Pundire6732bb2020-09-28 12:43:59 +0530228 ALOGV("out_set_format: %d",format);
Amit Pundir4e375822019-04-18 16:46:10 +0530229 return -ENOSYS;
230}
231
Amit Pundire6732bb2020-09-28 12:43:59 +0530232static int do_output_standby(struct alsa_stream_out *out)
233{
234 struct alsa_audio_device *adev = out->dev;
235
236 fir_reset(out->speaker_eq);
237
238 if (!out->standby) {
239 pcm_close(out->pcm);
240 out->pcm = NULL;
241 adev->active_output = NULL;
242 out->standby = 1;
243 }
244 aec_set_spk_running(adev->aec, false);
245 return 0;
246}
247
248static int out_standby(struct audio_stream *stream)
249{
250 ALOGV("out_standby");
251 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
252 int status;
253
254 pthread_mutex_lock(&out->dev->lock);
255 pthread_mutex_lock(&out->lock);
256 status = do_output_standby(out);
257 pthread_mutex_unlock(&out->lock);
258 pthread_mutex_unlock(&out->dev->lock);
259 return status;
260}
261
Amit Pundir4e375822019-04-18 16:46:10 +0530262static int out_dump(const struct audio_stream *stream, int fd)
263{
Amit Pundire6732bb2020-09-28 12:43:59 +0530264 ALOGV("out_dump");
Amit Pundir4e375822019-04-18 16:46:10 +0530265 return 0;
266}
267
268static int out_set_parameters(struct audio_stream *stream, const char *kvpairs)
269{
Amit Pundire6732bb2020-09-28 12:43:59 +0530270 ALOGV("out_set_parameters");
271 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
272 struct alsa_audio_device *adev = out->dev;
Amit Pundir4e375822019-04-18 16:46:10 +0530273 struct str_parms *parms;
274 char value[32];
Amit Pundire6732bb2020-09-28 12:43:59 +0530275 int ret, val = 0;
Amit Pundir4e375822019-04-18 16:46:10 +0530276
277 parms = str_parms_create_str(kvpairs);
278
Amit Pundire6732bb2020-09-28 12:43:59 +0530279 ret = str_parms_get_str(parms, AUDIO_PARAMETER_STREAM_ROUTING, value, sizeof(value));
280 if (ret >= 0) {
281 val = atoi(value);
282 pthread_mutex_lock(&adev->lock);
283 pthread_mutex_lock(&out->lock);
284 if (((out->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) {
285 out->devices &= ~AUDIO_DEVICE_OUT_ALL;
286 out->devices |= val;
287 }
288 pthread_mutex_unlock(&out->lock);
289 pthread_mutex_unlock(&adev->lock);
Amit Pundir4e375822019-04-18 16:46:10 +0530290 }
291
292 str_parms_destroy(parms);
Amit Pundire6732bb2020-09-28 12:43:59 +0530293 return 0;
Amit Pundir4e375822019-04-18 16:46:10 +0530294}
295
296static char * out_get_parameters(const struct audio_stream *stream, const char *keys)
297{
Amit Pundire6732bb2020-09-28 12:43:59 +0530298 ALOGV("out_get_parameters");
299 return strdup("");
Amit Pundir4e375822019-04-18 16:46:10 +0530300}
301
302static uint32_t out_get_latency(const struct audio_stream_out *stream)
303{
Amit Pundire6732bb2020-09-28 12:43:59 +0530304 ALOGV("out_get_latency");
305 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
306 return (PLAYBACK_PERIOD_SIZE * PLAYBACK_PERIOD_COUNT * 1000) / out->config.rate;
Amit Pundir4e375822019-04-18 16:46:10 +0530307}
308
309static int out_set_volume(struct audio_stream_out *stream, float left,
Amit Pundire6732bb2020-09-28 12:43:59 +0530310 float right)
Amit Pundir4e375822019-04-18 16:46:10 +0530311{
Amit Pundire6732bb2020-09-28 12:43:59 +0530312 ALOGV("out_set_volume: Left:%f Right:%f", left, right);
Amit Pundir456949d2020-02-18 22:44:16 +0530313 return -ENOSYS;
Amit Pundir4e375822019-04-18 16:46:10 +0530314}
315
Amit Pundire6732bb2020-09-28 12:43:59 +0530316static ssize_t out_write(struct audio_stream_out *stream, const void* buffer,
317 size_t bytes)
Amit Pundir4e375822019-04-18 16:46:10 +0530318{
Amit Pundire6732bb2020-09-28 12:43:59 +0530319 int ret;
320 struct alsa_stream_out *out = (struct alsa_stream_out *)stream;
321 struct alsa_audio_device *adev = out->dev;
322 size_t frame_size = audio_stream_out_frame_size(stream);
323 size_t out_frames = bytes / frame_size;
Amit Pundir456949d2020-02-18 22:44:16 +0530324
Amit Pundire6732bb2020-09-28 12:43:59 +0530325 ALOGV("%s: devices: %d, bytes %zu", __func__, out->devices, bytes);
Amit Pundir456949d2020-02-18 22:44:16 +0530326
Amit Pundire6732bb2020-09-28 12:43:59 +0530327 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
328 * on the output stream mutex - e.g. executing select_mode() while holding the hw device
329 * mutex
330 */
331 pthread_mutex_lock(&adev->lock);
Amit Pundir456949d2020-02-18 22:44:16 +0530332 pthread_mutex_lock(&out->lock);
Amit Pundir456949d2020-02-18 22:44:16 +0530333 if (out->standby) {
Amit Pundire6732bb2020-09-28 12:43:59 +0530334 ret = start_output_stream(out);
335 if (ret != 0) {
336 pthread_mutex_unlock(&adev->lock);
337 goto exit;
338 }
339 out->standby = 0;
340 aec_set_spk_running(adev->aec, true);
Amit Pundir456949d2020-02-18 22:44:16 +0530341 }
342
Amit Pundire6732bb2020-09-28 12:43:59 +0530343 pthread_mutex_unlock(&adev->lock);
Amit Pundir456949d2020-02-18 22:44:16 +0530344
Amit Pundire6732bb2020-09-28 12:43:59 +0530345 if (out->speaker_eq != NULL) {
346 fir_process_interleaved(out->speaker_eq, (int16_t*)buffer, (int16_t*)buffer, out_frames);
Amit Pundir456949d2020-02-18 22:44:16 +0530347 }
Amit Pundir456949d2020-02-18 22:44:16 +0530348
Amit Pundire6732bb2020-09-28 12:43:59 +0530349 ret = pcm_write(out->pcm, buffer, out_frames * frame_size);
350 if (ret == 0) {
351 out->frames_written += out_frames;
352
353 struct aec_info info;
354 get_pcm_timestamp(out->pcm, out->config.rate, &info, true /*isOutput*/);
355 out->timestamp = info.timestamp;
356 info.bytes = out_frames * frame_size;
357 int aec_ret = write_to_reference_fifo(adev->aec, (void *)buffer, &info);
358 if (aec_ret) {
359 ALOGE("AEC: Write to speaker loopback FIFO failed!");
360 }
361 }
362
363exit:
Amit Pundir4e375822019-04-18 16:46:10 +0530364 pthread_mutex_unlock(&out->lock);
365
Amit Pundire6732bb2020-09-28 12:43:59 +0530366 if (ret != 0) {
367 usleep((int64_t)bytes * 1000000 / audio_stream_out_frame_size(stream) /
368 out_get_sample_rate(&stream->common));
Amit Pundir4e375822019-04-18 16:46:10 +0530369 }
370
Amit Pundir456949d2020-02-18 22:44:16 +0530371 return bytes;
Amit Pundir4e375822019-04-18 16:46:10 +0530372}
373
Amit Pundire6732bb2020-09-28 12:43:59 +0530374static int out_get_render_position(const struct audio_stream_out *stream,
375 uint32_t *dsp_frames)
376{
377 ALOGV("out_get_render_position: dsp_frames: %p", dsp_frames);
378 return -ENOSYS;
379}
380
Amit Pundir4e375822019-04-18 16:46:10 +0530381static int out_get_presentation_position(const struct audio_stream_out *stream,
382 uint64_t *frames, struct timespec *timestamp)
383{
Amit Pundir456949d2020-02-18 22:44:16 +0530384 if (stream == NULL || frames == NULL || timestamp == NULL) {
385 return -EINVAL;
386 }
Amit Pundire6732bb2020-09-28 12:43:59 +0530387 struct alsa_stream_out* out = (struct alsa_stream_out*)stream;
Amit Pundir4e375822019-04-18 16:46:10 +0530388
Amit Pundire6732bb2020-09-28 12:43:59 +0530389 *frames = out->frames_written;
390 *timestamp = out->timestamp;
391 ALOGV("%s: frames: %" PRIu64 ", timestamp (nsec): %" PRIu64, __func__, *frames,
392 audio_utils_ns_from_timespec(timestamp));
Amit Pundir4e375822019-04-18 16:46:10 +0530393
Amit Pundir456949d2020-02-18 22:44:16 +0530394 return 0;
Amit Pundir4e375822019-04-18 16:46:10 +0530395}
396
397
398static int out_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
399{
Amit Pundire6732bb2020-09-28 12:43:59 +0530400 ALOGV("out_add_audio_effect: %p", effect);
Amit Pundir4e375822019-04-18 16:46:10 +0530401 return 0;
402}
403
404static int out_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
405{
Amit Pundire6732bb2020-09-28 12:43:59 +0530406 ALOGV("out_remove_audio_effect: %p", effect);
Amit Pundir4e375822019-04-18 16:46:10 +0530407 return 0;
408}
409
410static int out_get_next_write_timestamp(const struct audio_stream_out *stream,
Amit Pundire6732bb2020-09-28 12:43:59 +0530411 int64_t *timestamp)
Amit Pundir4e375822019-04-18 16:46:10 +0530412{
Amit Pundire6732bb2020-09-28 12:43:59 +0530413 *timestamp = 0;
414 ALOGV("out_get_next_write_timestamp: %ld", (long int)(*timestamp));
Amit Pundir456949d2020-02-18 22:44:16 +0530415 return -ENOSYS;
Amit Pundir4e375822019-04-18 16:46:10 +0530416}
417
Amit Pundire6732bb2020-09-28 12:43:59 +0530418/** audio_stream_in implementation **/
419
420/* must be called with hw device and input stream mutexes locked */
421static int start_input_stream(struct alsa_stream_in *in)
422{
423 struct alsa_audio_device *adev = in->dev;
424 in->unavailable = true;
425 unsigned int pcm_retry_count = PCM_OPEN_RETRIES;
426
427 while (1) {
428 in->pcm = pcm_open(CARD_IN, PORT_BUILTIN_MIC, PCM_IN | PCM_MONOTONIC, &in->config);
429 if ((in->pcm != NULL) && pcm_is_ready(in->pcm)) {
430 break;
431 } else {
432 ALOGE("cannot open pcm_in driver: %s", pcm_get_error(in->pcm));
433 if (in->pcm != NULL) {
434 pcm_close(in->pcm);
435 in->pcm = NULL;
436 }
437 if (--pcm_retry_count == 0) {
438 ALOGE("Failed to open pcm_in after %d tries", PCM_OPEN_RETRIES);
439 return -ENODEV;
440 }
441 usleep(PCM_OPEN_WAIT_TIME_MS * 1000);
442 }
443 }
444 in->unavailable = false;
445 adev->active_input = in;
446 return 0;
447}
448
449static void get_mic_characteristics(struct audio_microphone_characteristic_t* mic_data,
450 size_t* mic_count) {
451 *mic_count = 1;
452 memset(mic_data, 0, sizeof(struct audio_microphone_characteristic_t));
453 strlcpy(mic_data->device_id, "builtin_mic", AUDIO_MICROPHONE_ID_MAX_LEN - 1);
454 strlcpy(mic_data->address, "top", AUDIO_DEVICE_MAX_ADDRESS_LEN - 1);
455 memset(mic_data->channel_mapping, AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED,
456 sizeof(mic_data->channel_mapping));
457 mic_data->device = AUDIO_DEVICE_IN_BUILTIN_MIC;
458 mic_data->sensitivity = -37.0;
459 mic_data->max_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
460 mic_data->min_spl = AUDIO_MICROPHONE_SPL_UNKNOWN;
461 mic_data->orientation.x = 0.0f;
462 mic_data->orientation.y = 0.0f;
463 mic_data->orientation.z = 0.0f;
464 mic_data->geometric_location.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
465 mic_data->geometric_location.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
466 mic_data->geometric_location.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN;
467}
468
Amit Pundir4e375822019-04-18 16:46:10 +0530469static uint32_t in_get_sample_rate(const struct audio_stream *stream)
470{
Amit Pundire6732bb2020-09-28 12:43:59 +0530471 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
472 return in->config.rate;
Amit Pundir4e375822019-04-18 16:46:10 +0530473}
474
475static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate)
476{
Amit Pundire6732bb2020-09-28 12:43:59 +0530477 ALOGV("in_set_sample_rate: %d", rate);
Amit Pundir4e375822019-04-18 16:46:10 +0530478 return -ENOSYS;
479}
480
Amit Pundire6732bb2020-09-28 12:43:59 +0530481static size_t get_input_buffer_size(size_t frames, audio_format_t format,
482 audio_channel_mask_t channel_mask) {
483 /* return the closest majoring multiple of 16 frames, as
484 * audioflinger expects audio buffers to be a multiple of 16 frames */
485 frames = ((frames + 15) / 16) * 16;
486 size_t bytes_per_frame = audio_channel_count_from_in_mask(channel_mask) *
487 audio_bytes_per_sample(format);
488 size_t buffer_size = frames * bytes_per_frame;
489 return buffer_size;
Amit Pundir4e375822019-04-18 16:46:10 +0530490}
491
492static audio_channel_mask_t in_get_channels(const struct audio_stream *stream)
493{
Amit Pundire6732bb2020-09-28 12:43:59 +0530494 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
495 ALOGV("in_get_channels: %d", in->config.channels);
496 return audio_channel_in_mask_from_count(in->config.channels);
Amit Pundir4e375822019-04-18 16:46:10 +0530497}
498
499static audio_format_t in_get_format(const struct audio_stream *stream)
500{
Amit Pundire6732bb2020-09-28 12:43:59 +0530501 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
502 ALOGV("in_get_format: %d", in->config.format);
503 return audio_format_from_pcm_format(in->config.format);
Amit Pundir4e375822019-04-18 16:46:10 +0530504}
505
506static int in_set_format(struct audio_stream *stream, audio_format_t format)
507{
508 return -ENOSYS;
509}
510
Amit Pundire6732bb2020-09-28 12:43:59 +0530511static size_t in_get_buffer_size(const struct audio_stream *stream)
Amit Pundir4e375822019-04-18 16:46:10 +0530512{
Amit Pundire6732bb2020-09-28 12:43:59 +0530513 struct alsa_stream_in* in = (struct alsa_stream_in*)stream;
514 size_t frames = CAPTURE_PERIOD_SIZE;
515 if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) {
516 frames = CAPTURE_PERIOD_SIZE * PLAYBACK_CODEC_SAMPLING_RATE / CAPTURE_CODEC_SAMPLING_RATE;
517 }
518
519 size_t buffer_size =
520 get_input_buffer_size(frames, stream->get_format(stream), stream->get_channels(stream));
521 ALOGV("in_get_buffer_size: %zu", buffer_size);
522 return buffer_size;
523}
524
525static int in_get_active_microphones(const struct audio_stream_in* stream,
526 struct audio_microphone_characteristic_t* mic_array,
527 size_t* mic_count) {
528 ALOGV("in_get_active_microphones");
529 if ((mic_array == NULL) || (mic_count == NULL)) {
530 return -EINVAL;
531 }
532 struct alsa_stream_in* in = (struct alsa_stream_in*)stream;
533 struct audio_hw_device* dev = (struct audio_hw_device*)in->dev;
534 bool mic_muted = false;
535 adev_get_mic_mute(dev, &mic_muted);
536 if ((in->source == AUDIO_SOURCE_ECHO_REFERENCE) || mic_muted) {
537 *mic_count = 0;
538 return 0;
539 }
540 adev_get_microphones(dev, mic_array, mic_count);
541 return 0;
542}
543
544static int do_input_standby(struct alsa_stream_in *in)
545{
546 struct alsa_audio_device *adev = in->dev;
547
548 if (!in->standby) {
549 pcm_close(in->pcm);
550 in->pcm = NULL;
551 adev->active_input = NULL;
552 in->standby = true;
553 }
554 return 0;
555}
556
557static int in_standby(struct audio_stream *stream)
558{
559 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
560 int status;
Amit Pundir456949d2020-02-18 22:44:16 +0530561
562 pthread_mutex_lock(&in->lock);
Amit Pundire6732bb2020-09-28 12:43:59 +0530563 pthread_mutex_lock(&in->dev->lock);
564 status = do_input_standby(in);
565 pthread_mutex_unlock(&in->dev->lock);
Amit Pundir456949d2020-02-18 22:44:16 +0530566 pthread_mutex_unlock(&in->lock);
Amit Pundire6732bb2020-09-28 12:43:59 +0530567 return status;
568}
569
570static int in_dump(const struct audio_stream *stream, int fd)
571{
572 struct alsa_stream_in* in = (struct alsa_stream_in*)stream;
573 if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) {
574 return 0;
575 }
576
577 struct audio_microphone_characteristic_t mic_array[AUDIO_MICROPHONE_MAX_COUNT];
578 size_t mic_count;
579
580 get_mic_characteristics(mic_array, &mic_count);
581
582 dprintf(fd, " Microphone count: %zd\n", mic_count);
583 size_t idx;
584 for (idx = 0; idx < mic_count; idx++) {
585 dprintf(fd, " Microphone: %zd\n", idx);
586 dprintf(fd, " Address: %s\n", mic_array[idx].address);
587 dprintf(fd, " Device: %d\n", mic_array[idx].device);
588 dprintf(fd, " Sensitivity (dB): %.2f\n", mic_array[idx].sensitivity);
589 }
590
Amit Pundir4e375822019-04-18 16:46:10 +0530591 return 0;
592}
593
594static int in_set_parameters(struct audio_stream *stream, const char *kvpairs)
595{
Amit Pundire6732bb2020-09-28 12:43:59 +0530596 return 0;
Amit Pundir4e375822019-04-18 16:46:10 +0530597}
598
599static char * in_get_parameters(const struct audio_stream *stream,
Amit Pundire6732bb2020-09-28 12:43:59 +0530600 const char *keys)
Amit Pundir4e375822019-04-18 16:46:10 +0530601{
Amit Pundire6732bb2020-09-28 12:43:59 +0530602 return strdup("");
Amit Pundir4e375822019-04-18 16:46:10 +0530603}
604
605static int in_set_gain(struct audio_stream_in *stream, float gain)
606{
607 return 0;
608}
609
Amit Pundir456949d2020-02-18 22:44:16 +0530610static ssize_t in_read(struct audio_stream_in *stream, void* buffer,
Amit Pundire6732bb2020-09-28 12:43:59 +0530611 size_t bytes)
Amit Pundir456949d2020-02-18 22:44:16 +0530612{
Amit Pundire6732bb2020-09-28 12:43:59 +0530613 int ret;
614 struct alsa_stream_in *in = (struct alsa_stream_in *)stream;
615 struct alsa_audio_device *adev = in->dev;
616 size_t frame_size = audio_stream_in_frame_size(stream);
617 size_t in_frames = bytes / frame_size;
Amit Pundir456949d2020-02-18 22:44:16 +0530618
Amit Pundire6732bb2020-09-28 12:43:59 +0530619 ALOGV("in_read: stream: %d, bytes %zu", in->source, bytes);
Amit Pundir456949d2020-02-18 22:44:16 +0530620
Amit Pundire6732bb2020-09-28 12:43:59 +0530621 /* Special handling for Echo Reference: simply get the reference from FIFO.
622 * The format and sample rate should be specified by arguments to adev_open_input_stream. */
623 if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) {
624 struct aec_info info;
625 info.bytes = bytes;
Amit Pundir456949d2020-02-18 22:44:16 +0530626
Amit Pundire6732bb2020-09-28 12:43:59 +0530627 const uint64_t time_increment_nsec = (uint64_t)bytes * NANOS_PER_SECOND /
628 audio_stream_in_frame_size(stream) /
629 in_get_sample_rate(&stream->common);
630 if (!aec_get_spk_running(adev->aec)) {
631 if (in->timestamp_nsec == 0) {
632 struct timespec now;
633 clock_gettime(CLOCK_MONOTONIC, &now);
634 const uint64_t timestamp_nsec = audio_utils_ns_from_timespec(&now);
635 in->timestamp_nsec = timestamp_nsec;
636 } else {
637 in->timestamp_nsec += time_increment_nsec;
638 }
639 memset(buffer, 0, bytes);
640 const uint64_t time_increment_usec = time_increment_nsec / 1000;
641 usleep(time_increment_usec);
642 } else {
643 int ref_ret = get_reference_samples(adev->aec, buffer, &info);
644 if ((ref_ret) || (info.timestamp_usec == 0)) {
645 memset(buffer, 0, bytes);
646 in->timestamp_nsec += time_increment_nsec;
647 } else {
648 in->timestamp_nsec = 1000 * info.timestamp_usec;
Amit Pundir456949d2020-02-18 22:44:16 +0530649 }
650 }
Amit Pundire6732bb2020-09-28 12:43:59 +0530651 in->frames_read += in_frames;
Amit Pundir456949d2020-02-18 22:44:16 +0530652
Amit Pundire6732bb2020-09-28 12:43:59 +0530653#if DEBUG_AEC
654 FILE* fp_ref = fopen("/data/local/traces/aec_ref.pcm", "a+");
655 if (fp_ref) {
656 fwrite((char*)buffer, 1, bytes, fp_ref);
657 fclose(fp_ref);
658 } else {
659 ALOGE("AEC debug: Could not open file aec_ref.pcm!");
Amit Pundir456949d2020-02-18 22:44:16 +0530660 }
Amit Pundire6732bb2020-09-28 12:43:59 +0530661 FILE* fp_ref_ts = fopen("/data/local/traces/aec_ref_timestamps.txt", "a+");
662 if (fp_ref_ts) {
663 fprintf(fp_ref_ts, "%" PRIu64 "\n", in->timestamp_nsec);
664 fclose(fp_ref_ts);
665 } else {
666 ALOGE("AEC debug: Could not open file aec_ref_timestamps.txt!");
667 }
668#endif
669 return info.bytes;
670 }
671
672 /* Microphone input stream read */
673
674 /* acquiring hw device mutex systematically is useful if a low priority thread is waiting
675 * on the input stream mutex - e.g. executing select_mode() while holding the hw device
676 * mutex
677 */
678 pthread_mutex_lock(&in->lock);
679 pthread_mutex_lock(&adev->lock);
680 if (in->standby) {
681 ret = start_input_stream(in);
682 if (ret != 0) {
683 pthread_mutex_unlock(&adev->lock);
684 ALOGE("start_input_stream failed with code %d", ret);
685 goto exit;
686 }
687 in->standby = false;
688 }
689
690 pthread_mutex_unlock(&adev->lock);
691
692 ret = pcm_read(in->pcm, buffer, in_frames * frame_size);
693 struct aec_info info;
694 get_pcm_timestamp(in->pcm, in->config.rate, &info, false /*isOutput*/);
695 if (ret == 0) {
696 in->frames_read += in_frames;
697 in->timestamp_nsec = audio_utils_ns_from_timespec(&info.timestamp);
698 }
699 else {
700 ALOGE("pcm_read failed with code %d", ret);
Amit Pundir456949d2020-02-18 22:44:16 +0530701 }
702
703exit:
Amit Pundir456949d2020-02-18 22:44:16 +0530704 pthread_mutex_unlock(&in->lock);
705
Amit Pundire6732bb2020-09-28 12:43:59 +0530706 bool mic_muted = false;
707 adev_get_mic_mute((struct audio_hw_device*)adev, &mic_muted);
708 if (mic_muted) {
709 memset(buffer, 0, bytes);
710 }
711
712 if (ret != 0) {
713 usleep((int64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) /
714 in_get_sample_rate(&stream->common));
715 } else {
716 /* Process AEC if available */
717 /* TODO move to a separate thread */
718 if (!mic_muted) {
719 info.bytes = bytes;
720 int aec_ret = process_aec(adev->aec, buffer, &info);
721 if (aec_ret) {
722 ALOGE("process_aec returned error code %d", aec_ret);
723 }
724 }
725 }
726
727#if DEBUG_AEC && !defined(AEC_HAL)
728 FILE* fp_in = fopen("/data/local/traces/aec_in.pcm", "a+");
729 if (fp_in) {
730 fwrite((char*)buffer, 1, bytes, fp_in);
731 fclose(fp_in);
732 } else {
733 ALOGE("AEC debug: Could not open file aec_in.pcm!");
734 }
735 FILE* fp_mic_ts = fopen("/data/local/traces/aec_in_timestamps.txt", "a+");
736 if (fp_mic_ts) {
737 fprintf(fp_mic_ts, "%" PRIu64 "\n", in->timestamp_nsec);
738 fclose(fp_mic_ts);
739 } else {
740 ALOGE("AEC debug: Could not open file aec_in_timestamps.txt!");
741 }
742#endif
743
Amit Pundir4e375822019-04-18 16:46:10 +0530744 return bytes;
745}
746
Amit Pundire6732bb2020-09-28 12:43:59 +0530747static int in_get_capture_position(const struct audio_stream_in* stream, int64_t* frames,
748 int64_t* time) {
749 if (stream == NULL || frames == NULL || time == NULL) {
750 return -EINVAL;
751 }
752 struct alsa_stream_in* in = (struct alsa_stream_in*)stream;
753
754 *frames = in->frames_read;
755 *time = in->timestamp_nsec;
756 ALOGV("%s: source: %d, timestamp (nsec): %" PRIu64, __func__, in->source, *time);
757
758 return 0;
759}
760
Amit Pundir4e375822019-04-18 16:46:10 +0530761static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream)
762{
763 return 0;
764}
765
766static int in_add_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
767{
768 return 0;
769}
770
771static int in_remove_audio_effect(const struct audio_stream *stream, effect_handle_t effect)
772{
773 return 0;
774}
775
776static int adev_open_output_stream(struct audio_hw_device *dev,
Amit Pundire6732bb2020-09-28 12:43:59 +0530777 audio_io_handle_t handle,
778 audio_devices_t devices,
779 audio_output_flags_t flags,
780 struct audio_config *config,
781 struct audio_stream_out **stream_out,
782 const char *address __unused)
Amit Pundir4e375822019-04-18 16:46:10 +0530783{
Amit Pundire6732bb2020-09-28 12:43:59 +0530784 ALOGV("adev_open_output_stream...");
785
786 struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
787 struct alsa_stream_out *out;
788 struct pcm_params *params;
Amit Pundir4e375822019-04-18 16:46:10 +0530789 int ret = 0;
790
Amit Pundire6732bb2020-09-28 12:43:59 +0530791 int out_port = get_audio_output_port(devices);
Amit Pundir4e375822019-04-18 16:46:10 +0530792
Amit Pundire6732bb2020-09-28 12:43:59 +0530793 params = pcm_params_get(CARD_OUT, out_port, PCM_OUT);
794 if (!params)
795 return -ENOSYS;
Amit Pundir456949d2020-02-18 22:44:16 +0530796
Amit Pundire6732bb2020-09-28 12:43:59 +0530797 out = (struct alsa_stream_out *)calloc(1, sizeof(struct alsa_stream_out));
Amit Pundir4e375822019-04-18 16:46:10 +0530798 if (!out)
799 return -ENOMEM;
800
801 out->stream.common.get_sample_rate = out_get_sample_rate;
802 out->stream.common.set_sample_rate = out_set_sample_rate;
803 out->stream.common.get_buffer_size = out_get_buffer_size;
804 out->stream.common.get_channels = out_get_channels;
805 out->stream.common.get_format = out_get_format;
806 out->stream.common.set_format = out_set_format;
807 out->stream.common.standby = out_standby;
808 out->stream.common.dump = out_dump;
809 out->stream.common.set_parameters = out_set_parameters;
810 out->stream.common.get_parameters = out_get_parameters;
811 out->stream.common.add_audio_effect = out_add_audio_effect;
812 out->stream.common.remove_audio_effect = out_remove_audio_effect;
813 out->stream.get_latency = out_get_latency;
814 out->stream.set_volume = out_set_volume;
815 out->stream.write = out_write;
816 out->stream.get_render_position = out_get_render_position;
Amit Pundir456949d2020-02-18 22:44:16 +0530817 out->stream.get_next_write_timestamp = out_get_next_write_timestamp;
Amit Pundire6732bb2020-09-28 12:43:59 +0530818 out->stream.get_presentation_position = out_get_presentation_position;
Amit Pundir4e375822019-04-18 16:46:10 +0530819
Amit Pundire6732bb2020-09-28 12:43:59 +0530820 out->config.channels = CHANNEL_STEREO;
821 out->config.rate = PLAYBACK_CODEC_SAMPLING_RATE;
822 out->config.format = PCM_FORMAT_S16_LE;
823 out->config.period_size = PLAYBACK_PERIOD_SIZE;
824 out->config.period_count = PLAYBACK_PERIOD_COUNT;
Amit Pundir4e375822019-04-18 16:46:10 +0530825
Amit Pundire6732bb2020-09-28 12:43:59 +0530826 if (out->config.rate != config->sample_rate ||
827 audio_channel_count_from_out_mask(config->channel_mask) != CHANNEL_STEREO ||
828 out->config.format != pcm_format_from_audio_format(config->format) ) {
829 config->sample_rate = out->config.rate;
830 config->format = audio_format_from_pcm_format(out->config.format);
831 config->channel_mask = audio_channel_out_mask_from_count(CHANNEL_STEREO);
832 ret = -EINVAL;
Amit Pundir4e375822019-04-18 16:46:10 +0530833 }
834
Amit Pundire6732bb2020-09-28 12:43:59 +0530835 ALOGI("adev_open_output_stream selects channels=%d rate=%d format=%d, devices=%d",
836 out->config.channels, out->config.rate, out->config.format, devices);
837
838 out->dev = ladev;
839 out->standby = 1;
840 out->unavailable = false;
841 out->devices = devices;
842
843 config->format = out_get_format(&out->stream.common);
844 config->channel_mask = out_get_channels(&out->stream.common);
845 config->sample_rate = out_get_sample_rate(&out->stream.common);
Amit Pundir4e375822019-04-18 16:46:10 +0530846
847 *stream_out = &out->stream;
848
Amit Pundire6732bb2020-09-28 12:43:59 +0530849 out->speaker_eq = NULL;
850 if (out_port == PORT_INTERNAL_SPEAKER) {
851 out_set_eq(out);
852 if (out->speaker_eq == NULL) {
853 ALOGE("%s: Failed to initialize speaker EQ", __func__);
854 }
855 }
856
857 /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */
858 ret = 0;
859
860 if (ret == 0) {
861 int aec_ret = init_aec_reference_config(ladev->aec, out);
862 if (aec_ret) {
863 ALOGE("AEC: Speaker config init failed!");
864 return -EINVAL;
865 }
866 }
Amit Pundir4e375822019-04-18 16:46:10 +0530867
868 return ret;
869}
870
871static void adev_close_output_stream(struct audio_hw_device *dev,
Amit Pundire6732bb2020-09-28 12:43:59 +0530872 struct audio_stream_out *stream)
Amit Pundir4e375822019-04-18 16:46:10 +0530873{
Amit Pundire6732bb2020-09-28 12:43:59 +0530874 ALOGV("adev_close_output_stream...");
875 struct alsa_audio_device *adev = (struct alsa_audio_device *)dev;
876 destroy_aec_reference_config(adev->aec);
877 struct alsa_stream_out* out = (struct alsa_stream_out*)stream;
878 fir_release(out->speaker_eq);
Amit Pundir4e375822019-04-18 16:46:10 +0530879 free(stream);
880}
881
882static int adev_set_parameters(struct audio_hw_device *dev, const char *kvpairs)
883{
Amit Pundire6732bb2020-09-28 12:43:59 +0530884 ALOGV("adev_set_parameters");
885 return -ENOSYS;
Amit Pundir4e375822019-04-18 16:46:10 +0530886}
887
888static char * adev_get_parameters(const struct audio_hw_device *dev,
Amit Pundire6732bb2020-09-28 12:43:59 +0530889 const char *keys)
Amit Pundir4e375822019-04-18 16:46:10 +0530890{
Amit Pundire6732bb2020-09-28 12:43:59 +0530891 ALOGV("adev_get_parameters");
Amit Pundir4e375822019-04-18 16:46:10 +0530892 return strdup("");
893}
894
Amit Pundire6732bb2020-09-28 12:43:59 +0530895static int adev_get_microphones(const struct audio_hw_device* dev,
896 struct audio_microphone_characteristic_t* mic_array,
897 size_t* mic_count) {
898 ALOGV("adev_get_microphones");
899 if ((mic_array == NULL) || (mic_count == NULL)) {
900 return -EINVAL;
901 }
902 get_mic_characteristics(mic_array, mic_count);
903 return 0;
904}
905
Amit Pundir4e375822019-04-18 16:46:10 +0530906static int adev_init_check(const struct audio_hw_device *dev)
907{
Amit Pundire6732bb2020-09-28 12:43:59 +0530908 ALOGV("adev_init_check");
Amit Pundir4e375822019-04-18 16:46:10 +0530909 return 0;
910}
911
912static int adev_set_voice_volume(struct audio_hw_device *dev, float volume)
913{
Amit Pundire6732bb2020-09-28 12:43:59 +0530914 ALOGV("adev_set_voice_volume: %f", volume);
915 return -ENOSYS;
Amit Pundir4e375822019-04-18 16:46:10 +0530916}
917
918static int adev_set_master_volume(struct audio_hw_device *dev, float volume)
919{
Amit Pundire6732bb2020-09-28 12:43:59 +0530920 ALOGV("adev_set_master_volume: %f", volume);
Amit Pundir4e375822019-04-18 16:46:10 +0530921 return -ENOSYS;
922}
923
924static int adev_get_master_volume(struct audio_hw_device *dev, float *volume)
925{
Amit Pundire6732bb2020-09-28 12:43:59 +0530926 ALOGV("adev_get_master_volume: %f", *volume);
Amit Pundir4e375822019-04-18 16:46:10 +0530927 return -ENOSYS;
928}
929
930static int adev_set_master_mute(struct audio_hw_device *dev, bool muted)
931{
Amit Pundire6732bb2020-09-28 12:43:59 +0530932 ALOGV("adev_set_master_mute: %d", muted);
Amit Pundir4e375822019-04-18 16:46:10 +0530933 return -ENOSYS;
934}
935
936static int adev_get_master_mute(struct audio_hw_device *dev, bool *muted)
937{
Amit Pundire6732bb2020-09-28 12:43:59 +0530938 ALOGV("adev_get_master_mute: %d", *muted);
Amit Pundir4e375822019-04-18 16:46:10 +0530939 return -ENOSYS;
940}
941
942static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode)
943{
Amit Pundire6732bb2020-09-28 12:43:59 +0530944 ALOGV("adev_set_mode: %d", mode);
Amit Pundir4e375822019-04-18 16:46:10 +0530945 return 0;
946}
947
948static int adev_set_mic_mute(struct audio_hw_device *dev, bool state)
949{
Amit Pundire6732bb2020-09-28 12:43:59 +0530950 ALOGV("adev_set_mic_mute: %d",state);
951 struct alsa_audio_device *adev = (struct alsa_audio_device *)dev;
Amit Pundir456949d2020-02-18 22:44:16 +0530952 pthread_mutex_lock(&adev->lock);
953 adev->mic_mute = state;
954 pthread_mutex_unlock(&adev->lock);
955 return 0;
Amit Pundir4e375822019-04-18 16:46:10 +0530956}
957
958static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state)
959{
Amit Pundire6732bb2020-09-28 12:43:59 +0530960 ALOGV("adev_get_mic_mute");
961 struct alsa_audio_device *adev = (struct alsa_audio_device *)dev;
Amit Pundir456949d2020-02-18 22:44:16 +0530962 pthread_mutex_lock(&adev->lock);
963 *state = adev->mic_mute;
964 pthread_mutex_unlock(&adev->lock);
965 return 0;
Amit Pundir4e375822019-04-18 16:46:10 +0530966}
967
968static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev,
Amit Pundire6732bb2020-09-28 12:43:59 +0530969 const struct audio_config *config)
Amit Pundir4e375822019-04-18 16:46:10 +0530970{
Amit Pundire6732bb2020-09-28 12:43:59 +0530971 size_t buffer_size =
972 get_input_buffer_size(CAPTURE_PERIOD_SIZE, config->format, config->channel_mask);
973 ALOGV("adev_get_input_buffer_size: %zu", buffer_size);
974 return buffer_size;
Amit Pundir4e375822019-04-18 16:46:10 +0530975}
976
Amit Pundire6732bb2020-09-28 12:43:59 +0530977static int adev_open_input_stream(struct audio_hw_device* dev, audio_io_handle_t handle,
978 audio_devices_t devices, struct audio_config* config,
979 struct audio_stream_in** stream_in,
980 audio_input_flags_t flags __unused, const char* address __unused,
981 audio_source_t source) {
982 ALOGV("adev_open_input_stream...");
Amit Pundir456949d2020-02-18 22:44:16 +0530983
Amit Pundire6732bb2020-09-28 12:43:59 +0530984 struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
985 struct alsa_stream_in *in;
986 struct pcm_params *params;
Amit Pundir456949d2020-02-18 22:44:16 +0530987 int ret = 0;
Amit Pundir456949d2020-02-18 22:44:16 +0530988
Amit Pundire6732bb2020-09-28 12:43:59 +0530989 params = pcm_params_get(CARD_IN, PORT_BUILTIN_MIC, PCM_IN);
990 if (!params)
991 return -ENOSYS;
992
993 in = (struct alsa_stream_in *)calloc(1, sizeof(struct alsa_stream_in));
994 if (!in)
995 return -ENOMEM;
Amit Pundir4e375822019-04-18 16:46:10 +0530996
997 in->stream.common.get_sample_rate = in_get_sample_rate;
Amit Pundire6732bb2020-09-28 12:43:59 +0530998 in->stream.common.set_sample_rate = in_set_sample_rate;
Amit Pundir4e375822019-04-18 16:46:10 +0530999 in->stream.common.get_buffer_size = in_get_buffer_size;
1000 in->stream.common.get_channels = in_get_channels;
1001 in->stream.common.get_format = in_get_format;
Amit Pundire6732bb2020-09-28 12:43:59 +05301002 in->stream.common.set_format = in_set_format;
Amit Pundir4e375822019-04-18 16:46:10 +05301003 in->stream.common.standby = in_standby;
1004 in->stream.common.dump = in_dump;
1005 in->stream.common.set_parameters = in_set_parameters;
1006 in->stream.common.get_parameters = in_get_parameters;
Amit Pundire6732bb2020-09-28 12:43:59 +05301007 in->stream.common.add_audio_effect = in_add_audio_effect;
1008 in->stream.common.remove_audio_effect = in_remove_audio_effect;
1009 in->stream.set_gain = in_set_gain;
Amit Pundir4e375822019-04-18 16:46:10 +05301010 in->stream.read = in_read;
Amit Pundire6732bb2020-09-28 12:43:59 +05301011 in->stream.get_input_frames_lost = in_get_input_frames_lost;
Amit Pundir456949d2020-02-18 22:44:16 +05301012 in->stream.get_capture_position = in_get_capture_position;
1013 in->stream.get_active_microphones = in_get_active_microphones;
1014
Amit Pundire6732bb2020-09-28 12:43:59 +05301015 in->config.channels = CHANNEL_STEREO;
1016 if (source == AUDIO_SOURCE_ECHO_REFERENCE) {
1017 in->config.rate = PLAYBACK_CODEC_SAMPLING_RATE;
1018 } else {
1019 in->config.rate = CAPTURE_CODEC_SAMPLING_RATE;
1020 }
1021 in->config.format = PCM_FORMAT_S32_LE;
1022 in->config.period_size = CAPTURE_PERIOD_SIZE;
1023 in->config.period_count = CAPTURE_PERIOD_COUNT;
Amit Pundir456949d2020-02-18 22:44:16 +05301024
Amit Pundire6732bb2020-09-28 12:43:59 +05301025 if (in->config.rate != config->sample_rate ||
1026 audio_channel_count_from_in_mask(config->channel_mask) != CHANNEL_STEREO ||
1027 in->config.format != pcm_format_from_audio_format(config->format) ) {
1028 ret = -EINVAL;
1029 }
Amit Pundir456949d2020-02-18 22:44:16 +05301030
Amit Pundire6732bb2020-09-28 12:43:59 +05301031 ALOGI("adev_open_input_stream selects channels=%d rate=%d format=%d source=%d",
1032 in->config.channels, in->config.rate, in->config.format, source);
1033
1034 in->dev = ladev;
Amit Pundir456949d2020-02-18 22:44:16 +05301035 in->standby = true;
Amit Pundire6732bb2020-09-28 12:43:59 +05301036 in->unavailable = false;
1037 in->source = source;
1038 in->devices = devices;
Amit Pundir456949d2020-02-18 22:44:16 +05301039
Amit Pundire6732bb2020-09-28 12:43:59 +05301040 config->format = in_get_format(&in->stream.common);
1041 config->channel_mask = in_get_channels(&in->stream.common);
1042 config->sample_rate = in_get_sample_rate(&in->stream.common);
Amit Pundir456949d2020-02-18 22:44:16 +05301043
Amit Pundire6732bb2020-09-28 12:43:59 +05301044 /* If AEC is in the app, only configure based on ECHO_REFERENCE spec.
1045 * If AEC is in the HAL, configure using the given mic stream. */
1046 bool aecInput = true;
1047#if !defined(AEC_HAL)
1048 aecInput = (in->source == AUDIO_SOURCE_ECHO_REFERENCE);
1049#endif
Amit Pundir4e375822019-04-18 16:46:10 +05301050
Amit Pundire6732bb2020-09-28 12:43:59 +05301051 if ((ret == 0) && aecInput) {
1052 int aec_ret = init_aec_mic_config(ladev->aec, in);
1053 if (aec_ret) {
1054 ALOGE("AEC: Mic config init failed!");
Amit Pundir456949d2020-02-18 22:44:16 +05301055 return -EINVAL;
1056 }
Amit Pundire6732bb2020-09-28 12:43:59 +05301057 }
1058
1059 if (ret) {
1060 free(in);
Amit Pundir456949d2020-02-18 22:44:16 +05301061 } else {
Amit Pundire6732bb2020-09-28 12:43:59 +05301062 *stream_in = &in->stream;
Amit Pundir456949d2020-02-18 22:44:16 +05301063 }
1064
Amit Pundire6732bb2020-09-28 12:43:59 +05301065#if DEBUG_AEC
1066 remove("/data/local/traces/aec_ref.pcm");
1067 remove("/data/local/traces/aec_in.pcm");
1068 remove("/data/local/traces/aec_ref_timestamps.txt");
1069 remove("/data/local/traces/aec_in_timestamps.txt");
1070#endif
1071 return ret;
1072}
Amit Pundir456949d2020-02-18 22:44:16 +05301073
Amit Pundire6732bb2020-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...");
1078 struct alsa_audio_device *adev = (struct alsa_audio_device *)dev;
1079 destroy_aec_mic_config(adev->aec);
1080 free(stream);
1081 return;
1082}
Amit Pundir456949d2020-02-18 22:44:16 +05301083
Amit Pundire6732bb2020-09-28 12:43:59 +05301084static int adev_dump(const audio_hw_device_t *device, int fd)
1085{
1086 ALOGV("adev_dump");
Amit Pundir4e375822019-04-18 16:46:10 +05301087 return 0;
1088}
1089
Amit Pundire6732bb2020-09-28 12:43:59 +05301090static int adev_close(hw_device_t *device)
Amit Pundir456949d2020-02-18 22:44:16 +05301091{
Amit Pundire6732bb2020-09-28 12:43:59 +05301092 ALOGV("adev_close");
Amit Pundir456949d2020-02-18 22:44:16 +05301093
Amit Pundire6732bb2020-09-28 12:43:59 +05301094 struct alsa_audio_device *adev = (struct alsa_audio_device *)device;
1095 release_aec(adev->aec);
1096 free(device);
1097 return 0;
Amit Pundir456949d2020-02-18 22:44:16 +05301098}
1099
Amit Pundir4e375822019-04-18 16:46:10 +05301100static int adev_open(const hw_module_t* module, const char* name,
Amit Pundire6732bb2020-09-28 12:43:59 +05301101 hw_device_t** device)
Amit Pundir4e375822019-04-18 16:46:10 +05301102{
Amit Pundire6732bb2020-09-28 12:43:59 +05301103 struct alsa_audio_device *adev;
1104
1105 ALOGV("adev_open: %s", name);
Amit Pundir4e375822019-04-18 16:46:10 +05301106
1107 if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
1108 return -EINVAL;
1109
Amit Pundire6732bb2020-09-28 12:43:59 +05301110 adev = calloc(1, sizeof(struct alsa_audio_device));
1111 if (!adev)
1112 return -ENOMEM;
Amit Pundir4e375822019-04-18 16:46:10 +05301113
Amit Pundire6732bb2020-09-28 12:43:59 +05301114 adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
1115 adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
1116 adev->hw_device.common.module = (struct hw_module_t *) module;
1117 adev->hw_device.common.close = adev_close;
1118 adev->hw_device.init_check = adev_init_check;
1119 adev->hw_device.set_voice_volume = adev_set_voice_volume;
1120 adev->hw_device.set_master_volume = adev_set_master_volume;
1121 adev->hw_device.get_master_volume = adev_get_master_volume;
1122 adev->hw_device.set_master_mute = adev_set_master_mute;
1123 adev->hw_device.get_master_mute = adev_get_master_mute;
1124 adev->hw_device.set_mode = adev_set_mode;
1125 adev->hw_device.set_mic_mute = adev_set_mic_mute;
1126 adev->hw_device.get_mic_mute = adev_get_mic_mute;
1127 adev->hw_device.set_parameters = adev_set_parameters;
1128 adev->hw_device.get_parameters = adev_get_parameters;
1129 adev->hw_device.get_input_buffer_size = adev_get_input_buffer_size;
1130 adev->hw_device.open_output_stream = adev_open_output_stream;
1131 adev->hw_device.close_output_stream = adev_close_output_stream;
1132 adev->hw_device.open_input_stream = adev_open_input_stream;
1133 adev->hw_device.close_input_stream = adev_close_input_stream;
1134 adev->hw_device.dump = adev_dump;
1135 adev->hw_device.get_microphones = adev_get_microphones;
Amit Pundir4e375822019-04-18 16:46:10 +05301136
Amit Pundire6732bb2020-09-28 12:43:59 +05301137 *device = &adev->hw_device.common;
Amit Pundir4e375822019-04-18 16:46:10 +05301138
Amit Pundire6732bb2020-09-28 12:43:59 +05301139 adev->mixer = mixer_open(CARD_OUT);
Amit Pundir4e375822019-04-18 16:46:10 +05301140
Amit Pundire6732bb2020-09-28 12:43:59 +05301141 if (!adev->mixer) {
1142 ALOGE("Unable to open the mixer, aborting.");
1143 return -EINVAL;
Amit Pundir456949d2020-02-18 22:44:16 +05301144 }
1145
Amit Pundire6732bb2020-09-28 12:43:59 +05301146 adev->audio_route = audio_route_init(CARD_OUT, MIXER_XML_PATH);
1147 if (!adev->audio_route) {
1148 ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
1149 return -EINVAL;
1150 }
Amit Pundir456949d2020-02-18 22:44:16 +05301151
Amit Pundire6732bb2020-09-28 12:43:59 +05301152 pthread_mutex_lock(&adev->lock);
1153 if (init_aec(CAPTURE_CODEC_SAMPLING_RATE, NUM_AEC_REFERENCE_CHANNELS,
1154 CHANNEL_STEREO, &adev->aec)) {
1155 pthread_mutex_unlock(&adev->lock);
1156 return -EINVAL;
1157 }
1158 pthread_mutex_unlock(&adev->lock);
1159
Amit Pundir4e375822019-04-18 16:46:10 +05301160 return 0;
1161}
1162
1163static struct hw_module_methods_t hal_module_methods = {
1164 .open = adev_open,
1165};
1166
1167struct audio_module HAL_MODULE_INFO_SYM = {
1168 .common = {
1169 .tag = HARDWARE_MODULE_TAG,
1170 .module_api_version = AUDIO_MODULE_API_VERSION_0_1,
1171 .hal_api_version = HARDWARE_HAL_API_VERSION,
1172 .id = AUDIO_HARDWARE_MODULE_ID,
Amit Pundire6732bb2020-09-28 12:43:59 +05301173 .name = "Yukawa audio HW HAL",
Amit Pundir4e375822019-04-18 16:46:10 +05301174 .author = "The Android Open Source Project",
1175 .methods = &hal_module_methods,
1176 },
1177};