2013-09-24 12:54:42 -07:00
using MediaBrowser.Controller ;
2015-03-25 11:29:21 -07:00
using MediaBrowser.Controller.Configuration ;
using MediaBrowser.Controller.Net ;
using System ;
2013-09-24 12:54:42 -07:00
using System.IO ;
2015-03-25 11:29:21 -07:00
using System.Linq ;
2016-06-18 23:18:29 -07:00
using System.Threading.Tasks ;
2017-05-25 23:48:54 -07:00
2017-04-09 14:38:59 -07:00
using MediaBrowser.Controller.MediaEncoding ;
2016-10-25 12:02:04 -07:00
using MediaBrowser.Model.IO ;
using MediaBrowser.Model.Services ;
2013-09-24 12:54:42 -07:00
namespace MediaBrowser.Api.Playback.Hls
{
/// <summary>
/// Class GetHlsAudioSegment
/// </summary>
2017-04-27 11:24:04 -07:00
// Can't require authentication just yet due to seeing some requests come from Chrome without full query string
//[Authenticated]
2013-09-24 12:54:42 -07:00
[Route("/Audio/{Id}/hls/{SegmentId}/stream.mp3", "GET")]
[Route("/Audio/{Id}/hls/{SegmentId}/stream.aac", "GET")]
2015-05-24 11:33:28 -07:00
public class GetHlsAudioSegmentLegacy
2013-09-24 12:54:42 -07:00
{
2015-05-24 11:33:28 -07:00
// TODO: Deprecate with new iOS app
2013-09-24 12:54:42 -07:00
/// <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")]
2017-04-27 11:24:04 -07:00
[Authenticated]
2015-05-24 11:33:28 -07:00
public class GetHlsPlaylistLegacy
2013-09-24 12:54:42 -07:00
{
2015-03-25 11:29:21 -07:00
// TODO: Deprecate with new iOS app
2013-09-24 12:54:42 -07:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
public string Id { get ; set ; }
public string PlaylistId { get ; set ; }
}
2013-10-03 08:24:32 -07:00
[Route("/Videos/ActiveEncodings", "DELETE")]
2017-04-27 11:24:04 -07:00
[Authenticated]
2013-10-03 07:14:40 -07:00
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 ; }
2015-03-19 09:16:33 -07:00
2015-03-29 11:31:28 -07:00
[ApiMember(Name = "PlaySessionId", Description = "The play session id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "DELETE")]
public string PlaySessionId { get ; set ; }
2013-10-03 07:14:40 -07:00
}
2015-03-25 11:29:21 -07:00
/// <summary>
/// Class GetHlsVideoSegment
/// </summary>
2017-04-27 11:24:04 -07:00
// Can't require authentication just yet due to seeing some requests come from Chrome without full query string
//[Authenticated]
2017-03-15 12:57:18 -07:00
[Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.{SegmentContainer}", "GET")]
2015-05-24 11:33:28 -07:00
public class GetHlsVideoSegmentLegacy : VideoStreamRequest
2015-03-25 11:29:21 -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-09-24 12:54:42 -07:00
public class HlsSegmentService : BaseApiService
{
private readonly IServerApplicationPaths _appPaths ;
2015-03-25 11:29:21 -07:00
private readonly IServerConfigurationManager _config ;
2016-10-31 20:07:45 -07:00
private readonly IFileSystem _fileSystem ;
2013-09-24 12:54:42 -07:00
2016-10-31 20:07:45 -07:00
public HlsSegmentService ( IServerApplicationPaths appPaths , IServerConfigurationManager config , IFileSystem fileSystem )
2013-09-24 12:54:42 -07:00
{
_appPaths = appPaths ;
2015-03-25 11:29:21 -07:00
_config = config ;
2016-10-31 20:07:45 -07:00
_fileSystem = fileSystem ;
2013-09-24 12:54:42 -07:00
}
2016-06-18 23:18:29 -07:00
public Task < object > Get ( GetHlsPlaylistLegacy request )
2013-09-24 12:54:42 -07:00
{
2013-12-07 08:52:38 -07:00
var file = request . PlaylistId + Path . GetExtension ( Request . PathInfo ) ;
2015-01-16 21:29:53 -07:00
file = Path . Combine ( _appPaths . TranscodingTempPath , file ) ;
2013-09-24 12:54:42 -07:00
2015-03-25 11:29:21 -07:00
return GetFileResult ( file , file ) ;
2013-09-24 12:54:42 -07:00
}
2013-10-03 07:14:40 -07:00
public void Delete ( StopEncodingProcess request )
{
2015-03-29 11:31:28 -07:00
ApiEntryPoint . Instance . KillTranscodingJobs ( request . DeviceId , request . PlaySessionId , path = > true ) ;
2013-10-03 07:14:40 -07:00
}
2013-09-24 12:54:42 -07:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2016-06-18 23:18:29 -07:00
public Task < object > Get ( GetHlsVideoSegmentLegacy request )
2013-09-24 12:54:42 -07:00
{
2013-12-07 08:52:38 -07:00
var file = request . SegmentId + Path . GetExtension ( Request . PathInfo ) ;
2017-03-18 23:10:11 -07:00
var transcodeFolderPath = _config . ApplicationPaths . TranscodingTempPath ;
file = Path . Combine ( transcodeFolderPath , file ) ;
2015-03-25 11:29:21 -07:00
2016-09-18 13:38:38 -07:00
var normalizedPlaylistId = request . PlaylistId ;
2015-03-25 11:29:21 -07:00
2017-03-18 23:10:11 -07:00
var playlistPath = _fileSystem . GetFilePaths ( transcodeFolderPath )
2015-03-25 11:29:21 -07:00
. FirstOrDefault ( i = > string . Equals ( Path . GetExtension ( i ) , ".m3u8" , StringComparison . OrdinalIgnoreCase ) & & i . IndexOf ( normalizedPlaylistId , StringComparison . OrdinalIgnoreCase ) ! = - 1 ) ;
return GetFileResult ( file , playlistPath ) ;
}
2013-09-24 12:54:42 -07:00
2015-03-25 11:29:21 -07:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2016-11-10 14:06:00 -07:00
public Task < object > Get ( GetHlsAudioSegmentLegacy request )
2015-03-25 11:29:21 -07:00
{
// TODO: Deprecate with new iOS app
var file = request . SegmentId + Path . GetExtension ( Request . PathInfo ) ;
2015-01-16 21:29:53 -07:00
file = Path . Combine ( _appPaths . TranscodingTempPath , file ) ;
2013-09-24 12:54:42 -07:00
2016-11-10 14:06:00 -07:00
return ResultFactory . GetStaticFileResult ( Request , file , FileShareMode . ReadWrite ) ;
2013-09-24 12:54:42 -07:00
}
2015-03-25 11:29:21 -07:00
2016-06-18 23:18:29 -07:00
private Task < object > GetFileResult ( string path , string playlistPath )
2015-03-25 11:29:21 -07:00
{
var transcodingJob = ApiEntryPoint . Instance . OnTranscodeBeginRequest ( playlistPath , TranscodingJobType . Hls ) ;
return ResultFactory . GetStaticFileResult ( Request , new StaticFileResultOptions
{
Path = path ,
2016-10-25 12:02:04 -07:00
FileShare = FileShareMode . ReadWrite ,
2015-03-25 11:29:21 -07:00
OnComplete = ( ) = >
{
if ( transcodingJob ! = null )
{
ApiEntryPoint . Instance . OnTranscodeEndRequest ( transcodingJob ) ;
}
}
} ) ;
}
2013-09-24 12:54:42 -07:00
}
}