hal: Add support for sound trigger feature
Add sound trigger extension to communicate with sound trigger hal
for reading keyword data and manage concurrencies.
Change-Id: Iebbc0bf60be3bb12d752a1c905f71e9ea14fbf46
diff --git a/hal/Android.mk b/hal/Android.mk
index 8bb668a..346fcb6 100644
--- a/hal/Android.mk
+++ b/hal/Android.mk
@@ -64,6 +64,13 @@
LOCAL_CFLAGS += -DNO_AUDIO_OUT
endif
+ifeq ($(strip $(BOARD_SUPPORTS_SOUND_TRIGGER)),true)
+ LOCAL_CFLAGS += -DSOUND_TRIGGER_ENABLED
+ LOCAL_CFLAGS += -DSOUND_TRIGGER_PLATFORM_NAME=$(TARGET_BOARD_PLATFORM)
+ LOCAL_C_INCLUDES += $(TARGET_OUT_HEADERS)/mm-audio/sound_trigger
+ LOCAL_SRC_FILES += audio_extn/soundtrigger.c
+endif
+
LOCAL_MODULE := audio.primary.$(TARGET_BOARD_PLATFORM)
LOCAL_MODULE_RELATIVE_PATH := hw
diff --git a/hal/audio_extn/audio_extn.h b/hal/audio_extn/audio_extn.h
index 26c2fb4..e4f0374 100644
--- a/hal/audio_extn/audio_extn.h
+++ b/hal/audio_extn/audio_extn.h
@@ -38,4 +38,35 @@
struct str_parms *parms);
#endif
+#ifndef SOUND_TRIGGER_ENABLED
+#define audio_extn_sound_trigger_init(adev) (0)
+#define audio_extn_sound_trigger_deinit(adev) (0)
+#define audio_extn_sound_trigger_update_device_status(snd_dev, event) (0)
+#define audio_extn_sound_trigger_set_parameters(adev, parms) (0)
+#define audio_extn_sound_trigger_check_and_get_session(in) (0)
+#define audio_extn_sound_trigger_stop_lab(in) (0)
+#define audio_extn_sound_trigger_read(in, buffer, bytes) (0)
+
+#else
+
+enum st_event_type {
+ ST_EVENT_SND_DEVICE_FREE,
+ ST_EVENT_SND_DEVICE_BUSY,
+ ST_EVENT_STREAM_FREE,
+ ST_EVENT_STREAM_BUSY
+};
+typedef enum st_event_type st_event_type_t;
+
+int audio_extn_sound_trigger_init(struct audio_device *adev);
+void audio_extn_sound_trigger_deinit(struct audio_device *adev);
+void audio_extn_sound_trigger_update_device_status(snd_device_t snd_device,
+ st_event_type_t event);
+void audio_extn_sound_trigger_set_parameters(struct audio_device *adev,
+ struct str_parms *parms);
+void audio_extn_sound_trigger_check_and_get_session(struct stream_in *in);
+void audio_extn_sound_trigger_stop_lab(struct stream_in *in);
+int audio_extn_sound_trigger_read(struct stream_in *in, void *buffer,
+ size_t bytes);
+#endif
+
#endif /* AUDIO_EXTN_H */
diff --git a/hal/audio_extn/soundtrigger.c b/hal/audio_extn/soundtrigger.c
new file mode 100644
index 0000000..4d09387
--- /dev/null
+++ b/hal/audio_extn/soundtrigger.c
@@ -0,0 +1,348 @@
+/*
+ * Copyright (C) 2015 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * 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.
+ */
+#define LOG_TAG "soundtrigger"
+/* #define LOG_NDEBUG 0 */
+#define LOG_NDDEBUG 0
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <cutils/log.h>
+#include "audio_hw.h"
+#include "audio_extn.h"
+#include "platform.h"
+#include "platform_api.h"
+#include "sound_trigger_prop_intf.h"
+
+#define XSTR(x) STR(x)
+#define STR(x) #x
+
+struct sound_trigger_info {
+ struct sound_trigger_session_info st_ses;
+ bool lab_stopped;
+ struct listnode list;
+};
+
+struct sound_trigger_audio_device {
+ void *lib_handle;
+ struct audio_device *adev;
+ sound_trigger_hw_call_back_t st_callback;
+ struct listnode st_ses_list;
+ pthread_mutex_t lock;
+};
+
+static struct sound_trigger_audio_device *st_dev;
+
+static struct sound_trigger_info *
+get_sound_trigger_info(int capture_handle)
+{
+ struct sound_trigger_info *st_ses_info = NULL;
+ struct listnode *node;
+ ALOGV("%s: list %d capture_handle %d", __func__,
+ list_empty(&st_dev->st_ses_list), capture_handle);
+ list_for_each(node, &st_dev->st_ses_list) {
+ st_ses_info = node_to_item(node, struct sound_trigger_info , list);
+ if (st_ses_info->st_ses.capture_handle == capture_handle)
+ return st_ses_info;
+ }
+ return NULL;
+}
+
+int audio_hw_call_back(sound_trigger_event_type_t event,
+ sound_trigger_event_info_t* config)
+{
+ int status = 0;
+ struct sound_trigger_info *st_ses_info;
+
+ if (!st_dev)
+ return -EINVAL;
+
+ pthread_mutex_lock(&st_dev->lock);
+ switch (event) {
+ case ST_EVENT_SESSION_REGISTER:
+ if (!config) {
+ ALOGE("%s: NULL config", __func__);
+ status = -EINVAL;
+ break;
+ }
+ st_ses_info= calloc(1, sizeof(struct sound_trigger_info ));
+ if (!st_ses_info) {
+ ALOGE("%s: st_ses_info alloc failed", __func__);
+ status = -ENOMEM;
+ break;
+ }
+ memcpy(&st_ses_info->st_ses, &config->st_ses, sizeof (config->st_ses));
+ ALOGV("%s: add capture_handle %d pcm %p", __func__,
+ st_ses_info->st_ses.capture_handle, st_ses_info->st_ses.pcm);
+ list_add_tail(&st_dev->st_ses_list, &st_ses_info->list);
+ break;
+
+ case ST_EVENT_SESSION_DEREGISTER:
+ if (!config) {
+ ALOGE("%s: NULL config", __func__);
+ status = -EINVAL;
+ break;
+ }
+ st_ses_info = get_sound_trigger_info(config->st_ses.capture_handle);
+ if (!st_ses_info) {
+ ALOGE("%s: pcm %p not in the list!", __func__, config->st_ses.pcm);
+ status = -EINVAL;
+ break;
+ }
+ ALOGV("%s: remove capture_handle %d pcm %p", __func__,
+ st_ses_info->st_ses.capture_handle, st_ses_info->st_ses.pcm);
+ list_remove(&st_ses_info->list);
+ free(st_ses_info);
+ break;
+ default:
+ ALOGW("%s: Unknown event %d", __func__, event);
+ break;
+ }
+ pthread_mutex_unlock(&st_dev->lock);
+ return status;
+}
+
+int audio_extn_sound_trigger_read(struct stream_in *in, void *buffer,
+ size_t bytes)
+{
+ int ret = -1;
+ struct sound_trigger_info *st_info = NULL;
+ audio_event_info_t event;
+
+ if (!st_dev)
+ return ret;
+
+ if (!in->is_st_session_active) {
+ ALOGE(" %s: Sound trigger is not active", __func__);
+ goto exit;
+ }
+ if (in->standby)
+ in->standby = false;
+
+ pthread_mutex_lock(&st_dev->lock);
+ st_info = get_sound_trigger_info(in->capture_handle);
+ pthread_mutex_unlock(&st_dev->lock);
+ if (st_info) {
+ event.u.aud_info.ses_info = &st_info->st_ses;
+ event.u.aud_info.buf = buffer;
+ event.u.aud_info.num_bytes = bytes;
+ ret = st_dev->st_callback(AUDIO_EVENT_READ_SAMPLES, &event);
+ }
+
+exit:
+ if (ret) {
+ if (-ENETRESET == ret)
+ in->is_st_session_active = false;
+ memset(buffer, 0, bytes);
+ ALOGV("%s: read failed status %d - sleep", __func__, ret);
+ usleep((bytes * 1000000) / (audio_stream_in_frame_size((struct audio_stream_in *)in) *
+ in->config.rate));
+ }
+ return ret;
+}
+
+void audio_extn_sound_trigger_stop_lab(struct stream_in *in)
+{
+ int status = 0;
+ struct sound_trigger_info *st_ses_info = NULL;
+ audio_event_info_t event;
+
+ if (!st_dev || !in)
+ return;
+
+ pthread_mutex_lock(&st_dev->lock);
+ st_ses_info = get_sound_trigger_info(in->capture_handle);
+ pthread_mutex_unlock(&st_dev->lock);
+ if (st_ses_info) {
+ event.u.ses_info = st_ses_info->st_ses;
+ ALOGV("%s: AUDIO_EVENT_STOP_LAB pcm %p", __func__, st_ses_info->st_ses.pcm);
+ st_dev->st_callback(AUDIO_EVENT_STOP_LAB, &event);
+ }
+}
+void audio_extn_sound_trigger_check_and_get_session(struct stream_in *in)
+{
+ struct sound_trigger_info *st_ses_info = NULL;
+ struct listnode *node;
+
+ if (!st_dev || !in)
+ return;
+
+ pthread_mutex_lock(&st_dev->lock);
+ in->is_st_session = false;
+ ALOGV("%s: list %d capture_handle %d", __func__,
+ list_empty(&st_dev->st_ses_list), in->capture_handle);
+ list_for_each(node, &st_dev->st_ses_list) {
+ st_ses_info = node_to_item(node, struct sound_trigger_info , list);
+ if (st_ses_info->st_ses.capture_handle == in->capture_handle) {
+ in->pcm = st_ses_info->st_ses.pcm;
+ in->config = st_ses_info->st_ses.config;
+ in->channel_mask = audio_channel_in_mask_from_count(in->config.channels);
+ in->is_st_session = true;
+ in->is_st_session_active = true;
+ ALOGD("%s: capture_handle %d is sound trigger", __func__, in->capture_handle);
+ break;
+ }
+ }
+ pthread_mutex_unlock(&st_dev->lock);
+}
+
+void audio_extn_sound_trigger_update_device_status(snd_device_t snd_device,
+ st_event_type_t event)
+{
+ int device_type = -1;
+
+ if (!st_dev)
+ return;
+
+ if (snd_device >= SND_DEVICE_OUT_BEGIN &&
+ snd_device < SND_DEVICE_OUT_END)
+ device_type = PCM_PLAYBACK;
+ else if (snd_device >= SND_DEVICE_IN_BEGIN &&
+ snd_device < SND_DEVICE_IN_END)
+ device_type = PCM_CAPTURE;
+ else {
+ ALOGE("%s: invalid device 0x%x, for event %d",
+ __func__, snd_device, event);
+ return;
+ }
+
+ ALOGI("%s: device 0x%x of type %d for Event %d",
+ __func__, snd_device, device_type, event);
+ if (device_type == PCM_CAPTURE) {
+ switch(event) {
+ case ST_EVENT_SND_DEVICE_FREE:
+ st_dev->st_callback(AUDIO_EVENT_CAPTURE_DEVICE_INACTIVE, NULL);
+ break;
+ case ST_EVENT_SND_DEVICE_BUSY:
+ st_dev->st_callback(AUDIO_EVENT_CAPTURE_DEVICE_ACTIVE, NULL);
+ break;
+ default:
+ ALOGW("%s:invalid event %d for device 0x%x",
+ __func__, event, snd_device);
+ }
+ }/*Events for output device, if required can be placed here in else*/
+}
+
+void audio_extn_sound_trigger_set_parameters(struct audio_device *adev __unused,
+ struct str_parms *params)
+{
+ audio_event_info_t event;
+ char value[32];
+ int ret, val;
+
+ if(!st_dev || !params) {
+ ALOGE("%s: str_params NULL", __func__);
+ return;
+ }
+
+ ret = str_parms_get_str(params, "SND_CARD_STATUS", value,
+ sizeof(value));
+ if (ret > 0) {
+ if (strstr(value, "OFFLINE")) {
+ event.u.status = SND_CARD_STATUS_OFFLINE;
+ st_dev->st_callback(AUDIO_EVENT_SSR, &event);
+ }
+ else if (strstr(value, "ONLINE")) {
+ event.u.status = SND_CARD_STATUS_ONLINE;
+ st_dev->st_callback(AUDIO_EVENT_SSR, &event);
+ }
+ else
+ ALOGE("%s: unknown snd_card_status", __func__);
+ }
+
+ ret = str_parms_get_str(params, "CPE_STATUS", value, sizeof(value));
+ if (ret > 0) {
+ if (strstr(value, "OFFLINE")) {
+ event.u.status = CPE_STATUS_OFFLINE;
+ st_dev->st_callback(AUDIO_EVENT_SSR, &event);
+ }
+ else if (strstr(value, "ONLINE")) {
+ event.u.status = CPE_STATUS_ONLINE;
+ st_dev->st_callback(AUDIO_EVENT_SSR, &event);
+ }
+ else
+ ALOGE("%s: unknown CPE status", __func__);
+ }
+
+ ret = str_parms_get_int(params, "SVA_NUM_SESSIONS", &val);
+ if (ret >= 0) {
+ event.u.value = val;
+ st_dev->st_callback(AUDIO_EVENT_NUM_ST_SESSIONS, &event);
+ }
+}
+
+int audio_extn_sound_trigger_init(struct audio_device *adev)
+{
+ int status = 0;
+ char sound_trigger_lib[100];
+ void *lib_handle;
+
+ ALOGI("%s: Enter", __func__);
+
+ st_dev = (struct sound_trigger_audio_device*)
+ calloc(1, sizeof(struct sound_trigger_audio_device));
+ if (!st_dev) {
+ ALOGE("%s: ERROR. sound trigger alloc failed", __func__);
+ return -ENOMEM;
+ }
+
+ snprintf(sound_trigger_lib, sizeof(sound_trigger_lib),
+ "/system/vendor/lib/hw/sound_trigger.primary.%s.so",
+ XSTR(SOUND_TRIGGER_PLATFORM_NAME));
+
+ st_dev->lib_handle = dlopen(sound_trigger_lib, RTLD_NOW);
+
+ if (st_dev->lib_handle == NULL) {
+ ALOGE("%s: DLOPEN failed for %s. error = %s", __func__, sound_trigger_lib,
+ dlerror());
+ status = -EINVAL;
+ goto cleanup;
+ }
+ ALOGI("%s: DLOPEN successful for %s", __func__, sound_trigger_lib);
+
+ st_dev->st_callback = (sound_trigger_hw_call_back_t)
+ dlsym(st_dev->lib_handle, "sound_trigger_hw_call_back");
+
+ if (st_dev->st_callback == NULL) {
+ ALOGE("%s: ERROR. dlsym Error:%s sound_trigger_hw_call_back", __func__,
+ dlerror());
+ goto cleanup;
+ }
+
+ st_dev->adev = adev;
+ list_init(&st_dev->st_ses_list);
+
+ return 0;
+
+cleanup:
+ if (st_dev->lib_handle)
+ dlclose(st_dev->lib_handle);
+ free(st_dev);
+ st_dev = NULL;
+ return status;
+
+}
+
+void audio_extn_sound_trigger_deinit(struct audio_device *adev)
+{
+ ALOGI("%s: Enter", __func__);
+ if (st_dev && (st_dev->adev == adev) && st_dev->lib_handle) {
+ dlclose(st_dev->lib_handle);
+ free(st_dev);
+ st_dev = NULL;
+ }
+}
diff --git a/hal/audio_hw.c b/hal/audio_hw.c
index 9c6c1a2..74ed5aa 100644
--- a/hal/audio_hw.c
+++ b/hal/audio_hw.c
@@ -273,8 +273,15 @@
return 0;
}
+ /* due to the possibility of calibration overwrite between listen
+ and audio, notify sound trigger hal before audio calibration is sent */
+ audio_extn_sound_trigger_update_device_status(snd_device,
+ ST_EVENT_SND_DEVICE_BUSY);
+
if (platform_send_audio_calibration(adev->platform, snd_device) < 0) {
adev->snd_dev_ref_cnt[snd_device]--;
+ audio_extn_sound_trigger_update_device_status(snd_device,
+ ST_EVENT_SND_DEVICE_FREE);
return -EINVAL;
}
@@ -302,6 +309,9 @@
const char * dev_path = platform_get_snd_device_name(snd_device);
ALOGD("%s: snd_device(%d: %s)", __func__, snd_device, dev_path);
audio_route_reset_and_update_path(adev->audio_route, dev_path);
+ audio_extn_sound_trigger_update_device_status(snd_device,
+ ST_EVENT_SND_DEVICE_FREE);
+
}
return 0;
}
@@ -1733,6 +1743,13 @@
int status = 0;
ALOGV("%s: enter", __func__);
pthread_mutex_lock(&in->lock);
+
+ if (!in->standby && in->is_st_session) {
+ ALOGD("%s: sound trigger pcm stop lab", __func__);
+ audio_extn_sound_trigger_stop_lab(in);
+ in->standby = true;
+ }
+
if (!in->standby) {
pthread_mutex_lock(&adev->lock);
in->standby = true;
@@ -1819,6 +1836,14 @@
int i, ret = -1;
pthread_mutex_lock(&in->lock);
+ if (in->is_st_session) {
+ ALOGVV(" %s: reading on st session bytes=%d", __func__, bytes);
+ /* Read from sound trigger HAL */
+ audio_extn_sound_trigger_read(in, buffer, bytes);
+ pthread_mutex_unlock(&in->lock);
+ return bytes;
+ }
+
if (in->standby) {
pthread_mutex_lock(&adev->lock);
ret = start_input_stream(in);
@@ -2358,7 +2383,7 @@
}
static int adev_open_input_stream(struct audio_hw_device *dev,
- audio_io_handle_t handle __unused,
+ audio_io_handle_t handle,
audio_devices_t devices,
struct audio_config *config,
struct audio_stream_in **stream_in,
@@ -2402,6 +2427,7 @@
in->dev = adev;
in->standby = 1;
in->channel_mask = config->channel_mask;
+ in->capture_handle = handle;
/* Update config params with the requested sample rate and channels */
if (in->device == AUDIO_DEVICE_IN_TELEPHONY_RX) {
@@ -2444,6 +2470,9 @@
in->config.channels = channel_count;
in->config.rate = config->sample_rate;
+ /* This stream could be for sound trigger lab,
+ get sound trigger pcm if present */
+ audio_extn_sound_trigger_check_and_get_session(in);
*stream_in = &in->stream;
ALOGV("%s: exit", __func__);
@@ -2613,6 +2642,7 @@
free(adev->snd_dev_ref_cnt);
platform_deinit(adev->platform);
audio_extn_extspk_deinit(adev->extspk);
+ audio_extn_sound_trigger_deinit(adev);
for (i = 0; i < ARRAY_SIZE(adev->use_case_table); ++i) {
pcm_params_free(adev->use_case_table[i]);
}
@@ -2701,6 +2731,7 @@
}
adev->extspk = audio_extn_extspk_init(adev);
+ audio_extn_sound_trigger_init(adev);
if (access(VISUALIZER_LIBRARY_PATH, R_OK) == 0) {
adev->visualizer_lib = dlopen(VISUALIZER_LIBRARY_PATH, RTLD_NOW);
diff --git a/hal/audio_hw.h b/hal/audio_hw.h
index 1888aa1..7f58388 100644
--- a/hal/audio_hw.h
+++ b/hal/audio_hw.h
@@ -167,6 +167,10 @@
bool enable_aec;
bool enable_ns;
+ audio_io_handle_t capture_handle;
+ bool is_st_session;
+ bool is_st_session_active;
+
struct audio_device *dev;
};