Files
jellyfin-jellyfin-1/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs

206 lines
7.6 KiB
C#
Raw Normal View History

2014-06-02 15:32:41 -04:00
using System.Threading;
using MediaBrowser.Common.IO;
2014-06-02 15:32:41 -04:00
using MediaBrowser.Common.Net;
2014-05-18 15:58:42 -04:00
using MediaBrowser.Controller.Channels;
using MediaBrowser.Controller.Configuration;
2014-03-25 01:25:03 -04:00
using MediaBrowser.Controller.Dlna;
2013-09-04 13:02:19 -04:00
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Library;
2013-12-21 13:37:34 -05:00
using MediaBrowser.Controller.LiveTv;
2014-02-20 11:37:41 -05:00
using MediaBrowser.Controller.MediaEncoding;
2013-12-05 22:39:44 -05:00
using MediaBrowser.Controller.Persistence;
2013-08-09 21:16:31 -04:00
using MediaBrowser.Model.IO;
2013-12-07 10:52:38 -05:00
using ServiceStack;
2013-07-02 14:24:29 -04:00
using System;
2014-01-13 00:41:00 -05:00
using System.IO;
using System.Linq;
2014-01-12 16:32:13 -05:00
using System.Threading.Tasks;
namespace MediaBrowser.Api.Playback.Hls
{
2013-04-29 12:01:23 -04:00
/// <summary>
/// Class GetHlsVideoStream
/// </summary>
[Route("/Videos/{Id}/stream.m3u8", "GET")]
2013-03-28 10:42:03 -04:00
[Api(Description = "Gets a video stream using HTTP live streaming.")]
public class GetHlsVideoStream : VideoStreamRequest
{
2013-09-06 13:27:06 -04:00
[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; }
2013-09-06 13:27:06 -04:00
[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; }
}
2014-01-13 00:41:00 -05: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.")]
2014-01-23 13:05:41 -05:00
public class GetHlsVideoSegment : VideoStreamRequest
2014-01-13 00:41:00 -05:00
{
public string PlaylistId { get; set; }
/// <summary>
/// Gets or sets the segment id.
/// </summary>
/// <value>The segment id.</value>
public string SegmentId { get; set; }
}
2013-04-29 12:01:23 -04:00
/// <summary>
/// Class VideoHlsService
/// </summary>
public class VideoHlsService : BaseHlsService
{
2014-06-11 15:31:33 -04:00
public VideoHlsService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, ILiveTvManager liveTvManager, IDlnaManager dlnaManager, IChannelManager channelManager, ISubtitleEncoder subtitleEncoder) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, liveTvManager, dlnaManager, channelManager, subtitleEncoder)
{
}
2014-01-13 00:41:00 -05:00
/// <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(Request.PathInfo);
file = Path.Combine(ServerConfigurationManager.ApplicationPaths.TranscodingTempPath, file);
OnBeginRequest(request.PlaylistId);
return ResultFactory.GetStaticFileResult(Request, file);
}
2014-01-23 13:05:41 -05:00
/// <summary>
/// Called when [begin request].
/// </summary>
/// <param name="playlistId">The playlist id.</param>
protected void OnBeginRequest(string playlistId)
2014-01-12 16:32:13 -05:00
{
2014-01-23 13:05:41 -05:00
var normalizedPlaylistId = playlistId.Replace("-low", string.Empty);
2014-01-12 16:32:13 -05:00
2014-01-23 13:05:41 -05:00
foreach (var playlist in Directory.EnumerateFiles(ServerConfigurationManager.ApplicationPaths.TranscodingTempPath, "*.m3u8")
.Where(i => i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1)
.ToList())
2014-01-12 16:32:13 -05:00
{
2014-01-23 13:05:41 -05:00
ExtendPlaylistTimer(playlist);
2014-01-12 16:32:13 -05:00
}
}
2014-01-23 13:05:41 -05:00
private async void ExtendPlaylistTimer(string playlist)
2014-01-12 16:32:13 -05:00
{
2014-01-23 13:05:41 -05:00
ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlist, TranscodingJobType.Hls);
2014-01-12 16:32:13 -05:00
2014-01-23 13:05:41 -05:00
await Task.Delay(20000).ConfigureAwait(false);
2014-01-12 16:32:13 -05:00
2014-01-23 13:05:41 -05:00
ApiEntryPoint.Instance.OnTranscodeEndRequest(playlist, TranscodingJobType.Hls);
2014-01-12 16:32:13 -05:00
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetHlsVideoStream request)
{
return ProcessRequest(request);
}
2013-04-29 12:01:23 -04:00
/// <summary>
/// Gets the audio arguments.
/// </summary>
/// <param name="state">The state.</param>
/// <returns>System.String.</returns>
protected override string GetAudioArguments(StreamState state)
{
var codec = state.OutputAudioCodec;
2013-03-20 10:27:56 -04:00
if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
{
return "-codec:a:0 copy";
}
var args = "-codec:a:0 " + codec;
if (state.AudioStream != null)
{
var channels = state.OutputAudioChannels;
if (channels.HasValue)
{
args += " -ac " + channels.Value;
}
var bitrate = state.OutputAudioBitrate;
2013-08-29 22:13:58 -04:00
if (bitrate.HasValue)
{
2013-08-29 22:13:58 -04:00
args += " -ab " + bitrate.Value.ToString(UsCulture);
}
2014-01-10 08:52:01 -05:00
args += " " + GetAudioFilterParam(state, true);
2013-03-20 10:27:56 -04:00
return args;
}
return args;
}
/// <summary>
/// Gets the video arguments.
/// </summary>
/// <param name="state">The state.</param>
/// <returns>System.String.</returns>
2014-06-10 13:36:06 -04:00
protected override string GetVideoArguments(StreamState state)
{
var codec = state.OutputVideoCodec;
// See if we can save come cpu cycles by avoiding encoding
if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
{
return IsH264(state.VideoStream) ? "-codec:v:0 copy -bsf h264_mp4toannexb" : "-codec:v:0 copy";
}
2014-03-03 23:58:19 -05:00
var keyFrameArg = state.ReadInputAtNativeFramerate ?
" -force_key_frames expr:if(isnan(prev_forced_t),gte(t,.1),gte(t,prev_forced_t+1))" :
" -force_key_frames expr:if(isnan(prev_forced_t),gte(t,.1),gte(t,prev_forced_t+5))";
var hasGraphicalSubs = state.SubtitleStream != null && state.SubtitleStream.IsGraphicalSubtitleStream;
2013-12-05 22:39:44 -05:00
2014-02-02 09:47:00 -05:00
var args = "-codec:v:0 " + codec + " " + GetVideoQualityParam(state, "libx264", true) + keyFrameArg;
2013-12-05 22:39:44 -05:00
// Add resolution params, if specified
if (!hasGraphicalSubs)
{
if (state.VideoRequest.Width.HasValue || state.VideoRequest.Height.HasValue || state.VideoRequest.MaxHeight.HasValue || state.VideoRequest.MaxWidth.HasValue)
{
2014-06-10 13:36:06 -04:00
args += GetOutputSizeParam(state, codec, CancellationToken.None);
}
}
// This is for internal graphical subs
if (hasGraphicalSubs)
2013-03-23 23:56:01 -04:00
{
args += GetInternalGraphicalSubtitleParam(state, codec);
2013-03-23 23:56:01 -04:00
}
2013-12-05 22:39:44 -05:00
return args;
}
/// <summary>
/// Gets the segment file extension.
/// </summary>
/// <param name="state">The state.</param>
/// <returns>System.String.</returns>
protected override string GetSegmentFileExtension(StreamState state)
{
return ".ts";
}
}
}