2013-11-01 08:59:42 -07:00
using MediaBrowser.Common.IO ;
2014-05-18 12:58:42 -07:00
using MediaBrowser.Controller.Channels ;
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-02-27 16:33:40 -07:00
using MediaBrowser.Controller.Library ;
2013-12-21 11:37:34 -07:00
using MediaBrowser.Controller.LiveTv ;
2014-02-20 09:37:41 -07:00
using MediaBrowser.Controller.MediaEncoding ;
2013-08-09 18:16:31 -07:00
using MediaBrowser.Model.IO ;
2013-12-07 08:52:38 -07:00
using ServiceStack ;
2013-07-02 11:24:29 -07:00
using System ;
2014-01-12 22:41:00 -07:00
using System.IO ;
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
}
2014-07-01 14:13:32 -07:00
[Route("/Videos/{Id}/live.m3u8", "GET")]
[Api(Description = "Gets a video stream using HTTP live streaming.")]
public class GetLiveHlsStream : VideoStreamRequest
{
}
2014-09-17 21:50:21 -07:00
2014-01-12 22:41:00 -07:00
/// <summary>
/// Class GetHlsVideoSegment
/// </summary>
[Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.ts", "GET")]
[Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
2014-01-23 11:05:41 -07:00
public class GetHlsVideoSegment : VideoStreamRequest
2014-01-12 22:41:00 -07:00
{
public string PlaylistId { get ; set ; }
/// <summary>
/// Gets or sets the segment id.
/// </summary>
/// <value>The segment id.</value>
public string SegmentId { get ; set ; }
}
2013-04-29 09:01:23 -07:00
/// <summary>
/// Class VideoHlsService
/// </summary>
2013-02-27 16:33:40 -07:00
public class VideoHlsService : BaseHlsService
{
2015-01-19 22:19:13 -07:00
public VideoHlsService ( IServerConfigurationManager serverConfig , IUserManager userManager , ILibraryManager libraryManager , IIsoManager isoManager , IMediaEncoder mediaEncoder , IFileSystem fileSystem , ILiveTvManager liveTvManager , IDlnaManager dlnaManager , IChannelManager channelManager , ISubtitleEncoder subtitleEncoder , IDeviceManager deviceManager ) : base ( serverConfig , userManager , libraryManager , isoManager , mediaEncoder , fileSystem , liveTvManager , dlnaManager , channelManager , subtitleEncoder , deviceManager )
2013-02-27 16:33:40 -07:00
{
}
2014-01-12 22:41:00 -07:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetHlsVideoSegment request )
{
var file = request . SegmentId + Path . GetExtension ( Request . PathInfo ) ;
2015-01-16 21:29:53 -07:00
file = Path . Combine ( ServerConfigurationManager . ApplicationPaths . TranscodingTempPath , file ) ;
2014-01-12 22:41:00 -07:00
2014-09-02 19:30:24 -07:00
return ResultFactory . GetStaticFileResult ( Request , file ) ;
2014-01-12 14:32:13 -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 )
{
2014-07-01 14:13:32 -07:00
return ProcessRequest ( request , false ) ;
}
public object Get ( GetLiveHlsStream request )
{
return ProcessRequest ( request , true ) ;
2013-03-08 23:13:10 -07:00
}
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 )
{
2014-06-05 17:39:02 -07:00
var codec = state . OutputAudioCodec ;
2013-03-20 07:27:56 -07:00
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 ;
2014-06-23 09:05:19 -07:00
var channels = state . OutputAudioChannels ;
2013-02-27 16:33:40 -07:00
2014-06-23 09:05:19 -07:00
if ( channels . HasValue )
{
args + = " -ac " + channels . Value ;
}
2013-02-27 16:33:40 -07:00
2014-06-23 09:05:19 -07:00
var bitrate = state . OutputAudioBitrate ;
2013-03-20 07:27:56 -07:00
2014-06-23 09:05:19 -07:00
if ( bitrate . HasValue )
{
args + = " -ab " + bitrate . Value . ToString ( UsCulture ) ;
2013-02-27 16:33:40 -07:00
}
2014-06-23 09:05:19 -07:00
args + = " " + GetAudioFilterParam ( state , true ) ;
2013-02-27 16:33:40 -07:00
return args ;
}
/// <summary>
/// Gets the video arguments.
/// </summary>
/// <param name="state">The state.</param>
/// <returns>System.String.</returns>
2014-06-10 10:36:06 -07:00
protected override string GetVideoArguments ( StreamState state )
2013-02-27 16:33:40 -07:00
{
2014-06-05 17:39:02 -07:00
var codec = state . OutputVideoCodec ;
2013-02-27 16:33:40 -07:00
2014-12-26 10:45:06 -07:00
var args = "-codec:v:0 " + codec ;
if ( state . EnableMpegtsM2TsMode )
{
args + = " -mpegts_m2ts_mode 1" ;
}
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 ) )
{
2014-12-26 10:45:06 -07:00
return state . VideoStream ! = null & & IsH264 ( state . VideoStream ) ?
args + " -bsf:v h264_mp4toannexb" :
args ;
2013-02-27 16:33:40 -07:00
}
2014-12-26 10:45:06 -07:00
2014-06-26 10:04:11 -07:00
var keyFrameArg = string . Format ( " -force_key_frames expr:gte(t,n_forced*{0})" ,
state . SegmentLength . ToString ( UsCulture ) ) ;
2013-03-28 08:22:05 -07:00
2014-06-11 13:57:18 -07:00
var hasGraphicalSubs = state . SubtitleStream ! = null & & ! state . SubtitleStream . IsTextSubtitleStream ;
2013-12-05 20:39:44 -07:00
2014-12-26 10:45:06 -07:00
args + = " " + GetVideoQualityParam ( state , H264Encoder , true ) + keyFrameArg ;
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
{
2014-08-16 22:38:13 -07:00
args + = GetOutputSizeParam ( state , codec ) ;
2013-02-27 16:33:40 -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
{
2014-07-29 20:31:35 -07:00
args + = GetGraphicalSubtitleParam ( 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" ;
}
}
}