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
35 changes: 34 additions & 1 deletion samples/LiveStreamingServerNet.FlvDemo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using LiveStreamingServerNet.Flv.Contracts;
using LiveStreamingServerNet.Flv.Installer;
using LiveStreamingServerNet.Utilities.Contracts;
using System.Net;

namespace LiveStreamingServerNet.FlvDemo
Expand All @@ -11,7 +13,9 @@

builder.Services.AddLiveStreamingServer(
new IPEndPoint(IPAddress.Any, 1935),
options => options.AddFlv()
options => options.AddFlv(configure =>
configure.AddStreamEventHandler<FlvServerStreamEventHandler>()
)
);

var app = builder.Build();
Expand All @@ -25,4 +29,33 @@
await app.RunAsync();
}
}

public class FlvServerStreamEventHandler : IFlvServerStreamEventHandler
{
private readonly IFlvStreamInfoManager _streamInfoManager;
private readonly ILogger<FlvServerStreamEventHandler> _logger;

public FlvServerStreamEventHandler(IFlvStreamInfoManager streamInfoManager, ILogger<FlvServerStreamEventHandler> logger)
{
_streamInfoManager = streamInfoManager;
_logger = logger;
}

public ValueTask OnFlvStreamSubscribedAsync(IEventContext context, IFlvClientHandle client)
{
var streamInfo = _streamInfoManager.GetStreamInfo(client.StreamPath);
_logger.LogInformation("Client {ClientId} subscribed to stream {StreamPath}. Total subscribers: {SubscriberCount}",
client.ClientId, client.StreamPath, streamInfo?.Subscribers.Count() ?? 0);

return ValueTask.CompletedTask;
}

public ValueTask OnFlvStreamUnsubscribedAsync(IEventContext context, string clientId, string streamPath)
{
var streamInfo = _streamInfoManager.GetStreamInfo(streamPath);
_logger.LogInformation("Client {ClientId} unsubscribed from stream {StreamPath}. Total subscribers: {SubscriberCount}",
clientId, streamPath, streamInfo?.Subscribers.Count() ?? 0);
return ValueTask.CompletedTask;
}
}
}
13 changes: 13 additions & 0 deletions src/LiveStreamingServerNet.Flv/Contracts/IFlvClientHandle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace LiveStreamingServerNet.Flv.Contracts
{
/// <summary>
/// Represents a handle to an FLV client connection, providing methods to manage the connection.
/// </summary>
public interface IFlvClientHandle : IFlvClientInfo
{
/// <summary>
/// Stops the FLV client.
/// </summary>
void Stop();
}
}
28 changes: 28 additions & 0 deletions src/LiveStreamingServerNet.Flv/Contracts/IFlvClientInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace LiveStreamingServerNet.Flv.Contracts
{
/// <summary>
/// Provides information about an FLV client connection.
/// </summary>
public interface IFlvClientInfo
{
/// <summary>
/// Gets the unique identifier for the FLV client.
/// </summary>
string ClientId { get; }

/// <summary>
/// Gets the resolved path of the requested stream.
/// </summary>
string StreamPath { get; }

/// <summary>
/// Gets the arguments provided when the client requested the stream.
/// </summary>
IReadOnlyDictionary<string, string> StreamArguments { get; }

/// <summary>
/// Gets request details associated with the FLV client connection.
/// </summary>
IFlvRequest Request { get; }
}
}
101 changes: 101 additions & 0 deletions src/LiveStreamingServerNet.Flv/Contracts/IFlvRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Primitives;
using System.Net;
using System.Security.Cryptography.X509Certificates;

namespace LiveStreamingServerNet.Flv.Contracts
{
public interface IFlvRequest
{
/// <summary>
/// Gets or sets the HTTP request scheme.
/// </summary>
/// <returns>The HTTP request scheme.</returns>
string Scheme { get; }

/// <summary>
/// Returns true if the RequestScheme is https.
/// </summary>
/// <returns>true if this request is using https; otherwise, false.</returns>
bool IsHttps { get; }

/// <summary>
/// Gets the Host header. May include the port.
/// </summary>
/// <return>The Host header.</return>
HostString Host { get; }

/// <summary>
/// Gets the base path for the request. The path base should not end with a trailing slash.
/// </summary>
/// <returns>The base path for the request.</returns>
PathString PathBase { get; set; }

/// <summary>
/// Gets or sets the request path from RequestPath.
/// </summary>
/// <returns>The request path from RequestPath.</returns>
PathString Path { get; }

/// <summary>
/// Gets the raw query string used to create the query collection in Request.Query.
/// </summary>
/// <returns>The raw query string.</returns>
QueryString QueryString { get; }

/// <summary>
/// Gets the query value collection parsed from Request.QueryString.
/// </summary>
/// <returns>The query value collection parsed from Request.QueryString.</returns>
IQueryCollection Query { get; }

/// <summary>
/// Gets or sets the request protocol (e.g. HTTP/1.1).
/// </summary>
/// <returns>The request protocol.</returns>
string Protocol { get; set; }

/// <summary>
/// Gets the request headers.
/// </summary>
/// <returns>The request headers.</returns>
IReadOnlyDictionary<string, StringValues> Headers { get; }

/// <summary>
/// Gets the collection of Cookies for this request.
/// </summary>
/// <returns>The collection of Cookies for this request.</returns>
IRequestCookieCollection Cookies { get; }

/// <summary>
/// Gets the collection of route values for this request.
/// </summary>
/// <returns>The collection of route values for this request.</returns>
IReadOnlyDictionary<string, object?> RouteValues { get; }

/// <summary>
/// Gets the IP address of the remote target. Can be null.
/// </summary>
IPAddress? RemoteIpAddress { get; }

/// <summary>
/// Gets the port of the remote target.
/// </summary>
int RemotePort { get; set; }

/// <summary>
/// Gets the IP address of the local host.
/// </summary>
IPAddress? LocalIpAddress { get; }

/// <summary>
/// Gets the port of the local host.
/// </summary>
int LocalPort { get; }

/// <summary>
/// Gets the client certificate.
/// </summary>
X509Certificate2? ClientCertificate { get; }
}
}
31 changes: 31 additions & 0 deletions src/LiveStreamingServerNet.Flv/Contracts/IFlvStreamEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using LiveStreamingServerNet.Utilities.Contracts;

