Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ internal interface IReconnectScheduler
event Action ReconnectionScheduled;

void Reset();

void SetTarget(IReconnectTarget target);
}

internal interface IReconnectTarget
{
event ConnectionStateChangeHandler ConnectionStateChanged;
ConnectionState ConnectionState { get; }
}

/// <summary>
/// Schedules next reconnection time based on the past attempts and network availability
/// </summary>
Expand Down Expand Up @@ -44,29 +53,26 @@ private set
}
}

public ReconnectScheduler(ITimeService timeService, IStreamVideoLowLevelClient lowLevelClient,
INetworkMonitor networkMonitor, Func<bool> shouldReconnect)
public ReconnectScheduler(ITimeService timeService, INetworkMonitor networkMonitor, Func<bool> shouldReconnect)
{
_shouldReconnect = shouldReconnect ?? throw new ArgumentNullException(nameof(shouldReconnect));
_client = lowLevelClient ?? throw new ArgumentNullException(nameof(lowLevelClient));
_timeService = timeService ?? throw new ArgumentNullException(nameof(timeService));
_networkMonitor = networkMonitor ?? throw new ArgumentNullException(nameof(networkMonitor));

_networkMonitor.NetworkAvailabilityChanged += OnNetworkAvailabilityChanged;
}

_client.Connected += OnConnected;
//_client.Reconnecting += OnReconnecting;
_client.ConnectionStateChanged += OnConnectionStateChanged;
//StreamTODO: refactor so that each WS instance creates the reconnector internally and injects its instance
public void SetTarget(IReconnectTarget target)
{
UnsubscribeFromTarget();
_target = target ?? throw new ArgumentNullException(nameof(target));
SubscribeToTarget();
}

public void Dispose()
{
if (_client != null)
{
_client.Connected -= OnConnected;
//_client.Reconnecting -= OnReconnecting;
_client.ConnectionStateChanged -= OnConnectionStateChanged;
}
UnsubscribeFromTarget();
}

public void SetReconnectStrategySettings(ReconnectStrategy reconnectStrategy, float? exponentialMinInterval,
Expand Down Expand Up @@ -114,14 +120,35 @@ public void Stop()
}

//StreamTodo: connection info could be split to separate interface
private readonly IStreamVideoLowLevelClient _client;
private readonly ITimeService _timeService;
private readonly INetworkMonitor _networkMonitor;

private int _reconnectAttempts;
private bool _isStopped;
private double? _nextReconnectTime;
private Func<bool> _shouldReconnect;

private IReconnectTarget _target;

private void SubscribeToTarget()
{
if (_target == null)
{
return;
}

_target.ConnectionStateChanged += OnConnectionStateChanged;
}

private void UnsubscribeFromTarget()
{
if (_target == null)
{
return;
}

_target.ConnectionStateChanged -= OnConnectionStateChanged;
}

private void TryScheduleNextReconnectTime()
{
Expand Down Expand Up @@ -198,8 +225,8 @@ private void OnNetworkAvailabilityChanged(bool isNetworkAvailable)
return;
}

if (_client.ConnectionState == ConnectionState.Connected ||
_client.ConnectionState == ConnectionState.Connecting)
if (_target.ConnectionState == ConnectionState.Connected ||
_target.ConnectionState == ConnectionState.Connecting)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ public void TryRestartAudioPlayback()
public void PauseAndroidAudioPlayback()
{
#if STREAM_NATIVE_AUDIO
WebRTC.StopAudioPlayback();
WebRTC.MuteAndroidAudioPlayback();
_logs.Warning("Audio Playback is paused. This stops all audio coming from StreamVideo SDK on Android platform.");
#else
throw new NotSupportedException(
Expand All @@ -682,7 +682,7 @@ public void PauseAndroidAudioPlayback()
public void ResumeAndroidAudioPlayback()
{
#if STREAM_NATIVE_AUDIO
WebRTC.StartAudioPlayback(AudioOutputSampleRate, AudioOutputChannels);
WebRTC.UnmuteAndroidAudioPlayback();
_logs.Warning("Audio Playback is resumed. This resumes audio coming from StreamVideo SDK on Android platform.");
#else
throw new NotSupportedException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ public StreamVideoLowLevelClient(IWebsocketClient coordinatorWebSocket, IWebsock
}

//StreamTodo: move to factory
var coordinatorReconnect = new ReconnectScheduler(_timeService, this, _networkMonitor, shouldReconnect: () => true);
var sfuReconnect = new ReconnectScheduler(_timeService, this, _networkMonitor, shouldReconnect: () => RtcSession.ShouldSfuAttemptToReconnect());
var coordinatorReconnect = new ReconnectScheduler(_timeService, _networkMonitor, shouldReconnect: () => true);
var sfuReconnect = new ReconnectScheduler(_timeService, _networkMonitor, shouldReconnect: () => RtcSession.ShouldSfuAttemptToReconnect());

//StreamTodo: move to factory
_coordinatorWS = new CoordinatorWebSocket(coordinatorWebSocket, coordinatorReconnect, authProvider: this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace StreamVideo.Core.LowLevelClient.WebSockets
{
internal abstract class BasePersistentWebSocket : IPersistentWebSocket
internal abstract class BasePersistentWebSocket : IPersistentWebSocket, IReconnectTarget
{
public event ConnectionStateChangeHandler ConnectionStateChanged;
public event Action Connected;
Expand Down Expand Up @@ -207,6 +207,8 @@ protected BasePersistentWebSocket(IWebsocketClient websocketClient, IReconnectSc
Logs = logs;
WebsocketClient = websocketClient ?? throw new ArgumentNullException(nameof(websocketClient));

_reconnectScheduler.SetTarget(this);

WebsocketClient.ConnectionFailed += OnConnectionFailed;
WebsocketClient.Disconnected += OnDisconnected;

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,16 @@ public void StopAudioPlayback()
NativeMethods.StopAudioPlayback(self);
}

public void MuteAndroidAudioPlayback()
{
NativeMethods.MuteAndroidAudioPlayback(self);
}

public void UnmuteAndroidAudioPlayback()
{
NativeMethods.UnmuteAndroidAudioPlayback(self);
}

public void SetAndroidAudioUsageMode(int usage)
{
NativeMethods.SetAndroidAudioUsageMode(self, usage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,16 @@ public static void StopAudioPlayback()
Context.StopAudioPlayback();
}

public static void MuteAndroidAudioPlayback()
{
Context.MuteAndroidAudioPlayback();
}

public static void UnmuteAndroidAudioPlayback()
{
Context.UnmuteAndroidAudioPlayback();
}

/// <summary>
/// Sets the Android audio usage mode for audio playback.
/// Changing the usage mode during an active call will restart the audio stream.
Expand Down Expand Up @@ -1866,6 +1876,12 @@ public static extern IntPtr CreateVideoRenderer(
[DllImport(WebRTC.Lib)]
public static extern void StopAudioPlayback(IntPtr context);

[DllImport(WebRTC.Lib)]
public static extern void MuteAndroidAudioPlayback(IntPtr context);

[DllImport(WebRTC.Lib)]
public static extern void UnmuteAndroidAudioPlayback(IntPtr context);

[DllImport(WebRTC.Lib)]
public static extern void SetAudioProcessingModule(IntPtr context, bool enabled, bool echoCancellationEnabled, bool autoGainEnabled, bool noiseSuppressionEnabled, int noiseSuppressionLevel);

Expand Down
Loading