forked from Blup1980/microsip_42
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainDlg.cpp
4575 lines (4213 loc) · 123 KB
/
mainDlg.cpp
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) 2011-2020 MicroSIP (http://www.microsip.org)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define THIS_FILENAME "mainDlg.cpp"
#include "mainDlg.h"
#include "microsip.h"
#include "Mmsystem.h"
#include "settings.h"
#include "global.h"
#include "ModelessMessageBox.h"
#include "utf.h"
#include "json.h"
#include "XMLFile.h"
#include "Markup.h"
#include "langpack.h"
#include "jumplist.h"
#include "atlenc.h"
#include <io.h>
#include <afxmt.h>
#include <afxinet.h>
#include <ws2tcpip.h>
#include <Dbt.h>
#include <Strsafe.h>
#include <locale.h>
#include <Wtsapi32.h>
#include <afxsock.h>
#include "atlrx.h"
using namespace MSIP;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CmainDlg *mainDlg;
static UINT WM_SHELLHOOKMESSAGE;
static UINT BASED_CODE indicators[] =
{
IDS_STATUSBAR,
IDS_STATUSBAR2,
};
static bool timerContactBlinkState = false;
CCriticalSection gethostbyaddrThreadCS;
static CString gethostbyaddrThreadResult;
static DWORD WINAPI gethostbyaddrThread(LPVOID lpParam)
{
CString *addr = (CString *)lpParam;
CString res = *addr;
delete addr;
struct hostent *he = NULL;
struct in_addr inaddr;
inaddr.S_un.S_addr = inet_addr(CStringA(res));
if (inaddr.S_un.S_addr != INADDR_NONE && inaddr.S_un.S_addr != INADDR_ANY) {
he = gethostbyaddr((char *)&inaddr, 4, AF_INET);
if (he) {
res = he->h_name;
}
}
gethostbyaddrThreadCS.Lock();
gethostbyaddrThreadResult = res;
gethostbyaddrThreadCS.Unlock();
return 0;
}
static void on_reg_state2(pjsua_acc_id acc_id, pjsua_reg_info *info)
{
if (!IsWindow(mainDlg->m_hWnd)) {
return;
}
CString *str = NULL;
PostMessage(mainDlg->m_hWnd, UM_ON_REG_STATE2, (WPARAM)info->cbparam->code, (LPARAM)str);
}
LRESULT CmainDlg::onRegState2(WPARAM wParam, LPARAM lParam)
{
int code = wParam;
if (code == 200) {
if (accountSettings.usersDirectory.Find(_T("%s")) != -1 || accountSettings.usersDirectory.Find(_T("{")) != -1) {
UsersDirectoryLoad();
}
}
UpdateWindowText(_T(""), IDI_DEFAULT, true);
return 0;
}
/* Callback from timer when the maximum call duration has been
* exceeded.
*/
static void call_timeout_callback(pj_timer_heap_t *timer_heap,
struct pj_timer_entry *entry)
{
pjsua_call_id call_id = entry->id;
pjsua_msg_data msg_data_;
pjsip_generic_string_hdr warn;
pj_str_t hname = pj_str("Warning");
pj_str_t hvalue = pj_str("399 localhost \"Call duration exceeded\"");
PJ_UNUSED_ARG(timer_heap);
if (call_id == PJSUA_INVALID_ID) {
PJ_LOG(1, (THIS_FILENAME, "Invalid call ID in timer callback"));
return;
}
/* Add warning header */
pjsua_msg_data_init(&msg_data_);
pjsip_generic_string_hdr_init2(&warn, &hname, &hvalue);
pj_list_push_back(&msg_data_.hdr_list, &warn);
/* Call duration has been exceeded; disconnect the call */
PJ_LOG(3, (THIS_FILENAME, "Duration (%d seconds) has been exceeded "
"for call %d, disconnecting the call",
accountSettings.autoHangUpTime, call_id));
entry->id = PJSUA_INVALID_ID;
pjsua_call_hangup(call_id, 200, NULL, &msg_data_);
}
static void on_call_state(pjsua_call_id call_id, pjsip_event *e)
{
if (!IsWindow(mainDlg->m_hWnd)) {
return;
}
pjsua_call_info *call_info = new pjsua_call_info();
if (pjsua_call_get_info(call_id, call_info) != PJ_SUCCESS || call_info->state == PJSIP_INV_STATE_NULL) {
return;
}
call_user_data *user_data = (call_user_data *)pjsua_call_get_user_data(call_info->id);
// reset user_data after call transfer
if (user_data) {
user_data->CS.Lock();
bool callIdMissmatch = user_data->call_id != PJSUA_INVALID_ID && user_data->call_id != call_info->id;
bool hidden = user_data->hidden;
user_data->CS.Unlock();
if (callIdMissmatch) {
pjsua_call_set_user_data(call_info->id, NULL);
user_data = NULL;
}
else {
if (hidden) {
if (call_info->state == PJSIP_INV_STATE_DISCONNECTED) {
delete user_data;
}
return;
}
}
}
if (!user_data) {
user_data = new call_user_data(call_info->id);
pjsua_call_set_user_data(call_info->id, user_data);
}
user_data->CS.Lock();
switch (call_info->state) {
case PJSIP_INV_STATE_CALLING:
msip_call_unhold(call_info);
break;
case PJSIP_INV_STATE_CONNECTING:
msip_call_unhold(call_info);
break;
case PJSIP_INV_STATE_CONFIRMED:
if (accountSettings.autoRecording) {
msip_call_recording_start(user_data, call_info);
}
if (accountSettings.autoHangUpTime > 0) {
/* Schedule timer to hangup call after the specified duration */
pj_time_val delay;
user_data->auto_hangup_timer.id = call_info->id;
user_data->auto_hangup_timer.cb = &call_timeout_callback;
delay.sec = accountSettings.autoHangUpTime;
delay.msec = 0;
pjsua_schedule_timer(&user_data->auto_hangup_timer, &delay);
}
break;
case PJSIP_INV_STATE_DISCONNECTED:
msip_call_recording_stop(user_data);
/* Cancel duration timer, if any */
if (user_data->auto_hangup_timer.id != PJSUA_INVALID_ID) {
pjsua_cancel_timer(&user_data->auto_hangup_timer);
user_data->auto_hangup_timer.id = PJSUA_INVALID_ID;
}
call_deinit_tonegen(call_info->id);
msip_conference_leave(call_info);
pjsua_call_set_user_data(call_info->id, NULL);
break;
}
user_data->CS.Unlock();
PostMessage(mainDlg->m_hWnd, UM_ON_CALL_STATE, (WPARAM)call_info, (LPARAM)user_data);
}
LRESULT CmainDlg::onCallState(WPARAM wParam, LPARAM lParam)
{
pjsua_call_info *call_info = (pjsua_call_info *)wParam;
call_user_data *user_data = (call_user_data *)lParam;
CString number = PjToStr(&call_info->remote_info, TRUE);
SIPURI sipuri;
ParseSIPURI(number, &sipuri);
user_data->CS.Lock();
CString *str = new CString();
CString adder;
if (call_info->state != PJSIP_INV_STATE_DISCONNECTED && call_info->state != PJSIP_INV_STATE_CONNECTING && call_info->remote_contact.slen > 0) {
SIPURI contactURI;
ParseSIPURI(PjToStr(&call_info->remote_contact, TRUE), &contactURI);
CString contactDomain = RemovePort(contactURI.domain);
struct hostent *he = NULL;
if (IsIP(contactDomain)) {
HANDLE hThread;
CString *addr = new CString(contactDomain);
if (addr) {
hThread = CreateThread(NULL, 0, gethostbyaddrThread, addr, 0, NULL);
if (WaitForSingleObject(hThread, 500) == 0) {
gethostbyaddrThreadCS.Lock();
contactDomain = gethostbyaddrThreadResult;
gethostbyaddrThreadCS.Unlock();
}
}
}
adder.AppendFormat(_T("%s, "), contactDomain);
}
unsigned cnt = 0;
unsigned cnt_srtp = 0;
switch (call_info->state) {
case PJSIP_INV_STATE_CALLING:
*str = Translate(_T("Calling"));
str->AppendFormat(_T(" %s "), !sipuri.user.IsEmpty() ? sipuri.user : sipuri.domain);
str->Append(_T("..."));
break;
case PJSIP_INV_STATE_INCOMING:
str->SetString(Translate(_T("Incoming Call")));
break;
case PJSIP_INV_STATE_EARLY:
str->SetString(Translate(PjToStr(&call_info->last_status_text).GetBuffer()));
break;
case PJSIP_INV_STATE_CONNECTING:
str->Format(_T("%s..."), Translate(_T("Connecting")));
break;
case PJSIP_INV_STATE_CONFIRMED:
str->SetString(Translate(_T("Connected")));
for (unsigned i = 0; i < call_info->media_cnt; i++) {
if (call_info->media[i].dir != PJMEDIA_DIR_NONE &&
(call_info->media[i].type == PJMEDIA_TYPE_AUDIO || call_info->media[i].type == PJMEDIA_TYPE_VIDEO)) {
cnt++;
pjsua_call_info call_info_stub;
if (pjsua_var.state == PJSUA_STATE_RUNNING && pjsua_call_get_info(call_info->id, &call_info_stub) == PJ_SUCCESS) {
pjsua_stream_info psi;
if (pjsua_call_get_stream_info(call_info->id, call_info->media[i].index, &psi) == PJ_SUCCESS) {
if (call_info->media[i].type == PJMEDIA_TYPE_AUDIO) {
adder.AppendFormat(_T("%s@%dkHz %dkbit/s%s, "),
PjToStr(&psi.info.aud.fmt.encoding_name), psi.info.aud.fmt.clock_rate / 1000,
psi.info.aud.param->info.avg_bps / 1000,
psi.info.aud.fmt.channel_cnt == 2 ? _T(" Stereo") : _T("")
);
}
else {
adder.AppendFormat(_T("%s %dkbit/s, "),
PjToStr(&psi.info.vid.codec_info.encoding_name),
psi.info.vid.codec_param->enc_fmt.det.vid.max_bps / 1000
);
}
}
pjmedia_transport_info t;
if (pjsua_call_get_med_transport_info(call_info->id, call_info->media[i].index, &t) == PJ_SUCCESS) {
for (unsigned j = 0; j < t.specific_info_cnt; j++) {
if (t.spc_info[j].buffer[0]) {
switch (t.spc_info[j].type) {
case PJMEDIA_TRANSPORT_TYPE_SRTP:
adder.Append(_T("SRTP, "));
cnt_srtp++;
break;
case PJMEDIA_TRANSPORT_TYPE_ICE:
adder.Append(_T("ICE, "));
break;
}
}
}
}
}
}
}
if (cnt_srtp && cnt == cnt_srtp) {
user_data->srtp = MSIP_SRTP;
}
else {
user_data->srtp = MSIP_SRTP_DISABLED;
}
break;
case PJSIP_INV_STATE_DISCONNECTED:
//--
if (call_info->last_status == 200) {
str->SetString(Translate(_T("Call Ended")));
}
else {
CString rab = PjToStr(&call_info->last_status_text);
if (rab.Find(_T("(PJ_ERESOLVE)")) != -1) {
rab = _T("Cannot get IP address of the called host.");
}
if (call_info->last_status == 487) {
str->SetString(Translate(_T("Cancel")));
}
else
if (call_info->acc_id && call_info->last_status >= 500 && call_info->last_status < 600) {
str->Format(_T("Server Failure: "));
str->AppendFormat(_T("%d %s"), call_info->last_status, Translate(rab.GetBuffer()));
}
else {
str->SetString(rab);
}
}
break;
}
if (!str->IsEmpty() && !adder.IsEmpty()) {
str->AppendFormat(_T(" (%s)"), adder.Left(adder.GetLength() - 2));
}
if (call_info->state == PJSIP_INV_STATE_CALLING) {
//--
if (!accountSettings.cmdOutgoingCall.IsEmpty()) {
CString params = sipuri.user;
RunCmd(accountSettings.cmdOutgoingCall, params);
}
//--
}
if (call_info->state == PJSIP_INV_STATE_CONFIRMED) {
PostMessage(WM_TIMER, IDT_TIMER_CALL, NULL);
SetTimer(IDT_TIMER_CALL, 1000, NULL);
if (call_info->role == PJSIP_ROLE_UAS) {
//--
if (!accountSettings.cmdCallAnswer.IsEmpty()) {
CString params = sipuri.user;
RunCmd(accountSettings.cmdCallAnswer, params);
}
//--
}
//--
if (!accountSettings.cmdCallStart.IsEmpty()) {
CString params = sipuri.user;
RunCmd(accountSettings.cmdCallStart, params);
}
//--
if (!user_data->commands.IsEmpty()) {
SetTimer((UINT_PTR)call_info->id, 1000, (TIMERPROC)DTMFQueueTimerHandler);
}
}
if (!accountSettings.singleMode) {
if (call_info->state != PJSIP_INV_STATE_CONFIRMED) {
if (call_info->state != PJSIP_INV_STATE_DISCONNECTED) {
UpdateWindowText(*str, call_info->role == PJSIP_ROLE_UAS ? IDI_CALL_IN : IDI_CALL_OUT);
}
else {
PostMessage(UM_UPDATEWINDOWTEXT, 0, 0);
}
}
}
if (call_info->role == PJSIP_ROLE_UAC) {
if (call_info->last_status == 180 && !call_info->media_cnt) {
if (toneCalls.IsEmpty()) {
PostMessage(WM_TIMER, IDT_TIMER_TONE, NULL);
SetTimer(IDT_TIMER_TONE, 4500, NULL);
toneCalls.AddTail(call_info->id);
}
else if (toneCalls.Find(call_info->id) == NULL) {
toneCalls.AddTail(call_info->id);
}
}
else {
POSITION position = toneCalls.Find(call_info->id);
if (position != NULL) {
toneCalls.RemoveAt(position);
if (toneCalls.IsEmpty()) {
KillTimer(IDT_TIMER_TONE);
PostMessage(UM_ON_PLAYER_STOP, 0, 0);
}
}
}
}
bool doNotShowMessagesWindow =
call_info->state == PJSIP_INV_STATE_INCOMING ||
call_info->state == PJSIP_INV_STATE_EARLY ||
call_info->state == PJSIP_INV_STATE_DISCONNECTED ||
accountSettings.singleMode ||
(accountSettings.silent && !mainDlg->IsWindowVisible());
if (user_data->autoAnswer) {
if (!accountSettings.bringToFrontOnIncoming) {
doNotShowMessagesWindow = true;
}
}
MessagesContact* messagesContact = messagesDlg->AddTab(number, _T(""),
(!accountSettings.singleMode &&
(call_info->state == PJSIP_INV_STATE_CONFIRMED
|| call_info->state == PJSIP_INV_STATE_CONNECTING)
)
||
(accountSettings.singleMode
&&
(
(call_info->role == PJSIP_ROLE_UAC && call_info->state != PJSIP_INV_STATE_DISCONNECTED)
||
(call_info->role == PJSIP_ROLE_UAS &&
(call_info->state == PJSIP_INV_STATE_CONFIRMED
|| call_info->state == PJSIP_INV_STATE_CONNECTING)
)
))
? TRUE : FALSE,
call_info, user_data, doNotShowMessagesWindow, call_info->state == PJSIP_INV_STATE_DISCONNECTED
);
if (call_info->state == PJSIP_INV_STATE_CONFIRMED) {
if (!accountSettings.singleMode && accountSettings.AC) {
messagesDlg->OnMergeAll();
}
}
if (call_info->state == PJSIP_INV_STATE_DISCONNECTED) {
CString number = sipuri.user;
if (call_info->last_status == 200) {
onPlayerPlay(MSIP_SOUND_HANGUP, 0);
}
else {
if (accountSettings.singleMode) {
if (call_info->last_status == 487 || (call_info->role == PJSIP_ROLE_UAS && (call_info->last_status == 486 || call_info->last_status == 600 || call_info->last_status == 603))) {
//don't show
}
else {
if (messagesContact) {
BaloonPopup(*str, messagesContact->name, NIIF_INFO);
}
else {
BaloonPopup(*str, number, NIIF_INFO);
}
}
}
}
if (call_info->last_status == 486 || call_info->last_status == 600 || call_info->last_status == 603) {
//--
if (!accountSettings.cmdCallBusy.IsEmpty()) {
CString params = number;
RunCmd(accountSettings.cmdCallBusy, params);
}
//--
}
else {
//--
if (!accountSettings.cmdCallEnd.IsEmpty()) {
CString params = number;
RunCmd(accountSettings.cmdCallEnd, params);
}
//--
}
if (call_info->role == PJSIP_ROLE_UAS && call_info->last_status == 487) {
//-- missed call
missed = true;
}
}
if (messagesContact) {
if (call_info->state == PJSIP_INV_STATE_DISCONNECTED) {
messagesContact->mediaStatus = PJSUA_CALL_MEDIA_ERROR;
}
if (call_info->role == PJSIP_ROLE_UAS) {
if (call_info->state == PJSIP_INV_STATE_CONFIRMED) {
pageCalls->Add(call_info->call_id, messagesContact->number + messagesContact->numberParameters, messagesContact->name, MSIP_CALL_IN);
}
else if (call_info->state == PJSIP_INV_STATE_INCOMING || call_info->state == PJSIP_INV_STATE_EARLY) {
pageCalls->Add(call_info->call_id, messagesContact->number + messagesContact->numberParameters, messagesContact->name, MSIP_CALL_MISS);
}
}
else {
if (call_info->state == PJSIP_INV_STATE_CALLING) {
pageCalls->Add(call_info->call_id, messagesContact->number + messagesContact->numberParameters, messagesContact->name, MSIP_CALL_OUT);
}
}
}
if (call_info->state == PJSIP_INV_STATE_DISCONNECTED) {
pageCalls->SetDuration(call_info->call_id, msip_get_duration(&call_info->connect_duration));
CString number = sipuri.user;
CString name = pageContacts->GetNameByNumber(GetSIPURI(sipuri.user, true));
if (name.IsEmpty()) {
name = !sipuri.name.IsEmpty() ? sipuri.name : (!sipuri.user.IsEmpty() ? sipuri.user : sipuri.domain);
}
pageCalls->Add(call_info->call_id, number, name, MSIP_CALL_MISS);
pageCalls->SetInfo(call_info->call_id, *str);
}
if (accountSettings.singleMode) {
if (call_info->state == PJSIP_INV_STATE_DISCONNECTED) {
pjsua_call_id current_call_id = CurrentCallId();
if (current_call_id == call_info->id || current_call_id == -1) {
pageDialer->Clear(false);
pageDialer->UpdateCallButton(FALSE, 0);
}
PostMessage(UM_UPDATEWINDOWTEXT, 0, 0);
}
else {
if (call_info->state != PJSIP_INV_STATE_CONFIRMED) {
UpdateWindowText(*str, call_info->role == PJSIP_ROLE_UAS ? IDI_CALL_IN : IDI_CALL_OUT);
}
int tabN = 0;
GotoTab(tabN);
messagesDlg->OnChangeTab(call_info, user_data);
}
}
else {
if (call_info->state == PJSIP_INV_STATE_DISCONNECTED) {
pageDialer->PostMessage(WM_COMMAND, MAKELPARAM(IDC_CLEAR, 0), 0);
}
}
if (messagesContact && !str->IsEmpty()) {
messagesDlg->AddMessage(messagesContact, *str, MSIP_MESSAGE_TYPE_SYSTEM,
call_info->state == PJSIP_INV_STATE_INCOMING || call_info->state == PJSIP_INV_STATE_EARLY
);
}
if (call_info->state == PJSIP_INV_STATE_DISCONNECTED) {
messagesDlg->OnEndCall(call_info);
if (pjsua_var.state != PJSUA_STATE_RUNNING || !pjsua_call_get_count()) {
#ifdef _GLOBAL_VIDEO
if (previewWin) {
previewWin->PostMessage(WM_CLOSE, NULL, NULL);
}
#endif
}
}
if (call_info->role == PJSIP_ROLE_UAS) {
if (call_info->state != PJSIP_INV_STATE_INCOMING && call_info->state != PJSIP_INV_STATE_EARLY) {
int count = ringinDlgs.GetCount();
if (!count) {
if (call_info->state != PJSIP_INV_STATE_DISCONNECTED || (call_info->state == PJSIP_INV_STATE_DISCONNECTED && call_info->connect_duration.sec == 0 && call_info->connect_duration.msec == 0)) {
PlayerStop();
}
}
else {
for (int i = 0; i < count; i++) {
RinginDlg *ringinDlg = ringinDlgs.GetAt(i);
if (call_info->id == ringinDlg->call_id) {
if (count == 1) {
PlayerStop();
}
ringinDlgs.RemoveAt(i);
ringinDlg->DestroyWindow();
break;
}
}
}
}
}
if (call_info->state != PJSIP_INV_STATE_INCOMING &&
call_info->state != PJSIP_INV_STATE_EARLY
) {
if (call_info->state != PJSIP_INV_STATE_DISCONNECTED) {
if (messagesContact) {
pageDialer->SetName(messagesContact->name);
}
}
else {
pageDialer->SetName();
}
}
user_data->CS.Unlock();
// --delete user data
if (call_info->state == PJSIP_INV_STATE_DISCONNECTED) {
if (user_data) {
delete user_data;
}
}
// --
delete call_info;
delete str;
bool hasCalls = call_get_count_noincoming();
if (pageDialer->IsChild(&pageDialer->m_ButtonRec)) {
pageDialer->m_ButtonRec.EnableWindow(hasCalls);
}
return 0;
}
static void on_call_media_state(pjsua_call_id call_id)
{
pjsua_call_info *call_info = new pjsua_call_info();
if (pjsua_call_get_info(call_id, call_info) != PJ_SUCCESS || call_info->state == PJSIP_INV_STATE_NULL) {
return;
}
call_user_data *user_data = (call_user_data *)pjsua_call_get_user_data(call_info->id);
if (call_info->media_status == PJSUA_CALL_MEDIA_ACTIVE
|| call_info->media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD
) {
msip_conference_join(call_info);
pjsua_conf_connect(call_info->conf_slot, 0);
pjsua_conf_connect(0, call_info->conf_slot);
//--
user_data->CS.Lock();
user_data->holdFrom = -1;
if (user_data->recorder_id != PJSUA_INVALID_ID) {
pjsua_conf_port_id rec_conf_port_id = pjsua_recorder_get_conf_port(user_data->recorder_id);
pjsua_conf_connect(call_info->conf_slot, rec_conf_port_id);
}
user_data->CS.Unlock();
//--
::SetTimer(mainDlg->pageDialer->m_hWnd, IDT_TIMER_VU_METER, 100, NULL);
//--
}
else {
msip_conference_leave(call_info, true);
pjsua_conf_disconnect(call_info->conf_slot, 0);
pjsua_conf_disconnect(0, call_info->conf_slot);
call_deinit_tonegen(call_id);
//--
user_data->CS.Lock();
user_data->holdFrom = msip_get_duration(&call_info->connect_duration);
user_data->CS.Unlock();
//--
}
PostMessage(mainDlg->m_hWnd, UM_ON_CALL_MEDIA_STATE, (WPARAM)call_info, (LPARAM)user_data);
}
LRESULT CmainDlg::onCallMediaState(WPARAM wParam, LPARAM lParam)
{
pjsua_call_info *call_info = (pjsua_call_info *)wParam;
call_user_data *user_data = (call_user_data *)lParam;
messagesDlg->UpdateHoldButton(call_info);
CString message;
CString number = PjToStr(&call_info->remote_info, TRUE);
MessagesContact* messagesContact = messagesDlg->AddTab(number, _T(""), FALSE, call_info, user_data, TRUE, TRUE);
if (messagesContact) {
if (call_info->media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD) {
message = _T("Call on Remote Hold");
}
if (call_info->media_status == PJSUA_CALL_MEDIA_LOCAL_HOLD) {
message = _T("Call on Local Hold");
}
if (call_info->media_status == PJSUA_CALL_MEDIA_NONE) {
message = _T("Call on Hold");
}
if (messagesContact->mediaStatus != PJSUA_CALL_MEDIA_ERROR && messagesContact->mediaStatus != call_info->media_status && call_info->media_status == PJSUA_CALL_MEDIA_ACTIVE) {
message = _T("Call is Active");
}
if (!message.IsEmpty()) {
messagesDlg->AddMessage(messagesContact, Translate(message.GetBuffer()), MSIP_MESSAGE_TYPE_SYSTEM, TRUE);
}
messagesContact->mediaStatus = call_info->media_status;
pageDialer->SetName();
}
if (call_info->media_status == PJSUA_CALL_MEDIA_ACTIVE
|| call_info->media_status == PJSUA_CALL_MEDIA_REMOTE_HOLD
) {
onRefreshLevels(0, 0);
}
delete call_info;
return 0;
}
static void on_call_media_event(pjsua_call_id call_id,
unsigned med_idx,
pjmedia_event *event)
{
char event_name[5];
PJ_LOG(5, (THIS_FILENAME, "Event %s",
pjmedia_fourcc_name(event->type, event_name)));
//#if PJSUA_HAS_VIDEO
//if (event->type == PJMEDIA_EVENT_FMT_CHANGED) {
// pjsua_call_info ci;
// pjsua_call_get_info(call_id, &ci);
// if ((ci.media[med_idx].type == PJMEDIA_TYPE_VIDEO) &&
// (ci.media[med_idx].dir & PJMEDIA_DIR_DECODING)) {
// pjsua_vid_win_id wid;
// pjmedia_rect_size size;
// pjsua_vid_win_info win_info;
// wid = ci.media[med_idx].stream.vid.win_in;
// pjsua_vid_win_get_info(wid, &win_info);
// size = event->data.fmt_changed.new_fmt.det.vid.size;
// if (size.w != win_info.size.w || size.h != win_info.size.h) {
// pjsua_vid_win_set_size(wid, &size);
// /* Re-arrange video windows */
// arrange_window(PJSUA_INVALID_ID);
// }
// }
//}
//#else
// PJ_UNUSED_ARG(call_id);
// PJ_UNUSED_ARG(med_idx);
// PJ_UNUSED_ARG(event);
//#endif
}
static void on_incoming_call(pjsua_acc_id acc, pjsua_call_id call_id,
pjsip_rx_data *rdata)
{
pjsua_call_info call_info;
pjsua_call_get_info(call_id, &call_info);
call_user_data *user_data = (call_user_data *)pjsua_call_get_user_data(call_info.id);
if (!user_data) {
user_data = new call_user_data(call_info.id);
pjsua_call_set_user_data(call_info.id, user_data);
}
user_data->CS.Lock();
SIPURI sipuri;
ParseSIPURI(PjToStr(&call_info.remote_info, TRUE), &sipuri);
user_data->name = sipuri.name;
if (accountSettings.forceCodec) {
pjsua_call *call;
pjsip_dialog *dlg;
pj_status_t status;
status = acquire_call("on_incoming_call()", call_id, &call, &dlg);
if (status == PJ_SUCCESS) {
pjmedia_sdp_neg_set_prefer_remote_codec_order(call->inv->neg, PJ_FALSE);
pjsip_dlg_dec_lock(dlg);
}
}
//--
if (!accountSettings.cmdIncomingCall.IsEmpty()) {
CString params = sipuri.user;
RunCmd(accountSettings.cmdIncomingCall, params);
}
//--
pjsua_call_id call_ids[PJSUA_MAX_CALLS];
unsigned calls_count = PJSUA_MAX_CALLS;
unsigned calls_count_cmp = 0;
if (pjsua_enum_calls(call_ids, &calls_count) == PJ_SUCCESS) {
for (unsigned i = 0; i < calls_count; ++i) {
pjsua_call_info call_info_curr;
if (pjsua_call_get_info(call_ids[i], &call_info_curr) == PJ_SUCCESS) {
SIPURI sipuri_curr;
ParseSIPURI(PjToStr(&call_info_curr.remote_info, TRUE), &sipuri_curr);
if (call_info_curr.id != call_info.id &&
sipuri.user + _T("@") + sipuri.domain == sipuri_curr.user + _T("@") + sipuri_curr.domain
) {
// 486 Busy Here
msip_call_busy(call_info.id);
goto return_on_incoming_call;
}
if (call_info_curr.state != PJSIP_INV_STATE_DISCONNECTED) {
calls_count_cmp++;
}
}
}
}
if ((!accountSettings.callWaiting && calls_count_cmp > 1) || (accountSettings.maxConcurrentCalls > 0 && calls_count_cmp > accountSettings.maxConcurrentCalls)) {
// 486 Busy Here
msip_call_busy(call_info.id);
goto return_on_incoming_call;
}
if (IsWindow(mainDlg->m_hWnd)) {
pjsip_generic_string_hdr *hsr;
// -- diversion
const pj_str_t headerDiversion = { "Diversion",9 };
hsr = (pjsip_generic_string_hdr*)pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &headerDiversion, NULL);
if (hsr) {
CString str = PjToStr(&hsr->hvalue, true);
SIPURI sipuriDiversion;
ParseSIPURI(str, &sipuriDiversion);
user_data->diversion = !sipuriDiversion.user.IsEmpty() ? sipuriDiversion.user : sipuriDiversion.domain;
}
// -- end diversion
// -- caller id
const pj_str_t headerCallerID = { "P-Asserted-Identity",19 };
hsr = (pjsip_generic_string_hdr*)pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &headerCallerID, NULL);
if (!hsr) {
const pj_str_t headerCallerID = { "Remote-Party-Id",15 };
hsr = (pjsip_generic_string_hdr*)pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &headerCallerID, NULL);
}
if (hsr) {
user_data->callerID = PjToStr(&hsr->hvalue, true);
}
// -- end caller id
if (!mainDlg->callIdIncomingIgnore.IsEmpty() && mainDlg->callIdIncomingIgnore == PjToStr(&call_info.call_id)) {
pjsua_call_answer(call_id, 487, NULL, NULL);
goto return_on_incoming_call;
}
bool reject = false;
CString reason;
if (accountSettings.denyIncoming == _T("all")) {
reject = true;
}
else if (accountSettings.denyIncoming == _T("button")) {
reject = accountSettings.DND;
reason = _T("Do Not Disturb");
}
else if (accountSettings.denyIncoming == _T("user")) {
SIPURI sipuri_curr;
ParseSIPURI(PjToStr(&call_info.local_info, TRUE), &sipuri_curr);
if (sipuri_curr.user != get_account_username()) {
reject = true;
}
}
else if (accountSettings.denyIncoming == _T("domain")) {
SIPURI sipuri_curr;
ParseSIPURI(PjToStr(&call_info.local_info, TRUE), &sipuri_curr);
if (accountSettings.accountId) {
if (sipuri_curr.domain != get_account_domain()) {
reject = true;
}
}
}
else if (accountSettings.denyIncoming == _T("rdomain")) {
if (accountSettings.accountId) {
if (sipuri.domain != get_account_domain()) {
reject = true;
}
}
}
else if (accountSettings.denyIncoming == _T("address")) {
SIPURI sipuri_curr;
ParseSIPURI(PjToStr(&call_info.local_info, TRUE), &sipuri_curr);
if (sipuri_curr.user != get_account_username() || (get_account_domain() != _T("") && sipuri_curr.domain != get_account_domain())) {
reject = true;
}
}
if (reject) {
if (reason.IsEmpty()) {
reason = _T("Denied");
}
msip_call_busy(call_info.id, reason);
goto return_on_incoming_call;
}
accountSettings.lastCallNumber = sipuri.user;
accountSettings.lastCallHasVideo = false;
bool autoAnswer = false;
int autoAnswerDelay = accountSettings.autoAnswerDelay;
bool noRingDialog = false;
if (accountSettings.autoAnswer == _T("all")) {
autoAnswer = true;
}
else if (accountSettings.autoAnswer == _T("button")) {
autoAnswer = accountSettings.AA;
}
else if (accountSettings.autoAnswer == _T("header")) {
//--
pjsip_generic_string_hdr *hsr = NULL;
const pj_str_t header = pj_str("X-AUTOANSWER");
hsr = (pjsip_generic_string_hdr*)pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &header, NULL);
if (hsr) {
CString autoAnswerValue = PjToStr(&hsr->hvalue, TRUE);
autoAnswerValue.MakeLower();
if (autoAnswerValue == _T("true") || autoAnswerValue == _T("1")) {
autoAnswer = true;
}
}
//--
if (!autoAnswer) {
pjsip_generic_string_hdr *hsr = NULL;
const pj_str_t header = pj_str("Call-Info");
hsr = (pjsip_generic_string_hdr*)pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &header, NULL);
if (hsr) {
CString callInfoValue = PjToStr(&hsr->hvalue, TRUE);
callInfoValue.MakeLower();
if (callInfoValue.Find(_T("auto answer")) != -1) {
autoAnswer = true;
}
else {
CAtlRegExp<> regex;
REParseError parseStatus = regex.Parse(_T("answer-after={[0-9]+}"), true);
if (parseStatus == REPARSE_ERROR_OK) {
CAtlREMatchContext<> mc;
if (regex.Match(callInfoValue, &mc) && mc.m_uNumGroups == 1) {
const CAtlREMatchContext<>::RECHAR* szStart = 0;
const CAtlREMatchContext<>::RECHAR* szEnd = 0;
mc.GetMatch(0, &szStart, &szEnd);
ptrdiff_t nLength = szEnd - szStart;
CStringA text(szStart, nLength);
autoAnswerDelay = atoi(text);
autoAnswer = true;
}
}
}
}
}
}
if (autoAnswer && autoAnswerDelay > 0) {
autoAnswer = false;
if (mainDlg->autoAnswerCallId == PJSUA_INVALID_ID) {
mainDlg->autoAnswerCallId = call_id;
mainDlg->SetTimer(IDT_TIMER_AUTOANSWER, autoAnswerDelay * 1000, NULL);
}
}
BOOL playBeep = FALSE;
if (autoAnswer
&& calls_count <= 1
) {
mainDlg->AutoAnswer(call_id);
}
else {
if (accountSettings.hidden) {
mainDlg->PostMessage(UM_CALL_HANGUP, (WPARAM)call_id, NULL);
}
else {
if (!noRingDialog) {
//--
const pj_str_t headerUserAgent = { "User-Agent",10 };
pjsip_generic_string_hdr *hsr = NULL;
hsr = (pjsip_generic_string_hdr*)pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &headerUserAgent, NULL);
if (hsr) {
user_data->userAgent = PjToStr(&hsr->hvalue, true);
}
//--
mainDlg->PostMessage(UM_CREATE_RINGING, (WPARAM)call_id, NULL);
}
pjsua_call_answer(call_id, 180, NULL, NULL);
if (call_get_count_noincoming()) {
playBeep = TRUE;
}
else {
if (!accountSettings.ringtone.GetLength()) {
mainDlg->PostMessage(UM_ON_PLAYER_PLAY, MSIP_SOUND_RINGTONE, 0);
}
else {
mainDlg->PostMessage(UM_ON_PLAYER_PLAY, MSIP_SOUND_CUSTOM, (LPARAM)&accountSettings.ringtone);
}
}
//--
if (!accountSettings.cmdCallRing.IsEmpty()) {
CString params = sipuri.user;
RunCmd(accountSettings.cmdCallRing, params);
}
//--
}
}
if (accountSettings.localDTMF && playBeep) {
mainDlg->PostMessage(UM_ON_PLAYER_PLAY, MSIP_SOUND_RINGIN2, 0);
}
}
return_on_incoming_call:
user_data->CS.Unlock();
}
static void on_nat_detect(const pj_stun_nat_detect_result *res)
{
if (res->status != PJ_SUCCESS) {
pjsua_perror(THIS_FILENAME, "NAT detection failed", res->status);
}
else {
if (res->nat_type == PJ_STUN_NAT_TYPE_SYMMETRIC) {