-
Notifications
You must be signed in to change notification settings - Fork 319
/
channel.c
1422 lines (1213 loc) · 35.3 KB
/
channel.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
/*
Copyright (C) 2009 - 2010
Artem Makhutov <[email protected]>
http://www.makhutov.org
Dmitry Vagin <[email protected]>
bg <[email protected]>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /* HAVE_CONFIG_H */
#include <asterisk.h>
#include <asterisk/dsp.h> /* ast_dsp_digitreset() */
#include <asterisk/pbx.h> /* pbx_builtin_setvar_helper() */
#include <asterisk/module.h> /* ast_module_ref() ast_module_info = shit */
#include <asterisk/causes.h> /* AST_CAUSE_INCOMPATIBLE_DESTINATION AST_CAUSE_FACILITY_NOT_IMPLEMENTED AST_CAUSE_REQUESTED_CHAN_UNAVAIL */
#include <asterisk/musiconhold.h> /* ast_moh_start() ast_moh_stop() */
#include <asterisk/lock.h> /* AST_MUTEX_DEFINE_STATIC */
#include <asterisk/timing.h> /* ast_timer_fd() ast_timer_set_rate() ast_timer_ack() */
#include <asterisk/version.h> /* ASTERISK_VERSION_NUM */
#include "channel.h"
#include "chan_dongle.h"
#include "at_command.h"
#include "helpers.h" /* get_at_clir_value() */
#include "at_queue.h" /* write_all() TODO: move out */
#include "manager.h" /* manager_event_call_state_change() */
static char silence_frame[FRAME_SIZE];
#/* */
static int parse_dial_string(char * dialstr, const char** number, int * opts)
{
char* options;
char* dest_num;
int lopts = 0;
options = strchr (dialstr, '/');
if (!options)
{
ast_log (LOG_WARNING, "Can't determine destination in chan_dongle\n");
return AST_CAUSE_INCOMPATIBLE_DESTINATION;
}
*options++ = '\0';
dest_num = strchr(options, ':');
if(!dest_num)
{
dest_num = options;
}
else
{
*dest_num++ = '\0';
if (!strcasecmp(options, "holdother"))
lopts = CALL_FLAG_HOLD_OTHER;
else if (!strcasecmp(options, "conference"))
lopts = CALL_FLAG_HOLD_OTHER | CALL_FLAG_CONFERENCE;
else
{
ast_log (LOG_WARNING, "Invalid options in chan_dongle\n");
return AST_CAUSE_INCOMPATIBLE_DESTINATION;
}
}
if (*dest_num == '\0')
{
ast_log (LOG_WARNING, "Empty destination in chan_dongle\n");
return AST_CAUSE_INCOMPATIBLE_DESTINATION;
}
if (!is_valid_phone_number(dest_num))
{
ast_log (LOG_WARNING, "Invalid destination '%s' in chan_dongle, only 0123456789*#+ABC allowed\n", dest_num);
return AST_CAUSE_INCOMPATIBLE_DESTINATION;
}
*number = dest_num;
*opts = lopts;
return 0;
}
#/* */
EXPORT_DEF int channels_loop(struct pvt * pvt, const struct ast_channel * requestor)
{
/* not allow hold requester channel :) */
/* FIXME: requestor may be just proxy/masquerade for real channel */
// use ast_bridged_channel(chan) ?
// use requestor->tech->get_base_channel() ?
if(requestor && requestor->tech == &channel_tech && requestor->tech_pvt && ((struct cpvt*)requestor->tech_pvt)->pvt == pvt)
return 1;
return 0;
}
#if ASTERISK_VERSION_NUM >= 10800
// TODO: simplify by move common code to functions
static struct ast_channel * channel_request (attribute_unused const char * type, format_t format, const struct ast_channel * requestor, void * data, int * cause)
#else /* #if ASTERISK_VERSION_NUM >= 10800 */
/* TODO: add check when request 'holdother' what requestor is not on same device for 1.6 */
static struct ast_channel * channel_request (attribute_unused const char * type, int format, void * data, int * cause)
#endif /* #if ASTERISK_VERSION_NUM >= 10800 */
{
#if ASTERISK_VERSION_NUM >= 10800
format_t oldformat;
#else
int oldformat;
const struct ast_channel * requestor = NULL;
#endif
char * dest_dev;
const char * dest_num;
struct ast_channel * channel = NULL;
struct pvt * pvt;
int opts = CALL_FLAG_NONE;
int exists;
if (!data)
{
ast_log (LOG_WARNING, "Channel requested with no data\n");
*cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
return NULL;
}
oldformat = format;
format &= AST_FORMAT_SLINEAR;
if (!format)
{
#if ASTERISK_VERSION_NUM >= 10800
ast_log (LOG_WARNING, "Asked to get a channel of unsupported format '%s'\n", ast_getformatname (oldformat));
#else
ast_log (LOG_WARNING, "Asked to get a channel of unsupported format '%d'\n", oldformat);
#endif
*cause = AST_CAUSE_FACILITY_NOT_IMPLEMENTED;
return NULL;
}
dest_dev = ast_strdupa (data);
*cause = parse_dial_string(dest_dev, &dest_num, &opts);
if(*cause)
return NULL;
#if ASTERISK_VERSION_NUM >= 10800
pvt = find_device_by_resource(dest_dev, opts, requestor, &exists);
#else
pvt = find_device_by_resource(dest_dev, opts, NULL, &exists);
#endif
if(pvt)
{
channel = new_channel (pvt, AST_STATE_DOWN, NULL, pvt_get_pseudo_call_idx(pvt), CALL_DIR_OUTGOING, CALL_STATE_INIT, NULL, requestor);
ast_mutex_unlock (&pvt->lock);
if(!channel)
{
ast_log (LOG_WARNING, "Unable to allocate channel structure\n");
*cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
}
}
else
{
ast_log (LOG_WARNING, "[%s] Request to call on device %s\n", dest_dev, exists ? "which can not make call at this moment" : "not exists");
*cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
}
return channel;
}
#/* */
static int channel_call (struct ast_channel* channel, char* dest, attribute_unused int timeout)
{
struct cpvt* cpvt = channel->tech_pvt;
struct pvt* pvt;
char* dest_dev;
const char* dest_num;
int clir = 0;
int opts;
if(!cpvt || cpvt->channel != channel || !cpvt->pvt)
{
ast_log (LOG_WARNING, "call on unreferenced %s\n", channel->name);
return -1;
}
pvt = cpvt->pvt;
dest_dev = ast_strdupa (dest);
if(parse_dial_string(dest_dev, &dest_num, &opts))
return -1;
if ((channel->_state != AST_STATE_DOWN) && (channel->_state != AST_STATE_RESERVED))
{
ast_log (LOG_WARNING, "channel_call called on %s, neither down nor reserved\n", channel->name);
return -1;
}
ast_mutex_lock (&pvt->lock);
// FIXME: check if bridged on same device with CALL_FLAG_HOLD_OTHER
if (!ready4voice_call(pvt, cpvt, opts))
{
ast_mutex_unlock (&pvt->lock);
ast_log (LOG_ERROR, "[%s] Error device already in use or uninitialized\n", PVT_ID(pvt));
return -1;
}
CPVT_SET_FLAGS(cpvt, opts);
ast_debug (1, "[%s] Calling %s on %s\n", PVT_ID(pvt), dest, channel->name);
if (CONF_SHARED(pvt, usecallingpres))
{
if (CONF_SHARED(pvt, callingpres) < 0)
{
#if ASTERISK_VERSION_NUM >= 10800
clir = channel->connected.id.number.presentation;
#else
clir = channel->cid.cid_pres;
#endif
}
else
{
clir = CONF_SHARED(pvt, callingpres);
}
clir = get_at_clir_value (pvt, clir);
}
else
{
clir = -1;
}
PVT_STAT(pvt, out_calls) ++;
if (at_enque_dial (cpvt, dest_num, clir))
{
ast_mutex_unlock (&pvt->lock);
ast_log (LOG_ERROR, "[%s] Error sending ATD command\n", PVT_ID(pvt));
return -1;
}
ast_mutex_unlock (&pvt->lock);
return 0;
}
#/* ARCH: move to cpvt level */
static void disactivate_call(struct cpvt* cpvt)
{
if(cpvt->channel && CPVT_TEST_FLAG(cpvt, CALL_FLAG_ACTIVATED))
{
mixb_detach(&cpvt->pvt->a_write_mixb, &cpvt->mixstream);
ast_channel_set_fd (cpvt->channel, 1, -1);
ast_channel_set_fd (cpvt->channel, 0, -1);
CPVT_RESET_FLAGS(cpvt, CALL_FLAG_ACTIVATED | CALL_FLAG_MASTER);
ast_debug (6, "[%s] call idx %d disactivated\n", PVT_ID(cpvt->pvt), cpvt->call_idx);
}
}
#/* ARCH: move to cpvt level */
static void activate_call(struct cpvt* cpvt)
{
struct cpvt* cpvt2;
struct pvt* pvt;
/* nothing todo, already main */
if(CPVT_TEST_FLAG(cpvt, CALL_FLAG_MASTER))
return;
/* drop any other from MASTER, any set pipe for actives */
pvt = cpvt->pvt;
AST_LIST_TRAVERSE(&pvt->chans, cpvt2, entry)
{
if(cpvt2 != cpvt)
{
if(CPVT_TEST_FLAG(cpvt, CALL_FLAG_MASTER))
{
ast_debug (6, "[%s] call idx %d gave master\n", PVT_ID(pvt), cpvt2->call_idx);
}
CPVT_RESET_FLAGS(cpvt2, CALL_FLAG_MASTER);
if(cpvt2->channel)
{
ast_channel_set_fd (cpvt2->channel, 1, -1);
if(CPVT_TEST_FLAG(cpvt, CALL_FLAG_ACTIVATED))
{
ast_channel_set_fd (cpvt2->channel, 0, cpvt2->rd_pipe[PIPE_READ]);
ast_debug (6, "[%s] call idx %d still active fd %d\n", PVT_ID(pvt), cpvt2->call_idx, cpvt2->rd_pipe[PIPE_READ]);
}
}
}
}
/* setup call local write possition */
if(!CPVT_TEST_FLAG(cpvt, CALL_FLAG_ACTIVATED))
{
// FIXME: reset possition?
mixb_attach(&pvt->a_write_mixb, &cpvt->mixstream);
// rb_init (&cpvt->a_write_rb, cpvt->a_write_buf, sizeof (cpvt->a_write_buf));
// cpvt->write = pvt->a_write_rb.write;
// cpvt->used = pvt->a_write_rb.used;
}
if (pvt->audio_fd >= 0)
{
CPVT_SET_FLAGS(cpvt, CALL_FLAG_ACTIVATED | CALL_FLAG_MASTER);
if(cpvt->channel)
{
ast_channel_set_fd (cpvt->channel, 0, pvt->audio_fd);
if (pvt->a_timer)
{
ast_channel_set_fd (cpvt->channel, 1, ast_timer_fd (pvt->a_timer));
ast_timer_set_rate (pvt->a_timer, 50);
/* ast_debug (3, "[%s] Timer set\n", PVT_ID(pvt));
*/
}
}
if(pvt->dsp)
ast_dsp_digitreset(pvt->dsp);
pvt->dtmf_digit = 0;
ast_debug (6, "[%s] call idx %d was master\n", PVT_ID(pvt), cpvt->call_idx);
}
}
#/* we has 2 case of call this function, when local side want terminate call and when called for cleanup after remote side alreay terminate call, CEND received and cpvt destroyed */
static int channel_hangup (struct ast_channel* channel)
{
struct cpvt* cpvt = channel->tech_pvt;
struct pvt* pvt;
/* its possible call with channel w/o tech_pvt */
if(cpvt && cpvt->channel == channel && cpvt->pvt)
{
pvt = cpvt->pvt;
ast_mutex_lock (&pvt->lock);
ast_debug (1, "[%s] Hanging up call idx %d need hangup %d\n", PVT_ID(pvt), cpvt->call_idx, CPVT_TEST_FLAG(cpvt, CALL_FLAG_NEED_HANGUP) ? 1 : 0);
if (CPVT_TEST_FLAG(cpvt, CALL_FLAG_NEED_HANGUP))
{
if (at_enque_hangup (cpvt, cpvt->call_idx))
ast_log (LOG_ERROR, "[%s] Error adding AT+CHUP command to queue, call not terminated!\n", PVT_ID(pvt));
else
CPVT_RESET_FLAGS(cpvt, CALL_FLAG_NEED_HANGUP);
}
disactivate_call (cpvt);
/* drop cpvt->channel reference */
cpvt->channel = NULL;
ast_mutex_unlock (&pvt->lock);
}
/* drop channel -> cpvt reference */
channel->tech_pvt = NULL;
ast_module_unref (self_module());
ast_setstate (channel, AST_STATE_DOWN);
return 0;
}
#/* */
static int channel_answer (struct ast_channel* channel)
{
struct cpvt* cpvt = channel->tech_pvt;
struct pvt* pvt;
if(!cpvt || cpvt->channel != channel || !cpvt->pvt)
{
ast_log (LOG_WARNING, "call on unreferenced %s\n", channel->name);
return 0;
}
pvt = cpvt->pvt;
ast_mutex_lock (&pvt->lock);
if (cpvt->dir == CALL_DIR_INCOMING)
{
if (at_enque_answer (cpvt))
{
ast_log (LOG_ERROR, "[%s] Error sending answer commands\n", PVT_ID(pvt));
}
}
ast_mutex_unlock (&pvt->lock);
return 0;
}
#/* */
static int channel_digit_begin (struct ast_channel* channel, char digit)
{
struct cpvt* cpvt = channel->tech_pvt;
struct pvt* pvt;
int rv;
if(!cpvt || cpvt->channel != channel || !cpvt->pvt)
{
ast_log (LOG_WARNING, "call on unreferenced %s\n", channel->name);
return -1;
}
pvt = cpvt->pvt;
ast_mutex_lock (&pvt->lock);
rv = at_enque_dtmf (cpvt, digit);
if (rv)
{
ast_mutex_unlock (&pvt->lock);
if(rv == -1974)
ast_log (LOG_WARNING, "[%s] Sending DTMF %c not supported by dongle. Tell Asterisk to generate inband\n", PVT_ID(pvt), digit);
else
ast_log (LOG_ERROR, "[%s] Error adding DTMF %c command to queue\n", PVT_ID(pvt), digit);
return -1;
}
ast_mutex_unlock (&pvt->lock);
ast_debug (3, "[%s] Send DTMF %c\n", PVT_ID(pvt), digit);
return 0;
}
#/* */
static int channel_digit_end (attribute_unused struct ast_channel* channel, attribute_unused char digit, attribute_unused unsigned int duration)
{
return 0;
}
#/* ARCH: move to cpvt level */
static void iov_write(struct pvt* pvt, int fd, struct iovec * iov, int iovcnt)
{
ssize_t written;
ssize_t done = 0;
int count = 10;
while(iovcnt)
{
again:
written = writev (fd, iov, iovcnt);
if(written < 0)
{
if((errno == EINTR || errno == EAGAIN))
{
--count;
if(count != 0)
goto again;
ast_debug (1, "[%s] Deadlock avoided for write!\n", PVT_ID(pvt));
}
break;
}
else
{
done += written;
count = 10;
do
{
if((size_t)written >= iov->iov_len)
{
written -= iov->iov_len;
iovcnt--;
iov++;
}
else
{
iov->iov_len -= written;
goto again;
}
} while(written > 0);
}
}
PVT_STAT(pvt, a_write_bytes) += done;
if (done != FRAME_SIZE)
{
ast_debug (1, "[%s] Write error!\n", PVT_ID(pvt));
}
}
#/* */
static void timing_write (struct pvt* pvt)
{
size_t used;
int iovcnt;
struct iovec iov[3];
const char* msg = NULL;
// char buffer[FRAME_SIZE];
// struct cpvt* cpvt;
// ast_debug (6, "[%s] tm write |\n", PVT_ID(pvt));
// memset(buffer, 0, sizeof(buffer));
// AST_LIST_TRAVERSE(&pvt->chans, cpvt, entry) {
// if(!CPVT_IS_ACTIVE(cpvt))
// continue;
used = mixb_used (&pvt->a_write_mixb);
// used = rb_used (&cpvt->a_write_rb);
if (used >= FRAME_SIZE)
{
iovcnt = mixb_read_n_iov (&pvt->a_write_mixb, iov, FRAME_SIZE);
mixb_read_n_iov (&pvt->a_write_mixb, iov, FRAME_SIZE);
mixb_read_upd (&pvt->a_write_mixb, FRAME_SIZE);
}
else if (used > 0)
{
PVT_STAT(pvt, write_tframes) ++;
msg = "[%s] write truncated frame\n";
iovcnt = mixb_read_all_iov (&pvt->a_write_mixb, iov);
mixb_read_all_iov (&pvt->a_write_mixb, iov);
mixb_read_upd (&pvt->a_write_mixb, used);
iov[iovcnt].iov_base = silence_frame;
iov[iovcnt].iov_len = FRAME_SIZE - used;
iovcnt++;
}
else
{
PVT_STAT(pvt, write_sframes) ++;
msg = "[%s] write silence\n";
iov[0].iov_base = silence_frame;
iov[0].iov_len = FRAME_SIZE;
iovcnt = 1;
// continue;
}
// iov_add(buffer, sizeof(buffer), iov);
if(msg)
ast_debug (7, msg, PVT_ID(pvt));
// }
PVT_STAT(pvt, write_frames) ++;
iov_write(pvt, pvt->audio_fd, iov, iovcnt);
// if(write_all(pvt->audio_fd, buffer, sizeof(buffer)) != sizeof(buffer))
// ast_debug (1, "[%s] Write error!\n", PVT_ID(pvt));
}
#/* copy voice data from device to each channel in conference */
static void write_conference(struct pvt * pvt, const char * buffer, size_t length)
{
struct cpvt* cpvt;
size_t wr;
AST_LIST_TRAVERSE(&pvt->chans, cpvt, entry) {
if(CPVT_IS_ACTIVE(cpvt) && !CPVT_IS_MASTER(cpvt) && CPVT_TEST_FLAG(cpvt, CALL_FLAG_MULTIPARTY) && cpvt->rd_pipe[PIPE_WRITE] >= 0)
{
wr = write_all(cpvt->rd_pipe[PIPE_WRITE], buffer, length);
// ast_debug (6, "[%s] write2 | call idx %d pipe fd %d wrote %d bytes\n", PVT_ID(pvt), cpvt->call_idx, cpvt->rd_pipe[PIPE_WRITE], wr);
if(wr != length)
{
ast_debug (1, "[%s] Pipe write error %d\n", PVT_ID(pvt), errno);
}
}
}
}
#if ASTERISK_VERSION_NUM >= 10800
#define subclass_codec subclass.codec
#define subclass_integer subclass.integer
#else
#define subclass_codec subclass
#define subclass_integer subclass
#endif
#/* */
static struct ast_frame* channel_read (struct ast_channel* channel)
{
struct cpvt* cpvt = channel->tech_pvt;
struct pvt* pvt;
struct ast_frame* f = &ast_null_frame;
ssize_t res;
if(!cpvt || cpvt->channel != channel || !cpvt->pvt)
{
return f;
}
pvt = cpvt->pvt;
while (ast_mutex_trylock (&pvt->lock))
{
CHANNEL_DEADLOCK_AVOIDANCE (channel);
}
ast_debug (7, "[%s] read call idx %d state %d audio_fd %d\n", PVT_ID(pvt), cpvt->call_idx, cpvt->state, pvt->audio_fd);
/* FIXME: move down for enable timing_write() to device ? */
if (!CPVT_IS_SOUND_SOURCE(cpvt) || pvt->audio_fd < 0)
{
goto e_return;
}
if (pvt->a_timer && channel->fdno == 1)
{
ast_timer_ack (pvt->a_timer, 1);
timing_write (pvt);
ast_debug (7, "[%s] *** timing ***\n", PVT_ID(pvt));
}
else
{
memset (&cpvt->a_read_frame, 0, sizeof (cpvt->a_read_frame));
cpvt->a_read_frame.frametype = AST_FRAME_VOICE;
cpvt->a_read_frame.subclass_codec= AST_FORMAT_SLINEAR;
cpvt->a_read_frame.data.ptr = cpvt->a_read_buf + AST_FRIENDLY_OFFSET;
cpvt->a_read_frame.offset = AST_FRIENDLY_OFFSET;
cpvt->a_read_frame.src = AST_MODULE;
res = read (CPVT_IS_MASTER(cpvt) ? pvt->audio_fd : cpvt->rd_pipe[PIPE_READ], cpvt->a_read_frame.data.ptr, FRAME_SIZE);
if (res <= 0)
{
if (errno != EAGAIN && errno != EINTR)
{
ast_debug (1, "[%s] Read error %d, going to wait for new connection\n", PVT_ID(pvt), errno);
}
goto e_return;
}
/* ast_debug (7, "[%s] call idx %d read %u\n", PVT_ID(pvt), cpvt->call_idx, (unsigned)res);
ast_debug (6, "[%s] read | call idx %d fd %d readed %d bytes\n", PVT_ID(pvt), cpvt->call_idx, pvt->audio_fd, res);
*/
if(CPVT_IS_MASTER(cpvt))
{
if(CPVT_TEST_FLAG(cpvt, CALL_FLAG_MULTIPARTY))
write_conference(pvt, cpvt->a_read_frame.data.ptr, res);
PVT_STAT(pvt, a_read_bytes) += res;
PVT_STAT(pvt, read_frames) ++;
if(res < FRAME_SIZE)
PVT_STAT(pvt, read_sframes) ++;
}
cpvt->a_read_frame.samples = res / 2;
cpvt->a_read_frame.datalen = res;
ast_frame_byteswap_le (&cpvt->a_read_frame);
/*
cpvt->a_read_frame.ts;
cpvt->a_read_frame.len;
cpvt->a_read_frame.seqno;
*/
f = &cpvt->a_read_frame;
if (pvt->dsp)
{
f = ast_dsp_process (channel, pvt->dsp, f);
if ((f->frametype == AST_FRAME_DTMF_END) || (f->frametype == AST_FRAME_DTMF_BEGIN))
{
if ((f->subclass_integer == 'm') || (f->subclass_integer == 'u'))
{
f->frametype = AST_FRAME_NULL;
f->subclass_integer = 0;
goto e_return;
}
if(f->frametype == AST_FRAME_DTMF_BEGIN)
{
pvt->dtmf_begin_time = ast_tvnow();
}
else if (f->frametype == AST_FRAME_DTMF_END)
{
if(!ast_tvzero(pvt->dtmf_begin_time) && ast_tvdiff_ms(ast_tvnow(), pvt->dtmf_begin_time) < CONF_SHARED(pvt, mindtmfgap))
{
ast_debug(1, "[%s] DTMF char %c ignored min gap %d > %ld\n", PVT_ID(pvt), f->subclass_integer, CONF_SHARED(pvt, mindtmfgap), (long)ast_tvdiff_ms(ast_tvnow(), pvt->dtmf_begin_time));
f->frametype = AST_FRAME_NULL;
f->subclass_integer = 0;
}
else if(f->len < CONF_SHARED(pvt, mindtmfduration))
{
ast_debug(1, "[%s] DTMF char %c ignored min duration %d > %ld\n", PVT_ID(pvt), f->subclass_integer, CONF_SHARED(pvt, mindtmfduration), f->len);
f->frametype = AST_FRAME_NULL;
f->subclass_integer = 0;
}
else if(f->subclass_integer == pvt->dtmf_digit
&&
!ast_tvzero(pvt->dtmf_end_time)
&&
ast_tvdiff_ms(ast_tvnow(), pvt->dtmf_end_time) < CONF_SHARED(pvt, mindtmfinterval))
{
ast_debug(1, "[%s] DTMF char %c ignored min interval %d > %ld\n", PVT_ID(pvt), f->subclass_integer, CONF_SHARED(pvt, mindtmfinterval), (long)ast_tvdiff_ms(ast_tvnow(), pvt->dtmf_end_time));
f->frametype = AST_FRAME_NULL;
f->subclass_integer = 0;
}
else
{
ast_debug(1, "[%s] Got DTMF char %c\n",PVT_ID(pvt), f->subclass_integer);
pvt->dtmf_digit = f->subclass_integer;
pvt->dtmf_end_time = ast_tvnow();
}
}
goto e_return;
}
}
if (CONF_SHARED(pvt, rxgain) && f->frametype == AST_FRAME_VOICE)
{
if (ast_frame_adjust_volume (f, CONF_SHARED(pvt, rxgain)) == -1)
{
ast_debug (1, "[%s] Volume could not be adjusted!\n", PVT_ID(pvt));
}
}
}
e_return:
ast_mutex_unlock (&pvt->lock);
return f;
}
#/* */
static int channel_write (struct ast_channel* channel, struct ast_frame* f)
{
struct cpvt* cpvt = channel->tech_pvt;
struct pvt* pvt;
size_t count;
int gains[2];
if (f->frametype != AST_FRAME_VOICE || f->subclass_codec != AST_FORMAT_SLINEAR)
{
return 0;
}
if(!cpvt || cpvt->channel != channel || !cpvt->pvt)
{
ast_log (LOG_WARNING, "call on unreferenced %s\n", channel->name);
return 0;
}
/* TODO: write silence better ? */
/* TODO: check end of bridge loop condition */
/* never write to same device from other channel its possible for call hold or conference */
if(CPVT_TEST_FLAG(cpvt, CALL_FLAG_BRIDGE_LOOP))
return 0;
pvt = cpvt->pvt;
ast_debug (7, "[%s] write call idx %d state %d\n", PVT_ID(pvt), cpvt->call_idx, cpvt->state);
while (ast_mutex_trylock (&pvt->lock))
{
CHANNEL_DEADLOCK_AVOIDANCE (channel);
}
if(!CPVT_IS_ACTIVE(cpvt))
goto e_return;
if(CPVT_TEST_FLAG(cpvt, CALL_FLAG_MULTIPARTY) && !CPVT_TEST_FLAG(cpvt, CALL_FLAG_BRIDGE_CHECK))
{
struct ast_channel* bridged = ast_bridged_channel(channel);
CPVT_SET_FLAGS(cpvt, CALL_FLAG_BRIDGE_CHECK);
if(bridged && bridged->tech == &channel_tech && bridged->tech_pvt && ((struct cpvt*)bridged->tech_pvt)->pvt == pvt)
{
CPVT_SET_FLAGS(cpvt, CALL_FLAG_BRIDGE_LOOP);
CPVT_SET_FLAGS((struct cpvt*)bridged->tech_pvt, CALL_FLAG_BRIDGE_LOOP);
ast_log (LOG_WARNING, "[%s] Bridged channels %s and %s working on same device, discard writes to avoid voice loop\n", PVT_ID(pvt), channel->name, bridged->name);
goto e_return;
}
}
if (pvt->audio_fd < 0)
{
ast_debug (1, "[%s] audio_fd not ready\n", PVT_ID(pvt));
}
else
{
if(f->datalen)
{
/** try to minimize of ast_frame_adjust_volume() calls:
* one hand we must obey txgain but with other divide gain to
* number of mixed channels. In some cases one call of ast_frame_adjust_volume() enough
*/
gains[1] = mixb_streams(&pvt->a_write_mixb);
if(gains[1] < 1 || pvt->a_timer == NULL)
gains[1] = 1;
gains[0] = CONF_SHARED(pvt, txgain);
if(gains[0] <= -2)
{
gains[0] *= gains[1];
gains[1] = 0;
}
else if(gains[0] <= 1)
{
gains[0] = - gains[1];
gains[1] = 0;
}
else if(gains[0] % gains[1] == 0)
{
gains[0] /= gains[1];
gains[1] = 0;
}
for(count = 0; count < ITEMS_OF(gains); ++count)
{
if(gains[count] > 1 || gains[count] < -1)
if(ast_frame_adjust_volume (f, gains[count]) == -1)
{
ast_debug (1, "[%s] Volume could not be adjusted!\n", PVT_ID(pvt));
}
}
}
ast_frame_byteswap_le(f);
if (pvt->a_timer)
{
count = mixb_free (&pvt->a_write_mixb, &cpvt->mixstream);
if (count < (size_t) f->datalen)
{
mixb_read_upd (&pvt->a_write_mixb, f->datalen - count);
PVT_STAT(pvt, write_rb_overflow_bytes) += f->datalen - count;
PVT_STAT(pvt, write_rb_overflow) ++;
}
mixb_write (&pvt->a_write_mixb, &cpvt->mixstream, f->data.ptr, f->datalen);
/*
ast_debug (6, "[%s] write | call idx %d, %d bytes lwrite %d lused %d write %d used %d\n", PVT_ID(pvt), cpvt->call_idx, f->datalen, cpvt->write, cpvt->used, pvt->a_write_rb.write, pvt->a_write_rb.used);
rb_tetris(&pvt->a_write_rb, f->data.ptr, f->datalen, &cpvt->write, &cpvt->used);
ast_debug (6, "[%s] write | lwrite %d lused %d write %d used %d\n", PVT_ID(pvt), cpvt->write, cpvt->used, pvt->a_write_rb.write, pvt->a_write_rb.used);
*/
}
else
{
if(mixb_streams(&pvt->a_write_mixb) != 1)
{
ast_log (LOG_ERROR, "[%s] write conference without timer\n", PVT_ID(pvt));
goto e_return;
}
{
int iovcnt;
struct iovec iov[2];
ast_frame_byteswap_le (f);
iov[0].iov_base = f->data.ptr;
iov[0].iov_len = FRAME_SIZE;
if (f->datalen < FRAME_SIZE)
{
iov[0].iov_len = f->datalen;
iov[1].iov_base = silence_frame;
iov[1].iov_len = FRAME_SIZE - f->datalen;
iovcnt = 2;
PVT_STAT(pvt, write_tframes) ++;
}
else
{
iovcnt = 1;
}
iov_write(pvt, pvt->audio_fd, iov, iovcnt);
PVT_STAT(pvt, write_frames) ++;
}
}
/* if (f->datalen != 320)
*/
{
ast_debug (7, "[%s] Write frame: samples = %d, data lenght = %d byte\n", PVT_ID(pvt), f->samples, f->datalen);
}
}
e_return:
ast_mutex_unlock (&pvt->lock);
return 0;
}
#undef subclass_integer
#undef subclass_codec
#/* */
static int channel_fixup (struct ast_channel* oldchannel, struct ast_channel* newchannel)
{
struct cpvt * cpvt = newchannel->tech_pvt;
struct pvt* pvt;
if (!cpvt || !cpvt->pvt)
{
ast_log (LOG_WARNING, "call on unreferenced %s\n", newchannel->name);
return -1;
}
pvt = cpvt->pvt;
ast_mutex_lock (&pvt->lock);
if (cpvt->channel == oldchannel)
{
cpvt->channel = newchannel;
}
ast_mutex_unlock (&pvt->lock);
return 0;
}
#/* FIXME: must modify in conjuction with state on call not whole device? */
static int channel_devicestate (void* data)
{
char* device;
struct pvt* pvt;
int res = AST_DEVICE_INVALID;
device = ast_strdupa (data ? data : "");
ast_debug (1, "Checking device state for device %s\n", device);
pvt = find_device_ext (device, NULL);
if (pvt)
{
if (pvt->connected)
{
if (is_dial_possible(pvt, CALL_FLAG_NONE))
{
res = AST_DEVICE_NOT_INUSE;
}
else
{
res = AST_DEVICE_INUSE;
}
}
ast_mutex_unlock (&pvt->lock);
}
return res;
}
#/* */
static int channel_indicate (struct ast_channel* channel, int condition, const void* data, attribute_unused size_t datalen)
{
int res = 0;
ast_debug (1, "[%s] Requested indication %d\n", channel->name, condition);
switch (condition)
{
case AST_CONTROL_BUSY:
case AST_CONTROL_CONGESTION:
case AST_CONTROL_RINGING:
case -1:
res = -1;
break;
/* appears in r295843 */
#ifdef HAVE_AST_CONTROL_SRCCHANGE
case AST_CONTROL_SRCCHANGE:
#endif
case AST_CONTROL_PROGRESS:
case AST_CONTROL_PROCEEDING:
case AST_CONTROL_VIDUPDATE:
case AST_CONTROL_SRCUPDATE:
break;
case AST_CONTROL_HOLD:
ast_moh_start (channel, data, NULL);
break;
case AST_CONTROL_UNHOLD:
ast_moh_stop (channel);
break;
default:
ast_log (LOG_WARNING, "[%s] Don't know how to indicate condition %d\n", channel->name, condition);
res = -1;
break;
}
return res;
}
/* ARCH: move to cpvt */
/* FIXME: protection for cpvt->channel if exists */
#/* NOTE: called from device level with locked pvt */
EXPORT_DEF void change_channel_state(struct cpvt * cpvt, unsigned newstate, int cause)
{
struct ast_channel * channel;
struct pvt* pvt;
call_state_t oldstate = cpvt->state;
short call_idx;