diff --git a/MediaBrowser.Api/ApiEntryPoint.cs b/MediaBrowser.Api/ApiEntryPoint.cs index a21fc22fe..a6958b95d 100644 --- a/MediaBrowser.Api/ApiEntryPoint.cs +++ b/MediaBrowser.Api/ApiEntryPoint.cs @@ -274,32 +274,37 @@ namespace MediaBrowser.Api return null; } - job.ActiveRequestCount++; - - job.DisposeKillTimer(); + OnTranscodeBeginRequest(job); return job; } } + public void OnTranscodeBeginRequest(TranscodingJob job) + { + job.ActiveRequestCount++; + + job.DisposeKillTimer(); + } + public void OnTranscodeEndRequest(TranscodingJob job) { job.ActiveRequestCount--; if (job.ActiveRequestCount == 0) { - if (job.Type == TranscodingJobType.Progressive) - { - const int timerDuration = 1000; + // TODO: Lower this hls timeout + var timerDuration = job.Type == TranscodingJobType.Progressive ? + 1000 : + 14400000; - if (job.KillTimer == null) - { - job.KillTimer = new Timer(OnTranscodeKillTimerStopped, job, timerDuration, Timeout.Infinite); - } - else - { - job.KillTimer.Change(timerDuration, Timeout.Infinite); - } + if (job.KillTimer == null) + { + job.KillTimer = new Timer(OnTranscodeKillTimerStopped, job, timerDuration, Timeout.Infinite); + } + else + { + job.KillTimer.Change(timerDuration, Timeout.Infinite); } } } @@ -498,7 +503,7 @@ namespace MediaBrowser.Api .ToList(); Exception e = null; - + foreach (var file in filesToDelete) { try diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs index c9ae6669b..48523e255 100644 --- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs @@ -114,7 +114,7 @@ namespace MediaBrowser.Api.Playback.Hls if (File.Exists(segmentPath)) { - job = ApiEntryPoint.Instance.GetTranscodingJob(playlistPath, TranscodingJobType); + job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType); return await GetSegmentResult(playlistPath, segmentPath, requestedIndex, segmentLength, job, cancellationToken).ConfigureAwait(false); } @@ -123,7 +123,7 @@ namespace MediaBrowser.Api.Playback.Hls { if (File.Exists(segmentPath)) { - job = ApiEntryPoint.Instance.GetTranscodingJob(playlistPath, TranscodingJobType); + job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType); return await GetSegmentResult(playlistPath, segmentPath, requestedIndex, segmentLength, job, cancellationToken).ConfigureAwait(false); } else @@ -145,6 +145,7 @@ namespace MediaBrowser.Api.Playback.Hls request.StartTimeTicks = GetSeekPositionTicks(state, requestedIndex); job = await StartFfMpeg(state, playlistPath, cancellationTokenSource).ConfigureAwait(false); + ApiEntryPoint.Instance.OnTranscodeBeginRequest(job); } catch { @@ -168,7 +169,7 @@ namespace MediaBrowser.Api.Playback.Hls } Logger.Info("returning {0}", segmentPath); - job = job ?? ApiEntryPoint.Instance.GetTranscodingJob(playlistPath, TranscodingJobType); + job = job ?? ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType); return await GetSegmentResult(playlistPath, segmentPath, requestedIndex, segmentLength, job, cancellationToken).ConfigureAwait(false); } @@ -368,8 +369,8 @@ namespace MediaBrowser.Api.Playback.Hls if (transcodingJob != null) { transcodingJob.DownloadPositionTicks = Math.Max(transcodingJob.DownloadPositionTicks ?? segmentEndingPositionTicks, segmentEndingPositionTicks); + ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob); } - } }); } diff --git a/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs b/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs index da4ffb55d..1fe0661d9 100644 --- a/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs +++ b/MediaBrowser.Api/Playback/Hls/HlsSegmentService.cs @@ -1,6 +1,10 @@ using MediaBrowser.Controller; +using MediaBrowser.Controller.Configuration; +using MediaBrowser.Controller.Net; using ServiceStack; +using System; using System.IO; +using System.Linq; namespace MediaBrowser.Api.Playback.Hls { @@ -32,6 +36,8 @@ namespace MediaBrowser.Api.Playback.Hls [Api(Description = "Gets an Http live streaming segment file. Internal use only.")] public class GetHlsPlaylist { + // TODO: Deprecate with new iOS app + /// /// Gets or sets the id. /// @@ -52,22 +58,39 @@ namespace MediaBrowser.Api.Playback.Hls public string StreamId { get; set; } } + /// + /// Class GetHlsVideoSegment + /// + [Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.ts", "GET")] + [Api(Description = "Gets an Http live streaming segment file. Internal use only.")] + public class GetHlsVideoSegment : VideoStreamRequest + { + public string PlaylistId { get; set; } + + /// + /// Gets or sets the segment id. + /// + /// The segment id. + public string SegmentId { get; set; } + } + public class HlsSegmentService : BaseApiService { private readonly IServerApplicationPaths _appPaths; + private readonly IServerConfigurationManager _config; - public HlsSegmentService(IServerApplicationPaths appPaths) + public HlsSegmentService(IServerApplicationPaths appPaths, IServerConfigurationManager config) { _appPaths = appPaths; + _config = config; } public object Get(GetHlsPlaylist request) { var file = request.PlaylistId + Path.GetExtension(Request.PathInfo); - file = Path.Combine(_appPaths.TranscodingTempPath, file); - return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite); + return GetFileResult(file, file); } public void Delete(StopEncodingProcess request) @@ -80,13 +103,49 @@ namespace MediaBrowser.Api.Playback.Hls /// /// The request. /// System.Object. - public object Get(GetHlsAudioSegment request) + public object Get(GetHlsVideoSegment request) { var file = request.SegmentId + Path.GetExtension(Request.PathInfo); + file = Path.Combine(_config.ApplicationPaths.TranscodingTempPath, file); + var normalizedPlaylistId = request.PlaylistId.Replace("-low", string.Empty); + + var playlistPath = Directory.EnumerateFiles(_config.ApplicationPaths.TranscodingTempPath, "*") + .FirstOrDefault(i => string.Equals(Path.GetExtension(i), ".m3u8", StringComparison.OrdinalIgnoreCase) && i.IndexOf(normalizedPlaylistId, StringComparison.OrdinalIgnoreCase) != -1); + + return GetFileResult(file, playlistPath); + } + + /// + /// Gets the specified request. + /// + /// The request. + /// System.Object. + public object Get(GetHlsAudioSegment request) + { + // TODO: Deprecate with new iOS app + var file = request.SegmentId + Path.GetExtension(Request.PathInfo); file = Path.Combine(_appPaths.TranscodingTempPath, file); return ResultFactory.GetStaticFileResult(Request, file, FileShare.ReadWrite); } + + private object GetFileResult(string path, string playlistPath) + { + var transcodingJob = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType.Hls); + + return ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions + { + Path = path, + FileShare = FileShare.ReadWrite, + OnComplete = () => + { + if (transcodingJob != null) + { + ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob); + } + } + }); + } } } diff --git a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs index 69e86777b..e1baf8c12 100644 --- a/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs +++ b/MediaBrowser.Api/Playback/Hls/VideoHlsService.cs @@ -19,6 +19,8 @@ namespace MediaBrowser.Api.Playback.Hls [Api(Description = "Gets a video stream using HTTP live streaming.")] public class GetHlsVideoStream : VideoStreamRequest { + // TODO: Deprecate with new iOS app + [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; } @@ -35,22 +37,6 @@ namespace MediaBrowser.Api.Playback.Hls { } - /// - /// Class GetHlsVideoSegment - /// - [Route("/Videos/{Id}/hls/{PlaylistId}/{SegmentId}.ts", "GET")] - [Api(Description = "Gets an Http live streaming segment file. Internal use only.")] - public class GetHlsVideoSegment : VideoStreamRequest - { - public string PlaylistId { get; set; } - - /// - /// Gets or sets the segment id. - /// - /// The segment id. - public string SegmentId { get; set; } - } - /// /// Class VideoHlsService /// @@ -60,20 +46,6 @@ namespace MediaBrowser.Api.Playback.Hls { } - /// - /// Gets the specified request. - /// - /// The request. - /// System.Object. - public object Get(GetHlsVideoSegment request) - { - var file = request.SegmentId + Path.GetExtension(Request.PathInfo); - - file = Path.Combine(ServerConfigurationManager.ApplicationPaths.TranscodingTempPath, file); - - return ResultFactory.GetStaticFileResult(Request, file); - } - /// /// Gets the specified request. /// diff --git a/MediaBrowser.WebDashboard/Api/PackageCreator.cs b/MediaBrowser.WebDashboard/Api/PackageCreator.cs index 4262a314d..0a995a845 100644 --- a/MediaBrowser.WebDashboard/Api/PackageCreator.cs +++ b/MediaBrowser.WebDashboard/Api/PackageCreator.cs @@ -288,7 +288,7 @@ namespace MediaBrowser.WebDashboard.Api "thirdparty/apiclient/ajax.js", "thirdparty/apiclient/events.js", "thirdparty/apiclient/deferred.js", - "thirdparty/apiclient/mediabrowser.apiclient.js", + "thirdparty/apiclient/apiclient.js", "thirdparty/apiclient/connectservice.js", "thirdparty/apiclient/serverdiscovery.js", "thirdparty/apiclient/connectionmanager.js" diff --git a/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj b/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj index d8ab31551..4dcc0c6a2 100644 --- a/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj +++ b/MediaBrowser.WebDashboard/MediaBrowser.WebDashboard.csproj @@ -190,7 +190,7 @@ PreserveNewest - + PreserveNewest @@ -202,7 +202,7 @@ PreserveNewest - + PreserveNewest