jellyfin/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs

175 lines
6.6 KiB
C#
Raw Normal View History

using MediaBrowser.Controller;
2015-03-25 18:29:21 +00:00
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Net;
2013-12-07 15:52:38 +00:00
using ServiceStack;
2015-03-25 18:29:21 +00:00
using System;
using System.IO;
2015-03-25 18:29:21 +00:00
using System.Linq;
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.")]
2015-05-24 18:33:28 +00:00
public class GetHlsAudioSegmentLegacy
{
2015-05-24 18:33:28 +00:00
// TODO: Deprecate with new iOS app
/// <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; }
}
2015-05-24 18:33:28 +00:00
/// <summary>
/// Class GetHlsVideoStream
/// </summary>
[Route("/Videos/{Id}/stream.m3u8", "GET")]
[Api(Description = "Gets a video stream using HTTP live streaming.")]
public class GetHlsVideoStreamLegacy : VideoStreamRequest
{
// TODO: Deprecate with new iOS app
[ApiMember(Name = "BaselineStreamAudioBitRate", Description = "Optional. Specify the audio bitrate for the baseline stream.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? BaselineStreamAudioBitRate { get; set; }
[ApiMember(Name = "AppendBaselineStream", Description = "Optional. Whether or not to include a baseline audio-only stream in the master playlist.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool AppendBaselineStream { get; set; }
[ApiMember(Name = "TimeStampOffsetMs", Description = "Optional. Alter the timestamps in the playlist by a given amount, in ms. Default is 1000.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int TimeStampOffsetMs { 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.")]
2015-05-24 18:33:28 +00:00
public class GetHlsPlaylistLegacy
{
2015-03-25 18:29:21 +00:00
// TODO: Deprecate with new iOS app
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public string Id { get; set; }
public string PlaylistId { get; set; }
}
2013-10-03 15:24:32 +00:00
[Route("/Videos/ActiveEncodings", "DELETE")]
2013-10-03 14:14:40 +00:00
[Api(Description = "Stops an encoding process")]
public class StopEncodingProcess
{
[ApiMember(Name = "DeviceId", Description = "The device id of the client requesting. Used to stop encoding processes when needed.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
public string DeviceId { get; set; }
2015-03-19 16:16:33 +00:00
2015-03-29 18:31:28 +00:00
[ApiMember(Name = "PlaySessionId", Description = "The play session id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
public string PlaySessionId { get; set; }
2013-10-03 14:14:40 +00:00
}
2015-03-25 18:29:21 +00:00
/// <summary>
/// Class GetHlsVideoSegment
/// </summary>
[Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.ts", "GET")]
[Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
2015-05-24 18:33:28 +00:00
public class GetHlsVideoSegmentLegacy : VideoStreamRequest
2015-03-25 18:29:21 +00:00
{
2015-05-24 18:33:28 +00:00
// TODO: Deprecate with new iOS app
2015-03-25 18:29:21 +00:00
public string PlaylistId { get; set; }
/// <summary>
/// Gets or sets the segment id.
/// </summary>
/// <value>The segment id.</value>
public string SegmentId { get; set; }
}
public class HlsSegmentService : BaseApiService
{
private readonly IServerApplicationPaths _appPaths;
2015-03-25 18:29:21 +00:00
private readonly IServerConfigurationManager _config;
2015-03-25 18:29:21 +00:00
public HlsSegmentService(IServerApplicationPaths appPaths, IServerConfigurationManager config)
{
_appPaths = appPaths;
2015-03-25 18:29:21 +00:00
_config = config;
}
2015-05-24 18:33:28 +00:00
public object Get(GetHlsPlaylistLegacy request)
{
2013-12-07 15:52:38 +00:00
var file = request.PlaylistId + Path.GetExtension(Request.PathInfo);
2015-01-17 04:29:53 +00:00
file = Path.Combine(_appPaths.TranscodingTempPath, file);
2015-03-25 18:29:21 +00:00
return GetFileResult(file, file);
}
2013-10-03 14:14:40 +00:00
public void Delete(StopEncodingProcess request)
{
2015-03-29 18:31:28 +00:00
ApiEntryPoint.Instance.KillTranscodingJobs(request.DeviceId, request.PlaySessionId, path => true);
2013-10-03 14:14:40 +00:00
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2015-05-24 18:33:28 +00:00
public object Get(GetHlsVideoSegmentLegacy request)
{
2013-12-07 15:52:38 +00:00
var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
2015-03-25 18:29:21 +00:00
file = Path.Combine(_config.ApplicationPaths.TranscodingTempPath, file);
var normalizedPlaylistId = request.PlaylistId.Replace("-low", string.Empty);
var playlistPath = Directory.EnumerateFiles(_config.ApplicationPaths.TranscodingTempPath, "*")
.FirstOrDefault(i => string.Equals(Path.GetExtension(i), ".m3u8", StringComparison.OrdinalIgnoreCase) && i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1);
return GetFileResult(file, playlistPath);
}
2015-03-25 18:29:21 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2015-05-24 18:33:28 +00:00
public object Get(GetHlsAudioSegmentLegacy request)
2015-03-25 18:29:21 +00:00
{
// TODO: Deprecate with new iOS app
var file = request.SegmentId + Path.GetExtension(Request.PathInfo);
2015-01-17 04:29:53 +00:00
file = Path.Combine(_appPaths.TranscodingTempPath, file);
2013-12-07 15:52:38 +00:00
return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite);
}
2015-03-25 18:29:21 +00:00
private object GetFileResult(string path, string playlistPath)
{
var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls);
return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions
{
Path = path,
FileShare = FileShare.ReadWrite,
OnComplete = () =>
{
if (transcodingJob != null)
{
ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);
}
}
});
}
}
}