2019-01-13 20:24:58 +01:00
|
|
|
using System;
|
2019-06-05 20:59:06 +02:00
|
|
|
using System.Globalization;
|
2019-02-02 23:55:47 +01:00
|
|
|
using System.Threading.Tasks;
|
2018-12-14 10:40:55 +01: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 20:24:58 +01:00
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using MediaBrowser.Model.Serialization;
|
2018-12-14 10:40:55 +01:00
|
|
|
using MediaBrowser.Model.Services;
|
2019-11-17 23:05:39 +01:00
|
|
|
using Microsoft.Extensions.Logging;
|
2018-12-14 10:40:55 +01: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 22:53:53 +02:00
|
|
|
public VideoHlsService(
|
2019-12-10 23:00:59 +01:00
|
|
|
ILogger<VideoHlsService> logger,
|
|
|
|
IServerConfigurationManager serverConfigurationManager,
|
|
|
|
IHttpResultFactory httpResultFactory,
|
2019-10-26 22:53:53 +02: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 23:00:59 +01:00
|
|
|
: base(
|
|
|
|
logger,
|
|
|
|
serverConfigurationManager,
|
|
|
|
httpResultFactory,
|
|
|
|
userManager,
|
|
|
|
libraryManager,
|
|
|
|
isoManager,
|
|
|
|
mediaEncoder,
|
|
|
|
fileSystem,
|
|
|
|
dlnaManager,
|
|
|
|
deviceManager,
|
|
|
|
mediaSourceManager,
|
|
|
|
jsonSerializer,
|
|
|
|
authorizationContext,
|
|
|
|
encodingHelper)
|
2019-10-26 22:53:53 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-02 23:55:47 +01:00
|
|
|
public Task<object> Get(GetLiveHlsStream request)
|
2018-12-14 10:40:55 +01:00
|
|
|
{
|
2019-02-02 23:55:47 +01:00
|
|
|
return ProcessRequestAsync(request, true);
|
2018-12-14 10:40:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the audio arguments.
|
|
|
|
/// </summary>
|
|
|
|
protected override string GetAudioArguments(StreamState state, EncodingOptions encodingOptions)
|
|
|
|
{
|
|
|
|
var codec = EncodingHelper.GetAudioEncoder(state);
|
|
|
|
|
2020-05-25 23:58:19 +02:00
|
|
|
if (EncodingHelper.IsCopyCodec(codec))
|
2018-12-14 10:40:55 +01: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 20:59:06 +02:00
|
|
|
args += " -ab " + bitrate.Value.ToString(CultureInfo.InvariantCulture);
|
2018-12-14 10:40:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state.OutputAudioSampleRate.HasValue)
|
|
|
|
{
|
2019-06-05 20:59:06 +02:00
|
|
|
args += " -ar " + state.OutputAudioSampleRate.Value.ToString(CultureInfo.InvariantCulture);
|
2018-12-14 10:40:55 +01: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-02 00:03:48 +02:00
|
|
|
if (state.VideoStream != null &&
|
2018-12-14 10:40:55 +01:00
|
|
|
!string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2019-06-02 00:03:48 +02:00
|
|
|
string bitStreamArgs = EncodingHelper.GetBitStreamArgs(state.VideoStream);
|
|
|
|
if (!string.IsNullOrEmpty(bitStreamArgs))
|
|
|
|
{
|
|
|
|
args += " " + bitStreamArgs;
|
|
|
|
}
|
2018-12-14 10:40:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var keyFrameArg = string.Format(" -force_key_frames \"expr:gte(t,n_forced*{0})\"",
|
2019-06-05 20:59:06 +02:00
|
|
|
state.SegmentLength.ToString(CultureInfo.InvariantCulture));
|
2018-12-14 10:40:55 +01:00
|
|
|
|
|
|
|
var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Encode;
|
|
|
|
|
2019-06-02 00:03:48 +02:00
|
|
|
args += " " + EncodingHelper.GetVideoQualityParam(state, codec, encodingOptions, GetDefaultEncoderPreset()) + keyFrameArg;
|
2018-12-14 10:40:55 +01: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|