Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 1 | /* st_hw_session_pcm.h |
| 2 | * |
Saurav Kumar | 5e2957e | 2020-06-03 18:22:51 +0530 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, The Linux Foundation. All rights reserved. |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are |
| 7 | * met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following |
| 12 | * disclaimer in the documentation and/or other materials provided |
| 13 | * with the distribution. |
| 14 | * * Neither the name of The Linux Foundation nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived |
| 16 | * from this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 19 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 22 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 25 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 27 | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 28 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | #include <pthread.h> |
| 31 | #include <tinyalsa/asoundlib.h> |
| 32 | #include <audio_route/audio_route.h> |
| 33 | |
| 34 | #include "sound_trigger_platform.h" |
| 35 | #include "st_hw_session.h" |
| 36 | #include "st_common_defs.h" |
| 37 | |
| 38 | struct sound_trigger_device; |
| 39 | |
Saurav Kumar | 5e2957e | 2020-06-03 18:22:51 +0530 | [diff] [blame] | 40 | |
| 41 | #define ST_FFV_CONFIG_FILE_NAME "BF_1out.cfg" |
| 42 | #define FFV_LIB_NAME "libffv.so" |
| 43 | #define ESP_LIB_NAME "libesp.so" |
Quinn Male | 2e88375 | 2019-03-22 11:28:54 -0700 | [diff] [blame] | 44 | |
| 45 | #define SOUND_TRIGGER_MAX_EVNT_PAYLOAD_SIZE (256) |
| 46 | |
| 47 | #define SOUND_TRIGGER_PCM_MAX_RETRY (10) |
| 48 | #define SOUND_TRIGGER_PCM_SLEEP_WAIT (1000) |
| 49 | |
| 50 | #define SOUND_TRIGGER_PCM_PERIOD_COUNT (8) |
| 51 | #define SOUND_TRIGGER_PCM_PERIOD_SIZE \ |
| 52 | ((((SOUND_TRIGGER_SAMPLING_RATE_16000 * SOUND_TRIGGER_PCM_BUFFER_DURATION_MS) \ |
| 53 | /(SOUND_TRIGGER_PCM_PERIOD_COUNT * 1000)) + 0x1f) & ~0x1f) |
| 54 | |
| 55 | #define ST_EC_REF_LOOPBACK_DEVICE_MONO "ec-ref-loopback-mono" |
| 56 | #define ST_EC_REF_LOOPBACK_DEVICE_STEREO "ec-ref-loopback-stereo" |
| 57 | #define ST_EC_REF_LOOPBACK_DEVICE_QUAD "ec-ref-loopback-quad" |
| 58 | |
| 59 | |
| 60 | #define NUM_PROCESS_BUFS 4 |
| 61 | #define ST_ESP_FRAME_LENGTH_MS 16 |
| 62 | |
| 63 | enum det_event_keys { |
| 64 | KWD_CONFIDENCE_LEVEL = 0x0, |
| 65 | KWD_INDEX, |
| 66 | KWD_MAX |
| 67 | }; |
| 68 | |
| 69 | struct process_buf { |
| 70 | void *data; |
| 71 | int length; |
| 72 | int16_t *in_buf_ptr; |
| 73 | int16_t *ec_ref_buf_ptr; |
| 74 | int16_t *out_buf_ptr; |
| 75 | }; |
| 76 | |
| 77 | struct process_buf_queue { |
| 78 | struct process_buf_queue *next; |
| 79 | struct process_buf buffer; |
| 80 | }; |
| 81 | |
| 82 | struct st_hw_session_pcm { |
| 83 | st_hw_session_t common; |
| 84 | |
| 85 | void *handle; /* handle to ffv library object */ |
| 86 | void *esp_handle; /* handle to esp library object */ |
| 87 | |
| 88 | pthread_t callback_thread; |
| 89 | bool exit_callback_thread; |
| 90 | |
| 91 | pthread_t capture_thread; |
| 92 | bool exit_capture_thread; |
| 93 | |
| 94 | int pcm_id; |
| 95 | struct pcm *pcm; |
| 96 | struct pcm_config capture_config; |
| 97 | struct pcm_config out_config; |
| 98 | struct pcm_config lab_config; |
| 99 | |
| 100 | int ec_ref_pcm_id; |
| 101 | struct pcm *ec_ref_pcm; |
| 102 | struct pcm_config ec_ref_config; |
| 103 | unsigned int ec_ref_use_case_idx; |
| 104 | |
| 105 | bool use_topology_intf; |
| 106 | |
| 107 | struct st_hw_session *transit_ses; |
| 108 | |
| 109 | unsigned char *in_buf; /* buffer received from pcm read */ |
| 110 | unsigned int in_buf_size; |
| 111 | |
| 112 | unsigned char *ec_ref_buf; /* ec ref data received from pcm read */ |
| 113 | unsigned int ec_ref_buf_size; |
| 114 | |
| 115 | unsigned char *split_in_buf; /* in buffer post ec ref split */ |
| 116 | unsigned int split_in_buf_size; |
| 117 | |
| 118 | unsigned char *out_buf; /* ffv library output buffer */ |
| 119 | unsigned int out_buf_size; |
| 120 | |
| 121 | unsigned char *bsp_out_buf; /* ffv library bsp output buffer */ |
| 122 | unsigned int bsp_out_buf_size; |
| 123 | |
| 124 | pthread_mutex_t callback_thread_lock; |
| 125 | pthread_mutex_t capture_thread_lock; |
| 126 | |
| 127 | bool buffers_allocated; |
| 128 | bool lab_buffers_allocated; |
| 129 | size_t read_buf_len; |
| 130 | |
| 131 | /* Buffers used for LAB*/ |
| 132 | unsigned char *lab_cap_buf; /* small buffer to hold one chunk received from library */ |
| 133 | unsigned int lab_cap_buf_size; |
| 134 | unsigned char *lab_out_buf; /* larger buffer that stores samples pulled from library |
| 135 | this buffer should hold enough samples to handle the |
| 136 | delay from generating the recognition event until |
| 137 | upper layers start pulling data */ |
| 138 | unsigned int lab_out_buf_size; |
| 139 | unsigned int lab_out_buf_unread_bytes; |
| 140 | unsigned char *lab_out_buf_start_ptr; |
| 141 | unsigned char *lab_out_buf_end_ptr; |
| 142 | unsigned char *lab_out_buf_wr_ptr; |
| 143 | unsigned char *lab_out_buf_rd_ptr; |
| 144 | pthread_cond_t lab_out_buf_cond; |
| 145 | pthread_mutex_t lab_out_buf_lock; |
| 146 | |
| 147 | bool exit_lab_processing; |
| 148 | |
| 149 | struct listnode ev_list; |
| 150 | pthread_cond_t ev_cond; |
| 151 | |
| 152 | pthread_t st_ffv_process_thread; |
| 153 | pthread_mutex_t st_ffv_process_lock; |
| 154 | bool exit_st_ffv_process_thread; |
| 155 | bool st_ffv_process_thread_started; |
| 156 | pthread_cond_t st_ffv_process_cond; |
| 157 | pthread_cond_t st_ffv_capture_cond; |
| 158 | void *process_buf_data; |
| 159 | struct process_buf_queue *process_buf_free; |
| 160 | struct process_buf_queue *process_buf; |
| 161 | struct process_buf_queue process_buf_nodes[NUM_PROCESS_BUFS]; |
| 162 | |
| 163 | char *ec_ref_dev_name; |
| 164 | bool session_restart_done; |
| 165 | bool capture_thread_started; |
| 166 | }; |
| 167 | |
| 168 | typedef struct st_hw_session_pcm st_hw_session_pcm_t; |
| 169 | |
| 170 | #ifdef ST_ARM_ENABLED |
| 171 | int st_hw_sess_pcm_init(st_hw_session_t *const p_ses, |
| 172 | hw_ses_event_callback_t cb, void *cookie, st_exec_mode_t exec_mode, |
| 173 | struct st_vendor_info *v_info, sound_model_handle_t sm_handle, |
| 174 | struct sound_trigger_device *stdev); |
| 175 | void st_hw_sess_pcm_deinit(st_hw_session_t *const p_ses); |
| 176 | int st_hw_pcm_init(void); |
| 177 | void st_hw_pcm_deinit(void); |
| 178 | #else |
| 179 | #define st_hw_sess_pcm_init(p_ses, cb, cookie, exec_mode, v_info, \ |
| 180 | sm_handle, stdev) (0) |
| 181 | #define st_hw_sess_pcm_deinit(p_ses) (0) |
| 182 | #define st_hw_pcm_init() (0) |
| 183 | #define st_hw_pcm_deinit() (0) |
| 184 | #endif |