Skip to content

Commit

Permalink
feat: improve rtmp client status
Browse files Browse the repository at this point in the history
  • Loading branch information
josephnhtam committed Oct 1, 2024
1 parent 2b80c3e commit 03f8d01
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ namespace LiveStreamingServerNet.Rtmp.Client.Contracts
public interface IRtmpClient : IAsyncDisposable
{
IServiceProvider Services { get; }

bool IsHandshakeCompleted { get; }
bool IsConnected { get; }
bool IsStarted { get; }
bool IsStopped { get; }
RtmpClientStatus Status { get; }

Task<ConnectResponse> ConnectAsync(ServerEndPoint endPoint, string appName);
Task<ConnectResponse> ConnectAsync(ServerEndPoint endPoint, string appName, IDictionary<string, object> information);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ public HostRtmpClient(ServiceProvider serviceProvider)
_innerClient = serviceProvider.GetRequiredService<IRtmpClient>();
}

public bool IsHandshakeCompleted => _innerClient.IsHandshakeCompleted;
public bool IsConnected => _innerClient.IsConnected;
public bool IsStarted => _innerClient.IsStarted;
public bool IsStopped => _innerClient.IsStopped;
public IServiceProvider Services => _innerClient.Services;
public RtmpClientStatus Status => _innerClient.Status;

public Task<ConnectResponse> ConnectAsync(ServerEndPoint endPoint, string appName)
=> _innerClient.ConnectAsync(endPoint, appName);
Expand Down
45 changes: 27 additions & 18 deletions src/LiveStreamingServerNet.Rtmp.Client/Internal/RtmpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,9 @@ internal class RtmpClient : IRtmpClient, IRtmpHandshakeEventHandler
private readonly TaskCompletionSource _clientTcs = new();
private readonly TaskCompletionSource _handshakeTcs = new();

private Task? _clientTask;
private int _connectOnce;
private uint _lastChunkStreamId = RtmpConstants.ReservedChunkStreamId;

public bool IsConnected { get; private set; }
public bool IsHandshakeCompleted => _handshakeTcs.Task.IsCompletedSuccessfully;

public bool IsStarted => _clientTask != null;
public bool IsStopped => _clientTcs.Task.IsCompleted;

public IServiceProvider Services => _client.Services;
private bool _connected;
private Task? _clientTask;

public RtmpClient(
IClient client,
Expand All @@ -54,6 +46,28 @@ public RtmpClient(
_config = config.Value;
}

public IServiceProvider Services => _client.Services;

public RtmpClientStatus Status
{
get
{
if (_clientTcs.Task.IsCompleted)
return RtmpClientStatus.Stopped;

if (_connected)
return RtmpClientStatus.Connected;

if (_handshakeTcs.Task.IsCompletedSuccessfully)
return RtmpClientStatus.HandshakeCompleted;

if (_clientTask != null)
return RtmpClientStatus.Connecting;

return RtmpClientStatus.None;
}
}

public Task<ConnectResponse> ConnectAsync(ServerEndPoint endPoint, string appName)
{
return ConnectAsync(endPoint, appName, new Dictionary<string, object>());
Expand All @@ -78,7 +92,7 @@ public async Task<ConnectResponse> ConnectAsync(ServerEndPoint endPoint, string

public async Task<IRtmpStream> CreateStreamAsync()
{
if (!IsConnected)
if (!_connected)
throw new InvalidOperationException("Client is not connected.");

var createStreamTcs = new TaskCompletionSource<IRtmpStream>();
Expand Down Expand Up @@ -118,7 +132,7 @@ private async Task RunClientAsync(ServerEndPoint endPoint)
}
finally
{
IsConnected = false;
_connected = false;
}
}

Expand All @@ -136,7 +150,7 @@ private async Task<ConnectResponse> ConnectAfterHandshakeAsync(string appName, I
{
if (success)
{
IsConnected = true;
_connected = true;
connectTcs.TrySetResult(new(new Dictionary<string, object>(information), parameters));
}
else
Expand Down Expand Up @@ -213,11 +227,6 @@ public ValueTask OnRtmpHandshakeCompleteAsync(IEventContext context)
return ValueTask.CompletedTask;
}

public uint GetNextChunkStreamId()
{
return Interlocked.Increment(ref _lastChunkStreamId);
}

public async ValueTask DisposeAsync()
{
if (!_clientCts.IsCancellationRequested)
Expand Down
12 changes: 12 additions & 0 deletions src/LiveStreamingServerNet.Rtmp.Client/RtmpClientStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace LiveStreamingServerNet.Rtmp.Client
{
public enum RtmpClientStatus
{
None = 0,
Connecting = 1,
HandshakeStarted = 2,
HandshakeCompleted = 3,
Connected = 4,
Stopped = 5
}
}

0 comments on commit 03f8d01

Please sign in to comment.