usingAzure.Storage.Blobs;usingLiveStreamingServerNet;usingLiveStreamingServerNet.StreamProcessor;usingLiveStreamingServerNet.StreamProcessor.AzureBlobStorage.Installer;usingLiveStreamingServerNet.StreamProcessor.Hls;usingLiveStreamingServerNet.StreamProcessor.Hls.Contracts;usingLiveStreamingServerNet.StreamProcessor.Installer;usingLiveStreamingServerNet.Utilities.Contracts;usingMicrosoft.Extensions.Logging;usingSystem.Net;varoutputDir=Path.Combine(Directory.GetCurrentDirectory(),"output");newDirectoryInfo(outputDir).Create();varblobContainerClient=newBlobContainerClient(Environment.GetEnvironmentVariable("AZURE_BLOB_STORAGE_CONNECTION_STRING"),Environment.GetEnvironmentVariable("AZURE_BLOB_CONTAINER"));usingvarliveStreamingServer=LiveStreamingServerBuilder.Create().ConfigureRtmpServer(options=>options.AddStreamProcessor(options=>{options.AddHlsUploader(uploaderOptions=>{uploaderOptions.AddHlsStorageEventHandler<HlsStorageEventListener>();uploaderOptions.AddAzureBlobStorage(blobContainerClient);});}).AddHlsTransmuxer()).ConfigureLogging(options=>options.AddConsole()).Build();awaitliveStreamingServer.RunAsync(newIPEndPoint(IPAddress.Any,1935));publicclassHlsStorageEventListener:IHlsStorageEventHandler{privatereadonlyILogger_logger;publicHlsStorageEventListener(ILogger<HlsStorageEventListener>logger){_logger=logger;}publicTaskOnHlsFilesStoredAsync(IEventContexteventContext,StreamProcessingContextcontext,boolinitial,IReadOnlyList<StoredManifest>storedManifests,IReadOnlyList<StoredTsSegment>storedTsSegments){if(!initial)returnTask.CompletedTask;varmainManifestName=Path.GetFileName(context.OutputPath);varmainManifest=storedManifests.FirstOrDefault(x=>x.Name.Equals(mainManifestName));if(mainManifest!=default)_logger.LogInformation($"[{context.Identifier}] Main manifest {mainManifestName} stored at {mainManifest.Uri}");returnTask.CompletedTask;}publicTaskOnHlsFilesStoringCompleteAsync(IEventContexteventContext,StreamProcessingContextcontext){returnTask.CompletedTask;}}
This setup allows the server to receive RTMP streams, transmux them to HLS formats, and then upload the HLS files to Azure Blob Storage. Besides, the HlsStorageEventListener will log information about the stored files for monitoring purposes.
Step 3: Launch Your Live Streaming Server
To execute your live streaming server, run the following command:
dotnet run
Once a live stream is published to the live streaming server, the corresponding stream will be automatically uploaded to the Azure Blob Storage container.