@@ -430,6 +430,9 @@ class TelnyxClient(
430
430
431
431
Handler (Looper .getMainLooper()).postDelayed(Runnable {
432
432
if (! ConnectivityHelper .isNetworkEnabled(context)) {
433
+ getActiveCalls().forEach { (_, call) ->
434
+ call.updateCallState(CallState .DROPPED )
435
+ }
433
436
socketResponseLiveData.postValue(SocketResponse .error(" No Network Connection" ))
434
437
} else {
435
438
// Network is switched here. Either from Wifi to LTE or vice-versa
@@ -448,10 +451,15 @@ class TelnyxClient(
448
451
private suspend fun reconnectToSocket () = withContext(Dispatchers .Default ) {
449
452
450
453
// Disconnect active calls for reconnection
451
- getActiveCalls()?.forEach { (_, call) ->
452
- call?.peerConnection?.disconnect()
454
+ getActiveCalls().forEach { (_, call) ->
455
+ webRTCReporter?.pauseStats()
456
+ call.peerConnection?.disconnect()
457
+ call.updateCallState(CallState .RECONNECTING )
453
458
}
454
459
460
+ // Delay for network to be properly established
461
+ delay(RECONNECT_DELAY )
462
+
455
463
// Create new socket connection
456
464
socketReconnection = TxSocket (
457
465
socket.host_address,
@@ -471,8 +479,6 @@ class TelnyxClient(
471
479
if (pushMetaData == null ) Config .TELNYX_PROD_HOST_ADDRESS
472
480
else
473
481
Config .TELNYX_PROD_HOST_ADDRESS
474
-
475
-
476
482
}
477
483
478
484
@@ -563,8 +569,7 @@ class TelnyxClient(
563
569
txPushMetaData : String? = null,
564
570
) {
565
571
566
- socketResponseLiveData =
567
- MutableLiveData <SocketResponse <ReceivedMessageBody >>(SocketResponse .initialised())
572
+ socketResponseLiveData.postValue(SocketResponse .initialised())
568
573
waitingForReg = true
569
574
invalidateGatewayResponseTimer()
570
575
resetGatewayCounters()
@@ -617,8 +622,7 @@ class TelnyxClient(
617
622
autoLogin : Boolean = true,
618
623
) {
619
624
620
- socketResponseLiveData =
621
- MutableLiveData <SocketResponse <ReceivedMessageBody >>(SocketResponse .initialised())
625
+ socketResponseLiveData.postValue(SocketResponse .initialised())
622
626
waitingForReg = true
623
627
invalidateGatewayResponseTimer()
624
628
resetGatewayCounters()
@@ -680,8 +684,7 @@ class TelnyxClient(
680
684
autoLogin : Boolean = true,
681
685
) {
682
686
683
- socketResponseLiveData =
684
- MutableLiveData <SocketResponse <ReceivedMessageBody >>(SocketResponse .initialised())
687
+ socketResponseLiveData.postValue(SocketResponse .initialised())
685
688
waitingForReg = true
686
689
invalidateGatewayResponseTimer()
687
690
resetGatewayCounters()
@@ -1641,18 +1644,20 @@ class TelnyxClient(
1641
1644
1642
1645
override fun onAttachReceived (jsonObject : JsonObject ) {
1643
1646
1644
- val attachCall = call!! .copy(
1647
+ val params = jsonObject.getAsJsonObject(" params" )
1648
+ val offerCallId = UUID .fromString(params.get(" callID" ).asString)
1649
+
1650
+ calls[offerCallId]?.copy(
1645
1651
context = context,
1646
1652
client = this ,
1647
1653
socket = socket,
1648
1654
sessionId = sessid,
1649
1655
audioManager = audioManager!! ,
1650
1656
providedTurn = providedTurn!! ,
1651
- providedStun = providedStun!!
1652
- ).apply {
1657
+ providedStun = providedStun!! ,
1658
+ mutableCallStateFlow = calls[offerCallId]!! .mutableCallStateFlow,
1659
+ )?.apply {
1653
1660
1654
- val params = jsonObject.getAsJsonObject(" params" )
1655
- val offerCallId = UUID .fromString(params.get(" callID" ).asString)
1656
1661
val remoteSdp = params.get(" sdp" ).asString
1657
1662
val voiceSdkID = jsonObject.getAsJsonPrimitive(" voice_sdk_id" )?.asString
1658
1663
if (voiceSdkID != null ) {
@@ -1672,11 +1677,11 @@ class TelnyxClient(
1672
1677
1673
1678
1674
1679
peerConnection = Peer (context, client, providedTurn, providedStun, offerCallId).also {
1675
- if (isDebug) {
1676
- webRTCReporter = WebRTCReporter (socket, callId, telnyxLegId?.toString(), it)
1677
- webRTCReporter?.startStats()
1678
- }
1679
- }
1680
+ if (isDebug) {
1681
+ webRTCReporter = WebRTCReporter (socket, callId, telnyxLegId?.toString(), it)
1682
+ webRTCReporter?.startStats()
1683
+ }
1684
+ }
1680
1685
1681
1686
peerConnection?.startLocalAudioCapture()
1682
1687
@@ -1696,9 +1701,14 @@ class TelnyxClient(
1696
1701
},
1697
1702
Call .ICE_CANDIDATE_DELAY
1698
1703
)
1704
+ calls[this .callId]?.updateCallState(CallState .ACTIVE )
1705
+ this .setCallState(calls[this .callId]?.callStateFlow?.value ? : CallState .ACTIVE )
1706
+ calls[this .callId] = this .apply {
1707
+ updateCallState(CallState .ACTIVE )
1708
+ }
1709
+ } ? : run {
1710
+ Timber .e(" Call not found for Attach" )
1699
1711
}
1700
- attachCall.updateCallState(CallState .ACTIVE )
1701
- calls[attachCall.callId] = attachCall
1702
1712
}
1703
1713
1704
1714
override fun setCallRecovering () {
0 commit comments