diff options
author | Joonas Kylmälä <joonas.kylmala@iki.fi> | 2020-11-28 11:32:28 -0500 |
---|---|---|
committer | Joonas Kylmälä <joonas.kylmala@iki.fi> | 2020-11-28 11:32:28 -0500 |
commit | db0f5726f587e6d4e942d358115853ac5e11b55d (patch) | |
tree | 5b9aab11ce353ff7bca90856495f67e9e50e463d /audio/audio_hw.c | |
parent | 2406ae1558f0c5429f88ab97c7c7cdf42d6269a4 (diff) | |
download | device_samsung_midas-common-db0f5726f587e6d4e942d358115853ac5e11b55d.tar.gz device_samsung_midas-common-db0f5726f587e6d4e942d358115853ac5e11b55d.tar.bz2 device_samsung_midas-common-db0f5726f587e6d4e942d358115853ac5e11b55d.zip |
audio: update module to latest upstream version
This updates the audio module to version
e24372e861c14654a4eb9449dd3d0a615522f084
from https://android.googlesource.com/device/linaro/dragonboard
Signed-off-by: Joonas Kylmälä <joonas.kylmala@iki.fi>
Diffstat (limited to 'audio/audio_hw.c')
-rw-r--r-- | audio/audio_hw.c | 754 |
1 files changed, 603 insertions, 151 deletions
diff --git a/audio/audio_hw.c b/audio/audio_hw.c index 7be7178..4a16ac1 100644 --- a/audio/audio_hw.c +++ b/audio/audio_hw.c @@ -12,19 +12,20 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. + * + * Copied as it is from device/amlogic/generic/hal/audio/ */ -#define MIXER_XML_PATH "/vendor/etc/mixer_paths.xml" - -#define LOG_TAG "audio_hw_dragonboard" +#define LOG_TAG "audio_hw_yukawa" //#define LOG_NDEBUG 0 #include <errno.h> +#include <inttypes.h> #include <malloc.h> #include <pthread.h> #include <stdint.h> -#include <sys/time.h> #include <stdlib.h> +#include <sys/time.h> #include <unistd.h> #include <log/log.h> @@ -35,87 +36,152 @@ #include <system/audio.h> #include <hardware/audio.h> +#include <audio_effects/effect_aec.h> #include <audio_route/audio_route.h> -#include <sound/asound.h> -#include <tinyalsa/asoundlib.h> -#include <audio_utils/resampler.h> +#include <audio_utils/clock.h> #include <audio_utils/echo_reference.h> -#include <hardware/audio_effect.h> +#include <audio_utils/resampler.h> #include <hardware/audio_alsaops.h> -#include <audio_effects/effect_aec.h> +#include <hardware/audio_effect.h> +#include <sound/asound.h> +#include <tinyalsa/asoundlib.h> +#include <sys/ioctl.h> -#define CARD_OUT 0 -#define PORT_CODEC 0 -/* Minimum granularity - Arbitrary but small value */ -#define CODEC_BASE_FRAME_COUNT 32 - -/* number of base blocks in a short period (low latency) */ -#define PERIOD_MULTIPLIER 32 /* 21 ms */ -/* number of frames per short period (low latency) */ -#define PERIOD_SIZE (CODEC_BASE_FRAME_COUNT * PERIOD_MULTIPLIER) -/* number of pseudo periods for low latency playback */ -#define PLAYBACK_PERIOD_COUNT 2 -#define PLAYBACK_PERIOD_START_THRESHOLD 2 -#define CODEC_SAMPLING_RATE 48000 -#define CHANNEL_STEREO 2 -#define MIN_WRITE_SLEEP_US 5000 - -struct stub_stream_in { - struct audio_stream_in stream; -}; +#include "audio_aec.h" +#include "audio_hw.h" -struct alsa_audio_device { - struct audio_hw_device hw_device; +static int adev_get_mic_mute(const struct audio_hw_device* dev, bool* state); +static int adev_get_microphones(const struct audio_hw_device* dev, + struct audio_microphone_characteristic_t* mic_array, + size_t* mic_count); +static size_t out_get_buffer_size(const struct audio_stream* stream); - pthread_mutex_t lock; /* see note below on mutex acquisition order */ - int devices; - struct alsa_stream_in *active_input; - struct alsa_stream_out *active_output; - struct audio_route *audio_route; - struct mixer *mixer; - bool mic_mute; -}; +static int get_audio_output_port(audio_devices_t devices) { + /* Only HDMI out for now #FIXME */ + return PORT_HDMI; +} -struct alsa_stream_out { - struct audio_stream_out stream; - - pthread_mutex_t lock; /* see note below on mutex acquisition order */ - struct pcm_config config; - struct pcm *pcm; - bool unavailable; - int standby; - struct alsa_audio_device *dev; - int write_threshold; - unsigned int written; -}; +static void timestamp_adjust(struct timespec* ts, ssize_t frames, uint32_t sampling_rate) { + /* This function assumes the adjustment (in nsec) is less than the max value of long, + * which for 32-bit long this is 2^31 * 1e-9 seconds, slightly over 2 seconds. + * For 64-bit long it is 9e+9 seconds. */ + long adj_nsec = (frames / (float) sampling_rate) * 1E9L; + ts->tv_nsec += adj_nsec; + while (ts->tv_nsec > 1E9L) { + ts->tv_sec++; + ts->tv_nsec -= 1E9L; + } + if (ts->tv_nsec < 0) { + ts->tv_sec--; + ts->tv_nsec += 1E9L; + } +} +/* Helper function to get PCM hardware timestamp. + * Only the field 'timestamp' of argument 'ts' is updated. */ +static int get_pcm_timestamp(struct pcm* pcm, uint32_t sample_rate, struct aec_info* info, + bool isOutput) { + int ret = 0; + if (pcm_get_htimestamp(pcm, &info->available, &info->timestamp) < 0) { + ALOGE("Error getting PCM timestamp!"); + info->timestamp.tv_sec = 0; + info->timestamp.tv_nsec = 0; + return -EINVAL; + } + ssize_t frames; + if (isOutput) { + frames = pcm_get_buffer_size(pcm) - info->available; + } else { + frames = -info->available; /* rewind timestamp */ + } + timestamp_adjust(&info->timestamp, frames, sample_rate); + return ret; +} + +static int read_filter_from_file(const char* filename, int16_t* filter, int max_length) { + FILE* fp = fopen(filename, "r"); + if (fp == NULL) { + ALOGI("%s: File %s not found.", __func__, filename); + return 0; + } + int num_taps = 0; + char* line = NULL; + size_t len = 0; + while (!feof(fp)) { + size_t size = getline(&line, &len, fp); + if ((line[0] == '#') || (size < 2)) { + continue; + } + int n = sscanf(line, "%" SCNd16 "\n", &filter[num_taps++]); + if (n < 1) { + ALOGE("Could not find coefficient %d! Exiting...", num_taps - 1); + return 0; + } + ALOGV("Coeff %d : %" PRId16, num_taps, filter[num_taps - 1]); + if (num_taps == max_length) { + ALOGI("%s: max tap length %d reached.", __func__, max_length); + break; + } + } + free(line); + fclose(fp); + return num_taps; +} + +static void out_set_eq(struct alsa_stream_out* out) { + out->speaker_eq = NULL; + int16_t* speaker_eq_coeffs = (int16_t*)calloc(SPEAKER_MAX_EQ_LENGTH, sizeof(int16_t)); + if (speaker_eq_coeffs == NULL) { + ALOGE("%s: Failed to allocate speaker EQ", __func__); + return; + } + int num_taps = read_filter_from_file(SPEAKER_EQ_FILE, speaker_eq_coeffs, SPEAKER_MAX_EQ_LENGTH); + if (num_taps == 0) { + ALOGI("%s: Empty filter file or 0 taps set.", __func__); + free(speaker_eq_coeffs); + return; + } + out->speaker_eq = fir_init( + out->config.channels, FIR_SINGLE_FILTER, num_taps, + out_get_buffer_size(&out->stream.common) / out->config.channels / sizeof(int16_t), + speaker_eq_coeffs); + free(speaker_eq_coeffs); +} /* must be called with hw device and output stream mutexes locked */ static int start_output_stream(struct alsa_stream_out *out) { struct alsa_audio_device *adev = out->dev; - if (out->unavailable) - return -ENODEV; - /* default to low power: will be corrected in out_write if necessary before first write to * tinyalsa. */ - out->write_threshold = PLAYBACK_PERIOD_COUNT * PERIOD_SIZE; - out->config.start_threshold = PLAYBACK_PERIOD_START_THRESHOLD * PERIOD_SIZE; - out->config.avail_min = PERIOD_SIZE; - - out->pcm = pcm_open(CARD_OUT, PORT_CODEC, PCM_OUT | PCM_MMAP | PCM_NOIRQ | PCM_MONOTONIC, &out->config); - - if (!pcm_is_ready(out->pcm)) { - ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm)); - pcm_close(out->pcm); - adev->active_output = NULL; - out->unavailable = true; - return -ENODEV; + out->write_threshold = PLAYBACK_PERIOD_COUNT * PLAYBACK_PERIOD_SIZE; + out->config.start_threshold = PLAYBACK_PERIOD_START_THRESHOLD * PLAYBACK_PERIOD_SIZE; + out->config.avail_min = PLAYBACK_PERIOD_SIZE; + out->unavailable = true; + unsigned int pcm_retry_count = PCM_OPEN_RETRIES; + int out_port = get_audio_output_port(out->devices); + + while (1) { + out->pcm = pcm_open(CARD_OUT, out_port, PCM_OUT | PCM_MONOTONIC, &out->config); + if ((out->pcm != NULL) && pcm_is_ready(out->pcm)) { + break; + } else { + ALOGE("cannot open pcm_out driver: %s", pcm_get_error(out->pcm)); + if (out->pcm != NULL) { + pcm_close(out->pcm); + out->pcm = NULL; + } + if (--pcm_retry_count == 0) { + ALOGE("Failed to open pcm_out after %d tries", PCM_OPEN_RETRIES); + return -ENODEV; + } + usleep(PCM_OPEN_WAIT_TIME_MS * 1000); + } } - + out->unavailable = false; adev->active_output = out; return 0; } @@ -138,7 +204,7 @@ static size_t out_get_buffer_size(const struct audio_stream *stream) /* return the closest majoring multiple of 16 frames, as * audioflinger expects audio buffers to be a multiple of 16 frames */ - size_t size = PERIOD_SIZE; + size_t size = PLAYBACK_PERIOD_SIZE; size = ((size + 15) / 16) * 16; return size * audio_stream_out_frame_size((struct audio_stream_out *)stream); } @@ -167,12 +233,15 @@ static int do_output_standby(struct alsa_stream_out *out) { struct alsa_audio_device *adev = out->dev; + fir_reset(out->speaker_eq); + if (!out->standby) { pcm_close(out->pcm); out->pcm = NULL; adev->active_output = NULL; out->standby = 1; } + aec_set_spk_running(adev->aec, false); return 0; } @@ -212,16 +281,16 @@ static int out_set_parameters(struct audio_stream *stream, const char *kvpairs) val = atoi(value); pthread_mutex_lock(&adev->lock); pthread_mutex_lock(&out->lock); - if (((adev->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) { - adev->devices &= ~AUDIO_DEVICE_OUT_ALL; - adev->devices |= val; + if (((out->devices & AUDIO_DEVICE_OUT_ALL) != val) && (val != 0)) { + out->devices &= ~AUDIO_DEVICE_OUT_ALL; + out->devices |= val; } pthread_mutex_unlock(&out->lock); pthread_mutex_unlock(&adev->lock); } str_parms_destroy(parms); - return ret; + return 0; } static char * out_get_parameters(const struct audio_stream *stream, const char *keys) @@ -234,14 +303,14 @@ static uint32_t out_get_latency(const struct audio_stream_out *stream) { ALOGV("out_get_latency"); struct alsa_stream_out *out = (struct alsa_stream_out *)stream; - return (PERIOD_SIZE * PLAYBACK_PERIOD_COUNT * 1000) / out->config.rate; + return (PLAYBACK_PERIOD_SIZE * PLAYBACK_PERIOD_COUNT * 1000) / out->config.rate; } static int out_set_volume(struct audio_stream_out *stream, float left, float right) { ALOGV("out_set_volume: Left:%f Right:%f", left, right); - return 0; + return -ENOSYS; } static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, @@ -253,6 +322,8 @@ static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, size_t frame_size = audio_stream_out_frame_size(stream); size_t out_frames = bytes / frame_size; + ALOGV("%s: devices: %d, bytes %zu", __func__, out->devices, bytes); + /* acquiring hw device mutex systematically is useful if a low priority thread is waiting * on the output stream mutex - e.g. executing select_mode() while holding the hw device * mutex @@ -266,14 +337,29 @@ static ssize_t out_write(struct audio_stream_out *stream, const void* buffer, goto exit; } out->standby = 0; + aec_set_spk_running(adev->aec, true); } pthread_mutex_unlock(&adev->lock); - ret = pcm_mmap_write(out->pcm, buffer, out_frames * frame_size); + if (out->speaker_eq != NULL) { + fir_process_interleaved(out->speaker_eq, (int16_t*)buffer, (int16_t*)buffer, out_frames); + } + + ret = pcm_write(out->pcm, buffer, out_frames * frame_size); if (ret == 0) { - out->written += out_frames; + out->frames_written += out_frames; + + struct aec_info info; + get_pcm_timestamp(out->pcm, out->config.rate, &info, true /*isOutput*/); + out->timestamp = info.timestamp; + info.bytes = out_frames * frame_size; + int aec_ret = write_to_reference_fifo(adev->aec, (void *)buffer, &info); + if (aec_ret) { + ALOGE("AEC: Write to speaker loopback FIFO failed!"); + } } + exit: pthread_mutex_unlock(&out->lock); @@ -288,30 +374,24 @@ exit: static int out_get_render_position(const struct audio_stream_out *stream, uint32_t *dsp_frames) { - *dsp_frames = 0; ALOGV("out_get_render_position: dsp_frames: %p", dsp_frames); - return -EINVAL; + return -ENOSYS; } static int out_get_presentation_position(const struct audio_stream_out *stream, uint64_t *frames, struct timespec *timestamp) { - struct alsa_stream_out *out = (struct alsa_stream_out *)stream; - int ret = -1; - - if (out->pcm) { - unsigned int avail; - if (pcm_get_htimestamp(out->pcm, &avail, timestamp) == 0) { - size_t kernel_buffer_size = out->config.period_size * out->config.period_count; - int64_t signed_frames = out->written - kernel_buffer_size + avail; - if (signed_frames >= 0) { - *frames = signed_frames; - ret = 0; - } - } - } + if (stream == NULL || frames == NULL || timestamp == NULL) { + return -EINVAL; + } + struct alsa_stream_out* out = (struct alsa_stream_out*)stream; - return ret; + *frames = out->frames_written; + *timestamp = out->timestamp; + ALOGV("%s: frames: %" PRIu64 ", timestamp (nsec): %" PRIu64, __func__, *frames, + audio_utils_ns_from_timespec(timestamp)); + + return 0; } @@ -332,14 +412,64 @@ static int out_get_next_write_timestamp(const struct audio_stream_out *stream, { *timestamp = 0; ALOGV("out_get_next_write_timestamp: %ld", (long int)(*timestamp)); - return -EINVAL; + return -ENOSYS; } /** audio_stream_in implementation **/ + +/* must be called with hw device and input stream mutexes locked */ +static int start_input_stream(struct alsa_stream_in *in) +{ + struct alsa_audio_device *adev = in->dev; + in->unavailable = true; + unsigned int pcm_retry_count = PCM_OPEN_RETRIES; + + while (1) { + in->pcm = pcm_open(CARD_IN, PORT_BUILTIN_MIC, PCM_IN | PCM_MONOTONIC, &in->config); + if ((in->pcm != NULL) && pcm_is_ready(in->pcm)) { + break; + } else { + ALOGE("cannot open pcm_in driver: %s", pcm_get_error(in->pcm)); + if (in->pcm != NULL) { + pcm_close(in->pcm); + in->pcm = NULL; + } + if (--pcm_retry_count == 0) { + ALOGE("Failed to open pcm_in after %d tries", PCM_OPEN_RETRIES); + return -ENODEV; + } + usleep(PCM_OPEN_WAIT_TIME_MS * 1000); + } + } + in->unavailable = false; + adev->active_input = in; + return 0; +} + +static void get_mic_characteristics(struct audio_microphone_characteristic_t* mic_data, + size_t* mic_count) { + *mic_count = 1; + memset(mic_data, 0, sizeof(struct audio_microphone_characteristic_t)); + strlcpy(mic_data->device_id, "builtin_mic", AUDIO_MICROPHONE_ID_MAX_LEN - 1); + strlcpy(mic_data->address, "top", AUDIO_DEVICE_MAX_ADDRESS_LEN - 1); + memset(mic_data->channel_mapping, AUDIO_MICROPHONE_CHANNEL_MAPPING_UNUSED, + sizeof(mic_data->channel_mapping)); + mic_data->device = AUDIO_DEVICE_IN_BUILTIN_MIC; + mic_data->sensitivity = -37.0; + mic_data->max_spl = AUDIO_MICROPHONE_SPL_UNKNOWN; + mic_data->min_spl = AUDIO_MICROPHONE_SPL_UNKNOWN; + mic_data->orientation.x = 0.0f; + mic_data->orientation.y = 0.0f; + mic_data->orientation.z = 0.0f; + mic_data->geometric_location.x = AUDIO_MICROPHONE_COORDINATE_UNKNOWN; + mic_data->geometric_location.y = AUDIO_MICROPHONE_COORDINATE_UNKNOWN; + mic_data->geometric_location.z = AUDIO_MICROPHONE_COORDINATE_UNKNOWN; +} + static uint32_t in_get_sample_rate(const struct audio_stream *stream) { - ALOGV("in_get_sample_rate"); - return 8000; + struct alsa_stream_in *in = (struct alsa_stream_in *)stream; + return in->config.rate; } static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) @@ -348,21 +478,29 @@ static int in_set_sample_rate(struct audio_stream *stream, uint32_t rate) return -ENOSYS; } -static size_t in_get_buffer_size(const struct audio_stream *stream) -{ - ALOGV("in_get_buffer_size: %d", 320); - return 320; +static size_t get_input_buffer_size(size_t frames, audio_format_t format, + audio_channel_mask_t channel_mask) { + /* return the closest majoring multiple of 16 frames, as + * audioflinger expects audio buffers to be a multiple of 16 frames */ + frames = ((frames + 15) / 16) * 16; + size_t bytes_per_frame = audio_channel_count_from_in_mask(channel_mask) * + audio_bytes_per_sample(format); + size_t buffer_size = frames * bytes_per_frame; + return buffer_size; } static audio_channel_mask_t in_get_channels(const struct audio_stream *stream) { - ALOGV("in_get_channels: %d", AUDIO_CHANNEL_IN_MONO); - return AUDIO_CHANNEL_IN_MONO; + struct alsa_stream_in *in = (struct alsa_stream_in *)stream; + ALOGV("in_get_channels: %d", in->config.channels); + return audio_channel_in_mask_from_count(in->config.channels); } static audio_format_t in_get_format(const struct audio_stream *stream) { - return AUDIO_FORMAT_PCM_16_BIT; + struct alsa_stream_in *in = (struct alsa_stream_in *)stream; + ALOGV("in_get_format: %d", in->config.format); + return audio_format_from_pcm_format(in->config.format); } static int in_set_format(struct audio_stream *stream, audio_format_t format) @@ -370,13 +508,86 @@ static int in_set_format(struct audio_stream *stream, audio_format_t format) return -ENOSYS; } -static int in_standby(struct audio_stream *stream) +static size_t in_get_buffer_size(const struct audio_stream *stream) +{ + struct alsa_stream_in* in = (struct alsa_stream_in*)stream; + size_t frames = CAPTURE_PERIOD_SIZE; + if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) { + frames = CAPTURE_PERIOD_SIZE * PLAYBACK_CODEC_SAMPLING_RATE / CAPTURE_CODEC_SAMPLING_RATE; + } + + size_t buffer_size = + get_input_buffer_size(frames, stream->get_format(stream), stream->get_channels(stream)); + ALOGV("in_get_buffer_size: %zu", buffer_size); + return buffer_size; +} + +static int in_get_active_microphones(const struct audio_stream_in* stream, + struct audio_microphone_characteristic_t* mic_array, + size_t* mic_count) { + ALOGV("in_get_active_microphones"); + if ((mic_array == NULL) || (mic_count == NULL)) { + return -EINVAL; + } + struct alsa_stream_in* in = (struct alsa_stream_in*)stream; + struct audio_hw_device* dev = (struct audio_hw_device*)in->dev; + bool mic_muted = false; + adev_get_mic_mute(dev, &mic_muted); + if ((in->source == AUDIO_SOURCE_ECHO_REFERENCE) || mic_muted) { + *mic_count = 0; + return 0; + } + adev_get_microphones(dev, mic_array, mic_count); + return 0; +} + +static int do_input_standby(struct alsa_stream_in *in) { + struct alsa_audio_device *adev = in->dev; + + if (!in->standby) { + pcm_close(in->pcm); + in->pcm = NULL; + adev->active_input = NULL; + in->standby = true; + } return 0; } +static int in_standby(struct audio_stream *stream) +{ + struct alsa_stream_in *in = (struct alsa_stream_in *)stream; + int status; + + pthread_mutex_lock(&in->lock); + pthread_mutex_lock(&in->dev->lock); + status = do_input_standby(in); + pthread_mutex_unlock(&in->dev->lock); + pthread_mutex_unlock(&in->lock); + return status; +} + static int in_dump(const struct audio_stream *stream, int fd) { + struct alsa_stream_in* in = (struct alsa_stream_in*)stream; + if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) { + return 0; + } + + struct audio_microphone_characteristic_t mic_array[AUDIO_MICROPHONE_MAX_COUNT]; + size_t mic_count; + + get_mic_characteristics(mic_array, &mic_count); + + dprintf(fd, " Microphone count: %zd\n", mic_count); + size_t idx; + for (idx = 0; idx < mic_count; idx++) { + dprintf(fd, " Microphone: %zd\n", idx); + dprintf(fd, " Address: %s\n", mic_array[idx].address); + dprintf(fd, " Device: %d\n", mic_array[idx].device); + dprintf(fd, " Sensitivity (dB): %.2f\n", mic_array[idx].sensitivity); + } + return 0; } @@ -399,14 +610,154 @@ static int in_set_gain(struct audio_stream_in *stream, float gain) static ssize_t in_read(struct audio_stream_in *stream, void* buffer, size_t bytes) { - ALOGV("in_read: bytes %zu", bytes); - /* XXX: fake timing for audio input */ - usleep((int64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) / - in_get_sample_rate(&stream->common)); - memset(buffer, 0, bytes); + int ret; + struct alsa_stream_in *in = (struct alsa_stream_in *)stream; + struct alsa_audio_device *adev = in->dev; + size_t frame_size = audio_stream_in_frame_size(stream); + size_t in_frames = bytes / frame_size; + + ALOGV("in_read: stream: %d, bytes %zu", in->source, bytes); + + /* Special handling for Echo Reference: simply get the reference from FIFO. + * The format and sample rate should be specified by arguments to adev_open_input_stream. */ + if (in->source == AUDIO_SOURCE_ECHO_REFERENCE) { + struct aec_info info; + info.bytes = bytes; + + const uint64_t time_increment_nsec = (uint64_t)bytes * NANOS_PER_SECOND / + audio_stream_in_frame_size(stream) / + in_get_sample_rate(&stream->common); + if (!aec_get_spk_running(adev->aec)) { + if (in->timestamp_nsec == 0) { + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + const uint64_t timestamp_nsec = audio_utils_ns_from_timespec(&now); + in->timestamp_nsec = timestamp_nsec; + } else { + in->timestamp_nsec += time_increment_nsec; + } + memset(buffer, 0, bytes); + const uint64_t time_increment_usec = time_increment_nsec / 1000; + usleep(time_increment_usec); + } else { + int ref_ret = get_reference_samples(adev->aec, buffer, &info); + if ((ref_ret) || (info.timestamp_usec == 0)) { + memset(buffer, 0, bytes); + in->timestamp_nsec += time_increment_nsec; + } else { + in->timestamp_nsec = 1000 * info.timestamp_usec; + } + } + in->frames_read += in_frames; + +#if DEBUG_AEC + FILE* fp_ref = fopen("/data/local/traces/aec_ref.pcm", "a+"); + if (fp_ref) { + fwrite((char*)buffer, 1, bytes, fp_ref); + fclose(fp_ref); + } else { + ALOGE("AEC debug: Could not open file aec_ref.pcm!"); + } + FILE* fp_ref_ts = fopen("/data/local/traces/aec_ref_timestamps.txt", "a+"); + if (fp_ref_ts) { + fprintf(fp_ref_ts, "%" PRIu64 "\n", in->timestamp_nsec); + fclose(fp_ref_ts); + } else { + ALOGE("AEC debug: Could not open file aec_ref_timestamps.txt!"); + } +#endif + return info.bytes; + } + + /* Microphone input stream read */ + + /* acquiring hw device mutex systematically is useful if a low priority thread is waiting + * on the input stream mutex - e.g. executing select_mode() while holding the hw device + * mutex + */ + pthread_mutex_lock(&in->lock); + pthread_mutex_lock(&adev->lock); + if (in->standby) { + ret = start_input_stream(in); + if (ret != 0) { + pthread_mutex_unlock(&adev->lock); + ALOGE("start_input_stream failed with code %d", ret); + goto exit; + } + in->standby = false; + } + + pthread_mutex_unlock(&adev->lock); + + ret = pcm_read(in->pcm, buffer, in_frames * frame_size); + struct aec_info info; + get_pcm_timestamp(in->pcm, in->config.rate, &info, false /*isOutput*/); + if (ret == 0) { + in->frames_read += in_frames; + in->timestamp_nsec = audio_utils_ns_from_timespec(&info.timestamp); + } + else { + ALOGE("pcm_read failed with code %d", ret); + } + +exit: + pthread_mutex_unlock(&in->lock); + + bool mic_muted = false; + adev_get_mic_mute((struct audio_hw_device*)adev, &mic_muted); + if (mic_muted) { + memset(buffer, 0, bytes); + } + + if (ret != 0) { + usleep((int64_t)bytes * 1000000 / audio_stream_in_frame_size(stream) / + in_get_sample_rate(&stream->common)); + } else { + /* Process AEC if available */ + /* TODO move to a separate thread */ + if (!mic_muted) { + info.bytes = bytes; + int aec_ret = process_aec(adev->aec, buffer, &info); + if (aec_ret) { + ALOGE("process_aec returned error code %d", aec_ret); + } + } + } + +#if DEBUG_AEC && !defined(AEC_HAL) + FILE* fp_in = fopen("/data/local/traces/aec_in.pcm", "a+"); + if (fp_in) { + fwrite((char*)buffer, 1, bytes, fp_in); + fclose(fp_in); + } else { + ALOGE("AEC debug: Could not open file aec_in.pcm!"); + } + FILE* fp_mic_ts = fopen("/data/local/traces/aec_in_timestamps.txt", "a+"); + if (fp_mic_ts) { + fprintf(fp_mic_ts, "%" PRIu64 "\n", in->timestamp_nsec); + fclose(fp_mic_ts); + } else { + ALOGE("AEC debug: Could not open file aec_in_timestamps.txt!"); + } +#endif + return bytes; } +static int in_get_capture_position(const struct audio_stream_in* stream, int64_t* frames, + int64_t* time) { + if (stream == NULL || frames == NULL || time == NULL) { + return -EINVAL; + } + struct alsa_stream_in* in = (struct alsa_stream_in*)stream; + + *frames = in->frames_read; + *time = in->timestamp_nsec; + ALOGV("%s: source: %d, timestamp (nsec): %" PRIu64, __func__, in->source, *time); + + return 0; +} + static uint32_t in_get_input_frames_lost(struct audio_stream_in *stream) { return 0; @@ -437,7 +788,9 @@ static int adev_open_output_stream(struct audio_hw_device *dev, struct pcm_params *params; int ret = 0; - params = pcm_params_get(CARD_OUT, PORT_CODEC, PCM_OUT); + int out_port = get_audio_output_port(devices); + + params = pcm_params_get(CARD_OUT, out_port, PCM_OUT); if (!params) return -ENOSYS; @@ -465,9 +818,9 @@ static int adev_open_output_stream(struct audio_hw_device *dev, out->stream.get_presentation_position = out_get_presentation_position; out->config.channels = CHANNEL_STEREO; - out->config.rate = CODEC_SAMPLING_RATE; + out->config.rate = PLAYBACK_CODEC_SAMPLING_RATE; out->config.format = PCM_FORMAT_S16_LE; - out->config.period_size = PERIOD_SIZE; + out->config.period_size = PLAYBACK_PERIOD_SIZE; out->config.period_count = PLAYBACK_PERIOD_COUNT; if (out->config.rate != config->sample_rate || @@ -479,12 +832,13 @@ static int adev_open_output_stream(struct audio_hw_device *dev, ret = -EINVAL; } - ALOGI("adev_open_output_stream selects channels=%d rate=%d format=%d", - out->config.channels, out->config.rate, out->config.format); + ALOGI("adev_open_output_stream selects channels=%d rate=%d format=%d, devices=%d", + out->config.channels, out->config.rate, out->config.format, devices); out->dev = ladev; out->standby = 1; out->unavailable = false; + out->devices = devices; config->format = out_get_format(&out->stream.common); config->channel_mask = out_get_channels(&out->stream.common); @@ -492,9 +846,25 @@ static int adev_open_output_stream(struct audio_hw_device *dev, *stream_out = &out->stream; + out->speaker_eq = NULL; + if (out_port == PORT_INTERNAL_SPEAKER) { + out_set_eq(out); + if (out->speaker_eq == NULL) { + ALOGE("%s: Failed to initialize speaker EQ", __func__); + } + } + /* TODO The retry mechanism isn't implemented in AudioPolicyManager/AudioFlinger. */ ret = 0; + if (ret == 0) { + int aec_ret = init_aec_reference_config(ladev->aec, out); + if (aec_ret) { + ALOGE("AEC: Speaker config init failed!"); + return -EINVAL; + } + } + return ret; } @@ -502,6 +872,10 @@ static void adev_close_output_stream(struct audio_hw_device *dev, struct audio_stream_out *stream) { ALOGV("adev_close_output_stream..."); + struct alsa_audio_device *adev = (struct alsa_audio_device *)dev; + destroy_aec_reference_config(adev->aec); + struct alsa_stream_out* out = (struct alsa_stream_out*)stream; + fir_release(out->speaker_eq); free(stream); } @@ -518,6 +892,17 @@ static char * adev_get_parameters(const struct audio_hw_device *dev, return strdup(""); } +static int adev_get_microphones(const struct audio_hw_device* dev, + struct audio_microphone_characteristic_t* mic_array, + size_t* mic_count) { + ALOGV("adev_get_microphones"); + if ((mic_array == NULL) || (mic_count == NULL)) { + return -EINVAL; + } + get_mic_characteristics(mic_array, mic_count); + return 0; +} + static int adev_init_check(const struct audio_hw_device *dev) { ALOGV("adev_init_check"); @@ -563,36 +948,49 @@ static int adev_set_mode(struct audio_hw_device *dev, audio_mode_t mode) static int adev_set_mic_mute(struct audio_hw_device *dev, bool state) { ALOGV("adev_set_mic_mute: %d",state); - return -ENOSYS; + struct alsa_audio_device *adev = (struct alsa_audio_device *)dev; + pthread_mutex_lock(&adev->lock); + adev->mic_mute = state; + pthread_mutex_unlock(&adev->lock); + return 0; } static int adev_get_mic_mute(const struct audio_hw_device *dev, bool *state) { ALOGV("adev_get_mic_mute"); - return -ENOSYS; + struct alsa_audio_device *adev = (struct alsa_audio_device *)dev; + pthread_mutex_lock(&adev->lock); + *state = adev->mic_mute; + pthread_mutex_unlock(&adev->lock); + return 0; } static size_t adev_get_input_buffer_size(const struct audio_hw_device *dev, const struct audio_config *config) { - ALOGV("adev_get_input_buffer_size: %d", 320); - return 320; + size_t buffer_size = + get_input_buffer_size(CAPTURE_PERIOD_SIZE, config->format, config->channel_mask); + ALOGV("adev_get_input_buffer_size: %zu", buffer_size); + return buffer_size; } -static int adev_open_input_stream(struct audio_hw_device __unused *dev, - audio_io_handle_t handle, - audio_devices_t devices, - struct audio_config *config, - struct audio_stream_in **stream_in, - audio_input_flags_t flags __unused, - const char *address __unused, - audio_source_t source __unused) -{ - struct stub_stream_in *in; - +static int adev_open_input_stream(struct audio_hw_device* dev, audio_io_handle_t handle, + audio_devices_t devices, struct audio_config* config, + struct audio_stream_in** stream_in, + audio_input_flags_t flags __unused, const char* address __unused, + audio_source_t source) { ALOGV("adev_open_input_stream..."); - in = (struct stub_stream_in *)calloc(1, sizeof(struct stub_stream_in)); + struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev; + struct alsa_stream_in *in; + struct pcm_params *params; + int ret = 0; + + params = pcm_params_get(CARD_IN, PORT_BUILTIN_MIC, PCM_IN); + if (!params) + return -ENOSYS; + + in = (struct alsa_stream_in *)calloc(1, sizeof(struct alsa_stream_in)); if (!in) return -ENOMEM; @@ -611,15 +1009,75 @@ static int adev_open_input_stream(struct audio_hw_device __unused *dev, in->stream.set_gain = in_set_gain; in->stream.read = in_read; in->stream.get_input_frames_lost = in_get_input_frames_lost; + in->stream.get_capture_position = in_get_capture_position; + in->stream.get_active_microphones = in_get_active_microphones; + + in->config.channels = CHANNEL_STEREO; + if (source == AUDIO_SOURCE_ECHO_REFERENCE) { + in->config.rate = PLAYBACK_CODEC_SAMPLING_RATE; + } else { + in->config.rate = CAPTURE_CODEC_SAMPLING_RATE; + } + in->config.format = PCM_FORMAT_S32_LE; + in->config.period_size = CAPTURE_PERIOD_SIZE; + in->config.period_count = CAPTURE_PERIOD_COUNT; - *stream_in = &in->stream; - return 0; + if (in->config.rate != config->sample_rate || + audio_channel_count_from_in_mask(config->channel_mask) != CHANNEL_STEREO || + in->config.format != pcm_format_from_audio_format(config->format) ) { + ret = -EINVAL; + } + + ALOGI("adev_open_input_stream selects channels=%d rate=%d format=%d source=%d", + in->config.channels, in->config.rate, in->config.format, source); + + in->dev = ladev; + in->standby = true; + in->unavailable = false; + in->source = source; + in->devices = devices; + + config->format = in_get_format(&in->stream.common); + config->channel_mask = in_get_channels(&in->stream.common); + config->sample_rate = in_get_sample_rate(&in->stream.common); + + /* If AEC is in the app, only configure based on ECHO_REFERENCE spec. + * If AEC is in the HAL, configure using the given mic stream. */ + bool aecInput = true; +#if !defined(AEC_HAL) + aecInput = (in->source == AUDIO_SOURCE_ECHO_REFERENCE); +#endif + + if ((ret == 0) && aecInput) { + int aec_ret = init_aec_mic_config(ladev->aec, in); + if (aec_ret) { + ALOGE("AEC: Mic config init failed!"); + return -EINVAL; + } + } + + if (ret) { + free(in); + } else { + *stream_in = &in->stream; + } + +#if DEBUG_AEC + remove("/data/local/traces/aec_ref.pcm"); + remove("/data/local/traces/aec_in.pcm"); + remove("/data/local/traces/aec_ref_timestamps.txt"); + remove("/data/local/traces/aec_in_timestamps.txt"); +#endif + return ret; } static void adev_close_input_stream(struct audio_hw_device *dev, - struct audio_stream_in *in) + struct audio_stream_in *stream) { ALOGV("adev_close_input_stream..."); + struct alsa_audio_device *adev = (struct alsa_audio_device *)dev; + destroy_aec_mic_config(adev->aec); + free(stream); return; } @@ -632,6 +1090,9 @@ static int adev_dump(const audio_hw_device_t *device, int fd) static int adev_close(hw_device_t *device) { ALOGV("adev_close"); + + struct alsa_audio_device *adev = (struct alsa_audio_device *)device; + release_aec(adev->aec); free(device); return 0; } @@ -671,26 +1132,10 @@ static int adev_open(const hw_module_t* module, const char* name, adev->hw_device.open_input_stream = adev_open_input_stream; adev->hw_device.close_input_stream = adev_close_input_stream; adev->hw_device.dump = adev_dump; - - adev->devices = AUDIO_DEVICE_NONE; + adev->hw_device.get_microphones = adev_get_microphones; *device = &adev->hw_device.common; - /* TODO: Ideally we should use Alsa UCM instead of libaudioroute - * because with Alsa UCM: - * - We can share the work with GNU/Linux. For instance we - * might have automatic support for the PinePhone and our - * work would benefit GNU/Linux distributions as well. - * - It opens the door to generic images: With Alsa UCM, we - * don't need build time configuration anymore: we could - * ship a single audio library and Alsa UCM would take care - * of the routing and abstract away the device specific part - * for us. If we code that ourselves, it will probably not - * be as fine grained nor as good as Alsa UCM. - * - It can be experimented with without even recompiling, this - * could make porting Replicant to new devices much less time - * consuming and way easier. - */ adev->mixer = mixer_open(CARD_OUT); if (!adev->mixer) { @@ -698,13 +1143,20 @@ static int adev_open(const hw_module_t* module, const char* name, return -EINVAL; } - /* Set default audio route */ adev->audio_route = audio_route_init(CARD_OUT, MIXER_XML_PATH); if (!adev->audio_route) { ALOGE("%s: Failed to init audio route controls, aborting.", __func__); return -EINVAL; } + pthread_mutex_lock(&adev->lock); + if (init_aec(CAPTURE_CODEC_SAMPLING_RATE, NUM_AEC_REFERENCE_CHANNELS, + CHANNEL_STEREO, &adev->aec)) { + pthread_mutex_unlock(&adev->lock); + return -EINVAL; + } + pthread_mutex_unlock(&adev->lock); + return 0; } @@ -718,7 +1170,7 @@ struct audio_module HAL_MODULE_INFO_SYM = { .module_api_version = AUDIO_MODULE_API_VERSION_0_1, .hal_api_version = HARDWARE_HAL_API_VERSION, .id = AUDIO_HARDWARE_MODULE_ID, - .name = "Generic Audio HAL for dragonboards", + .name = "Yukawa audio HW HAL", .author = "The Android Open Source Project", .methods = &hal_module_methods, }, |