2013-09-24 19:54:42 +00:00
using MediaBrowser.Controller ;
2013-12-07 15:52:38 +00:00
using ServiceStack ;
2013-09-24 19:54:42 +00:00
using System ;
using System.IO ;
using System.Linq ;
using System.Threading.Tasks ;
namespace MediaBrowser.Api.Playback.Hls
{
/// <summary>
/// Class GetHlsAudioSegment
/// </summary>
[Route("/Audio/{Id}/hls/{SegmentId}/stream.mp3", "GET")]
[Route("/Audio/{Id}/hls/{SegmentId}/stream.aac", "GET")]
[Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
public class GetHlsAudioSegment
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public string Id { get ; set ; }
/// <summary>
/// Gets or sets the segment id.
/// </summary>
/// <value>The segment id.</value>
public string SegmentId { get ; set ; }
}
/// <summary>
/// Class GetHlsVideoSegment
/// </summary>
[Route("/Videos/{Id}/hls/{PlaylistId}/stream.m3u8", "GET")]
[Api(Description = "Gets an Http live streaming segment file. Internal use only.")]
public class GetHlsPlaylist
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public string Id { get ; set ; }
public string PlaylistId { get ; set ; }
}
2013-10-03 15:24:32 +00:00
[Route("/Videos/ActiveEncodings", "DELETE")]
2013-10-03 14:14:40 +00:00
[Api(Description = "Stops an encoding process")]
public class StopEncodingProcess
{
[ApiMember(Name = "DeviceId", Description = "The device id of the client requesting. Used to stop encoding processes when needed.", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
public string DeviceId { get ; set ; }
}
2013-09-24 19:54:42 +00:00
public class HlsSegmentService : BaseApiService
{
private readonly IServerApplicationPaths _appPaths ;
public HlsSegmentService ( IServerApplicationPaths appPaths )
{
_appPaths = appPaths ;
}
public object Get ( GetHlsPlaylist request )
{
2014-09-03 02:30:24 +00:00
var normalizedPlaylistId = request . PlaylistId . Replace ( "-low" , string . Empty ) ;
foreach ( var playlist in Directory . EnumerateFiles ( _appPaths . TranscodingTempPath , "*.m3u8" )
. Where ( i = > i . IndexOf ( normalizedPlaylistId , StringComparison . OrdinalIgnoreCase ) ! = - 1 )
. ToList ( ) )
{
if ( ! string . IsNullOrEmpty ( playlist ) )
{
ExtendPlaylistTimer ( playlist ) ;
}
}
2013-09-24 19:54:42 +00:00
2013-12-07 15:52:38 +00:00
var file = request . PlaylistId + Path . GetExtension ( Request . PathInfo ) ;
2013-09-24 19:54:42 +00:00
2014-01-11 18:58:50 +00:00
file = Path . Combine ( _appPaths . TranscodingTempPath , file ) ;
2013-09-24 19:54:42 +00:00
2013-12-07 15:52:38 +00:00
return ResultFactory . GetStaticFileResult ( Request , file , FileShare . ReadWrite ) ;
2013-09-24 19:54:42 +00:00
}
2013-10-03 14:14:40 +00:00
public void Delete ( StopEncodingProcess request )
{
2014-07-08 01:41:03 +00:00
var task = ApiEntryPoint . Instance . KillTranscodingJobs ( request . DeviceId , path = > true , true ) ;
2014-07-02 18:34:08 +00:00
Task . WaitAll ( task ) ;
2013-10-03 14:14:40 +00:00
}
2013-09-24 19:54:42 +00:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetHlsAudioSegment request )
{
2013-12-07 15:52:38 +00:00
var file = request . SegmentId + Path . GetExtension ( Request . PathInfo ) ;
2013-09-24 19:54:42 +00:00
2014-01-11 18:58:50 +00:00
file = Path . Combine ( _appPaths . TranscodingTempPath , file ) ;
2013-09-24 19:54:42 +00:00
2013-12-07 15:52:38 +00:00
return ResultFactory . GetStaticFileResult ( Request , file , FileShare . ReadWrite ) ;
2013-09-24 19:54:42 +00:00
}
2013-12-30 02:41:22 +00:00
private async void ExtendPlaylistTimer ( string playlist )
2013-09-24 19:54:42 +00:00
{
2014-09-03 02:30:24 +00:00
var job = ApiEntryPoint . Instance . OnTranscodeBeginRequest ( playlist , TranscodingJobType . Hls ) ;
2013-09-24 19:54:42 +00:00
2013-12-30 02:41:22 +00:00
await Task . Delay ( 20000 ) . ConfigureAwait ( false ) ;
2013-09-24 19:54:42 +00:00
2014-09-03 02:30:24 +00:00
if ( job ! = null )
{
ApiEntryPoint . Instance . OnTranscodeEndRequest ( job ) ;
}
2013-09-24 19:54:42 +00:00
}
}
}