The Free Software Media System - Server Backend & API
Stop waiting for a segment to become ready if there's no alive transcode
Vasily7aea9266d05d
diff --git a/MediaBrowser.Api/Playback/BaseStreamingService.cs b/MediaBrowser.Api/Playback/BaseStreamingService.csindex 700883c963..2e9c1c3d4c 100644--- a/MediaBrowser.Api/Playback/BaseStreamingService.cs+++ b/MediaBrowser.Api/Playback/BaseStreamingService.cs@@ -297,12 +297,12 @@ namespace MediaBrowser.Api.Playback// Wait for the file to exist before proceeedingvar waitFor = state.WaitForPath ?? outputPath;- Logger.LogDebug("Waiting for the creation of '{0}'", waitFor);+ Logger.LogDebug("Waiting for the creation of {0}", waitFor);while (!File.Exists(waitFor) && !transcodingJob.HasExited){await Task.Delay(100, cancellationTokenSource.Token).ConfigureAwait(false);}- Logger.LogDebug("File '{0}' created or transcoding has finished", waitFor);+ Logger.LogDebug("File {0} created or transcoding has finished", waitFor);if (state.IsInputVideo && transcodingJob.Type == TranscodingJobType.Progressive && !transcodingJob.HasExited){diff --git a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.csindex 15dd5add4a..a06a2f84d9 100644--- a/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs+++ b/MediaBrowser.Api/Playback/Hls/DynamicHlsService.cs@@ -192,6 +192,7 @@ namespace MediaBrowser.Api.Playback.Hlsif (File.Exists(segmentPath)){job = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType);+ Logger.LogDebug("returning {0} [it exists, try 1]", segmentPath);return await GetSegmentResult(state, playlistPath, segmentPath, segmentExtension, requestedIndex, job, cancellationToken).ConfigureAwait(false);}@@ -207,6 +208,7 @@ namespace MediaBrowser.Api.Playback.Hlsjob = ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType);transcodingLock.Release();released = true;+ Logger.LogDebug("returning {0} [it exists, try 2]", segmentPath);return await GetSegmentResult(state, playlistPath, segmentPath, segmentExtension, requestedIndex, job, cancellationToken).ConfigureAwait(false);}else@@ -278,7 +280,7 @@ namespace MediaBrowser.Api.Playback.Hls// await Task.Delay(50, cancellationToken).ConfigureAwait(false);//}- Logger.LogInformation("returning {0}", segmentPath);+ Logger.LogDebug("returning {0} [general case]", segmentPath);job = job ?? ApiEntryPoint.Instance.OnTranscodeBeginRequest(playlistPath, TranscodingJobType);return await GetSegmentResult(state, playlistPath, segmentPath, segmentExtension, requestedIndex, job, cancellationToken).ConfigureAwait(false);}@@ -465,6 +467,7 @@ namespace MediaBrowser.Api.Playback.Hlsif (transcodingJob != null && transcodingJob.HasExited){// Transcoding job is over, so assume all existing files are ready+ Logger.LogDebug("serving up {0} as transcode is over", segmentPath);return await GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob).ConfigureAwait(false);}@@ -473,19 +476,21 @@ namespace MediaBrowser.Api.Playback.Hls// If requested segment is less than transcoding position, we can't transcode backwards, so assume it's readyif (segmentIndex < currentTranscodingIndex){+ Logger.LogDebug("serving up {0} as transcode index {1} is past requested point {2}", segmentPath, segmentIndex, currentTranscodingIndex);return await GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob).ConfigureAwait(false);}}var nextSegmentPath = GetSegmentPath(state, playlistPath, segmentIndex + 1);- while (!cancellationToken.IsCancellationRequested)+ while (!cancellationToken.IsCancellationRequested && transcodingJob != null && !transcodingJob.HasExited){// To be considered ready, the segment file has to exist AND- // either the transcoding job should be done or next segment should also exit+ // either the transcoding job should be done or next segment should also existif (segmentExists){- if ((transcodingJob != null && transcodingJob.HasExited) || File.Exists(nextSegmentPath))+ if (transcodingJob.HasExited || File.Exists(nextSegmentPath)){+ Logger.LogDebug("serving up {0} as it deemed ready", segmentPath);return await GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob).ConfigureAwait(false);}}@@ -501,6 +506,14 @@ namespace MediaBrowser.Api.Playback.Hls}cancellationToken.ThrowIfCancellationRequested();+ if (!File.Exists(segmentPath))+ {+ Logger.LogWarning("cannot serve {0} as transcoding quit before we got there", segmentPath);+ }+ else+ {+ Logger.LogDebug("serving {0} as it's on disk and transcoding stopped", segmentPath);+ }return await GetSegmentResult(state, segmentPath, segmentIndex, transcodingJob).ConfigureAwait(false);}@@ -514,6 +527,7 @@ namespace MediaBrowser.Api.Playback.HlsFileShare = FileShareMode.ReadWrite,OnComplete = () =>{+ Logger.LogDebug("finished serving {0}", segmentPath);if (transcodingJob != null){transcodingJob.DownloadPositionTicks = Math.Max(transcodingJob.DownloadPositionTicks ?? segmentEndingPositionTicks, segmentEndingPositionTicks);