aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs')
-rw-r--r--MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs147
1 files changed, 147 insertions, 0 deletions
diff --git a/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs b/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs
new file mode 100644
index 000000000..f1fa86f78
--- /dev/null
+++ b/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs
@@ -0,0 +1,147 @@
+using MediaBrowser.Controller;
+using ServiceStack.ServiceHost;
+using System;
+using System.IO;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace MediaBrowser.Api.Playback.Hls
+{
+ /// <summary>
+ /// Class GetHlsAudioSegment
+ /// </summary>
+ [Route("/Audio/{Id}/hls/{SegmentId}/stream.mp3", "GET")]
+ [Route("/Audio/{Id}/hls/{SegmentId}/stream.aac", "GET")]
+ [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
+ public class GetHlsAudioSegment
+ {
+ /// <summary>
+ /// Gets or sets the id.
+ /// </summary>
+ /// <value>The id.</value>
+ public string Id { get; set; }
+
+ /// <summary>
+ /// Gets or sets the segment id.
+ /// </summary>
+ /// <value>The segment id.</value>
+ public string SegmentId { get; set; }
+ }
+
+ /// <summary>
+ /// Class GetHlsVideoSegment
+ /// </summary>
+ [Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.ts", "GET")]
+ [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
+ public class GetHlsVideoSegment
+ {
+ /// <summary>
+ /// Gets or sets the id.
+ /// </summary>
+ /// <value>The id.</value>
+ public string Id { get; set; }
+
+ public string PlaylistId { get; set; }
+
+ /// <summary>
+ /// Gets or sets the segment id.
+ /// </summary>
+ /// <value>The segment id.</value>
+ public string SegmentId { get; set; }
+ }
+
+ /// <summary>
+ /// Class GetHlsVideoSegment
+ /// </summary>
+ [Route("/Videos/{Id}/hls/{PlaylistId}/stream.m3u8", "GET")]
+ [Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
+ public class GetHlsPlaylist
+ {
+ /// <summary>
+ /// Gets or sets the id.
+ /// </summary>
+ /// <value>The id.</value>
+ public string Id { get; set; }
+
+ public string PlaylistId { get; set; }
+ }
+
+ public class HlsSegmentService : BaseApiService
+ {
+ private readonly IServerApplicationPaths _appPaths;
+
+ public HlsSegmentService(IServerApplicationPaths appPaths)
+ {
+ _appPaths = appPaths;
+ }
+
+ public object Get(GetHlsPlaylist request)
+ {
+ OnBeginRequest(request.PlaylistId);
+
+ var file = request.PlaylistId + Path.GetExtension(RequestContext.PathInfo);
+
+ file = Path.Combine(_appPaths.EncodedMediaCachePath, file);
+
+ return ResultFactory.GetStaticFileResult(RequestContext, file, FileShare.ReadWrite);
+ }
+
+ /// <summary>
+ /// Gets the specified request.
+ /// </summary>
+ /// <param name="request">The request.</param>
+ /// <returns>System.Object.</returns>
+ public object Get(GetHlsVideoSegment request)
+ {
+ var file = request.SegmentId + Path.GetExtension(RequestContext.PathInfo);
+
+ file = Path.Combine(_appPaths.EncodedMediaCachePath, file);
+
+ OnBeginRequest(request.PlaylistId);
+
+ return ResultFactory.GetStaticFileResult(RequestContext, file);
+ }
+
+ /// <summary>
+ /// Gets the specified request.
+ /// </summary>
+ /// <param name="request">The request.</param>
+ /// <returns>System.Object.</returns>
+ public object Get(GetHlsAudioSegment request)
+ {
+ var file = request.SegmentId + Path.GetExtension(RequestContext.PathInfo);
+
+ file = Path.Combine(_appPaths.EncodedMediaCachePath, file);
+
+ return ResultFactory.GetStaticFileResult(RequestContext, file, FileShare.ReadWrite);
+ }
+
+ /// <summary>
+ /// Called when [begin request].
+ /// </summary>
+ /// <param name="playlistId">The playlist id.</param>
+ protected void OnBeginRequest(string playlistId)
+ {
+ var normalizedPlaylistId = playlistId.Replace("-low", string.Empty);
+
+ foreach (var playlist in Directory.EnumerateFiles(_appPaths.EncodedMediaCachePath, "*.m3u8")
+ .Where(i => i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1)
+ .ToList())
+ {
+ ExtendPlaylistTimer(playlist);
+ }
+ }
+
+ private void ExtendPlaylistTimer(string playlist)
+ {
+ ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType.Hls);
+
+ Task.Run(async () =>
+ {
+ await Task.Delay(20000).ConfigureAwait(false);
+
+ ApiEntryPoint.Instance.OnTranscodeEndRequest(playlist, TranscodingJobType.Hls);
+ });
+ }
+ }
+}