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
60 changes: 31 additions & 29 deletions docs/tutorials/adaptive-hls.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,37 @@ var outputDirectory = Path.Combine(Directory.GetCurrentDirectory(), "output");
using var liveStreamingServer = LiveStreamingServerBuilder.Create()
.ConfigureRtmpServer(options => options
.AddStreamProcessor()
.AddAdaptiveHlsTranscoder(config =>
{
config.FFmpegPath = ExecutableFinder.FindExecutableFromPATH("ffmpeg")!;
config.FFprobePath = ExecutableFinder.FindExecutableFromPATH("ffprobe")!;
config.OutputPathResolver = new HlsOutputPathResolver(outputDirectory);

config.DownsamplingFilters = new DownsamplingFilter[]{
new DownsamplingFilter(
Name: "360p",
Height: 360,
MaxVideoBitrate: "600k",
MaxAudioBitrate: "64k"
),

new DownsamplingFilter(
Name: "480p",
Height: 480,
MaxVideoBitrate: "1500k",
MaxAudioBitrate: "128k"
),

new DownsamplingFilter(
Name: "720p",
Height: 720,
MaxVideoBitrate: "3000k",
MaxAudioBitrate: "256k"
)
};
})
.AddAdaptiveHlsTranscoder(configure =>
configure.ConfigureDefault(config =>
{
config.FFmpegPath = ExecutableFinder.FindExecutableFromPATH("ffmpeg")!;
config.FFprobePath = ExecutableFinder.FindExecutableFromPATH("ffprobe")!;
config.OutputPathResolver = new HlsOutputPathResolver(outputDirectory);

config.DownsamplingFilters = new DownsamplingFilter[]{
new DownsamplingFilter(
Name: "360p",
Height: 360,
MaxVideoBitrate: "600k",
MaxAudioBitrate: "64k"
),

new DownsamplingFilter(
Name: "480p",
Height: 480,
MaxVideoBitrate: "1500k",
MaxAudioBitrate: "128k"
),

new DownsamplingFilter(
Name: "720p",
Height: 720,
MaxVideoBitrate: "3000k",
MaxAudioBitrate: "256k"
)
};
})
)
)
.ConfigureLogging(options => options.AddConsole())
.Build();
Expand Down
102 changes: 67 additions & 35 deletions samples/LiveStreamingServerNet.AdaptiveHlsDemo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public static async Task Main(string[] args)
builder.Services.AddCors(options =>
options.AddDefaultPolicy(policy =>
policy.AllowAnyHeader()
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyOrigin()
.AllowAnyMethod()
)
);

Expand All @@ -47,42 +47,74 @@ private static IServiceCollection AddLiveStreamingServer(this IServiceCollection
new StreamProcessorEventListener(
svc.GetRequiredService<ILogger<StreamProcessorEventListener>>()));
})
.AddAdaptiveHlsTranscoder(options =>
{
options.FFmpegPath = ExecutableFinder.FindExecutableFromPATH("ffmpeg")!;
options.FFprobePath = ExecutableFinder.FindExecutableFromPATH("ffprobe")!;

options.DownsamplingFilters =
[
new DownsamplingFilter(
Name: "360p",
Height: 360,
MaxVideoBitrate: "600k",
MaxAudioBitrate: "64k"
),

new DownsamplingFilter(
Name: "480p",
Height: 480,
MaxVideoBitrate: "1500k",
MaxAudioBitrate: "128k"
),

new DownsamplingFilter(
Name: "720p",
Height: 720,
MaxVideoBitrate: "3000k",
MaxAudioBitrate: "256k"
)
];

// Hardware acceleration
// options.VideoDecodingArguments = "-hwaccel auto -c:v h264_cuvid";
// options.VideoEncodingArguments = "-c:v h264_nvenc -g 30";
})
.AddAdaptiveHlsTranscoder(configure =>
configure.ConfigureDefault(config =>
{
config.FFmpegPath = ExecutableFinder.FindExecutableFromPATH("ffmpeg")!;
config.FFprobePath = ExecutableFinder.FindExecutableFromPATH("ffprobe")!;

config.DownsamplingFilters =
[
new DownsamplingFilter(
Name: "360p",
Height: 360,
MaxVideoBitrate: "600k",
MaxAudioBitrate: "64k"
),

new DownsamplingFilter(
Name: "480p",
Height: 480,
MaxVideoBitrate: "1500k",
MaxAudioBitrate: "128k"
),

new DownsamplingFilter(
Name: "720p",
Height: 720,
MaxVideoBitrate: "3000k",
MaxAudioBitrate: "256k"
)
];

// Hardware acceleration
// options.VideoDecodingArguments = "-hwaccel auto -c:v h264_cuvid";
// options.VideoEncodingArguments = "-c:v h264_nvenc -g 30";
}).UseConfigurationResolver(new CustomAdaptiveHlsTranscoderConfigurationResolver())
)
);
}

