2013-03-09 06:13:10 +00:00
|
|
|
|
using System.IO;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
2013-03-04 05:43:06 +00:00
|
|
|
|
using MediaBrowser.Controller;
|
2013-02-27 23:33:40 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using System;
|
2013-03-09 06:13:10 +00:00
|
|
|
|
using ServiceStack.ServiceHost;
|
2013-02-27 23:33:40 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api.Playback.Hls
|
|
|
|
|
{
|
2013-03-09 06:13:10 +00:00
|
|
|
|
[Route("/Videos/{Id}/stream.m3u8", "GET")]
|
2013-03-28 14:42:03 +00:00
|
|
|
|
[Api(Description = "Gets a video stream using HTTP live streaming.")]
|
2013-03-09 06:13:10 +00:00
|
|
|
|
public class GetHlsVideoStream : VideoStreamRequest
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-11 03:12:21 +00:00
|
|
|
|
[Route("/Videos/{Id}/segments/{SegmentId}/stream.ts", "GET")]
|
2013-03-28 14:42:03 +00:00
|
|
|
|
[Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
|
2013-03-09 06:13:10 +00:00
|
|
|
|
public class GetHlsVideoSegment
|
|
|
|
|
{
|
|
|
|
|
public string Id { get; set; }
|
|
|
|
|
|
|
|
|
|
public string SegmentId { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-27 23:33:40 +00:00
|
|
|
|
public class VideoHlsService : BaseHlsService
|
|
|
|
|
{
|
2013-03-04 05:43:06 +00:00
|
|
|
|
public VideoHlsService(IServerApplicationPaths appPaths, IUserManager userManager, ILibraryManager libraryManager, IIsoManager isoManager)
|
|
|
|
|
: base(appPaths, userManager, libraryManager, isoManager)
|
2013-02-27 23:33:40 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-09 06:13:10 +00:00
|
|
|
|
public object Get(GetHlsVideoSegment request)
|
|
|
|
|
{
|
2013-03-23 04:04:36 +00:00
|
|
|
|
var file = SegmentFilePrefix + request.SegmentId + Path.GetExtension(RequestContext.PathInfo);
|
2013-03-09 06:13:10 +00:00
|
|
|
|
|
|
|
|
|
file = Path.Combine(ApplicationPaths.EncodedMediaCachePath, file);
|
|
|
|
|
|
2013-03-24 02:45:00 +00:00
|
|
|
|
return ResultFactory.GetStaticFileResult(RequestContext, file);
|
2013-03-09 06:13:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
public object Get(GetHlsVideoStream request)
|
|
|
|
|
{
|
|
|
|
|
return ProcessRequest(request);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-27 23:33:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the audio arguments.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
protected override string GetAudioArguments(StreamState state)
|
|
|
|
|
{
|
2013-03-20 14:27:56 +00:00
|
|
|
|
var codec = GetAudioCodec(state.Request);
|
|
|
|
|
|
|
|
|
|
if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
|
2013-02-27 23:33:40 +00:00
|
|
|
|
{
|
|
|
|
|
return "-codec:a:0 copy";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var args = "-codec:a:0 " + codec;
|
|
|
|
|
|
|
|
|
|
if (state.AudioStream != null)
|
|
|
|
|
{
|
|
|
|
|
var channels = GetNumAudioChannelsParam(state.Request, state.AudioStream);
|
|
|
|
|
|
|
|
|
|
if (channels.HasValue)
|
|
|
|
|
{
|
|
|
|
|
args += " -ac " + channels.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state.Request.AudioSampleRate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
args += " -ar " + state.Request.AudioSampleRate.Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (state.Request.AudioBitRate.HasValue)
|
|
|
|
|
{
|
|
|
|
|
args += " -ab " + state.Request.AudioBitRate.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 22:37:20 +00:00
|
|
|
|
var volParam = string.Empty;
|
|
|
|
|
|
|
|
|
|
// 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-03-24 22:45:53 +00:00
|
|
|
|
args += string.Format(" -af \"aresample=async=1000{0}\"", volParam);
|
2013-03-20 14:27:56 +00:00
|
|
|
|
|
2013-02-27 23:33:40 +00:00
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the video arguments.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
protected override string GetVideoArguments(StreamState state)
|
|
|
|
|
{
|
2013-03-09 02:34:54 +00:00
|
|
|
|
var codec = GetVideoCodec(state.VideoRequest);
|
2013-02-27 23:33:40 +00:00
|
|
|
|
|
|
|
|
|
// See if we can save come cpu cycles by avoiding encoding
|
|
|
|
|
if (codec.Equals("copy", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
{
|
|
|
|
|
return IsH264(state.VideoStream) ? "-codec:v:0 copy -bsf h264_mp4toannexb" : "-codec:v:0 copy";
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-28 15:22:05 +00:00
|
|
|
|
const string keyFrameArg = " -force_key_frames expr:if(isnan(prev_forced_t),gte(t,0),gte(t,prev_forced_t+5))";
|
|
|
|
|
|
|
|
|
|
var args = "-codec:v:0 " + codec + " -preset superfast" + keyFrameArg;
|
2013-02-27 23:33:40 +00:00
|
|
|
|
|
2013-03-09 02:34:54 +00:00
|
|
|
|
if (state.VideoRequest.VideoBitRate.HasValue)
|
2013-02-27 23:33:40 +00:00
|
|
|
|
{
|
2013-03-09 02:34:54 +00:00
|
|
|
|
args += string.Format(" -b:v {0}", state.VideoRequest.VideoBitRate.Value);
|
2013-02-27 23:33:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add resolution params, if specified
|
2013-03-09 02:34:54 +00:00
|
|
|
|
if (state.VideoRequest.Width.HasValue || state.VideoRequest.Height.HasValue || state.VideoRequest.MaxHeight.HasValue || state.VideoRequest.MaxWidth.HasValue)
|
2013-02-27 23:33:40 +00:00
|
|
|
|
{
|
|
|
|
|
args += GetOutputSizeParam(state, codec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the output framerate based on the FrameRate param
|
2013-03-20 14:27:56 +00:00
|
|
|
|
var framerate = state.VideoRequest.Framerate ?? 0;
|
2013-02-27 23:33:40 +00:00
|
|
|
|
|
|
|
|
|
// We have to supply a framerate for hls, so if it's null, account for that here
|
|
|
|
|
if (framerate.Equals(0))
|
|
|
|
|
{
|
|
|
|
|
framerate = state.VideoStream.AverageFrameRate ?? 0;
|
|
|
|
|
}
|
|
|
|
|
if (framerate.Equals(0))
|
|
|
|
|
{
|
|
|
|
|
framerate = state.VideoStream.RealFrameRate ?? 0;
|
|
|
|
|
}
|
|
|
|
|
if (framerate.Equals(0))
|
|
|
|
|
{
|
|
|
|
|
framerate = 23.976;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-23 04:37:22 +00:00
|
|
|
|
framerate = Math.Round(framerate);
|
|
|
|
|
|
2013-02-27 23:33:40 +00:00
|
|
|
|
args += string.Format(" -r {0}", framerate);
|
|
|
|
|
|
2013-03-20 14:27:56 +00:00
|
|
|
|
args += " -vsync vfr";
|
2013-03-24 03:56:01 +00:00
|
|
|
|
|
2013-03-28 14:42:03 +00:00
|
|
|
|
if (!string.IsNullOrEmpty(state.VideoRequest.Profile))
|
|
|
|
|
{
|
2013-03-28 15:52:26 +00:00
|
|
|
|
args += " -profile:v" + state.VideoRequest.Profile;
|
2013-03-28 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(state.VideoRequest.Level))
|
|
|
|
|
{
|
2013-03-28 15:52:26 +00:00
|
|
|
|
args += " -level 3" + state.VideoRequest.Level;
|
2013-03-28 14:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 03:56:01 +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-27 23:33:40 +00:00
|
|
|
|
return args;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the segment file extension.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
/// <returns>System.String.</returns>
|
|
|
|
|
protected override string GetSegmentFileExtension(StreamState state)
|
|
|
|
|
{
|
|
|
|
|
return ".ts";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|