2013-11-01 08:59:42 -07:00
using MediaBrowser.Common.IO ;
2013-10-31 07:03:23 -07:00
using MediaBrowser.Common.MediaInfo ;
2013-03-03 22:43:06 -07:00
using MediaBrowser.Controller ;
2013-09-04 10:02:19 -07:00
using MediaBrowser.Controller.Dto ;
2013-02-27 16:33:40 -07:00
using MediaBrowser.Controller.Library ;
2013-12-05 20:39:44 -07:00
using MediaBrowser.Controller.Persistence ;
2013-08-09 18:16:31 -07:00
using MediaBrowser.Model.IO ;
2013-03-08 23:13:10 -07:00
using ServiceStack.ServiceHost ;
2013-07-02 11:24:29 -07:00
using System ;
2013-02-27 16:33:40 -07:00
namespace MediaBrowser.Api.Playback.Hls
{
2013-04-29 09:01:23 -07:00
/// <summary>
/// Class GetHlsVideoStream
/// </summary>
2013-03-08 23:13:10 -07:00
[Route("/Videos/{Id}/stream.m3u8", "GET")]
2013-03-28 07:42:03 -07:00
[Api(Description = "Gets a video stream using HTTP live streaming.")]
2013-03-08 23:13:10 -07:00
public class GetHlsVideoStream : VideoStreamRequest
{
2013-09-06 10:27:06 -07:00
[ApiMember(Name = "BaselineStreamAudioBitRate", Description = "Optional. Specify the audio bitrate for the baseline stream.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? BaselineStreamAudioBitRate { get ; set ; }
2013-03-08 23:13:10 -07:00
2013-09-06 10:27:06 -07:00
[ApiMember(Name = "AppendBaselineStream", Description = "Optional. Whether or not to include a baseline audio-only stream in the master playlist.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool AppendBaselineStream { get ; set ; }
2013-09-19 13:03:14 -07:00
[ApiMember(Name = "TimeStampOffsetMs", Description = "Optional. Alter the timestamps in the playlist by a given amount, in ms. Default is 1000.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int TimeStampOffsetMs { get ; set ; }
2013-03-08 23:13:10 -07:00
}
2013-04-29 09:01:23 -07:00
/// <summary>
/// Class VideoHlsService
/// </summary>
2013-02-27 16:33:40 -07:00
public class VideoHlsService : BaseHlsService
{
2013-12-05 20:39:44 -07:00
public VideoHlsService ( IServerApplicationPaths appPaths , IUserManager userManager , ILibraryManager libraryManager , IIsoManager isoManager , IMediaEncoder mediaEncoder , IDtoService dtoService , IFileSystem fileSystem , IItemRepository itemRepository )
: base ( appPaths , userManager , libraryManager , isoManager , mediaEncoder , dtoService , fileSystem , itemRepository )
2013-02-27 16:33:40 -07:00
{
}
2013-03-08 23:13:10 -07: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-04-29 09:01:23 -07:00
2013-02-27 16:33:40 -07: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 07:27:56 -07:00
var codec = GetAudioCodec ( state . Request ) ;
if ( codec . Equals ( "copy" , StringComparison . OrdinalIgnoreCase ) )
2013-02-27 16:33:40 -07: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 ;
}
2013-08-29 19:13:58 -07:00
var bitrate = GetAudioBitrateParam ( state ) ;
if ( bitrate . HasValue )
2013-02-27 16:33:40 -07:00
{
2013-08-29 19:13:58 -07:00
args + = " -ab " + bitrate . Value . ToString ( UsCulture ) ;
2013-02-27 16:33:40 -07:00
}
2013-03-24 15:37:20 -07:00
var volParam = string . Empty ;
2013-11-20 08:50:54 -07:00
var audioSampleRate = string . Empty ;
2013-03-24 15:37:20 -07: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 )
{
2013-10-24 04:58:35 -07:00
volParam = ",volume=2.000000" ;
2013-03-24 15:37:20 -07:00
}
2013-12-05 20:39:44 -07:00
2013-10-27 21:01:35 -07:00
if ( state . Request . AudioSampleRate . HasValue )
{
2013-12-05 20:39:44 -07:00
audioSampleRate = state . Request . AudioSampleRate . Value + ":" ;
2013-10-27 21:01:35 -07:00
}
2013-10-24 04:58:35 -07:00
2013-12-05 20:39:44 -07:00
args + = string . Format ( " -af \"adelay=1,aresample={0}async=1000{1}\"" , audioSampleRate , volParam ) ;
2013-03-20 07:27:56 -07:00
2013-02-27 16:33:40 -07:00
return args ;
}
return args ;
}
/// <summary>
/// Gets the video arguments.
/// </summary>
/// <param name="state">The state.</param>
2013-04-29 09:01:23 -07:00
/// <param name="performSubtitleConversion">if set to <c>true</c> [perform subtitle conversion].</param>
2013-02-27 16:33:40 -07:00
/// <returns>System.String.</returns>
2013-04-29 09:01:23 -07:00
protected override string GetVideoArguments ( StreamState state , bool performSubtitleConversion )
2013-02-27 16:33:40 -07:00
{
2013-03-08 19:34:54 -07:00
var codec = GetVideoCodec ( state . VideoRequest ) ;
2013-02-27 16:33:40 -07: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-09-02 13:19:11 -07:00
const string keyFrameArg = " -force_key_frames expr:if(isnan(prev_forced_t),gte(t,.1),gte(t,prev_forced_t+5))" ;
2013-03-28 08:22:05 -07:00
2013-11-20 08:50:54 -07: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 ) ;
2013-12-05 20:39:44 -07:00
2013-03-28 08:22:05 -07:00
var args = "-codec:v:0 " + codec + " -preset superfast" + keyFrameArg ;
2013-02-27 16:33:40 -07:00
2013-08-29 19:13:58 -07:00
var bitrate = GetVideoBitrateParam ( state ) ;
2013-02-27 16:33:40 -07:00
2013-08-29 19:13:58 -07:00
if ( bitrate . HasValue )
{
2013-09-27 05:00:47 -07:00
args + = string . Format ( " -b:v {0} -maxrate ({0}*.85) -bufsize {0}" , bitrate . Value . ToString ( UsCulture ) ) ;
2013-06-02 07:47:28 -07:00
}
2013-12-05 20:39:44 -07:00
2013-02-27 16:33:40 -07:00
// Add resolution params, if specified
2013-11-20 08:50:54 -07:00
if ( ! hasGraphicalSubs )
2013-02-27 16:33:40 -07:00
{
2013-11-20 08:50:54 -07:00
if ( state . VideoRequest . Width . HasValue | | state . VideoRequest . Height . HasValue | | state . VideoRequest . MaxHeight . HasValue | | state . VideoRequest . MaxWidth . HasValue )
{
args + = GetOutputSizeParam ( state , codec , performSubtitleConversion ) ;
}
2013-02-27 16:33:40 -07:00
}
2013-11-13 09:45:58 -07:00
if ( state . VideoRequest . Framerate . HasValue )
2013-02-27 16:33:40 -07:00
{
2013-11-13 09:45:58 -07:00
args + = string . Format ( " -r {0}" , state . VideoRequest . Framerate . Value ) ;
2013-02-27 16:33:40 -07:00
}
2013-03-20 07:27:56 -07:00
args + = " -vsync vfr" ;
2013-03-23 20:56:01 -07:00
2013-03-28 07:42:03 -07:00
if ( ! string . IsNullOrEmpty ( state . VideoRequest . Profile ) )
{
2013-03-29 05:26:40 -07:00
args + = " -profile:v " + state . VideoRequest . Profile ;
2013-03-28 07:42:03 -07:00
}
if ( ! string . IsNullOrEmpty ( state . VideoRequest . Level ) )
{
2013-03-28 22:45:45 -07:00
args + = " -level " + state . VideoRequest . Level ;
2013-03-28 07:42:03 -07:00
}
2013-11-20 08:50:54 -07:00
// This is for internal graphical subs
if ( hasGraphicalSubs )
2013-03-23 20:56:01 -07:00
{
2013-11-20 08:50:54 -07:00
args + = GetInternalGraphicalSubtitleParam ( state , codec ) ;
2013-03-23 20:56:01 -07:00
}
2013-12-05 20:39:44 -07:00
2013-02-27 16:33:40 -07: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" ;
}
}
}