jellyfin-server/MediaBrowser.Api/Playback/Progressive/VideoService.cs

288 lines
10 KiB
C#
Raw Normal View History

2013-04-29 16:01:23 +00:00
using MediaBrowser.Common.IO;
using MediaBrowser.Common.MediaInfo;
2013-03-04 05:43:06 +00:00
using MediaBrowser.Controller;
2013-02-21 01:33:05 +00:00
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
2013-02-27 04:44:41 +00:00
using ServiceStack.ServiceHost;
2013-04-29 16:01:23 +00:00
using System;
using System.IO;
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>
/// Class GetAudioStream
/// </summary>
[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")]
[Route("/Videos/{Id}/stream.avi", "GET")]
2013-03-11 14:54:08 +00:00
[Route("/Videos/{Id}/stream.m2ts", "GET")]
[Route("/Videos/{Id}/stream.3gp", "GET")]
2013-04-10 21:03:41 +00:00
[Route("/Videos/{Id}/stream.wmv", "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")]
[Route("/Videos/{Id}/stream.avi", "HEAD")]
[Route("/Videos/{Id}/stream.3gp", "HEAD")]
2013-04-10 21:03:41 +00:00
[Route("/Videos/{Id}/stream.wmv", "HEAD")]
2013-03-13 03:57:54 +00:00
[Route("/Videos/{Id}/stream.m2ts", "HEAD")]
[Route("/Videos/{Id}/stream", "HEAD")]
2013-04-03 20:30:56 +00:00
[Api(Description = "Gets a video stream")]
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
{
2013-04-29 16:01:23 +00:00
/// <summary>
/// Initializes a new instance of the <see cref="VideoService"/> class.
/// </summary>
/// <param name="appPaths">The app paths.</param>
/// <param name="userManager">The user manager.</param>
/// <param name="libraryManager">The library manager.</param>
/// <param name="isoManager">The iso manager.</param>
/// <param name="mediaEncoder">The media encoder.</param>
public VideoService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder)
: base(appPaths, userManager, libraryManager, isoManager, mediaEncoder)
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>
public object Get(GetVideoStream request)
{
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>
2013-03-13 03:57:54 +00:00
public object Head(GetVideoStream request)
{
return ProcessRequest(request, true);
}
2013-02-21 01:33:05 +00:00
/// <summary>
2013-02-27 04:19:05 +00:00
/// Gets the command line arguments.
2013-02-21 01:33:05 +00:00
/// </summary>
/// <param name="outputPath">The output path.</param>
2013-02-27 04:19:05 +00:00
/// <param name="state">The state.</param>
2013-04-29 16:01:23 +00:00
/// <param name="performSubtitleConversions">if set to <c>true</c> [perform subtitle conversions].</param>
2013-02-21 01:33:05 +00:00
/// <returns>System.String.</returns>
2013-04-29 16:01:23 +00:00
protected override string GetCommandLineArguments(string outputPath, StreamState state, bool performSubtitleConversions)
2013-02-21 01:33:05 +00:00
{
2013-02-27 04:19:05 +00:00
var video = (Video)state.Item;
var probeSize = GetProbeSizeArgument(state.Item);
2013-02-21 01:33:05 +00:00
// Get the output codec name
2013-03-09 02:34:54 +00:00
var videoCodec = GetVideoCodec(state.VideoRequest);
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
{
2013-03-13 02:56:13 +00:00
format = " -f mp4 -movflags frag_keyframe+empty_moov";
2013-03-07 17:39:21 +00:00
}
return string.Format("{0} {1} {2} -i {3}{4}{5} {6} {7} -threads 0 {8}{9} \"{10}\"",
2013-02-21 01:33:05 +00:00
probeSize,
2013-05-01 20:07:20 +00:00
GetUserAgentParam(state.Item),
2013-02-27 04:19:05 +00:00
GetFastSeekCommandLineParameter(state.Request),
GetInputArgument(video, state.IsoMount),
GetSlowSeekCommandLineParameter(state.Request),
2013-03-13 02:56:13 +00:00
keyFrame,
2013-02-27 04:19:05 +00:00
GetMapArgs(state),
2013-04-29 16:01:23 +00:00
GetVideoArguments(state, videoCodec, performSubtitleConversions),
2013-02-27 04:19:05 +00:00
GetAudioArguments(state),
2013-03-07 17:39:21 +00:00
format,
2013-02-21 01:33:05 +00:00
outputPath
).Trim();
}
/// <summary>
/// Gets video arguments to pass to ffmpeg
/// </summary>
2013-02-27 04:19:05 +00:00
/// <param name="state">The state.</param>
2013-03-28 15:52:26 +00:00
/// <param name="codec">The video codec.</param>
2013-04-29 16:01:23 +00:00
/// <param name="performSubtitleConversion">if set to <c>true</c> [perform subtitle conversion].</param>
2013-02-21 01:33:05 +00:00
/// <returns>System.String.</returns>
2013-04-29 16:01:23 +00:00
private string GetVideoArguments(StreamState state, string codec, bool performSubtitleConversion)
2013-02-21 01:33:05 +00:00
{
2013-03-28 15:52:26 +00:00
var args = "-vcodec " + codec;
2013-02-21 01:33:05 +00:00
// See if we can save come cpu cycles by avoiding encoding
2013-03-28 15:52:26 +00:00
if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
2013-02-21 01:33:05 +00:00
{
2013-05-01 20:07:20 +00:00
return state.VideoStream != null && IsH264(state.VideoStream) ? args + " -bsf h264_mp4toannexb" : args;
}
2013-02-21 01:33:05 +00:00
const string keyFrameArg = " -force_key_frames expr:if(isnan(prev_forced_t),gte(t,0),gte(t,prev_forced_t+2))";
2013-02-21 01:33:05 +00:00
args += keyFrameArg;
2013-02-21 01:33:05 +00:00
var request = state.VideoRequest;
// Add resolution params, if specified
if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
{
2013-04-29 16:01:23 +00:00
args += GetOutputSizeParam(state, codec, performSubtitleConversion);
}
2013-03-20 14:27:56 +00:00
if (request.Framerate.HasValue)
{
args += string.Format(" -r {0}", request.Framerate.Value);
}
// Add the audio bitrate
2013-03-28 15:52:26 +00:00
var qualityParam = GetVideoQualityParam(request, codec);
if (!string.IsNullOrEmpty(qualityParam))
{
args += " " + qualityParam;
}
args += " -vsync vfr";
if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
{
args += " -profile:v " + state.VideoRequest.Profile;
2013-02-21 01:33:05 +00:00
}
if (!string.IsNullOrEmpty(state.VideoRequest.Level))
2013-02-21 01:33:05 +00:00
{
2013-03-29 05:45:45 +00:00
args += " -level " + state.VideoRequest.Level;
2013-03-28 15:52:26 +00:00
}
if (state.SubtitleStream != null)
{
// This is for internal graphical subs
if (!state.SubtitleStream.IsExternal && (state.SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 || state.SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1))
{
args += GetInternalGraphicalSubtitleParam(state, codec);
}
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.
2013-02-27 04:19:05 +00:00
if (state.AudioStream == null)
2013-02-21 01:33:05 +00:00
{
return string.Empty;
}
2013-02-27 04:19:05 +00:00
var request = state.Request;
2013-02-21 01:33:05 +00:00
// Get the output codec name
2013-02-27 04:19:05 +00:00
var codec = GetAudioCodec(request);
2013-02-21 01:33:05 +00:00
2013-03-24 22:37:20 +00:00
if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
{
return "-acodec copy";
}
2013-02-21 01:33:05 +00:00
var args = "-acodec " + codec;
2013-03-24 22:37:20 +00:00
// Add the number of audio channels
var channels = GetNumAudioChannelsParam(request, state.AudioStream);
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
2013-03-24 22:37:20 +00:00
if (request.AudioSampleRate.HasValue)
{
args += " -ar " + request.AudioSampleRate.Value;
}
2013-03-24 22:37:20 +00:00
if (request.AudioBitRate.HasValue)
{
args += " -ab " + request.AudioBitRate.Value;
}
2013-02-21 01:33:05 +00:00
2013-03-24 22:37:20 +00:00
var volParam = string.Empty;
2013-02-21 01:33:05 +00:00
2013-03-24 22:37:20 +00:00
// Boost volume to 200% when downsampling from 6ch to 2ch
if (channels.HasValue && channels.Value <= 2 && state.AudioStream.Channels.HasValue && state.AudioStream.Channels.Value > 5)
{
volParam = ",volume=2.000000";
2013-02-21 01:33:05 +00:00
}
2013-03-24 22:45:53 +00:00
args += string.Format(" -af \"aresample=async=1000{0}\"", volParam);
2013-03-24 22:37:20 +00:00
2013-02-21 01:33:05 +00:00
return args;
}
/// <summary>
/// Gets the video bitrate to specify on the command line
/// </summary>
2013-02-27 04:19:05 +00:00
/// <param name="request">The request.</param>
2013-02-21 01:33:05 +00:00
/// <param name="videoCodec">The video codec.</param>
/// <returns>System.String.</returns>
2013-03-09 02:34:54 +00:00
private string GetVideoQualityParam(VideoStreamRequest request, string videoCodec)
2013-02-21 01:33:05 +00:00
{
var args = string.Empty;
// webm
if (videoCodec.Equals("libvpx", StringComparison.OrdinalIgnoreCase))
{
2013-05-07 04:39:48 +00:00
args = "-speed 16 -quality good -profile:v 0 -slices 8";
2013-02-21 01:33:05 +00:00
}
// asf/wmv
else if (videoCodec.Equals("wmv2", StringComparison.OrdinalIgnoreCase))
{
args = "-g 100 -qmax 15";
}
else if (videoCodec.Equals("libx264", StringComparison.OrdinalIgnoreCase))
{
args = "-preset superfast";
}
else if (videoCodec.Equals("mpeg4", StringComparison.OrdinalIgnoreCase))
{
args = "-mbd rd -flags +mv4+aic -trellis 2 -cmp 2 -subcmp 2 -bf 2";
}
2013-02-27 04:19:05 +00:00
if (request.VideoBitRate.HasValue)
2013-02-21 01:33:05 +00:00
{
2013-02-27 04:19:05 +00:00
args += " -b:v " + request.VideoBitRate;
2013-02-21 01:33:05 +00:00
}
return args.Trim();
}
}
}