Skip to content

Commit

Permalink
Merge pull request #44 from GetStream/feature/handle-missing-sfu-events
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
sierpinskid authored Dec 19, 2023
2 parents 657d572 + fa08366 commit 18ae04d
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public interface IStreamVideoLowLevelClient : IAuthProvider, IConnectionProvider
/// <summary>
/// Client is attempting to reconnect after lost connection
/// </summary>
event Action Reconnecting;
//event Action Reconnecting; //StreamTodo: reimplemented reconnecting event

/// <summary>
/// Client lost connection with the server. if ReconnectStrategy is Exponential or Constant it will attempt to reconnect.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ReconnectScheduler(ITimeService timeService, IStreamVideoLowLevelClient l
_networkMonitor.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;

_client.Connected += OnConnected;
_client.Reconnecting += OnReconnecting;
//_client.Reconnecting += OnReconnecting;
_client.ConnectionStateChanged += OnConnectionStateChanged;
}

Expand All @@ -61,7 +61,7 @@ public void Dispose()
if (_client != null)
{
_client.Connected -= OnConnected;
_client.Reconnecting -= OnReconnecting;
//_client.Reconnecting -= OnReconnecting;
_client.ConnectionStateChanged -= OnConnectionStateChanged;
}
}
Expand Down Expand Up @@ -199,10 +199,11 @@ private void OnNetworkAvailabilityChanged(bool isNetworkAvailable)
NextReconnectTime = _timeService.Time;
}

private void OnReconnecting()
{
_reconnectAttempts++;
}
//StreamTodo: Unused "OnReconnecting"
// private void OnReconnecting()
// {
// _reconnectAttempts++;
// }