private class CustomAdaptiveHlsTranscoderConfigurationResolver : IAdaptiveHlsTranscoderConfigurationResolver
{
public ValueTask<AdaptiveHlsTranscoderConfiguration?> ResolveAsync(
IServiceProvider services, Guid contextIdentifier, string streamPath, IReadOnlyDictionary<string, string> streamArguments)
{
if (!streamArguments.ContainsKey("lowquality"))
{
return ValueTask.FromResult<AdaptiveHlsTranscoderConfiguration?>(null);
}

var config = new AdaptiveHlsTranscoderConfiguration
{
FFmpegPath = ExecutableFinder.FindExecutableFromPATH("ffmpeg")!,
FFprobePath = ExecutableFinder.FindExecutableFromPATH("ffprobe")!,

DownsamplingFilters =
[
new DownsamplingFilter(
Name: "360p",
Height: 360,
MaxVideoBitrate: "600k",
MaxAudioBitrate: "64k"
)
]
};

return ValueTask.FromResult<AdaptiveHlsTranscoderConfiguration?>(config);
}
}

private class StreamProcessorEventListener : IStreamProcessorEventHandler
{
private readonly ILogger _logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public static async Task Main(string[] args)
builder.Services.AddCors(options =>
options.AddDefaultPolicy(policy =>
policy.AllowAnyHeader()
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyOrigin()
.AllowAnyMethod()
)
);

Expand All @@ -40,64 +40,67 @@ private static IServiceCollection AddLiveStreamingServer(this IServiceCollection
options => options
.Configure(options => options.EnableGopCaching = false)
.AddStreamProcessor()
.AddAdaptiveHlsTranscoder(options =>
{
options.FFmpegPath = ExecutableFinder.FindExecutableFromPATH("ffmpeg")!;
options.FFprobePath = ExecutableFinder.FindExecutableFromPATH("ffprobe")!;
.AddAdaptiveHlsTranscoder(configure =>
configure.ConfigureDefault(config =>
{
config.FFmpegPath = ExecutableFinder.FindExecutableFromPATH("ffmpeg")!;
config.FFprobePath = ExecutableFinder.FindExecutableFromPATH("ffprobe")!;

// Add an icon as the second input to the FFmpeg command
options.AdditionalInputs = [Path.Combine(Directory.GetCurrentDirectory(), "icon.png")];
// Add an icon as the second input to the FFmpeg command
config.AdditionalInputs = [Path.Combine(Directory.GetCurrentDirectory(), "icon.png")];

// Add custom complex filters to scale the icon to different sizes
options.AdditionalComplexFilters = [
"[1:v]scale=-2:64[icon360]",
"[1:v]scale=-2:85[icon480]",
"[1:v]scale=-2:128[icon720]",
"[1:v]scale=-2:128[icon720t]"
];
// Add custom complex filters to scale the icon to different sizes
config.AdditionalComplexFilters =
[
"[1:v]scale=-2:64[icon360]",
"[1:v]scale=-2:85[icon480]",
"[1:v]scale=-2:128[icon720]",
"[1:v]scale=-2:128[icon720t]"
];

// Add custom audio filters to reduce the volume by half
options.AudioFilters = ["volume=0.5"];
// Add custom audio filters to reduce the volume by half
config.AudioFilters = ["volume=0.5"];

options.DownsamplingFilters =
[
new DownsamplingFilter(
Name: "360p",
Height: 360,
MaxVideoBitrate: "600k",
MaxAudioBitrate: "64k",
// Overlay the icon360 on the video
VideoFilter: ["[icon360]overlay"]
),
config.DownsamplingFilters =
[
new DownsamplingFilter(
Name: "360p",
Height: 360,
MaxVideoBitrate: "600k",
MaxAudioBitrate: "64k",
// Overlay the icon360 on the video
VideoFilter: ["[icon360]overlay"]
),

new DownsamplingFilter(
Name: "480p",
Height: 480,
MaxVideoBitrate: "1500k",
MaxAudioBitrate: "128k",
// Overlay the icon480 on the video
VideoFilter: ["[icon480]overlay"]
),
new DownsamplingFilter(
Name: "480p",
Height: 480,
MaxVideoBitrate: "1500k",
MaxAudioBitrate: "128k",
// Overlay the icon480 on the video
VideoFilter: ["[icon480]overlay"]
),

new DownsamplingFilter(
Name: "720p",
Height: 720,
MaxVideoBitrate: "3000k",
MaxAudioBitrate: "256k",
// Overlay the icon720 on the video
VideoFilter: ["[icon720]overlay"]
),
new DownsamplingFilter(
Name: "720p",
Height: 720,
MaxVideoBitrate: "3000k",
MaxAudioBitrate: "256k",
// Overlay the icon720 on the video
VideoFilter: ["[icon720]overlay"]
),

new DownsamplingFilter(
Name: "720p_rotated",
Height: 720,
MaxVideoBitrate: "3000k",
MaxAudioBitrate: "256k",
// Overlay the icon720t on the video, and rotate the video 90 degrees
VideoFilter: ["[icon720t]overlay", "transpose=1"]
),
];
})
new DownsamplingFilter(
Name: "720p_rotated",
Height: 720,
MaxVideoBitrate: "3000k",
MaxAudioBitrate: "256k",
// Overlay the icon720t on the video, and rotate the video 90 degrees
VideoFilter: ["[icon720t]overlay", "transpose=1"]
),
];
})
)
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using LiveStreamingServerNet.StreamProcessor.Hls.Configurations;

namespace LiveStreamingServerNet.StreamProcessor.Contracts
{
/// <summary>
/// Defines a contract for resolving adaptive HLS transcoder configurations based on stream context.
/// </summary>
public interface IAdaptiveHlsTranscoderConfigurationResolver
{
/// <summary>
/// Resolves the adaptive HLS transcoder configuration based on the provided stream path and arguments.
/// </summary>
/// <param name="services">Service provider for accessing dependencies</param>
/// <param name="contextIdentifier">Unique identifier for the processing context</param>
/// <param name="streamPath">Original path of the stream being processed</param>
/// <param name="streamArguments">Additional arguments associated with the stream</param>
/// <returns>The resolved adaptive HLS transcoder configuration, or null to use the default configuration</returns>
ValueTask<AdaptiveHlsTranscoderConfiguration?> ResolveAsync(IServiceProvider services, Guid contextIdentifier, string streamPath, IReadOnlyDictionary<string, string> streamArguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,6 @@ public class AdaptiveHlsTranscoderConfiguration
/// Default: 30 seconds.
/// </summary>
public TimeSpan? CleanupDelay { get; set; } = TimeSpan.FromSeconds(30);

/// <summary>
/// Default output path resolver that creates paths in a subdirectory of the current working directory.
/// Path format: "./output/{contextIdentifier}/output.m3u8".
/// </summary>
public static Task<string> DefaultOutputPathResolver(Guid contextIdentifier, string streamPath, IReadOnlyDictionary<string, string> streamArguments)
{
return Task.FromResult(Path.Combine(Directory.GetCurrentDirectory(), "output", contextIdentifier.ToString(), "output.m3u8"));
}
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using LiveStreamingServerNet.StreamProcessor.Contracts;
using LiveStreamingServerNet.StreamProcessor.Hls.Configurations;
using LiveStreamingServerNet.StreamProcessor.Installer.Contracts;

namespace LiveStreamingServerNet.StreamProcessor.Installer
{
public class AdaptiveHlsTranscoderConfigurator : IAdaptiveHlsTranscoderConfigurator
{
private readonly AdaptiveHlsTranscoderConfiguratorContext _context;

public AdaptiveHlsTranscoderConfigurator(AdaptiveHlsTranscoderConfiguratorContext context)
{
_context = context;
}

public IAdaptiveHlsTranscoderConfigurator ConfigureDefault(Action<AdaptiveHlsTranscoderConfiguration> configure)
{
configure.Invoke(_context.Configuration);
return this;
}

public IAdaptiveHlsTranscoderConfigurator UseConfigurationResolver(IAdaptiveHlsTranscoderConfigurationResolver resolver)
{
_context.ConfigurationResolver = resolver;
return this;
}
}

public class AdaptiveHlsTranscoderConfiguratorContext
{
public AdaptiveHlsTranscoderConfiguration Configuration { get; }
public IAdaptiveHlsTranscoderConfigurationResolver? ConfigurationResolver { get; set; }

public AdaptiveHlsTranscoderConfiguratorContext(AdaptiveHlsTranscoderConfiguration configuration)
{
Configuration = configuration;
}
}
}
Loading