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

222 lines
8.3 KiB
C#
Raw Normal View History

using MediaBrowser.Common.IO;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Configuration;
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-09-04 17:02:19 +00:00
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Library;
2013-12-21 18:37:34 +00:00
using MediaBrowser.Controller.LiveTv;
2014-02-20 16:37:41 +00:00
using MediaBrowser.Controller.MediaEncoding;
2013-06-18 19:16:27 +00:00
using MediaBrowser.Controller.Persistence;
2013-08-10 01:16:31 +00:00
using MediaBrowser.Model.IO;
2013-12-07 15:52:38 +00:00
using ServiceStack;
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")]
[Route("/Videos/{Id}/stream.wtv", "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")]
[Route("/Videos/{Id}/stream.wtv", "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
{
2014-03-25 05:25:03 +00:00
public VideoService(IServerConfigurationManager serverConfig, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager, IMediaEncoder mediaEncoder, IDtoService dtoService, IFileSystem fileSystem, IItemRepository itemRepository, ILiveTvManager liveTvManager, IEncodingManager encodingManager, IDlnaManager dlnaManager, IHttpClient httpClient, IImageProcessor imageProcessor) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, dtoService, fileSystem, itemRepository, liveTvManager, encodingManager, dlnaManager, httpClient, 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>
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
{
// 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
}
var threads = GetNumberOfThreads(state, string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase));
2013-05-09 14:23:01 +00:00
2014-01-22 01:37:01 +00:00
var inputModifier = GetInputModifier(state);
return string.Format("{0} -i {1}{2}{3} {4} {5} -map_metadata -1 -threads {6} {7}{8} \"{9}\"",
2014-01-22 01:37:01 +00:00
inputModifier,
2013-12-19 21:51:32 +00:00
GetInputArgument(state),
2013-02-27 04:19:05 +00:00
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-05-09 14:23:01 +00:00
threads,
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
2013-09-02 20:19:11 +00:00
const string keyFrameArg = " -force_key_frames expr:if(isnan(prev_forced_t),gte(t,.1),gte(t,prev_forced_t+5))";
2013-02-21 01:33:05 +00:00
args += keyFrameArg;
2013-02-21 01:33:05 +00:00
var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsExternal &&
(state.SubtitleStream.Codec.IndexOf("pgs", StringComparison.OrdinalIgnoreCase) != -1 ||
state.SubtitleStream.Codec.IndexOf("dvd", StringComparison.OrdinalIgnoreCase) != -1);
var request = state.VideoRequest;
// Add resolution params, if specified
if (!hasGraphicalSubs)
{
if (request.Width.HasValue || request.Height.HasValue || request.MaxHeight.HasValue || request.MaxWidth.HasValue)
{
args += GetOutputSizeParam(state, codec, performSubtitleConversion);
}
}
2013-03-20 14:27:56 +00:00
2014-02-02 14:47:00 +00:00
var qualityParam = GetVideoQualityParam(state, codec, false);
if (!string.IsNullOrEmpty(qualityParam))
{
args += " " + qualityParam.Trim();
}
// This is for internal graphical subs
if (hasGraphicalSubs)
2013-03-28 15:52:26 +00:00
{
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-08-30 02:13:58 +00:00
var bitrate = GetAudioBitrateParam(state);
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
2014-01-10 13:52:01 +00:00
args += " " + GetAudioFilterParam(state, true);
2013-02-21 01:33:05 +00:00
return args;
2013-02-21 01:33:05 +00:00
}
}
}