-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add LiveStreamingServerNet.HlsAuthenticationDemo
- Loading branch information
1 parent
36fdb01
commit 4b511f3
Showing
6 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
samples/LiveStreamingServerNet.HlsAuthenticationDemo/Dockerfile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base | ||
USER app | ||
WORKDIR /app | ||
EXPOSE 8080 | ||
EXPOSE 8081 | ||
EXPOSE 1935 | ||
|
||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build | ||
ARG BUILD_CONFIGURATION=Release | ||
WORKDIR /src | ||
COPY ["samples/LiveStreamingServerNet.HlsAuthenticationDemo/LiveStreamingServerNet.HlsAuthenticationDemo.csproj", "samples/LiveStreamingServerNet.HlsAuthenticationDemo/"] | ||
COPY ["src/LiveStreamingServerNet.StreamProcessor/LiveStreamingServerNet.StreamProcessor.csproj", "src/LiveStreamingServerNet.StreamProcessor/"] | ||
COPY ["src/LiveStreamingServerNet.Rtmp/LiveStreamingServerNet.Rtmp.csproj", "src/LiveStreamingServerNet.Rtmp/"] | ||
COPY ["src/LiveStreamingServerNet.Networking/LiveStreamingServerNet.Networking.csproj", "src/LiveStreamingServerNet.Networking/"] | ||
COPY ["src/LiveStreamingServerNet.Utilities/LiveStreamingServerNet.Utilities.csproj", "src/LiveStreamingServerNet.Utilities/"] | ||
COPY ["src/LiveStreamingServerNet/LiveStreamingServerNet.csproj", "src/LiveStreamingServerNet/"] | ||
RUN dotnet restore "./samples/LiveStreamingServerNet.HlsAuthenticationDemo/LiveStreamingServerNet.HlsAuthenticationDemo.csproj" | ||
COPY . . | ||
WORKDIR "/src/samples/LiveStreamingServerNet.HlsAuthenticationDemo" | ||
RUN dotnet build "./LiveStreamingServerNet.HlsAuthenticationDemo.csproj" -c $BUILD_CONFIGURATION -o /app/build | ||
|
||
FROM build AS publish | ||
ARG BUILD_CONFIGURATION=Release | ||
RUN dotnet publish "./LiveStreamingServerNet.HlsAuthenticationDemo.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false | ||
|
||
FROM base AS final | ||
WORKDIR /app | ||
COPY --from=publish /app/publish . | ||
ENTRYPOINT ["dotnet", "LiveStreamingServerNet.HlsAuthenticationDemo.dll"] |
27 changes: 27 additions & 0 deletions
27
...eamingServerNet.HlsAuthenticationDemo/LiveStreamingServerNet.HlsAuthenticationDemo.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<InvariantGlobalization>true</InvariantGlobalization> | ||
<IsPackable>false</IsPackable> | ||
<ServerGarbageCollection>false</ServerGarbageCollection> | ||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS> | ||
<DockerfileContext>..\..</DockerfileContext> | ||
<DockerfileRunArguments>-p 1935:1935 -p 8080:8080</DockerfileRunArguments> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.7" /> | ||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" /> | ||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\LiveStreamingServerNet.StreamProcessor.AspNetCore\LiveStreamingServerNet.StreamProcessor.AspNetCore.csproj" /> | ||
<ProjectReference Include="..\..\src\LiveStreamingServerNet.StreamProcessor\LiveStreamingServerNet.StreamProcessor.csproj" /> | ||
<ProjectReference Include="..\..\src\LiveStreamingServerNet\LiveStreamingServerNet.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
94 changes: 94 additions & 0 deletions
94
samples/LiveStreamingServerNet.HlsAuthenticationDemo/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
using LiveStreamingServerNet.Networking.Helpers; | ||
using LiveStreamingServerNet.Rtmp; | ||
using LiveStreamingServerNet.StreamProcessor.AspNetCore.Configurations; | ||
using LiveStreamingServerNet.StreamProcessor.AspNetCore.Installer; | ||
using LiveStreamingServerNet.StreamProcessor.Installer; | ||
using Microsoft.IdentityModel.Tokens; | ||
using System.Net; | ||
using System.Text; | ||
|
||
namespace LiveStreamingServerNet.HlsAuthenticationDemo | ||
{ | ||
public class Program | ||
{ | ||
public static async Task Main(string[] args) | ||
{ | ||
using var liveStreamingServer = CreateLiveStreamingServer(); | ||
|
||
var builder = WebApplication.CreateBuilder(args); | ||
|
||
builder.Services.AddBackgroundServer(liveStreamingServer, new IPEndPoint(IPAddress.Any, 1935)); | ||
|
||
builder.Services.AddCors(options => | ||
options.AddDefaultPolicy(policy => | ||
policy.AllowAnyHeader() | ||
.AllowAnyOrigin() | ||
.AllowAnyMethod() | ||
) | ||
); | ||
|
||
// | ||
builder.Services.AddAuthentication() | ||
.AddJwtBearer(options => | ||
{ | ||
options.TokenValidationParameters = new TokenValidationParameters | ||
{ | ||
ValidateIssuer = false, | ||
ValidateAudience = false, | ||
ValidateLifetime = false, | ||
ValidateIssuerSigningKey = true, | ||
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes( | ||
"THIS_IS_A_SUPER_SECRET_KEY_FOR_DEMO_PURPOSE_ONLY")), | ||
}; | ||
} | ||
); | ||
|
||
builder.Services.AddAuthorization(); | ||
|
||
var app = builder.Build(); | ||
|
||
app.UseAuthorization(); | ||
|
||
app.UseAuthentication(); | ||
|
||
app.UseCors(); | ||
|
||
// Given that the scheme is https, the port is 7138, and the stream path is live/demo, | ||
// the HLS stream will be available at https://localhost:7138/hls/live/demo/output.m3u8 | ||
app.UseHlsFiles(liveStreamingServer, new HlsServingOptions | ||
{ | ||
RequestPath = "/hls", | ||
|
||
// Use bearer token authentication for HLS files | ||
OnProcessRequestAsync = async (context) => | ||
{ | ||
if (context.Context.User.Identity?.IsAuthenticated != true) | ||
{ | ||
context.Context.Response.StatusCode = StatusCodes.Status401Unauthorized; | ||
await context.Context.Response.CompleteAsync(); | ||
context.Context.RequestServices | ||
.GetRequiredService<ILogger<Program>>() | ||
.LogWarning($"Unauthorized request for {context.StreamPath} from {context.Context.Connection.RemoteIpAddress}"); | ||
} | ||
} | ||
}); | ||
|
||
await app.RunAsync(); | ||
} | ||
|
||
private static ILiveStreamingServer CreateLiveStreamingServer() | ||
{ | ||
return LiveStreamingServerBuilder.Create() | ||
.ConfigureRtmpServer(options => options | ||
.Configure(options => options.EnableGopCaching = false) | ||
.AddVideoCodecFilter(builder => builder.Include(VideoCodec.AVC)) | ||
.AddAudioCodecFilter(builder => builder.Include(AudioCodec.AAC)) | ||
.AddStreamProcessor() | ||
.AddHlsTransmuxer() | ||
) | ||
.ConfigureLogging(options => options.AddConsole()) | ||
.Build(); | ||
} | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
samples/LiveStreamingServerNet.HlsAuthenticationDemo/Properties/launchSettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ | ||
"profiles": { | ||
"http": { | ||
"commandName": "Project", | ||
"launchUrl": "swagger", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"dotnetRunMessages": true, | ||
"applicationUrl": "http://localhost:5010" | ||
}, | ||
"https": { | ||
"commandName": "Project", | ||
"launchUrl": "swagger", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"dotnetRunMessages": true, | ||
"applicationUrl": "https://localhost:7138;http://localhost:5010" | ||
}, | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchUrl": "swagger", | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"Container (Dockerfile)": { | ||
"commandName": "Docker", | ||
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger", | ||
"environmentVariables": { | ||
"ASPNETCORE_HTTPS_PORTS": "8081", | ||
"ASPNETCORE_HTTP_PORTS": "8080" | ||
}, | ||
"publishAllPorts": true, | ||
"useSSL": true | ||
} | ||
}, | ||
"$schema": "http://json.schemastore.org/launchsettings.json", | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:40329", | ||
"sslPort": 44320 | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
samples/LiveStreamingServerNet.HlsAuthenticationDemo/appsettings.Development.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
samples/LiveStreamingServerNet.HlsAuthenticationDemo/appsettings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
"Microsoft.AspNetCore": "Warning" | ||
} | ||
}, | ||
"AllowedHosts": "*" | ||
} |