@@ -455,9 +455,31 @@ class CallStore extends EventEmitter {
455
455
}
456
456
457
457
async startGroupCall ( chatId ) {
458
- let { currentGroupCall } = this ;
459
- LOG_CALL ( 'startGroupCall' , currentGroupCall ) ;
460
- if ( currentGroupCall ) {
458
+ let { currentCall, currentGroupCall } = this ;
459
+ LOG_CALL ( 'startGroupCall' , currentCall , currentGroupCall ) ;
460
+ if ( currentCall ) {
461
+ const { callId } = currentCall ;
462
+ const call = this . p2pGet ( callId ) ;
463
+ if ( call ) {
464
+ showAlert ( {
465
+ title : LStore . getString ( 'VoipOngoingAlertTitle' ) ,
466
+ message : LStore . replaceTags ( LStore . formatString ( 'VoipOngoingAlert2' , getUserFullName ( call . user_id , null ) , getChatTitle ( chatId ) ) ) ,
467
+ ok : LStore . getString ( 'OK' ) ,
468
+ cancel : LStore . getString ( 'Cancel' ) ,
469
+ onResult : async result => {
470
+ if ( result ) {
471
+ currentCall = this . currentCall ;
472
+ if ( currentCall ) {
473
+ await this . p2pHangUp ( currentCall . callId , true ) ;
474
+ }
475
+ await this . startGroupCallInternal ( chatId ) ;
476
+ }
477
+ }
478
+ } ) ;
479
+ }
480
+
481
+ return ;
482
+ } else if ( currentGroupCall ) {
461
483
const { chatId : oldChatId } = currentGroupCall ;
462
484
463
485
showAlert ( {
@@ -504,7 +526,7 @@ class CallStore extends EventEmitter {
504
526
TdLibController . send ( { '@type' : 'getGroupCall' , group_call_id : groupCallId } ) ;
505
527
}
506
528
507
- let { currentGroupCall } = this ;
529
+ let { currentGroupCall, currentCall } = this ;
508
530
let streamManager = null ;
509
531
try {
510
532
if ( rejoin ) {
@@ -529,27 +551,53 @@ class CallStore extends EventEmitter {
529
551
}
530
552
if ( ! streamManager || ! streamManager . inputStream ) return ;
531
553
532
- LOG_CALL ( 'joinGroupCall has another' , groupCallId , currentGroupCall ) ;
533
- if ( currentGroupCall && ! rejoin ) {
534
- const { chatId : oldChatId } = currentGroupCall ;
554
+ LOG_CALL ( 'joinGroupCall has another' , groupCallId , currentCall , currentGroupCall ) ;
555
+ if ( ! rejoin ) {
556
+ if ( currentCall ) {
557
+ const { callId } = currentCall ;
558
+ const call = this . p2pGet ( callId ) ;
559
+ if ( call ) {
560
+ const { user_id } = call ;
561
+
562
+ showAlert ( {
563
+ title : LStore . getString ( 'VoipOngoingAlertTitle' ) ,
564
+ message : LStore . replaceTags ( LStore . formatString ( 'VoipOngoingAlert2' , getUserFullName ( user_id , null ) , getChatTitle ( chatId ) ) ) ,
565
+ ok : LStore . getString ( 'OK' ) ,
566
+ cancel : LStore . getString ( 'Cancel' ) ,
567
+ onResult : async result => {
568
+ if ( result ) {
569
+ currentCall = this . currentCall ;
570
+ if ( currentCall ) {
571
+ this . p2pHangUp ( currentCall . callId , true ) ;
572
+ }
573
+ this . joinGroupCallInternal ( chatId , groupCallId , streamManager , muted , rejoin ) ;
574
+ }
575
+ }
576
+ } ) ;
535
577
536
- showAlert ( {
537
- title : LStore . getString ( 'VoipOngoingChatAlertTitle' ) ,
538
- message : LStore . replaceTags ( LStore . formatString ( 'VoipOngoingChatAlert' , getChatTitle ( oldChatId ) , getChatTitle ( chatId ) ) ) ,
539
- ok : LStore . getString ( 'OK' ) ,
540
- cancel : LStore . getString ( 'Cancel' ) ,
541
- onResult : async result => {
542
- if ( result ) {
543
- currentGroupCall = this . currentGroupCall ;
544
- if ( currentGroupCall ) {
545
- this . hangUp ( currentGroupCall . groupCallId ) ;
578
+ return ;
579
+ }
580
+ } else if ( currentGroupCall ) {
581
+ const { chatId : oldChatId } = currentGroupCall ;
582
+
583
+ showAlert ( {
584
+ title : LStore . getString ( 'VoipOngoingChatAlertTitle' ) ,
585
+ message : LStore . replaceTags ( LStore . formatString ( 'VoipOngoingChatAlert' , getChatTitle ( oldChatId ) , getChatTitle ( chatId ) ) ) ,
586
+ ok : LStore . getString ( 'OK' ) ,
587
+ cancel : LStore . getString ( 'Cancel' ) ,
588
+ onResult : async result => {
589
+ if ( result ) {
590
+ currentGroupCall = this . currentGroupCall ;
591
+ if ( currentGroupCall ) {
592
+ this . hangUp ( currentGroupCall . groupCallId ) ;
593
+ }
594
+ this . joinGroupCallInternal ( chatId , groupCallId , streamManager , muted , rejoin ) ;
546
595
}
547
- this . joinGroupCallInternal ( chatId , groupCallId , streamManager , muted , rejoin ) ;
548
596
}
549
- }
550
- } ) ;
597
+ } ) ;
551
598
552
- return ;
599
+ return ;
600
+ }
553
601
}
554
602
555
603
await this . joinGroupCallInternal ( chatId , groupCallId , streamManager , muted , rejoin ) ;
@@ -1439,23 +1487,65 @@ class CallStore extends EventEmitter {
1439
1487
} ;
1440
1488
}
1441
1489
1442
- p2pStartCall ( userId , isVideo ) {
1490
+ async p2pStartCall ( userId , isVideo ) {
1443
1491
LOG_P2P_CALL ( 'p2pStartCall' , userId , isVideo ) ;
1444
1492
1493
+ let { currentCall, currentGroupCall } = this ;
1494
+ if ( currentCall ) {
1495
+ const { callId } = currentCall ;
1496
+ const call = this . p2pGet ( callId ) ;
1497
+ if ( call ) {
1498
+ showAlert ( {
1499
+ title : LStore . getString ( 'VoipOngoingAlertTitle' ) ,
1500
+ message : LStore . replaceTags ( LStore . formatString ( 'VoipOngoingAlert' , getUserFullName ( call . user_id , null ) , getUserFullName ( userId , null ) ) ) ,
1501
+ ok : LStore . getString ( 'OK' ) ,
1502
+ cancel : LStore . getString ( 'Cancel' ) ,
1503
+ onResult : async result => {
1504
+ if ( result ) {
1505
+ currentCall = this . currentCall ;
1506
+ if ( currentCall ) {
1507
+ await this . p2pHangUp ( currentCall . callId , true ) ;
1508
+ }
1509
+ await this . p2pStartCallInternal ( userId , isVideo ) ;
1510
+ }
1511
+ }
1512
+ } ) ;
1513
+ }
1514
+
1515
+ return ;
1516
+ } else if ( currentGroupCall ) {
1517
+ const { chatId : oldChatId } = currentGroupCall ;
1518
+
1519
+ showAlert ( {
1520
+ title : LStore . getString ( 'VoipOngoingChatAlertTitle' ) ,
1521
+ message : LStore . replaceTags ( LStore . formatString ( 'VoipOngoingChatAlert2' , getChatTitle ( oldChatId ) , getUserFullName ( userId , null ) ) ) ,
1522
+ ok : LStore . getString ( 'OK' ) ,
1523
+ cancel : LStore . getString ( 'Cancel' ) ,
1524
+ onResult : async result => {
1525
+ if ( result ) {
1526
+ currentGroupCall = this . currentGroupCall ;
1527
+ if ( currentGroupCall ) {
1528
+ await this . hangUp ( currentGroupCall . groupCallId ) ;
1529
+ }
1530
+ await this . p2pStartCallInternal ( userId , isVideo ) ;
1531
+ }
1532
+ }
1533
+ } ) ;
1534
+
1535
+ return ;
1536
+ }
1537
+
1538
+ await this . p2pStartCallInternal ( userId , isVideo ) ;
1539
+ }
1540
+
1541
+ p2pStartCallInternal ( userId , isVideo ) {
1542
+ LOG_P2P_CALL ( 'p2pStartCallInternal' , userId , isVideo ) ;
1543
+
1445
1544
const fullInfo = UserStore . getFullInfo ( userId ) ;
1446
1545
if ( ! fullInfo ) return ;
1447
1546
1448
1547
const { can_be_called, has_private_calls, supports_video_calls } = fullInfo ;
1449
- LOG_P2P_CALL ( 'p2pStartCall fullInfo' , fullInfo , can_be_called , has_private_calls , supports_video_calls ) ;
1450
- // if (!can_be_called || has_private_calls) {
1451
- // showAlert({
1452
- // title: LStore.getString('VoipFailed'),
1453
- // message: LStore.replaceTags(LStore.formatString('CallNotAvailable', getUserFullName(userId, null))),
1454
- // // LStore.getString('CallNotAvailable'), getUserFullName(userId, null)),
1455
- // ok: LStore.getString('OK')
1456
- // });
1457
- // return;
1458
- // }
1548
+ LOG_P2P_CALL ( 'p2pStartCallInternal fullInfo' , fullInfo , can_be_called , has_private_calls , supports_video_calls ) ;
1459
1549
1460
1550
const protocol = this . p2pGetProtocol ( ) ;
1461
1551
LOG_P2P_CALL ( '[tdlib] createCall' , userId , protocol , isVideo , supports_video_calls ) ;
@@ -1470,6 +1560,60 @@ class CallStore extends EventEmitter {
1470
1560
p2pAcceptCall ( callId ) {
1471
1561
LOG_P2P_CALL ( 'p2pAcceptCall' , callId ) ;
1472
1562
1563
+ const call = this . p2pGet ( callId ) ;
1564
+ if ( ! call ) return ;
1565
+
1566
+ let { currentCall, currentGroupCall } = this ;
1567
+ if ( currentCall ) {
1568
+ const { callId : prevCallId } = currentCall ;
1569
+ const prevCall = this . p2pGet ( prevCallId ) ;
1570
+ if ( prevCall ) {
1571
+ showAlert ( {
1572
+ title : LStore . getString ( 'VoipOngoingAlertTitle' ) ,
1573
+ message : LStore . replaceTags ( LStore . formatString ( 'VoipOngoingAlert' , getUserFullName ( prevCall . user_id , null ) , getUserFullName ( call . user_id , null ) ) ) ,
1574
+ ok : LStore . getString ( 'OK' ) ,
1575
+ cancel : LStore . getString ( 'Cancel' ) ,
1576
+ onResult : async result => {
1577
+ if ( result ) {
1578
+ currentCall = this . currentCall ;
1579
+ if ( currentCall ) {
1580
+ await this . p2pHangUp ( currentCall . callId , true ) ;
1581
+ }
1582
+ await this . p2pAcceptCallInternal ( callId ) ;
1583
+ }
1584
+ }
1585
+ } ) ;
1586
+
1587
+ return ;
1588
+ }
1589
+ } else if ( currentGroupCall ) {
1590
+ const { chatId : oldChatId } = currentGroupCall ;
1591
+
1592
+ showAlert ( {
1593
+ title : LStore . getString ( 'VoipOngoingChatAlertTitle' ) ,
1594
+ message : LStore . replaceTags ( LStore . formatString ( 'VoipOngoingChatAlert2' , getChatTitle ( oldChatId ) , getUserFullName ( call . user_id , null ) ) ) ,
1595
+ ok : LStore . getString ( 'OK' ) ,
1596
+ cancel : LStore . getString ( 'Cancel' ) ,
1597
+ onResult : async result => {
1598
+ if ( result ) {
1599
+ currentGroupCall = this . currentGroupCall ;
1600
+ if ( currentGroupCall ) {
1601
+ await this . hangUp ( currentGroupCall . groupCallId ) ;
1602
+ }
1603
+ await this . p2pAcceptCallInternal ( callId ) ;
1604
+ }
1605
+ }
1606
+ } ) ;
1607
+
1608
+ return ;
1609
+ }
1610
+
1611
+ this . p2pAcceptCallInternal ( callId ) ;
1612
+ }
1613
+
1614
+ async p2pAcceptCallInternal ( callId ) {
1615
+ LOG_P2P_CALL ( 'p2pAcceptCallInternal' , callId ) ;
1616
+
1473
1617
const protocol = this . p2pGetProtocol ( ) ;
1474
1618
LOG_P2P_CALL ( '[tdlib] acceptCall' , callId , protocol ) ;
1475
1619
TdLibController . send ( {
@@ -1479,20 +1623,6 @@ class CallStore extends EventEmitter {
1479
1623
} ) ;
1480
1624
}
1481
1625
1482
- p2pDiscardCall ( callId , isDisconnected , duration , isVideo , connectionId ) {
1483
- LOG_P2P_CALL ( 'p2pDiscardCall' , callId ) ;
1484
-
1485
- LOG_P2P_CALL ( '[tdlib] discardCall' , callId , isDisconnected , duration , isVideo , connectionId ) ;
1486
- TdLibController . send ( {
1487
- '@type' : 'discardCall' ,
1488
- call_id : callId ,
1489
- is_disconnected : isDisconnected ,
1490
- duration,
1491
- is_video : isVideo ,
1492
- connection_id : connectionId
1493
- } ) ;
1494
- }
1495
-
1496
1626
p2pSendCallSignalingData ( callId , data ) {
1497
1627
LOG_P2P_CALL ( '[tdlib] sendCallSignalingData' , callId , data ) ;
1498
1628
TdLibController . send ( {
@@ -1554,6 +1684,7 @@ class CallStore extends EventEmitter {
1554
1684
1555
1685
async p2pJoinCall ( callId ) {
1556
1686
LOG_P2P_CALL ( 'p2pJoinCall' , callId ) ;
1687
+
1557
1688
const call = this . p2pGet ( callId ) ;
1558
1689
if ( ! call ) return ;
1559
1690
@@ -1590,6 +1721,13 @@ class CallStore extends EventEmitter {
1590
1721
if ( video ) {
1591
1722
video . srcObject = outputStream ;
1592
1723
}
1724
+
1725
+ track . onmute = ( ) => {
1726
+ LOG_P2P_CALL ( '[track] onmute' , track ) ;
1727
+ } ;
1728
+ track . onunmute = ( ) => {
1729
+ LOG_P2P_CALL ( '[track] onunmute' , track ) ;
1730
+ } ;
1593
1731
} ;
1594
1732
1595
1733
this . currentCall = {
@@ -1758,19 +1896,27 @@ class CallStore extends EventEmitter {
1758
1896
// this.p2pSendCallSignalingData(callId, JSON.stringify({ type: 'answer', data: answer }));
1759
1897
}
1760
1898
1761
- p2pHangUp ( callId ) {
1762
- LOG_P2P_CALL ( 'hangUp' , callId ) ;
1899
+ p2pHangUp ( callId , discard = false ) {
1763
1900
const { currentCall } = this ;
1764
- if ( ! currentCall ) return ;
1765
- if ( currentCall . callId !== callId ) return ;
1766
-
1767
- const call = this . get ( callId ) ;
1768
- if ( call ) return ;
1901
+ LOG_P2P_CALL ( 'hangUp' , callId , currentCall ) ;
1902
+ if ( currentCall && currentCall . callId === callId ) {
1903
+ const { connection, inputStream, outputStream, screenStream } = currentCall ;
1904
+ this . p2pCloseConnectionAndStream ( connection , inputStream , outputStream , screenStream ) ;
1769
1905
1770
- const { connection , inputStream , outputStream , screenStream } = currentCall ;
1771
- this . p2pCloseConnectionAndStream ( connection , inputStream , outputStream , screenStream ) ;
1906
+ this . currentCall = null ;
1907
+ }
1772
1908
1773
- this . currentCall = null ;
1909
+ if ( discard ) {
1910
+ LOG_P2P_CALL ( '[tdlib] discardCall' , callId ) ;
1911
+ TdLibController . send ( {
1912
+ '@type' : 'discardCall' ,
1913
+ call_id : callId ,
1914
+ is_disconnected : false ,
1915
+ duration : 0 ,
1916
+ is_video : false ,
1917
+ connection_id : 0
1918
+ } ) ;
1919
+ }
1774
1920
}
1775
1921
1776
1922
async p2pStartScreenSharing ( ) {
0 commit comments