2014-02-13 16:38:43 +00:00
|
|
|
using MediaBrowser.Common.Net;
|
2013-12-15 19:39:21 +00:00
|
|
|
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;
|
2013-09-18 18:49:06 +00:00
|
|
|
using MediaBrowser.Controller.Drawing;
|
2013-02-27 20:25:45 +00:00
|
|
|
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-04-29 16:01:23 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
2017-01-29 20:00:29 +00:00
|
|
|
using System.Linq;
|
2016-06-19 06:18:29 +00:00
|
|
|
using System.Threading.Tasks;
|
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;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-02-27 04:19:05 +00:00
|
|
|
namespace MediaBrowser.Api.Playback.Progressive
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2013-02-27 04:44:41 +00:00
|
|
|
/// <summary>
|
2015-05-24 18:33:28 +00:00
|
|
|
/// Class GetVideoStream
|
2013-02-27 04:44:41 +00:00
|
|
|
/// </summary>
|
2015-09-10 03:22:52 +00:00
|
|
|
[Route("/Videos/{Id}/stream.mpegts", "GET")]
|
2013-02-27 04:44:41 +00:00
|
|
|
[Route("/Videos/{Id}/stream.ts", "GET")]
|
|
|
|
[Route("/Videos/{Id}/stream.webm", "GET")]
|
|
|
|
[Route("/Videos/{Id}/stream.asf", "GET")]
|
|
|
|
[Route("/Videos/{Id}/stream.wmv", "GET")]
|
|
|
|
[Route("/Videos/{Id}/stream.ogv", "GET")]
|
|
|
|
[Route("/Videos/{Id}/stream.mp4", "GET")]
|
|
|
|
[Route("/Videos/{Id}/stream.m4v", "GET")]
|
|
|
|
[Route("/Videos/{Id}/stream.mkv", "GET")]
|
|
|
|
[Route("/Videos/{Id}/stream.mpeg", "GET")]
|
2014-06-18 05:16:00 +00:00
|
|
|
[Route("/Videos/{Id}/stream.mpg", "GET")]
|
2013-02-27 04:44:41 +00:00
|
|
|
[Route("/Videos/{Id}/stream.avi", "GET")]
|
2013-03-11 14:54:08 +00:00
|
|
|
[Route("/Videos/{Id}/stream.m2ts", "GET")]
|
2013-04-08 15:55:53 +00:00
|
|
|
[Route("/Videos/{Id}/stream.3gp", "GET")]
|
2013-04-10 21:03:41 +00:00
|
|
|
[Route("/Videos/{Id}/stream.wmv", "GET")]
|
2014-01-07 18:39:35 +00:00
|
|
|
[Route("/Videos/{Id}/stream.wtv", "GET")]
|
2014-10-11 20:38:13 +00:00
|
|
|
[Route("/Videos/{Id}/stream.mov", "GET")]
|
2016-02-29 16:23:30 +00:00
|
|
|
[Route("/Videos/{Id}/stream.iso", "GET")]
|
2013-02-27 04:44:41 +00:00
|
|
|
[Route("/Videos/{Id}/stream", "GET")]
|
2013-03-13 03:57:54 +00:00
|
|
|
[Route("/Videos/{Id}/stream.ts", "HEAD")]
|
|
|
|
[Route("/Videos/{Id}/stream.webm", "HEAD")]
|
|
|
|
[Route("/Videos/{Id}/stream.asf", "HEAD")]
|
|
|
|
[Route("/Videos/{Id}/stream.wmv", "HEAD")]
|
|
|
|
[Route("/Videos/{Id}/stream.ogv", "HEAD")]
|
|
|
|
[Route("/Videos/{Id}/stream.mp4", "HEAD")]
|
|
|
|
[Route("/Videos/{Id}/stream.m4v", "HEAD")]
|
|
|
|
[Route("/Videos/{Id}/stream.mkv", "HEAD")]
|
|
|
|
[Route("/Videos/{Id}/stream.mpeg", "HEAD")]
|
2014-06-18 05:16:00 +00:00
|
|
|
[Route("/Videos/{Id}/stream.mpg", "HEAD")]
|
2013-03-13 03:57:54 +00:00
|
|
|
[Route("/Videos/{Id}/stream.avi", "HEAD")]
|
2013-04-08 15:55:53 +00:00
|
|
|
[Route("/Videos/{Id}/stream.3gp", "HEAD")]
|
2013-04-10 21:03:41 +00:00
|
|
|
[Route("/Videos/{Id}/stream.wmv", "HEAD")]
|
2014-01-07 18:39:35 +00:00
|
|
|
[Route("/Videos/{Id}/stream.wtv", "HEAD")]
|
2013-03-13 03:57:54 +00:00
|
|
|
[Route("/Videos/{Id}/stream.m2ts", "HEAD")]
|
2014-10-11 20:38:13 +00:00
|
|
|
[Route("/Videos/{Id}/stream.mov", "HEAD")]
|
2016-02-29 16:23:30 +00:00
|
|
|
[Route("/Videos/{Id}/stream.iso", "HEAD")]
|
2013-03-13 03:57:54 +00:00
|
|
|
[Route("/Videos/{Id}/stream", "HEAD")]
|
2013-03-09 02:34:54 +00:00
|
|
|
public class GetVideoStream : VideoStreamRequest
|
2013-02-27 04:44:41 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2013-03-07 17:39:21 +00:00
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
2013-02-27 04:19:05 +00:00
|
|
|
/// Class VideoService
|
2013-02-21 01:33:05 +00:00
|
|
|
/// </summary>
|
2013-02-27 04:19:05 +00:00
|
|
|
public class VideoService : BaseProgressiveStreamingService
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2016-11-10 14:41:24 +00:00
|
|
|
public VideoService(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, IImageProcessor imageProcessor) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, authorizationContext, imageProcessor)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-02-27 04:44:41 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
2016-06-19 06:18:29 +00:00
|
|
|
public Task<object> Get(GetVideoStream request)
|
2013-02-27 04:44:41 +00:00
|
|
|
{
|
2013-03-13 03:57:54 +00:00
|
|
|
return ProcessRequest(request, false);
|
2013-02-27 04:44:41 +00:00
|
|
|
}
|
2013-03-07 17:39:21 +00:00
|
|
|
|
2013-04-29 16:01:23 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Heads the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
2016-06-19 06:18:29 +00:00
|
|
|
public Task<object> Head(GetVideoStream request)
|
2013-03-13 03:57:54 +00:00
|
|
|
{
|
|
|
|
return ProcessRequest(request, true);
|
|
|
|
}
|
2013-03-20 14:38:56 +00:00
|
|
|
|
2015-03-28 20:22:27 +00:00
|
|
|
protected override string GetCommandLineArguments(string outputPath, StreamState state, bool isEncoding)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2017-02-02 16:02:01 +00:00
|
|
|
var encodingOptions = ApiEntryPoint.Instance.GetEncodingOptions();
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
// Get the output codec name
|
2017-02-02 16:02:01 +00:00
|
|
|
var videoCodec = EncodingHelper.GetVideoEncoder(state, encodingOptions);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-03-07 17:39:21 +00:00
|
|
|
var format = string.Empty;
|
2013-03-13 02:56:13 +00:00
|
|
|
var keyFrame = string.Empty;
|
2013-03-07 17:39:21 +00:00
|
|
|
|
2013-03-13 02:56:13 +00:00
|
|
|
if (string.Equals(Path.GetExtension(outputPath), ".mp4", StringComparison.OrdinalIgnoreCase))
|
2013-03-07 17:39:21 +00:00
|
|
|
{
|
2015-02-06 05:39:07 +00:00
|
|
|
// Comparison: https://github.com/jansmolders86/mediacenterjs/blob/master/lib/transcoding/desktop.js
|
2013-03-13 02:56:13 +00:00
|
|
|
format = " -f mp4 -movflags frag_keyframe+empty_moov";
|
2013-03-07 17:39:21 +00:00
|
|
|
}
|
|
|
|
|
2017-02-02 16:02:01 +00:00
|
|
|
var threads = EncodingHelper.GetNumberOfThreads(state, encodingOptions, string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase));
|
2013-05-09 14:23:01 +00:00
|
|
|
|
2017-02-02 16:02:01 +00:00
|
|
|
var inputModifier = EncodingHelper.GetInputModifier(state, encodingOptions);
|
2014-01-22 01:37:01 +00:00
|
|
|
|
2017-01-29 20:00:29 +00:00
|
|
|
var subtitleArguments = state.SubtitleStream != null &&
|
|
|
|
state.SubtitleDeliveryMethod == SubtitleDeliveryMethod.Embed
|
|
|
|
? GetSubtitleArguments(state)
|
|
|
|
: string.Empty;
|
|
|
|
|
|
|
|
return string.Format("{0} {1}{2} {3} {4} -map_metadata -1 -map_chapters -1 -threads {5} {6}{7}{8} -y \"{9}\"",
|
2014-01-22 01:37:01 +00:00
|
|
|
inputModifier,
|
2017-02-02 16:02:01 +00:00
|
|
|
EncodingHelper.GetInputArgument(state, encodingOptions),
|
2013-03-13 02:56:13 +00:00
|
|
|
keyFrame,
|
2017-02-02 16:02:01 +00:00
|
|
|
EncodingHelper.GetMapArgs(state),
|
2014-06-10 17:36:06 +00:00
|
|
|
GetVideoArguments(state, videoCodec),
|
2013-05-09 14:23:01 +00:00
|
|
|
threads,
|
2013-02-27 04:19:05 +00:00
|
|
|
GetAudioArguments(state),
|
2017-01-29 20:00:29 +00:00
|
|
|
subtitleArguments,
|
2013-03-07 17:39:21 +00:00
|
|
|
format,
|
2013-02-21 01:33:05 +00:00
|
|
|
outputPath
|
|
|
|
).Trim();
|
|
|
|
}
|
|
|
|
|
2017-01-29 20:00:29 +00:00
|
|
|
private string GetSubtitleArguments(StreamState state)
|
|
|
|
{
|
|
|
|
var format = state.SupportedSubtitleCodecs.FirstOrDefault();
|
|
|
|
string codec;
|
|
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(format) || string.Equals(format, state.SubtitleStream.Codec, StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
|
|
|
codec = "copy";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
codec = format;
|
|
|
|
}
|
|
|
|
|
|
|
|
return " -codec:s:0 " + codec;
|
|
|
|
}
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets video arguments to pass to ffmpeg
|
|
|
|
/// </summary>
|
2013-02-27 04:19:05 +00:00
|
|
|
/// <param name="state">The state.</param>
|
2015-12-12 20:41:25 +00:00
|
|
|
/// <param name="videoCodec">The video codec.</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <returns>System.String.</returns>
|
2015-12-12 20:41:25 +00:00
|
|
|
private string GetVideoArguments(StreamState state, string videoCodec)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2015-12-12 20:41:25 +00:00
|
|
|
var args = "-codec:v:0 " + videoCodec;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2014-05-07 18:38:50 +00:00
|
|
|
if (state.EnableMpegtsM2TsMode)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2014-05-07 18:38:50 +00:00
|
|
|
args += " -mpegts_m2ts_mode 1";
|
2013-03-28 15:22:05 +00:00
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2015-12-12 20:41:25 +00:00
|
|
|
if (string.Equals(videoCodec, "copy", StringComparison.OrdinalIgnoreCase))
|
|
|
|
{
|
2017-02-02 16:02:01 +00:00
|
|
|
if (state.VideoStream != null && EncodingHelper.IsH264(state.VideoStream) && string.Equals(state.OutputContainer, "ts", StringComparison.OrdinalIgnoreCase) && !string.Equals(state.VideoStream.NalLengthSize, "0", StringComparison.OrdinalIgnoreCase))
|
2015-12-12 07:05:36 +00:00
|
|
|
{
|
|
|
|
args += " -bsf:v h264_mp4toannexb";
|
|
|
|
}
|
|
|
|
|
2016-03-01 19:39:46 +00:00
|
|
|
if (state.RunTimeTicks.HasValue && state.VideoRequest.CopyTimestamps)
|
|
|
|
{
|
|
|
|
args += " -copyts -avoid_negative_ts disabled -start_at_zero";
|
|
|
|
}
|
2016-08-07 20:13:30 +00:00
|
|
|
|
2016-10-22 19:21:50 +00:00
|
|
|
if (!state.RunTimeTicks.HasValue)
|
|
|
|
{
|
2016-12-22 16:47:53 +00:00
|
|
|
args += " -flags -global_header -fflags +genpts";
|
2016-10-22 19:21:50 +00:00
|
|
|
}
|
|
|
|
|
2015-12-12 07:05:36 +00:00
|
|
|
return args;
|
2014-03-26 22:14:25 +00:00
|
|
|
}
|
|
|
|
|
2016-08-07 20:13:30 +00:00
|
|
|
var keyFrameArg = string.Format(" -force_key_frames \"expr:gte(t,n_forced*{0})\"",
|
2014-06-30 03:04:50 +00:00
|
|
|
5.ToString(UsCulture));
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-03-28 15:22:05 +00:00
|
|
|
args += keyFrameArg;
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2016-03-07 18:50:58 +00:00
|
|
|
var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream && state.VideoRequest.SubtitleMethod == SubtitleDeliveryMethod.Encode;
|
2013-11-20 15:50:54 +00:00
|
|
|
|
2016-02-24 07:11:35 +00:00
|
|
|
var hasCopyTs = false;
|
2013-03-28 15:22:05 +00:00
|
|
|
// Add resolution params, if specified
|
2013-11-20 15:50:54 +00:00
|
|
|
if (!hasGraphicalSubs)
|
2013-03-28 15:22:05 +00:00
|
|
|
{
|
2017-02-02 16:02:01 +00:00
|
|
|
var outputSizeParam = EncodingHelper.GetOutputSizeParam(state, videoCodec);
|
2016-02-24 07:11:35 +00:00
|
|
|
args += outputSizeParam;
|
|
|
|
hasCopyTs = outputSizeParam.IndexOf("copyts", StringComparison.OrdinalIgnoreCase) != -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state.RunTimeTicks.HasValue && state.VideoRequest.CopyTimestamps)
|
|
|
|
{
|
|
|
|
if (!hasCopyTs)
|
|
|
|
{
|
|
|
|
args += " -copyts";
|
|
|
|
}
|
|
|
|
args += " -avoid_negative_ts disabled -start_at_zero";
|
2013-03-28 15:22:05 +00:00
|
|
|
}
|
2013-03-20 14:27:56 +00:00
|
|
|
|
2017-02-02 16:02:01 +00:00
|
|
|
var encodingOptions = ApiEntryPoint.Instance.GetEncodingOptions();
|
|
|
|
var qualityParam = EncodingHelper.GetVideoQualityParam(state, videoCodec, encodingOptions, GetDefaultH264Preset());
|
2013-12-15 19:39:21 +00:00
|
|
|
|
2013-03-28 15:22:05 +00:00
|
|
|
if (!string.IsNullOrEmpty(qualityParam))
|
|
|
|
{
|
2013-12-15 19:39:21 +00:00
|
|
|
args += " " + qualityParam.Trim();
|
2013-03-28 15:22:05 +00:00
|
|
|
}
|
2013-03-28 14:48:56 +00:00
|
|
|
|
2013-11-20 15:50:54 +00:00
|
|
|
// This is for internal graphical subs
|
|
|
|
if (hasGraphicalSubs)
|
2013-03-28 15:52:26 +00:00
|
|
|
{
|
2017-02-02 16:02:01 +00:00
|
|
|
args += EncodingHelper.GetGraphicalSubtitleParam(state, videoCodec);
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 19:21:50 +00:00
|
|
|
if (!state.RunTimeTicks.HasValue)
|
|
|
|
{
|
2016-11-25 17:36:00 +00:00
|
|
|
args += " -flags -global_header";
|
2016-10-22 19:21:50 +00:00
|
|
|
}
|
|
|
|
|
2013-02-21 01:33:05 +00:00
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets audio arguments to pass to ffmpeg
|
|
|
|
/// </summary>
|
2013-02-27 04:19:05 +00:00
|
|
|
/// <param name="state">The state.</param>
|
2013-02-21 01:33:05 +00:00
|
|
|
/// <returns>System.String.</returns>
|
2013-02-27 04:19:05 +00:00
|
|
|
private string GetAudioArguments(StreamState state)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
// If the video doesn't have an audio stream, return a default.
|
2014-05-18 19:58:42 +00:00
|
|
|
if (state.AudioStream == null && state.VideoStream != null)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the output codec name
|
2017-02-02 16:02:01 +00:00
|
|
|
var codec = EncodingHelper.GetAudioEncoder(state);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2014-12-26 17:45:06 +00:00
|
|
|
var args = "-codec:a:0 " + codec;
|
|
|
|
|
2015-06-03 03:15:46 +00:00
|
|
|
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
|
2013-03-24 22:37:20 +00:00
|
|
|
{
|
2014-12-26 17:45:06 +00:00
|
|
|
return args;
|
2013-03-24 22:37:20 +00:00
|
|
|
}
|
2013-12-15 19:39:21 +00:00
|
|
|
|
2013-03-24 22:37:20 +00:00
|
|
|
// Add the number of audio channels
|
2014-04-23 00:50:47 +00:00
|
|
|
var channels = state.OutputAudioChannels;
|
2013-03-24 22:37:20 +00:00
|
|
|
|
|
|
|
if (channels.HasValue)
|
2013-02-21 01:33:05 +00:00
|
|
|
{
|
2013-03-24 22:37:20 +00:00
|
|
|
args += " -ac " + channels.Value;
|
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2014-04-23 00:50:47 +00:00
|
|
|
var bitrate = state.OutputAudioBitrate;
|
2013-08-30 02:13:58 +00:00
|
|
|
|
|
|
|
if (bitrate.HasValue)
|
2013-03-24 22:37:20 +00:00
|
|
|
{
|
2013-08-30 02:13:58 +00:00
|
|
|
args += " -ab " + bitrate.Value.ToString(UsCulture);
|
2013-03-24 22:37:20 +00:00
|
|
|
}
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2017-02-02 16:02:01 +00:00
|
|
|
args += " " + EncodingHelper.GetAudioFilterParam(state, ApiEntryPoint.Instance.GetEncodingOptions(), false);
|
2013-02-21 01:33:05 +00:00
|
|
|
|
2013-12-15 19:39:21 +00:00
|
|
|
return args;
|
2013-02-21 01:33:05 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-07 20:13:30 +00:00
|
|
|
}
|