Repository navigation
Expand file tree
/
Copy pathrayo_output_component.c
More file actions
1410 lines (1263 loc) · 47.7 KB
/
Copy pathrayo_output_component.c
File metadata and controls
1410 lines (1263 loc) · 47.7 KB
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
/*
* mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
* Copyright (C) 2013-2018, Grasshopper
*
* 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 mod_rayo for FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
*
* The Initial Developer of the Original Code is Grasshopper
* Portions created by the Initial Developer are Copyright (C)
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Chris Rienzo <chris.rienzo@grasshopper.com>
*
* output_component.c -- Rayo output component implementation
*
*/
#include "rayo_components.h"
#include "rayo_elements.h"
/**
* An output component
*/
struct output_component {
/** component base class */
struct rayo_component base;
/** document to play */
iks *document;
/** where to start playing in document */
int start_offset_ms;
/** maximum time to play */
int max_time_ms;
/** silence between repeats */
int repeat_interval_ms;
/** number of times to repeat */
int repeat_times;
/** true if started paused */
switch_bool_t start_paused;
/** true if stopped */
int stop;
/** output renderer to use */
const char *renderer;
/** optional headers to pass to renderer */
const char *headers;
/** audio direction */
const char *direction;
};
#define OUTPUT_FINISH "finish", RAYO_OUTPUT_COMPLETE_NS
#define OUTPUT_MAX_TIME "max-time", RAYO_OUTPUT_COMPLETE_NS
#define OUTPUT_COMPONENT(x) ((struct output_component *)x)
/**
* Create new output component
*/
static struct rayo_component *create_output_component(struct rayo_actor *actor, const char *type, iks *output, const char *client_jid)
{
switch_memory_pool_t *pool;
struct output_component *output_component = NULL;
switch_core_new_memory_pool(&pool);
output_component = switch_core_alloc(pool, sizeof(*output_component));
output_component = OUTPUT_COMPONENT(rayo_component_init((struct rayo_component *)output_component, pool, type, "output", NULL, actor, client_jid));
if (output_component) {
output_component->document = iks_copy(output);
output_component->start_offset_ms = iks_find_int_attrib(output, "start-offset");
output_component->repeat_interval_ms = iks_find_int_attrib(output, "repeat-interval");
output_component->repeat_times = iks_find_int_attrib(output, "repeat-times");
output_component->max_time_ms = iks_find_int_attrib(output, "max-time");
output_component->start_paused = iks_find_bool_attrib(output, "start-paused");
output_component->renderer = switch_core_strdup(RAYO_POOL(output_component), iks_find_attrib_soft(output, "renderer"));
output_component->direction = strcmp(iks_find_attrib_soft(output, "direction"), "in") ? "m" : "mr";
output_component->headers = NULL;
/* get custom headers */
{
switch_stream_handle_t headers = { 0 };
iks *header = NULL;
int first = 1;
SWITCH_STANDARD_STREAM(headers);
for (header = iks_find(output, "header"); header; header = iks_next_tag(header)) {
if (!strcmp("header", iks_name(header))) {
const char *name = iks_find_attrib_soft(header, "name");
const char *value = iks_find_attrib_soft(header, "value");
if (!zstr(name) && !zstr(value)) {
headers.write_function(&headers, "%s%s=%s", first ? "{" : ",", name, value);
first = 0;
}
}
}
if (headers.data && !first) {
headers.write_function(&headers, "}");
output_component->headers = switch_core_strdup(RAYO_POOL(output_component), (char *)headers.data);
}
switch_safe_free(headers.data);
}
} else {
switch_core_destroy_memory_pool(&pool);
}
return RAYO_COMPONENT(output_component);
}
/**
* Start execution of call output component
* @param component to start
* @param session the session to output to
* @param output the output request
* @param iq the original request
*/
static iks *start_call_output(struct rayo_component *component, switch_core_session_t *session, iks *output, iks *iq)
{
switch_stream_handle_t stream = { 0 };
/* acknowledge command */
rayo_component_send_start(component, iq);
/* build playback command */
SWITCH_STANDARD_STREAM(stream);
stream.write_function(&stream, "{id=%s,session=%s,pause=%s",
RAYO_JID(component), switch_core_session_get_uuid(session),
OUTPUT_COMPONENT(component)->start_paused ? "true" : "false");
if (OUTPUT_COMPONENT(component)->max_time_ms > 0) {
stream.write_function(&stream, ",timeout=%i", OUTPUT_COMPONENT(component)->max_time_ms);
}
if (OUTPUT_COMPONENT(component)->start_offset_ms > 0) {
stream.write_function(&stream, ",start_offset_ms=%i", OUTPUT_COMPONENT(component)->start_offset_ms);
}
stream.write_function(&stream, "}fileman://rayo://%s", RAYO_JID(component));
if (switch_ivr_displace_session(session, stream.data, 0, OUTPUT_COMPONENT(component)->direction) == SWITCH_STATUS_SUCCESS) {
RAYO_RELEASE(component);
} else {
if (component->complete) {
/* component is already destroyed */
RAYO_RELEASE(component);
} else {
/* need to destroy component */
if (OUTPUT_COMPONENT(component)->document) {
iks_delete(OUTPUT_COMPONENT(component)->document);
}
if (switch_channel_get_state(switch_core_session_get_channel(session)) >= CS_HANGUP) {
rayo_component_send_complete(component, COMPONENT_COMPLETE_HANGUP);
} else {
rayo_component_send_complete(component, COMPONENT_COMPLETE_ERROR);
}
}
}
switch_safe_free(stream.data);
return NULL;
}
/**
* Start execution of call output component
*/
static iks *start_call_output_component(struct rayo_actor *call, struct rayo_message *msg, void *session_data)
{
iks *iq = msg->payload;
switch_core_session_t *session = (switch_core_session_t *)session_data;
struct rayo_component *output_component = NULL;
iks *output = iks_find(iq, "output");
iks *document = NULL;
/* validate output attributes */
if (!VALIDATE_RAYO_OUTPUT(output)) {
return iks_new_error(iq, STANZA_ERROR_BAD_REQUEST);
}
/* check if <document> exists */
document = iks_find(output, "document");
if (!document) {
return iks_new_error(iq, STANZA_ERROR_BAD_REQUEST);
}
output_component = create_output_component(call, RAT_CALL_COMPONENT, output, iks_find_attrib(iq, "from"));
if (!output_component) {
return iks_new_error_detailed(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "Failed to create output entity");
}
return start_call_output(output_component, session, output, iq);
}
/**
* Start execution of mixer output component
*/
static iks *start_mixer_output_component(struct rayo_actor *mixer, struct rayo_message *msg, void *data)
{
iks *iq = msg->payload;
struct rayo_component *component = NULL;
iks *output = iks_find(iq, "output");
iks *document = NULL;
switch_stream_handle_t stream = { 0 };
/* validate output attributes */
if (!VALIDATE_RAYO_OUTPUT(output)) {
return iks_new_error(iq, STANZA_ERROR_BAD_REQUEST);
}
/* check if <document> exists */
document = iks_find(output, "document");
if (!document) {
return iks_new_error(iq, STANZA_ERROR_BAD_REQUEST);
}
component = create_output_component(mixer, RAT_MIXER_COMPONENT, output, iks_find_attrib(iq, "from"));
if (!component) {
return iks_new_error_detailed(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "Failed to create output entity");
}
/* build conference command */
SWITCH_STANDARD_STREAM(stream);
stream.write_function(&stream, "%s play ", rayo_mixer_get_name(RAYO_MIXER(mixer)), RAYO_ID(component));
stream.write_function(&stream, "{id=%s,pause=%s",
RAYO_JID(component),
OUTPUT_COMPONENT(component)->start_paused ? "true" : "false");
if (OUTPUT_COMPONENT(component)->max_time_ms > 0) {
stream.write_function(&stream, ",timeout=%i", OUTPUT_COMPONENT(component)->max_time_ms);
}
if (OUTPUT_COMPONENT(component)->start_offset_ms > 0) {
stream.write_function(&stream, ",start_offset_ms=%i", OUTPUT_COMPONENT(component)->start_offset_ms);
}
stream.write_function(&stream, "}fileman://rayo://%s", RAYO_JID(component));
/* acknowledge command */
rayo_component_send_start(component, iq);
rayo_component_api_execute_async(component, "conference", stream.data);
switch_safe_free(stream.data);
RAYO_RELEASE(component);
return NULL;
}
/**
* Stop execution of output component
*/
static iks *stop_output_component(struct rayo_actor *component, struct rayo_message *msg, void *data)
{
iks *iq = msg->payload;
iks *result = NULL;
switch_core_session_t *session = NULL;
switch_stream_handle_t stream = { 0 };
char *command = switch_mprintf("%s stop", RAYO_JID(component));
SWITCH_STANDARD_STREAM(stream);
OUTPUT_COMPONENT(component)->stop = 1;
if (!strcmp(RAYO_ACTOR(component)->type, RAT_CALL_COMPONENT)) {
session = (switch_core_session_t *)data;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s stopping\n", RAYO_JID(component));
switch_api_execute("fileman", command, NULL, &stream);
if (!zstr((char *)stream.data) && !strncmp((char *)stream.data, "+OK", 3)) {
result = iks_new_iq_result(iq);
} else if (session && switch_channel_get_state(switch_core_session_get_channel(session)) >= CS_HANGUP) {
result = iks_new_error_detailed(iq, STANZA_ERROR_UNEXPECTED_REQUEST, "call has ended");
} else if (!zstr((char *)stream.data)) {
result = iks_new_error_detailed_printf(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "%s", (char *)stream.data);
} else {
result = iks_new_error(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR);
}
switch_safe_free(stream.data);
switch_safe_free(command);
return result;
}
/**
* Pause execution of output component
*/
static iks *pause_output_component(struct rayo_actor *component, struct rayo_message *msg, void *data)
{
iks *iq = msg->payload;
iks *result = NULL;
switch_core_session_t *session = NULL;
switch_stream_handle_t stream = { 0 };
char *command = switch_mprintf("%s pause", RAYO_JID(component));
SWITCH_STANDARD_STREAM(stream);
if (!strcmp(RAYO_ACTOR(component)->type, RAT_CALL_COMPONENT)) {
session = (switch_core_session_t *)data;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s pausing\n", RAYO_JID(component));
switch_api_execute("fileman", command, NULL, &stream);
if (!zstr((char *)stream.data) && !strncmp((char *)stream.data, "+OK", 3)) {
result = iks_new_iq_result(iq);
} else if (session && switch_channel_get_state(switch_core_session_get_channel(session)) >= CS_HANGUP) {
result = iks_new_error_detailed(iq, STANZA_ERROR_UNEXPECTED_REQUEST, "call has ended");
} else if (!zstr((char *)stream.data)) {
result = iks_new_error_detailed_printf(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "%s", (char *)stream.data);
} else {
result = iks_new_error(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR);
}
switch_safe_free(stream.data);
switch_safe_free(command);
return result;
}
/**
* Resume execution of output component
*/
static iks *resume_output_component(struct rayo_actor *component, struct rayo_message *msg, void *data)
{
iks *iq = msg->payload;
iks *result = NULL;
switch_core_session_t *session = NULL;
switch_stream_handle_t stream = { 0 };
char *command = switch_mprintf("%s resume", RAYO_JID(component));
SWITCH_STANDARD_STREAM(stream);
if (!strcmp(RAYO_ACTOR(component)->type, RAT_CALL_COMPONENT)) {
session = (switch_core_session_t *)data;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s resuming\n", RAYO_JID(component));
switch_api_execute("fileman", command, NULL, &stream);
if (!zstr((char *)stream.data) && !strncmp((char *)stream.data, "+OK", 3)) {
result = iks_new_iq_result(iq);
} else if (session && switch_channel_get_state(switch_core_session_get_channel(session)) >= CS_HANGUP) {
result = iks_new_error_detailed(iq, STANZA_ERROR_UNEXPECTED_REQUEST, "call has ended");
} else if (!zstr((char *)stream.data)) {
result = iks_new_error_detailed_printf(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "%s", (char *)stream.data);
} else {
result = iks_new_error(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR);
}
switch_safe_free(stream.data);
switch_safe_free(command);
return result;
}
/**
* Speed up execution of output component
*/
static iks *speed_up_output_component(struct rayo_actor *component, struct rayo_message *msg, void *data)
{
iks *iq = msg->payload;
iks *result = NULL;
switch_core_session_t *session = NULL;
switch_stream_handle_t stream = { 0 };
char *command = switch_mprintf("%s speed:+", RAYO_JID(component));
SWITCH_STANDARD_STREAM(stream);
if (!strcmp(RAYO_ACTOR(component)->type, RAT_CALL_COMPONENT)) {
session = (switch_core_session_t *)data;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s speeding up\n", RAYO_JID(component));
switch_api_execute("fileman", command, NULL, &stream);
if (!zstr((char *)stream.data) && !strncmp((char *)stream.data, "+OK", 3)) {
result = iks_new_iq_result(iq);
} else if (session && switch_channel_get_state(switch_core_session_get_channel(session)) >= CS_HANGUP) {
result = iks_new_error_detailed(iq, STANZA_ERROR_UNEXPECTED_REQUEST, "call has ended");
} else if (!zstr((char *)stream.data)) {
result = iks_new_error_detailed_printf(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "%s", (char *)stream.data);
} else {
result = iks_new_error(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR);
}
switch_safe_free(stream.data);
switch_safe_free(command);
return result;
}
/**
* Slow down execution of output component
*/
static iks *speed_down_output_component(struct rayo_actor *component, struct rayo_message *msg, void *data)
{
iks *iq = msg->payload;
iks *result = NULL;
switch_core_session_t *session = NULL;
switch_stream_handle_t stream = { 0 };
char *command = switch_mprintf("%s speed:-", RAYO_JID(component));
SWITCH_STANDARD_STREAM(stream);
if (!strcmp(RAYO_ACTOR(component)->type, RAT_CALL_COMPONENT)) {
session = (switch_core_session_t *)data;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s slowing down\n", RAYO_JID(component));
switch_api_execute("fileman", command, NULL, &stream);
if (!zstr((char *)stream.data) && !strncmp((char *)stream.data, "+OK", 3)) {
result = iks_new_iq_result(iq);
} else if (session && switch_channel_get_state(switch_core_session_get_channel(session)) >= CS_HANGUP) {
result = iks_new_error_detailed(iq, STANZA_ERROR_UNEXPECTED_REQUEST, "call has ended");
} else if (!zstr((char *)stream.data)) {
result = iks_new_error_detailed_printf(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "%s", (char *)stream.data);
} else {
result = iks_new_error(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR);
}
switch_safe_free(stream.data);
switch_safe_free(command);
return result;
}
/**
* Increase volume of output component
*/
static iks *volume_up_output_component(struct rayo_actor *component, struct rayo_message *msg, void *data)
{
iks *iq = msg->payload;
iks *result = NULL;
switch_core_session_t *session = NULL;
switch_stream_handle_t stream = { 0 };
char *command = switch_mprintf("%s volume:+", RAYO_JID(component));
SWITCH_STANDARD_STREAM(stream);
if (!strcmp(RAYO_ACTOR(component)->type, RAT_CALL_COMPONENT)) {
session = (switch_core_session_t *)data;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s increasing volume\n", RAYO_JID(component));
switch_api_execute("fileman", command, NULL, &stream);
if (!zstr((char *)stream.data) && !strncmp((char *)stream.data, "+OK", 3)) {
result = iks_new_iq_result(iq);
} else if (session && switch_channel_get_state(switch_core_session_get_channel(session)) >= CS_HANGUP) {
result = iks_new_error_detailed(iq, STANZA_ERROR_UNEXPECTED_REQUEST, "call has ended");
} else if (!zstr((char *)stream.data)) {
result = iks_new_error_detailed_printf(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "%s", (char *)stream.data);
} else {
result = iks_new_error(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR);
}
switch_safe_free(stream.data);
switch_safe_free(command);
return result;
}
/**
* Lower volume of output component
*/
static iks *volume_down_output_component(struct rayo_actor *component, struct rayo_message *msg, void *data)
{
iks *iq = msg->payload;
iks *result = NULL;
switch_core_session_t *session = NULL;
switch_stream_handle_t stream = { 0 };
char *command = switch_mprintf("%s volume:-", RAYO_JID(component));
SWITCH_STANDARD_STREAM(stream);
if (!strcmp(RAYO_ACTOR(component)->type, RAT_CALL_COMPONENT)) {
session = (switch_core_session_t *)data;
}
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "%s lowering volume\n", RAYO_JID(component));
switch_api_execute("fileman", command, NULL, &stream);
if (!zstr((char *)stream.data) && !strncmp((char *)stream.data, "+OK", 3)) {
result = iks_new_iq_result(iq);
} else if (session && switch_channel_get_state(switch_core_session_get_channel(session)) >= CS_HANGUP) {
result = iks_new_error_detailed(iq, STANZA_ERROR_UNEXPECTED_REQUEST, "call has ended");
} else if (!zstr((char *)stream.data)) {
result = iks_new_error_detailed_printf(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "%s", (char *)stream.data);
} else {
result = iks_new_error(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR);
}
switch_safe_free(stream.data);
switch_safe_free(command);
return result;
}
/**
* Seek output component
*/
static iks *seek_output_component(struct rayo_actor *component, struct rayo_message *msg, void *data)
{
iks *iq = msg->payload;
iks *seek = iks_find(iq, "seek");
if (VALIDATE_RAYO_OUTPUT_SEEK(seek)) {
iks *result = NULL;
switch_core_session_t *session = NULL;
int is_forward = !strcmp("forward", iks_find_attrib(seek, "direction"));
int amount_ms = iks_find_int_attrib(seek, "amount");
char *command = switch_mprintf("%s seek:%s%i", RAYO_JID(component),
is_forward ? "+" : "-", amount_ms);
switch_stream_handle_t stream = { 0 };
SWITCH_STANDARD_STREAM(stream);
if (!strcmp(RAYO_ACTOR(component)->type, RAT_CALL_COMPONENT)) {
session = (switch_core_session_t *)data;
}
switch_api_execute("fileman", command, NULL, &stream);
if (!zstr((char *)stream.data) && !strncmp((char *)stream.data, "+OK", 3)) {
result = iks_new_iq_result(iq);
} else if (session && switch_channel_get_state(switch_core_session_get_channel(session)) >= CS_HANGUP) {
result = iks_new_error_detailed(iq, STANZA_ERROR_UNEXPECTED_REQUEST, "call has ended");
} else if (!zstr((char *)stream.data)) {
result = iks_new_error_detailed_printf(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR, "%s", (char *)stream.data);
} else {
result = iks_new_error(iq, STANZA_ERROR_INTERNAL_SERVER_ERROR);
}
switch_safe_free(stream.data);
switch_safe_free(command);
return result;
}
return iks_new_error(iq, STANZA_ERROR_BAD_REQUEST);
}
/**
* Rayo document playback state
*/
struct rayo_file_context {
/** handle to current file */
switch_file_handle_t fh;
/** current document being played */
iks *cur_doc;
/** current file string being played */
char *ssml;
/** The component */
struct rayo_component *component;
/** number of times played */
int play_count;
/** have any files successfully opened? */
int could_open;
};
/**
* open next file for reading
* @param handle the file handle
*/
static switch_status_t next_file(switch_file_handle_t *handle)
{
int loops = 0;
struct rayo_file_context *context = handle->private_info;
struct output_component *output = context->component ? OUTPUT_COMPONENT(context->component) : NULL;
if (!output) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, "Missing output component!\n");
return SWITCH_STATUS_FALSE;
}
top:
if (switch_test_flag((&context->fh), SWITCH_FILE_OPEN)) {
switch_core_file_close(&context->fh);
}
if (switch_test_flag(handle, SWITCH_FILE_FLAG_WRITE)) {
/* unsupported */
return SWITCH_STATUS_FALSE;
}
if (!context->cur_doc) {
context->cur_doc = iks_find(output->document, "document");
if (!context->cur_doc) {
iks_delete(output->document);
output->document = NULL;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Missing <document>\n");
return SWITCH_STATUS_FALSE;
}
} else {
context->cur_doc = iks_next_tag(context->cur_doc);
}
/* done? */
if (!context->cur_doc) {
if (context->could_open && ++loops < 2 && (output->repeat_times == 0 || ++context->play_count < output->repeat_times)) {
/* repeat all document(s) */
if (!output->repeat_interval_ms) {
goto top;
}
} else {
/* no more files to play */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Done playing\n");
return SWITCH_STATUS_FALSE;
}
}
if (!context->cur_doc) {
/* play silence between repeats */
switch_safe_free(context->ssml);
context->ssml = switch_mprintf("silence_stream://%i", output->repeat_interval_ms);
} else {
/* play next document */
iks *speak = NULL;
switch_safe_free(context->ssml);
context->ssml = NULL;
speak = iks_find(context->cur_doc, "speak");
if (speak) {
/* <speak> is child node */
char *ssml_str = iks_string(NULL, speak);
if (zstr(output->renderer)) {
/* FS must parse the SSML */
context->ssml = switch_mprintf("ssml://%s", ssml_str);
} else {
/* renderer will parse the SSML */
if (!zstr(output->headers) && !strncmp("unimrcp", output->renderer, 7)) {
/* pass MRCP headers */
context->ssml = switch_mprintf("tts://%s||%s%s", output->renderer, output->headers, ssml_str);
} else {
context->ssml = switch_mprintf("tts://%s||%s", output->renderer, ssml_str);
}
}
iks_free(ssml_str);
} else if (iks_has_children(context->cur_doc)) {
/* check if <speak> is in CDATA */
const char *ssml_str = NULL;
iks *ssml = iks_child(context->cur_doc);
if (ssml && iks_type(ssml) == IKS_CDATA) {
ssml_str = iks_cdata(ssml);
}
if (zstr(ssml_str)) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Missing <document> CDATA\n");
return SWITCH_STATUS_FALSE;
}
if (zstr(output->renderer)) {
/* FS must parse the SSML */
context->ssml = switch_mprintf("ssml://%s", ssml_str);
} else {
/* renderer will parse the SSML */
if (!zstr(output->headers) && !strncmp("unimrcp", output->renderer, 7)) {
/* pass MRCP headers */
context->ssml = switch_mprintf("tts://%s||%s%s", output->renderer, output->headers, ssml_str);
} else {
context->ssml = switch_mprintf("tts://%s||%s", output->renderer, ssml_str);
}
}
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Missing <speak>\n");
return SWITCH_STATUS_FALSE;
}
}
if (switch_core_file_open(&context->fh, context->ssml, handle->channels, handle->samplerate, handle->flags, NULL) != SWITCH_STATUS_SUCCESS) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Failed to open %s\n", context->ssml);
goto top;
} else {
context->could_open = 1;
}
handle->samples = context->fh.samples;
handle->format = context->fh.format;
handle->sections = context->fh.sections;
handle->seekable = context->fh.seekable;
handle->speed = context->fh.speed;
handle->vol = context->fh.vol;
handle->offset_pos = context->fh.offset_pos;
handle->interval = context->fh.interval;
if (switch_test_flag((&context->fh), SWITCH_FILE_NATIVE)) {
switch_set_flag_locked(handle, SWITCH_FILE_NATIVE);
} else {
switch_clear_flag_locked(handle, SWITCH_FILE_NATIVE);
}
return SWITCH_STATUS_SUCCESS;
}
/**
* Transforms Rayo document into sub-format and opens file_string.
* @param handle
* @param path the inline Rayo document
* @return SWITCH_STATUS_SUCCESS if opened
*/
static switch_status_t rayo_file_open(switch_file_handle_t *handle, const char *path)
{
switch_status_t status = SWITCH_STATUS_FALSE;
struct rayo_file_context *context = switch_core_alloc(handle->memory_pool, sizeof(*context));
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "Got path %s\n", path);
context->component = RAYO_COMPONENT_LOCATE(path);
if (context->component) {
handle->private_info = context;
context->cur_doc = NULL;
context->play_count = 0;
context->could_open = 0;
status = next_file(handle);
} else {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "File error! %s\n", path);
return SWITCH_STATUS_FALSE;
}
if (status != SWITCH_STATUS_SUCCESS && context->component) {
/* complete error event will be sent by calling thread */
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, "Status = %i\n", status);
RAYO_RELEASE(context->component);
}
return status;
}
/**
* Close SSML document.
* @param handle
* @return SWITCH_STATUS_SUCCESS
*/
static switch_status_t rayo_file_close(switch_file_handle_t *handle)
{
struct rayo_file_context *context = (struct rayo_file_context *)handle->private_info;
if (context && context->component) {
struct output_component *output = OUTPUT_COMPONENT(context->component);
/* send completion and destroy */
if (!strcmp(RAYO_ACTOR(context->component)->type, RAT_CALL_COMPONENT)) {
/* call output... check for hangup */
switch_core_session_t *session = switch_core_session_locate(RAYO_ACTOR(context->component)->parent->id);
if (session) {
if (switch_channel_get_state(switch_core_session_get_channel(session)) >= CS_HANGUP) {
rayo_component_send_complete(context->component, COMPONENT_COMPLETE_HANGUP);
} else if (output->stop) {
rayo_component_send_complete(context->component, COMPONENT_COMPLETE_STOP);
} else {
rayo_component_send_complete(context->component, OUTPUT_FINISH);
}
switch_core_session_rwunlock(session);
} else {
/* session is gone */
rayo_component_send_complete(context->component, COMPONENT_COMPLETE_HANGUP);
}
} else if (output->stop) {
rayo_component_send_complete(context->component, COMPONENT_COMPLETE_STOP);
} else {
/* mixer output... finished */
rayo_component_send_complete(context->component, OUTPUT_FINISH);
}
/* TODO timed out */
/* cleanup internals */
switch_safe_free(context->ssml);
context->ssml = NULL;
if (output->document) {
iks_delete(output->document);
output->document = NULL;
}
/* close SSML file */
if (switch_test_flag((&context->fh), SWITCH_FILE_OPEN)) {
return switch_core_file_close(&context->fh);
}
}
return SWITCH_STATUS_SUCCESS;
}
/**
* Read from SSML document
* @param handle
* @param data
* @param len
* @return
*/
static switch_status_t rayo_file_read(switch_file_handle_t *handle, void *data, size_t *len)
{
switch_status_t status;
struct rayo_file_context *context = (struct rayo_file_context *)handle->private_info;
size_t llen = *len;
if (OUTPUT_COMPONENT(context->component)->stop) {
return SWITCH_STATUS_FALSE;
} else {
status = switch_core_file_read(&context->fh, data, len);
if (status != SWITCH_STATUS_SUCCESS) {
if ((status = next_file(handle)) != SWITCH_STATUS_SUCCESS) {
return status;
}
*len = llen;
status = switch_core_file_read(&context->fh, data, len);
}
}
return status;
}
/**
* Seek file
*/
static switch_status_t rayo_file_seek(switch_file_handle_t *handle, unsigned int *cur_sample, int64_t samples, int whence)
{
struct rayo_file_context *context = handle->private_info;
if (samples == 0 && whence == SWITCH_SEEK_SET) {
/* restart from beginning */
context->cur_doc = NULL;
context->play_count = 0;
return next_file(handle);
}
if (!handle->seekable) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_WARNING, "File is not seekable\n");
return SWITCH_STATUS_NOTIMPL;
}
return switch_core_file_seek(&context->fh, cur_sample, samples, whence);
}
/**
* Manages access to fileman controls
*/
struct {
/** synchronizes access to fileman hash */
switch_mutex_t *mutex;
/** fileman mapped by id */
switch_hash_t *hash;
} fileman_globals;
#define FILE_STARTBYTES 1024 * 32
#define FILE_BLOCKSIZE 1024 * 8
#define FILE_BUFSIZE 1024 * 64
/**
* Fileman playback state
*/
struct fileman_file_context {
/** handle to current file */
switch_file_handle_t fh;
/** file buffer */
int16_t *abuf;
/** end of file */
int eof;
/** maximum size of a packet in 2-byte samples */
switch_size_t max_frame_len;
/** optional session UUID */
const char *uuid;
/** fileman control ID */
const char *id;
/** done flag */
int done;
};
/**
* Wraps file with interface that can be controlled by fileman flags
* @param handle
* @param path the file to play
* @return SWITCH_STATUS_SUCCESS if opened
*/
static switch_status_t fileman_file_open(switch_file_handle_t *handle, const char *path)
{
int start_offset_ms = 0;
switch_status_t status = SWITCH_STATUS_FALSE;
struct fileman_file_context *context = switch_core_alloc(handle->memory_pool, sizeof(*context));
handle->private_info = context;
if (handle->params) {
const char *id = switch_event_get_header(handle->params, "id");
const char *uuid = switch_event_get_header(handle->params, "session");
const char *start_offset_ms_str = switch_event_get_header(handle->params, "start_offset_ms");
if (!zstr(id)) {
context->id = switch_core_strdup(handle->memory_pool, id);
}
if (!zstr(uuid)) {
context->uuid = switch_core_strdup(handle->memory_pool, uuid);
}
if (!zstr(start_offset_ms_str) && switch_is_number(start_offset_ms_str)) {
start_offset_ms = atoi(start_offset_ms_str);
if (start_offset_ms < 0) {
start_offset_ms = 0;
}
}
}
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(context->uuid), SWITCH_LOG_DEBUG, "Got path %s\n", path);
if ((status = switch_core_file_open(&context->fh, path, handle->channels, handle->samplerate, handle->flags, NULL)) != SWITCH_STATUS_SUCCESS) {
return status;
}
/* set up handle for external control */
if (!context->id) {
/* use filename as ID */
context->id = switch_core_strdup(handle->memory_pool, path);
}
switch_mutex_lock(fileman_globals.mutex);
if (!switch_core_hash_find(fileman_globals.hash, context->id)) {
switch_core_hash_insert(fileman_globals.hash, context->id, handle);
} else {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(context->uuid), SWITCH_LOG_WARNING, "Duplicate fileman ID: %s\n", context->id);
return SWITCH_STATUS_FALSE;
}
switch_mutex_unlock(fileman_globals.mutex);
context->max_frame_len = (handle->samplerate / 1000 * SWITCH_MAX_INTERVAL);
switch_zmalloc(context->abuf, FILE_STARTBYTES * sizeof(*context->abuf));
if (!context->fh.audio_buffer) {
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(context->uuid), SWITCH_LOG_DEBUG, "Create audio buffer\n");
switch_buffer_create_dynamic(&context->fh.audio_buffer, FILE_BLOCKSIZE, FILE_BUFSIZE, 0);
switch_assert(context->fh.audio_buffer);
}
handle->samples = context->fh.samples;
handle->format = context->fh.format;
handle->sections = context->fh.sections;
handle->seekable = context->fh.seekable;
handle->speed = context->fh.speed;
handle->vol = context->fh.vol;
handle->offset_pos = context->fh.offset_pos;
handle->interval = context->fh.interval;
if (switch_test_flag((&context->fh), SWITCH_FILE_NATIVE)) {
switch_set_flag_locked(handle, SWITCH_FILE_NATIVE);
} else {
switch_clear_flag_locked(handle, SWITCH_FILE_NATIVE);
}
if (handle->params && switch_true(switch_event_get_header(handle->params, "pause"))) {
switch_set_flag_locked(handle, SWITCH_FILE_PAUSE);
}
if (handle->seekable && start_offset_ms) {
unsigned int pos = 0;
int32_t target = start_offset_ms * (handle->samplerate / 1000);
switch_log_printf(SWITCH_CHANNEL_UUID_LOG(context->uuid), SWITCH_LOG_DEBUG, "seek to position %d\n", target);
switch_core_file_seek(&context->fh, &pos, target, SEEK_SET);
}
return status;
}
/**
* Close file.
* @param handle
* @return SWITCH_STATUS_SUCCESS
*/
static switch_status_t fileman_file_close(switch_file_handle_t *handle)
{
struct fileman_file_context *context = (struct fileman_file_context *)handle->private_info;
switch_file_handle_t *fh = &context->fh;
if (context->id) {
switch_mutex_lock(fileman_globals.mutex);
switch_core_hash_delete(fileman_globals.hash, context->id);
switch_mutex_unlock(fileman_globals.mutex);
}
if (switch_test_flag(fh, SWITCH_FILE_OPEN)) {
free(context->abuf);
if (fh->audio_buffer) {
switch_buffer_destroy(&fh->audio_buffer);
}
if (fh->sp_audio_buffer) {
switch_buffer_destroy(&fh->sp_audio_buffer);
}
return switch_core_file_close(fh);
}
return SWITCH_STATUS_SUCCESS;
}
/**
* Write to file
* @param handle
* @param data
* @param len
* @return
*/
static switch_status_t fileman_file_write(switch_file_handle_t *handle, void *data, size_t *len)
{
struct fileman_file_context *context = (struct fileman_file_context *)handle->private_info;
switch_file_handle_t *fh = &context->fh;
if (!switch_test_flag(handle, SWITCH_FILE_PAUSE)) {
return switch_core_file_write(fh, data, len);
}
return SWITCH_STATUS_SUCCESS;
}
/**
* Read from file
* @param handle
* @param data
* @param len
* @return
*/
static switch_status_t fileman_file_read(switch_file_handle_t *handle, void *data, size_t *len)
{
struct fileman_file_context *context = (struct fileman_file_context *)handle->private_info;
switch_file_handle_t *fh = &context->fh;
switch_status_t status = SWITCH_STATUS_SUCCESS;
switch_size_t o_len = 0;
/* anything called "_len" is measured in 2-byte samples */
if (switch_test_flag(fh, SWITCH_FILE_NATIVE)) {
return switch_core_file_read(fh, data, len);
}
//switch_log_printf(SWITCH_CHANNEL_UUID_LOG(context->uuid), SWITCH_LOG_DEBUG, "len = %"SWITCH_SIZE_T_FMT"\n", *len);
if (*len > context->max_frame_len) {
*len = context->max_frame_len;
}
for (;;) {
int do_speed = 1;
size_t read_bytes = 0;
if (context->done) {
/* done with this file */
status = SWITCH_STATUS_FALSE;
goto done;
} else if (switch_test_flag(handle, SWITCH_FILE_PAUSE)) {
//switch_log_printf(SWITCH_CHANNEL_UUID_LOG(context->uuid), SWITCH_LOG_DEBUG, "Read pause frame\n");
memset(context->abuf, 255, *len * 2);
do_speed = 0;
o_len = *len;
} else if (fh->sp_audio_buffer && (context->eof || (switch_buffer_inuse(fh->sp_audio_buffer) > (switch_size_t) (*len * 2)))) {
//switch_log_printf(SWITCH_CHANNEL_UUID_LOG(context->uuid), SWITCH_LOG_DEBUG, "Read speed frame\n");
/* get next speed frame */
if (!(read_bytes = switch_buffer_read(fh->sp_audio_buffer, context->abuf, *len * 2))) {
/* This is the reverse of what happens in switch_ivr_play_file... i think that implementation is wrong */
if (context->eof) {