diff --git a/Packages/StreamVideo/Runtime/Core/LowLevelClient/ReconnectScheduler.cs b/Packages/StreamVideo/Runtime/Core/LowLevelClient/ReconnectScheduler.cs index a812779c..cb75286a 100644 --- a/Packages/StreamVideo/Runtime/Core/LowLevelClient/ReconnectScheduler.cs +++ b/Packages/StreamVideo/Runtime/Core/LowLevelClient/ReconnectScheduler.cs @@ -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; } } + /// /// Schedules next reconnection time based on the past attempts and network availability /// @@ -44,29 +53,26 @@ private set } } - public ReconnectScheduler(ITimeService timeService, IStreamVideoLowLevelClient lowLevelClient, - INetworkMonitor networkMonitor, Func shouldReconnect) + public ReconnectScheduler(ITimeService timeService, INetworkMonitor networkMonitor, Func 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, @@ -114,7 +120,6 @@ public void Stop() } //StreamTodo: connection info could be split to separate interface - private readonly IStreamVideoLowLevelClient _client; private readonly ITimeService _timeService; private readonly INetworkMonitor _networkMonitor; @@ -122,6 +127,28 @@ public void Stop() private bool _isStopped; private double? _nextReconnectTime; private Func _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() { @@ -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; } diff --git a/Packages/StreamVideo/Runtime/Core/LowLevelClient/RtcSession.cs b/Packages/StreamVideo/Runtime/Core/LowLevelClient/RtcSession.cs index d6414914..02663ebe 100644 --- a/Packages/StreamVideo/Runtime/Core/LowLevelClient/RtcSession.cs +++ b/Packages/StreamVideo/Runtime/Core/LowLevelClient/RtcSession.cs @@ -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( @@ -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( diff --git a/Packages/StreamVideo/Runtime/Core/LowLevelClient/StreamVideoLowLevelClient.cs b/Packages/StreamVideo/Runtime/Core/LowLevelClient/StreamVideoLowLevelClient.cs index 47840ce5..5bdd0196 100644 --- a/Packages/StreamVideo/Runtime/Core/LowLevelClient/StreamVideoLowLevelClient.cs +++ b/Packages/StreamVideo/Runtime/Core/LowLevelClient/StreamVideoLowLevelClient.cs @@ -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, diff --git a/Packages/StreamVideo/Runtime/Core/LowLevelClient/WebSockets/BasePersistentWebSocket.cs b/Packages/StreamVideo/Runtime/Core/LowLevelClient/WebSockets/BasePersistentWebSocket.cs index cbab6449..77e53ae4 100644 --- a/Packages/StreamVideo/Runtime/Core/LowLevelClient/WebSockets/BasePersistentWebSocket.cs +++ b/Packages/StreamVideo/Runtime/Core/LowLevelClient/WebSockets/BasePersistentWebSocket.cs @@ -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; @@ -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; diff --git a/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Plugins/Android/libwebrtc.aar b/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Plugins/Android/libwebrtc.aar index 1b3a9a72..a44ddb01 100644 Binary files a/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Plugins/Android/libwebrtc.aar and b/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Plugins/Android/libwebrtc.aar differ diff --git a/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/Context.cs b/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/Context.cs index ea3ec37b..be9498af 100644 --- a/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/Context.cs +++ b/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/Context.cs @@ -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); diff --git a/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/WebRTC.cs b/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/WebRTC.cs index 9472218c..d52e2881 100644 --- a/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/WebRTC.cs +++ b/Packages/StreamVideo/Runtime/Libs/io.stream.unity.webrtc/Runtime/Scripts/WebRTC.cs @@ -1153,6 +1153,16 @@ public static void StopAudioPlayback() Context.StopAudioPlayback(); } + public static void MuteAndroidAudioPlayback() + { + Context.MuteAndroidAudioPlayback(); + } + + public static void UnmuteAndroidAudioPlayback() + { + Context.UnmuteAndroidAudioPlayback(); + } + /// /// Sets the Android audio usage mode for audio playback. /// Changing the usage mode during an active call will restart the audio stream. @@ -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);