jellyfin-server/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs

126 lines
4.6 KiB
C#
Raw Normal View History

using MediaBrowser.Controller.Configuration;
2015-01-20 05:19:13 +00:00
using MediaBrowser.Controller.Devices;
2014-03-25 05:25:03 +00:00
using MediaBrowser.Controller.Dlna;
using MediaBrowser.Controller.Library;
2014-02-20 16:37:41 +00:00
using MediaBrowser.Controller.MediaEncoding;
2013-08-10 01:16:31 +00:00
using MediaBrowser.Model.IO;
2015-05-04 17:44:25 +00:00
using MediaBrowser.Model.Serialization;
2013-07-02 18:24:29 +00:00
using System;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.IO;
2016-11-10 14:41:24 +00:00
using MediaBrowser.Controller.Net;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.IO;
2016-03-07 18:50:58 +00:00
using MediaBrowser.Model.Dlna;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.Services;
namespace MediaBrowser.Api.Playback.Hls
{
2014-07-01 21:13:32 +00:00
[Route("/Videos/{Id}/live.m3u8", "GET")]
public class GetLiveHlsStream : VideoStreamRequest
{
}
2014-09-18 04:50:21 +00:00
2013-04-29 16:01:23 +00:00
/// <summary>
/// Class VideoHlsService
/// </summary>
public class VideoHlsService : BaseHlsService
{
2014-07-01 21:13:32 +00:00
public object Get(GetLiveHlsStream request)
{
return ProcessRequest(request, true);
}
2013-04-29 16:01:23 +00: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 = EncodingHelper.GetAudioEncoder(state);
2013-03-20 14:27:56 +00:00
2015-06-03 03:15:46 +00:00
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
{
return "-codec:a:0 copy";
}
var args = "-codec:a:0 " + codec;
2014-06-23 16:05:19 +00:00
var channels = state.OutputAudioChannels;
2014-06-23 16:05:19 +00:00
if (channels.HasValue)
{
args += " -ac " + channels.Value;
}
2014-06-23 16:05:19 +00:00
var bitrate = state.OutputAudioBitrate;
2013-03-20 14:27:56 +00:00
2014-06-23 16:05:19 +00:00
if (bitrate.HasValue)
{
args += " -ab " + bitrate.Value.ToString(UsCulture);
}
args += " " + EncodingHelper.GetAudioFilterParam(state, ApiEntryPoint.Instance.GetEncodingOptions(), true);
2014-06-23 16:05:19 +00:00
return args;
}
/// <summary>
/// Gets the video arguments.
/// </summary>
/// <param name="state">The state.</param>
/// <returns>System.String.</returns>
2014-06-10 17:36:06 +00:00
protected override string GetVideoArguments(StreamState state)
{
var codec = EncodingHelper.GetVideoEncoder(state, ApiEntryPoint.Instance.GetEncodingOptions());
2014-12-26 17:45:06 +00:00
var args = "-codec:v:0 " + codec;
if (state.EnableMpegtsM2TsMode)
{
args += " -mpegts_m2ts_mode 1";
}
// See if we can save come cpu cycles by avoiding encoding
if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
{
2016-04-14 19:12:00 +00:00
// if h264_mp4toannexb is ever added, do not use it for live tv
if (state.VideoStream != null && EncodingHelper.IsH264(state.VideoStream) && !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase))
2016-04-18 03:50:44 +00:00
{
args += " -bsf:v h264_mp4toannexb";
}
2016-11-16 20:52:17 +00:00
args += " -flags -global_header";
2016-04-14 19:12:00 +00:00
return args;
}
2016-04-14 19:12:00 +00:00
2015-08-09 16:35:27 +00:00
var keyFrameArg = string.Format(" -force_key_frames \"expr:gte(t,n_forced*{0})\"",
2014-06-26 17:04:11 +00:00
state.SegmentLength.ToString(UsCulture));
2016-03-07 18:50:58 +00:00
var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.VideoRequest.SubtitleMethod == SubtitleDeliveryMethod.Encode;
2013-12-06 03:39:44 +00:00
var encodingOptions = ApiEntryPoint.Instance.GetEncodingOptions();
2017-03-15 19:57:18 +00:00
args += " " + EncodingHelper.GetVideoQualityParam(state, codec, encodingOptions, GetDefaultH264Preset()) + keyFrameArg;
2013-12-06 03:39:44 +00:00
// Add resolution params, if specified
if (!hasGraphicalSubs)
{
args += EncodingHelper.GetOutputSizeParam(state, codec);
}
// This is for internal graphical subs
if (hasGraphicalSubs)
2013-03-24 03:56:01 +00:00
{
args += EncodingHelper.GetGraphicalSubtitleParam(state, codec);
2013-03-24 03:56:01 +00:00
}
2013-12-06 03:39:44 +00:00
2016-11-16 20:52:17 +00:00
args += " -flags -global_header";
2016-09-20 19:42:53 +00:00
return args;
}
2016-11-10 14:41:24 +00:00
public VideoHlsService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IFileSystem fileSystem, IDlnaManager dlnaManager, ISubtitleEncoder subtitleEncoder, IDeviceManager deviceManager, IMediaSourceManager mediaSourceManager, IZipClient zipClient, IJsonSerializer jsonSerializer, IAuthorizationContext authorizationContext) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext)
{
}
}
}