using MediaBrowser.Common.Net.Handlers; using MediaBrowser.Controller; using System; using System.ComponentModel.Composition; using System.IO; using System.Net; using System.Threading.Tasks; namespace MediaBrowser.Api.Streaming { /// /// Class HlsSegmentHandler /// [Export(typeof(IHttpServerHandler))] public class HlsSegmentHandler : BaseHandler { /// /// The segment file prefix /// public const string SegmentFilePrefix = "segment-"; /// /// Handleses the request. /// /// The request. /// true if XXXX, false otherwise public override bool HandlesRequest(HttpListenerRequest request) { const string url = "/api/" + SegmentFilePrefix; return request.Url.LocalPath.IndexOf(url, StringComparison.OrdinalIgnoreCase) != -1; } /// /// Writes the response to output stream. /// /// The stream. /// The response info. /// Length of the content. /// Task. /// /// protected override Task WriteResponseToOutputStream(Stream stream, ResponseInfo responseInfo, long? contentLength) { throw new NotImplementedException(); } /// /// Gets the response info. /// /// Task{ResponseInfo}. /// /// protected override Task GetResponseInfo() { throw new NotImplementedException(); } /// /// Processes the request. /// /// The CTX. /// Task. public override async Task ProcessRequest(HttpListenerContext ctx) { var path = Path.GetFileName(ctx.Request.Url.LocalPath); path = Path.Combine(Kernel.ApplicationPaths.FFMpegStreamCachePath, path); var playlistFilename = Path.GetFileNameWithoutExtension(path).Substring(SegmentFilePrefix.Length); playlistFilename = playlistFilename.Substring(0, playlistFilename.Length - 3); var playlistPath = Path.Combine(Path.GetDirectoryName(path), playlistFilename + ".m3u8"); Plugin.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls); try { await new StaticFileHandler(Kernel) { Path = path }.ProcessRequest(ctx).ConfigureAwait(false); } finally { Plugin.Instance.OnTranscodeEndRequest(playlistPath, TranscodingJobType.Hls); } } } }