summaryrefslogtreecommitdiff
path: root/audio/audio_hw.c
diff options
context:
space:
mode:
Diffstat (limited to 'audio/audio_hw.c')
-rw-r--r--audio/audio_hw.c823
1 files changed, 676 insertions, 147 deletions
diff --git a/audio/audio_hw.c b/audio/audio_hw.c
index d601ea8..75d3ff0 100644
--- a/audio/audio_hw.c
+++ b/audio/audio_hw.c
@@ -12,18 +12,22 @@
* 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 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 <string.h>
#include <log/log.h>
#include <cutils/str_parms.h>
@@ -33,84 +37,178 @@
#include <system/audio.h>
#include <hardware/audio.h>
-#include <sound/asound.h>
-#include <tinyalsa/asoundlib.h>
-#include <audio_utils/resampler.h>
+#include <audio_effects/effect_aec.h>
+#include <audio_route/audio_route.h>
+#include <audio_utils/clock.h>
#include <audio_utils/echo_reference.h>
-#include <hardware/audio_effect.h>
+#include <audio_utils/resampler.h>
+#include <cutils/properties.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>
+
+#include "audio_aec.h"
+#include "audio_hw.h"
+
+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);
+
+static bool is_aec_input(const struct alsa_stream_in* in) {
+ /* 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 aec_input = true;
+#if !defined(AEC_HAL)
+ aec_input = (in->source == AUDIO_SOURCE_ECHO_REFERENCE);
+#endif
+ return aec_input;
+}
-#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;
-};
+static int get_audio_output_port(audio_devices_t devices) {
+ /* Only HDMI out for now #FIXME */
+ return PORT_HDMI;
+}
-struct alsa_audio_device {
- struct audio_hw_device hw_device;
+static int get_audio_card(int direction, int port) {
+ struct pcm_params* params = NULL;
+ int card = 0;
- 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;
- bool mic_mute;
-};
+ while (!params && card < 8) {
+ /* Find the first input/output device that works */
+ params = pcm_params_get(card, port, direction);
+ card++;
+ }
+ pcm_params_free(params);
-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;
-};
+ return card - 1;
+}
+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);
+ int out_card = get_audio_card(PCM_OUT, out_port);
+
+ while (1) {
+ out->pcm = pcm_open(out_card, 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;
}
@@ -133,7 +231,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);
}
@@ -162,12 +260,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;
}
@@ -207,16 +308,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)
@@ -229,14 +330,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,
@@ -248,6 +349,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
@@ -261,14 +364,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);
@@ -283,30 +401,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;
}
@@ -327,14 +439,65 @@ 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;
+ int in_card = get_audio_card(PCM_IN, PORT_BUILTIN_MIC);
+
+ while (1) {
+ in->pcm = pcm_open(in_card, 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)
@@ -343,21 +506,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)
@@ -365,13 +536,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;
}
@@ -394,14 +638,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;
@@ -428,17 +812,18 @@ static int adev_open_output_stream(struct audio_hw_device *dev,
ALOGV("adev_open_output_stream...");
struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
- struct alsa_stream_out *out;
- struct pcm_params *params;
- int ret = 0;
-
- params = pcm_params_get(CARD_OUT, PORT_CODEC, PCM_OUT);
- if (!params)
+ int out_port = get_audio_output_port(devices);
+ int out_card = get_audio_card(PCM_OUT, out_port);
+ struct pcm_params* params = pcm_params_get(out_card, out_port, PCM_OUT);
+ if (!params) {
return -ENOSYS;
+ }
- out = (struct alsa_stream_out *)calloc(1, sizeof(struct alsa_stream_out));
- if (!out)
+ struct alsa_stream_out* out =
+ (struct alsa_stream_out*)calloc(1, sizeof(struct alsa_stream_out));
+ if (!out) {
return -ENOMEM;
+ }
out->stream.common.get_sample_rate = out_get_sample_rate;
out->stream.common.set_sample_rate = out_set_sample_rate;
@@ -460,9 +845,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 ||
@@ -471,32 +856,53 @@ static int adev_open_output_stream(struct audio_hw_device *dev,
config->sample_rate = out->config.rate;
config->format = audio_format_from_pcm_format(out->config.format);
config->channel_mask = audio_channel_out_mask_from_count(CHANNEL_STEREO);
- ret = -EINVAL;
+ goto error_1;
}
- 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);
config->sample_rate = out_get_sample_rate(&out->stream.common);
- *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;
+ int aec_ret = init_aec_reference_config(ladev->aec, out);
+ if (aec_ret) {
+ ALOGE("AEC: Speaker config init failed!");
+ goto error_2;
+ }
- return ret;
+ *stream_out = &out->stream;
+ return 0;
+
+error_2:
+ fir_release(out->speaker_eq);
+error_1:
+ free(out);
+ return -EINVAL;
}
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);
}
@@ -513,6 +919,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");
@@ -558,38 +975,51 @@ 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));
- if (!in)
+ struct alsa_audio_device *ladev = (struct alsa_audio_device *)dev;
+
+ int in_card = get_audio_card(PCM_IN, PORT_BUILTIN_MIC);
+ struct pcm_params* params = pcm_params_get(in_card, PORT_BUILTIN_MIC, PCM_IN);
+ if (!params) {
+ return -ENOSYS;
+ }
+
+ struct alsa_stream_in* in = (struct alsa_stream_in*)calloc(1, sizeof(struct alsa_stream_in));
+ if (!in) {
return -ENOMEM;
+ }
in->stream.common.get_sample_rate = in_get_sample_rate;
in->stream.common.set_sample_rate = in_set_sample_rate;
@@ -606,15 +1036,69 @@ 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;
+
+ 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) ) {
+ 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);
+ goto error_1;
+ }
+
+ 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;
+
+ if (is_aec_input(in)) {
+ int aec_ret = init_aec_mic_config(ladev->aec, in);
+ if (aec_ret) {
+ ALOGE("AEC: Mic config init failed!");
+ goto error_1;
+ }
+ }
+
+#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
*stream_in = &in->stream;
return 0;
+
+error_1:
+ free(in);
+ return -EINVAL;
}
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_stream_in* in = (struct alsa_stream_in*)stream;
+ if (is_aec_input(in)) {
+ destroy_aec_mic_config(in->dev->aec);
+ }
+ free(stream);
return;
}
@@ -627,6 +1111,11 @@ 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);
+ audio_route_free(adev->audio_route);
+ mixer_close(adev->mixer);
free(device);
return 0;
}
@@ -634,16 +1123,19 @@ static int adev_close(hw_device_t *device)
static int adev_open(const hw_module_t* module, const char* name,
hw_device_t** device)
{
- struct alsa_audio_device *adev;
-
+ char vendor_hw[PROPERTY_VALUE_MAX] = {0};
+ // Prefix for the hdmi path, the board name is the suffix
+ char path_name[256] = "hdmi_";
ALOGV("adev_open: %s", name);
- if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0)
+ if (strcmp(name, AUDIO_HARDWARE_INTERFACE) != 0) {
return -EINVAL;
+ }
- adev = calloc(1, sizeof(struct alsa_audio_device));
- if (!adev)
+ struct alsa_audio_device* adev = calloc(1, sizeof(struct alsa_audio_device));
+ if (!adev) {
return -ENOMEM;
+ }
adev->hw_device.common.tag = HARDWARE_DEVICE_TAG;
adev->hw_device.common.version = AUDIO_DEVICE_API_VERSION_2_0;
@@ -666,12 +1158,49 @@ 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;
+ int out_card = get_audio_card(PCM_OUT, 0);
+ adev->mixer = mixer_open(out_card);
+ if (!adev->mixer) {
+ ALOGE("Unable to open the mixer, aborting.");
+ goto error_1;
+ }
+
+ adev->audio_route = audio_route_init(out_card, MIXER_XML_PATH);
+ if (!adev->audio_route) {
+ ALOGE("%s: Failed to init audio route controls, aborting.", __func__);
+ goto error_2;
+ }
+
+ /*
+ * To support both the db845c and rb5 we need to used the right mixer path
+ * we do this by checking the hardware name. Which is set at boot time.
+ */
+ property_get("vendor.hw", vendor_hw, "db845c");
+ strlcat(path_name, vendor_hw, 256);
+ ALOGV("%s: Using mixer path: %s", __func__, path_name);
+ audio_route_apply_and_update_path(adev->audio_route, path_name);
+
+ 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);
+ goto error_3;
+ }
+ pthread_mutex_unlock(&adev->lock);
+
return 0;
+
+error_3:
+ audio_route_free(adev->audio_route);
+error_2:
+ mixer_close(adev->mixer);
+error_1:
+ free(adev);
+ return -EINVAL;
}
static struct hw_module_methods_t hal_module_methods = {
@@ -684,7 +1213,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,
},