namespace LiveStreamingServerNet.Flv.Contracts
{
/// <summary>
/// Handles FLV stream playback events.
/// </summary>
public interface IFlvServerStreamEventHandler
{
/// <summary>
/// Gets the execution order of this handler. Lower numbers execute first.
/// </summary>
/// <returns>The order value, default is 0</returns>
int GetOrder() => 0;

/// <summary>
/// Called when a client begins playing/subscribing to a stream.
/// </summary>
/// <param name="context">The event context</param>
/// <param name="client">The client handle interface</param>
ValueTask OnFlvStreamSubscribedAsync(IEventContext context, IFlvClientHandle client);

/// <summary>
/// Called when a client stops playing/subscribing to a stream.
/// </summary>
/// <param name="context">The event context</param>
/// <param name="clientId">The ID of the client</param>
/// <param name="streamPath">The path of the stream</param>
ValueTask OnFlvStreamUnsubscribedAsync(IEventContext context, string clientId, string streamPath);
}
}
29 changes: 29 additions & 0 deletions src/LiveStreamingServerNet.Flv/Contracts/IFlvStreamInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace LiveStreamingServerNet.Flv.Contracts
{
/// <summary>
/// Provides information about an active FLV stream.
/// </summary>
public interface IFlvStreamInfo
{
/// <summary>
/// Gets the path identifier of the stream.
/// </summary>
string StreamPath { get; }

/// <summary>
/// Gets the arguments provided when the stream was published.
/// </summary>
IReadOnlyDictionary<string, string> StreamArguments { get; }

/// <summary>
/// Gets the stream metadata sent by the publisher, if any.
/// Contains information like video dimensions, framerate, etc.
/// </summary>
IReadOnlyDictionary<string, object>? MetaData { get; }

/// <summary>
/// Gets a list of client handle interfaces for all current subscribers/viewers.
/// </summary>
IReadOnlyList<IFlvClientHandle> Subscribers { get; }
}
}
27 changes: 27 additions & 0 deletions src/LiveStreamingServerNet.Flv/Contracts/IFlvStreamInfoManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace LiveStreamingServerNet.Flv.Contracts
{
/// <summary>
/// Manages and provides information about FLV streams.
/// </summary>
public interface IFlvStreamInfoManager
{
/// <summary>
/// Gets a list of all active stream paths.
/// </summary>
/// <returns>List of stream path identifiers</returns>
IList<string> GetStreamPaths();

/// <summary>
/// Gets information about all active streams.
/// </summary>
/// <returns>List of stream information objects</returns>
IList<IFlvStreamInfo> GetStreamInfos();

/// <summary>
/// Gets information about a specific stream.
/// </summary>
/// <param name="streamPath">The path identifier of the stream</param>
/// <returns>Stream information object if the stream exists, null otherwise</returns>
IFlvStreamInfo? GetStreamInfo(string streamPath);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LiveStreamingServerNet.Flv.Configurations;
using LiveStreamingServerNet.Flv.Contracts;

namespace LiveStreamingServerNet.Flv.Installer.Contracts
{
Expand All @@ -20,5 +21,18 @@ public interface IFlvConfigurator
/// <param name="configure">Action to modify the media streaming configuration.</param>
/// <returns>The configurator instance for method chaining.</returns>
IFlvConfigurator ConfigureMediaStreaming(Action<MediaStreamingConfiguration>? configure);

/// <summary>
/// Adds a stream event handler implementation.
/// </summary>
/// <typeparam name="TStreamEventHandler">Type of the stream event handler</typeparam>
IFlvConfigurator AddStreamEventHandler<TStreamEventHandler>()
where TStreamEventHandler : class, IFlvServerStreamEventHandler;

/// <summary>
/// Adds a stream event handler using a factory method.
/// </summary>
/// <param name="implementationFactory">Factory method to create the handler</param>
IFlvConfigurator AddStreamEventHandler(Func<IServiceProvider, IFlvServerStreamEventHandler> implementationFactory);
}
}
15 changes: 15 additions & 0 deletions src/LiveStreamingServerNet.Flv/Installer/FlvConfigurator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LiveStreamingServerNet.Flv.Configurations;
using LiveStreamingServerNet.Flv.Contracts;
using LiveStreamingServerNet.Flv.Installer.Contracts;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -28,5 +29,19 @@ public IFlvConfigurator ConfigureMediaStreaming(Action<MediaStreamingConfigurati

return this;
}

public IFlvConfigurator AddStreamEventHandler<TStreamEventHandler>()
where TStreamEventHandler : class, IFlvServerStreamEventHandler
{
_services.AddSingleton<IFlvServerStreamEventHandler, TStreamEventHandler>();
return this;
}

public IFlvConfigurator AddStreamEventHandler(Func<IServiceProvider, IFlvServerStreamEventHandler> implementationFactory)
{
_services.AddSingleton(implementationFactory);
return this;
}

}
}
28 changes: 17 additions & 11 deletions src/LiveStreamingServerNet.Flv/Installer/FlvInstaller.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using LiveStreamingServerNet.Flv.Configurations;
using LiveStreamingServerNet.Flv.Contracts;
using LiveStreamingServerNet.Flv.Installer.Contracts;
using LiveStreamingServerNet.Flv.Internal;
using LiveStreamingServerNet.Flv.Internal.Contracts;
using LiveStreamingServerNet.Flv.Internal.HttpClients;
using LiveStreamingServerNet.Flv.Internal.HttpClients.Contracts;
using LiveStreamingServerNet.Flv.Internal.MediaPacketDiscarders;
Expand Down Expand Up @@ -41,22 +44,25 @@ public static IRtmpServerConfigurator AddFlv(this IRtmpServerConfigurator config
var services = configurator.Services;

configurator.AddStreamEventHandler<RtmpServerStreamEventListener>()
.AddMediaMessageInterceptor<RtmpMediaMessageScraper>()
.AddMediaCachingInterceptor<RtmpMediaCacheScraper>();
.AddMediaMessageInterceptor<RtmpMediaMessageScraper>()
.AddMediaCachingInterceptor<RtmpMediaCacheScraper>();

services.AddSingleton<IFlvWriterFactory, FlvWriterFactory>()
.AddSingleton<IFlvClientFactory, FlvClientFactory>()
.AddSingleton<IFlvClientHandler, FlvClientHandler>()
.AddSingleton<IMediaPacketDiscarderFactory, MediaPacketDiscarderFactory>();
.AddSingleton<IFlvClientFactory, FlvClientFactory>()
.AddSingleton<IFlvClientHandler, FlvClientHandler>()
.AddSingleton<IMediaPacketDiscarderFactory, MediaPacketDiscarderFactory>();

services.AddSingleton<IHttpFlvClientFactory, HttpFlvClientFactory>();

services.AddSingleton<IWebSocketFlvClientFactory, WebSocketFlvClientFactory>();
services.AddSingleton<IHttpFlvClientFactory, HttpFlvClientFactory>()
.AddSingleton<IWebSocketFlvClientFactory, WebSocketFlvClientFactory>();

services.AddSingleton<IFlvMediaTagSenderService, FlvMediaTagSenderService>()
.AddSingleton<IFlvMediaTagCacherService, FlvMediaTagCacherService>()
.AddSingleton<IFlvStreamManagerService, FlvStreamManagerService>()
.AddSingleton<IFlvMediaTagBroadcasterService, FlvMediaTagBroadcasterService>();
.AddSingleton<IFlvMediaTagCacherService, FlvMediaTagCacherService>()
.AddSingleton<IFlvStreamManagerService, FlvStreamManagerService>()
.AddSingleton<IFlvMediaTagBroadcasterService, FlvMediaTagBroadcasterService>();

services.AddSingleton<IFlvServerStreamEventDispatcher, FlvServerStreamEventDispatcher>();

services.AddSingleton<IFlvStreamInfoManager, FlvStreamInfoManager>();

configure?.Invoke(new FlvConfigurator(services));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LiveStreamingServerNet.Rtmp.Utilities.Containers;
using LiveStreamingServerNet.Flv.Contracts;
using LiveStreamingServerNet.Rtmp.Utilities.Containers;
using LiveStreamingServerNet.Utilities.Buffers.Contracts;

namespace LiveStreamingServerNet.Flv.Internal.Contracts
Expand All @@ -7,6 +8,8 @@ internal interface IFlvClient : IAsyncDisposable
{
string ClientId { get; }
string StreamPath { get; }
IReadOnlyDictionary<string, string> StreamArguments { get; }
IFlvRequest Request { get; }
CancellationToken StoppingToken { get; }
void Stop();
void CompleteInitialization();
Expand Down
Loading