Serving HLS Live Streams
LiveStreamingServerNet provides a seamless way to the transmux RTMP streams into HLS streams using the built-in HLS transmuxer, without the need for external tools like FFmpeg.
Quick Setup Guide for Your Live Streaming Server
This section will guide you through adding a HLS transmuxer to convert RTMP streams into HLS streams and serving the HLS live streams with ASP.NET Core.
Step 1: Initialize a New Project and Add Required Packages
Create an empty ASP.NET Core Web application and add the necessary packages using the following commands:
dotnet new web
dotnet add package LiveStreamingServerNet
dotnet add package LiveStreamingServerNet.StreamProcessor
dotnet add package LiveStreamingServerNet.StreamProcessor.AspNetCore
Step 2: Configure Your Live Streaming Server
Edit Program.cs
file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
This code sets up the server using LiveStreamingServerNet to listen on port 1935 for RTMP streams. Whenever an RTMP stream is published to the server, a HLS transmuxer will be created to convert the RTMP stream into an HLS stream, and its manifest will be stored as /hls-output/{streamPath}/output.m3u8
. Additionally, the HlsTransmuxerEventListener
is added to log out the transmuxer events. And finally, the HlsFilesMiddleware
is added to serve the generated HLS live streams at /hls/**
.
Step 3: Launch Your Live Streaming Server
Execute your live streaming server by running the following command:
dotnet run --urls="https://+:8080"
Now, when you publish a live stream to rtmp://localhost:1935/live/demo
, the HLS transmuxer will automatically convert it into HLS format. You can access the HLS stream by visiting https://localhost:8080/hls/live/demo/output.m3u8
. If you need to play the HLS stream in a browser, you generally need a JavaScript library such as hls.js or video.js.
Filtering Video and Audio Codecs
The HLS transmuxer doesn't transcode the RTMP streams but only changes the container format from RTMP to HLS, which uses the MPEG-TS format for its media segments. This process doesn't alter the actual video or audio data and is significantly less resource-intensive compared to transcoding.
Therefore, to ensure that transmuxing works as expected, it’s necessary to verify that the incoming RTMP stream contains elementary streams with the H.264 and AAC codecs.
You can add the video and codec filters like this:
1 2 3 4 5 6 7 8 |
|
Enabling HLS Transmuxer Conditionally
Sometimes, not all the RTMP streams require transmuxing into HLS streams. In such cases, you can provide an IStreamProcessorCondition
to the HlsTransmuxerConfiguration
in order to selectively enable the HLS Transmuxer.
For Example, you can implement an IStreamProcessorCondition
like this:
1 2 3 4 5 6 7 |
|
And add it to the HlsTransmuxerConfiguration
:
1 2 3 4 5 6 7 |
|
Now, only RTMP streams published with argument hls=true
(e.g. rtmp://localhost:1935/live/demo?hls=true
) will be transmuxed into HLS streams.