-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmod_unimrcp.c
4651 lines (4207 loc) · 179 KB
/
mod_unimrcp.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
/*
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2009-2015, Anthony Minessale II <[email protected]>
*
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (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.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is FreeSWITCH mod_unimrcp
*
* The Initial Developer of the Original Code is
* Christopher M. Rienzo <[email protected]>
*
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Brian West <[email protected]>
* Christopher M. Rienzo <[email protected]>
* Luke Dashjr <[email protected]> (OpenMethods, LLC)
*
* Maintainer: Christopher M. Rienzo <[email protected]>
*
* mod_unimrcp.c -- UniMRCP module (MRCP client)
*
*/
#include <switch.h>
/* UniMRCP includes */
#include "apt.h"
#include "apt_log.h"
#include "unimrcp_client.h"
#include "mrcp_application.h"
#include "mrcp_session.h"
#include "mrcp_message.h"
#include "mrcp_generic_header.h"
#include "mrcp_synth_header.h"
#include "mrcp_synth_resource.h"
#include "mrcp_recog_header.h"
#include "mrcp_recog_resource.h"
#include "uni_version.h"
#include "mrcp_resource_loader.h"
#include "mpf_engine.h"
#include "mpf_codec_manager.h"
#include "mpf_dtmf_generator.h"
#include "mpf_rtp_termination_factory.h"
#include "mrcp_sofiasip_client_agent.h"
#include "mrcp_unirtsp_client_agent.h"
#include "mrcp_client_connection.h"
#include "apt_net.h"
/*********************************************************************************************************************************************
* mod_unimrcp : module interface to FreeSWITCH
*/
/* module name */
#define MOD_UNIMRCP "unimrcp"
/* module config file */
#define CONFIG_FILE "unimrcp.conf"
/**
* A UniMRCP application.
*/
struct mod_unimrcp_application {
/** UniMRCP application */
mrcp_application_t *app;
/** MRCP callbacks from UniMRCP to this module's application */
mrcp_app_message_dispatcher_t dispatcher;
/** Audio callbacks from UniMRCP to this module's application */
mpf_audio_stream_vtable_t audio_stream_vtable;
/** maps FreeSWITCH param to MRCP param name */
switch_hash_t *fs_param_map;
/** maps MRCP header to unimrcp header handler function */
switch_hash_t *param_id_map;
};
typedef struct mod_unimrcp_application mod_unimrcp_application_t;
/**
* module globals - global configuration and variables
*/
struct mod_unimrcp_globals {
/** max-connection-count config */
char *unimrcp_max_connection_count;
/** request-timeout config */
char *unimrcp_request_timeout;
/** rx-buffer-size */
char *unimrcp_rx_buffer_size;
/** tx-buffer-size */
char *unimrcp_tx_buffer_size;
/** offer-new-connection config */
char *unimrcp_offer_new_connection;
/** default-tts-profile config */
char *unimrcp_default_synth_profile;
/** default-asr-profile config */
char *unimrcp_default_recog_profile;
/** log level for UniMRCP library */
char *unimrcp_log_level;
/** profile events configuration param */
char *enable_profile_events_param;
/** True if profile events are wanted */
int enable_profile_events;
/** the MRCP client stack */
mrcp_client_t *mrcp_client;
/** synthesizer application */
mod_unimrcp_application_t synth;
/** recognizer application */
mod_unimrcp_application_t recog;
/** synchronize access for speech channel numbering */
switch_mutex_t *mutex;
/** next available speech channel number */
int speech_channel_number;
/** the available profiles */
switch_hash_t *profiles;
/** UniMRCP and APT compatible memory pool */
apr_pool_t *apr_pool;
};
typedef struct mod_unimrcp_globals mod_unimrcp_globals_t;
/** Module global variables */
static mod_unimrcp_globals_t globals;
/**
* Profile-specific configuration. This allows us to handle differing MRCP server behavior
* on a per-profile basis
*/
struct profile {
/** name of the profile */
char *name;
/** MIME type to use for JSGF grammars */
const char *jsgf_mime_type;
/** MIME type to use for GSL grammars */
const char *gsl_mime_type;
/** MIME type to use for SRGS XML grammars */
const char *srgs_xml_mime_type;
/** MIME type to use for SRGS ABNF grammars */
const char *srgs_mime_type;
/** MIME type to use for Google Speech module context */
const char *xml_mime_type;
/** MIME type to use for SSML (TTS) */
const char *ssml_mime_type;
/** Default params to use for RECOGNIZE requests */
switch_hash_t *default_recog_params;
/** Default params to use for SPEAK requests */
switch_hash_t *default_synth_params;
};
typedef struct profile profile_t;
static switch_status_t profile_create(profile_t ** profile, const char *name, switch_memory_pool_t *pool);
/* Profile events that may be monitored. Useful for tracking MRCP profile utilization */
#define MY_EVENT_PROFILE_CREATE "unimrcp::profile_create"
#define MY_EVENT_PROFILE_OPEN "unimrcp::profile_open"
#define MY_EVENT_PROFILE_CLOSE "unimrcp::profile_close"
/**
* Defines XML parsing instructions
*/
static switch_xml_config_item_t instructions[] = {
SWITCH_CONFIG_ITEM_STRING_STRDUP("max-connection-count", CONFIG_REQUIRED, &globals.unimrcp_max_connection_count, "100", "",
"The max MRCPv2 connections to manage"),
SWITCH_CONFIG_ITEM_STRING_STRDUP("offer-new-connection", CONFIG_REQUIRED, &globals.unimrcp_offer_new_connection, "1", "", ""),
SWITCH_CONFIG_ITEM_STRING_STRDUP("default-tts-profile", CONFIG_REQUIRED, &globals.unimrcp_default_synth_profile, "default", "",
"The default profile to use for TTS"),
SWITCH_CONFIG_ITEM_STRING_STRDUP("default-asr-profile", CONFIG_REQUIRED, &globals.unimrcp_default_recog_profile, "default", "",
"The default profile to use for ASR"),
SWITCH_CONFIG_ITEM_STRING_STRDUP("log-level", CONFIG_REQUIRED, &globals.unimrcp_log_level, "WARNING",
"EMERGENCY|ALERT|CRITICAL|ERROR|WARNING|NOTICE|INFO|DEBUG", "Logging level for UniMRCP"),
SWITCH_CONFIG_ITEM_STRING_STRDUP("enable-profile-events", CONFIG_REQUIRED, &globals.enable_profile_events_param, "false", "",
"Fire profile events (true|false)"),
SWITCH_CONFIG_ITEM_STRING_STRDUP("request-timeout", CONFIG_REQUIRED, &globals.unimrcp_request_timeout, "10000", "",
"Maximum time to wait for server response to a request"),
SWITCH_CONFIG_ITEM_STRING_STRDUP("connection-rx-buffer-size", 0, &globals.unimrcp_rx_buffer_size, "1024", "",
"Maximum time to wait for server response to a request"),
SWITCH_CONFIG_ITEM_STRING_STRDUP("connection-tx-buffer-size", 0, &globals.unimrcp_tx_buffer_size, "1024", "",
"Maximum time to wait for server response to a request"),
SWITCH_CONFIG_ITEM_END()
};
/* mod_unimrcp interface to FreeSWITCH */
SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_unimrcp_shutdown);
SWITCH_MODULE_RUNTIME_FUNCTION(mod_unimrcp_runtime);
SWITCH_MODULE_LOAD_FUNCTION(mod_unimrcp_load);
SWITCH_MODULE_DEFINITION(mod_unimrcp, mod_unimrcp_load, mod_unimrcp_shutdown, NULL);
static switch_status_t mod_unimrcp_do_config();
static mrcp_client_t *mod_unimrcp_client_create(switch_memory_pool_t *mod_pool);
static int process_rtp_config(mrcp_client_t *client, mpf_rtp_config_t *rtp_config, mpf_rtp_settings_t *rtp_settings, const char *param, const char *val, apr_pool_t *pool);
static int process_mrcpv1_config(rtsp_client_config_t *config, mrcp_sig_settings_t *sig_settings, const char *param, const char *val, apr_pool_t *pool);
static int process_mrcpv2_config(mrcp_sofia_client_config_t *config, mrcp_sig_settings_t *sig_settings, const char *param, const char *val, apr_pool_t *pool);
static int process_profile_config(profile_t *profile, const char *param, const char *val, switch_memory_pool_t *pool);
/* UniMRCP <--> FreeSWITCH logging bridge */
static apt_bool_t unimrcp_log(const char *file, int line, const char *obj, apt_log_priority_e priority, const char *format, va_list arg_ptr);
static apt_log_priority_e str_to_log_level(const char *level);
static int get_next_speech_channel_number(void);
#define XML_ID "<?xml"
#define SRGS_ID "<grammar"
#define SSML_ID "<speak"
#define GSL_ID ";GSL2.0"
#define ABNF_ID "#ABNF"
#define JSGF_ID "#JSGF"
#define GSR_ID "<speech-context"
#define BUILTIN_ID "builtin:"
#define SESSION_ID "session:"
#define HTTP_ID "http://"
#define HTTPS_ID "https://"
#define FILE_ID "file://"
#define INLINE_ID "inline:"
static int text_starts_with(const char *text, const char *match);
static const char *skip_initial_whitespace(const char *text);
/**
* UniMRCP parameter ID container
*/
struct unimrcp_param_id {
/** The parameter ID */
int id;
};
typedef struct unimrcp_param_id unimrcp_param_id_t;
static unimrcp_param_id_t *unimrcp_param_id_create(int id, switch_memory_pool_t *pool);
/********************************************************************************************************************************************
* AUDIO QUEUE : UniMRCP <--> FreeSWITCH audio buffering
*/
/* size of the buffer */
#define AUDIO_QUEUE_SIZE (1024 * 32)
/* Define to enable read/write logging and dumping of queue data to file */
#undef MOD_UNIMRCP_DEBUG_AUDIO_QUEUE
/**
* Audio queue internals
*/
struct audio_queue {
#ifdef MOD_UNIMRCP_DEBUG_AUDIO_QUEUE
/** debug file for tx operations */
switch_file_t *file_write;
/** debug file name */
char file_write_name[30];
/** debug file for rx operations */
switch_file_t *file_read;
/** debug file name */
char file_read_name[30];
#endif
/** the buffer of audio data */
switch_buffer_t *buffer;
/** synchronizes access to queue */
switch_mutex_t *mutex;
/** signaling for blocked readers/writers */
switch_thread_cond_t *cond;
/** total bytes written */
switch_size_t write_bytes;
/** total bytes read */
switch_size_t read_bytes;
/** number of bytes reader is waiting for */
switch_size_t waiting;
/** name of this queue (for logging) */
char *name;
/** optional session uuid associated with this queue (for logging) */
char *session_uuid;
};
typedef struct audio_queue audio_queue_t;
static switch_status_t audio_queue_create(audio_queue_t ** queue, const char *name, const char *session_uuid, switch_memory_pool_t *pool);
static switch_status_t audio_queue_write(audio_queue_t *queue, void *data, switch_size_t *data_len);
static switch_status_t audio_queue_read(audio_queue_t *queue, void *data, switch_size_t *data_len, int block);
static switch_status_t audio_queue_clear(audio_queue_t *queue);
static switch_status_t audio_queue_signal(audio_queue_t *queue);
static switch_status_t audio_queue_destroy(audio_queue_t *queue);
/*********************************************************************************************************************************************
* SPEECH_CHANNEL : speech functions common to recognizer and synthesizer
*/
#define SPEECH_CHANNEL_TIMEOUT_USEC (5000 * 1000)
#define AUDIO_TIMEOUT_USEC (SWITCH_MAX_INTERVAL * 1000)
/**
* Type of MRCP channel
*/
enum speech_channel_type {
SPEECH_CHANNEL_SYNTHESIZER,
SPEECH_CHANNEL_RECOGNIZER
};
typedef enum speech_channel_type speech_channel_type_t;
/**
* channel states
*/
enum speech_channel_state {
/** closed */
SPEECH_CHANNEL_CLOSED,
/** ready for speech request */
SPEECH_CHANNEL_READY,
/** processing speech request */
SPEECH_CHANNEL_PROCESSING,
/** finished processing speech request */
SPEECH_CHANNEL_DONE,
/** error opening channel */
SPEECH_CHANNEL_ERROR
};
typedef enum speech_channel_state speech_channel_state_t;
/**
* An MRCP speech channel
*/
struct speech_channel {
/** the name of this channel (for logging) */
char *name;
/** optional session associated w/ this channel */
char *session_uuid;
/** The profile used by this channel */
profile_t *profile;
/** type of channel */
speech_channel_type_t type;
/** application this channel is running */
mod_unimrcp_application_t *application;
/** UniMRCP session */
mrcp_session_t *unimrcp_session;
/** UniMRCP channel */
mrcp_channel_t *unimrcp_channel;
/** memory pool */
switch_memory_pool_t *memory_pool;
/** memory pool compatible with apt and unimrcp */
apr_pool_t *apr_pool;
/** synchronizes channel state */
switch_mutex_t *mutex;
/** wait on channel states */
switch_thread_cond_t *cond;
/** channel state */
speech_channel_state_t state;
/** UniMRCP <--> FreeSWITCH audio buffer */
audio_queue_t *audio_queue;
/** True, if channel was opened successfully */
int channel_opened;
/** rate */
uint16_t rate;
/** silence sample */
int silence;
/** speech channel params */
switch_hash_t *params;
/** app specific data */
void *data;
void *fsh;
};
typedef struct speech_channel speech_channel_t;
/* speech channel interface for UniMRCP */
static apt_bool_t speech_on_session_terminate(mrcp_application_t *application, mrcp_session_t *session, mrcp_sig_status_code_e status);
static apt_bool_t speech_on_channel_add(mrcp_application_t *application, mrcp_session_t *session, mrcp_channel_t *channel,
mrcp_sig_status_code_e status);
static apt_bool_t speech_on_channel_remove(mrcp_application_t *application, mrcp_session_t *session, mrcp_channel_t *channel,
mrcp_sig_status_code_e status);
/* speech_channel funcs */
static switch_status_t speech_channel_create(speech_channel_t ** schannel, const char *name, const char *session_uuid, speech_channel_type_t type, mod_unimrcp_application_t *app,
uint16_t rate, switch_memory_pool_t *pool);
static mpf_termination_t *speech_channel_create_mpf_termination(speech_channel_t *schannel);
static switch_status_t speech_channel_open(speech_channel_t *schannel, profile_t *profile);
static switch_status_t speech_channel_destroy(speech_channel_t *schannel);
static switch_status_t speech_channel_stop(speech_channel_t *schannel);
static switch_status_t speech_channel_set_param(speech_channel_t *schannel, const char *name, const char *val);
static switch_status_t speech_channel_write(speech_channel_t *schannel, void *data, switch_size_t *len);
static switch_status_t speech_channel_read(speech_channel_t *schannel, void *data, switch_size_t *len, int block);
static switch_status_t speech_channel_set_state(speech_channel_t *schannel, speech_channel_state_t state);
static switch_status_t speech_channel_set_state_unlocked(speech_channel_t *schannel, speech_channel_state_t state);
static const char *speech_channel_state_to_string(speech_channel_state_t state);
static const char *speech_channel_type_to_string(speech_channel_type_t type);
/*********************************************************************************************************************************************
* SYNTHESIZER : UniMRCP <--> FreeSWITCH tts interface
*/
/* synthesis languages */
#define MIME_TYPE_PLAIN_TEXT "text/plain"
static switch_status_t synth_load(switch_loadable_module_interface_t *module_interface, switch_memory_pool_t *pool);
static switch_status_t synth_shutdown();
/* synthesizer's interface for FreeSWITCH */
static switch_status_t synth_speech_open(switch_speech_handle_t *sh, const char *voice_name, int rate, int channels, switch_speech_flag_t *flags);
static switch_status_t synth_speech_close(switch_speech_handle_t *sh, switch_speech_flag_t *flags);
static switch_status_t synth_speech_feed_tts(switch_speech_handle_t *sh, char *text, switch_speech_flag_t *flags);
static switch_status_t synth_speech_read_tts(switch_speech_handle_t *sh, void *data, switch_size_t *datalen, switch_speech_flag_t *flags);
static void synth_speech_flush_tts(switch_speech_handle_t *sh);
static void synth_speech_text_param_tts(switch_speech_handle_t *sh, char *param, const char *val);
static void synth_speech_numeric_param_tts(switch_speech_handle_t *sh, char *param, int val);
static void synth_speech_float_param_tts(switch_speech_handle_t *sh, char *param, double val);
/* synthesizer's interface for UniMRCP */
static apt_bool_t synth_message_handler(const mrcp_app_message_t *app_message);
static apt_bool_t synth_on_message_receive(mrcp_application_t *application, mrcp_session_t *session, mrcp_channel_t *channel, mrcp_message_t *message);
static apt_bool_t synth_stream_write(mpf_audio_stream_t *stream, const mpf_frame_t *frame);
/* synthesizer specific speech_channel funcs */
static switch_status_t synth_channel_speak(speech_channel_t *schannel, const char *text);
static switch_status_t synth_channel_set_params(speech_channel_t *schannel, mrcp_message_t *msg, mrcp_generic_header_t *gen_hdr,
mrcp_synth_header_t *synth_hdr);
static switch_status_t synth_channel_set_header(speech_channel_t *schannel, int id, char *val, mrcp_message_t *msg, mrcp_synth_header_t *synth_hdr);
/*********************************************************************************************************************************************
* GRAMMAR : recognizer grammar management
*/
/**
* type of the grammar
*/
enum grammar_type {
GRAMMAR_TYPE_UNKNOWN,
/* text/uri-list */
GRAMMAR_TYPE_URI,
/* application/srgs */
GRAMMAR_TYPE_SRGS,
/* application/srgs+xml */
GRAMMAR_TYPE_SRGS_XML,
/* application/x-nuance-gsl */
GRAMMAR_TYPE_NUANCE_GSL,
/* application/x-jsgf */
GRAMMAR_TYPE_JSGF,
/* application/xml */
GRAMMAR_TYPE_XML
};
typedef enum grammar_type grammar_type_t;
/**
* A grammar for recognition
*/
struct grammar {
/** name of this grammar */
char *name;
/** grammar MIME type */
grammar_type_t type;
/** the grammar or its URI, depending on type */
char *data;
};
typedef struct grammar grammar_t;
static switch_status_t grammar_create(grammar_t ** grammar, const char *name, grammar_type_t type, const char *data, switch_memory_pool_t *pool);
static const char *grammar_type_to_mime(grammar_type_t type, profile_t *profile);
/*********************************************************************************************************************************************
* RECOGNIZER : UniMRCP <--> FreeSWITCH asr interface
*/
#define START_OF_INPUT_RECEIVED 1
#define START_OF_INPUT_REPORTED 2
/**
* Data specific to the recognizer
*/
struct recognizer_data {
/** the available grammars */
switch_hash_t *grammars;
/** the enabled grammars */
switch_hash_t *enabled_grammars;
/** recognize result */
char *result;
/** recognize result headers */
switch_event_t *result_headers;
/** true, if voice has started */
int start_of_input;
/** true, if input timers have started */
int timers_started;
/** UniMRCP mpf stream */
mpf_audio_stream_t *unimrcp_stream;
/** DTMF generator */
mpf_dtmf_generator_t *dtmf_generator;
/** true, if presently transmitting DTMF */
char dtmf_generator_active;
};
typedef struct recognizer_data recognizer_data_t;
static switch_status_t recog_load(switch_loadable_module_interface_t *module_interface, switch_memory_pool_t *pool);
static switch_status_t recog_shutdown();
/* recognizer's interface for FreeSWITCH */
static switch_status_t recog_asr_open(switch_asr_handle_t *ah, const char *codec, int rate, const char *dest, switch_asr_flag_t *flags);
static switch_status_t recog_asr_load_grammar(switch_asr_handle_t *ah, const char *grammar, const char *name);
static switch_status_t recog_asr_unload_grammar(switch_asr_handle_t *ah, const char *name);
static switch_status_t recog_asr_enable_grammar(switch_asr_handle_t *ah, const char *name);
static switch_status_t recog_asr_disable_grammar(switch_asr_handle_t *ah, const char *name);
static switch_status_t recog_asr_disable_all_grammars(switch_asr_handle_t *ah);
static switch_status_t recog_asr_close(switch_asr_handle_t *ah, switch_asr_flag_t *flags);
static switch_status_t recog_asr_feed(switch_asr_handle_t *ah, void *data, unsigned int len, switch_asr_flag_t *flags);
static switch_status_t recog_asr_feed_dtmf(switch_asr_handle_t *ah, const switch_dtmf_t *dtmf, switch_asr_flag_t *flags);
static switch_status_t recog_asr_resume(switch_asr_handle_t *ah);
static switch_status_t recog_asr_pause(switch_asr_handle_t *ah);
static switch_status_t recog_asr_check_results(switch_asr_handle_t *ah, switch_asr_flag_t *flags);
static switch_status_t recog_asr_get_results(switch_asr_handle_t *ah, char **xmlstr, switch_asr_flag_t *flags);
static switch_status_t recog_asr_get_result_headers(switch_asr_handle_t *ah, switch_event_t **headers, switch_asr_flag_t *flags);
static switch_status_t recog_asr_start_input_timers(switch_asr_handle_t *ah);
static void recog_asr_text_param(switch_asr_handle_t *ah, char *param, const char *val);
static void recog_asr_numeric_param(switch_asr_handle_t *ah, char *param, int val);
static void recog_asr_float_param(switch_asr_handle_t *ah, char *param, double val);
/* recognizer's interface for UniMRCP */
static apt_bool_t recog_message_handler(const mrcp_app_message_t *app_message);
static apt_bool_t recog_on_message_receive(mrcp_application_t *application, mrcp_session_t *session, mrcp_channel_t *channel, mrcp_message_t *message);
static apt_bool_t recog_stream_open(mpf_audio_stream_t *stream, mpf_codec_t *codec);
static apt_bool_t recog_stream_read(mpf_audio_stream_t *stream, mpf_frame_t *frame);
/* recognizer specific speech_channel_funcs */
static switch_status_t recog_channel_start(speech_channel_t *schannel);
static switch_status_t recog_channel_load_grammar(speech_channel_t *schannel, const char *name, grammar_type_t type, const char *data);
static switch_status_t recog_channel_unload_grammar(speech_channel_t *schannel, const char *name);
static switch_status_t recog_channel_enable_grammar(speech_channel_t *schannel, const char *name);
static switch_status_t recog_channel_disable_grammar(speech_channel_t *schannel, const char *name);
static switch_status_t recog_channel_disable_all_grammars(speech_channel_t *schannel);
static switch_status_t recog_channel_check_results(speech_channel_t *schannel);
static switch_status_t recog_channel_set_start_of_input(speech_channel_t *schannel);
static switch_status_t recog_channel_start_input_timers(speech_channel_t *schannel);
static switch_status_t recog_channel_set_results(speech_channel_t *schannel, const char *results);
static switch_status_t recog_channel_set_result_headers(speech_channel_t *schannel, mrcp_recog_header_t *recog_hdr);
static switch_status_t recog_channel_get_results(speech_channel_t *schannel, char **results);
static switch_status_t recog_channel_get_result_headers(speech_channel_t *schannel, switch_event_t **result_headers);
static switch_status_t recog_channel_set_params(speech_channel_t *schannel, mrcp_message_t *msg, mrcp_generic_header_t *gen_hdr,
mrcp_recog_header_t *recog_hdr);
static switch_status_t recog_channel_set_header(speech_channel_t *schannel, int id, char *val, mrcp_message_t *msg, mrcp_recog_header_t *recog_hdr);
static switch_status_t recog_channel_set_timers_started(speech_channel_t *schannel);
/**
* Create a mod_unimrcp profile
* @param profile the created profile
* @param name the profile name
* @param pool the memory pool to use
* @return SWITCH_STATUS_SUCCESS if the profile is created
*/
static switch_status_t profile_create(profile_t ** profile, const char *name, switch_memory_pool_t *pool)
{
switch_status_t status = SWITCH_STATUS_SUCCESS;
profile_t *lprofile = NULL;
switch_event_t *event = NULL;
lprofile = (profile_t *) switch_core_alloc(pool, sizeof(profile_t));
if (lprofile) {
lprofile->name = switch_core_strdup(pool, name);
lprofile->srgs_mime_type = "application/srgs";
lprofile->srgs_xml_mime_type = "application/srgs+xml";
lprofile->gsl_mime_type = "application/x-nuance-gsl";
lprofile->jsgf_mime_type = "application/x-jsgf";
lprofile->ssml_mime_type = "application/ssml+xml";
lprofile->xml_mime_type = "application/xml";
switch_core_hash_init(&lprofile->default_synth_params);
switch_core_hash_init(&lprofile->default_recog_params);
*profile = lprofile;
if (globals.enable_profile_events && switch_event_create_subclass(&event, SWITCH_EVENT_CUSTOM, MY_EVENT_PROFILE_CREATE) == SWITCH_STATUS_SUCCESS) {
switch_event_add_header_string(event, SWITCH_STACK_BOTTOM, "MRCP-Profile", lprofile->name);
switch_event_fire(&event);
}
} else {
*profile = NULL;
status = SWITCH_STATUS_FALSE;
}
return status;
}
/**
* Inspect text to determine if its first non-whitespace text matches "match"
* @param text the text to inspect.
* @param match the text to match
* @return true if matches
*/
static int text_starts_with(const char *text, const char *match)
{
int result = 0;
text = skip_initial_whitespace(text);
if (!zstr(text)) {
size_t textlen, matchlen;
textlen = strlen(text);
matchlen = strlen(match);
/* is there a match? */
result = textlen > matchlen && !strncmp(match, text, matchlen);
}
return result;
}
/**
* Find the first non-whitespace text character in text
* @param text the text to scan
* @return pointer to the first non-whitespace char in text or the empty string if none
*/
static const char *skip_initial_whitespace(const char *text)
{
if (!zstr(text)) {
while(switch_isspace(*text)) {
text++;
}
}
return text;
}
/**
* Create the audio queue
*
* @param audio_queue the created queue
* @param name the name of this queue (for logging)
* @param session_uuid optional session associated with this channel
* @param pool memory pool to allocate queue from
* @return SWITCH_STATUS_SUCCESS if successful. SWITCH_STATUS_FALSE if unable to allocate queue
*/
static switch_status_t audio_queue_create(audio_queue_t ** audio_queue, const char *name, const char *session_uuid, switch_memory_pool_t *pool)
{
switch_status_t status = SWITCH_STATUS_SUCCESS;
audio_queue_t *laudio_queue = NULL;
#ifdef MOD_UNIMRCP_DEBUG_AUDIO_QUEUE
int flags;
#endif
char *lname;
char *lsession_uuid = NULL;
*audio_queue = NULL;
lname = zstr(name) ? "" : switch_core_strdup(pool, name);
lsession_uuid = zstr(session_uuid) ? NULL : switch_core_strdup(pool, session_uuid);
if ((laudio_queue = (audio_queue_t *) switch_core_alloc(pool, sizeof(audio_queue_t))) == NULL) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(lsession_uuid), SWITCH_LOG_ERROR, "(%s) unable to create audio queue\n", lname);
status = SWITCH_STATUS_FALSE;
goto done;
}
laudio_queue->name = lname;
laudio_queue->session_uuid = lsession_uuid;
if (switch_buffer_create(pool, &laudio_queue->buffer, AUDIO_QUEUE_SIZE) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(lsession_uuid), SWITCH_LOG_ERROR, "(%s) unable to create audio queue buffer\n", laudio_queue->name);
status = SWITCH_STATUS_FALSE;
goto done;
}
if (switch_mutex_init(&laudio_queue->mutex, SWITCH_MUTEX_UNNESTED, pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(lsession_uuid), SWITCH_LOG_ERROR, "(%s) unable to create audio queue mutex\n", laudio_queue->name);
status = SWITCH_STATUS_FALSE;
goto done;
}
if (switch_thread_cond_create(&laudio_queue->cond, pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(lsession_uuid), SWITCH_LOG_ERROR, "(%s) unable to create audio queue condition variable\n", laudio_queue->name);
status = SWITCH_STATUS_FALSE;
goto done;
}
#ifdef MOD_UNIMRCP_DEBUG_AUDIO_QUEUE
flags = SWITCH_FOPEN_CREATE | SWITCH_FOPEN_WRITE | SWITCH_FOPEN_TRUNCATE | SWITCH_FOPEN_BINARY;
strcpy(laudio_queue->file_read_name, "/tmp/mod_unimrcp_rx_XXXXXX");
if (switch_file_mktemp(&laudio_queue->file_read, laudio_queue->file_read_name, flags, pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(laudio_queue->session_uuid), SWITCH_LOG_ERROR, "(%s) unable to create audio queue read file\n", laudio_queue->name);
laudio_queue->file_read = NULL;
} else {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(laudio_queue->session_uuid), SWITCH_LOG_DEBUG, "(%s) queue rx saved to %s\n", laudio_queue->name, laudio_queue->file_read_name);
}
strcpy(laudio_queue->file_write_name, "/tmp/mod_unimrcp_tx_XXXXXX");
if (switch_file_mktemp(&laudio_queue->file_write, laudio_queue->file_write_name, flags, pool) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(laudio_queue->session_uuid), SWITCH_LOG_ERROR, "(%s) unable to create audio queue write file\n", laudio_queue->name);
laudio_queue->file_write = NULL;
} else {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(laudio_queue->session_uuid), SWITCH_LOG_DEBUG, "(%s) queue tx saved to %s\n", laudio_queue->name, laudio_queue->file_write_name);
}
#endif
laudio_queue->write_bytes = 0;
laudio_queue->read_bytes = 0;
laudio_queue->waiting = 0;
*audio_queue = laudio_queue;
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(laudio_queue->session_uuid), SWITCH_LOG_DEBUG, "(%s) audio queue created\n", laudio_queue->name);
done:
if (status != SWITCH_STATUS_SUCCESS) {
audio_queue_destroy(laudio_queue);
}
return status;
}
/**
* Write to the audio queue
*
* @param queue the queue to write to
* @param data the data to write
* @param data_len the number of octets to write
* @return SWITCH_STATUS_SUCCESS if data was written, SWITCH_STATUS_FALSE if data can't be written because queue is full
*/
static switch_status_t audio_queue_write(audio_queue_t *queue, void *data, switch_size_t *data_len)
{
switch_status_t status = SWITCH_STATUS_SUCCESS;
#ifdef MOD_UNIMRCP_DEBUG_AUDIO_QUEUE
switch_size_t len = *data_len;
#endif
switch_mutex_lock(queue->mutex);
#ifdef MOD_UNIMRCP_DEBUG_AUDIO_QUEUE
if (queue->file_write) {
switch_file_write(queue->file_write, data, &len);
}
#endif
if (switch_buffer_write(queue->buffer, data, *data_len) > 0) {
queue->write_bytes = queue->write_bytes + *data_len;
#ifdef MOD_UNIMRCP_DEBUG_AUDIO_QUEUE
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(queue->session_uuid), SWITCH_LOG_DEBUG, "(%s) audio queue write total = %ld\trequested = %ld\n", queue->name, queue->write_bytes,
*data_len);
#endif
if (queue->waiting <= switch_buffer_inuse(queue->buffer)) {
switch_thread_cond_signal(queue->cond);
}
} else {
*data_len = 0;
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(queue->session_uuid), SWITCH_LOG_DEBUG, "(%s) audio queue overflow!\n", queue->name);
status = SWITCH_STATUS_FALSE;
}
switch_mutex_unlock(queue->mutex);
return status;
}
/**
* Read from the audio queue
*
* @param queue the queue to read from
* @param data the read data
* @param data_len the amount of data requested / actual amount of data read (returned)
* @param block 1 if blocking is allowed
* @return SWITCH_STATUS_SUCCESS if successful. SWITCH_STATUS_FALSE if no data was requested, or there was a timeout while waiting to read
*/
static switch_status_t audio_queue_read(audio_queue_t *queue, void *data, switch_size_t *data_len, int block)
{
switch_size_t requested = *data_len;
switch_status_t status = SWITCH_STATUS_SUCCESS;
#ifdef MOD_UNIMRCP_DEBUG_AUDIO_QUEUE
switch_size_t len = *data_len;
#endif
switch_mutex_lock(queue->mutex);
/* allow the initial frame to buffer */
if (!queue->read_bytes && switch_buffer_inuse(queue->buffer) < requested) {
*data_len = 0;
status = SWITCH_STATUS_SUCCESS;
goto done;
}
/* wait for data, if allowed */
if (block) {
while (switch_buffer_inuse(queue->buffer) < requested) {
queue->waiting = requested;
if (switch_thread_cond_timedwait(queue->cond, queue->mutex, AUDIO_TIMEOUT_USEC) == SWITCH_STATUS_TIMEOUT) {
break;
}
}
queue->waiting = 0;
}
if (switch_buffer_inuse(queue->buffer) < requested) {
requested = switch_buffer_inuse(queue->buffer);
}
if (requested == 0) {
*data_len = 0;
status = SWITCH_STATUS_FALSE;
goto done;
}
/* read the data */
*data_len = switch_buffer_read(queue->buffer, data, requested);
queue->read_bytes = queue->read_bytes + *data_len;
#ifdef MOD_UNIMRCP_DEBUG_AUDIO_QUEUE
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(queue->session_uuid), SWITCH_LOG_DEBUG, "(%s) audio queue read total = %ld\tread = %ld\trequested = %ld\n", queue->name,
queue->read_bytes, *data_len, requested);
if (queue->file_read) {
switch_file_write(queue->file_read, data, &len);
}
#endif
done:
switch_mutex_unlock(queue->mutex);
return status;
}
/**
* Empty the queue
*
* @param queue the queue to empty
* @return SWITCH_STATUS_SUCCESS
*/
static switch_status_t audio_queue_clear(audio_queue_t *queue)
{
switch_mutex_lock(queue->mutex);
switch_buffer_zero(queue->buffer);
switch_thread_cond_signal(queue->cond);
switch_mutex_unlock(queue->mutex);
queue->read_bytes = 0;
queue->write_bytes = 0;
queue->waiting = 0;
return SWITCH_STATUS_SUCCESS;
}
/**
* Wake any threads waiting on this queue
*
* @param queue the queue to empty
* @return SWITCH_STATUS_SUCCESS
*/
static switch_status_t audio_queue_signal(audio_queue_t *queue)
{
switch_mutex_lock(queue->mutex);
switch_thread_cond_signal(queue->cond);
switch_mutex_unlock(queue->mutex);
return SWITCH_STATUS_SUCCESS;
}
/**
* Destroy the audio queue
*
* @param queue the queue to clean up
* @return SWITCH_STATUS_SUCCESS
*/
static switch_status_t audio_queue_destroy(audio_queue_t *queue)
{
if (queue) {
char *name = queue->name;
if (zstr(name)) {
name = "";
}
#ifdef MOD_UNIMRCP_DEBUG_AUDIO_QUEUE
if (queue->file_read) {
switch_file_close(queue->file_read);
queue->file_read = NULL;
}
if (queue->file_write) {
switch_file_close(queue->file_write);
queue->file_write = NULL;
}
#endif
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(queue->session_uuid), SWITCH_LOG_DEBUG, "(%s) audio queue destroyed\n", name);
}
return SWITCH_STATUS_SUCCESS;
}
/**
* Create a speech channel
*
* @param schannel the created channel
* @param name the name of the channel
* @param session_uuid optional session associated with this channel
* @param type the type of channel to create
* @param app the application
* @param rate the rate to use
* @param pool the memory pool to use
* @return SWITCH_STATUS_SUCCESS if successful. SWITCH_STATUS_FALSE if the channel cannot be allocated.
*/
static switch_status_t speech_channel_create(speech_channel_t ** schannel, const char *name, const char *session_uuid, speech_channel_type_t type, mod_unimrcp_application_t *app,
uint16_t rate, switch_memory_pool_t *pool)
{
switch_status_t status = SWITCH_STATUS_SUCCESS;
speech_channel_t *schan = NULL;
*schannel = NULL;
if ((schan = (speech_channel_t *) switch_core_alloc(pool, sizeof(speech_channel_t))) == NULL) {
status = SWITCH_STATUS_FALSE;
goto done;
}
schan->profile = NULL;
schan->type = type;
schan->application = app;
schan->state = SPEECH_CHANNEL_CLOSED;
schan->memory_pool = pool;
schan->apr_pool = NULL;
schan->params = NULL;
schan->rate = rate;
schan->silence = 0; /* L16 silence sample */
schan->channel_opened = 0;
apr_pool_create(&schan->apr_pool, NULL);
if (switch_mutex_init(&schan->mutex, SWITCH_MUTEX_UNNESTED, pool) != SWITCH_STATUS_SUCCESS ||
switch_thread_cond_create(&schan->cond, pool) != SWITCH_STATUS_SUCCESS ||
audio_queue_create(&schan->audio_queue, name, session_uuid, pool) != SWITCH_STATUS_SUCCESS) {
status = SWITCH_STATUS_FALSE;
goto done;
}
switch_core_hash_init(&schan->params);
schan->data = NULL;
schan->name = zstr(name) ? "" : switch_core_strdup(pool, name);
schan->session_uuid = zstr(session_uuid) ? NULL : switch_core_strdup(pool, session_uuid);
*schannel = schan;
done:
return status;
}
/**
* Destroy the speech channel
*
* @param schannel the channel to destroy
* @return SWITCH_STATUS_SUCCESS
*/
static switch_status_t speech_channel_destroy(speech_channel_t *schannel)
{
if (schannel) {
/* Terminate the MRCP session if not already done */
if (schannel->mutex) {
switch_mutex_lock(schannel->mutex);
if (schannel->state != SPEECH_CHANNEL_CLOSED) {
int warned = 0;
mrcp_application_session_terminate(schannel->unimrcp_session);
/* wait forever for session to terminate. Log WARNING if this starts taking too long */
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_DEBUG, "(%s) Waiting for MRCP session to terminate\n", schannel->name);
while (schannel->state != SPEECH_CHANNEL_CLOSED) {
if (switch_thread_cond_timedwait(schannel->cond, schannel->mutex, SPEECH_CHANNEL_TIMEOUT_USEC) == SWITCH_STATUS_TIMEOUT && !warned) {
warned = 1;
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(schannel->session_uuid), SWITCH_LOG_WARNING, "(%s) MRCP session has not terminated after %d ms\n", schannel->name, SPEECH_CHANNEL_TIMEOUT_USEC / (1000));
}
}
}
switch_mutex_unlock(schannel->mutex);
}
/* It is now safe to clean up the speech channel */
if (schannel->mutex) {
switch_mutex_lock(schannel->mutex);
}
audio_queue_destroy(schannel->audio_queue);
schannel->audio_queue = NULL;
if (schannel->params) {
switch_core_hash_destroy(&schannel->params);
}
if (schannel->mutex) {
switch_mutex_unlock(schannel->mutex);
}
if (schannel->apr_pool) {
apr_pool_destroy(schannel->apr_pool);
schannel->apr_pool = NULL;
}
}
return SWITCH_STATUS_SUCCESS;
}
/**
* Create the audio termination for the speech channel
* @param schannel the speech channel
* @return the termination or NULL
*/
static mpf_termination_t *speech_channel_create_mpf_termination(speech_channel_t *schannel)
{
mpf_termination_t *termination = NULL;
mpf_stream_capabilities_t *capabilities = NULL;
int sample_rates;
if (schannel->type == SPEECH_CHANNEL_SYNTHESIZER) {
capabilities = mpf_sink_stream_capabilities_create(schannel->unimrcp_session->pool);
} else {
capabilities = mpf_source_stream_capabilities_create(schannel->unimrcp_session->pool);
}
/* FreeSWITCH is capable of resampling so pick rates that are are multiples of the desired rate.
* UniMRCP should transcode whatever the MRCP server wants to use into LPCM (host-byte ordered L16) for us.
*/
if (schannel->rate == 16000) {
sample_rates = MPF_SAMPLE_RATE_8000 | MPF_SAMPLE_RATE_16000;
} else if (schannel->rate == 32000) {
sample_rates = MPF_SAMPLE_RATE_8000 | MPF_SAMPLE_RATE_16000 | MPF_SAMPLE_RATE_32000;
} else if (schannel->rate == 48000) {
sample_rates = MPF_SAMPLE_RATE_8000 | MPF_SAMPLE_RATE_16000 | MPF_SAMPLE_RATE_48000;
} else {
sample_rates = MPF_SAMPLE_RATE_8000;
}
mpf_codec_capabilities_add(&capabilities->codecs, sample_rates, "LPCM");
termination =
mrcp_application_audio_termination_create(schannel->unimrcp_session, &schannel->application->audio_stream_vtable, capabilities, schannel);
return termination;
}
/**
* Open the speech channel
*