Skip to content

Commit

Permalink
Connection timing
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandar-apostolov committed Dec 18, 2024
1 parent a048ff9 commit 2286a7c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,9 @@ public class Call(
internal var session: RtcSession? = null
var sessionId = UUID.randomUUID().toString()

internal var connectedAt: Long? = null
internal var reconnectAt: Pair<WebsocketReconnectStrategy, Long>? = null

internal val mediaManager by lazy {
if (testInstanceProvider.mediaManagerCreator != null) {
testInstanceProvider.mediaManagerCreator!!.invoke()
Expand Down Expand Up @@ -463,6 +466,7 @@ public class Call(
session = if (testInstanceProvider.rtcSessionCreator != null) {
testInstanceProvider.rtcSessionCreator!!.invoke()
} else {
connectedAt = System.currentTimeMillis()
RtcSession(
sessionId = this.sessionId,
apiKey = clientImpl.apiKey,
Expand Down Expand Up @@ -617,6 +621,7 @@ public class Call(
subscriptions = subscriptionsInfo,
reconnect_attempt = reconnectAttepmts,
)
reconnectAt = Pair(WebsocketReconnectStrategy.WEBSOCKET_RECONNECT_STRATEGY_FAST, System.currentTimeMillis())
session.fastReconnect(reconnectDetails)
} else {
logger.e { "[reconnect] Disconnecting" }
Expand All @@ -629,6 +634,7 @@ public class Call(
*/
suspend fun rejoin() = schedule {
logger.d { "[rejoin] Rejoining" }
reconnectAt = Pair(WebsocketReconnectStrategy.WEBSOCKET_RECONNECT_STRATEGY_REJOIN, System.currentTimeMillis())
reconnectAttepmts++
state._connection.value = RealtimeConnection.Reconnecting
location?.let {
Expand Down Expand Up @@ -702,6 +708,7 @@ public class Call(
reconnect_attempt = reconnectAttepmts,
)
session.prepareRejoin()
reconnectAt = Pair(WebsocketReconnectStrategy.WEBSOCKET_RECONNECT_STRATEGY_MIGRATE, System.currentTimeMillis())
val newSession = RtcSession(
clientImpl,
powerManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,13 @@ import stream.video.sfu.models.WebsocketReconnectStrategy
import stream.video.sfu.signal.ICERestartRequest
import stream.video.sfu.signal.ICERestartResponse
import stream.video.sfu.signal.ICETrickleResponse
import stream.video.sfu.signal.Reconnection
import stream.video.sfu.signal.SendAnswerRequest
import stream.video.sfu.signal.SendAnswerResponse
import stream.video.sfu.signal.SendStatsRequest
import stream.video.sfu.signal.SetPublisherRequest
import stream.video.sfu.signal.SetPublisherResponse
import stream.video.sfu.signal.Telemetry
import stream.video.sfu.signal.TrackMuteState
import stream.video.sfu.signal.TrackSubscriptionDetails
import stream.video.sfu.signal.UpdateMuteStatesRequest
Expand Down Expand Up @@ -301,6 +303,9 @@ public class RtcSession internal constructor(
return setTrack(sessionId, type, track)
}

private var connectedAt: Long? = null
private var reconnectAt: Pair<WebsocketReconnectStrategy, Long>? = null

/**
* Connection and WebRTC.
*/
Expand Down Expand Up @@ -519,6 +524,7 @@ public class RtcSession internal constructor(
logger.d { "Connecting RTC, $request" }
listenToSfuSocket()
sfuConnectionModule.socketConnection.connect(request)
connectedAt = System.currentTimeMillis()
sfuConnectionModule.socketConnection.whenConnected {
connectRtc()
}
Expand Down Expand Up @@ -1631,6 +1637,7 @@ public class RtcSession internal constructor(

internal suspend fun sendCallStats(report: CallStatsReport) {
val result = wrapAPICall {
val now = System.currentTimeMillis()
val androidThermalState =
safeCallWithDefault(AndroidThermalState.ANDROID_THERMAL_STATE_UNSPECIFIED) {
val thermalState = powerManager?.currentThermalStatus
Expand Down Expand Up @@ -1663,6 +1670,22 @@ public class RtcSession internal constructor(
thermal_state = androidThermalState,
is_power_saver_mode = powerSaving,
),
telemetry = safeCallWithDefault(null) {
if (call.connectedAt != null || call.reconnectAt != null) {
Telemetry(
connection_time_seconds = call.connectedAt?.let { (now - it) / 1000 }
?.toFloat(),
reconnection = call.reconnectAt?.let {
Reconnection(
time_seconds = ((now - it.second) / 1000).toFloat(),
strategy = it.first,
)
},
)
} else {
null
}
},
),
)
}
Expand Down

0 comments on commit 2286a7c

Please sign in to comment.