From 43acb5e9003d4069af3516781284b34678c04943 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sierpi=C5=84ski?= <33436839+sierpinskid@users.noreply.github.com> Date: Thu, 13 Feb 2025 15:51:48 +0100 Subject: [PATCH 1/2] Fix CallEnded event being called twice for the local user when local user ends the call. EndCallAsync doesn't need to call LeaveCallAsync because it will be called by the received `call.ended` event --- .../Runtime/Core/StreamVideoClient.cs | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Packages/StreamVideo/Runtime/Core/StreamVideoClient.cs b/Packages/StreamVideo/Runtime/Core/StreamVideoClient.cs index 82b005f..93831c7 100644 --- a/Packages/StreamVideo/Runtime/Core/StreamVideoClient.cs +++ b/Packages/StreamVideo/Runtime/Core/StreamVideoClient.cs @@ -281,12 +281,9 @@ void IStreamVideoClientEventsListener.Destroy() Task IInternalStreamVideoClient.LeaveCallAsync(IStreamCall call) => LeaveCallAsync(call); - async Task IInternalStreamVideoClient.EndCallAsync(IStreamCall call) - { - //StreamTodo: check if call is active - await InternalLowLevelClient.InternalVideoClientApi.EndCallAsync(call.Type, call.Id); - await LeaveCallAsync(call); - } + // Leaving the call will be triggered by the received `call.ended` event + Task IInternalStreamVideoClient.EndCallAsync(IStreamCall call) + => InternalLowLevelClient.InternalVideoClientApi.EndCallAsync(call.Type, call.Id); Task IInternalStreamVideoClient.StartHLSAsync(IStreamCall call) => InternalLowLevelClient.InternalVideoClientApi.StartBroadcastingAsync(call.Type, call.Id); @@ -390,8 +387,15 @@ public Task SendDebugLogs(string callId, string participantId) private async Task LeaveCallAsync(IStreamCall call) { - //StreamTodo: check if call is active - await InternalLowLevelClient.RtcSession.StopAsync(); + try + { + await InternalLowLevelClient.RtcSession.StopAsync(); + } + catch (Exception e) + { + _logs.Exception(e); + } + CallEnded?.Invoke(call); } @@ -506,6 +510,11 @@ private void OnInternalCallUpdatedEvent(CallUpdatedEventInternalDTO eventData) private void OnInternalCallEndedEvent(CallEndedEventInternalDTO eventData) { var call = _cache.TryCreateOrUpdate(eventData.Call); + if (call == null) + { + _logs.Error($"Received call ended event for a call that doesn't exist. {nameof(eventData.Call.Cid)}:" + eventData?.Call?.Cid); + return; + } call.LeaveAsync().LogIfFailed(); } From df1f06dce2aca30ca5380a5046e2e7106ca4be8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sierpi=C5=84ski?= <33436839+sierpinskid@users.noreply.github.com> Date: Thu, 13 Feb 2025 16:38:00 +0100 Subject: [PATCH 2/2] The change failed stats request to warning - this can fail if remote user ends the call while the request is being made. So it fails just before receiving the `call.ended` event --- .../StreamVideo/Runtime/Core/LowLevelClient/RtcSession.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Packages/StreamVideo/Runtime/Core/LowLevelClient/RtcSession.cs b/Packages/StreamVideo/Runtime/Core/LowLevelClient/RtcSession.cs index d5535c8..f003a1e 100644 --- a/Packages/StreamVideo/Runtime/Core/LowLevelClient/RtcSession.cs +++ b/Packages/StreamVideo/Runtime/Core/LowLevelClient/RtcSession.cs @@ -220,7 +220,11 @@ public async Task SendWebRtcStats(SendStatsRequest request) if (response.Error != null) { - _logs.Error(response.Error.Message); + // StreamTodo: perhaps failure on stats sending should be silent? This can return "call not found" if call ended before `call.ended` event was received + // Maybe we can wait 1-2s before displaying the error to cover this case + + _logs.Warning("Sending webRTC stats failed: " + response.Error.Message); + _logs.ErrorIfDebug("Sending webRTC stats failed: " + response.Error.Message); } }