2019-01-13 19:24:58 +00:00
|
|
|
using System;
|
2019-06-05 18:59:06 +00:00
|
|
|
using System.Globalization;
|
2019-02-02 22:55:47 +00:00
|
|
|
using System.Threading.Tasks;
|
2018-12-14 09:40:55 +00:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Devices;
|
|
|
|
using MediaBrowser.Controller.Dlna;
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
|
|
|
using MediaBrowser.Controller.Net;
|
|
|
|
using MediaBrowser.Model.Configuration;
|
|
|
|
using MediaBrowser.Model.Dlna;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2018-12-14 09:40:55 +00:00
|
|
|
using MediaBrowser.Model.Services;
|
2019-11-17 22:05:39 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Api.Playback.Hls
|
|
|
|
{
|
|
|
|
[Route("/Videos/{Id}/live.m3u8", "GET")]
|
|
|
|
public class GetLiveHlsStream : VideoStreamRequest
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Class VideoHlsService
|
|
|
|
/// </summary>
|
|
|
|
[Authenticated]
|
|
|
|
public class VideoHlsService : BaseHlsService
|
|
|
|
{
|
2019-10-26 20:53:53 +00:00
|
|
|
public VideoHlsService(
|
2019-12-10 22:00:59 +00:00
|
|
|
ILogger<VideoHlsService> logger,
|
|
|
|
IServerConfigurationManager serverConfigurationManager,
|
|
|
|
IHttpResultFactory httpResultFactory,
|
2019-10-26 20:53:53 +00:00
|
|
|
IUserManager userManager,
|
|
|
|
ILibraryManager libraryManager,
|
|
|
|
IIsoManager isoManager,
|
|
|
|
IMediaEncoder mediaEncoder,
|
|
|
|
IFileSystem fileSystem,
|
|
|
|
IDlnaManager dlnaManager,
|
|
|
|
IDeviceManager deviceManager,
|
|
|
|
IMediaSourceManager mediaSourceManager,
|
|
|
|
IJsonSerializer jsonSerializer,
|
|
|
|
IAuthorizationContext authorizationContext,
|
|
|
|
EncodingHelper encodingHelper)
|
2019-12-10 22:00:59 +00:00
|
|
|
: base(
|
|
|
|
logger,
|
|
|
|
serverConfigurationManager,
|
|
|
|
httpResultFactory,
|
|
|
|
userManager,
|
|
|
|
libraryManager,
|
|
|
|
isoManager,
|
|
|
|
mediaEncoder,
|
|
|
|
fileSystem,
|
|
|
|
dlnaManager,
|
|
|
|
deviceManager,
|
|
|
|
mediaSourceManager,
|
|
|
|
jsonSerializer,
|
|
|
|
authorizationContext,
|
|
|
|
encodingHelper)
|
2019-10-26 20:53:53 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-02 22:55:47 +00:00
|
|
|
public Task<object> Get(GetLiveHlsStream request)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-02-02 22:55:47 +00:00
|
|
|
return ProcessRequestAsync(request, true);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the audio arguments.
|
|
|
|
/// </summary>
|
|
|
|
protected override string GetAudioArguments(StreamState state, EncodingOptions encodingOptions)
|
|
|
|
{
|
|
|
|
var codec = EncodingHelper.GetAudioEncoder(state);
|
|
|
|
|
2020-05-25 21:58:19 +00:00
|
|
|
if (EncodingHelper.IsCopyCodec(codec))
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
return "-codec:a:0 copy";
|
|
|
|
}
|
|
|
|
|
|
|
|
var args = "-codec:a:0 " + codec;
|
|
|
|
|
|
|
|
var channels = state.OutputAudioChannels;
|
|
|
|
|
|
|
|
if (channels.HasValue)
|
|
|
|
{
|
|
|
|
args += " -ac " + channels.Value;
|
|
|
|
}
|
|
|
|
|
|
|
|
var bitrate = state.OutputAudioBitrate;
|
|
|
|
|
|
|
|
if (bitrate.HasValue)
|
|
|
|
{
|
2019-06-05 18:59:06 +00:00
|
|
|
args += " -ab " + bitrate.Value.ToString(CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state.OutputAudioSampleRate.HasValue)
|
|
|
|
{
|
2019-06-05 18:59:06 +00:00
|
|
|
args += " -ar " + state.OutputAudioSampleRate.Value.ToString(CultureInfo.InvariantCulture);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
args += " " + EncodingHelper.GetAudioFilterParam(state, encodingOptions, true);
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the video arguments.
|
|
|
|
/// </summary>
|
|
|
|
protected override string GetVideoArguments(StreamState state, EncodingOptions encodingOptions)
|
|
|
|
{
|
|
|
|
if (!state.IsOutputVideo)
|
|
|
|
{
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
var codec = EncodingHelper.GetVideoEncoder(state, encodingOptions);
|
|
|
|
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
// if h264_mp4toannexb is ever added, do not use it for live tv
|
2019-06-01 22:03:48 +00:00
|
|
|
if (state.VideoStream != null &&
|
2018-12-14 09:40:55 +00:00
|
|
|
!string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2019-06-01 22:03:48 +00:00
|
|
|
string bitStreamArgs = EncodingHelper.GetBitStreamArgs(state.VideoStream);
|
|
|
|
if (!string.IsNullOrEmpty(bitStreamArgs))
|
|
|
|
{
|
|
|
|
args += " " + bitStreamArgs;
|
|
|
|
}
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var keyFrameArg = string.Format(" -force_key_frames \"expr:gte(t,n_forced*{0})\"",
|
2019-06-05 18:59:06 +00:00
|
|
|
state.SegmentLength.ToString(CultureInfo.InvariantCulture));
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode;
|
|
|
|
|
2019-06-01 22:03:48 +00:00
|
|
|
args += " " + EncodingHelper.GetVideoQualityParam(state, codec, encodingOptions, GetDefaultEncoderPreset()) + keyFrameArg;
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
// Add resolution params, if specified
|
|
|
|
if (!hasGraphicalSubs)
|
|
|
|
{
|
|
|
|
args += EncodingHelper.GetOutputSizeParam(state, encodingOptions, codec);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is for internal graphical subs
|
|
|
|
if (hasGraphicalSubs)
|
|
|
|
{
|
|
|
|
args += EncodingHelper.GetGraphicalSubtitleParam(state, encodingOptions, codec);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
args += " -flags -global_header";
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(state.OutputVideoSync))
|
|
|
|
{
|
|
|
|
args += " -vsync " + state.OutputVideoSync;
|
|
|
|
}
|
|
|
|
|
|
|
|
args += EncodingHelper.GetOutputFFlags(state);
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|