The Free Software Media System - Server Backend & API
improve stopping of progressive streams
Luke Pulverenti24003580e797
diff --git a/MediaBrowser.Api/ApiEntryPoint.cs b/MediaBrowser.Api/ApiEntryPoint.csindex dbc1179e22..1a7f4a2b12 100644--- a/MediaBrowser.Api/ApiEntryPoint.cs+++ b/MediaBrowser.Api/ApiEntryPoint.cs@@ -349,7 +349,7 @@ namespace MediaBrowser.Apireturn;}- var timerDuration = 1000;+ var timerDuration = 10000;if (job.Type != TranscodingJobType.Progressive){diff --git a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.csindex 449100fc42..4649499c46 100644--- a/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs+++ b/MediaBrowser.Api/Playback/Progressive/BaseProgressiveStreamingService.cs@@ -142,7 +142,8 @@ namespace MediaBrowser.Api.Playback.Progressivevar outputPath = state.OutputFilePath;var outputPathExists = FileSystem.FileExists(outputPath);- var isTranscodeCached = outputPathExists && !ApiEntryPoint.Instance.HasActiveTranscodingJob(outputPath, TranscodingJobType.Progressive);+ var transcodingJob = ApiEntryPoint.Instance.GetTranscodingJob(outputPath, TranscodingJobType.Progressive);+ var isTranscodeCached = outputPathExists && transcodingJob != null;AddDlnaHeaders(state, responseHeaders, request.Static || isTranscodeCached);@@ -159,6 +160,7 @@ namespace MediaBrowser.Api.Playback.ProgressiveContentType = contentType,IsHeadRequest = isHeadRequest,Path = state.MediaPath+}).ConfigureAwait(false);}}@@ -170,13 +172,25 @@ namespace MediaBrowser.Api.Playback.Progressivetry{+ if (transcodingJob != null)+ {+ ApiEntryPoint.Instance.OnTranscodeBeginRequest(transcodingJob);+ }+return await ResultFactory.GetStaticFileResult(Request, new StaticFileResultOptions{ResponseHeaders = responseHeaders,ContentType = contentType,IsHeadRequest = isHeadRequest,Path = outputPath,- FileShare = FileShare.ReadWrite+ FileShare = FileShare.ReadWrite,+ OnComplete = () =>+ {+ if (transcodingJob != null)+ {+ ApiEntryPoint.Instance.OnTranscodeEndRequest(transcodingJob);+ }+ }}).ConfigureAwait(false);}@@ -348,7 +362,7 @@ namespace MediaBrowser.Api.Playback.ProgressiveoutputHeaders[item.Key] = item.Value;}- Func<Stream,Task> streamWriter = stream => new ProgressiveFileCopier(FileSystem, job, Logger).StreamFile(outputPath, stream, CancellationToken.None);+ Func<Stream, Task> streamWriter = stream => new ProgressiveFileCopier(FileSystem, job, Logger).StreamFile(outputPath, stream, CancellationToken.None);return ResultFactory.GetAsyncStreamWriter(streamWriter, outputHeaders);}diff --git a/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs b/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.csindex 2019b73c86..63d71b85ef 100644--- a/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs+++ b/MediaBrowser.Api/Playback/Progressive/ProgressiveStreamWriter.cs@@ -27,31 +27,41 @@ namespace MediaBrowser.Api.Playback.Progressivepublic async Task StreamFile(string path, Stream outputStream, CancellationToken cancellationToken){- var eofCount = 0;-- using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))+ try{- while (eofCount < 15)+ var eofCount = 0;++ using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true)){- var bytesRead = await CopyToAsyncInternal(fs, outputStream, BufferSize, cancellationToken).ConfigureAwait(false);+ while (eofCount < 15)+ {+ var bytesRead = await CopyToAsyncInternal(fs, outputStream, BufferSize, cancellationToken).ConfigureAwait(false);- //var position = fs.Position;- //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);+ //var position = fs.Position;+ //_logger.Debug("Streamed {0} bytes to position {1} from file {2}", bytesRead, position, path);- if (bytesRead == 0)- {- if (_job == null || _job.HasExited)+ if (bytesRead == 0){- eofCount++;+ if (_job == null || _job.HasExited)+ {+ eofCount++;+ }+ await Task.Delay(100, cancellationToken).ConfigureAwait(false);+ }+ else+ {+ eofCount = 0;}- await Task.Delay(100, cancellationToken).ConfigureAwait(false);- }- else- {- eofCount = 0;}}}+ finally+ {+ if (_job != null)+ {+ ApiEntryPoint.Instance.OnTranscodeEndRequest(_job);+ }+ }}private async Task<int> CopyToAsyncInternal(Stream source, Stream destination, Int32 bufferSize, CancellationToken cancellationToken)