-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
st_hw_session_pcm.c
2235 lines (1975 loc) · 81.7 KB
/
st_hw_session_pcm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* st_hw_session_pcm.c
*
* This file implements the hw session functionality of SVA using capture path
*
* Copyright (c) 2017-2020, The Linux Foundation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of The Linux Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define LOG_TAG "sound_trigger_hw"
/* #define LOG_NDEBUG 0 */
#define LOG_NDDEBUG 0
#include <errno.h>
#include <stdlib.h>
#include <dlfcn.h>
#include <fcntl.h>
#include <sched.h>
#include <sys/resource.h>
#include <sys/prctl.h>
#include <sys/ioctl.h>
#include <cutils/log.h>
#include <cutils/atomic.h>
#include <system/thread_defs.h>
#include <stdarg.h>
#include <unistd.h>
#include "st_common_defs.h"
#include "sound_trigger_platform.h"
#include "st_hw_session_pcm.h"
#include "sound_trigger_hw.h"
#include "ffv_interface.h"
#include "esp_api.h"
/* uncomment if process buffers need to be queued */
//#define QUEUE_PROCESS_BUFFERS
typedef struct {
struct listnode node;
FfvEventType event_type;
void *ev_payload;
} callback_event_t;
typedef union {
ffv_history_buffer_length_param_t buffer_length_params;
ffv_src_tracking_param_t src_tracking_params;
ffv_target_channel_index_param_t ch_index_params;
esp_energy_levels_t esp_energy_levels_params;
} st_get_param_payload_t;
static int reg_sm(st_hw_session_t* p_ses, void *sm_data,
unsigned int sm_size, uint32_t model_id);
static int reg_sm_params(st_hw_session_t* p_ses, unsigned int recognition_mode,
bool capture_requested, struct sound_trigger_recognition_config *rc_config);
static int dereg_sm(st_hw_session_t* p_ses,uint32_t model_id __unused);
static int dereg_sm_params(st_hw_session_t* p_ses);
static int start(st_hw_session_t* p_ses);
static int stop(st_hw_session_t* p_ses);
static int stop_buffering(st_hw_session_t* p_ses);
static int set_device(st_hw_session_t *p_ses,
bool enable);
static int disable_device(st_hw_session_t *p_ses, bool setting_device);
static int enable_device(st_hw_session_t *p_ses, bool setting_device);
static void process_lab_capture(st_hw_session_t *p_ses);
static int restart(st_hw_session_t* p_ses, unsigned int recognition_mode,
struct sound_trigger_recognition_config *rc_config __unused);
static int read_pcm(st_hw_session_t *p_ses,
unsigned char *buf,
unsigned int bytes);
static int allocate_buffers(st_hw_session_pcm_t* p_ses);
static int deallocate_buffers(st_hw_session_pcm_t* p_ses);
static int get_param_data(st_hw_session_t* p_ses, const char *param,
void *payload, size_t payload_size, size_t *param_data_size);
static void st_event_callback(void *cb_data, void *handle, FfvEventType event_type,
void *event_payload, size_t event_payload_size);
static FfvStatusType (*ffv_init_fn)(void** handle, int num_tx_in_ch,
int num_out_ch, int num_ec_ref_ch, int frame_len, int sample_rate,
const char *config_file_name, char *svaModelBuffer,
uint32_t svaModelSize, int* totMemSize, int product_id, const char* license);
static void (*ffv_deinit_fn)(void* handle);
static void (*ffv_process_fn)(void *handle, const int16_t *in_pcm,
int16_t *out_pcm, const int16_t *ec_ref_pcm);
static void (*ffv_process_v2_fn)(void *handle, const int16_t *in_pcm,
int16_t *out_pcm, int16_t *bsp_out_pcm, const int16_t *ec_ref_pcm);
static int (*ffv_read_fn)(void* handle, int16_t *buf_pcm,
int max_buf_len);
static FfvStatusType (*ffv_get_param_fn)(void *handle, char *params_buffer_ptr,
int param_id, int buffer_size, int *param_size_ptr);
static FfvStatusType (*ffv_set_param_fn)(void *handle, char *params_buffer_ptr,
int param_id, int param_size);
static FfvStatusType (*ffv_register_event_callback_fn)(void *handle,
ffv_event_callback_fn_t fun_ptr, void *cb_data);
static EspStatusType (*esp_init_fn)(void **handle, int frame_len_ms,
int sample_rate, int* totMemSize);
static void (*esp_deinit_fn)(void *handle);
static void (*esp_process_fn)(void *handle, const int16 *in_pcm);
static EspStatusType (*esp_get_param_fn)(void *handle, char *params_buffer_ptr,
int param_id, int buffer_size, int *param_size_ptr);
static int send_detection_request(st_hw_session_t *p_ses __unused);
static struct pcm_config stdev_arm_pcm_config = {
.channels = SOUND_TRIGGER_CHANNEL_MODE_MONO,
.rate = SOUND_TRIGGER_SAMPLING_RATE_16000,
.period_size = SOUND_TRIGGER_PCM_PERIOD_SIZE,
.period_count = SOUND_TRIGGER_PCM_PERIOD_COUNT,
.format = PCM_FORMAT_S16_LE,
};
struct st_session_fptrs pcm_fptrs = {
.reg_sm = reg_sm,
.reg_sm_params = reg_sm_params,
.dereg_sm = dereg_sm,
.dereg_sm_params = dereg_sm_params,
.start = start,
.restart = restart,
.stop = stop,
.stop_buffering = stop_buffering,
.set_device = set_device,
.read_pcm = read_pcm,
.process_lab_capture = process_lab_capture,
.disable_device = disable_device,
.enable_device = enable_device,
.get_param_data = get_param_data,
.send_detection_request = send_detection_request,
};
struct st_hw_pcm_data {
void *ffv_lib_handle;
void *esp_lib_handle;
};
static struct st_hw_pcm_data pcm_data = {.ffv_lib_handle = NULL, .esp_lib_handle = NULL};
#if LINUX_ENABLED
#define restart_session(p_ses) (0)
#else
/*
* While running FFV with barge-in usecase, there is a timestamp
* discontinuity observed in one of the sessions (capture/EC capture)
* randomly. So encoder is dropping data which has timestamp discontinuity
* in one of the sessions. This leads to EC convergence issue where one of
* the sessions has old data and other has new data which is leading to
* zero detections.
* Restart both the sessions once, to flush out the data after setting up
* FFV session to fix the issue.
*/
static int restart_session(st_hw_session_t* p_ses)
{
int status = 0;
status = stop(p_ses);
if (status) {
ALOGE("%s: failed to stop err %d", __func__, status);
goto exit;
}
status = start(p_ses);
if (status)
ALOGE("%s: failed to start err %d", __func__, status);
exit:
return status;
}
#endif
#ifndef SOUND_TRIGGER_CPU_AFFINITY_SET
#define st_cpu_affinity_set(p_pcm_ses) (0)
#else
/* affine soundtrigger thread to CPU core one */
static void st_cpu_affinity_set(st_hw_session_pcm_t *p_pcm_ses)
{
cpu_set_t cpuset;
struct sched_param sched_param;
int policy = SCHED_FIFO, rc = 0;
ALOGV("%s: Set CPU affinity for SVA thread, pcm_ses %p", __func__, p_pcm_ses);
CPU_ZERO(&cpuset);
CPU_SET(1, &cpuset);
#ifdef LINUX_ENABLED
if (pthread_setaffinity_np(p_pcm_ses->capture_thread, sizeof(cpuset),
&cpuset) != 0)
#else
if (sched_setaffinity(0, sizeof(cpuset), &cpuset) != 0)
#endif
ALOGE("%s: CPU Affinity allocation failed for Capture thread",
__func__);
sched_param.sched_priority = sched_get_priority_min(policy);
rc = sched_setscheduler(0, policy, &sched_param);
if (rc != 0)
ALOGE("%s: Failed to set realtime priority", __func__);
}
#endif
static void get_lib_path(char * lib_path, int path_size)
{
#ifdef LINUX_ENABLED
#ifdef __LP64__
/* libs are stored in /usr/lib64 */
snprintf(lib_path, path_size, "%s", "/usr/lib64");
#else
/* libs are stored in /usr/lib */
snprintf(lib_path, path_size, "%s", "/usr/lib");
#endif
#else
#ifdef __LP64__
/* libs are stored in /vendor/lib64 */
snprintf(lib_path, path_size, "%s", "/vendor/lib64");
#else
/* libs are stored in /vendor/lib */
snprintf(lib_path, path_size, "%s", "/vendor/lib");
#endif
#endif
}
static size_t get_ffv_read_buffer_len(st_hw_session_pcm_t *p_ses)
{
st_get_param_payload_t *params_buffer_ptr;
int param_id, buffer_size, param_size;
size_t buf_size = 0;
FfvStatusType status_type;
buffer_size = sizeof(st_get_param_payload_t);
params_buffer_ptr = (st_get_param_payload_t *)calloc(1, buffer_size);
if (!params_buffer_ptr) {
ALOGE("%s: ERROR. Can not allocate buffer size %d", __func__, buffer_size);
goto exit;
}
param_id = FFV_HISTORY_BUFFER_LENGTH_PARAM;
status_type = ffv_get_param_fn(p_ses->handle, (char *)params_buffer_ptr,
param_id, buffer_size, ¶m_size);
if (status_type) {
ALOGE("%s: ERROR. ffv_get_param_fn ret %d", __func__, status_type);
goto exit;
}
if (param_size != sizeof(ffv_history_buffer_length_param_t)) {
ALOGE("%s: ERROR. Invalid param size returned %d", __func__, param_size);
goto exit;
}
buf_size = params_buffer_ptr->buffer_length_params.bufSize;
exit:
ALOGD("%s: read buffer len %zu", __func__, buf_size);
return buf_size;
}
static int check_and_set_ec_ref_device(st_hw_session_t *p_ses, bool enable)
{
char st_device_name[DEVICE_NAME_MAX_SIZE];
struct st_vendor_info* v_info = p_ses->vendor_uuid_info;
st_hw_session_pcm_t *p_pcm_ses = (st_hw_session_pcm_t *)p_ses;
FfvStatusType status_type;
ffv_quadrx_use_dwnmix_param_t quad_downmix;
if (v_info->split_ec_ref_data) {
ALOGV("%s: Ignore ec ref set device %p", __func__, p_ses);
return 0;
}
if (enable) {
if (v_info->ec_ref_channel_cnt == SOUND_TRIGGER_CHANNEL_MODE_MONO) {
strlcpy(st_device_name, ST_EC_REF_LOOPBACK_DEVICE_MONO,
DEVICE_NAME_MAX_SIZE);
} else if (v_info->ec_ref_channel_cnt == SOUND_TRIGGER_CHANNEL_MODE_STEREO) {
strlcpy(st_device_name, ST_EC_REF_LOOPBACK_DEVICE_STEREO,
DEVICE_NAME_MAX_SIZE);
} else if (v_info->ec_ref_channel_cnt == SOUND_TRIGGER_CHANNEL_MODE_QUAD) {
strlcpy(st_device_name, ST_EC_REF_LOOPBACK_DEVICE_QUAD,
DEVICE_NAME_MAX_SIZE);
quad_downmix.quadrx_dwnmix_enable = true;
ALOGD("set param for 4 ch ec");
status_type = ffv_set_param_fn(p_pcm_ses->handle,
(char *)&quad_downmix,
FFV_QUADRX_USE_DWNMIX_PARAM,
sizeof(ffv_quadrx_use_dwnmix_param_t));
if (status_type) {
ALOGE("%s: ERROR. ffv_set_param_fn for quad channel ec ref %d",
__func__, status_type);
return -EINVAL;
}
} else {
ALOGE("%s: Invalid ec ref channel count %d",
__func__, v_info->ec_ref_channel_cnt);
return -EINVAL;
}
if (platform_stdev_compare_device_type(p_ses->stdev->ec_ref_dev,
AUDIO_DEVICE_OUT_LINE))
strlcat(st_device_name, " lineout", DEVICE_NAME_MAX_SIZE);
ALOGD("%s: enable device = %s", __func__,
st_device_name);
audio_route_apply_and_update_path(p_ses->stdev->audio_route,
st_device_name);
p_pcm_ses->ec_ref_dev_name = strdup(st_device_name);
} else {
if (!p_pcm_ses->ec_ref_dev_name) {
ALOGE("%s: Invalid ec ref device name", __func__);
return -EINVAL;
}
ALOGD("%s: disable device = %s", __func__,
p_pcm_ses->ec_ref_dev_name);
audio_route_reset_and_update_path(p_ses->stdev->audio_route,
p_pcm_ses->ec_ref_dev_name);
free(p_pcm_ses->ec_ref_dev_name);
p_pcm_ses->ec_ref_dev_name = NULL;
}
return 0;
}
static int check_and_start_ec_ref_ses(st_hw_session_t *p_ses, bool start)
{
st_hw_session_pcm_t *p_pcm_ses = (st_hw_session_pcm_t *)p_ses;
int status = 0;
int retry_num = 0;
if (p_ses->vendor_uuid_info->split_ec_ref_data) {
ALOGV("%s:[%d] Ignore ec ref start", __func__, p_pcm_ses->common.sm_handle);
return status;
}
if (!p_pcm_ses->ec_ref_pcm) {
ALOGW("%s: ec ref pcm NULL", __func__);
return status;
}
if (start) {
status = pcm_start(p_pcm_ses->ec_ref_pcm);
while (status && (retry_num < SOUND_TRIGGER_PCM_MAX_RETRY)) {
usleep(SOUND_TRIGGER_PCM_SLEEP_WAIT);
retry_num++;
ALOGI("%s: pcm_start retrying..status %d errno %d, retry cnt %d",
__func__, status, errno, retry_num);
status = pcm_start(p_pcm_ses->ec_ref_pcm);
}
if (status) {
ALOGE("%s: ERROR. pcm_start failed, returned status %d, err=%s",
__func__, status, pcm_get_error(p_pcm_ses->ec_ref_pcm));
}
ALOGD("%s: ec ref session started", __func__);
} else {
pcm_stop(p_pcm_ses->ec_ref_pcm);
}
return status;
}
static int check_and_enable_ec_ref_use_case(bool enable, st_hw_session_t *p_ses)
{
st_hw_session_pcm_t *p_pcm_ses = (st_hw_session_pcm_t *)p_ses;
char use_case[USECASE_STRING_SIZE];
audio_devices_t capture_device;
int status = 0;
if (p_ses->vendor_uuid_info->split_ec_ref_data) {
ALOGV("%s:[%d] Ignore ec ref enable usecase", __func__, p_pcm_ses->common.sm_handle);
return status;
}
strlcpy(use_case,
p_ses->stdev->arm_pcm_use_cases[p_pcm_ses->ec_ref_use_case_idx].use_case,
USECASE_STRING_SIZE);
if (enable) {
ALOGD("%s: enable use case = %s", __func__, use_case);
audio_route_apply_and_update_path(p_ses->stdev->audio_route, use_case);
/* send device calibration for ec ref session */
capture_device = platform_stdev_get_capture_device(p_ses->stdev->platform);
platform_stdev_send_calibration(p_ses->stdev->platform,
capture_device,
p_ses->exec_mode,
p_ses->vendor_uuid_info,
SOUND_TRIGGER_DEVICE_DEFAULT_APP_TYPE,
false, ST_DEVICE_CAL);
status = pcm_prepare(p_pcm_ses->ec_ref_pcm);
if (status < 0) {
ALOGE("%s: pcm_prepare returned %s", __func__,
pcm_get_error(p_pcm_ses->ec_ref_pcm));
return status;
}
} else {
ALOGD("%s: disable use case = %s", __func__, use_case);
audio_route_reset_and_update_path(p_ses->stdev->audio_route, use_case);
}
return status;
}
static int enable_use_case(bool enable, st_hw_session_t *p_ses)
{
st_hw_session_pcm_t *p_pcm_ses = (st_hw_session_pcm_t *)p_ses;
char use_case[USECASE_STRING_SIZE];
int status = 0;
strlcpy(use_case,
p_ses->stdev->arm_pcm_use_cases[p_ses->use_case_idx].use_case,
USECASE_STRING_SIZE);
if (enable) {
ALOGD("%s: enable use case = %s", __func__, use_case);
audio_route_apply_and_update_path(p_ses->stdev->audio_route, use_case);
status = pcm_prepare(p_pcm_ses->pcm);
if (status < 0) {
ALOGE("%s: pcm_prepare returned %s", __func__,
pcm_get_error(p_pcm_ses->pcm));
return status;
}
} else {
ALOGD("%s: disable use case = %s", __func__, use_case);
audio_route_reset_and_update_path(p_ses->stdev->audio_route, use_case);
}
return status;
}
#ifdef QUEUE_PROCESS_BUFFERS
static void process_buf_queue_push(struct process_buf_queue **queue,
struct process_buf_queue *node)
{
struct process_buf_queue *iter;
node->next = NULL;
if ((*queue) == NULL) {
*queue = node;
} else {
iter = *queue;
while (iter->next) {
iter = iter->next;
}
iter->next = node;
}
}
static struct process_buf_queue *process_buf_queue_pop(struct process_buf_queue **queue)
{
struct process_buf_queue *node = (*queue);
if (node != NULL) {
*queue = node->next;
node->next = NULL;
}
return node;
}
static int init_process_buffers(st_hw_session_pcm_t *p_pcm_ses)
{
unsigned int data_size, in_buf_size, i;
bool split_ec_ref_data = p_pcm_ses->common.vendor_uuid_info->split_ec_ref_data;
in_buf_size = split_ec_ref_data ? p_pcm_ses->split_in_buf_size : p_pcm_ses->in_buf_size;
data_size = in_buf_size + p_pcm_ses->ec_ref_buf_size + p_pcm_ses->out_buf_size;
p_pcm_ses->process_buf_data = (void *)calloc(data_size, NUM_PROCESS_BUFS);
if (p_pcm_ses->process_buf_data == NULL) {
ALOGE("%s: failed to allocate process buf data %d", __func__, data_size);
return -ENOMEM;
}
p_pcm_ses->process_buf = NULL;
p_pcm_ses->process_buf_free = NULL;
for (i = 0; i < NUM_PROCESS_BUFS; i++) {
struct process_buf_queue *buf = &p_pcm_ses->process_buf_nodes[i];
buf->buffer.data = &(((char *)p_pcm_ses->process_buf_data)[i * data_size]);
buf->buffer.length = data_size;
buf->buffer.in_buf_ptr = (int16_t *)buf->buffer.data;
buf->buffer.ec_ref_buf_ptr = (int16_t *) ((char *)buf->buffer.data + in_buf_size);
buf->buffer.out_buf_ptr = (int16_t *)((char *)buf->buffer.data + p_pcm_ses->ec_ref_buf_size);
process_buf_queue_push(&p_pcm_ses->process_buf_free, buf);
}
return 0;
}
static int deinit_process_buffers(st_hw_session_pcm_t *p_pcm_ses)
{
if(p_pcm_ses->process_buf_data != NULL)
free(p_pcm_ses->process_buf_data);
p_pcm_ses->process_buf_data = NULL;
p_pcm_ses->process_buf = NULL;
p_pcm_ses->process_buf_free = NULL;
}
static void *st_ffv_process_thread_loop(void *context)
{
st_hw_session_pcm_t *p_pcm_ses =
(st_hw_session_pcm_t *)context;
bool split_ec_ref_data = p_pcm_ses->common.vendor_uuid_info->split_ec_ref_data;
unsigned int in_buf_size =
split_ec_ref_data ? p_pcm_ses->split_in_buf_size : p_pcm_ses->in_buf_size;
struct process_buf_queue *p_buf;
if (p_pcm_ses == NULL) {
ALOGE("%s: ERROR: invalid context", __func__);
return NULL;
}
if (p_pcm_ses->common.stdev->enable_debug_dumps) {
ST_DBG_DECLARE(FILE *fptr_cap = NULL; static int file_cnt = 0);
ST_DBG_FILE_OPEN_WR(fptr_cap, ST_DEBUG_DUMP_LOCATION,
"ffv_capture_data", "pcm", file_cnt++);
ST_DBG_DECLARE(FILE *fptr_ec = NULL; static int file_cnt_2 = 0);
ST_DBG_FILE_OPEN_WR(fptr_ec, ST_DEBUG_DUMP_LOCATION,
"ffv_ec_ref_data", "pcm", file_cnt_2++);
ST_DBG_DECLARE(FILE *fptr_out = NULL; static int file_cnt_3 = 0);
ST_DBG_FILE_OPEN_WR(fptr_out, ST_DEBUG_DUMP_LOCATION,
"ffv_out_data", "pcm", file_cnt_3++);
}
setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
prctl(PR_SET_NAME, (unsigned long)"sound trigger process", 0, 0, 0);
ALOGD("%s:[%d] Enter", __func__, p_pcm_ses->common.sm_handle);
pthread_mutex_lock(&p_pcm_ses->st_ffv_process_lock);
init_process_buffers(p_pcm_ses);
while (!p_pcm_ses->exit_st_ffv_process_thread) {
struct process_buf_queue *p_buf;
int16_t *process_in_ptr, *process_out_ptr, *process_ec_ref_ptr;
if (p_pcm_ses->process_buf == NULL) {
ALOGV("%s: waiting for buffer", __func__);
pthread_cond_wait(&p_pcm_ses->st_ffv_process_cond,
&p_pcm_ses->st_ffv_process_lock);
}
if (p_pcm_ses->exit_st_ffv_process_thread)
break;
p_buf = process_buf_queue_pop(&p_pcm_ses->process_buf);
pthread_mutex_unlock(&p_pcm_ses->st_ffv_process_lock);
process_in_ptr = p_buf->buffer.in_buf_ptr;
process_ec_ref_ptr = p_buf->buffer.ec_ref_buf_ptr;
process_out_ptr = p_buf->buffer.out_buf_ptr;
if (p_pcm_ses->common.stdev->enable_debug_dumps) {
ST_DBG_FILE_WRITE(fptr_cap, process_in_ptr, in_buf_size);
ST_DBG_FILE_WRITE(fptr_ec, process_ec_ref_ptr,
p_pcm_ses->ec_ref_buf_size);
}
ffv_process_fn(p_pcm_ses->handle, process_in_ptr,
process_out_ptr, process_ec_ref_ptr);
if (p_pcm_ses->common.stdev->enable_debug_dumps) {
ST_DBG_FILE_WRITE(fptr_out, process_out_ptr,
p_pcm_ses->out_buf_size);
}
pthread_mutex_lock(&p_pcm_ses->st_ffv_process_lock);
process_buf_queue_push(&p_pcm_ses->process_buf_free, p_buf);
pthread_cond_signal(&p_pcm_ses->st_ffv_capture_cond);
}
exit:
p_pcm_ses->st_ffv_process_thread_started = false;
deinit_process_buffers(p_pcm_ses);
pthread_mutex_unlock(&p_pcm_ses->st_ffv_process_lock);
if (p_pcm_ses->common.stdev->enable_debug_dumps) {
ST_DBG_FILE_CLOSE(fptr_cap);
ST_DBG_FILE_CLOSE(fptr_ec);
ST_DBG_FILE_CLOSE(fptr_out);
}
ALOGD("%s: Exit", __func__);
return NULL;
}
static int push_process_buffers(st_hw_session_pcm_t *p_pcm_ses, int16_t *in_pcm,
int16_t *out_pcm, int16_t *ec_ref_pcm, unsigned int in_buf_size)
{
struct process_buf_queue *p_buf;
pthread_mutex_lock(&p_pcm_ses->st_ffv_process_lock);
if (!p_pcm_ses->st_ffv_process_thread_started) {
ALOGE("%s: ERROR. process thread not started", __func__);
return -EINVAL;
}
if (p_pcm_ses->process_buf_free == NULL) {
ALOGD("%s: waiting for process buffer free", __func__);
pthread_cond_wait(&p_pcm_ses->st_ffv_capture_cond,
&p_pcm_ses->st_ffv_process_lock);
if (p_pcm_ses->process_buf_free == NULL) {
pthread_mutex_unlock(&p_pcm_ses->st_ffv_process_lock);
ALOGE("%s: failed to acquire buffers", __func__);
return -EINVAL;
}
}
p_buf = process_buf_queue_pop(&p_pcm_ses->process_buf_free);
memcpy(p_buf->buffer.in_buf_ptr, in_pcm, in_buf_size);
memcpy(p_buf->buffer.ec_ref_buf_ptr, ec_ref_pcm, p_pcm_ses->ec_ref_buf_size);
memcpy(p_buf->buffer.out_buf_ptr, out_pcm, p_pcm_ses->out_buf_size);
process_buf_queue_push(&p_pcm_ses->process_buf, p_buf);
pthread_cond_signal(&p_pcm_ses->st_ffv_process_cond);
pthread_mutex_unlock(&p_pcm_ses->st_ffv_process_lock);
return 0;
}
#else
static void *st_ffv_process_thread_loop(void *context __unused)
{
return NULL;
}
#endif
static void *capture_thread_loop(void *context)
{
st_hw_session_pcm_t *p_pcm_ses =
(st_hw_session_pcm_t *)context;
int status = 0;
int16_t *in_ptr = NULL, *process_in_ptr = NULL, *process_out_ptr = NULL;
int16_t *process_bsp_out_ptr = NULL;
int16_t *process_ec_ref_ptr = NULL;
int i, ch;
int total_in_ch, in_ch, ec_ref_ch;
unsigned int in_buf_size;
static bool write_1 = true;
if (p_pcm_ses->common.stdev->enable_debug_dumps) {
ST_DBG_DECLARE(FILE *fptr_cap = NULL; static int file_cnt = 0);
ST_DBG_DECLARE(FILE *fptr_ec = NULL; static int file_cnt_2 = 0);
ST_DBG_DECLARE(FILE *fptr_split = NULL; static int file_cnt_3 = 0);
ST_DBG_DECLARE(FILE *fptr_out = NULL; static int file_cnt_4 = 0);
ST_DBG_DECLARE(FILE *fptr_bsp_out = NULL; static int file_cnt_5 = 0);
}
if (p_pcm_ses == NULL) {
ALOGE("%s: ERROR: invalid context", __func__);
goto exit;
}
if (p_pcm_ses->common.stdev->enable_debug_dumps) {
ST_DBG_FILE_OPEN_WR(fptr_cap, ST_DEBUG_DUMP_LOCATION,
"st_capture_data", "pcm", file_cnt++);
ST_DBG_FILE_OPEN_WR(fptr_ec, ST_DEBUG_DUMP_LOCATION,
"st_ec_ref_data", "pcm", file_cnt_2++);
ST_DBG_FILE_OPEN_WR(fptr_split, ST_DEBUG_DUMP_LOCATION,
"st_split_capture_data", "pcm", file_cnt_3++);
ST_DBG_FILE_OPEN_WR(fptr_out, ST_DEBUG_DUMP_LOCATION,
"st_out_data", "pcm", file_cnt_4++);
ST_DBG_FILE_OPEN_WR(fptr_bsp_out, ST_DEBUG_DUMP_LOCATION,
"st_bsp_out_data", "pcm", file_cnt_5++);
}
st_cpu_affinity_set(p_pcm_ses);
setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_AUDIO);
prctl(PR_SET_NAME, (unsigned long)"sound trigger capture", 0, 0, 0);
ALOGD("%s:[%d] Enter", __func__, p_pcm_ses->common.sm_handle);
ALOGV("%s:[%d] split ec ref %s", __func__, p_pcm_ses->common.sm_handle,
p_pcm_ses->common.vendor_uuid_info->split_ec_ref_data ? "set" : "not set");
pthread_mutex_lock(&p_pcm_ses->capture_thread_lock);
while (!p_pcm_ses->exit_capture_thread) {
pthread_mutex_unlock(&p_pcm_ses->capture_thread_lock);
ALOGVV("%s: pcm_read reading bytes=%d", __func__, p_pcm_ses->in_buf_size);
status = pcm_read(p_pcm_ses->pcm, p_pcm_ses->in_buf, p_pcm_ses->in_buf_size);
ALOGVV("%s: pcm_read done", __func__);
if (p_pcm_ses->common.stdev->enable_debug_dumps) {
ST_DBG_FILE_WRITE(fptr_cap, p_pcm_ses->in_buf,
p_pcm_ses->in_buf_size);
}
if (status) {
ALOGE("%s: pcm read failed status %d - %s", __func__, status,
pcm_get_error(p_pcm_ses->pcm));
p_pcm_ses->exit_capture_thread = true;
goto exit;
}
if (!p_pcm_ses->common.vendor_uuid_info->split_ec_ref_data) {
/* read EC ref data */
ALOGVV("%s: pcm_read reading bytes=%d", __func__, p_pcm_ses->ec_ref_buf_size);
status = pcm_read(p_pcm_ses->ec_ref_pcm, p_pcm_ses->ec_ref_buf, p_pcm_ses->ec_ref_buf_size);
ALOGVV("%s: pcm_read done", __func__);
if (p_pcm_ses->common.stdev->enable_debug_dumps) {
ST_DBG_FILE_WRITE(fptr_ec, p_pcm_ses->ec_ref_buf,
p_pcm_ses->ec_ref_buf_size);
}
if (status) {
ALOGE("%s: ec ref pcm read failed status %d - %s", __func__, status,
pcm_get_error(p_pcm_ses->ec_ref_pcm));
p_pcm_ses->exit_capture_thread = true;
goto exit;
}
process_in_ptr = (int16_t *)p_pcm_ses->in_buf;
process_ec_ref_ptr = (int16_t *)p_pcm_ses->ec_ref_buf;
in_buf_size = p_pcm_ses->in_buf_size;
} else {
/* split input buffer into actual input channels and EC ref channels */
in_ptr = (int16_t *)p_pcm_ses->in_buf;
process_in_ptr = (int16_t *)p_pcm_ses->split_in_buf;
process_ec_ref_ptr = (int16_t *)p_pcm_ses->ec_ref_buf;
total_in_ch = p_pcm_ses->capture_config.channels;
ec_ref_ch = p_pcm_ses->ec_ref_config.channels;
in_ch = total_in_ch - ec_ref_ch;
for (i = 0; i < (int) p_pcm_ses->capture_config.period_size; i++) {
for (ch = 0; ch < in_ch; ch++) {
process_in_ptr[i*in_ch+ch] =
in_ptr[i*total_in_ch+ch];
}
for (ch = 0; ch < ec_ref_ch; ch++) {
process_ec_ref_ptr[i*ec_ref_ch+ch] =
in_ptr[i*total_in_ch+in_ch+ch];
}
}
if (p_pcm_ses->common.stdev->enable_debug_dumps) {
ST_DBG_FILE_WRITE(fptr_split, p_pcm_ses->split_in_buf,
p_pcm_ses->split_in_buf_size);
ST_DBG_FILE_WRITE(fptr_ec, p_pcm_ses->ec_ref_buf,
p_pcm_ses->ec_ref_buf_size);
}
in_buf_size = p_pcm_ses->split_in_buf_size;
}
process_out_ptr = (int16_t *)p_pcm_ses->out_buf;
process_bsp_out_ptr = (int16_t *)p_pcm_ses->bsp_out_buf;
#ifdef QUEUE_PROCESS_BUFFERS
status = push_process_buffers(p_pcm_ses, process_in_ptr,
process_out_ptr, process_ec_ref_ptr, in_buf_size);
if (status) {
ALOGE("%s: failed to push process buffers %d", __func__, status);
p_pcm_ses->exit_capture_thread = true;
goto exit;
}
#else
if (ffv_process_v2_fn) {
if (write_1)
ALOGD("%s: calling ffv_process_v2_fn", __func__);
ffv_process_v2_fn(p_pcm_ses->handle, process_in_ptr,
process_out_ptr, process_bsp_out_ptr, process_ec_ref_ptr);
if (p_pcm_ses->esp_handle)
esp_process_fn(p_pcm_ses->esp_handle, process_bsp_out_ptr);
} else {
if (write_1)
ALOGD("%s: calling ffv_process_fn", __func__);
ffv_process_fn(p_pcm_ses->handle, process_in_ptr,
process_out_ptr, process_ec_ref_ptr);
}
if (write_1) {
ALOGD("%s: return from ffv_process(_v2)_fn", __func__);
write_1 = false;
}
if (p_pcm_ses->common.stdev->enable_debug_dumps) {
ST_DBG_FILE_WRITE(fptr_out, process_out_ptr,
p_pcm_ses->out_buf_size);
ST_DBG_FILE_WRITE(fptr_bsp_out, process_bsp_out_ptr,
p_pcm_ses->bsp_out_buf_size);
}
#endif
pthread_mutex_lock(&p_pcm_ses->capture_thread_lock);
}
pthread_mutex_unlock(&p_pcm_ses->capture_thread_lock);
exit:
ALOGD("%s: Exit status=%d", __func__, status);
p_pcm_ses->capture_thread_started = false;
write_1 = true;
if (p_pcm_ses->common.stdev->enable_debug_dumps) {
ST_DBG_FILE_CLOSE(fptr_cap);
ST_DBG_FILE_CLOSE(fptr_ec);
ST_DBG_FILE_CLOSE(fptr_split);
ST_DBG_FILE_CLOSE(fptr_out);
ST_DBG_FILE_CLOSE(fptr_bsp_out);
}
return NULL;
}
static void st_event_callback(void *cb_data, void *handle, FfvEventType event_type,
void *event_payload, size_t event_payload_size __unused)
{
st_hw_session_pcm_t *p_pcm_ses = (st_hw_session_pcm_t *)cb_data;
callback_event_t *ev = NULL;
ALOGD("%s: handle %p", __func__, handle);
if (!handle || !event_payload) {
ALOGE("%s: ERROR. NULL params", __func__);
return;
}
if (!p_pcm_ses) {
ALOGE("%s: ERROR. NULL session", __func__);
return;
}
ALOGD("%s: sm handle [%d]", __func__, p_pcm_ses->common.sm_handle);
ev= (callback_event_t *)calloc(1, sizeof(callback_event_t));
if (!ev) {
ALOGE("%s: failed to allocate mem for event", __func__);
goto exit;
}
ev->event_type = event_type;
ev->ev_payload = event_payload;
exit:
pthread_mutex_lock(&p_pcm_ses->callback_thread_lock);
if (!ev)
p_pcm_ses->exit_callback_thread = true;
else
list_add_tail(&p_pcm_ses->ev_list, &ev->node);
pthread_cond_signal(&p_pcm_ses->ev_cond);
pthread_mutex_unlock(&p_pcm_ses->callback_thread_lock);
}
/*
* Print detection event payload for non LE case only as
* client/test app is parsing/printing the payload in LE case.
*/
#if LINUX_ENABLED
#define print_detection_event(hw_sess_event) (0)
#else
static void print_detection_event(st_hw_sess_event_t sess_info)
{
uint32_t *payload_32 = NULL;
uint32_t version, key, key_version, key_size;
int k;
payload_32 = (uint32_t *)sess_info.payload.detected.detect_payload;
version = *payload_32++;
ALOGVV("%s: version %d", __func__, version);
if (version == 0x1) {
for (k = 0; k < KWD_MAX; k++) {
key = *payload_32++;
key_version = *payload_32++;
key_size = *payload_32++;
if (key == KWD_INDEX) {
/* parse payload for key == KWD_INDEX */
if (key_version != 0x1) {
ALOGE("%s: Invalid version for keyword index key %d",
__func__, key_version);
goto exit;
}
ALOGD("%s: keyword start index %d", __func__, *payload_32++);
ALOGD("%s: keyword stop index %d", __func__, *payload_32++);
} else {
payload_32 = (uint32_t *)((char *)payload_32 + key_size);
}
}
} else {
ALOGE("%s: Invalid version for detection event payload %d",
__func__, version);
goto exit;
}
exit:
return;
}
#endif
static void *callback_thread_loop(void *context)
{
st_hw_session_pcm_t *p_pcm_ses =
(st_hw_session_pcm_t *)context;
int event_status;
st_hw_sess_event_t hw_sess_event; /* used to report event to st_session */
void *payload;
int payload_size;
struct listnode *item;
callback_event_t *ev = NULL;
uint64_t timestamp;
ffv_keyword_detection_status_t *ev_payload;
bool send_event;
if (p_pcm_ses == NULL) {
ALOGE("%s: ERROR. null context.. exiting", __func__);
return NULL;
}
ALOGD("%s:[%d] Enter", __func__, p_pcm_ses->common.sm_handle);
setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_DEFAULT);
prctl(PR_SET_NAME, (unsigned long)"sound trigger callback", 0, 0, 0);
pthread_mutex_lock(&p_pcm_ses->callback_thread_lock);
while (!p_pcm_ses->exit_callback_thread) {
if (list_empty(&p_pcm_ses->ev_list)) {
ALOGI("%s:[%d] Waiting for event",
__func__, p_pcm_ses->common.sm_handle);
pthread_cond_wait(&p_pcm_ses->ev_cond, &p_pcm_ses->callback_thread_lock);
}
if (p_pcm_ses->exit_callback_thread)
break;
send_event = false;
item = list_head(&p_pcm_ses->ev_list);
ev = node_to_item(item, callback_event_t, node);
list_remove(item);
ALOGD("%s: processing event type %d", __func__, ev->event_type);
switch (ev->event_type) {
case EVENT_KEYWORD_DETECTION:
ev_payload = (ffv_keyword_detection_status_t *)ev->ev_payload;
timestamp = ((uint64_t)ev_payload->timestamp_msw << 32) |
ev_payload->timestamp_lsw;
payload_size = ev_payload->payload_size;
payload = (char *)ev_payload + sizeof(ffv_keyword_detection_status_t);
ALOGD("%s: detection status %d", __func__, ev_payload->detection_status);
switch(ev_payload->detection_status) {
case KWD_STATUS_DETECTED:
case KWD_STATUS_IN_DISCOVERY:
case KWD_STATUS_IN_PROCESS:
event_status = RECOGNITION_STATUS_SUCCESS;
send_event = true;
break;
case KWD_STATUS_END_SPEECH:
case KWD_STATUS_REJECTED:
event_status = RECOGNITION_STATUS_FAILURE;
send_event = true;
break;
case KWD_STATUS_NONE:
default:
break;
}
break;
default:
break;
}
free(ev);
if (!send_event)
continue;
ALOGI("%s:[%d] Recognition event %d",
__func__, p_pcm_ses->common.sm_handle, event_status);
/* inform st_session of the event */
hw_sess_event.event_id = ST_HW_SESS_EVENT_DETECTED;
hw_sess_event.payload.detected.timestamp = timestamp;
hw_sess_event.payload.detected.detect_status = event_status;
hw_sess_event.payload.detected.detect_payload = payload;
hw_sess_event.payload.detected.payload_size = payload_size;
print_detection_event(hw_sess_event);
p_pcm_ses->common.callback_to_st_session(&hw_sess_event,
p_pcm_ses->common.cookie);
}
while (!list_empty(&p_pcm_ses->ev_list)) {
item = list_head(&p_pcm_ses->ev_list);
list_remove(item);
free(node_to_item(item, callback_event_t, node));
}
pthread_mutex_unlock(&p_pcm_ses->callback_thread_lock);
ALOGD("%s:[%d] Exit", __func__, p_pcm_ses->common.sm_handle);
return NULL;
}
static int deallocate_lab_buffers(st_hw_session_pcm_t* p_ses)
{
ALOGVV("%s:[%d] Enter", __func__, p_ses->common.sm_handle);
if (p_ses->lab_out_buf) {
free(p_ses->lab_out_buf);
p_ses->lab_out_buf = NULL;
}
if (p_ses->lab_cap_buf) {
free(p_ses->lab_cap_buf);
p_ses->lab_cap_buf = NULL;
}
p_ses->lab_buffers_allocated = false;
return 0;
}
static int allocate_lab_buffers(st_hw_session_pcm_t* p_ses)
{
int status = 0;
ALOGVV("%s:[%d] Enter", __func__, p_ses->common.sm_handle);
p_ses->lab_cap_buf_size = p_ses->lab_config.period_size * p_ses->lab_config.channels *
(pcm_format_to_bits(p_ses->lab_config.format) >> 3);
p_ses->lab_cap_buf = (unsigned char *) calloc(1, p_ses->lab_cap_buf_size);
if (!p_ses->lab_cap_buf) {
ALOGE("%s: ERROR. Can not allcoate lab buffer size %d", __func__, p_ses->lab_cap_buf_size);