2013-11-01 08:59:42 -07:00
|
|
|
using MediaBrowser.Common.IO;
|
2014-02-13 09:38:43 -07:00
|
|
|
using MediaBrowser.Common.Net;
|
2013-12-15 12:39:21 -07:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
2015-01-19 22:19:13 -07:00
|
|
|
using MediaBrowser.Controller.Devices;
|
2014-03-24 22:25:03 -07:00
|
|
|
using MediaBrowser.Controller.Dlna;
|
2013-09-18 11:49:06 -07:00
|
|
|
using MediaBrowser.Controller.Drawing;
|
2013-02-27 13:25:45 -07:00
|
|
|
using MediaBrowser.Controller.Library;
|
2014-02-20 09:37:41 -07:00
|
|
|
using MediaBrowser.Controller.MediaEncoding;
|
2013-08-09 18:16:31 -07:00
|
|
|
using MediaBrowser.Model.IO;
|
2015-05-04 10:44:25 -07:00
|
|
|
using MediaBrowser.Model.Serialization;
|
2013-12-07 08:52:38 -07:00
|
|
|
using ServiceStack;
|
2013-04-29 09:01:23 -07:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2013-02-26 21:19:05 -07:00
|
|
|
namespace MediaBrowser.Api.Playback.Progressive
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2013-02-26 21:44:41 -07:00
|
|
|
/// <summary>
|
2015-05-24 11:33:28 -07:00
|
|
|
/// Class GetVideoStream
|
2013-02-26 21:44:41 -07:00
|
|
|
/// </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")]
|
2014-06-17 22:16:00 -07:00
|
|
|
[Route("/Videos/{Id}/stream.mpg", "GET")]
|
2013-02-26 21:44:41 -07:00
|
|
|
[Route("/Videos/{Id}/stream.avi", "GET")]
|
2013-03-11 07:54:08 -07:00
|
|
|
[Route("/Videos/{Id}/stream.m2ts", "GET")]
|
2013-04-08 08:55:53 -07:00
|
|
|
[Route("/Videos/{Id}/stream.3gp", "GET")]
|
2013-04-10 14:03:41 -07:00
|
|
|
[Route("/Videos/{Id}/stream.wmv", "GET")]
|
2014-01-07 11:39:35 -07:00
|
|
|
[Route("/Videos/{Id}/stream.wtv", "GET")]
|
2014-10-11 13:38:13 -07:00
|
|
|
[Route("/Videos/{Id}/stream.mov", "GET")]
|
2013-02-26 21:44:41 -07:00
|
|
|
[Route("/Videos/{Id}/stream", "GET")]
|
2013-03-12 20:57:54 -07: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-17 22:16:00 -07:00
|
|
|
[Route("/Videos/{Id}/stream.mpg", "HEAD")]
|
2013-03-12 20:57:54 -07:00
|
|
|
[Route("/Videos/{Id}/stream.avi", "HEAD")]
|
2013-04-08 08:55:53 -07:00
|
|
|
[Route("/Videos/{Id}/stream.3gp", "HEAD")]
|
2013-04-10 14:03:41 -07:00
|
|
|
[Route("/Videos/{Id}/stream.wmv", "HEAD")]
|
2014-01-07 11:39:35 -07:00
|
|
|
[Route("/Videos/{Id}/stream.wtv", "HEAD")]
|
2013-03-12 20:57:54 -07:00
|
|
|
[Route("/Videos/{Id}/stream.m2ts", "HEAD")]
|
2014-10-11 13:38:13 -07:00
|
|
|
[Route("/Videos/{Id}/stream.mov", "HEAD")]
|
2013-03-12 20:57:54 -07:00
|
|
|
[Route("/Videos/{Id}/stream", "HEAD")]
|
2013-04-03 13:30:56 -07:00
|
|
|
[Api(Description = "Gets a video stream")]
|
2013-03-08 19:34:54 -07:00
|
|
|
public class GetVideoStream : VideoStreamRequest
|
2013-02-26 21:44:41 -07:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2013-03-07 10:39:21 -07:00
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
/// <summary>
|
2013-02-26 21:19:05 -07:00
|
|
|
/// Class VideoService
|
2013-02-20 18:33:05 -07:00
|
|
|
/// </summary>
|
2013-02-26 21:19:05 -07:00
|
|
|
public class VideoService : BaseProgressiveStreamingService
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2015-05-04 10:44:25 -07: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, IImageProcessor imageProcessor, IHttpClient httpClient) : base(serverConfig, userManager, libraryManager, isoManager, mediaEncoder, fileSystem, dlnaManager, subtitleEncoder, deviceManager, mediaSourceManager, zipClient, jsonSerializer, imageProcessor, httpClient)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-02-26 21:44:41 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
public object Get(GetVideoStream request)
|
|
|
|
{
|
2013-03-12 20:57:54 -07:00
|
|
|
return ProcessRequest(request, false);
|
2013-02-26 21:44:41 -07:00
|
|
|
}
|
2013-03-07 10:39:21 -07:00
|
|
|
|
2013-04-29 09:01:23 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Heads the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
2013-03-12 20:57:54 -07:00
|
|
|
public object Head(GetVideoStream request)
|
|
|
|
{
|
|
|
|
return ProcessRequest(request, true);
|
|
|
|
}
|
2013-03-20 07:38:56 -07:00
|
|
|
|
2015-03-28 13:22:27 -07:00
|
|
|
protected override string GetCommandLineArguments(string outputPath, StreamState state, bool isEncoding)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
// Get the output codec name
|
2015-07-25 10:21:10 -07:00
|
|
|
var videoCodec = GetVideoEncoder(state);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2013-03-07 10:39:21 -07:00
|
|
|
var format = string.Empty;
|
2013-03-12 19:56:13 -07:00
|
|
|
var keyFrame = string.Empty;
|
2013-03-07 10:39:21 -07:00
|
|
|
|
2013-03-12 19:56:13 -07:00
|
|
|
if (string.Equals(Path.GetExtension(outputPath), ".mp4", StringComparison.OrdinalIgnoreCase))
|
2013-03-07 10:39:21 -07:00
|
|
|
{
|
2015-02-05 22:39:07 -07:00
|
|
|
// Comparison: https://github.com/jansmolders86/mediacenterjs/blob/master/lib/transcoding/desktop.js
|
2013-03-12 19:56:13 -07:00
|
|
|
format = " -f mp4 -movflags frag_keyframe+empty_moov";
|
2013-03-07 10:39:21 -07:00
|
|
|
}
|
|
|
|
|
2014-03-12 12:56:12 -07:00
|
|
|
var threads = GetNumberOfThreads(state, string.Equals(videoCodec, "libvpx", StringComparison.OrdinalIgnoreCase));
|
2013-05-09 07:23:01 -07:00
|
|
|
|
2014-01-21 18:37:01 -07:00
|
|
|
var inputModifier = GetInputModifier(state);
|
|
|
|
|
2014-12-19 23:06:27 -07:00
|
|
|
return string.Format("{0} {1}{2} {3} {4} -map_metadata -1 -threads {5} {6}{7} -y \"{8}\"",
|
2014-01-21 18:37:01 -07:00
|
|
|
inputModifier,
|
2015-03-28 13:22:27 -07:00
|
|
|
GetInputArgument(state),
|
2013-03-12 19:56:13 -07:00
|
|
|
keyFrame,
|
2013-02-26 21:19:05 -07:00
|
|
|
GetMapArgs(state),
|
2014-06-10 10:36:06 -07:00
|
|
|
GetVideoArguments(state, videoCodec),
|
2013-05-09 07:23:01 -07:00
|
|
|
threads,
|
2013-02-26 21:19:05 -07:00
|
|
|
GetAudioArguments(state),
|
2013-03-07 10:39:21 -07:00
|
|
|
format,
|
2013-02-20 18:33:05 -07:00
|
|
|
outputPath
|
|
|
|
).Trim();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets video arguments to pass to ffmpeg
|
|
|
|
/// </summary>
|
2013-02-26 21:19:05 -07:00
|
|
|
/// <param name="state">The state.</param>
|
2013-03-28 08:52:26 -07:00
|
|
|
/// <param name="codec">The video codec.</param>
|
2013-02-20 18:33:05 -07:00
|
|
|
/// <returns>System.String.</returns>
|
2014-06-10 10:36:06 -07:00
|
|
|
private string GetVideoArguments(StreamState state, string codec)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2014-12-26 10:45:06 -07:00
|
|
|
var args = "-codec:v:0 " + codec;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2014-05-07 11:38:50 -07:00
|
|
|
if (state.EnableMpegtsM2TsMode)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2014-05-07 11:38:50 -07:00
|
|
|
args += " -mpegts_m2ts_mode 1";
|
2013-03-28 08:22:05 -07:00
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2014-05-07 11:38:50 -07:00
|
|
|
// See if we can save come cpu cycles by avoiding encoding
|
|
|
|
if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
|
2014-03-26 15:14:25 -07:00
|
|
|
{
|
2014-12-26 10:45:06 -07:00
|
|
|
return state.VideoStream != null && IsH264(state.VideoStream) && string.Equals(state.OutputContainer, "ts", StringComparison.OrdinalIgnoreCase) ?
|
|
|
|
args + " -bsf:v h264_mp4toannexb" :
|
|
|
|
args;
|
2014-03-26 15:14:25 -07:00
|
|
|
}
|
|
|
|
|
2014-06-29 20:04:50 -07:00
|
|
|
var keyFrameArg = string.Format(" -force_key_frames expr:gte(t,n_forced*{0})",
|
|
|
|
5.ToString(UsCulture));
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2013-03-28 08:22:05 -07:00
|
|
|
args += keyFrameArg;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2014-06-11 13:57:18 -07:00
|
|
|
var hasGraphicalSubs = state.SubtitleStream != null && !state.SubtitleStream.IsTextSubtitleStream;
|
2013-11-20 08:50:54 -07:00
|
|
|
|
2013-03-28 08:22:05 -07:00
|
|
|
// Add resolution params, if specified
|
2013-11-20 08:50:54 -07:00
|
|
|
if (!hasGraphicalSubs)
|
2013-03-28 08:22:05 -07:00
|
|
|
{
|
2014-08-16 22:38:13 -07:00
|
|
|
args += GetOutputSizeParam(state, codec);
|
2013-03-28 08:22:05 -07:00
|
|
|
}
|
2013-03-20 07:27:56 -07:00
|
|
|
|
2014-02-02 07:47:00 -07:00
|
|
|
var qualityParam = GetVideoQualityParam(state, codec, false);
|
2013-12-15 12:39:21 -07:00
|
|
|
|
2013-03-28 08:22:05 -07:00
|
|
|
if (!string.IsNullOrEmpty(qualityParam))
|
|
|
|
{
|
2013-12-15 12:39:21 -07:00
|
|
|
args += " " + qualityParam.Trim();
|
2013-03-28 08:22:05 -07:00
|
|
|
}
|
2013-03-28 07:48:56 -07:00
|
|
|
|
2013-11-20 08:50:54 -07:00
|
|
|
// This is for internal graphical subs
|
|
|
|
if (hasGraphicalSubs)
|
2013-03-28 08:52:26 -07:00
|
|
|
{
|
2014-07-29 20:31:35 -07:00
|
|
|
args += GetGraphicalSubtitleParam(state, codec);
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets audio arguments to pass to ffmpeg
|
|
|
|
/// </summary>
|
2013-02-26 21:19:05 -07:00
|
|
|
/// <param name="state">The state.</param>
|
2013-02-20 18:33:05 -07:00
|
|
|
/// <returns>System.String.</returns>
|
2013-02-26 21:19:05 -07:00
|
|
|
private string GetAudioArguments(StreamState state)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
// If the video doesn't have an audio stream, return a default.
|
2014-05-18 12:58:42 -07:00
|
|
|
if (state.AudioStream == null && state.VideoStream != null)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
return string.Empty;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the output codec name
|
2015-07-25 10:21:10 -07:00
|
|
|
var codec = GetAudioEncoder(state);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2014-12-26 10:45:06 -07:00
|
|
|
var args = "-codec:a:0 " + codec;
|
|
|
|
|
2015-06-02 20:15:46 -07:00
|
|
|
if (string.Equals(codec, "copy", StringComparison.OrdinalIgnoreCase))
|
2013-03-24 15:37:20 -07:00
|
|
|
{
|
2014-12-26 10:45:06 -07:00
|
|
|
return args;
|
2013-03-24 15:37:20 -07:00
|
|
|
}
|
2013-12-15 12:39:21 -07:00
|
|
|
|
2013-03-24 15:37:20 -07:00
|
|
|
// Add the number of audio channels
|
2014-04-22 17:50:47 -07:00
|
|
|
var channels = state.OutputAudioChannels;
|
2013-03-24 15:37:20 -07:00
|
|
|
|
|
|
|
if (channels.HasValue)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2013-03-24 15:37:20 -07:00
|
|
|
args += " -ac " + channels.Value;
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2014-04-22 17:50:47 -07:00
|
|
|
var bitrate = state.OutputAudioBitrate;
|
2013-08-29 19:13:58 -07:00
|
|
|
|
|
|
|
if (bitrate.HasValue)
|
2013-03-24 15:37:20 -07:00
|
|
|
{
|
2013-08-29 19:13:58 -07:00
|
|
|
args += " -ab " + bitrate.Value.ToString(UsCulture);
|
2013-03-24 15:37:20 -07:00
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2014-06-24 14:45:21 -07:00
|
|
|
args += " " + GetAudioFilterParam(state, false);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2013-12-15 12:39:21 -07:00
|
|
|
return args;
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|