aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/Streaming/HlsSegmentHandler.cs
blob: 4305fd8a79182134539de956e5bcb859e9905a13 (plain)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using MediaBrowser.Common.Net.Handlers;
using MediaBrowser.Controller;
using System;
using System.IO;
using System.Net;
using System.Threading.Tasks;

namespace MediaBrowser.Api.Streaming
{
    /// <summary>
    /// Class HlsSegmentHandler
    /// </summary>
    public class HlsSegmentHandler : BaseHandler<Kernel>
    {
        /// <summary>
        /// The segment file prefix
        /// </summary>
        public const string SegmentFilePrefix = "segment-";

        /// <summary>
        /// Handleses the request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
        public override bool HandlesRequest(HttpListenerRequest request)
        {
            const string url = "/api/" + SegmentFilePrefix;

            return request.Url.LocalPath.IndexOf(url, StringComparison.OrdinalIgnoreCase) != -1;
        }

        /// <summary>
        /// Writes the response to output stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="responseInfo">The response info.</param>
        /// <param name="contentLength">Length of the content.</param>
        /// <returns>Task.</returns>
        /// <exception cref="System.NotImplementedException"></exception>
        /// <exception cref="NotImplementedException"></exception>
        protected override Task WriteResponseToOutputStream(Stream stream, ResponseInfo responseInfo, long? contentLength)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the response info.
        /// </summary>
        /// <returns>Task{ResponseInfo}.</returns>
        /// <exception cref="System.NotImplementedException"></exception>
        /// <exception cref="NotImplementedException"></exception>
        protected override Task<ResponseInfo> GetResponseInfo()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Processes the request.
        /// </summary>
        /// <param name="ctx">The CTX.</param>
        /// <returns>Task.</returns>
        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);
            }
        }
    }
}