private void OnConnected()
{
Expand Down
114 changes: 76 additions & 38 deletions Packages/StreamVideo/Runtime/Core/LowLevelClient/RtcSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,41 +125,58 @@ public Camera VideoSceneInput

public string SessionId { get; private set; }

public RtcSession(SfuWebSocket sfuWebSocket, ILogs logs, ISerializer serializer, IHttpClient httpClient,
ITimeService timeService, IStreamClientConfig config)
public RtcSession(SfuWebSocket sfuWebSocket, Func<IStreamCall, HttpClient> httpClientFactory, ILogs logs, ISerializer serializer, ITimeService timeService,
IStreamClientConfig config)
{
_httpClientFactory = httpClientFactory;
_config = config;
_timeService = timeService;
_serializer = serializer;
_httpClient = httpClient;
_logs = logs;

//StreamTodo: SFU WS should be created here so that RTC session owns it
_sfuWebSocket = sfuWebSocket ?? throw new ArgumentNullException(nameof(sfuWebSocket));
_sfuWebSocket.JoinResponse += OnSfuJoinResponse;
_sfuWebSocket.IceTrickle += OnSfuIceTrickle;

_sfuWebSocket.SubscriberOffer += OnSfuSubscriberOffer;
_sfuWebSocket.TrackPublished += OnSfuTrackPublished;
_sfuWebSocket.TrackUnpublished += OnSfuTrackUnpublished;
_sfuWebSocket.PublisherAnswer += OnSfuPublisherAnswer;
_sfuWebSocket.ConnectionQualityChanged += OnSfuConnectionQualityChanged;
_sfuWebSocket.AudioLevelChanged += OnSfuAudioLevelChanged;
_sfuWebSocket.IceTrickle += OnSfuIceTrickle;
_sfuWebSocket.ChangePublishQuality += OnSfuChangePublishQuality;
_sfuWebSocket.ParticipantJoined += OnSfuParticipantJoined;
_sfuWebSocket.ParticipantLeft += OnSfuParticipantLeft;
_sfuWebSocket.DominantSpeakerChanged += OnSfuDominantSpeakerChanged;
_sfuWebSocket.Error += SfuWebSocketOnError;
_sfuWebSocket.JoinResponse += OnSfuJoinResponse;
_sfuWebSocket.TrackPublished += OnSfuTrackPublished;
_sfuWebSocket.TrackUnpublished += OnSfuTrackUnpublished;
_sfuWebSocket.Error += OnSfuWebSocketOnError;
_sfuWebSocket.CallGrantsUpdated += OnSfuCallGrantsUpdated;
_sfuWebSocket.GoAway += OnSfuGoAway;
_sfuWebSocket.IceRestart += OnSfuIceRestart;
_sfuWebSocket.PinsUpdated += OnSfuPinsUpdated;
}

public void Dispose()
{
StopAsync().LogIfFailed();

_sfuWebSocket.JoinResponse -= OnSfuJoinResponse;
_sfuWebSocket.IceTrickle -= OnSfuIceTrickle;
_sfuWebSocket.SubscriberOffer -= OnSfuSubscriberOffer;
_sfuWebSocket.TrackPublished -= OnSfuTrackPublished;
_sfuWebSocket.TrackUnpublished -= OnSfuTrackUnpublished;
_sfuWebSocket.PublisherAnswer -= OnSfuPublisherAnswer;
_sfuWebSocket.ConnectionQualityChanged -= OnSfuConnectionQualityChanged;
_sfuWebSocket.AudioLevelChanged -= OnSfuAudioLevelChanged;
_sfuWebSocket.IceTrickle -= OnSfuIceTrickle;
_sfuWebSocket.ChangePublishQuality -= OnSfuChangePublishQuality;
_sfuWebSocket.ParticipantJoined -= OnSfuParticipantJoined;
_sfuWebSocket.ParticipantLeft -= OnSfuParticipantLeft;
_sfuWebSocket.DominantSpeakerChanged -= OnSfuDominantSpeakerChanged;
_sfuWebSocket.Error -= SfuWebSocketOnError;
_sfuWebSocket.JoinResponse -= OnSfuJoinResponse;
_sfuWebSocket.TrackPublished -= OnSfuTrackPublished;
_sfuWebSocket.TrackUnpublished -= OnSfuTrackUnpublished;
_sfuWebSocket.Error -= OnSfuWebSocketOnError;
_sfuWebSocket.CallGrantsUpdated -= OnSfuCallGrantsUpdated;
_sfuWebSocket.GoAway -= OnSfuGoAway;
_sfuWebSocket.IceRestart -= OnSfuIceRestart;
_sfuWebSocket.PinsUpdated -= OnSfuPinsUpdated;
_sfuWebSocket.Dispose();

DisposeSubscriber();
Expand Down Expand Up @@ -198,6 +215,7 @@ public async Task StartAsync(StreamCall call)
ClearSession();

ActiveCall = call ?? throw new ArgumentNullException(nameof(call));
_httpClient = _httpClientFactory(ActiveCall);

CallState = CallingState.Joining;

Expand Down Expand Up @@ -257,10 +275,11 @@ public void Reconnect()
private readonly ILogs _logs;
private readonly ITimeService _timeService;
private readonly IStreamClientConfig _config;
private readonly Func<IStreamCall, HttpClient> _httpClientFactory;

private readonly List<ICETrickle> _pendingIceTrickleRequests = new List<ICETrickle>();

private IHttpClient _httpClient;
private HttpClient _httpClient;
private CallingState _callState;

private StreamPeerConnection _subscriber;
Expand All @@ -270,6 +289,11 @@ public void Reconnect()
private float _lastTrackSubscriptionRequestTime;
private bool _trackSubscriptionRequested;
private bool _trackSubscriptionRequestInProgress;

private SdpMungeUtils _sdpMungeUtils = new SdpMungeUtils();
private AudioSource _audioInput;
private WebCamTexture _videoInput;
private Camera _videoSceneInput;

private void ClearSession()
{
Expand Down Expand Up @@ -455,7 +479,9 @@ private void OnSfuIceTrickle(ICETrickle iceTrickle)
*/
private async void OnSfuSubscriberOffer(SubscriberOffer subscriberOffer)
{
#if STREAM_DEBUG_ENABLED
_logs.Warning("OnSfuSubscriberOffer");
#endif
//StreamTodo: check RtcSession.kt handleSubscriberOffer for the retry logic

try
Expand Down Expand Up @@ -589,40 +615,57 @@ private void OnSfuDominantSpeakerChanged(DominantSpeakerChanged dominantSpeakerC
ActiveCall.UpdateFromSfu(dominantSpeakerChanged, _cache);
}

private void SfuWebSocketOnError(Error obj)
private void OnSfuWebSocketOnError(Error obj)
{
_logs.Error($"Sfu Error - Code: {obj.Error_.Code}, Message: {obj.Error_.Message}, ShouldRetry: {obj.Error_.ShouldRetry}");
}

private void OnSfuPinsUpdated(PinsChanged pinsChanged)
{
}

private void OnSfuIceRestart(ICERestart iceRestart)
{
}

private void OnSfuGoAway(GoAway goAway)
{
}

private void OnSfuCallGrantsUpdated(CallGrantsUpdated callGrantsUpdated)
{
}

private void OnSfuChangePublishQuality(ChangePublishQuality changePublishQuality)
{
}

private void OnSfuConnectionQualityChanged(ConnectionQualityChanged connectionQualityChanged)
{
}

private void OnSfuAudioLevelChanged(AudioLevelChanged audioLevelChanged)
{
}

private void OnSfuPublisherAnswer(PublisherAnswer publisherAnswer)
{
}

//StreamTodo: implement retry strategy like in Android SDK
private async Task<TResponse> RpcCallAsync<TRequest, TResponse>(TRequest request,
Func<HttpClient, TRequest, Task<TResponse>> rpcCallAsync, string debugRequestName, bool preLog = false)
{
var serializedRequest = _serializer.Serialize(request);

#if STREAM_DEBUG_ENABLED
if (preLog)
{
_logs.Warning($"[RPC REQUEST START] {debugRequestName} {serializedRequest}");
}
#endif

//StreamTodo: use injected client or cache this one
var connectUrl = ActiveCall.Credentials.Server.Url.Replace("/twirp", "");

//StreamTodo: move headers population logic elsewhere + remove duplication with main client
var httpClient = new HttpClient()
{
DefaultRequestHeaders =
{
{ "stream-auth-type", "jwt" },
{ "X-Stream-Client", "stream-video-unity-client-0.1.0" }
}
};

httpClient.DefaultRequestHeaders.Authorization
= new AuthenticationHeaderValue(ActiveCall.Credentials.Token);
httpClient.BaseAddress = new Uri(connectUrl);

var response = await rpcCallAsync(httpClient, request);
var response = await rpcCallAsync(_httpClient, request);
var serializedResponse = _serializer.Serialize(response);

#if STREAM_DEBUG_ENABLED
Expand Down Expand Up @@ -744,11 +787,6 @@ await _publisher.SetRemoteDescriptionAsync(new RTCSessionDescription()
}
}

private SdpMungeUtils _sdpMungeUtils = new SdpMungeUtils();
private AudioSource _audioInput;
private WebCamTexture _videoInput;
private Camera _videoSceneInput;

private string ExtractVideoTrackId(string sdp)
{
var lines = sdp.Split("\n");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ private void OnIceGatheringStateChange(RTCIceGatheringState state)

private void OnNegotiationNeeded()
{
//_logs.Warning($"$$$$$$$ [{_peerType}] OnNegotiationNeeded");
#if STREAM_DEBUG_ENABLED
_logs.Warning($"[{_peerType}] OnNegotiationNeeded");
#endif

//StreamTodo: take into account race conditions https://blog.mozilla.org/webrtc/perfect-negotiation-in-webrtc/
//We want to set the local description if signalingState is stable - we need to check it because state could change during async operations
Expand Down Expand Up @@ -444,7 +446,6 @@ private static IEnumerable<RTCRtpEncodingParameters> GetVideoEncodingParameters(
Debug.LogWarning($"Rid values: {fullQuality.rid}, {halfQuality.rid}, {quarterQuality.rid}");
#endif

//StreamTodo: temporarily disabled because simulcast is not working with current Unity's WebRTC lib
yield return quarterQuality;
yield return halfQuality;
yield return fullQuality;
Expand Down
Loading

0 comments on commit 18ae04d

Please sign in to comment.